With LVM we can manage our partition of a easy and flexible way. For this guide I’ll use three disk of 340 GB, 90 GB and 160 GB. With this basic guide We’ll create a simple group, add or remove partitions from a group and extend or reduce the size of a LVM device.
- Install LVM support package:
# apt-get install lvm2
Create a simple LVM disk
1.- Partition the disk with fdisk with the type of partition LVM (8e) :
2.- Initialize the partitions with pvcreate:
# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created # pvcreate /dev/sdc1 Physical volume "/dev/sdc1" successfully created # pvcreate /dev/sdd1 Physical volume "/dev/sdd1" successfully created
3.- Create an LVM group containing the partitions:
# vgcreate mygrp /dev/sdb1 /dev/sdd1 Volume group "mygrp" successfully created
4.- Use the command vgdisplay to see our LVM groups configured in our system:
5.- Create the LVM device:
# lvcreate -L 350G -n disk1 mygrp Logical volume "disk1" created
6.- Use the command line lvdisplay to see our disk:
7.- Create the filesystem in our logical volume device:
# mkfs.xfs /dev/mygrp/disk1 # mount -t xfs /dev/mygrp/disk1 /mnt/disk1/
Add a new disk to our Volume group
1.- Extend our Volume group adding the new partition:
# vgextend mygrp /dev/sdc1 Volume group "mygrp" successfully extended
2.- Execute vgdisplay to see the new changes:
Extend the size of a LVM
1.- Extend the volume size:
# lvextend -L +10G /dev/mygrp/disk1 Extending logical volume disk1 to 360.00 GiB Logical volume disk1 successfully resized
2.- Apply the extension to the xfs filesystem:
# xfs_growfs /dev/mygrp/disk1 meta-data=/dev/mapper/mygrp-disk1 isize=256 agcount=4, agsize=22937600 blks = sectsz=512 attr=2 data = bsize=4096 blocks=91750400, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 log =internal bsize=4096 blocks=44800, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 91750400 to 94371840
3.- Test the changes:
Reduce the size of a LVM
1.- reducing the size:
# lvreduce -L -190G /dev/mygrp/disk1 WARNING: Reducing active and open logical volume to 130.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce disk1? [y/n]: y Reducing logical volume disk1 to 130.00 GiB Logical volume disk1 successfully resized
**Note with xfs filesystem It’s not posible shrink the filesystem, you have to back up your files before the resize and format the volume. With ext2/3 the utility resize2fs or resize4fs for ext4, also with reiserfs you can use resize_reiserfs.
2.- Format the partition and see the changes:
# mkfs.xfs -f /dev/mygrp/disk1
Hot remove disk from a volume group
I’ll remove the disk 1 (/dev/sdb1) from the volume group mygrp. Firs we have to move the content of /dev/sdb1 to another partition equal or greater than the size of this partition. For this I’ll add another partition of 400GB to the volume group mygrp:
1.- First we have to move the content of /dev/sdb1 to the new partition /dev/sde1:
# pvmove /dev/sdb1 /dev/sde1 /dev/sdb1: Moved: 9.6% /dev/sdb1: Moved: 17.2% /dev/sdb1: Moved: 24.8% /dev/sdb1: Moved: 33.5% /dev/sdb1: Moved: 40.1% /dev/sdb1: Moved: 54.2% /dev/sdb1: Moved: 68.3% /dev/sdb1: Moved: 75.9% /dev/sdb1: Moved: 81.3% /dev/sdb1: Moved: 93.3% /dev/sdb1: Moved: 100.0%
2.- Remove /dev/sdb1 from the volume group:
# vgreduce mygrp /dev/sdb1 Removed "/dev/sdb1" from volume group "mygrp"
3.- Now we can remove the device:
# pvremove /dev/sdb1 Labels on physical volume "/dev/sdb1" successfully wiped
4.- see the changes: