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 /* ext2 requires a small initrd in order to boot. This builds it. */
31 #include "full-write.h"
33 #include "xvasprintf.h"
36 #include "ext2internal.h"
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 *);
42 /* The init binary. */
43 extern char _binary_init_start, _binary_init_end, _binary_init_size;
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.
49 static const char *kmods[] = {
55 "scsi_transport_spi.ko",
68 ext2_make_initrd (const char *modpath, const char *initrd)
70 char dir[] = "/tmp/ext2initrdXXXXXX";
71 if (mkdtemp (dir) == NULL)
72 error (EXIT_FAILURE, errno, "mkdtemp");
77 /* Copy kernel modules into tmpdir. */
78 size_t n = strlen (modpath) + strlen (dir) + 64;
80 for (i = 0; kmods[i] != NULL; ++i)
81 n += strlen (kmods[i]) + 16;
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]);
90 strcat (cmd, "| xargs cp -t ");
92 if (verbose >= 2) fprintf (stderr, "%s\n", cmd);
94 if (r == -1 || WEXITSTATUS (r) != 0)
95 error (EXIT_FAILURE, 0, "ext2_make_initrd: copy kmods failed");
98 /* The above command effectively gives us the final list of modules.
99 * Calculate dependencies from modpath/modules.dep and write that
102 read_module_deps (modpath);
104 cmd = xasprintf ("tsort > %s/modules", dir);
105 if (verbose >= 2) fprintf (stderr, "%s\n", cmd);
106 FILE *pp = popen (cmd, "w");
108 error (EXIT_FAILURE, errno, "tsort: failed to create modules list");
110 DIR *dr = opendir (dir);
112 error (EXIT_FAILURE, errno, "opendir: %s", dir);
115 while ((errno = 0, d = readdir (dr)) != NULL) {
116 size_t n = strlen (d->d_name);
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);
123 /* Reversed so that tsort will print the final list in the
124 * order that it has to be loaded.
126 fprintf (pp, "%s %s\n", dep, d->d_name);
128 /* No dependencies, just make it depend on itself so that
131 fprintf (pp, "%s %s\n", d->d_name, d->d_name);
135 error (EXIT_FAILURE, errno, "readdir: %s", dir);
137 if (closedir (dr) == -1)
138 error (EXIT_FAILURE, errno, "closedir: %s", dir);
140 if (pclose (pp) == -1)
141 error (EXIT_FAILURE, errno, "pclose: %s", cmd);
146 /* Copy in insmod static binary. */
147 cmd = xasprintf ("cp /sbin/insmod.static %s", dir);
148 if (verbose >= 2) fprintf (stderr, "%s\n", cmd);
150 if (r == -1 || WEXITSTATUS (r) != 0)
151 error (EXIT_FAILURE, 0,
152 "ext2_make_initrd: copy /sbin/insmod.static failed");
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);
159 error (EXIT_FAILURE, errno, "open: %s", init);
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);
165 if (close (fd) == -1)
166 error (EXIT_FAILURE, errno, "close: %s", init);
170 /* Build the cpio file. */
171 cmd = xasprintf ("(cd %s && (echo . ; ls -1)"
172 " | cpio --quiet -o -H newc) > '%s'",
174 if (verbose >= 2) fprintf (stderr, "%s\n", cmd);
176 if (r == -1 || WEXITSTATUS (r) != 0)
177 error (EXIT_FAILURE, 0, "ext2_make_initrd: cpio failed");
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);
187 /* Module dependencies. */
193 struct moddep *moddeps = NULL;
195 static void add_module_dep (const char *name, const char *dep);
198 free_module_deps (void)
200 /* Short-lived program, don't bother to free it. */
204 /* Read modules.dep into internal structure. */
206 read_module_deps (const char *modpath)
210 char *filename = xasprintf ("%s/modules.dep", modpath);
211 FILE *fp = fopen (filename, "r");
213 error (EXIT_FAILURE, errno, "open: %s", modpath);
218 while ((len = getline (&line, &llen, fp)) != -1) {
219 if (len > 0 && line[len-1] == '\n')
222 char *name = strtok (line, ": ");
225 /* Only want the module basename, but keep the ".ko" extension. */
226 char *p = strrchr (name, '/');
230 while ((dep = strtok (NULL, " ")) != NULL) {
231 p = strrchr (dep, '/');
234 add_module_dep (name, dep);
242 /* Module 'name' requires 'dep' to be loaded first. */
244 add_module_dep (const char *name, const char *dep)
246 struct moddep *m = xmalloc (sizeof *m);
249 m->name = xstrdup (name);
250 m->dep = xstrdup (dep);
254 get_module_dep (const char *name)
258 for (m = moddeps; m; m = m->next)
259 if (strcmp (m->name, name) == 0)