Deleted Added
full compact
bsdlabel.c (68044) bsdlabel.c (73034)
1/*
2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Symmetric Computer Systems.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 32 unchanged lines hidden (view full) ---

41#endif /* not lint */
42
43#ifndef lint
44#if 0
45static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 1/7/94";
46/* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */
47#endif
48static const char rcsid[] =
1/*
2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Symmetric Computer Systems.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 32 unchanged lines hidden (view full) ---

41#endif /* not lint */
42
43#ifndef lint
44#if 0
45static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 1/7/94";
46/* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */
47#endif
48static const char rcsid[] =
49 "$FreeBSD: head/sbin/bsdlabel/bsdlabel.c 68044 2000-10-31 07:07:39Z jkh $";
49 "$FreeBSD: head/sbin/bsdlabel/bsdlabel.c 73034 2001-02-25 16:47:44Z jwd $";
50#endif /* not lint */
51
52#include <sys/param.h>
53#include <sys/file.h>
54#include <sys/stat.h>
55#include <sys/wait.h>
56#define DKTYPENAMES
57#include <sys/disklabel.h>

--- 17 unchanged lines hidden (view full) ---

75 * The bootstrap source must leave space at the proper offset
76 * for the label on such machines.
77 */
78
79#ifndef BBSIZE
80#define BBSIZE 8192 /* size of boot area, with label */
81#endif
82
50#endif /* not lint */
51
52#include <sys/param.h>
53#include <sys/file.h>
54#include <sys/stat.h>
55#include <sys/wait.h>
56#define DKTYPENAMES
57#include <sys/disklabel.h>

--- 17 unchanged lines hidden (view full) ---

75 * The bootstrap source must leave space at the proper offset
76 * for the label on such machines.
77 */
78
79#ifndef BBSIZE
80#define BBSIZE 8192 /* size of boot area, with label */
81#endif
82
83/* FIX! These are too low, but are traditional */
84#define DEFAULT_NEWFS_BLOCK 8192U
85#define DEFAULT_NEWFS_FRAG 1024U
86#define DEFAULT_NEWFS_CPG 16U
87
88#define BIG_NEWFS_BLOCK 16384U
89#define BIG_NEWFS_FRAG 4096U
90#define BIG_NEWFS_CPG 64U
91
83#ifdef tahoe
84#define NUMBOOT 0
85#else
86#if defined(__alpha__) || defined(hp300) || defined(hp800)
87#define NUMBOOT 1
88#else
89#define NUMBOOT 2
90#endif

--- 22 unchanged lines hidden (view full) ---

113char *dkname;
114char *specname;
115char tmpfil[] = PATH_TMPFILE;
116
117char namebuf[BBSIZE], *np = namebuf;
118struct disklabel lab;
119char bootarea[BBSIZE];
120
92#ifdef tahoe
93#define NUMBOOT 0
94#else
95#if defined(__alpha__) || defined(hp300) || defined(hp800)
96#define NUMBOOT 1
97#else
98#define NUMBOOT 2
99#endif

--- 22 unchanged lines hidden (view full) ---

