- 1 Introduction
- 2 Installation
- 3 Configuration
- 4 Troubleshooting
- 5 Tips & Tricks
- 6 More Resources
LVM is a Logical Volume Manager for the Linux kernel. With LVM you can abstract your storage space and have "virtual partitions" which are easier to modify. The basic building blocks of LVM are:
- Physical volume (PV): Partition on hard disk (or even hard disk itself or loopback file) on which you can have volume groups. It has a special header and is divided into physical extents. Think of physical volumes as big building blocks which can be used to build your hard drive.
- Volume group (VG): Group of physical volumes that are used as storage volume (as one disk). They contain logical volumes. Think of volume groups as hard drives.
- Logical volume (LV): A "virtual/logical partition" that resides in a volume group and is composed of physical extents. Think of logical volumes as normal partitions.
- Physical extent (PE): A small part of a disk (usually 4MB) that can be assigned to a logical Volume. Think of physical extents as parts of disks that can be allocated to any partition.
With LVM you can more easily handle your partitions (logical volumes) than normal hard drive partitions. For example, you can:
- Use any number of disks as one big disk(VG)
- Have partitions(LV) stretched over several disks (they can be as big as all of your disk storage together)
- Resize/create/delete partitions(LV) and disks(VG) as you like (it doesn't depend on position of the logical volumes within volume groups as with normal partitions)
- Resize/create/delete partitions(LV) and disks(VG) online (filesystems on them still need to be resized, but some support online resizing)
- Name your disks(VG) and partitions(LV) as you like
- Create small partitions(LV) and resize them "dynamically" as they get more filled (growing must be still done by hand, but you can do it online with some filesystems)
Physical disks Disk1 (/dev/sda): _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |Partition1 50GB (Physical volume) |Partition2 80GB (Physical volume) | |/dev/sda1 |/dev/sda2 | |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | Disk2 (/dev/sdb): _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |Partition1 120GB (Physical volume) | |/dev/sdb1 | | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _|
LVM logical volumes Volume Group1 (/dev/MyStorage/ = /dev/sda1 + /dev/sda2 + /dev/sdb1): _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |Logical volume1 15GB |Logical volume2 35GB |Logical volume3 200GB | |/dev/MyStorage/rootvol|/dev/MyStorage/homevol |/dev/MyStorage/mediavol | |_ _ _ _ _ _ _ _ _ _ _ |_ _ _ _ _ _ _ _ _ _ _ _ _ |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
To sum it all up: With LVM you can use all your storage space as one big disk (volume group) and have more flexibility over your partitions (logical volumes).
Before doing anything we need to load the appropriate module:
# modprobe dm-mod
If you already have Arch Linux installed and you just want to add/try a partition with LVM, jump to partition disks.
Installing Arch Linux on LVM
Prior to running Arch Linux install scripts (/arch/setup) to install Arch Linux, you need to partition your disk with Template:Codeline (or any other tool of your liking). Because grub legacy (grub with version less than 1.0) can't boot from LVM logical volumes you can't have Template:Filename in LVM, so you need to create a boot partition. 100MB should be enough. The other solution would be to use lilo or grub with version 1.95 or newer.
Next you need to create a partition for LVM. Its filesystem type should be 'Linux LVM', so use a partition id 0x8e (filesystem type: 8e). You need to create only one LVM partition on each disk you want to use with LVM. Your logical volumes will reside inside these partitions so size them accordingly. If you will use only LVM and no other external partitions, use all the free space on each disk.
Create Physical volumes
Now you need to initialize these partitions so they can be used by LVM. Use Template:Codeline to find out which partitions have filesystem type 'Linux LVM' and create a physical volume on them:
# pvcreate /dev/sda2
Substitute Template:Filename with all your partitions to create physical volumes on all of them. This command creates a header on each partition so it can be used for LVM. You can track created physical volumes with:
Create Volume group(s)
Next step is to create a volume group on this physical volume. First you need to create a volume group on one of the new partitions and then add to it all other physical volumes you want to have in it:
# vgcreate VolGroup00 /dev/sda2 # vgextend VolGroup00 /dev/sdb1
Also you can use any other name you like instead of VolGroup00 for a volume group when creating it. You can track how your volume group grows with:
Create Logical Volumes
Now we need to create logical volumes on this volume group. You create a logical volume with the next command by giving the name of a new logical volume, its size, and the volume group it will live on:
# lvcreate -L 10G VolGroup00 -n lvolhome
This will create a logical volume that you can access later with Template:Filename or Template:Filename. Same as with the volume groups, you can use any name you want for your logical volume when creating it.
To create swap on a logical volume, an additional argument is needed:
# lvcreate -C y -L 10G VolGroup00 -n lvolswap
The Template:Codeline is used to create a contiguous partition, which means that your swap space doesn't get partitioned over one or more disks nor over non-contiguous physical extents.
If you want to fill all the free space left on a volume group, use the next command:
# lvcreate -l +100%FREE VolGroup00 -n lvolmedia
You can track created logical volumes with:
Create filesystem and mount logical volumes
Your logical volumes should now be located in Template:Filename and Template:Filename. If you can't find them, use the next commands to bring up the module for creating device nodes and to make volume groups available:
# modprobe dm-mod # vgchange -ay
Now you can create filesystems on logical volumes and mount them as normal partitions (if you are installing Arch linux, skip this step. Use the arch installer to pick the LVM partitions that you have created):
# mkfs.ext3 /dev/mapper/VolGroup00-lvolhome # mount /dev/mapper/VolGroup00-lvolhome /home
If you are installing Arch linux, start /arch/setup, go to Prepare Hard Drive directly to step 3 Set Filesystem Mountpoints and read the Important section below before proceeding with installation!
There are just a few things you need to be careful with while using/installing Arch Linux with LVM (in brackets are the corresponding menus during installation):
- When choosing mountpoints, just select your newly created logical volumes (use: Template:Filename).
Do NOT select the actual partitions on which logical volumes were created (don't use: Template:Filename). (Set Filesystem Mountpoints)
- Make sure you change USELVM="no" to USELVM="yes" in Template:Filename (Configure System)
- Make sure that lvm2 is in the HOOKS section of Template:Filename just before the filesystems so that your kernel will find LVM volumes at boot time. Also, if you wish to use LVM snapshots, add dm-snapshot to the MODULES variable. (Configure System)
- Make sure Template:Filename uses the right volumes for root. It should look something like this: (Install Bootloader)
... # (0) Arch Linux title Arch Linux root (hd0,0) kernel /vmlinuz26 root=/dev/mapper/VolGroup00-lvolroot resume=/dev/mapper/VolGroup00-lvolswap ro initrd /kernel26.img ...
If you are using LILO check Template:Filename:
image=/boot/vmlinuz26 label=arch append="root=/dev/mapper/VolGroup00-lvolroot resume=/dev/mapper/VolGroup00-lvolswap ro" initrd=/boot/kernel26.img
Grow logical volume
To grow a logical volume you first need to grow the logical volume and then the filesystem to use the newly created free space. Let's say we have a logical volume of 15GB with ext3 on it and we want to grow it to 20G. We need to do the following steps:
# lvextend -L 20G VolGroup00/lvolhome (or lvresize -L +5G VolGroup00/lvolhome) # resize2fs /dev/VolGroup00/lvolhome
If you want to fill all the free space on a volume group, use the next command:
# lvextend -l +100%FREE VolGroup00/lvolhome
Shrink logical volume
Because your filesystem is probably as big as the logical volume it resides on, you need to shrink the filesystem first and then shrink the logical volume. Depending on your filesystem, you may need to unmount it first. Let's say we have a logical volume of 15GB with ext3 on it and we want to shrink it to 10G. We need to do the following steps:
# resize2fs /dev/VolGroup00/lvolhome 9G # lvreduce -L 10G VolGroup00/lvolhome (or lvresize -L -5G VolGroup00/lvolhome) # resize2fs /dev/VolGroup00/lvolhome
Here we shrunk the filesystem more than needed so that when we shrunk the logical volume we didn't accidentally cut off the end of the filesystem. After that we normally grow the filesystem to fill all free space left on logical volume. You may use Template:Codeline instead of Template:Codeline.
Add partition to a volume group
To add a partition to your volume group you must first make its type 'Linux LVM' (for example with Template:Codeline). Then you need to create a physical volume on it and extend the volume group over it:
# pvcreate /dev/sdb1 # vgextend VolGroup00 /dev/sdb1
Now you have free space in your volume group that can be used by logical volumes in this group.
Remove partition from a volume group
All of the data on that partition needs to be moved to another partition. Fortunately, LVM makes this easy:
# pvmove /dev/mapper/myvg-mypv
If you want to have the data on a specific physical volume, specify that as the second argument to Template:Codeline.
Then the physical volume needs to be removed from the volume group:
# vgreduce myVg /dev/mapper/myvg-mypv
Or remove all empty physical volumes:
# vgreduce --all vg0
And lastly, if you want to use the partition for something else, and want to avoid LVM thinking that the partition is a physical volume:
# pvremove /dev/mapper/myvg-removedpv
LVM allows you to take a snapshot of your system in a much more efficient way than a traditional backup. It does this efficiently by using a COW (copy-on-write) policy. The initial snapshot you take simply contains hard-links to the inodes of your actual data. So long as your data remains unchanged, the snapshot merely contains its inode pointers and not the data itself. Whenever you modify a file or directory that the snapshot points to, LVM automatically clones the data, the old copy referenced by the snapshot, and the new copy referenced by your active system. Thus, you can snapshot a system with 35GB of data using just 2GB of free space so long as you modify less than 2GB (on both the original and snapshot).
You create snapshot logical volumes just like normal ones.
# lvcreate --size 100M --snapshot --name snap01 /dev/mapper/vg0-pv
With that volume, you may modify less than 100M of data, before the snapshot volume fills up.
It is important to have the dm-snapshot module listed in the MODULES variable of Template:Filename, otherwise the system will not boot. If you do this on an already installed system, make sure to rebuild the image with
# mkinitcpio -g /boot/kernel26.img
Todo: scripts to automate snapshots of root before updates, to rollback... updating Template:Filename to boot snapshots (separate article?)
snapshots are primarily used to provide a frozen copy of a filesystem to make backups; a backup taking two hours provides a more consistent image of the filesystem than directly backing up the partition.
LVM commands don't work
- Load proper module:
# modprobe dm-mod
- Try preceding commands with lvm like this:
# lvm pvdisplay
Set Filesystem Mountpoints page doesn't show logical volumes
If you are installing on a system where there is an existing volume group, you may find that even after doing "modprobe dm-mod" you don't see the list of logical volumes.
In this case, you may also need to do:
# vgchange -ay <volgroup>
in order to activate the volume group and make the logical volumes available.
Tips & Tricks
Other LVM articles on the Archwiki: