From dc8c05ad76b3ada43b864afed94c07f38d453ca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Sun, 29 Oct 2023 23:03:26 -0400 Subject: [PATCH 1/2] lxd-to-incus: Split the targets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Graber --- cmd/lxd-to-incus/targets.go | 92 ----------------------------- cmd/lxd-to-incus/targets_openrc.go | 53 +++++++++++++++++ cmd/lxd-to-incus/targets_systemd.go | 53 +++++++++++++++++ 3 files changed, 106 insertions(+), 92 deletions(-) create mode 100644 cmd/lxd-to-incus/targets_openrc.go create mode 100644 cmd/lxd-to-incus/targets_systemd.go diff --git a/cmd/lxd-to-incus/targets.go b/cmd/lxd-to-incus/targets.go index 0940e15970d..5ed94ba99c6 100644 --- a/cmd/lxd-to-incus/targets.go +++ b/cmd/lxd-to-incus/targets.go @@ -1,11 +1,7 @@ package main import ( - "time" - "github.com/lxc/incus/client" - "github.com/lxc/incus/shared/subprocess" - "github.com/lxc/incus/shared/util" ) type Target interface { @@ -17,91 +13,3 @@ type Target interface { } var targets = []Target{&targetSystemd{}, &targetOpenRC{}} - -type targetSystemd struct{} - -func (s *targetSystemd) Present() bool { - if !util.PathExists("/var/lib/incus/") { - return false - } - - _, err := subprocess.RunCommand("systemctl", "list-unit-files", "incus.service") - if err != nil { - return false - } - - return true -} - -func (s *targetSystemd) Stop() error { - _, err := subprocess.RunCommand("systemctl", "stop", "incus.service", "incus.socket") - return err -} - -func (s *targetSystemd) Start() error { - _, err := subprocess.RunCommand("systemctl", "start", "incus.service", "incus.socket") - if err != nil { - return err - } - - // Wait for the socket to become available. - time.Sleep(5 * time.Second) - - return nil -} - -func (s *targetSystemd) Connect() (incus.InstanceServer, error) { - return incus.ConnectIncusUnix("/var/lib/incus/unix.socket", nil) -} - -func (s *targetSystemd) Paths() (*DaemonPaths, error) { - return &DaemonPaths{ - Daemon: "/var/lib/incus/", - Logs: "/var/log/incus/", - Cache: "/var/cache/incus/", - }, nil -} - -type targetOpenRC struct{} - -func (s *targetOpenRC) Present() bool { - if !util.PathExists("/var/lib/incus/") { - return false - } - - _, err := subprocess.RunCommand("rc-service", "--exists", "incus") - if err != nil { - return false - } - - return true -} - -func (s *targetOpenRC) Stop() error { - _, err := subprocess.RunCommand("rc-service", "incus", "stop") - return err -} - -func (s *targetOpenRC) Start() error { - _, err := subprocess.RunCommand("rc-service", "incus", "start") - if err != nil { - return err - } - - // Wait for the socket to become available. - time.Sleep(5 * time.Second) - - return nil -} - -func (s *targetOpenRC) Connect() (incus.InstanceServer, error) { - return incus.ConnectIncusUnix("/var/lib/incus/unix.socket", nil) -} - -func (s *targetOpenRC) Paths() (*DaemonPaths, error) { - return &DaemonPaths{ - Daemon: "/var/lib/incus/", - Logs: "/var/log/incus/", - Cache: "/var/cache/incus/", - }, nil -} diff --git a/cmd/lxd-to-incus/targets_openrc.go b/cmd/lxd-to-incus/targets_openrc.go new file mode 100644 index 00000000000..78be0ebdfdc --- /dev/null +++ b/cmd/lxd-to-incus/targets_openrc.go @@ -0,0 +1,53 @@ +package main + +import ( + "time" + + "github.com/lxc/incus/client" + "github.com/lxc/incus/shared/subprocess" + "github.com/lxc/incus/shared/util" +) + +type targetOpenRC struct{} + +func (s *targetOpenRC) Present() bool { + if !util.PathExists("/var/lib/incus/") { + return false + } + + _, err := subprocess.RunCommand("rc-service", "--exists", "incus") + if err != nil { + return false + } + + return true +} + +func (s *targetOpenRC) Stop() error { + _, err := subprocess.RunCommand("rc-service", "incus", "stop") + return err +} + +func (s *targetOpenRC) Start() error { + _, err := subprocess.RunCommand("rc-service", "incus", "start") + if err != nil { + return err + } + + // Wait for the socket to become available. + time.Sleep(5 * time.Second) + + return nil +} + +func (s *targetOpenRC) Connect() (incus.InstanceServer, error) { + return incus.ConnectIncusUnix("/var/lib/incus/unix.socket", nil) +} + +func (s *targetOpenRC) Paths() (*DaemonPaths, error) { + return &DaemonPaths{ + Daemon: "/var/lib/incus/", + Logs: "/var/log/incus/", + Cache: "/var/cache/incus/", + }, nil +} diff --git a/cmd/lxd-to-incus/targets_systemd.go b/cmd/lxd-to-incus/targets_systemd.go new file mode 100644 index 00000000000..a3759a5d9fa --- /dev/null +++ b/cmd/lxd-to-incus/targets_systemd.go @@ -0,0 +1,53 @@ +package main + +import ( + "time" + + "github.com/lxc/incus/client" + "github.com/lxc/incus/shared/subprocess" + "github.com/lxc/incus/shared/util" +) + +type targetSystemd struct{} + +func (s *targetSystemd) Present() bool { + if !util.PathExists("/var/lib/incus/") { + return false + } + + _, err := subprocess.RunCommand("systemctl", "list-unit-files", "incus.service") + if err != nil { + return false + } + + return true +} + +func (s *targetSystemd) Stop() error { + _, err := subprocess.RunCommand("systemctl", "stop", "incus.service", "incus.socket") + return err +} + +func (s *targetSystemd) Start() error { + _, err := subprocess.RunCommand("systemctl", "start", "incus.service", "incus.socket") + if err != nil { + return err + } + + // Wait for the socket to become available. + time.Sleep(5 * time.Second) + + return nil +} + +func (s *targetSystemd) Connect() (incus.InstanceServer, error) { + return incus.ConnectIncusUnix("/var/lib/incus/unix.socket", nil) +} + +func (s *targetSystemd) Paths() (*DaemonPaths, error) { + return &DaemonPaths{ + Daemon: "/var/lib/incus/", + Logs: "/var/log/incus/", + Cache: "/var/cache/incus/", + }, nil +} From 8b3fe7abf85606fb37a810808445eeac835ae4cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Sun, 29 Oct 2023 23:18:46 -0400 Subject: [PATCH 2/2] lxd-to-incus: Handle mountpoint on daemon path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Graber --- cmd/lxd-to-incus/main.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/cmd/lxd-to-incus/main.go b/cmd/lxd-to-incus/main.go index d91ed76f4ad..338d8385960 100644 --- a/cmd/lxd-to-incus/main.go +++ b/cmd/lxd-to-incus/main.go @@ -16,6 +16,7 @@ import ( "github.com/lxc/incus/client" cli "github.com/lxc/incus/internal/cmd" + "github.com/lxc/incus/internal/linux" "github.com/lxc/incus/internal/version" incusAPI "github.com/lxc/incus/shared/api" "github.com/lxc/incus/shared/subprocess" @@ -668,9 +669,32 @@ Instead this tool will be providing specific commands for each of the servers. return fmt.Errorf("Failed to move %q to %q: %w", sourcePaths.Cache, targetPaths.Cache, err) } - _, err = subprocess.RunCommand("mv", sourcePaths.Daemon, targetPaths.Daemon) - if err != nil { - return fmt.Errorf("Failed to move %q to %q: %w", sourcePaths.Daemon, targetPaths.Daemon, err) + if linux.IsMountPoint(sourcePaths.Daemon) { + err = os.MkdirAll(targetPaths.Daemon, 0711) + if err != nil { + return fmt.Errorf("Failed to create target directory: %w", err) + } + + err = unix.Mount(sourcePaths.Daemon, targetPaths.Daemon, "none", unix.MS_BIND|unix.MS_REC, "") + if err != nil { + return fmt.Errorf("Failed to bind mount %q to %q: %w", sourcePaths.Daemon, targetPaths.Daemon, err) + } + + err = unix.Unmount(sourcePaths.Daemon, unix.MNT_DETACH) + if err != nil { + return fmt.Errorf("Failed to unmount source mount %q: %w", sourcePaths.Daemon, err) + } + + fmt.Println("") + fmt.Printf("WARNING: %s was detected to be a mountpoint.\n", sourcePaths.Daemon) + fmt.Printf("The migration logic has moved this mount to the new target path at %s.\n", targetPaths.Daemon) + fmt.Printf("However it is your responsability to modify your system settings to ensure this mount will be properly restored on reboot.\n") + fmt.Println("") + } else { + _, err = subprocess.RunCommand("mv", sourcePaths.Daemon, targetPaths.Daemon) + if err != nil { + return fmt.Errorf("Failed to move %q to %q: %w", sourcePaths.Daemon, targetPaths.Daemon, err) + } } // Migrate database format.