Boot a (Gentoo) LiveCD ISO image from a HDD partition

Note: This is all command line stuff – no GUIs here for this one!

Useage

I’ve used this trick for updating/reinstalling over an existing system on remote machines and for machines that don’t have any removable media. An alternative trick for such situations could be to use “PXE” booting (such as DRBL), but for that you need to have control of the network and a pxe/dhcp/tftp boot server.

Overview

Here, a Gentoo LiveCD image is mounted on loopback to copy all the files to a spare partition on the HDD. The existing system’s Grub is then used to boot the LiveCD system.

Method

So… Using the existing host system:

Download the latest Gentoo LiveCD image. This example uses the 2011-06-09 Gentoo 64-bit minimal install LiveCD from a UK mirror:

wget http://gentoo.virginmedia.com/releases/amd64/current-iso/install-amd64-minimal-20110609.iso

(A full list of mirrors can be found on: Gentoo Installation media.)

Then as root, where “sdaY” is your target partition for the LiveCD, and “(hd0,X)” is the Grub name for that partition:

# Format a sacrificial partition
mke2fs -v -L gentoo-image /dev/sdaY

# Make a couple of mount point directories
mkdir /mnt/gentoo
mkdir /mnt/hd

# Mount the iso image and the sacrificial partition
mount -o loop install-amd64-minimal-20110609.iso /mnt/gentoo
mount -t ext2 /dev/sdaY /mnt/hd

# Copy the LiveCD data from the iso image
cp -a /mnt/gentoo/* /mnt/hd

# Append a new boot menu item to allow selection to boot the Gentoo LiveCD located on the HDD
echo >>/boot/grub/menu.lst
echo "title gentoo-Live" >>/boot/grub/menu.lst
echo "root (hd0,X)" >>/boot/grub/menu.lst
echo "kernel (hd0,X)/isolinux/gentoo root=/dev/ram0 init=/linuxrc dokeymap looptype=squashfs loop=/image.squashfs cdroot vga=769" >>/boot/grub/menu.lst
echo "initrd (hd0,X)/isolinux/gentoo.igz" >>/boot/grub/menu.lst

Then issue a reboot command (such as “reboot”) and select the ‘gentoo-Live’ option from the Grub boot menu.

Further notes:

  • A convenient trick for if you have no spare partitions is to use the swap space partition for the LiveCD data… Use “swapon -s” to see what swap space is allocated, and “swapoff -a” to cleanly ‘unmount’ all the swap space;
  • If you have made your boot partition 256MB (any more is wasteful), then you’ll have enough space to use that for the LiveCD data. Do not reformat, just copy the data there and update the Grub menu.lst;
  • The Linux kernel counts sata HDDs sequentially as sda, sdb, sdc, etc, (or hda, hdb, hdc, etc for old pata ide disks,) and partitions usually in the sequence 1,5,6,7,etc for one primary partion followed by logical (secondary) partitions. Grub counts both disks and partitions counting from “0”, whereas Grub2 counts the partitions counting from “1” to match the Linux partition number. For example, Grub “sda6” is “hd(0,5)” whereas for Grub2 it is “hd(0,6)”.

Other Methods?

The mount-and-extract part is done because Grub appears not to be able to recognise/read from the HDD ISO image directly. Also, the Gentoo LiveCD uses “isolinux” for boot which can’t read from HDDs (the syslinux version is needed for HDDs). Is there an easy way to boot directly into the ISO image on the disk? For example, to use:

dd if=install-amd64-minimal-20110609.iso of=/dev/sdaY

and then use some Grub trickery to directly use that for booting?

GPL

All code and comments on this page are freely available for use under the current GPL.

Martin

Leave a Reply