Monday, July 4, 2011

Boot an ISO Image using Grub2, or Install Ubuntu without needing a CD/DVD or USB drive

Grub2 is capable of directly booting ISO images for many Linux distros if the entries have been properly defined in the Grub2 configuration files.

What if you don't have access to a CD/DVD drive/disc or a USB drive and you want to install Ubuntu (or any other Linux)?

Or if you want to perform some system maintenance tasks like those related to partitions or filesystems which can only be performed from within a Live CD environment?

Grub2 comes in handy at that point as it would not only boot the ISO image from your HDD, but it would also let you perform an installation from the ISO image if you want to.

Things You Need
  • An existing install of Ubuntu (or any other Linux) with a working instance of Grub2.
  • The desired ISO image that you want to boot/install.
Procedure

The ISO image can be placed virtually anywhere on your HDD but for the sake of simplicity, we would place it inside a new directory under /boot.

1. Create the new directory and copy your ISO image:

sudo mkdir /boot/iso
sudo cp ~/Desktop/name.iso /boot/iso

Where '~/Desktop/name.iso' is the location and name of your ISO image assuming that the image is located at your desktop.

2. Add the Grub2 entry:

The entry for the ISO image needs to be added to '/etc/grub.d/40_custom' file. Edit the file by:

gksudo gedit /etc/grub.d/40_custom

And replace the text in that file with this one:

#!/bin/sh
echo "Adding 40_custom." >&2
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.

menuentry "Oneiric ISO" {
        set isofile="/boot/iso/oneiric-desktop-i386.iso"
        loopback loop (hd0,8)$isofile
        linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile noprompt noeject
        initrd (loop)/casper/initrd.lz
}

Where is:
  • "Oneiric ISO" = The name you want to display in the Grub menu.
  • "boot/iso/oneiric-desktop-i386.iso" = The location and name of your ISO image.
  • (hd0,8) = The partition which contains the ISO image.
Grub reads the partitions in a different way than Ubuntu does. 'hd0' means first HDD which is read as 'sda' by Ubuntu, and '8' is the partition which is the same as for Ubuntu. So in other words, (hd0,8) means 'sda8'.

To find out your partition, run this command in a Terminal:

sudo fdisk -l

Suppose your image is contained in the 'sda1' partition, you'd change (hd0,8) in the above line to (hd0,1) and if the image is in the 'sdb1' partition, you'd change (hd0,8) to (hd1,1).

3. Save and close this file and now run this command:

sudo update-grub

4. Reboot and choose the new ISO entry from Grub menu this time. Hopefully, it will boot successfully to the desktop.

Now, if you want to perform an installation from the same ISO, you'd need to unmount the ISO image first as it is mounted from a partition on your HDD (probably you've got a single HDD and want to install Ubuntu to the same HDD) and the installer needs to unmount any mounted partitions before it can make any changes.

So, from the Live CD environment, go to a Terminal and run:

sudo umount -l /isodevice

Thats all. Now you can double-click the 'Install' icon at the desktop and proceed with the installation.

For more information, see here:

http://ubuntuforums.org/showthread.php?t=1549847

http://ubuntuforums.org/showthread.php?t=1599293

Related Posts:

Grub2 , Installation