1 /* $NetBSD: makefs.c,v 1.26 2006/10/22 21:11:56 christos Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (c) 2001-2003 Wasabi Systems, Inc.
7 * All rights reserved.
8 *
9 * Written by Luke Mewburn for Wasabi Systems, Inc.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed for the NetBSD Project by
22 * Wasabi Systems, Inc.
23 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
24 * or promote products derived from this software without specific prior
25 * written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <assert.h>
44 #include <ctype.h>
45 #include <errno.h>
46 #include <limits.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <time.h>
51 #include <unistd.h>
52 #include <stdbool.h>
53 #include <util.h>
54
55 #include "makefs.h"
56 #include "mtree.h"
57
58 /*
59 * list of supported file systems and dispatch functions
60 */
61 typedef struct {
62 const char *type;
63 void (*prepare_options)(fsinfo_t *);
64 int (*parse_options)(const char *, fsinfo_t *);
65 void (*cleanup_options)(fsinfo_t *);
66 void (*make_fs)(const char *, const char *, fsnode *,
67 fsinfo_t *);
68 } fstype_t;
69
70 static fstype_t fstypes[] = {
71 #define ENTRY(name) { \
72 # name, name ## _prep_opts, name ## _parse_opts, \
73 name ## _cleanup_opts, name ## _makefs \
74 }
75 ENTRY(cd9660),
76 ENTRY(ffs),
77 ENTRY(msdos),
78 #ifdef HAVE_ZFS
79 ENTRY(zfs),
80 #endif
81 { .type = NULL },
82 };
83
84 u_int debug;
85 int dupsok;
86 struct timespec start_time;
87 struct stat stampst;
88
89 static fstype_t *get_fstype(const char *);
90 static int get_tstamp(const char *, struct stat *);
91 static void usage(fstype_t *, fsinfo_t *);
92
93 int
main(int argc,char * argv[])94 main(int argc, char *argv[])
95 {
96 struct stat sb;
97 struct timeval start;
98 fstype_t *fstype;
99 fsinfo_t fsoptions;
100 fsnode *root;
101 int ch, i, len;
102 const char *subtree;
103 const char *specfile;
104
105 setprogname(argv[0]);
106
107 debug = 0;
108 if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
109 errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
110
111 /* set default fsoptions */
112 (void)memset(&fsoptions, 0, sizeof(fsoptions));
113 fsoptions.fd = -1;
114 fsoptions.sectorsize = -1;
115
116 if (fstype->prepare_options)
117 fstype->prepare_options(&fsoptions);
118
119 specfile = NULL;
120 #ifdef CLOCK_REALTIME
121 ch = clock_gettime(CLOCK_REALTIME, &start_time);
122 #else
123 ch = gettimeofday(&start, NULL);
124 start_time.tv_sec = start.tv_sec;
125 start_time.tv_nsec = start.tv_usec * 1000;
126 #endif
127 if (ch == -1)
128 err(1, "Unable to get system time");
129
130
131 while ((ch = getopt(argc, argv, "B:b:Dd:f:F:M:m:N:O:o:pR:s:S:t:T:xZ")) != -1) {
132 switch (ch) {
133
134 case 'B':
135 if (strcmp(optarg, "be") == 0 ||
136 strcmp(optarg, "4321") == 0 ||
137 strcmp(optarg, "big") == 0) {
138 #if BYTE_ORDER == LITTLE_ENDIAN
139 fsoptions.needswap = 1;
140 #endif
141 } else if (strcmp(optarg, "le") == 0 ||
142 strcmp(optarg, "1234") == 0 ||
143 strcmp(optarg, "little") == 0) {
144 #if BYTE_ORDER == BIG_ENDIAN
145 fsoptions.needswap = 1;
146 #endif
147 } else {
148 warnx("Invalid endian `%s'.", optarg);
149 usage(fstype, &fsoptions);
150 }
151 break;
152
153 case 'b':
154 len = strlen(optarg) - 1;
155 if (optarg[len] == '%') {
156 optarg[len] = '\0';
157 fsoptions.freeblockpc =
158 strsuftoll("free block percentage",
159 optarg, 0, 99);
160 } else {
161 fsoptions.freeblocks =
162 strsuftoll("free blocks",
163 optarg, 0, LLONG_MAX);
164 }
165 break;
166
167 case 'D':
168 dupsok++;
169 break;
170
171 case 'd':
172 debug = strtoll(optarg, NULL, 0);
173 break;
174
175 case 'f':
176 len = strlen(optarg) - 1;
177 if (optarg[len] == '%') {
178 optarg[len] = '\0';
179 fsoptions.freefilepc =
180 strsuftoll("free file percentage",
181 optarg, 0, 99);
182 } else {
183 fsoptions.freefiles =
184 strsuftoll("free files",
185 optarg, 0, LLONG_MAX);
186 }
187 break;
188
189 case 'F':
190 specfile = optarg;
191 break;
192
193 case 'M':
194 fsoptions.minsize =
195 strsuftoll("minimum size", optarg, 1LL, LLONG_MAX);
196 break;
197
198 case 'N':
199 if (! setup_getid(optarg))
200 errx(1,
201 "Unable to use user and group databases in `%s'",
202 optarg);
203 break;
204
205 case 'm':
206 fsoptions.maxsize =
207 strsuftoll("maximum size", optarg, 1LL, LLONG_MAX);
208 break;
209
210 case 'O':
211 fsoptions.offset =
212 strsuftoll("offset", optarg, 0LL, LLONG_MAX);
213 break;
214
215 case 'o':
216 {
217 char *p;
218
219 while ((p = strsep(&optarg, ",")) != NULL) {
220 if (*p == '\0')
221 errx(1, "Empty option");
222 if (! fstype->parse_options(p, &fsoptions))
223 usage(fstype, &fsoptions);
224 }
225 break;
226 }
227 case 'p':
228 /* Deprecated in favor of 'Z' */
229 fsoptions.sparse = 1;
230 break;
231
232 case 'R':
233 /* Round image size up to specified block size */
234 fsoptions.roundup =
235 strsuftoll("roundup-size", optarg, 0, LLONG_MAX);
236 break;
237
238 case 's':
239 fsoptions.minsize = fsoptions.maxsize =
240 strsuftoll("size", optarg, 1LL, LLONG_MAX);
241 break;
242
243 case 'S':
244 fsoptions.sectorsize =
245 (int)strsuftoll("sector size", optarg,
246 1LL, INT_MAX);
247 break;
248
249 case 't':
250 /* Check current one and cleanup if necessary. */
251 if (fstype->cleanup_options)
252 fstype->cleanup_options(&fsoptions);
253 fsoptions.fs_specific = NULL;
254 if ((fstype = get_fstype(optarg)) == NULL)
255 errx(1, "Unknown fs type `%s'.", optarg);
256 fstype->prepare_options(&fsoptions);
257 break;
258
259 case 'T':
260 if (get_tstamp(optarg, &stampst) == -1)
261 errx(1, "Cannot get timestamp from `%s'",
262 optarg);
263 break;
264
265 case 'x':
266 fsoptions.onlyspec = 1;
267 break;
268
269 case 'Z':
270 /* Superscedes 'p' for compatibility with NetBSD makefs(8) */
271 fsoptions.sparse = 1;
272 break;
273
274 default:
275 usage(fstype, &fsoptions);
276 /* NOTREACHED */
277
278 }
279 }
280 if (debug) {
281 printf("debug mask: 0x%08x\n", debug);
282 printf("start time: %ld.%ld, %s",
283 (long)start_time.tv_sec, (long)start_time.tv_nsec,
284 ctime(&start_time.tv_sec));
285 }
286 argc -= optind;
287 argv += optind;
288
289 if (argc < 2)
290 usage(fstype, &fsoptions);
291
292 /* -x must be accompanied by -F */
293 if (fsoptions.onlyspec != 0 && specfile == NULL)
294 errx(1, "-x requires -F mtree-specfile.");
295
296 /* Accept '-' as meaning "read from standard input". */
297 if (strcmp(argv[1], "-") == 0)
298 sb.st_mode = S_IFREG;
299 else {
300 if (stat(argv[1], &sb) == -1)
301 err(1, "Can't stat `%s'", argv[1]);
302 }
303
304 switch (sb.st_mode & S_IFMT) {
305 case S_IFDIR: /* walk the tree */
306 subtree = argv[1];
307 TIMER_START(start);
308 root = walk_dir(subtree, ".", NULL, NULL);
309 TIMER_RESULTS(start, "walk_dir");
310 break;
311 case S_IFREG: /* read the manifest file */
312 subtree = ".";
313 TIMER_START(start);
314 root = read_mtree(argv[1], NULL);
315 TIMER_RESULTS(start, "manifest");
316 break;
317 default:
318 errx(1, "%s: not a file or directory", argv[1]);
319 /* NOTREACHED */
320 }
321
322 /* append extra directory */
323 for (i = 2; i < argc; i++) {
324 if (stat(argv[i], &sb) == -1)
325 err(1, "Can't stat `%s'", argv[i]);
326 if (!S_ISDIR(sb.st_mode))
327 errx(1, "%s: not a directory", argv[i]);
328 TIMER_START(start);
329 root = walk_dir(argv[i], ".", NULL, root);
330 TIMER_RESULTS(start, "walk_dir2");
331 }
332
333 if (specfile) { /* apply a specfile */
334 TIMER_START(start);
335 apply_specfile(specfile, subtree, root, fsoptions.onlyspec);
336 TIMER_RESULTS(start, "apply_specfile");
337 }
338
339 if (debug & DEBUG_DUMP_FSNODES) {
340 printf("\nparent: %s\n", subtree);
341 dump_fsnodes(root);
342 putchar('\n');
343 }
344
345 /* build the file system */
346 TIMER_START(start);
347 fstype->make_fs(argv[0], subtree, root, &fsoptions);
348 TIMER_RESULTS(start, "make_fs");
349
350 free_fsnodes(root);
351
352 exit(0);
353 /* NOTREACHED */
354 }
355
356 int
set_option(const option_t * options,const char * option,char * buf,size_t len)357 set_option(const option_t *options, const char *option, char *buf, size_t len)
358 {
359 char *var, *val;
360 int retval;
361
362 assert(option != NULL);
363
364 var = estrdup(option);
365 for (val = var; *val; val++)
366 if (*val == '=') {
367 *val++ = '\0';
368 break;
369 }
370 retval = set_option_var(options, var, val, buf, len);
371 free(var);
372 return retval;
373 }
374
375 int
set_option_var(const option_t * options,const char * var,const char * val,char * buf,size_t len)376 set_option_var(const option_t *options, const char *var, const char *val,
377 char *buf, size_t len)
378 {
379 char *s;
380 size_t i;
381
382 #define NUM(type) \
383 if (!*val) { \
384 *(type *)options[i].value = 1; \
385 break; \
386 } \
387 *(type *)options[i].value = (type)strsuftoll(options[i].desc, val, \
388 options[i].minimum, options[i].maximum); break
389
390 for (i = 0; options[i].name != NULL; i++) {
391 if (var[1] == '\0') {
392 if (options[i].letter != var[0])
393 continue;
394 } else if (strcmp(options[i].name, var) != 0)
395 continue;
396 switch (options[i].type) {
397 case OPT_BOOL:
398 *(bool *)options[i].value = 1;
399 break;
400 case OPT_STRARRAY:
401 strlcpy((void *)options[i].value, val, (size_t)
402 options[i].maximum);
403 break;
404 case OPT_STRPTR:
405 s = estrdup(val);
406 *(char **)options[i].value = s;
407 break;
408 case OPT_STRBUF:
409 if (buf == NULL)
410 abort();
411 strlcpy(buf, val, len);
412 break;
413 case OPT_INT64:
414 NUM(uint64_t);
415 case OPT_INT32:
416 NUM(uint32_t);
417 case OPT_INT16:
418 NUM(uint16_t);
419 case OPT_INT8:
420 NUM(uint8_t);
421 default:
422 warnx("Unknown type %d in option %s", options[i].type,
423 val);
424 return 0;
425 }
426 return i;
427 }
428 warnx("Unknown option `%s'", var);
429 return -1;
430 }
431
432
433 static fstype_t *
get_fstype(const char * type)434 get_fstype(const char *type)
435 {
436 int i;
437
438 for (i = 0; fstypes[i].type != NULL; i++)
439 if (strcmp(fstypes[i].type, type) == 0)
440 return (&fstypes[i]);
441 return (NULL);
442 }
443
444 option_t *
copy_opts(const option_t * o)445 copy_opts(const option_t *o)
446 {
447 size_t i;
448
449 for (i = 0; o[i].name; i++)
450 continue;
451 i++;
452 return memcpy(ecalloc(i, sizeof(*o)), o, i * sizeof(*o));
453 }
454
455 static int
get_tstamp(const char * b,struct stat * st)456 get_tstamp(const char *b, struct stat *st)
457 {
458 time_t when;
459 char *eb;
460 long long l;
461
462 if (stat(b, st) != -1)
463 return 0;
464
465 {
466 errno = 0;
467 l = strtoll(b, &eb, 0);
468 if (b == eb || *eb || errno)
469 return -1;
470 when = (time_t)l;
471 }
472
473 st->st_ino = 1;
474 #ifdef HAVE_STRUCT_STAT_BIRTHTIME
475 st->st_birthtime =
476 #endif
477 st->st_mtime = st->st_ctime = st->st_atime = when;
478 return 0;
479 }
480
481 static void
usage(fstype_t * fstype,fsinfo_t * fsoptions)482 usage(fstype_t *fstype, fsinfo_t *fsoptions)
483 {
484 const char *prog;
485
486 prog = getprogname();
487 fprintf(stderr,
488 "Usage: %s [-xZ] [-B endian] [-b free-blocks] [-d debug-mask]\n"
489 "\t[-F mtree-specfile] [-f free-files] [-M minimum-size] [-m maximum-size]\n"
490 "\t[-N userdb-dir] [-O offset] [-o fs-options] [-R roundup-size]\n"
491 "\t[-S sector-size] [-s image-size] [-T <timestamp/file>] [-t fs-type]\n"
492 "\timage-file directory | manifest [extra-directory ...]\n",
493 prog);
494
495 if (fstype) {
496 size_t i;
497 option_t *o = fsoptions->fs_options;
498
499 fprintf(stderr, "\n%s specific options:\n", fstype->type);
500 for (i = 0; o[i].name != NULL; i++)
501 fprintf(stderr, "\t%c%c%20.20s\t%s\n",
502 o[i].letter ? o[i].letter : ' ',
503 o[i].letter ? ',' : ' ',
504 o[i].name, o[i].desc);
505 }
506 exit(1);
507 }
508