Grub Meanderings

We learn by our mistakes. Or by doing stupid things then fixing them.

  • Create a boot partition and swap partitions
  • mk fs on the boot partitions
  • mkswap on the swap
  • Use cp -r –preserve=all to copy the files from the old usb stick to the new one.
  • Put the new USB stick in the machine and power it up

Of course, you get an error “Missing Operating System”

  • Install grub using grub-install specifying the partition.
  • Put the USB back in and try again…

This time the machine boots but grub can’t find anything so it goes into rescue mode.

Grub uses the uuid of the root partitions to find vmlinuz and the initramfs. As I’ve changed the USB key but not any of the files, grub is still looking for the partitions on the old key

So in grub-rescue

  • Identify available partitions by typing ls
  • Choose a partition, say (hd0,1) (the MSDOS reference can be safely ignored)
  • ls (hd0,1)/ which will list the files on the partition.
  • If it looks like the root partition, use that one
  • set prefix=(hd0,1)/boot/grub
  • set root=(hd0,1)
  • thenload the linux module: insmod linux
  • start the boot sequence by loading linux and initrd
    • linux /vmlinuz root=/dev/sda1 rw
    • initrd /initrd.img
    • boot

Note the rw on the linux line? The default behaviour is that grub boots root as read only. When linux itself starts, it mounts all the partitions in /etc/fstab at which point root is remounted as read write.

The system should now boot up. The correct partition uuid can be added to the grub configuration and grub added to the mbr, at the same time the uuids in /etc/fstab can be updated.

Without the rw on the linux line above,Linux will boot but the root partition will not be writable. This means that you can’t change the configuration file; in this case reboot, choose advanced options in the grub menu and add rw to the appropriate line and boot.

The clever thing to do is to alter both /boot/grub/grub.cfg and /etc/fstab at the same time. If you just alter grub.cfg, the system will boot but Linux won’t be able to remount the root partition as read/write, which is a less than optimal situation. The way around this is to use advanced options in the grub menu to specify that grub should boot the partition as read/write, then adjust /etc/fstab so that it works correctly.

Assuming we now have a writable partition and a booted linux system, the new uuids can be identified by either

  • sudo blkid or
  • ls -al /dev/disk/by-uuid

Then replace the UUIDs in /etc/fstab and /boot/grub/grub.cfg and reboot.

With thanks to