Saturday, January 12, 2013

2013 January 12 | Disk Administration | Resizing Ext4 Partitions on a LIVE CentOS Virtual Machine

Goal: Increase the storage space for the variable data partition (/var) on a system without re-installing from scratch (Resizing Ext4 Partitions on a LIVE CentOS Virtual Machine).

Symptom: The current partition has 30GB for testing mode, but we need about 250GB to go "live" in production mode.

Reason: We re-sized the partition "live" in order to avoid reconfiguration of the virtual machine from scratch.

Results: The virtual HDD/LVM partition was successfully changed and re-sized to the new size without reconfiguration of the machine.


Have the VMWARE administrator increase the capacity of the virtual HDD attached to the machine. If possible, the VMWARE admin should also perform the resize using the “boot image” of the OS with HDD utilities (such as GPARTED).  If not possible, follow these steps carefully!

Changing the Virtual HDD size:

With the VM booted and from within the VM OS:

  • Optional: stop all processes such as apache/mysql and other client applications

  • Run “fdisk” on the HDD device (/dev/sda): # fdisk /dev/sda

  • Note the start cylinder for the existing data partition and the partition type: For example: start cylinder 64 - type 8e linux LVM

  • Delete the partition (dangerous - be careful) - e.g. (d) then number 2!

  • Recreate the partition table entry using the old start cylinder and new maximum size (n), primary partition, etc)

  • Change the partition type to the former type using (t) command:

  • Write the partition table (w) and the reboot the VM.

Resizing the “VG/LV/FS”:

First,  resize the physical volume using this command: sudo pvresize /dev/sda2

Next, note “free PE” from this command: # sudo vgdisplay

Then find the LV name:  # df -h

Extend the LV to the new maximum size available:
# sudo lvextend /dev/mapper/VolGroup-lv_root --extents +56319
(note the +56319 comes from the vgdisplay command which shows the Free PE)

This increases the file system size to the new size.  
# sudo resize2fs /dev/mapper/VolGroup-lv_root
It may take a long time to run this command.

Reboot the system.  Verify the change in file system with the command: # df -h

[thomw@vm ~]$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root    245G   17G  217G   7% /
tmpfs                    499M     0  499M   0% /dev/shm
/dev/sda1             485M  118M  342M  26% /boot