1/*-
2 * Copyright (c) 2007, 2008 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sbin/geom/class/part/geom_part.c 279324 2015-02-26 15:59:45Z ae $");
29
30#include <sys/stat.h>
31#include <sys/vtoc.h>
32
33#include <assert.h>
34#include <ctype.h>
35#include <err.h>
36#include <errno.h>
37#include <fcntl.h>
38#include <libgeom.h>
39#include <libutil.h>
40#include <paths.h>
41#include <signal.h>
42#include <stdint.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <limits.h>
46#include <inttypes.h>
47#include <string.h>
48#include <strings.h>
49#include <unistd.h>
50
51#include "core/geom.h"
52#include "misc/subr.h"
53
54#ifdef STATIC_GEOM_CLASSES
55#define PUBSYM(x) gpart_##x
56#else
57#define PUBSYM(x) x
58#endif
59
60uint32_t PUBSYM(lib_version) = G_LIB_VERSION;
61uint32_t PUBSYM(version) = 0;
62
63static char sstart[32];
64static char ssize[32];
65volatile sig_atomic_t undo_restore;
66
67#define GPART_AUTOFILL "*"
68#define GPART_FLAGS "C"
69
70#define GPART_PARAM_BOOTCODE "bootcode"
71#define GPART_PARAM_INDEX "index"
72#define GPART_PARAM_PARTCODE "partcode"
73
74static struct gclass *find_class(struct gmesh *, const char *);
75static struct ggeom * find_geom(struct gclass *, const char *);
76static const char *find_geomcfg(struct ggeom *, const char *);
77static const char *find_provcfg(struct gprovider *, const char *);
78static struct gprovider *find_provider(struct ggeom *, off_t);
79static const char *fmtsize(int64_t);
80static int gpart_autofill(struct gctl_req *);
81static int gpart_autofill_resize(struct gctl_req *);
82static void gpart_bootcode(struct gctl_req *, unsigned int);
83static void *gpart_bootfile_read(const char *, ssize_t *);
84static void gpart_issue(struct gctl_req *, unsigned int);
85static void gpart_show(struct gctl_req *, unsigned int);
86static void gpart_show_geom(struct ggeom *, const char *, int);
87static int gpart_show_hasopt(struct gctl_req *, const char *, const char *);
88static void gpart_write_partcode(struct ggeom *, int, void *, ssize_t);
89static void gpart_write_partcode_vtoc8(struct ggeom *, int, void *);
90static void gpart_print_error(const char *);
91static void gpart_backup(struct gctl_req *, unsigned int);
92static void gpart_restore(struct gctl_req *, unsigned int);
93
94struct g_command PUBSYM(class_commands)[] = {
95 { "add", 0, gpart_issue, {
96 { 'a', "alignment", GPART_AUTOFILL, G_TYPE_STRING },
97 { 'b', "start", GPART_AUTOFILL, G_TYPE_STRING },
98 { 's', "size", GPART_AUTOFILL, G_TYPE_STRING },
99 { 't', "type", NULL, G_TYPE_STRING },
100 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
101 { 'l', "label", G_VAL_OPTIONAL, G_TYPE_STRING },
102 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
103 G_OPT_SENTINEL },
104 "-t type [-a alignment] [-b start] [-s size] [-i index] "
105 "[-l label] [-f flags] geom"
106 },
107 { "backup", 0, gpart_backup, G_NULL_OPTS,
108 "geom"
109 },
110 { "bootcode", 0, gpart_bootcode, {
111 { 'b', GPART_PARAM_BOOTCODE, G_VAL_OPTIONAL, G_TYPE_STRING },
112 { 'p', GPART_PARAM_PARTCODE, G_VAL_OPTIONAL, G_TYPE_STRING },
113 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
114 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
115 G_OPT_SENTINEL },
116 "[-b bootcode] [-p partcode -i index] [-f flags] geom"
117 },
118 { "commit", 0, gpart_issue, G_NULL_OPTS,
119 "geom"
120 },
121 { "create", 0, gpart_issue, {
122 { 's', "scheme", NULL, G_TYPE_STRING },
123 { 'n', "entries", G_VAL_OPTIONAL, G_TYPE_NUMBER },
124 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
125 G_OPT_SENTINEL },
126 "-s scheme [-n entries] [-f flags] provider"
127 },
128 { "delete", 0, gpart_issue, {
129 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
130 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
131 G_OPT_SENTINEL },
132 "-i index [-f flags] geom"
133 },
134 { "destroy", 0, gpart_issue, {
135 { 'F', "force", NULL, G_TYPE_BOOL },
136 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
137 G_OPT_SENTINEL },
138 "[-F] [-f flags] geom"
139 },
140 { "modify", 0, gpart_issue, {
141 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
142 { 'l', "label", G_VAL_OPTIONAL, G_TYPE_STRING },
143 { 't', "type", G_VAL_OPTIONAL, G_TYPE_STRING },
144 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
145 G_OPT_SENTINEL },
146 "-i index [-l label] [-t type] [-f flags] geom"
147 },
148 { "set", 0, gpart_issue, {
149 { 'a', "attrib", NULL, G_TYPE_STRING },
150 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
151 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
152 G_OPT_SENTINEL },
153 "-a attrib [-i index] [-f flags] geom"
154 },
155 { "show", 0, gpart_show, {
156 { 'l', "show_label", NULL, G_TYPE_BOOL },
157 { 'r', "show_rawtype", NULL, G_TYPE_BOOL },
158 { 'p', "show_providers", NULL, G_TYPE_BOOL },
159 G_OPT_SENTINEL },
160 "[-l | -r] [-p] [geom ...]"
161 },
162 { "undo", 0, gpart_issue, G_NULL_OPTS,
163 "geom"
164 },
165 { "unset", 0, gpart_issue, {
166 { 'a', "attrib", NULL, G_TYPE_STRING },
167 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
168 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
169 G_OPT_SENTINEL },
170 "-a attrib [-i index] [-f flags] geom"
171 },
172 { "resize", 0, gpart_issue, {
173 { 'a', "alignment", GPART_AUTOFILL, G_TYPE_STRING },
174 { 's', "size", GPART_AUTOFILL, G_TYPE_STRING },
175 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
176 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
177 G_OPT_SENTINEL },
178 "-i index [-a alignment] [-s size] [-f flags] geom"
179 },
180 { "restore", 0, gpart_restore, {
181 { 'F', "force", NULL, G_TYPE_BOOL },
182 { 'l', "restore_labels", NULL, G_TYPE_BOOL },
183 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
184 G_OPT_SENTINEL },
185 "[-lF] [-f flags] provider [...]"
186 },
187 { "recover", 0, gpart_issue, {
188 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
189 G_OPT_SENTINEL },
190 "[-f flags] geom"
191 },
192 G_CMD_SENTINEL
193};
194
195static struct gclass *
196find_class(struct gmesh *mesh, const char *name)
197{
198 struct gclass *classp;
199
200 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
201 if (strcmp(classp->lg_name, name) == 0)
202 return (classp);
203 }
204 return (NULL);
205}
206
207static struct ggeom *
208find_geom(struct gclass *classp, const char *name)
209{
210 struct ggeom *gp;
211
212 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
213 name += sizeof(_PATH_DEV) - 1;
214 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
215 if (strcmp(gp->lg_name, name) == 0)
216 return (gp);
217 }
218 return (NULL);
219}
220
221static const char *
222find_geomcfg(struct ggeom *gp, const char *cfg)
223{
224 struct gconfig *gc;
225
226 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
227 if (!strcmp(gc->lg_name, cfg))
228 return (gc->lg_val);
229 }
230 return (NULL);
231}
232
233static const char *
234find_provcfg(struct gprovider *pp, const char *cfg)
235{
236 struct gconfig *gc;
237
238 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
239 if (!strcmp(gc->lg_name, cfg))
240 return (gc->lg_val);
241 }
242 return (NULL);
243}
244
245static struct gprovider *
246find_provider(struct ggeom *gp, off_t minsector)
247{
248 struct gprovider *pp, *bestpp;
249 const char *s;
250 off_t sector, bestsector;
251
252 bestpp = NULL;
253 bestsector = 0;
254 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
255 s = find_provcfg(pp, "start");
256 sector = (off_t)strtoimax(s, NULL, 0);
257 if (sector < minsector)
258 continue;
259 if (bestpp != NULL && sector >= bestsector)
260 continue;
261
262 bestpp = pp;
263 bestsector = sector;
264 }
265 return (bestpp);
266}
267
268static const char *
269fmtsize(int64_t rawsz)
270{
271 static char buf[5];
272
273 humanize_number(buf, sizeof(buf), rawsz, "", HN_AUTOSCALE,
274 HN_B | HN_NOSPACE | HN_DECIMAL);
275 return (buf);
276}
277
278static const char *
279fmtattrib(struct gprovider *pp)
280{
281 static char buf[128];
282 struct gconfig *gc;
283 u_int idx;
284
285 buf[0] = '\0';
286 idx = 0;
287 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
288 if (strcmp(gc->lg_name, "attrib") != 0)
289 continue;
290 idx += snprintf(buf + idx, sizeof(buf) - idx, "%s%s",
291 (idx == 0) ? " [" : ",", gc->lg_val);
292 }
293 if (idx > 0)
294 snprintf(buf + idx, sizeof(buf) - idx, "] ");
295 return (buf);
296}
297
298#define ALIGNDOWN(d, a) ((d) - (d) % (a))
299#define ALIGNUP(d, a) ((d) % (a) ? (d) - (d) % (a) + (a): (d))
300
301static int
302gpart_autofill_resize(struct gctl_req *req)
303{
304 struct gmesh mesh;
305 struct gclass *cp;
306 struct ggeom *gp;
307 struct gprovider *pp;
308 off_t last, size, start, new_size;
309 off_t lba, new_lba, alignment, offset;
310 const char *s;
311 int error, idx, has_alignment;
312
313 idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
314 if (idx < 1)
315 errx(EXIT_FAILURE, "invalid partition index");
316
317 error = geom_gettree(&mesh);
318 if (error)
319 return (error);
320 s = gctl_get_ascii(req, "class");
321 if (s == NULL)
322 abort();
323 cp = find_class(&mesh, s);
324 if (cp == NULL)
325 errx(EXIT_FAILURE, "Class %s not found.", s);
326 s = gctl_get_ascii(req, "arg0");
327 if (s == NULL)
328 abort();
329 gp = find_geom(cp, s);
330 if (gp == NULL)
331 errx(EXIT_FAILURE, "No such geom: %s.", s);
332 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
333 if (pp == NULL)
334 errx(EXIT_FAILURE, "Provider for geom %s not found.", s);
335
336 s = gctl_get_ascii(req, "alignment");
337 has_alignment = (*s == '*') ? 0 : 1;
338 alignment = 1;
339 if (has_alignment) {
340 error = g_parse_lba(s, pp->lg_sectorsize, &alignment);
341 if (error)
342 errc(EXIT_FAILURE, error, "Invalid alignment param");
343 if (alignment == 0)
344 errx(EXIT_FAILURE, "Invalid alignment param");
345 } else {
346 lba = pp->lg_stripesize / pp->lg_sectorsize;
347 if (lba > 0)
348 alignment = lba;
349 }
350 error = gctl_delete_param(req, "alignment");
351 if (error)
352 errc(EXIT_FAILURE, error, "internal error");
353
354 s = gctl_get_ascii(req, "size");
355 if (*s == '*')
356 new_size = 0;
357 else {
358 error = g_parse_lba(s, pp->lg_sectorsize, &new_size);
359 if (error)
360 errc(EXIT_FAILURE, error, "Invalid size param");
361 /* no autofill necessary. */
362 if (has_alignment == 0)
363 goto done;
364 }
365
366 offset = (pp->lg_stripeoffset / pp->lg_sectorsize) % alignment;
367 s = find_geomcfg(gp, "last");
368 if (s == NULL)
369 errx(EXIT_FAILURE, "Final block not found for geom %s",
370 gp->lg_name);
371 last = (off_t)strtoimax(s, NULL, 0);
372 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
373 s = find_provcfg(pp, "index");
374 if (s == NULL)
375 continue;
376 if (atoi(s) == idx)
377 break;
378 }
379 if (pp == NULL)
380 errx(EXIT_FAILURE, "invalid partition index");
381
382 s = find_provcfg(pp, "start");
383 start = (off_t)strtoimax(s, NULL, 0);
384 s = find_provcfg(pp, "end");
385 lba = (off_t)strtoimax(s, NULL, 0);
386 size = lba - start + 1;
387
388 pp = find_provider(gp, lba + 1);
389 if (new_size > 0 && (new_size <= size || pp == NULL)) {
390 /* The start offset may be not aligned, so we align the end
391 * offset and then calculate the size.
392 */
393 new_size = ALIGNDOWN(start + offset + new_size,
394 alignment) - start - offset;
395 goto done;
396 }
397 if (pp == NULL) {
398 new_size = ALIGNDOWN(last + offset + 1, alignment) -
399 start - offset;
400 if (new_size < size)
401 return (ENOSPC);
402 } else {
403 s = find_provcfg(pp, "start");
404 new_lba = (off_t)strtoimax(s, NULL, 0);
405 /*
406 * Is there any free space between current and
407 * next providers?
408 */
409 new_lba = ALIGNDOWN(new_lba + offset, alignment) - offset;
410 if (new_lba > lba)
411 new_size = new_lba - start;
412 else {
413 geom_deletetree(&mesh);
414 return (ENOSPC);
415 }
416 }
417done:
418 snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)new_size);
419 gctl_change_param(req, "size", -1, ssize);
420 geom_deletetree(&mesh);
421 return (0);
422}
423
424static int
425gpart_autofill(struct gctl_req *req)
426{
427 struct gmesh mesh;
428 struct gclass *cp;
429 struct ggeom *gp;
430 struct gprovider *pp;
431 off_t first, last, a_first;
432 off_t size, start, a_lba;
433 off_t lba, len, alignment, offset;
434 uintmax_t grade;
435 const char *s;
436 int error, has_size, has_start, has_alignment;
437
438 s = gctl_get_ascii(req, "verb");
439 if (strcmp(s, "resize") == 0)
440 return gpart_autofill_resize(req);
441 if (strcmp(s, "add") != 0)
442 return (0);
443
444 error = geom_gettree(&mesh);
445 if (error)
446 return (error);
447 s = gctl_get_ascii(req, "class");
448 if (s == NULL)
449 abort();
450 cp = find_class(&mesh, s);
451 if (cp == NULL)
452 errx(EXIT_FAILURE, "Class %s not found.", s);
453 s = gctl_get_ascii(req, "arg0");
454 if (s == NULL)
455 abort();
456 gp = find_geom(cp, s);
457 if (gp == NULL) {
458 if (g_device_path(s) == NULL) {
459 errx(EXIT_FAILURE, "No such geom %s.", s);
460 } else {
461 /*
462 * We don't free memory allocated by g_device_path() as
463 * we are about to exit.
464 */
465 errx(EXIT_FAILURE,
466 "No partitioning scheme found on geom %s. Create one first using 'gpart create'.",
467 s);
468 }
469 }
470 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
471 if (pp == NULL)
472 errx(EXIT_FAILURE, "Provider for geom %s not found.", s);
473
474 s = gctl_get_ascii(req, "alignment");
475 has_alignment = (*s == '*') ? 0 : 1;
476 alignment = 1;
477 if (has_alignment) {
478 error = g_parse_lba(s, pp->lg_sectorsize, &alignment);
479 if (error)
480 errc(EXIT_FAILURE, error, "Invalid alignment param");
481 if (alignment == 0)
482 errx(EXIT_FAILURE, "Invalid alignment param");
483 }
484 error = gctl_delete_param(req, "alignment");
485 if (error)
486 errc(EXIT_FAILURE, error, "internal error");
487
488 s = gctl_get_ascii(req, "size");
489 has_size = (*s == '*') ? 0 : 1;
490 size = 0;
491 if (has_size) {
492 error = g_parse_lba(s, pp->lg_sectorsize, &size);
493 if (error)
494 errc(EXIT_FAILURE, error, "Invalid size param");
495 }
496
497 s = gctl_get_ascii(req, "start");
498 has_start = (*s == '*') ? 0 : 1;
499 start = 0ULL;
500 if (has_start) {
501 error = g_parse_lba(s, pp->lg_sectorsize, &start);
502 if (error)
503 errc(EXIT_FAILURE, error, "Invalid start param");
504 }
505
506 /* No autofill necessary. */
507 if (has_size && has_start && !has_alignment)
508 goto done;
509
510 len = pp->lg_stripesize / pp->lg_sectorsize;
511 if (len > 0 && !has_alignment)
512 alignment = len;
513
514 /* Adjust parameters to stripeoffset */
515 offset = (pp->lg_stripeoffset / pp->lg_sectorsize) % alignment;
516 start = ALIGNUP(start + offset, alignment);
517 if (size > alignment)
518 size = ALIGNDOWN(size, alignment);
519
520 s = find_geomcfg(gp, "first");
521 if (s == NULL)
522 errx(EXIT_FAILURE, "Starting block not found for geom %s",
523 gp->lg_name);
524 first = (off_t)strtoimax(s, NULL, 0);
525 s = find_geomcfg(gp, "last");
526 if (s == NULL)
527 errx(EXIT_FAILURE, "Final block not found for geom %s",
528 gp->lg_name);
529 last = (off_t)strtoimax(s, NULL, 0);
530 grade = ~0ULL;
531 a_first = ALIGNUP(first + offset, alignment);
532 last = ALIGNDOWN(last + offset, alignment);
533 if (a_first < start)
534 a_first = start;
535 while ((pp = find_provider(gp, first)) != NULL) {
536 s = find_provcfg(pp, "start");
537 lba = (off_t)strtoimax(s, NULL, 0);
538 a_lba = ALIGNDOWN(lba + offset, alignment);
539 if (first < a_lba && a_first < a_lba) {
540 /* Free space [first, lba> */
541 len = a_lba - a_first;
542 if (has_size) {
543 if (len >= size &&
544 (uintmax_t)(len - size) < grade) {
545 start = a_first;
546 grade = len - size;
547 }
548 } else if (has_start) {
549 if (start >= a_first && start < a_lba) {
550 size = a_lba - start;
551 grade = start - a_first;
552 }
553 } else {
554 if (grade == ~0ULL || len > size) {
555 start = a_first;
556 size = len;
557 grade = 0;
558 }
559 }
560 }
561
562 s = find_provcfg(pp, "end");
563 first = (off_t)strtoimax(s, NULL, 0) + 1;
564 if (first + offset > a_first)
565 a_first = ALIGNUP(first + offset, alignment);
566 }
567 if (a_first <= last) {
568 /* Free space [first-last] */
569 len = ALIGNDOWN(last - a_first + 1, alignment);
570 if (has_size) {
571 if (len >= size &&
572 (uintmax_t)(len - size) < grade) {
573 start = a_first;
574 grade = len - size;
575 }
576 } else if (has_start) {
577 if (start >= a_first && start <= last) {
578 size = ALIGNDOWN(last - start + 1, alignment);
579 grade = start - a_first;
580 }
581 } else {
582 if (grade == ~0ULL || len > size) {
583 start = a_first;
584 size = len;
585 grade = 0;
586 }
587 }
588 }
589 if (grade == ~0ULL) {
590 geom_deletetree(&mesh);
591 return (ENOSPC);
592 }
593 start -= offset; /* Return back to real offset */
594done:
595 snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)size);
596 gctl_change_param(req, "size", -1, ssize);
597 snprintf(sstart, sizeof(sstart), "%jd", (intmax_t)start);
598 gctl_change_param(req, "start", -1, sstart);
599 geom_deletetree(&mesh);
600 return (0);
601}
602
603static void
604gpart_show_geom(struct ggeom *gp, const char *element, int show_providers)
605{
606 struct gprovider *pp;
607 const char *s, *scheme;
608 off_t first, last, sector, end;
609 off_t length, secsz;
610 int idx, wblocks, wname, wmax;
611
612 scheme = find_geomcfg(gp, "scheme");
613 if (scheme == NULL)
614 errx(EXIT_FAILURE, "Scheme not found for geom %s", gp->lg_name);
615 s = find_geomcfg(gp, "first");
616 if (s == NULL)
617 errx(EXIT_FAILURE, "Starting block not found for geom %s",
618 gp->lg_name);
619 first = (off_t)strtoimax(s, NULL, 0);
620 s = find_geomcfg(gp, "last");
621 if (s == NULL)
622 errx(EXIT_FAILURE, "Final block not found for geom %s",
623 gp->lg_name);
624 last = (off_t)strtoimax(s, NULL, 0);
625 wblocks = strlen(s);
626 s = find_geomcfg(gp, "state");
627 if (s == NULL)
628 errx(EXIT_FAILURE, "State not found for geom %s", gp->lg_name);
629 if (s != NULL && *s != 'C')
630 s = NULL;
631 wmax = strlen(gp->lg_name);
632 if (show_providers) {
633 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
634 wname = strlen(pp->lg_name);
635 if (wname > wmax)
636 wmax = wname;
637 }
638 }
639 wname = wmax;
640 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
641 secsz = pp->lg_sectorsize;
642 printf("=>%*jd %*jd %*s %s (%s)%s\n",
643 wblocks, (intmax_t)first, wblocks, (intmax_t)(last - first + 1),
644 wname, gp->lg_name,
645 scheme, fmtsize(pp->lg_mediasize),
646 s ? " [CORRUPT]": "");
647
648 while ((pp = find_provider(gp, first)) != NULL) {
649 s = find_provcfg(pp, "start");
650 sector = (off_t)strtoimax(s, NULL, 0);
651
652 s = find_provcfg(pp, "end");
653 end = (off_t)strtoimax(s, NULL, 0);
654 length = end - sector + 1;
655
656 s = find_provcfg(pp, "index");
657 idx = atoi(s);
658 if (first < sector) {
659 printf(" %*jd %*jd %*s - free - (%s)\n",
660 wblocks, (intmax_t)first, wblocks,
661 (intmax_t)(sector - first), wname, "",
662 fmtsize((sector - first) * secsz));
663 }
664 if (show_providers) {
665 printf(" %*jd %*jd %*s %s %s (%s)\n",
666 wblocks, (intmax_t)sector, wblocks,
667 (intmax_t)length, wname, pp->lg_name,
668 find_provcfg(pp, element), fmtattrib(pp),
669 fmtsize(pp->lg_mediasize));
670 } else
671 printf(" %*jd %*jd %*d %s %s (%s)\n",
672 wblocks, (intmax_t)sector, wblocks,
673 (intmax_t)length, wname, idx,
674 find_provcfg(pp, element), fmtattrib(pp),
675 fmtsize(pp->lg_mediasize));
676 first = end + 1;
677 }
678 if (first <= last) {
679 length = last - first + 1;
680 printf(" %*jd %*jd %*s - free - (%s)\n",
681 wblocks, (intmax_t)first, wblocks, (intmax_t)length,
682 wname, "",
683 fmtsize(length * secsz));
684 }
685 printf("\n");
686}
687
688static int
689gpart_show_hasopt(struct gctl_req *req, const char *opt, const char *elt)
690{
691
692 if (!gctl_get_int(req, "%s", opt))
693 return (0);
694
695 if (elt != NULL)
696 errx(EXIT_FAILURE, "-l and -r are mutually exclusive");
697
698 return (1);
699}
700
701static void
702gpart_show(struct gctl_req *req, unsigned int fl __unused)
703{
704 struct gmesh mesh;
705 struct gclass *classp;
706 struct ggeom *gp;
707 const char *element, *name;
708 int error, i, nargs, show_providers;
709
710 element = NULL;
711 if (gpart_show_hasopt(req, "show_label", element))
712 element = "label";
713 if (gpart_show_hasopt(req, "show_rawtype", element))
714 element = "rawtype";
715 if (element == NULL)
716 element = "type";
717
718 name = gctl_get_ascii(req, "class");
719 if (name == NULL)
720 abort();
721 error = geom_gettree(&mesh);
722 if (error != 0)
723 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
724 classp = find_class(&mesh, name);
725 if (classp == NULL) {
726 geom_deletetree(&mesh);
727 errx(EXIT_FAILURE, "Class %s not found.", name);
728 }
729 show_providers = gctl_get_int(req, "show_providers");
730 nargs = gctl_get_int(req, "nargs");
731 if (nargs > 0) {
732 for (i = 0; i < nargs; i++) {
733 name = gctl_get_ascii(req, "arg%d", i);
734 gp = find_geom(classp, name);
735 if (gp != NULL)
736 gpart_show_geom(gp, element, show_providers);
737 else
738 errx(EXIT_FAILURE, "No such geom: %s.", name);
739 }
740 } else {
741 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
742 gpart_show_geom(gp, element, show_providers);
743 }
744 }
745 geom_deletetree(&mesh);
746}
747
748static void
749gpart_backup(struct gctl_req *req, unsigned int fl __unused)
750{
751 struct gmesh mesh;
752 struct gclass *classp;
753 struct gprovider *pp;
754 struct ggeom *gp;
755 const char *s, *scheme;
756 off_t sector, end;
757 off_t length;
758 int error, i, windex, wblocks, wtype;
759
760 if (gctl_get_int(req, "nargs") != 1)
761 errx(EXIT_FAILURE, "Invalid number of arguments.");
762 error = geom_gettree(&mesh);
763 if (error != 0)
764 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
765 s = gctl_get_ascii(req, "class");
766 if (s == NULL)
767 abort();
768 classp = find_class(&mesh, s);
769 if (classp == NULL) {
770 geom_deletetree(&mesh);
771 errx(EXIT_FAILURE, "Class %s not found.", s);
772 }
773 s = gctl_get_ascii(req, "arg0");
774 if (s == NULL)
775 abort();
776 gp = find_geom(classp, s);
777 if (gp == NULL)
778 errx(EXIT_FAILURE, "No such geom: %s.", s);
779 scheme = find_geomcfg(gp, "scheme");
780 if (scheme == NULL)
781 abort();
782 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
783 s = find_geomcfg(gp, "last");
784 if (s == NULL)
785 abort();
786 wblocks = strlen(s);
787 wtype = 0;
788 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
789 s = find_provcfg(pp, "type");
790 i = strlen(s);
791 if (i > wtype)
792 wtype = i;
793 }
794 s = find_geomcfg(gp, "entries");
795 if (s == NULL)
796 abort();
797 windex = strlen(s);
798 printf("%s %s\n", scheme, s);
799 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
800 s = find_provcfg(pp, "start");
801 sector = (off_t)strtoimax(s, NULL, 0);
802
803 s = find_provcfg(pp, "end");
804 end = (off_t)strtoimax(s, NULL, 0);
805 length = end - sector + 1;
806
807 s = find_provcfg(pp, "label");
808 printf("%-*s %*s %*jd %*jd %s %s\n",
809 windex, find_provcfg(pp, "index"),
810 wtype, find_provcfg(pp, "type"),
811 wblocks, (intmax_t)sector,
812 wblocks, (intmax_t)length,
813 (s != NULL) ? s: "", fmtattrib(pp));
814 }
815 geom_deletetree(&mesh);
816}
817
818static int
819skip_line(const char *p)
820{
821
822 while (*p != '\0') {
823 if (*p == '#')
824 return (1);
825 if (isspace(*p) == 0)
826 return (0);
827 p++;
828 }
829 return (1);
830}
831
832static void
833gpart_sighndl(int sig __unused)
834{
835 undo_restore = 1;
836}
837
838static void
839gpart_restore(struct gctl_req *req, unsigned int fl __unused)
840{
841 struct gmesh mesh;
842 struct gclass *classp;
843 struct gctl_req *r;
844 struct ggeom *gp;
845 struct sigaction si_sa;
846 const char *s, *flags, *errstr, *label;
847 char **ap, *argv[6], line[BUFSIZ], *pline;
848 int error, forced, i, l, nargs, created, rl;
849 intmax_t n;
850
851 nargs = gctl_get_int(req, "nargs");
852 if (nargs < 1)
853 errx(EXIT_FAILURE, "Invalid number of arguments.");
854
855 forced = gctl_get_int(req, "force");
856 flags = gctl_get_ascii(req, "flags");
857 rl = gctl_get_int(req, "restore_labels");
858 s = gctl_get_ascii(req, "class");
859 if (s == NULL)
860 abort();
861 error = geom_gettree(&mesh);
862 if (error != 0)
863 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
864 classp = find_class(&mesh, s);
865 if (classp == NULL) {
866 geom_deletetree(&mesh);
867 errx(EXIT_FAILURE, "Class %s not found.", s);
868 }
869
870 sigemptyset(&si_sa.sa_mask);
871 si_sa.sa_flags = 0;
872 si_sa.sa_handler = gpart_sighndl;
873 if (sigaction(SIGINT, &si_sa, 0) == -1)
874 err(EXIT_FAILURE, "sigaction SIGINT");
875
876 if (forced) {
877 /* destroy existent partition table before restore */
878 for (i = 0; i < nargs; i++) {
879 s = gctl_get_ascii(req, "arg%d", i);
880 gp = find_geom(classp, s);
881 if (gp != NULL) {
882 r = gctl_get_handle();
883 gctl_ro_param(r, "class", -1,
884 classp->lg_name);
885 gctl_ro_param(r, "verb", -1, "destroy");
886 gctl_ro_param(r, "flags", -1, "restore");
887 gctl_ro_param(r, "force", sizeof(forced),
888 &forced);
889 gctl_ro_param(r, "arg0", -1, s);
890 errstr = gctl_issue(r);
891 if (errstr != NULL && errstr[0] != '\0') {
892 gpart_print_error(errstr);
893 gctl_free(r);
894 goto backout;
895 }
896 gctl_free(r);
897 }
898 }
899 }
900 created = 0;
901 while (undo_restore == 0 &&
902 fgets(line, sizeof(line) - 1, stdin) != NULL) {
903 /* Format of backup entries:
904 * <scheme name> <number of entries>
905 * <index> <type> <start> <size> [label] ['['attrib[,attrib]']']
906 */
907 pline = (char *)line;
908 pline[strlen(line) - 1] = 0;
909 if (skip_line(pline))
910 continue;
911 for (ap = argv;
912 (*ap = strsep(&pline, " \t")) != NULL;)
913 if (**ap != '\0' && ++ap >= &argv[6])
914 break;
915 l = ap - &argv[0];
916 label = pline = NULL;
917 if (l == 1 || l == 2) { /* create table */
918 if (created)
919 errx(EXIT_FAILURE, "Incorrect backup format.");
920 if (l == 2)
921 n = strtoimax(argv[1], NULL, 0);
922 for (i = 0; i < nargs; i++) {
923 s = gctl_get_ascii(req, "arg%d", i);
924 r = gctl_get_handle();
925 gctl_ro_param(r, "class", -1,
926 classp->lg_name);
927 gctl_ro_param(r, "verb", -1, "create");
928 gctl_ro_param(r, "scheme", -1, argv[0]);
929 if (l == 2)
930 gctl_ro_param(r, "entries",
931 sizeof(n), &n);
932 gctl_ro_param(r, "flags", -1, "restore");
933 gctl_ro_param(r, "arg0", -1, s);
934 errstr = gctl_issue(r);
935 if (errstr != NULL && errstr[0] != '\0') {
936 gpart_print_error(errstr);
937 gctl_free(r);
938 goto backout;
939 }
940 gctl_free(r);
941 }
942 created = 1;
943 continue;
944 } else if (l < 4 || created == 0)
945 errx(EXIT_FAILURE, "Incorrect backup format.");
946 else if (l == 5) {
947 if (strchr(argv[4], '[') == NULL)
948 label = argv[4];
949 else
950 pline = argv[4];
951 } else if (l == 6) {
952 label = argv[4];
953 pline = argv[5];
954 }
955 /* Add partitions to each table */
956 for (i = 0; i < nargs; i++) {
957 s = gctl_get_ascii(req, "arg%d", i);
958 r = gctl_get_handle();
959 n = strtoimax(argv[0], NULL, 0);
960 gctl_ro_param(r, "class", -1, classp->lg_name);
961 gctl_ro_param(r, "verb", -1, "add");
962 gctl_ro_param(r, "flags", -1, "restore");
963 gctl_ro_param(r, GPART_PARAM_INDEX, sizeof(n), &n);
964 gctl_ro_param(r, "type", -1, argv[1]);
965 gctl_ro_param(r, "start", -1, argv[2]);
966 gctl_ro_param(r, "size", -1, argv[3]);
967 if (rl != 0 && label != NULL)
968 gctl_ro_param(r, "label", -1, argv[4]);
969 gctl_ro_param(r, "alignment", -1, GPART_AUTOFILL);
970 gctl_ro_param(r, "arg0", -1, s);
971 error = gpart_autofill(r);
972 if (error != 0)
973 errc(EXIT_FAILURE, error, "autofill");
974 errstr = gctl_issue(r);
975 if (errstr != NULL && errstr[0] != '\0') {
976 gpart_print_error(errstr);
977 gctl_free(r);
978 goto backout;
979 }
980 gctl_free(r);
981 }
982 if (pline == NULL || *pline != '[')
983 continue;
984 /* set attributes */
985 pline++;
986 for (ap = argv;
987 (*ap = strsep(&pline, ",]")) != NULL;)
988 if (**ap != '\0' && ++ap >= &argv[6])
989 break;
990 for (i = 0; i < nargs; i++) {
991 l = ap - &argv[0];
992 s = gctl_get_ascii(req, "arg%d", i);
993 while (l > 0) {
994 r = gctl_get_handle();
995 gctl_ro_param(r, "class", -1, classp->lg_name);
996 gctl_ro_param(r, "verb", -1, "set");
997 gctl_ro_param(r, "flags", -1, "restore");
998 gctl_ro_param(r, GPART_PARAM_INDEX,
999 sizeof(n), &n);
1000 gctl_ro_param(r, "attrib", -1, argv[--l]);
1001 gctl_ro_param(r, "arg0", -1, s);
1002 errstr = gctl_issue(r);
1003 if (errstr != NULL && errstr[0] != '\0') {
1004 gpart_print_error(errstr);
1005 gctl_free(r);
1006 goto backout;
1007 }
1008 gctl_free(r);
1009 }
1010 }
1011 }
1012 if (undo_restore)
1013 goto backout;
1014 /* commit changes if needed */
1015 if (strchr(flags, 'C') != NULL) {
1016 for (i = 0; i < nargs; i++) {
1017 s = gctl_get_ascii(req, "arg%d", i);
1018 r = gctl_get_handle();
1019 gctl_ro_param(r, "class", -1, classp->lg_name);
1020 gctl_ro_param(r, "verb", -1, "commit");
1021 gctl_ro_param(r, "arg0", -1, s);
1022 errstr = gctl_issue(r);
1023 if (errstr != NULL && errstr[0] != '\0') {
1024 gpart_print_error(errstr);
1025 gctl_free(r);
1026 goto backout;
1027 }
1028 gctl_free(r);
1029 }
1030 }
1031 gctl_free(req);
1032 geom_deletetree(&mesh);
1033 exit(EXIT_SUCCESS);
1034
1035backout:
1036 for (i = 0; i < nargs; i++) {
1037 s = gctl_get_ascii(req, "arg%d", i);
1038 r = gctl_get_handle();
1039 gctl_ro_param(r, "class", -1, classp->lg_name);
1040 gctl_ro_param(r, "verb", -1, "undo");
1041 gctl_ro_param(r, "arg0", -1, s);
1042 gctl_issue(r);
1043 gctl_free(r);
1044 }
1045 gctl_free(req);
1046 geom_deletetree(&mesh);
1047 exit(EXIT_FAILURE);
1048}
1049
1050static void *
1051gpart_bootfile_read(const char *bootfile, ssize_t *size)
1052{
1053 struct stat sb;
1054 void *code;
1055 int fd;
1056
1057 if (stat(bootfile, &sb) == -1)
1058 err(EXIT_FAILURE, "%s", bootfile);
1059 if (!S_ISREG(sb.st_mode))
1060 errx(EXIT_FAILURE, "%s: not a regular file", bootfile);
1061 if (sb.st_size == 0)
1062 errx(EXIT_FAILURE, "%s: empty file", bootfile);
1063 if (*size > 0 && sb.st_size > *size)
1064 errx(EXIT_FAILURE, "%s: file too big (%zu limit)", bootfile,
1065 *size);
1066
1067 *size = sb.st_size;
1068
1069 fd = open(bootfile, O_RDONLY);
1070 if (fd == -1)
1071 err(EXIT_FAILURE, "%s", bootfile);
1072 code = malloc(*size);
1073 if (code == NULL)
1074 err(EXIT_FAILURE, NULL);
1075 if (read(fd, code, *size) != *size)
1076 err(EXIT_FAILURE, "%s", bootfile);
1077 close(fd);
1078
1079 return (code);
1080}
1081
1082static void
1083gpart_write_partcode(struct ggeom *gp, int idx, void *code, ssize_t size)
1084{
1085 char dsf[128];
1086 struct gprovider *pp;
1087 const char *s;
1088 char *buf;
1089 off_t bsize;
1090 int fd;
1091
1092 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1093 s = find_provcfg(pp, "index");
1094 if (s == NULL)
1095 continue;
1096 if (atoi(s) == idx)
1097 break;
1098 }
1099
1100 if (pp != NULL) {
1101 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1102 fd = open(dsf, O_WRONLY);
1103 if (fd == -1)
1104 err(EXIT_FAILURE, "%s", dsf);
1105 if (lseek(fd, size, SEEK_SET) != size)
1106 errx(EXIT_FAILURE, "%s: not enough space", dsf);
1107 if (lseek(fd, 0, SEEK_SET) != 0)
1108 err(EXIT_FAILURE, "%s", dsf);
1109
1110 /*
1111 * When writing to a disk device, the write must be
1112 * sector aligned and not write to any partial sectors,
1113 * so round up the buffer size to the next sector and zero it.
1114 */
1115 bsize = (size + pp->lg_sectorsize - 1) /
1116 pp->lg_sectorsize * pp->lg_sectorsize;
1117 buf = calloc(1, bsize);
1118 if (buf == NULL)
1119 err(EXIT_FAILURE, "%s", dsf);
1120 bcopy(code, buf, size);
1121 if (write(fd, buf, bsize) != bsize)
1122 err(EXIT_FAILURE, "%s", dsf);
1123 free(buf);
1124 close(fd);
1125 } else
1126 errx(EXIT_FAILURE, "invalid partition index");
1127}
1128
1129static void
1130gpart_write_partcode_vtoc8(struct ggeom *gp, int idx, void *code)
1131{
1132 char dsf[128];
1133 struct gprovider *pp;
1134 const char *s;
1135 int installed, fd;
1136
1137 installed = 0;
1138 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1139 s = find_provcfg(pp, "index");
1140 if (s == NULL)
1141 continue;
1142 if (idx != 0 && atoi(s) != idx)
1143 continue;
1144 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1145 if (pp->lg_sectorsize != sizeof(struct vtoc8))
1146 errx(EXIT_FAILURE, "%s: unexpected sector "
1147 "size (%d)\n", dsf, pp->lg_sectorsize);
1148 fd = open(dsf, O_WRONLY);
1149 if (fd == -1)
1150 err(EXIT_FAILURE, "%s", dsf);
1151 if (lseek(fd, VTOC_BOOTSIZE, SEEK_SET) != VTOC_BOOTSIZE)
1152 continue;
1153 /*
1154 * We ignore the first VTOC_BOOTSIZE bytes of boot code in
1155 * order to avoid overwriting the label.
1156 */
1157 if (lseek(fd, sizeof(struct vtoc8), SEEK_SET) !=
1158 sizeof(struct vtoc8))
1159 err(EXIT_FAILURE, "%s", dsf);
1160 if (write(fd, (caddr_t)code + sizeof(struct vtoc8),
1161 VTOC_BOOTSIZE - sizeof(struct vtoc8)) != VTOC_BOOTSIZE -
1162 sizeof(struct vtoc8))
1163 err(EXIT_FAILURE, "%s", dsf);
1164 installed++;
1165 close(fd);
1166 if (idx != 0 && atoi(s) == idx)
1167 break;
1168 }
1169 if (installed == 0)
1170 errx(EXIT_FAILURE, "%s: no partitions", gp->lg_name);
1171}
1172
1173static void
1174gpart_bootcode(struct gctl_req *req, unsigned int fl)
1175{
1176 struct gmesh mesh;
1177 struct gclass *classp;
1178 struct ggeom *gp;
1179 const char *s;
1180 void *bootcode, *partcode;
1181 size_t bootsize, partsize;
1182 int error, idx, vtoc8;
1183
1184 if (gctl_has_param(req, GPART_PARAM_BOOTCODE)) {
1185 s = gctl_get_ascii(req, GPART_PARAM_BOOTCODE);
1186 bootsize = 800 * 1024; /* Arbitrary limit. */
1187 bootcode = gpart_bootfile_read(s, &bootsize);
1188 error = gctl_change_param(req, GPART_PARAM_BOOTCODE, bootsize,
1189 bootcode);
1190 if (error)
1191 errc(EXIT_FAILURE, error, "internal error");
1192 } else {
1193 bootcode = NULL;
1194 bootsize = 0;
1195 }
1196
1197 s = gctl_get_ascii(req, "class");
1198 if (s == NULL)
1199 abort();
1200 error = geom_gettree(&mesh);
1201 if (error != 0)
1202 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
1203 classp = find_class(&mesh, s);
1204 if (classp == NULL) {
1205 geom_deletetree(&mesh);
1206 errx(EXIT_FAILURE, "Class %s not found.", s);
1207 }
1208 if (gctl_get_int(req, "nargs") != 1)
1209 errx(EXIT_FAILURE, "Invalid number of arguments.");
1210 s = gctl_get_ascii(req, "arg0");
1211 if (s == NULL)
1212 abort();
1213 gp = find_geom(classp, s);
1214 if (gp == NULL)
1215 errx(EXIT_FAILURE, "No such geom: %s.", s);
1216 s = find_geomcfg(gp, "scheme");
1217 if (s == NULL)
1218 errx(EXIT_FAILURE, "Scheme not found for geom %s", gp->lg_name);
1219 vtoc8 = 0;
1220 if (strcmp(s, "VTOC8") == 0)
1221 vtoc8 = 1;
1222
1223 if (gctl_has_param(req, GPART_PARAM_PARTCODE)) {
1224 s = gctl_get_ascii(req, GPART_PARAM_PARTCODE);
1225 partsize = vtoc8 != 0 ? VTOC_BOOTSIZE : bootsize * 1024;
1226 partcode = gpart_bootfile_read(s, &partsize);
1227 error = gctl_delete_param(req, GPART_PARAM_PARTCODE);
1228 if (error)
1229 errc(EXIT_FAILURE, error, "internal error");
1230 } else {
1231 partcode = NULL;
1232 partsize = 0;
1233 }
1234
1235 if (gctl_has_param(req, GPART_PARAM_INDEX)) {
1236 if (partcode == NULL)
1237 errx(EXIT_FAILURE, "-i is only valid with -p");
1238 idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
1239 if (idx < 1)
1240 errx(EXIT_FAILURE, "invalid partition index");
1241 error = gctl_delete_param(req, GPART_PARAM_INDEX);
1242 if (error)
1243 errc(EXIT_FAILURE, error, "internal error");
1244 } else
1245 idx = 0;
1246
1247 if (partcode != NULL) {
1248 if (vtoc8 == 0) {
1249 if (idx == 0)
1250 errx(EXIT_FAILURE, "missing -i option");
1251 gpart_write_partcode(gp, idx, partcode, partsize);
1252 } else {
1253 if (partsize != VTOC_BOOTSIZE)
1254 errx(EXIT_FAILURE, "invalid bootcode");
1255 gpart_write_partcode_vtoc8(gp, idx, partcode);
1256 }
1257 } else
1258 if (bootcode == NULL)
1259 errx(EXIT_FAILURE, "no -b nor -p");
1260
1261 if (bootcode != NULL)
1262 gpart_issue(req, fl);
1263
1264 geom_deletetree(&mesh);
1265}
1266
1267static void
1268gpart_print_error(const char *errstr)
1269{
1270 char *errmsg;
1271 int error;
1272
1273 error = strtol(errstr, &errmsg, 0);
1274 if (errmsg != errstr) {
1275 while (errmsg[0] == ' ')
1276 errmsg++;
1277 if (errmsg[0] != '\0')
1278 warnc(error, "%s", errmsg);
1279 else
1280 warnc(error, NULL);
1281 } else
1282 warnx("%s", errmsg);
1283}
1284
1285static void
1286gpart_issue(struct gctl_req *req, unsigned int fl __unused)
1287{
1288 char buf[4096];
1289 const char *errstr;
1290 int error, status;
1291
1292 if (gctl_get_int(req, "nargs") != 1)
1293 errx(EXIT_FAILURE, "Invalid number of arguments.");
1294 (void)gctl_delete_param(req, "nargs");
1295
1296 /* autofill parameters (if applicable). */
1297 error = gpart_autofill(req);
1298 if (error) {
1299 warnc(error, "autofill");
1300 status = EXIT_FAILURE;
1301 goto done;
1302 }
1303
1304 bzero(buf, sizeof(buf));
1305 gctl_rw_param(req, "output", sizeof(buf), buf);
1306 errstr = gctl_issue(req);
1307 if (errstr == NULL || errstr[0] == '\0') {
1308 if (buf[0] != '\0')
1309 printf("%s", buf);
1310 status = EXIT_SUCCESS;
1311 goto done;
1312 }
1313
1314 gpart_print_error(errstr);
1315 status = EXIT_FAILURE;
1316
1317 done:
1318 gctl_free(req);
1319 exit(status);
1320}
2 * Copyright (c) 2007, 2008 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sbin/geom/class/part/geom_part.c 279324 2015-02-26 15:59:45Z ae $");
29
30#include <sys/stat.h>
31#include <sys/vtoc.h>
32
33#include <assert.h>
34#include <ctype.h>
35#include <err.h>
36#include <errno.h>
37#include <fcntl.h>
38#include <libgeom.h>
39#include <libutil.h>
40#include <paths.h>
41#include <signal.h>
42#include <stdint.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <limits.h>
46#include <inttypes.h>
47#include <string.h>
48#include <strings.h>
49#include <unistd.h>
50
51#include "core/geom.h"
52#include "misc/subr.h"
53
54#ifdef STATIC_GEOM_CLASSES
55#define PUBSYM(x) gpart_##x
56#else
57#define PUBSYM(x) x
58#endif
59
60uint32_t PUBSYM(lib_version) = G_LIB_VERSION;
61uint32_t PUBSYM(version) = 0;
62
63static char sstart[32];
64static char ssize[32];
65volatile sig_atomic_t undo_restore;
66
67#define GPART_AUTOFILL "*"
68#define GPART_FLAGS "C"
69
70#define GPART_PARAM_BOOTCODE "bootcode"
71#define GPART_PARAM_INDEX "index"
72#define GPART_PARAM_PARTCODE "partcode"
73
74static struct gclass *find_class(struct gmesh *, const char *);
75static struct ggeom * find_geom(struct gclass *, const char *);
76static const char *find_geomcfg(struct ggeom *, const char *);
77static const char *find_provcfg(struct gprovider *, const char *);
78static struct gprovider *find_provider(struct ggeom *, off_t);
79static const char *fmtsize(int64_t);
80static int gpart_autofill(struct gctl_req *);
81static int gpart_autofill_resize(struct gctl_req *);
82static void gpart_bootcode(struct gctl_req *, unsigned int);
83static void *gpart_bootfile_read(const char *, ssize_t *);
84static void gpart_issue(struct gctl_req *, unsigned int);
85static void gpart_show(struct gctl_req *, unsigned int);
86static void gpart_show_geom(struct ggeom *, const char *, int);
87static int gpart_show_hasopt(struct gctl_req *, const char *, const char *);
88static void gpart_write_partcode(struct ggeom *, int, void *, ssize_t);
89static void gpart_write_partcode_vtoc8(struct ggeom *, int, void *);
90static void gpart_print_error(const char *);
91static void gpart_backup(struct gctl_req *, unsigned int);
92static void gpart_restore(struct gctl_req *, unsigned int);
93
94struct g_command PUBSYM(class_commands)[] = {
95 { "add", 0, gpart_issue, {
96 { 'a', "alignment", GPART_AUTOFILL, G_TYPE_STRING },
97 { 'b', "start", GPART_AUTOFILL, G_TYPE_STRING },
98 { 's', "size", GPART_AUTOFILL, G_TYPE_STRING },
99 { 't', "type", NULL, G_TYPE_STRING },
100 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
101 { 'l', "label", G_VAL_OPTIONAL, G_TYPE_STRING },
102 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
103 G_OPT_SENTINEL },
104 "-t type [-a alignment] [-b start] [-s size] [-i index] "
105 "[-l label] [-f flags] geom"
106 },
107 { "backup", 0, gpart_backup, G_NULL_OPTS,
108 "geom"
109 },
110 { "bootcode", 0, gpart_bootcode, {
111 { 'b', GPART_PARAM_BOOTCODE, G_VAL_OPTIONAL, G_TYPE_STRING },
112 { 'p', GPART_PARAM_PARTCODE, G_VAL_OPTIONAL, G_TYPE_STRING },
113 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
114 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
115 G_OPT_SENTINEL },
116 "[-b bootcode] [-p partcode -i index] [-f flags] geom"
117 },
118 { "commit", 0, gpart_issue, G_NULL_OPTS,
119 "geom"
120 },
121 { "create", 0, gpart_issue, {
122 { 's', "scheme", NULL, G_TYPE_STRING },
123 { 'n', "entries", G_VAL_OPTIONAL, G_TYPE_NUMBER },
124 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
125 G_OPT_SENTINEL },
126 "-s scheme [-n entries] [-f flags] provider"
127 },
128 { "delete", 0, gpart_issue, {
129 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
130 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
131 G_OPT_SENTINEL },
132 "-i index [-f flags] geom"
133 },
134 { "destroy", 0, gpart_issue, {
135 { 'F', "force", NULL, G_TYPE_BOOL },
136 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
137 G_OPT_SENTINEL },
138 "[-F] [-f flags] geom"
139 },
140 { "modify", 0, gpart_issue, {
141 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
142 { 'l', "label", G_VAL_OPTIONAL, G_TYPE_STRING },
143 { 't', "type", G_VAL_OPTIONAL, G_TYPE_STRING },
144 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
145 G_OPT_SENTINEL },
146 "-i index [-l label] [-t type] [-f flags] geom"
147 },
148 { "set", 0, gpart_issue, {
149 { 'a', "attrib", NULL, G_TYPE_STRING },
150 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
151 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
152 G_OPT_SENTINEL },
153 "-a attrib [-i index] [-f flags] geom"
154 },
155 { "show", 0, gpart_show, {
156 { 'l', "show_label", NULL, G_TYPE_BOOL },
157 { 'r', "show_rawtype", NULL, G_TYPE_BOOL },
158 { 'p', "show_providers", NULL, G_TYPE_BOOL },
159 G_OPT_SENTINEL },
160 "[-l | -r] [-p] [geom ...]"
161 },
162 { "undo", 0, gpart_issue, G_NULL_OPTS,
163 "geom"
164 },
165 { "unset", 0, gpart_issue, {
166 { 'a', "attrib", NULL, G_TYPE_STRING },
167 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
168 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
169 G_OPT_SENTINEL },
170 "-a attrib [-i index] [-f flags] geom"
171 },
172 { "resize", 0, gpart_issue, {
173 { 'a', "alignment", GPART_AUTOFILL, G_TYPE_STRING },
174 { 's', "size", GPART_AUTOFILL, G_TYPE_STRING },
175 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
176 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
177 G_OPT_SENTINEL },
178 "-i index [-a alignment] [-s size] [-f flags] geom"
179 },
180 { "restore", 0, gpart_restore, {
181 { 'F', "force", NULL, G_TYPE_BOOL },
182 { 'l', "restore_labels", NULL, G_TYPE_BOOL },
183 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
184 G_OPT_SENTINEL },
185 "[-lF] [-f flags] provider [...]"
186 },
187 { "recover", 0, gpart_issue, {
188 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
189 G_OPT_SENTINEL },
190 "[-f flags] geom"
191 },
192 G_CMD_SENTINEL
193};
194
195static struct gclass *
196find_class(struct gmesh *mesh, const char *name)
197{
198 struct gclass *classp;
199
200 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
201 if (strcmp(classp->lg_name, name) == 0)
202 return (classp);
203 }
204 return (NULL);
205}
206
207static struct ggeom *
208find_geom(struct gclass *classp, const char *name)
209{
210 struct ggeom *gp;
211
212 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
213 name += sizeof(_PATH_DEV) - 1;
214 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
215 if (strcmp(gp->lg_name, name) == 0)
216 return (gp);
217 }
218 return (NULL);
219}
220
221static const char *
222find_geomcfg(struct ggeom *gp, const char *cfg)
223{
224 struct gconfig *gc;
225
226 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
227 if (!strcmp(gc->lg_name, cfg))
228 return (gc->lg_val);
229 }
230 return (NULL);
231}
232
233static const char *
234find_provcfg(struct gprovider *pp, const char *cfg)
235{
236 struct gconfig *gc;
237
238 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
239 if (!strcmp(gc->lg_name, cfg))
240 return (gc->lg_val);
241 }
242 return (NULL);
243}
244
245static struct gprovider *
246find_provider(struct ggeom *gp, off_t minsector)
247{
248 struct gprovider *pp, *bestpp;
249 const char *s;
250 off_t sector, bestsector;
251
252 bestpp = NULL;
253 bestsector = 0;
254 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
255 s = find_provcfg(pp, "start");
256 sector = (off_t)strtoimax(s, NULL, 0);
257 if (sector < minsector)
258 continue;
259 if (bestpp != NULL && sector >= bestsector)
260 continue;
261
262 bestpp = pp;
263 bestsector = sector;
264 }
265 return (bestpp);
266}
267
268static const char *
269fmtsize(int64_t rawsz)
270{
271 static char buf[5];
272
273 humanize_number(buf, sizeof(buf), rawsz, "", HN_AUTOSCALE,
274 HN_B | HN_NOSPACE | HN_DECIMAL);
275 return (buf);
276}
277
278static const char *
279fmtattrib(struct gprovider *pp)
280{
281 static char buf[128];
282 struct gconfig *gc;
283 u_int idx;
284
285 buf[0] = '\0';
286 idx = 0;
287 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
288 if (strcmp(gc->lg_name, "attrib") != 0)
289 continue;
290 idx += snprintf(buf + idx, sizeof(buf) - idx, "%s%s",
291 (idx == 0) ? " [" : ",", gc->lg_val);
292 }
293 if (idx > 0)
294 snprintf(buf + idx, sizeof(buf) - idx, "] ");
295 return (buf);
296}
297
298#define ALIGNDOWN(d, a) ((d) - (d) % (a))
299#define ALIGNUP(d, a) ((d) % (a) ? (d) - (d) % (a) + (a): (d))
300
301static int
302gpart_autofill_resize(struct gctl_req *req)
303{
304 struct gmesh mesh;
305 struct gclass *cp;
306 struct ggeom *gp;
307 struct gprovider *pp;
308 off_t last, size, start, new_size;
309 off_t lba, new_lba, alignment, offset;
310 const char *s;
311 int error, idx, has_alignment;
312
313 idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
314 if (idx < 1)
315 errx(EXIT_FAILURE, "invalid partition index");
316
317 error = geom_gettree(&mesh);
318 if (error)
319 return (error);
320 s = gctl_get_ascii(req, "class");
321 if (s == NULL)
322 abort();
323 cp = find_class(&mesh, s);
324 if (cp == NULL)
325 errx(EXIT_FAILURE, "Class %s not found.", s);
326 s = gctl_get_ascii(req, "arg0");
327 if (s == NULL)
328 abort();
329 gp = find_geom(cp, s);
330 if (gp == NULL)
331 errx(EXIT_FAILURE, "No such geom: %s.", s);
332 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
333 if (pp == NULL)
334 errx(EXIT_FAILURE, "Provider for geom %s not found.", s);
335
336 s = gctl_get_ascii(req, "alignment");
337 has_alignment = (*s == '*') ? 0 : 1;
338 alignment = 1;
339 if (has_alignment) {
340 error = g_parse_lba(s, pp->lg_sectorsize, &alignment);
341 if (error)
342 errc(EXIT_FAILURE, error, "Invalid alignment param");
343 if (alignment == 0)
344 errx(EXIT_FAILURE, "Invalid alignment param");
345 } else {
346 lba = pp->lg_stripesize / pp->lg_sectorsize;
347 if (lba > 0)
348 alignment = lba;
349 }
350 error = gctl_delete_param(req, "alignment");
351 if (error)
352 errc(EXIT_FAILURE, error, "internal error");
353
354 s = gctl_get_ascii(req, "size");
355 if (*s == '*')
356 new_size = 0;
357 else {
358 error = g_parse_lba(s, pp->lg_sectorsize, &new_size);
359 if (error)
360 errc(EXIT_FAILURE, error, "Invalid size param");
361 /* no autofill necessary. */
362 if (has_alignment == 0)
363 goto done;
364 }
365
366 offset = (pp->lg_stripeoffset / pp->lg_sectorsize) % alignment;
367 s = find_geomcfg(gp, "last");
368 if (s == NULL)
369 errx(EXIT_FAILURE, "Final block not found for geom %s",
370 gp->lg_name);
371 last = (off_t)strtoimax(s, NULL, 0);
372 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
373 s = find_provcfg(pp, "index");
374 if (s == NULL)
375 continue;
376 if (atoi(s) == idx)
377 break;
378 }
379 if (pp == NULL)
380 errx(EXIT_FAILURE, "invalid partition index");
381
382 s = find_provcfg(pp, "start");
383 start = (off_t)strtoimax(s, NULL, 0);
384 s = find_provcfg(pp, "end");
385 lba = (off_t)strtoimax(s, NULL, 0);
386 size = lba - start + 1;
387
388 pp = find_provider(gp, lba + 1);
389 if (new_size > 0 && (new_size <= size || pp == NULL)) {
390 /* The start offset may be not aligned, so we align the end
391 * offset and then calculate the size.
392 */
393 new_size = ALIGNDOWN(start + offset + new_size,
394 alignment) - start - offset;
395 goto done;
396 }
397 if (pp == NULL) {
398 new_size = ALIGNDOWN(last + offset + 1, alignment) -
399 start - offset;
400 if (new_size < size)
401 return (ENOSPC);
402 } else {
403 s = find_provcfg(pp, "start");
404 new_lba = (off_t)strtoimax(s, NULL, 0);
405 /*
406 * Is there any free space between current and
407 * next providers?
408 */
409 new_lba = ALIGNDOWN(new_lba + offset, alignment) - offset;
410 if (new_lba > lba)
411 new_size = new_lba - start;
412 else {
413 geom_deletetree(&mesh);
414 return (ENOSPC);
415 }
416 }
417done:
418 snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)new_size);
419 gctl_change_param(req, "size", -1, ssize);
420 geom_deletetree(&mesh);
421 return (0);
422}
423
424static int
425gpart_autofill(struct gctl_req *req)
426{
427 struct gmesh mesh;
428 struct gclass *cp;
429 struct ggeom *gp;
430 struct gprovider *pp;
431 off_t first, last, a_first;
432 off_t size, start, a_lba;
433 off_t lba, len, alignment, offset;
434 uintmax_t grade;
435 const char *s;
436 int error, has_size, has_start, has_alignment;
437
438 s = gctl_get_ascii(req, "verb");
439 if (strcmp(s, "resize") == 0)
440 return gpart_autofill_resize(req);
441 if (strcmp(s, "add") != 0)
442 return (0);
443
444 error = geom_gettree(&mesh);
445 if (error)
446 return (error);
447 s = gctl_get_ascii(req, "class");
448 if (s == NULL)
449 abort();
450 cp = find_class(&mesh, s);
451 if (cp == NULL)
452 errx(EXIT_FAILURE, "Class %s not found.", s);
453 s = gctl_get_ascii(req, "arg0");
454 if (s == NULL)
455 abort();
456 gp = find_geom(cp, s);
457 if (gp == NULL) {
458 if (g_device_path(s) == NULL) {
459 errx(EXIT_FAILURE, "No such geom %s.", s);
460 } else {
461 /*
462 * We don't free memory allocated by g_device_path() as
463 * we are about to exit.
464 */
465 errx(EXIT_FAILURE,
466 "No partitioning scheme found on geom %s. Create one first using 'gpart create'.",
467 s);
468 }
469 }
470 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
471 if (pp == NULL)
472 errx(EXIT_FAILURE, "Provider for geom %s not found.", s);
473
474 s = gctl_get_ascii(req, "alignment");
475 has_alignment = (*s == '*') ? 0 : 1;
476 alignment = 1;
477 if (has_alignment) {
478 error = g_parse_lba(s, pp->lg_sectorsize, &alignment);
479 if (error)
480 errc(EXIT_FAILURE, error, "Invalid alignment param");
481 if (alignment == 0)
482 errx(EXIT_FAILURE, "Invalid alignment param");
483 }
484 error = gctl_delete_param(req, "alignment");
485 if (error)
486 errc(EXIT_FAILURE, error, "internal error");
487
488 s = gctl_get_ascii(req, "size");
489 has_size = (*s == '*') ? 0 : 1;
490 size = 0;
491 if (has_size) {
492 error = g_parse_lba(s, pp->lg_sectorsize, &size);
493 if (error)
494 errc(EXIT_FAILURE, error, "Invalid size param");
495 }
496
497 s = gctl_get_ascii(req, "start");
498 has_start = (*s == '*') ? 0 : 1;
499 start = 0ULL;
500 if (has_start) {
501 error = g_parse_lba(s, pp->lg_sectorsize, &start);
502 if (error)
503 errc(EXIT_FAILURE, error, "Invalid start param");
504 }
505
506 /* No autofill necessary. */
507 if (has_size && has_start && !has_alignment)
508 goto done;
509
510 len = pp->lg_stripesize / pp->lg_sectorsize;
511 if (len > 0 && !has_alignment)
512 alignment = len;
513
514 /* Adjust parameters to stripeoffset */
515 offset = (pp->lg_stripeoffset / pp->lg_sectorsize) % alignment;
516 start = ALIGNUP(start + offset, alignment);
517 if (size > alignment)
518 size = ALIGNDOWN(size, alignment);
519
520 s = find_geomcfg(gp, "first");
521 if (s == NULL)
522 errx(EXIT_FAILURE, "Starting block not found for geom %s",
523 gp->lg_name);
524 first = (off_t)strtoimax(s, NULL, 0);
525 s = find_geomcfg(gp, "last");
526 if (s == NULL)
527 errx(EXIT_FAILURE, "Final block not found for geom %s",
528 gp->lg_name);
529 last = (off_t)strtoimax(s, NULL, 0);
530 grade = ~0ULL;
531 a_first = ALIGNUP(first + offset, alignment);
532 last = ALIGNDOWN(last + offset, alignment);
533 if (a_first < start)
534 a_first = start;
535 while ((pp = find_provider(gp, first)) != NULL) {
536 s = find_provcfg(pp, "start");
537 lba = (off_t)strtoimax(s, NULL, 0);
538 a_lba = ALIGNDOWN(lba + offset, alignment);
539 if (first < a_lba && a_first < a_lba) {
540 /* Free space [first, lba> */
541 len = a_lba - a_first;
542 if (has_size) {
543 if (len >= size &&
544 (uintmax_t)(len - size) < grade) {
545 start = a_first;
546 grade = len - size;
547 }
548 } else if (has_start) {
549 if (start >= a_first && start < a_lba) {
550 size = a_lba - start;
551 grade = start - a_first;
552 }
553 } else {
554 if (grade == ~0ULL || len > size) {
555 start = a_first;
556 size = len;
557 grade = 0;
558 }
559 }
560 }
561
562 s = find_provcfg(pp, "end");
563 first = (off_t)strtoimax(s, NULL, 0) + 1;
564 if (first + offset > a_first)
565 a_first = ALIGNUP(first + offset, alignment);
566 }
567 if (a_first <= last) {
568 /* Free space [first-last] */
569 len = ALIGNDOWN(last - a_first + 1, alignment);
570 if (has_size) {
571 if (len >= size &&
572 (uintmax_t)(len - size) < grade) {
573 start = a_first;
574 grade = len - size;
575 }
576 } else if (has_start) {
577 if (start >= a_first && start <= last) {
578 size = ALIGNDOWN(last - start + 1, alignment);
579 grade = start - a_first;
580 }
581 } else {
582 if (grade == ~0ULL || len > size) {
583 start = a_first;
584 size = len;
585 grade = 0;
586 }
587 }
588 }
589 if (grade == ~0ULL) {
590 geom_deletetree(&mesh);
591 return (ENOSPC);
592 }
593 start -= offset; /* Return back to real offset */
594done:
595 snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)size);
596 gctl_change_param(req, "size", -1, ssize);
597 snprintf(sstart, sizeof(sstart), "%jd", (intmax_t)start);
598 gctl_change_param(req, "start", -1, sstart);
599 geom_deletetree(&mesh);
600 return (0);
601}
602
603static void
604gpart_show_geom(struct ggeom *gp, const char *element, int show_providers)
605{
606 struct gprovider *pp;
607 const char *s, *scheme;
608 off_t first, last, sector, end;
609 off_t length, secsz;
610 int idx, wblocks, wname, wmax;
611
612 scheme = find_geomcfg(gp, "scheme");
613 if (scheme == NULL)
614 errx(EXIT_FAILURE, "Scheme not found for geom %s", gp->lg_name);
615 s = find_geomcfg(gp, "first");
616 if (s == NULL)
617 errx(EXIT_FAILURE, "Starting block not found for geom %s",
618 gp->lg_name);
619 first = (off_t)strtoimax(s, NULL, 0);
620 s = find_geomcfg(gp, "last");
621 if (s == NULL)
622 errx(EXIT_FAILURE, "Final block not found for geom %s",
623 gp->lg_name);
624 last = (off_t)strtoimax(s, NULL, 0);
625 wblocks = strlen(s);
626 s = find_geomcfg(gp, "state");
627 if (s == NULL)
628 errx(EXIT_FAILURE, "State not found for geom %s", gp->lg_name);
629 if (s != NULL && *s != 'C')
630 s = NULL;
631 wmax = strlen(gp->lg_name);
632 if (show_providers) {
633 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
634 wname = strlen(pp->lg_name);
635 if (wname > wmax)
636 wmax = wname;
637 }
638 }
639 wname = wmax;
640 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
641 secsz = pp->lg_sectorsize;
642 printf("=>%*jd %*jd %*s %s (%s)%s\n",
643 wblocks, (intmax_t)first, wblocks, (intmax_t)(last - first + 1),
644 wname, gp->lg_name,
645 scheme, fmtsize(pp->lg_mediasize),
646 s ? " [CORRUPT]": "");
647
648 while ((pp = find_provider(gp, first)) != NULL) {
649 s = find_provcfg(pp, "start");
650 sector = (off_t)strtoimax(s, NULL, 0);
651
652 s = find_provcfg(pp, "end");
653 end = (off_t)strtoimax(s, NULL, 0);
654 length = end - sector + 1;
655
656 s = find_provcfg(pp, "index");
657 idx = atoi(s);
658 if (first < sector) {
659 printf(" %*jd %*jd %*s - free - (%s)\n",
660 wblocks, (intmax_t)first, wblocks,
661 (intmax_t)(sector - first), wname, "",
662 fmtsize((sector - first) * secsz));
663 }
664 if (show_providers) {
665 printf(" %*jd %*jd %*s %s %s (%s)\n",
666 wblocks, (intmax_t)sector, wblocks,
667 (intmax_t)length, wname, pp->lg_name,
668 find_provcfg(pp, element), fmtattrib(pp),
669 fmtsize(pp->lg_mediasize));
670 } else
671 printf(" %*jd %*jd %*d %s %s (%s)\n",
672 wblocks, (intmax_t)sector, wblocks,
673 (intmax_t)length, wname, idx,
674 find_provcfg(pp, element), fmtattrib(pp),
675 fmtsize(pp->lg_mediasize));
676 first = end + 1;
677 }
678 if (first <= last) {
679 length = last - first + 1;
680 printf(" %*jd %*jd %*s - free - (%s)\n",
681 wblocks, (intmax_t)first, wblocks, (intmax_t)length,
682 wname, "",
683 fmtsize(length * secsz));
684 }
685 printf("\n");
686}
687
688static int
689gpart_show_hasopt(struct gctl_req *req, const char *opt, const char *elt)
690{
691
692 if (!gctl_get_int(req, "%s", opt))
693 return (0);
694
695 if (elt != NULL)
696 errx(EXIT_FAILURE, "-l and -r are mutually exclusive");
697
698 return (1);
699}
700
701static void
702gpart_show(struct gctl_req *req, unsigned int fl __unused)
703{
704 struct gmesh mesh;
705 struct gclass *classp;
706 struct ggeom *gp;
707 const char *element, *name;
708 int error, i, nargs, show_providers;
709
710 element = NULL;
711 if (gpart_show_hasopt(req, "show_label", element))
712 element = "label";
713 if (gpart_show_hasopt(req, "show_rawtype", element))
714 element = "rawtype";
715 if (element == NULL)
716 element = "type";
717
718 name = gctl_get_ascii(req, "class");
719 if (name == NULL)
720 abort();
721 error = geom_gettree(&mesh);
722 if (error != 0)
723 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
724 classp = find_class(&mesh, name);
725 if (classp == NULL) {
726 geom_deletetree(&mesh);
727 errx(EXIT_FAILURE, "Class %s not found.", name);
728 }
729 show_providers = gctl_get_int(req, "show_providers");
730 nargs = gctl_get_int(req, "nargs");
731 if (nargs > 0) {
732 for (i = 0; i < nargs; i++) {
733 name = gctl_get_ascii(req, "arg%d", i);
734 gp = find_geom(classp, name);
735 if (gp != NULL)
736 gpart_show_geom(gp, element, show_providers);
737 else
738 errx(EXIT_FAILURE, "No such geom: %s.", name);
739 }
740 } else {
741 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
742 gpart_show_geom(gp, element, show_providers);
743 }
744 }
745 geom_deletetree(&mesh);
746}
747
748static void
749gpart_backup(struct gctl_req *req, unsigned int fl __unused)
750{
751 struct gmesh mesh;
752 struct gclass *classp;
753 struct gprovider *pp;
754 struct ggeom *gp;
755 const char *s, *scheme;
756 off_t sector, end;
757 off_t length;
758 int error, i, windex, wblocks, wtype;
759
760 if (gctl_get_int(req, "nargs") != 1)
761 errx(EXIT_FAILURE, "Invalid number of arguments.");
762 error = geom_gettree(&mesh);
763 if (error != 0)
764 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
765 s = gctl_get_ascii(req, "class");
766 if (s == NULL)
767 abort();
768 classp = find_class(&mesh, s);
769 if (classp == NULL) {
770 geom_deletetree(&mesh);
771 errx(EXIT_FAILURE, "Class %s not found.", s);
772 }
773 s = gctl_get_ascii(req, "arg0");
774 if (s == NULL)
775 abort();
776 gp = find_geom(classp, s);
777 if (gp == NULL)
778 errx(EXIT_FAILURE, "No such geom: %s.", s);
779 scheme = find_geomcfg(gp, "scheme");
780 if (scheme == NULL)
781 abort();
782 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
783 s = find_geomcfg(gp, "last");
784 if (s == NULL)
785 abort();
786 wblocks = strlen(s);
787 wtype = 0;
788 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
789 s = find_provcfg(pp, "type");
790 i = strlen(s);
791 if (i > wtype)
792 wtype = i;
793 }
794 s = find_geomcfg(gp, "entries");
795 if (s == NULL)
796 abort();
797 windex = strlen(s);
798 printf("%s %s\n", scheme, s);
799 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
800 s = find_provcfg(pp, "start");
801 sector = (off_t)strtoimax(s, NULL, 0);
802
803 s = find_provcfg(pp, "end");
804 end = (off_t)strtoimax(s, NULL, 0);
805 length = end - sector + 1;
806
807 s = find_provcfg(pp, "label");
808 printf("%-*s %*s %*jd %*jd %s %s\n",
809 windex, find_provcfg(pp, "index"),
810 wtype, find_provcfg(pp, "type"),
811 wblocks, (intmax_t)sector,
812 wblocks, (intmax_t)length,
813 (s != NULL) ? s: "", fmtattrib(pp));
814 }
815 geom_deletetree(&mesh);
816}
817
818static int
819skip_line(const char *p)
820{
821
822 while (*p != '\0') {
823 if (*p == '#')
824 return (1);
825 if (isspace(*p) == 0)
826 return (0);
827 p++;
828 }
829 return (1);
830}
831
832static void
833gpart_sighndl(int sig __unused)
834{
835 undo_restore = 1;
836}
837
838static void
839gpart_restore(struct gctl_req *req, unsigned int fl __unused)
840{
841 struct gmesh mesh;
842 struct gclass *classp;
843 struct gctl_req *r;
844 struct ggeom *gp;
845 struct sigaction si_sa;
846 const char *s, *flags, *errstr, *label;
847 char **ap, *argv[6], line[BUFSIZ], *pline;
848 int error, forced, i, l, nargs, created, rl;
849 intmax_t n;
850
851 nargs = gctl_get_int(req, "nargs");
852 if (nargs < 1)
853 errx(EXIT_FAILURE, "Invalid number of arguments.");
854
855 forced = gctl_get_int(req, "force");
856 flags = gctl_get_ascii(req, "flags");
857 rl = gctl_get_int(req, "restore_labels");
858 s = gctl_get_ascii(req, "class");
859 if (s == NULL)
860 abort();
861 error = geom_gettree(&mesh);
862 if (error != 0)
863 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
864 classp = find_class(&mesh, s);
865 if (classp == NULL) {
866 geom_deletetree(&mesh);
867 errx(EXIT_FAILURE, "Class %s not found.", s);
868 }
869
870 sigemptyset(&si_sa.sa_mask);
871 si_sa.sa_flags = 0;
872 si_sa.sa_handler = gpart_sighndl;
873 if (sigaction(SIGINT, &si_sa, 0) == -1)
874 err(EXIT_FAILURE, "sigaction SIGINT");
875
876 if (forced) {
877 /* destroy existent partition table before restore */
878 for (i = 0; i < nargs; i++) {
879 s = gctl_get_ascii(req, "arg%d", i);
880 gp = find_geom(classp, s);
881 if (gp != NULL) {
882 r = gctl_get_handle();
883 gctl_ro_param(r, "class", -1,
884 classp->lg_name);
885 gctl_ro_param(r, "verb", -1, "destroy");
886 gctl_ro_param(r, "flags", -1, "restore");
887 gctl_ro_param(r, "force", sizeof(forced),
888 &forced);
889 gctl_ro_param(r, "arg0", -1, s);
890 errstr = gctl_issue(r);
891 if (errstr != NULL && errstr[0] != '\0') {
892 gpart_print_error(errstr);
893 gctl_free(r);
894 goto backout;
895 }
896 gctl_free(r);
897 }
898 }
899 }
900 created = 0;
901 while (undo_restore == 0 &&
902 fgets(line, sizeof(line) - 1, stdin) != NULL) {
903 /* Format of backup entries:
904 * <scheme name> <number of entries>
905 * <index> <type> <start> <size> [label] ['['attrib[,attrib]']']
906 */
907 pline = (char *)line;
908 pline[strlen(line) - 1] = 0;
909 if (skip_line(pline))
910 continue;
911 for (ap = argv;
912 (*ap = strsep(&pline, " \t")) != NULL;)
913 if (**ap != '\0' && ++ap >= &argv[6])
914 break;
915 l = ap - &argv[0];
916 label = pline = NULL;
917 if (l == 1 || l == 2) { /* create table */
918 if (created)
919 errx(EXIT_FAILURE, "Incorrect backup format.");
920 if (l == 2)
921 n = strtoimax(argv[1], NULL, 0);
922 for (i = 0; i < nargs; i++) {
923 s = gctl_get_ascii(req, "arg%d", i);
924 r = gctl_get_handle();
925 gctl_ro_param(r, "class", -1,
926 classp->lg_name);
927 gctl_ro_param(r, "verb", -1, "create");
928 gctl_ro_param(r, "scheme", -1, argv[0]);
929 if (l == 2)
930 gctl_ro_param(r, "entries",
931 sizeof(n), &n);
932 gctl_ro_param(r, "flags", -1, "restore");
933 gctl_ro_param(r, "arg0", -1, s);
934 errstr = gctl_issue(r);
935 if (errstr != NULL && errstr[0] != '\0') {
936 gpart_print_error(errstr);
937 gctl_free(r);
938 goto backout;
939 }
940 gctl_free(r);
941 }
942 created = 1;
943 continue;
944 } else if (l < 4 || created == 0)
945 errx(EXIT_FAILURE, "Incorrect backup format.");
946 else if (l == 5) {
947 if (strchr(argv[4], '[') == NULL)
948 label = argv[4];
949 else
950 pline = argv[4];
951 } else if (l == 6) {
952 label = argv[4];
953 pline = argv[5];
954 }
955 /* Add partitions to each table */
956 for (i = 0; i < nargs; i++) {
957 s = gctl_get_ascii(req, "arg%d", i);
958 r = gctl_get_handle();
959 n = strtoimax(argv[0], NULL, 0);
960 gctl_ro_param(r, "class", -1, classp->lg_name);
961 gctl_ro_param(r, "verb", -1, "add");
962 gctl_ro_param(r, "flags", -1, "restore");
963 gctl_ro_param(r, GPART_PARAM_INDEX, sizeof(n), &n);
964 gctl_ro_param(r, "type", -1, argv[1]);
965 gctl_ro_param(r, "start", -1, argv[2]);
966 gctl_ro_param(r, "size", -1, argv[3]);
967 if (rl != 0 && label != NULL)
968 gctl_ro_param(r, "label", -1, argv[4]);
969 gctl_ro_param(r, "alignment", -1, GPART_AUTOFILL);
970 gctl_ro_param(r, "arg0", -1, s);
971 error = gpart_autofill(r);
972 if (error != 0)
973 errc(EXIT_FAILURE, error, "autofill");
974 errstr = gctl_issue(r);
975 if (errstr != NULL && errstr[0] != '\0') {
976 gpart_print_error(errstr);
977 gctl_free(r);
978 goto backout;
979 }
980 gctl_free(r);
981 }
982 if (pline == NULL || *pline != '[')
983 continue;
984 /* set attributes */
985 pline++;
986 for (ap = argv;
987 (*ap = strsep(&pline, ",]")) != NULL;)
988 if (**ap != '\0' && ++ap >= &argv[6])
989 break;
990 for (i = 0; i < nargs; i++) {
991 l = ap - &argv[0];
992 s = gctl_get_ascii(req, "arg%d", i);
993 while (l > 0) {
994 r = gctl_get_handle();
995 gctl_ro_param(r, "class", -1, classp->lg_name);
996 gctl_ro_param(r, "verb", -1, "set");
997 gctl_ro_param(r, "flags", -1, "restore");
998 gctl_ro_param(r, GPART_PARAM_INDEX,
999 sizeof(n), &n);
1000 gctl_ro_param(r, "attrib", -1, argv[--l]);
1001 gctl_ro_param(r, "arg0", -1, s);
1002 errstr = gctl_issue(r);
1003 if (errstr != NULL && errstr[0] != '\0') {
1004 gpart_print_error(errstr);
1005 gctl_free(r);
1006 goto backout;
1007 }
1008 gctl_free(r);
1009 }
1010 }
1011 }
1012 if (undo_restore)
1013 goto backout;
1014 /* commit changes if needed */
1015 if (strchr(flags, 'C') != NULL) {
1016 for (i = 0; i < nargs; i++) {
1017 s = gctl_get_ascii(req, "arg%d", i);
1018 r = gctl_get_handle();
1019 gctl_ro_param(r, "class", -1, classp->lg_name);
1020 gctl_ro_param(r, "verb", -1, "commit");
1021 gctl_ro_param(r, "arg0", -1, s);
1022 errstr = gctl_issue(r);
1023 if (errstr != NULL && errstr[0] != '\0') {
1024 gpart_print_error(errstr);
1025 gctl_free(r);
1026 goto backout;
1027 }
1028 gctl_free(r);
1029 }
1030 }
1031 gctl_free(req);
1032 geom_deletetree(&mesh);
1033 exit(EXIT_SUCCESS);
1034
1035backout:
1036 for (i = 0; i < nargs; i++) {
1037 s = gctl_get_ascii(req, "arg%d", i);
1038 r = gctl_get_handle();
1039 gctl_ro_param(r, "class", -1, classp->lg_name);
1040 gctl_ro_param(r, "verb", -1, "undo");
1041 gctl_ro_param(r, "arg0", -1, s);
1042 gctl_issue(r);
1043 gctl_free(r);
1044 }
1045 gctl_free(req);
1046 geom_deletetree(&mesh);
1047 exit(EXIT_FAILURE);
1048}
1049
1050static void *
1051gpart_bootfile_read(const char *bootfile, ssize_t *size)
1052{
1053 struct stat sb;
1054 void *code;
1055 int fd;
1056
1057 if (stat(bootfile, &sb) == -1)
1058 err(EXIT_FAILURE, "%s", bootfile);
1059 if (!S_ISREG(sb.st_mode))
1060 errx(EXIT_FAILURE, "%s: not a regular file", bootfile);
1061 if (sb.st_size == 0)
1062 errx(EXIT_FAILURE, "%s: empty file", bootfile);
1063 if (*size > 0 && sb.st_size > *size)
1064 errx(EXIT_FAILURE, "%s: file too big (%zu limit)", bootfile,
1065 *size);
1066
1067 *size = sb.st_size;
1068
1069 fd = open(bootfile, O_RDONLY);
1070 if (fd == -1)
1071 err(EXIT_FAILURE, "%s", bootfile);
1072 code = malloc(*size);
1073 if (code == NULL)
1074 err(EXIT_FAILURE, NULL);
1075 if (read(fd, code, *size) != *size)
1076 err(EXIT_FAILURE, "%s", bootfile);
1077 close(fd);
1078
1079 return (code);
1080}
1081
1082static void
1083gpart_write_partcode(struct ggeom *gp, int idx, void *code, ssize_t size)
1084{
1085 char dsf[128];
1086 struct gprovider *pp;
1087 const char *s;
1088 char *buf;
1089 off_t bsize;
1090 int fd;
1091
1092 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1093 s = find_provcfg(pp, "index");
1094 if (s == NULL)
1095 continue;
1096 if (atoi(s) == idx)
1097 break;
1098 }
1099
1100 if (pp != NULL) {
1101 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1102 fd = open(dsf, O_WRONLY);
1103 if (fd == -1)
1104 err(EXIT_FAILURE, "%s", dsf);
1105 if (lseek(fd, size, SEEK_SET) != size)
1106 errx(EXIT_FAILURE, "%s: not enough space", dsf);
1107 if (lseek(fd, 0, SEEK_SET) != 0)
1108 err(EXIT_FAILURE, "%s", dsf);
1109
1110 /*
1111 * When writing to a disk device, the write must be
1112 * sector aligned and not write to any partial sectors,
1113 * so round up the buffer size to the next sector and zero it.
1114 */
1115 bsize = (size + pp->lg_sectorsize - 1) /
1116 pp->lg_sectorsize * pp->lg_sectorsize;
1117 buf = calloc(1, bsize);
1118 if (buf == NULL)
1119 err(EXIT_FAILURE, "%s", dsf);
1120 bcopy(code, buf, size);
1121 if (write(fd, buf, bsize) != bsize)
1122 err(EXIT_FAILURE, "%s", dsf);
1123 free(buf);
1124 close(fd);
1125 } else
1126 errx(EXIT_FAILURE, "invalid partition index");
1127}
1128
1129static void
1130gpart_write_partcode_vtoc8(struct ggeom *gp, int idx, void *code)
1131{
1132 char dsf[128];
1133 struct gprovider *pp;
1134 const char *s;
1135 int installed, fd;
1136
1137 installed = 0;
1138 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1139 s = find_provcfg(pp, "index");
1140 if (s == NULL)
1141 continue;
1142 if (idx != 0 && atoi(s) != idx)
1143 continue;
1144 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1145 if (pp->lg_sectorsize != sizeof(struct vtoc8))
1146 errx(EXIT_FAILURE, "%s: unexpected sector "
1147 "size (%d)\n", dsf, pp->lg_sectorsize);
1148 fd = open(dsf, O_WRONLY);
1149 if (fd == -1)
1150 err(EXIT_FAILURE, "%s", dsf);
1151 if (lseek(fd, VTOC_BOOTSIZE, SEEK_SET) != VTOC_BOOTSIZE)
1152 continue;
1153 /*
1154 * We ignore the first VTOC_BOOTSIZE bytes of boot code in
1155 * order to avoid overwriting the label.
1156 */
1157 if (lseek(fd, sizeof(struct vtoc8), SEEK_SET) !=
1158 sizeof(struct vtoc8))
1159 err(EXIT_FAILURE, "%s", dsf);
1160 if (write(fd, (caddr_t)code + sizeof(struct vtoc8),
1161 VTOC_BOOTSIZE - sizeof(struct vtoc8)) != VTOC_BOOTSIZE -
1162 sizeof(struct vtoc8))
1163 err(EXIT_FAILURE, "%s", dsf);
1164 installed++;
1165 close(fd);
1166 if (idx != 0 && atoi(s) == idx)
1167 break;
1168 }
1169 if (installed == 0)
1170 errx(EXIT_FAILURE, "%s: no partitions", gp->lg_name);
1171}
1172
1173static void
1174gpart_bootcode(struct gctl_req *req, unsigned int fl)
1175{
1176 struct gmesh mesh;
1177 struct gclass *classp;
1178 struct ggeom *gp;
1179 const char *s;
1180 void *bootcode, *partcode;
1181 size_t bootsize, partsize;
1182 int error, idx, vtoc8;
1183
1184 if (gctl_has_param(req, GPART_PARAM_BOOTCODE)) {
1185 s = gctl_get_ascii(req, GPART_PARAM_BOOTCODE);
1186 bootsize = 800 * 1024; /* Arbitrary limit. */
1187 bootcode = gpart_bootfile_read(s, &bootsize);
1188 error = gctl_change_param(req, GPART_PARAM_BOOTCODE, bootsize,
1189 bootcode);
1190 if (error)
1191 errc(EXIT_FAILURE, error, "internal error");
1192 } else {
1193 bootcode = NULL;
1194 bootsize = 0;
1195 }
1196
1197 s = gctl_get_ascii(req, "class");
1198 if (s == NULL)
1199 abort();
1200 error = geom_gettree(&mesh);
1201 if (error != 0)
1202 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
1203 classp = find_class(&mesh, s);
1204 if (classp == NULL) {
1205 geom_deletetree(&mesh);
1206 errx(EXIT_FAILURE, "Class %s not found.", s);
1207 }
1208 if (gctl_get_int(req, "nargs") != 1)
1209 errx(EXIT_FAILURE, "Invalid number of arguments.");
1210 s = gctl_get_ascii(req, "arg0");
1211 if (s == NULL)
1212 abort();
1213 gp = find_geom(classp, s);
1214 if (gp == NULL)
1215 errx(EXIT_FAILURE, "No such geom: %s.", s);
1216 s = find_geomcfg(gp, "scheme");
1217 if (s == NULL)
1218 errx(EXIT_FAILURE, "Scheme not found for geom %s", gp->lg_name);
1219 vtoc8 = 0;
1220 if (strcmp(s, "VTOC8") == 0)
1221 vtoc8 = 1;
1222
1223 if (gctl_has_param(req, GPART_PARAM_PARTCODE)) {
1224 s = gctl_get_ascii(req, GPART_PARAM_PARTCODE);
1225 partsize = vtoc8 != 0 ? VTOC_BOOTSIZE : bootsize * 1024;
1226 partcode = gpart_bootfile_read(s, &partsize);
1227 error = gctl_delete_param(req, GPART_PARAM_PARTCODE);
1228 if (error)
1229 errc(EXIT_FAILURE, error, "internal error");
1230 } else {
1231 partcode = NULL;
1232 partsize = 0;
1233 }
1234
1235 if (gctl_has_param(req, GPART_PARAM_INDEX)) {
1236 if (partcode == NULL)
1237 errx(EXIT_FAILURE, "-i is only valid with -p");
1238 idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
1239 if (idx < 1)
1240 errx(EXIT_FAILURE, "invalid partition index");
1241 error = gctl_delete_param(req, GPART_PARAM_INDEX);
1242 if (error)
1243 errc(EXIT_FAILURE, error, "internal error");
1244 } else
1245 idx = 0;
1246
1247 if (partcode != NULL) {
1248 if (vtoc8 == 0) {
1249 if (idx == 0)
1250 errx(EXIT_FAILURE, "missing -i option");
1251 gpart_write_partcode(gp, idx, partcode, partsize);
1252 } else {
1253 if (partsize != VTOC_BOOTSIZE)
1254 errx(EXIT_FAILURE, "invalid bootcode");
1255 gpart_write_partcode_vtoc8(gp, idx, partcode);
1256 }
1257 } else
1258 if (bootcode == NULL)
1259 errx(EXIT_FAILURE, "no -b nor -p");
1260
1261 if (bootcode != NULL)
1262 gpart_issue(req, fl);
1263
1264 geom_deletetree(&mesh);
1265}
1266
1267static void
1268gpart_print_error(const char *errstr)
1269{
1270 char *errmsg;
1271 int error;
1272
1273 error = strtol(errstr, &errmsg, 0);
1274 if (errmsg != errstr) {
1275 while (errmsg[0] == ' ')
1276 errmsg++;
1277 if (errmsg[0] != '\0')
1278 warnc(error, "%s", errmsg);
1279 else
1280 warnc(error, NULL);
1281 } else
1282 warnx("%s", errmsg);
1283}
1284
1285static void
1286gpart_issue(struct gctl_req *req, unsigned int fl __unused)
1287{
1288 char buf[4096];
1289 const char *errstr;
1290 int error, status;
1291
1292 if (gctl_get_int(req, "nargs") != 1)
1293 errx(EXIT_FAILURE, "Invalid number of arguments.");
1294 (void)gctl_delete_param(req, "nargs");
1295
1296 /* autofill parameters (if applicable). */
1297 error = gpart_autofill(req);
1298 if (error) {
1299 warnc(error, "autofill");
1300 status = EXIT_FAILURE;
1301 goto done;
1302 }
1303
1304 bzero(buf, sizeof(buf));
1305 gctl_rw_param(req, "output", sizeof(buf), buf);
1306 errstr = gctl_issue(req);
1307 if (errstr == NULL || errstr[0] == '\0') {
1308 if (buf[0] != '\0')
1309 printf("%s", buf);
1310 status = EXIT_SUCCESS;
1311 goto done;
1312 }
1313
1314 gpart_print_error(errstr);
1315 status = EXIT_FAILURE;
1316
1317 done:
1318 gctl_free(req);
1319 exit(status);
1320}