agent (unreleased): a boot token that a clock step cannot move (R-1395)

/disks answers guest_boot_key (the kernel's boot_id and the guest init's start) beside the unchanged guest_boot_id.

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
admin committed 2026-11-14 17:43:33 +01:00
1 parent a1135018c7
commit d5ce5c364a
6 files changed
+180 -9

No files matched your search

+53 -6
View File
@@ -1,6 +1,7 @@
package localapi
import (
"regexp"
"bufio"
"context"
"fmt"
@@ -288,21 +289,67 @@ func (b *GuestBinder) guestInitPID(ctx context.Context, vmid int) string {
// /proc/<pid>/stat — ticks since host boot, unique per process launch) changes on a guest reboot. ""
// on any read error (the controller then keeps its last-seen → no spurious recreate).
func (b *GuestBinder) GuestBootID(ctx context.Context, vmid int) string {
pid := b.guestInitPID(ctx, vmid)
if pid == "" {
return ""
}
start := procStarttime(pid)
start := b.guestInitStart(ctx, vmid)
if start == "" {
return ""
}
bt := hostBtime()
bt := hostBtimeFn()
if bt == "" {
bt = "0"
}
return bt + "-" + start
}
// guestInitStart is the guest init's start time in ticks since the host's boot ("" = the guest does not run, or a
// read error). Both boot tokens end with it.
func (b *GuestBinder) guestInitStart(ctx context.Context, vmid int) string {
pid := b.guestInitPID(ctx, vmid)
if pid == "" {
return ""
}
return procStarttimeFn(pid)
}
// GuestBootKey (R-1395) is GuestBootID with the host's boot TIME replaced by the kernel's boot_id:
// "<boot_id>-<guest-init-starttime>". The kernel computes the boot time (`btime`) from the wall clock, so a STEP of
// the host's clock moves GuestBootID — on the N100 on 2026-11-14 the time service came back, stepped +32.7 s, and the
// controller recreated every drive-backed app as if the guest had booted. The kernel's boot_id is a random value made
// once per boot; no clock can move it. Same meaning as before: it changes on a host reboot (new boot_id) and on a
// guest reboot (new init start), and is stable across an agent or controller restart.
//
// "" when the guest does not run or the boot_id cannot be read — the controller then decides by GuestBootID, which
// is still sent (controllers that do not know the key read only that). Pinned by TestGuestBootKey_*.
func (b *GuestBinder) GuestBootKey(ctx context.Context, vmid int) string {
start := b.guestInitStart(ctx, vmid)
return guestBootKey(hostBootIDFn(), start)
}
// guestBootKey joins the two halves; "" when either is missing or the boot_id is not the kernel's shape.
func guestBootKey(bootID, start string) string {
if start == "" || !kernelBootIDRe.MatchString(bootID) {
return ""
}
return bootID + "-" + start
}
var kernelBootIDRe = regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`)
// The three reads of /proc behind the boot tokens, as seams (the tests move the boot time and keep the boot_id).
var (
hostBtimeFn = hostBtime
hostBootIDFn = hostBootID
procStarttimeFn = procStarttime
)
// hostBootID returns the kernel's boot_id (/proc/sys/kernel/random/boot_id): random, made once per boot. "" on error.
func hostBootID() string {
data, err := os.ReadFile("/proc/sys/kernel/random/boot_id")
if err != nil {
return ""
}
return strings.TrimSpace(string(data))
}
// procStarttime returns field 22 (starttime) of /proc/<pid>/stat. The comm field (2) can contain spaces
// and parentheses, so we split AFTER the last ')': field 22 is index 19 of the post-comm fields
// (field 3 = state is index 0). "" on any error.