Difference between revisions of "LVM"

From ArchWiki
Jump to: navigation, search
(Additional resources: Format links)
(Configure system: add .service to systemctl commands)
(45 intermediate revisions by 24 users not shown)
Line 1: Line 1:
[[Category:Getting and installing Arch (English)]]
+
[[Category:Getting and installing Arch]]
[[Category:File systems (English)]]
+
[[Category:File systems]]
{{i18n|LVM}}
+
[[cs:LVM]]
 +
[[de:LVM]]
 +
[[es:LVM]]
 
[[fr:LVM]]
 
[[fr:LVM]]
 
+
[[it:LVM]]
 +
[[ru:LVM]]
 +
[[tr:LVM]]
 +
[[zh-CN:LVM]]
 +
{{Out of date|This article has not been updated to use the new Arch install scripts. References to /arch/setup and the installer are not applicable to recent installation media.}}
 
{{Article summary start}}
 
{{Article summary start}}
 
{{Article summary text|This article will provide an example of how to install and configure Arch Linux with Logical Volume Manager (LVM).}}
 
{{Article summary text|This article will provide an example of how to install and configure Arch Linux with Logical Volume Manager (LVM).}}
 
{{Article summary heading|Required software}}
 
{{Article summary heading|Required software}}
{{Article summary link|Software|}}
+
{{Article summary text|{{pkg|lvm2}}}}
 
{{Article summary heading|Related}}
 
{{Article summary heading|Related}}
 
{{Article summary wiki|Software RAID and LVM}}
 
{{Article summary wiki|Software RAID and LVM}}
Line 19: Line 25:
  
 
Note that LVM is not used for the boot partition, because of the bootloader problem.
 
Note that LVM is not used for the boot partition, because of the bootloader problem.
 +
{{Tip|/boot cannot reside in a LVM partition when using [[GRUB Legacy]], which does not support LVM. If you want /boot to reside in a LVM partition, use [[GRUB2]]. }}
  
 
+
The basic building blocks of LVM are:
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.
 
