qubes-prepare-saved-domain.sh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/bin/bash
  2. set -o pipefail
  3. get_encoded_script()
  4. {
  5. ENCODED_SCRIPT=`
  6. if [ "$1" == "vm-default" ]; then
  7. echo /usr/lib/qubes/dispvm-prerun.sh
  8. else
  9. cat "$1"
  10. fi | base64 -w0` || exit 1
  11. }
  12. if [ $# != 2 -a $# != 3 ] ; then
  13. echo usage: $0 domainname savefile_to_be_created [preload script]
  14. exit 1
  15. fi
  16. export PATH=$PATH:/sbin:/usr/sbin
  17. if [ $# = 3 ] ; then
  18. get_encoded_script $3
  19. fi
  20. VMDIR=/var/lib/qubes/appvms/$1
  21. if ! [ -d $VMDIR ] ; then
  22. echo $VMDIR does not exist ?
  23. exit 1
  24. fi
  25. if ! qvm-start $1 --no-guid --dvm ; then
  26. exit 1
  27. fi
  28. ID=`xl domid $1`
  29. if [ "$ID" = "" ] ; then
  30. echo cannot get domain id
  31. exit 1
  32. fi
  33. echo "Waiting for DVM domainid=$ID ..."
  34. if [ -n "$ENCODED_SCRIPT" ] ; then
  35. xenstore-write /local/domain/$ID/qubes-save-script "$ENCODED_SCRIPT"
  36. fi
  37. #set -x
  38. xenstore-write /local/domain/$ID/qubes-save-request 1
  39. xenstore-watch-qubes /local/domain/$ID/device/qubes-used-mem
  40. xenstore-read /local/domain/$ID/qubes-gateway | \
  41. cut -d . -f 3 | tr -d "\n" > $VMDIR/netvm-id.txt
  42. xl block-detach $1 xvdb
  43. MEM=$(xenstore-read /local/domain/$ID/device/qubes-used-mem)
  44. echo "DVM boot complete, memory used=$MEM. Saving image..."
  45. QMEMMAN_STOP=/var/run/qubes/do-not-membalance
  46. touch $QMEMMAN_STOP
  47. xl mem-set $1 $(($MEM/1000))
  48. sleep 1
  49. touch $2
  50. if ! xl save $1 $2 $VMDIR/$1.conf; then
  51. rm -f $QMEMMAN_STOP
  52. exit 1
  53. fi
  54. rm -f $QMEMMAN_STOP
  55. cd $VMDIR
  56. # Apparently baloon driver isn't effective enough on some kernels - xl
  57. # restore still needs initial memory amount
  58. #sed -i -e "s/^memory.*/memory = $((MEM/1000))/" dvm.conf
  59. tar -Scvf saved-cows.tar volatile.img
  60. echo "DVM savefile created successfully."