122char *dkname;
123char *specname;
124char tmpfil[] = PATH_TMPFILE;
125
126char namebuf[BBSIZE], *np = namebuf;
127struct disklabel lab;
128char bootarea[BBSIZE];
129
130/* partition 'c' is the full disk and is special */
131#define FULL_DISK_PART 2
132#define MAX_PART ('z')
133#define MAX_NUM_PARTS (1 + MAX_PART - 'a')
134char part_size_type[MAX_NUM_PARTS];
135char part_offset_type[MAX_NUM_PARTS];
136int part_set[MAX_NUM_PARTS];
137
121#if NUMBOOT > 0
122int installboot; /* non-zero if we should install a boot program */
123char *bootbuf; /* pointer to buffer with remainder of boot prog */
124int bootsize; /* size of remaining boot program */
125char *xxboot; /* primary boot */
126char *bootxx; /* secondary boot */
127char boot0[MAXPATHLEN];
128char boot1[MAXPATHLEN];
129#endif
130
131enum {
132 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
133} op = UNSPEC;
134
135int rflag;
138#if NUMBOOT > 0
139int installboot; /* non-zero if we should install a boot program */
140char *bootbuf; /* pointer to buffer with remainder of boot prog */
141int bootsize; /* size of remaining boot program */
142char *xxboot; /* primary boot */
143char *bootxx; /* secondary boot */
144char boot0[MAXPATHLEN];
145char boot1[MAXPATHLEN];
146#endif
147
148enum {
149 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
150} op = UNSPEC;
151
152int rflag;
153int disable_write; /* set to disable writing to disk label */
136
137#ifdef DEBUG
138int debug;
154
155#ifdef DEBUG
156int debug;
139#define OPTIONS "BNRWb:ders:w"
157#define OPTIONS "BNRWb:denrs:w"
140#else
158#else
141#define OPTIONS "BNRWb:ers:w"
159#define OPTIONS "BNRWb:enrs:w"
142#endif
143
144int
145main(argc, argv)
146 int argc;
147 char *argv[];
148{
149 register struct disklabel *lp;

--- 16 unchanged lines hidden (view full) ---

166 break;
167#endif
168#endif
169 case 'N':
170 if (op != UNSPEC)
171 usage();
172 op = NOWRITE;
173 break;
160#endif
161
162int
163main(argc, argv)
164 int argc;
165 char *argv[];
166{
167 register struct disklabel *lp;

--- 16 unchanged lines hidden (view full) ---

184 break;
185#endif
186#endif
187 case 'N':
188 if (op != UNSPEC)
189 usage();
190 op = NOWRITE;
191 break;
192 case 'n':
193 disable_write = 1;
194 break;
174 case 'R':
175 if (op != UNSPEC)
176 usage();
177 op = RESTORE;
178 break;
179 case 'W':
180 if (op != UNSPEC)
181 usage();

--- 210 unchanged lines hidden (view full) ---

392#ifdef __alpha__
393 u_long *p, sum;
394 int i;
395#endif
396#ifdef vax
397 register int i;
398#endif
399
195 case 'R':
196 if (op != UNSPEC)
197 usage();
198 op = RESTORE;
199 break;
200 case 'W':
201 if (op != UNSPEC)
202 usage();

--- 210 unchanged lines hidden (view full) ---

413#ifdef __alpha__
414 u_long *p, sum;
415 int i;
416#endif
417#ifdef vax
418 register int i;
419#endif
420
400 setbootflag(lp);
401 lp->d_magic = DISKMAGIC;
402 lp->d_magic2 = DISKMAGIC;
403 lp->d_checksum = 0;
404 lp->d_checksum = dkcksum(lp);
405 if (rflag) {
406 /*
407 * First set the kernel disk label,
408 * then write a label to the raw disk.
409 * If the SDINFO ioctl fails because it is unimplemented,
410 * keep going; otherwise, the kernel consistency checks
411 * may prevent us from changing the current (in-core)
412 * label.
413 */
414 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
415 errno != ENODEV && errno != ENOTTY) {
416 l_perror("ioctl DIOCSDINFO");
417 return (1);
418 }
419 (void)lseek(f, (off_t)0, SEEK_SET);
420
421 if (disable_write) {
422 Warning("write to disk label supressed - label was as follows:");
423 display(stdout, lp);
424 return (0);
425 } else {
426 setbootflag(lp);
427 lp->d_magic = DISKMAGIC;
428 lp->d_magic2 = DISKMAGIC;
429 lp->d_checksum = 0;
430 lp->d_checksum = dkcksum(lp);
431 if (rflag) {
432 /*
433 * First set the kernel disk label,
434 * then write a label to the raw disk.
435 * If the SDINFO ioctl fails because it is unimplemented,
436 * keep going; otherwise, the kernel consistency checks
437 * may prevent us from changing the current (in-core)
438 * label.
439 */
440 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
441 errno != ENODEV && errno != ENOTTY) {
442 l_perror("ioctl DIOCSDINFO");
443 return (1);
444 }
445 (void)lseek(f, (off_t)0, SEEK_SET);
446
421#ifdef __alpha__
447#ifdef __alpha__
422 /*
423 * Generate the bootblock checksum for the SRM console.
424 */
425 for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++)
426 sum += p[i];
427 p[63] = sum;
448 /*
449 * Generate the bootblock checksum for the SRM console.
450 */
451 for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++)
452 sum += p[i];
453 p[63] = sum;
428#endif
454#endif
429
430 /*
431 * write enable label sector before write (if necessary),
432 * disable after writing.
433 */
434 flag = 1;
435 if (ioctl(f, DIOCWLABEL, &flag) < 0)
436 warn("ioctl DIOCWLABEL");
437 if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
438 warn("write");
439 return (1);
440 }
455
456 /*
457 * write enable label sector before write (if necessary),
458 * disable after writing.
459 */
460 flag = 1;
461 if (ioctl(f, DIOCWLABEL, &flag) < 0)
462 warn("ioctl DIOCWLABEL");
463 if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
464 warn("write");
465 return (1);
466 }
441#if NUMBOOT > 0
467#if NUMBOOT > 0
442 /*
443 * Output the remainder of the disklabel
444 */
445 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
446 warn("write");
447 return(1);
448 }
468 /*
469 * Output the remainder of the disklabel
470 */
471 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
472 warn("write");
473 return(1);
474 }
449#endif
475#endif
450 flag = 0;
451 (void) ioctl(f, DIOCWLABEL, &flag);
452 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
453 l_perror("ioctl DIOCWDINFO");
454 return (1);
455 }
476 flag = 0;
477 (void) ioctl(f, DIOCWLABEL, &flag);
478 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
479 l_perror("ioctl DIOCWDINFO");
480 return (1);
481 }
456#ifdef vax
482#ifdef vax
457 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
458 daddr_t alt;
459
460 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
461 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
462 (void)lseek(f, (off_t)((alt + i) * lp->d_secsize),
463 SEEK_SET);
464 if (write(f, boot, lp->d_secsize) < lp->d_secsize)
465 warn("alternate label %d write", i/2);
483 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
484 daddr_t alt;
485
486 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
487 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
488 (void)lseek(f, (off_t)((alt + i) * lp->d_secsize),
489 SEEK_SET);
490 if (write(f, boot, lp->d_secsize) < lp->d_secsize)
491 warn("alternate label %d write", i/2);
492 }
466 }
493 }
467 }
468#endif
494#endif
495 }
469 return (0);
470}
471
472void
473l_perror(s)
474 char *s;
475{
476 switch (errno) {

--- 451 unchanged lines hidden (view full) ---

928 */
929int
930getasciilabel(f, lp)
931 FILE *f;
932 register struct disklabel *lp;
933{
934 register char **cpp, *cp;
935 register struct partition *pp;
496 return (0);
497}
498
499void
500l_perror(s)
501 char *s;
502{
503 switch (errno) {

--- 451 unchanged lines hidden (view full) ---

955 */
956int
957getasciilabel(f, lp)
958 FILE *f;
959 register struct disklabel *lp;
960{
961 register char **cpp, *cp;
962 register struct partition *pp;
963 int i;
964 unsigned int part;
936 char *tp, *s, line[BUFSIZ];
937 int v, lineno = 0, errors = 0;
938
939 lp->d_bbsize = BBSIZE; /* XXX */
940 lp->d_sbsize = SBSIZE; /* XXX */
941 while (fgets(line, sizeof(line) - 1, f)) {
942 lineno++;
943 if ((cp = index(line,'\n')) != 0)

--- 188 unchanged lines hidden (view full) ---

1132 if (v < 0) {
1133 fprintf(stderr, "line %d: %s: bad %s\n",
1134 lineno, tp, cp);
1135 errors++;
1136 } else
1137 lp->d_trkseek = v;
1138 continue;
1139 }
965 char *tp, *s, line[BUFSIZ];
966 int v, lineno = 0, errors = 0;
967
968 lp->d_bbsize = BBSIZE; /* XXX */
969 lp->d_sbsize = SBSIZE; /* XXX */
970 while (fgets(line, sizeof(line) - 1, f)) {
971 lineno++;
972 if ((cp = index(line,'\n')) != 0)

--- 188 unchanged lines hidden (view full) ---

1161 if (v < 0) {
1162 fprintf(stderr, "line %d: %s: bad %s\n",
1163 lineno, tp, cp);
1164 errors++;
1165 } else
1166 lp->d_trkseek = v;
1167 continue;
1168 }
1140 if ('a' <= *cp && *cp <= 'z' && cp[1] == '\0') {
1141 unsigned part = *cp - 'a';
1142
1143 if (part > lp->d_npartitions) {
1169 /* the ':' was removed above */
1170 if ('a' <= *cp && *cp <= MAX_PART && cp[1] == '\0') {
1171 part = *cp - 'a';
1172 if (part >= lp->d_npartitions) {
1144 fprintf(stderr,
1173 fprintf(stderr,
1145 "line %d: bad partition name\n", lineno);
1174 "line %d: partition name out of range a-%c: %s\n",
1175 lineno, 'a' + lp->d_npartitions - 1, cp);
1146 errors++;
1147 continue;
1148 }
1149 pp = &lp->d_partitions[part];
1176 errors++;
1177 continue;
1178 }
1179 pp = &lp->d_partitions[part];
1180 part_set[part] = 1;
1150#define NXTNUM(n) { \
1151 if (tp == NULL) { \
1152 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1153 errors++; \
1154 break; \
1155 } else { \
1156 cp = tp, tp = word(cp); \
1157 if (tp == NULL) \
1158 tp = cp; \
1159 (n) = atoi(cp); \
1160 } \
1161 }
1181#define NXTNUM(n) { \
1182 if (tp == NULL) { \
1183 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1184 errors++; \
1185 break; \
1186 } else { \
1187 cp = tp, tp = word(cp); \
1188 if (tp == NULL) \
1189 tp = cp; \
1190 (n) = atoi(cp); \
1191 } \
1192 }
1162
1163 NXTNUM(v);
1164 if (v < 0) {
1193/* retain 1 character following number */
1194#define NXTWORD(w,n) { \
1195 if (tp == NULL) { \
1196 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1197 errors++; \
1198 break; \
1199 } else { \
1200 char *tmp; \
1201 cp = tp, tp = word(cp); \
1202 if (tp == NULL) \
1203 tp = cp; \
1204 (n) = strtol(cp,&tmp,10); \
1205 if (tmp) (w) = *tmp; \
1206 } \
1207 }
1208 v = 0;
1209 NXTWORD(part_size_type[part],v);
1210 if (v < 0 || (v == 0 && part_size_type[part] != '*')) {
1165 fprintf(stderr,
1166 "line %d: %s: bad partition size\n",
1167 lineno, cp);
1168 errors++;
1211 fprintf(stderr,
1212 "line %d: %s: bad partition size\n",
1213 lineno, cp);
1214 errors++;
1169 } else
1215 break;
1216 } else {
1170 pp->p_size = v;
1217 pp->p_size = v;
1171 NXTNUM(v);
1172 if (v < 0) {
1173 fprintf(stderr,
1174 "line %d: %s: bad partition offset\n",
1175 lineno, cp);
1176 errors++;
1177 } else
1178 pp->p_offset = v;
1179 cp = tp, tp = word(cp);
1180 cpp = fstypenames;
1181 for (; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1182 if ((s = *cpp) && streq(s, cp)) {
1183 pp->p_fstype = cpp - fstypenames;
1184 goto gottype;
1185 }
1186 if (isdigit(*cp))
1187 v = atoi(cp);
1188 else
1189 v = FSMAXTYPES;
1190 if ((unsigned)v >= FSMAXTYPES) {
1191 fprintf(stderr, "line %d: %s %s\n", lineno,
1192 "Warning, unknown filesystem type", cp);
1193 v = FS_UNUSED;
1194 }
1195 pp->p_fstype = v;
1196 gottype:
1197
1218
1198 switch (pp->p_fstype) {
1199
1200 case FS_UNUSED: /* XXX */
1201 NXTNUM(pp->p_fsize);
1202 if (pp->p_fsize == 0)
1219 v = 0;
1220 NXTWORD(part_offset_type[part],v);
1221 if (v < 0 || (v == 0 &&
1222 part_offset_type[part] != '*' &&
1223 part_offset_type[part] != '\0')) {
1224 fprintf(stderr,
1225 "line %d: %s: bad partition offset\n",
1226 lineno, cp);
1227 errors++;
1203 break;
1228 break;
1204 NXTNUM(v);
1205 pp->p_frag = v / pp->p_fsize;
1206 break;
1229 } else {
1230 pp->p_offset = v;
1231 cp = tp, tp = word(cp);
1232 cpp = fstypenames;
1233 for (; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1234 if ((s = *cpp) && streq(s, cp)) {
1235 pp->p_fstype = cpp -
1236 fstypenames;
1237 goto gottype;
1238 }
1239 if (isdigit(*cp))
1240 v = atoi(cp);
1241 else
1242 v = FSMAXTYPES;
1243 if ((unsigned)v >= FSMAXTYPES) {
1244 fprintf(stderr,
1245 "line %d: Warning, unknown "
1246 "filesystem type %s\n",
1247 lineno, cp);
1248 v = FS_UNUSED;
1249 }
1250 pp->p_fstype = v;
1251 gottype:;
1252 /*
1253 * Note: NXTNUM will break us out of the
1254 * switch only!
1255 */
1256 switch (pp->p_fstype) {
1257 case FS_UNUSED:
1258 /*
1259 * allow us to accept defaults for
1260 * fsize/frag/cpg
1261 */
1262 if (tp) {
1263 NXTNUM(pp->p_fsize);
1264 if (pp->p_fsize == 0)
1265 break;
1266 NXTNUM(v);
1267 pp->p_frag = v / pp->p_fsize;
1268 }
1269 /* else default to 0's */
1270 break;
1207
1271
1208 case FS_BSDFFS:
1209 NXTNUM(pp->p_fsize);
1210 if (pp->p_fsize == 0)
1211 break;
1212 NXTNUM(v);
1213 pp->p_frag = v / pp->p_fsize;
1214 NXTNUM(pp->p_cpg);
1215 break;
1272 /* These happen to be the same */
1273 case FS_BSDFFS:
1274 case FS_BSDLFS:
1275 if (tp) {
1276 NXTNUM(pp->p_fsize);
1277 if (pp->p_fsize == 0)
1278 break;
1279 NXTNUM(v);
1280 pp->p_frag = v / pp->p_fsize;
1281 NXTNUM(pp->p_cpg);
1282 } else {
1283 /*
1284 * FIX! poor attempt at
1285 * adaptive
1286 */
1287 /* 1 GB */
1288 if (pp->p_size < 1*1024*1024*1024/lp->d_secsize) {
1289/* FIX! These are too low, but are traditional */
1290 pp->p_fsize = DEFAULT_NEWFS_BLOCK;
1291 pp->p_frag = (unsigned char) DEFAULT_NEWFS_FRAG;
1292 pp->p_cpg = DEFAULT_NEWFS_CPG;
1293 } else {
1294 pp->p_fsize = BIG_NEWFS_BLOCK;
1295 pp->p_frag = (unsigned char) BIG_NEWFS_FRAG;
1296 pp->p_cpg = BIG_NEWFS_CPG;
1297 }
1298 }
1299 break;
1300 default:
1301 break;
1302 }
1216
1303
1217 case FS_BSDLFS:
1218 NXTNUM(pp->p_fsize);
1219 if (pp->p_fsize == 0)
1220 break;
1221 NXTNUM(v);
1222 pp->p_frag = v / pp->p_fsize;
1223 NXTNUM(pp->p_cpg);
1224 break;
1225
1226 default:
1227 break;
1304 /*
1305 * note: we may not have
1306 * gotten all the entries for
1307 * the fs though if we didn't,
1308 * errors will be set.
1309 */
1310 }
1228 }
1229 continue;
1230 }
1231 fprintf(stderr, "line %d: %s: Unknown disklabel field\n",
1232 lineno, cp);
1233 errors++;
1311 }
1312 continue;
1313 }
1314 fprintf(stderr, "line %d: %s: Unknown disklabel field\n",
1315 lineno, cp);
1316 errors++;
1234 next:
1235 ;
1317 next:;
1236 }
1237 errors += checklabel(lp);
1238 return (errors == 0);
1239}
1240
1241/*
1242 * Check disklabel for errors and fill in
1243 * derived fields according to supplied values.
1244 */
1245int
1246checklabel(lp)
1247 register struct disklabel *lp;
1248{
1249 register struct partition *pp;
1250 int i, errors = 0;
1251 char part;
1318 }
1319 errors += checklabel(lp);
1320 return (errors == 0);
1321}
1322
1323/*
1324 * Check disklabel for errors and fill in
1325 * derived fields according to supplied values.
1326 */
1327int
1328checklabel(lp)
1329 register struct disklabel *lp;
1330{
1331 register struct partition *pp;
1332 int i, errors = 0;
1333 char part;
1334 unsigned long total_size,total_percent,current_offset;
1335 int seen_default_offset;
1336 int hog_part;
1337 int j;
1338 struct partition *pp2;
1252
1253 if (lp->d_secsize == 0) {
1254 fprintf(stderr, "sector size 0\n");
1255 return (1);
1256 }
1257 if (lp->d_nsectors == 0) {
1258 fprintf(stderr, "sectors/track 0\n");
1259 return (1);

--- 20 unchanged lines hidden (view full) ---

1280 if (lp->d_sbsize == 0) {
1281 fprintf(stderr, "super block size 0\n");
1282 errors++;
1283 } else if (lp->d_sbsize % lp->d_secsize)
1284 Warning("super block size %% sector-size != 0");
1285 if (lp->d_npartitions > MAXPARTITIONS)
1286 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1287 (u_long)lp->d_npartitions, MAXPARTITIONS);
1339
1340 if (lp->d_secsize == 0) {
1341 fprintf(stderr, "sector size 0\n");
1342 return (1);
1343 }
1344 if (lp->d_nsectors == 0) {
1345 fprintf(stderr, "sectors/track 0\n");
1346 return (1);

--- 20 unchanged lines hidden (view full) ---

1367 if (lp->d_sbsize == 0) {
1368 fprintf(stderr, "super block size 0\n");
1369 errors++;
1370 } else if (lp->d_sbsize % lp->d_secsize)
1371 Warning("super block size %% sector-size != 0");
1372 if (lp->d_npartitions > MAXPARTITIONS)
1373 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1374 (u_long)lp->d_npartitions, MAXPARTITIONS);
1375
1376 /* first allocate space to the partitions, then offsets */
1377 total_size = 0; /* in sectors */
1378 total_percent = 0; /* in percent */
1379 hog_part = -1;
1380 /* find all fixed partitions */
1288 for (i = 0; i < lp->d_npartitions; i++) {
1381 for (i = 0; i < lp->d_npartitions; i++) {
1382 pp = &lp->d_partitions[i];
1383 if (part_set[i]) {
1384 if (part_size_type[i] == '*') {
1385 /* partition 2 ('c') is special */
1386 if (i == FULL_DISK_PART) {
1387 pp->p_size = lp->d_secperunit;
1388 } else {
1389 if (hog_part != -1)
1390 Warning("Too many '*' partitions (%c and %c)",
1391 hog_part + 'a',i + 'a');
1392 else
1393 hog_part = i;
1394 }
1395 } else {
1396 char *type;
1397 unsigned long size;
1398
1399 size = pp->p_size;
1400 switch (part_size_type[i]) {
1401 case '%':
1402 total_percent += size;
1403 break;
1404 case 'k':
1405 case 'K':
1406 size *= 1024UL;
1407 break;
1408 case 'm':
1409 case 'M':
1410 size *= ((unsigned long) 1024*1024);
1411 break;
1412 case 'g':
1413 case 'G':
1414 size *= ((unsigned long) 1024*1024*1024);
1415 break;
1416 case '\0':
1417 break;
1418 default:
1419 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1420 break;
1421 }
1422 /* don't count %'s yet */
1423 if (part_size_type[i] != '%') {
1424 /*
1425 * for all not in sectors, convert to
1426 * sectors
1427 */
1428 if (part_size_type[i] != '\0') {
1429 if (size % lp->d_secsize != 0)
1430 Warning("partition %c not an integer number of sectors",
1431 i + 'a');
1432 size /= lp->d_secsize;
1433 pp->p_size = size;
1434 }
1435 /* else already in sectors */
1436 /* partition 2 ('c') is special */
1437 if (i != FULL_DISK_PART)
1438 total_size += size;
1439 }
1440 }
1441 }
1442 }
1443 /* handle % partitions - note %'s don't need to add up to 100! */
1444 if (total_percent != 0) {
1445 long free_space = lp->d_secperunit - total_size;
1446 if (total_percent > 100) {
1447 fprintf(stderr,"total percentage %d is greater than 100\n",
1448 total_percent);
1449 errors++;
1450 }
1451
1452 if (free_space > 0) {
1453 for (i = 0; i < lp->d_npartitions; i++) {
1454 pp = &lp->d_partitions[i];
1455 if (part_set[i] && part_size_type[i] == '%') {
1456 unsigned long old_size = pp->p_size;
1457 /* careful of overflows! and integer roundoff */
1458 pp->p_size = ((double)pp->p_size/100) * free_space;
1459 total_size += pp->p_size;
1460
1461 /* FIX we can lose a sector or so due to roundoff per
1462 partition. A more complex algorithm could avoid that */
1463 }
1464 }
1465 } else {
1466 fprintf(stderr,
1467 "%ld sectors available to give to '*' and '%' partitions\n",
1468 free_space);
1469 errors++;
1470 /* fix? set all % partitions to size 0? */
1471 }
1472 }
1473 /* give anything remaining to the hog partition */
1474 if (hog_part != -1) {
1475 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1476 total_size = lp->d_secperunit;
1477 }
1478
1479 /* Now set the offsets for each partition */
1480 current_offset = 0; /* in sectors */
1481 seen_default_offset = 0;
1482 for (i = 0; i < lp->d_npartitions; i++) {
1289 part = 'a' + i;
1290 pp = &lp->d_partitions[i];
1483 part = 'a' + i;
1484 pp = &lp->d_partitions[i];
1485 if (part_set[i]) {
1486 if (part_offset_type[i] == '*') {
1487 /* partition 2 ('c') is special */
1488 if (i == FULL_DISK_PART) {
1489 pp->p_offset = 0;
1490 } else {
1491 pp->p_offset = current_offset;
1492 seen_default_offset = 1;
1493 }
1494 } else {
1495 /* allow them to be out of order for old-style tables */
1496 /* partition 2 ('c') is special */
1497 if (pp->p_offset < current_offset &&
1498 seen_default_offset && i != FULL_DISK_PART) {
1499 fprintf(stderr,
1500"Offset %ld for partition %c overlaps previous partition which ends at %ld\n",
1501 pp->p_offset,i+'a',current_offset);
1502 fprintf(stderr,
1503"Labels with any *'s for offset must be in ascending order by sector\n");
1504 errors++;
1505 } else if (pp->p_offset != current_offset &&
1506 i != FULL_DISK_PART && seen_default_offset) {
1507 /*
1508 * this may give unneeded warnings if
1509 * partitions are out-of-order
1510 */
1511 Warning(
1512"Offset %ld for partition %c doesn't match expected value %ld",
1513 pp->p_offset, i + 'a', current_offset);
1514 }
1515 }
1516 /* partition 2 ('c') is special */
1517 if (i != FULL_DISK_PART)
1518 current_offset = pp->p_offset + pp->p_size;
1519 }
1520 }
1521
1522 for (i = 0; i < lp->d_npartitions; i++) {
1523 part = 'a' + i;
1524 pp = &lp->d_partitions[i];
1291 if (pp->p_size == 0 && pp->p_offset != 0)
1292 Warning("partition %c: size 0, but offset %lu",
1293 part, (u_long)pp->p_offset);
1294#ifdef notdef
1295 if (pp->p_size % lp->d_secpercyl)
1296 Warning("partition %c: size %% cylinder-size != 0",
1297 part);
1298 if (pp->p_offset % lp->d_secpercyl)

--- 6 unchanged lines hidden (view full) ---

1305 errors++;
1306 }
1307 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1308 fprintf(stderr,
1309 "partition %c: partition extends past end of unit\n",
1310 part);
1311 errors++;
1312 }
1525 if (pp->p_size == 0 && pp->p_offset != 0)
1526 Warning("partition %c: size 0, but offset %lu",
1527 part, (u_long)pp->p_offset);
1528#ifdef notdef
1529 if (pp->p_size % lp->d_secpercyl)
1530 Warning("partition %c: size %% cylinder-size != 0",
1531 part);
1532 if (pp->p_offset % lp->d_secpercyl)

--- 6 unchanged lines hidden (view full) ---

1539 errors++;
1540 }
1541 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1542 fprintf(stderr,
1543 "partition %c: partition extends past end of unit\n",
1544 part);
1545 errors++;
1546 }
1547 if (i == FULL_DISK_PART)
1548 {
1549 if (pp->p_fstype != FS_UNUSED)
1550 Warning("partition %c is not marked as unused!",part);
1551 if (pp->p_offset != 0)
1552 Warning("partition %c doesn't start at 0!",part);
1553 if (pp->p_size != lp->d_secperunit)
1554 Warning("partition %c doesn't cover the whole unit!",part);
1555
1556 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1557 (pp->p_size != lp->d_secperunit)) {
1558 Warning("An incorrect partition %c may cause problems for "
1559 "standard system utilities",part);
1560 }
1561 }
1562
1563 /* check for overlaps */
1564 /* this will check for all possible overlaps once and only once */
1565 for (j = 0; j < i; j++) {
1566 /* partition 2 ('c') is special */
1567 if (j != FULL_DISK_PART && i != FULL_DISK_PART &&
1568 part_set[i] && part_set[j]) {
1569 pp2 = &lp->d_partitions[j];
1570 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1571 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1572 pp2->p_offset >= pp->p_offset)) {
1573 fprintf(stderr,"partitions %c and %c overlap!\n",
1574 j + 'a', i + 'a');
1575 errors++;
1576 }
1577 }
1578 }
1313 }
1314 for (; i < MAXPARTITIONS; i++) {
1315 part = 'a' + i;
1316 pp = &lp->d_partitions[i];
1317 if (pp->p_size || pp->p_offset)
1318 Warning("unused partition %c: size %d offset %lu",
1319 'a' + i, pp->p_size, (u_long)pp->p_offset);
1320 }

