Logical Volume Manager (LVM) in a nutshell

Posted: May 31, 2010 in FOSS, Systems/Network Administration

LOGICAL VOLUME MANAGER (LVM)
———————————————-

A Logical Volume is a layer of abstraction that allows easy
manipulation of volumes including resizing of filesystems.

LVM allows re-organisation of the file systems across multiple
physical devices.

null

a) Devices are designtated as Physical Volumes.
b) One or more Physical Volumes create a Volume Group.
c) Volume groups are defined with Physical extends of a fixed size.
d) Logical Volumes are created on Volume Groups and are composed
of physical extends
e) File systems may be created on Logical Volumes.

Let us learn first learn how to create Logical Volume Manager.

1 ) First create a physical parition on your Hard Disk which you
want to later convert to a Logical Volume

First create a partition say /dev/sda and toggle the partition
type to ‘8e’ Linux LVM.

The output on of the fdisk /dev/sda on my machine is
provided below.

—————————————————————————–
[root@zion ~]# fdisk /dev/sda

The number of cylinders for this disk is set to 30401.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): n
First cylinder (25111-30401, default 25111):
Using default value 25111
Last cylinder or +size or +sizeM or +sizeK (25111-30401, default 30401): +500M

Command (m for help): t
Partition number (1-12): 11
Hex code (type L to list codes): L

0 Empty 1e Hidden W95 FAT1 80 Old Minix bf Solaris
1 FAT12 24 NEC DOS 81 Minix / old Lin c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 82 Linux swap / So c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 83 Linux c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 84 OS/2 hidden C: c7 Syrinx
5 Extended 41 PPC PReP Boot 85 Linux extended da Non-FS data
6 FAT16 42 SFS 86 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS 4d QNX4.x 87 NTFS volume set de Dell Utility
8 AIX 4e QNX4.x 2nd part 88 Linux plaintext df BootIt
9 AIX bootable 4f QNX4.x 3rd part 8e Linux LVM e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 93 Amoeba e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 94 Amoeba BBT e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M 9f BSD/OS eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi ee EFI GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a5 FreeBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a6 OpenBSD f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a7 NeXTSTEP f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a8 Darwin UFS f4 SpeedStor
14 Hidden FAT16 ❤ 61 SpeedStor a9 NetBSD f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys ab Darwin boot fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
Hex code (type L to list codes): 8e
Changed system type of partition 11 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sda: 250.0 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 1275 10241406 83 Linux
/dev/sda2 1276 7649 51199155 83 Linux
/dev/sda3 7650 14023 51199155 83 Linux
/dev/sda4 14024 30401 131556285 5 Extended
/dev/sda5 14024 16573 20482843+ 83 Linux
/dev/sda6 16574 19123 20482843+ 83 Linux
/dev/sda7 19124 21673 20482843+ 83 Linux
/dev/sda8 21674 23585 15358108+ 83 Linux
/dev/sda9 23586 24350 6144831 82 Linux swap / Solaris
/dev/sda10 24351 24987 5116671 83 Linux
/dev/sda11 24988 25110 987966 8e Linux LVM

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
[root@zion ~]# partprobe -s
/dev/sda: msdos partitions 1 2 3 4
[root@zion ~]#
——————————————————————————————-

2) Now Create the Physical Volumes with the ‘pvcreate’ command.

—————————————————————-
[root@zion ~]# pvcreate /dev/sda11
Physical volume “/dev/sda11” successfully created
[root@zion ~]# pvdisplay
“/dev/sda11” is a new physical volume of “964.81 MB”
— NEW Physical volume —
PV Name /dev/sda11
VG Name
PV Size 964.81 MB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID xTmbTD-KKrr-aJSd-yrWF-EpMN-ks0s-tZUewb
[root@zion ~]#
——————————————————————

3) Now create the Volume Group vg0 with the ‘vgcreate’ command.

————————————————————
[root@zion ~]# vgcreate vg0 /dev/sda11
Volume group “vg0” successfully created
———————————————————–

Now view the volume with the ‘vgdisplay’ command.

