Skip to content

Commit 112582a

Browse files
committed
wip: move shared functions to a header file for re-use
Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me> init: abstract configuring the root block device Configuring the root block device is a fairly isolated block, so abstract it into its own separate function so it can be re-used. Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me> wip: abstract most of the content in init.c to utils.h Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me> wip: split utils into .h and .c files Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me> wip: move config parsing logic to its own files Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me> wip: move fs related code to its own files Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me> wip: move timesync related code to its own files Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me> wip: do not expose helper functions Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me> wip: remove misc #include directives Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
1 parent 065e19d commit 112582a

10 files changed

Lines changed: 1017 additions & 877 deletions

File tree

init/fs.c

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#include "fs.h"
2+
3+
static int try_mount(const char *source, const char *target, const char *fstype,
4+
unsigned long mountflags, const void *data);
5+
6+
void setup_root_block_device(void)
7+
{
8+
int fd;
9+
char *krun_root;
10+
char *krun_root_fstype;
11+
char *krun_root_options;
12+
13+
krun_root = getenv("KRUN_BLOCK_ROOT_DEVICE");
14+
if (krun_root) {
15+
if (mkdir("/newroot", 0755) < 0 && errno != EEXIST) {
16+
perror("mkdir(/newroot)");
17+
exit(-1);
18+
}
19+
20+
krun_root_fstype = getenv("KRUN_BLOCK_ROOT_FSTYPE");
21+
krun_root_options = getenv("KRUN_BLOCK_ROOT_OPTIONS");
22+
23+
if (try_mount(krun_root, "/newroot", krun_root_fstype, 0,
24+
krun_root_options) < 0) {
25+
perror("mount KRUN_BLOCK_ROOT_DEVICE");
26+
exit(-1);
27+
}
28+
29+
chdir("/newroot");
30+
31+
fd = open("/", O_RDONLY);
32+
if (fd < 0) {
33+
perror("Couldn't open temporary root directory for removing");
34+
exit(-1);
35+
}
36+
if (ioctl(fd, KRUN_REMOVE_ROOT_DIR_IOCTL) < 0) {
37+
perror("Error removing temporary root directory");
38+
}
39+
close(fd);
40+
41+
if (mount(".", "/", NULL, MS_MOVE, NULL) < 0) {
42+
perror("remount root");
43+
exit(-1);
44+
}
45+
chroot(".");
46+
47+
// we must mount filesystems again after chrooting
48+
if (mount_filesystems() < 0) {
49+
printf("Couldn't mount filesystems, bailing out\n");
50+
exit(-2);
51+
}
52+
}
53+
54+
if (mount(NULL, "/", NULL, MS_REC | MS_SHARED, NULL) < 0) {
55+
perror("Couldn't set shared propagation on the root mount");
56+
exit(-1);
57+
}
58+
}
59+
60+
int mount_filesystems()
61+
{
62+
char *const DIRS_LEVEL1[] = {"/dev", "/proc", "/sys"};
63+
char *const DIRS_LEVEL2[] = {"/dev/pts", "/dev/shm"};
64+
int i;
65+
66+
for (i = 0; i < 3; ++i) {
67+
if (mkdir(DIRS_LEVEL1[i], 0755) < 0 && errno != EEXIST) {
68+
printf("Error creating directory (%s)\n", DIRS_LEVEL1[i]);
69+
return -1;
70+
}
71+
}
72+
73+
if (mount("devtmpfs", "/dev", "devtmpfs", MS_RELATIME, NULL) < 0 &&
74+
errno != EBUSY) {
75+
perror("mount(/dev)");
76+
return -1;
77+
}
78+
79+
if (mount("proc", "/proc", "proc",
80+
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
81+
perror("mount(/proc)");
82+
return -1;
83+
}
84+
85+
if (mount("sysfs", "/sys", "sysfs",
86+
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
87+
perror("mount(/sys)");
88+
return -1;
89+
}
90+
91+
if (mount("cgroup2", "/sys/fs/cgroup", "cgroup2",
92+
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
93+
perror("mount(/sys/fs/cgroup)");
94+
return -1;
95+
}
96+
97+
for (i = 0; i < 2; ++i) {
98+
if (mkdir(DIRS_LEVEL2[i], 0755) < 0 && errno != EEXIST) {
99+
printf("Error creating directory (%s)\n", DIRS_LEVEL2[i]);
100+
return -1;
101+
}
102+
}
103+
104+
if (mount("devpts", "/dev/pts", "devpts",
105+
MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
106+
perror("mount(/dev/pts)");
107+
return -1;
108+
}
109+
110+
if (mount("tmpfs", "/dev/shm", "tmpfs", MS_NOEXEC | MS_NOSUID | MS_RELATIME,
111+
NULL) < 0) {
112+
perror("mount(/dev/shm)");
113+
return -1;
114+
}
115+
116+
/* May fail if already exists and that's fine. */
117+
symlink("/proc/self/fd", "/dev/fd");
118+
119+
return 0;
120+
}
121+
122+
static int try_mount(const char *source, const char *target, const char *fstype,
123+
unsigned long mountflags, const void *data)
124+
{
125+
FILE *f;
126+
char line[129];
127+
int mount_status = -1;
128+
129+
if (fstype) {
130+
return mount(source, target, fstype, mountflags, data);
131+
}
132+
133+
f = fopen("/proc/filesystems", "r");
134+
if (f == NULL) {
135+
perror("fopen(/proc/filesystems)");
136+
return -1;
137+
}
138+
while (fgets(line, sizeof(line), f)) {
139+
char fstype[sizeof(line)];
140+
if (!strncmp(line, "nodev", 5)) {
141+
continue;
142+
}
143+
if (sscanf(line, "%128s\n", fstype) != 1) {
144+
continue;
145+
}
146+
147+
mount_status = mount(source, target, fstype, mountflags, data);
148+
if (mount_status == 0) {
149+
break;
150+
}
151+
}
152+
fclose(f);
153+
154+
return mount_status;
155+
}
156+
157+
int is_virtiofs(const char *path)
158+
{
159+
struct statfs fs;
160+
161+
if (statfs(path, &fs) != 0) {
162+
perror("statfs");
163+
return -1;
164+
}
165+
166+
// virtiofs magic number: 0x65735546
167+
return (fs.f_type == 0x65735546) ? 1 : 0;
168+
}
169+

init/fs.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef FS_H
2+
#define FS_H
3+
4+
#include <string.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <fcntl.h>
8+
#include <errno.h>
9+
#include <unistd.h>
10+
11+
#include <sys/mount.h>
12+
#include <sys/stat.h>
13+
#include <sys/statfs.h>
14+
15+
#define KRUN_REMOVE_ROOT_DIR_IOCTL 0x7603
16+
17+
void setup_root_block_device(void);
18+
int is_virtiofs(const char *path);
19+
int mount_filesystems();
20+
21+
#endif

0 commit comments

Comments
 (0)