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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::path::{Path, PathBuf};
use std::process::Command;

use tracing::{error, info};

use crate::buildah_error::BuildahError;
use crate::parsing::parse_output_line;

/// Instance of a Buildah mount.
/// Unmounted on drop.
#[derive(Debug)]
pub struct Mount {
    container_name: String,
    host_path: PathBuf,
}

impl Mount {
    /// Mount a working container's root filesystem.
    #[tracing::instrument]
    pub fn new(container_name: &str) -> Result<Self, BuildahError> {
        let output = Command::new("buildah")
            .arg("mount")
            .arg(container_name)
            .output()?;

        let path = parse_output!(output);
        info!("Mounted container {}", container_name);
        Ok(Mount { container_name: container_name.to_string(), host_path: PathBuf::from(path) })
    }

    /// ...
    pub fn host_path(&self) -> &Path {
        self.host_path.as_path()
    }
}

/// Unmount the root file system on the working container.
impl Drop for Mount {
    #[tracing::instrument]
    fn drop(&mut self) {
        let output = Command::new("buildah")
            .arg("umount")
            .arg(&self.container_name)
            .output();

        match output {
            Ok(output) => {
                if !output.status.success() {
                    // Ignore error
                    let error = BuildahError::Buildah(parse_output_line(output.stderr));
                    error!("Couldn't umount container {}: {:?}", self.container_name, error);
                    return;
                }

                let _id = parse_output_line(output.stdout);
                info!("Umounted container {}", self.container_name);
            }
            Err(error) => {
                // Ignore error
                error!("Couldn't umount container {}: {:?}", self.container_name, error);
            }
        }
    }
}