0% found this document useful (0 votes)
16 views15 pages

Linux LVM Concept

LVM (Logical Volume Manager) is a Linux tool that allows for flexible management of storage, enabling users to create, resize, and organize storage space across multiple drives. It involves three main components: Physical Volumes (PV), Volume Groups (VG), and Logical Volumes (LV), which facilitate dynamic storage management. Additionally, AWS EBS (Elastic Block Store) can be integrated with LVM to provide persistent block storage for EC2 instances.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views15 pages

Linux LVM Concept

LVM (Logical Volume Manager) is a Linux tool that allows for flexible management of storage, enabling users to create, resize, and organize storage space across multiple drives. It involves three main components: Physical Volumes (PV), Volume Groups (VG), and Logical Volumes (LV), which facilitate dynamic storage management. Additionally, AWS EBS (Elastic Block Store) can be integrated with LVM to provide persistent block storage for EC2 instances.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

What is LVM?

In simple terms, LVM (Logical Volume Manager) is a tool in Linux that helps you manage your
computer’s storage. It lets you create, resize, and organize your storage space without being
stuck with the size of your hard drives or partitions.

Imagine LVM as a flexible storage organizer. You can move, resize, and even combine storage
from different hard drives to create one big storage space that’s easy to manage. It helps you
make the most of your storage and avoid running out of space.

Don’t worry if this sounds a bit complicated right now. As you read on, we’ll explain how
volumes work and how LVM fits into the picture.

What are volumes in linux ?

volumes in Linux are like containers that hold your data. They represent storage space on your
computer or server, and you can use them to store files and programs.
These volumes can be physical (like an SSD or HDD), partitioned sections of a physical disk, or
even logical/virtual disks managed by a tool called LVM (Logical Volume Manager).

Types of Volumes

1. Physical Volume (PV)

●​ A physical volume is the actual storage device, like an HDD (Hard Disk Drive) or SSD
(Solid State Drive).

Example: Your laptop's SSD or an external hard drive.

📌 Key Point: Every volume must exist on a physical storage device.


2. Volume Group (VG)
●​ A storage pool made from multiple physical volumes.

Example: Combining an SSD[10 GB ] and HDD [ 10 GB ] in one group .

📌 Key Point: VGs merge multiple devices into one.


3. Logical Volume (LV) - Managed by LVM

●​ A flexible storage unit inside a volume group, easy to resize or move.

Example: Creating a 10GB Logical Volume (LV) from a 20GB Volume Group (VG)
📌 Key Point: LVs allow for dynamic storage management, including resizing and moving.
What is AWS EBS?

AWS EBS (Elastic Block Store) is a block storage service that provides persistent storage for
Amazon EC2 instances. It behaves like a physical volume (PV) in Linux, offering block-level
storage that can be attached to instances.

📌 Key Point: AWS EBS is not an LV, but it is used as a PV in LVM.​


📌 You can create Logical Volumes (LVs) inside AWS EBS by using LVM for more flexibility in
managing your storage.

Now, let's set up LVM on top of AWS EBS

I already have an EC2 instance running, but you can launch one yourself—it's a simple process.
By default, AWS provides an 8GB EBS volume with your instance unless you customize the
storage settings during creation.​

Connect to Remote Server via SSH


Now Let’s understand some Important commands ​