--- 26 unchanged lines hidden (view full) ---

1347 return (NULL);
1348 }
1349
1350 /*
1351 * Try to use the new get-virgin-label ioctl. If it fails,
1352 * fallback to the old get-disdk-info ioctl.
1353 */
1354 if (ioctl(f, DIOCGDVIRGIN, &lab) < 0) {
1579 }
1580 for (; i < MAXPARTITIONS; i++) {
1581 part = 'a' + i;
1582 pp = &lp->d_partitions[i];
1583 if (pp->p_size || pp->p_offset)
1584 Warning("unused partition %c: size %d offset %lu",
1585 'a' + i, pp->p_size, (u_long)pp->p_offset);
1586 }

--- 26 unchanged lines hidden (view full) ---

1613 return (NULL);
1614 }
1615
1616 /*
1617 * Try to use the new get-virgin-label ioctl. If it fails,
1618 * fallback to the old get-disdk-info ioctl.
1619 */
1620 if (ioctl(f, DIOCGDVIRGIN, &lab) < 0) {
1355 if (ioctl(f, DIOCGDINFO, &lab) < 0) {
1356 warn("ioctl DIOCGDINFO");
1357 close(f);
1358 return (NULL);
1359 }
1621 if (ioctl(f, DIOCGDINFO, &lab) < 0) {
1622 warn("ioctl DIOCGDINFO");
1623 close(f);
1624 return (NULL);
1625 }
1360 }
1361 close(f);
1362 lab.d_boot0 = NULL;
1363 lab.d_boot1 = NULL;
1364 return (&lab);
1365}
1366
1367/*

--- 54 unchanged lines hidden (view full) ---

1422
1423void
1424usage()
1425{
1426#if NUMBOOT > 0
1427 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1428 "usage: disklabel [-r] disk",
1429 "\t\t(to read label)",
1626 }
1627 close(f);
1628 lab.d_boot0 = NULL;
1629 lab.d_boot1 = NULL;
1630 return (&lab);
1631}
1632
1633/*

--- 54 unchanged lines hidden (view full) ---

1688
1689void
1690usage()
1691{
1692#if NUMBOOT > 0
1693 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1694 "usage: disklabel [-r] disk",
1695 "\t\t(to read label)",
1430 " disklabel -w [-r] disk type [ packid ]",
1696 " disklabel -w [-r] [-n] disk type [ packid ]",
1431 "\t\t(to write label with existing boot program)",
1697 "\t\t(to write label with existing boot program)",
1432 " disklabel -e [-r] disk",
1698 " disklabel -e [-r] [-n] disk",
1433 "\t\t(to edit label)",
1699 "\t\t(to edit label)",
1434 " disklabel -R [-r] disk protofile",
1700 " disklabel -R [-r] [-n] disk protofile",
1435 "\t\t(to restore label with existing boot program)",
1436#if NUMBOOT > 1
1701 "\t\t(to restore label with existing boot program)",
1702#if NUMBOOT > 1
1437 " disklabel -B [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1703 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1438 "\t\t(to install boot program with existing label)",
1704 "\t\t(to install boot program with existing label)",
1439 " disklabel -w -B [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1705 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1440 "\t\t(to write label and boot program)",
1706 "\t\t(to write label and boot program)",
1441 " disklabel -R -B [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1707 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1442 "\t\t(to restore label and boot program)",
1443#else
1708 "\t\t(to restore label and boot program)",
1709#else
1444 " disklabel -B [ -b bootprog ] disk [ type ]",
1710 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1445 "\t\t(to install boot program with existing on-disk label)",
1711 "\t\t(to install boot program with existing on-disk label)",
1446 " disklabel -w -B [ -b bootprog ] disk type [ packid ]",
1712 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1447 "\t\t(to write label and install boot program)",
1713 "\t\t(to write label and install boot program)",
1448 " disklabel -R -B [ -b bootprog ] disk protofile [ type ]",
1714 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1449 "\t\t(to restore label and install boot program)",
1450#endif
1451 " disklabel [-NW] disk",
1452 "\t\t(to write disable/enable label)");
1453#else
1454 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1455 "usage: disklabel [-r] disk", "(to read label)",
1715 "\t\t(to restore label and install boot program)",
1716#endif
1717 " disklabel [-NW] disk",
1718 "\t\t(to write disable/enable label)");
1719#else
1720 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1721 "usage: disklabel [-r] disk", "(to read label)",
1456 " disklabel -w [-r] disk type [ packid ]",
1722 " disklabel -w [-r] [-n] disk type [ packid ]",
1457 "\t\t(to write label)",
1723 "\t\t(to write label)",
1458 " disklabel -e [-r] disk",
1724 " disklabel -e [-r] [-n] disk",
1459 "\t\t(to edit label)",
1725 "\t\t(to edit label)",
1460 " disklabel -R [-r] disk protofile",
1726 " disklabel -R [-r] [-n] disk protofile",
1461 "\t\t(to restore label)",
1462 " disklabel [-NW] disk",
1463 "\t\t(to write disable/enable label)");
1464#endif
1465 exit(1);
1466}
1727 "\t\t(to restore label)",
1728 " disklabel [-NW] disk",
1729 "\t\t(to write disable/enable label)");
1730#endif
1731 exit(1);
1732}