Remove bogus 'whitelist' parameter from usage.
[febootstrap.git] / helper / main.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 <errno.h>
25 #include <unistd.h>
26 #include <getopt.h>
27 #include <limits.h>
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #include <assert.h>
31
32 #include "error.h"
33
34 #include "helper.h"
35
36 struct timeval start_t;
37 int verbose = 0;
38
39 static const char *format = "cpio";
40
41 enum { HELP_OPTION = CHAR_MAX + 1 };
42
43 static const char *options = "f:k:vV";
44 static const struct option long_options[] = {
45   { "help", 0, 0, HELP_OPTION },
46   { "format", required_argument, 0, 'f' },
47   { "kmods", required_argument, 0, 'k' },
48   { "verbose", 0, 0, 'v' },
49   { "version", 0, 0, 'V' },
50   { 0, 0, 0, 0 }
51 };
52
53 static void
54 usage (const char *progname)
55 {
56   printf ("%s: build the supermin appliance on the fly\n"
57           "\n"
58           "Usage:\n"
59           "  %s [-options] inputs [...] host_cpu kernel initrd\n"
60           "  %s -f ext2 inputs [...] host_cpu kernel initrd appliance\n"
61           "  %s --help\n"
62           "  %s --version\n"
63           "\n"
64           "This script is used by febootstrap to build the supermin appliance\n"
65           "(kernel and initrd output files).  You should NOT need to run this\n"
66           "program directly except if you are debugging tricky supermin\n"
67           "appliance problems.\n"
68           "\n"
69           "NB: The kernel and initrd parameters are OUTPUT parameters.  If\n"
70           "those files exist, they are overwritten by the output.\n"
71           "\n"
72           "Options:\n"
73           "  --help\n"
74           "       Display this help text and exit.\n"
75           "  -f cpio | --format cpio\n"
76           "       Specify output format (default: cpio).\n"
77           "  -k file | --kmods file\n"
78           "       Specify kernel module whitelist.\n"
79           "  --verbose | -v\n"
80           "       Enable verbose messages (give multiple times for more verbosity).\n"
81           "  --version | -V\n"
82           "       Display version number and exit.\n",
83           progname, progname, progname, progname, progname);
84 }
85
86 int
87 main (int argc, char *argv[])
88 {
89   /* First thing: start the clock. */
90   gettimeofday (&start_t, NULL);
91
92   const char *whitelist = NULL;
93
94   /* Command line arguments. */
95   for (;;) {
96     int c = getopt_long (argc, argv, options, long_options, NULL);
97     if (c == -1) break;
98
99     switch (c) {
100     case HELP_OPTION:
101       usage (argv[0]);
102       exit (EXIT_SUCCESS);
103
104     case 'f':
105       format = optarg;
106       break;
107
108     case 'k':
109       whitelist = optarg;
110       break;
111
112     case 'v':
113       verbose++;
114       break;
115
116     case 'V':
117       printf (PACKAGE_NAME " " PACKAGE_VERSION "\n");
118       exit (EXIT_SUCCESS);
119
120     default:
121       usage (argv[0]);
122       exit (EXIT_FAILURE);
123     }
124   }
125
126   /* Select the correct writer module. */
127   struct writer *writer;
128   int nr_outputs;
129
130   if (strcmp (format, "cpio") == 0) {
131     writer = &cpio_writer;
132     nr_outputs = 2;             /* kernel and appliance (== initrd) */
133   }
134   else if (strcmp (format, "ext2") == 0) {
135     writer = &ext2_writer;
136     nr_outputs = 3;             /* kernel, initrd, appliance */
137   }
138   else {
139     fprintf (stderr, "%s: incorrect output format (-f): must be cpio\n",
140              argv[0]);
141     exit (EXIT_FAILURE);
142   }
143
144   /* [optind .. optind+nr_inputs-1] hostcpu [argc-nr_outputs-1 .. argc-1]
145    * <----     nr_inputs      ---->    1    <----    nr_outputs     ---->
146    */
147   char **inputs = &argv[optind];
148   int nr_inputs = argc - nr_outputs - 1 - optind;
149   char **outputs = &argv[optind+nr_inputs+1];
150   /*assert (outputs [nr_outputs] == NULL);
151     assert (inputs [nr_inputs + 1 + nr_outputs] == NULL);*/
152
153   if (nr_inputs < 1) {
154     fprintf (stderr, "%s: not enough files specified on the command line\n",
155              argv[0]);
156     exit (EXIT_FAILURE);
157   }
158
159   /* See: https://bugzilla.redhat.com/show_bug.cgi?id=558593 */
160   const char *hostcpu = outputs[-1];
161
162   /* Output files. */
163   const char *kernel = outputs[0];
164   const char *initrd;
165   const char *appliance;
166   initrd = appliance = outputs[1];
167   if (nr_outputs > 2)
168     appliance = outputs[2];
169
170   if (verbose) {
171     print_timestamped_message ("whitelist = %s, "
172                                "host_cpu = %s, "
173                                "kernel = %s, "
174                                "initrd = %s, "
175                                "appliance = %s",
176                                whitelist ? : "(not specified)",
177                                hostcpu, kernel, initrd, appliance);
178     int i;
179     for (i = 0; i < nr_inputs; ++i)
180       print_timestamped_message ("inputs[%d] = %s", i, inputs[i]);
181   }
182
183   /* Remove the output files if they exist. */
184   unlink (kernel);
185   unlink (initrd);
186   if (initrd != appliance)
187     unlink (appliance);
188
189   /* Create kernel output file. */
190   const char *modpath;
191   modpath = create_kernel (hostcpu, kernel);
192
193   if (verbose)
194     print_timestamped_message ("finished creating kernel");
195
196   /* Create the appliance. */
197   create_appliance (inputs, nr_inputs, whitelist, modpath,
198                     initrd, appliance, writer);
199
200   if (verbose)
201     print_timestamped_message ("finished creating appliance");
202
203   exit (EXIT_SUCCESS);
204 }