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