LVM commands and brief reference

Common commands for using LVM and some references. Some are harmless some are harmful: know what you type.

LVM basics        
LVM usage requires few different steps:        

Creation of physical volumes (PV) (it just tags a device or partition to be used as PV):      
pvcreate /dev/sda1 /dev/sdb2        

Volume Group (VG) creation (it uses one or more PV to create a VG. Consider a VG as a virtual device that can grow with more PV at any time and has to be splitted in different partitions (Logical Volumes)). Here is named mygroup:        
vgcreate mygroup /dev/sda1 /dev/sdb2        

Creation of a Logical Volume (LV) named myvolume inside the previously created VG mygroup of 25 Gb (consider Logical Volumes as virtual partitions, that can be formatted and mounted as normal partitions and can be extended or reduced with LV commands) :        
lvcreate -L 25G -n myvolume mygroup        

Show LVM information      
To show info about local Physical Volumes:        

To show info about Volume Groups:        

To show info about Logical Volumes        

Growing and reducing        
To add a PV to an existing Volume Group ("make the virtual disk bigger") use the command vgextend, here the new PV /dev/hdb1 is added to existing VG mygroup: vgextend mygroup /dev/hdb1      

To extend a LV with no data:      
lvextend -L +1G /dev/mygroup/myvolume      

To extend a LV formatted in ext2 or ext3 and with data (first enlarge LV then enlarge FS) - Here myvolume is extended by 1 Gb:      
umount /mnt      
lvextend -L +1G /dev/mygroup/myvolume      
resize2fs /dev/mygroup/myvolume mount /mnt      

Alternatively instead of using resize2fs you can use ext2online for enlarging a mounted file systems. So the previous example becomes:      
lvextend -L +1G /dev/mygroup/myvolume      
ext2online /dev/mygroup/myvolume      

To reduce a LV formatted in ext2 or ext3 and with data (first reduce FS then reduce LV) - Here myvolume has size set to 5 Gb:      
umount /mnt      
resize2fs /dev/mygroup/myvolume 5G      
lvreduce -L 5G /dev/mygroup/myvolume      
mount /mnt

Some internals      
Cache file with filters for device names:        

Directory of automatic backup of last VG metadata:        

Directory ot automatic archive of VG metadata:        

Show lvm configuration:        
lvm dumpconfig         

Scans local devices to look for Logical Volumes:        

To use a device ( ex: /dev/sda as PV instead of a partition (ex: /dev/sda1) remember to wipe out it's boot sector otherwise unexpected behaviours may occur:      
dd if=/dev/zero of=/dev/sda bs=512 count=1        

To use a device or a partition as PV it's suggested (not required) to tag it with the proper type. You can use fdisk and then tag the partition or device ID with type LVM (82):      
fdisk /dev/sda - type t - enter 8e      

Clustered Logical Volume Manager (CLVM)        
CLVM is an extension to LVM2 for managing LV information  in a cluster.        
A clustered Volume Group should be used on storage used by different nodes of a RedHat Cluster.        
In /etc/lvm/lvm.conf a clustered locking type must be defined:        
locking_type = 3        
locking_library = /lib/
locking_type = 2        

Alternatively type:        
lvmconf --enable-cluster      
To create a clustered volume group (here share01, using PV /dev/sdc1): vgcreate share01 -c y /dev/sdc1

Privacy Policy