summary refs log tree commit diff
path: root/host/start-vm/modprobe.rs
blob: 61868204b0784cf41514d26b5dfb000f3f116d04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2022 Alyssa Ross <hi@alyssa.is>

use std::ffi::OsStr;
use std::fmt::{self, Display, Formatter};
use std::io;
use std::os::unix::prelude::*;
use std::process::{Command, ExitStatus};

#[derive(Debug)]
pub enum ModprobeError {
    Spawn(io::Error),
    Fail(ExitStatus),
}

impl Display for ModprobeError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        use ModprobeError::*;
        match self {
            Spawn(e) => write!(f, "failed to spawn modprobe: {}", e),
            Fail(status) => {
                if let Some(code) = status.code() {
                    write!(f, "modprobe exited with status {}", code)
                } else {
                    write!(f, "modprobe killed by signal {}", status.signal().unwrap())
                }
            }
        }
    }
}

pub fn modprobe<I, S>(module_names: I) -> Result<(), ModprobeError>
where
    I: IntoIterator<Item = S>,
    S: AsRef<OsStr>,
{
    let status = Command::new("modprobe")
        .arg("-q")
        .arg("--")
        .args(module_names)
        .status()
        .map_err(ModprobeError::Spawn)?;

    if status.success() {
        Ok(())
    } else {
        Err(ModprobeError::Fail(status))
    }
}