2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007, 2008 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
53 #include "core/geom.h"
54 #include "misc/subr.h"
56 #ifdef STATIC_GEOM_CLASSES
57 #define PUBSYM(x) gpart_##x
62 uint32_t PUBSYM(lib_version) = G_LIB_VERSION;
63 uint32_t PUBSYM(version) = 0;
65 static char sstart[32];
66 static char ssize[32];
67 volatile sig_atomic_t undo_restore;
69 #define GPART_AUTOFILL "*"
70 #define GPART_FLAGS "C"
72 #define GPART_PARAM_BOOTCODE "bootcode"
73 #define GPART_PARAM_INDEX "index"
74 #define GPART_PARAM_PARTCODE "partcode"
76 static struct gclass *find_class(struct gmesh *, const char *);
77 static struct ggeom * find_geom(struct gclass *, const char *);
78 static int geom_is_withered(struct ggeom *);
79 static const char *find_geomcfg(struct ggeom *, const char *);
80 static const char *find_provcfg(struct gprovider *, const char *);
81 static struct gprovider *find_provider(struct ggeom *, off_t);
82 static const char *fmtsize(int64_t);
83 static int gpart_autofill(struct gctl_req *);
84 static int gpart_autofill_resize(struct gctl_req *);
85 static void gpart_bootcode(struct gctl_req *, unsigned int);
86 static void *gpart_bootfile_read(const char *, ssize_t *);
87 static _Noreturn void gpart_issue(struct gctl_req *, unsigned int);
88 static void gpart_show(struct gctl_req *, unsigned int);
89 static void gpart_show_geom(struct ggeom *, const char *, int);
90 static int gpart_show_hasopt(struct gctl_req *, const char *, const char *);
91 static void gpart_write_partcode(struct ggeom *, int, void *, ssize_t);
92 static void gpart_write_partcode_vtoc8(struct ggeom *, int, void *);
93 static void gpart_print_error(const char *);
94 static void gpart_backup(struct gctl_req *, unsigned int);
95 static void gpart_restore(struct gctl_req *, unsigned int);
97 struct g_command PUBSYM(class_commands)[] = {
98 { "add", 0, gpart_issue, {
99 { 'a', "alignment", GPART_AUTOFILL, G_TYPE_STRING },
100 { 'b', "start", GPART_AUTOFILL, G_TYPE_STRING },
101 { 's', "size", GPART_AUTOFILL, G_TYPE_STRING },
102 { 't', "type", NULL, G_TYPE_STRING },
103 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
104 { 'l', "label", G_VAL_OPTIONAL, G_TYPE_STRING },
105 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
107 "-t type [-a alignment] [-b start] [-s size] [-i index] "
108 "[-l label] [-f flags] geom"
110 { "backup", 0, gpart_backup, G_NULL_OPTS,
113 { "bootcode", 0, gpart_bootcode, {
114 { 'b', GPART_PARAM_BOOTCODE, G_VAL_OPTIONAL, G_TYPE_STRING },
115 { 'p', GPART_PARAM_PARTCODE, G_VAL_OPTIONAL, G_TYPE_STRING },
116 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
117 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
119 "[-b bootcode] [-p partcode -i index] [-f flags] geom"
121 { "commit", 0, gpart_issue, G_NULL_OPTS,
124 { "create", 0, gpart_issue, {
125 { 's', "scheme", NULL, G_TYPE_STRING },
126 { 'n', "entries", G_VAL_OPTIONAL, G_TYPE_NUMBER },
127 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
129 "-s scheme [-n entries] [-f flags] provider"
131 { "delete", 0, gpart_issue, {
132 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
133 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
135 "-i index [-f flags] geom"
137 { "destroy", 0, gpart_issue, {
138 { 'F', "force", NULL, G_TYPE_BOOL },
139 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
141 "[-F] [-f flags] geom"
143 { "modify", 0, gpart_issue, {
144 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
145 { 'l', "label", G_VAL_OPTIONAL, G_TYPE_STRING },
146 { 't', "type", G_VAL_OPTIONAL, G_TYPE_STRING },
147 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
149 "-i index [-l label] [-t type] [-f flags] geom"
151 { "set", 0, gpart_issue, {
152 { 'a', "attrib", NULL, G_TYPE_STRING },
153 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
154 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
156 "-a attrib [-i index] [-f flags] geom"
158 { "show", 0, gpart_show, {
159 { 'l', "show_label", NULL, G_TYPE_BOOL },
160 { 'r', "show_rawtype", NULL, G_TYPE_BOOL },
161 { 'p', "show_providers", NULL, G_TYPE_BOOL },
163 "[-l | -r] [-p] [geom ...]"
165 { "undo", 0, gpart_issue, G_NULL_OPTS,
168 { "unset", 0, gpart_issue, {
169 { 'a', "attrib", NULL, G_TYPE_STRING },
170 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
171 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
173 "-a attrib [-i index] [-f flags] geom"
175 { "resize", 0, gpart_issue, {
176 { 'a', "alignment", GPART_AUTOFILL, G_TYPE_STRING },
177 { 's', "size", GPART_AUTOFILL, G_TYPE_STRING },
178 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
179 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
181 "-i index [-a alignment] [-s size] [-f flags] geom"
183 { "restore", 0, gpart_restore, {
184 { 'F', "force", NULL, G_TYPE_BOOL },
185 { 'l', "restore_labels", NULL, G_TYPE_BOOL },
186 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
188 "[-lF] [-f flags] provider [...]"
190 { "recover", 0, gpart_issue, {
191 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
198 static struct gclass *
199 find_class(struct gmesh *mesh, const char *name)
201 struct gclass *classp;
203 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
204 if (strcmp(classp->lg_name, name) == 0)
210 static struct ggeom *
211 find_geom(struct gclass *classp, const char *name)
213 struct ggeom *gp, *wgp;
215 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
216 name += sizeof(_PATH_DEV) - 1;
218 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
219 if (strcmp(gp->lg_name, name) != 0)
221 if (!geom_is_withered(gp))
230 geom_is_withered(struct ggeom *gp)
234 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
235 if (!strcmp(gc->lg_name, "wither"))
242 find_geomcfg(struct ggeom *gp, const char *cfg)
246 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
247 if (!strcmp(gc->lg_name, cfg))
254 find_provcfg(struct gprovider *pp, const char *cfg)
258 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
259 if (!strcmp(gc->lg_name, cfg))
265 static struct gprovider *
266 find_provider(struct ggeom *gp, off_t minsector)
268 struct gprovider *pp, *bestpp;
270 off_t sector, bestsector;
274 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
275 s = find_provcfg(pp, "start");
276 sector = (off_t)strtoimax(s, NULL, 0);
277 if (sector < minsector)
279 if (bestpp != NULL && sector >= bestsector)
289 fmtsize(int64_t rawsz)
293 humanize_number(buf, sizeof(buf), rawsz, "", HN_AUTOSCALE,
294 HN_B | HN_NOSPACE | HN_DECIMAL);
299 fmtattrib(struct gprovider *pp)
301 static char buf[128];
307 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
308 if (strcmp(gc->lg_name, "attrib") != 0)
310 idx += snprintf(buf + idx, sizeof(buf) - idx, "%s%s",
311 (idx == 0) ? " [" : ",", gc->lg_val);
314 snprintf(buf + idx, sizeof(buf) - idx, "] ");
318 #define ALIGNDOWN(d, a) ((d) - (d) % (a))
319 #define ALIGNUP(d, a) ((d) % (a) ? (d) - (d) % (a) + (a): (d))
322 gpart_autofill_resize(struct gctl_req *req)
327 struct gprovider *pp;
328 off_t last, size, start, new_size;
329 off_t lba, new_lba, alignment, offset;
331 int error, idx, has_alignment;
333 idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
335 errx(EXIT_FAILURE, "invalid partition index");
337 error = geom_gettree(&mesh);
340 s = gctl_get_ascii(req, "class");
343 cp = find_class(&mesh, s);
345 errx(EXIT_FAILURE, "Class %s not found.", s);
346 s = gctl_get_ascii(req, "arg0");
349 gp = find_geom(cp, s);
351 errx(EXIT_FAILURE, "No such geom: %s.", s);
352 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
354 errx(EXIT_FAILURE, "Provider for geom %s not found.", s);
356 s = gctl_get_ascii(req, "alignment");
357 has_alignment = (*s == '*') ? 0 : 1;
360 error = g_parse_lba(s, pp->lg_sectorsize, &alignment);
362 errc(EXIT_FAILURE, error, "Invalid alignment param");
364 errx(EXIT_FAILURE, "Invalid alignment param");
366 lba = pp->lg_stripesize / pp->lg_sectorsize;
370 error = gctl_delete_param(req, "alignment");
372 errc(EXIT_FAILURE, error, "internal error");
374 s = gctl_get_ascii(req, "size");
378 error = g_parse_lba(s, pp->lg_sectorsize, &new_size);
380 errc(EXIT_FAILURE, error, "Invalid size param");
381 /* no autofill necessary. */
382 if (has_alignment == 0)
386 offset = (pp->lg_stripeoffset / pp->lg_sectorsize) % alignment;
387 s = find_geomcfg(gp, "last");
389 errx(EXIT_FAILURE, "Final block not found for geom %s",
391 last = (off_t)strtoimax(s, NULL, 0);
392 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
393 s = find_provcfg(pp, "index");
400 errx(EXIT_FAILURE, "invalid partition index");
402 s = find_provcfg(pp, "start");
403 start = (off_t)strtoimax(s, NULL, 0);
404 s = find_provcfg(pp, "end");
405 lba = (off_t)strtoimax(s, NULL, 0);
406 size = lba - start + 1;
408 pp = find_provider(gp, lba + 1);
409 if (new_size > 0 && (new_size <= size || pp == NULL)) {
410 /* The start offset may be not aligned, so we align the end
411 * offset and then calculate the size.
413 new_size = ALIGNDOWN(start + offset + new_size,
414 alignment) - start - offset;
418 new_size = ALIGNDOWN(last + offset + 1, alignment) -
423 s = find_provcfg(pp, "start");
424 new_lba = (off_t)strtoimax(s, NULL, 0);
426 * Is there any free space between current and
429 new_lba = ALIGNDOWN(new_lba + offset, alignment) - offset;
431 new_size = new_lba - start;
433 geom_deletetree(&mesh);
438 snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)new_size);
439 gctl_change_param(req, "size", -1, ssize);
440 geom_deletetree(&mesh);
445 gpart_autofill(struct gctl_req *req)
450 struct gprovider *pp;
451 off_t first, last, a_first;
452 off_t size, start, a_lba;
453 off_t lba, len, alignment, offset;
456 int error, has_size, has_start, has_alignment;
458 s = gctl_get_ascii(req, "verb");
459 if (strcmp(s, "resize") == 0)
460 return gpart_autofill_resize(req);
461 if (strcmp(s, "add") != 0)
464 error = geom_gettree(&mesh);
467 s = gctl_get_ascii(req, "class");
470 cp = find_class(&mesh, s);
472 errx(EXIT_FAILURE, "Class %s not found.", s);
473 s = gctl_get_ascii(req, "arg0");
476 gp = find_geom(cp, s);
478 if (g_device_path(s) == NULL) {
479 errx(EXIT_FAILURE, "No such geom %s.", s);
482 * We don't free memory allocated by g_device_path() as
483 * we are about to exit.
486 "No partitioning scheme found on geom %s. Create one first using 'gpart create'.",
490 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
492 errx(EXIT_FAILURE, "Provider for geom %s not found.", s);
494 s = gctl_get_ascii(req, "alignment");
495 has_alignment = (*s == '*') ? 0 : 1;
498 error = g_parse_lba(s, pp->lg_sectorsize, &alignment);
500 errc(EXIT_FAILURE, error, "Invalid alignment param");
502 errx(EXIT_FAILURE, "Invalid alignment param");
504 error = gctl_delete_param(req, "alignment");
506 errc(EXIT_FAILURE, error, "internal error");
508 s = gctl_get_ascii(req, "size");
509 has_size = (*s == '*') ? 0 : 1;
512 error = g_parse_lba(s, pp->lg_sectorsize, &size);
514 errc(EXIT_FAILURE, error, "Invalid size param");
517 s = gctl_get_ascii(req, "start");
518 has_start = (*s == '*') ? 0 : 1;
521 error = g_parse_lba(s, pp->lg_sectorsize, &start);
523 errc(EXIT_FAILURE, error, "Invalid start param");
526 /* No autofill necessary. */
527 if (has_size && has_start && !has_alignment)
530 len = pp->lg_stripesize / pp->lg_sectorsize;
531 if (len > 0 && !has_alignment)
534 /* Adjust parameters to stripeoffset */
535 offset = (pp->lg_stripeoffset / pp->lg_sectorsize) % alignment;
536 start = ALIGNUP(start + offset, alignment);
537 if (size > alignment)
538 size = ALIGNDOWN(size, alignment);
540 s = find_geomcfg(gp, "first");
542 errx(EXIT_FAILURE, "Starting block not found for geom %s",
544 first = (off_t)strtoimax(s, NULL, 0);
545 s = find_geomcfg(gp, "last");
547 errx(EXIT_FAILURE, "Final block not found for geom %s",
549 last = (off_t)strtoimax(s, NULL, 0);
551 a_first = ALIGNUP(first + offset, alignment);
552 last = ALIGNDOWN(last + offset + 1, alignment) - 1;
555 while ((pp = find_provider(gp, first)) != NULL) {
556 s = find_provcfg(pp, "start");
557 lba = (off_t)strtoimax(s, NULL, 0);
558 a_lba = ALIGNDOWN(lba + offset, alignment);
559 if (first < a_lba && a_first < a_lba) {
560 /* Free space [first, lba> */
561 len = a_lba - a_first;
564 (uintmax_t)(len - size) < grade) {
568 } else if (has_start) {
569 if (start >= a_first && start < a_lba) {
570 size = a_lba - start;
571 grade = start - a_first;
574 if (grade == ~0ULL || len > size) {
582 s = find_provcfg(pp, "end");
583 first = (off_t)strtoimax(s, NULL, 0) + 1;
584 if (first + offset > a_first)
585 a_first = ALIGNUP(first + offset, alignment);
587 if (a_first <= last) {
588 /* Free space [first-last] */
589 len = ALIGNDOWN(last - a_first + 1, alignment);
592 (uintmax_t)(len - size) < grade) {
596 } else if (has_start) {
597 if (start >= a_first && start <= last) {
598 size = ALIGNDOWN(last - start + 1, alignment);
599 grade = start - a_first;
602 if (grade == ~0ULL || len > size) {
609 if (grade == ~0ULL) {
610 geom_deletetree(&mesh);
613 start -= offset; /* Return back to real offset */
615 snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)size);
616 gctl_change_param(req, "size", -1, ssize);
617 snprintf(sstart, sizeof(sstart), "%jd", (intmax_t)start);
618 gctl_change_param(req, "start", -1, sstart);
619 geom_deletetree(&mesh);
624 gpart_show_geom(struct ggeom *gp, const char *element, int show_providers)
626 struct gprovider *pp;
627 const char *s, *scheme;
628 off_t first, last, sector, end;
630 int idx, wblocks, wname, wmax;
632 if (geom_is_withered(gp))
634 scheme = find_geomcfg(gp, "scheme");
636 errx(EXIT_FAILURE, "Scheme not found for geom %s", gp->lg_name);
637 s = find_geomcfg(gp, "first");
639 errx(EXIT_FAILURE, "Starting block not found for geom %s",
641 first = (off_t)strtoimax(s, NULL, 0);
642 s = find_geomcfg(gp, "last");
644 errx(EXIT_FAILURE, "Final block not found for geom %s",
646 last = (off_t)strtoimax(s, NULL, 0);
648 s = find_geomcfg(gp, "state");
650 errx(EXIT_FAILURE, "State not found for geom %s", gp->lg_name);
651 if (s != NULL && *s != 'C')
653 wmax = strlen(gp->lg_name);
654 if (show_providers) {
655 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
656 wname = strlen(pp->lg_name);
662 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
663 secsz = pp->lg_sectorsize;
664 printf("=>%*jd %*jd %*s %s (%s)%s\n",
665 wblocks, (intmax_t)first, wblocks, (intmax_t)(last - first + 1),
667 scheme, fmtsize(pp->lg_mediasize),
668 s ? " [CORRUPT]": "");
670 while ((pp = find_provider(gp, first)) != NULL) {
671 s = find_provcfg(pp, "start");
672 sector = (off_t)strtoimax(s, NULL, 0);
674 s = find_provcfg(pp, "end");
675 end = (off_t)strtoimax(s, NULL, 0);
676 length = end - sector + 1;
678 s = find_provcfg(pp, "index");
680 if (first < sector) {
681 printf(" %*jd %*jd %*s - free - (%s)\n",
682 wblocks, (intmax_t)first, wblocks,
683 (intmax_t)(sector - first), wname, "",
684 fmtsize((sector - first) * secsz));
686 if (show_providers) {
687 printf(" %*jd %*jd %*s %s %s (%s)\n",
688 wblocks, (intmax_t)sector, wblocks,
689 (intmax_t)length, wname, pp->lg_name,
690 find_provcfg(pp, element), fmtattrib(pp),
691 fmtsize(pp->lg_mediasize));
693 printf(" %*jd %*jd %*d %s %s (%s)\n",
694 wblocks, (intmax_t)sector, wblocks,
695 (intmax_t)length, wname, idx,
696 find_provcfg(pp, element), fmtattrib(pp),
697 fmtsize(pp->lg_mediasize));
701 length = last - first + 1;
702 printf(" %*jd %*jd %*s - free - (%s)\n",
703 wblocks, (intmax_t)first, wblocks, (intmax_t)length,
705 fmtsize(length * secsz));
711 gpart_show_hasopt(struct gctl_req *req, const char *opt, const char *elt)
714 if (!gctl_get_int(req, "%s", opt))
718 errx(EXIT_FAILURE, "-l and -r are mutually exclusive");
724 gpart_show(struct gctl_req *req, unsigned int fl __unused)
727 struct gclass *classp;
729 const char *element, *name;
730 int error, i, nargs, show_providers;
733 if (gpart_show_hasopt(req, "show_label", element))
735 if (gpart_show_hasopt(req, "show_rawtype", element))
740 name = gctl_get_ascii(req, "class");
743 error = geom_gettree(&mesh);
745 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
746 classp = find_class(&mesh, name);
747 if (classp == NULL) {
748 geom_deletetree(&mesh);
749 errx(EXIT_FAILURE, "Class %s not found.", name);
751 show_providers = gctl_get_int(req, "show_providers");
752 nargs = gctl_get_int(req, "nargs");
754 for (i = 0; i < nargs; i++) {
755 name = gctl_get_ascii(req, "arg%d", i);
756 gp = find_geom(classp, name);
758 gpart_show_geom(gp, element, show_providers);
760 errx(EXIT_FAILURE, "No such geom: %s.", name);
763 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
764 gpart_show_geom(gp, element, show_providers);
767 geom_deletetree(&mesh);
771 gpart_backup(struct gctl_req *req, unsigned int fl __unused)
774 struct gclass *classp;
775 struct gprovider *pp;
777 const char *s, *scheme;
780 int error, i, windex, wblocks, wtype;
782 if (gctl_get_int(req, "nargs") != 1)
783 errx(EXIT_FAILURE, "Invalid number of arguments.");
784 error = geom_gettree(&mesh);
786 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
787 s = gctl_get_ascii(req, "class");
790 classp = find_class(&mesh, s);
791 if (classp == NULL) {
792 geom_deletetree(&mesh);
793 errx(EXIT_FAILURE, "Class %s not found.", s);
795 s = gctl_get_ascii(req, "arg0");
798 gp = find_geom(classp, s);
800 errx(EXIT_FAILURE, "No such geom: %s.", s);
801 scheme = find_geomcfg(gp, "scheme");
804 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
805 s = find_geomcfg(gp, "last");
810 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
811 s = find_provcfg(pp, "type");
816 s = find_geomcfg(gp, "entries");
820 printf("%s %s\n", scheme, s);
821 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
822 s = find_provcfg(pp, "start");
823 sector = (off_t)strtoimax(s, NULL, 0);
825 s = find_provcfg(pp, "end");
826 end = (off_t)strtoimax(s, NULL, 0);
827 length = end - sector + 1;
829 s = find_provcfg(pp, "label");
830 printf("%-*s %*s %*jd %*jd %s %s\n",
831 windex, find_provcfg(pp, "index"),
832 wtype, find_provcfg(pp, "type"),
833 wblocks, (intmax_t)sector,
834 wblocks, (intmax_t)length,
835 (s != NULL) ? s: "", fmtattrib(pp));
837 geom_deletetree(&mesh);
841 skip_line(const char *p)
847 if (isspace(*p) == 0)
855 gpart_sighndl(int sig __unused)
861 gpart_restore(struct gctl_req *req, unsigned int fl __unused)
864 struct gclass *classp;
867 struct sigaction si_sa;
868 const char *s, *flags, *errstr, *label;
869 char **ap, *argv[6], line[BUFSIZ], *pline;
870 int error, forced, i, l, nargs, created, rl;
873 nargs = gctl_get_int(req, "nargs");
875 errx(EXIT_FAILURE, "Invalid number of arguments.");
877 forced = gctl_get_int(req, "force");
878 flags = gctl_get_ascii(req, "flags");
879 rl = gctl_get_int(req, "restore_labels");
880 s = gctl_get_ascii(req, "class");
883 error = geom_gettree(&mesh);
885 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
886 classp = find_class(&mesh, s);
887 if (classp == NULL) {
888 geom_deletetree(&mesh);
889 errx(EXIT_FAILURE, "Class %s not found.", s);
892 sigemptyset(&si_sa.sa_mask);
894 si_sa.sa_handler = gpart_sighndl;
895 if (sigaction(SIGINT, &si_sa, 0) == -1)
896 err(EXIT_FAILURE, "sigaction SIGINT");
899 /* destroy existent partition table before restore */
900 for (i = 0; i < nargs; i++) {
901 s = gctl_get_ascii(req, "arg%d", i);
902 gp = find_geom(classp, s);
904 r = gctl_get_handle();
905 gctl_ro_param(r, "class", -1,
907 gctl_ro_param(r, "verb", -1, "destroy");
908 gctl_ro_param(r, "flags", -1, "restore");
909 gctl_ro_param(r, "force", sizeof(forced),
911 gctl_ro_param(r, "arg0", -1, s);
912 errstr = gctl_issue(r);
913 if (errstr != NULL && errstr[0] != '\0') {
914 gpart_print_error(errstr);
923 while (undo_restore == 0 &&
924 fgets(line, sizeof(line) - 1, stdin) != NULL) {
925 /* Format of backup entries:
926 * <scheme name> <number of entries>
927 * <index> <type> <start> <size> [label] ['['attrib[,attrib]']']
929 pline = (char *)line;
930 pline[strlen(line) - 1] = 0;
931 if (skip_line(pline))
934 (*ap = strsep(&pline, " \t")) != NULL;)
935 if (**ap != '\0' && ++ap >= &argv[6])
938 label = pline = NULL;
939 if (l == 1 || l == 2) { /* create table */
941 errx(EXIT_FAILURE, "Incorrect backup format.");
943 n = strtoimax(argv[1], NULL, 0);
944 for (i = 0; i < nargs; i++) {
945 s = gctl_get_ascii(req, "arg%d", i);
946 r = gctl_get_handle();
947 gctl_ro_param(r, "class", -1,
949 gctl_ro_param(r, "verb", -1, "create");
950 gctl_ro_param(r, "scheme", -1, argv[0]);
952 gctl_ro_param(r, "entries",
954 gctl_ro_param(r, "flags", -1, "restore");
955 gctl_ro_param(r, "arg0", -1, s);
956 errstr = gctl_issue(r);
957 if (errstr != NULL && errstr[0] != '\0') {
958 gpart_print_error(errstr);
966 } else if (l < 4 || created == 0)
967 errx(EXIT_FAILURE, "Incorrect backup format.");
969 if (strchr(argv[4], '[') == NULL)
977 /* Add partitions to each table */
978 for (i = 0; i < nargs; i++) {
979 s = gctl_get_ascii(req, "arg%d", i);
980 r = gctl_get_handle();
981 n = strtoimax(argv[0], NULL, 0);
982 gctl_ro_param(r, "class", -1, classp->lg_name);
983 gctl_ro_param(r, "verb", -1, "add");
984 gctl_ro_param(r, "flags", -1, "restore");
985 gctl_ro_param(r, GPART_PARAM_INDEX, sizeof(n), &n);
986 gctl_ro_param(r, "type", -1, argv[1]);
987 gctl_ro_param(r, "start", -1, argv[2]);
988 gctl_ro_param(r, "size", -1, argv[3]);
989 if (rl != 0 && label != NULL)
990 gctl_ro_param(r, "label", -1, argv[4]);
991 gctl_ro_param(r, "alignment", -1, GPART_AUTOFILL);
992 gctl_ro_param(r, "arg0", -1, s);
993 error = gpart_autofill(r);
995 errc(EXIT_FAILURE, error, "autofill");
996 errstr = gctl_issue(r);
997 if (errstr != NULL && errstr[0] != '\0') {
998 gpart_print_error(errstr);
1004 if (pline == NULL || *pline != '[')
1006 /* set attributes */
1009 (*ap = strsep(&pline, ",]")) != NULL;)
1010 if (**ap != '\0' && ++ap >= &argv[6])
1012 for (i = 0; i < nargs; i++) {
1014 s = gctl_get_ascii(req, "arg%d", i);
1016 r = gctl_get_handle();
1017 gctl_ro_param(r, "class", -1, classp->lg_name);
1018 gctl_ro_param(r, "verb", -1, "set");
1019 gctl_ro_param(r, "flags", -1, "restore");
1020 gctl_ro_param(r, GPART_PARAM_INDEX,
1022 gctl_ro_param(r, "attrib", -1, argv[--l]);
1023 gctl_ro_param(r, "arg0", -1, s);
1024 errstr = gctl_issue(r);
1025 if (errstr != NULL && errstr[0] != '\0') {
1026 gpart_print_error(errstr);
1036 /* commit changes if needed */
1037 if (strchr(flags, 'C') != NULL) {
1038 for (i = 0; i < nargs; i++) {
1039 s = gctl_get_ascii(req, "arg%d", i);
1040 r = gctl_get_handle();
1041 gctl_ro_param(r, "class", -1, classp->lg_name);
1042 gctl_ro_param(r, "verb", -1, "commit");
1043 gctl_ro_param(r, "arg0", -1, s);
1044 errstr = gctl_issue(r);
1045 if (errstr != NULL && errstr[0] != '\0') {
1046 gpart_print_error(errstr);
1054 geom_deletetree(&mesh);
1058 for (i = 0; i < nargs; i++) {
1059 s = gctl_get_ascii(req, "arg%d", i);
1060 r = gctl_get_handle();
1061 gctl_ro_param(r, "class", -1, classp->lg_name);
1062 gctl_ro_param(r, "verb", -1, "undo");
1063 gctl_ro_param(r, "arg0", -1, s);
1068 geom_deletetree(&mesh);
1073 gpart_bootfile_read(const char *bootfile, ssize_t *size)
1079 if (stat(bootfile, &sb) == -1)
1080 err(EXIT_FAILURE, "%s", bootfile);
1081 if (!S_ISREG(sb.st_mode))
1082 errx(EXIT_FAILURE, "%s: not a regular file", bootfile);
1083 if (sb.st_size == 0)
1084 errx(EXIT_FAILURE, "%s: empty file", bootfile);
1085 if (*size > 0 && sb.st_size > *size)
1086 errx(EXIT_FAILURE, "%s: file too big (%zu limit)", bootfile,
1091 fd = open(bootfile, O_RDONLY);
1093 err(EXIT_FAILURE, "%s", bootfile);
1094 code = malloc(*size);
1096 err(EXIT_FAILURE, NULL);
1097 if (read(fd, code, *size) != *size)
1098 err(EXIT_FAILURE, "%s", bootfile);
1105 gpart_write_partcode(struct ggeom *gp, int idx, void *code, ssize_t size)
1108 struct gprovider *pp;
1114 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1115 s = find_provcfg(pp, "index");
1123 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1124 if (pp->lg_mediasize < size)
1125 errx(EXIT_FAILURE, "%s: not enough space", dsf);
1126 fd = open(dsf, O_WRONLY);
1128 err(EXIT_FAILURE, "%s", dsf);
1130 * When writing to a disk device, the write must be
1131 * sector aligned and not write to any partial sectors,
1132 * so round up the buffer size to the next sector and zero it.
1134 bsize = (size + pp->lg_sectorsize - 1) /
1135 pp->lg_sectorsize * pp->lg_sectorsize;
1136 buf = calloc(1, bsize);
1138 err(EXIT_FAILURE, "%s", dsf);
1139 bcopy(code, buf, size);
1140 if (write(fd, buf, bsize) != bsize)
1141 err(EXIT_FAILURE, "%s", dsf);
1144 printf("partcode written to %s\n", pp->lg_name);
1146 errx(EXIT_FAILURE, "invalid partition index");
1150 gpart_write_partcode_vtoc8(struct ggeom *gp, int idx, void *code)
1153 struct gprovider *pp;
1158 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1159 s = find_provcfg(pp, "index");
1162 if (idx != 0 && atoi(s) != idx)
1164 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1165 if (pp->lg_sectorsize != sizeof(struct vtoc8))
1166 errx(EXIT_FAILURE, "%s: unexpected sector "
1167 "size (%d)\n", dsf, pp->lg_sectorsize);
1168 if (pp->lg_mediasize < VTOC_BOOTSIZE)
1170 fd = open(dsf, O_WRONLY);
1172 err(EXIT_FAILURE, "%s", dsf);
1174 * We ignore the first VTOC_BOOTSIZE bytes of boot code in
1175 * order to avoid overwriting the label.
1177 if (lseek(fd, sizeof(struct vtoc8), SEEK_SET) !=
1178 sizeof(struct vtoc8))
1179 err(EXIT_FAILURE, "%s", dsf);
1180 if (write(fd, (caddr_t)code + sizeof(struct vtoc8),
1181 VTOC_BOOTSIZE - sizeof(struct vtoc8)) != VTOC_BOOTSIZE -
1182 sizeof(struct vtoc8))
1183 err(EXIT_FAILURE, "%s", dsf);
1186 if (idx != 0 && atoi(s) == idx)
1190 errx(EXIT_FAILURE, "%s: no partitions", gp->lg_name);
1192 printf("partcode written to %s\n",
1193 idx != 0 ? pp->lg_name: gp->lg_name);
1197 gpart_bootcode(struct gctl_req *req, unsigned int fl)
1200 struct gclass *classp;
1203 void *bootcode, *partcode;
1204 size_t bootsize, partsize;
1205 int error, idx, vtoc8;
1207 if (gctl_has_param(req, GPART_PARAM_BOOTCODE)) {
1208 s = gctl_get_ascii(req, GPART_PARAM_BOOTCODE);
1209 bootsize = 800 * 1024; /* Arbitrary limit. */
1210 bootcode = gpart_bootfile_read(s, &bootsize);
1211 error = gctl_change_param(req, GPART_PARAM_BOOTCODE, bootsize,
1214 errc(EXIT_FAILURE, error, "internal error");
1218 s = gctl_get_ascii(req, "class");
1221 error = geom_gettree(&mesh);
1223 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
1224 classp = find_class(&mesh, s);
1225 if (classp == NULL) {
1226 geom_deletetree(&mesh);
1227 errx(EXIT_FAILURE, "Class %s not found.", s);
1229 if (gctl_get_int(req, "nargs") != 1)
1230 errx(EXIT_FAILURE, "Invalid number of arguments.");
1231 s = gctl_get_ascii(req, "arg0");
1234 gp = find_geom(classp, s);
1236 errx(EXIT_FAILURE, "No such geom: %s.", s);
1237 s = find_geomcfg(gp, "scheme");
1239 errx(EXIT_FAILURE, "Scheme not found for geom %s", gp->lg_name);
1240 if (strcmp(s, "VTOC8") == 0)
1245 if (gctl_has_param(req, GPART_PARAM_PARTCODE)) {
1246 s = gctl_get_ascii(req, GPART_PARAM_PARTCODE);
1248 partsize = VTOC_BOOTSIZE;
1250 partsize = 1024 * 1024; /* Arbitrary limit. */
1251 partcode = gpart_bootfile_read(s, &partsize);
1252 error = gctl_delete_param(req, GPART_PARAM_PARTCODE);
1254 errc(EXIT_FAILURE, error, "internal error");
1258 if (gctl_has_param(req, GPART_PARAM_INDEX)) {
1259 if (partcode == NULL)
1260 errx(EXIT_FAILURE, "-i is only valid with -p");
1261 idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
1263 errx(EXIT_FAILURE, "invalid partition index");
1264 error = gctl_delete_param(req, GPART_PARAM_INDEX);
1266 errc(EXIT_FAILURE, error, "internal error");
1270 if (partcode != NULL) {
1273 errx(EXIT_FAILURE, "missing -i option");
1274 gpart_write_partcode(gp, idx, partcode, partsize);
1276 if (partsize != VTOC_BOOTSIZE)
1277 errx(EXIT_FAILURE, "invalid bootcode");
1278 gpart_write_partcode_vtoc8(gp, idx, partcode);
1281 if (bootcode == NULL)
1282 errx(EXIT_FAILURE, "no -b nor -p");
1284 if (bootcode != NULL)
1285 gpart_issue(req, fl);
1287 geom_deletetree(&mesh);
1292 gpart_print_error(const char *errstr)
1297 error = strtol(errstr, &errmsg, 0);
1298 if (errmsg != errstr) {
1299 while (errmsg[0] == ' ')
1301 if (errmsg[0] != '\0')
1302 warnc(error, "%s", errmsg);
1306 warnx("%s", errmsg);
1309 static _Noreturn void
1310 gpart_issue(struct gctl_req *req, unsigned int fl __unused)
1316 if (gctl_get_int(req, "nargs") != 1)
1317 errx(EXIT_FAILURE, "Invalid number of arguments.");
1318 (void)gctl_delete_param(req, "nargs");
1320 /* autofill parameters (if applicable). */
1321 error = gpart_autofill(req);
1323 warnc(error, "autofill");
1324 status = EXIT_FAILURE;
1328 bzero(buf, sizeof(buf));
1329 gctl_rw_param(req, "output", sizeof(buf), buf);
1330 errstr = gctl_issue(req);
1331 if (errstr == NULL || errstr[0] == '\0') {
1334 status = EXIT_SUCCESS;
1338 gpart_print_error(errstr);
1339 status = EXIT_FAILURE;