Saturday, October 26, 2019

Creating a Virtual USB Device, Backed By Image File

Similar the way you virtualize hard disks, you could also virtualize USB devices. I’ve seen very little examples on this topic, hence detailing the steps on Virtualizing a USB.

You are having the below advantages, compared to the physical redirection of a USB device to a virtual machine.

  1. Complete your POCs much faster (You are now working with Image files in Hard disks, not USB devices)

  2. Test your Virtual USB devices in Virtual Machines, before restoring it to the actual physical USB Device.  

  3. Backup/Restore means, simply copying/restoring the image file (rather than the physical USB Device) – This helps not to corrupt the original physical USB device, until you get things right

  4. Start your POC, even without a real USB device and later copy the image to a real USB device, once you’re done with the changes

Eg: The below command create a 1GB image file under a second, thats blazing fast
dd if=/dev/zero of=USB-1GB.img count=1024 bs=1048576


How to create a Virtual USB (In Qemu-KVM)


1. Create an image (img file) of your Physical USB device:

Or just start with an image file without a USB Device

 

This could be easily done through ‘Linux dd’ command, but you could also perform this through Disk GUI

 

Or even better, you could start without a USB file, but simply create an image file on the fly using 'dd’ command. The below command creates a 1GB image file. Once you’re done with the tests/experiments restore the image back to a real physical USB file

dd if=/dev/zero of=USB-1GB.img count=1024 bs=1048576


2. Attach the USB image file, to Virtual Machine

 

You could perform this through directly editing the qemu config file for the virtual machine:


The below figure shows adding an USB Image file (prepaid earlier), or directly pointing to the physical USB device (without an image file)


Or through VirtManager GUI

The below figure shows adding an USB Image file (prepaid earlier)


The below figure shows adding a virtual USB device, directly pointing to the physical USB device (without an image file)

This option could also done in VirtualBox, as VirtualBox provide option to passthrough a Physical USB Device using a intermediate VMDK file. This could perform through the below command


VBoxManage internalcommands createrawvmdk -filename USB-8GB.vmdk -rawdisk /dev/sdc




3. Have your tests/experiments on this USB image file with in the Virtual machine

 

Say, Creating a Multi Boot Live USB, mentioned in this post.


 

4. Once done, restore the Image back to your Physical USB device

 

Again this could be done through ‘Linux dd’ command, but the same can also be done through Disk GUI


 


 


No comments:

Post a Comment