#!/bin/sh # (C) 2017-2024 by Christian Hesse # # 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 # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. 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' } CLEAN=0 REBOOT=0 TIMER=0 while getopts 'cfhrty' opt; do case ${opt} in c) if pacman-conf 'CleanMethod' | grep -q 'KeepCurrent'; then CLEAN=1 else echo 'The clean method is not set to keep current. Not cleaning.' >&2 fi ;; f) rm -f /system-update ;; h) help exit 0 ;; r) REBOOT=1 ;; t) TIMER=1 ;; y) pacman --sync --refresh ;; esac done # make sure no other update is pending if [ -e '/system-update' -a "$(readlink '/system-update')" != '/var/cache/pacman/pkg' ]; then echo 'Another update is pending.' >&2 exit 1 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 # 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." exit 0 fi # clean cache if [ ${CLEAN} -eq 1 ]; then pacman --sync --noconfirm --clean fi # download packages pacman --config /run/pacman.conf --sync --noconfirm --sysupgrade --downloadonly # enable system update ln -sf /var/cache/pacman/pkg /system-update # 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 fi # 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