diff --git a/lxd/network/openvswitch/shared.go b/lxd/network/openvswitch/shared.go index d7be93b5dd81..9ee8e9fac4f2 100644 --- a/lxd/network/openvswitch/shared.go +++ b/lxd/network/openvswitch/shared.go @@ -1,6 +1,8 @@ package openvswitch import ( + "fmt" + "os" "regexp" "strconv" "strings" @@ -9,6 +11,18 @@ import ( // MicroovnTargetChassisPath matches the path of the "ovn-chassis" content interface target with MicroOVN. var MicroovnTargetChassisPath = regexp.MustCompile(`^/var/snap/lxd/[0-9]+/microovn/chassis/switch`) +// vsctl either returns the "ovs-vsctl" command if OVN is built in with LXD +// or if MicroOVN is installed and the 'ovn-chassis' content interface is connected, +// it returns "ovs-vsctl" with its southbound db unix socket. +func vsctl(args ...string) (string, []string) { + targetPath, _ := os.Readlink("/run/openvswitch") + if !MicroovnTargetChassisPath.MatchString(targetPath) { + return "ovs-vsctl", args + } + + return "ovs-vsctl", append([]string{fmt.Sprintf("--db=unix:%s/db.sock", targetPath)}, args...) +} + // unquote passes s through strconv.Unquote if the first character is a ", otherwise returns s unmodified. // This is useful as openvswitch's tools can sometimes return values double quoted if they start with a number. func unquote(s string) (string, error) {