# Linux LVM - Creating, Extending, and Shrinking Logical Volumes

Linux LVM (Logical Volume Manager) is a system for managing disk storage, allowing you to create, resize, and delete partitions more flexibly than traditional partitioning schemes. LVM gives you the power to manage multiple hard drives as a single volume group, dynamically allocate storage, and resize partitions without downtime. This guide covers the steps to create and manage LVM on Linux.

**Prerequisites:**

* You should have root or sudo privileges.
    
* Make sure you have unused disk space or a spare disk available for creating an LVM.
    

**Steps to Create LVM in Linux**

**<mark>Step 1: Install LVM</mark>**

Before using LVM, ensure the `lvm2` package is installed:

```plaintext
sudo apt update
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726490604070/848b0e9d-1d02-487c-a933-f895e42423ec.png align="center")

```plaintext
sudo apt install lvm2
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726490650474/90bb6aa6-42ce-4fe2-8c35-0eaf0714c867.png align="center")

**<mark>Step 2: Identify Available Disks</mark>**

Use the `lsblk` command to list available disks:

```plaintext
lsblk
```

This will show a list of disks.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726490724360/4e97552c-98d6-4b18-827c-877129393f5d.png align="center")

From the `lsblk` output, here's a summary of available disks:

* `sda` (40 GB): This is your main disk with three partitions:
    
    * `sda1` (1 MB): Unallocated partition.
        
    * `sda2` (513 MB): EFI boot partition mounted at `/boot/efi`.
        
    * `sda3` (39.5 GB): The main partition mounted at `/`.
        

The rest of the listed items are `loop` devices (snap packages) and an optical drive (`sr0`), which cannot be used for LVM.

It appears you do not have any unused or unallocated physical disks like `/dev/sdb` for LVM creation. If you'd like to proceed with LVM on your current setup, one option is to shrink an existing partition or attach a new virtual disk if you're in a virtualized environment.

**<mark>Step 3: Add a new disk</mark>**

*I am using a VM on vSphere. Here are the steps to add a new virtual disk in vSphere*

Log in to your vSphere Client. &gt; Power off the virtual machine we want to modify.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726547898574/9ec4e07c-d256-411f-930a-0138a6fa2fa5.png align="center")

Right-click the VM and choose **Edit Settings**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726547978272/45a63c67-3c23-43e5-b621-39b7cff3e417.png align="center")

In the **Virtual Harddisk** tab, click **Add** at the top.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726548107977/09a94bc4-9379-4ca9-ac3f-931b929a66ad.png align="center")

Select **Hard Disk** &gt; Click **Next**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726548237213/7aee3596-be0a-4f24-84f4-206bf912609b.png align="center")

**Add New Virtual Disk**:

Select Create a **new virtual disk**. &gt; Click **Next**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726548364519/561287cd-1fb5-4ce0-b52c-04792cdfc010.png align="center")

Specify the disk size (e.g., 10GB).

Choose the type of disk provisioning &gt; Select **Thin Provision**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726548586624/db391630-7ef6-44aa-8608-969e32e9e0e7.png align="center")

Click **Finish**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726548713913/a5f56dbf-e22a-428b-98b2-31e40b7ec450.png align="center")

Click **OK** to save the settings.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726548783230/99c9bb45-e578-4b93-a700-72c927d8d4d3.png align="center")

Power on the VM.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726548923399/4c0c7d92-305a-4da9-b700-7a68d0bd3d54.png align="center")

**<mark>Step 4 :Verify the New Disk is Recognized:</mark>**

Once the VM is powered on, log in to the Linux system.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726549238528/8b53af72-38b1-470b-bfbf-091a16a54e0a.png align="center")

Run the following command to verify the new disk is recognized:

```plaintext
lsblk
```

You should see a new disk, likely labeled as `/dev/sdb` or `/dev/sdc` (depending on your setup).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726549511825/7a55ef6a-3917-48e9-b812-5036eac080ad.png align="center")

If the new disk does not appear, run:

```plaintext
sudo partprobe
```

**<mark>Step 5: Create Physical Volumes (PV)</mark>**

Once the new disk is detected, initialize it for LVM:

```plaintext
sudo pvcreate /dev/sdb
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726549657126/a8dc43ad-e90e-4d90-af64-528bfca36ce9.png align="center")

To verify the physical volumes:

```plaintext
sudo pvdisplay
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726549700051/9f1c8b48-7503-4a36-a7ca-bba7bf6485df.png align="center")

If want, You can initialize multiple disks:

```plaintext
sudo pvcreate /dev/sdb /dev/sdc
```

**<mark>Step 6: Create a Volume Group (VG)</mark>**

Add the new physical volume to a volume group:

```plaintext
sudo vgcreate my_volume_group /dev/sdb
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726549888857/e1a2c496-71a9-416b-b85a-a1a1f3990963.png align="center")

You can verify the volume group:

```plaintext
sudo vgdisplay
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726549928312/3ea5a21b-7157-4a21-8b7b-f2ebe3ee05bd.png align="center")

