Thursday, January 9, 2014

Resizing Virtual Disks of VirtualBox

I have a virtual box version 4.3.4 64bit installed in Windows7 64bit. As a virtual host there is 64bit Redhat6.2 Enterprise Server and an Oracle Database 11.2.0.3 installed on it. The case is, I need more disk space in my virtual host for the Oracle Database datafiles. I have already assigned three virtual disks and I dont want to add more disks but to extend the existing ones. After extending the virtual disks of the Virtualbox then I need to extend the physical volumes and the logical volumes and then the filesystems.

Hence there is not any user interface functionality, starting from Virtualbox 4.0 there is a way to extend the .vdi virtual disk sizes. "VboxManage" supports to extend and change the .vdi virtual disk attributes. "VboxManage" executable can be found in the following paths of the related operating systems.


--Using Windows as a Host
# "C:\Program Files\Oracle\VirtualBox\VboxManage.exe" modifyhd virtual_hard_disk_name.vdi --resize size_in_mb

--Using Mac as a Host
# /Applications/VirtualBox.app/Contents/MacOS/VBoxManage modifyhd virtual_hard_disk_name.vdi --resize size_in_mb

--Using Linux as a Host
# VBoxManage modifyhd virtual_hard_disk_name.vdi --resize size_in_mb

# "C:\Program Files\Oracle\VirtualBox\VboxManage.exe" list hdds
UUID:           f7a2fda3-c730-465a-b022-8ee89e48a093
Parent UUID:    base
State:          locked write
Type:           normal (base)
Location:       E:\VirtualBox VMs\rhel6\rhel6_root.vdi
Storage format: VDI
Capacity:       10240 MBytes

UUID:           2be1f81e-b86b-41fe-9268-8622b5f4e4ec
Parent UUID:    base
State:          locked write
Type:           normal (base)
Location:       E:\VirtualBox VMs\rhel6\rhel6_oracle01.vdi
Storage format: VDI
Capacity:       20480 MBytes

UUID:           e62699cc-19dc-4e34-bf09-8e1a9ac6b415
Parent UUID:    base
State:          locked write
Type:           normal (base)
Location:       E:\VirtualBox VMs\rhel6\rhel6_oracle02.vdi
Storage format: VDI
Capacity:       20480 MBytes

UUID:           2d2d2490-d8a3-4184-bf54-02d874264f1b
Parent UUID:    base
State:          locked write
Type:           normal (base)
Location:       E:\VirtualBox VMs\rhel6\rhel6_oracle03.vdi
Storage format: VDI
Capacity:       20480 MBytes

UUID:           3d4385bf-ac3b-4ae7-a3f3-13c14fac4ff5
Parent UUID:    base
State:          locked write
Type:           normal (base)
Location:       E:\VirtualBox VMs\rhel6\rhel6_oracle04.vdi
Storage format: VDI
Capacity:       30720 MBytes

Virtual drives of the Virtualbox can be extended by using the following command (Please refer to your installation directory of the virtualbox to locate and execute the VboxManage.exe). VboxManage accepts either the full path of the virtual disk or the UUID of the disk. I prefered the UUID to extend.


# "C:\Program Files\Oracle\VirtualBox\VboxManage.exe" modifyhd 2d2d2490-d8a3-4184-bf54-02d874264f1b --resize 30170
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

As you can examine from the following output that the disk UUID 2d2d2490-d8a3-4184-bf54-02d874264f1b is extended from 20480MB to 30720MB successfully.


# "C:\Program Files\Oracle\VirtualBox\VboxManage.exe" list hdds
UUID:           f7a2fda3-c730-465a-b022-8ee89e48a093
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       E:\VirtualBox VMs\rhel6\rhel6_root.vdi
Storage format: VDI
Capacity:       10240 MBytes

UUID:           2be1f81e-b86b-41fe-9268-8622b5f4e4ec
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       E:\VirtualBox VMs\rhel6\rhel6_oracle01.vdi
Storage format: VDI
Capacity:       20480 MBytes

UUID:           e62699cc-19dc-4e34-bf09-8e1a9ac6b415
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       E:\VirtualBox VMs\rhel6\rhel6_oracle02.vdi
Storage format: VDI
Capacity:       20480 MBytes

UUID:           2d2d2490-d8a3-4184-bf54-02d874264f1b
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       E:\VirtualBox VMs\rhel6\rhel6_oracle03.vdi
Storage format: VDI
Capacity:       30720 MBytes

UUID:           3d4385bf-ac3b-4ae7-a3f3-13c14fac4ff5
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       E:\VirtualBox VMs\rhel6\rhel6_oracle04.vdi
Storage format: VDI
Capacity:       30720 MBytes

The rest of the operations will be held in the virtual host, which is RHEL6.2 64bit with logical volume manager used.

First /dev/sd* devices should be rescanned. I used /sbin/partprobe and then a reboot because all my devices where mounted. In order not to reboot, all devices should be unmounted first.

After the reboot, checking with fdisk gives the updated disk size of 30Gb. But the problem is the partition itself. The partition is still 20G and there is a way to extend it which is simply removing and recreating it. But, the thing is, i dont want to lose the data inside this partition which is a part of my volume group. After some investigation, I learned that if I delete the partition and create it with same SystemID (8e - Linux LVM) and stating from the very same starting block my data should not be lost. This worths trying.


# fdisk /dev/sdc

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/sdc: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x00079340

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        2611    20970496   8e  Linux LVM

Command (m for help): d
Selected partition 1

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-3916, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-3916, default 3916):
Using default value 3916

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdc: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x00079340

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        3916    31454246   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

After the reboot it seems the partition is extended successfully and my LVM works fine. Next, I should extend my physical volume pv's.


# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             9.9G  7.1G  2.3G  76% /
tmpfs                 2.0G  100K  2.0G   1% /dev/shm
/dev/mapper/vg_oracle-lv_oracle
                       30G   17G   12G  61% /oracle
/dev/mapper/vg_oracle-lv_oradata
                       60G   53G  3.2G  95% /oradata
shmfs                 2.0G  100K  2.0G   1% /dev/shm
shared                120G   94G   26G  79% /media/sf_shared
/dev/sr0               62M   62M     0 100% /media/VBOXADDITIONS_4.3.4_91027

# pvresize /dev/sdc1 30720
  Physical volume "/dev/sdc1" changed
  Failed to read physical volume "30720"
  1 physical volume(s) resized / 0 physical volume(s) not resized

# pvdisplay /dev/sdc1
  --- Physical volume ---
  PV Name               /dev/sdc1
  VG Name               vg_oracle
  PV Size               30.00 GiB / not usable 986.00 KiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              7679
  Free PE               2560
  Allocated PE          5119
  PV UUID               bkjcdV-ZOMc-0pSe-BfcW-pbny-ka4f-MiDBFO

# vgdisplay
  --- Volume group ---
  VG Name               vg_oracle
  System ID           
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  21
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               109.98 GiB
  PE Size               4.00 MiB
  Total PE              28156
  Alloc PE / Size       23015 / 89.90 GiB
  Free  PE / Size       5141 / 20.08 GiB
  VG UUID               9ERVOu-ATwS-j7H5-5vce-PLj9-3aG9-c7biBO



with a couple of reboots it seems that it is possible to extend the virtual disks of Virtualbox without dataloss... Cheers..