Python: Trying to build (or access) a list of block devices

I can get a list of devices with qvm-block list, muck around with it in a bash script, and pass it to a python program. But there seems to be no way to get the size of the devices (unlike many other utilities there’s no way to specify what columns you want to see…or if there is, it’s totally undocumented; it doesn’t show up in the man page nor is it printed via --help). So this complete python noob is trying to figure out how to do this inside of python, since I’m just going to hand the list to a python script anyway.

So I am trying to call whatever classes, etc. to get a list of devices. I found /usr/lib/python3.11/site-packages/qubesadmin/devices.py m which looks like it MIGHT be something another python script can pull in to get information on all of the devices that exist. However, it seems to want to operate on an individual vm basis (and so I’d need to call it once for dom0 and once for sys-usb). The problem is this takes a VM object…and I can’t figure out where that comes from! Evidently, I’d have to import something else, declare an object, look up dom0 (or alternatively, loop through all vms) and then sys_usb, and pass those objects to DeviceManager in the hopes I can get a list out of it.

Part of my difficulty here is I am in the middle of a setup I don’t know, so my question might be incoherent to someone who actually does understand the system.

import qubesadmin

app = qubesadmin.Qubes()

dom0 = app.domains["dom0"]
sys_usb = app.domains["sys-usb"]
# or iterate over all
for vm in app.domains.values():
    # do something

And having the vm object, you can get devices

for dev in vm.devices["block"].available():
    # do something
    print(f"{dev!s}: {dev.data}")

You can find some docs at qubesadmin package — Qubes Admin client v4.2.17-0-gfda74dc documentation (although not much really…)

That’s VERY helpful, thank you!

Is the source code for whatever class dev and vm (and vm.devices) are in your example, on dom0 somewhere?

Also, is there a quick way to get the label in addition to the size?

(Never mind, found it. dev.description, but you need to extract what’s in the parens.)

See the docs. As for dom0, it’s in /usr/lib/python3*/site-packages/qubesadmin/ look for vm/__init__.py and devices.py.

1 Like