setup-rw.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/sh
  2. if mountpoint -q /rw ; then
  3. # This means /rw is mounted now.
  4. echo "Checking /rw" >&2
  5. if ! [ -d /rw/config ] ; then
  6. echo "Virgin boot of the VM: populating /rw/config" >&2
  7. mkdir -p /rw/config
  8. touch /rw/config/rc.local
  9. cat > /rw/config/rc.local <<EOF
  10. #!/bin/sh
  11. # This script will be executed at every VM startup, you can place your own
  12. # custom commands here. This include overriding some configuration in /etc,
  13. # starting services etc.
  14. #
  15. # You need to make this script executable to have it enabled.
  16. # Example for overriding the whole CUPS configuration:
  17. # rm -rf /etc/cups
  18. # ln -s /rw/config/cups /etc/cups
  19. # systemctl --no-block restart cups
  20. EOF
  21. touch /rw/config/qubes-firewall-user-script
  22. cat > /rw/config/qubes-firewall-user-script <<EOF
  23. #!/bin/sh
  24. # This script is called in ProxyVM after firewall every update (configuration
  25. # change, starting some VM etc). This is good place to write own custom
  26. # firewall rules, in addition to autogenerated one. Remember that in most cases
  27. # you'll need to insert the rules at the beginning (iptables -I) to have it
  28. # efective.
  29. #
  30. # You need to make this script executable to have it enabled.
  31. EOF
  32. touch /rw/config/suspend-module-blacklist
  33. cat > /rw/config/suspend-module-blacklist <<EOF
  34. # You can list here modules you want to be unloaded before going to sleep. This
  35. # file is used only if the VM has any PCI device assigned. Modules will be
  36. # automatically loaded after resume.
  37. EOF
  38. fi
  39. if ! [ -d /rw/usrlocal ] ; then
  40. if [ -d /usr/local.orig ] ; then
  41. echo "Virgin boot of the VM: populating /rw/usrlocal from /usr/local.orig" >&2
  42. cp -af /usr/local.orig /rw/usrlocal
  43. else
  44. echo "Virgin boot of the VM: creating /rw/usrlocal" >&2
  45. mkdir -p /rw/usrlocal
  46. fi
  47. fi
  48. if ! [ -d /rw/home ] ; then
  49. echo "Virgin boot of the VM: populating /rw/home" >&2
  50. mkdir -p /rw/home
  51. fi
  52. # Chown home if users' UIDs have changed - can be the case on template switch.
  53. for pair in $(getent passwd | awk -F : '/\/home/ { print $1":"$3":"$4":"$6 } ') ; do
  54. user=$(echo "$pair" | awk -F : ' { print $1 } ')
  55. uid=$(echo "$pair" | awk -F : ' { print $2 } ')
  56. gid=$(echo "$pair" | awk -F : ' { print $3 } ')
  57. homedir=$(echo "$pair" | awk -F : ' { print $4 } ')
  58. if ! test -d /rw"$homedir" ; then
  59. if [ "$homedir" == "/home/user" -a -d /home.orig/"$user" ] ; then
  60. echo "Virgin boot of the VM: populating /rw$homedir from /home.orig/$user" >&2
  61. cp -af /home.orig/"$user" /rw"$homedir"
  62. else
  63. echo "Virgin boot of the VM: populating /rw$homedir from /etc/skel" >&2
  64. cp -af /etc/skel /rw"$homedir"
  65. fi
  66. chown -R "$uid" /rw"$homedir" &
  67. chgrp -R "$gid" /rw"$homedir" &
  68. chmod 700 /rw"$homedir" &
  69. wait
  70. fi
  71. homedir_uid=$(ls -dn /rw"$homedir" | awk '{print $3}')
  72. homedir_gid=$(ls -dn /rw"$homedir" | awk '{print $4}')
  73. if [ "$uid" -ne "$homedir_uid" ]; then
  74. echo "Virgin boot of the VM: adjusting ownership on /rw$homedir to $uid" >&2
  75. find /rw/"$homedir" -uid "$homedir_uid" -print0 | xargs -0 echo chown "$uid"
  76. fi
  77. if [ "$gid" -ne "$homedir_gid" ]; then
  78. echo "Virgin boot of the VM: adjusting groupship on /rw$homedir to $gid" >&2
  79. find /rw/"$homedir" -gid "$homedir_gid" -print0 | xargs -0 echo chgrp "$gid"
  80. fi
  81. done
  82. echo "Finished checking /rw" >&2
  83. fi
  84. # Old Qubes versions had symlink /home -> /rw/home; now we use mount --bind
  85. if [ -L /home ]; then
  86. rm /home
  87. mkdir /home
  88. fi
  89. if [ ! -e /var/lib/qubes/first-boot-completed ]; then
  90. touch /var/lib/qubes/first-boot-completed
  91. fi