qubes-prepare-saved-domain.sh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. echo "Waiting for DVM $1 ..." >&2
  29. if [ -n "$ENCODED_SCRIPT" ] ; then
  30. qubesdb-write -d $1 /qubes-save-script "$ENCODED_SCRIPT"
  31. fi
  32. #set -x
  33. qubesdb-write -d $1 /qubes-save-request 1
  34. qubesdb-watch -d $1 /qubes-used-mem
  35. qubesdb-read -d $1 /qubes-gateway | \
  36. cut -d . -f 3 | tr -d "\n" > $VMDIR/netvm-id.txt
  37. # FIXME: get connection URI from core scripts
  38. virsh -c xen:/// detach-disk $1 xvdb
  39. MEM=$(qubesdb-read -d $1 /qubes-used-mem)
  40. echo "DVM boot complete, memory used=$MEM. Saving image..." >&2
  41. QMEMMAN_STOP=/var/run/qubes/do-not-membalance
  42. touch $QMEMMAN_STOP
  43. virsh -c xen:/// setmem $1 $MEM
  44. # Add some safety margin
  45. virsh -c xen:/// setmaxmem $1 $[ $MEM + 1024 ]
  46. # Stop qubesdb daemon now, so VM can restart it later
  47. kill `cat /var/run/qubes/qubesdb.$1.pid`
  48. sleep 1
  49. touch $2
  50. if ! virsh -c xen:/// save $1 $2; then
  51. rm -f $QMEMMAN_STOP
  52. exit 1
  53. fi
  54. rm -f $QMEMMAN_STOP
  55. ln -sf $VMDIR /var/lib/qubes/dvmdata/vmdir
  56. cd $VMDIR
  57. tar -Scf saved-cows.tar volatile.img
  58. echo "DVM savefile created successfully."