2474b149fc4ec9c27f4798b85884e49fab10a1c4
[febootstrap.git] / helper / appliance.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 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <dirent.h>
28 #include <fnmatch.h>
29 #include <sys/stat.h>
30 #include <assert.h>
31
32 #include "error.h"
33 #include "fts_.h"
34 #include "xalloc.h"
35 #include "xvasprintf.h"
36
37 #include "helper.h"
38
39 static void iterate_inputs (char **inputs, int nr_inputs, struct writer *);
40 static void iterate_input_directory (const char *dirname, int dirfd, struct writer *);
41 static void add_kernel_modules (const char *whitelist, const char *modpath, struct writer *);
42 static void add_hostfiles (const char *hostfiles_file, struct writer *);
43
44 /* Create the appliance.
45  *
46  * The initrd consists of these components concatenated together:
47  *
48  * (1) The base skeleton appliance that we constructed at build time.
49  *     format = plain cpio
50  * (2) The host files which match wildcards in *.supermin.hostfiles.
51  *     input format = plain text, output format = plain cpio
52  * (3) The modules from modpath which are on the module whitelist.
53  *     output format = plain cpio
54  *
55  * The original shell script used the external cpio program to create
56  * parts (2) and (3), but we have decided it's going to be faster if
57  * we just write out the data outselves.  The reasons are that
58  * external cpio is slow (particularly when used with SELinux because
59  * it does 512 byte reads), and the format that we're writing is
60  * narrow and well understood, because we only care that the Linux
61  * kernel can read it.
62  *
63  * This version contains some improvements over the C version written
64  * for libguestfs, in that we can have multiple base images (or
65  * hostfiles) or use a directory to store these files.
66  */
67 void
68 create_appliance (char **inputs, int nr_inputs,
69                   const char *whitelist,
70                   const char *modpath,
71                   const char *initrd,
72                   const char *appliance,
73                   struct writer *writer)
74 {
75   writer->wr_start (appliance, modpath, initrd);
76
77   iterate_inputs (inputs, nr_inputs, writer);
78
79   /* Kernel modules (3). */
80   add_kernel_modules (whitelist, modpath, writer);
81
82   writer->wr_end ();
83 }
84
85 /* Iterate over the inputs to find out what they are, visiting
86  * directories if specified.
87  */
88 static void
89 iterate_inputs (char **inputs, int nr_inputs, struct writer *writer)
90 {
91   int i;
92   for (i = 0; i < nr_inputs; ++i) {
93     if (verbose)
94       print_timestamped_message ("visiting %s", inputs[i]);
95
96     int fd = open (inputs[i], O_RDONLY);
97     if (fd == -1)
98       error (EXIT_FAILURE, errno, "open: %s", inputs[i]);
99
100     struct stat statbuf;
101     if (fstat (fd, &statbuf) == -1)
102       error (EXIT_FAILURE, errno, "fstat: %s", inputs[i]);
103
104     /* Directory? */
105     if (S_ISDIR (statbuf.st_mode))
106       iterate_input_directory (inputs[i], fd, writer);
107     else if (S_ISREG (statbuf.st_mode)) {
108       /* Is it a cpio file? */
109       char buf[6];
110       if (read (fd, buf, 6) == 6 && memcmp (buf, "070701", 6) == 0)
111         /* Yes, a cpio file.  This is a skeleton appliance, case (1). */
112         writer->wr_cpio_file (inputs[i]);
113       else
114         /* No, must be hostfiles, case (2). */
115         add_hostfiles (inputs[i], writer);
116     }
117     else
118       error (EXIT_FAILURE, 0, "%s: input is not a regular file or directory",
119              inputs[i]);
120
121     close (fd);
122   }
123 }
124
125 static void
126 iterate_input_directory (const char *dirname, int dirfd, struct writer *writer)
127 {
128   char path[PATH_MAX];
129   strcpy (path, dirname);
130   size_t len = strlen (dirname);
131   path[len++] = '/';
132
133   char *inputs[] = { path };
134
135   DIR *dir = fdopendir (dirfd);
136   if (dir == NULL)
137     error (EXIT_FAILURE, errno, "fdopendir: %s", dirname);
138
139   struct dirent *d;
140   while ((errno = 0, d = readdir (dir)) != NULL) {
141     if (d->d_name[0] == '.') /* ignore ., .. and any hidden files. */
142       continue;
143
144     strcpy (&path[len], d->d_name);
145     iterate_inputs (inputs, 1, writer);
146   }
147
148   if (errno != 0)
149     error (EXIT_FAILURE, errno, "readdir: %s", dirname);
150
151   if (closedir (dir) == -1)
152     error (EXIT_FAILURE, errno, "closedir: %s", dirname);
153 }
154
155 /* Copy kernel modules.
156  *
157  * Find every file under modpath.
158  *
159  * Exclude all *.ko files, *except* ones which match names in
160  * the whitelist (which may contain wildcards).  Include all
161  * other files.
162  *
163  * Add chosen files to the output.
164  *
165  * whitelist_file may be NULL, to include ALL kernel modules.
166  */
167 static void
168 add_kernel_modules (const char *whitelist_file, const char *modpath,
169                     struct writer *writer)
170 {
171   char **whitelist = NULL;
172   if (whitelist_file != NULL)
173     whitelist = load_file (whitelist_file);
174
175   char *paths[2] = { (char *) modpath, NULL };
176   FTS *fts = fts_open (paths, FTS_COMFOLLOW|FTS_PHYSICAL, NULL);
177   if (fts == NULL)
178     error (EXIT_FAILURE, errno, "add_kernel_modules: fts_open: %s", modpath);
179
180   for (;;) {
181     errno = 0;
182     FTSENT *entry = fts_read (fts);
183     if (entry == NULL && errno != 0)
184       error (EXIT_FAILURE, errno, "add_kernel_modules: fts_read: %s", modpath);
185     if (entry == NULL)
186       break;
187
188     /* Ignore directories being visited in post-order. */
189     if (entry->fts_info & FTS_DP)
190       continue;
191
192     /* Is it a *.ko file? */
193     if (entry->fts_namelen >= 3 &&
194         entry->fts_name[entry->fts_namelen-3] == '.' &&
195         entry->fts_name[entry->fts_namelen-2] == 'k' &&
196         entry->fts_name[entry->fts_namelen-1] == 'o') {
197       if (whitelist) {
198         /* Is it a *.ko file which is on the whitelist? */
199         size_t j;
200         for (j = 0; whitelist[j] != NULL; ++j) {
201           int r;
202           r = fnmatch (whitelist[j], entry->fts_name, 0);
203           if (r == 0) {
204             /* It's on the whitelist, so include it. */
205             if (verbose >= 2)
206               fprintf (stderr, "including kernel module %s (matches whitelist entry %s)\n",
207                        entry->fts_name, whitelist[j]);
208             writer->wr_fts_entry (entry);
209             break;
210           } else if (r != FNM_NOMATCH)
211             error (EXIT_FAILURE, 0, "internal error: fnmatch ('%s', '%s', %d) returned unexpected non-zero value %d\n",
212                    whitelist[j], entry->fts_name, 0, r);
213         } /* for (j) */
214       } else { /* whitelist == NULL, always include */
215         if (verbose >= 2)
216           fprintf (stderr, "including kernel module %s\n", entry->fts_name);
217         writer->wr_fts_entry (entry);
218       }
219     } else
220       /* It's some other sort of file, or a directory, always include. */
221       writer->wr_fts_entry (entry);
222   }
223
224   if (fts_close (fts) == -1)
225     error (EXIT_FAILURE, errno, "add_kernel_modules: fts_close: %s", modpath);
226 }
227
228 /* Copy the host files.
229  *
230  * Read the list of entries in hostfiles (which may contain
231  * wildcards).  Look them up in the filesystem, and add those files
232  * that exist.  Ignore any files that don't exist or are not readable.
233  */
234 static void
235 add_hostfiles (const char *hostfiles_file, struct writer *writer)
236 {
237   char **hostfiles = load_file (hostfiles_file);
238
239   /* Hostfiles list can contain "." before each path - ignore it.
240    * It also contains each directory name before we enter it.  But
241    * we don't read that until we see a wildcard for that directory.
242    */
243   size_t i, j;
244   for (i = 0; hostfiles[i] != NULL; ++i) {
245     char *hostfile = hostfiles[i];
246     if (hostfile[0] == '.')
247       hostfile++;
248
249     struct stat statbuf;
250
251     /* Is it a wildcard? */
252     if (strchr (hostfile, '*') || strchr (hostfile, '?')) {
253       char *dirname = xstrdup (hostfile);
254       char *patt = strrchr (dirname, '/');
255       assert (patt);
256       *patt++ = '\0';
257
258       char **files = read_dir (dirname);
259       files = filter_fnmatch (files, patt, FNM_NOESCAPE);
260
261       /* Add matching files. */
262       for (j = 0; files[j] != NULL; ++j) {
263         char *tmp = xasprintf ("%s/%s", dirname, files[j]);
264
265         if (verbose >= 2)
266           fprintf (stderr, "including host file %s (matches %s)\n", tmp, patt);
267
268         writer->wr_file (tmp);
269
270         free (tmp);
271       }
272     }
273     /* Else does this file/directory/whatever exist? */
274     else if (lstat (hostfile, &statbuf) == 0) {
275       if (verbose >= 2)
276         fprintf (stderr, "including host file %s (directly referenced)\n",
277                  hostfile);
278
279       writer->wr_file_stat (hostfile, &statbuf);
280     } /* Ignore files that don't exist. */
281   }
282 }