9753a77
#!/bin/bash
9753a77
#
9753a77
# Do automatic relabelling
9753a77
#
9753a77
9753a77
# . /etc/init.d/functions
9753a77
4af347c
# If the user has this (or similar) UEFI boot order:
4af347c
#
4af347c
#             Windows | grub | Linux
4af347c
#
4af347c
# And decides to boot into grub/Linux, then the reboot at the end of autorelabel
4af347c
# would cause the system to boot into Windows again, if the autorelabel was run.
4af347c
#
4af347c
# This function restores the UEFI boot order, so the user will boot into the
4af347c
# previously set (and expected) partition.
4af347c
efi_set_boot_next() {
4af347c
    # NOTE: The [ -x /usr/sbin/efibootmgr ] test is not sufficent -- it could
4af347c
    #       succeed even on system which is not EFI-enabled...
4af347c
    if ! efibootmgr > /dev/null 2>&1; then
4af347c
        return
4af347c
    fi
4af347c
4af347c
    # NOTE: It it possible that some other services might be setting the
4af347c
    #       'BootNext' item for any reasons, and we shouldn't override it if so.
4af347c
    if ! efibootmgr | grep --quiet -e 'BootNext'; then
4af347c
        CURRENT_BOOT="$(efibootmgr | grep -e 'BootCurrent' | sed -re 's/(^.+:[[:space:]]*)([[:xdigit:]]+)/\2/')"
4af347c
        efibootmgr -n "${CURRENT_BOOT}" > /dev/null 2>&1
4af347c
    fi
4af347c
}
4af347c
9753a77
relabel_selinux() {
9753a77
    # if /sbin/init is not labeled correctly this process is running in the
9753a77
    # wrong context, so a reboot will be required after relabel
9753a77
    AUTORELABEL=
9753a77
    . /etc/selinux/config
9753a77
    echo "0" > /sys/fs/selinux/enforce
a16e7bc
    [ -x /bin/plymouth ] && plymouth --quit
9753a77
9753a77
    if [ "$AUTORELABEL" = "0" ]; then
9753a77
	echo
9753a77
	echo $"*** Warning -- SELinux ${SELINUXTYPE} policy relabel is required. "
9753a77
	echo $"*** /etc/selinux/config indicates you want to manually fix labeling"
9753a77
	echo $"*** problems. Dropping you to a shell; the system will reboot"
9753a77
	echo $"*** when you leave the shell."
9753a77
	sulogin
9753a77
9753a77
    else
9753a77
	echo
9753a77
	echo $"*** Warning -- SELinux ${SELINUXTYPE} policy relabel is required."
9753a77
	echo $"*** Relabeling could take a very long time, depending on file"
9753a77
	echo $"*** system size and speed of hard drives."
9753a77
9753a77
	FORCE=`cat /.autorelabel`
9753a77
        [ -x "/usr/sbin/quotaoff" ] && /usr/sbin/quotaoff -aug
db14db8
	/sbin/fixfiles $FORCE restore
9753a77
    fi
4af347c
9753a77
    rm -f  /.autorelabel
9753a77
    /usr/lib/dracut/dracut-initramfs-restore
4af347c
    efi_set_boot_next
3bbe617
    if [ -x /usr/bin/grub2-editenv ]; then
3bbe617
        grub2-editenv - incr boot_indeterminate >/dev/null 2>&1
3bbe617
    fi
f5a2299
    sync
9753a77
    systemctl --force reboot
9753a77
}
9753a77
9753a77
# Check to see if a full relabel is needed
9753a77
if [ "$READONLY" != "yes" ]; then
9753a77
    restorecon $(awk '!/^#/ && $4 !~ /noauto/ && $2 ~ /^\// { print $2 }' /etc/fstab) >/dev/null 2>&1
9753a77
    relabel_selinux
9753a77
fi