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