HOWTO: Boot a Rescue ISO Directly from GRUB

Posted
Modified
Comments 0

Know what’s a real PITA? Having to carry around a ton of USB sticks (or even optical media!) for different rescue instances.

Did you know that if you’re using GRUB, you don’t have to?

A couple of caveats:

  • It only works for GRUB2. I’m not aware of any other bootloader that supports loop-mounting, and legacy GRUB definitely doesn’t support it.
  • Your system still has to be able to boot to GRUB; it won’t be able to fix a broken GRUB install obviously.
  • It works best if you have a fairly sizeable boot/ESP partition, depending on how many ISOs you want to make available and their respective sizes.
  • It also works best if you have a cron job to automatically download the newest version of an ISO.
    • Obviously, you need to have the ISOs you want to boot from downloaded to a filesystem accessible by your bootloader (as indicated above, I just put them in /boot/iso).

If you don’t want the nitty-gritty, I have a script that will download the latest Arch ISO (which has a small number of rescue utilities available on its own), download the latest SystemRescueCD, a grub script for Arch and a grub script for SystemRescueCD. I might figure it out for CentOS/RHEL or Debian/Ubuntu if there are any requests for it.

For this example, we’ll just be including SystemRescueCD (which I’ll subsequently be referring to as sysresccd because it’s shorter to type and is the abbreviation used internally by the project itself) but there’s no real limit on how many of these you can include – only the storage capacity of wherever you store the ISO files. Generally speaking, though, you shouldn’t need any other ISO for your rescue operations.

I assume below that your ESP is set to /boot (so you have e.g. a /boot/EFI directory). If you’re using a different mountpoint, adjust accordingly. If you aren’t using (U)EFI, you can ignore this assumption.

  1. First, make sure your /boot directory’s device is mounted: mount /boot (if you get an error message about it already being mounted, that’s fine and dandy).
  2. Then you need to make a destination for your ISOs: mkdir /boot/iso
  3. Now you’ll need to download the latest sysresccd ISO file — at the time of writing, this is version 6.0.3. Make sure you confirm the checksum. To make this easier so you don’t need to update configuration files with each new version of the ISO you download, save it as /boot/iso/sysresccd.iso.
  4. Now you’ll need to add a GRUB script:
    1. touch /etc/grub.d/40_custom_sysresccd
    2. chmod 755 /etc/grub.d/40_custom_sysresccd
    3. …and use your preferred editor to edit /etc/grub.d/40_custom_sysresccd and add the following:
#!/bin/sh
exec tail -n +3 $0
# Copy this file to /etc/grub.d/40_custom_sysresccd with mode 0755 and run grub-mkconfig -o /boot/grub/grub.cfg

menuentry 'System Rescue CD' {
	set isofile='/iso/sysresccd.iso'
	probe -u $root --set=imgdevuuid
	set imgdevpath="/dev/disk/by-uuid/$imgdevuuid"
	loopback loop $isofile
	linux (loop)/sysresccd/boot/x86_64/vmlinuz archisobasedir=sysresccd img_dev=$imgdevpath img_loop=$isofile earlymodules=loop
	initrd (loop)/sysresccd/boot/intel_ucode.img (loop)/sysresccd/boot/amd_ucode.img (loop)/sysresccd/boot/x86_64/sysresccd.img
}

And that’s it! To make the new script active, run grub-mkconfig -o /boot/grub/grub.cfg (or wherever your grub config is). It won’t show up in the GRUB menu until you do.

Note that the linux ... and initrd ... lines are going to vary greatly based on the distro that the ISO is based on. You may need to do some digging into the initrd/initramfs to determine valid kernel cmd variables and to see how the initrd/initramfs actually mounts and boots the system.

Author
Categories

Comments

There are currently no comments on this article.

Comment...

Enter your comment below. Fields marked * are required. You must preview your comment before submitting it.