Rewrite febootstrap as a general supermin appliance building tool.
[febootstrap.git] / helper / ext2initrd.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 /* ext2 requires a small initrd in order to boot.  This builds it. */
20
21 #include <config.h>
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <dirent.h>
28 #include <errno.h>
29
30 #include "error.h"
31 #include "full-write.h"
32 #include "xalloc.h"
33 #include "xvasprintf.h"
34
35 #include "helper.h"
36 #include "ext2internal.h"
37
38 static void read_module_deps (const char *modpath);
39 static void free_module_deps (void);
40 static const char *get_module_dep (const char *);
41
42 /* The init binary. */
43 extern char _binary_init_start, _binary_init_end, _binary_init_size;
44
45 /* The list of modules (wildcards) we consider for inclusion in the
46  * mini initrd.  Only what is needed in order to find a device with an
47  * ext2 filesystem on it.
48  */
49 static const char *kmods[] = {
50   "ext2.ko",
51   "virtio*.ko",
52   "ide*.ko",
53   "libata*.ko",
54   "piix*.ko",
55   "scsi_transport_spi.ko",
56   "scsi_mod.ko",
57   "sd_mod.ko",
58   "sym53c8xx.ko",
59   "ata_piix.ko",
60   "sr_mod.ko",
61   "mbcache.ko",
62   "crc*.ko",
63   "libcrc*.ko",
64   NULL
65 };
66
67 void
68 ext2_make_initrd (const char *modpath, const char *initrd)
69 {
70   char dir[] = "/tmp/ext2initrdXXXXXX";
71   if (mkdtemp (dir) == NULL)
72     error (EXIT_FAILURE, errno, "mkdtemp");
73
74   char *cmd;
75   int r;
76
77   /* Copy kernel modules into tmpdir. */
78   size_t n = strlen (modpath) + strlen (dir) + 64;
79   size_t i;
80   for (i = 0; kmods[i] != NULL; ++i)
81     n += strlen (kmods[i]) + 16;
82   cmd = malloc (n);
83   sprintf (cmd, "find '%s' ", modpath);
84   for (i = 0; kmods[i] != NULL; ++i) {
85     if (i > 0) strcat (cmd, "-o ");
86     strcat (cmd, "-name '");
87     strcat (cmd, kmods[i]);
88     strcat (cmd, "' ");
89   }
90   strcat (cmd, "| xargs cp -t ");
91   strcat (cmd, dir);
92   if (verbose >= 2) fprintf (stderr, "%s\n", cmd);
93   r = system (cmd);
94   if (r == -1 || WEXITSTATUS (r) != 0)
95     error (EXIT_FAILURE, 0, "ext2_make_initrd: copy kmods failed");
96   free (cmd);
97
98   /* The above command effectively gives us the final list of modules.
99    * Calculate dependencies from modpath/modules.dep and write that
100    * into the output.
101    */
102   read_module_deps (modpath);
103
104   cmd = xasprintf ("tsort > %s/modules", dir);
105   if (verbose >= 2) fprintf (stderr, "%s\n", cmd);
106   FILE *pp = popen (cmd, "w");
107   if (pp == NULL)
108     error (EXIT_FAILURE, errno, "tsort: failed to create modules list");
109
110   DIR *dr = opendir (dir);
111   if (dr == NULL)
112     error (EXIT_FAILURE, errno, "opendir: %s", dir);
113
114   struct dirent *d;
115   while ((errno = 0, d = readdir (dr)) != NULL) {
116     size_t n = strlen (d->d_name);
117     if (n >= 3 &&
118         d->d_name[n-3] == '.' &&
119         d->d_name[n-2] == 'k' &&
120         d->d_name[n-1] == 'o') {
121       const char *dep = get_module_dep (d->d_name);
122       if (dep)
123         /* Reversed so that tsort will print the final list in the
124          * order that it has to be loaded.
125          */
126         fprintf (pp, "%s %s\n", dep, d->d_name);
127       else
128         /* No dependencies, just make it depend on itself so that
129          * tsort prints it.
130          */
131         fprintf (pp, "%s %s\n", d->d_name, d->d_name);
132     }
133   }
134   if (errno)
135     error (EXIT_FAILURE, errno, "readdir: %s", dir);
136
137   if (closedir (dr) == -1)
138     error (EXIT_FAILURE, errno, "closedir: %s", dir);
139
140   if (pclose (pp) == -1)
141     error (EXIT_FAILURE, errno, "pclose: %s", cmd);
142
143   free (cmd);
144   free_module_deps ();
145
146   /* Copy in insmod static binary. */
147   cmd = xasprintf ("cp /sbin/insmod.static %s", dir);
148   if (verbose >= 2) fprintf (stderr, "%s\n", cmd);
149   r = system (cmd);
150   if (r == -1 || WEXITSTATUS (r) != 0)
151     error (EXIT_FAILURE, 0,
152            "ext2_make_initrd: copy /sbin/insmod.static failed");
153   free (cmd);
154
155   /* Copy in the init program, linked into this program as a data blob. */
156   char *init = xasprintf ("%s/init", dir);
157   int fd = open (init, O_WRONLY|O_TRUNC|O_CREAT|O_NOCTTY, 0755);
158   if (fd == -1)
159     error (EXIT_FAILURE, errno, "open: %s", init);
160
161   n = (size_t) &_binary_init_size;
162   if (full_write (fd, &_binary_init_start, n) != n)
163     error (EXIT_FAILURE, errno, "write: %s", init);
164
165   if (close (fd) == -1)
166     error (EXIT_FAILURE, errno, "close: %s", init);
167
168   free (init);
169
170   /* Build the cpio file. */
171   cmd = xasprintf ("(cd %s && (echo . ; ls -1)"
172                    " | cpio --quiet -o -H newc) > '%s'",
173                    dir, initrd);
174   if (verbose >= 2) fprintf (stderr, "%s\n", cmd);
175   r = system (cmd);
176   if (r == -1 || WEXITSTATUS (r) != 0)
177     error (EXIT_FAILURE, 0, "ext2_make_initrd: cpio failed");
178   free (cmd);
179
180   /* Construction of 'dir' above ensures this is safe. */
181   cmd = xasprintf ("rm -rf %s", dir);
182   if (verbose >= 2) fprintf (stderr, "%s\n", cmd);
183   system (cmd);
184   free (cmd);
185 }
186
187 /* Module dependencies. */
188 struct moddep {
189   struct moddep *next;
190   char *name;
191   char *dep;
192 };
193 struct moddep *moddeps = NULL;
194
195 static void add_module_dep (const char *name, const char *dep);
196
197 static void
198 free_module_deps (void)
199 {
200   /* Short-lived program, don't bother to free it. */
201   moddeps = NULL;
202 }
203
204 /* Read modules.dep into internal structure. */
205 static void
206 read_module_deps (const char *modpath)
207 {
208   free_module_deps ();
209
210   char *filename = xasprintf ("%s/modules.dep", modpath);
211   FILE *fp = fopen (filename, "r");
212   if (fp == NULL)
213     error (EXIT_FAILURE, errno, "open: %s", modpath);
214
215   char *line = NULL;
216   size_t llen = 0;
217   ssize_t len;
218   while ((len = getline (&line, &llen, fp)) != -1) {
219     if (len > 0 && line[len-1] == '\n')
220       line[--len] = '\0';
221
222     char *name = strtok (line, ": ");
223     if (!name) continue;
224
225     /* Only want the module basename, but keep the ".ko" extension. */
226     char *p = strrchr (name, '/');
227     if (p) name = p+1;
228
229     char *dep;
230     while ((dep = strtok (NULL, " ")) != NULL) {
231       p = strrchr (dep, '/');
232       if (p) dep = p+1;
233
234       add_module_dep (name, dep);
235     }
236   }
237
238   free (line);
239   fclose (fp);
240 }
241
242 /* Module 'name' requires 'dep' to be loaded first. */
243 static void
244 add_module_dep (const char *name, const char *dep)
245 {
246   struct moddep *m = xmalloc (sizeof *m);
247   m->next = moddeps;
248   moddeps = m;
249   m->name = xstrdup (name);
250   m->dep = xstrdup (dep);
251 }
252
253 static const char *
254 get_module_dep (const char *name)
255 {
256   struct moddep *m;
257
258   for (m = moddeps; m; m = m->next)
259     if (strcmp (m->name, name) == 0)
260       return m->dep;
261
262   return NULL;
263 }