Skip to content

Commit

Permalink
Fixed hardcoded root partition device name sda2
Browse files Browse the repository at this point in the history
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 <nanjunjie@gmail.com>
  • Loading branch information
nanjj committed Apr 2, 2024
1 parent a7a0d8d commit a6af75a
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 26 deletions.
10 changes: 6 additions & 4 deletions images/almalinux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ actions:
target="$(readlink -f /etc/grub2-efi.cfg)"
grub2-mkconfig -o "${target}"
sed -i "s#root=[^ ]*#root=/dev/sda2#g" "${target}"
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i "${target}"
types:
- vm
releases:
Expand All @@ -424,11 +424,13 @@ actions:
# Create grub.cfg file
grub2-mkconfig -o "${target}"
sed -i "s#root=[^ ]*#root=/dev/sda2#g" "${target}"
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i "${target}"
# Update files in /boot/loader/entries/. `grubby` needs to be run after
# `grub2-mkconfig` as the latter overwrites files in /boot/loader/entries/.
grubby --update-kernel=/boot/vmlinuz-${kver} --args="root=/dev/sda2 ro"
grubby --update-kernel=/boot/vmlinuz-${kver} --args="root=${rootdevuuid} ro"
# Regenerate initramfs
dracut --kver "${kver}" -f
Expand Down
3 changes: 2 additions & 1 deletion images/alpine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ actions:
target=/boot/grub/grub.cfg
grub-mkconfig -o "${target}"
sed -i "s#root=[^ ]*#root=/dev/sda2#g" "${target}"
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i "${target}"
TARGET="x86_64"
[ "$(uname -m)" = "aarch64" ] && TARGET="arm64"
Expand Down
4 changes: 2 additions & 2 deletions images/archlinux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -796,8 +796,8 @@ actions:
grub-install --target="${TARGET}-efi" --efi-directory=/boot/efi --no-nvram --removable
grub-install --target="${TARGET}-efi" --efi-directory=/boot/efi --no-nvram
grub-mkconfig -o /boot/grub/grub.cfg
sed -i "s#root=[^ ]*#root=/dev/sda2#g" /boot/grub/grub.cfg
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i /boot/grub/grub.cfg
# Rebuild initrd
sed -i 's#^MODULES=.*#MODULES=(virtio_pci virtio_scsi virtio_console)#' /etc/mkinitcpio.conf
Expand Down
12 changes: 7 additions & 5 deletions images/centos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,8 @@ actions:
umount /sys/firmware
sed -i "s#root=[^ ]*#root=/dev/sda2#g" "${target}"
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i "${target}"
# Workaround for broken systemd in centos7
systemctl enable incus-agent-workaround.service
Expand All @@ -851,8 +852,8 @@ actions:
target="$(readlink -f /etc/grub2-efi.cfg)"
grub2-mkconfig -o "${target}"
sed -i "s#root=[^ ]*#root=/dev/sda2#g" "${target}"
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i "${target}"
types:
- vm
releases:
Expand All @@ -867,11 +868,12 @@ actions:
# Create grub.cfg file
grub2-mkconfig -o "${target}"
sed -i "s#root=[^ ]*#root=/dev/sda2#g" "${target}"
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i "${target}"
# Update files in /boot/loader/entries/. `grubby` needs to be run after
# `grub2-mkconfig` as the latter overwrites files in /boot/loader/entries/.
grubby --update-kernel=/boot/vmlinuz-${kver} --args="root=/dev/sda2 ro"
grubby --update-kernel=/boot/vmlinuz-${kver} --args="root=${rootdevuuid} ro"
# Regenerate initramfs
dracut --kver "${kver}" -f
Expand Down
3 changes: 2 additions & 1 deletion images/debian.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,8 @@ actions:
grub-install --uefi-secure-boot --target="${TARGET}-efi" --no-nvram
update-grub
sed -i "s#root=[^ ]*#root=/dev/sda2#g" /boot/grub/grub.cfg
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i /boot/grub/grub.cfg
types:
- vm
releases:
Expand Down
3 changes: 2 additions & 1 deletion images/fedora.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ actions:
target=/boot/efi/EFI/fedora/grub.cfg
grub2-mkconfig -o "${target}"
sed -i "s#root=[^ ]*#root=/dev/sda2#g" "${target}"
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i "${target}"
sed -Eri "s#^(linux|initrd) .+/overlay/#\1 /#g" /boot/loader/entries/*
sed -ri 's#^options .+#options $kernelopts#g' /boot/loader/entries/*
Expand Down
7 changes: 4 additions & 3 deletions images/gentoo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,8 @@ actions:
rm -Rf /boot/* /boot/efi/* || true
systemd-machine-id-setup
bootctl install --no-variables --esp-path=/boot/efi/
echo "root=/dev/sda2" >> /etc/kernel/cmdline
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
echo "root=${rootdevuuid}" >> /etc/kernel/cmdline
echo "sys-apps/systemd kernel-install" >> /etc/portage/package.use/systemd
echo "sys-kernel/installkernel dracut systemd systemd-boot" >> /etc/portage/package.use/installkernel
Expand Down Expand Up @@ -668,7 +668,8 @@ actions:
grub-install --target=${TARGET}-efi --no-nvram --removable
grub-install --target=${TARGET}-efi --no-nvram
grub-mkconfig -o /boot/grub/grub.cfg
sed -i "s#root=[^ ]*#root=/dev/sda2#g" /boot/grub/grub.cfg
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i /boot/grub/grub.cfg
types:
- vm
variants:
Expand Down
4 changes: 2 additions & 2 deletions images/openeuler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ actions:
grub2-mkconfig -o "${target}"
umount /sys/firmware
sed -i "s#root=[^ ]*#root=/dev/sda2#g" "${target}"
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i "${target}"
# Workaround for broken systemd in centos7
systemctl enable incus-agent-workaround.service
Expand Down
3 changes: 2 additions & 1 deletion images/opensuse.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ actions:
grub2-mkconfig -o /boot/grub2/grub.cfg
umount /sys/firmware
sed -i "s#root=[^ ]*#root=/dev/sda2#g" /boot/grub2/grub.cfg
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i /boot/grub2/grub.cfg
types:
- vm

Expand Down
9 changes: 6 additions & 3 deletions images/rockylinux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ actions:
dracut --kver "${kver}" -f
target="$(readlink -f /etc/grub2-efi.cfg)"
grub2-mkconfig -o "${target}"
sed -i "s#root=[^ ]*#root=/dev/sda2#g" "${target}"
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i "${target}"
types:
- vm
releases:
Expand All @@ -374,10 +375,12 @@ actions:
target=/boot/efi/EFI/rocky/grub.cfg
# Create grub.cfg file
grub2-mkconfig -o "${target}"
sed -i "s#root=[^ ]*#root=/dev/sda2#g" "${target}"
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i "${target}"
# Update files in /boot/loader/entries/. `grubby` needs to be run after
# `grub2-mkconfig` as the latter overwrites files in /boot/loader/entries/.
grubby --update-kernel=/boot/vmlinuz-${kver} --args="root=/dev/sda2 ro"
grubby --update-kernel=/boot/vmlinuz-${kver} --args="root=${rootdevuuid} ro"
# Regenerate initramfs
dracut --kver "${kver}" -f
types:
Expand Down
6 changes: 4 additions & 2 deletions images/springdalelinux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,8 @@ actions:
umount /sys/firmware
sed -i "s#root=[^ ]*#root=/dev/sda2#g" "${target}"
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i "${target}"
# Workaround for broken systemd in centos7
systemctl enable incus-agent-workaround.service
Expand All @@ -452,7 +453,8 @@ actions:
target="$(readlink -f /etc/grub2-efi.cfg)"
grub2-mkconfig -o "${target}"
sed -i "s#root=[^ ]*#root=/dev/sda2#g" "${target}"
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i "${target}"
types:
- vm
releases:
Expand Down
3 changes: 2 additions & 1 deletion images/ubuntu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,8 @@ actions:
grub-install --uefi-secure-boot --target="${TARGET}-efi" --no-nvram
update-grub
sed -i "s#root=[^ ]*#root=/dev/sda2#g" /boot/grub/grub.cfg
rootdevuuid=$(blkid -o export $(echo -n /dev/loop[0-9]*p2) | grep ^UUID)
sed -e "s#root=[^ ]*#root=${rootdevuuid}#g" -i /boot/grub/grub.cfg
types:
- vm

Expand Down

0 comments on commit a6af75a

Please sign in to comment.