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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::{
fs::{self, File},
io::{self, Write},
thread, time,
};
pub fn make_filesystem(filesystem_name: &str, device_name: &str) -> io::Result<(String, String)> {
match run_make_filesystem(filesystem_name, device_name) {
Ok(v) => {
log::debug!("first time success 'make_filesystem'");
return Ok(v);
}
Err(e) => {
log::warn!(
"first time failure 'make_filesystem' {}; retrying after 5-second...",
e
);
thread::sleep(time::Duration::from_secs(5));
return run_make_filesystem(filesystem_name, device_name);
}
}
}
pub fn run_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() {
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
}
}
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() {
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";
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()))
}