Skip to navigation
FreeBSD (pkgbase) automating weekly base updates
17.11.26
# FreeBSD (pkgbase) — checking the version and automating weekly base updates Applies to: **neriene** (FreeBSD with the base system installed as packages, "packaged base" / pkgbase). General rules, not tied to one host. ## 1. Checking which version you run | Command | Shows | |---|---| | `freebsd-version` | installed userland version | | `freebsd-version -k` | kernel FreeBSD matches | | `freebsd-version -u` | userland FreeBSD matches | | `freebsd-version -r` | running kernel | | `freebsd-version -kru` | all three — use this to spot a stale kernel after an update | | `uname -a` | kernel, build time, arch | | `uname -r` | kernel release only | | `uname -U` / `uname -K` | userland / kernel pretty (from the build) | | `sysctl kern.osrelease kern.osreldate kern.ostype` | machine-readable | | `cat /etc/os-release` | `VERSION="14.0-RELEASE-pN"` (13+) | If `freebsd-version -r` and `-u` disagree: you installed a new kernel but haven't rebooted yet. --- ## 2. pkgbase background — why `freebsd-update` says "incompatible" Error: > freebsd-update is incompatible with the use of packaged base. Please see https://wiki.freebsd.org/PkgBase for more information. That means the base system is installed as `pkg` packages (names start with `FreeBSD-`), and **package manager is the update path for the OS**, full stop. `freebsd-update` never works here — do not try, do not work around. Find the base repo name (needed for `-r`): ``` pkg -vv | grep -B1 -A3 base # or cat /etc/pkg/FreeBSD.conf # or pkg repos ``` On this box the base repo is named **`FreeBSD-base`**. Manual equivalent of "freebsd-update fetch + install": ``` pkg update && pkg upgrade # everything, OS + packages # or, OS only: pkg upgrade -r FreeBSD-base ``` If the kernel package changed: **reboot**, then confirm with `freebsd-version -kru`. --- ## 3. Safety — what "stable" does and doesn't buy - Stable/release branches change slowly on purpose (security fixes, errata). Automating **patch-level** base upgrades is a reasonable, common practice. - Not covered: third-party ports, local configs, anything built against base libs. "Stable" is statistical, not a waiver. - A kernel you installed is **not** a kernel you run — reboot still required. - Never automate **release upgrades** (13.x → 14.x): those are `freebsd-update -r
upgrade`-style procedures with a much longer checklist. This automation is for patch updates only. - Make sure the **mail gets read**; silent automation on an unmonitored box is not "safe". - Have a rollback story *in addition to* the automation: - ZFS boot environments: `bectl create pre-update-$(date +%Y%m%d)` before an upgrade, boot into it if needed; - or a working backup/snapshot setup. - `pkg upgrade -n` = dry run, prints what would happen, installs nothing. - `pkg lock -y
` pins a package you never want auto-bumped. - `pkg audit -F` — cheap daily CVE report, worth automating too. --- ## 4. Option B — weekly automation with periodic(8) (chosen setup) With `periodic`, **no custom cron line is needed**: stock `/etc/crontab` already runs `periodic daily` / `weekly` / `monthly`. Verify on the box: ``` grep periodic /etc/crontab ``` If that grep prints nothing (customised crontab, or `weekly_enable="NO"` in `/etc/periodic.conf`), the schedule won't fire — either restore those lines or wrap your own cron line. ### 4.1 Create `/usr/local/etc/periodic/weekly/900.pkg-upgrade` Use `/usr/local/etc/periodic`, **not** `/etc/periodic` — under pkgbase the contents of `/etc` belong to base packages and would fight updates; `/usr/local/etc/periodic` is the local shelf meant for this. Executable, numbered name, mode 755. ```sh #!/bin/sh # Automatic base-system upgrade (pkgbase) - neriene rc=$(/usr/sbin/pkg upgrade -n -r FreeBSD-base 2>/dev/null | tail -n 1) case "$weekly_pkg_upgrade_enable" in [Yy][Ee][Ss]) ;; *) exit 0 ;; esac /usr/sbin/pkg update -f || exit 1 /usr/sbin/pkg upgrade -y -r FreeBSD-base || exit 1 # If a kernel was swapped, the running kernel stays old -> mail about it if [ "$(/usr/bin/uname -r)" != "$(/usr/sbin/freebsd-version -u | sed 's/-p[0-9]*$//')" ]; then echo "KERNEL UPDATE INSTALLED - reboot pending on $(hostname)" exit 1 fi exit 0 ``` ``` chmod 755 /usr/local/etc/periodic/weekly/900.pkg-upgrade ``` Notes: - `-r FreeBSD-base` ⇒ OS only (closest to a cautious `freebsd-update`). Full automation of OS + packages: drop the `-r` and straight `pkg upgrade -y`. - The `uname` vs `freebsd-version` check mails only when a reboot is pending. Delete those lines and the script never makes noise. - periodic convention: `exit 0` = silent, non-zero = output is shown / mailed. ### 4.2 Enable it — `/etc/periodic.conf` ``` weekly_pkg_upgrade_enable="YES" ``` That one line is the master switch. Remove it and the script no-ops forever. ### 4.3 Test it ``` periodic weekly ``` Silent output is **expected** (script exits 0, pkg up to date, nothing to report). If you want to watch it actually do work: ``` env weekly_pkg_upgrade_enable=YES sh -x /usr/local/etc/periodic/weekly/900.pkg-upgrade ``` Direct `sh script` always exits 0 at the `case` — `/etc/periodic.conf` is sourced **only** by `periodic(8)`, not when the script runs standalone. Use `env weekly_pkg_upgrade_enable=YES sh -x ...`. ### 4.4 Debugging: "no output" / "nothing happens" | Check | Command | Symptom → fix | |---|---|---| | script exists + executable | `ls -l /usr/local/etc/periodic/weekly/900.pkg-upgrade` | missing / wrong name (must be `
.
`) / wrong mode | | enabled | `grep pkg /etc/periodic.conf` | missing → add the variable, or the case exits silently | | repo name right | `pkg -vv \| grep -B1 -A3 base` | wrong `-r` value → script fails with no output; fix the repo name | | trace it | `env weekly_pkg_upgrade_enable=YES sh -x ...` | see the exact failing command and `exit=$?` | | state today | `pkg upgrade -n -r FreeBSD-base` | "Your packages are up to date." | | after install | `freebsd-version -kru` | `-r` vs `-u` differ → reboot pending | Reality-check from first run: pkg answered `Your packages are up to date.` and the script exited 0 — i.e. the mechanism works; there was just nothing to install. --- ## 5. Option A — plain cron (rejected, kept for reference) ``` 17 4 * * 0 /usr/sbin/pkg update -f >> /var/log/pkg-upgrade.log 2>&1 && /usr/sbin/pkg upgrade -y >> /var/log/pkg-upgrade.log 2>&1 ``` Same `-r FreeBSD-base` restriction applies. No `periodic.conf` switch, more logging boilerplate. --- ## 6. Kernel reboot policy Weekly `periodic` will install kernel packages when patches ship; nothing reboots for you. Rule of thumb: after an upgrade week where the script said "KERNEL UPDATE INSTALLED", reboot in a maintenance window, then verify `freebsd-version -kru`.
Reply
Anonymous
Information Epoch 1795523552
Use shell scripts to increase leverage and portability.
Home
Notebook
Contact us