Skip to content

Commit ca78132

Browse files
nrabulinskislp
authored andcommitted
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 b4c40f7 commit ca78132

5 files changed

Lines changed: 28 additions & 2 deletions

File tree

README.md

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

1616
Available positional items:
1717
COMMAND the command you want to execute in the vm
@@ -54,6 +54,9 @@ Available options:
5454
Valid options are "box" and "fex". If this argument is not
5555
present, muvm will try to use FEX, falling back to Box if it
5656
can't be found.
57+
-x, --execute-pre=COMMAND Command to run inside the VM before guest server starts.
58+
Can be used for e.g. setting up additional mounts.
59+
Can be specified multiple times.
5760
-h, --help Prints help information
5861
```
5962

crates/muvm/src/bin/muvm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ fn main() -> Result<ExitCode> {
397397
merged_rootfs: options.merged_rootfs,
398398
emulator: options.emulator,
399399
cwd,
400+
init_commands: options.init_commands,
400401
};
401402
let mut muvm_config_file = NamedTempFile::new()
402403
.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: 11 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,16 @@ fn main() -> Result<()> {
9494
}
9595
}
9696

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

99109
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
@@ -44,6 +44,7 @@ pub struct GuestConfiguration {
4444
pub merged_rootfs: bool,
4545
pub emulator: Option<Emulator>,
4646
pub cwd: PathBuf,
47+
pub init_commands: Vec<PathBuf>,
4748
}
4849

4950
pub const PULSE_SOCKET: u32 = 3333;

0 commit comments

Comments
 (0)