How to Use a USB Drive as a Hard Disk in VirtualBox (Linux)

VirtualBox is a powerful virtualization tool that allows you to run multiple operating systems on your computer. One of the advanced features is the ability to use a USB drive as a virtual hard disk. This can be particularly useful for running portable operating systems or testing environments directly from a USB drive. In this tutorial, we will guide you through the steps to set up a USB drive as a hard disk in VirtualBox.

Step 1: Connect the USB Drive to Your Host Machine

  • Plug in your USB drive to your computer.

Step 2: Identify the USB Drive

  • Open a terminal.
  • Use the lsblk command to list all storage devices and identify your USB drive. For example:
lsblk
  • Note the device name of your USB drive (e.g., /dev/sdb).

Step 3: Create a VirtualBox VMDK File for the USB Drive

  • Open a terminal.
  • Use the VBoxManage createmedium command to create a VMDK file that points to the USB drive:
sudo VBoxManage createmedium disk --filename /path/here/usb.vmdk --format VMDK --variant RawDisk --property RawDrive=/dev/sdx

Replace /path/here/usb.vmdk with the path where you want to create the VMDK file and /dev/sdx with the device name of your USB drive.

Step 4: Set Permissions (Linux-specific)

  • Change the permissions of the USB drive to ensure VirtualBox can access it:
sudo chown user_here:user_here /dev/sdx
sudo chmod 666 /dev/sdx
  • Replace /dev/sdx with the device name of your USB drive.

Step 5: Add the VMDK to Your Virtual Machine

  1. Open VirtualBox.
  2. Select the virtual machine you want to use with the USB drive.
  3. Click on Settings.
  4. Go to the Storage section.
  5. Click on the Controller: SATA or Controller: IDE (depending on your setup).
  6. Click on the Add Hard Disk icon (a small hard drive with a green plus sign).
  7. Select Choose existing disk.
  8. Browse to /path/here/usb.vmdk and select it.
  9. Click OK to close the settings window.

Step 6: Ensure the Disk is Not Mounted

  • Make sure the USB drive is not mounted by the host system:
sudo umount /dev/sdx

Step 7: Start the Virtual Machine

  1. Start your virtual machine.
  2. The virtual machine should recognize the USB drive as a hard disk.

Troubleshooting:

  • Permission Issues: Ensure you have set the correct permissions on both the VMDK file and the USB drive.
  • Incorrect Path: Double-check the path to the VMDK file and the device name of the USB drive.
  • Unmount the Drive: Make sure the USB drive is not mounted on the host system.
  • Check Logs: If you encounter issues, check the VirtualBox logs located in ~/.config/VirtualBox/VBoxSVC.log for more details.

Conclusion:

Using a USB drive as a hard disk in VirtualBox can extend the flexibility of your virtual machines. Whether for portability, testing, or simply using additional storage, following these steps will help you set up your USB drive quickly and efficiently. Happy virtualizing!