Skip to content

Instantly share code, notes, and snippets.

@mrngm
Created January 6, 2018 12:37
Show Gist options
  • Save mrngm/5357a8544191f627fa9bc2dd5341767f to your computer and use it in GitHub Desktop.
Save mrngm/5357a8544191f627fa9bc2dd5341767f to your computer and use it in GitHub Desktop.
Shows Vdev->block device mappings for a Xen guest
#!/bin/bash
# Written in 2018 by Gerdriaan Mulder <hello[at]moeilijklastig(dot)nl>
# This tool shows the mapping between a Xen Vdev attached to a Xen guest.
# Example output that uses an LVM backend:
#
# # ./list-vm-disks myguest
# Domain -> ID: myguest -> 42
# Listing Vdev -> LV mappings...
#
# Vdev 51714 -> /dev/vpsdisks/myguest.myhostna.me-disk
# Vdev 51713 -> /dev/vpsdisks/myguest.myhostna.me-swap
# Vdev 51729 -> /dev/vpsdisks/myoldguest.myhostna.me-disk
print_usage() {
echo "Usage: $0 [-h|--help] <domain>"
echo ''
echo '<domain> A (unique part of a) domain name to examine'
echo ''
echo 'This tool shows a mapping between a Xen Vdev (as returned by'
echo '`xl block-list <domain-id>` and the attached block device, which'
echo 'is usually an LVM logical volume.'
echo ''
echo 'The Vdev number can then be fed to a tool like `xl block-detach`:'
echo '`xl block-detach <domain-id> <Vdev>`; this removes the block'
echo 'device from <domain-id>.'
echo ''
echo 'Attaching a block device can be done like:'
echo '`xl block-attach <domain-name> "phy:/path/to/lvm/disk,xvdX,<r|w>"`'
}
ARGC="$#"
DOMNAME="$1"
if [[ ! "$ARGC" -eq 1 ]]; then
print_usage
exit 0;
elif [[ "$ARGC" -eq 1 && "${DOMNAME:0:1}" == "-" ]]; then
print_usage
exit 0;
fi
DOMID="`xl list | grep ${DOMNAME} | tail -n 1 | awk '{print $2}'`"
echo "Domain -> ID: $DOMNAME -> $DOMID"
echo "Listing Vdev -> LV mappings..."
echo ''
VDEVS="`xl block-list ${DOMID} | tail -n +2 | awk '{print $1}'`"
for j in ${VDEVS}; do
LV="`xenstore-read /local/domain/0/backend/vbd/${DOMID}/${j}/params`"
echo "Vdev $j -> $LV"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment