qvm-create-default-dvm 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/sh
  2. if [ $# != 1 -a $# != 2 -o $1 == "--help" -o $1 == "-h" ] ; then
  3. echo 'Usage: qvm-create-default-dvm templatename|--default-template|--used-template [script-name|--default-script]'
  4. exit 1
  5. fi
  6. export ROOT=/var/lib/qubes/dvmdata/savefile-root
  7. TEMPLATENAME=$1
  8. if [ "$TEMPLATENAME" = --used-template ] ; then
  9. if [ -e $ROOT ] ; then
  10. TEMPLATENAME=$(readlink $ROOT | sed -e 's/.root.img//' -e 's/.*\///')
  11. else
  12. TEMPLATENAME=--default-template
  13. fi
  14. fi
  15. if [ "$TEMPLATENAME" = --default-template ] ; then
  16. TEMPLATENAME=$(qubes-prefs --get default-template)
  17. if [ "X"$TEMPLATENAME = "X" ] ; then
  18. echo No default template ?
  19. exit 1
  20. fi
  21. fi
  22. if [ -z "$2" -o "X""$2" = "X""--default-script" ] ; then
  23. SCRIPTNAME="vm-default"
  24. else
  25. SCRIPTNAME=$2
  26. fi
  27. if ! [ -d "/var/lib/qubes/vm-templates/$TEMPLATENAME" ] ; then
  28. echo /var/lib/qubes/vm-templates/$TEMPLATENAME is not a directory
  29. exit 1
  30. fi
  31. DVMTMPL="$TEMPLATENAME"-dvm
  32. DVMTMPLDIR="/var/lib/qubes/appvms/$DVMTMPL"
  33. if ! [ -d "$DVMTMPLDIR" ] ; then
  34. # unfortunately, currently there are reliability issues with save of a domain
  35. # with multiple CPUs and/or more than 4000M RAM
  36. if ! qvm-create --force-root --vcpus=1 --internal -t "$TEMPLATENAME" -l gray "$DVMTMPL" ; then exit 1 ; fi
  37. MAXMEM=`qvm-prefs --force-root $DVMTMPL|grep ^maxmem|awk '{print $3}'`
  38. if [ "$MAXMEM" -ge 4000 ]; then
  39. qvm-prefs --force-root -s $DVMTMPL maxmem 4000
  40. fi
  41. fi
  42. if ! /usr/lib/qubes/qubes-prepare-saved-domain.sh \
  43. "$DVMTMPL" "/var/lib/qubes/appvms/$DVMTMPL/dvm-savefile" $SCRIPTNAME ; then
  44. exit 1
  45. fi
  46. DEFAULT=/var/lib/qubes/dvmdata/default-savefile
  47. CURRENT=/var/run/qubes/current-savefile
  48. SHMDIR=/dev/shm/qubes
  49. SHMCOPY=$SHMDIR/current-savefile
  50. rm -f $ROOT $DEFAULT $CURRENT
  51. ln -s "/var/lib/qubes/appvms/$DVMTMPL/dvm-savefile" $DEFAULT
  52. ln -s "/var/lib/qubes/vm-templates/$TEMPLATENAME/root.img" $ROOT
  53. if [ -f /var/lib/qubes/dvmdata/dont-use-shm ] ; then
  54. ln -s $DEFAULT $CURRENT
  55. else
  56. mkdir -m 770 $SHMDIR 2>/dev/null
  57. chgrp qubes $SHMDIR 2>/dev/null
  58. rm -f $SHMCOPY
  59. cp $DEFAULT $SHMCOPY || exit 1
  60. chgrp qubes $SHMCOPY
  61. chmod 660 $SHMCOPY
  62. ln -s $SHMCOPY $CURRENT
  63. fi
  64. if [ $(whoami) = "root" ] ; then
  65. chgrp qubes "$DVMTMPLDIR" "$DVMTMPLDIR"/*
  66. chmod 660 "$DVMTMPLDIR"/*
  67. chmod 770 "$DVMTMPLDIR"
  68. fi