Unable to insert the virtual optical disk in an Ubuntu virtual machine

When you try to install a new version of the VirtualBox Guest Addition on an Ubuntu virtual machine you may get the error message

Unable to insert the virtual optical disk C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso into the machine Ubuntu 64.

Could not mount the media/drive ‘C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso’ (VERR_PDM_MEDIA_LOCKED).

Result Code: E_FAIL (0x80004005)
Component: ConsoleWrap
Interface: IConsole {872da645-4a9b-1727-bee2-5585105b9eed}
Callee: IMachine {85cd948e-a71f-4289-281e-0ca7ad48cd89}

To mount the new CD, first eject the currently mounted virtual CD

  1. Click the CD icon on the left side of the screen
  2. Click the eject icon next of the currently mounted Guest Addition CD

Raw-mode is unavailable courtesy of Hyper-V error in VirtualBox

For VirtualBox to be able to start virtual machines, Hyper-V has to be turned off in Windows 10.

If you get the error message when you try to launch a virtual machine in VirtualBox

Raw-mode is unavailable courtesy of Hyper-V

Check the Hyper-V setting

  1. Open a command prompt as an administrator,
  2. Execute the command:
    bcdedit

    The default value of the hypervisorlaunchtype is “Auto”

    For VirtualBox to be able to launch virtual machines, we have to turn off hypervisorlaunchtype

Disable Hyper-V

To disable Hyper-V

  1. Open a command prompt as an administrator
  2. Execute the command
    bcdedit /set hypervisorlaunchtype off
  3. Restart the computer. Do not shut down and start the computer, that does not work.

Enable Hyper-V

Docker for Windows needs the hardware virtualization that Hyper-V provides. To enbale Hyper-V again

  1. Open a command prompt as an administrator
  2. Execute the command
    bcdedit /set hypervisorlaunchtype auto
  3. Restart the computer for the change to take effect.

Connect to a database in a Docker container from a VirtualBox virtual machine

Docker is an emerging technology to launch applications on any guest operating system using any host operating system. The same host can run multiple Docker containers of any type, so one physical or virtual server can serve as a complete mini cloud environment. Most of the time Linux (including macOS) is used as the host operating system.

In this example, we will launch a PostgreSQL Docker container on macOS, and access the PostgreSQL database server from a Windows VirtualBox virtual machine.

Thanks to Robert Lestak for the Docker scripts.

Create a network between the guest VM and the host in Oracle VirtualBox

A VirtualBox virtual machine can share the networking with the host with the NAT setting, or it can be an independent computer on the network with the Bridged setting.

To access the host directly from the guest VM we can set up a host-only network, that only the host and the guest can access. This way we can launch a container on the host and access it from the guest via the host-only mini-network.

To set up the private host-only network

  1. Open the VirtualBox application and shut down all virtual machines
  2. In the File menu select Host Network Manager
  3. On the Host Network Manager window click the Create button
  4. Click the Enable checkbox and the Close button

Connect the virtual machine to the host-only network

The VirtualBox virtual machines can have four virtual network adapters. Always use the first adapter to connect to the host-only network.

  1. Select the virtual machine in the list and click the Settings icon
  2. On the Network tab click the arrow next to the Attached to drop-down
  3. Select the Host-only adapter
  4. The host-only network is automatically selected
  5. Select the second adapter, click the Enable Network Adapter checkbox, and select NAT
  6. Click the OK button to close the window.

Find the IP address of the host machine

  1. Open a terminal on the host machine.
  2. On Mac
    1. enter the following command. Use the IP address range of the host-only network above.
      ifconfig | grep 192.168.56

      inet 192.168.56.1 netmask 0xffffff00 broadcast 192.168.56.255

    2. Based on the output  above, the IP address of the host machine is 192.168.56.1

Launch a PostgreSQL Docker container

  1. Create a docker-compose.yml file. Containers are ephemeral, when we terminate them, all data stored inside the container is lost. We want to keep the database files on our local hard drive, so this configuration will map the /var/lib/postgresql/data directory of the Docker container to the pgdata subdirectory of the current working directory. The 5432 is the default port of the PostgreSQL database server, we will expose it on port 5433 of the host.
    version: '3'
    
    services:
      postgres:
        image: postgres:10
        env_file: .env
        ports:
          - "5433:5432"
        restart: always
        volumes:
          - $PWD/pgdata:/var/lib/postgresql/data
  2. Open a terminal window in the same directory and launch the Docker container
    docker-compose up -d

