Recently, after an OS updated and rebooted, I was not able to login to the desktop environment. When that happened, I thought to myself, “I wish I could just do a quick EBS snapshot like I would on the cloud before running any OS updates.”

On my Linux desktop, the root volume is an LVM volume, I knew that snapshot was possible, but never tried to create one before. A friend at work encouraged that I should make use of it, and it was not that hard to get started. Well, let’s see how it goes as I’m about to explore how LVM snapshot works on my desktop.

According to this [1], first I need to check for free space in the volume group where the snapshot is to be created on.

[root@watamem ~]# vgs
  VG             #PV #LV #SN Attr   VSize   VFree
  fedora_watamem   1   2   0 wz--n- 232.39g 88.91g

[root@watamem ~]# lvs
  LV   VG             Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home fedora_watamem -wi-ao---- 53.48g
  root fedora_watamem -wi-ao---- 90.00g

From the above output, there are 88.91 GB available in the VG fedora_watamem. Next, I’m going to create a snapshot volume named root_snap of /dev/fedora_watamem/root that is 3 GB is size.

[root@watamem ~]# lvcreate -L 3G -n root_snap -s /dev/fedora_watamem/root
  Logical volume "root_snap" created.
  • -L or --size: size
  • -n or --name: name of the (snapshot) volume
  • -s or --snapthos: type of the volume, snapshot
[root@watamem ~]# lvs
  LV        VG             Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home      fedora_watamem -wi-ao---- 53.48g
  root      fedora_watamem owi-aos--- 90.00g
  root_snap fedora_watamem swi-a-s---  3.00g      root   0.02

[root@watamem ~]# lvdisplay fedora_watamem/root_snap
  --- Logical volume ---
  LV Path                /dev/fedora_watamem/root_snap
  LV Name                root_snap
  VG Name                fedora_watamem
  LV UUID                AwlTIG-zpZV-722e-vr4l-H8Wj-mprA-Dlb8fI
  LV Write Access        read/write
  LV Creation host, time watamem.local, 2024-07-26 12:00:19 +1000
  LV snapshot status     active destination for root
  LV Status              available
  # open                 0
  LV Size                90.00 GiB
  Current LE             23040
  COW-table size         3.00 GiB
  COW-table LE           768
  Allocated to snapshot  0.02%
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:4

Whenever more data is added or written to /root, I should expect to see an increase in the Data% column.

To restore snapshot, the volume requires to be unmounted. Then use the lvconvert command with the --merge option to merge a snapshot into its original (the root) volume. Here a sample command:

# lvconvert --merge fedora_watamem/root_snap
Merging of volume fedora_watamem/root_snap started.
  fedora_watamem/root: Merged: 100.00%

A bit of warning, I didn’t get to try running the lvconvert command on my desktop. Clearly, I would need to use a rescue bootable device in order to unmount the root volume of my desktop. This might happen sometimes in the future when the OS update causes the issue again.

References: