+91 – 7838219999

contact@nitinfotech.com

HomeTech SolutionsLinuxCreate LVM Disk Storage: Step-by-Step Guide for Linux Administrators

Create LVM Disk Storage: Step-by-Step Guide for Linux Administrators

Wednesday, July 17, 2024

Creating disk storage with Logical Volume Management (LVM) in Linux involves several steps. LVM provides a flexible approach to disk management, allowing you to create, resize, and manage disk storage efficiently. Here is a step-by-step guide to creating and managing disk storage using LVM, including formatting with XFS and extending the filesystem with resize2fs.

Step-by-Step Guide to Creating LVM Storage

Step 1: Install LVM2 Package

Ensure the lvm2 package is installed on your system. If not, install it using your package manager.

For Debian/Ubuntu:

sudo apt-get install lvm2

For Red Hat/CentOS:

sudo yum install lvm2

Step 2: Prepare Physical Volumes (PVs)

Identify the disks or partitions you want to use for LVM. You can use fdisk or lsblk to list available disks.

lsblk

Assume you want to use /dev/sdb and /dev/sdc.

Create physical volumes on these disks:

sudo pvcreate /dev/sdb /dev/sdc

Verify the creation of physical volumes:

sudo pvs

Step 3: Create a Volume Group (VG)

Create a volume group from the physical volumes. Here, we will create a volume group named vg_data.

sudo vgcreate vg_data /dev/sdb /dev/sdc

Verify the creation of the volume group:

sudo vgs

Step 4: Create Logical Volumes (LVs)

Create logical volumes within the volume group. For example, create a logical volume named lv_data with a size of 100GB.

sudo lvcreate -L 100G -n lv_data vg_data

Alternatively, you can create a logical volume using all the available space:

sudo lvcreate -l 100%FREE -n lv_data vg_data

Verify the creation of the logical volume:

sudo lvs

Step 5: Format the Logical Volume

Format the logical volume with a filesystem. For example, format it with the XFS filesystem.

sudo mkfs.xfs /dev/vg_data/lv_data

Step 6: Mount the Logical Volume

Create a mount point and mount the logical volume.

sudo mkdir /mnt/data sudo mount /dev/vg_data/lv_data /mnt/data

Verify the mount:

df -h

Step 7: Persistent Mount (Optional)

To make the mount persistent across reboots, add an entry to /etc/fstab.

First, get the UUID of the logical volume:

sudo blkid /dev/vg_data/lv_data

Add the following line to /etc/fstab:

UUID=<UUID-from-blkid> /mnt/data xfs defaults 0 2

Replace <UUID-from-blkid> with the actual UUID from the blkid command.

Extending the Filesystem

Step 8: Extend the Logical Volume

When you need to extend the logical volume, use the lvextend command. For example, to extend lv_data by an additional 50GB:

sudo lvextend -L +50G /dev/vg_data/lv_data

Alternatively, extend it to use all the remaining free space:

sudo lvextend -l +100%FREE /dev/vg_data/lv_data

Step 9: Resize the Filesyste

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

For XFS filesystems, use xfs_growfs:

sudo xfs_growfs /mnt/data

For ext4 filesystems, use resize2fs:

sudo resize2fs /dev/vg_data/lv_data

Summary of Commands

1. Install LVM2:

sudo apt-get install lvm2

2. Create Physical Volumes:

sudo pvcreate /dev/sdb /dev/sdc

3. Create a Volume Group:

sudo vgcreate vg_data /dev/sdb /dev/sdc

4. Create a Logical Volume:

sudo lvcreate -L 100G -n lv_data vg_data

5. Format the Logical Volume:

sudo mkfs.xfs /dev/vg_data/lv_data

6. Mount the Logical Volume:

sudo mkdir /mnt/data sudo mount /dev/vg_data/lv_data /mnt/data

7. Make Mount Persistent:

sudo blkid /dev/vg_data/lv_data sudo nano /etc/fstab

8. Extend the Logical Volume:

sudo lvextend -L +50G /dev/vg_data/lv_data

9. Resize the Filesystem:

sudo xfs_growfs /mnt/data

Conclusion

By following these steps, you can create and manage disk storage with LVM in Linux, format the logical volume with XFS, and extend the filesystem when needed. LVM provides flexibility and ease of management for disk storage, allowing you to efficiently handle your storage needs.