geom_part.c revision 179854
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: head/sbin/geom/class/part/geom_part.c 179854 2008-06-18 01:46:32Z marcel $"); 29 30#include <stdio.h> 31#include <stdint.h> 32#include <stdlib.h> 33#include <unistd.h> 34#include <err.h> 35#include <fcntl.h> 36#include <string.h> 37#include <strings.h> 38#include <libgeom.h> 39#include <paths.h> 40#include <errno.h> 41#include <assert.h> 42#include <sys/stat.h> 43 44#include "core/geom.h" 45#include "misc/subr.h" 46 47#ifdef STATIC_GEOM_CLASSES 48#define PUBSYM(x) gpart_##x 49#else 50#define PUBSYM(x) x 51#endif 52 53uint32_t PUBSYM(lib_version) = G_LIB_VERSION; 54uint32_t PUBSYM(version) = 0; 55 56static char optional[] = ""; 57static char flags[] = "C"; 58 59static char bootcode_param[] = "bootcode"; 60static char index_param[] = "index"; 61static char partcode_param[] = "partcode"; 62 63static void gpart_bootcode(struct gctl_req *, unsigned int); 64static void gpart_show(struct gctl_req *, unsigned int); 65 66struct g_command PUBSYM(class_commands)[] = { 67 { "add", 0, NULL, { 68 { 'b', "start", NULL, G_TYPE_STRING }, 69 { 's', "size", NULL, G_TYPE_STRING }, 70 { 't', "type", NULL, G_TYPE_STRING }, 71 { 'i', index_param, optional, G_TYPE_STRING }, 72 { 'l', "label", optional, G_TYPE_STRING }, 73 { 'f', "flags", flags, G_TYPE_STRING }, 74 G_OPT_SENTINEL }, 75 "geom", NULL 76 }, 77 { "bootcode", 0, gpart_bootcode, { 78 { 'b', bootcode_param, optional, G_TYPE_STRING }, 79 { 'p', partcode_param, optional, G_TYPE_STRING }, 80 { 'i', index_param, optional, G_TYPE_STRING }, 81 { 'f', "flags", flags, G_TYPE_STRING }, 82 G_OPT_SENTINEL }, 83 "geom", NULL 84 }, 85 { "commit", 0, NULL, G_NULL_OPTS, "geom", NULL }, 86 { "create", 0, NULL, { 87 { 's', "scheme", NULL, G_TYPE_STRING }, 88 { 'n', "entries", optional, G_TYPE_STRING }, 89 { 'f', "flags", flags, G_TYPE_STRING }, 90 G_OPT_SENTINEL }, 91 "provider", NULL 92 }, 93 { "delete", 0, NULL, { 94 { 'i', index_param, NULL, G_TYPE_STRING }, 95 { 'f', "flags", flags, G_TYPE_STRING }, 96 G_OPT_SENTINEL }, 97 "geom", NULL 98 }, 99 { "destroy", 0, NULL, { 100 { 'f', "flags", flags, G_TYPE_STRING }, 101 G_OPT_SENTINEL }, 102 "geom", NULL }, 103 { "modify", 0, NULL, { 104 { 'i', index_param, NULL, G_TYPE_STRING }, 105 { 'l', "label", optional, G_TYPE_STRING }, 106 { 't', "type", optional, G_TYPE_STRING }, 107 { 'f', "flags", flags, G_TYPE_STRING }, 108 G_OPT_SENTINEL }, 109 "geom", NULL 110 }, 111 { "set", 0, NULL, { 112 { 'a', "attrib", NULL, G_TYPE_STRING }, 113 { 'i', index_param, NULL, G_TYPE_STRING }, 114 { 'f', "flags", flags, G_TYPE_STRING }, 115 G_OPT_SENTINEL }, 116 "geom", NULL 117 }, 118 { "show", 0, gpart_show, { 119 { 'l', "show_label", NULL, G_TYPE_BOOL }, 120 { 'r', "show_rawtype", NULL, G_TYPE_BOOL }, 121 G_OPT_SENTINEL }, 122 NULL, "[-lr] [geom ...]" 123 }, 124 { "undo", 0, NULL, G_NULL_OPTS, "geom", NULL }, 125 { "unset", 0, NULL, { 126 { 'a', "attrib", NULL, G_TYPE_STRING }, 127 { 'i', index_param, NULL, G_TYPE_STRING }, 128 { 'f', "flags", flags, G_TYPE_STRING }, 129 G_OPT_SENTINEL }, 130 "geom", NULL 131 }, 132 G_CMD_SENTINEL 133}; 134 135static struct gclass * 136find_class(struct gmesh *mesh, const char *name) 137{ 138 struct gclass *classp; 139 140 LIST_FOREACH(classp, &mesh->lg_class, lg_class) { 141 if (strcmp(classp->lg_name, name) == 0) 142 return (classp); 143 } 144 return (NULL); 145} 146 147static struct ggeom * 148find_geom(struct gclass *classp, const char *name) 149{ 150 struct ggeom *gp; 151 152 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 153 if (strcmp(gp->lg_name, name) == 0) 154 return (gp); 155 } 156 return (NULL); 157} 158 159static const char * 160find_geomcfg(struct ggeom *gp, const char *cfg) 161{ 162 struct gconfig *gc; 163 164 LIST_FOREACH(gc, &gp->lg_config, lg_config) { 165 if (!strcmp(gc->lg_name, cfg)) 166 return (gc->lg_val); 167 } 168 return (NULL); 169} 170 171static const char * 172find_provcfg(struct gprovider *pp, const char *cfg) 173{ 174 struct gconfig *gc; 175 176 LIST_FOREACH(gc, &pp->lg_config, lg_config) { 177 if (!strcmp(gc->lg_name, cfg)) 178 return (gc->lg_val); 179 } 180 return (NULL); 181} 182 183static struct gprovider * 184find_provider(struct ggeom *gp, unsigned long long minsector) 185{ 186 struct gprovider *pp, *bestpp; 187 unsigned long long offset; 188 unsigned long long sector, bestsector; 189 190 bestpp = NULL; 191 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) { 192 offset = atoll(find_provcfg(pp, "offset")); 193 sector = offset / pp->lg_sectorsize; 194 if (sector < minsector) 195 continue; 196 if (bestpp != NULL && sector >= bestsector) 197 continue; 198 bestpp = pp; 199 bestsector = sector; 200 } 201 return (bestpp); 202} 203 204static const char * 205fmtsize(long double rawsz) 206{ 207 static char buf[32]; 208 static const char *sfx[] = { "B", "KB", "MB", "GB", "TB" }; 209 long double sz; 210 int sfxidx; 211 212 sfxidx = 0; 213 sz = (long double)rawsz; 214 while (sfxidx < 4 && sz > 1099.0) { 215 sz /= 1000; 216 sfxidx++; 217 } 218 219 sprintf(buf, "%.1Lf%s", sz, sfx[sfxidx]); 220 return (buf); 221} 222 223static const char * 224fmtattrib(struct gprovider *pp) 225{ 226 static char buf[64]; 227 const char *val; 228 229 val = find_provcfg(pp, "attrib"); 230 if (val == NULL) 231 return (""); 232 snprintf(buf, sizeof(buf), " [%s] ", val); 233 return (buf); 234} 235 236static void 237gpart_show_geom(struct ggeom *gp, const char *element) 238{ 239 struct gprovider *pp; 240 const char *s, *scheme; 241 unsigned long long first, last, sector, end; 242 unsigned long long offset, length, secsz; 243 int idx, wblocks, wname; 244 245 scheme = find_geomcfg(gp, "scheme"); 246 s = find_geomcfg(gp, "first"); 247 first = atoll(s); 248 s = find_geomcfg(gp, "last"); 249 last = atoll(s); 250 wblocks = strlen(s); 251 wname = strlen(gp->lg_name); 252 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider; 253 secsz = pp->lg_sectorsize; 254 printf("=>%*llu %*llu %*s %s (%s)\n", 255 wblocks, first, wblocks, (last - first + 1), 256 wname, gp->lg_name, 257 scheme, fmtsize(pp->lg_mediasize)); 258 259 while ((pp = find_provider(gp, first)) != NULL) { 260 s = find_provcfg(pp, "offset"); 261 offset = atoll(s); 262 sector = offset / secsz; 263 s = find_provcfg(pp, "length"); 264 length = atoll(s); 265 s = find_provcfg(pp, "index"); 266 idx = atoi(s); 267 end = sector + length / secsz; 268 if (first < sector) { 269 printf(" %*llu %*llu %*s - free - (%s)\n", 270 wblocks, first, wblocks, sector - first, 271 wname, "", 272 fmtsize((sector - first) * secsz)); 273 } 274 printf(" %*llu %*llu %*d %s %s (%s)\n", 275 wblocks, sector, wblocks, end - sector, 276 wname, idx, find_provcfg(pp, element), 277 fmtattrib(pp), fmtsize(pp->lg_mediasize)); 278 first = end; 279 } 280 if (first <= last) { 281 printf(" %*llu %*llu %*s - free - (%s)\n", 282 wblocks, first, wblocks, last - first + 1, 283 wname, "", 284 fmtsize((last - first + 1) * secsz)); 285 } 286 printf("\n"); 287} 288 289static int 290gpart_show_hasopt(struct gctl_req *req, const char *opt, const char *elt) 291{ 292 293 if (!gctl_get_int(req, opt)) 294 return (0); 295 296 if (elt != NULL) 297 errx(EXIT_FAILURE, "-l and -r are mutually exclusive"); 298 299 return (1); 300} 301 302static void 303gpart_show(struct gctl_req *req, unsigned int fl __unused) 304{ 305 struct gmesh mesh; 306 struct gclass *classp; 307 struct ggeom *gp; 308 const char *element, *name; 309 int error, i, nargs; 310 311 element = NULL; 312 if (gpart_show_hasopt(req, "show_label", element)) 313 element = "label"; 314 if (gpart_show_hasopt(req, "show_rawtype", element)) 315 element = "rawtype"; 316 if (element == NULL) 317 element = "type"; 318 319 name = gctl_get_ascii(req, "class"); 320 if (name == NULL) 321 abort(); 322 error = geom_gettree(&mesh); 323 if (error != 0) 324 errc(EXIT_FAILURE, error, "Cannot get GEOM tree"); 325 classp = find_class(&mesh, name); 326 if (classp == NULL) { 327 geom_deletetree(&mesh); 328 errx(EXIT_FAILURE, "Class %s not found.", name); 329 } 330 nargs = gctl_get_int(req, "nargs"); 331 if (nargs > 0) { 332 for (i = 0; i < nargs; i++) { 333 name = gctl_get_ascii(req, "arg%d", i); 334 gp = find_geom(classp, name); 335 if (gp != NULL) 336 gpart_show_geom(gp, element); 337 else 338 errx(EXIT_FAILURE, "No such geom: %s.", name); 339 } 340 } else { 341 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 342 gpart_show_geom(gp, element); 343 } 344 } 345 geom_deletetree(&mesh); 346} 347 348static void * 349gpart_bootfile_read(const char *bootfile, ssize_t *size) 350{ 351 struct stat sb; 352 void *code; 353 int fd; 354 355 if (stat(bootfile, &sb) == -1) 356 err(EXIT_FAILURE, "%s", bootfile); 357 if (!S_ISREG(sb.st_mode)) 358 errx(EXIT_FAILURE, "%s: not a regular file", bootfile); 359 if (sb.st_size == 0) 360 errx(EXIT_FAILURE, "%s: empty file", bootfile); 361 if (*size > 0 && sb.st_size >= *size) 362 errx(EXIT_FAILURE, "%s: file too big (%zu limit)", bootfile, 363 *size); 364 365 *size = sb.st_size; 366 367 fd = open(bootfile, O_RDONLY); 368 if (fd == -1) 369 err(EXIT_FAILURE, "%s", bootfile); 370 code = malloc(*size); 371 if (code == NULL) 372 err(EXIT_FAILURE, NULL); 373 if (read(fd, code, *size) != *size) 374 err(EXIT_FAILURE, "%s", bootfile); 375 close(fd); 376 377 return (code); 378} 379 380static void 381gpart_write_partcode(struct gctl_req *req, int idx, void *code, ssize_t size) 382{ 383 char dsf[128]; 384 struct gmesh mesh; 385 struct gclass *classp; 386 struct ggeom *gp; 387 struct gprovider *pp; 388 const char *s; 389 int error, fd; 390 391 s = gctl_get_ascii(req, "class"); 392 if (s == NULL) 393 abort(); 394 error = geom_gettree(&mesh); 395 if (error != 0) 396 errc(EXIT_FAILURE, error, "Cannot get GEOM tree"); 397 classp = find_class(&mesh, s); 398 if (classp == NULL) { 399 geom_deletetree(&mesh); 400 errx(EXIT_FAILURE, "Class %s not found.", s); 401 } 402 s = gctl_get_ascii(req, "geom"); 403 gp = find_geom(classp, s); 404 if (gp == NULL) 405 errx(EXIT_FAILURE, "No such geom: %s.", s); 406 407 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) { 408 s = find_provcfg(pp, "index"); 409 if (s == NULL) 410 continue; 411 if (atoi(s) == idx) 412 break; 413 } 414 415 if (pp != NULL) { 416 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name); 417 fd = open(dsf, O_WRONLY); 418 if (fd == -1) 419 err(EXIT_FAILURE, "%s", dsf); 420 if (lseek(fd, size, SEEK_SET) != size) 421 errx(EXIT_FAILURE, "%s: not enough space", dsf); 422 if (lseek(fd, 0, SEEK_SET) != 0) 423 err(EXIT_FAILURE, "%s", dsf); 424 if (write(fd, code, size) != size) 425 err(EXIT_FAILURE, "%s", dsf); 426 close(fd); 427 } else 428 errx(EXIT_FAILURE, "invalid partition index"); 429 430 geom_deletetree(&mesh); 431} 432 433static void 434gpart_bootcode(struct gctl_req *req, unsigned int fl __unused) 435{ 436 const char *s; 437 char *sp; 438 void *bootcode, *partcode; 439 size_t bootsize, partsize; 440 int error, idx; 441 442 if (gctl_has_param(req, bootcode_param)) { 443 s = gctl_get_ascii(req, bootcode_param); 444 bootsize = 64 * 1024; /* Arbitrary limit. */ 445 bootcode = gpart_bootfile_read(s, &bootsize); 446 error = gctl_change_param(req, bootcode_param, bootsize, 447 bootcode); 448 if (error) 449 errc(EXIT_FAILURE, error, "internal error"); 450 } else { 451 bootcode = NULL; 452 bootsize = 0; 453 } 454 455 if (gctl_has_param(req, partcode_param)) { 456 s = gctl_get_ascii(req, partcode_param); 457 partsize = bootsize * 1024; 458 partcode = gpart_bootfile_read(s, &partsize); 459 error = gctl_delete_param(req, partcode_param); 460 if (error) 461 errc(EXIT_FAILURE, error, "internal error"); 462 } else { 463 partcode = NULL; 464 partsize = 0; 465 } 466 467 if (gctl_has_param(req, index_param)) { 468 if (partcode == NULL) 469 errx(EXIT_FAILURE, "-i is only valid with -p"); 470 s = gctl_get_ascii(req, index_param); 471 idx = strtol(s, &sp, 10); 472 if (idx < 1 || *s == '\0' || *sp != '\0') 473 errx(EXIT_FAILURE, "invalid partition index"); 474 error = gctl_delete_param(req, index_param); 475 if (error) 476 errc(EXIT_FAILURE, error, "internal error"); 477 } else 478 idx = 0; 479 480 if (partcode != NULL) { 481 if (idx == 0) 482 errx(EXIT_FAILURE, "missing -i option"); 483 gpart_write_partcode(req, idx, partcode, partsize); 484 } else { 485 if (bootcode == NULL) 486 errx(EXIT_FAILURE, "no -b nor -p"); 487 } 488 489 if (bootcode != NULL) { 490 s = gctl_issue(req); 491 if (s != NULL) 492 errx(EXIT_FAILURE, "%s", s); 493 } 494} 495