Estou usando este código para anexar um novo qcow
a um /dev/nbd
:
for x in /sys/class/block/nbd[0-9]{1,} ; do
S=$(cat $x/size)
if [ "$S" = "0" ] ; then
dev_name=$(basename $x)
${SUDO} qemu-nbd -d /dev/${dev_name} || true
echo "connecting ${QCOW_FILE} to /dev/${dev_name}"
${SUDO} qemu-nbd -c /dev/${dev_name} ${QCOW_FILE} && IS_MOUNTED="yes" || ${SUDO} qemu-nbd -d /dev/${dev_name}
[[ "${IS_MOUNTED:-no}" != "yes" ]] && continue
break
fi
done