xref: /illumos-gate/usr/src/lib/libbe/common/be_utils.c (revision ec8422d0)
1f169c0eaSGlenn Lagasse /*
2f169c0eaSGlenn Lagasse  * CDDL HEADER START
3f169c0eaSGlenn Lagasse  *
4f169c0eaSGlenn Lagasse  * The contents of this file are subject to the terms of the
5f169c0eaSGlenn Lagasse  * Common Development and Distribution License (the "License").
6f169c0eaSGlenn Lagasse  * You may not use this file except in compliance with the License.
7f169c0eaSGlenn Lagasse  *
8f169c0eaSGlenn Lagasse  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9f169c0eaSGlenn Lagasse  * or http://www.opensolaris.org/os/licensing.
10f169c0eaSGlenn Lagasse  * See the License for the specific language governing permissions
11f169c0eaSGlenn Lagasse  * and limitations under the License.
12f169c0eaSGlenn Lagasse  *
13f169c0eaSGlenn Lagasse  * When distributing Covered Code, include this CDDL HEADER in each
14f169c0eaSGlenn Lagasse  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15f169c0eaSGlenn Lagasse  * If applicable, add the following below this CDDL HEADER, with the
16f169c0eaSGlenn Lagasse  * fields enclosed by brackets "[]" replaced with your own identifying
17f169c0eaSGlenn Lagasse  * information: Portions Copyright [yyyy] [name of copyright owner]
18f169c0eaSGlenn Lagasse  *
19f169c0eaSGlenn Lagasse  * CDDL HEADER END
20f169c0eaSGlenn Lagasse  */
21f169c0eaSGlenn Lagasse 
22f169c0eaSGlenn Lagasse /*
23f169c0eaSGlenn Lagasse  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
247e0e2549SAlexander Eremin  * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
25f5e5a2c4SToomas Soome  * Copyright 2016 Toomas Soome <tsoome@me.com>
269adfa60dSMatthew Ahrens  * Copyright (c) 2015 by Delphix. All rights reserved.
27*ec8422d0SAndy Fiddaman  * Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
28ce829a51SJohn Levon  * Copyright (c) 2018, Joyent, Inc.
296b1d07a4SAlexander Eremin  */
306b1d07a4SAlexander Eremin 
316b1d07a4SAlexander Eremin 
32f169c0eaSGlenn Lagasse /*
33f169c0eaSGlenn Lagasse  * System includes
34f169c0eaSGlenn Lagasse  */
35f169c0eaSGlenn Lagasse #include <assert.h>
36f169c0eaSGlenn Lagasse #include <errno.h>
37f169c0eaSGlenn Lagasse #include <libgen.h>
38f169c0eaSGlenn Lagasse #include <libintl.h>
39f169c0eaSGlenn Lagasse #include <libnvpair.h>
40f169c0eaSGlenn Lagasse #include <libzfs.h>
41f169c0eaSGlenn Lagasse #include <libgen.h>
42f169c0eaSGlenn Lagasse #include <stdio.h>
43f169c0eaSGlenn Lagasse #include <stdlib.h>
44f169c0eaSGlenn Lagasse #include <string.h>
45f169c0eaSGlenn Lagasse #include <sys/stat.h>
46f169c0eaSGlenn Lagasse #include <sys/types.h>
47f169c0eaSGlenn Lagasse #include <sys/vfstab.h>
48f169c0eaSGlenn Lagasse #include <sys/param.h>
49f169c0eaSGlenn Lagasse #include <sys/systeminfo.h>
50f169c0eaSGlenn Lagasse #include <ctype.h>
51f169c0eaSGlenn Lagasse #include <time.h>
52f169c0eaSGlenn Lagasse #include <unistd.h>
53f169c0eaSGlenn Lagasse #include <fcntl.h>
54de1ab35cSAlexander Eremin #include <deflt.h>
55f169c0eaSGlenn Lagasse #include <wait.h>
566b1d07a4SAlexander Eremin #include <libdevinfo.h>
577e0e2549SAlexander Eremin #include <libgen.h>
58f169c0eaSGlenn Lagasse 
59f169c0eaSGlenn Lagasse #include <libbe.h>
60f169c0eaSGlenn Lagasse #include <libbe_priv.h>
61a63c99a2SToomas Soome #include <boot_utils.h>
62f5e5a2c4SToomas Soome #include <ficl.h>
63f5e5a2c4SToomas Soome #include <ficlplatform/emu.h>
64f169c0eaSGlenn Lagasse 
65f169c0eaSGlenn Lagasse /* Private function prototypes */
66f169c0eaSGlenn Lagasse static int update_dataset(char *, int, char *, char *, char *);
67f169c0eaSGlenn Lagasse static int _update_vfstab(char *, char *, char *, char *, be_fs_list_data_t *);
68de1ab35cSAlexander Eremin static int be_open_menu(char *, char *, FILE **, char *, boolean_t);
69de1ab35cSAlexander Eremin static int be_create_menu(char *, char *, FILE **, char *);
70f169c0eaSGlenn Lagasse static char *be_get_auto_name(char *, char *, boolean_t);
71f169c0eaSGlenn Lagasse 
72f169c0eaSGlenn Lagasse /*
73f169c0eaSGlenn Lagasse  * Global error printing
74f169c0eaSGlenn Lagasse  */
75f169c0eaSGlenn Lagasse boolean_t do_print = B_FALSE;
76f169c0eaSGlenn Lagasse 
77f169c0eaSGlenn Lagasse /*
78f169c0eaSGlenn Lagasse  * Private datatypes
79f169c0eaSGlenn Lagasse  */
80f169c0eaSGlenn Lagasse typedef struct zone_be_name_cb_data {
81f169c0eaSGlenn Lagasse 	char *base_be_name;
82f169c0eaSGlenn Lagasse 	int num;
83f169c0eaSGlenn Lagasse } zone_be_name_cb_data_t;
84f169c0eaSGlenn Lagasse 
85f169c0eaSGlenn Lagasse /* ********************************************************************	*/
86f169c0eaSGlenn Lagasse /*			Public Functions				*/
87f169c0eaSGlenn Lagasse /* ******************************************************************** */
88f169c0eaSGlenn Lagasse 
89f5e5a2c4SToomas Soome /*
90f5e5a2c4SToomas Soome  * Callback for ficl to suppress all output from ficl, as we do not
91f5e5a2c4SToomas Soome  * want to confuse user with messages from ficl, and we are only
92f5e5a2c4SToomas Soome  * checking results from function calls.
93f5e5a2c4SToomas Soome  */
94f5e5a2c4SToomas Soome /*ARGSUSED*/
95f5e5a2c4SToomas Soome static void
ficlSuppressTextOutput(ficlCallback * cb,char * text)96f5e5a2c4SToomas Soome ficlSuppressTextOutput(ficlCallback *cb, char *text)
97f5e5a2c4SToomas Soome {
98f5e5a2c4SToomas Soome 	/* This function is intentionally doing nothing. */
99f5e5a2c4SToomas Soome }
100f5e5a2c4SToomas Soome 
101f5e5a2c4SToomas Soome /*
102f5e5a2c4SToomas Soome  * Function:	be_get_boot_args
103f5e5a2c4SToomas Soome  * Description:	Returns the fast boot argument string for enumerated BE.
104f5e5a2c4SToomas Soome  * Parameters:
105f5e5a2c4SToomas Soome  *		fbarg - pointer to argument string.
106f5e5a2c4SToomas Soome  *		entry - index of BE.
107f5e5a2c4SToomas Soome  * Returns:
108f5e5a2c4SToomas Soome  *		fast boot argument string.
109f5e5a2c4SToomas Soome  * Scope:
110f5e5a2c4SToomas Soome  *		Public
111f5e5a2c4SToomas Soome  */
112f5e5a2c4SToomas Soome int
be_get_boot_args(char ** fbarg,int entry)113f5e5a2c4SToomas Soome be_get_boot_args(char **fbarg, int entry)
114f5e5a2c4SToomas Soome {
115f5e5a2c4SToomas Soome 	be_node_list_t *node, *be_nodes = NULL;
116f5e5a2c4SToomas Soome 	be_transaction_data_t bt = {0};
117f5e5a2c4SToomas Soome 	char *mountpoint = NULL;
118f5e5a2c4SToomas Soome 	boolean_t be_mounted = B_FALSE;
119f5e5a2c4SToomas Soome 	int ret = BE_SUCCESS;
120f5e5a2c4SToomas Soome 	int index;
121f5e5a2c4SToomas Soome 	ficlVm *vm;
122f5e5a2c4SToomas Soome 
123f5e5a2c4SToomas Soome 	*fbarg = NULL;
124f5e5a2c4SToomas Soome 	if (!be_zfs_init())
125f5e5a2c4SToomas Soome 		return (BE_ERR_INIT);
126f5e5a2c4SToomas Soome 
127f5e5a2c4SToomas Soome 	/*
128f5e5a2c4SToomas Soome 	 * need pool name, menu.lst has entries from our pool only
129f5e5a2c4SToomas Soome 	 */
130f5e5a2c4SToomas Soome 	ret = be_find_current_be(&bt);
131f5e5a2c4SToomas Soome 	if (ret != BE_SUCCESS) {
132f5e5a2c4SToomas Soome 		be_zfs_fini();
133f5e5a2c4SToomas Soome 		return (ret);
134f5e5a2c4SToomas Soome 	}
135f5e5a2c4SToomas Soome 
136f5e5a2c4SToomas Soome 	/*
137f5e5a2c4SToomas Soome 	 * be_get_boot_args() is for loader, fail with grub will trigger
138f5e5a2c4SToomas Soome 	 * normal boot.
139f5e5a2c4SToomas Soome 	 */
140f5e5a2c4SToomas Soome 	if (be_has_grub()) {
141f5e5a2c4SToomas Soome 		ret = BE_ERR_INIT;
142f5e5a2c4SToomas Soome 		goto done;
143f5e5a2c4SToomas Soome 	}
144f5e5a2c4SToomas Soome 
145a897f28bSAndy Fiddaman 	ret = _be_list(NULL, &be_nodes, BE_LIST_DEFAULT);
146f5e5a2c4SToomas Soome 	if (ret != BE_SUCCESS)
147f5e5a2c4SToomas Soome 		goto done;
148f5e5a2c4SToomas Soome 
149f5e5a2c4SToomas Soome 	/*
150f5e5a2c4SToomas Soome 	 * iterate through be_nodes,
151f5e5a2c4SToomas Soome 	 * if entry == -1, stop if be_active_on_boot,
152f5e5a2c4SToomas Soome 	 * else stop if index == entry.
153f5e5a2c4SToomas Soome 	 */
154f5e5a2c4SToomas Soome 	index = 0;
155f5e5a2c4SToomas Soome 	for (node = be_nodes; node != NULL; node = node->be_next_node) {
156f5e5a2c4SToomas Soome 		if (strcmp(node->be_rpool, bt.obe_zpool) != 0)
157f5e5a2c4SToomas Soome 			continue;
158f5e5a2c4SToomas Soome 		if (entry == BE_ENTRY_DEFAULT &&
159f5e5a2c4SToomas Soome 		    node->be_active_on_boot == B_TRUE)
160f5e5a2c4SToomas Soome 			break;
161f5e5a2c4SToomas Soome 		if (index == entry)
162f5e5a2c4SToomas Soome 			break;
163f5e5a2c4SToomas Soome 		index++;
164f5e5a2c4SToomas Soome 	}
165f5e5a2c4SToomas Soome 	if (node == NULL) {
166f5e5a2c4SToomas Soome 		be_free_list(be_nodes);
167f5e5a2c4SToomas Soome 		ret = BE_ERR_NOENT;
168f5e5a2c4SToomas Soome 		goto done;
169f5e5a2c4SToomas Soome 	}
170f5e5a2c4SToomas Soome 
171f5e5a2c4SToomas Soome 	/* try to mount inactive be */
172f5e5a2c4SToomas Soome 	if (node->be_active == B_FALSE) {
173f5e5a2c4SToomas Soome 		ret = _be_mount(node->be_node_name, &mountpoint,
174f5e5a2c4SToomas Soome 		    BE_MOUNT_FLAG_NO_ZONES);
175f5e5a2c4SToomas Soome 		if (ret != BE_SUCCESS && ret != BE_ERR_MOUNTED) {
176f5e5a2c4SToomas Soome 			be_free_list(be_nodes);
177f5e5a2c4SToomas Soome 			goto done;
178f5e5a2c4SToomas Soome 		} else
179f5e5a2c4SToomas Soome 			be_mounted = B_TRUE;
180f5e5a2c4SToomas Soome 	}
181f5e5a2c4SToomas Soome 
182f5e5a2c4SToomas Soome 	vm = bf_init("", ficlSuppressTextOutput);
183f5e5a2c4SToomas Soome 	if (vm != NULL) {
184f5e5a2c4SToomas Soome 		/*
185f5e5a2c4SToomas Soome 		 * zfs MAXNAMELEN is 256, so we need to pick buf large enough
186f5e5a2c4SToomas Soome 		 * to contain such names.
187f5e5a2c4SToomas Soome 		 */
188f5e5a2c4SToomas Soome 		char buf[MAXNAMELEN * 2];
189f5e5a2c4SToomas Soome 		char *kernel_options = NULL;
190f5e5a2c4SToomas Soome 		char *kernel = NULL;
191f5e5a2c4SToomas Soome 		char *tmp;
192f5e5a2c4SToomas Soome 		zpool_handle_t *zph;
193f5e5a2c4SToomas Soome 
194f5e5a2c4SToomas Soome 		/*
195f5e5a2c4SToomas Soome 		 * just try to interpret following words. on error
196f5e5a2c4SToomas Soome 		 * we will be missing kernelname, and will get out.
197f5e5a2c4SToomas Soome 		 */
198f5e5a2c4SToomas Soome 		(void) snprintf(buf, sizeof (buf), "set currdev=zfs:%s:",
199f5e5a2c4SToomas Soome 		    node->be_root_ds);
200f5e5a2c4SToomas Soome 		ret = ficlVmEvaluate(vm, buf);
201f5e5a2c4SToomas Soome 		if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
202f5e5a2c4SToomas Soome 			be_print_err(gettext("be_get_boot_args: error "
203f5e5a2c4SToomas Soome 			    "interpreting boot config: %d\n"), ret);
204f5e5a2c4SToomas Soome 			bf_fini();
205f5e5a2c4SToomas Soome 			ret = BE_ERR_NO_MENU;
206f5e5a2c4SToomas Soome 			goto cleanup;
207f5e5a2c4SToomas Soome 		}
208f5e5a2c4SToomas Soome 		(void) snprintf(buf, sizeof (buf),
209f5e5a2c4SToomas Soome 		    "include /boot/forth/loader.4th");
210f5e5a2c4SToomas Soome 		ret = ficlVmEvaluate(vm, buf);
211f5e5a2c4SToomas Soome 		if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
212f5e5a2c4SToomas Soome 			be_print_err(gettext("be_get_boot_args: error "
213f5e5a2c4SToomas Soome 			    "interpreting boot config: %d\n"), ret);
214f5e5a2c4SToomas Soome 			bf_fini();
215f5e5a2c4SToomas Soome 			ret = BE_ERR_NO_MENU;
216f5e5a2c4SToomas Soome 			goto cleanup;
217f5e5a2c4SToomas Soome 		}
218f5e5a2c4SToomas Soome 		(void) snprintf(buf, sizeof (buf), "start");
219f5e5a2c4SToomas Soome 		ret = ficlVmEvaluate(vm, buf);
220f5e5a2c4SToomas Soome 		if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
221f5e5a2c4SToomas Soome 			be_print_err(gettext("be_get_boot_args: error "
222f5e5a2c4SToomas Soome 			    "interpreting boot config: %d\n"), ret);
223f5e5a2c4SToomas Soome 			bf_fini();
224f5e5a2c4SToomas Soome 			ret = BE_ERR_NO_MENU;
225f5e5a2c4SToomas Soome 			goto cleanup;
226f5e5a2c4SToomas Soome 		}
227f5e5a2c4SToomas Soome 		(void) snprintf(buf, sizeof (buf), "boot");
228f5e5a2c4SToomas Soome 		ret = ficlVmEvaluate(vm, buf);
229f5e5a2c4SToomas Soome 		bf_fini();
230f5e5a2c4SToomas Soome 		if (ret != FICL_VM_STATUS_OUT_OF_TEXT) {
231f5e5a2c4SToomas Soome 			be_print_err(gettext("be_get_boot_args: error "
232f5e5a2c4SToomas Soome 			    "interpreting boot config: %d\n"), ret);
233f5e5a2c4SToomas Soome 			ret = BE_ERR_NO_MENU;
234f5e5a2c4SToomas Soome 			goto cleanup;
235f5e5a2c4SToomas Soome 		}
236f5e5a2c4SToomas Soome 
237f5e5a2c4SToomas Soome 		kernel_options = getenv("boot-args");
238f5e5a2c4SToomas Soome 		kernel = getenv("kernelname");
239f5e5a2c4SToomas Soome 
240f5e5a2c4SToomas Soome 		if (kernel == NULL) {
241f5e5a2c4SToomas Soome 			be_print_err(gettext("be_get_boot_args: no kernel\n"));
242f5e5a2c4SToomas Soome 			ret = BE_ERR_NOENT;
243f5e5a2c4SToomas Soome 			goto cleanup;
244f5e5a2c4SToomas Soome 		}
245f5e5a2c4SToomas Soome 
246f5e5a2c4SToomas Soome 		if ((zph = zpool_open(g_zfs, node->be_rpool)) == NULL) {
247f5e5a2c4SToomas Soome 			be_print_err(gettext("be_get_boot_args: failed to "
248f5e5a2c4SToomas Soome 			    "open root pool (%s): %s\n"), node->be_rpool,
249f5e5a2c4SToomas Soome 			    libzfs_error_description(g_zfs));
250f5e5a2c4SToomas Soome 			ret = zfs_err_to_be_err(g_zfs);
251f5e5a2c4SToomas Soome 			goto cleanup;
252f5e5a2c4SToomas Soome 		}
253f5e5a2c4SToomas Soome 		ret = zpool_get_physpath(zph, buf, sizeof (buf));
254f5e5a2c4SToomas Soome 		zpool_close(zph);
255f5e5a2c4SToomas Soome 		if (ret != 0) {
256f5e5a2c4SToomas Soome 			be_print_err(gettext("be_get_boot_args: failed to "
257f5e5a2c4SToomas Soome 			    "get physpath\n"));
258f5e5a2c4SToomas Soome 			goto cleanup;
259f5e5a2c4SToomas Soome 		}
260f5e5a2c4SToomas Soome 
261f5e5a2c4SToomas Soome 		/* zpool_get_physpath() can return space separated list */
262f5e5a2c4SToomas Soome 		tmp = buf;
263f5e5a2c4SToomas Soome 		tmp = strsep(&tmp, " ");
264f5e5a2c4SToomas Soome 
265f5e5a2c4SToomas Soome 		if (kernel_options == NULL || *kernel_options == '\0')
266f5e5a2c4SToomas Soome 			(void) asprintf(fbarg, "/ %s "
267f5e5a2c4SToomas Soome 			    "-B zfs-bootfs=%s,bootpath=\"%s\"\n", kernel,
268f5e5a2c4SToomas Soome 			    node->be_root_ds, tmp);
269f5e5a2c4SToomas Soome 		else
270f5e5a2c4SToomas Soome 			(void) asprintf(fbarg, "/ %s %s "
271f5e5a2c4SToomas Soome 			    "-B zfs-bootfs=%s,bootpath=\"%s\"\n", kernel,
272f5e5a2c4SToomas Soome 			    kernel_options, node->be_root_ds, tmp);
273f5e5a2c4SToomas Soome 
274ce829a51SJohn Levon 		if (*fbarg == NULL)
275f5e5a2c4SToomas Soome 			ret = BE_ERR_NOMEM;
276f5e5a2c4SToomas Soome 		else
277f5e5a2c4SToomas Soome 			ret = 0;
278f5e5a2c4SToomas Soome 	} else
279f5e5a2c4SToomas Soome 		ret = BE_ERR_NOMEM;
280f5e5a2c4SToomas Soome cleanup:
281f5e5a2c4SToomas Soome 	if (be_mounted == B_TRUE)
282f5e5a2c4SToomas Soome 		(void) _be_unmount(node->be_node_name, BE_UNMOUNT_FLAG_FORCE);
283f5e5a2c4SToomas Soome 	be_free_list(be_nodes);
284f5e5a2c4SToomas Soome done:
285f5e5a2c4SToomas Soome 	free(mountpoint);
286f5e5a2c4SToomas Soome 	free(bt.obe_name);
287f5e5a2c4SToomas Soome 	free(bt.obe_root_ds);
288f5e5a2c4SToomas Soome 	free(bt.obe_zpool);
289f5e5a2c4SToomas Soome 	free(bt.obe_snap_name);
290f5e5a2c4SToomas Soome 	free(bt.obe_altroot);
291f5e5a2c4SToomas Soome 	be_zfs_fini();
292f5e5a2c4SToomas Soome 	return (ret);
293f5e5a2c4SToomas Soome }
294f5e5a2c4SToomas Soome 
295f169c0eaSGlenn Lagasse /*
296f169c0eaSGlenn Lagasse  * Function:	be_max_avail
297f169c0eaSGlenn Lagasse  * Description:	Returns the available size for the zfs dataset passed in.
298f169c0eaSGlenn Lagasse  * Parameters:
299f169c0eaSGlenn Lagasse  *		dataset - The dataset we want to get the available space for.
300f169c0eaSGlenn Lagasse  *		ret - The available size will be returned in this.
301f169c0eaSGlenn Lagasse  * Returns:
302f169c0eaSGlenn Lagasse  *		The error returned by the zfs get property function.
303f169c0eaSGlenn Lagasse  * Scope:
304f169c0eaSGlenn Lagasse  *		Public
305f169c0eaSGlenn Lagasse  */
306f169c0eaSGlenn Lagasse int
be_max_avail(char * dataset,uint64_t * ret)307f169c0eaSGlenn Lagasse be_max_avail(char *dataset, uint64_t *ret)
308f169c0eaSGlenn Lagasse {
309f169c0eaSGlenn Lagasse 	zfs_handle_t *zhp;
310f169c0eaSGlenn Lagasse 	int err = 0;
311f169c0eaSGlenn Lagasse 
312f169c0eaSGlenn Lagasse 	/* Initialize libzfs handle */
313f169c0eaSGlenn Lagasse 	if (!be_zfs_init())
314f169c0eaSGlenn Lagasse 		return (BE_ERR_INIT);
315f169c0eaSGlenn Lagasse 
316f169c0eaSGlenn Lagasse 	zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET);
317f169c0eaSGlenn Lagasse 	if (zhp == NULL) {
318f169c0eaSGlenn Lagasse 		/*
319f169c0eaSGlenn Lagasse 		 * The zfs_open failed return an error
320f169c0eaSGlenn Lagasse 		 */
321f169c0eaSGlenn Lagasse 		err = zfs_err_to_be_err(g_zfs);
322f169c0eaSGlenn Lagasse 	} else {
323f169c0eaSGlenn Lagasse 		err = be_maxsize_avail(zhp, ret);
324f169c0eaSGlenn Lagasse 	}
325f169c0eaSGlenn Lagasse 	ZFS_CLOSE(zhp);
326f169c0eaSGlenn Lagasse 	be_zfs_fini();
327f169c0eaSGlenn Lagasse 	return (err);
328f169c0eaSGlenn Lagasse }
329f169c0eaSGlenn Lagasse 
330f169c0eaSGlenn Lagasse /*
331f169c0eaSGlenn Lagasse  * Function:	libbe_print_errors
332f169c0eaSGlenn Lagasse  * Description:	Turns on/off error output for the library.
333f169c0eaSGlenn Lagasse  * Parameter:
334f169c0eaSGlenn Lagasse  *		set_do_print - Boolean that turns library error
335f169c0eaSGlenn Lagasse  *			       printing on or off.
336f169c0eaSGlenn Lagasse  * Returns:
337f169c0eaSGlenn Lagasse  *		None
338f169c0eaSGlenn Lagasse  * Scope:
339f169c0eaSGlenn Lagasse  *		Public;
340f169c0eaSGlenn Lagasse  */
341f169c0eaSGlenn Lagasse void
libbe_print_errors(boolean_t set_do_print)342f169c0eaSGlenn Lagasse libbe_print_errors(boolean_t set_do_print)
343f169c0eaSGlenn Lagasse {
344f169c0eaSGlenn Lagasse 	do_print = set_do_print;
345f169c0eaSGlenn Lagasse }
346f169c0eaSGlenn Lagasse 
347f169c0eaSGlenn Lagasse /* ********************************************************************	*/
348f169c0eaSGlenn Lagasse /*			Semi-Private Functions				*/
349f169c0eaSGlenn Lagasse /* ******************************************************************** */
350f169c0eaSGlenn Lagasse 
351f169c0eaSGlenn Lagasse /*
352f169c0eaSGlenn Lagasse  * Function:	be_zfs_init
353f169c0eaSGlenn Lagasse  * Description:	Initializes the libary global libzfs handle.
354f169c0eaSGlenn Lagasse  * Parameters:
355f169c0eaSGlenn Lagasse  *		None
356f169c0eaSGlenn Lagasse  * Returns:
357f169c0eaSGlenn Lagasse  *		B_TRUE - Success
358f169c0eaSGlenn Lagasse  *		B_FALSE - Failure
359f169c0eaSGlenn Lagasse  * Scope:
360f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
361f169c0eaSGlenn Lagasse  */
362f169c0eaSGlenn Lagasse boolean_t
be_zfs_init(void)363f169c0eaSGlenn Lagasse be_zfs_init(void)
364f169c0eaSGlenn Lagasse {
365f169c0eaSGlenn Lagasse 	be_zfs_fini();
366f169c0eaSGlenn Lagasse 
367f169c0eaSGlenn Lagasse 	if ((g_zfs = libzfs_init()) == NULL) {
368f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_zfs_init: failed to initialize ZFS "
369f169c0eaSGlenn Lagasse 		    "library\n"));
370f169c0eaSGlenn Lagasse 		return (B_FALSE);
371f169c0eaSGlenn Lagasse 	}
372f169c0eaSGlenn Lagasse 
373f169c0eaSGlenn Lagasse 	return (B_TRUE);
374f169c0eaSGlenn Lagasse }
375f169c0eaSGlenn Lagasse 
376f169c0eaSGlenn Lagasse /*
377f169c0eaSGlenn Lagasse  * Function:	be_zfs_fini
378f169c0eaSGlenn Lagasse  * Description:	Closes the library global libzfs handle if it currently open.
379f169c0eaSGlenn Lagasse  * Parameter:
380f169c0eaSGlenn Lagasse  *		None
381f169c0eaSGlenn Lagasse  * Returns:
382f169c0eaSGlenn Lagasse  *		None
383f169c0eaSGlenn Lagasse  * Scope:
384f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
385f169c0eaSGlenn Lagasse  */
386f169c0eaSGlenn Lagasse void
be_zfs_fini(void)387f169c0eaSGlenn Lagasse be_zfs_fini(void)
388f169c0eaSGlenn Lagasse {
389f169c0eaSGlenn Lagasse 	if (g_zfs)
390f169c0eaSGlenn Lagasse 		libzfs_fini(g_zfs);
391f169c0eaSGlenn Lagasse 
392f169c0eaSGlenn Lagasse 	g_zfs = NULL;
393f169c0eaSGlenn Lagasse }
394f169c0eaSGlenn Lagasse 
395de1ab35cSAlexander Eremin /*
396de1ab35cSAlexander Eremin  * Function:	be_get_defaults
397de1ab35cSAlexander Eremin  * Description:	Open defaults and gets be default paramets
398de1ab35cSAlexander Eremin  * Parameters:
399de1ab35cSAlexander Eremin  *		defaults - be defaults struct
400de1ab35cSAlexander Eremin  * Returns:
401de1ab35cSAlexander Eremin  *		None
402de1ab35cSAlexander Eremin  * Scope:
403de1ab35cSAlexander Eremin  *		Semi-private (library wide use only)
404de1ab35cSAlexander Eremin  */
405de1ab35cSAlexander Eremin void
be_get_defaults(struct be_defaults * defaults)406de1ab35cSAlexander Eremin be_get_defaults(struct be_defaults *defaults)
407de1ab35cSAlexander Eremin {
408de1ab35cSAlexander Eremin 	void	*defp;
409de1ab35cSAlexander Eremin 
410fa0c327aSToomas Soome 	defaults->be_deflt_grub = B_FALSE;
411de1ab35cSAlexander Eremin 	defaults->be_deflt_rpool_container = B_FALSE;
412de1ab35cSAlexander Eremin 	defaults->be_deflt_bename_starts_with[0] = '\0';
413de1ab35cSAlexander Eremin 
414de1ab35cSAlexander Eremin 	if ((defp = defopen_r(BE_DEFAULTS)) != NULL) {
415de1ab35cSAlexander Eremin 		const char *res = defread_r(BE_DFLT_BENAME_STARTS, defp);
416fa0c327aSToomas Soome 		if (res != NULL && res[0] != '\0') {
417de1ab35cSAlexander Eremin 			(void) strlcpy(defaults->be_deflt_bename_starts_with,
4189adfa60dSMatthew Ahrens 			    res, ZFS_MAX_DATASET_NAME_LEN);
419de1ab35cSAlexander Eremin 			defaults->be_deflt_rpool_container = B_TRUE;
420de1ab35cSAlexander Eremin 		}
421fa0c327aSToomas Soome 		if (be_is_isa("i386")) {
422fa0c327aSToomas Soome 			res = defread_r(BE_DFLT_BE_HAS_GRUB, defp);
423fa0c327aSToomas Soome 			if (res != NULL && res[0] != '\0') {
424fa0c327aSToomas Soome 				if (strcasecmp(res, "true") == 0)
425fa0c327aSToomas Soome 					defaults->be_deflt_grub = B_TRUE;
426fa0c327aSToomas Soome 			}
427fa0c327aSToomas Soome 		}
428de1ab35cSAlexander Eremin 		defclose_r(defp);
429de1ab35cSAlexander Eremin 	}
430de1ab35cSAlexander Eremin }
431de1ab35cSAlexander Eremin 
432f169c0eaSGlenn Lagasse /*
433f169c0eaSGlenn Lagasse  * Function:	be_make_root_ds
434f169c0eaSGlenn Lagasse  * Description:	Generate string for BE's root dataset given the pool
435f169c0eaSGlenn Lagasse  *		it lives in and the BE name.
436f169c0eaSGlenn Lagasse  * Parameters:
437f169c0eaSGlenn Lagasse  *		zpool - pointer zpool name.
438f169c0eaSGlenn Lagasse  *		be_name - pointer to BE name.
439f169c0eaSGlenn Lagasse  *		be_root_ds - pointer to buffer to return BE root dataset in.
440f169c0eaSGlenn Lagasse  *		be_root_ds_size - size of be_root_ds
441f169c0eaSGlenn Lagasse  * Returns:
442*ec8422d0SAndy Fiddaman  *		BE_SUCCESS - Success
443*ec8422d0SAndy Fiddaman  *		be_errno_t - Failure
444f169c0eaSGlenn Lagasse  * Scope:
445f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
446f169c0eaSGlenn Lagasse  */
447*ec8422d0SAndy Fiddaman int
be_make_root_ds(const char * zpool,const char * be_name,char * be_root_ds,int be_root_ds_size)448f169c0eaSGlenn Lagasse be_make_root_ds(const char *zpool, const char *be_name, char *be_root_ds,
449f169c0eaSGlenn Lagasse     int be_root_ds_size)
450f169c0eaSGlenn Lagasse {
451de1ab35cSAlexander Eremin 	struct be_defaults be_defaults;
452de1ab35cSAlexander Eremin 	be_get_defaults(&be_defaults);
453*ec8422d0SAndy Fiddaman 
454*ec8422d0SAndy Fiddaman 	assert(zpool != NULL);
455de1ab35cSAlexander Eremin 
4567e0e2549SAlexander Eremin 	if (getzoneid() == GLOBAL_ZONEID) {
4577e0e2549SAlexander Eremin 		if (be_defaults.be_deflt_rpool_container) {
4587e0e2549SAlexander Eremin 			(void) snprintf(be_root_ds, be_root_ds_size,
4597e0e2549SAlexander Eremin 			    "%s/%s", zpool, be_name);
4607e0e2549SAlexander Eremin 		} else {
4617e0e2549SAlexander Eremin 			(void) snprintf(be_root_ds, be_root_ds_size,
4627e0e2549SAlexander Eremin 			    "%s/%s/%s", zpool, BE_CONTAINER_DS_NAME, be_name);
4637e0e2549SAlexander Eremin 		}
4647e0e2549SAlexander Eremin 	} else {
4657e0e2549SAlexander Eremin 		/*
466*ec8422d0SAndy Fiddaman 		 * In a non-global zone we can use the path from the mounted
467*ec8422d0SAndy Fiddaman 		 * root dataset to generate the BE's root dataset string.
4687e0e2549SAlexander Eremin 		 */
469*ec8422d0SAndy Fiddaman 		char *root_ds = be_get_ds_from_dir("/");
470*ec8422d0SAndy Fiddaman 
471*ec8422d0SAndy Fiddaman 		if (root_ds == NULL) {
4727e0e2549SAlexander Eremin 			be_print_err(gettext("be_make_root_ds: zone root "
4737e0e2549SAlexander Eremin 			    "dataset is not mounted\n"));
474*ec8422d0SAndy Fiddaman 			return (BE_ERR_NOTMOUNTED);
4757e0e2549SAlexander Eremin 		}
476*ec8422d0SAndy Fiddaman 		if (strncmp(root_ds, zpool, strlen(zpool)) != 0 ||
477*ec8422d0SAndy Fiddaman 		    root_ds[strlen(zpool)] != '/') {
478*ec8422d0SAndy Fiddaman 			/*
479*ec8422d0SAndy Fiddaman 			 * This pool is not the one that contains the zone
480*ec8422d0SAndy Fiddaman 			 * root.
481*ec8422d0SAndy Fiddaman 			 */
482*ec8422d0SAndy Fiddaman 			return (BE_ERR_ACCESS);
483*ec8422d0SAndy Fiddaman 		}
484*ec8422d0SAndy Fiddaman 
485*ec8422d0SAndy Fiddaman 		(void) snprintf(be_root_ds, be_root_ds_size, "%s/%s",
486*ec8422d0SAndy Fiddaman 		    dirname(root_ds), be_name);
4877e0e2549SAlexander Eremin 	}
488*ec8422d0SAndy Fiddaman 
489*ec8422d0SAndy Fiddaman 	return (BE_SUCCESS);
490f169c0eaSGlenn Lagasse }
491f169c0eaSGlenn Lagasse 
492f169c0eaSGlenn Lagasse /*
493f169c0eaSGlenn Lagasse  * Function:	be_make_container_ds
494f169c0eaSGlenn Lagasse  * Description:	Generate string for the BE container dataset given a pool name.
495f169c0eaSGlenn Lagasse  * Parameters:
496f169c0eaSGlenn Lagasse  *		zpool - pointer zpool name.
497f169c0eaSGlenn Lagasse  *		container_ds - pointer to buffer to return BE container
498f169c0eaSGlenn Lagasse  *			dataset in.
499f169c0eaSGlenn Lagasse  *		container_ds_size - size of container_ds
500f169c0eaSGlenn Lagasse  * Returns:
501*ec8422d0SAndy Fiddaman  *		BE_SUCCESS - Success
502*ec8422d0SAndy Fiddaman  *		be_errno_t - Failure
503f169c0eaSGlenn Lagasse  * Scope:
504f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
505f169c0eaSGlenn Lagasse  */
506*ec8422d0SAndy Fiddaman int
be_make_container_ds(const char * zpool,char * container_ds,int container_ds_size)507*ec8422d0SAndy Fiddaman be_make_container_ds(const char *zpool, char *container_ds,
508f169c0eaSGlenn Lagasse     int container_ds_size)
509f169c0eaSGlenn Lagasse {
510de1ab35cSAlexander Eremin 	struct be_defaults be_defaults;
511de1ab35cSAlexander Eremin 	be_get_defaults(&be_defaults);
512de1ab35cSAlexander Eremin 
5137e0e2549SAlexander Eremin 	if (getzoneid() == GLOBAL_ZONEID) {
5147e0e2549SAlexander Eremin 		if (be_defaults.be_deflt_rpool_container) {
5157e0e2549SAlexander Eremin 			(void) snprintf(container_ds, container_ds_size,
5167e0e2549SAlexander Eremin 			    "%s", zpool);
5177e0e2549SAlexander Eremin 		} else {
5187e0e2549SAlexander Eremin 			(void) snprintf(container_ds, container_ds_size,
5197e0e2549SAlexander Eremin 			    "%s/%s", zpool, BE_CONTAINER_DS_NAME);
5207e0e2549SAlexander Eremin 		}
5217e0e2549SAlexander Eremin 	} else {
522*ec8422d0SAndy Fiddaman 		char *root_ds = be_get_ds_from_dir("/");
523*ec8422d0SAndy Fiddaman 
524*ec8422d0SAndy Fiddaman 		if (root_ds == NULL) {
5257e0e2549SAlexander Eremin 			be_print_err(gettext("be_make_container_ds: zone root "
5267e0e2549SAlexander Eremin 			    "dataset is not mounted\n"));
527*ec8422d0SAndy Fiddaman 			return (BE_ERR_NOTMOUNTED);
528*ec8422d0SAndy Fiddaman 		}
529*ec8422d0SAndy Fiddaman 		if (strncmp(root_ds, zpool, strlen(zpool)) != 0 ||
530*ec8422d0SAndy Fiddaman 		    root_ds[strlen(zpool)] != '/') {
531*ec8422d0SAndy Fiddaman 			/*
532*ec8422d0SAndy Fiddaman 			 * This pool is not the one that contains the zone
533*ec8422d0SAndy Fiddaman 			 * root.
534*ec8422d0SAndy Fiddaman 			 */
535*ec8422d0SAndy Fiddaman 			return (BE_ERR_ACCESS);
5367e0e2549SAlexander Eremin 		}
537*ec8422d0SAndy Fiddaman 		(void) strlcpy(container_ds, dirname(root_ds),
538*ec8422d0SAndy Fiddaman 		    container_ds_size);
5397e0e2549SAlexander Eremin 	}
540*ec8422d0SAndy Fiddaman 
541*ec8422d0SAndy Fiddaman 	return (BE_SUCCESS);
542f169c0eaSGlenn Lagasse }
543f169c0eaSGlenn Lagasse 
54402123a49SAndy Fiddaman /*
54502123a49SAndy Fiddaman  * Function:	be_make_root_container_ds
54602123a49SAndy Fiddaman  * Description:	Generate string for the BE root container dataset given a pool
54702123a49SAndy Fiddaman  *              name.
54802123a49SAndy Fiddaman  * Parameters:
54902123a49SAndy Fiddaman  *		zpool - pointer zpool name.
55002123a49SAndy Fiddaman  *		container_ds - pointer to buffer in which to return result
55102123a49SAndy Fiddaman  *		container_ds_size - size of container_ds
55202123a49SAndy Fiddaman  * Returns:
553*ec8422d0SAndy Fiddaman  *		BE_SUCCESS - Success
554*ec8422d0SAndy Fiddaman  *		be_errno_t - Failure
55502123a49SAndy Fiddaman  * Scope:
55602123a49SAndy Fiddaman  *		Semi-private (library wide use only)
55702123a49SAndy Fiddaman  */
558*ec8422d0SAndy Fiddaman int
be_make_root_container_ds(const char * zpool,char * container_ds,int container_ds_size)55902123a49SAndy Fiddaman be_make_root_container_ds(const char *zpool, char *container_ds,
56002123a49SAndy Fiddaman     int container_ds_size)
56102123a49SAndy Fiddaman {
56202123a49SAndy Fiddaman 	char *root;
563*ec8422d0SAndy Fiddaman 	int ret;
56402123a49SAndy Fiddaman 
565*ec8422d0SAndy Fiddaman 	if ((ret = be_make_container_ds(zpool, container_ds,
566*ec8422d0SAndy Fiddaman 	    container_ds_size)) != BE_SUCCESS) {
567*ec8422d0SAndy Fiddaman 		return (ret);
568*ec8422d0SAndy Fiddaman 	}
56902123a49SAndy Fiddaman 
57002123a49SAndy Fiddaman 	/* If the container DS ends with /ROOT, remove it.  */
57102123a49SAndy Fiddaman 
57202123a49SAndy Fiddaman 	if ((root = strrchr(container_ds, '/')) != NULL &&
57302123a49SAndy Fiddaman 	    strcmp(root + 1, BE_CONTAINER_DS_NAME) == 0) {
57402123a49SAndy Fiddaman 		*root = '\0';
57502123a49SAndy Fiddaman 	}
576*ec8422d0SAndy Fiddaman 
577*ec8422d0SAndy Fiddaman 	return (BE_SUCCESS);
57802123a49SAndy Fiddaman }
57902123a49SAndy Fiddaman 
580f169c0eaSGlenn Lagasse /*
581f169c0eaSGlenn Lagasse  * Function:	be_make_name_from_ds
582f169c0eaSGlenn Lagasse  * Description:	This function takes a dataset name and strips off the
583f169c0eaSGlenn Lagasse  *		BE container dataset portion from the beginning.  The
584f169c0eaSGlenn Lagasse  *		returned name is allocated in heap storage, so the caller
585f169c0eaSGlenn Lagasse  *		is responsible for freeing it.
586f169c0eaSGlenn Lagasse  * Parameters:
587f169c0eaSGlenn Lagasse  *		dataset - dataset to get name from.
588f169c0eaSGlenn Lagasse  *		rc_loc - dataset underwhich the root container dataset lives.
589f169c0eaSGlenn Lagasse  * Returns:
590f169c0eaSGlenn Lagasse  *		name of dataset relative to BE container dataset.
591f169c0eaSGlenn Lagasse  *		NULL if dataset is not under a BE root dataset.
592f169c0eaSGlenn Lagasse  * Scope:
593f169c0eaSGlenn Lagasse  *		Semi-primate (library wide use only)
594f169c0eaSGlenn Lagasse  */
595f169c0eaSGlenn Lagasse char *
be_make_name_from_ds(const char * dataset,char * rc_loc)596f169c0eaSGlenn Lagasse be_make_name_from_ds(const char *dataset, char *rc_loc)
597f169c0eaSGlenn Lagasse {
5989adfa60dSMatthew Ahrens 	char	ds[ZFS_MAX_DATASET_NAME_LEN];
599f169c0eaSGlenn Lagasse 	char	*tok = NULL;
600f169c0eaSGlenn Lagasse 	char	*name = NULL;
601de1ab35cSAlexander Eremin 	struct be_defaults be_defaults;
602de1ab35cSAlexander Eremin 	int	rlen = strlen(rc_loc);
603de1ab35cSAlexander Eremin 
604de1ab35cSAlexander Eremin 	be_get_defaults(&be_defaults);
605f169c0eaSGlenn Lagasse 
606f169c0eaSGlenn Lagasse 	/*
607f169c0eaSGlenn Lagasse 	 * First token is the location of where the root container dataset
608f169c0eaSGlenn Lagasse 	 * lives; it must match rc_loc.
609f169c0eaSGlenn Lagasse 	 */
610de1ab35cSAlexander Eremin 	if (strncmp(dataset, rc_loc, rlen) == 0 && dataset[rlen] == '/')
611de1ab35cSAlexander Eremin 		(void) strlcpy(ds, dataset + rlen + 1, sizeof (ds));
612de1ab35cSAlexander Eremin 	else
613f169c0eaSGlenn Lagasse 		return (NULL);
614f169c0eaSGlenn Lagasse 
615de1ab35cSAlexander Eremin 	if (be_defaults.be_deflt_rpool_container) {
616de1ab35cSAlexander Eremin 		if ((name = strdup(ds)) == NULL) {
617de1ab35cSAlexander Eremin 			be_print_err(gettext("be_make_name_from_ds: "
618de1ab35cSAlexander Eremin 			    "memory allocation failed\n"));
619de1ab35cSAlexander Eremin 			return (NULL);
620de1ab35cSAlexander Eremin 		}
621de1ab35cSAlexander Eremin 	} else {
622de1ab35cSAlexander Eremin 		/* Second token must be BE container dataset name */
623de1ab35cSAlexander Eremin 		if ((tok = strtok(ds, "/")) == NULL ||
624de1ab35cSAlexander Eremin 		    strcmp(tok, BE_CONTAINER_DS_NAME) != 0)
625de1ab35cSAlexander Eremin 			return (NULL);
626f169c0eaSGlenn Lagasse 
627de1ab35cSAlexander Eremin 		/* Return the remaining token if one exists */
628de1ab35cSAlexander Eremin 		if ((tok = strtok(NULL, "")) == NULL)
629de1ab35cSAlexander Eremin 			return (NULL);
630f169c0eaSGlenn Lagasse 
631de1ab35cSAlexander Eremin 		if ((name = strdup(tok)) == NULL) {
632de1ab35cSAlexander Eremin 			be_print_err(gettext("be_make_name_from_ds: "
633de1ab35cSAlexander Eremin 			    "memory allocation failed\n"));
634de1ab35cSAlexander Eremin 			return (NULL);
635de1ab35cSAlexander Eremin 		}
636f169c0eaSGlenn Lagasse 	}
637f169c0eaSGlenn Lagasse 
638f169c0eaSGlenn Lagasse 	return (name);
639f169c0eaSGlenn Lagasse }
640f169c0eaSGlenn Lagasse 
641f169c0eaSGlenn Lagasse /*
642f169c0eaSGlenn Lagasse  * Function:	be_maxsize_avail
643f169c0eaSGlenn Lagasse  * Description:	Returns the available size for the zfs handle passed in.
644f169c0eaSGlenn Lagasse  * Parameters:
645f169c0eaSGlenn Lagasse  *		zhp - A pointer to the open zfs handle.
646f169c0eaSGlenn Lagasse  *		ret - The available size will be returned in this.
647f169c0eaSGlenn Lagasse  * Returns:
648f169c0eaSGlenn Lagasse  *		The error returned by the zfs get property function.
649f169c0eaSGlenn Lagasse  * Scope:
650f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
651f169c0eaSGlenn Lagasse  */
652f169c0eaSGlenn Lagasse int
be_maxsize_avail(zfs_handle_t * zhp,uint64_t * ret)653f169c0eaSGlenn Lagasse be_maxsize_avail(zfs_handle_t *zhp, uint64_t *ret)
654f169c0eaSGlenn Lagasse {
655f169c0eaSGlenn Lagasse 	return ((*ret = zfs_prop_get_int(zhp, ZFS_PROP_AVAILABLE)));
656f169c0eaSGlenn Lagasse }
657f169c0eaSGlenn Lagasse 
658f169c0eaSGlenn Lagasse /*
659f169c0eaSGlenn Lagasse  * Function:	be_append_menu
660f169c0eaSGlenn Lagasse  * Description:	Appends an entry for a BE into the menu.lst.
661f169c0eaSGlenn Lagasse  * Parameters:
662f169c0eaSGlenn Lagasse  *		be_name - pointer to name of BE to add boot menu entry for.
663f169c0eaSGlenn Lagasse  *		be_root_pool - pointer to name of pool BE lives in.
664f169c0eaSGlenn Lagasse  *		boot_pool - Used if the pool containing the grub menu is
665f169c0eaSGlenn Lagasse  *			    different than the one contaiing the BE. This
666f169c0eaSGlenn Lagasse  *			    will normally be NULL.
667f169c0eaSGlenn Lagasse  *		be_orig_root_ds - The root dataset for the BE. This is
668f169c0eaSGlenn Lagasse  *			used to check to see if an entry already exists
669f169c0eaSGlenn Lagasse  *			for this BE.
670f169c0eaSGlenn Lagasse  *		description - pointer to description of BE to be added in
671f169c0eaSGlenn Lagasse  *			the title line for this BEs entry.
672f169c0eaSGlenn Lagasse  * Returns:
673f169c0eaSGlenn Lagasse  *		BE_SUCCESS - Success
674f169c0eaSGlenn Lagasse  *		be_errno_t - Failure
675f169c0eaSGlenn Lagasse  * Scope:
676f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
677f169c0eaSGlenn Lagasse  */
678f169c0eaSGlenn Lagasse int
be_append_menu(char * be_name,char * be_root_pool,char * boot_pool,char * be_orig_root_ds,char * description)679f169c0eaSGlenn Lagasse be_append_menu(char *be_name, char *be_root_pool, char *boot_pool,
680f169c0eaSGlenn Lagasse     char *be_orig_root_ds, char *description)
681f169c0eaSGlenn Lagasse {
682f169c0eaSGlenn Lagasse 	zfs_handle_t *zhp = NULL;
683f169c0eaSGlenn Lagasse 	char menu_file[MAXPATHLEN];
684f169c0eaSGlenn Lagasse 	char be_root_ds[MAXPATHLEN];
685f169c0eaSGlenn Lagasse 	char line[BUFSIZ];
686f169c0eaSGlenn Lagasse 	char temp_line[BUFSIZ];
687f169c0eaSGlenn Lagasse 	char title[MAXPATHLEN];
688f169c0eaSGlenn Lagasse 	char *entries[BUFSIZ];
689f169c0eaSGlenn Lagasse 	char *tmp_entries[BUFSIZ];
690f169c0eaSGlenn Lagasse 	char *pool_mntpnt = NULL;
691f169c0eaSGlenn Lagasse 	char *ptmp_mntpnt = NULL;
692f169c0eaSGlenn Lagasse 	char *orig_mntpnt = NULL;
693f169c0eaSGlenn Lagasse 	boolean_t found_be = B_FALSE;
694f169c0eaSGlenn Lagasse 	boolean_t found_orig_be = B_FALSE;
695f169c0eaSGlenn Lagasse 	boolean_t found_title = B_FALSE;
696f169c0eaSGlenn Lagasse 	boolean_t pool_mounted = B_FALSE;
697f169c0eaSGlenn Lagasse 	boolean_t collect_lines = B_FALSE;
698f169c0eaSGlenn Lagasse 	FILE *menu_fp = NULL;
699f169c0eaSGlenn Lagasse 	int err = 0, ret = BE_SUCCESS;
700f169c0eaSGlenn Lagasse 	int i, num_tmp_lines = 0, num_lines = 0;
701f169c0eaSGlenn Lagasse 
702f169c0eaSGlenn Lagasse 	if (be_name == NULL || be_root_pool == NULL)
703f169c0eaSGlenn Lagasse 		return (BE_ERR_INVAL);
704f169c0eaSGlenn Lagasse 
705f169c0eaSGlenn Lagasse 	if (boot_pool == NULL)
706f169c0eaSGlenn Lagasse 		boot_pool = be_root_pool;
707f169c0eaSGlenn Lagasse 
708f169c0eaSGlenn Lagasse 	if ((zhp = zfs_open(g_zfs, be_root_pool, ZFS_TYPE_DATASET)) == NULL) {
709f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_append_menu: failed to open "
710f169c0eaSGlenn Lagasse 		    "pool dataset for %s: %s\n"), be_root_pool,
711f169c0eaSGlenn Lagasse 		    libzfs_error_description(g_zfs));
712f169c0eaSGlenn Lagasse 		return (zfs_err_to_be_err(g_zfs));
713f169c0eaSGlenn Lagasse 	}
714f169c0eaSGlenn Lagasse 
715f169c0eaSGlenn Lagasse 	/*
716f169c0eaSGlenn Lagasse 	 * Check to see if the pool's dataset is mounted. If it isn't we'll
717f169c0eaSGlenn Lagasse 	 * attempt to mount it.
718f169c0eaSGlenn Lagasse 	 */
719f169c0eaSGlenn Lagasse 	if ((ret = be_mount_pool(zhp, &ptmp_mntpnt, &orig_mntpnt,
720f169c0eaSGlenn Lagasse 	    &pool_mounted)) != BE_SUCCESS) {
721f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_append_menu: pool dataset "
722f169c0eaSGlenn Lagasse 		    "(%s) could not be mounted\n"), be_root_pool);
723f169c0eaSGlenn Lagasse 		ZFS_CLOSE(zhp);
724f169c0eaSGlenn Lagasse 		return (ret);
725f169c0eaSGlenn Lagasse 	}
726f169c0eaSGlenn Lagasse 
727f169c0eaSGlenn Lagasse 	/*
728f169c0eaSGlenn Lagasse 	 * Get the mountpoint for the root pool dataset.
729f169c0eaSGlenn Lagasse 	 */
730f169c0eaSGlenn Lagasse 	if (!zfs_is_mounted(zhp, &pool_mntpnt)) {
731f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_append_menu: pool "
732f169c0eaSGlenn Lagasse 		    "dataset (%s) is not mounted. Can't set "
733f169c0eaSGlenn Lagasse 		    "the default BE in the grub menu.\n"), be_root_pool);
734f169c0eaSGlenn Lagasse 		ret = BE_ERR_NO_MENU;
735f169c0eaSGlenn Lagasse 		goto cleanup;
736f169c0eaSGlenn Lagasse 	}
737f169c0eaSGlenn Lagasse 
738f169c0eaSGlenn Lagasse 	/*
739f169c0eaSGlenn Lagasse 	 * Check to see if this system supports grub
740f169c0eaSGlenn Lagasse 	 */
741f169c0eaSGlenn Lagasse 	if (be_has_grub()) {
742f169c0eaSGlenn Lagasse 		(void) snprintf(menu_file, sizeof (menu_file),
743f169c0eaSGlenn Lagasse 		    "%s%s", pool_mntpnt, BE_GRUB_MENU);
744f169c0eaSGlenn Lagasse 	} else {
745f169c0eaSGlenn Lagasse 		(void) snprintf(menu_file, sizeof (menu_file),
746f169c0eaSGlenn Lagasse 		    "%s%s", pool_mntpnt, BE_SPARC_MENU);
747f169c0eaSGlenn Lagasse 	}
748f169c0eaSGlenn Lagasse 
749*ec8422d0SAndy Fiddaman 	if ((ret = be_make_root_ds(be_root_pool, be_name, be_root_ds,
750*ec8422d0SAndy Fiddaman 	    sizeof (be_root_ds))) != BE_SUCCESS) {
751*ec8422d0SAndy Fiddaman 		be_print_err(gettext("%s: failed to get BE container dataset "
752*ec8422d0SAndy Fiddaman 		    "for %s/%s\n"), __func__, be_root_pool, be_name);
753*ec8422d0SAndy Fiddaman 		goto cleanup;
754*ec8422d0SAndy Fiddaman 	}
755f169c0eaSGlenn Lagasse 
756f169c0eaSGlenn Lagasse 	/*
757f169c0eaSGlenn Lagasse 	 * Iterate through menu first to make sure the BE doesn't already
758f169c0eaSGlenn Lagasse 	 * have an entry in the menu.
759f169c0eaSGlenn Lagasse 	 *
760f169c0eaSGlenn Lagasse 	 * Additionally while iterating through the menu, if we have an
761f169c0eaSGlenn Lagasse 	 * original root dataset for a BE we're cloning from, we need to keep
762f169c0eaSGlenn Lagasse 	 * track of that BE's menu entry. We will then use the lines from
763f169c0eaSGlenn Lagasse 	 * that entry to create the entry for the new BE.
764f169c0eaSGlenn Lagasse 	 */
765de1ab35cSAlexander Eremin 	if ((ret = be_open_menu(be_root_pool, menu_file,
766f169c0eaSGlenn Lagasse 	    &menu_fp, "r", B_TRUE)) != BE_SUCCESS) {
767f169c0eaSGlenn Lagasse 		goto cleanup;
768f169c0eaSGlenn Lagasse 	} else if (menu_fp == NULL) {
769f169c0eaSGlenn Lagasse 		ret = BE_ERR_NO_MENU;
770f169c0eaSGlenn Lagasse 		goto cleanup;
771f169c0eaSGlenn Lagasse 	}
772f169c0eaSGlenn Lagasse 
773f169c0eaSGlenn Lagasse 	free(pool_mntpnt);
774f169c0eaSGlenn Lagasse 	pool_mntpnt = NULL;
775f169c0eaSGlenn Lagasse 
776f169c0eaSGlenn Lagasse 	while (fgets(line, BUFSIZ, menu_fp)) {
777f169c0eaSGlenn Lagasse 		char *tok = NULL;
778f169c0eaSGlenn Lagasse 
779f169c0eaSGlenn Lagasse 		(void) strlcpy(temp_line, line, BUFSIZ);
780f169c0eaSGlenn Lagasse 		tok = strtok(line, BE_WHITE_SPACE);
781f169c0eaSGlenn Lagasse 
782f169c0eaSGlenn Lagasse 		if (tok == NULL || tok[0] == '#') {
783f169c0eaSGlenn Lagasse 			continue;
784f169c0eaSGlenn Lagasse 		} else if (strcmp(tok, "title") == 0) {
785f169c0eaSGlenn Lagasse 			collect_lines = B_FALSE;
786f169c0eaSGlenn Lagasse 			if ((tok = strtok(NULL, "\n")) == NULL)
787f169c0eaSGlenn Lagasse 				(void) strlcpy(title, "", sizeof (title));
788f169c0eaSGlenn Lagasse 			else
789f169c0eaSGlenn Lagasse 				(void) strlcpy(title, tok, sizeof (title));
790f169c0eaSGlenn Lagasse 			found_title = B_TRUE;
791f169c0eaSGlenn Lagasse 
792f169c0eaSGlenn Lagasse 			if (num_tmp_lines != 0) {
793f169c0eaSGlenn Lagasse 				for (i = 0; i < num_tmp_lines; i++) {
794f169c0eaSGlenn Lagasse 					free(tmp_entries[i]);
795f169c0eaSGlenn Lagasse 					tmp_entries[i] = NULL;
796f169c0eaSGlenn Lagasse 				}
797f169c0eaSGlenn Lagasse 				num_tmp_lines = 0;
798f169c0eaSGlenn Lagasse 			}
799f169c0eaSGlenn Lagasse 		} else if (strcmp(tok, "bootfs") == 0) {
800f169c0eaSGlenn Lagasse 			char *bootfs = strtok(NULL, BE_WHITE_SPACE);
801f169c0eaSGlenn Lagasse 			found_title = B_FALSE;
802f169c0eaSGlenn Lagasse 			if (bootfs == NULL)
803f169c0eaSGlenn Lagasse 				continue;
804f169c0eaSGlenn Lagasse 
805f169c0eaSGlenn Lagasse 			if (strcmp(bootfs, be_root_ds) == 0) {
806f169c0eaSGlenn Lagasse 				found_be = B_TRUE;
807f169c0eaSGlenn Lagasse 				break;
808f169c0eaSGlenn Lagasse 			}
809f169c0eaSGlenn Lagasse 
810f169c0eaSGlenn Lagasse 			if (be_orig_root_ds != NULL &&
811f169c0eaSGlenn Lagasse 			    strcmp(bootfs, be_orig_root_ds) == 0 &&
812f169c0eaSGlenn Lagasse 			    !found_orig_be) {
813f169c0eaSGlenn Lagasse 				char str[BUFSIZ];
814f169c0eaSGlenn Lagasse 				found_orig_be = B_TRUE;
815f169c0eaSGlenn Lagasse 				num_lines = 0;
816f169c0eaSGlenn Lagasse 				/*
817f169c0eaSGlenn Lagasse 				 * Store the new title line
818f169c0eaSGlenn Lagasse 				 */
819f169c0eaSGlenn Lagasse 				(void) snprintf(str, BUFSIZ, "title %s\n",
820f169c0eaSGlenn Lagasse 				    description ? description : be_name);
821f169c0eaSGlenn Lagasse 				entries[num_lines] = strdup(str);
822f169c0eaSGlenn Lagasse 				num_lines++;
823f169c0eaSGlenn Lagasse 				/*
824f169c0eaSGlenn Lagasse 				 * If there are any lines between the title
825f169c0eaSGlenn Lagasse 				 * and the bootfs line store these. Also
826f169c0eaSGlenn Lagasse 				 * free the temporary lines.
827f169c0eaSGlenn Lagasse 				 */
828f169c0eaSGlenn Lagasse 				for (i = 0; i < num_tmp_lines; i++) {
829f169c0eaSGlenn Lagasse 					entries[num_lines] = tmp_entries[i];
830f169c0eaSGlenn Lagasse 					tmp_entries[i] = NULL;
831f169c0eaSGlenn Lagasse 					num_lines++;
832f169c0eaSGlenn Lagasse 				}
833f169c0eaSGlenn Lagasse 				num_tmp_lines = 0;
834f169c0eaSGlenn Lagasse 				/*
835f169c0eaSGlenn Lagasse 				 * Store the new bootfs line.
836f169c0eaSGlenn Lagasse 				 */
837f169c0eaSGlenn Lagasse 				(void) snprintf(str, BUFSIZ, "bootfs %s\n",
838f169c0eaSGlenn Lagasse 				    be_root_ds);
839f169c0eaSGlenn Lagasse 				entries[num_lines] = strdup(str);
840f169c0eaSGlenn Lagasse 				num_lines++;
841f169c0eaSGlenn Lagasse 				collect_lines = B_TRUE;
842f169c0eaSGlenn Lagasse 			}
843f169c0eaSGlenn Lagasse 		} else if (found_orig_be && collect_lines) {
844f169c0eaSGlenn Lagasse 			/*
845f169c0eaSGlenn Lagasse 			 * get the rest of the lines for the original BE and
846f169c0eaSGlenn Lagasse 			 * store them.
847f169c0eaSGlenn Lagasse 			 */
848f169c0eaSGlenn Lagasse 			if (strstr(line, BE_GRUB_COMMENT) != NULL ||
849f169c0eaSGlenn Lagasse 			    strstr(line, "BOOTADM") != NULL)
850f169c0eaSGlenn Lagasse 				continue;
851e77c795bSGarrett D'Amore 			if (strcmp(tok, "splashimage") == 0) {
852e77c795bSGarrett D'Amore 				entries[num_lines] =
853e77c795bSGarrett D'Amore 				    strdup("splashimage "
854e77c795bSGarrett D'Amore 				    "/boot/splashimage.xpm\n");
855e77c795bSGarrett D'Amore 			} else {
856e77c795bSGarrett D'Amore 				entries[num_lines] = strdup(temp_line);
857e77c795bSGarrett D'Amore 			}
858f169c0eaSGlenn Lagasse 			num_lines++;
859f169c0eaSGlenn Lagasse 		} else if (found_title && !found_orig_be) {
860f169c0eaSGlenn Lagasse 			tmp_entries[num_tmp_lines] = strdup(temp_line);
861f169c0eaSGlenn Lagasse 			num_tmp_lines++;
862f169c0eaSGlenn Lagasse 		}
863f169c0eaSGlenn Lagasse 	}
864f169c0eaSGlenn Lagasse 
865f169c0eaSGlenn Lagasse 	(void) fclose(menu_fp);
866f169c0eaSGlenn Lagasse 
867f169c0eaSGlenn Lagasse 	if (found_be) {
868f169c0eaSGlenn Lagasse 		/*
869f169c0eaSGlenn Lagasse 		 * If an entry for this BE was already in the menu, then if
870f169c0eaSGlenn Lagasse 		 * that entry's title matches what we would have put in
871f169c0eaSGlenn Lagasse 		 * return success.  Otherwise return failure.
872f169c0eaSGlenn Lagasse 		 */
873f169c0eaSGlenn Lagasse 		char *new_title = description ? description : be_name;
874f169c0eaSGlenn Lagasse 
875f169c0eaSGlenn Lagasse 		if (strcmp(title, new_title) == 0) {
876f169c0eaSGlenn Lagasse 			ret = BE_SUCCESS;
877f169c0eaSGlenn Lagasse 			goto cleanup;
878f169c0eaSGlenn Lagasse 		} else {
879f169c0eaSGlenn Lagasse 			if (be_remove_menu(be_name, be_root_pool,
880f169c0eaSGlenn Lagasse 			    boot_pool) != BE_SUCCESS) {
881f169c0eaSGlenn Lagasse 				be_print_err(gettext("be_append_menu: "
882f169c0eaSGlenn Lagasse 				    "Failed to remove existing unusable "
883f169c0eaSGlenn Lagasse 				    "entry '%s' in boot menu.\n"), be_name);
884f169c0eaSGlenn Lagasse 				ret = BE_ERR_BE_EXISTS;
885f169c0eaSGlenn Lagasse 				goto cleanup;
886f169c0eaSGlenn Lagasse 			}
887f169c0eaSGlenn Lagasse 		}
888f169c0eaSGlenn Lagasse 	}
889f169c0eaSGlenn Lagasse 
890f169c0eaSGlenn Lagasse 	/* Append BE entry to the end of the file */
891f169c0eaSGlenn Lagasse 	menu_fp = fopen(menu_file, "a+");
892f169c0eaSGlenn Lagasse 	err = errno;
893f169c0eaSGlenn Lagasse 	if (menu_fp == NULL) {
894f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_append_menu: failed "
895f169c0eaSGlenn Lagasse 		    "to open menu.lst file %s\n"), menu_file);
896f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
897f169c0eaSGlenn Lagasse 		goto cleanup;
898f169c0eaSGlenn Lagasse 	}
899f169c0eaSGlenn Lagasse 
900f169c0eaSGlenn Lagasse 	if (found_orig_be) {
901f169c0eaSGlenn Lagasse 		/*
902f169c0eaSGlenn Lagasse 		 * write out all the stored lines
903f169c0eaSGlenn Lagasse 		 */
904f169c0eaSGlenn Lagasse 		for (i = 0; i < num_lines; i++) {
905f169c0eaSGlenn Lagasse 			(void) fprintf(menu_fp, "%s", entries[i]);
906f169c0eaSGlenn Lagasse 			free(entries[i]);
907f169c0eaSGlenn Lagasse 		}
908f169c0eaSGlenn Lagasse 		num_lines = 0;
909f169c0eaSGlenn Lagasse 
910f169c0eaSGlenn Lagasse 		/*
911f169c0eaSGlenn Lagasse 		 * Check to see if this system supports grub
912f169c0eaSGlenn Lagasse 		 */
913f169c0eaSGlenn Lagasse 		if (be_has_grub())
914f169c0eaSGlenn Lagasse 			(void) fprintf(menu_fp, "%s\n", BE_GRUB_COMMENT);
915f169c0eaSGlenn Lagasse 		ret = BE_SUCCESS;
916f169c0eaSGlenn Lagasse 	} else {
917f169c0eaSGlenn Lagasse 		(void) fprintf(menu_fp, "title %s\n",
918f169c0eaSGlenn Lagasse 		    description ? description : be_name);
919f169c0eaSGlenn Lagasse 		(void) fprintf(menu_fp, "bootfs %s\n", be_root_ds);
920f169c0eaSGlenn Lagasse 
921f169c0eaSGlenn Lagasse 		/*
922f169c0eaSGlenn Lagasse 		 * Check to see if this system supports grub
923f169c0eaSGlenn Lagasse 		 */
924f169c0eaSGlenn Lagasse 		if (be_has_grub()) {
925f169c0eaSGlenn Lagasse 			(void) fprintf(menu_fp, "kernel$ "
926f169c0eaSGlenn Lagasse 			    "/platform/i86pc/kernel/$ISADIR/unix -B "
927f169c0eaSGlenn Lagasse 			    "$ZFS-BOOTFS\n");
928f169c0eaSGlenn Lagasse 			(void) fprintf(menu_fp, "module$ "
929f169c0eaSGlenn Lagasse 			    "/platform/i86pc/$ISADIR/boot_archive\n");
930f169c0eaSGlenn Lagasse 			(void) fprintf(menu_fp, "%s\n", BE_GRUB_COMMENT);
931f169c0eaSGlenn Lagasse 		}
932f169c0eaSGlenn Lagasse 		ret = BE_SUCCESS;
933f169c0eaSGlenn Lagasse 	}
934f169c0eaSGlenn Lagasse 	(void) fclose(menu_fp);
935f169c0eaSGlenn Lagasse cleanup:
936f169c0eaSGlenn Lagasse 	if (pool_mounted) {
937f169c0eaSGlenn Lagasse 		int err = BE_SUCCESS;
938f169c0eaSGlenn Lagasse 		err = be_unmount_pool(zhp, ptmp_mntpnt, orig_mntpnt);
939f169c0eaSGlenn Lagasse 		if (ret == BE_SUCCESS)
940f169c0eaSGlenn Lagasse 			ret = err;
941f169c0eaSGlenn Lagasse 		free(orig_mntpnt);
942f169c0eaSGlenn Lagasse 		free(ptmp_mntpnt);
943f169c0eaSGlenn Lagasse 	}
944f169c0eaSGlenn Lagasse 	ZFS_CLOSE(zhp);
945f169c0eaSGlenn Lagasse 	if (num_tmp_lines > 0) {
946f169c0eaSGlenn Lagasse 		for (i = 0; i < num_tmp_lines; i++) {
947f169c0eaSGlenn Lagasse 			free(tmp_entries[i]);
948f169c0eaSGlenn Lagasse 			tmp_entries[i] = NULL;
949f169c0eaSGlenn Lagasse 		}
950f169c0eaSGlenn Lagasse 	}
951f169c0eaSGlenn Lagasse 	if (num_lines > 0) {
952f169c0eaSGlenn Lagasse 		for (i = 0; i < num_lines; i++) {
953f169c0eaSGlenn Lagasse 			free(entries[i]);
954f169c0eaSGlenn Lagasse 			entries[i] = NULL;
955f169c0eaSGlenn Lagasse 		}
956f169c0eaSGlenn Lagasse 	}
957f169c0eaSGlenn Lagasse 	return (ret);
958f169c0eaSGlenn Lagasse }
959f169c0eaSGlenn Lagasse 
960f169c0eaSGlenn Lagasse /*
961f169c0eaSGlenn Lagasse  * Function:	be_remove_menu
962f169c0eaSGlenn Lagasse  * Description:	Removes a BE's entry from a menu.lst file.
963f169c0eaSGlenn Lagasse  * Parameters:
964f169c0eaSGlenn Lagasse  *		be_name - the name of BE whose entry is to be removed from
965f169c0eaSGlenn Lagasse  *			the menu.lst file.
966f169c0eaSGlenn Lagasse  *		be_root_pool - the pool that be_name lives in.
967f169c0eaSGlenn Lagasse  *		boot_pool - the pool where the BE is, if different than
968f169c0eaSGlenn Lagasse  *			the pool containing the boot menu.  If this is
969f169c0eaSGlenn Lagasse  *			NULL it will be set to be_root_pool.
970f169c0eaSGlenn Lagasse  * Returns:
971f169c0eaSGlenn Lagasse  *		BE_SUCCESS - Success
972f169c0eaSGlenn Lagasse  *		be_errno_t - Failure
973f169c0eaSGlenn Lagasse  * Scope:
974f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
975f169c0eaSGlenn Lagasse  */
976f169c0eaSGlenn Lagasse int
be_remove_menu(char * be_name,char * be_root_pool,char * boot_pool)977f169c0eaSGlenn Lagasse be_remove_menu(char *be_name, char *be_root_pool, char *boot_pool)
978f169c0eaSGlenn Lagasse {
979f169c0eaSGlenn Lagasse 	zfs_handle_t	*zhp = NULL;
980f169c0eaSGlenn Lagasse 	char		be_root_ds[MAXPATHLEN];
981f169c0eaSGlenn Lagasse 	char		**buffer = NULL;
982f169c0eaSGlenn Lagasse 	char		menu_buf[BUFSIZ];
983f169c0eaSGlenn Lagasse 	char		menu[MAXPATHLEN];
984f169c0eaSGlenn Lagasse 	char		*pool_mntpnt = NULL;
985f169c0eaSGlenn Lagasse 	char		*ptmp_mntpnt = NULL;
986f169c0eaSGlenn Lagasse 	char		*orig_mntpnt = NULL;
987f169c0eaSGlenn Lagasse 	char		*tmp_menu = NULL;
988f169c0eaSGlenn Lagasse 	FILE		*menu_fp = NULL;
989f169c0eaSGlenn Lagasse 	FILE		*tmp_menu_fp = NULL;
990f169c0eaSGlenn Lagasse 	struct stat	sb;
991f169c0eaSGlenn Lagasse 	int		ret = BE_SUCCESS;
992f169c0eaSGlenn Lagasse 	int		i;
993f169c0eaSGlenn Lagasse 	int		fd;
994f169c0eaSGlenn Lagasse 	int		err = 0;
995f169c0eaSGlenn Lagasse 	int		nlines = 0;
996f169c0eaSGlenn Lagasse 	int		default_entry = 0;
997f169c0eaSGlenn Lagasse 	int		entry_cnt = 0;
998f169c0eaSGlenn Lagasse 	int		entry_del = 0;
999f169c0eaSGlenn Lagasse 	int		num_entry_del = 0;
1000f169c0eaSGlenn Lagasse 	int		tmp_menu_len = 0;
1001f169c0eaSGlenn Lagasse 	boolean_t	write = B_TRUE;
1002f169c0eaSGlenn Lagasse 	boolean_t	do_buffer = B_FALSE;
1003f169c0eaSGlenn Lagasse 	boolean_t	pool_mounted = B_FALSE;
1004f169c0eaSGlenn Lagasse 
1005f169c0eaSGlenn Lagasse 	if (boot_pool == NULL)
1006f169c0eaSGlenn Lagasse 		boot_pool = be_root_pool;
1007f169c0eaSGlenn Lagasse 
1008f169c0eaSGlenn Lagasse 	/* Get name of BE's root dataset */
1009*ec8422d0SAndy Fiddaman 	if ((ret = be_make_root_ds(be_root_pool, be_name, be_root_ds,
1010*ec8422d0SAndy Fiddaman 	    sizeof (be_root_ds))) != BE_SUCCESS) {
1011*ec8422d0SAndy Fiddaman 		be_print_err(gettext("%s: failed to get BE container dataset "
1012*ec8422d0SAndy Fiddaman 		    "for %s/%s\n"), __func__, be_root_pool, be_name);
1013*ec8422d0SAndy Fiddaman 		return (ret);
1014*ec8422d0SAndy Fiddaman 	}
1015f169c0eaSGlenn Lagasse 
1016f169c0eaSGlenn Lagasse 	/* Get handle to pool dataset */
1017f169c0eaSGlenn Lagasse 	if ((zhp = zfs_open(g_zfs, be_root_pool, ZFS_TYPE_DATASET)) == NULL) {
1018f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_remove_menu: "
1019f169c0eaSGlenn Lagasse 		    "failed to open pool dataset for %s: %s"),
1020f169c0eaSGlenn Lagasse 		    be_root_pool, libzfs_error_description(g_zfs));
1021f169c0eaSGlenn Lagasse 		return (zfs_err_to_be_err(g_zfs));
1022f169c0eaSGlenn Lagasse 	}
1023f169c0eaSGlenn Lagasse 
1024f169c0eaSGlenn Lagasse 	/*
1025f169c0eaSGlenn Lagasse 	 * Check to see if the pool's dataset is mounted. If it isn't we'll
1026f169c0eaSGlenn Lagasse 	 * attempt to mount it.
1027f169c0eaSGlenn Lagasse 	 */
1028f169c0eaSGlenn Lagasse 	if ((ret = be_mount_pool(zhp, &ptmp_mntpnt, &orig_mntpnt,
1029f169c0eaSGlenn Lagasse 	    &pool_mounted)) != BE_SUCCESS) {
1030f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_remove_menu: pool dataset "
1031f169c0eaSGlenn Lagasse 		    "(%s) could not be mounted\n"), be_root_pool);
1032f169c0eaSGlenn Lagasse 		ZFS_CLOSE(zhp);
1033f169c0eaSGlenn Lagasse 		return (ret);
1034f169c0eaSGlenn Lagasse 	}
1035f169c0eaSGlenn Lagasse 
1036f169c0eaSGlenn Lagasse 	/*
1037f169c0eaSGlenn Lagasse 	 * Get the mountpoint for the root pool dataset.
1038f169c0eaSGlenn Lagasse 	 */
1039f169c0eaSGlenn Lagasse 	if (!zfs_is_mounted(zhp, &pool_mntpnt)) {
1040f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_remove_menu: pool "
1041f169c0eaSGlenn Lagasse 		    "dataset (%s) is not mounted. Can't set "
1042f169c0eaSGlenn Lagasse 		    "the default BE in the grub menu.\n"), be_root_pool);
1043f169c0eaSGlenn Lagasse 		ret = BE_ERR_NO_MENU;
1044f169c0eaSGlenn Lagasse 		goto cleanup;
1045f169c0eaSGlenn Lagasse 	}
1046f169c0eaSGlenn Lagasse 
1047f169c0eaSGlenn Lagasse 	/* Get path to boot menu */
1048f169c0eaSGlenn Lagasse 	(void) strlcpy(menu, pool_mntpnt, sizeof (menu));
1049f169c0eaSGlenn Lagasse 
1050f169c0eaSGlenn Lagasse 	/*
1051f169c0eaSGlenn Lagasse 	 * Check to see if this system supports grub
1052f169c0eaSGlenn Lagasse 	 */
1053f169c0eaSGlenn Lagasse 	if (be_has_grub())
1054f169c0eaSGlenn Lagasse 		(void) strlcat(menu, BE_GRUB_MENU, sizeof (menu));
1055f169c0eaSGlenn Lagasse 	else
1056f169c0eaSGlenn Lagasse 		(void) strlcat(menu, BE_SPARC_MENU, sizeof (menu));
1057f169c0eaSGlenn Lagasse 
1058f169c0eaSGlenn Lagasse 	/* Get handle to boot menu file */
1059de1ab35cSAlexander Eremin 	if ((ret = be_open_menu(be_root_pool, menu, &menu_fp, "r",
1060f169c0eaSGlenn Lagasse 	    B_TRUE)) != BE_SUCCESS) {
1061f169c0eaSGlenn Lagasse 		goto cleanup;
1062f169c0eaSGlenn Lagasse 	} else if (menu_fp == NULL) {
1063f169c0eaSGlenn Lagasse 		ret = BE_ERR_NO_MENU;
1064f169c0eaSGlenn Lagasse 		goto cleanup;
1065f169c0eaSGlenn Lagasse 	}
1066f169c0eaSGlenn Lagasse 
1067f169c0eaSGlenn Lagasse 	free(pool_mntpnt);
1068f169c0eaSGlenn Lagasse 	pool_mntpnt = NULL;
1069f169c0eaSGlenn Lagasse 
1070f169c0eaSGlenn Lagasse 	/* Grab the stats of the original menu file */
1071f169c0eaSGlenn Lagasse 	if (stat(menu, &sb) != 0) {
1072f169c0eaSGlenn Lagasse 		err = errno;
1073f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_remove_menu: "
1074f169c0eaSGlenn Lagasse 		    "failed to stat file %s: %s\n"), menu, strerror(err));
1075f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
1076f169c0eaSGlenn Lagasse 		goto cleanup;
1077f169c0eaSGlenn Lagasse 	}
1078f169c0eaSGlenn Lagasse 
1079f169c0eaSGlenn Lagasse 	/* Create a tmp file for the modified menu.lst */
1080f169c0eaSGlenn Lagasse 	tmp_menu_len = strlen(menu) + 7;
1081f169c0eaSGlenn Lagasse 	if ((tmp_menu = (char *)malloc(tmp_menu_len)) == NULL) {
1082f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_remove_menu: malloc failed\n"));
1083f169c0eaSGlenn Lagasse 		ret = BE_ERR_NOMEM;
1084f169c0eaSGlenn Lagasse 		goto cleanup;
1085f169c0eaSGlenn Lagasse 	}
1086f169c0eaSGlenn Lagasse 	(void) memset(tmp_menu, 0, tmp_menu_len);
1087f169c0eaSGlenn Lagasse 	(void) strlcpy(tmp_menu, menu, tmp_menu_len);
1088f169c0eaSGlenn Lagasse 	(void) strlcat(tmp_menu, "XXXXXX", tmp_menu_len);
1089f169c0eaSGlenn Lagasse 	if ((fd = mkstemp(tmp_menu)) == -1) {
1090f169c0eaSGlenn Lagasse 		err = errno;
1091f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_remove_menu: mkstemp failed\n"));
1092f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
1093f169c0eaSGlenn Lagasse 		free(tmp_menu);
1094f169c0eaSGlenn Lagasse 		tmp_menu = NULL;
1095f169c0eaSGlenn Lagasse 		goto cleanup;
1096f169c0eaSGlenn Lagasse 	}
1097f169c0eaSGlenn Lagasse 	if ((tmp_menu_fp = fdopen(fd, "w")) == NULL) {
1098f169c0eaSGlenn Lagasse 		err = errno;
1099f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_remove_menu: "
1100f169c0eaSGlenn Lagasse 		    "could not open tmp file for write: %s\n"), strerror(err));
1101f169c0eaSGlenn Lagasse 		(void) close(fd);
1102f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
1103f169c0eaSGlenn Lagasse 		goto cleanup;
1104f169c0eaSGlenn Lagasse 	}
1105f169c0eaSGlenn Lagasse 
1106f169c0eaSGlenn Lagasse 	while (fgets(menu_buf, BUFSIZ, menu_fp)) {
1107f169c0eaSGlenn Lagasse 		char tline [BUFSIZ];
1108f169c0eaSGlenn Lagasse 		char *tok = NULL;
1109f169c0eaSGlenn Lagasse 
1110f169c0eaSGlenn Lagasse 		(void) strlcpy(tline, menu_buf, sizeof (tline));
1111f169c0eaSGlenn Lagasse 
1112f169c0eaSGlenn Lagasse 		/* Tokenize line */
1113f169c0eaSGlenn Lagasse 		tok = strtok(tline, BE_WHITE_SPACE);
1114f169c0eaSGlenn Lagasse 
1115f169c0eaSGlenn Lagasse 		if (tok == NULL || tok[0] == '#') {
1116f169c0eaSGlenn Lagasse 			/* Found empty line or comment line */
1117f169c0eaSGlenn Lagasse 			if (do_buffer) {
1118f169c0eaSGlenn Lagasse 				/* Buffer this line */
1119f169c0eaSGlenn Lagasse 				if ((buffer = (char **)realloc(buffer,
1120f169c0eaSGlenn Lagasse 				    sizeof (char *)*(nlines + 1))) == NULL) {
1121f169c0eaSGlenn Lagasse 					ret = BE_ERR_NOMEM;
1122f169c0eaSGlenn Lagasse 					goto cleanup;
1123f169c0eaSGlenn Lagasse 				}
1124f169c0eaSGlenn Lagasse 				if ((buffer[nlines++] = strdup(menu_buf))
1125f169c0eaSGlenn Lagasse 				    == NULL) {
1126f169c0eaSGlenn Lagasse 					ret = BE_ERR_NOMEM;
1127f169c0eaSGlenn Lagasse 					goto cleanup;
1128f169c0eaSGlenn Lagasse 				}
1129f169c0eaSGlenn Lagasse 
1130f169c0eaSGlenn Lagasse 			} else if (write || strncmp(menu_buf, BE_GRUB_COMMENT,
1131f169c0eaSGlenn Lagasse 			    strlen(BE_GRUB_COMMENT)) != 0) {
1132f169c0eaSGlenn Lagasse 				/* Write this line out */
1133f169c0eaSGlenn Lagasse 				(void) fputs(menu_buf, tmp_menu_fp);
1134f169c0eaSGlenn Lagasse 			}
1135f169c0eaSGlenn Lagasse 		} else if (strcmp(tok, "default") == 0) {
1136f169c0eaSGlenn Lagasse 			/*
1137f169c0eaSGlenn Lagasse 			 * Record what 'default' is set to because we might
1138f169c0eaSGlenn Lagasse 			 * need to adjust this upon deleting an entry.
1139f169c0eaSGlenn Lagasse 			 */
1140f169c0eaSGlenn Lagasse 			tok = strtok(NULL, BE_WHITE_SPACE);
1141f169c0eaSGlenn Lagasse 
1142f169c0eaSGlenn Lagasse 			if (tok != NULL) {
1143f169c0eaSGlenn Lagasse 				default_entry = atoi(tok);
1144f169c0eaSGlenn Lagasse 			}
1145f169c0eaSGlenn Lagasse 
1146f169c0eaSGlenn Lagasse 			(void) fputs(menu_buf, tmp_menu_fp);
1147f169c0eaSGlenn Lagasse 		} else if (strcmp(tok, "title") == 0) {
1148f169c0eaSGlenn Lagasse 			/*
1149f169c0eaSGlenn Lagasse 			 * If we've reached a 'title' line and do_buffer is
1150f169c0eaSGlenn Lagasse 			 * is true, that means we've just buffered an entire
1151f169c0eaSGlenn Lagasse 			 * entry without finding a 'bootfs' directive.  We
1152f169c0eaSGlenn Lagasse 			 * need to write that entry out and keep searching.
1153f169c0eaSGlenn Lagasse 			 */
1154f169c0eaSGlenn Lagasse 			if (do_buffer) {
1155f169c0eaSGlenn Lagasse 				for (i = 0; i < nlines; i++) {
1156f169c0eaSGlenn Lagasse 					(void) fputs(buffer[i], tmp_menu_fp);
1157f169c0eaSGlenn Lagasse 					free(buffer[i]);
1158f169c0eaSGlenn Lagasse 				}
1159f169c0eaSGlenn Lagasse 				free(buffer);
1160f169c0eaSGlenn Lagasse 				buffer = NULL;
1161f169c0eaSGlenn Lagasse 				nlines = 0;
1162f169c0eaSGlenn Lagasse 			}
1163f169c0eaSGlenn Lagasse 
1164f169c0eaSGlenn Lagasse 			/*
1165f169c0eaSGlenn Lagasse 			 * Turn writing off and buffering on, and increment
1166f169c0eaSGlenn Lagasse 			 * our entry counter.
1167f169c0eaSGlenn Lagasse 			 */
1168f169c0eaSGlenn Lagasse 			write = B_FALSE;
1169f169c0eaSGlenn Lagasse 			do_buffer = B_TRUE;
1170f169c0eaSGlenn Lagasse 			entry_cnt++;
1171f169c0eaSGlenn Lagasse 
1172f169c0eaSGlenn Lagasse 			/* Buffer this 'title' line */
1173f169c0eaSGlenn Lagasse 			if ((buffer = (char **)realloc(buffer,
1174f169c0eaSGlenn Lagasse 			    sizeof (char *)*(nlines + 1))) == NULL) {
1175f169c0eaSGlenn Lagasse 				ret = BE_ERR_NOMEM;
1176f169c0eaSGlenn Lagasse 				goto cleanup;
1177f169c0eaSGlenn Lagasse 			}
1178f169c0eaSGlenn Lagasse 			if ((buffer[nlines++] = strdup(menu_buf)) == NULL) {
1179f169c0eaSGlenn Lagasse 				ret = BE_ERR_NOMEM;
1180f169c0eaSGlenn Lagasse 				goto cleanup;
1181f169c0eaSGlenn Lagasse 			}
1182f169c0eaSGlenn Lagasse 
1183f169c0eaSGlenn Lagasse 		} else if (strcmp(tok, "bootfs") == 0) {
1184f169c0eaSGlenn Lagasse 			char *bootfs = NULL;
1185f169c0eaSGlenn Lagasse 
1186f169c0eaSGlenn Lagasse 			/*
1187f169c0eaSGlenn Lagasse 			 * Found a 'bootfs' line.  See if it matches the
1188f169c0eaSGlenn Lagasse 			 * BE we're looking for.
1189f169c0eaSGlenn Lagasse 			 */
1190f169c0eaSGlenn Lagasse 			if ((bootfs = strtok(NULL, BE_WHITE_SPACE)) == NULL ||
1191f169c0eaSGlenn Lagasse 			    strcmp(bootfs, be_root_ds) != 0) {
1192f169c0eaSGlenn Lagasse 				/*
1193f169c0eaSGlenn Lagasse 				 * Either there's nothing after the 'bootfs'
1194f169c0eaSGlenn Lagasse 				 * or this is not the BE we're looking for,
1195f169c0eaSGlenn Lagasse 				 * write out the line(s) we've buffered since
1196f169c0eaSGlenn Lagasse 				 * finding the title.
1197f169c0eaSGlenn Lagasse 				 */
1198f169c0eaSGlenn Lagasse 				for (i = 0; i < nlines; i++) {
1199f169c0eaSGlenn Lagasse 					(void) fputs(buffer[i], tmp_menu_fp);
1200f169c0eaSGlenn Lagasse 					free(buffer[i]);
1201f169c0eaSGlenn Lagasse 				}
1202f169c0eaSGlenn Lagasse 				free(buffer);
1203f169c0eaSGlenn Lagasse 				buffer = NULL;
1204f169c0eaSGlenn Lagasse 				nlines = 0;
1205f169c0eaSGlenn Lagasse 
1206f169c0eaSGlenn Lagasse 				/*
1207f169c0eaSGlenn Lagasse 				 * Turn writing back on, and turn off buffering
1208f169c0eaSGlenn Lagasse 				 * since this isn't the entry we're looking
1209f169c0eaSGlenn Lagasse 				 * for.
1210f169c0eaSGlenn Lagasse 				 */
1211f169c0eaSGlenn Lagasse 				write = B_TRUE;
1212f169c0eaSGlenn Lagasse 				do_buffer = B_FALSE;
1213f169c0eaSGlenn Lagasse 
1214f169c0eaSGlenn Lagasse 				/* Write this 'bootfs' line out. */
1215f169c0eaSGlenn Lagasse 				(void) fputs(menu_buf, tmp_menu_fp);
1216f169c0eaSGlenn Lagasse 			} else {
1217f169c0eaSGlenn Lagasse 				/*
1218f169c0eaSGlenn Lagasse 				 * Found the entry we're looking for.
1219f169c0eaSGlenn Lagasse 				 * Record its entry number, increment the
1220f169c0eaSGlenn Lagasse 				 * number of entries we've deleted, and turn
1221f169c0eaSGlenn Lagasse 				 * writing off.  Also, throw away the lines
1222f169c0eaSGlenn Lagasse 				 * we've buffered for this entry so far, we
1223f169c0eaSGlenn Lagasse 				 * don't need them.
1224f169c0eaSGlenn Lagasse 				 */
1225f169c0eaSGlenn Lagasse 				entry_del = entry_cnt - 1;
1226f169c0eaSGlenn Lagasse 				num_entry_del++;
1227f169c0eaSGlenn Lagasse 				write = B_FALSE;
1228f169c0eaSGlenn Lagasse 				do_buffer = B_FALSE;
1229f169c0eaSGlenn Lagasse 
1230f169c0eaSGlenn Lagasse 				for (i = 0; i < nlines; i++) {
1231f169c0eaSGlenn Lagasse 					free(buffer[i]);
1232f169c0eaSGlenn Lagasse 				}
1233f169c0eaSGlenn Lagasse 				free(buffer);
1234f169c0eaSGlenn Lagasse 				buffer = NULL;
1235f169c0eaSGlenn Lagasse 				nlines = 0;
1236f169c0eaSGlenn Lagasse 			}
1237f169c0eaSGlenn Lagasse 		} else {
1238f169c0eaSGlenn Lagasse 			if (do_buffer) {
1239f169c0eaSGlenn Lagasse 				/* Buffer this line */
1240f169c0eaSGlenn Lagasse 				if ((buffer = (char **)realloc(buffer,
1241f169c0eaSGlenn Lagasse 				    sizeof (char *)*(nlines + 1))) == NULL) {
1242f169c0eaSGlenn Lagasse 					ret = BE_ERR_NOMEM;
1243f169c0eaSGlenn Lagasse 					goto cleanup;
1244f169c0eaSGlenn Lagasse 				}
1245f169c0eaSGlenn Lagasse 				if ((buffer[nlines++] = strdup(menu_buf))
1246f169c0eaSGlenn Lagasse 				    == NULL) {
1247f169c0eaSGlenn Lagasse 					ret = BE_ERR_NOMEM;
1248f169c0eaSGlenn Lagasse 					goto cleanup;
1249f169c0eaSGlenn Lagasse 				}
1250f169c0eaSGlenn Lagasse 			} else if (write) {
1251f169c0eaSGlenn Lagasse 				/* Write this line out */
1252f169c0eaSGlenn Lagasse 				(void) fputs(menu_buf, tmp_menu_fp);
1253f169c0eaSGlenn Lagasse 			}
1254f169c0eaSGlenn Lagasse 		}
1255f169c0eaSGlenn Lagasse 	}
1256f169c0eaSGlenn Lagasse 
1257f169c0eaSGlenn Lagasse 	(void) fclose(menu_fp);
1258f169c0eaSGlenn Lagasse 	menu_fp = NULL;
1259f169c0eaSGlenn Lagasse 	(void) fclose(tmp_menu_fp);
1260f169c0eaSGlenn Lagasse 	tmp_menu_fp = NULL;
1261f169c0eaSGlenn Lagasse 
1262f169c0eaSGlenn Lagasse 	/* Copy the modified menu.lst into place */
1263f169c0eaSGlenn Lagasse 	if (rename(tmp_menu, menu) != 0) {
1264f169c0eaSGlenn Lagasse 		err = errno;
1265f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_remove_menu: "
1266f169c0eaSGlenn Lagasse 		    "failed to rename file %s to %s: %s\n"),
1267f169c0eaSGlenn Lagasse 		    tmp_menu, menu, strerror(err));
1268f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
1269f169c0eaSGlenn Lagasse 		goto cleanup;
1270f169c0eaSGlenn Lagasse 	}
1271f169c0eaSGlenn Lagasse 	free(tmp_menu);
1272f169c0eaSGlenn Lagasse 	tmp_menu = NULL;
1273f169c0eaSGlenn Lagasse 
1274f169c0eaSGlenn Lagasse 	/*
1275f169c0eaSGlenn Lagasse 	 * If we've removed an entry, see if we need to
1276f169c0eaSGlenn Lagasse 	 * adjust the default value in the menu.lst.  If the
1277f169c0eaSGlenn Lagasse 	 * entry we've deleted comes before the default entry
1278f169c0eaSGlenn Lagasse 	 * we need to adjust the default value accordingly.
1279f169c0eaSGlenn Lagasse 	 *
1280f169c0eaSGlenn Lagasse 	 * be_has_grub is used here to check to see if this system
1281f169c0eaSGlenn Lagasse 	 * supports grub.
1282f169c0eaSGlenn Lagasse 	 */
1283f169c0eaSGlenn Lagasse 	if (be_has_grub() && num_entry_del > 0) {
1284f169c0eaSGlenn Lagasse 		if (entry_del <= default_entry) {
1285f169c0eaSGlenn Lagasse 			default_entry = default_entry - num_entry_del;
1286f169c0eaSGlenn Lagasse 			if (default_entry < 0)
1287f169c0eaSGlenn Lagasse 				default_entry = 0;
1288f169c0eaSGlenn Lagasse 
1289f169c0eaSGlenn Lagasse 			/*
1290f169c0eaSGlenn Lagasse 			 * Adjust the default value by rewriting the
1291f169c0eaSGlenn Lagasse 			 * menu.lst file.  This may be overkill, but to
1292f169c0eaSGlenn Lagasse 			 * preserve the location of the 'default' entry
1293f169c0eaSGlenn Lagasse 			 * in the file, we need to do this.
1294f169c0eaSGlenn Lagasse 			 */
1295f169c0eaSGlenn Lagasse 
1296f169c0eaSGlenn Lagasse 			/* Get handle to boot menu file */
1297f169c0eaSGlenn Lagasse 			if ((menu_fp = fopen(menu, "r")) == NULL) {
1298f169c0eaSGlenn Lagasse 				err = errno;
1299f169c0eaSGlenn Lagasse 				be_print_err(gettext("be_remove_menu: "
1300f169c0eaSGlenn Lagasse 				    "failed to open menu.lst (%s): %s\n"),
1301f169c0eaSGlenn Lagasse 				    menu, strerror(err));
1302f169c0eaSGlenn Lagasse 				ret = errno_to_be_err(err);
1303f169c0eaSGlenn Lagasse 				goto cleanup;
1304f169c0eaSGlenn Lagasse 			}
1305f169c0eaSGlenn Lagasse 
1306f169c0eaSGlenn Lagasse 			/* Create a tmp file for the modified menu.lst */
1307f169c0eaSGlenn Lagasse 			tmp_menu_len = strlen(menu) + 7;
1308f169c0eaSGlenn Lagasse 			if ((tmp_menu = (char *)malloc(tmp_menu_len))
1309f169c0eaSGlenn Lagasse 			    == NULL) {
1310f169c0eaSGlenn Lagasse 				be_print_err(gettext("be_remove_menu: "
1311f169c0eaSGlenn Lagasse 				    "malloc failed\n"));
1312f169c0eaSGlenn Lagasse 				ret = BE_ERR_NOMEM;
1313f169c0eaSGlenn Lagasse 				goto cleanup;
1314f169c0eaSGlenn Lagasse 			}
1315f169c0eaSGlenn Lagasse 			(void) memset(tmp_menu, 0, tmp_menu_len);
1316f169c0eaSGlenn Lagasse 			(void) strlcpy(tmp_menu, menu, tmp_menu_len);
1317f169c0eaSGlenn Lagasse 			(void) strlcat(tmp_menu, "XXXXXX", tmp_menu_len);
1318f169c0eaSGlenn Lagasse 			if ((fd = mkstemp(tmp_menu)) == -1) {
1319f169c0eaSGlenn Lagasse 				err = errno;
1320f169c0eaSGlenn Lagasse 				be_print_err(gettext("be_remove_menu: "
1321f169c0eaSGlenn Lagasse 				    "mkstemp failed: %s\n"), strerror(err));
1322f169c0eaSGlenn Lagasse 				ret = errno_to_be_err(err);
1323f169c0eaSGlenn Lagasse 				free(tmp_menu);
1324f169c0eaSGlenn Lagasse 				tmp_menu = NULL;
1325f169c0eaSGlenn Lagasse 				goto cleanup;
1326f169c0eaSGlenn Lagasse 			}
1327f169c0eaSGlenn Lagasse 			if ((tmp_menu_fp = fdopen(fd, "w")) == NULL) {
1328f169c0eaSGlenn Lagasse 				err = errno;
1329f169c0eaSGlenn Lagasse 				be_print_err(gettext("be_remove_menu: "
1330f169c0eaSGlenn Lagasse 				    "could not open tmp file for write: %s\n"),
1331f169c0eaSGlenn Lagasse 				    strerror(err));
1332f169c0eaSGlenn Lagasse 				(void) close(fd);
1333f169c0eaSGlenn Lagasse 				ret = errno_to_be_err(err);
1334f169c0eaSGlenn Lagasse 				goto cleanup;
1335f169c0eaSGlenn Lagasse 			}
1336f169c0eaSGlenn Lagasse 
1337f169c0eaSGlenn Lagasse 			while (fgets(menu_buf, BUFSIZ, menu_fp)) {
1338f169c0eaSGlenn Lagasse 				char tline [BUFSIZ];
1339f169c0eaSGlenn Lagasse 				char *tok = NULL;
1340f169c0eaSGlenn Lagasse 
1341f169c0eaSGlenn Lagasse 				(void) strlcpy(tline, menu_buf, sizeof (tline));
1342f169c0eaSGlenn Lagasse 
1343f169c0eaSGlenn Lagasse 				/* Tokenize line */
1344f169c0eaSGlenn Lagasse 				tok = strtok(tline, BE_WHITE_SPACE);
1345f169c0eaSGlenn Lagasse 
1346f169c0eaSGlenn Lagasse 				if (tok == NULL) {
1347f169c0eaSGlenn Lagasse 					/* Found empty line, write it out */
1348f169c0eaSGlenn Lagasse 					(void) fputs(menu_buf, tmp_menu_fp);
1349f169c0eaSGlenn Lagasse 				} else if (strcmp(tok, "default") == 0) {
1350f169c0eaSGlenn Lagasse 					/* Found the default line, adjust it */
1351f169c0eaSGlenn Lagasse 					(void) snprintf(tline, sizeof (tline),
1352f169c0eaSGlenn Lagasse 					    "default %d\n", default_entry);
1353f169c0eaSGlenn Lagasse 
1354f169c0eaSGlenn Lagasse 					(void) fputs(tline, tmp_menu_fp);
1355f169c0eaSGlenn Lagasse 				} else {
1356f169c0eaSGlenn Lagasse 					/* Pass through all other lines */
1357f169c0eaSGlenn Lagasse 					(void) fputs(menu_buf, tmp_menu_fp);
1358f169c0eaSGlenn Lagasse 				}
1359f169c0eaSGlenn Lagasse 			}
1360f169c0eaSGlenn Lagasse 
1361f169c0eaSGlenn Lagasse 			(void) fclose(menu_fp);
1362f169c0eaSGlenn Lagasse 			menu_fp = NULL;
1363f169c0eaSGlenn Lagasse 			(void) fclose(tmp_menu_fp);
1364f169c0eaSGlenn Lagasse 			tmp_menu_fp = NULL;
1365f169c0eaSGlenn Lagasse 
1366f169c0eaSGlenn Lagasse 			/* Copy the modified menu.lst into place */
1367f169c0eaSGlenn Lagasse 			if (rename(tmp_menu, menu) != 0) {
1368f169c0eaSGlenn Lagasse 				err = errno;
1369f169c0eaSGlenn Lagasse 				be_print_err(gettext("be_remove_menu: "
1370f169c0eaSGlenn Lagasse 				    "failed to rename file %s to %s: %s\n"),
1371f169c0eaSGlenn Lagasse 				    tmp_menu, menu, strerror(err));
1372f169c0eaSGlenn Lagasse 				ret = errno_to_be_err(err);
1373f169c0eaSGlenn Lagasse 				goto cleanup;
1374f169c0eaSGlenn Lagasse 			}
1375f169c0eaSGlenn Lagasse 
1376f169c0eaSGlenn Lagasse 			free(tmp_menu);
1377f169c0eaSGlenn Lagasse 			tmp_menu = NULL;
1378f169c0eaSGlenn Lagasse 		}
1379f169c0eaSGlenn Lagasse 	}
1380f169c0eaSGlenn Lagasse 
1381f169c0eaSGlenn Lagasse 	/* Set the perms and ownership of the updated file */
1382f169c0eaSGlenn Lagasse 	if (chmod(menu, sb.st_mode) != 0) {
1383f169c0eaSGlenn Lagasse 		err = errno;
1384f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_remove_menu: "
1385f169c0eaSGlenn Lagasse 		    "failed to chmod %s: %s\n"), menu, strerror(err));
1386f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
1387f169c0eaSGlenn Lagasse 		goto cleanup;
1388f169c0eaSGlenn Lagasse 	}
1389f169c0eaSGlenn Lagasse 	if (chown(menu, sb.st_uid, sb.st_gid) != 0) {
1390f169c0eaSGlenn Lagasse 		err = errno;
1391f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_remove_menu: "
1392f169c0eaSGlenn Lagasse 		    "failed to chown %s: %s\n"), menu, strerror(err));
1393f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
1394f169c0eaSGlenn Lagasse 		goto cleanup;
1395f169c0eaSGlenn Lagasse 	}
1396f169c0eaSGlenn Lagasse 
1397f169c0eaSGlenn Lagasse cleanup:
1398f169c0eaSGlenn Lagasse 	if (pool_mounted) {
1399f169c0eaSGlenn Lagasse 		int err = BE_SUCCESS;
1400f169c0eaSGlenn Lagasse 		err = be_unmount_pool(zhp, ptmp_mntpnt, orig_mntpnt);
1401f169c0eaSGlenn Lagasse 		if (ret == BE_SUCCESS)
1402f169c0eaSGlenn Lagasse 			ret = err;
1403f169c0eaSGlenn Lagasse 		free(orig_mntpnt);
1404f169c0eaSGlenn Lagasse 		free(ptmp_mntpnt);
1405f169c0eaSGlenn Lagasse 	}
1406f169c0eaSGlenn Lagasse 	ZFS_CLOSE(zhp);
1407f169c0eaSGlenn Lagasse 
1408f169c0eaSGlenn Lagasse 	free(buffer);
1409f169c0eaSGlenn Lagasse 	if (menu_fp != NULL)
1410f169c0eaSGlenn Lagasse 		(void) fclose(menu_fp);
1411f169c0eaSGlenn Lagasse 	if (tmp_menu_fp != NULL)
1412f169c0eaSGlenn Lagasse 		(void) fclose(tmp_menu_fp);
1413f169c0eaSGlenn Lagasse 	if (tmp_menu != NULL) {
1414f169c0eaSGlenn Lagasse 		(void) unlink(tmp_menu);
1415f169c0eaSGlenn Lagasse 		free(tmp_menu);
1416f169c0eaSGlenn Lagasse 	}
1417f169c0eaSGlenn Lagasse 
1418f169c0eaSGlenn Lagasse 	return (ret);
1419f169c0eaSGlenn Lagasse }
1420f169c0eaSGlenn Lagasse 
1421f169c0eaSGlenn Lagasse /*
1422f169c0eaSGlenn Lagasse  * Function:	be_default_grub_bootfs
1423f169c0eaSGlenn Lagasse  * Description:	This function returns the dataset in the default entry of
1424f169c0eaSGlenn Lagasse  *		the grub menu. If no default entry is found with a valid bootfs
1425f169c0eaSGlenn Lagasse  *		entry NULL is returned.
1426f169c0eaSGlenn Lagasse  * Parameters:
1427f169c0eaSGlenn Lagasse  *		be_root_pool - This is the name of the root pool where the
1428f169c0eaSGlenn Lagasse  *			       grub menu can be found.
1429f169c0eaSGlenn Lagasse  *              def_bootfs - This is used to pass back the bootfs string. On
1430f169c0eaSGlenn Lagasse  *				error NULL is returned here.
1431f169c0eaSGlenn Lagasse  * Returns:
1432f169c0eaSGlenn Lagasse  *		Success - BE_SUCCESS is returned.
1433f169c0eaSGlenn Lagasse  *		Failure - a be_errno_t is returned.
1434f169c0eaSGlenn Lagasse  * Scope:
1435f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
1436f169c0eaSGlenn Lagasse  */
1437f169c0eaSGlenn Lagasse int
be_default_grub_bootfs(const char * be_root_pool,char ** def_bootfs)1438f169c0eaSGlenn Lagasse be_default_grub_bootfs(const char *be_root_pool, char **def_bootfs)
1439f169c0eaSGlenn Lagasse {
1440f169c0eaSGlenn Lagasse 	zfs_handle_t	*zhp = NULL;
1441f169c0eaSGlenn Lagasse 	char		grub_file[MAXPATHLEN];
1442f169c0eaSGlenn Lagasse 	FILE		*menu_fp;
1443f169c0eaSGlenn Lagasse 	char		line[BUFSIZ];
1444f169c0eaSGlenn Lagasse 	char		*pool_mntpnt = NULL;
1445f169c0eaSGlenn Lagasse 	char		*ptmp_mntpnt = NULL;
1446f169c0eaSGlenn Lagasse 	char		*orig_mntpnt = NULL;
1447f169c0eaSGlenn Lagasse 	int		default_entry = 0, entries = 0;
1448f169c0eaSGlenn Lagasse 	int		found_default = 0;
1449f169c0eaSGlenn Lagasse 	int		ret = BE_SUCCESS;
1450f169c0eaSGlenn Lagasse 	boolean_t	pool_mounted = B_FALSE;
1451f169c0eaSGlenn Lagasse 
1452f169c0eaSGlenn Lagasse 	errno = 0;
1453f169c0eaSGlenn Lagasse 
1454f169c0eaSGlenn Lagasse 	/*
1455f169c0eaSGlenn Lagasse 	 * Check to see if this system supports grub
1456f169c0eaSGlenn Lagasse 	 */
1457f169c0eaSGlenn Lagasse 	if (!be_has_grub()) {
1458f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_default_grub_bootfs: operation "
1459f169c0eaSGlenn Lagasse 		    "not supported on this architecture\n"));
1460f169c0eaSGlenn Lagasse 		return (BE_ERR_NOTSUP);
1461f169c0eaSGlenn Lagasse 	}
1462f169c0eaSGlenn Lagasse 
1463f169c0eaSGlenn Lagasse 	*def_bootfs = NULL;
1464f169c0eaSGlenn Lagasse 
1465f169c0eaSGlenn Lagasse 	/* Get handle to pool dataset */
1466f169c0eaSGlenn Lagasse 	if ((zhp = zfs_open(g_zfs, be_root_pool, ZFS_TYPE_DATASET)) == NULL) {
1467f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_default_grub_bootfs: "
1468f169c0eaSGlenn Lagasse 		    "failed to open pool dataset for %s: %s"),
1469f169c0eaSGlenn Lagasse 		    be_root_pool, libzfs_error_description(g_zfs));
1470f169c0eaSGlenn Lagasse 		return (zfs_err_to_be_err(g_zfs));
1471f169c0eaSGlenn Lagasse 	}
1472f169c0eaSGlenn Lagasse 
1473f169c0eaSGlenn Lagasse 	/*
1474f169c0eaSGlenn Lagasse 	 * Check to see if the pool's dataset is mounted. If it isn't we'll
1475f169c0eaSGlenn Lagasse 	 * attempt to mount it.
1476f169c0eaSGlenn Lagasse 	 */
1477f169c0eaSGlenn Lagasse 	if ((ret = be_mount_pool(zhp, &ptmp_mntpnt, &orig_mntpnt,
1478f169c0eaSGlenn Lagasse 	    &pool_mounted)) != BE_SUCCESS) {
1479f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_default_grub_bootfs: pool dataset "
1480f169c0eaSGlenn Lagasse 		    "(%s) could not be mounted\n"), be_root_pool);
1481f169c0eaSGlenn Lagasse 		ZFS_CLOSE(zhp);
1482f169c0eaSGlenn Lagasse 		return (ret);
1483f169c0eaSGlenn Lagasse 	}
1484f169c0eaSGlenn Lagasse 
1485f169c0eaSGlenn Lagasse 	/*
1486f169c0eaSGlenn Lagasse 	 * Get the mountpoint for the root pool dataset.
1487f169c0eaSGlenn Lagasse 	 */
1488f169c0eaSGlenn Lagasse 	if (!zfs_is_mounted(zhp, &pool_mntpnt)) {
1489f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_default_grub_bootfs: failed "
1490f169c0eaSGlenn Lagasse 		    "to get mount point for the root pool. Can't set "
1491f169c0eaSGlenn Lagasse 		    "the default BE in the grub menu.\n"));
1492f169c0eaSGlenn Lagasse 		ret = BE_ERR_NO_MENU;
1493f169c0eaSGlenn Lagasse 		goto cleanup;
1494f169c0eaSGlenn Lagasse 	}
1495f169c0eaSGlenn Lagasse 
1496f169c0eaSGlenn Lagasse 	(void) snprintf(grub_file, MAXPATHLEN, "%s%s",
1497f169c0eaSGlenn Lagasse 	    pool_mntpnt, BE_GRUB_MENU);
1498f169c0eaSGlenn Lagasse 
1499de1ab35cSAlexander Eremin 	if ((ret = be_open_menu((char *)be_root_pool, grub_file,
1500f169c0eaSGlenn Lagasse 	    &menu_fp, "r", B_FALSE)) != BE_SUCCESS) {
1501f169c0eaSGlenn Lagasse 		goto cleanup;
1502f169c0eaSGlenn Lagasse 	} else if (menu_fp == NULL) {
1503f169c0eaSGlenn Lagasse 		ret = BE_ERR_NO_MENU;
1504f169c0eaSGlenn Lagasse 		goto cleanup;
1505f169c0eaSGlenn Lagasse 	}
1506f169c0eaSGlenn Lagasse 
1507f169c0eaSGlenn Lagasse 	free(pool_mntpnt);
1508f169c0eaSGlenn Lagasse 	pool_mntpnt = NULL;
1509f169c0eaSGlenn Lagasse 
1510f169c0eaSGlenn Lagasse 	while (fgets(line, BUFSIZ, menu_fp)) {
1511f169c0eaSGlenn Lagasse 		char *tok = strtok(line, BE_WHITE_SPACE);
1512f169c0eaSGlenn Lagasse 
1513f169c0eaSGlenn Lagasse 		if (tok != NULL && tok[0] != '#') {
1514f169c0eaSGlenn Lagasse 			if (!found_default) {
1515f169c0eaSGlenn Lagasse 				if (strcmp(tok, "default") == 0) {
1516f169c0eaSGlenn Lagasse 					tok = strtok(NULL, BE_WHITE_SPACE);
1517f169c0eaSGlenn Lagasse 					if (tok != NULL) {
1518f169c0eaSGlenn Lagasse 						default_entry = atoi(tok);
1519f169c0eaSGlenn Lagasse 						rewind(menu_fp);
1520f169c0eaSGlenn Lagasse 						found_default = 1;
1521f169c0eaSGlenn Lagasse 					}
1522f169c0eaSGlenn Lagasse 				}
1523f169c0eaSGlenn Lagasse 				continue;
1524f169c0eaSGlenn Lagasse 			}
1525f169c0eaSGlenn Lagasse 			if (strcmp(tok, "title") == 0) {
1526f169c0eaSGlenn Lagasse 				entries++;
1527f169c0eaSGlenn Lagasse 			} else if (default_entry == entries - 1) {
1528f169c0eaSGlenn Lagasse 				if (strcmp(tok, "bootfs") == 0) {
1529f169c0eaSGlenn Lagasse 					tok = strtok(NULL, BE_WHITE_SPACE);
1530f169c0eaSGlenn Lagasse 					(void) fclose(menu_fp);
1531f169c0eaSGlenn Lagasse 
1532f169c0eaSGlenn Lagasse 					if (tok == NULL) {
1533f169c0eaSGlenn Lagasse 						ret = BE_SUCCESS;
1534f169c0eaSGlenn Lagasse 						goto cleanup;
1535f169c0eaSGlenn Lagasse 					}
1536f169c0eaSGlenn Lagasse 
1537f169c0eaSGlenn Lagasse 					if ((*def_bootfs = strdup(tok)) !=
1538f169c0eaSGlenn Lagasse 					    NULL) {
1539f169c0eaSGlenn Lagasse 						ret = BE_SUCCESS;
1540f169c0eaSGlenn Lagasse 						goto cleanup;
1541f169c0eaSGlenn Lagasse 					}
1542f169c0eaSGlenn Lagasse 					be_print_err(gettext(
1543f169c0eaSGlenn Lagasse 					    "be_default_grub_bootfs: "
1544f169c0eaSGlenn Lagasse 					    "memory allocation failed\n"));
1545f169c0eaSGlenn Lagasse 					ret = BE_ERR_NOMEM;
1546f169c0eaSGlenn Lagasse 					goto cleanup;
1547f169c0eaSGlenn Lagasse 				}
1548f169c0eaSGlenn Lagasse 			} else if (default_entry < entries - 1) {
1549f169c0eaSGlenn Lagasse 				/*
1550f169c0eaSGlenn Lagasse 				 * no bootfs entry for the default entry.
1551f169c0eaSGlenn Lagasse 				 */
1552f169c0eaSGlenn Lagasse 				break;
1553f169c0eaSGlenn Lagasse 			}
1554f169c0eaSGlenn Lagasse 		}
1555f169c0eaSGlenn Lagasse 	}
1556f169c0eaSGlenn Lagasse 	(void) fclose(menu_fp);
1557f169c0eaSGlenn Lagasse 
1558f169c0eaSGlenn Lagasse cleanup:
1559f169c0eaSGlenn Lagasse 	if (pool_mounted) {
1560f169c0eaSGlenn Lagasse 		int err = BE_SUCCESS;
1561f169c0eaSGlenn Lagasse 		err = be_unmount_pool(zhp, ptmp_mntpnt, orig_mntpnt);
1562f169c0eaSGlenn Lagasse 		if (ret == BE_SUCCESS)
1563f169c0eaSGlenn Lagasse 			ret = err;
1564f169c0eaSGlenn Lagasse 		free(orig_mntpnt);
1565f169c0eaSGlenn Lagasse 		free(ptmp_mntpnt);
1566f169c0eaSGlenn Lagasse 	}
1567f169c0eaSGlenn Lagasse 	ZFS_CLOSE(zhp);
1568f169c0eaSGlenn Lagasse 	return (ret);
1569f169c0eaSGlenn Lagasse }
1570f169c0eaSGlenn Lagasse 
1571f169c0eaSGlenn Lagasse /*
1572f169c0eaSGlenn Lagasse  * Function:	be_change_grub_default
1573f169c0eaSGlenn Lagasse  * Description:	This function takes two parameters. These are the name of
1574f169c0eaSGlenn Lagasse  *		the BE we want to have as the default booted in the grub
1575f169c0eaSGlenn Lagasse  *		menu and the root pool where the path to the grub menu exists.
1576f169c0eaSGlenn Lagasse  *		The code takes this and finds the BE's entry in the grub menu
1577f169c0eaSGlenn Lagasse  *		and changes the default entry to point to that entry in the
1578f169c0eaSGlenn Lagasse  *		list.
1579f169c0eaSGlenn Lagasse  * Parameters:
1580f169c0eaSGlenn Lagasse  *		be_name - This is the name of the BE wanted as the default
1581f169c0eaSGlenn Lagasse  *			for the next boot.
1582f169c0eaSGlenn Lagasse  *		be_root_pool - This is the name of the root pool where the
1583f169c0eaSGlenn Lagasse  *			grub menu can be found.
1584f169c0eaSGlenn Lagasse  * Returns:
1585f169c0eaSGlenn Lagasse  *		BE_SUCCESS - Success
1586f169c0eaSGlenn Lagasse  *		be_errno_t - Failure
1587f169c0eaSGlenn Lagasse  * Scope:
1588f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
1589f169c0eaSGlenn Lagasse  */
1590f169c0eaSGlenn Lagasse int
be_change_grub_default(char * be_name,char * be_root_pool)1591f169c0eaSGlenn Lagasse be_change_grub_default(char *be_name, char *be_root_pool)
1592f169c0eaSGlenn Lagasse {
1593f169c0eaSGlenn Lagasse 	zfs_handle_t	*zhp = NULL;
1594f169c0eaSGlenn Lagasse 	char	grub_file[MAXPATHLEN];
1595fafb665dSToomas Soome 	char	*temp_grub = NULL;
1596f169c0eaSGlenn Lagasse 	char	*pool_mntpnt = NULL;
1597f169c0eaSGlenn Lagasse 	char	*ptmp_mntpnt = NULL;
1598f169c0eaSGlenn Lagasse 	char	*orig_mntpnt = NULL;
1599f169c0eaSGlenn Lagasse 	char	line[BUFSIZ];
1600f169c0eaSGlenn Lagasse 	char	temp_line[BUFSIZ];
1601f169c0eaSGlenn Lagasse 	char	be_root_ds[MAXPATHLEN];
1602f169c0eaSGlenn Lagasse 	FILE	*grub_fp = NULL;
1603f169c0eaSGlenn Lagasse 	FILE	*temp_fp = NULL;
1604f169c0eaSGlenn Lagasse 	struct stat	sb;
1605f169c0eaSGlenn Lagasse 	int	temp_grub_len = 0;
1606f169c0eaSGlenn Lagasse 	int	fd, entries = 0;
1607f169c0eaSGlenn Lagasse 	int	err = 0;
1608f169c0eaSGlenn Lagasse 	int	ret = BE_SUCCESS;
1609f169c0eaSGlenn Lagasse 	boolean_t	found_default = B_FALSE;
1610f169c0eaSGlenn Lagasse 	boolean_t	pool_mounted = B_FALSE;
1611f169c0eaSGlenn Lagasse 
1612f169c0eaSGlenn Lagasse 	errno = 0;
1613f169c0eaSGlenn Lagasse 
1614f169c0eaSGlenn Lagasse 	/*
1615f169c0eaSGlenn Lagasse 	 * Check to see if this system supports grub
1616f169c0eaSGlenn Lagasse 	 */
1617f169c0eaSGlenn Lagasse 	if (!be_has_grub()) {
1618f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_change_grub_default: operation "
1619f169c0eaSGlenn Lagasse 		    "not supported on this architecture\n"));
1620f169c0eaSGlenn Lagasse 		return (BE_ERR_NOTSUP);
1621f169c0eaSGlenn Lagasse 	}
1622f169c0eaSGlenn Lagasse 
1623f169c0eaSGlenn Lagasse 	/* Generate string for BE's root dataset */
1624*ec8422d0SAndy Fiddaman 	if ((ret = be_make_root_ds(be_root_pool, be_name, be_root_ds,
1625*ec8422d0SAndy Fiddaman 	    sizeof (be_root_ds))) != BE_SUCCESS) {
1626*ec8422d0SAndy Fiddaman 		be_print_err(gettext("%s: failed to get BE container dataset "
1627*ec8422d0SAndy Fiddaman 		    "for %s/%s\n"), __func__, be_root_pool, be_name);
1628*ec8422d0SAndy Fiddaman 		return (ret);
1629*ec8422d0SAndy Fiddaman 	}
1630f169c0eaSGlenn Lagasse 
1631f169c0eaSGlenn Lagasse 	/* Get handle to pool dataset */
1632f169c0eaSGlenn Lagasse 	if ((zhp = zfs_open(g_zfs, be_root_pool, ZFS_TYPE_DATASET)) == NULL) {
1633f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_change_grub_default: "
1634f169c0eaSGlenn Lagasse 		    "failed to open pool dataset for %s: %s"),
1635f169c0eaSGlenn Lagasse 		    be_root_pool, libzfs_error_description(g_zfs));
1636f169c0eaSGlenn Lagasse 		return (zfs_err_to_be_err(g_zfs));
1637f169c0eaSGlenn Lagasse 	}
1638f169c0eaSGlenn Lagasse 
1639f169c0eaSGlenn Lagasse 	/*
1640f169c0eaSGlenn Lagasse 	 * Check to see if the pool's dataset is mounted. If it isn't we'll
1641f169c0eaSGlenn Lagasse 	 * attempt to mount it.
1642f169c0eaSGlenn Lagasse 	 */
1643f169c0eaSGlenn Lagasse 	if ((ret = be_mount_pool(zhp, &ptmp_mntpnt, &orig_mntpnt,
1644f169c0eaSGlenn Lagasse 	    &pool_mounted)) != BE_SUCCESS) {
1645f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_change_grub_default: pool dataset "
1646f169c0eaSGlenn Lagasse 		    "(%s) could not be mounted\n"), be_root_pool);
1647f169c0eaSGlenn Lagasse 		ZFS_CLOSE(zhp);
1648f169c0eaSGlenn Lagasse 		return (ret);
1649f169c0eaSGlenn Lagasse 	}
1650f169c0eaSGlenn Lagasse 
1651f169c0eaSGlenn Lagasse 	/*
1652f169c0eaSGlenn Lagasse 	 * Get the mountpoint for the root pool dataset.
1653f169c0eaSGlenn Lagasse 	 */
1654f169c0eaSGlenn Lagasse 	if (!zfs_is_mounted(zhp, &pool_mntpnt)) {
1655f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_change_grub_default: pool "
1656f169c0eaSGlenn Lagasse 		    "dataset (%s) is not mounted. Can't set "
1657f169c0eaSGlenn Lagasse 		    "the default BE in the grub menu.\n"), be_root_pool);
1658f169c0eaSGlenn Lagasse 		ret = BE_ERR_NO_MENU;
1659f169c0eaSGlenn Lagasse 		goto cleanup;
1660f169c0eaSGlenn Lagasse 	}
1661f169c0eaSGlenn Lagasse 
1662f169c0eaSGlenn Lagasse 	(void) snprintf(grub_file, MAXPATHLEN, "%s%s",
1663f169c0eaSGlenn Lagasse 	    pool_mntpnt, BE_GRUB_MENU);
1664f169c0eaSGlenn Lagasse 
1665de1ab35cSAlexander Eremin 	if ((ret = be_open_menu(be_root_pool, grub_file,
1666f169c0eaSGlenn Lagasse 	    &grub_fp, "r+", B_TRUE)) != BE_SUCCESS) {
1667f169c0eaSGlenn Lagasse 		goto cleanup;
1668f169c0eaSGlenn Lagasse 	} else if (grub_fp == NULL) {
1669f169c0eaSGlenn Lagasse 		ret = BE_ERR_NO_MENU;
1670f169c0eaSGlenn Lagasse 		goto cleanup;
1671f169c0eaSGlenn Lagasse 	}
1672f169c0eaSGlenn Lagasse 
1673f169c0eaSGlenn Lagasse 	free(pool_mntpnt);
1674f169c0eaSGlenn Lagasse 	pool_mntpnt = NULL;
1675f169c0eaSGlenn Lagasse 
1676f169c0eaSGlenn Lagasse 	/* Grab the stats of the original menu file */
1677f169c0eaSGlenn Lagasse 	if (stat(grub_file, &sb) != 0) {
1678f169c0eaSGlenn Lagasse 		err = errno;
1679f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_change_grub_default: "
1680f169c0eaSGlenn Lagasse 		    "failed to stat file %s: %s\n"), grub_file, strerror(err));
1681f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
1682f169c0eaSGlenn Lagasse 		goto cleanup;
1683f169c0eaSGlenn Lagasse 	}
1684f169c0eaSGlenn Lagasse 
1685f169c0eaSGlenn Lagasse 	/* Create a tmp file for the modified menu.lst */
1686f169c0eaSGlenn Lagasse 	temp_grub_len = strlen(grub_file) + 7;
1687f169c0eaSGlenn Lagasse 	if ((temp_grub = (char *)malloc(temp_grub_len)) == NULL) {
1688f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_change_grub_default: "
1689f169c0eaSGlenn Lagasse 		    "malloc failed\n"));
1690f169c0eaSGlenn Lagasse 		ret = BE_ERR_NOMEM;
1691f169c0eaSGlenn Lagasse 		goto cleanup;
1692f169c0eaSGlenn Lagasse 	}
1693f169c0eaSGlenn Lagasse 	(void) memset(temp_grub, 0, temp_grub_len);
1694f169c0eaSGlenn Lagasse 	(void) strlcpy(temp_grub, grub_file, temp_grub_len);
1695f169c0eaSGlenn Lagasse 	(void) strlcat(temp_grub, "XXXXXX", temp_grub_len);
1696f169c0eaSGlenn Lagasse 	if ((fd = mkstemp(temp_grub)) == -1) {
1697f169c0eaSGlenn Lagasse 		err = errno;
1698f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_change_grub_default: "
1699f169c0eaSGlenn Lagasse 		    "mkstemp failed: %s\n"), strerror(err));
1700f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
1701f169c0eaSGlenn Lagasse 		free(temp_grub);
1702f169c0eaSGlenn Lagasse 		temp_grub = NULL;
1703f169c0eaSGlenn Lagasse 		goto cleanup;
1704f169c0eaSGlenn Lagasse 	}
1705f169c0eaSGlenn Lagasse 	if ((temp_fp = fdopen(fd, "w")) == NULL) {
1706f169c0eaSGlenn Lagasse 		err = errno;
1707f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_change_grub_default: "
1708f169c0eaSGlenn Lagasse 		    "failed to open %s file: %s\n"),
1709f169c0eaSGlenn Lagasse 		    temp_grub, strerror(err));
1710f169c0eaSGlenn Lagasse 		(void) close(fd);
1711f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
1712f169c0eaSGlenn Lagasse 		goto cleanup;
1713f169c0eaSGlenn Lagasse 	}
1714f169c0eaSGlenn Lagasse 
1715f169c0eaSGlenn Lagasse 	while (fgets(line, BUFSIZ, grub_fp)) {
1716f169c0eaSGlenn Lagasse 		char *tok = strtok(line, BE_WHITE_SPACE);
1717f169c0eaSGlenn Lagasse 
1718f169c0eaSGlenn Lagasse 		if (tok == NULL || tok[0] == '#') {
1719f169c0eaSGlenn Lagasse 			continue;
1720f169c0eaSGlenn Lagasse 		} else if (strcmp(tok, "title") == 0) {
1721f169c0eaSGlenn Lagasse 			entries++;
1722f169c0eaSGlenn Lagasse 			continue;
1723f169c0eaSGlenn Lagasse 		} else if (strcmp(tok, "bootfs") == 0) {
1724f169c0eaSGlenn Lagasse 			char *bootfs = strtok(NULL, BE_WHITE_SPACE);
1725f169c0eaSGlenn Lagasse 			if (bootfs == NULL)
1726f169c0eaSGlenn Lagasse 				continue;
1727f169c0eaSGlenn Lagasse 
1728f169c0eaSGlenn Lagasse 			if (strcmp(bootfs, be_root_ds) == 0) {
1729f169c0eaSGlenn Lagasse 				found_default = B_TRUE;
1730f169c0eaSGlenn Lagasse 				break;
1731f169c0eaSGlenn Lagasse 			}
1732f169c0eaSGlenn Lagasse 		}
1733f169c0eaSGlenn Lagasse 	}
1734f169c0eaSGlenn Lagasse 
1735f169c0eaSGlenn Lagasse 	if (!found_default) {
1736f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_change_grub_default: failed "
1737f169c0eaSGlenn Lagasse 		    "to find entry for %s in the grub menu\n"),
1738f169c0eaSGlenn Lagasse 		    be_name);
1739f169c0eaSGlenn Lagasse 		ret = BE_ERR_BE_NOENT;
1740f169c0eaSGlenn Lagasse 		goto cleanup;
1741f169c0eaSGlenn Lagasse 	}
1742f169c0eaSGlenn Lagasse 
1743f169c0eaSGlenn Lagasse 	rewind(grub_fp);
1744f169c0eaSGlenn Lagasse 
1745f169c0eaSGlenn Lagasse 	while (fgets(line, BUFSIZ, grub_fp)) {
1746f169c0eaSGlenn Lagasse 		char *tok = NULL;
1747f169c0eaSGlenn Lagasse 
1748f169c0eaSGlenn Lagasse 		(void) strncpy(temp_line, line, BUFSIZ);
1749f169c0eaSGlenn Lagasse 
1750f169c0eaSGlenn Lagasse 		if ((tok = strtok(temp_line, BE_WHITE_SPACE)) != NULL &&
1751f169c0eaSGlenn Lagasse 		    strcmp(tok, "default") == 0) {
1752f169c0eaSGlenn Lagasse 			(void) snprintf(temp_line, BUFSIZ, "default %d\n",
1753f169c0eaSGlenn Lagasse 			    entries - 1 >= 0 ? entries - 1 : 0);
1754f169c0eaSGlenn Lagasse 			(void) fputs(temp_line, temp_fp);
1755f169c0eaSGlenn Lagasse 		} else {
1756f169c0eaSGlenn Lagasse 			(void) fputs(line, temp_fp);
1757f169c0eaSGlenn Lagasse 		}
1758f169c0eaSGlenn Lagasse 	}
1759f169c0eaSGlenn Lagasse 
1760f169c0eaSGlenn Lagasse 	(void) fclose(grub_fp);
1761f169c0eaSGlenn Lagasse 	grub_fp = NULL;
1762f169c0eaSGlenn Lagasse 	(void) fclose(temp_fp);
1763f169c0eaSGlenn Lagasse 	temp_fp = NULL;
1764f169c0eaSGlenn Lagasse 
1765f169c0eaSGlenn Lagasse 	if (rename(temp_grub, grub_file) != 0) {
1766f169c0eaSGlenn Lagasse 		err = errno;
1767f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_change_grub_default: "
1768f169c0eaSGlenn Lagasse 		    "failed to rename file %s to %s: %s\n"),
1769f169c0eaSGlenn Lagasse 		    temp_grub, grub_file, strerror(err));
1770f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
1771f169c0eaSGlenn Lagasse 		goto cleanup;
1772f169c0eaSGlenn Lagasse 	}
1773f169c0eaSGlenn Lagasse 	free(temp_grub);
1774f169c0eaSGlenn Lagasse 	temp_grub = NULL;
1775f169c0eaSGlenn Lagasse 
1776f169c0eaSGlenn Lagasse 	/* Set the perms and ownership of the updated file */
1777f169c0eaSGlenn Lagasse 	if (chmod(grub_file, sb.st_mode) != 0) {
1778f169c0eaSGlenn Lagasse 		err = errno;
1779f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_change_grub_default: "
1780f169c0eaSGlenn Lagasse 		    "failed to chmod %s: %s\n"), grub_file, strerror(err));
1781f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
1782f169c0eaSGlenn Lagasse 		goto cleanup;
1783f169c0eaSGlenn Lagasse 	}
1784f169c0eaSGlenn Lagasse 	if (chown(grub_file, sb.st_uid, sb.st_gid) != 0) {
1785f169c0eaSGlenn Lagasse 		err = errno;
1786f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_change_grub_default: "
1787f169c0eaSGlenn Lagasse 		    "failed to chown %s: %s\n"), grub_file, strerror(err));
1788f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
1789f169c0eaSGlenn Lagasse 	}
1790f169c0eaSGlenn Lagasse 
1791f169c0eaSGlenn Lagasse cleanup:
1792f169c0eaSGlenn Lagasse 	if (pool_mounted) {
1793f169c0eaSGlenn Lagasse 		int err = BE_SUCCESS;
1794f169c0eaSGlenn Lagasse 		err = be_unmount_pool(zhp, ptmp_mntpnt, orig_mntpnt);
1795f169c0eaSGlenn Lagasse 		if (ret == BE_SUCCESS)
1796f169c0eaSGlenn Lagasse 			ret = err;
1797f169c0eaSGlenn Lagasse 		free(orig_mntpnt);
1798f169c0eaSGlenn Lagasse 		free(ptmp_mntpnt);
1799f169c0eaSGlenn Lagasse 	}
1800f169c0eaSGlenn Lagasse 	ZFS_CLOSE(zhp);
1801f169c0eaSGlenn Lagasse 	if (grub_fp != NULL)
1802f169c0eaSGlenn Lagasse 		(void) fclose(grub_fp);
1803f169c0eaSGlenn Lagasse 	if (temp_fp != NULL)
1804f169c0eaSGlenn Lagasse 		(void) fclose(temp_fp);
1805f169c0eaSGlenn Lagasse 	if (temp_grub != NULL) {
1806f169c0eaSGlenn Lagasse 		(void) unlink(temp_grub);
1807f169c0eaSGlenn Lagasse 		free(temp_grub);
1808f169c0eaSGlenn Lagasse 	}
1809f169c0eaSGlenn Lagasse 
1810f169c0eaSGlenn Lagasse 	return (ret);
1811f169c0eaSGlenn Lagasse }
1812f169c0eaSGlenn Lagasse 
1813f169c0eaSGlenn Lagasse /*
1814f169c0eaSGlenn Lagasse  * Function:	be_update_menu
1815f169c0eaSGlenn Lagasse  * Description:	This function is used by be_rename to change the BE name in
1816f169c0eaSGlenn Lagasse  *		an existing entry in the grub menu to the new name of the BE.
1817f169c0eaSGlenn Lagasse  * Parameters:
1818f169c0eaSGlenn Lagasse  *		be_orig_name - the original name of the BE
1819f169c0eaSGlenn Lagasse  *		be_new_name - the new name the BE is being renameed to.
1820f169c0eaSGlenn Lagasse  *		be_root_pool - The pool which contains the grub menu
1821f169c0eaSGlenn Lagasse  *		boot_pool - the pool where the BE is, if different than
1822f169c0eaSGlenn Lagasse  *			the pool containing the boot menu.  If this is
1823f169c0eaSGlenn Lagasse  *			NULL it will be set to be_root_pool.
1824f169c0eaSGlenn Lagasse  * Returns:
1825f169c0eaSGlenn Lagasse  *		BE_SUCCESS - Success
1826f169c0eaSGlenn Lagasse  *		be_errno_t - Failure
1827f169c0eaSGlenn Lagasse  * Scope:
1828f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
1829f169c0eaSGlenn Lagasse  */
1830f169c0eaSGlenn Lagasse int
be_update_menu(char * be_orig_name,char * be_new_name,char * be_root_pool,char * boot_pool)1831f169c0eaSGlenn Lagasse be_update_menu(char *be_orig_name, char *be_new_name, char *be_root_pool,
1832f169c0eaSGlenn Lagasse     char *boot_pool)
1833f169c0eaSGlenn Lagasse {
1834f169c0eaSGlenn Lagasse 	zfs_handle_t *zhp = NULL;
1835f169c0eaSGlenn Lagasse 	char menu_file[MAXPATHLEN];
1836f169c0eaSGlenn Lagasse 	char be_root_ds[MAXPATHLEN];
1837f169c0eaSGlenn Lagasse 	char be_new_root_ds[MAXPATHLEN];
1838f169c0eaSGlenn Lagasse 	char line[BUFSIZ];
1839f169c0eaSGlenn Lagasse 	char *pool_mntpnt = NULL;
1840f169c0eaSGlenn Lagasse 	char *ptmp_mntpnt = NULL;
1841f169c0eaSGlenn Lagasse 	char *orig_mntpnt = NULL;
1842f169c0eaSGlenn Lagasse 	char *temp_menu = NULL;
1843f169c0eaSGlenn Lagasse 	FILE *menu_fp = NULL;
1844f169c0eaSGlenn Lagasse 	FILE *new_fp = NULL;
1845f169c0eaSGlenn Lagasse 	struct stat sb;
1846f169c0eaSGlenn Lagasse 	int temp_menu_len = 0;
1847f169c0eaSGlenn Lagasse 	int tmp_fd;
1848f169c0eaSGlenn Lagasse 	int ret = BE_SUCCESS;
1849f169c0eaSGlenn Lagasse 	int err = 0;
1850f169c0eaSGlenn Lagasse 	boolean_t pool_mounted = B_FALSE;
1851f169c0eaSGlenn Lagasse 
1852f169c0eaSGlenn Lagasse 	errno = 0;
1853f169c0eaSGlenn Lagasse 
1854f169c0eaSGlenn Lagasse 	if (boot_pool == NULL)
1855f169c0eaSGlenn Lagasse 		boot_pool = be_root_pool;
1856f169c0eaSGlenn Lagasse 
1857f169c0eaSGlenn Lagasse 	if ((zhp = zfs_open(g_zfs, be_root_pool, ZFS_TYPE_DATASET)) == NULL) {
1858f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_update_menu: failed to open "
1859f169c0eaSGlenn Lagasse 		    "pool dataset for %s: %s\n"), be_root_pool,
1860f169c0eaSGlenn Lagasse 		    libzfs_error_description(g_zfs));
1861f169c0eaSGlenn Lagasse 		return (zfs_err_to_be_err(g_zfs));
1862f169c0eaSGlenn Lagasse 	}
1863f169c0eaSGlenn Lagasse 
1864f169c0eaSGlenn Lagasse 	/*
1865f169c0eaSGlenn Lagasse 	 * Check to see if the pool's dataset is mounted. If it isn't we'll
1866f169c0eaSGlenn Lagasse 	 * attempt to mount it.
1867f169c0eaSGlenn Lagasse 	 */
1868f169c0eaSGlenn Lagasse 	if ((ret = be_mount_pool(zhp, &ptmp_mntpnt, &orig_mntpnt,
1869f169c0eaSGlenn Lagasse 	    &pool_mounted)) != BE_SUCCESS) {
1870f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_update_menu: pool dataset "
1871f169c0eaSGlenn Lagasse 		    "(%s) could not be mounted\n"), be_root_pool);
1872f169c0eaSGlenn Lagasse 		ZFS_CLOSE(zhp);
1873f169c0eaSGlenn Lagasse 		return (ret);
1874f169c0eaSGlenn Lagasse 	}
1875f169c0eaSGlenn Lagasse 
1876f169c0eaSGlenn Lagasse 	/*
1877f169c0eaSGlenn Lagasse 	 * Get the mountpoint for the root pool dataset.
1878f169c0eaSGlenn Lagasse 	 */
1879f169c0eaSGlenn Lagasse 	if (!zfs_is_mounted(zhp, &pool_mntpnt)) {
1880f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_update_menu: failed "
1881f169c0eaSGlenn Lagasse 		    "to get mount point for the root pool. Can't set "
1882f169c0eaSGlenn Lagasse 		    "the default BE in the grub menu.\n"));
1883f169c0eaSGlenn Lagasse 		ret = BE_ERR_NO_MENU;
1884f169c0eaSGlenn Lagasse 		goto cleanup;
1885f169c0eaSGlenn Lagasse 	}
1886f169c0eaSGlenn Lagasse 
1887f169c0eaSGlenn Lagasse 	/*
1888f169c0eaSGlenn Lagasse 	 * Check to see if this system supports grub
1889f169c0eaSGlenn Lagasse 	 */
1890f169c0eaSGlenn Lagasse 	if (be_has_grub()) {
1891f169c0eaSGlenn Lagasse 		(void) snprintf(menu_file, sizeof (menu_file),
1892f169c0eaSGlenn Lagasse 		    "%s%s", pool_mntpnt, BE_GRUB_MENU);
1893f169c0eaSGlenn Lagasse 	} else {
1894f169c0eaSGlenn Lagasse 		(void) snprintf(menu_file, sizeof (menu_file),
1895f169c0eaSGlenn Lagasse 		    "%s%s", pool_mntpnt, BE_SPARC_MENU);
1896f169c0eaSGlenn Lagasse 	}
1897f169c0eaSGlenn Lagasse 
1898*ec8422d0SAndy Fiddaman 	if ((ret = be_make_root_ds(be_root_pool, be_orig_name, be_root_ds,
1899*ec8422d0SAndy Fiddaman 	    sizeof (be_root_ds))) != BE_SUCCESS) {
1900*ec8422d0SAndy Fiddaman 		be_print_err(gettext("%s: failed to get BE container dataset "
1901*ec8422d0SAndy Fiddaman 		    "for %s/%s\n"), __func__, be_root_pool, be_orig_name);
1902*ec8422d0SAndy Fiddaman 		goto cleanup;
1903*ec8422d0SAndy Fiddaman 	}
1904*ec8422d0SAndy Fiddaman 	if ((ret = be_make_root_ds(be_root_pool, be_new_name, be_new_root_ds,
1905*ec8422d0SAndy Fiddaman 	    sizeof (be_new_root_ds))) != BE_SUCCESS) {
1906*ec8422d0SAndy Fiddaman 		be_print_err(gettext("%s: failed to get BE container dataset "
1907*ec8422d0SAndy Fiddaman 		    "for %s/%s\n"), __func__, be_root_pool, be_new_name);
1908*ec8422d0SAndy Fiddaman 		goto cleanup;
1909*ec8422d0SAndy Fiddaman 	}
1910f169c0eaSGlenn Lagasse 
1911de1ab35cSAlexander Eremin 	if ((ret = be_open_menu(be_root_pool, menu_file,
1912f169c0eaSGlenn Lagasse 	    &menu_fp, "r", B_TRUE)) != BE_SUCCESS) {
1913f169c0eaSGlenn Lagasse 		goto cleanup;
1914f169c0eaSGlenn Lagasse 	} else if (menu_fp == NULL) {
1915f169c0eaSGlenn Lagasse 		ret = BE_ERR_NO_MENU;
1916f169c0eaSGlenn Lagasse 		goto cleanup;
1917f169c0eaSGlenn Lagasse 	}
1918f169c0eaSGlenn Lagasse 
1919f169c0eaSGlenn Lagasse 	free(pool_mntpnt);
1920f169c0eaSGlenn Lagasse 	pool_mntpnt = NULL;
1921f169c0eaSGlenn Lagasse 
1922f169c0eaSGlenn Lagasse 	/* Grab the stat of the original menu file */
1923f169c0eaSGlenn Lagasse 	if (stat(menu_file, &sb) != 0) {
1924f169c0eaSGlenn Lagasse 		err = errno;
1925f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_update_menu: "
1926f169c0eaSGlenn Lagasse 		    "failed to stat file %s: %s\n"), menu_file, strerror(err));
1927f169c0eaSGlenn Lagasse 		(void) fclose(menu_fp);
1928f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
1929f169c0eaSGlenn Lagasse 		goto cleanup;
1930f169c0eaSGlenn Lagasse 	}
1931f169c0eaSGlenn Lagasse 
1932f169c0eaSGlenn Lagasse 	/* Create tmp file for modified menu.lst */
1933f169c0eaSGlenn Lagasse 	temp_menu_len = strlen(menu_file) + 7;
1934f169c0eaSGlenn Lagasse 	if ((temp_menu = (char *)malloc(temp_menu_len))
1935f169c0eaSGlenn Lagasse 	    == NULL) {
1936f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_update_menu: "
1937f169c0eaSGlenn Lagasse 		    "malloc failed\n"));
1938f169c0eaSGlenn Lagasse 		(void) fclose(menu_fp);
1939f169c0eaSGlenn Lagasse 		ret = BE_ERR_NOMEM;
1940f169c0eaSGlenn Lagasse 		goto cleanup;
1941f169c0eaSGlenn Lagasse 	}
1942f169c0eaSGlenn Lagasse 	(void) memset(temp_menu, 0, temp_menu_len);
1943f169c0eaSGlenn Lagasse 	(void) strlcpy(temp_menu, menu_file, temp_menu_len);
1944f169c0eaSGlenn Lagasse 	(void) strlcat(temp_menu, "XXXXXX", temp_menu_len);
1945f169c0eaSGlenn Lagasse 	if ((tmp_fd = mkstemp(temp_menu)) == -1) {
1946f169c0eaSGlenn Lagasse 		err = errno;
1947f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_update_menu: "
1948f169c0eaSGlenn Lagasse 		    "mkstemp failed: %s\n"), strerror(err));
1949f169c0eaSGlenn Lagasse 		(void) fclose(menu_fp);
1950f169c0eaSGlenn Lagasse 		free(temp_menu);
1951f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
1952f169c0eaSGlenn Lagasse 		goto cleanup;
1953f169c0eaSGlenn Lagasse 	}
1954f169c0eaSGlenn Lagasse 	if ((new_fp = fdopen(tmp_fd, "w")) == NULL) {
1955f169c0eaSGlenn Lagasse 		err = errno;
1956f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_update_menu: "
1957f169c0eaSGlenn Lagasse 		    "fdopen failed: %s\n"), strerror(err));
1958f169c0eaSGlenn Lagasse 		(void) close(tmp_fd);
1959f169c0eaSGlenn Lagasse 		(void) fclose(menu_fp);
1960f169c0eaSGlenn Lagasse 		free(temp_menu);
1961f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
1962f169c0eaSGlenn Lagasse 		goto cleanup;
1963f169c0eaSGlenn Lagasse 	}
1964f169c0eaSGlenn Lagasse 
1965f169c0eaSGlenn Lagasse 	while (fgets(line, BUFSIZ, menu_fp)) {
1966f169c0eaSGlenn Lagasse 		char tline[BUFSIZ];
1967f169c0eaSGlenn Lagasse 		char new_line[BUFSIZ];
1968f169c0eaSGlenn Lagasse 		char *c = NULL;
1969f169c0eaSGlenn Lagasse 
1970f169c0eaSGlenn Lagasse 		(void) strlcpy(tline, line, sizeof (tline));
1971f169c0eaSGlenn Lagasse 
1972f169c0eaSGlenn Lagasse 		/* Tokenize line */
1973f169c0eaSGlenn Lagasse 		c = strtok(tline, BE_WHITE_SPACE);
1974f169c0eaSGlenn Lagasse 
1975f169c0eaSGlenn Lagasse 		if (c == NULL) {
1976f169c0eaSGlenn Lagasse 			/* Found empty line, write it out. */
1977f169c0eaSGlenn Lagasse 			(void) fputs(line, new_fp);
1978f169c0eaSGlenn Lagasse 		} else if (c[0] == '#') {
1979f169c0eaSGlenn Lagasse 			/* Found a comment line, write it out. */
1980f169c0eaSGlenn Lagasse 			(void) fputs(line, new_fp);
1981f169c0eaSGlenn Lagasse 		} else if (strcmp(c, "title") == 0) {
1982f169c0eaSGlenn Lagasse 			char *name = NULL;
1983f169c0eaSGlenn Lagasse 			char *desc = NULL;
1984f169c0eaSGlenn Lagasse 
1985f169c0eaSGlenn Lagasse 			/*
1986f169c0eaSGlenn Lagasse 			 * Found a 'title' line, parse out BE name or
1987f169c0eaSGlenn Lagasse 			 * the description.
1988f169c0eaSGlenn Lagasse 			 */
1989f169c0eaSGlenn Lagasse 			name = strtok(NULL, BE_WHITE_SPACE);
1990f169c0eaSGlenn Lagasse 
1991f169c0eaSGlenn Lagasse 			if (name == NULL) {
1992f169c0eaSGlenn Lagasse 				/*
1993f169c0eaSGlenn Lagasse 				 * Nothing after 'title', just push
1994f169c0eaSGlenn Lagasse 				 * this line through
1995f169c0eaSGlenn Lagasse 				 */
1996f169c0eaSGlenn Lagasse 				(void) fputs(line, new_fp);
1997f169c0eaSGlenn Lagasse 			} else {
1998f169c0eaSGlenn Lagasse 				/*
1999f169c0eaSGlenn Lagasse 				 * Grab the remainder of the title which
2000f169c0eaSGlenn Lagasse 				 * could be a multi worded description
2001f169c0eaSGlenn Lagasse 				 */
2002f169c0eaSGlenn Lagasse 				desc = strtok(NULL, "\n");
2003f169c0eaSGlenn Lagasse 
2004f169c0eaSGlenn Lagasse 				if (strcmp(name, be_orig_name) == 0) {
2005f169c0eaSGlenn Lagasse 					/*
2006f169c0eaSGlenn Lagasse 					 * The first token of the title is
2007f169c0eaSGlenn Lagasse 					 * the old BE name, replace it with
2008f169c0eaSGlenn Lagasse 					 * the new one, and write it out
2009f169c0eaSGlenn Lagasse 					 * along with the remainder of
2010f169c0eaSGlenn Lagasse 					 * description if there is one.
2011f169c0eaSGlenn Lagasse 					 */
2012f169c0eaSGlenn Lagasse 					if (desc) {
2013f169c0eaSGlenn Lagasse 						(void) snprintf(new_line,
2014f169c0eaSGlenn Lagasse 						    sizeof (new_line),
2015f169c0eaSGlenn Lagasse 						    "title %s %s\n",
2016f169c0eaSGlenn Lagasse 						    be_new_name, desc);
2017f169c0eaSGlenn Lagasse 					} else {
2018f169c0eaSGlenn Lagasse 						(void) snprintf(new_line,
2019f169c0eaSGlenn Lagasse 						    sizeof (new_line),
2020f169c0eaSGlenn Lagasse 						    "title %s\n", be_new_name);
2021f169c0eaSGlenn Lagasse 					}
2022f169c0eaSGlenn Lagasse 
2023f169c0eaSGlenn Lagasse 					(void) fputs(new_line, new_fp);
2024f169c0eaSGlenn Lagasse 				} else {
2025f169c0eaSGlenn Lagasse 					(void) fputs(line, new_fp);
2026f169c0eaSGlenn Lagasse 				}
2027f169c0eaSGlenn Lagasse 			}
2028f169c0eaSGlenn Lagasse 		} else if (strcmp(c, "bootfs") == 0) {
2029f169c0eaSGlenn Lagasse 			/*
2030f169c0eaSGlenn Lagasse 			 * Found a 'bootfs' line, parse out the BE root
2031f169c0eaSGlenn Lagasse 			 * dataset value.
2032f169c0eaSGlenn Lagasse 			 */
2033f169c0eaSGlenn Lagasse 			char *root_ds = strtok(NULL, BE_WHITE_SPACE);
2034f169c0eaSGlenn Lagasse 
2035f169c0eaSGlenn Lagasse 			if (root_ds == NULL) {
2036f169c0eaSGlenn Lagasse 				/*
2037f169c0eaSGlenn Lagasse 				 * Nothing after 'bootfs', just push
2038f169c0eaSGlenn Lagasse 				 * this line through
2039f169c0eaSGlenn Lagasse 				 */
2040f169c0eaSGlenn Lagasse 				(void) fputs(line, new_fp);
2041f169c0eaSGlenn Lagasse 			} else {
2042f169c0eaSGlenn Lagasse 				/*
2043f169c0eaSGlenn Lagasse 				 * If this bootfs is the one we're renaming,
2044f169c0eaSGlenn Lagasse 				 * write out the new root dataset value
2045f169c0eaSGlenn Lagasse 				 */
2046f169c0eaSGlenn Lagasse 				if (strcmp(root_ds, be_root_ds) == 0) {
2047f169c0eaSGlenn Lagasse 					(void) snprintf(new_line,
2048f169c0eaSGlenn Lagasse 					    sizeof (new_line), "bootfs %s\n",
2049f169c0eaSGlenn Lagasse 					    be_new_root_ds);
2050f169c0eaSGlenn Lagasse 
2051f169c0eaSGlenn Lagasse 					(void) fputs(new_line, new_fp);
2052f169c0eaSGlenn Lagasse 				} else {
2053f169c0eaSGlenn Lagasse 					(void) fputs(line, new_fp);
2054f169c0eaSGlenn Lagasse 				}
2055f169c0eaSGlenn Lagasse 			}
2056f169c0eaSGlenn Lagasse 		} else {
2057f169c0eaSGlenn Lagasse 			/*
2058f169c0eaSGlenn Lagasse 			 * Found some other line we don't care
2059f169c0eaSGlenn Lagasse 			 * about, write it out.
2060f169c0eaSGlenn Lagasse 			 */
2061f169c0eaSGlenn Lagasse 			(void) fputs(line, new_fp);
2062f169c0eaSGlenn Lagasse 		}
2063f169c0eaSGlenn Lagasse 	}
2064f169c0eaSGlenn Lagasse 
2065f169c0eaSGlenn Lagasse 	(void) fclose(menu_fp);
2066f169c0eaSGlenn Lagasse 	(void) fclose(new_fp);
2067f169c0eaSGlenn Lagasse 	(void) close(tmp_fd);
2068f169c0eaSGlenn Lagasse 
2069f169c0eaSGlenn Lagasse 	if (rename(temp_menu, menu_file) != 0) {
2070f169c0eaSGlenn Lagasse 		err = errno;
2071f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_update_menu: "
2072f169c0eaSGlenn Lagasse 		    "failed to rename file %s to %s: %s\n"),
2073f169c0eaSGlenn Lagasse 		    temp_menu, menu_file, strerror(err));
2074f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
2075f169c0eaSGlenn Lagasse 	}
2076f169c0eaSGlenn Lagasse 	free(temp_menu);
2077f169c0eaSGlenn Lagasse 
2078f169c0eaSGlenn Lagasse 	/* Set the perms and ownership of the updated file */
2079f169c0eaSGlenn Lagasse 	if (chmod(menu_file, sb.st_mode) != 0) {
2080f169c0eaSGlenn Lagasse 		err = errno;
2081f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_update_menu: "
2082f169c0eaSGlenn Lagasse 		    "failed to chmod %s: %s\n"), menu_file, strerror(err));
2083f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
2084f169c0eaSGlenn Lagasse 		goto cleanup;
2085f169c0eaSGlenn Lagasse 	}
2086f169c0eaSGlenn Lagasse 	if (chown(menu_file, sb.st_uid, sb.st_gid) != 0) {
2087f169c0eaSGlenn Lagasse 		err = errno;
2088f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_update_menu: "
2089f169c0eaSGlenn Lagasse 		    "failed to chown %s: %s\n"), menu_file, strerror(err));
2090f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
2091f169c0eaSGlenn Lagasse 	}
2092f169c0eaSGlenn Lagasse 
2093f169c0eaSGlenn Lagasse cleanup:
2094f169c0eaSGlenn Lagasse 	if (pool_mounted) {
2095f169c0eaSGlenn Lagasse 		int err = BE_SUCCESS;
2096f169c0eaSGlenn Lagasse 		err = be_unmount_pool(zhp, ptmp_mntpnt, orig_mntpnt);
2097f169c0eaSGlenn Lagasse 		if (ret == BE_SUCCESS)
2098f169c0eaSGlenn Lagasse 			ret = err;
2099f169c0eaSGlenn Lagasse 		free(orig_mntpnt);
2100f169c0eaSGlenn Lagasse 		free(ptmp_mntpnt);
2101f169c0eaSGlenn Lagasse 	}
2102f169c0eaSGlenn Lagasse 	ZFS_CLOSE(zhp);
2103f169c0eaSGlenn Lagasse 	return (ret);
2104f169c0eaSGlenn Lagasse }
2105f169c0eaSGlenn Lagasse 
2106f169c0eaSGlenn Lagasse /*
2107f169c0eaSGlenn Lagasse  * Function:	be_has_menu_entry
2108f169c0eaSGlenn Lagasse  * Description:	Checks to see if the BEs root dataset has an entry in the grub
2109f169c0eaSGlenn Lagasse  *		menu.
2110f169c0eaSGlenn Lagasse  * Parameters:
2111f169c0eaSGlenn Lagasse  *		be_dataset - The root dataset of the BE
2112f169c0eaSGlenn Lagasse  *		be_root_pool - The pool which contains the boot menu
2113f169c0eaSGlenn Lagasse  *		entry - A pointer the the entry number of the BE if found.
2114f169c0eaSGlenn Lagasse  * Returns:
2115f169c0eaSGlenn Lagasse  *		B_TRUE - Success
2116f169c0eaSGlenn Lagasse  *		B_FALSE - Failure
2117f169c0eaSGlenn Lagasse  * Scope:
2118f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
2119f169c0eaSGlenn Lagasse  */
2120f169c0eaSGlenn Lagasse boolean_t
be_has_menu_entry(char * be_dataset,char * be_root_pool,int * entry)2121f169c0eaSGlenn Lagasse be_has_menu_entry(char *be_dataset, char *be_root_pool, int *entry)
2122f169c0eaSGlenn Lagasse {
2123f169c0eaSGlenn Lagasse 	zfs_handle_t *zhp = NULL;
2124f169c0eaSGlenn Lagasse 	char		menu_file[MAXPATHLEN];
2125bb771288SToomas Soome 	FILE		*menu_fp = NULL;
2126f169c0eaSGlenn Lagasse 	char		line[BUFSIZ];
2127f169c0eaSGlenn Lagasse 	char		*last;
2128f169c0eaSGlenn Lagasse 	char		*rpool_mntpnt = NULL;
2129f169c0eaSGlenn Lagasse 	char		*ptmp_mntpnt = NULL;
2130f169c0eaSGlenn Lagasse 	char		*orig_mntpnt = NULL;
2131f169c0eaSGlenn Lagasse 	int		ent_num = 0;
2132f169c0eaSGlenn Lagasse 	boolean_t	ret = 0;
2133f169c0eaSGlenn Lagasse 	boolean_t	pool_mounted = B_FALSE;
2134f169c0eaSGlenn Lagasse 
2135f169c0eaSGlenn Lagasse 
2136f169c0eaSGlenn Lagasse 	/*
2137f169c0eaSGlenn Lagasse 	 * Check to see if this system supports grub
2138f169c0eaSGlenn Lagasse 	 */
2139f169c0eaSGlenn Lagasse 	if ((zhp = zfs_open(g_zfs, be_root_pool, ZFS_TYPE_DATASET)) == NULL) {
2140f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_has_menu_entry: failed to open "
2141f169c0eaSGlenn Lagasse 		    "pool dataset for %s: %s\n"), be_root_pool,
2142f169c0eaSGlenn Lagasse 		    libzfs_error_description(g_zfs));
2143f169c0eaSGlenn Lagasse 		return (B_FALSE);
2144f169c0eaSGlenn Lagasse 	}
2145f169c0eaSGlenn Lagasse 
2146f169c0eaSGlenn Lagasse 	/*
2147f169c0eaSGlenn Lagasse 	 * Check to see if the pool's dataset is mounted. If it isn't we'll
2148f169c0eaSGlenn Lagasse 	 * attempt to mount it.
2149f169c0eaSGlenn Lagasse 	 */
2150f169c0eaSGlenn Lagasse 	if (be_mount_pool(zhp, &ptmp_mntpnt, &orig_mntpnt,
2151f169c0eaSGlenn Lagasse 	    &pool_mounted) != 0) {
2152f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_has_menu_entry: pool dataset "
2153f169c0eaSGlenn Lagasse 		    "(%s) could not be mounted\n"), be_root_pool);
2154f169c0eaSGlenn Lagasse 		ZFS_CLOSE(zhp);
2155f169c0eaSGlenn Lagasse 		return (B_FALSE);
2156f169c0eaSGlenn Lagasse 	}
2157f169c0eaSGlenn Lagasse 
2158f169c0eaSGlenn Lagasse 	/*
2159f169c0eaSGlenn Lagasse 	 * Get the mountpoint for the root pool dataset.
2160f169c0eaSGlenn Lagasse 	 */
2161f169c0eaSGlenn Lagasse 	if (!zfs_is_mounted(zhp, &rpool_mntpnt)) {
2162f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_has_menu_entry: pool "
2163f169c0eaSGlenn Lagasse 		    "dataset (%s) is not mounted. Can't set "
2164f169c0eaSGlenn Lagasse 		    "the default BE in the grub menu.\n"), be_root_pool);
2165f169c0eaSGlenn Lagasse 		ret = B_FALSE;
2166f169c0eaSGlenn Lagasse 		goto cleanup;
2167f169c0eaSGlenn Lagasse 	}
2168f169c0eaSGlenn Lagasse 
2169f169c0eaSGlenn Lagasse 	if (be_has_grub()) {
2170f169c0eaSGlenn Lagasse 		(void) snprintf(menu_file, MAXPATHLEN, "/%s%s",
2171f169c0eaSGlenn Lagasse 		    rpool_mntpnt, BE_GRUB_MENU);
2172f169c0eaSGlenn Lagasse 	} else {
2173f169c0eaSGlenn Lagasse 		(void) snprintf(menu_file, MAXPATHLEN, "/%s%s",
2174f169c0eaSGlenn Lagasse 		    rpool_mntpnt, BE_SPARC_MENU);
2175f169c0eaSGlenn Lagasse 	}
2176f169c0eaSGlenn Lagasse 
2177de1ab35cSAlexander Eremin 	if (be_open_menu(be_root_pool, menu_file, &menu_fp, "r",
2178f169c0eaSGlenn Lagasse 	    B_FALSE) != 0) {
2179f169c0eaSGlenn Lagasse 		ret = B_FALSE;
2180f169c0eaSGlenn Lagasse 		goto cleanup;
2181f169c0eaSGlenn Lagasse 	} else if (menu_fp == NULL) {
2182f169c0eaSGlenn Lagasse 		ret = B_FALSE;
2183f169c0eaSGlenn Lagasse 		goto cleanup;
2184f169c0eaSGlenn Lagasse 	}
2185f169c0eaSGlenn Lagasse 
2186f169c0eaSGlenn Lagasse 	free(rpool_mntpnt);
2187f169c0eaSGlenn Lagasse 	rpool_mntpnt = NULL;
2188f169c0eaSGlenn Lagasse 
2189f169c0eaSGlenn Lagasse 	while (fgets(line, BUFSIZ, menu_fp)) {
2190f169c0eaSGlenn Lagasse 		char *tok = strtok_r(line, BE_WHITE_SPACE, &last);
2191f169c0eaSGlenn Lagasse 
2192f169c0eaSGlenn Lagasse 		if (tok != NULL && tok[0] != '#') {
2193f169c0eaSGlenn Lagasse 			if (strcmp(tok, "bootfs") == 0) {
2194f169c0eaSGlenn Lagasse 				tok = strtok_r(last, BE_WHITE_SPACE, &last);
2195f169c0eaSGlenn Lagasse 				if (tok != NULL && strcmp(tok,
2196f169c0eaSGlenn Lagasse 				    be_dataset) == 0) {
2197f169c0eaSGlenn Lagasse 					/*
2198f169c0eaSGlenn Lagasse 					 * The entry number needs to be
2199f169c0eaSGlenn Lagasse 					 * decremented here because the title
2200f169c0eaSGlenn Lagasse 					 * will always be the first line for
2201f169c0eaSGlenn Lagasse 					 * an entry. Because of this we'll
2202f169c0eaSGlenn Lagasse 					 * always be off by one entry when we
2203f169c0eaSGlenn Lagasse 					 * check for bootfs.
2204f169c0eaSGlenn Lagasse 					 */
2205f169c0eaSGlenn Lagasse 					*entry = ent_num - 1;
2206f169c0eaSGlenn Lagasse 					ret = B_TRUE;
2207f169c0eaSGlenn Lagasse 					goto cleanup;
2208f169c0eaSGlenn Lagasse 				}
2209f169c0eaSGlenn Lagasse 			} else if (strcmp(tok, "title") == 0)
2210f169c0eaSGlenn Lagasse 				ent_num++;
2211f169c0eaSGlenn Lagasse 		}
2212f169c0eaSGlenn Lagasse 	}
2213f169c0eaSGlenn Lagasse 
2214f169c0eaSGlenn Lagasse cleanup:
2215f169c0eaSGlenn Lagasse 	if (pool_mounted) {
2216f169c0eaSGlenn Lagasse 		(void) be_unmount_pool(zhp, ptmp_mntpnt, orig_mntpnt);
2217f169c0eaSGlenn Lagasse 		free(orig_mntpnt);
2218f169c0eaSGlenn Lagasse 		free(ptmp_mntpnt);
2219f169c0eaSGlenn Lagasse 	}
2220f169c0eaSGlenn Lagasse 	ZFS_CLOSE(zhp);
2221f169c0eaSGlenn Lagasse 	(void) fclose(menu_fp);
2222f169c0eaSGlenn Lagasse 	return (ret);
2223f169c0eaSGlenn Lagasse }
2224f169c0eaSGlenn Lagasse 
2225f169c0eaSGlenn Lagasse /*
2226f169c0eaSGlenn Lagasse  * Function:	be_update_vfstab
2227f169c0eaSGlenn Lagasse  * Description:	This function digs into a BE's vfstab and updates all
2228f169c0eaSGlenn Lagasse  *		entries with file systems listed in be_fs_list_data_t.
2229f169c0eaSGlenn Lagasse  *		The entry's root container dataset and be_name will be
2230f169c0eaSGlenn Lagasse  *		updated with the parameters passed in.
2231f169c0eaSGlenn Lagasse  * Parameters:
2232f169c0eaSGlenn Lagasse  *		be_name - name of BE to update
2233f169c0eaSGlenn Lagasse  *		old_rc_loc - dataset under which the root container dataset
2234f169c0eaSGlenn Lagasse  *			of the old BE resides in.
2235f169c0eaSGlenn Lagasse  *		new_rc_loc - dataset under which the root container dataset
2236f169c0eaSGlenn Lagasse  *			of the new BE resides in.
2237f169c0eaSGlenn Lagasse  *		fld - be_fs_list_data_t pointer providing the list of
2238f169c0eaSGlenn Lagasse  *			file systems to look for in vfstab.
2239f169c0eaSGlenn Lagasse  *		mountpoint - directory of where BE is currently mounted.
2240f169c0eaSGlenn Lagasse  *			If NULL, then BE is not currently mounted.
2241f169c0eaSGlenn Lagasse  * Returns:
2242f169c0eaSGlenn Lagasse  *		BE_SUCCESS - Success
2243f169c0eaSGlenn Lagasse  *		be_errno_t - Failure
2244f169c0eaSGlenn Lagasse  * Scope:
2245f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
2246f169c0eaSGlenn Lagasse  */
2247f169c0eaSGlenn Lagasse int
be_update_vfstab(char * be_name,char * old_rc_loc,char * new_rc_loc,be_fs_list_data_t * fld,char * mountpoint)2248f169c0eaSGlenn Lagasse be_update_vfstab(char *be_name, char *old_rc_loc, char *new_rc_loc,
2249f169c0eaSGlenn Lagasse     be_fs_list_data_t *fld, char *mountpoint)
2250f169c0eaSGlenn Lagasse {
2251f169c0eaSGlenn Lagasse 	char		*tmp_mountpoint = NULL;
2252f169c0eaSGlenn Lagasse 	char		alt_vfstab[MAXPATHLEN];
2253f169c0eaSGlenn Lagasse 	int		ret = BE_SUCCESS, err = BE_SUCCESS;
2254f169c0eaSGlenn Lagasse 
2255f169c0eaSGlenn Lagasse 	if (fld == NULL || fld->fs_list == NULL || fld->fs_num == 0)
2256f169c0eaSGlenn Lagasse 		return (BE_SUCCESS);
2257f169c0eaSGlenn Lagasse 
2258f169c0eaSGlenn Lagasse 	/* If BE not already mounted, mount the BE */
2259f169c0eaSGlenn Lagasse 	if (mountpoint == NULL) {
2260f169c0eaSGlenn Lagasse 		if ((ret = _be_mount(be_name, &tmp_mountpoint,
2261f169c0eaSGlenn Lagasse 		    BE_MOUNT_FLAG_NO_ZONES)) != BE_SUCCESS) {
2262f169c0eaSGlenn Lagasse 			be_print_err(gettext("be_update_vfstab: "
2263f169c0eaSGlenn Lagasse 			    "failed to mount BE (%s)\n"), be_name);
2264f169c0eaSGlenn Lagasse 			return (ret);
2265f169c0eaSGlenn Lagasse 		}
2266f169c0eaSGlenn Lagasse 	} else {
2267f169c0eaSGlenn Lagasse 		tmp_mountpoint = mountpoint;
2268f169c0eaSGlenn Lagasse 	}
2269f169c0eaSGlenn Lagasse 
2270f169c0eaSGlenn Lagasse 	/* Get string for vfstab in the mounted BE. */
2271f169c0eaSGlenn Lagasse 	(void) snprintf(alt_vfstab, sizeof (alt_vfstab), "%s/etc/vfstab",
2272f169c0eaSGlenn Lagasse 	    tmp_mountpoint);
2273f169c0eaSGlenn Lagasse 
2274f169c0eaSGlenn Lagasse 	/* Update the vfstab */
2275f169c0eaSGlenn Lagasse 	ret = _update_vfstab(alt_vfstab, be_name, old_rc_loc, new_rc_loc,
2276f169c0eaSGlenn Lagasse 	    fld);
2277f169c0eaSGlenn Lagasse 
2278f169c0eaSGlenn Lagasse 	/* Unmount BE if we mounted it */
2279f169c0eaSGlenn Lagasse 	if (mountpoint == NULL) {
2280f169c0eaSGlenn Lagasse 		if ((err = _be_unmount(be_name, 0)) == BE_SUCCESS) {
2281f169c0eaSGlenn Lagasse 			/* Remove temporary mountpoint */
2282f169c0eaSGlenn Lagasse 			(void) rmdir(tmp_mountpoint);
2283f169c0eaSGlenn Lagasse 		} else {
2284f169c0eaSGlenn Lagasse 			be_print_err(gettext("be_update_vfstab: "
2285f169c0eaSGlenn Lagasse 			    "failed to unmount BE %s mounted at %s\n"),
2286f169c0eaSGlenn Lagasse 			    be_name, tmp_mountpoint);
2287f169c0eaSGlenn Lagasse 			if (ret == BE_SUCCESS)
2288f169c0eaSGlenn Lagasse 				ret = err;
2289f169c0eaSGlenn Lagasse 		}
2290f169c0eaSGlenn Lagasse 
2291f169c0eaSGlenn Lagasse 		free(tmp_mountpoint);
2292f169c0eaSGlenn Lagasse 	}
2293f169c0eaSGlenn Lagasse 
2294f169c0eaSGlenn Lagasse 	return (ret);
2295f169c0eaSGlenn Lagasse }
2296f169c0eaSGlenn Lagasse 
2297f169c0eaSGlenn Lagasse /*
2298f169c0eaSGlenn Lagasse  * Function:	be_update_zone_vfstab
2299f169c0eaSGlenn Lagasse  * Description:	This function digs into a zone BE's vfstab and updates all
2300f169c0eaSGlenn Lagasse  *		entries with file systems listed in be_fs_list_data_t.
2301f169c0eaSGlenn Lagasse  *		The entry's root container dataset and be_name will be
2302f169c0eaSGlenn Lagasse  *		updated with the parameters passed in.
2303f169c0eaSGlenn Lagasse  * Parameters:
2304f169c0eaSGlenn Lagasse  *		zhp - zfs_handle_t pointer to zone root dataset.
2305f169c0eaSGlenn Lagasse  *		be_name - name of zone BE to update
2306f169c0eaSGlenn Lagasse  *		old_rc_loc - dataset under which the root container dataset
2307f169c0eaSGlenn Lagasse  *			of the old zone BE resides in.
2308f169c0eaSGlenn Lagasse  *		new_rc_loc - dataset under which the root container dataset
2309f169c0eaSGlenn Lagasse  *			of the new zone BE resides in.
2310f169c0eaSGlenn Lagasse  *		fld - be_fs_list_data_t pointer providing the list of
2311f169c0eaSGlenn Lagasse  *			file systems to look for in vfstab.
2312f169c0eaSGlenn Lagasse  * Returns:
2313f169c0eaSGlenn Lagasse  *		BE_SUCCESS - Success
2314f169c0eaSGlenn Lagasse  *		be_errno_t - Failure
2315f169c0eaSGlenn Lagasse  * Scope:
2316f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
2317f169c0eaSGlenn Lagasse  */
2318f169c0eaSGlenn Lagasse int
be_update_zone_vfstab(zfs_handle_t * zhp,char * be_name,char * old_rc_loc,char * new_rc_loc,be_fs_list_data_t * fld)2319f169c0eaSGlenn Lagasse be_update_zone_vfstab(zfs_handle_t *zhp, char *be_name, char *old_rc_loc,
2320f169c0eaSGlenn Lagasse     char *new_rc_loc, be_fs_list_data_t *fld)
2321f169c0eaSGlenn Lagasse {
2322f169c0eaSGlenn Lagasse 	be_mount_data_t		md = { 0 };
2323f169c0eaSGlenn Lagasse 	be_unmount_data_t	ud = { 0 };
2324f169c0eaSGlenn Lagasse 	char			alt_vfstab[MAXPATHLEN];
2325f169c0eaSGlenn Lagasse 	boolean_t		mounted_here = B_FALSE;
2326f169c0eaSGlenn Lagasse 	int			ret = BE_SUCCESS;
2327f169c0eaSGlenn Lagasse 
2328f169c0eaSGlenn Lagasse 	/*
2329f169c0eaSGlenn Lagasse 	 * If zone root not already mounted, mount it at a
2330f169c0eaSGlenn Lagasse 	 * temporary location.
2331f169c0eaSGlenn Lagasse 	 */
2332f169c0eaSGlenn Lagasse 	if (!zfs_is_mounted(zhp, &md.altroot)) {
2333f169c0eaSGlenn Lagasse 		/* Generate temporary mountpoint to mount zone root */
2334f169c0eaSGlenn Lagasse 		if ((ret = be_make_tmp_mountpoint(&md.altroot)) != BE_SUCCESS) {
2335f169c0eaSGlenn Lagasse 			be_print_err(gettext("be_update_zone_vfstab: "
2336f169c0eaSGlenn Lagasse 			    "failed to make temporary mountpoint to "
2337f169c0eaSGlenn Lagasse 			    "mount zone root\n"));
2338f169c0eaSGlenn Lagasse 			return (ret);
2339f169c0eaSGlenn Lagasse 		}
2340f169c0eaSGlenn Lagasse 
2341f169c0eaSGlenn Lagasse 		if (be_mount_zone_root(zhp, &md) != BE_SUCCESS) {
2342f169c0eaSGlenn Lagasse 			be_print_err(gettext("be_update_zone_vfstab: "
2343f169c0eaSGlenn Lagasse 			    "failed to mount zone root %s\n"),
2344f169c0eaSGlenn Lagasse 			    zfs_get_name(zhp));
2345f169c0eaSGlenn Lagasse 			free(md.altroot);
2346f169c0eaSGlenn Lagasse 			return (BE_ERR_MOUNT_ZONEROOT);
2347f169c0eaSGlenn Lagasse 		}
2348f169c0eaSGlenn Lagasse 		mounted_here = B_TRUE;
2349f169c0eaSGlenn Lagasse 	}
2350f169c0eaSGlenn Lagasse 
2351f169c0eaSGlenn Lagasse 	/* Get string from vfstab in the mounted zone BE */
2352f169c0eaSGlenn Lagasse 	(void) snprintf(alt_vfstab, sizeof (alt_vfstab), "%s/etc/vfstab",
2353f169c0eaSGlenn Lagasse 	    md.altroot);
2354f169c0eaSGlenn Lagasse 
2355f169c0eaSGlenn Lagasse 	/* Update the vfstab */
2356f169c0eaSGlenn Lagasse 	ret = _update_vfstab(alt_vfstab, be_name, old_rc_loc, new_rc_loc,
2357f169c0eaSGlenn Lagasse 	    fld);
2358f169c0eaSGlenn Lagasse 
2359f169c0eaSGlenn Lagasse 	/* Unmount zone root if we mounted it */
2360f169c0eaSGlenn Lagasse 	if (mounted_here) {
2361f169c0eaSGlenn Lagasse 		ud.force = B_TRUE;
2362f169c0eaSGlenn Lagasse 
2363f169c0eaSGlenn Lagasse 		if (be_unmount_zone_root(zhp, &ud) == BE_SUCCESS) {
2364f169c0eaSGlenn Lagasse 			/* Remove the temporary mountpoint */
2365f169c0eaSGlenn Lagasse 			(void) rmdir(md.altroot);
2366f169c0eaSGlenn Lagasse 		} else {
2367f169c0eaSGlenn Lagasse 			be_print_err(gettext("be_update_zone_vfstab: "
2368f169c0eaSGlenn Lagasse 			    "failed to unmount zone root %s from %s\n"),
2369f169c0eaSGlenn Lagasse 			    zfs_get_name(zhp), md.altroot);
2370f169c0eaSGlenn Lagasse 			if (ret == 0)
2371f169c0eaSGlenn Lagasse 				ret = BE_ERR_UMOUNT_ZONEROOT;
2372f169c0eaSGlenn Lagasse 		}
2373f169c0eaSGlenn Lagasse 	}
2374f169c0eaSGlenn Lagasse 
2375f169c0eaSGlenn Lagasse 	free(md.altroot);
2376f169c0eaSGlenn Lagasse 	return (ret);
2377f169c0eaSGlenn Lagasse }
2378f169c0eaSGlenn Lagasse 
2379f169c0eaSGlenn Lagasse /*
2380f169c0eaSGlenn Lagasse  * Function:	be_auto_snap_name
2381f169c0eaSGlenn Lagasse  * Description:	Generate an auto snapshot name constructed based on the
2382f169c0eaSGlenn Lagasse  *		current date and time.  The auto snapshot name is of the form:
2383f169c0eaSGlenn Lagasse  *
2384f169c0eaSGlenn Lagasse  *			<date>-<time>
2385f169c0eaSGlenn Lagasse  *
2386f169c0eaSGlenn Lagasse  *		where <date> is in ISO standard format, so the resultant name
2387f169c0eaSGlenn Lagasse  *		is of the form:
2388f169c0eaSGlenn Lagasse  *
2389f169c0eaSGlenn Lagasse  *			%Y-%m-%d-%H:%M:%S
2390f169c0eaSGlenn Lagasse  *
2391f169c0eaSGlenn Lagasse  * Parameters:
2392f169c0eaSGlenn Lagasse  *		None
2393f169c0eaSGlenn Lagasse  * Returns:
2394f169c0eaSGlenn Lagasse  *		Success - pointer to auto generated snapshot name.  The name
2395f169c0eaSGlenn Lagasse  *			is allocated in heap storage so the caller is
2396f169c0eaSGlenn Lagasse  *			responsible for free'ing the name.
2397f169c0eaSGlenn Lagasse  *		Failure - NULL
2398f169c0eaSGlenn Lagasse  * Scope:
2399f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
2400f169c0eaSGlenn Lagasse  */
2401f169c0eaSGlenn Lagasse char *
be_auto_snap_name(void)2402f169c0eaSGlenn Lagasse be_auto_snap_name(void)
2403f169c0eaSGlenn Lagasse {
240447f78bf4SToomas Soome 	time_t		utc_tm = 0;
2405f169c0eaSGlenn Lagasse 	struct tm	*gmt_tm = NULL;
2406f169c0eaSGlenn Lagasse 	char		gmt_time_str[64];
2407f169c0eaSGlenn Lagasse 	char		*auto_snap_name = NULL;
2408f169c0eaSGlenn Lagasse 
2409f169c0eaSGlenn Lagasse 	if (time(&utc_tm) == -1) {
2410f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_auto_snap_name: time() failed\n"));
2411f169c0eaSGlenn Lagasse 		return (NULL);
2412f169c0eaSGlenn Lagasse 	}
2413f169c0eaSGlenn Lagasse 
2414f169c0eaSGlenn Lagasse 	if ((gmt_tm = gmtime(&utc_tm)) == NULL) {
2415f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_auto_snap_name: gmtime() failed\n"));
2416f169c0eaSGlenn Lagasse 		return (NULL);
2417f169c0eaSGlenn Lagasse 	}
2418f169c0eaSGlenn Lagasse 
2419f169c0eaSGlenn Lagasse 	(void) strftime(gmt_time_str, sizeof (gmt_time_str), "%F-%T", gmt_tm);
2420f169c0eaSGlenn Lagasse 
2421f169c0eaSGlenn Lagasse 	if ((auto_snap_name = strdup(gmt_time_str)) == NULL) {
2422f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_auto_snap_name: "
2423f169c0eaSGlenn Lagasse 		    "memory allocation failed\n"));
2424f169c0eaSGlenn Lagasse 		return (NULL);
2425f169c0eaSGlenn Lagasse 	}
2426f169c0eaSGlenn Lagasse 
2427f169c0eaSGlenn Lagasse 	return (auto_snap_name);
2428f169c0eaSGlenn Lagasse }
2429f169c0eaSGlenn Lagasse 
2430f169c0eaSGlenn Lagasse /*
2431f169c0eaSGlenn Lagasse  * Function:	be_auto_be_name
2432f169c0eaSGlenn Lagasse  * Description:	Generate an auto BE name constructed based on the BE name
2433f169c0eaSGlenn Lagasse  *		of the original BE being cloned.
2434f169c0eaSGlenn Lagasse  * Parameters:
2435f169c0eaSGlenn Lagasse  *		obe_name - name of the original BE being cloned.
2436f169c0eaSGlenn Lagasse  * Returns:
2437f169c0eaSGlenn Lagasse  *		Success - pointer to auto generated BE name.  The name
2438f169c0eaSGlenn Lagasse  *			is allocated in heap storage so the caller is
2439f169c0eaSGlenn Lagasse  *			responsible for free'ing the name.
2440f169c0eaSGlenn Lagasse  *		Failure - NULL
2441f169c0eaSGlenn Lagasse  * Scope:
2442f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
2443f169c0eaSGlenn Lagasse  */
2444f169c0eaSGlenn Lagasse char *
be_auto_be_name(char * obe_name)2445f169c0eaSGlenn Lagasse be_auto_be_name(char *obe_name)
2446f169c0eaSGlenn Lagasse {
2447f169c0eaSGlenn Lagasse 	return (be_get_auto_name(obe_name, NULL, B_FALSE));
2448f169c0eaSGlenn Lagasse }
2449f169c0eaSGlenn Lagasse 
2450f169c0eaSGlenn Lagasse /*
2451f169c0eaSGlenn Lagasse  * Function:	be_auto_zone_be_name
2452f169c0eaSGlenn Lagasse  * Description:	Generate an auto BE name for a zone constructed based on
2453f169c0eaSGlenn Lagasse  *              the BE name of the original zone BE being cloned.
2454f169c0eaSGlenn Lagasse  * Parameters:
2455f169c0eaSGlenn Lagasse  *              container_ds - container dataset for the zone.
2456f169c0eaSGlenn Lagasse  *		zbe_name - name of the original zone BE being cloned.
2457f169c0eaSGlenn Lagasse  * Returns:
2458f169c0eaSGlenn Lagasse  *		Success - pointer to auto generated BE name.  The name
2459f169c0eaSGlenn Lagasse  *			is allocated in heap storage so the caller is
2460f169c0eaSGlenn Lagasse  *			responsible for free'ing the name.
2461f169c0eaSGlenn Lagasse  *		Failure - NULL
2462f169c0eaSGlenn Lagasse  * Scope:
2463f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
2464f169c0eaSGlenn Lagasse  */
2465f169c0eaSGlenn Lagasse char *
be_auto_zone_be_name(char * container_ds,char * zbe_name)2466f169c0eaSGlenn Lagasse be_auto_zone_be_name(char *container_ds, char *zbe_name)
2467f169c0eaSGlenn Lagasse {
2468f169c0eaSGlenn Lagasse 	return (be_get_auto_name(zbe_name, container_ds, B_TRUE));
2469f169c0eaSGlenn Lagasse }
2470f169c0eaSGlenn Lagasse 
2471f169c0eaSGlenn Lagasse /*
2472f169c0eaSGlenn Lagasse  * Function:	be_valid_be_name
2473f169c0eaSGlenn Lagasse  * Description:	Validates a BE name.
2474f169c0eaSGlenn Lagasse  * Parameters:
2475f169c0eaSGlenn Lagasse  *		be_name - name of BE to validate
2476f169c0eaSGlenn Lagasse  * Returns:
2477f169c0eaSGlenn Lagasse  *		B_TRUE - be_name is valid
2478f169c0eaSGlenn Lagasse  *		B_FALSE - be_name is invalid
2479f169c0eaSGlenn Lagasse  * Scope:
2480f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
2481f169c0eaSGlenn Lagasse  */
2482f169c0eaSGlenn Lagasse 
2483f169c0eaSGlenn Lagasse boolean_t
be_valid_be_name(const char * be_name)2484f169c0eaSGlenn Lagasse be_valid_be_name(const char *be_name)
2485f169c0eaSGlenn Lagasse {
2486f169c0eaSGlenn Lagasse 	const char	*c = NULL;
2487de1ab35cSAlexander Eremin 	struct be_defaults be_defaults;
2488f169c0eaSGlenn Lagasse 
2489f169c0eaSGlenn Lagasse 	if (be_name == NULL)
2490f169c0eaSGlenn Lagasse 		return (B_FALSE);
2491f169c0eaSGlenn Lagasse 
2492de1ab35cSAlexander Eremin 	be_get_defaults(&be_defaults);
2493de1ab35cSAlexander Eremin 
2494f169c0eaSGlenn Lagasse 	/*
2495f169c0eaSGlenn Lagasse 	 * A BE name must not be a multi-level dataset name.  We also check
2496f169c0eaSGlenn Lagasse 	 * that it does not contain the ' ' and '%' characters.  The ' ' is
2497f169c0eaSGlenn Lagasse 	 * a valid character for datasets, however we don't allow that in a
2498f169c0eaSGlenn Lagasse 	 * BE name.  The '%' is invalid, but zfs_name_valid() allows it for
2499f169c0eaSGlenn Lagasse 	 * internal reasons, so we explicitly check for it here.
2500f169c0eaSGlenn Lagasse 	 */
2501f169c0eaSGlenn Lagasse 	c = be_name;
2502f169c0eaSGlenn Lagasse 	while (*c != '\0' && *c != '/' && *c != ' ' && *c != '%')
2503f169c0eaSGlenn Lagasse 		c++;
2504f169c0eaSGlenn Lagasse 
2505f169c0eaSGlenn Lagasse 	if (*c != '\0')
2506f169c0eaSGlenn Lagasse 		return (B_FALSE);
2507f169c0eaSGlenn Lagasse 
2508f169c0eaSGlenn Lagasse 	/*
2509f169c0eaSGlenn Lagasse 	 * The BE name must comply with a zfs dataset filesystem. We also
2510f169c0eaSGlenn Lagasse 	 * verify its length to be < BE_NAME_MAX_LEN.
2511f169c0eaSGlenn Lagasse 	 */
2512f169c0eaSGlenn Lagasse 	if (!zfs_name_valid(be_name, ZFS_TYPE_FILESYSTEM) ||
2513f169c0eaSGlenn Lagasse 	    strlen(be_name) > BE_NAME_MAX_LEN)
2514f169c0eaSGlenn Lagasse 		return (B_FALSE);
2515f169c0eaSGlenn Lagasse 
2516de1ab35cSAlexander Eremin 	if (be_defaults.be_deflt_bename_starts_with[0] != '\0' &&
2517de1ab35cSAlexander Eremin 	    strstr(be_name, be_defaults.be_deflt_bename_starts_with) == NULL) {
2518de1ab35cSAlexander Eremin 		return (B_FALSE);
2519de1ab35cSAlexander Eremin 	}
2520de1ab35cSAlexander Eremin 
2521f169c0eaSGlenn Lagasse 	return (B_TRUE);
2522f169c0eaSGlenn Lagasse }
2523f169c0eaSGlenn Lagasse 
2524f169c0eaSGlenn Lagasse /*
2525f169c0eaSGlenn Lagasse  * Function:	be_valid_auto_snap_name
2526f169c0eaSGlenn Lagasse  * Description:	This function checks that a snapshot name is a valid auto
2527f169c0eaSGlenn Lagasse  *		generated snapshot name.  A valid auto generated snapshot
2528f169c0eaSGlenn Lagasse  *		name is of the form:
2529f169c0eaSGlenn Lagasse  *
2530f169c0eaSGlenn Lagasse  *			%Y-%m-%d-%H:%M:%S
2531f169c0eaSGlenn Lagasse  *
2532f169c0eaSGlenn Lagasse  *		An older form of the auto generated snapshot name also
2533f169c0eaSGlenn Lagasse  *		included the snapshot's BE cleanup policy and a reserved
2534f169c0eaSGlenn Lagasse  *		field.  Those names will also be verified by this function.
2535f169c0eaSGlenn Lagasse  *
2536f169c0eaSGlenn Lagasse  *		Examples of valid auto snapshot names are:
2537f169c0eaSGlenn Lagasse  *
2538f169c0eaSGlenn Lagasse  *			2008-03-31-18:41:30
2539f169c0eaSGlenn Lagasse  *			2008-03-31-22:17:24
2540f169c0eaSGlenn Lagasse  *			<policy>:-:2008:04-05-09:12:55
2541f169c0eaSGlenn Lagasse  *			<policy>:-:2008:04-06-15:34:12
2542f169c0eaSGlenn Lagasse  *
2543f169c0eaSGlenn Lagasse  * Parameters:
2544f169c0eaSGlenn Lagasse  *		name - name of the snapshot to be validated.
2545f169c0eaSGlenn Lagasse  * Returns:
2546f169c0eaSGlenn Lagasse  *		B_TRUE - the name is a valid auto snapshot name.
2547f169c0eaSGlenn Lagasse  *		B_FALSE - the name is not a valid auto snapshot name.
2548f169c0eaSGlenn Lagasse  * Scope:
2549f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
2550f169c0eaSGlenn Lagasse  */
2551f169c0eaSGlenn Lagasse boolean_t
be_valid_auto_snap_name(char * name)2552f169c0eaSGlenn Lagasse be_valid_auto_snap_name(char *name)
2553f169c0eaSGlenn Lagasse {
2554f169c0eaSGlenn Lagasse 	struct tm gmt_tm;
2555f169c0eaSGlenn Lagasse 
2556f169c0eaSGlenn Lagasse 	char *policy = NULL;
2557f169c0eaSGlenn Lagasse 	char *reserved = NULL;
2558f169c0eaSGlenn Lagasse 	char *date = NULL;
2559f169c0eaSGlenn Lagasse 	char *c = NULL;
2560f169c0eaSGlenn Lagasse 
2561f169c0eaSGlenn Lagasse 	/* Validate the snapshot name by converting it into utc time */
2562f169c0eaSGlenn Lagasse 	if (strptime(name, "%Y-%m-%d-%T", &gmt_tm) != NULL &&
2563f169c0eaSGlenn Lagasse 	    (mktime(&gmt_tm) != -1)) {
2564f169c0eaSGlenn Lagasse 		return (B_TRUE);
2565f169c0eaSGlenn Lagasse 	}
2566f169c0eaSGlenn Lagasse 
2567f169c0eaSGlenn Lagasse 	/*
2568f169c0eaSGlenn Lagasse 	 * Validate the snapshot name against the older form of an
2569f169c0eaSGlenn Lagasse 	 * auto generated snapshot name.
2570f169c0eaSGlenn Lagasse 	 */
2571f169c0eaSGlenn Lagasse 	policy = strdup(name);
2572f169c0eaSGlenn Lagasse 
2573f169c0eaSGlenn Lagasse 	/*
2574f169c0eaSGlenn Lagasse 	 * Get the first field from the snapshot name,
2575f169c0eaSGlenn Lagasse 	 * which is the BE policy
2576f169c0eaSGlenn Lagasse 	 */
2577f169c0eaSGlenn Lagasse 	c = strchr(policy, ':');
2578f169c0eaSGlenn Lagasse 	if (c == NULL) {
2579f169c0eaSGlenn Lagasse 		free(policy);
2580f169c0eaSGlenn Lagasse 		return (B_FALSE);
2581f169c0eaSGlenn Lagasse 	}
2582f169c0eaSGlenn Lagasse 	c[0] = '\0';
2583f169c0eaSGlenn Lagasse 
2584f169c0eaSGlenn Lagasse 	/* Validate the policy name */
2585f169c0eaSGlenn Lagasse 	if (!valid_be_policy(policy)) {
2586f169c0eaSGlenn Lagasse 		free(policy);
2587f169c0eaSGlenn Lagasse 		return (B_FALSE);
2588f169c0eaSGlenn Lagasse 	}
2589f169c0eaSGlenn Lagasse 
2590f169c0eaSGlenn Lagasse 	/* Get the next field, which is the reserved field. */
259147f78bf4SToomas Soome 	if (c[1] == '\0') {
2592f169c0eaSGlenn Lagasse 		free(policy);
2593f169c0eaSGlenn Lagasse 		return (B_FALSE);
2594f169c0eaSGlenn Lagasse 	}
2595f169c0eaSGlenn Lagasse 	reserved = c+1;
2596f169c0eaSGlenn Lagasse 	c = strchr(reserved, ':');
2597f169c0eaSGlenn Lagasse 	if (c == NULL) {
2598f169c0eaSGlenn Lagasse 		free(policy);
2599f169c0eaSGlenn Lagasse 		return (B_FALSE);
2600f169c0eaSGlenn Lagasse 	}
2601f169c0eaSGlenn Lagasse 	c[0] = '\0';
2602f169c0eaSGlenn Lagasse 
2603f169c0eaSGlenn Lagasse 	/* Validate the reserved field */
2604f169c0eaSGlenn Lagasse 	if (strcmp(reserved, "-") != 0) {
2605f169c0eaSGlenn Lagasse 		free(policy);
2606f169c0eaSGlenn Lagasse 		return (B_FALSE);
2607f169c0eaSGlenn Lagasse 	}
2608f169c0eaSGlenn Lagasse 
2609f169c0eaSGlenn Lagasse 	/* The remaining string should be the date field */
261047f78bf4SToomas Soome 	if (c[1] == '\0') {
2611f169c0eaSGlenn Lagasse 		free(policy);
2612f169c0eaSGlenn Lagasse 		return (B_FALSE);
2613f169c0eaSGlenn Lagasse 	}
2614f169c0eaSGlenn Lagasse 	date = c+1;
2615f169c0eaSGlenn Lagasse 
2616f169c0eaSGlenn Lagasse 	/* Validate the date string by converting it into utc time */
2617f169c0eaSGlenn Lagasse 	if (strptime(date, "%Y-%m-%d-%T", &gmt_tm) == NULL ||
2618f169c0eaSGlenn Lagasse 	    (mktime(&gmt_tm) == -1)) {
2619f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_valid_auto_snap_name: "
2620f169c0eaSGlenn Lagasse 		    "invalid auto snapshot name\n"));
2621f169c0eaSGlenn Lagasse 		free(policy);
2622f169c0eaSGlenn Lagasse 		return (B_FALSE);
2623f169c0eaSGlenn Lagasse 	}
2624f169c0eaSGlenn Lagasse 
2625f169c0eaSGlenn Lagasse 	free(policy);
2626f169c0eaSGlenn Lagasse 	return (B_TRUE);
2627f169c0eaSGlenn Lagasse }
2628f169c0eaSGlenn Lagasse 
2629f169c0eaSGlenn Lagasse /*
2630f169c0eaSGlenn Lagasse  * Function:	be_default_policy
2631f169c0eaSGlenn Lagasse  * Description:	Temporary hardcoded policy support.  This function returns
2632f169c0eaSGlenn Lagasse  *		the default policy type to be used to create a BE or a BE
2633f169c0eaSGlenn Lagasse  *		snapshot.
2634f169c0eaSGlenn Lagasse  * Parameters:
2635f169c0eaSGlenn Lagasse  *		None
2636f169c0eaSGlenn Lagasse  * Returns:
2637f169c0eaSGlenn Lagasse  *		Name of default BE policy.
2638f169c0eaSGlenn Lagasse  * Scope:
2639f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
2640f169c0eaSGlenn Lagasse  */
2641f169c0eaSGlenn Lagasse char *
be_default_policy(void)2642f169c0eaSGlenn Lagasse be_default_policy(void)
2643f169c0eaSGlenn Lagasse {
2644f169c0eaSGlenn Lagasse 	return (BE_PLCY_STATIC);
2645f169c0eaSGlenn Lagasse }
2646f169c0eaSGlenn Lagasse 
2647f169c0eaSGlenn Lagasse /*
2648f169c0eaSGlenn Lagasse  * Function:	valid_be_policy
2649f169c0eaSGlenn Lagasse  * Description:	Temporary hardcoded policy support.  This function valids
2650f169c0eaSGlenn Lagasse  *		whether a policy is a valid known policy or not.
2651f169c0eaSGlenn Lagasse  * Paramters:
2652f169c0eaSGlenn Lagasse  *		policy - name of policy to validate.
2653f169c0eaSGlenn Lagasse  * Returns:
2654f169c0eaSGlenn Lagasse  *		B_TRUE - policy is a valid.
2655f169c0eaSGlenn Lagasse  *		B_FALSE - policy is invalid.
2656f169c0eaSGlenn Lagasse  * Scope:
2657f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
2658f169c0eaSGlenn Lagasse  */
2659f169c0eaSGlenn Lagasse boolean_t
valid_be_policy(char * policy)2660f169c0eaSGlenn Lagasse valid_be_policy(char *policy)
2661f169c0eaSGlenn Lagasse {
2662f169c0eaSGlenn Lagasse 	if (policy == NULL)
2663f169c0eaSGlenn Lagasse 		return (B_FALSE);
2664f169c0eaSGlenn Lagasse 
2665f169c0eaSGlenn Lagasse 	if (strcmp(policy, BE_PLCY_STATIC) == 0 ||
2666f169c0eaSGlenn Lagasse 	    strcmp(policy, BE_PLCY_VOLATILE) == 0) {
2667f169c0eaSGlenn Lagasse 		return (B_TRUE);
2668f169c0eaSGlenn Lagasse 	}
2669f169c0eaSGlenn Lagasse 
2670f169c0eaSGlenn Lagasse 	return (B_FALSE);
2671f169c0eaSGlenn Lagasse }
2672f169c0eaSGlenn Lagasse 
2673f169c0eaSGlenn Lagasse /*
2674f169c0eaSGlenn Lagasse  * Function:	be_print_err
2675f169c0eaSGlenn Lagasse  * Description:	This function prints out error messages if do_print is
2676f169c0eaSGlenn Lagasse  *		set to B_TRUE or if the BE_PRINT_ERR environment variable
2677f169c0eaSGlenn Lagasse  *		is set to true.
2678f169c0eaSGlenn Lagasse  * Paramters:
2679f169c0eaSGlenn Lagasse  *		prnt_str - the string we wish to print and any arguments
2680f169c0eaSGlenn Lagasse  *		for the format of that string.
2681f169c0eaSGlenn Lagasse  * Returns:
2682f169c0eaSGlenn Lagasse  *		void
2683f169c0eaSGlenn Lagasse  * Scope:
2684f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
2685f169c0eaSGlenn Lagasse  */
2686f169c0eaSGlenn Lagasse void
be_print_err(char * prnt_str,...)2687f169c0eaSGlenn Lagasse be_print_err(char *prnt_str, ...)
2688f169c0eaSGlenn Lagasse {
2689f169c0eaSGlenn Lagasse 	va_list ap;
2690f169c0eaSGlenn Lagasse 	char buf[BUFSIZ];
2691f169c0eaSGlenn Lagasse 	char *env_buf;
2692f169c0eaSGlenn Lagasse 	static boolean_t env_checked = B_FALSE;
2693f169c0eaSGlenn Lagasse 
2694f169c0eaSGlenn Lagasse 	if (!env_checked) {
2695f169c0eaSGlenn Lagasse 		if ((env_buf = getenv("BE_PRINT_ERR")) != NULL) {
2696f169c0eaSGlenn Lagasse 			if (strcasecmp(env_buf, "true") == 0) {
2697f169c0eaSGlenn Lagasse 				do_print = B_TRUE;
2698f169c0eaSGlenn Lagasse 			}
2699f169c0eaSGlenn Lagasse 		}
2700f169c0eaSGlenn Lagasse 		env_checked = B_TRUE;
2701f169c0eaSGlenn Lagasse 	}
2702f169c0eaSGlenn Lagasse 
2703f169c0eaSGlenn Lagasse 	if (do_print) {
2704f169c0eaSGlenn Lagasse 		va_start(ap, prnt_str);
2705f169c0eaSGlenn Lagasse 		/* LINTED variable format specifier */
2706f169c0eaSGlenn Lagasse 		(void) vsnprintf(buf, BUFSIZ, prnt_str, ap);
2707f169c0eaSGlenn Lagasse 		(void) fputs(buf, stderr);
2708f169c0eaSGlenn Lagasse 		va_end(ap);
2709f169c0eaSGlenn Lagasse 	}
2710f169c0eaSGlenn Lagasse }
2711f169c0eaSGlenn Lagasse 
2712f169c0eaSGlenn Lagasse /*
2713f169c0eaSGlenn Lagasse  * Function:	be_find_current_be
2714f169c0eaSGlenn Lagasse  * Description:	Find the currently "active" BE. Fill in the
2715a897f28bSAndy Fiddaman  *		passed in be_transaction_data_t reference with the
2716f169c0eaSGlenn Lagasse  *		active BE's data.
2717f169c0eaSGlenn Lagasse  * Paramters:
2718f169c0eaSGlenn Lagasse  *		none
2719f169c0eaSGlenn Lagasse  * Returns:
2720f169c0eaSGlenn Lagasse  *		BE_SUCCESS - Success
2721f169c0eaSGlenn Lagasse  *		be_errnot_t - Failure
2722f169c0eaSGlenn Lagasse  * Scope:
2723f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
2724f169c0eaSGlenn Lagasse  * Notes:
2725f169c0eaSGlenn Lagasse  *		The caller is responsible for initializing the libzfs handle
2726f169c0eaSGlenn Lagasse  *		and freeing the memory used by the active be_name.
2727f169c0eaSGlenn Lagasse  */
2728f169c0eaSGlenn Lagasse int
be_find_current_be(be_transaction_data_t * bt)2729f169c0eaSGlenn Lagasse be_find_current_be(be_transaction_data_t *bt)
2730f169c0eaSGlenn Lagasse {
2731f169c0eaSGlenn Lagasse 	int	zret;
2732f169c0eaSGlenn Lagasse 
2733f169c0eaSGlenn Lagasse 	if ((zret = zpool_iter(g_zfs, be_zpool_find_current_be_callback,
2734f169c0eaSGlenn Lagasse 	    bt)) == 0) {
2735f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_find_current_be: failed to "
2736f169c0eaSGlenn Lagasse 		    "find current BE name\n"));
2737f169c0eaSGlenn Lagasse 		return (BE_ERR_BE_NOENT);
2738f169c0eaSGlenn Lagasse 	} else if (zret < 0) {
2739f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_find_current_be: "
2740f169c0eaSGlenn Lagasse 		    "zpool_iter failed: %s\n"),
2741f169c0eaSGlenn Lagasse 		    libzfs_error_description(g_zfs));
2742f169c0eaSGlenn Lagasse 		return (zfs_err_to_be_err(g_zfs));
2743f169c0eaSGlenn Lagasse 	}
2744f169c0eaSGlenn Lagasse 
2745f169c0eaSGlenn Lagasse 	return (BE_SUCCESS);
2746f169c0eaSGlenn Lagasse }
2747f169c0eaSGlenn Lagasse 
2748f169c0eaSGlenn Lagasse /*
2749f169c0eaSGlenn Lagasse  * Function:	be_zpool_find_current_be_callback
2750f169c0eaSGlenn Lagasse  * Description: Callback function used to iterate through all existing pools
2751f169c0eaSGlenn Lagasse  *		to find the BE that is the currently booted BE.
2752f169c0eaSGlenn Lagasse  * Parameters:
2753f169c0eaSGlenn Lagasse  *		zlp - zpool_handle_t pointer to the current pool being
2754f169c0eaSGlenn Lagasse  *			looked at.
2755f169c0eaSGlenn Lagasse  *		data - be_transaction_data_t pointer.
2756f169c0eaSGlenn Lagasse  *			Upon successfully finding the current BE, the
2757f169c0eaSGlenn Lagasse  *			obe_zpool member of this parameter is set to the
2758f169c0eaSGlenn Lagasse  *			pool it is found in.
2759f169c0eaSGlenn Lagasse  * Return:
2760f169c0eaSGlenn Lagasse  *		1 - Found current BE in this pool.
2761f169c0eaSGlenn Lagasse  *		0 - Did not find current BE in this pool.
2762f169c0eaSGlenn Lagasse  * Scope:
2763f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
2764f169c0eaSGlenn Lagasse  */
2765f169c0eaSGlenn Lagasse int
be_zpool_find_current_be_callback(zpool_handle_t * zlp,void * data)2766f169c0eaSGlenn Lagasse be_zpool_find_current_be_callback(zpool_handle_t *zlp, void *data)
2767f169c0eaSGlenn Lagasse {
2768f169c0eaSGlenn Lagasse 	be_transaction_data_t	*bt = data;
2769f169c0eaSGlenn Lagasse 	zfs_handle_t		*zhp = NULL;
2770f169c0eaSGlenn Lagasse 	const char		*zpool =  zpool_get_name(zlp);
2771f169c0eaSGlenn Lagasse 	char			be_container_ds[MAXPATHLEN];
2772f169c0eaSGlenn Lagasse 
2773f169c0eaSGlenn Lagasse 	/*
2774f169c0eaSGlenn Lagasse 	 * Generate string for BE container dataset
2775f169c0eaSGlenn Lagasse 	 */
2776*ec8422d0SAndy Fiddaman 	if (be_make_container_ds(zpool, be_container_ds,
2777*ec8422d0SAndy Fiddaman 	    sizeof (be_container_ds)) != BE_SUCCESS) {
2778*ec8422d0SAndy Fiddaman 		zpool_close(zlp);
2779*ec8422d0SAndy Fiddaman 		return (0);
27807e0e2549SAlexander Eremin 	}
2781f169c0eaSGlenn Lagasse 
2782f169c0eaSGlenn Lagasse 	/*
2783f169c0eaSGlenn Lagasse 	 * Check if a BE container dataset exists in this pool.
2784f169c0eaSGlenn Lagasse 	 */
2785f169c0eaSGlenn Lagasse 	if (!zfs_dataset_exists(g_zfs, be_container_ds, ZFS_TYPE_FILESYSTEM)) {
2786f169c0eaSGlenn Lagasse 		zpool_close(zlp);
2787f169c0eaSGlenn Lagasse 		return (0);
2788f169c0eaSGlenn Lagasse 	}
2789f169c0eaSGlenn Lagasse 
2790f169c0eaSGlenn Lagasse 	/*
2791f169c0eaSGlenn Lagasse 	 * Get handle to this zpool's BE container dataset.
2792f169c0eaSGlenn Lagasse 	 */
2793f169c0eaSGlenn Lagasse 	if ((zhp = zfs_open(g_zfs, be_container_ds, ZFS_TYPE_FILESYSTEM)) ==
2794f169c0eaSGlenn Lagasse 	    NULL) {
2795f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_zpool_find_current_be_callback: "
2796f169c0eaSGlenn Lagasse 		    "failed to open BE container dataset (%s)\n"),
2797f169c0eaSGlenn Lagasse 		    be_container_ds);
2798f169c0eaSGlenn Lagasse 		zpool_close(zlp);
2799f169c0eaSGlenn Lagasse 		return (0);
2800f169c0eaSGlenn Lagasse 	}
2801f169c0eaSGlenn Lagasse 
2802f169c0eaSGlenn Lagasse 	/*
2803f169c0eaSGlenn Lagasse 	 * Iterate through all potential BEs in this zpool
2804f169c0eaSGlenn Lagasse 	 */
2805f169c0eaSGlenn Lagasse 	if (zfs_iter_filesystems(zhp, be_zfs_find_current_be_callback, bt)) {
2806f169c0eaSGlenn Lagasse 		/*
2807f169c0eaSGlenn Lagasse 		 * Found current BE dataset; set obe_zpool
2808f169c0eaSGlenn Lagasse 		 */
2809f169c0eaSGlenn Lagasse 		if ((bt->obe_zpool = strdup(zpool)) == NULL) {
2810f169c0eaSGlenn Lagasse 			be_print_err(gettext(
2811f169c0eaSGlenn Lagasse 			    "be_zpool_find_current_be_callback: "
2812f169c0eaSGlenn Lagasse 			    "memory allocation failed\n"));
2813f169c0eaSGlenn Lagasse 			ZFS_CLOSE(zhp);
2814f169c0eaSGlenn Lagasse 			zpool_close(zlp);
2815f169c0eaSGlenn Lagasse 			return (0);
2816f169c0eaSGlenn Lagasse 		}
2817f169c0eaSGlenn Lagasse 
2818f169c0eaSGlenn Lagasse 		ZFS_CLOSE(zhp);
2819f169c0eaSGlenn Lagasse 		zpool_close(zlp);
2820f169c0eaSGlenn Lagasse 		return (1);
2821f169c0eaSGlenn Lagasse 	}
2822f169c0eaSGlenn Lagasse 
2823f169c0eaSGlenn Lagasse 	ZFS_CLOSE(zhp);
2824f169c0eaSGlenn Lagasse 	zpool_close(zlp);
2825f169c0eaSGlenn Lagasse 
2826f169c0eaSGlenn Lagasse 	return (0);
2827f169c0eaSGlenn Lagasse }
2828f169c0eaSGlenn Lagasse 
2829f169c0eaSGlenn Lagasse /*
2830f169c0eaSGlenn Lagasse  * Function:	be_zfs_find_current_be_callback
2831f169c0eaSGlenn Lagasse  * Description:	Callback function used to iterate through all BEs in a
2832f169c0eaSGlenn Lagasse  *		pool to find the BE that is the currently booted BE.
2833f169c0eaSGlenn Lagasse  * Parameters:
2834f169c0eaSGlenn Lagasse  *		zhp - zfs_handle_t pointer to current filesystem being checked.
2835f169c0eaSGlenn Lagasse  *		data - be_transaction-data_t pointer
2836f169c0eaSGlenn Lagasse  *			Upon successfully finding the current BE, the
2837f169c0eaSGlenn Lagasse  *			obe_name and obe_root_ds members of this parameter
2838f169c0eaSGlenn Lagasse  *			are set to the BE name and BE's root dataset
2839f169c0eaSGlenn Lagasse  *			respectively.
2840f169c0eaSGlenn Lagasse  * Return:
2841f169c0eaSGlenn Lagasse  *		1 - Found current BE.
2842f169c0eaSGlenn Lagasse  *		0 - Did not find current BE.
2843f169c0eaSGlenn Lagasse  * Scope:
2844f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
2845f169c0eaSGlenn Lagasse  */
2846f169c0eaSGlenn Lagasse int
be_zfs_find_current_be_callback(zfs_handle_t * zhp,void * data)2847f169c0eaSGlenn Lagasse be_zfs_find_current_be_callback(zfs_handle_t *zhp, void *data)
2848f169c0eaSGlenn Lagasse {
2849f169c0eaSGlenn Lagasse 	be_transaction_data_t	*bt = data;
2850f169c0eaSGlenn Lagasse 	char			*mp = NULL;
2851f169c0eaSGlenn Lagasse 
2852f169c0eaSGlenn Lagasse 	/*
2853f169c0eaSGlenn Lagasse 	 * Check if dataset is mounted, and if so where.
2854f169c0eaSGlenn Lagasse 	 */
2855f169c0eaSGlenn Lagasse 	if (zfs_is_mounted(zhp, &mp)) {
2856f169c0eaSGlenn Lagasse 		/*
2857f169c0eaSGlenn Lagasse 		 * If mounted at root, set obe_root_ds and obe_name
2858f169c0eaSGlenn Lagasse 		 */
2859f169c0eaSGlenn Lagasse 		if (mp != NULL && strcmp(mp, "/") == 0) {
2860f169c0eaSGlenn Lagasse 			free(mp);
2861f169c0eaSGlenn Lagasse 
2862f169c0eaSGlenn Lagasse 			if ((bt->obe_root_ds = strdup(zfs_get_name(zhp)))
2863f169c0eaSGlenn Lagasse 			    == NULL) {
2864f169c0eaSGlenn Lagasse 				be_print_err(gettext(
2865f169c0eaSGlenn Lagasse 				    "be_zfs_find_current_be_callback: "
2866f169c0eaSGlenn Lagasse 				    "memory allocation failed\n"));
2867f169c0eaSGlenn Lagasse 				ZFS_CLOSE(zhp);
2868f169c0eaSGlenn Lagasse 				return (0);
2869f169c0eaSGlenn Lagasse 			}
2870de1ab35cSAlexander Eremin 
2871f169c0eaSGlenn Lagasse 			if ((bt->obe_name = strdup(basename(bt->obe_root_ds)))
2872f169c0eaSGlenn Lagasse 			    == NULL) {
2873f169c0eaSGlenn Lagasse 				be_print_err(gettext(
2874f169c0eaSGlenn Lagasse 				    "be_zfs_find_current_be_callback: "
2875f169c0eaSGlenn Lagasse 				    "memory allocation failed\n"));
2876f169c0eaSGlenn Lagasse 				ZFS_CLOSE(zhp);
2877f169c0eaSGlenn Lagasse 				return (0);
2878f169c0eaSGlenn Lagasse 			}
2879f169c0eaSGlenn Lagasse 
2880f169c0eaSGlenn Lagasse 			ZFS_CLOSE(zhp);
2881f169c0eaSGlenn Lagasse 			return (1);
2882f169c0eaSGlenn Lagasse 		}
2883f169c0eaSGlenn Lagasse 
2884f169c0eaSGlenn Lagasse 		free(mp);
2885f169c0eaSGlenn Lagasse 	}
2886f169c0eaSGlenn Lagasse 	ZFS_CLOSE(zhp);
2887f169c0eaSGlenn Lagasse 
2888f169c0eaSGlenn Lagasse 	return (0);
2889f169c0eaSGlenn Lagasse }
2890f169c0eaSGlenn Lagasse 
2891f169c0eaSGlenn Lagasse /*
2892f169c0eaSGlenn Lagasse  * Function:	be_check_be_roots_callback
2893f169c0eaSGlenn Lagasse  * Description:	This function checks whether or not the dataset name passed
2894f169c0eaSGlenn Lagasse  *		is hierachically located under the BE root container dataset
2895f169c0eaSGlenn Lagasse  *		for this pool.
2896f169c0eaSGlenn Lagasse  * Parameters:
2897f169c0eaSGlenn Lagasse  *		zlp - zpool_handle_t pointer to current pool being processed.
2898f169c0eaSGlenn Lagasse  *		data - name of dataset to check
2899f169c0eaSGlenn Lagasse  * Returns:
2900f169c0eaSGlenn Lagasse  *		0 - dataset is not in this pool's BE root container dataset
2901f169c0eaSGlenn Lagasse  *		1 - dataset is in this pool's BE root container dataset
2902f169c0eaSGlenn Lagasse  * Scope:
2903f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
2904f169c0eaSGlenn Lagasse  */
2905f169c0eaSGlenn Lagasse int
be_check_be_roots_callback(zpool_handle_t * zlp,void * data)2906f169c0eaSGlenn Lagasse be_check_be_roots_callback(zpool_handle_t *zlp, void *data)
2907f169c0eaSGlenn Lagasse {
2908f169c0eaSGlenn Lagasse 	const char	*zpool = zpool_get_name(zlp);
2909f169c0eaSGlenn Lagasse 	char		*ds = data;
2910f169c0eaSGlenn Lagasse 	char		be_container_ds[MAXPATHLEN];
2911f169c0eaSGlenn Lagasse 
2912f169c0eaSGlenn Lagasse 	/* Generate string for this pool's BE root container dataset */
2913*ec8422d0SAndy Fiddaman 	if (be_make_container_ds(zpool, be_container_ds,
2914*ec8422d0SAndy Fiddaman 	    sizeof (be_container_ds)) != BE_SUCCESS) {
2915*ec8422d0SAndy Fiddaman 		return (0);
2916*ec8422d0SAndy Fiddaman 	}
2917f169c0eaSGlenn Lagasse 
2918f169c0eaSGlenn Lagasse 	/*
2919f169c0eaSGlenn Lagasse 	 * If dataset lives under the BE root container dataset
2920f169c0eaSGlenn Lagasse 	 * of this pool, return failure.
2921f169c0eaSGlenn Lagasse 	 */
2922f169c0eaSGlenn Lagasse 	if (strncmp(be_container_ds, ds, strlen(be_container_ds)) == 0 &&
2923f169c0eaSGlenn Lagasse 	    ds[strlen(be_container_ds)] == '/') {
2924f169c0eaSGlenn Lagasse 		zpool_close(zlp);
2925f169c0eaSGlenn Lagasse 		return (1);
2926f169c0eaSGlenn Lagasse 	}
2927f169c0eaSGlenn Lagasse 
2928f169c0eaSGlenn Lagasse 	zpool_close(zlp);
2929f169c0eaSGlenn Lagasse 	return (0);
2930f169c0eaSGlenn Lagasse }
2931f169c0eaSGlenn Lagasse 
2932f169c0eaSGlenn Lagasse /*
2933f169c0eaSGlenn Lagasse  * Function:	zfs_err_to_be_err
2934f169c0eaSGlenn Lagasse  * Description:	This function takes the error stored in the libzfs handle
2935f169c0eaSGlenn Lagasse  *		and maps it to an be_errno_t. If there are no matching
2936f169c0eaSGlenn Lagasse  *		be_errno_t's then BE_ERR_ZFS is returned.
2937f169c0eaSGlenn Lagasse  * Paramters:
2938f169c0eaSGlenn Lagasse  *		zfsh - The libzfs handle containing the error we're looking up.
2939f169c0eaSGlenn Lagasse  * Returns:
2940f169c0eaSGlenn Lagasse  *		be_errno_t
2941f169c0eaSGlenn Lagasse  * Scope:
2942f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
2943f169c0eaSGlenn Lagasse  */
2944f169c0eaSGlenn Lagasse int
zfs_err_to_be_err(libzfs_handle_t * zfsh)2945f169c0eaSGlenn Lagasse zfs_err_to_be_err(libzfs_handle_t *zfsh)
2946f169c0eaSGlenn Lagasse {
2947f169c0eaSGlenn Lagasse 	int err = libzfs_errno(zfsh);
2948f169c0eaSGlenn Lagasse 
2949f169c0eaSGlenn Lagasse 	switch (err) {
2950f169c0eaSGlenn Lagasse 	case 0:
2951f169c0eaSGlenn Lagasse 		return (BE_SUCCESS);
2952f169c0eaSGlenn Lagasse 	case EZFS_PERM:
2953f169c0eaSGlenn Lagasse 		return (BE_ERR_PERM);
2954f169c0eaSGlenn Lagasse 	case EZFS_INTR:
2955f169c0eaSGlenn Lagasse 		return (BE_ERR_INTR);
2956f169c0eaSGlenn Lagasse 	case EZFS_NOENT:
2957f169c0eaSGlenn Lagasse 		return (BE_ERR_NOENT);
2958f169c0eaSGlenn Lagasse 	case EZFS_NOSPC:
2959f169c0eaSGlenn Lagasse 		return (BE_ERR_NOSPC);
2960f169c0eaSGlenn Lagasse 	case EZFS_MOUNTFAILED:
2961f169c0eaSGlenn Lagasse 		return (BE_ERR_MOUNT);
2962f169c0eaSGlenn Lagasse 	case EZFS_UMOUNTFAILED:
2963f169c0eaSGlenn Lagasse 		return (BE_ERR_UMOUNT);
2964f169c0eaSGlenn Lagasse 	case EZFS_EXISTS:
2965f169c0eaSGlenn Lagasse 		return (BE_ERR_BE_EXISTS);
2966f169c0eaSGlenn Lagasse 	case EZFS_BUSY:
2967f169c0eaSGlenn Lagasse 		return (BE_ERR_DEV_BUSY);
2968f9af39baSGeorge Wilson 	case EZFS_POOLREADONLY:
2969f169c0eaSGlenn Lagasse 		return (BE_ERR_ROFS);
2970f169c0eaSGlenn Lagasse 	case EZFS_NAMETOOLONG:
2971f169c0eaSGlenn Lagasse 		return (BE_ERR_NAMETOOLONG);
2972f169c0eaSGlenn Lagasse 	case EZFS_NODEVICE:
2973f169c0eaSGlenn Lagasse 		return (BE_ERR_NODEV);
2974f169c0eaSGlenn Lagasse 	case EZFS_POOL_INVALARG:
2975f169c0eaSGlenn Lagasse 		return (BE_ERR_INVAL);
2976f169c0eaSGlenn Lagasse 	case EZFS_PROPTYPE:
2977f169c0eaSGlenn Lagasse 		return (BE_ERR_INVALPROP);
2978f169c0eaSGlenn Lagasse 	case EZFS_BADTYPE:
2979f169c0eaSGlenn Lagasse 		return (BE_ERR_DSTYPE);
2980f169c0eaSGlenn Lagasse 	case EZFS_PROPNONINHERIT:
2981f169c0eaSGlenn Lagasse 		return (BE_ERR_NONINHERIT);
2982f169c0eaSGlenn Lagasse 	case EZFS_PROPREADONLY:
2983f169c0eaSGlenn Lagasse 		return (BE_ERR_READONLYPROP);
2984f169c0eaSGlenn Lagasse 	case EZFS_RESILVERING:
2985f169c0eaSGlenn Lagasse 	case EZFS_POOLUNAVAIL:
2986f169c0eaSGlenn Lagasse 		return (BE_ERR_UNAVAIL);
2987f169c0eaSGlenn Lagasse 	case EZFS_DSREADONLY:
2988f169c0eaSGlenn Lagasse 		return (BE_ERR_READONLYDS);
2989f169c0eaSGlenn Lagasse 	default:
2990f169c0eaSGlenn Lagasse 		return (BE_ERR_ZFS);
2991f169c0eaSGlenn Lagasse 	}
2992f169c0eaSGlenn Lagasse }
2993f169c0eaSGlenn Lagasse 
2994f169c0eaSGlenn Lagasse /*
2995f169c0eaSGlenn Lagasse  * Function:	errno_to_be_err
2996f169c0eaSGlenn Lagasse  * Description:	This function takes an errno and maps it to an be_errno_t.
2997f169c0eaSGlenn Lagasse  *		If there are no matching be_errno_t's then BE_ERR_UNKNOWN is
2998f169c0eaSGlenn Lagasse  *		returned.
2999f169c0eaSGlenn Lagasse  * Paramters:
3000f169c0eaSGlenn Lagasse  *		err - The errno we're compairing against.
3001f169c0eaSGlenn Lagasse  * Returns:
3002f169c0eaSGlenn Lagasse  *		be_errno_t
3003f169c0eaSGlenn Lagasse  * Scope:
3004f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
3005f169c0eaSGlenn Lagasse  */
3006f169c0eaSGlenn Lagasse int
errno_to_be_err(int err)3007f169c0eaSGlenn Lagasse errno_to_be_err(int err)
3008f169c0eaSGlenn Lagasse {
3009f169c0eaSGlenn Lagasse 	switch (err) {
3010f169c0eaSGlenn Lagasse 	case EPERM:
3011f169c0eaSGlenn Lagasse 		return (BE_ERR_PERM);
3012f169c0eaSGlenn Lagasse 	case EACCES:
3013f169c0eaSGlenn Lagasse 		return (BE_ERR_ACCESS);
3014f169c0eaSGlenn Lagasse 	case ECANCELED:
3015f169c0eaSGlenn Lagasse 		return (BE_ERR_CANCELED);
3016f169c0eaSGlenn Lagasse 	case EINTR:
3017f169c0eaSGlenn Lagasse 		return (BE_ERR_INTR);
3018f169c0eaSGlenn Lagasse 	case ENOENT:
3019f169c0eaSGlenn Lagasse 		return (BE_ERR_NOENT);
3020f169c0eaSGlenn Lagasse 	case ENOSPC:
3021f169c0eaSGlenn Lagasse 	case EDQUOT:
3022f169c0eaSGlenn Lagasse 		return (BE_ERR_NOSPC);
3023f169c0eaSGlenn Lagasse 	case EEXIST:
3024f169c0eaSGlenn Lagasse 		return (BE_ERR_BE_EXISTS);
3025f169c0eaSGlenn Lagasse 	case EBUSY:
3026f169c0eaSGlenn Lagasse 		return (BE_ERR_BUSY);
3027f169c0eaSGlenn Lagasse 	case EROFS:
3028f169c0eaSGlenn Lagasse 		return (BE_ERR_ROFS);
3029f169c0eaSGlenn Lagasse 	case ENAMETOOLONG:
3030f169c0eaSGlenn Lagasse 		return (BE_ERR_NAMETOOLONG);
3031f169c0eaSGlenn Lagasse 	case ENXIO:
3032f169c0eaSGlenn Lagasse 		return (BE_ERR_NXIO);
3033f169c0eaSGlenn Lagasse 	case EINVAL:
3034f169c0eaSGlenn Lagasse 		return (BE_ERR_INVAL);
3035f169c0eaSGlenn Lagasse 	case EFAULT:
3036f169c0eaSGlenn Lagasse 		return (BE_ERR_FAULT);
3037f169c0eaSGlenn Lagasse 	default:
3038f169c0eaSGlenn Lagasse 		return (BE_ERR_UNKNOWN);
3039f169c0eaSGlenn Lagasse 	}
3040f169c0eaSGlenn Lagasse }
3041f169c0eaSGlenn Lagasse 
3042f169c0eaSGlenn Lagasse /*
3043f169c0eaSGlenn Lagasse  * Function:	be_err_to_str
3044f169c0eaSGlenn Lagasse  * Description:	This function takes a be_errno_t and maps it to a message.
3045f169c0eaSGlenn Lagasse  *		If there are no matching be_errno_t's then NULL is returned.
3046f169c0eaSGlenn Lagasse  * Paramters:
3047f169c0eaSGlenn Lagasse  *		be_errno_t - The be_errno_t we're mapping.
3048f169c0eaSGlenn Lagasse  * Returns:
3049f169c0eaSGlenn Lagasse  *		string or NULL if the error code is not known.
3050f169c0eaSGlenn Lagasse  * Scope:
3051f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
3052f169c0eaSGlenn Lagasse  */
3053f169c0eaSGlenn Lagasse char *
be_err_to_str(int err)3054f169c0eaSGlenn Lagasse be_err_to_str(int err)
3055f169c0eaSGlenn Lagasse {
3056f169c0eaSGlenn Lagasse 	switch (err) {
3057f169c0eaSGlenn Lagasse 	case BE_ERR_ACCESS:
3058f169c0eaSGlenn Lagasse 		return (gettext("Permission denied."));
3059f169c0eaSGlenn Lagasse 	case BE_ERR_ACTIVATE_CURR:
3060f169c0eaSGlenn Lagasse 		return (gettext("Activation of current BE failed."));
3061f169c0eaSGlenn Lagasse 	case BE_ERR_AUTONAME:
3062f169c0eaSGlenn Lagasse 		return (gettext("Auto naming failed."));
3063f169c0eaSGlenn Lagasse 	case BE_ERR_BE_NOENT:
3064f169c0eaSGlenn Lagasse 		return (gettext("No such BE."));
3065f169c0eaSGlenn Lagasse 	case BE_ERR_BUSY:
3066f169c0eaSGlenn Lagasse 		return (gettext("Mount busy."));
3067f169c0eaSGlenn Lagasse 	case BE_ERR_DEV_BUSY:
3068f169c0eaSGlenn Lagasse 		return (gettext("Device busy."));
3069f169c0eaSGlenn Lagasse 	case BE_ERR_CANCELED:
3070f169c0eaSGlenn Lagasse 		return (gettext("Operation canceled."));
3071f169c0eaSGlenn Lagasse 	case BE_ERR_CLONE:
3072f169c0eaSGlenn Lagasse 		return (gettext("BE clone failed."));
3073f169c0eaSGlenn Lagasse 	case BE_ERR_COPY:
3074f169c0eaSGlenn Lagasse 		return (gettext("BE copy failed."));
3075f169c0eaSGlenn Lagasse 	case BE_ERR_CREATDS:
3076f169c0eaSGlenn Lagasse 		return (gettext("Dataset creation failed."));
3077f169c0eaSGlenn Lagasse 	case BE_ERR_CURR_BE_NOT_FOUND:
3078f169c0eaSGlenn Lagasse 		return (gettext("Can't find current BE."));
3079f169c0eaSGlenn Lagasse 	case BE_ERR_DESTROY:
3080f169c0eaSGlenn Lagasse 		return (gettext("Failed to destroy BE or snapshot."));
3081f169c0eaSGlenn Lagasse 	case BE_ERR_DESTROY_CURR_BE:
3082f169c0eaSGlenn Lagasse 		return (gettext("Cannot destroy current BE."));
3083f169c0eaSGlenn Lagasse 	case BE_ERR_DEMOTE:
3084f169c0eaSGlenn Lagasse 		return (gettext("BE demotion failed."));
3085f169c0eaSGlenn Lagasse 	case BE_ERR_DSTYPE:
3086f169c0eaSGlenn Lagasse 		return (gettext("Invalid dataset type."));
3087f169c0eaSGlenn Lagasse 	case BE_ERR_BE_EXISTS:
3088f169c0eaSGlenn Lagasse 		return (gettext("BE exists."));
3089f169c0eaSGlenn Lagasse 	case BE_ERR_INIT:
3090f169c0eaSGlenn Lagasse 		return (gettext("be_zfs_init failed."));
3091f169c0eaSGlenn Lagasse 	case BE_ERR_INTR:
3092f169c0eaSGlenn Lagasse 		return (gettext("Interupted system call."));
3093f169c0eaSGlenn Lagasse 	case BE_ERR_INVAL:
3094f169c0eaSGlenn Lagasse 		return (gettext("Invalid argument."));
3095f169c0eaSGlenn Lagasse 	case BE_ERR_INVALPROP:
3096f169c0eaSGlenn Lagasse 		return (gettext("Invalid property for dataset."));
3097f169c0eaSGlenn Lagasse 	case BE_ERR_INVALMOUNTPOINT:
3098f169c0eaSGlenn Lagasse 		return (gettext("Unexpected mountpoint."));
3099f169c0eaSGlenn Lagasse 	case BE_ERR_MOUNT:
3100f169c0eaSGlenn Lagasse 		return (gettext("Mount failed."));
3101f169c0eaSGlenn Lagasse 	case BE_ERR_MOUNTED:
3102f169c0eaSGlenn Lagasse 		return (gettext("Already mounted."));
3103f169c0eaSGlenn Lagasse 	case BE_ERR_NAMETOOLONG:
3104f169c0eaSGlenn Lagasse 		return (gettext("name > BUFSIZ."));
3105f169c0eaSGlenn Lagasse 	case BE_ERR_NOENT:
3106f169c0eaSGlenn Lagasse 		return (gettext("Doesn't exist."));
3107f169c0eaSGlenn Lagasse 	case BE_ERR_POOL_NOENT:
3108f169c0eaSGlenn Lagasse 		return (gettext("No such pool."));
3109f169c0eaSGlenn Lagasse 	case BE_ERR_NODEV:
3110f169c0eaSGlenn Lagasse 		return (gettext("No such device."));
3111f169c0eaSGlenn Lagasse 	case BE_ERR_NOTMOUNTED:
3112f169c0eaSGlenn Lagasse 		return (gettext("File system not mounted."));
3113f169c0eaSGlenn Lagasse 	case BE_ERR_NOMEM:
3114f169c0eaSGlenn Lagasse 		return (gettext("Not enough memory."));
3115f169c0eaSGlenn Lagasse 	case BE_ERR_NONINHERIT:
3116f169c0eaSGlenn Lagasse 		return (gettext(
3117f169c0eaSGlenn Lagasse 		    "Property is not inheritable for the BE dataset."));
3118f169c0eaSGlenn Lagasse 	case BE_ERR_NXIO:
3119f169c0eaSGlenn Lagasse 		return (gettext("No such device or address."));
3120f169c0eaSGlenn Lagasse 	case BE_ERR_NOSPC:
3121f169c0eaSGlenn Lagasse 		return (gettext("No space on device."));
3122f169c0eaSGlenn Lagasse 	case BE_ERR_NOTSUP:
3123f169c0eaSGlenn Lagasse 		return (gettext("Operation not supported."));
3124f169c0eaSGlenn Lagasse 	case BE_ERR_OPEN:
3125f169c0eaSGlenn Lagasse 		return (gettext("Open failed."));
3126f169c0eaSGlenn Lagasse 	case BE_ERR_PERM:
3127f169c0eaSGlenn Lagasse 		return (gettext("Not owner."));
3128f169c0eaSGlenn Lagasse 	case BE_ERR_UNAVAIL:
3129f169c0eaSGlenn Lagasse 		return (gettext("The BE is currently unavailable."));
3130f169c0eaSGlenn Lagasse 	case BE_ERR_PROMOTE:
3131f169c0eaSGlenn Lagasse 		return (gettext("BE promotion failed."));
3132f169c0eaSGlenn Lagasse 	case BE_ERR_ROFS:
3133f169c0eaSGlenn Lagasse 		return (gettext("Read only file system."));
3134f169c0eaSGlenn Lagasse 	case BE_ERR_READONLYDS:
3135f169c0eaSGlenn Lagasse 		return (gettext("Read only dataset."));
3136f169c0eaSGlenn Lagasse 	case BE_ERR_READONLYPROP:
3137f169c0eaSGlenn Lagasse 		return (gettext("Read only property."));
3138f169c0eaSGlenn Lagasse 	case BE_ERR_RENAME_ACTIVE:
3139f169c0eaSGlenn Lagasse 		return (gettext("Renaming the active BE is not supported."));
3140f169c0eaSGlenn Lagasse 	case BE_ERR_SS_EXISTS:
3141f169c0eaSGlenn Lagasse 		return (gettext("Snapshot exists."));
3142f169c0eaSGlenn Lagasse 	case BE_ERR_SS_NOENT:
3143f169c0eaSGlenn Lagasse 		return (gettext("No such snapshot."));
3144f169c0eaSGlenn Lagasse 	case BE_ERR_UMOUNT:
3145f169c0eaSGlenn Lagasse 		return (gettext("Unmount failed."));
3146f169c0eaSGlenn Lagasse 	case BE_ERR_UMOUNT_CURR_BE:
3147f169c0eaSGlenn Lagasse 		return (gettext("Can't unmount the current BE."));
3148f169c0eaSGlenn Lagasse 	case BE_ERR_UMOUNT_SHARED:
3149f169c0eaSGlenn Lagasse 		return (gettext("Unmount of a shared File System failed."));
3150f169c0eaSGlenn Lagasse 	case BE_ERR_FAULT:
3151f169c0eaSGlenn Lagasse 		return (gettext("Bad address."));
3152f169c0eaSGlenn Lagasse 	case BE_ERR_UNKNOWN:
3153f169c0eaSGlenn Lagasse 		return (gettext("Unknown error."));
3154f169c0eaSGlenn Lagasse 	case BE_ERR_ZFS:
3155f169c0eaSGlenn Lagasse 		return (gettext("ZFS returned an error."));
3156f169c0eaSGlenn Lagasse 	case BE_ERR_GEN_UUID:
3157f169c0eaSGlenn Lagasse 		return (gettext("Failed to generate uuid."));
3158f169c0eaSGlenn Lagasse 	case BE_ERR_PARSE_UUID:
3159f169c0eaSGlenn Lagasse 		return (gettext("Failed to parse uuid."));
3160f169c0eaSGlenn Lagasse 	case BE_ERR_NO_UUID:
3161f169c0eaSGlenn Lagasse 		return (gettext("No uuid"));
3162f169c0eaSGlenn Lagasse 	case BE_ERR_ZONE_NO_PARENTBE:
3163f169c0eaSGlenn Lagasse 		return (gettext("No parent uuid"));
3164f169c0eaSGlenn Lagasse 	case BE_ERR_ZONE_MULTIPLE_ACTIVE:
3165f169c0eaSGlenn Lagasse 		return (gettext("Multiple active zone roots"));
3166f169c0eaSGlenn Lagasse 	case BE_ERR_ZONE_NO_ACTIVE_ROOT:
3167f169c0eaSGlenn Lagasse 		return (gettext("No active zone root"));
3168f169c0eaSGlenn Lagasse 	case BE_ERR_ZONE_ROOT_NOT_LEGACY:
3169f169c0eaSGlenn Lagasse 		return (gettext("Zone root not legacy"));
3170f169c0eaSGlenn Lagasse 	case BE_ERR_MOUNT_ZONEROOT:
3171f169c0eaSGlenn Lagasse 		return (gettext("Failed to mount a zone root."));
3172f169c0eaSGlenn Lagasse 	case BE_ERR_UMOUNT_ZONEROOT:
3173f169c0eaSGlenn Lagasse 		return (gettext("Failed to unmount a zone root."));
3174f169c0eaSGlenn Lagasse 	case BE_ERR_NO_MOUNTED_ZONE:
3175f169c0eaSGlenn Lagasse 		return (gettext("Zone is not mounted"));
3176f169c0eaSGlenn Lagasse 	case BE_ERR_ZONES_UNMOUNT:
3177f169c0eaSGlenn Lagasse 		return (gettext("Unable to unmount a zone BE."));
3178f169c0eaSGlenn Lagasse 	case BE_ERR_NO_MENU:
3179f169c0eaSGlenn Lagasse 		return (gettext("Missing boot menu file."));
3180f169c0eaSGlenn Lagasse 	case BE_ERR_BAD_MENU_PATH:
3181f169c0eaSGlenn Lagasse 		return (gettext("Invalid path for menu.lst file"));
3182f169c0eaSGlenn Lagasse 	case BE_ERR_ZONE_SS_EXISTS:
3183f169c0eaSGlenn Lagasse 		return (gettext("Zone snapshot exists."));
3184f169c0eaSGlenn Lagasse 	case BE_ERR_BOOTFILE_INST:
3185f169c0eaSGlenn Lagasse 		return (gettext("Error installing boot files."));
3186f169c0eaSGlenn Lagasse 	case BE_ERR_EXTCMD:
3187f169c0eaSGlenn Lagasse 		return (gettext("Error running an external command."));
3188f169c0eaSGlenn Lagasse 	default:
3189f169c0eaSGlenn Lagasse 		return (NULL);
3190f169c0eaSGlenn Lagasse 	}
3191f169c0eaSGlenn Lagasse }
3192f169c0eaSGlenn Lagasse 
3193f169c0eaSGlenn Lagasse /*
3194f169c0eaSGlenn Lagasse  * Function:    be_has_grub
3195f169c0eaSGlenn Lagasse  * Description: Boolean function indicating whether the current system
3196f169c0eaSGlenn Lagasse  *		uses grub.
3197f169c0eaSGlenn Lagasse  * Return:      B_FALSE - the system does not have grub
3198f169c0eaSGlenn Lagasse  *              B_TRUE - the system does have grub.
3199f169c0eaSGlenn Lagasse  * Scope:
3200f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
3201f169c0eaSGlenn Lagasse  */
3202f169c0eaSGlenn Lagasse boolean_t
be_has_grub(void)3203f169c0eaSGlenn Lagasse be_has_grub(void)
3204f169c0eaSGlenn Lagasse {
3205fa0c327aSToomas Soome 	static struct be_defaults be_defaults;
3206fa0c327aSToomas Soome 	static boolean_t be_deflts_set = B_FALSE;
3207fa0c327aSToomas Soome 
3208fa0c327aSToomas Soome 	/* Cache the defaults, because be_has_grub is used often. */
3209fa0c327aSToomas Soome 	if (be_deflts_set == B_FALSE) {
3210fa0c327aSToomas Soome 		be_get_defaults(&be_defaults);
3211fa0c327aSToomas Soome 		be_deflts_set = B_TRUE;
3212fa0c327aSToomas Soome 	}
3213fa0c327aSToomas Soome 
3214fa0c327aSToomas Soome 	return (be_defaults.be_deflt_grub);
3215f169c0eaSGlenn Lagasse }
3216f169c0eaSGlenn Lagasse 
3217f169c0eaSGlenn Lagasse /*
3218f169c0eaSGlenn Lagasse  * Function:    be_is_isa
3219f169c0eaSGlenn Lagasse  * Description: Boolean function indicating whether the instruction set
3220f169c0eaSGlenn Lagasse  *              architecture of the executing system matches the name provided.
3221f169c0eaSGlenn Lagasse  *              The string must match a system defined architecture (e.g.
3222f169c0eaSGlenn Lagasse  *              "i386", "sparc") and is case sensitive.
3223f169c0eaSGlenn Lagasse  * Parameters:  name - string representing the name of instruction set
3224f169c0eaSGlenn Lagasse  *			architecture being tested
3225f169c0eaSGlenn Lagasse  * Returns:     B_FALSE - the system instruction set architecture is different
3226f169c0eaSGlenn Lagasse  *			from the one specified
3227f169c0eaSGlenn Lagasse  *              B_TRUE - the system instruction set architecture is the same
3228f169c0eaSGlenn Lagasse  *			as the one specified
3229f169c0eaSGlenn Lagasse  * Scope:
3230f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
3231f169c0eaSGlenn Lagasse  */
3232f169c0eaSGlenn Lagasse boolean_t
be_is_isa(char * name)3233f169c0eaSGlenn Lagasse be_is_isa(char *name)
3234f169c0eaSGlenn Lagasse {
3235f169c0eaSGlenn Lagasse 	return ((strcmp((char *)be_get_default_isa(), name) == 0));
3236f169c0eaSGlenn Lagasse }
3237f169c0eaSGlenn Lagasse 
3238f169c0eaSGlenn Lagasse /*
3239f169c0eaSGlenn Lagasse  * Function: be_get_default_isa
3240f169c0eaSGlenn Lagasse  * Description:
3241f169c0eaSGlenn Lagasse  *      Returns the default instruction set architecture of the
3242f169c0eaSGlenn Lagasse  *      machine it is executed on. (eg. sparc, i386, ...)
3243f169c0eaSGlenn Lagasse  *      NOTE:   SYS_INST environment variable may override default
3244f169c0eaSGlenn Lagasse  *              return value
3245f169c0eaSGlenn Lagasse  * Parameters:
3246f169c0eaSGlenn Lagasse  *		none
3247f169c0eaSGlenn Lagasse  * Returns:
3248f169c0eaSGlenn Lagasse  *		NULL - the architecture returned by sysinfo() was too
3249f169c0eaSGlenn Lagasse  *			long for local variables
3250f169c0eaSGlenn Lagasse  *		char * - pointer to a string containing the default
3251f169c0eaSGlenn Lagasse  *			implementation
3252f169c0eaSGlenn Lagasse  * Scope:
3253f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
3254f169c0eaSGlenn Lagasse  */
3255f169c0eaSGlenn Lagasse char *
be_get_default_isa(void)3256f169c0eaSGlenn Lagasse be_get_default_isa(void)
3257f169c0eaSGlenn Lagasse {
3258f169c0eaSGlenn Lagasse 	int	i;
3259f169c0eaSGlenn Lagasse 	char	*envp;
3260f169c0eaSGlenn Lagasse 	static char	default_inst[ARCH_LENGTH] = "";
3261f169c0eaSGlenn Lagasse 
3262f169c0eaSGlenn Lagasse 	if (default_inst[0] == '\0') {
3263f169c0eaSGlenn Lagasse 		if ((envp = getenv("SYS_INST")) != NULL) {
3264f169c0eaSGlenn Lagasse 			if ((int)strlen(envp) >= ARCH_LENGTH)
3265f169c0eaSGlenn Lagasse 				return (NULL);
3266f169c0eaSGlenn Lagasse 			else
3267f169c0eaSGlenn Lagasse 				(void) strcpy(default_inst, envp);
3268f169c0eaSGlenn Lagasse 		} else  {
3269f169c0eaSGlenn Lagasse 			i = sysinfo(SI_ARCHITECTURE, default_inst, ARCH_LENGTH);
3270f169c0eaSGlenn Lagasse 			if (i < 0 || i > ARCH_LENGTH)
3271f169c0eaSGlenn Lagasse 				return (NULL);
3272f169c0eaSGlenn Lagasse 		}
3273f169c0eaSGlenn Lagasse 	}
3274f169c0eaSGlenn Lagasse 	return (default_inst);
3275f169c0eaSGlenn Lagasse }
3276f169c0eaSGlenn Lagasse 
3277a63c99a2SToomas Soome /*
3278a63c99a2SToomas Soome  * Function: be_get_platform
3279a63c99a2SToomas Soome  * Description:
3280a63c99a2SToomas Soome  *      Returns the platfom name
3281a63c99a2SToomas Soome  * Parameters:
3282a63c99a2SToomas Soome  *		none
3283a63c99a2SToomas Soome  * Returns:
3284a63c99a2SToomas Soome  *		NULL - the platform name returned by sysinfo() was too
3285a63c99a2SToomas Soome  *			long for local variables
3286a63c99a2SToomas Soome  *		char * - pointer to a string containing the platform name
3287a63c99a2SToomas Soome  * Scope:
3288a63c99a2SToomas Soome  *		Semi-private (library wide use only)
3289a63c99a2SToomas Soome  */
3290a63c99a2SToomas Soome char *
be_get_platform(void)3291a63c99a2SToomas Soome be_get_platform(void)
3292a63c99a2SToomas Soome {
3293a63c99a2SToomas Soome 	int	i;
3294a63c99a2SToomas Soome 	static char	default_inst[ARCH_LENGTH] = "";
3295a63c99a2SToomas Soome 
3296a63c99a2SToomas Soome 	if (default_inst[0] == '\0') {
3297a63c99a2SToomas Soome 		i = sysinfo(SI_PLATFORM, default_inst, ARCH_LENGTH);
3298a63c99a2SToomas Soome 		if (i < 0 || i > ARCH_LENGTH)
3299a63c99a2SToomas Soome 			return (NULL);
3300a63c99a2SToomas Soome 	}
3301a63c99a2SToomas Soome 	return (default_inst);
3302a63c99a2SToomas Soome }
3303a63c99a2SToomas Soome 
3304f169c0eaSGlenn Lagasse /*
3305f169c0eaSGlenn Lagasse  * Function: be_run_cmd
3306f169c0eaSGlenn Lagasse  * Description:
3307f169c0eaSGlenn Lagasse  *	Runs a command in a separate subprocess.  Splits out stdout from stderr
3308f169c0eaSGlenn Lagasse  *	and sends each to its own buffer.  Buffers must be pre-allocated and
3309f169c0eaSGlenn Lagasse  *	passed in as arguments.  Buffer sizes are also passed in as arguments.
3310f169c0eaSGlenn Lagasse  *
3311f169c0eaSGlenn Lagasse  *	Notes / caveats:
3312f169c0eaSGlenn Lagasse  *	- Command being run is assumed to not have any stdout or stderr
3313f169c0eaSGlenn Lagasse  *		redirection.
3314f169c0eaSGlenn Lagasse  *	- Commands which emit total stderr output of greater than PIPE_BUF
3315f169c0eaSGlenn Lagasse  *		bytes can hang.  For such commands, a different implementation
3316f169c0eaSGlenn Lagasse  *		which uses poll(2) must be used.
3317f169c0eaSGlenn Lagasse  *	- stdout_buf can be NULL.  In this case, stdout_bufsize is ignored, and
3318f169c0eaSGlenn Lagasse  *		the stream which would have gone to it is sent to the bit
3319f169c0eaSGlenn Lagasse  *		bucket.
3320f169c0eaSGlenn Lagasse  *	- stderr_buf cannot be NULL.
3321f169c0eaSGlenn Lagasse  *	- Only subprocess errors are appended to the stderr_buf.  Errors
3322f169c0eaSGlenn Lagasse  *		running the command are reported through be_print_err().
3323f169c0eaSGlenn Lagasse  *	- Data which would overflow its respective buffer is sent to the bit
3324f169c0eaSGlenn Lagasse  *		bucket.
3325f169c0eaSGlenn Lagasse  *
3326f169c0eaSGlenn Lagasse  * Parameters:
3327f169c0eaSGlenn Lagasse  *		command: command to run.  Assumed not to have embedded stdout
3328f169c0eaSGlenn Lagasse  *			or stderr redirection.  May have stdin redirection,
3329f169c0eaSGlenn Lagasse  *			however.
3330f169c0eaSGlenn Lagasse  *		stderr_buf: buffer returning subprocess stderr data.  Errors
3331f169c0eaSGlenn Lagasse  *			reported by this function are reported through
3332f169c0eaSGlenn Lagasse  *			be_print_err().
3333f169c0eaSGlenn Lagasse  *		stderr_bufsize: size of stderr_buf
3334f169c0eaSGlenn Lagasse  *		stdout_buf: buffer returning subprocess stdout data.
3335f169c0eaSGlenn Lagasse  *		stdout_bufsize: size of stdout_buf
3336f169c0eaSGlenn Lagasse  * Returns:
3337f169c0eaSGlenn Lagasse  *		BE_SUCCESS - The command ran successfully without returning
3338f169c0eaSGlenn Lagasse  *			errors.
3339f169c0eaSGlenn Lagasse  *		BE_ERR_EXTCMD
3340f169c0eaSGlenn Lagasse  *			- The command could not be run.
3341f169c0eaSGlenn Lagasse  *			- The command terminated with error status.
3342f169c0eaSGlenn Lagasse  *			- There were errors extracting or returning subprocess
3343f169c0eaSGlenn Lagasse  *				data.
3344f169c0eaSGlenn Lagasse  *		BE_ERR_NOMEM - The command exceeds the command buffer size.
3345f169c0eaSGlenn Lagasse  *		BE_ERR_INVAL - An invalid argument was specified.
3346f169c0eaSGlenn Lagasse  * Scope:
3347f169c0eaSGlenn Lagasse  *		Semi-private (library wide use only)
3348f169c0eaSGlenn Lagasse  */
3349f169c0eaSGlenn Lagasse int
be_run_cmd(char * command,char * stderr_buf,int stderr_bufsize,char * stdout_buf,int stdout_bufsize)3350f169c0eaSGlenn Lagasse be_run_cmd(char *command, char *stderr_buf, int stderr_bufsize,
3351f169c0eaSGlenn Lagasse     char *stdout_buf, int stdout_bufsize)
3352f169c0eaSGlenn Lagasse {
3353f169c0eaSGlenn Lagasse 	char *temp_filename = strdup(tmpnam(NULL));
3354f169c0eaSGlenn Lagasse 	FILE *stdout_str = NULL;
3355f169c0eaSGlenn Lagasse 	FILE *stderr_str = NULL;
3356f169c0eaSGlenn Lagasse 	char cmdline[BUFSIZ];
3357f169c0eaSGlenn Lagasse 	char oneline[BUFSIZ];
3358f169c0eaSGlenn Lagasse 	int exit_status;
3359f169c0eaSGlenn Lagasse 	int rval = BE_SUCCESS;
3360f169c0eaSGlenn Lagasse 
3361f169c0eaSGlenn Lagasse 	if ((command == NULL) || (stderr_buf == NULL) ||
3362f169c0eaSGlenn Lagasse 	    (stderr_bufsize <= 0) || (stdout_bufsize <  0) ||
3363f169c0eaSGlenn Lagasse 	    ((stdout_buf != NULL) ^ (stdout_bufsize != 0))) {
3364f169c0eaSGlenn Lagasse 		return (BE_ERR_INVAL);
33652b66e652SToomas Soome 	}
3366f169c0eaSGlenn Lagasse 
3367f169c0eaSGlenn Lagasse 	/* Set up command so popen returns stderr, not stdout */
3368f169c0eaSGlenn Lagasse 	if (snprintf(cmdline, BUFSIZ, "%s 2> %s", command,
3369f169c0eaSGlenn Lagasse 	    temp_filename) >= BUFSIZ) {
3370f169c0eaSGlenn Lagasse 		rval = BE_ERR_NOMEM;
3371f169c0eaSGlenn Lagasse 		goto cleanup;
3372f169c0eaSGlenn Lagasse 	}
3373f169c0eaSGlenn Lagasse 
3374f169c0eaSGlenn Lagasse 	/* Set up the fifo that will make stderr available. */
3375f169c0eaSGlenn Lagasse 	if (mkfifo(temp_filename, 0600) != 0) {
3376f169c0eaSGlenn Lagasse 		(void) be_print_err(gettext("be_run_cmd: mkfifo: %s\n"),
3377f169c0eaSGlenn Lagasse 		    strerror(errno));
3378f169c0eaSGlenn Lagasse 		rval = BE_ERR_EXTCMD;
3379f169c0eaSGlenn Lagasse 		goto cleanup;
3380f169c0eaSGlenn Lagasse 	}
3381f169c0eaSGlenn Lagasse 
3382f169c0eaSGlenn Lagasse 	if ((stdout_str = popen(cmdline, "r")) == NULL) {
3383f169c0eaSGlenn Lagasse 		(void) be_print_err(gettext("be_run_cmd: popen: %s\n"),
3384f169c0eaSGlenn Lagasse 		    strerror(errno));
3385f169c0eaSGlenn Lagasse 		rval = BE_ERR_EXTCMD;
3386f169c0eaSGlenn Lagasse 		goto cleanup;
3387f169c0eaSGlenn Lagasse 	}
3388f169c0eaSGlenn Lagasse 
3389f169c0eaSGlenn Lagasse 	if ((stderr_str = fopen(temp_filename, "r")) == NULL) {
3390f169c0eaSGlenn Lagasse 		(void) be_print_err(gettext("be_run_cmd: fopen: %s\n"),
3391f169c0eaSGlenn Lagasse 		    strerror(errno));
3392f169c0eaSGlenn Lagasse 		(void) pclose(stdout_str);
3393f169c0eaSGlenn Lagasse 		rval = BE_ERR_EXTCMD;
3394f169c0eaSGlenn Lagasse 		goto cleanup;
3395f169c0eaSGlenn Lagasse 	}
3396f169c0eaSGlenn Lagasse 
3397f169c0eaSGlenn Lagasse 	/* Read stdout first, as it usually outputs more than stderr. */
3398f169c0eaSGlenn Lagasse 	oneline[BUFSIZ-1] = '\0';
3399f169c0eaSGlenn Lagasse 	while (fgets(oneline, BUFSIZ-1, stdout_str) != NULL) {
3400f169c0eaSGlenn Lagasse 		if (stdout_str != NULL) {
3401f169c0eaSGlenn Lagasse 			(void) strlcat(stdout_buf, oneline, stdout_bufsize);
3402f169c0eaSGlenn Lagasse 		}
3403f169c0eaSGlenn Lagasse 	}
3404f169c0eaSGlenn Lagasse 
3405f169c0eaSGlenn Lagasse 	while (fgets(oneline, BUFSIZ-1, stderr_str) != NULL) {
3406f169c0eaSGlenn Lagasse 		(void) strlcat(stderr_buf, oneline, stderr_bufsize);
3407f169c0eaSGlenn Lagasse 	}
3408f169c0eaSGlenn Lagasse 
3409f169c0eaSGlenn Lagasse 	/* Close pipe, get exit status. */
3410f169c0eaSGlenn Lagasse 	if ((exit_status = pclose(stdout_str)) == -1) {
3411f169c0eaSGlenn Lagasse 		(void) be_print_err(gettext("be_run_cmd: pclose: %s\n"),
3412f169c0eaSGlenn Lagasse 		    strerror(errno));
3413f169c0eaSGlenn Lagasse 		rval = BE_ERR_EXTCMD;
3414f169c0eaSGlenn Lagasse 	} else if (WIFEXITED(exit_status)) {
3415f169c0eaSGlenn Lagasse 		exit_status = (int)((char)WEXITSTATUS(exit_status));
3416a63c99a2SToomas Soome 		/*
3417a63c99a2SToomas Soome 		 * error code BC_NOUPDT means more recent version
3418a63c99a2SToomas Soome 		 * is installed
3419a63c99a2SToomas Soome 		 */
3420a63c99a2SToomas Soome 		if (exit_status != BC_SUCCESS && exit_status != BC_NOUPDT) {
3421f169c0eaSGlenn Lagasse 			(void) snprintf(oneline, BUFSIZ, gettext("be_run_cmd: "
3422f169c0eaSGlenn Lagasse 			    "command terminated with error status: %d\n"),
3423f169c0eaSGlenn Lagasse 			    exit_status);
3424f169c0eaSGlenn Lagasse 			(void) strlcat(stderr_buf, oneline, stderr_bufsize);
3425f169c0eaSGlenn Lagasse 			rval = BE_ERR_EXTCMD;
3426f169c0eaSGlenn Lagasse 		}
3427f169c0eaSGlenn Lagasse 	} else {
3428f169c0eaSGlenn Lagasse 		(void) snprintf(oneline, BUFSIZ, gettext("be_run_cmd: command "
3429f169c0eaSGlenn Lagasse 		    "terminated on signal: %s\n"),
3430f169c0eaSGlenn Lagasse 		    strsignal(WTERMSIG(exit_status)));
3431f169c0eaSGlenn Lagasse 		(void) strlcat(stderr_buf, oneline, stderr_bufsize);
3432f169c0eaSGlenn Lagasse 		rval = BE_ERR_EXTCMD;
3433f169c0eaSGlenn Lagasse 	}
3434f169c0eaSGlenn Lagasse 
3435f169c0eaSGlenn Lagasse cleanup:
3436f169c0eaSGlenn Lagasse 	(void) unlink(temp_filename);
3437f169c0eaSGlenn Lagasse 	(void) free(temp_filename);
3438f169c0eaSGlenn Lagasse 
3439f169c0eaSGlenn Lagasse 	return (rval);
3440f169c0eaSGlenn Lagasse }
3441f169c0eaSGlenn Lagasse 
3442f169c0eaSGlenn Lagasse /* ********************************************************************	*/
3443f169c0eaSGlenn Lagasse /*			Private Functions				*/
3444f169c0eaSGlenn Lagasse /* ******************************************************************** */
3445f169c0eaSGlenn Lagasse 
3446f169c0eaSGlenn Lagasse /*
3447f169c0eaSGlenn Lagasse  * Function:	update_dataset
3448f169c0eaSGlenn Lagasse  * Description:	This function takes a dataset name and replaces the zpool
3449f169c0eaSGlenn Lagasse  *		and be_name components of the dataset with the new be_name
3450f169c0eaSGlenn Lagasse  *		zpool passed in.
3451f169c0eaSGlenn Lagasse  * Parameters:
3452f169c0eaSGlenn Lagasse  *		dataset - name of dataset
3453f169c0eaSGlenn Lagasse  *		dataset_len - lenth of buffer in which dataset is passed in.
3454f169c0eaSGlenn Lagasse  *		be_name - name of new BE name to update to.
3455f169c0eaSGlenn Lagasse  *		old_rc_loc - dataset under which the root container dataset
3456f169c0eaSGlenn Lagasse  *			for the old BE lives.
3457f169c0eaSGlenn Lagasse  *		new_rc_loc - dataset under which the root container dataset
3458f169c0eaSGlenn Lagasse  *			for the new BE lives.
3459f169c0eaSGlenn Lagasse  * Returns:
3460f169c0eaSGlenn Lagasse  *		BE_SUCCESS - Success
3461f169c0eaSGlenn Lagasse  *		be_errno_t - Failure
3462f169c0eaSGlenn Lagasse  * Scope:
3463f169c0eaSGlenn Lagasse  *		Private
3464f169c0eaSGlenn Lagasse  */
3465f169c0eaSGlenn Lagasse static int
update_dataset(char * dataset,int dataset_len,char * be_name,char * old_rc_loc,char * new_rc_loc)3466f169c0eaSGlenn Lagasse update_dataset(char *dataset, int dataset_len, char *be_name,
3467f169c0eaSGlenn Lagasse     char *old_rc_loc, char *new_rc_loc)
3468f169c0eaSGlenn Lagasse {
3469f169c0eaSGlenn Lagasse 	char	*ds = NULL;
3470f169c0eaSGlenn Lagasse 	char	*sub_ds = NULL;
3471*ec8422d0SAndy Fiddaman 	int	ret;
3472f169c0eaSGlenn Lagasse 
3473f169c0eaSGlenn Lagasse 	/* Tear off the BE container dataset */
3474f169c0eaSGlenn Lagasse 	if ((ds = be_make_name_from_ds(dataset, old_rc_loc)) == NULL) {
3475f169c0eaSGlenn Lagasse 		return (BE_ERR_INVAL);
3476f169c0eaSGlenn Lagasse 	}
3477f169c0eaSGlenn Lagasse 
3478f169c0eaSGlenn Lagasse 	/* Get dataset name relative to BE root, if there is one */
3479f169c0eaSGlenn Lagasse 	sub_ds = strchr(ds, '/');
3480f169c0eaSGlenn Lagasse 
3481f169c0eaSGlenn Lagasse 	/* Generate the BE root dataset name */
3482*ec8422d0SAndy Fiddaman 	if ((ret = be_make_root_ds(new_rc_loc, be_name, dataset,
3483*ec8422d0SAndy Fiddaman 	    dataset_len)) != BE_SUCCESS) {
3484*ec8422d0SAndy Fiddaman 		return (ret);
3485*ec8422d0SAndy Fiddaman 	}
3486f169c0eaSGlenn Lagasse 
3487f169c0eaSGlenn Lagasse 	/* If a subordinate dataset name was found, append it */
3488f169c0eaSGlenn Lagasse 	if (sub_ds != NULL)
3489f169c0eaSGlenn Lagasse 		(void) strlcat(dataset, sub_ds, dataset_len);
3490f169c0eaSGlenn Lagasse 
3491f169c0eaSGlenn Lagasse 	free(ds);
3492f169c0eaSGlenn Lagasse 	return (BE_SUCCESS);
3493f169c0eaSGlenn Lagasse }
3494f169c0eaSGlenn Lagasse 
3495f169c0eaSGlenn Lagasse /*
3496f169c0eaSGlenn Lagasse  * Function:	_update_vfstab
3497f169c0eaSGlenn Lagasse  * Description:	This function updates a vfstab file to reflect the new
3498f169c0eaSGlenn Lagasse  *		root container dataset location and be_name for all
3499f169c0eaSGlenn Lagasse  *		entries listed in the be_fs_list_data_t structure passed in.
3500f169c0eaSGlenn Lagasse  * Parameters:
3501f169c0eaSGlenn Lagasse  *		vfstab - vfstab file to modify
3502f169c0eaSGlenn Lagasse  *		be_name - name of BE to update.
3503f169c0eaSGlenn Lagasse  *		old_rc_loc - dataset under which the root container dataset
3504f169c0eaSGlenn Lagasse  *			of the old BE resides in.
3505f169c0eaSGlenn Lagasse  *		new_rc_loc - dataset under which the root container dataset
3506f169c0eaSGlenn Lagasse  *			of the new BE resides in.
3507f169c0eaSGlenn Lagasse  *		fld - be_fs_list_data_t pointer providing the list of
3508f169c0eaSGlenn Lagasse  *			file systems to look for in vfstab.
3509f169c0eaSGlenn Lagasse  * Returns:
3510f169c0eaSGlenn Lagasse  *		BE_SUCCESS - Success
3511f169c0eaSGlenn Lagasse  *		be_errno_t - Failure
3512f169c0eaSGlenn Lagasse  * Scope:
3513f169c0eaSGlenn Lagasse  *		Private
3514f169c0eaSGlenn Lagasse  */
3515f169c0eaSGlenn Lagasse static int
_update_vfstab(char * vfstab,char * be_name,char * old_rc_loc,char * new_rc_loc,be_fs_list_data_t * fld)3516f169c0eaSGlenn Lagasse _update_vfstab(char *vfstab, char *be_name, char *old_rc_loc,
3517f169c0eaSGlenn Lagasse     char *new_rc_loc, be_fs_list_data_t *fld)
3518f169c0eaSGlenn Lagasse {
3519f169c0eaSGlenn Lagasse 	struct vfstab	vp;
3520f169c0eaSGlenn Lagasse 	char		*tmp_vfstab = NULL;
3521f169c0eaSGlenn Lagasse 	char		comments_buf[BUFSIZ];
3522f169c0eaSGlenn Lagasse 	FILE		*comments = NULL;
3523f169c0eaSGlenn Lagasse 	FILE		*vfs_ents = NULL;
3524f169c0eaSGlenn Lagasse 	FILE		*tfile = NULL;
3525f169c0eaSGlenn Lagasse 	struct stat	sb;
3526f169c0eaSGlenn Lagasse 	char		dev[MAXPATHLEN];
3527f169c0eaSGlenn Lagasse 	char		*c;
3528f169c0eaSGlenn Lagasse 	int		fd;
3529f169c0eaSGlenn Lagasse 	int		ret = BE_SUCCESS, err = 0;
3530f169c0eaSGlenn Lagasse 	int		i;
3531f169c0eaSGlenn Lagasse 	int		tmp_vfstab_len = 0;
3532f169c0eaSGlenn Lagasse 
3533f169c0eaSGlenn Lagasse 	errno = 0;
3534f169c0eaSGlenn Lagasse 
3535f169c0eaSGlenn Lagasse 	/*
3536f169c0eaSGlenn Lagasse 	 * Open vfstab for reading twice.  First is for comments,
3537f169c0eaSGlenn Lagasse 	 * second is for actual entries.
3538f169c0eaSGlenn Lagasse 	 */
3539f169c0eaSGlenn Lagasse 	if ((comments = fopen(vfstab, "r")) == NULL ||
3540f169c0eaSGlenn Lagasse 	    (vfs_ents = fopen(vfstab, "r")) == NULL) {
3541f169c0eaSGlenn Lagasse 		err = errno;
3542f169c0eaSGlenn Lagasse 		be_print_err(gettext("_update_vfstab: "
3543f169c0eaSGlenn Lagasse 		    "failed to open vfstab (%s): %s\n"), vfstab,
3544f169c0eaSGlenn Lagasse 		    strerror(err));
3545f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
3546f169c0eaSGlenn Lagasse 		goto cleanup;
3547f169c0eaSGlenn Lagasse 	}
3548f169c0eaSGlenn Lagasse 
3549f169c0eaSGlenn Lagasse 	/* Grab the stats of the original vfstab file */
3550f169c0eaSGlenn Lagasse 	if (stat(vfstab, &sb) != 0) {
3551f169c0eaSGlenn Lagasse 		err = errno;
3552f169c0eaSGlenn Lagasse 		be_print_err(gettext("_update_vfstab: "
3553f169c0eaSGlenn Lagasse 		    "failed to stat file %s: %s\n"), vfstab,
3554f169c0eaSGlenn Lagasse 		    strerror(err));
3555f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
3556f169c0eaSGlenn Lagasse 		goto cleanup;
3557f169c0eaSGlenn Lagasse 	}
3558f169c0eaSGlenn Lagasse 
3559f169c0eaSGlenn Lagasse 	/* Create tmp file for modified vfstab */
3560f169c0eaSGlenn Lagasse 	if ((tmp_vfstab = (char *)malloc(strlen(vfstab) + 7))
3561f169c0eaSGlenn Lagasse 	    == NULL) {
3562f169c0eaSGlenn Lagasse 		be_print_err(gettext("_update_vfstab: "
3563f169c0eaSGlenn Lagasse 		    "malloc failed\n"));
3564f169c0eaSGlenn Lagasse 		ret = BE_ERR_NOMEM;
3565f169c0eaSGlenn Lagasse 		goto cleanup;
3566f169c0eaSGlenn Lagasse 	}
3567f169c0eaSGlenn Lagasse 	tmp_vfstab_len = strlen(vfstab) + 7;
3568f169c0eaSGlenn Lagasse 	(void) memset(tmp_vfstab, 0, tmp_vfstab_len);
3569f169c0eaSGlenn Lagasse 	(void) strlcpy(tmp_vfstab, vfstab, tmp_vfstab_len);
3570f169c0eaSGlenn Lagasse 	(void) strlcat(tmp_vfstab, "XXXXXX", tmp_vfstab_len);
3571f169c0eaSGlenn Lagasse 	if ((fd = mkstemp(tmp_vfstab)) == -1) {
3572f169c0eaSGlenn Lagasse 		err = errno;
3573f169c0eaSGlenn Lagasse 		be_print_err(gettext("_update_vfstab: "
3574f169c0eaSGlenn Lagasse 		    "mkstemp failed: %s\n"), strerror(err));
3575f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
3576f169c0eaSGlenn Lagasse 		goto cleanup;
3577f169c0eaSGlenn Lagasse 	}
3578f169c0eaSGlenn Lagasse 	if ((tfile = fdopen(fd, "w")) == NULL) {
3579f169c0eaSGlenn Lagasse 		err = errno;
3580f169c0eaSGlenn Lagasse 		be_print_err(gettext("_update_vfstab: "
3581f169c0eaSGlenn Lagasse 		    "could not open file for write\n"));
3582f169c0eaSGlenn Lagasse 		(void) close(fd);
3583f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
3584f169c0eaSGlenn Lagasse 		goto cleanup;
3585f169c0eaSGlenn Lagasse 	}
3586f169c0eaSGlenn Lagasse 
3587f169c0eaSGlenn Lagasse 	while (fgets(comments_buf, BUFSIZ, comments)) {
3588f169c0eaSGlenn Lagasse 		for (c = comments_buf; *c != '\0' && isspace(*c); c++)
3589f169c0eaSGlenn Lagasse 			;
3590f169c0eaSGlenn Lagasse 		if (*c == '\0') {
3591f169c0eaSGlenn Lagasse 			continue;
3592f169c0eaSGlenn Lagasse 		} else if (*c == '#') {
3593f169c0eaSGlenn Lagasse 			/*
3594f169c0eaSGlenn Lagasse 			 * If line is a comment line, just put
3595f169c0eaSGlenn Lagasse 			 * it through to the tmp vfstab.
3596f169c0eaSGlenn Lagasse 			 */
3597f169c0eaSGlenn Lagasse 			(void) fputs(comments_buf, tfile);
3598f169c0eaSGlenn Lagasse 		} else {
3599f169c0eaSGlenn Lagasse 			/*
3600f169c0eaSGlenn Lagasse 			 * Else line is a vfstab entry, grab it
3601f169c0eaSGlenn Lagasse 			 * into a vfstab struct.
3602f169c0eaSGlenn Lagasse 			 */
3603f169c0eaSGlenn Lagasse 			if (getvfsent(vfs_ents, &vp) != 0) {
3604f169c0eaSGlenn Lagasse 				err = errno;
3605f169c0eaSGlenn Lagasse 				be_print_err(gettext("_update_vfstab: "
3606f169c0eaSGlenn Lagasse 				    "getvfsent failed: %s\n"), strerror(err));
3607f169c0eaSGlenn Lagasse 				ret = errno_to_be_err(err);
3608f169c0eaSGlenn Lagasse 				goto cleanup;
3609f169c0eaSGlenn Lagasse 			}
3610f169c0eaSGlenn Lagasse 
3611f169c0eaSGlenn Lagasse 			if (vp.vfs_special == NULL || vp.vfs_mountp == NULL) {
3612f169c0eaSGlenn Lagasse 				(void) putvfsent(tfile, &vp);
3613f169c0eaSGlenn Lagasse 				continue;
3614f169c0eaSGlenn Lagasse 			}
3615f169c0eaSGlenn Lagasse 
3616f169c0eaSGlenn Lagasse 			/*
3617f169c0eaSGlenn Lagasse 			 * If the entry is one of the entries in the list
3618f169c0eaSGlenn Lagasse 			 * of file systems to update, modify it's device
3619f169c0eaSGlenn Lagasse 			 * field to be correct for this BE.
3620f169c0eaSGlenn Lagasse 			 */
3621f169c0eaSGlenn Lagasse 			for (i = 0; i < fld->fs_num; i++) {
3622f169c0eaSGlenn Lagasse 				if (strcmp(vp.vfs_special, fld->fs_list[i])
3623f169c0eaSGlenn Lagasse 				    == 0) {
3624f169c0eaSGlenn Lagasse 					/*
3625f169c0eaSGlenn Lagasse 					 * Found entry that needs an update.
3626f169c0eaSGlenn Lagasse 					 * Replace the root container dataset
3627f169c0eaSGlenn Lagasse 					 * location and be_name in the
3628f169c0eaSGlenn Lagasse 					 * entry's device.
3629f169c0eaSGlenn Lagasse 					 */
3630f169c0eaSGlenn Lagasse 					(void) strlcpy(dev, vp.vfs_special,
3631f169c0eaSGlenn Lagasse 					    sizeof (dev));
3632f169c0eaSGlenn Lagasse 
3633f169c0eaSGlenn Lagasse 					if ((ret = update_dataset(dev,
3634f169c0eaSGlenn Lagasse 					    sizeof (dev), be_name, old_rc_loc,
3635f169c0eaSGlenn Lagasse 					    new_rc_loc)) != 0) {
3636f169c0eaSGlenn Lagasse 						be_print_err(
3637f169c0eaSGlenn Lagasse 						    gettext("_update_vfstab: "
3638f169c0eaSGlenn Lagasse 						    "Failed to update device "
3639f169c0eaSGlenn Lagasse 						    "field for vfstab entry "
3640f169c0eaSGlenn Lagasse 						    "%s\n"), fld->fs_list[i]);
3641f169c0eaSGlenn Lagasse 						goto cleanup;
3642f169c0eaSGlenn Lagasse 					}
3643f169c0eaSGlenn Lagasse 
3644f169c0eaSGlenn Lagasse 					vp.vfs_special = dev;
3645f169c0eaSGlenn Lagasse 					break;
3646f169c0eaSGlenn Lagasse 				}
3647f169c0eaSGlenn Lagasse 			}
3648f169c0eaSGlenn Lagasse 
3649f169c0eaSGlenn Lagasse 			/* Put entry through to tmp vfstab */
3650f169c0eaSGlenn Lagasse 			(void) putvfsent(tfile, &vp);
3651f169c0eaSGlenn Lagasse 		}
3652f169c0eaSGlenn Lagasse 	}
3653f169c0eaSGlenn Lagasse 
3654f169c0eaSGlenn Lagasse 	(void) fclose(comments);
3655f169c0eaSGlenn Lagasse 	comments = NULL;
3656f169c0eaSGlenn Lagasse 	(void) fclose(vfs_ents);
3657f169c0eaSGlenn Lagasse 	vfs_ents = NULL;
3658f169c0eaSGlenn Lagasse 	(void) fclose(tfile);
3659f169c0eaSGlenn Lagasse 	tfile = NULL;
3660f169c0eaSGlenn Lagasse 
3661f169c0eaSGlenn Lagasse 	/* Copy tmp vfstab into place */
3662f169c0eaSGlenn Lagasse 	if (rename(tmp_vfstab, vfstab) != 0) {
3663f169c0eaSGlenn Lagasse 		err = errno;
3664f169c0eaSGlenn Lagasse 		be_print_err(gettext("_update_vfstab: "
3665f169c0eaSGlenn Lagasse 		    "failed to rename file %s to %s: %s\n"), tmp_vfstab,
3666f169c0eaSGlenn Lagasse 		    vfstab, strerror(err));
3667f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
3668f169c0eaSGlenn Lagasse 		goto cleanup;
3669f169c0eaSGlenn Lagasse 	}
3670f169c0eaSGlenn Lagasse 
3671f169c0eaSGlenn Lagasse 	/* Set the perms and ownership of the updated file */
3672f169c0eaSGlenn Lagasse 	if (chmod(vfstab, sb.st_mode) != 0) {
3673f169c0eaSGlenn Lagasse 		err = errno;
3674f169c0eaSGlenn Lagasse 		be_print_err(gettext("_update_vfstab: "
3675f169c0eaSGlenn Lagasse 		    "failed to chmod %s: %s\n"), vfstab, strerror(err));
3676f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
3677f169c0eaSGlenn Lagasse 		goto cleanup;
3678f169c0eaSGlenn Lagasse 	}
3679f169c0eaSGlenn Lagasse 	if (chown(vfstab, sb.st_uid, sb.st_gid) != 0) {
3680f169c0eaSGlenn Lagasse 		err = errno;
3681f169c0eaSGlenn Lagasse 		be_print_err(gettext("_update_vfstab: "
3682f169c0eaSGlenn Lagasse 		    "failed to chown %s: %s\n"), vfstab, strerror(err));
3683f169c0eaSGlenn Lagasse 		ret = errno_to_be_err(err);
3684f169c0eaSGlenn Lagasse 		goto cleanup;
3685f169c0eaSGlenn Lagasse 	}
3686f169c0eaSGlenn Lagasse 
3687f169c0eaSGlenn Lagasse cleanup:
3688f169c0eaSGlenn Lagasse 	if (comments != NULL)
3689f169c0eaSGlenn Lagasse 		(void) fclose(comments);
3690f169c0eaSGlenn Lagasse 	if (vfs_ents != NULL)
3691f169c0eaSGlenn Lagasse 		(void) fclose(vfs_ents);
3692f169c0eaSGlenn Lagasse 	(void) unlink(tmp_vfstab);
3693f169c0eaSGlenn Lagasse 	(void) free(tmp_vfstab);
3694f169c0eaSGlenn Lagasse 	if (tfile != NULL)
3695f169c0eaSGlenn Lagasse 		(void) fclose(tfile);
3696f169c0eaSGlenn Lagasse 
3697f169c0eaSGlenn Lagasse 	return (ret);
3698f169c0eaSGlenn Lagasse }
3699f169c0eaSGlenn Lagasse 
3700f169c0eaSGlenn Lagasse 
3701f169c0eaSGlenn Lagasse /*
3702f169c0eaSGlenn Lagasse  * Function:	be_get_auto_name
3703f169c0eaSGlenn Lagasse  * Description:	Generate an auto name constructed based on the BE name
3704f169c0eaSGlenn Lagasse  *		of the original BE or zone BE being cloned.
3705f169c0eaSGlenn Lagasse  * Parameters:
3706f169c0eaSGlenn Lagasse  *		obe_name - name of the original BE or zone BE being cloned.
3707f169c0eaSGlenn Lagasse  *              container_ds - container dataset for the zone.
3708f169c0eaSGlenn Lagasse  *                             Note: if zone_be is false this should be
3709f169c0eaSGlenn Lagasse  *                                  NULL.
3710f169c0eaSGlenn Lagasse  *		zone_be - flag that indicates if we are operating on a zone BE.
3711f169c0eaSGlenn Lagasse  * Returns:
3712f169c0eaSGlenn Lagasse  *		Success - pointer to auto generated BE name.  The name
3713f169c0eaSGlenn Lagasse  *			is allocated in heap storage so the caller is
3714f169c0eaSGlenn Lagasse  *			responsible for free'ing the name.
3715f169c0eaSGlenn Lagasse  *		Failure - NULL
3716f169c0eaSGlenn Lagasse  * Scope:
3717f169c0eaSGlenn Lagasse  *		Private
3718f169c0eaSGlenn Lagasse  */
3719f169c0eaSGlenn Lagasse static char *
be_get_auto_name(char * obe_name,char * be_container_ds,boolean_t zone_be)3720f169c0eaSGlenn Lagasse be_get_auto_name(char *obe_name, char *be_container_ds, boolean_t zone_be)
3721f169c0eaSGlenn Lagasse {
3722f169c0eaSGlenn Lagasse 	be_node_list_t	*be_nodes = NULL;
3723f169c0eaSGlenn Lagasse 	be_node_list_t	*cur_be = NULL;
3724f169c0eaSGlenn Lagasse 	char		auto_be_name[MAXPATHLEN];
3725f169c0eaSGlenn Lagasse 	char		base_be_name[MAXPATHLEN];
3726f169c0eaSGlenn Lagasse 	char		cur_be_name[MAXPATHLEN];
3727f169c0eaSGlenn Lagasse 	char		*num_str = NULL;
3728f169c0eaSGlenn Lagasse 	char		*c = NULL;
3729f169c0eaSGlenn Lagasse 	int		num = 0;
3730f169c0eaSGlenn Lagasse 	int		cur_num = 0;
3731f169c0eaSGlenn Lagasse 
3732f169c0eaSGlenn Lagasse 	errno = 0;
3733f169c0eaSGlenn Lagasse 
3734f169c0eaSGlenn Lagasse 	/*
3735f169c0eaSGlenn Lagasse 	 * Check if obe_name is already in an auto BE name format.
3736f169c0eaSGlenn Lagasse 	 * If it is, then strip off the increment number to get the
3737f169c0eaSGlenn Lagasse 	 * base name.
3738f169c0eaSGlenn Lagasse 	 */
3739f169c0eaSGlenn Lagasse 	(void) strlcpy(base_be_name, obe_name, sizeof (base_be_name));
3740f169c0eaSGlenn Lagasse 
3741f169c0eaSGlenn Lagasse 	if ((num_str = strrchr(base_be_name, BE_AUTO_NAME_DELIM))
3742f169c0eaSGlenn Lagasse 	    != NULL) {
3743f169c0eaSGlenn Lagasse 		/* Make sure remaining string is all digits */
3744f169c0eaSGlenn Lagasse 		c = num_str + 1;
3745f169c0eaSGlenn Lagasse 		while (c[0] != '\0' && isdigit(c[0]))
3746f169c0eaSGlenn Lagasse 			c++;
3747f169c0eaSGlenn Lagasse 		/*
3748f169c0eaSGlenn Lagasse 		 * If we're now at the end of the string strip off the
3749f169c0eaSGlenn Lagasse 		 * increment number.
3750f169c0eaSGlenn Lagasse 		 */
3751f169c0eaSGlenn Lagasse 		if (c[0] == '\0')
3752f169c0eaSGlenn Lagasse 			num_str[0] = '\0';
3753f169c0eaSGlenn Lagasse 	}
3754f169c0eaSGlenn Lagasse 
3755f169c0eaSGlenn Lagasse 	if (zone_be) {
3756f169c0eaSGlenn Lagasse 		if (be_container_ds == NULL)
3757f169c0eaSGlenn Lagasse 			return (NULL);
3758f169c0eaSGlenn Lagasse 		if (be_get_zone_be_list(obe_name, be_container_ds,
3759f169c0eaSGlenn Lagasse 		    &be_nodes) != BE_SUCCESS) {
3760f169c0eaSGlenn Lagasse 			be_print_err(gettext("be_get_auto_name: "
3761f169c0eaSGlenn Lagasse 			    "be_get_zone_be_list failed\n"));
3762f169c0eaSGlenn Lagasse 			return (NULL);
3763f169c0eaSGlenn Lagasse 		}
3764a897f28bSAndy Fiddaman 	} else if (_be_list(NULL, &be_nodes, BE_LIST_DEFAULT) != BE_SUCCESS) {
3765f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_get_auto_name: be_list failed\n"));
3766f169c0eaSGlenn Lagasse 		return (NULL);
3767f169c0eaSGlenn Lagasse 	}
3768f169c0eaSGlenn Lagasse 
3769f169c0eaSGlenn Lagasse 	for (cur_be = be_nodes; cur_be != NULL; cur_be = cur_be->be_next_node) {
3770f169c0eaSGlenn Lagasse 		(void) strlcpy(cur_be_name, cur_be->be_node_name,
3771f169c0eaSGlenn Lagasse 		    sizeof (cur_be_name));
3772f169c0eaSGlenn Lagasse 
3773f169c0eaSGlenn Lagasse 		/* If cur_be_name doesn't match at least base be name, skip. */
3774f169c0eaSGlenn Lagasse 		if (strncmp(cur_be_name, base_be_name, strlen(base_be_name))
3775f169c0eaSGlenn Lagasse 		    != 0)
3776f169c0eaSGlenn Lagasse 			continue;
3777f169c0eaSGlenn Lagasse 
3778f169c0eaSGlenn Lagasse 		/* Get the string following the base be name */
3779f169c0eaSGlenn Lagasse 		num_str = cur_be_name + strlen(base_be_name);
3780f169c0eaSGlenn Lagasse 
3781f169c0eaSGlenn Lagasse 		/*
3782f169c0eaSGlenn Lagasse 		 * If nothing follows the base be name, this cur_be_name
3783f169c0eaSGlenn Lagasse 		 * is the BE named with the base be name, skip.
3784f169c0eaSGlenn Lagasse 		 */
3785f169c0eaSGlenn Lagasse 		if (num_str == NULL || num_str[0] == '\0')
3786f169c0eaSGlenn Lagasse 			continue;
3787f169c0eaSGlenn Lagasse 
3788f169c0eaSGlenn Lagasse 		/*
3789f169c0eaSGlenn Lagasse 		 * Remove the name delimiter.  If its not there,
3790f169c0eaSGlenn Lagasse 		 * cur_be_name isn't part of this BE name stream, skip.
3791f169c0eaSGlenn Lagasse 		 */
3792f169c0eaSGlenn Lagasse 		if (num_str[0] == BE_AUTO_NAME_DELIM)
3793f169c0eaSGlenn Lagasse 			num_str++;
3794f169c0eaSGlenn Lagasse 		else
3795f169c0eaSGlenn Lagasse 			continue;
3796f169c0eaSGlenn Lagasse 
3797f169c0eaSGlenn Lagasse 		/* Make sure remaining string is all digits */
3798f169c0eaSGlenn Lagasse 		c = num_str;
3799f169c0eaSGlenn Lagasse 		while (c[0] != '\0' && isdigit(c[0]))
3800f169c0eaSGlenn Lagasse 			c++;
3801f169c0eaSGlenn Lagasse 		if (c[0] != '\0')
3802f169c0eaSGlenn Lagasse 			continue;
3803f169c0eaSGlenn Lagasse 
3804f169c0eaSGlenn Lagasse 		/* Convert the number string to an int */
3805f169c0eaSGlenn Lagasse 		cur_num = atoi(num_str);
3806f169c0eaSGlenn Lagasse 
3807f169c0eaSGlenn Lagasse 		/*
3808f169c0eaSGlenn Lagasse 		 * If failed to convert the string, skip it.  If its too
3809f169c0eaSGlenn Lagasse 		 * long to be converted to an int, we wouldn't auto generate
3810f169c0eaSGlenn Lagasse 		 * this number anyway so there couldn't be a conflict.
3811f169c0eaSGlenn Lagasse 		 * We treat it as a manually created BE name.
3812f169c0eaSGlenn Lagasse 		 */
3813f169c0eaSGlenn Lagasse 		if (cur_num == 0 && errno == EINVAL)
3814f169c0eaSGlenn Lagasse 			continue;
3815f169c0eaSGlenn Lagasse 
3816f169c0eaSGlenn Lagasse 		/*
3817f169c0eaSGlenn Lagasse 		 * Compare current number to current max number,
3818f169c0eaSGlenn Lagasse 		 * take higher of the two.
3819f169c0eaSGlenn Lagasse 		 */
3820f169c0eaSGlenn Lagasse 		if (cur_num > num)
3821f169c0eaSGlenn Lagasse 			num = cur_num;
3822f169c0eaSGlenn Lagasse 	}
3823f169c0eaSGlenn Lagasse 
3824f169c0eaSGlenn Lagasse 	/*
3825f169c0eaSGlenn Lagasse 	 * Store off a copy of 'num' incase we need it later.  If incrementing
3826f169c0eaSGlenn Lagasse 	 * 'num' causes it to roll over, this means 'num' is the largest
3827f169c0eaSGlenn Lagasse 	 * positive int possible; we'll need it later in the loop to determine
3828f169c0eaSGlenn Lagasse 	 * if we've exhausted all possible increment numbers.  We store it in
3829f169c0eaSGlenn Lagasse 	 * 'cur_num'.
3830f169c0eaSGlenn Lagasse 	 */
3831f169c0eaSGlenn Lagasse 	cur_num = num;
3832f169c0eaSGlenn Lagasse 
3833f169c0eaSGlenn Lagasse 	/* Increment 'num' to get new auto BE name number */
3834f169c0eaSGlenn Lagasse 	if (++num <= 0) {
3835f169c0eaSGlenn Lagasse 		int ret = 0;
3836f169c0eaSGlenn Lagasse 
3837f169c0eaSGlenn Lagasse 		/*
3838f169c0eaSGlenn Lagasse 		 * Since incrementing 'num' caused it to rollover, start
3839f169c0eaSGlenn Lagasse 		 * over at 0 and find the first available number.
3840f169c0eaSGlenn Lagasse 		 */
3841f169c0eaSGlenn Lagasse 		for (num = 0; num < cur_num; num++) {
3842f169c0eaSGlenn Lagasse 
3843f169c0eaSGlenn Lagasse 			(void) snprintf(cur_be_name, sizeof (cur_be_name),
3844f169c0eaSGlenn Lagasse 			    "%s%c%d", base_be_name, BE_AUTO_NAME_DELIM, num);
3845f169c0eaSGlenn Lagasse 
3846f169c0eaSGlenn Lagasse 			ret = zpool_iter(g_zfs, be_exists_callback,
3847f169c0eaSGlenn Lagasse 			    cur_be_name);
3848f169c0eaSGlenn Lagasse 
3849f169c0eaSGlenn Lagasse 			if (ret == 0) {
3850f169c0eaSGlenn Lagasse 				/*
3851f169c0eaSGlenn Lagasse 				 * BE name doesn't exist, break out
3852f169c0eaSGlenn Lagasse 				 * to use 'num'.
3853f169c0eaSGlenn Lagasse 				 */
3854f169c0eaSGlenn Lagasse 				break;
3855f169c0eaSGlenn Lagasse 			} else if (ret == 1) {
3856f169c0eaSGlenn Lagasse 				/* BE name exists, continue looking */
3857f169c0eaSGlenn Lagasse 				continue;
3858f169c0eaSGlenn Lagasse 			} else {
3859f169c0eaSGlenn Lagasse 				be_print_err(gettext("be_get_auto_name: "
3860f169c0eaSGlenn Lagasse 				    "zpool_iter failed: %s\n"),
3861f169c0eaSGlenn Lagasse 				    libzfs_error_description(g_zfs));
3862f169c0eaSGlenn Lagasse 				be_free_list(be_nodes);
3863f169c0eaSGlenn Lagasse 				return (NULL);
3864f169c0eaSGlenn Lagasse 			}
3865f169c0eaSGlenn Lagasse 		}
3866f169c0eaSGlenn Lagasse 
3867f169c0eaSGlenn Lagasse 		/*
3868f169c0eaSGlenn Lagasse 		 * If 'num' equals 'cur_num', we've exhausted all possible
3869f169c0eaSGlenn Lagasse 		 * auto BE names for this base BE name.
3870f169c0eaSGlenn Lagasse 		 */
3871f169c0eaSGlenn Lagasse 		if (num == cur_num) {
3872f169c0eaSGlenn Lagasse 			be_print_err(gettext("be_get_auto_name: "
3873f169c0eaSGlenn Lagasse 			    "No more available auto BE names for base "
3874f169c0eaSGlenn Lagasse 			    "BE name %s\n"), base_be_name);
3875f169c0eaSGlenn Lagasse 			be_free_list(be_nodes);
3876f169c0eaSGlenn Lagasse 			return (NULL);
3877f169c0eaSGlenn Lagasse 		}
3878f169c0eaSGlenn Lagasse 	}
3879f169c0eaSGlenn Lagasse 
3880f169c0eaSGlenn Lagasse 	be_free_list(be_nodes);
3881f169c0eaSGlenn Lagasse 
3882f169c0eaSGlenn Lagasse 	/*
3883f169c0eaSGlenn Lagasse 	 * Generate string for auto BE name.
3884f169c0eaSGlenn Lagasse 	 */
3885f169c0eaSGlenn Lagasse 	(void) snprintf(auto_be_name, sizeof (auto_be_name), "%s%c%d",
3886f169c0eaSGlenn Lagasse 	    base_be_name, BE_AUTO_NAME_DELIM, num);
3887f169c0eaSGlenn Lagasse 
3888f169c0eaSGlenn Lagasse 	if ((c = strdup(auto_be_name)) == NULL) {
3889f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_get_auto_name: "
3890f169c0eaSGlenn Lagasse 		    "memory allocation failed\n"));
3891f169c0eaSGlenn Lagasse 		return (NULL);
3892f169c0eaSGlenn Lagasse 	}
3893f169c0eaSGlenn Lagasse 
3894f169c0eaSGlenn Lagasse 	return (c);
3895f169c0eaSGlenn Lagasse }
3896f169c0eaSGlenn Lagasse 
38976b1d07a4SAlexander Eremin /*
38986b1d07a4SAlexander Eremin  * Function:	be_get_console_prop
38996b1d07a4SAlexander Eremin  * Description:	Determine console device.
39006b1d07a4SAlexander Eremin  * Returns:
39016b1d07a4SAlexander Eremin  *		Success - pointer to console setting.
39026b1d07a4SAlexander Eremin  *		Failure - NULL
39036b1d07a4SAlexander Eremin  * Scope:
39046b1d07a4SAlexander Eremin  *		Private
39056b1d07a4SAlexander Eremin  */
39066b1d07a4SAlexander Eremin static char *
be_get_console_prop(void)39076b1d07a4SAlexander Eremin be_get_console_prop(void)
39086b1d07a4SAlexander Eremin {
39096b1d07a4SAlexander Eremin 	di_node_t	dn;
39106b1d07a4SAlexander Eremin 	char *console = NULL;
39116b1d07a4SAlexander Eremin 
39126b1d07a4SAlexander Eremin 	if ((dn = di_init("/", DINFOPROP)) == DI_NODE_NIL) {
39136b1d07a4SAlexander Eremin 		be_print_err(gettext("be_get_console_prop: "
39146b1d07a4SAlexander Eremin 		    "di_init() failed\n"));
39156b1d07a4SAlexander Eremin 		return (NULL);
39166b1d07a4SAlexander Eremin 	}
39176b1d07a4SAlexander Eremin 
39186b1d07a4SAlexander Eremin 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, dn,
39196b1d07a4SAlexander Eremin 	    "console", &console) != -1) {
39206b1d07a4SAlexander Eremin 		di_fini(dn);
39216b1d07a4SAlexander Eremin 		return (console);
39226b1d07a4SAlexander Eremin 	}
39236b1d07a4SAlexander Eremin 
39246b1d07a4SAlexander Eremin 	if (console == NULL) {
39256b1d07a4SAlexander Eremin 		if (di_prop_lookup_strings(DDI_DEV_T_ANY, dn,
39266b1d07a4SAlexander Eremin 		    "output-device", &console) != -1) {
39276b1d07a4SAlexander Eremin 			di_fini(dn);
39286b1d07a4SAlexander Eremin 			if (strncmp(console, "screen", strlen("screen")) == 0)
39296b1d07a4SAlexander Eremin 				console = BE_DEFAULT_CONSOLE;
39306b1d07a4SAlexander Eremin 		}
39316b1d07a4SAlexander Eremin 	}
39326b1d07a4SAlexander Eremin 
39336b1d07a4SAlexander Eremin 	/*
39346b1d07a4SAlexander Eremin 	 * Default console to text
39356b1d07a4SAlexander Eremin 	 */
39366b1d07a4SAlexander Eremin 	if (console == NULL) {
39376b1d07a4SAlexander Eremin 		console = BE_DEFAULT_CONSOLE;
39386b1d07a4SAlexander Eremin 	}
39396b1d07a4SAlexander Eremin 
39406b1d07a4SAlexander Eremin 	return (console);
39416b1d07a4SAlexander Eremin }
39426b1d07a4SAlexander Eremin 
3943f169c0eaSGlenn Lagasse /*
3944f169c0eaSGlenn Lagasse  * Function:	be_create_menu
3945f169c0eaSGlenn Lagasse  * Description:
3946f169c0eaSGlenn Lagasse  *		This function is used if no menu.lst file exists. In
3947f169c0eaSGlenn Lagasse  *		this case a new file is created and if needed default
3948f169c0eaSGlenn Lagasse  *		lines are added to the file.
3949f169c0eaSGlenn Lagasse  * Parameters:
3950f169c0eaSGlenn Lagasse  *		pool - The name of the pool the menu.lst file is on
3951f169c0eaSGlenn Lagasse  *		menu_file - The name of the file we're creating.
3952f169c0eaSGlenn Lagasse  *		menu_fp - A pointer to the file pointer of the file we
3953f169c0eaSGlenn Lagasse  *			  created. This is also used to pass back the file
3954f169c0eaSGlenn Lagasse  *			  pointer to the newly created file.
3955f169c0eaSGlenn Lagasse  *		mode - the original mode used for the failed attempt to
3956f169c0eaSGlenn Lagasse  *		       non-existent file.
3957f169c0eaSGlenn Lagasse  * Returns:
3958f169c0eaSGlenn Lagasse  *		BE_SUCCESS - Success
3959f169c0eaSGlenn Lagasse  *		be_errno_t - Failure
3960f169c0eaSGlenn Lagasse  * Scope:
3961f169c0eaSGlenn Lagasse  *		Private
3962f169c0eaSGlenn Lagasse  */
3963f169c0eaSGlenn Lagasse static int
be_create_menu(char * pool,char * menu_file,FILE ** menu_fp,char * mode)3964f169c0eaSGlenn Lagasse be_create_menu(
3965f169c0eaSGlenn Lagasse 	char *pool,
3966f169c0eaSGlenn Lagasse 	char *menu_file,
3967f169c0eaSGlenn Lagasse 	FILE **menu_fp,
3968f169c0eaSGlenn Lagasse 	char *mode)
3969f169c0eaSGlenn Lagasse {
3970f169c0eaSGlenn Lagasse 	be_node_list_t	*be_nodes = NULL;
3971f169c0eaSGlenn Lagasse 	char *menu_path = NULL;
3972f169c0eaSGlenn Lagasse 	char *be_rpool = NULL;
3973f169c0eaSGlenn Lagasse 	char *be_name = NULL;
39746b1d07a4SAlexander Eremin 	char *console = NULL;
3975f169c0eaSGlenn Lagasse 	errno = 0;
3976f169c0eaSGlenn Lagasse 
3977f169c0eaSGlenn Lagasse 	if (menu_file == NULL || menu_fp == NULL || mode == NULL)
3978f169c0eaSGlenn Lagasse 		return (BE_ERR_INVAL);
3979f169c0eaSGlenn Lagasse 
3980f169c0eaSGlenn Lagasse 	menu_path = strdup(menu_file);
3981f169c0eaSGlenn Lagasse 	if (menu_path == NULL)
3982f169c0eaSGlenn Lagasse 		return (BE_ERR_NOMEM);
3983f169c0eaSGlenn Lagasse 
3984f169c0eaSGlenn Lagasse 	(void) dirname(menu_path);
3985f169c0eaSGlenn Lagasse 	if (*menu_path == '.') {
3986f169c0eaSGlenn Lagasse 		free(menu_path);
3987f169c0eaSGlenn Lagasse 		return (BE_ERR_BAD_MENU_PATH);
3988f169c0eaSGlenn Lagasse 	}
3989f169c0eaSGlenn Lagasse 	if (mkdirp(menu_path,
3990f169c0eaSGlenn Lagasse 	    S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1 &&
3991f169c0eaSGlenn Lagasse 	    errno != EEXIST) {
3992f169c0eaSGlenn Lagasse 		be_print_err(gettext("be_create_menu: Failed to create the %s "
3993f169c0eaSGlenn Lagasse 		    "directory: %s\n"), menu_path, strerror(errno));
3994ce829a51SJohn Levon 		free(menu_path);
3995f169c0eaSGlenn Lagasse 		return (errno_to_be_err(errno));
3996f169c0eaSGlenn Lagasse 	}
3997f169c0eaSGlenn Lagasse 	free(menu_path);
3998f169c0eaSGlenn Lagasse 
3999f169c0eaSGlenn Lagasse 	/*
4000f169c0eaSGlenn Lagasse 	 * Check to see if this system supports grub
4001f169c0eaSGlenn Lagasse 	 */
4002f169c0eaSGlenn Lagasse 	if (be_has_grub()) {
4003f169c0eaSGlenn Lagasse 		/*
4004f169c0eaSGlenn Lagasse 		 * The grub menu is missing so we need to create it
4005f169c0eaSGlenn Lagasse 		 * and fill in the first few lines.
4006f169c0eaSGlenn Lagasse 		 */
40076b1d07a4SAlexander Eremin 		FILE *temp_fp = fopen(menu_file, "a+");
40086b1d07a4SAlexander Eremin 		if (temp_fp == NULL) {
40096b1d07a4SAlexander Eremin 			*menu_fp = NULL;
40106b1d07a4SAlexander Eremin 			return (errno_to_be_err(errno));
4011f169c0eaSGlenn Lagasse 		}
40126b1d07a4SAlexander Eremin 
40136b1d07a4SAlexander Eremin 		if ((console = be_get_console_prop()) != NULL) {
40146b1d07a4SAlexander Eremin 
40156b1d07a4SAlexander Eremin 			/*
40166b1d07a4SAlexander Eremin 			 * If console is redirected to serial line,
40176b1d07a4SAlexander Eremin 			 * GRUB splash screen will not be enabled.
40186b1d07a4SAlexander Eremin 			 */
40196b1d07a4SAlexander Eremin 			if (strncmp(console, "text", strlen("text")) == 0 ||
40206b1d07a4SAlexander Eremin 			    strncmp(console, "graphics",
40216b1d07a4SAlexander Eremin 			    strlen("graphics")) == 0) {
40226b1d07a4SAlexander Eremin 
40236b1d07a4SAlexander Eremin 				(void) fprintf(temp_fp, "%s\n", BE_GRUB_SPLASH);
40246b1d07a4SAlexander Eremin 				(void) fprintf(temp_fp, "%s\n",
40256b1d07a4SAlexander Eremin 				    BE_GRUB_FOREGROUND);
40266b1d07a4SAlexander Eremin 				(void) fprintf(temp_fp, "%s\n",
40276b1d07a4SAlexander Eremin 				    BE_GRUB_BACKGROUND);
40286b1d07a4SAlexander Eremin 				(void) fprintf(temp_fp, "%s\n",
40296b1d07a4SAlexander Eremin 				    BE_GRUB_DEFAULT);
40306b1d07a4SAlexander Eremin 			} else {
40316b1d07a4SAlexander Eremin 				be_print_err(gettext("be_create_menu: "
40326b1d07a4SAlexander Eremin 				    "console on serial line, "
40336b1d07a4SAlexander Eremin 				    "GRUB splash image will be disabled\n"));
40346b1d07a4SAlexander Eremin 			}
40356b1d07a4SAlexander Eremin 		}
40366b1d07a4SAlexander Eremin 
40376b1d07a4SAlexander Eremin 		(void) fprintf(temp_fp,	"timeout 30\n");
40386b1d07a4SAlexander Eremin 		(void) fclose(temp_fp);
40396b1d07a4SAlexander Eremin 
4040f169c0eaSGlenn Lagasse 	} else {
4041f169c0eaSGlenn Lagasse 		/*
4042f169c0eaSGlenn Lagasse 		 * The menu file doesn't exist so we need to create a
4043f169c0eaSGlenn Lagasse 		 * blank file.
4044f169c0eaSGlenn Lagasse 		 */
4045f169c0eaSGlenn Lagasse 		FILE *temp_fp = fopen(menu_file, "w+");
4046f169c0eaSGlenn Lagasse 		if (temp_fp == NULL) {
4047f169c0eaSGlenn Lagasse 			*menu_fp = NULL;
4048f169c0eaSGlenn Lagasse 			return (errno_to_be_err(errno));
4049f169c0eaSGlenn Lagasse 		}
4050f169c0eaSGlenn Lagasse 		(void) fclose(temp_fp);
4051f169c0eaSGlenn Lagasse 	}
4052f169c0eaSGlenn Lagasse 
4053f169c0eaSGlenn Lagasse 	/*
4054f169c0eaSGlenn Lagasse 	 * Now we need to add all the BE's back into the the file.
4055f169c0eaSGlenn Lagasse 	 */
4056a897f28bSAndy Fiddaman 	if (_be_list(NULL, &be_nodes, BE_LIST_DEFAULT) == BE_SUCCESS) {
4057f169c0eaSGlenn Lagasse 		while (be_nodes != NULL) {
4058f169c0eaSGlenn Lagasse 			if (strcmp(pool, be_nodes->be_rpool) == 0) {
4059f169c0eaSGlenn Lagasse 				(void) be_append_menu(be_nodes->be_node_name,
4060f169c0eaSGlenn Lagasse 				    be_nodes->be_rpool, NULL, NULL, NULL);
4061f169c0eaSGlenn Lagasse 			}
4062f169c0eaSGlenn Lagasse 			if (be_nodes->be_active_on_boot) {
4063f169c0eaSGlenn Lagasse 				be_rpool = strdup(be_nodes->be_rpool);
4064f169c0eaSGlenn Lagasse 				be_name = strdup(be_nodes->be_node_name);
4065f169c0eaSGlenn Lagasse 			}
4066f169c0eaSGlenn Lagasse 
4067f169c0eaSGlenn Lagasse 			be_nodes = be_nodes->be_next_node;
4068f169c0eaSGlenn Lagasse 		}
4069f169c0eaSGlenn Lagasse 	}
4070f169c0eaSGlenn Lagasse 	be_free_list(be_nodes);
4071f169c0eaSGlenn Lagasse 
4072f169c0eaSGlenn Lagasse 	/*
4073f169c0eaSGlenn Lagasse 	 * Check to see if this system supports grub
4074f169c0eaSGlenn Lagasse 	 */
4075f169c0eaSGlenn Lagasse 	if (be_has_grub()) {
4076f169c0eaSGlenn Lagasse 		int err = be_change_grub_default(be_name, be_rpool);
4077f169c0eaSGlenn Lagasse 		if (err != BE_SUCCESS)
4078f169c0eaSGlenn Lagasse 			return (err);
4079f169c0eaSGlenn Lagasse 	}
4080f169c0eaSGlenn Lagasse 	*menu_fp = fopen(menu_file, mode);
4081f169c0eaSGlenn Lagasse 	if (*menu_fp == NULL)
4082f169c0eaSGlenn Lagasse 		return (errno_to_be_err(errno));
4083f169c0eaSGlenn Lagasse 
4084f169c0eaSGlenn Lagasse 	return (BE_SUCCESS);
4085f169c0eaSGlenn Lagasse }
4086f169c0eaSGlenn Lagasse 
4087f169c0eaSGlenn Lagasse /*
4088f169c0eaSGlenn Lagasse  * Function:	be_open_menu
4089f169c0eaSGlenn Lagasse  * Description:
4090f169c0eaSGlenn Lagasse  *		This function is used it open the menu.lst file. If this
4091f169c0eaSGlenn Lagasse  *              file does not exist be_create_menu is called to create it
4092f169c0eaSGlenn Lagasse  *              and the open file pointer is returned. If the file does
4093f169c0eaSGlenn Lagasse  *              exist it is simply opened using the mode passed in.
4094f169c0eaSGlenn Lagasse  * Parameters:
4095f169c0eaSGlenn Lagasse  *		pool - The name of the pool the menu.lst file is on
4096f169c0eaSGlenn Lagasse  *		menu_file - The name of the file we're opening.
4097f169c0eaSGlenn Lagasse  *		menu_fp - A pointer to the file pointer of the file we're
4098f169c0eaSGlenn Lagasse  *			  opening. This is also used to pass back the file
4099f169c0eaSGlenn Lagasse  *			  pointer.
4100f169c0eaSGlenn Lagasse  *		mode - the original mode to be used for opening the menu.lst
4101f169c0eaSGlenn Lagasse  *                     file.
4102f169c0eaSGlenn Lagasse  *              create_menu - If this is true and the menu.lst file does not
4103f169c0eaSGlenn Lagasse  *                            exist we will attempt to re-create it. However
4104f169c0eaSGlenn Lagasse  *                            if it's false the error returned from the fopen
4105f169c0eaSGlenn Lagasse  *                            will be returned.
4106f169c0eaSGlenn Lagasse  * Returns:
4107f169c0eaSGlenn Lagasse  *		BE_SUCCESS - Success
4108f169c0eaSGlenn Lagasse  *		be_errno_t - Failure
4109f169c0eaSGlenn Lagasse  * Scope:
4110f169c0eaSGlenn Lagasse  *		Private
4111f169c0eaSGlenn Lagasse  */
4112f169c0eaSGlenn Lagasse static int
be_open_menu(char * pool,char * menu_file,FILE ** menu_fp,char * mode,boolean_t create_menu)4113f169c0eaSGlenn Lagasse be_open_menu(
4114f169c0eaSGlenn Lagasse 	char *pool,
4115f169c0eaSGlenn Lagasse 	char *menu_file,
4116f169c0eaSGlenn Lagasse 	FILE **menu_fp,
4117f169c0eaSGlenn Lagasse 	char *mode,
4118f169c0eaSGlenn Lagasse 	boolean_t create_menu)
4119f169c0eaSGlenn Lagasse {
4120f169c0eaSGlenn Lagasse 	int	err = 0;
4121f169c0eaSGlenn Lagasse 	boolean_t	set_print = B_FALSE;
4122f169c0eaSGlenn Lagasse 
4123f169c0eaSGlenn Lagasse 	*menu_fp = fopen(menu_file, mode);
4124f169c0eaSGlenn Lagasse 	err = errno;
4125f169c0eaSGlenn Lagasse 	if (*menu_fp == NULL) {
4126f169c0eaSGlenn Lagasse 		if (err == ENOENT && create_menu) {
4127f169c0eaSGlenn Lagasse 			be_print_err(gettext("be_open_menu: menu.lst "
4128f169c0eaSGlenn Lagasse 			    "file %s does not exist,\n"), menu_file);
4129f169c0eaSGlenn Lagasse 			if (!do_print) {
4130f169c0eaSGlenn Lagasse 				set_print = B_TRUE;
4131f169c0eaSGlenn Lagasse 				do_print = B_TRUE;
4132f169c0eaSGlenn Lagasse 			}
4133f169c0eaSGlenn Lagasse 			be_print_err(gettext("WARNING: menu.lst "
4134f169c0eaSGlenn Lagasse 			    "file %s does not exist,\n         generating "
4135f169c0eaSGlenn Lagasse 			    "a new menu.lst file\n"), menu_file);
4136f169c0eaSGlenn Lagasse 			if (set_print)
4137f169c0eaSGlenn Lagasse 				do_print = B_FALSE;
4138f169c0eaSGlenn Lagasse 			err = 0;
4139de1ab35cSAlexander Eremin 			if ((err = be_create_menu(pool, menu_file,
4140f169c0eaSGlenn Lagasse 			    menu_fp, mode)) == ENOENT)
4141f169c0eaSGlenn Lagasse 				return (BE_ERR_NO_MENU);
4142f169c0eaSGlenn Lagasse 			else if (err != BE_SUCCESS)
4143f169c0eaSGlenn Lagasse 				return (err);
4144f169c0eaSGlenn Lagasse 			else if (*menu_fp == NULL)
4145f169c0eaSGlenn Lagasse 				return (BE_ERR_NO_MENU);
4146f169c0eaSGlenn Lagasse 		} else {
4147f169c0eaSGlenn Lagasse 			be_print_err(gettext("be_open_menu: failed "
4148f169c0eaSGlenn Lagasse 			    "to open menu.lst file %s\n"), menu_file);
4149f169c0eaSGlenn Lagasse 			if (err == ENOENT)
4150f169c0eaSGlenn Lagasse 				return (BE_ERR_NO_MENU);
4151f169c0eaSGlenn Lagasse 			else
4152f169c0eaSGlenn Lagasse 				return (errno_to_be_err(err));
4153f169c0eaSGlenn Lagasse 		}
4154f169c0eaSGlenn Lagasse 	}
4155f169c0eaSGlenn Lagasse 	return (BE_SUCCESS);
4156f169c0eaSGlenn Lagasse }
4157