**<mark>Step 7: Create Logical Volumes (LV)</mark>**

Logical Volumes are the partitions you create from the volume group. Use `lvcreate` to create a logical volume:

```plaintext
sudo lvcreate -L 8G -n my_logical_volume my_volume_group
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726550201117/f3829a60-0d0e-4918-ae29-81a8cba2446d.png align="center")

* `-L 8G`: Allocates 8GB of space.
    
* `-n my_logical_volume`: Names the logical volume.
    
* `my_volume_group`: Specifies the volume group from which to allocate space.
    

**To verify the logical volume:**

```plaintext
sudo lvdisplay
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726550252925/665a455d-6d03-45c6-9ea6-a4b632542886.png align="center")

**<mark>Step 8: Format the Logical Volume</mark>**

Format the logical volume with a filesystem (e.g., ext4):

```plaintext
sudo mkfs.ext4 /dev/my_volume_group/my_logical_volume
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726550340194/b6ba9a6c-2822-40da-9f91-4d62d526876a.png align="center")

**<mark>Step 9: Mount the Logical Volume</mark>**

Mount the logical volume to a directory:

```plaintext
sudo mkdir /mnt/my_lvm
sudo mount /dev/my_volume_group/my_logical_volume /mnt/my_lvm
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726550416811/aa1dc884-4e95-42a3-9a5f-671ba6f32528.png align="center")

To ensure the volume mounts automatically after a reboot, add the following line to `/etc/fstab`:

**Open the** `/etc/fstab` File: Open the `/etc/fstab` file in a text editor with root privileges:

```plaintext
sudo nano /etc/fstab
```

**Add an Entry for the Logical Volume**: Add the following line at the end of the file. Replace `/dev/my_volume_group/my_logical_volume` with the actual path of your logical volume, and `/mnt/my_lvm` with your mount point:

```plaintext
/dev/my_volume_group/my_logical_volume /mnt/my_lvm ext4 defaults 0 0
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726551671083/dc334c5e-3707-4afa-a6da-80d4fb37488f.png align="center")

* `/dev/my_volume_group/my_logical_volume`: This is the device path of the logical volume.
    
* `/mnt/my_lvm`: This is the directory where the logical volume will be mounted.
    
* `ext4`: This is the filesystem type. If you formatted the logical volume with a different filesystem (e.g., `xfs`), replace `ext4` with that.
    
* `defaults`: These are the default mount options.
    
* `0 0`: The first `0` means not to dump the filesystem, and the second `0` indicates that this filesystem should not be checked on boot.
    

**Save the Changes**:  
In **Nano**, press `CTRL + O` to save, and then `CTRL + X` to exit the editor.

**Test the Configuration**:  
You can test that your new entry is correct without rebooting by running:

```plaintext
sudo mount -a
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726551812077/d26bc170-1567-4db3-9c61-5548d5ab0e9d.png align="center")

If no errors are shown, your logical volume is now mounted.

**Verify the Mount**:  
To verify that it's mounted, run:

```plaintext
df -h
```

You should see your logical volume listed under the mount point (`/mnt/my_lvm`).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726551966583/7124b4bf-c595-4363-aa08-a6d59ad9be5f.png align="center")

It looks like your logical volume is successfully mounted.

This indicates that:

* The logical volume is mounted on `/mnt/my_lvm`.
    
* It has a total size of 7.8 GB, with 24 KB used and 7.4 GB available.
    

**<mark>Step 10: Extend Logical Volumes</mark>**

To manage the size of logical volumes in LVM (Logical Volume Manager), you can either extend or shrink them based on your needs.

**<mark>Extending a Logical Volume</mark>**

#### **Check the Available Free Space**

First, check the available space in your volume group:

```plaintext
sudo vgdisplay
```

Look for the `Free PE / Size` line to see how much free space is available.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726553330007/c8f1045c-30dd-4ff6-b159-0c43c2ec5f90.png align="center")

It looks like your volume group, `my_volume_group`, has 2.00 GiB of free space available. Here’s how you can extend a logical volume with this free space:

**Check the Logical Volume**

First, identify the logical volume you want to extend. Use the following command to list your logical volumes:

```plaintext
sudo lvdisplay
```

Look for the LV Name and LV Size that you wish to extend.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726553600318/3fba8113-26da-485a-b26d-c6a3be7e4572.png align="center")

#### **Extend the Logical Volume**

To extend the logical volume, use the `lvextend` command. You can specify the size to increase by:

```plaintext
sudo lvextend -L +1G /dev/my_volume_group/my_logical_volume
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726553681022/df8303c2-7cce-4b16-8691-d4c06de441bd.png align="center")

#### **Resize the Filesystem**

After extending the logical volume, you need to resize the filesystem to use the new space.

For **ext4** filesystems, use:

```plaintext
sudo resize2fs /dev/my_volume_group/my_logical_volume
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726553822625/21902c63-ac3e-4521-b7df-f4875d17c386.png align="center")

For **xfs** filesystems, use:

```plaintext
sudo xfs_growfs /dev/my_volume_group/my_logical_volume
```

