newfs_ext2fs.c revision 1.3 1 /* $NetBSD: newfs_ext2fs.c,v 1.3 2007/12/15 19:44:47 perry Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
35 The Regents of the University of California. All rights reserved.\n");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)newfs.c 8.13 (Berkeley) 5/1/95";
41 #else
42 __RCSID("$NetBSD: newfs_ext2fs.c,v 1.3 2007/12/15 19:44:47 perry Exp $");
43 #endif
44 #endif /* not lint */
45
46 /*
47 * newfs: friendly front end to mke2fs
48 */
49 #include <sys/param.h>
50 #include <sys/ioctl.h>
51 #include <sys/disklabel.h>
52 #include <sys/disk.h>
53 #include <sys/file.h>
54 #include <sys/mount.h>
55
56 #include <ufs/ext2fs/ext2fs.h>
57
58 #include <disktab.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <limits.h>
62 #include <paths.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
67 #include <util.h>
68 #include <mntopts.h>
69
70 #include "extern.h"
71 #include "partutil.h"
72
73 static int64_t strsuftoi64(const char *, const char *, int64_t, int64_t, int *);
74 static void usage(void) __dead;
75
76 /*
77 * For file systems smaller than SMALL_FSSIZE we use the S_DFL_* defaults,
78 * otherwise if less than MEDIUM_FSSIZE use M_DFL_*, otherwise use
79 * L_DFL_*.
80 */
81 #define SMALL_FSSIZE ((4 * 1024 * 1024) / sectorsize) /* 4MB */
82 #define S_DFL_BSIZE 1024
83 #define MEDIUM_FSSIZE ((512 * 1024 * 1024) / sectorsize) /* 512MB */
84 #define M_DFL_BSIZE 1024
85 #define L_DFL_BSIZE 4096
86
87 /*
88 * Each file system has a number of inodes statically allocated.
89 * We allocate one inode slot per 2, 4, or 8 blocks, expecting this
90 * to be far more than we will ever need.
91 */
92 #define S_DFL_NINODE(blocks) ((blocks) / 8)
93 #define M_DFL_NINODE(blocks) ((blocks) / 4)
94 #define L_DFL_NINODE(blocks) ((blocks) / 2)
95
96 /*
97 * Default sector size.
98 */
99 #define DFL_SECSIZE 512
100
101 int Nflag; /* run without writing file system */
102 int Oflag = 0; /* format as conservative REV0 by default */
103 int verbosity; /* amount of printf() output */
104 #define DEFAULT_VERBOSITY 3 /* 4 is traditional behavior of newfs(8) */
105 int64_t fssize; /* file system size */
106 uint sectorsize; /* bytes/sector */
107 uint fsize = 0; /* fragment size */
108 uint bsize = 0; /* block size */
109 uint minfree = MINFREE; /* free space threshold */
110 uint density; /* number of bytes per inode */
111 uint num_inodes; /* number of inodes (overrides density) */
112 char *volname = NULL; /* volume name */
113
114 static char *disktype = NULL;
115 static char device[MAXPATHLEN];
116
117 static const char lmsg[] = "%s: can't read disk label";
118
119 int
120 main(int argc, char *argv[])
121 {
122 struct disk_geom geo;
123 struct dkwedge_info dkw;
124 struct statvfs *mp;
125 struct stat sb;
126 int ch, fsi, fso, len, n, Fflag, Iflag, Zflag;
127 char *cp, *s1, *s2, *special;
128 const char *opstring;
129 int byte_sized;
130 uint blocks; /* number of blocks */
131
132 cp = NULL;
133 fsi = fso = -1;
134 Fflag = Iflag = Zflag = 0;
135 verbosity = -1;
136 opstring = "FINO:S:V:Zb:f:i:l:m:n:s:v:";
137 byte_sized = 0;
138 while ((ch = getopt(argc, argv, opstring)) != -1)
139 switch (ch) {
140 case 'F':
141 Fflag = 1;
142 break;
143 case 'I':
144 Iflag = 1;
145 break;
146 case 'N':
147 Nflag = 1;
148 if (verbosity == -1)
149 verbosity = DEFAULT_VERBOSITY;
150 break;
151 case 'O':
152 Oflag = strsuftoi64("format", optarg, 0, 1, NULL);
153 break;
154 case 'S':
155 /*
156 * XXX:
157 * non-512 byte sectors almost certainly don't work.
158 */
159 sectorsize = strsuftoi64("sector size",
160 optarg, 512, 65536, NULL);
161 if (!powerof2(sectorsize))
162 errx(EXIT_FAILURE,
163 "sector size `%s' is not a power of 2.",
164 optarg);
165 break;
166 case 'V':
167 verbosity = strsuftoi64("verbose", optarg, 0, 4, NULL);
168 break;
169 case 'Z':
170 Zflag = 1;
171 break;
172 case 'b':
173 bsize = strsuftoi64("block size",
174 optarg, MINBSIZE, EXT2_MAXBSIZE, NULL);
175 break;
176 case 'f':
177 fsize = strsuftoi64("fragment size",
178 optarg, MINBSIZE, EXT2_MAXBSIZE, NULL);
179 break;
180 case 'i':
181 density = strsuftoi64("bytes per inode",
182 optarg, 1, INT_MAX, NULL);
183 break;
184 case 'm':
185 minfree = strsuftoi64("free space %",
186 optarg, 0, 99, NULL);
187 break;
188 case 'n':
189 num_inodes = strsuftoi64("number of inodes",
190 optarg, 1, INT_MAX, NULL);
191 break;
192 case 's':
193 fssize = strsuftoi64("file system size",
194 optarg, INT64_MIN, INT64_MAX, &byte_sized);
195 break;
196 case 'v':
197 volname = optarg;
198 if (volname[0] == '\0')
199 errx(EXIT_FAILURE,
200 "Volume name cannot be zero length");
201 break;
202 case '?':
203 default:
204 usage();
205 }
206 argc -= optind;
207 argv += optind;
208
209 if (verbosity == -1)
210 /* Default to showing cg info */
211 verbosity = DEFAULT_VERBOSITY;
212
213 if (argc != 1)
214 usage();
215
216 memset(&sb, 0, sizeof(sb));
217 memset(&dkw, 0, sizeof(dkw));
218 special = argv[0];
219 if (Fflag) {
220 int fl;
221 /*
222 * It's a file system image
223 * no label, use fixed default for sectorsize.
224 */
225 if (sectorsize == 0)
226 sectorsize = DFL_SECSIZE;
227
228 /* creating image in a regular file */
229 if (Nflag)
230 fl = O_RDONLY;
231 else {
232 if (fssize > 0)
233 fl = O_RDWR | O_CREAT;
234 else
235 fl = O_RDWR;
236 }
237 fsi = open(special, fl, 0777);
238 if (fsi == -1)
239 err(EXIT_FAILURE, "can't open file %s", special);
240 if (fstat(fsi, &sb) == -1)
241 err(EXIT_FAILURE, "can't fstat opened %s", special);
242 if (!Nflag)
243 fso = fsi;
244 } else { /* !Fflag */
245 fsi = opendisk(special, O_RDONLY, device, sizeof(device), 0);
246 special = device;
247 if (fsi < 0 || fstat(fsi, &sb) == -1)
248 err(EXIT_FAILURE, "%s: open for read", special);
249
250 if (!Nflag) {
251 fso = open(special, O_WRONLY, 0);
252 if (fso < 0)
253 err(EXIT_FAILURE,
254 "%s: open for write", special);
255
256 /* Bail if target special is mounted */
257 n = getmntinfo(&mp, MNT_NOWAIT);
258 if (n == 0)
259 err(EXIT_FAILURE, "%s: getmntinfo", special);
260
261 len = sizeof(_PATH_DEV) - 1;
262 s1 = special;
263 if (strncmp(_PATH_DEV, s1, len) == 0)
264 s1 += len;
265
266 while (--n >= 0) {
267 s2 = mp->f_mntfromname;
268 if (strncmp(_PATH_DEV, s2, len) == 0) {
269 s2 += len - 1;
270 *s2 = 'r';
271 }
272 if (strcmp(s1, s2) == 0 ||
273 strcmp(s1, &s2[1]) == 0)
274 errx(EXIT_FAILURE,
275 "%s is mounted on %s",
276 special, mp->f_mntonname);
277 ++mp;
278 }
279 }
280
281 if (getdiskinfo(special, fsi, disktype, &geo, &dkw) == -1)
282 errx(EXIT_FAILURE, lmsg, special);
283
284 if (sectorsize == 0) {
285 sectorsize = geo.dg_secsize;
286 if (sectorsize <= 0)
287 errx(EXIT_FAILURE, "no default sector size");
288 }
289
290 if (dkw.dkw_parent[0]) {
291 if (dkw.dkw_size == 0)
292 errx(EXIT_FAILURE,
293 "%s partition is unavailable", special);
294
295 if (!Iflag) {
296 static const char m[] =
297 "%s partition type is not `%s' (or use -I)";
298 if (strcmp(dkw.dkw_ptype, DKW_PTYPE_EXT2FS))
299 errx(EXIT_FAILURE, m,
300 special, "Linux Ext2");
301 }
302 }
303 }
304
305 if (byte_sized)
306 fssize /= sectorsize;
307 if (fssize <= 0) {
308 if (sb.st_size != 0)
309 fssize += sb.st_size / sectorsize;
310 else
311 fssize += dkw.dkw_size;
312 if (fssize <= 0)
313 errx(EXIT_FAILURE,
314 "Unable to determine file system size");
315 }
316
317 if (dkw.dkw_parent[0] && fssize > dkw.dkw_size)
318 errx(EXIT_FAILURE,
319 "size %" PRIu64 " exceeds maximum file system size on "
320 "`%s' of %" PRIu64 " sectors",
321 fssize, special, dkw.dkw_size);
322
323 /* XXXLUKEM: only ftruncate() regular files ? (dsl: or at all?) */
324 if (Fflag && fso != -1
325 && ftruncate(fso, (off_t)fssize * sectorsize) == -1)
326 err(1, "can't ftruncate %s to %" PRId64, special, fssize);
327
328 if (Zflag && fso != -1) { /* pre-zero (and de-sparce) the file */
329 char *buf;
330 int bufsize, i;
331 off_t bufrem;
332 struct statvfs sfs;
333
334 if (fstatvfs(fso, &sfs) == -1) {
335 warn("can't fstatvfs `%s'", special);
336 bufsize = 8192;
337 } else
338 bufsize = sfs.f_iosize;
339
340 if ((buf = calloc(1, bufsize)) == NULL)
341 err(1, "can't malloc buffer of %d",
342 bufsize);
343 bufrem = fssize * sectorsize;
344 if (verbosity > 0)
345 printf("Creating file system image in `%s', "
346 "size %" PRId64 " bytes, in %d byte chunks.\n",
347 special, bufrem, bufsize);
348 while (bufrem > 0) {
349 i = write(fso, buf, MIN(bufsize, bufrem));
350 if (i == -1)
351 err(1, "writing image");
352 bufrem -= i;
353 }
354 free(buf);
355 }
356
357 /* Sort out fragment and block sizes */
358 if (bsize == 0) {
359 bsize = fsize;
360 if (bsize == 0) {
361 if (fssize < SMALL_FSSIZE)
362 bsize = S_DFL_BSIZE;
363 else if (fssize < MEDIUM_FSSIZE)
364 bsize = M_DFL_BSIZE;
365 else
366 bsize = L_DFL_BSIZE;
367 }
368 }
369 if (fsize == 0)
370 fsize = bsize;
371
372 blocks = fssize * sectorsize / bsize;
373
374 if (num_inodes == 0) {
375 if (density != 0)
376 num_inodes = fssize / density;
377 else {
378 if (fssize < SMALL_FSSIZE)
379 num_inodes = S_DFL_NINODE(blocks);
380 else if (fssize < MEDIUM_FSSIZE)
381 num_inodes = M_DFL_NINODE(blocks);
382 else
383 num_inodes = L_DFL_NINODE(blocks);
384 }
385 }
386 mke2fs(special, fsi, fso);
387
388 if (fsi != -1)
389 close(fsi);
390 if (fso != -1 && fso != fsi)
391 close(fso);
392 exit(EXIT_SUCCESS);
393 }
394
395 static int64_t
396 strsuftoi64(const char *desc, const char *arg, int64_t min, int64_t max,
397 int *num_suffix)
398 {
399 int64_t result, r1;
400 int shift = 0;
401 char *ep;
402
403 errno = 0;
404 r1 = strtoll(arg, &ep, 10);
405 if (ep[0] != '\0' && ep[1] != '\0')
406 errx(EXIT_FAILURE,
407 "%s `%s' is not a valid number.", desc, arg);
408 switch (ep[0]) {
409 case '\0':
410 case 's':
411 case 'S':
412 if (num_suffix != NULL)
413 *num_suffix = 0;
414 break;
415 case 'g':
416 case 'G':
417 shift += 10;
418 /* FALLTHROUGH */
419 case 'm':
420 case 'M':
421 shift += 10;
422 /* FALLTHROUGH */
423 case 'k':
424 case 'K':
425 shift += 10;
426 /* FALLTHROUGH */
427 case 'b':
428 case 'B':
429 if (num_suffix != NULL)
430 *num_suffix = 1;
431 break;
432 default:
433 errx(EXIT_FAILURE,
434 "`%s' is not a valid suffix for %s.", ep, desc);
435 }
436 result = r1 << shift;
437 if (errno == ERANGE || result >> shift != r1)
438 errx(EXIT_FAILURE,
439 "%s `%s' is too large to convert.", desc, arg);
440 if (result < min)
441 errx(EXIT_FAILURE,
442 "%s `%s' (%" PRId64 ") is less than the minimum (%"
443 PRId64 ").", desc, arg, result, min);
444 if (result > max)
445 errx(EXIT_FAILURE,
446 "%s `%s' (%" PRId64 ") is greater than the maximum (%"
447 PRId64 ").", desc, arg, result, max);
448 return result;
449 }
450
451 static const char help_strings[] =
452 "\t-F \t\tcreate file system image in regular file\n"
453 "\t-I \t\tdo not check that the file system type is `Linux Ext2'\n"
454 "\t-N \t\tdo not create file system, just print out parameters\n"
455 "\t-O N\t\tfilesystem revision: 0 ==> REV0, 1 ==> REV1 (default 0)\n"
456 "\t-S secsize\tsector size\n"
457 "\t-V verbose\toutput verbosity: 0 ==> none, 4 ==> max\n"
458 "\t-Z \t\tpre-zero the image file\n"
459 "\t-b bsize\tblock size\n"
460 "\t-f fsize\tfragment size\n"
461 "\t-i density\tnumber of bytes per inode\n"
462 "\t-m minfree\tminimum free space %\n"
463 "\t-n inodes\tnumber of inodes (overrides -i density)\n"
464 "\t-s fssize\tfile system size (sectors)\n"
465 "\t-v volname\text2fs volume name\n";
466
467 static void
468 usage(void)
469 {
470
471 fprintf(stderr,
472 "usage: %s [ fsoptions ] special-device\n", getprogname());
473 fprintf(stderr, "where fsoptions are:\n");
474 fprintf(stderr, "%s", help_strings);
475
476 exit(EXIT_FAILURE);
477 }
478