* '''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.
Line 62: Line 68:
  
 
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).
 
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).
 +
 +
==Advantages==
 +
 +
Here are some things you can do with LVM that you cannot (or cannot do easily) with just mdadm, MBR partitions, GPT partitions, parted/gparted and a file-level tool like rsync.
 +
# Online/live partition resizing
 +
# No need for an extended partition (not relevant for GPT)
 +
# Resize partitions regardless of their order on disk (no need to ensure surrounding available space)
 +
# Online/live migration of partitions being used by services without having to restart services
 +
 +
These can be very helpful in a server situation, desktop less so, but you must decide if the features are worth the abstraction.
  
 
==Installation==
 
==Installation==
Line 69: Line 85:
  
 
===Installing Arch Linux on LVM===
 
===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 {{Codeline|cfdisk}} (or any other tool of your liking). Because grub legacy (grub with version less than 1.0) cannot boot from LVM logical volumes you cannot have {{Filename|/boot}} 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.
+
Prior to running Arch Linux install scripts (/arch/setup) to install Arch Linux, you need to partition your disk with {{Ic|cfdisk}} (or any other tool of your liking). Because grub legacy (grub with version less than 1.0) cannot boot from LVM logical volumes you cannot have {{ic|/boot}} 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.
  
 
===Partition disks===
 
===Partition disks===
 
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.
 
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.
  
{{Warning|/boot cannot reside in an LVM partition because grub (version<1.95) does not support LVM.}}
+
{{Warning|/boot cannot reside in LVM when using [[GRUB Legacy]], which does not support LVM. If you want /boot to reside in a LVM partition, use [[GRUB2]]. }}
  
 
{{Tip|All LVM partitions on all disks can be configured to appear as one big disk.}}
 
{{Tip|All LVM partitions on all disks can be configured to appear as one big disk.}}
  
===Create Physical volumes===
+
===Create physical volumes===
Now you need to initialize these partitions so they can be used by LVM. Use {{Codeline|fdisk -l}} to find out which partitions have filesystem type 'Linux LVM' and create a physical volume on them:
+
Now you need to initialize these partitions so they can be used by LVM. Use {{Ic|fdisk -l}} to find out which partitions have filesystem type 'Linux LVM' and create a physical volume on them:
 
  # pvcreate /dev/sda2
 
  # pvcreate /dev/sda2
Substitute {{Filename|/dev/sda2}} 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.
+
Substitute {{ic|/dev/sda2}} 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:
 
You can track created physical volumes with:
 
  # pvdisplay
 
  # pvdisplay
  
===Create Volume group(s)===
+
{{Note|If using a SSD use {{ic|pvcreate --dataalignment 1m /dev/sda2}} (for erase block size < 1MiB), see e.g. [http://serverfault.com/questions/356534/ssd-erase-block-size-lvm-pv-on-raw-device-alignment here]}}
 +
 
 +
===Create volume group===
 
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:
 
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
 
  # vgcreate VolGroup00 /dev/sda2
Line 94: Line 112:
 
{{Note|You can create more than one volume group if you need to, but then you will not have all your storage presented as one disk.}}
 
{{Note|You can create more than one volume group if you need to, but then you will not have all your storage presented as one disk.}}
  
===Create Logical Volumes===
+
===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:
 
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
 
  # lvcreate -L 10G VolGroup00 -n lvolhome
This will create a logical volume that you can access later with {{Filename|/dev/mapper/Volgroup00-lvolhome}} or {{Filename|/dev/VolGroup00/lvolhome}}. Same as with the volume groups, you can use any name you want for your logical volume when creating it.
+
This will create a logical volume that you can access later with {{ic|/dev/mapper/Volgroup00-lvolhome}} or {{ic|/dev/VolGroup00/lvolhome}}. 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:
 
To create swap on a logical volume, an additional argument is needed:
 
  # lvcreate -C y -L 10G VolGroup00 -n lvolswap
 
  # lvcreate -C y -L 10G VolGroup00 -n lvolswap
The {{Codeline|-C y}} is used to create a contiguous partition, which means that your swap space does not get partitioned over one or more disks nor over non-contiguous physical extents.
+
The {{Ic|-C y}} is used to create a contiguous partition, which means that your swap space does not 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:
 
If you want to fill all the free space left on a volume group, use the next command:
Line 113: Line 131:
 
{{Tip|You can start out with relatively small logical volumes and expand them later if needed. For simplicity, leave some free space in the volume group so there is room for expansion.}}
 
{{Tip|You can start out with relatively small logical volumes and expand them later if needed. For simplicity, leave some free space in the volume group so there is room for expansion.}}
  
===Create filesystem and mount logical volumes===
+
===Create filesystems and mount logical volumes===
Your logical volumes should now be located in {{Filename|/dev/mapper/}} and {{Filename|/dev/YourVolumeGroupName}}. If you cannot find them, use the next commands to bring up the module for creating device nodes and to make volume groups available:
+
Your logical volumes should now be located in {{ic|/dev/mapper/}} and {{ic|/dev/YourVolumeGroupName}}. If you cannot find them, use the next commands to bring up the module for creating device nodes and to make volume groups available:
 
  # modprobe dm-mod
 
  # modprobe dm-mod
 
  # vgscan
 
  # vgscan
 
  # vgchange -ay
 
  # 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):
+
Now you can create filesystems on logical volumes and mount them as normal partitions (if you are installing Arch linux, refer to [[Beginners' Guide#Mount the partitions|mounting the partitions]] for additional details):
  # mkfs.ext3 /dev/mapper/VolGroup00-lvolhome
+
  # mkfs.ext4 /dev/mapper/VolGroup00-lvolhome
 
  # mount /dev/mapper/VolGroup00-lvolhome /home
 
  # 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 [[Lvm#Important|Important]] section below before proceeding with installation!'''''
+
<s>If you are installing Arch linux, start /arch/setup, go to ''Prepare Hard Drive'' directly to step 3 ''Set Filesystem Mountpoints'' and '''''read the [[Lvm#Important|Important]] section below before proceeding with installation!'''''</s>
  
