PKGBUILD.install 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. #!/bin/bash
  2. qubes_preset_file="75-qubes-vm.preset"
  3. ###########################
  4. ## Pre-Install functions ##
  5. ###########################
  6. update_default_user() {
  7. # Make sure there is a qubes group
  8. groupadd --force --system --gid 98 qubes
  9. # Archlinux bash version has a 'bug' when running su -c, /etc/profile is not loaded because bash consider there is no interactive pty when running 'su - user -c' or something like this.
  10. # See https://bugs.archlinux.org/task/31831
  11. id -u 'user' >/dev/null 2>&1 || {
  12. useradd --user-group --create-home --shell /bin/bash user
  13. }
  14. usermod -a --groups qubes user
  15. }
  16. ## arg 1: the new package version
  17. pre_install() {
  18. echo "Pre install..."
  19. update_default_user
  20. # do this whole %pre thing only when updating for the first time...
  21. mkdir -p /var/lib/qubes
  22. # Backup fstab / But use archlinux defaults (cp instead of mv)
  23. if [ -e /etc/fstab ] ; then
  24. cp /etc/fstab /var/lib/qubes/fstab.orig
  25. fi
  26. # Add qubes core related fstab entries
  27. echo "xen /proc/xen xenfs defaults 0 0" >> /etc/fstab
  28. usermod -p '' root
  29. usermod -L user
  30. }
  31. ## arg 1: the new package version
  32. ## arg 2: the old package version
  33. pre_upgrade() {
  34. # do something here
  35. echo "Pre upgrade..."
  36. update_default_user
  37. }
  38. ###################
  39. ## Install Hooks ##
  40. ###################
  41. configure_notification-daemon() {
  42. # Enable autostart of notification-daemon when installed
  43. if [ ! -L /etc/xdg/autostart/notification-daemon.desktop ]; then
  44. ln -s /usr/share/applications/notification-daemon.desktop /etc/xdg/autostart/
  45. fi
  46. }
  47. configure_selinux() {
  48. # SELinux is not enabled on archlinux
  49. #echo "--> Disabling SELinux..."
  50. echo "SELINUX not enabled on archlinux. skipped."
  51. # sed -e s/^SELINUX=.*$/SELINUX=disabled/ -i /etc/selinux/config
  52. # setenforce 0 2>/dev/null
  53. }
  54. ############################
  55. ## Post-Install functions ##
  56. ############################
  57. update_qubesconfig() {
  58. # Remove old firmware updates link
  59. if [ -L /lib/firmware/updates ]; then
  60. rm -f /lib/firmware/updates
  61. fi
  62. # convert /usr/local symlink to a mount point
  63. if [ -L /usr/local ]; then
  64. rm -f /usr/local
  65. mkdir /usr/local
  66. mount /usr/local || :
  67. fi
  68. #/usr/lib/qubes/update-proxy-configs
  69. # Archlinux pacman configuration is handled in update_finalize
  70. if ! [ -r /etc/dconf/profile/user ]; then
  71. mkdir -p /etc/dconf/profile
  72. echo "user-db:user" >> /etc/dconf/profile/user
  73. echo "system-db:local" >> /etc/dconf/profile/user
  74. fi
  75. dconf update &> /dev/null || :
  76. # Location of files which contains list of protected files
  77. mkdir -p /etc/qubes/protected-files.d
  78. # shellcheck source=init/functions
  79. . /usr/lib/qubes/init/functions
  80. # qubes-core-vm has been broken for some time - it overrides /etc/hosts; restore original content
  81. if ! is_protected_file /etc/hosts ; then
  82. if ! grep -q localhost /etc/hosts; then
  83. cat <<EOF > /etc/hosts
  84. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 $(hostname)
  85. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  86. EOF
  87. fi
  88. fi
  89. # ensure that hostname resolves to 127.0.0.1 resp. ::1 and that /etc/hosts is
  90. # in the form expected by qubes-sysinit.sh
  91. if ! is_protected_file /etc/hostname ; then
  92. for ip in '127\.0\.0\.1' '::1'; do
  93. if grep -q "^${ip}\(\s\|$\)" /etc/hosts; then
  94. sed -i "/^${ip}\s/,+0s/\(\s$(hostname)\)\+\(\s\|$\)/\2/g" /etc/hosts
  95. sed -i "s/^${ip}\(\s\|$\).*$/\0 $(hostname)/" /etc/hosts
  96. else
  97. echo "${ip} $(hostname)" >> /etc/hosts
  98. fi
  99. done
  100. fi
  101. }
  102. ############################
  103. ## Service Management Functions ##
  104. ############################
  105. is_static() {
  106. [ -f "/usr/lib/systemd/system/$1" ] && ! grep -q '^[[].nstall]' "/usr/lib/systemd/system/$1"
  107. }
  108. is_masked() {
  109. if [ ! -L /etc/systemd/system/"$1" ]
  110. then
  111. return 1
  112. fi
  113. target=$(readlink /etc/systemd/system/"$1" 2>/dev/null) || :
  114. if [ "$target" = "/dev/null" ]
  115. then
  116. return 0
  117. fi
  118. return 1
  119. }
  120. mask() {
  121. ln -sf /dev/null /etc/systemd/system/"$1"
  122. }
  123. unmask() {
  124. if ! is_masked "$1"
  125. then
  126. return 0
  127. fi
  128. rm -f /etc/systemd/system/"$1"
  129. }
  130. preset_units() {
  131. local represet=
  132. while read -r action unit_name
  133. do
  134. if [ "$action" = "#" ] && [ "$unit_name" = "Units below this line will be re-preset on package upgrade" ]
  135. then
  136. represet=1
  137. continue
  138. fi
  139. echo "$action $unit_name" | grep -q '^[[:space:]]*[^#;]' || continue
  140. [[ -n "$action" && -n "$unit_name" ]] || continue
  141. if [ "$2" = "initial" ] || [ "$represet" = "1" ]
  142. then
  143. if [ "$action" = "disable" ] && is_static "$unit_name"
  144. then
  145. if ! is_masked "$unit_name"
  146. then
  147. # We must effectively mask these units, even if they are static.
  148. mask "$unit_name"
  149. fi
  150. elif [ "$action" = "enable" ] && is_static "$unit_name"
  151. then
  152. if is_masked "$unit_name"
  153. then
  154. # We masked this static unit before, now we unmask it.
  155. unmask "$unit_name"
  156. fi
  157. systemctl --no-reload preset "$unit_name" >/dev/null 2>&1 || :
  158. else
  159. systemctl --no-reload preset "$unit_name" >/dev/null 2>&1 || :
  160. fi
  161. fi
  162. done < "$1"
  163. }
  164. restore_units() {
  165. grep '^[[:space:]]*[^#;]' "$1" | while read -r action unit_name
  166. do
  167. if is_static "$unit_name" && is_masked "$unit_name"
  168. then
  169. # If the unit had been masked by us, we must unmask it here.
  170. # Otherwise systemctl preset will fail badly.
  171. unmask "$unit_name"
  172. fi
  173. systemctl --no-reload preset "$unit_name" >/dev/null 2>&1 || :
  174. done
  175. }
  176. configure_systemd() {
  177. if [ "$1" -eq 1 ]
  178. then
  179. preset_units /usr/lib/systemd/system-preset/$qubes_preset_file initial
  180. changed=true
  181. else
  182. preset_units /usr/lib/systemd/system-preset/$qubes_preset_file upgrade
  183. changed=true
  184. # Upgrade path - now qubes-iptables is used instead
  185. for svc in iptables ip6tables
  186. do
  187. if [ -f "$svc".service ]
  188. then
  189. systemctl --no-reload preset "$svc".service
  190. changed=true
  191. fi
  192. done
  193. fi
  194. if [ "$1" -eq 1 ]
  195. then
  196. # First install.
  197. # Set default "runlevel".
  198. # FIXME: this ought to be done via kernel command line.
  199. # The fewer deviations of the template from the seed
  200. # image, the better.
  201. rm -f /etc/systemd/system/default.target
  202. ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
  203. changed=true
  204. fi
  205. # remove old symlinks
  206. if [ -L /etc/systemd/system/sysinit.target.wants/qubes-random-seed.service ]
  207. then
  208. rm -f /etc/systemd/system/sysinit.target.wants/qubes-random-seed.service
  209. changed=true
  210. fi
  211. if [ -L /etc/systemd/system/multi-user.target.wants/qubes-mount-home.service ]
  212. then
  213. rm -f /etc/systemd/system/multi-user.target.wants/qubes-mount-home.service
  214. changed=true
  215. fi
  216. if [ "x$changed" != "x" ]
  217. then
  218. systemctl daemon-reload
  219. fi
  220. }
  221. ######################
  222. ## Archlinux Specific Functions ##
  223. ######################
  224. config_prependtomark() {
  225. FILE=$1
  226. APPENDBEFORELINE=$2
  227. APPENDLINE=$3
  228. grep -F -q "$APPENDLINE" "$FILE" || sed "/$APPENDBEFORELINE/i$APPENDLINE" -i "$FILE"
  229. }
  230. config_appendtomark() {
  231. FILE=$1
  232. APPENDAFTERLINE=$2
  233. APPENDLINE=$3
  234. grep -F -q "$APPENDLINE" "$FILE" || sed "/$APPENDAFTERLINE/a$APPENDLINE" -i "$FILE"
  235. }
  236. config_cleanupmark() {
  237. FILE="$1"
  238. BEGINMARK="$2"
  239. ENDMARK="$3"
  240. if grep -F -q "$BEGINMARK" "$FILE"; then
  241. if grep -F -q "$ENDMARK" "$FILE"; then
  242. cp "$FILE" "$FILE.qubes-update-orig"
  243. sed -i -e "/^$BEGINMARK$/,/^$ENDMARK$/{
  244. /^$ENDMARK$/b
  245. /^$BEGINMARK$/!d
  246. }" "$FILE"
  247. rm -f "$FILE.qubes-update-orig"
  248. else
  249. echo "ERROR: found $BEGINMARK marker but not $ENDMARK in $FILE. Please cleanup this file manually."
  250. fi
  251. elif grep -F -q "$ENDMARK" "$FILE"; then
  252. echo "ERROR: found $ENDMARK marker but not $BEGINMARK in $FILE. Please cleanup this file manually."
  253. fi
  254. }
  255. update_finalize() {
  256. # Archlinux specific: If marker exists, cleanup text between begin and end marker
  257. QUBES_MARKER="### QUBES CONFIG MARKER ###"
  258. if grep -F -q "$QUBES_MARKER" /etc/pacman.conf; then
  259. config_prependtomark "/etc/pacman.conf" "# REPOSITORIES" "### QUBES CONFIG END MARKER ###"
  260. config_cleanupmark "/etc/pacman.conf" "$QUBES_MARKER" "### QUBES CONFIG END MARKER ###"
  261. # Else, add qubes config block marker
  262. else
  263. config_prependtomark "/etc/pacman.conf" "# REPOSITORIES" "$QUBES_MARKER"
  264. config_prependtomark "/etc/pacman.conf" "# REPOSITORIES" "### QUBES CONFIG END MARKER ###"
  265. fi
  266. # Include /etc/pacman.d drop-in directory
  267. config_appendtomark "/etc/pacman.conf" "$QUBES_MARKER" "Include = /etc/pacman.d/*.conf"
  268. /usr/lib/qubes/update-proxy-configs
  269. # Archlinux specific: Update pam.d configuration for su to enable systemd-login wrapper
  270. # This is required as qubes-gui agent calls xinit with su -l user without initializing properly
  271. # the user session.
  272. # pam_unix.so can also be removed from su configuration
  273. # as system-login (which include system-auth) already gives pam_unix.so
  274. # with more appropriate parameters (fix the missing nullok parameter)
  275. if grep -q pam_unix.so /etc/pam.d/su; then
  276. echo "Fixing pam.d"
  277. cp /etc/pam.d/qrexec /etc/pam.d/su-l
  278. fi
  279. # Archlinux specific: ensure tty1 is enabled
  280. rm -f /etc/systemd/system/getty.target.wants/getty@tty*.service
  281. systemctl enable getty\@tty1.service
  282. systemctl daemon-reload
  283. }
  284. ## arg 1: the new package version
  285. post_install() {
  286. update_qubesconfig
  287. # do the rest of %post thing only when updating for the first time...
  288. if [ -e /etc/init/serial.conf ] && ! [ -f /var/lib/qubes/serial.orig ] ; then
  289. cp /etc/init/serial.conf /var/lib/qubes/serial.orig
  290. fi
  291. chgrp user /var/lib/qubes/dom0-updates
  292. # Remove most of the udev scripts to speed up the VM boot time
  293. # Just leave the xen* scripts, that are needed if this VM was
  294. # ever used as a net backend (e.g. as a VPN domain in the future)
  295. #echo "--> Removing unnecessary udev scripts..."
  296. mkdir -p /var/lib/qubes/removed-udev-scripts
  297. for f in /etc/udev/rules.d/*
  298. do
  299. if [ "$(basename "$f")" == "xen-backend.rules" ] ; then
  300. continue
  301. fi
  302. if [ "$(basename "$f")" == "50-qubes-misc.rules" ] ; then
  303. continue
  304. fi
  305. if echo "$f" | grep -q qubes; then
  306. continue
  307. fi
  308. mv "$f" /var/lib/qubes/removed-udev-scripts/
  309. done
  310. mkdir -p /rw
  311. configure_notification-daemon
  312. configure_selinux
  313. configure_systemd 0
  314. update_finalize
  315. }
  316. ## arg 1: the new package version
  317. ## arg 2: the old package version
  318. post_upgrade() {
  319. update_qubesconfig
  320. configure_notification-daemon
  321. configure_selinux
  322. configure_systemd 1
  323. update_finalize
  324. }
  325. ######################
  326. ## Remove functions ##
  327. ######################
  328. ## arg 1: the old package version
  329. pre_remove() {
  330. # no more packages left
  331. if [ -e /var/lib/qubes/fstab.orig ] ; then
  332. mv /var/lib/qubes/fstab.orig /etc/fstab
  333. fi
  334. mv /var/lib/qubes/removed-udev-scripts/* /etc/udev/rules.d/
  335. if [ -e /var/lib/qubes/serial.orig ] ; then
  336. mv /var/lib/qubes/serial.orig /etc/init/serial.conf
  337. fi
  338. if [ "$1" -eq 0 ] ; then
  339. # Run this only during uninstall.
  340. # Save the preset file to later use it to re-preset services there
  341. # once the Qubes OS preset file is removed.
  342. mkdir -p /run/qubes-uninstall
  343. cp -f /usr/lib/systemd/system-preset/$qubes_preset_file /run/qubes-uninstall/
  344. cp -f /usr/lib/systemd/system-preset/$qubes_preset_file /run/qubes-uninstall/
  345. fi
  346. }
  347. ## arg 1: the old package version
  348. post_remove() {
  349. changed=
  350. if [ -d /run/qubes-uninstall ]
  351. then
  352. # We have a saved preset file (or more).
  353. # Re-preset the units mentioned there.
  354. restore_units /run/qubes-uninstall/$qubes_preset_file
  355. rm -rf /run/qubes-uninstall
  356. changed=true
  357. fi
  358. if [ "x$changed" != "x" ]
  359. then
  360. systemctl daemon-reload
  361. fi
  362. if [ -L /lib/firmware/updates ] ; then
  363. rm /lib/firmware/updates
  364. fi
  365. rm -rf /var/lib/qubes/xdg
  366. for srv in qubes-sysinit qubes-misc-post qubes-mount-dirs qubes-qrexec-agent; do
  367. systemctl disable $srv.service
  368. done
  369. }