1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/stat.h> 33 #include <sys/types.h> 34 35 #include <ctype.h> 36 #include <kenv.h> 37 #include <libgen.h> 38 #include <libzfs_core.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <time.h> 42 #include <unistd.h> 43 44 #include "be.h" 45 #include "be_impl.h" 46 47 /* 48 * Iterator function for locating the rootfs amongst the children of the 49 * zfs_be_root set by loader(8). data is expected to be a libbe_handle_t *. 50 */ 51 static int 52 be_locate_rootfs(zfs_handle_t *chkds, void *data) 53 { 54 libbe_handle_t *lbh; 55 char *mntpoint; 56 57 lbh = (libbe_handle_t *)data; 58 if (lbh == NULL) 59 return (1); 60 61 if (zfs_is_mounted(chkds, &mntpoint) && strcmp(mntpoint, "/") == 0) { 62 strncpy(lbh->rootfs, zfs_get_name(chkds), BE_MAXPATHLEN); 63 return (1); 64 } 65 66 return (0); 67 } 68 69 /* 70 * Initializes the libbe context to operate in the root boot environment 71 * dataset, for example, zroot/ROOT. 72 */ 73 libbe_handle_t * 74 libbe_init(void) 75 { 76 struct stat sb; 77 dev_t root_dev, boot_dev; 78 libbe_handle_t *lbh; 79 zfs_handle_t *rootds; 80 char *poolname, *pos; 81 int pnamelen; 82 83 lbh = NULL; 84 poolname = pos = NULL; 85 pnamelen = 0; 86 rootds = NULL; 87 88 /* Verify that /boot and / are mounted on the same filesystem */ 89 /* TODO: use errno here?? */ 90 if (stat("/", &sb) != 0) 91 goto err; 92 93 root_dev = sb.st_dev; 94 95 if (stat("/boot", &sb) != 0) 96 goto err; 97 98 boot_dev = sb.st_dev; 99 100 if (root_dev != boot_dev) { 101 fprintf(stderr, "/ and /boot not on same device, quitting\n"); 102 goto err; 103 } 104 105 if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL) 106 goto err; 107 108 if ((lbh->lzh = libzfs_init()) == NULL) 109 goto err; 110 111 /* Obtain path to boot environment root */ 112 if ((kenv(KENV_GET, "zfs_be_root", lbh->root, BE_MAXPATHLEN)) == -1) 113 goto err; 114 115 /* Remove leading 'zfs:' if present, otherwise use value as-is */ 116 if (strcmp(lbh->root, "zfs:") == 0) 117 strncpy(lbh->root, strchr(lbh->root, ':') + sizeof(char), 118 BE_MAXPATHLEN); 119 120 if ((pos = strchr(lbh->root, '/')) == NULL) 121 goto err; 122 123 pnamelen = pos - lbh->root; 124 poolname = malloc(pnamelen + 1); 125 if (poolname == NULL) 126 goto err; 127 128 strncpy(poolname, lbh->root, pnamelen); 129 poolname[pnamelen] = '\0'; 130 if ((lbh->active_phandle = zpool_open(lbh->lzh, poolname)) == NULL) 131 goto err; 132 133 if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_BOOTFS, lbh->bootfs, 134 BE_MAXPATHLEN, NULL, true) != 0) 135 goto err; 136 137 /* Obtain path to boot environment rootfs (currently booted) */ 138 /* XXX Get dataset mounted at / by kenv/GUID from mountroot? */ 139 if ((rootds = zfs_open(lbh->lzh, lbh->root, ZFS_TYPE_DATASET)) == NULL) 140 goto err; 141 142 zfs_iter_filesystems(rootds, be_locate_rootfs, lbh); 143 zfs_close(rootds); 144 rootds = NULL; 145 if (*lbh->rootfs == '\0') 146 goto err; 147 148 return (lbh); 149 err: 150 if (lbh != NULL) { 151 if (lbh->active_phandle != NULL) 152 zpool_close(lbh->active_phandle); 153 if (lbh->lzh != NULL) 154 libzfs_fini(lbh->lzh); 155 free(lbh); 156 } 157 if (rootds != NULL) 158 zfs_close(rootds); 159 free(poolname); 160 return (NULL); 161 } 162 163 164 /* 165 * Free memory allocated by libbe_init() 166 */ 167 void 168 libbe_close(libbe_handle_t *lbh) 169 { 170 171 if (lbh->active_phandle != NULL) 172 zpool_close(lbh->active_phandle); 173 libzfs_fini(lbh->lzh); 174 free(lbh); 175 } 176 177 /* 178 * Proxy through to libzfs for the moment. 179 */ 180 void 181 be_nicenum(uint64_t num, char *buf, size_t buflen) 182 { 183 184 zfs_nicenum(num, buf, buflen); 185 } 186 187 static int 188 be_destroy_cb(zfs_handle_t *zfs_hdl, void *data) 189 { 190 int err; 191 192 if ((err = zfs_iter_children(zfs_hdl, be_destroy_cb, data)) != 0) 193 return (err); 194 if ((err = zfs_destroy(zfs_hdl, false)) != 0) 195 return (err); 196 return (0); 197 } 198 199 /* 200 * Destroy the boot environment or snapshot specified by the name 201 * parameter. Options are or'd together with the possible values: 202 * BE_DESTROY_FORCE : forces operation on mounted datasets 203 */ 204 int 205 be_destroy(libbe_handle_t *lbh, const char *name, int options) 206 { 207 zfs_handle_t *fs; 208 char path[BE_MAXPATHLEN]; 209 char *p; 210 int err, force, mounted; 211 212 p = path; 213 force = options & BE_DESTROY_FORCE; 214 err = BE_ERR_SUCCESS; 215 216 be_root_concat(lbh, name, path); 217 218 if (strchr(name, '@') == NULL) { 219 if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_FILESYSTEM)) 220 return (set_error(lbh, BE_ERR_NOENT)); 221 222 if (strcmp(path, lbh->rootfs) == 0) 223 return (set_error(lbh, BE_ERR_DESTROYACT)); 224 225 fs = zfs_open(lbh->lzh, p, ZFS_TYPE_FILESYSTEM); 226 } else { 227 228 if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_SNAPSHOT)) 229 return (set_error(lbh, BE_ERR_NOENT)); 230 231 fs = zfs_open(lbh->lzh, p, ZFS_TYPE_SNAPSHOT); 232 } 233 234 if (fs == NULL) 235 return (set_error(lbh, BE_ERR_ZFSOPEN)); 236 237 /* Check if mounted, unmount if force is specified */ 238 if ((mounted = zfs_is_mounted(fs, NULL)) != 0) { 239 if (force) 240 zfs_unmount(fs, NULL, 0); 241 else 242 return (set_error(lbh, BE_ERR_DESTROYMNT)); 243 } 244 245 if ((err = be_destroy_cb(fs, NULL)) != 0) { 246 /* Children are still present or the mount is referenced */ 247 if (err == EBUSY) 248 return (set_error(lbh, BE_ERR_DESTROYMNT)); 249 return (set_error(lbh, BE_ERR_UNKNOWN)); 250 } 251 252 return (0); 253 } 254 255 256 int 257 be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name, 258 bool recursive, char *result) 259 { 260 char buf[BE_MAXPATHLEN]; 261 time_t rawtime; 262 int len, err; 263 264 be_root_concat(lbh, source, buf); 265 266 if (!be_exists(lbh, buf)) 267 return (BE_ERR_NOENT); 268 269 if (snap_name != NULL) { 270 strcat(buf, "@"); 271 strcat(buf, snap_name); 272 if (result != NULL) 273 snprintf(result, BE_MAXPATHLEN, "%s@%s", source, 274 snap_name); 275 } else { 276 time(&rawtime); 277 len = strlen(buf); 278 strftime(buf + len, BE_MAXPATHLEN - len, 279 "@%F-%T", localtime(&rawtime)); 280 if (result != NULL) 281 strcpy(result, strrchr(buf, '/') + 1); 282 } 283 284 if ((err = zfs_snapshot(lbh->lzh, buf, recursive, NULL)) != 0) { 285 switch (err) { 286 case EZFS_INVALIDNAME: 287 return (set_error(lbh, BE_ERR_INVALIDNAME)); 288 289 default: 290 /* 291 * The other errors that zfs_ioc_snapshot might return 292 * shouldn't happen if we've set things up properly, so 293 * we'll gloss over them and call it UNKNOWN as it will 294 * require further triage. 295 */ 296 if (errno == ENOTSUP) 297 return (set_error(lbh, BE_ERR_NOPOOL)); 298 return (set_error(lbh, BE_ERR_UNKNOWN)); 299 } 300 } 301 302 return (BE_ERR_SUCCESS); 303 } 304 305 306 /* 307 * Create the boot environment specified by the name parameter 308 */ 309 int 310 be_create(libbe_handle_t *lbh, const char *name) 311 { 312 int err; 313 314 err = be_create_from_existing(lbh, name, be_active_path(lbh)); 315 316 return (set_error(lbh, err)); 317 } 318 319 320 static int 321 be_deep_clone_prop(int prop, void *cb) 322 { 323 int err; 324 struct libbe_dccb *dccb; 325 zprop_source_t src; 326 char pval[BE_MAXPATHLEN]; 327 char source[BE_MAXPATHLEN]; 328 329 dccb = cb; 330 /* Skip some properties we don't want to touch */ 331 if (prop == ZFS_PROP_CANMOUNT) 332 return (ZPROP_CONT); 333 334 /* Don't copy readonly properties */ 335 if (zfs_prop_readonly(prop)) 336 return (ZPROP_CONT); 337 338 if ((err = zfs_prop_get(dccb->zhp, prop, (char *)&pval, 339 sizeof(pval), &src, (char *)&source, sizeof(source), false))) 340 /* Just continue if we fail to read a property */ 341 return (ZPROP_CONT); 342 343 /* Only copy locally defined properties */ 344 if (src != ZPROP_SRC_LOCAL) 345 return (ZPROP_CONT); 346 347 nvlist_add_string(dccb->props, zfs_prop_to_name(prop), (char *)pval); 348 349 return (ZPROP_CONT); 350 } 351 352 static int 353 be_deep_clone(zfs_handle_t *ds, void *data) 354 { 355 int err; 356 char be_path[BE_MAXPATHLEN]; 357 char snap_path[BE_MAXPATHLEN]; 358 const char *dspath; 359 char *dsname; 360 zfs_handle_t *snap_hdl; 361 nvlist_t *props; 362 struct libbe_deep_clone *isdc, sdc; 363 struct libbe_dccb dccb; 364 365 isdc = (struct libbe_deep_clone *)data; 366 dspath = zfs_get_name(ds); 367 if ((dsname = strrchr(dspath, '/')) == NULL) 368 return (BE_ERR_UNKNOWN); 369 dsname++; 370 371 if (isdc->bename == NULL) 372 snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, dsname); 373 else 374 snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, isdc->bename); 375 376 snprintf(snap_path, sizeof(snap_path), "%s@%s", dspath, isdc->snapname); 377 378 if (zfs_dataset_exists(isdc->lbh->lzh, be_path, ZFS_TYPE_DATASET)) 379 return (set_error(isdc->lbh, BE_ERR_EXISTS)); 380 381 if ((snap_hdl = 382 zfs_open(isdc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) == NULL) 383 return (set_error(isdc->lbh, BE_ERR_ZFSOPEN)); 384 385 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 386 nvlist_add_string(props, "canmount", "noauto"); 387 388 dccb.zhp = ds; 389 dccb.props = props; 390 if (zprop_iter(be_deep_clone_prop, &dccb, B_FALSE, B_FALSE, 391 ZFS_TYPE_FILESYSTEM) == ZPROP_INVAL) 392 return (-1); 393 394 if ((err = zfs_clone(snap_hdl, be_path, props)) != 0) { 395 switch (err) { 396 case EZFS_SUCCESS: 397 err = BE_ERR_SUCCESS; 398 break; 399 default: 400 err = BE_ERR_ZFSCLONE; 401 break; 402 } 403 } 404 405 nvlist_free(props); 406 zfs_close(snap_hdl); 407 408 sdc.lbh = isdc->lbh; 409 sdc.bename = NULL; 410 sdc.snapname = isdc->snapname; 411 sdc.be_root = (char *)&be_path; 412 413 err = zfs_iter_filesystems(ds, be_deep_clone, &sdc); 414 415 return (err); 416 } 417 418 /* 419 * Create the boot environment from pre-existing snapshot 420 */ 421 int 422 be_create_from_existing_snap(libbe_handle_t *lbh, const char *name, 423 const char *snap) 424 { 425 int err; 426 char be_path[BE_MAXPATHLEN]; 427 char snap_path[BE_MAXPATHLEN]; 428 const char *bename; 429 char *parentname, *snapname; 430 zfs_handle_t *parent_hdl; 431 struct libbe_deep_clone sdc; 432 433 if ((err = be_validate_name(lbh, name)) != 0) 434 return (set_error(lbh, err)); 435 if ((err = be_root_concat(lbh, snap, snap_path)) != 0) 436 return (set_error(lbh, err)); 437 if ((err = be_validate_snap(lbh, snap_path)) != 0) 438 return (set_error(lbh, err)); 439 440 if ((err = be_root_concat(lbh, name, be_path)) != 0) 441 return (set_error(lbh, err)); 442 443 if ((bename = strrchr(name, '/')) == NULL) 444 bename = name; 445 else 446 bename++; 447 448 if ((parentname = strdup(snap_path)) == NULL) { 449 err = BE_ERR_UNKNOWN; 450 return (set_error(lbh, err)); 451 } 452 snapname = strchr(parentname, '@'); 453 if (snapname == NULL) { 454 err = BE_ERR_UNKNOWN; 455 return (set_error(lbh, err)); 456 } 457 *snapname = '\0'; 458 snapname++; 459 460 sdc.lbh = lbh; 461 sdc.bename = bename; 462 sdc.snapname = snapname; 463 sdc.be_root = lbh->root; 464 465 parent_hdl = zfs_open(lbh->lzh, parentname, ZFS_TYPE_DATASET); 466 err = be_deep_clone(parent_hdl, &sdc); 467 468 return (set_error(lbh, err)); 469 } 470 471 472 /* 473 * Create a boot environment from an existing boot environment 474 */ 475 int 476 be_create_from_existing(libbe_handle_t *lbh, const char *name, const char *old) 477 { 478 int err; 479 char buf[BE_MAXPATHLEN]; 480 481 if ((err = be_snapshot(lbh, old, NULL, true, (char *)&buf))) 482 return (set_error(lbh, err)); 483 484 err = be_create_from_existing_snap(lbh, name, (char *)buf); 485 486 return (set_error(lbh, err)); 487 } 488 489 490 /* 491 * Verifies that a snapshot has a valid name, exists, and has a mountpoint of 492 * '/'. Returns BE_ERR_SUCCESS (0), upon success, or the relevant BE_ERR_* upon 493 * failure. Does not set the internal library error state. 494 */ 495 int 496 be_validate_snap(libbe_handle_t *lbh, const char *snap_name) 497 { 498 zfs_handle_t *zfs_hdl; 499 char buf[BE_MAXPATHLEN]; 500 char *delim_pos; 501 int err = BE_ERR_SUCCESS; 502 503 if (strlen(snap_name) >= BE_MAXPATHLEN) 504 return (BE_ERR_PATHLEN); 505 506 if (!zfs_dataset_exists(lbh->lzh, snap_name, 507 ZFS_TYPE_SNAPSHOT)) 508 return (BE_ERR_NOENT); 509 510 strncpy(buf, snap_name, BE_MAXPATHLEN); 511 512 /* Find the base filesystem of the snapshot */ 513 if ((delim_pos = strchr(buf, '@')) == NULL) 514 return (BE_ERR_INVALIDNAME); 515 *delim_pos = '\0'; 516 517 if ((zfs_hdl = 518 zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL) 519 return (BE_ERR_NOORIGIN); 520 521 if ((err = zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, buf, BE_MAXPATHLEN, 522 NULL, NULL, 0, 1)) != 0) 523 err = BE_ERR_INVORIGIN; 524 525 if ((err != 0) && (strncmp(buf, "/", BE_MAXPATHLEN) != 0)) 526 err = BE_ERR_INVORIGIN; 527 528 zfs_close(zfs_hdl); 529 530 return (err); 531 } 532 533 534 /* 535 * Idempotently appends the name argument to the root boot environment path 536 * and copies the resulting string into the result buffer (which is assumed 537 * to be at least BE_MAXPATHLEN characters long. Returns BE_ERR_SUCCESS upon 538 * success, BE_ERR_PATHLEN if the resulting path is longer than BE_MAXPATHLEN, 539 * or BE_ERR_INVALIDNAME if the name is a path that does not begin with 540 * zfs_be_root. Does not set internal library error state. 541 */ 542 int 543 be_root_concat(libbe_handle_t *lbh, const char *name, char *result) 544 { 545 size_t name_len, root_len; 546 547 name_len = strlen(name); 548 root_len = strlen(lbh->root); 549 550 /* Act idempotently; return be name if it is already a full path */ 551 if (strrchr(name, '/') != NULL) { 552 if (strstr(name, lbh->root) != name) 553 return (BE_ERR_INVALIDNAME); 554 555 if (name_len >= BE_MAXPATHLEN) 556 return (BE_ERR_PATHLEN); 557 558 strncpy(result, name, BE_MAXPATHLEN); 559 return (BE_ERR_SUCCESS); 560 } else if (name_len + root_len + 1 < BE_MAXPATHLEN) { 561 snprintf(result, BE_MAXPATHLEN, "%s/%s", lbh->root, 562 name); 563 return (BE_ERR_SUCCESS); 564 } 565 566 return (BE_ERR_PATHLEN); 567 } 568 569 570 /* 571 * Verifies the validity of a boot environment name (A-Za-z0-9-_.). Returns 572 * BE_ERR_SUCCESS (0) if name is valid, otherwise returns BE_ERR_INVALIDNAME. 573 * Does not set internal library error state. 574 */ 575 int 576 be_validate_name(libbe_handle_t *lbh __unused, const char *name) 577 { 578 for (int i = 0; *name; i++) { 579 char c = *(name++); 580 if (isalnum(c) || (c == '-') || (c == '_') || (c == '.')) 581 continue; 582 return (BE_ERR_INVALIDNAME); 583 } 584 585 return (BE_ERR_SUCCESS); 586 } 587 588 589 /* 590 * usage 591 */ 592 int 593 be_rename(libbe_handle_t *lbh, const char *old, const char *new) 594 { 595 char full_old[BE_MAXPATHLEN]; 596 char full_new[BE_MAXPATHLEN]; 597 zfs_handle_t *zfs_hdl; 598 int err; 599 600 if ((err = be_root_concat(lbh, old, full_old)) != 0) 601 return (set_error(lbh, err)); 602 if ((err = be_root_concat(lbh, new, full_new)) != 0) 603 return (set_error(lbh, err)); 604 605 if ((err = be_validate_name(lbh, new)) != 0) 606 return (err); 607 608 /* Check if old is active BE */ 609 if (strcmp(full_old, be_active_path(lbh)) == 0) 610 return (set_error(lbh, BE_ERR_MOUNTED)); 611 612 if (!zfs_dataset_exists(lbh->lzh, full_old, ZFS_TYPE_DATASET)) 613 return (set_error(lbh, BE_ERR_NOENT)); 614 615 if (zfs_dataset_exists(lbh->lzh, full_new, ZFS_TYPE_DATASET)) 616 return (set_error(lbh, BE_ERR_EXISTS)); 617 618 /* XXX TODO 619 * - What about mounted BEs? 620 * - if mounted error out unless a force flag is set? 621 */ 622 if ((zfs_hdl = zfs_open(lbh->lzh, full_old, 623 ZFS_TYPE_FILESYSTEM)) == NULL) 624 return (set_error(lbh, BE_ERR_ZFSOPEN)); 625 626 /* recurse, nounmount, forceunmount */ 627 struct renameflags flags = { 0, 0, 0 }; 628 629 err = zfs_rename(zfs_hdl, NULL, full_new, flags); 630 631 zfs_close(zfs_hdl); 632 633 return (set_error(lbh, err)); 634 } 635 636 637 int 638 be_export(libbe_handle_t *lbh, const char *bootenv, int fd) 639 { 640 char snap_name[BE_MAXPATHLEN]; 641 char buf[BE_MAXPATHLEN]; 642 zfs_handle_t *zfs; 643 int err; 644 645 if ((err = be_snapshot(lbh, bootenv, NULL, true, snap_name)) != 0) 646 /* XXX TODO error handle */ 647 return (-1); 648 649 be_root_concat(lbh, snap_name, buf); 650 651 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL) 652 return (set_error(lbh, BE_ERR_ZFSOPEN)); 653 654 err = zfs_send_one(zfs, NULL, fd, 0); 655 return (err); 656 } 657 658 659 int 660 be_import(libbe_handle_t *lbh, const char *bootenv, int fd) 661 { 662 char buf[BE_MAXPATHLEN]; 663 time_t rawtime; 664 nvlist_t *props; 665 zfs_handle_t *zfs; 666 int err, len; 667 char nbuf[24]; 668 669 /* 670 * We don't need this to be incredibly random, just unique enough that 671 * it won't conflict with an existing dataset name. Chopping time 672 * down to 32 bits is probably good enough for this. 673 */ 674 snprintf(nbuf, 24, "tmp%u", 675 (uint32_t)(time(NULL) & 0xFFFFFFFF)); 676 if ((err = be_root_concat(lbh, nbuf, buf)) != 0) 677 /* 678 * Technically this is our problem, but we try to use short 679 * enough names that we won't run into problems except in 680 * worst-case BE root approaching MAXPATHLEN. 681 */ 682 return (set_error(lbh, BE_ERR_PATHLEN)); 683 684 time(&rawtime); 685 len = strlen(buf); 686 strftime(buf + len, BE_MAXPATHLEN - len, 687 "@%F-%T", localtime(&rawtime)); 688 689 /* lzc_receive(SNAPNAME, PROPS, ORIGIN, FORCE, fd)) { */ 690 if ((err = lzc_receive(buf, NULL, NULL, false, fd)) != 0) { 691 switch (err) { 692 case EINVAL: 693 return (set_error(lbh, BE_ERR_NOORIGIN)); 694 case ENOENT: 695 return (set_error(lbh, BE_ERR_NOENT)); 696 case EIO: 697 return (set_error(lbh, BE_ERR_IO)); 698 default: 699 return (set_error(lbh, BE_ERR_UNKNOWN)); 700 } 701 } 702 703 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL) 704 return (set_error(lbh, BE_ERR_ZFSOPEN)); 705 706 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 707 nvlist_add_string(props, "canmount", "noauto"); 708 nvlist_add_string(props, "mountpoint", "/"); 709 710 be_root_concat(lbh, bootenv, buf); 711 712 err = zfs_clone(zfs, buf, props); 713 zfs_close(zfs); 714 715 nvlist_free(props); 716 717 /* XXX TODO: recursively delete nbuf dataset */ 718 return (err); 719 } 720 721 722 int 723 be_add_child(libbe_handle_t *lbh, const char *child_path, bool cp_if_exists) 724 { 725 struct stat sb; 726 char active[BE_MAXPATHLEN]; 727 char buf[BE_MAXPATHLEN]; 728 nvlist_t *props; 729 const char *s; 730 zfs_handle_t *zfs; 731 long snap_name; 732 int err, pos; 733 734 /* Require absolute paths */ 735 if (*child_path != '/') 736 /* XXX TODO: create appropriate error */ 737 return (-1); 738 739 strncpy(active, be_active_path(lbh), BE_MAXPATHLEN); 740 strcpy(buf, active); 741 742 /* Create non-mountable parent dataset(s) */ 743 s = child_path; 744 for (char *p; (p = strchr(s+1, '/')) != NULL; s = p) { 745 size_t len = p - s; 746 strncat(buf, s, len); 747 748 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 749 nvlist_add_string(props, "canmount", "off"); 750 nvlist_add_string(props, "mountpoint", "none"); 751 zfs_create(lbh->lzh, buf, ZFS_TYPE_DATASET, props); 752 nvlist_free(props); 753 } 754 755 /* Path does not exist as a descendent of / yet */ 756 pos = strlen(active); 757 758 /* XXX TODO: Verify that resulting str is less than BE_MAXPATHLEN */ 759 strncpy(&active[pos], child_path, BE_MAXPATHLEN-pos); 760 761 if (stat(child_path, &sb) != 0) { 762 /* Verify that error is ENOENT */ 763 if (errno != 2) 764 /* XXX TODO: create appropriate error */ 765 return (-1); 766 767 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 768 nvlist_add_string(props, "canmount", "noauto"); 769 nvlist_add_string(props, "mountpoint", child_path); 770 771 /* Create */ 772 if ((err = 773 zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET, props)) != 0) 774 /* XXX TODO handle error */ 775 return (-1); 776 nvlist_free(props); 777 778 if ((zfs = 779 zfs_open(lbh->lzh, active, ZFS_TYPE_DATASET)) == NULL) 780 /* XXX TODO handle error */ 781 return (-1); 782 783 /* Set props */ 784 if ((err = zfs_prop_set(zfs, "canmount", "noauto")) != 0) 785 /* TODO handle error */ 786 return (-1); 787 } else if (cp_if_exists) { 788 /* Path is already a descendent of / and should be copied */ 789 790 /* XXX TODO ? */ 791 792 /* 793 * Establish if the existing path is a zfs dataset or just 794 * the subdirectory of one 795 */ 796 797 /* XXX TODO: use mktemp */ 798 snap_name = random(); 799 800 snprintf(buf, BE_MAXPATHLEN, "%s@%ld", child_path, snap_name); 801 802 if ((err = zfs_snapshot(lbh->lzh, buf, false, NULL)) != 0) 803 /* XXX TODO correct error */ 804 return (-1); 805 806 /* Clone */ 807 if ((zfs = 808 zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL) 809 /* XXX TODO correct error */ 810 return (-1); 811 812 if ((err = zfs_clone(zfs, active, NULL)) != 0) 813 /* XXX TODO correct error */ 814 return (-1); 815 816 /* set props */ 817 } else 818 /* TODO: error code for exists, but not cp? */ 819 return (-1); 820 821 return (BE_ERR_SUCCESS); 822 } 823 824 static int 825 be_set_nextboot(libbe_handle_t *lbh, nvlist_t *config, uint64_t pool_guid, 826 const char *zfsdev) 827 { 828 nvlist_t **child; 829 uint64_t vdev_guid; 830 int c, children; 831 832 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, &child, 833 &children) == 0) { 834 for (c = 0; c < children; ++c) 835 if (be_set_nextboot(lbh, child[c], pool_guid, zfsdev) != 0) 836 return (1); 837 return (0); 838 } 839 840 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 841 &vdev_guid) != 0) { 842 return (1); 843 } 844 845 if (zpool_nextboot(lbh->lzh, pool_guid, vdev_guid, zfsdev) != 0) { 846 perror("ZFS_IOC_NEXTBOOT failed"); 847 return (1); 848 } 849 850 return (0); 851 } 852 853 854 int 855 be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary) 856 { 857 char be_path[BE_MAXPATHLEN]; 858 char buf[BE_MAXPATHLEN]; 859 uint64_t pool_guid; 860 nvlist_t *config, *vdevs; 861 int err; 862 863 be_root_concat(lbh, bootenv, be_path); 864 865 /* Note: be_exists fails if mountpoint is not / */ 866 if (!be_exists(lbh, be_path)) 867 return (BE_ERR_NOENT); 868 869 if (temporary) { 870 config = zpool_get_config(lbh->active_phandle, NULL); 871 if (config == NULL) { 872 printf("no config\n"); 873 return (1); 874 } 875 876 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 877 &pool_guid) != 0) 878 return (1); 879 880 /* Expected format according to zfsbootcfg(8) man */ 881 strcpy(buf, "zfs:"); 882 strcat(buf, be_path); 883 strcat(buf, ":"); 884 885 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &vdevs) != 0) 886 return (1); 887 888 return (be_set_nextboot(lbh, vdevs, pool_guid, buf)); 889 } else { 890 /* Obtain bootenv zpool */ 891 err = zpool_set_prop(lbh->active_phandle, "bootfs", be_path); 892 893 switch (err) { 894 case 0: 895 return (BE_ERR_SUCCESS); 896 897 default: 898 /* XXX TODO correct errors */ 899 return (-1); 900 } 901 } 902 } 903