1 /* 2 * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7 #include <sys/param.h> 8 #include <sys/module.h> 9 #include <sys/mount.h> 10 #include <sys/stat.h> 11 #include <sys/ucred.h> 12 #include <sys/queue.h> 13 #include <sys/zfs_context.h> 14 #include <sys/mntent.h> 15 #include <sys/zfs_ioctl.h> 16 17 #include <libzutil.h> 18 #include <ctype.h> 19 #include <libgen.h> 20 #include <libzfs_core.h> 21 #include <libzfs_impl.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <time.h> 25 #include <unistd.h> 26 #include <libzfsbootenv.h> 27 28 #include "be.h" 29 #include "be_impl.h" 30 31 struct promote_entry { 32 char name[BE_MAXPATHLEN]; 33 SLIST_ENTRY(promote_entry) link; 34 }; 35 36 struct be_destroy_data { 37 libbe_handle_t *lbh; 38 char target_name[BE_MAXPATHLEN]; 39 char *snapname; 40 SLIST_HEAD(, promote_entry) promotelist; 41 }; 42 43 #if SOON 44 static int be_create_child_noent(libbe_handle_t *lbh, const char *active, 45 const char *child_path); 46 static int be_create_child_cloned(libbe_handle_t *lbh, const char *active); 47 #endif 48 49 /* Arbitrary... should tune */ 50 #define BE_SNAP_SERIAL_MAX 1024 51 52 /* 53 * Iterator function for locating the rootfs amongst the children of the 54 * zfs_be_root set by loader(8). data is expected to be a libbe_handle_t *. 55 */ 56 static int 57 be_locate_rootfs(libbe_handle_t *lbh) 58 { 59 struct statfs sfs; 60 struct mnttab entry; 61 zfs_handle_t *zfs; 62 63 /* 64 * Check first if root is ZFS; if not, we'll bail on rootfs capture. 65 * Unfortunately needed because zfs_path_to_zhandle will emit to 66 * stderr if / isn't actually a ZFS filesystem, which we'd like 67 * to avoid. 68 */ 69 if (statfs("/", &sfs) == 0) { 70 statfs2mnttab(&sfs, &entry); 71 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 72 return (1); 73 } else 74 return (1); 75 zfs = zfs_path_to_zhandle(lbh->lzh, "/", ZFS_TYPE_FILESYSTEM); 76 if (zfs == NULL) 77 return (1); 78 79 strlcpy(lbh->rootfs, zfs_get_name(zfs), sizeof(lbh->rootfs)); 80 zfs_close(zfs); 81 return (0); 82 } 83 84 /* 85 * Initializes the libbe context to operate in the root boot environment 86 * dataset, for example, zroot/ROOT. 87 */ 88 libbe_handle_t * 89 libbe_init(const char *root) 90 { 91 char altroot[MAXPATHLEN]; 92 libbe_handle_t *lbh; 93 char *poolname, *pos; 94 int pnamelen; 95 96 lbh = NULL; 97 poolname = pos = NULL; 98 99 /* 100 * If the zfs kmod's not loaded then the later libzfs_init() will load 101 * the module for us, but that's not desirable for a couple reasons. If 102 * the module's not loaded, there's no pool imported and we're going to 103 * fail anyways. We also don't really want libbe consumers to have that 104 * kind of side-effect (module loading) in the general case. 105 */ 106 if (modfind("zfs") < 0) 107 goto err; 108 109 if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL) 110 goto err; 111 112 if ((lbh->lzh = libzfs_init()) == NULL) 113 goto err; 114 115 /* 116 * Grab rootfs, we'll work backwards from there if an optional BE root 117 * has not been passed in. 118 */ 119 if (be_locate_rootfs(lbh) != 0) { 120 if (root == NULL) 121 goto err; 122 *lbh->rootfs = '\0'; 123 } 124 if (root == NULL) { 125 /* Strip off the final slash from rootfs to get the be root */ 126 strlcpy(lbh->root, lbh->rootfs, sizeof(lbh->root)); 127 pos = strrchr(lbh->root, '/'); 128 if (pos == NULL) 129 goto err; 130 *pos = '\0'; 131 } else 132 strlcpy(lbh->root, root, sizeof(lbh->root)); 133 134 if ((pos = strchr(lbh->root, '/')) == NULL) 135 goto err; 136 137 pnamelen = pos - lbh->root; 138 poolname = malloc(pnamelen + 1); 139 if (poolname == NULL) 140 goto err; 141 142 strlcpy(poolname, lbh->root, pnamelen + 1); 143 if ((lbh->active_phandle = zpool_open(lbh->lzh, poolname)) == NULL) 144 goto err; 145 free(poolname); 146 poolname = NULL; 147 148 if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_BOOTFS, lbh->bootfs, 149 sizeof(lbh->bootfs), NULL, true) != 0) 150 goto err; 151 152 if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_ALTROOT, 153 altroot, sizeof(altroot), NULL, true) == 0 && 154 strcmp(altroot, "-") != 0) 155 lbh->altroot_len = strlen(altroot); 156 157 (void) lzbe_get_boot_device(zpool_get_name(lbh->active_phandle), 158 &lbh->bootonce); 159 160 return (lbh); 161 err: 162 if (lbh != NULL) { 163 if (lbh->active_phandle != NULL) 164 zpool_close(lbh->active_phandle); 165 if (lbh->lzh != NULL) 166 libzfs_fini(lbh->lzh); 167 free(lbh); 168 } 169 free(poolname); 170 return (NULL); 171 } 172 173 174 /* 175 * Free memory allocated by libbe_init() 176 */ 177 void 178 libbe_close(libbe_handle_t *lbh) 179 { 180 181 if (lbh->active_phandle != NULL) 182 zpool_close(lbh->active_phandle); 183 libzfs_fini(lbh->lzh); 184 185 free(lbh->bootonce); 186 free(lbh); 187 } 188 189 /* 190 * Proxy through to libzfs for the moment. 191 */ 192 void 193 be_nicenum(uint64_t num, char *buf, size_t buflen) 194 { 195 196 zfs_nicenum(num, buf, buflen); 197 } 198 199 static bool 200 be_should_promote_clones(zfs_handle_t *zfs_hdl, struct be_destroy_data *bdd) 201 { 202 char *atpos; 203 204 if (zfs_get_type(zfs_hdl) != ZFS_TYPE_SNAPSHOT) 205 return (false); 206 207 /* 208 * If we're deleting a snapshot, we need to make sure we only promote 209 * clones that are derived from one of the snapshots we're deleting, 210 * rather than that of a snapshot we're not touching. This keeps stuff 211 * in a consistent state, making sure that we don't error out unless 212 * we really need to. 213 */ 214 if (bdd->snapname == NULL) 215 return (true); 216 217 atpos = strchr(zfs_get_name(zfs_hdl), '@'); 218 return (strcmp(atpos + 1, bdd->snapname) == 0); 219 } 220 221 /* 222 * This is executed from be_promote_dependent_clones via zfs_iter_dependents, 223 * It checks if the dependent type is a snapshot then attempts to find any 224 * clones associated with it. Any clones not related to the destroy target are 225 * added to the promote list. 226 */ 227 static int 228 be_dependent_clone_cb(zfs_handle_t *zfs_hdl, void *data) 229 { 230 int err; 231 bool found; 232 const char *name; 233 struct nvlist *nvl; 234 struct nvpair *nvp; 235 struct be_destroy_data *bdd; 236 struct promote_entry *entry, *newentry; 237 238 nvp = NULL; 239 err = 0; 240 bdd = (struct be_destroy_data *)data; 241 242 if (be_should_promote_clones(zfs_hdl, bdd) && 243 (nvl = zfs_get_clones_nvl(zfs_hdl)) != NULL) { 244 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 245 name = nvpair_name(nvp); 246 247 /* 248 * Skip if the clone is equal to, or a child of, the 249 * destroy target. 250 */ 251 if (strncmp(name, bdd->target_name, 252 strlen(bdd->target_name)) == 0 || 253 strstr(name, bdd->target_name) == name) { 254 continue; 255 } 256 257 found = false; 258 SLIST_FOREACH(entry, &bdd->promotelist, link) { 259 if (strcmp(entry->name, name) == 0) { 260 found = true; 261 break; 262 } 263 } 264 265 if (found) 266 continue; 267 268 newentry = malloc(sizeof(struct promote_entry)); 269 if (newentry == NULL) { 270 err = ENOMEM; 271 break; 272 } 273 274 #define BE_COPY_NAME(entry, src) \ 275 strlcpy((entry)->name, (src), sizeof((entry)->name)) 276 if (BE_COPY_NAME(newentry, name) >= 277 sizeof(newentry->name)) { 278 /* Shouldn't happen. */ 279 free(newentry); 280 err = ENAMETOOLONG; 281 break; 282 } 283 #undef BE_COPY_NAME 284 285 /* 286 * We're building up a SLIST here to make sure both that 287 * we get the order right and so that we don't 288 * inadvertently observe the wrong state by promoting 289 * datasets while we're still walking the tree. The 290 * latter can lead to situations where we promote a BE 291 * then effectively demote it again. 292 */ 293 SLIST_INSERT_HEAD(&bdd->promotelist, newentry, link); 294 } 295 nvlist_free(nvl); 296 } 297 zfs_close(zfs_hdl); 298 return (err); 299 } 300 301 /* 302 * This is called before a destroy, so that any datasets(environments) that are 303 * dependent on this one get promoted before destroying the target. 304 */ 305 static int 306 be_promote_dependent_clones(zfs_handle_t *zfs_hdl, struct be_destroy_data *bdd) 307 { 308 int err; 309 zfs_handle_t *clone; 310 struct promote_entry *entry; 311 312 snprintf(bdd->target_name, BE_MAXPATHLEN, "%s/", zfs_get_name(zfs_hdl)); 313 err = zfs_iter_dependents(zfs_hdl, true, be_dependent_clone_cb, bdd); 314 315 /* 316 * Drain the list and walk away from it if we're only deleting a 317 * snapshot. 318 */ 319 if (bdd->snapname != NULL && !SLIST_EMPTY(&bdd->promotelist)) 320 err = BE_ERR_HASCLONES; 321 while (!SLIST_EMPTY(&bdd->promotelist)) { 322 entry = SLIST_FIRST(&bdd->promotelist); 323 SLIST_REMOVE_HEAD(&bdd->promotelist, link); 324 325 #define ZFS_GRAB_CLONE() \ 326 zfs_open(bdd->lbh->lzh, entry->name, ZFS_TYPE_FILESYSTEM) 327 /* 328 * Just skip this part on error, we still want to clean up the 329 * promotion list after the first error. We'll then preserve it 330 * all the way back. 331 */ 332 if (err == 0 && (clone = ZFS_GRAB_CLONE()) != NULL) { 333 err = zfs_promote(clone); 334 if (err != 0) 335 err = BE_ERR_DESTROYMNT; 336 zfs_close(clone); 337 } 338 #undef ZFS_GRAB_CLONE 339 free(entry); 340 } 341 342 return (err); 343 } 344 345 static int 346 be_destroy_cb(zfs_handle_t *zfs_hdl, void *data) 347 { 348 char path[BE_MAXPATHLEN]; 349 struct be_destroy_data *bdd; 350 zfs_handle_t *snap; 351 int err; 352 353 bdd = (struct be_destroy_data *)data; 354 if (bdd->snapname == NULL) { 355 err = zfs_iter_children(zfs_hdl, be_destroy_cb, data); 356 if (err != 0) 357 return (err); 358 return (zfs_destroy(zfs_hdl, false)); 359 } 360 /* If we're dealing with snapshots instead, delete that one alone */ 361 err = zfs_iter_filesystems(zfs_hdl, be_destroy_cb, data); 362 if (err != 0) 363 return (err); 364 /* 365 * This part is intentionally glossing over any potential errors, 366 * because there's a lot less potential for errors when we're cleaning 367 * up snapshots rather than a full deep BE. The primary error case 368 * here being if the snapshot doesn't exist in the first place, which 369 * the caller will likely deem insignificant as long as it doesn't 370 * exist after the call. Thus, such a missing snapshot shouldn't jam 371 * up the destruction. 372 */ 373 snprintf(path, sizeof(path), "%s@%s", zfs_get_name(zfs_hdl), 374 bdd->snapname); 375 if (!zfs_dataset_exists(bdd->lbh->lzh, path, ZFS_TYPE_SNAPSHOT)) 376 return (0); 377 snap = zfs_open(bdd->lbh->lzh, path, ZFS_TYPE_SNAPSHOT); 378 if (snap != NULL) 379 zfs_destroy(snap, false); 380 return (0); 381 } 382 383 #define BE_DESTROY_WANTORIGIN (BE_DESTROY_ORIGIN | BE_DESTROY_AUTOORIGIN) 384 /* 385 * Destroy the boot environment or snapshot specified by the name 386 * parameter. Options are or'd together with the possible values: 387 * BE_DESTROY_FORCE : forces operation on mounted datasets 388 * BE_DESTROY_ORIGIN: destroy the origin snapshot as well 389 */ 390 static int 391 be_destroy_internal(libbe_handle_t *lbh, const char *name, int options, 392 bool odestroyer) 393 { 394 struct be_destroy_data bdd; 395 char origin[BE_MAXPATHLEN], path[BE_MAXPATHLEN]; 396 zfs_handle_t *fs; 397 char *snapdelim; 398 int err, force, mounted; 399 size_t rootlen; 400 401 bdd.lbh = lbh; 402 bdd.snapname = NULL; 403 SLIST_INIT(&bdd.promotelist); 404 force = options & BE_DESTROY_FORCE; 405 *origin = '\0'; 406 407 be_root_concat(lbh, name, path); 408 409 if ((snapdelim = strchr(path, '@')) == NULL) { 410 if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_FILESYSTEM)) 411 return (set_error(lbh, BE_ERR_NOENT)); 412 413 if (strcmp(path, lbh->rootfs) == 0 || 414 strcmp(path, lbh->bootfs) == 0) 415 return (set_error(lbh, BE_ERR_DESTROYACT)); 416 417 fs = zfs_open(lbh->lzh, path, ZFS_TYPE_FILESYSTEM); 418 if (fs == NULL) 419 return (set_error(lbh, BE_ERR_ZFSOPEN)); 420 421 /* Don't destroy a mounted dataset unless force is specified */ 422 if ((mounted = zfs_is_mounted(fs, NULL)) != 0) { 423 if (force) { 424 zfs_unmount(fs, NULL, 0); 425 } else { 426 free(bdd.snapname); 427 return (set_error(lbh, BE_ERR_DESTROYMNT)); 428 } 429 } 430 431 /* Handle destroying bootonce */ 432 if (lbh->bootonce != NULL && 433 strcmp(path, lbh->bootonce) == 0) 434 (void) lzbe_set_boot_device( 435 zpool_get_name(lbh->active_phandle), lzbe_add, NULL); 436 } else { 437 /* 438 * If we're initially destroying a snapshot, origin options do 439 * not make sense. If we're destroying the origin snapshot of 440 * a BE, we want to maintain the options in case we need to 441 * fake success after failing to promote. 442 */ 443 if (!odestroyer) 444 options &= ~BE_DESTROY_WANTORIGIN; 445 if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_SNAPSHOT)) 446 return (set_error(lbh, BE_ERR_NOENT)); 447 448 bdd.snapname = strdup(snapdelim + 1); 449 if (bdd.snapname == NULL) 450 return (set_error(lbh, BE_ERR_NOMEM)); 451 *snapdelim = '\0'; 452 fs = zfs_open(lbh->lzh, path, ZFS_TYPE_DATASET); 453 if (fs == NULL) { 454 free(bdd.snapname); 455 return (set_error(lbh, BE_ERR_ZFSOPEN)); 456 } 457 } 458 459 /* 460 * Whether we're destroying a BE or a single snapshot, we need to walk 461 * the tree of what we're going to destroy and promote everything in our 462 * path so that we can make it happen. 463 */ 464 if ((err = be_promote_dependent_clones(fs, &bdd)) != 0) { 465 free(bdd.snapname); 466 467 /* 468 * If we're just destroying the origin of some other dataset 469 * we were invoked to destroy, then we just ignore 470 * BE_ERR_HASCLONES and return success unless the caller wanted 471 * to force the issue. 472 */ 473 if (odestroyer && err == BE_ERR_HASCLONES && 474 (options & BE_DESTROY_AUTOORIGIN) != 0) 475 return (0); 476 return (set_error(lbh, err)); 477 } 478 479 /* 480 * This was deferred until after we promote all of the derivatives so 481 * that we grab the new origin after everything's settled down. 482 */ 483 if ((options & BE_DESTROY_WANTORIGIN) != 0 && 484 zfs_prop_get(fs, ZFS_PROP_ORIGIN, origin, sizeof(origin), 485 NULL, NULL, 0, 1) != 0 && 486 (options & BE_DESTROY_ORIGIN) != 0) 487 return (set_error(lbh, BE_ERR_NOORIGIN)); 488 489 /* 490 * If the caller wants auto-origin destruction and the origin 491 * name matches one of our automatically created snapshot names 492 * (i.e. strftime("%F-%T") with a serial at the end), then 493 * we'll set the DESTROY_ORIGIN flag and nuke it 494 * be_is_auto_snapshot_name is exported from libbe(3) so that 495 * the caller can determine if it needs to warn about the origin 496 * not being destroyed or not. 497 */ 498 if ((options & BE_DESTROY_AUTOORIGIN) != 0 && *origin != '\0' && 499 be_is_auto_snapshot_name(lbh, origin)) 500 options |= BE_DESTROY_ORIGIN; 501 502 err = be_destroy_cb(fs, &bdd); 503 zfs_close(fs); 504 free(bdd.snapname); 505 if (err != 0) { 506 /* Children are still present or the mount is referenced */ 507 if (err == EBUSY) 508 return (set_error(lbh, BE_ERR_DESTROYMNT)); 509 return (set_error(lbh, BE_ERR_UNKNOWN)); 510 } 511 512 if ((options & BE_DESTROY_ORIGIN) == 0) 513 return (0); 514 515 /* The origin can't possibly be shorter than the BE root */ 516 rootlen = strlen(lbh->root); 517 if (*origin == '\0' || strlen(origin) <= rootlen + 1) 518 return (set_error(lbh, BE_ERR_INVORIGIN)); 519 520 /* 521 * We'll be chopping off the BE root and running this back through 522 * be_destroy, so that we properly handle the origin snapshot whether 523 * it be that of a deep BE or not. 524 */ 525 if (strncmp(origin, lbh->root, rootlen) != 0 || origin[rootlen] != '/') 526 return (0); 527 528 return (be_destroy_internal(lbh, origin + rootlen + 1, 529 options & ~BE_DESTROY_ORIGIN, true)); 530 } 531 532 int 533 be_destroy(libbe_handle_t *lbh, const char *name, int options) 534 { 535 536 /* 537 * The consumer must not set both BE_DESTROY_AUTOORIGIN and 538 * BE_DESTROY_ORIGIN. Internally, we'll set the latter from the former. 539 * The latter should imply that we must succeed at destroying the 540 * origin, or complain otherwise. 541 */ 542 if ((options & BE_DESTROY_WANTORIGIN) == BE_DESTROY_WANTORIGIN) 543 return (set_error(lbh, BE_ERR_UNKNOWN)); 544 return (be_destroy_internal(lbh, name, options, false)); 545 } 546 547 static void 548 be_setup_snapshot_name(libbe_handle_t *lbh, char *buf, size_t buflen) 549 { 550 time_t rawtime; 551 int len, serial; 552 553 time(&rawtime); 554 len = strlen(buf); 555 len += strftime(buf + len, buflen - len, "@%F-%T", localtime(&rawtime)); 556 /* No room for serial... caller will do its best */ 557 if (buflen - len < 2) 558 return; 559 560 for (serial = 0; serial < BE_SNAP_SERIAL_MAX; ++serial) { 561 snprintf(buf + len, buflen - len, "-%d", serial); 562 if (!zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) 563 return; 564 } 565 } 566 567 bool 568 be_is_auto_snapshot_name(libbe_handle_t *lbh __unused, const char *name) 569 { 570 const char *snap; 571 int day, hour, minute, month, second, serial, year; 572 573 if ((snap = strchr(name, '@')) == NULL) 574 return (false); 575 ++snap; 576 /* We'll grab the individual components and do some light validation. */ 577 if (sscanf(snap, "%d-%d-%d-%d:%d:%d-%d", &year, &month, &day, &hour, 578 &minute, &second, &serial) != 7) 579 return (false); 580 return (year >= 1970) && (month >= 1 && month <= 12) && 581 (day >= 1 && day <= 31) && (hour >= 0 && hour <= 23) && 582 (minute >= 0 && minute <= 59) && (second >= 0 && second <= 60) && 583 serial >= 0; 584 } 585 586 int 587 be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name, 588 bool recursive, char *result) 589 { 590 char buf[BE_MAXPATHLEN]; 591 int err; 592 593 be_root_concat(lbh, source, buf); 594 595 if ((err = be_exists(lbh, buf)) != 0) 596 return (set_error(lbh, err)); 597 598 if (snap_name != NULL) { 599 if (strlcat(buf, "@", sizeof(buf)) >= sizeof(buf)) 600 return (set_error(lbh, BE_ERR_INVALIDNAME)); 601 602 if (strlcat(buf, snap_name, sizeof(buf)) >= sizeof(buf)) 603 return (set_error(lbh, BE_ERR_INVALIDNAME)); 604 605 if (result != NULL) 606 snprintf(result, BE_MAXPATHLEN, "%s@%s", source, 607 snap_name); 608 } else { 609 be_setup_snapshot_name(lbh, buf, sizeof(buf)); 610 611 if (result != NULL && strlcpy(result, strrchr(buf, '/') + 1, 612 sizeof(buf)) >= sizeof(buf)) 613 return (set_error(lbh, BE_ERR_INVALIDNAME)); 614 } 615 if ((err = zfs_snapshot(lbh->lzh, buf, recursive, NULL)) != 0) { 616 switch (err) { 617 case EZFS_INVALIDNAME: 618 return (set_error(lbh, BE_ERR_INVALIDNAME)); 619 620 default: 621 /* 622 * The other errors that zfs_ioc_snapshot might return 623 * shouldn't happen if we've set things up properly, so 624 * we'll gloss over them and call it UNKNOWN as it will 625 * require further triage. 626 */ 627 if (errno == ENOTSUP) 628 return (set_error(lbh, BE_ERR_NOPOOL)); 629 return (set_error(lbh, BE_ERR_UNKNOWN)); 630 } 631 } 632 633 return (BE_ERR_SUCCESS); 634 } 635 636 637 /* 638 * Create the boot environment specified by the name parameter 639 */ 640 int 641 be_create(libbe_handle_t *lbh, const char *name) 642 { 643 int err; 644 645 err = be_create_from_existing(lbh, name, be_active_path(lbh)); 646 647 return (set_error(lbh, err)); 648 } 649 650 static int 651 be_deep_clone_prop(int prop, void *cb) 652 { 653 int err; 654 struct libbe_dccb *dccb; 655 zprop_source_t src; 656 char pval[BE_MAXPATHLEN]; 657 char source[BE_MAXPATHLEN]; 658 char *val; 659 660 dccb = cb; 661 /* Skip some properties we don't want to touch */ 662 switch (prop) { 663 /* 664 * libzfs insists on these being naturally inherited in the 665 * cloning process. 666 */ 667 case ZFS_PROP_KEYFORMAT: 668 case ZFS_PROP_KEYLOCATION: 669 case ZFS_PROP_ENCRYPTION: 670 case ZFS_PROP_PBKDF2_ITERS: 671 672 /* FALLTHROUGH */ 673 case ZFS_PROP_CANMOUNT: /* Forced by libbe */ 674 return (ZPROP_CONT); 675 } 676 677 /* Don't copy readonly properties */ 678 if (zfs_prop_readonly(prop)) 679 return (ZPROP_CONT); 680 681 if ((err = zfs_prop_get(dccb->zhp, prop, (char *)&pval, 682 sizeof(pval), &src, (char *)&source, sizeof(source), false))) 683 /* Just continue if we fail to read a property */ 684 return (ZPROP_CONT); 685 686 /* 687 * Only copy locally defined or received properties. This continues 688 * to avoid temporary/default/local properties intentionally without 689 * breaking received datasets. 690 */ 691 if (src != ZPROP_SRC_LOCAL && src != ZPROP_SRC_RECEIVED) 692 return (ZPROP_CONT); 693 694 /* Augment mountpoint with altroot, if needed */ 695 val = pval; 696 if (prop == ZFS_PROP_MOUNTPOINT) 697 val = be_mountpoint_augmented(dccb->lbh, val); 698 699 nvlist_add_string(dccb->props, zfs_prop_to_name(prop), val); 700 701 return (ZPROP_CONT); 702 } 703 704 /* 705 * Return the corresponding boot environment path for a given 706 * dataset path, the constructed path is placed in 'result'. 707 * 708 * example: say our new boot environment name is 'bootenv' and 709 * the dataset path is 'zroot/ROOT/default/data/set'. 710 * 711 * result should produce: 'zroot/ROOT/bootenv/data/set' 712 */ 713 static int 714 be_get_path(struct libbe_deep_clone *ldc, const char *dspath, char *result, int result_size) 715 { 716 char *pos; 717 char *child_dataset; 718 719 /* match the root path for the boot environments */ 720 pos = strstr(dspath, ldc->lbh->root); 721 722 /* no match, different pools? */ 723 if (pos == NULL) 724 return (BE_ERR_BADPATH); 725 726 /* root path of the new boot environment */ 727 snprintf(result, result_size, "%s/%s", ldc->lbh->root, ldc->bename); 728 729 /* gets us to the parent dataset, the +1 consumes a trailing slash */ 730 pos += strlen(ldc->lbh->root) + 1; 731 732 /* skip the parent dataset */ 733 if ((child_dataset = strchr(pos, '/')) != NULL) 734 strlcat(result, child_dataset, result_size); 735 736 return (BE_ERR_SUCCESS); 737 } 738 739 static int 740 be_clone_cb(zfs_handle_t *ds, void *data) 741 { 742 int err; 743 char be_path[BE_MAXPATHLEN]; 744 char snap_path[BE_MAXPATHLEN]; 745 const char *dspath; 746 zfs_handle_t *snap_hdl; 747 nvlist_t *props; 748 struct libbe_deep_clone *ldc; 749 struct libbe_dccb dccb; 750 751 ldc = (struct libbe_deep_clone *)data; 752 dspath = zfs_get_name(ds); 753 754 snprintf(snap_path, sizeof(snap_path), "%s@%s", dspath, ldc->snapname); 755 756 /* construct the boot environment path from the dataset we're cloning */ 757 if (be_get_path(ldc, dspath, be_path, sizeof(be_path)) != BE_ERR_SUCCESS) 758 return (BE_ERR_UNKNOWN); 759 760 /* the dataset to be created (i.e. the boot environment) already exists */ 761 if (zfs_dataset_exists(ldc->lbh->lzh, be_path, ZFS_TYPE_DATASET)) 762 return (BE_ERR_EXISTS); 763 764 /* no snapshot found for this dataset, silently skip it */ 765 if (!zfs_dataset_exists(ldc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) 766 return (0); 767 768 if ((snap_hdl = 769 zfs_open(ldc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) == NULL) 770 return (BE_ERR_ZFSOPEN); 771 772 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 773 nvlist_add_string(props, "canmount", "noauto"); 774 775 dccb.lbh = ldc->lbh; 776 dccb.zhp = ds; 777 dccb.props = props; 778 if (zprop_iter(be_deep_clone_prop, &dccb, B_FALSE, B_FALSE, 779 ZFS_TYPE_FILESYSTEM) == ZPROP_INVAL) 780 return (-1); 781 782 if ((err = zfs_clone(snap_hdl, be_path, props)) != 0) 783 return (BE_ERR_ZFSCLONE); 784 785 nvlist_free(props); 786 zfs_close(snap_hdl); 787 788 if (ldc->depth_limit == -1 || ldc->depth < ldc->depth_limit) { 789 ldc->depth++; 790 err = zfs_iter_filesystems(ds, be_clone_cb, ldc); 791 ldc->depth--; 792 } 793 794 return (err); 795 } 796 797 /* 798 * Create a boot environment with a given name from a given snapshot. 799 * Snapshots can be in the format 'zroot/ROOT/default@snapshot' or 800 * 'default@snapshot'. In the latter case, 'default@snapshot' will be prepended 801 * with the root path that libbe was initailized with. 802 */ 803 static int 804 be_clone(libbe_handle_t *lbh, const char *bename, const char *snapshot, int depth) 805 { 806 int err; 807 char snap_path[BE_MAXPATHLEN]; 808 char *parentname, *snapname; 809 zfs_handle_t *parent_hdl; 810 struct libbe_deep_clone ldc; 811 812 /* ensure the boot environment name is valid */ 813 if ((err = be_validate_name(lbh, bename)) != 0) 814 return (set_error(lbh, err)); 815 816 /* 817 * prepend the boot environment root path if we're 818 * given a partial snapshot name. 819 */ 820 if ((err = be_root_concat(lbh, snapshot, snap_path)) != 0) 821 return (set_error(lbh, err)); 822 823 /* ensure the snapshot exists */ 824 if ((err = be_validate_snap(lbh, snap_path)) != 0) 825 return (set_error(lbh, err)); 826 827 /* get a copy of the snapshot path so we can disect it */ 828 if ((parentname = strdup(snap_path)) == NULL) 829 return (set_error(lbh, BE_ERR_UNKNOWN)); 830 831 /* split dataset name from snapshot name */ 832 snapname = strchr(parentname, '@'); 833 if (snapname == NULL) { 834 free(parentname); 835 return (set_error(lbh, BE_ERR_UNKNOWN)); 836 } 837 *snapname = '\0'; 838 snapname++; 839 840 /* set-up the boot environment */ 841 ldc.lbh = lbh; 842 ldc.bename = bename; 843 ldc.snapname = snapname; 844 ldc.depth = 0; 845 ldc.depth_limit = depth; 846 847 /* the boot environment will be cloned from this dataset */ 848 parent_hdl = zfs_open(lbh->lzh, parentname, ZFS_TYPE_DATASET); 849 850 /* create the boot environment */ 851 err = be_clone_cb(parent_hdl, &ldc); 852 853 free(parentname); 854 return (set_error(lbh, err)); 855 } 856 857 /* 858 * Create a boot environment from pre-existing snapshot, specifying a depth. 859 */ 860 int be_create_depth(libbe_handle_t *lbh, const char *bename, 861 const char *snap, int depth) 862 { 863 return (be_clone(lbh, bename, snap, depth)); 864 } 865 866 /* 867 * Create the boot environment from pre-existing snapshot 868 */ 869 int 870 be_create_from_existing_snap(libbe_handle_t *lbh, const char *bename, 871 const char *snap) 872 { 873 return (be_clone(lbh, bename, snap, -1)); 874 } 875 876 877 /* 878 * Create a boot environment from an existing boot environment 879 */ 880 int 881 be_create_from_existing(libbe_handle_t *lbh, const char *bename, const char *old) 882 { 883 int err; 884 char snap[BE_MAXPATHLEN]; 885 886 if ((err = be_snapshot(lbh, old, NULL, true, snap)) != 0) 887 return (set_error(lbh, err)); 888 889 err = be_clone(lbh, bename, snap, -1); 890 891 return (set_error(lbh, err)); 892 } 893 894 /* 895 * Create a zfs dataset and map the return to libbe error. 896 */ 897 static int 898 be_zfs_create(libzfs_handle_t *lzh, const char *buf, zfs_type_t t, 899 nvlist_t *props) 900 { 901 int err; 902 903 if ((err = zfs_create(lzh, buf, t, props)) != 0) { 904 switch (err) { 905 case EZFS_EXISTS: 906 return (BE_ERR_EXISTS); 907 case EZFS_NOENT: 908 return (BE_ERR_NOENT); 909 case EZFS_BADTYPE: 910 case EZFS_BADVERSION: 911 return (BE_ERR_NOPOOL); 912 case EZFS_BADPROP: 913 default: 914 /* We set something up wrong, probably... */ 915 return (BE_ERR_UNKNOWN); 916 } 917 } 918 919 return (BE_ERR_SUCCESS); 920 } 921 922 923 /* 924 * Create an empty boot environment. 925 */ 926 int 927 be_create_empty(libbe_handle_t *lbh, const char *bename) 928 { 929 char buf[BE_MAXPATHLEN]; 930 int err; 931 932 if ((err = be_validate_name(lbh, bename)) != 0) 933 return (set_error(lbh, err)); 934 935 if ((err = be_root_concat(lbh, bename, buf)) != 0) 936 return (set_error(lbh, err)); 937 938 err = be_zfs_create(lbh->lzh, buf, ZFS_TYPE_FILESYSTEM, NULL); 939 return (set_error(lbh, err)); 940 } 941 942 943 /* 944 * Verifies that a snapshot has a valid name, exists, and has a mountpoint of 945 * '/'. Returns BE_ERR_SUCCESS (0), upon success, or the relevant BE_ERR_* upon 946 * failure. Does not set the internal library error state. 947 */ 948 int 949 be_validate_snap(libbe_handle_t *lbh, const char *snap_name) 950 { 951 952 if (strlen(snap_name) >= BE_MAXPATHLEN) 953 return (BE_ERR_PATHLEN); 954 955 if (!zfs_name_valid(snap_name, ZFS_TYPE_SNAPSHOT)) 956 return (BE_ERR_INVALIDNAME); 957 958 if (!zfs_dataset_exists(lbh->lzh, snap_name, 959 ZFS_TYPE_SNAPSHOT)) 960 return (BE_ERR_NOENT); 961 962 return (BE_ERR_SUCCESS); 963 } 964 965 966 /* 967 * Idempotently appends the name argument to the root boot environment path 968 * and copies the resulting string into the result buffer (which is assumed 969 * to be at least BE_MAXPATHLEN characters long. Returns BE_ERR_SUCCESS upon 970 * success, BE_ERR_PATHLEN if the resulting path is longer than BE_MAXPATHLEN, 971 * or BE_ERR_INVALIDNAME if the name is a path that does not begin with 972 * zfs_be_root. Does not set internal library error state. 973 */ 974 int 975 be_root_concat(libbe_handle_t *lbh, const char *name, char *result) 976 { 977 size_t name_len, root_len; 978 979 name_len = strlen(name); 980 root_len = strlen(lbh->root); 981 982 /* Act idempotently; return be name if it is already a full path */ 983 if (strrchr(name, '/') != NULL) { 984 if (strstr(name, lbh->root) != name) 985 return (BE_ERR_INVALIDNAME); 986 987 if (name_len >= BE_MAXPATHLEN) 988 return (BE_ERR_PATHLEN); 989 990 strlcpy(result, name, BE_MAXPATHLEN); 991 return (BE_ERR_SUCCESS); 992 } else if (name_len + root_len + 1 < BE_MAXPATHLEN) { 993 snprintf(result, BE_MAXPATHLEN, "%s/%s", lbh->root, 994 name); 995 return (BE_ERR_SUCCESS); 996 } 997 998 return (BE_ERR_PATHLEN); 999 } 1000 1001 1002 /* 1003 * Verifies the validity of a boot environment name (A-Za-z0-9-_.). Returns 1004 * BE_ERR_SUCCESS (0) if name is valid, otherwise returns BE_ERR_INVALIDNAME 1005 * or BE_ERR_PATHLEN. 1006 * Does not set internal library error state. 1007 */ 1008 int 1009 be_validate_name(libbe_handle_t *lbh, const char *name) 1010 { 1011 1012 /* 1013 * Impose the additional restriction that the entire dataset name must 1014 * not exceed the maximum length of a dataset, i.e. MAXNAMELEN. 1015 */ 1016 if (strlen(lbh->root) + 1 + strlen(name) > MAXNAMELEN) 1017 return (BE_ERR_PATHLEN); 1018 1019 if (!zfs_name_valid(name, ZFS_TYPE_DATASET)) 1020 return (BE_ERR_INVALIDNAME); 1021 1022 /* 1023 * ZFS allows spaces in boot environment names, but the kernel can't 1024 * handle booting from such a dataset right now. vfs.root.mountfrom 1025 * is defined to be a space-separated list, and there's no protocol for 1026 * escaping whitespace in the path component of a dev:path spec. So 1027 * while loader can handle this situation alright, it can't safely pass 1028 * it on to mountroot. 1029 */ 1030 if (strchr(name, ' ') != NULL) 1031 return (BE_ERR_INVALIDNAME); 1032 1033 return (BE_ERR_SUCCESS); 1034 } 1035 1036 1037 /* 1038 * usage 1039 */ 1040 int 1041 be_rename(libbe_handle_t *lbh, const char *old, const char *new) 1042 { 1043 char full_old[BE_MAXPATHLEN]; 1044 char full_new[BE_MAXPATHLEN]; 1045 zfs_handle_t *zfs_hdl; 1046 int err; 1047 1048 /* 1049 * be_validate_name is documented not to set error state, so we should 1050 * do so here. 1051 */ 1052 if ((err = be_validate_name(lbh, new)) != 0) 1053 return (set_error(lbh, err)); 1054 if ((err = be_root_concat(lbh, old, full_old)) != 0) 1055 return (set_error(lbh, err)); 1056 if ((err = be_root_concat(lbh, new, full_new)) != 0) 1057 return (set_error(lbh, err)); 1058 1059 if (!zfs_dataset_exists(lbh->lzh, full_old, ZFS_TYPE_DATASET)) 1060 return (set_error(lbh, BE_ERR_NOENT)); 1061 1062 if (zfs_dataset_exists(lbh->lzh, full_new, ZFS_TYPE_DATASET)) 1063 return (set_error(lbh, BE_ERR_EXISTS)); 1064 1065 if ((zfs_hdl = zfs_open(lbh->lzh, full_old, 1066 ZFS_TYPE_FILESYSTEM)) == NULL) 1067 return (set_error(lbh, BE_ERR_ZFSOPEN)); 1068 1069 /* recurse, nounmount, forceunmount */ 1070 struct renameflags flags = { 1071 .nounmount = 1, 1072 }; 1073 err = zfs_rename(zfs_hdl, full_new, flags); 1074 if (err != 0) 1075 goto error; 1076 1077 /* handle renaming bootonce */ 1078 if (lbh->bootonce != NULL && 1079 strcmp(full_old, lbh->bootonce) == 0) 1080 err = be_activate(lbh, new, true); 1081 1082 error: 1083 zfs_close(zfs_hdl); 1084 return (set_error(lbh, err)); 1085 } 1086 1087 1088 int 1089 be_export(libbe_handle_t *lbh, const char *bootenv, int fd) 1090 { 1091 char snap_name[BE_MAXPATHLEN]; 1092 char buf[BE_MAXPATHLEN]; 1093 zfs_handle_t *zfs; 1094 sendflags_t flags = { 0 }; 1095 int err; 1096 1097 if ((err = be_snapshot(lbh, bootenv, NULL, true, snap_name)) != 0) 1098 /* Use the error set by be_snapshot */ 1099 return (err); 1100 1101 be_root_concat(lbh, snap_name, buf); 1102 1103 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL) 1104 return (set_error(lbh, BE_ERR_ZFSOPEN)); 1105 1106 err = zfs_send_one(zfs, NULL, fd, &flags, /* redactbook */ NULL); 1107 zfs_close(zfs); 1108 1109 return (err); 1110 } 1111 1112 1113 int 1114 be_import(libbe_handle_t *lbh, const char *bootenv, int fd) 1115 { 1116 char buf[BE_MAXPATHLEN]; 1117 nvlist_t *props; 1118 zfs_handle_t *zfs; 1119 recvflags_t flags = { .nomount = 1 }; 1120 int err; 1121 1122 be_root_concat(lbh, bootenv, buf); 1123 1124 if ((err = zfs_receive(lbh->lzh, buf, NULL, &flags, fd, NULL)) != 0) { 1125 switch (err) { 1126 case EINVAL: 1127 return (set_error(lbh, BE_ERR_NOORIGIN)); 1128 case ENOENT: 1129 return (set_error(lbh, BE_ERR_NOENT)); 1130 case EIO: 1131 return (set_error(lbh, BE_ERR_IO)); 1132 default: 1133 return (set_error(lbh, BE_ERR_UNKNOWN)); 1134 } 1135 } 1136 1137 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_FILESYSTEM)) == NULL) 1138 return (set_error(lbh, BE_ERR_ZFSOPEN)); 1139 1140 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 1141 nvlist_add_string(props, "canmount", "noauto"); 1142 nvlist_add_string(props, "mountpoint", "none"); 1143 1144 err = zfs_prop_set_list(zfs, props); 1145 nvlist_free(props); 1146 1147 zfs_close(zfs); 1148 1149 if (err != 0) 1150 return (set_error(lbh, BE_ERR_UNKNOWN)); 1151 1152 return (0); 1153 } 1154 1155 #if SOON 1156 static int 1157 be_create_child_noent(libbe_handle_t *lbh, const char *active, 1158 const char *child_path) 1159 { 1160 nvlist_t *props; 1161 zfs_handle_t *zfs; 1162 int err; 1163 1164 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 1165 nvlist_add_string(props, "canmount", "noauto"); 1166 nvlist_add_string(props, "mountpoint", child_path); 1167 1168 /* Create */ 1169 if ((err = be_zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET, 1170 props)) != 0) { 1171 return (set_error(lbh, err)); 1172 } 1173 nvlist_free(props); 1174 1175 if ((zfs = zfs_open(lbh->lzh, active, ZFS_TYPE_DATASET)) == NULL) 1176 return (set_error(lbh, BE_ERR_ZFSOPEN)); 1177 1178 /* Set props */ 1179 if ((err = zfs_prop_set(zfs, "canmount", "noauto")) != 0) { 1180 zfs_close(zfs); 1181 /* 1182 * Similar to other cases, this shouldn't fail unless we've 1183 * done something wrong. This is a new dataset that shouldn't 1184 * have been mounted anywhere between creation and now. 1185 */ 1186 if (err == EZFS_NOMEM) 1187 return (set_error(lbh, BE_ERR_NOMEM)); 1188 return (set_error(lbh, BE_ERR_UNKNOWN)); 1189 } 1190 zfs_close(zfs); 1191 return (BE_ERR_SUCCESS); 1192 } 1193 1194 static int 1195 be_create_child_cloned(libbe_handle_t *lbh, const char *active) 1196 { 1197 char buf[BE_MAXPATHLEN], tmp[BE_MAXPATHLEN]; 1198 zfs_handle_t *zfs; 1199 int err; 1200 1201 /* XXX TODO ? */ 1202 1203 /* 1204 * Establish if the existing path is a zfs dataset or just 1205 * the subdirectory of one 1206 */ 1207 strlcpy(tmp, "tmp/be_snap.XXXXX", sizeof(tmp)); 1208 if (mktemp(tmp) == NULL) 1209 return (set_error(lbh, BE_ERR_UNKNOWN)); 1210 1211 be_root_concat(lbh, tmp, buf); 1212 printf("Here %s?\n", buf); 1213 if ((err = zfs_snapshot(lbh->lzh, buf, false, NULL)) != 0) { 1214 switch (err) { 1215 case EZFS_INVALIDNAME: 1216 return (set_error(lbh, BE_ERR_INVALIDNAME)); 1217 1218 default: 1219 /* 1220 * The other errors that zfs_ioc_snapshot might return 1221 * shouldn't happen if we've set things up properly, so 1222 * we'll gloss over them and call it UNKNOWN as it will 1223 * require further triage. 1224 */ 1225 if (errno == ENOTSUP) 1226 return (set_error(lbh, BE_ERR_NOPOOL)); 1227 return (set_error(lbh, BE_ERR_UNKNOWN)); 1228 } 1229 } 1230 1231 /* Clone */ 1232 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL) 1233 return (BE_ERR_ZFSOPEN); 1234 1235 if ((err = zfs_clone(zfs, active, NULL)) != 0) 1236 /* XXX TODO correct error */ 1237 return (set_error(lbh, BE_ERR_UNKNOWN)); 1238 1239 /* set props */ 1240 zfs_close(zfs); 1241 return (BE_ERR_SUCCESS); 1242 } 1243 1244 int 1245 be_add_child(libbe_handle_t *lbh, const char *child_path, bool cp_if_exists) 1246 { 1247 struct stat sb; 1248 char active[BE_MAXPATHLEN], buf[BE_MAXPATHLEN]; 1249 nvlist_t *props; 1250 const char *s; 1251 1252 /* Require absolute paths */ 1253 if (*child_path != '/') 1254 return (set_error(lbh, BE_ERR_BADPATH)); 1255 1256 strlcpy(active, be_active_path(lbh), BE_MAXPATHLEN); 1257 strcpy(buf, active); 1258 1259 /* Create non-mountable parent dataset(s) */ 1260 s = child_path; 1261 for (char *p; (p = strchr(s+1, '/')) != NULL; s = p) { 1262 size_t len = p - s; 1263 strncat(buf, s, len); 1264 1265 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 1266 nvlist_add_string(props, "canmount", "off"); 1267 nvlist_add_string(props, "mountpoint", "none"); 1268 zfs_create(lbh->lzh, buf, ZFS_TYPE_DATASET, props); 1269 nvlist_free(props); 1270 } 1271 1272 /* Path does not exist as a descendent of / yet */ 1273 if (strlcat(active, child_path, BE_MAXPATHLEN) >= BE_MAXPATHLEN) 1274 return (set_error(lbh, BE_ERR_PATHLEN)); 1275 1276 if (stat(child_path, &sb) != 0) { 1277 /* Verify that error is ENOENT */ 1278 if (errno != ENOENT) 1279 return (set_error(lbh, BE_ERR_UNKNOWN)); 1280 return (be_create_child_noent(lbh, active, child_path)); 1281 } else if (cp_if_exists) 1282 /* Path is already a descendent of / and should be copied */ 1283 return (be_create_child_cloned(lbh, active)); 1284 return (set_error(lbh, BE_ERR_EXISTS)); 1285 } 1286 #endif /* SOON */ 1287 1288 /* 1289 * Deactivate old BE dataset; currently just sets canmount=noauto or 1290 * resets boot once configuration. 1291 */ 1292 int 1293 be_deactivate(libbe_handle_t *lbh, const char *ds, bool temporary) 1294 { 1295 zfs_handle_t *zfs; 1296 1297 if (temporary) { 1298 return (lzbe_set_boot_device( 1299 zpool_get_name(lbh->active_phandle), lzbe_add, NULL)); 1300 } 1301 1302 if ((zfs = zfs_open(lbh->lzh, ds, ZFS_TYPE_DATASET)) == NULL) 1303 return (1); 1304 if (zfs_prop_set(zfs, "canmount", "noauto") != 0) 1305 return (1); 1306 zfs_close(zfs); 1307 return (0); 1308 } 1309 1310 static int 1311 be_zfs_promote_cb(zfs_handle_t *zhp, void *data) 1312 { 1313 char origin[BE_MAXPATHLEN]; 1314 bool *found_origin = (bool *)data; 1315 int err; 1316 1317 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin, sizeof(origin), 1318 NULL, NULL, 0, true) == 0) { 1319 *found_origin = true; 1320 err = zfs_promote(zhp); 1321 if (err) 1322 return (err); 1323 } 1324 1325 return (zfs_iter_filesystems(zhp, be_zfs_promote_cb, data)); 1326 } 1327 1328 static int 1329 be_zfs_promote(zfs_handle_t *zhp, bool *found_origin) 1330 { 1331 *found_origin = false; 1332 return (be_zfs_promote_cb(zhp, (void *)found_origin)); 1333 } 1334 1335 int 1336 be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary) 1337 { 1338 char be_path[BE_MAXPATHLEN]; 1339 zfs_handle_t *zhp; 1340 int err; 1341 bool found_origin; 1342 1343 be_root_concat(lbh, bootenv, be_path); 1344 1345 /* Note: be_exists fails if mountpoint is not / */ 1346 if ((err = be_exists(lbh, be_path)) != 0) 1347 return (set_error(lbh, err)); 1348 1349 if (temporary) { 1350 return (lzbe_set_boot_device( 1351 zpool_get_name(lbh->active_phandle), lzbe_add, be_path)); 1352 } else { 1353 if (strncmp(lbh->bootfs, "-", 1) != 0 && 1354 be_deactivate(lbh, lbh->bootfs, false) != 0) 1355 return (-1); 1356 1357 /* Obtain bootenv zpool */ 1358 err = zpool_set_prop(lbh->active_phandle, "bootfs", be_path); 1359 if (err) 1360 return (-1); 1361 1362 for (;;) { 1363 zhp = zfs_open(lbh->lzh, be_path, ZFS_TYPE_FILESYSTEM); 1364 if (zhp == NULL) 1365 return (-1); 1366 1367 err = be_zfs_promote(zhp, &found_origin); 1368 1369 zfs_close(zhp); 1370 if (!found_origin) 1371 break; 1372 if (err) 1373 return (err); 1374 } 1375 1376 if (err) 1377 return (-1); 1378 } 1379 1380 return (BE_ERR_SUCCESS); 1381 } 1382 1383 int 1384 be_log_history(libbe_handle_t *lbh, const char *message) 1385 { 1386 int err; 1387 1388 err = zpool_log_history(lbh->lzh, message); 1389 if (err) 1390 return (set_error(lbh, BE_ERR_UNKNOWN)); 1391 1392 return (BE_ERR_SUCCESS); 1393 } 1394