Tuesday, May 21, 2013

How To Create A Multi-Partition Multi-Boot Pendrive


Step 1: Install gparted
In rpm-based operating systems:
$ yum install -y gparted
In debian-based operating systems:
$ sudo apt-get install -y gparted
Step 2: Created 5 Logical Partitions each of 850MB in your pendrive. Give appropriate label to each partition. The last 4 will hold the content for installation. The first partition can be used as a normal pen drive uses. Note: These 5 Logical partitions have been created out of a single extended partition.
Step 3: Download the images of the distros.
$ wget http://centos.mirror.euserv.net/6.4/isos/x86_64/CentOS-6.4-x86_64-LiveCD.iso 
$ wget http://ftp.jaist.ac.jp/pub/Linux/Fedora/releases/17/Live/x86_64/Fedora-17-x86_64-Live-Desktop.iso
$ wget http://gb.releases.ubuntu.com//raring/ubuntu-13.04-desktop-amd64.iso
$ wget http://gb.releases.ubuntu.com//raring/ubuntu-13.04-desktop-i386.iso
Step 4: Mount all the 5 partitions. (In GUI mode, or any other mode you prefer)
Step 5: Mount the images one by one and then extract their contents to the respective partitions
$ mount -o loop /home/Wani/OSImages/CentOS-6.4-x86_64-LiveCD.iso /mnt
$ cp -rv /mnt/* /run/media/Wani/CENTOS
$ umount /mnt
$ mount -o loop /home/Wani/OSImages/Fedora-17-x86_64-Live-Desktop.iso /mnt
$ cp -rv /mnt/* /run/media/Wani/FEDORA
$ umount /mnt
$ mount -o loop /home/Wani/OSImages/ubuntu-13.04-desktop-amd64.iso /mnt
$ cp -rv /mnt/* /run/media/Wani/UBUNTU13.04
$ umount /mnt
$ mount -o loop /home/Wani/OSImages/ubuntu-13.04-desktop-i386.iso /mnt
$ cp -rv /mnt/* /run/media/Wani/UBUNTU13.041
$ umount /mnt
In my OS, the mount point was /run/media/Wani. It might be different in your OS.
Step 6: Install grub into the pendrive
$ grub2-install --root-directory /run/media/Wani/Partition1-Name /dev/sdx #replace x with the appropriate letter
Step 7: Find the location of files grub.cfg/grub.conf/loopback.cfg in each partition.
FEDORA:        /mnt/EFI/BOOT/grub.conf
CENTOS:        /EFI/boot/grub.conf
UBUNTU13.04:   /boot/grub/grub.cfg
UBUNTU13.041:  /boot/grub/loopback.cfg
Step 8: Find the mapping of each partition with its partition number. It will depend on the order in which these partitions were created
FEDORA: /dev/sda5
CENTOS:        /dev/sda6
UBUNTU13.04:   /dev/sda7
UBUNTU13.041:  /dev/sda8
Step 9: Configure grub file on pendrive.
$ vim /run/media/Wani/Partition1-Name/boot/grub2/grub.cfg
Template:
menuentry "OS Name" {
 root=(hd0,msdos(mapping-number))
  legacy_configfile (location-of-grub-config-file)
}

set timeout=10
set default=0

menuentry "Fedora 17 x86_64" {
 root=(hd0,msdos5)
  legacy_configfile /EFI/BOOT/grub.conf
}
menuentry "CentOS 6.4 x86_64" {
 root=(hd0,msdos6)
  legacy_configfile /EFI/boot/grub.conf
}
menuentry "Ubuntu 13.04 64bit" {
 root=(hd0,msdos7)
  configfile /boot/grub/grub.cfg
}
menuentry "Ubuntu 13.04 32-bit" {
 root=(hd0,msdos8)
  configfile /boot/grub/loopback.cfg
}
Note that Fedora and CENTOS use the old grub syntax, hence the option legacy_configfile but Ubuntu uses the new one.

Step 10: Change the grub.conf file in the partition of FEDORA and CENTOS
In /EFI/boot/grub.conf, change the value of root from
root=live:LABEL=CentOS-6.4-x86_64-LiveCD
to
root=live:LABEL=CENTOS
Note: The label needs to be changed, so that grub knows, which partition to boot from. The 'label' is the label that you put while creating the partitions. Repeat the same for FEDORA.

Step 11(Optional): Mark the partitions containing extracted images as hidden, so that they are not automatically mounted. Use the Partition-1 as for normal data transferring purposes.

Update: For Fedora 18, a following changes have to be made:

(i) Go in to the partition in which Fedora-18-x86_64-Live-Desktop.iso was extracted. Go to the folder EFI/Boot and create a file grub.conf with the following configuration: (Assumed that the label of the partition is FEDORA18)
default=0
timeout 10
hiddenmenu

title Fedora-18-x86_64-Live-Desktop.iso
  findiso
  kernel /isolinux/vmlinuz0 root=live:LABEL=FEDORA18 ro rd.live.image quiet  rhgb xdriver=vesa nomodeset
  initrd /isolinux/initrd0.img
title Verify and Boot Fedora-18-x86_64-Live-Desktop.iso
  findiso
  kernel /isolinux/vmlinuz0 root=live:LABEL=FEDORA18  ro rd.live.image quiet  rhgb rd.live.check
  initrd /isolinux/initrd0.img

(i) Add the following in the first partition's boot/grub2/grub.cfg:
menuentry "Fedora 18 x86_64" {
  root=(hd0,msdos5)
  legacy_configfile /EFI/BOOT/grub.conf
}