—————————————————————
[root@zion ~]# vgdisplay
— Volume group —
VG Name vg0
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 964.00 MB
PE Size 4.00 MB
Total PE 241
Alloc PE / Size 0 / 0
Free PE / Size 241 / 964.00 MB
VG UUID 3CJdMi-JtLM-2Pff-8hMS-e0oR-hj3X-IekASK
—————————————————————-

3) Now create the Logical Volume inside the Volume group.

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

4) Now format the file system as ext3 with the mke2fs file system
with the -j flag which basically adds journalling to the ext2
file system

ext2 + Journalling — > ext3

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

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

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

5) Now view the Logical Volume size with ‘lvdisplay’

[root@zion ~]# lvdisplay
— Logical volume —
LV Name /dev/vg0/data
VG Name vg0
LV UUID dQxJZu-EjMK-IVJr-zNUJ-dp36-eGWq-3fq0kn
LV Write Access read/write
LV Status available
# open 0
LV Size 500.00 MB
Current LE 125
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:0
[root@zion ~]#

Once we have learned how to create Logical volumes, let’s now learn
how to extend and reduce the size of Logical Volume

Extending (Extending the current 500MB logical volume by 250 MB and
——————————————————————————————-
making it 750MB).
————————–

—————————————————————————————————————

1) First see the size of the Logical Volume with the ‘lvdisplay’ command.

[root@zion ~]# lvdisplay
— Logical volume —
LV Name /dev/vg0/data
VG Name vg0
LV UUID dQxJZu-EjMK-IVJr-zNUJ-dp36-eGWq-3fq0kn
LV Write Access read/write
LV Status available
# open 0
LV Size 500.00 MB
Current LE 125
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:0

2) Now extend the Logical Volume to 750 MB from 500 MB with the lvextend command as shown below.

[root@zion ~]# lvextend -L 750M /dev/vg0/data
Rounding up size to full physical extent 752.00 MB
Extending logical volume data to 752.00 MB
Logical volume data successfully resized
[root@zion ~]#

3) Now view the extended Logical Volume with the ‘lvdisplay’ command.

[root@zion ~]# lvdisplay
— Logical volume —
LV Name /dev/vg0/data
VG Name vg0
LV UUID dQxJZu-EjMK-IVJr-zNUJ-dp36-eGWq-3fq0kn
LV Write Access read/write
LV Status available
# open 0
LV Size 752.00 MB
Current LE 188
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:0
[root@zion ~]#

4) Now use the ‘resize2fs’ command to grow the mounted filesystem
to 750 MB from 500 MB.

[root@zion ~]# resize2fs -p /dev/vg0/data
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg0/data to 770048 (1k) blocks.
Begin pass 1 (max = 31)
Extending the inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/vg0/data is now 770048 blocks long.
[root@zion ~]#

5) Once this is done let us mount the /dev/vg0/data LVM on to the /data
directory as follows.

[root@zion ~]# mkdir /data
[root@zion ~]# mount /dev/vg0/data /data/
[root@zion ~]# mount
/dev/sda8 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda10 on /tmp type ext3 (rw)
/dev/sda7 on /var type ext3 (rw,acl)
/dev/sda5 on /opt type ext3 (rw)
/dev/sda3 on /home type ext3 (rw)
/dev/sda2 on /usr type ext3 (rw)
/dev/sda6 on /usr/local type ext3 (rw)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)
/dev/mapper/vg0-data on /data type ext3 (rw)

6) Also add the entry in /etc/fstab so that LVM is automouted
on reboot.

We can now view the LVM as /dev/mapper/vg0-data mounted
on the /data directory.

[root@zion ~]# fdisk -l

Disk /dev/sda: 250.0 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 1275 10241406 83 Linux
/dev/sda2 1276 7649 51199155 83 Linux
/dev/sda3 7650 14023 51199155 83 Linux
/dev/sda4 14024 30401 131556285 5 Extended
/dev/sda5 14024 16573 20482843+ 83 Linux
/dev/sda6 16574 19123 20482843+ 83 Linux
/dev/sda7 19124 21673 20482843+ 83 Linux
/dev/sda8 21674 23585 15358108+ 83 Linux
/dev/sda9 23586 24350 6144831 82 Linux swap / Solaris
/dev/sda10 24351 24987 5116671 83 Linux
/dev/sda11 24988 25110 987966 8e Linux LVM

