The disk partitioning scheme you just saw is useful for dividing the data into an organizational structure that meets and makes some sense. However, once the partition is created, making changes is complicated without involving a possible loss of data.
At best, it is possible to move the data to the beginning of the disk and repartition it “hot”. Worst case scenario, you need a backup, recreate the partitions, and restore the backup to the new layout.
To avoid all this hassle and possible data loss, the staff decided to create a logical volume manager, called LVM – Logical Volume Manager.
LVM on Linux is a Disk Manager integrated with the Linux Kernel. It allows you to:
In this way, LVM was created to expand the file system that is traditionally viewed as a set of physical disks and partitions. Its purpose is to allow great flexibility for the administrator in managing the disks.
Imagine that the user has the following partition scheme without LVM:
Mount Point | Partition | Size |
/boot | /dev/sda1 | 500 Megabytes |
Swap | /dev/sda2 | 1 Gigabyte |
/ | /dev/sda3 | 6 Gigabytes |
/home | /dev/sda4 4 | Gigabytes |
In this example, if the user wanted to increase the root partition, he would have to reformat his disk, or even move part of the data to another disk and mount the new partition as a root directory.
Whereas, if the user uses LVM, he could simply decrease the size of /home and increase the root, or even add another disk and increase the root, without having to back up the data, format the partition, and copy the data back. Look at the same example using volumes:
Mount Point | Partition | Size |
/boot | /dev/sda1 | 500 Megabytes |
Swap | /dev/vg00/swap | 1 Gigabyte |
/ | /dev/vg00/root | 6 Gigabytes |
/home | /dev/vg00/home | 4 Gigabytes |
In large systems with many disks, it is almost impossible to manage the disks without the use of LVM.
It’s important that you 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 the application of some patches.
Kernels versions 4.x and 5.x already support LVM version 2.
The only partition that cannot be used with the volume manager is /boot.
Before showing the LVM commands, you need to understand the terminology that LVM uses:
To work with LVM, you need to initialize the disks for LVM, then create at least one Volume group, create at least one logical volume in the group you just created, format the volume with the desired file system, and then mount it.
Briefly, it is necessary to take the following steps:
If you don’t want to use the entire disk as an LVM, you can use fdisk to create one or more LVM-like partitions. To create an LVM partition with fdisk, you create the partition normally and change the partition type to 8e.
For example, let’s imagine that the system was installed on the /dev/sda disk.
And for LVM, two more disks will be used: /dev/sdb and /dev/sdc that are not partitioned.
Before adding a disk or partition as a physical LVM volume, you must initialize it with the pvcreate command.
To initialize physical volumes from 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
After initializing the disks, you need to create a volume group with the disks with the vgcreate command:
# vgcreate my volume /dev/sdb /dev/sdc Volume group “myvolume” successfully created
After creating the volume group, it is necessary to activate it with the vgchange command:
# vgchange -a and myvolume 0 logical volume (s) in volume group “myvolume” now active
After the system is rebooted it is necessary to activate the volume group again. Therefore, it is necessary to include this command in the system load scripts.
The lvcreate command creates logical volumes. In the example, a 1GB logical volume called logic1 will be created on the volume myvolume:
# lvcreate -L 1000 -n logic1 my volume Logical volume 'logic1' created
Since the /dev/sdb and /dev/sdc disks each have 2GB in our example, it is possible to create up to 4 volumes of 1GB each, or a single logical volume of 4GB, as in the example below:
# lvcreate -L 4000 -n logic1 my volume Logical volume 'logic1' created
The lvchange command activates/deactivates the logical volume for use:
To ACTIVATE:
# lvchange -a and /dev/myvolume/logic1
To DISABLE:
# lvchange -a n /dev/myvolume/logic1
Any file system can be used to format the logical volume:
# mkfs.ext4 /dev/myvolume/logic1 mke2fs 1.41.14 (22-Dec-2010) Filesystem label= OS type: Linux (...)
Once you have formatted the logical volume, you need to mount it:
# mount /dev/myvolume/logic1 /mnt
After these steps, the logical volume will be ready for use.
You can also use LVM to increase or decrease the size of a volume.
First you need to create the physical volume:
# pvcreate /dev/sdd
Assign it to the group:
# vgextend my /dev/sdd group
Disassemble the logical volume:
# umount /dev/myvolume/logic1
Increase the logical volume group:
# lvextend -L +13090M /dev/myvolume/logic1
Search for error and repair:
# e2fsck -f /dev/myvolume/logic1
Finally, we resized:
# resize2fs /dev/myvolume/logic1
Now just put it back together:
# mount /dev/myvolume/logic1 /mnt
Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.
Share
The sudo command on Linux executes a given command as if it were another user.…
The ss command on Linux is extremely useful for investigating sockets, providing various information about…
Free Linux command shows the amount of total memory in use and available, as well…
The shell has structures for testing conditions and executing certain program sequences several times (loop),…
The /etc/fstab file stores the configuration of which devices should be mounted and what is…
The Netcat Command in Linux or nc is a utility used to do “almost anything”…
This website uses cookies.