Increase the size of a VDI file

In this post, we are going to increase the size of the VDI file simulating the hard disk associate to one of our guest OS in VirutalBox.

What is the VDI file?

VirtualBox uses VDI files as the place to save all the information, that means, filesystems configured in your guest OS.
In the HOST OS is a normal file with extension VDI, which means Virtual Disk Image.

Why should I increase the size of a VDI file?

The short answer is because you are scarce of free space in your guest OS.

When you install a Guest OS with VirtualBox, during the initial configuration process, the setup requires you to set the size dedicated to the system. This is the size of your virtual filesystem (VDI file) that will be used during the installation of the guest system. There are several options, a fixed size for a VDI file, or dynamic (recommended and default). The differences of these two systems is, that the first one reserves the space from the beginning of the installation, occupying free space in your HOST OS. The second option will increase according to the occupied space inside the guest OS.

The first option is legacy, and today is not real worth to select it. Also, it doesn’t allow increasing the original size of the VDI file. We can solve this problem converting the fixed VDI file to a dynamic VDI.

Steps to increase the size of a VDI file

We will go step by step to increase the size of our GUEST OS. In my case is a DEBIAN Linux Guest OS under Windows 10 as HOST OS. But the process is similar in other configurations.

  1. Previous steps
    1. MAKE A BACKUP OF VDI FILE!!!!
    2. Cloning VDI fixed to VDI dynamic (NOT NECESSARY IF VDI is already dynamic)
    3. Attach the new VDI file to GUEST OS Setup (in HOST OS)
  2. Increase the size of VDI file (in HOST OS)
  3. Increase/modify the configuration of the filesystem inside the GUEST OS (in GUEST OS)
  4. Check the new size and test it on the GUEST OS

1. Previous steps

1.1 Make a backup of your VDI file.

GUI

If you don’t know where it is the VDI file, you can check directly from the GUI of VIRTUALBOX.
Screenshot of Virtual Box Setup Storage window shows the path to the VDI file of the GUEST OS

Under settings of the Guest OS, select the STORAGE, and check the PATH to the VDI file.
In general is something like ~/Virtual Box VMs/GuestOsName/GuestOsName.vdi.

On Windows, is the same, but using its path structure C:\users\youruser\Virtual Box VMs\GuestOsName\GuestOsName.vdi.

COMMAND-LINE

Another option is to open a terminal and use the command:

1
VBoxManage list hdds

This will show all your VDI files associated with disks in your installed guest OS. In our case:
Commandline program: VBoxManage list hdds, output show details of VDI harddisk configured in VirtualBox

You can see the path to the VDI file, and also some other info, like the UUID associated.

NOTE: VBoxManage is a commandline application installed with VirtualBox. In a Windows host, you can find it in the folder of the installation, by default: C:\Program Files\Oracle\Virtual Box\VBoxManage.exe. In other host operating system, like *nix family. You can use whereis VBoxManage, but probably is accessible directly. To avoid repetition, add it to your path or use the fullpath to the program when need it.

Too make a backup you just need to copy the VDI file in a safe place when the Guest OS is not running. Don’t skip this step unless you don’t care of loosing everything inside the Guest OS if something from here goes wrong.

1.2 Cloning a fixed VDI file to a dynamic file

This step is needed only if your VDI file was created with fixed size. If you already know that your VDI is a dynamic variant, then, you can jump to the next section.

The fixed variant was useful for old magnetic harddisk. But, this fixed variant of VDI files can not increase its size.

Magnetic hard disks today are mostly for backups, NAS systems, or directly, save permanent files like media files. When your computer has a SSD, and enough space for your GUEST OS, a fixed size VDI format for your Guest OS doesn’t have practical sense. The original reason was to avoid common fragmentation of big files in HDDs.

COMMAND-LINE

To check the format, we can use again VBoxManage

1
VBoxManage showmediuninfo <uuid|filename>

We can indicate the UUID or the filename (with full or relative path).
Get the format of your VDI file from the commandline using VBoxManage
As you see, in this example, the VDI file is already Dynamic. So we don’t need do to do anything else. If your case is the same, go the section 2

GUI

Another option, is using directly the GUI of Virtual Box. You can open the tool Media, and check the VDI file, and then, directly selecting the tab Information after clicking on Properties.
VirtualBox tool Media Disks to check the format of the VDI file