=== Important ===
+
=== Set filesystem mountpoints ===
There are just a few things you need to be careful with while using/installing Arch Linux with LVM.
+
* When choosing mountpoints, just select your newly created logical volumes (use: {{ic|/dev/mapper/Volgroup00-lvolhome}}).<br>Do NOT select the actual partitions on which logical volumes were created (do not use: {{ic|/dev/sda2}}).
  
==== Set Filesystem Mountpoints ====
+
=== Configure system ===
* When choosing mountpoints, just select your newly created logical volumes (use: {{Filename|/dev/mapper/Volgroup00-lvolhome}}).<br>Do NOT select the actual partitions on which logical volumes were created (do not use: {{Filename|/dev/sda2}}).
+
  
==== Configure System ====
+
For late-boot activation (non-root-filesystem) of volume groups, enable the {{ic|lvm}} unit file,
*Make sure you change {{codeline|<nowiki>USELVM="no"</nowiki>}} to {{codeline|<nowiki>USELVM="yes"</nowiki>}} in {{Filename|/etc/rc.conf}}. The {{filename|rc.sysinit}} script that parses the {{codeline|USELVM}} variable entry will accept either {{codeline|yes}} or {{codeline|YES}}, however it will not accept mixed case.
+
 
* Make sure that ''lvm2'' is in the HOOKS section of {{Filename|/etc/mkinitcpio.conf}} just before the ''filesystems'' so that your kernel will find LVM volumes at boot time.
+
{{bc|# systemctl enable lvm.service}}
* If your root filesystem ( "/" ) is put onto a logical volume, make sure regenerate kernel image based on above modified {{Filename|/etc/mkinitcpio.conf}} by using below command so that bootloader can find your root during booting
+
 
      cd /boot
+
If you are using LVM on an encrypted device, use this instead:
      mkinitcpio -p linux
+
 
* Make sure {{Filename|/boot/grub/menu.lst}} uses the right volumes for root. It should look something like this: (''Install Bootloader'')
+
{{bc|# systemctl enable lvm-on-crypt.service}}
      ...
+
 
      (0) Arch Linux
+
Or, if you still use sysvinit, modify {{ic|USELVM}} appropriately:
      title  Arch Linux
+
 
      root  (hd0,0)
+
{{hc|/etc/rc.conf:|<nowiki>USELVM="yes"</nowiki>}}
      kernel /vmlinuz26 '''root=/dev/mapper/VolGroup00-lvolroot''' resume=/dev/mapper/VolGroup00-lvolswap ro
+
 
      initrd /kernel26.img
+
For early-boot activation of volume groups (namely for root filesystem containers), you'll need to make sure the {{ic|lvm2}} and {{ic|udev}} [[mkinitcpio]] hooks are enabled.
      ...
+
 
* If you are using LILO check {{Filename|/etc/lilo.conf}}:
+
{{hc|/etc/mkinitcpio.conf:|<nowiki>HOOKS="base udev ... lvm2 filesystems"</nowiki>}}
      image=/boot/vmlinuz26
+
 
        label=arch
+
Also make sure the {{ic|dm_mod}} module is enabled.
        append="'''root=/dev/mapper/VolGroup00-lvolroot''' resume=/dev/mapper/VolGroup00-lvolswap ro"
+
 
        initrd=/boot/kernel26.img
+
{{hc|/etc/mkinitcpio.conf:|<nowiki>MODULES="dm_mod ..."</nowiki>}}
 +
 
 +
You will need to [[Mkinitcpio#Image_creation_and_activation|rebuild]] the initramfs to commit any changes you made.
  
 
== Configuration ==
 
== Configuration ==
Line 154: Line 173:
 
  # lvextend -L 20G VolGroup00/lvolhome (or lvresize -L +5G VolGroup00/lvolhome)
 
  # lvextend -L 20G VolGroup00/lvolhome (or lvresize -L +5G VolGroup00/lvolhome)
 
  # resize2fs /dev/VolGroup00/lvolhome
 
  # resize2fs /dev/VolGroup00/lvolhome
You may use {{Codeline|lvresize}} instead of {{Codeline|lvextend}}.
+
You may use {{Ic|lvresize}} instead of {{Ic|lvextend}}.
  
 
If you want to fill all the free space on a volume group, use the next command:
 
If you want to fill all the free space on a volume group, use the next command:
Line 168: Line 187:
 
  # lvreduce -L 10G VolGroup00/lvolhome (or lvresize -L -5G VolGroup00/lvolhome)
 
  # lvreduce -L 10G VolGroup00/lvolhome (or lvresize -L -5G VolGroup00/lvolhome)
 
  # resize2fs /dev/VolGroup00/lvolhome
 
  # resize2fs /dev/VolGroup00/lvolhome
Here we shrunk the filesystem more than needed so that when we shrunk the logical volume we did not 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 {{Codeline|lvresize}} instead of {{Codeline|lvreduce}}.
+
Here we shrunk the filesystem more than needed so that when we shrunk the logical volume we did not 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 {{Ic|lvresize}} instead of {{Ic|lvreduce}}.
  
{{Warning|Do not reduce the filesystem size to less than the amount of space occupied by data or you risk data loss.}}
+
{{Warning|
 
+
*Do not reduce the filesystem size to less than the amount of space occupied by data or you risk data loss.
{{Warning|Not all filesystems support shrinking without loss of data and/or shrinking online.}}
+
*Not all filesystems support shrinking without loss of data and/or shrinking online.}}
  
 
{{Note|It is better to reduce the filesystem to a smaller size than the logical volume, so that after resizing the logical volume, we do not accidentally cut off some data from the end of the filesystem.}}
 
{{Note|It is better to reduce the filesystem to a smaller size than the logical volume, so that after resizing the logical volume, we do not accidentally cut off some data from the end of the filesystem.}}
Line 181: Line 200:
 
First, find out the name of the logical volume you want to remove. You can get a list of all logical volumes installed on the system with:
 
First, find out the name of the logical volume you want to remove. You can get a list of all logical volumes installed on the system with:
  
{{cli|# lvs}}
+
{{bc|# lvs}}
  
 
Next, look up the mountpoint for your chosen logical volume...:
 
Next, look up the mountpoint for your chosen logical volume...:
  
{{cli|$ df -h}}
+
{{bc|$ df -h}}
  
 
... and unmount it:
 
... and unmount it:
  
{{cli|# umount /your_mountpoint}}
+
{{bc|# umount /your_mountpoint}}
  
 
Finally, remove the logical volume:
 
Finally, remove the logical volume:
  
{{cli|# lvremove /dev/yourVG/yourLV}}
+
{{bc|# lvremove /dev/yourVG/yourLV}}
  
Confirm by typing "y" and you are done.
+
Confirm by typing {{keypress|y}} and you are done.
  
Dont forget, to update /etc/fstab:
+
Do not forget, to update {{ic|/etc/fstab}}!
 
+
{{cli|# sudo nano /etc/fstab}}
+
  
 
You can verify the removal of your logical volume by typing "lvs" as root again (see first step of this section).
 
You can verify the removal of your logical volume by typing "lvs" as root again (see first step of this section).
  
=== Add partition to a volume group ===
+
=== Add physical volume to a volume group ===
To add a partition to your volume group you must first make its type 'Linux LVM' (for example with {{Codeline|cfdisk}}). Then you need to create a physical volume on it and extend the volume group over it:
+
 
# pvcreate /dev/sdb1
+
You first create a new physical volume on the block device you wish to use, then extend your volume group
# vgextend VolGroup00 /dev/sdb1
+
 
Now you have free space in your volume group that can be used by logical volumes in this group.
+
{{bc|1=
 +
# pvcreate /dev/sdb1
 +
# vgextend VolGroup00 /dev/sdb1}}
 +
 
 +
This of course will increase the total number of physical extents on your volume group, which can be allocated by logical volumes as you see fit.
  
{{Tip|You can add partitions from any disks to volume groups.}}
+
{{Note|It is considered good form to a partition table on your storage medium below LVM, and use the appropriate type code: {{ic|8e}} for MBR, and {{ic|8e00}} for GPT partitions.}}
  
 
=== Remove partition from a volume 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:
 
All of the data on that partition needs to be moved to another partition. Fortunately, LVM makes this easy:
 
  # pvmove /dev/sdb1
 
  # pvmove /dev/sdb1
If you want to have the data on a specific physical volume, specify that as the second argument to {{Codeline|pvmove}}:
+
If you want to have the data on a specific physical volume, specify that as the second argument to {{Ic|pvmove}}:
 
  # pvmove /dev/sdb1 /dev/sdf1
 
  # pvmove /dev/sdb1 /dev/sdf1
 
Then the physical volume needs to be removed from the volume group:
 
Then the physical volume needs to be removed from the volume group:
Line 234: Line 255:
 
With that volume, you may modify less than 100M of data, before the snapshot volume fills up.
 
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 {{Filename|/etc/mkinitcpio.conf}}, otherwise the system will not boot. If you do this on an already installed system, make sure to rebuild the image with
+
It is important to have the ''dm-snapshot'' module listed in the MODULES variable of {{ic|/etc/mkinitcpio.conf}}, 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
+
  # mkinitcpio -g /boot/initramfs-linux.img
  
Todo: scripts to automate snapshots of root before updates, to rollback... updating {{Filename|menu.lst}} to boot snapshots (separate article?)
+
Todo: scripts to automate snapshots of root before updates, to rollback... updating {{ic|menu.lst}} 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.
 
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.
Line 248: Line 269:
 
  # lvm pvdisplay
 
  # lvm pvdisplay
  
=== Set Filesystem Mountpoints page does not show logical volumes ===
+
=== Set filesystem mountpoints page does not 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 do not see the list of 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 do not see the list of logical volumes.
  
Line 258: Line 279:
 
in order to activate the volume group and make the logical volumes available.
 
in order to activate the volume group and make the logical volumes available.
  
===Receiving Input/Output Errors after plugging in a removable device with LVM partitions===
+
===LVM on removable media===
 
Symptoms:
 
Symptoms:
 
  ~$ sudo vgscan
 
  ~$ sudo vgscan
Line 278: Line 299:
 
  # vgscan
 
  # vgscan
 
  # vgchange -ay <volume group name>
 
  # vgchange -ay <volume group name>
 
== Tips & Tricks ==
 
Todo
 
  
 
== Additional resources ==
 
== Additional resources ==
Line 287: Line 305:
 
* [http://www.gentoo.org/doc/en/lvm2.xml Gentoo LVM2 installation] guide at Gentoo Documentation
 
* [http://www.gentoo.org/doc/en/lvm2.xml Gentoo LVM2 installation] guide at Gentoo Documentation
 
* [http://en.gentoo-wiki.com/wiki/LVM LVM] article at Gentoo Wiki
 
* [http://en.gentoo-wiki.com/wiki/LVM LVM] article at Gentoo Wiki
* [http://www.joshbryan.com/blog/2008/01/02/lvm2-mirrors-vs-md-raid-1LVM2 Mirrors vs. MD Raid 1] post by Josh Bryan
+
* [http://www.joshbryan.com/blog/2008/01/02/lvm2-mirrors-vs-md-raid-1/ LVM2 Mirrors vs. MD Raid 1] post by Josh Bryan

Revision as of 13:25, 9 January 2013

Tango-view-refresh-red.pngThis article or section is out of date.Tango-view-refresh-red.png

Reason: This article has not been updated to use the new Arch install scripts. References to /arch/setup and the installer are not applicable to recent installation media. (Discuss in Talk:LVM#)
Summary help replacing me
This article will provide an example of how to install and configure Arch Linux with Logical Volume Manager (LVM).
Required software
lvm2
Related
Software RAID and LVM
System Encryption with LUKS

Introduction

Template:Wikipedia

LVM (Logical Volume Management) makes use of the device-mapper feature of the Linux kernel to provide a system of partitions that is independent of the underlying disk's layout. With LVM you can abstract your storage space and have "virtual partitions" which makes it easier to extend and shrink partitions (subject to the filesystem you use allowing this) and add/remove partitions without worrying about whether you have enough contiguous space on a particular disk, without getting caught up in the problems of fdisking a disk that is in use (and wondering whether the kernel is using the old or new partition table) and without having to move other partition out of the way. This is strictly an ease-of-management issue: it does not provide any addition security. However, it sits nicely with the other two technologies we are using.

Note that LVM is not used for the boot partition, because of the bootloader problem.

Tip: /boot cannot reside in a LVM partition when using GRUB Legacy, which does not support LVM. If you want /boot to reside in a LVM partition, use GRUB2.

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 does not 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)
  • ...

Example:

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).

Advantages

Here are some things you can do with LVM that you cannot (or cannot do easily) with just mdadm, MBR partitions, GPT partitions, parted/gparted and a file-level tool like rsync.

  1. Online/live partition resizing
  2. No need for an extended partition (not relevant for GPT)
  3. Resize partitions regardless of their order on disk (no need to ensure surrounding available space)
  4. Online/live migration of partitions being used by services without having to restart services

These can be very helpful in a server situation, desktop less so, but you must decide if the features are worth the abstraction.

Installation

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 cfdisk (or any other tool of your liking). Because grub legacy (grub with version less than 1.0) cannot boot from LVM logical volumes you cannot have /boot 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.

Partition disks

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.

Warning: /boot cannot reside in LVM when using GRUB Legacy, which does not support LVM. If you want /boot to reside in a LVM partition, use GRUB2.
Tip: All LVM partitions on all disks can be configured to appear as one big disk.

Create physical volumes

Now you need to initialize these partitions so they can be used by LVM. Use fdisk -l to find out which partitions have filesystem type 'Linux LVM' and create a physical volume on them:

# pvcreate /dev/sda2

Substitute /dev/sda2 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:

# pvdisplay
Note: If using a SSD use pvcreate --dataalignment 1m /dev/sda2 (for erase block size < 1MiB), see e.g. here

Create volume group

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:

# vgdisplay
Note: You can create more than one volume group if you need to, but then you will not have all your storage presented as one disk.

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 /dev/mapper/Volgroup00-lvolhome or /dev/VolGroup00/lvolhome. 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 -C y is used to create a contiguous partition, which means that your swap space does not 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:

# lvdisplay
Note: You may need to load the device-mapper kernel module (modprobe dm-mod) for the above commands to succeed:
Tip: You can start out with relatively small logical volumes and expand them later if needed. For simplicity, leave some free space in the volume group so there is room for expansion.

Create filesystems and mount logical volumes

Your logical volumes should now be located in /dev/mapper/ and /dev/YourVolumeGroupName. If you cannot find them, use the next commands to bring up the module for creating device nodes and to make volume groups available:

# modprobe dm-mod
# vgscan
# vgchange -ay

Now you can create filesystems on logical volumes and mount them as normal partitions (if you are installing Arch linux, refer to mounting the partitions for additional details):

# mkfs.ext4 /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!

Set filesystem mountpoints

  • When choosing mountpoints, just select your newly created logical volumes (use: /dev/mapper/Volgroup00-lvolhome).
    Do NOT select the actual partitions on which logical volumes were created (do not use: /dev/sda2).

Configure system

For late-boot activation (non-root-filesystem) of volume groups, enable the lvm unit file,

# systemctl enable lvm.service

If you are using LVM on an encrypted device, use this instead:

# systemctl enable lvm-on-crypt.service

Or, if you still use sysvinit, modify USELVM appropriately:

/etc/rc.conf:
USELVM="yes"

For early-boot activation of volume groups (namely for root filesystem containers), you'll need to make sure the lvm2 and udev mkinitcpio hooks are enabled.

/etc/mkinitcpio.conf:
HOOKS="base udev ... lvm2 filesystems"

Also make sure the dm_mod module is enabled.

/etc/mkinitcpio.conf:
MODULES="dm_mod ..."

You will need to rebuild the initramfs to commit any changes you made.

Configuration

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 us 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

You may use lvresize instead of lvextend.

If you want to fill all the free space on a volume group, use the next command:

# lvextend -l +100%FREE VolGroup00/lvolhome
Warning: Not all filesystems support growing without loss of data and/or growing online.
Note: If you do not resize your filesystem, you will still have a volume with the same size as before (volume will be bigger but partly unused).

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 us 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 did not 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 lvresize instead of lvreduce.

Warning:
  • Do not reduce the filesystem size to less than the amount of space occupied by data or you risk data loss.
  • Not all filesystems support shrinking without loss of data and/or shrinking online.
Note: It is better to reduce the filesystem to a smaller size than the logical volume, so that after resizing the logical volume, we do not accidentally cut off some data from the end of the filesystem.

Remove logical volume

Warning: Before you remove a logical volume, make sure to move all data that you want to keep somewhere else, otherwise it will be lost!

First, find out the name of the logical volume you want to remove. You can get a list of all logical volumes installed on the system with:

# lvs

Next, look up the mountpoint for your chosen logical volume...:

$ df -h

... and unmount it:

# umount /your_mountpoint

Finally, remove the logical volume:

# lvremove /dev/yourVG/yourLV

Confirm by typing Template:Keypress and you are done.

Do not forget, to update /etc/fstab!

You can verify the removal of your logical volume by typing "lvs" as root again (see first step of this section).

Add physical volume to a volume group

You first create a new physical volume on the block device you wish to use, then extend your volume group

# pvcreate /dev/sdb1
# vgextend VolGroup00 /dev/sdb1

This of course will increase the total number of physical extents on your volume group, which can be allocated by logical volumes as you see fit.

Note: It is considered good form to a partition table on your storage medium below LVM, and use the appropriate type code: 8e for MBR, and 8e00 for GPT partitions.

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/sdb1

If you want to have the data on a specific physical volume, specify that as the second argument to pvmove:

# pvmove /dev/sdb1 /dev/sdf1

Then the physical volume needs to be removed from the volume group:

# vgreduce myVg /dev/sdb1

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/sdb1

Snapshots

Introduction

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).

Configuration

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 /etc/mkinitcpio.conf, 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/initramfs-linux.img

Todo: scripts to automate snapshots of root before updates, to rollback... updating menu.lst 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.

Troubleshooting

LVM commands do not work

  • Load proper module:
# modprobe dm_mod
  • Try preceding commands with lvm like this:
# lvm pvdisplay

Set filesystem mountpoints page does not 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 do not see the list of logical volumes.

In this case, you may also need to do:

# vgscan

or:

# vgchange -ay <volgroup>

in order to activate the volume group and make the logical volumes available.

LVM on removable media

Symptoms:

~$ sudo vgscan
 Reading all physical volumes.  This may take a while...
 /dev/backupdrive1/backup: read failed after 0 of 4096 at 319836585984: Input/output error
 /dev/backupdrive1/backup: read failed after 0 of 4096 at 319836643328: Input/output error
 /dev/backupdrive1/backup: read failed after 0 of 4096 at 0: Input/output error
 /dev/backupdrive1/backup: read failed after 0 of 4096 at 4096: Input/output error
 Found volume group "backupdrive1" using metadata type lvm2
 Found volume group "networkdrive" using metadata type lvm2

Cause:

Removing an external LVM drive without deactivating the volume group(s) first. Before you disconnect, make sure to:
# vgchange -an <volume group name>

Fix: (assuming you already tried to activate the volume group with vgchange -ay <vg>, and are receiving the Input/output errors

# vgchange -an <volume group name>
Unplug the external drive and wait a few minutes
# vgscan
# vgchange -ay <volume group name>

Additional resources