2019-10-26

Boot a linux live ISO image from grub

Trying out a particular flavor using its live-CD distribution is a good idea before going ahead with a full installation in our machine.

We used to have to burn a live system ISO image onto a CD or DVD, and then it became far more convenient to burn it onto a USB thumb drive.

However, another option to boot the live image is possible:  if we have a Linux system already, and the ISO file is in our filesystem, it is possible to boot into the ISO directly from the grub boot menu, avoiding the process of burning the file into any type of media.

Every once in a while I want to do this and I always forget from one time to the next, so I am documenting it here.

The key is to create a grub.cfg menu entry with the right info.   Searching through the net I found there are different parameters required, depending on the type of Linux distribution.   I have not been able to find proper documentation on the boot parameters that may be passed to the kernel (I could find several examples, but no "formal" documentation).

My guess is that different distributions may use different parameters, but here are a couple of examples:

menuentry 'Kali Linux 2019.3 Live' {
       set isofile="/iso/kali-linux-2019.3-amd64.iso"
       insmod ext2
       insmod loopback
       insmod iso9660
       loopback loop (hd3,gpt2)$isofile
       linux (loop)/live/vmlinuz boot=live fromiso=/dev/sda2/$isofile
       initrd (loop)/live/initrd.img
       }

menuentry 'Ubuntu 19.10 Live' {
       set gfxpayload=keep
       set isofile="/iso/ubuntu-19.10-desktop-amd64.iso"
       insmod ext2
       insmod loopback
       insmod iso9660
       loopback loop (hd3,gpt2)$isofile
       linux (loop)/casper/vmlinuz iso-scan/filename=$isofile noprompt noeject
       initrd (loop)/casper/initrd
       }

I found these two distros require a different way to specify the location of the live image after the kernel has booted.    Kali requires the "fromiso" parameter while Ubuntu (and its derivatives) require "iso-scan/filename".  See above for examples.

In the above example, the location of the ISO file is in the third harddrive, and second GPT partition. 

During my trial and error to get this right I found that the specification of harddisk and partition required in these menu entries may be different than what you'd expect.  

For instante, you may have saved the ISO file into:
             /home/username/iso/image.iso

In this example, the /home partition is in /dev/sda.    This would usually be hd0 to grub, but during boot time I found that in some hardware configurations, grub would identify the disk normally found in /dev/sda as hd3, not hd0

A little playing around may be necessary to find the correct pair (hdX,partitionY) specification.   Boot into grub, and while the menu is showing, press "c" for a command interface.   From there use the "ls" command to get a list of harddisks and partitions available.   The list shown ought to help identify which disk is the one with the /home partition.  
If the disk is partitioned with an MSDOS table, your partition would be referred to as (hd3,msdos1),  while in a GPT table, it would be (hd3,gpt2),  for the second partition in the 3rd disk.