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 { "ffs", ffs_prep_opts, ffs_parse_opts, ffs_cleanup_opts, ffs_makefs },
70 { "cd9660", cd9660_prep_opts, cd9660_parse_opts, cd9660_cleanup_opts,
77 struct timespec start_time;
79 static fstype_t *get_fstype(const char *);
80 static void usage(void);
81 int main(int, char *[]);
84 main(int argc, char *argv[])
98 if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
99 errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
101 /* set default fsoptions */
102 (void)memset(&fsoptions, 0, sizeof(fsoptions));
104 fsoptions.sectorsize = -1;
106 if (fstype->prepare_options)
107 fstype->prepare_options(&fsoptions);
110 if (gettimeofday(&start, NULL) == -1)
111 err(1, "Unable to get system time");
113 start_time.tv_sec = start.tv_sec;
114 start_time.tv_nsec = start.tv_usec * 1000;
116 while ((ch = getopt(argc, argv, "B:b:Dd:f:F:M:m:N:o:pR:s:S:t:xZ")) != -1) {
120 if (strcmp(optarg, "be") == 0 ||
121 strcmp(optarg, "4321") == 0 ||
122 strcmp(optarg, "big") == 0) {
123 #if BYTE_ORDER == LITTLE_ENDIAN
124 fsoptions.needswap = 1;
126 } else if (strcmp(optarg, "le") == 0 ||
127 strcmp(optarg, "1234") == 0 ||
128 strcmp(optarg, "little") == 0) {
129 #if BYTE_ORDER == BIG_ENDIAN
130 fsoptions.needswap = 1;
133 warnx("Invalid endian `%s'.", optarg);
139 len = strlen(optarg) - 1;
140 if (optarg[len] == '%') {
142 fsoptions.freeblockpc =
143 strsuftoll("free block percentage",
146 fsoptions.freeblocks =
147 strsuftoll("free blocks",
148 optarg, 0, LLONG_MAX);
157 debug = strtoll(optarg, NULL, 0);
161 len = strlen(optarg) - 1;
162 if (optarg[len] == '%') {
164 fsoptions.freefilepc =
165 strsuftoll("free file percentage",
168 fsoptions.freefiles =
169 strsuftoll("free files",
170 optarg, 0, LLONG_MAX);
180 strsuftoll("minimum size", optarg, 1LL, LLONG_MAX);
184 if (! setup_getid(optarg))
186 "Unable to use user and group databases in `%s'",
192 strsuftoll("maximum size", optarg, 1LL, LLONG_MAX);
199 while ((p = strsep(&optarg, ",")) != NULL) {
201 errx(1, "Empty option");
202 if (! fstype->parse_options(p, &fsoptions))
208 /* Deprecated in favor of 'Z' */
209 fsoptions.sparse = 1;
213 /* Round image size up to specified block size */
215 strsuftoll("roundup-size", optarg, 0, LLONG_MAX);
219 fsoptions.minsize = fsoptions.maxsize =
220 strsuftoll("size", optarg, 1LL, LLONG_MAX);
224 fsoptions.sectorsize =
225 (int)strsuftoll("sector size", optarg,
230 /* Check current one and cleanup if necessary. */
231 if (fstype->cleanup_options)
232 fstype->cleanup_options(&fsoptions);
233 fsoptions.fs_specific = NULL;
234 if ((fstype = get_fstype(optarg)) == NULL)
235 errx(1, "Unknown fs type `%s'.", optarg);
236 fstype->prepare_options(&fsoptions);
240 fsoptions.onlyspec = 1;
244 /* Superscedes 'p' for compatibility with NetBSD makefs(8) */
245 fsoptions.sparse = 1;
256 printf("debug mask: 0x%08x\n", debug);
257 printf("start time: %ld.%ld, %s",
258 (long)start_time.tv_sec, (long)start_time.tv_nsec,
259 ctime(&start_time.tv_sec));
267 /* -x must be accompanied by -F */
268 if (fsoptions.onlyspec != 0 && specfile == NULL)
269 errx(1, "-x requires -F mtree-specfile.");
271 /* Accept '-' as meaning "read from standard input". */
272 if (strcmp(argv[1], "-") == 0)
273 sb.st_mode = S_IFREG;
275 if (stat(argv[1], &sb) == -1)
276 err(1, "Can't stat `%s'", argv[1]);
279 switch (sb.st_mode & S_IFMT) {
280 case S_IFDIR: /* walk the tree */
283 root = walk_dir(subtree, ".", NULL, NULL);
284 TIMER_RESULTS(start, "walk_dir");
286 case S_IFREG: /* read the manifest file */
289 root = read_mtree(argv[1], NULL);
290 TIMER_RESULTS(start, "manifest");
293 errx(1, "%s: not a file or directory", argv[1]);
297 /* append extra directory */
298 for (i = 2; i < argc; i++) {
299 if (stat(argv[i], &sb) == -1)
300 err(1, "Can't stat `%s'", argv[i]);
301 if (!S_ISDIR(sb.st_mode))
302 errx(1, "%s: not a directory", argv[i]);
304 root = walk_dir(argv[i], ".", NULL, root);
305 TIMER_RESULTS(start, "walk_dir2");
308 if (specfile) { /* apply a specfile */
310 apply_specfile(specfile, subtree, root, fsoptions.onlyspec);
311 TIMER_RESULTS(start, "apply_specfile");
314 if (debug & DEBUG_DUMP_FSNODES) {
315 printf("\nparent: %s\n", subtree);
320 /* build the file system */
322 fstype->make_fs(argv[0], subtree, root, &fsoptions);
323 TIMER_RESULTS(start, "make_fs");
333 set_option(option_t *options, const char *var, const char *val)
337 for (i = 0; options[i].name != NULL; i++) {
338 if (strcmp(options[i].name, var) != 0)
340 *options[i].value = (int)strsuftoll(options[i].desc, val,
341 options[i].minimum, options[i].maximum);
344 warnx("Unknown option `%s'", var);
350 get_fstype(const char *type)
354 for (i = 0; fstypes[i].type != NULL; i++)
355 if (strcmp(fstypes[i].type, type) == 0)
356 return (&fstypes[i]);
365 prog = getprogname();
367 "usage: %s [-t fs-type] [-o fs-options] [-d debug-mask] [-B endian]\n"
368 "\t[-S sector-size] [-M minimum-size] [-m maximum-size] [-R roundup-size]\n"
369 "\t[-s image-size] [-b free-blocks] [-f free-files] [-F mtree-specfile]\n"
370 "\t[-xZ] [-N userdb-dir] image-file directory | manifest [extra-directory ...]\n",