1 /* $NetBSD: makefs.c,v 1.50 2013/08/05 14:41:57 reinoud Exp $ */
4 * Copyright (c) 2001-2003 Wasabi Systems, Inc.
7 * Written by Luke Mewburn for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 #if HAVE_NBTOOL_CONFIG_H
39 #include "nbtool_config.h"
42 #include <sys/cdefs.h>
43 #if defined(__RCSID) && !defined(__lint)
44 __RCSID("$NetBSD: makefs.c,v 1.50 2013/08/05 14:41:57 reinoud Exp $");
63 * list of supported file systems and dispatch functions
67 void (*prepare_options
)(fsinfo_t
*);
68 int (*parse_options
)(const char *, fsinfo_t
*);
69 void (*cleanup_options
)(fsinfo_t
*);
70 void (*make_fs
)(const char *, const char *, fsnode
*,
74 static fstype_t fstypes
[] = {
75 #define ENTRY(name) { \
76 # name, name ## _prep_opts, name ## _parse_opts, \
77 name ## _cleanup_opts, name ## _makefs \
89 struct timespec start_time
;
91 static fstype_t
*get_fstype(const char *);
92 static void usage(fstype_t
*, fsinfo_t
*) __dead
;
95 main(int argc
, char *argv
[])
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 if (gettimeofday(&start
, NULL
) == -1)
120 err(1, "Unable to get system time");
122 start_time
.tv_sec
= start
.tv_sec
;
123 start_time
.tv_nsec
= start
.tv_usec
* 1000;
125 while ((ch
= getopt(argc
, argv
, "B:b:d:f:F:M:m:N:O:o:rs:S:t:xZ")) != -1) {
129 if (strcmp(optarg
, "be") == 0 ||
130 strcmp(optarg
, "4321") == 0 ||
131 strcmp(optarg
, "big") == 0) {
132 #if BYTE_ORDER == LITTLE_ENDIAN
133 fsoptions
.needswap
= 1;
135 } else if (strcmp(optarg
, "le") == 0 ||
136 strcmp(optarg
, "1234") == 0 ||
137 strcmp(optarg
, "little") == 0) {
138 #if BYTE_ORDER == BIG_ENDIAN
139 fsoptions
.needswap
= 1;
142 warnx("Invalid endian `%s'.", optarg
);
143 usage(fstype
, &fsoptions
);
148 len
= strlen(optarg
) - 1;
149 if (optarg
[len
] == '%') {
151 fsoptions
.freeblockpc
=
152 strsuftoll("free block percentage",
155 fsoptions
.freeblocks
=
156 strsuftoll("free blocks",
157 optarg
, 0, LLONG_MAX
);
162 debug
= strtoll(optarg
, NULL
, 0);
166 len
= strlen(optarg
) - 1;
167 if (optarg
[len
] == '%') {
169 fsoptions
.freefilepc
=
170 strsuftoll("free file percentage",
173 fsoptions
.freefiles
=
174 strsuftoll("free files",
175 optarg
, 0, LLONG_MAX
);
185 strsuftoll("minimum size", optarg
, 1LL, LLONG_MAX
);
189 if (! setup_getid(optarg
))
191 "Unable to use user and group databases in `%s'",
197 strsuftoll("maximum size", optarg
, 1LL, LLONG_MAX
);
202 strsuftoll("offset", optarg
, 0LL, LLONG_MAX
);
209 while ((p
= strsep(&optarg
, ",")) != NULL
) {
211 errx(1, "Empty option");
212 if (! fstype
->parse_options(p
, &fsoptions
))
213 usage(fstype
, &fsoptions
);
219 fsoptions
.replace
= 1;
223 fsoptions
.minsize
= fsoptions
.maxsize
=
224 strsuftoll("size", optarg
, 1LL, LLONG_MAX
);
228 fsoptions
.sectorsize
=
229 (int)strsuftoll("sector size", optarg
,
234 /* Check current one and cleanup if necessary. */
235 if (fstype
->cleanup_options
)
236 fstype
->cleanup_options(&fsoptions
);
237 fsoptions
.fs_specific
= NULL
;
238 if ((fstype
= get_fstype(optarg
)) == NULL
)
239 errx(1, "Unknown fs type `%s'.", optarg
);
240 fstype
->prepare_options(&fsoptions
);
244 fsoptions
.onlyspec
= 1;
248 fsoptions
.sparse
= 1;
253 usage(fstype
, &fsoptions
);
259 printf("debug mask: 0x%08x\n", debug
);
260 printf("start time: %ld.%ld, %s",
261 (long)start_time
.tv_sec
, (long)start_time
.tv_nsec
,
262 ctime(&start_time
.tv_sec
));
268 usage(fstype
, &fsoptions
);
270 /* -x must be accompanied by -F */
271 if (fsoptions
.onlyspec
!= 0 && specfile
== NULL
)
272 errx(1, "-x requires -F mtree-specfile.");
276 root
= walk_dir(argv
[1], ".", NULL
, NULL
, fsoptions
.replace
);
277 TIMER_RESULTS(start
, "walk_dir");
279 /* append extra directory */
280 for (i
= 2; i
< argc
; i
++) {
282 if (stat(argv
[i
], &sb
) == -1)
283 err(1, "Can't stat `%s'", argv
[i
]);
284 if (!S_ISDIR(sb
.st_mode
))
285 errx(1, "%s: not a directory", argv
[i
]);
287 root
= walk_dir(argv
[i
], ".", NULL
, root
, fsoptions
.replace
);
288 TIMER_RESULTS(start
, "walk_dir2");
291 if (specfile
) { /* apply a specfile */
293 apply_specfile(specfile
, argv
[1], root
, fsoptions
.onlyspec
);
294 TIMER_RESULTS(start
, "apply_specfile");
297 if (debug
& DEBUG_DUMP_FSNODES
) {
298 printf("\nparent: %s\n", argv
[1]);
303 /* build the file system */
305 fstype
->make_fs(argv
[0], argv
[1], root
, &fsoptions
);
306 TIMER_RESULTS(start
, "make_fs");
315 set_option(const option_t
*options
, const char *option
, char *buf
, size_t len
)
320 assert(option
!= NULL
);
322 var
= estrdup(option
);
323 for (val
= var
; *val
; val
++)
328 retval
= set_option_var(options
, var
, val
, buf
, len
);
334 set_option_var(const option_t
*options
, const char *var
, const char *val
,
335 char *buf
, size_t len
)
342 *(type *)options[i].value = 1; \
345 *(type *)options[i].value = (type)strsuftoll(options[i].desc, val, \
346 options[i].minimum, options[i].maximum); break
348 for (i
= 0; options
[i
].name
!= NULL
; i
++) {
349 if (var
[1] == '\0') {
350 if (options
[i
].letter
!= var
[0])
352 } else if (strcmp(options
[i
].name
, var
) != 0)
354 switch (options
[i
].type
) {
356 *(bool *)options
[i
].value
= 1;
359 strlcpy((void *)options
[i
].value
, val
, (size_t)
364 *(char **)options
[i
].value
= s
;
369 strlcpy(buf
, val
, len
);
380 warnx("Unknown type %d in option %s", options
[i
].type
,
386 warnx("Unknown option `%s'", var
);
392 get_fstype(const char *type
)
396 for (i
= 0; fstypes
[i
].type
!= NULL
; i
++)
397 if (strcmp(fstypes
[i
].type
, type
) == 0)
398 return (&fstypes
[i
]);
403 copy_opts(const option_t
*o
)
406 for (i
= 0; o
[i
].name
; i
++)
409 return memcpy(ecalloc(i
, sizeof(*o
)), o
, i
* sizeof(*o
));
413 usage(fstype_t
*fstype
, fsinfo_t
*fsoptions
)
417 prog
= getprogname();
419 "Usage: %s [-rxZ] [-B endian] [-b free-blocks] [-d debug-mask]\n"
420 "\t[-F mtree-specfile] [-f free-files] [-M minimum-size] [-m maximum-size]\n"
421 "\t[-N userdb-dir] [-O offset] [-o fs-options] [-S sector-size]\n"
422 "\t[-s image-size] [-t fs-type] image-file directory [extra-directory ...]\n",
427 option_t
*o
= fsoptions
->fs_options
;
429 fprintf(stderr
, "\n%s specific options:\n", fstype
->type
);
430 for (i
= 0; o
[i
].name
!= NULL
; i
++)
431 fprintf(stderr
, "\t%c%c%20.20s\t%s\n",
432 o
[i
].letter
? o
[i
].letter
: ' ',
433 o
[i
].letter
? ',' : ' ',
434 o
[i
].name
, o
[i
].desc
);