2 * Copyright (c) 2007, 2008 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
51 #include "core/geom.h"
52 #include "misc/subr.h"
54 #ifdef STATIC_GEOM_CLASSES
55 #define PUBSYM(x) gpart_##x
60 uint32_t PUBSYM(lib_version) = G_LIB_VERSION;
61 uint32_t PUBSYM(version) = 0;
63 static char sstart[32];
64 static char ssize[32];
65 volatile sig_atomic_t undo_restore;
67 #define GPART_AUTOFILL "*"
68 #define GPART_FLAGS "C"
70 #define GPART_PARAM_BOOTCODE "bootcode"
71 #define GPART_PARAM_INDEX "index"
72 #define GPART_PARAM_PARTCODE "partcode"
74 static struct gclass *find_class(struct gmesh *, const char *);
75 static struct ggeom * find_geom(struct gclass *, const char *);
76 static const char *find_geomcfg(struct ggeom *, const char *);
77 static const char *find_provcfg(struct gprovider *, const char *);
78 static struct gprovider *find_provider(struct ggeom *, off_t);
79 static const char *fmtsize(int64_t);
80 static int gpart_autofill(struct gctl_req *);
81 static int gpart_autofill_resize(struct gctl_req *);
82 static void gpart_bootcode(struct gctl_req *, unsigned int);
83 static void *gpart_bootfile_read(const char *, ssize_t *);
84 static void gpart_issue(struct gctl_req *, unsigned int);
85 static void gpart_show(struct gctl_req *, unsigned int);
86 static void gpart_show_geom(struct ggeom *, const char *, int);
87 static int gpart_show_hasopt(struct gctl_req *, const char *, const char *);
88 static void gpart_write_partcode(struct ggeom *, int, void *, ssize_t);
89 static void gpart_write_partcode_vtoc8(struct ggeom *, int, void *);
90 static void gpart_print_error(const char *);
91 static void gpart_backup(struct gctl_req *, unsigned int);
92 static void gpart_restore(struct gctl_req *, unsigned int);
94 struct 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 },
104 "-t type [-a alignment] [-b start] [-s size] [-i index] "
105 "[-l label] [-f flags] geom"
107 { "backup", 0, gpart_backup, G_NULL_OPTS,
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 },
116 "[-b bootcode] [-p partcode -i index] [-f flags] geom"
118 { "commit", 0, gpart_issue, G_NULL_OPTS,
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 },
126 "-s scheme [-n entries] [-f flags] provider"
128 { "delete", 0, gpart_issue, {
129 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
130 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
132 "-i index [-f flags] geom"
134 { "destroy", 0, gpart_issue, {
135 { 'F', "force", NULL, G_TYPE_BOOL },
136 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
138 "[-F] [-f flags] geom"
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 },
146 "-i index [-l label] [-t type] [-f flags] geom"
148 { "set", 0, gpart_issue, {
149 { 'a', "attrib", NULL, G_TYPE_STRING },
150 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
151 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
153 "-a attrib -i index [-f flags] geom"
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 },
160 "[-l | -r] [-p] [geom ...]"
162 { "undo", 0, gpart_issue, G_NULL_OPTS,
165 { "unset", 0, gpart_issue, {
166 { 'a', "attrib", NULL, G_TYPE_STRING },
167 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
168 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
170 "-a attrib -i index [-f flags] geom"
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 },
178 "-i index [-a alignment] [-s size] [-f flags] geom"
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 },
185 "[-lF] [-f flags] provider [...]"
187 { "recover", 0, gpart_issue, {
188 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
195 static struct gclass *
196 find_class(struct gmesh *mesh, const char *name)
198 struct gclass *classp;
200 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
201 if (strcmp(classp->lg_name, name) == 0)
207 static struct ggeom *
208 find_geom(struct gclass *classp, const char *name)
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)
222 find_geomcfg(struct ggeom *gp, const char *cfg)
226 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
227 if (!strcmp(gc->lg_name, cfg))
234 find_provcfg(struct gprovider *pp, const char *cfg)
238 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
239 if (!strcmp(gc->lg_name, cfg))
245 static struct gprovider *
246 find_provider(struct ggeom *gp, off_t minsector)
248 struct gprovider *pp, *bestpp;
250 off_t sector, bestsector;
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)
259 if (bestpp != NULL && sector >= bestsector)
269 fmtsize(int64_t rawsz)
273 humanize_number(buf, sizeof(buf), rawsz, "", HN_AUTOSCALE,
274 HN_B | HN_NOSPACE | HN_DECIMAL);
279 fmtattrib(struct gprovider *pp)
281 static char buf[128];
287 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
288 if (strcmp(gc->lg_name, "attrib") != 0)
290 idx += snprintf(buf + idx, sizeof(buf) - idx, "%s%s",
291 (idx == 0) ? " [" : ",", gc->lg_val);
294 snprintf(buf + idx, sizeof(buf) - idx, "] ");
298 #define ALIGNDOWN(d, a) ((d) - (d) % (a))
299 #define ALIGNUP(d, a) ((d) % (a) ? (d) - (d) % (a) + (a): (d))
302 gpart_autofill_resize(struct gctl_req *req)
307 struct gprovider *pp;
308 off_t last, size, start, new_size;
309 off_t lba, new_lba, alignment, offset;
311 int error, idx, has_alignment;
313 idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
315 errx(EXIT_FAILURE, "invalid partition index");
317 error = geom_gettree(&mesh);
320 s = gctl_get_ascii(req, "class");
323 cp = find_class(&mesh, s);
325 errx(EXIT_FAILURE, "Class %s not found.", s);
326 s = gctl_get_ascii(req, "arg0");
329 gp = find_geom(cp, s);
331 errx(EXIT_FAILURE, "No such geom: %s.", s);
332 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
334 errx(EXIT_FAILURE, "Provider for geom %s not found.", s);
336 s = gctl_get_ascii(req, "alignment");
337 has_alignment = (*s == '*') ? 0 : 1;
340 error = g_parse_lba(s, pp->lg_sectorsize, &alignment);
342 errc(EXIT_FAILURE, error, "Invalid alignment param");
344 errx(EXIT_FAILURE, "Invalid alignment param");
346 lba = pp->lg_stripesize / pp->lg_sectorsize;
350 error = gctl_delete_param(req, "alignment");
352 errc(EXIT_FAILURE, error, "internal error");
354 s = gctl_get_ascii(req, "size");
358 error = g_parse_lba(s, pp->lg_sectorsize, &new_size);
360 errc(EXIT_FAILURE, error, "Invalid size param");
361 /* no autofill necessary. */
362 if (has_alignment == 0)
366 offset = (pp->lg_stripeoffset / pp->lg_sectorsize) % alignment;
367 last = (off_t)strtoimax(find_geomcfg(gp, "last"), NULL, 0);
368 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
369 s = find_provcfg(pp, "index");
376 errx(EXIT_FAILURE, "invalid partition index");
378 s = find_provcfg(pp, "start");
379 start = (off_t)strtoimax(s, NULL, 0);
380 s = find_provcfg(pp, "end");
381 lba = (off_t)strtoimax(s, NULL, 0);
382 size = lba - start + 1;
384 pp = find_provider(gp, lba + 1);
385 if (new_size > 0 && (new_size <= size || pp == NULL)) {
386 /* The start offset may be not aligned, so we align the end
387 * offset and then calculate the size.
389 new_size = ALIGNDOWN(start + offset + new_size,
390 alignment) - start - offset;
394 new_size = ALIGNDOWN(last + offset + 1, alignment) -
399 s = find_provcfg(pp, "start");
400 new_lba = (off_t)strtoimax(s, NULL, 0);
402 * Is there any free space between current and
405 new_lba = ALIGNDOWN(new_lba + offset, alignment) - offset;
407 new_size = new_lba - start;
409 geom_deletetree(&mesh);
414 snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)new_size);
415 gctl_change_param(req, "size", -1, ssize);
416 geom_deletetree(&mesh);
421 gpart_autofill(struct gctl_req *req)
426 struct gprovider *pp;
427 off_t first, last, a_first;
428 off_t size, start, a_lba;
429 off_t lba, len, alignment, offset;
432 int error, has_size, has_start, has_alignment;
434 s = gctl_get_ascii(req, "verb");
435 if (strcmp(s, "resize") == 0)
436 return gpart_autofill_resize(req);
437 if (strcmp(s, "add") != 0)
440 error = geom_gettree(&mesh);
443 s = gctl_get_ascii(req, "class");
446 cp = find_class(&mesh, s);
448 errx(EXIT_FAILURE, "Class %s not found.", s);
449 s = gctl_get_ascii(req, "arg0");
452 gp = find_geom(cp, s);
454 errx(EXIT_FAILURE, "No such geom: %s.", s);
455 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
457 errx(EXIT_FAILURE, "Provider for geom %s not found.", s);
459 s = gctl_get_ascii(req, "alignment");
460 has_alignment = (*s == '*') ? 0 : 1;
463 error = g_parse_lba(s, pp->lg_sectorsize, &alignment);
465 errc(EXIT_FAILURE, error, "Invalid alignment param");
467 errx(EXIT_FAILURE, "Invalid alignment param");
469 error = gctl_delete_param(req, "alignment");
471 errc(EXIT_FAILURE, error, "internal error");
473 s = gctl_get_ascii(req, "size");
474 has_size = (*s == '*') ? 0 : 1;
477 error = g_parse_lba(s, pp->lg_sectorsize, &size);
479 errc(EXIT_FAILURE, error, "Invalid size param");
482 s = gctl_get_ascii(req, "start");
483 has_start = (*s == '*') ? 0 : 1;
486 error = g_parse_lba(s, pp->lg_sectorsize, &start);
488 errc(EXIT_FAILURE, error, "Invalid start param");
491 /* No autofill necessary. */
492 if (has_size && has_start && !has_alignment)
495 len = pp->lg_stripesize / pp->lg_sectorsize;
496 if (len > 0 && !has_alignment)
499 /* Adjust parameters to stripeoffset */
500 offset = (pp->lg_stripeoffset / pp->lg_sectorsize) % alignment;
501 start = ALIGNUP(start + offset, alignment);
502 if (size > alignment)
503 size = ALIGNDOWN(size, alignment);
505 first = (off_t)strtoimax(find_geomcfg(gp, "first"), NULL, 0);
506 last = (off_t)strtoimax(find_geomcfg(gp, "last"), NULL, 0);
508 a_first = ALIGNUP(first + offset, alignment);
509 last = ALIGNDOWN(last + offset, alignment);
510 while ((pp = find_provider(gp, first)) != NULL) {
511 s = find_provcfg(pp, "start");
512 lba = (off_t)strtoimax(s, NULL, 0);
513 a_lba = ALIGNDOWN(lba + offset, alignment);
514 if (first < a_lba && a_first < a_lba) {
515 /* Free space [first, lba> */
516 len = a_lba - a_first;
519 (uintmax_t)(len - size) < grade) {
523 } else if (has_start) {
524 if (start >= a_first && start < a_lba) {
525 size = a_lba - start;
526 grade = start - a_first;
529 if (grade == ~0ULL || len > size) {
537 s = find_provcfg(pp, "end");
538 first = (off_t)strtoimax(s, NULL, 0) + 1;
539 a_first = ALIGNUP(first + offset, alignment);
541 if (a_first <= last) {
542 /* Free space [first-last] */
543 len = ALIGNDOWN(last - a_first + 1, alignment);
546 (uintmax_t)(len - size) < grade) {
550 } else if (has_start) {
551 if (start >= a_first && start <= last) {
552 size = ALIGNDOWN(last - start + 1, alignment);
553 grade = start - a_first;
556 if (grade == ~0ULL || len > size) {
563 if (grade == ~0ULL) {
564 geom_deletetree(&mesh);
567 start -= offset; /* Return back to real offset */
569 snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)size);
570 gctl_change_param(req, "size", -1, ssize);
571 snprintf(sstart, sizeof(sstart), "%jd", (intmax_t)start);
572 gctl_change_param(req, "start", -1, sstart);
573 geom_deletetree(&mesh);
578 gpart_show_geom(struct ggeom *gp, const char *element, int show_providers)
580 struct gprovider *pp;
581 const char *s, *scheme;
582 off_t first, last, sector, end;
584 int idx, wblocks, wname, wmax;
586 scheme = find_geomcfg(gp, "scheme");
587 s = find_geomcfg(gp, "first");
588 first = (off_t)strtoimax(s, NULL, 0);
589 s = find_geomcfg(gp, "last");
590 last = (off_t)strtoimax(s, NULL, 0);
592 s = find_geomcfg(gp, "state");
593 if (s != NULL && *s != 'C')
595 wmax = strlen(gp->lg_name);
596 if (show_providers) {
597 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
598 wname = strlen(pp->lg_name);
604 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
605 secsz = pp->lg_sectorsize;
606 printf("=>%*jd %*jd %*s %s (%s)%s\n",
607 wblocks, (intmax_t)first, wblocks, (intmax_t)(last - first + 1),
609 scheme, fmtsize(pp->lg_mediasize),
610 s ? " [CORRUPT]": "");
612 while ((pp = find_provider(gp, first)) != NULL) {
613 s = find_provcfg(pp, "start");
614 sector = (off_t)strtoimax(s, NULL, 0);
616 s = find_provcfg(pp, "end");
617 end = (off_t)strtoimax(s, NULL, 0);
618 length = end - sector + 1;
620 s = find_provcfg(pp, "index");
622 if (first < sector) {
623 printf(" %*jd %*jd %*s - free - (%s)\n",
624 wblocks, (intmax_t)first, wblocks,
625 (intmax_t)(sector - first), wname, "",
626 fmtsize((sector - first) * secsz));
628 if (show_providers) {
629 printf(" %*jd %*jd %*s %s %s (%s)\n",
630 wblocks, (intmax_t)sector, wblocks,
631 (intmax_t)length, wname, pp->lg_name,
632 find_provcfg(pp, element), fmtattrib(pp),
633 fmtsize(pp->lg_mediasize));
635 printf(" %*jd %*jd %*d %s %s (%s)\n",
636 wblocks, (intmax_t)sector, wblocks,
637 (intmax_t)length, wname, idx,
638 find_provcfg(pp, element), fmtattrib(pp),
639 fmtsize(pp->lg_mediasize));
643 length = last - first + 1;
644 printf(" %*jd %*jd %*s - free - (%s)\n",
645 wblocks, (intmax_t)first, wblocks, (intmax_t)length,
647 fmtsize(length * secsz));
653 gpart_show_hasopt(struct gctl_req *req, const char *opt, const char *elt)
656 if (!gctl_get_int(req, "%s", opt))
660 errx(EXIT_FAILURE, "-l and -r are mutually exclusive");
666 gpart_show(struct gctl_req *req, unsigned int fl __unused)
669 struct gclass *classp;
671 const char *element, *name;
672 int error, i, nargs, show_providers;
675 if (gpart_show_hasopt(req, "show_label", element))
677 if (gpart_show_hasopt(req, "show_rawtype", element))
682 name = gctl_get_ascii(req, "class");
685 error = geom_gettree(&mesh);
687 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
688 classp = find_class(&mesh, name);
689 if (classp == NULL) {
690 geom_deletetree(&mesh);
691 errx(EXIT_FAILURE, "Class %s not found.", name);
693 show_providers = gctl_get_int(req, "show_providers");
694 nargs = gctl_get_int(req, "nargs");
696 for (i = 0; i < nargs; i++) {
697 name = gctl_get_ascii(req, "arg%d", i);
698 gp = find_geom(classp, name);
700 gpart_show_geom(gp, element, show_providers);
702 errx(EXIT_FAILURE, "No such geom: %s.", name);
705 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
706 gpart_show_geom(gp, element, show_providers);
709 geom_deletetree(&mesh);
713 gpart_backup(struct gctl_req *req, unsigned int fl __unused)
716 struct gclass *classp;
717 struct gprovider *pp;
719 const char *s, *scheme;
722 int error, i, windex, wblocks, wtype;
724 if (gctl_get_int(req, "nargs") != 1)
725 errx(EXIT_FAILURE, "Invalid number of arguments.");
726 error = geom_gettree(&mesh);
728 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
729 s = gctl_get_ascii(req, "class");
732 classp = find_class(&mesh, s);
733 if (classp == NULL) {
734 geom_deletetree(&mesh);
735 errx(EXIT_FAILURE, "Class %s not found.", s);
737 s = gctl_get_ascii(req, "arg0");
740 gp = find_geom(classp, s);
742 errx(EXIT_FAILURE, "No such geom: %s.", s);
743 scheme = find_geomcfg(gp, "scheme");
746 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
747 secsz = pp->lg_sectorsize;
748 s = find_geomcfg(gp, "last");
751 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
752 s = find_provcfg(pp, "type");
757 s = find_geomcfg(gp, "entries");
759 printf("%s %s\n", scheme, s);
760 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
761 s = find_provcfg(pp, "start");
762 sector = (off_t)strtoimax(s, NULL, 0);
764 s = find_provcfg(pp, "end");
765 end = (off_t)strtoimax(s, NULL, 0);
766 length = end - sector + 1;
768 s = find_provcfg(pp, "label");
769 printf("%-*s %*s %*jd %*jd %s %s\n",
770 windex, find_provcfg(pp, "index"),
771 wtype, find_provcfg(pp, "type"),
772 wblocks, (intmax_t)sector,
773 wblocks, (intmax_t)length,
774 (s != NULL) ? s: "", fmtattrib(pp));
776 geom_deletetree(&mesh);
780 skip_line(const char *p)
786 if (isspace(*p) == 0)
794 gpart_sighndl(int sig __unused)
800 gpart_restore(struct gctl_req *req, unsigned int fl __unused)
803 struct gclass *classp;
806 struct sigaction si_sa;
807 const char *s, *flags, *errstr, *label;
808 char **ap, *argv[6], line[BUFSIZ], *pline;
809 int error, forced, i, l, nargs, created, rl;
812 nargs = gctl_get_int(req, "nargs");
814 errx(EXIT_FAILURE, "Invalid number of arguments.");
816 forced = gctl_get_int(req, "force");
817 flags = gctl_get_ascii(req, "flags");
818 rl = gctl_get_int(req, "restore_labels");
819 s = gctl_get_ascii(req, "class");
822 error = geom_gettree(&mesh);
824 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
825 classp = find_class(&mesh, s);
826 if (classp == NULL) {
827 geom_deletetree(&mesh);
828 errx(EXIT_FAILURE, "Class %s not found.", s);
831 sigemptyset(&si_sa.sa_mask);
833 si_sa.sa_handler = gpart_sighndl;
834 if (sigaction(SIGINT, &si_sa, 0) == -1)
835 err(EXIT_FAILURE, "sigaction SIGINT");
838 /* destroy existent partition table before restore */
839 for (i = 0; i < nargs; i++) {
840 s = gctl_get_ascii(req, "arg%d", i);
841 gp = find_geom(classp, s);
843 r = gctl_get_handle();
844 gctl_ro_param(r, "class", -1,
846 gctl_ro_param(r, "verb", -1, "destroy");
847 gctl_ro_param(r, "flags", -1, "restore");
848 gctl_ro_param(r, "force", sizeof(forced),
850 gctl_ro_param(r, "arg0", -1, s);
851 errstr = gctl_issue(r);
852 if (errstr != NULL && errstr[0] != '\0') {
853 gpart_print_error(errstr);
862 while (undo_restore == 0 &&
863 fgets(line, sizeof(line) - 1, stdin) != NULL) {
864 /* Format of backup entries:
865 * <scheme name> <number of entries>
866 * <index> <type> <start> <size> [label] ['['attrib[,attrib]']']
868 pline = (char *)line;
869 pline[strlen(line) - 1] = 0;
870 if (skip_line(pline))
873 (*ap = strsep(&pline, " \t")) != NULL;)
874 if (**ap != '\0' && ++ap >= &argv[6])
877 label = pline = NULL;
878 if (l == 1 || l == 2) { /* create table */
880 errx(EXIT_FAILURE, "Incorrect backup format.");
882 n = strtoimax(argv[1], NULL, 0);
883 for (i = 0; i < nargs; i++) {
884 s = gctl_get_ascii(req, "arg%d", i);
885 r = gctl_get_handle();
886 gctl_ro_param(r, "class", -1,
888 gctl_ro_param(r, "verb", -1, "create");
889 gctl_ro_param(r, "scheme", -1, argv[0]);
891 gctl_ro_param(r, "entries",
893 gctl_ro_param(r, "flags", -1, "restore");
894 gctl_ro_param(r, "arg0", -1, s);
895 errstr = gctl_issue(r);
896 if (errstr != NULL && errstr[0] != '\0') {
897 gpart_print_error(errstr);
905 } else if (l < 4 || created == 0)
906 errx(EXIT_FAILURE, "Incorrect backup format.");
908 if (strchr(argv[4], '[') == NULL)
916 /* Add partitions to each table */
917 for (i = 0; i < nargs; i++) {
918 s = gctl_get_ascii(req, "arg%d", i);
919 r = gctl_get_handle();
920 n = strtoimax(argv[0], NULL, 0);
921 gctl_ro_param(r, "class", -1, classp->lg_name);
922 gctl_ro_param(r, "verb", -1, "add");
923 gctl_ro_param(r, "flags", -1, "restore");
924 gctl_ro_param(r, GPART_PARAM_INDEX, sizeof(n), &n);
925 gctl_ro_param(r, "type", -1, argv[1]);
926 gctl_ro_param(r, "start", -1, argv[2]);
927 gctl_ro_param(r, "size", -1, argv[3]);
928 if (rl != 0 && label != NULL)
929 gctl_ro_param(r, "label", -1, argv[4]);
930 gctl_ro_param(r, "alignment", -1, GPART_AUTOFILL);
931 gctl_ro_param(r, "arg0", -1, s);
932 error = gpart_autofill(r);
934 errc(EXIT_FAILURE, error, "autofill");
935 errstr = gctl_issue(r);
936 if (errstr != NULL && errstr[0] != '\0') {
937 gpart_print_error(errstr);
943 if (pline == NULL || *pline != '[')
948 (*ap = strsep(&pline, ",]")) != NULL;)
949 if (**ap != '\0' && ++ap >= &argv[6])
951 for (i = 0; i < nargs; i++) {
953 s = gctl_get_ascii(req, "arg%d", i);
955 r = gctl_get_handle();
956 gctl_ro_param(r, "class", -1, classp->lg_name);
957 gctl_ro_param(r, "verb", -1, "set");
958 gctl_ro_param(r, "flags", -1, "restore");
959 gctl_ro_param(r, GPART_PARAM_INDEX,
961 gctl_ro_param(r, "attrib", -1, argv[--l]);
962 gctl_ro_param(r, "arg0", -1, s);
963 errstr = gctl_issue(r);
964 if (errstr != NULL && errstr[0] != '\0') {
965 gpart_print_error(errstr);
975 /* commit changes if needed */
976 if (strchr(flags, 'C') != NULL) {
977 for (i = 0; i < nargs; i++) {
978 s = gctl_get_ascii(req, "arg%d", i);
979 r = gctl_get_handle();
980 gctl_ro_param(r, "class", -1, classp->lg_name);
981 gctl_ro_param(r, "verb", -1, "commit");
982 gctl_ro_param(r, "arg0", -1, s);
983 errstr = gctl_issue(r);
984 if (errstr != NULL && errstr[0] != '\0') {
985 gpart_print_error(errstr);
993 geom_deletetree(&mesh);
997 for (i = 0; i < nargs; i++) {
998 s = gctl_get_ascii(req, "arg%d", i);
999 r = gctl_get_handle();
1000 gctl_ro_param(r, "class", -1, classp->lg_name);
1001 gctl_ro_param(r, "verb", -1, "undo");
1002 gctl_ro_param(r, "arg0", -1, s);
1007 geom_deletetree(&mesh);
1012 gpart_bootfile_read(const char *bootfile, ssize_t *size)
1018 if (stat(bootfile, &sb) == -1)
1019 err(EXIT_FAILURE, "%s", bootfile);
1020 if (!S_ISREG(sb.st_mode))
1021 errx(EXIT_FAILURE, "%s: not a regular file", bootfile);
1022 if (sb.st_size == 0)
1023 errx(EXIT_FAILURE, "%s: empty file", bootfile);
1024 if (*size > 0 && sb.st_size > *size)
1025 errx(EXIT_FAILURE, "%s: file too big (%zu limit)", bootfile,
1030 fd = open(bootfile, O_RDONLY);
1032 err(EXIT_FAILURE, "%s", bootfile);
1033 code = malloc(*size);
1035 err(EXIT_FAILURE, NULL);
1036 if (read(fd, code, *size) != *size)
1037 err(EXIT_FAILURE, "%s", bootfile);
1044 gpart_write_partcode(struct ggeom *gp, int idx, void *code, ssize_t size)
1047 struct gprovider *pp;
1053 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1054 s = find_provcfg(pp, "index");
1062 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1063 fd = open(dsf, O_WRONLY);
1065 err(EXIT_FAILURE, "%s", dsf);
1066 if (lseek(fd, size, SEEK_SET) != size)
1067 errx(EXIT_FAILURE, "%s: not enough space", dsf);
1068 if (lseek(fd, 0, SEEK_SET) != 0)
1069 err(EXIT_FAILURE, "%s", dsf);
1072 * When writing to a disk device, the write must be
1073 * sector aligned and not write to any partial sectors,
1074 * so round up the buffer size to the next sector and zero it.
1076 bsize = (size + pp->lg_sectorsize - 1) /
1077 pp->lg_sectorsize * pp->lg_sectorsize;
1078 buf = calloc(1, bsize);
1080 err(EXIT_FAILURE, "%s", dsf);
1081 bcopy(code, buf, size);
1082 if (write(fd, buf, bsize) != bsize)
1083 err(EXIT_FAILURE, "%s", dsf);
1087 errx(EXIT_FAILURE, "invalid partition index");
1091 gpart_write_partcode_vtoc8(struct ggeom *gp, int idx, void *code)
1094 struct gprovider *pp;
1099 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1100 s = find_provcfg(pp, "index");
1103 if (idx != 0 && atoi(s) != idx)
1105 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1106 if (pp->lg_sectorsize != sizeof(struct vtoc8))
1107 errx(EXIT_FAILURE, "%s: unexpected sector "
1108 "size (%d)\n", dsf, pp->lg_sectorsize);
1109 fd = open(dsf, O_WRONLY);
1111 err(EXIT_FAILURE, "%s", dsf);
1112 if (lseek(fd, VTOC_BOOTSIZE, SEEK_SET) != VTOC_BOOTSIZE)
1115 * We ignore the first VTOC_BOOTSIZE bytes of boot code in
1116 * order to avoid overwriting the label.
1118 if (lseek(fd, sizeof(struct vtoc8), SEEK_SET) !=
1119 sizeof(struct vtoc8))
1120 err(EXIT_FAILURE, "%s", dsf);
1121 if (write(fd, (caddr_t)code + sizeof(struct vtoc8),
1122 VTOC_BOOTSIZE - sizeof(struct vtoc8)) != VTOC_BOOTSIZE -
1123 sizeof(struct vtoc8))
1124 err(EXIT_FAILURE, "%s", dsf);
1127 if (idx != 0 && atoi(s) == idx)
1131 errx(EXIT_FAILURE, "%s: no partitions", gp->lg_name);
1135 gpart_bootcode(struct gctl_req *req, unsigned int fl)
1138 struct gclass *classp;
1141 void *bootcode, *partcode;
1142 size_t bootsize, partsize;
1143 int error, idx, vtoc8;
1145 if (gctl_has_param(req, GPART_PARAM_BOOTCODE)) {
1146 s = gctl_get_ascii(req, GPART_PARAM_BOOTCODE);
1147 bootsize = 800 * 1024; /* Arbitrary limit. */
1148 bootcode = gpart_bootfile_read(s, &bootsize);
1149 error = gctl_change_param(req, GPART_PARAM_BOOTCODE, bootsize,
1152 errc(EXIT_FAILURE, error, "internal error");
1158 s = gctl_get_ascii(req, "class");
1161 error = geom_gettree(&mesh);
1163 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
1164 classp = find_class(&mesh, s);
1165 if (classp == NULL) {
1166 geom_deletetree(&mesh);
1167 errx(EXIT_FAILURE, "Class %s not found.", s);
1169 if (gctl_get_int(req, "nargs") != 1)
1170 errx(EXIT_FAILURE, "Invalid number of arguments.");
1171 s = gctl_get_ascii(req, "arg0");
1174 gp = find_geom(classp, s);
1176 errx(EXIT_FAILURE, "No such geom: %s.", s);
1177 s = find_geomcfg(gp, "scheme");
1179 if (strcmp(s, "VTOC8") == 0)
1182 if (gctl_has_param(req, GPART_PARAM_PARTCODE)) {
1183 s = gctl_get_ascii(req, GPART_PARAM_PARTCODE);
1184 partsize = vtoc8 != 0 ? VTOC_BOOTSIZE : bootsize * 1024;
1185 partcode = gpart_bootfile_read(s, &partsize);
1186 error = gctl_delete_param(req, GPART_PARAM_PARTCODE);
1188 errc(EXIT_FAILURE, error, "internal error");
1194 if (gctl_has_param(req, GPART_PARAM_INDEX)) {
1195 if (partcode == NULL)
1196 errx(EXIT_FAILURE, "-i is only valid with -p");
1197 idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
1199 errx(EXIT_FAILURE, "invalid partition index");
1200 error = gctl_delete_param(req, GPART_PARAM_INDEX);
1202 errc(EXIT_FAILURE, error, "internal error");
1206 if (partcode != NULL) {
1209 errx(EXIT_FAILURE, "missing -i option");
1210 gpart_write_partcode(gp, idx, partcode, partsize);
1212 if (partsize != VTOC_BOOTSIZE)
1213 errx(EXIT_FAILURE, "invalid bootcode");
1214 gpart_write_partcode_vtoc8(gp, idx, partcode);
1217 if (bootcode == NULL)
1218 errx(EXIT_FAILURE, "no -b nor -p");
1220 if (bootcode != NULL)
1221 gpart_issue(req, fl);
1223 geom_deletetree(&mesh);
1227 gpart_print_error(const char *errstr)
1232 error = strtol(errstr, &errmsg, 0);
1233 if (errmsg != errstr) {
1234 while (errmsg[0] == ' ')
1236 if (errmsg[0] != '\0')
1237 warnc(error, "%s", errmsg);
1241 warnx("%s", errmsg);
1245 gpart_issue(struct gctl_req *req, unsigned int fl __unused)
1251 if (gctl_get_int(req, "nargs") != 1)
1252 errx(EXIT_FAILURE, "Invalid number of arguments.");
1253 (void)gctl_delete_param(req, "nargs");
1255 /* autofill parameters (if applicable). */
1256 error = gpart_autofill(req);
1258 warnc(error, "autofill");
1259 status = EXIT_FAILURE;
1263 bzero(buf, sizeof(buf));
1264 gctl_rw_param(req, "output", sizeof(buf), buf);
1265 errstr = gctl_issue(req);
1266 if (errstr == NULL || errstr[0] == '\0') {
1269 status = EXIT_SUCCESS;
1273 gpart_print_error(errstr);
1274 status = EXIT_FAILURE;