/ Engineering

Resizing disk size without restarting process

Often, we end up adding disks to increase space. However, we have to mount them on a new partition. Change our data modules everywhere.

Ex: Mongo disk is originally on /dev/sdb1. Now, we need to add 100gb more. So, we'll take snapshot of the current volume, launch a new volume with 100gb more. Plan out how to restart the process without affecting anything. We'll be painfully successful.
Given the beauty of cloud computing and the virtual world our servers (and we too (tongue) ) live in, we have the advantage of separating logical and physical.
Introducing the hero, Logical Volume Manager (LVM).

What does it do?

It allows you to resize an existing disk partition, irrespective of underlying disk layout.
In other words, you can connect 5 physical hard disk drives to your machine and tell your OS to treat all of them as single partition. Also, you can add 1 more hard drive to the same partition.

What is LVM?

Necessary stuff about LVM:

Components:

  • Physical Volume (PV): Underlying disk partitions that builds up to volume group.
  • Volume Group (VG): A combination of few partitions (PVs). Similar to an actual disk drive with several partitions.
  • Logical Volume (LV): Similar to partitions on disk drive. Made from Volume Group.
  • Physical Extent (PE): The smallest amount of the disk that can be given to a logical volume and further additions are done in multiples of physical extent.

Rest of the unnecessary stuff can be read here, when it becomes necessary.

Installation:
sudo yum install lvm2

How to create a LVM? (In an interactive shell)

Need to create an actual partition and set the type of the partition as LVM.

  • Assuming we've a partition as /dev/sdb, run the command fdisk /dev/sdb. This will open the fdisk prompt.
  • Press 'n' followed by 'p', n for new and p for partition. On a new disk, this should be first partition.
    So, input 1.
  • Now, accept the defaults until you reach 'command (m for help)'
    Don't do it blindly, though you can.
  • We've a new partition. We need to set it as LVM. To do this, give the command 't', followed by '8e'.
    What is 8e, should be clear when you're doing this.
  • Write (save) these changes to the disk – press 'w'

So, now you should have '/dev/sdb1'

Create physical volume (PV): pvcreate /dev/sdb1

pvdisplay should give you output something like this:

--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name 
PV Size 100.00 GiB
Allocatable NO
PE Size 0 
Total PE 0
Free PE 0
Allocated PE 0
PV UUID some-random-id-here

VG Name in above output is empty. So, this PV is not part of any Volume Group. We need to add this.
Oops, we don't have a VG. Create it now:
vgcreate samplevg /dev/sdb1

vgdisplay should give you an output similar to this:

--- Volume group ---
VG Name samplevg
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 15.00 GiB
PE Size 4.00 MiB
Total PE 3839
Alloc PE / Size 0 / 0 
Free PE / Size 3839 / 15.00 GiB
VG UUID some-random-lengthy-Id

Create a logical volume now, in the group.
lvcreate --name samplelv --size 3G samplevg

Logical Volume display by command
lvdisplay
Which outputs to

--- Logical volume ---
LV Path /dev/samplevg/samplelv
LV Name samplelv
VG Name samplevg
LV UUID yet-another-random-unique-id
LV Write Access read/write
LV Creation host, time localhost, 2016-10-22 18:34:57 +0000
LV Status available
LV Size 3.00 GiB
Current LE 1280
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1

Creating a file system on the logical volume group that we've just created. Choosing ext4 here, as it works most of the times.
mkfs.ext4 /dev/samplevg/samplelv

We can mount it and use it now.

How to extend LVM or add disk to LVM Partition?

Now the real fun part. Resizing the disk our application is using, without restarting the application.

  • Repeat steps 1 and 2 from "How to create a LVM?", replacing sdb with sdc.
  • Now, we got the disk added in the PV. Now let us extend the volume group.Extending Volume Group with new partition
    vgextend samplevg /dev/sdc1

Extending our logical volume with new partition
lvextend /dev/samplevg/samplelv /dev/sdc1

You'll get an output like:

Size of logical volume samplevg/samplelv changed from 3.00 GiB to 10.00 GiB

Tell our Volume to have fun with extra size
resize2fs /dev/samplevg/samplelv (Note that I've made mkfs as ext4, hence resize.)

You can apply this seamlessly when you need to increase disk size of your process, and doesn't want to take snapshot of existing, detaching it, attaching new, restart the process, blah blah.
If anything in this doc requires more explanation, feel free to comment.

P.S.: The values in this document are not real.