xref: /illumos-gate/usr/src/cmd/zoneadm/zfs.c (revision c75cc341)
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>
460b5de56dSgjelinek 
470b5de56dSgjelinek #include "zoneadm.h"
480b5de56dSgjelinek 
4999653d4eSeschrock libzfs_handle_t *g_zfs;
500b5de56dSgjelinek 
510b5de56dSgjelinek typedef struct zfs_mount_data {
520b5de56dSgjelinek 	char		*match_name;
530b5de56dSgjelinek 	zfs_handle_t	*match_handle;
540b5de56dSgjelinek } zfs_mount_data_t;
550b5de56dSgjelinek 
560b5de56dSgjelinek typedef struct zfs_snapshot_data {
570b5de56dSgjelinek 	char	*match_name;
580b5de56dSgjelinek 	int	len;
590b5de56dSgjelinek 	int	max;
600b5de56dSgjelinek } zfs_snapshot_data_t;
610b5de56dSgjelinek 
620b5de56dSgjelinek /*
630b5de56dSgjelinek  * A ZFS file system iterator call-back function which is used to validate
640b5de56dSgjelinek  * datasets imported into the zone.
650b5de56dSgjelinek  */
660b5de56dSgjelinek /* ARGSUSED */
670b5de56dSgjelinek static int
680b5de56dSgjelinek check_zvol(zfs_handle_t *zhp, void *unused)
690b5de56dSgjelinek {
700b5de56dSgjelinek 	int ret;
710b5de56dSgjelinek 
720b5de56dSgjelinek 	if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
730b5de56dSgjelinek 		/*
740b5de56dSgjelinek 		 * TRANSLATION_NOTE
750b5de56dSgjelinek 		 * zfs and dataset are literals that should not be translated.
760b5de56dSgjelinek 		 */
770b5de56dSgjelinek 		(void) fprintf(stderr, gettext("cannot verify zfs dataset %s: "
780b5de56dSgjelinek 		    "volumes cannot be specified as a zone dataset resource\n"),
790b5de56dSgjelinek 		    zfs_get_name(zhp));
800b5de56dSgjelinek 		ret = -1;
810b5de56dSgjelinek 	} else {
820b5de56dSgjelinek 		ret = zfs_iter_children(zhp, check_zvol, NULL);
830b5de56dSgjelinek 	}
840b5de56dSgjelinek 
850b5de56dSgjelinek 	zfs_close(zhp);
860b5de56dSgjelinek 
870b5de56dSgjelinek 	return (ret);
880b5de56dSgjelinek }
890b5de56dSgjelinek 
900b5de56dSgjelinek /*
910b5de56dSgjelinek  * A ZFS file system iterator call-back function which returns the
920b5de56dSgjelinek  * zfs_handle_t for a ZFS file system on the specified mount point.
930b5de56dSgjelinek  */
940b5de56dSgjelinek static int
950b5de56dSgjelinek match_mountpoint(zfs_handle_t *zhp, void *data)
960b5de56dSgjelinek {
970b5de56dSgjelinek 	int			res;
980b5de56dSgjelinek 	zfs_mount_data_t	*cbp;
990b5de56dSgjelinek 	char			mp[ZFS_MAXPROPLEN];
1000b5de56dSgjelinek 
1010b5de56dSgjelinek 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
1020b5de56dSgjelinek 		zfs_close(zhp);
1030b5de56dSgjelinek 		return (0);
1040b5de56dSgjelinek 	}
1050b5de56dSgjelinek 
10611506c41Sgjelinek 	/* First check if the dataset is mounted. */
10711506c41Sgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTED, mp, sizeof (mp), NULL, NULL,
10811506c41Sgjelinek 	    0, B_FALSE) != 0 || strcmp(mp, "no") == 0) {
10911506c41Sgjelinek 		zfs_close(zhp);
11011506c41Sgjelinek 		return (0);
11111506c41Sgjelinek 	}
11211506c41Sgjelinek 
11311506c41Sgjelinek 	/* Now check mount point. */
1140b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mp, sizeof (mp), NULL, NULL,
11511506c41Sgjelinek 	    0, B_FALSE) != 0) {
11611506c41Sgjelinek 		zfs_close(zhp);
11711506c41Sgjelinek 		return (0);
11811506c41Sgjelinek 	}
11911506c41Sgjelinek 
12011506c41Sgjelinek 	cbp = (zfs_mount_data_t *)data;
12111506c41Sgjelinek 
12211506c41Sgjelinek 	if (strcmp(mp, "legacy") == 0) {
12311506c41Sgjelinek 		/* If legacy, must look in mnttab for mountpoint. */
12411506c41Sgjelinek 		FILE		*fp;
12511506c41Sgjelinek 		struct mnttab	entry;
12611506c41Sgjelinek 		const char	*nm;
12711506c41Sgjelinek 
12811506c41Sgjelinek 		nm = zfs_get_name(zhp);
12911506c41Sgjelinek 		if ((fp = fopen(MNTTAB, "r")) == NULL) {
13011506c41Sgjelinek 			zfs_close(zhp);
13111506c41Sgjelinek 			return (0);
13211506c41Sgjelinek 		}
13311506c41Sgjelinek 
13411506c41Sgjelinek 		while (getmntent(fp, &entry) == 0) {
13511506c41Sgjelinek 			if (strcmp(nm, entry.mnt_special) == 0) {
13611506c41Sgjelinek 				if (strcmp(entry.mnt_mountp, cbp->match_name)
13711506c41Sgjelinek 				    == 0) {
13811506c41Sgjelinek 					(void) fclose(fp);
13911506c41Sgjelinek 					cbp->match_handle = zhp;
14011506c41Sgjelinek 					return (1);
14111506c41Sgjelinek 				}
14211506c41Sgjelinek 				break;
14311506c41Sgjelinek 			}
14411506c41Sgjelinek 		}
14511506c41Sgjelinek 		(void) fclose(fp);
14611506c41Sgjelinek 
14711506c41Sgjelinek 	} else if (strcmp(mp, cbp->match_name) == 0) {
1480b5de56dSgjelinek 		cbp->match_handle = zhp;
1490b5de56dSgjelinek 		return (1);
1500b5de56dSgjelinek 	}
1510b5de56dSgjelinek 
15211506c41Sgjelinek 	/* Iterate over any nested datasets. */
1530b5de56dSgjelinek 	res = zfs_iter_filesystems(zhp, match_mountpoint, data);
1540b5de56dSgjelinek 	zfs_close(zhp);
1550b5de56dSgjelinek 	return (res);
1560b5de56dSgjelinek }
1570b5de56dSgjelinek 
1580b5de56dSgjelinek /*
1590b5de56dSgjelinek  * Get ZFS handle for the specified mount point.
1600b5de56dSgjelinek  */
1610b5de56dSgjelinek static zfs_handle_t *
1620b5de56dSgjelinek mount2zhandle(char *mountpoint)
1630b5de56dSgjelinek {
1640b5de56dSgjelinek 	zfs_mount_data_t	cb;
1650b5de56dSgjelinek 
1660b5de56dSgjelinek 	cb.match_name = mountpoint;
1670b5de56dSgjelinek 	cb.match_handle = NULL;
16899653d4eSeschrock 	(void) zfs_iter_root(g_zfs, match_mountpoint, &cb);
1690b5de56dSgjelinek 	return (cb.match_handle);
1700b5de56dSgjelinek }
1710b5de56dSgjelinek 
1720b5de56dSgjelinek /*
1730b5de56dSgjelinek  * Check if there is already a file system (zfs or any other type) mounted on
1740b5de56dSgjelinek  * path.
1750b5de56dSgjelinek  */
1760b5de56dSgjelinek static boolean_t
1770b5de56dSgjelinek is_mountpnt(char *path)
1780b5de56dSgjelinek {
1790b5de56dSgjelinek 	FILE		*fp;
1800b5de56dSgjelinek 	struct mnttab	entry;
1810b5de56dSgjelinek 
18211506c41Sgjelinek 	if ((fp = fopen(MNTTAB, "r")) == NULL)
1830b5de56dSgjelinek 		return (B_FALSE);
1840b5de56dSgjelinek 
1850b5de56dSgjelinek 	while (getmntent(fp, &entry) == 0) {
1860b5de56dSgjelinek 		if (strcmp(path, entry.mnt_mountp) == 0) {
1870b5de56dSgjelinek 			(void) fclose(fp);
1880b5de56dSgjelinek 			return (B_TRUE);
1890b5de56dSgjelinek 		}
1900b5de56dSgjelinek 	}
1910b5de56dSgjelinek 
1920b5de56dSgjelinek 	(void) fclose(fp);
1930b5de56dSgjelinek 	return (B_FALSE);
1940b5de56dSgjelinek }
1950b5de56dSgjelinek 
1960b5de56dSgjelinek /*
197ff17c8bfSgjelinek  * Run the brand's pre-snapshot hook before we take a ZFS snapshot of the zone.
1980b5de56dSgjelinek  */
1990b5de56dSgjelinek static int
200ff17c8bfSgjelinek pre_snapshot(char *presnapbuf)
2010b5de56dSgjelinek {
202ff17c8bfSgjelinek 	int status;
2030b5de56dSgjelinek 
204ff17c8bfSgjelinek 	/* No brand-specific handler */
205ff17c8bfSgjelinek 	if (presnapbuf[0] == '\0')
206ff17c8bfSgjelinek 		return (Z_OK);
2070b5de56dSgjelinek 
208ff17c8bfSgjelinek 	/* Run the hook */
209*c75cc341S 	status = do_subproc(presnapbuf);
210ff17c8bfSgjelinek 	if ((status = subproc_status(gettext("brand-specific presnapshot"),
211ff17c8bfSgjelinek 	    status, B_FALSE)) != ZONE_SUBPROC_OK)
2120b5de56dSgjelinek 		return (Z_ERR);
2130b5de56dSgjelinek 
2140b5de56dSgjelinek 	return (Z_OK);
2150b5de56dSgjelinek }
2160b5de56dSgjelinek 
2170b5de56dSgjelinek /*
218ff17c8bfSgjelinek  * Run the brand's post-snapshot hook after we take a ZFS snapshot of the zone.
2190b5de56dSgjelinek  */
2200b5de56dSgjelinek static int
221ff17c8bfSgjelinek post_snapshot(char *postsnapbuf)
2220b5de56dSgjelinek {
223ff17c8bfSgjelinek 	int status;
2240b5de56dSgjelinek 
225ff17c8bfSgjelinek 	/* No brand-specific handler */
226ff17c8bfSgjelinek 	if (postsnapbuf[0] == '\0')
227ff17c8bfSgjelinek 		return (Z_OK);
2280b5de56dSgjelinek 
229ff17c8bfSgjelinek 	/* Run the hook */
230*c75cc341S 	status = do_subproc(postsnapbuf);
231ff17c8bfSgjelinek 	if ((status = subproc_status(gettext("brand-specific postsnapshot"),
232ff17c8bfSgjelinek 	    status, B_FALSE)) != ZONE_SUBPROC_OK)
2330b5de56dSgjelinek 		return (Z_ERR);
2340b5de56dSgjelinek 
2350b5de56dSgjelinek 	return (Z_OK);
2360b5de56dSgjelinek }
2370b5de56dSgjelinek 
2380b5de56dSgjelinek /*
2390b5de56dSgjelinek  * This is a ZFS snapshot iterator call-back function which returns the
2400b5de56dSgjelinek  * highest number of SUNWzone snapshots that have been taken.
2410b5de56dSgjelinek  */
2420b5de56dSgjelinek static int
2430b5de56dSgjelinek get_snap_max(zfs_handle_t *zhp, void *data)
2440b5de56dSgjelinek {
2450b5de56dSgjelinek 	int			res;
2460b5de56dSgjelinek 	zfs_snapshot_data_t	*cbp;
2470b5de56dSgjelinek 
2480b5de56dSgjelinek 	if (zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) {
2490b5de56dSgjelinek 		zfs_close(zhp);
2500b5de56dSgjelinek 		return (0);
2510b5de56dSgjelinek 	}
2520b5de56dSgjelinek 
2530b5de56dSgjelinek 	cbp = (zfs_snapshot_data_t *)data;
2540b5de56dSgjelinek 
2550b5de56dSgjelinek 	if (strncmp(zfs_get_name(zhp), cbp->match_name, cbp->len) == 0) {
2560b5de56dSgjelinek 		char	*nump;
2570b5de56dSgjelinek 		int	num;
2580b5de56dSgjelinek 
2590b5de56dSgjelinek 		nump = (char *)(zfs_get_name(zhp) + cbp->len);
2600b5de56dSgjelinek 		num = atoi(nump);
2610b5de56dSgjelinek 		if (num > cbp->max)
2620b5de56dSgjelinek 			cbp->max = num;
2630b5de56dSgjelinek 	}
2640b5de56dSgjelinek 
2650b5de56dSgjelinek 	res = zfs_iter_snapshots(zhp, get_snap_max, data);
2660b5de56dSgjelinek 	zfs_close(zhp);
2670b5de56dSgjelinek 	return (res);
2680b5de56dSgjelinek }
2690b5de56dSgjelinek 
2700b5de56dSgjelinek /*
2710b5de56dSgjelinek  * Take a ZFS snapshot to be used for cloning the zone.
2720b5de56dSgjelinek  */
2730b5de56dSgjelinek static int
274ff17c8bfSgjelinek take_snapshot(zfs_handle_t *zhp, char *snapshot_name, int snap_size,
275ff17c8bfSgjelinek     char *presnapbuf, char *postsnapbuf)
2760b5de56dSgjelinek {
2770b5de56dSgjelinek 	int			res;
2780b5de56dSgjelinek 	char			template[ZFS_MAXNAMELEN];
2790b5de56dSgjelinek 	zfs_snapshot_data_t	cb;
2800b5de56dSgjelinek 
2810b5de56dSgjelinek 	/*
2820b5de56dSgjelinek 	 * First we need to figure out the next available name for the
2830b5de56dSgjelinek 	 * zone snapshot.  Look through the list of zones snapshots for
2840b5de56dSgjelinek 	 * this file system to determine the maximum snapshot name.
2850b5de56dSgjelinek 	 */
2860b5de56dSgjelinek 	if (snprintf(template, sizeof (template), "%s@SUNWzone",
2870b5de56dSgjelinek 	    zfs_get_name(zhp)) >=  sizeof (template))
2880b5de56dSgjelinek 		return (Z_ERR);
2890b5de56dSgjelinek 
2900b5de56dSgjelinek 	cb.match_name = template;
2910b5de56dSgjelinek 	cb.len = strlen(template);
2920b5de56dSgjelinek 	cb.max = 0;
2930b5de56dSgjelinek 
2940b5de56dSgjelinek 	if (zfs_iter_snapshots(zhp, get_snap_max, &cb) != 0)
2950b5de56dSgjelinek 		return (Z_ERR);
2960b5de56dSgjelinek 
2970b5de56dSgjelinek 	cb.max++;
2980b5de56dSgjelinek 
2990b5de56dSgjelinek 	if (snprintf(snapshot_name, snap_size, "%s@SUNWzone%d",
3000b5de56dSgjelinek 	    zfs_get_name(zhp), cb.max) >= snap_size)
3010b5de56dSgjelinek 		return (Z_ERR);
3020b5de56dSgjelinek 
303ff17c8bfSgjelinek 	if (pre_snapshot(presnapbuf) != Z_OK)
3040b5de56dSgjelinek 		return (Z_ERR);
305bb0ade09Sahrens 	res = zfs_snapshot(g_zfs, snapshot_name, B_FALSE, NULL);
306ff17c8bfSgjelinek 	if (post_snapshot(postsnapbuf) != Z_OK)
3070b5de56dSgjelinek 		return (Z_ERR);
3080b5de56dSgjelinek 
3090b5de56dSgjelinek 	if (res != 0)
3100b5de56dSgjelinek 		return (Z_ERR);
3110b5de56dSgjelinek 	return (Z_OK);
3120b5de56dSgjelinek }
3130b5de56dSgjelinek 
3140b5de56dSgjelinek /*
3150b5de56dSgjelinek  * We are using an explicit snapshot from some earlier point in time so
316ff17c8bfSgjelinek  * we need to validate it.  Run the brand specific hook.
3170b5de56dSgjelinek  */
3180b5de56dSgjelinek static int
319ff17c8bfSgjelinek validate_snapshot(char *snapshot_name, char *snap_path, char *validsnapbuf)
3200b5de56dSgjelinek {
321ff17c8bfSgjelinek 	int status;
322ff17c8bfSgjelinek 	char cmdbuf[MAXPATHLEN];
3230b5de56dSgjelinek 
324ff17c8bfSgjelinek 	/* No brand-specific handler */
325ff17c8bfSgjelinek 	if (validsnapbuf[0] == '\0')
326ff17c8bfSgjelinek 		return (Z_OK);
3270b5de56dSgjelinek 
328ff17c8bfSgjelinek 	/* pass args - snapshot_name & snap_path */
329ff17c8bfSgjelinek 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %s %s", validsnapbuf,
330ff17c8bfSgjelinek 	    snapshot_name, snap_path) >= sizeof (cmdbuf)) {
331ff17c8bfSgjelinek 		zerror("Command line too long");
3320b5de56dSgjelinek 		return (Z_ERR);
3330b5de56dSgjelinek 	}
3340b5de56dSgjelinek 
335ff17c8bfSgjelinek 	/* Run the hook */
336*c75cc341S 	status = do_subproc(cmdbuf);
337ff17c8bfSgjelinek 	if ((status = subproc_status(gettext("brand-specific validatesnapshot"),
338ff17c8bfSgjelinek 	    status, B_FALSE)) != ZONE_SUBPROC_OK)
339ff17c8bfSgjelinek 		return (Z_ERR);
3400b5de56dSgjelinek 
341ff17c8bfSgjelinek 	return (Z_OK);
3420b5de56dSgjelinek }
3430b5de56dSgjelinek 
3440b5de56dSgjelinek /*
3450b5de56dSgjelinek  * Remove the sw inventory file from inside this zonepath that we picked up out
3460b5de56dSgjelinek  * of the snapshot.
3470b5de56dSgjelinek  */
3480b5de56dSgjelinek static int
3490b5de56dSgjelinek clean_out_clone()
3500b5de56dSgjelinek {
3510b5de56dSgjelinek 	int err;
3520b5de56dSgjelinek 	zone_dochandle_t handle;
3530b5de56dSgjelinek 
3540b5de56dSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
3550b5de56dSgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3560b5de56dSgjelinek 		return (Z_ERR);
3570b5de56dSgjelinek 	}
3580b5de56dSgjelinek 
3590b5de56dSgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
3600b5de56dSgjelinek 		errno = err;
3610b5de56dSgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3620b5de56dSgjelinek 		zonecfg_fini_handle(handle);
3630b5de56dSgjelinek 		return (Z_ERR);
3640b5de56dSgjelinek 	}
3650b5de56dSgjelinek 
3660b5de56dSgjelinek 	zonecfg_rm_detached(handle, B_FALSE);
3670b5de56dSgjelinek 	zonecfg_fini_handle(handle);
3680b5de56dSgjelinek 
3690b5de56dSgjelinek 	return (Z_OK);
3700b5de56dSgjelinek }
3710b5de56dSgjelinek 
3720b5de56dSgjelinek /*
3730b5de56dSgjelinek  * Make a ZFS clone on zonepath from snapshot_name.
3740b5de56dSgjelinek  */
3750b5de56dSgjelinek static int
3760b5de56dSgjelinek clone_snap(char *snapshot_name, char *zonepath)
3770b5de56dSgjelinek {
3780b5de56dSgjelinek 	int		res = Z_OK;
3790b5de56dSgjelinek 	int		err;
3800b5de56dSgjelinek 	zfs_handle_t	*zhp;
3810b5de56dSgjelinek 	zfs_handle_t	*clone;
382e9dbad6fSeschrock 	nvlist_t	*props = NULL;
3830b5de56dSgjelinek 
38499653d4eSeschrock 	if ((zhp = zfs_open(g_zfs, snapshot_name, ZFS_TYPE_SNAPSHOT)) == NULL)
3850b5de56dSgjelinek 		return (Z_NO_ENTRY);
3860b5de56dSgjelinek 
3870b5de56dSgjelinek 	(void) printf(gettext("Cloning snapshot %s\n"), snapshot_name);
3880b5de56dSgjelinek 
389e9dbad6fSeschrock 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0 ||
3905f8e1617Snn 	    nvlist_add_string(props, zfs_prop_to_name(ZFS_PROP_SHARENFS),
3915f8e1617Snn 	    "off") != 0) {
3925f8e1617Snn 		if (props != NULL)
3935f8e1617Snn 			nvlist_free(props);
394e9dbad6fSeschrock 		(void) fprintf(stderr, gettext("could not create ZFS clone "
395e9dbad6fSeschrock 		    "%s: out of memory\n"), zonepath);
396e9dbad6fSeschrock 		return (Z_ERR);
397e9dbad6fSeschrock 	}
398e9dbad6fSeschrock 
399e9dbad6fSeschrock 	err = zfs_clone(zhp, zonepath, props);
4000b5de56dSgjelinek 	zfs_close(zhp);
401e9dbad6fSeschrock 
402e9dbad6fSeschrock 	nvlist_free(props);
403e9dbad6fSeschrock 
4040b5de56dSgjelinek 	if (err != 0)
4050b5de56dSgjelinek 		return (Z_ERR);
4060b5de56dSgjelinek 
4070b5de56dSgjelinek 	/* create the mountpoint if necessary */
408990b4856Slling 	if ((clone = zfs_open(g_zfs, zonepath, ZFS_TYPE_DATASET)) == NULL)
4090b5de56dSgjelinek 		return (Z_ERR);
4100b5de56dSgjelinek 
4110b5de56dSgjelinek 	/*
4120b5de56dSgjelinek 	 * The clone has been created so we need to print a diagnostic
4130b5de56dSgjelinek 	 * message if one of the following steps fails for some reason.
4140b5de56dSgjelinek 	 */
4150b5de56dSgjelinek 	if (zfs_mount(clone, NULL, 0) != 0) {
4160b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not mount ZFS clone "
4170b5de56dSgjelinek 		    "%s\n"), zfs_get_name(clone));
4180b5de56dSgjelinek 		res = Z_ERR;
4190b5de56dSgjelinek 
420e9dbad6fSeschrock 	} else if (clean_out_clone() != Z_OK) {
421e9dbad6fSeschrock 		(void) fprintf(stderr, gettext("could not remove the "
422e9dbad6fSeschrock 		    "software inventory from ZFS clone %s\n"),
423e9dbad6fSeschrock 		    zfs_get_name(clone));
424e9dbad6fSeschrock 		res = Z_ERR;
4250b5de56dSgjelinek 	}
4260b5de56dSgjelinek 
4270b5de56dSgjelinek 	zfs_close(clone);
4280b5de56dSgjelinek 	return (res);
4290b5de56dSgjelinek }
4300b5de56dSgjelinek 
4310b5de56dSgjelinek /*
4320b5de56dSgjelinek  * This function takes a zonepath and attempts to determine what the ZFS
4330b5de56dSgjelinek  * file system name (not mountpoint) should be for that path.  We do not
4340b5de56dSgjelinek  * assume that zonepath is an existing directory or ZFS fs since we use
4350b5de56dSgjelinek  * this function as part of the process of creating a new ZFS fs or clone.
4360b5de56dSgjelinek  *
4370b5de56dSgjelinek  * The way this works is that we look at the parent directory of the zonepath
4380b5de56dSgjelinek  * to see if it is a ZFS fs.  If it is, we get the name of that ZFS fs and
4390b5de56dSgjelinek  * append the last component of the zonepath to generate the ZFS name for the
4400b5de56dSgjelinek  * zonepath.  This matches the algorithm that ZFS uses for automatically
4410b5de56dSgjelinek  * mounting a new fs after it is created.
4420b5de56dSgjelinek  *
4430b5de56dSgjelinek  * Although a ZFS fs can be mounted anywhere, we don't worry about handling
4440b5de56dSgjelinek  * all of the complexity that a user could possibly configure with arbitrary
4450b5de56dSgjelinek  * mounts since there is no way to generate a ZFS name from a random path in
4460b5de56dSgjelinek  * the file system.  We only try to handle the automatic mounts that ZFS does
4470b5de56dSgjelinek  * for each file system.  ZFS restricts this so that a new fs must be created
4480b5de56dSgjelinek  * in an existing parent ZFS fs.  It then automatically mounts the new fs
4490b5de56dSgjelinek  * directly under the mountpoint for the parent fs using the last component
4500b5de56dSgjelinek  * of the name as the mountpoint directory.
4510b5de56dSgjelinek  *
4520b5de56dSgjelinek  * For example:
4530b5de56dSgjelinek  *    Name			Mountpoint
4540b5de56dSgjelinek  *    space/eng/dev/test/zone1	/project1/eng/dev/test/zone1
4550b5de56dSgjelinek  *
4560b5de56dSgjelinek  * Return Z_OK if the path mapped to a ZFS file system name, otherwise return
4570b5de56dSgjelinek  * Z_ERR.
4580b5de56dSgjelinek  */
4590b5de56dSgjelinek static int
4600b5de56dSgjelinek path2name(char *zonepath, char *zfs_name, int len)
4610b5de56dSgjelinek {
4620b5de56dSgjelinek 	int		res;
46311506c41Sgjelinek 	char		*bnm, *dnm, *dname, *bname;
4640b5de56dSgjelinek 	zfs_handle_t	*zhp;
46511506c41Sgjelinek 	struct stat	stbuf;
46611506c41Sgjelinek 
46711506c41Sgjelinek 	/*
46811506c41Sgjelinek 	 * We need two tmp strings to handle paths directly in / (e.g. /foo)
46911506c41Sgjelinek 	 * since dirname will overwrite the first char after "/" in this case.
47011506c41Sgjelinek 	 */
47111506c41Sgjelinek 	if ((bnm = strdup(zonepath)) == NULL)
47211506c41Sgjelinek 		return (Z_ERR);
4730b5de56dSgjelinek 
47411506c41Sgjelinek 	if ((dnm = strdup(zonepath)) == NULL) {
47511506c41Sgjelinek 		free(bnm);
4760b5de56dSgjelinek 		return (Z_ERR);
47711506c41Sgjelinek 	}
47811506c41Sgjelinek 
47911506c41Sgjelinek 	bname = basename(bnm);
48011506c41Sgjelinek 	dname = dirname(dnm);
4810b5de56dSgjelinek 
4820b5de56dSgjelinek 	/*
48311506c41Sgjelinek 	 * This is a quick test to save iterating over all of the zfs datasets
48411506c41Sgjelinek 	 * on the system (which can be a lot).  If the parent dir is not in a
48511506c41Sgjelinek 	 * ZFS fs, then we're done.
4860b5de56dSgjelinek 	 */
48711506c41Sgjelinek 	if (stat(dname, &stbuf) != 0 || !S_ISDIR(stbuf.st_mode) ||
48811506c41Sgjelinek 	    strcmp(stbuf.st_fstype, MNTTYPE_ZFS) != 0) {
48911506c41Sgjelinek 		free(bnm);
49011506c41Sgjelinek 		free(dnm);
4910b5de56dSgjelinek 		return (Z_ERR);
49211506c41Sgjelinek 	}
49311506c41Sgjelinek 
49411506c41Sgjelinek 	/* See if the parent directory is its own ZFS dataset. */
49511506c41Sgjelinek 	if ((zhp = mount2zhandle(dname)) == NULL) {
49611506c41Sgjelinek 		/*
49711506c41Sgjelinek 		 * The parent is not a ZFS dataset so we can't automatically
49811506c41Sgjelinek 		 * create a dataset on the given path.
49911506c41Sgjelinek 		 */
50011506c41Sgjelinek 		free(bnm);
50111506c41Sgjelinek 		free(dnm);
50211506c41Sgjelinek 		return (Z_ERR);
50311506c41Sgjelinek 	}
5040b5de56dSgjelinek 
50511506c41Sgjelinek 	res = snprintf(zfs_name, len, "%s/%s", zfs_get_name(zhp), bname);
5060b5de56dSgjelinek 
50711506c41Sgjelinek 	free(bnm);
50811506c41Sgjelinek 	free(dnm);
5090b5de56dSgjelinek 	zfs_close(zhp);
5100b5de56dSgjelinek 	if (res >= len)
5110b5de56dSgjelinek 		return (Z_ERR);
5120b5de56dSgjelinek 
5130b5de56dSgjelinek 	return (Z_OK);
5140b5de56dSgjelinek }
5150b5de56dSgjelinek 
5160b5de56dSgjelinek /*
5170b5de56dSgjelinek  * A ZFS file system iterator call-back function used to determine if the
5180b5de56dSgjelinek  * file system has dependents (snapshots & clones).
5190b5de56dSgjelinek  */
5200b5de56dSgjelinek /* ARGSUSED */
5210b5de56dSgjelinek static int
5220b5de56dSgjelinek has_dependent(zfs_handle_t *zhp, void *data)
5230b5de56dSgjelinek {
5240b5de56dSgjelinek 	zfs_close(zhp);
5250b5de56dSgjelinek 	return (1);
5260b5de56dSgjelinek }
5270b5de56dSgjelinek 
5280b5de56dSgjelinek /*
5290b5de56dSgjelinek  * Given a snapshot name, get the file system path where the snapshot lives.
5300b5de56dSgjelinek  * A snapshot name is of the form fs_name@snap_name.  For example, snapshot
5310b5de56dSgjelinek  * pl/zones/z1@SUNWzone1 would have a path of
5320b5de56dSgjelinek  * /pl/zones/z1/.zfs/snapshot/SUNWzone1.
5330b5de56dSgjelinek  */
5340b5de56dSgjelinek static int
5350b5de56dSgjelinek snap2path(char *snap_name, char *path, int len)
5360b5de56dSgjelinek {
5370b5de56dSgjelinek 	char		*p;
5380b5de56dSgjelinek 	zfs_handle_t	*zhp;
5390b5de56dSgjelinek 	char		mp[ZFS_MAXPROPLEN];
5400b5de56dSgjelinek 
5410b5de56dSgjelinek 	if ((p = strrchr(snap_name, '@')) == NULL)
5420b5de56dSgjelinek 		return (Z_ERR);
5430b5de56dSgjelinek 
5440b5de56dSgjelinek 	/* Get the file system name from the snap_name. */
5450b5de56dSgjelinek 	*p = '\0';
546990b4856Slling 	zhp = zfs_open(g_zfs, snap_name, ZFS_TYPE_DATASET);
5470b5de56dSgjelinek 	*p = '@';
5480b5de56dSgjelinek 	if (zhp == NULL)
5490b5de56dSgjelinek 		return (Z_ERR);
5500b5de56dSgjelinek 
5510b5de56dSgjelinek 	/* Get the file system mount point. */
5520b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mp, sizeof (mp), NULL, NULL,
55399653d4eSeschrock 	    0, B_FALSE) != 0) {
5540b5de56dSgjelinek 		zfs_close(zhp);
5550b5de56dSgjelinek 		return (Z_ERR);
5560b5de56dSgjelinek 	}
5570b5de56dSgjelinek 	zfs_close(zhp);
5580b5de56dSgjelinek 
5590b5de56dSgjelinek 	p++;
5600b5de56dSgjelinek 	if (snprintf(path, len, "%s/.zfs/snapshot/%s", mp, p) >= len)
5610b5de56dSgjelinek 		return (Z_ERR);
5620b5de56dSgjelinek 
5630b5de56dSgjelinek 	return (Z_OK);
5640b5de56dSgjelinek }
5650b5de56dSgjelinek 
5660b5de56dSgjelinek /*
5670b5de56dSgjelinek  * Clone a pre-existing ZFS snapshot, either by making a direct ZFS clone, if
5680b5de56dSgjelinek  * possible, or by copying the data from the snapshot to the zonepath.
5690b5de56dSgjelinek  */
5700b5de56dSgjelinek int
571ff17c8bfSgjelinek clone_snapshot_zfs(char *snap_name, char *zonepath, char *validatesnap)
5720b5de56dSgjelinek {
5730b5de56dSgjelinek 	int	err = Z_OK;
5740b5de56dSgjelinek 	char	clone_name[MAXPATHLEN];
5750b5de56dSgjelinek 	char	snap_path[MAXPATHLEN];
5760b5de56dSgjelinek 
5770b5de56dSgjelinek 	if (snap2path(snap_name, snap_path, sizeof (snap_path)) != Z_OK) {
5780b5de56dSgjelinek 		(void) fprintf(stderr, gettext("unable to find path for %s.\n"),
5790b5de56dSgjelinek 		    snap_name);
5800b5de56dSgjelinek 		return (Z_ERR);
5810b5de56dSgjelinek 	}
5820b5de56dSgjelinek 
583ff17c8bfSgjelinek 	if (validate_snapshot(snap_name, snap_path, validatesnap) != Z_OK)
5840b5de56dSgjelinek 		return (Z_NO_ENTRY);
5850b5de56dSgjelinek 
5860b5de56dSgjelinek 	/*
5870b5de56dSgjelinek 	 * The zonepath cannot be ZFS cloned, try to copy the data from
5880b5de56dSgjelinek 	 * within the snapshot to the zonepath.
5890b5de56dSgjelinek 	 */
5900b5de56dSgjelinek 	if (path2name(zonepath, clone_name, sizeof (clone_name)) != Z_OK) {
5910b5de56dSgjelinek 		if ((err = clone_copy(snap_path, zonepath)) == Z_OK)
5920b5de56dSgjelinek 			if (clean_out_clone() != Z_OK)
5930b5de56dSgjelinek 				(void) fprintf(stderr,
5940b5de56dSgjelinek 				    gettext("could not remove the "
5950b5de56dSgjelinek 				    "software inventory from %s\n"), zonepath);
5960b5de56dSgjelinek 
5970b5de56dSgjelinek 		return (err);
5980b5de56dSgjelinek 	}
5990b5de56dSgjelinek 
6000b5de56dSgjelinek 	if ((err = clone_snap(snap_name, clone_name)) != Z_OK) {
6010b5de56dSgjelinek 		if (err != Z_NO_ENTRY) {
6020b5de56dSgjelinek 			/*
6030b5de56dSgjelinek 			 * Cloning the snapshot failed.  Fall back to trying
6040b5de56dSgjelinek 			 * to install the zone by copying from the snapshot.
6050b5de56dSgjelinek 			 */
6060b5de56dSgjelinek 			if ((err = clone_copy(snap_path, zonepath)) == Z_OK)
6070b5de56dSgjelinek 				if (clean_out_clone() != Z_OK)
6080b5de56dSgjelinek 					(void) fprintf(stderr,
6090b5de56dSgjelinek 					    gettext("could not remove the "
6100b5de56dSgjelinek 					    "software inventory from %s\n"),
6110b5de56dSgjelinek 					    zonepath);
6120b5de56dSgjelinek 		} else {
6130b5de56dSgjelinek 			/*
6140b5de56dSgjelinek 			 * The snapshot is unusable for some reason so restore
6150b5de56dSgjelinek 			 * the zone state to configured since we were unable to
6160b5de56dSgjelinek 			 * actually do anything about getting the zone
6170b5de56dSgjelinek 			 * installed.
6180b5de56dSgjelinek 			 */
6190b5de56dSgjelinek 			int tmp;
6200b5de56dSgjelinek 
6210b5de56dSgjelinek 			if ((tmp = zone_set_state(target_zone,
6220b5de56dSgjelinek 			    ZONE_STATE_CONFIGURED)) != Z_OK) {
6230b5de56dSgjelinek 				errno = tmp;
6240b5de56dSgjelinek 				zperror2(target_zone,
6250b5de56dSgjelinek 				    gettext("could not set state"));
6260b5de56dSgjelinek 			}
6270b5de56dSgjelinek 		}
6280b5de56dSgjelinek 	}
6290b5de56dSgjelinek 
6300b5de56dSgjelinek 	return (err);
6310b5de56dSgjelinek }
6320b5de56dSgjelinek 
6330b5de56dSgjelinek /*
6340b5de56dSgjelinek  * Attempt to clone a source_zone to a target zonepath by using a ZFS clone.
6350b5de56dSgjelinek  */
6360b5de56dSgjelinek int
637ff17c8bfSgjelinek clone_zfs(char *source_zonepath, char *zonepath, char *presnapbuf,
638ff17c8bfSgjelinek     char *postsnapbuf)
6390b5de56dSgjelinek {
6400b5de56dSgjelinek 	zfs_handle_t	*zhp;
6410b5de56dSgjelinek 	char		clone_name[MAXPATHLEN];
6420b5de56dSgjelinek 	char		snap_name[MAXPATHLEN];
6430b5de56dSgjelinek 
6440b5de56dSgjelinek 	/*
6450b5de56dSgjelinek 	 * Try to get a zfs handle for the source_zonepath.  If this fails
6460b5de56dSgjelinek 	 * the source_zonepath is not ZFS so return an error.
6470b5de56dSgjelinek 	 */
6480b5de56dSgjelinek 	if ((zhp = mount2zhandle(source_zonepath)) == NULL)
6490b5de56dSgjelinek 		return (Z_ERR);
6500b5de56dSgjelinek 
6510b5de56dSgjelinek 	/*
6520b5de56dSgjelinek 	 * Check if there is a file system already mounted on zonepath.  If so,
6530b5de56dSgjelinek 	 * we can't clone to the path so we should fall back to copying.
6540b5de56dSgjelinek 	 */
6550b5de56dSgjelinek 	if (is_mountpnt(zonepath)) {
6560b5de56dSgjelinek 		zfs_close(zhp);
6570b5de56dSgjelinek 		(void) fprintf(stderr,
6580b5de56dSgjelinek 		    gettext("A file system is already mounted on %s,\n"
6590b5de56dSgjelinek 		    "preventing use of a ZFS clone.\n"), zonepath);
6600b5de56dSgjelinek 		return (Z_ERR);
6610b5de56dSgjelinek 	}
6620b5de56dSgjelinek 
6630b5de56dSgjelinek 	/*
6640b5de56dSgjelinek 	 * Instead of using path2name to get the clone name from the zonepath,
6650b5de56dSgjelinek 	 * we could generate a name from the source zone ZFS name.  However,
6660b5de56dSgjelinek 	 * this would mean we would create the clone under the ZFS fs of the
6670b5de56dSgjelinek 	 * source instead of what the zonepath says.  For example,
6680b5de56dSgjelinek 	 *
6690b5de56dSgjelinek 	 * source_zonepath		zonepath
6700b5de56dSgjelinek 	 * /pl/zones/dev/z1		/pl/zones/deploy/z2
6710b5de56dSgjelinek 	 *
6720b5de56dSgjelinek 	 * We don't want the clone to be under "dev", we want it under
6730b5de56dSgjelinek 	 * "deploy", so that we can leverage the normal attribute inheritance
6740b5de56dSgjelinek 	 * that ZFS provides in the fs hierarchy.
6750b5de56dSgjelinek 	 */
6760b5de56dSgjelinek 	if (path2name(zonepath, clone_name, sizeof (clone_name)) != Z_OK) {
6770b5de56dSgjelinek 		zfs_close(zhp);
6780b5de56dSgjelinek 		return (Z_ERR);
6790b5de56dSgjelinek 	}
6800b5de56dSgjelinek 
681ff17c8bfSgjelinek 	if (take_snapshot(zhp, snap_name, sizeof (snap_name), presnapbuf,
682ff17c8bfSgjelinek 	    postsnapbuf) != Z_OK) {
6830b5de56dSgjelinek 		zfs_close(zhp);
6840b5de56dSgjelinek 		return (Z_ERR);
6850b5de56dSgjelinek 	}
6860b5de56dSgjelinek 	zfs_close(zhp);
6870b5de56dSgjelinek 
688d9e728a2Sgjelinek 	if (clone_snap(snap_name, clone_name) != Z_OK) {
689d9e728a2Sgjelinek 		/* Clean up the snapshot we just took. */
690d9e728a2Sgjelinek 		if ((zhp = zfs_open(g_zfs, snap_name, ZFS_TYPE_SNAPSHOT))
691d9e728a2Sgjelinek 		    != NULL) {
692d9e728a2Sgjelinek 			if (zfs_unmount(zhp, NULL, 0) == 0)
693d9e728a2Sgjelinek 				(void) zfs_destroy(zhp);
694d9e728a2Sgjelinek 			zfs_close(zhp);
695d9e728a2Sgjelinek 		}
696d9e728a2Sgjelinek 
6970b5de56dSgjelinek 		return (Z_ERR);
698d9e728a2Sgjelinek 	}
6990b5de56dSgjelinek 
7000b5de56dSgjelinek 	(void) printf(gettext("Instead of copying, a ZFS clone has been "
7010b5de56dSgjelinek 	    "created for this zone.\n"));
7020b5de56dSgjelinek 
7030b5de56dSgjelinek 	return (Z_OK);
7040b5de56dSgjelinek }
7050b5de56dSgjelinek 
7060b5de56dSgjelinek /*
7070b5de56dSgjelinek  * Attempt to create a ZFS file system for the specified zonepath.
7080b5de56dSgjelinek  * We either will successfully create a ZFS file system and get it mounted
7090b5de56dSgjelinek  * on the zonepath or we don't.  The caller doesn't care since a regular
7100b5de56dSgjelinek  * directory is used for the zonepath if no ZFS file system is mounted there.
7110b5de56dSgjelinek  */
7120b5de56dSgjelinek void
7130b5de56dSgjelinek create_zfs_zonepath(char *zonepath)
7140b5de56dSgjelinek {
7150b5de56dSgjelinek 	zfs_handle_t	*zhp;
7160b5de56dSgjelinek 	char		zfs_name[MAXPATHLEN];
717e9dbad6fSeschrock 	nvlist_t	*props = NULL;
7180b5de56dSgjelinek 
7190b5de56dSgjelinek 	if (path2name(zonepath, zfs_name, sizeof (zfs_name)) != Z_OK)
7200b5de56dSgjelinek 		return;
7210b5de56dSgjelinek 
722d1f855d7S 	/* Check if the dataset already exists. */
723d1f855d7S 	if ((zhp = zfs_open(g_zfs, zfs_name, ZFS_TYPE_DATASET)) != NULL) {
724d1f855d7S 		zfs_close(zhp);
725d1f855d7S 		return;
726d1f855d7S 	}
727d1f855d7S 
728e9dbad6fSeschrock 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0 ||
7295f8e1617Snn 	    nvlist_add_string(props, zfs_prop_to_name(ZFS_PROP_SHARENFS),
7305f8e1617Snn 	    "off") != 0) {
7315f8e1617Snn 		if (props != NULL)
7325f8e1617Snn 			nvlist_free(props);
733e9dbad6fSeschrock 		(void) fprintf(stderr, gettext("cannot create ZFS dataset %s: "
734e9dbad6fSeschrock 		    "out of memory\n"), zfs_name);
735e9dbad6fSeschrock 	}
736e9dbad6fSeschrock 
737e9dbad6fSeschrock 	if (zfs_create(g_zfs, zfs_name, ZFS_TYPE_FILESYSTEM, props) != 0 ||
738990b4856Slling 	    (zhp = zfs_open(g_zfs, zfs_name, ZFS_TYPE_DATASET)) == NULL) {
73999653d4eSeschrock 		(void) fprintf(stderr, gettext("cannot create ZFS dataset %s: "
74099653d4eSeschrock 		    "%s\n"), zfs_name, libzfs_error_description(g_zfs));
741e9dbad6fSeschrock 		nvlist_free(props);
7420b5de56dSgjelinek 		return;
7430b5de56dSgjelinek 	}
7440b5de56dSgjelinek 
745e9dbad6fSeschrock 	nvlist_free(props);
746e9dbad6fSeschrock 
7470b5de56dSgjelinek 	if (zfs_mount(zhp, NULL, 0) != 0) {
74899653d4eSeschrock 		(void) fprintf(stderr, gettext("cannot mount ZFS dataset %s: "
74999653d4eSeschrock 		    "%s\n"), zfs_name, libzfs_error_description(g_zfs));
7500b5de56dSgjelinek 		(void) zfs_destroy(zhp);
7510b5de56dSgjelinek 	} else {
7520b5de56dSgjelinek 		if (chmod(zonepath, S_IRWXU) != 0) {
7530b5de56dSgjelinek 			(void) fprintf(stderr, gettext("file system %s "
7540b5de56dSgjelinek 			    "successfully created, but chmod %o failed: %s\n"),
7550b5de56dSgjelinek 			    zfs_name, S_IRWXU, strerror(errno));
7560b5de56dSgjelinek 			(void) destroy_zfs(zonepath);
7570b5de56dSgjelinek 		} else {
7580b5de56dSgjelinek 			(void) printf(gettext("A ZFS file system has been "
7590b5de56dSgjelinek 			    "created for this zone.\n"));
7600b5de56dSgjelinek 		}
7610b5de56dSgjelinek 	}
7620b5de56dSgjelinek 
7630b5de56dSgjelinek 	zfs_close(zhp);
7640b5de56dSgjelinek }
7650b5de56dSgjelinek 
7660b5de56dSgjelinek /*
7670b5de56dSgjelinek  * If the zonepath is a ZFS file system, attempt to destroy it.  We return Z_OK
7680b5de56dSgjelinek  * if we were able to zfs_destroy the zonepath, otherwise we return Z_ERR
7690b5de56dSgjelinek  * which means the caller should clean up the zonepath in the traditional
7700b5de56dSgjelinek  * way.
7710b5de56dSgjelinek  */
7720b5de56dSgjelinek int
7730b5de56dSgjelinek destroy_zfs(char *zonepath)
7740b5de56dSgjelinek {
7750b5de56dSgjelinek 	zfs_handle_t	*zhp;
7760b5de56dSgjelinek 	boolean_t	is_clone = B_FALSE;
7770b5de56dSgjelinek 	char		origin[ZFS_MAXPROPLEN];
7780b5de56dSgjelinek 
77999653d4eSeschrock 	if ((zhp = mount2zhandle(zonepath)) == NULL)
7800b5de56dSgjelinek 		return (Z_ERR);
7810b5de56dSgjelinek 
7820b5de56dSgjelinek 	/*
7830b5de56dSgjelinek 	 * We can't destroy the file system if it has dependents.
7840b5de56dSgjelinek 	 */
7853bb79becSeschrock 	if (zfs_iter_dependents(zhp, B_TRUE, has_dependent, NULL) != 0 ||
7860b5de56dSgjelinek 	    zfs_unmount(zhp, NULL, 0) != 0) {
7870b5de56dSgjelinek 		zfs_close(zhp);
7880b5de56dSgjelinek 		return (Z_ERR);
7890b5de56dSgjelinek 	}
7900b5de56dSgjelinek 
7910b5de56dSgjelinek 	/*
7920b5de56dSgjelinek 	 * This might be a clone.  Try to get the snapshot so we can attempt
7930b5de56dSgjelinek 	 * to destroy that as well.
7940b5de56dSgjelinek 	 */
7950b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin, sizeof (origin), NULL,
79699653d4eSeschrock 	    NULL, 0, B_FALSE) == 0)
7970b5de56dSgjelinek 		is_clone = B_TRUE;
7980b5de56dSgjelinek 
7990b5de56dSgjelinek 	if (zfs_destroy(zhp) != 0) {
8000b5de56dSgjelinek 		/*
8010b5de56dSgjelinek 		 * If the destroy fails for some reason, try to remount
8020b5de56dSgjelinek 		 * the file system so that we can use "rm -rf" to clean up
8030b5de56dSgjelinek 		 * instead.
8040b5de56dSgjelinek 		 */
8050b5de56dSgjelinek 		(void) zfs_mount(zhp, NULL, 0);
8060b5de56dSgjelinek 		zfs_close(zhp);
8070b5de56dSgjelinek 		return (Z_ERR);
8080b5de56dSgjelinek 	}
8090b5de56dSgjelinek 
810d9e728a2Sgjelinek 	/*
811d9e728a2Sgjelinek 	 * If the zone has ever been moved then the mountpoint dir will not be
812d9e728a2Sgjelinek 	 * cleaned up by the zfs_destroy().  To handle this case try to clean
813d9e728a2Sgjelinek 	 * it up now but don't worry if it fails, that will be normal.
814d9e728a2Sgjelinek 	 */
815d9e728a2Sgjelinek 	(void) rmdir(zonepath);
816d9e728a2Sgjelinek 
8170b5de56dSgjelinek 	(void) printf(gettext("The ZFS file system for this zone has been "
8180b5de56dSgjelinek 	    "destroyed.\n"));
8190b5de56dSgjelinek 
8200b5de56dSgjelinek 	if (is_clone) {
8210b5de56dSgjelinek 		zfs_handle_t	*ohp;
8220b5de56dSgjelinek 
8230b5de56dSgjelinek 		/*
8240b5de56dSgjelinek 		 * Try to clean up the snapshot that the clone was taken from.
8250b5de56dSgjelinek 		 */
82699653d4eSeschrock 		if ((ohp = zfs_open(g_zfs, origin,
82799653d4eSeschrock 		    ZFS_TYPE_SNAPSHOT)) != NULL) {
8283bb79becSeschrock 			if (zfs_iter_dependents(ohp, B_TRUE, has_dependent,
8293bb79becSeschrock 			    NULL) == 0 && zfs_unmount(ohp, NULL, 0) == 0)
8300b5de56dSgjelinek 				(void) zfs_destroy(ohp);
8310b5de56dSgjelinek 			zfs_close(ohp);
8320b5de56dSgjelinek 		}
8330b5de56dSgjelinek 	}
8340b5de56dSgjelinek 
8350b5de56dSgjelinek 	zfs_close(zhp);
8360b5de56dSgjelinek 	return (Z_OK);
8370b5de56dSgjelinek }
8380b5de56dSgjelinek 
8390b5de56dSgjelinek /*
8400b5de56dSgjelinek  * Return true if the path is its own zfs file system.  We determine this
8410b5de56dSgjelinek  * by stat-ing the path to see if it is zfs and stat-ing the parent to see
8420b5de56dSgjelinek  * if it is a different fs.
8430b5de56dSgjelinek  */
8440b5de56dSgjelinek boolean_t
8450b5de56dSgjelinek is_zonepath_zfs(char *zonepath)
8460b5de56dSgjelinek {
8470b5de56dSgjelinek 	int res;
8480b5de56dSgjelinek 	char *path;
8490b5de56dSgjelinek 	char *parent;
8503f2f09c1Sdp 	struct statvfs64 buf1, buf2;
8510b5de56dSgjelinek 
8523f2f09c1Sdp 	if (statvfs64(zonepath, &buf1) != 0)
8530b5de56dSgjelinek 		return (B_FALSE);
8540b5de56dSgjelinek 
8550b5de56dSgjelinek 	if (strcmp(buf1.f_basetype, "zfs") != 0)
8560b5de56dSgjelinek 		return (B_FALSE);
8570b5de56dSgjelinek 
8580b5de56dSgjelinek 	if ((path = strdup(zonepath)) == NULL)
8590b5de56dSgjelinek 		return (B_FALSE);
8600b5de56dSgjelinek 
8610b5de56dSgjelinek 	parent = dirname(path);
8623f2f09c1Sdp 	res = statvfs64(parent, &buf2);
8630b5de56dSgjelinek 	free(path);
8640b5de56dSgjelinek 
8650b5de56dSgjelinek 	if (res != 0)
8660b5de56dSgjelinek 		return (B_FALSE);
8670b5de56dSgjelinek 
8680b5de56dSgjelinek 	if (buf1.f_fsid == buf2.f_fsid)
8690b5de56dSgjelinek 		return (B_FALSE);
8700b5de56dSgjelinek 
8710b5de56dSgjelinek 	return (B_TRUE);
8720b5de56dSgjelinek }
8730b5de56dSgjelinek 
8740b5de56dSgjelinek /*
8750b5de56dSgjelinek  * Implement the fast move of a ZFS file system by simply updating the
8760b5de56dSgjelinek  * mountpoint.  Since it is file system already, we don't have the
8770b5de56dSgjelinek  * issue of cross-file system copying.
8780b5de56dSgjelinek  */
8790b5de56dSgjelinek int
8800b5de56dSgjelinek move_zfs(char *zonepath, char *new_zonepath)
8810b5de56dSgjelinek {
8820b5de56dSgjelinek 	int		ret = Z_ERR;
8830b5de56dSgjelinek 	zfs_handle_t	*zhp;
8840b5de56dSgjelinek 
88599653d4eSeschrock 	if ((zhp = mount2zhandle(zonepath)) == NULL)
8860b5de56dSgjelinek 		return (Z_ERR);
8870b5de56dSgjelinek 
888e9dbad6fSeschrock 	if (zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
889e9dbad6fSeschrock 	    new_zonepath) == 0) {
8900b5de56dSgjelinek 		/*
8910b5de56dSgjelinek 		 * Clean up the old mount point.  We ignore any failure since
8920b5de56dSgjelinek 		 * the zone is already successfully mounted on the new path.
8930b5de56dSgjelinek 		 */
8940b5de56dSgjelinek 		(void) rmdir(zonepath);
8950b5de56dSgjelinek 		ret = Z_OK;
8960b5de56dSgjelinek 	}
8970b5de56dSgjelinek 
8980b5de56dSgjelinek 	zfs_close(zhp);
8990b5de56dSgjelinek 
9000b5de56dSgjelinek 	return (ret);
9010b5de56dSgjelinek }
9020b5de56dSgjelinek 
9030b5de56dSgjelinek /*
9040b5de56dSgjelinek  * Validate that the given dataset exists on the system, and that neither it nor
9050b5de56dSgjelinek  * its children are zvols.
9060b5de56dSgjelinek  *
9070b5de56dSgjelinek  * Note that we don't do anything with the 'zoned' property here.  All
9080b5de56dSgjelinek  * management is done in zoneadmd when the zone is actually rebooted.  This
9090b5de56dSgjelinek  * allows us to automatically set the zoned property even when a zone is
9100b5de56dSgjelinek  * rebooted by the administrator.
9110b5de56dSgjelinek  */
9120b5de56dSgjelinek int
9130b5de56dSgjelinek verify_datasets(zone_dochandle_t handle)
9140b5de56dSgjelinek {
9150b5de56dSgjelinek 	int return_code = Z_OK;
9160b5de56dSgjelinek 	struct zone_dstab dstab;
9170b5de56dSgjelinek 	zfs_handle_t *zhp;
9180b5de56dSgjelinek 	char propbuf[ZFS_MAXPROPLEN];
9190b5de56dSgjelinek 	char source[ZFS_MAXNAMELEN];
920990b4856Slling 	zprop_source_t srctype;
9210b5de56dSgjelinek 
9220b5de56dSgjelinek 	if (zonecfg_setdsent(handle) != Z_OK) {
9230b5de56dSgjelinek 		/*
9240b5de56dSgjelinek 		 * TRANSLATION_NOTE
9250b5de56dSgjelinek 		 * zfs and dataset are literals that should not be translated.
9260b5de56dSgjelinek 		 */
9270b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify zfs datasets: "
9280b5de56dSgjelinek 		    "unable to enumerate datasets\n"));
9290b5de56dSgjelinek 		return (Z_ERR);
9300b5de56dSgjelinek 	}
9310b5de56dSgjelinek 
9320b5de56dSgjelinek 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
9330b5de56dSgjelinek 
93499653d4eSeschrock 		if ((zhp = zfs_open(g_zfs, dstab.zone_dataset_name,
9350b5de56dSgjelinek 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
93699653d4eSeschrock 			(void) fprintf(stderr, gettext("could not verify zfs "
93799653d4eSeschrock 			    "dataset %s: %s\n"), dstab.zone_dataset_name,
93899653d4eSeschrock 			    libzfs_error_description(g_zfs));
9390b5de56dSgjelinek 			return_code = Z_ERR;
9400b5de56dSgjelinek 			continue;
9410b5de56dSgjelinek 		}
9420b5de56dSgjelinek 
9430b5de56dSgjelinek 		if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf,
9440b5de56dSgjelinek 		    sizeof (propbuf), &srctype, source,
9450b5de56dSgjelinek 		    sizeof (source), 0) == 0 &&
946990b4856Slling 		    (srctype == ZPROP_SRC_INHERITED)) {
9470b5de56dSgjelinek 			(void) fprintf(stderr, gettext("could not verify zfs "
9480b5de56dSgjelinek 			    "dataset %s: mountpoint cannot be inherited\n"),
9490b5de56dSgjelinek 			    dstab.zone_dataset_name);
9500b5de56dSgjelinek 			return_code = Z_ERR;
9510b5de56dSgjelinek 			zfs_close(zhp);
9520b5de56dSgjelinek 			continue;
9530b5de56dSgjelinek 		}
9540b5de56dSgjelinek 
9550b5de56dSgjelinek 		if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
9560b5de56dSgjelinek 			(void) fprintf(stderr, gettext("cannot verify zfs "
9570b5de56dSgjelinek 			    "dataset %s: volumes cannot be specified as a "
9580b5de56dSgjelinek 			    "zone dataset resource\n"),
9590b5de56dSgjelinek 			    dstab.zone_dataset_name);
9600b5de56dSgjelinek 			return_code = Z_ERR;
9610b5de56dSgjelinek 		}
9620b5de56dSgjelinek 
9630b5de56dSgjelinek 		if (zfs_iter_children(zhp, check_zvol, NULL) != 0)
9640b5de56dSgjelinek 			return_code = Z_ERR;
9650b5de56dSgjelinek 
9660b5de56dSgjelinek 		zfs_close(zhp);
9670b5de56dSgjelinek 	}
9680b5de56dSgjelinek 	(void) zonecfg_enddsent(handle);
9690b5de56dSgjelinek 
9700b5de56dSgjelinek 	return (return_code);
9710b5de56dSgjelinek }
9720b5de56dSgjelinek 
9730b5de56dSgjelinek /*
9740b5de56dSgjelinek  * Verify that the ZFS dataset exists, and its mountpoint
9750b5de56dSgjelinek  * property is set to "legacy".
9760b5de56dSgjelinek  */
9770b5de56dSgjelinek int
9780b5de56dSgjelinek verify_fs_zfs(struct zone_fstab *fstab)
9790b5de56dSgjelinek {
9800b5de56dSgjelinek 	zfs_handle_t *zhp;
9810b5de56dSgjelinek 	char propbuf[ZFS_MAXPROPLEN];
9820b5de56dSgjelinek 
98399653d4eSeschrock 	if ((zhp = zfs_open(g_zfs, fstab->zone_fs_special,
984990b4856Slling 	    ZFS_TYPE_DATASET)) == NULL) {
9850b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify fs %s: "
9860b5de56dSgjelinek 		    "could not access zfs dataset '%s'\n"),
9870b5de56dSgjelinek 		    fstab->zone_fs_dir, fstab->zone_fs_special);
9880b5de56dSgjelinek 		return (Z_ERR);
9890b5de56dSgjelinek 	}
9900b5de56dSgjelinek 
9910b5de56dSgjelinek 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
9920b5de56dSgjelinek 		(void) fprintf(stderr, gettext("cannot verify fs %s: "
9930b5de56dSgjelinek 		    "'%s' is not a file system\n"),
9940b5de56dSgjelinek 		    fstab->zone_fs_dir, fstab->zone_fs_special);
9950b5de56dSgjelinek 		zfs_close(zhp);
9960b5de56dSgjelinek 		return (Z_ERR);
9970b5de56dSgjelinek 	}
9980b5de56dSgjelinek 
9990b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, sizeof (propbuf),
10000b5de56dSgjelinek 	    NULL, NULL, 0, 0) != 0 || strcmp(propbuf, "legacy") != 0) {
10010b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify fs %s: "
10020b5de56dSgjelinek 		    "zfs '%s' mountpoint is not \"legacy\"\n"),
10030b5de56dSgjelinek 		    fstab->zone_fs_dir, fstab->zone_fs_special);
10040b5de56dSgjelinek 		zfs_close(zhp);
10050b5de56dSgjelinek 		return (Z_ERR);
10060b5de56dSgjelinek 	}
10070b5de56dSgjelinek 
10080b5de56dSgjelinek 	zfs_close(zhp);
100999653d4eSeschrock 	return (Z_OK);
101099653d4eSeschrock }
101199653d4eSeschrock 
101299653d4eSeschrock int
101399653d4eSeschrock init_zfs(void)
101499653d4eSeschrock {
101599653d4eSeschrock 	if ((g_zfs = libzfs_init()) == NULL) {
101699653d4eSeschrock 		(void) fprintf(stderr, gettext("failed to initialize ZFS "
101799653d4eSeschrock 		    "library\n"));
101899653d4eSeschrock 		return (Z_ERR);
101999653d4eSeschrock 	}
102099653d4eSeschrock 
10210b5de56dSgjelinek 	return (Z_OK);
10220b5de56dSgjelinek }
1023