LVM on Linux: Create, Resize, and Manage Logical Volumes
LVM is useful for managing disks intelligently through volumes. Learn about this technology used in many distributions
LVM is a disk manager for the Linux Kernel. It allows disks to be swapped without service interruption (hotswap), resize volumes, create backup images of volumes, create a single volume from several # mount /dev/meuvolume/logico1 /dadosdiscos (similar to RAID 0) or create mirrored volumes on more than one disk (similar to RAID 1).
LVM makes it possible to expand the filesystem that traditionally is seen as a set of physical disks and partitions. Its goal is to allow great flexibility for the administrator in managing the disks.
Imagine that the user has the following partition scheme without LVM:
/boot /dev/hda1 50 Megabytes
swap /dev/hda2 256 Megabytes
/ /dev/hda3 2 Gigabytes
/home /dev/hda4 6 Gigabytes
In this example, if the user wants to increase the root partition, they would have to reformat their disk, or even move part of the data to another disk and mount the new partition as a directory of root.
Whereas, if the user uses LVM, they could simply shrink the size of /home and increase root, or even add another disk and increase root, without needing to backup the data, format the partition and copy the data back.
Observe the same example using volumes:
/boot /dev/hda1 50Mb
swap /dev/vg00/swap 256Mb
/ /dev/vg00/root 2 Gigabytes
/home /dev/vg00/home 6 Gigabytes
In large systems with many disks, it is practically unfeasible to manage the disks without using LVM.
It is important for you to know that there are two versions of LVM: 1 and 2. Version 2 is supported by Kernel 2.6 and 3.x, and by Kernel 2.4 with application of some patches.
The only Linux partition that cannot be used with the volume manager is /boot.
LVM Terminology
- Physical Volume: It is a disk or some hardware that behaves like a disk (such as a storage that uses RAID);
- Volume Group: It is an abstraction of LVM that brings together logical volumes and physical volumes in the same administrative unit;
- Logical Volume: It is the equivalent to a partition in a non-LVM system.
Step by step to work with LVM
To work with LVM you need to do the following steps:
- If you are going to use only one partition of a disk, you need to create it with fdisk and change its type;
- Initialize the physical volumes (partitions or disks) with the pvcreate command;
- Create a volume group with the vgcreate command;
- Activate a volume group with the vgchange command;
- Create a logical volume with the lvcreate command;
- Format the logical volume with the desired filesystem.
Prepare the partitions for LVM with fdisk
If you do not want to use the entire disk as LVM, you can use fdisk to create one or more LVM type partitions.
To create an LVM partition with fdisk, you create the partition normally and change its type to 8e.
For example, let's imagine that the system was installed on disk sda. And two disks will be used for LVM: sdb and sdc which are not partitioned.
Before adding a disk or partition as a physical volume of LVM you need to initialize it with the pvcreate command.
Initialize physical volumes with pvcreate
To initialize physical volumes of entire disks the command is pvcreate and the full path of the partition or disk:
# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created
# pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created
Remember to first change the partition types with fdisk to 8e.
Creating a volume group with vgcreate
After initializing, you need to create a volume group with the disks with the vgcreate command:
# vgcreate meuvolume /dev/sdb /dev/sdc
Volume group "meuvolume" successfully created
Activating a volume group with vgchange
After creating the volume group, it is necessary to activate it with the vgchange command:
# vgchange -a y meuvolume
0 logical volume(s) in volume group "meuvolume" now active
After rebooting the system it is necessary to activate the volume group again. Therefore, it is necessary to include this command in the system boot scripts.
Remove a volume group with vgremove
To remove a volume group there cannot be logical volumes inside the group.
The command to remove volumes is vgremove and the volume needs to be deactivated:
# vgchange -a n meuvolume
# vgremove meuvolume
Add new "physical volumes to the volume group with vgextend
New disks can be added to the volume group with the command:
# vgextend meuvolume /dev/sdd
Remove "physical volumes from the volume group" with vgreduce
Disks can be removed from the volume group with the vgreduce command.
You need to make sure there is no logical volume on the physical volume, which can be migrated to another disk.
# vgreduce meuvolume /dev/sdd
Creating logical volumes with lvcreate
The lvcreate command creates logical volumes. In the example, a 1GB logical volume called logico1 will be created on the meuvolume volume:
# lvcreate -L 1000 -n logico1 meuvolume
Logical volume "logico1" created
Since sdb and sdc each have 2GB, it's possible to create up to 4 volumes of 1GB each, or 1 single logical volume of 4GB, as in the example below:
# lvcreate -L 4000 -n logico1 meuvolume
Logical volume "logico1" created
Activating the logical volume with lvchange
The lvchange command activates / deactivates the logical volume for use:
ACTIVATE:
# lvchange -a y /dev/meuvolume/logico1
DEACTIVATE:
# lvchange -a n /dev/meuvolume/logico1
Formatting the logical volume with mkfs
Any file system can be used to format the logical volume:
# mkfs.ext4 /dev/meuvolume/logico1
mke2fs 1.41.14 (22-Dec-2010)
Filesystem label=
OS type: Linux
(…)
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
After formatting the logical volume, it needs to be mounted:
# mount /dev/meuvolume/logico1 /dados
After these steps the logical volume will be ready for use.
Increasing the volume size
First you need to create the physical volume:
# pvcreate /dev/hda8
Assign it to the group:
# vgextend meugrupo /dev/hda8
Unmount the logical volume:
# umount /dev/meuvolume/logico1
Extend the logical volume group:
# lvextend -L +13090M /dev/meuvolume/logico1
Check for errors and repair:
# e2fsck -f /dev/meuvolume/logico1
Finally, resize:
# resize2fs /dev/meuvolume/logico1
Now just mount:
# mount /dev/meuvolume/logico1 /dados
Decreasing the volume size
Unmount the file system:
# umount /dev/meuvolume/logico1
Check the logical volume for errors:
# e2fsck -f /dev/meuvolume/logico1
Reduce the partition:
# resize2fs /dev/meuvolume/logico1 524288
Reduce the logical volume:
# lvreduce -L -50M //dev/meuvolume/logico1
Remount the file system:
# mount /dev/meuvolume/logico1 /dados
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Did you like it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.