Skip to main content
1 of 3
jc__
  • 2.8k
  • 2
  • 18
  • 23

How can I install several Linux distributions on a USB stick?

Assume:

  • BIOS boot from USB drive
  • MBR Partitions
  • GRUB boot loader

Tools:

Overview:

  • Zero out the partition table and boot record of the USB drive
  • Create Partitions on USB drive
  • Create OSs for USB drive in a virtual machine
  • Clone New OSs with clonezilla
  • Restore OSs to USB drive
  • Update and customize GRUB2

Step 1

Get tools ready and determine what OSs to put on USB and what size partitions and if swap partition will be used.

Your USB looks to be 32GB total.

NTFS 16GB partiton for Microsoft interoperatiblty.

Ubuntu14 32bit server 4GB ext4 with 1GB swap partition

Ubuntu16 64bit server 8GB ext4 with 4GB swap partition

NOTE: If you do not use stand by or hibernate on any of the OSs, they could share a single swap partition, or even use a swap file in each of the OS partitons.

Partition Layout:

  • P1 -- 15GB NTFS
  • P2 -- 4GB ext4
  • P3 -- 8GB ext4
  • P4 -- extended
  • E1 -- 4GB swap
  • E2 -- 1GB swap

NOTE: BIOS and MBR will only see 4 primary partitions. Use an extended partition in place of a single primary partition to overcome this limit.


Step 2

Prepare USB drive.

Create a new Virtual Machine named "test" No hard drive required or wanted for this VM. This should make the USB drive /dev/sda. This will make it simpler later.

Connect the Parted Magic ISO to the VM and boot to the ISO.

Connect USB drive and 'pass to the VM'

Verify USB visible in VM and note device number.

Start Partition Editor from desktop icon.

The USB drive in my setup is: /dev/sda

Zero out partition table. This will delete ALL info on this device. Copy any data you want to keep first.

  • MUST have the correct device (example: /dev/sda) because you can make your computer unbootable if you zero out the wrong device.
  • dd if=/dev/zero of=/dev/sda bs=512 count=4

Refresh the drive list in GParted (ctrl+R)

device --> create partition table "msdos"

Create partitions:

NOTE: I created the NTFS partition last to 'use up' the remaining space.

  • Create Extended Partition at the back end of the space. 5GB or 5120
  • Create both swap partition in the Extended Partition. Type linux-swap. 4GB, 1GB. Extended Partition will be full.
  • Create the 2 OS partitions at the end of the unallocated space. Type ext4. 4GB, 8GB.
  • Label the 4GB 'Ubuntu14'
  • Label the 8GB 'Ubuntu16'
  • Create the NTFS data partition using the remaining unalocated space at the beginning of the drive. Type NTFS.
  • Label the NTFS 'data'

Apply changes. Should look something close to this. Note my USB drive is 1TB so my NTFS partition is much larger. GParted Screen shot


Step 3

Prepare the OSs that will be restored to the USB drive.

Created new Virtual Machine and install Ubuntu14 server 32bit. I made the VM install drive smaller than the partiton size on the USB. 4GB. During installation I used MBR not LVM and let the install create a swap partition.

Created new Virtual Machine and installed Ubuntu16 server 64bit. I made the VM install drive smaller than the partiton size on the USB. 8GB. During installation I used MBR not LVM and let the install create a swap partition.

Make any changes, updates, installs you want to each of the OSs.

Shutdown the new VMs

Start clone of new VM.

Attach the pmagic_2013_08_01.iso to each of the new VM and boot from the ISO.

Look at the partitions on each VM. Note the location of the installed OS. Both should be /dev/sda1. (If not make adjustments later)

Now clone each OS using clonezilla from the desktop icon. I will place my cloned images on my desktop via ssh, so setup network before starting clonezilla.

Clonezilla options:

  • device-image -- cloning device /dev/sda1 to an image
  • ssh_server -- placing the image on my desktop. Place yours where ever you can get to it.
  • Beginner mode -- most defaults will work
  • saveparts -- we only want the OS partition.
  • give the image a name you will reconize later.
  • sda1 -- select the OS partition. Should only be one choice, but may not.
  • skip checking/repairing source file system -- The VM should all be clean and ready
  • Yes, check the saved image -- verify that the clone image is a good file. follow prompts.
  • wait.
  • Finished! -- poweroff is fine at this point.

Step 4

Restore OS images to the USB drive.

Go back to the 'test' VM that does not have a virtual drive attached and boot from pmagic_2013_08_01.iso.

Pass the USB drive to the 'test' VM.

Start the partition editor from the desktop icon. We are back here. See the Screenshot.

Match up the OS with the USB partitions to the image partitions.

Ubuntu14:

  • image partition -- /dev/sda1
  • USB partition -- /dev/sda3

Ubuntu16:

  • image partition -- /dev/sda1
  • USB partition -- /dev/sda2

NOTE: This version of clonezilla by default will restore the image to the same partition it was made from. /dev/sda1 to /dev/sda1. Not what we want, so this hack will get the images restored to the USB partitions.

clonezilla hack to restore image to a different partition it was cloned from:

Rename all files in the image directory with the new partition id.

/home/jc/clonezilla_images/ubuntu14/sda1* to /home/jc/clonezilla_images/ubuntu14/sda3*

Modify the content of /home/jc/clonezilla_images/ubuntu14/parts. Replace 'sda1' with 'sda3'

