diff options
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/pacman-offline | 121 |
1 files changed, 93 insertions, 28 deletions
diff --git a/bin/pacman-offline b/bin/pacman-offline index acc8646..73723af 100755 --- a/bin/pacman-offline +++ b/bin/pacman-offline @@ -1,6 +1,6 @@ #!/bin/sh -# (C) 2017-2024 by Christian Hesse <mail@eworm.de> +# (C) 2017-2025 by Christian Hesse <mail@eworm.de> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -10,22 +10,70 @@ set -e function help() { - echo "usage: ${0} [OPTIONS]" - echo - echo ' -f force if other system-update is pending' - echo ' -c clean before download' - echo ' -h this help' - echo ' -r reboot and install immediately' - echo ' -t start timer for nightly reboot' - echo ' -y update sync databases' + cat <<-EOM + usage: ${0} [OPTIONS] + + -a abort pending system-update + -c clean before download + -f force if other system-update is pending + -h this help + -p reboot, install and poweroff immediately + -r reboot and install immediately + -t start timer for nightly reboot + -y update sync databases + EOM } CLEAN=0 +POWEROFF=0 REBOOT=0 TIMER=0 -while getopts 'cfhrty' opt; do +while getopts 'acfhprty' opt; do case ${opt} in + h) + help + exit 0 + ;; + a|c|f|p|r|t|y) + ;; + *) + exit 1 + ;; + esac +done + +if systemd-detect-virt --chroot 2>/dev/null; then + echo 'Running in chroot, skipping.' >&2 + exit 0 +fi + +if [ ! -d /run/systemd/system ]; then + echo 'Current root is not booted, skipping.' >&2 + exit 0 +fi + +if [ "${UID}" -ne 0 ]; then + if command -v pkexec >/dev/null; then + echo 'Missing privileges, trying to elevate.' >&2 + exec pkexec "${0}" "${@}" + fi + + echo "You need elevated privileges. Please run as user 'root'!" >&2 + exit 1 +fi + +OPTIND=1 +while getopts 'acfhprty' opt; do + case ${opt} in + a) + rm --force \ + /system-update \ + /run/systemd/system/systemd-poweroff.service \ + /run/systemd/system/systemd-reboot.service + systemctl daemon-reload + exit 0 + ;; c) if pacman-conf 'CleanMethod' | grep -q 'KeepCurrent'; then CLEAN=1 @@ -36,9 +84,9 @@ while getopts 'cfhrty' opt; do f) rm -f /system-update ;; - h) - help - exit 0 + p) + POWEROFF=1 + REBOOT=1 ;; r) REBOOT=1 @@ -61,14 +109,17 @@ fi # exclude /etc/pacman.d/offline.conf function finish { rm -f /run/pacman.conf; } trap finish EXIT -sed '/^Include *= *\/etc\/pacman\.d\/offline\.conf$/s|^|#|' < /etc/pacman.conf > /run/pacman.conf +sed \ + -e '/^Include *= *\/etc\/pacman\.d\/offline\.conf$/s|^|#|' \ + -e '/^#Include *= *\/etc\/pacman\.d\/offline-include\.conf$/s|^#||' \ + < /etc/pacman.conf > /run/pacman.conf # remove the symlink for now, will be recreated it later rm -f /system-update # check for available updates if [ "$(pacman --config /run/pacman.conf --sync --sysupgrade --print | wc -l)" -eq 0 ]; then - echo "No updates available." + echo 'No updates available.' exit 0 fi @@ -82,26 +133,40 @@ pacman --config /run/pacman.conf --sync --noconfirm --sysupgrade --downloadonly # enable system update ln -sf /var/cache/pacman/pkg /system-update +if [ ${POWEROFF} -eq 1 ]; then + touch /run/pacman-offline-poweroff +fi + +# force a soft-reboot on (manual) reboot ... +ln -sf ../../../usr/lib/systemd/system/systemd-soft-reboot.service \ + /run/systemd/system/systemd-reboot.service + +# ... and also on poweroff, but prepare poweroff +cp /usr/lib/systemd/system/systemd-soft-reboot.service \ + /run/systemd/system/systemd-poweroff.service +cat >> /run/systemd/system/systemd-poweroff.service <<-EOF -# reboot if requested + [Service] + ExecStart=/usr/bin/touch /run/pacman-offline-poweroff + EOF + +# reload for service changes +systemctl daemon-reload + +# (soft-)reboot if requested if [ ${REBOOT} -eq 1 ]; then - if systemctl --dry-run soft-reboot 2>/dev/null; then - echo "Soft-rebooting for update." - systemctl soft-reboot - else - echo "Rebooting for update." - systemctl reboot - fi -# force a soft-reboot on (manual) reboot -elif [ -e /usr/lib/systemd/system/systemd-soft-reboot.service ]; then - ln -sf ../../../usr/lib/systemd/system/systemd-soft-reboot.service /run/systemd/system/systemd-reboot.service - systemctl daemon-reload + echo 'Rebooting for update.' + exec systemctl reboot fi +echo 'Updates will be installed on next reboot.' + # start timer if requested if [ ${TIMER} -eq 1 ]; then systemctl start pacman-offline-reboot.timer fi # show the timer (if active) -systemctl --quiet --no-pager list-timers pacman-offline-prepare.timer pacman-offline-reboot.timer +systemctl --quiet --no-pager list-timers \ + pacman-offline-prepare.timer \ + pacman-offline-reboot.timer |