Skip to the text
KDOS
Menu
kdos.dev KDOS from Scratch KDOS 0.2 · 4dd4a10 · 2026-11-30 Contents Index

95.25. nut-2.8.5

UPS and inverter monitoring that shuts the machine down before the battery runs out

Version 2.8.5, release 1
Shelf hardware — Device access, bus and sensor tools, power management, and Bluetooth
Phase 41_system (System), 673 of 1047 in build order
Licence GPL-2.0-or-later
Homepage https://networkupstools.org/
Build time 1.1 SBU
Installed size 21.7 MiB in 362 files
Recipe ports/core/hardware/nut/kpkgbuild

About

Network UPS Tools monitors uninterruptible power supplies, inverters and power distribution units, and shuts the machine down cleanly before the battery runs out. Its drivers talk to the device, upsd serves the readings, and upsmon watches them and starts the shutdown; upsc and nut-scanner read and find devices. With MODE in /etc/nut/nut.conf the machine is a stand-alone UPS host, a server for other machines, or a client of another machine’s upsd. The init script, the firewall name and the final power cut are described in Administration and Shutdown.

Sources

Dependencies

Depends on: pkgconf, openssl, libusb, libmodbus, libgpiod, i2c-tools, avahi, libtool, eudev, net-snmp, neon, freeipmi, powerman.

Required by: no port names it.

Optional, off in KDOS

These switches in build.sh turn off a feature whose library is a port here. The recipe may turn each off on purpose, or because the library was not yet a port when the recipe was written; this list cannot tell which.

Installation

From the repository root, inside the build container, this is the line kdosbuild runs for the port:

script/chroot/exec.sh bash -c 'source script/phases/41_system/phase.env && kpkg install nut'

What kpkg does

kpkg runs these steps, in this order, as root inside the chroot. The port directory is /ports/core/hardware/nut.

1. Verify every declared source. Each file a sha256 = line names, found in the port directory or in /var/cache/kpkg/sources, must hash to its line; a mismatch stops the build before anything is unpacked.

cd /ports/core/hardware/nut
echo '18bf32e59eb764b13da3c4fa70384926d7fa584cb31d2fe7f137a570633eeec1  nut-2.8.5.tar.gz' | sha256sum -c -

2. Unpack. The work directory is emptied and the first source is unpacked into $SRC with its top directory stripped; any later tarball lands unstripped beside it in $SRC_ROOT, and a file that is not a tarball is copied into $SRC.

SRC_ROOT=/var/cache/kpkg/work/nut
SRC=$SRC_ROOT/nut-2.8.5
PKG=$SRC_ROOT/pkg
rm -rf "$SRC_ROOT"
mkdir -p "$SRC" "$PKG"
tar --no-same-permissions --warning=no-unknown-keyword -xf nut-2.8.5.tar.gz -C "$SRC" --strip-components=1

Every config.guess and config.sub older than automake’s is then replaced with automake’s, so a configure script recognises the musl target.

3. Build. build.sh is sourced in a subshell under set -e, from $SRC, with name, version, release and the recipe’s helpers _series and the five paths PKGNAME, PORT_SRC, SRC_ROOT, SRC and PKG set as shell variables. Its body, verbatim:

./configure --prefix=/usr --sysconfdir=/etc/nut --libexecdir=/usr/lib/nut \
	--datadir=/usr/share/nut \
	--disable-static \
	--with-user=nut --with-group=nut \
	--with-statepath=/run/nut --with-altpidpath=/run/nut \
	--with-drvpath=/usr/lib/nut \
	--with-udev-dir=/usr/lib/udev \
	--with-serial --with-usb --with-modbus --with-gpio --with-linux_i2c \
	--with-snmp --with-neon --with-ipmi --with-freeipmi \
	--with-powerman --without-upower --without-macosx_ups \
	--with-avahi --with-libltdl --with-nut-scanner \
	--with-ssl --with-openssl --without-nss \
	--without-wrap --without-cgi \
	--without-nut_monitor --without-pynut --without-nutconf \
	--with-dev \
	--with-doc=man=dist-auto \
	--with-systemdsystemunitdir=no --with-systemdsystempresetdir=no \
	--with-systemdshutdowndir=no --with-systemdtmpfilesdir=no \
	--with-systemdsysusersdir=no --without-libsystemd \
	--with-hotplug-dir=no --with-devd-dir=no \
	--with-augeas-lenses-dir=no \
	--with-solaris-smf=no --with-solaris-init=no \
	--with-solaris-pkg-svr4=no --with-solaris-pkg-ips=no \
	--enable-cppunit=no
make
make DESTDIR=$PKG install

install -d "$PKG/etc/init.d"
cat > "$PKG/etc/init.d/56_nut.sh" <<'KDOS_SH'
#!/bin/bash
. /etc/init.d/service_helper

CONF="/etc/nut/nut.conf"

# The MODE line of nut.conf, unquoted, without a trailing comment.
nut_mode() {
    [ -r "$CONF" ] || return 0
    while IFS= read -r l; do
        case "$l" in
            MODE=*) l=${l#MODE=}; l=${l//\"/}; echo "${l%%[[:space:]#]*}"; return ;;
        esac
    done < "$CONF"
}

