Skip to content

Commit 96d10b5

Browse files
committed
Add execute-pre option
Allows to execute a command before the guest server starts. Useful on distros like NixOS where we need additional mounts. Signed-off-by: Nikodem Rabuliński <[email protected]>
1 parent 1e9cec2 commit 96d10b5

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
``` sh
1414
Usage: muvm [-c=CPU_LIST]... [-e=ENV]... [--mem=MEM] [--vram=VRAM] [--passt-socket=PATH] [-f=
1515
FEX_IMAGE]... [-m] [-i] [-t] [--privileged] [-p=<[[IP:][HOST_PORT]:]GUEST_PORT[/PROTOCOL]>]... [
16-
--emu=EMU] COMMAND [COMMAND_ARGS]...
16+
--emu=EMU] [-x=COMMAND]... COMMAND [COMMAND_ARGS]...
1717

1818
Available positional items:
1919
COMMAND the command you want to execute in the vm
@@ -56,6 +56,8 @@ Available options:
5656
Valid options are "box" and "fex". If this argument is not
5757
present, muvm will try to use FEX, falling back to Box if it
5858
can't be found.
59+
-x, --execute-pre=COMMAND Command to run inside the VM before guest server starts.
60+
Can be used for e.g. setting up additional mounts. Can be specified many times
5961
-h, --help Prints help information
6062
```
6163

crates/muvm/src/bin/muvm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ fn main() -> Result<ExitCode> {
389389
host_display: display,
390390
merged_rootfs: options.merged_rootfs,
391391
emulator: options.emulator,
392+
init_commands: options.init_commands,
392393
};
393394
let mut muvm_config_file = NamedTempFile::new()
394395
.context("Failed to create a temporary file to store the muvm guest config")?;

crates/muvm/src/cli_options.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub struct Options {
2121
pub privileged: bool,
2222
pub publish_ports: Vec<String>,
2323
pub emulator: Option<Emulator>,
24+
pub init_commands: Vec<PathBuf>,
2425
pub command: PathBuf,
2526
pub command_args: Vec<String>,
2627
}
@@ -132,6 +133,14 @@ pub fn options() -> OptionParser<Options> {
132133
)
133134
.argument::<String>("[[IP:][HOST_PORT]:]GUEST_PORT[/PROTOCOL]")
134135
.many();
136+
let init_commands = long("execute-pre")
137+
.short('x')
138+
.help(
139+
"Command to run inside the VM before guest server starts.
140+
Can be used for e.g. setting up additional mounts. Can be specified many times",
141+
)
142+
.argument("COMMAND")
143+
.many();
135144
let command = positional("COMMAND").help("the command you want to execute in the vm");
136145
let command_args = any::<String, _, _>("COMMAND_ARGS", |arg| {
137146
(!["--help", "-h"].contains(&&*arg)).then_some(arg)
@@ -152,6 +161,7 @@ pub fn options() -> OptionParser<Options> {
152161
privileged,
153162
publish_ports,
154163
emulator,
164+
init_commands,
155165
// positionals
156166
command,
157167
command_args,

crates/muvm/src/guest/bin/muvm-guest.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use std::fs::File;
22
use std::io::Read;
33
use std::os::fd::AsFd;
44
use std::panic::catch_unwind;
5-
use std::process::Command;
5+
use std::process::{Command, Stdio};
66
use std::{cmp, env, fs, thread};
77

8-
use anyhow::{Context, Result};
8+
use anyhow::{anyhow, Context, Result};
99
use muvm::guest::box64::setup_box;
1010
use muvm::guest::bridge::pipewire::start_pwbridge;
1111
use muvm::guest::bridge::x11::start_x11bridge;
@@ -94,6 +94,18 @@ fn main() -> Result<()> {
9494
}
9595
}
9696

97+
for init_command in options.init_commands {
98+
let code = Command::new(&init_command)
99+
.stdin(Stdio::null())
100+
.stdout(Stdio::piped())
101+
.stderr(Stdio::piped())
102+
.spawn()?
103+
.wait()?;
104+
if !code.success() {
105+
return Err(anyhow!("Executing `{}` failed", init_command.display()));
106+
}
107+
}
108+
97109
configure_network()?;
98110

99111
let run_path = match setup_user(Uid::from(options.uid), Gid::from(options.gid)) {

crates/muvm/src/utils/launch.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub struct GuestConfiguration {
4343
pub host_display: Option<String>,
4444
pub merged_rootfs: bool,
4545
pub emulator: Option<Emulator>,
46+
pub init_commands: Vec<PathBuf>,
4647
}
4748

4849
pub const PULSE_SOCKET: u32 = 3333;

0 commit comments

Comments
 (0)