Published on October 25, 2012 by Alexandru Juncu
Tagged: grub2, bootloader, linux, kernel, iso, image

Note: This article focuses on Debian based Linux distributions.

Grub2 is the next generation Linux bootloader that is trying to replace the “Legacy” Grub version. It is a complete rewrite of Grub 1 and only lately becoming fully featured compared to the old version and even comes with some new interesting features.

The old Grub’s configuration was rather straightforward, everything being done in a configuration file in the grub directory of the /boot partition (it’s a common practice to have /boot mounted on a separate filesystem). In Debian it was usually /boot/grub/menu.lst and in Red Hat /boot/grub/grub.conf (sometimes one being a symlink to the other).

The configuration file for Grub2 is /boot/grub/grub.cfg. But the file itself should never be modified (not even root has write access). Instead, this file is generated by commands like update-grub2. It is generated based on other configuration files like (in Debian) /etc/default/grub, which has things like global configurations, timers and default settings.

The menu entries for the operating systems themselves are generated based on files in the /etc/grub.d/ directory (in Debian). An interesting feature of Grub2 is the fact that these files are actually Bash scripts. OS entries don’t need to be hard coded, but can be scripted. One such script is the 10_linux file that detects any new kernel image in the /boot directory and writes a new entry for it without having to manually add it. Manual entries can also be written in these files (usually in the 40_custom script file).

An interesting new feature in Grub2 is the possibility to boot from an ISO file. A LiveCD can be stored in an iso file on disk and loaded by grub without having to burn it onto CD or having to boot the normal system first. A menu entry for ISO booting would look like this:

menuentry "Ubuntu LiveCD" {
        loopback loop (hd0,1)/boot/iso/ubuntu-12.04-desktop-i386.iso
        linux (loop)/casper/vmlinuz boot=casper :iso-scan/filename=/boot/iso/ubuntu-12.04-desktop-i386.iso noprompt noeject
        initrd (loop)/casper/initrd.lz
}

Based on the previous ideas, here’s a way to configure grub to make an entry for every .iso file that you have in a specified directory. First, create a directory to store the .iso files (ex. /boot/iso/) and move your Live CDs there.

Next, make a script in the /etc/grub.d/ directory. Let’s call it 42_iso (the number in front dictates the order in which the scripts are executed).

#!/bin/bash

ISO_DIR="/boot/iso/"

for iso in $(ls $ISO_DIR*.iso); do
	echo "menuentry \"$iso\" {"
	echo "set isofile=\"$iso\""
	echo "loopback loop (hd0,1)\$isofile"
	echo "linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=\$isofile noprompt noeject"
	echo "initrd (loop)/casper/initrd.lz"
	echo "}"

done

Don’t forget to give it executable access. Then run the update-grub2 command to generate the Grub2 configuration file.

chmod +x /etc/grub.d/42_iso
update-grub2

Thanks to doraz for suggesting ISO booting with Grub.


comments powered by Disqus