Skip to content
This repository has been archived by the owner on Jan 12, 2025. It is now read-only.

Commit

Permalink
virtme-ng-init: support long commands in graphic mode
Browse files Browse the repository at this point in the history
Support long commands using the "--" syntax also when running virtme-ng
in graphic mode.

Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
  • Loading branch information
Andrea Righi committed Nov 7, 2023
1 parent 2b45725 commit af4cfe1
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ fn extract_user_script(virtme_script: &str) -> Option<String> {
None
}

fn do_run_user_script(cmd: &str) {
fn run_user_script() {
if !std::path::Path::new("/dev/virtio-ports/virtme.stdin").exists()
|| !std::path::Path::new("/dev/virtio-ports/virtme.stdout").exists()
|| !std::path::Path::new("/dev/virtio-ports/virtme.stderr").exists()
Expand Down Expand Up @@ -663,7 +663,7 @@ fn do_run_user_script(cmd: &str) {
clear_virtme_envs();
unsafe {
Command::new("/bin/sh")
.args(["-c", cmd])
.args(["/tmp/.virtme-script"])
.pre_exec(move || {
nix::libc::setsid();
libc::close(libc::STDIN_FILENO);
Expand All @@ -679,17 +679,27 @@ fn do_run_user_script(cmd: &str) {
.output()
.expect("Failed to execute script");
}
poweroff();
}
}

fn run_user_script() -> bool {
fn create_user_script(cmd: &str) {
let file_path = "/tmp/.virtme-script";
let mut file = File::create(file_path).expect("Failed to create virtme-script file");
file.write_all(cmd.as_bytes())
.expect("Failed to write data to virtme-script file");
}

fn setup_user_script() {
if let Ok(cmdline) = std::fs::read_to_string("/proc/cmdline") {
if let Some(cmd) = extract_user_script(&cmdline) {
do_run_user_script(&cmd);
return true;
create_user_script(&cmd);
if let Ok(_) = env::var("virtme_graphics") {
return;
}
run_user_script();
}
}
return false;
}

fn setup_root_home() {
Expand Down Expand Up @@ -761,7 +771,7 @@ fn init_xdg_runtime_dir() {
env::set_var("XDG_RUNTIME_DIR", dir);
}

fn run_user_gui(tty_fd: libc::c_int, app: &str) {
fn run_user_gui(tty_fd: libc::c_int) {
init_xdg_runtime_dir();

// Generate a bare minimum xinitrc
Expand All @@ -776,9 +786,11 @@ fn run_user_gui(tty_fd: libc::c_int, app: &str) {
}
}
}
if let Err(err) =
utils::create_file(xinitrc, 0o0644, &format!("{}\nexec {}", pre_exec_cmd, app))
{
if let Err(err) = utils::create_file(
xinitrc,
0o0644,
&format!("{}\n/bin/bash /tmp/.virtme-script", pre_exec_cmd),
) {
utils::log(&format!("failed to generate {}: {}", xinitrc, err));
return;
}
Expand Down Expand Up @@ -823,10 +835,11 @@ fn run_user_session() {
let tty_fd = open(consdev.as_str(), OFlag::from_bits_truncate(flags), mode)
.expect("failed to open console");

if let Ok(app) = env::var("virtme_graphics") {
run_user_gui(tty_fd, &app);
if let Ok(_) = env::var("virtme_graphics") {
run_user_gui(tty_fd);
} else {
run_user_shell(tty_fd);
}
run_user_shell(tty_fd);
}

fn setup_user_session() {
Expand Down Expand Up @@ -919,10 +932,9 @@ fn main() {

// Start user session (batch or interactive).
set_cwd();
if !run_user_script() {
setup_user_session();
run_user_session();
}
setup_user_script();
setup_user_session();
run_user_session();

// Shutdown the system.
poweroff();
Expand Down

0 comments on commit af4cfe1

Please sign in to comment.