Connect to the host machine from the guest machine

Test the connection

  1. Open a terminal on the guest machine
  2. Test the connection to the host machine
    ping 192.168.56.1

Configure an ASP.NET Core 2.1 application to connect to the Docker PostgreSQL database server

  1. Change the connection string, and specify the exposed port on the host machine:
    "Server=192.168.56.1;Port=5433;Database=MY_DATABASE;Username=MY_USERNAME;Password=MY_PASSWORD"

 

 

Nested VirtualBox virtual machine on a VirtualBox virtual machine

There can be reasons when we want to run a VirtualBox virtual machine on another VirtualBox virtual machine.

I am setting up a Windows virtual machine to interview job candidates and want to run a small Ubuntu virtual machine on the Windows virtual machine. It is not recommended for performance reasons, but I want to set up a base interview machine that I can wipe and recreate any time for the next candidate.

Besides the performance hit there is one more rule: the nested guest operating system needs to be 32 bit.

Even if you have a 64 bit workstation, and run a 64 bit guest operating system on it, the VirtualBox on the guest operating system can only host 32 bit guest-guest operating systems, because for a 64 bit operating system to run, the host machine needs to have the CPU Extension feature turned on. Currently only physical CPUs have the CPU Extension feature, so even a 64 bit guest virtual machine cannot provide it.

To set up the 32 bit nested virtual machine

  1. Download ISO image of the 32 bit version of Ubuntu from http://releases.ubuntu.com/artful/
  2. Start VirtualBox
  3. Click the New icon to create a new virtual machine
  4. Select Linux (32 bit)
  5. Accept the default memory and hard drive settings

Start the virtual machine the first time

  1. Select the new virtual machine
  2. Click the Start button
  3. Browse to the downloaded 32 bit Ubuntu ISO image
  4. Follow the prompts to set up the operating system

 

Resize the VirtualBox image hard drive

When your VirtualBox virtual machine’s hard drive fills up Virtual Box does not provide a user interface to extend it. If you run VirtualBox on a Macintosh workstation

  1. Stop the virtual machine
  2. On a Macintosh workstation, the virtual machine image files are located at ~/VirtualBox VMs
  3. The name of the subdirectory and the image file matches the name of the machine
  4. Open a terminal window
  5. Navigate to the VirtualBox application directory
    $ cd /Applications/VirtualBox.app/Contents/Resources/VirtualBoxVM.app/Contents/MacOS
  6. Check the current size of the hard drive. Place a backslash in front of every space character in the path.
    VBoxManage showhdinfo ~/VirtualBox\ VMs/Win\ 10/Win\ 10.vdi
  7. Resize the drive. Place a backslash in front of every space character in the path, and specify the size in megabytes, so 100 GB is approximately 100000 MB
    VBoxManage modifyhd --resize 100000 ~/VirtualBox\ VMs/Win\ 10/Win\ 10.vdi

    UMG/Win\ 10\ UMG.vdi
    0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100%

  8. Check the size again to make sure the resizing succeeded.

Extend the volume on the enlarged partition

Windows 10 virtual machine

  1. Start the virtual machine
  2. Click the Start button and the Gear icon to start the control panel
  3. Type admin into the search box and select Administrative Tools
  4. Double-click Computer Management
  5. Select Disk Management
  6. Right-click the partition and select Extend Volume
  7. Click Next on every page of the wizard to extend the volume to the maximum available size.

Git Bash and Command Prompt cannot see the VirtualBox shared folder on Windows 10 guest

When you start a Windows 10 guest machine in VirtualBox on your workstation, you can set up shared folders to seamlessly copy files between your workstation and the Windows 10 guest machine.

The shared folder appears in Windows Explorer as a mapped network location.

For more on this see Configure the Windows virtual machine in VirtualBox

On a Windows 7 guest machine the Git Bash, Command Prompt, and PowerShell windows all can see the mapped E: network drive, so you can change the working directory with

cd E:

and execute commands in the shared folders.

On a Windows 10 guest machine, only the PowerShell window can see the shared folder as a mapped network drive, E:


Command prompt will display the error message

