Linux LVM – how to!

 

 

I will run through some use cases for using LVM. I will begin by giving an easy-to-understand description of how LVM works. Followed by how to set up the first volume group and logical volume and I’ll end it with info on how to extend an existing partition using ext3, ext4 and XFS.

At first, a drawing to visualize what LVM is all about:

Let’s describe them from the bottom and up:

Physical Volumes (PV): These are disks. There is a single LVM partition on each disk. LVM will automatically create this! These can also be RAID devices. If you run a software raid, you can simply add /dev/mdX devices. Or if you use SAN you can add the devices presented to your OS. Basically, any raw device will do.

Volume Groups (VG): The volume group are groups of physical volumes. The total size of the volume group equals the total size of all disks added to the group.

Logical Volumes (LV): These are slices of the volume group. I can decide the size of these exactly as I like and it does not matter if I  exceed the size of a single hard disk – LVM will take care of that. The operating system will through the device mapper see the logical volumes so you can add your filesystem of choice.

File Systems: The name is pretty much self explanatory. You can choose your favorite file system, e.g. ext4 and then just make the filesystem.

Let’s get down to business!

You should have a basic understanding of how LVM works by now.  This is the setup I will be using for testing:

Redhat Enterprise Linux 6
LVM  version 2.02.87
Virtualized in Virtual Box environment
4 vDisk: 20 GB for OS, 3 x 10 GB for LVM testing

Operating system on top of LVM

You can do that. In fact, it’s quite normal. There is just one partition you must create on your disk outside of LVM; your /boot disk. Otherwise it’s fine to use LVM. One good thing is that you can slice your disk into small pieces and then easily add disk space to partitions that need it – if you run out of space.

It makes good sense to put e.g. /var on a separate partition to avoid logfiles getting filled up, thus stalling your entire OS. Anyway, this is about LVM, back on track.

This is the layout for my operating system disk:

Device Mountpoint Size
/dev/osvg/root / 5G
/dev/sda1 /boot 200M
/dev/osvg/home /home 1G
/dev/osvg/var /var 2G
/dev/osvg/swap swap 2G

osvg is the volume group.

Now, let’s say I purchased a new hard disk, it’s 10G in size. I want to have 2 partitions; one for pictures and one for movies. Of course movies need more size than pictures, so I’d like to split it 30/70.

I could simply partition it and then done with that. But let’s use LVM instead. I start by adding my new disk to the LVM as a PV:

[root@tutsrv01 ~]# pvcreate /dev/sdb
Writing physical volume data to disk "/dev/sdb"
Physical volume "/dev/sdb" successfully created

Great, so now LVM knows about my disk. I can verify that with pvdisplay, to see physical volumes the LVM knows:

[root@tutsrv01 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               osvg
  PV Size               14.15 GiB / not usable 0
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              3622
  Free PE               1122
  Allocated PE          2500
  PV UUID               msF4tW-JcB1-obDQ-WjPI-2oPl-H8wi-ybEhzj

  "/dev/sdb" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb
  VG Name
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               A5Kqvj-tECM-W2Q6-QBDP-l4Lr-nZxb-hhFn2G

Great, so I want to create a volume group for my media files. I’ll call it media – and I want to use the /dev/sdb PV for it

[root@tutsrv01 ~]# vgcreate media /dev/sdb
  Volume group "media" successfully created
[root@tutsrv01 ~]# vgdisplay media
  --- Volume group ---
  VG Name               media
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               10.00 GiB
  PE Size               4.00 MiB
  Total PE              2559
  Alloc PE / Size       0 / 0
  Free  PE / Size       2559 / 10.00 GiB
  VG UUID               3VGq1Y-tsNi-EIyC-THYs-2wjT-uyOO-lRirqB

As you can see I created it and it has free space in it.  Next step is to create my two logical volumes for my data:

[root@tutsrv01 ~]# lvcreate -L 3G -n pictures media
  Logical volume "pictures" created
[root@tutsrv01 ~]# lvcreate -l+100%FREE -n movies media
  Logical volume "movies" created

You may notice that the two are created in different ways. You use -L for defining a size, whereas you use -l to define a percentage. The first partition I created 3 gigabytes large, and then I just want to have the other one 7 gigabytes – but as you may know, it can be tricky to just hit that exact number. Instead I tell it to just use 100% of the free space in the Volume group.

Now I need to build the filesystem on my logical volumes. I’ll use ext4

[root@tutsrv01 ~]# mkfs.ext4 /dev/media/pictures
[root@tutsrv01 ~]# mkfs.ext4 /dev/media/movies

I removed the output. Now I’m ready, so lets add these to fstab and mount them

[root@tutsrv01 mnt]# mkdir /mnt/pictures /mnt/movies
[root@tutsrv01 mnt]# echo "/dev/media/pictures /mnt/pictures ext4 noatime 1 2" >> /etc/fstab 
[root@tutsrv01 mnt]# echo "/dev/media/movies /mnt/movies ext4 noatime 1 2" >> /etc/fstab
[root@tutsrv01 mnt]# mount -a
[root@tutsrv01 mnt]# df -h
--SNIP--
/dev/mapper/media-pictures
                      3.0G   69M  2.8G   3% /mnt/pictures
/dev/mapper/media-movies
                      6.9G  144M  6.4G   3% /mnt/movies

Great! After some time I run into this:

/dev/mapper/media-pictures
                      3.0G  2.6G  302M  90% /mnt/pictures
/dev/mapper/media-movies
                      6.9G  6.0G  550M  92% /mnt/movies

Let’s extend the drive. I have purchased 2 new disks, cause I don’t want to run out of space very soon. They’re called /dev/sdc and /dev/sdd. I want to add 6G to pictures and the rest to movies.

[root@tutsrv01 ~]# pvcreate /dev/sdc /dev/sdd
  Writing physical volume data to disk "/dev/sdc"
  Physical volume "/dev/sdc" successfully created
  Writing physical volume data to disk "/dev/sdd"
  Physical volume "/dev/sdd" successfully created

[root@tutsrv01 ~]# vgextend media /dev/sdc /dev/sdd
  Volume group "media" successfully extended

[root@tutsrv01 ~]# lvextend -L+6G /dev/media/pictures 
  Extending logical volume pictures to 9.00 GiB
  Logical volume pictures successfully resized

[root@tutsrv01 ~]# lvextend -l+100%FREE /dev/media/movies 
  Extending logical volume movies to 20.99 GiB
  Logical volume movies successfully resized

So they have been extended now. Notice that the lvextend command uses a + because we are adding the diskspace. I could also avoid the + if I’d rather decide an absolute size.

Let’s resize the filesystem. ext4 supports doing this online:

[root@tutsrv01 ~]# resize2fs /dev/media/pictures 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/media/pictures is mounted on /mnt/pictures; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/media/pictures to 2359296 (4k) blocks.
The filesystem on /dev/media/pictures is now 2359296 blocks long.

[root@tutsrv01 ~]# resize2fs /dev/media/movies 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/media/movies is mounted on /mnt/movies; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/media/movies to 5501952 (4k) blocks.
The filesystem on /dev/media/movies is now 5501952 blocks long.

Voila!

/dev/mapper/media-pictures
                      8.9G  2.6G  5.9G  30% /mnt/pictures
/dev/mapper/media-movies
                       21G  6.1G   14G  31% /mnt/movies

Now that you’re hopelessly in love with LVM, you should definitely start using it and stop making excuses. There are other great features such as snapshotting – but I’m sure that you can’t wait diving into the manpages. Have fun!

Published
Categorized as Linux