qubes-prepare-saved-domain.sh 1.6 KB

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