-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnob.c
More file actions
198 lines (174 loc) · 7.74 KB
/
nob.c
File metadata and controls
198 lines (174 loc) · 7.74 KB
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
#define NOB_IMPLEMENTATION
#define NOB_STRIP_PREFIX
#include "nob.h"
static const char *ARCH = "aarch64";
static const char *QEMU = "qemu-system-aarch64";
static const char *IMGK = "vmlinuz-lts";
static const char *USER_NAME = "mitre";
static const char *HOSTNAME = "mini";
static const char *KURL_FMT = "https://dl-cdn.alpinelinux.org/alpine/v3.20/releases/%s/netboot/vmlinuz-lts";
// Precomputed SHA-512 hash for password "mitre"
static const char *MITRE_HASH = "$6$nQmdwcmrIuU2vesW$SOH6oVx5GlCSGrl0LCmFPZIYPMHcw4rc9tTNnsuV1l641zxpU0p/cTbe.rH4ZJveoi8vSf7awzEzXTrI2pZGu0";
static bool write_text_file(const char *path, const char *txt)
{
return write_entire_file(path, txt, strlen(txt));
}
static bool write_from_asset(const char *asset_path, const char *dst_path)
{
Nob_String_Builder sb = {0};
if (!read_entire_file(asset_path, &sb)) return false;
bool ok = write_entire_file(dst_path, sb.items, sb.count);
sb_free(sb);
return ok;
}
int main(int argc, char **argv)
{
NOB_GO_REBUILD_URSELF(argc, argv);
const char *workdir = "mini-sinit";
const char *cwd = get_current_dir_temp();
log(NOB_INFO, "[*] Preparando carpetas...");
mkdir_if_not_exists(workdir);
const char *build_dir = temp_sprintf("%s/build", workdir);
const char *out_dir = temp_sprintf("%s/out", workdir);
mkdir_if_not_exists(build_dir);
mkdir_if_not_exists(out_dir);
const char *rootfs = temp_sprintf("%s/rootfs", workdir);
mkdir_if_not_exists(rootfs);
const char *subdirs[] = {
"bin", "sbin", "usr", "usr/bin", "usr/sbin", "etc", "proc", "sys", "dev", "run", "root", "home", "var", "tmp"
};
for (size_t i = 0; i < NOB_ARRAY_LEN(subdirs); ++i) {
mkdir_if_not_exists(temp_sprintf("%s/%s", rootfs, subdirs[i]));
}
// chmod 1777 tmp
{
Cmd cmd = {0};
cmd_append(&cmd, "chmod", "1777", temp_sprintf("%s/tmp", rootfs));
if (!cmd_run(&cmd)) return 1;
}
const char *kernel_path = temp_sprintf("%s/%s", workdir, IMGK);
if (!file_exists(kernel_path)) {
log(NOB_INFO, "[*] Descargando kernel LTS Alpine (%s)...", ARCH);
Cmd cmd = {0};
const char *kurl = temp_sprintf(KURL_FMT, ARCH);
cmd_append(&cmd, "curl", "-fsSL", kurl, "-o", kernel_path);
if (!cmd_run(&cmd)) return 1;
} else {
log(NOB_INFO, "[*] Kernel LTS Alpine (%s) ya existe, saltando descarga...", ARCH);
}
const char *dockerfile_path = temp_sprintf("%s/build/Dockerfile", workdir);
if (!copy_file("assets/Dockerfile", dockerfile_path)) {
log(NOB_ERROR, "No se pudo escribir Dockerfile en %s", dockerfile_path);
return 1;
}
size_t bin_count = 0;
{
Nob_File_Paths paths = {0};
const char *bin_dir = temp_sprintf("%s/out/bin", workdir);
if (read_entire_dir(bin_dir, &paths)) {
bin_count = paths.count;
}
da_free(paths);
}
if (bin_count >= 150) {
log(NOB_INFO, "[*] Binarios ya compilados encontrados (%zu archivos), saltando compilación...", bin_count);
} else {
log(NOB_INFO, "[*] Construyendo toolchain estática en Docker...");
{
Cmd cmd = {0};
const char *abs_build = temp_sprintf("%s/%s", cwd, build_dir);
cmd_append(&cmd, "env", "DOCKER_BUILDKIT=0", "docker", "build",
"-t", "mini-sinit-build",
"-f", dockerfile_path,
abs_build);
if (!cmd_run(&cmd)) return 1;
}
log(NOB_INFO, "[*] Extrayendo binarios compilados...");
{
Cmd cmd = {0};
const char *abs_out = temp_sprintf("%s/%s", cwd, out_dir);
cmd_append(&cmd, "docker", "run", "--rm",
"-v", temp_sprintf("%s:/hostout", abs_out),
"mini-sinit-build", "sh", "-lc",
"cp -a /out/* /hostout/");
if (!cmd_run(&cmd)) return 1;
}
}
log(NOB_INFO, "[*] Poblado de rootfs con sbase/ubase/sinit/doas...");
{
(void) mkdir_if_not_exists(temp_sprintf("%s/rootfs/usr/bin", workdir));
(void) mkdir_if_not_exists(temp_sprintf("%s/rootfs/usr/sbin", workdir));
(void) copy_directory_recursively(temp_sprintf("%s/out/bin", workdir), temp_sprintf("%s/rootfs/bin", workdir));
(void) copy_directory_recursively(temp_sprintf("%s/out/sbin", workdir), temp_sprintf("%s/rootfs/sbin", workdir));
}
if (!file_exists(temp_sprintf("%s/rootfs/bin/sh", workdir))) {
log(NOB_ERROR, "ERROR: no se encontró /bin/sh (sbase).");
return 1;
}
write_text_file(temp_sprintf("%s/rootfs/etc/fstab", workdir), "");
if (!write_from_asset("assets/rc.init", temp_sprintf("%s/rootfs/etc/rc.init", workdir))) return 1;
if (!write_from_asset("assets/rc.shutdown", temp_sprintf("%s/rootfs/etc/rc.shutdown", workdir))) return 1;
// chmod +x
{
Cmd cmd = {0};
cmd_append(&cmd, "chmod", "+x", temp_sprintf("%s/rootfs/etc/rc.init", workdir), temp_sprintf("%s/rootfs/etc/rc.shutdown", workdir));
if (!cmd_run(&cmd)) return 1;
}
if (!write_from_asset("assets/doas.conf", temp_sprintf("%s/rootfs/etc/doas.conf", workdir))) return 1;
{
Nob_String_Builder sb = {0};
if (!read_entire_file("assets/passwd.tmpl", &sb)) return 1;
sb_append_null(&sb);
const char *passwd_txt = temp_sprintf(sb.items, USER_NAME, USER_NAME, USER_NAME);
sb_free(sb);
write_text_file(temp_sprintf("%s/rootfs/etc/passwd", workdir), passwd_txt);
}
if (!write_from_asset("assets/group", temp_sprintf("%s/rootfs/etc/group", workdir))) return 1;
{
Nob_String_Builder sb = {0};
if (!read_entire_file("assets/shadow.tmpl", &sb)) return 1;
sb_append_null(&sb);
const char *shadow_txt = temp_sprintf(sb.items, MITRE_HASH, USER_NAME, MITRE_HASH);
sb_free(sb);
write_text_file(temp_sprintf("%s/rootfs/etc/shadow", workdir), shadow_txt);
Cmd cmd = {0};
cmd_append(&cmd, "chmod", "600", temp_sprintf("%s/rootfs/etc/shadow", workdir));
if (!cmd_run(&cmd)) return 1;
}
mkdir_if_not_exists(temp_sprintf("%s/rootfs/home/%s", workdir, USER_NAME));
write_text_file(temp_sprintf("%s/rootfs/root/.profile", workdir), "export PATH=/bin:/sbin:/usr/bin:/usr/sbin\n");
(void) copy_file(
temp_sprintf("%s/rootfs/root/.profile", workdir),
temp_sprintf("%s/rootfs/home/%s/.profile", workdir, USER_NAME)
);
write_text_file(temp_sprintf("%s/rootfs/etc/hostname", workdir), HOSTNAME);
if (!write_from_asset("assets/motd", temp_sprintf("%s/rootfs/etc/motd", workdir))) return 1;
{
Cmd cmd = {0};
cmd_append(&cmd, "ln", "-sf", "/sbin/sinit", temp_sprintf("%s/rootfs/sbin/init", workdir));
if (!cmd_run(&cmd)) return 1;
}
log(NOB_INFO, "[*] Generando initramfs...");
const char *initramfs_path = temp_sprintf("%s/initramfs.img", workdir);
{
Cmd cmd = {0};
const char *pipeline = temp_sprintf("cd %s/rootfs && find . | cpio -H newc -o | gzip -9", workdir);
cmd_append(&cmd, "sh", "-lc", pipeline);
if (!cmd_run(&cmd, .stdout_path = initramfs_path)) return 1;
}
log(NOB_INFO, "[*] Booteando en QEMU (Ctrl+A X para salir si usa nographic)...");
{
Cmd cmd = {0};
cmd_append(&cmd,
QEMU,
"-nodefaults", "-no-reboot", "-serial", "mon:stdio", "-nographic",
"-machine", "virt,accel=tcg",
"-cpu", "max",
"-smp", "2", "-m", "512M",
"-kernel", kernel_path,
"-initrd", initramfs_path,
"-append", "console=ttyAMA0,115200 earlyprintk rdinit=/etc/rc.init");
if (!cmd_run(&cmd)) return 1;
}
return 0;
}