LVM - ArchWiki

# LVM

Related articles

- Software RAID and LVM
- dm-crypt/Encrypting an entire system#LVM on LUKS
- dm-crypt/Encrypting an entire system#LUKS on LVM
- Resizing LVM-on-LUKS

From Wikipedia:Logical Volume Manager (Linux):

  LVM is a logical volume manager for the Linux kernel; it manages disk drives and similar mass-storage devices.

### LVM Building Blocks

Logical Volume Management utilizes the kernel's device-mapper feature to provide a system of partitions independent of underlying disk layout. With LVM you abstract your storage and have "virtual partitions", making extending/shrinking easier (subject to potential filesystem limitations). Virtual partitions allow addition and removal without worry of whether you have enough contiguous space on a particular disk, getting caught up fdisking a disk in use (and wondering whether the kernel is using the old or new partition table), or, having to move other partitions out of the way. This is strictly an ease-of-management solution: LVM adds no security.

Basic building blocks of LVM:

- Physical volume (PV): Partition on hard disk (or even the 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 used to build your hard drive.
- Volume group (VG): Group of physical volumes used as a 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): The smallest size in the physical volume that can be assigned to a logical volume (default 4MiB). Think of physical extents as parts of disks that can be allocated to any partition.

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

### Advantages

LVM gives you more flexibility than just using normal hard drive partitions:

- Use any number of disks as one big disk.
- Have logical volumes stretched over several disks.
- Create small logical volumes and resize them "dynamically" as they get filled up.
- Resize logical volumes regardless of their order on disk. It does not depend on the position of the LV within VG, there is no need to ensure surrounding available space.
- Resize/create/delete logical and physical volumes online. File systems on them still need to be resized, but some (such as ext4) support online resizing.
- Online/live migration of LV being used by services to different disks without having to restart services.
- Snapshots allow you to backup a frozen copy of the file system, while keeping service downtime to a minimum.
- Support for various device-mapper targets, including transparent filesystem encryption and caching of frequently used data.

### Disadvantages

- Additional steps in setting up the system, more complicated.

## Installing Arch Linux on LVM

You should create your LVM Volumes between the partitioning and formatting steps of the installation procedure. Instead of directly formatting a partition to be your root file system, the file system will be created inside a logical volume (LV).

Make sure the lvm2 package is installed.

Quick overview:

- Create partition(s) where your PV(s) will reside. Set the partition type to 'Linux LVM', which is 8e if you use MBR, 8e00 for GPT.
- Create your physical volumes (PVs). If you have one disk it is best to just create one PV in one large partition. If you have multiple disks you can create partitions on each of them and create a PV on each partition.
- Create your volume group (VG) and add all PVs to it.
- Create logical volumes (LVs) inside that VG.
- Continue with “Format the partitions” step of Beginners' guide.
- When you reach the “Create initial ramdisk environment” step in the Beginners Guide, add the `lvm` hook to `/etc/mkinitcpio.conf` (see below for details).

Warning: `/boot` cannot reside in LVM when using GRUB Legacy, which does not support LVM. GRUB users do not have this limitation. If you need to use GRUB Legacy, you must create a separate `/boot` partition and format it directly.

### Create partitions

Note: This step is optional and depends on the users preference. In most cases it is recommended to partition the device first, though.

See Partitioning on how to create partitions on your device.

### Create physical volumes

To list all your devices capable of being used as a physical volume:

    # lvmdiskscan

Warning: Make sure you target the correct device, or below commands will result in data loss!

Create a physical volume on them:

    # pvcreate DEVICE

This command creates a header on each device so it can be used for LVM. As defined in LVM#LVM_Building_Blocks, DEVICE can be a disk (e.g. `/dev/sda`), a partition (e.g. `/dev/sda2`) or a loop back device. For example:

    # pvcreate /dev/sda2

You can track created physical volumes with:

    # pvdisplay

Note: If using a SSD without partitioning it first, use `pvcreate --dataalignment 1m /dev/sda` (for erase block size < 1MiB), see e.g. here

### Create volume group

The next step is to create a volume group on this physical volume.

First you need to create a volume group on one of the physical volumes:

    # vgcreate <volume_group> <physical_volume>

