Linux LVM howto, Creation, expansion and reduction of volumes

I will show how to create, expand and reduce LVM volumes, all these actions have been performed with RedHat 6 LVM directly from the console. I think it goes without saying, but it is advisable to make a backup of the original disks.

The first thing is to understand the structure that follows LVM works with 3 types of elements:

  • Volume groups, all related commands that begin with vg*, a volume group can contain one or more logical volumes. And it is composed of physical volumes.
  • Logical volumes, all related commands begin with lv*, a logical volume resides within a volume group.
  • Physical volumes, in this case all commands start with pv*, a volume group are composed of one or more physical volumes.

There are a number of commands to manage LVM volumes:

Source   
[root@tester1 ~]# lvm
lvm> help
Available lvm commands:
Use 'lvm help <command></command>' for more information
dumpconfig Dump active configuration
formats List available metadata formats
help Display help for commands
lvchange Change the attributes of logical volume(s)
lvconvert Change logical volume layout
lvcreate Create a logical volume
lvdisplay Display information about a logical volume
lvextend Add space to a logical volume
lvmchange With the device mapper, this is obsolete and does nothing.
lvmdiskscan List devices that may be used as physical volumes
lvmsadc Collect activity data
lvmsar Create activity report
lvreduce Reduce the size of a logical volume
lvremove Remove logical volume(s) from the system
lvrename Rename a logical volume
lvresize Resize a logical volume
lvs Display information about logical volumes
lvscan List all logical volumes in all volume groups
pvchange Change attributes of physical volume(s)
pvresize Resize physical volume(s)
pvck Check the consistency of physical volume(s)
pvcreate Initialize physical volume(s) for use by LVM
pvdata Display the on-disk metadata for physical volume(s)
pvdisplay Display various attributes of physical volume(s)
pvmove Move extents from one physical volume to another
pvremove Remove LVM label(s) from physical volume(s)
pvs Display information about physical volumes
pvscan List all physical volumes
segtypes List available segment types
vgcfgbackup Backup volume group configuration(s)
vgcfgrestore Restore volume group configuration
vgchange Change volume group attributes
vgck Check the consistency of volume group(s)
vgconvert Change volume group metadata format
vgcreate Create a volume group
vgdisplay Display volume group information
vgexport Unregister volume group(s) from the system
vgextend Add physical volumes to a volume group
vgimport Register exported volume group with system
vgmerge Merge volume groups
vgmknodes Create the special files for volume group devices in /dev
vgreduce Remove physical volume(s) from a volume group
vgremove Remove volume group(s)
vgrename Rename a volume group
vgs Display information about volume groups
vgscan Search for all volume groups
vgsplit Move physical volumes into a new or existing volume group
version Display software and driver version information

To work with the examples, I connected to the machine 2 HD 100Mb each.

Creating volume groups and logical volume with one physical disk

Initially we have only one volume group with 2 volumes:

Source   
lvm> vgs
VG #PV #LV #SN Attr VSize VFree
vg_tester1 1 2 0 wz--n- 7,51g 0
lvm> lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lv_root vg_tester1 -wi-ao 6,51g
lv_swap vg_tester1 -wi-ao 1,00g
lvm> lvscan
ACTIVE '/dev/vg_tester1/lv_root' [6,51 GiB] inherit
ACTIVE '/dev/vg_tester1/lv_swap' [1,00 GiB] inherit

We can see the disks that are available with:

Source   
lvm> lvmdiskscan
/dev/ram0 [ 16,00 MiB]
/dev/root [ 6,51 GiB]
/dev/ram1 [ 16,00 MiB]
/dev/sda1 [ 500,00 MiB]
/dev/vg_tester1/lv_swap [ 1,00 GiB]
/dev/ram2 [ 16,00 MiB]
/dev/sda2 [ 7,51 GiB] LVM physical volume
/dev/ram3 [ 16,00 MiB]
/dev/ram4 [ 16,00 MiB]
/dev/ram5 [ 16,00 MiB]
/dev/ram6 [ 16,00 MiB]
/dev/ram7 [ 16,00 MiB]
/dev/ram8 [ 16,00 MiB]
/dev/ram9 [ 16,00 MiB]
/dev/ram10 [ 16,00 MiB]
/dev/ram11 [ 16,00 MiB]
/dev/ram12 [ 16,00 MiB]
/dev/ram13 [ 16,00 MiB]
/dev/ram14 [ 16,00 MiB]
/dev/ram15 [ 16,00 MiB]
/dev/sdb [ 102,40 MiB]
/dev/sdc [ 102,40 MiB]
4 disks
17 partitions
0 LVM physical volume whole disks
1 LVM physical volume

