Skip to content

Commit

Permalink
virtme-ng: honor virtme_user when running user script
Browse files Browse the repository at this point in the history
When virtme_user is specified in the boot options we should also honor
it when running scripts, instead of just using it in interactive mode.

Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
  • Loading branch information
Andrea Righi committed Nov 12, 2023
1 parent 2f0d249 commit f20306d
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,26 @@ fn run_user_script() {
)
.expect("failed to open console.");

// Determine if we need to switch to a different user, or if we can run the script as root.
let cmd: String;
let args: Vec<&str>;
let user: String;
if let Ok(virtme_user) = env::var("virtme_user") {
user = virtme_user;
} else {
user = String::new();
}
if !user.is_empty() {
cmd = "su".to_string();
args = vec![&user, "-c", USER_SCRIPT];
} else {
cmd = "/bin/sh".to_string();
args = vec![USER_SCRIPT];
}
clear_virtme_envs();
unsafe {
Command::new("/bin/sh")
.args([USER_SCRIPT])
Command::new(&cmd)
.args(&args)
.pre_exec(move || {
nix::libc::setsid();
libc::close(libc::STDIN_FILENO);
Expand All @@ -686,9 +702,7 @@ fn run_user_script() {
}

fn create_user_script(cmd: &str) {
let mut file = File::create(USER_SCRIPT).expect("Failed to create virtme-script file");
file.write_all(cmd.as_bytes())
.expect("Failed to write data to virtme-script file");
utils::create_file(USER_SCRIPT, 0o0755, cmd).expect("Failed to create virtme-script file");
}

fn setup_user_script() {
Expand Down

0 comments on commit f20306d

Please sign in to comment.