Accessing External Hard Drives in a Virtual Machine

2017-12-11

This post explains how to access data stored on an external hard disk drive from within a virtual machine. The use case is to read from and write to files on external disks whose filesystem the host operating system doesn't support. For example, if your host machine runs Windows or macOS, you won't be able to natively access btrfs partitions or ext2 TrueCrypt volumes. Never format/initialize your external hard drive, as doing so will result in data loss! Moreover, don't attempt to mount your hard drive on your host operating system, since doing so might lead to data loss as well.

The following steps describe how to mount the partitions from an external USB drive on a Linux distribution running inside a virtual machine using VirtualBox:

  1. Download and install the latest stable version of VirtualBox for your host operating system. At the time of writing, this is version 5.2.2.
  2. Install the extension pack for VirtualBox on the host. Update: As of VirtualBox 7, the extension pack is not needed anymore.
  3. Create a virtual machine and install your Linux distribution of choice.
  4. If necessary, install the packages for accessing the specific filesystems inside your virtual machine. For example, zuluCrypt supports a variety of encrypted filesystems and offers a command-line interface as well as a graphical user interface.
  5. If you intend to share folders between the virtual machine host and guest, install the VirtualBox Guest Extensions on your Linux guest and follow the official instructions.
  6. Shut down the virtual machine.
  7. Attach the disk to your computer. Don't format/initialize the disk and don't even attempt to mount it!
  8. Under the VirtualBox settings for your virtual machine, go to Ports and switch to the USB tab.
  9. Check Enable USB Controller and select the corresponding controller version of your drive. Update: Select the USB 3.0 (xHCI) controller as long as the guest supports it, even if the host lacks support.
  10. Under USB device filters, go to the icons on the right and select the second icon from the top, i.e., click the + button that allows you to add a filter whose values are set to the values of a USB device which is attached to the host. Choose your USB drive as the device. Update: You may remove everything except for the name, vendor ID, and product ID. At this point, you may unplug the drives from your host, run your Linux virtual machine, and once the virtual machine has fully booted, plug the disks in again. Never mount the disks on the host.
  11. Run your Linux virtual machine. Update: With VirtualBox 7 on macOS hosts, attaching USB drives to the guest might fail with a VERR_SHARING_VIOLATION error.
  12. In your virtual machine, mount the partition of your external drive using the appropriate tool as read-only or read-write. This tool can be mount(8) on the command line, cryptsetup, zuluCrypt, VeraCrypt, or a file manager in case you installed a desktop environment.
  13. Read or write to files on the mounted partition inside your virtual machine, as you please.
  14. In your virtual machine, unmount the partition using a suitable tool. For example, the command umount(8) allows you to unmount filesystems on the command line.
  15. Detach the disk from your computer. There is nothing to do on the host operating system, since you hopefully didn't mount the disk there.