LVM
From Gentoo Linux Wiki
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.
Contents |
[edit] 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 sys-fs/lvm2:
[edit] Kernel config
This differs depending on if you use your own kernel, or if you use genkernel.
[edit] Manual kernel config
| Linux Kernel Configuration: Kernel Config for LVM2 |
Device Drivers --->
Multiple devices driver support (RAID and LVM) --->
<*> Device mapper support
< > Crypt target support
< > Snapshot target
< > Mirror target
< > Zero target
< > Multipath target
< > I/O delaying target (EXPERIMENTAL)
[ ] DM uevents (EXPERIMENTAL)
< > Bad Block Relocation Device Target (EXPERIMENTAL)
|
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.
[edit] 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
[edit] Installing Gentoo on LVM2
If you plan to install on LVM2 please see Gentoo LVM2 installation at gentoo.org (for now at least).
[edit] Getting started with LVM2
[edit] Init script
| Fix me: The init script is only used for baselayout-2, which, as of the time this was written, wasn't stable yet. The author runs a stable Gentoo system and thus never used this. |
[edit] 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 = [ "a|/dev/sd[bc]|", "r/.*/" ] ...
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.
[edit] 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.
[edit] 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:
[edit] 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:
[edit] 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.
[edit] 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/<name of volume group>/<name of logical volume>
So for the partition we created above we would use this command to create an xfs file system on it:
[edit] Step 6: Add to /etc/fstab
This is done as usual. Just remember the format of the device paths mentioned in last section.
[edit] Administration tasks
Maybe your LVs weren't large enough? Or you want to add another PV to a VG? Well easy to solve:
[edit] 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.
[edit] Administrating VGs
[edit] 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:
[edit] 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:
[edit] 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:
[edit] Administrating LVs
[edit] 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:
[edit] 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!
[edit] 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:
[edit] 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:
- resize2fs /dev/my_first_vg/my_first_lv
- Then remount the file system.
- reiserfs (reiser3)
- You don't need to unmount if you don't want. Here is what to run:
- resize_reiserfs /dev/my_first_vg/my_first_lv
- 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:
- xfs_growfs /quux
- 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:
- btrfsctl -r +?g /quux
For other file systems please see the relevant documentation.
[edit] 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:
- resize2fs /dev/my_first_vg/my_first_lv 12345678
lvreduce -L-1G /dev/my_first_vg/my_first_lv - 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:
- resize_reiserfs -s-1G /dev/my_first_vg/my_first_lv
lvreduce -L-1G /dev/my_first_vg/my_first_lv - 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:
- btrfsctl -r -1g /mnt
lvreduce -L-1G dev/my_first_vg/my_first_lv
Note that you can't shrink xfs or jfs.
For other file systems please see the relevant documentation.
[edit] Troubleshooting
[edit] 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 sys-apps/parted is already emerged
Both commands force the kernel to reread the partition tables on your disks.
[edit] Related technologies
There is also sys-fs/evms, 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 (sys-fs/cryptsetup) and Device-mapper RAID (sys-fs/dmraid).