1 /* $NetBSD: makefs.c,v 1.26 2006/10/22 21:11:56 christos Exp $ */
4 * SPDX-License-Identifier: BSD-4-Clause
6 * Copyright (c) 2001-2003 Wasabi Systems, Inc.
9 * Written by Luke Mewburn for Wasabi Systems, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/types.h>
61 * list of supported file systems and dispatch functions
65 void (*prepare_options)(fsinfo_t *);
66 int (*parse_options)(const char *, fsinfo_t *);
67 void (*cleanup_options)(fsinfo_t *);
68 void (*make_fs)(const char *, const char *, fsnode *,
72 static fstype_t fstypes[] = {
73 #define ENTRY(name) { \
74 # name, name ## _prep_opts, name ## _parse_opts, \
75 name ## _cleanup_opts, name ## _makefs \
85 struct timespec start_time;
88 static fstype_t *get_fstype(const char *);
89 static int get_tstamp(const char *, struct stat *);
90 static void usage(fstype_t *, fsinfo_t *);
93 main(int argc, char *argv[])
102 const char *specfile;
104 setprogname(argv[0]);
107 if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
108 errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
110 /* set default fsoptions */
111 (void)memset(&fsoptions, 0, sizeof(fsoptions));
113 fsoptions.sectorsize = -1;
115 if (fstype->prepare_options)
116 fstype->prepare_options(&fsoptions);
119 #ifdef CLOCK_REALTIME
120 ch = clock_gettime(CLOCK_REALTIME, &start_time);
122 ch = gettimeofday(&start, NULL);
123 start_time.tv_sec = start.tv_sec;
124 start_time.tv_nsec = start.tv_usec * 1000;
127 err(1, "Unable to get system time");
130 while ((ch = getopt(argc, argv, "B:b:Dd:f:F:M:m:N:O:o:pR:s:S:t:T:xZ")) != -1) {
134 if (strcmp(optarg, "be") == 0 ||
135 strcmp(optarg, "4321") == 0 ||
136 strcmp(optarg, "big") == 0) {
137 #if BYTE_ORDER == LITTLE_ENDIAN
138 fsoptions.needswap = 1;
140 } else if (strcmp(optarg, "le") == 0 ||
141 strcmp(optarg, "1234") == 0 ||
142 strcmp(optarg, "little") == 0) {
143 #if BYTE_ORDER == BIG_ENDIAN
144 fsoptions.needswap = 1;
147 warnx("Invalid endian `%s'.", optarg);
148 usage(fstype, &fsoptions);
153 len = strlen(optarg) - 1;
154 if (optarg[len] == '%') {
156 fsoptions.freeblockpc =
157 strsuftoll("free block percentage",
160 fsoptions.freeblocks =
161 strsuftoll("free blocks",
162 optarg, 0, LLONG_MAX);
171 debug = strtoll(optarg, NULL, 0);
175 len = strlen(optarg) - 1;
176 if (optarg[len] == '%') {
178 fsoptions.freefilepc =
179 strsuftoll("free file percentage",
182 fsoptions.freefiles =
183 strsuftoll("free files",
184 optarg, 0, LLONG_MAX);
194 strsuftoll("minimum size", optarg, 1LL, LLONG_MAX);
198 if (! setup_getid(optarg))
200 "Unable to use user and group databases in `%s'",
206 strsuftoll("maximum size", optarg, 1LL, LLONG_MAX);
211 strsuftoll("offset", optarg, 0LL, LLONG_MAX);
218 while ((p = strsep(&optarg, ",")) != NULL) {
220 errx(1, "Empty option");
221 if (! fstype->parse_options(p, &fsoptions))
222 usage(fstype, &fsoptions);
227 /* Deprecated in favor of 'Z' */
228 fsoptions.sparse = 1;
232 /* Round image size up to specified block size */
234 strsuftoll("roundup-size", optarg, 0, LLONG_MAX);
238 fsoptions.minsize = fsoptions.maxsize =
239 strsuftoll("size", optarg, 1LL, LLONG_MAX);
243 fsoptions.sectorsize =
244 (int)strsuftoll("sector size", optarg,
249 /* Check current one and cleanup if necessary. */
250 if (fstype->cleanup_options)
251 fstype->cleanup_options(&fsoptions);
252 fsoptions.fs_specific = NULL;
253 if ((fstype = get_fstype(optarg)) == NULL)
254 errx(1, "Unknown fs type `%s'.", optarg);
255 fstype->prepare_options(&fsoptions);
259 if (get_tstamp(optarg, &stampst) == -1)
260 errx(1, "Cannot get timestamp from `%s'",
265 fsoptions.onlyspec = 1;
269 /* Superscedes 'p' for compatibility with NetBSD makefs(8) */
270 fsoptions.sparse = 1;
275 usage(fstype, &fsoptions);
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));
290 usage(fstype, &fsoptions);
292 /* -x must be accompanied by -F */
293 if (fsoptions.onlyspec != 0 && specfile == NULL)
294 errx(1, "-x requires -F mtree-specfile.");
296 /* Accept '-' as meaning "read from standard input". */
297 if (strcmp(argv[1], "-") == 0)
298 sb.st_mode = S_IFREG;
300 if (stat(argv[1], &sb) == -1)
301 err(1, "Can't stat `%s'", argv[1]);
304 switch (sb.st_mode & S_IFMT) {
305 case S_IFDIR: /* walk the tree */
308 root = walk_dir(subtree, ".", NULL, NULL);
309 TIMER_RESULTS(start, "walk_dir");
311 case S_IFREG: /* read the manifest file */
314 root = read_mtree(argv[1], NULL);
315 TIMER_RESULTS(start, "manifest");
318 errx(1, "%s: not a file or directory", argv[1]);
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]);
329 root = walk_dir(argv[i], ".", NULL, root);
330 TIMER_RESULTS(start, "walk_dir2");
333 if (specfile) { /* apply a specfile */
335 apply_specfile(specfile, subtree, root, fsoptions.onlyspec);
336 TIMER_RESULTS(start, "apply_specfile");
339 if (debug & DEBUG_DUMP_FSNODES) {
340 printf("\nparent: %s\n", subtree);
345 /* build the file system */
347 fstype->make_fs(argv[0], subtree, root, &fsoptions);
348 TIMER_RESULTS(start, "make_fs");
357 set_option(const option_t *options, const char *option, char *buf, size_t len)
362 assert(option != NULL);
364 var = estrdup(option);
365 for (val = var; *val; val++)
370 retval = set_option_var(options, var, val, buf, len);
376 set_option_var(const option_t *options, const char *var, const char *val,
377 char *buf, size_t len)
384 *(type *)options[i].value = 1; \
387 *(type *)options[i].value = (type)strsuftoll(options[i].desc, val, \
388 options[i].minimum, options[i].maximum); break
390 for (i = 0; options[i].name != NULL; i++) {
391 if (var[1] == '\0') {
392 if (options[i].letter != var[0])
394 } else if (strcmp(options[i].name, var) != 0)
396 switch (options[i].type) {
398 *(bool *)options[i].value = 1;
401 strlcpy((void *)options[i].value, val, (size_t)
406 *(char **)options[i].value = s;
411 strlcpy(buf, val, len);
422 warnx("Unknown type %d in option %s", options[i].type,
428 warnx("Unknown option `%s'", var);
434 get_fstype(const char *type)
438 for (i = 0; fstypes[i].type != NULL; i++)
439 if (strcmp(fstypes[i].type, type) == 0)
440 return (&fstypes[i]);
445 copy_opts(const option_t *o)
449 for (i = 0; o[i].name; i++)
452 return memcpy(ecalloc(i, sizeof(*o)), o, i * sizeof(*o));
456 get_tstamp(const char *b, struct stat *st)
462 if (stat(b, st) != -1)
467 l = strtoll(b, &eb, 0);
468 if (b == eb || *eb || errno)
474 #ifdef HAVE_STRUCT_STAT_BIRTHTIME
477 st->st_mtime = st->st_ctime = st->st_atime = when;
482 usage(fstype_t *fstype, fsinfo_t *fsoptions)
486 prog = getprogname();
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",
497 option_t *o = fsoptions->fs_options;
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);