Skip to content

Instantly share code, notes, and snippets.

@dosaboy
Last active March 28, 2019 16:36
Show Gist options
  • Select an option

  • Save dosaboy/7fff340efdd1787e58eb27cc78e12360 to your computer and use it in GitHub Desktop.

Select an option

Save dosaboy/7fff340efdd1787e58eb27cc78e12360 to your computer and use it in GitHub Desktop.
#!/bin/bash -eu
#
# Origin: https://gist.github.com/dosaboy/7fff340efdd1787e58eb27cc78e12360
#
# Authors:
# - [email protected]
# - [email protected]
#
# This must be executed on the hypervisor node where the instance is running
#
# Returns /dev/<device> names for any block devices attached to the
# instance that are backed by an rbd image.
(($#==1)) || \
{ echo "Usage: `basename $0` <virsh domain>";
exit 1; }
domain=$1
dpkg -s jq| grep -q "Status: install ok installed" || sudo apt install -y jq
get_server_rbd_blkdevs ()
{
domain=$1
dout=`mktemp -d`
out=`virsh qemu-monitor-command ${domain} '{"execute":"query-block"}'| jq -r ".return[]"`
readarray -t blkdevinfo<<<`echo "$out"| jq -r '"__DEV__" + .device + "__FILE__" + .inserted.file'`
# each line should correspond to a different device.
for line in "${blkdevinfo[@]}"; do
virtio_blkdev=`echo $line| sed -r 's/.*__DEV__(.+)__FILE__.+/\1/g;t;d'`
# First pass is libvirt 4.0.0 (Bionic) format which contains json in the "file:" section
driver=`echo $line| sed -r 's/.+json:(.+)/\1/g;t;d'| jq -r .file.driver`
# Else libvirt 2.5.0 (Xenial+Ocata UCA) format i.e. no json
[ -n "$driver" ] || driver=`echo $line| sed -r 's/.+(rbd):.+/\1/g;t;d'`
if [ "$driver" = rbd ]; then
# Again try bionic format first
image=`echo $line| sed -r 's/.+json:(.+)/\1/g;t;d'| jq -r .file.image`
# Then try Ocata format
[ -n "$image" ] || image=`echo $line| sed -r 's/.+rbd:.+\/(.+):id=.+/\1/g;t;d'`
else
# we only care about rbd devices
continue
fi
mkdir -p $dout/${domain}
echo $virtio_blkdev > $dout/${domain}/$image
done
if [ -d "$dout/${domain}" ]; then
for image in `ls $dout/$domain`; do
virsh domblklist $domain| egrep "$image\$"| awk '{print $1}'
done
fi
rm -rf $dout
}
get_server_rbd_blkdevs $domain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment