1 /* febootstrap-supermin-helper reimplementation in C.
2 * Copyright (C) 2009-2010 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* This very minimal init "script" goes in the mini-initrd used to
20 * boot the ext2-based appliance. Note we have no shell, so we cannot
21 * use system(3) to run external commands. In fact, we don't have
22 * very much at all, except this program, and some kernel modules.
34 #include <sys/types.h>
35 #include <sys/mount.h>
39 #include <asm/unistd.h>
41 extern long init_module (void *, unsigned long, const char *);
43 /* Leave this enabled for now. When we get more confident in the boot
44 * process we can turn this off or make it configurable.
48 static void mount_proc (void);
49 static void print_uptime (void);
50 static void insmod (const char *filename);
51 static void show_directory (const char *dir);
53 static char line[1024];
61 fprintf (stderr, "febootstrap: ext2 mini initrd starting up\n");
63 /* Create some fixed directories. */
65 mkdir ("/root", 0755);
70 fprintf (stderr, "febootstrap: mounting /sys\n");
71 if (mount ("sysfs", "/sys", "sysfs", 0, "") == -1) {
72 perror ("mount: /sys");
76 FILE *fp = fopen ("/modules", "r");
78 perror ("fopen: /modules");
81 while (fgets (line, sizeof line, fp)) {
82 size_t n = strlen (line);
83 if (n > 0 && line[n-1] == '\n')
86 /* XXX Because of the way we construct the module list, the
87 * "modules" file can contain non-existent modules. Ignore those
88 * for now. Really we should add them as missing dependencies.
89 * See ext2initrd.c:ext2_make_initrd().
91 if (access (line, R_OK) == 0)
94 fprintf (stderr, "skipped %s, module is missing\n", line);
98 /* Look for the ext2 filesystem device. It's always the last
100 * XXX More than 25 devices?
102 char path[] = "/sys/block/xdx/dev";
103 char class[3] = { 'v', 's', 'h' };
106 for (i = 0; i < sizeof class; ++i) {
107 for (j = 'z'; j >= 'a'; --j) {
110 fp = fopen (path, "r");
116 "febootstrap: no ext2 root device found\n"
117 "Please include FULL verbose output in your bug report.\n");
122 fprintf (stderr, "febootstrap: picked %s as root device\n", path);
124 fgets (line, sizeof line, fp);
125 int major = atoi (line);
126 char *p = line + strcspn (line, ":") + 1;
127 int minor = atoi (p);
130 if (umount ("/sys") == -1) {
131 perror ("umount: /sys");
136 fprintf (stderr, "febootstrap: creating /dev/root as block special %d:%d\n",
139 if (mknod ("/dev/root", S_IFBLK|0700, makedev (major, minor)) == -1) {
140 perror ("mknod: /dev/root");
144 /* Mount new root and chroot to it. */
146 fprintf (stderr, "febootstrap: mounting new root on /root\n");
147 if (mount ("/dev/root", "/root", "ext2", MS_NOATIME, "") == -1) {
148 perror ("mount: /root");
152 /* Note that pivot_root won't work. See the note in
153 * Documentation/filesystems/ramfs-rootfs-initramfs.txt
154 * We could remove the old initramfs files, but let's not bother.
157 fprintf (stderr, "febootstrap: chroot\n");
159 if (chroot ("/root") == -1) {
160 perror ("chroot: /root");
166 /* Run /init from ext2 filesystem. */
168 execl ("/init", "init", NULL);
169 perror ("execl: /init");
171 /* /init failed to execute, but why? Before we ditch, print some
172 * debug. Although we have a full appliance, the fact that /init
173 * failed to run means we may not be able to run any commands.
175 show_directory ("/");
176 show_directory ("/bin");
177 show_directory ("/lib");
178 show_directory ("/lib64");
185 insmod (const char *filename)
188 fprintf (stderr, "febootstrap: internal insmod %s\n", filename);
190 int fd = open (filename, O_RDONLY);
192 fprintf (stderr, "insmod: open: %s: %m\n", filename);
196 if (fstat (fd, &st) == -1) {
197 perror ("insmod: fstat");
200 char buf[st.st_size];
203 long rc = read (fd, buf + offset, st.st_size - offset);
205 perror ("insmod: read");
209 } while (offset < st.st_size);
212 if (init_module (buf, st.st_size, "") != 0) {
213 fprintf (stderr, "insmod: init_module: %s: %m\n", filename);
214 /* However ignore the error because this can just happen because
215 * of a missing device.
220 /* Mount /proc unless it's mounted already. */
224 if (access ("/proc/uptime", R_OK) == -1) {
225 mkdir ("/proc", 0755);
228 fprintf (stderr, "febootstrap: mounting /proc\n");
230 if (mount ("proc", "/proc", "proc", 0, "") == -1) {
231 perror ("mount: /proc");
237 /* Print contents of /proc/uptime. */
241 FILE *fp = fopen ("/proc/uptime", "r");
243 perror ("/proc/uptime");
247 fgets (line, sizeof line, fp);
250 fprintf (stderr, "febootstrap: uptime: %s", line);
253 /* Display a directory on stderr. This is used for debugging only. */
258 case DT_BLK: return 'b';
259 case DT_CHR: return 'c';
260 case DT_DIR: return 'd';
261 case DT_FIFO: return 'p';
262 case DT_LNK: return 'l';
263 case DT_REG: return '-';
264 case DT_SOCK: return 's';
265 case DT_UNKNOWN: return 'u';
271 show_directory (const char *dirname)
276 char link[PATH_MAX+1];
279 fprintf (stderr, "febootstrap: debug: listing directory %s\n", dirname);
281 if (chdir (dirname) == -1) {
293 while ((d = readdir (dir)) != NULL) {
294 fprintf (stderr, "%5d %c %-16s", d->d_ino, dirtype (d->d_type), d->d_name);
295 if (lstat (d->d_name, &statbuf) >= 0) {
296 fprintf (stderr, " %06o %d %d:%d",
297 statbuf.st_mode, statbuf.st_size,
298 statbuf.st_uid, statbuf.st_gid);
299 if (S_ISLNK (statbuf.st_mode)) {
300 n = readlink (d->d_name, link, PATH_MAX);
303 fprintf (stderr, " -> %s", link);
307 fprintf (stderr, "\n");