Sunday, January 13, 2013

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

5 comments:

  1. Good process, however, vgdisplay reported "free PE" as 0/0. Before extending the volume, I had to resize the physical volume: pvresize /dev/sda2. Once that completed, I was able to get the "free PE" value to extend the logical volume. I'm running CentOS 6.4...

    ReplyDelete
  2. Thanks for that feedback. I don't know why that step wasn't needed for me. But it was a while back when I did this so there is a possibility that I've missed that step in my instructions. Apologies...

    ReplyDelete
  3. HIGHLY useful. I used the gparted live CD to resize some stuff, then when I booted up, df still showed the same usage on disk. Your instructions with lvextend, and resize2fs were spot on. df now shows an accurate reading.

    ReplyDelete
  4. Great post. can you please calrify

    1. After VMWare admin increase disk size, do we need to reboot VM or not?
    2. When you say live, is it mean without rebooting the VM?
    3. I am trying to do without rebooting the VM. And when i tried to create new partition as mention below
    "Recreate the partition table entry using the old start cylinder and new maximum size (n), primary partition, etc)"

    What is meaning of size (n) i am not able to enter more than the value already there i.e. 2088 cylinders. If i am entering that no size increase. Still old value. Can you please clarify?

    ReplyDelete
  5. @CloudTech

    1. You have to reboot the VM after you use FDISK - it won't recognize the change to the partition table.
    2. I mean "live" while the VM is running and from within the VM - not from the Virtual Machine console.
    3. The person in charge of the VM Host needs to set your virtual disk to a larger size. Since that control was out of my hands, I was left to resizing the disk from within the VM. However, first I had to work with the administrator and get them to make the virtual disk larger from the VM Host console. Does that make sense?

    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!

    ReplyDelete