[root@zion ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda8 15G 1.2G 13G 9% /
/dev/sda10 4.8G 412M 4.1G 9% /tmp
/dev/sda7 19G 677M 18G 4% /var
/dev/sda5 19G 248M 18G 2% /opt
/dev/sda3 48G 4.1G 41G 10% /home
/dev/sda2 48G 6.1G 39G 14% /usr
/dev/sda6 19G 173M 18G 1% /usr/local
/dev/sda1 9.5G 189M 8.8G 3% /boot
tmpfs 1.5G 0 1.5G 0% /dev/shm
/dev/mapper/vg0-data 729M 11M 681M 2% /data
[root@zion ~]#

Reducing a the Size of a Logical Volume Manager
—————————————————————

Now let us learn how to reduce the size of the LVM with the
‘lvreduce’ command

The procedure for reducing the size of an already mounted LVM using the ‘lvreduce’ command is slightly different from that of the ‘lvextend’ command.

Reducing the size of Logical Volumes (Reducing 750 MB to 500 MB)
————————————————————————————–

1) First check the size of the /dev/mapper/vg0-data LVM using the
df command on the mount point /data

[root@zion ~]# df /data/
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg0-data 745943 10790 696651 2% /data
[root@zion ~]#

2) Now unmount the parition

[root@zion ~]# umount /data

Check if the partition is unmounted.

[root@zion ~]# mount
/dev/sda8 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda10 on /tmp type ext3 (rw)
/dev/sda7 on /var type ext3 (rw,acl)
/dev/sda5 on /opt type ext3 (rw)
/dev/sda3 on /home type ext3 (rw)
/dev/sda2 on /usr type ext3 (rw)
/dev/sda6 on /usr/local type ext3 (rw)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)

3) The difference between the procedures of ‘lvmextend’ and
‘lvmreduce’ apart from first unmounting the partition
to be reduced, is the use of the ‘e2fsck’ command which
is used to check a Linux second extended file system (ext2fs).
E2fsck also supports ext2 filesystems containing a journal,
which are also sometimes known as ext3 filesystems, by first
applying the journal to the filesystem before continuing with
normal e2fsck processing.

[root@zion ~]# e2fsck -f /dev/vg0/data
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg0/data: 11/191008 files (9.1% non-contiguous), 34895/770048 blocks
[root@zion ~]#

4) Reize the filesystem size using resize2fs from 750MB to 500 MB

[root@zion ~]# resize2fs /dev/vg0/data 500M
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg0/data to 512000 (1k) blocks.
The filesystem on /dev/vg0/data is now 512000 blocks long.

5) Use the ‘lvreduce’ command to reduce the size of the LVM
from 750 MB to 500 MB.

[root@zion ~]# lvreduce -L 500M /dev/vg0/data
WARNING: Reducing active logical volume to 500.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce data? [y/n]: y
Reducing logical volume data to 500.00 MB
Logical volume data successfully resized
[root@zion ~]#

6) Now display the new size of the Logical Volume.

[root@zion ~]# lvdisplay
— Logical volume —
LV Name /dev/vg0/data
VG Name vg0
LV UUID dQxJZu-EjMK-IVJr-zNUJ-dp36-eGWq-3fq0kn
LV Write Access read/write
LV Status available
# open 0
LV Size 500.00 MB
Current LE 125
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:0
[root@zion ~]#

Hope you found the writeup helpful.

Advertisements
Comments
  1. pravink.22 says:

    Thxs buddy.. !!!!
    Check below mentioned url, very easy to understand
    http://www.redhatlinux.info/2010/11/lvm-logical-volume-manager.html

  2. vivekvc says:

    You are most welcome Pravink. Most of the technical entries on my blog are created with the intention of forming a source of future reference for myself as well as being useful for others.

    Do keep visiting and offer your invaluable feedbacks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s