diff --git a/internal/usbgadget/changeset.go b/internal/usbgadget/changeset.go index 3a5ceaa..57f5d7d 100644 --- a/internal/usbgadget/changeset.go +++ b/internal/usbgadget/changeset.go @@ -48,7 +48,7 @@ var FileStateString = map[FileState]string{ FileStateFileContentMatch: "FILE_CONTENT_MATCH", FileStateFileWrite: "FILE_WRITE", FileStateMounted: "MOUNTED", - FileStateMountedConfigFS: "CONFIGFS_MOUNT", + FileStateMountedConfigFS: "CONFIGFS_MOUNTED", FileStateSymlink: "SYMLINK", FileStateSymlinkInOrderConfigFS: "SYMLINK_IN_ORDER_CONFIGFS", FileStateTouch: "TOUCH", @@ -155,6 +155,10 @@ func (f *RequestedFileChange) String() string { s = fmt.Sprintf("unknown expected state %d for %s", f.ExpectedState, f.Path) } + if len(f.Description) > 0 { + s += fmt.Sprintf(" (%s)", f.Description) + } + return s } diff --git a/internal/usbgadget/config.go b/internal/usbgadget/config.go index f4c9ce4..bdc1849 100644 --- a/internal/usbgadget/config.go +++ b/internal/usbgadget/config.go @@ -2,7 +2,6 @@ package usbgadget import ( "fmt" - "os" "os/exec" ) @@ -158,19 +157,9 @@ func (u *UsbGadget) OverrideGadgetConfig(itemKey string, itemAttr string, value } func mountConfigFS(path string) error { - _, err := os.Stat(path) - // TODO: check if it's mounted properly - if err == nil { - return nil - } - - if os.IsNotExist(err) { - err = exec.Command("mount", "-t", "configfs", "none", path).Run() - if err != nil { - return fmt.Errorf("failed to mount configfs: %w", err) - } - } else { - return fmt.Errorf("unable to access usb gadget path: %w", err) + err := exec.Command("mount", "-t", "configfs", "none", path).Run() + if err != nil { + return fmt.Errorf("failed to mount configfs: %w", err) } return nil } diff --git a/internal/usbgadget/config_tx.go b/internal/usbgadget/config_tx.go index be72487..ce98dd1 100644 --- a/internal/usbgadget/config_tx.go +++ b/internal/usbgadget/config_tx.go @@ -81,11 +81,12 @@ func (tx *UsbGadgetTransaction) addFileChange(component string, change Requested return key } -func (tx *UsbGadgetTransaction) mkdirAll(component string, path string, description string) string { +func (tx *UsbGadgetTransaction) mkdirAll(component string, path string, description string, deps []string) string { return tx.addFileChange(component, RequestedFileChange{ Path: path, ExpectedState: FileStateDirectory, Description: description, + DependsOn: deps, }) } @@ -131,14 +132,25 @@ func (tx *UsbGadgetTransaction) MountConfigFS() { } func (tx *UsbGadgetTransaction) CreateConfigPath() { - tx.mkdirAll("gadget", tx.configC1Path, "create config path") + tx.mkdirAll( + "gadget", + tx.configC1Path, + "create config path", + []string{configFSPath}, + ) } func (tx *UsbGadgetTransaction) WriteGadgetConfig() { // create kvm gadget path - tx.mkdirAll("gadget", tx.kvmGadgetPath, "create kvm gadget path") + tx.mkdirAll( + "gadget", + tx.kvmGadgetPath, + "create kvm gadget path", + []string{tx.configC1Path}, + ) deps := make([]string, 0) + deps = append(deps, tx.kvmGadgetPath) for _, val := range tx.orderedConfigItems { key := val.key @@ -188,7 +200,10 @@ func (tx *UsbGadgetTransaction) writeGadgetItemConfig(item gadgetConfigItem, dep files = append(files, deps...) gadgetItemPath := joinPath(tx.kvmGadgetPath, item.path) - files = append(files, tx.mkdirAll(component, gadgetItemPath, "create gadget item directory")) + if gadgetItemPath != tx.kvmGadgetPath { + gadgetItemDir := tx.mkdirAll(component, gadgetItemPath, "create gadget item directory", files) + files = append(files, gadgetItemDir) + } beforeChange := make([]string, 0) disableGadgetItemKey := fmt.Sprintf("disable-%s", item.device) @@ -231,7 +246,10 @@ func (tx *UsbGadgetTransaction) writeGadgetItemConfig(item gadgetConfigItem, dep // create config directory if configAttrs are set if len(item.configAttrs) > 0 { configItemPath := joinPath(tx.configC1Path, item.configPath) - tx.mkdirAll(component, configItemPath, "create config item directory") + if configItemPath != tx.configC1Path { + configItemDir := tx.mkdirAll(component, configItemPath, "create config item directory", files) + files = append(files, configItemDir) + } files = append(files, tx.writeGadgetAttrs( configItemPath, item.configAttrs,