Some Acronyms

  • VG= Volume Group (an object that binds LVs and PVs which are exclusive to a particular VG)

  • PV= Physical Volume (a real drive or something that looks real to LVM)

  • LV= Logical Volume (a mountable device which can contain filesystems in an LVM setup)

  • PE= Physical Extents (arbitary PV chunks used for accounting by LVM, same size for all PVs in VG)

  • LE= Logical Extent (arbitrary LV chunks, same size for all LVs in VG)

Preparing System for LVM

Kernel Setup

Need to enable device mapper.

    Device Drivers  --->
        Multi-device support (RAID and LVM)  --->
            [*] Multiple devices driver support (RAID and LVM)
            <*> Device mapper support

And this looks fun! If you want dm-crypt support, do this.

<*> Crypt target support

User Utilities

Get user utilities if not already present.

emerge sys-fs/lvm2
emerge sys-fs/cryptsetup-luks   # Very optional.

Physical Volume Managemnt

Paranoid?

If you’re going to set up a crypto filesystem and you’re really serious about entropy, now is a good time fill the drive with random crap.

dd if=/dev/urandom of=/dev/sdb bs=4096

Partitioning

Set up a Linux LVM partition with fdisk, type 8e.

Preparing Drives

Prevent LVM from scanning dumb things by adding a filter in the /etc/lvm/lvm.conf. This keeps lvm confined to drive b.

filter = [ "a/dev/sdb.*", "r/.*/" ]

It’s not unwise to break up the physical disk into a few partitions. The idea is that if for some reason you need a non LVM partition, you can get it. Once LVM is working, the multiple partitions won’t be noticeable anyway. Here’s an example:

Disk /dev/sdb: 80.0 GB, 80000000000 bytes
255 heads, 63 sectors/track, 9726 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
   /dev/sdb1               1        2433    19543041   8e  Linux LVM
   /dev/sdb2            2434        4866    19543072+  8e  Linux LVM
   /dev/sdb3            4867        7299    19543072+  8e  Linux LVM
   /dev/sdb4            7300        9726    19494877+  8e  Linux LVM

Creating Physical Volumes (PV)

Let LVM know what it has to work with by creating physical volumes.

    # pvcreate /dev/sdb[1234]
    Physical volume "/dev/sdb1" successfully created
    Physical volume "/dev/sdb2" successfully created
    Physical volume "/dev/sdb3" successfully created
    Physical volume "/dev/sdb4" successfully created

Finding out about Physical Volumes (PV)

pvdisplay - for each PV, shows VG, the PV size (i.e. hard drive’s actual manufacturer specified size), PE size in bytes, Total PE (which should be similar, but not exactly the PV size), Free PE, Allocated PE (TotalPE - FreePE).

So to find how much drive is unused, multiply "Free PE" by "PE Size" (FPE PES * 1e6 / =⇒ answer in GB).

Note
This seems to only work after creating a volume group.

Volume Group Management

Creating and Extending Volume Groups (VG)

After telling LVM what real hardware there is to work with, some of that can get added to a "volume group" which will then be like a virtual drive that is a lot nicer to work with than a crusty real one. This creates a VG called xed_vg with the first 2 sdb partitions.

vgcreate xed_vg /dev/sdb[12]

To make your VG bigger by adding more physical volumes:

vgextend xed_vg /dev/sdb[34]

Finding out about Volume Groups (VG)

To get statistics about the virtual drive, i.e. the volume group, use:

vgdisplay xed_vg

Removing a Physical Volume from a VG

This takes the device /dev/hdb3 out of the pool of physical resources for the_cool_vol_group.

pvreduce the_cool_vol_group /dev/hdb3

Logical Volume Management

Creating Logical Volumes

The default size spec with -L is in MB, but G,K,T can be used. An even more sophisticated way to establish the size of LV is to specify the size in LE which is the natural chunk size for the VG.

lvcreate -L 15G -n xed_MonThuBackup xed_vg
lvcreate -L 15G -n xed_TueFriBackup xed_vg
lvcreate -L 15G -n xed_WedSatBackup xed_vg
lvcreate -L 15G -n xed_SunBackup xed_vg

To create a 256MB device which can be used as a swap partition.

lvcreate -L 256 -n xed.swap xed_vg

Once the logical volumes are created, you can format or otherwise use them like normal partitions.

Finding out about Logical Volumes (LV)

lvdisplay /dev/xen_vg/dom0

Shows what VG it’s in. Shows the LV size in human readable format. Shows LE. And much, much more!

With no arguments, shows all LV summaries.

Setting Up A Data Drive

It turns out that you can use LVM on raw disks (no partition tables needed). Here is an example of that.

pvcreate /dev/sdb
vgcreate vg_fourtb /dev/sdb
vgdisplay
lvcreate -l 100%VG -n special_archive vg_fourtb
mkfs.ext3 -v -L SPECIALARCH /dev/vg_fourtb/special_archive
vi /etc/fstab
echo "/dev/vg_fourtb/special_archive /local ext3 rw,auto 0 0" >> /etc/fstab
mkdir /local
mount /local

Fixing Unavailable Volumes

If the volumes turn up as "inactive" or "unavailable" (like after a reboot using a boot CD), you might need to tell the kernel’s mapper about the lv’s explicitly. You can do an lvscan and see the inactive ones:

    livecd root # lvscan
        ACTIVE            '/dev/vg-spot/spot-swap' [64.00 MB] inherit
        inactive          '/dev/vg-spot/spot-tmp' [64.00 MB] inherit
        inactive          '/dev/vg-spot/spot-var' [64.00 MB] inherit
        inactive          '/dev/vg-spot/spot-root' [756.00 MB] inherit

To make active:

lvchange -a y /dev/vg-spot/spot-root

Deleting Logical Volumes

To get rid of a logical volume completely use the lvremove command. Note that you need to specify the complete path to the logical volume.

    # lvremove /dev/test_vg/test_5G_lv
    Do you really want to remove active logical volume test_5G_lv?
    [y/n]: y
      Logical volume "test_5G_lv" successfully removed

Activating LVs Already On the Drive

Let’s say you have a drive that already has had LVM setup the partitions. Now you can use pvdisplay, vgdisplay, and lvdisplay, but the devices listed are not there. You need to use this command:

lvchange -ay

Now you should be able to find the volumes listed by lvdisplay.

Extending a Logical Volume

If you have some space available, for example if you buy a new drive and add it to the system, this is how to enlarge a logical volume. I don’t know this for a fact, but it’s probably a good idea to unmount the thing before trying this trick.

Take a look at the free space using vgdisplay. Then

# lvextend -l +358 /dev/xen_vg/tempscratch

Here, 358 is the number of Free Physical Extents in the vg.