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