qubes-prepare-saved-domain.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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]" >&2
  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 ?" >&2
  23. exit 1
  24. fi
  25. if ! qvm-start $1 --no-guid --dvm ; then
  26. exit 1
  27. fi
  28. ID=`virsh -c xen:/// domid $1`
  29. if [ "$ID" = "" ] ; then
  30. echo "cannot get domain id" >&2
  31. exit 1
  32. fi
  33. echo "Waiting for DVM domainid=$ID ..." >&2
  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. # FIXME: get connection URI from core scripts
  43. virsh -c xen:/// detach-disk $1 xvdb
  44. MEM=$(xenstore-read /local/domain/$ID/device/qubes-used-mem)
  45. echo "DVM boot complete, memory used=$MEM. Saving image..." >&2
  46. QMEMMAN_STOP=/var/run/qubes/do-not-membalance
  47. touch $QMEMMAN_STOP
  48. virsh -c xen:/// setmem $1 $MEM
  49. # Add some safety margin
  50. virsh -c xen:/// setmaxmem $1 $[ $MEM + 1024 ]
  51. sleep 1
  52. touch $2
  53. if ! virsh -c xen:/// save $1 $2; then
  54. rm -f $QMEMMAN_STOP
  55. exit 1
  56. fi
  57. rm -f $QMEMMAN_STOP
  58. ln -s $VMDIR /var/lib/qubes/dvmdata/vmdir
  59. cd $VMDIR
  60. tar -Scf saved-cows.tar volatile.img
  61. echo "DVM savefile created successfully."