]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sbin/geom/class/part/geom_part.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sbin / geom / class / part / geom_part.c
1 /*-
2  * Copyright (c) 2007, 2008 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/stat.h>
31 #include <sys/vtoc.h>
32
33 #include <assert.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <libgeom.h>
38 #include <libutil.h>
39 #include <paths.h>
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <strings.h>
45 #include <unistd.h>
46
47 #include "core/geom.h"
48 #include "misc/subr.h"
49
50 #ifdef STATIC_GEOM_CLASSES
51 #define PUBSYM(x)       gpart_##x
52 #else
53 #define PUBSYM(x)       x
54 #endif
55
56 uint32_t PUBSYM(lib_version) = G_LIB_VERSION;
57 uint32_t PUBSYM(version) = 0;
58
59 static char autofill[] = "*";
60 static char optional[] = "";
61 static char flags[] = "C";
62
63 static const char const bootcode_param[] = "bootcode";
64 static const char const index_param[] = "index";
65 static const char const partcode_param[] = "partcode";
66
67 static struct gclass *find_class(struct gmesh *, const char *);
68 static struct ggeom * find_geom(struct gclass *, const char *);
69 static const char *find_geomcfg(struct ggeom *, const char *);
70 static const char *find_provcfg(struct gprovider *, const char *);
71 static struct gprovider *find_provider(struct ggeom *,
72     unsigned long long);
73 static const char *fmtsize(int64_t);
74 static int gpart_autofill(struct gctl_req *);
75 static void gpart_bootcode(struct gctl_req *, unsigned int);
76 static void *gpart_bootfile_read(const char *, ssize_t *);
77 static void gpart_issue(struct gctl_req *, unsigned int);
78 static void gpart_show(struct gctl_req *, unsigned int);
79 static void gpart_show_geom(struct ggeom *, const char *);
80 static int gpart_show_hasopt(struct gctl_req *, const char *, const char *);
81 static void gpart_write_partcode(struct ggeom *, int, void *, ssize_t);
82 static void gpart_write_partcode_vtoc8(struct ggeom *, int, void *);
83
84 struct g_command PUBSYM(class_commands)[] = {
85         { "add", 0, gpart_issue, {
86                 { 'b', "start", autofill, G_TYPE_ASCLBA },
87                 { 's', "size", autofill, G_TYPE_ASCLBA },
88                 { 't', "type", NULL, G_TYPE_STRING },
89                 { 'i', index_param, optional, G_TYPE_ASCNUM },
90                 { 'l', "label", optional, G_TYPE_STRING },
91                 { 'f', "flags", flags, G_TYPE_STRING },
92                 G_OPT_SENTINEL },
93           "geom", NULL
94         },
95         { "bootcode", 0, gpart_bootcode, {
96                 { 'b', bootcode_param, optional, G_TYPE_STRING },
97                 { 'p', partcode_param, optional, G_TYPE_STRING },
98                 { 'i', index_param, optional, G_TYPE_ASCNUM },
99                 { 'f', "flags", flags, G_TYPE_STRING },
100                 G_OPT_SENTINEL },
101           "geom", NULL
102         },
103         { "commit", 0, gpart_issue, G_NULL_OPTS, "geom", NULL },
104         { "create", 0, gpart_issue, {
105                 { 's', "scheme", NULL, G_TYPE_STRING },
106                 { 'n', "entries", optional, G_TYPE_ASCNUM },
107                 { 'f', "flags", flags, G_TYPE_STRING },
108                 G_OPT_SENTINEL },
109           "provider", NULL
110         },
111         { "delete", 0, gpart_issue, {
112                 { 'i', index_param, NULL, G_TYPE_ASCNUM },
113                 { 'f', "flags", flags, G_TYPE_STRING },
114                 G_OPT_SENTINEL },
115           "geom", NULL
116         },
117         { "destroy", 0, gpart_issue, {
118                 { 'f', "flags", flags, G_TYPE_STRING },
119                 G_OPT_SENTINEL },
120           "geom", NULL },
121         { "modify", 0, gpart_issue, {
122                 { 'i', index_param, NULL, G_TYPE_ASCNUM },
123                 { 'l', "label", optional, G_TYPE_STRING },
124                 { 't', "type", optional, G_TYPE_STRING },
125                 { 'f', "flags", flags, G_TYPE_STRING },
126                 G_OPT_SENTINEL },
127           "geom", NULL
128         },
129         { "set", 0, gpart_issue, {
130                 { 'a', "attrib", NULL, G_TYPE_STRING },
131                 { 'i', index_param, NULL, G_TYPE_ASCNUM },
132                 { 'f', "flags", flags, G_TYPE_STRING },
133                 G_OPT_SENTINEL },
134           "geom", NULL
135         },
136         { "show", 0, gpart_show, {
137                 { 'l', "show_label", NULL, G_TYPE_BOOL },
138                 { 'r', "show_rawtype", NULL, G_TYPE_BOOL },
139                 G_OPT_SENTINEL },
140           NULL, "[-lr] [geom ...]"
141         },
142         { "undo", 0, gpart_issue, G_NULL_OPTS, "geom", NULL },
143         { "unset", 0, gpart_issue, {
144                 { 'a', "attrib", NULL, G_TYPE_STRING },
145                 { 'i', index_param, NULL, G_TYPE_ASCNUM },
146                 { 'f', "flags", flags, G_TYPE_STRING },
147                 G_OPT_SENTINEL },
148           "geom", NULL
149         },
150         G_CMD_SENTINEL
151 };
152
153 static struct gclass *
154 find_class(struct gmesh *mesh, const char *name)
155 {
156         struct gclass *classp;
157
158         LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
159                 if (strcmp(classp->lg_name, name) == 0)
160                         return (classp);
161         }
162         return (NULL);
163 }
164
165 static struct ggeom *
166 find_geom(struct gclass *classp, const char *name)
167 {
168         struct ggeom *gp;
169
170         LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
171                 if (strcmp(gp->lg_name, name) == 0)
172                         return (gp);
173         }
174         return (NULL);
175 }
176
177 static const char *
178 find_geomcfg(struct ggeom *gp, const char *cfg)
179 {
180         struct gconfig *gc;
181
182         LIST_FOREACH(gc, &gp->lg_config, lg_config) {
183                 if (!strcmp(gc->lg_name, cfg))
184                         return (gc->lg_val);
185         }
186         return (NULL);
187 }
188
189 static const char *
190 find_provcfg(struct gprovider *pp, const char *cfg)
191 {
192         struct gconfig *gc;
193
194         LIST_FOREACH(gc, &pp->lg_config, lg_config) {
195                 if (!strcmp(gc->lg_name, cfg))
196                         return (gc->lg_val);
197         }
198         return (NULL);
199 }
200
201 static struct gprovider *
202 find_provider(struct ggeom *gp, unsigned long long minsector)
203 {
204         struct gprovider *pp, *bestpp;
205         const char *s;
206         unsigned long long sector, bestsector;
207
208         bestpp = NULL;
209         LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
210                 s = find_provcfg(pp, "start");
211                 if (s == NULL) {
212                         s = find_provcfg(pp, "offset");
213                         sector = atoll(s) / pp->lg_sectorsize;
214                 } else
215                         sector = atoll(s);
216
217                 if (sector < minsector)
218                         continue;
219                 if (bestpp != NULL && sector >= bestsector)
220                         continue;
221
222                 bestpp = pp;
223                 bestsector = sector;
224         }
225         return (bestpp);
226 }
227
228 static const char *
229 fmtsize(int64_t rawsz)
230 {
231         static char buf[5];
232
233         humanize_number(buf, sizeof(buf), rawsz, "", HN_AUTOSCALE,
234             HN_B | HN_NOSPACE | HN_DECIMAL);
235         return (buf);
236 }
237
238 static const char *
239 fmtattrib(struct gprovider *pp)
240 {
241         static char buf[128];
242         struct gconfig *gc;
243         u_int idx;
244
245         buf[0] = '\0';
246         idx = 0;
247         LIST_FOREACH(gc, &pp->lg_config, lg_config) {
248                 if (strcmp(gc->lg_name, "attrib") != 0)
249                         continue;
250                 idx += snprintf(buf + idx, sizeof(buf) - idx, "%s%s",
251                     (idx == 0) ? " [" : ",", gc->lg_val);
252         }
253         if (idx > 0)
254                 snprintf(buf + idx, sizeof(buf) - idx, "] ");
255         return (buf);
256 }
257
258 static int
259 gpart_autofill(struct gctl_req *req)
260 {
261         struct gmesh mesh;
262         struct gclass *cp;
263         struct ggeom *gp;
264         struct gprovider *pp;
265         unsigned long long first, last;
266         unsigned long long size, start;
267         unsigned long long lba, len, grade;
268         const char *s;
269         char *val;
270         int error, has_size, has_start;
271
272         s = gctl_get_ascii(req, "verb");
273         if (strcmp(s, "add") != 0)
274                 return (0);
275
276         s = gctl_get_ascii(req, "size");
277         has_size = (*s == '*') ? 0 : 1;
278         size = (has_size) ? (unsigned long long)atoll(s) : 0ULL;
279
280         s = gctl_get_ascii(req, "start");
281         has_start = (*s == '*') ? 0 : 1;
282         start = (has_start) ? (unsigned long long)atoll(s) : ~0ULL;
283
284         /* No autofill necessary. */
285         if (has_size && has_start)
286                 return (0);
287
288         error = geom_gettree(&mesh);
289         if (error)
290                 return (error);
291         s = gctl_get_ascii(req, "class");
292         if (s == NULL)
293                 abort();
294         cp = find_class(&mesh, s);
295         if (cp == NULL)
296                 errx(EXIT_FAILURE, "Class %s not found.", s);
297         s = gctl_get_ascii(req, "geom");
298         if (s == NULL)
299                 abort();
300         gp = find_geom(cp, s);
301         if (gp == NULL)
302                 errx(EXIT_FAILURE, "No such geom: %s.", s);
303         first = atoll(find_geomcfg(gp, "first"));
304         last = atoll(find_geomcfg(gp, "last"));
305         grade = ~0ULL;
306         while ((pp = find_provider(gp, first)) != NULL) {
307                 s = find_provcfg(pp, "start");
308                 if (s == NULL) {
309                         s = find_provcfg(pp, "offset");
310                         lba = atoll(s) / pp->lg_sectorsize;
311                 } else
312                         lba = atoll(s);
313
314                 if (first < lba) {
315                         /* Free space [first, lba> */
316                         len = lba - first;
317                         if (has_size) {
318                                 if (len >= size && len - size < grade) {
319                                         start = first;
320                                         grade = len - size;
321                                 }
322                         } else if (has_start) {
323                                 if (start >= first && start < lba) {
324                                         size = lba - start;
325                                         grade = start - first;
326                                 }
327                         } else {
328                                 if (grade == ~0ULL || len > size) {
329                                         start = first;
330                                         size = len;
331                                         grade = 0;
332                                 }
333                         }
334                 }
335
336                 s = find_provcfg(pp, "end");
337                 if (s == NULL) {
338                         s = find_provcfg(pp, "length");
339                         first = lba + atoll(s) / pp->lg_sectorsize;
340                 } else
341                         first = atoll(s) + 1;
342         }
343         if (first <= last) {
344                 /* Free space [first-last] */
345                 len = last - first + 1;
346                 if (has_size) {
347                         if (len >= size && len - size < grade) {
348                                 start = first;
349                                 grade = len - size;
350                         }
351                 } else if (has_start) {
352                         if (start >= first && start <= last) {
353                                 size = last - start + 1;
354                                 grade = start - first;
355                         }
356                 } else {
357                         if (grade == ~0ULL || len > size) {
358                                 start = first;
359                                 size = len;
360                                 grade = 0;
361                         }
362                 }
363         }
364
365         if (grade == ~0ULL)
366                 return (ENOSPC);
367
368         if (!has_size) {
369                 asprintf(&val, "%llu", size);
370                 if (val == NULL)
371                         return (ENOMEM);
372                 gctl_change_param(req, "size", -1, val);
373         }
374         if (!has_start) {
375                 asprintf(&val, "%llu", start);
376                 if (val == NULL)
377                         return (ENOMEM);
378                 gctl_change_param(req, "start", -1, val);
379         }
380         return (0);
381 }
382
383 static void
384 gpart_show_geom(struct ggeom *gp, const char *element)
385 {
386         struct gprovider *pp;
387         const char *s, *scheme;
388         unsigned long long first, last, sector, end;
389         unsigned long long length, secsz;
390         int idx, wblocks, wname;
391
392         scheme = find_geomcfg(gp, "scheme");
393         s = find_geomcfg(gp, "first");
394         first = atoll(s);
395         s = find_geomcfg(gp, "last");
396         last = atoll(s);
397         wblocks = strlen(s);
398         wname = strlen(gp->lg_name);
399         pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
400         secsz = pp->lg_sectorsize;
401         printf("=>%*llu  %*llu  %*s  %s  (%s)\n",
402             wblocks, first, wblocks, (last - first + 1),
403             wname, gp->lg_name,
404             scheme, fmtsize(pp->lg_mediasize));
405
406         while ((pp = find_provider(gp, first)) != NULL) {
407                 s = find_provcfg(pp, "start");
408                 if (s == NULL) {
409                         s = find_provcfg(pp, "offset");
410                         sector = atoll(s) / secsz;
411                 } else
412                         sector = atoll(s);
413
414                 s = find_provcfg(pp, "end");
415                 if (s == NULL) {
416                         s = find_provcfg(pp, "length");
417                         length = atoll(s) / secsz;
418                         end = sector + length - 1;
419                 } else {
420                         end = atoll(s);
421                         length = end - sector + 1;
422                 }
423                 s = find_provcfg(pp, "index");
424                 idx = atoi(s);
425                 if (first < sector) {
426                         printf("  %*llu  %*llu  %*s  - free -  (%s)\n",
427                             wblocks, first, wblocks, sector - first,
428                             wname, "",
429                             fmtsize((sector - first) * secsz));
430                 }
431                 printf("  %*llu  %*llu  %*d  %s %s (%s)\n",
432                     wblocks, sector, wblocks, length,
433                     wname, idx, find_provcfg(pp, element),
434                     fmtattrib(pp), fmtsize(pp->lg_mediasize));
435                 first = end + 1;
436         }
437         if (first <= last) {
438                 length = last - first + 1;
439                 printf("  %*llu  %*llu  %*s  - free -  (%s)\n",
440                     wblocks, first, wblocks, length,
441                     wname, "",
442                     fmtsize(length * secsz));
443         }
444         printf("\n");
445 }
446
447 static int
448 gpart_show_hasopt(struct gctl_req *req, const char *opt, const char *elt)
449 {
450
451         if (!gctl_get_int(req, opt))
452                 return (0);
453
454         if (elt != NULL)
455                 errx(EXIT_FAILURE, "-l and -r are mutually exclusive");
456
457         return (1);
458 }
459
460 static void
461 gpart_show(struct gctl_req *req, unsigned int fl __unused)
462 {
463         struct gmesh mesh;
464         struct gclass *classp;
465         struct ggeom *gp;
466         const char *element, *name;
467         int error, i, nargs;
468
469         element = NULL;
470         if (gpart_show_hasopt(req, "show_label", element))
471                 element = "label";
472         if (gpart_show_hasopt(req, "show_rawtype", element))
473                 element = "rawtype";
474         if (element == NULL)
475                 element = "type";
476
477         name = gctl_get_ascii(req, "class");
478         if (name == NULL)
479                 abort();
480         error = geom_gettree(&mesh);
481         if (error != 0)
482                 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
483         classp = find_class(&mesh, name);
484         if (classp == NULL) {
485                 geom_deletetree(&mesh);
486                 errx(EXIT_FAILURE, "Class %s not found.", name);
487         }
488         nargs = gctl_get_int(req, "nargs");
489         if (nargs > 0) {
490                 for (i = 0; i < nargs; i++) {
491                         name = gctl_get_ascii(req, "arg%d", i);
492                         gp = find_geom(classp, name);
493                         if (gp != NULL)
494                                 gpart_show_geom(gp, element);
495                         else
496                                 errx(EXIT_FAILURE, "No such geom: %s.", name);
497                 }
498         } else {
499                 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
500                         gpart_show_geom(gp, element);
501                 }
502         }
503         geom_deletetree(&mesh);
504 }
505
506 static void *
507 gpart_bootfile_read(const char *bootfile, ssize_t *size)
508 {
509         struct stat sb;
510         void *code;
511         int fd;
512
513         if (stat(bootfile, &sb) == -1)
514                 err(EXIT_FAILURE, "%s", bootfile);
515         if (!S_ISREG(sb.st_mode))
516                 errx(EXIT_FAILURE, "%s: not a regular file", bootfile);
517         if (sb.st_size == 0)
518                 errx(EXIT_FAILURE, "%s: empty file", bootfile);
519         if (*size > 0 && sb.st_size > *size)
520                 errx(EXIT_FAILURE, "%s: file too big (%zu limit)", bootfile,
521                     *size);
522
523         *size = sb.st_size;
524
525         fd = open(bootfile, O_RDONLY);
526         if (fd == -1)
527                 err(EXIT_FAILURE, "%s", bootfile);
528         code = malloc(*size);
529         if (code == NULL)
530                 err(EXIT_FAILURE, NULL);
531         if (read(fd, code, *size) != *size)
532                 err(EXIT_FAILURE, "%s", bootfile);
533         close(fd);
534
535         return (code);
536 }
537
538 static void
539 gpart_write_partcode(struct ggeom *gp, int idx, void *code, ssize_t size)
540 {
541         char dsf[128];
542         struct gprovider *pp;
543         const char *s;
544         char *buf;
545         off_t bsize;
546         int fd;
547
548         LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
549                 s = find_provcfg(pp, "index");
550                 if (s == NULL)
551                         continue;
552                 if (atoi(s) == idx)
553                         break;
554         }
555
556         if (pp != NULL) {
557                 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
558                 fd = open(dsf, O_WRONLY);
559                 if (fd == -1)
560                         err(EXIT_FAILURE, "%s", dsf);
561                 if (lseek(fd, size, SEEK_SET) != size)
562                         errx(EXIT_FAILURE, "%s: not enough space", dsf);
563                 if (lseek(fd, 0, SEEK_SET) != 0)
564                         err(EXIT_FAILURE, "%s", dsf);
565
566                 /*
567                  * When writing to a disk device, the write must be
568                  * sector aligned and not write to any partial sectors,
569                  * so round up the buffer size to the next sector and zero it.
570                  */
571                 bsize = (size + pp->lg_sectorsize - 1) /
572                     pp->lg_sectorsize * pp->lg_sectorsize;
573                 buf = calloc(1, bsize);
574                 if (buf == NULL)
575                         err(EXIT_FAILURE, "%s", dsf);
576                 bcopy(code, buf, size);
577                 if (write(fd, buf, bsize) != bsize)
578                         err(EXIT_FAILURE, "%s", dsf);
579                 free(buf);
580                 close(fd);
581         } else
582                 errx(EXIT_FAILURE, "invalid partition index");
583 }
584
585 static void
586 gpart_write_partcode_vtoc8(struct ggeom *gp, int idx, void *code)
587 {
588         char dsf[128];
589         struct gprovider *pp;
590         const char *s;
591         int installed, fd;
592
593         installed = 0;
594         LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
595                 s = find_provcfg(pp, "index");
596                 if (s == NULL)
597                         continue;
598                 if (idx != 0 && atoi(s) != idx)
599                         continue;
600                 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
601                 if (pp->lg_sectorsize != sizeof(struct vtoc8))
602                         errx(EXIT_FAILURE, "%s: unexpected sector "
603                             "size (%d)\n", dsf, pp->lg_sectorsize);
604                 fd = open(dsf, O_WRONLY);
605                 if (fd == -1)
606                         err(EXIT_FAILURE, "%s", dsf);
607                 if (lseek(fd, VTOC_BOOTSIZE, SEEK_SET) != VTOC_BOOTSIZE)
608                         continue;
609                 /*
610                  * We ignore the first VTOC_BOOTSIZE bytes of boot code in
611                  * order to avoid overwriting the label.
612                  */
613                 if (lseek(fd, sizeof(struct vtoc8), SEEK_SET) !=
614                     sizeof(struct vtoc8))
615                         err(EXIT_FAILURE, "%s", dsf);
616                 if (write(fd, (caddr_t)code + sizeof(struct vtoc8),
617                     VTOC_BOOTSIZE - sizeof(struct vtoc8)) != VTOC_BOOTSIZE -
618                     sizeof(struct vtoc8))
619                         err(EXIT_FAILURE, "%s", dsf);
620                 installed++;
621                 close(fd);
622                 if (idx != 0 && atoi(s) == idx)
623                         break;
624         }
625         if (installed == 0)
626                 errx(EXIT_FAILURE, "%s: no partitions", gp->lg_name);
627 }
628
629 static void
630 gpart_bootcode(struct gctl_req *req, unsigned int fl)
631 {
632         struct gmesh mesh;
633         struct gclass *classp;
634         struct ggeom *gp;
635         const char *s;
636         char *sp;
637         void *bootcode, *partcode;
638         size_t bootsize, partsize;
639         int error, idx, vtoc8;
640
641         if (gctl_has_param(req, bootcode_param)) {
642                 s = gctl_get_ascii(req, bootcode_param);
643                 bootsize = 800 * 1024;          /* Arbitrary limit. */
644                 bootcode = gpart_bootfile_read(s, &bootsize);
645                 error = gctl_change_param(req, bootcode_param, bootsize,
646                     bootcode);
647                 if (error)
648                         errc(EXIT_FAILURE, error, "internal error");
649         } else {
650                 bootcode = NULL;
651                 bootsize = 0;
652         }
653
654         s = gctl_get_ascii(req, "class");
655         if (s == NULL)
656                 abort();
657         error = geom_gettree(&mesh);
658         if (error != 0)
659                 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
660         classp = find_class(&mesh, s);
661         if (classp == NULL) {
662                 geom_deletetree(&mesh);
663                 errx(EXIT_FAILURE, "Class %s not found.", s);
664         }
665         s = gctl_get_ascii(req, "geom");
666         if (s == NULL)
667                 abort();
668         gp = find_geom(classp, s);
669         if (gp == NULL)
670                 errx(EXIT_FAILURE, "No such geom: %s.", s);
671         s = find_geomcfg(gp, "scheme");
672         vtoc8 = 0;
673         if (strcmp(s, "VTOC8") == 0)
674                 vtoc8 = 1;
675
676         if (gctl_has_param(req, partcode_param)) {
677                 s = gctl_get_ascii(req, partcode_param);
678                 partsize = vtoc8 != 0 ? VTOC_BOOTSIZE : bootsize * 1024;
679                 partcode = gpart_bootfile_read(s, &partsize);
680                 error = gctl_delete_param(req, partcode_param);
681                 if (error)
682                         errc(EXIT_FAILURE, error, "internal error");
683         } else {
684                 partcode = NULL;
685                 partsize = 0;
686         }
687
688         if (gctl_has_param(req, index_param)) {
689                 if (partcode == NULL)
690                         errx(EXIT_FAILURE, "-i is only valid with -p");
691                 s = gctl_get_ascii(req, index_param);
692                 idx = strtol(s, &sp, 10);
693                 if (idx < 1 || *s == '\0' || *sp != '\0')
694                         errx(EXIT_FAILURE, "invalid partition index");
695                 error = gctl_delete_param(req, index_param);
696                 if (error)
697                         errc(EXIT_FAILURE, error, "internal error");
698         } else
699                 idx = 0;
700
701         if (partcode != NULL) {
702                 if (vtoc8 == 0) {
703                         if (idx == 0)
704                                 errx(EXIT_FAILURE, "missing -i option");
705                         gpart_write_partcode(gp, idx, partcode, partsize);
706                 } else
707                         gpart_write_partcode_vtoc8(gp, idx, partcode);
708         } else
709                 if (bootcode == NULL)
710                         errx(EXIT_FAILURE, "no -b nor -p");
711
712         if (bootcode != NULL)
713                 gpart_issue(req, fl);
714
715         geom_deletetree(&mesh);
716 }
717
718 static void
719 gpart_issue(struct gctl_req *req, unsigned int fl __unused)
720 {
721         char buf[4096];
722         char *errmsg;
723         const char *errstr;
724         int error, status;
725
726         /* autofill parameters (if applicable). */
727         error = gpart_autofill(req);
728         if (error) {
729                 warnc(error, "autofill");
730                 status = EXIT_FAILURE;
731                 goto done;
732         }
733
734         bzero(buf, sizeof(buf));
735         gctl_rw_param(req, "output", sizeof(buf), buf);
736         errstr = gctl_issue(req);
737         if (errstr == NULL || errstr[0] == '\0') {
738                 if (buf[0] != '\0')
739                         printf("%s", buf);
740                 status = EXIT_SUCCESS;
741                 goto done;
742         }
743
744         error = strtol(errstr, &errmsg, 0);
745         if (errmsg != errstr) {
746                 while (errmsg[0] == ' ')
747                         errmsg++;
748                 if (errmsg[0] != '\0')
749                         warnc(error, "%s", errmsg);
750                 else
751                         warnc(error, NULL);
752         } else
753                 warnx("%s", errmsg);
754
755         status = EXIT_FAILURE;
756
757  done:
758         gctl_free(req);
759         exit(status);
760 }