Find package dependencies fix for pacman.
[febootstrap.git] / helper / init.c
1 /* febootstrap-supermin-helper reimplementation in C.
2  * Copyright (C) 2009-2010 Red Hat Inc.
3  *
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.
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  * 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.
17  */
18
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.
23  */
24
25 #include <config.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <dirent.h>
34 #include <sys/types.h>
35 #include <sys/mount.h>
36 #include <sys/stat.h>
37 #include <sys/wait.h>
38
39 #include <asm/unistd.h>
40
41 #ifdef HAVE_LIBZ
42 #include <zlib.h>
43 #endif
44
45 extern long init_module (void *, unsigned long, const char *);
46
47 /* translation taken from module-init-tools/insmod.c  */
48 static const char *moderror(int err)
49 {
50   switch (err) {
51   case ENOEXEC:
52     return "Invalid module format";
53   case ENOENT:
54     return "Unknown symbol in module";
55   case ESRCH:
56     return "Module has wrong symbol version";
57   case EINVAL:
58     return "Invalid parameters";
59   default:
60     return strerror(err);
61   }
62 }
63
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.
66  */
67 #define verbose 1
68
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);
73
74 static char line[1024];
75
76 int
77 main ()
78 {
79   mount_proc ();
80
81   print_uptime ();
82   fprintf (stderr, "febootstrap: ext2 mini initrd starting up\n");
83
84   /* Create some fixed directories. */
85   mkdir ("/dev", 0755);
86   mkdir ("/root", 0755);
87   mkdir ("/sys", 0755);
88
89   /* Mount /sys. */
90   if (verbose)
91     fprintf (stderr, "febootstrap: mounting /sys\n");
92   if (mount ("sysfs", "/sys", "sysfs", 0, "") == -1) {
93     perror ("mount: /sys");
94     exit (EXIT_FAILURE);
95   }
96
97   FILE *fp = fopen ("/modules", "r");
98   if (fp == NULL) {
99     perror ("fopen: /modules");
100     exit (EXIT_FAILURE);
101   }
102   while (fgets (line, sizeof line, fp)) {
103     size_t n = strlen (line);
104     if (n > 0 && line[n-1] == '\n')
105       line[--n] = '\0';
106
107     /* XXX Because of the way we construct the module list, the
108      * "modules" file can contain non-existent modules.  Ignore those
109      * for now.  Really we should add them as missing dependencies.
110      * See ext2initrd.c:ext2_make_initrd().
111      */
112     if (access (line, R_OK) == 0)
113       insmod (line);
114     else
115       fprintf (stderr, "skipped %s, module is missing\n", line);
116   }
117   fclose (fp);
118
119   /* Look for the ext2 filesystem device.  It's always the last
120    * one that was added.
121    * XXX More than 25 devices?
122    */
123   char path[] = "/sys/block/xdx/dev";
124   char class[3] = { 'v', 's', 'h' };
125   size_t i, j;
126   fp = NULL;
127   for (i = 0; i < sizeof class; ++i) {
128     for (j = 'z'; j >= 'a'; --j) {
129       path[11] = class[i];
130       path[13] = j;
131       fp = fopen (path, "r");
132       if (fp != NULL)
133         goto found;
134     }
135   }
136   fprintf (stderr,
137            "febootstrap: no ext2 root device found\n"
138            "Please include FULL verbose output in your bug report.\n");
139   exit (EXIT_FAILURE);
140
141  found:
142   if (verbose)
143     fprintf (stderr, "febootstrap: picked %s as root device\n", path);
144
145   fgets (line, sizeof line, fp);
146   int major = atoi (line);
147   char *p = line + strcspn (line, ":") + 1;
148   int minor = atoi (p);
149
150   fclose (fp);
151   if (umount ("/sys") == -1) {
152     perror ("umount: /sys");
153     exit (EXIT_FAILURE);
154   }
155
156   if (verbose)
157     fprintf (stderr, "febootstrap: creating /dev/root as block special %d:%d\n",
158              major, minor);
159
160   if (mknod ("/dev/root", S_IFBLK|0700, makedev (major, minor)) == -1) {
161     perror ("mknod: /dev/root");
162     exit (EXIT_FAILURE);
163   }
164
165   /* Mount new root and chroot to it. */
166   if (verbose)
167     fprintf (stderr, "febootstrap: mounting new root on /root\n");
168   if (mount ("/dev/root", "/root", "ext2", MS_NOATIME, "") == -1) {
169     perror ("mount: /root");
170     exit (EXIT_FAILURE);
171   }
172
173   /* Note that pivot_root won't work.  See the note in
174    * Documentation/filesystems/ramfs-rootfs-initramfs.txt
175    * We could remove the old initramfs files, but let's not bother.
176    */
177   if (verbose)
178     fprintf (stderr, "febootstrap: chroot\n");
179
180   if (chroot ("/root") == -1) {
181     perror ("chroot: /root");
182     exit (EXIT_FAILURE);
183   }
184
185   chdir ("/");
186
187   /* Run /init from ext2 filesystem. */
188   print_uptime ();
189   execl ("/init", "init", NULL);
190   perror ("execl: /init");
191
192   /* /init failed to execute, but why?  Before we ditch, print some
193    * debug.  Although we have a full appliance, the fact that /init
194    * failed to run means we may not be able to run any commands.
195    */
196   show_directory ("/");
197   show_directory ("/bin");
198   show_directory ("/lib");
199   show_directory ("/lib64");
200   fflush (stderr);
201
202   exit (EXIT_FAILURE);
203 }
204
205 static void
206 insmod (const char *filename)
207 {
208   size_t size;
209
210   if (verbose)
211     fprintf (stderr, "febootstrap: internal insmod %s\n", filename);
212
213 #ifdef HAVE_LIBZ
214   gzFile gzfp = gzopen (filename, "rb");
215   int capacity = 64*1024;
216   char *buf = (char *) malloc (capacity);
217   int tmpsize = 8 * 1024;
218   char tmp[tmpsize];
219   int num;
220
221   size = 0;
222
223   if (gzfp == NULL) {
224     fprintf (stderr, "insmod: gzopen failed: %s", filename);
225     exit (EXIT_FAILURE);
226   }
227   while ((num = gzread (gzfp, tmp, tmpsize)) > 0) {
228     if (num > capacity) {
229       buf = (char*) realloc (buf, size*2);
230       capacity = size;
231     }
232     memcpy (buf+size, tmp, num);
233     capacity -= num;
234     size += num;
235   }
236   if (num == -1) {
237     perror ("insmod: gzread");
238     exit (EXIT_FAILURE);
239   }
240   gzclose (gzfp);
241 #else
242   int fd = open (filename, O_RDONLY);
243   if (fd == -1) {
244     fprintf (stderr, "insmod: open: %s: %m\n", filename);
245     exit (EXIT_FAILURE);
246   }
247   struct stat st;
248   if (fstat (fd, &st) == -1) {
249     perror ("insmod: fstat");
250     exit (EXIT_FAILURE);
251   }
252   size = st.st_size;
253   char buf[size];
254   size_t offset = 0;
255   do {
256     ssize_t rc = read (fd, buf + offset, size - offset);
257     if (rc == -1) {
258       perror ("insmod: read");
259       exit (EXIT_FAILURE);
260     }
261     offset += rc;
262   } while (offset < size);
263   close (fd);
264 #endif
265
266   if (init_module (buf, size, "") != 0) {
267     fprintf (stderr, "insmod: init_module: %s: %s\n", filename, moderror (errno));
268     /* However ignore the error because this can just happen because
269      * of a missing device.
270      */
271   }
272
273 #ifdef HAVE_LIBZ
274   free (buf);
275 #endif
276 }
277
278 /* Mount /proc unless it's mounted already. */
279 static void
280 mount_proc (void)
281 {
282   if (access ("/proc/uptime", R_OK) == -1) {
283     mkdir ("/proc", 0755);
284
285     if (verbose)
286       fprintf (stderr, "febootstrap: mounting /proc\n");
287
288     if (mount ("proc", "/proc", "proc", 0, "") == -1) {
289       perror ("mount: /proc");
290       /* Non-fatal. */
291     }
292   }
293 }
294
295 /* Print contents of /proc/uptime. */
296 static void
297 print_uptime (void)
298 {
299   FILE *fp = fopen ("/proc/uptime", "r");
300   if (fp == NULL) {
301     perror ("/proc/uptime");
302     return;
303   }
304
305   fgets (line, sizeof line, fp);
306   fclose (fp);
307
308   fprintf (stderr, "febootstrap: uptime: %s", line);
309 }
310
311 /* Display a directory on stderr.  This is used for debugging only. */
312 static char
313 dirtype (int dt)
314 {
315   switch (dt) {
316   case DT_BLK: return 'b';
317   case DT_CHR: return 'c';
318   case DT_DIR: return 'd';
319   case DT_FIFO: return 'p';
320   case DT_LNK: return 'l';
321   case DT_REG: return '-';
322   case DT_SOCK: return 's';
323   case DT_UNKNOWN: return 'u';
324   default: return '?';
325   }
326 }
327
328 static void
329 show_directory (const char *dirname)
330 {
331   DIR *dir;
332   struct dirent *d;
333   struct stat statbuf;
334   char link[PATH_MAX+1];
335   ssize_t n;
336
337   fprintf (stderr, "febootstrap: debug: listing directory %s\n", dirname);
338
339   if (chdir (dirname) == -1) {
340     perror (dirname);
341     return;
342   }
343
344   dir = opendir (".");
345   if (!dir) {
346     perror (dirname);
347     chdir ("/");
348     return;
349   }
350
351   while ((d = readdir (dir)) != NULL) {
352     fprintf (stderr, "%5lu %c %-16s", d->d_ino, dirtype (d->d_type), d->d_name);
353     if (lstat (d->d_name, &statbuf) >= 0) {
354       fprintf (stderr, " %06o %ld %d:%d",
355                statbuf.st_mode, statbuf.st_size,
356                statbuf.st_uid, statbuf.st_gid);
357       if (S_ISLNK (statbuf.st_mode)) {
358         n = readlink (d->d_name, link, PATH_MAX);
359         if (n >= 0) {
360           link[n] = '\0';
361           fprintf (stderr, " -> %s", link);
362         }
363       }
364     }
365     fprintf (stderr, "\n");
366   }
367
368   closedir (dir);
369   chdir ("/");
370 }