1080*80 ad

How to Create LVM Logical Volumes in Linux

Linux systems rely on robust storage management, and one of the most flexible approaches available is using Logical Volume Management (LVM). Unlike traditional partitions tied to specific disk areas, LVM provides a layer of abstraction, allowing you to manage storage more dynamically. Understanding how to create logical volumes is fundamental to leveraging LVM’s power for easy resizing, snapshots, and flexible allocation.

At its core, LVM operates using three primary concepts:

  1. Physical Volumes (PVs): These are your raw disks or partitions that LVM can use. You must initialize a disk or partition to be recognized as a PV.
  2. Volume Groups (VGs): A VG is a pool of storage created by combining one or more PVs. It acts as a single large virtual disk from which logical volumes are carved out.
  3. Logical Volumes (LVs): These are the partitions you actually use. An LV is created from the free space within a VG. You format an LV with a filesystem and mount it like a traditional partition, but its size isn’t fixed to a physical disk segment.

Here’s a step-by-step guide to creating and using a logical volume:

Step 1: Identify and Prepare Disks (Create Physical Volumes)

First, identify the disk or partition you want to use. Let’s say you have a disk like /dev/sdb or a partition like /dev/sdc1. You need to turn these into Physical Volumes recognized by LVM.
Use the pvcreate command:
pvcreate /dev/sdb
Or for a partition:
pvcreate /dev/sdc1
You can verify your PVs using pvdisplay or pvs.

Step 2: Create a Volume Group

Now, combine one or more Physical Volumes into a Volume Group. Let’s create a VG named my_volume_group using /dev/sdb.
Use the vgcreate command:
vgcreate myvolumegroup /dev/sdb
To add another PV later (e.g., /dev/sdc1) to the same VG, you would use vgextend:
vgextend myvolumegroup /dev/sdc1
Check your Volume Groups with vgdisplay or vgs.

Step 3: Create a Logical Volume

From the space available in your Volume Group (my_volume_group), you can now create Logical Volumes. When creating an LV, you specify its size and name. Let’s create an LV named my_logical_volume of 10GB within my_volume_group.
Use the lvcreate command:
lvcreate -L 10G -n mylogicalvolume myvolumegroup
The -L flag specifies the size, and -n specifies the name. The new LV will be created under /dev/mapper/my_volume_group-my_logical_volume or sometimes linked under /dev/my_volume_group/my_logical_volume.
View your Logical Volumes with lvdisplay or lvs.

Step 4: Format the Logical Volume

Before you can use the new Logical Volume, you must format it with a filesystem, such as ext4 or XFS.
Use the mkfs command (replace ext4 with your desired filesystem type):
mkfs.ext4 /dev/myvolumegroup/mylogicalvolume
Or using the /dev/mapper path:
mkfs.ext4 /dev/mapper/myvolumegroup-mylogicalvolume

Step 5: Mount and Make Persistent

Finally, create a directory where you want to access the volume and mount the Logical Volume to that directory.
Create a mount point, for example:
mkdir /mnt/mydata
Mount the volume:
mount /dev/myvolumegroup/mylogicalvolume /mnt/mydata
Your logical volume is now ready to use at /mnt/mydata.

To ensure the volume is mounted automatically every time the system boots, add an entry to the /etc/fstab file. You should use the UUID of the filesystem for reliability. Find the UUID using blkid:
blkid /dev/myvolumegroup/mylogicalvolume
Add a line to /etc/fstab similar to this, replacing the UUID and mount options as needed:
UUID=youruuidhere /mnt/mydata ext4 defaults 0 2

By following these steps, you establish a flexible storage setup using LVM. This foundation allows for easy expansion of logical volumes by adding more physical disks to the volume group, or shrinking/moving volumes, making storage management much more adaptable to changing needs.

Source: https://kifarunix.com/how-to-create-lvm-logical-volumes-in-linux/

900*80 ad

      1080*80 ad