Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next free device option for qemu-nbd

Tags:

qemu

Is there an option for the qemu-nbd command to get the next free, i.e. unused NBD like losetup -f does? The manpage of 0.0.1 (which is version of the currently stable release 1.7.0 of qemu) doesn't mention anything.

like image 842
Kalle Richter Avatar asked Mar 20 '14 13:03

Kalle Richter


1 Answers

You can query attributes about nbd devices in sysfs.

For example:

cat /sys/class/block/nbd0/size

Will return 0, or the size of the mapped image file otherwise, if /dev/ndb0 is in use.

So you could iterate each device until you find one with 0 and attempt to try that with qemu-nbd.

Something like this should do it:

for x in /sys/class/block/nbd* ; do
  S=`cat $x/size`
  if [ "$S" == "0" ] ; then
    qemu-nbd -c /dev/`basename $x` some_file.img
    break
  fi
done
like image 107
6EQUJ5 Avatar answered Jan 01 '23 11:01

6EQUJ5