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?

up vote 1 down vote accepted

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 it.
  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.

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 Stretch Lite 2018-06-27, flashed it to a SD Card and boot it in a RasPi.

Upgrade and 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 update
rpi ~$ sudo apt full-upgrade

Reboot.

rpi ~$ sudo sed -i '1i initramfs initramfs.gz' /boot/config.txt
rpi ~$ sudo apt install lvm2
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:

/dev/mapper/rpi_vg-root_lv
to
/dev/mapper/rpi_vg-rpi_base

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


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

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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