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

lxd-imagebuilder: Export root partition device uuid as variable (from Incus) #83

Merged
merged 2 commits into from
Aug 6, 2024
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 doc/examples/ubuntu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ actions:
update-grub
grub-install --uefi-secure-boot --target="${TARGET}-efi" --no-nvram --removable
update-grub
sed -i "s#root=[^ ]*#root=/dev/sda2#g" /boot/grub/grub.cfg
sed -i "s#root=[^ ]*#root=${LXD_IMAGEBUILDER_ROOT_UUID}#g" /boot/grub/grub.cfg
types:
- vm

Expand Down
8 changes: 8 additions & 0 deletions lxd-imagebuilder/main_lxd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -350,6 +351,13 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string, overlayDir string) error
return fmt.Errorf("Failed to mount UEFI partition: %w", err)
}

rootUUID, err := vm.findRootfsDevUUID()
if err != nil {
return fmt.Errorf("Failed to find rootfs device UUID: %w", err)
}

c.global.ctx = context.WithValue(c.global.ctx, shared.ContextKeyEnviron, []string{fmt.Sprintf("%s=%s", shared.EnvRootUUID, rootUUID)})

// We cannot use LXD's 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)
Expand Down
26 changes: 26 additions & 0 deletions lxd-imagebuilder/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ func (v *vm) getUEFIDevFile() string {
return fmt.Sprintf("%sp1", v.loopDevice)
}

func (v *vm) findRootfsDevUUID() (rootUUID string, err error) {
rootfsDevFile := v.getRootfsDevFile()
if rootfsDevFile == "" {
return "", fmt.Errorf("Failed to get rootfs device name")
}

var out strings.Builder
if err = shared.RunCommand(v.ctx, nil, &out, "blkid", "-o", "export", rootfsDevFile); err != nil {
return "", fmt.Errorf("Failed to get rootfs device UUID: %w", err)
}

fields := strings.Fields(out.String())
for _, field := range fields {
if strings.HasPrefix(field, "UUID=") {
rootUUID = field
break
}
}

if rootUUID == "" {
return "", fmt.Errorf("No rootfs device UUID found")
}

return rootUUID, nil
}

func (v *vm) createEmptyDiskImage() error {
f, err := os.Create(v.imageFile)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions shared/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
yaml "gopkg.in/yaml.v2"
)

const (
ContextKeyEnviron = ContextKey("environ")
EnvRootUUID = "LXD_IMAGEBUILDER_ROOT_UUID"
)

// EnvVariable represents a environment variable.
type EnvVariable struct {
Value string
Expand All @@ -32,6 +37,9 @@ type EnvVariable struct {
// Environment represents a set of environment variables.
type Environment map[string]EnvVariable

// ContextKey type.
type ContextKey string

// Copy copies a file.
func Copy(src, dest string) error {
var err error
Expand Down Expand Up @@ -61,6 +69,10 @@ func Copy(src, dest string) error {
// RunCommand runs a command. Stdout is written to the given io.Writer. If nil, it's written to the real stdout. Stderr is always written to the real stderr.
func RunCommand(ctx context.Context, stdin io.Reader, stdout io.Writer, name string, arg ...string) error {
cmd := exec.CommandContext(ctx, name, arg...)
env, ok := ctx.Value(ContextKeyEnviron).([]string)
if ok && len(env) > 0 {
cmd.Env = append(os.Environ(), env...)
}

if stdin != nil {
cmd.Stdin = stdin
Expand Down
Loading