1 Multiboot USB Stick with Persistence from the Command Line
2 ==========================================================
4 :date: 2019-07-10 13:48
5 :category: System Administration
6 :tags: command line, system administration, boot, grub2, efi
7 :authors: Maximilian Friedersdorff
8 :summary: Creating a USB stick for booting live Linux distributions with persistence
11 There exist numerous graphical tools for creating bootable USB drives. Some
12 support creating USB drives that can boot into any number of Linux distributions,
13 chosen at boot time. Most of them support creating a persistence file or
14 partition to allow you to save files or install additional applications.
16 Every time I need one, I find that a different one is recommended. Invariably it
17 is not packaged for my current distribution.
19 This is a set of instructions for creating a multiboot USB stick with optional
20 persistence for one of the installed distributions.
27 Download whatever Live disk images you want to install. Good ones are the
28 Ubuntu install images, ArchLinux, Debian etc. Software updates will consume a
29 large amount of space and Kernel upgrades are likely impossible: Choose recently
34 Use your favourite partitioning tool to create a `FAT32` partition at the
35 beginning of the drive. It needs to be large enough to contain all the disk
36 images (ISOs) that you want to boot from, plus some space for the grub boot
41 .. code-block:: console
45 Welcome to fdisk (util-linux 2.34).
46 Changes will remain in memory only, until you decide to write them.
47 Be careful before using the write command.
49 Command (m for help): o
50 Created a new DOS disklabel with disk identifier 0xf0e82e51.
52 Command (m for help): n
54 p primary (0 primary, 0 extended, 4 free)
55 e extended (container for logical partitions)
57 Partition number (1-4, default 1):
58 First sector (2048-15633407, default 2048):
59 Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-15633407, default 15633407): +2G
61 Created a new partition 1 of type 'Linux' and of size 2 GiB.
62 Partition #1 contains a vfat signature.
64 Do you want to remove the signature? [Y]es/[N]o: y
66 The signature will be removed by a write command.
68 Command (m for help): t
70 Hex code (type L to list all codes): b
71 Changed type of partition 'Linux' to 'W95 FAT32'.
73 Command (m for help): w
74 The partition table has been altered.
77 The above creates a 2G partition of the `FAT32` type. My drive previously
78 contained a `FAT32` file system, hence the warning.
82 Create a `FAT32` file system in the newly created partition:
84 .. code-block:: console
86 # mkfs.fat -F 32 /dev/sdX1
91 Mount your `FAT32` partition and create a `grub` directory:
93 .. code-block:: console
95 # mount /dev/sdX1 /mnt/multiboot
96 # mkdir /mnt/multiboot/grub
100 .. code-block:: console
102 # grub-install /dev/sdX \
103 --target=x86_64-efi \
104 --efi-directory=/mnt/multiboot/ \
105 --boot-directory=/mnt/multiboot/boot \
111 Copy all ISOs that you want to be able to boot to some location on the `FAT32`
114 .. code-block:: console
116 # mkdir /mnt/multiboot/iso_boot
117 # cp /tmp/all_my_isos/*.iso /mnt/multiboot/iso_boot/
121 Create a grub configuration file at `/mnt/multiboot/boot/grub/grub.cfg` and create
122 one or more entries for every image that you want to boot. A grub configuration
123 for booting just `Xubuntu 18.04` without persistence looks like this:
132 menuentry "Run Xubuntu 18.04 64 bit" {
133 loopback loop /iso_boot/xubuntu-18.04.2-desktop-amd64.iso
135 linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=/iso_boot/xubuntu-18.04.2-desktop-amd64.iso quiet splash ---
136 initrd (loop)/casper/initrd
140 The paths `/casper/vmlinuz` and `/casper/initrd` correspond to the locations
141 of the kernel and initramfs inside the ISO respectively, which may be different
142 between different distributions.
144 You can list the content of the ISO as follows:
146 .. code-block:: console
149 # losetup /dev/loop0 /tmp/os_image.iso
150 # partprobe /dev/loop0
151 sh: dmidecode: command not found
152 Warning: The driver descriptor says the physical block size is 2048 bytes, but Linux says it is 512 bytes.
153 # mount /dev/loop0 /mnt/tmp
154 mount: /mnt/tmp: WARNING: device write-protected, mounted read-only.
156 You can now list the contents of the ISO with standard tools. Look for files
157 which contain `vmlinuz` and `initrd` or `initramfs` in the name.
159 The kernel options will likely also be different for different distributions.
160 Ideally look at the grub configuration for the ISO that you want to install and
161 copy the relevant kernel options from there.
163 Unmount the ISO and destroy the loop device when you are done:
165 .. code-block:: console
168 # losetup -d /dev/loop0
170 Enable Persistence (Optional)
171 -----------------------------
173 On Ubuntu, almost certainly on Ubuntu based distributions, probably on Debian
174 and friends and maybe on other distributions, you can make use of a separate
175 partition on the USB stick for persistence. Setup is surprisingly trivial, but
176 it will only be possible to enable this for a single installed image.
178 Create another partition on the USB stick and format it with the file system of
181 .. code-block:: console
185 Welcome to fdisk (util-linux 2.34).
186 Changes will remain in memory only, until you decide to write them.
187 Be careful before using the write command.
190 Command (m for help): n
192 p primary (1 primary, 0 extended, 3 free)
193 e extended (container for logical partitions)
194 Select (default p): p
195 Partition number (2-4, default 2):
196 First sector (4196352-15633407, default 4196352):
197 Last sector, +/-sectors or +/-size{K,M,G,T,P} (4196352-15633407, default 15633407): +4G
199 Created a new partition 2 of type 'Linux' and of size 4 GiB.
201 Command (m for help): w
202 The partition table has been altered.
203 Calling ioctl() to re-read partition table.
206 # mkfs.ext4 -L casper-rw /dev/sdX2
207 mke2fs 1.45.2 (27-May-2019)
208 Creating file system with 1048576 4k blocks and 262144 inodes
209 Filesystem UUID: f86ba47b-9049-4970-9050-07e95d7d0743
210 Superblock backups stored on blocks:
211 32768, 98304, 163840, 229376, 294912, 819200, 884736
213 Allocating group tables: done
214 Writing inode tables: done
215 Creating journal (16384 blocks): done
216 Writing superblocks and file system accounting information: done
218 Make sure the file system has the label `casper-rw`. The size of the partition
219 needs to be large enough to hold all the changes you make. If you intend to
220 update software on the USB stick you will need a few Gigabytes at least.
222 Finally edit the grub configuration file on the USB drive to add the option
223 to boot with persistence enabled:
227 menuentry "Run Xubuntu 18.04 64 bit - Persistent, in RAM" {
228 loopback loop /iso_boot/xubuntu-18.04.2-desktop-amd64.iso
230 linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=/iso_boot/xubuntu-18.04.2-desktop-amd64.iso quiet splash persistent toram ---
231 initrd (loop)/casper/initrd
235 The `persistent` option enables the persistence. The kernel (or the initramfs)
236 will look for a partition labeled `casper-rw` on boot to use for the
237 persistence. A bonus: the `toram` option on Ubuntu will load the contents of
238 the USB stick into RAM on boot, enabling relatively snappy behaviour after the
239 boot process completes.
241 I'm not sure what the limits are on the choice of file system for the persistence
242 partition. `ext4` definitely works but `f2fs`, which is otherwise a desirable
243 choice, did not work out of the box. I haven't investigate further.