Logical Volume Manager (LVM) helps you manage your storage better by introducing a layer of abstraction over your storage hardware. When you're freed from hardware limitations you can use more than one storage device for a volume or partition. You can also resize volumes dynamically and create snapshots for restoration points.
To start working with LVM the first thing you need to do is create an LVM-type partition using fdisk or a similar disk setup utility. With fdisk, to work on a disk under /dev/sdb, run the command fdisk /dev/sdb.
In the interactive fdisk menu press n for a new partition. Then choose the partition to be a primary partition under the number one. Leave the default values for the starting and ending cylinders so you can make use of the full capacity of the disk. Finally, press t (for type) and choose the code 8e, which indicates the Linux LVM partition type. Write the changes with w and exit the program.
To make sure the new LVM partition is ready, run the command fdisk -l /dev/sdb. Your new partition should appear as /dev/sdb1:
fdisk -l /dev/sdb
Device Boot Start End Blocks Id System
/dev/sdb1 1 1044 8385898+ 8e Linux LVM
The number one in /dev/sdb1 comes from the setup in fdisk, and means primary partition number one.
Once you have the LVM partition you need to initialize the physical volume (PV) using the command pvcreate. To continue our example with /dev/sdb1, run pvcreate /dev/sdb1.
LVM physical volumes are the basis of volume groups (VG), which can comprise one or more PVs. This allows some useful functionality, such as disk striping for better read/write performance.
Once you've initialized the PV on /dev/sdb1 you can either extend a VG or create a new one. To extend a VG, use the command vgextend. In our case, the command vgextend VolGroup /dev/sdb1 extends the VG called VolGroup, which is the default one in CentOS 6.
vgextend VolGroup /dev/sdb1
If there is no VG yet on the server, you can create a new VG with the command vgcreate. For example, to create a new VG called TestsVolGroup, run the command vgcreate TestsVolGroup /dev/sdb1.
vgcreate TestsVolGroup /dev/sdb1
You should stick with only one VG and not create multiple ones on the same server. One VG provides enough flexibility and functionality to create, increase, and decrease numerous volumes on a server.
Before and after making changes on VGs use the command vgs to show summary information about the available VGs. In our example, vgs shows:
VG #PV #LV #SN Attr VSize VFree
VolGroup 2 2 0 wz--n- 15.50g 8.00g
In this output, the VG VolGroup has two physical volumes and two logical volumes. (We'll talk about logical volumes in a moment.) The attribute column shows the VolGroup is writable (w) and resizable (z). The total size is 15.5GB, with 8GB free. For additional details and options, check the manual for vgs.
As a rule of thumb you should plan to leave some free unallocated space in a VG. Having around 20% free capacity allows you to prevent disasters with logical volumes (partitions) running out of space.
VGs provide the necessary storage resources to create new or to extend existing logical volumes (LV), on top of which filesystems can be created. The two most common operations on LVs are creating a new LV and extending an existing one.
By default, CentOS 6 comes with two LVs: lv_root for the root (/) filesystem directory and lv_swap for the Linux swap partition. You may want to create a new LV to provide a dedicated partition for a specific purpose. For example, it's considered a good practice to place logs (/var/log/) in a separate filesystem partition, in order to prevent runaway logs from exhausting the disk space on the root filesystem and thus causing downtime.
To create a new LV, use the command lvcreate. As an example, let's create a new LV from the previously mentioned VG VolGroup. The LV will be 3GB in size and will be used for a partition for the Apache logs. These logs are stored by default in /var/log/httpd/, so that's where we'll mount the new partition.
To create the new example LV use the command lvcreate --name lv_httpd_logs --size 3G VolGroup. After this, run lvs; you should see a new LV called lv_httpd_logs.
lvcreate --name lv_httpd_logs --size 3G VolGroup
Next, format the lv_httpd_logs LV. To do so you need the LV's path. To find this path run the command lvdisplay, which is similar to lvs but provides more verbose output. In the output look for the value of LV Path; it should be something like /dev/VolGroup/lv_httpd_logs.
Once you know the path, format the LV in your preferred filesystem (ext2, ext3, ext4, reiserfs). For example, to format it in ext4, use the command mkfs.ext4 /dev/VolGroup/lv_httpd_logs.
After it's formatted you can mount the newly created filesystem. The best way to do that is to add a new row in /etc/fstab that looks like /dev/mapper/VolGroup-lv_httpd_logs /var/log/httpd/ ext4 defaults 1 1. This ensures that the new partition is mounted by default at boot time. To mount it without waiting for the next boot, run the command mount --all, which mounts all filesystems described in /etc/fstab.
/dev/mapper/VolGroup-lv_httpd_logs /var/log/httpd/ ext4 defaults 1 1
When you want to add more disk space to an LV and its partition, you can extend it with the command lvextend. Find the LV's path with lvdisplay, then issue a command like lvextend -L+1G /dev/VolGroup/lv_root to extend the LV called lv_root by 1GB.
lvextend -L+1G /dev/VolGroup/lv_root
After you extend an LV you must resize its filesystem accordingly so you can see the increased space. To do this use the command resize2fs followed by the LV's path; for instance, resize2fs /dev/VolGroup/lv_root. Usually this process is reliable and trouble-free, but it's not a bad idea to try minimizing read/write activity on the partition when you run this command.
Other possible LV operations include lvreduce for reducing an LV's size. This operation is not as safe and troublefree as other LV commands, so use and test such operations with care to avoid possible fatal data loss.
One of the most notable LVM features is its ability to create snapshots, which give you a kind of fast, non-interruptive, easy image backup. LVM snapshots let you compare and track changes in the data on the LV between the time the snapshot is taken and the current state. If you need to, you can later restore the partition to its condition at the time of the snapshot. Snapshots work in a manner similar to snapshots in virtual environments like VMware and VirtualBox.
To create an LVM snapshot, use the command lvcreate with the option --snapshot. You have to give the snapshot a name with the option --name. The last argument is the target path of the LV to be snapshotted.
Creating a snapshot creates a new LV, so an important consideration is the size of the new LV. The LV size is determined by the --size option. This size of the snapshot LV determines how many megabytes of changes can be stored since the original state of the LV you're backing up. For example, if you expect to make data changes equal to one GB, you will need a snapshot LV with at least one GB of space. A good generic size for a snapshot LV is about 20% of the size of the original volume.
A command to create a snapshot LV might like: lvcreate --size 1G --snapshot --name logs_snapshot /dev/VolGroup/lv_httpd_logs. This command creates a 1GB snapshot called logs_snapshot for the lv_httpd_logs volume. You can check the snapshot with the command lvdisplay; the output should contain one important row, LV snapshot status active destination for lv_httpd_logs, confirming it's the snapshot for lv_httpd_logs.
lvcreate --size 1G --snapshot --name logs_snapshot /dev/VolGroup/lv_httpd_logs
LV snapshot status active destination for lv_httpd_logs
Once you've created a snapshot for an LV you can revert to its initial state at any time. To do so, first make sure the original LV's filesystem is not mounted. In our example, we would run umount /dev/VolGroup/lv_httpd_logs.
Next, use lvconvert's --merge option to start the process of reverting back. The full command would look like: lvconvert --merge /dev/VolGroup/logs_snapshot. The output should be:
lvconvert --merge /dev/VolGroup/logs_snapshot
Merging of volume logs_snapshot started.
lv_httpd_logs: Merged: 0.0%
Merge of snapshot into logical volume lv_httpd_logs has finished.
Logical volume "logs_snapshot" successfully removed
Finally, mount the logs partition back with the command mount -a. The logs partition should be back to the state it was in when the snapshot was taken.
While this article describes how to work with LVM at the command line, you can if you prefer use a graphical tool too. CentOS provides one called system-config-lvm.
Finally, not that the examples in this article have been tested with CentOS 6 and LVM 2.02.95(2)-RHEL6 (2012-05-16). (To see your LVM version, run the command lvm version.) With ongoing LVM development and changes to other system components, your mileage may vary, especially with other Linux distributions and LVM versions.
Allowed tags: <a> link, <b> bold, <i> italics