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>
39 struct timeval start_t;
42 enum { HELP_OPTION = CHAR_MAX + 1 };
44 static const char *options = "f:g:k:u:vV";
45 static const struct option long_options[] = {
46 { "help", 0, 0, HELP_OPTION },
47 { "format", required_argument, 0, 'f' },
48 { "group", 0, 0, 'g' },
49 { "kmods", required_argument, 0, 'k' },
50 { "user", 0, 0, 'u' },
51 { "verbose", 0, 0, 'v' },
52 { "version", 0, 0, 'V' },
57 usage (FILE *f, const char *progname)
60 "%s: build the supermin appliance on the fly\n"
63 " %s [-options] inputs [...] host_cpu kernel initrd\n"
64 " %s -f ext2 inputs [...] host_cpu kernel initrd appliance\n"
65 " %s -f checksum inputs [...] host_cpu\n"
69 "This script is used by febootstrap to build the supermin appliance\n"
70 "(kernel and initrd output files). You should NOT need to run this\n"
71 "program directly except if you are debugging tricky supermin\n"
72 "appliance problems.\n"
74 "NB: The kernel and initrd parameters are OUTPUT parameters. If\n"
75 "those files exist, they are overwritten by the output.\n"
79 " Display this help text and exit.\n"
80 " -f cpio|ext2|checksum | --format cpio|ext2|checksum\n"
81 " Specify output format (default: cpio).\n"
83 " The user name or uid the appliance will run as. Use of this\n"
84 " option requires root privileges.\n"
86 " The group name or gid the appliance will run as. Use of\n"
87 " this option requires root privileges.\n"
88 " -k file | --kmods file\n"
89 " Specify kernel module whitelist.\n"
91 " Enable verbose messages (give multiple times for more verbosity).\n"
93 " Display version number and exit.\n",
94 progname, progname, progname, progname, progname, progname);
98 parseuser (const char *id, const char *progname)
108 fprintf (stderr, "Error looking up user: %m\n");
113 int err = xstrtol (id, NULL, 10, &val, "");
114 if (err != LONGINT_OK) {
115 fprintf (stderr, "%s is not a valid user name or uid\n", id);
116 usage (stderr, progname);
127 parsegroup (const char *id, const char *progname)
137 fprintf (stderr, "Error looking up group: %m\n");
142 int err = xstrtol (id, NULL, 10, &val, "");
143 if (err != LONGINT_OK) {
144 fprintf (stderr, "%s is not a valid group name or gid\n", id);
145 usage (stderr, progname);
156 main (int argc, char *argv[])
158 /* First thing: start the clock. */
159 gettimeofday (&start_t, NULL);
161 const char *format = "cpio";
162 const char *whitelist = NULL;
164 uid_t euid = geteuid ();
165 gid_t egid = getegid ();
167 /* Command line arguments. */
169 int c = getopt_long (argc, argv, options, long_options, NULL);
174 usage (stdout, argv[0]);
182 euid = parseuser (optarg, argv[0]);
186 egid = parsegroup (optarg, argv[0]);
198 printf (PACKAGE_NAME " " PACKAGE_VERSION "\n");
202 usage (stderr, argv[0]);
207 /* We need to set the real, not effective, uid here to work round a
208 * misfeature in bash. bash will automatically reset euid to uid when
209 * invoked. As shell is used in places by febootstrap-supermin-helper, this
210 * results in code running with varying privilege. */
211 uid_t uid = getuid ();
212 gid_t gid = getgid ();
214 if (uid != euid || gid != egid) {
216 fprintf (stderr, "The -u and -g options require root privileges.\n");
217 usage (stderr, argv[0]);
221 /* Need to become root first because setgid and setuid require it */
222 if (seteuid (0) == -1) {
227 /* Set gid and uid to command-line parameters */
228 if (setgid (egid) == -1) {
232 if (setuid (euid) == -1) {
238 /* Select the correct writer module. */
239 struct writer *writer;
242 if (strcmp (format, "cpio") == 0) {
243 writer = &cpio_writer;
244 nr_outputs = 2; /* kernel and appliance (== initrd) */
246 else if (strcmp (format, "ext2") == 0) {
247 writer = &ext2_writer;
248 nr_outputs = 3; /* kernel, initrd, appliance */
250 else if (strcmp (format, "checksum") == 0) {
251 writer = &checksum_writer;
252 nr_outputs = 0; /* (none) */
256 "%s: incorrect output format (-f): must be cpio|ext2|checksum\n",
261 /* [optind .. optind+nr_inputs-1] hostcpu [argc-nr_outputs-1 .. argc-1]
262 * <---- nr_inputs ----> 1 <---- nr_outputs ---->
264 char **inputs = &argv[optind];
265 int nr_inputs = argc - nr_outputs - 1 - optind;
266 char **outputs = &argv[optind+nr_inputs+1];
267 /*assert (outputs [nr_outputs] == NULL);
268 assert (inputs [nr_inputs + 1 + nr_outputs] == NULL);*/
271 fprintf (stderr, "%s: not enough files specified on the command line\n",
276 /* See: https://bugzilla.redhat.com/show_bug.cgi?id=558593 */
277 const char *hostcpu = outputs[-1];
280 const char *kernel = NULL, *initrd = NULL, *appliance = NULL;
284 initrd = appliance = outputs[1];
286 appliance = outputs[2];
289 print_timestamped_message ("whitelist = %s, "
294 whitelist ? : "(not specified)",
295 hostcpu, kernel, initrd, appliance);
297 for (i = 0; i < nr_inputs; ++i)
298 print_timestamped_message ("inputs[%d] = %s", i, inputs[i]);
301 /* Remove the output files if they exist. */
306 if (appliance && initrd != appliance)
309 /* Create kernel output file. */
310 const char *modpath = create_kernel (hostcpu, kernel);
313 print_timestamped_message ("finished creating kernel");
315 /* Create the appliance. */
316 create_appliance (hostcpu, inputs, nr_inputs, whitelist, modpath,
317 initrd, appliance, writer);
320 print_timestamped_message ("finished creating appliance");