PKGBUILD.install 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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/zsh 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. # Create NetworkManager configuration if we do not have it
  59. if ! [ -e /etc/NetworkManager/NetworkManager.conf ]; then
  60. echo '[main]' > /etc/NetworkManager/NetworkManager.conf
  61. echo 'plugins = keyfile' >> /etc/NetworkManager/NetworkManager.conf
  62. echo '[keyfile]' >> /etc/NetworkManager/NetworkManager.conf
  63. fi
  64. /usr/lib/qubes/qubes-fix-nm-conf.sh
  65. # Remove ip_forward setting from sysctl, so NM will not reset it
  66. # Archlinux now use sysctl.d/ instead of sysctl.conf
  67. #sed 's/^net.ipv4.ip_forward.*/#\0/' -i /etc/sysctl.conf
  68. # Remove old firmware updates link
  69. if [ -L /lib/firmware/updates ]; then
  70. rm -f /lib/firmware/updates
  71. fi
  72. # Yum proxy configuration is fedora specific
  73. #if ! grep -q '/etc/yum\.conf\.d/qubes-proxy\.conf' /etc/yum.conf; then
  74. # echo >> /etc/yum.conf
  75. # echo '# Yum does not support inclusion of config dir...' >> /etc/yum.conf
  76. # echo 'include=file:///etc/yum.conf.d/qubes-proxy.conf' >> /etc/yum.conf
  77. #fi
  78. #/usr/lib/qubes/update-proxy-configs
  79. # Archlinux pacman configuration is handled in update_finalize
  80. # Location of files which contains list of protected files
  81. mkdir -p /etc/qubes/protected-files.d
  82. # shellcheck source=init/functions
  83. . /usr/lib/qubes/init/functions
  84. # qubes-core-vm has been broken for some time - it overrides /etc/hosts; restore original content
  85. if ! is_protected_file /etc/hosts ; then
  86. if ! grep -q localhost /etc/hosts; then
  87. cat <<EOF > /etc/hosts
  88. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 $(hostname)
  89. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  90. EOF
  91. fi
  92. fi
  93. # ensure that hostname resolves to 127.0.0.1 resp. ::1 and that /etc/hosts is
  94. # in the form expected by qubes-sysinit.sh
  95. if ! is_protected_file /etc/hostname ; then
  96. for ip in '127\.0\.0\.1' '::1'; do
  97. if grep -q "^${ip}\(\s\|$\)" /etc/hosts; then
  98. sed -i "/^${ip}\s/,+0s/\(\s$(hostname)\)\+\(\s\|$\)/\2/g" /etc/hosts
  99. sed -i "s/^${ip}\(\s\|$\).*$/\0 $(hostname)/" /etc/hosts
  100. else
  101. echo "${ip} $(hostname)" >> /etc/hosts
  102. fi
  103. done
  104. fi
  105. # Make sure there is a default locale set so gnome-terminal will start
  106. if [ ! -e /etc/locale.conf ] || ! grep -q LANG /etc/locale.conf; then
  107. touch /etc/locale.conf
  108. echo "LANG=en_US.UTF-8" >> /etc/locale.conf
  109. fi
  110. # ... and make sure it is really generated
  111. # This line is buggy as LANG can be set to LANG="en_US.UTF-8". The Quotes must be stripped
  112. current_locale=$(grep LANG /etc/locale.conf|cut -f 2 -d = | tr -d '"')
  113. if [ -n "$current_locale" ] && ! locale -a | grep -q "$current_locale"; then
  114. base=$(echo "$current_locale" | cut -f 1 -d .)
  115. charmap=$(echo "$current_locale.UTF-8" | cut -f 2 -d .)
  116. [ -n "$charmap" ] && charmap="-f $charmap"
  117. # shellcheck disable=SC2086
  118. localedef -i "$base" $charmap "$current_locale"
  119. fi
  120. }
  121. ############################
  122. ## Service Management Functions ##
  123. ############################
  124. is_static() {
  125. [ -f "/usr/lib/systemd/system/$1" ] && ! grep -q '^[[].nstall]' "/usr/lib/systemd/system/$1"
  126. }
  127. is_masked() {
  128. if [ ! -L /etc/systemd/system/"$1" ]
  129. then
  130. return 1
  131. fi
  132. target=$(readlink /etc/systemd/system/"$1" 2>/dev/null) || :
  133. if [ "$target" = "/dev/null" ]
  134. then
  135. return 0
  136. fi
  137. return 1
  138. }
  139. mask() {
  140. ln -sf /dev/null /etc/systemd/system/"$1"
  141. }
  142. unmask() {
  143. if ! is_masked "$1"
  144. then
  145. return 0
  146. fi
  147. rm -f /etc/systemd/system/"$1"
  148. }
  149. preset_units() {
  150. local represet=
  151. while read -r action unit_name
  152. do
  153. if [ "$action" = "#" ] && [ "$unit_name" = "Units below this line will be re-preset on package upgrade" ]
  154. then
  155. represet=1
  156. continue
  157. fi
  158. echo "$action $unit_name" | grep -q '^[[:space:]]*[^#;]' || continue
  159. [[ -n "$action" && -n "$unit_name" ]] || continue
  160. if [ "$2" = "initial" ] || [ "$represet" = "1" ]
  161. then
  162. if [ "$action" = "disable" ] && is_static "$unit_name"
  163. then
  164. if ! is_masked "$unit_name"
  165. then
  166. # We must effectively mask these units, even if they are static.
  167. mask "$unit_name"
  168. fi
  169. elif [ "$action" = "enable" ] && is_static "$unit_name"
  170. then
  171. if is_masked "$unit_name"
  172. then
  173. # We masked this static unit before, now we unmask it.
  174. unmask "$unit_name"
  175. fi
  176. systemctl --no-reload preset "$unit_name" >/dev/null 2>&1 || :
  177. else
  178. systemctl --no-reload preset "$unit_name" >/dev/null 2>&1 || :
  179. fi
  180. fi
  181. done < "$1"
  182. }
  183. restore_units() {
  184. grep '^[[:space:]]*[^#;]' "$1" | while read -r action unit_name
  185. do
  186. if is_static "$unit_name" && is_masked "$unit_name"
  187. then
  188. # If the unit had been masked by us, we must unmask it here.
  189. # Otherwise systemctl preset will fail badly.
  190. unmask "$unit_name"
  191. fi
  192. systemctl --no-reload preset "$unit_name" >/dev/null 2>&1 || :
  193. done
  194. }
  195. configure_systemd() {
  196. if [ "$1" -eq 1 ]
  197. then
  198. preset_units /usr/lib/systemd/system-preset/$qubes_preset_file initial
  199. changed=true
  200. else
  201. preset_units /usr/lib/systemd/system-preset/$qubes_preset_file upgrade
  202. changed=true
  203. # Upgrade path - now qubes-iptables is used instead
  204. for svc in iptables ip6tables
  205. do
  206. if [ -f "$svc".service ]
  207. then
  208. systemctl --no-reload preset "$svc".service
  209. changed=true
  210. fi
  211. done
  212. fi
  213. if [ "$1" -eq 1 ]
  214. then
  215. # First install.
  216. # Set default "runlevel".
  217. # FIXME: this ought to be done via kernel command line.
  218. # The fewer deviations of the template from the seed
  219. # image, the better.
  220. rm -f /etc/systemd/system/default.target
  221. ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
  222. changed=true
  223. fi
  224. # remove old symlinks
  225. if [ -L /etc/systemd/system/sysinit.target.wants/qubes-random-seed.service ]
  226. then
  227. rm -f /etc/systemd/system/sysinit.target.wants/qubes-random-seed.service
  228. changed=true
  229. fi
  230. if [ -L /etc/systemd/system/multi-user.target.wants/qubes-mount-home.service ]
  231. then
  232. rm -f /etc/systemd/system/multi-user.target.wants/qubes-mount-home.service
  233. changed=true
  234. fi
  235. if [ "x$changed" != "x" ]
  236. then
  237. systemctl daemon-reload
  238. fi
  239. }
  240. ######################
  241. ## Archlinux Specific Functions ##
  242. ######################
  243. config_prependtomark() {
  244. FILE=$1
  245. APPENDBEFORELINE=$2
  246. APPENDLINE=$3
  247. grep -F -q "$APPENDLINE" "$FILE" || sed "/$APPENDBEFORELINE/i$APPENDLINE" -i "$FILE"
  248. }
  249. config_appendtomark() {
  250. FILE=$1
  251. APPENDAFTERLINE=$2
  252. APPENDLINE=$3
  253. grep -F -q "$APPENDLINE" "$FILE" || sed "/$APPENDAFTERLINE/a$APPENDLINE" -i "$FILE"
  254. }
  255. config_cleanupmark() {
  256. FILE="$1"
  257. BEGINMARK="$2"
  258. ENDMARK="$3"
  259. if grep -F -q "$BEGINMARK" "$FILE"; then
  260. if grep -F -q "$ENDMARK" "$FILE"; then
  261. cp "$FILE" "$FILE.qubes-update-orig"
  262. sed -i -e "/^$BEGINMARK$/,/^$ENDMARK$/{
  263. /^$ENDMARK$/b
  264. /^$BEGINMARK$/!d
  265. }" "$FILE"
  266. rm -f "$FILE.qubes-update-orig"
  267. else
  268. echo "ERROR: found $BEGINMARK marker but not $ENDMARK in $FILE. Please cleanup this file manually."
  269. fi
  270. elif grep -F -q "$ENDMARK" "$FILE"; then
  271. echo "ERROR: found $ENDMARK marker but not $BEGINMARK in $FILE. Please cleanup this file manually."
  272. fi
  273. }
  274. update_finalize() {
  275. # Archlinux specific: If marker exists, cleanup text between begin and end marker
  276. QUBES_MARKER="### QUBES CONFIG MARKER ###"
  277. if grep -F -q "$QUBES_MARKER" /etc/pacman.conf; then
  278. config_prependtomark "/etc/pacman.conf" "# REPOSITORIES" "### QUBES CONFIG END MARKER ###"
  279. config_cleanupmark "/etc/pacman.conf" "$QUBES_MARKER" "### QUBES CONFIG END MARKER ###"
  280. # Else, add qubes config block marker
  281. else
  282. config_prependtomark "/etc/pacman.conf" "# REPOSITORIES" "$QUBES_MARKER"
  283. config_prependtomark "/etc/pacman.conf" "# REPOSITORIES" "### QUBES CONFIG END MARKER ###"
  284. fi
  285. # Include /etc/pacman.d drop-in directory
  286. config_appendtomark "/etc/pacman.conf" "$QUBES_MARKER" "Include = /etc/pacman.d/*.conf"
  287. /usr/lib/qubes/update-proxy-configs
  288. # Archlinux specific: Update pam.d configuration for su to enable systemd-login wrapper
  289. # Also remove pam_unix.so from su configuration
  290. # as system-login (which include system-auth) already gives pam_unix.so
  291. # with more appropriate parameters (fix the missing nullok parameter)
  292. if grep -q pam_unix.so /etc/pam.d/su; then
  293. echo "Fixing pam.d"
  294. cat <<EOF > /etc/pam.d/su
  295. #%PAM-1.0
  296. auth sufficient pam_rootok.so
  297. # Uncomment the following line to implicitly trust users in the "wheel" group.
  298. #auth sufficient pam_wheel.so trust use_uid
  299. # Uncomment the following line to require a user to be in the "wheel" group.
  300. #auth required pam_wheel.so use_uid
  301. auth include system-login
  302. account include system-login
  303. session include system-login
  304. EOF
  305. cp /etc/pam.d/su /etc/pam.d/su-l
  306. fi
  307. # Archlinux specific: ensure tty1 is enabled
  308. rm -f /etc/systemd/system/getty.target.wants/getty@tty*.service
  309. systemctl enable getty\@tty1.service
  310. systemctl daemon-reload
  311. }
  312. ## arg 1: the new package version
  313. post_install() {
  314. update_qubesconfig
  315. # do the rest of %post thing only when updating for the first time...
  316. if [ -e /etc/init/serial.conf ] && ! [ -f /var/lib/qubes/serial.orig ] ; then
  317. cp /etc/init/serial.conf /var/lib/qubes/serial.orig
  318. fi
  319. # Remove most of the udev scripts to speed up the VM boot time
  320. # Just leave the xen* scripts, that are needed if this VM was
  321. # ever used as a net backend (e.g. as a VPN domain in the future)
  322. #echo "--> Removing unnecessary udev scripts..."
  323. mkdir -p /var/lib/qubes/removed-udev-scripts
  324. for f in /etc/udev/rules.d/*
  325. do
  326. if [ "$(basename "$f")" == "xen-backend.rules" ] ; then
  327. continue
  328. fi
  329. if [ "$(basename "$f")" == "50-qubes-misc.rules" ] ; then
  330. continue
  331. fi
  332. if echo "$f" | grep -q qubes; then
  333. continue
  334. fi
  335. mv "$f" /var/lib/qubes/removed-udev-scripts/
  336. done
  337. mkdir -p /rw
  338. configure_notification-daemon
  339. configure_selinux
  340. configure_systemd 0
  341. update_finalize
  342. }
  343. ## arg 1: the new package version
  344. ## arg 2: the old package version
  345. post_upgrade() {
  346. update_qubesconfig
  347. configure_notification-daemon
  348. configure_selinux
  349. configure_systemd 1
  350. update_finalize
  351. }
  352. ######################
  353. ## Remove functions ##
  354. ######################
  355. ## arg 1: the old package version
  356. pre_remove() {
  357. # no more packages left
  358. if [ -e /var/lib/qubes/fstab.orig ] ; then
  359. mv /var/lib/qubes/fstab.orig /etc/fstab
  360. fi
  361. mv /var/lib/qubes/removed-udev-scripts/* /etc/udev/rules.d/
  362. if [ -e /var/lib/qubes/serial.orig ] ; then
  363. mv /var/lib/qubes/serial.orig /etc/init/serial.conf
  364. fi
  365. if [ "$1" -eq 0 ] ; then
  366. # Run this only during uninstall.
  367. # Save the preset file to later use it to re-preset services there
  368. # once the Qubes OS preset file is removed.
  369. mkdir -p /run/qubes-uninstall
  370. cp -f /usr/lib/systemd/system-preset/$qubes_preset_file /run/qubes-uninstall/
  371. cp -f /usr/lib/systemd/system-preset/$qubes_preset_file /run/qubes-uninstall/
  372. fi
  373. }
  374. ## arg 1: the old package version
  375. post_remove() {
  376. changed=
  377. if [ -d /run/qubes-uninstall ]
  378. then
  379. # We have a saved preset file (or more).
  380. # Re-preset the units mentioned there.
  381. restore_units /run/qubes-uninstall/$qubes_preset_file
  382. rm -rf /run/qubes-uninstall
  383. changed=true
  384. fi
  385. if [ "x$changed" != "x" ]
  386. then
  387. systemctl daemon-reload
  388. fi
  389. if [ -L /lib/firmware/updates ] ; then
  390. rm /lib/firmware/updates
  391. fi
  392. rm -rf /var/lib/qubes/xdg
  393. for srv in qubes-sysinit qubes-misc-post qubes-mount-dirs qubes-network qubes-qrexec-agent; do
  394. systemctl disable $srv.service
  395. done
  396. }