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, insmod.static, and some
34 #include <sys/types.h>
35 #include <sys/mount.h>
39 /* Leave this enabled for now. When we get more confident in the boot
40 * process we can turn this off or make it configurable.
44 static void print_uptime (void);
45 static void insmod (const char *filename);
46 static void show_directory (const char *dir);
48 static char line[1024];
54 fprintf (stderr, "febootstrap: ext2 mini initrd starting up\n");
56 /* Create some fixed directories. */
58 mkdir ("/root", 0755);
63 fprintf (stderr, "febootstrap: mounting /sys\n");
64 if (mount ("sysfs", "/sys", "sysfs", 0, "") == -1) {
65 perror ("mount: /sys");
69 /* A perennial problem is that /sbin/insmod.static is not
70 * executable. Just make it executable. It's easier than fixing
73 chmod ("/sbin/insmod.static", 0755);
75 FILE *fp = fopen ("/modules", "r");
77 perror ("fopen: /modules");
80 while (fgets (line, sizeof line, fp)) {
81 size_t n = strlen (line);
82 if (n > 0 && line[n-1] == '\n')
88 /* Look for the ext2 filesystem device. It's always the last
90 * XXX More than 25 devices?
92 char path[] = "/sys/block/xdx/dev";
93 char class[3] = { 'v', 's', 'h' };
96 for (i = 0; i < sizeof class; ++i) {
97 for (j = 'z'; j >= 'a'; --j) {
100 fp = fopen (path, "r");
106 "febootstrap: no ext2 root device found\n"
107 "Please include FULL verbose output in your bug report.\n");
112 fprintf (stderr, "febootstrap: picked %s as root device\n", path);
114 fgets (line, sizeof line, fp);
115 int major = atoi (line);
116 char *p = line + strcspn (line, ":") + 1;
117 int minor = atoi (p);
120 if (umount ("/sys") == -1) {
121 perror ("umount: /sys");
126 fprintf (stderr, "febootstrap: creating /dev/root as block special %d:%d\n",
129 if (mknod ("/dev/root", S_IFBLK|0700, makedev (major, minor)) == -1) {
130 perror ("mknod: /dev/root");
134 /* Mount new root and chroot to it. */
136 fprintf (stderr, "febootstrap: mounting new root on /root\n");
137 if (mount ("/dev/root", "/root", "ext2", MS_NOATIME, "") == -1) {
138 perror ("mount: /root");
142 /* Note that pivot_root won't work. See the note in
143 * Documentation/filesystems/ramfs-rootfs-initramfs.txt
144 * We could remove the old initramfs files, but let's not bother.
147 fprintf (stderr, "febootstrap: chroot\n");
149 if (chroot ("/root") == -1) {
150 perror ("chroot: /root");
156 /* Run /init from ext2 filesystem. */
158 execl ("/init", "init", NULL);
159 perror ("execl: /init");
161 /* /init failed to execute, but why? Before we ditch, print some
162 * debug. Although we have a full appliance, the fact that /init
163 * failed to run means we may not be able to run any commands.
165 show_directory ("/");
166 show_directory ("/bin");
167 show_directory ("/lib");
168 show_directory ("/lib64");
175 insmod (const char *filename)
178 fprintf (stderr, "febootstrap: insmod %s\n", filename);
182 perror ("insmod: fork");
186 if (pid == 0) { /* Child. */
187 execl ("/insmod.static", "insmod.static", filename, NULL);
188 perror ("insmod: execl");
189 _exit (EXIT_FAILURE);
194 if (wait (&status) == -1 ||
195 WEXITSTATUS (status) != 0)
196 perror ("insmod: wait");
197 /* but ignore the error, some will be because the device is not found */
200 /* Print contents of /proc/uptime. */
204 FILE *fp = fopen ("/proc/uptime", "r");
206 perror ("/proc/uptime");
210 fgets (line, sizeof line, fp);
213 fprintf (stderr, "febootstrap: uptime: %s", line);
216 /* Display a directory on stderr. This is used for debugging only. */
221 case DT_BLK: return 'b';
222 case DT_CHR: return 'c';
223 case DT_DIR: return 'd';
224 case DT_FIFO: return 'p';
225 case DT_LNK: return 'l';
226 case DT_REG: return '-';
227 case DT_SOCK: return 's';
228 case DT_UNKNOWN: return 'u';
234 show_directory (const char *dirname)
239 char link[PATH_MAX+1];
242 fprintf (stderr, "febootstrap: debug: listing directory %s\n", dirname);
244 if (chdir (dirname) == -1) {
256 while ((d = readdir (dir)) != NULL) {
257 fprintf (stderr, "%5d %c %-16s", d->d_ino, dirtype (d->d_type), d->d_name);
258 if (lstat (d->d_name, &statbuf) >= 0) {
259 fprintf (stderr, " %06o %d %d:%d",
260 statbuf.st_mode, statbuf.st_size,
261 statbuf.st_uid, statbuf.st_gid);
262 if (S_ISLNK (statbuf.st_mode)) {
263 n = readlink (d->d_name, link, PATH_MAX);
266 fprintf (stderr, " -> %s", link);
270 fprintf (stderr, "\n");