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$");
46 #include "core/geom.h"
47 #include "misc/subr.h"
49 #ifdef STATIC_GEOM_CLASSES
50 #define PUBSYM(x) gpart_##x
55 uint32_t PUBSYM(lib_version) = G_LIB_VERSION;
56 uint32_t PUBSYM(version) = 0;
58 static char optional[] = "";
59 static char flags[] = "C";
61 static char bootcode_param[] = "bootcode";
62 static char index_param[] = "index";
63 static char partcode_param[] = "partcode";
65 static void gpart_bootcode(struct gctl_req *, unsigned int);
66 static void gpart_issue(struct gctl_req *, unsigned int);
67 static void gpart_show(struct gctl_req *, unsigned int);
69 struct g_command PUBSYM(class_commands)[] = {
70 { "add", 0, gpart_issue, {
71 { 'b', "start", NULL, G_TYPE_STRING },
72 { 's', "size", NULL, G_TYPE_STRING },
73 { 't', "type", NULL, G_TYPE_STRING },
74 { 'i', index_param, optional, G_TYPE_STRING },
75 { 'l', "label", optional, G_TYPE_STRING },
76 { 'f', "flags", flags, G_TYPE_STRING },
80 { "bootcode", 0, gpart_bootcode, {
81 { 'b', bootcode_param, optional, G_TYPE_STRING },
82 { 'p', partcode_param, optional, G_TYPE_STRING },
83 { 'i', index_param, optional, G_TYPE_STRING },
84 { 'f', "flags", flags, G_TYPE_STRING },
88 { "commit", 0, gpart_issue, G_NULL_OPTS, "geom", NULL },
89 { "create", 0, gpart_issue, {
90 { 's', "scheme", NULL, G_TYPE_STRING },
91 { 'n', "entries", optional, G_TYPE_STRING },
92 { 'f', "flags", flags, G_TYPE_STRING },
96 { "delete", 0, gpart_issue, {
97 { 'i', index_param, NULL, G_TYPE_STRING },
98 { 'f', "flags", flags, G_TYPE_STRING },
102 { "destroy", 0, gpart_issue, {
103 { 'f', "flags", flags, G_TYPE_STRING },
106 { "modify", 0, gpart_issue, {
107 { 'i', index_param, NULL, G_TYPE_STRING },
108 { 'l', "label", optional, G_TYPE_STRING },
109 { 't', "type", optional, G_TYPE_STRING },
110 { 'f', "flags", flags, G_TYPE_STRING },
114 { "set", 0, gpart_issue, {
115 { 'a', "attrib", NULL, G_TYPE_STRING },
116 { 'i', index_param, NULL, G_TYPE_STRING },
117 { 'f', "flags", flags, G_TYPE_STRING },
121 { "show", 0, gpart_show, {
122 { 'l', "show_label", NULL, G_TYPE_BOOL },
123 { 'r', "show_rawtype", NULL, G_TYPE_BOOL },
125 NULL, "[-lr] [geom ...]"
127 { "undo", 0, gpart_issue, G_NULL_OPTS, "geom", NULL },
128 { "unset", 0, gpart_issue, {
129 { 'a', "attrib", NULL, G_TYPE_STRING },
130 { 'i', index_param, NULL, G_TYPE_STRING },
131 { 'f', "flags", flags, G_TYPE_STRING },
138 static struct gclass *
139 find_class(struct gmesh *mesh, const char *name)
141 struct gclass *classp;
143 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
144 if (strcmp(classp->lg_name, name) == 0)
150 static struct ggeom *
151 find_geom(struct gclass *classp, const char *name)
155 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
156 if (strcmp(gp->lg_name, name) == 0)
163 find_geomcfg(struct ggeom *gp, const char *cfg)
167 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
168 if (!strcmp(gc->lg_name, cfg))
175 find_provcfg(struct gprovider *pp, const char *cfg)
179 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
180 if (!strcmp(gc->lg_name, cfg))
186 static struct gprovider *
187 find_provider(struct ggeom *gp, unsigned long long minsector)
189 struct gprovider *pp, *bestpp;
191 unsigned long long sector, bestsector;
194 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
195 s = find_provcfg(pp, "start");
197 s = find_provcfg(pp, "offset");
198 sector = atoll(s) / pp->lg_sectorsize;
202 if (sector < minsector)
204 if (bestpp != NULL && sector >= bestsector)
214 fmtsize(int64_t rawsz)
218 humanize_number(buf, sizeof(buf), rawsz, "", HN_AUTOSCALE,
219 HN_B | HN_NOSPACE | HN_DECIMAL);
224 fmtattrib(struct gprovider *pp)
226 static char buf[128];
232 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
233 if (strcmp(gc->lg_name, "attrib") != 0)
235 idx += snprintf(buf + idx, sizeof(buf) - idx, "%s%s",
236 (idx == 0) ? " [" : ",", gc->lg_val);
239 snprintf(buf + idx, sizeof(buf) - idx, "] ");
244 gpart_show_geom(struct ggeom *gp, const char *element)
246 struct gprovider *pp;
247 const char *s, *scheme;
248 unsigned long long first, last, sector, end;
249 unsigned long long length, secsz;
250 int idx, wblocks, wname;
252 scheme = find_geomcfg(gp, "scheme");
253 s = find_geomcfg(gp, "first");
255 s = find_geomcfg(gp, "last");
258 wname = strlen(gp->lg_name);
259 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
260 secsz = pp->lg_sectorsize;
261 printf("=>%*llu %*llu %*s %s (%s)\n",
262 wblocks, first, wblocks, (last - first + 1),
264 scheme, fmtsize(pp->lg_mediasize));
266 while ((pp = find_provider(gp, first)) != NULL) {
267 s = find_provcfg(pp, "start");
269 s = find_provcfg(pp, "offset");
270 sector = atoll(s) / secsz;
274 s = find_provcfg(pp, "end");
276 s = find_provcfg(pp, "length");
277 length = atoll(s) / secsz;
278 end = sector + length - 1;
281 length = end - sector + 1;
283 s = find_provcfg(pp, "index");
285 if (first < sector) {
286 printf(" %*llu %*llu %*s - free - (%s)\n",
287 wblocks, first, wblocks, sector - first,
289 fmtsize((sector - first) * secsz));
291 printf(" %*llu %*llu %*d %s %s (%s)\n",
292 wblocks, sector, wblocks, length,
293 wname, idx, find_provcfg(pp, element),
294 fmtattrib(pp), fmtsize(pp->lg_mediasize));
298 length = last - first + 1;
299 printf(" %*llu %*llu %*s - free - (%s)\n",
300 wblocks, first, wblocks, length,
302 fmtsize(length * secsz));
308 gpart_show_hasopt(struct gctl_req *req, const char *opt, const char *elt)
311 if (!gctl_get_int(req, opt))
315 errx(EXIT_FAILURE, "-l and -r are mutually exclusive");
321 gpart_show(struct gctl_req *req, unsigned int fl __unused)
324 struct gclass *classp;
326 const char *element, *name;
330 if (gpart_show_hasopt(req, "show_label", element))
332 if (gpart_show_hasopt(req, "show_rawtype", element))
337 name = gctl_get_ascii(req, "class");
340 error = geom_gettree(&mesh);
342 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
343 classp = find_class(&mesh, name);
344 if (classp == NULL) {
345 geom_deletetree(&mesh);
346 errx(EXIT_FAILURE, "Class %s not found.", name);
348 nargs = gctl_get_int(req, "nargs");
350 for (i = 0; i < nargs; i++) {
351 name = gctl_get_ascii(req, "arg%d", i);
352 gp = find_geom(classp, name);
354 gpart_show_geom(gp, element);
356 errx(EXIT_FAILURE, "No such geom: %s.", name);
359 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
360 gpart_show_geom(gp, element);
363 geom_deletetree(&mesh);
367 gpart_bootfile_read(const char *bootfile, ssize_t *size)
373 if (stat(bootfile, &sb) == -1)
374 err(EXIT_FAILURE, "%s", bootfile);
375 if (!S_ISREG(sb.st_mode))
376 errx(EXIT_FAILURE, "%s: not a regular file", bootfile);
378 errx(EXIT_FAILURE, "%s: empty file", bootfile);
379 if (*size > 0 && sb.st_size >= *size)
380 errx(EXIT_FAILURE, "%s: file too big (%zu limit)", bootfile,
385 fd = open(bootfile, O_RDONLY);
387 err(EXIT_FAILURE, "%s", bootfile);
388 code = malloc(*size);
390 err(EXIT_FAILURE, NULL);
391 if (read(fd, code, *size) != *size)
392 err(EXIT_FAILURE, "%s", bootfile);
399 gpart_write_partcode(struct gctl_req *req, int idx, void *code, ssize_t size)
403 struct gclass *classp;
405 struct gprovider *pp;
411 s = gctl_get_ascii(req, "class");
414 error = geom_gettree(&mesh);
416 errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
417 classp = find_class(&mesh, s);
418 if (classp == NULL) {
419 geom_deletetree(&mesh);
420 errx(EXIT_FAILURE, "Class %s not found.", s);
422 s = gctl_get_ascii(req, "geom");
423 gp = find_geom(classp, s);
425 errx(EXIT_FAILURE, "No such geom: %s.", s);
427 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
428 s = find_provcfg(pp, "index");
436 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
437 fd = open(dsf, O_WRONLY);
439 err(EXIT_FAILURE, "%s", dsf);
440 if (lseek(fd, size, SEEK_SET) != size)
441 errx(EXIT_FAILURE, "%s: not enough space", dsf);
442 if (lseek(fd, 0, SEEK_SET) != 0)
443 err(EXIT_FAILURE, "%s", dsf);
446 * When writing to a disk device, the write must be
447 * sector aligned and not write to any partial sectors,
448 * so round up the buffer size to the next sector and zero it.
450 bsize = (size + pp->lg_sectorsize - 1) /
451 pp->lg_sectorsize * pp->lg_sectorsize;
452 buf = calloc(1, bsize);
454 err(EXIT_FAILURE, "%s", dsf);
455 bcopy(code, buf, size);
456 if (write(fd, buf, bsize) != bsize)
457 err(EXIT_FAILURE, "%s", dsf);
461 errx(EXIT_FAILURE, "invalid partition index");
463 geom_deletetree(&mesh);
467 gpart_bootcode(struct gctl_req *req, unsigned int fl)
471 void *bootcode, *partcode;
472 size_t bootsize, partsize;
475 if (gctl_has_param(req, bootcode_param)) {
476 s = gctl_get_ascii(req, bootcode_param);
477 bootsize = 64 * 1024; /* Arbitrary limit. */
478 bootcode = gpart_bootfile_read(s, &bootsize);
479 error = gctl_change_param(req, bootcode_param, bootsize,
482 errc(EXIT_FAILURE, error, "internal error");
488 if (gctl_has_param(req, partcode_param)) {
489 s = gctl_get_ascii(req, partcode_param);
490 partsize = bootsize * 1024;
491 partcode = gpart_bootfile_read(s, &partsize);
492 error = gctl_delete_param(req, partcode_param);
494 errc(EXIT_FAILURE, error, "internal error");
500 if (gctl_has_param(req, index_param)) {
501 if (partcode == NULL)
502 errx(EXIT_FAILURE, "-i is only valid with -p");
503 s = gctl_get_ascii(req, index_param);
504 idx = strtol(s, &sp, 10);
505 if (idx < 1 || *s == '\0' || *sp != '\0')
506 errx(EXIT_FAILURE, "invalid partition index");
507 error = gctl_delete_param(req, index_param);
509 errc(EXIT_FAILURE, error, "internal error");
513 if (partcode != NULL) {
515 errx(EXIT_FAILURE, "missing -i option");
516 gpart_write_partcode(req, idx, partcode, partsize);
518 if (bootcode == NULL)
519 errx(EXIT_FAILURE, "no -b nor -p");
522 if (bootcode != NULL)
523 gpart_issue(req, fl);
527 gpart_issue(struct gctl_req *req, unsigned int fl __unused)
534 bzero(buf, sizeof(buf));
535 gctl_rw_param(req, "output", sizeof(buf), buf);
536 errstr = gctl_issue(req);
537 if (errstr == NULL || errstr[0] == '\0') {
540 status = EXIT_SUCCESS;
544 error = strtol(errstr, &errmsg, 0);
545 if (errmsg != errstr) {
546 while (errmsg[0] == ' ')
548 if (errmsg[0] != '\0')
549 warnc(error, "%s", errmsg);
555 status = EXIT_FAILURE;