Wednesday, August 27, 2014

How To Create an LVM Drive

I posted some time ago about how to add a new physical volume to an existing logical volume, but I need a new logical volume now. So let's write down the steps for doing that.
  1. Add the physical drive to the machine, and then format whatever space you want to use on it. Nowadays, I use gparted to do this, as fdisk doesn't seem to support really big drives. So we need to choose "lvm2 pv" (LVM version 2, physical volume) as the type.
    Let's suppose that partition is /dev/sdf1.
  2. Create the physical volume.

    > pvcreate -t /dev/sdf1
      TEST MODE: Metadata will NOT be updated and volumes will not be (de)activated.
      Physical volume "/dev/sdf1" successfully created
    > pvcreate /dev/sdf1
      Physical volume "/dev/sdf1" successfully created


    I always use the "-t" option first, to make sure the command will do what I wish.
  3. Check your work, if you wish:
    > pvdisplay /dev/sdf1For those new to LVM, the "physical volume" is the closest layer to the hardware. It represents actual physical diskspace.
  4. You can repeat those commands to create more physical volumes if you wish. These can then be grouped together in a "volume group", which basically acts as if it is one huge disk, even though it may physically be spread across a number of devices
  5. Create a volume group in which our new physical volume(s) can reside:
    > vgcreate newvg /dev/sdf1 [/dev/sdg1 ...]
      Volume group "newvg" successfully created
  6. Check your work, if you wish:
    > vgdisplay newvg
  7. Create the logical volume:
    > lvcreate -n newlv -l 100%FREE newvg
    The -n option gives the volume a name, and the -l option says how much space in the newvg volume group to allocate. There are a lot of ways to give this option. See the lvcreate manpage for info.
  8. Check your work:
    > lvcreate newlv
    This will also show you the path to the device. Here on Fedora 20, it is: /dev/newvg/newlv. I.e.: /dev/VolumeGroup/LogicalVolume. I don't know if that can vary from distro to distro.
  9. Finally, we need to create the actual filesystem:
    > mke2fs -L NewVolume /dev/newvg/newlv
    Of course, there are other ways to do this, depending upon exactly what you want to do.
The LVM HowTo has lots more information.