diff --git a/docs/os-v3.md b/docs/os-v3.md index 09a6da9f6..ebf7d9972 100644 --- a/docs/os-v3.md +++ b/docs/os-v3.md @@ -156,10 +156,14 @@ named here as what they are: | logging | `06onboard-logs` keeps the boot's logs on `/data/logs` — the partition, not the filesystem that the next appliance replaces | `/data/logs` non-empty on the installed boot | | packages and services | `apt` works and its packages land in the save layer; the appliance's own services are `/etc/init.d` scripts, which is what `rc.services` runs (there is no systemd here, and nothing pretends there is) | — | -| an optional static address | `1bit-network set-static [gateway] [dns…]` writes **`/data/net.conf`** — on the data partition, so it outlives an appliance update — and `/etc/init.d/14onboard-net` applies it at boot through **connman's own CLI** (`connmanctl config --ipv4 manual …`), with `ip` only as the fallback for a link connman has no service for yet. The console's static action stays session-only, because a wrong address on a headless box is a walk to the machine; this one is persistent **and reversible in one command**: `1bit-network reset` deletes the file and hands the interface back to DHCP. An unparsable or unusable file is not applied at all — the boot leaves the link on DHCP and says so in `/var/log/1bit-network.log` | `tests/one_bit_network_conf_test.sh` (the address and file validation, 26 arms, no root or interface needed) and the installed boot's `1bit-network` rows | +| an optional static address | `1bit-network set-static [gateway] [dns…]` writes **`/data/net.conf`** — on the data partition, so it outlives an appliance update — and `/etc/init.d/zz-onboard-net` applies it at boot through **connman's own CLI** (`connmanctl config --ipv4 manual …`), with `ip` only as the fallback for a link connman has no service for yet. The console's static action stays session-only, because a wrong address on a headless box is a walk to the machine; this one is persistent **and reversible in one command**: `1bit-network reset` deletes the file and hands the interface back to DHCP. An unparsable or unusable file is not applied at all — the boot leaves the link on DHCP and says so in `/var/log/1bit-network.log` | `tests/one_bit_network_conf_test.sh` (the address and file validation, the `prefix_to_mask` table and the CIDR split — 43 assertions, no root or interface needed) and the installed boot's `1bit-network` rows | **Recovery, stated where someone under pressure will look:** `1bit-network reset` on tty1, the serial console, or the -console menu's bash item. The file is `/data/net.conf`; deleting it is the same thing. +console menu's bash item — and deleting `/data/net.conf` really is the same thing: the boot undoes a manual configuration +*this box* wrote (it records that in `/data/.net-manual`) and leaves a connection it never touched alone. A file that no +longer parses is treated the same way: the address is handed back rather than left applied invisibly. +*A machine configured while #1513 was merged has a manual connman config and no marker, so run `1bit-network reset` once +after updating it.* ## The Ubuntu plan (history, not a target) diff --git a/os/v3/puppy/README.md b/os/v3/puppy/README.md index 7db5b0d19..73ddff04f 100644 --- a/os/v3/puppy/README.md +++ b/os/v3/puppy/README.md @@ -160,7 +160,7 @@ this recipe ships are: | `05onboard-data` | finds `1BIT-DATA` (`1bit.data=` on the boot line, else the label), mounts it at `/data`, creates the contract dirs, points `/etc/1bit/lemond.conf` at `/data/lemonade/lemond.conf`, copies `/data/ssh/authorized_keys` and links the host key from `/data/ssh`, and registers `/data/models` in Lemonade's `user_models.json` | | `10lemonade` | starts `/opt/1bit-package/lemond-onebit`, binds `0.0.0.0:13305`, logs to `/var/log/lemond-onebit.log`. **No key by default** (cycle 20, the owner's "blank key": the appliance ships like stock Lemonade). A key is opt-in and lives in `/etc/1bit/lemond.conf` (mode 600, a symlink onto `/data`) — console menu item 1 mints one, removes it, and restarts this service so lemond comes up in the other mode; nothing else in the image holds it | | `07onboard-user` | **the installed machine's normal account** (engine#712): on an installed boot only (`/data` mounted with `1bit-install`'s `.installed` on it) it creates `onebit` — home, bash, **locked password** so a key is the only way in — installs `/data/ssh/authorized_keys` as its `0600` key file (re-applied at every installed boot, exactly as `20onboard-ssh` does for root), and appends **one** sudo rule, `onebit ALL=(ALL) NOPASSWD: ALL`, backed up and proved with `sudo -n true` as that user, rolled back if it fails. A live stick exits without touching anything: a machine that forgets stays root-only | -| `14onboard-net` + `usr/local/bin/1bit-network` | **the persistent network identity** (engine#712): `1bit-network set-static [gateway] [dns…]` validates the answer, writes `/data/net.conf` (data partition, so it survives an appliance update) and applies it now through connman's own CLI; `14onboard-net` applies the same file at every boot, and does nothing at all when the file is absent or does not validate (a bad file leaves the link on DHCP rather than half-applying). `1bit-network reset` is the whole recovery path: it deletes the file and gives the interface back to DHCP. The console's own static action stays session-only on purpose | +| `zz-onboard-net` + `usr/local/bin/1bit-network` | **the persistent network identity** (engine#712): `1bit-network set-static [gateway] [dns…]` validates the answer, writes `/data/net.conf` (data partition, so it survives an appliance update) and applies it now through connman's own CLI; `zz-onboard-net` applies the same file at every boot, and does nothing at all when the file is absent or does not validate (a bad file leaves the link on DHCP rather than half-applying). `1bit-network reset` is the whole recovery path: it deletes the file and gives the interface back to DHCP. The console's own static action stays session-only on purpose | | `20onboard-ssh` | `ssh-keygen -A`, `/root/.ssh` 700 + `authorized_keys` 600, then sshd. An image with no key provisions no listener | | `21serial-console` | a root shell on `/dev/ttyS0`, only when the command line carries `1bit.serial=1` (that shell has no password, so it is opt-in) | | `26onboard-board` | `os/mini/board.sh` as `/usr/sbin/1bit-board` (the path release-check's `fans` row reads): EC fan curve, power profile and the detached boost loop, on a board whose DMI it knows; on any other machine it writes nothing and says so | diff --git a/os/v3/puppy/container-build.sh b/os/v3/puppy/container-build.sh index 04cead47e..b2e45345f 100644 --- a/os/v3/puppy/container-build.sh +++ b/os/v3/puppy/container-build.sh @@ -359,7 +359,7 @@ if [ -n "${PAYLOAD:-}" ]; then chmod 0755 "$skeleton/usr/local/bin/1bit-tty1" chmod 0755 "$skeleton"/etc/init.d/03onboard-efivars "$skeleton"/etc/init.d/05onboard-data \ "$skeleton"/etc/init.d/06onboard-logs "$skeleton"/etc/init.d/07onboard-user \ - "$skeleton"/etc/init.d/08onboard-presets "$skeleton"/etc/init.d/14onboard-net \ + "$skeleton"/etc/init.d/08onboard-presets "$skeleton"/etc/init.d/zz-onboard-net \ "$skeleton"/etc/init.d/10lemonade \ "$skeleton"/etc/init.d/18onboard-mdns \ "$skeleton"/etc/init.d/20onboard-ssh "$skeleton"/etc/init.d/21serial-console \ @@ -650,7 +650,7 @@ if [ -n "${PAYLOAD:-}" ]; then usr/local/bin/1bit-record usr/local/bin/1bit-lemonade-webapp \ usr/local/bin/1bit-webapp-proxy usr/local/bin/1bit-desktop usr/local/bin/1bit-logs \ etc/init.d/06onboard-logs etc/init.d/07onboard-user etc/init.d/08onboard-presets \ - etc/init.d/14onboard-net usr/local/bin/1bit-network \ + etc/init.d/zz-onboard-net usr/local/bin/1bit-network \ root/Startup/1bit-lemonade-webapp \ etc/personal_settings_popup_disabled etc/1bit/palemoon-prefs.js \ etc/init.d/24onboard-webapp opt/1bit/webapp/index.html \ diff --git a/os/v3/puppy/embed/etc/init.d/14onboard-net b/os/v3/puppy/embed/etc/init.d/14onboard-net deleted file mode 100755 index e69119cd3..000000000 --- a/os/v3/puppy/embed/etc/init.d/14onboard-net +++ /dev/null @@ -1,127 +0,0 @@ -#!/bin/sh -# Copyright 2026 bong-water-water-bong -# SPDX-License-Identifier: Apache-2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# /etc/init.d/14onboard-net - apply the persistent network identity, if an install has one (engine#712). -# -# `/data/net.conf` is written by `1bit-network set-static` and lives on the DATA partition, so it survives an appliance -# update (nothing the OS set carries is touched). This runs after 05onboard-data has mounted /data and before the -# services that talk to the network; with no file, or a file that does not validate, it does NOTHING and says so - a -# machine that cannot reach the LAN is worse than one on DHCP, and the repair path has to stay obvious: -# -# recovery: 1bit-network reset (tty1, the serial console, or the console menu's bash item) -# -# It never writes the file, never guesses an address, and never leaves the interface without one: connman is asked for -# the manual configuration first (it owns the link on this base), and `ip` is only the fallback for a boot where connman -# has no service for that interface yet. -set -u - -# shellcheck disable=SC2154 # iface/static/gateway/dns are set by read_conf, sourced from 1bit-network below -iface=''; static=''; gateway=''; dns='' # set by read_conf; defined here for `set -u` -DATA=/data -CONF=$DATA/net.conf -LOG=/var/log/1bit-network.log -log() { printf '%s\n' "$*" | tee -a "$LOG"; } - -[ "${1:-start}" = "stop" ] && exit 0 -mkdir -p /tmp 2> /dev/null -: >> "$LOG" 2> /dev/null || LOG=/dev/null - -[ -f "$CONF" ] || { log "14onboard-net: no $CONF: the interface stays on DHCP (connman)"; exit 0; } - -# the same parse and validation as 1bit-network, sourced rather than copied: one definition of "a usable net.conf" -NETWORK=/usr/local/bin/1bit-network -if [ -r "$NETWORK" ]; then - # shellcheck disable=SC1090 # the path is the image's own, and this is the point: one definition of the format - ONEBIT_NETWORK_LIB=1 . "$NETWORK" -fi -if ! command -v read_conf > /dev/null 2>&1 || ! command -v conf_ok > /dev/null 2>&1; then - log "14onboard-net: cannot load the parser from $NETWORK: leaving the network alone (recovery: 1bit-network reset)" - exit 0 -fi - -read_conf "$CONF" -if ! why=$(conf_ok 2>&1); then - log "14onboard-net: $CONF is not usable ($why): leaving the network on DHCP. Recovery: 1bit-network reset" - exit 0 -fi - -# wait for the interface to exist: on a cold boot the driver may still be binding when rc.services starts -i=0 -while [ ! -e "/sys/class/net/$iface" ] && [ $i -lt 20 ]; do sleep 1; i=$((i + 1)); done -if [ ! -e "/sys/class/net/$iface" ]; then - log "14onboard-net: $iface did not appear in 20 s: leaving the network alone" - exit 0 -fi - -applied=no -if command -v connmanctl > /dev/null 2>&1; then - # WHICH service? connman's own name carries the interface's MAC (`ethernet__cable`), so the match is by MAC - # from /sys/class/net: `awk '/^\*/ { print $2; exit }'` (the first version) reconfigured whichever link happened to - # be connected first - on a box with two interfaces that is the wrong one, silently. - mac=$(cat "/sys/class/net/$iface/address" 2> /dev/null | tr -d ':') - svc='' - if [ -n "$mac" ]; then - svc=$(connmanctl services 2> /dev/null | awk -v m="$mac" ' - index($0, m) { for (i = 1; i <= NF; i++) if ($i ~ /^(ethernet|wifi|bluetooth)_/) { print $i; exit } }') - fi - if [ -n "$svc" ]; then - # connman's manual IPv4 configuration takes ADDRESS and NETMASK as two fields (never a CIDR), the gateway third, - # and DNS through --nameservers: `--ipv4 manual "$static" "$gateway"` sent "10.0.2.50/24" as the address and the - # gateway as the netmask (measured by review on #1513). - addr=${static%/*} - mask=$(prefix_to_mask "${static#*/}") - if [ -n "$gateway" ]; then - connmanctl config "$svc" --ipv4 manual "$addr" "$mask" "$gateway" >> "$LOG" 2>&1 - else - connmanctl config "$svc" --ipv4 manual "$addr" "$mask" >> "$LOG" 2>&1 - fi - rc=$? - if [ $rc -eq 0 ] && [ -n "$dns" ]; then - connmanctl config "$svc" --nameservers $dns >> "$LOG" 2>&1 - fi - # and believe connman, not its exit code: read the configuration back - i=0 - while [ $i -lt 10 ]; do - if connmanctl services --properties "$svc" 2> /dev/null | grep -q "Method=manual"; then - applied=yes - break - fi - sleep 1 - i=$((i + 1)) - done - [ "$applied" = yes ] || log "14onboard-net: connman accepted the config but does not report Method=manual" \ - "for $svc (rc=$rc, see $LOG); falling back to ip" - else - log "14onboard-net: connman has no service for $iface (mac ${mac:-unknown}): falling back to ip" - fi -fi - -if [ "$applied" = no ]; then - # no connman service for this link yet, or connman did not take the manual config: put it on with ip, which connman - # leaves alone for as long as it has no service of its own here - if ip addr add "$static" dev "$iface" >> "$LOG" 2>&1; then - [ -n "$gateway" ] && ip route replace default via "$gateway" dev "$iface" >> "$LOG" 2>&1 - applied=yes - fi -fi - -if [ "$applied" = yes ]; then - log "14onboard-net: $iface carries $static${gateway:+ via $gateway} from $CONF" - exit 0 -fi -log "14onboard-net: could not apply $static to $iface (see $LOG); the link keeps whatever DHCP gave it." \ - "Recovery: 1bit-network reset" -exit 0 diff --git a/os/v3/puppy/embed/etc/init.d/zz-onboard-net b/os/v3/puppy/embed/etc/init.d/zz-onboard-net new file mode 100755 index 000000000..86390d327 --- /dev/null +++ b/os/v3/puppy/embed/etc/init.d/zz-onboard-net @@ -0,0 +1,190 @@ +#!/bin/sh +# Copyright 2026 bong-water-water-bong +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# /etc/init.d/zz-onboard-net - apply the persistent network identity, if an install has one (engine#712). +# +# `/data/net.conf` is written by `1bit-network set-static` and lives on the DATA partition, so it survives an appliance +# update (nothing the OS set carries is touched). +# +# WHY THE NAME STARTS WITH zz-: connman ships as `/etc/init.d/connman`, which sorts under "c" - i.e. AFTER every numeric +# script - so a `14onboard-net` ran BEFORE the daemon that owns the link existed: `connmanctl` had no service, the `ip` +# fallback put the address on, and connman then took the link over and flushed it, so the machine came up with no address +# at all for a while (measured: the row saw "" right after the reboot and the static address only minutes later, +# applied by connman from its own stored config). Running after it means the config is given to connman itself. +# +# With NO file - or a file that does not validate - it does nothing to the interface, except undo a manual configuration +# this box set earlier (`/data/.net-manual`): deleting `/data/net.conf` from a rescue shell is the recovery the docs +# promise, and connman would otherwise keep applying the old address at every boot. Doing nothing to a link it did not +# configure is deliberate - a machine that cannot reach the LAN is worse than one on DHCP, and the repair path has to +# stay obvious: +# +# recovery: 1bit-network reset (tty1, the serial console, or the console menu's bash item) +# +# It never writes the file, never guesses an address, and never leaves the interface without one: connman is asked for +# the manual configuration first (it owns the link on this base), and `ip` is only the fallback for a boot where connman +# has no service for that interface yet. +set -u + +# shellcheck disable=SC2154 # iface/static/gateway/dns are set by read_conf, sourced from 1bit-network below +iface=''; static=''; gateway=''; dns='' # set by read_conf; defined here for `set -u` +DATA=/data +CONF=$DATA/net.conf +LOG=/var/log/1bit-network.log +log() { printf '%s\n' "$*" | tee -a "$LOG"; } + +[ "${1:-start}" = "stop" ] && exit 0 +mkdir -p /tmp 2> /dev/null +: >> "$LOG" 2> /dev/null || LOG=/dev/null + +MARK=$DATA/.net-manual + +# load the library first: it defines the format parse AND the service lookup, and the hand-back below needs both +NETWORK=/usr/local/bin/1bit-network +if [ -r "$NETWORK" ]; then + # shellcheck disable=SC1090 # the image's own path; one definition of the format and of the service lookup + ONEBIT_NETWORK_LIB=1 . "$NETWORK" +fi + +# hand_back REASON: give the interface this box configured back to DHCP. connman keeps a manual configuration in its OWN +# state, so removing /data/net.conf is not enough - without this the old address comes back at every boot and the recovery +# the docs promise does not recover (measured, and the review found the two paths that skipped it). +# The marker is removed ONLY when connman actually took the change: a failed hand-back has to stay visible to the next boot. +hand_back() { + miface=$(head -1 "$MARK" 2> /dev/null) + [ -n "$miface" ] || { log "zz-onboard-net: $1: $MARK is empty, so there is nothing to hand back"; return 1; } + # connman starts late (it sorts under "c"), so wait for it to publish a service for that interface before asking - + # the first version looked it up immediately and logged "will retry next boot" at every boot on a slow box + i=0 + while [ $i -lt 30 ]; do + [ -n "$(connman_service_for_iface "$miface" 2> /dev/null)" ] && break + sleep 1 + i=$((i + 1)) + done + svc=$(connman_service_for_iface "$miface" 2> /dev/null) + if [ -n "$svc" ] && connmanctl config "$svc" --ipv4 dhcp >> "$LOG" 2>&1; then + rm -f "$MARK" + log "zz-onboard-net: $1: $svc handed back to DHCP (that manual config was this box's)" + return 0 + fi + log "zz-onboard-net: $1: $miface could not be handed back to DHCP (see $LOG); the marker stays, the next boot retries" + return 1 +} + +# No file: nothing to apply - except undoing a manual configuration this box set earlier, which is the recovery the docs +# promise for `rm /data/net.conf`. A service this box never touched is left alone. +if [ ! -f "$CONF" ]; then + if [ -f "$MARK" ]; then + hand_back "no $CONF" + exit 0 + fi + log "zz-onboard-net: no $CONF: the interface stays on DHCP (connman)" + exit 0 +fi + +# the same parse and validation as 1bit-network, sourced rather than copied: one definition of "a usable net.conf" +NETWORK=/usr/local/bin/1bit-network +if [ -r "$NETWORK" ]; then + # shellcheck disable=SC1090 # the path is the image's own, and this is the point: one definition of the format + ONEBIT_NETWORK_LIB=1 . "$NETWORK" +fi +if ! command -v read_conf > /dev/null 2>&1 || ! command -v conf_ok > /dev/null 2>&1; then + log "zz-onboard-net: cannot load the parser from $NETWORK: leaving the network alone (recovery: 1bit-network reset)" + exit 0 +fi + +read_conf "$CONF" +if ! why=$(conf_ok 2>&1); then + # A broken file must not leave connman applying an address nobody can see: undo our own manual config, and say which + # case this is (the header promised exactly this, and it did not do it - review finding on #1527) + if [ -f "$MARK" ]; then + hand_back "$CONF is not usable ($why)" + else + log "zz-onboard-net: $CONF is not usable ($why): leaving the network on DHCP. Recovery: 1bit-network reset" + fi + exit 0 +fi + +# wait for the interface to exist: on a cold boot the driver may still be binding when rc.services starts +i=0 +while [ ! -e "/sys/class/net/$iface" ] && [ $i -lt 20 ]; do sleep 1; i=$((i + 1)); done +if [ ! -e "/sys/class/net/$iface" ]; then + log "zz-onboard-net: $iface did not appear in 20 s: leaving the network alone" + exit 0 +fi + +applied=no +# connman starts LATE (`/etc/init.d/connman` sorts under "c", after every numeric script), and this script is meant to +# run after it: give it up to 30 s to publish a service for this link rather than racing it +i=0 +while [ $i -lt 30 ]; do + connmanctl services 2> /dev/null | grep -q "$(cat "/sys/class/net/$iface/address" 2> /dev/null | tr -d ':')" && break + sleep 1 + i=$((i + 1)) +done + +if command -v connmanctl > /dev/null 2>&1; then + # WHICH service? connman's own name carries the interface's MAC (`ethernet__cable`), so the match is by MAC + # from /sys/class/net: `awk '/^\*/ { print $2; exit }'` (the first version) reconfigured whichever link happened to + # be connected first - on a box with two interfaces that is the wrong one, silently. + svc=$(connman_service_for_iface "$iface") + if [ -n "$svc" ]; then + # connman's manual IPv4 configuration takes ADDRESS and NETMASK as two fields (never a CIDR), the gateway third, + # and DNS through --nameservers: `--ipv4 manual "$static" "$gateway"` sent "10.0.2.50/24" as the address and the + # gateway as the netmask (measured by review on #1513). + addr=${static%/*} + mask=$(prefix_to_mask "${static#*/}") + if [ -n "$gateway" ]; then + connmanctl config "$svc" --ipv4 manual "$addr" "$mask" "$gateway" >> "$LOG" 2>&1 + else + connmanctl config "$svc" --ipv4 manual "$addr" "$mask" >> "$LOG" 2>&1 + fi + rc=$? + if [ $rc -eq 0 ] && [ -n "$dns" ]; then + connmanctl config "$svc" --nameservers $dns >> "$LOG" 2>&1 + fi + # and believe connman, not its exit code: read the configuration back + i=0 + while [ $i -lt 10 ]; do + if connmanctl services --properties "$svc" 2> /dev/null | grep -q "Method=manual"; then + applied=yes + break + fi + sleep 1 + i=$((i + 1)) + done + [ "$applied" = yes ] || log "zz-onboard-net: connman accepted the config but does not report Method=manual" \ + "for $svc (rc=$rc, see $LOG); falling back to ip" + else + log "zz-onboard-net: connman has no service for $iface (mac ${mac:-unknown}): falling back to ip" + fi +fi + +if [ "$applied" = no ]; then + # no connman service for this link yet, or connman did not take the manual config: put it on with ip, which connman + # leaves alone for as long as it has no service of its own here + if ip addr add "$static" dev "$iface" >> "$LOG" 2>&1; then + [ -n "$gateway" ] && ip route replace default via "$gateway" dev "$iface" >> "$LOG" 2>&1 + applied=yes + fi +fi + +if [ "$applied" = yes ]; then + log "zz-onboard-net: $iface carries $static${gateway:+ via $gateway} from $CONF" + exit 0 +fi +log "zz-onboard-net: could not apply $static to $iface (see $LOG); the link keeps whatever DHCP gave it." \ + "Recovery: 1bit-network reset" +exit 0 diff --git a/os/v3/puppy/embed/usr/local/bin/1bit-network b/os/v3/puppy/embed/usr/local/bin/1bit-network index e458b3e56..ac73d241d 100755 --- a/os/v3/puppy/embed/usr/local/bin/1bit-network +++ b/os/v3/puppy/embed/usr/local/bin/1bit-network @@ -35,6 +35,9 @@ set -u DATA=/data CONF=$DATA/net.conf +# what WE put on connman's own state (it persists a manual config across reboots, independent of net.conf): the boot uses +# it to hand the link back to DHCP when the file is gone, and never touches a configuration it did not write +MARK=$DATA/.net-manual LOG=/var/log/1bit-network.log say() { printf '%s\n' "$*" | tee -a "$LOG"; } die() { say "1bit-network: REFUSED: $*" >&2; exit 2; } @@ -135,6 +138,18 @@ conf_ok() { # the loaded values have to be usable, or the boot must not tou done return 0 } +# connman_service_for_iface IFACE: connman names a service after the link's MAC (`ethernet__cable`), so the match is +# by MAC out of /sys/class/net. Matching on "the first connected service" reconfigures whichever link happened to connect +# first - silently the wrong one on a machine with two interfaces (review finding on #1513). +connman_service_for_iface() { + [ -n "${1:-}" ] || return 1 + command -v connmanctl > /dev/null 2>&1 || return 1 + _csfi_mac=$(cat "/sys/class/net/$1/address" 2> /dev/null | tr -d ':') + [ -n "$_csfi_mac" ] || return 1 + connmanctl services 2> /dev/null | awk -v m="$_csfi_mac" ' + index($0, m) { for (i = 1; i <= NF; i++) if ($i ~ /^(ethernet|wifi|bluetooth)_/) { print $i; exit } }' +} + # on this boot: the interface that carries the default route, else the first one that is not loopback and is up default_iface() { dev=$(ip route show default 2> /dev/null | awk '/^default/ { for (i = 1; i <= NF; i++) if ($i == "dev") { print $(i + 1); exit } }') @@ -190,14 +205,24 @@ fi if [ "$mode" = reset ]; then read_conf "$CONF" rm -f "$CONF" || fail "cannot remove $CONF" - # give the interface back to DHCP through connman, so a wrong address really is gone and not just forgotten + # the marker goes only after connman has been handed back below; if that fails it stays, and the next boot retries + # give the interface back to DHCP through connman, so a wrong address really is gone and not just forgotten. + # The lookup is by MAC (the old one took the first connected service on any link, which on a two-interface machine + # is the wrong one), and the result is reported rather than assumed. if [ -n "${iface:-}" ] && command -v connmanctl > /dev/null 2>&1; then - svc=$(connmanctl services 2> /dev/null | awk -v i="$iface" ' - /^\*/ { name = $2; mac = $3 } - NF == 1 && name { print name; exit } - { }' | head -1) - [ -n "$svc" ] && connmanctl config "$svc" --ipv4 dhcp >> "$LOG" 2>&1 && \ - say "1bit-network: $svc back to DHCP" + svc=$(connman_service_for_iface "$iface") + if [ -n "$svc" ]; then + if connmanctl config "$svc" --ipv4 dhcp >> "$LOG" 2>&1; then + say "1bit-network: $svc back to DHCP" + # ONLY here: with the file already gone, a failed hand-back that also dropped the marker would leave no + # file, no marker and connman still applying the old address for ever - the hole this marker exists for + rm -f "$MARK" 2> /dev/null + else + say "1bit-network: connman refused to put $svc back on DHCP (see $LOG); the marker stays and the next boot retries" + fi + else + say "1bit-network: connman has no service for $iface; the marker stays and the next boot retries" + fi fi say "1bit-network: reset - $CONF removed, the next boot and this one use DHCP" say " (if the link still carries the old address, restart the network: /etc/init.d/connman restart)" @@ -224,7 +249,7 @@ esac tmp=$CONF.tmp { - echo "# written by 1bit-network on $(date -u +%FT%TZ); applied at boot by /etc/init.d/14onboard-net" + echo "# written by 1bit-network on $(date -u +%FT%TZ); applied at boot by /etc/init.d/zz-onboard-net" echo "# recovery: 1bit-network reset (from tty1, the serial console or the console menu)" echo "iface=$dev" echo "static=$addr" @@ -236,10 +261,11 @@ tmp=$CONF.tmp true } > "$tmp" || fail "cannot write $tmp" mv -f "$tmp" "$CONF" || fail "cannot install $CONF" +printf '%s\n' "$dev" > "$MARK" 2> /dev/null # so a boot without the file can undo what this command did sync say "1bit-network: persisted to $CONF (iface=$dev static=$addr gateway=${gw:--} dns=${dnsv:--})" -/etc/init.d/14onboard-net start || fail "applied the file but the interface did not take the address (see $LOG)" +/etc/init.d/zz-onboard-net start || fail "applied the file but the interface did not take the address (see $LOG)" if ip -4 -o addr show dev "$dev" 2> /dev/null | grep -q " ${addr%/*}/"; then say "1bit-network: $dev carries ${addr} now, and will again at the next boot" [ -z "$gw" ] || say " gateway $gw" diff --git a/os/v3/puppy/install-probe.sh b/os/v3/puppy/install-probe.sh index 436c0e9d8..4924ad78b 100755 --- a/os/v3/puppy/install-probe.sh +++ b/os/v3/puppy/install-probe.sh @@ -709,20 +709,31 @@ if assert_pupsfs: print("FAIL net the machine did not come back after the static-address reboot"); ok = False v.close() else: + # zz-onboard-net runs AFTER connman (which sorts under "c"), so it lands later than the shell: wait for + # it rather than sampling the instant the shell appears (measured: "" at the shell, the address + # minutes later from connman's own stored config - which is what "applied" must never look like) + val("WAITNET", "n=0; while [ $n -lt 18 ]; do " + "ip -4 -o addr show dev %s 2>/dev/null | grep -q '%s' && break; " + "sleep 5; n=$((n+1)); done; echo waited" % (dev, static_addr), timeout=180) per = val("PERSIST", "ip -4 -o addr show dev %s 2>/dev/null | awk '{ print $4 }'" % dev) f = val("CONF", "grep -c '^static=' /data/net.conf 2>/dev/null") - cm = val("CONNMAN", "connmanctl services --properties 2>/dev/null | grep -c 'Method=manual'") - print("INFO net after the reboot: %s | net.conf static lines=%s | connman Method=manual=%s" % (per or "", f, cm)) + # `connmanctl services --properties` does not print the IPv4 configuration on this build (measured), so + # the proof is the applier's own log line from THIS boot plus an address DHCP would never hand out: + # an address cannot survive a reboot, so .250 on the interface means something applied it after boot. + bootline = val("BOOTLOG", "grep -c 'zz-onboard-net: .*carries 10.0.2.250' /var/log/1bit-network.log 2>/dev/null") + print("INFO net after the reboot: %s | net.conf static lines=%s | applier log lines for it=%s" + % (per or "", f, bootline or "0")) if static_addr not in per: print("FAIL net after the reboot the interface does not carry the static %s (%s)" % (static_addr, per or "")); ok = False elif f != "1": print("FAIL net /data/net.conf lost its static line across the reboot (%s)" % (f or "0")); ok = False - elif not cm.isdigit() or int(cm) < 1: - print("FAIL net connman does not report Method=manual after the reboot: the address may be" - " left over from before, not applied (%s)" % (cm or "")); ok = False + elif not bootline.isdigit() or int(bootline) < 1: + print("FAIL net the applier did not log that it applied the file after the reboot (%s lines)," + " so the address has no explanation on this boot" % (bootline or "")); ok = False else: - print("PASS net after a reboot %s carries %s and connman reports Method=manual: the address" - " is applied from /data/net.conf, not inherited" % (dev, static_addr)) + print("PASS net after a reboot %s carries %s (which DHCP never hands out) and" + " /var/log/1bit-network.log has this boot's application from /data/net.conf" + % (dev, static_addr)) val("RESETNET", "1bit-network reset 2>&1 | tr '\\n' '; '", timeout=180) left = val("LEFT", "ls /data/net.conf 2>&1 | head -1") # and DHCP has to come back on its own: a reset that leaves the box on the static address is a @@ -739,6 +750,31 @@ if assert_pupsfs: else: print("PASS net reset removed /data/net.conf and the link is back on DHCP (%s)" % dhcp_addr.split("/")[0]) + # ---- and the OTHER documented recovery: delete /data/net.conf by hand (a rescue shell), reboot. + # connman keeps a manual configuration in its own state, so without the marker the box would stay + # on the static address for ever while the doc says "deleting it is the same thing" (review on + # #1527). Re-apply, then delete the file instead of using reset. + val("SETAGAIN", "1bit-network set-static %s %s >/dev/null 2>&1;" % (static_addr, gw or "") + + " rm -f /data/net.conf; ls /data/.net-manual >/dev/null 2>&1 && echo marker", timeout=240) + if not v.reboot(): + print("FAIL net the machine did not come back after the hand-deleted net.conf"); ok = False + v.close() + else: + val("WAITDHCP", "n=0; while [ $n -lt 24 ]; do " + "ip -4 -o addr show dev %s 2>/dev/null | grep -q '%s' && break; " + "sleep 5; n=$((n+1)); done; echo waited" % (dev, dhcp_addr.split('/')[0]), timeout=180) + back = val("DHCPBACK", "ip -4 -o addr show dev %s 2>/dev/null | awk '{ print $4 }'" % dev) + mk = val("MARK", "ls /data/.net-manual 2>&1 | head -1") + print("INFO net after deleting net.conf by hand and rebooting: %s | marker: %s" + % (back or "", mk[-40:])) + if dhcp_addr.split("/")[0] not in (back or ""): + print("FAIL net with net.conf deleted by hand the link did not return to DHCP (%s):" + " the documented hand-recovery does not recover" % (back or "")); ok = False + elif "No such file" not in mk: + print("FAIL net the marker survived: the box keeps undoing DHCP every boot (%s)" % mk[-40:]); ok = False + else: + print("PASS net deleting /data/net.conf by hand is a real recovery: the next boot" + " hands the link back to DHCP (%s) and clears the marker" % dhcp_addr.split("/")[0]) else: print("INFO net no global IPv4 address on this boot (%s): the static-address rows are a SKIP" % (cur or "")) wrote = val("WROTE", "sh -c 'echo 712 > /root/712-persist; sync; cat /root/712-persist'")