VirtualBox has a graphical front end which gives us easy access to manage and utilize our virtual machines. Now suppose we are in a situation where we don’t have access to that shiny Graphical User Interface world (for example remote-accessing a server) and trying to manage VirtualBox’s Virtual Disk Image. Well, VirtualBox is not always about their GUI. In fact, we can too manage everything using terminal / command line.
In this article, we will discuss about some notes about how we can manage VDI. The term manage in this arcitle including: creating a VDI, resizing, mount images to our system, etc. In this article I use:
- Slackware64 14.0 as host
- VirtualBox 4.6.2
Although it is said that we use Slackware64 as host, the method we use here is a generic one. It means, we can do it into other linux distribution as well.
Create A Virtual Disk
Creating a Virtual Disk is as simple as creating one for Qemu. Here we need to specify where we will store the vdi and what size it should be. The size would be on Mega Bytes unit. Now, we have two scenario:
Creating a Fixed Size (Static) VDI with 10GB size
Let’s do a simple calculation. The size should be 10GB, which is around 10*1024MB or 1024 MB. By default, VirtualBox will create a dynamic disk, unless told otherwise. In this scenario we will store the VDI as XathDisk.vdi on current working directory. The –variant Fixed is a must to make sure a static disk. Now the command to do so:
VBoxManage createhd --filename XathDisk.vdi --size 10240 --variant Fixed
Not so hard, isn’t it? 😀
Now, please note that a fixed size disc image can’t be resized (at least at this time, using VirtualBox 4.2.6).
Creating a Dynamic Size VDI with maximum size 10GB
It is even easier than previous scenario. Now, following command is creating the disk like previous did but in dynamic size
VBoxManage createhd --filename XathDisk.vdi --size 10240
Resizing the Virtual Disk
Resizing means we change the size of the disk. Fortunately, VirtualBox offer a tool to do so. On real machine, this would be like copying the disk data into different disk.
Please note that this method is currently works only for dynamic size disk.
Now, suppose we have a disk initially has a size of 8GB. We want to increase the disk size, or enlarging the size to 10GB. We can simply did following command:
VBoxManage modifyhd XathDisk.vdi --resize 10240
However, the size is logically used by disk to set it as maximum size while the partition in inside of disk still use the old size. You can use partition tool such as gparted to modify the partition size.
Cloning a VDI mean we create an identical disk image to the older one. This is very useful especially if we experiment with machine having a high failure probability.
Now, to clone a Virtual Disk we can use following command:
VBoxManage clonehd <old one> <new one>
The <old one> is the source / the disk which we want to clone and <new one> is the result / the cloned disk.
Convert Dynamic to Static and Static to Dynamic Disk
At this time, there is no default command to do conversion. What we can do is cloning old one to new one with different properties. Thus, we can convert dynamic to static disk by clone the disk and set the –variant to fixed and vice versa. Later, we can remove the old one and rename the new one as old one.
Here is how conversion work (assuming you use VDI format):
# Dynamic to Static VBoxManage clonehd <old one> <new one> --variant Fixed rm -f <old one> mv <new one> <old one> #Static to Dynamic VBoxManage clonehd <old one> <new one> --variant Standard rm -f <old one> mv <new one> <old one>
Mounting a Virtual Disk
using Network Block Device
On every QEMU package, there is a tool for utilizing nbd (network block device). Technically speakin, QEMU use Kernel facility for communicating with network block device. The network block device itself is a “remote-server” and kernel use it as one of its block device. QEMU then will attach the VDI as if the VDI is a network block device. Later we can use the block device by mounting it to a directory.
Now let see the example. Using Xathrya.vdi, I will attach it to first network device. The network device is enabled after I insert kernel module nbd and then they are listed on /dev directory. I then mount it as /mnt/disk/nbd0 (which is already created).
modprobe nbd # Attach & Mount qemu-nbd -c /dev/nbd0 Xathrya.vdi mount /dev/nbd0 /mtn/disk/nbd0 # Detach & Unmount umount /dev/nbd0 qemu -d /dev/nbd0virtualbox, virtualization