Skip to content

Commit

Permalink
Merge pull request #207 from stgraber/migrate
Browse files Browse the repository at this point in the history
lxd-to-incus: Handle mountpoints
  • Loading branch information
brauner authored Oct 30, 2023
2 parents cba17bd + 8b3fe7a commit fb8b527
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 95 deletions.
30 changes: 27 additions & 3 deletions cmd/lxd-to-incus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
92 changes: 0 additions & 92 deletions cmd/lxd-to-incus/targets.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
53 changes: 53 additions & 0 deletions cmd/lxd-to-incus/targets_openrc.go
Original file line number Diff line number Diff line change
@@ -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
}
53 changes: 53 additions & 0 deletions cmd/lxd-to-incus/targets_systemd.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit fb8b527

Please sign in to comment.