newfs.c revision 1.2 1 /* $NetBSD: newfs.c,v 1.2 1999/07/15 19:09:40 perseant Exp $ */
2
3 /*-
4 * Copyright (c) 1989, 1992, 1993
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1989, 1992, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)newfs.c 8.5 (Berkeley) 5/24/95";
45 #else
46 __RCSID("$NetBSD: newfs.c,v 1.2 1999/07/15 19:09:40 perseant Exp $");
47 #endif
48 #endif /* not lint */
49
50 /*
51 * newfs: friendly front end to mkfs
52 */
53 #include <sys/param.h>
54 #include <sys/ucred.h>
55 #include <sys/stat.h>
56 #include <sys/ioctl.h>
57 #include <sys/disklabel.h>
58 #include <sys/file.h>
59 #include <sys/mount.h>
60 #include <sys/sysctl.h>
61
62 #include <ufs/ufs/dir.h>
63 #include <ufs/ufs/dinode.h>
64
65 #include <disktab.h>
66 #include <errno.h>
67 #include <unistd.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <ctype.h>
71 #include <string.h>
72 #include <paths.h>
73 #include <util.h>
74 #include "config.h"
75 #include "extern.h"
76
77 #define COMPAT /* allow non-labeled disks */
78
79 int Nflag; /* run without writing file system */
80 int fssize; /* file system size */
81 int sectorsize; /* bytes/sector */
82 int fsize = 0; /* fragment size */
83 int bsize = 0; /* block size */
84 int minfree = MINFREE; /* free space threshold */
85 int bbsize = BBSIZE; /* boot block size */
86 int sbsize = SBSIZE; /* superblock size */
87 u_long memleft; /* virtual memory available */
88 caddr_t membase; /* start address of memory based filesystem */
89 #ifdef COMPAT
90 char *disktype;
91 int unlabeled;
92 #endif
93
94 char device[MAXPATHLEN];
95 char *progname, *special;
96
97 int main __P((int, char **));
98 static struct disklabel *getdisklabel __P((char *, int));
99 static struct disklabel *debug_readlabel __P((int));
100 #ifdef notdef
101 static void rewritelabel __P((char *, int, struct disklabel *));
102 #endif
103 static void usage __P((void));
104
105 int
106 main(argc, argv)
107 int argc;
108 char *argv[];
109 {
110 int ch;
111 struct partition *pp;
112 struct disklabel *lp;
113 struct stat st;
114 int debug, lfs, fsi, fso, segsize, maxpartitions;
115 char *cp, *opstring;
116
117 if ((progname = strrchr(*argv, '/')) != NULL)
118 ++progname;
119 else
120 progname = *argv;
121
122 maxpartitions = getmaxpartitions();
123 if (maxpartitions > 26)
124 fatal("insane maxpartitions value %d", maxpartitions);
125
126 opstring = "B:DLNb:f:m:s:";
127
128 debug = lfs = segsize = 0;
129 while ((ch = getopt(argc, argv, opstring)) != -1)
130 switch(ch) {
131 case 'B': /* LFS segment size */
132 if ((segsize = atoi(optarg)) < LFS_MINSEGSIZE)
133 fatal("%s: bad segment size", optarg);
134 break;
135 case 'D':
136 debug = 1;
137 break;
138 case 'L': /* Create lfs */
139 lfs = 1;
140 break;
141 case 'N':
142 Nflag++;
143 break;
144 #ifdef COMPAT
145 case 'T':
146 disktype = optarg;
147 break;
148 #endif
149 case 'b': /* used for LFS */
150 if ((bsize = atoi(optarg)) < LFS_MINBLOCKSIZE)
151 fatal("%s: bad block size", optarg);
152 break;
153 case 'f':
154 if ((fsize = atoi(optarg)) <= 0)
155 fatal("%s: bad frag size", optarg);
156 break;
157 case 'm':
158 if ((minfree = atoi(optarg)) < 0 || minfree > 99)
159 fatal("%s: bad free space %%\n", optarg);
160 break;
161 case 's':
162 if ((fssize = atoi(optarg)) <= 0)
163 fatal("%s: bad file system size", optarg);
164 break;
165 case '?':
166 default:
167 usage();
168 }
169 argc -= optind;
170 argv += optind;
171
172 if (argc != 2 && argc != 1)
173 usage();
174
175 /*
176 * If the -N flag isn't specified, open the output file. If no path
177 * prefix, try /dev/r%s and then /dev/%s.
178 */
179 special = argv[0];
180 if (strchr(special, '/') == NULL) {
181 (void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV,
182 special);
183 if (stat(device, &st) == -1)
184 (void)snprintf(device, sizeof(device), "%s%s",
185 _PATH_DEV, special);
186 special = device;
187 }
188 if (!Nflag) {
189 fso = open(special,
190 (debug ? O_CREAT : 0) | O_WRONLY, DEFFILEMODE);
191 if (fso < 0)
192 fatal("%s: %s", special, strerror(errno));
193 } else
194 fso = -1;
195
196 /* Open the input file. */
197 fsi = open(special, O_RDONLY);
198 if (fsi < 0)
199 fatal("%s: %s", special, strerror(errno));
200 if (fstat(fsi, &st) < 0)
201 fatal("%s: %s", special, strerror(errno));
202
203 if (!debug && !S_ISCHR(st.st_mode))
204 (void)printf("%s: %s: not a character-special device\n",
205 progname, special);
206 cp = strchr(argv[0], '\0') - 1;
207 if (!debug
208 && (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
209 && !isdigit(*cp))))
210 fatal("%s: can't figure out file system partition", argv[0]);
211
212 #ifdef COMPAT
213 if (disktype == NULL)
214 disktype = argv[1];
215 #endif
216 if (debug)
217 lp = debug_readlabel(fsi);
218 else
219 lp = getdisklabel(special, fsi);
220
221 if (isdigit(*cp))
222 pp = &lp->d_partitions[0];
223 else
224 pp = &lp->d_partitions[*cp - 'a'];
225 if (pp->p_size == 0)
226 fatal("%s: `%c' partition is unavailable", argv[0], *cp);
227
228 /* If we're making a LFS, we break out here */
229 exit(make_lfs(fso, lp, pp, minfree, bsize, fsize, segsize));
230 }
231
232 #ifdef COMPAT
233 char lmsg[] = "%s: can't read disk label; disk type must be specified";
234 #else
235 char lmsg[] = "%s: can't read disk label";
236 #endif
237
238 static struct disklabel *
239 getdisklabel(s, fd)
240 char *s;
241 int fd;
242 {
243 static struct disklabel lab;
244
245 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
246 #ifdef COMPAT
247 if (disktype) {
248 struct disklabel *lp;
249
250 unlabeled++;
251 lp = getdiskbyname(disktype);
252 if (lp == NULL)
253 fatal("%s: unknown disk type", disktype);
254 return (lp);
255 }
256 #endif
257 (void)fprintf(stderr,
258 "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
259 fatal(lmsg, s);
260 }
261 return (&lab);
262 }
263
264
265 static struct disklabel *
266 debug_readlabel(fd)
267 int fd;
268 {
269 static struct disklabel lab;
270 int n;
271
272 if ((n = read(fd, &lab, sizeof(struct disklabel))) < 0)
273 fatal("unable to read disk label: %s", strerror(errno));
274 else if (n < sizeof(struct disklabel))
275 fatal("short read of disklabel: %d of %d bytes", n,
276 sizeof(struct disklabel));
277 return(&lab);
278 }
279
280 #ifdef notdef
281 static void
282 rewritelabel(s, fd, lp)
283 char *s;
284 int fd;
285 struct disklabel *lp;
286 {
287 #ifdef COMPAT
288 if (unlabeled)
289 return;
290 #endif
291 lp->d_checksum = 0;
292 lp->d_checksum = dkcksum(lp);
293 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
294 (void)fprintf(stderr,
295 "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
296 fatal("%s: can't rewrite disk label", s);
297 }
298 #if __vax__
299 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
300 int i;
301 int cfd;
302 daddr_t alt;
303 char specname[64];
304 char blk[1024];
305 char *cp;
306
307 /*
308 * Make name for 'c' partition.
309 */
310 strcpy(specname, s);
311 cp = specname + strlen(specname) - 1;
312 if (!isdigit(*cp))
313 *cp = 'c';
314 cfd = open(specname, O_WRONLY);
315 if (cfd < 0)
316 fatal("%s: %s", specname, strerror(errno));
317 memset(blk, 0, sizeof(blk));
318 *(struct disklabel *)(blk + LABELOFFSET) = *lp;
319 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
320 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
321 if (lseek(cfd, (off_t)((alt + i) * lp->d_secsize),
322 SEEK_SET) == -1)
323 fatal("lseek to badsector area: %s",
324 strerror(errno));
325 if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
326 fprintf(stderr,
327 "%s: alternate label %d write: %s\n",
328 progname, i/2, strerror(errno));
329 }
330 close(cfd);
331 }
332 #endif /* vax */
333 }
334 #endif /* notdef */
335
336 void
337 usage()
338 {
339 fprintf(stderr, "usage: newfs_lfs [ -fsoptions ] special-device\n");
340 fprintf(stderr, "where fsoptions are:\n");
341 fprintf(stderr, "\t-B LFS segment size\n");
342 fprintf(stderr, "\t-D debug\n");
343 /* fprintf(stderr, "\t-L create LFS file system\n"); */
344 fprintf(stderr,
345 "\t-N do not create file system, just print out parameters\n");
346 fprintf(stderr, "\t-b block size\n");
347 fprintf(stderr, "\t-f frag size\n");
348 fprintf(stderr, "\t-m minimum free space %%\n");
349 fprintf(stderr, "\t-s file system size (sectors)\n");
350 exit(1);
351 }
352