use flock for mailbox locking
[unix-history] / usr / src / sbin / newfs / newfs.c
CommitLineData
a54c0b3f 1#ifndef lint
b7069ef9 2static char sccsid[] = "@(#)newfs.c 4.13 %G%";
a54c0b3f
SL
3#endif
4
5/*
c6003316 6 * newfs: friendly front end to mkfs
a54c0b3f
SL
7 */
8#include <sys/param.h>
9#include <sys/stat.h>
10#include <sys/fs.h>
14b4eb74 11#include <sys/dir.h>
a54c0b3f
SL
12
13#include <stdio.h>
a54c0b3f
SL
14#include <disktab.h>
15
89241117
SL
16#define BOOTDIR "/usr/mdec" /* directory for boot blocks */
17
a54c0b3f 18int verbose; /* show mkfs line before exec */
11b2fe08 19int noboot; /* do not fill boot blocks */
a54c0b3f
SL
20int fssize; /* file system size */
21int fsize; /* fragment size */
22int bsize; /* block size */
23int ntracks; /* # tracks/cylinder */
24int nsectors; /* # sectors/track */
25int sectorsize; /* bytes/sector */
26int cpg; /* cylinders/cylinder group */
11b2fe08 27int minfree = -1; /* free space threshold */
c6003316 28int rpm; /* revolutions/minute of drive */
685a1465 29int density; /* number of bytes per inode */
a54c0b3f
SL
30
31char *av[20]; /* argv array and buffers for exec */
32char a2[20];
33char a3[20];
34char a4[20];
35char a5[20];
36char a6[20];
37char a7[20];
c6003316
SL
38char a8[20];
39char a9[20];
685a1465 40char a10[20];
a54c0b3f
SL
41char device[MAXPATHLEN];
42char cmd[BUFSIZ];
43
44char *index();
45char *rindex();
46char *sprintf();
47
48main(argc, argv)
1b8b6e44 49 int argc;
a54c0b3f
SL
50 char *argv[];
51{
52 char *cp, *special;
53 register struct disktab *dp;
54 register struct partition *pp;
55 struct stat st;
56 register int i;
57 int status;
58
59 argc--, argv++;
60 while (argc > 0 && argv[0][0] == '-') {
61 for (cp = &argv[0][1]; *cp; cp++)
62 switch (*cp) {
63
64 case 'v':
65 verbose++;
66 break;
67
11b2fe08
HS
68 case 'n':
69 noboot++;
70 break;
71
a54c0b3f
SL
72 case 's':
73 if (argc < 1)
74 fatal("-s: missing file system size");
75 argc--, argv++;
76 fssize = atoi(*argv);
77 if (fssize < 0)
78 fatal("%s: bad file system size",
79 *argv);
80 goto next;
81
82 case 't':
83 if (argc < 1)
84 fatal("-t: missing track total");
85 argc--, argv++;
86 ntracks = atoi(*argv);
87 if (ntracks < 0)
88 fatal("%s: bad total tracks", *argv);
89 goto next;
90
91 case 'b':
92 if (argc < 1)
93 fatal("-b: missing block size");
94 argc--, argv++;
95 bsize = atoi(*argv);
96 if (bsize < 0 || bsize < MINBSIZE)
97 fatal("%s: bad block size", *argv);
98 goto next;
99
100 case 'f':
101 if (argc < 1)
102 fatal("-f: missing frag size");
103 argc--, argv++;
104 fsize = atoi(*argv);
105 if (fsize < 0)
106 fatal("%s: bad frag size", *argv);
107 goto next;
108
109 case 'S':
110 if (argc < 1)
111 fatal("-S: missing sector size");
112 argc--, argv++;
113 sectorsize = atoi(*argv);
114 if (sectorsize < 0)
115 fatal("%s: bad sector size", *argv);
116 goto next;
117
118 case 'c':
119 if (argc < 1)
120 fatal("-c: missing cylinders/group");
121 argc--, argv++;
122 cpg = atoi(*argv);
123 if (cpg < 0)
124 fatal("%s: bad cylinders/group", *argv);
125 goto next;
126
c6003316
SL
127 case 'm':
128 if (argc < 1)
129 fatal("-m: missing free space %%\n");
130 argc--, argv++;
131 minfree = atoi(*argv);
132 if (minfree < 0 || minfree > 99)
133 fatal("%s: bad free space %%\n",
134 *argv);
135 goto next;
136
137 case 'r':
138 if (argc < 1)
139 fatal("-r: missing revs/minute\n");
140 argc--, argv++;
141 rpm = atoi(*argv);
142 if (rpm < 0)
143 fatal("%s: bad revs/minute\n", *argv);
144 goto next;
145
685a1465
KM
146 case 'i':
147 if (argc < 1)
148 fatal("-i: missing bytes per inode\n");
149 argc--, argv++;
150 density = atoi(*argv);
151 if (density < 0)
152 fatal("%s: bad bytes per inode\n",
153 *argv);
154 goto next;
155
a54c0b3f
SL
156 default:
157 fatal("-%c: unknown flag", cp);
158 }
159next:
160 argc--, argv++;
161 }
162 if (argc < 2) {
c6003316 163 fprintf(stderr, "usage: newfs [ -v ] [ mkfs-options ] %s\n",
a54c0b3f
SL
164 "special-device device-type");
165 fprintf(stderr, "where mkfs-options are:\n");
e6b4584b
SL
166 fprintf(stderr, "\t-s file system size (sectors)\n");
167 fprintf(stderr, "\t-b block size\n");
168 fprintf(stderr, "\t-f frag size\n");
2d2a9370
SL
169 fprintf(stderr, "\t-t tracks/cylinder\n");
170 fprintf(stderr, "\t-c cylinders/group\n");
c6003316
SL
171 fprintf(stderr, "\t-m minimum free space %%\n");
172 fprintf(stderr, "\t-r revolutions/minute\n");
e6b4584b 173 fprintf(stderr, "\t-S sector size\n");
685a1465 174 fprintf(stderr, "\t-i number of bytes per inode\n");
a54c0b3f
SL
175 exit(1);
176 }
177 special = argv[0];
1b8b6e44
KM
178 cp = rindex(special, '/');
179 if (cp != 0)
180 special = cp + 1;
2f56e34c 181 if (*special == 'r' && special[1] != 'a' && special[1] != 'b')
1b8b6e44
KM
182 special++;
183 special = sprintf(device, "/dev/r%s", special);
a54c0b3f 184 if (stat(special, &st) < 0) {
c6003316 185 fprintf(stderr, "newfs: "); perror(special);
a54c0b3f
SL
186 exit(2);
187 }
1b8b6e44
KM
188 if ((st.st_mode & S_IFMT) != S_IFCHR)
189 fatal("%s: not a character device", special);
a54c0b3f
SL
190 dp = getdiskbyname(argv[1]);
191 if (dp == 0)
192 fatal("%s: unknown disk type", argv[1]);
193 cp = index(argv[0], '\0') - 1;
194 if (cp == 0 || *cp < 'a' || *cp > 'h')
195 fatal("%s: can't figure out file system partition", argv[0]);
196 pp = &dp->d_partitions[*cp - 'a'];
197 if (fssize == 0) {
198 fssize = pp->p_size;
199 if (fssize < 0)
200 fatal("%s: no default size for `%c' partition",
201 argv[1], *cp);
202 }
203 if (nsectors == 0) {
204 nsectors = dp->d_nsectors;
205 if (nsectors < 0)
206 fatal("%s: no default #sectors/track", argv[1]);
207 }
208 if (ntracks == 0) {
209 ntracks = dp->d_ntracks;
210 if (ntracks < 0)
211 fatal("%s: no default #tracks", argv[1]);
212 }
213 if (sectorsize == 0) {
214 sectorsize = dp->d_secsize;
215 if (sectorsize < 0)
216 fatal("%s: no default sector size", argv[1]);
217 }
218 if (bsize == 0) {
219 bsize = pp->p_bsize;
220 if (bsize < 0)
221 fatal("%s: no default block size for `%c' partition",
222 argv[1], *cp);
223 }
224 if (fsize == 0) {
225 fsize = pp->p_fsize;
226 if (fsize < 0)
227 fatal("%s: no default frag size for `%c' partition",
228 argv[1], *cp);
229 }
c6003316
SL
230 if (rpm == 0) {
231 rpm = dp->d_rpm;
232 if (rpm < 0)
233 fatal("%s: no default revolutions/minute value",
234 argv[1]);
235 }
685a1465
KM
236 if (density <= 0)
237 density = 2048;
11b2fe08 238 if (minfree < 0)
c6003316
SL
239 minfree = 10;
240 if (cpg == 0)
241 cpg = 16;
a54c0b3f
SL
242 i = 0;
243 av[i++] = sprintf(a2, "%d", fssize);
244 av[i++] = sprintf(a3, "%d", nsectors);
245 av[i++] = sprintf(a4, "%d", ntracks);
246 av[i++] = sprintf(a5, "%d", bsize);
247 av[i++] = sprintf(a6, "%d", fsize);
c6003316
SL
248 av[i++] = sprintf(a7, "%d", cpg);
249 av[i++] = sprintf(a8, "%d", minfree);
250 av[i++] = sprintf(a9, "%d", rpm / 60);
685a1465 251 av[i++] = sprintf(a10, "%d", density);
a54c0b3f
SL
252 av[i++] = 0;
253 sprintf(cmd, "/etc/mkfs %s", special);
254 for (i = 0; av[i] != 0; i++) {
255 strcat(cmd, " ");
256 strcat(cmd, av[i]);
257 }
258 if (verbose)
259 printf("%s\n", cmd);
a54c0b3f 260 if (status = system(cmd))
b7069ef9 261 exit(status >> 8);
11b2fe08 262 if (*cp == 'a' && !noboot) {
a54c0b3f 263 char type[3];
d314bb68 264 struct stat sb;
a54c0b3f
SL
265
266 cp = rindex(special, '/');
267 if (cp == NULL)
268 fatal("%s: can't figure out disk type from name",
269 special);
d314bb68 270 if (stat(special, &sb) >= 0 && (sb.st_mode & S_IFMT) == S_IFCHR)
2d2a9370 271 cp++;
a54c0b3f
SL
272 type[0] = *++cp;
273 type[1] = *++cp;
274 type[2] = '\0';
275 installboot(special, type);
276 }
277 exit(0);
278}
279
280installboot(dev, type)
281 char *dev, *type;
282{
283 int fd;
284 char bootblock[MAXPATHLEN], standalonecode[MAXPATHLEN];
285 char bootimage[BBSIZE];
286
89241117
SL
287 sprintf(bootblock, "%s/%sboot", BOOTDIR, type);
288 sprintf(standalonecode, "%s/boot%s", BOOTDIR, type);
a54c0b3f
SL
289 if (verbose) {
290 printf("installing boot code\n");
291 printf("sector 0 boot = %s\n", bootblock);
292 printf("1st level boot = %s\n", standalonecode);
293 }
294 fd = open(bootblock, 0);
295 if (fd < 0) {
c6003316 296 fprintf(stderr, "newfs: "); perror(bootblock);
a54c0b3f
SL
297 exit(1);
298 }
299 if (read(fd, bootimage, DEV_BSIZE) < 0) {
c6003316 300 fprintf(stderr, "newfs: "); perror(bootblock);
a54c0b3f
SL
301 exit(2);
302 }
303 close(fd);
304 fd = open(standalonecode, 0);
305 if (fd < 0) {
c6003316 306 fprintf(stderr, "newfs: "); perror(standalonecode);
a54c0b3f
SL
307 exit(1);
308 }
309 if (read(fd, &bootimage[DEV_BSIZE], BBSIZE - DEV_BSIZE) < 0) {
c6003316 310 fprintf(stderr, "newfs: "); perror(standalonecode);
a54c0b3f
SL
311 exit(2);
312 }
313 close(fd);
f55416c1 314 fd = open(dev, 1);
a54c0b3f 315 if (fd < 0) {
c6003316 316 fprintf(stderr, "newfs: "); perror(dev);
a54c0b3f
SL
317 exit(1);
318 }
319 if (write(fd, bootimage, BBSIZE) != BBSIZE) {
c6003316 320 fprintf(stderr, "newfs: "); perror(dev);
a54c0b3f
SL
321 exit(2);
322 }
323 close(fd);
a54c0b3f
SL
324}
325
326/*VARARGS*/
327fatal(fmt, arg1, arg2)
328 char *fmt;
329{
330
c6003316 331 fprintf(stderr, "newfs: ");
a54c0b3f
SL
332 fprintf(stderr, fmt, arg1, arg2);
333 putc('\n', stderr);
334 exit(10);
335}