In this case, I show an old VDI file that was fixed size (Buster64c.vdi), and I already cloned in a new file and resized (Bullseye.vdi), that now is dynamic. So, that is the reason because is not attached to any Guest OS. In the case that something would go wrong. You could always attach the original file to your Virtual Machine to avoid losing your data.

ONLY INSTRUCTIONS TO CLONE FROM THE COMMAND-LINE

The command that you need to clone a VDI file is:

1
VBoxManage clonemedium <uuid|inputfile> <uuid|outputfile> [options]

For more details check Oracle’s VirtualBox website VBoxManage user guide docs

As an example, I’m going to use the old Buster64c.vdi as input or source file, and Bullseye.vdi as output. In your case, you must use the UUID that corresponds to your old file, or the filename. Unless that you want to overwrite an existing file, you must indicate a new filename for the new clone.

The default options already create a file with VDI format and dynamic variant. Then, just indicating the inputfile and the outputfile, we get a new clone with the dynamic variant.

1
2
VBoxManage clonemedium Buster64c.vdi Bullseye.vdi
VBoxManage clonemedium Buster64c.vdi Bullseye.vdi --format=VDI --variant=Standard

Selecting the first or the second line makes no difference except the number of keystrokes.

After a few seconds, the command is complete and we have a new clone dynamic of our old fixed VDI.

1.3 Attach the new VDI file to GUEST OS (in HOST OS)

The next step is attaching the new cloned dynamic VDI file to our Virtual Machine.
To do that, we go to properties of the Virtual Box under the section storage, and select the new VDI file.

  1. We have to detach the old one,
  2. Then attach the new one.
  3. Keep the original port association.

VirtualBox Gui storage properties of the Virtual Box, detach old VDI

As I said before, all of this is not necessary if you had originally a dynamic variant of the VDI file.

2. Increase the size of VDI file (in HOST OS)

Now, we are ready to increase the size of the VDI file.

GUI

We can use the GUI to enlarge it or VBoxManage (and enough space in our Host hardrive).

Rembember to shut down any Guest OS that could be using your VDI file.

In the MEDIA TOOL from the Virtual Box GUI application, selecting the VDI file, Properties, and the tab Attributes you have a slider that allows you to select the new size. Then APPLY, and your VDI file should be increased when the operation ends.

VirtualBox GUI operations to increase the size of VDI file

COMMAND-LINE

The other option, is to use VBoxManage modifymedium.

1
VBoxManage modifymedium <uuid|filename> --resize x

Where x is the new size in megabytes.
For example, if we want to increase my the Xubuntu 18.04.5 32b.vdi from 2 GB to aproximately 40 GB, we can use this instruction:

1
VBoxManage modifymedium 0b2ac595-8227-4c53-9d91-896aa2171194 --resize 40960

Note that resizing to smaller size is most of time not allowed.

3. Increase/modify the configuration of filesystem inside the VDI (in GUEST OS)

Now, we can check that the Virtual Box is running, but before, to get profit of the new size of the new virtual disk image, we need to inform the Guest OS. This can be done with loading an iso image of GPARTED to boot in Guest OS. There are other solutions.

Is beyond the scope of this article to tell you how to modify or adapt the disk partition of the virtual device inside your Guest OS.
Depends of several factors, in case that your virtual device is only one drive and one partition, then is pretty straightforward: you must increase the size of that device and partition using the unnallocated space.

In other case, where your file disk partition table has several sections, you should know how to reorganize them without deleting any old partition with your data. Deleting one partition and then creating a new one will wipe out the data inside that partition. Be careful. And check the documentation of your partition tool.

4. Check the new size and test it on the GUEST OS.

If last part were ok, then you can see the new size inside the VirtualBox GUI or using VBoxManage mediumiunfo.

Now, we can boot your Guest OS, and check inside if everything is working as before, but the size of your filesystem has increased as we configured in the previous step with the partition disk tool.

After some testing, we are safe that our Guest OS has more space inside.

Please, feel free to send me any suggestion.

I want to end mentioning the answers in Superuser figuring out how to solve this problem.

How to make accessible a site from the Guest OS to the Host OS with Virtualbox

