Skip to content

Commit d56d3d3

Browse files
Enrique SaurezCopilot
andcommitted
guest: route new sandbox boots through nvx-init
Install through the existing fixed guest-binary list and preserve the legacy-token and blockless routes. Pass the known helper PIDs and move the runtime adapter overlay to the new init path. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 10c9dff8-5c75-4c96-9739-3896adfb0c71
1 parent 206b3f1 commit d56d3d3

6 files changed

Lines changed: 203 additions & 17 deletions

File tree

‎guest/common/init‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ case "$cmdline" in
3535
*" nvx_sandbox=1 "*) sandbox_mode=true ;;
3636
*) sandbox_mode=false ;;
3737
esac
38+
new_init=false
39+
if [ "$sandbox_mode" = true ]; then
40+
case "$cmdline" in
41+
*" nvx_lifecycle="*) ;;
42+
*) new_init=true ;;
43+
esac
44+
fi
3845

3946
fatal() {
4047
echo "$*" >&2
@@ -189,7 +196,7 @@ for token in $cmdline; do
189196
esac
190197
done
191198
set +f
192-
if [ -n "$nvx_exec" ]; then
199+
if [ -n "$nvx_exec" ] && [ "$new_init" = false ]; then
193200
case "$nvx_exec" in
194201
/*) ;;
195202
*) fatal "nvx_exec must be an absolute initramfs path" ;;
@@ -206,6 +213,7 @@ if [ "$sandbox_mode" = false ]; then
206213
mount_virtfs
207214
fi
208215
# virt-net: if --net attached a NIC, configure the only non-loopback interface.
216+
announcer_pid=
209217
nip=$(sed -n 's/.*virtnet_ip=\([^ ]*\).*/\1/p' /proc/cmdline)
210218
if [ -n "$nip" ]; then
211219
nmask=$(sed -n 's/.*virtnet_mask=\([^ ]*\).*/\1/p' /proc/cmdline)
@@ -289,6 +297,7 @@ if [ -n "$nip" ]; then
289297
allow | host-loopback)
290298
if [ -n "$ngw" ] && [ -x /sbin/nvx-ingress ]; then
291299
/sbin/nvx-ingress "$ngw" </dev/null &
300+
announcer_pid=$!
292301
else
293302
virtnet_error "$ningress ingress needs /sbin/nvx-ingress and an IPv4 gateway"
294303
fi
@@ -308,9 +317,24 @@ if [ -n "$nip" ]; then
308317
fi
309318
fi
310319
if [ "$sandbox_mode" = true ]; then
311-
[ -x /sbin/nvx-init-agent ] ||
320+
if [ "$new_init" = false ]; then
321+
[ -x /sbin/nvx-init-agent ] ||
322+
workload_fatal "selected guest cannot act as sandbox control"
323+
exec /sbin/nvx-init-agent
324+
fi
325+
[ -x /sbin/nvx-init ] ||
312326
workload_fatal "selected guest cannot act as sandbox control"
313-
exec /sbin/nvx-init-agent
327+
# nvx-time boot returns before its child publishes the daemon PID.
328+
tries=0
329+
while [ ! -r /run/nvx/time/daemon.pid ] && [ "$tries" -lt 600 ]; do
330+
sleep 0.05
331+
tries=$((tries + 1))
332+
done
333+
time_daemon=$(cat /run/nvx/time/daemon.pid) ||
334+
workload_fatal "time daemon process ID is unavailable"
335+
set -- "$time_daemon"
336+
[ -z "$announcer_pid" ] || set -- "$@" "$announcer_pid"
337+
exec /sbin/nvx-init "$@"
314338
fi
315339
case "$workload_lifecycle" in
316340
one-shot)

‎scripts/test_nvx_tools.py‎

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18440,6 +18440,168 @@ def _shell_function(source: str, name: str) -> str:
1844018440
return source[start : source.index("\n}\n", start) + 3]
1844118441

1844218442

