Skip to content

Commit 788e6d9

Browse files
committed
Add a flag to run a custom init instead of muvm-guest
Allow the user to replace muvm-guest with a custom init process. With updated libkrun, it will even run as PID 1, making it possible to run systemd. Of course, this is not the suggested way to use muvm, but some use cases necesitate the use of systemd. The next few commits will facilitate running individual parts of muvm-guest as separate processes under a custom service manager. Signed-off-by: Val Packett <[email protected]>
1 parent 5b35e56 commit 788e6d9

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

crates/muvm/src/bin/muvm.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,21 @@ fn main() -> Result<ExitCode> {
422422
.to_str()
423423
.context("Temporary directory path contains invalid UTF-8")?
424424
.to_owned();
425-
let muvm_guest_args = vec![
426-
muvm_guest_path
427-
.to_str()
428-
.context("Failed to process `muvm-guest` path as it contains invalid UTF-8")?
429-
.to_owned(),
430-
muvm_config_path,
431-
];
425+
let custom_init = options.custom_init_cmdline.is_some();
426+
let muvm_guest_args = if let Some(cmdline) = options.custom_init_cmdline {
427+
cmdline
428+
.split_whitespace()
429+
.map(|a| a.to_owned())
430+
.collect::<Vec<String>>()
431+
} else {
432+
vec![
433+
muvm_guest_path
434+
.to_str()
435+
.context("Failed to process `muvm-guest` path as it contains invalid UTF-8")?
436+
.to_owned(),
437+
muvm_config_path,
438+
]
439+
};
432440

433441
// And forward XAUTHORITY. This will be modified to fix the
434442
// display name in muvm-guest.
@@ -459,7 +467,11 @@ fn main() -> Result<ExitCode> {
459467

460468
let krun_config_env = CString::new(format!("KRUN_CONFIG={}", config_file.path().display()))
461469
.context("Failed to process config_file var as it contains NUL character")?;
462-
let env: Vec<*const c_char> = vec![krun_config_env.as_ptr(), std::ptr::null()];
470+
let mut env: Vec<*const c_char> = vec![krun_config_env.as_ptr()];
471+
if custom_init {
472+
env.push(c"KRUN_INIT_PID1=1".as_ptr());
473+
}
474+
env.push(std::ptr::null());
463475

464476
{
465477
// Sets environment variables to be configured in the context of the executable.

crates/muvm/src/cli_options.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub struct Options {
2323
pub emulator: Option<Emulator>,
2424
pub init_commands: Vec<PathBuf>,
2525
pub user_init_commands: Vec<PathBuf>,
26+
pub custom_init_cmdline: Option<String>,
2627
pub command: PathBuf,
2728
pub command_args: Vec<String>,
2829
}
@@ -151,6 +152,13 @@ pub fn options() -> OptionParser<Options> {
151152
)
152153
.argument("COMMAND")
153154
.many();
155+
let custom_init_cmdline = long("custom-init-cmdline")
156+
.help(
157+
"Command and arguments to run as PID 1, replacing muvm's own init.
158+
(Warning: this will break many muvm features, unless your init reimplements them.)",
159+
)
160+
.argument("CMDLINE")
161+
.optional();
154162
let command = positional("COMMAND").help("the command you want to execute in the vm");
155163
let command_args = any::<String, _, _>("COMMAND_ARGS", |arg| {
156164
(!["--help", "-h"].contains(&&*arg)).then_some(arg)
@@ -173,6 +181,7 @@ pub fn options() -> OptionParser<Options> {
173181
emulator,
174182
init_commands,
175183
user_init_commands,
184+
custom_init_cmdline,
176185
// positionals
177186
command,
178187
command_args,

0 commit comments

Comments
 (0)