makefs.c revision 332981
1/*	$NetBSD: makefs.c,v 1.26 2006/10/22 21:11:56 christos Exp $	*/
2
3/*-
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (c) 2001-2003 Wasabi Systems, Inc.
7 * All rights reserved.
8 *
9 * Written by Luke Mewburn for Wasabi Systems, Inc.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *      This product includes software developed for the NetBSD Project by
22 *      Wasabi Systems, Inc.
23 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
24 *    or promote products derived from this software without specific prior
25 *    written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: stable/11/usr.sbin/makefs/makefs.c 332981 2018-04-25 01:48:15Z benno $");
42
43#include <sys/types.h>
44#include <sys/stat.h>
45#include <assert.h>
46#include <ctype.h>
47#include <errno.h>
48#include <limits.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <time.h>
53#include <unistd.h>
54#include <stdbool.h>
55#include <util.h>
56
57#include "makefs.h"
58#include "mtree.h"
59
60/*
61 * list of supported file systems and dispatch functions
62 */
63typedef struct {
64	const char	*type;
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 *,
69				fsinfo_t *);
70} fstype_t;
71
72static fstype_t fstypes[] = {
73#define ENTRY(name) { \
74	# name, name ## _prep_opts, name ## _parse_opts, \
75	name ## _cleanup_opts, name ## _makefs  \
76}
77	ENTRY(ffs),
78	ENTRY(cd9660),
79	{ .type = NULL	},
80};
81
82u_int		debug;
83int		dupsok;
84struct timespec	start_time;
85struct stat stampst;
86
87static	fstype_t *get_fstype(const char *);
88static int get_tstamp(const char *, struct stat *);
89static	void	usage(fstype_t *, fsinfo_t *);
90int		main(int, char *[]);
91
92int
93main(int argc, char *argv[])
94{
95	struct stat	 sb;
96	struct timeval	 start;
97	fstype_t	*fstype;
98	fsinfo_t	 fsoptions;
99	fsnode		*root;
100	int	 	 ch, i, len;
101	char		*subtree;
102	char		*specfile;
103
104	setprogname(argv[0]);
105
106	debug = 0;
107	if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
108		errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
109
110		/* set default fsoptions */
111	(void)memset(&fsoptions, 0, sizeof(fsoptions));
112	fsoptions.fd = -1;
113	fsoptions.sectorsize = -1;
114
115	if (fstype->prepare_options)
116		fstype->prepare_options(&fsoptions);
117
118	specfile = NULL;
119	ch = gettimeofday(&start, NULL);
120	start_time.tv_sec = start.tv_sec;
121	start_time.tv_nsec = start.tv_usec * 1000;
122
123	if (ch == -1)
124		err(1, "Unable to get system time");
125
126
127	while ((ch = getopt(argc, argv, "B:b:Dd:f:F:M:m:N:o:pR:s:S:t:T:xZ")) != -1) {
128		switch (ch) {
129
130		case 'B':
131			if (strcmp(optarg, "be") == 0 ||
132			    strcmp(optarg, "4321") == 0 ||
133			    strcmp(optarg, "big") == 0) {
134#if BYTE_ORDER == LITTLE_ENDIAN
135				fsoptions.needswap = 1;
136#endif
137			} else if (strcmp(optarg, "le") == 0 ||
138			    strcmp(optarg, "1234") == 0 ||
139			    strcmp(optarg, "little") == 0) {
140#if BYTE_ORDER == BIG_ENDIAN
141				fsoptions.needswap = 1;
142#endif
143			} else {
144				warnx("Invalid endian `%s'.", optarg);
145				usage(fstype, &fsoptions);
146			}
147			break;
148
149		case 'b':
150			len = strlen(optarg) - 1;
151			if (optarg[len] == '%') {
152				optarg[len] = '\0';
153				fsoptions.freeblockpc =
154				    strsuftoll("free block percentage",
155					optarg, 0, 99);
156			} else {
157				fsoptions.freeblocks =
158				    strsuftoll("free blocks",
159					optarg, 0, LLONG_MAX);
160			}
161			break;
162
163		case 'D':
164			dupsok = 1;
165			break;
166
167		case 'd':
168			debug = strtoll(optarg, NULL, 0);
169			break;
170
171		case 'f':
172			len = strlen(optarg) - 1;
173			if (optarg[len] == '%') {
174				optarg[len] = '\0';
175				fsoptions.freefilepc =
176				    strsuftoll("free file percentage",
177					optarg, 0, 99);
178			} else {
179				fsoptions.freefiles =
180				    strsuftoll("free files",
181					optarg, 0, LLONG_MAX);
182			}
183			break;
184
185		case 'F':
186			specfile = optarg;
187			break;
188
189		case 'M':
190			fsoptions.minsize =
191			    strsuftoll("minimum size", optarg, 1LL, LLONG_MAX);
192			break;
193
194		case 'N':
195			if (! setup_getid(optarg))
196				errx(1,
197			    "Unable to use user and group databases in `%s'",
198				    optarg);
199			break;
200
201		case 'm':
202			fsoptions.maxsize =
203			    strsuftoll("maximum size", optarg, 1LL, LLONG_MAX);
204			break;
205
206		case 'o':
207		{
208			char *p;
209
210			while ((p = strsep(&optarg, ",")) != NULL) {
211				if (*p == '\0')
212					errx(1, "Empty option");
213				if (! fstype->parse_options(p, &fsoptions))
214					usage(fstype, &fsoptions);
215			}
216			break;
217		}
218		case 'p':
219			/* Deprecated in favor of 'Z' */
220			fsoptions.sparse = 1;
221			break;
222
223		case 'R':
224			/* Round image size up to specified block size */
225			fsoptions.roundup =
226			    strsuftoll("roundup-size", optarg, 0, LLONG_MAX);
227			break;
228
229		case 's':
230			fsoptions.minsize = fsoptions.maxsize =
231			    strsuftoll("size", optarg, 1LL, LLONG_MAX);
232			break;
233
234		case 'S':
235			fsoptions.sectorsize =
236			    (int)strsuftoll("sector size", optarg,
237				1LL, INT_MAX);
238			break;
239
240		case 't':
241			/* Check current one and cleanup if necessary. */
242			if (fstype->cleanup_options)
243				fstype->cleanup_options(&fsoptions);
244			fsoptions.fs_specific = NULL;
245			if ((fstype = get_fstype(optarg)) == NULL)
246				errx(1, "Unknown fs type `%s'.", optarg);
247			fstype->prepare_options(&fsoptions);
248			break;
249
250		case 'T':
251			if (get_tstamp(optarg, &stampst) == -1)
252				errx(1, "Cannot get timestamp from `%s'",
253				    optarg);
254			break;
255
256		case 'x':
257			fsoptions.onlyspec = 1;
258			break;
259
260		case 'Z':
261			/* Superscedes 'p' for compatibility with NetBSD makefs(8) */
262			fsoptions.sparse = 1;
263			break;
264
265		case '?':
266		default:
267			usage(fstype, &fsoptions);
268			/* NOTREACHED */
269
270		}
271	}
272	if (debug) {
273		printf("debug mask: 0x%08x\n", debug);
274		printf("start time: %ld.%ld, %s",
275		    (long)start_time.tv_sec, (long)start_time.tv_nsec,
276		    ctime(&start_time.tv_sec));
277	}
278	argc -= optind;
279	argv += optind;
280
281	if (argc < 2)
282		usage(fstype, &fsoptions);
283
284	/* -x must be accompanied by -F */
285	if (fsoptions.onlyspec != 0 && specfile == NULL)
286		errx(1, "-x requires -F mtree-specfile.");
287
288	/* Accept '-' as meaning "read from standard input". */
289	if (strcmp(argv[1], "-") == 0)
290		sb.st_mode = S_IFREG;
291	else {
292		if (stat(argv[1], &sb) == -1)
293			err(1, "Can't stat `%s'", argv[1]);
294	}
295
296	switch (sb.st_mode & S_IFMT) {
297	case S_IFDIR:		/* walk the tree */
298		subtree = argv[1];
299		TIMER_START(start);
300		root = walk_dir(subtree, ".", NULL, NULL);
301		TIMER_RESULTS(start, "walk_dir");
302		break;
303	case S_IFREG:		/* read the manifest file */
304		subtree = ".";
305		TIMER_START(start);
306		root = read_mtree(argv[1], NULL);
307		TIMER_RESULTS(start, "manifest");
308		break;
309	default:
310		errx(1, "%s: not a file or directory", argv[1]);
311		/* NOTREACHED */
312	}
313
314	/* append extra directory */
315	for (i = 2; i < argc; i++) {
316		if (stat(argv[i], &sb) == -1)
317			err(1, "Can't stat `%s'", argv[i]);
318		if (!S_ISDIR(sb.st_mode))
319			errx(1, "%s: not a directory", argv[i]);
320		TIMER_START(start);
321		root = walk_dir(argv[i], ".", NULL, root);
322		TIMER_RESULTS(start, "walk_dir2");
323	}
324
325	if (specfile) {		/* apply a specfile */
326		TIMER_START(start);
327		apply_specfile(specfile, subtree, root, fsoptions.onlyspec);
328		TIMER_RESULTS(start, "apply_specfile");
329	}
330
331	if (debug & DEBUG_DUMP_FSNODES) {
332		printf("\nparent: %s\n", subtree);
333		dump_fsnodes(root);
334		putchar('\n');
335	}
336
337				/* build the file system */
338	TIMER_START(start);
339	fstype->make_fs(argv[0], subtree, root, &fsoptions);
340	TIMER_RESULTS(start, "make_fs");
341
342	free_fsnodes(root);
343
344	exit(0);
345	/* NOTREACHED */
346}
347
348int
349set_option(const option_t *options, const char *option, char *buf, size_t len)
350{
351	char *var, *val;
352	int retval;
353
354	assert(option != NULL);
355
356	var = estrdup(option);
357	for (val = var; *val; val++)
358		if (*val == '=') {
359			*val++ = '\0';
360			break;
361		}
362	retval = set_option_var(options, var, val, buf, len);
363	free(var);
364	return retval;
365}
366
367int
368set_option_var(const option_t *options, const char *var, const char *val,
369    char *buf, size_t len)
370{
371	char *s;
372	size_t i;
373
374#define NUM(type) \
375	if (!*val) { \
376		*(type *)options[i].value = 1; \
377		break; \
378	} \
379	*(type *)options[i].value = (type)strsuftoll(options[i].desc, val, \
380	    options[i].minimum, options[i].maximum); break
381
382	for (i = 0; options[i].name != NULL; i++) {
383		if (var[1] == '\0') {
384			if (options[i].letter != var[0])
385				continue;
386		} else if (strcmp(options[i].name, var) != 0)
387			continue;
388		switch (options[i].type) {
389		case OPT_BOOL:
390			*(bool *)options[i].value = 1;
391			break;
392		case OPT_STRARRAY:
393			strlcpy((void *)options[i].value, val, (size_t)
394			    options[i].maximum);
395			break;
396		case OPT_STRPTR:
397			s = estrdup(val);
398			*(char **)options[i].value = s;
399			break;
400		case OPT_STRBUF:
401			if (buf == NULL)
402				abort();
403			strlcpy(buf, val, len);
404			break;
405		case OPT_INT64:
406			NUM(uint64_t);
407		case OPT_INT32:
408			NUM(uint32_t);
409		case OPT_INT16:
410			NUM(uint16_t);
411		case OPT_INT8:
412			NUM(uint8_t);
413		default:
414			warnx("Unknown type %d in option %s", options[i].type,
415			    val);
416			return 0;
417		}
418		return i;
419	}
420	warnx("Unknown option `%s'", var);
421	return -1;
422}
423
424
425static fstype_t *
426get_fstype(const char *type)
427{
428	int i;
429
430	for (i = 0; fstypes[i].type != NULL; i++)
431		if (strcmp(fstypes[i].type, type) == 0)
432			return (&fstypes[i]);
433	return (NULL);
434}
435
436option_t *
437copy_opts(const option_t *o)
438{
439	size_t i;
440
441	for (i = 0; o[i].name; i++)
442		continue;
443	i++;
444	return memcpy(ecalloc(i, sizeof(*o)), o, i * sizeof(*o));
445}
446
447static int
448get_tstamp(const char *b, struct stat *st)
449{
450	time_t when;
451	char *eb;
452	long long l;
453
454	if (stat(b, st) != -1)
455		return 0;
456
457	{
458		errno = 0;
459		l = strtoll(b, &eb, 0);
460		if (b == eb || *eb || errno)
461			return -1;
462		when = (time_t)l;
463	}
464
465	st->st_ino = 1;
466#ifdef HAVE_STRUCT_STAT_BIRTHTIME
467	st->st_birthtime =
468#endif
469	st->st_mtime = st->st_ctime = st->st_atime = when;
470	return 0;
471}
472
473static void
474usage(fstype_t *fstype, fsinfo_t *fsoptions)
475{
476	const char *prog;
477
478	prog = getprogname();
479	fprintf(stderr,
480"Usage: %s [-xZ] [-B endian] [-b free-blocks] [-d debug-mask]\n"
481"\t[-F mtree-specfile] [-f free-files] [-M minimum-size] [-m maximum-size]\n"
482"\t[-N userdb-dir] [-o fs-options] [-R roundup-size] [-S sector-size]\n"
483"\t[-s image-size] [-T <timestamp/file>] [-t fs-type]\n"
484"\timage-file directory | manifest [extra-directory ...]\n",
485	    prog);
486
487	if (fstype) {
488		size_t i;
489		option_t *o = fsoptions->fs_options;
490
491		fprintf(stderr, "\n%s specific options:\n", fstype->type);
492		for (i = 0; o[i].name != NULL; i++)
493			fprintf(stderr, "\t%c%c%20.20s\t%s\n",
494			    o[i].letter ? o[i].letter : ' ',
495			    o[i].letter ? ',' : ' ',
496			    o[i].name, o[i].desc);
497	}
498	exit(1);
499}
500