The system cannot find the drive specified

When you try to start the Git Bash window with Git Bash Here, nothing happens.

If you start Git Bash from the menu and try to change the working directory to the E: drive, you get the error message

bash: cd: /e: No such file or directory


Map the shared drive

If you need to use the Command prompt or the Bash terminal, you can temporarily map the shared folder

  1. Open the Command Prompt on the Windows 10 guest machine
  2. Execute the command
net use x: \\vboxsvr\Users

Automate the drive mapping

Mapped drives are user-specific so if you want to use a mapped drive as an Administrator, you have to map is as Administrator.

Enable the visibility of the mapped drives

  1. Start Regedit
  2. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System
  3. Create a DWORD value EnableLinkedConnections with the value 1
  4. Restart the computer

Create the automated process to map the drive of the host computer

  1. Create a new batch file in your Documents folder: map_drive.bat
    net use x: \\vboxsvr\Users
  2. Create a new scheduled task: Map the Mac file system to X
    1. Set the user account to SYSTEM
    2. Run with highest privileges
    3. Configure for Windows 10
    4. Set the trigger to startup
    5. Create a new action

To use the Bash terminal

  1. Open the Bash terminal in a directory on the C: drive
  2. Change to the shared directory with
    cd /x/...

To use the command prompt

  1. Open the command prompt
  2. Change to the shared directory with
    x:
    cd ...

Configure the Ubuntu virtual machine in VirtualBox

Enable shared folders on the virtual machines

To be able to use shared folders between the host ( your workstation ) and the virtual machine.

  • Start the Ubuntu virtual machine in Virtual Box,
  • Select the virtual machine window on your workstation,
  • In the Devices menu of Virtual Box select Insert Guest Additions CD image…
  • Click the Run button,
  • Enter your admin password and click Authenticate,
  • Select the terminal window and press Enter,

  • Restart the virtual machine for the change to take effect,
  • In Ubuntu
    1. Open a terminal window,
    2. In the upper left corner of the desktop click the Search icon,
    3. Type terminal into the search box,
    4. Select the Terminal icon.
  • In the Terminal window create a directory for the mount point
    mkdir ~/host-share
    sudo chmod 777 ~/host-share
  • Mount the Shared Windows location. Get the Machine Folder name from the virtual machine’s Shared Folders tab in VirtualBox

    (If you shared the root of a Windows C: drive, the name is “C_DRIVE”.)

    sudo mount -t vboxsf -o uid=$UID,gid=$(id -g) MY_MACHINE_FOLDER_NAME ~/host-share

Source: https://help.ubuntu.com/community/VirtualBox/SharedFolders

Make the mounting permanent

  1. Create the folder /opt/scripts
  2. Create a new file in the new folder mount_host_share
    #!/bin/sh
    # Mount the Host Share to see the host computer's file system
    echo MY_PASSWORD | sudo -S mount -t vboxsf -o uid=1000,gid=1000 MY_MACHINE_FOLDER_NAME ~/host-share
  3. Make the file executable
    chmod +x /opt/scripts/mount_host_share
  4. On the Ubuntu desktop click the Search icon and type startup, and click the Startup Applications icon
  5. On the Startup Applications Preferences window click Add,
  6. Enter the Name and this Command, and click Save
    /opt/scripts/mount_host_share

The shared drive appears in the File manager.

Relax the password requirement

The virtual machine is already protected by the operating system of the host, your workstation, so it is not necessary to enter the password every time you want to switch to the guest machine. To remove the password requirement

  1. Click the System Settings icon in the sidebar,
  2. Uncheck the Waking from suspend and Returning from blank screen checkboxes.

Configure the Windows virtual machine in VirtualBox

Enable shared folders on the virtual machine

To be able to use shared folders between the host ( your workstation ) and the Windows virtual machine.

  1. Start the Windows virtual machine in VirtualBox,
  2. Select the virtual machine window on your workstation,
  3. In the Devices menu of VirtualBox select Insert Guest Additions CD image…
  4. In the virtual machine start Windows Explorer,
  5. Open the VirtualBox Guest Additions CD,
  6. Start VBoxWindowsAdditions.exe,

  7. On the User Account Control popup click Yes,
  8. Click Next on the setup page,
  9. Click Next on the path selection page,
  10. Click the Install button,
  11. Save all your documents on the virtual machine, and allow the reboot of the virtual machine for the changes to take effect.

