Skip to content

Commit 80f516f

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 58cecdf commit 80f516f

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
@@ -429,13 +429,21 @@ fn main() -> Result<ExitCode> {
429429
.to_str()
430430
.context("Temporary directory path contains invalid UTF-8")?
431431
.to_owned();
432-
let muvm_guest_args = 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-
];
432+
let custom_init = options.custom_init_cmdline.is_some();
433+
let muvm_guest_args = if let Some(cmdline) = options.custom_init_cmdline {
434+
cmdline
435+
.split_whitespace()
436+
.map(|a| a.to_owned())
437+
.collect::<Vec<String>>()
438+
} else {
439+
vec![
440+
muvm_guest_path
441+
.to_str()
442+
.context("Failed to process `muvm-guest` path as it contains invalid UTF-8")?
443+
.to_owned(),
444+
muvm_config_path,
445+
]
446+
};
439447

440448
// And forward XAUTHORITY. This will be modified to fix the
441449
// display name in muvm-guest.
@@ -470,7 +478,11 @@ fn main() -> Result<ExitCode> {
470478

471479
let krun_config_env = CString::new(format!("KRUN_CONFIG={}", config_file.path().display()))
472480
.context("Failed to process config_file var as it contains NUL character")?;
473-
let env: Vec<*const c_char> = vec![krun_config_env.as_ptr(), std::ptr::null()];
481+
let mut env: Vec<*const c_char> = vec![krun_config_env.as_ptr()];
482+
if custom_init {
483+
env.push(c"KRUN_INIT_PID1=1".as_ptr());
484+
}
485+
env.push(std::ptr::null());
474486

475487
{
476488
// 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
@@ -47,6 +47,7 @@ pub struct Options {
4747
pub emulator: Option<Emulator>,
4848
pub init_commands: Vec<PathBuf>,
4949
pub user_init_commands: Vec<PathBuf>,
50+
pub custom_init_cmdline: Option<String>,
5051
pub command: PathBuf,
5152
pub command_args: Vec<String>,
5253
}
@@ -179,6 +180,13 @@ pub fn options() -> OptionParser<Options> {
179180
)
180181
.argument("COMMAND")
181182
.many();
183+
let custom_init_cmdline = long("custom-init-cmdline")
184+
.help(
185+
"Command and arguments to run as PID 1, replacing muvm's own init.
186+
(Warning: this will break many muvm features, unless your init reimplements them.)",
187+
)
188+
.argument("CMDLINE")
189+
.optional();
182190
let command = positional("COMMAND").help("the command you want to execute in the vm");
183191
let command_args = any::<String, _, _>("COMMAND_ARGS", |arg| {
184192
(!["--help", "-h"].contains(&&*arg)).then_some(arg)
@@ -202,6 +210,7 @@ pub fn options() -> OptionParser<Options> {
202210
emulator,
203211
init_commands,
204212
user_init_commands,
213+
custom_init_cmdline,
205214
// positionals
206215
command,
207216
command_args,

0 commit comments

Comments
 (0)