init: attempt to mount efivarfs
[project/procd.git] / initd / early.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #include <sys/mount.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/sysmacros.h>
19
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24
25 #include "../utils/utils.h"
26 #include "init.h"
27 #include "../libc-compat.h"
28 #include "../container.h"
29
30 static void
31 early_dev(void)
32 {
33 mkdev("*", 0600);
34 mknod("/dev/null", 0666, makedev(1, 3));
35 }
36
37 static void
38 early_console(const char *dev)
39 {
40 struct stat s;
41
42 if (stat(dev, &s)) {
43 ERROR("Failed to stat %s: %m\n", dev);
44 return;
45 }
46
47 if (patch_stdio(dev)) {
48 ERROR("Failed to setup i/o redirection\n");
49 return;
50 }
51
52 fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
53 }
54
55 static void
56 early_mounts(void)
57 {
58 unsigned int oldumask = umask(0);
59
60 if (!is_container()) {
61 mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL);
62 mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL);
63 mount("efivars", "/sys/firmware/efi/efivars", "efivarfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL);
64 mount("cgroup2", "/sys/fs/cgroup", "cgroup2", MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, "nsdelegate");
65 mount("tmpfs", "/dev", "tmpfs", MS_NOATIME | MS_NOEXEC | MS_NOSUID, "mode=0755,size=512K");
66 ignore(symlink("/tmp/shm", "/dev/shm"));
67 mkdir("/dev/pts", 0755);
68 mount("devpts", "/dev/pts", "devpts", MS_NOATIME | MS_NOEXEC | MS_NOSUID, NULL);
69
70 early_dev();
71 }
72
73 early_console("/dev/console");
74
75 mount("tmpfs", "/tmp", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOATIME, "mode=01777");
76 mkdir("/tmp/shm", 01777);
77
78 mkdir("/tmp/run", 0755);
79 mkdir("/tmp/lock", 0755);
80 mkdir("/tmp/state", 0755);
81 umask(oldumask);
82 }
83
84 static void
85 early_env(void)
86 {
87 setenv("PATH", EARLY_PATH, 1);
88 }
89
90 void
91 early(void)
92 {
93 if (getpid() != 1)
94 return;
95
96 early_mounts();
97 early_env();
98
99 LOG("Console is alive\n");
100 }