The shared folders will be available in the virtual machine’s Windows Explorer under Network locations as the E: drive

Important

Refresh

When an application copies or deletes files and folders in the displayed shared folder, click the Refresh button to see the new state of the directory. Windows usually does not refresh the view of the shared folder.

 

Command window

On a Windows 10 guest machine, the Git Bash and Command Prompt windows cannot see the mapped shared folders. To execute commands in shared folders, use the PowerShell window. For more see Git Bash and Command Prompt cannot see the VirtualBox shared folder on Windows 10 guest

VirtualBox installation and configuration on Windows

Download Virtual Box from https://www.virtualbox.org/wiki/Downloads and follow the instructions to install it.

At the time of writing this was the section where the installer files were referenced

virtual-box-01-download

Run the downloaded installer file and accept all default values.

The installer starts the Virtual Box application.You can close it, we will use other tools to start it in the future.

To be able to launch 64-bit guest operating systems turn off Hyper-V in Windows Features

  1. In the Windows search box enter Windows Features and select Turn Windows Features on or off,
  2. Uncheck Hyper-V and click OK,
  3. Reboot the computer for the setting to take effect.

Create a virtual machine

To create a new virtual machine

Download the  OS image from the manufacturer’s web site.

  1. Start the VirtualBox application
  2. Click the New icon
  3. Enter a name, select the operating system type and version, and set the memory size,
  4. Set the initial size of the virtual hard disk
  5. Select the virtual machine icon and click the Start button,
  6. Click the Browse icon and select the OS image file in the Downloads directory,
  7. Click the Start button

Configure VirtualBox for each virtual machine

 Networking

To share the connectivity of the host computer with the virtual machine, mainly if you use VPN

  1. In Virtual box click Settings
  2. Select Network
  3. Attach the network adapter to NAT

Copy and paste

To enable copy and paste between the virtual machine and the host ( your workstation )

  1. On the General tab of the Settings page select the Bidirectional shared clipboard

Shared Folders

Specify the shared folder on your workstation

  1. In the VirtualBox menu select Preferences
  2. On the Shared Folders tab click the + icon
  3. To share the entire system drive, select the C: drive of your workstation

Configure the virtual machine OS

To configure the virtual machine operating system in VirtualBox, see Virtual machine configuration on the VirtualBox page.

VirtualBox installation and configuration on Macintosh

Download VirtualBox from https://www.virtualbox.org/wiki/Downloads and follow the instructions to install it.

At the time of writing, this was the section where the installer files were referenced

virtual-box-01-download

Run the downloaded installer file and accept all default values.

The installer starts the application.

 

Create a virtual machine

To create a new virtual machine

Download the OS image from the manufacturer’s website.

  1. Start the VirtualBox application
  2. Click the New icon
  3. Click the Expert Mode button
  4. Enter the name, select the operating system type and version, and set the memory size,
  5. Set the initial size of the virtual hard disk, and click the Create button,
  6. Select the virtual machine icon and click the Start button
  7. Click the Browse icon and select the OS image file in the Downloads directory,
  8. Click the Start button,

Configure VirtualBox for each virtual machine

Networking

To share the connectivity of the host computer with the virtual machine, mainly if you use VPN

  1. In VirtualBox select the virtual machine icon and click Settings
  2. Select Network
  3. Attach the network adapter to NAT

If your company use automated software installation and updates, set up a second network connection using the Bridged Adapter. This way the virtual machine will get an independent IP address in the same IP address range as the host machine.

  1. Select the Adapter 2 tab,
  2. Select the Enable Network Adapter checkbox,
  3. Select the Bridged Adapter in the Attached to drop-down,
  4. In the Name drop-down list select the fastest network connection of the host machine.

Copy and paste

To enable copy and paste between the virtual machine and the host ( your workstation )

  1. On the General tab of the Settings page select the Bidirectional shared clipboard

Shared Folders

Specify the shared folder on your workstation

  • In the VirtualBox menu select Preferences
  • On the Shared Folders tab click the + icon
  • Select your home folder on your workstation

Configure the virtual machine OS

To configure the virtual machine operating system in VirtualBox, see Virtual machine configuration on the VirtualBox page.