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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use std::{
collections::{HashMap, HashSet},
net::{IpAddr, ToSocketAddrs},
};
use crate::ssh::{SshCommand, SshShell};
pub fn escape_for_bash(s: &str) -> String {
let mut new = String::with_capacity(s.len());
new.push('\'');
for c in s.chars() {
if c == '\'' {
new.push('\''); new.push('"');
new.push('\''); new.push('"');
new.push('\''); } else {
new.push(c);
}
}
new.push('\'');
new
}
pub fn get_host_ip<A: ToSocketAddrs>(addr: A) -> (IpAddr, u16) {
let addr = addr.to_socket_addrs().unwrap().next().unwrap();
let ip = addr.ip();
let port = addr.port();
(ip, port)
}
pub fn set_cpu_scaling_governor(gov: &str) -> SshCommand {
cmd!("sudo cpupower frequency-set -g {}", gov)
}
pub fn swapoff(device: &str) -> SshCommand {
cmd!("sudo swapoff {}", device)
}
pub fn swapon(device: &str) -> SshCommand {
cmd!("sudo swapon {}", device)
}
pub fn add_to_group(group: &str) -> SshCommand {
cmd!("sudo usermod -aG {} `whoami`", group).use_bash()
}
pub fn write_gpt(device: &str) -> SshCommand {
cmd!("sudo parted -a optimal {} -s -- mklabel gpt", device)
}
pub fn create_partition(device: &str) -> SshCommand {
cmd!(
"sudo parted -a optimal {} -s -- mkpart primary 0% 100%",
device
)
}
pub fn reboot(shell: &mut SshShell, dry_run: bool) -> Result<(), failure::Error> {
let _ = shell.run(cmd!("sudo reboot").dry_run(dry_run));
if !dry_run {
std::thread::sleep(std::time::Duration::from_secs(10));
shell.reconnect()?;
}
shell.run(cmd!("whoami").dry_run(dry_run))?;
Ok(())
}
pub fn format_partition_as_ext4<P: AsRef<std::path::Path>>(
shell: &SshShell,
dry_run: bool,
partition: &str,
mount: P,
owner: &str,
) -> Result<(), failure::Error> {
shell.run(cmd!("lsblk").dry_run(dry_run))?;
shell.run(cmd!("sudo mkfs.ext4 {}", partition).dry_run(dry_run))?;
shell.run(cmd!("mkdir -p /tmp/tmp_mnt").dry_run(dry_run))?;
shell.run(cmd!("sudo mount -t ext4 {} /tmp/tmp_mnt", partition).dry_run(dry_run))?;
shell.run(cmd!("sudo chown {} /tmp/tmp_mnt", owner).dry_run(dry_run))?;
shell.run(cmd!("rsync -a {}/ /tmp/tmp_mnt/", mount.as_ref().display()).dry_run(dry_run))?;
shell.run(cmd!("sync").dry_run(dry_run))?;
shell.run(cmd!("sudo umount /tmp/tmp_mnt").dry_run(dry_run))?;
shell.run(
cmd!(
"sudo mount -t ext4 {} {}",
partition,
mount.as_ref().display()
)
.dry_run(dry_run),
)?;
shell.run(cmd!("sudo chown {} {}", owner, mount.as_ref().display()).dry_run(dry_run))?;
let uuid = shell
.run(
cmd!("sudo blkid -o export {} | grep '^UUID='", partition)
.use_bash()
.dry_run(dry_run),
)?
.stdout;
let uuid = uuid.trim();
shell.run(
cmd!(
r#"echo "{} {} ext4 defaults 0 1" | sudo tee -a /etc/fstab"#,
uuid,
mount.as_ref().display()
)
.dry_run(dry_run),
)?;
shell.run(cmd!("lsblk").dry_run(dry_run))?;
Ok(())
}
pub fn get_partitions(
shell: &SshShell,
device: &str,
dry_run: bool,
) -> Result<HashSet<String>, failure::Error> {
Ok(shell
.run(cmd!("lsblk -o KNAME {}", device).dry_run(dry_run))?
.stdout
.lines()
.map(|line| line.trim().to_owned())
.skip(2)
.collect())
}
pub fn get_unpartitioned_devs(
shell: &SshShell,
dry_run: bool,
) -> Result<HashSet<String>, failure::Error> {
let lsblk = shell.run(cmd!("lsblk -o KNAME").dry_run(dry_run))?.stdout;
let mut devices: HashSet<&str> = lsblk.lines().map(|line| line.trim()).skip(1).collect();
let partitions: HashMap<_, _> = devices
.iter()
.map(|&dev| {
(
dev,
get_partitions(shell, &format!("/dev/{}", dev), dry_run),
)
})
.collect();
for (dev, parts) in partitions.into_iter() {
let parts = parts?;
if !parts.is_empty() {
devices.remove(dev);
for part in parts {
devices.remove(part.as_str());
}
}
}
Ok(devices.iter().map(|&dev| dev.to_owned()).collect())
}
pub fn get_mounted_devs(
shell: &SshShell,
dry_run: bool,
) -> Result<Vec<(String, String)>, failure::Error> {
let devices = shell
.run(cmd!("lsblk -o KNAME,MOUNTPOINT").dry_run(dry_run))?
.stdout;
let devices = devices.lines().skip(1);
let mut mounted = vec![];
for line in devices {
let split: Vec<_> = line
.split(char::is_whitespace)
.filter(|s| !s.is_empty())
.collect();
if split.len() > 1 {
mounted.push((split[0].to_owned(), split[1].to_owned()));
}
}
Ok(mounted)
}
pub fn get_dev_sizes(
shell: &SshShell,
devs: &HashSet<String>,
dry_run: bool,
) -> Result<Vec<String>, failure::Error> {
let per_dev = devs
.iter()
.map(|dev| shell.run(cmd!("lsblk -o SIZE /dev/{}", dev).dry_run(dry_run)));
let mut sizes = vec![];
for size in per_dev {
sizes.push(
size?
.stdout
.lines()
.skip(1)
.next()
.unwrap()
.trim()
.to_owned(),
);
}
Ok(sizes)
}