LVM

LVM stands for Logical Volume Manager. LVM allows you to create, resize, duplicate, and remove partitions at runtime, without reboot (and sometimes even without unmount).

It works by creating one or more physical partitions (PV - physical volume). These are combined into volume groups (VG). At least one PV is needed in a VG. Then the logical partitions (LV - logical volumes) are "allocated" from these volume groups.

There are two versions of LVM: The original LVM, and LVM2. The original LVM is deprecated these days, so when the word LVM is mentioned in this guide it refers to LVM2 unless explicitly said.

LVM also supports advanced features such as mirroring or creating snapshots of LVs. These snapshots can be copy-on-write, thus making them very fast to create, and only changes (either to original or the snapshot) will cause more disk space to be used. However this guide is meant as an introduction and won't describe these features.

Prerequisites
LVM2 uses the Device Mapper feature of the kernel. This means that feature have to be compiled in to the kernel.

You will also need the user space tools from :

Kernel config
This differs depending on if you use your own kernel, or if you use genkernel.

Manual kernel config
Note that you can also enable those unchecked options if you want. Especially Snapshot target and Mirror target may be useful, those are needed for more advanced features of LVM (such as snapshots and moving LVs between PVs). However, you should of course be careful with those marked EXPERIMENTAL. As always when changing kernel options, read the help for each option if you are unsure.

genkernel
genkernel --lvm all If you use luks don't forget --luks genkernel --lvm --luks all If you want to manually edit the kernel config use genkernel --menuconfig --lvm --luks all

Installing Gentoo on LVM2
If you plan to install on LVM2 please see Gentoo LVM2 installation at gentoo.org (for now at least).

Optional: Edit /etc/lvm/lvm.conf
You may want to edit the filter line in /etc/lvm/lvm.conf in order to speed up LVM scanning your partitions. Otherwise it may take a while as it will try to scan all the disk devices, some of which could be CD drives or similar for example. This would happen at every boot.

To only scan /dev/sdb and /dev/sdc for LVM partitions you would modify the line to something like this:

filter is a list of regular expressions, the prefix a means accept (include the device) and r means reject (exclude the device).

See the comments in the file and man lvm.conf for more details about the format.

Step 1: Create partitions for PVs
First you have to create a partition on your disk to use as a LVM physical volume (PV). This means using fdisk, cfdisk or some other tool like that. This is done as usual, please see the Gentoo Handbook for details on how you use fdisk. Note that the partition type should be for LVM PV partitions.

Step 2: Create PVs
This is easy, you use the tool pvcreate. Say for example you want to make /dev/sda3 and /dev/sdb2 PVs then you would run:

Step 3: Create VGs
Next you must create a volume group. This is done using vgcreate. If you want to add /dev/sda3 and /dev/sdb2 to a volume group named my_first_vg you would write:

Step 4: Create LVs
Next you need to create the actual "virtual" partitions, called logical volumes, that you want to use.

If you want create a LV called "my_lv" that is 5 GB on my_first_vg you would run this:

The name is completely up to you, but reasonable names could be things like:
 * usr - for /usr
 * var - for /var
 * home - for /home
 * root - for / (note not recommended, you would need to use initramfs to be able to boot)
 * portage - for /usr/portage

You figure out what names works for you.

Alternative configuration

LVM2 natively supports creating volumens with RAID1 (mirror) and RAID4/5/6 for reduncancy.

To setup a RAID1 mirror with two devices you would issue:

Step 5: Create file systems on your LVs
You create the file systems more or less like usual. The tricky bit could be finding out where the device file is, so here is the naming scheme: /dev/ /

So for the partition we created above we would use this command to create an xfs file system on it:

Step 6: Add to /etc/fstab
This is done as usual. Just remember the format of the device paths mentioned in last section.

Administration tasks
Maybe your LVs weren't large enough? Or you want to add another PV to a VG? Well easy to solve:

Administering PVs
How to create PVs was described above. There is really no other "trivial" administration task, thus this is mostly outside the scope of this document.

Adding a new PV to an existing VG
If you want to add the PV /dev/sdc1 to the VG my_first_vg then you would run this:

Removing a PV from an existing VG
If you have to remove on PV from the VG (e.g. you want to remove the hard drive), you must first add enough additional storage space to the VG. See the previous paragraph for this purpose. To move all data away from the PV /dev/sdc1, run this:

After the PV has been freed from all data, you can reduce the VG my_first_vg by that PV /dev/sdc1:

Now you can wipe the header in the PV so it won't be recogniced as PV any more:

Listing information about VGs
To list all VGs on a system:

To list information about all VGs:

To list information about the VG my_first_vg:

Listing LVs
To list all LVs on a system use:

To show information about all LVs use:

To show information about the LV my_first_lv in the VG my_first_vg:

Creating a new LV
This is done like you did when you created your first LVs. To create a 10GB LV named my_other_lv in the VG my_first_vg:

Don't forget to create a file system on it!

Removing a LV
First unmount the LV. Then run lvremove. For example if you want to remove my_other_lv that is found in the VG my_first_vg:

Growing a LV
This depends on the file system you selected, and if you want to shrink or grow it. From the LVM point of view it is all the same, but not for the file systems. For example not all file systems can be shrunk, nor can all be grown. Some can be grown without unmounting, but not all.

To extend the logical volume my_first_lv in the volume group my_first_vg to be 10 GB you would run:

To extend the logical volume my_first_lv in the volume group my_first_vg with another 1 GB (relative the current size) you would run:

The next step (resizing the actual file system) depends on what file system you use:
 * ext2/ext3
 * First unmount the file system. Then run:
 * Then remount the file system.
 * Then remount the file system.


 * ext4
 * You don't need to unmount if you don't want. Just run:


 * reiserfs (reiser3)
 * You don't need to unmount if you don't want. Here is what to run:
 * If the file system is mounted, add the switch -f to the above command.
 * If the file system is mounted, add the switch -f to the above command.


 * xfs
 * For XFS file systems you must have them mounted to be able to resize.
 * If /dev/my_first_vg/my_first_lv is mounted on /quux you would run:


 * btrfs
 * For btrfs file systems you must have them mounted to be able to resize.
 * If /dev/my_first_vg/my_first_lv is mounted on /quux and you want to grow by ?GB you would run:

For other file systems please see the relevant documentation.

Shrinking a LV

 * ext2/ext3
 * First calculate the new size in blocks you want, as well as how much that would be in MB or GB. Then unmount.
 * Then run:
 * where 12345678 is the size in blocks you calculated and -L-1G is the the same size in "normal" units.
 * where 12345678 is the size in blocks you calculated and -L-1G is the the same size in "normal" units.


 * reiserfs (reiser3)
 * First unmount, then if you want to shrink with 1 GB you would run:


 * btrfs
 * Keep the filesystem mounted. If e.g. dev/my_first_vg/my_first_lv is mounted on /mnt, then to shrink by 1GB run:

Note that you can't shrink xfs or jfs.

For other file systems please see the relevant documentation.

Re-read partitions
If fdisk tells you: WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot. Syncing disks. after you write the partition table to disk, and you don't feel like rebooting,

or if is already emerged

Both commands force the kernel to reread the partition tables on your disks.

Related technologies
There is also, which is a similar technology. EVMS uses the same device-mapper kernel technology as LVM. It is worth mentioning that the device-mapper is also used by many other interesting technologies (other than dynamic partitions), such as partition encryption and Device-mapper RAID.

Other resources

 * Gentoo LVM2 installation at gentoo.org
 * LVM HOWTO at tldp.org
 * LVM2 RAID Logical Volumes at redhat.com