2017-10-01 20:48:13 +02:00
#!/bin/bash
2017-01-29 13:55:35 +01:00
qubes_preset_file="75-qubes-vm.preset"
2013-04-12 16:43:46 +02:00
2015-07-08 15:01:21 +02:00
###########################
## Pre-Install functions ##
###########################
2013-04-12 16:43:46 +02:00
2015-07-08 15:01:21 +02:00
update_default_user() {
2017-10-01 20:48:13 +02:00
# Make sure there is a qubes group
groupadd --force --system --gid 98 qubes
# 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.
# See https://bugs.archlinux.org/task/31831
id -u 'user' >/dev/null 2>&1 || {
useradd --user-group --create-home --shell /bin/zsh user
}
usermod -a --groups qubes user
2015-07-08 15:01:21 +02:00
}
2013-04-12 16:43:46 +02:00
## arg 1: the new package version
2013-04-18 13:34:50 +02:00
pre_install() {
2017-10-01 20:48:13 +02:00
echo "Pre install..."
2013-04-26 09:32:10 +02:00
2017-10-01 20:48:13 +02:00
update_default_user
2013-04-26 09:32:10 +02:00
2017-10-01 20:48:13 +02:00
# do this whole %pre thing only when updating for the first time...
2013-04-26 09:32:10 +02:00
2017-10-01 20:48:13 +02:00
mkdir -p /var/lib/qubes
2014-02-03 20:10:36 +01:00
2017-10-01 20:48:13 +02:00
# Backup fstab / But use archlinux defaults (cp instead of mv)
if [ -e /etc/fstab ] ; then
cp /etc/fstab /var/lib/qubes/fstab.orig
fi
2013-04-26 09:32:10 +02:00
2017-10-01 20:48:13 +02:00
# Add qubes core related fstab entries
echo "xen /proc/xen xenfs defaults 0 0" >> /etc/fstab
2015-02-07 12:07:35 +01:00
2017-10-01 20:48:13 +02:00
usermod -p '' root
usermod -L user
2013-04-18 13:34:50 +02:00
}
2013-04-12 16:43:46 +02:00
2015-07-08 15:01:21 +02:00
2013-04-12 16:43:46 +02:00
## arg 1: the new package version
2015-07-08 15:01:21 +02:00
## arg 2: the old package version
pre_upgrade() {
2017-10-01 20:48:13 +02:00
# do something here
echo "Pre upgrade..."
2015-07-08 15:01:21 +02:00
2017-10-01 20:48:13 +02:00
update_default_user
2015-07-08 15:01:21 +02:00
}
2015-10-16 14:34:15 +02:00
###################
## Install Hooks ##
###################
2015-07-08 15:01:21 +02:00
2015-10-16 14:34:15 +02:00
configure_notification-daemon() {
# Enable autostart of notification-daemon when installed
2017-01-29 14:34:50 +01:00
if [ ! -L /etc/xdg/autostart/notification-daemon.desktop ]; then
2015-10-16 14:34:15 +02:00
ln -s /usr/share/applications/notification-daemon.desktop /etc/xdg/autostart/
fi
2015-07-08 15:01:21 +02:00
}
2015-10-16 14:34:15 +02:00
configure_selinux() {
2017-10-01 20:48:13 +02:00
# SELinux is not enabled on archlinux
#echo "--> Disabling SELinux..."
echo "SELINUX not enabled on archlinux. skipped."
# sed -e s/^SELINUX=.*$/SELINUX=disabled/ -i /etc/selinux/config
# setenforce 0 2>/dev/null
2015-07-08 15:01:21 +02:00
}
2015-10-16 14:34:15 +02:00
############################
## Post-Install functions ##
############################
2015-07-08 15:01:21 +02:00
update_qubesconfig() {
2017-10-01 20:48:13 +02:00
# Create NetworkManager configuration if we do not have it
if ! [ -e /etc/NetworkManager/NetworkManager.conf ]; then
echo '[main]' > /etc/NetworkManager/NetworkManager.conf
echo 'plugins = keyfile' >> /etc/NetworkManager/NetworkManager.conf
echo '[keyfile]' >> /etc/NetworkManager/NetworkManager.conf
fi
/usr/lib/qubes/qubes-fix-nm-conf.sh
# Remove ip_forward setting from sysctl, so NM will not reset it
# Archlinux now use sysctl.d/ instead of sysctl.conf
#sed 's/^net.ipv4.ip_forward.*/#\0/' -i /etc/sysctl.conf
2013-04-12 16:43:46 +02:00
2017-10-01 20:48:13 +02:00
# Remove old firmware updates link
if [ -L /lib/firmware/updates ]; then
rm -f /lib/firmware/updates
fi
# Yum proxy configuration is fedora specific
#if ! grep -q '/etc/yum\.conf\.d/qubes-proxy\.conf' /etc/yum.conf; then
# echo >> /etc/yum.conf
# echo '# Yum does not support inclusion of config dir...' >> /etc/yum.conf
# echo 'include=file:///etc/yum.conf.d/qubes-proxy.conf' >> /etc/yum.conf
#fi
#/usr/lib/qubes/update-proxy-configs
# Archlinux pacman configuration is handled in update_finalize
# Location of files which contains list of protected files
mkdir -p /etc/qubes/protected-files.d
2017-10-02 13:49:40 +02:00
# shellcheck source=init/functions
2017-10-01 20:48:13 +02:00
. /usr/lib/qubes/init/functions
# qubes-core-vm has been broken for some time - it overrides /etc/hosts; restore original content
if ! is_protected_file /etc/hosts ; then
if ! grep -q localhost /etc/hosts; then
cat <<EOF > /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 $(hostname)
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
2015-07-14 08:09:11 +02:00
EOF
2015-07-08 15:01:21 +02:00
2017-10-01 20:48:13 +02:00
fi
fi
# ensure that hostname resolves to 127.0.0.1 resp. ::1 and that /etc/hosts is
# in the form expected by qubes-sysinit.sh
if ! is_protected_file /etc/hostname ; then
for ip in '127\.0\.0\.1' '::1'; do
if grep -q "^${ip}\(\s\|$\)" /etc/hosts; then
sed -i "/^${ip}\s/,+0s/\(\s$(hostname)\)\+\(\s\|$\)/\2/g" /etc/hosts
sed -i "s/^${ip}\(\s\|$\).*$/\0 $(hostname)/" /etc/hosts
else
echo "${ip} $(hostname)" >> /etc/hosts
fi
done
fi
# Make sure there is a default locale set so gnome-terminal will start
if [ ! -e /etc/locale.conf ] || ! grep -q LANG /etc/locale.conf; then
touch /etc/locale.conf
echo "LANG=en_US.UTF-8" >> /etc/locale.conf
fi
# ... and make sure it is really generated
# This line is buggy as LANG can be set to LANG="en_US.UTF-8". The Quotes must be stripped
current_locale=$(grep LANG /etc/locale.conf|cut -f 2 -d = | tr -d '"')
if [ -n "$current_locale" ] && ! locale -a | grep -q "$current_locale"; then
base=$(echo "$current_locale" | cut -f 1 -d .)
charmap=$(echo "$current_locale.UTF-8" | cut -f 2 -d .)
[ -n "$charmap" ] && charmap="-f $charmap"
2017-10-02 00:22:40 +02:00
# shellcheck disable=SC2086
localedef -i "$base" $charmap "$current_locale"
2017-10-01 20:48:13 +02:00
fi
2015-07-08 15:01:21 +02:00
}
2017-01-29 13:55:35 +01:00
############################
## Service Management Functions ##
############################
2017-01-29 14:34:50 +01:00
is_static() {
[ -f "/usr/lib/systemd/system/$1" ] && ! grep -q '^[[].nstall]' "/usr/lib/systemd/system/$1"
}
2017-01-29 13:55:35 +01:00
is_masked() {
if [ ! -L /etc/systemd/system/"$1" ]
then
2017-10-01 20:48:13 +02:00
return 1
2017-01-29 13:55:35 +01:00
fi
2017-10-01 20:48:13 +02:00
target=$(readlink /etc/systemd/system/"$1" 2>/dev/null) || :
2017-01-29 13:55:35 +01:00
if [ "$target" = "/dev/null" ]
then
2017-10-01 20:48:13 +02:00
return 0
2017-01-29 13:55:35 +01:00
fi
return 1
}
mask() {
ln -sf /dev/null /etc/systemd/system/"$1"
}
unmask() {
if ! is_masked "$1"
then
return 0
fi
rm -f /etc/systemd/system/"$1"
}
2015-10-16 14:34:15 +02:00
2017-01-29 13:55:35 +01:00
preset_units() {
local represet=
2017-10-02 00:22:40 +02:00
while read -r action unit_name
2017-01-29 13:55:35 +01:00
do
2017-10-01 20:48:13 +02:00
if [ "$action" = "#" ] && [ "$unit_name" = "Units below this line will be re-preset on package upgrade" ]
2017-01-29 13:55:35 +01:00
then
represet=1
continue
fi
echo "$action $unit_name" | grep -q '^[[:space:]]*[^#;]' || continue
2017-10-01 20:48:13 +02:00
[[ -n "$action" && -n "$unit_name" ]] || continue
if [ "$2" = "initial" ] || [ "$represet" = "1" ]
2017-01-29 13:55:35 +01:00
then
if [ "$action" = "disable" ] && is_static "$unit_name"
then
if ! is_masked "$unit_name"
then
# We must effectively mask these units, even if they are static.
mask "$unit_name"
fi
elif [ "$action" = "enable" ] && is_static "$unit_name"
then
if is_masked "$unit_name"
then
# We masked this static unit before, now we unmask it.
unmask "$unit_name"
fi
2017-10-01 20:48:13 +02:00
systemctl --no-reload preset "$unit_name" >/dev/null 2>&1 || :
2017-01-29 13:55:35 +01:00
else
systemctl --no-reload preset "$unit_name" >/dev/null 2>&1 || :
fi
fi
2017-10-02 00:22:40 +02:00
done < "$1"
2017-01-29 13:55:35 +01:00
}
restore_units() {
2017-10-02 13:49:40 +02:00
grep '^[[:space:]]*[^#;]' "$1" | while read -r action unit_name
2017-01-29 13:55:35 +01:00
do
if is_static "$unit_name" && is_masked "$unit_name"
then
# If the unit had been masked by us, we must unmask it here.
# Otherwise systemctl preset will fail badly.
unmask "$unit_name"
fi
systemctl --no-reload preset "$unit_name" >/dev/null 2>&1 || :
done
}
configure_systemd() {
2017-10-01 20:48:13 +02:00
if [ "$1" -eq 1 ]
then
preset_units /usr/lib/systemd/system-preset/$qubes_preset_file initial
changed=true
else
preset_units /usr/lib/systemd/system-preset/$qubes_preset_file upgrade
changed=true
# Upgrade path - now qubes-iptables is used instead
for svc in iptables ip6tables
do
if [ -f "$svc".service ]
then
systemctl --no-reload preset "$svc".service
changed=true
fi
done
fi
2015-10-16 14:34:15 +02:00
2017-10-01 20:48:13 +02:00
if [ "$1" -eq 1 ]
then
# First install.
# Set default "runlevel".
# FIXME: this ought to be done via kernel command line.
# The fewer deviations of the template from the seed
# image, the better.
rm -f /etc/systemd/system/default.target
ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
changed=true
fi
2015-10-16 14:34:15 +02:00
2017-10-01 20:48:13 +02:00
# remove old symlinks
if [ -L /etc/systemd/system/sysinit.target.wants/qubes-random-seed.service ]
then
rm -f /etc/systemd/system/sysinit.target.wants/qubes-random-seed.service
changed=true
fi
if [ -L /etc/systemd/system/multi-user.target.wants/qubes-mount-home.service ]
then
rm -f /etc/systemd/system/multi-user.target.wants/qubes-mount-home.service
changed=true
fi
if [ "x$changed" != "x" ]
then
systemctl daemon-reload
fi
2015-10-16 14:34:15 +02:00
}
2017-01-29 13:55:35 +01:00
######################
## Archlinux Specific Functions ##
######################
2016-01-21 18:21:44 +01:00
config_prependtomark() {
2017-10-01 20:48:13 +02:00
FILE=$1
APPENDBEFORELINE=$2
APPENDLINE=$3
grep -F -q "$APPENDLINE" "$FILE" || sed "/$APPENDBEFORELINE/i$APPENDLINE" -i "$FILE"
2016-01-21 18:21:44 +01:00
}
2016-01-21 07:59:35 +01:00
2016-01-21 18:21:44 +01:00
config_appendtomark() {
2017-10-01 20:48:13 +02:00
FILE=$1
APPENDAFTERLINE=$2
APPENDLINE=$3
grep -F -q "$APPENDLINE" "$FILE" || sed "/$APPENDAFTERLINE/a$APPENDLINE" -i "$FILE"
2016-01-21 07:59:35 +01:00
}
2016-07-11 15:15:45 +02:00
config_cleanupmark() {
2017-10-01 20:48:13 +02:00
FILE="$1"
BEGINMARK="$2"
ENDMARK="$3"
if grep -F -q "$BEGINMARK" "$FILE"; then
if grep -F -q "$ENDMARK" "$FILE"; then
cp "$FILE" "$FILE.qubes-update-orig"
sed -i -e "/^$BEGINMARK$/,/^$ENDMARK$/{
/^$ENDMARK$/b
/^$BEGINMARK$/!d
}" "$FILE"
rm -f "$FILE.qubes-update-orig"
else
echo "ERROR: found $BEGINMARK marker but not $ENDMARK in $FILE. Please cleanup this file manually."
fi
elif grep -F -q "$ENDMARK" "$FILE"; then
echo "ERROR: found $ENDMARK marker but not $BEGINMARK in $FILE. Please cleanup this file manually."
fi
2016-07-11 15:15:45 +02:00
}
2015-10-16 14:34:15 +02:00
update_finalize() {
2017-10-01 20:48:13 +02:00
# Archlinux specific: If marker exists, cleanup text between begin and end marker
QUBES_MARKER="### QUBES CONFIG MARKER ###"
if grep -F -q "$QUBES_MARKER" /etc/pacman.conf; then
config_prependtomark "/etc/pacman.conf" "# REPOSITORIES" "### QUBES CONFIG END MARKER ###"
config_cleanupmark "/etc/pacman.conf" "$QUBES_MARKER" "### QUBES CONFIG END MARKER ###"
# Else, add qubes config block marker
else
config_prependtomark "/etc/pacman.conf" "# REPOSITORIES" "$QUBES_MARKER"
config_prependtomark "/etc/pacman.conf" "# REPOSITORIES" "### QUBES CONFIG END MARKER ###"
fi
# Include /etc/pacman.d drop-in directory
config_appendtomark "/etc/pacman.conf" "$QUBES_MARKER" "Include = /etc/pacman.d/*.conf"
/usr/lib/qubes/update-proxy-configs
# Archlinux specific: Update pam.d configuration for su to enable systemd-login wrapper
# Also remove pam_unix.so from su configuration
# as system-login (which include system-auth) already gives pam_unix.so
# with more appropriate parameters (fix the missing nullok parameter)
2015-07-08 15:01:21 +02:00
2017-10-01 20:48:13 +02:00
if grep -q pam_unix.so /etc/pam.d/su; then
echo "Fixing pam.d"
cat <<EOF > /etc/pam.d/su
2016-04-26 12:57:36 +02:00
#%PAM-1.0
auth sufficient pam_rootok.so
# Uncomment the following line to implicitly trust users in the "wheel" group.
#auth sufficient pam_wheel.so trust use_uid
# Uncomment the following line to require a user to be in the "wheel" group.
#auth required pam_wheel.so use_uid
auth include system-login
account include system-login
session include system-login
EOF
2017-10-01 20:48:13 +02:00
cp /etc/pam.d/su /etc/pam.d/su-l
fi
2015-07-08 15:01:21 +02:00
2017-10-01 20:48:13 +02:00
# Archlinux specific: ensure tty1 is enabled
rm -f /etc/systemd/system/getty.target.wants/getty@tty*.service
systemctl enable getty\@tty1.service
2016-07-11 15:15:45 +02:00
2017-10-01 20:48:13 +02:00
systemctl daemon-reload
2013-04-12 16:43:46 +02:00
}
## arg 1: the new package version
2015-07-08 15:01:21 +02:00
post_install() {
2017-10-01 20:48:13 +02:00
update_qubesconfig
2013-04-12 16:43:46 +02:00
2017-10-01 20:48:13 +02:00
# do the rest of %post thing only when updating for the first time...
if [ -e /etc/init/serial.conf ] && ! [ -f /var/lib/qubes/serial.orig ] ; then
cp /etc/init/serial.conf /var/lib/qubes/serial.orig
fi
2015-07-08 15:01:21 +02:00
2017-10-01 20:48:13 +02:00
# Remove most of the udev scripts to speed up the VM boot time
# Just leave the xen* scripts, that are needed if this VM was
# ever used as a net backend (e.g. as a VPN domain in the future)
#echo "--> Removing unnecessary udev scripts..."
mkdir -p /var/lib/qubes/removed-udev-scripts
for f in /etc/udev/rules.d/*
do
if [ "$(basename "$f")" == "xen-backend.rules" ] ; then
continue
fi
2015-10-15 04:34:55 +02:00
2017-10-01 20:48:13 +02:00
if [ "$(basename "$f")" == "50-qubes-misc.rules" ] ; then
continue
fi
2015-10-15 04:34:55 +02:00
2017-10-01 20:48:13 +02:00
if echo "$f" | grep -q qubes; then
continue
fi
2015-10-15 04:34:55 +02:00
2017-10-01 20:48:13 +02:00
mv "$f" /var/lib/qubes/removed-udev-scripts/
done
2015-07-08 15:01:21 +02:00
2017-10-01 20:48:13 +02:00
mkdir -p /rw
2015-07-08 15:01:21 +02:00
2017-10-01 20:48:13 +02:00
configure_notification-daemon
configure_selinux
2015-07-28 16:12:07 +02:00
2017-10-01 20:48:13 +02:00
configure_systemd 0
2015-07-08 15:01:21 +02:00
2017-10-01 20:48:13 +02:00
update_finalize
2013-04-12 16:43:46 +02:00
}
## arg 1: the new package version
## arg 2: the old package version
2015-07-08 15:01:21 +02:00
post_upgrade() {
2017-10-01 20:48:13 +02:00
update_qubesconfig
2015-07-08 15:01:21 +02:00
2017-10-01 20:48:13 +02:00
configure_notification-daemon
configure_selinux
2015-07-28 16:12:07 +02:00
2017-10-01 20:48:13 +02:00
configure_systemd 1
2015-10-18 11:11:45 +02:00
2017-01-29 14:34:50 +01:00
2017-10-01 20:48:13 +02:00
update_finalize
2013-04-18 13:34:50 +02:00
}
2013-04-12 16:43:46 +02:00
2015-07-08 15:01:21 +02:00
######################
## Remove functions ##
######################
2013-04-12 16:43:46 +02:00
## arg 1: the old package version
2014-02-03 20:10:36 +01:00
pre_remove() {
# no more packages left
if [ -e /var/lib/qubes/fstab.orig ] ; then
mv /var/lib/qubes/fstab.orig /etc/fstab
fi
mv /var/lib/qubes/removed-udev-scripts/* /etc/udev/rules.d/
if [ -e /var/lib/qubes/serial.orig ] ; then
mv /var/lib/qubes/serial.orig /etc/init/serial.conf
fi
2017-01-29 14:34:50 +01:00
2017-10-01 20:48:13 +02:00
if [ "$1" -eq 0 ] ; then
2017-01-29 13:55:35 +01:00
# Run this only during uninstall.
# Save the preset file to later use it to re-preset services there
# once the Qubes OS preset file is removed.
mkdir -p /run/qubes-uninstall
cp -f /usr/lib/systemd/system-preset/$qubes_preset_file /run/qubes-uninstall/
cp -f /usr/lib/systemd/system-preset/$qubes_preset_file /run/qubes-uninstall/
fi
2014-02-03 20:10:36 +01:00
}
2013-04-12 16:43:46 +02:00
## arg 1: the old package version
2014-01-20 10:35:50 +01:00
post_remove() {
2017-01-29 13:55:35 +01:00
changed=
if [ -d /run/qubes-uninstall ]
then
# We have a saved preset file (or more).
# Re-preset the units mentioned there.
restore_units /run/qubes-uninstall/$qubes_preset_file
rm -rf /run/qubes-uninstall
changed=true
fi
if [ "x$changed" != "x" ]
then
systemctl daemon-reload
fi
2014-02-03 20:10:36 +01:00
if [ -L /lib/firmware/updates ] ; then
rm /lib/firmware/updates
fi
2015-10-18 11:11:45 +02:00
rm -rf /var/lib/qubes/xdg
2017-05-29 11:56:42 +02:00
for srv in qubes-sysinit qubes-misc-post qubes-mount-dirs qubes-network qubes-qrexec-agent; do
2015-07-08 15:01:21 +02:00
systemctl disable $srv.service
2015-07-14 08:09:11 +02:00
done
2014-01-20 10:35:50 +01:00
}