Skip to content

Commit c120d62

Browse files
authored
Merge pull request #1330 from stgraber/main
incusd/device/nic/bridged: Handle invalid configuration
2 parents ef534bc + 0d31b2a commit c120d62

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

internal/server/device/nic_bridged.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,12 @@ func (d *nicBridged) Stop() (*deviceConfig.RunConfig, error) {
813813

814814
// postStop is run after the device is removed from the instance.
815815
func (d *nicBridged) postStop() error {
816+
// Handle the case where validation fails but the device still must be removed.
817+
bridgeName := d.config["parent"]
818+
if bridgeName == "" && d.config["network"] != "" {
819+
bridgeName = d.config["network"]
820+
}
821+
816822
defer func() {
817823
_ = d.volatileSet(map[string]string{
818824
"host_name": "",
@@ -825,9 +831,9 @@ func (d *nicBridged) postStop() error {
825831

826832
if d.config["host_name"] != "" && network.InterfaceExists(d.config["host_name"]) {
827833
// Detach host-side end of veth pair from bridge (required for openvswitch particularly).
828-
err := network.DetachInterface(d.state, d.config["parent"], d.config["host_name"])
834+
err := network.DetachInterface(d.state, bridgeName, d.config["host_name"])
829835
if err != nil {
830-
return fmt.Errorf("Failed to detach interface %q from %q: %w", d.config["host_name"], d.config["parent"], err)
836+
return fmt.Errorf("Failed to detach interface %q from %q: %w", d.config["host_name"], bridgeName, err)
831837
}
832838

833839
// Removing host-side end of veth pair will delete the peer end too.
@@ -843,7 +849,7 @@ func (d *nicBridged) postStop() error {
843849
routes = append(routes, util.SplitNTrimSpace(d.config["ipv6.routes"], ",", -1, true)...)
844850
routes = append(routes, util.SplitNTrimSpace(d.config["ipv4.routes.external"], ",", -1, true)...)
845851
routes = append(routes, util.SplitNTrimSpace(d.config["ipv6.routes.external"], ",", -1, true)...)
846-
networkNICRouteDelete(d.config["parent"], routes...)
852+
networkNICRouteDelete(bridgeName, routes...)
847853

848854
if util.IsTrue(d.config["security.mac_filtering"]) || util.IsTrue(d.config["security.ipv4_filtering"]) || util.IsTrue(d.config["security.ipv6_filtering"]) {
849855
d.removeFilters(d.config)
@@ -854,25 +860,31 @@ func (d *nicBridged) postStop() error {
854860

855861
// Remove is run when the device is removed from the instance or the instance is deleted.
856862
func (d *nicBridged) Remove() error {
857-
if d.config["parent"] != "" {
863+
// Handle the case where validation fails but the device still must be removed.
864+
bridgeName := d.config["parent"]
865+
if bridgeName == "" && d.config["network"] != "" {
866+
bridgeName = d.config["network"]
867+
}
868+
869+
if bridgeName != "" {
858870
dnsmasq.ConfigMutex.Lock()
859871
defer dnsmasq.ConfigMutex.Unlock()
860872

861-
if network.InterfaceExists(d.config["parent"]) {
862-
err := d.networkClearLease(d.inst.Name(), d.config["parent"], d.config["hwaddr"], clearLeaseAll)
873+
if network.InterfaceExists(bridgeName) {
874+
err := d.networkClearLease(d.inst.Name(), bridgeName, d.config["hwaddr"], clearLeaseAll)
863875
if err != nil {
864876
return fmt.Errorf("Failed clearing leases: %w", err)
865877
}
866878
}
867879

868880
// Remove dnsmasq config if it exists (doesn't return error if file is missing).
869-
err := dnsmasq.RemoveStaticEntry(d.config["parent"], d.inst.Project().Name, d.inst.Name(), d.Name())
881+
err := dnsmasq.RemoveStaticEntry(bridgeName, d.inst.Project().Name, d.inst.Name(), d.Name())
870882
if err != nil {
871883
return err
872884
}
873885

874886
// Reload dnsmasq to apply new settings if dnsmasq is running.
875-
err = dnsmasq.Kill(d.config["parent"], true)
887+
err = dnsmasq.Kill(bridgeName, true)
876888
if err != nil {
877889
return err
878890
}

0 commit comments

Comments
 (0)