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.
28 #include <sys/types.h>
36 struct timeval start_t;
39 static const char *format = "cpio";
41 enum { HELP_OPTION = CHAR_MAX + 1 };
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' },
54 usage (FILE *f, const char *progname)
57 "%s: build the supermin appliance on the fly\n"
60 " %s [-options] inputs [...] host_cpu kernel initrd\n"
61 " %s -f ext2 inputs [...] host_cpu kernel initrd appliance\n"
62 " %s -f checksum inputs [...] host_cpu\n"
66 "This script is used by febootstrap to build the supermin appliance\n"
67 "(kernel and initrd output files). You should NOT need to run this\n"
68 "program directly except if you are debugging tricky supermin\n"
69 "appliance problems.\n"
71 "NB: The kernel and initrd parameters are OUTPUT parameters. If\n"
72 "those files exist, they are overwritten by the output.\n"
76 " Display this help text and exit.\n"
77 " -f cpio|ext2|checksum | --format cpio|ext2|checksum\n"
78 " Specify output format (default: cpio).\n"
79 " -k file | --kmods file\n"
80 " Specify kernel module whitelist.\n"
82 " Enable verbose messages (give multiple times for more verbosity).\n"
84 " Display version number and exit.\n",
85 progname, progname, progname, progname, progname, progname);
89 main (int argc, char *argv[])
91 /* First thing: start the clock. */
92 gettimeofday (&start_t, NULL);
94 const char *whitelist = NULL;
96 /* Command line arguments. */
98 int c = getopt_long (argc, argv, options, long_options, NULL);
103 usage (stdout, argv[0]);
119 printf (PACKAGE_NAME " " PACKAGE_VERSION "\n");
123 usage (stderr, argv[0]);
128 /* Select the correct writer module. */
129 struct writer *writer;
132 if (strcmp (format, "cpio") == 0) {
133 writer = &cpio_writer;
134 nr_outputs = 2; /* kernel and appliance (== initrd) */
136 else if (strcmp (format, "ext2") == 0) {
137 writer = &ext2_writer;
138 nr_outputs = 3; /* kernel, initrd, appliance */
140 else if (strcmp (format, "checksum") == 0) {
141 writer = &checksum_writer;
142 nr_outputs = 0; /* (none) */
146 "%s: incorrect output format (-f): must be cpio|ext2|checksum\n",
151 /* [optind .. optind+nr_inputs-1] hostcpu [argc-nr_outputs-1 .. argc-1]
152 * <---- nr_inputs ----> 1 <---- nr_outputs ---->
154 char **inputs = &argv[optind];
155 int nr_inputs = argc - nr_outputs - 1 - optind;
156 char **outputs = &argv[optind+nr_inputs+1];
157 /*assert (outputs [nr_outputs] == NULL);
158 assert (inputs [nr_inputs + 1 + nr_outputs] == NULL);*/
161 fprintf (stderr, "%s: not enough files specified on the command line\n",
166 /* See: https://bugzilla.redhat.com/show_bug.cgi?id=558593 */
167 const char *hostcpu = outputs[-1];
170 const char *kernel = NULL, *initrd = NULL, *appliance = NULL;
174 initrd = appliance = outputs[1];
176 appliance = outputs[2];
179 print_timestamped_message ("whitelist = %s, "
184 whitelist ? : "(not specified)",
185 hostcpu, kernel, initrd, appliance);
187 for (i = 0; i < nr_inputs; ++i)
188 print_timestamped_message ("inputs[%d] = %s", i, inputs[i]);
191 /* Remove the output files if they exist. */
196 if (appliance && initrd != appliance)
199 /* Create kernel output file. */
200 const char *modpath = create_kernel (hostcpu, kernel);
203 print_timestamped_message ("finished creating kernel");
205 /* Create the appliance. */
206 create_appliance (hostcpu, inputs, nr_inputs, whitelist, modpath,
207 initrd, appliance, writer);
210 print_timestamped_message ("finished creating appliance");