-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathsetup.rs
665 lines (619 loc) · 24.8 KB
/
setup.rs
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
use std::collections::{BTreeMap, BTreeSet};
use std::env;
use std::env::{current_exe};
use std::io::{self, BufRead, BufReader, ErrorKind, Write};
use std::fs::{read_link, OpenOptions};
use std::fs::File;
use std::os::unix::fs::{symlink};
use std::path::{Path, PathBuf};
use libc::pid_t;
use libmount::{BindMount, Tmpfs, Remount};
use crate::config::{Container, Settings};
use crate::config::command::{CommandInfo, CapsuleInfo};
use crate::config::read_settings::{MergedSettings};
use crate::config::volumes::Volume;
use crate::container::root::{change_root};
use crate::container::mount::{mount_dev, mount_proc, mount_run, mount_system_dirs, unmount};
use crate::container::network::detect_local_dns;
use crate::container::util::{clean_dir, hardlink_dir, version_from_symlink};
use crate::file_util::{Dir, safe_ensure_dir};
use crate::process_util::{DEFAULT_PATH, PROXY_ENV_VARS};
use crate::storage_dir::sanitize;
use crate::wrapper::snapshot::make_snapshot;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum WriteMode {
ReadOnly,
TransientHardlinkCopy(pid_t),
}
fn create_storage_dir(storage_dir: &Path, project_root: &Path)
-> Result<PathBuf, String>
{
let name = match project_root.file_name().and_then(|x| x.to_str()) {
Some(name) => name,
None => return Err(format!(
"Project dir {:?} is either root or has bad characters",
project_root)),
};
let path = storage_dir.join(name);
if !path.exists() {
return Ok(path);
}
for i in 1..101 {
let result = format!("{}-{}", name, i);
let path = storage_dir.join(result);
if !path.exists() {
return Ok(path);
}
}
return Err(format!("Too many similar paths named {} in {:?}",
name, storage_dir));
}
fn make_cache_dir(_project_root: &Path, vagga_base: &Path,
settings: &MergedSettings)
-> Result<PathBuf, String>
{
match settings.cache_dir {
Some(ref dir) if settings.shared_cache => {
if !dir.exists() {
return Err(format!(concat!("Cache directory {:?} must exists.",
" Please either create it or remove that configuration",
" setting"), dir));
}
return Ok(dir.clone());
}
_ => {
let dir = vagga_base.join(".cache");
safe_ensure_dir(&dir)?;
return Ok(dir);
}
}
}
fn _vagga_base(project_root: &Path, settings: &MergedSettings)
-> Result<Result<PathBuf, (PathBuf, PathBuf)>, String>
{
if let Some(ref dir) = settings.storage_dir {
let lnkdir = project_root.join(".vagga/.lnk");
match read_link(&lnkdir) {
Ok(lnk) => {
if let Some(name) = lnk.file_name() {
let target = dir.join(name);
if lnk.parent() != Some(&*dir) {
let newlnk = dir.join(name);
if newlnk.exists() {
return Err(format!("You have set `storage-dir` \
to {:?}, but .vagga/.lnk points to {:?}. \
You probably need to run \
`ln -sfn {:?} .vagga/.lnk`",
dir, lnk, newlnk));
} else {
return Err(format!("You have set `storage-dir` \
to {:?}, but .vagga/.lnk points to {:?}. \
If storage dir is new, \
just remove `.vagga` dir.",
dir, lnk));
}
}
if !lnkdir.exists() {
return Err(format!("Your .vagga/.lnk points to a \
non-existent directory. Presumably you deleted \
dir {:?}. Just remove .vagga/.lnk now.",
lnk));
}
return Ok(Ok(target));
} else {
return Err(format!(concat!("Bad link .vagga/.lnk: {:?}.",
" You are pobably need to remove it now"),
lnk));
}
}
Err(ref e) if e.kind() == ErrorKind::NotFound => {
return Ok(Err((lnkdir, dir.clone())));
}
Err(ref e) => {
return Err(format!("Can't read link .vagga/.lnk: {}", e));
}
};
} else {
return Ok(Ok(project_root.join(".vagga")));
}
}
pub fn get_vagga_base(project_root: &Path, settings: &MergedSettings)
-> Result<Option<PathBuf>, String>
{
return _vagga_base(project_root, settings).map(|x| x.ok());
}
fn vagga_base(project_root: &Path, settings: &MergedSettings)
-> Result<PathBuf, String>
{
if env_is_set("_VAGGA_IN_CAPSULE") {
return Ok("/vagga/base".into());
}
match _vagga_base(project_root, settings) {
Ok(Err((lnkdir, dir))) => {
let ref svar = settings.storage_subdir_from_env_var;
let target = if let Some(ref var) = *svar {
if let Ok(user_dir) = env::var("_VAGGA_STORAGE_SUBDIR") {
let sanitized = sanitize(&user_dir);
if sanitized.len() == 0 {
return Err(format!("Couldn't determine storage-dir \
subdir from environment, original value {:?} \
(got from {:?})",
user_dir, var));
} else {
info!("Storage dir {:?} (got from {:?})",
user_dir, var);
dir.join(user_dir)
}
} else {
return Err("Internal error: _VAGGA_STORAGE_SUBDIR".into())
}
} else {
create_storage_dir(&dir, project_root)?
};
safe_ensure_dir(&target)?;
symlink(&target, &lnkdir)
.map_err(|e| format!("Error symlinking storage: {}", e))?;
if svar.is_none() {
// Backlink is useful only when not using from-env-var
symlink(project_root, &target.join(".lnk"))
.map_err(|e| format!("Error symlinking storage: {}", e))?;
}
return Ok(target)
}
Ok(Ok(path)) => {
return Ok(path);
}
Err(e) => {
return Err(e);
}
}
}
fn make_mountpoint(project_root: &Path) -> Result<(), String> {
let vagga_dir = project_root.join(".vagga");
safe_ensure_dir(&vagga_dir)?;
let mnt_dir = vagga_dir.join(".mnt");
safe_ensure_dir(&mnt_dir)?;
return Ok(());
}
fn env_is_set(key: &str) -> bool {
env::var(key).map(|x| x.len() > 0).unwrap_or(false)
}
pub fn setup_base_filesystem(project_root: &Path, settings: &MergedSettings)
-> Result<(), String>
{
let mnt_dir = project_root.join(".vagga/.mnt");
make_mountpoint(project_root)?;
Tmpfs::new(&mnt_dir).size_bytes(100 << 20).mount()
.map_err(|e| format!("{}", e))?;
let proc_dir = mnt_dir.join("proc");
try_msg!(Dir::new(&proc_dir).create(),
"Error creating /proc: {err}");
mount_proc(&proc_dir)?;
let dev_dir = mnt_dir.join("dev");
try_msg!(Dir::new(&dev_dir).create(),
"Error creating /dev: {err}");
mount_dev(&dev_dir)?;
let sys_dir = mnt_dir.join("sys");
try_msg!(Dir::new(&sys_dir).create(),
"Error creating /sys: {err}");
try_msg!(BindMount::new("/sys", &sys_dir).mount(), "mount /sys: {err}");
let selinux = sys_dir.join("fs/selinux");
if selinux.is_dir() {
// Need this go get some selinux-aware commands to work (see #65)
Remount::new(&sys_dir.join("fs/selinux"))
.bind(true).readonly(true).remount()
.map_err(|e| {
let err = e.to_string();
// Sometimes dir exists but is not a mountpoint, that's okay
if !err.contains("Cannot find mount point") {
warn!("remount /sys/fs/selinux: {}", e)
}
}).ok();
}
let vagga_dir = mnt_dir.join("vagga");
try_msg!(Dir::new(&vagga_dir).create(),
"Error creating /vagga: {err}");
let bin_dir = vagga_dir.join("bin");
try_msg!(Dir::new(&bin_dir).create(),
"Error creating /vagga/bin: {err}");
try_msg!(BindMount::new(¤t_exe().unwrap().parent().unwrap(),
&bin_dir)
.readonly(true)
.mount(), "mount /vagga/bin: {err}");
if let Some(ref storage_dir) = settings.storage_dir {
let tgtstorage = vagga_dir.join("storage");
try_msg!(Dir::new(&tgtstorage).create(),
"Error creating {path:?} directory: {err}", path=&tgtstorage);
try_msg!(BindMount::new(storage_dir, &tgtstorage).mount(),
"mount storage: {err}");
}
let etc_dir = mnt_dir.join("etc");
try_msg!(Dir::new(&etc_dir).create(),
"Error creating /etc: {err}");
let ref hosts_path = etc_dir.join("hosts");
File::create(hosts_path).map_err(|e|
format!("Cannot create {:?} file: {}", hosts_path, e))?;
BindMount::new(Path::new("/etc/hosts"), hosts_path)
.mount()
.map_err(|e| format!("Error mounting /etc/hosts: {}", e))?;
let local_dns = if env_is_set("VAGGA_IN_NAMESPACE") ||
env_is_set("VAGGA_NAMESPACE_DIR")
{
detect_local_dns().map_err(|e| format!("{}", e))?
} else {
None
};
let resolv_source = if let Some(_) = local_dns {
// TODO(tailhook) do it on detached tmpfs
File::create("/tmp/resolv.conf")
.and_then(|mut f|
// redirect to a gateway IP
writeln!(&mut f, "nameserver 172.23.255.1"))
.map_err(|e| format!("Can't create temporary resolv.conf: {}", e))?;
Path::new("/tmp/resolv.conf")
} else {
Path::new("/etc/resolv.conf")
};
let ref resolv_path = etc_dir.join("resolv.conf");
if !resolv_path.exists() {
File::create(resolv_path).map_err(|e|
format!("Cannot create {:?} file: {}", resolv_path, e))?;
}
BindMount::new(&resolv_source, resolv_path)
.mount()
.map_err(|e| format!("Error copying /etc/resolv.conf: {}", e))?;
let local_base = vagga_dir.join("base");
safe_ensure_dir(&local_base)?;
let vagga_base = vagga_base(project_root, settings)?;
try_msg!(BindMount::new(&vagga_base, &local_base).mount(),
"mount /vagga/base: {err}");
safe_ensure_dir(&local_base.join(".roots"))?;
safe_ensure_dir(&local_base.join(".transient"))?;
let cache_dir = vagga_dir.join("cache");
try_msg!(Dir::new(&cache_dir).create(),
"Error creating /vagga/cache: {err}");
let locl_cache = make_cache_dir(project_root, &vagga_base, settings)?;
try_msg!(BindMount::new(&locl_cache, &cache_dir).mount(),
"mount /vagga/cache: {err}");
if let Ok(nsdir) = env::var("VAGGA_NAMESPACE_DIR") {
let newns_dir = vagga_dir.join("namespaces");
try_msg!(Dir::new(&newns_dir).recursive(true).create(),
"Error creating directory for namespaces: {err}");
try_msg!(BindMount::new(&nsdir, &newns_dir).mount(),
"namespace dir: {err}");
}
let volume_dir = mnt_dir.join("volumes");
try_msg!(Dir::new(&volume_dir).create(),
"Error creating /volumes: {err}");
for (name, source_path) in &settings.external_volumes {
let dest = volume_dir.join(name);
if source_path.is_dir() {
try_msg!(Dir::new(&dest).create(),
"Error creating {p:?}: {err}", p=dest);
} else {
try_msg!(File::create(&dest),
"Error creating {p:?}: {err}", p=dest);
}
try_msg!(BindMount::new(source_path, &dest).mount(),
"volume: {err}");
}
let work_dir = mnt_dir.join("work");
try_msg!(Dir::new(&work_dir).create(),
"Error creating /work: {err}");
try_msg!(BindMount::new(project_root, &work_dir).mount(),
"mount /work: {err}");
let old_root = vagga_dir.join("old_root");
try_msg!(Dir::new(&old_root).create(),
"Error creating /vagga/old_root: {err}");
change_root(&mnt_dir, &old_root)?;
unmount(&Path::new("/work/.vagga/.mnt"))
.map_err(|e| format!("Error unmounting `.vagga/.mnt`: {}", e))?;
unmount(&Path::new("/vagga/old_root"))?;
Ok(())
}
pub fn get_environment(settings: &Settings, container: &Container,
command: Option<&CommandInfo>)
-> Result<BTreeMap<String, String>, String>
{
_get_environment(DEFAULT_PATH, settings, Some(container),
command.map(|c| &c.environ))
}
pub fn get_capsule_environment(settings: &Settings,
command: &CapsuleInfo)
-> Result<BTreeMap<String, String>, String>
{
_get_environment("/vagga/bin:/bin", settings, None, Some(&command.environ))
}
fn _get_environment(default_path: &str, settings: &Settings,
container: Option<&Container>,
command_env: Option<&BTreeMap<String, String>>)
-> Result<BTreeMap<String, String>, String>
{
let mut result = BTreeMap::new();
result.insert("PATH".to_string(),
default_path.to_string());
if settings.proxy_env_vars {
for k in &PROXY_ENV_VARS {
if let Ok(v) = env::var(k) {
result.insert(k.to_string(), v);
}
}
}
if let Some(ref filename) = container.and_then(|x| x.environ_file.as_ref()) {
let f = BufReader::new(
File::open(filename)
.map_err(|e| format!("Error reading environment file {:?}: {}",
filename, e))?);
for line_read in f.lines() {
let line = line_read
.map_err(|e| format!("Error reading environment file {:?}: {}",
filename, e))?;
let mut pair = line[..].splitn(2, '=');
let key = pair.next().unwrap();
let mut value = pair.next()
.ok_or(format!("Error reading environment file {:?}: bad format",
filename))?;
if value.len() > 0 && value.starts_with("\"") {
value = value.trim_matches('"');
}
result.insert(key.to_string(), value.to_string());
}
}
if let Some(container) = container {
for (ref k, ref v) in container.environ.iter() {
result.insert(k.to_string(), v.to_string());
}
}
if let Some(command_env) = command_env {
for (ref k, ref v) in command_env.iter() {
result.insert(k.to_string(), v.to_string());
}
}
for (ref k, ref v) in settings.environ.iter() {
result.insert(k.to_string(), v.to_string());
}
for (k, v) in env::vars() {
if k.starts_with("VAGGAENV_") {
result.insert(k[9..].to_string(), v);
} else if k.starts_with("VAGGAOPT_") || k.starts_with("VAGGACLI_") {
result.insert(k, v);
}
}
return Ok(result);
}
pub struct SetupInfo<'a> {
pub volumes: BTreeMap<&'a PathBuf, &'a Volume>,
pub tmp_volumes: BTreeSet<&'a String>,
pub write_mode: WriteMode,
pub resolv_conf_path: Option<&'a PathBuf>,
pub hosts_file_path: Option<&'a PathBuf>,
}
impl<'a> SetupInfo<'a> {
pub fn from_container(container: &Container) -> SetupInfo {
let mut setup_info = SetupInfo {
volumes: BTreeMap::new(),
tmp_volumes: BTreeSet::new(),
write_mode: WriteMode::ReadOnly,
resolv_conf_path: container.resolv_conf_path.as_ref(),
hosts_file_path: container.hosts_file_path.as_ref(),
};
setup_info.volumes(&container.volumes);
setup_info
}
pub fn volumes(&mut self, volumes: &'a BTreeMap<PathBuf, Volume>)
-> &mut SetupInfo<'a>
{
for (path, vol) in volumes.iter() {
self.volumes.insert(&path, &vol);
}
self
}
pub fn write_mode(&mut self, write_mode: WriteMode)
-> &mut SetupInfo<'a>
{
self.write_mode = write_mode;
self
}
}
pub fn setup_filesystem(setup_info: &SetupInfo, container_ver: &str)
-> Result<(), String>
{
let tgtroot = Path::new("/vagga/root");
if !tgtroot.exists() {
try_msg!(Dir::new(&tgtroot).create(),
"Can't create rootfs mountpoint: {err}");
}
let image_base = Path::new("/vagga/base/.roots").join(container_ver);
match setup_info.write_mode {
WriteMode::ReadOnly => {
let nroot = image_base.join("root");
try_msg!(BindMount::new(&nroot, &tgtroot).readonly(true).mount(),
"mount root: {err}");
}
WriteMode::TransientHardlinkCopy(pid) => {
let oldpath = image_base.join("root");
let newpath = Path::new("/vagga/base/.transient")
.join(format!("{}.{}", container_ver, pid));
if newpath.exists() {
try_msg!(clean_dir(&newpath, true),
"Error removing dir: {err}");
}
try_msg!(Dir::new(&newpath).create(),
"Error creating directory: {err}");
try_msg!(hardlink_dir(&oldpath, &newpath),
"Can't hardlink {p:?}: {err}", p=newpath);
try_msg!(BindMount::new(&newpath, &tgtroot).mount(),
"transient root: {err}");
}
};
File::create(image_base.join("last_use"))
.map_err(|e| warn!("Can't write image usage info: {}", e)).ok();
mount_system_dirs()
.map_err(|e| format!("Error mounting system dirs: {}", e))?;
if let None = setup_info.volumes.get(&PathBuf::from("/tmp")) {
Tmpfs::new(&tgtroot.join("tmp"))
.size_bytes(100 << 20)
.mode(0o1777)
.mount().map_err(|e| format!("{}", e))?;
}
if let None = setup_info.volumes.get(&PathBuf::from("/run")) {
mount_run(&tgtroot.join("run"))?;
}
for (path, vol) in setup_info.volumes.iter() {
let ref rel_path = path.strip_prefix("/").unwrap();
let dest = tgtroot.join(rel_path);
use crate::config::volumes::Volume as V;
match *vol {
&V::Tmpfs(ref params) => {
Tmpfs::new(&dest)
.size_bytes(params.size)
.mode(params.mode)
.mount().map_err(|e| format!("{}", e))?;
for (subpath, info) in ¶ms.subdirs {
try_msg!(Dir::new(&dest.join(&subpath))
.mode(info.mode)
.create(),
"Error creating subdir {sub:?} of {vol:?}: {err}",
sub=subpath, vol=path);
}
for (subpath, text) in ¶ms.files {
let text = text.as_ref().map(|x| &x[..]).unwrap_or("");
try_msg!(File::create(&dest.join(&subpath))
.and_then(|mut f| f.write_all(text.as_bytes())),
"Error creating file {sub:?} of {vol:?}: {err}",
sub=subpath, vol=path);
}
}
&V::VaggaBin => {
try_msg!(BindMount::new("/vagga/bin", &dest).mount(),
"mount !VaggaBin: {err}");
}
&V::BindRW(ref bindpath) => {
try_msg!(BindMount::new(&bindpath, &dest).mount(),
"mount !BindRW: {err}");
}
&V::BindRO(ref bindpath) => {
try_msg!(BindMount::new(&bindpath, &dest)
.readonly(true)
.mount(),
"mount !BindRO: {err}");
}
&V::CacheDir(ref bindpath) => {
// avoid mounting cache root
if bindpath == Path::new("") {
return Err("mount !CacheDir: path must not be empty".into())
}
// bind path must be relative to cache root
if bindpath.is_absolute() {
return Err(format!(
"mount !CacheDir {:?}: path must not be absolute",
&bindpath))
}
let source = Path::new("/vagga/cache").join(bindpath);
if !source.exists() {
try_msg!(Dir::new(&source).recursive(true).create(),
"error creating dir for volume {p:?}: {err}", p=path);
}
try_msg!(BindMount::new(&source, &dest).mount(),
"mount !CacheDir: {err}");
}
&V::Empty => {
Tmpfs::new(&dest)
.size_bytes(1)
.mode(0)
.mount().map_err(|e| format!("{}", e))?;
try_msg!(Remount::new(&dest).bind(true).readonly(true).remount(),
"remount !Empty: {err}");
}
&V::Snapshot(ref info) => {
let ref src = match info.container {
Some(ref src_container) => {
let container_ver = version_from_symlink(
format!("/work/.vagga/{}", src_container))?;
Path::new("/vagga/base/.roots")
.join(container_ver)
.join("root")
.join(rel_path)
},
None => dest.clone(),
};
make_snapshot(src, &dest, info)?;
}
&V::Container(ref child_cont) => {
let container_ver = version_from_symlink(
format!("/work/.vagga/{}", child_cont))?;
let target = Path::new("/vagga/base/.roots")
.join(container_ver).join("root");
try_msg!(BindMount::new(&target, &dest).readonly(true).mount(),
"mount !Container: {err}");
}
&V::Persistent(ref info) => {
if setup_info.tmp_volumes.contains(&info.name) {
let path = Path::new("/vagga/base/.volumes")
.join(&format!(".tmp.{}", info.name));
try_msg!(Dir::new(&path).recursive(true).create(),
"error creating dir for volume {p:?}: {err}", p=path);
try_msg!(BindMount::new(&path, &dest).mount(),
"mount !BindRW: {err}");
} else {
let path = Path::new("/vagga/base/.volumes")
.join(&info.name);
if info.init_command.is_none() {
try_msg!(Dir::new(&path)
.recursive(true)
.uid(info.owner_uid)
.gid(info.owner_gid)
.create(),
"error creating dir for volume {p:?}: {err}",
p=path);
} else if !path.exists() {
return Err(format!("Internal error: \
Got uninitialized volume {:?}",
info.name));
}
try_msg!(BindMount::new(&path, &dest).mount(),
"mount !BindRW: {err}");
};
}
}
}
if let Ok(_) = env::var("VAGGA_NAMESPACE_DIR") {
let newns_dir = tgtroot.join("tmp/vagga/namespaces");
try_msg!(Dir::new(&newns_dir).recursive(true).create(),
"Error creating directory for namespaces: {err}");
try_msg!(BindMount::new("/vagga/namespaces", &newns_dir).mount(),
"mount namespaces: {err}");
}
if let Some(path) = setup_info.resolv_conf_path {
if !setup_info.volumes.contains_key(&path) {
let ref path = tgtroot.join(path.strip_prefix("/").unwrap());
create_and_mount(Path::new("/etc/resolv.conf"), path)?;
}
}
if let Some(path) = setup_info.hosts_file_path {
if !setup_info.volumes.contains_key(&path) {
let ref path = tgtroot.join(path.strip_prefix("/").unwrap());
create_and_mount(Path::new("/etc/hosts"), path)?;
}
}
change_root(&tgtroot, &tgtroot.join("tmp"))
.map_err(|e| format!("Error changing root: {}", e))?;
unmount(&Path::new("/tmp"))
.map_err(|e| format!("Error unmounting `/tmp`: {}", e))?;
Ok(())
}
fn create_and_mount(src: &Path, dst: &Path) -> Result<(), String> {
OpenOptions::new().write(true).create_new(true).open(dst)
.map(|_| ())
.or_else(|e| {
if e.kind() == io::ErrorKind::AlreadyExists {
Ok(())
} else {
Err(format!("Cannot create file {:?}: {}", dst, e))
}
})?;
BindMount::new(src, dst)
.mount()
.map_err(|e| format!("Error mounting {:?}: {}", src, e))?;
Ok(())
}