qubes-core-agent.postinst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. #!/bin/bash
  2. # postinst script for core-agent-linux
  3. #
  4. # see: dh_installdeb(1)
  5. set -e
  6. # The postint script may be called in the following ways:
  7. # * <postinst> 'configure' <most-recently-configured-version>
  8. # * <old-postinst> 'abort-upgrade' <new version>
  9. # * <conflictor's-postinst> 'abort-remove' 'in-favour' <package>
  10. # <new-version>
  11. # * <postinst> 'abort-remove'
  12. # * <deconfigured's-postinst> 'abort-deconfigure' 'in-favour'
  13. # <failed-install-package> <version> 'removing'
  14. # <conflicting-package> <version>
  15. #
  16. # For details, see http://www.debian.org/doc/debian-policy/ or
  17. # https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html or
  18. # the debian-policy package
  19. # Directory that modified desktop entry config files are stored in
  20. XDG_CONFIG_QUBES="/usr/share/qubes/xdg"
  21. # Install overriden services only when original exists
  22. installOverridenServices() {
  23. override_dir="${1}"
  24. service="${2}"
  25. retval=1
  26. for unit in ${service}; do
  27. unit="${unit%%.*}"
  28. unit_name="$(basename ${unit})"
  29. if [ -f ${unit}.service ]; then
  30. echo "Installing override for ${unit}.service..."
  31. cp ${override_dir}/${unit_name}.service /etc/systemd/system/
  32. retval=0
  33. fi
  34. if [ -f ${unit}.socket -a -f ${override_dir}/${unit}.socket ]; then
  35. echo "Installing override for ${unit}.socket..."
  36. cp ${override_dir}/${unit_name}.socket /etc/systemd/system/
  37. retval=0
  38. fi
  39. if [ -f ${unit}.path -a -f ${override_dir}/${unit}.path ]; then
  40. echo "Installing override for ${unit}.path..."
  41. cp ${override_dir}/${unit_name}.path /etc/systemd/system/
  42. retval=0
  43. fi
  44. done
  45. return ${retval}
  46. }
  47. reenableNetworkManager() {
  48. # Disable original service to enable overriden one
  49. echo "Disabling original service to enable overriden one..."
  50. disableSystemdUnits ModemManager.service
  51. disableSystemdUnits NetworkManager.service
  52. # Disable D-BUS activation of NetworkManager - in AppVm it causes problems (eg PackageKit timeouts)
  53. echo "Disable D-BUS activation of NetworkManager - in AppVm it causes problems (eg PackageKit timeouts)"
  54. systemctl mask dbus-org.freedesktop.NetworkManager.service 2> /dev/null || echo "Could not disable D-BUS activation of NetworkManager"
  55. echo "Re-enabling original service to enable overriden one..."
  56. enableSystemdUnits ModemManager.service
  57. enableSystemdUnits NetworkManager.service
  58. # Fix for https://bugzilla.redhat.com/show_bug.cgi?id=974811
  59. echo "Fix for https://bugzilla.redhat.com/show_bug.cgi?id=974811"
  60. enableSystemdUnits NetworkManager-dispatcher.service
  61. }
  62. remove_ShowIn() {
  63. if [ -e "${1}" ]; then
  64. sed -i '/^\(Not\|Only\)ShowIn/d' "${1}"
  65. fi
  66. }
  67. showIn() {
  68. desktop_entry="${1}"
  69. shown_in="${2}"
  70. message="${shown_in:-"Shown in All;"}"
  71. desktop_entry_qubes="${XDG_CONFIG_QUBES}/autostart/${desktop_entry##*/}"
  72. # Make sure Qubes autostart directory exists
  73. mkdir -p "${XDG_CONFIG_QUBES}/autostart"
  74. # Desktop entry exists, so move to Qubes directory and modify it
  75. if [ -e "${desktop_entry}" ]; then
  76. echo "Desktop Entry Modification - ${message} ${desktop_entry##*/}..."
  77. cp -pf "${desktop_entry}" "${desktop_entry_qubes}"
  78. remove_ShowIn "${desktop_entry_qubes}"
  79. sed -i '/^X-GNOME-Autostart-enabled.*[fF0]/d' "${desktop_entry_qubes}"
  80. # Will only be '' if shown in all
  81. if [ ! "${shown_in}x" == "x" ]; then
  82. echo "${shown_in}" >> "${desktop_entry_qubes}" || true
  83. fi
  84. # Desktop entry must have been removed, so also remove from Qubes directory
  85. else
  86. echo "Desktop Entry Modification - Remove: ${desktop_entry##*/}..."
  87. rm -f "${desktop_entry_qubes}"
  88. fi
  89. }
  90. setArrayAsGlobal() {
  91. local array="$1"
  92. local export_as="$2"
  93. local code=$(declare -p "$array")
  94. local replaced="${code/$array/$export_as}"
  95. eval ${replaced/declare -/declare -g}
  96. }
  97. systemdInfo() {
  98. unit=${1}
  99. return_global_var=${2}
  100. declare -A INFO
  101. while read line; do
  102. INFO[${line%%=*}]="${line##*=}"
  103. done < <(systemctl show ${unit} 2> /dev/null)
  104. setArrayAsGlobal INFO $return_global_var
  105. }
  106. displayFailedStatus() {
  107. action=${1}
  108. unit=${2}
  109. systemdInfo ${unit} info
  110. echo
  111. echo "==================================================="
  112. echo "FAILED: systemd ${action} ${unit}"
  113. echo "==================================================="
  114. echo " LoadState = ${info[LoadState]}"
  115. echo " LoadError = ${info[LoadError]}"
  116. echo " ActiveState = ${info[ActiveState]}"
  117. echo " SubState = ${info[SubState]}"
  118. echo "UnitFileState = ${info[UnitFileState]}"
  119. echo
  120. }
  121. # Disable systemd units
  122. disableSystemdUnits() {
  123. for unit in $*; do
  124. systemctl is-enabled ${unit} > /dev/null 2>&1 && {
  125. echo "Disabling ${unit}..."
  126. systemctl is-active ${unit} > /dev/null 2>&1 && {
  127. systemctl stop ${unit} > /dev/null 2>&1 || displayFailedStatus stop ${unit}
  128. }
  129. if [ -f /lib/systemd/system/${unit} ]; then
  130. if fgrep -q '[Install]' /lib/systemd/system/${unit}; then
  131. systemctl disable ${unit} > /dev/null 2>&1 || displayFailedStatus disable ${unit}
  132. else
  133. # Forcibly disable
  134. echo "Forcibly disabling: ${unit}"
  135. ln -sf /dev/null /etc/systemd/system/${unit}
  136. fi
  137. else
  138. systemctl disable ${unit} > /dev/null 2>&1 || displayFailedStatus disable ${unit}
  139. fi
  140. } || {
  141. echo "It appears ${unit} is already disabled!"
  142. #displayFailedStatus is-disabled ${unit}
  143. }
  144. done
  145. }
  146. # Enable systemd units
  147. enableSystemdUnits() {
  148. for unit in $*; do
  149. systemctl is-enabled ${unit} > /dev/null 2>&1 && {
  150. echo "It appears ${unit} is already enabled!"
  151. #displayFailedStatus is-enabled ${unit}
  152. } || {
  153. echo "Enabling: ${unit}..."
  154. systemctl enable ${unit} > /dev/null 2>&1 && {
  155. systemctl start ${unit} > /dev/null 2>&1 || displayFailedStatus start ${unit}
  156. } || {
  157. echo "Could not enable: ${unit}"
  158. displayFailedStatus enable ${unit}
  159. }
  160. }
  161. done
  162. }
  163. # Manually trigger all triggers to automaticatly configure
  164. triggerTriggers() {
  165. path="$(readlink -m ${0})"
  166. triggers="${path/postinst/triggers}"
  167. awk '{sub(/[ \t]*#.*/,"")} NF' ${triggers} | while read line
  168. do
  169. /bin/bash -c "${0} triggered ${line##* }" || true
  170. done
  171. }
  172. case "${1}" in
  173. configure)
  174. # disable some Upstart services
  175. for init in plymouth-shutdown \
  176. prefdm \
  177. splash-manager \
  178. start-ttys \
  179. tty ; do
  180. if [ -e /etc/init/${init}.conf ]; then
  181. mv -f /etc/init/${init}.conf /etc/init/${init}.conf.disabled
  182. fi
  183. done
  184. # Stops Qt form using the MIT-SHM X11 Shared Memory Extension
  185. echo 'export QT_X11_NO_MITSHM=1' > /etc/profile.d/qt_x11_no_mitshm.sh
  186. chmod 0755 /etc/profile.d/qt_x11_no_mitshm.sh
  187. # Sudo's defualt umask is 077 so set sane default of 022
  188. # Also don't allow QT to used shared memory to prevent errors
  189. echo 'Defaults umask = 0002' > /etc/sudoers.d/umask
  190. echo 'Defaults umask_override' >> /etc/sudoers.d/umask
  191. chmod 0440 /etc/sudoers.d/umask
  192. echo 'Defaults env_keep += "QT_X11_NO_MITSHM"' > /etc/sudoers.d/qt_x11_no_mitshm
  193. chmod 0440 /etc/sudoers.d/qt_x11_no_mitshm
  194. # Create NetworkManager configuration if we do not have it
  195. if ! [ -e /etc/NetworkManager/NetworkManager.conf ]; then
  196. echo '[main]' > /etc/NetworkManager/NetworkManager.conf
  197. echo 'plugins = keyfile' >> /etc/NetworkManager/NetworkManager.conf
  198. echo '[keyfile]' >> /etc/NetworkManager/NetworkManager.conf
  199. fi
  200. # XXX: Test to see if this will satisify dispatcher dependancy
  201. if [ ! -e "/lib/systemd/system/org.freedesktop.nm_dispatcher.service" ]; then
  202. ln -s org.freedesktop.nm_dispatcher.service NetworkManager-dispatcher.service
  203. fi
  204. # Remove old firmware updates link
  205. if [ -L /lib/firmware/updates ]; then
  206. rm -f /lib/firmware/updates
  207. fi
  208. #if ! grep -q '/etc/yum\.conf\.d/qubes-proxy\.conf' /etc/yum.conf; then
  209. # echo >> /etc/yum.conf
  210. # echo '# Yum does not support inclusion of config dir...' >> /etc/yum.conf
  211. # echo 'include=file:///etc/yum.conf.d/qubes-proxy.conf' >> /etc/yum.conf
  212. #fi
  213. # Revert 'Prevent unnecessary updates in VMs':
  214. #sed -i -e '/^exclude = kernel/d' /etc/yum.conf
  215. # ensure that hostname resolves to 127.0.1.1 resp. ::1 and that /etc/hosts is
  216. # in the form expected by qubes-sysinit.sh
  217. for ip in '127\.0\.1\.1' '::1'; do
  218. if grep -q "^${ip}\(\s\|$\)" /etc/hosts; then
  219. sed -i "/^${ip}\s/,+0s/\(\s`hostname`\)\+\(\s\|$\)/\2/g" /etc/hosts
  220. sed -i "s/^${ip}\(\s\|$\).*$/\0 `hostname`/" /etc/hosts
  221. else
  222. echo "${ip//\\/} `hostname`" >> /etc/hosts
  223. fi
  224. done
  225. # remove hostname from 127.0.0.1 line (in debian the hostname is by default
  226. # resolved to 127.0.1.1)
  227. sed -i "/^127\.0\.0\.1\s/,+0s/\(\s`hostname`\)\+\(\s\|$\)/\2/g" /etc/hosts
  228. chown user:user /home_volatile/user
  229. #if [ "${1}" != 1 ] ; then
  230. # # do the rest of %post thing only when updating for the first time...
  231. # exit 0
  232. #fi
  233. if [ -e /etc/init/serial.conf ] && ! [ -f /var/lib/qubes/serial.orig ] ; then
  234. cp /etc/init/serial.conf /var/lib/qubes/serial.orig
  235. fi
  236. # Remove most of the udev scripts to speed up the VM boot time
  237. # Just leave the xen* scripts, that are needed if this VM was
  238. # ever used as a net backend (e.g. as a VPN domain in the future)
  239. #echo "--> Removing unnecessary udev scripts..."
  240. mkdir -p /var/lib/qubes/removed-udev-scripts
  241. for f in /etc/udev/rules.d/*
  242. do
  243. if [ $(basename ${f}) == "xen-backend.rules" ] ; then
  244. continue
  245. fi
  246. if [ $(basename ${f}) == "50-qubes-misc.rules" ] ; then
  247. continue
  248. fi
  249. if echo ${f} | grep -q qubes; then
  250. continue
  251. fi
  252. mv ${f} /var/lib/qubes/removed-udev-scripts/
  253. done
  254. # Create /rw directory
  255. mkdir -p /rw
  256. # XXX: TODO: Needs to be implemented still
  257. #rm -f /etc/mtab
  258. #echo "--> Removing HWADDR setting from /etc/sysconfig/network-scripts/ifcfg-eth0"
  259. #mv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.orig
  260. #grep -v HWADDR /etc/sysconfig/network-scripts/ifcfg-eth0.orig > /etc/sysconfig/network-scripts/ifcfg-eth0
  261. # Enable Qubes systemd units
  262. enableSystemdUnits \
  263. qubes-sysinit.service \
  264. qubes-misc-post.service \
  265. qubes-netwatcher.service \
  266. qubes-network.service \
  267. qubes-firewall.service \
  268. qubes-updates-proxy.service \
  269. qubes-updates-proxy.timer \
  270. qubes-qrexec-agent.service
  271. # Set default "runlevel"
  272. rm -f /etc/systemd/system/default.target
  273. ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
  274. # Process all triggers which will set defaults to wanted values
  275. triggerTriggers
  276. disableSystemdUnits \
  277. alsa-store.service \
  278. alsa-restore.service \
  279. auditd.service \
  280. avahi.service \
  281. avahi-daemon.service \
  282. backuppc.service \
  283. cpuspeed.service \
  284. crond.service \
  285. fedora-autorelabel.service \
  286. fedora-autorelabel-mark.service \
  287. ipmi.service \
  288. hwclock-load.service \
  289. hwclock-save.service \
  290. mdmonitor.service \
  291. multipathd.service \
  292. openct.service \
  293. rpcbind.service \
  294. mcelog.service \
  295. fedora-storage-init.service \
  296. fedora-storage-init-late.service \
  297. plymouth-start.service \
  298. plymouth-read-write.service \
  299. plymouth-quit.service \
  300. plymouth-quit-wait.service \
  301. sshd.service \
  302. tcsd.service \
  303. sm-client.service \
  304. sendmail.service \
  305. mdmonitor-takeover.service \
  306. rngd smartd.service \
  307. upower.service \
  308. irqbalance.service \
  309. colord.service
  310. rm -f /etc/systemd/system/getty.target.wants/getty@tty*.service
  311. # Enable other systemd units
  312. enableSystemdUnits \
  313. rsyslog.service
  314. # XXX: TODO: Needs to be implemented still
  315. # These do not exist on debian; maybe a different package name
  316. # iptables.service \
  317. # ntpd.service \
  318. # ip6tables.service \
  319. ;;
  320. abort-upgrade|abort-remove|abort-deconfigure)
  321. exit 0
  322. ;;
  323. triggered)
  324. for trigger in ${2}; do
  325. case "${trigger}" in
  326. # Update Qubes App Menus
  327. /usr/share/applications)
  328. echo "Updating Qubes App Menus..."
  329. /usr/lib/qubes/qubes-trigger-sync-appmenus.sh || true
  330. ;;
  331. # Install overriden services only when original exists
  332. /lib/systemd/system/NetworkManager.service | \
  333. /lib/systemd/system/NetworkManager-wait-online.service | \
  334. /lib/systemd/system/ModemManager.service)
  335. UNITDIR=/lib/systemd/system
  336. OVERRIDEDIR=/usr/lib/qubes/init
  337. installOverridenServices "${OVERRIDEDIR}" "${trigger}"
  338. if [ $? -eq 0 ]; then
  339. reenableNetworkManager
  340. fi
  341. ;;
  342. # Enable cups only when it is real Systemd service
  343. /lib/systemd/system/cups.service)
  344. echo "Enabling cups"
  345. [ -e /lib/systemd/system/cups.service ] && enableSystemdUnits cups.service
  346. ;;
  347. # "Enable haveged service"
  348. /lib/systemd/system/haveged.service)
  349. echo "Enabling haveged service"
  350. enableSystemdUnits haveged.service
  351. ;;
  352. # Install overridden serial.conf init script
  353. /etc/init/serial.conf)
  354. echo "Installing over-ridden serial.conf init script..."
  355. if [ -e /etc/init/serial.conf ]; then
  356. cp /usr/share/qubes/serial.conf /etc/init/serial.conf
  357. fi
  358. ;;
  359. # Disable SELinux"
  360. /etc/selinux/config)
  361. echo "Disabling SELinux..."
  362. if [ -e /etc/selinux/config ]; then
  363. sed -e s/^SELINUX=.*$/SELINUX=disabled/ </etc/selinux/config >/etc/selinux/config.processed
  364. mv /etc/selinux/config.processed /etc/selinux/config
  365. setenforce 0 2>/dev/null
  366. fi
  367. ;;
  368. # Desktop Entry Modification - Remove existing rules
  369. /etc/xdg/autostart/gpk-update-icon.desktop | \
  370. /etc/xdg/autostart/nm-applet.desktop | \
  371. /etc/xdg/autostart/abrt-applet.desktop | \
  372. /etc/xdg/autostart/notify-osd.desktop)
  373. showIn "${trigger}"
  374. ;;
  375. # Desktop Entry Modification - Not shown in Qubes
  376. /etc/xdg/autostart/pulseaudio.desktop | \
  377. /etc/xdg/autostart/deja-dup-monitor.desktop | \
  378. /etc/xdg/autostart/imsettings-start.desktop | \
  379. /etc/xdg/autostart/krb5-auth-dialog.desktop | \
  380. /etc/xdg/autostart/pulseaudio.desktop | \
  381. /etc/xdg/autostart/restorecond.desktop | \
  382. /etc/xdg/autostart/sealertauto.desktop | \
  383. /etc/xdg/autostart/gnome-power-manager.desktop | \
  384. /etc/xdg/autostart/gnome-sound-applet.desktop | \
  385. /etc/xdg/autostart/gnome-screensaver.desktop | \
  386. /etc/xdg/autostart/orca-autostart.desktop)
  387. showIn "${trigger}" 'NotShowIn=QUBES;'
  388. ;;
  389. # Desktop Entry Modification - Not shown in in DisposableVM
  390. /etc/xdg/autostart/gcm-apply.desktop)
  391. showIn "${trigger}" 'NotShowIn=DisposableVM;'
  392. ;;
  393. # Desktop Entry Modification - Only shown in AppVM
  394. /etc/xdg/autostart/gnome-keyring-gpg.desktop | \
  395. /etc/xdg/autostart/gnome-keyring-pkcs11.desktop | \
  396. /etc/xdg/autostart/gnome-keyring-secrets.desktop | \
  397. /etc/xdg/autostart/gnome-keyring-ssh.desktop | \
  398. /etc/xdg/autostart/gnome-settings-daemon.desktop | \
  399. /etc/xdg/autostart/user-dirs-update-gtk.desktop | \
  400. /etc/xdg/autostart/gsettings-data-convert.desktop)
  401. showIn "${trigger}" 'OnlyShowIn=GNOME;AppVM;'
  402. ;;
  403. # Desktop Entry Modification - Only shown in Gnome & UpdateableVM
  404. /etc/xdg/autostart/gpk-update-icon.desktop)
  405. showIn "${trigger}" 'OnlyShowIn=GNOME;UpdateableVM;'
  406. ;;
  407. # Desktop Entry Modification - Only shown in Gnome & Qubes
  408. /etc/xdg/autostart/nm-applet.desktop)
  409. showIn "${trigger}" 'OnlyShowIn=GNOME;QUBES;'
  410. ;;
  411. *)
  412. echo "postinst called with unknown trigger \`${2}'" >&2
  413. exit 1
  414. ;;
  415. esac
  416. done
  417. exit 0
  418. ;;
  419. *)
  420. echo "postinst called with unknown argument \`${1}'" >&2
  421. exit 1
  422. ;;
  423. esac
  424. # dh_installdeb will replace this with shell code automatically
  425. # generated by other debhelper scripts.
  426. #DEBHELPER#
  427. exit 0
  428. # vim: set ts=4 sw=4 sts=4 et :