1. Introduction

Developers created ext4 (Fourth Extended File System) for Linux, making it the most widely used journaling file system that succeeded ext3. Moreover, it’s an improvement over its predecessors, ext2 and ext3.

Taking regular snapshots of the ext4 file system is crucial for maintaining data integrity and ensuring quick recovery from failures. This process is crucial for several reasons. Firstly, it ensures data integrity, and additionally, it facilitates backup, disaster recovery, and system maintenance.

In this tutorial, we’ll learn how to take snapshots of the ext4 file system in Linux.

2. Prerequisites

To begin with, Logical Volume Manager (LVM) is the focus of this tutorial. We’re using LVM because it provides the ability to take ext4 snapshots, making it an ideal choice for efficient and flexible disk management.

LVM is a powerful and flexible disk management system in Linux that provides ways to manage disk storage compared to traditional partitioning. To get started, it’s imperative that we take a few things into consideration:

  • confirm that the Linux system uses ext4 file system
  • ensure there’s enough space on the system to store the snapshot
  • check if we’ve installed and configured LVM (Logical Volume Manager)
  • verify volume group (VG) and logical volume (LV) information

Next, for guaranteed success, the options above should be verified before we take a snapshot of the file system on a Linux operating system.

3. System Setup

In this section, let’s confirm all the prerequisites.

3.1. Checking File System

Now, let’s check the file system type on our Linux operating system using the df command:

$ df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
tmpfs          tmpfs  1.6G  2.3M  1.6G   1% /run
/dev/sda2      ext4    77G   25G   49G  34% /
...

As shown above, the result shows that ext4 is the file system of our hard drive partition. df -Th outputs a list of our mounted file systems along with their types, total size, used space, available space, usage percentage, and mount points in a human-readable format.

3.2. Checking Available Storage

Next, enough storage space is an important prerequisite for this process; however, it’s best to have a separate disk for snapshot and backup storage. So, on this Linux system, there’s already a disk dedicated to it. Let’s take a look at it using the lsblk command:

$ lsblk
NAME                MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda                   8:0    0   80G  0 disk 
├─sda1                8:1    0  487M  0 part /boot
├─sda2                8:2    0    1K  0 part 
└─sda5                8:5    0 79.5G  0 part 
  ├─kali--vg-root   254:0    0 78.6G  0 lvm  /
  └─kali--vg-swap_1 254:1    0  980M  0 lvm  [SWAP]
sdb                   8:16   0   60G  0 disk 
sr0                  11:0    1 1024M  0 rom

From the result above, we can spot the sdb disk with 60 GB of space. Here, we used another disk added to this machine.

However, we can partition a hard disk into two distinct storages: one for the Linux operating system and the other for snapshots and backups.

So, if we intend to take and manage snapshots, we should plan for this during the installation of our Linux operating system by adding a partition specifically for managing snapshots.

3.3. LVM Installation

The next thing we’ll need to do is install LVM. We can install LVM on Ubuntu/Debian with apt-get:

$ sudo apt-get install lvm2

Next, let’s explore the syntax for installing LVM on Fedora/CentOS/RHEL:

$ sudo yum install lvm2

Consequently, if we’re just installing LVM on our Linux system and haven’t yet configured it, we need to create a physical volume, logical volume, and volume group before proceeding further.

3.4. Confirm Volume Group (VG) and Logical Volume (LV)

Let’s proceed to check the VG of this Linux system:

$ sudo vgdisplay
  --- Volume group ---
  VG Name               kali-vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  7
...

As shown above, vgdisplay confirms the volume group, along with other information.

Next, we can check the LV using the lvdisplay command:

$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/kali-vg/root
  LV Name                root
  VG Name                kali-vg
  LV UUID                vzcyRj-CPR7-KI1G-Hi04-PZI0-RK1Z-KuWuP1
  LV Write Access        read/write
...

Consequently, we’ve confirmed both the VG and the LV, as shown in the results of our commands above.

4. Setup for Taking Snapshot With LVM

In this section, let’s look at the things that need to be done before our system is ready to take snapshots using LVM.

4.1. Formatting the Designated Disk

Firstly, let’s use the fdisk command to get essential information about the disk specified:

$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 60 GiB, 64424509440 bytes, 125829120 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

From the result, we can see that there is no partitioning on the sdb disk. So, that means we can proceed to partition the disk by specifying its device path and no further options:

$ sudo fdisk /dev/sdb   
Welcome to fdisk (util-linux 2.40.1).
...
Device does not contain a recognized partition table.
Created a new DOS (MBR) disklabel with disk identifier 0xccf97bd9.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-125829119, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-125829119, default 125829119): 

Created a new partition 1 of type 'Linux' and of size 60 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

As shown above, we used n to initiate the partitioning, then picked primary using p, chose 1 out of the available default numbers, and pressed the Enter key on our keyboard to accept default options for the first and last sectors. Finally, we use the w option to write to memory all the changes we’ve made and save the partition.

Let’s check the changes that have been made to the sdb disk:

$ sudo lsblk  
NAME                MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda                   8:0    0   80G  0 disk 
...
sdb                   8:16   0   60G  0 disk 
└─sdb1                8:17   0   60G  0 part 
sr0                  11:0    1 1024M  0 rom

Here, we can see that we now have a partition named sdb1 on the disk.

4.2. Create and Extend Volumes

Now, let’s create a physical volume for the new partition:

$ sudo pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.

Next, let’s extend the group volume to logically increase the available space for storage:

$ sudo vgextend kali-vg /dev/sdb1
  Volume group "kali-vg" successfully extended

With this command, we’ve successfully extended the volume group of kali-vg. Let’s view the volume group of kali-vg using the vgdisplay command:

$ sudo vgdisplay kali-vg
  --- Volume group ---
  VG Name               kali-vg
...
  Alloc PE / Size       20357 / <79.52 GiB
  Free  PE / Size       15359 / <60.00 GiB
  VG UUID               1Xcs9y-3fp9-YOda-h171-LZn7-70Wh-sTbdJX

According to the output of the command, Free PE, which is the new disk we added to the system, has been added to kali-vg and the size is 60.00 GiB.

5. Create the Snapshot

Now, after all the prep work, we can proceed to take a snapshot of our system’s file system:

$ sudo lvcreate --size 10G --snapshot --name root_snapshot /dev/kali-vg/root
      Logical volume "root_snapshot" created.

As shown in the result above, we successfully created a snapshot of the original logical volume: /dev/kali-vg/root. In addition, the –size, –snapshot, and –name instructs the command lvcreate to create a 10G logical volume to store changes made to the original volume, take the current state of the original volume, and name the storage file.

Let’s check on the snapshot we’ve just created:

$ sudo lvs
  LV            VG      Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root          kali-vg owi-aos---  78.56g                                                    
  root_snapshot kali-vg swi-aos---  10.00g      root   0.02                                   
  swap_1        kali-vg -wi-ao---- 980.00m 

The command created the snapshot for the original volume, named root_snapshot, with a size of 10G, as shown in the result above.

6. Conclusion

In this article, we learned how to take snapshots of the ext4 file system on a Linux system, covering all the basics from prerequisites, to using LVM, and finally to taking snapshots.

By following the detailed steps outlined in this article, Linux administrators can effectively manage their system’s storage and protect their valuable data against unforeseen issues.