5b70e6
#!/bin/sh
5b70e6
5b70e6
# This systemd.generator(7) detects if SELinux is running and if the
5b70e6
# user requested an autorelabel, and if so sets the default target to
5b70e6
# selinux-autorelabel.target, which will cause the filesystem to be
5b70e6
# relabelled and then the system will reboot again and boot into the
5b70e6
# real default target.
5b70e6
5b70e6
PATH=/usr/sbin:$PATH
5b70e6
unitdir=/usr/lib/systemd/system
5b70e6
5b70e6
# If invoked with no arguments (for testing) write to /tmp.
5b70e6
earlydir="/tmp"
5b70e6
if [ -n "$2" ]; then
5b70e6
    earlydir="$2"
5b70e6
fi
5b70e6
5b70e6
set_target ()
5b70e6
{
5b70e6
    ln -sf "$unitdir/selinux-autorelabel.target" "$earlydir/default.target"
bf43bd
    AUTORELABEL="1"
bf43bd
    source /etc/selinux/config
bf43bd
    if [ "$AUTORELABEL" = "0" ]; then
bf43bd
        mkdir -p "$earlydir/selinux-autorelabel.service.d"
bf43bd
        cat > "$earlydir/selinux-autorelabel.service.d/tty.conf" <
bf43bd
[Service]
bf43bd
StandardInput=tty
bf43bd
EOF
bf43bd
    fi
5b70e6
}
5b70e6
5b70e6
if selinuxenabled; then
5b70e6
    if test -f /.autorelabel; then
5b70e6
        set_target
5b70e6
    elif grep -sqE "\bautorelabel\b" /proc/cmdline; then
5b70e6
        set_target
5b70e6
    fi
5b70e6
fi