Skip to content

Commit eeb103a

Browse files
authored
fix: configFS might not be mounted if the directory exists (#479)
1 parent 8cf6b40 commit eeb103a

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

internal/usbgadget/changeset.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var FileStateString = map[FileState]string{
4848
FileStateFileContentMatch: "FILE_CONTENT_MATCH",
4949
FileStateFileWrite: "FILE_WRITE",
5050
FileStateMounted: "MOUNTED",
51-
FileStateMountedConfigFS: "CONFIGFS_MOUNT",
51+
FileStateMountedConfigFS: "CONFIGFS_MOUNTED",
5252
FileStateSymlink: "SYMLINK",
5353
FileStateSymlinkInOrderConfigFS: "SYMLINK_IN_ORDER_CONFIGFS",
5454
FileStateTouch: "TOUCH",
@@ -155,6 +155,10 @@ func (f *RequestedFileChange) String() string {
155155
s = fmt.Sprintf("unknown expected state %d for %s", f.ExpectedState, f.Path)
156156
}
157157

158+
if len(f.Description) > 0 {
159+
s += fmt.Sprintf(" (%s)", f.Description)
160+
}
161+
158162
return s
159163
}
160164

internal/usbgadget/config.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package usbgadget
22

33
import (
44
"fmt"
5-
"os"
65
"os/exec"
76
)
87

@@ -158,19 +157,9 @@ func (u *UsbGadget) OverrideGadgetConfig(itemKey string, itemAttr string, value
158157
}
159158

160159
func mountConfigFS(path string) error {
161-
_, err := os.Stat(path)
162-
// TODO: check if it's mounted properly
163-
if err == nil {
164-
return nil
165-
}
166-
167-
if os.IsNotExist(err) {
168-
err = exec.Command("mount", "-t", "configfs", "none", path).Run()
169-
if err != nil {
170-
return fmt.Errorf("failed to mount configfs: %w", err)
171-
}
172-
} else {
173-
return fmt.Errorf("unable to access usb gadget path: %w", err)
160+
err := exec.Command("mount", "-t", "configfs", "none", path).Run()
161+
if err != nil {
162+
return fmt.Errorf("failed to mount configfs: %w", err)
174163
}
175164
return nil
176165
}

internal/usbgadget/config_tx.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ func (tx *UsbGadgetTransaction) addFileChange(component string, change Requested
8181
return key
8282
}
8383

84-
func (tx *UsbGadgetTransaction) mkdirAll(component string, path string, description string) string {
84+
func (tx *UsbGadgetTransaction) mkdirAll(component string, path string, description string, deps []string) string {
8585
return tx.addFileChange(component, RequestedFileChange{
8686
Path: path,
8787
ExpectedState: FileStateDirectory,
8888
Description: description,
89+
DependsOn: deps,
8990
})
9091
}
9192

@@ -131,14 +132,25 @@ func (tx *UsbGadgetTransaction) MountConfigFS() {
131132
}
132133

133134
func (tx *UsbGadgetTransaction) CreateConfigPath() {
134-
tx.mkdirAll("gadget", tx.configC1Path, "create config path")
135+
tx.mkdirAll(
136+
"gadget",
137+
tx.configC1Path,
138+
"create config path",
139+
[]string{configFSPath},
140+
)
135141
}
136142

137143
func (tx *UsbGadgetTransaction) WriteGadgetConfig() {
138144
// create kvm gadget path
139-
tx.mkdirAll("gadget", tx.kvmGadgetPath, "create kvm gadget path")
145+
tx.mkdirAll(
146+
"gadget",
147+
tx.kvmGadgetPath,
148+
"create kvm gadget path",
149+
[]string{tx.configC1Path},
150+
)
140151

141152
deps := make([]string, 0)
153+
deps = append(deps, tx.kvmGadgetPath)
142154

143155
for _, val := range tx.orderedConfigItems {
144156
key := val.key
@@ -188,7 +200,10 @@ func (tx *UsbGadgetTransaction) writeGadgetItemConfig(item gadgetConfigItem, dep
188200
files = append(files, deps...)
189201

190202
gadgetItemPath := joinPath(tx.kvmGadgetPath, item.path)
191-
files = append(files, tx.mkdirAll(component, gadgetItemPath, "create gadget item directory"))
203+
if gadgetItemPath != tx.kvmGadgetPath {
204+
gadgetItemDir := tx.mkdirAll(component, gadgetItemPath, "create gadget item directory", files)
205+
files = append(files, gadgetItemDir)
206+
}
192207

193208
beforeChange := make([]string, 0)
194209
disableGadgetItemKey := fmt.Sprintf("disable-%s", item.device)
@@ -231,7 +246,10 @@ func (tx *UsbGadgetTransaction) writeGadgetItemConfig(item gadgetConfigItem, dep
231246
// create config directory if configAttrs are set
232247
if len(item.configAttrs) > 0 {
233248
configItemPath := joinPath(tx.configC1Path, item.configPath)
234-
tx.mkdirAll(component, configItemPath, "create config item directory")
249+
if configItemPath != tx.configC1Path {
250+
configItemDir := tx.mkdirAll(component, configItemPath, "create config item directory", files)
251+
files = append(files, configItemDir)
252+
}
235253
files = append(files, tx.writeGadgetAttrs(
236254
configItemPath,
237255
item.configAttrs,

0 commit comments

Comments
 (0)