this is obsolete doc -- see http://doc.nethence.com/ instead

LVM2 usage and administration 

 

Configuration 

Note. on Slackware and Redhat 

 

Exclude CDrom from LVM, 

cd /etc/lvm/
cp lvm.conf lvm.conf.dist
vi lvm.conf

change the filter directive to, 

filter = [ "r|/dev/cdrom|" ]

Note. Fo SAN and multipathing, you might also want to exclude sd* and let LVM handle only dm-0. 

 

Create PV/VG/LV/filesystem 

Note. If you are using a partition instead of the whole harddrive for the physical volume, make sure it has type, 

8e  Linux LVM

 

Create a physical volume, a volume group and a logical volume, 

pvcreate /dev/sdb
vgcreate vgdata /dev/sdb
vgdisplay | grep Free
lvcreate -l FREE_PE -n data vgdata
mkfs.ext4 /dev/vgdata/data

 

Add PV & extend VG/LV/filesystem 

Proceed like e.g., 

pvcreate /dev/...
vgextend vgdata /dev/...
vgdisplay | grep Free
lvextend -l +FREE_PE /dev/vgdata/data
resize2fs /dev/vgdata/data

Note. filesystem extention may be be performed on-line (ext4) 

 

Usage 

Display physical volumes, volume groups and logical volumes, 

#pvscan
pvs
#vgdisplay
#vgdisplay -v
vgs
vgs -l

 

Hot-grow the size of the root file system with no reboot 

Make sure it's an ext3, ext4 or XFS file system first, 

mount | grep 'on / '

 

On the same boot disk 

If possible, hot-grow the vdisk size (see if the VM infrastructure you are using allows you do do that on that vdisk) and rescan, 

ls -l /sys/class/scsi_device/
for device in `ls -1 /sys/class/scsi_device/`; do echo 1 > "/sys/class/scsi_device/$device/device/rescan"; echo $device; sleep 1; done

then add a "Linux LVM" partition (change sda3 partition number accordingly), 

fdisk /dev/sda

nevermind the warning, 

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 or after you run partprobe(8) or kpartx(8)

and rescan partitions with, 

partprobe -s
#partx -v -a /dev/sda

and make it join the vg, 

  pvcreate /dev/sda3 

vgs

  vgextend vg_rhel6vm1 /dev/sda3 

 

On a additional disk 

If you cannot, add a vdisk and rescan, 

ls -l /sys/class/scsi_host/
echo "- - -" > /sys/class/scsi_host/host0/scan

and make it join the vg, 

pvcreate /dev/sdb
vgs
vgextend vg_rhel6vm1 /dev/sdb

 

Growing the LV and the filesystem (-r) 

then extend the lv and grow the ext4 filesystem, 

lvs
vgdisplay vg_rhel6vm1 | grep Free
lvextend -l +2559 /dev/vg_rhel6vm1/lv_root
#lvextend -r -l +2559 /dev/vg_rhel6vm1/lv_root

 

Growing the fs seperately 

For ext3/ext4, 

df -h
resize2fs /dev/vg_rhel6vm1/lv_root

 

For XFS, 

xfs_growfs /dev/mapper/vg_rhel7-lv_root

 

Refs about vdisk. 

Increase A VMware Disk Size (VMDK) Formatted As Linux LVM without rebooting: https://ma.ttias.be/increase-a-vmware-disk-size-vmdk-formatted-as-linux-lvm-without-rebooting/ 

Howto extend LVM on VMWare Guest running Linux: http://echenh.blogspot.fr/2010/04/how-to-extend-lvm-on-vmware-guest-os.html 

Can I increase the size of a Linux VM's .vmdk disk without a reboot?: https://communities.vmware.com/thread/212019?start=15&tstart=0 

 

Refs. about xfs. 

http://ask.xmodulo.com/expand-xfs-file-system.html 

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Storage_Administration_Guide/xfsgrow.html 

https://docs.oracle.com/cd/E37670_01/E37355/html/ol_grow_xfs.html 

 

Reduce the size of a filesystem and logical volume 

Update, just use the lvreduce -r feature to do both at once. 

 

First, reduce the filesystem size, 

fsck -f /dev/mapper/vg_data-lv_data

...144315055/199750656 blocks 

#lvreduce -r -L 650G /dev/vg_data/lv_data
resize2fs /dev/mapper/vg_data-lv_data 650G
...170393600 (4k) blocks.

 

Then reduce the LV size, 

echo $((199750656 * 4096))
818178686976
lvdisplay --units B /dev/vg_data/lv_data
lvreduce -L 650G /dev/vg_data/lv_data
echo $((170393600 * 4096))
697932185600
lvdisplay --units B /dev/vg_data/lv_data

 

Refs. 

https://ext4.wiki.kernel.org/index.php/Ext4_Howto 

http://www.systutorials.com/124416/shrinking-a-ext4-file-system-on-lvm-in-linux/ 

https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout 

http://serverfault.com/questions/604893/ext3-ext4-physical-block-size-view 

https://www.thomas-krenn.com/en/wiki/Partition_Alignment 

http://seriousbirder.com/blogs/lvreduce-ext4-example/ 

http://www.microhowto.info/howto/reduce_the_size_of_an_lvm_logical_volume.html 

https://blog.shadypixel.com/how-to-shrink-an-lvm-volume-safely/ 

 

Using LVM2 pvmove to relocate extents from a physical volume to another one 

Introduction 

This should work on any GNU/Linux that has LVM version 2. As for on-line resizing, you should have an ext3 file system, at least (grow only, not shrink). In this example I've got two drives, a 3TB (/dev/sdb) and a 2TB (/dev/sdc) hard disk. The 3TB is free to be erased and will be used as a new physical volume. The 2TB drive has a Linux LVM DOS partition on it hence an LVM physical volume on /dev/sdc1. I need to move the vgdata volume group that it contains, to the larger 3TB disk. In brief, I need to move the physical extents from /dev/sdc1 to /dev/sdb. 

 

Move the extents to the new and larger disk 

Create the new physical volume and add it to the volume group, 

pvcreate /dev/sdb
vgextend vgdata /dev/sdb

you can now proceed with 'pvmove', 

pvmove -v -i 5 /dev/sdc1 /dev/sdb

Note. With the "-v -i 5" command arguments, prmove prints some information at the beginning and one line to print the percentage every 5 seconds. 

Note. pvmove reduces the size of the volume group on sdc and increases it on sdb. 

Note. Once a pvmove is initiated, you can stop (^C) and get back to the process simply by typing (other command line arguments will be ignored anyway), 

#pvmove

 

Ok, all done? Then remove /dev/sdc from the volume group, 

vgreduce vgdata /dev/sdc
#or vgreduce --all vgdata

now check the physical volumes again, 

pvs

eventually wipe-out the physical volume from the relying disk, 

pvremove /dev/sdc1

 

Extend the logical volume and check again, 

lvextend --extents +100%FREE /dev/vgdata/lvdata

now extend the file system that's on it (on-line resizing for growing works for ext3 and ext4 at least), 

df -h
fsadm -l resize /dev/vgdata/lvdata

note. The -l option seems to be required for a logical volume, eventhough it's already resized.