`1- Lsblk -This command in Linux shows a clear list of all storage devices and their partitions.
Think of it as a map of your system's disks—it tells you what storage is available, how it's
divided, and where it's mounted.

Breaking it Down

●​ NAME → The disk name (xvda)


●​ SIZE → Total size of the disk
●​ TYPE → Whether it's a disk or partition
●​ MOUNT POINT → If mounted, it shows where (e.g., / or /mnt/data)

📌 If a volume is attached but not mounted, the "MOUNTPOINT" column will be empty.​
📌 If a volume is mounted, it will show the directory where it is accessible.
Difference between Attached and mounted

1️⃣ Attached:

●​ The storage (like an EBS volume) is connected to the system.


●​ It's not yet available for use or accessible by the OS.
●​ Key Point: Just connected, but not ready to store data yet.

2️⃣ Mounted:

●​ The storage is linked to a specific folder (mount point), making it usable.


●​ You can access it and store data in that folder.
●​ Key Point: Mounted means it’s now accessible and ready for use.

📌 Example in AWS:
●​ When you attach an EBS volume to an EC2 instance, it's connected, but not usable until
you mount it to a folder (e.g., /mnt/data).
●​ Key Point: You can't store data on the volume until it’s mounted.

2- Df -h - This command helps monitor your filesystem(volumes) space usage and provides a
warning when storage is running low, so you can take action before running out of space.

📌 Key Point: You can see i have used filesystem and volumes interchangeably in this context
Here's why:

1️⃣ Volume refers to a logical storage unit, such as a disk or partition.

2️⃣ File System refers to the way data is organized and stored on that volume.

So, while "filesystem" technically refers to how data is organized on a volume, it's common to
use the term "filesystem" when talking about volumes in this context, because that's what the
df-h command reports on.

Let's roll out some additional EBS volumes and attach to our EC2 instance

I manually created volumes of sizes 10GB, 12GB, and 14GB, while the 8GB volume was
automatically created when I launched the EC2 instance.
Now that we've created the volumes, we can proceed to attach them to our EC2 instance.

📌 Key Point: Ensure you use the correct device name as per the recommended naming

conventions. I’ve named my 10 GB EBS volume as /dev/sdf, but Linux may automatically
rename it to /dev/xvdf as mentioned .

Similarly, I've attached all the volumes, and you can do the same.
Now that we've successfully attached the volumes

let's check if they are showing up on our EC2 instance.

We previously added new volumes and named them sdf, sdg, and sdh. However, Linux
renamed them to xvdf, xvdg, and xvdh. While the volumes are present as we can see , their
mount points are empty because they have only been attached, not mounted yet .

Before Moving further lets go through some lvm commands

pvcreate - Create a physical volume (PV)​


vgcreate - Create a volume group (VG)​
lvcreate - Create a logical volume (LV)​
lvextend - Extend LV size​
lvreduce - Reduce LV size​
vgextend - Add PV to VG​
vgreduce - Remove PV from VG​
lvremove - Remove LV​
vgremove - Remove VG​
pvremove - Remove PV
pvs - gives summary of all physical volumes
vgs - gives summary of all volume groups
lvs - gives summary of all logical volumes

Become a root user

ubuntu@ip-172-31-5-208:~$ sudo su ​
root@ip-172-31-5-208:/home/ubuntu#

Run lvm command

Now we are inside the Logical volume manager [LVM]

Let's create the Physical volume

EBS volumes, by default, are just raw devices that the operating system sees. To manage them
with LVM (Logical Volume Manager), you need to initialize them as Physical Volumes (PVs)
using the pvcreate command.

So your physical volumes are created -


You can see your physical volumes like this -

Let's create the volume group

Creating a volume group with /dev/xvdf and /dev/xvdg

lvm > vgcreate tws_vg /dev/xvdf /dev/xvdg ​


​ Volume group "tws_vg" successfully created ​
lvm>

you can see your volume groups like this -

Now finally Let’s create the logical volume

Creating a logical volume tws_lv of 10GB .


You can see your logical volumes like this -

View Attached Storage Devices with lsblk​


📌 Key Point: We can see that a 10GB logical volume has been created, which is currently
allocated from xvdf. In the future, if we create more logical volumes, they might be allocated
from xvdg as well.
Check Disk Space Usage with df -h

📌 Key Point :But surprisingly We can't see xvdf, xvdg, and xvdh because they are not

mounted yet. Let's go ahead and mount them.

Let’s do Mounting Now

1- Mounting Logical volume tws_lv

●​ Create a Mount Point

Before mounting, we need to create a directory where the logical volume will be accessible.

●​ Format the Logical Volume

To create a filesystem on the logical volume, we use the mkfs.ext4 command.

You can format any logical volume by specifying its correct path, which follows the format:

syntax - mkfs.ext4 /dev/<VG_NAME>/<LV_NAME>

Replace <VG_NAME> and <LV_NAME> with your actual volume group and logical volume
names.

●​ Mount the logical volume

mount <logical_volume> <mount_point>


<logical_volume>: Path to the logical volume (e.g., /dev/tws_vg/tws_lv).
<mount_point>: Directory where the volume will be mounted (e.g.,
/mnt/tws_lv_mount).

After running this command, the contents of /dev/tws_vg/tws_lv will be available inside
/mnt/tws_lv_mount.​

📌

Key Point : To confirm that the logical volume has been successfully mounted, use the
df -h command:


We can see our logical volume tws_lv successfully mounted at /mnt/tws_lv_mount

Now that the logical volume is mounted, the space is ready for use.

You can create new folders and files within it as needed.


2- Mounting AWS EBS volume disk directly

●​ Create a Mount Point

To mount an AWS EBS volume to an EC2 instance, first, you need to create a mount point
directory where the volume will be attached.

The first directory is our mounting point

●​ Format the AWS EBS volume disk

To format an AWS EBS volume disk , you can use the following syntax:

syntax - mkfs -t ext4 <path_to_aws_ebs_volume>



●​ Mount the AWS EBS volume disk

Use the following command to mount the EBS volume (/dev/xvdh) to the mount point
(/mnt/ebs_volume)


📌 Key Point : To confirm that the logical volume has been successfully mounted, use the
df -h command:​



You can see that all mounted filesystems are listed, and /dev/xvdh (the AWS EBS volume) is
mounted at /mnt/tws_diskmount. Now, you can make use of this space for storing files and
directories.

Conclusion

I hope this guide helped you understand how to efficiently manage storage on AWS EC2 using
LVM. From creating Physical Volumes to formatting and mounting AWS EBS volumes, we've
covered it all. With this knowledge, you're now equipped to manage scalable and flexible
storage effectively. Keep learning and growing!

Hare krishna 🙏

You might also like