Skip to content

Commit 49abab0

Browse files
committed
Fixed hardcoded root partition device name sda2
The root partition may not be /dev/sda2, for example if set the boot disk as the second disk, the root partition device name will be /dev/sdb2. It may be /dev/vda2 if not using san disk. The fix figured out the root partition device UUID and set the UUID as the root, the root partition device UUID keeps no change. Signed-off-by: JUN JIE NAN <[email protected]>
1 parent a49adf1 commit 49abab0

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

distrobuilder/main_incus.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"io"
@@ -401,6 +402,13 @@ func (c *cmdIncus) run(cmd *cobra.Command, args []string, overlayDir string) err
401402

402403
c.global.logger.WithField("trigger", "post-files").Info("Running hooks")
403404

405+
// actionCtx := context.WithValue(c.global.ctx)
406+
actionEnviron := []string{}
407+
if rootfsDevUUID, err := vm.findRootfsDevUUID(); err != nil {
408+
return err
409+
} else {
410+
actionEnviron = append(actionEnviron, fmt.Sprintf("ROOTFS_DEVICE_UUID=%s", rootfsDevUUID))
411+
}
404412
// Run post files hook
405413
for _, action := range c.global.definition.GetRunnableActions("post-files", imageTargets) {
406414
if action.Pongo {
@@ -410,7 +418,8 @@ func (c *cmdIncus) run(cmd *cobra.Command, args []string, overlayDir string) err
410418
}
411419
}
412420

413-
err := shared.RunScript(c.global.ctx, action.Action)
421+
err := shared.RunScript(context.WithValue(c.global.ctx, "environ", actionEnviron),
422+
action.Action)
414423
if err != nil {
415424
{
416425
err := exitChroot()

distrobuilder/vm.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,31 @@ func (v *vm) getUEFIDevFile() string {
6161
return fmt.Sprintf("%sp1", v.loopDevice)
6262
}
6363

64+
func (v *vm) findRootfsDevUUID() (rootUUID string, err error) {
65+
rootfsDevFile := v.getRootfsDevFile()
66+
if rootfsDevFile == "" {
67+
err = fmt.Errorf("Failed to get rootfs device name.")
68+
return
69+
}
70+
var out strings.Builder
71+
if err = shared.RunCommand(v.ctx, nil, &out, "blkid", "-o", "export", rootfsDevFile); err != nil {
72+
err = fmt.Errorf("Failed to get rootfs device UUID: %w", err)
73+
return
74+
}
75+
fields := strings.Fields(out.String())
76+
for _, field := range fields {
77+
if strings.HasPrefix(field, "UUID") {
78+
rootUUID = field
79+
break
80+
}
81+
}
82+
if rootUUID == "" {
83+
err = fmt.Errorf("No rootfs device UUID found")
84+
return
85+
}
86+
return
87+
}
88+
6489
func (v *vm) createEmptyDiskImage() error {
6590
f, err := os.Create(v.imageFile)
6691
if err != nil {

doc/examples/ubuntu.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ actions:
322322
update-grub
323323
grub-install --uefi-secure-boot --target="${TARGET}-efi" --no-nvram --removable
324324
update-grub
325-
sed -i "s#root=[^ ]*#root=/dev/sda2#g" /boot/grub/grub.cfg
325+
sed -e "s#root=[^ ]*#root=${ROOTFS_DEVICE_UUID}#g" -i /boot/grub/grub.cfg
326326
types:
327327
- vm
328328

shared/util.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ func Copy(src, dest string) error {
5656
// 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.
5757
func RunCommand(ctx context.Context, stdin io.Reader, stdout io.Writer, name string, arg ...string) error {
5858
cmd := exec.CommandContext(ctx, name, arg...)
59+
if env, ok := ctx.Value("environ").([]string); ok && len(env) > 0 {
60+
cmd.Env = append(os.Environ(), env...)
61+
}
5962

6063
if stdin != nil {
6164
cmd.Stdin = stdin

0 commit comments

Comments
 (0)