qubes_prepare_saved_domain.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/sh
  2. get_encoded_script()
  3. {
  4. if ! [ -f "$1" ] ; then
  5. echo $1 is not a file ?
  6. exit 1
  7. fi
  8. ENCODED_SCRIPT=`cat "$1" | perl -e 'use MIME::Base64 qw(encode_base64); local($/) = undef;print encode_base64(<STDIN>)'|tr -d "\n"`
  9. }
  10. if [ $# != 2 -a $# != 3 ] ; then
  11. echo usage: $0 domainname savefile_to_be_created [preload script]
  12. exit 1
  13. fi
  14. export PATH=$PATH:/sbin:/usr/sbin
  15. if [ $# = 3 ] ; then
  16. get_encoded_script $3
  17. fi
  18. VMDIR=/var/lib/qubes/appvms/$1
  19. if ! [ -d $VMDIR ] ; then
  20. echo $VMDIR does not exist ?
  21. exit 1
  22. fi
  23. if ! qvm-start $1 --no-guid --dvm ; then
  24. exit 1
  25. fi
  26. ID=none
  27. for i in $(xenstore-list /local/domain) ; do
  28. name=$(xenstore-read /local/domain/$i/name)
  29. if [ "x"$name = "x"$1 ] ; then
  30. ID=$i
  31. fi
  32. done
  33. if [ $ID = none ] ; then
  34. echo cannot get domain id
  35. exit 1
  36. fi
  37. echo "Waiting for DVM domainid=$ID ..."
  38. if [ -n "$ENCODED_SCRIPT" ] ; then
  39. xenstore-write /local/domain/$ID/qubes_save_script "$ENCODED_SCRIPT"
  40. fi
  41. #set -x
  42. xenstore-write /local/domain/$ID/qubes_save_request 1
  43. xenstore-watch /local/domain/$ID/device/qubes_used_mem
  44. xenstore-read /local/domain/$ID/qubes_gateway | \
  45. cut -d . -f 2 | tr -d "\n" > $VMDIR/netvm_id.txt
  46. xm block-detach $1 /dev/xvdb
  47. MEM=$(xenstore-read /local/domain/$ID/device/qubes_used_mem)
  48. echo "DVM boot complete, memory used=$MEM. Saving image..."
  49. QMEMMAN_STOP=/var/run/qubes/do-not-membalance
  50. touch $QMEMMAN_STOP
  51. xm mem-set $1 $(($MEM/1000))
  52. sleep 1
  53. touch $2
  54. if ! xm save $1 $2 ; then
  55. rm -f $QMEMMAN_STOP
  56. exit 1
  57. fi
  58. rm -f $QMEMMAN_STOP
  59. cd $VMDIR
  60. tar -Scvf saved_cows.tar root-cow.img swap-cow.img
  61. echo "DVM savefile created successfully."