Skip to content

Commit

Permalink
virtme-ng-init: channel the return code of a command to the host
Browse files Browse the repository at this point in the history
Send the return code of the command executed inside the guest to the
host.

This allows to run commands inside a guest and give users the impression
that they are running on the host.

This feature is particularly useful for automation with virtme-ng, since
it easily allows to check the return code of a script executed inside
virtme-ng as if it was running directly on the host.

Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
  • Loading branch information
Andrea Righi committed Jan 31, 2024
1 parent f3a737c commit b8cba09
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,11 +629,15 @@ fn run_user_script(uid: u32) {
} else {
// Re-create stdout/stderr to connect to the virtio-serial ports.
let io_files = [
("/dev/virtio-ports/virtme.ret", "/dev/virtme.ret"),
("/dev/virtio-ports/virtme.dev_stdin", "/dev/stdin"),
("/dev/virtio-ports/virtme.dev_stdout", "/dev/stdout"),
("/dev/virtio-ports/virtme.dev_stderr", "/dev/stderr"),
];
for (src, dst) in io_files.iter() {
if !std::path::Path::new(src).exists() {
continue;
}
if std::path::Path::new(dst).exists() {
utils::do_unlink(dst);
}
Expand All @@ -657,7 +661,7 @@ fn run_user_script(uid: u32) {
};
clear_virtme_envs();
unsafe {
Command::new(cmd)
let ret = Command::new(cmd)
.args(&args)
.pre_exec(move || {
nix::libc::setsid();
Expand All @@ -673,6 +677,18 @@ fn run_user_script(uid: u32) {
})
.output()
.expect("Failed to execute script");

// Channel the return code to the host via /dev/virtme.ret
if let Ok(mut file) = OpenOptions::new().write(true).open("/dev/virtme.ret") {
// Write the value of output.status.code() to the file
if let Some(code) = ret.status.code() {
file.write_all(code.to_string().as_bytes())
.expect("Failed to write to file");
} else {
// Handle the case where output.status.code() is None
file.write_all(b"-1").expect("Failed to write to file");
}
}
}
poweroff();
}
Expand Down

0 comments on commit b8cba09

Please sign in to comment.