Skip to main content

How to use LVM snapshots to restore Linux systems

It can be cumbersome to create and recreate lab or practice systems. Use LVM and a basic WebUI to quickly restore your Linux systems back to a golden image.
Image
Woman with camera taking a snapshot

Image by Gerd Altmann from Pixabay

In cases where kickstarting or using scripts to automate the post-provisioning of systems is not feasible or practical, it can be a challenge to create test systems for yourself or coworkers. For example, if you're trying to use various Red Hat Satellite systems for your own learning, it would take a long time to install a virtual machine, sync content, create content views, and so on. What if you finally get the system built how you want it, but while studying, you accidentally make it unusable? Either you're trying to debug a system when you don’t know how it works in the first place or you're going through the time-consuming process of rebuilding it from scratch. If you make these systems available to your coworkers, then the likelihood of a system becoming unusable increases.

[ You might also like: Logical Volume Manager (LVM) versus standard partitioning in Linux ]

As a solution to these challenges, I used Logical Volume Manager (LVM) snapshots. To begin, I first created a new virtual machine (VM) whose backing store was a logical volume. I installed the operating system, configured services, and set the system up as a gold image. I then took a snapshot of the LVM and booted the virtual machine from that snapshot. When the system needed to be reset back to the gold state, it was a simple process of destroying and removing the current VM and creating a new snapshot. I could then start the virtual machine from that recreated snapshot.

To make this more accessible to users, a WebUI was created, which used PEAR components, PHP, and sudo access for Apache to run the default lvremove, lvcreate, virsh, and xm commands. In addition, two custom scripts were created, one of which would simply SSH to the VM and run who to see if anybody was using it, and the other was a script that would execute those LVM commands to virsh destroy $1, lvremove /dev/vgvms/$1.ss -f, and then lvcreate -L +5G -s -n $1.ss /dev/vgvms/$1.gold. Using this method, it was important to make sure the VM name matched the LVM name and that the snapshot was *.ss with the gold image named as *.gold.

I had a little title with a link to lorenz, which was a system using the same type of setup but was a hypervisor for basic RHEL clients, not Satellite servers. The main table in the WebUI gets populated with code:

if(is_array($inactive_guests)) {
  foreach ($inactive_guests as $key=>$value) {
    $tpl->setVariable(array(
                            'server_name'  => $key,
                            'virsh_status' => $value['status'],
                            'fqdn' => $value['fqdn'],
                            'userinfo' => $value['userinfo'],
                            'notes' => $value['notes'],
                            'inactivecontrols' => inactive_controls($key)));
    $tpl->parse('inactiveserver');
  }
}

The WebUI displays those fields for each system, giving this output:

Image
WebUI output

The main advantages of this method are that you can quickly restore to a known good state, the WebUI reports if anybody is using a particular system, and it’s a straightforward system to set up. However, some of the drawbacks are that it’s fairly storage-intensive and there's no way to reserve a system to prevent somebody else from using it or restoring it to the golden state. However, with a little additional development, those issues could be addressed.

Credits for the initial development of this project go to Jeremy Agee and Patrick Wiggins. You may download the WebUI source code.

[ Free course: Red Hat Satellite Technical Overview. ] 

Topics:   Linux   Linux administration  
Author’s photo

Peter Gervase

I am a Senior Principal Security Architect at Verizon. Before that, I worked at Red Hat in various roles such as consulting and in the Solutions Architect where I specialized in Smart Management, Ansible, and OpenShift. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.