Extending disk size of your Virtual Machine in Virtual Box in Mac OS X (Linux)

So this was my problem:

I have a CentOS 6.4 which I just realized I cannot extend my database anymore because I'm running out of disk space.

So let's go by giving a solution to our problem.

In Mac OS X, if you go to to VirtualBox application package, there is a file called VBoxManage which is a utility.

#> ls -al /Applications/VirtualBox.app/Contents/MacOS/
Display all 102 possibilities? (y or n)
ExtensionPacks/                       VBoxDDR0.r0.codesign                  VBoxNetDHCP.dylib                     VMMGC.gc-x86
UserManual.pdf                        VBoxDDU.dylib                         VBoxOGLhostcrutil.dylib               VMMGC.gc-x86.codesign
VBoxAuth.dylib                        VBoxDbg.dylib                         VBoxOGLhosterrorspu.dylib             VMMR0.r0
VBoxAuthSimple.dylib                  VBoxDragAndDropSvc.dylib              VBoxOGLrenderspu.dylib                VMMR0.r0.codesign
VBoxAutostart                         VBoxEFI32.fd                          VBoxPython2_5.so                      VirtualBox
VBoxAutostart-amd64                   VBoxEFI64.fd                          VBoxPython2_6.so                      VirtualBox-amd64
VBoxAutostart-x86                     VBoxExtPackHelperApp                  VBoxPython2_7.so                      VirtualBox-x86
VBoxAutostartDarwin.sh                VBoxExtPackHelperApp-amd64            VBoxREM.dylib                         VirtualBox.dylib
VBoxBalloonCtrl                       VBoxExtPackHelperApp-x86              VBoxREM32.dylib                       VirtualBoxVM
VBoxBalloonCtrl-amd64                 VBoxGuestAdditions.iso                VBoxREM64.dylib                       VirtualBoxVM-amd64
VBoxBalloonCtrl-x86                   VBoxGuestControlSvc.dylib             VBoxRT.dylib                          VirtualBoxVM-x86
VBoxDD.dylib                          VBoxGuestPropSvc.dylib                VBoxSVC                               accessible/
VBoxDD2.dylib                         VBoxHeadless                          VBoxSVC-amd64                         components/
VBoxDD2GC.gc                          VBoxHeadless-amd64                    VBoxSVC-x86                           libvboxjxpcom.jnilib
VBoxDD2GC.gc-amd64                    VBoxHeadless-x86                      VBoxSharedClipboard.dylib             nls/
VBoxDD2GC.gc-amd64.codesign           VBoxHeadless.dylib                    VBoxSharedCrOpenGL.dylib              org.virtualbox.vboxautostart.plist
VBoxDD2GC.gc-x86                      VBoxHostChannel.dylib                 VBoxSharedFolders.dylib               org.virtualbox.vboxballoonctrl.plist
VBoxDD2GC.gc-x86.codesign             VBoxManage                            VBoxVMM.dylib                         org.virtualbox.vboxwebsrv.plist
VBoxDD2R0.r0                          VBoxManage-amd64                      VBoxXPCOM.dylib                       sdk/
VBoxDD2R0.r0.codesign                 VBoxManage-x86                        VBoxXPCOMC.dylib                      vboxshell.py
VBoxDDGC.gc                           VBoxNetAdpCtl                         VBoxXPCOMIPCD                         vboxwebsrv
VBoxDDGC.gc-amd64                     VBoxNetAdpCtl-amd64                   VBoxXPCOMIPCD-amd64                   vboxwebsrv-amd64
VBoxDDGC.gc-amd64.codesign            VBoxNetAdpCtl-x86                     VBoxXPCOMIPCD-x86                     vboxwebsrv-x86
VBoxDDGC.gc-x86                       VBoxNetDHCP                           VMMGC.gc                              vmstarter
VBoxDDGC.gc-x86.codesign              VBoxNetDHCP-amd64                     VMMGC.gc-amd64                        
VBoxDDR0.r0                           VBoxNetDHCP-x86                       VMMGC.gc-amd64.codesign               



