4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2016 Toomas Soome <tsoome@me.com>
26 * Copyright (c) 2015 by Delphix. All rights reserved.
37 #include <libnvpair.h>
44 #include <sys/types.h>
45 #include <sys/vfstab.h>
46 #include <sys/param.h>
47 #include <sys/systeminfo.h>
54 #include <libdevinfo.h>
58 #include <libbe_priv.h>
59 #include <boot_utils.h>
61 #include <ficlplatform/emu.h>
63 /* Private function prototypes */
64 static int update_dataset(char *, int, char *, char *, char *);
65 static int _update_vfstab(char *, char *, char *, char *, be_fs_list_data_t
*);
66 static int be_open_menu(char *, char *, FILE **, char *, boolean_t
);
67 static int be_create_menu(char *, char *, FILE **, char *);
68 static char *be_get_auto_name(char *, char *, boolean_t
);
71 * Global error printing
73 boolean_t do_print
= B_FALSE
;
78 typedef struct zone_be_name_cb_data
{
81 } zone_be_name_cb_data_t
;
83 /* ******************************************************************** */
84 /* Public Functions */
85 /* ******************************************************************** */
88 * Callback for ficl to suppress all output from ficl, as we do not
89 * want to confuse user with messages from ficl, and we are only
90 * checking results from function calls.
94 ficlSuppressTextOutput(ficlCallback
*cb
, char *text
)
96 /* This function is intentionally doing nothing. */
100 * Function: be_get_boot_args
101 * Description: Returns the fast boot argument string for enumerated BE.
103 * fbarg - pointer to argument string.
104 * entry - index of BE.
106 * fast boot argument string.
111 be_get_boot_args(char **fbarg
, int entry
)
113 be_node_list_t
*node
, *be_nodes
= NULL
;
114 be_transaction_data_t bt
= {0};
115 char *mountpoint
= NULL
;
116 boolean_t be_mounted
= B_FALSE
;
117 int ret
= BE_SUCCESS
;
123 return (BE_ERR_INIT
);
126 * need pool name, menu.lst has entries from our pool only
128 ret
= be_find_current_be(&bt
);
129 if (ret
!= BE_SUCCESS
) {
135 * be_get_boot_args() is for loader, fail with grub will trigger
143 ret
= _be_list(NULL
, &be_nodes
);
144 if (ret
!= BE_SUCCESS
)
148 * iterate through be_nodes,
149 * if entry == -1, stop if be_active_on_boot,
150 * else stop if index == entry.
153 for (node
= be_nodes
; node
!= NULL
; node
= node
->be_next_node
) {
154 if (strcmp(node
->be_rpool
, bt
.obe_zpool
) != 0)
156 if (entry
== BE_ENTRY_DEFAULT
&&
157 node
->be_active_on_boot
== B_TRUE
)
164 be_free_list(be_nodes
);
169 /* try to mount inactive be */
170 if (node
->be_active
== B_FALSE
) {
171 ret
= _be_mount(node
->be_node_name
, &mountpoint
,
172 BE_MOUNT_FLAG_NO_ZONES
);
173 if (ret
!= BE_SUCCESS
&& ret
!= BE_ERR_MOUNTED
) {
174 be_free_list(be_nodes
);
180 vm
= bf_init("", ficlSuppressTextOutput
);
183 * zfs MAXNAMELEN is 256, so we need to pick buf large enough
184 * to contain such names.
186 char buf
[MAXNAMELEN
* 2];
187 char *kernel_options
= NULL
;
193 * just try to interpret following words. on error
194 * we will be missing kernelname, and will get out.
196 (void) snprintf(buf
, sizeof (buf
), "set currdev=zfs:%s:",
198 ret
= ficlVmEvaluate(vm
, buf
);
199 if (ret
!= FICL_VM_STATUS_OUT_OF_TEXT
) {
200 be_print_err(gettext("be_get_boot_args: error "
201 "interpreting boot config: %d\n"), ret
);
203 ret
= BE_ERR_NO_MENU
;
206 (void) snprintf(buf
, sizeof (buf
),
207 "include /boot/forth/loader.4th");
208 ret
= ficlVmEvaluate(vm
, buf
);
209 if (ret
!= FICL_VM_STATUS_OUT_OF_TEXT
) {
210 be_print_err(gettext("be_get_boot_args: error "
211 "interpreting boot config: %d\n"), ret
);
213 ret
= BE_ERR_NO_MENU
;
216 (void) snprintf(buf
, sizeof (buf
), "start");
217 ret
= ficlVmEvaluate(vm
, buf
);
218 if (ret
!= FICL_VM_STATUS_OUT_OF_TEXT
) {
219 be_print_err(gettext("be_get_boot_args: error "
220 "interpreting boot config: %d\n"), ret
);
222 ret
= BE_ERR_NO_MENU
;
225 (void) snprintf(buf
, sizeof (buf
), "boot");
226 ret
= ficlVmEvaluate(vm
, buf
);
228 if (ret
!= FICL_VM_STATUS_OUT_OF_TEXT
) {
229 be_print_err(gettext("be_get_boot_args: error "
230 "interpreting boot config: %d\n"), ret
);
231 ret
= BE_ERR_NO_MENU
;
235 kernel_options
= getenv("boot-args");
236 kernel
= getenv("kernelname");
238 if (kernel
== NULL
) {
239 be_print_err(gettext("be_get_boot_args: no kernel\n"));
244 if ((zph
= zpool_open(g_zfs
, node
->be_rpool
)) == NULL
) {
245 be_print_err(gettext("be_get_boot_args: failed to "
246 "open root pool (%s): %s\n"), node
->be_rpool
,
247 libzfs_error_description(g_zfs
));
248 ret
= zfs_err_to_be_err(g_zfs
);
251 ret
= zpool_get_physpath(zph
, buf
, sizeof (buf
));
254 be_print_err(gettext("be_get_boot_args: failed to "
259 /* zpool_get_physpath() can return space separated list */
261 tmp
= strsep(&tmp
, " ");
263 if (kernel_options
== NULL
|| *kernel_options
== '\0')
264 (void) asprintf(fbarg
, "/ %s "
265 "-B zfs-bootfs=%s,bootpath=\"%s\"\n", kernel
,
266 node
->be_root_ds
, tmp
);
268 (void) asprintf(fbarg
, "/ %s %s "
269 "-B zfs-bootfs=%s,bootpath=\"%s\"\n", kernel
,
270 kernel_options
, node
->be_root_ds
, tmp
);
279 if (be_mounted
== B_TRUE
)
280 (void) _be_unmount(node
->be_node_name
, BE_UNMOUNT_FLAG_FORCE
);
281 be_free_list(be_nodes
);
285 free(bt
.obe_root_ds
);
287 free(bt
.obe_snap_name
);
288 free(bt
.obe_altroot
);
294 * Function: be_max_avail
295 * Description: Returns the available size for the zfs dataset passed in.
297 * dataset - The dataset we want to get the available space for.
298 * ret - The available size will be returned in this.
300 * The error returned by the zfs get property function.
305 be_max_avail(char *dataset
, uint64_t *ret
)
310 /* Initialize libzfs handle */
312 return (BE_ERR_INIT
);
314 zhp
= zfs_open(g_zfs
, dataset
, ZFS_TYPE_DATASET
);
317 * The zfs_open failed return an error
319 err
= zfs_err_to_be_err(g_zfs
);
321 err
= be_maxsize_avail(zhp
, ret
);
329 * Function: libbe_print_errors
330 * Description: Turns on/off error output for the library.
332 * set_do_print - Boolean that turns library error
333 * printing on or off.
340 libbe_print_errors(boolean_t set_do_print
)
342 do_print
= set_do_print
;
345 /* ******************************************************************** */
346 /* Semi-Private Functions */
347 /* ******************************************************************** */
350 * Function: be_zfs_init
351 * Description: Initializes the libary global libzfs handle.
358 * Semi-private (library wide use only)
365 if ((g_zfs
= libzfs_init()) == NULL
) {
366 be_print_err(gettext("be_zfs_init: failed to initialize ZFS "
375 * Function: be_zfs_fini
376 * Description: Closes the library global libzfs handle if it currently open.
382 * Semi-private (library wide use only)
394 * Function: be_get_defaults
395 * Description: Open defaults and gets be default paramets
397 * defaults - be defaults struct
401 * Semi-private (library wide use only)
404 be_get_defaults(struct be_defaults
*defaults
)
408 defaults
->be_deflt_grub
= B_FALSE
;
409 defaults
->be_deflt_rpool_container
= B_FALSE
;
410 defaults
->be_deflt_bename_starts_with
[0] = '\0';
412 if ((defp
= defopen_r(BE_DEFAULTS
)) != NULL
) {
413 const char *res
= defread_r(BE_DFLT_BENAME_STARTS
, defp
);
414 if (res
!= NULL
&& res
[0] != '\0') {
415 (void) strlcpy(defaults
->be_deflt_bename_starts_with
,
416 res
, ZFS_MAX_DATASET_NAME_LEN
);
417 defaults
->be_deflt_rpool_container
= B_TRUE
;
419 if (be_is_isa("i386")) {
420 res
= defread_r(BE_DFLT_BE_HAS_GRUB
, defp
);
421 if (res
!= NULL
&& res
[0] != '\0') {
422 if (strcasecmp(res
, "true") == 0)
423 defaults
->be_deflt_grub
= B_TRUE
;
431 * Function: be_make_root_ds
432 * Description: Generate string for BE's root dataset given the pool
433 * it lives in and the BE name.
435 * zpool - pointer zpool name.
436 * be_name - pointer to BE name.
437 * be_root_ds - pointer to buffer to return BE root dataset in.
438 * be_root_ds_size - size of be_root_ds
442 * Semi-private (library wide use only)
445 be_make_root_ds(const char *zpool
, const char *be_name
, char *be_root_ds
,
448 struct be_defaults be_defaults
;
449 be_get_defaults(&be_defaults
);
450 char *root_ds
= NULL
;
452 if (getzoneid() == GLOBAL_ZONEID
) {
453 if (be_defaults
.be_deflt_rpool_container
) {
454 (void) snprintf(be_root_ds
, be_root_ds_size
,
455 "%s/%s", zpool
, be_name
);
457 (void) snprintf(be_root_ds
, be_root_ds_size
,
458 "%s/%s/%s", zpool
, BE_CONTAINER_DS_NAME
, be_name
);
462 * In non-global zone we can use path from mounted root dataset
463 * to generate BE's root dataset string.
465 if ((root_ds
= be_get_ds_from_dir("/")) != NULL
) {
466 (void) snprintf(be_root_ds
, be_root_ds_size
, "%s/%s",
467 dirname(root_ds
), be_name
);
469 be_print_err(gettext("be_make_root_ds: zone root "
470 "dataset is not mounted\n"));
477 * Function: be_make_container_ds
478 * Description: Generate string for the BE container dataset given a pool name.
480 * zpool - pointer zpool name.
481 * container_ds - pointer to buffer to return BE container
483 * container_ds_size - size of container_ds
487 * Semi-private (library wide use only)
490 be_make_container_ds(const char *zpool
, char *container_ds
,
491 int container_ds_size
)
493 struct be_defaults be_defaults
;
494 be_get_defaults(&be_defaults
);
495 char *root_ds
= NULL
;
497 if (getzoneid() == GLOBAL_ZONEID
) {
498 if (be_defaults
.be_deflt_rpool_container
) {
499 (void) snprintf(container_ds
, container_ds_size
,
502 (void) snprintf(container_ds
, container_ds_size
,
503 "%s/%s", zpool
, BE_CONTAINER_DS_NAME
);
506 if ((root_ds
= be_get_ds_from_dir("/")) != NULL
) {
507 (void) strlcpy(container_ds
, dirname(root_ds
),
510 be_print_err(gettext("be_make_container_ds: zone root "
511 "dataset is not mounted\n"));
518 * Function: be_make_name_from_ds
519 * Description: This function takes a dataset name and strips off the
520 * BE container dataset portion from the beginning. The
521 * returned name is allocated in heap storage, so the caller
522 * is responsible for freeing it.
524 * dataset - dataset to get name from.
525 * rc_loc - dataset underwhich the root container dataset lives.
527 * name of dataset relative to BE container dataset.
528 * NULL if dataset is not under a BE root dataset.
530 * Semi-primate (library wide use only)
533 be_make_name_from_ds(const char *dataset
, char *rc_loc
)
535 char ds
[ZFS_MAX_DATASET_NAME_LEN
];
538 struct be_defaults be_defaults
;
539 int rlen
= strlen(rc_loc
);
541 be_get_defaults(&be_defaults
);
544 * First token is the location of where the root container dataset
545 * lives; it must match rc_loc.
547 if (strncmp(dataset
, rc_loc
, rlen
) == 0 && dataset
[rlen
] == '/')
548 (void) strlcpy(ds
, dataset
+ rlen
+ 1, sizeof (ds
));
552 if (be_defaults
.be_deflt_rpool_container
) {
553 if ((name
= strdup(ds
)) == NULL
) {
554 be_print_err(gettext("be_make_name_from_ds: "
555 "memory allocation failed\n"));
559 /* Second token must be BE container dataset name */
560 if ((tok
= strtok(ds
, "/")) == NULL
||
561 strcmp(tok
, BE_CONTAINER_DS_NAME
) != 0)
564 /* Return the remaining token if one exists */
565 if ((tok
= strtok(NULL
, "")) == NULL
)
568 if ((name
= strdup(tok
)) == NULL
) {
569 be_print_err(gettext("be_make_name_from_ds: "
570 "memory allocation failed\n"));
579 * Function: be_maxsize_avail
580 * Description: Returns the available size for the zfs handle passed in.
582 * zhp - A pointer to the open zfs handle.
583 * ret - The available size will be returned in this.
585 * The error returned by the zfs get property function.
587 * Semi-private (library wide use only)
590 be_maxsize_avail(zfs_handle_t
*zhp
, uint64_t *ret
)
592 return ((*ret
= zfs_prop_get_int(zhp
, ZFS_PROP_AVAILABLE
)));
596 * Function: be_append_menu
597 * Description: Appends an entry for a BE into the menu.lst.
599 * be_name - pointer to name of BE to add boot menu entry for.
600 * be_root_pool - pointer to name of pool BE lives in.
601 * boot_pool - Used if the pool containing the grub menu is
602 * different than the one contaiing the BE. This
603 * will normally be NULL.
604 * be_orig_root_ds - The root dataset for the BE. This is
605 * used to check to see if an entry already exists
607 * description - pointer to description of BE to be added in
608 * the title line for this BEs entry.
610 * BE_SUCCESS - Success
611 * be_errno_t - Failure
613 * Semi-private (library wide use only)
616 be_append_menu(char *be_name
, char *be_root_pool
, char *boot_pool
,
617 char *be_orig_root_ds
, char *description
)
619 zfs_handle_t
*zhp
= NULL
;
620 char menu_file
[MAXPATHLEN
];
621 char be_root_ds
[MAXPATHLEN
];
623 char temp_line
[BUFSIZ
];
624 char title
[MAXPATHLEN
];
625 char *entries
[BUFSIZ
];
626 char *tmp_entries
[BUFSIZ
];
627 char *pool_mntpnt
= NULL
;
628 char *ptmp_mntpnt
= NULL
;
629 char *orig_mntpnt
= NULL
;
630 boolean_t found_be
= B_FALSE
;
631 boolean_t found_orig_be
= B_FALSE
;
632 boolean_t found_title
= B_FALSE
;
633 boolean_t pool_mounted
= B_FALSE
;
634 boolean_t collect_lines
= B_FALSE
;
635 FILE *menu_fp
= NULL
;
636 int err
= 0, ret
= BE_SUCCESS
;
637 int i
, num_tmp_lines
= 0, num_lines
= 0;
639 if (be_name
== NULL
|| be_root_pool
== NULL
)
640 return (BE_ERR_INVAL
);
642 if (boot_pool
== NULL
)
643 boot_pool
= be_root_pool
;
645 if ((zhp
= zfs_open(g_zfs
, be_root_pool
, ZFS_TYPE_DATASET
)) == NULL
) {
646 be_print_err(gettext("be_append_menu: failed to open "
647 "pool dataset for %s: %s\n"), be_root_pool
,
648 libzfs_error_description(g_zfs
));
649 return (zfs_err_to_be_err(g_zfs
));
653 * Check to see if the pool's dataset is mounted. If it isn't we'll
654 * attempt to mount it.
656 if ((ret
= be_mount_pool(zhp
, &ptmp_mntpnt
, &orig_mntpnt
,
657 &pool_mounted
)) != BE_SUCCESS
) {
658 be_print_err(gettext("be_append_menu: pool dataset "
659 "(%s) could not be mounted\n"), be_root_pool
);
665 * Get the mountpoint for the root pool dataset.
667 if (!zfs_is_mounted(zhp
, &pool_mntpnt
)) {
668 be_print_err(gettext("be_append_menu: pool "
669 "dataset (%s) is not mounted. Can't set "
670 "the default BE in the grub menu.\n"), be_root_pool
);
671 ret
= BE_ERR_NO_MENU
;
676 * Check to see if this system supports grub
679 (void) snprintf(menu_file
, sizeof (menu_file
),
680 "%s%s", pool_mntpnt
, BE_GRUB_MENU
);
682 (void) snprintf(menu_file
, sizeof (menu_file
),
683 "%s%s", pool_mntpnt
, BE_SPARC_MENU
);
686 be_make_root_ds(be_root_pool
, be_name
, be_root_ds
, sizeof (be_root_ds
));
689 * Iterate through menu first to make sure the BE doesn't already
690 * have an entry in the menu.
692 * Additionally while iterating through the menu, if we have an
693 * original root dataset for a BE we're cloning from, we need to keep
694 * track of that BE's menu entry. We will then use the lines from
695 * that entry to create the entry for the new BE.
697 if ((ret
= be_open_menu(be_root_pool
, menu_file
,
698 &menu_fp
, "r", B_TRUE
)) != BE_SUCCESS
) {
700 } else if (menu_fp
== NULL
) {
701 ret
= BE_ERR_NO_MENU
;
708 while (fgets(line
, BUFSIZ
, menu_fp
)) {
711 (void) strlcpy(temp_line
, line
, BUFSIZ
);
712 tok
= strtok(line
, BE_WHITE_SPACE
);
714 if (tok
== NULL
|| tok
[0] == '#') {
716 } else if (strcmp(tok
, "title") == 0) {
717 collect_lines
= B_FALSE
;
718 if ((tok
= strtok(NULL
, "\n")) == NULL
)
719 (void) strlcpy(title
, "", sizeof (title
));
721 (void) strlcpy(title
, tok
, sizeof (title
));
722 found_title
= B_TRUE
;
724 if (num_tmp_lines
!= 0) {
725 for (i
= 0; i
< num_tmp_lines
; i
++) {
726 free(tmp_entries
[i
]);
727 tmp_entries
[i
] = NULL
;
731 } else if (strcmp(tok
, "bootfs") == 0) {
732 char *bootfs
= strtok(NULL
, BE_WHITE_SPACE
);
733 found_title
= B_FALSE
;
737 if (strcmp(bootfs
, be_root_ds
) == 0) {
742 if (be_orig_root_ds
!= NULL
&&
743 strcmp(bootfs
, be_orig_root_ds
) == 0 &&
746 found_orig_be
= B_TRUE
;
749 * Store the new title line
751 (void) snprintf(str
, BUFSIZ
, "title %s\n",
752 description
? description
: be_name
);
753 entries
[num_lines
] = strdup(str
);
756 * If there are any lines between the title
757 * and the bootfs line store these. Also
758 * free the temporary lines.
760 for (i
= 0; i
< num_tmp_lines
; i
++) {
761 entries
[num_lines
] = tmp_entries
[i
];
762 tmp_entries
[i
] = NULL
;
767 * Store the new bootfs line.
769 (void) snprintf(str
, BUFSIZ
, "bootfs %s\n",
771 entries
[num_lines
] = strdup(str
);
773 collect_lines
= B_TRUE
;
775 } else if (found_orig_be
&& collect_lines
) {
777 * get the rest of the lines for the original BE and
780 if (strstr(line
, BE_GRUB_COMMENT
) != NULL
||
781 strstr(line
, "BOOTADM") != NULL
)
783 if (strcmp(tok
, "splashimage") == 0) {
785 strdup("splashimage "
786 "/boot/splashimage.xpm\n");
788 entries
[num_lines
] = strdup(temp_line
);
791 } else if (found_title
&& !found_orig_be
) {
792 tmp_entries
[num_tmp_lines
] = strdup(temp_line
);
797 (void) fclose(menu_fp
);
801 * If an entry for this BE was already in the menu, then if
802 * that entry's title matches what we would have put in
803 * return success. Otherwise return failure.
805 char *new_title
= description
? description
: be_name
;
807 if (strcmp(title
, new_title
) == 0) {
811 if (be_remove_menu(be_name
, be_root_pool
,
812 boot_pool
) != BE_SUCCESS
) {
813 be_print_err(gettext("be_append_menu: "
814 "Failed to remove existing unusable "
815 "entry '%s' in boot menu.\n"), be_name
);
816 ret
= BE_ERR_BE_EXISTS
;
822 /* Append BE entry to the end of the file */
823 menu_fp
= fopen(menu_file
, "a+");
825 if (menu_fp
== NULL
) {
826 be_print_err(gettext("be_append_menu: failed "
827 "to open menu.lst file %s\n"), menu_file
);
828 ret
= errno_to_be_err(err
);
834 * write out all the stored lines
836 for (i
= 0; i
< num_lines
; i
++) {
837 (void) fprintf(menu_fp
, "%s", entries
[i
]);
843 * Check to see if this system supports grub
846 (void) fprintf(menu_fp
, "%s\n", BE_GRUB_COMMENT
);
849 (void) fprintf(menu_fp
, "title %s\n",
850 description
? description
: be_name
);
851 (void) fprintf(menu_fp
, "bootfs %s\n", be_root_ds
);
854 * Check to see if this system supports grub
857 (void) fprintf(menu_fp
, "kernel$ "
858 "/platform/i86pc/kernel/$ISADIR/unix -B "
860 (void) fprintf(menu_fp
, "module$ "
861 "/platform/i86pc/$ISADIR/boot_archive\n");
862 (void) fprintf(menu_fp
, "%s\n", BE_GRUB_COMMENT
);
866 (void) fclose(menu_fp
);
869 int err
= BE_SUCCESS
;
870 err
= be_unmount_pool(zhp
, ptmp_mntpnt
, orig_mntpnt
);
871 if (ret
== BE_SUCCESS
)
877 if (num_tmp_lines
> 0) {
878 for (i
= 0; i
< num_tmp_lines
; i
++) {
879 free(tmp_entries
[i
]);
880 tmp_entries
[i
] = NULL
;
884 for (i
= 0; i
< num_lines
; i
++) {
893 * Function: be_remove_menu
894 * Description: Removes a BE's entry from a menu.lst file.
896 * be_name - the name of BE whose entry is to be removed from
898 * be_root_pool - the pool that be_name lives in.
899 * boot_pool - the pool where the BE is, if different than
900 * the pool containing the boot menu. If this is
901 * NULL it will be set to be_root_pool.
903 * BE_SUCCESS - Success
904 * be_errno_t - Failure
906 * Semi-private (library wide use only)
909 be_remove_menu(char *be_name
, char *be_root_pool
, char *boot_pool
)
911 zfs_handle_t
*zhp
= NULL
;
912 char be_root_ds
[MAXPATHLEN
];
913 char **buffer
= NULL
;
914 char menu_buf
[BUFSIZ
];
915 char menu
[MAXPATHLEN
];
916 char *pool_mntpnt
= NULL
;
917 char *ptmp_mntpnt
= NULL
;
918 char *orig_mntpnt
= NULL
;
919 char *tmp_menu
= NULL
;
920 FILE *menu_fp
= NULL
;
921 FILE *tmp_menu_fp
= NULL
;
923 int ret
= BE_SUCCESS
;
928 int default_entry
= 0;
931 int num_entry_del
= 0;
932 int tmp_menu_len
= 0;
933 boolean_t write
= B_TRUE
;
934 boolean_t do_buffer
= B_FALSE
;
935 boolean_t pool_mounted
= B_FALSE
;
937 if (boot_pool
== NULL
)
938 boot_pool
= be_root_pool
;
940 /* Get name of BE's root dataset */
941 be_make_root_ds(be_root_pool
, be_name
, be_root_ds
, sizeof (be_root_ds
));
943 /* Get handle to pool dataset */
944 if ((zhp
= zfs_open(g_zfs
, be_root_pool
, ZFS_TYPE_DATASET
)) == NULL
) {
945 be_print_err(gettext("be_remove_menu: "
946 "failed to open pool dataset for %s: %s"),
947 be_root_pool
, libzfs_error_description(g_zfs
));
948 return (zfs_err_to_be_err(g_zfs
));
952 * Check to see if the pool's dataset is mounted. If it isn't we'll
953 * attempt to mount it.
955 if ((ret
= be_mount_pool(zhp
, &ptmp_mntpnt
, &orig_mntpnt
,
956 &pool_mounted
)) != BE_SUCCESS
) {
957 be_print_err(gettext("be_remove_menu: pool dataset "
958 "(%s) could not be mounted\n"), be_root_pool
);
964 * Get the mountpoint for the root pool dataset.
966 if (!zfs_is_mounted(zhp
, &pool_mntpnt
)) {
967 be_print_err(gettext("be_remove_menu: pool "
968 "dataset (%s) is not mounted. Can't set "
969 "the default BE in the grub menu.\n"), be_root_pool
);
970 ret
= BE_ERR_NO_MENU
;
974 /* Get path to boot menu */
975 (void) strlcpy(menu
, pool_mntpnt
, sizeof (menu
));
978 * Check to see if this system supports grub
981 (void) strlcat(menu
, BE_GRUB_MENU
, sizeof (menu
));
983 (void) strlcat(menu
, BE_SPARC_MENU
, sizeof (menu
));
985 /* Get handle to boot menu file */
986 if ((ret
= be_open_menu(be_root_pool
, menu
, &menu_fp
, "r",
987 B_TRUE
)) != BE_SUCCESS
) {
989 } else if (menu_fp
== NULL
) {
990 ret
= BE_ERR_NO_MENU
;
997 /* Grab the stats of the original menu file */
998 if (stat(menu
, &sb
) != 0) {
1000 be_print_err(gettext("be_remove_menu: "
1001 "failed to stat file %s: %s\n"), menu
, strerror(err
));
1002 ret
= errno_to_be_err(err
);
1006 /* Create a tmp file for the modified menu.lst */
1007 tmp_menu_len
= strlen(menu
) + 7;
1008 if ((tmp_menu
= (char *)malloc(tmp_menu_len
)) == NULL
) {
1009 be_print_err(gettext("be_remove_menu: malloc failed\n"));
1013 (void) memset(tmp_menu
, 0, tmp_menu_len
);
1014 (void) strlcpy(tmp_menu
, menu
, tmp_menu_len
);
1015 (void) strlcat(tmp_menu
, "XXXXXX", tmp_menu_len
);
1016 if ((fd
= mkstemp(tmp_menu
)) == -1) {
1018 be_print_err(gettext("be_remove_menu: mkstemp failed\n"));
1019 ret
= errno_to_be_err(err
);
1024 if ((tmp_menu_fp
= fdopen(fd
, "w")) == NULL
) {
1026 be_print_err(gettext("be_remove_menu: "
1027 "could not open tmp file for write: %s\n"), strerror(err
));
1029 ret
= errno_to_be_err(err
);
1033 while (fgets(menu_buf
, BUFSIZ
, menu_fp
)) {
1034 char tline
[BUFSIZ
];
1037 (void) strlcpy(tline
, menu_buf
, sizeof (tline
));
1040 tok
= strtok(tline
, BE_WHITE_SPACE
);
1042 if (tok
== NULL
|| tok
[0] == '#') {
1043 /* Found empty line or comment line */
1045 /* Buffer this line */
1046 if ((buffer
= (char **)realloc(buffer
,
1047 sizeof (char *)*(nlines
+ 1))) == NULL
) {
1051 if ((buffer
[nlines
++] = strdup(menu_buf
))
1057 } else if (write
|| strncmp(menu_buf
, BE_GRUB_COMMENT
,
1058 strlen(BE_GRUB_COMMENT
)) != 0) {
1059 /* Write this line out */
1060 (void) fputs(menu_buf
, tmp_menu_fp
);
1062 } else if (strcmp(tok
, "default") == 0) {
1064 * Record what 'default' is set to because we might
1065 * need to adjust this upon deleting an entry.
1067 tok
= strtok(NULL
, BE_WHITE_SPACE
);
1070 default_entry
= atoi(tok
);
1073 (void) fputs(menu_buf
, tmp_menu_fp
);
1074 } else if (strcmp(tok
, "title") == 0) {
1076 * If we've reached a 'title' line and do_buffer is
1077 * is true, that means we've just buffered an entire
1078 * entry without finding a 'bootfs' directive. We
1079 * need to write that entry out and keep searching.
1082 for (i
= 0; i
< nlines
; i
++) {
1083 (void) fputs(buffer
[i
], tmp_menu_fp
);
1092 * Turn writing off and buffering on, and increment
1093 * our entry counter.
1099 /* Buffer this 'title' line */
1100 if ((buffer
= (char **)realloc(buffer
,
1101 sizeof (char *)*(nlines
+ 1))) == NULL
) {
1105 if ((buffer
[nlines
++] = strdup(menu_buf
)) == NULL
) {
1110 } else if (strcmp(tok
, "bootfs") == 0) {
1111 char *bootfs
= NULL
;
1114 * Found a 'bootfs' line. See if it matches the
1115 * BE we're looking for.
1117 if ((bootfs
= strtok(NULL
, BE_WHITE_SPACE
)) == NULL
||
1118 strcmp(bootfs
, be_root_ds
) != 0) {
1120 * Either there's nothing after the 'bootfs'
1121 * or this is not the BE we're looking for,
1122 * write out the line(s) we've buffered since
1123 * finding the title.
1125 for (i
= 0; i
< nlines
; i
++) {
1126 (void) fputs(buffer
[i
], tmp_menu_fp
);
1134 * Turn writing back on, and turn off buffering
1135 * since this isn't the entry we're looking
1139 do_buffer
= B_FALSE
;
1141 /* Write this 'bootfs' line out. */
1142 (void) fputs(menu_buf
, tmp_menu_fp
);
1145 * Found the entry we're looking for.
1146 * Record its entry number, increment the
1147 * number of entries we've deleted, and turn
1148 * writing off. Also, throw away the lines
1149 * we've buffered for this entry so far, we
1152 entry_del
= entry_cnt
- 1;
1155 do_buffer
= B_FALSE
;
1157 for (i
= 0; i
< nlines
; i
++) {
1166 /* Buffer this line */
1167 if ((buffer
= (char **)realloc(buffer
,
1168 sizeof (char *)*(nlines
+ 1))) == NULL
) {
1172 if ((buffer
[nlines
++] = strdup(menu_buf
))
1178 /* Write this line out */
1179 (void) fputs(menu_buf
, tmp_menu_fp
);
1184 (void) fclose(menu_fp
);
1186 (void) fclose(tmp_menu_fp
);
1189 /* Copy the modified menu.lst into place */
1190 if (rename(tmp_menu
, menu
) != 0) {
1192 be_print_err(gettext("be_remove_menu: "
1193 "failed to rename file %s to %s: %s\n"),
1194 tmp_menu
, menu
, strerror(err
));
1195 ret
= errno_to_be_err(err
);
1202 * If we've removed an entry, see if we need to
1203 * adjust the default value in the menu.lst. If the
1204 * entry we've deleted comes before the default entry
1205 * we need to adjust the default value accordingly.
1207 * be_has_grub is used here to check to see if this system
1210 if (be_has_grub() && num_entry_del
> 0) {
1211 if (entry_del
<= default_entry
) {
1212 default_entry
= default_entry
- num_entry_del
;
1213 if (default_entry
< 0)
1217 * Adjust the default value by rewriting the
1218 * menu.lst file. This may be overkill, but to
1219 * preserve the location of the 'default' entry
1220 * in the file, we need to do this.
1223 /* Get handle to boot menu file */
1224 if ((menu_fp
= fopen(menu
, "r")) == NULL
) {
1226 be_print_err(gettext("be_remove_menu: "
1227 "failed to open menu.lst (%s): %s\n"),
1228 menu
, strerror(err
));
1229 ret
= errno_to_be_err(err
);
1233 /* Create a tmp file for the modified menu.lst */
1234 tmp_menu_len
= strlen(menu
) + 7;
1235 if ((tmp_menu
= (char *)malloc(tmp_menu_len
))
1237 be_print_err(gettext("be_remove_menu: "
1238 "malloc failed\n"));
1242 (void) memset(tmp_menu
, 0, tmp_menu_len
);
1243 (void) strlcpy(tmp_menu
, menu
, tmp_menu_len
);
1244 (void) strlcat(tmp_menu
, "XXXXXX", tmp_menu_len
);
1245 if ((fd
= mkstemp(tmp_menu
)) == -1) {
1247 be_print_err(gettext("be_remove_menu: "
1248 "mkstemp failed: %s\n"), strerror(err
));
1249 ret
= errno_to_be_err(err
);
1254 if ((tmp_menu_fp
= fdopen(fd
, "w")) == NULL
) {
1256 be_print_err(gettext("be_remove_menu: "
1257 "could not open tmp file for write: %s\n"),
1260 ret
= errno_to_be_err(err
);
1264 while (fgets(menu_buf
, BUFSIZ
, menu_fp
)) {
1265 char tline
[BUFSIZ
];
1268 (void) strlcpy(tline
, menu_buf
, sizeof (tline
));
1271 tok
= strtok(tline
, BE_WHITE_SPACE
);
1274 /* Found empty line, write it out */
1275 (void) fputs(menu_buf
, tmp_menu_fp
);
1276 } else if (strcmp(tok
, "default") == 0) {
1277 /* Found the default line, adjust it */
1278 (void) snprintf(tline
, sizeof (tline
),
1279 "default %d\n", default_entry
);
1281 (void) fputs(tline
, tmp_menu_fp
);
1283 /* Pass through all other lines */
1284 (void) fputs(menu_buf
, tmp_menu_fp
);
1288 (void) fclose(menu_fp
);
1290 (void) fclose(tmp_menu_fp
);
1293 /* Copy the modified menu.lst into place */
1294 if (rename(tmp_menu
, menu
) != 0) {
1296 be_print_err(gettext("be_remove_menu: "
1297 "failed to rename file %s to %s: %s\n"),
1298 tmp_menu
, menu
, strerror(err
));
1299 ret
= errno_to_be_err(err
);
1308 /* Set the perms and ownership of the updated file */
1309 if (chmod(menu
, sb
.st_mode
) != 0) {
1311 be_print_err(gettext("be_remove_menu: "
1312 "failed to chmod %s: %s\n"), menu
, strerror(err
));
1313 ret
= errno_to_be_err(err
);
1316 if (chown(menu
, sb
.st_uid
, sb
.st_gid
) != 0) {
1318 be_print_err(gettext("be_remove_menu: "
1319 "failed to chown %s: %s\n"), menu
, strerror(err
));
1320 ret
= errno_to_be_err(err
);
1326 int err
= BE_SUCCESS
;
1327 err
= be_unmount_pool(zhp
, ptmp_mntpnt
, orig_mntpnt
);
1328 if (ret
== BE_SUCCESS
)
1336 if (menu_fp
!= NULL
)
1337 (void) fclose(menu_fp
);
1338 if (tmp_menu_fp
!= NULL
)
1339 (void) fclose(tmp_menu_fp
);
1340 if (tmp_menu
!= NULL
) {
1341 (void) unlink(tmp_menu
);
1349 * Function: be_default_grub_bootfs
1350 * Description: This function returns the dataset in the default entry of
1351 * the grub menu. If no default entry is found with a valid bootfs
1352 * entry NULL is returned.
1354 * be_root_pool - This is the name of the root pool where the
1355 * grub menu can be found.
1356 * def_bootfs - This is used to pass back the bootfs string. On
1357 * error NULL is returned here.
1359 * Success - BE_SUCCESS is returned.
1360 * Failure - a be_errno_t is returned.
1362 * Semi-private (library wide use only)
1365 be_default_grub_bootfs(const char *be_root_pool
, char **def_bootfs
)
1367 zfs_handle_t
*zhp
= NULL
;
1368 char grub_file
[MAXPATHLEN
];
1371 char *pool_mntpnt
= NULL
;
1372 char *ptmp_mntpnt
= NULL
;
1373 char *orig_mntpnt
= NULL
;
1374 int default_entry
= 0, entries
= 0;
1375 int found_default
= 0;
1376 int ret
= BE_SUCCESS
;
1377 boolean_t pool_mounted
= B_FALSE
;
1382 * Check to see if this system supports grub
1384 if (!be_has_grub()) {
1385 be_print_err(gettext("be_default_grub_bootfs: operation "
1386 "not supported on this architecture\n"));
1387 return (BE_ERR_NOTSUP
);
1392 /* Get handle to pool dataset */
1393 if ((zhp
= zfs_open(g_zfs
, be_root_pool
, ZFS_TYPE_DATASET
)) == NULL
) {
1394 be_print_err(gettext("be_default_grub_bootfs: "
1395 "failed to open pool dataset for %s: %s"),
1396 be_root_pool
, libzfs_error_description(g_zfs
));
1397 return (zfs_err_to_be_err(g_zfs
));
1401 * Check to see if the pool's dataset is mounted. If it isn't we'll
1402 * attempt to mount it.
1404 if ((ret
= be_mount_pool(zhp
, &ptmp_mntpnt
, &orig_mntpnt
,
1405 &pool_mounted
)) != BE_SUCCESS
) {
1406 be_print_err(gettext("be_default_grub_bootfs: pool dataset "
1407 "(%s) could not be mounted\n"), be_root_pool
);
1413 * Get the mountpoint for the root pool dataset.
1415 if (!zfs_is_mounted(zhp
, &pool_mntpnt
)) {
1416 be_print_err(gettext("be_default_grub_bootfs: failed "
1417 "to get mount point for the root pool. Can't set "
1418 "the default BE in the grub menu.\n"));
1419 ret
= BE_ERR_NO_MENU
;
1423 (void) snprintf(grub_file
, MAXPATHLEN
, "%s%s",
1424 pool_mntpnt
, BE_GRUB_MENU
);
1426 if ((ret
= be_open_menu((char *)be_root_pool
, grub_file
,
1427 &menu_fp
, "r", B_FALSE
)) != BE_SUCCESS
) {
1429 } else if (menu_fp
== NULL
) {
1430 ret
= BE_ERR_NO_MENU
;
1437 while (fgets(line
, BUFSIZ
, menu_fp
)) {
1438 char *tok
= strtok(line
, BE_WHITE_SPACE
);
1440 if (tok
!= NULL
&& tok
[0] != '#') {
1441 if (!found_default
) {
1442 if (strcmp(tok
, "default") == 0) {
1443 tok
= strtok(NULL
, BE_WHITE_SPACE
);
1445 default_entry
= atoi(tok
);
1452 if (strcmp(tok
, "title") == 0) {
1454 } else if (default_entry
== entries
- 1) {
1455 if (strcmp(tok
, "bootfs") == 0) {
1456 tok
= strtok(NULL
, BE_WHITE_SPACE
);
1457 (void) fclose(menu_fp
);
1464 if ((*def_bootfs
= strdup(tok
)) !=
1469 be_print_err(gettext(
1470 "be_default_grub_bootfs: "
1471 "memory allocation failed\n"));
1475 } else if (default_entry
< entries
- 1) {
1477 * no bootfs entry for the default entry.
1483 (void) fclose(menu_fp
);
1487 int err
= BE_SUCCESS
;
1488 err
= be_unmount_pool(zhp
, ptmp_mntpnt
, orig_mntpnt
);
1489 if (ret
== BE_SUCCESS
)
1499 * Function: be_change_grub_default
1500 * Description: This function takes two parameters. These are the name of
1501 * the BE we want to have as the default booted in the grub
1502 * menu and the root pool where the path to the grub menu exists.
1503 * The code takes this and finds the BE's entry in the grub menu
1504 * and changes the default entry to point to that entry in the
1507 * be_name - This is the name of the BE wanted as the default
1508 * for the next boot.
1509 * be_root_pool - This is the name of the root pool where the
1510 * grub menu can be found.
1512 * BE_SUCCESS - Success
1513 * be_errno_t - Failure
1515 * Semi-private (library wide use only)
1518 be_change_grub_default(char *be_name
, char *be_root_pool
)
1520 zfs_handle_t
*zhp
= NULL
;
1521 char grub_file
[MAXPATHLEN
];
1522 char *temp_grub
= NULL
;
1523 char *pool_mntpnt
= NULL
;
1524 char *ptmp_mntpnt
= NULL
;
1525 char *orig_mntpnt
= NULL
;
1527 char temp_line
[BUFSIZ
];
1528 char be_root_ds
[MAXPATHLEN
];
1529 FILE *grub_fp
= NULL
;
1530 FILE *temp_fp
= NULL
;
1532 int temp_grub_len
= 0;
1533 int fd
, entries
= 0;
1535 int ret
= BE_SUCCESS
;
1536 boolean_t found_default
= B_FALSE
;
1537 boolean_t pool_mounted
= B_FALSE
;
1542 * Check to see if this system supports grub
1544 if (!be_has_grub()) {
1545 be_print_err(gettext("be_change_grub_default: operation "
1546 "not supported on this architecture\n"));
1547 return (BE_ERR_NOTSUP
);
1550 /* Generate string for BE's root dataset */
1551 be_make_root_ds(be_root_pool
, be_name
, be_root_ds
, sizeof (be_root_ds
));
1553 /* Get handle to pool dataset */
1554 if ((zhp
= zfs_open(g_zfs
, be_root_pool
, ZFS_TYPE_DATASET
)) == NULL
) {
1555 be_print_err(gettext("be_change_grub_default: "
1556 "failed to open pool dataset for %s: %s"),
1557 be_root_pool
, libzfs_error_description(g_zfs
));
1558 return (zfs_err_to_be_err(g_zfs
));
1562 * Check to see if the pool's dataset is mounted. If it isn't we'll
1563 * attempt to mount it.
1565 if ((ret
= be_mount_pool(zhp
, &ptmp_mntpnt
, &orig_mntpnt
,
1566 &pool_mounted
)) != BE_SUCCESS
) {
1567 be_print_err(gettext("be_change_grub_default: pool dataset "
1568 "(%s) could not be mounted\n"), be_root_pool
);
1574 * Get the mountpoint for the root pool dataset.
1576 if (!zfs_is_mounted(zhp
, &pool_mntpnt
)) {
1577 be_print_err(gettext("be_change_grub_default: pool "
1578 "dataset (%s) is not mounted. Can't set "
1579 "the default BE in the grub menu.\n"), be_root_pool
);
1580 ret
= BE_ERR_NO_MENU
;
1584 (void) snprintf(grub_file
, MAXPATHLEN
, "%s%s",
1585 pool_mntpnt
, BE_GRUB_MENU
);
1587 if ((ret
= be_open_menu(be_root_pool
, grub_file
,
1588 &grub_fp
, "r+", B_TRUE
)) != BE_SUCCESS
) {
1590 } else if (grub_fp
== NULL
) {
1591 ret
= BE_ERR_NO_MENU
;
1598 /* Grab the stats of the original menu file */
1599 if (stat(grub_file
, &sb
) != 0) {
1601 be_print_err(gettext("be_change_grub_default: "
1602 "failed to stat file %s: %s\n"), grub_file
, strerror(err
));
1603 ret
= errno_to_be_err(err
);
1607 /* Create a tmp file for the modified menu.lst */
1608 temp_grub_len
= strlen(grub_file
) + 7;
1609 if ((temp_grub
= (char *)malloc(temp_grub_len
)) == NULL
) {
1610 be_print_err(gettext("be_change_grub_default: "
1611 "malloc failed\n"));
1615 (void) memset(temp_grub
, 0, temp_grub_len
);
1616 (void) strlcpy(temp_grub
, grub_file
, temp_grub_len
);
1617 (void) strlcat(temp_grub
, "XXXXXX", temp_grub_len
);
1618 if ((fd
= mkstemp(temp_grub
)) == -1) {
1620 be_print_err(gettext("be_change_grub_default: "
1621 "mkstemp failed: %s\n"), strerror(err
));
1622 ret
= errno_to_be_err(err
);
1627 if ((temp_fp
= fdopen(fd
, "w")) == NULL
) {
1629 be_print_err(gettext("be_change_grub_default: "
1630 "failed to open %s file: %s\n"),
1631 temp_grub
, strerror(err
));
1633 ret
= errno_to_be_err(err
);
1637 while (fgets(line
, BUFSIZ
, grub_fp
)) {
1638 char *tok
= strtok(line
, BE_WHITE_SPACE
);
1640 if (tok
== NULL
|| tok
[0] == '#') {
1642 } else if (strcmp(tok
, "title") == 0) {
1645 } else if (strcmp(tok
, "bootfs") == 0) {
1646 char *bootfs
= strtok(NULL
, BE_WHITE_SPACE
);
1650 if (strcmp(bootfs
, be_root_ds
) == 0) {
1651 found_default
= B_TRUE
;
1657 if (!found_default
) {
1658 be_print_err(gettext("be_change_grub_default: failed "
1659 "to find entry for %s in the grub menu\n"),
1661 ret
= BE_ERR_BE_NOENT
;
1667 while (fgets(line
, BUFSIZ
, grub_fp
)) {
1670 (void) strncpy(temp_line
, line
, BUFSIZ
);
1672 if ((tok
= strtok(temp_line
, BE_WHITE_SPACE
)) != NULL
&&
1673 strcmp(tok
, "default") == 0) {
1674 (void) snprintf(temp_line
, BUFSIZ
, "default %d\n",
1675 entries
- 1 >= 0 ? entries
- 1 : 0);
1676 (void) fputs(temp_line
, temp_fp
);
1678 (void) fputs(line
, temp_fp
);
1682 (void) fclose(grub_fp
);
1684 (void) fclose(temp_fp
);
1687 if (rename(temp_grub
, grub_file
) != 0) {
1689 be_print_err(gettext("be_change_grub_default: "
1690 "failed to rename file %s to %s: %s\n"),
1691 temp_grub
, grub_file
, strerror(err
));
1692 ret
= errno_to_be_err(err
);
1698 /* Set the perms and ownership of the updated file */
1699 if (chmod(grub_file
, sb
.st_mode
) != 0) {
1701 be_print_err(gettext("be_change_grub_default: "
1702 "failed to chmod %s: %s\n"), grub_file
, strerror(err
));
1703 ret
= errno_to_be_err(err
);
1706 if (chown(grub_file
, sb
.st_uid
, sb
.st_gid
) != 0) {
1708 be_print_err(gettext("be_change_grub_default: "
1709 "failed to chown %s: %s\n"), grub_file
, strerror(err
));
1710 ret
= errno_to_be_err(err
);
1715 int err
= BE_SUCCESS
;
1716 err
= be_unmount_pool(zhp
, ptmp_mntpnt
, orig_mntpnt
);
1717 if (ret
== BE_SUCCESS
)
1723 if (grub_fp
!= NULL
)
1724 (void) fclose(grub_fp
);
1725 if (temp_fp
!= NULL
)
1726 (void) fclose(temp_fp
);
1727 if (temp_grub
!= NULL
) {
1728 (void) unlink(temp_grub
);
1736 * Function: be_update_menu
1737 * Description: This function is used by be_rename to change the BE name in
1738 * an existing entry in the grub menu to the new name of the BE.
1740 * be_orig_name - the original name of the BE
1741 * be_new_name - the new name the BE is being renameed to.
1742 * be_root_pool - The pool which contains the grub menu
1743 * boot_pool - the pool where the BE is, if different than
1744 * the pool containing the boot menu. If this is
1745 * NULL it will be set to be_root_pool.
1747 * BE_SUCCESS - Success
1748 * be_errno_t - Failure
1750 * Semi-private (library wide use only)
1753 be_update_menu(char *be_orig_name
, char *be_new_name
, char *be_root_pool
,
1756 zfs_handle_t
*zhp
= NULL
;
1757 char menu_file
[MAXPATHLEN
];
1758 char be_root_ds
[MAXPATHLEN
];
1759 char be_new_root_ds
[MAXPATHLEN
];
1761 char *pool_mntpnt
= NULL
;
1762 char *ptmp_mntpnt
= NULL
;
1763 char *orig_mntpnt
= NULL
;
1764 char *temp_menu
= NULL
;
1765 FILE *menu_fp
= NULL
;
1766 FILE *new_fp
= NULL
;
1768 int temp_menu_len
= 0;
1770 int ret
= BE_SUCCESS
;
1772 boolean_t pool_mounted
= B_FALSE
;
1776 if (boot_pool
== NULL
)
1777 boot_pool
= be_root_pool
;
1779 if ((zhp
= zfs_open(g_zfs
, be_root_pool
, ZFS_TYPE_DATASET
)) == NULL
) {
1780 be_print_err(gettext("be_update_menu: failed to open "
1781 "pool dataset for %s: %s\n"), be_root_pool
,
1782 libzfs_error_description(g_zfs
));
1783 return (zfs_err_to_be_err(g_zfs
));
1787 * Check to see if the pool's dataset is mounted. If it isn't we'll
1788 * attempt to mount it.
1790 if ((ret
= be_mount_pool(zhp
, &ptmp_mntpnt
, &orig_mntpnt
,
1791 &pool_mounted
)) != BE_SUCCESS
) {
1792 be_print_err(gettext("be_update_menu: pool dataset "
1793 "(%s) could not be mounted\n"), be_root_pool
);
1799 * Get the mountpoint for the root pool dataset.
1801 if (!zfs_is_mounted(zhp
, &pool_mntpnt
)) {
1802 be_print_err(gettext("be_update_menu: failed "
1803 "to get mount point for the root pool. Can't set "
1804 "the default BE in the grub menu.\n"));
1805 ret
= BE_ERR_NO_MENU
;
1810 * Check to see if this system supports grub
1812 if (be_has_grub()) {
1813 (void) snprintf(menu_file
, sizeof (menu_file
),
1814 "%s%s", pool_mntpnt
, BE_GRUB_MENU
);
1816 (void) snprintf(menu_file
, sizeof (menu_file
),
1817 "%s%s", pool_mntpnt
, BE_SPARC_MENU
);
1820 be_make_root_ds(be_root_pool
, be_orig_name
, be_root_ds
,
1821 sizeof (be_root_ds
));
1822 be_make_root_ds(be_root_pool
, be_new_name
, be_new_root_ds
,
1823 sizeof (be_new_root_ds
));
1825 if ((ret
= be_open_menu(be_root_pool
, menu_file
,
1826 &menu_fp
, "r", B_TRUE
)) != BE_SUCCESS
) {
1828 } else if (menu_fp
== NULL
) {
1829 ret
= BE_ERR_NO_MENU
;
1836 /* Grab the stat of the original menu file */
1837 if (stat(menu_file
, &sb
) != 0) {
1839 be_print_err(gettext("be_update_menu: "
1840 "failed to stat file %s: %s\n"), menu_file
, strerror(err
));
1841 (void) fclose(menu_fp
);
1842 ret
= errno_to_be_err(err
);
1846 /* Create tmp file for modified menu.lst */
1847 temp_menu_len
= strlen(menu_file
) + 7;
1848 if ((temp_menu
= (char *)malloc(temp_menu_len
))
1850 be_print_err(gettext("be_update_menu: "
1851 "malloc failed\n"));
1852 (void) fclose(menu_fp
);
1856 (void) memset(temp_menu
, 0, temp_menu_len
);
1857 (void) strlcpy(temp_menu
, menu_file
, temp_menu_len
);
1858 (void) strlcat(temp_menu
, "XXXXXX", temp_menu_len
);
1859 if ((tmp_fd
= mkstemp(temp_menu
)) == -1) {
1861 be_print_err(gettext("be_update_menu: "
1862 "mkstemp failed: %s\n"), strerror(err
));
1863 (void) fclose(menu_fp
);
1865 ret
= errno_to_be_err(err
);
1868 if ((new_fp
= fdopen(tmp_fd
, "w")) == NULL
) {
1870 be_print_err(gettext("be_update_menu: "
1871 "fdopen failed: %s\n"), strerror(err
));
1872 (void) close(tmp_fd
);
1873 (void) fclose(menu_fp
);
1875 ret
= errno_to_be_err(err
);
1879 while (fgets(line
, BUFSIZ
, menu_fp
)) {
1881 char new_line
[BUFSIZ
];
1884 (void) strlcpy(tline
, line
, sizeof (tline
));
1887 c
= strtok(tline
, BE_WHITE_SPACE
);
1890 /* Found empty line, write it out. */
1891 (void) fputs(line
, new_fp
);
1892 } else if (c
[0] == '#') {
1893 /* Found a comment line, write it out. */
1894 (void) fputs(line
, new_fp
);
1895 } else if (strcmp(c
, "title") == 0) {
1900 * Found a 'title' line, parse out BE name or
1903 name
= strtok(NULL
, BE_WHITE_SPACE
);
1907 * Nothing after 'title', just push
1910 (void) fputs(line
, new_fp
);
1913 * Grab the remainder of the title which
1914 * could be a multi worded description
1916 desc
= strtok(NULL
, "\n");
1918 if (strcmp(name
, be_orig_name
) == 0) {
1920 * The first token of the title is
1921 * the old BE name, replace it with
1922 * the new one, and write it out
1923 * along with the remainder of
1924 * description if there is one.
1927 (void) snprintf(new_line
,
1932 (void) snprintf(new_line
,
1934 "title %s\n", be_new_name
);
1937 (void) fputs(new_line
, new_fp
);
1939 (void) fputs(line
, new_fp
);
1942 } else if (strcmp(c
, "bootfs") == 0) {
1944 * Found a 'bootfs' line, parse out the BE root
1947 char *root_ds
= strtok(NULL
, BE_WHITE_SPACE
);
1949 if (root_ds
== NULL
) {
1951 * Nothing after 'bootfs', just push
1954 (void) fputs(line
, new_fp
);
1957 * If this bootfs is the one we're renaming,
1958 * write out the new root dataset value
1960 if (strcmp(root_ds
, be_root_ds
) == 0) {
1961 (void) snprintf(new_line
,
1962 sizeof (new_line
), "bootfs %s\n",
1965 (void) fputs(new_line
, new_fp
);
1967 (void) fputs(line
, new_fp
);
1972 * Found some other line we don't care
1973 * about, write it out.
1975 (void) fputs(line
, new_fp
);
1979 (void) fclose(menu_fp
);
1980 (void) fclose(new_fp
);
1981 (void) close(tmp_fd
);
1983 if (rename(temp_menu
, menu_file
) != 0) {
1985 be_print_err(gettext("be_update_menu: "
1986 "failed to rename file %s to %s: %s\n"),
1987 temp_menu
, menu_file
, strerror(err
));
1988 ret
= errno_to_be_err(err
);
1992 /* Set the perms and ownership of the updated file */
1993 if (chmod(menu_file
, sb
.st_mode
) != 0) {
1995 be_print_err(gettext("be_update_menu: "
1996 "failed to chmod %s: %s\n"), menu_file
, strerror(err
));
1997 ret
= errno_to_be_err(err
);
2000 if (chown(menu_file
, sb
.st_uid
, sb
.st_gid
) != 0) {
2002 be_print_err(gettext("be_update_menu: "
2003 "failed to chown %s: %s\n"), menu_file
, strerror(err
));
2004 ret
= errno_to_be_err(err
);
2009 int err
= BE_SUCCESS
;
2010 err
= be_unmount_pool(zhp
, ptmp_mntpnt
, orig_mntpnt
);
2011 if (ret
== BE_SUCCESS
)
2021 * Function: be_has_menu_entry
2022 * Description: Checks to see if the BEs root dataset has an entry in the grub
2025 * be_dataset - The root dataset of the BE
2026 * be_root_pool - The pool which contains the boot menu
2027 * entry - A pointer the the entry number of the BE if found.
2032 * Semi-private (library wide use only)
2035 be_has_menu_entry(char *be_dataset
, char *be_root_pool
, int *entry
)
2037 zfs_handle_t
*zhp
= NULL
;
2038 char menu_file
[MAXPATHLEN
];
2042 char *rpool_mntpnt
= NULL
;
2043 char *ptmp_mntpnt
= NULL
;
2044 char *orig_mntpnt
= NULL
;
2047 boolean_t pool_mounted
= B_FALSE
;
2051 * Check to see if this system supports grub
2053 if ((zhp
= zfs_open(g_zfs
, be_root_pool
, ZFS_TYPE_DATASET
)) == NULL
) {
2054 be_print_err(gettext("be_has_menu_entry: failed to open "
2055 "pool dataset for %s: %s\n"), be_root_pool
,
2056 libzfs_error_description(g_zfs
));
2061 * Check to see if the pool's dataset is mounted. If it isn't we'll
2062 * attempt to mount it.
2064 if (be_mount_pool(zhp
, &ptmp_mntpnt
, &orig_mntpnt
,
2065 &pool_mounted
) != 0) {
2066 be_print_err(gettext("be_has_menu_entry: pool dataset "
2067 "(%s) could not be mounted\n"), be_root_pool
);
2073 * Get the mountpoint for the root pool dataset.
2075 if (!zfs_is_mounted(zhp
, &rpool_mntpnt
)) {
2076 be_print_err(gettext("be_has_menu_entry: pool "
2077 "dataset (%s) is not mounted. Can't set "
2078 "the default BE in the grub menu.\n"), be_root_pool
);
2083 if (be_has_grub()) {
2084 (void) snprintf(menu_file
, MAXPATHLEN
, "/%s%s",
2085 rpool_mntpnt
, BE_GRUB_MENU
);
2087 (void) snprintf(menu_file
, MAXPATHLEN
, "/%s%s",
2088 rpool_mntpnt
, BE_SPARC_MENU
);
2091 if (be_open_menu(be_root_pool
, menu_file
, &menu_fp
, "r",
2095 } else if (menu_fp
== NULL
) {
2101 rpool_mntpnt
= NULL
;
2103 while (fgets(line
, BUFSIZ
, menu_fp
)) {
2104 char *tok
= strtok_r(line
, BE_WHITE_SPACE
, &last
);
2106 if (tok
!= NULL
&& tok
[0] != '#') {
2107 if (strcmp(tok
, "bootfs") == 0) {
2108 tok
= strtok_r(last
, BE_WHITE_SPACE
, &last
);
2109 if (tok
!= NULL
&& strcmp(tok
,
2111 (void) fclose(menu_fp
);
2113 * The entry number needs to be
2114 * decremented here because the title
2115 * will always be the first line for
2116 * an entry. Because of this we'll
2117 * always be off by one entry when we
2120 *entry
= ent_num
- 1;
2124 } else if (strcmp(tok
, "title") == 0)
2131 (void) be_unmount_pool(zhp
, ptmp_mntpnt
, orig_mntpnt
);
2136 (void) fclose(menu_fp
);
2141 * Function: be_update_vfstab
2142 * Description: This function digs into a BE's vfstab and updates all
2143 * entries with file systems listed in be_fs_list_data_t.
2144 * The entry's root container dataset and be_name will be
2145 * updated with the parameters passed in.
2147 * be_name - name of BE to update
2148 * old_rc_loc - dataset under which the root container dataset
2149 * of the old BE resides in.
2150 * new_rc_loc - dataset under which the root container dataset
2151 * of the new BE resides in.
2152 * fld - be_fs_list_data_t pointer providing the list of
2153 * file systems to look for in vfstab.
2154 * mountpoint - directory of where BE is currently mounted.
2155 * If NULL, then BE is not currently mounted.
2157 * BE_SUCCESS - Success
2158 * be_errno_t - Failure
2160 * Semi-private (library wide use only)
2163 be_update_vfstab(char *be_name
, char *old_rc_loc
, char *new_rc_loc
,
2164 be_fs_list_data_t
*fld
, char *mountpoint
)
2166 char *tmp_mountpoint
= NULL
;
2167 char alt_vfstab
[MAXPATHLEN
];
2168 int ret
= BE_SUCCESS
, err
= BE_SUCCESS
;
2170 if (fld
== NULL
|| fld
->fs_list
== NULL
|| fld
->fs_num
== 0)
2171 return (BE_SUCCESS
);
2173 /* If BE not already mounted, mount the BE */
2174 if (mountpoint
== NULL
) {
2175 if ((ret
= _be_mount(be_name
, &tmp_mountpoint
,
2176 BE_MOUNT_FLAG_NO_ZONES
)) != BE_SUCCESS
) {
2177 be_print_err(gettext("be_update_vfstab: "
2178 "failed to mount BE (%s)\n"), be_name
);
2182 tmp_mountpoint
= mountpoint
;
2185 /* Get string for vfstab in the mounted BE. */
2186 (void) snprintf(alt_vfstab
, sizeof (alt_vfstab
), "%s/etc/vfstab",
2189 /* Update the vfstab */
2190 ret
= _update_vfstab(alt_vfstab
, be_name
, old_rc_loc
, new_rc_loc
,
2193 /* Unmount BE if we mounted it */
2194 if (mountpoint
== NULL
) {
2195 if ((err
= _be_unmount(be_name
, 0)) == BE_SUCCESS
) {
2196 /* Remove temporary mountpoint */
2197 (void) rmdir(tmp_mountpoint
);
2199 be_print_err(gettext("be_update_vfstab: "
2200 "failed to unmount BE %s mounted at %s\n"),
2201 be_name
, tmp_mountpoint
);
2202 if (ret
== BE_SUCCESS
)
2206 free(tmp_mountpoint
);
2213 * Function: be_update_zone_vfstab
2214 * Description: This function digs into a zone BE's vfstab and updates all
2215 * entries with file systems listed in be_fs_list_data_t.
2216 * The entry's root container dataset and be_name will be
2217 * updated with the parameters passed in.
2219 * zhp - zfs_handle_t pointer to zone root dataset.
2220 * be_name - name of zone BE to update
2221 * old_rc_loc - dataset under which the root container dataset
2222 * of the old zone BE resides in.
2223 * new_rc_loc - dataset under which the root container dataset
2224 * of the new zone BE resides in.
2225 * fld - be_fs_list_data_t pointer providing the list of
2226 * file systems to look for in vfstab.
2228 * BE_SUCCESS - Success
2229 * be_errno_t - Failure
2231 * Semi-private (library wide use only)
2234 be_update_zone_vfstab(zfs_handle_t
*zhp
, char *be_name
, char *old_rc_loc
,
2235 char *new_rc_loc
, be_fs_list_data_t
*fld
)
2237 be_mount_data_t md
= { 0 };
2238 be_unmount_data_t ud
= { 0 };
2239 char alt_vfstab
[MAXPATHLEN
];
2240 boolean_t mounted_here
= B_FALSE
;
2241 int ret
= BE_SUCCESS
;
2244 * If zone root not already mounted, mount it at a
2245 * temporary location.
2247 if (!zfs_is_mounted(zhp
, &md
.altroot
)) {
2248 /* Generate temporary mountpoint to mount zone root */
2249 if ((ret
= be_make_tmp_mountpoint(&md
.altroot
)) != BE_SUCCESS
) {
2250 be_print_err(gettext("be_update_zone_vfstab: "
2251 "failed to make temporary mountpoint to "
2252 "mount zone root\n"));
2256 if (be_mount_zone_root(zhp
, &md
) != BE_SUCCESS
) {
2257 be_print_err(gettext("be_update_zone_vfstab: "
2258 "failed to mount zone root %s\n"),
2261 return (BE_ERR_MOUNT_ZONEROOT
);
2263 mounted_here
= B_TRUE
;
2266 /* Get string from vfstab in the mounted zone BE */
2267 (void) snprintf(alt_vfstab
, sizeof (alt_vfstab
), "%s/etc/vfstab",
2270 /* Update the vfstab */
2271 ret
= _update_vfstab(alt_vfstab
, be_name
, old_rc_loc
, new_rc_loc
,
2274 /* Unmount zone root if we mounted it */
2278 if (be_unmount_zone_root(zhp
, &ud
) == BE_SUCCESS
) {
2279 /* Remove the temporary mountpoint */
2280 (void) rmdir(md
.altroot
);
2282 be_print_err(gettext("be_update_zone_vfstab: "
2283 "failed to unmount zone root %s from %s\n"),
2284 zfs_get_name(zhp
), md
.altroot
);
2286 ret
= BE_ERR_UMOUNT_ZONEROOT
;
2295 * Function: be_auto_snap_name
2296 * Description: Generate an auto snapshot name constructed based on the
2297 * current date and time. The auto snapshot name is of the form:
2301 * where <date> is in ISO standard format, so the resultant name
2309 * Success - pointer to auto generated snapshot name. The name
2310 * is allocated in heap storage so the caller is
2311 * responsible for free'ing the name.
2314 * Semi-private (library wide use only)
2317 be_auto_snap_name(void)
2319 time_t utc_tm
= NULL
;
2320 struct tm
*gmt_tm
= NULL
;
2321 char gmt_time_str
[64];
2322 char *auto_snap_name
= NULL
;
2324 if (time(&utc_tm
) == -1) {
2325 be_print_err(gettext("be_auto_snap_name: time() failed\n"));
2329 if ((gmt_tm
= gmtime(&utc_tm
)) == NULL
) {
2330 be_print_err(gettext("be_auto_snap_name: gmtime() failed\n"));
2334 (void) strftime(gmt_time_str
, sizeof (gmt_time_str
), "%F-%T", gmt_tm
);
2336 if ((auto_snap_name
= strdup(gmt_time_str
)) == NULL
) {
2337 be_print_err(gettext("be_auto_snap_name: "
2338 "memory allocation failed\n"));
2342 return (auto_snap_name
);
2346 * Function: be_auto_be_name
2347 * Description: Generate an auto BE name constructed based on the BE name
2348 * of the original BE being cloned.
2350 * obe_name - name of the original BE being cloned.
2352 * Success - pointer to auto generated BE name. The name
2353 * is allocated in heap storage so the caller is
2354 * responsible for free'ing the name.
2357 * Semi-private (library wide use only)
2360 be_auto_be_name(char *obe_name
)
2362 return (be_get_auto_name(obe_name
, NULL
, B_FALSE
));
2366 * Function: be_auto_zone_be_name
2367 * Description: Generate an auto BE name for a zone constructed based on
2368 * the BE name of the original zone BE being cloned.
2370 * container_ds - container dataset for the zone.
2371 * zbe_name - name of the original zone BE being cloned.
2373 * Success - pointer to auto generated BE name. The name
2374 * is allocated in heap storage so the caller is
2375 * responsible for free'ing the name.
2378 * Semi-private (library wide use only)
2381 be_auto_zone_be_name(char *container_ds
, char *zbe_name
)
2383 return (be_get_auto_name(zbe_name
, container_ds
, B_TRUE
));
2387 * Function: be_valid_be_name
2388 * Description: Validates a BE name.
2390 * be_name - name of BE to validate
2392 * B_TRUE - be_name is valid
2393 * B_FALSE - be_name is invalid
2395 * Semi-private (library wide use only)
2399 be_valid_be_name(const char *be_name
)
2401 const char *c
= NULL
;
2402 struct be_defaults be_defaults
;
2404 if (be_name
== NULL
)
2407 be_get_defaults(&be_defaults
);
2410 * A BE name must not be a multi-level dataset name. We also check
2411 * that it does not contain the ' ' and '%' characters. The ' ' is
2412 * a valid character for datasets, however we don't allow that in a
2413 * BE name. The '%' is invalid, but zfs_name_valid() allows it for
2414 * internal reasons, so we explicitly check for it here.
2417 while (*c
!= '\0' && *c
!= '/' && *c
!= ' ' && *c
!= '%')
2424 * The BE name must comply with a zfs dataset filesystem. We also
2425 * verify its length to be < BE_NAME_MAX_LEN.
2427 if (!zfs_name_valid(be_name
, ZFS_TYPE_FILESYSTEM
) ||
2428 strlen(be_name
) > BE_NAME_MAX_LEN
)
2431 if (be_defaults
.be_deflt_bename_starts_with
[0] != '\0' &&
2432 strstr(be_name
, be_defaults
.be_deflt_bename_starts_with
) == NULL
) {
2440 * Function: be_valid_auto_snap_name
2441 * Description: This function checks that a snapshot name is a valid auto
2442 * generated snapshot name. A valid auto generated snapshot
2443 * name is of the form:
2447 * An older form of the auto generated snapshot name also
2448 * included the snapshot's BE cleanup policy and a reserved
2449 * field. Those names will also be verified by this function.
2451 * Examples of valid auto snapshot names are:
2453 * 2008-03-31-18:41:30
2454 * 2008-03-31-22:17:24
2455 * <policy>:-:2008:04-05-09:12:55
2456 * <policy>:-:2008:04-06-15:34:12
2459 * name - name of the snapshot to be validated.
2461 * B_TRUE - the name is a valid auto snapshot name.
2462 * B_FALSE - the name is not a valid auto snapshot name.
2464 * Semi-private (library wide use only)
2467 be_valid_auto_snap_name(char *name
)
2471 char *policy
= NULL
;
2472 char *reserved
= NULL
;
2476 /* Validate the snapshot name by converting it into utc time */
2477 if (strptime(name
, "%Y-%m-%d-%T", &gmt_tm
) != NULL
&&
2478 (mktime(&gmt_tm
) != -1)) {
2483 * Validate the snapshot name against the older form of an
2484 * auto generated snapshot name.
2486 policy
= strdup(name
);
2489 * Get the first field from the snapshot name,
2490 * which is the BE policy
2492 c
= strchr(policy
, ':');
2499 /* Validate the policy name */
2500 if (!valid_be_policy(policy
)) {
2505 /* Get the next field, which is the reserved field. */
2506 if (c
[1] == NULL
|| c
[1] == '\0') {
2511 c
= strchr(reserved
, ':');
2518 /* Validate the reserved field */
2519 if (strcmp(reserved
, "-") != 0) {
2524 /* The remaining string should be the date field */
2525 if (c
[1] == NULL
|| c
[1] == '\0') {
2531 /* Validate the date string by converting it into utc time */
2532 if (strptime(date
, "%Y-%m-%d-%T", &gmt_tm
) == NULL
||
2533 (mktime(&gmt_tm
) == -1)) {
2534 be_print_err(gettext("be_valid_auto_snap_name: "
2535 "invalid auto snapshot name\n"));
2545 * Function: be_default_policy
2546 * Description: Temporary hardcoded policy support. This function returns
2547 * the default policy type to be used to create a BE or a BE
2552 * Name of default BE policy.
2554 * Semi-private (library wide use only)
2557 be_default_policy(void)
2559 return (BE_PLCY_STATIC
);
2563 * Function: valid_be_policy
2564 * Description: Temporary hardcoded policy support. This function valids
2565 * whether a policy is a valid known policy or not.
2567 * policy - name of policy to validate.
2569 * B_TRUE - policy is a valid.
2570 * B_FALSE - policy is invalid.
2572 * Semi-private (library wide use only)
2575 valid_be_policy(char *policy
)
2580 if (strcmp(policy
, BE_PLCY_STATIC
) == 0 ||
2581 strcmp(policy
, BE_PLCY_VOLATILE
) == 0) {
2589 * Function: be_print_err
2590 * Description: This function prints out error messages if do_print is
2591 * set to B_TRUE or if the BE_PRINT_ERR environment variable
2594 * prnt_str - the string we wish to print and any arguments
2595 * for the format of that string.
2599 * Semi-private (library wide use only)
2602 be_print_err(char *prnt_str
, ...)
2607 static boolean_t env_checked
= B_FALSE
;
2610 if ((env_buf
= getenv("BE_PRINT_ERR")) != NULL
) {
2611 if (strcasecmp(env_buf
, "true") == 0) {
2615 env_checked
= B_TRUE
;
2619 va_start(ap
, prnt_str
);
2620 /* LINTED variable format specifier */
2621 (void) vsnprintf(buf
, BUFSIZ
, prnt_str
, ap
);
2622 (void) fputs(buf
, stderr
);
2628 * Function: be_find_current_be
2629 * Description: Find the currently "active" BE. Fill in the
2630 * passed in be_transaction_data_t reference with the
2635 * BE_SUCCESS - Success
2636 * be_errnot_t - Failure
2638 * Semi-private (library wide use only)
2640 * The caller is responsible for initializing the libzfs handle
2641 * and freeing the memory used by the active be_name.
2644 be_find_current_be(be_transaction_data_t
*bt
)
2648 if ((zret
= zpool_iter(g_zfs
, be_zpool_find_current_be_callback
,
2650 be_print_err(gettext("be_find_current_be: failed to "
2651 "find current BE name\n"));
2652 return (BE_ERR_BE_NOENT
);
2653 } else if (zret
< 0) {
2654 be_print_err(gettext("be_find_current_be: "
2655 "zpool_iter failed: %s\n"),
2656 libzfs_error_description(g_zfs
));
2657 return (zfs_err_to_be_err(g_zfs
));
2660 return (BE_SUCCESS
);
2664 * Function: be_zpool_find_current_be_callback
2665 * Description: Callback function used to iterate through all existing pools
2666 * to find the BE that is the currently booted BE.
2668 * zlp - zpool_handle_t pointer to the current pool being
2670 * data - be_transaction_data_t pointer.
2671 * Upon successfully finding the current BE, the
2672 * obe_zpool member of this parameter is set to the
2673 * pool it is found in.
2675 * 1 - Found current BE in this pool.
2676 * 0 - Did not find current BE in this pool.
2678 * Semi-private (library wide use only)
2681 be_zpool_find_current_be_callback(zpool_handle_t
*zlp
, void *data
)
2683 be_transaction_data_t
*bt
= data
;
2684 zfs_handle_t
*zhp
= NULL
;
2685 const char *zpool
= zpool_get_name(zlp
);
2686 char be_container_ds
[MAXPATHLEN
];
2690 * Generate string for BE container dataset
2692 if (getzoneid() != GLOBAL_ZONEID
) {
2693 if ((zpath
= be_get_ds_from_dir("/")) != NULL
) {
2694 (void) strlcpy(be_container_ds
, dirname(zpath
),
2695 sizeof (be_container_ds
));
2697 be_print_err(gettext(
2698 "be_zpool_find_current_be_callback: "
2699 "zone root dataset is not mounted\n"));
2703 be_make_container_ds(zpool
, be_container_ds
,
2704 sizeof (be_container_ds
));
2708 * Check if a BE container dataset exists in this pool.
2710 if (!zfs_dataset_exists(g_zfs
, be_container_ds
, ZFS_TYPE_FILESYSTEM
)) {
2716 * Get handle to this zpool's BE container dataset.
2718 if ((zhp
= zfs_open(g_zfs
, be_container_ds
, ZFS_TYPE_FILESYSTEM
)) ==
2720 be_print_err(gettext("be_zpool_find_current_be_callback: "
2721 "failed to open BE container dataset (%s)\n"),
2728 * Iterate through all potential BEs in this zpool
2730 if (zfs_iter_filesystems(zhp
, be_zfs_find_current_be_callback
, bt
)) {
2732 * Found current BE dataset; set obe_zpool
2734 if ((bt
->obe_zpool
= strdup(zpool
)) == NULL
) {
2735 be_print_err(gettext(
2736 "be_zpool_find_current_be_callback: "
2737 "memory allocation failed\n"));
2755 * Function: be_zfs_find_current_be_callback
2756 * Description: Callback function used to iterate through all BEs in a
2757 * pool to find the BE that is the currently booted BE.
2759 * zhp - zfs_handle_t pointer to current filesystem being checked.
2760 * data - be_transaction-data_t pointer
2761 * Upon successfully finding the current BE, the
2762 * obe_name and obe_root_ds members of this parameter
2763 * are set to the BE name and BE's root dataset
2766 * 1 - Found current BE.
2767 * 0 - Did not find current BE.
2769 * Semi-private (library wide use only)
2772 be_zfs_find_current_be_callback(zfs_handle_t
*zhp
, void *data
)
2774 be_transaction_data_t
*bt
= data
;
2778 * Check if dataset is mounted, and if so where.
2780 if (zfs_is_mounted(zhp
, &mp
)) {
2782 * If mounted at root, set obe_root_ds and obe_name
2784 if (mp
!= NULL
&& strcmp(mp
, "/") == 0) {
2787 if ((bt
->obe_root_ds
= strdup(zfs_get_name(zhp
)))
2789 be_print_err(gettext(
2790 "be_zfs_find_current_be_callback: "
2791 "memory allocation failed\n"));
2796 if ((bt
->obe_name
= strdup(basename(bt
->obe_root_ds
)))
2798 be_print_err(gettext(
2799 "be_zfs_find_current_be_callback: "
2800 "memory allocation failed\n"));
2817 * Function: be_check_be_roots_callback
2818 * Description: This function checks whether or not the dataset name passed
2819 * is hierachically located under the BE root container dataset
2822 * zlp - zpool_handle_t pointer to current pool being processed.
2823 * data - name of dataset to check
2825 * 0 - dataset is not in this pool's BE root container dataset
2826 * 1 - dataset is in this pool's BE root container dataset
2828 * Semi-private (library wide use only)
2831 be_check_be_roots_callback(zpool_handle_t
*zlp
, void *data
)
2833 const char *zpool
= zpool_get_name(zlp
);
2835 char be_container_ds
[MAXPATHLEN
];
2837 /* Generate string for this pool's BE root container dataset */
2838 be_make_container_ds(zpool
, be_container_ds
, sizeof (be_container_ds
));
2841 * If dataset lives under the BE root container dataset
2842 * of this pool, return failure.
2844 if (strncmp(be_container_ds
, ds
, strlen(be_container_ds
)) == 0 &&
2845 ds
[strlen(be_container_ds
)] == '/') {
2855 * Function: zfs_err_to_be_err
2856 * Description: This function takes the error stored in the libzfs handle
2857 * and maps it to an be_errno_t. If there are no matching
2858 * be_errno_t's then BE_ERR_ZFS is returned.
2860 * zfsh - The libzfs handle containing the error we're looking up.
2864 * Semi-private (library wide use only)
2867 zfs_err_to_be_err(libzfs_handle_t
*zfsh
)
2869 int err
= libzfs_errno(zfsh
);
2873 return (BE_SUCCESS
);
2875 return (BE_ERR_PERM
);
2877 return (BE_ERR_INTR
);
2879 return (BE_ERR_NOENT
);
2881 return (BE_ERR_NOSPC
);
2882 case EZFS_MOUNTFAILED
:
2883 return (BE_ERR_MOUNT
);
2884 case EZFS_UMOUNTFAILED
:
2885 return (BE_ERR_UMOUNT
);
2887 return (BE_ERR_BE_EXISTS
);
2889 return (BE_ERR_DEV_BUSY
);
2890 case EZFS_POOLREADONLY
:
2891 return (BE_ERR_ROFS
);
2892 case EZFS_NAMETOOLONG
:
2893 return (BE_ERR_NAMETOOLONG
);
2895 return (BE_ERR_NODEV
);
2896 case EZFS_POOL_INVALARG
:
2897 return (BE_ERR_INVAL
);
2899 return (BE_ERR_INVALPROP
);
2901 return (BE_ERR_DSTYPE
);
2902 case EZFS_PROPNONINHERIT
:
2903 return (BE_ERR_NONINHERIT
);
2904 case EZFS_PROPREADONLY
:
2905 return (BE_ERR_READONLYPROP
);
2906 case EZFS_RESILVERING
:
2907 case EZFS_POOLUNAVAIL
:
2908 return (BE_ERR_UNAVAIL
);
2909 case EZFS_DSREADONLY
:
2910 return (BE_ERR_READONLYDS
);
2912 return (BE_ERR_ZFS
);
2917 * Function: errno_to_be_err
2918 * Description: This function takes an errno and maps it to an be_errno_t.
2919 * If there are no matching be_errno_t's then BE_ERR_UNKNOWN is
2922 * err - The errno we're compairing against.
2926 * Semi-private (library wide use only)
2929 errno_to_be_err(int err
)
2933 return (BE_ERR_PERM
);
2935 return (BE_ERR_ACCESS
);
2937 return (BE_ERR_CANCELED
);
2939 return (BE_ERR_INTR
);
2941 return (BE_ERR_NOENT
);
2944 return (BE_ERR_NOSPC
);
2946 return (BE_ERR_BE_EXISTS
);
2948 return (BE_ERR_BUSY
);
2950 return (BE_ERR_ROFS
);
2952 return (BE_ERR_NAMETOOLONG
);
2954 return (BE_ERR_NXIO
);
2956 return (BE_ERR_INVAL
);
2958 return (BE_ERR_FAULT
);
2960 return (BE_ERR_UNKNOWN
);
2965 * Function: be_err_to_str
2966 * Description: This function takes a be_errno_t and maps it to a message.
2967 * If there are no matching be_errno_t's then NULL is returned.
2969 * be_errno_t - The be_errno_t we're mapping.
2971 * string or NULL if the error code is not known.
2973 * Semi-private (library wide use only)
2976 be_err_to_str(int err
)
2980 return (gettext("Permission denied."));
2981 case BE_ERR_ACTIVATE_CURR
:
2982 return (gettext("Activation of current BE failed."));
2983 case BE_ERR_AUTONAME
:
2984 return (gettext("Auto naming failed."));
2985 case BE_ERR_BE_NOENT
:
2986 return (gettext("No such BE."));
2988 return (gettext("Mount busy."));
2989 case BE_ERR_DEV_BUSY
:
2990 return (gettext("Device busy."));
2991 case BE_ERR_CANCELED
:
2992 return (gettext("Operation canceled."));
2994 return (gettext("BE clone failed."));
2996 return (gettext("BE copy failed."));
2997 case BE_ERR_CREATDS
:
2998 return (gettext("Dataset creation failed."));
2999 case BE_ERR_CURR_BE_NOT_FOUND
:
3000 return (gettext("Can't find current BE."));
3001 case BE_ERR_DESTROY
:
3002 return (gettext("Failed to destroy BE or snapshot."));
3003 case BE_ERR_DESTROY_CURR_BE
:
3004 return (gettext("Cannot destroy current BE."));
3006 return (gettext("BE demotion failed."));
3008 return (gettext("Invalid dataset type."));
3009 case BE_ERR_BE_EXISTS
:
3010 return (gettext("BE exists."));
3012 return (gettext("be_zfs_init failed."));
3014 return (gettext("Interupted system call."));
3016 return (gettext("Invalid argument."));
3017 case BE_ERR_INVALPROP
:
3018 return (gettext("Invalid property for dataset."));
3019 case BE_ERR_INVALMOUNTPOINT
:
3020 return (gettext("Unexpected mountpoint."));
3022 return (gettext("Mount failed."));
3023 case BE_ERR_MOUNTED
:
3024 return (gettext("Already mounted."));
3025 case BE_ERR_NAMETOOLONG
:
3026 return (gettext("name > BUFSIZ."));
3028 return (gettext("Doesn't exist."));
3029 case BE_ERR_POOL_NOENT
:
3030 return (gettext("No such pool."));
3032 return (gettext("No such device."));
3033 case BE_ERR_NOTMOUNTED
:
3034 return (gettext("File system not mounted."));
3036 return (gettext("Not enough memory."));
3037 case BE_ERR_NONINHERIT
:
3039 "Property is not inheritable for the BE dataset."));
3041 return (gettext("No such device or address."));
3043 return (gettext("No space on device."));
3045 return (gettext("Operation not supported."));
3047 return (gettext("Open failed."));
3049 return (gettext("Not owner."));
3050 case BE_ERR_UNAVAIL
:
3051 return (gettext("The BE is currently unavailable."));
3052 case BE_ERR_PROMOTE
:
3053 return (gettext("BE promotion failed."));
3055 return (gettext("Read only file system."));
3056 case BE_ERR_READONLYDS
:
3057 return (gettext("Read only dataset."));
3058 case BE_ERR_READONLYPROP
:
3059 return (gettext("Read only property."));
3060 case BE_ERR_RENAME_ACTIVE
:
3061 return (gettext("Renaming the active BE is not supported."));
3062 case BE_ERR_SS_EXISTS
:
3063 return (gettext("Snapshot exists."));
3064 case BE_ERR_SS_NOENT
:
3065 return (gettext("No such snapshot."));
3067 return (gettext("Unmount failed."));
3068 case BE_ERR_UMOUNT_CURR_BE
:
3069 return (gettext("Can't unmount the current BE."));
3070 case BE_ERR_UMOUNT_SHARED
:
3071 return (gettext("Unmount of a shared File System failed."));
3073 return (gettext("Bad address."));
3074 case BE_ERR_UNKNOWN
:
3075 return (gettext("Unknown error."));
3077 return (gettext("ZFS returned an error."));
3078 case BE_ERR_GEN_UUID
:
3079 return (gettext("Failed to generate uuid."));
3080 case BE_ERR_PARSE_UUID
:
3081 return (gettext("Failed to parse uuid."));
3082 case BE_ERR_NO_UUID
:
3083 return (gettext("No uuid"));
3084 case BE_ERR_ZONE_NO_PARENTBE
:
3085 return (gettext("No parent uuid"));
3086 case BE_ERR_ZONE_MULTIPLE_ACTIVE
:
3087 return (gettext("Multiple active zone roots"));
3088 case BE_ERR_ZONE_NO_ACTIVE_ROOT
:
3089 return (gettext("No active zone root"));
3090 case BE_ERR_ZONE_ROOT_NOT_LEGACY
:
3091 return (gettext("Zone root not legacy"));
3092 case BE_ERR_MOUNT_ZONEROOT
:
3093 return (gettext("Failed to mount a zone root."));
3094 case BE_ERR_UMOUNT_ZONEROOT
:
3095 return (gettext("Failed to unmount a zone root."));
3096 case BE_ERR_NO_MOUNTED_ZONE
:
3097 return (gettext("Zone is not mounted"));
3098 case BE_ERR_ZONES_UNMOUNT
:
3099 return (gettext("Unable to unmount a zone BE."));
3100 case BE_ERR_NO_MENU
:
3101 return (gettext("Missing boot menu file."));
3102 case BE_ERR_BAD_MENU_PATH
:
3103 return (gettext("Invalid path for menu.lst file"));
3104 case BE_ERR_ZONE_SS_EXISTS
:
3105 return (gettext("Zone snapshot exists."));
3106 case BE_ERR_BOOTFILE_INST
:
3107 return (gettext("Error installing boot files."));
3109 return (gettext("Error running an external command."));
3116 * Function: be_has_grub
3117 * Description: Boolean function indicating whether the current system
3119 * Return: B_FALSE - the system does not have grub
3120 * B_TRUE - the system does have grub.
3122 * Semi-private (library wide use only)
3127 static struct be_defaults be_defaults
;
3128 static boolean_t be_deflts_set
= B_FALSE
;
3130 /* Cache the defaults, because be_has_grub is used often. */
3131 if (be_deflts_set
== B_FALSE
) {
3132 be_get_defaults(&be_defaults
);
3133 be_deflts_set
= B_TRUE
;
3136 return (be_defaults
.be_deflt_grub
);
3140 * Function: be_is_isa
3141 * Description: Boolean function indicating whether the instruction set
3142 * architecture of the executing system matches the name provided.
3143 * The string must match a system defined architecture (e.g.
3144 * "i386", "sparc") and is case sensitive.
3145 * Parameters: name - string representing the name of instruction set
3146 * architecture being tested
3147 * Returns: B_FALSE - the system instruction set architecture is different
3148 * from the one specified
3149 * B_TRUE - the system instruction set architecture is the same
3150 * as the one specified
3152 * Semi-private (library wide use only)
3155 be_is_isa(char *name
)
3157 return ((strcmp((char *)be_get_default_isa(), name
) == 0));
3161 * Function: be_get_default_isa
3163 * Returns the default instruction set architecture of the
3164 * machine it is executed on. (eg. sparc, i386, ...)
3165 * NOTE: SYS_INST environment variable may override default
3170 * NULL - the architecture returned by sysinfo() was too
3171 * long for local variables
3172 * char * - pointer to a string containing the default
3175 * Semi-private (library wide use only)
3178 be_get_default_isa(void)
3182 static char default_inst
[ARCH_LENGTH
] = "";
3184 if (default_inst
[0] == '\0') {
3185 if ((envp
= getenv("SYS_INST")) != NULL
) {
3186 if ((int)strlen(envp
) >= ARCH_LENGTH
)
3189 (void) strcpy(default_inst
, envp
);
3191 i
= sysinfo(SI_ARCHITECTURE
, default_inst
, ARCH_LENGTH
);
3192 if (i
< 0 || i
> ARCH_LENGTH
)
3196 return (default_inst
);
3200 * Function: be_get_platform
3202 * Returns the platfom name
3206 * NULL - the platform name returned by sysinfo() was too
3207 * long for local variables
3208 * char * - pointer to a string containing the platform name
3210 * Semi-private (library wide use only)
3213 be_get_platform(void)
3216 static char default_inst
[ARCH_LENGTH
] = "";
3218 if (default_inst
[0] == '\0') {
3219 i
= sysinfo(SI_PLATFORM
, default_inst
, ARCH_LENGTH
);
3220 if (i
< 0 || i
> ARCH_LENGTH
)
3223 return (default_inst
);
3227 * Function: be_run_cmd
3229 * Runs a command in a separate subprocess. Splits out stdout from stderr
3230 * and sends each to its own buffer. Buffers must be pre-allocated and
3231 * passed in as arguments. Buffer sizes are also passed in as arguments.
3234 * - Command being run is assumed to not have any stdout or stderr
3236 * - Commands which emit total stderr output of greater than PIPE_BUF
3237 * bytes can hang. For such commands, a different implementation
3238 * which uses poll(2) must be used.
3239 * - stdout_buf can be NULL. In this case, stdout_bufsize is ignored, and
3240 * the stream which would have gone to it is sent to the bit
3242 * - stderr_buf cannot be NULL.
3243 * - Only subprocess errors are appended to the stderr_buf. Errors
3244 * running the command are reported through be_print_err().
3245 * - Data which would overflow its respective buffer is sent to the bit
3249 * command: command to run. Assumed not to have embedded stdout
3250 * or stderr redirection. May have stdin redirection,
3252 * stderr_buf: buffer returning subprocess stderr data. Errors
3253 * reported by this function are reported through
3255 * stderr_bufsize: size of stderr_buf
3256 * stdout_buf: buffer returning subprocess stdout data.
3257 * stdout_bufsize: size of stdout_buf
3259 * BE_SUCCESS - The command ran successfully without returning
3262 * - The command could not be run.
3263 * - The command terminated with error status.
3264 * - There were errors extracting or returning subprocess
3266 * BE_ERR_NOMEM - The command exceeds the command buffer size.
3267 * BE_ERR_INVAL - An invalid argument was specified.
3269 * Semi-private (library wide use only)
3272 be_run_cmd(char *command
, char *stderr_buf
, int stderr_bufsize
,
3273 char *stdout_buf
, int stdout_bufsize
)
3275 char *temp_filename
= strdup(tmpnam(NULL
));
3276 FILE *stdout_str
= NULL
;
3277 FILE *stderr_str
= NULL
;
3278 char cmdline
[BUFSIZ
];
3279 char oneline
[BUFSIZ
];
3281 int rval
= BE_SUCCESS
;
3283 if ((command
== NULL
) || (stderr_buf
== NULL
) ||
3284 (stderr_bufsize
<= 0) || (stdout_bufsize
< 0) ||
3285 ((stdout_buf
!= NULL
) ^ (stdout_bufsize
!= 0))) {
3286 return (BE_ERR_INVAL
);
3289 /* Set up command so popen returns stderr, not stdout */
3290 if (snprintf(cmdline
, BUFSIZ
, "%s 2> %s", command
,
3291 temp_filename
) >= BUFSIZ
) {
3292 rval
= BE_ERR_NOMEM
;
3296 /* Set up the fifo that will make stderr available. */
3297 if (mkfifo(temp_filename
, 0600) != 0) {
3298 (void) be_print_err(gettext("be_run_cmd: mkfifo: %s\n"),
3300 rval
= BE_ERR_EXTCMD
;
3304 if ((stdout_str
= popen(cmdline
, "r")) == NULL
) {
3305 (void) be_print_err(gettext("be_run_cmd: popen: %s\n"),
3307 rval
= BE_ERR_EXTCMD
;
3311 if ((stderr_str
= fopen(temp_filename
, "r")) == NULL
) {
3312 (void) be_print_err(gettext("be_run_cmd: fopen: %s\n"),
3314 (void) pclose(stdout_str
);
3315 rval
= BE_ERR_EXTCMD
;
3319 /* Read stdout first, as it usually outputs more than stderr. */
3320 oneline
[BUFSIZ
-1] = '\0';
3321 while (fgets(oneline
, BUFSIZ
-1, stdout_str
) != NULL
) {
3322 if (stdout_str
!= NULL
) {
3323 (void) strlcat(stdout_buf
, oneline
, stdout_bufsize
);
3327 while (fgets(oneline
, BUFSIZ
-1, stderr_str
) != NULL
) {
3328 (void) strlcat(stderr_buf
, oneline
, stderr_bufsize
);
3331 /* Close pipe, get exit status. */
3332 if ((exit_status
= pclose(stdout_str
)) == -1) {
3333 (void) be_print_err(gettext("be_run_cmd: pclose: %s\n"),
3335 rval
= BE_ERR_EXTCMD
;
3336 } else if (WIFEXITED(exit_status
)) {
3337 exit_status
= (int)((char)WEXITSTATUS(exit_status
));
3339 * error code BC_NOUPDT means more recent version
3342 if (exit_status
!= BC_SUCCESS
&& exit_status
!= BC_NOUPDT
) {
3343 (void) snprintf(oneline
, BUFSIZ
, gettext("be_run_cmd: "
3344 "command terminated with error status: %d\n"),
3346 (void) strlcat(stderr_buf
, oneline
, stderr_bufsize
);
3347 rval
= BE_ERR_EXTCMD
;
3350 (void) snprintf(oneline
, BUFSIZ
, gettext("be_run_cmd: command "
3351 "terminated on signal: %s\n"),
3352 strsignal(WTERMSIG(exit_status
)));
3353 (void) strlcat(stderr_buf
, oneline
, stderr_bufsize
);
3354 rval
= BE_ERR_EXTCMD
;
3358 (void) unlink(temp_filename
);
3359 (void) free(temp_filename
);
3364 /* ******************************************************************** */
3365 /* Private Functions */
3366 /* ******************************************************************** */
3369 * Function: update_dataset
3370 * Description: This function takes a dataset name and replaces the zpool
3371 * and be_name components of the dataset with the new be_name
3374 * dataset - name of dataset
3375 * dataset_len - lenth of buffer in which dataset is passed in.
3376 * be_name - name of new BE name to update to.
3377 * old_rc_loc - dataset under which the root container dataset
3378 * for the old BE lives.
3379 * new_rc_loc - dataset under which the root container dataset
3380 * for the new BE lives.
3382 * BE_SUCCESS - Success
3383 * be_errno_t - Failure
3388 update_dataset(char *dataset
, int dataset_len
, char *be_name
,
3389 char *old_rc_loc
, char *new_rc_loc
)
3392 char *sub_ds
= NULL
;
3394 /* Tear off the BE container dataset */
3395 if ((ds
= be_make_name_from_ds(dataset
, old_rc_loc
)) == NULL
) {
3396 return (BE_ERR_INVAL
);
3399 /* Get dataset name relative to BE root, if there is one */
3400 sub_ds
= strchr(ds
, '/');
3402 /* Generate the BE root dataset name */
3403 be_make_root_ds(new_rc_loc
, be_name
, dataset
, dataset_len
);
3405 /* If a subordinate dataset name was found, append it */
3407 (void) strlcat(dataset
, sub_ds
, dataset_len
);
3410 return (BE_SUCCESS
);
3414 * Function: _update_vfstab
3415 * Description: This function updates a vfstab file to reflect the new
3416 * root container dataset location and be_name for all
3417 * entries listed in the be_fs_list_data_t structure passed in.
3419 * vfstab - vfstab file to modify
3420 * be_name - name of BE to update.
3421 * old_rc_loc - dataset under which the root container dataset
3422 * of the old BE resides in.
3423 * new_rc_loc - dataset under which the root container dataset
3424 * of the new BE resides in.
3425 * fld - be_fs_list_data_t pointer providing the list of
3426 * file systems to look for in vfstab.
3428 * BE_SUCCESS - Success
3429 * be_errno_t - Failure
3434 _update_vfstab(char *vfstab
, char *be_name
, char *old_rc_loc
,
3435 char *new_rc_loc
, be_fs_list_data_t
*fld
)
3438 char *tmp_vfstab
= NULL
;
3439 char comments_buf
[BUFSIZ
];
3440 FILE *comments
= NULL
;
3441 FILE *vfs_ents
= NULL
;
3444 char dev
[MAXPATHLEN
];
3447 int ret
= BE_SUCCESS
, err
= 0;
3449 int tmp_vfstab_len
= 0;
3454 * Open vfstab for reading twice. First is for comments,
3455 * second is for actual entries.
3457 if ((comments
= fopen(vfstab
, "r")) == NULL
||
3458 (vfs_ents
= fopen(vfstab
, "r")) == NULL
) {
3460 be_print_err(gettext("_update_vfstab: "
3461 "failed to open vfstab (%s): %s\n"), vfstab
,
3463 ret
= errno_to_be_err(err
);
3467 /* Grab the stats of the original vfstab file */
3468 if (stat(vfstab
, &sb
) != 0) {
3470 be_print_err(gettext("_update_vfstab: "
3471 "failed to stat file %s: %s\n"), vfstab
,
3473 ret
= errno_to_be_err(err
);
3477 /* Create tmp file for modified vfstab */
3478 if ((tmp_vfstab
= (char *)malloc(strlen(vfstab
) + 7))
3480 be_print_err(gettext("_update_vfstab: "
3481 "malloc failed\n"));
3485 tmp_vfstab_len
= strlen(vfstab
) + 7;
3486 (void) memset(tmp_vfstab
, 0, tmp_vfstab_len
);
3487 (void) strlcpy(tmp_vfstab
, vfstab
, tmp_vfstab_len
);
3488 (void) strlcat(tmp_vfstab
, "XXXXXX", tmp_vfstab_len
);
3489 if ((fd
= mkstemp(tmp_vfstab
)) == -1) {
3491 be_print_err(gettext("_update_vfstab: "
3492 "mkstemp failed: %s\n"), strerror(err
));
3493 ret
= errno_to_be_err(err
);
3496 if ((tfile
= fdopen(fd
, "w")) == NULL
) {
3498 be_print_err(gettext("_update_vfstab: "
3499 "could not open file for write\n"));
3501 ret
= errno_to_be_err(err
);
3505 while (fgets(comments_buf
, BUFSIZ
, comments
)) {
3506 for (c
= comments_buf
; *c
!= '\0' && isspace(*c
); c
++)
3510 } else if (*c
== '#') {
3512 * If line is a comment line, just put
3513 * it through to the tmp vfstab.
3515 (void) fputs(comments_buf
, tfile
);
3518 * Else line is a vfstab entry, grab it
3519 * into a vfstab struct.
3521 if (getvfsent(vfs_ents
, &vp
) != 0) {
3523 be_print_err(gettext("_update_vfstab: "
3524 "getvfsent failed: %s\n"), strerror(err
));
3525 ret
= errno_to_be_err(err
);
3529 if (vp
.vfs_special
== NULL
|| vp
.vfs_mountp
== NULL
) {
3530 (void) putvfsent(tfile
, &vp
);
3535 * If the entry is one of the entries in the list
3536 * of file systems to update, modify it's device
3537 * field to be correct for this BE.
3539 for (i
= 0; i
< fld
->fs_num
; i
++) {
3540 if (strcmp(vp
.vfs_special
, fld
->fs_list
[i
])
3543 * Found entry that needs an update.
3544 * Replace the root container dataset
3545 * location and be_name in the
3548 (void) strlcpy(dev
, vp
.vfs_special
,
3551 if ((ret
= update_dataset(dev
,
3552 sizeof (dev
), be_name
, old_rc_loc
,
3553 new_rc_loc
)) != 0) {
3555 gettext("_update_vfstab: "
3556 "Failed to update device "
3557 "field for vfstab entry "
3558 "%s\n"), fld
->fs_list
[i
]);
3562 vp
.vfs_special
= dev
;
3567 /* Put entry through to tmp vfstab */
3568 (void) putvfsent(tfile
, &vp
);
3572 (void) fclose(comments
);
3574 (void) fclose(vfs_ents
);
3576 (void) fclose(tfile
);
3579 /* Copy tmp vfstab into place */
3580 if (rename(tmp_vfstab
, vfstab
) != 0) {
3582 be_print_err(gettext("_update_vfstab: "
3583 "failed to rename file %s to %s: %s\n"), tmp_vfstab
,
3584 vfstab
, strerror(err
));
3585 ret
= errno_to_be_err(err
);
3589 /* Set the perms and ownership of the updated file */
3590 if (chmod(vfstab
, sb
.st_mode
) != 0) {
3592 be_print_err(gettext("_update_vfstab: "
3593 "failed to chmod %s: %s\n"), vfstab
, strerror(err
));
3594 ret
= errno_to_be_err(err
);
3597 if (chown(vfstab
, sb
.st_uid
, sb
.st_gid
) != 0) {
3599 be_print_err(gettext("_update_vfstab: "
3600 "failed to chown %s: %s\n"), vfstab
, strerror(err
));
3601 ret
= errno_to_be_err(err
);
3606 if (comments
!= NULL
)
3607 (void) fclose(comments
);
3608 if (vfs_ents
!= NULL
)
3609 (void) fclose(vfs_ents
);
3610 (void) unlink(tmp_vfstab
);
3611 (void) free(tmp_vfstab
);
3613 (void) fclose(tfile
);
3620 * Function: be_get_auto_name
3621 * Description: Generate an auto name constructed based on the BE name
3622 * of the original BE or zone BE being cloned.
3624 * obe_name - name of the original BE or zone BE being cloned.
3625 * container_ds - container dataset for the zone.
3626 * Note: if zone_be is false this should be
3628 * zone_be - flag that indicates if we are operating on a zone BE.
3630 * Success - pointer to auto generated BE name. The name
3631 * is allocated in heap storage so the caller is
3632 * responsible for free'ing the name.
3638 be_get_auto_name(char *obe_name
, char *be_container_ds
, boolean_t zone_be
)
3640 be_node_list_t
*be_nodes
= NULL
;
3641 be_node_list_t
*cur_be
= NULL
;
3642 char auto_be_name
[MAXPATHLEN
];
3643 char base_be_name
[MAXPATHLEN
];
3644 char cur_be_name
[MAXPATHLEN
];
3645 char *num_str
= NULL
;
3653 * Check if obe_name is already in an auto BE name format.
3654 * If it is, then strip off the increment number to get the
3657 (void) strlcpy(base_be_name
, obe_name
, sizeof (base_be_name
));
3659 if ((num_str
= strrchr(base_be_name
, BE_AUTO_NAME_DELIM
))
3661 /* Make sure remaining string is all digits */
3663 while (c
[0] != '\0' && isdigit(c
[0]))
3666 * If we're now at the end of the string strip off the
3674 if (be_container_ds
== NULL
)
3676 if (be_get_zone_be_list(obe_name
, be_container_ds
,
3677 &be_nodes
) != BE_SUCCESS
) {
3678 be_print_err(gettext("be_get_auto_name: "
3679 "be_get_zone_be_list failed\n"));
3682 } else if (_be_list(NULL
, &be_nodes
) != BE_SUCCESS
) {
3683 be_print_err(gettext("be_get_auto_name: be_list failed\n"));
3687 for (cur_be
= be_nodes
; cur_be
!= NULL
; cur_be
= cur_be
->be_next_node
) {
3688 (void) strlcpy(cur_be_name
, cur_be
->be_node_name
,
3689 sizeof (cur_be_name
));
3691 /* If cur_be_name doesn't match at least base be name, skip. */
3692 if (strncmp(cur_be_name
, base_be_name
, strlen(base_be_name
))
3696 /* Get the string following the base be name */
3697 num_str
= cur_be_name
+ strlen(base_be_name
);
3700 * If nothing follows the base be name, this cur_be_name
3701 * is the BE named with the base be name, skip.
3703 if (num_str
== NULL
|| num_str
[0] == '\0')
3707 * Remove the name delimiter. If its not there,
3708 * cur_be_name isn't part of this BE name stream, skip.
3710 if (num_str
[0] == BE_AUTO_NAME_DELIM
)
3715 /* Make sure remaining string is all digits */
3717 while (c
[0] != '\0' && isdigit(c
[0]))
3722 /* Convert the number string to an int */
3723 cur_num
= atoi(num_str
);
3726 * If failed to convert the string, skip it. If its too
3727 * long to be converted to an int, we wouldn't auto generate
3728 * this number anyway so there couldn't be a conflict.
3729 * We treat it as a manually created BE name.
3731 if (cur_num
== 0 && errno
== EINVAL
)
3735 * Compare current number to current max number,
3736 * take higher of the two.
3743 * Store off a copy of 'num' incase we need it later. If incrementing
3744 * 'num' causes it to roll over, this means 'num' is the largest
3745 * positive int possible; we'll need it later in the loop to determine
3746 * if we've exhausted all possible increment numbers. We store it in
3751 /* Increment 'num' to get new auto BE name number */
3756 * Since incrementing 'num' caused it to rollover, start
3757 * over at 0 and find the first available number.
3759 for (num
= 0; num
< cur_num
; num
++) {
3761 (void) snprintf(cur_be_name
, sizeof (cur_be_name
),
3762 "%s%c%d", base_be_name
, BE_AUTO_NAME_DELIM
, num
);
3764 ret
= zpool_iter(g_zfs
, be_exists_callback
,
3769 * BE name doesn't exist, break out
3773 } else if (ret
== 1) {
3774 /* BE name exists, continue looking */
3777 be_print_err(gettext("be_get_auto_name: "
3778 "zpool_iter failed: %s\n"),
3779 libzfs_error_description(g_zfs
));
3780 be_free_list(be_nodes
);
3786 * If 'num' equals 'cur_num', we've exhausted all possible
3787 * auto BE names for this base BE name.
3789 if (num
== cur_num
) {
3790 be_print_err(gettext("be_get_auto_name: "
3791 "No more available auto BE names for base "
3792 "BE name %s\n"), base_be_name
);
3793 be_free_list(be_nodes
);
3798 be_free_list(be_nodes
);
3801 * Generate string for auto BE name.
3803 (void) snprintf(auto_be_name
, sizeof (auto_be_name
), "%s%c%d",
3804 base_be_name
, BE_AUTO_NAME_DELIM
, num
);
3806 if ((c
= strdup(auto_be_name
)) == NULL
) {
3807 be_print_err(gettext("be_get_auto_name: "
3808 "memory allocation failed\n"));
3816 * Function: be_get_console_prop
3817 * Description: Determine console device.
3819 * Success - pointer to console setting.
3825 be_get_console_prop(void)
3828 char *console
= NULL
;
3830 if ((dn
= di_init("/", DINFOPROP
)) == DI_NODE_NIL
) {
3831 be_print_err(gettext("be_get_console_prop: "
3832 "di_init() failed\n"));
3836 if (di_prop_lookup_strings(DDI_DEV_T_ANY
, dn
,
3837 "console", &console
) != -1) {
3842 if (console
== NULL
) {
3843 if (di_prop_lookup_strings(DDI_DEV_T_ANY
, dn
,
3844 "output-device", &console
) != -1) {
3846 if (strncmp(console
, "screen", strlen("screen")) == 0)
3847 console
= BE_DEFAULT_CONSOLE
;
3852 * Default console to text
3854 if (console
== NULL
) {
3855 console
= BE_DEFAULT_CONSOLE
;
3862 * Function: be_create_menu
3864 * This function is used if no menu.lst file exists. In
3865 * this case a new file is created and if needed default
3866 * lines are added to the file.
3868 * pool - The name of the pool the menu.lst file is on
3869 * menu_file - The name of the file we're creating.
3870 * menu_fp - A pointer to the file pointer of the file we
3871 * created. This is also used to pass back the file
3872 * pointer to the newly created file.
3873 * mode - the original mode used for the failed attempt to
3874 * non-existent file.
3876 * BE_SUCCESS - Success
3877 * be_errno_t - Failure
3888 be_node_list_t
*be_nodes
= NULL
;
3889 char *menu_path
= NULL
;
3890 char *be_rpool
= NULL
;
3891 char *be_name
= NULL
;
3892 char *console
= NULL
;
3895 if (menu_file
== NULL
|| menu_fp
== NULL
|| mode
== NULL
)
3896 return (BE_ERR_INVAL
);
3898 menu_path
= strdup(menu_file
);
3899 if (menu_path
== NULL
)
3900 return (BE_ERR_NOMEM
);
3902 (void) dirname(menu_path
);
3903 if (*menu_path
== '.') {
3905 return (BE_ERR_BAD_MENU_PATH
);
3907 if (mkdirp(menu_path
,
3908 S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == -1 &&
3911 be_print_err(gettext("be_create_menu: Failed to create the %s "
3912 "directory: %s\n"), menu_path
, strerror(errno
));
3913 return (errno_to_be_err(errno
));
3918 * Check to see if this system supports grub
3920 if (be_has_grub()) {
3922 * The grub menu is missing so we need to create it
3923 * and fill in the first few lines.
3925 FILE *temp_fp
= fopen(menu_file
, "a+");
3926 if (temp_fp
== NULL
) {
3928 return (errno_to_be_err(errno
));
3931 if ((console
= be_get_console_prop()) != NULL
) {
3934 * If console is redirected to serial line,
3935 * GRUB splash screen will not be enabled.
3937 if (strncmp(console
, "text", strlen("text")) == 0 ||
3938 strncmp(console
, "graphics",
3939 strlen("graphics")) == 0) {
3941 (void) fprintf(temp_fp
, "%s\n", BE_GRUB_SPLASH
);
3942 (void) fprintf(temp_fp
, "%s\n",
3943 BE_GRUB_FOREGROUND
);
3944 (void) fprintf(temp_fp
, "%s\n",
3945 BE_GRUB_BACKGROUND
);
3946 (void) fprintf(temp_fp
, "%s\n",
3949 be_print_err(gettext("be_create_menu: "
3950 "console on serial line, "
3951 "GRUB splash image will be disabled\n"));
3955 (void) fprintf(temp_fp
, "timeout 30\n");
3956 (void) fclose(temp_fp
);
3960 * The menu file doesn't exist so we need to create a
3963 FILE *temp_fp
= fopen(menu_file
, "w+");
3964 if (temp_fp
== NULL
) {
3966 return (errno_to_be_err(errno
));
3968 (void) fclose(temp_fp
);
3972 * Now we need to add all the BE's back into the the file.
3974 if (_be_list(NULL
, &be_nodes
) == BE_SUCCESS
) {
3975 while (be_nodes
!= NULL
) {
3976 if (strcmp(pool
, be_nodes
->be_rpool
) == 0) {
3977 (void) be_append_menu(be_nodes
->be_node_name
,
3978 be_nodes
->be_rpool
, NULL
, NULL
, NULL
);
3980 if (be_nodes
->be_active_on_boot
) {
3981 be_rpool
= strdup(be_nodes
->be_rpool
);
3982 be_name
= strdup(be_nodes
->be_node_name
);
3985 be_nodes
= be_nodes
->be_next_node
;
3988 be_free_list(be_nodes
);
3991 * Check to see if this system supports grub
3993 if (be_has_grub()) {
3994 int err
= be_change_grub_default(be_name
, be_rpool
);
3995 if (err
!= BE_SUCCESS
)
3998 *menu_fp
= fopen(menu_file
, mode
);
3999 if (*menu_fp
== NULL
)
4000 return (errno_to_be_err(errno
));
4002 return (BE_SUCCESS
);
4006 * Function: be_open_menu
4008 * This function is used it open the menu.lst file. If this
4009 * file does not exist be_create_menu is called to create it
4010 * and the open file pointer is returned. If the file does
4011 * exist it is simply opened using the mode passed in.
4013 * pool - The name of the pool the menu.lst file is on
4014 * menu_file - The name of the file we're opening.
4015 * menu_fp - A pointer to the file pointer of the file we're
4016 * opening. This is also used to pass back the file
4018 * mode - the original mode to be used for opening the menu.lst
4020 * create_menu - If this is true and the menu.lst file does not
4021 * exist we will attempt to re-create it. However
4022 * if it's false the error returned from the fopen
4025 * BE_SUCCESS - Success
4026 * be_errno_t - Failure
4036 boolean_t create_menu
)
4039 boolean_t set_print
= B_FALSE
;
4041 *menu_fp
= fopen(menu_file
, mode
);
4043 if (*menu_fp
== NULL
) {
4044 if (err
== ENOENT
&& create_menu
) {
4045 be_print_err(gettext("be_open_menu: menu.lst "
4046 "file %s does not exist,\n"), menu_file
);
4051 be_print_err(gettext("WARNING: menu.lst "
4052 "file %s does not exist,\n generating "
4053 "a new menu.lst file\n"), menu_file
);
4057 if ((err
= be_create_menu(pool
, menu_file
,
4058 menu_fp
, mode
)) == ENOENT
)
4059 return (BE_ERR_NO_MENU
);
4060 else if (err
!= BE_SUCCESS
)
4062 else if (*menu_fp
== NULL
)
4063 return (BE_ERR_NO_MENU
);
4065 be_print_err(gettext("be_open_menu: failed "
4066 "to open menu.lst file %s\n"), menu_file
);
4068 return (BE_ERR_NO_MENU
);
4070 return (errno_to_be_err(err
));
4073 return (BE_SUCCESS
);