1 /* $NetBSD: makefs.c,v 1.25 2006/10/10 01:55:45 dbj 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.25 2006/10/10 01:55:45 dbj Exp $");
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 { "ffs", ffs_prep_opts
, ffs_parse_opts
, ffs_cleanup_opts
, ffs_makefs
},
74 { "cd9660", cd9660_prep_opts
, cd9660_parse_opts
, cd9660_cleanup_opts
,
80 struct timespec start_time
;
82 static fstype_t
*get_fstype(const char *);
83 static void usage(void);
84 int main(int, char *[]);
87 main(int argc
, char *argv
[])
99 if ((fstype
= get_fstype(DEFAULT_FSTYPE
)) == NULL
)
100 errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE
);
102 /* set default fsoptions */
103 (void)memset(&fsoptions
, 0, sizeof(fsoptions
));
105 fsoptions
.sectorsize
= -1;
107 if (fstype
->prepare_options
)
108 fstype
->prepare_options(&fsoptions
);
111 if (gettimeofday(&start
, NULL
) == -1)
112 err(1, "Unable to get system time");
114 start_time
.tv_sec
= start
.tv_sec
;
115 start_time
.tv_nsec
= start
.tv_usec
* 1000;
117 while ((ch
= getopt(argc
, argv
, "B:b:d:f:F:M:m:N:o:s:S:t:x")) != -1) {
121 if (strcmp(optarg
, "be") == 0 ||
122 strcmp(optarg
, "4321") == 0 ||
123 strcmp(optarg
, "big") == 0) {
124 #if BYTE_ORDER == LITTLE_ENDIAN
125 fsoptions
.needswap
= 1;
127 } else if (strcmp(optarg
, "le") == 0 ||
128 strcmp(optarg
, "1234") == 0 ||
129 strcmp(optarg
, "little") == 0) {
130 #if BYTE_ORDER == BIG_ENDIAN
131 fsoptions
.needswap
= 1;
134 warnx("Invalid endian `%s'.", optarg
);
140 len
= strlen(optarg
) - 1;
141 if (optarg
[len
] == '%') {
143 fsoptions
.freeblockpc
=
144 strsuftoll("free block percentage",
147 fsoptions
.freeblocks
=
148 strsuftoll("free blocks",
149 optarg
, 0, LLONG_MAX
);
154 debug
= strtoll(optarg
, NULL
, 0);
158 len
= strlen(optarg
) - 1;
159 if (optarg
[len
] == '%') {
161 fsoptions
.freefilepc
=
162 strsuftoll("free file percentage",
165 fsoptions
.freefiles
=
166 strsuftoll("free files",
167 optarg
, 0, LLONG_MAX
);
177 strsuftoll("minimum size", optarg
, 1LL, LLONG_MAX
);
181 if (! setup_getid(optarg
))
183 "Unable to use user and group databases in `%s'",
189 strsuftoll("maximum size", optarg
, 1LL, LLONG_MAX
);
196 while ((p
= strsep(&optarg
, ",")) != NULL
) {
198 errx(1, "Empty option");
199 if (! fstype
->parse_options(p
, &fsoptions
))
206 fsoptions
.minsize
= fsoptions
.maxsize
=
207 strsuftoll("size", optarg
, 1LL, LLONG_MAX
);
211 fsoptions
.sectorsize
=
212 (int)strsuftoll("sector size", optarg
,
217 /* Check current one and cleanup if necessary. */
218 if (fstype
->cleanup_options
)
219 fstype
->cleanup_options(&fsoptions
);
220 fsoptions
.fs_specific
= NULL
;
221 if ((fstype
= get_fstype(optarg
)) == NULL
)
222 errx(1, "Unknown fs type `%s'.", optarg
);
223 fstype
->prepare_options(&fsoptions
);
227 fsoptions
.onlyspec
= 1;
238 printf("debug mask: 0x%08x\n", debug
);
239 printf("start time: %ld.%ld, %s",
240 (long)start_time
.tv_sec
, (long)start_time
.tv_nsec
,
241 ctime(&start_time
.tv_sec
));
249 /* -x must be accompanied by -F */
250 if (fsoptions
.onlyspec
!= 0 && specfile
== NULL
)
251 errx(1, "-x requires -F mtree-specfile.");
255 root
= walk_dir(argv
[1], NULL
);
256 TIMER_RESULTS(start
, "walk_dir");
258 if (specfile
) { /* apply a specfile */
260 apply_specfile(specfile
, argv
[1], root
, fsoptions
.onlyspec
);
261 TIMER_RESULTS(start
, "apply_specfile");
264 if (debug
& DEBUG_DUMP_FSNODES
) {
265 printf("\nparent: %s\n", argv
[1]);
266 dump_fsnodes(".", root
);
270 /* build the file system */
272 fstype
->make_fs(argv
[0], argv
[1], root
, &fsoptions
);
273 TIMER_RESULTS(start
, "make_fs");
283 set_option(option_t
*options
, const char *var
, const char *val
)
287 for (i
= 0; options
[i
].name
!= NULL
; i
++) {
288 if (strcmp(options
[i
].name
, var
) != 0)
290 *options
[i
].value
= (int)strsuftoll(options
[i
].desc
, val
,
291 options
[i
].minimum
, options
[i
].maximum
);
294 warnx("Unknown option `%s'", var
);
300 get_fstype(const char *type
)
304 for (i
= 0; fstypes
[i
].type
!= NULL
; i
++)
305 if (strcmp(fstypes
[i
].type
, type
) == 0)
306 return (&fstypes
[i
]);
315 prog
= getprogname();
317 "usage: %s [-t fs-type] [-o fs-options] [-d debug-mask] [-B endian]\n"
318 "\t[-S sector-size] [-M minimum-size] [-m maximum-size] [-s image-size]\n"
319 "\t[-b free-blocks] [-f free-files] [-F mtree-specfile] [-x]\n"
320 "\t[-N userdb-dir] image-file directory\n",