For example:

    # vgcreate VolGroup00 /dev/sda2

Then add to it all other physical volumes you want to have in it:

    # vgextend <volume_group> <physical_volume>
    # vgextend <volume_group> <another_physical_volume>
    # ...

For example:

    # vgextend VolGroup00 /dev/sdb1
    # vgextend VolGroup00 /dev/sdc

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 in one step

LVM allows you to combine the creation of a volume group and the physical volumes in one easy step. For example, to create the group VolGroup00 with the three devices mentioned above, you can run:

    # vgcreate VolGroup00 /dev/sda2 /dev/sdb1 /dev/sdc

This command will first set up the three partitions as physical volumes (if necessary) and then create the volume group with the three volumes. The command will warn you it detects an existing filesystem on any of the devices.

### 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 <size> <volume_group> -n <logical_volume>

For example:

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

You can also specify one or more physical volumes to restrict where LVM allocates the data. For example, you may wish to create a logical volume for the root filesystem on your small SSD, and your home volume on a slower mechanical drive. Simply add the physical volume devices to the command line, for example:

    # lvcreate -L 10G VolGroup00 -n lvolhome /dev/sdc1

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

    # lvcreate -l +100%FREE  <volume_group> -n <logical_volume>

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 file systems 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 file systems on logical volumes and mount them as normal partitions (if you are installing Arch linux, refer to mounting the partitions for additional details):

    # mkfs.<fstype> /dev/mapper/<volume_group>-<logical_volume>
    # mount /dev/mapper/<volume_group>-<logical_volume> /<mountpoint>

For example:

    # mkfs.ext4 /dev/mapper/VolGroup00-lvolhome
    # mount /dev/mapper/VolGroup00-lvolhome /home

Warning: 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`).

### Add lvm hook to mkinitcpio.conf

You will need to make sure the `udev` and `lvm2` mkinitcpio hooks are enabled.

`udev` is there by default. Edit the file and insert `lvm2` between `block` and `filesystems` like so:

    /etc/mkinitcpio.conf

    HOOKS="base udev ... block lvm2 filesystems"

Afterwards, you can continue in normal installation instructions with the create an initial ramdisk step.

## Configuration

### Advanced options

If you need monitoring (needed for snapshots) you can enable lvmetad. For this set `use_lvmetad = 1` in `/etc/lvm/lvm.conf`. This is the default by now.

You can restrict the volumes that are activated automatically by setting the `auto_activation_volume_list` in `/etc/lvm/lvm.conf`. If in doubt, leave this option commented out.

### Grow physical volume

After changing the size of a device that has a physical volume on it, you need to grow the physical volume using the following command:

    # pvresize DEVICE

For example, to grow a physical volume located on a mdadm RAID array:

    # pvresize /dev/md0

Note: This command can be done while the volume is online.

### Grow logical volume

To increase the available space on a filesystem that resides on a logical volume, two steps need to be done. First grow the logical volume, and then resize the filesystem to use the newly created free space.

#### lvextend

To grow a logical volume, two different commands can be used, `lvextend` or `lvresize`.

    # lvextend -L +<size> <volume_group>/<logical_volume>

For example:

    # lvextend -L +20G VolGroup00/lvolhome

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

    # lvextend -l +100%FREE <volume_group>/<logical_volume>

#### resize2fs

To resize an ext2,ext3 or ext4 filesystem:

    # resize2fs /dev/<volume_group>/<logical_volume>

Warning: Not all file systems support growing without loss of data and/or growing online.
Note: If you do not resize your filesystem, there will not be more free space available on the filesystem. The logical volume will be bigger but partly unused.

For example:

    # resize2fs /dev/VolGroup00/lvolhome

### Shrink logical volume

Because your file system is probably as big as the logical volume it resides on, you need to shrink the file system first and then shrink the logical volume. Depending on your file system, you may need to unmount it first. Let us say we have a logical volume of 15 GB with ext3 on it and we want to shrink it to 10 GB.

First shrink the filesystem more than needed so that when you later shrink the logical volume you do not accidentally cut off the end of the filesystem:

    # resize2fs /dev/VolGroup00/lvolhome 9G

Now resize the logical volume:

    # lvreduce -L 10G VolGroup00/lvolhome

Note: In order to specify a relative size with lvreduce, it is still needed to prefix the size with the `-` sign.
Tip: Alternatively, you may use lvresize instead of lvreduce:

    # lvresize -L -5G VolGroup00/lvolhome

Finally, normally grow the filesystem to fill all the free space left on the logical volume:

    # resize2fs /dev/VolGroup00/lvolhome

Warning:

- Do not reduce the file system size to less than the amount of space occupied by data or you risk data loss.
- Not all file systems support shrinking without loss of data and/or shrinking online.

### 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 with:

    # lvs

Next, look up the mountpoint of the chosen logical volume:

    $ lsblk

Then unmount the filesystem on the logical volume:

    # umount /<mountpoint>

Finally, remove the logical volume:

    # lvremove <volume_group>/<logical_volume>

For example:

    # lvremove VolGroup00/lvolhome

Confirm by typing in `y`.

Update `/etc/fstab` as necessary.

You can verify the removal of the 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 have a partition table on your storage medium below LVM. 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

### Deactivate volume group

Just invoke

    # vgchange -a n my_volume_group

This will deactivate the volume group and allow you to unmount the container it is stored in.

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

Reverting the modified 'pv' logical volume to the state when the 'snap01' snapshot was taken can be done with

`# lvconvert --merge /dev/vg0/snap01`

