Linux Labs EspañolES

Storage: disks, LVM and RAID Labs

Take a snapshot before a risky change and roll back

Practitioner25 minYour VMRHEL and Debian families

Objective

A change made to lv_app after the snapshot is gone after the rollback, and you saw both versions side by side before deciding.

Requirements

  • A VM where you have sudo. The lab creates disk images under /var/tmp and attaches them as loop devices, so no spare disk is needed.

Setup

bash
sudo truncate -s 2G /var/tmp/lab-snap.img
sudo losetup -f --show /var/tmp/lab-snap.img          # assume /dev/loop0
command -v lvmdevices && sudo lvmdevices --adddev /dev/loop0
sudo pvcreate /dev/loop0 && sudo vgcreate vg_snap /dev/loop0
sudo lvcreate -n lv_app -L 1G vg_snap && sudo mkfs.xfs /dev/vg_snap/lv_app
sudo mkdir -p /srv/app && sudo mount /dev/vg_snap/lv_app /srv/app
echo "version 1" | sudo tee /srv/app/release.txt

Steps

  1. Create a 300 MiB snapshot of lv_app named pre_upgrade.
  2. Simulate the bad change: overwrite release.txt and add a new file.
  3. Mount the snapshot read-only at /mnt/pre and compare both versions.
  4. Roll lv_app back to the snapshot and mount it again.

Verification

bash
cat /srv/app/release.txt
ls /srv/app
sudo lvs vg_snap
Show the solution
bash
sudo lvcreate -s -L 300M -n pre_upgrade vg_snap/lv_app
echo "version 2 (broken)" | sudo tee /srv/app/release.txt
sudo touch /srv/app/new-feature.conf
sudo mkdir -p /mnt/pre
sudo mount -o ro,nouuid /dev/vg_snap/pre_upgrade /mnt/pre    # nouuid: same XFS UUID as the origin
diff -r /mnt/pre /srv/app
sudo umount /mnt/pre /srv/app
sudo lvconvert --merge vg_snap/pre_upgrade
sudo mount /dev/vg_snap/lv_app /srv/app
cat /srv/app/release.txt      # version 1

The merge consumes the snapshot: lvs no longer lists pre_upgrade. If the origin were still mounted, LVM would postpone the merge to the next activation.

Clean up: umount /srv/app, vgremove -y vg_snap, pvremove and losetup -d /dev/loop0, then remove the image.