Skip to content

Instantly share code, notes, and snippets.

@mentha
Created June 22, 2023 12:26
Show Gist options
  • Save mentha/caebc8edb7743ef1431124ec53479050 to your computer and use it in GitHub Desktop.
Save mentha/caebc8edb7743ef1431124ec53479050 to your computer and use it in GitHub Desktop.
auto system update with dnf
[Unit]
Before=getty-pre.target
Before=multi-user.target graphical.target
[Service]
Type=oneshot
ExecStart=@libexec@/dnf-autoupdate-boot.sh
StandardInput=tty
StandardOutput=tty
StandardError=tty
[Install]
WantedBy=basic.target
Also=dnf-autoupdate.timer
#!/bin/bash
boot_timeout=30 # delay update on boot for 30 seconds, allowing user to skip
. '@configfile@' 2> /dev/null
export LC_ALL=C.UTF-8
if ! (dnf --cacheonly --assumeno update &> /dev/null); then
dnf --cacheonly --assumeno update 2>&1 | head -n-1
echo
if ! read -p "DNF Updates available, apply now? (auto apply in $boot_timeout secs) [y/N] " -t "$boot_timeout" -r reply; then
reply=y
fi
if [ "$reply" = "y" ] || [ "$reply" = "Y" ]; then
dnf --cacheonly --assumeyes update
fi
fi
rm -f '@statefile@'
#!/bin/bash
reboot_on_idle=0 # reboot if security updates were installed and no user sessions present
. '@configfile@' 2> /dev/null
sec_update_installed=0
. '@statefile@' 2> /dev/null
export LC_ALL=C.UTF-8
if ! dnf --refresh --assumeno update --security; then
if dnf --assumeyes update --security; then
sec_update_installed=1
fi
fi
umask 077
echo "sec_update_installed=$sec_update_installed" > '@statefile@'
dnf --downloadonly update
if [ $sec_update_installed -ne 0 ] && [ $reboot_on_idle -ne 0 ]; then
sessions="$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager NCurrentSessions | awk '{ print $2 }')"
if [ "$sessions" = "0" ]; then
systemctl reboot
fi
fi
[Service]
Type=oneshot
ExecStart=@libexec@/dnf-autoupdate-timer.sh
Name: dnf-autoupdate
Version: 20230622
Release: 1%{?dist}
Summary: Automatic system update using dnf
License: Unlicense
BuildArch: noarch
Source0: %{name}-boot.sh.in
Source1: %{name}-timer.sh.in
Source2: %{name}-boot.service.in
Source3: %{name}.service.in
Source4: %{name}.timer
BuildRequires: systemd-rpm-macros
Requires: systemd
%description
Automatic system update using dnf.
%build
for f in %{SOURCE0} %{SOURCE1} %{SOURCE2} %{SOURCE3}; do
sed 's!@libexec@!%{_libexecdir}!g; s!@configfile@!%{_sysconfdir}/%{name}.conf!g; s!@statefile@!/run/%{name}.state!g' < "$f" > "$(basename "$f" | sed 's!\.in$!!')"
done
%install
install -Dm755 -t %{buildroot}/%{_libexecdir} %{name}-boot.sh
install -Dm755 -t %{buildroot}/%{_libexecdir} %{name}-timer.sh
install -Dm644 -t %{buildroot}/%{_unitdir} %{name}-boot.service
install -Dm644 -t %{buildroot}/%{_unitdir} %{name}.service
install -Dm644 -t %{buildroot}/%{_unitdir} %{SOURCE4}
%files
%{_libexecdir}/%{name}-boot.sh
%{_libexecdir}/%{name}-timer.sh
%{_unitdir}/%{name}-boot.service
%{_unitdir}/%{name}.service
%{_unitdir}/%{name}.timer
%post
%systemd_post %{name}-boot.service
%systemd_post %{name}.service
%systemd_post %{name}.timer
%preun
%systemd_preun %{name}-boot.service
%systemd_preun %{name}.service
%systemd_preun %{name}.timer
%postun
%systemd_postun %{name}-boot.service
%systemd_postun %{name}.service
%systemd_postun %{name}.timer
[Timer]
OnBootSec=10min
OnUnitInactiveSec=4h
RandomizedDelaySec=10min
[Install]
WantedBy=timers.target
Also=dnf-autoupdate-boot.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment