2 * Copyright (c) 2022, Netflix, Inc.
4 * SPDX-License-Identifier: BSD-2-Clause
8 * Mini-init(8) so we can run as init/pid 1 in a LinuxBoot environment.
12 #include "host_syscall.h"
16 * Create a 'standard' early boot environment. Cribbed from the things that
17 * sysvinit, u-root, and initramfs-tools do. This is a minimal environment
18 * for modern Linux systems, though the /tmp, /run and /var stuff can likely
19 * be done inside the initrd image itself (as can creating the mount points
20 * for /proc, /dev and /sys).
22 * Note: We ignore errors here. There's no stderr to report them to yet. These
23 * operations generally can't fail, but if they do, we may not have the ability
24 * to report them later.
30 * Create directories for mandatory filesystems and mount them.
32 host_mkdir("/proc", 0555);
33 host_mount("proc", "/proc", "proc", MS_RELATIME, "");
34 host_mkdir("/sys", 0555);
35 host_mount("sysfs", "/sys", "sysfs", MS_RELATIME, "");
36 host_mkdir("/dev", 0755);
37 host_mount("devtmpfs", "/dev", "devtmpfs", MS_RELATIME,
38 "mode=0755,nr_inodes=0");
41 * Create compat links: /dev/fd lives in /proc, and needs some help to
44 host_symlink("/proc/self/fd", "/dev/fd");
45 host_symlink("fd/0", "/dev/stdin");
46 host_symlink("fd/1", "/dev/stdout");
47 host_symlink("fd/2", "/dev/stderr");
51 * Unsure if we need this, but create a sane /tmp just in case that's useful.
52 * and point /run over to it.
54 host_mkdir("/tmp", 01777);
55 host_mount("tmpfs", "/tmp", "tmpfs", MS_RELATIME, "size=10%,mode=1777");
56 host_symlink("/tmp", "/run");
59 * Unsure the loader needs /var and /var/log, but they are easy to
62 host_mkdir("/var", 0555);
63 host_mkdir("/var/lock", 0555);
64 host_symlink("/tmp", "/var/tmp");
73 * sysvinit asks the linux kernel to convert the CTRL-ALT-DEL to a SIGINT,
78 * Setup /dev/console as stdin/out/err
83 fd = host_open("/dev/console", HOST_O_RDWR | HOST_O_NOCTTY, 0);
88 * I think we may need to put it in 'raw' mode, but maybe not. Linux
89 * sysvinit sets it into 'sane' mode with several tweaks. Not enabled at
90 * the moment since host console initialization seems sufficient.
92 struct host_termios tty;
95 host_tcsetattr(fd, HOST_TCANOW, &tty);
96 host_tcflush(fd, HOST_TCIOFLUSH)
104 * since we're running as init, we need to catch some signals
110 * sysvinit catches a lot of signals, but the boot loader needn't catch
111 * so many since we don't do as much as it does. If we need to, put the
112 * signal catching / ignoring code here. If we implement a 'shell'
113 * function to spawn a sub-shell, we'll likely need to do a lot more.
123 if (host_getpid() != 1)