xref: /illumos-gate/usr/src/cmd/zoneadm/zfs.c (revision 6a9cb0ea17f11529b1bb8ca31944abd37736b99e)
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 /*
232b6c28b8Sbatschul  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24*6a9cb0eaSEric Schrock  * Copyright (c) 2012 by Delphix. All rights reserved.
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>
470094b373Sjv #include <strings.h>
480094b373Sjv #include <assert.h>
490b5de56dSgjelinek 
500b5de56dSgjelinek #include "zoneadm.h"
510b5de56dSgjelinek 
5299653d4eSeschrock libzfs_handle_t *g_zfs;
530b5de56dSgjelinek 
540b5de56dSgjelinek typedef struct zfs_mount_data {
550b5de56dSgjelinek 	char		*match_name;
560b5de56dSgjelinek 	zfs_handle_t	*match_handle;
570b5de56dSgjelinek } zfs_mount_data_t;
580b5de56dSgjelinek 
590b5de56dSgjelinek typedef struct zfs_snapshot_data {
60286822ddS 	char	*match_name;	/* zonename@SUNWzone */
61286822ddS 	int	len;		/* strlen of match_name */
62286822ddS 	int	max;		/* highest digit appended to snap name */
63286822ddS 	int	num;		/* number of snapshots to rename */
64286822ddS 	int	cntr;		/* counter for renaming snapshots */
650b5de56dSgjelinek } zfs_snapshot_data_t;
660b5de56dSgjelinek 
67286822ddS typedef struct clone_data {
68286822ddS 	zfs_handle_t	*clone_zhp;	/* clone dataset to promote */
69286822ddS 	time_t		origin_creation; /* snapshot creation time of clone */
70286822ddS 	const char	*snapshot;	/* snapshot of dataset being demoted */
71286822ddS } clone_data_t;
72286822ddS 
730b5de56dSgjelinek /*
740b5de56dSgjelinek  * A ZFS file system iterator call-back function which is used to validate
750b5de56dSgjelinek  * datasets imported into the zone.
760b5de56dSgjelinek  */
770b5de56dSgjelinek /* ARGSUSED */
780b5de56dSgjelinek static int
790b5de56dSgjelinek check_zvol(zfs_handle_t *zhp, void *unused)
800b5de56dSgjelinek {
810b5de56dSgjelinek 	int ret;
820b5de56dSgjelinek 
830b5de56dSgjelinek 	if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
840b5de56dSgjelinek 		/*
850b5de56dSgjelinek 		 * TRANSLATION_NOTE
860b5de56dSgjelinek 		 * zfs and dataset are literals that should not be translated.
870b5de56dSgjelinek 		 */
880b5de56dSgjelinek 		(void) fprintf(stderr, gettext("cannot verify zfs dataset %s: "
890b5de56dSgjelinek 		    "volumes cannot be specified as a zone dataset resource\n"),
900b5de56dSgjelinek 		    zfs_get_name(zhp));
910b5de56dSgjelinek 		ret = -1;
920b5de56dSgjelinek 	} else {
930b5de56dSgjelinek 		ret = zfs_iter_children(zhp, check_zvol, NULL);
940b5de56dSgjelinek 	}
950b5de56dSgjelinek 
960b5de56dSgjelinek 	zfs_close(zhp);
970b5de56dSgjelinek 
980b5de56dSgjelinek 	return (ret);
990b5de56dSgjelinek }
1000b5de56dSgjelinek 
1010b5de56dSgjelinek /*
1020b5de56dSgjelinek  * A ZFS file system iterator call-back function which returns the
1030b5de56dSgjelinek  * zfs_handle_t for a ZFS file system on the specified mount point.
1040b5de56dSgjelinek  */
1050b5de56dSgjelinek static int
1060b5de56dSgjelinek match_mountpoint(zfs_handle_t *zhp, void *data)
1070b5de56dSgjelinek {
1080b5de56dSgjelinek 	int			res;
1090b5de56dSgjelinek 	zfs_mount_data_t	*cbp;
1100b5de56dSgjelinek 	char			mp[ZFS_MAXPROPLEN];
1110b5de56dSgjelinek 
1120b5de56dSgjelinek 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
1130b5de56dSgjelinek 		zfs_close(zhp);
1140b5de56dSgjelinek 		return (0);
1150b5de56dSgjelinek 	}
1160b5de56dSgjelinek 
11711506c41Sgjelinek 	/* First check if the dataset is mounted. */
11811506c41Sgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTED, mp, sizeof (mp), NULL, NULL,
11911506c41Sgjelinek 	    0, B_FALSE) != 0 || strcmp(mp, "no") == 0) {
12011506c41Sgjelinek 		zfs_close(zhp);
12111506c41Sgjelinek 		return (0);
12211506c41Sgjelinek 	}
12311506c41Sgjelinek 
12411506c41Sgjelinek 	/* Now check mount point. */
1250b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mp, sizeof (mp), NULL, NULL,
12611506c41Sgjelinek 	    0, B_FALSE) != 0) {
12711506c41Sgjelinek 		zfs_close(zhp);
12811506c41Sgjelinek 		return (0);
12911506c41Sgjelinek 	}
13011506c41Sgjelinek 
13111506c41Sgjelinek 	cbp = (zfs_mount_data_t *)data;
13211506c41Sgjelinek 
13311506c41Sgjelinek 	if (strcmp(mp, "legacy") == 0) {
13411506c41Sgjelinek 		/* If legacy, must look in mnttab for mountpoint. */
13511506c41Sgjelinek 		FILE		*fp;
13611506c41Sgjelinek 		struct mnttab	entry;
13711506c41Sgjelinek 		const char	*nm;
13811506c41Sgjelinek 
13911506c41Sgjelinek 		nm = zfs_get_name(zhp);
14011506c41Sgjelinek 		if ((fp = fopen(MNTTAB, "r")) == NULL) {
14111506c41Sgjelinek 			zfs_close(zhp);
14211506c41Sgjelinek 			return (0);
14311506c41Sgjelinek 		}
14411506c41Sgjelinek 
14511506c41Sgjelinek 		while (getmntent(fp, &entry) == 0) {
14611506c41Sgjelinek 			if (strcmp(nm, entry.mnt_special) == 0) {
14711506c41Sgjelinek 				if (strcmp(entry.mnt_mountp, cbp->match_name)
14811506c41Sgjelinek 				    == 0) {
14911506c41Sgjelinek 					(void) fclose(fp);
15011506c41Sgjelinek 					cbp->match_handle = zhp;
15111506c41Sgjelinek 					return (1);
15211506c41Sgjelinek 				}
15311506c41Sgjelinek 				break;
15411506c41Sgjelinek 			}
15511506c41Sgjelinek 		}
15611506c41Sgjelinek 		(void) fclose(fp);
15711506c41Sgjelinek 
15811506c41Sgjelinek 	} else if (strcmp(mp, cbp->match_name) == 0) {
1590b5de56dSgjelinek 		cbp->match_handle = zhp;
1600b5de56dSgjelinek 		return (1);
1610b5de56dSgjelinek 	}
1620b5de56dSgjelinek 
16311506c41Sgjelinek 	/* Iterate over any nested datasets. */
1640b5de56dSgjelinek 	res = zfs_iter_filesystems(zhp, match_mountpoint, data);
1650b5de56dSgjelinek 	zfs_close(zhp);
1660b5de56dSgjelinek 	return (res);
1670b5de56dSgjelinek }
1680b5de56dSgjelinek 
1690b5de56dSgjelinek /*
1700b5de56dSgjelinek  * Get ZFS handle for the specified mount point.
1710b5de56dSgjelinek  */
1720b5de56dSgjelinek static zfs_handle_t *
1730b5de56dSgjelinek mount2zhandle(char *mountpoint)
1740b5de56dSgjelinek {
1750b5de56dSgjelinek 	zfs_mount_data_t	cb;
1760b5de56dSgjelinek 
1770b5de56dSgjelinek 	cb.match_name = mountpoint;
1780b5de56dSgjelinek 	cb.match_handle = NULL;
17999653d4eSeschrock 	(void) zfs_iter_root(g_zfs, match_mountpoint, &cb);
1800b5de56dSgjelinek 	return (cb.match_handle);
1810b5de56dSgjelinek }
1820b5de56dSgjelinek 
1830b5de56dSgjelinek /*
1840b5de56dSgjelinek  * Check if there is already a file system (zfs or any other type) mounted on
1850b5de56dSgjelinek  * path.
1860b5de56dSgjelinek  */
1870b5de56dSgjelinek static boolean_t
1880b5de56dSgjelinek is_mountpnt(char *path)
1890b5de56dSgjelinek {
1900b5de56dSgjelinek 	FILE		*fp;
1910b5de56dSgjelinek 	struct mnttab	entry;
1920b5de56dSgjelinek 
19311506c41Sgjelinek 	if ((fp = fopen(MNTTAB, "r")) == NULL)
1940b5de56dSgjelinek 		return (B_FALSE);
1950b5de56dSgjelinek 
1960b5de56dSgjelinek 	while (getmntent(fp, &entry) == 0) {
1970b5de56dSgjelinek 		if (strcmp(path, entry.mnt_mountp) == 0) {
1980b5de56dSgjelinek 			(void) fclose(fp);
1990b5de56dSgjelinek 			return (B_TRUE);
2000b5de56dSgjelinek 		}
2010b5de56dSgjelinek 	}
2020b5de56dSgjelinek 
2030b5de56dSgjelinek 	(void) fclose(fp);
2040b5de56dSgjelinek 	return (B_FALSE);
2050b5de56dSgjelinek }
2060b5de56dSgjelinek 
2070b5de56dSgjelinek /*
208ff17c8bfSgjelinek  * Run the brand's pre-snapshot hook before we take a ZFS snapshot of the zone.
2090b5de56dSgjelinek  */
2100b5de56dSgjelinek static int
211ff17c8bfSgjelinek pre_snapshot(char *presnapbuf)
2120b5de56dSgjelinek {
213ff17c8bfSgjelinek 	int status;
2140b5de56dSgjelinek 
215ff17c8bfSgjelinek 	/* No brand-specific handler */
216ff17c8bfSgjelinek 	if (presnapbuf[0] == '\0')
217ff17c8bfSgjelinek 		return (Z_OK);
2180b5de56dSgjelinek 
219ff17c8bfSgjelinek 	/* Run the hook */
220c75cc341S 	status = do_subproc(presnapbuf);
221ff17c8bfSgjelinek 	if ((status = subproc_status(gettext("brand-specific presnapshot"),
222ff17c8bfSgjelinek 	    status, B_FALSE)) != ZONE_SUBPROC_OK)
2230b5de56dSgjelinek 		return (Z_ERR);
2240b5de56dSgjelinek 
2250b5de56dSgjelinek 	return (Z_OK);
2260b5de56dSgjelinek }
2270b5de56dSgjelinek 
2280b5de56dSgjelinek /*
229ff17c8bfSgjelinek  * Run the brand's post-snapshot hook after we take a ZFS snapshot of the zone.
2300b5de56dSgjelinek  */
2310b5de56dSgjelinek static int
232ff17c8bfSgjelinek post_snapshot(char *postsnapbuf)
2330b5de56dSgjelinek {
234ff17c8bfSgjelinek 	int status;
2350b5de56dSgjelinek 
236ff17c8bfSgjelinek 	/* No brand-specific handler */
237ff17c8bfSgjelinek 	if (postsnapbuf[0] == '\0')
238ff17c8bfSgjelinek 		return (Z_OK);
2390b5de56dSgjelinek 
240ff17c8bfSgjelinek 	/* Run the hook */
241c75cc341S 	status = do_subproc(postsnapbuf);
242ff17c8bfSgjelinek 	if ((status = subproc_status(gettext("brand-specific postsnapshot"),
243ff17c8bfSgjelinek 	    status, B_FALSE)) != ZONE_SUBPROC_OK)
2440b5de56dSgjelinek 		return (Z_ERR);
2450b5de56dSgjelinek 
2460b5de56dSgjelinek 	return (Z_OK);
2470b5de56dSgjelinek }
2480b5de56dSgjelinek 
2490b5de56dSgjelinek /*
2500b5de56dSgjelinek  * This is a ZFS snapshot iterator call-back function which returns the
2510b5de56dSgjelinek  * highest number of SUNWzone snapshots that have been taken.
2520b5de56dSgjelinek  */
2530b5de56dSgjelinek static int
2540b5de56dSgjelinek get_snap_max(zfs_handle_t *zhp, void *data)
2550b5de56dSgjelinek {
2560b5de56dSgjelinek 	int			res;
2570b5de56dSgjelinek 	zfs_snapshot_data_t	*cbp;
2580b5de56dSgjelinek 
2590b5de56dSgjelinek 	if (zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) {
2600b5de56dSgjelinek 		zfs_close(zhp);
2610b5de56dSgjelinek 		return (0);
2620b5de56dSgjelinek 	}
2630b5de56dSgjelinek 
2640b5de56dSgjelinek 	cbp = (zfs_snapshot_data_t *)data;
2650b5de56dSgjelinek 
2660b5de56dSgjelinek 	if (strncmp(zfs_get_name(zhp), cbp->match_name, cbp->len) == 0) {
2670b5de56dSgjelinek 		char	*nump;
2680b5de56dSgjelinek 		int	num;
2690b5de56dSgjelinek 
270286822ddS 		cbp->num++;
2710b5de56dSgjelinek 		nump = (char *)(zfs_get_name(zhp) + cbp->len);
2720b5de56dSgjelinek 		num = atoi(nump);
2730b5de56dSgjelinek 		if (num > cbp->max)
2740b5de56dSgjelinek 			cbp->max = num;
2750b5de56dSgjelinek 	}
2760b5de56dSgjelinek 
2770b5de56dSgjelinek 	res = zfs_iter_snapshots(zhp, get_snap_max, data);
2780b5de56dSgjelinek 	zfs_close(zhp);
2790b5de56dSgjelinek 	return (res);
2800b5de56dSgjelinek }
2810b5de56dSgjelinek 
2820b5de56dSgjelinek /*
2830b5de56dSgjelinek  * Take a ZFS snapshot to be used for cloning the zone.
2840b5de56dSgjelinek  */
2850b5de56dSgjelinek static int
286ff17c8bfSgjelinek take_snapshot(zfs_handle_t *zhp, char *snapshot_name, int snap_size,
287ff17c8bfSgjelinek     char *presnapbuf, char *postsnapbuf)
2880b5de56dSgjelinek {
2890b5de56dSgjelinek 	int			res;
2900b5de56dSgjelinek 	char			template[ZFS_MAXNAMELEN];
2910b5de56dSgjelinek 	zfs_snapshot_data_t	cb;
2920b5de56dSgjelinek 
2930b5de56dSgjelinek 	/*
2940b5de56dSgjelinek 	 * First we need to figure out the next available name for the
2950b5de56dSgjelinek 	 * zone snapshot.  Look through the list of zones snapshots for
2960b5de56dSgjelinek 	 * this file system to determine the maximum snapshot name.
2970b5de56dSgjelinek 	 */
2980b5de56dSgjelinek 	if (snprintf(template, sizeof (template), "%s@SUNWzone",
2990b5de56dSgjelinek 	    zfs_get_name(zhp)) >=  sizeof (template))
3000b5de56dSgjelinek 		return (Z_ERR);
3010b5de56dSgjelinek 
3020b5de56dSgjelinek 	cb.match_name = template;
3030b5de56dSgjelinek 	cb.len = strlen(template);
3040b5de56dSgjelinek 	cb.max = 0;
3050b5de56dSgjelinek 
3060b5de56dSgjelinek 	if (zfs_iter_snapshots(zhp, get_snap_max, &cb) != 0)
3070b5de56dSgjelinek 		return (Z_ERR);
3080b5de56dSgjelinek 
3090b5de56dSgjelinek 	cb.max++;
3100b5de56dSgjelinek 
3110b5de56dSgjelinek 	if (snprintf(snapshot_name, snap_size, "%s@SUNWzone%d",
3120b5de56dSgjelinek 	    zfs_get_name(zhp), cb.max) >= snap_size)
3130b5de56dSgjelinek 		return (Z_ERR);
3140b5de56dSgjelinek 
315ff17c8bfSgjelinek 	if (pre_snapshot(presnapbuf) != Z_OK)
3160b5de56dSgjelinek 		return (Z_ERR);
317bb0ade09Sahrens 	res = zfs_snapshot(g_zfs, snapshot_name, B_FALSE, NULL);
318ff17c8bfSgjelinek 	if (post_snapshot(postsnapbuf) != Z_OK)
3190b5de56dSgjelinek 		return (Z_ERR);
3200b5de56dSgjelinek 
3210b5de56dSgjelinek 	if (res != 0)
3220b5de56dSgjelinek 		return (Z_ERR);
3230b5de56dSgjelinek 	return (Z_OK);
3240b5de56dSgjelinek }
3250b5de56dSgjelinek 
3260b5de56dSgjelinek /*
3270b5de56dSgjelinek  * We are using an explicit snapshot from some earlier point in time so
328ff17c8bfSgjelinek  * we need to validate it.  Run the brand specific hook.
3290b5de56dSgjelinek  */
3300b5de56dSgjelinek static int
331ff17c8bfSgjelinek validate_snapshot(char *snapshot_name, char *snap_path, char *validsnapbuf)
3320b5de56dSgjelinek {
333ff17c8bfSgjelinek 	int status;
334ff17c8bfSgjelinek 	char cmdbuf[MAXPATHLEN];
3350b5de56dSgjelinek 
336ff17c8bfSgjelinek 	/* No brand-specific handler */
337ff17c8bfSgjelinek 	if (validsnapbuf[0] == '\0')
338ff17c8bfSgjelinek 		return (Z_OK);
3390b5de56dSgjelinek 
340ff17c8bfSgjelinek 	/* pass args - snapshot_name & snap_path */
341ff17c8bfSgjelinek 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %s %s", validsnapbuf,
342ff17c8bfSgjelinek 	    snapshot_name, snap_path) >= sizeof (cmdbuf)) {
343ff17c8bfSgjelinek 		zerror("Command line too long");
3440b5de56dSgjelinek 		return (Z_ERR);
3450b5de56dSgjelinek 	}
3460b5de56dSgjelinek 
347ff17c8bfSgjelinek 	/* Run the hook */
348c75cc341S 	status = do_subproc(cmdbuf);
349ff17c8bfSgjelinek 	if ((status = subproc_status(gettext("brand-specific validatesnapshot"),
350ff17c8bfSgjelinek 	    status, B_FALSE)) != ZONE_SUBPROC_OK)
351ff17c8bfSgjelinek 		return (Z_ERR);
3520b5de56dSgjelinek 
353ff17c8bfSgjelinek 	return (Z_OK);
3540b5de56dSgjelinek }
3550b5de56dSgjelinek 
3560b5de56dSgjelinek /*
3570b5de56dSgjelinek  * Remove the sw inventory file from inside this zonepath that we picked up out
3580b5de56dSgjelinek  * of the snapshot.
3590b5de56dSgjelinek  */
3600b5de56dSgjelinek static int
3610b5de56dSgjelinek clean_out_clone()
3620b5de56dSgjelinek {
3630b5de56dSgjelinek 	int err;
3640b5de56dSgjelinek 	zone_dochandle_t handle;
3650b5de56dSgjelinek 
3660b5de56dSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
3670b5de56dSgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3680b5de56dSgjelinek 		return (Z_ERR);
3690b5de56dSgjelinek 	}
3700b5de56dSgjelinek 
3710b5de56dSgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
3720b5de56dSgjelinek 		errno = err;
3730b5de56dSgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3740b5de56dSgjelinek 		zonecfg_fini_handle(handle);
3750b5de56dSgjelinek 		return (Z_ERR);
3760b5de56dSgjelinek 	}
3770b5de56dSgjelinek 
3780b5de56dSgjelinek 	zonecfg_rm_detached(handle, B_FALSE);
3790b5de56dSgjelinek 	zonecfg_fini_handle(handle);
3800b5de56dSgjelinek 
3810b5de56dSgjelinek 	return (Z_OK);
3820b5de56dSgjelinek }
3830b5de56dSgjelinek 
3840b5de56dSgjelinek /*
3850b5de56dSgjelinek  * Make a ZFS clone on zonepath from snapshot_name.
3860b5de56dSgjelinek  */
3870b5de56dSgjelinek static int
3880b5de56dSgjelinek clone_snap(char *snapshot_name, char *zonepath)
3890b5de56dSgjelinek {
3900b5de56dSgjelinek 	int		res = Z_OK;
3910b5de56dSgjelinek 	int		err;
3920b5de56dSgjelinek 	zfs_handle_t	*zhp;
3930b5de56dSgjelinek 	zfs_handle_t	*clone;
394e9dbad6fSeschrock 	nvlist_t	*props = NULL;
3950b5de56dSgjelinek 
39699653d4eSeschrock 	if ((zhp = zfs_open(g_zfs, snapshot_name, ZFS_TYPE_SNAPSHOT)) == NULL)
3970b5de56dSgjelinek 		return (Z_NO_ENTRY);
3980b5de56dSgjelinek 
3990b5de56dSgjelinek 	(void) printf(gettext("Cloning snapshot %s\n"), snapshot_name);
4000b5de56dSgjelinek 
4012b6c28b8Sbatschul 	/*
4022b6c28b8Sbatschul 	 * We turn off zfs SHARENFS and SHARESMB properties on the
4032b6c28b8Sbatschul 	 * zoneroot dataset in order to prevent the GZ from sharing
4042b6c28b8Sbatschul 	 * NGZ data by accident.
4052b6c28b8Sbatschul 	 */
4062b6c28b8Sbatschul 	if ((nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) ||
4072b6c28b8Sbatschul 	    (nvlist_add_string(props, zfs_prop_to_name(ZFS_PROP_SHARENFS),
4082b6c28b8Sbatschul 	    "off") != 0) ||
4092b6c28b8Sbatschul 	    (nvlist_add_string(props, zfs_prop_to_name(ZFS_PROP_SHARESMB),
4102b6c28b8Sbatschul 	    "off") != 0)) {
4115f8e1617Snn 		if (props != NULL)
4125f8e1617Snn 			nvlist_free(props);
413e9dbad6fSeschrock 		(void) fprintf(stderr, gettext("could not create ZFS clone "
414e9dbad6fSeschrock 		    "%s: out of memory\n"), zonepath);
415e9dbad6fSeschrock 		return (Z_ERR);
416e9dbad6fSeschrock 	}
417e9dbad6fSeschrock 
418e9dbad6fSeschrock 	err = zfs_clone(zhp, zonepath, props);
4190b5de56dSgjelinek 	zfs_close(zhp);
420e9dbad6fSeschrock 
421e9dbad6fSeschrock 	nvlist_free(props);
422e9dbad6fSeschrock 
4230b5de56dSgjelinek 	if (err != 0)
4240b5de56dSgjelinek 		return (Z_ERR);
4250b5de56dSgjelinek 
4260b5de56dSgjelinek 	/* create the mountpoint if necessary */
427990b4856Slling 	if ((clone = zfs_open(g_zfs, zonepath, ZFS_TYPE_DATASET)) == NULL)
4280b5de56dSgjelinek 		return (Z_ERR);
4290b5de56dSgjelinek 
4300b5de56dSgjelinek 	/*
4310b5de56dSgjelinek 	 * The clone has been created so we need to print a diagnostic
4320b5de56dSgjelinek 	 * message if one of the following steps fails for some reason.
4330b5de56dSgjelinek 	 */
4340b5de56dSgjelinek 	if (zfs_mount(clone, NULL, 0) != 0) {
4350b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not mount ZFS clone "
4360b5de56dSgjelinek 		    "%s\n"), zfs_get_name(clone));
4370b5de56dSgjelinek 		res = Z_ERR;
4380b5de56dSgjelinek 
439e9dbad6fSeschrock 	} else if (clean_out_clone() != Z_OK) {
440e9dbad6fSeschrock 		(void) fprintf(stderr, gettext("could not remove the "
441e9dbad6fSeschrock 		    "software inventory from ZFS clone %s\n"),
442e9dbad6fSeschrock 		    zfs_get_name(clone));
443e9dbad6fSeschrock 		res = Z_ERR;
4440b5de56dSgjelinek 	}
4450b5de56dSgjelinek 
4460b5de56dSgjelinek 	zfs_close(clone);
4470b5de56dSgjelinek 	return (res);
4480b5de56dSgjelinek }
4490b5de56dSgjelinek 
4500b5de56dSgjelinek /*
4510b5de56dSgjelinek  * This function takes a zonepath and attempts to determine what the ZFS
4520b5de56dSgjelinek  * file system name (not mountpoint) should be for that path.  We do not
4530b5de56dSgjelinek  * assume that zonepath is an existing directory or ZFS fs since we use
4540b5de56dSgjelinek  * this function as part of the process of creating a new ZFS fs or clone.
4550b5de56dSgjelinek  *
4560b5de56dSgjelinek  * The way this works is that we look at the parent directory of the zonepath
4570b5de56dSgjelinek  * to see if it is a ZFS fs.  If it is, we get the name of that ZFS fs and
4580b5de56dSgjelinek  * append the last component of the zonepath to generate the ZFS name for the
4590b5de56dSgjelinek  * zonepath.  This matches the algorithm that ZFS uses for automatically
4600b5de56dSgjelinek  * mounting a new fs after it is created.
4610b5de56dSgjelinek  *
4620b5de56dSgjelinek  * Although a ZFS fs can be mounted anywhere, we don't worry about handling
4630b5de56dSgjelinek  * all of the complexity that a user could possibly configure with arbitrary
4640b5de56dSgjelinek  * mounts since there is no way to generate a ZFS name from a random path in
4650b5de56dSgjelinek  * the file system.  We only try to handle the automatic mounts that ZFS does
4660b5de56dSgjelinek  * for each file system.  ZFS restricts this so that a new fs must be created
4670b5de56dSgjelinek  * in an existing parent ZFS fs.  It then automatically mounts the new fs
4680b5de56dSgjelinek  * directly under the mountpoint for the parent fs using the last component
4690b5de56dSgjelinek  * of the name as the mountpoint directory.
4700b5de56dSgjelinek  *
4710b5de56dSgjelinek  * For example:
4720b5de56dSgjelinek  *    Name			Mountpoint
4730b5de56dSgjelinek  *    space/eng/dev/test/zone1	/project1/eng/dev/test/zone1
4740b5de56dSgjelinek  *
4750b5de56dSgjelinek  * Return Z_OK if the path mapped to a ZFS file system name, otherwise return
4760b5de56dSgjelinek  * Z_ERR.
4770b5de56dSgjelinek  */
4780b5de56dSgjelinek static int
4790b5de56dSgjelinek path2name(char *zonepath, char *zfs_name, int len)
4800b5de56dSgjelinek {
4810b5de56dSgjelinek 	int		res;
48211506c41Sgjelinek 	char		*bnm, *dnm, *dname, *bname;
4830b5de56dSgjelinek 	zfs_handle_t	*zhp;
48411506c41Sgjelinek 	struct stat	stbuf;
48511506c41Sgjelinek 
48611506c41Sgjelinek 	/*
48711506c41Sgjelinek 	 * We need two tmp strings to handle paths directly in / (e.g. /foo)
48811506c41Sgjelinek 	 * since dirname will overwrite the first char after "/" in this case.
48911506c41Sgjelinek 	 */
49011506c41Sgjelinek 	if ((bnm = strdup(zonepath)) == NULL)
49111506c41Sgjelinek 		return (Z_ERR);
4920b5de56dSgjelinek 
49311506c41Sgjelinek 	if ((dnm = strdup(zonepath)) == NULL) {
49411506c41Sgjelinek 		free(bnm);
4950b5de56dSgjelinek 		return (Z_ERR);
49611506c41Sgjelinek 	}
49711506c41Sgjelinek 
49811506c41Sgjelinek 	bname = basename(bnm);
49911506c41Sgjelinek 	dname = dirname(dnm);
5000b5de56dSgjelinek 
5010b5de56dSgjelinek 	/*
50211506c41Sgjelinek 	 * This is a quick test to save iterating over all of the zfs datasets
50311506c41Sgjelinek 	 * on the system (which can be a lot).  If the parent dir is not in a
50411506c41Sgjelinek 	 * ZFS fs, then we're done.
5050b5de56dSgjelinek 	 */
50611506c41Sgjelinek 	if (stat(dname, &stbuf) != 0 || !S_ISDIR(stbuf.st_mode) ||
50711506c41Sgjelinek 	    strcmp(stbuf.st_fstype, MNTTYPE_ZFS) != 0) {
50811506c41Sgjelinek 		free(bnm);
50911506c41Sgjelinek 		free(dnm);
5100b5de56dSgjelinek 		return (Z_ERR);
51111506c41Sgjelinek 	}
51211506c41Sgjelinek 
51311506c41Sgjelinek 	/* See if the parent directory is its own ZFS dataset. */
51411506c41Sgjelinek 	if ((zhp = mount2zhandle(dname)) == NULL) {
51511506c41Sgjelinek 		/*
51611506c41Sgjelinek 		 * The parent is not a ZFS dataset so we can't automatically
51711506c41Sgjelinek 		 * create a dataset on the given path.
51811506c41Sgjelinek 		 */
51911506c41Sgjelinek 		free(bnm);
52011506c41Sgjelinek 		free(dnm);
52111506c41Sgjelinek 		return (Z_ERR);
52211506c41Sgjelinek 	}
5230b5de56dSgjelinek 
52411506c41Sgjelinek 	res = snprintf(zfs_name, len, "%s/%s", zfs_get_name(zhp), bname);
5250b5de56dSgjelinek 
52611506c41Sgjelinek 	free(bnm);
52711506c41Sgjelinek 	free(dnm);
5280b5de56dSgjelinek 	zfs_close(zhp);
5290b5de56dSgjelinek 	if (res >= len)
5300b5de56dSgjelinek 		return (Z_ERR);
5310b5de56dSgjelinek 
5320b5de56dSgjelinek 	return (Z_OK);
5330b5de56dSgjelinek }
5340b5de56dSgjelinek 
5350b5de56dSgjelinek /*
5360b5de56dSgjelinek  * A ZFS file system iterator call-back function used to determine if the
5370b5de56dSgjelinek  * file system has dependents (snapshots & clones).
5380b5de56dSgjelinek  */
5390b5de56dSgjelinek /* ARGSUSED */
5400b5de56dSgjelinek static int
5410b5de56dSgjelinek has_dependent(zfs_handle_t *zhp, void *data)
5420b5de56dSgjelinek {
5430b5de56dSgjelinek 	zfs_close(zhp);
5440b5de56dSgjelinek 	return (1);
5450b5de56dSgjelinek }
5460b5de56dSgjelinek 
5470b5de56dSgjelinek /*
5480b5de56dSgjelinek  * Given a snapshot name, get the file system path where the snapshot lives.
5490b5de56dSgjelinek  * A snapshot name is of the form fs_name@snap_name.  For example, snapshot
5500b5de56dSgjelinek  * pl/zones/z1@SUNWzone1 would have a path of
5510b5de56dSgjelinek  * /pl/zones/z1/.zfs/snapshot/SUNWzone1.
5520b5de56dSgjelinek  */
5530b5de56dSgjelinek static int
5540b5de56dSgjelinek snap2path(char *snap_name, char *path, int len)
5550b5de56dSgjelinek {
5560b5de56dSgjelinek 	char		*p;
5570b5de56dSgjelinek 	zfs_handle_t	*zhp;
5580b5de56dSgjelinek 	char		mp[ZFS_MAXPROPLEN];
5590b5de56dSgjelinek 
5600b5de56dSgjelinek 	if ((p = strrchr(snap_name, '@')) == NULL)
5610b5de56dSgjelinek 		return (Z_ERR);
5620b5de56dSgjelinek 
5630b5de56dSgjelinek 	/* Get the file system name from the snap_name. */
5640b5de56dSgjelinek 	*p = '\0';
565990b4856Slling 	zhp = zfs_open(g_zfs, snap_name, ZFS_TYPE_DATASET);
5660b5de56dSgjelinek 	*p = '@';
5670b5de56dSgjelinek 	if (zhp == NULL)
5680b5de56dSgjelinek 		return (Z_ERR);
5690b5de56dSgjelinek 
5700b5de56dSgjelinek 	/* Get the file system mount point. */
5710b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mp, sizeof (mp), NULL, NULL,
57299653d4eSeschrock 	    0, B_FALSE) != 0) {
5730b5de56dSgjelinek 		zfs_close(zhp);
5740b5de56dSgjelinek 		return (Z_ERR);
5750b5de56dSgjelinek 	}
5760b5de56dSgjelinek 	zfs_close(zhp);
5770b5de56dSgjelinek 
5780b5de56dSgjelinek 	p++;
5790b5de56dSgjelinek 	if (snprintf(path, len, "%s/.zfs/snapshot/%s", mp, p) >= len)
5800b5de56dSgjelinek 		return (Z_ERR);
5810b5de56dSgjelinek 
5820b5de56dSgjelinek 	return (Z_OK);
5830b5de56dSgjelinek }
5840b5de56dSgjelinek 
585286822ddS /*
586286822ddS  * This callback function is used to iterate through a snapshot's dependencies
587286822ddS  * to find a filesystem that is a direct clone of the snapshot being iterated.
588286822ddS  */
589286822ddS static int
590286822ddS get_direct_clone(zfs_handle_t *zhp, void *data)
591286822ddS {
592286822ddS 	clone_data_t	*cd = data;
593286822ddS 	char		origin[ZFS_MAXNAMELEN];
594286822ddS 	char		ds_path[ZFS_MAXNAMELEN];
595286822ddS 
596286822ddS 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
597286822ddS 		zfs_close(zhp);
598286822ddS 		return (0);
599286822ddS 	}
600286822ddS 
601286822ddS 	(void) strlcpy(ds_path, zfs_get_name(zhp), sizeof (ds_path));
602286822ddS 
603286822ddS 	/* Make sure this is a direct clone of the snapshot we're iterating. */
604286822ddS 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin, sizeof (origin), NULL,
605286822ddS 	    NULL, 0, B_FALSE) != 0 || strcmp(origin, cd->snapshot) != 0) {
606286822ddS 		zfs_close(zhp);
607286822ddS 		return (0);
608286822ddS 	}
609286822ddS 
610286822ddS 	if (cd->clone_zhp != NULL)
611286822ddS 		zfs_close(cd->clone_zhp);
612286822ddS 
613286822ddS 	cd->clone_zhp = zhp;
614286822ddS 	return (1);
615286822ddS }
616286822ddS 
617286822ddS /*
618286822ddS  * A ZFS file system iterator call-back function used to determine the clone
619286822ddS  * to promote.  This function finds the youngest (i.e. last one taken) snapshot
620286822ddS  * that has a clone.  If found, it returns a reference to that clone in the
621286822ddS  * callback data.
622286822ddS  */
623286822ddS static int
624286822ddS find_clone(zfs_handle_t *zhp, void *data)
625286822ddS {
626286822ddS 	clone_data_t	*cd = data;
627286822ddS 	time_t		snap_creation;
628286822ddS 	int		zret = 0;
629286822ddS 
630286822ddS 	/* If snapshot has no clones, skip it */
631286822ddS 	if (zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES) == 0) {
632286822ddS 		zfs_close(zhp);
633286822ddS 		return (0);
634286822ddS 	}
635286822ddS 
636286822ddS 	cd->snapshot = zfs_get_name(zhp);
637286822ddS 
638286822ddS 	/* Get the creation time of this snapshot */
639286822ddS 	snap_creation = (time_t)zfs_prop_get_int(zhp, ZFS_PROP_CREATION);
640286822ddS 
641286822ddS 	/*
642286822ddS 	 * If this snapshot's creation time is greater than (i.e. younger than)
643286822ddS 	 * the current youngest snapshot found, iterate this snapshot to
644286822ddS 	 * get the right clone.
645286822ddS 	 */
646286822ddS 	if (snap_creation >= cd->origin_creation) {
647286822ddS 		/*
648286822ddS 		 * Iterate the dependents of this snapshot to find a clone
649286822ddS 		 * that's a direct dependent.
650286822ddS 		 */
651286822ddS 		if ((zret = zfs_iter_dependents(zhp, B_FALSE, get_direct_clone,
652286822ddS 		    cd)) == -1) {
653286822ddS 			zfs_close(zhp);
654286822ddS 			return (1);
655286822ddS 		} else if (zret == 1) {
656286822ddS 			/*
657286822ddS 			 * Found a clone, update the origin_creation time
658286822ddS 			 * in the callback data.
659286822ddS 			 */
660286822ddS 			cd->origin_creation = snap_creation;
661286822ddS 		}
662286822ddS 	}
663286822ddS 
664286822ddS 	zfs_close(zhp);
665286822ddS 	return (0);
666286822ddS }
667286822ddS 
668286822ddS /*
669286822ddS  * A ZFS file system iterator call-back function used to remove standalone
670286822ddS  * snapshots.
671286822ddS  */
672286822ddS /* ARGSUSED */
673286822ddS static int
674286822ddS rm_snap(zfs_handle_t *zhp, void *data)
675286822ddS {
676286822ddS 	/* If snapshot has clones, something is wrong */
677286822ddS 	if (zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES) != 0) {
678286822ddS 		zfs_close(zhp);
679286822ddS 		return (1);
680286822ddS 	}
681286822ddS 
682286822ddS 	if (zfs_unmount(zhp, NULL, 0) == 0) {
683842727c2SChris Kirby 		(void) zfs_destroy(zhp, B_FALSE);
684286822ddS 	}
685286822ddS 
686286822ddS 	zfs_close(zhp);
687286822ddS 	return (0);
688286822ddS }
689286822ddS 
690286822ddS /*
691286822ddS  * A ZFS snapshot iterator call-back function which renames snapshots.
692286822ddS  */
693286822ddS static int
694286822ddS rename_snap(zfs_handle_t *zhp, void *data)
695286822ddS {
696286822ddS 	int			res;
697286822ddS 	zfs_snapshot_data_t	*cbp;
698286822ddS 	char			template[ZFS_MAXNAMELEN];
699286822ddS 
700286822ddS 	cbp = (zfs_snapshot_data_t *)data;
701286822ddS 
702286822ddS 	/*
703286822ddS 	 * When renaming snapshots with the iterator, the iterator can see
704286822ddS 	 * the same snapshot after we've renamed up in the namespace.  To
705286822ddS 	 * prevent this we check the count for the number of snapshots we have
706286822ddS 	 * to rename and stop at that point.
707286822ddS 	 */
708286822ddS 	if (cbp->cntr >= cbp->num) {
709286822ddS 		zfs_close(zhp);
710286822ddS 		return (0);
711286822ddS 	}
712286822ddS 
713286822ddS 	if (zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) {
714286822ddS 		zfs_close(zhp);
715286822ddS 		return (0);
716286822ddS 	}
717286822ddS 
718286822ddS 	/* Only rename the snapshots we automatically generate when we clone. */
719286822ddS 	if (strncmp(zfs_get_name(zhp), cbp->match_name, cbp->len) != 0) {
720286822ddS 		zfs_close(zhp);
721286822ddS 		return (0);
722286822ddS 	}
723286822ddS 
724286822ddS 	(void) snprintf(template, sizeof (template), "%s%d", cbp->match_name,
725286822ddS 	    cbp->max++);
726286822ddS 
727*6a9cb0eaSEric Schrock 	res = (zfs_rename(zhp, template, B_FALSE, B_FALSE) != 0);
728286822ddS 	if (res != 0)
729286822ddS 		(void) fprintf(stderr, gettext("failed to rename snapshot %s "
730286822ddS 		    "to %s: %s\n"), zfs_get_name(zhp), template,
731286822ddS 		    libzfs_error_description(g_zfs));
732286822ddS 
733286822ddS 	cbp->cntr++;
734286822ddS 
735286822ddS 	zfs_close(zhp);
736286822ddS 	return (res);
737286822ddS }
738286822ddS 
739286822ddS /*
740286822ddS  * Rename the source dataset's snapshots that are automatically generated when
741286822ddS  * we clone a zone so that there won't be a name collision when we promote the
742286822ddS  * cloned dataset.  Once the snapshots have been renamed, then promote the
743286822ddS  * clone.
744286822ddS  *
745286822ddS  * The snapshot rename process gets the highest number on the snapshot names
746286822ddS  * (the format is zonename@SUNWzoneXX where XX are digits) on both the source
747286822ddS  * and clone datasets, then renames the source dataset snapshots starting at
748286822ddS  * the next number.
749286822ddS  */
750286822ddS static int
751286822ddS promote_clone(zfs_handle_t *src_zhp, zfs_handle_t *cln_zhp)
752286822ddS {
753286822ddS 	zfs_snapshot_data_t	sd;
754286822ddS 	char			nm[ZFS_MAXNAMELEN];
755286822ddS 	char			template[ZFS_MAXNAMELEN];
756286822ddS 
757286822ddS 	(void) strlcpy(nm, zfs_get_name(cln_zhp), sizeof (nm));
758286822ddS 	/*
759286822ddS 	 * Start by getting the clone's snapshot max which we use
760286822ddS 	 * during the rename of the original dataset's snapshots.
761286822ddS 	 */
762286822ddS 	(void) snprintf(template, sizeof (template), "%s@SUNWzone", nm);
763286822ddS 	sd.match_name = template;
764286822ddS 	sd.len = strlen(template);
765286822ddS 	sd.max = 0;
766286822ddS 
767286822ddS 	if (zfs_iter_snapshots(cln_zhp, get_snap_max, &sd) != 0)
768286822ddS 		return (Z_ERR);
769286822ddS 
770286822ddS 	/*
771286822ddS 	 * Now make sure the source's snapshot max is at least as high as
772286822ddS 	 * the clone's snapshot max.
773286822ddS 	 */
774286822ddS 	(void) snprintf(template, sizeof (template), "%s@SUNWzone",
775286822ddS 	    zfs_get_name(src_zhp));
776286822ddS 	sd.match_name = template;
777286822ddS 	sd.len = strlen(template);
778286822ddS 	sd.num = 0;
779286822ddS 
780286822ddS 	if (zfs_iter_snapshots(src_zhp, get_snap_max, &sd) != 0)
781286822ddS 		return (Z_ERR);
782286822ddS 
783286822ddS 	/*
784286822ddS 	 * Now rename the source dataset's snapshots so there's no
785286822ddS 	 * conflict when we promote the clone.
786286822ddS 	 */
787286822ddS 	sd.max++;
788286822ddS 	sd.cntr = 0;
789286822ddS 	if (zfs_iter_snapshots(src_zhp, rename_snap, &sd) != 0)
790286822ddS 		return (Z_ERR);
791286822ddS 
792286822ddS 	/* close and reopen the clone dataset to get the latest info */
793286822ddS 	zfs_close(cln_zhp);
794286822ddS 	if ((cln_zhp = zfs_open(g_zfs, nm, ZFS_TYPE_FILESYSTEM)) == NULL)
795286822ddS 		return (Z_ERR);
796286822ddS 
797286822ddS 	if (zfs_promote(cln_zhp) != 0) {
798286822ddS 		(void) fprintf(stderr, gettext("failed to promote %s: %s\n"),
799286822ddS 		    nm, libzfs_error_description(g_zfs));
800286822ddS 		return (Z_ERR);
801286822ddS 	}
802286822ddS 
803286822ddS 	zfs_close(cln_zhp);
804286822ddS 	return (Z_OK);
805286822ddS }
806286822ddS 
807286822ddS /*
808286822ddS  * Promote the youngest clone.  That clone will then become the origin of all
809286822ddS  * of the other clones that were hanging off of the source dataset.
810286822ddS  */
811286822ddS int
812286822ddS promote_all_clones(zfs_handle_t *zhp)
813286822ddS {
814286822ddS 	clone_data_t	cd;
815286822ddS 	char		nm[ZFS_MAXNAMELEN];
816286822ddS 
817286822ddS 	cd.clone_zhp = NULL;
818286822ddS 	cd.origin_creation = 0;
819286822ddS 	cd.snapshot = NULL;
820286822ddS 
821286822ddS 	if (zfs_iter_snapshots(zhp, find_clone, &cd) != 0) {
822286822ddS 		zfs_close(zhp);
823286822ddS 		return (Z_ERR);
824286822ddS 	}
825286822ddS 
826286822ddS 	/* Nothing to promote. */
827286822ddS 	if (cd.clone_zhp == NULL)
828286822ddS 		return (Z_OK);
829286822ddS 
830286822ddS 	/* Found the youngest clone to promote.  Promote it. */
831286822ddS 	if (promote_clone(zhp, cd.clone_zhp) != 0) {
832286822ddS 		zfs_close(cd.clone_zhp);
833286822ddS 		zfs_close(zhp);
834286822ddS 		return (Z_ERR);
835286822ddS 	}
836286822ddS 
837286822ddS 	/* close and reopen the main dataset to get the latest info */
838286822ddS 	(void) strlcpy(nm, zfs_get_name(zhp), sizeof (nm));
839286822ddS 	zfs_close(zhp);
840286822ddS 	if ((zhp = zfs_open(g_zfs, nm, ZFS_TYPE_FILESYSTEM)) == NULL)
841286822ddS 		return (Z_ERR);
842286822ddS 
843286822ddS 	return (Z_OK);
844286822ddS }
845286822ddS 
8460b5de56dSgjelinek /*
8470b5de56dSgjelinek  * Clone a pre-existing ZFS snapshot, either by making a direct ZFS clone, if
8480b5de56dSgjelinek  * possible, or by copying the data from the snapshot to the zonepath.
8490b5de56dSgjelinek  */
8500b5de56dSgjelinek int
851ff17c8bfSgjelinek clone_snapshot_zfs(char *snap_name, char *zonepath, char *validatesnap)
8520b5de56dSgjelinek {
8530b5de56dSgjelinek 	int	err = Z_OK;
8540b5de56dSgjelinek 	char	clone_name[MAXPATHLEN];
8550b5de56dSgjelinek 	char	snap_path[MAXPATHLEN];
8560b5de56dSgjelinek 
8570b5de56dSgjelinek 	if (snap2path(snap_name, snap_path, sizeof (snap_path)) != Z_OK) {
8580b5de56dSgjelinek 		(void) fprintf(stderr, gettext("unable to find path for %s.\n"),
8590b5de56dSgjelinek 		    snap_name);
8600b5de56dSgjelinek 		return (Z_ERR);
8610b5de56dSgjelinek 	}
8620b5de56dSgjelinek 
863ff17c8bfSgjelinek 	if (validate_snapshot(snap_name, snap_path, validatesnap) != Z_OK)
8640b5de56dSgjelinek 		return (Z_NO_ENTRY);
8650b5de56dSgjelinek 
8660b5de56dSgjelinek 	/*
8670b5de56dSgjelinek 	 * The zonepath cannot be ZFS cloned, try to copy the data from
8680b5de56dSgjelinek 	 * within the snapshot to the zonepath.
8690b5de56dSgjelinek 	 */
8700b5de56dSgjelinek 	if (path2name(zonepath, clone_name, sizeof (clone_name)) != Z_OK) {
8710b5de56dSgjelinek 		if ((err = clone_copy(snap_path, zonepath)) == Z_OK)
8720b5de56dSgjelinek 			if (clean_out_clone() != Z_OK)
8730b5de56dSgjelinek 				(void) fprintf(stderr,
8740b5de56dSgjelinek 				    gettext("could not remove the "
8750b5de56dSgjelinek 				    "software inventory from %s\n"), zonepath);
8760b5de56dSgjelinek 
8770b5de56dSgjelinek 		return (err);
8780b5de56dSgjelinek 	}
8790b5de56dSgjelinek 
8800b5de56dSgjelinek 	if ((err = clone_snap(snap_name, clone_name)) != Z_OK) {
8810b5de56dSgjelinek 		if (err != Z_NO_ENTRY) {
8820b5de56dSgjelinek 			/*
8830b5de56dSgjelinek 			 * Cloning the snapshot failed.  Fall back to trying
8840b5de56dSgjelinek 			 * to install the zone by copying from the snapshot.
8850b5de56dSgjelinek 			 */
8860b5de56dSgjelinek 			if ((err = clone_copy(snap_path, zonepath)) == Z_OK)
8870b5de56dSgjelinek 				if (clean_out_clone() != Z_OK)
8880b5de56dSgjelinek 					(void) fprintf(stderr,
8890b5de56dSgjelinek 					    gettext("could not remove the "
8900b5de56dSgjelinek 					    "software inventory from %s\n"),
8910b5de56dSgjelinek 					    zonepath);
8920b5de56dSgjelinek 		} else {
8930b5de56dSgjelinek 			/*
8940b5de56dSgjelinek 			 * The snapshot is unusable for some reason so restore
8950b5de56dSgjelinek 			 * the zone state to configured since we were unable to
8960b5de56dSgjelinek 			 * actually do anything about getting the zone
8970b5de56dSgjelinek 			 * installed.
8980b5de56dSgjelinek 			 */
8990b5de56dSgjelinek 			int tmp;
9000b5de56dSgjelinek 
9010b5de56dSgjelinek 			if ((tmp = zone_set_state(target_zone,
9020b5de56dSgjelinek 			    ZONE_STATE_CONFIGURED)) != Z_OK) {
9030b5de56dSgjelinek 				errno = tmp;
9040b5de56dSgjelinek 				zperror2(target_zone,
9050b5de56dSgjelinek 				    gettext("could not set state"));
9060b5de56dSgjelinek 			}
9070b5de56dSgjelinek 		}
9080b5de56dSgjelinek 	}
9090b5de56dSgjelinek 
9100b5de56dSgjelinek 	return (err);
9110b5de56dSgjelinek }
9120b5de56dSgjelinek 
9130b5de56dSgjelinek /*
9140b5de56dSgjelinek  * Attempt to clone a source_zone to a target zonepath by using a ZFS clone.
9150b5de56dSgjelinek  */
9160b5de56dSgjelinek int
917ff17c8bfSgjelinek clone_zfs(char *source_zonepath, char *zonepath, char *presnapbuf,
918ff17c8bfSgjelinek     char *postsnapbuf)
9190b5de56dSgjelinek {
9200b5de56dSgjelinek 	zfs_handle_t	*zhp;
9210b5de56dSgjelinek 	char		clone_name[MAXPATHLEN];
9220b5de56dSgjelinek 	char		snap_name[MAXPATHLEN];
9230b5de56dSgjelinek 
9240b5de56dSgjelinek 	/*
9250b5de56dSgjelinek 	 * Try to get a zfs handle for the source_zonepath.  If this fails
9260b5de56dSgjelinek 	 * the source_zonepath is not ZFS so return an error.
9270b5de56dSgjelinek 	 */
9280b5de56dSgjelinek 	if ((zhp = mount2zhandle(source_zonepath)) == NULL)
9290b5de56dSgjelinek 		return (Z_ERR);
9300b5de56dSgjelinek 
9310b5de56dSgjelinek 	/*
9320b5de56dSgjelinek 	 * Check if there is a file system already mounted on zonepath.  If so,
9330b5de56dSgjelinek 	 * we can't clone to the path so we should fall back to copying.
9340b5de56dSgjelinek 	 */
9350b5de56dSgjelinek 	if (is_mountpnt(zonepath)) {
9360b5de56dSgjelinek 		zfs_close(zhp);
9370b5de56dSgjelinek 		(void) fprintf(stderr,
9380b5de56dSgjelinek 		    gettext("A file system is already mounted on %s,\n"
9390b5de56dSgjelinek 		    "preventing use of a ZFS clone.\n"), zonepath);
9400b5de56dSgjelinek 		return (Z_ERR);
9410b5de56dSgjelinek 	}
9420b5de56dSgjelinek 
9430b5de56dSgjelinek 	/*
9440b5de56dSgjelinek 	 * Instead of using path2name to get the clone name from the zonepath,
9450b5de56dSgjelinek 	 * we could generate a name from the source zone ZFS name.  However,
9460b5de56dSgjelinek 	 * this would mean we would create the clone under the ZFS fs of the
9470b5de56dSgjelinek 	 * source instead of what the zonepath says.  For example,
9480b5de56dSgjelinek 	 *
9490b5de56dSgjelinek 	 * source_zonepath		zonepath
9500b5de56dSgjelinek 	 * /pl/zones/dev/z1		/pl/zones/deploy/z2
9510b5de56dSgjelinek 	 *
9520b5de56dSgjelinek 	 * We don't want the clone to be under "dev", we want it under
9530b5de56dSgjelinek 	 * "deploy", so that we can leverage the normal attribute inheritance
9540b5de56dSgjelinek 	 * that ZFS provides in the fs hierarchy.
9550b5de56dSgjelinek 	 */
9560b5de56dSgjelinek 	if (path2name(zonepath, clone_name, sizeof (clone_name)) != Z_OK) {
9570b5de56dSgjelinek 		zfs_close(zhp);
9580b5de56dSgjelinek 		return (Z_ERR);
9590b5de56dSgjelinek 	}
9600b5de56dSgjelinek 
961ff17c8bfSgjelinek 	if (take_snapshot(zhp, snap_name, sizeof (snap_name), presnapbuf,
962ff17c8bfSgjelinek 	    postsnapbuf) != Z_OK) {
9630b5de56dSgjelinek 		zfs_close(zhp);
9640b5de56dSgjelinek 		return (Z_ERR);
9650b5de56dSgjelinek 	}
9660b5de56dSgjelinek 	zfs_close(zhp);
9670b5de56dSgjelinek 
968d9e728a2Sgjelinek 	if (clone_snap(snap_name, clone_name) != Z_OK) {
969d9e728a2Sgjelinek 		/* Clean up the snapshot we just took. */
970d9e728a2Sgjelinek 		if ((zhp = zfs_open(g_zfs, snap_name, ZFS_TYPE_SNAPSHOT))
971d9e728a2Sgjelinek 		    != NULL) {
972d9e728a2Sgjelinek 			if (zfs_unmount(zhp, NULL, 0) == 0)
973842727c2SChris Kirby 				(void) zfs_destroy(zhp, B_FALSE);
974d9e728a2Sgjelinek 			zfs_close(zhp);
975d9e728a2Sgjelinek 		}
976d9e728a2Sgjelinek 
9770b5de56dSgjelinek 		return (Z_ERR);
978d9e728a2Sgjelinek 	}
9790b5de56dSgjelinek 
9800b5de56dSgjelinek 	(void) printf(gettext("Instead of copying, a ZFS clone has been "
9810b5de56dSgjelinek 	    "created for this zone.\n"));
9820b5de56dSgjelinek 
9830b5de56dSgjelinek 	return (Z_OK);
9840b5de56dSgjelinek }
9850b5de56dSgjelinek 
9860b5de56dSgjelinek /*
9870b5de56dSgjelinek  * Attempt to create a ZFS file system for the specified zonepath.
9880b5de56dSgjelinek  * We either will successfully create a ZFS file system and get it mounted
9890b5de56dSgjelinek  * on the zonepath or we don't.  The caller doesn't care since a regular
9900b5de56dSgjelinek  * directory is used for the zonepath if no ZFS file system is mounted there.
9910b5de56dSgjelinek  */
9920b5de56dSgjelinek void
9930b5de56dSgjelinek create_zfs_zonepath(char *zonepath)
9940b5de56dSgjelinek {
9950b5de56dSgjelinek 	zfs_handle_t	*zhp;
9960b5de56dSgjelinek 	char		zfs_name[MAXPATHLEN];
997e9dbad6fSeschrock 	nvlist_t	*props = NULL;
9980b5de56dSgjelinek 
9990b5de56dSgjelinek 	if (path2name(zonepath, zfs_name, sizeof (zfs_name)) != Z_OK)
10000b5de56dSgjelinek 		return;
10010b5de56dSgjelinek 
1002d1f855d7S 	/* Check if the dataset already exists. */
1003d1f855d7S 	if ((zhp = zfs_open(g_zfs, zfs_name, ZFS_TYPE_DATASET)) != NULL) {
1004d1f855d7S 		zfs_close(zhp);
1005d1f855d7S 		return;
1006d1f855d7S 	}
1007d1f855d7S 
10082b6c28b8Sbatschul 	/*
10092b6c28b8Sbatschul 	 * We turn off zfs SHARENFS and SHARESMB properties on the
10102b6c28b8Sbatschul 	 * zoneroot dataset in order to prevent the GZ from sharing
10112b6c28b8Sbatschul 	 * NGZ data by accident.
10122b6c28b8Sbatschul 	 */
10132b6c28b8Sbatschul 	if ((nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) ||
10142b6c28b8Sbatschul 	    (nvlist_add_string(props, zfs_prop_to_name(ZFS_PROP_SHARENFS),
10152b6c28b8Sbatschul 	    "off") != 0) ||
10162b6c28b8Sbatschul 	    (nvlist_add_string(props, zfs_prop_to_name(ZFS_PROP_SHARESMB),
10172b6c28b8Sbatschul 	    "off") != 0)) {
10185f8e1617Snn 		if (props != NULL)
10195f8e1617Snn 			nvlist_free(props);
1020e9dbad6fSeschrock 		(void) fprintf(stderr, gettext("cannot create ZFS dataset %s: "
1021e9dbad6fSeschrock 		    "out of memory\n"), zfs_name);
1022e9dbad6fSeschrock 	}
1023e9dbad6fSeschrock 
1024e9dbad6fSeschrock 	if (zfs_create(g_zfs, zfs_name, ZFS_TYPE_FILESYSTEM, props) != 0 ||
1025990b4856Slling 	    (zhp = zfs_open(g_zfs, zfs_name, ZFS_TYPE_DATASET)) == NULL) {
102699653d4eSeschrock 		(void) fprintf(stderr, gettext("cannot create ZFS dataset %s: "
102799653d4eSeschrock 		    "%s\n"), zfs_name, libzfs_error_description(g_zfs));
1028e9dbad6fSeschrock 		nvlist_free(props);
10290b5de56dSgjelinek 		return;
10300b5de56dSgjelinek 	}
10310b5de56dSgjelinek 
1032e9dbad6fSeschrock 	nvlist_free(props);
1033e9dbad6fSeschrock 
10340b5de56dSgjelinek 	if (zfs_mount(zhp, NULL, 0) != 0) {
103599653d4eSeschrock 		(void) fprintf(stderr, gettext("cannot mount ZFS dataset %s: "
103699653d4eSeschrock 		    "%s\n"), zfs_name, libzfs_error_description(g_zfs));
1037842727c2SChris Kirby 		(void) zfs_destroy(zhp, B_FALSE);
10380b5de56dSgjelinek 	} else {
10390b5de56dSgjelinek 		if (chmod(zonepath, S_IRWXU) != 0) {
10400b5de56dSgjelinek 			(void) fprintf(stderr, gettext("file system %s "
10410b5de56dSgjelinek 			    "successfully created, but chmod %o failed: %s\n"),
10420b5de56dSgjelinek 			    zfs_name, S_IRWXU, strerror(errno));
10430b5de56dSgjelinek 			(void) destroy_zfs(zonepath);
10440b5de56dSgjelinek 		} else {
10450b5de56dSgjelinek 			(void) printf(gettext("A ZFS file system has been "
10460b5de56dSgjelinek 			    "created for this zone.\n"));
10470b5de56dSgjelinek 		}
10480b5de56dSgjelinek 	}
10490b5de56dSgjelinek 
10500b5de56dSgjelinek 	zfs_close(zhp);
10510b5de56dSgjelinek }
10520b5de56dSgjelinek 
10530b5de56dSgjelinek /*
10540b5de56dSgjelinek  * If the zonepath is a ZFS file system, attempt to destroy it.  We return Z_OK
10550b5de56dSgjelinek  * if we were able to zfs_destroy the zonepath, otherwise we return Z_ERR
10560b5de56dSgjelinek  * which means the caller should clean up the zonepath in the traditional
10570b5de56dSgjelinek  * way.
10580b5de56dSgjelinek  */
10590b5de56dSgjelinek int
10600b5de56dSgjelinek destroy_zfs(char *zonepath)
10610b5de56dSgjelinek {
10620b5de56dSgjelinek 	zfs_handle_t	*zhp;
10630b5de56dSgjelinek 	boolean_t	is_clone = B_FALSE;
10640b5de56dSgjelinek 	char		origin[ZFS_MAXPROPLEN];
10650b5de56dSgjelinek 
106699653d4eSeschrock 	if ((zhp = mount2zhandle(zonepath)) == NULL)
10670b5de56dSgjelinek 		return (Z_ERR);
10680b5de56dSgjelinek 
1069286822ddS 	if (promote_all_clones(zhp) != 0)
1070286822ddS 		return (Z_ERR);
1071286822ddS 
1072286822ddS 	/* Now cleanup any snapshots remaining. */
1073286822ddS 	if (zfs_iter_snapshots(zhp, rm_snap, NULL) != 0) {
1074286822ddS 		zfs_close(zhp);
1075286822ddS 		return (Z_ERR);
1076286822ddS 	}
1077286822ddS 
10780b5de56dSgjelinek 	/*
1079286822ddS 	 * We can't destroy the file system if it has still has dependents.
1080286822ddS 	 * There shouldn't be any at this point, but we'll double check.
10810b5de56dSgjelinek 	 */
1082286822ddS 	if (zfs_iter_dependents(zhp, B_TRUE, has_dependent, NULL) != 0) {
1083286822ddS 		(void) fprintf(stderr, gettext("zfs destroy %s failed: the "
1084286822ddS 		    "dataset still has dependents\n"), zfs_get_name(zhp));
10850b5de56dSgjelinek 		zfs_close(zhp);
10860b5de56dSgjelinek 		return (Z_ERR);
10870b5de56dSgjelinek 	}
10880b5de56dSgjelinek 
10890b5de56dSgjelinek 	/*
10900b5de56dSgjelinek 	 * This might be a clone.  Try to get the snapshot so we can attempt
10910b5de56dSgjelinek 	 * to destroy that as well.
10920b5de56dSgjelinek 	 */
10930b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin, sizeof (origin), NULL,
109499653d4eSeschrock 	    NULL, 0, B_FALSE) == 0)
10950b5de56dSgjelinek 		is_clone = B_TRUE;
10960b5de56dSgjelinek 
1097286822ddS 	if (zfs_unmount(zhp, NULL, 0) != 0) {
1098286822ddS 		(void) fprintf(stderr, gettext("zfs unmount %s failed: %s\n"),
1099286822ddS 		    zfs_get_name(zhp), libzfs_error_description(g_zfs));
1100286822ddS 		zfs_close(zhp);
1101286822ddS 		return (Z_ERR);
1102286822ddS 	}
1103286822ddS 
1104842727c2SChris Kirby 	if (zfs_destroy(zhp, B_FALSE) != 0) {
11050b5de56dSgjelinek 		/*
11060b5de56dSgjelinek 		 * If the destroy fails for some reason, try to remount
11070b5de56dSgjelinek 		 * the file system so that we can use "rm -rf" to clean up
11080b5de56dSgjelinek 		 * instead.
11090b5de56dSgjelinek 		 */
1110286822ddS 		(void) fprintf(stderr, gettext("zfs destroy %s failed: %s\n"),
1111286822ddS 		    zfs_get_name(zhp), libzfs_error_description(g_zfs));
11120b5de56dSgjelinek 		(void) zfs_mount(zhp, NULL, 0);
11130b5de56dSgjelinek 		zfs_close(zhp);
11140b5de56dSgjelinek 		return (Z_ERR);
11150b5de56dSgjelinek 	}
11160b5de56dSgjelinek 
1117d9e728a2Sgjelinek 	/*
1118d9e728a2Sgjelinek 	 * If the zone has ever been moved then the mountpoint dir will not be
1119d9e728a2Sgjelinek 	 * cleaned up by the zfs_destroy().  To handle this case try to clean
1120d9e728a2Sgjelinek 	 * it up now but don't worry if it fails, that will be normal.
1121d9e728a2Sgjelinek 	 */
1122d9e728a2Sgjelinek 	(void) rmdir(zonepath);
1123d9e728a2Sgjelinek 
11240b5de56dSgjelinek 	(void) printf(gettext("The ZFS file system for this zone has been "
11250b5de56dSgjelinek 	    "destroyed.\n"));
11260b5de56dSgjelinek 
11270b5de56dSgjelinek 	if (is_clone) {
11280b5de56dSgjelinek 		zfs_handle_t	*ohp;
11290b5de56dSgjelinek 
11300b5de56dSgjelinek 		/*
11310b5de56dSgjelinek 		 * Try to clean up the snapshot that the clone was taken from.
11320b5de56dSgjelinek 		 */
113399653d4eSeschrock 		if ((ohp = zfs_open(g_zfs, origin,
113499653d4eSeschrock 		    ZFS_TYPE_SNAPSHOT)) != NULL) {
11353bb79becSeschrock 			if (zfs_iter_dependents(ohp, B_TRUE, has_dependent,
11363bb79becSeschrock 			    NULL) == 0 && zfs_unmount(ohp, NULL, 0) == 0)
1137842727c2SChris Kirby 				(void) zfs_destroy(ohp, B_FALSE);
11380b5de56dSgjelinek 			zfs_close(ohp);
11390b5de56dSgjelinek 		}
11400b5de56dSgjelinek 	}
11410b5de56dSgjelinek 
11420b5de56dSgjelinek 	zfs_close(zhp);
11430b5de56dSgjelinek 	return (Z_OK);
11440b5de56dSgjelinek }
11450b5de56dSgjelinek 
11460b5de56dSgjelinek /*
11470b5de56dSgjelinek  * Return true if the path is its own zfs file system.  We determine this
11480b5de56dSgjelinek  * by stat-ing the path to see if it is zfs and stat-ing the parent to see
11490b5de56dSgjelinek  * if it is a different fs.
11500b5de56dSgjelinek  */
11510b5de56dSgjelinek boolean_t
11520b5de56dSgjelinek is_zonepath_zfs(char *zonepath)
11530b5de56dSgjelinek {
11540b5de56dSgjelinek 	int res;
11550b5de56dSgjelinek 	char *path;
11560b5de56dSgjelinek 	char *parent;
11573f2f09c1Sdp 	struct statvfs64 buf1, buf2;
11580b5de56dSgjelinek 
11593f2f09c1Sdp 	if (statvfs64(zonepath, &buf1) != 0)
11600b5de56dSgjelinek 		return (B_FALSE);
11610b5de56dSgjelinek 
11620b5de56dSgjelinek 	if (strcmp(buf1.f_basetype, "zfs") != 0)
11630b5de56dSgjelinek 		return (B_FALSE);
11640b5de56dSgjelinek 
11650b5de56dSgjelinek 	if ((path = strdup(zonepath)) == NULL)
11660b5de56dSgjelinek 		return (B_FALSE);
11670b5de56dSgjelinek 
11680b5de56dSgjelinek 	parent = dirname(path);
11693f2f09c1Sdp 	res = statvfs64(parent, &buf2);
11700b5de56dSgjelinek 	free(path);
11710b5de56dSgjelinek 
11720b5de56dSgjelinek 	if (res != 0)
11730b5de56dSgjelinek 		return (B_FALSE);
11740b5de56dSgjelinek 
11750b5de56dSgjelinek 	if (buf1.f_fsid == buf2.f_fsid)
11760b5de56dSgjelinek 		return (B_FALSE);
11770b5de56dSgjelinek 
11780b5de56dSgjelinek 	return (B_TRUE);
11790b5de56dSgjelinek }
11800b5de56dSgjelinek 
11810b5de56dSgjelinek /*
11820b5de56dSgjelinek  * Implement the fast move of a ZFS file system by simply updating the
11830b5de56dSgjelinek  * mountpoint.  Since it is file system already, we don't have the
11840b5de56dSgjelinek  * issue of cross-file system copying.
11850b5de56dSgjelinek  */
11860b5de56dSgjelinek int
11870b5de56dSgjelinek move_zfs(char *zonepath, char *new_zonepath)
11880b5de56dSgjelinek {
11890b5de56dSgjelinek 	int		ret = Z_ERR;
11900b5de56dSgjelinek 	zfs_handle_t	*zhp;
11910b5de56dSgjelinek 
119299653d4eSeschrock 	if ((zhp = mount2zhandle(zonepath)) == NULL)
11930b5de56dSgjelinek 		return (Z_ERR);
11940b5de56dSgjelinek 
1195e9dbad6fSeschrock 	if (zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1196e9dbad6fSeschrock 	    new_zonepath) == 0) {
11970b5de56dSgjelinek 		/*
11980b5de56dSgjelinek 		 * Clean up the old mount point.  We ignore any failure since
11990b5de56dSgjelinek 		 * the zone is already successfully mounted on the new path.
12000b5de56dSgjelinek 		 */
12010b5de56dSgjelinek 		(void) rmdir(zonepath);
12020b5de56dSgjelinek 		ret = Z_OK;
12030b5de56dSgjelinek 	}
12040b5de56dSgjelinek 
12050b5de56dSgjelinek 	zfs_close(zhp);
12060b5de56dSgjelinek 
12070b5de56dSgjelinek 	return (ret);
12080b5de56dSgjelinek }
12090b5de56dSgjelinek 
12100b5de56dSgjelinek /*
12110b5de56dSgjelinek  * Validate that the given dataset exists on the system, and that neither it nor
12120b5de56dSgjelinek  * its children are zvols.
12130b5de56dSgjelinek  *
12140b5de56dSgjelinek  * Note that we don't do anything with the 'zoned' property here.  All
12150b5de56dSgjelinek  * management is done in zoneadmd when the zone is actually rebooted.  This
12160b5de56dSgjelinek  * allows us to automatically set the zoned property even when a zone is
12170b5de56dSgjelinek  * rebooted by the administrator.
12180b5de56dSgjelinek  */
12190b5de56dSgjelinek int
12200b5de56dSgjelinek verify_datasets(zone_dochandle_t handle)
12210b5de56dSgjelinek {
12220b5de56dSgjelinek 	int return_code = Z_OK;
12230b5de56dSgjelinek 	struct zone_dstab dstab;
12240b5de56dSgjelinek 	zfs_handle_t *zhp;
12250b5de56dSgjelinek 	char propbuf[ZFS_MAXPROPLEN];
12260b5de56dSgjelinek 	char source[ZFS_MAXNAMELEN];
1227990b4856Slling 	zprop_source_t srctype;
12280b5de56dSgjelinek 
12290b5de56dSgjelinek 	if (zonecfg_setdsent(handle) != Z_OK) {
12300b5de56dSgjelinek 		/*
12310b5de56dSgjelinek 		 * TRANSLATION_NOTE
12320b5de56dSgjelinek 		 * zfs and dataset are literals that should not be translated.
12330b5de56dSgjelinek 		 */
12340b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify zfs datasets: "
12350b5de56dSgjelinek 		    "unable to enumerate datasets\n"));
12360b5de56dSgjelinek 		return (Z_ERR);
12370b5de56dSgjelinek 	}
12380b5de56dSgjelinek 
12390b5de56dSgjelinek 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
12400b5de56dSgjelinek 
124199653d4eSeschrock 		if ((zhp = zfs_open(g_zfs, dstab.zone_dataset_name,
12420b5de56dSgjelinek 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
124399653d4eSeschrock 			(void) fprintf(stderr, gettext("could not verify zfs "
124499653d4eSeschrock 			    "dataset %s: %s\n"), dstab.zone_dataset_name,
124599653d4eSeschrock 			    libzfs_error_description(g_zfs));
12460b5de56dSgjelinek 			return_code = Z_ERR;
12470b5de56dSgjelinek 			continue;
12480b5de56dSgjelinek 		}
12490b5de56dSgjelinek 
12500b5de56dSgjelinek 		if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf,
12510b5de56dSgjelinek 		    sizeof (propbuf), &srctype, source,
12520b5de56dSgjelinek 		    sizeof (source), 0) == 0 &&
1253990b4856Slling 		    (srctype == ZPROP_SRC_INHERITED)) {
12540b5de56dSgjelinek 			(void) fprintf(stderr, gettext("could not verify zfs "
12550b5de56dSgjelinek 			    "dataset %s: mountpoint cannot be inherited\n"),
12560b5de56dSgjelinek 			    dstab.zone_dataset_name);
12570b5de56dSgjelinek 			return_code = Z_ERR;
12580b5de56dSgjelinek 			zfs_close(zhp);
12590b5de56dSgjelinek 			continue;
12600b5de56dSgjelinek 		}
12610b5de56dSgjelinek 
12620b5de56dSgjelinek 		if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
12630b5de56dSgjelinek 			(void) fprintf(stderr, gettext("cannot verify zfs "
12640b5de56dSgjelinek 			    "dataset %s: volumes cannot be specified as a "
12650b5de56dSgjelinek 			    "zone dataset resource\n"),
12660b5de56dSgjelinek 			    dstab.zone_dataset_name);
12670b5de56dSgjelinek 			return_code = Z_ERR;
12680b5de56dSgjelinek 		}
12690b5de56dSgjelinek 
12700b5de56dSgjelinek 		if (zfs_iter_children(zhp, check_zvol, NULL) != 0)
12710b5de56dSgjelinek 			return_code = Z_ERR;
12720b5de56dSgjelinek 
12730b5de56dSgjelinek 		zfs_close(zhp);
12740b5de56dSgjelinek 	}
12750b5de56dSgjelinek 	(void) zonecfg_enddsent(handle);
12760b5de56dSgjelinek 
12770b5de56dSgjelinek 	return (return_code);
12780b5de56dSgjelinek }
12790b5de56dSgjelinek 
12800b5de56dSgjelinek /*
12810b5de56dSgjelinek  * Verify that the ZFS dataset exists, and its mountpoint
12820b5de56dSgjelinek  * property is set to "legacy".
12830b5de56dSgjelinek  */
12840b5de56dSgjelinek int
12850b5de56dSgjelinek verify_fs_zfs(struct zone_fstab *fstab)
12860b5de56dSgjelinek {
12870b5de56dSgjelinek 	zfs_handle_t *zhp;
12880b5de56dSgjelinek 	char propbuf[ZFS_MAXPROPLEN];
12890b5de56dSgjelinek 
129099653d4eSeschrock 	if ((zhp = zfs_open(g_zfs, fstab->zone_fs_special,
1291990b4856Slling 	    ZFS_TYPE_DATASET)) == NULL) {
12920b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify fs %s: "
12930b5de56dSgjelinek 		    "could not access zfs dataset '%s'\n"),
12940b5de56dSgjelinek 		    fstab->zone_fs_dir, fstab->zone_fs_special);
12950b5de56dSgjelinek 		return (Z_ERR);
12960b5de56dSgjelinek 	}
12970b5de56dSgjelinek 
12980b5de56dSgjelinek 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
12990b5de56dSgjelinek 		(void) fprintf(stderr, gettext("cannot verify fs %s: "
13000b5de56dSgjelinek 		    "'%s' is not a file system\n"),
13010b5de56dSgjelinek 		    fstab->zone_fs_dir, fstab->zone_fs_special);
13020b5de56dSgjelinek 		zfs_close(zhp);
13030b5de56dSgjelinek 		return (Z_ERR);
13040b5de56dSgjelinek 	}
13050b5de56dSgjelinek 
13060b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, sizeof (propbuf),
13070b5de56dSgjelinek 	    NULL, NULL, 0, 0) != 0 || strcmp(propbuf, "legacy") != 0) {
13080b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify fs %s: "
13090b5de56dSgjelinek 		    "zfs '%s' mountpoint is not \"legacy\"\n"),
13100b5de56dSgjelinek 		    fstab->zone_fs_dir, fstab->zone_fs_special);
13110b5de56dSgjelinek 		zfs_close(zhp);
13120b5de56dSgjelinek 		return (Z_ERR);
13130b5de56dSgjelinek 	}
13140b5de56dSgjelinek 
13150b5de56dSgjelinek 	zfs_close(zhp);
131699653d4eSeschrock 	return (Z_OK);
131799653d4eSeschrock }
131899653d4eSeschrock 
13190094b373Sjv /*
13200094b373Sjv  * Destroy the specified mnttab structure that was created by mnttab_dup().
13210094b373Sjv  * NOTE: The structure's mnt_time field isn't freed.
13220094b373Sjv  */
13230094b373Sjv static void
13240094b373Sjv mnttab_destroy(struct mnttab *tabp)
13250094b373Sjv {
13260094b373Sjv 	assert(tabp != NULL);
13270094b373Sjv 
13280094b373Sjv 	free(tabp->mnt_mountp);
13290094b373Sjv 	free(tabp->mnt_special);
13300094b373Sjv 	free(tabp->mnt_fstype);
13310094b373Sjv 	free(tabp->mnt_mntopts);
13320094b373Sjv 	free(tabp);
13330094b373Sjv }
13340094b373Sjv 
13350094b373Sjv /*
13360094b373Sjv  * Duplicate the specified mnttab structure.  The mnt_mountp and mnt_time
13370094b373Sjv  * fields aren't duplicated.  This function returns a pointer to the new mnttab
13380094b373Sjv  * structure or NULL if an error occurred.  If an error occurs, then this
13390094b373Sjv  * function sets errno to reflect the error.  mnttab structures created by
13400094b373Sjv  * this function should be destroyed via mnttab_destroy().
13410094b373Sjv  */
13420094b373Sjv static struct mnttab *
13430094b373Sjv mnttab_dup(const struct mnttab *srcp)
13440094b373Sjv {
13450094b373Sjv 	struct mnttab *retval;
13460094b373Sjv 
13470094b373Sjv 	assert(srcp != NULL);
13480094b373Sjv 
13490094b373Sjv 	retval = (struct mnttab *)calloc(1, sizeof (*retval));
13500094b373Sjv 	if (retval == NULL) {
13510094b373Sjv 		errno = ENOMEM;
13520094b373Sjv 		return (NULL);
13530094b373Sjv 	}
13540094b373Sjv 	if (srcp->mnt_special != NULL) {
13550094b373Sjv 		retval->mnt_special = strdup(srcp->mnt_special);
13560094b373Sjv 		if (retval->mnt_special == NULL)
13570094b373Sjv 			goto err;
13580094b373Sjv 	}
13590094b373Sjv 	if (srcp->mnt_fstype != NULL) {
13600094b373Sjv 		retval->mnt_fstype = strdup(srcp->mnt_fstype);
13610094b373Sjv 		if (retval->mnt_fstype == NULL)
13620094b373Sjv 			goto err;
13630094b373Sjv 	}
13640094b373Sjv 	retval->mnt_mntopts = (char *)malloc(MAX_MNTOPT_STR * sizeof (char));
13650094b373Sjv 	if (retval->mnt_mntopts == NULL)
13660094b373Sjv 		goto err;
13670094b373Sjv 	if (srcp->mnt_mntopts != NULL) {
13680094b373Sjv 		if (strlcpy(retval->mnt_mntopts, srcp->mnt_mntopts,
13690094b373Sjv 		    MAX_MNTOPT_STR * sizeof (char)) >= MAX_MNTOPT_STR *
13700094b373Sjv 		    sizeof (char)) {
13710094b373Sjv 			mnttab_destroy(retval);
13720094b373Sjv 			errno = EOVERFLOW; /* similar to mount(2) behavior */
13730094b373Sjv 			return (NULL);
13740094b373Sjv 		}
13750094b373Sjv 	} else {
13760094b373Sjv 		retval->mnt_mntopts[0] = '\0';
13770094b373Sjv 	}
13780094b373Sjv 	return (retval);
13790094b373Sjv 
13800094b373Sjv err:
13810094b373Sjv 	mnttab_destroy(retval);
13820094b373Sjv 	errno = ENOMEM;
13830094b373Sjv 	return (NULL);
13840094b373Sjv }
13850094b373Sjv 
13860094b373Sjv /*
13870094b373Sjv  * Determine whether the specified ZFS dataset's mountpoint property is set
13880094b373Sjv  * to "legacy".  If the specified dataset does not have a legacy mountpoint,
13890094b373Sjv  * then the string pointer to which the mountpoint argument points is assigned
13900094b373Sjv  * a dynamically-allocated string containing the dataset's mountpoint
13910094b373Sjv  * property.  If the dataset's mountpoint property is "legacy" or a libzfs
13920094b373Sjv  * error occurs, then the string pointer to which the mountpoint argument
13930094b373Sjv  * points isn't modified.
13940094b373Sjv  *
13950094b373Sjv  * This function returns B_TRUE if it doesn't encounter any fatal errors.
13960094b373Sjv  * It returns B_FALSE if it encounters a fatal error and sets errno to the
13970094b373Sjv  * appropriate error code.
13980094b373Sjv  */
13990094b373Sjv static boolean_t
14000094b373Sjv get_zfs_non_legacy_mountpoint(const char *dataset_name, char **mountpoint)
14010094b373Sjv {
14020094b373Sjv 	zfs_handle_t *zhp;
14030094b373Sjv 	char propbuf[ZFS_MAXPROPLEN];
14040094b373Sjv 
14050094b373Sjv 	assert(dataset_name != NULL);
14060094b373Sjv 	assert(mountpoint != NULL);
14070094b373Sjv 
14080094b373Sjv 	if ((zhp = zfs_open(g_zfs, dataset_name, ZFS_TYPE_DATASET)) == NULL) {
14090094b373Sjv 		errno = EINVAL;
14100094b373Sjv 		return (B_FALSE);
14110094b373Sjv 	}
14120094b373Sjv 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, sizeof (propbuf),
14130094b373Sjv 	    NULL, NULL, 0, 0) != 0) {
14140094b373Sjv 		zfs_close(zhp);
14150094b373Sjv 		errno = EINVAL;
14160094b373Sjv 		return (B_FALSE);
14170094b373Sjv 	}
14180094b373Sjv 	zfs_close(zhp);
14190094b373Sjv 	if (strcmp(propbuf, "legacy") != 0) {
14200094b373Sjv 		if ((*mountpoint = strdup(propbuf)) == NULL) {
14210094b373Sjv 			errno = ENOMEM;
14220094b373Sjv 			return (B_FALSE);
14230094b373Sjv 		}
14240094b373Sjv 	}
14250094b373Sjv 	return (B_TRUE);
14260094b373Sjv }
14270094b373Sjv 
14280094b373Sjv 
14290094b373Sjv /*
14300094b373Sjv  * This zonecfg_find_mounts() callback records information about mounts of
14310094b373Sjv  * interest in a zonepath.  It also tallies the number of zone
14320094b373Sjv  * root overlay mounts and the number of unexpected mounts found.
14330094b373Sjv  * This function outputs errors using zerror() if it finds unexpected
14340094b373Sjv  * mounts.  cookiep should point to an initialized zone_mounts_t structure.
14350094b373Sjv  *
14360094b373Sjv  * This function returns zero on success and a nonzero value on failure.
14370094b373Sjv  */
14380094b373Sjv static int
14390094b373Sjv zone_mounts_cb(const struct mnttab *mountp, void *cookiep)
14400094b373Sjv {
14410094b373Sjv 	zone_mounts_t *mounts;
14420094b373Sjv 	const char *zone_mount_dir;
14430094b373Sjv 
14440094b373Sjv 	assert(mountp != NULL);
14450094b373Sjv 	assert(cookiep != NULL);
14460094b373Sjv 
14470094b373Sjv 	mounts = (zone_mounts_t *)cookiep;
14480094b373Sjv 	zone_mount_dir = mountp->mnt_mountp + mounts->zonepath_len;
14490094b373Sjv 	if (strcmp(zone_mount_dir, "/root") == 0) {
14500094b373Sjv 		/*
14510094b373Sjv 		 * Check for an overlay mount.  If we already detected a /root
14520094b373Sjv 		 * mount, then the current mount must be an overlay mount.
14530094b373Sjv 		 */
14540094b373Sjv 		if (mounts->root_mnttab != NULL) {
14550094b373Sjv 			mounts->num_root_overlay_mounts++;
14560094b373Sjv 			return (0);
14570094b373Sjv 		}
14580094b373Sjv 
14590094b373Sjv 		/*
14600094b373Sjv 		 * Store the root mount's mnttab information in the
14610094b373Sjv 		 * zone_mounts_t structure for future use.
14620094b373Sjv 		 */
14630094b373Sjv 		if ((mounts->root_mnttab = mnttab_dup(mountp)) == NULL) {
14640094b373Sjv 			zperror(cmd_to_str(CMD_MOVE), B_FALSE);
14650094b373Sjv 			return (-1);
14660094b373Sjv 		}
14670094b373Sjv 
14680094b373Sjv 		/*
14690094b373Sjv 		 * Determine if the filesystem is a ZFS filesystem with a
14700094b373Sjv 		 * non-legacy mountpoint.  If it is, then set the root
14710094b373Sjv 		 * filesystem's mnttab's mnt_mountp field to a non-NULL
14720094b373Sjv 		 * value, which will serve as a flag to indicate this special
14730094b373Sjv 		 * condition.
14740094b373Sjv 		 */
14750094b373Sjv 		if (strcmp(mountp->mnt_fstype, MNTTYPE_ZFS) == 0 &&
14760094b373Sjv 		    get_zfs_non_legacy_mountpoint(mountp->mnt_special,
14770094b373Sjv 		    &mounts->root_mnttab->mnt_mountp) != B_TRUE) {
14780094b373Sjv 			zperror(cmd_to_str(CMD_MOVE), B_FALSE);
14790094b373Sjv 			return (-1);
14800094b373Sjv 		}
14810094b373Sjv 	} else {
14820094b373Sjv 		/*
14830094b373Sjv 		 * An unexpected mount was found.  Notify the user.
14840094b373Sjv 		 */
14850094b373Sjv 		if (mounts->num_unexpected_mounts == 0)
14860094b373Sjv 			zerror(gettext("These file systems are mounted on "
14870094b373Sjv 			    "subdirectories of %s.\n"), mounts->zonepath);
14880094b373Sjv 		mounts->num_unexpected_mounts++;
14890094b373Sjv 		(void) zfm_print(mountp, NULL);
14900094b373Sjv 	}
14910094b373Sjv 	return (0);
14920094b373Sjv }
14930094b373Sjv 
14940094b373Sjv /*
14950094b373Sjv  * Initialize the specified zone_mounts_t structure for the given zonepath.
14960094b373Sjv  * If this function succeeds, it returns zero and the specified zone_mounts_t
14970094b373Sjv  * structure contains information about mounts in the specified zonepath.
14980094b373Sjv  * The function returns a nonzero value if it fails.  The zone_mounts_t
14990094b373Sjv  * structure doesn't need be destroyed via zone_mounts_destroy() if this
15000094b373Sjv  * function fails.
15010094b373Sjv  */
15020094b373Sjv int
15030094b373Sjv zone_mounts_init(zone_mounts_t *mounts, const char *zonepath)
15040094b373Sjv {
15050094b373Sjv 	assert(mounts != NULL);
15060094b373Sjv 	assert(zonepath != NULL);
15070094b373Sjv 
15080094b373Sjv 	bzero(mounts, sizeof (*mounts));
15090094b373Sjv 	if ((mounts->zonepath = strdup(zonepath)) == NULL) {
15100094b373Sjv 		zerror(gettext("the process ran out of memory while checking "
15110094b373Sjv 		    "for mounts in zonepath %s."), zonepath);
15120094b373Sjv 		return (-1);
15130094b373Sjv 	}
15140094b373Sjv 	mounts->zonepath_len = strlen(zonepath);
15150094b373Sjv 	if (zonecfg_find_mounts((char *)zonepath, zone_mounts_cb, mounts) ==
15160094b373Sjv 	    -1) {
15170094b373Sjv 		zerror(gettext("an error occurred while checking for mounts "
15180094b373Sjv 		    "in zonepath %s."), zonepath);
15190094b373Sjv 		zone_mounts_destroy(mounts);
15200094b373Sjv 		return (-1);
15210094b373Sjv 	}
15220094b373Sjv 	return (0);
15230094b373Sjv }
15240094b373Sjv 
15250094b373Sjv /*
15260094b373Sjv  * Destroy the memory used by the specified zone_mounts_t structure's fields.
15270094b373Sjv  * This function doesn't free the memory occupied by the structure itself
15280094b373Sjv  * (i.e., it doesn't free the parameter).
15290094b373Sjv  */
15300094b373Sjv void
15310094b373Sjv zone_mounts_destroy(zone_mounts_t *mounts)
15320094b373Sjv {
15330094b373Sjv 	assert(mounts != NULL);
15340094b373Sjv 
15350094b373Sjv 	free(mounts->zonepath);
15360094b373Sjv 	if (mounts->root_mnttab != NULL)
15370094b373Sjv 		mnttab_destroy(mounts->root_mnttab);
15380094b373Sjv }
15390094b373Sjv 
15400094b373Sjv /*
15410094b373Sjv  * Mount a moving zone's root filesystem (if it had a root filesystem mount
15420094b373Sjv  * prior to the move) using the specified zonepath.  mounts should refer to
15430094b373Sjv  * the zone_mounts_t structure describing the zone's mount information.
15440094b373Sjv  *
15450094b373Sjv  * This function returns zero if the mount succeeds and a nonzero value
15460094b373Sjv  * if it doesn't.
15470094b373Sjv  */
15480094b373Sjv int
15490094b373Sjv zone_mount_rootfs(zone_mounts_t *mounts, const char *zonepath)
15500094b373Sjv {
15510094b373Sjv 	char zoneroot[MAXPATHLEN];
15520094b373Sjv 	struct mnttab *mtab;
15530094b373Sjv 	int flags;
15540094b373Sjv 
15550094b373Sjv 	assert(mounts != NULL);
15560094b373Sjv 	assert(zonepath != NULL);
15570094b373Sjv 
15580094b373Sjv 	/*
15590094b373Sjv 	 * If there isn't a root filesystem, then don't do anything.
15600094b373Sjv 	 */
15610094b373Sjv 	mtab = mounts->root_mnttab;
15620094b373Sjv 	if (mtab == NULL)
15630094b373Sjv 		return (0);
15640094b373Sjv 
15650094b373Sjv 	/*
15660094b373Sjv 	 * Determine the root filesystem's new mountpoint.
15670094b373Sjv 	 */
15680094b373Sjv 	if (snprintf(zoneroot, sizeof (zoneroot), "%s/root", zonepath) >=
15690094b373Sjv 	    sizeof (zoneroot)) {
15700094b373Sjv 		zerror(gettext("Zonepath %s is too long.\n"), zonepath);
15710094b373Sjv 		return (-1);
15720094b373Sjv 	}
15730094b373Sjv 
15740094b373Sjv 	/*
15750094b373Sjv 	 * If the root filesystem is a non-legacy ZFS filesystem (i.e., if it's
15760094b373Sjv 	 * mnt_mountp field is non-NULL), then make the filesystem's new
15770094b373Sjv 	 * mount point its mountpoint property and mount the filesystem.
15780094b373Sjv 	 */
15790094b373Sjv 	if (mtab->mnt_mountp != NULL) {
15800094b373Sjv 		zfs_handle_t *zhp;
15810094b373Sjv 
15820094b373Sjv 		if ((zhp = zfs_open(g_zfs, mtab->mnt_special,
15830094b373Sjv 		    ZFS_TYPE_DATASET)) == NULL) {
15840094b373Sjv 			zerror(gettext("could not get ZFS handle for the zone's"
15850094b373Sjv 			    " root filesystem"));
15860094b373Sjv 			return (-1);
15870094b373Sjv 		}
15880094b373Sjv 		if (zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
15890094b373Sjv 		    zoneroot) != 0) {
15900094b373Sjv 			zerror(gettext("could not modify zone's root "
15910094b373Sjv 			    "filesystem's mountpoint property"));
15920094b373Sjv 			zfs_close(zhp);
15930094b373Sjv 			return (-1);
15940094b373Sjv 		}
15950094b373Sjv 		if (zfs_mount(zhp, mtab->mnt_mntopts, 0) != 0) {
15960094b373Sjv 			zerror(gettext("unable to mount zone root %s: %s"),
15970094b373Sjv 			    zoneroot, libzfs_error_description(g_zfs));
15980094b373Sjv 			if (zfs_prop_set(zhp,
15990094b373Sjv 			    zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
16000094b373Sjv 			    mtab->mnt_mountp) != 0)
16010094b373Sjv 				zerror(gettext("unable to restore zone's root "
16020094b373Sjv 				    "filesystem's mountpoint property"));
16030094b373Sjv 			zfs_close(zhp);
16040094b373Sjv 			return (-1);
16050094b373Sjv 		}
16060094b373Sjv 		zfs_close(zhp);
16070094b373Sjv 		return (0);
16080094b373Sjv 	}
16090094b373Sjv 
16100094b373Sjv 	/*
16110094b373Sjv 	 * The root filesystem is either a legacy-mounted ZFS filesystem or
16120094b373Sjv 	 * a non-ZFS filesystem.  Use mount(2) to mount the root filesystem.
16130094b373Sjv 	 */
16140094b373Sjv 	if (mtab->mnt_mntopts != NULL)
16150094b373Sjv 		flags = MS_OPTIONSTR;
16160094b373Sjv 	else
16170094b373Sjv 		flags = 0;
16180094b373Sjv 	if (mount(mtab->mnt_special, zoneroot, flags, mtab->mnt_fstype, NULL, 0,
16190094b373Sjv 	    mtab->mnt_mntopts, MAX_MNTOPT_STR * sizeof (char)) != 0) {
16200094b373Sjv 		flags = errno;
16210094b373Sjv 		zerror(gettext("unable to mount zone root %s: %s"), zoneroot,
16220094b373Sjv 		    strerror(flags));
16230094b373Sjv 		return (-1);
16240094b373Sjv 	}
16250094b373Sjv 	return (0);
16260094b373Sjv }
16270094b373Sjv 
16280094b373Sjv /*
16290094b373Sjv  * Unmount a moving zone's root filesystem (if such a mount exists) using the
16300094b373Sjv  * specified zonepath.  mounts should refer to the zone_mounts_t structure
16310094b373Sjv  * describing the zone's mount information.  If force is B_TRUE, then if the
16320094b373Sjv  * unmount fails, then the function will try to forcibly unmount the zone's root
16330094b373Sjv  * filesystem.
16340094b373Sjv  *
16350094b373Sjv  * This function returns zero if the unmount (forced or otherwise) succeeds;
16360094b373Sjv  * otherwise, it returns a nonzero value.
16370094b373Sjv  */
16380094b373Sjv int
16390094b373Sjv zone_unmount_rootfs(zone_mounts_t *mounts, const char *zonepath,
16400094b373Sjv     boolean_t force)
16410094b373Sjv {
16420094b373Sjv 	char zoneroot[MAXPATHLEN];
16430094b373Sjv 	struct mnttab *mtab;
16440094b373Sjv 	int err;
16450094b373Sjv 
16460094b373Sjv 	assert(mounts != NULL);
16470094b373Sjv 	assert(zonepath != NULL);
16480094b373Sjv 
16490094b373Sjv 	/*
16500094b373Sjv 	 * If there isn't a root filesystem, then don't do anything.
16510094b373Sjv 	 */
16520094b373Sjv 	mtab = mounts->root_mnttab;
16530094b373Sjv 	if (mtab == NULL)
16540094b373Sjv 		return (0);
16550094b373Sjv 
16560094b373Sjv 	/*
16570094b373Sjv 	 * Determine the root filesystem's mountpoint.
16580094b373Sjv 	 */
16590094b373Sjv 	if (snprintf(zoneroot, sizeof (zoneroot), "%s/root", zonepath) >=
16600094b373Sjv 	    sizeof (zoneroot)) {
16610094b373Sjv 		zerror(gettext("Zonepath %s is too long.\n"), zonepath);
16620094b373Sjv 		return (-1);
16630094b373Sjv 	}
16640094b373Sjv 
16650094b373Sjv 	/*
16660094b373Sjv 	 * If the root filesystem is a non-legacy ZFS fileystem, then unmount
16670094b373Sjv 	 * the filesystem via libzfs.
16680094b373Sjv 	 */
16690094b373Sjv 	if (mtab->mnt_mountp != NULL) {
16700094b373Sjv 		zfs_handle_t *zhp;
16710094b373Sjv 
16720094b373Sjv 		if ((zhp = zfs_open(g_zfs, mtab->mnt_special,
16730094b373Sjv 		    ZFS_TYPE_DATASET)) == NULL) {
16740094b373Sjv 			zerror(gettext("could not get ZFS handle for the zone's"
16750094b373Sjv 			    " root filesystem"));
16760094b373Sjv 			return (-1);
16770094b373Sjv 		}
16780094b373Sjv 		if (zfs_unmount(zhp, zoneroot, 0) != 0) {
16790094b373Sjv 			if (force && zfs_unmount(zhp, zoneroot, MS_FORCE) ==
16800094b373Sjv 			    0) {
16810094b373Sjv 				zfs_close(zhp);
16820094b373Sjv 				return (0);
16830094b373Sjv 			}
16840094b373Sjv 			zerror(gettext("unable to unmount zone root %s: %s"),
16850094b373Sjv 			    zoneroot, libzfs_error_description(g_zfs));
16860094b373Sjv 			zfs_close(zhp);
16870094b373Sjv 			return (-1);
16880094b373Sjv 		}
16890094b373Sjv 		zfs_close(zhp);
16900094b373Sjv 		return (0);
16910094b373Sjv 	}
16920094b373Sjv 
16930094b373Sjv 	/*
16940094b373Sjv 	 * Use umount(2) to unmount the root filesystem.  If this fails, then
16950094b373Sjv 	 * forcibly unmount it if the force flag is set.
16960094b373Sjv 	 */
16970094b373Sjv 	if (umount(zoneroot) != 0) {
16980094b373Sjv 		if (force && umount2(zoneroot, MS_FORCE) == 0)
16990094b373Sjv 			return (0);
17000094b373Sjv 		err = errno;
17010094b373Sjv 		zerror(gettext("unable to unmount zone root %s: %s"), zoneroot,
17020094b373Sjv 		    strerror(err));
17030094b373Sjv 		return (-1);
17040094b373Sjv 	}
17050094b373Sjv 	return (0);
17060094b373Sjv }
17070094b373Sjv 
170899653d4eSeschrock int
170999653d4eSeschrock init_zfs(void)
171099653d4eSeschrock {
171199653d4eSeschrock 	if ((g_zfs = libzfs_init()) == NULL) {
171299653d4eSeschrock 		(void) fprintf(stderr, gettext("failed to initialize ZFS "
171399653d4eSeschrock 		    "library\n"));
171499653d4eSeschrock 		return (Z_ERR);
171599653d4eSeschrock 	}
171699653d4eSeschrock 
17170b5de56dSgjelinek 	return (Z_OK);
17180b5de56dSgjelinek }
1719