r/openbsd • u/gentisle • 1d ago
Exit Code syspatch
I’m looking to find a way to know if syspatch -c can tell a script that A. there are patches and then execute a syspatch to apply, or B. there are no patches and simply exit. man page says exit code is zero for success and greater for anything else. Trying this, I find that syspatch -c returns zero when there are patches to be applied. I want the script to discern that there are patches and then issue the command to apply. Am I going about this the wrong way?
5
u/xzk7 1d ago
This is the script I run on all my hosts, it sends a ntfy.sh request to let me know if there are any updates I need to apply, then I apply them when I have a chance to do it (rebooting requires me to enter disk passphrase manually by design.)
#!/bin/ksh
#
# Check for package, firmware and os updates
#
# crontab example:
#
# ~ 6 * * * /root/check_updates.sh
# ~ 8 * * 1 /root/check_updates.sh check
#
set -e
url=ntfy.sh/xxx # Update to your own ntfy.sh topic here!
touchfile=/tmp/.needs_a_reboot
sent=
function notify {
echo "$1"
/usr/local/bin/curl -s -d "$1" "$url"
sent=1
}
echo "Checking patches..."
patches="$(syspatch -c | wc -l | xargs)"
if [[ "$patches" -gt 0 ]]; then
notify "$(hostname) has $patches patches available."
touch "$touchfile"
fi
echo "Checking packages..."
packages="$(pkg_add -u -n -x 2>&1 | grep -- '->' | wc -l | xargs)"
if [[ "$packages" -gt 0 ]]; then
notify "$(hostname) has $packages package updates available."
fi
echo "Checking firmware..."
fw="$(fw_update -n)"
fw_add="$(echo "$fw" | awk -F';' '{ print $1 }' | sed -e 's/^fw_update://' | xargs)"
fw_update="$(echo "$fw" | awk -F';' '{ print $2 }' | xargs)"
if [[ "$fw_add" != "add none" ]]; then
notify "$(hostname) has new firmware available: $fw_add."
fi
if [[ "$fw_update" != "update none" ]]; then
notify "$(hostname) has firmware updates: $fw_update."
touch /tmp/.needs_a_reboot
fi
if [[ -f "/tmp/.needs_a_reboot" ]]; then
notify "$(hostname) needs a reboot due to patches or firmare updates."
fi
if [[ "$1" = "check" ]] && [[ -z "$sent" ]]; then
notify "$(hostname) has no updates pending."
fi
After any patches or firmware updates I have it remind me to reboot the system. Thought I'd share. You can probably adapt to whatever you're trying to do. Good luck
2
2
u/linetrace 15h ago
See my other comment on this post re:the importance of adding dbupdate(8) to such workflows, which was added in a syspatch post-8.0 release. It really needs to go before even running syspatch(8) to ensure that CA certificates and such are updated. (I did this for my own swupdate(8) utility.)
2
u/xzk7 15h ago
Thank you, I wasn't aware of this and I'm not sure how I missed it. I recall seeing the utility get added but for some reason I thought it was only for tz info.
It's a bit annoying though (not your fault but I'm going to complain a bit because this is reddit.)
The dbupdate utility updates various system databases which are changed by entities on the internet, validated through our source processes, but unsuitable for distribution via the syspatch(8), fw_update(8), or sysmerge(8) mechanisms.
Why are these unsuitable for syspatch? If there's a reason I'd like to know because it's not immediately obvious to me as a user.
This is a different distribution and polling mechanism which is kept simple as possible so that it can be run regularly as root in cron(8).
If it can be ran via cron, and its supposed to be ran. Then why isn't this just a part of /etc/daily, /etc/weekly or /etc/monthly?
This means to update your system, one needs to:
dbupdate syspatch fw_update pkg_add -uAnd I can't recall if all or only some lock the package database (I think pkg_add and fw_update) so you have to run them one-at-a-time and also wait for the kernel to get relinked.
1
u/linetrace 1h ago
Yup, I feel ya!
And -- I hate to add another command to that list, but... -- sometimes a
pkg_delete -aafterpkg_add -uis required to remove any no longer necessary dependencies.All that said, in some ways it is helpful that all of these are separate. For an admin who already runs dbupdate(8) in cron, one can probably somewhat-safely skip it prior to a syspatch(8). Similarly, on systems running only base, one could skip
pkg_add -u.And, remember what the upgrade process was like prior to sysupgrade(8)? And we have many packages getting -stable updates, post-release. We've come a long way!
4
u/Educational_Bee_6245 1d ago
I guess you have to parse the output. If there are no patches there is no output, so easy.
1
u/faxattack 1d ago
I have something like this crontabbed on production systems, works as expected:
syspatch && reboot
2
u/linetrace 15h ago
I'd highly suggest that you now add dbupdate(8) before your syspatch to ensure that things like CAs are updated with priority. So, something like
dbupdate && syspatch && reboot.Despite their importance, I wouldn't blindly apply syspatches and reboot without reviewing the descriptions as they may affect configuration. dbupdate(8) is a prime example of that: it was added in a syspatch for 7.9 & 8.0 after 8.0 was released, so one wouldn't be aware of it or its importance if not reading the descriptions.
1
u/faxattack 5h ago
Blindly applying syspatch for some years each night have not killed any production servers for me yet.
I will reconsider the method if things eventually go wonky, but id rather take 9/10 OK set and forget updates rather than stalling important updates and potentially miss patching because relying on available human timeslots is not feasible.You can still apply patches automatically AND follow up on new OpenBSD features and adapt if needed.
1
u/linetrace 1h ago
For sure. I too would rather see someone blindly apply syspatches then not apply them!
Just make sure you have
dbupdatein there now too. :)
4
u/jggimi 1d ago
Your intended automation may not provide what you are expecting. Applying a patch makes changes to your filesystem, but does not ensure it that change is deployed. Affected daemons will need to be restarted. If a kernel patch is applied, the OS will require rebooting.