# Logical Volume Management

🌐 **Day 2 of DevOps** 🌐

On the second day of my DevOps journey, I explored the concepts of Logical Volume Management (LVM) on Linux (Ubuntu 22.04), hosted on an Amazon EC2 instance. To meet storage requirements, I utilized Amazon EBS (Elastic Block Store).

I learned several important concepts, including:

* Using **lsblk** and **df -h** commands to view storage information
    
* Linux volumes and Amazon EBS
    
* Differences between **physical volumes, logical volumes, and volume groups** in Linux
    
* Understanding and managing **Logical Volume Manager (LVM)**
    
* **Mounting volumes** in Linux
    
* Managing **Amazon EBS** on EC2 instances
    
* Leveraging LVM for **dynamic storage management**
    

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">For Reference and hands-on practical, I find this video very helpful <a target="_blank" rel="noopener noreferrer nofollow" href="https://youtu.be/Evnf2AAt7FQ?si=dLKIWiFTspxscprT" style="pointer-events: none">TrainWithShubham</a></div>
</div>

## Two Basic Commands

1. `lsblk` : It displays information about all available or specified block devices (disks or partitions) in tree like structure.
    
2. `df -h` : It shows available and used disk space on mounted file systems ,i.e, it shows only mounted volumes.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729350894339/3d1e4d8a-0d18-4f11-8e5b-49109e16ace6.png align="center")

## Logical Volume Management (LVM)

To understand LVM , first of all create a EC2 instance and three blocks of storage on AWS Elastic Block Store

* **Step 1: Launch an EC2 Instance**
    
    1. Go to the AWS Management Console.
        
    2. Create a new **EC2 instance** (choose **Ubuntu** as the operating system).
        
    3. Use the **default root volume size** of **8GB** (attached as `/dev/sda1`).
        
    4. Launch the instance
        
* **Step 2: Create Three EBS Volumes**
    
    1. In the **Elastic Block Store (EBS)** section of the console:
        
        * Create **three volumes**:
            
            * **5GB**
                
            * **7GB**
                
            * **8GB**
                
        * Choose the same **Availability Zone** as your EC2 instance to ensure compatibility.
            
        
        <div data-node-type="callout">
        <div data-node-type="callout-emoji">💡</div>
        <div data-node-type="callout-text">Recommended device names for <strong>root volumes</strong> are <code>/dev/sda1</code>, and for <strong>data volumes</strong>, it is best to choose names from the range <code>/dev/sd[f-p]</code></div>
        </div>
        
    2. Attach these volumes to the instance:
        
        * **5GB** volume: `/dev/sdf`
            
        * **7GB** volume: `/dev/sdg`
            
        * **8GB** volume: `/dev/sdh`
            
* **Step 3: Device Name Changes after Attachment**
    
    * After attaching the volumes, the **Linux kernel** automatically renames them from `/dev/sdf`, `/dev/sdg`, and `/dev/sdh` to `/dev/xvdf`, `/dev/xvdg`, and `/dev/xvdh`.
        
    * **Note:** The naming convention varies slightly, but **root volumes** usually follow `/dev/sda1` or `/dev/xvda`.
        

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">The <strong>EBS volumes</strong> attached are visible under <code>lsblk</code> shell command . Since they are not mounted yet, they won’t appear in <code>df -h</code> shell command.</div>
</div>

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Difference between Volume Attach and Volume Mount in Linux</div>
</div>

| Volume Attach | Volume Mount |
| --- | --- |
| Attaching a volume means linking a storage device (like a block device or virtual disk) to the system, making it **available at the device level**. | Mounting is the process of **making a volume accessible through the filesystem** by assigning it a directory (mount point). |
| The system can detect the device, but **the storage is not yet usable** for data operations. | After mounting, users and applications can **read/write** data to the volume. |

Let us understand LVM using a diagram:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729346292979/42e717fd-cf7f-462e-aa73-9bd55663ea51.png align="center")

Block Storages → Physical Volumes → Volume Groups → Logical Volumes → Mount Logical Volume → Unmount logical volume

To create above flow execute these below shell command on your Linux EC2 instance:

```bash
#Switch to root to access lvm
sudo su

#Go to lvm
lvm

#In the lvm interface execute these shell commands

#Create Physical Volume from attached block storages
pvcreate /dev/xvdf /dev/xvdg /dev/xvdh
pvs # to display all phsyical volumes

#Create Volume Group from two physical volumes
vgcreate test_vg /dev/xvdf /dev/xvdg
vgs # to display all volume groups

#Create logical volume of 3GB from this volume group
lvcreate -L 3G -n test_lv test_vg
lvs # to display all logical volumes


pvdisplay # complete information about physical volumes
vgdisplay # complete information about volume groups
lvdisplay # complete informtaion about logical volumes

#move out of lvm interface
exit

#When you execute the lsblk command in Linux, 
#it will display the logical volumes as part of a tree-like structure 
#under the relevant physical volume (PV). 
#it can also span across single PV or across two or more PVs

#make a temporary mount location, from where the mounted volume can be used
mkdir /mnt/test_lv_mount

#to mount the logical volume firstly format the volume and make it a file system
#ext4 file system
mkfs.ext4 /dev/test_vg/test_lv

#mount the volume "mount <src-lv-location> <mount-location>
mount /dev/test_vg/test_lv /mnt/test_lv_mount

#now you can use this 3GB mounted logical volume
#to verify you can see it under df -h command with /mnt/test_lv_mount as mount location
cd /mnt/test_lv_mount
mkdir devops
echo "This is mount location" > test.txt
cd /
cat /mnt/test_lv_mount/devops/test.txt
#Output : This is mount location
#Successfully mounted

#unmount "umount <mount-location>
umount /mnt/test_lv_mount

cat /mnt/test_lv_mount/devops/test.txt
#Output : No such file or directory exists
```

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Mount Volume → Store Data → Unmount volume : Does this means all data stored at that mount location lost ?? NO, we can again mount to the same mount location with the same logical volume</div>
</div>

```bash
#format the third PV into a file system of ext4
mkfs -t ext4 /dev/xvdh
```

**Using LVM with EBS for Dynamic Storage Management:**

```bash
#open lvm interface
lvm

#to extend the size of test_lv from 3GB to 10GB
lvextend -L +7G /dev/test_vg/test_lv
#if test_vg has sufficient space then test_lv space can be increased

#to reduce the size of test_lv from 10GB to 4GB
#BE CAREFUL WHILE REDUCING VOLUME SPACE : DATA LOSS !!!!
lvreduce -L 4G /dev/test_vg/test_lv

#move out of lvm interface
exit
```

---

**If you found this useful**, consider sharing it! 📢✨
