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 print_uptime (void);
49 static void insmod (const char *filename);
50 static void show_directory (const char *dir);
52 static char line[1024];
58 fprintf (stderr, "febootstrap: ext2 mini initrd starting up\n");
60 /* Create some fixed directories. */
62 mkdir ("/root", 0755);
67 fprintf (stderr, "febootstrap: mounting /sys\n");
68 if (mount ("sysfs", "/sys", "sysfs", 0, "") == -1) {
69 perror ("mount: /sys");
73 FILE *fp = fopen ("/modules", "r");
75 perror ("fopen: /modules");
78 while (fgets (line, sizeof line, fp)) {
79 size_t n = strlen (line);
80 if (n > 0 && line[n-1] == '\n')
83 /* XXX Because of the way we construct the module list, the
84 * "modules" file can contain non-existent modules. Ignore those
85 * for now. Really we should add them as missing dependencies.
86 * See ext2initrd.c:ext2_make_initrd().
88 if (access (line, R_OK) == 0)
91 fprintf (stderr, "skipped %s, module is missing\n", line);
95 /* Look for the ext2 filesystem device. It's always the last
97 * XXX More than 25 devices?
99 char path[] = "/sys/block/xdx/dev";
100 char class[3] = { 'v', 's', 'h' };
103 for (i = 0; i < sizeof class; ++i) {
104 for (j = 'z'; j >= 'a'; --j) {
107 fp = fopen (path, "r");
113 "febootstrap: no ext2 root device found\n"
114 "Please include FULL verbose output in your bug report.\n");
119 fprintf (stderr, "febootstrap: picked %s as root device\n", path);
121 fgets (line, sizeof line, fp);
122 int major = atoi (line);
123 char *p = line + strcspn (line, ":") + 1;
124 int minor = atoi (p);
127 if (umount ("/sys") == -1) {
128 perror ("umount: /sys");
133 fprintf (stderr, "febootstrap: creating /dev/root as block special %d:%d\n",
136 if (mknod ("/dev/root", S_IFBLK|0700, makedev (major, minor)) == -1) {
137 perror ("mknod: /dev/root");
141 /* Mount new root and chroot to it. */
143 fprintf (stderr, "febootstrap: mounting new root on /root\n");
144 if (mount ("/dev/root", "/root", "ext2", MS_NOATIME, "") == -1) {
145 perror ("mount: /root");
149 /* Note that pivot_root won't work. See the note in
150 * Documentation/filesystems/ramfs-rootfs-initramfs.txt
151 * We could remove the old initramfs files, but let's not bother.
154 fprintf (stderr, "febootstrap: chroot\n");
156 if (chroot ("/root") == -1) {
157 perror ("chroot: /root");
163 /* Run /init from ext2 filesystem. */
165 execl ("/init", "init", NULL);
166 perror ("execl: /init");
168 /* /init failed to execute, but why? Before we ditch, print some
169 * debug. Although we have a full appliance, the fact that /init
170 * failed to run means we may not be able to run any commands.
172 show_directory ("/");
173 show_directory ("/bin");
174 show_directory ("/lib");
175 show_directory ("/lib64");
182 insmod (const char *filename)
185 fprintf (stderr, "febootstrap: internal insmod %s\n", filename);
187 int fd = open (filename, O_RDONLY);
189 fprintf (stderr, "insmod: open: %s: %m\n", filename);
193 if (fstat (fd, &st) == -1) {
194 perror ("insmod: fstat");
197 char buf[st.st_size];
200 long rc = read (fd, buf + offset, st.st_size - offset);
202 perror ("insmod: read");
206 } while (offset < st.st_size);
209 if (init_module (buf, st.st_size, "") != 0) {
210 fprintf (stderr, "insmod: init_module: %s: %m\n", filename);
211 /* However ignore the error because this can just happen because
212 * of a missing device.
217 /* Print contents of /proc/uptime. */
221 FILE *fp = fopen ("/proc/uptime", "r");
223 perror ("/proc/uptime");
227 fgets (line, sizeof line, fp);
230 fprintf (stderr, "febootstrap: uptime: %s", line);
233 /* Display a directory on stderr. This is used for debugging only. */
238 case DT_BLK: return 'b';
239 case DT_CHR: return 'c';
240 case DT_DIR: return 'd';
241 case DT_FIFO: return 'p';
242 case DT_LNK: return 'l';
243 case DT_REG: return '-';
244 case DT_SOCK: return 's';
245 case DT_UNKNOWN: return 'u';
251 show_directory (const char *dirname)
256 char link[PATH_MAX+1];
259 fprintf (stderr, "febootstrap: debug: listing directory %s\n", dirname);
261 if (chdir (dirname) == -1) {
273 while ((d = readdir (dir)) != NULL) {
274 fprintf (stderr, "%5d %c %-16s", d->d_ino, dirtype (d->d_type), d->d_name);
275 if (lstat (d->d_name, &statbuf) >= 0) {
276 fprintf (stderr, " %06o %d %d:%d",
277 statbuf.st_mode, statbuf.st_size,
278 statbuf.st_uid, statbuf.st_gid);
279 if (S_ISLNK (statbuf.st_mode)) {
280 n = readlink (d->d_name, link, PATH_MAX);
283 fprintf (stderr, " -> %s", link);
287 fprintf (stderr, "\n");