qemu-mount - Tools for mounting QEMU images
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qemu-part 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/sh
  2. #
  3. # show partition table of qemu image file
  4. #
  5. prog=$(basename "$0")
  6. # print error message
  7. error() {
  8. echo "$prog:" "$@" 2>&1
  9. exit 1
  10. }
  11. # check command line
  12. if [ $# -ne 1 ] || [ "$1" = "-h" ] || [ "$1" = "-?" ]; then
  13. cat << EOF >&2
  14. Show partition table of qemu image file
  15. Usage: $prog qemu_image
  16. EOF
  17. exit 1
  18. fi
  19. image="$1"
  20. # use sudo for unprivileged users
  21. SUDO=
  22. [ "$(id -u)" -ne 0 ] && SUDO=sudo
  23. # check image
  24. [ -e "$image" ] || error "$image: no such file"
  25. [ -f "$image" ] || error "$image must be a regular file"
  26. img_fmt=$(qemu-img info "$image" | sed -n 's/^file format: \(.*\)/\1/p')
  27. [ -n "$img_fmt" ] || error "$image has unknown format"
  28. # load kernel module, check if sudo works
  29. $SUDO modprobe nbd || exit $?
  30. # lock
  31. umask=$(umask); umask 0
  32. (
  33. flock -w 5 9 || error "Can't get mount lock"
  34. umask "$umask"
  35. # find free nbd device
  36. nbd_size=1
  37. for nbd in /sys/class/block/nbd*; do
  38. nbd_size=$(cat "${nbd}/size")
  39. [ "$nbd_size" -gt 0 ] || break
  40. done
  41. [ "$nbd_size" -gt 0 ] && error "no free nbd devices"
  42. nbd=$(basename "$nbd")
  43. # qemu-nbd
  44. $SUDO qemu-nbd -c "/dev/$nbd" -r -f "$img_fmt" "$image" || exit
  45. # show partition table
  46. $SUDO fdisk -l "/dev/$nbd"
  47. ret=$?
  48. sync
  49. $SUDO qemu-nbd -d "/dev/$nbd" > /dev/null 2>&1
  50. exit $ret
  51. ) 9>> /var/lock/qemu-mount.lock