]> git.friedersdorff.com Git - max/notes.git/blob - content/multiboot_live_linux.rst
Write about creating multiboot usb sticks
[max/notes.git] / content / multiboot_live_linux.rst
1 Multiboot USB Stick with Persistence from the Command Line
2 ==========================================================
3
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
9 :status: published
10
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.
15
16 Every time I need one, I find that a different one is recommended. Invariably it 
17 is not packaged for my current distribution.
18
19 This is a set of instructions for creating a multiboot USB stick with optional
20 persistence for one of the installed distributions.
21
22 .. contents::
23
24 Get ISOs
25 --------
26
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
30 updated images.
31
32 Partition USB stick
33 -------------------
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 
37 loader.
38
39 Using fdisk:
40
41 .. code-block:: console
42
43         # fdisk /dev/sdX 
44
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.
48
49         Command (m for help): o
50         Created a new DOS disklabel with disk identifier 0xf0e82e51.
51
52         Command (m for help): n
53         Partition type
54            p   primary (0 primary, 0 extended, 4 free)
55            e   extended (container for logical partitions)
56         Select (default p): p
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
60
61         Created a new partition 1 of type 'Linux' and of size 2 GiB.
62         Partition #1 contains a vfat signature.
63
64         Do you want to remove the signature? [Y]es/[N]o: y
65
66         The signature will be removed by a write command.
67
68         Command (m for help): t
69         Selected partition 1
70         Hex code (type L to list all codes): b
71         Changed type of partition 'Linux' to 'W95 FAT32'.
72
73         Command (m for help): w
74         The partition table has been altered.
75         Syncing disks.
76
77 The above creates a 2G partition of the `FAT32` type.  My drive previously
78 contained a `FAT32` file system, hence the warning.
79
80 Create file system
81 ------------------
82 Create a `FAT32` file system in the newly created partition:
83
84 .. code-block:: console
85
86         # mkfs.fat -F 32 /dev/sdX1
87
88 Install Grub
89 ------------
90
91 Mount your `FAT32` partition and create a `grub` directory:
92
93 .. code-block:: console
94
95         # mount /dev/sdX1 /mnt/multiboot
96         # mkdir /mnt/multiboot/grub
97
98 and install grub:
99
100 .. code-block:: console
101
102         # grub-install /dev/sdX \
103                 --target=x86_64-efi \
104                 --efi-directory=/mnt/multiboot/ \
105                 --boot-directory=/mnt/multiboot/boot \
106                 --removable 
107
108 Copy ISOs
109 ---------
110
111 Copy all ISOs that you want to be able to boot to some location on the `FAT32`
112 partition:
113
114 .. code-block:: console
115
116         # mkdir /mnt/multiboot/iso_boot
117         # cp /tmp/all_my_isos/*.iso /mnt/multiboot/iso_boot/
118
119 Configure Grub
120 --------------
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:
124
125 .. code-block:: cfg
126
127         set timeout=10
128         set default=0
129         insmod loopback
130         insmod all_video
131
132         menuentry "Run Xubuntu 18.04 64 bit" {
133                 loopback loop /iso_boot/xubuntu-18.04.2-desktop-amd64.iso
134                 set gfxpayload=keep
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
137         }
138
139
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.
143
144 You can list the content of the ISO as follows:
145
146 .. code-block:: console
147
148    # modprobe loop
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.
155
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.
158
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.
162
163 Unmount the ISO and destroy the loop device when you are done:
164
165 .. code-block:: console
166
167    # umount /mnt/tmp
168    # losetup -d /dev/loop0
169
170 Enable Persistence (Optional)
171 -----------------------------
172
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.
177
178 Create another partition on the USB stick and format it with the file system of
179 your choice:
180
181 .. code-block:: console
182
183    # fdisk /dev/sdX
184
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.
188
189
190    Command (m for help): n
191    Partition type
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
198
199    Created a new partition 2 of type 'Linux' and of size 4 GiB.
200
201    Command (m for help): w
202    The partition table has been altered.
203    Calling ioctl() to re-read partition table.
204    Syncing disks.
205
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
212
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
217
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.
221
222 Finally edit the grub configuration file on the USB drive to add the option
223 to boot with persistence enabled:
224
225 .. code-block:: cfg
226
227    menuentry "Run Xubuntu 18.04 64 bit - Persistent, in RAM" {
228            loopback loop /iso_boot/xubuntu-18.04.2-desktop-amd64.iso
229            set gfxpayload=keep
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
232    }
233
234
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.
240
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.