How to get a column with a list of domains to which devices are connected? qvm-device list

to identify “BACKEND:DEVID”, the script outputs the first column using awk

qvm-block ls | awk '{print $1}'

later my program should find the domain name to detach the device, but device names can contain spaces and awk ignores the “USED BY” column and displays the second part of the device name instead of displaying the domain name.

For example:

qvm-block ls
BACKEND:DEVID	   DESCRIPTION		   USED BY
sys-usb:1-2	       (256Gb)		       disp666   (read-only=no, frontend-dev=xvdi)
sys-usb:2-2	       Sony_Inc. (128Gb)   disp777   (read-only=no, frontend-dev=xvdi)

qvm-block ls | awk '{print $3}'
disp666
(128Gb)

qvm-block ls | awk '{print $4}'
(read-only=no,
disp777

In this story, what annoys me the most is the illogic of the developers. Why specify the domain from which I want to detach the device? The device can be attached to only one domain and it would be enough to specify the device that you want to detach.

qvm-block detach sys-usb:1-2

Even awk '{print $3}' ?

'{print $3}' works for devices with names without spaces

Ahh got it.

Try this then:

qvm-block ls | cut -d' ' -f 3-

it’s not clear what your code does but it doesn’t work. you need to display a column with domains

It displays everything after column 3 included. I tried with a random text and it worked for me.

Can you provide some example rows? Feel free to change names, just preserve the structure.

For example:

qvm-block ls
BACKEND:DEVID	   DESCRIPTION		   USED BY
sys-usb:1-2	       (256Gb)		       disp666   (read-only=no, frontend-dev=xvdi)
sys-usb:2-2	       Sony_Inc. (128Gb)   disp777   (read-only=no, frontend-dev=xvdi)

qvm-block ls | awk '{print $3}'
disp666
(128Gb)

qvm-block ls | awk '{print $4}'
(read-only=no,
disp777

Thank you for clarifying.

Try this:

qvm-block ls | sed 's/.*(.*b)//g' | awk '{print $1}'

It should work if the description always ends with ([size] [unit]).

qvm-block ls | sed 's/.*(.*b)//g' | awk '{print $1}'
disp666
sys-usb:2-2

the description is always different, there are many devices, there may be more than one space in the name. otherwise why should I write a script? for two flash drives?)

The code I provided works with as many spaces as you want, provided the description ends with b). If that’s not the case you should let me know.

I have tried multiple situations, even changing descriptions, and it works (with cat instead of qvm-block).

This is more generic:

qvm-block ls | sed 's/.*b)//' | awk '{print $1}'

Are the VMs always disposables?

You can do it by finding out the offset of “USED BY” substring in first line and then parse all other lines with this offset.

do not compare cat to qvm-block ls. try on live premender, not on files. I don’t understand where you saw the “b”? the disk label can be any:

qvm-block ls
BACKEND:DEVID	   DESCRIPTION		   USED BY
sys-usb:1-2	       (My cats3)		   disp666   (read-only=no, frontend-dev=xvdi)
sys-usb:2-2	       Sony_Inc. (128Gb)   work1     (read-only=no, frontend-dev=xvdi)

domain names not only disp*

sounds cool. Run qubes and try to do it

Both your examples:

DESCRIPTION
(256Gb)
Sony_Inc. (128Gb)

They both end in b).

If you’re asked for a generalized example and all the rows in the sample you’ve provided contain a pattern, I’m assuming that pattern is contained in all other rows.

This works.

qvm-block ls | grep read | sed 's/(read.*//' | awk '{print $NF}'

Work’s for me. Thanks!

Work’s for me. Thanks!