Same for Ubuntu16, adjust numbers sda2.

NOTE: I started from device sda on both the VM and USB. If yours uses another device somewhere like /dev/sdb changes to more files required. Its just simpler to start with both on the same device. /dev/sda.

Restore images to the USB drive via clonezilla

Clonezilla options:

  • device-image -- cloning device /dev/sda1 to an image
  • ssh_server -- Or where ever you put the image.
  • Beginner mode -- most defaults will work
  • restoreparts -- restoring image of a partition
  • select image to restore
  • select the correct USB partition for the image -- Ubuntu14 to sda3; Ubuntu16 to sda2
  • follow prompts.
  • wait.
  • Enter command line prompt. -- Still have more images to restore

Restart clonezilla and repeat for all images.

My USB unmounted from the VM so had to pass through the USB drive again.


Step 5

Install GRUB to USB drive

The Grub boot loader will be installed to the first 512 bytes of the USB drive. This loader will point to a grub.cfg file. Lets place ours on the Ubuntu16 partition. Its can be anywhere grub can read

We will use our host OS to do this.

You could use a live OS in a VM to accomplish this.

Should be using a current linux OS with grub2.

Attach USB.

My USB drive is currently mounted as device /dev/sdf. Yours will differ.

Mount the partition the grub.cfg will live in.

Ubuntu16 ext4

sudo mount /dev/sdf2 /media/usb_u16

install grub bootloader to usb device

sudo grub-install --recheck --boot-directory=/media/usb_u16/boot /dev/sdf

This will place the config file pointer to the Ubuntu16 partition boot directory. Common location for Ubuntu. Yours can be anywhere.

Installation finished. No error reported.

Create a grub.cfg file to work from as a template.+

sudo grub-mkconfig --output=/media/usb_u16/boot/grub/grub.cfg

The important thing here is that grub-mkconfig will probe the USB drive and create boot entries for the OSs we installed on the USB drive. The part we do not want is it will also probe any installed drives on the host machine and create entries for these as well.

Now unmount everything and test USB boot.

If everything works you should be able to boot to the USB OSs. If not go back...


Step 6 Customize grub.cfg on the USB drive

When you boot this USB drive you will see the OSs on the host PC even if the USB boots on another PC. Lets fix this.

Lets gather some more info.

Each partition has an UUID number assigned to it. Get the UUIDs for our USB drive.

blkid

/dev/sdf2: UUID="078ecc53-f73c-4cc7-9d45-f78aa534b7c5" TYPE="ext4" PARTUUID="00005785-02" /dev/sdf3: UUID="a4654333-ee62-4dac-8c09-79a034e3125d" TYPE="ext4" PARTUUID="00005785-03" /dev/sdf4: LABEL="data" UUID="667308CA28B6BB86" TYPE="ntfs" PARTUUID="00005785-04" /dev/sdf5: UUID="8646e3f8-4f5c-40d4-a409-c4d65bf98d21" TYPE="swap" PARTUUID="00005785-05" /dev/sdf6: UUID="61b8680c-22c9-4efc-98a1-2aaaa4a7f304" TYPE="swap" PARTUUID="00005785-06" 

Yours will differ.

The 2 swap partition UUIDs save for later. You will add these to the /etc/fstab file to auto mount on boot.

The 2 OS partition UUIDs:

/dev/sdf2: UUID="078ecc53-f73c-4cc7-9d45-f78aa534b7c5" -- Ubuntu16 /dev/sdf3: UUID="a4654333-ee62-4dac-8c09-79a034e3125d" -- Ubuntu14 

Copy the 2 files memdisk and super_grub2_disk_hybrid_2.02s9.iso to the grub config directory, and splash.jpg image if you have one.

Ubuntu16 ext4

sudo mount /dev/sdf2 /media/usb_u16

sudo cp ./memdisk /media/usb_u16/boot

sudo cp ./super_grub2_disk_hybrid_2.02s9.iso /media/usb_u16/boot

sudo cp ./splash.jpg /media/usb_u16/boot

Create a backup copy of the created grub.cfg

sudo mv /media/usb_u16/boot/grub/grub.cfg /media/usb_u16/boot/grub/grub.cfg.orginal

Create our custom grub.cfg

sudo vi /media/usb_u16/boot/grub/grub.cfg

In short we need to copy from grub.cfg.orginal just the bare minimum. The grub setup stuff insmod..., the display stuff gfxmode=..., and the boot entries. The boot entries can be found by searching for the UUID numbers from above. Copy all including the sub-menu entries or just the main entry.

The memdisk and the super_grub2_disk_hybrid_2.02s9.iso will allow us to boot from any OS on the host machine even when the boot entries do not exist.

There are many reasons why this is not the right way to do it, but it works.

Modify the Example grub.cfg for this USB drive.

Make a backup of you custom grub.cfg file because something will try to overwrite it.

sudo cp /media/usb_u16/boot/grub/grub.cfg /media/usb_u16/boot/grub/grub.cfg.custom


Step 7

Adjust the installed OSs

  • Add the swap partition UUID to /etc/fstab
  • Add any auto mount partitions (NTFS data, the other OS ext4) on the USB to /etc/fstab
  • On Ubuntu I would adjust the default grub entries with my changes so when the OS is updayed the changes will propergate to the new grub menu.

Test it!

jc__
  • 2.8k
  • 2
  • 18
  • 23