Monday, 16 January 2017

Working with LVM in CentOS 7: Resizing volumes and creating snapshots


Note: The following is not intended in any way as a practical example - rather it exists purely to demonstrate the creation of logical volumes, resizing them and creating snapshots.

Let's firstly setup the disks / partitions appropriately - I am going to use GPT for the disks, also ensuring that when creating the partitions they are setup as the 'LVM' type:

fdisk /dev/sdb
o # Initialize disk with mbr
n # Create new primary partition
[50% of disk size]
t 8e # Set partition type (or 15 if you are using GPT)
n # Create new primary partition
[50% of disk size]
t 8e # Set partition type (or 15 if you are using GPT)
w # Write changes

So we end up with two partitions /dev/sdb1 and /dev/sdb2.

and we will also create a partition on /dev/sdc - which consumes all available HD sectors:

fdisk /dev/sdc
o # Initialize disk with mbr
n # Create new primary partition
[accept defaults]
t 8e # Set partition type (or 15 if you are using GPT)
w # Write changes

So we end up with /dev/sdc1.

We will now need to create our physical volume group - this is simply all of the 'physical' disks or disk partitions e.g. /dev/sdb1:

pvcreate /dev/sdb1 /dev/sdb2 /dev/sdc1

Although I received something like:

  Device /dev/sdb2 not found (or ignored by filtering).
  Device /dev/sdc1 not found (or ignored by filtering).
  Physical volume "/dev/sdb1" successfully created.

In order to resolve this we need to set the disk label for each partition to 'loop' - this simply makes the partitions look like a normal disk.

parted /dev/sdb2
mklabel loop

and

parted /dev/sdc1
mklabel loop

and attempt to re-add:

pvcreate /dev/sdb1 /dev/sdb2 /dev/sdc1

Confirm them with:

pvdisplay

I actually don't want to include /dev/sdb2 - so i'll remove it with pvremove:

pvremove /dev/sdb2

And now create a volume group with our new physical volumes - for example:

vgcreate myvolumegroup /dev/sdb1 /dev/sdc1

And confirm with:

vgdisplay

  --- Volume group ---
  VG Name               myvolumegroup
  System ID
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               8.79 GiB
  PE Size               4.00 MiB
  Total PE              2251
  Alloc PE / Size       0 / 0
  Free  PE / Size       2251 / 8.79 GiB
  VG UUID               222222-bkQV-1111-xAzI-bbPr-AP7O-333333

We can now create our logical volume - using the '-L' parameter to specific a size of 1GB, the '-n' to specify the name of 'testlv' and the target volume group 'myvolumegroup':

lvcreate -L 1G -n testlv myvolumegroup

and confirm with:

lvdisplay

We can also get the block device from the above output - and mount it / create a file system for it with:

mkfs.xfs /dev/testvol/testlv

Extending the logical volume

Extend the actual logical volume by 50MB:

lvextend -L +50M /dev/testvol/testlv

and then ensure the file-system is increased as well:

xfs_growfs /dev/testvol/testlv

If you choose ext3/4 you will need to use the 'resize2fs' utility and also ensure that the file-system is not mounted.

Now let's say that we do not have enough free disk space in our volume group - in that case we will need to add an additional disk / partition - we can do this with the vgextend command:

pvcreate /dev/sdb2
vgextend volume01 /dev/sdb2

Creating a snapshot of a logical volume

We can take snapshots of logical volumes with the 'lvcreate' command - although when snapshotting a logical volume it's important to note that the snapshot is not a 'like for like' copy in terms of blocks - instead it only holds blocks that have changed on the original logical volume since the snapshot was taken!

lvcreate -L 50M -s -n mysnapshot /dev/testvol/testlv

and to remove the snapshot we can issue:

lvremove /dev/testvol/mysnapshot

0 comments:

Post a Comment