Resizing Root disk of a linux Virtual Machine

In CloudStack, the default root disk size comes from the template. For the default CentOs 6.5 template, this was around 1.3GB.
I wanted a VM in my CloudStack deployment with root disk size as 20GB. 

I created a vm with custom root disk size as 20GB. Even after specifying root disk size, df -h was showing only 1.3GB as usable disk. 

[root@VM-8b19c14c-9feb-4b77-a488-502b38e32ed1 ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root  1.3G  604M  649M  49% /
tmpfs                         1.9G     0  1.9G   0% /dev/shm
/dev/xvda1                    485M   32M  428M   7% /boot


fdisk -l showed that there is additional space but volumes were not created.

[root@VM-8b19c14c-9feb-4b77-a488-502b38e32ed1 ~]# fdisk -l

Disk /dev/xvda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b380d

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/xvda2              64         262     1584128   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

Disk /dev/mapper/VolGroup-lv_root: 1405 MB, 1405091840 bytes
255 heads, 63 sectors/track, 170 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/VolGroup-lv_swap: 213 MB, 213909504 bytes
255 heads, 63 sectors/track, 26 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


I followed below steps to increase the root disk size:

Create a new partition in xvda for unused space
[root@VM-8b19c14c-9feb-4b77-a488-502b38e32ed1 ~]# fdisk /dev/xvda
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (262-2610, default 262):
Using default value 262
Last cylinder, +cylinders or +size{K,M,G} (262-2610, default 2610):
Using default value 2610

While you are in the command mode, change partition type from Linux to Linux LVM 
Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)

Verify new partition by typing p in the command mode
Command (m for help): p

Disk /dev/xvda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b380d

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/xvda2              64         262     1584128   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/xvda3             262        2610    18867673   8e  Linux LVM


write/save partition table by typing 'w' in command prompt
Command (m for help): w
The partition table has been altered!

reboot to sync disks
[root@VM-8b19c14c-9feb-4b77-a488-502b38e32ed1 ~]# reboot

Create a physical volume using pvcreate
[root@VM-8b19c14c-9feb-4b77-a488-502b38e32ed1 ~]# pvcreate /dev/xvda3
  Physical volume "/dev/xvda3" successfully created

Check used space
[root@VM-8b19c14c-9feb-4b77-a488-502b38e32ed1 ~]# pvs -o+pv_used
  PV         VG       Fmt  Attr PSize  PFree  Used
  /dev/xvda2 VolGroup lvm2 a--   1.51g     0  1.51g
  /dev/xvda3          lvm2 a--  17.99g 17.99g    0
[root@VM-8b19c14c-9feb-4b77-a488-502b38e32ed1 ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root  1.3G  604M  649M  49% /
tmpfs                         1.9G     0  1.9G   0% /dev/shm
/dev/xvda1                    485M   32M  428M   7% /boot

Using volume group from previus pvs command, perform physical volume extend
[root@VM-8b19c14c-9feb-4b77-a488-502b38e32ed1 ~]# vgextend VolGroup /dev/xvda3
  Volume group "VolGroup" successfully extended

Merge new volume into existing volume
[root@VM-8b19c14c-9feb-4b77-a488-502b38e32ed1 ~]# pvmove /dev/xvda2 /dev/xvda3
  /dev/xvda2: Moved: 1.0%
  /dev/xvda2: Moved: 86.8%
  /dev/xvda2: Moved: 100.0%

Reduce Volume
[root@VM-8b19c14c-9feb-4b77-a488-502b38e32ed1 ~]# vgreduce VolGroup /dev/xvda2
  Removed "/dev/xvda2" from volume group "VolGroup"

Get free space for logical volume using pvs and increase volume space
[root@VM-8b19c14c-9feb-4b77-a488-502b38e32ed1 ~]# pvs -o+pv_used
  PV         VG       Fmt  Attr PSize  PFree  Used
  /dev/xvda2          lvm2 a--   1.51g  1.51g    0
  /dev/xvda3 VolGroup lvm2 a--  17.99g 16.48g 1.51g
[root@VM-8b19c14c-9feb-4b77-a488-502b38e32ed1 ~]# lvextend -L +16.48G /dev/mapper/VolGroup-lv_root
  Rounding size to boundary between physical extents: 16.48 GiB
  Extending logical volume lv_root to 17.79 GiB
  Logical volume lv_root successfully resized

Resize main volume
[root@VM-8b19c14c-9feb-4b77-a488-502b38e32ed1 ~]# resize2fs /dev/mapper/VolGroup-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/VolGroup-lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/mapper/VolGroup-lv_root to 4663296 (4k) blocks.
The filesystem on /dev/mapper/VolGroup-lv_root is now 4663296 blocks long.

Remove unwanted physical volume
[root@VM-8b19c14c-9feb-4b77-a488-502b38e32ed1 ~]# pvremove /dev/xvda2
  Labels on physical volume "/dev/xvda2" successfully wiped

Congratulations!! Your root disk size has increased and now ready to use.
[root@VM-8b19c14c-9feb-4b77-a488-502b38e32ed1 ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   18G  605M   17G   4% /
tmpfs                         1.9G     0  1.9G   0% /dev/shm
/dev/xvda1                    485M   32M  428M   7% /boot


If you do not see custom Root Disk size option in your CloudStack environment, you can create a data disk of size 20GB attach it to the VM and follow similar steps. The only change will be in the diskdrive.
Here, we created a new partition on xvda. For an attached data disk, you will have to create new partition1 on vdb and proceed.

Thank you Raja Pullela for helping me with this.

Comments

Popular posts from this blog

Configuring ADFS SSO with Apache CloudStack/CloudPlatform using SAML

Installing VmWare PowerCli on MacOS

A Look at Cloudplatform/Apache CloudStack LDAP Authentication Options