Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LVM snapshots

LVM snapshots are logical volumes that reflect the state of the snapshoted volume at the exact moment in time the snapshot was created. Useful for backups and reference points we can revert back to.

Creating snapshots

$ sudo lvcreate --size 5G --snapshot --name root-backup /dev/vg0/root
  Logical volume "root-backup" created.

We created a new volume named root-backup, of 5GB size and type snapshot. The target volume for which we created a snapshot is /dev/vg0/root (my root partition).

The 5 GB size is a buffer area. Until the changes made on our root partition fit inside that preallocated space, we are able to revert back to the point referenced by the snapshot. When we pass that threshold the snapshot will become inactive (= useless).

With lvs we can check the available space left on the snapshot buffer. (Or with lvdisplay, but that would be more verbose for our usecase)

$ sudo lvs
  LV          VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  docker-pool vg0  twi-a-t---   3.80g             0.00   0.02                            
  root        vg0  owi-aos--- 238.00g                                                    
  root-backup vg0  swi-a-s---   5.00g      root   0.00

Next I install all the packages that start with the gnome prefix, and see how that changes usage.

$ sudo dnf install "gnome-*"
[... ignored lines ...]
Install  589 Packages

Total download size: 519 M
Installed size: 1.4 G

$ sudo lvs
  LV          VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  docker-pool vg0  twi-a-t---   3.80g             0.00   0.02                            
  root        vg0  owi-aos--- 238.00g                                                    
  root-backup vg0  swi-a-s---   5.00g      root   47.56

Rolling back to a snapshot

$ sudo lvconvert --merge /dev/vg0/root-backup 
  Logical volume vg0/root contains a filesystem in use.
  Can't merge over open origin volume.
  Merging of snapshot vg0/root-backup will occur on next activation of vg0/root.

As seen from the message the restoration will happen at the next activation of vg0/root. In my case that means a system reboot. You might want to drop down to a runlevel 3, or multi-user.target / rescue.target (since everybody's using systemd nowadays), and unmount and restore that way. Reboot looks simpler for me.

Persisting changes

In order to persist the changes that have been made since the snapshot is created you can just remove the snapshot logical volume.

$ sudo lvremove /dev/vg0/root-backup 
Do you really want to remove active logical volume root-backup? [y/n]: y
  Logical volume "root-backup" successfully removed

Pitfalls

  • Unless you've allocated space in advance in your volume group for snapshots you won't be able to use this functionality. On a default install you might allocate all the disk space to a single logical volume like I did. In that scenario you can create somme free space for snapshoting by downsizing your logical volumes. I wasn't forced to do that since I had another hard drive on which I've created a new LVM physical volume, and attached that to my main volume group.

To check if you have enough space to create snapshots, use the vgdisplay command and check the "Free PE / Size" field

$ sudo vgdisplay                                
  --- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  66
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               247.76 GiB
  PE Size               4.00 MiB
  Total PE              63427
  Alloc PE / Size       62030 / 242.30 GiB
  Free  PE / Size       1397 / 5.46 GiB
  VG UUID               819Sbr-dcnl-45Ym-7ety-b5TH-MNqZ-2L5rGM
  • The merge isn't instant and for my example it took a couple of minutes for the merge to complete. You can see if a merge is still in progress using lvdisplay and checking if the "LV Snapshot Status" field is present. When the merging is done that field won't show up and vgdisplay will show the snapshot allocated size as free space once again
sudo lvdisplay       
  --- Logical volume ---
  LV Path                /dev/vg0/root
  LV Name                root
  VG Name                vg0
  LV UUID                b3bnyS-O4Ju-4vQx-13LK-zEJq-gvE6-N4GkbX
  LV Write Access        read/write
  LV Creation host, time x, 2016-02-17 00:23:24 +0200
  LV snapshot status     source of
                         root-backup [active]
  LV Status              available
  # open                 1
  LV Size                238.00 GiB
  Current LE             60928
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment