Multiboot From A USB Stick Using GRUB

This time we’re going to look into multiboot from a USB stick (aka pendrive) using GRUB, with a special look into Ubuntu Server – which clearly has problems seeing the USB drive on installing startup – thus making it impossible.

This will be a short blog on how to get it working from pendrive, any additional customisations are beyond the subject of this post.

I’ve made the assumption that you’re root at the time of this process, but if you prefer to run it from your personal account then just precede all the commands with sudo.

Preparing a USB stick

1. Identify the USB stick – run:
# fdisk -l

Identify your pendrive in the output – it will most likely be the last device listed. Check if the sizes match and take down a note of the device name – in my case it’s /dev/sdb1 so I’ll use this from now on in every command – you’ll have to replace it accordingly to your output of fdisk. If you have more than one partition on the pendrive, remember the number as we’ll delete them later in fdisk using this number.

2. Unmount the device if mounted
# umount /dev/sdb1

3. Format the USB stick, create bootable partition (ALL DATA on pendrive will be LOST!):
# fdisk /dev/sdb

Now you’ve entered into fdisk, editing the USB stick device (sdb without partition number!). Run following commands:

# d <enter> – if you had one partition only you should see something like this “Selected Partition 1 ”. If there was more than one you’ll be asked which partition number repeatedly for every partition, until you see something like this “No partition is defined yet!”.

# n <enter> – add new partition
# p <enter> – primary partition
# 1 <enter> – partition number
# <enter> to use default first sector
# <enter> to use default last sector
# a <enter> – set bootable flag
# 1 <enter> – to set first partition as bootable
# w <enter> – to sava changes to pendrive

4. Create FAT32 file system on the newly created partition
# mkfs.vfat -F 32 /dev/sdb1

5. Mount the device again
# mkdir /mnt/multiboot – create mounting point
# mount /dev/sdb1 /mnt/multiboot/ – mount it

6. Install GRUB on the pendrive
# grub-install –no-floppy –root-directory=/mnt/multiboot/ /dev/sdb

7. Create grub.cfg file. Now enter to the newly installed GRUB folder
# cd /mnt/multiboot/boot/grub/

And create a text file called grub.cfg, then open it with your favourite editor and copy following into it:

set timeout=10
set default=0

menuentry “Run Ubuntu Server 10.04.4 ISO” {
loopback loop /ubuntu-10.04.4-server-amd64.iso
linux (loop)/install/vmlinuz boot=casper iso-scan/filename=/ubuntu-10.04.4-server-amd64.iso
initrd (loop)/install/initrd.gz

8. Copy the iso image to root of the pendrive
As I’ve mentioned before I’ve been working with Ubuntu Server 10.04.4 64-bit as you can see in grub menu entry just above, so if your iso is called something different then don’t forget to amend grub.cfg accordingly!

And this is it, if you have followed this guide and not encountered any errors, then after booting from this pendrive you should get the GRUB menu with this one entry. Of course, you can try this approach with other distributions by editing the iso file name and ensuring that any lines starting with linux and initrd are pointing to existing files within this specific iso (you can extract such iso to ensure that you’ve got right paths). This here works for Centos 5.8 32-bit, the only difference is that the file initrd is ending with .img instead of .gz and the folder is called isolinux rather than install:

menuentry “Run CentOS-5.8-i386-bin-1of7.iso ISO” {
loopback loop /CentOS-5.8-i386-bin-1of7.iso
linux (loop)/isolinux/vmlinuz
initrd (loop)/isolinux/initrd.img

So now, when you have your multiboot USB yum may want to try to install Ubuntu and there will be a sad surprise waiting for you – installer doesn’t see UBS/iso image, so the installation will have to be cancelled (applies to Ubuntu Server version, Desktop may not have this bug).

This, in short, is how you may deal with it. Once installer displays info about ‘no CD-ROM in drive’ press CTRL+ALT+F2 and. This will bring the shell command line up, hit enter. Now we have to mount the pendrive and link the iso image with the CD-ROM drive. Type in these commands:

# mkdir /iso

# mount /dev/sdb1 /iso

#  ln -sf /iso/iso/ubuntu10.04.4-server-amd64-custom.iso /dev/sr0

Now press CTRL+ALT+F1 and you’ll be back in installer, select RETRY and if there was no errors with mounting the installer should see the iso image as if there was a CD in the drive.

When you have just one or two installations to do this solution will be alright, but when there are tens of servers it may be a little bit frustrating. In my next post I’ll describe how I’ve made this process a bit easier for myself.

This entry was posted in Managed Hosting. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>