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"
75 #define GPART_PARAM_SKIP_DSN "skip_dsn"
77 static struct gclass *find_class(struct gmesh *, const char *);
78 static struct ggeom * find_geom(struct gclass *, const char *);
79 static int geom_is_withered(struct ggeom *);
80 static const char *find_geomcfg(struct ggeom *, const char *);
81 static const char *find_provcfg(struct gprovider *, const char *);
82 static struct gprovider *find_provider(struct ggeom *, off_t);
83 static const char *fmtsize(int64_t);
84 static int gpart_autofill(struct gctl_req *);
85 static int gpart_autofill_resize(struct gctl_req *);
86 static void gpart_bootcode(struct gctl_req *, unsigned int);
87 static void *gpart_bootfile_read(const char *, ssize_t *);
88 static _Noreturn void gpart_issue(struct gctl_req *, unsigned int);
89 static void gpart_show(struct gctl_req *, unsigned int);
90 static void gpart_show_geom(struct ggeom *, const char *, int);
91 static int gpart_show_hasopt(struct gctl_req *, const char *, const char *);
92 static void gpart_write_partcode(struct ggeom *, int, void *, ssize_t);
93 static void gpart_write_partcode_vtoc8(struct ggeom *, int, void *);
94 static void gpart_print_error(const char *);
95 static void gpart_backup(struct gctl_req *, unsigned int);
96 static void gpart_restore(struct gctl_req *, unsigned int);
98 struct g_command PUBSYM(class_commands)[] = {
99 { "add", 0, gpart_issue, {
100 { 'a', "alignment", GPART_AUTOFILL, G_TYPE_STRING },
101 { 'b', "start", GPART_AUTOFILL, G_TYPE_STRING },
102 { 's', "size", GPART_AUTOFILL, G_TYPE_STRING },
103 { 't', "type", NULL, G_TYPE_STRING },
104 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
105 { 'l', "label", G_VAL_OPTIONAL, G_TYPE_STRING },
106 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
108 "-t type [-a alignment] [-b start] [-s size] [-i index] "
109 "[-l label] [-f flags] geom"
111 { "backup", 0, gpart_backup, G_NULL_OPTS,
114 { "bootcode", 0, gpart_bootcode, {
115 { 'b', GPART_PARAM_BOOTCODE, G_VAL_OPTIONAL, G_TYPE_STRING },
116 { 'p', GPART_PARAM_PARTCODE, G_VAL_OPTIONAL, G_TYPE_STRING },
117 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
118 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
119 { 'N', GPART_PARAM_SKIP_DSN, NULL, G_TYPE_BOOL },
121 "[-N] [-b bootcode] [-p partcode -i index] [-f flags] geom"
123 { "commit", 0, gpart_issue, G_NULL_OPTS,
126 { "create", 0, gpart_issue, {
127 { 's', "scheme", NULL, G_TYPE_STRING },
128 { 'n', "entries", G_VAL_OPTIONAL, G_TYPE_NUMBER },
129 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
131 "-s scheme [-n entries] [-f flags] provider"
133 { "delete", 0, gpart_issue, {
134 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
135 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
137 "-i index [-f flags] geom"
139 { "destroy", 0, gpart_issue, {
140 { 'F', "force", NULL, G_TYPE_BOOL },
141 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
143 "[-F] [-f flags] geom"
145 { "modify", 0, gpart_issue, {
146 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
147 { 'l', "label", G_VAL_OPTIONAL, G_TYPE_STRING },
148 { 't', "type", G_VAL_OPTIONAL, G_TYPE_STRING },
149 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
151 "-i index [-l label] [-t type] [-f flags] geom"
153 { "set", 0, gpart_issue, {
154 { 'a', "attrib", NULL, G_TYPE_STRING },
155 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
156 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
158 "-a attrib [-i index] [-f flags] geom"
160 { "show", 0, gpart_show, {
161 { 'l', "show_label", NULL, G_TYPE_BOOL },
162 { 'r', "show_rawtype", NULL, G_TYPE_BOOL },
163 { 'p', "show_providers", NULL, G_TYPE_BOOL },
165 "[-l | -r] [-p] [geom ...]"
167 { "undo", 0, gpart_issue, G_NULL_OPTS,
170 { "unset", 0, gpart_issue, {
171 { 'a', "attrib", NULL, G_TYPE_STRING },
172 { 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
173 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
175 "-a attrib [-i index] [-f flags] geom"
177 { "resize", 0, gpart_issue, {
178 { 'a', "alignment", GPART_AUTOFILL, G_TYPE_STRING },
179 { 's', "size", GPART_AUTOFILL, G_TYPE_STRING },
180 { 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
181 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
183 "-i index [-a alignment] [-s size] [-f flags] geom"
185 { "restore", 0, gpart_restore, {
186 { 'F', "force", NULL, G_TYPE_BOOL },
187 { 'l', "restore_labels", NULL, G_TYPE_BOOL },
188 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
190 "[-lF] [-f flags] provider [...]"
192 { "recover", 0, gpart_issue, {
193 { 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
200 static struct gclass *
201 find_class(struct gmesh *mesh, const char *name)
203 struct gclass *classp;
205 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
206 if (strcmp(classp->lg_name, name) == 0)
212 static struct ggeom *
213 find_geom(struct gclass *classp, const char *name)
215 struct ggeom *gp, *wgp;
217 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
218 name += sizeof(_PATH_DEV) - 1;
220 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
221 if (strcmp(gp->lg_name, name) != 0)
223 if (!geom_is_withered(gp))
232 geom_is_withered(struct ggeom *gp)
236 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
237 if (!strcmp(gc->lg_name, "wither"))
244 find_geomcfg(struct ggeom *gp, const char *cfg)
248 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
249 if (!strcmp(gc->lg_name, cfg))
256 find_provcfg(struct gprovider *pp, const char *cfg)
260 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
261 if (!strcmp(gc->lg_name, cfg))
267 static struct gprovider *
268 find_provider(struct ggeom *gp, off_t minsector)
270 struct gprovider *pp, *bestpp;
272 off_t sector, bestsector;
276 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
277 s = find_provcfg(pp, "start");
278 sector = (off_t)strtoimax(s, NULL, 0);
279 if (sector < minsector)
281 if (bestpp != NULL && sector >= bestsector)
291 fmtsize(int64_t rawsz)
295 humanize_number(buf, sizeof(buf), rawsz, "", HN_AUTOSCALE,
296 HN_B | HN_NOSPACE | HN_DECIMAL);
301 fmtattrib(struct gprovider *pp)
303 static char buf[128];
309 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
310 if (strcmp(gc->lg_name, "attrib") != 0)
312 idx += snprintf(buf + idx, sizeof(buf) - idx, "%s%s",
313 (idx == 0) ? " [" : ",", gc->lg_val);
316 snprintf(buf + idx, sizeof(buf) - idx, "] ");
320 #define ALIGNDOWN(d, a) ((d) - (d) % (a))
321 #define ALIGNUP(d, a) ((d) % (a) ? (d) - (d) % (a) + (a): (d))
324 gpart_autofill_resize(struct gctl_req *req)
329 struct gprovider *pp;
330 off_t last, size, start, new_size;
331 off_t lba, new_lba, alignment, offset;
333 int error, idx, has_alignment;
335 idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
337 errx(EXIT_FAILURE, "invalid partition index");
339 error = geom_gettree(&mesh);
342 s = gctl_get_ascii(req, "class");
345 cp = find_class(&mesh, s);
347 errx(EXIT_FAILURE, "Class %s not found.", s);
348 s = gctl_get_ascii(req, "arg0");
351 gp = find_geom(cp, s);
353 errx(EXIT_FAILURE, "No such geom: %s.", s);
354 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
356 errx(EXIT_FAILURE, "Provider for geom %s not found.", s);
358 s = gctl_get_ascii(req, "alignment");
359 has_alignment = (*s == '*') ? 0 : 1;
362 error = g_parse_lba(s, pp->lg_sectorsize, &alignment);
364 errc(EXIT_FAILURE, error, "Invalid alignment param");
366 errx(EXIT_FAILURE, "Invalid alignment param");
368 lba = pp->lg_stripesize / pp->lg_sectorsize;
372 error = gctl_delete_param(req, "alignment");
374 errc(EXIT_FAILURE, error, "internal error");
376 s = gctl_get_ascii(req, "size");
380 error = g_parse_lba(s, pp->lg_sectorsize, &new_size);
382 errc(EXIT_FAILURE, error, "Invalid size param");
383 /* no autofill necessary. */
384 if (has_alignment == 0)
388 offset = (pp->lg_stripeoffset / pp->lg_sectorsize) % alignment;
389 s = find_geomcfg(gp, "last");
391 errx(EXIT_FAILURE, "Final block not found for geom %s",
393 last = (off_t)strtoimax(s, NULL, 0);
394 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
395 s = find_provcfg(pp, "index");
402 errx(EXIT_FAILURE, "invalid partition index");
404 s = find_provcfg(pp, "start");
405 start = (off_t)strtoimax(s, NULL, 0);
406 s = find_provcfg(pp, "end");
407 lba = (off_t)strtoimax(s, NULL, 0);
408 size = lba - start + 1;
410 pp = find_provider(gp, lba + 1);
411 if (new_size > 0 && (new_size <= size || pp == NULL)) {
412 /* The start offset may be not aligned, so we align the end
413 * offset and then calculate the size.
415 new_size = ALIGNDOWN(start + offset + new_size,
416 alignment) - start - offset;
420 new_size = ALIGNDOWN(last + offset + 1, alignment) -
425 s = find_provcfg(pp, "start");
426 new_lba = (off_t)strtoimax(s, NULL, 0);
428 * Is there any free space between current and
431 new_lba = ALIGNDOWN(new_lba + offset, alignment) - offset;
433 new_size = new_lba - start;
435 geom_deletetree(&mesh);
440 snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)new_size);
441 gctl_change_param(req, "size", -1, ssize);
442 geom_deletetree(&mesh);
447 gpart_autofill(struct gctl_req *req)
452 struct gprovider *pp;
453 off_t first, last, a_first;
454 off_t size, start, a_lba;
455 off_t lba, len, alignment, offset;
458 int error, has_size, has_start, has_alignment;
460 s = gctl_get_ascii(req, "verb");
461 if (strcmp(s, "resize") == 0)
462 return gpart_autofill_resize(req);
463 if (strcmp(s, "add") != 0)
466 error = geom_gettree(&mesh);
469 s = gctl_get_ascii(req, "class");
472 cp = find_class(&mesh, s);
474 errx(EXIT_FAILURE, "Class %s not found.", s);
475 s = gctl_get_ascii(req, "arg0");
478 gp = find_geom(cp, s);
480 if (g_device_path(s) == NULL) {
481 errx(EXIT_FAILURE, "No such geom %s.", s);
484 * We don't free memory allocated by g_device_path() as
485 * we are about to exit.
488 "No partitioning scheme found on geom %s. Create one first using 'gpart create'.",
492 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
494 errx(EXIT_FAILURE, "Provider for geom %s not found.", s);
496 s = gctl_get_ascii(req, "alignment");
497 has_alignment = (*s == '*') ? 0 : 1;
500 error = g_parse_lba(s, pp->lg_sectorsize, &alignment);
502 errc(EXIT_FAILURE, error, "Invalid alignment param");
504 errx(EXIT_FAILURE, "Invalid alignment param");
506 error = gctl_delete_param(req, "alignment");
508 errc(EXIT_FAILURE, error, "internal error");
510 s = gctl_get_ascii(req, "size");
511 has_size = (*s == '*') ? 0 : 1;
514 error = g_parse_lba(s, pp->lg_sectorsize, &size);
516 errc(EXIT_FAILURE, error, "Invalid size param");
519 s = gctl_get_ascii(req, "start");
520 has_start = (*s == '*') ? 0 : 1;
523 error = g_parse_lba(s, pp->lg_sectorsize, &start);
525 errc(EXIT_FAILURE, error, "Invalid start param");
528 /* No autofill necessary. */
529 if (has_size && has_start && !has_alignment)
532 len = pp->lg_stripesize / pp->lg_sectorsize;
533 if (len > 0 && !has_alignment)
536 /* Adjust parameters to stripeoffset */
537 offset = (pp->lg_stripeoffset / pp->lg_sectorsize) % alignment;
538 start = ALIGNUP(start + offset, alignment);
539 if (size > alignment)
540 size = ALIGNDOWN(size, alignment);
542 s = find_geomcfg(gp, "first");
544 errx(EXIT_FAILURE, "Starting block not found for geom %s",
546 first = (off_t)strtoimax(s, NULL, 0);
547 s = find_geomcfg(gp, "last");
549 errx(EXIT_FAILURE, "Final block not found for geom %s",
551 last = (off_t)strtoimax(s, NULL, 0);
553 a_first = ALIGNUP(first + offset, alignment);
554 last = ALIGNDOWN(last + offset + 1, alignment) - 1;
557 while ((pp = find_provider(gp, first)) != NULL) {
558 s = find_provcfg(pp, "start");
559 lba = (off_t)strtoimax(s, NULL, 0);
560 a_lba = ALIGNDOWN(lba + offset, alignment);
561 if (first < a_lba && a_first < a_lba) {
562 /* Free space [first, lba> */
563 len = a_lba - a_first;
566 (uintmax_t)(len - size) < grade) {
570 } else if (has_start) {
571 if (start >= a_first && start < a_lba) {
572 size = a_lba - start;
573 grade = start - a_first;
576 if (grade == ~0ULL || len > size) {
584 s = find_provcfg(pp, "end");
585 first = (off_t)strtoimax(s, NULL, 0) + 1;
586 if (first + offset > a_first)
587 a_first = ALIGNUP(first + offset, alignment);
589 if (a_first <= last) {
590 /* Free space [first-last] */
591 len = ALIGNDOWN(last - a_first + 1, alignment);
594 (uintmax_t)(len - size) < grade) {
598 } else if (has_start) {
599 if (start >= a_first && start <= last) {
600 size = ALIGNDOWN(last - start + 1, alignment);
601 grade = start - a_first;
604 if (grade == ~0ULL || len > size) {
611 if (grade == ~0ULL) {
612 geom_deletetree(&mesh);
615 start -= offset; /* Return back to real offset */
617 snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)size);
618 gctl_change_param(req, "size", -1, ssize);
619 snprintf(sstart, sizeof(sstart), "%jd", (intmax_t)start);
620 gctl_change_param(req, "start", -1, sstart);
621 geom_deletetree(&mesh);
626 gpart_show_geom(struct ggeom *gp, const char *element, int show_providers)
628 struct gprovider *pp;
629 const char *s, *scheme;
630 off_t first, last, sector, end;
632 int idx, wblocks, wname, wmax;
634 if (geom_is_withered(gp))
636 scheme = find_geomcfg(gp, "scheme");
638 errx(EXIT_FAILURE, "Scheme not found for geom %s", gp->lg_name);
639 s = find_geomcfg(gp, "first");
641 errx(EXIT_FAILURE, "Starting block not found for geom %s",
643 first = (off_t)strtoimax(s, NULL, 0);
644 s = find_geomcfg(gp, "last");
646 errx(EXIT_FAILURE, "Final block not found for geom %s",
648 last = (off_t)strtoimax(s, NULL, 0);
650 s = find_geomcfg(gp, "state");
652 errx(EXIT_FAILURE, "State not found for geom %s", gp->lg_name);
653 if (s != NULL && *s != 'C')
655 wmax = strlen(gp->lg_name);
656 if (show_providers) {
657 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
658 wname = strlen(pp->lg_name);
664 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
665 secsz = pp->lg_sectorsize;
666 printf("=>%*jd %*jd %*s %s (%s)%s\n",
667 wblocks, (intmax_t)first, wblocks, (intmax_t)(last - first + 1),
669 scheme, fmtsize(pp->lg_mediasize),
670 s ? " [CORRUPT]": "");
672 while ((pp = find_provider(gp, first)) != NULL) {
673 s = find_provcfg(pp, "start");
674 sector = (off_t)strtoimax(s, NULL, 0);
676 s = find_provcfg(pp, "end");
677 end = (off_t)strtoimax(s, NULL, 0);
678 length = end - sector + 1;
680 s = find_provcfg(pp, "index");
682 if (first < sector) {
683 printf(" %*jd %*jd %*s - free - (%s)\n",
684 wblocks, (intmax_t)first, wblocks,
685 (intmax_t)(sector - first), wname, "",
686 fmtsize((sector - first) * secsz));
688 if (show_providers) {
689 printf(" %*jd %*jd %*s %s %s (%s)\n",
690 wblocks, (intmax_t)sector, wblocks,
691 (intmax_t)length, wname, pp->lg_name,
692 find_provcfg(pp, element), fmtattrib(pp),
693 fmtsize(pp->lg_mediasize));
695 printf(" %*jd %*jd %*d %s %s (%s)\n",
696 wblocks, (intmax_t)sector, wblocks,
697 (intmax_t)length, wname, idx,
698 find_provcfg(pp, element), fmtattrib(pp),
699 fmtsize(pp->lg_mediasize));
703 length = last - first + 1;
704 printf(" %*jd %*jd %*s - free - (%s)\n",
705 wblocks, (intmax_t)first, wblocks, (intmax_t)length,
707 fmtsize(length * secsz));
713 gpart_show_hasopt(struct gctl_req *req, const char *opt, const char *elt)
716 if (!gctl_get_int(req, "%s", opt))
720 errx(EXIT_FAILURE, "-l and -r are mutually exclusive");
726 gpart_show(struct gctl_req *req, unsigned int fl __unused)
729 struct gclass *classp;
731 const char *element, *name;
732 int error, i, nargs, show_providers;
735 if (gpart_show_hasopt(req, "show_label", element))
737 if (gpart_show_hasopt(req, "show_rawtype", element))
742 name = gctl_get_ascii(req, "class");
745 error = geom_gettree(&mesh);
747 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
748 classp = find_class(&mesh, name);
749 if (classp == NULL) {
750 geom_deletetree(&mesh);
751 errx(EXIT_FAILURE, "Class %s not found.", name);
753 show_providers = gctl_get_int(req, "show_providers");
754 nargs = gctl_get_int(req, "nargs");
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 *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 error = geom_gettree(&mesh);
788 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
789 s = gctl_get_ascii(req, "class");
792 classp = find_class(&mesh, s);
793 if (classp == NULL) {
794 geom_deletetree(&mesh);
795 errx(EXIT_FAILURE, "Class %s not found.", s);
797 s = gctl_get_ascii(req, "arg0");
800 gp = find_geom(classp, s);
802 errx(EXIT_FAILURE, "No such geom: %s.", s);
803 scheme = find_geomcfg(gp, "scheme");
806 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
807 s = find_geomcfg(gp, "last");
812 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
813 s = find_provcfg(pp, "type");
818 s = find_geomcfg(gp, "entries");
822 printf("%s %s\n", scheme, s);
823 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
824 s = find_provcfg(pp, "start");
825 sector = (off_t)strtoimax(s, NULL, 0);
827 s = find_provcfg(pp, "end");
828 end = (off_t)strtoimax(s, NULL, 0);
829 length = end - sector + 1;
831 s = find_provcfg(pp, "label");
832 printf("%-*s %*s %*jd %*jd %s %s\n",
833 windex, find_provcfg(pp, "index"),
834 wtype, find_provcfg(pp, "type"),
835 wblocks, (intmax_t)sector,
836 wblocks, (intmax_t)length,
837 (s != NULL) ? s: "", fmtattrib(pp));
839 geom_deletetree(&mesh);
843 skip_line(const char *p)
849 if (isspace(*p) == 0)
857 gpart_sighndl(int sig __unused)
863 gpart_restore(struct gctl_req *req, unsigned int fl __unused)
866 struct gclass *classp;
869 struct sigaction si_sa;
870 const char *s, *flags, *errstr, *label;
871 char **ap, *argv[6], line[BUFSIZ], *pline;
872 int error, forced, i, l, nargs, created, rl;
875 nargs = gctl_get_int(req, "nargs");
877 errx(EXIT_FAILURE, "Invalid number of arguments.");
879 forced = gctl_get_int(req, "force");
880 flags = gctl_get_ascii(req, "flags");
881 rl = gctl_get_int(req, "restore_labels");
882 s = gctl_get_ascii(req, "class");
885 error = geom_gettree(&mesh);
887 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
888 classp = find_class(&mesh, s);
889 if (classp == NULL) {
890 geom_deletetree(&mesh);
891 errx(EXIT_FAILURE, "Class %s not found.", s);
894 sigemptyset(&si_sa.sa_mask);
896 si_sa.sa_handler = gpart_sighndl;
897 if (sigaction(SIGINT, &si_sa, 0) == -1)
898 err(EXIT_FAILURE, "sigaction SIGINT");
901 /* destroy existent partition table before restore */
902 for (i = 0; i < nargs; i++) {
903 s = gctl_get_ascii(req, "arg%d", i);
904 gp = find_geom(classp, s);
906 r = gctl_get_handle();
907 gctl_ro_param(r, "class", -1,
909 gctl_ro_param(r, "verb", -1, "destroy");
910 gctl_ro_param(r, "flags", -1, "restore");
911 gctl_ro_param(r, "force", sizeof(forced),
913 gctl_ro_param(r, "arg0", -1, s);
914 errstr = gctl_issue(r);
915 if (errstr != NULL && errstr[0] != '\0') {
916 gpart_print_error(errstr);
925 while (undo_restore == 0 &&
926 fgets(line, sizeof(line) - 1, stdin) != NULL) {
927 /* Format of backup entries:
928 * <scheme name> <number of entries>
929 * <index> <type> <start> <size> [label] ['['attrib[,attrib]']']
931 pline = (char *)line;
932 pline[strlen(line) - 1] = 0;
933 if (skip_line(pline))
936 (*ap = strsep(&pline, " \t")) != NULL;)
937 if (**ap != '\0' && ++ap >= &argv[6])
940 label = pline = NULL;
941 if (l == 1 || l == 2) { /* create table */
943 errx(EXIT_FAILURE, "Incorrect backup format.");
945 n = strtoimax(argv[1], NULL, 0);
946 for (i = 0; i < nargs; i++) {
947 s = gctl_get_ascii(req, "arg%d", i);
948 r = gctl_get_handle();
949 gctl_ro_param(r, "class", -1,
951 gctl_ro_param(r, "verb", -1, "create");
952 gctl_ro_param(r, "scheme", -1, argv[0]);
954 gctl_ro_param(r, "entries",
956 gctl_ro_param(r, "flags", -1, "restore");
957 gctl_ro_param(r, "arg0", -1, s);
958 errstr = gctl_issue(r);
959 if (errstr != NULL && errstr[0] != '\0') {
960 gpart_print_error(errstr);
968 } else if (l < 4 || created == 0)
969 errx(EXIT_FAILURE, "Incorrect backup format.");
971 if (strchr(argv[4], '[') == NULL)
979 /* Add partitions to each table */
980 for (i = 0; i < nargs; i++) {
981 s = gctl_get_ascii(req, "arg%d", i);
982 r = gctl_get_handle();
983 n = strtoimax(argv[0], NULL, 0);
984 gctl_ro_param(r, "class", -1, classp->lg_name);
985 gctl_ro_param(r, "verb", -1, "add");
986 gctl_ro_param(r, "flags", -1, "restore");
987 gctl_ro_param(r, GPART_PARAM_INDEX, sizeof(n), &n);
988 gctl_ro_param(r, "type", -1, argv[1]);
989 gctl_ro_param(r, "start", -1, argv[2]);
990 gctl_ro_param(r, "size", -1, argv[3]);
991 if (rl != 0 && label != NULL)
992 gctl_ro_param(r, "label", -1, argv[4]);
993 gctl_ro_param(r, "alignment", -1, GPART_AUTOFILL);
994 gctl_ro_param(r, "arg0", -1, s);
995 error = gpart_autofill(r);
997 errc(EXIT_FAILURE, error, "autofill");
998 errstr = gctl_issue(r);
999 if (errstr != NULL && errstr[0] != '\0') {
1000 gpart_print_error(errstr);
1006 if (pline == NULL || *pline != '[')
1008 /* set attributes */
1011 (*ap = strsep(&pline, ",]")) != NULL;)
1012 if (**ap != '\0' && ++ap >= &argv[6])
1014 for (i = 0; i < nargs; i++) {
1016 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, "set");
1021 gctl_ro_param(r, "flags", -1, "restore");
1022 gctl_ro_param(r, GPART_PARAM_INDEX,
1024 gctl_ro_param(r, "attrib", -1, argv[--l]);
1025 gctl_ro_param(r, "arg0", -1, s);
1026 errstr = gctl_issue(r);
1027 if (errstr != NULL && errstr[0] != '\0') {
1028 gpart_print_error(errstr);
1038 /* commit changes if needed */
1039 if (strchr(flags, 'C') != NULL) {
1040 for (i = 0; i < nargs; i++) {
1041 s = gctl_get_ascii(req, "arg%d", i);
1042 r = gctl_get_handle();
1043 gctl_ro_param(r, "class", -1, classp->lg_name);
1044 gctl_ro_param(r, "verb", -1, "commit");
1045 gctl_ro_param(r, "arg0", -1, s);
1046 errstr = gctl_issue(r);
1047 if (errstr != NULL && errstr[0] != '\0') {
1048 gpart_print_error(errstr);
1056 geom_deletetree(&mesh);
1060 for (i = 0; i < nargs; i++) {
1061 s = gctl_get_ascii(req, "arg%d", i);
1062 r = gctl_get_handle();
1063 gctl_ro_param(r, "class", -1, classp->lg_name);
1064 gctl_ro_param(r, "verb", -1, "undo");
1065 gctl_ro_param(r, "arg0", -1, s);
1070 geom_deletetree(&mesh);
1075 gpart_bootfile_read(const char *bootfile, ssize_t *size)
1081 if (stat(bootfile, &sb) == -1)
1082 err(EXIT_FAILURE, "%s", bootfile);
1083 if (!S_ISREG(sb.st_mode))
1084 errx(EXIT_FAILURE, "%s: not a regular file", bootfile);
1085 if (sb.st_size == 0)
1086 errx(EXIT_FAILURE, "%s: empty file", bootfile);
1087 if (*size > 0 && sb.st_size > *size)
1088 errx(EXIT_FAILURE, "%s: file too big (%zu limit)", bootfile,
1093 fd = open(bootfile, O_RDONLY);
1095 err(EXIT_FAILURE, "%s", bootfile);
1096 code = malloc(*size);
1098 err(EXIT_FAILURE, NULL);
1099 if (read(fd, code, *size) != *size)
1100 err(EXIT_FAILURE, "%s", bootfile);
1107 gpart_write_partcode(struct ggeom *gp, int idx, void *code, ssize_t size)
1110 struct gprovider *pp;
1116 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1117 s = find_provcfg(pp, "index");
1125 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1126 if (pp->lg_mediasize < size)
1127 errx(EXIT_FAILURE, "%s: not enough space", dsf);
1128 fd = open(dsf, O_WRONLY);
1130 err(EXIT_FAILURE, "%s", dsf);
1132 * When writing to a disk device, the write must be
1133 * sector aligned and not write to any partial sectors,
1134 * so round up the buffer size to the next sector and zero it.
1136 bsize = (size + pp->lg_sectorsize - 1) /
1137 pp->lg_sectorsize * pp->lg_sectorsize;
1138 buf = calloc(1, bsize);
1140 err(EXIT_FAILURE, "%s", dsf);
1141 bcopy(code, buf, size);
1142 if (write(fd, buf, bsize) != bsize)
1143 err(EXIT_FAILURE, "%s", dsf);
1146 printf("partcode written to %s\n", pp->lg_name);
1148 errx(EXIT_FAILURE, "invalid partition index");
1152 gpart_write_partcode_vtoc8(struct ggeom *gp, int idx, void *code)
1155 struct gprovider *pp;
1160 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1161 s = find_provcfg(pp, "index");
1164 if (idx != 0 && atoi(s) != idx)
1166 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1167 if (pp->lg_sectorsize != sizeof(struct vtoc8))
1168 errx(EXIT_FAILURE, "%s: unexpected sector "
1169 "size (%d)\n", dsf, pp->lg_sectorsize);
1170 if (pp->lg_mediasize < VTOC_BOOTSIZE)
1172 fd = open(dsf, O_WRONLY);
1174 err(EXIT_FAILURE, "%s", dsf);
1176 * We ignore the first VTOC_BOOTSIZE bytes of boot code in
1177 * order to avoid overwriting the label.
1179 if (lseek(fd, sizeof(struct vtoc8), SEEK_SET) !=
1180 sizeof(struct vtoc8))
1181 err(EXIT_FAILURE, "%s", dsf);
1182 if (write(fd, (caddr_t)code + sizeof(struct vtoc8),
1183 VTOC_BOOTSIZE - sizeof(struct vtoc8)) != VTOC_BOOTSIZE -
1184 sizeof(struct vtoc8))
1185 err(EXIT_FAILURE, "%s", dsf);
1188 if (idx != 0 && atoi(s) == idx)
1192 errx(EXIT_FAILURE, "%s: no partitions", gp->lg_name);
1194 printf("partcode written to %s\n",
1195 idx != 0 ? pp->lg_name: gp->lg_name);
1199 gpart_bootcode(struct gctl_req *req, unsigned int fl)
1202 struct gclass *classp;
1205 void *bootcode, *partcode;
1206 size_t bootsize, partsize;
1207 int error, idx, vtoc8;
1209 if (gctl_has_param(req, GPART_PARAM_BOOTCODE)) {
1210 s = gctl_get_ascii(req, GPART_PARAM_BOOTCODE);
1211 bootsize = 800 * 1024; /* Arbitrary limit. */
1212 bootcode = gpart_bootfile_read(s, &bootsize);
1213 error = gctl_change_param(req, GPART_PARAM_BOOTCODE, bootsize,
1216 errc(EXIT_FAILURE, error, "internal error");
1220 s = gctl_get_ascii(req, "class");
1223 error = geom_gettree(&mesh);
1225 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
1226 classp = find_class(&mesh, s);
1227 if (classp == NULL) {
1228 geom_deletetree(&mesh);
1229 errx(EXIT_FAILURE, "Class %s not found.", s);
1231 if (gctl_get_int(req, "nargs") != 1)
1232 errx(EXIT_FAILURE, "Invalid number of arguments.");
1233 s = gctl_get_ascii(req, "arg0");
1236 gp = find_geom(classp, s);
1238 errx(EXIT_FAILURE, "No such geom: %s.", s);
1239 s = find_geomcfg(gp, "scheme");
1241 errx(EXIT_FAILURE, "Scheme not found for geom %s", gp->lg_name);
1242 if (strcmp(s, "VTOC8") == 0)
1247 if (gctl_has_param(req, GPART_PARAM_PARTCODE)) {
1248 s = gctl_get_ascii(req, GPART_PARAM_PARTCODE);
1250 partsize = VTOC_BOOTSIZE;
1252 partsize = 1024 * 1024; /* Arbitrary limit. */
1253 partcode = gpart_bootfile_read(s, &partsize);
1254 error = gctl_delete_param(req, GPART_PARAM_PARTCODE);
1256 errc(EXIT_FAILURE, error, "internal error");
1260 if (gctl_has_param(req, GPART_PARAM_INDEX)) {
1261 if (partcode == NULL)
1262 errx(EXIT_FAILURE, "-i is only valid with -p");
1263 idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
1265 errx(EXIT_FAILURE, "invalid partition index");
1266 error = gctl_delete_param(req, GPART_PARAM_INDEX);
1268 errc(EXIT_FAILURE, error, "internal error");
1272 if (partcode != NULL) {
1275 errx(EXIT_FAILURE, "missing -i option");
1276 gpart_write_partcode(gp, idx, partcode, partsize);
1278 if (partsize != VTOC_BOOTSIZE)
1279 errx(EXIT_FAILURE, "invalid bootcode");
1280 gpart_write_partcode_vtoc8(gp, idx, partcode);
1283 if (bootcode == NULL)
1284 errx(EXIT_FAILURE, "no -b nor -p");
1286 if (bootcode != NULL)
1287 gpart_issue(req, fl);
1289 geom_deletetree(&mesh);
1294 gpart_print_error(const char *errstr)
1299 error = strtol(errstr, &errmsg, 0);
1300 if (errmsg != errstr) {
1301 while (errmsg[0] == ' ')
1303 if (errmsg[0] != '\0')
1304 warnc(error, "%s", errmsg);
1308 warnx("%s", errmsg);
1311 static _Noreturn void
1312 gpart_issue(struct gctl_req *req, unsigned int fl __unused)
1318 if (gctl_get_int(req, "nargs") != 1)
1319 errx(EXIT_FAILURE, "Invalid number of arguments.");
1320 (void)gctl_delete_param(req, "nargs");
1322 /* autofill parameters (if applicable). */
1323 error = gpart_autofill(req);
1325 warnc(error, "autofill");
1326 status = EXIT_FAILURE;
1330 bzero(buf, sizeof(buf));
1331 gctl_rw_param(req, "output", sizeof(buf), buf);
1332 errstr = gctl_issue(req);
1333 if (errstr == NULL || errstr[0] == '\0') {
1336 status = EXIT_SUCCESS;
1340 gpart_print_error(errstr);
1341 status = EXIT_FAILURE;