LVM (Logical Volume Manager) has some useful enhancements. You can make a snapshot, then check new programs and if they don't work as expected or you don't like them you can simply revert to your previous installation. This is also very useful for developer to test things. You can make backups on the running system from a snapshot without worrying about changes during the backup. No need to set devices read only or exclude particular directories.

Can this be used on a Raspberry Pi? Is there a way to setup LVM on it?


You can move the root partition of Raspbian to a LVM volume.
But what does it help me?

  1. You can take snapshots from your running system.
  2. You can install software and if you don't like it you can revert to the snapshot.
  3. If you like installed software you can commit the snapshot, in fact delete the snapshot.
  4. It is very easy to make backups on the running system. Just make a snapshot, mount it and backup it, no worry about changing files from the running system.
  5. You can heavy modify your system, also with drivers and firmware and deinstall essential software until it doesn't boot anymore (kernel panic, rainbow screen). It doesn't matter. You simply boot into the snapshot, means into the running system the snapshot was taken. Then you can revert to the snapshot.

Creating initramfs.gz with LVM drivers

Moving the root partition isn't the problem but the LVM drivers must be loaded on boot up so the root partition can be mounted and used for booting. Fortunately the Raspberry Pi supports loading a ramdisk for initrd so we have to do an additional step to create the initramfs.gz containing drivers for LVM.

The following is only for a quick setup an initramfs where you have to monitor kernel updates by yourself. To use a more automated setup an initramfs you can look at How can I use an init ramdisk (initramfs) on boot up Raspberry Pi?.

If you are unexperienced with linux please try it first with a new image how I did for this How To. You can loose all data.

For reference I use Raspbian Buster Lite 2019-06-20, flashed it to a SD Card and boot it in a RasPi. First do an upgrade to get the latest software versions.

rpi ~$ sudo apt update
rpi ~$ sudo apt full-upgrade
rpi ~$ sudo apt install initramfs-tools-core


Enable using an initramfs by inserting initramfs initramfs.gz (without =) in /boot/config.txt, install drivers for LVM and create the initramfs:

rpi ~$ sudo apt install lvm2

# I only want to load listed modules
rpi ~$ sudo sed -i 's/^MODULES=most/MODULES=list/' /etc/initramfs-tools/initramfs.conf

# In `/boot/config.txt` define to load the ramdisk
rpi ~$ sudo sed -i '1i initramfs initramfs.gz followkernel' /boot/config.txt

# Create the ramdisk. You can ignore warnings.
rpi ~$ sudo mkinitramfs -o /boot/initramfs.gz
rpi ~$ sudo apt clean

Reboot. It should do it as always but now using the initram. Check with:

rpi ~$ journalctl -b | grep initr
Jul 12 17:13:55 raspberrypi kernel: Trying to unpack rootfs image as initramfs...
Jul 12 17:13:55 raspberrypi kernel: Freeing initrd memory: 8088K

Moving root partition to the LVM volume

Poweroff your system and attach its SD Card to a debian like computer. It must also have LVM2 installed (sudo apt install lvm2) even you don't use it there. We need it to setup LVM on the SD Card. We will now backup the root partition of the SD Card, create a logical volume (lv) there and restore it to the lv. I will create a volume of 3 GB. You can use what you like but leave enough space for snapshots. I suggest to start with minimal space because with LVM it is no problem to extend the volume at any time. My SD Card is attached to /dev/sdb:

Backup the root partition

pc ~$ sudo -Es
pc ~# e2fsck -f /dev/sdb2
pc ~# mkdir /mnt/sdb2
pc ~# mount /dev/sdb2 /mnt/sdb2
pc ~# tar -cvzf root-partition.tar.gz -C /mnt/sdb2 -V "backup of the root partition" ./ # last characters are dot and slash
pc ~# umount /mnt/sdb2

If you like you can test the backup before overwriting the root-partition:

pc ~# tar -tvzf root-partition.tar.gz

Create LVM volume, leave boot partition /dev/sdb1 untouched.

