Monday, June 3, 2013

CentOS:: Resizing LVM (Physical / Virtual)

A bit of background. XenServer and VM templates for easy deploying of virtual machines. We have a standard disk configuration of the following,

  • Disk /dev/xvda: 12.9 GB in two partitions
    • /dev/xvda1 ext4 /boot (500MB)
    • /dev/xvda2 the rest used as a physical volume (PV) for the LVM volume group called /dev/mapper/vg_centos63base-lv_root.

Action Plan:

I need to have a xxGB partition because of some developer that needs more disk space to accomplish his/her task. There are two ways of doing this. Create a extra xxGB disk and attach it the vm under new directory/partition or you can extend the current LVM with the attached disk (What happens when you lose the file you extended you disk with?). Both ideas seems boring and particularly not efficient, especially if you have a couple of vms. 

The approach I’m following does take a fair bit more time, but I regard it as a elegant solution. Also I would recommend you to take snapshots before the resize and also assign downtime as you need to reboot the vm a couple of times.

So here’s the action list,

  1. Run # fdisk -l and verify the size of your /dev/xvda
  2. Shutdown VM.
  3. Increase the virtual harddrive, please follow each vendors specific approach.
  4. Power on the VM.
  5. Run # fdisk -l and verify that the size of the /dev/xvda device did increase to your specified size, in my case I increase disk from 12GB to 16GB.
  6. Run # fdisk /dev/xvda
    1. under fdisk press ‘p’ – this will display your current layout
    2. We need to delete the 2nd partition and recreate it with the correct settings.
    3. press ‘d’ and enter ’2′ — You have now deleted the partition. We need to create it now.
    4. press ‘n’ and enter ‘p’ then ’2′. You have created a 2nd partition.
    5. press ‘p’ and you’ll see the partition but something is incorrect, our partition was a Linux LVM.
    6. press ‘t’ and enter ’2′ then enter ’8e’. This is HEX code for the Linux LVM system type.
    7. press ‘p’ and you’ll see the partition system type is now corrected.
    8. press ‘w’
  7. Now reboot the VM. The kernel needs to re-read the partition table of /dev/xvda
  8. Run # pvdisplay will show the current Physical Volume Size.
  9. Run # pvresize /dev/xvda2, then run # pvdisplay to display the extra storage available.
  10. # vgdisplay  - will show you the exact amount of “Free  PE / Size”
  11. Now we can just increase our root logical volume #
    • lvextend -L Size /dev/mapper/VG 
    • OR
    • lvextend -l +100%FREE /dev/mapper/VG
    • In my case I used,
      •  # lvextend -l +100%FREE /dev/mapper/vg_centos63base-lv_root
  12. We Need to resize the ext3/4 filesystem,
    • # resize2fs /dev/mapper/vg_centos63base-lv_root
  13. # df -h - Now you will see the extra diskspace!
Finally do a reboot to confirm. 

Have fun. (This is a updated entry from my old blog entry on wordpress.)


No comments:

Post a Comment