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

Fixed hardcoded root partition device name sda2 #830

Merged
merged 1 commit into from
Apr 7, 2024
Merged
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
8 changes: 8 additions & 0 deletions distrobuilder/main_incus.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"errors"
"fmt"
"io"
@@ -352,6 +353,13 @@ func (c *cmdIncus) run(cmd *cobra.Command, args []string, overlayDir string) err
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 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)
29 changes: 29 additions & 0 deletions distrobuilder/vm.go
Original file line number Diff line number Diff line change
@@ -61,6 +61,35 @@ func (v *vm) getUEFIDevFile() string {
return fmt.Sprintf("%sp1", v.loopDevice)
}

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

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

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

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

return
}

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

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

// EnvVariable represents a environment variable.
type EnvVariable struct {
Value string
@@ -27,6 +32,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
@@ -56,6 +64,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