Linux

How to create a multiboot USB drive in Linux with bash

A multiboot USB drive is a USB drive that contains multiple operating systems, allowing you to boot and run multiple operating systems from a single USB drive.


Introduction to Multiboot USB Drives

A multiboot USB drive is a USB drive that contains multiple operating systems, allowing you to boot and run multiple operating systems from a single USB drive.

This is useful for a number of scenarios, such as:

  • Testing different operating systems
  • Installing an operating system on multiple computers
  • Carrying your favorite operating systems with you on the go

In this presentation, we will cover the process of creating a multiboot USB drive using a script.

Prerequisites

Before you can use the script to create a multiboot USB drive, you will need to:

  • Have a USB drive with sufficient capacity to hold all of the operating systems that you want to include
  • Have the files for each operating system that you want to include on the USB drive
  • Know the device name of the USB drive (e.g. /dev/sdb)

The Script

Here is the script that you can use to create a multiboot USB drive:


#!/bin/bash

# Get the device name of the USB drive
echo "Enter the device name of the USB drive (e.g. /dev/sdb):"
read device

# Make sure the device name is correct
echo "You entered $device. Is this correct? (y/n)"
read confirm
if [ "$confirm" != "y" ]; then
echo "Exiting script."
exit
fi

# Unmount the USB drive
umount $device

# Create a new GPT partition table
parted $device mklabel gpt

# Create a new partition for the multiboot bootloader
parted $device mkpart primary fat32 1MiB 551MiB

# Set the boot flag on the new partition
parted $device set 1 boot on

# Create a new partition for the operating systems
parted $device mkpart primary ext4 551MiB 100%

# Format the boot partition with FAT32
mkfs.vfat -F32 "${device}1"

# Format the operating system partition with ext4
mkfs.ext4 "${device}2"

# Mount the boot partition
mkdir boot
mount "${device}1" boot

# Mount the operating system partition
mkdir os
mount "${device}2" os

# Install the multiboot bootloader
grub-install --target=i386-pc --boot-directory=boot $device

# Generate the configuration file for the multiboot bootloader
cat > boot/grub/grub.cfg <<EOF
# Set the default boot entry
set default=0

# Set the timeout in seconds
set timeout=5

# Create a menu entry for each operating system
menuentry "Operating System 1" {
set root=(hd0,1)
chainloader +1
}

menuentry "Operating System 2" {
set root=(hd0,1)
chainloader +1
}

# Add additional menu entries for each additional operating system
EOF

# Copy the operating system files to the operating system partition
cp -r /path/to/operating/system/1/* os/
cp -r /path/to/operating/system/2/* os/

# Add additional cp commands for each additional operating system

# Unmount the USB drive
umount boot
umount os

# Remove the mount directories
rmdir boot
rmdir os

echo "Multiboot USB drive created successfully!"

To use this script, simply replace “/path/to/operating/system/1” and “/path/to/operating/system/2” with the paths to the directories containing the files for the operating systems that you want to include on the USB drive. You can also add additional menu entries and cp commands for each additional operating system that you want to include.