Logical Volume Management (LVM) is a flexible method for managing storage on Linux systems. It allows you to dynamically adjust storage space without recreating partitions or deleting data. This guide explains how to create, extend, and permanently mount an LVM.
Create LVM
Create a Physical Volume (PV)
A Physical Volume (PV) is the foundation of LVM. It represents a physical disk or partition to be used by LVM. To create a PV, run:
|
|
Explanation:
/dev/sdb
is the disk you want to use for LVM. Replace it with the actual device name of your disk.- This step marks the disk as LVM-compatible and prepares it for use in a Volume Group.##
Create a Volume Group (VG)
A Volume Group (VG) is a collection of Physical Volumes. It provides the storage space from which Logical Volumes are created. To create a VG and add the previously created PV, use:
|
|
Explanation:
vg_data
is the name of the Volume Group. You can choose any name that makes sense for your setup./dev/sdb
is the Physical Volume to be added to the Volume Group.##
Create a Logical Volume (LV)
A Logical Volume (LV) is the actual usable storage space allocated from a Volume Group. To create an LV that uses all available space in the Volume Group, run:
|
|
Explanation:
-l 100%FREE
allocates all available space in the Volume Group to the Logical Volume.-n lv_data
assigns the namelv_data
to the Logical Volume.vg_data
is the Volume Group from which the space is allocated.##
Create a Filesystem
To use the Logical Volume, you need to create a filesystem on it. In this example, we use the ext4 filesystem:
|
|
Explanation:
/dev/vg_data/lv_data
is the path to the Logical Volume.- The ext4 filesystem is a common choice for Linux systems, but you can use other filesystems like XFS or btrfs depending on your requirements.##
Extend LVM
If you need more storage space, you can extend an existing LVM. Follow these steps:
Resize the Physical Volume (PV)
If the underlying disk has been expanded (e.g., by adding more storage in a virtual machine), you need to update the Physical Volume:
|
|
Explanation:
- This command adjusts the size of the Physical Volume to match the new size of the disk.
/dev/sdb
is the disk you are resizing.##
Extend the Logical Volume (LV)
To use the additional storage space, extend the Logical Volume:
|
|
Explanation:
-l +100%FREE
extends the Logical Volume by using all available free space in the Volume Group./dev/vg_data/lv_data
is the Logical Volume you are extending.##
Resize the Filesystem
After extending the Logical Volume, you need to resize the filesystem to utilize the new space:
|
|
Explanation:
resize2fs
adjusts the size of the ext4 filesystem to match the new size of the Logical Volume./dev/mapper/vg_data-lv_data
is the path to the Logical Volume.##
Add Entry to /etc/fstab
To ensure the Logical Volume is automatically mounted at system startup, you need to add it to the /etc/fstab
file.
Display the UUID
Find the UUID of the Logical Volume:
|
|
Explanation:
- The UUID is a unique identifier for the Logical Volume. It is required to add the volume to
/etc/fstab
.##
Add Entry to /etc/fstab
Add the UUID to the /etc/fstab
file so the volume is mounted automatically at boot:
|
|
Explanation:
- Replace
foo
with the actual UUID of the Logical Volume. /mnt/data
is the mount point where the Logical Volume will be accessible. Adjust this to your desired directory.ext4
is the filesystem type. Use the appropriate type if you used a different filesystem.##
Reload Systemd
Reload the Systemd configuration to apply the changes:
|
|
Explanation:
- This ensures that Systemd recognizes the updated
/etc/fstab
configuration.##
Mount the Volume
Finally, mount the volume to verify that everything is working correctly:
|
|
Explanation:
- This command mounts all filesystems listed in
/etc/fstab
. If there are no errors, the Logical Volume should now be mounted and accessible.