qvm-create-default-dvm 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/sh
  2. if [ $# != 1 -a $# != 2 ] ; 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. DEFAULTCONF=/var/lib/qubes/dvmdata/default-dvm.conf
  48. CURRENT=/var/run/qubes/current-savefile
  49. CURRENTCONF=/var/run/qubes/current-dvm.conf
  50. SHMDIR=/dev/shm/qubes
  51. SHMCOPY=$SHMDIR/current-savefile
  52. rm -f $ROOT $DEFAULT $CURRENT $DEFAULTCONF $CURRENTCONF
  53. ln -s "/var/lib/qubes/appvms/$DVMTMPL/dvm-savefile" $DEFAULT
  54. ln -s "/var/lib/qubes/vm-templates/$TEMPLATENAME/root.img" $ROOT
  55. ln -s $DVMTMPLDIR/dvm.conf $DEFAULTCONF
  56. ln -s $DVMTMPLDIR/dvm.conf $CURRENTCONF
  57. if [ -f /var/lib/qubes/dvmdata/dont-use-shm ] ; then
  58. ln -s $DEFAULT $CURRENT
  59. else
  60. mkdir -m 770 $SHMDIR 2>/dev/null
  61. chgrp qubes $SHMDIR 2>/dev/null
  62. rm -f $SHMCOPY
  63. cp $DEFAULT $SHMCOPY || exit 1
  64. chgrp qubes $SHMCOPY
  65. chmod 660 $SHMCOPY
  66. ln -s $SHMCOPY $CURRENT
  67. fi
  68. if [ $(whoami) = "root" ] ; then
  69. chgrp qubes "$DVMTMPLDIR" "$DVMTMPLDIR"/*
  70. chmod 660 "$DVMTMPLDIR"/*
  71. chmod 770 "$DVMTMPLDIR"
  72. fi