-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmounted.c
53 lines (44 loc) · 1.23 KB
/
mounted.c
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
//
// Execute parameters inside a mounted container. Default parameter is the
// default shell. Mounting happens inside an isolated mount namespace.
//
// Example to get how much disk space a container is using:
// $ plash with-mount 70 du -sh
// 7,2M .
#define _GNU_SOURCE
#define USAGE "usage: plash with-mount CONTAINER [ CMD1 [ CMD2 ... ] ]\n"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <plash.h>
int mounted_main(int argc, char *argv[]) {
if (argc < 2) {
fputs(USAGE, stderr);
return EXIT_FAILURE;
}
char *image_id = argv[1];
char *cmd = argv[2];
pl_unshare_user();
pl_unshare_mount();
char *mountpoint;
asprintf(&mountpoint, "%s/mnt", plash("data"));
plash("mount", image_id, mountpoint);
if (chdir(mountpoint) == -1)
pl_fatal("chdir");
if (cmd == NULL) {
char *default_root_shell = pl_get_default_root_shell();
execlp(default_root_shell, default_root_shell, NULL);
pl_fatal("execlp");
} else {
argv++; // chop argv[0]
argv++; // chop the image_id arg;
execvp(*argv, argv);
if (errno == ENOENT) {
fprintf(stderr, "%s: command not found\n", *argv);
return 127;
}
pl_fatal("execvp");
}
return EXIT_SUCCESS;
}