File: main_forkmigrate.go

package info (click to toggle)
incus 6.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,392 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (81 lines) | stat: -rw-r--r-- 1,672 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main

import (
	"errors"
	"fmt"
	"os"
	"strconv"

	liblxc "gopkg.in/lxc/go-lxc.v2"
	"github.com/spf13/cobra"
)

type cmdForkmigrate struct {
	global *cmdGlobal
}

func (c *cmdForkmigrate) command() *cobra.Command {
	// Main subcommand
	cmd := &cobra.Command{}
	cmd.Use = "forkmigrate <container name> <containers path> <config> <images path> <preserve>"
	cmd.Short = "Restore the container from saved state"
	cmd.Long = `Description:
  Restore the container from saved state

  This internal command is used to start the container as a separate
  process, restoring its recorded state.
`
	cmd.RunE = c.run
	cmd.Hidden = true

	return cmd
}

func (c *cmdForkmigrate) run(cmd *cobra.Command, args []string) error {
	// Quick checks.
	if len(args) != 5 {
		_ = cmd.Help()

		if len(args) == 0 {
			return nil
		}

		return errors.New("Missing required arguments")
	}

	// Only root should run this
	if os.Geteuid() != 0 {
		return errors.New("This must be run as root")
	}

	name := args[0]
	lxcpath := args[1]
	configPath := args[2]
	imagesDir := args[3]

	preservesInodes, err := strconv.ParseBool(args[4])
	if err != nil {
		return err
	}

	d, err := liblxc.NewContainer(name, lxcpath)
	if err != nil {
		return err
	}

	err = d.LoadConfigFile(configPath)
	if err != nil {
		return fmt.Errorf("Failed loading config file %q: %w", configPath, err)
	}

	/* see https://github.com/golang/go/issues/13155, startContainer, and dc3a229 */
	_ = os.Stdin.Close()
	_ = os.Stdout.Close()
	_ = os.Stderr.Close()

	return d.Migrate(liblxc.MIGRATE_RESTORE, liblxc.MigrateOptions{
		Directory:       imagesDir,
		Verbose:         true,
		PreservesInodes: preservesInodes,
	})
}