openwrt-iac / uapi / docs / 4.0.1 / Non-uci state: what uapi covers and what it doesn't
uapi's design contract is "uci is the source of truth". Almost every resource
translates HTTP REST verbs into uci_set / uci_commit followed by a daemon
reload. That model covers most of OpenWrt's configuration surface, but not all
of it. This document inventories the small set of state that lives outside uci,
in two categories:
The bar for adding to category 1 is high. New non-uci resources should justify themselves against three questions: is the underlying daemon's uci surface genuinely missing this? would the right long-term fix be to add a uci option upstream rather than carry workaround state in uapi? does the resource fit a curated CRUD shape, or is it a one-shot action better handled by an operator tool?
| Resource | Source of truth | Lock | Reload | Notes |
|---|---|---|---|---|
packages/installed |
apk DB (apk add/del shell-out) |
with_lock on global |
none | postinst runs as root; package's responsibility to wire its own uci/init scripts |
packages/feeds |
/etc/apk/repositories.d/*.list + apk update |
with_lock on global |
none | url-validated, name-validated |
dhcp/leases |
/tmp/dhcp.leases (v4), ubus call dhcp ipv6leases of odhcpd (v6, via ?family=v6) |
none (read-only) | n/a | dnsmasq IPv4 and odhcpd IPv6 leases |
network/wireguard_kernel_peers |
wg show <iface> dump (parse) |
none (read-only) | n/a | kernel wg peers that no uci section claims |
system/password |
/bin/busybox passwd <user> (stdin pipe) |
with_lock on global |
none | write-only; audit logs token+user, never the password |
system/authorized_keys |
/etc/dropbear/authorized_keys (mode 0600) |
with_lock on global for writes; reads lock-free |
none | dropbear re-reads the file per connection |
system/cron_jobs |
/etc/crontabs/root (mode 0600) |
with_lock on global for writes; reads lock-free |
cron |
one entry per line; other lines stay byte for byte |
Each entry below expands on its lock semantics, audit shape, the file/process it touches, and why the standard uci-transaction recipe doesn't apply.
Adding a non-uci resource means adding a row above. The bar is high: prefer
driving the underlying daemon's uci surface if the option exists, or upstreaming
the option to OpenWrt uci if it doesn't, before adding non-uci state to uapi.
Each item that it answers holds runtime, {} when it reads no live state, and
its schema requires it (#322). make_collection adds {} to each item of a
read-only collection, and a writable one puts it in its item view.
packages/installedapk info
(apk-tools 3.x returns nothing for --installed); writes shell out to apk
add <name> and apk del <name> via fs.popen. A single package's version
comes from apk query --from installed --format json, because the
<name>-<version> line of apk info cannot be split right for a name that
ends in a dash and digits.apk add refreshes a feed index only when its cached copy
is older than 4 hours, or missing, as after a reboot (the cache is under
/var/cache/apk, on tmpfs). Inside that window a feed can rebuild a package
in place (-r2 to -r3) and drop the old file, so the cached index names a
download that 404s, or it can add a package the cached index does not list.
When apk add fails, uapi runs apk update and tries apk add once more,
and the second result is the one reported. The retry runs even when apk
update fails, because it exits with the number of feeds it could not refresh:
one unreachable feed makes it fail after it has refreshed all the others. A
refresh that uapi killed at its bound skips the retry: the feed that held it
is still stale, so the retry would wait on it as long again. A successful
install costs nothing extra. A failure that a refresh cannot fix, such as a
bad name, pays for one refresh before it is reported. Read-only apk commands
(apk search, apk fetch, any --simulate) never refresh, so they can show
a stale index that apk add would refresh.reload_budget_s of config limits (20 s unless set) to apk add, apk del
and apk update as --timeout, and kills apk update when it runs longer
than that. A kill frees the apk lock, which apk opens with O_CLOEXEC, and
keeps the previous index, which apk replaces through a temporary file. uapi
does not kill apk add or apk del, because a kill in the middle of an
install leaves a package half done. Their time stays unbounded: each stale
index and each package is a fetch that can stall for up to the bound, a name
lookup takes as long as the resolver allows, and the scripts of a package run
as long as they run. GNU wget, which wget-ssl or wget-nossl puts in place
of uclient-fetch, tries a fetch that times out up to 20 times, so
--timeout then bounds each try only.transaction.with_lock on /var/lock/uapi.lock. apk has its own DB
lock; the uapi flock serialises concurrent uapi requests so they don't race
apk-vs-apk and surface as nondeterministic 5xxs. Contention returns 423
locked with Retry-After: 1.^[A-Za-z0-9_+][A-Za-z0-9_+.-]*$.
Names starting with - or . are rejected to prevent apk-flag injection
(--allow-untrusted, --repository=...); the shell-out also uses apk add --
<name> as a defense-in-depth separator.uapi-pkg-failure <request_id> action=install name="..." exit=<n>
output="..."). The HTTP response carries only a generic "apk add failed (exit
N); see syslog /var/lib/apk/...), not a uci config. The right place to drive it is apk
itself.packages/feeds/etc/apk/repositories.d/*.list.packages/installed. After every write, apk update is
invoked so the new feed is immediately available.apk update IS the reload. It runs under the time bound of
packages/installed, and the feed file stays written when it fails. A
create reports the result in update_status: ok, apk update failed (exit
N); see syslog <request_id>, or apk update did not finish within N s and was
killed; see syslog <request_id>. A remove answers 204 whatever apk update
returns.^[A-Za-z0-9_][A-Za-z0-9_.-]*$ (no
leading dot, no leading dash). URL must be http:// or https://. file://
and other schemes are rejected./etc/apk/repositories.d/ directly. There is no
uci wrapper for it upstream.dhcp/leases (read-only)/tmp/dhcp.leases (the dnsmasq lease dump) for IPv4.
?family=v6 reads the IPv6 leases of odhcpd, see below (merged in v4, #320).<expires_at> <mac> <ip> <hostname>
<client id>. dnsmasq writes * for a missing host name or client id, and
each reads null. A client id reads as duid. dnsmasq writes 0 for a lease
that does not expire, and expires_at reads null for it, as for an IPv6
lease and as rpcd-mod-luci reads it./etc/config/.dhcp/leases?family=v6 (read-only)ubus call dhcp ipv6leases of odhcpd, as rpcd-mod-luci
reads it for LuCI since f6eea3082e. odhcpd can change or drop its lease file,
and writes it from a timer, up to one second after a change. When odhcpd does
not run, the list is empty.ipv6-addr list for an address lease (IA_NA) and an
ipv6-prefix list for a delegated prefix (IA_PD). The read keeps the
leases with the flag bound, as LuCI and the lease file do, and gives one
entry for each address or prefix. prefix_length is null for an address.
iaid is the hex of the number, as the lease file wrote it, and
expires_at is the time of the read plus valid, or null for 4294967295.
hostname is the name that the client sent, null for the empty one, with a
control byte, the backslash and each byte that is not part of valid UTF-8
escaped as \xNN in lowercase hex, as rpcd-mod-luci escapes it, so the JSON
stays valid. The v4 read escapes the hostnames of dnsmasq the same way.
interface is the device that odhcpd names, for example br-lan.
dhcp/servers counts the leases of its interface, a lease of several
addresses once, through the l3_device that netifd reports for it. Two uci
interfaces on one device count each other's leases.ip of its last entry, for the
IPv4 leases too: dnsmasq can hold two leases for one MAC, and holds one lease
for each address. The Link to the next page keeps the family.network/wireguard_kernel_peers (read-only)wg show <interface> dump per
up interface whose uci section has proto=wireguard. The dump is cached on the
connection for the request, so a read of network/wireguard_peers and a read
of this collection in one request share it. An interface that netifd does not
report up holds no peer and lists nothing. An up interface whose wg show
fails, or prints a dump in a shape that uapi does not read, lists nothing too,
and the response names it in X-Kernel-Unread, so that a failed read never
looks like a kernel that holds no drift.wg show runs outside every transaction, as
the runtime read does.wireguard_<interface>
claims its key. The key is read as wg reads the stored public_key, the later
PublicKey= lines of one value included. A disabled section claims too: the
runtime of a disabled section already reports a peer that the kernel holds
under its key, so listing it would report the drift twice.wg set added it, or a failed apply left it behind. Before this collection it
was the only wireguard state that no read showed, because the runtime of
network/wireguard_peers annotates each section and lists nothing on its own.system/password (write-only)/etc/shadow, via passwd(1).transaction.with_lock./bin/busybox passwd <user> and pipes
<pw>\n<pw>\n through stdin (the same recipe LuCI uses for its "Router
Password" page).user must match ^(root|[a-z][a-z0-9_-]*)$. password must
be at least 8 characters. A user that /etc/passwd does not hold answers
422 conflict on user, and passwd does not run. busybox passwd finds the
user with getpwnam and exits 1 for an unknown one. /etc/shadow need not
hold the user: passwd then writes the hash into /etc/passwd. LuCI does no
such check, but its page only sets the password of root.uapi-passwd-set <request_id> user="...".
Failures emit uapi-passwd-failure <request_id> user="..." exit=<n>. The
password VALUE is never logged.POST /api/v4/system/password returns 204. There is no
GET (password hashes don't leak through the API at all).passwd -d root line, etc.)
is the alternative for purely offline provisioning.system/authorized_keys/etc/dropbear/authorized_keys (mode 0600).transaction.with_lock for writes; reads are lock-free.500: reading it as empty would let an add or a remove rewrite
it with only its own keys, and lock root out of SSH.<type> <base64-blob>
[comment]. Allowed types: ssh-rsa, ssh-ed25519,
ecdsa-sha2-nistp{256,384,521}, sk-ssh-ed25519@openssh.com,
sk-ecdsa-sha2-nistp256@openssh.com. Options blocks (command="...",
no-pty, etc.) before the key type are stripped from the canonical form.^[a-f0-9]{12}$. Same key always gets the same id, idempotent across re-adds.GET lists; POST adds one; PUT replaces wholesale
(dedup); DELETE /<id> removes one.fs plugin.system/cron_jobs/etc/crontabs/root, read by busybox crond. Other users'
crontabs are out of scope.transaction.with_lock for writes. Reads are lock-free.authorized_keys. Every other line stays byte for byte: entries from
packages, comments, and MAILTO=, SHELL= and PATH= lines. An appended
entry goes at the end, so an environment line earlier in the file applies to
it. A crontab that exists but cannot be read fails the request with 500,
because treating it as empty would let the write replace every line in it.\ with the next one,
and drops the backslash, before it looks for #. Measured on the device:
touch /tmp/c1 \ followed by /tmp/c2 ran touch /tmp/c1 /tmp/c2, and a
comment that ends in \ hid the entry below it. uapi reads the file in the
same groups. A delete removes every physical line of its entry, and an append
after a trailing backslash first adds a blank line, which ends the
continuation without changing what crond reads for the line before./etc/init.d/cron reload. The init script registers each crontab
with procd as a watched file, so the reload restarts crond only when the
content changed. It is also what starts crond on a stock image: there is no
crontab there, and the init script does not start crond while /etc/crontabs/
is empty. A failed reload puts the old file back and reloads again, and the
request answers reload_failed_restored or reload_failed_unrecovered, as a
curated write does. uapi waits for the reload and the one of the restore for
20 s in all, as for a curated write, and does not kill them: a reload still
running then keeps the new file, and the write answers 200 with
X-Reload-Status: unverified, which a cron write carries in that case only, a
syslog warning uapi-cron-reload-unverified whose note names the reload,
and crond_running as procd reports it at that moment. Removing the last
entry leaves an empty file, so crond keeps running with nothing to do.runtime.crond_running asks procd whether crond runs, on every
read and after every write, delete included. The reload exit status cannot
tell: with an invalid system.cronloglevel the init script prints "validation
failed", exits 0, and procd stops crond. A write in that state answers 200
with crond_running: false. uapi does not know whether a job ran, and does
not claim to.# ends the line wherever it
appears, environment lines and lines short of five fields plus a command are
skipped, and field spacing collapses. A line from another writer therefore
shows up as crond sees it, which is not always how it looks. Where its
schedule is one crond misreads, such as minute 75 or weekday 7, the entry
says so with runtime.runs_as_written: false.unknown_field. busybox crond does not refuse a
bad field: it logs a parse error and keeps the line with a half-filled
schedule, so minute 75 runs every minute and weekday 7 runs every day. The
schedule is five fields separated by single spaces, each a list of values,
ranges (wrapping ones such as 22-2 included) and *, with a /step after
* or a range. Ranges: minute 0-59, hour 0-23, day 1-31, month 1-12 or
jan-dec, weekday 0-6 or sun-sat. Refused, each for a reason crond
gives:@daily, @reboot and the other special schedules. OpenWrt builds busybox
without FEATURE_CROND_SPECIAL_TIMES: crond skips a short @daily line and
misreads a long one.5/10 as 5 alone, and other crons
read it as 5-59/10 or refuse it.*/N in the day field. busybox fills * for days from day 0, so */2 runs
on days 2 to 30 where other crons run on days 1 to 31, and it ORs such a day
with a restricted weekday where other crons AND them. 1-31/N means the
same everywhere.# in the command. crond cuts the line there: touch /tmp/a#b runs touch
/tmp/a.\, which joins the next line into it.%. busybox crond gives it no special meaning, so it passes through to
the shell unchanged.sha256("<schedule>
<command>"). A change to either field gives a new id, so an update is a
delete plus a create. Identical lines share an id: a read lists them once, and
a delete removes them all.GET lists, POST adds one, and DELETE /<id> removes one
and answers 200 with the removed entry and crond's state after the reload.
There is no PUT for the whole file, although that is how LuCI edits it (one
textarea, saved whole). Packages write to the same file: acme adds `0 0 * *each time it starts, andsafe-searchandwg-installer` add their own lines. A client that owned the whole file would
delete those lines, and the packages would add them back on their next start.system:cron_jobs:ro or a scope above it, *:ro included. A command can
carry a credential, so keep secrets in a 0600 file that the command reads. The
command stays readable because a client cannot adopt or compare an entry it
cannot see.uapi-cron-job-add <request_id> id=<id>, which ties
the generic write line to the entry it made. The command is not logged, since
commands can carry credentials.system.cronloglevel, and busybox crond reads only crontab
files. LuCI writes the file directly too.The following items are real configuration concerns that uapi deliberately does
NOT cover. For each, the recommended out-of-band path is listed. An IaC
orchestrator typically wires these once at image-bake time (cloud-init /
/etc/uci-defaults/) rather than reaching for them on every reconcile.
/etc/inittab is not uci./etc/inittab at first boot.etherwake via packages/installed and run it
from a script over SSH or via a separate orchestration tool. The MAC +
interface map is operator memory.px5g_x509uhttpd/certs curates the px5g cert-generation PARAMETERS
(days, bits, CN, etc.), but the actual one-shot regeneration is an action. uci
doesn't model it.px5g_x509 (or acme.sh / luci-app-acme for real
certs) out-of-band. For production, an ACME workflow keyed off a cron or
systemd timer is the standard approach.sysctl tunables from an OPNsense-style migrationsysctl
parameter) and drop it into /etc/sysctl.d/ at image-bake time.vnstat/config (including its interfaces list) for configuring the
collector; the database itself stays where vnstat puts it.If your real-world configuration needs something uapi can't currently express, the first question to ask is: does the underlying OpenWrt package expose this via uci? If yes, the curated resource is undercurated; file an issue. If no, the right fix is usually to upstream a uci option, not to add another non-uci resource to uapi.
If, after weighing those, a new non-uci resource really IS the right answer, it needs to ship together with:
<domain>:<resource>:rw / :ro).