Logical Volume Manager and Logical Volumes – Linux

Introduction

This post describes what is LVM (Logical Volume Manager) in Linux and how to create Logical volumes.

LVM is a higher level layer of abstraction then traditional linux disk and partitions. This allows for greater flexibility in allocating storage. Logical volumes can be resized and moved between physical devices easily. Physical devices can be added and removed with relative ease. LVM managed volumes can also have sensible names like “database” or “home” rather then somewhat cryptic “sda” or “hda” device names.

As shown in above figure,

  • Devices are designated as Physical Volume
  • One or more physical volumes are used to create volume group
  • Physical volumes are defined with Physical extents of a fixed size
  • Logical volumes are created on volume group and are composed of physical extents
  • File system may be created on Logical Volume

Creating Logical Volume

Creating Logical volume is a 6 step process

1) Create a partitions of the type Linux LVM. The code for this is 8e. For details about creating partitions check the previous post Creating Partition and Filesystem in Linux.

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         123      987966   8e  Linux LVM
/dev/sdb2             124         246      987997+  8e  Linux LVM
/dev/sdb3             247         369      987997+  8e  Linux LVM
/dev/sdb4             370         652     2273197+   5  Extended
/dev/sdb5             370         492      987966   8e  Linux LVM
/dev/sdb6             493         615      987966   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

run partprobe command for kernel to read the partition table

[root@localhost ~]# partprobe
Warning: Unable to open /dev/hdc read-write (Read-only file system).  /dev/hdc has been opened read-only.
[root@localhost ~]# cat /proc/partitions
major minor  #blocks  name

8     0   10485760 sda
8     1     104391 sda1
8     2    3068415 sda2
8     3    3068415 sda3
8     4          1 sda4
8     5    1020096 sda5
8     6    1020096 sda6
8     7     514048 sda7
8     8     987966 sda8
8    16    5242880 sdb
8    17     987966 sdb1
8    18     987997 sdb2
8    19     987997 sdb3
8    20          1 sdb4
8    21     987966 sdb5
8    22     987966 sdb6

2) Create a phyical volume out of these partitions

[root@localhost ~]# pvcreate /dev/sdb1
Physical volume “/dev/sdb1” successfully created
[root@localhost ~]# pvcreate /dev/sdb2
Wiping software RAID md superblock on /dev/sdb2
Physical volume “/dev/sdb2” successfully created
[root@localhost ~]# pvcreate /dev/sdb3
Wiping software RAID md superblock on /dev/sdb3
Physical volume “/dev/sdb3” successfully created

3) Create a volume group using these physical volumes

[root@localhost ~]# vgcreate vg0 /dev/sdb1 /dev/sdb2
Volume group “vg0” successfully created

4) Create a logical volume from this volume group

[root@localhost ~]# lvcreate -L 512M -n data vg0
Logical volume “data” created

5) Formatting the logical volume

[root@localhost ~]# mkfs.ext3 /dev/vg0/data
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
64 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

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

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

6) Mount the logical volume

[root@localhost ~]# mkdir /vol0
[root@localhost ~]# mount /dev/vg0/data /vol0
[root@localhost ~]# cd /vol0/

[root@localhost vol0]# df -h .
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg0-data  496M   19M  452M   4% /vol0

Hope this helps !!