fix: configFS might not be mounted if the directory exists (#479)

This commit is contained in:
Aveline 2025-05-19 23:59:02 +02:00 committed by GitHub
parent 8cf6b40dc3
commit eeb103adf9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 20 deletions

View File

@ -48,7 +48,7 @@ var FileStateString = map[FileState]string{
FileStateFileContentMatch: "FILE_CONTENT_MATCH", FileStateFileContentMatch: "FILE_CONTENT_MATCH",
FileStateFileWrite: "FILE_WRITE", FileStateFileWrite: "FILE_WRITE",
FileStateMounted: "MOUNTED", FileStateMounted: "MOUNTED",
FileStateMountedConfigFS: "CONFIGFS_MOUNT", FileStateMountedConfigFS: "CONFIGFS_MOUNTED",
FileStateSymlink: "SYMLINK", FileStateSymlink: "SYMLINK",
FileStateSymlinkInOrderConfigFS: "SYMLINK_IN_ORDER_CONFIGFS", FileStateSymlinkInOrderConfigFS: "SYMLINK_IN_ORDER_CONFIGFS",
FileStateTouch: "TOUCH", FileStateTouch: "TOUCH",
@ -155,6 +155,10 @@ func (f *RequestedFileChange) String() string {
s = fmt.Sprintf("unknown expected state %d for %s", f.ExpectedState, f.Path) 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 return s
} }

View File

@ -2,7 +2,6 @@ package usbgadget
import ( import (
"fmt" "fmt"
"os"
"os/exec" "os/exec"
) )
@ -158,19 +157,9 @@ func (u *UsbGadget) OverrideGadgetConfig(itemKey string, itemAttr string, value
} }
func mountConfigFS(path string) error { func mountConfigFS(path string) error {
_, err := os.Stat(path) err := exec.Command("mount", "-t", "configfs", "none", path).Run()
// TODO: check if it's mounted properly if err != nil {
if err == nil { return fmt.Errorf("failed to mount configfs: %w", err)
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)
} }
return nil return nil
} }

View File

@ -81,11 +81,12 @@ func (tx *UsbGadgetTransaction) addFileChange(component string, change Requested
return key 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{ return tx.addFileChange(component, RequestedFileChange{
Path: path, Path: path,
ExpectedState: FileStateDirectory, ExpectedState: FileStateDirectory,
Description: description, Description: description,
DependsOn: deps,
}) })
} }
@ -131,14 +132,25 @@ func (tx *UsbGadgetTransaction) MountConfigFS() {
} }
func (tx *UsbGadgetTransaction) CreateConfigPath() { 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() { func (tx *UsbGadgetTransaction) WriteGadgetConfig() {
// create kvm gadget path // 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 := make([]string, 0)
deps = append(deps, tx.kvmGadgetPath)
for _, val := range tx.orderedConfigItems { for _, val := range tx.orderedConfigItems {
key := val.key key := val.key
@ -188,7 +200,10 @@ func (tx *UsbGadgetTransaction) writeGadgetItemConfig(item gadgetConfigItem, dep
files = append(files, deps...) files = append(files, deps...)
gadgetItemPath := joinPath(tx.kvmGadgetPath, item.path) 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) beforeChange := make([]string, 0)
disableGadgetItemKey := fmt.Sprintf("disable-%s", item.device) 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 // create config directory if configAttrs are set
if len(item.configAttrs) > 0 { if len(item.configAttrs) > 0 {
configItemPath := joinPath(tx.configC1Path, item.configPath) 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( files = append(files, tx.writeGadgetAttrs(
configItemPath, configItemPath,
item.configAttrs, item.configAttrs,