qubes-prepare-saved-domain.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 --dvm ; then
  26. exit 1
  27. fi
  28. ID=`virsh -c xen:/// domid $1`
  29. echo "Waiting for DVM $1 ..." >&2
  30. if [ -n "$ENCODED_SCRIPT" ] ; then
  31. qubesdb-write -d $1 /qubes-save-script "$ENCODED_SCRIPT"
  32. fi
  33. #set -x
  34. qubesdb-write -d $1 /qubes-save-request 1
  35. qubesdb-watch -d $1 /qubes-used-mem
  36. qubesdb-read -d $1 /qubes-gateway | \
  37. cut -d . -f 3 | tr -d "\n" > $VMDIR/netvm-id.txt
  38. kill `cat /var/run/qubes/guid-running.$ID`
  39. # FIXME: get connection URI from core scripts
  40. virsh -c xen:/// detach-disk $1 xvdb
  41. MEM=$(qubesdb-read -d $1 /qubes-used-mem | grep '^[0-9]\+$' | head -n 1)
  42. echo "DVM boot complete, memory used=$MEM. Saving image..." >&2
  43. QMEMMAN_STOP=/var/run/qubes/do-not-membalance
  44. touch $QMEMMAN_STOP
  45. virsh -c xen:/// setmem $1 $MEM
  46. # Add some safety margin
  47. virsh -c xen:/// setmaxmem $1 $[ $MEM + 1024 ]
  48. # Stop qubesdb daemon now, so VM can restart it later
  49. kill `cat /var/run/qubes/qubesdb.$1.pid`
  50. sleep 1
  51. touch $2
  52. if ! virsh -c xen:/// save $1 $2; then
  53. rm -f $QMEMMAN_STOP
  54. qvm-kill $1
  55. exit 1
  56. fi
  57. rm -f $QMEMMAN_STOP
  58. # Do not allow smaller allocation than 400MB. If that small number comes from
  59. # an error, it would prevent further savefile regeneration (because VM would
  60. # not start with too little memory). Also 'maxmem' depends on 'memory', so
  61. # 400MB is sane compromise.
  62. if [ "$MEM" -lt 409600 ]; then
  63. qvm-prefs -s $1 memory 400
  64. else
  65. qvm-prefs -s $1 memory $[ $MEM / 1024 ]
  66. fi
  67. ln -snf $VMDIR /var/lib/qubes/dvmdata/vmdir
  68. cd $VMDIR
  69. bsdtar -cSf saved-cows.tar volatile.img
  70. echo "DVM savefile created successfully."