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 autofill[] = "*";
64 static char optional[] = "";
65 static char flags[] = "C";
67 static char sstart[32];
68 static char ssize[32];
69 volatile sig_atomic_t undo_restore;
71 static const char const bootcode_param[] = "bootcode";
72 static const char const index_param[] = "index";
73 static const char const partcode_param[] = "partcode";
75 static struct gclass *find_class(struct gmesh *, const char *);
76 static struct ggeom * find_geom(struct gclass *, const char *);
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 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 *);
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_destroy(struct gctl_req *, unsigned int);
92 static void gpart_backup(struct gctl_req *, unsigned int);
93 static void gpart_restore(struct gctl_req *, unsigned int);
95 struct g_command PUBSYM(class_commands)[] = {
96 { "add", 0, gpart_issue, {
97 { 'b', "start", autofill, G_TYPE_STRING },
98 { 's', "size", autofill, G_TYPE_STRING },
99 { 't', "type", NULL, G_TYPE_STRING },
100 { 'i', index_param, optional, G_TYPE_ASCNUM },
101 { 'l', "label", optional, G_TYPE_STRING },
102 { 'f', "flags", flags, G_TYPE_STRING },
106 { "backup", 0, gpart_backup, G_NULL_OPTS,
109 { "bootcode", 0, gpart_bootcode, {
110 { 'b', bootcode_param, optional, G_TYPE_STRING },
111 { 'p', partcode_param, optional, G_TYPE_STRING },
112 { 'i', index_param, optional, G_TYPE_ASCNUM },
113 { 'f', "flags", flags, G_TYPE_STRING },
117 { "commit", 0, gpart_issue, G_NULL_OPTS, "geom", NULL },
118 { "create", 0, gpart_issue, {
119 { 's', "scheme", NULL, G_TYPE_STRING },
120 { 'n', "entries", optional, G_TYPE_ASCNUM },
121 { 'f', "flags", flags, G_TYPE_STRING },
125 { "delete", 0, gpart_issue, {
126 { 'i', index_param, NULL, G_TYPE_ASCNUM },
127 { 'f', "flags", flags, G_TYPE_STRING },
131 { "destroy", 0, gpart_destroy, {
132 { 'F', "force", NULL, G_TYPE_BOOL },
133 { 'f', "flags", flags, G_TYPE_STRING },
136 { "modify", 0, gpart_issue, {
137 { 'i', index_param, NULL, G_TYPE_ASCNUM },
138 { 'l', "label", optional, G_TYPE_STRING },
139 { 't', "type", optional, G_TYPE_STRING },
140 { 'f', "flags", flags, G_TYPE_STRING },
144 { "set", 0, gpart_issue, {
145 { 'a', "attrib", NULL, G_TYPE_STRING },
146 { 'i', index_param, NULL, G_TYPE_ASCNUM },
147 { 'f', "flags", flags, G_TYPE_STRING },
151 { "show", 0, gpart_show, {
152 { 'l', "show_label", NULL, G_TYPE_BOOL },
153 { 'r', "show_rawtype", NULL, G_TYPE_BOOL },
155 NULL, "[-lr] [geom ...]"
157 { "undo", 0, gpart_issue, G_NULL_OPTS, "geom", NULL },
158 { "unset", 0, gpart_issue, {
159 { 'a', "attrib", NULL, G_TYPE_STRING },
160 { 'i', index_param, NULL, G_TYPE_ASCNUM },
161 { 'f', "flags", flags, G_TYPE_STRING },
165 { "resize", 0, gpart_issue, {
166 { 's', "size", autofill, G_TYPE_STRING },
167 { 'i', index_param, NULL, G_TYPE_ASCNUM },
168 { 'f', "flags", flags, G_TYPE_STRING },
172 { "restore", 0, gpart_restore, {
173 { 'F', "force", NULL, G_TYPE_BOOL },
174 { 'l', "restore_labels", NULL, G_TYPE_BOOL },
175 { 'f', "flags", flags, G_TYPE_STRING },
177 NULL, "[-lF] [-f flags] provider [...]"
179 { "recover", 0, gpart_issue, {
180 { 'f', "flags", flags, G_TYPE_STRING },
187 static struct gclass *
188 find_class(struct gmesh *mesh, const char *name)
190 struct gclass *classp;
192 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
193 if (strcmp(classp->lg_name, name) == 0)
199 static struct ggeom *
200 find_geom(struct gclass *classp, const char *name)
204 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
205 name += strlen(_PATH_DEV);
206 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
207 if (strcmp(gp->lg_name, name) == 0)
214 find_geomcfg(struct ggeom *gp, const char *cfg)
218 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
219 if (!strcmp(gc->lg_name, cfg))
226 find_provcfg(struct gprovider *pp, const char *cfg)
230 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
231 if (!strcmp(gc->lg_name, cfg))
237 static struct gprovider *
238 find_provider(struct ggeom *gp, off_t minsector)
240 struct gprovider *pp, *bestpp;
242 off_t sector, bestsector;
246 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
247 s = find_provcfg(pp, "start");
249 s = find_provcfg(pp, "offset");
251 (off_t)strtoimax(s, NULL, 0) / pp->lg_sectorsize;
253 sector = (off_t)strtoimax(s, NULL, 0);
255 if (sector < minsector)
257 if (bestpp != NULL && sector >= bestsector)
267 fmtsize(int64_t rawsz)
271 humanize_number(buf, sizeof(buf), rawsz, "", HN_AUTOSCALE,
272 HN_B | HN_NOSPACE | HN_DECIMAL);
277 fmtattrib(struct gprovider *pp)
279 static char buf[128];
285 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
286 if (strcmp(gc->lg_name, "attrib") != 0)
288 idx += snprintf(buf + idx, sizeof(buf) - idx, "%s%s",
289 (idx == 0) ? " [" : ",", gc->lg_val);
292 snprintf(buf + idx, sizeof(buf) - idx, "] ");
297 gpart_autofill_resize(struct gctl_req *req)
302 struct gprovider *pp;
303 off_t last, size, start, new_size;
309 s = gctl_get_ascii(req, index_param);
310 idx = strtol(s, &val, 10);
311 if (idx < 1 || *s == '\0' || *val != '\0')
312 errx(EXIT_FAILURE, "invalid partition index");
314 error = geom_gettree(&mesh);
317 s = gctl_get_ascii(req, "class");
320 cp = find_class(&mesh, s);
322 errx(EXIT_FAILURE, "Class %s not found.", s);
323 s = gctl_get_ascii(req, "geom");
326 gp = find_geom(cp, s);
328 errx(EXIT_FAILURE, "No such geom: %s.", s);
329 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
331 errx(EXIT_FAILURE, "Provider for geom %s not found.", s);
333 s = gctl_get_ascii(req, "size");
337 error = g_parse_lba(s, pp->lg_sectorsize, &new_size);
339 errc(EXIT_FAILURE, error, "Invalid size param");
340 /* no autofill necessary. */
344 last = (off_t)strtoimax(find_geomcfg(gp, "last"), NULL, 0);
345 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
346 s = find_provcfg(pp, "index");
353 errx(EXIT_FAILURE, "invalid partition index");
355 s = find_provcfg(pp, "start");
357 s = find_provcfg(pp, "offset");
358 start = (off_t)strtoimax(s, NULL, 0) / pp->lg_sectorsize;
360 start = (off_t)strtoimax(s, NULL, 0);
361 s = find_provcfg(pp, "end");
363 s = find_provcfg(pp, "length");
365 (off_t)strtoimax(s, NULL, 0) / pp->lg_sectorsize;
367 lba = (off_t)strtoimax(s, NULL, 0) + 1;
370 geom_deletetree(&mesh);
374 pp = find_provider(gp, lba);
376 new_size = last - start + 1;
378 s = find_provcfg(pp, "start");
380 s = find_provcfg(pp, "offset");
382 (off_t)strtoimax(s, NULL, 0) / pp->lg_sectorsize;
384 new_lba = (off_t)strtoimax(s, NULL, 0);
386 * Is there any free space between current and
390 new_size = new_lba - start;
392 geom_deletetree(&mesh);
397 snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)new_size);
398 gctl_change_param(req, "size", -1, ssize);
399 geom_deletetree(&mesh);
404 gpart_autofill(struct gctl_req *req)
409 struct gprovider *pp;
415 int error, has_size, has_start;
417 s = gctl_get_ascii(req, "verb");
418 if (strcmp(s, "resize") == 0)
419 return gpart_autofill_resize(req);
420 if (strcmp(s, "add") != 0)
423 error = geom_gettree(&mesh);
426 s = gctl_get_ascii(req, "class");
429 cp = find_class(&mesh, s);
431 errx(EXIT_FAILURE, "Class %s not found.", s);
432 s = gctl_get_ascii(req, "geom");
435 gp = find_geom(cp, s);
437 errx(EXIT_FAILURE, "No such geom: %s.", s);
438 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
440 errx(EXIT_FAILURE, "Provider for geom %s not found.", s);
442 s = gctl_get_ascii(req, "size");
443 has_size = (*s == '*') ? 0 : 1;
446 error = g_parse_lba(s, pp->lg_sectorsize, &size);
448 errc(EXIT_FAILURE, error, "Invalid size param");
451 s = gctl_get_ascii(req, "start");
452 has_start = (*s == '*') ? 0 : 1;
455 error = g_parse_lba(s, pp->lg_sectorsize, &start);
457 errc(EXIT_FAILURE, error, "Invalid start param");
460 /* No autofill necessary. */
461 if (has_size && has_start)
464 first = (off_t)strtoimax(find_geomcfg(gp, "first"), NULL, 0);
465 last = (off_t)strtoimax(find_geomcfg(gp, "last"), NULL, 0);
467 while ((pp = find_provider(gp, first)) != NULL) {
468 s = find_provcfg(pp, "start");
470 s = find_provcfg(pp, "offset");
471 lba = (off_t)strtoimax(s, NULL, 0) / pp->lg_sectorsize;
473 lba = (off_t)strtoimax(s, NULL, 0);
476 /* Free space [first, lba> */
480 (uintmax_t)(len - size) < grade) {
484 } else if (has_start) {
485 if (start >= first && start < lba) {
487 grade = start - first;
490 if (grade == ~0ULL || len > size) {
498 s = find_provcfg(pp, "end");
500 s = find_provcfg(pp, "length");
502 (off_t)strtoimax(s, NULL, 0) / pp->lg_sectorsize;
504 first = (off_t)strtoimax(s, NULL, 0) + 1;
507 /* Free space [first-last] */
508 len = last - first + 1;
511 (uintmax_t)(len - size) < grade) {
515 } else if (has_start) {
516 if (start >= first && start <= last) {
517 size = last - start + 1;
518 grade = start - first;
521 if (grade == ~0ULL || len > size) {
529 if (grade == ~0ULL) {
530 geom_deletetree(&mesh);
535 snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)size);
536 gctl_change_param(req, "size", -1, ssize);
537 snprintf(sstart, sizeof(sstart), "%jd", (intmax_t)start);
538 gctl_change_param(req, "start", -1, sstart);
539 geom_deletetree(&mesh);
544 gpart_show_geom(struct ggeom *gp, const char *element)
546 struct gprovider *pp;
547 const char *s, *scheme;
548 off_t first, last, sector, end;
550 int idx, wblocks, wname;
552 scheme = find_geomcfg(gp, "scheme");
553 s = find_geomcfg(gp, "first");
554 first = (off_t)strtoimax(s, NULL, 0);
555 s = find_geomcfg(gp, "last");
556 last = (off_t)strtoimax(s, NULL, 0);
558 s = find_geomcfg(gp, "state");
559 if (s != NULL && *s != 'C')
561 wname = strlen(gp->lg_name);
562 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
563 secsz = pp->lg_sectorsize;
564 printf("=>%*jd %*jd %*s %s (%s)%s\n",
565 wblocks, (intmax_t)first, wblocks, (intmax_t)(last - first + 1),
567 scheme, fmtsize(pp->lg_mediasize),
568 s ? " [CORRUPT]": "");
570 while ((pp = find_provider(gp, first)) != NULL) {
571 s = find_provcfg(pp, "start");
573 s = find_provcfg(pp, "offset");
574 sector = (off_t)strtoimax(s, NULL, 0) / secsz;
576 sector = (off_t)strtoimax(s, NULL, 0);
578 s = find_provcfg(pp, "end");
580 s = find_provcfg(pp, "length");
581 length = (off_t)strtoimax(s, NULL, 0) / secsz;
582 end = sector + length - 1;
584 end = (off_t)strtoimax(s, NULL, 0);
585 length = end - sector + 1;
587 s = find_provcfg(pp, "index");
589 if (first < sector) {
590 printf(" %*jd %*jd %*s - free - (%s)\n",
591 wblocks, (intmax_t)first, wblocks,
592 (intmax_t)(sector - first), wname, "",
593 fmtsize((sector - first) * secsz));
595 printf(" %*jd %*jd %*d %s %s (%s)\n",
596 wblocks, (intmax_t)sector, wblocks, (intmax_t)length,
597 wname, idx, find_provcfg(pp, element),
598 fmtattrib(pp), fmtsize(pp->lg_mediasize));
602 length = last - first + 1;
603 printf(" %*jd %*jd %*s - free - (%s)\n",
604 wblocks, (intmax_t)first, wblocks, (intmax_t)length,
606 fmtsize(length * secsz));
612 gpart_show_hasopt(struct gctl_req *req, const char *opt, const char *elt)
615 if (!gctl_get_int(req, opt))
619 errx(EXIT_FAILURE, "-l and -r are mutually exclusive");
625 gpart_show(struct gctl_req *req, unsigned int fl __unused)
628 struct gclass *classp;
630 const char *element, *name;
634 if (gpart_show_hasopt(req, "show_label", element))
636 if (gpart_show_hasopt(req, "show_rawtype", element))
641 name = gctl_get_ascii(req, "class");
644 error = geom_gettree(&mesh);
646 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
647 classp = find_class(&mesh, name);
648 if (classp == NULL) {
649 geom_deletetree(&mesh);
650 errx(EXIT_FAILURE, "Class %s not found.", name);
652 nargs = gctl_get_int(req, "nargs");
654 for (i = 0; i < nargs; i++) {
655 name = gctl_get_ascii(req, "arg%d", i);
656 gp = find_geom(classp, name);
658 gpart_show_geom(gp, element);
660 errx(EXIT_FAILURE, "No such geom: %s.", name);
663 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
664 gpart_show_geom(gp, element);
667 geom_deletetree(&mesh);
671 gpart_backup(struct gctl_req *req, unsigned int fl __unused)
674 struct gclass *classp;
675 struct gprovider *pp;
677 const char *s, *scheme;
680 int error, i, windex, wblocks, wtype;
682 if (gctl_get_int(req, "nargs") != 1)
683 errx(EXIT_FAILURE, "Invalid number of arguments.");
684 error = geom_gettree(&mesh);
686 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
687 s = gctl_get_ascii(req, "class");
690 classp = find_class(&mesh, s);
691 if (classp == NULL) {
692 geom_deletetree(&mesh);
693 errx(EXIT_FAILURE, "Class %s not found.", s);
695 s = gctl_get_ascii(req, "arg0");
698 gp = find_geom(classp, s);
700 errx(EXIT_FAILURE, "No such geom: %s.", s);
701 scheme = find_geomcfg(gp, "scheme");
704 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
705 secsz = pp->lg_sectorsize;
706 s = find_geomcfg(gp, "last");
709 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
710 s = find_provcfg(pp, "type");
715 s = find_geomcfg(gp, "entries");
717 printf("%s %s\n", scheme, s);
718 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
719 s = find_provcfg(pp, "start");
721 s = find_provcfg(pp, "offset");
722 sector = (off_t)strtoimax(s, NULL, 0) / secsz;
724 sector = (off_t)strtoimax(s, NULL, 0);
726 s = find_provcfg(pp, "end");
728 s = find_provcfg(pp, "length");
729 length = (off_t)strtoimax(s, NULL, 0) / secsz;
731 end = (off_t)strtoimax(s, NULL, 0);
732 length = end - sector + 1;
734 s = find_provcfg(pp, "label");
735 printf("%-*s %*s %*jd %*jd %s %s\n",
736 windex, find_provcfg(pp, "index"),
737 wtype, find_provcfg(pp, "type"),
738 wblocks, (intmax_t)sector,
739 wblocks, (intmax_t)length,
740 (s != NULL) ? s: "", fmtattrib(pp));
742 geom_deletetree(&mesh);
746 skip_line(const char *p)
752 if (isspace(*p) == 0)
760 gpart_sighndl(int sig __unused)
766 gpart_restore(struct gctl_req *req, unsigned int fl __unused)
769 struct gclass *classp;
772 struct sigaction si_sa;
773 const char *s, *flagss, *errstr, *label;
774 char **ap, *argv[6], line[BUFSIZ], *pline;
775 int error, forced, i, l, nargs, created, rl;
777 nargs = gctl_get_int(req, "nargs");
779 errx(EXIT_FAILURE, "Invalid number of arguments.");
781 forced = gctl_get_int(req, "force");
782 flagss = gctl_get_ascii(req, "flags");
783 rl = gctl_get_int(req, "restore_labels");
784 s = gctl_get_ascii(req, "class");
787 error = geom_gettree(&mesh);
789 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
790 classp = find_class(&mesh, s);
791 if (classp == NULL) {
792 geom_deletetree(&mesh);
793 errx(EXIT_FAILURE, "Class %s not found.", s);
796 sigemptyset(&si_sa.sa_mask);
798 si_sa.sa_handler = gpart_sighndl;
799 if (sigaction(SIGINT, &si_sa, 0) == -1)
800 err(EXIT_FAILURE, "sigaction SIGINT");
803 /* destroy existent partition table before restore */
804 for (i = 0; i < nargs; i++) {
805 s = gctl_get_ascii(req, "arg%d", i);
806 gp = find_geom(classp, s);
808 r = gctl_get_handle();
809 gctl_ro_param(r, "class", -1,
811 gctl_ro_param(r, "verb", -1, "destroy");
812 gctl_ro_param(r, "flags", -1, "restore");
813 gctl_ro_param(r, "force", sizeof(forced),
815 gctl_ro_param(r, "geom", -1, s);
816 errstr = gctl_issue(r);
817 if (errstr != NULL && errstr[0] != '\0') {
818 gpart_print_error(errstr);
827 while (undo_restore == 0 &&
828 fgets(line, sizeof(line) - 1, stdin) != NULL) {
829 /* Format of backup entries:
830 * <scheme name> <number of entries>
831 * <index> <type> <start> <size> [label] ['['attrib[,attrib]']']
833 pline = (char *)line;
834 pline[strlen(line) - 1] = 0;
835 if (skip_line(pline))
838 (*ap = strsep(&pline, " \t")) != NULL;)
839 if (**ap != '\0' && ++ap >= &argv[6])
842 label = pline = NULL;
843 if (l == 1 || l == 2) { /* create table */
845 errx(EXIT_FAILURE, "Incorrect backup format.");
846 for (i = 0; i < nargs; i++) {
847 s = gctl_get_ascii(req, "arg%d", i);
848 r = gctl_get_handle();
849 gctl_ro_param(r, "class", -1,
851 gctl_ro_param(r, "verb", -1, "create");
852 gctl_ro_param(r, "scheme", -1, argv[0]);
854 gctl_ro_param(r, "entries",
856 gctl_ro_param(r, "flags", -1, "restore");
857 gctl_ro_param(r, "provider", -1, s);
858 errstr = gctl_issue(r);
859 if (errstr != NULL && errstr[0] != '\0') {
860 gpart_print_error(errstr);
868 } else if (l < 4 || created == 0)
869 errx(EXIT_FAILURE, "Incorrect backup format.");
871 if (strchr(argv[4], '[') == NULL)
879 /* Add partitions to each table */
880 for (i = 0; i < nargs; i++) {
881 s = gctl_get_ascii(req, "arg%d", i);
882 r = gctl_get_handle();
883 gctl_ro_param(r, "class", -1, classp->lg_name);
884 gctl_ro_param(r, "verb", -1, "add");
885 gctl_ro_param(r, "flags", -1, "restore");
886 gctl_ro_param(r, index_param, -1, argv[0]);
887 gctl_ro_param(r, "type", -1, argv[1]);
888 gctl_ro_param(r, "start", -1, argv[2]);
889 gctl_ro_param(r, "size", -1, argv[3]);
890 if (rl != 0 && label != NULL)
891 gctl_ro_param(r, "label", -1, argv[4]);
892 gctl_ro_param(r, "geom", -1, s);
893 error = gpart_autofill(r);
895 errc(EXIT_FAILURE, error, "autofill");
896 errstr = gctl_issue(r);
897 if (errstr != NULL && errstr[0] != '\0') {
898 gpart_print_error(errstr);
904 if (pline == NULL || *pline != '[')
908 label = argv[0]; /* save pointer to index_param */
910 (*ap = strsep(&pline, ",]")) != NULL;)
911 if (**ap != '\0' && ++ap >= &argv[6])
913 for (i = 0; i < nargs; i++) {
915 s = gctl_get_ascii(req, "arg%d", i);
917 r = gctl_get_handle();
918 gctl_ro_param(r, "class", -1, classp->lg_name);
919 gctl_ro_param(r, "verb", -1, "set");
920 gctl_ro_param(r, "flags", -1, "restore");
921 gctl_ro_param(r, index_param, -1, label);
922 gctl_ro_param(r, "attrib", -1, argv[--l]);
923 gctl_ro_param(r, "geom", -1, s);
924 errstr = gctl_issue(r);
925 if (errstr != NULL && errstr[0] != '\0') {
926 gpart_print_error(errstr);
936 /* commit changes if needed */
937 if (strchr(flagss, 'C') != NULL) {
938 for (i = 0; i < nargs; i++) {
939 s = gctl_get_ascii(req, "arg%d", i);
940 r = gctl_get_handle();
941 gctl_ro_param(r, "class", -1, classp->lg_name);
942 gctl_ro_param(r, "verb", -1, "commit");
943 gctl_ro_param(r, "geom", -1, s);
944 errstr = gctl_issue(r);
945 if (errstr != NULL && errstr[0] != '\0') {
946 gpart_print_error(errstr);
954 geom_deletetree(&mesh);
958 for (i = 0; i < nargs; i++) {
959 s = gctl_get_ascii(req, "arg%d", i);
960 r = gctl_get_handle();
961 gctl_ro_param(r, "class", -1, classp->lg_name);
962 gctl_ro_param(r, "verb", -1, "undo");
963 gctl_ro_param(r, "geom", -1, s);
968 geom_deletetree(&mesh);
973 gpart_bootfile_read(const char *bootfile, ssize_t *size)
979 if (stat(bootfile, &sb) == -1)
980 err(EXIT_FAILURE, "%s", bootfile);
981 if (!S_ISREG(sb.st_mode))
982 errx(EXIT_FAILURE, "%s: not a regular file", bootfile);
984 errx(EXIT_FAILURE, "%s: empty file", bootfile);
985 if (*size > 0 && sb.st_size > *size)
986 errx(EXIT_FAILURE, "%s: file too big (%zu limit)", bootfile,
991 fd = open(bootfile, O_RDONLY);
993 err(EXIT_FAILURE, "%s", bootfile);
994 code = malloc(*size);
996 err(EXIT_FAILURE, NULL);
997 if (read(fd, code, *size) != *size)
998 err(EXIT_FAILURE, "%s", bootfile);
1005 gpart_write_partcode(struct ggeom *gp, int idx, void *code, ssize_t size)
1008 struct gprovider *pp;
1014 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1015 s = find_provcfg(pp, "index");
1023 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1024 fd = open(dsf, O_WRONLY);
1026 err(EXIT_FAILURE, "%s", dsf);
1027 if (lseek(fd, size, SEEK_SET) != size)
1028 errx(EXIT_FAILURE, "%s: not enough space", dsf);
1029 if (lseek(fd, 0, SEEK_SET) != 0)
1030 err(EXIT_FAILURE, "%s", dsf);
1033 * When writing to a disk device, the write must be
1034 * sector aligned and not write to any partial sectors,
1035 * so round up the buffer size to the next sector and zero it.
1037 bsize = (size + pp->lg_sectorsize - 1) /
1038 pp->lg_sectorsize * pp->lg_sectorsize;
1039 buf = calloc(1, bsize);
1041 err(EXIT_FAILURE, "%s", dsf);
1042 bcopy(code, buf, size);
1043 if (write(fd, buf, bsize) != bsize)
1044 err(EXIT_FAILURE, "%s", dsf);
1048 errx(EXIT_FAILURE, "invalid partition index");
1052 gpart_write_partcode_vtoc8(struct ggeom *gp, int idx, void *code)
1055 struct gprovider *pp;
1060 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1061 s = find_provcfg(pp, "index");
1064 if (idx != 0 && atoi(s) != idx)
1066 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1067 if (pp->lg_sectorsize != sizeof(struct vtoc8))
1068 errx(EXIT_FAILURE, "%s: unexpected sector "
1069 "size (%d)\n", dsf, pp->lg_sectorsize);
1070 fd = open(dsf, O_WRONLY);
1072 err(EXIT_FAILURE, "%s", dsf);
1073 if (lseek(fd, VTOC_BOOTSIZE, SEEK_SET) != VTOC_BOOTSIZE)
1076 * We ignore the first VTOC_BOOTSIZE bytes of boot code in
1077 * order to avoid overwriting the label.
1079 if (lseek(fd, sizeof(struct vtoc8), SEEK_SET) !=
1080 sizeof(struct vtoc8))
1081 err(EXIT_FAILURE, "%s", dsf);
1082 if (write(fd, (caddr_t)code + sizeof(struct vtoc8),
1083 VTOC_BOOTSIZE - sizeof(struct vtoc8)) != VTOC_BOOTSIZE -
1084 sizeof(struct vtoc8))
1085 err(EXIT_FAILURE, "%s", dsf);
1088 if (idx != 0 && atoi(s) == idx)
1092 errx(EXIT_FAILURE, "%s: no partitions", gp->lg_name);
1096 gpart_bootcode(struct gctl_req *req, unsigned int fl)
1099 struct gclass *classp;
1103 void *bootcode, *partcode;
1104 size_t bootsize, partsize;
1105 int error, idx, vtoc8;
1107 if (gctl_has_param(req, bootcode_param)) {
1108 s = gctl_get_ascii(req, bootcode_param);
1109 bootsize = 800 * 1024; /* Arbitrary limit. */
1110 bootcode = gpart_bootfile_read(s, &bootsize);
1111 error = gctl_change_param(req, bootcode_param, bootsize,
1114 errc(EXIT_FAILURE, error, "internal error");
1120 s = gctl_get_ascii(req, "class");
1123 error = geom_gettree(&mesh);
1125 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
1126 classp = find_class(&mesh, s);
1127 if (classp == NULL) {
1128 geom_deletetree(&mesh);
1129 errx(EXIT_FAILURE, "Class %s not found.", s);
1131 s = gctl_get_ascii(req, "geom");
1134 gp = find_geom(classp, s);
1136 errx(EXIT_FAILURE, "No such geom: %s.", s);
1137 s = find_geomcfg(gp, "scheme");
1139 if (strcmp(s, "VTOC8") == 0)
1142 if (gctl_has_param(req, partcode_param)) {
1143 s = gctl_get_ascii(req, partcode_param);
1144 partsize = vtoc8 != 0 ? VTOC_BOOTSIZE : bootsize * 1024;
1145 partcode = gpart_bootfile_read(s, &partsize);
1146 error = gctl_delete_param(req, partcode_param);
1148 errc(EXIT_FAILURE, error, "internal error");
1154 if (gctl_has_param(req, index_param)) {
1155 if (partcode == NULL)
1156 errx(EXIT_FAILURE, "-i is only valid with -p");
1157 s = gctl_get_ascii(req, index_param);
1158 idx = strtol(s, &sp, 10);
1159 if (idx < 1 || *s == '\0' || *sp != '\0')
1160 errx(EXIT_FAILURE, "invalid partition index");
1161 error = gctl_delete_param(req, index_param);
1163 errc(EXIT_FAILURE, error, "internal error");
1167 if (partcode != NULL) {
1170 errx(EXIT_FAILURE, "missing -i option");
1171 gpart_write_partcode(gp, idx, partcode, partsize);
1173 gpart_write_partcode_vtoc8(gp, idx, partcode);
1175 if (bootcode == NULL)
1176 errx(EXIT_FAILURE, "no -b nor -p");
1178 if (bootcode != NULL)
1179 gpart_issue(req, fl);
1181 geom_deletetree(&mesh);
1185 gpart_destroy(struct gctl_req *req, unsigned int fl)
1188 if (gctl_get_int(req, "force"))
1189 gctl_change_param(req, "force", -1, "1");
1191 gctl_change_param(req, "force", -1, "0");
1192 gpart_issue(req, fl);
1196 gpart_print_error(const char *errstr)
1201 error = strtol(errstr, &errmsg, 0);
1202 if (errmsg != errstr) {
1203 while (errmsg[0] == ' ')
1205 if (errmsg[0] != '\0')
1206 warnc(error, "%s", errmsg);
1210 warnx("%s", errmsg);
1214 gpart_issue(struct gctl_req *req, unsigned int fl __unused)
1220 /* autofill parameters (if applicable). */
1221 error = gpart_autofill(req);
1223 warnc(error, "autofill");
1224 status = EXIT_FAILURE;
1228 bzero(buf, sizeof(buf));
1229 gctl_rw_param(req, "output", sizeof(buf), buf);
1230 errstr = gctl_issue(req);
1231 if (errstr == NULL || errstr[0] == '\0') {
1234 status = EXIT_SUCCESS;
1238 gpart_print_error(errstr);
\r
1239 status = EXIT_FAILURE;