LVM is a Logical Volume Manager for the Linux operating system.
Volume management creates a layer of abstraction over physical storage, allowing you to create logical storage volumes. This provides much greater flexibility in a number of ways than using physical storage directly.
A logical volume provides storage virtualization. With a logical volume, you are not restricted to physical disk sizes. In addition, the hardware storage configuration is hidden from the software so it can be resized and moved without stopping applications or unmounting file systems. This can reduce operational costs.
Logical volume management provides a higher-level view of the disk storage on a computer system than the traditional view of disks and partitions. This gives the system administrator much more flexibility in allocating storage to applications and users.
Storage volumes created under the control of the logical volume manager can be resized and moved around almost at will, although this may need some upgrading of file system tools.
The logical volume manager also allows management of storage volumes in user-defined groups, allowing the system administrator to deal with sensibly named volume groups such as “development” and “sales” rather than physical disk names such as “sda” and “sdb”.
LVM 2 – The latest version of LVM for Linux.
How to install LVM on RHEL/CentOS:
yum install lvm2
How to install LVM on Ubuntu:
sudo apt-get install lvm2
Create LVM step by step:
Step1:
Create PhysicalVolume with the help of pvcreate command.
pvcreate command initializes PhysicalVolume for later use by the Logical Volume Manager (LVM). Each PhysicalVolume can be a disk partition, whole disk, meta device, or loopback file. You can create partition using fdisk command see How to create partition. You must change partition type to Linux LVM id 8e.
Usage: # pvcreate [options] PhysicalVolume [PhysicalVolume…]
Execute below command to create PhysicalVolume in this example /dev/sdc1 is used in your case it may be /dev/sd{a/b or c}{1,2 or 3}
# pvcreate /dev/sdc1 Physical volume "/dev/sdc1" successfully created
Check all PhysicalVolume information by issuing pvdisplay command.
pvdisplay allows you to see the attributes of one or more physical volumes like size, physical extent size, space used for the volume group descriptor area and so on. We can see below that our PhysicalVolume has been created named /dev/sdc1 .
# pvdisplay --- Physical volume --- "/dev/sdc1" is a new physical volume of "203.92 MiB" --- NEW Physical volume --- PV Name /dev/sdc1 VG Name PV Size 203.92 MiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID T3TGDz-4KRd-4fFa-6BID-12zg-LmU2-sUPKJe
Step2:
Now create volume group with the help of vgcreate command.
vgcreate command creates a new volume group called VolumeGroupName using the block special device PhysicalDevicePath. If PhysicalDevicePath was not previously configured for LVM with pvcreate, the device will be initialized with the same default values used with pvcreate.
Execute below command to create VolumeGroup named TestVolGrp including physical device /dev/sdc1.
Usage: vgcreate [options] VolumeGroupName PhysicalDevicePath [PhysicalDevicePath…]
# vgcreate TestVolGrp /dev/sdc1 Volume group "TestVolGrp" successfully created
Check volume group information by issuing vgdisplay command:
vgdisplay allows you to see the attributes of VolumeGroupName (or all volume groups if none is given) with it’s physical and logical volumes and their sizes etc.
# vgdisplay
--- Volume group --- VG Name TestVolGrp 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 200.00 MiB PE Size 4.00 MiB Total PE 50 Alloc PE / Size 0 / 0 Free PE / Size 50 / 200.00 MiB VG UUID eE1Kpx-9ff3-6iW9-GiI9-C1Pg-8LVz-ltE4wg
Step3:
Now create logical volume in a volume group using lvcreate command.
lvcreate command creates a new logical volume in a volume group by allocating logical extents from the free physical extent pool of that volume group. If there
are not enough free physical extents then the volume group can be extended (will see it later) with other physical volumes or by reducing existing logical volumes of this volume group in size (see later). If you specify one or more PhysicalVolumes, allocation of physical extents will be restricted to these volumes.
Execute below command to create logical volume named TestVolGrp, of course you can keep other names what ever you want.
Usage: lvcreate [options] [VolumeGroup{Name|Path} [/ExternalOrigin | Origin | Pool}LogicalVolumeName] [PhysicalVolumePath[:PE[-PE]]…]
# lvcreate -L 200m -n TVG_test TestVolGrp
In above example we have created a logical volume named TestVolGrp with 200 MB of size. where -L is used for defining the size in your case it could be GB or TB check appropriate options for that (-L|–size LogicalVolumeSize[bBsSkKmMgGtTpPeE]}). -n option is used to name the Logical Volume.
Now check logical volume information by issuing lvdisplay command.
lvdisplay allows you to see the attributes of a logical volume like size, read/write status, snapshot information etc.
# lvdisplay
--- Logical volume --- LV Path /dev/TestVolGrp/TVG_test LV Name TVG_test VG Name TestVolGrp LV UUID GVIOF6-iTUi-pq3z-7bWg-B1rX-T7aB-emzz77 LV Write Access read/write LV Creation host, time graphite, 2016-01-22 00:28:13 -0500 LV Status available # open 0 LV Size 200.00 MiB Current LE 50 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2
Now you are able to see LV Path, LV Name, VG Name, LV Size ,etc. information. It means we have created our Logical Volume successfully. Now create filesystem on our newly created Logical volume( TVG_test ) mke2fs is used to create an ext2, ext3, or ext4 filesystem, usually in a disk partition. device is the special file corresponding to the device (e.g /dev/hdXX). blocks-count is the number of blocks on the device. Or you can use mkfs.ext3 or mkfs.ext4 in order to create filesystem on created logical volume. In our example below we have used mkfs.ext4.
# mkfs.ext4 /dev/TestVolGrp/TVG_test
Now you can mount TVG_test Logical Volume by executing below command.
mount -t ext4 /dev/TestVolGrp/TVG_test /mnt
How to extend size of the logical volume
In our scenario we have existing disk of 200MB which is /dev/sdc1 which is part of TVG_test Logical Volume and fully dedicated to that LV. Now we have to add more space to the existing mounted volume /dev/TestVolGrp/TVG_test current size 200M. Task is to add more 200MB to /dev/TestVolGrp/TVG_test.
Now list all physical volumes, volume groups and logical groups to see current status.
# pvs PV VG Fmt Attr PSize PFree /dev/sdc1 TestVolGrp lvm2 a-- 200.00m 0
# vgs VG #PV #LV #SN Attr VSize VFree TestVolGrp 1 1 0 wz--n- 200.00m 0
# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert TVG_test TestVolGrp -wi-a----- 200.00m
Now add one more disk or partition. In this case we are using /dev/sdc2 as additional partition. Please refer How to create partition. You must change partition type to Linux LVM id 8e.
Now add PhysicalVolume for later use by the Logical Volume Manager (LVM). Each PhysicalVolume can be a disk partition, whole disk, meta device, or loopback file.
Run below command, once it created successfully we will see like this:
# pvcreate /dev/sdc2 Physical volume "/dev/sdc2" successfully created
Now check Physical Volumes, you will be able to see new partition added to physical volumes, but yet not associated with any Volume Group(vgs).
# pvs PV VG Fmt Attr PSize PFree /dev/sdc1 TestVolGrp lvm2 a-- 200.00m 0 /dev/sdc2 lvm2 --- 203.95m 203.95m
In order to add /dev/sdc2 partation to TestVolGrp run following command:
vgextend TestVolGrp /dev/sdc2 Volume group "TestVolGrp" successfully extended
Now you can see we have extended our TestVolGrp by adding additional disk. /dev/sdc2 now associated with TestVolGrp.
# pvs PV VG Fmt Attr PSize PFree /dev/sda2 VolGroup lvm2 a-- 19.51g 0 /dev/sdc1 TestVolGrp lvm2 a-- 200.00m 0 /dev/sdc2 TestVolGrp lvm2 a-- 200.00m 0
vgs command will show VG size as 400 M.
# vgs VG #PV #LV #SN Attr VSize VFree TestVolGrp 2 1 0 wz--n- 400.00m 200.00m VolGroup 1 2 0 wz--n- 19.51g 0
Now time to extend our Logical Volume. Run lvextend command to extend the LV (TVG_test). vgextend allows you to add one or more initialized physical volumes to an existing volume group to extend it in size. Moreover, it allows you to re-add a physical
volume that has gone missing previously, due to a transient device failure, without re-initialising it.
lvextend -L +200M /dev/TestVolGrp/TVG_test Size of logical volume TestVolGrp/TVG_test changed from 200.00 MiB (50 extents) to 400.00 MiB (100 extents). Logical volume TVG_test successfully resized
Now run resize2fs program. The resize2fs program will resize ext2, ext3, or ext4 file systems. It can be used to enlarge or shrink an unmounted file system located on device. If the filesystem is mounted, it
can be used to expand the size of the mounted filesystem, assuming the kernel supports on-line resizing.
# resize2fs /dev/TestVolGrp/TVG_test 400M resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/TestVolGrp/TVG_test is mounted on /mnt; on-line resizing required old desc_blocks = 1, new_desc_blocks = 2 Performing an on-line resize of /dev/TestVolGrp/TVG_test to 409600 (1k) blocks. The filesystem on /dev/TestVolGrp/TVG_test is now 409600 blocks long.
It will ask you to run e2fsck -f /dev/TestVolGrp/TVG_test. e2fsck is used to check the ext2/ext3/ext4 family of file systems.
e2fsck -f /dev/TestVolGrp/TVG_test 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/TestVolGrp/TVG_test: 12/51200 files (0.0% non-contiguous), 12110/204800 blocks
Now check the lvs
lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert TVG_test TestVolGrp -wi-a----- 400.00m lv_root VolGroup -wi-ao---- 17.57g lv_swap VolGroup -wi-ao---- 1.94g
Now we can see the changes has done successfully. df command will show disk size as 384MB.
# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/TestVolGrp-TVG_test 384M 2.3M 362M 1% /mnt
How to reduce size of the logical volume
Now we will see how to reduce size of our logical volume from 400 MB to 100 MB. With the help of lvreduce command. lvreduce allows you to reduce the size of a logical volume. Be careful when reducing a logical volume’s size, because data in the reduced part is lost!!!
- In order to reduce the size of the logical volume we need first unmount the partition to be reduced.
# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/TestVolGrp-TVG_test 384M 2.3M 362M 1% /mnt
# umount /mnt/
- Check a Linux ext2/ext3/ext4 file system. Note that in general it is not safe to run e2fsck on mounted filesystems.
# e2fsck -f /dev/TestVolGrp/TVG_test
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/TestVolGrp/TVG_test: 12/102400 files (0.0% non-contiguous), 19334/409600 blocks
- You should therefore ensure that any filesystem on the volume is resized before running lvreduce so that the extents that are to be removed are not in use. -M option will shrink the filesystem to the minimum size.
# resize2fs /dev/TestVolGrp/TVG_test -M 100M
- Now run the lvreduce command which allows you to reduce the size of a logical volume. It will give you a warning whether you want proceed to reduce or not. Press ‘y’ to continue.
# lvreduce -L -300M /dev/TestVolGrp/TVG_test WARNING: Reducing active logical volume to 100.00 MiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce TVG_test? [y/n]: y Size of logical volume TestVolGrp/TVG_test changed from 400.00 MiB (100 extents) to 100.00 MiB (25 extents). Logical volume TVG_test successfully resized
- If you mount the partition it will not reflect the correct changes, you will have to again run resize2fs command to reflect correct changes but without size option.
resize2fs /dev/TestVolGrp/TVG_test
- Now mount the LV again :
mount -t ext4 /dev/TestVolGrp/TVG_test /mnt
-
# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/TestVolGrp-TVG_test 93M 1.6M 87M 2% /mnt
MOST COMMENTED
Uncategorized
Ubuntu 16.04 No desktop only shows background wallpaper
Administration / DNS / Linux
Dig command examples
Virtualization
OpenVz(Kernel Base Open source Virtulization)
Uncategorized
Install Ansible on Linux
Puppet
Configuring puppet4 server agent and puppetdb on ubuntu16.04
Database
Installing postgresql on ubuntu 16.04
Puppet
opensource puppet4 installation on ubuntu16.04