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
|
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <stdio.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <unistd.h>
struct rootfs {
char *dev;
int flags;
};
int get_rootfs(struct rootfs *root)
{
int fd;
ssize_t len;
char buf[1024];
root->dev = NULL;
root->flags = 0;
fd = open("/proc/cmdline", O_RDONLY);
if (fd < 0) {
return -1;
}
len = read(fd, buf, sizeof(buf) - 1);
if (len < 0) {
close(fd);
return -1;
}
buf[len] = '\0';
close(fd);
// Read cmdline by words
// not using strtok
char *p = buf;
while (*p) {
while (*p == ' ')
p++;
if (*p == '\0')
break;
char *start = p;
while (*p && *p != ' ')
p++;
size_t word_len = p - start;
if (strncmp(start, "root=", 5) == 0) {
size_t dev_len = word_len - 5;
root->dev = strndup(start + 5, dev_len);
} else if (strncmp(start, "ro", 2) == 0) {
root->flags |= MS_RDONLY;
} else if (strncmp(start, "rw", 2) == 0) {
root->flags &= ~MS_RDONLY;
}
}
return 0;
}
int main(void)
{
struct rootfs root;
char *const argv[] = { "/bin/init", NULL };
if (mkdir("/dev", 0755) < 0 && errno != EEXIST) {
perror("mkdir /dev");
return -1;
}
if (mount("devtmpfs", "/dev", "devtmpfs", 0, NULL) < 0) {
perror("mount devtmpfs");
return -1;
}
if (mkdir("/proc", 0755) < 0 && errno != EEXIST) {
perror("mkdir /proc");
return -1;
}
if (mount("proc", "/proc", "proc", 0, NULL) < 0) {
perror("mount proc");
return -1;
}
/* Get root filesystem info from kernel cmdline */
if (get_rootfs(&root) < 0) {
err(1, "init: get rootfs info");
}
if (mount(root.dev, "/root", "ext4", root.flags, NULL) < 0)
err(1, "init: mount rootfs %s", root.dev);
if (mkdir("/root/dev", 0755) < 0)
err(1, "init: mkdir /root/dev");
if (mount("/dev", "/root/dev", NULL, MS_BIND | MS_REC, NULL) < 0)
err(1, "init: mount --bind /dev");
if (mkdir("/root/proc", 0755) < 0)
err(1, "init: chdir /root");
if (mount("/proc", "/root/proc", NULL, MS_BIND | MS_REC, NULL) < 0)
err(1, "init: mount --bind /proc");
if (mkdir("/root/sys", 0755) < 0)
err(1, "init: chdir /root");
if (mount("sysfs", "/root/sys", "sysfs", 0, NULL) < 0)
err(1, "init: mount --bind /sys");
if (chroot("/root") < 0)
err(1, "init: chroot /root");
if (chdir("/") < 0)
err(1, "init: chdir /");
execve("/bin/init", argv, NULL);
err(1, "init: execve /bin/init");
}
|