1 /* febootstrap-supermin-helper reimplementation in C.
2 * Copyright (C) 2009-2011 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>
45 extern long init_module (void *, unsigned long, const char *);
47 /* translation taken from module-init-tools/insmod.c */
48 static const char *moderror(int err)
52 return "Invalid module format";
54 return "Unknown symbol in module";
56 return "Module has wrong symbol version";
58 return "Invalid parameters";
64 /* Leave this enabled for now. When we get more confident in the boot
65 * process we can turn this off or make it configurable.
69 static void mount_proc (void);
70 static void print_uptime (void);
71 static void insmod (const char *filename);
72 static void show_directory (const char *dir);
74 static char line[1024];
82 fprintf (stderr, "febootstrap: ext2 mini initrd starting up: "
89 /* Create some fixed directories. */
91 mkdir ("/root", 0755);
96 fprintf (stderr, "febootstrap: mounting /sys\n");
97 if (mount ("sysfs", "/sys", "sysfs", 0, "") == -1) {
98 perror ("mount: /sys");
102 FILE *fp = fopen ("/modules", "r");
104 perror ("fopen: /modules");
107 while (fgets (line, sizeof line, fp)) {
108 size_t n = strlen (line);
109 if (n > 0 && line[n-1] == '\n')
112 /* XXX Because of the way we construct the module list, the
113 * "modules" file can contain non-existent modules. Ignore those
114 * for now. Really we should add them as missing dependencies.
115 * See ext2initrd.c:ext2_make_initrd().
117 if (access (line, R_OK) == 0)
120 fprintf (stderr, "skipped %s, module is missing\n", line);
124 /* Look for the ext2 filesystem device. It's always the last
125 * one that was added.
126 * XXX More than 25 devices?
128 char path[] = "/sys/block/xdx/dev";
129 char class[3] = { 'v', 's', 'h' };
132 for (i = 0; i < sizeof class; ++i) {
133 for (j = 'z'; j >= 'a'; --j) {
136 fp = fopen (path, "r");
142 "febootstrap: no ext2 root device found\n"
143 "Please include FULL verbose output in your bug report.\n");
148 fprintf (stderr, "febootstrap: picked %s as root device\n", path);
150 fgets (line, sizeof line, fp);
151 int major = atoi (line);
152 char *p = line + strcspn (line, ":") + 1;
153 int minor = atoi (p);
156 if (umount ("/sys") == -1) {
157 perror ("umount: /sys");
162 fprintf (stderr, "febootstrap: creating /dev/root as block special %d:%d\n",
165 if (mknod ("/dev/root", S_IFBLK|0700, makedev (major, minor)) == -1) {
166 perror ("mknod: /dev/root");
170 /* Mount new root and chroot to it. */
172 fprintf (stderr, "febootstrap: mounting new root on /root\n");
173 if (mount ("/dev/root", "/root", "ext2", MS_NOATIME, "") == -1) {
174 perror ("mount: /root");
178 /* Note that pivot_root won't work. See the note in
179 * Documentation/filesystems/ramfs-rootfs-initramfs.txt
180 * We could remove the old initramfs files, but let's not bother.
183 fprintf (stderr, "febootstrap: chroot\n");
185 if (chroot ("/root") == -1) {
186 perror ("chroot: /root");
192 /* Run /init from ext2 filesystem. */
194 execl ("/init", "init", NULL);
195 perror ("execl: /init");
197 /* /init failed to execute, but why? Before we ditch, print some
198 * debug. Although we have a full appliance, the fact that /init
199 * failed to run means we may not be able to run any commands.
201 show_directory ("/");
202 show_directory ("/bin");
203 show_directory ("/lib");
204 show_directory ("/lib64");
211 insmod (const char *filename)
216 fprintf (stderr, "febootstrap: internal insmod %s\n", filename);
219 gzFile gzfp = gzopen (filename, "rb");
220 int capacity = 64*1024;
221 char *buf = (char *) malloc (capacity);
222 int tmpsize = 8 * 1024;
229 fprintf (stderr, "insmod: gzopen failed: %s", filename);
232 while ((num = gzread (gzfp, tmp, tmpsize)) > 0) {
233 if (num > capacity) {
234 buf = (char*) realloc (buf, size*2);
237 memcpy (buf+size, tmp, num);
242 perror ("insmod: gzread");
247 int fd = open (filename, O_RDONLY);
249 fprintf (stderr, "insmod: open: %s: %m\n", filename);
253 if (fstat (fd, &st) == -1) {
254 perror ("insmod: fstat");
261 ssize_t rc = read (fd, buf + offset, size - offset);
263 perror ("insmod: read");
267 } while (offset < size);
271 if (init_module (buf, size, "") != 0) {
272 fprintf (stderr, "insmod: init_module: %s: %s\n", filename, moderror (errno));
273 /* However ignore the error because this can just happen because
274 * of a missing device.
283 /* Mount /proc unless it's mounted already. */
287 if (access ("/proc/uptime", R_OK) == -1) {
288 mkdir ("/proc", 0755);
291 fprintf (stderr, "febootstrap: mounting /proc\n");
293 if (mount ("proc", "/proc", "proc", 0, "") == -1) {
294 perror ("mount: /proc");
300 /* Print contents of /proc/uptime. */
304 FILE *fp = fopen ("/proc/uptime", "r");
306 perror ("/proc/uptime");
310 fgets (line, sizeof line, fp);
313 fprintf (stderr, "febootstrap: uptime: %s", line);
316 /* Display a directory on stderr. This is used for debugging only. */
321 case DT_BLK: return 'b';
322 case DT_CHR: return 'c';
323 case DT_DIR: return 'd';
324 case DT_FIFO: return 'p';
325 case DT_LNK: return 'l';
326 case DT_REG: return '-';
327 case DT_SOCK: return 's';
328 case DT_UNKNOWN: return 'u';
334 show_directory (const char *dirname)
339 char link[PATH_MAX+1];
342 fprintf (stderr, "febootstrap: debug: listing directory %s\n", dirname);
344 if (chdir (dirname) == -1) {
356 while ((d = readdir (dir)) != NULL) {
357 fprintf (stderr, "%5lu %c %-16s", d->d_ino, dirtype (d->d_type), d->d_name);
358 if (lstat (d->d_name, &statbuf) >= 0) {
359 fprintf (stderr, " %06o %ld %d:%d",
360 statbuf.st_mode, statbuf.st_size,
361 statbuf.st_uid, statbuf.st_gid);
362 if (S_ISLNK (statbuf.st_mode)) {
363 n = readlink (d->d_name, link, PATH_MAX);
366 fprintf (stderr, " -> %s", link);
370 fprintf (stderr, "\n");