Skip to content

Commit

Permalink
Rename lxd-agent to incus-agent
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
  • Loading branch information
stgraber committed Sep 5, 2023
1 parent 7040c48 commit 118c7d5
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 89 deletions.
4 changes: 2 additions & 2 deletions doc/examples/ubuntu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ files:
types:
- vm

- name: lxd-agent
generator: lxd-agent
- name: incus-agent
generator: incus-agent
types:
- vm

Expand Down
6 changes: 3 additions & 3 deletions doc/reference/generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Available generators are
* [`hosts`](#hosts)
* [`remove`](#remove)
* [`template`](#template)
* [`lxd-agent`](#lxd-agent)
* [`incus-agent`](#incus-agent)
* [`fstab`](#fstab)

In the image definition YAML, they are listed under `files`.
Expand Down Expand Up @@ -108,9 +108,9 @@ The `when` key can be one or more of:

See {ref}`lxd:image-format` in the LXD documentation for more information.

## `lxd-agent`
## `incus-agent`

This generator creates the `systemd` unit files which are needed to start the `lxd-agent` in LXD VMs.
This generator creates the `systemd` unit files which are needed to start the `incus-agent` in LXD VMs.

This comment has been minimized.

Copy link
@perlun

perlun Jan 23, 2024

Should the final part be "LXD VMs" or "Incus VMs"? 🤔

This comment has been minimized.

Copy link
@stgraber

stgraber Jan 23, 2024

Author Member

Yeah, that was fixed in a subsequent commit.

This comment has been minimized.

Copy link
@perlun

perlun Jan 24, 2024

Ok, cool! 🙏🏻


## `fstab`

Expand Down
21 changes: 12 additions & 9 deletions generators/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ type Generator interface {
}

var generators = map[string]func() generator{
"cloud-init": func() generator { return &cloudInit{} },
"copy": func() generator { return &copy{} },
"dump": func() generator { return &dump{} },
"fstab": func() generator { return &fstab{} },
"hostname": func() generator { return &hostname{} },
"hosts": func() generator { return &hosts{} },
"lxd-agent": func() generator { return &lxdAgent{} },
"remove": func() generator { return &remove{} },
"template": func() generator { return &template{} },
"cloud-init": func() generator { return &cloudInit{} },
"copy": func() generator { return &copy{} },
"dump": func() generator { return &dump{} },
"fstab": func() generator { return &fstab{} },
"hostname": func() generator { return &hostname{} },
"hosts": func() generator { return &hosts{} },
"incus-agent": func() generator { return &incusAgent{} },
"remove": func() generator { return &remove{} },
"template": func() generator { return &template{} },

// Legacy.
"lxd-agent": func() generator { return &incusAgent{} },
}

// Load loads and initializes a generator.
Expand Down
109 changes: 60 additions & 49 deletions generators/incus-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"github.com/lxc/distrobuilder/shared"
)

var lxdAgentSetupScript = `#!/bin/sh
var incusAgentSetupScript = `#!/bin/sh
set -eu
PREFIX="/run/lxd_agent"
PREFIX="/run/incus_agent"
# Functions.
mount_virtiofs() {
Expand Down Expand Up @@ -53,19 +53,26 @@ rmdir "${PREFIX}/.mnt"
# Fix up permissions.
chown -R root:root "${PREFIX}"
# Legacy.
if [ -e "${PREFIX}/lxd-agent" ]; then
ln -s lxd-agent "${PREFIX}"/incus-agent
fi
exit 0
`

type lxdAgent struct {
type incusAgent struct {
common
}

// RunLXC is not supported.
func (g *lxdAgent) RunLXC(img *image.LXCImage, target shared.DefinitionTargetLXC) error {
func (g *incusAgent) RunLXC(img *image.LXCImage, target shared.DefinitionTargetLXC) error {
return ErrNotSupported
}

// RunIncus creates systemd unit files for the lxd-agent.
func (g *lxdAgent) RunIncus(img *image.IncusImage, target shared.DefinitionTargetIncus) error {
// RunIncus creates systemd unit files for the agent.
func (g *incusAgent) RunIncus(img *image.IncusImage, target shared.DefinitionTargetIncus) error {
initFile := filepath.Join(g.sourceDir, "sbin", "init")

fi, err := os.Lstat(initFile)
Expand Down Expand Up @@ -94,28 +101,28 @@ func (g *lxdAgent) RunIncus(img *image.IncusImage, target shared.DefinitionTarge
}

// Run does nothing.
func (g *lxdAgent) Run() error {
func (g *incusAgent) Run() error {
return nil
}

func (g *lxdAgent) handleSystemd() error {
func (g *incusAgent) handleSystemd() error {
systemdPath := filepath.Join("/", "lib", "systemd")
if !incus.PathExists(filepath.Join(g.sourceDir, systemdPath)) {
systemdPath = filepath.Join("/", "usr", "lib", "systemd")
}

lxdAgentServiceUnit := fmt.Sprintf(`[Unit]
Description=LXD - agent
Documentation=https://documentation.ubuntu.com/lxd/en/latest/
ConditionPathExists=/dev/virtio-ports/org.linuxcontainers.lxd
incusAgentServiceUnit := fmt.Sprintf(`[Unit]
Description=Incus - agent
Documentation=https://linuxcontainers.org/incus/docs/main/
ConditionPathExistsGlob=/dev/virtio-ports/org.linuxcontainers.*
Before=cloud-init.target cloud-init.service cloud-init-local.service
DefaultDependencies=no
[Service]
Type=notify
WorkingDirectory=-/run/lxd_agent
ExecStartPre=%s/lxd-agent-setup
ExecStart=/run/lxd_agent/lxd-agent
WorkingDirectory=-/run/incus_agent
ExecStartPre=%s/incus-agent-setup
ExecStart=/run/incus_agent/incus-agent
Restart=on-failure
RestartSec=5s
StartLimitInterval=60
Expand All @@ -125,21 +132,21 @@ StartLimitBurst=10
WantedBy=multi-user.target
`, systemdPath)

path := filepath.Join(g.sourceDir, systemdPath, "system", "lxd-agent.service")
path := filepath.Join(g.sourceDir, systemdPath, "system", "incus-agent.service")

err := os.WriteFile(path, []byte(lxdAgentServiceUnit), 0644)
err := os.WriteFile(path, []byte(incusAgentServiceUnit), 0644)
if err != nil {
return fmt.Errorf("Failed to write file %q: %w", path, err)
}

err = os.Symlink(path, filepath.Join(g.sourceDir, "/etc/systemd/system/multi-user.target.wants/lxd-agent.service"))
err = os.Symlink(path, filepath.Join(g.sourceDir, "/etc/systemd/system/multi-user.target.wants/incus-agent.service"))
if err != nil {
return fmt.Errorf("Failed to create symlink %q: %w", filepath.Join(g.sourceDir, "/etc/systemd/system/multi-user.target.wants/lxd-agent.service"), err)
return fmt.Errorf("Failed to create symlink %q: %w", filepath.Join(g.sourceDir, "/etc/systemd/system/multi-user.target.wants/incus-agent.service"), err)
}

path = filepath.Join(g.sourceDir, systemdPath, "lxd-agent-setup")
path = filepath.Join(g.sourceDir, systemdPath, "incus-agent-setup")

err = os.WriteFile(path, []byte(lxdAgentSetupScript), 0755)
err = os.WriteFile(path, []byte(incusAgentSetupScript), 0755)
if err != nil {
return fmt.Errorf("Failed to write file %q: %w", path, err)
}
Expand All @@ -150,73 +157,77 @@ WantedBy=multi-user.target
udevPath = filepath.Join("/", "usr", "lib", "udev", "rules.d")
}

lxdAgentRules := `ACTION=="add", SYMLINK=="virtio-ports/org.linuxcontainers.lxd", TAG+="systemd"
SYMLINK=="virtio-ports/org.linuxcontainers.lxd", RUN+="/bin/systemctl start lxd-agent.service"
incusAgentRules := `ACTION=="add", SYMLINK=="virtio-ports/org.linuxcontainers.incus", TAG+="systemd"
SYMLINK=="virtio-ports/org.linuxcontainers.incus", RUN+="/bin/systemctl start incus-agent.service"
# Legacy.
ACTION=="add", SYMLINK=="virtio-ports/org.linuxcontainers.lxd", TAG+="systemd"
SYMLINK=="virtio-ports/org.linuxcontainers.lxd", RUN+="/bin/systemctl start incus-agent.service"
`
err = os.WriteFile(filepath.Join(g.sourceDir, udevPath, "99-lxd-agent.rules"), []byte(lxdAgentRules), 0400)
err = os.WriteFile(filepath.Join(g.sourceDir, udevPath, "99-incus-agent.rules"), []byte(incusAgentRules), 0400)
if err != nil {
return fmt.Errorf("Failed to write file %q: %w", filepath.Join(g.sourceDir, udevPath, "99-lxd-agent.rules"), err)
return fmt.Errorf("Failed to write file %q: %w", filepath.Join(g.sourceDir, udevPath, "99-incus-agent.rules"), err)
}

return nil
}

func (g *lxdAgent) handleOpenRC() error {
lxdAgentScript := `#!/sbin/openrc-run
func (g *incusAgent) handleOpenRC() error {
incusAgentScript := `#!/sbin/openrc-run
description="LXD - agent"
command=/run/lxd_agent/lxd-agent
description="Incus - agent"
command=/run/incus_agent/incus-agent
command_background=true
pidfile=/run/lxd-agent.pid
start_stop_daemon_args="--chdir /run/lxd_agent"
required_dirs=/run/lxd_agent
pidfile=/run/incus-agent.pid
start_stop_daemon_args="--chdir /run/incus_agent"
required_dirs=/run/incus_agent
depend() {
need lxd-agent-setup
after lxd-agent-setup
need incus-agent-setup
after incus-agent-setup
before cloud-init
before cloud-init-local
}
`

err := os.WriteFile(filepath.Join(g.sourceDir, "/etc/init.d/lxd-agent"), []byte(lxdAgentScript), 0755)
err := os.WriteFile(filepath.Join(g.sourceDir, "/etc/init.d/incus-agent"), []byte(incusAgentScript), 0755)
if err != nil {
return fmt.Errorf("Failed to write file %q: %w", filepath.Join(g.sourceDir, "/etc/init.d/lxd-agent"), err)
return fmt.Errorf("Failed to write file %q: %w", filepath.Join(g.sourceDir, "/etc/init.d/incus-agent"), err)
}

err = os.Symlink("/etc/init.d/lxd-agent", filepath.Join(g.sourceDir, "/etc/runlevels/default/lxd-agent"))
err = os.Symlink("/etc/init.d/incus-agent", filepath.Join(g.sourceDir, "/etc/runlevels/default/incus-agent"))
if err != nil {
return fmt.Errorf("Failed to create symlink %q: %w", filepath.Join(g.sourceDir, "/etc/runlevels/default/lxd-agent"), err)
return fmt.Errorf("Failed to create symlink %q: %w", filepath.Join(g.sourceDir, "/etc/runlevels/default/incus-agent"), err)
}

lxdConfigShareMountScript := `#!/sbin/openrc-run
incusConfigShareMountScript := `#!/sbin/openrc-run
description="LXD - agent - setup"
command=/usr/local/bin/lxd-agent-setup
required_files=/dev/virtio-ports/org.linuxcontainers.lxd
description="Incus - agent - setup"
command=/usr/local/bin/incus-agent-setup
required_dirs=/dev/virtio-ports/
`

err = os.WriteFile(filepath.Join(g.sourceDir, "/etc/init.d/lxd-agent-setup"), []byte(lxdConfigShareMountScript), 0755)
err = os.WriteFile(filepath.Join(g.sourceDir, "/etc/init.d/incus-agent-setup"), []byte(incusConfigShareMountScript), 0755)
if err != nil {
return fmt.Errorf("Failed to write file %q: %w", filepath.Join(g.sourceDir, "/etc/init.d/lxd-agent-setup"), err)
return fmt.Errorf("Failed to write file %q: %w", filepath.Join(g.sourceDir, "/etc/init.d/incus-agent-setup"), err)
}

err = os.Symlink("/etc/init.d/lxd-agent-setup", filepath.Join(g.sourceDir, "/etc/runlevels/default/lxd-agent-setup"))
err = os.Symlink("/etc/init.d/incus-agent-setup", filepath.Join(g.sourceDir, "/etc/runlevels/default/incus-agent-setup"))
if err != nil {
return fmt.Errorf("Failed to create symlink %q: %w", filepath.Join(g.sourceDir, "/etc/runlevels/default/lxd-agent-setup"), err)
return fmt.Errorf("Failed to create symlink %q: %w", filepath.Join(g.sourceDir, "/etc/runlevels/default/incus-agent-setup"), err)
}

path := filepath.Join(g.sourceDir, "/usr/local/bin", "lxd-agent-setup")
path := filepath.Join(g.sourceDir, "/usr/local/bin", "incus-agent-setup")

err = os.WriteFile(path, []byte(lxdAgentSetupScript), 0755)
err = os.WriteFile(path, []byte(incusAgentSetupScript), 0755)
if err != nil {
return fmt.Errorf("Failed to write file %q: %w", path, err)
}

return nil
}

func (g *lxdAgent) getInitSystemFromInittab() error {
func (g *incusAgent) getInitSystemFromInittab() error {
f, err := os.Open(filepath.Join(g.sourceDir, "etc", "inittab"))
if err != nil {
return fmt.Errorf("Failed to open file %q: %w", filepath.Join(g.sourceDir, "etc", "inittab"), err)
Expand Down
48 changes: 24 additions & 24 deletions image/incus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/lxc/distrobuilder/shared"
)

var lxdDef = shared.Definition{
var incusDef = shared.Definition{
Image: shared.DefinitionImage{
Description: "{{ image.distribution|capfirst }} {{ image. release }}",
Distribution: "ubuntu",
Expand All @@ -34,7 +34,7 @@ var lxdDef = shared.Definition{
},
}

func setupLXD(t *testing.T) *IncusImage {
func setupIncus(t *testing.T) *IncusImage {
cacheDir := filepath.Join(os.TempDir(), "distrobuilder-test")

err := os.MkdirAll(filepath.Join(cacheDir, "rootfs"), 0755)
Expand All @@ -43,41 +43,41 @@ func setupLXD(t *testing.T) *IncusImage {
err = os.MkdirAll(filepath.Join(cacheDir, "templates"), 0755)
require.NoError(t, err)

image := NewIncusImage(context.TODO(), cacheDir, "", cacheDir, lxdDef)
image := NewIncusImage(context.TODO(), cacheDir, "", cacheDir, incusDef)

fail := true
defer func() {
if fail {
teardownLXD(t)
teardownIncus(t)
}
}()

// Check cache directory
require.Equal(t, cacheDir, image.cacheDir)
require.Equal(t, lxdDef, image.definition)
require.Equal(t, incusDef, image.definition)

lxdDef.SetDefaults()
incusDef.SetDefaults()

err = lxdDef.Validate()
err = incusDef.Validate()
require.NoError(t, err)

fail = false
return image
}

func teardownLXD(t *testing.T) {
func teardownIncus(t *testing.T) {
os.RemoveAll(filepath.Join(os.TempDir(), "distrobuilder-test"))
}

func TestLXDBuild(t *testing.T) {
image := setupLXD(t)
defer teardownLXD(t)
func TestIncusBuild(t *testing.T) {
image := setupIncus(t)
defer teardownIncus(t)

testLXDBuildSplitImage(t, image)
testLXDBuildUnifiedImage(t, image)
testIncusBuildSplitImage(t, image)
testIncusBuildUnifiedImage(t, image)
}

func testLXDBuildSplitImage(t *testing.T, image *IncusImage) {
func testIncusBuildSplitImage(t *testing.T, image *IncusImage) {
// Create split tarball and squashfs.
imageFile, rootfsFile, err := image.Build(false, "xz", false)
require.NoError(t, err)
Expand All @@ -100,7 +100,7 @@ func testLXDBuildSplitImage(t *testing.T, image *IncusImage) {
os.Remove("rootfs.squashfs")
}

func testLXDBuildUnifiedImage(t *testing.T, image *IncusImage) {
func testIncusBuildUnifiedImage(t *testing.T, image *IncusImage) {
// Create unified tarball with custom name.
_, _, err := image.Build(true, "xz", false)
require.NoError(t, err)
Expand All @@ -123,9 +123,9 @@ func testLXDBuildUnifiedImage(t *testing.T, image *IncusImage) {
require.FileExists(t, "incus.tar.xz")
}

func TestLXDCreateMetadata(t *testing.T) {
image := setupLXD(t)
defer teardownLXD(t)
func TestIncusCreateMetadata(t *testing.T) {
image := setupIncus(t)
defer teardownIncus(t)

err := image.createMetadata()
require.NoError(t, err)
Expand All @@ -148,24 +148,24 @@ func TestLXDCreateMetadata(t *testing.T) {
{
"Properties[os]",
image.Metadata.Properties["os"],
lxdDef.Image.Distribution,
incusDef.Image.Distribution,
},
{
"Properties[release]",
image.Metadata.Properties["release"],
lxdDef.Image.Release,
incusDef.Image.Release,
},
{
"Properties[description]",
image.Metadata.Properties["description"],
fmt.Sprintf("%s %s", cases.Title(language.English).String(lxdDef.Image.Distribution),
lxdDef.Image.Release),
fmt.Sprintf("%s %s", cases.Title(language.English).String(incusDef.Image.Distribution),
incusDef.Image.Release),
},
{
"Properties[name]",
image.Metadata.Properties["name"],
fmt.Sprintf("%s-%s-%s-%s", strings.ToLower(lxdDef.Image.Distribution),
lxdDef.Image.Release, "x86_64", lxdDef.Image.Serial),
fmt.Sprintf("%s-%s-%s-%s", strings.ToLower(incusDef.Image.Distribution),
incusDef.Image.Release, "x86_64", incusDef.Image.Serial),
},
}

Expand Down

0 comments on commit 118c7d5

Please sign in to comment.