Replace `/mnt/my_lvm` with the mount point of your logical volume.

**Verify the Changes**

Finally, verify that the logical volume has been extended and the filesystem has been resized:

```plaintext
sudo lvdisplay
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726553909277/f505716e-197d-430f-ad85-64c15d2adcad.png align="center")

Now logical volume `my_logical_volume` has been successfully extended to 9.00 GiB. This output confirms that the extension was successful.

```plaintext
df -h
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726553956363/931fa2c7-02dc-47e8-ad34-40d4f6ee00df.png align="center")

The output of `df -h` shows that your logical volume `/dev/mapper/my_volume_group-my_logical_volume` is now 8.8 GiB in size, which aligns with the 9.00 GiB size you set for the logical volume.

It looks like the filesystem has been successfully resized to use the new space.

**<mark>Step 11: Shrink Logical Volumes</mark>**

#### **Backup Your Data**

Before shrinking a logical volume, make sure to back up any important data. Shrinking can potentially lead to data loss if not done carefully.

**Unmount the Filesystem**

If possible, unmount the filesystem to avoid data corruption. This step is particularly important for shrinking filesystems.

```plaintext
sudo umount /mnt/my_lvm
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726554898201/b5a21a3d-c78b-4936-87a9-33a04a0fe28b.png align="center")

#### **Check Filesystem Consistency**

Ensure that the filesystem is clean. For **ext4** filesystems, use:

```plaintext
sudo e2fsck -f /dev/my_volume_group/my_logical_volume
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726554951704/d4542da1-d744-4dd6-ae55-5b12b7d84832.png align="center")

#### **Resize the Filesystem**

Shrink the filesystem before shrinking the logical volume. For **ext4**, use:

Here, `5G` is the new size for the filesystem. Adjust this based on how much space you want to keep.

```plaintext
sudo resize2fs /dev/my_volume_group/my_logical_volume 5G
```

It looks like the filesystem has been successfully resized to accommodate the new size of 5G. The output indicates that the filesystem has been resized to 1,310,720 blocks, which corresponds to a total size of around 5G (since each block is 4k).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726555504147/5587832e-ba9a-4d87-847e-2757dccc7f14.png align="center")

<mark>For </mark> **<mark>xfs</mark>**<mark>, shrinking is not supported directly. You would need to back up the data, recreate the filesystem, and restore the data.</mark>

#### **Shrink the Logical Volume**

After resizing the filesystem, shrink the logical volume using:

```plaintext
sudo lvreduce -L 5G /dev/my_volume_group/my_logical_volume
```

Here, `5G` should be less than or equal to the size you set with `resize2fs`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726555657771/d246a284-dc88-41a1-9c2c-5da286829110.png align="center")

The logical volume has been successfully resized to 5.00 GiB, as indicated by the output.

Just to summarize:

* You resized the filesystem to 5G using `resize2fs`.
    
* You then shrank the logical volume to 5G using `lvreduce`.
    

**Remount the Filesystem**:

```plaintext
sudo mount /dev/my_volume_group/my_logical_volume /mnt/my_lvm
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726555961367/2df19948-248e-406b-90d8-efa2f243650b.png align="center")

**Verify the Changes**:

Check the new sizes with:

```plaintext
sudo lvdisplay
df -h
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726556010413/660f716b-0432-4954-8441-a9fde3ebda1f.png align="center")

The output from `lvdisplay` confirms that the logical volume size has been successfully reduced to 5.00 GiB. Here's a summary of the relevant details:

* **LV Path**: `/dev/my_volume_group/my_logical_volume`
    
* **LV Size**: 5.00 GiB
    

```plaintext
df -h
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726556064394/2e83b78b-d03f-452f-8c0d-bed2bb97cb42.png align="center")

The `df -h` output shows that the filesystem on the logical volume `/dev/mapper/my_volume_group-my_logical_volume` has been resized to approximately 4.9 GiB, which aligns with the 5.00 GiB you set for the logical volume.

Here’s a summary:

* **Filesystem Size**: 4.9G
    
* **Used**: 24K
    
* **Available**: 4.6G
    
* **Mounted on**: /mnt/my\_lvm
    

**<mark>Summary of Commands</mark>**

**Extending:**

```plaintext
sudo vgdisplay  # Check available space
sudo lvextend -L +5G /dev/my_volume_group/my_logical_volume  # Extend LV
sudo resize2fs /dev/my_volume_group/my_logical_volume  # Resize filesystem
```

**Shrinking:**

```plaintext
sudo e2fsck -f /dev/my_volume_group/my_logical_volume  # Check filesystem
sudo resize2fs /dev/my_volume_group/my_logical_volume 5G  # Shrink filesystem
sudo lvreduce -L 5G /dev/my_volume_group/my_logical_volume  # Shrink LV
```

### Conclusion

LVM provides a robust and flexible way to manage storage on Linux. With these steps, you've created and managed LVM partitions, giving you more control over how you allocate and resize storage. By using LVM, you can easily expand storage, create backups, and manage multiple drives more efficiently than traditional partitioning methods.
