-
Notifications
You must be signed in to change notification settings - Fork 0
/
initrd-init.c
654 lines (608 loc) · 15.6 KB
/
initrd-init.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/* SPDX-License-Identifier: GPL-2.0-only
*
* Simplistic initramfs init
* Based on switch_root.c by Rob Landley.
*
* Copyright (c) 2020-2024 Vitaly Chikunov <[email protected]>
*/
#define _GNU_SOURCE
#include <blkid/blkid.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <glob.h>
#include <libgen.h>
#include <linux/limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/reboot.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/vfs.h>
#include <sys/wait.h>
#include <termios.h>
#include <unistd.h>
# define init_module(image, len, param) syscall(__NR_init_module, image, len, param)
static int debug = 0;
static char *rdshell = NULL;
static char *rdbreak = NULL;
static char *newroot = "/newroot";
static char *modules = "modules.conf";
static char *vm_init = "/usr/lib/vm-run/vm-init";
static char *get_option(const char *opt);
__attribute__ ((format (printf, 2, 3)))
static void warn(int err, const char *fmt, ...)
{
va_list args;
printf("init: ");
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
if (err)
printf(": %s\n", strerror(err));
else
printf("\n");
}
static void runner(char *pathname, char *script)
{
char *argv0 = pathname;
if (strstr(pathname, "box")) {
/* Perhaps we don't want to run raw toybox but a shell from it. */
argv0 = script ? "sh" : "-sh";
}
char * const args[] = { argv0, script, NULL };
if (debug)
warn(0, "try %s [%s]", pathname, argv0);
execv(pathname, args);
if (debug)
warn(errno, "execv '%s'", pathname);
}
#define SYS_PATH "/sbin:/usr/sbin:/bin:/usr/bin"
static void try_executable(char *binary, char *script)
{
if (strstr(binary, "/")) {
runner(binary, script);
return;
}
/* Also try to run from '/'. */
char *path = strdup(SYS_PATH ":/");
if (!path) {
warn(errno, "strdup");
return;
}
for (char *tok = strtok(path, ":"); tok; tok = strtok(NULL, ":")) {
char *where;
if (tok[0] == '/' && tok[1] == '\0')
*tok = '\0';
if (asprintf(&where, "%s/%s", tok, binary) != -1) {
if (access(where, X_OK) == 0)
runner(where, script);
free(where);
} else
warn(errno, "asprintf");
}
free(path);
if (debug)
warn(ENOENT, "exec '%s'", binary);
}
/* Quietly update loglevel=, but do not override what user requested. */
static void loglevel(const char *level)
{
const char *ll = get_option("loglevel");;
if (ll)
return;
const int fd = open("/proc/sys/kernel/printk", O_WRONLY);
if (fd == -1) {
warn(errno, "open printk");
return;
}
if (write(fd, level, strlen(level)) == -1)
warn(errno, "write printk");
close(fd);
}
static char *rdprompt = "rdshell# ";
static int exec_shell(char *script)
{
/* Make user slightly more happy by setting some env. */
setenv("PS1", rdprompt, 0);
setenv("PATH", SYS_PATH, 0);
struct termios ti;
if (tcgetattr(0, &ti) == 0) {
ti.c_cc[VERASE] = 8;
tcsetattr(0, TCSANOW, &ti);
}
loglevel("8");
char *tty = get_option("console"); /* Only a 1st. */
if (tty && *tty) {
if (*tty != '/' && asprintf(&tty, "/dev/%s", tty) == -1)
goto notty;
/* New session because SID 0 cannot acquire CTTY. */
setsid();
/* Automatically becomes CTTY. */
int fd = open(tty, O_RDWR);
if (fd != -1) {
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
if (fd > 2)
close(fd);
} else
warn(errno, "open '%s'", tty);
}
notty:
/* Don't run script directly (yet). */
if (!script)
try_executable(rdshell, NULL);
if (script || strcmp("sh", rdshell) == 0) {
try_executable("toybox", script);
try_executable("busybox", script);
}
return -1;
}
static int exec_rdshell()
{
if (!rdshell)
return 0;
warn(0, "launching rdshell (%s)", rdshell);
int ret = exec_shell(NULL);
warn(0, "rdshell failed");
return ret;
}
static void rdbreak_shell(const char *breakpoint)
{
if (!rdbreak)
return;
warn(0, "launching rdbreak shell (%s)", breakpoint);
pid_t pid = fork();
if (pid == -1) {
warn(errno, "fork");
} else if (pid) {
wait(NULL);
} else { /* child */
rdshell = "sh";
rdprompt = "rdbreak# ";
exec_shell(NULL);
warn(0, "rdbreak shell failed");
exit(127);
}
warn(0, "continue boot process");
}
static void exec_rdscript(char *script)
{
if (debug)
warn(0, "run rdscript=%s", script);
pid_t pid = fork();
if (pid == -1) {
warn(errno, "fork");
} else if (pid) {
int status;
wait(&status);
int ret = -1, sig;
if (WIFEXITED(status)) {
ret = WEXITSTATUS(status);
if (debug)
warn(0, "rdscript exited with status %d", ret);
} else if (WIFSIGNALED(status)) {
sig = WTERMSIG(status);
if (debug)
warn(0, "rdscript terminated by signal %d", sig);
ret = 128 + sig;
}
char *dev_ec = getenv("exitcode");
if (dev_ec) {
FILE *fd = fopen(dev_ec, "w");
if (fd) {
fprintf(fd, "%d\n", ret);
fflush(fd);
fdatasync(fileno(fd));
fclose(fd);
if (debug)
warn(0, "wrote exitcode=%d into %s", ret, dev_ec);
} else
warn(errno, "open %s", dev_ec);
} else
warn(0, "Exit code %d is lost", ret);
} else {
exec_shell(script);
exit(127);
}
exec_rdshell();
}
static unsigned int term_sleep = 1;
static void terminate()
{
sleep(term_sleep);
if (reboot(RB_POWER_OFF) == -1)
warn(errno, "reboot");
exit(1);
}
__attribute__ ((format (printf, 2, 3)))
static void xerrno(int err, const char *fmt, ...)
{
va_list args;
printf("init: ");
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
if (err)
printf(": %s\n", strerror(err));
else
printf("\n");
exec_rdshell();
terminate();
}
static int _modprobe(void)
{
int loaded = 0, failed = 0;
FILE *fd = fopen(modules, "r");
if (!fd) {
warn(errno, "open '%s'", modules);
return 0;
}
char buf[256];
while (fgets(buf, sizeof(buf), fd) && buf[0]) {
buf[strlen(buf) - 1] = '\0';
int f = open(buf, O_RDONLY | O_CLOEXEC);
if (f < 0) {
warn(errno, "open '%s'", buf);
continue;
}
struct stat st;
if (fstat(f, &st))
xerrno(errno, "fstat '%s'", buf);
char *image = malloc(st.st_size);
if (!image)
xerrno(errno, "malloc %ld bytes", st.st_size);
if (read(f, image, st.st_size) != st.st_size)
xerrno(errno, "read %ld bytes from '%s'", st.st_size, buf);
close(f);
if (debug)
warn(0, "insmod '%s'", buf);
int r = init_module(image, st.st_size, "");
if (!r) {
loaded++;
} else {
int printerr = 0;
if (errno != EEXIST && !(failed && errno == ENOENT)) {
failed++;
printerr++;
}
if (printerr || debug)
warn(errno, "init_module '%s'", buf);
}
free(image);
}
fclose(fd);
return failed ? loaded : 0;
}
static void modprobe(void)
{
/* Load while it loads, to workaround intermittent load ordering failures. */
while (_modprobe())
warn(0, "Retry modules loading.");
}
#define COMMAND_LINE_SIZE 2048
static char cmdline[COMMAND_LINE_SIZE];
void get_cmdline(void)
{
const char *proc = "/proc";
if (mkdir(proc, 0755))
xerrno(errno, "mkdir '%s'", proc);
if (debug)
warn(0, "mount proc to %s", proc);
if (mount("proc", proc, "proc", 0, NULL))
xerrno(errno, "mount '%s'", proc);
const char *proc_cmdline = "/proc/cmdline";
FILE *fd = fopen(proc_cmdline, "r");
if (!fd)
xerrno(errno, "open '%s'", proc_cmdline);
if (!fgets(cmdline, sizeof(cmdline), fd))
xerrno(errno, "read error '%s'", proc_cmdline);
if (fclose(fd) == EOF)
warn(errno, "fclose '%s'", proc_cmdline);
}
/* Get value of cmdline option. Options w/o value are skipped. */
static char *get_option(const char *opt)
{
char *p = cmdline;
size_t optlen = strlen(opt);
while (*p) {
while (isspace(*p))
p++;
if (!*p)
break;
char *o = p; // start of option
while (*p && !isspace(*p) && *p != '=')
p++;
int match = 0;
if ((p - o) == optlen && !strncmp(opt, o, p - o))
match++;
if (!*p || isspace(*p))
continue;
o = ++p; // start of value
if (*p == '"') {
o = ++p; // skip opening quote
while (*p && *p != '"')
p++;
if (match)
return strndup(o, p - o);
if (*p)
p++; // skip closing quote
} else {
while (*p && !isspace(*p))
p++;
if (match)
return strndup(o, p - o);
}
}
return NULL;
}
static int sysfs_mounted;
static void mount_sys(void)
{
if (sysfs_mounted)
return;
const char *sys = "/sys";
if (mkdir(sys, 0755))
xerrno(errno, "mkdir '%s'", sys);
if (debug)
warn(0, "mount sysfs to %s", sys);
if (mount("sysfs", sys, "sysfs", 0, NULL))
warn(errno, "mount '%s'", sys);
else
sysfs_mounted++;
}
// source name from qemu -device ..,mount_tag=
static char *find_mount_tag(void)
{
mount_sys();
glob_t globbuf;
int n = glob("/sys/bus/virtio/drivers/9pnet_virtio/virtio*/mount_tag",
GLOB_NOSORT, NULL, &globbuf);
if (n || globbuf.gl_pathc < 1) {
warn(errno, "glob: 9p mount_tag not found (ret: %d)", n);
return NULL;
}
const char *mount_tag = globbuf.gl_pathv[0];
FILE *fd = fopen(mount_tag, "r");
if (!fd) {
warn(errno, "open '%s'", mount_tag);
return NULL;
}
static char buf[128];
if (!fread(buf, 1, sizeof(buf), fd))
warn(errno, "read '%s'", mount_tag);
if (fclose(fd) == EOF)
warn(errno, "fclose '%s'", mount_tag);
return buf[0] ? buf : NULL;
}
/* Read a short line without EOL (suitable for sysfs data). */
static char *readln(char *path)
{
static char buf[NAME_MAX];
FILE *fd = fopen(path, "r");
if (!fd) {
warn(errno, "open '%s'", path);
return NULL;
}
char *ptr = NULL;
if (fgets(buf, sizeof(buf), fd) && buf[0]) {
size_t len = strlen(buf);
if (buf[len - 1] == '\n')
buf[len - 1] = '\0';
ptr = buf;
}
fclose(fd);
return ptr;
}
static void sync_vports(const char *rdscript)
{
mount_sys();
glob_t globbuf;
int n;
/*
* Should we wait for vports to appear or not?
*
* If in `/sys/bus/virtio/devices` appears a directory
* `virtio<n>` then `-device virtio-serial` _maybe_ passed to QEMU.
* We don't need to know for sure because of next condition. Then inside of the dir:
* If `driver` is a symlink to `../../../../bus/virtio/drivers/virtio_console`
* then `virtio-console` kernel module is loaded.
* If there is `virtio-ports` dir then `-device virtserialport`
* (or `-device virtioconsole` which is useless for us) is passed to QEMU. The dir
* appears very early.
* After this the module may take quite some time to negotiate vports with QEMU.
* Here we could wait for 'vport*' devices to appear
* in `/sys/class/virtio-ports` first (we need it to find a `name`)
* and in `/dev/vport*` after that. Then don't appear at once.
*/
if ((n = glob("/sys/bus/virtio/devices/virtio*/driver", GLOB_NOSORT, NULL, &globbuf))) {
if (debug)
warn(errno, "glob: no virtio device drivers (ret: %d)", n);
return;
}
int virtio_console = 0;
int i;
for (i = 0; i < globbuf.gl_pathc; i ++) {
char link[PATH_MAX];
char *path = globbuf.gl_pathv[i];
if (readlink(path, link, sizeof(link)) == -1)
continue;
if (!(path = basename(link)))
continue;
if (strcmp("virtio_console", path) == 0) {
virtio_console++;
break;
}
}
if (!virtio_console) {
if (debug)
warn(errno, "no virtio_console device driver (%d)", i);
return;
}
/* Preconditions succeeded. Now give them 0.1 second to appear, or if we going to call rdscript
* then give them generous 1 second, because we almost sure it should be there. */
char dev_vport[32] = {};
const int try_max = rdscript ? 1000 : 100;
for (int try = 0; try < try_max; try++) {
if (glob("/sys/class/virtio-ports/*/name", GLOB_NOSORT, NULL, &globbuf))
goto retry;
for (i = 0; i < globbuf.gl_pathc; i++) {
char *path = globbuf.gl_pathv[i];
char *tag;
if (!(tag = readln(path)) ||
!(path = dirname(path)) ||
!(path = basename(path)))
continue;
/*
* Unfortunately, we cannot distinguish virtserialport from virtioconsole.
* Latter vport if not accessible, giving ENXIO, instead we should have
* been able to access hvc, but we don't know its name (or number).
*/
snprintf(dev_vport, sizeof(dev_vport), "/dev/%s", path);
setenv(tag, dev_vport, 0);
if (strcmp("exitcode", tag) == 0) {
if (debug)
warn(0, "found %s virtio-port: %s (iter=%d)", tag, path, try);
try = try_max;
}
}
globfree(&globbuf);
retry:
usleep(1000);
}
if (!*dev_vport) {
if (debug)
warn(0, "no vports appeared (iter=%d)", try_max);
return;
}
/* Now wait for the actual device, which can appear even later. */
for (int try = 0; try < try_max; try++) {
if (!access(dev_vport, F_OK)) {
if (debug)
warn(0, "found %s device (iter=%d)", dev_vport, try);
break;
}
usleep(1000);
}
}
static void mount_devtmpfs()
{
const char *dev = "dev";
if (mkdir(dev, 0755) && errno != EEXIST)
xerrno(errno, "mkdir '%s'", dev);
if (debug)
warn(0, "mount devtmpfs to %s", dev);
if (mount("devtmpfs", dev, "devtmpfs", 0, NULL))
xerrno(errno, "mount %s", dev);
/* Will not be able to umount it, but it will disappear after
* `mount --move . /` on its own. */
}
int main(int argc, char **argv)
{
/* We want to enable debug options as early as possible. */
rdshell = getenv("rdshell");
rdbreak = getenv("rdbreak");
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "rddebug") == 0)
debug++;
else if (strcmp(argv[i], "rdshell") == 0)
rdshell = "sh";
}
if (debug)
warn(0, "vm-run initrd%s%s",
rdshell ? " [rdshell]" : "",
rdbreak ? " [rdbreak]" : "");
/* poweroff is not always installed. */
if (argc > 0 && !strcmp(argv[0], "poweroff"))
reboot(RB_POWER_OFF);
if (getpid() != 1)
xerrno(0, "not pid 1");
struct statfs stfs;
if (statfs("/", &stfs) ||
(stfs.f_type != 0x01021994 && stfs.f_type != 0x858458f6))
xerrno(0, "root is not tmpfs or ramfs");
get_cmdline();
if (debug)
loglevel("8");
modprobe();
mount_devtmpfs();
char *rdscript = getenv("RDSCRIPT");
sync_vports(rdscript);
if (rdscript) {
rdbreak_shell("pre-script");
/* RDSCRIPT is only supposed to run in initrd env. */
exec_rdscript(rdscript);
term_sleep = 0;
terminate();
}
if (mkdir(newroot, 0755))
xerrno(errno, "mkdir '%s'", newroot);
const char *root = get_option("root");
const char *rootfstype = get_option("rootfstype");
const char *rootflags = get_option("rootflags");
if (root) {
if (strncmp(root, "/dev/", 5)) {
mount_sys();
/* Tag formats are in findfs(8). */
char *dev = blkid_evaluate_tag(root, NULL, NULL);
if (!dev) {
/* There are some output from resolver, like
* 'Can't open blockdev', which interferes with
* this message on console. */
warn(0, "unable to resolve '%s'", root);
} else
root = dev;
}
if (!rootfstype) {
blkid_probe pr = blkid_new_probe_from_filename(root);
if (pr && !blkid_do_fullprobe(pr)) {
const char *type;
if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL))
rootfstype = type;
}
}
} else {
char *mount_tag = find_mount_tag();
if (mount_tag) {
rootflags = "version=9p2000.L,trans=virtio,access=any,msize=262144";
rootfstype = "9p";
root = mount_tag;
} else
xerrno(0, "rootfs not found.");
}
if (debug)
warn(0, "mount %s from %s to %s flags '%s'", rootfstype, root, newroot,
rootflags ?: "");
if (mount(root, newroot, rootfstype, 0, rootflags))
xerrno(errno, "mount root=%s (type=%s flags=%s)", root, rootfstype, rootflags);
if (rdbreak)
rdbreak_shell("pre-pivot");
else if (exec_rdshell())
terminate();
if (debug)
warn(0, "switch root");
struct stat st1, st2;
if (chdir(newroot) ||
stat(".", &st1) || stat("/", &st2) ||
st1.st_dev == st2.st_dev)
xerrno(0, "bad newroot");
if (mount(".", "/", NULL, MS_MOVE, NULL))
xerrno(errno, "mount --move");
if (chroot("."))
xerrno(errno, "chroot");
if (chdir("/"))
xerrno(errno, "chdir");
/* Allow to run user specified init=, perhaps for experiments. */
char *init = get_option("init");
if (!init)
init = vm_init;
if (debug)
warn(0, "exec '%s'", init);
char * const args[] = { init, NULL };
execv(init, args);
xerrno(errno, "execv '%s'", init);
}