How To Attach A USB To VM Via the USB Name?

I’m very green when it comes to the command line (especially scripts)… but I do know that it’s a bit more secure to attach individual partition blocks (rather than the entire an USB device) to a VM (assuming it’s a USB drive and not some other type of device).

So I would be inclined to try to use the device ID (‘devid’) and attach it with qvm-block. Of course, your usb VM might assign a different device node depending on the other devices connected to your USB controller. So I might try to grep that info using qvm-block and a constant name for the USB device. For example, if I issue:

qvm-block

I might get the following result:

sys-usb:sda      STORAGE_DEVICE ()
sys-usb:sdb      Flash_Blu_30 (backup)

But I only want to attach sys-usb:sdb However, the devid (sdb) might be called something else next time I insert my drive. I do know the drive is always called “Flash_Blu_30”, so I use that to grep the devid info to a variable using something like:

blockvar=qvm-block | grep -Po '(?<=sys-usb:).*(?= Flash_Blu_30)'

which returns everything in between sys:usb and Flash_Blu_30. In this case, “sdb”.

Then I can just use the following to attach the drive partition to my <appVM>:

qvm-block attach <appVM> sys-usb:$blockvar

This example would mean you would need a unique script for different USB sticks but grep is very powerful. I’m sure you could come up with a single script with input options that allow you to extract the devid you need to attach the necessary block partitions of different USB drives to your VMs of choice.

1 Like

You’re running this in dom0, correct?

Yes… dom0 - with the USB drive plugged in.

Can you get this to see a card reader port as well?
(qvm-block shows the attached USB HD, but it only shows the SD card if I first attach the SD card to an AppVm.)

@Emily

Can you get this to see a card reader port as well?

I assume you have sys-usb set up (ideally a disposable VM… which was an option when installing Qubes but easy enough to set up manually). If so, dom0 can’t see USB drives. They are hidden from dom0 for security. So sys-usb acts as the ‘eyes’ for dom0 to ‘see’ attached devices and blocks.

If you start sys-usb and plug in a drive, the blocks will show up with qvm-block of qvm-device block (same thing) in dom0. Likewise, if you insert an SD card into your reader port, the blocks will show up with the qvm-block command in dom0 (as long as sys-usb is running). No need to “attach” the reader or blocks to another AppVM (which is most easily done via the GUI tray icon).

Short answer: Yes - as long as the PCI controller for the SD reader is connected to sys-usb.

(The new Librem 14 has the SD reader on the USB device controller.)

Will this run at startup in sys-usb?

And when I then later on startup the VM it will attach automatically?