geom_part.c revision 179769
1/*- 2 * Copyright (c) 2007 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 179769 2008-06-13 00:04:10Z 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 { "show", 0, gpart_show, { 112 { 'l', "show_label", NULL, G_TYPE_BOOL }, 113 { 'r', "show_rawtype", NULL, G_TYPE_BOOL }, 114 G_OPT_SENTINEL }, 115 NULL, "[-lr] [geom ...]" 116 }, 117 { "undo", 0, NULL, G_NULL_OPTS, "geom", NULL }, 118 G_CMD_SENTINEL 119}; 120 121static struct gclass * 122find_class(struct gmesh *mesh, const char *name) 123{ 124 struct gclass *classp; 125 126 LIST_FOREACH(classp, &mesh->lg_class, lg_class) { 127 if (strcmp(classp->lg_name, name) == 0) 128 return (classp); 129 } 130 return (NULL); 131} 132 133static struct ggeom * 134find_geom(struct gclass *classp, const char *name) 135{ 136 struct ggeom *gp; 137 138 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 139 if (strcmp(gp->lg_name, name) == 0) 140 return (gp); 141 } 142 return (NULL); 143} 144 145static const char * 146find_geomcfg(struct ggeom *gp, const char *cfg) 147{ 148 struct gconfig *gc; 149 150 LIST_FOREACH(gc, &gp->lg_config, lg_config) { 151 if (!strcmp(gc->lg_name, cfg)) 152 return (gc->lg_val); 153 } 154 return (NULL); 155} 156 157static const char * 158find_provcfg(struct gprovider *pp, const char *cfg) 159{ 160 struct gconfig *gc; 161 162 LIST_FOREACH(gc, &pp->lg_config, lg_config) { 163 if (!strcmp(gc->lg_name, cfg)) 164 return (gc->lg_val); 165 } 166 return (NULL); 167} 168 169static struct gprovider * 170find_provider(struct ggeom *gp, unsigned long long minsector) 171{ 172 struct gprovider *pp, *bestpp; 173 unsigned long long offset; 174 unsigned long long sector, bestsector; 175 176 bestpp = NULL; 177 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) { 178 offset = atoll(find_provcfg(pp, "offset")); 179 sector = offset / pp->lg_sectorsize; 180 if (sector < minsector) 181 continue; 182 if (bestpp != NULL && sector >= bestsector) 183 continue; 184 bestpp = pp; 185 bestsector = sector; 186 } 187 return (bestpp); 188} 189 190static const char * 191fmtsize(long double rawsz) 192{ 193 static char buf[32]; 194 static const char *sfx[] = { "B", "KB", "MB", "GB", "TB" }; 195 long double sz; 196 int sfxidx; 197 198 sfxidx = 0; 199 sz = (long double)rawsz; 200 while (sfxidx < 4 && sz > 1099.0) { 201 sz /= 1000; 202 sfxidx++; 203 } 204 205 sprintf(buf, "%.1Lf%s", sz, sfx[sfxidx]); 206 return (buf); 207} 208 209static void 210gpart_show_geom(struct ggeom *gp, const char *element) 211{ 212 struct gprovider *pp; 213 const char *s, *scheme; 214 unsigned long long first, last, sector, end; 215 unsigned long long offset, length, secsz; 216 int idx, wblocks, wname; 217 218 scheme = find_geomcfg(gp, "scheme"); 219 s = find_geomcfg(gp, "first"); 220 first = atoll(s); 221 s = find_geomcfg(gp, "last"); 222 last = atoll(s); 223 wblocks = strlen(s); 224 wname = strlen(gp->lg_name); 225 pp = LIST_FIRST(&gp->lg_consumer)->lg_provider; 226 secsz = pp->lg_sectorsize; 227 printf("=>%*llu %*llu %*s %s (%s)\n", 228 wblocks, first, wblocks, (last - first + 1), 229 wname, gp->lg_name, 230 scheme, fmtsize(pp->lg_mediasize)); 231 232 while ((pp = find_provider(gp, first)) != NULL) { 233 s = find_provcfg(pp, "offset"); 234 offset = atoll(s); 235 sector = offset / secsz; 236 s = find_provcfg(pp, "length"); 237 length = atoll(s); 238 s = find_provcfg(pp, "index"); 239 idx = atoi(s); 240 end = sector + length / secsz; 241 if (first < sector) { 242 printf(" %*llu %*llu %*s - free - (%s)\n", 243 wblocks, first, wblocks, sector - first, 244 wname, "", 245 fmtsize((sector - first) * secsz)); 246 } 247 printf(" %*llu %*llu %*d %s (%s)\n", 248 wblocks, sector, wblocks, end - sector, 249 wname, idx, 250 find_provcfg(pp, element), fmtsize(pp->lg_mediasize)); 251 first = end; 252 } 253 if (first <= last) { 254 printf(" %*llu %*llu %*s - free - (%s)\n", 255 wblocks, first, wblocks, last - first + 1, 256 wname, "", 257 fmtsize((last - first + 1) * secsz)); 258 } 259 printf("\n"); 260} 261 262static int 263gpart_show_hasopt(struct gctl_req *req, const char *opt, const char *elt) 264{ 265 266 if (!gctl_get_int(req, opt)) 267 return (0); 268 269 if (elt != NULL) 270 errx(EXIT_FAILURE, "-l and -r are mutually exclusive"); 271 272 return (1); 273} 274 275static void 276gpart_show(struct gctl_req *req, unsigned int fl __unused) 277{ 278 struct gmesh mesh; 279 struct gclass *classp; 280 struct ggeom *gp; 281 const char *element, *name; 282 int error, i, nargs; 283 284 element = NULL; 285 if (gpart_show_hasopt(req, "show_label", element)) 286 element = "label"; 287 if (gpart_show_hasopt(req, "show_rawtype", element)) 288 element = "rawtype"; 289 if (element == NULL) 290 element = "type"; 291 292 name = gctl_get_ascii(req, "class"); 293 if (name == NULL) 294 abort(); 295 error = geom_gettree(&mesh); 296 if (error != 0) 297 errc(EXIT_FAILURE, error, "Cannot get GEOM tree"); 298 classp = find_class(&mesh, name); 299 if (classp == NULL) { 300 geom_deletetree(&mesh); 301 errx(EXIT_FAILURE, "Class %s not found.", name); 302 } 303 nargs = gctl_get_int(req, "nargs"); 304 if (nargs > 0) { 305 for (i = 0; i < nargs; i++) { 306 name = gctl_get_ascii(req, "arg%d", i); 307 gp = find_geom(classp, name); 308 if (gp != NULL) 309 gpart_show_geom(gp, element); 310 else 311 errx(EXIT_FAILURE, "No such geom: %s.", name); 312 } 313 } else { 314 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 315 gpart_show_geom(gp, element); 316 } 317 } 318 geom_deletetree(&mesh); 319} 320 321static void * 322gpart_bootfile_read(const char *bootfile, ssize_t *size) 323{ 324 struct stat sb; 325 void *code; 326 int fd; 327 328 if (stat(bootfile, &sb) == -1) 329 err(EXIT_FAILURE, "%s", bootfile); 330 if (!S_ISREG(sb.st_mode)) 331 errx(EXIT_FAILURE, "%s: not a regular file", bootfile); 332 if (sb.st_size == 0) 333 errx(EXIT_FAILURE, "%s: empty file", bootfile); 334 if (*size > 0 && sb.st_size >= *size) 335 errx(EXIT_FAILURE, "%s: file too big (%zu limit)", bootfile, 336 *size); 337 338 *size = sb.st_size; 339 340 fd = open(bootfile, O_RDONLY); 341 if (fd == -1) 342 err(EXIT_FAILURE, "%s", bootfile); 343 code = malloc(*size); 344 if (code == NULL) 345 err(EXIT_FAILURE, NULL); 346 if (read(fd, code, *size) != *size) 347 err(EXIT_FAILURE, "%s", bootfile); 348 close(fd); 349 350 return (code); 351} 352 353static void 354gpart_write_partcode(struct gctl_req *req, int idx, void *code, ssize_t size) 355{ 356 char dsf[128]; 357 struct gmesh mesh; 358 struct gclass *classp; 359 struct ggeom *gp; 360 struct gprovider *pp; 361 const char *s; 362 int error, fd; 363 364 s = gctl_get_ascii(req, "class"); 365 if (s == NULL) 366 abort(); 367 error = geom_gettree(&mesh); 368 if (error != 0) 369 errc(EXIT_FAILURE, error, "Cannot get GEOM tree"); 370 classp = find_class(&mesh, s); 371 if (classp == NULL) { 372 geom_deletetree(&mesh); 373 errx(EXIT_FAILURE, "Class %s not found.", s); 374 } 375 s = gctl_get_ascii(req, "geom"); 376 gp = find_geom(classp, s); 377 if (gp == NULL) 378 errx(EXIT_FAILURE, "No such geom: %s.", s); 379 380 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) { 381 s = find_provcfg(pp, "index"); 382 if (s == NULL) 383 continue; 384 if (atoi(s) == idx) 385 break; 386 } 387 388 if (pp != NULL) { 389 snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name); 390 fd = open(dsf, O_WRONLY); 391 if (fd == -1) 392 err(EXIT_FAILURE, "%s", dsf); 393 if (lseek(fd, size, SEEK_SET) != size) 394 errx(EXIT_FAILURE, "%s: not enough space", dsf); 395 if (lseek(fd, 0, SEEK_SET) != 0) 396 err(EXIT_FAILURE, "%s", dsf); 397 if (write(fd, code, size) != size) 398 err(EXIT_FAILURE, "%s", dsf); 399 close(fd); 400 } else 401 errx(EXIT_FAILURE, "invalid partition index"); 402 403 geom_deletetree(&mesh); 404} 405 406static void 407gpart_bootcode(struct gctl_req *req, unsigned int fl __unused) 408{ 409 const char *s; 410 char *sp; 411 void *bootcode, *partcode; 412 size_t bootsize, partsize; 413 int error, idx; 414 415 if (gctl_has_param(req, bootcode_param)) { 416 s = gctl_get_ascii(req, bootcode_param); 417 bootsize = 64 * 1024; /* Arbitrary limit. */ 418 bootcode = gpart_bootfile_read(s, &bootsize); 419 error = gctl_change_param(req, bootcode_param, bootsize, 420 bootcode); 421 if (error) 422 errc(EXIT_FAILURE, error, "internal error"); 423 } else { 424 bootcode = NULL; 425 bootsize = 0; 426 } 427 428 if (gctl_has_param(req, partcode_param)) { 429 s = gctl_get_ascii(req, partcode_param); 430 partsize = bootsize * 1024; 431 partcode = gpart_bootfile_read(s, &partsize); 432 error = gctl_delete_param(req, partcode_param); 433 if (error) 434 errc(EXIT_FAILURE, error, "internal error"); 435 } else { 436 partcode = NULL; 437 partsize = 0; 438 } 439 440 if (gctl_has_param(req, index_param)) { 441 if (partcode == NULL) 442 errx(EXIT_FAILURE, "-i is only valid with -p"); 443 s = gctl_get_ascii(req, index_param); 444 idx = strtol(s, &sp, 10); 445 if (idx < 1 || *s == '\0' || *sp != '\0') 446 errx(EXIT_FAILURE, "invalid partition index"); 447 error = gctl_delete_param(req, index_param); 448 if (error) 449 errc(EXIT_FAILURE, error, "internal error"); 450 } else 451 idx = 0; 452 453 if (partcode != NULL) { 454 if (idx == 0) 455 errx(EXIT_FAILURE, "missing -i option"); 456 gpart_write_partcode(req, idx, partcode, partsize); 457 } else { 458 if (bootcode == NULL) 459 errx(EXIT_FAILURE, "no -b nor -p"); 460 } 461 462 if (bootcode != NULL) { 463 s = gctl_issue(req); 464 if (s != NULL) 465 errx(EXIT_FAILURE, "%s", s); 466 } 467} 468