LVM how to

Written on April 7, 2009 by Kev

This is a quick and dirty guide to setting up an LVM based filesystem.

First create some disk partitions. In this example I’ve got two identical unformatted partitions on a couple of hard drives. These appear in /dev as sda4 and sdb4. Please note your partition number will most likely be different.
Also note that it’s easy to break your system with some of these commands. You have been warned, so don’t blame me if you wipe your system out!

First make the partitions available to LVM:
pvcreate /dev/sda4
Physical volume “/dev/sda4″ successfully created

pvcreate /dev/sdb4
Physical volume “/dev/sdb4″ successfully created

Next create a volume group:
vgcreate xenimagesvg /dev/sda4 /dev/sdb4
Volume group “xenimagesvg” successfully created

We can check the size etc of the new volume group with vgdisplay:

vgdisplay xenimagesvg
— Volume group —
VG Name xenimagesvg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 289.52 GB
PE Size 4.00 MB
Total PE 74116
Alloc PE / Size 0 / 0
Free PE / Size 74116 / 289.52 GB
VG UUID m408F0-77d6-WXwU-ykD9-5OCB-pIkG-cSbIDP

Next, we need to create a logical volume in our new volume group:
lvcreate -n xenimageslv -L 250G xenimagesvg

The above will create a logical volume called xenimageslv in volume group xenimagesvg

Now we need to format the logical volume so that it can be used as a filesystem.
I’m going to format to ext3 using mke2fs with -j (for journalling, aka ext3):
mke2fs -j /dev/mapper/xenimagesvg-xenimageslv
mke2fs 1.41.3 (12-Oct-2008)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
16384000 inodes, 65536000 blocks
3276800 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
2000 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

And finally we need to mount our new filesystem so that we can actually use it. Here I’ll add the entry into fstab so that it mounts on system startup.

Create a folder to mount the filesystem into:
mkdir xenimages

edit fstab and add the following line to mount our new lvm filesystem at system startup…
/dev/mapper/xenimagesvg-xenimageslv /xenimages ext3 errors=remount-ro 0 0

Now run mount-a to re-run fstab mounts and mount the new filesystem

Check it’s mounted with df -h:
df -h
Filesystem Size Used Avail Use% Mounted on
21G 4.5G 15G 24% /
tmpfs 1.9G 0 1.9G 0% /lib/init/rw
udev 10M 124K 9.9M 2% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/md1 92M 51M 36M 59% /boot
247G 188M 234G 1% /xenimages