Accessing External Hard Drives in a Virtual Machine
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:
- 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.
- Install the extension pack for VirtualBox.
- Create a virtual machine and install your Linux distribution of choice.
- 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.
- 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.
- Shut down the virtual machine.
- Attach the disk to your computer. Don't format/initialize the disk and don't even attempt to mount it!
- Under the VirtualBox settings for your virtual machine, go to Ports and switch to the USB tab.
- Check Enable USB Controller and select the corresponding controller version of your drive.
- 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.
- Run your Linux virtual machine.
- 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, zuluCrypt, VeraCrypt, or a file manager in case you installed a desktop environment.
- Read or write to files on the mounted partition inside your virtual machine, as you please.
- 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.
- 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.