I wanted to boot to an LVM root partition on my USB drive and looked around the Internet. The examples given were very helpful to me understanding what to do, but they were out-of-date with the lastest Raspberry Pi OS, Bookworm.
This documents how I configured booting from a USB drive with the root on an LVM volume using Raspberry Pi 4, and Raspberry Pi OS Lite (64-bit) (Bookworm).
You could do the same thing booting from a MicroSD card. Just tweak the instructions. If re-using the same SD card, you'd want to redo the partions and reformat them (including LVM steps) after tar
ing the contents.
This readme assumes you are familiar with Raspberry Pi installation and that you have all the peripheral equipment to do the installation such as power supply, case, keyboard, and display.
All of the work here was done on an Ubuntu 22.04 desktop and a Raspberry Pi 4B. On the desktop, I had an available USB port for the USB drive and a MicroSD card reader (both of these were provided by an external USB-C dock).
The MicroSD card was a 32GB Samsung Evo Plus. The USB drive was a 1 TB Seagate USB HDD.
You'll need a keyboard and monitor for the initial Raspberry Pi OS installation unless you configured all the extra settings including SSH and networking in the rpi-imager
creation of the image (by clicking the gear icon).
The follownig tools were used.
Installed on the desktop via apt-get install rpi-imager
.
Installed on the desktop, and eventually the pi, via apt-get install lvm2
.
- Plug both MicroSD and USB drive into Ubuntu desktop. For this example,
/dev/sda
is the MicroSD and/dev/sdb
is the USB drive. - Using
parted
, remove all partitions on both devices. THIS DESTROYS ALL EXISTING DATA - On the USB drive, create a 500MB FAT32 partition and the remainder of the disk as an LVM partition:
parted /dev/sdb mkpart primary fat32 2048s 512MiB
parted /dev/sdb mkpart primary ext4 512MiB 100%
parted /dev/sdb set 2 lvm on
- Format and label the FAT partition:
mkfs.fat -F 32 -n bootfs-rpi /dev/sdb1
- Setup LVM on the USB drive, create and format the root volume:
pvcreate /dev/sdb2
vgcreate usb-rpi /dev/sdb2
lvcreate -L 30G -n rootfs usb-rpi
mke2fs -t ext4 -L rootfs-rpi /dev/usb-rpi/rootfs
- Launch
rpi-imager
- Choose OS, Raspberry Pi OS (other), Raspberry Pi OS Lite (64-bit)
- Choose Storage, select the MicroSD device
- Optionally set other options by clicking the gear icon.
- Write
- After safely ejecting the MicroSD, place it in the Raspberry Pi, boot, and follow the installation.
- Once the OS installation is complete, install
LVM
. Look at the output of thelvm2
install and note that there is a hook that updates theinitramfs
image -- soLVM
is automatically included in the Init RAM Disk!!! This is a significant improvement from previous examples. It must have been an update in Raspberry Pi OS.
sudo apt update
sudo apt-get install lvm2 -y
- DO NOT MAKE ANY OTHER UPDATES AT THIS TIME
- Safely shutdown the pi with
sudo shutdown now
- Remove the MicroSD and place it back in the desktop.
- Mount the SD card partitions (assuming it is
/dev/sda
) and archive the contents:
sudo -Es
mkdir -p rpi/boot/firmware
mount /dev/sda2 rpi
mount /dev/sda1 rpi/boot/firmware
tar -cvzf rpi.tar.gz -C rpi ./
umount /rpi/boot/firmware
umount /rpi
- Mount the USB partitions and restore the contents. Leave them mounted for now:
mount /dev/usb-rpi/rootfs rpi
mount /dev/sdb1 rpi/boot/firmware
tar -xvzf rpi.tar.gz -C rpi
- In
rpi/boot/firmware/cmdline.txt
, update theroot=
entry to beroot=LABEL=rootfs-rpi
. This will cause the root partition to be loaded from our LVM rootfs volume. SinceLVM
is a part of the Init RAM Disk image now, the LVM volume will be usable during the boot sequence. - In
rpi/etc/fstab
, update the entries for/
and/boot/firmware
toLABEL=rootfs-rpi
andLABEL=bootfs-rpi
respectively.
- Unmount the partitions:
umount rpi/boot/firmware
umount rpi
rmdir rpi
- Safely eject (NOTE: You shouldn't need to deactivate the LVM volumes to safely eject).
- Plug the USB drive into the Raspberry Pi
- The MicroSD should not be in the Pi
- Boot up!
- Optionally use
raspi-config
to set the boot order to be USB drive first.
Answer to "Easy backups and snapshots of a running system with LVM" on Stack Exchange