Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change naming to Incus #764

Merged
merged 17 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .sphinx/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
myst_heading_anchors = 7

intersphinx_mapping = {
'lxd': ('https://documentation.ubuntu.com/lxd/en/latest/', None)
'incus': ('https://linuxcontainers.org/incus/docs/main/', None)
}

if os.path.exists("../doc/substitutions.yaml"):
Expand Down
1 change: 1 addition & 0 deletions .sphinx/wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ idmap
idmapped
idmaps
incrementing
Incus
InfiniBand
InfluxDB
init
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# distrobuilder
System container and VM image builder for LXC and LXD
System container and VM image builder for Incus and LXC.

Incus images may also be compatible with Canonical's LXD.

## Status
Type | Service | Status
Expand All @@ -11,22 +13,22 @@ Project status | CII Best Practices | [![CII Best Practices](https://bestpra
## Command line options

<!-- Include start CLI -->
The following are the command line options of `distrobuilder`. You can use `distrobuilder` to create container images for both LXC and LXD.
The following are the command line options of `distrobuilder`. You can use `distrobuilder` to create container images for both Incus and LXC.

```bash
$ distrobuilder
System container and VM image builder for LXC and LXD
System container and VM image builder for Incus and LXC

Usage:
distrobuilder [command]

Available Commands:
build-dir Build plain rootfs
build-incus Build Incus image from scratch
build-lxc Build LXC image from scratch
build-lxd Build LXD image from scratch
help Help about any command
pack-incus Create Incus image from existing rootfs
pack-lxc Create LXC image from existing rootfs
pack-lxd Create LXD image from existing rootfs
repack-windows Repack Windows ISO with drivers included

Flags:
Expand Down
33 changes: 19 additions & 14 deletions distrobuilder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import (
"strings"
"time"

lxd "github.com/canonical/lxd/shared"
incus "github.com/lxc/incus/shared"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -125,7 +125,7 @@ func main() {

app := &cobra.Command{
Use: "distrobuilder",
Short: "System container and VM image builder for LXC and LXD",
Short: "System container and VM image builder for LXC and Incus",
stgraber marked this conversation as resolved.
Show resolved Hide resolved
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Quick checks
if os.Geteuid() != 0 {
Expand Down Expand Up @@ -205,10 +205,10 @@ func main() {
app.AddCommand(LXCCmd.commandBuild())
app.AddCommand(LXCCmd.commandPack())

// LXD sub-commands
LXDCmd := cmdLXD{global: &globalCmd}
app.AddCommand(LXDCmd.commandBuild())
app.AddCommand(LXDCmd.commandPack())
// Incus sub-commands
IncusCmd := cmdIncus{global: &globalCmd}
app.AddCommand(IncusCmd.commandBuild())
app.AddCommand(IncusCmd.commandPack())

// build-dir sub-command
buildDirCmd := cmdBuildDir{global: &globalCmd}
Expand Down Expand Up @@ -306,7 +306,7 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Failed to get definition: %w", err)
}

// Create cache directory if we also plan on creating LXC or LXD images
// Create cache directory if we also plan on creating LXC or Incus images
if !isRunningBuildDir {
err = os.MkdirAll(c.flagCacheDir, 0755)
if err != nil {
Expand Down Expand Up @@ -355,7 +355,7 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
// only these sections will be processed.
imageTargets := shared.ImageTargetUndefined

// If we're running either build-lxc or build-lxd, include types which are
// If we're running either build-lxc or build-incus, include types which are
// meant for all.
if !isRunningBuildDir {
imageTargets |= shared.ImageTargetAll
Expand All @@ -365,9 +365,9 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
case "build-lxc":
// If we're running build-lxc, also process container-only sections.
imageTargets |= shared.ImageTargetContainer
case "build-lxd":
case "build-incus":
// Include either container-specific or vm-specific sections when
// running build-lxd.
// running build-incus.
ok, err := cmd.Flags().GetBool("vm")
if err != nil {
return fmt.Errorf(`Failed to get bool value of "vm": %w`, err)
Expand Down Expand Up @@ -608,7 +608,7 @@ func getDefinition(fname string, options []string) (*shared.Definition, error) {
// addSystemdGenerator creates a systemd-generator which runs on boot, and does some configuration around the system itself and networking.
func addSystemdGenerator() error {
// Check if container has systemd
if !lxd.PathExists("/etc/systemd") {
if !incus.PathExists("/etc/systemd") {
return nil
}

Expand All @@ -634,6 +634,11 @@ is_lxd_vm() {
[ -e /dev/virtio-ports/org.linuxcontainers.lxd ]
}

# is_incus_vm succeeds if we're running inside an Incus VM
is_incus_vm() {
[ -e /dev/virtio-ports/org.linuxcontainers.incus ]
}

# is_in_path succeeds if the given file exists in on of the paths
is_in_path() {
# Don't use $PATH as that may not include all relevant paths
Expand Down Expand Up @@ -754,10 +759,10 @@ EOF
}

## Main logic
# Nothing to do in LXD VM but deployed in case it is later converted to a container
is_lxd_vm && exit 0
# Nothing to do in Incus VM but deployed in case it is later converted to a container
is_incus_vm || is_lxd_vm && exit 0

# Exit immediately if not a LXC/LXD container
# Exit immediately if not an Incus/LXC container
is_lxc_container || exit 0

# Check for NetworkManager
Expand Down
78 changes: 37 additions & 41 deletions distrobuilder/main_lxd.go → distrobuilder/main_incus.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"os/exec"
"path/filepath"

client "github.com/canonical/lxd/client"
lxd "github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
client "github.com/lxc/incus/client"
incus "github.com/lxc/incus/shared"
"github.com/lxc/incus/shared/api"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"
Expand All @@ -21,30 +21,31 @@ import (
"github.com/lxc/distrobuilder/shared"
)

type cmdLXD struct {
type cmdIncus struct {
cmdBuild *cobra.Command
cmdPack *cobra.Command
global *cmdGlobal

flagType string
flagCompression string
flagVM bool
flagImportIntoLXD string
flagType string
flagCompression string
flagVM bool
flagImportIntoIncus string
}

func (c *cmdLXD) commandBuild() *cobra.Command {
func (c *cmdIncus) commandBuild() *cobra.Command {
c.cmdBuild = &cobra.Command{
Use: "build-lxd <filename|-> [target dir] [--type=TYPE] [--compression=COMPRESSION] [--import-into-lxd]",
Short: "Build LXD image from scratch",
Long: fmt.Sprintf(`Build LXD image from scratch
Use: "build-incus <filename|-> [target dir] [--type=TYPE] [--compression=COMPRESSION] [--import-into-incus]",
Aliases: []string{"build-lxd"},
Short: "Build Incus image from scratch",
Long: fmt.Sprintf(`Build Incus image from scratch

%s

%s
`, typeDescription, compressionDescription),
Args: cobra.RangeArgs(1, 2),
PreRunE: func(cmd *cobra.Command, args []string) error {
if !lxd.StringInSlice(c.flagType, []string{"split", "unified"}) {
if !incus.StringInSlice(c.flagType, []string{"split", "unified"}) {
return errors.New("--type needs to be one of ['split', 'unified']")
}

Expand Down Expand Up @@ -93,27 +94,26 @@ func (c *cmdLXD) commandBuild() *cobra.Command {
c.cmdBuild.Flags().StringVar(&c.flagType, "type", "split", "Type of tarball to create"+"``")
c.cmdBuild.Flags().StringVar(&c.flagCompression, "compression", "xz", "Type of compression to use"+"``")
c.cmdBuild.Flags().BoolVar(&c.flagVM, "vm", false, "Create a qcow2 image for VMs"+"``")
c.cmdBuild.Flags().StringVar(&c.flagImportIntoLXD, "import-into-lxd", "", "Import built image into LXD"+"``")
c.cmdBuild.Flags().Lookup("import-into-lxd").NoOptDefVal = "-"
c.cmdBuild.Flags().StringVar(&c.global.flagSourcesDir, "sources-dir", filepath.Join(os.TempDir(), "distrobuilder"), "Sources directory for distribution tarballs"+"``")
c.cmdBuild.Flags().StringVar(&c.flagImportIntoIncus, "import-into-incus", "", "Import built image into Incus"+"``")
c.cmdBuild.Flags().BoolVar(&c.global.flagKeepSources, "keep-sources", true, "Keep sources after build"+"``")

return c.cmdBuild
}

func (c *cmdLXD) commandPack() *cobra.Command {
func (c *cmdIncus) commandPack() *cobra.Command {
c.cmdPack = &cobra.Command{
Use: "pack-lxd <filename|-> <source dir> [target dir] [--type=TYPE] [--compression=COMPRESSION] [--import-into-lxd]",
Short: "Create LXD image from existing rootfs",
Long: fmt.Sprintf(`Create LXD image from existing rootfs
Use: "pack-incus <filename|-> <source dir> [target dir] [--type=TYPE] [--compression=COMPRESSION] [--import-into-incus]",
Aliases: []string{"pack-lxd"},
Short: "Create Incus image from existing rootfs",
Long: fmt.Sprintf(`Create Incus image from existing rootfs

%s

%s
`, typeDescription, compressionDescription),
Args: cobra.RangeArgs(2, 3),
PreRunE: func(cmd *cobra.Command, args []string) error {
if !lxd.StringInSlice(c.flagType, []string{"split", "unified"}) {
if !incus.StringInSlice(c.flagType, []string{"split", "unified"}) {
return errors.New("--type needs to be one of ['split', 'unified']")
}

Expand Down Expand Up @@ -171,13 +171,13 @@ func (c *cmdLXD) commandPack() *cobra.Command {
c.cmdPack.Flags().StringVar(&c.flagType, "type", "split", "Type of tarball to create")
c.cmdPack.Flags().StringVar(&c.flagCompression, "compression", "xz", "Type of compression to use")
c.cmdPack.Flags().BoolVar(&c.flagVM, "vm", false, "Create a qcow2 image for VMs"+"``")
c.cmdPack.Flags().StringVar(&c.flagImportIntoLXD, "import-into-lxd", "", "Import built image into LXD"+"``")
c.cmdPack.Flags().Lookup("import-into-lxd").NoOptDefVal = "-"
c.cmdPack.Flags().StringVar(&c.flagImportIntoIncus, "import-into-incus", "", "Import built image into Incus"+"``")
c.cmdPack.Flags().Lookup("import-into-incus").NoOptDefVal = "-"

return c.cmdPack
}

func (c *cmdLXD) runPack(cmd *cobra.Command, args []string, overlayDir string) error {
func (c *cmdIncus) runPack(cmd *cobra.Command, args []string, overlayDir string) error {
// Setup the mounts and chroot into the rootfs
exitChroot, err := shared.SetupChroot(overlayDir, *c.global.definition, nil)
if err != nil {
Expand Down Expand Up @@ -253,8 +253,8 @@ func (c *cmdLXD) runPack(cmd *cobra.Command, args []string, overlayDir string) e
return nil
}

func (c *cmdLXD) run(cmd *cobra.Command, args []string, overlayDir string) error {
img := image.NewLXDImage(c.global.ctx, overlayDir, c.global.targetDir,
func (c *cmdIncus) run(cmd *cobra.Command, args []string, overlayDir string) error {
img := image.NewIncusImage(c.global.ctx, overlayDir, c.global.targetDir,
c.global.flagCacheDir, *c.global.definition)

imageTargets := shared.ImageTargetUndefined | shared.ImageTargetAll
Expand All @@ -277,9 +277,9 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string, overlayDir string) error

c.global.logger.WithField("generator", file.Generator).Info("Running generator")

err = generator.RunLXD(img, c.global.definition.Targets.LXD)
err = generator.RunIncus(img, c.global.definition.Targets.Incus)
if err != nil {
return fmt.Errorf("Failed to create LXD data: %w", err)
return fmt.Errorf("Failed to create Incus data: %w", err)
}
}

Expand All @@ -303,7 +303,7 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string, overlayDir string) error

imgFile := filepath.Join(c.global.flagCacheDir, imgFilename)

vm, err = newVM(c.global.ctx, imgFile, vmDir, c.global.definition.Targets.LXD.VM.Filesystem, c.global.definition.Targets.LXD.VM.Size)
vm, err = newVM(c.global.ctx, imgFile, vmDir, c.global.definition.Targets.Incus.VM.Filesystem, c.global.definition.Targets.Incus.VM.Size)
if err != nil {
return fmt.Errorf("Failed to instantiate VM: %w", err)
}
Expand Down Expand Up @@ -351,7 +351,7 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string, overlayDir string) error
return fmt.Errorf("Failed to mount UEFI partition: %w", err)
}

// We cannot use LXD's rsync package as that uses the --delete flag which
// We cannot use Incus' rsync package as that uses the --delete flag which
// causes an issue due to the boot/efi directory being present.
err = shared.RsyncLocal(c.global.ctx, overlayDir+"/", vmDir)
if err != nil {
Expand Down Expand Up @@ -440,25 +440,21 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string, overlayDir string) error
}
}

c.global.logger.WithFields(logrus.Fields{"type": c.flagType, "vm": c.flagVM, "compression": c.flagCompression}).Info("Creating LXD image")
c.global.logger.WithFields(logrus.Fields{"type": c.flagType, "vm": c.flagVM, "compression": c.flagCompression}).Info("Creating Incus image")

imageFile, rootfsFile, err := img.Build(c.flagType == "unified", c.flagCompression, c.flagVM)
if err != nil {
return fmt.Errorf("Failed to create LXD image: %w", err)
return fmt.Errorf("Failed to create Incus image: %w", err)
}

importFlag := cmd.Flags().Lookup("import-into-lxd")
importFlag := cmd.Flags().Lookup("import-into-incus")

if importFlag.Changed {
path := ""

if lxd.PathExists("/var/snap/lxd/common/lxd") {
path = "/var/snap/lxd/common/lxd/unix.socket"
}

server, err := client.ConnectLXDUnix(path, nil)
server, err := client.ConnectIncusUnix(path, nil)
if err != nil {
return fmt.Errorf("Failed to connect to LXD: %w", err)
return fmt.Errorf("Failed to connect to Incus: %w", err)
}

image := api.ImagesPost{
Expand Down Expand Up @@ -510,7 +506,7 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string, overlayDir string) error
return fmt.Errorf("Failed to create image: %w", err)
}

// Don't create alias if the flag value is equal to the NoOptDefVal (the default value if --import-into-lxd flag is set without any value).
// Don't create alias if the flag value is equal to the NoOptDefVal (the default value if --import-into-incus flag is set without any value).
if importFlag.Value.String() == importFlag.NoOptDefVal {
return nil
}
Expand Down Expand Up @@ -539,7 +535,7 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string, overlayDir string) error
return nil
}

func (c *cmdLXD) checkVMDependencies() error {
func (c *cmdIncus) checkVMDependencies() error {
dependencies := []string{"btrfs", "mkfs.ext4", "mkfs.vfat", "qemu-img", "rsync", "sgdisk"}

for _, dep := range dependencies {
Expand Down
Loading
Loading