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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::{
    fs::{self, File},
    io::{self, Write},
};

/// Makes a new file system on a specified device.
///
/// e.g.,
/// sudo mkfs -t ext4 /dev/nvme1n1
///
/// ref. See https://github.com/cholcombe973/block-utils/blob/master/src/lib.rs for other commands.
/// ref. https://stackoverflow.com/questions/45167717/mounting-a-nvme-disk-on-aws-ec2
pub fn make_filesystem(filesystem_name: &str, device_name: &str) -> io::Result<(String, String)> {
    let device_path = if device_name.starts_with("/dev/") {
        device_name.to_string()
    } else {
        format!("/dev/{}", device_name)
    };

    log::info!(
        "making file system with 'mkfs' command on the device path '{}'",
        device_path
    );

    let cmd = format!("sudo mkfs -t {} {}", filesystem_name, device_path);
    let res = command_manager::run(&cmd);
    if res.is_err() {
        // e.g., mke2fs 1.45.5 (07-Jan-2020) /dev/nvme1n1 is mounted; will not make a filesystem here!
        let e = res.err().unwrap();
        if !e
            .to_string()
            .contains(format!("{} is mounted", device_path).as_str())
        {
            return Err(e);
        }

        log::warn!("ignoring the 'is mounted' error '{}'", e.to_string());
        Ok((String::new(), e.to_string()))
    } else {
        res
    }
}

/// Mounts the file system to the specified directory.
/// And updates "/etc/fstab" to auto remount in case of instance reboot.
///
/// e.g.,
/// sudo mount /dev/nvme1n1 /data -t ext4
///
/// ref. See https://github.com/cholcombe973/block-utils/blob/master/src/lib.rs for other commands.
/// ref. https://stackoverflow.com/questions/45167717/mounting-a-nvme-disk-on-aws-ec2
pub fn mount_filesystem(
    filesystem_name: &str,
    device_name: &str,
    dir_name: &str,
) -> io::Result<(String, String)> {
    let device_path = if device_name.starts_with("/dev/") {
        device_name.to_string()
    } else {
        format!("/dev/{}", device_name)
    };

    log::info!(
        "mounting the file system with 'mount' command on the device path '{}'",
        device_path
    );

    let cmd = format!(
        "sudo mount {} {} -t {}",
        device_path, dir_name, filesystem_name
    );
    let res = command_manager::run(&cmd);
    if res.is_err() {
        // e.g., mount: /data: /dev/nvme1n1 already mounted on /data
        let e = res.err().unwrap();
        if !e
            .to_string()
            .contains(format!("{} already mounted", device_path).as_str())
        {
            return Err(e);
        }

        log::warn!("ignoring the 'already mounted' error '{}'", e.to_string());
        Ok((String::new(), e.to_string()))
    } else {
        res
    }
}

const FSTAB_PATH: &str = "/etc/fstab";

/// Updates "/etc/fstab" to auto remount in case of instance reboot.
/// The output is the contents of the "fstab" file.
///
/// e.g.,
/// sudo echo '/dev/nvme1n1       /data   ext4    defaults,nofail 0       2' >> /etc/fstab
/// sudo mount --all
///
/// ref. See https://github.com/cholcombe973/block-utils/blob/master/src/lib.rs for other commands.
/// ref. https://stackoverflow.com/questions/45167717/mounting-a-nvme-disk-on-aws-ec2
pub fn update_fstab(
    filesystem_name: &str,
    device_name: &str,
    dir_name: &str,
) -> io::Result<(String, String)> {
    let device_path = if device_name.starts_with("/dev/") {
        device_name.to_string()
    } else {
        format!("/dev/{}", device_name)
    };

    log::info!(
        "updating the fstab file on the device path '{}'",
        device_path
    );

    let line = format!(
        "{}       {}   {}    defaults,nofail 0       2",
        device_path, dir_name, filesystem_name
    );
    let mut contents = fs::read_to_string(FSTAB_PATH)?;
    if contents.contains(&line) {
        log::warn!("fstab already contains '{}', skipping updating fstab", line);
        return Ok((contents, String::new()));
    }
    contents.push('\n');
    contents.push_str(&line);

    let tmp_path = random_manager::tmp_path(10, None)?;
    let mut f = File::create(&tmp_path)?;
    f.write_all(contents.as_bytes())?;

    let cmd = format!("sudo cp {} {}", tmp_path, FSTAB_PATH);
    command_manager::run(&cmd)?;
    command_manager::run("sudo mount --all")?;

    Ok((contents, String::new()))
}