Skip to content

Commit dc5ad75

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 dc5ad75

5 files changed

Lines changed: 25 additions & 2 deletions

File tree

README.md

Lines changed: 4 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,9 @@ 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.
61+
Can be specified multiple times.
5962
-h, --help Prints help information
6063
```
6164

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: 11 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,15 @@ 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.
141+
Can be specified multiple times.",
142+
)
143+
.argument("COMMAND")
144+
.many();
135145
let command = positional("COMMAND").help("the command you want to execute in the vm");
136146
let command_args = any::<String, _, _>("COMMAND_ARGS", |arg| {
137147
(!["--help", "-h"].contains(&&*arg)).then_some(arg)
@@ -152,6 +162,7 @@ pub fn options() -> OptionParser<Options> {
152162
privileged,
153163
publish_ports,
154164
emulator,
165+
init_commands,
155166
// positionals
156167
command,
157168
command_args,

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::panic::catch_unwind;
55
use std::process::Command;
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,13 @@ fn main() -> Result<()> {
9494
}
9595
}
9696

97+
for init_command in options.init_commands {
98+
let code = Command::new(&init_command).spawn()?.wait()?;
99+
if !code.success() {
100+
return Err(anyhow!("Executing `{}` failed", init_command.display()));
101+
}
102+
}
103+
97104
configure_network()?;
98105

99106
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)