You can see that /dev/sda and /dev/sdb are available for use by LVM, create a physical volume for use by LVM:

Source   
lvm> pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created

And then a volume group “test” with the physical volume.

Source   
lvm> vgcreate vgtest /dev/sdb
Volume group "vgtest" successfully created

Now we create a logical volume “lv_1_vgtest” within the volume group “vgtest”:

Source   
lvm> lvcreate -L 100M -n lv_1_vgtest vgtest
Logical volume "lv_1_vgtest" created

List volume groups:

Source   
lvm> vgs
VG #PV #LV #SN Attr VSize VFree
vg_tester1 1 2 0 wz--n- 7,51g 0
vgtest 1 1 0 wz--n- 100,00m 0

And the logical volumes (the second command displays the full path of the newly created device /dev/*) with the association to the volume group:

Source   
lvm> lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lv_root vg_tester1 -wi-ao 6,51g
lv_swap vg_tester1 -wi-ao 1,00g
lv_1_vgtest vgtest -wi-a- 100,00m
lvm> lvscan
ACTIVE '/dev/vg_tester1/lv_root' [6,51 GiB] inherit
ACTIVE '/dev/vg_tester1/lv_swap' [1,00 GiB] inherit
ACTIVE '/dev/vgtest/lv_1_vgtest' [100,00 MiB] inherit

We can see the physical volumes and its association with the volume groups:

Source   
lvm> pvscan
PV /dev/sdb VG vgtest lvm2 [100,00 MiB / 0 free]
PV /dev/sda2 VG vg_tester1 lvm2 [7,51 GiB / 0 free]
Total: 2 [7,61 GiB] / in use: 2 [7,61 GiB] / in no VG: 0 [0 ]

Ok now we have a logical volume /dev/vgtest/lv_1_vgtest with 100Mb of space unformatted.

We format and mounted:

Source   
[root@tester1 ~]# mkfs.ext4 /dev/vgtest/lv_1_vgtest
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Writing inode tables: done
This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@tester1 ~]# mount /dev/vgtest/lv_1_vgtest /mnt
[root@tester1 ~]# df -h
S.ficheros Size Used Avail Use% Montado en
/dev/mapper/vg_tester1-lv_root
6,5G 679M 5,5G 11% /
tmpfs 250M 0 250M 0% /dev/shm
/dev/sda1 485M 27M 433M 6% /boot
/dev/mapper/vgtest-lv_1_vgtest
97M 5,6M 87M 7% /mnt

Extension of the volume group and logical volume 1 more physical disk.

Suppose we have out of space and give us another disk 100Mb, we want /dev/mapper/vgtest-lv_1_vgtest have finally 200Mb of space.

For this we have to do 4 things:

  • Create a new physical volume /dev/sdc that we already have connected.
  • Expand the volume group “vgtest” adding a physical volume 100Mb /dev/sdc.
  • Extend the logical volume “/dev/mapper/vgtest-lv_1_vgtest” from 100Mb to 200Mb.
  • Resize the file system that was created in “/dev/mapper/vgtest-lv_1_vgtest” to take advantage of 200Mb.

Let’s do it, we created the new physical volume (use /dev/sdc):

Source   
lvm> lvmdiskscan
/dev/ram0 [ 16,00 MiB]
/dev/root [ 6,51 GiB]
/dev/ram1 [ 16,00 MiB]
/dev/sda1 [ 500,00 MiB]
/dev/vg_tester1/lv_swap [ 1,00 GiB]
/dev/ram2 [ 16,00 MiB]
/dev/sda2 [ 7,51 GiB] LVM physical volume
/dev/vgtest/lv_1_vgtest [ 100,00 MiB]
/dev/ram3 [ 16,00 MiB]
/dev/ram4 [ 16,00 MiB]
/dev/ram5 [ 16,00 MiB]
/dev/ram6 [ 16,00 MiB]
/dev/ram7 [ 16,00 MiB]
/dev/ram8 [ 16,00 MiB]
/dev/ram9 [ 16,00 MiB]
/dev/ram10 [ 16,00 MiB]
/dev/ram11 [ 16,00 MiB]
/dev/ram12 [ 16,00 MiB]
/dev/ram13 [ 16,00 MiB]
/dev/ram14 [ 16,00 MiB]
/dev/ram15 [ 16,00 MiB]
/dev/sdb [ 102,40 MiB] LVM physical volume
/dev/sdc [ 102,40 MiB]
4 disks
17 partitions
1 LVM physical volume whole disk
1 LVM physical volume
lvm> pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created
lvm> lvmdiskscan
/dev/ram0 [ 16,00 MiB]
/dev/root [ 6,51 GiB]
/dev/ram1 [ 16,00 MiB]
/dev/sda1 [ 500,00 MiB]
/dev/vg_tester1/lv_swap [ 1,00 GiB]
/dev/ram2 [ 16,00 MiB]
/dev/sda2 [ 7,51 GiB] LVM physical volume
/dev/vgtest/lv_1_vgtest [ 100,00 MiB]
/dev/ram3 [ 16,00 MiB]
/dev/ram4 [ 16,00 MiB]
/dev/ram5 [ 16,00 MiB]
/dev/ram6 [ 16,00 MiB]
/dev/ram7 [ 16,00 MiB]
/dev/ram8 [ 16,00 MiB]
/dev/ram9 [ 16,00 MiB]
/dev/ram10 [ 16,00 MiB]
/dev/ram11 [ 16,00 MiB]
/dev/ram12 [ 16,00 MiB]
/dev/ram13 [ 16,00 MiB]
/dev/ram14 [ 16,00 MiB]
/dev/ram15 [ 16,00 MiB]
/dev/sdb [ 102,40 MiB] LVM physical volume
/dev/sdc [ 102,40 MiB] LVM physical volume
3 disks
17 partitions
2 LVM physical volume whole disks
1 LVM physical volume

Extension of the volume group “vgtest”:

Source   
lvm> vgextend vgtest /dev/sdc
Volume group "vgtest" successfully extended
lvm> vgs
VG #PV #LV #SN Attr VSize VFree
vg_tester1 1 2 0 wz--n- 7,51g 0
vgtest 2 1 0 wz--n- 200,00m 100,00m
lvm> lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lv_root vg_tester1 -wi-ao 6,51g
lv_swap vg_tester1 -wi-ao 1,00g
lv_1_vgtest vgtest -wi-ao 100,00m

As you can see the volume group has a size of 200MB but the logical volume remains 100Mb.

Extension of logical volume “lv_1_vgtest” with:

Source   
lvm> lvextend -L200M /dev/vgtest/lv_1_vgtest
Extending logical volume lv_1_vgtest to 200,00 MiB
Logical volume lv_1_vgtest successfully resized
lvm> lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lv_root vg_tester1 -wi-ao 6,51g
lv_swap vg_tester1 -wi-ao 1,00g
lv_1_vgtest vgtest -wi-ao 200,00m

To extend the logical volume has been defined size explicitly, but we could have used all the free disc:

Source   
lvextend -l +100%FREE /dev/vgtest/lv_1_vgtest

Now we only resizing ext4 file system (can be done with the mounted volume):

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

The OS can already see the logical volume 200 Mb:

Source   
[root@tester1 ~]# df -h
S.ficheros Size Used Avail Use% Montado en
/dev/mapper/vg_tester1-lv_root
6,5G 679M 5,5G 11% /
tmpfs 250M 0 250M 0% /dev/shm
/dev/sda1 485M 27M 433M 6% /boot
/dev/mapper/vgtest-lv_1_vgtest
194M 5,6M 179M 3% /mnt

Now let’s add some files on the mount point /mnt is where is mounted /dev/mapper/vgtest-lv_1_vgtest, with the idea of removing some and try to reduce the logical volume without losing data.

Copy a few files in the vg (151M):

Source   
[root@tester1 ~]# cp /usr/lib/* /mnt/ -R
[root@tester1 mnt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_tester1-lv_root
6,5G 679M 5,5G 11% /
tmpfs 250M 0 250M 0% /dev/shm
/dev/sda1 485M 27M 433M 6% /boot
/dev/mapper/vgtest-lv_1_vgtest
194M 158M 27M 86% /mnt
[root@tester1 ~]# cd /mnt/
[root@tester1 mnt]# rm /mnt/locale/ -Rf
[root@tester1 mnt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_tester1-lv_root
6,5G 679M 5,5G 11% /
tmpfs 250M 0 250M 0% /dev/shm
/dev/sda1 485M 27M 433M 6% /boot
/dev/mapper/vgtest-lv_1_vgtest
194M 65M 120M 35% /mnt

Reduction of volume group and logical volume we eliminate one physical disk.

We will reduce the file system 200Mb to 80Mb, you must unmount the volume:

Source   
[root@tester1 ~]# umount /mnt

[root@tester1 ~]# e2fsck -f /dev/vgtest/lv_1_vgtest
e2fsck 1.41.12 (17-May-2010)
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/vgtest/lv_1_vgtest: 2845/49400 files (0.3% non-contiguous), 71042/204800 blocks
[root@tester1 ~]# resize2fs /dev/vgtest/lv_1_vgtest 80M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vgtest/lv_1_vgtest to 81920 (1k) blocks.
The filesystem on /dev/vgtest/lv_1_vgtest is now 81920 blocks long

What has been done is checked before then reduce the volume.

Now we can reduce the logical volume to 90Mb (the file system takes only 80Mb), a message that we can lose data (without saying that a backup is highly recommended) appears:

Source   
lvm> lvreduce -L90M /dev/vgtest/lv_1_vgtest
Rounding up size to full physical extent 92,00 MiB
WARNING: Reducing active logical volume to 92,00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_1_vgtest? [y/n]: y
Reducing logical volume lv_1_vgtest to 92,00 MiB
Logical volume lv_1_vgtest successfully resized

Ok the logical volume has been reduced:

Source   
lvm> lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lv_root vg_tester1 -wi-ao 6,51g
lv_swap vg_tester1 -wi-ao 1,00g
lv_1_vgtest vgtest -wi-a- 92,00m

Is not lost anything:

Source   
[root@tester1 ~]# mount /dev/vgtest/lv_1_vgtest /mnt
[root@tester1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_tester1-lv_root
6,5G 679M 5,5G 11% /
tmpfs 250M 0 250M 0% /dev/shm
/dev/sda1 485M 27M 433M 6% /boot
/dev/mapper/vgtest-lv_1_vgtest
78M 64M 11M 86% /mnt

Now we see the use of the physical disks in each group:

Source   
lvm> pvscan
PV /dev/sdb VG vgtest lvm2 [100,00 MiB / 8,00 MiB free]
PV /dev/sdc VG vgtest lvm2 [100,00 MiB / 100,00 MiB free]
PV /dev/sda2 VG vg_tester1 lvm2 [7,51 GiB / 0 free]
Total: 3 [7,70 GiB] / in use: 3 [7,70 GiB] / in no VG: 0 [0 ]

You can see that /dev/sdc may be released from the group “vgtest” is not in use:

Source   
lvm> vgreduce vgtest /dev/sdc
Removed "/dev/sdc" from volume group "vgtest"
lvm> pvscan
PV /dev/sdb VG vgtest lvm2 [100,00 MiB / 8,00 MiB free]
PV /dev/sda2 VG vg_tester1 lvm2 [7,51 GiB / 0 free]
PV /dev/sdc lvm2 [102,40 MiB]
Total: 3 [7,71 GiB] / in use: 2 [7,61 GiB] / in no VG: 1 [102,40 MiB]

At this time we can eliminate the physical disk:

Source   
lvm> pvremove /dev/sdc
Labels on physical volume "/dev/sdc" successfully wiped
lvm> pvscan
PV /dev/sdb VG vgtest lvm2 [100,00 MiB / 8,00 MiB free]
PV /dev/sda2 VG vg_tester1 lvm2 [7,51 GiB / 0 free]
Total: 2 [7,61 GiB] / in use: 2 [7,61 GiB] / in no VG: 0 [0 ]

Finally the physical /dev/sdc disk can be unassigning from the machine permanently, LVM not used at all.

Leave a Reply