case "$1" in
    start)
        [ ! -x /usr/sbin/upsd ] && { echo "[SKIP] nut: /usr/sbin/upsd not found"; exit 0; }
        mode=$(nut_mode)
        case "$mode" in
            standalone|netserver|netclient) ;;
            *) echo "[SKIP] nut: MODE is '${mode:-none}' in $CONF"; exit 0 ;;
        esac
        if ! getent passwd nut >/dev/null; then
            echo "[SKIP] nut: no nut account (the port's postinstall makes it)"
            exit 0
        fi
        install -d -o nut -g nut -m 770 /run/nut
        echo "[KDOS] Starting nut ($mode)..."
        if [ "$mode" != netclient ]; then
            supervise nut-drivers /usr/sbin/upsdrvctl -F start
            supervise nut-upsd /usr/sbin/upsd -F
        fi
        supervise nut-upsmon /usr/sbin/upsmon -F
        ;;
    stop)
        stop_service nut-upsmon
        stop_service nut-upsd
        stop_service nut-drivers
        ;;
    status)
        check_status nut-upsmon
        ;;
    *)      echo "Usage: $0 {start|stop|status}"; exit 1 ;;
esac
KDOS_SH
chmod 755 "$PKG/etc/init.d/56_nut.sh"

The switches, and why each is set:

The common switches of each build system are explained in the Flag glossary.

postinstall.sh runs on the installed system after the package is adopted:

# The drivers, upsd and upsmon's unprivileged half run as this account, and
# the USB rules the port installs give its group the UPS devices.
#
# Everything is written under PKG_ROOT, the root kpkgadd is installing into:
# --root and an A/B update install into a tree that is not the running one, and
# an account made in / instead would be missing from the system that needs it.
#
# The guard reads PKG_ROOT's files: getent answers for the running root, not
# the tree being installed, and would let useradd run against an account the
# target already has.
root="${PKG_ROOT:-/}"
grep -q '^nut:' "$root/etc/group" 2>/dev/null || groupadd -R "$root" -r nut
grep -q '^nut:' "$root/etc/passwd" 2>/dev/null || \
	useradd -R "$root" -r -g nut -G dialout -d /nonexistent -s /sbin/nologin nut

# The guard leaves an existing account as it is, so the home is set apart
# from it: a tree that already has the account takes /nonexistent too. usermod
# exits 0 without writing when the home is already /nonexistent.
usermod -R "$root" -d /nonexistent nut

4. Package. kpkg removes libtool archives, the info directory index, mimeinfo.cache and every fonts.dir from $PKG, copies the source’s licence files into /usr/share/licenses/nut, prunes what the image does not ship, lints the tree and rolls it reproducibly:

tar --sort=name --format=gnu --numeric-owner --owner=0 --group=0 \
    --mtime=@$SOURCE_DATE_EPOCH \
    --use-compress-program='xz -9 -T0 --block-size=32MiB --no-adjust' \
    -cf nut-2.8.5-1.tar.xz -C "$PKG" .

5. Adopt. kpkgadd installs the package’s files over the root, refusing a path another package owns, records the file list in /var/lib/kpkg/db/nut, and runs the triggers its paths call for.

Contents

Installed programs: nut-scanner, upsc, upscmd, upsd, upsdrvctl, upslog, upsmon, upsrw, upssched, upssched-cmd.

Installed libraries: libnutclient.so, libnutclientstub.so, libnutscan.so, libupsclient.so.

Installed directories: /etc/nut, /usr/lib/nut, /usr/share/doc/nut, /usr/share/licenses/nut, /usr/share/nut.

Short descriptions

nut-scanner Tool to scan communication buses for NUT devices
upsc Lightweight read-only NUT client
upscmd Network UPS Tools device/driver instant commands administration tool
upsd Network UPS Tools data server
upsdrvctl Network UPS Tools driver controller
upslog UPS status logger
upsmon UPS monitor and shutdown controller
upsrw Network UPS Tools device/driver variable administration tool
upssched Timer helper for scheduling events from upsmon
libnutclient.pc UPS monitoring with Network UPS Tools
libnutclientstub.pc Stub for UPS monitoring with Network UPS Tools (primarily for C++ tests)
libnutscan.pc Power devices discovery with Network UPS Tools
libupsclient.pc UPS monitoring with Network UPS Tools

Notes

/etc/nut/*.sample are upstream’s templates and are installed as they are. Nothing starts until /etc/nut/nut.conf exists with a MODE of standalone, netserver or netclient; the init script, 56_nut.sh, reads that line itself and skips otherwise.

The init script supervises each program in the foreground (-F) under ksvc. standalone and netserver start the drivers through upsdrvctl, then upsd and upsmon; netclient starts upsmon alone, watching another machine’s upsd. Each program started as root drops to the nut account itself, except upsmon’s parent, which stays root to run the shutdown. The script creates /run/nut owned by nut, because the drivers and upsd, running as nut, cannot create it under /run, and it skips when the nut account is missing.

postinstall.sh creates the account under PKG_ROOT, the root the package is being installed into, so an install into another tree (--root, or an A/B update) gives that tree the account. The account it creates is also in the dialout group, the group the tree’s udev rules give serial adapters and other plug-in devices; an account that already exists is not added to it. Both checks read etc/group and etc/passwd under PKG_ROOT directly, so they test the tree being installed into rather than the running system’s account database. The account’s home is /nonexistent: the state upsd and the drivers keep is under /run/nut, which the script creates. The hook sets the home with usermod after the creation check, so a tree that already had a nut account with another home is given /nonexistent as well.