In this post, we are going to explore how to make accessible a website that we are developing in the host OS from the guest OS (Debian 11 Bullseye).

Stack and Configuration

  • VirtualBox (Host OS: Windows 10, Guest OS: Debian 11)
  • Inside the Guest OS:
    • Apache 2

Steps

1. Creating the VirtualHost inside the Guest.

Apache allows you to configure a VirtualHost that allows the user to access a website from their computer having a custom URL, IP, etc. For example, let’s say your company has contracted me to develop the yourcompany.com website. To develop and test it in a pseudo-production environment, I can create something similar to yourcompany.local with a VirtualHost.

To do that, we have to:

  1. Create a folder to save the VirtualHost.
  2. Deploy the website to that folder.
  3. Configure the VirtualHost inside the Guest OS.

1.1 Create a folder to save the VirtualHost.

In general, you can do that in any place on your filesystem. However, I recommend doing this in the folder /var/www/yoursite. To avoid conflicts or issues with root user, we create the folder, and then we change the ownership to our local user.

1
2
3
$ cd /var/www
$ sudo mkdir yoursite
$ sudo chown youruser:youruser yoursite

1.2 Deploy the site on the folder

We can either clone our Git repository into the folder or deploy the necessary files to properly run the site. The specific details of configuring a site depend on the project and are beyond the scope of this article.

We can assume that the public files needed to access the site are going to be in: /var/www/yoursite/public. If your site uses a different folder, you need to adjust the path accordingly. If a folder needs to have its permissions changed to be writable, for example during the initial configuration, we can set that folder as writable by the www-data group.

1
2
3
$ cd /var/www/yoursite
$ chown -R youruser:www-data writablefolder
$ chmod -R g+w writablefolder

1.3 Configure the VirtualHost inside the Guest OS.

Now, we need to configure the VirtualHost. To do that, we can follow these steps:

  1. Create a configuration file for your site in /etc/apache2/sites-available/yoursite.conf. How to configure this file is beyond the scope of this post, but you can use an example that usually works for me. Just create it using your preferred editor with root privileges:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<VirtualHost *:80>
ServerName yoursite.local
ServerAlias www.yoursite.local
ServerAdmin admin@yoursite.local
DocumentRoot /var/www/yoursite/public

<Directory "/var/www/yoursite/public">
AllowOverride All
</Directory>

ErrorLog ${APACHE_LOG_DIR}/yoursite.log
CustomLog ${APACHE_LOG_DIR}/yoursite.log combined
# Header set Access-Control-Allow-Origin "*"
</VirtualHost>
  1. Enable the site with this command: $ sudo a2ensite yoursite
  2. Reload Apache2 with: $ sudo systemctl reload apache2
  3. Make it accessible by including an entry in the /etc/hosts like this, choosing a unique IP for each site you are configuring:
1
2
3
127.0.0.1   localhost # DO NOT CHANGE THIS
127.0.1.1 yoursite.local
127.0.1.2 anothersite.local

With all these steps completed, from within your Guest OS, you can access this site by entering http://yoursite.local in the URL bar of your browser.

2. Make accessible in the HOST OS

Whether you prefer to develop and use tools installed in your Host OS because they are quicker and faster than those running in a virtualized environment, as well as better simulate access to a remote server, VirtualBox lets you access the network of your Guest OS in different ways. The simplest, and honestly, the best method for that same reason, is configuring a NAT forwarding system, where you configure the internal address that we configured before as an internal local IP address. To do that:

2.1 Add an entry in the hosts file in the HOST OS

using the same value as in the GUEST. If your HOST is running Windows, you can access this file with administrator privileges at the default path: C:\Windows\System32\drivers\etc\hosts. Then, add this entry:

1
127.0.1.2	yoursite.local

2.1 Add a new rule to the NAT table of the Guest OS in VirtualBox

Inside the configuration/preferences of the VirtualBox Guest OS, you can access the NETWORK configuration, and add a new NAT Port Forwarding rule clicking on Advanced. In our case, this should be the data:

Name Protocol Host IP Host Port Guest IP Guest Port
yoursite.local TCP 127.0.1.1 80 80

Finally, you can access on the Host OS using your favourite browser to the site hosted in the Guest OS.