Logical Volume Manager - LVM on Linux
LVM is useful for managing disks in an intelligent way through volumes. Discover this technology used in many distributions
LVM is a Linux Kernel disk manager. It allows disks to be changed without service interruption (hotswap), change the size of the volumes, create backup of volume images, create a single volume from several # mount /dev/myvolume/logicco1 /datosdisks (similar to RAID 0), or create volumes mirrored on more than one disk (similar to RAID 1). LVM makes it possible to expand the file system which is traditionally viewed as a collection of physical disks and partitions. Its purpose is to allow great flexibility for the administrator in disk management. Imagine that the user has the following scheme of partitions without LVM: > /boot /dev/hda1 50 Megabytes swap /dev/hda2 256 Megabytes /dev/hda3 2 Gigabytes /home & nbsp; /dev/hda4 6 Gigabytes
In this example, if the user wishes to increase the root partition, it would have to reformat your 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: > /boot /dev/hda1 50Mb swap /dev/vg00/swap 256Mb /& nbsp; /dev/vg00/root 2 Gigabytes /home /dev/vg00/home 6 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. 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 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;
- Logic Volume: It is the equivalent to a partition on a non-LVM system.
Step by step to work with LVM
To work with LVM it is necessary to do the following steps: 1. If you are going to use only one disk partition, you must create it with fdisk and change its type; 2. Initialize physical volumes (partitions or disks) with the pvcreate command; Create 3. a volume group with the vgcreate command; 4. Activate a volume group with the vgchange command; 5. Create a logical volume with the lvcreate command; 6. Format the logical volume with the desired file system.
Prepare partitions for LVM with fdisk
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. To illustrate, let’s imagine that the system was installed on the SDA disk. And two disks will be used for LVM: sdb and sdc that are not partitioned. Before adding a disk or partition as a LVM’s physical volume must be initialized with the pvcreate command. ### Initialize physical volumes with pvcreate
To initialize physical disk volumes, use the command 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” created successfully
Remember to first change the type of the partitions with fdisk to 8e. ### Creating a volume group with vgcreate
Once initialized, it is necessary to create a group volume with the disks with the vgcreate command: > # vgcreate myvolume /dev/sdb /dev/sdc Volume group “myvolume” 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 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. ### Remove a volume group with vgremove
To remove a volume group there cannot be logical volumes within the group. The command to remove volumes is vgremove and the volume must be turned off: > # vgchange -a in my volume # vg remove my volume
Add new “physical volumes” to volume group with vgextend
New disks can be added to the volume group with the command: ### # vgextend my volume /dev/sdd
Remove “physical volumes from volume group” with vgreduce
Disks can be removed from the volume group with the vgreduce command. You need to make sure that there aren’t any logical volume on the physical volume, which can be migrated to another disk. > # vg reduce my volume /dev/sdd
Create logical volumes with lvcreate
The lvcreate command creates logical volumes. For example a logical volume will be created of 1 GB called logic1 on the volume myvolume: > # lvcreate -L 1000 -n logic1 myvolume Logical volume “logic1” created
Since SDB and SDC each have 2GB, 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
Activate logical volume with lvchange
The lvchange command activates/deactivates the volume logical for use: > ACTIVATE: # lvchange -a and /dev/myvolume/logic1 DISABLE: # lvchange -a n /dev/myvolume/logic1
Format the logical volume with mkfs
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 (…) 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 is necessary assemble it: > # mount /dev/myvolume/logic1 /data
After these steps, the logical volume will be ready for use. ### Increase volume size
First you need to create the physical volume: > # pvcreate /dev/hda8
Assign it to the group: > # vgextend my /dev/hda8 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 assemble: > # mount /dev/myvolume/logic1 /data
Decreasing volume size Unmount the file system: > # umount /dev/myvolume/logic1
Check the logical volume for errors: > # e2fsck -f /dev/myvolume/logic1
Shrink the partition: > # resize2fs /dev/myvolume/logic1 524288
Lower the logical volume: > # lvreduce -L -50M //dev/myvolume/logic1
Remount the file system: > # mount /dev/myvolume/logic1 /data