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 lvm2For Red Hat/CentOS:
sudo yum install lvm2Step 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.
lsblkAssume you want to use /dev/sdb and /dev/sdc.
Create physical volumes on these disks:
sudo pvcreate /dev/sdb /dev/sdcVerify the creation of physical volumes:
sudo pvsStep 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/sdcVerify the creation of the volume group:
sudo vgsStep 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_dataAlternatively, you can create a logical volume using all the available space:
sudo lvcreate -l 100%FREE -n lv_data vg_dataVerify the creation of the logical volume:
sudo lvsStep 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_dataStep 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/dataVerify the mount:
df -hStep 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_dataAdd the following line to /etc/fstab:
UUID=<UUID-from-blkid> /mnt/data xfs defaults 0 2Replace <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_dataAlternatively, extend it to use all the remaining free space:
sudo lvextend -l +100%FREE /dev/vg_data/lv_dataStep 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/dataFor ext4 filesystems, use resize2fs:
sudo resize2fs /dev/vg_data/lv_dataSummary of Commands
1. Install LVM2:
sudo apt-get install lvm22. Create Physical Volumes:
sudo pvcreate /dev/sdb /dev/sdc3. Create a Volume Group:
sudo vgcreate vg_data /dev/sdb /dev/sdc4. Create a Logical Volume:
sudo lvcreate -L 100G -n lv_data vg_data5. Format the Logical Volume:
sudo mkfs.xfs /dev/vg_data/lv_data6. Mount the Logical Volume:
sudo mkdir /mnt/data sudo mount /dev/vg_data/lv_data /mnt/data7. Make Mount Persistent:
sudo blkid /dev/vg_data/lv_data sudo nano /etc/fstab8. Extend the Logical Volume:
sudo lvextend -L +50G /dev/vg_data/lv_data9. Resize the Filesystem:
sudo xfs_growfs /mnt/dataConclusion
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.

