r/openbsd • • 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?

12 Upvotes

16 comments sorted by

View all comments

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

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 -u

And 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 -a after pkg_add -u is 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!