-
Notifications
You must be signed in to change notification settings - Fork 15
/
mount.c
102 lines (89 loc) · 2.87 KB
/
mount.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
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
//
// Mount a container-filesystem. Changes to the filesystem will be written to
// CHANGESDIR.
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <plash.h>
#define USAGE "usage: plash mount IMAGE_ID MOUNTPOINT [ CHANGESDIR ]\n"
#define CHANGESDIR_ALLOWED_CHARS \
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./-_"
char *prepare_lowerdir(char *image_id) {
char *lowerdir = "";
char *plash_data = plash("data");
char *nodepath = plash("nodepath", image_id);
int offset = strlen(plash_data) + strlen("/layer/");
if (strlen(nodepath) < offset) {
pl_fatal("Unexpected interal error: nodepath command output is shorter "
"than data command output");
}
char *curr;
char *str = nodepath + offset;
while (curr = strsep(&str, "/")) {
asprintf(&lowerdir, "%s/index/%s/_data/root:%s", plash_data, curr,
lowerdir) != -1 ||
pl_fatal("asprintf");
}
lowerdir[strlen(lowerdir) - 1] = '\0'; // chop the last ":"
return lowerdir;
}
char *prepare_workdir(char *changesdir) {
char *workdir;
asprintf(&workdir, "%s/work", changesdir) != -1 || pl_fatal("asprintf");
if (mkdir(workdir, 0755) == -1 && errno != EEXIST)
pl_fatal("mkdir");
return workdir;
}
char *prepare_datadir(char *changesdir) {
char *datadir;
asprintf(&datadir, "%s/data", changesdir) != -1 || pl_fatal("asprintf");
if (mkdir(datadir, 0755) == -1 && errno != EEXIST)
pl_fatal("mkdir");
return datadir;
}
void validate_changesdir(char *changesdir) {
int found_in_allowed;
for (size_t i = 0; changesdir[i]; i++) {
found_in_allowed = 0;
for (size_t j = 0; strlen(CHANGESDIR_ALLOWED_CHARS) > j; j++) {
if (changesdir[i] == CHANGESDIR_ALLOWED_CHARS[j]) {
found_in_allowed = 1;
}
}
if (!found_in_allowed) {
pl_fatal("Not allowed charachter found in changesdir: %c", changesdir[i]);
}
}
}
int mount_main(int argc, char *argv[]) {
if (argc < 3) {
{
fputs(USAGE, stderr);
return EXIT_FAILURE;
}
}
char *image_id = argv[1];
char *mountpoint = argv[2];
char *changesdir = argv[3];
char *mount_opts;
if (changesdir != NULL) {
validate_changesdir(changesdir);
if (mkdir(changesdir, 0755) == -1 && errno != EEXIST)
pl_fatal("mkdir %s", changesdir);
asprintf(&mount_opts, "lowerdir=%s,workdir=%s,upperdir=%s",
prepare_lowerdir(image_id), prepare_workdir(changesdir),
prepare_datadir(changesdir)) != -1 ||
pl_fatal("asprintf");
} else {
asprintf(&mount_opts, "lowerdir=%s", prepare_lowerdir(image_id)) != -1 ||
pl_fatal("asprintf");
}
execlp("mount", "mount", "-t", "overlay", "overlay", "-o", mount_opts,
mountpoint, NULL);
pl_fatal("execlp");
}