How I create LVM volumes

user warning: Table './drinking_drpl2/watchdog' is marked as crashed and last (automatic?) repair failed query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'flickr', 'Could not connect to Flickr, Error: Forbidden', 'a:0:{}', 4, '', 'http://rothwerx.com/content/how-i-create-lvm-volumes', '', '54.80.175.56', 1498331387) in /home1/drinking/public_html/rothwerx/modules/dblog/dblog.module on line 146.

Jeremiah - Posted on 08 March 2010

This is one of those things I'm mostly posting for my own reference, as I often forget everything I need to do, i.e. the uuid stuff.

  • Create partitions on the disks you want to use. You can use parted or cfdisk; I've always used fdisk but the man page for fdisk recommends against using it and instead using parted or cfdisk, so that's what I'll be using from now on I suppose.
  • Initialize the partitions (physical volumes) with pvcreate:
    pvcreate /dev/sdb1 /dev/sdc1
  • Create a new volume group if you don't already have one you want to use. Here we'll name it simply vg0, with a physical extent (PE) size of 16M, and add the two disks we initialized in the previous step:
    vgcreate -s 16M vg0 /dev/sdb1 /dev/sdc1
  • We want to use all the free space afforded on these disks, so we'll look for the Total PE number using vgdisplay, and then create a logical volume called "backup":
    vgdisplay vg0 | grep "Total PE"
    Total PE 59616
    lvcreate -l 59616 -n backup vg0
  • That should create a volume at /dev/vg0/backup. Now we want to create a filesystem on it. I'll use ext3 because my needs are modest and ext3 is pretty safe. I'll also set the reserved block count to 1%:
    mkfs.ext3 -m 1 -v /dev/vg0/backup
  • Ok, now we should have a working volume. You can test it by mounting it if you want. If everything goes well mount should automatically recognize the ext3 FS:
    mkdir /tmp/backup; mount /dev/vg0/backup /tmp/backup
  • To do this all properly we need to use UUID's in all the proper places. First:
    uuidgen /dev/vg0/backup > uuid
  • Now apply it to the volume using tune2fs:
    tune2fs -U `cat uuid` /dev/vg0/backup
  • It's a good idea to make a copy of your fstab before doing the following where we add a line to it (in case you forget the extra appending angle bracket):
    echo "UUID=`cat uuid` /mnt/backup ext3 defaults 0 2" >> /etc/fstab
  • And finally, a symlink:
    ln -s /dev/vg0/backup /dev/disk/by-uuid/`cat uuid`
  • Now when you reboot your system you should have your LVM volume mounted - in my case at /mnt/backup.