Resizing Virtual Machine Image

So let's start resizing the size of my virtual machine image by,


#> /Applications/VirtualBox.app/Contents/MacOS/VBoxManage modifyhd /Users/geekgogie/VirtualBox\ VMs/CentOS\ 6\ x86_64/CentOS\ 6\ x86_64.vdi --resize 25000
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%



Then we will create a new .vdi file by,

#> cd ~/VirtualBox\ VMs/CentOS\ 6\ x86_64/

#> /Applications/VirtualBox.app/Contents/MacOS/VBoxManage createhd --filename CentOS6.4.x86_64-New.vdi --size 30000 
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Disk image created. UUID: 65674c7b-2155-443f-8b2f-b4ed78fea5ce

#> ls -alt .
total 21076400
drwxr-xr-x   8 geekgogie  staff          272 Jul 16 23:12 .
-rw-------   1 geekgogie  staff       126976 Jul 16 23:12 CentOS6.4.x86_64-New.vdi
-rwxr-xr-x   1 geekgogie  staff  10790948864 Jul 16 23:12 CentOS 6 x86_64.vdi
drwx------   6 geekgogie  staff          204 Jul 16 22:58 Logs
-rw-------   1 geekgogie  staff        19063 Jul 16 22:58 CentOS 6 x86_64.vbox
drwxr-xr-x  15 geekgogie  staff          510 Jul 16 22:57 ..
-rw-------   1 geekgogie  staff        19063 Jul 16 22:50 CentOS 6 x86_64.vbox-prev
drwx------   2 geekgogie  staff           68 Jul 13 09:31 Snapshots
~/VirtualBox VMs/CentOS 6 x86_64@macmini #> ls -alth 
total 21076400
drwxr-xr-x   8 geekgogie  staff   272B Jul 16 23:12 .
-rw-------   1 geekgogie  staff   124K Jul 16 23:12 CentOS6.4.x86_64-New.vdi
-rwxr-xr-x   1 geekgogie  staff    10G Jul 16 23:12 CentOS 6 x86_64.vdi
drwx------   6 geekgogie  staff   204B Jul 16 22:58 Logs
-rw-------   1 geekgogie  staff    19K Jul 16 22:58 CentOS 6 x86_64.vbox
drwxr-xr-x  15 geekgogie  staff   510B Jul 16 22:57 ..
-rw-------   1 geekgogie  staff    19K Jul 16 22:50 CentOS 6 x86_64.vbox-prev
drwx------   2 geekgogie  staff    68B Jul 13 09:31 Snapshots


Since we have created a new vdi file, we need to clone the original .vdi to the new .vdi file. This is somewhat like a disk dump strategy but copying the metadata and contents from the existing file to the new file. Be sure that you're existing vdi file has been properly shutdown.


#> /Applications/VirtualBox.app/Contents/MacOS/VBoxManage clonehd CentOS\ 6\ x86_64.vdi CentOS6.4.x86_64-New.vdi --existing
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'VDI'. UUID: 65674c7b-2155-443f-8b2f-b4ed78fea5ce
~/VirtualBox VMs/CentOS 6 x86_64@macmini #> ls -alt .
total 41589168
drwxr-xr-x   8 geekgogie  staff          272 Jul 16 23:23 .
-rw-------   1 geekgogie  staff        19063 Jul 16 23:23 CentOS 6 x86_64.vbox
-rw-------   1 geekgogie  staff  10502664192 Jul 16 23:23 CentOS6.4.x86_64-New.vdi
-rw-------   1 geekgogie  staff        19063 Jul 16 23:17 CentOS 6 x86_64.vbox-prev
-rwxr-xr-x   1 geekgogie  staff  10790948864 Jul 16 23:17 CentOS 6 x86_64.vdi
drwx------   6 geekgogie  staff          204 Jul 16 22:58 Logs
drwxr-xr-x  15 geekgogie  staff          510 Jul 16 22:57 ..
drwx------   2 geekgogie  staff           68 Jul 13 09:31 Snapshots




