LVM snapshot of a physical server before upgrading

When using physical machines, virtual snapshots are not an option

Today we will upgrade a physical server, let’s start by creating an LVM Snapshot of the system in case anything goes wrong.

We need more disk space in the Volume Group

First we add an extra disk to the server “/dev/sdb” and create a partition “/dev/sdb1” of LVM type “8e” using fdisk. We then add the partition as a physical LVM volume with “pvcreate” and extend the volume group “rhel”.

[root@localhost ~]# pvcreate /dev/sdb1
[root@localhost ~]# vgextend rhel /dev/sdb1

It’s important that the free space is in the same volume group as we are creating the snapshot in.

[root@localhost ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  rhel   2   2   0 wz--n- 20.99g <20.00g

Next step is to perform the actual snapshot

The snapshot will be stored as a Logic Volume named “snap0” in the same Volume Group as the root filesystem.

[root@localhost ~]# lvcreate --snapshot --name snap0 --extents 100%FREE /dev/rhel/root
  Logical volume "snap0" created.

Upgrade the server

The machine is running Redhat Enterprise Linux 8 (RHEL) which means that we will use the “dnf” package manager to do the upgrade.

[root@localhost ~]# dnf check-update
[root@localhost ~]# dnf update

If all goes well

If the upgrade is successful, the snapshot can safely be removed with “lvremove”

[root@localhost ~]# lvremove /dev/rhel/snap0 
Do you really want to remove active logical volume rhel/snap0? [y/n]: y
  Logical volume "snap0" successfully removed.

[root@localhost ~]# reboot

Revert back if something goes wrong

In case of problems, the upgrade can be reverted with “lvconvert” using the “–merge” parameter. Since this is the root partition, a reboot is required.

[root@localhost ~]# lvconvert --merge /dev/system/snap_data
  Can't merge until origin volume is closed.
  Merging of snapshot system/snap_data will occur on next activation of system/data.

[root@localhost ~]# reboot

et voilà