User:SwifT/Wikified but not merged documents/LVM2

From Gentoo Wiki
Jump to:navigation Jump to:search


This guide is based on an example with two SATA hard disks. It means that you will more than likely need to change the drive, partition names and partition sizes to match your own setup and needs.

This document is not intended to be an LVM2 tutorial. It serves as a supplement to the Gentoo installation procedure as described in the Handbook, Part 1 . Make sure you read the Gentoo Installation Manual before you start your installation process.
For a complete LVM HOWTO point your browser to

Initial requirements

If you do a fresh install of Gentoo, you will need to use a bootable CD with LVM2 support such as a Gentoo Installation CD. You can find the Installation CDs for an x86 architecture on our mirrors under /releases/x86/current/installcd . Other architectures might be supported as well.

If you install LVM2 on a currently running system with some spare hard disk space, you will need to enable the LVM2 module ( dm-mod ) in the kernel. This module is available in gentoo-sources . Compiling your kernel and getting LVM2 to work is covered later in this guide.


Our example system has two SATA hard disks and will be partitioned as follows:

  • /dev/sda1 -- /boot
  • /dev/sda2 -- (swap)
  • /dev/sda3 -- /
  • /dev/sda4 -- Will be used by LVM2
  • /dev/sdb1 -- Will be used by LVM2

Pay attention to the partition names as it is easy to confuse the a's and b's, and the partition numbers. One false move could wipe out the wrong partition. You have been warned!

OK, time to start...


Follow the handbook, but with the following amendments to chapter 4. Preparing the Disks :

Use fdisk as described in the handbook, but use the partition scheme mentioned above as an example. It is only an example , adapt it to your own needs.

Create a small physical /boot partition (sda1). In this example, /boot will be not managed by LVM2. This partition will contain your bootloader and your kernel(s). A 64MB partition should be well enough for quite a few kernel generations.

Create a swap partition (sda2).

Create a / (root) partition (sda3). If you are interested in trying to put your root partition under LVM management (which we do not recommend), see the resources section at the end of this guide for a link to a mini-howto on how to do this. The size of the root partition need not be large if you will keep /opt /usr /home /var and /tmp in an LVM2 Volume Group (vg). In this case, 1GB should be sufficient. Note however that, if you have /usr or /var in an LVM2 volume group, you will need to boot the system with an initramfs.

It is not recommended to put the following directories in an LVM2 partition: /etc , /lib , /mnt , /proc , /sbin , /dev , and /root . This way, you would still be able to log into your system (crippled, but still somewhat usable, as root) if something goes terribly wrong. Also exclude /usr and /var from LVM2 if you do not want to boot with an initramfs.

Assuming the /boot , swap and root partitions do not use the whole physical disk, create a fourth partition on this disk and set it to type 8e (Linux LVM). If you have more physical drives you would like to use with LVM, create one partition on each and give them the same type (8e).

Considering the huge size of current disks, you might consider splitting your hard disks into smaller partitions instead of creating a big partition that will be added to an LVM2 volume group in one block. LVM2 makes it easy to extend your volumes after all. This leaves you some unallocated partitions you might need to use outside of an LVM2 group. In short, don't use your disk space until you know you need it. As an example, one contributor had split his 160Â Gb hard disk into 8 partitions of 20Â Gb each.

Create the filesystems on /dev/sda1 and /dev/sda3 , and create and activate the swap on /dev/sda2 as described in the handbook.

Start the lvm service.

CODE Starting the lvm service
# rc-config start lvm

It is recommended to add the lvm service to the boot runlevel. This way LVM2 will be activated each time the system boots.

CODE Adding lvm to the boot runlevel
# rc-config add lvm boot

Before scanning and activating LVM, you might want to edit /etc/lvm/lvm.conf to exclude some devices. By default, LVM2 will scan all devices, even your CDROM which can generate error messages. In the following example, the line that allows scanning of all devices is replaced by one that rejects every device but our two SATA disks.

CODE Activating LVM
## (Avoid scanning all devices but our disks)
# nano -w /etc/lvm/lvm.conf
## (Look for the following line)
    filter = [ "a/.*/" ]
## (Replace it with the following one to scan
/dev/sda and /dev/sdb and reject anything else)
    filter = [ "a|/dev/sd[ab]|", "r/.*/" ]
## (Save the file and quit nano)
# vgscan
  Reading all physical volumes. This may take a while...
  No volume groups found
## (Make any previously set up volume groups available)
# vgchange -a y

Prepare the partitions.

CODE Preparing the partitions
# pvcreate /dev/sda4 /dev/sdb1
  No physical volume label read from /dev/sda4
  Physical volume "/dev/sda4" successfully created
  No physical volume label read from /dev/sdb1
  Physical volume "/dev/sdb1" successfully created

Setup a volume group. A volume group is the result of combining several physical units into a single logical device.

In our example, /dev/sda1 , /dev/sda2 and /dev/sda3 are the /boot , swap and root partitions so we need to combine /dev/sda4 and /dev/sdb1 . It can be done with a single command, but, as an example, we will create our volume group and extend it.

CODE Creating and extending a volume group
## (Create a volume group named vg)
# vgcreate vg /dev/sda4
  /etc/lvm/backup: fsync failed: Invalid argument ## (Ignore this warning)
  Volume group "vg" successfully created
## (Extending an existing volume group)
# vgextend vg /dev/sdb1
  /etc/lvm/backup: fsync failed: Invalid argument ## (Ignore this warning, again and later as well)
  Volume group "vg" successfully extended

Create the logical volumes. Logical volumes are the equivalent of partitions you would create using fdisk in a non LVM2 environment. In our example, we create the following partitions:

Directory Size
/usr 10 GB
/home 5 GB
/opt 5 GB
/var 10 GB
/tmp 2 GB

Since we are going to use LVM2, we should not worry too much about partition sizes because they can always be expanded as needed.

As Terje Kvernes commented, it is easier to increase the size of a partition then to shrink it. You might therefore want to start with smaller partitions and increase their size as needed.

CODE Creating and extending logical volumes
# lvcreate -L10G -nusr  vg
  Logical volume "usr" created ## (Further similar messages not displayed)
# lvcreate -L5G  -nhome vg
# lvcreate -L5G  -nopt  vg
# lvcreate -L10G -nvar  vg
# lvcreate -L2G  -ntmp  vg
## (As an example, let's extend a logical volume with 5 extra Gbytes)
# lvextend -L+5G /dev/vg/home

Create filesystems on the logical volumes the same way you would on a regular partition. We use ext3 on the logical volumes but any filesystem of your choice will work:

CODE Creating the filesystems
# mke2fs -j /dev/vg/usr
# mke2fs -j /dev/vg/home
# mke2fs -j /dev/vg/opt
# mke2fs -j /dev/vg/var
# mke2fs -j /dev/vg/tmp

Mount your partitions as described in the handbook and mount your LVM2 logical volumes as if they were partitions. Replace the usual /dev/sdxx with /dev/vg/logical_volumename .

CODE Mounting your logical volumes
## (Make sure you have mounted your root partition as described in the handbook first)
# mkdir /mnt/gentoo/usr
# mount /dev/vg/usr /mnt/gentoo/usr
# mkdir /mnt/gentoo/home
# mount /dev/vg/home /mnt/gentoo/home
# mkdir /mnt/gentoo/opt
# mount /dev/vg/opt /mnt/gentoo/opt
# mkdir /mnt/gentoo/var
# mount /dev/vg/var /mnt/gentoo/var
# mkdir /mnt/gentoo/tmp
# mount /dev/vg/tmp /mnt/gentoo/tmp

The rest of the installation handbook is mostly unchanged so we shall not walk you through it again except to point out differences.

When configuring your kernel, make sure to configure your kernel to support LVM2. Select the LVM2 module as follows:

CODE Selecting the LVM2 module in the Linux kernel
Device Drivers  --->
 Multiple devices driver support (RAID and LVM) --->
   [*] Multiple devices driver support (RAID and LVM)
   < >   RAID support
   <M>   Device mapper support

The compiled module is called dm-mod.ko

Also build an initramfs if you have /usr or /var on an LVM-based partition. Don't forget to edit your boot loader to boot the system with the initramfs and add in dolvm as a boot parameter.

CODE Building an initramfs
# emerge genkernel
# genkernel --lvm --install initramfs
# nano -w /boot/grub/grub.conf
## (...)
title Gentoo Linux
root (hd0,0)
kernel /kernel-3.2.2-gentoo-r5 root=/dev/sda1 dolvm
initrd /initramfs-genkernel-x86_64-3.2.2-gentoo-r5

Now, install the lvm2 package.

Make sure your /usr/src/linux link points to the kernel sources you are using because the lvm2 ebuild depends on the device-mapper ebuild which will check the presence of a required source file under /usr/src/linux/include/linux .

CODE Emerging the LVM2 package
# emerge lvm2

Edit /etc/lvm/lvm.conf as described . The file you previously edited is part of your installation environment and will disappear after the next reboot. This time, you edit the real one inside your new Gentoo install.

When editing your /etc/fstab file, follow the handbook and add your LVM2 logical volumes as needed. Again, here are a few lines needed for our example:

CODE Extract of /etc/fstab
/dev/sda1     /boot   ext3    noauto,noatime 1 2
/dev/sda2     none    swap    sw             0 0
/dev/sda3     /       ext3    noatime        0 1
# Logical volumes
/dev/vg/usr   /usr    ext3    noatime        0 2
/dev/vg/home  /home   ext3    noatime        0 2
/dev/vg/opt   /opt    ext3    noatime        0 2
/dev/vg/var   /var    ext3    noatime        0 2
/dev/vg/tmp   /tmp    ext3    noatime        0 2

When you reach the end of the installation part of the handbook, don't forget to umount all your LVM2 logical volumes as well and for a good measure run the following command before you reboot:

CODE Shutting down LVM2
# vgchange -a n

Restart your machine and all partitions should be visible and mounted.

Continuing After a Reboot

If you have interrupted the Gentoo installation at one point and want to continue, you need to create the volume device nodes first:

CODE Reactivating the volumes
# vgscan --mknodes

Installation CDs with less recent tools might need to reactivate the volumes instead:

CODE Reactivating the volumes
## (Deactivate all volumes first)
# vgchange -a n
## (Export all the volumes)
# vgexport -a
## (Import all volumes)
# vgimport -a
## (Reactivate all volumes)
# vgchange -a y



Thanks Bangert Thilo Bangertand Kvernes Terje Kvernesfor their help and comments on this document.


We would like to thank the following authors and editors for their contributions to this guide:

  • Avi Schwartz
  • Rajiv Manglani
  • Xavier Neys