Next, we need to assign the storage controller for our new vdi being created and cloned. For this one, I'm using SATA, so this is how I add it.

But before that, If you're tired of copy-pasting long full-path of the command, just do,

#> PATH=$PATH:/Applications/VirtualBox.app/Contents/MacOS/

then let's add the controller using "IntelAhci" and it's storage as "SATA" with name "Controller SATA",

#> VBoxManage storagectl "CentOS-6.4 (slave)" --add SATA --controller IntelAhci --name "Controller SATA"

Then, let's attached it.

#> VBoxManage storageattach "CentOS-6.4 (slave)" --storagectl  "Controller SATA" --port 0 --device 0 --type hdd  --medium CentOS6.4.x86_64-New.vdi 



If you have problems also when using an existing VDI UUID, you can also change this by using the command,

#> /Applications/VirtualBox.app/Contents/MacOS/VBoxManage internalcommands sethduuid CentOS6.4.x86_64-New.vdi 
UUID changed to: a87c21b6-d339-4007-bb29-d69ee0a29762



You can also get the name of the Virtual Machine image by checking your .vbox file by (check the marked in red string of text),

#> cat CentOS-6.4\ \(slave\).vbox |grep "<Machine"
grep: warning: recursive search of stdin
(standard input):9:  <Machine uuid="{0a19abcc-1264-4d0b-b8bd-3f2d10768dd7}" name="CentOS-6.4 (slave)" OSType="RedHat_64" snapshotFolder="Snapshots" lastStateChange="2013-07-21T12:26:09Z">


Now, if you are already done, you need to use fdisk. First, run fdisk as root, see below,

[root@centos-slave ~]# fdisk -l

Disk /dev/sda: 31.5 GB, 31457280000 bytes
255 heads, 63 sectors/track, 3824 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: 0x0005faee

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1567    12069888   8e  Linux LVM
/dev/sda3            1567        3694    17088512    5  Extended
/dev/sda5            1567        1698     1055248+  82  Linux swap / Solaris
/dev/sda6            1699        3694    16031215+  83  Linux


Now, you just have to run,

[root@centos-slave ~]# fdisk /dev/sda

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): 

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit

   x   extra functionality (experts only)


However, I cannot cover here much on using fdisk but it's easy to follow how to create a new device. Just create using "n" command, then assign the proper blocks you have your new device created.

See here for helpful guide on using fdisk,

http://www.howtogeek.com/106873/how-to-use-fdisk-to-manage-partitions-on-linux/

http://download.parallels.com/desktop/v5/docs/en/Parallels_Desktop_Users_Guide/23117.htm

http://tldp.org/HOWTO/Partition/fdisk_partitioning.html


FYI: If you let, say, /usr/bin is full or any directory, you can however create some symbolical links and redirect the path to that device. That way, still, you can install more packages you want or you can build that package using ./configure and use the path where your new device is created, example, mine, I have created that /dev/sda6 which points to /opt/. See my df -h result.

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       11G  7.7G  2.3G  78% /
tmpfs                 499M     0  499M   0% /dev/shm
/dev/sda1             485M   72M  388M  16% /boot
/dev/sda6              16G  4.7G  9.7G  33% /opt


Helpful url's:

http://www.giannistsakiris.com/index.php/2009/05/06/virtualbox-how-to-change-the-uuid-of-virtual-disk-vdi/

http://www.virtualbox.org/manual/ch08.html

https://forums.virtualbox.org/viewtopic.php?f=2&t=25319

http://cheznick.net/main/content/creating-a-virtual-machine-using-the-virtualbox-cli



Comments

Popular posts from this blog

Converting sectors into MB - Useful in understanding the sectors in iostat in Linux

What is Disk Contention?

Installing MySQL from source: Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH)