2 * SPDX-License-Identifier: BSD-2-Clause
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>
50 #include "core/geom.h"
51 #include "misc/subr.h"
53 #ifdef STATIC_GEOM_CLASSES
54 #define PUBSYM(x) gpart_##x
59 uint32_t PUBSYM(lib_version) = G_LIB_VERSION;
60 uint32_t PUBSYM(version) = 0;
62 static char sstart[32];
63 static char ssize[32];
64 volatile sig_atomic_t undo_restore;
66 #define GPART_AUTOFILL "*"
67 #define GPART_FLAGS "C"
69 #define GPART_PARAM_BOOTCODE "bootcode"
70 #define GPART_PARAM_INDEX "index"
71 #define GPART_PARAM_PARTCODE "partcode"
72 #define GPART_PARAM_SKIP_DSN "skip_dsn"
74 static struct gclass *find_class(struct gmesh *, const char *);
75 static struct ggeom * find_geom(struct gclass *, const char *);
76 static int geom_is_withered(struct ggeom *);
77 static const char *find_geomcfg(struct ggeom *, const char *);
78 static const char *find_provcfg(struct gprovider *, const char *);
79 static struct gprovider *find_provider(struct ggeom *, off_t);
80 static const char *fmtsize(int64_t);
81 static int gpart_autofill(struct gctl_req *);
82 static int gpart_autofill_resize(struct gctl_req *);
83 static void gpart_bootcode(struct gctl_req *, unsigned int);
84 static void *gpart_bootfile_read(const char *, ssize_t *);
85 static _Noreturn void gpart_issue(struct gctl_req *, unsigned int);
86 static void gpart_show(struct gctl_req *, unsigned int);
87 static void gpart_show_geom(struct ggeom *, const char *, int);
88 static int gpart_show_hasopt(struct gctl_req *, const char *, const char *);
89 static void gpart_write_partcode(struct gctl_req *, int, void *, ssize_t);
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 },
115 { 'N', GPART_PARAM_SKIP_DSN, NULL, G_TYPE_BOOL },
117 "[-N] [-b bootcode] [-p partcode -i index] [-f flags] geom"
119 { "commit", 0, gpart_issue, G_NULL_OPTS,
122 { "create", 0, gpart_issue, {
123 { 's', "scheme", NULL, G_TYPE_STRING },
124 { 'n', "entries", G_VAL_OPTIONAL, G_TYPE_NUMBER },
125 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
127 "-s scheme [-n entries] [-f flags] provider"
129 { "delete", 0, gpart_issue, {
130 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
131 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
133 "-i index [-f flags] geom"
135 { "destroy", 0, gpart_issue, {
136 { 'F', "force", NULL, G_TYPE_BOOL },
137 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
139 "[-F] [-f flags] geom"
141 { "modify", 0, gpart_issue, {
142 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
143 { 'l', "label", G_VAL_OPTIONAL, G_TYPE_STRING },
144 { 't', "type", G_VAL_OPTIONAL, G_TYPE_STRING },
145 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
147 "-i index [-l label] [-t type] [-f flags] geom"
149 { "set", 0, gpart_issue, {
150 { 'a', "attrib", NULL, G_TYPE_STRING },
151 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
152 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
154 "-a attrib [-i index] [-f flags] geom"
156 { "show", 0, gpart_show, {
157 { 'l', "show_label", NULL, G_TYPE_BOOL },
158 { 'r', "show_rawtype", NULL, G_TYPE_BOOL },
159 { 'p', "show_providers", NULL, G_TYPE_BOOL },
161 "[-l | -r] [-p] [geom ...]"
163 { "undo", 0, gpart_issue, G_NULL_OPTS,
166 { "unset", 0, gpart_issue, {
167 { 'a', "attrib", NULL, G_TYPE_STRING },
168 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
169 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
171 "-a attrib [-i index] [-f flags] geom"
173 { "resize", 0, gpart_issue, {
174 { 'a', "alignment", GPART_AUTOFILL, G_TYPE_STRING },
175 { 's', "size", GPART_AUTOFILL, G_TYPE_STRING },
176 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
177 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
179 "-i index [-a alignment] [-s size] [-f flags] geom"
181 { "restore", 0, gpart_restore, {
182 { 'F', "force", NULL, G_TYPE_BOOL },
183 { 'l', "restore_labels", NULL, G_TYPE_BOOL },
184 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
186 "[-lF] [-f flags] provider [...]"
188 { "recover", 0, gpart_issue, {
189 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
196 static struct gclass *
197 find_class(struct gmesh *mesh, const char *name)
199 struct gclass *classp;
201 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
202 if (strcmp(classp->lg_name, name) == 0)
208 static struct ggeom *
209 find_geom(struct gclass *classp, const char *name)
211 struct ggeom *gp, *wgp;
213 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
214 name += sizeof(_PATH_DEV) - 1;
216 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
217 if (strcmp(gp->lg_name, name) != 0)
219 if (!geom_is_withered(gp))
228 geom_is_withered(struct ggeom *gp)
232 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
233 if (!strcmp(gc->lg_name, "wither"))
240 find_geomcfg(struct ggeom *gp, const char *cfg)
244 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
245 if (!strcmp(gc->lg_name, cfg))
252 find_provcfg(struct gprovider *pp, const char *cfg)
256 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
257 if (!strcmp(gc->lg_name, cfg))
263 static struct gprovider *
264 find_provider(struct ggeom *gp, off_t minsector)
266 struct gprovider *pp, *bestpp;
268 off_t sector, bestsector;
272 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
273 s = find_provcfg(pp, "start");
274 sector = (off_t)strtoimax(s, NULL, 0);
275 if (sector < minsector)
277 if (bestpp != NULL && sector >= bestsector)
287 fmtsize(int64_t rawsz)
291 humanize_number(buf, sizeof(buf), rawsz, "", HN_AUTOSCALE,
292 HN_B | HN_NOSPACE | HN_DECIMAL);
297 fmtattrib(struct gprovider *pp)
299 static char buf[128];
305 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
306 if (strcmp(gc->lg_name, "attrib") != 0)
308 idx += snprintf(buf + idx, sizeof(buf) - idx, "%s%s",
309 (idx == 0) ? " [" : ",", gc->lg_val);
312 snprintf(buf + idx, sizeof(buf) - idx, "] ");
316 #define ALIGNDOWN(d, a) ((d) - (d) % (a))
317 #define ALIGNUP(d, a) ((d) % (a) ? (d) - (d) % (a) + (a): (d))
320 gpart_autofill_resize(struct gctl_req *req)
325 struct gprovider *pp;
326 off_t last, size, start, new_size;
327 off_t lba, new_lba, alignment, offset;
329 int error, idx, has_alignment;
331 idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
333 errx(EXIT_FAILURE, "invalid partition index");
335 s = gctl_get_ascii(req, "class");
338 g = gctl_get_ascii(req, "arg0");
341 error = geom_gettree_geom(&mesh, s, g, 1);
344 cp = find_class(&mesh, s);
346 errx(EXIT_FAILURE, "Class %s not found.", s);
347 gp = find_geom(cp, g);
349 errx(EXIT_FAILURE, "No such geom: %s.", g);
350 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
352 errx(EXIT_FAILURE, "Provider for geom %s not found.", g);
354 s = gctl_get_ascii(req, "alignment");
355 has_alignment = (*s == '*') ? 0 : 1;
358 error = g_parse_lba(s, pp->lg_sectorsize, &alignment);
360 errc(EXIT_FAILURE, error, "Invalid alignment param");
362 errx(EXIT_FAILURE, "Invalid alignment param");
364 lba = pp->lg_stripesize / pp->lg_sectorsize;
368 error = gctl_delete_param(req, "alignment");
370 errc(EXIT_FAILURE, error, "internal error");
372 s = gctl_get_ascii(req, "size");
376 error = g_parse_lba(s, pp->lg_sectorsize, &new_size);
378 errc(EXIT_FAILURE, error, "Invalid size param");
379 /* no autofill necessary. */
380 if (has_alignment == 0)
384 offset = (pp->lg_stripeoffset / pp->lg_sectorsize) % alignment;
385 s = find_geomcfg(gp, "last");
387 errx(EXIT_FAILURE, "Final block not found for geom %s",
389 last = (off_t)strtoimax(s, NULL, 0);
390 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
391 s = find_provcfg(pp, "index");
398 errx(EXIT_FAILURE, "invalid partition index");
400 s = find_provcfg(pp, "start");
401 start = (off_t)strtoimax(s, NULL, 0);
402 s = find_provcfg(pp, "end");
403 lba = (off_t)strtoimax(s, NULL, 0);
404 size = lba - start + 1;
406 pp = find_provider(gp, lba + 1);
407 if (new_size > 0 && (new_size <= size || pp == NULL)) {
408 /* The start offset may be not aligned, so we align the end
409 * offset and then calculate the size.
411 new_size = ALIGNDOWN(start + offset + new_size,
412 alignment) - start - offset;
416 new_size = ALIGNDOWN(last + offset + 1, alignment) -
421 s = find_provcfg(pp, "start");
422 new_lba = (off_t)strtoimax(s, NULL, 0);
424 * Is there any free space between current and
427 new_lba = ALIGNDOWN(new_lba + offset, alignment) - offset;
429 new_size = new_lba - start;
431 geom_deletetree(&mesh);
436 snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)new_size);
437 gctl_change_param(req, "size", -1, ssize);
438 geom_deletetree(&mesh);
443 gpart_autofill(struct gctl_req *req)
448 struct gprovider *pp;
449 off_t first, last, a_first;
450 off_t size, start, a_lba;
451 off_t lba, len, alignment, offset;
454 int error, has_size, has_start, has_alignment;
456 s = gctl_get_ascii(req, "verb");
457 if (strcmp(s, "resize") == 0)
458 return gpart_autofill_resize(req);
459 if (strcmp(s, "add") != 0)
462 s = gctl_get_ascii(req, "class");
465 g = gctl_get_ascii(req, "arg0");
468 error = geom_gettree_geom(&mesh, s, g, 1);
471 cp = find_class(&mesh, s);
473 errx(EXIT_FAILURE, "Class %s not found.", s);
474 gp = find_geom(cp, g);
476 if (g_device_path(g) == NULL) {
477 errx(EXIT_FAILURE, "No such geom %s.", g);
480 * We don't free memory allocated by g_device_path() as
481 * we are about to exit.
484 "No partitioning scheme found on geom %s. Create one first using 'gpart create'.",
488 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
490 errx(EXIT_FAILURE, "Provider for geom %s not found.", g);
492 s = gctl_get_ascii(req, "alignment");
493 has_alignment = (*s == '*') ? 0 : 1;
496 error = g_parse_lba(s, pp->lg_sectorsize, &alignment);
498 errc(EXIT_FAILURE, error, "Invalid alignment param");
500 errx(EXIT_FAILURE, "Invalid alignment param");
502 error = gctl_delete_param(req, "alignment");
504 errc(EXIT_FAILURE, error, "internal error");
506 s = gctl_get_ascii(req, "size");
507 has_size = (*s == '*') ? 0 : 1;
510 error = g_parse_lba(s, pp->lg_sectorsize, &size);
512 errc(EXIT_FAILURE, error, "Invalid size param");
515 s = gctl_get_ascii(req, "start");
516 has_start = (*s == '*') ? 0 : 1;
519 error = g_parse_lba(s, pp->lg_sectorsize, &start);
521 errc(EXIT_FAILURE, error, "Invalid start param");
524 /* No autofill necessary. */
525 if (has_size && has_start && !has_alignment)
528 len = pp->lg_stripesize / pp->lg_sectorsize;
529 if (len > 0 && !has_alignment)
532 /* Adjust parameters to stripeoffset */
533 offset = (pp->lg_stripeoffset / pp->lg_sectorsize) % alignment;
534 start = ALIGNUP(start + offset, alignment);
535 if (size > alignment)
536 size = ALIGNDOWN(size, alignment);
538 s = find_geomcfg(gp, "first");
540 errx(EXIT_FAILURE, "Starting block not found for geom %s",
542 first = (off_t)strtoimax(s, NULL, 0);
543 s = find_geomcfg(gp, "last");
545 errx(EXIT_FAILURE, "Final block not found for geom %s",
547 last = (off_t)strtoimax(s, NULL, 0);
549 a_first = ALIGNUP(first + offset, alignment);
550 last = ALIGNDOWN(last + offset + 1, alignment) - 1;
553 while ((pp = find_provider(gp, first)) != NULL) {
554 s = find_provcfg(pp, "start");
555 lba = (off_t)strtoimax(s, NULL, 0);
556 a_lba = ALIGNDOWN(lba + offset, alignment);
557 if (first < a_lba && a_first < a_lba) {
558 /* Free space [first, lba> */
559 len = a_lba - a_first;
562 (uintmax_t)(len - size) < grade) {
566 } else if (has_start) {
567 if (start >= a_first && start < a_lba) {
568 size = a_lba - start;
569 grade = start - a_first;
572 if (grade == ~0ULL || len > size) {
580 s = find_provcfg(pp, "end");
581 first = (off_t)strtoimax(s, NULL, 0) + 1;
582 if (first + offset > a_first)
583 a_first = ALIGNUP(first + offset, alignment);
585 if (a_first <= last) {
586 /* Free space [first-last] */
587 len = ALIGNDOWN(last - a_first + 1, alignment);
590 (uintmax_t)(len - size) < grade) {
594 } else if (has_start) {
595 if (start >= a_first && start <= last) {
596 size = ALIGNDOWN(last - start + 1, alignment);
597 grade = start - a_first;
600 if (grade == ~0ULL || len > size) {
607 if (grade == ~0ULL) {
608 geom_deletetree(&mesh);
611 start -= offset; /* Return back to real offset */
613 snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)size);
614 gctl_change_param(req, "size", -1, ssize);
615 snprintf(sstart, sizeof(sstart), "%jd", (intmax_t)start);
616 gctl_change_param(req, "start", -1, sstart);
617 geom_deletetree(&mesh);
622 gpart_show_geom(struct ggeom *gp, const char *element, int show_providers)
624 struct gprovider *pp;
625 const char *s, *scheme;
626 off_t first, last, sector, end;
628 int idx, wblocks, wname, wmax;
630 if (geom_is_withered(gp))
632 scheme = find_geomcfg(gp, "scheme");
634 errx(EXIT_FAILURE, "Scheme not found for geom %s", gp->lg_name);
635 s = find_geomcfg(gp, "first");
637 errx(EXIT_FAILURE, "Starting block not found for geom %s",
639 first = (off_t)strtoimax(s, NULL, 0);
640 s = find_geomcfg(gp, "last");
642 errx(EXIT_FAILURE, "Final block not found for geom %s",
644 last = (off_t)strtoimax(s, NULL, 0);
646 s = find_geomcfg(gp, "state");
648 errx(EXIT_FAILURE, "State not found for geom %s", gp->lg_name);
649 if (s != NULL && *s != 'C')
651 wmax = strlen(gp->lg_name);
652 if (show_providers) {
653 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
654 wname = strlen(pp->lg_name);
660 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
661 secsz = pp->lg_sectorsize;
662 printf("=>%*jd %*jd %*s %s (%s)%s\n",
663 wblocks, (intmax_t)first, wblocks, (intmax_t)(last - first + 1),
665 scheme, fmtsize(pp->lg_mediasize),
666 s ? " [CORRUPT]": "");
668 while ((pp = find_provider(gp, first)) != NULL) {
669 s = find_provcfg(pp, "start");
670 sector = (off_t)strtoimax(s, NULL, 0);
672 s = find_provcfg(pp, "end");
673 end = (off_t)strtoimax(s, NULL, 0);
674 length = end - sector + 1;
676 s = find_provcfg(pp, "index");
678 if (first < sector) {
679 printf(" %*jd %*jd %*s - free - (%s)\n",
680 wblocks, (intmax_t)first, wblocks,
681 (intmax_t)(sector - first), wname, "",
682 fmtsize((sector - first) * secsz));
684 if (show_providers) {
685 printf(" %*jd %*jd %*s %s %s (%s)\n",
686 wblocks, (intmax_t)sector, wblocks,
687 (intmax_t)length, wname, pp->lg_name,
688 find_provcfg(pp, element), fmtattrib(pp),
689 fmtsize(pp->lg_mediasize));
691 printf(" %*jd %*jd %*d %s %s (%s)\n",
692 wblocks, (intmax_t)sector, wblocks,
693 (intmax_t)length, wname, idx,
694 find_provcfg(pp, element), fmtattrib(pp),
695 fmtsize(pp->lg_mediasize));
699 length = last - first + 1;
700 printf(" %*jd %*jd %*s - free - (%s)\n",
701 wblocks, (intmax_t)first, wblocks, (intmax_t)length,
703 fmtsize(length * secsz));
709 gpart_show_hasopt(struct gctl_req *req, const char *opt, const char *elt)
712 if (!gctl_get_int(req, "%s", opt))
716 errx(EXIT_FAILURE, "-l and -r are mutually exclusive");
722 gpart_show(struct gctl_req *req, unsigned int fl __unused)
725 struct gclass *classp;
727 const char *element, *name;
728 int error, i, nargs, show_providers;
731 if (gpart_show_hasopt(req, "show_label", element))
733 if (gpart_show_hasopt(req, "show_rawtype", element))
738 name = gctl_get_ascii(req, "class");
741 nargs = gctl_get_int(req, "nargs");
743 error = geom_gettree_geom(&mesh, name,
744 gctl_get_ascii(req, "arg0"), 1);
746 error = geom_gettree(&mesh);
748 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
749 classp = find_class(&mesh, name);
750 if (classp == NULL) {
751 geom_deletetree(&mesh);
752 errx(EXIT_FAILURE, "Class %s not found.", name);
754 show_providers = gctl_get_int(req, "show_providers");
756 for (i = 0; i < nargs; i++) {
757 name = gctl_get_ascii(req, "arg%d", i);
758 gp = find_geom(classp, name);
760 gpart_show_geom(gp, element, show_providers);
762 errx(EXIT_FAILURE, "No such geom: %s.", name);
765 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
766 gpart_show_geom(gp, element, show_providers);
769 geom_deletetree(&mesh);
773 gpart_backup(struct gctl_req *req, unsigned int fl __unused)
776 struct gclass *classp;
777 struct gprovider *pp;
779 const char *g, *s, *scheme;
782 int error, i, windex, wblocks, wtype;
784 if (gctl_get_int(req, "nargs") != 1)
785 errx(EXIT_FAILURE, "Invalid number of arguments.");
786 s = gctl_get_ascii(req, "class");
789 g = gctl_get_ascii(req, "arg0");
792 error = geom_gettree_geom(&mesh, s, g, 0);
794 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
795 classp = find_class(&mesh, s);
796 if (classp == NULL) {
797 geom_deletetree(&mesh);
798 errx(EXIT_FAILURE, "Class %s not found.", s);
800 gp = find_geom(classp, g);
802 errx(EXIT_FAILURE, "No such geom: %s.", g);
803 scheme = find_geomcfg(gp, "scheme");
806 s = find_geomcfg(gp, "last");
811 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
812 s = find_provcfg(pp, "type");
817 s = find_geomcfg(gp, "entries");
821 printf("%s %s\n", scheme, s);
822 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
823 s = find_provcfg(pp, "start");
824 sector = (off_t)strtoimax(s, NULL, 0);
826 s = find_provcfg(pp, "end");
827 end = (off_t)strtoimax(s, NULL, 0);
828 length = end - sector + 1;
830 s = find_provcfg(pp, "label");
831 printf("%-*s %*s %*jd %*jd %s %s\n",
832 windex, find_provcfg(pp, "index"),
833 wtype, find_provcfg(pp, "type"),
834 wblocks, (intmax_t)sector,
835 wblocks, (intmax_t)length,
836 (s != NULL) ? s: "", fmtattrib(pp));
838 geom_deletetree(&mesh);
842 skip_line(const char *p)
848 if (isspace(*p) == 0)
856 gpart_sighndl(int sig __unused)
862 gpart_restore(struct gctl_req *req, unsigned int fl __unused)
865 struct gclass *classp;
868 struct sigaction si_sa;
869 const char *s, *flags, *errstr, *label;
870 char **ap, *argv[6], line[BUFSIZ], *pline;
871 int error, forced, i, l, nargs, created, rl;
874 nargs = gctl_get_int(req, "nargs");
876 errx(EXIT_FAILURE, "Invalid number of arguments.");
878 forced = gctl_get_int(req, "force");
879 flags = gctl_get_ascii(req, "flags");
880 rl = gctl_get_int(req, "restore_labels");
881 s = gctl_get_ascii(req, "class");
884 error = geom_gettree(&mesh);
886 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
887 classp = find_class(&mesh, s);
888 if (classp == NULL) {
889 geom_deletetree(&mesh);
890 errx(EXIT_FAILURE, "Class %s not found.", s);
893 sigemptyset(&si_sa.sa_mask);
895 si_sa.sa_handler = gpart_sighndl;
896 if (sigaction(SIGINT, &si_sa, 0) == -1)
897 err(EXIT_FAILURE, "sigaction SIGINT");
900 /* destroy existent partition table before restore */
901 for (i = 0; i < nargs; i++) {
902 s = gctl_get_ascii(req, "arg%d", i);
903 gp = find_geom(classp, s);
905 r = gctl_get_handle();
906 gctl_ro_param(r, "class", -1,
908 gctl_ro_param(r, "verb", -1, "destroy");
909 gctl_ro_param(r, "flags", -1, "restore");
910 gctl_ro_param(r, "force", sizeof(forced),
912 gctl_ro_param(r, "arg0", -1, s);
913 errstr = gctl_issue(r);
914 if (errstr != NULL && errstr[0] != '\0') {
915 gpart_print_error(errstr);
924 while (undo_restore == 0 &&
925 fgets(line, sizeof(line) - 1, stdin) != NULL) {
926 /* Format of backup entries:
927 * <scheme name> <number of entries>
928 * <index> <type> <start> <size> [label] ['['attrib[,attrib]']']
930 pline = (char *)line;
931 pline[strlen(line) - 1] = 0;
932 if (skip_line(pline))
935 (*ap = strsep(&pline, " \t")) != NULL;)
936 if (**ap != '\0' && ++ap >= &argv[6])
939 label = pline = NULL;
940 if (l == 1 || l == 2) { /* create table */
942 errx(EXIT_FAILURE, "Incorrect backup format.");
944 n = strtoimax(argv[1], NULL, 0);
945 for (i = 0; i < nargs; i++) {
946 s = gctl_get_ascii(req, "arg%d", i);
947 r = gctl_get_handle();
948 gctl_ro_param(r, "class", -1,
950 gctl_ro_param(r, "verb", -1, "create");
951 gctl_ro_param(r, "scheme", -1, argv[0]);
953 gctl_ro_param(r, "entries",
955 gctl_ro_param(r, "flags", -1, "restore");
956 gctl_ro_param(r, "arg0", -1, s);
957 errstr = gctl_issue(r);
958 if (errstr != NULL && errstr[0] != '\0') {
959 gpart_print_error(errstr);
967 } else if (l < 4 || created == 0)
968 errx(EXIT_FAILURE, "Incorrect backup format.");
970 if (strchr(argv[4], '[') == NULL)
978 /* Add partitions to each table */
979 for (i = 0; i < nargs; i++) {
980 s = gctl_get_ascii(req, "arg%d", i);
981 r = gctl_get_handle();
982 n = strtoimax(argv[0], NULL, 0);
983 gctl_ro_param(r, "class", -1, classp->lg_name);
984 gctl_ro_param(r, "verb", -1, "add");
985 gctl_ro_param(r, "flags", -1, "restore");
986 gctl_ro_param(r, GPART_PARAM_INDEX, sizeof(n), &n);
987 gctl_ro_param(r, "type", -1, argv[1]);
988 gctl_ro_param(r, "start", -1, argv[2]);
989 gctl_ro_param(r, "size", -1, argv[3]);
990 if (rl != 0 && label != NULL)
991 gctl_ro_param(r, "label", -1, argv[4]);
992 gctl_ro_param(r, "alignment", -1, GPART_AUTOFILL);
993 gctl_ro_param(r, "arg0", -1, s);
994 error = gpart_autofill(r);
996 errc(EXIT_FAILURE, error, "autofill");
997 errstr = gctl_issue(r);
998 if (errstr != NULL && errstr[0] != '\0') {
999 gpart_print_error(errstr);
1005 if (pline == NULL || *pline != '[')
1007 /* set attributes */
1010 (*ap = strsep(&pline, ",]")) != NULL;)
1011 if (**ap != '\0' && ++ap >= &argv[6])
1013 for (i = 0; i < nargs; i++) {
1015 s = gctl_get_ascii(req, "arg%d", i);
1017 r = gctl_get_handle();
1018 gctl_ro_param(r, "class", -1, classp->lg_name);
1019 gctl_ro_param(r, "verb", -1, "set");
1020 gctl_ro_param(r, "flags", -1, "restore");
1021 gctl_ro_param(r, GPART_PARAM_INDEX,
1023 gctl_ro_param(r, "attrib", -1, argv[--l]);
1024 gctl_ro_param(r, "arg0", -1, s);
1025 errstr = gctl_issue(r);
1026 if (errstr != NULL && errstr[0] != '\0') {
1027 gpart_print_error(errstr);
1037 /* commit changes if needed */
1038 if (strchr(flags, 'C') != NULL) {
1039 for (i = 0; i < nargs; i++) {
1040 s = gctl_get_ascii(req, "arg%d", i);
1041 r = gctl_get_handle();
1042 gctl_ro_param(r, "class", -1, classp->lg_name);
1043 gctl_ro_param(r, "verb", -1, "commit");
1044 gctl_ro_param(r, "arg0", -1, s);
1045 errstr = gctl_issue(r);
1046 if (errstr != NULL && errstr[0] != '\0') {
1047 gpart_print_error(errstr);
1055 geom_deletetree(&mesh);
1059 for (i = 0; i < nargs; i++) {
1060 s = gctl_get_ascii(req, "arg%d", i);
1061 r = gctl_get_handle();
1062 gctl_ro_param(r, "class", -1, classp->lg_name);
1063 gctl_ro_param(r, "verb", -1, "undo");
1064 gctl_ro_param(r, "arg0", -1, s);
1069 geom_deletetree(&mesh);
1074 gpart_bootfile_read(const char *bootfile, ssize_t *size)
1080 if (stat(bootfile, &sb) == -1)
1081 err(EXIT_FAILURE, "%s", bootfile);
1082 if (!S_ISREG(sb.st_mode))
1083 errx(EXIT_FAILURE, "%s: not a regular file", bootfile);
1084 if (sb.st_size == 0)
1085 errx(EXIT_FAILURE, "%s: empty file", bootfile);
1086 if (*size > 0 && sb.st_size > *size)
1087 errx(EXIT_FAILURE, "%s: file too big (%zu limit)", bootfile,
1092 fd = open(bootfile, O_RDONLY);
1094 err(EXIT_FAILURE, "%s", bootfile);
1095 code = malloc(*size);
1097 err(EXIT_FAILURE, NULL);
1098 if (read(fd, code, *size) != *size)
1099 err(EXIT_FAILURE, "%s", bootfile);
1106 gpart_write_partcode(struct gctl_req *req, int idx, void *code, ssize_t size)
1110 struct gclass *classp;
1112 struct gprovider *pp;
1118 s = gctl_get_ascii(req, "class");
1121 g = gctl_get_ascii(req, "arg0");
1124 error = geom_gettree_geom(&mesh, s, g, 0);
1126 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
1127 classp = find_class(&mesh, s);
1128 if (classp == NULL) {
1129 geom_deletetree(&mesh);
1130 errx(EXIT_FAILURE, "Class %s not found.", s);
1132 gp = find_geom(classp, g);
1134 errx(EXIT_FAILURE, "No such geom: %s.", g);
1135 s = find_geomcfg(gp, "scheme");
1137 errx(EXIT_FAILURE, "Scheme not found for geom %s", gp->lg_name);
1139 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1140 s = find_provcfg(pp, "index");
1148 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1149 if (pp->lg_mediasize < size)
1150 errx(EXIT_FAILURE, "%s: not enough space", dsf);
1151 fd = open(dsf, O_WRONLY);
1153 err(EXIT_FAILURE, "%s", dsf);
1155 * When writing to a disk device, the write must be
1156 * sector aligned and not write to any partial sectors,
1157 * so round up the buffer size to the next sector and zero it.
1159 bsize = (size + pp->lg_sectorsize - 1) /
1160 pp->lg_sectorsize * pp->lg_sectorsize;
1161 buf = calloc(1, bsize);
1163 err(EXIT_FAILURE, "%s", dsf);
1164 bcopy(code, buf, size);
1165 if (write(fd, buf, bsize) != bsize)
1166 err(EXIT_FAILURE, "%s", dsf);
1169 printf("partcode written to %s\n", pp->lg_name);
1171 errx(EXIT_FAILURE, "invalid partition index");
1173 geom_deletetree(&mesh);
1177 gpart_bootcode(struct gctl_req *req, unsigned int fl)
1180 void *bootcode, *partcode;
1181 size_t bootsize, partsize;
1184 if (gctl_get_int(req, "nargs") != 1)
1185 errx(EXIT_FAILURE, "Invalid number of arguments.");
1187 if (gctl_has_param(req, GPART_PARAM_BOOTCODE)) {
1188 s = gctl_get_ascii(req, GPART_PARAM_BOOTCODE);
1189 bootsize = 800 * 1024; /* Arbitrary limit. */
1190 bootcode = gpart_bootfile_read(s, &bootsize);
1191 error = gctl_change_param(req, GPART_PARAM_BOOTCODE, bootsize,
1194 errc(EXIT_FAILURE, error, "internal error");
1198 if (!gctl_has_param(req, GPART_PARAM_PARTCODE)) {
1199 if (bootcode == NULL)
1200 errx(EXIT_FAILURE, "neither -b nor -p specified");
1201 if (gctl_has_param(req, GPART_PARAM_INDEX))
1202 errx(EXIT_FAILURE, "-i is only valid with -p");
1206 if (gctl_has_param(req, GPART_PARAM_INDEX)) {
1207 idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
1209 errx(EXIT_FAILURE, "invalid partition index");
1210 error = gctl_delete_param(req, GPART_PARAM_INDEX);
1212 errc(EXIT_FAILURE, error, "internal error");
1216 if (gctl_has_param(req, GPART_PARAM_PARTCODE)) {
1217 s = gctl_get_ascii(req, GPART_PARAM_PARTCODE);
1218 partsize = 1024 * 1024; /* Arbitrary limit. */
1219 partcode = gpart_bootfile_read(s, &partsize);
1220 error = gctl_delete_param(req, GPART_PARAM_PARTCODE);
1222 errc(EXIT_FAILURE, error, "internal error");
1224 errx(EXIT_FAILURE, "missing -i option");
1225 gpart_write_partcode(req, idx, partcode, partsize);
1230 if (bootcode != NULL)
1231 gpart_issue(req, fl);
1235 gpart_print_error(const char *errstr)
1240 error = strtol(errstr, &errmsg, 0);
1241 if (errmsg != errstr) {
1242 while (errmsg[0] == ' ')
1244 if (errmsg[0] != '\0')
1245 warnc(error, "%s", errmsg);
1249 warnx("%s", errmsg);
1252 static _Noreturn void
1253 gpart_issue(struct gctl_req *req, unsigned int fl __unused)
1259 if (gctl_get_int(req, "nargs") != 1)
1260 errx(EXIT_FAILURE, "Invalid number of arguments.");
1261 (void)gctl_delete_param(req, "nargs");
1263 /* autofill parameters (if applicable). */
1264 error = gpart_autofill(req);
1266 warnc(error, "autofill");
1267 status = EXIT_FAILURE;
1272 gctl_add_param(req, "output", sizeof(buf), buf,
1273 GCTL_PARAM_WR | GCTL_PARAM_ASCII);
1274 errstr = gctl_issue(req);
1275 if (errstr == NULL || errstr[0] == '\0') {
1278 status = EXIT_SUCCESS;
1282 gpart_print_error(errstr);
1283 status = EXIT_FAILURE;