diff --git a/internal/usbgadget/changeset_resolver.go b/internal/usbgadget/changeset_resolver.go index 9369daf..67812e0 100644 --- a/internal/usbgadget/changeset_resolver.go +++ b/internal/usbgadget/changeset_resolver.go @@ -48,6 +48,11 @@ func (c *ChangeSetResolver) doResolveChanges(initial bool) error { for _, key := range c.orderedChanges { change := c.changesMap[key.(string)] + if change == nil { + c.l.Error().Str("key", key.(string)).Msg("fileChange not found") + continue + } + if !initial { change.ResetActionResolution() } @@ -123,7 +128,11 @@ func (c *ChangeSetResolver) applyChanges() error { err := c.changeset.applyChange(change) if err != nil { - return err + if change.IgnoreErrors { + c.l.Warn().Str("change", change.String()).Err(err).Msg("ignoring error") + } else { + return err + } } } diff --git a/internal/usbgadget/config.go b/internal/usbgadget/config.go index bdc1849..1c4f9c3 100644 --- a/internal/usbgadget/config.go +++ b/internal/usbgadget/config.go @@ -177,12 +177,7 @@ func (u *UsbGadget) Init() error { u.udc = udcs[0] - err := u.WithTransaction(func() error { - u.tx.MountConfigFS() - u.tx.CreateConfigPath() - u.tx.WriteGadgetConfig() - return nil - }) + err := u.configureUsbGadget(false) if err != nil { return u.logError("unable to initialize USB stack", err) } @@ -196,13 +191,22 @@ func (u *UsbGadget) UpdateGadgetConfig() error { u.loadGadgetConfig() - err := u.WithTransaction(func() error { - u.tx.WriteGadgetConfig() - return nil - }) + err := u.configureUsbGadget(true) if err != nil { return u.logError("unable to update gadget config", err) } return nil } + +func (u *UsbGadget) configureUsbGadget(resetUsb bool) error { + return u.WithTransaction(func() error { + u.tx.MountConfigFS() + u.tx.CreateConfigPath() + u.tx.WriteGadgetConfig() + if resetUsb { + u.tx.RebindUsb(true) + } + return nil + }) +} diff --git a/internal/usbgadget/config_tx.go b/internal/usbgadget/config_tx.go index ce98dd1..df8a3d1 100644 --- a/internal/usbgadget/config_tx.go +++ b/internal/usbgadget/config_tx.go @@ -319,6 +319,7 @@ func (tx *UsbGadgetTransaction) WriteUDC() { // bound the gadget to a UDC (USB Device Controller) path := path.Join(tx.kvmGadgetPath, "UDC") tx.addFileChange("udc", RequestedFileChange{ + Key: "udc", Path: path, ExpectedState: FileStateFileContentMatch, ExpectedContent: []byte(tx.udc), @@ -334,6 +335,8 @@ func (tx *UsbGadgetTransaction) RebindUsb(ignoreUnbindError bool) { ExpectedState: FileStateFileWrite, ExpectedContent: []byte(tx.udc), Description: "unbind UDC", + DependsOn: []string{"udc"}, + IgnoreErrors: ignoreUnbindError, }) // bind the gadget to the UDC tx.addFileChange("udc", RequestedFileChange{ @@ -342,6 +345,5 @@ func (tx *UsbGadgetTransaction) RebindUsb(ignoreUnbindError bool) { ExpectedContent: []byte(tx.udc), Description: "bind UDC", DependsOn: []string{path.Join(tx.dwc3Path, "unbind")}, - IgnoreErrors: ignoreUnbindError, }) }