Published Dec. 15, 2024, 7:07 p.m. by dwest
This topic was the inspiration for starting a tech blog. Unless you are a Systems Administrator or a Linux Geek, then you probably never paid much attention to LVM, since standard partitioning is “good enough” for most cases. However, I am often asked for help when someone runs out of disk space and their system was setup with logical volumes. This is often the case for Red-Hat based distro’s. I started out by creating a wiki page to both help guide newer users when configuring it, but also as a helper tool that I can reference if I can’t remember the syntax for a specific operation. One day, a former co-worker contacted me and said that at their new job, one of their servers ran out of disk space on their root partition due to log data filling it up. The previous administrator had set it up using LVM, but none of the Systems Administrators were familiar with it, so they did not know how to fix it. My former co-worker had made a copy of my LVM wiki page and used that to save the day. If that page can make that big of a difference at just one company, then maybe creating a blog about it can help even more users.
Logical Volume Manager (LVM) is an abstraction layer that sits on top of normal disk partitions. It provides additional features, such as dynamic resizing and snapshots, that standard disk partitioning does not offer. It also lets you combine multiple disks together to form a single continuous volume, which is similar to some types of RAID configurations. One of the most useful features is that it lets you resize your filesystem without requiring a that you restart your system or unmount the existing volume. This can save you some downtime if a virtual disk is in danger of filling up and you need to increase the space it uses. However, it can also be confusing since there are many steps required to set it up correctly. This document will outline the basic steps to create and resize LVM volumes.
LVM consists of a few layers that provide certain functions to the storage volumes. Since we will be adjusting these layers, it is important to understand what each of them does.
Physical Volume (PV) - This is the base storage volume of the LVM and usually sits on top of a physical disk or partition. You can think of it as being a disk that has been initialized with LVM. Each new disk or partition added will contain their own physical volumes and can be combined using volume groups
Volume Group (VG) - The Volume Groups layer sits on top of the physical volume layer and can be used to aggregate multiple physical volumes together. This allows you to add additional disks to a storage pool that the Volume Group can scale across. It also holds the logical volumes
Logical Volume (LV) - The Logical Volumes are similar to a traditional partition and is where you will create your filesystems. Unlike standard partitions, logical volumes have the advantage of being easily resized or moved within a volume group, which might span multiple disks. Each logical volume will utilize a slice of the same volume group for disk space, so you can readjust your partitions after installation if your original scheme did not provide adequate space.
Below is a list of common commands that you will need to use when interacting with LVM. I highly recommend reading their MAN pages, but here is a quick bulleted list for reference:
fdisk
- Manipulate MBR disk partition tablegdisk
- Interactive GUID partition table (GPT) manipulatorpvscan
- scan all disks for physical volumespvdisplay
- display attributes of a physical volumepvcreate
-initialize a disk or partition for use by LVMvgscan
- scan all disks for volume groups and rebuild cachesvgdisplay
- display attributes of volume groupsvgcreate
- create a volume grouplvscan
- scan (all disks) for Logical Volumeslvdisplay
- display attributes of a logical volumelvcreate
- create a logical volume in an existing volume grouplvextend
- extend the size of a logical volumelvreduce
- reduce the size of a logical volumeresize2fs
- ext2/ext3/ext4 file system resizerMost of the time, new LVM partitions will get created during the OS installation process. However, as you add new disks to a system, you might need to create fresh volumes, so we will walk through this process. It is also worth noting that you can create a physical volume on a bare disk instead of a partition. However, there are some pro’s and con’s with doing this, with the main one being that you lose some flexibility when resizing a physical volume. For this walk-thru, I am going to assume you are adding LVM to a partition with a disk named like this “/dev/sd
Using gdisk
- Handles both GPT and MBR partition tables. Required if the partition is greater than 2TB
gdisk /dev/sd<drive>
o create a new empty GUID partition table (GPT)
n add a new partition
Enter the Hex Code for Partition Type (8e00 Linux LVM)
Hex code or GUID (L to show codes, Enter = 8300): 8e00
Write changes to disk
w write table to disk and exit
Using fdisk
- Older partitioning tool that usually can only do MBR partitions. It is sometimes symlinked to gdisk, so it might function the same.
fdisk /dev/sd<drive>
o create a new empty partition table (MBR)
n add a new partition
Enter the Hex Code for Partition Type (8e00 Linux LVM)
Hex code or GUID (L to show codes, Enter = 8300): 8e00
Write changes to disk
w write table to disk and exit
Using parted - Provides more partitioning features than gdisk and fdisk, but is also more complicated to use without a GUI (aka gparted)
parted /dev/sd<drive> -- mklabel <msdos|gpt> # Pick msdos for MBR or gpt for GPT partition tables
parted /dev/sd<drive> -- mkpart primary <512MiB> - <8GiB> # This is the partition size, so adjust these numbers to fit your requirements
Create a physical volume
pvcreate /dev/sd<device><part#>
Create a Volume Group. Replace “
vgcreate <vg_name> <device><part#>
Create a Logical Volume. You can use a specific size or use the volume groups free disk space.
lvcreate -L<size> -n <label> <vg_name>
lvcreate -L25G -n home vg01
lvcreate -l 100%FREE -n <label> <vg_name>
lvcreate -l 100%FREE -n home vg01
Format the Volume with your filesystem of choice.
mkfs -t <type> /dev/<vg_name>/<label>
mkfs -t ext4 /dev/vg01/home
For some filesystems, you might need to install some extra tools. However, EXT4 should be available for most modern Linux OS’s.
This will resize an existing LVM partition with the assumption that there is available space. This is often done in a virtual environment, using shared storage, when a volume has run out of space. In this scenario, make sure you have expanded the disk size using a partition manager, such as fdisk
, gdisk
, etc. One thing to note before you try this, check the filesystem type you are using. If you are using XFS, know that you can NOT shrink the filesystem. You can only grow it.
Scan and display all physical volumes. This will also display if a PV has unused, “Free”, space.
pvscan
Scan and display all the Volume Groups. This will also display if the VG has unused, “Free”, space. This will automatically change when you resize the logical volume
vgscan
vgdisplay
Scan and display all Logical Volumes. Make note of the “LV Path”
lvscan
lvdisplay
Adjust the size of the Logical Volume using one of the following methods
Extend Logical Volume to a fixed amount
lvextend -L<size> <LV Path>
lvextend -L12G /dev/vg01/root
Append additional space to the logical volume
lvextend -L+<size> <LV Path>
lvextend -L+12G /dev/vg01/root
Grow the logical volume using all remaining space
lvextend -r -l +100%FREE <LV Path>
lvextend -r -l +100%FREE /dev/vg01/root
Note: |
---|
Adding ‘-r’ to any of the previous commands will resize the filesystem to match the volume size. You can then skip the next step if you used it. |
EXT4 and similar files systems
resize2fs <lvname> <size>
XFS Filesystem
xfs_growfs -D <size> <lvname>
xfs_growfs -d <lvname>
WARNING: |
---|
Take care when shrinking a volume. Not all filesystems support shrinking and will get corrupted if you try. If you need to make more room on a volume by reducing the volume size of another, then you can do so with the following: |
lvreduce -L-<size> <LV Path>
ex.
lvreduce -L-12G /dev/vg1/root
Note: |
---|
Unless you are using the -r flag in the previous commands, you should first resize the filesystem before reducing the logical volume. Otherwise, you will corrupt the filesystem. Check the next step for instructions. |
So there you have it. You have been shown step-by-step how to create and resize an LVM volume. Please let me know if you have any questions or find any errors. Also, if you have suggestions to make it better, then I would be happy if you left a comment for me.
Much of this was compiled using the following sources:
There are no comments.