iarmin

Citrix Xenserver mount-vdi

Jan 29th, 2012
1,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.84 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Copyright (c) Citrix Systems 2008. All rights reserved.
  4. #
  5. # Script which lets you edit the grub.conf in a Linux guest virtual disk.
  6.  
  7. if [ ! -e /etc/xensource-inventory ]; then
  8.   echo Must run on a XenServer host.
  9.   exit 1
  10. fi
  11.  
  12. . /etc/xensource-inventory
  13.  
  14. XE="/opt/xensource/bin/xe"
  15.  
  16. device_number=
  17. grub_conf=
  18. default_file_list="/boot/grub/menu.lst /grub/menu.lst /boot/grub/grub.cfg /grub/grub.cfg"
  19.  
  20. function usage {
  21.     echo "Usage: $0 [-u <VM UUID>] [-n <VM name>]"
  22.     echo "    [-p <partition number>] [-f <filename>]"
  23.     echo
  24.     echo " -u: UUID of the VM boot disk you wish to edit"
  25.     echo " -n: Name label of the VM boot disk you wish to edit"
  26.     echo " -p: Partition number to mount (default: none)"
  27.     echo " -f: Location of bootloader config file to edit"
  28.     echo "       (defaults: ${default_file_list})"
  29.     echo
  30.     echo "Either -u or -n must be supplied to specify the VM to edit"
  31.     echo "Set the EDITOR environment variable if desired (default: nano)"
  32.     exit 1
  33. }
  34.  
  35. while getopts "hu:n:p:f:" opt ; do
  36.     case $opt in
  37.     h) usage ;;
  38.     u) vm_uuid=${OPTARG} ;;
  39.     n) vm_name="${OPTARG}" ;;
  40.     p) device_number=${OPTARG} ;;
  41.     f) default_file_list="${OPTARG}" ;;
  42.     *) echo "Invalid option"; usage ;;
  43.     esac
  44. done
  45.  
  46. # determine a VM UUID
  47. IFS=,
  48. if [ "${vm_name}" != "" ]; then
  49.    vm_uuids=$(${XE} vm-list name-label="${vm_name}" params=uuid --minimal)
  50.    num_vm_uuids=0
  51.    for vm in ${vm_uuids}; do
  52.       num_vm_uuids=$(( ${num_vm_uuids} + 1 ))
  53.    done
  54.    if [ ${num_vm_uuids} -eq 0 ]; then
  55.       echo Unable to find VMs matching name-label: ${vm_name}
  56.       exit 1
  57.    elif [ ${num_vm_uuids} -gt 1 ]; then
  58.       echo Found multiple VMs with name-label: ${vm_name}
  59.       echo ${vm_uuids}
  60.       echo Please specify one of the UUIDs using the -u option
  61.       exit 1
  62.    fi
  63.    vm_uuid=${vm_uuids}
  64. fi
  65.  
  66. # verify that the VM UUID is valid
  67. if [ "${vm_uuid}" = "" ]; then usage; fi
  68.  
  69. checkuuid=$(${XE} vm-list uuid="${vm_uuid}" params=uuid --minimal)
  70. if [ "${checkuuid}" = "" ]; then
  71.     echo Specified VM UUID "${vm_uuid}" does not exist.
  72.     exit 1
  73. fi
  74.  
  75. # select nano as the default editor
  76. if [ "${EDITOR}" = "" ]; then
  77.    EDITOR=nano
  78. fi
  79.  
  80. mnt=
  81. function cleanup {
  82.    if [ ! -z "${mnt}" ]; then
  83.       umount ${mnt} >/dev/null 2>&1
  84.       rmdir ${mnt}
  85.    fi
  86.  
  87.    if [ ! -z "${vbd_uuid}" ]; then
  88.       echo -n "Unplugging VBD: "
  89.       ${XE} vbd-unplug uuid=${vbd_uuid} timeout=20
  90.       # poll for the device to go away if we know its name
  91.       if [ "${device}" != "" ]; then
  92.           device_gone=0
  93.           for ((i=0; i<10; i++)); do
  94.               echo -n "."
  95.               if [ ! -b ${device} ]; then
  96.                   echo " done"
  97.                   device_gone=1
  98.                   break
  99.               fi
  100.               sleep 1
  101.           done
  102.           if [ ${device_gone} -eq 0 ]; then
  103.               echo " failed"
  104.               echo Please destroy VBD ${vbd_uuid} manually.
  105.           else
  106.               ${XE} vbd-destroy uuid=${vbd_uuid}
  107.           fi
  108.       fi
  109.    fi
  110. }
  111.  
  112. # Find the bootable VBD for the VM
  113. IFS=","
  114. bootable_vbds=$(${XE} vbd-list vm-uuid=${vm_uuid} bootable=true params=uuid --minimal)
  115. num_bootable_vbds=0
  116. for vbd in ${bootable_vbds}; do
  117.    num_bootable_vbds=$(( ${num_bootable_vbds} + 1 ))
  118. done
  119. if [ ${num_bootable_vbds} -eq 0 ]; then
  120.    echo Unable to find a bootable disk for VM.
  121.    echo There are no VBDs marked with bootable=true.
  122.    exit 1
  123. elif [ ${num_bootable_vbds} -gt 1 ]; then
  124.    echo Found ${num_bootable_vbds} for VM, but only 1 is allowed.
  125.    echo Please remove the bootable=true flag from some VBDs to fix this.
  126.    exit 1
  127. fi
  128.  
  129. # we now know that there is only one bootable VBD, so use that
  130. vbd_uuid=${bootable_vbds}
  131. vdi_uuid=$(${XE} vbd-param-get uuid=${vbd_uuid} param-name=vdi-uuid)
  132. if [ "${vdi_uuid}" = "" ]; then
  133.     echo Unable to determine VDI associated with VBD ${vbd_uuid}
  134.     exit 1
  135. fi
  136.  
  137. # check that the VDI is not currently in use
  138. vbd_uuids=$(${XE} vbd-list vdi-uuid=${vdi_uuid} params=uuid --minimal)
  139. if [ $? -ne 0 ]; then
  140.    echo Error in vbd-list call
  141.    exit 1
  142. fi
  143.  
  144. for vbd in ${vbd_uuids}; do
  145.    attached=$(${XE} vbd-list params=currently-attached uuid=${vbd} --minimal)
  146.    case ${attached} in
  147.    true)
  148.      vm_uuid=$(${XE} vbd-list params=vm-uuid uuid=${vbd} --minimal)
  149.      echo The VDI is currently in use by VBD:
  150.      echo ${vbd}
  151.      echo The associated VM is:
  152.      echo ${vm_uuid}
  153.      echo
  154.      ${XE} vm-list uuid=${vm_uuid}
  155.      echo Please shut down any running VMs which are currently using the disk.
  156.      exit 1
  157.      ;;
  158.    false)
  159.      ;;
  160.    *)
  161.      echo Internal error: unknown currently-attached result: ${attached}
  162.      ;;
  163.    esac
  164. done
  165.  
  166. echo -n "Creating dom0 VBD: "
  167. vbd_uuid=$(${XE} vbd-create vm-uuid=${CONTROL_DOMAIN_UUID} vdi-uuid=${vdi_uuid} device=autodetect)
  168. echo ${vbd_uuid}
  169.  
  170. if [ $? -ne 0 -o -z "${vbd_uuid}" ]; then
  171.   echo error creating VBD for dom0 block attach
  172.   cleanup
  173.   exit 1
  174. fi
  175.  
  176. echo -n "Plugging VBD: "
  177. ${XE} vbd-plug uuid=${vbd_uuid}
  178. echo ${device}
  179.  
  180. device=/dev/$(${XE} vbd-param-get uuid=${vbd_uuid} param-name=device)
  181.  
  182. if [ ! -b ${device} ]; then
  183.   echo ${device}: not a block special
  184.   cleanup
  185.   exit 1
  186. fi
  187.  
  188. echo -n "Waiting for ${device}${device_number}: "
  189. found_device=0
  190.  
  191. for ((i=0; i<5; i++)); do
  192.    echo -n '.'
  193.    if [ -b ${device}${device_number} ]; then
  194.       found_device=1
  195.       echo ' done'
  196.       break
  197.    fi
  198.    sleep 1
  199. done
  200.  
  201. if [ ${found_device} -eq 0 ]; then
  202.    echo Device ${device}${device_number} not found.
  203.    echo You must specify the correct partition number with -p
  204.    cleanup
  205.    exit 1
  206. fi
  207.  
  208. echo -n "Mounting filesystem: "
  209. mnt=/mnt/${vdi_uuid}
  210. mkdir -p ${mnt}
  211.  
  212. mount ${device}${device_number} ${mnt} > /dev/null 2>&1
  213.  
  214. if [ $? -ne 0 ]; then
  215.   echo " failed"
  216.   echo Partitions in the VDI are:
  217.   echo
  218.   ls -la ${device}*
  219.   echo
  220.   echo You can use the -p option to specify a partition number to mount.
  221.   cleanup
  222.   exit 1
  223. fi
  224.  
  225. echo " done"
  226.  
  227. mnt_umount="${mnt}_umount.sh"
  228.  
  229.  
  230. cat <<EOF > "${mnt_umount}"
  231. #!/bin/bash
  232.  
  233. if [ ! -z "${mnt}" ]; then
  234.   umount ${mnt} >/dev/null 2>&1
  235.   rmdir ${mnt}
  236. fi
  237.  
  238. if [ ! -z "${vbd_uuid}" ]; then
  239.   echo -n "Unplugging VBD: "
  240.   ${XE} vbd-unplug uuid=${vbd_uuid} timeout=20
  241.   # poll for the device to go away if we know its name
  242.   if [ "${device}" != "" ]; then
  243.     device_gone=0
  244.     for ((i=0; i<10; i++)); do
  245.       echo -n "."
  246.       if [ ! -b ${device} ]; then
  247.         echo " done"
  248.         device_gone=1
  249.         break
  250.       fi
  251.       sleep 1
  252.     done
  253.     if [ \${device_gone} -eq 0 ]; then
  254.       echo " failed"
  255.       echo Please destroy VBD ${vbd_uuid} manually.
  256.     else
  257.       ${XE} vbd-destroy uuid=${vbd_uuid}
  258.     fi
  259.   fi      
  260. fi
  261.  
  262. rm -f "${mnt_umount}"
  263. EOF
  264. chmod +x "${mnt_umount}"
  265.  
  266. echo " VBD UUID: ${vbd_uuid}"
  267. echo " VDI mounted to ${mnt}"
  268. echo " Release with ${mnt_umount}"
Advertisement
Add Comment
Please, Sign In to add comment