Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions doc/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -1021,14 +1021,34 @@ control session, as [`sandbox exec`](#managed-lifecycle) does, with the same
its standard input, output and error as `sandbox exec` does. It then asks
nvx-init to stop the VM, terminates OpenVMM if it does not exit within
`--timeout` seconds, warning that it did, and removes the VM directory, and
finally exits with the workload's status. A VM that exits before nvx-init is
ready, as when the guest
finally exits with the workload's status. A workload that outlives
`--timeout` fails the run with `timed-out`, and the run then terminates
OpenVMM as `stop` does once its grace has passed, waiting up to `--timeout`
seconds, and at least 10, for OpenVMM to exit, a wait that ends at most 10
seconds after Ctrl-C. When the run cannot confirm that OpenVMM exited, it fails
with `still-running`, after the workload's own error, and after Ctrl-C it
reports that error before it exits with status 130. An OpenVMM that the
termination reached runs no code of its own any longer, so the error says that
it is stuck exiting, and the run removes the VM directory all the same. Windows
keeps a terminated process until its pending I/O completes or is canceled, and
an OpenVMM terminated while its virtio-fs device had I/O in flight has stayed
so until the host restarted
([#1910](https://github.com/microsoft/nvx/issues/1910)). A VM that exits before
nvx-init is ready, as when the guest
refuses the workload identity, fails the run as it fails a `start`, with the
category and status of the outcome report that OpenVMM published, such as
`guest-exit status 125`. `--outcome-report` forwards that report, of the VM's
stop, with the permissions that OpenVMM gave it. Ctrl-C ends the VM, and the
run then exits with status 130. `--dry-run` prints the command and launches
nothing.
nothing. On Windows, OpenVMM runs in a job object that only the run holds and
that ends OpenVMM once it closes, so the VM ends with the run, whatever ends
the run, as when a supervisor terminates `nvx.exe`. OpenVMM also stays in the
jobs of `nvx.exe`, as its other child processes do, unless a job lets them
leave it silently, so that such a job can end it too. A run that is terminated
cannot remove its VM directory, `nvx-sandbox-run-` and 32 hexadecimal digits in
the temporary directory: the next run of the user removes it, once no process
holds its `openvmm.log` any longer, as no run or OpenVMM that still runs leaves
it.

CI runs `/sbin/nvx-sandbox-smoke`, with `--arg init`, as below, as the workload
of `sandbox run` and `sandbox exec`, to verify the security profile of the
Expand Down Expand Up @@ -1784,7 +1804,10 @@ terminates its OpenVMM without a guest shutdown, waits up to `--timeout`
seconds for it to exit, and warns that it did. A `stop` that cannot determine
the OpenVMM process, whose terminated OpenVMM does not exit in time, or whose
wait Ctrl-C ends, keeps the sandbox's runtime record, and the next `stop`
retries. The store's records belong to the version of NVX that wrote them, so
retries. The error of one whose terminated OpenVMM does not exit in time,
`still-running`, says that OpenVMM is stuck exiting, as for
[`sandbox run`](#experimental-single-workload-sandbox). The store's records
belong to the version of NVX that wrote them, so
stop and deprovision each sandbox with the version that started it before you
switch versions, as [Upgrades](#upgrades) says.

Expand Down
20 changes: 13 additions & 7 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ each code, which callers of the `nvx` library see in its place.
| `outcome-report-exists` | 1 | A file exists at the `--outcome-report` path. |
| `scratch-claimed` | 1 | Another sandbox claims the scratch image. |
| `exited` | 1 | OpenVMM exited, as when a start fails, whose message names the category and status of OpenVMM's outcome report when it has one. |
| `still-running` | 1 | A stop could not confirm the VM process's exit, so the caller keeps its verified process. |
| `still-running` | 1 | A stop could not confirm the VM process's exit, so the caller keeps its verified process. The message says that a process that its forced termination reached is stuck exiting. |
| `exec-refused` | 1 | The guest agent refused the workload, as for a working directory that it cannot use. |
| `endpoint-closed` | 1 | OpenVMM closed the control endpoint, as it does when the VM shuts down. |
| `protocol` | 1 | The control session failed otherwise, as when the guest breaks the protocol or the session is reset. |
Expand Down Expand Up @@ -208,10 +208,14 @@ combined with `--network-egress-allow` rules for direct connections.
`nvx run` checks its options, prints the OpenVMM command of a cold boot
after `>> `, and unless `--dry-run` is given, runs OpenVMM in a private
temporary VM directory, which it removes once OpenVMM has exited, on the
terminal. A cold boot attaches the guest's console, the boot console `hvc1`,
to the standard streams, so that the terminal's input reaches the guest through
it rather than through the byte channel, `hvc0`, whose output, such as the
kernel's early messages, goes to standard error with OpenVMM's log. It exits
terminal. On Windows, a cold boot's OpenVMM ends with `nvx run`, whatever ends
it, in a job object that only the run holds, as the OpenVMM of a
[`sandbox run`](#sandbox) does, and the next cold boot removes the VM
directory that one that was terminated left. A cold boot attaches the guest's
console, the boot console `hvc1`, to the standard streams, so that the
terminal's input reaches the guest through it rather than through the byte
channel, `hvc0`, whose output, such as the kernel's early messages, goes to
standard error with OpenVMM's log. It exits
with status 1 for an error, 2 for a usage error, and 130 after Ctrl-C, and
otherwise with the status that OpenVMM returns. Its errors and usage errors
carry their [error codes](#error-codes), as `error[CODE]: MESSAGE` and
Expand Down Expand Up @@ -393,8 +397,10 @@ artifacts before they launch, as `nvx run` does. A `run` keeps no state:
it launches the VM that a `provision` and a `start` of its options would
launch, in a temporary VM directory that it removes, executes its workload
once, as `exec` does, with its streams, has the guest shut the VM down, which
it terminates if it does not exit within `--timeout` seconds, and exits with
the workload's status; see
it terminates if it does not exit within `--timeout` seconds, as it terminates
the VM of a workload that outlives `--timeout`, and exits with the workload's
status. On Windows, its VM ends with it, whatever ends it, and the next `run`
removes the VM directory of one that was terminated; see
[Experimental single-workload sandbox](run.md#experimental-single-workload-sandbox).

See [Run](run.md) for artifact preparation, the security boundary, the
Expand Down
4 changes: 3 additions & 1 deletion nvx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@ serde_json = "1.0.120"
tempfile = "3.10"

# The job objects, process security descriptors, handles, and process lists of the process
# supervision and CLI tests, and the command-line splitting and file deletions of the CLI tests.
# supervision and CLI tests, the debuggers that hold the exits of their processes, and the
# command-line splitting and file deletions of the CLI tests.
[target.'cfg(windows)'.dev-dependencies]
windows-sys = { version = "0.61", features = [
"Win32_Foundation",
"Win32_Security",
"Win32_Security_Authorization",
"Win32_Storage_FileSystem",
"Win32_System_Diagnostics_Debug",
"Win32_System_Diagnostics_ToolHelp",
"Win32_System_JobObjects",
"Win32_System_Threading",
Expand Down
27 changes: 21 additions & 6 deletions nvx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,9 @@ OpenVMM's verified process once the grace deadline passes, and returns a
`Stopped` only once the process exited, with the read of its outcome report
kept apart, so that a report that can't be read never hides the exit. An exit
that it cannot confirm fails with `ErrorKind::StillRunning`, and the caller
keeps the process.
keeps the process. The error says that a process that the termination reached,
which runs no code of its own any longer, is stuck exiting, as Windows keeps a
terminated process until its pending I/O completes or is canceled.

`SavedVm` bundles what a caller saves to find its VM again: the `VmDir`, the
control endpoint, the capability's bytes, the process's `ProcessRef`, and the
Expand Down Expand Up @@ -1324,7 +1326,9 @@ exit with status 2, as `argparse` does, and help exits with status 0. Callers
tell errors apart by their codes and by stable fragments of their messages,
such as the names of options, fields, and files, since the wording may change.
Ctrl-C prints `Interrupted` and exits with status 130, without a code, since it
reports no error. `run`, `sandbox run`, and `sandbox exec` exit with the status
reports no error; a `sandbox run` reports first the error of a workload that
failed before Ctrl-C, and the `still-running` error of an OpenVMM whose exit it
cannot confirm. `run`, `sandbox run`, and `sandbox exec` exit with the status
of OpenVMM or of the workload, which is no error of `nvx` either. Text ends
its lines as Python's `print` ends them, with `\r\n` on Windows and `\n`
elsewhere, while workload output passes through unchanged; an error that
Expand Down Expand Up @@ -1523,7 +1527,10 @@ boot prints `>> COMMAND`, OpenVMM's command line, quoted as `sandbox run`
quotes it, and unless `--dry-run` is given, it runs OpenVMM with the standard
streams, so that the guest's console and the terminal settings that OpenVMM
restores behave as they do for OpenVMM itself, and exits with OpenVMM's
status; a restore does none of these, as the second item says.
status; a restore does none of these, as the second item says. On Windows, a
cold boot's OpenVMM runs in a job object that only `nvx` holds, as that of a
`sandbox run` does, which ends OpenVMM with `nvx`, whatever ends it, and the
next cold boot removes the VM directory that one that was terminated left.

- A cold boot runs the kernel and the initramfs of `--guest`, `alpine` by
default, with `--memory-mib` or else the guest's default, 128 MiB for Alpine
Expand Down Expand Up @@ -1604,7 +1611,15 @@ before them.
shut the VM down, waits up to `--timeout` seconds for OpenVMM to exit before
it terminates it, removes the VM directory, and exits with the workload's
status. `--outcome-report` forwards OpenVMM's outcome report of the VM's
stop.
stop. A workload that outlives `--timeout` fails with `timed-out`, after
which the run terminates OpenVMM as `stop` does, waiting up to `--timeout`
seconds, and at least 10, for it to exit, and fails with `still-running`,
which says that OpenVMM is stuck exiting, if it has not. On Windows, OpenVMM
runs in a job object that only the run holds, which ends OpenVMM once it
closes, so that the VM never outlives the run, whatever ends the run, and in
the jobs of `nvx` that keep their processes' children; the next run removes
the VM directory that a run that was terminated left, once no process holds
its log, which a run holds until it returns and its OpenVMM until it exits.
- `provision` records the sandbox in its state directory. `start` launches
OpenVMM detached with the managed lifecycle, passes it a fresh capability
through its standard input, and waits until the guest answers. `exec` runs a
Expand Down Expand Up @@ -1970,7 +1985,7 @@ wording of a message may change; callers of the CLI also have its codes.
| `Unavailable` | A missing or unusable artifact, hypervisor, or release, or an unsupported host |
| `Unsupported` | A request that NVX refuses: a value that it does not accept, such as a malformed or out-of-range one, options that it cannot combine, or a request that it cannot represent or enforce, such as a policy beyond a limit of OpenVMM or the guest agent |
| `Exited` | The VM's OpenVMM process exited |
| `StillRunning` | The exit of a VM's process could not be confirmed, so the VM may still run: a stop's, whose caller keeps its verified process, or that of the process that a failed spawn started and tried to end |
| `StillRunning` | The exit of a VM's process could not be confirmed, so the VM may still run: a stop's, whose caller keeps its verified process, or that of the process that a failed spawn started and tried to end; a process that a stop's termination reached is stuck exiting |
| `Io` | A host I/O failure, or state that NVX recorded on the host and cannot read |
| `NotFound` | State that an operation names and that does not exist on the host, such as the store of a sandbox that was never created or that a removal deleted |
| `Protocol` | A control session that failed, or a request that the guest agent refused |
Expand Down Expand Up @@ -2000,7 +2015,7 @@ has.
| `unavailable` | `Unavailable` | A dependency is missing or unusable, other than as a finer code says: install metadata, the hypervisor, a release, a distro layer or scratch image that the request names, or a host that NVX does not support |
| `unsupported` | `Unsupported` | NVX refuses the request: options that it cannot combine, a value that it does not accept, or a policy that it cannot represent or enforce. The message names the option or field |
| `exited` | `Exited` | OpenVMM exited, as when a start fails, whose message names the category and status of OpenVMM's outcome report when it has one |
| `still-running` | `StillRunning` | The exit of a VM's process could not be confirmed, as by a stop or after a start whose launch failed, so the VM may still run |
| `still-running` | `StillRunning` | The exit of a VM's process could not be confirmed, as by a stop or after a start whose launch failed, so the VM may still run; the message says that a process that the stop's termination reached is stuck exiting |
| `io` | `Io` | A host I/O operation failed, or state that NVX recorded on the host is unreadable or malformed |
| `not-found` | `NotFound` | Other state that the operation names does not exist on the host |
| `protocol` | `Protocol` | The control session failed otherwise: the peer violated the protocol, reset the session, or failed authentication |
Expand Down
28 changes: 24 additions & 4 deletions nvx/src/host/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ const MACHINES: [&str; 1] = ["microvm"];
/// The choices of `--processors`, the processor counts that a microVM takes.
const PROCESSORS: [&str; 4] = ["1", "2", "4", "8"];

/// The name of a cold run's temporary VM directory, before 32 random lowercase hexadecimal digits.
const RUN_DIRECTORY: &str = "nvx-run-";

/// The options of `snapshot restore` that a restore of `run` takes, which `sandbox` defines and
/// parses for both: the sandbox's state directory, the image, its upper, the workload, and the
/// timeout of the restore.
Expand Down Expand Up @@ -677,7 +680,9 @@ fn cold_vm_config(

/// Removes the temporary VM directory of a cold run, with what its VM left in it: OpenVMM's log
/// and outcome report, its control and boot-console sockets on Linux, and the staging file of a
/// report that OpenVMM did not finish writing, which [`StateFile::owns_temporary`] names.
/// report that OpenVMM did not finish writing, which [`StateFile::owns_temporary`] names. A
/// directory that is gone already, as another run on Windows removes one whose log nothing holds
/// any longer, is removed.
fn remove_vm_directory(dir: &VmDir) -> Result {
let remove = |path: &Path| match fs::remove_file(path) {
Ok(()) => Ok(()),
Expand All @@ -692,7 +697,12 @@ fn remove_vm_directory(dir: &VmDir) -> Result {
] {
remove(&dir.path().join(name))?;
}
for entry in fs::read_dir(dir.path()).map_err(Exit::from_os_error)? {
let entries = match fs::read_dir(dir.path()) {
Ok(entries) => entries,
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()),
Err(error) => return Err(Exit::from_os_error(error)),
};
for entry in entries {
let entry = entry.map_err(Exit::from_os_error)?;
if entry
.file_name()
Expand All @@ -702,7 +712,10 @@ fn remove_vm_directory(dir: &VmDir) -> Result {
remove(&entry.path())?;
}
}
fs::remove_dir(dir.path()).map_err(Exit::from_os_error)
match fs::remove_dir(dir.path()) {
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
removed => removed.map_err(Exit::from_os_error),
}
}

fn connect_running(
Expand Down Expand Up @@ -1162,13 +1175,20 @@ fn run_cold(
windows_breakaway: false,
options: LaunchOptions::default(),
};
let path = std::env::temp_dir().join(format!("nvx-run-{name}"));
let path = std::env::temp_dir().join(format!("{RUN_DIRECTORY}{name}"));
if path.try_exists().map_err(Exit::from_os_error)? {
return Err(Exit::error(
ErrorCode::Io,
"the temporary VM directory already exists",
));
}
// A run that ended without removing its VM directory, as one that was terminated does, leaves
// it to the next run, which tells it by its log, which no process holds any longer: this run
// reads its own until it returns.
#[cfg(windows)]
if !options.dry_run {
sandbox::remove_abandoned_vm_directories(&std::env::temp_dir(), RUN_DIRECTORY);
}
vm::create_private_dir(&path)?;
let vm_dir = VmDir::open(&path)?;
let mut cleanup = true;
Expand Down
2 changes: 2 additions & 0 deletions nvx/src/host/cli/sandbox/lifecycle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub(crate) use self::directory::{create_state_directory, prepare_state_directory
pub(crate) use self::outcome::{
copy_openvmm_outcome, validate_outcome_destination, write_exec_outcome,
};
#[cfg(windows)]
pub(crate) use self::start::remove_abandoned_vm_directories;
pub(crate) use self::start::{exec_workload, run, start};
pub(crate) use self::stop::{deprovision, stop};

Expand Down
Loading
Loading