18443+
class GuestInitRoutingTests(unittest.TestCase):
18444+
INIT = Path(__file__).parents[1] / "guest" / "common" / "init"
18445+
18446+
def setUp(self):
18447+
shell = _posix_shell()
18448+
if shell is None:
18449+
self.skipTest("POSIX shell is unavailable")
18450+
self.shell = shell
18451+
temporary = tempfile.TemporaryDirectory()
18452+
self.addCleanup(temporary.cleanup)
18453+
self.root = Path(temporary.name)
18454+
self.pid = self.root / "daemon.pid"
18455+
self.announcer = self.root / "announcer.pid"
18456+
(self.root / "net" / "eth0").mkdir(parents=True)
18457+
(self.root / "os-release").write_text("ID=alpine\n", encoding="utf-8")
18458+
(self.root / "alpine-release").write_text("test\n", encoding="utf-8")
18459+
self.helpers: dict[str, Path] = {}
18460+
for name, script in {
18461+
"nvx-time": f"printf '4242\\n' >{shlex.quote(self.pid.as_posix())}\n",
18462+
"nvx-init": 'printf "new-init"; printf " <%s>" "$@"; echo\n',
18463+
"nvx-init-agent": 'printf "old-init"; printf " <%s>" "$@"; echo\n',
18464+
"nvx-ingress": (
18465+
f'printf "%s\\n" "$$" >{shlex.quote(self.announcer.as_posix())}\n'
18466+
),
18467+
"nvx-hostmount": "echo hostmount\n",
18468+
"shell": "echo blockless-shell\n",
18469+
}.items():
18470+
path = self.root / name
18471+
path.write_text(f"#!/bin/sh\n{script}", encoding="utf-8")
18472+
path.chmod(0o755)
18473+
self.helpers[name] = path
18474+
18475+
def _run(self, cmdline: str) -> subprocess.CompletedProcess[str]:
18476+
command_line = self.root / "cmdline"
18477+
command_line.write_text(cmdline, encoding="utf-8")
18478+
source = self.INIT.read_text(encoding="utf-8")
18479+
replacements = {
18480+
"/sbin/nvx-init-agent": self.helpers["nvx-init-agent"],
18481+
"/sbin/nvx-init": self.helpers["nvx-init"],
18482+
"/sbin/nvx-time": self.helpers["nvx-time"],
18483+
"/sbin/nvx-ingress": self.helpers["nvx-ingress"],
18484+
"/sbin/nvx-hostmount": self.helpers["nvx-hostmount"],
18485+
"/proc/cmdline": command_line,
18486+
"/proc/sys/kernel/modules_disabled": self.root / "modules_disabled",
18487+
"/sys/class/net": self.root / "net",
18488+
"/run/nvx/time/daemon.pid": self.pid,
18489+
"/etc/os-release": self.root / "os-release",
18490+
"/etc/alpine-release": self.root / "alpine-release",
18491+
"exec /bin/sh": self.helpers["shell"],
18492+
}
18493+
for original, replacement in replacements.items():
18494+
target = shlex.quote(replacement.as_posix())
18495+
if original == "exec /bin/sh":
18496+
target = f"exec {target}"
18497+
source = source.replace(original, target)
18498+
source = source.replace("/sbin/nvx-exit", "nvx_exit")
18499+
source = source.replace("/sbin/nvx-snapshot", "nvx_snapshot")
18500+
return subprocess.run(
18501+
[self.shell, "-s"],
18502+
input=(
18503+
"mount() { :; }\nifconfig() { :; }\nroute() { :; }\n"
18504+
'nvx_exit() { exit "$1"; }\n'
18505+
"nvx_snapshot() { echo blockless-snapshot; }\n"
18506+
f"{source}"
18507+
),
18508+
text=True,
18509+
capture_output=True,
18510+
check=False,
18511+
timeout=40,
18512+
)
18513+
18514+
def test_tokenless_layered_and_fresh_sandboxes_use_new_init(self):
18515+
for tokens in (
18516+
"nvx_layer=distro,0xd0003000,erofs nvx_overlay_upper=ramfs",
18517+
"nvx_snapshot_tier=platform",
18518+
"nvx_snapshot_tier=platform nvx_mode=managed",
18519+
):
18520+
with self.subTest(tokens=tokens):
18521+
result = self._run(f"console=hvc0 nvx_sandbox=1 {tokens}")
18522+
self.assertEqual(result.returncode, 0, result.stderr)
18523+
self.assertEqual(result.stdout, "new-init <4242>\n")
18524+
18525+
def test_old_lifecycle_tokens_keep_the_old_layered_init(self):
18526+
for lifecycle in ("managed", "one-shot", ""):
18527+
with self.subTest(lifecycle=lifecycle):
18528+
result = self._run(
18529+
"nvx_sandbox=1 nvx_layer=distro,0xd0003000,erofs "
18530+
f"nvx_overlay_upper=ramfs nvx_lifecycle={lifecycle}"
18531+
)
18532+
self.assertEqual(result.returncode, 0, result.stderr)
18533+
self.assertTrue(result.stdout.startswith("old-init"), result.stdout)
18534+
self.assertNotIn("new-init", result.stdout)
18535+
18536+
def test_exec_override_cannot_bypass_the_new_sandbox_init(self):
18537+
result = self._run(
18538+
"nvx_sandbox=1 nvx_snapshot_tier=platform nvx_exec=/bin/true"
18539+
)
18540+
self.assertEqual(result.returncode, 0, result.stderr)
18541+
self.assertEqual(result.stdout, "new-init <4242>\n")
18542+
for tokens in ("", "nvx_sandbox=1 nvx_lifecycle=managed"):
18543+
with self.subTest(tokens=tokens):
18544+
result = self._run(f"{tokens} nvx_exec=/bin/true")
18545+
self.assertEqual(result.returncode, 0, result.stderr)
18546+
self.assertEqual(result.stdout, "")
18547+
18548+
def test_blockless_platform_and_snapshot_vms_keep_their_routes(self):
18549+
for tokens in ("", "nvx_snapshot_tier=platform", "shellsnap snap"):
18550+
with self.subTest(tokens=tokens):
18551+
result = self._run(f"console=hvc0 {tokens}")
18552+
self.assertEqual(result.returncode, 0, result.stderr)
18553+
self.assertIn("hostmount\n", result.stdout)
18554+
self.assertIn("blockless-shell\n", result.stdout)
18555+
self.assertNotIn("new-init", result.stdout)
18556+
self.assertNotIn("old-init", result.stdout)
18557+
if tokens == "shellsnap snap":
18558+
self.assertEqual(result.stdout.count("blockless-snapshot\n"), 2)
18559+
self.assertIn("SNAP-RESTORE-OK\n", result.stdout)
18560+
18561+
def test_new_init_receives_the_announcers_process_id_when_present(self):
18562+
for ingress in ("allow", "host-loopback"):
18563+
with self.subTest(ingress=ingress):
18564+
result = self._run(
18565+
"nvx_sandbox=1 nvx_snapshot_tier=platform "
18566+
"virtnet_ip=172.16.0.2 virtnet_gw=172.16.0.1 "
18567+
f"virtnet_ingress={ingress}"
18568+
)
18569+
self.assertEqual(result.returncode, 0, result.stderr)
18570+
pid = self.announcer.read_text(encoding="utf-8").strip()
18571+
self.assertGreater(int(pid), 1)
18572+
self.assertIn(f"new-init <4242> <{pid}>\n", result.stdout)
18573+
18574+
def test_new_init_waits_for_the_time_daemons_deferred_publication(self):
18575+
temporary = self.pid.with_name("daemon.pid.tmp")
18576+
self.helpers["nvx-time"].write_text(
18577+
"#!/bin/sh\n"
18578+
f"(sleep 0.1; printf '4242\\n' >{shlex.quote(temporary.as_posix())}; "
18579+
f"mv {shlex.quote(temporary.as_posix())} "
18580+
f"{shlex.quote(self.pid.as_posix())}) &\n",
18581+
encoding="utf-8",
18582+
)
18583+
result = self._run("nvx_sandbox=1 nvx_snapshot_tier=platform")
18584+
self.assertEqual(result.returncode, 0, result.stderr)
18585+
self.assertEqual(result.stdout, "new-init <4242>\n")
18586+
18587+
def test_missing_helpers_or_selected_init_fail_without_fallback(self):
18588+
self.helpers["nvx-time"].write_text("#!/bin/sh\nexit 0\n", encoding="utf-8")
18589+
self.pid.mkdir()
18590+
result = self._run("nvx_sandbox=1 nvx_snapshot_tier=platform")
18591+
self.assertEqual(result.returncode, 125, result.stdout)
18592+
self.assertIn("time daemon process ID is unavailable", result.stderr)
18593+
self.assertNotIn("new-init", result.stdout)
18594+
18595+
self.helpers["nvx-init"].unlink()
18596+
result = self._run("nvx_sandbox=1 nvx_snapshot_tier=platform")
18597+
self.assertEqual(result.returncode, 125, result.stdout)
18598+
self.assertIn("selected guest cannot act as sandbox control", result.stderr)
18599+
self.assertNotIn("old-init", result.stdout)
18600+
result = self._run("nvx_sandbox=1 nvx_lifecycle=managed")
18601+
self.assertEqual(result.returncode, 0, result.stderr)
18602+
self.assertTrue(result.stdout.startswith("old-init"), result.stdout)
18603+
18604+
1844318605
class GuestInitWorkloadIdentityTests(unittest.TestCase):
1844418606
"""The shell /init's workload account and identity checks, as a one-shot
1844518607
launch that asks for an account with nvx_workload_account=create runs

‎tests/runtime-adapter/README.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,8 +1116,9 @@ NVX's initramfs hands a VM of the managed lifecycle to NVX's managed agent: its
11161116
`/init` runs
11171117
`exec /sbin/nvx-managed-agent /dev/hvc2 - nvx-managed UID GID NAME HOME` as
11181118
PID 1. A VM with a layered root, whose kernel command line has
1119-
`nvx_sandbox=1`, goes to NVX's init agent instead, which assembles the root:
1120-
`/init` runs `exec /sbin/nvx-init-agent`, without arguments.
1119+
`nvx_sandbox=1` and no old OpenVMM lifecycle token, goes to NVX's init instead:
1120+
`/init` runs `exec /sbin/nvx-init TIME_DAEMON [ANNOUNCER]`, with the helpers'
1121+
process IDs. Old-token layered launches keep `/sbin/nvx-init-agent`.
11211122
`initramfs::write` copies that initramfs, and appends to the copy a second
11221123
archive, in the `newc` format, which Linux unpacks over the first:
11231124

@@ -1128,8 +1129,8 @@ archive, in the `newc` format, which Linux unpacks over the first:
11281129
- a symbolic link from `/sbin/nvx-managed-agent` to that script;
11291130
- a script, `/sbin/runtime-adapter-layered`, that runs
11301131
`runtime-adapter-guest init /dev/hvc2 layered`, which names the control tty
1131-
itself, since `/init` passes NVX's init agent none;
1132-
- a symbolic link from `/sbin/nvx-init-agent` to that script, so that NVX's
1132+
itself, and ignores the helper arguments;
1133+
- a symbolic link from `/sbin/nvx-init` to that script, so that NVX's
11331134
init agent never assembles the root in the adapter's VMs;
11341135
- for the capture of a template whose clones delay their repair, for tests, the
11351136
delay's file, `/etc/runtime-adapter-repair-delay`, in the `/etc` directory of

‎tests/runtime-adapter/src/initramfs.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
//! exec /sbin/nvx-managed-agent /dev/hvc2 - nvx-managed UID GID NAME HOME
88
//! ```
99
//!
10-
//! and a VM with a layered root, whose kernel command line has `nvx_sandbox=1`, to NVX's init
11-
//! agent, which assembles the root, without arguments:
10+
//! and a VM with a layered root, whose kernel command line has `nvx_sandbox=1` and no old
11+
//! OpenVMM lifecycle token, to NVX's init, with its helpers' process IDs:
1212
//!
1313
//! ```text
14-
//! exec /sbin/nvx-init-agent
14+
//! exec /sbin/nvx-init TIME_DAEMON [ANNOUNCER]
1515
//! ```
1616
//!
1717
//! [`write`](fn@write) copies that initramfs, and appends to the copy a second archive, in the
@@ -23,7 +23,7 @@
2323
//! - a symbolic link from [`MANAGED_AGENT`] to that script;
2424
//! - a script, at [`LAYERED_LAUNCHER`], that runs the guest agent as
2525
//! `runtime-adapter-guest init /dev/hvc2 layered`, PID 1 of a VM with a layered root, which
26-
//! names the control tty, [`CONTROL_TTY`], itself, since `/init` passes NVX's init agent none;
26+
//! names the control tty, [`CONTROL_TTY`], itself, and ignores the helper arguments;
2727
//! - and a symbolic link from [`INIT_AGENT`] to that script, so that NVX's init agent never
2828
//! assembles the root in the adapter's VMs;
2929
//! - for the capture of a template whose clones delay their repair, for tests, the delay's file,
@@ -53,7 +53,7 @@ pub const MANAGED_AGENT: &str = "/sbin/nvx-managed-agent";
5353
/// NVX's init agent.
5454
pub const LAYERED_LAUNCHER: &str = "/sbin/runtime-adapter-layered";
5555
/// Path of NVX's init agent, which `/init` runs in a VM with a layered root.
56-
pub const INIT_AGENT: &str = "/sbin/nvx-init-agent";
56+
pub const INIT_AGENT: &str = "/sbin/nvx-init";
5757
/// The VM's control tty, which `/init` requires of both lifecycles that NVX's agents serve.
5858
pub const CONTROL_TTY: &str = "/dev/hvc2";
5959

‎tests/runtime-adapter/src/worker.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//!
55
//! # PID 1 and the worker
66
//!
7-
//! In a VM with a layered root, NVX's `/init` runs `/sbin/nvx-init-agent`, which the adapter's
7+
//! In a VM with a tokenless layered root, NVX's `/init` runs `/sbin/nvx-init`, which the adapter's
88
//! initramfs replaces with `runtime-adapter-guest init /dev/hvc2 layered`; see the host side's
99
//! `initramfs` module. That PID 1, [`supervise`], stays minimal:
1010
//!

‎tests/runtime-adapter/tests/initramfs.rs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fn expected(agent: &[u8]) -> Vec<Entry> {
103103
0o100_755,
104104
initramfs::layered_launcher_script().as_bytes(),
105105
),
106-
entry(INIT_AGENT, 5, 0o120_777, LAYERED_LAUNCHER.as_bytes()),
106+
entry("/sbin/nvx-init", 5, 0o120_777, LAYERED_LAUNCHER.as_bytes()),
107107
]
108108
}
109109

@@ -151,15 +151,14 @@ fn the_launchers_run_the_guest_agent_on_the_control_tty() {
151151
);
152152
assert_eq!(MANAGED_AGENT, "/sbin/nvx-managed-agent");
153153

154-
// It passes NVX's init agent nothing, so the layered launcher names the control tty, which
155-
// `/init` requires of both lifecycles.
154+
// The layered launcher ignores the helper PIDs and names its control tty itself.
156155
let layered = runtime_adapter::GUEST_LAYERED_MODE;
157156
assert_eq!(
158157
initramfs::layered_launcher_script(),
159158
format!("#!/bin/sh\nexec {GUEST_AGENT} {init} /dev/hvc2 {layered}\n")
160159
);
161160
assert_eq!(CONTROL_TTY, "/dev/hvc2");
162-
assert_eq!(INIT_AGENT, "/sbin/nvx-init-agent");
161+
assert_eq!(INIT_AGENT, "/sbin/nvx-init");
163162
}
164163

165164
#[test]

0 commit comments

Comments
 (0)