We have dozens of Ubuntu nodes where I have to resize the hard disk drive to different sizes. Currently I’m doing all of the following steps manually:
- Increase the size of each node’s virtual hard disk in VMware vCenter.
- Change the configuration of the DVD drive, mount a GParted ISO, boot from BIOS, and change the boot order.
- Boot into GParted, manually increase
/dev/sda2
and/dev/sda5
. - Stop the VM, disable the DVD drive, and start the VM.
- Use
lvextend -r /dev/ubuntu/root /dev/sda5
to extend the LVM and resize the partition to its maximum possible size. - Optional: Check with
df -h
if everything’s OK.
I would love to automate this process, in a best case to provide a list of node names and corresponding sizes and let the tool do its job. In the case where there is no automated solution available I would love to hear about micro-optimizing every of these steps to make my tedious job easier.
We’re already automatically provisioning our nodes with Chef, and a VM template with a hard disk size of 16 GB.
Any smart ideas?
This can be done without a reboot or the nasty DVD step...
First, expand the disk at the vCenter/vSphere level. You know how to do that. It can also be automated or scripted.
Show your current block device size...
Rescan the SCSI bus within the VM to realize the new drive size.
(Note: The disk identifiers may vary, but tend to correspond with the VM's SCSI device nodes. You'll likely see 0:0:0:0 for the root volume you're dealing with.)
Verify this with
dmesg | tail
or anotherfdisk -l
Continue with your LVM magic...
That should take care of everything.
As @ewwhite wrote, you'll want to rescan the bus to update the OS with information about the new size of the disk.
But then what you'll have to do is enter fdisk and - perhaps counterintuitively delete the partition, and - this is important - recreate a new one with the same beginning but a new end before saving your partition table.
Update partition information by running
partx /dev/sda[n]
Finally we use
pvresize
,lvextend
, and the appropriate implementation of*_growfs
to end up with the new, root filesystem size.As usual: This is very unlikely to break anything unless you do something wrong, but always have working backups before modifying partition information on an important computer.
(Here's a link to a more elaborate writeup of the process I did a while ago.)