pc ~# parted /dev/sdb set 2 lvm on
pc ~# pvcreate -y /dev/sdb2
pc ~# vgcreate rpi_vg /dev/sdb2
pc ~# lvcreate rpi_vg --name root_lv --size 3G
pc ~# mkfs.ext4 -L rootfs /dev/mapper/rpi_vg-root_lv

Mount the new volume and restore the root partition:

pc ~# mount /dev/mapper/rpi_vg-root_lv /mnt/sdb2
pc ~# tar -xvzf root-partition.tar.gz -C /mnt/sdb2

Change device name for root partition in boot/cmdline.txt and in etc/fstab to /dev/mapper/rpi_vg-root_lv because its now on a logical volume:

pc ~# mount /dev/sdb1 /mnt/sdb2/boot
pc ~# sed -i 's/root=PARTUUID=[a-z0-9]*-02/root=\/dev\/mapper\/rpi_vg-root_lv/' /mnt/sdb2/boot/cmdline.txt
pc ~# sed -i 's/^PARTUUID=[a-z0-9]*-02/\/dev\/mapper\/rpi_vg-root_lv/' /mnt/sdb2/etc/fstab

Clean up

pc ~# umount /mnt/sdb2/boot
pc ~# umount /mnt/sdb2
pc ~# e2fsck -f /dev/mapper/rpi_vg-root_lv
pc ~# rmdir /mnt/sdb2
pc ~# exit
pc ~$

Take snapshots and backups

Now put the SD Card into the raspi, boot and login. Everything should be the same as before but we can use the Logical Volume Manager. Look at man lvm what's available. You can make your first snapshot from this base installation so you can always revert to it:

rpi ~$ sudo lvcreate --snapshot --name rpi_base --size 3G rpi_vg/root_lv
rpi ~$ sudo lvs
  LV       VG     Attr       LSize Pool Origin  Data%  Meta%  Move Log Cpy%Sync Convert
  root_lv  rpi_vg owi-aos--- 3.00g
  rpi_base rpi_vg swi-a-s--- 3.00g      root_lv 0.00
rpi ~$

Revert and remove(!) snapshot. You should create it afterwards immediately again.

rpi ~$ sudo lvconvert --merge rpi_vg/rpi_base
rpi ~$ sudo systemctl reboot

Be patient! It takes a long time, about some minutes. Never switch off your raspi.

Login and immediately create the snapshot again:

rpi ~$ sudo lvcreate --snapshot --name rpi_base --size 3G rpi_vg/root_lv

To commit simply delete the snapshot:

rpi ~$ sudo lvremove rpi_vg/rpi_base

To make a backup:
simply make a snapshot, mount it and backup it, e.g. with the base installation. You can use what ever backup tool you like.

rpi ~$ sudo mount /dev/mapper/rpi_vg-rpi_base /mnt/
rpi ~$ tar -czf - -C /mnt/ ./ | ssh backup-server dd of=raspi.tar.gz bs=10k

To boot into the snapshot:
Modify /boot/cmdline.txt and /etc/fstab and change the entries:


Then boot the RasPi. In case you have destroid your working system then you can revert it to the snapshot with:

rpi ~$ sudo lvconvert --merge rpi_vg/rpi_base

Before rebooting don't forget to change /boot/cmdline.txt back to root=/dev/mapper/rpi_vg-root_lv.

Don't forget

For future mounts of the SD Card on your PC don't forget to use:

pc ~$ sudo mount /dev/sdb1 /mnt/sdb1
pc ~$ sudo mount /dev/mapper/rpi_vg-root_lv /mnt/sdb2

And don't forget to update the initramfs.gz when you have made driver updates:

rpi ~$ sudo mkinitramfs -o /boot/initramfs.gz

[1] Can the raspberry boot to an LVM root partition?
[2] Can a Raspberry Pi be used to create a backup of itself?

  • thanks for sharing. do you know what would be pro's and cons of using this over openzfs? – ipatch Jan 11 at 20:17
  • @ipatch Sorry, I don't know openzfs. I have only used lvm yet because its direct supported by the kernel and part of the operating system. I think that's an important advantage. – Ingo Jan 11 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.