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$");
54 * list of supported file systems and dispatch functions
58 void (*prepare_options)(fsinfo_t *);
59 int (*parse_options)(const char *, fsinfo_t *);
60 void (*cleanup_options)(fsinfo_t *);
61 void (*make_fs)(const char *, const char *, fsnode *,
65 static fstype_t fstypes[] = {
66 { "ffs", ffs_prep_opts, ffs_parse_opts, ffs_cleanup_opts, ffs_makefs },
67 { "cd9660", cd9660_prep_opts, cd9660_parse_opts, cd9660_cleanup_opts,
73 struct timespec start_time;
75 static fstype_t *get_fstype(const char *);
76 static void usage(void);
77 int main(int, char *[]);
80 main(int argc, char *argv[])
92 if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
93 errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
95 /* set default fsoptions */
96 (void)memset(&fsoptions, 0, sizeof(fsoptions));
98 fsoptions.sectorsize = -1;
100 if (fstype->prepare_options)
101 fstype->prepare_options(&fsoptions);
104 if (gettimeofday(&start, NULL) == -1)
105 err(1, "Unable to get system time");
107 start_time.tv_sec = start.tv_sec;
108 start_time.tv_nsec = start.tv_usec * 1000;
110 while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:N:o:s:S:t:x")) != -1) {
114 if (strcmp(optarg, "be") == 0 ||
115 strcmp(optarg, "4321") == 0 ||
116 strcmp(optarg, "big") == 0) {
117 #if BYTE_ORDER == LITTLE_ENDIAN
118 fsoptions.needswap = 1;
120 } else if (strcmp(optarg, "le") == 0 ||
121 strcmp(optarg, "1234") == 0 ||
122 strcmp(optarg, "little") == 0) {
123 #if BYTE_ORDER == BIG_ENDIAN
124 fsoptions.needswap = 1;
127 warnx("Invalid endian `%s'.", optarg);
133 len = strlen(optarg) - 1;
134 if (optarg[len] == '%') {
136 fsoptions.freeblockpc =
137 strsuftoll("free block percentage",
140 fsoptions.freeblocks =
141 strsuftoll("free blocks",
142 optarg, 0, LLONG_MAX);
147 debug = strtoll(optarg, NULL, 0);
151 len = strlen(optarg) - 1;
152 if (optarg[len] == '%') {
154 fsoptions.freefilepc =
155 strsuftoll("free file percentage",
158 fsoptions.freefiles =
159 strsuftoll("free files",
160 optarg, 0, LLONG_MAX);
170 strsuftoll("minimum size", optarg, 1LL, LLONG_MAX);
174 if (! setup_getid(optarg))
176 "Unable to use user and group databases in `%s'",
182 strsuftoll("maximum size", optarg, 1LL, LLONG_MAX);
189 while ((p = strsep(&optarg, ",")) != NULL) {
191 errx(1, "Empty option");
192 if (! fstype->parse_options(p, &fsoptions))
199 fsoptions.minsize = fsoptions.maxsize =
200 strsuftoll("size", optarg, 1LL, LLONG_MAX);
204 fsoptions.sectorsize =
205 (int)strsuftoll("sector size", optarg,
210 /* Check current one and cleanup if necessary. */
211 if (fstype->cleanup_options)
212 fstype->cleanup_options(&fsoptions);
213 fsoptions.fs_specific = NULL;
214 if ((fstype = get_fstype(optarg)) == NULL)
215 errx(1, "Unknown fs type `%s'.", optarg);
216 fstype->prepare_options(&fsoptions);
220 fsoptions.onlyspec = 1;
231 printf("debug mask: 0x%08x\n", debug);
232 printf("start time: %ld.%ld, %s",
233 (long)start_time.tv_sec, (long)start_time.tv_nsec,
234 ctime(&start_time.tv_sec));
242 /* -x must be accompanied by -F */
243 if (fsoptions.onlyspec != 0 && specfile == NULL)
244 errx(1, "-x requires -F mtree-specfile.");
248 root = walk_dir(argv[1], NULL);
249 TIMER_RESULTS(start, "walk_dir");
251 if (specfile) { /* apply a specfile */
253 apply_specfile(specfile, argv[1], root, fsoptions.onlyspec);
254 TIMER_RESULTS(start, "apply_specfile");
257 if (debug & DEBUG_DUMP_FSNODES) {
258 printf("\nparent: %s\n", argv[1]);
259 dump_fsnodes(".", root);
263 /* build the file system */
265 fstype->make_fs(argv[0], argv[1], root, &fsoptions);
266 TIMER_RESULTS(start, "make_fs");
276 set_option(option_t *options, const char *var, const char *val)
280 for (i = 0; options[i].name != NULL; i++) {
281 if (strcmp(options[i].name, var) != 0)
283 *options[i].value = (int)strsuftoll(options[i].desc, val,
284 options[i].minimum, options[i].maximum);
287 warnx("Unknown option `%s'", var);
293 get_fstype(const char *type)
297 for (i = 0; fstypes[i].type != NULL; i++)
298 if (strcmp(fstypes[i].type, type) == 0)
299 return (&fstypes[i]);
308 prog = getprogname();
310 "usage: %s [-t fs-type] [-o fs-options] [-d debug-mask] [-B endian]\n"
311 "\t[-S sector-size] [-M minimum-size] [-m maximum-size] [-s image-size]\n"
312 "\t[-b free-blocks] [-f free-files] [-F mtree-specfile] [-x]\n"
313 "\t[-N userdb-dir] image-file directory\n",