1 /* $NetBSD: makefs.c,v 1.26 2006/10/22 21:11:56 christos 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 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/types.h>
57 * list of supported file systems and dispatch functions
61 void (*prepare_options)(fsinfo_t *);
62 int (*parse_options)(const char *, fsinfo_t *);
63 void (*cleanup_options)(fsinfo_t *);
64 void (*make_fs)(const char *, const char *, fsnode *,
68 static fstype_t fstypes[] = {
69 #define ENTRY(name) { \
70 # name, name ## _prep_opts, name ## _parse_opts, \
71 name ## _cleanup_opts, name ## _makefs \
80 struct timespec start_time;
83 static fstype_t *get_fstype(const char *);
84 static int get_tstamp(const char *, struct stat *);
85 static void usage(void);
86 int main(int, char *[]);
89 main(int argc, char *argv[])
100 setprogname(argv[0]);
103 if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
104 errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
106 /* set default fsoptions */
107 (void)memset(&fsoptions, 0, sizeof(fsoptions));
109 fsoptions.sectorsize = -1;
111 if (fstype->prepare_options)
112 fstype->prepare_options(&fsoptions);
115 ch = gettimeofday(&start, NULL);
116 start_time.tv_sec = start.tv_sec;
117 start_time.tv_nsec = start.tv_usec * 1000;
120 err(1, "Unable to get system time");
123 while ((ch = getopt(argc, argv, "B:b:Dd:f:F:M:m:N:o:pR:s:S:t:T:xZ")) != -1) {
127 if (strcmp(optarg, "be") == 0 ||
128 strcmp(optarg, "4321") == 0 ||
129 strcmp(optarg, "big") == 0) {
130 #if BYTE_ORDER == LITTLE_ENDIAN
131 fsoptions.needswap = 1;
133 } else if (strcmp(optarg, "le") == 0 ||
134 strcmp(optarg, "1234") == 0 ||
135 strcmp(optarg, "little") == 0) {
136 #if BYTE_ORDER == BIG_ENDIAN
137 fsoptions.needswap = 1;
140 warnx("Invalid endian `%s'.", optarg);
146 len = strlen(optarg) - 1;
147 if (optarg[len] == '%') {
149 fsoptions.freeblockpc =
150 strsuftoll("free block percentage",
153 fsoptions.freeblocks =
154 strsuftoll("free blocks",
155 optarg, 0, LLONG_MAX);
164 debug = strtoll(optarg, NULL, 0);
168 len = strlen(optarg) - 1;
169 if (optarg[len] == '%') {
171 fsoptions.freefilepc =
172 strsuftoll("free file percentage",
175 fsoptions.freefiles =
176 strsuftoll("free files",
177 optarg, 0, LLONG_MAX);
187 strsuftoll("minimum size", optarg, 1LL, LLONG_MAX);
191 if (! setup_getid(optarg))
193 "Unable to use user and group databases in `%s'",
199 strsuftoll("maximum size", optarg, 1LL, LLONG_MAX);
206 while ((p = strsep(&optarg, ",")) != NULL) {
208 errx(1, "Empty option");
209 if (! fstype->parse_options(p, &fsoptions))
215 /* Deprecated in favor of 'Z' */
216 fsoptions.sparse = 1;
220 /* Round image size up to specified block size */
222 strsuftoll("roundup-size", optarg, 0, LLONG_MAX);
226 fsoptions.minsize = fsoptions.maxsize =
227 strsuftoll("size", optarg, 1LL, LLONG_MAX);
231 fsoptions.sectorsize =
232 (int)strsuftoll("sector size", optarg,
237 /* Check current one and cleanup if necessary. */
238 if (fstype->cleanup_options)
239 fstype->cleanup_options(&fsoptions);
240 fsoptions.fs_specific = NULL;
241 if ((fstype = get_fstype(optarg)) == NULL)
242 errx(1, "Unknown fs type `%s'.", optarg);
243 fstype->prepare_options(&fsoptions);
247 if (get_tstamp(optarg, &stampst) == -1)
248 errx(1, "Cannot get timestamp from `%s'",
253 fsoptions.onlyspec = 1;
257 /* Superscedes 'p' for compatibility with NetBSD makefs(8) */
258 fsoptions.sparse = 1;
269 printf("debug mask: 0x%08x\n", debug);
270 printf("start time: %ld.%ld, %s",
271 (long)start_time.tv_sec, (long)start_time.tv_nsec,
272 ctime(&start_time.tv_sec));
280 /* -x must be accompanied by -F */
281 if (fsoptions.onlyspec != 0 && specfile == NULL)
282 errx(1, "-x requires -F mtree-specfile.");
284 /* Accept '-' as meaning "read from standard input". */
285 if (strcmp(argv[1], "-") == 0)
286 sb.st_mode = S_IFREG;
288 if (stat(argv[1], &sb) == -1)
289 err(1, "Can't stat `%s'", argv[1]);
292 switch (sb.st_mode & S_IFMT) {
293 case S_IFDIR: /* walk the tree */
296 root = walk_dir(subtree, ".", NULL, NULL);
297 TIMER_RESULTS(start, "walk_dir");
299 case S_IFREG: /* read the manifest file */
302 root = read_mtree(argv[1], NULL);
303 TIMER_RESULTS(start, "manifest");
306 errx(1, "%s: not a file or directory", argv[1]);
310 /* append extra directory */
311 for (i = 2; i < argc; i++) {
312 if (stat(argv[i], &sb) == -1)
313 err(1, "Can't stat `%s'", argv[i]);
314 if (!S_ISDIR(sb.st_mode))
315 errx(1, "%s: not a directory", argv[i]);
317 root = walk_dir(argv[i], ".", NULL, root);
318 TIMER_RESULTS(start, "walk_dir2");
321 if (specfile) { /* apply a specfile */
323 apply_specfile(specfile, subtree, root, fsoptions.onlyspec);
324 TIMER_RESULTS(start, "apply_specfile");
327 if (debug & DEBUG_DUMP_FSNODES) {
328 printf("\nparent: %s\n", subtree);
333 /* build the file system */
335 fstype->make_fs(argv[0], subtree, root, &fsoptions);
336 TIMER_RESULTS(start, "make_fs");
346 set_option(option_t *options, const char *var, const char *val)
350 for (i = 0; options[i].name != NULL; i++) {
351 if (strcmp(options[i].name, var) != 0)
353 *options[i].value = (int)strsuftoll(options[i].desc, val,
354 options[i].minimum, options[i].maximum);
357 warnx("Unknown option `%s'", var);
363 get_fstype(const char *type)
367 for (i = 0; fstypes[i].type != NULL; i++)
368 if (strcmp(fstypes[i].type, type) == 0)
369 return (&fstypes[i]);
374 get_tstamp(const char *b, struct stat *st)
380 if (stat(b, st) != -1)
385 l = strtoll(b, &eb, 0);
386 if (b == eb || *eb || errno)
392 #ifdef HAVE_STRUCT_STAT_BIRTHTIME
395 st->st_mtime = st->st_ctime = st->st_atime = when;
404 prog = getprogname();
406 "usage: %s [-xZ] [-B endian] [-b free-blocks] [-d debug-mask]\n"
407 "\t[-F mtree-specfile] [-f free-files] [-M minimum-size] [-m maximum-size]\n"
408 "\t[-N userdb-dir] [-o fs-options] [-R roundup-size] [-S sector-size]\n"
409 "\t[-s image-size] [-T <timestamp/file>] [-t fs-type]\n"
410 "\timage-file directory | manifest [extra-directory ...]\n",