In case the origin logical volume is active, merging will occur on the next reboot.(Merging can be done even from a LiveCD)

The snapshot will no longer exist after merging.

Also multiple snapshots can be taken and each one can be merged with the origin logical volume at will.

The snapshot can be mounted and backed up with dd or tar. The size of the backup file done with dd will be the size of the files residing on the snapshot volume. To restore just create a snapshot, mount it, and write or extract the backup to it. And then merge it with the origin.

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 file system to make backups; a backup taking two hours provides a more consistent image of the file system than directly backing up the partition.

See Create root filesystem snapshots with LVM for automating the creation of clean root file system snapshots during system startup for backup and rollback.

Dm-crypt/Encrypting an entire system#LVM on LUKS and Dm-crypt/Encrypting an entire system#LUKS on LVM.

If you have LVM volumes not activated via the initramfs, enable the lvm-monitoring service, which is provided by the lvm2 package.

## Troubleshooting

### Changes that could be required due to changes in the Arch-Linux defaults

The `use_lvmetad = 1` must be set in `/etc/lvm/lvm.conf`. This is the default now - if you have a `lvm.conf.pacnew` file, you must merge this change.

### LVM commands do not work

- Load proper module:

    # modprobe dm_mod

The `dm_mod` module should be automatically loaded. In case it does not, you can try:

The factual accuracy of this article or section is disputed.

Reason: Should module loading at boot be done using "/etc/modules-load.d" instead? (Discuss)

    /etc/mkinitcpio.conf:

    MODULES="dm_mod ..."

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

- Try preceding commands with lvm like this:

    # lvm pvdisplay

### Logical Volumes do not show up

If you are trying to mount existing logical volumes, but they do not show up in `lvscan`, you can use the following commands to activate them:

    # vgscan
    # vgchange -ay

### LVM on removable media

Symptoms:

    # 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

### Kernel options

In kernel options, you may need `dolvm`. `root=` should be set to the logical volume, e.g `/dev/mapper/vg-name-lv-name`.

### Resizing a contiguous logical volume fails

If trying to extend a logical volume errors with:

    " Insufficient suitable contiguous allocatable extents for logical volume "

The reason is that the logical volume was created with an explicit contiguous allocation policy (options `-C y` or `--alloc contiguous`) and no further adjacent contiguous extents are available (see also reference).

To fix this, prior to extending the logical volume, change its allocation policy with `lvchange --alloc inherit <logical_volume>`. If you need to keep the contiguous allocation policy, an alternative approach is to move the volume to a disk area with sufficient free extents (see [1]).

## See also

- LVM2 Resource Page on SourceWare.org
- LVM HOWTO article at The Linux Documentation project
- LVM article at Gentoo wiki
- LVM2 Mirrors vs. MD Raid 1 post by Josh Bryan
- Ubuntu LVM Guide Part 1 Part 2 detals snapshots

Categories:

- Getting and installing Arch
- File systems