Skip to content
Go back

Guide to LVM on Ubuntu

Published:

A Beginner’s Guide to LVM on Ubuntu

LVM, or Logical Volume Management, is a powerful tool that provides a more flexible way to manage storage than traditional partitioning. It allows you to resize, move, and create file systems without worrying about the physical layout of the hard drive. Here’s a step-by-step guide for a beginner to practice LVM on an Ubuntu system, using a virtual machine is highly recommended for this practice.


Install LVM2

First, you need to ensure the LVM2 package is installed on your Ubuntu system. Open a terminal and run the following command:

sudo apt update && sudo apt install lvm2 -y

Create a Physical Volume (PV)

A physical volume (PV) is a physical disk or partition that is designated for use by LVM. It’s the building block of an LVM setup. For this guide, we’ll assume you have a spare disk named /dev/sdc. If you don’t have a spare disk, you can create a partition on an existing disk and use that. Use the following command to create a PV:

output

sudo pvcreate /dev/xvdc

To check the status of your newly created physical volume, use the pvdisplay command:

sudo pvdisplay
# or 
sudo pvs

Create a Volume Group (VG)

A volume group (VG) pools one or more physical volumes into a single logical entity. It’s the central storage space from which you’ll create logical volumes. Create a VG named janak-vg using your new physical volume:

output

sudo vgcreate janak-vg /dev/xvdb

To verify the creation of the volume group, you can use the vgdisplay command:

sudo vgdisplay
# or
sudo vgs

Extend the Volume Group.

output

```bash
sudo vgextend janak-vg /dev/xbdc
```

(Replace `janak-vg` with the name of your volume group, e.g., `janak-vg`.)

Create a Logical Volume (LV)

A logical volume (LV) is the equivalent of a partition in a traditional setup. It’s a “virtual” partition created from the free space in a volume group. This is the volume that you will format and mount. Create a 5GB logical volume named janak-data-lv within janak-vg:

output

sudo lvcreate -L 550G -n janak-data-lv janak-vg
# or
sudo lvcreate -l +100%FREE -n janak-data-lv janak-vg

To see your new logical volume, use the lvdisplay command:

sudo lvdisplay
# or
sudo lvs

The difference between the options -L and -l in the lvextend command is:

To summarize:

OptionUsageExampleMeaning
-LSpecify size with units (e.g., G, M, etc.)-L +5GIncrease logical volume by 5 gigabytes
-lSpecify size in logical extents-l +100%FREEIncrease logical volume by all free extents

Thus, -L is for size units, and -l is for extents within LVM’s internal allocation system.


Format and Mount the Logical Volume

output

The logical volume is now a raw block device and needs to be formatted with a file system before it can be used. We’ll use the ext4 file system. The path to your new LV will be /dev/janak-vg/janak-data-lv.

sudo mkfs.ext4 /dev/janak-vg/janak-data-lv

Now that the LV is formatted, you can mount it to make it accessible to your system. First, create a mount point, which is a directory where the LV will be attached.

sudo mkdir -p /mnt/data

Next, mount the logical volume to the new directory:

sudo mount /dev/janak-vg/janak-data-lv /mnt/data

Making Logical Volumes Permanent

To ensure your logical volume’s storage is accessible after a reboot, you need to configure your system to automatically mount it during startup. This is done by adding an entry for the volume in the /etc/fstab file.


Find the Logical Volume’s UUID

First, you need to find the unique identifier (UUID) of your logical volume. Using the UUID is the most reliable method, as it ensures the correct volume is always mounted, even if its path changes.

Explanation:

Example Output:

/dev/mapper/my_vg-my_lv: UUID="9e64e9a3-5c74-4b55-a249-14a09e0a0f5a" TYPE="ext4"

Copy the long string of characters within the quotation marks after UUID=.


Step 2: Create a Mount Point Directory

A mount point is an empty directory that acts as the entry point for your logical volume. If you don’t already have one, create it.

Explanation:


Step 3: Edit the /etc/fstab File

The /etc/fstab file is the system’s configuration file for mounting filesystems at boot. You’ll add a new line to this file. Use a text editor like nano.

Explanation:

Add the following line to the end of the file. Replace the UUID and mount point with your own information.

UUID="9e64e9a3-5c74-4b55-a249-14a09e0a0f5a" /mnt/data_storage ext4 defaults 0 2

Field Explanations:

Press Ctrl + O to save the file and then Ctrl + X to exit nano.


Step 4: Test the fstab Entry

It’s crucial to test your new fstab entry before rebooting to ensure there are no errors.

Explanation:

If there are no error messages, your logical volume has been successfully mounted. You can now verify that everything is working as expected.

Now, your logical volume will be mounted automatically and ready to use every time you reboot.


Resize a Logical Volume (LV)

One of the main benefits of LVM is the ability to easily resize logical volumes. Let’s extend our LV to 555GB.

output

First, you need to extend the logical volume itself:

sudo lvextend -L +5G /dev/janak-vg/janak-data-lv

Next, you must resize the file system on the logical volume to recognize the new space. For ext4, use resize2fs:

sudo resize2fs /dev/janak-vg/janak-data-lv

Use df -h again to confirm the new size. The logical volume should now show as 7GB.


Remove the Logical Volume:

To remove a logical volume (LV), you must first unmount it if it’s currently in use. After unmounting, you can use the lvremove command to delete it.

Unmount the Logical Volume

First, determine if the logical volume is mounted. You can use the df -h command to see a list of mounted filesystems. Look for the logical volume you want to remove, which will likely be listed as /dev/VG_name/LV_name.

Once you’ve identified its mount point, use the umount command to unmount it.

sudo umount /mnt/data

Remove the Logical Volume

After confirming the logical volume is unmounted, you can safely remove it using lvremove. This action is permanent and will delete all data on the volume.

sudo lvremove /dev/janak-vg/janak-data-lv

Remove the volume group:

To remove a volume group (VG), you must first remove all the logical volumes (LVs) residing within it. This is a critical safety measure to prevent data loss. Once the logical volumes are gone, you can remove the volume group itself.

output

sudo vgremove janak-vg

Remove the Physical Volume from the Volume Group

You’ll use the vgreduce command to remove /dev/xvdc from the janak-vg volume group. Since there are no logical volumes (LVs) or data on this physical volume, this command will execute without any issues.

output

sudo vgreduce janak-vg /dev/xvdc

Remove the physical volume:

output

sudo pvremove /dev/xvdb

Your /dev/xvdb disk is now back to its initial state.


Thank You


Suggest Changes

Previous Post
Guide to NFS on Linux
Next Post
Hosting wordpress website using just Docker and domain name with ssl in simple steps.