Linux Services Organization

Our goal, introduce Linux services to the enterprise world.
Contact us in contact@linuxsv.org

Linux Services Organization : Linux LVM Linux Server

Logical Volume Manager (LVM) is a tool that allows management of logical partitions. These logical partitions are created by physical volumes PV formed by a collection of LVM partitions created with fdisk command. The physical partitions space can be assigned to a volume group VG, and the volume group can be divided into logical volumes LV where the filesystem can be created and mounted on the system.

The main advantage of LVM is the flexibility of the volume groups VG and logical volume partitions LV. When a logical volume partition reaches their full capacity, free space from the volume group can be added to the logical volume partition to increase the size of the partition. In the same way when a volume group reaches the full capacity, new physical partitions created for new disks can we added to the volume group in order to increase the volume group storage capacity.

LVM Creation

* The first step in order to create an LV is to create an LVM partition (code '8e') with fdisk command:

$ fdisk /dev/sdb

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-130, default 130): +1000M

Command (m for help): t
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

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

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

$ partprobe


Verify the result

$ fdisk -l

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device      Boot      Start      End      Blocks      Id      System
/dev/sdb1                      1      123      987966      8e      Linux LVM

Now /dev/sdb1 is a 1000M LVM partition ready to be used by LVM

* Second step is create a physical volume PV on /dev/sdb1 with pvcreate command:

$ pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created

* Third step is create a volume group VG from the PV create on /dev/sdb1 with vgcreate command :

$ vgcreate VolGroup /dev/sdb1
Volume group "VolGroup" successfully created

* Once created the VG the LV partition can be create using lvcreate command :

$ lvcreate -L200M -n VolGroupMnt VolGroup
Logical volume "VolGroupMnt" created


It creates a LV named VolGroupMnt stored on VG Volgroup with 200M size. The LV is a final partition where a filesystem can be created and can be mounted :

$ mkfs.ext4 /dev/VolGroup/VolGroupMnt

mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729

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

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




$ mount /dev/VolGroup/VolGroupMnt /mnt
$ df -h
...
/dev/mapper/VolGroup-VolGroupMnt
194M 5.6M 179M 4% /mnt

Now a LVM 200M ext4 partition is mounted on /mnt

LVM Resizing

LV Extension

As said before the main advantage of LVM is the property of increase/decrease LVs and VGs storage capacity. As example lets increase the size of LV VolGroupMnt in 200M from VG VolGroup so the final size must be 400M. First lets be sure that VG VolGroup has 200M free with the command vgdisplay :

$ vgdisplay VolGroup

--- Volume group ---
VG Name                      VolGroup
System ID                     
Format                                    lvm2
Metadata Areas                  1
Metadata Sequ No             2
VG Access                           read/write
VG Status                             resizable
MAX LV                                0
Cur LV                                  1
Open LV                               1
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                 50 / 200.00 MB
Free PE / Size                 191 / 764.00 MB
VG UUID                      sF2fFq-gzMh-Ycld-3mmU-9x4e-U0tO-2P7x5u


As can be seen there are 764M free on VG VolGroup so lets to increase the size of LV VolGroupMnt in 200M with lvextend command :

$ lvextend -L+200M /dev/VolGroup/VolGroupMnt

Extending logical volume VolGroupMnt to 400.00 MB
Logical volume VolGroupMnt successfully resized
Note : in this case '200M' is the quantity to be extended

$ df -h
...
/dev/mapper/VolGroup-VolGroupMnt
194M 5.6M 179M 4% /mnt


The LV size has been resized to 400M but the filesystem mounted on /mnt has only 200M. The filesystem must be increased with resize2fs command :

$ resize2fs /dev/VolGroup/VolGroupMnt

resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup/VolGroupMnt is mounted on /mnt; on-line resizing required
Performing an on-line resize of /dev/VolGroup/VolGroupMnt to 409600 (1k) blocks.
The filesystem on /dev/VolGroup/VolGroupMnt is now 409600 blocks long.
Online filesystem resize only works for journaled filesystem like ext4. For non-journaled filesystem the partition which contains the filesystem to be resized must be umounted first.

$ df -h
...
/dev/mapper/VolGroup-VolGroupMnt
388M 6.3M 362M 2% /mnt


Now the LV VolGroupMnt mounted on /mnt has a 400M ext4 filesystem.

VG Extension

The volume group size can also be extended adding new physical volumes to the volgroup using the command vgextend. Imagine that a new disk sdc has been added to the system and you need to extend the VG VolGroup :

1.- The first step is create a LVM partition '8e' with fdisk (/dev/sdc1)

$ fdisk /dev/sdc

2.- Create a physical volume on /dev/sdc1

$ pvcreate /dev/sdc1

3.- Add PV /dev/sdc1 to the VG VolGroup

$ vgextend VolGroup /dev/sdc1

Now VolGroup has been extended with the size of the PV /dev/sdc1 added

LV Reduction

This is a very dangerous operation because in the LV reduction data can be lost. These are the steps that must be followed in order to reduce an LV :

1.- Backup all data from LV in another partition
2.- Umount the LV partition

$ umount /dev/VolGroup/VolGroupMnt

3.- Check the filesystem to be reduced

$ e2fsck -f /dev/VolGroup/VolGroupMnt

4.- Reduce the filesystem on LV partition

$ resize2fs /dev/VolGroup/VolGroupMnt 200M

resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/VolGroup/VolGroupMnt to 204800 (1k) blocks.
The filesystem on /dev/VolGroup/VolGroupMnt is now 204800 blocks long.

Note : in this case the '200M' is the filesystem final size

5.- Reduce the LV partition

$ lvreduce -L200 /dev/VolGroup/VolGroupMnt

WARNING: Reducing active logical volume to 200.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce VolGroupMnt? [y/n]: y
Reducing logical volume VolGroupMnt to 200.00 MB
Logical volume VolGroupMnt successfully resized

Note : in this case the '200M' is the LV final size

LV VolGroupMnt has 200M of size with a 200M ext4 filesystem created on it. Note that in the case of LV reduction first the filesystem is reduced and then the LV partition. In case of LV extension is the opposite, first the LV partition is extended and then the filesystem.

Questions

1.- LVM can be used over 'Linux' disk partitions (true/false)

2.- In order to increase a LV partition first the LV must be increased and then the filesystem (true/false)

3.- When resizing the filesystem on LV it is not necessary to umount the LV partition first (true/false)

4.- Which command must be used in order to create a PV on /dev/sdc1 LVM partition ?

5.- Which command must be used in order to extended VG VolGroup00 with the PV /dev/sdd1 ?

6.- Which command shows all PVs status ?

7.- Which command shows all VGs status ?

8.- Which command shows all LVs status ?

9.- Which of the following commands can be used in order to extend in 300M the LV /dev/VolGroup/VolGroupRoot ?
A - lvextend -L300M /dev/VolGroup/VolGroupRoot
B - lvextend -L+300M /dev/VolGroup/VolGroupRoot
C - Both of them
D - None of them

10.- Which of the commands can be used to see the VG VolGroup status ?
A - vgdisplay
B - vgdisplay VolGroup
C - Both of them
D - None of them

Labs

1.- Create a new VG called VolGroup01 with 200M storage capacity. Create an 100 M LV called VolGroup01Tmp from VG VolGroup01, create a ext4 filesystem on it and mount it on /mnt/tmp.

2.- Extend in 400M VolGroup01Tmp in order to reach 500M of total size. First you must extend at least 300M VG VolGroup01 and then extend LV VolGroup01.

3.- Make sure LV VolGroup01Tmp is mounted at boot.

-- This page is part of Linux Server online tutorial --