xref: /illumos-gate/usr/src/cmd/zoneadm/zfs.c (revision 842727c2f41f01b380de4f5e787d905702870f23)
10b5de56dSgjelinek /*
20b5de56dSgjelinek  * CDDL HEADER START
30b5de56dSgjelinek  *
40b5de56dSgjelinek  * The contents of this file are subject to the terms of the
50b5de56dSgjelinek  * Common Development and Distribution License (the "License").
60b5de56dSgjelinek  * You may not use this file except in compliance with the License.
70b5de56dSgjelinek  *
80b5de56dSgjelinek  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90b5de56dSgjelinek  * or http://www.opensolaris.org/os/licensing.
100b5de56dSgjelinek  * See the License for the specific language governing permissions
110b5de56dSgjelinek  * and limitations under the License.
120b5de56dSgjelinek  *
130b5de56dSgjelinek  * When distributing Covered Code, include this CDDL HEADER in each
140b5de56dSgjelinek  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150b5de56dSgjelinek  * If applicable, add the following below this CDDL HEADER, with the
160b5de56dSgjelinek  * fields enclosed by brackets "[]" replaced with your own identifying
170b5de56dSgjelinek  * information: Portions Copyright [yyyy] [name of copyright owner]
180b5de56dSgjelinek  *
190b5de56dSgjelinek  * CDDL HEADER END
200b5de56dSgjelinek  */
210b5de56dSgjelinek 
220b5de56dSgjelinek /*
23d1f855d7S  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240b5de56dSgjelinek  * Use is subject to license terms.
250b5de56dSgjelinek  */
260b5de56dSgjelinek 
270b5de56dSgjelinek /*
280b5de56dSgjelinek  * This file contains the functions used to support the ZFS integration
290b5de56dSgjelinek  * with zones.  This includes validation (e.g. zonecfg dataset), cloning,
300b5de56dSgjelinek  * file system creation and destruction.
310b5de56dSgjelinek  */
320b5de56dSgjelinek 
330b5de56dSgjelinek #include <stdio.h>
340b5de56dSgjelinek #include <errno.h>
350b5de56dSgjelinek #include <unistd.h>
360b5de56dSgjelinek #include <string.h>
370b5de56dSgjelinek #include <locale.h>
380b5de56dSgjelinek #include <libintl.h>
390b5de56dSgjelinek #include <sys/stat.h>
400b5de56dSgjelinek #include <sys/statvfs.h>
410b5de56dSgjelinek #include <libgen.h>
420b5de56dSgjelinek #include <libzonecfg.h>
430b5de56dSgjelinek #include <sys/mnttab.h>
440b5de56dSgjelinek #include <libzfs.h>
4511506c41Sgjelinek #include <sys/mntent.h>
46286822ddS #include <values.h>
470b5de56dSgjelinek 
480b5de56dSgjelinek #include "zoneadm.h"
490b5de56dSgjelinek 
5099653d4eSeschrock libzfs_handle_t *g_zfs;
510b5de56dSgjelinek 
520b5de56dSgjelinek typedef struct zfs_mount_data {
530b5de56dSgjelinek 	char		*match_name;
540b5de56dSgjelinek 	zfs_handle_t	*match_handle;
550b5de56dSgjelinek } zfs_mount_data_t;
560b5de56dSgjelinek 
570b5de56dSgjelinek typedef struct zfs_snapshot_data {
58286822ddS 	char	*match_name;	/* zonename@SUNWzone */
59286822ddS 	int	len;		/* strlen of match_name */
60286822ddS 	int	max;		/* highest digit appended to snap name */
61286822ddS 	int	num;		/* number of snapshots to rename */
62286822ddS 	int	cntr;		/* counter for renaming snapshots */
630b5de56dSgjelinek } zfs_snapshot_data_t;
640b5de56dSgjelinek 
65286822ddS typedef struct clone_data {
66286822ddS 	zfs_handle_t	*clone_zhp;	/* clone dataset to promote */
67286822ddS 	time_t		origin_creation; /* snapshot creation time of clone */
68286822ddS 	const char	*snapshot;	/* snapshot of dataset being demoted */
69286822ddS } clone_data_t;
70286822ddS 
710b5de56dSgjelinek /*
720b5de56dSgjelinek  * A ZFS file system iterator call-back function which is used to validate
730b5de56dSgjelinek  * datasets imported into the zone.
740b5de56dSgjelinek  */
750b5de56dSgjelinek /* ARGSUSED */
760b5de56dSgjelinek static int
770b5de56dSgjelinek check_zvol(zfs_handle_t *zhp, void *unused)
780b5de56dSgjelinek {
790b5de56dSgjelinek 	int ret;
800b5de56dSgjelinek 
810b5de56dSgjelinek 	if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
820b5de56dSgjelinek 		/*
830b5de56dSgjelinek 		 * TRANSLATION_NOTE
840b5de56dSgjelinek 		 * zfs and dataset are literals that should not be translated.
850b5de56dSgjelinek 		 */
860b5de56dSgjelinek 		(void) fprintf(stderr, gettext("cannot verify zfs dataset %s: "
870b5de56dSgjelinek 		    "volumes cannot be specified as a zone dataset resource\n"),
880b5de56dSgjelinek 		    zfs_get_name(zhp));
890b5de56dSgjelinek 		ret = -1;
900b5de56dSgjelinek 	} else {
910b5de56dSgjelinek 		ret = zfs_iter_children(zhp, check_zvol, NULL);
920b5de56dSgjelinek 	}
930b5de56dSgjelinek 
940b5de56dSgjelinek 	zfs_close(zhp);
950b5de56dSgjelinek 
960b5de56dSgjelinek 	return (ret);
970b5de56dSgjelinek }
980b5de56dSgjelinek 
990b5de56dSgjelinek /*
1000b5de56dSgjelinek  * A ZFS file system iterator call-back function which returns the
1010b5de56dSgjelinek  * zfs_handle_t for a ZFS file system on the specified mount point.
1020b5de56dSgjelinek  */
1030b5de56dSgjelinek static int
1040b5de56dSgjelinek match_mountpoint(zfs_handle_t *zhp, void *data)
1050b5de56dSgjelinek {
1060b5de56dSgjelinek 	int			res;
1070b5de56dSgjelinek 	zfs_mount_data_t	*cbp;
1080b5de56dSgjelinek 	char			mp[ZFS_MAXPROPLEN];
1090b5de56dSgjelinek 
1100b5de56dSgjelinek 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
1110b5de56dSgjelinek 		zfs_close(zhp);
1120b5de56dSgjelinek 		return (0);
1130b5de56dSgjelinek 	}
1140b5de56dSgjelinek 
11511506c41Sgjelinek 	/* First check if the dataset is mounted. */
11611506c41Sgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTED, mp, sizeof (mp), NULL, NULL,
11711506c41Sgjelinek 	    0, B_FALSE) != 0 || strcmp(mp, "no") == 0) {
11811506c41Sgjelinek 		zfs_close(zhp);
11911506c41Sgjelinek 		return (0);
12011506c41Sgjelinek 	}
12111506c41Sgjelinek 
12211506c41Sgjelinek 	/* Now check mount point. */
1230b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mp, sizeof (mp), NULL, NULL,
12411506c41Sgjelinek 	    0, B_FALSE) != 0) {
12511506c41Sgjelinek 		zfs_close(zhp);
12611506c41Sgjelinek 		return (0);
12711506c41Sgjelinek 	}
12811506c41Sgjelinek 
12911506c41Sgjelinek 	cbp = (zfs_mount_data_t *)data;
13011506c41Sgjelinek 
13111506c41Sgjelinek 	if (strcmp(mp, "legacy") == 0) {
13211506c41Sgjelinek 		/* If legacy, must look in mnttab for mountpoint. */
13311506c41Sgjelinek 		FILE		*fp;
13411506c41Sgjelinek 		struct mnttab	entry;
13511506c41Sgjelinek 		const char	*nm;
13611506c41Sgjelinek 
13711506c41Sgjelinek 		nm = zfs_get_name(zhp);
13811506c41Sgjelinek 		if ((fp = fopen(MNTTAB, "r")) == NULL) {
13911506c41Sgjelinek 			zfs_close(zhp);
14011506c41Sgjelinek 			return (0);
14111506c41Sgjelinek 		}
14211506c41Sgjelinek 
14311506c41Sgjelinek 		while (getmntent(fp, &entry) == 0) {
14411506c41Sgjelinek 			if (strcmp(nm, entry.mnt_special) == 0) {
14511506c41Sgjelinek 				if (strcmp(entry.mnt_mountp, cbp->match_name)
14611506c41Sgjelinek 				    == 0) {
14711506c41Sgjelinek 					(void) fclose(fp);
14811506c41Sgjelinek 					cbp->match_handle = zhp;
14911506c41Sgjelinek 					return (1);
15011506c41Sgjelinek 				}
15111506c41Sgjelinek 				break;
15211506c41Sgjelinek 			}
15311506c41Sgjelinek 		}
15411506c41Sgjelinek 		(void) fclose(fp);
15511506c41Sgjelinek 
15611506c41Sgjelinek 	} else if (strcmp(mp, cbp->match_name) == 0) {
1570b5de56dSgjelinek 		cbp->match_handle = zhp;
1580b5de56dSgjelinek 		return (1);
1590b5de56dSgjelinek 	}
1600b5de56dSgjelinek 
16111506c41Sgjelinek 	/* Iterate over any nested datasets. */
1620b5de56dSgjelinek 	res = zfs_iter_filesystems(zhp, match_mountpoint, data);
1630b5de56dSgjelinek 	zfs_close(zhp);
1640b5de56dSgjelinek 	return (res);
1650b5de56dSgjelinek }
1660b5de56dSgjelinek 
1670b5de56dSgjelinek /*
1680b5de56dSgjelinek  * Get ZFS handle for the specified mount point.
1690b5de56dSgjelinek  */
1700b5de56dSgjelinek static zfs_handle_t *
1710b5de56dSgjelinek mount2zhandle(char *mountpoint)
1720b5de56dSgjelinek {
1730b5de56dSgjelinek 	zfs_mount_data_t	cb;
1740b5de56dSgjelinek 
1750b5de56dSgjelinek 	cb.match_name = mountpoint;
1760b5de56dSgjelinek 	cb.match_handle = NULL;
17799653d4eSeschrock 	(void) zfs_iter_root(g_zfs, match_mountpoint, &cb);
1780b5de56dSgjelinek 	return (cb.match_handle);
1790b5de56dSgjelinek }
1800b5de56dSgjelinek 
1810b5de56dSgjelinek /*
1820b5de56dSgjelinek  * Check if there is already a file system (zfs or any other type) mounted on
1830b5de56dSgjelinek  * path.
1840b5de56dSgjelinek  */
1850b5de56dSgjelinek static boolean_t
1860b5de56dSgjelinek is_mountpnt(char *path)
1870b5de56dSgjelinek {
1880b5de56dSgjelinek 	FILE		*fp;
1890b5de56dSgjelinek 	struct mnttab	entry;
1900b5de56dSgjelinek 
19111506c41Sgjelinek 	if ((fp = fopen(MNTTAB, "r")) == NULL)
1920b5de56dSgjelinek 		return (B_FALSE);
1930b5de56dSgjelinek 
1940b5de56dSgjelinek 	while (getmntent(fp, &entry) == 0) {
1950b5de56dSgjelinek 		if (strcmp(path, entry.mnt_mountp) == 0) {
1960b5de56dSgjelinek 			(void) fclose(fp);
1970b5de56dSgjelinek 			return (B_TRUE);
1980b5de56dSgjelinek 		}
1990b5de56dSgjelinek 	}
2000b5de56dSgjelinek 
2010b5de56dSgjelinek 	(void) fclose(fp);
2020b5de56dSgjelinek 	return (B_FALSE);
2030b5de56dSgjelinek }
2040b5de56dSgjelinek 
2050b5de56dSgjelinek /*
206ff17c8bfSgjelinek  * Run the brand's pre-snapshot hook before we take a ZFS snapshot of the zone.
2070b5de56dSgjelinek  */
2080b5de56dSgjelinek static int
209ff17c8bfSgjelinek pre_snapshot(char *presnapbuf)
2100b5de56dSgjelinek {
211ff17c8bfSgjelinek 	int status;
2120b5de56dSgjelinek 
213ff17c8bfSgjelinek 	/* No brand-specific handler */
214ff17c8bfSgjelinek 	if (presnapbuf[0] == '\0')
215ff17c8bfSgjelinek 		return (Z_OK);
2160b5de56dSgjelinek 
217ff17c8bfSgjelinek 	/* Run the hook */
218c75cc341S 	status = do_subproc(presnapbuf);
219ff17c8bfSgjelinek 	if ((status = subproc_status(gettext("brand-specific presnapshot"),
220ff17c8bfSgjelinek 	    status, B_FALSE)) != ZONE_SUBPROC_OK)
2210b5de56dSgjelinek 		return (Z_ERR);
2220b5de56dSgjelinek 
2230b5de56dSgjelinek 	return (Z_OK);
2240b5de56dSgjelinek }
2250b5de56dSgjelinek 
2260b5de56dSgjelinek /*
227ff17c8bfSgjelinek  * Run the brand's post-snapshot hook after we take a ZFS snapshot of the zone.
2280b5de56dSgjelinek  */
2290b5de56dSgjelinek static int
230ff17c8bfSgjelinek post_snapshot(char *postsnapbuf)
2310b5de56dSgjelinek {
232ff17c8bfSgjelinek 	int status;
2330b5de56dSgjelinek 
234ff17c8bfSgjelinek 	/* No brand-specific handler */
235ff17c8bfSgjelinek 	if (postsnapbuf[0] == '\0')
236ff17c8bfSgjelinek 		return (Z_OK);
2370b5de56dSgjelinek 
238ff17c8bfSgjelinek 	/* Run the hook */
239c75cc341S 	status = do_subproc(postsnapbuf);
240ff17c8bfSgjelinek 	if ((status = subproc_status(gettext("brand-specific postsnapshot"),
241ff17c8bfSgjelinek 	    status, B_FALSE)) != ZONE_SUBPROC_OK)
2420b5de56dSgjelinek 		return (Z_ERR);
2430b5de56dSgjelinek 
2440b5de56dSgjelinek 	return (Z_OK);
2450b5de56dSgjelinek }
2460b5de56dSgjelinek 
2470b5de56dSgjelinek /*
2480b5de56dSgjelinek  * This is a ZFS snapshot iterator call-back function which returns the
2490b5de56dSgjelinek  * highest number of SUNWzone snapshots that have been taken.
2500b5de56dSgjelinek  */
2510b5de56dSgjelinek static int
2520b5de56dSgjelinek get_snap_max(zfs_handle_t *zhp, void *data)
2530b5de56dSgjelinek {
2540b5de56dSgjelinek 	int			res;
2550b5de56dSgjelinek 	zfs_snapshot_data_t	*cbp;
2560b5de56dSgjelinek 
2570b5de56dSgjelinek 	if (zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) {
2580b5de56dSgjelinek 		zfs_close(zhp);
2590b5de56dSgjelinek 		return (0);
2600b5de56dSgjelinek 	}
2610b5de56dSgjelinek 
2620b5de56dSgjelinek 	cbp = (zfs_snapshot_data_t *)data;
2630b5de56dSgjelinek 
2640b5de56dSgjelinek 	if (strncmp(zfs_get_name(zhp), cbp->match_name, cbp->len) == 0) {
2650b5de56dSgjelinek 		char	*nump;
2660b5de56dSgjelinek 		int	num;
2670b5de56dSgjelinek 
268286822ddS 		cbp->num++;
2690b5de56dSgjelinek 		nump = (char *)(zfs_get_name(zhp) + cbp->len);
2700b5de56dSgjelinek 		num = atoi(nump);
2710b5de56dSgjelinek 		if (num > cbp->max)
2720b5de56dSgjelinek 			cbp->max = num;
2730b5de56dSgjelinek 	}
2740b5de56dSgjelinek 
2750b5de56dSgjelinek 	res = zfs_iter_snapshots(zhp, get_snap_max, data);
2760b5de56dSgjelinek 	zfs_close(zhp);
2770b5de56dSgjelinek 	return (res);
2780b5de56dSgjelinek }
2790b5de56dSgjelinek 
2800b5de56dSgjelinek /*
2810b5de56dSgjelinek  * Take a ZFS snapshot to be used for cloning the zone.
2820b5de56dSgjelinek  */
2830b5de56dSgjelinek static int
284ff17c8bfSgjelinek take_snapshot(zfs_handle_t *zhp, char *snapshot_name, int snap_size,
285ff17c8bfSgjelinek     char *presnapbuf, char *postsnapbuf)
2860b5de56dSgjelinek {
2870b5de56dSgjelinek 	int			res;
2880b5de56dSgjelinek 	char			template[ZFS_MAXNAMELEN];
2890b5de56dSgjelinek 	zfs_snapshot_data_t	cb;
2900b5de56dSgjelinek 
2910b5de56dSgjelinek 	/*
2920b5de56dSgjelinek 	 * First we need to figure out the next available name for the
2930b5de56dSgjelinek 	 * zone snapshot.  Look through the list of zones snapshots for
2940b5de56dSgjelinek 	 * this file system to determine the maximum snapshot name.
2950b5de56dSgjelinek 	 */
2960b5de56dSgjelinek 	if (snprintf(template, sizeof (template), "%s@SUNWzone",
2970b5de56dSgjelinek 	    zfs_get_name(zhp)) >=  sizeof (template))
2980b5de56dSgjelinek 		return (Z_ERR);
2990b5de56dSgjelinek 
3000b5de56dSgjelinek 	cb.match_name = template;
3010b5de56dSgjelinek 	cb.len = strlen(template);
3020b5de56dSgjelinek 	cb.max = 0;
3030b5de56dSgjelinek 
3040b5de56dSgjelinek 	if (zfs_iter_snapshots(zhp, get_snap_max, &cb) != 0)
3050b5de56dSgjelinek 		return (Z_ERR);
3060b5de56dSgjelinek 
3070b5de56dSgjelinek 	cb.max++;
3080b5de56dSgjelinek 
3090b5de56dSgjelinek 	if (snprintf(snapshot_name, snap_size, "%s@SUNWzone%d",
3100b5de56dSgjelinek 	    zfs_get_name(zhp), cb.max) >= snap_size)
3110b5de56dSgjelinek 		return (Z_ERR);
3120b5de56dSgjelinek 
313ff17c8bfSgjelinek 	if (pre_snapshot(presnapbuf) != Z_OK)
3140b5de56dSgjelinek 		return (Z_ERR);
315bb0ade09Sahrens 	res = zfs_snapshot(g_zfs, snapshot_name, B_FALSE, NULL);
316ff17c8bfSgjelinek 	if (post_snapshot(postsnapbuf) != Z_OK)
3170b5de56dSgjelinek 		return (Z_ERR);
3180b5de56dSgjelinek 
3190b5de56dSgjelinek 	if (res != 0)
3200b5de56dSgjelinek 		return (Z_ERR);
3210b5de56dSgjelinek 	return (Z_OK);
3220b5de56dSgjelinek }
3230b5de56dSgjelinek 
3240b5de56dSgjelinek /*
3250b5de56dSgjelinek  * We are using an explicit snapshot from some earlier point in time so
326ff17c8bfSgjelinek  * we need to validate it.  Run the brand specific hook.
3270b5de56dSgjelinek  */
3280b5de56dSgjelinek static int
329ff17c8bfSgjelinek validate_snapshot(char *snapshot_name, char *snap_path, char *validsnapbuf)
3300b5de56dSgjelinek {
331ff17c8bfSgjelinek 	int status;
332ff17c8bfSgjelinek 	char cmdbuf[MAXPATHLEN];
3330b5de56dSgjelinek 
334ff17c8bfSgjelinek 	/* No brand-specific handler */
335ff17c8bfSgjelinek 	if (validsnapbuf[0] == '\0')
336ff17c8bfSgjelinek 		return (Z_OK);
3370b5de56dSgjelinek 
338ff17c8bfSgjelinek 	/* pass args - snapshot_name & snap_path */
339ff17c8bfSgjelinek 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %s %s", validsnapbuf,
340ff17c8bfSgjelinek 	    snapshot_name, snap_path) >= sizeof (cmdbuf)) {
341ff17c8bfSgjelinek 		zerror("Command line too long");
3420b5de56dSgjelinek 		return (Z_ERR);
3430b5de56dSgjelinek 	}
3440b5de56dSgjelinek 
345ff17c8bfSgjelinek 	/* Run the hook */
346c75cc341S 	status = do_subproc(cmdbuf);
347ff17c8bfSgjelinek 	if ((status = subproc_status(gettext("brand-specific validatesnapshot"),
348ff17c8bfSgjelinek 	    status, B_FALSE)) != ZONE_SUBPROC_OK)
349ff17c8bfSgjelinek 		return (Z_ERR);
3500b5de56dSgjelinek 
351ff17c8bfSgjelinek 	return (Z_OK);
3520b5de56dSgjelinek }
3530b5de56dSgjelinek 
3540b5de56dSgjelinek /*
3550b5de56dSgjelinek  * Remove the sw inventory file from inside this zonepath that we picked up out
3560b5de56dSgjelinek  * of the snapshot.
3570b5de56dSgjelinek  */
3580b5de56dSgjelinek static int
3590b5de56dSgjelinek clean_out_clone()
3600b5de56dSgjelinek {
3610b5de56dSgjelinek 	int err;
3620b5de56dSgjelinek 	zone_dochandle_t handle;
3630b5de56dSgjelinek 
3640b5de56dSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
3650b5de56dSgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3660b5de56dSgjelinek 		return (Z_ERR);
3670b5de56dSgjelinek 	}
3680b5de56dSgjelinek 
3690b5de56dSgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
3700b5de56dSgjelinek 		errno = err;
3710b5de56dSgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3720b5de56dSgjelinek 		zonecfg_fini_handle(handle);
3730b5de56dSgjelinek 		return (Z_ERR);
3740b5de56dSgjelinek 	}
3750b5de56dSgjelinek 
3760b5de56dSgjelinek 	zonecfg_rm_detached(handle, B_FALSE);
3770b5de56dSgjelinek 	zonecfg_fini_handle(handle);
3780b5de56dSgjelinek 
3790b5de56dSgjelinek 	return (Z_OK);
3800b5de56dSgjelinek }
3810b5de56dSgjelinek 
3820b5de56dSgjelinek /*
3830b5de56dSgjelinek  * Make a ZFS clone on zonepath from snapshot_name.
3840b5de56dSgjelinek  */
3850b5de56dSgjelinek static int
3860b5de56dSgjelinek clone_snap(char *snapshot_name, char *zonepath)
3870b5de56dSgjelinek {
3880b5de56dSgjelinek 	int		res = Z_OK;
3890b5de56dSgjelinek 	int		err;
3900b5de56dSgjelinek 	zfs_handle_t	*zhp;
3910b5de56dSgjelinek 	zfs_handle_t	*clone;
392e9dbad6fSeschrock 	nvlist_t	*props = NULL;
3930b5de56dSgjelinek 
39499653d4eSeschrock 	if ((zhp = zfs_open(g_zfs, snapshot_name, ZFS_TYPE_SNAPSHOT)) == NULL)
3950b5de56dSgjelinek 		return (Z_NO_ENTRY);
3960b5de56dSgjelinek 
3970b5de56dSgjelinek 	(void) printf(gettext("Cloning snapshot %s\n"), snapshot_name);
3980b5de56dSgjelinek 
399e9dbad6fSeschrock 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0 ||
4005f8e1617Snn 	    nvlist_add_string(props, zfs_prop_to_name(ZFS_PROP_SHARENFS),
4015f8e1617Snn 	    "off") != 0) {
4025f8e1617Snn 		if (props != NULL)
4035f8e1617Snn 			nvlist_free(props);
404e9dbad6fSeschrock 		(void) fprintf(stderr, gettext("could not create ZFS clone "
405e9dbad6fSeschrock 		    "%s: out of memory\n"), zonepath);
406e9dbad6fSeschrock 		return (Z_ERR);
407e9dbad6fSeschrock 	}
408e9dbad6fSeschrock 
409e9dbad6fSeschrock 	err = zfs_clone(zhp, zonepath, props);
4100b5de56dSgjelinek 	zfs_close(zhp);
411e9dbad6fSeschrock 
412e9dbad6fSeschrock 	nvlist_free(props);
413e9dbad6fSeschrock 
4140b5de56dSgjelinek 	if (err != 0)
4150b5de56dSgjelinek 		return (Z_ERR);
4160b5de56dSgjelinek 
4170b5de56dSgjelinek 	/* create the mountpoint if necessary */
418990b4856Slling 	if ((clone = zfs_open(g_zfs, zonepath, ZFS_TYPE_DATASET)) == NULL)
4190b5de56dSgjelinek 		return (Z_ERR);
4200b5de56dSgjelinek 
4210b5de56dSgjelinek 	/*
4220b5de56dSgjelinek 	 * The clone has been created so we need to print a diagnostic
4230b5de56dSgjelinek 	 * message if one of the following steps fails for some reason.
4240b5de56dSgjelinek 	 */
4250b5de56dSgjelinek 	if (zfs_mount(clone, NULL, 0) != 0) {
4260b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not mount ZFS clone "
4270b5de56dSgjelinek 		    "%s\n"), zfs_get_name(clone));
4280b5de56dSgjelinek 		res = Z_ERR;
4290b5de56dSgjelinek 
430e9dbad6fSeschrock 	} else if (clean_out_clone() != Z_OK) {
431e9dbad6fSeschrock 		(void) fprintf(stderr, gettext("could not remove the "
432e9dbad6fSeschrock 		    "software inventory from ZFS clone %s\n"),
433e9dbad6fSeschrock 		    zfs_get_name(clone));
434e9dbad6fSeschrock 		res = Z_ERR;
4350b5de56dSgjelinek 	}
4360b5de56dSgjelinek 
4370b5de56dSgjelinek 	zfs_close(clone);
4380b5de56dSgjelinek 	return (res);
4390b5de56dSgjelinek }
4400b5de56dSgjelinek 
4410b5de56dSgjelinek /*
4420b5de56dSgjelinek  * This function takes a zonepath and attempts to determine what the ZFS
4430b5de56dSgjelinek  * file system name (not mountpoint) should be for that path.  We do not
4440b5de56dSgjelinek  * assume that zonepath is an existing directory or ZFS fs since we use
4450b5de56dSgjelinek  * this function as part of the process of creating a new ZFS fs or clone.
4460b5de56dSgjelinek  *
4470b5de56dSgjelinek  * The way this works is that we look at the parent directory of the zonepath
4480b5de56dSgjelinek  * to see if it is a ZFS fs.  If it is, we get the name of that ZFS fs and
4490b5de56dSgjelinek  * append the last component of the zonepath to generate the ZFS name for the
4500b5de56dSgjelinek  * zonepath.  This matches the algorithm that ZFS uses for automatically
4510b5de56dSgjelinek  * mounting a new fs after it is created.
4520b5de56dSgjelinek  *
4530b5de56dSgjelinek  * Although a ZFS fs can be mounted anywhere, we don't worry about handling
4540b5de56dSgjelinek  * all of the complexity that a user could possibly configure with arbitrary
4550b5de56dSgjelinek  * mounts since there is no way to generate a ZFS name from a random path in
4560b5de56dSgjelinek  * the file system.  We only try to handle the automatic mounts that ZFS does
4570b5de56dSgjelinek  * for each file system.  ZFS restricts this so that a new fs must be created
4580b5de56dSgjelinek  * in an existing parent ZFS fs.  It then automatically mounts the new fs
4590b5de56dSgjelinek  * directly under the mountpoint for the parent fs using the last component
4600b5de56dSgjelinek  * of the name as the mountpoint directory.
4610b5de56dSgjelinek  *
4620b5de56dSgjelinek  * For example:
4630b5de56dSgjelinek  *    Name			Mountpoint
4640b5de56dSgjelinek  *    space/eng/dev/test/zone1	/project1/eng/dev/test/zone1
4650b5de56dSgjelinek  *
4660b5de56dSgjelinek  * Return Z_OK if the path mapped to a ZFS file system name, otherwise return
4670b5de56dSgjelinek  * Z_ERR.
4680b5de56dSgjelinek  */
4690b5de56dSgjelinek static int
4700b5de56dSgjelinek path2name(char *zonepath, char *zfs_name, int len)
4710b5de56dSgjelinek {
4720b5de56dSgjelinek 	int		res;
47311506c41Sgjelinek 	char		*bnm, *dnm, *dname, *bname;
4740b5de56dSgjelinek 	zfs_handle_t	*zhp;
47511506c41Sgjelinek 	struct stat	stbuf;
47611506c41Sgjelinek 
47711506c41Sgjelinek 	/*
47811506c41Sgjelinek 	 * We need two tmp strings to handle paths directly in / (e.g. /foo)
47911506c41Sgjelinek 	 * since dirname will overwrite the first char after "/" in this case.
48011506c41Sgjelinek 	 */
48111506c41Sgjelinek 	if ((bnm = strdup(zonepath)) == NULL)
48211506c41Sgjelinek 		return (Z_ERR);
4830b5de56dSgjelinek 
48411506c41Sgjelinek 	if ((dnm = strdup(zonepath)) == NULL) {
48511506c41Sgjelinek 		free(bnm);
4860b5de56dSgjelinek 		return (Z_ERR);
48711506c41Sgjelinek 	}
48811506c41Sgjelinek 
48911506c41Sgjelinek 	bname = basename(bnm);
49011506c41Sgjelinek 	dname = dirname(dnm);
4910b5de56dSgjelinek 
4920b5de56dSgjelinek 	/*
49311506c41Sgjelinek 	 * This is a quick test to save iterating over all of the zfs datasets
49411506c41Sgjelinek 	 * on the system (which can be a lot).  If the parent dir is not in a
49511506c41Sgjelinek 	 * ZFS fs, then we're done.
4960b5de56dSgjelinek 	 */
49711506c41Sgjelinek 	if (stat(dname, &stbuf) != 0 || !S_ISDIR(stbuf.st_mode) ||
49811506c41Sgjelinek 	    strcmp(stbuf.st_fstype, MNTTYPE_ZFS) != 0) {
49911506c41Sgjelinek 		free(bnm);
50011506c41Sgjelinek 		free(dnm);
5010b5de56dSgjelinek 		return (Z_ERR);
50211506c41Sgjelinek 	}
50311506c41Sgjelinek 
50411506c41Sgjelinek 	/* See if the parent directory is its own ZFS dataset. */
50511506c41Sgjelinek 	if ((zhp = mount2zhandle(dname)) == NULL) {
50611506c41Sgjelinek 		/*
50711506c41Sgjelinek 		 * The parent is not a ZFS dataset so we can't automatically
50811506c41Sgjelinek 		 * create a dataset on the given path.
50911506c41Sgjelinek 		 */
51011506c41Sgjelinek 		free(bnm);
51111506c41Sgjelinek 		free(dnm);
51211506c41Sgjelinek 		return (Z_ERR);
51311506c41Sgjelinek 	}
5140b5de56dSgjelinek 
51511506c41Sgjelinek 	res = snprintf(zfs_name, len, "%s/%s", zfs_get_name(zhp), bname);
5160b5de56dSgjelinek 
51711506c41Sgjelinek 	free(bnm);
51811506c41Sgjelinek 	free(dnm);
5190b5de56dSgjelinek 	zfs_close(zhp);
5200b5de56dSgjelinek 	if (res >= len)
5210b5de56dSgjelinek 		return (Z_ERR);
5220b5de56dSgjelinek 
5230b5de56dSgjelinek 	return (Z_OK);
5240b5de56dSgjelinek }
5250b5de56dSgjelinek 
5260b5de56dSgjelinek /*
5270b5de56dSgjelinek  * A ZFS file system iterator call-back function used to determine if the
5280b5de56dSgjelinek  * file system has dependents (snapshots & clones).
5290b5de56dSgjelinek  */
5300b5de56dSgjelinek /* ARGSUSED */
5310b5de56dSgjelinek static int
5320b5de56dSgjelinek has_dependent(zfs_handle_t *zhp, void *data)
5330b5de56dSgjelinek {
5340b5de56dSgjelinek 	zfs_close(zhp);
5350b5de56dSgjelinek 	return (1);
5360b5de56dSgjelinek }
5370b5de56dSgjelinek 
5380b5de56dSgjelinek /*
5390b5de56dSgjelinek  * Given a snapshot name, get the file system path where the snapshot lives.
5400b5de56dSgjelinek  * A snapshot name is of the form fs_name@snap_name.  For example, snapshot
5410b5de56dSgjelinek  * pl/zones/z1@SUNWzone1 would have a path of
5420b5de56dSgjelinek  * /pl/zones/z1/.zfs/snapshot/SUNWzone1.
5430b5de56dSgjelinek  */
5440b5de56dSgjelinek static int
5450b5de56dSgjelinek snap2path(char *snap_name, char *path, int len)
5460b5de56dSgjelinek {
5470b5de56dSgjelinek 	char		*p;
5480b5de56dSgjelinek 	zfs_handle_t	*zhp;
5490b5de56dSgjelinek 	char		mp[ZFS_MAXPROPLEN];
5500b5de56dSgjelinek 
5510b5de56dSgjelinek 	if ((p = strrchr(snap_name, '@')) == NULL)
5520b5de56dSgjelinek 		return (Z_ERR);
5530b5de56dSgjelinek 
5540b5de56dSgjelinek 	/* Get the file system name from the snap_name. */
5550b5de56dSgjelinek 	*p = '\0';
556990b4856Slling 	zhp = zfs_open(g_zfs, snap_name, ZFS_TYPE_DATASET);
5570b5de56dSgjelinek 	*p = '@';
5580b5de56dSgjelinek 	if (zhp == NULL)
5590b5de56dSgjelinek 		return (Z_ERR);
5600b5de56dSgjelinek 
5610b5de56dSgjelinek 	/* Get the file system mount point. */
5620b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mp, sizeof (mp), NULL, NULL,
56399653d4eSeschrock 	    0, B_FALSE) != 0) {
5640b5de56dSgjelinek 		zfs_close(zhp);
5650b5de56dSgjelinek 		return (Z_ERR);
5660b5de56dSgjelinek 	}
5670b5de56dSgjelinek 	zfs_close(zhp);
5680b5de56dSgjelinek 
5690b5de56dSgjelinek 	p++;
5700b5de56dSgjelinek 	if (snprintf(path, len, "%s/.zfs/snapshot/%s", mp, p) >= len)
5710b5de56dSgjelinek 		return (Z_ERR);
5720b5de56dSgjelinek 
5730b5de56dSgjelinek 	return (Z_OK);
5740b5de56dSgjelinek }
5750b5de56dSgjelinek 
576286822ddS /*
577286822ddS  * This callback function is used to iterate through a snapshot's dependencies
578286822ddS  * to find a filesystem that is a direct clone of the snapshot being iterated.
579286822ddS  */
580286822ddS static int
581286822ddS get_direct_clone(zfs_handle_t *zhp, void *data)
582286822ddS {
583286822ddS 	clone_data_t	*cd = data;
584286822ddS 	char		origin[ZFS_MAXNAMELEN];
585286822ddS 	char		ds_path[ZFS_MAXNAMELEN];
586286822ddS 
587286822ddS 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
588286822ddS 		zfs_close(zhp);
589286822ddS 		return (0);
590286822ddS 	}
591286822ddS 
592286822ddS 	(void) strlcpy(ds_path, zfs_get_name(zhp), sizeof (ds_path));
593286822ddS 
594286822ddS 	/* Make sure this is a direct clone of the snapshot we're iterating. */
595286822ddS 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin, sizeof (origin), NULL,
596286822ddS 	    NULL, 0, B_FALSE) != 0 || strcmp(origin, cd->snapshot) != 0) {
597286822ddS 		zfs_close(zhp);
598286822ddS 		return (0);
599286822ddS 	}
600286822ddS 
601286822ddS 	if (cd->clone_zhp != NULL)
602286822ddS 		zfs_close(cd->clone_zhp);
603286822ddS 
604286822ddS 	cd->clone_zhp = zhp;
605286822ddS 	return (1);
606286822ddS }
607286822ddS 
608286822ddS /*
609286822ddS  * A ZFS file system iterator call-back function used to determine the clone
610286822ddS  * to promote.  This function finds the youngest (i.e. last one taken) snapshot
611286822ddS  * that has a clone.  If found, it returns a reference to that clone in the
612286822ddS  * callback data.
613286822ddS  */
614286822ddS static int
615286822ddS find_clone(zfs_handle_t *zhp, void *data)
616286822ddS {
617286822ddS 	clone_data_t	*cd = data;
618286822ddS 	time_t		snap_creation;
619286822ddS 	int		zret = 0;
620286822ddS 
621286822ddS 	/* If snapshot has no clones, skip it */
622286822ddS 	if (zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES) == 0) {
623286822ddS 		zfs_close(zhp);
624286822ddS 		return (0);
625286822ddS 	}
626286822ddS 
627286822ddS 	cd->snapshot = zfs_get_name(zhp);
628286822ddS 
629286822ddS 	/* Get the creation time of this snapshot */
630286822ddS 	snap_creation = (time_t)zfs_prop_get_int(zhp, ZFS_PROP_CREATION);
631286822ddS 
632286822ddS 	/*
633286822ddS 	 * If this snapshot's creation time is greater than (i.e. younger than)
634286822ddS 	 * the current youngest snapshot found, iterate this snapshot to
635286822ddS 	 * get the right clone.
636286822ddS 	 */
637286822ddS 	if (snap_creation >= cd->origin_creation) {
638286822ddS 		/*
639286822ddS 		 * Iterate the dependents of this snapshot to find a clone
640286822ddS 		 * that's a direct dependent.
641286822ddS 		 */
642286822ddS 		if ((zret = zfs_iter_dependents(zhp, B_FALSE, get_direct_clone,
643286822ddS 		    cd)) == -1) {
644286822ddS 			zfs_close(zhp);
645286822ddS 			return (1);
646286822ddS 		} else if (zret == 1) {
647286822ddS 			/*
648286822ddS 			 * Found a clone, update the origin_creation time
649286822ddS 			 * in the callback data.
650286822ddS 			 */
651286822ddS 			cd->origin_creation = snap_creation;
652286822ddS 		}
653286822ddS 	}
654286822ddS 
655286822ddS 	zfs_close(zhp);
656286822ddS 	return (0);
657286822ddS }
658286822ddS 
659286822ddS /*
660286822ddS  * A ZFS file system iterator call-back function used to remove standalone
661286822ddS  * snapshots.
662286822ddS  */
663286822ddS /* ARGSUSED */
664286822ddS static int
665286822ddS rm_snap(zfs_handle_t *zhp, void *data)
666286822ddS {
667286822ddS 	/* If snapshot has clones, something is wrong */
668286822ddS 	if (zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES) != 0) {
669286822ddS 		zfs_close(zhp);
670286822ddS 		return (1);
671286822ddS 	}
672286822ddS 
673286822ddS 	if (zfs_unmount(zhp, NULL, 0) == 0) {
674*842727c2SChris Kirby 		(void) zfs_destroy(zhp, B_FALSE);
675286822ddS 	}
676286822ddS 
677286822ddS 	zfs_close(zhp);
678286822ddS 	return (0);
679286822ddS }
680286822ddS 
681286822ddS /*
682286822ddS  * A ZFS snapshot iterator call-back function which renames snapshots.
683286822ddS  */
684286822ddS static int
685286822ddS rename_snap(zfs_handle_t *zhp, void *data)
686286822ddS {
687286822ddS 	int			res;
688286822ddS 	zfs_snapshot_data_t	*cbp;
689286822ddS 	char			template[ZFS_MAXNAMELEN];
690286822ddS 
691286822ddS 	cbp = (zfs_snapshot_data_t *)data;
692286822ddS 
693286822ddS 	/*
694286822ddS 	 * When renaming snapshots with the iterator, the iterator can see
695286822ddS 	 * the same snapshot after we've renamed up in the namespace.  To
696286822ddS 	 * prevent this we check the count for the number of snapshots we have
697286822ddS 	 * to rename and stop at that point.
698286822ddS 	 */
699286822ddS 	if (cbp->cntr >= cbp->num) {
700286822ddS 		zfs_close(zhp);
701286822ddS 		return (0);
702286822ddS 	}
703286822ddS 
704286822ddS 	if (zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) {
705286822ddS 		zfs_close(zhp);
706286822ddS 		return (0);
707286822ddS 	}
708286822ddS 
709286822ddS 	/* Only rename the snapshots we automatically generate when we clone. */
710286822ddS 	if (strncmp(zfs_get_name(zhp), cbp->match_name, cbp->len) != 0) {
711286822ddS 		zfs_close(zhp);
712286822ddS 		return (0);
713286822ddS 	}
714286822ddS 
715286822ddS 	(void) snprintf(template, sizeof (template), "%s%d", cbp->match_name,
716286822ddS 	    cbp->max++);
717286822ddS 
718286822ddS 	res = (zfs_rename(zhp, template, B_FALSE) != 0);
719286822ddS 	if (res != 0)
720286822ddS 		(void) fprintf(stderr, gettext("failed to rename snapshot %s "
721286822ddS 		    "to %s: %s\n"), zfs_get_name(zhp), template,
722286822ddS 		    libzfs_error_description(g_zfs));
723286822ddS 
724286822ddS 	cbp->cntr++;
725286822ddS 
726286822ddS 	zfs_close(zhp);
727286822ddS 	return (res);
728286822ddS }
729286822ddS 
730286822ddS /*
731286822ddS  * Rename the source dataset's snapshots that are automatically generated when
732286822ddS  * we clone a zone so that there won't be a name collision when we promote the
733286822ddS  * cloned dataset.  Once the snapshots have been renamed, then promote the
734286822ddS  * clone.
735286822ddS  *
736286822ddS  * The snapshot rename process gets the highest number on the snapshot names
737286822ddS  * (the format is zonename@SUNWzoneXX where XX are digits) on both the source
738286822ddS  * and clone datasets, then renames the source dataset snapshots starting at
739286822ddS  * the next number.
740286822ddS  */
741286822ddS static int
742286822ddS promote_clone(zfs_handle_t *src_zhp, zfs_handle_t *cln_zhp)
743286822ddS {
744286822ddS 	zfs_snapshot_data_t	sd;
745286822ddS 	char			nm[ZFS_MAXNAMELEN];
746286822ddS 	char			template[ZFS_MAXNAMELEN];
747286822ddS 
748286822ddS 	(void) strlcpy(nm, zfs_get_name(cln_zhp), sizeof (nm));
749286822ddS 	/*
750286822ddS 	 * Start by getting the clone's snapshot max which we use
751286822ddS 	 * during the rename of the original dataset's snapshots.
752286822ddS 	 */
753286822ddS 	(void) snprintf(template, sizeof (template), "%s@SUNWzone", nm);
754286822ddS 	sd.match_name = template;
755286822ddS 	sd.len = strlen(template);
756286822ddS 	sd.max = 0;
757286822ddS 
758286822ddS 	if (zfs_iter_snapshots(cln_zhp, get_snap_max, &sd) != 0)
759286822ddS 		return (Z_ERR);
760286822ddS 
761286822ddS 	/*
762286822ddS 	 * Now make sure the source's snapshot max is at least as high as
763286822ddS 	 * the clone's snapshot max.
764286822ddS 	 */
765286822ddS 	(void) snprintf(template, sizeof (template), "%s@SUNWzone",
766286822ddS 	    zfs_get_name(src_zhp));
767286822ddS 	sd.match_name = template;
768286822ddS 	sd.len = strlen(template);
769286822ddS 	sd.num = 0;
770286822ddS 
771286822ddS 	if (zfs_iter_snapshots(src_zhp, get_snap_max, &sd) != 0)
772286822ddS 		return (Z_ERR);
773286822ddS 
774286822ddS 	/*
775286822ddS 	 * Now rename the source dataset's snapshots so there's no
776286822ddS 	 * conflict when we promote the clone.
777286822ddS 	 */
778286822ddS 	sd.max++;
779286822ddS 	sd.cntr = 0;
780286822ddS 	if (zfs_iter_snapshots(src_zhp, rename_snap, &sd) != 0)
781286822ddS 		return (Z_ERR);
782286822ddS 
783286822ddS 	/* close and reopen the clone dataset to get the latest info */
784286822ddS 	zfs_close(cln_zhp);
785286822ddS 	if ((cln_zhp = zfs_open(g_zfs, nm, ZFS_TYPE_FILESYSTEM)) == NULL)
786286822ddS 		return (Z_ERR);
787286822ddS 
788286822ddS 	if (zfs_promote(cln_zhp) != 0) {
789286822ddS 		(void) fprintf(stderr, gettext("failed to promote %s: %s\n"),
790286822ddS 		    nm, libzfs_error_description(g_zfs));
791286822ddS 		return (Z_ERR);
792286822ddS 	}
793286822ddS 
794286822ddS 	zfs_close(cln_zhp);
795286822ddS 	return (Z_OK);
796286822ddS }
797286822ddS 
798286822ddS /*
799286822ddS  * Promote the youngest clone.  That clone will then become the origin of all
800286822ddS  * of the other clones that were hanging off of the source dataset.
801286822ddS  */
802286822ddS int
803286822ddS promote_all_clones(zfs_handle_t *zhp)
804286822ddS {
805286822ddS 	clone_data_t	cd;
806286822ddS 	char		nm[ZFS_MAXNAMELEN];
807286822ddS 
808286822ddS 	cd.clone_zhp = NULL;
809286822ddS 	cd.origin_creation = 0;
810286822ddS 	cd.snapshot = NULL;
811286822ddS 
812286822ddS 	if (zfs_iter_snapshots(zhp, find_clone, &cd) != 0) {
813286822ddS 		zfs_close(zhp);
814286822ddS 		return (Z_ERR);
815286822ddS 	}
816286822ddS 
817286822ddS 	/* Nothing to promote. */
818286822ddS 	if (cd.clone_zhp == NULL)
819286822ddS 		return (Z_OK);
820286822ddS 
821286822ddS 	/* Found the youngest clone to promote.  Promote it. */
822286822ddS 	if (promote_clone(zhp, cd.clone_zhp) != 0) {
823286822ddS 		zfs_close(cd.clone_zhp);
824286822ddS 		zfs_close(zhp);
825286822ddS 		return (Z_ERR);
826286822ddS 	}
827286822ddS 
828286822ddS 	/* close and reopen the main dataset to get the latest info */
829286822ddS 	(void) strlcpy(nm, zfs_get_name(zhp), sizeof (nm));
830286822ddS 	zfs_close(zhp);
831286822ddS 	if ((zhp = zfs_open(g_zfs, nm, ZFS_TYPE_FILESYSTEM)) == NULL)
832286822ddS 		return (Z_ERR);
833286822ddS 
834286822ddS 	return (Z_OK);
835286822ddS }
836286822ddS 
8370b5de56dSgjelinek /*
8380b5de56dSgjelinek  * Clone a pre-existing ZFS snapshot, either by making a direct ZFS clone, if
8390b5de56dSgjelinek  * possible, or by copying the data from the snapshot to the zonepath.
8400b5de56dSgjelinek  */
8410b5de56dSgjelinek int
842ff17c8bfSgjelinek clone_snapshot_zfs(char *snap_name, char *zonepath, char *validatesnap)
8430b5de56dSgjelinek {
8440b5de56dSgjelinek 	int	err = Z_OK;
8450b5de56dSgjelinek 	char	clone_name[MAXPATHLEN];
8460b5de56dSgjelinek 	char	snap_path[MAXPATHLEN];
8470b5de56dSgjelinek 
8480b5de56dSgjelinek 	if (snap2path(snap_name, snap_path, sizeof (snap_path)) != Z_OK) {
8490b5de56dSgjelinek 		(void) fprintf(stderr, gettext("unable to find path for %s.\n"),
8500b5de56dSgjelinek 		    snap_name);
8510b5de56dSgjelinek 		return (Z_ERR);
8520b5de56dSgjelinek 	}
8530b5de56dSgjelinek 
854ff17c8bfSgjelinek 	if (validate_snapshot(snap_name, snap_path, validatesnap) != Z_OK)
8550b5de56dSgjelinek 		return (Z_NO_ENTRY);
8560b5de56dSgjelinek 
8570b5de56dSgjelinek 	/*
8580b5de56dSgjelinek 	 * The zonepath cannot be ZFS cloned, try to copy the data from
8590b5de56dSgjelinek 	 * within the snapshot to the zonepath.
8600b5de56dSgjelinek 	 */
8610b5de56dSgjelinek 	if (path2name(zonepath, clone_name, sizeof (clone_name)) != Z_OK) {
8620b5de56dSgjelinek 		if ((err = clone_copy(snap_path, zonepath)) == Z_OK)
8630b5de56dSgjelinek 			if (clean_out_clone() != Z_OK)
8640b5de56dSgjelinek 				(void) fprintf(stderr,
8650b5de56dSgjelinek 				    gettext("could not remove the "
8660b5de56dSgjelinek 				    "software inventory from %s\n"), zonepath);
8670b5de56dSgjelinek 
8680b5de56dSgjelinek 		return (err);
8690b5de56dSgjelinek 	}
8700b5de56dSgjelinek 
8710b5de56dSgjelinek 	if ((err = clone_snap(snap_name, clone_name)) != Z_OK) {
8720b5de56dSgjelinek 		if (err != Z_NO_ENTRY) {
8730b5de56dSgjelinek 			/*
8740b5de56dSgjelinek 			 * Cloning the snapshot failed.  Fall back to trying
8750b5de56dSgjelinek 			 * to install the zone by copying from the snapshot.
8760b5de56dSgjelinek 			 */
8770b5de56dSgjelinek 			if ((err = clone_copy(snap_path, zonepath)) == Z_OK)
8780b5de56dSgjelinek 				if (clean_out_clone() != Z_OK)
8790b5de56dSgjelinek 					(void) fprintf(stderr,
8800b5de56dSgjelinek 					    gettext("could not remove the "
8810b5de56dSgjelinek 					    "software inventory from %s\n"),
8820b5de56dSgjelinek 					    zonepath);
8830b5de56dSgjelinek 		} else {
8840b5de56dSgjelinek 			/*
8850b5de56dSgjelinek 			 * The snapshot is unusable for some reason so restore
8860b5de56dSgjelinek 			 * the zone state to configured since we were unable to
8870b5de56dSgjelinek 			 * actually do anything about getting the zone
8880b5de56dSgjelinek 			 * installed.
8890b5de56dSgjelinek 			 */
8900b5de56dSgjelinek 			int tmp;
8910b5de56dSgjelinek 
8920b5de56dSgjelinek 			if ((tmp = zone_set_state(target_zone,
8930b5de56dSgjelinek 			    ZONE_STATE_CONFIGURED)) != Z_OK) {
8940b5de56dSgjelinek 				errno = tmp;
8950b5de56dSgjelinek 				zperror2(target_zone,
8960b5de56dSgjelinek 				    gettext("could not set state"));
8970b5de56dSgjelinek 			}
8980b5de56dSgjelinek 		}
8990b5de56dSgjelinek 	}
9000b5de56dSgjelinek 
9010b5de56dSgjelinek 	return (err);
9020b5de56dSgjelinek }
9030b5de56dSgjelinek 
9040b5de56dSgjelinek /*
9050b5de56dSgjelinek  * Attempt to clone a source_zone to a target zonepath by using a ZFS clone.
9060b5de56dSgjelinek  */
9070b5de56dSgjelinek int
908ff17c8bfSgjelinek clone_zfs(char *source_zonepath, char *zonepath, char *presnapbuf,
909ff17c8bfSgjelinek     char *postsnapbuf)
9100b5de56dSgjelinek {
9110b5de56dSgjelinek 	zfs_handle_t	*zhp;
9120b5de56dSgjelinek 	char		clone_name[MAXPATHLEN];
9130b5de56dSgjelinek 	char		snap_name[MAXPATHLEN];
9140b5de56dSgjelinek 
9150b5de56dSgjelinek 	/*
9160b5de56dSgjelinek 	 * Try to get a zfs handle for the source_zonepath.  If this fails
9170b5de56dSgjelinek 	 * the source_zonepath is not ZFS so return an error.
9180b5de56dSgjelinek 	 */
9190b5de56dSgjelinek 	if ((zhp = mount2zhandle(source_zonepath)) == NULL)
9200b5de56dSgjelinek 		return (Z_ERR);
9210b5de56dSgjelinek 
9220b5de56dSgjelinek 	/*
9230b5de56dSgjelinek 	 * Check if there is a file system already mounted on zonepath.  If so,
9240b5de56dSgjelinek 	 * we can't clone to the path so we should fall back to copying.
9250b5de56dSgjelinek 	 */
9260b5de56dSgjelinek 	if (is_mountpnt(zonepath)) {
9270b5de56dSgjelinek 		zfs_close(zhp);
9280b5de56dSgjelinek 		(void) fprintf(stderr,
9290b5de56dSgjelinek 		    gettext("A file system is already mounted on %s,\n"
9300b5de56dSgjelinek 		    "preventing use of a ZFS clone.\n"), zonepath);
9310b5de56dSgjelinek 		return (Z_ERR);
9320b5de56dSgjelinek 	}
9330b5de56dSgjelinek 
9340b5de56dSgjelinek 	/*
9350b5de56dSgjelinek 	 * Instead of using path2name to get the clone name from the zonepath,
9360b5de56dSgjelinek 	 * we could generate a name from the source zone ZFS name.  However,
9370b5de56dSgjelinek 	 * this would mean we would create the clone under the ZFS fs of the
9380b5de56dSgjelinek 	 * source instead of what the zonepath says.  For example,
9390b5de56dSgjelinek 	 *
9400b5de56dSgjelinek 	 * source_zonepath		zonepath
9410b5de56dSgjelinek 	 * /pl/zones/dev/z1		/pl/zones/deploy/z2
9420b5de56dSgjelinek 	 *
9430b5de56dSgjelinek 	 * We don't want the clone to be under "dev", we want it under
9440b5de56dSgjelinek 	 * "deploy", so that we can leverage the normal attribute inheritance
9450b5de56dSgjelinek 	 * that ZFS provides in the fs hierarchy.
9460b5de56dSgjelinek 	 */
9470b5de56dSgjelinek 	if (path2name(zonepath, clone_name, sizeof (clone_name)) != Z_OK) {
9480b5de56dSgjelinek 		zfs_close(zhp);
9490b5de56dSgjelinek 		return (Z_ERR);
9500b5de56dSgjelinek 	}
9510b5de56dSgjelinek 
952ff17c8bfSgjelinek 	if (take_snapshot(zhp, snap_name, sizeof (snap_name), presnapbuf,
953ff17c8bfSgjelinek 	    postsnapbuf) != Z_OK) {
9540b5de56dSgjelinek 		zfs_close(zhp);
9550b5de56dSgjelinek 		return (Z_ERR);
9560b5de56dSgjelinek 	}
9570b5de56dSgjelinek 	zfs_close(zhp);
9580b5de56dSgjelinek 
959d9e728a2Sgjelinek 	if (clone_snap(snap_name, clone_name) != Z_OK) {
960d9e728a2Sgjelinek 		/* Clean up the snapshot we just took. */
961d9e728a2Sgjelinek 		if ((zhp = zfs_open(g_zfs, snap_name, ZFS_TYPE_SNAPSHOT))
962d9e728a2Sgjelinek 		    != NULL) {
963d9e728a2Sgjelinek 			if (zfs_unmount(zhp, NULL, 0) == 0)
964*842727c2SChris Kirby 				(void) zfs_destroy(zhp, B_FALSE);
965d9e728a2Sgjelinek 			zfs_close(zhp);
966d9e728a2Sgjelinek 		}
967d9e728a2Sgjelinek 
9680b5de56dSgjelinek 		return (Z_ERR);
969d9e728a2Sgjelinek 	}
9700b5de56dSgjelinek 
9710b5de56dSgjelinek 	(void) printf(gettext("Instead of copying, a ZFS clone has been "
9720b5de56dSgjelinek 	    "created for this zone.\n"));
9730b5de56dSgjelinek 
9740b5de56dSgjelinek 	return (Z_OK);
9750b5de56dSgjelinek }
9760b5de56dSgjelinek 
9770b5de56dSgjelinek /*
9780b5de56dSgjelinek  * Attempt to create a ZFS file system for the specified zonepath.
9790b5de56dSgjelinek  * We either will successfully create a ZFS file system and get it mounted
9800b5de56dSgjelinek  * on the zonepath or we don't.  The caller doesn't care since a regular
9810b5de56dSgjelinek  * directory is used for the zonepath if no ZFS file system is mounted there.
9820b5de56dSgjelinek  */
9830b5de56dSgjelinek void
9840b5de56dSgjelinek create_zfs_zonepath(char *zonepath)
9850b5de56dSgjelinek {
9860b5de56dSgjelinek 	zfs_handle_t	*zhp;
9870b5de56dSgjelinek 	char		zfs_name[MAXPATHLEN];
988e9dbad6fSeschrock 	nvlist_t	*props = NULL;
9890b5de56dSgjelinek 
9900b5de56dSgjelinek 	if (path2name(zonepath, zfs_name, sizeof (zfs_name)) != Z_OK)
9910b5de56dSgjelinek 		return;
9920b5de56dSgjelinek 
993d1f855d7S 	/* Check if the dataset already exists. */
994d1f855d7S 	if ((zhp = zfs_open(g_zfs, zfs_name, ZFS_TYPE_DATASET)) != NULL) {
995d1f855d7S 		zfs_close(zhp);
996d1f855d7S 		return;
997d1f855d7S 	}
998d1f855d7S 
999e9dbad6fSeschrock 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0 ||
10005f8e1617Snn 	    nvlist_add_string(props, zfs_prop_to_name(ZFS_PROP_SHARENFS),
10015f8e1617Snn 	    "off") != 0) {
10025f8e1617Snn 		if (props != NULL)
10035f8e1617Snn 			nvlist_free(props);
1004e9dbad6fSeschrock 		(void) fprintf(stderr, gettext("cannot create ZFS dataset %s: "
1005e9dbad6fSeschrock 		    "out of memory\n"), zfs_name);
1006e9dbad6fSeschrock 	}
1007e9dbad6fSeschrock 
1008e9dbad6fSeschrock 	if (zfs_create(g_zfs, zfs_name, ZFS_TYPE_FILESYSTEM, props) != 0 ||
1009990b4856Slling 	    (zhp = zfs_open(g_zfs, zfs_name, ZFS_TYPE_DATASET)) == NULL) {
101099653d4eSeschrock 		(void) fprintf(stderr, gettext("cannot create ZFS dataset %s: "
101199653d4eSeschrock 		    "%s\n"), zfs_name, libzfs_error_description(g_zfs));
1012e9dbad6fSeschrock 		nvlist_free(props);
10130b5de56dSgjelinek 		return;
10140b5de56dSgjelinek 	}
10150b5de56dSgjelinek 
1016e9dbad6fSeschrock 	nvlist_free(props);
1017e9dbad6fSeschrock 
10180b5de56dSgjelinek 	if (zfs_mount(zhp, NULL, 0) != 0) {
101999653d4eSeschrock 		(void) fprintf(stderr, gettext("cannot mount ZFS dataset %s: "
102099653d4eSeschrock 		    "%s\n"), zfs_name, libzfs_error_description(g_zfs));
1021*842727c2SChris Kirby 		(void) zfs_destroy(zhp, B_FALSE);
10220b5de56dSgjelinek 	} else {
10230b5de56dSgjelinek 		if (chmod(zonepath, S_IRWXU) != 0) {
10240b5de56dSgjelinek 			(void) fprintf(stderr, gettext("file system %s "
10250b5de56dSgjelinek 			    "successfully created, but chmod %o failed: %s\n"),
10260b5de56dSgjelinek 			    zfs_name, S_IRWXU, strerror(errno));
10270b5de56dSgjelinek 			(void) destroy_zfs(zonepath);
10280b5de56dSgjelinek 		} else {
10290b5de56dSgjelinek 			(void) printf(gettext("A ZFS file system has been "
10300b5de56dSgjelinek 			    "created for this zone.\n"));
10310b5de56dSgjelinek 		}
10320b5de56dSgjelinek 	}
10330b5de56dSgjelinek 
10340b5de56dSgjelinek 	zfs_close(zhp);
10350b5de56dSgjelinek }
10360b5de56dSgjelinek 
10370b5de56dSgjelinek /*
10380b5de56dSgjelinek  * If the zonepath is a ZFS file system, attempt to destroy it.  We return Z_OK
10390b5de56dSgjelinek  * if we were able to zfs_destroy the zonepath, otherwise we return Z_ERR
10400b5de56dSgjelinek  * which means the caller should clean up the zonepath in the traditional
10410b5de56dSgjelinek  * way.
10420b5de56dSgjelinek  */
10430b5de56dSgjelinek int
10440b5de56dSgjelinek destroy_zfs(char *zonepath)
10450b5de56dSgjelinek {
10460b5de56dSgjelinek 	zfs_handle_t	*zhp;
10470b5de56dSgjelinek 	boolean_t	is_clone = B_FALSE;
10480b5de56dSgjelinek 	char		origin[ZFS_MAXPROPLEN];
10490b5de56dSgjelinek 
105099653d4eSeschrock 	if ((zhp = mount2zhandle(zonepath)) == NULL)
10510b5de56dSgjelinek 		return (Z_ERR);
10520b5de56dSgjelinek 
1053286822ddS 	if (promote_all_clones(zhp) != 0)
1054286822ddS 		return (Z_ERR);
1055286822ddS 
1056286822ddS 	/* Now cleanup any snapshots remaining. */
1057286822ddS 	if (zfs_iter_snapshots(zhp, rm_snap, NULL) != 0) {
1058286822ddS 		zfs_close(zhp);
1059286822ddS 		return (Z_ERR);
1060286822ddS 	}
1061286822ddS 
10620b5de56dSgjelinek 	/*
1063286822ddS 	 * We can't destroy the file system if it has still has dependents.
1064286822ddS 	 * There shouldn't be any at this point, but we'll double check.
10650b5de56dSgjelinek 	 */
1066286822ddS 	if (zfs_iter_dependents(zhp, B_TRUE, has_dependent, NULL) != 0) {
1067286822ddS 		(void) fprintf(stderr, gettext("zfs destroy %s failed: the "
1068286822ddS 		    "dataset still has dependents\n"), zfs_get_name(zhp));
10690b5de56dSgjelinek 		zfs_close(zhp);
10700b5de56dSgjelinek 		return (Z_ERR);
10710b5de56dSgjelinek 	}
10720b5de56dSgjelinek 
10730b5de56dSgjelinek 	/*
10740b5de56dSgjelinek 	 * This might be a clone.  Try to get the snapshot so we can attempt
10750b5de56dSgjelinek 	 * to destroy that as well.
10760b5de56dSgjelinek 	 */
10770b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin, sizeof (origin), NULL,
107899653d4eSeschrock 	    NULL, 0, B_FALSE) == 0)
10790b5de56dSgjelinek 		is_clone = B_TRUE;
10800b5de56dSgjelinek 
1081286822ddS 	if (zfs_unmount(zhp, NULL, 0) != 0) {
1082286822ddS 		(void) fprintf(stderr, gettext("zfs unmount %s failed: %s\n"),
1083286822ddS 		    zfs_get_name(zhp), libzfs_error_description(g_zfs));
1084286822ddS 		zfs_close(zhp);
1085286822ddS 		return (Z_ERR);
1086286822ddS 	}
1087286822ddS 
1088*842727c2SChris Kirby 	if (zfs_destroy(zhp, B_FALSE) != 0) {
10890b5de56dSgjelinek 		/*
10900b5de56dSgjelinek 		 * If the destroy fails for some reason, try to remount
10910b5de56dSgjelinek 		 * the file system so that we can use "rm -rf" to clean up
10920b5de56dSgjelinek 		 * instead.
10930b5de56dSgjelinek 		 */
1094286822ddS 		(void) fprintf(stderr, gettext("zfs destroy %s failed: %s\n"),
1095286822ddS 		    zfs_get_name(zhp), libzfs_error_description(g_zfs));
10960b5de56dSgjelinek 		(void) zfs_mount(zhp, NULL, 0);
10970b5de56dSgjelinek 		zfs_close(zhp);
10980b5de56dSgjelinek 		return (Z_ERR);
10990b5de56dSgjelinek 	}
11000b5de56dSgjelinek 
1101d9e728a2Sgjelinek 	/*
1102d9e728a2Sgjelinek 	 * If the zone has ever been moved then the mountpoint dir will not be
1103d9e728a2Sgjelinek 	 * cleaned up by the zfs_destroy().  To handle this case try to clean
1104d9e728a2Sgjelinek 	 * it up now but don't worry if it fails, that will be normal.
1105d9e728a2Sgjelinek 	 */
1106d9e728a2Sgjelinek 	(void) rmdir(zonepath);
1107d9e728a2Sgjelinek 
11080b5de56dSgjelinek 	(void) printf(gettext("The ZFS file system for this zone has been "
11090b5de56dSgjelinek 	    "destroyed.\n"));
11100b5de56dSgjelinek 
11110b5de56dSgjelinek 	if (is_clone) {
11120b5de56dSgjelinek 		zfs_handle_t	*ohp;
11130b5de56dSgjelinek 
11140b5de56dSgjelinek 		/*
11150b5de56dSgjelinek 		 * Try to clean up the snapshot that the clone was taken from.
11160b5de56dSgjelinek 		 */
111799653d4eSeschrock 		if ((ohp = zfs_open(g_zfs, origin,
111899653d4eSeschrock 		    ZFS_TYPE_SNAPSHOT)) != NULL) {
11193bb79becSeschrock 			if (zfs_iter_dependents(ohp, B_TRUE, has_dependent,
11203bb79becSeschrock 			    NULL) == 0 && zfs_unmount(ohp, NULL, 0) == 0)
1121*842727c2SChris Kirby 				(void) zfs_destroy(ohp, B_FALSE);
11220b5de56dSgjelinek 			zfs_close(ohp);
11230b5de56dSgjelinek 		}
11240b5de56dSgjelinek 	}
11250b5de56dSgjelinek 
11260b5de56dSgjelinek 	zfs_close(zhp);
11270b5de56dSgjelinek 	return (Z_OK);
11280b5de56dSgjelinek }
11290b5de56dSgjelinek 
11300b5de56dSgjelinek /*
11310b5de56dSgjelinek  * Return true if the path is its own zfs file system.  We determine this
11320b5de56dSgjelinek  * by stat-ing the path to see if it is zfs and stat-ing the parent to see
11330b5de56dSgjelinek  * if it is a different fs.
11340b5de56dSgjelinek  */
11350b5de56dSgjelinek boolean_t
11360b5de56dSgjelinek is_zonepath_zfs(char *zonepath)
11370b5de56dSgjelinek {
11380b5de56dSgjelinek 	int res;
11390b5de56dSgjelinek 	char *path;
11400b5de56dSgjelinek 	char *parent;
11413f2f09c1Sdp 	struct statvfs64 buf1, buf2;
11420b5de56dSgjelinek 
11433f2f09c1Sdp 	if (statvfs64(zonepath, &buf1) != 0)
11440b5de56dSgjelinek 		return (B_FALSE);
11450b5de56dSgjelinek 
11460b5de56dSgjelinek 	if (strcmp(buf1.f_basetype, "zfs") != 0)
11470b5de56dSgjelinek 		return (B_FALSE);
11480b5de56dSgjelinek 
11490b5de56dSgjelinek 	if ((path = strdup(zonepath)) == NULL)
11500b5de56dSgjelinek 		return (B_FALSE);
11510b5de56dSgjelinek 
11520b5de56dSgjelinek 	parent = dirname(path);
11533f2f09c1Sdp 	res = statvfs64(parent, &buf2);
11540b5de56dSgjelinek 	free(path);
11550b5de56dSgjelinek 
11560b5de56dSgjelinek 	if (res != 0)
11570b5de56dSgjelinek 		return (B_FALSE);
11580b5de56dSgjelinek 
11590b5de56dSgjelinek 	if (buf1.f_fsid == buf2.f_fsid)
11600b5de56dSgjelinek 		return (B_FALSE);
11610b5de56dSgjelinek 
11620b5de56dSgjelinek 	return (B_TRUE);
11630b5de56dSgjelinek }
11640b5de56dSgjelinek 
11650b5de56dSgjelinek /*
11660b5de56dSgjelinek  * Implement the fast move of a ZFS file system by simply updating the
11670b5de56dSgjelinek  * mountpoint.  Since it is file system already, we don't have the
11680b5de56dSgjelinek  * issue of cross-file system copying.
11690b5de56dSgjelinek  */
11700b5de56dSgjelinek int
11710b5de56dSgjelinek move_zfs(char *zonepath, char *new_zonepath)
11720b5de56dSgjelinek {
11730b5de56dSgjelinek 	int		ret = Z_ERR;
11740b5de56dSgjelinek 	zfs_handle_t	*zhp;
11750b5de56dSgjelinek 
117699653d4eSeschrock 	if ((zhp = mount2zhandle(zonepath)) == NULL)
11770b5de56dSgjelinek 		return (Z_ERR);
11780b5de56dSgjelinek 
1179e9dbad6fSeschrock 	if (zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1180e9dbad6fSeschrock 	    new_zonepath) == 0) {
11810b5de56dSgjelinek 		/*
11820b5de56dSgjelinek 		 * Clean up the old mount point.  We ignore any failure since
11830b5de56dSgjelinek 		 * the zone is already successfully mounted on the new path.
11840b5de56dSgjelinek 		 */
11850b5de56dSgjelinek 		(void) rmdir(zonepath);
11860b5de56dSgjelinek 		ret = Z_OK;
11870b5de56dSgjelinek 	}
11880b5de56dSgjelinek 
11890b5de56dSgjelinek 	zfs_close(zhp);
11900b5de56dSgjelinek 
11910b5de56dSgjelinek 	return (ret);
11920b5de56dSgjelinek }
11930b5de56dSgjelinek 
11940b5de56dSgjelinek /*
11950b5de56dSgjelinek  * Validate that the given dataset exists on the system, and that neither it nor
11960b5de56dSgjelinek  * its children are zvols.
11970b5de56dSgjelinek  *
11980b5de56dSgjelinek  * Note that we don't do anything with the 'zoned' property here.  All
11990b5de56dSgjelinek  * management is done in zoneadmd when the zone is actually rebooted.  This
12000b5de56dSgjelinek  * allows us to automatically set the zoned property even when a zone is
12010b5de56dSgjelinek  * rebooted by the administrator.
12020b5de56dSgjelinek  */
12030b5de56dSgjelinek int
12040b5de56dSgjelinek verify_datasets(zone_dochandle_t handle)
12050b5de56dSgjelinek {
12060b5de56dSgjelinek 	int return_code = Z_OK;
12070b5de56dSgjelinek 	struct zone_dstab dstab;
12080b5de56dSgjelinek 	zfs_handle_t *zhp;
12090b5de56dSgjelinek 	char propbuf[ZFS_MAXPROPLEN];
12100b5de56dSgjelinek 	char source[ZFS_MAXNAMELEN];
1211990b4856Slling 	zprop_source_t srctype;
12120b5de56dSgjelinek 
12130b5de56dSgjelinek 	if (zonecfg_setdsent(handle) != Z_OK) {
12140b5de56dSgjelinek 		/*
12150b5de56dSgjelinek 		 * TRANSLATION_NOTE
12160b5de56dSgjelinek 		 * zfs and dataset are literals that should not be translated.
12170b5de56dSgjelinek 		 */
12180b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify zfs datasets: "
12190b5de56dSgjelinek 		    "unable to enumerate datasets\n"));
12200b5de56dSgjelinek 		return (Z_ERR);
12210b5de56dSgjelinek 	}
12220b5de56dSgjelinek 
12230b5de56dSgjelinek 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
12240b5de56dSgjelinek 
122599653d4eSeschrock 		if ((zhp = zfs_open(g_zfs, dstab.zone_dataset_name,
12260b5de56dSgjelinek 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
122799653d4eSeschrock 			(void) fprintf(stderr, gettext("could not verify zfs "
122899653d4eSeschrock 			    "dataset %s: %s\n"), dstab.zone_dataset_name,
122999653d4eSeschrock 			    libzfs_error_description(g_zfs));
12300b5de56dSgjelinek 			return_code = Z_ERR;
12310b5de56dSgjelinek 			continue;
12320b5de56dSgjelinek 		}
12330b5de56dSgjelinek 
12340b5de56dSgjelinek 		if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf,
12350b5de56dSgjelinek 		    sizeof (propbuf), &srctype, source,
12360b5de56dSgjelinek 		    sizeof (source), 0) == 0 &&
1237990b4856Slling 		    (srctype == ZPROP_SRC_INHERITED)) {
12380b5de56dSgjelinek 			(void) fprintf(stderr, gettext("could not verify zfs "
12390b5de56dSgjelinek 			    "dataset %s: mountpoint cannot be inherited\n"),
12400b5de56dSgjelinek 			    dstab.zone_dataset_name);
12410b5de56dSgjelinek 			return_code = Z_ERR;
12420b5de56dSgjelinek 			zfs_close(zhp);
12430b5de56dSgjelinek 			continue;
12440b5de56dSgjelinek 		}
12450b5de56dSgjelinek 
12460b5de56dSgjelinek 		if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
12470b5de56dSgjelinek 			(void) fprintf(stderr, gettext("cannot verify zfs "
12480b5de56dSgjelinek 			    "dataset %s: volumes cannot be specified as a "
12490b5de56dSgjelinek 			    "zone dataset resource\n"),
12500b5de56dSgjelinek 			    dstab.zone_dataset_name);
12510b5de56dSgjelinek 			return_code = Z_ERR;
12520b5de56dSgjelinek 		}
12530b5de56dSgjelinek 
12540b5de56dSgjelinek 		if (zfs_iter_children(zhp, check_zvol, NULL) != 0)
12550b5de56dSgjelinek 			return_code = Z_ERR;
12560b5de56dSgjelinek 
12570b5de56dSgjelinek 		zfs_close(zhp);
12580b5de56dSgjelinek 	}
12590b5de56dSgjelinek 	(void) zonecfg_enddsent(handle);
12600b5de56dSgjelinek 
12610b5de56dSgjelinek 	return (return_code);
12620b5de56dSgjelinek }
12630b5de56dSgjelinek 
12640b5de56dSgjelinek /*
12650b5de56dSgjelinek  * Verify that the ZFS dataset exists, and its mountpoint
12660b5de56dSgjelinek  * property is set to "legacy".
12670b5de56dSgjelinek  */
12680b5de56dSgjelinek int
12690b5de56dSgjelinek verify_fs_zfs(struct zone_fstab *fstab)
12700b5de56dSgjelinek {
12710b5de56dSgjelinek 	zfs_handle_t *zhp;
12720b5de56dSgjelinek 	char propbuf[ZFS_MAXPROPLEN];
12730b5de56dSgjelinek 
127499653d4eSeschrock 	if ((zhp = zfs_open(g_zfs, fstab->zone_fs_special,
1275990b4856Slling 	    ZFS_TYPE_DATASET)) == NULL) {
12760b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify fs %s: "
12770b5de56dSgjelinek 		    "could not access zfs dataset '%s'\n"),
12780b5de56dSgjelinek 		    fstab->zone_fs_dir, fstab->zone_fs_special);
12790b5de56dSgjelinek 		return (Z_ERR);
12800b5de56dSgjelinek 	}
12810b5de56dSgjelinek 
12820b5de56dSgjelinek 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
12830b5de56dSgjelinek 		(void) fprintf(stderr, gettext("cannot verify fs %s: "
12840b5de56dSgjelinek 		    "'%s' is not a file system\n"),
12850b5de56dSgjelinek 		    fstab->zone_fs_dir, fstab->zone_fs_special);
12860b5de56dSgjelinek 		zfs_close(zhp);
12870b5de56dSgjelinek 		return (Z_ERR);
12880b5de56dSgjelinek 	}
12890b5de56dSgjelinek 
12900b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, sizeof (propbuf),
12910b5de56dSgjelinek 	    NULL, NULL, 0, 0) != 0 || strcmp(propbuf, "legacy") != 0) {
12920b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify fs %s: "
12930b5de56dSgjelinek 		    "zfs '%s' mountpoint is not \"legacy\"\n"),
12940b5de56dSgjelinek 		    fstab->zone_fs_dir, fstab->zone_fs_special);
12950b5de56dSgjelinek 		zfs_close(zhp);
12960b5de56dSgjelinek 		return (Z_ERR);
12970b5de56dSgjelinek 	}
12980b5de56dSgjelinek 
12990b5de56dSgjelinek 	zfs_close(zhp);
130099653d4eSeschrock 	return (Z_OK);
130199653d4eSeschrock }
130299653d4eSeschrock 
130399653d4eSeschrock int
130499653d4eSeschrock init_zfs(void)
130599653d4eSeschrock {
130699653d4eSeschrock 	if ((g_zfs = libzfs_init()) == NULL) {
130799653d4eSeschrock 		(void) fprintf(stderr, gettext("failed to initialize ZFS "
130899653d4eSeschrock 		    "library\n"));
130999653d4eSeschrock 		return (Z_ERR);
131099653d4eSeschrock 	}
131199653d4eSeschrock 
13120b5de56dSgjelinek 	return (Z_OK);
13130b5de56dSgjelinek }
1314