xref: /titanic_54/usr/src/cmd/zoneadm/zfs.c (revision ff17c8bf86c3e567734be83f90267edee20f580f)
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 /*
23*ff17c8bfSgjelinek  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240b5de56dSgjelinek  * Use is subject to license terms.
250b5de56dSgjelinek  */
260b5de56dSgjelinek 
270b5de56dSgjelinek #pragma ident	"%Z%%M%	%I%	%E% SMI"
280b5de56dSgjelinek 
290b5de56dSgjelinek /*
300b5de56dSgjelinek  * This file contains the functions used to support the ZFS integration
310b5de56dSgjelinek  * with zones.  This includes validation (e.g. zonecfg dataset), cloning,
320b5de56dSgjelinek  * file system creation and destruction.
330b5de56dSgjelinek  */
340b5de56dSgjelinek 
350b5de56dSgjelinek #include <stdio.h>
360b5de56dSgjelinek #include <errno.h>
370b5de56dSgjelinek #include <unistd.h>
380b5de56dSgjelinek #include <string.h>
390b5de56dSgjelinek #include <locale.h>
400b5de56dSgjelinek #include <libintl.h>
410b5de56dSgjelinek #include <sys/stat.h>
420b5de56dSgjelinek #include <sys/statvfs.h>
430b5de56dSgjelinek #include <libgen.h>
440b5de56dSgjelinek #include <libzonecfg.h>
450b5de56dSgjelinek #include <sys/mnttab.h>
460b5de56dSgjelinek #include <libzfs.h>
470b5de56dSgjelinek 
480b5de56dSgjelinek #include "zoneadm.h"
490b5de56dSgjelinek 
5099653d4eSeschrock libzfs_handle_t *g_zfs;
510b5de56dSgjelinek 
520b5de56dSgjelinek typedef struct zfs_mount_data {
530b5de56dSgjelinek 	char		*match_name;
540b5de56dSgjelinek 	zfs_handle_t	*match_handle;
550b5de56dSgjelinek } zfs_mount_data_t;
560b5de56dSgjelinek 
570b5de56dSgjelinek typedef struct zfs_snapshot_data {
580b5de56dSgjelinek 	char	*match_name;
590b5de56dSgjelinek 	int	len;
600b5de56dSgjelinek 	int	max;
610b5de56dSgjelinek } zfs_snapshot_data_t;
620b5de56dSgjelinek 
630b5de56dSgjelinek /*
640b5de56dSgjelinek  * A ZFS file system iterator call-back function which is used to validate
650b5de56dSgjelinek  * datasets imported into the zone.
660b5de56dSgjelinek  */
670b5de56dSgjelinek /* ARGSUSED */
680b5de56dSgjelinek static int
690b5de56dSgjelinek check_zvol(zfs_handle_t *zhp, void *unused)
700b5de56dSgjelinek {
710b5de56dSgjelinek 	int ret;
720b5de56dSgjelinek 
730b5de56dSgjelinek 	if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
740b5de56dSgjelinek 		/*
750b5de56dSgjelinek 		 * TRANSLATION_NOTE
760b5de56dSgjelinek 		 * zfs and dataset are literals that should not be translated.
770b5de56dSgjelinek 		 */
780b5de56dSgjelinek 		(void) fprintf(stderr, gettext("cannot verify zfs dataset %s: "
790b5de56dSgjelinek 		    "volumes cannot be specified as a zone dataset resource\n"),
800b5de56dSgjelinek 		    zfs_get_name(zhp));
810b5de56dSgjelinek 		ret = -1;
820b5de56dSgjelinek 	} else {
830b5de56dSgjelinek 		ret = zfs_iter_children(zhp, check_zvol, NULL);
840b5de56dSgjelinek 	}
850b5de56dSgjelinek 
860b5de56dSgjelinek 	zfs_close(zhp);
870b5de56dSgjelinek 
880b5de56dSgjelinek 	return (ret);
890b5de56dSgjelinek }
900b5de56dSgjelinek 
910b5de56dSgjelinek /*
920b5de56dSgjelinek  * A ZFS file system iterator call-back function which returns the
930b5de56dSgjelinek  * zfs_handle_t for a ZFS file system on the specified mount point.
940b5de56dSgjelinek  */
950b5de56dSgjelinek static int
960b5de56dSgjelinek match_mountpoint(zfs_handle_t *zhp, void *data)
970b5de56dSgjelinek {
980b5de56dSgjelinek 	int			res;
990b5de56dSgjelinek 	zfs_mount_data_t	*cbp;
1000b5de56dSgjelinek 	char			mp[ZFS_MAXPROPLEN];
1010b5de56dSgjelinek 
1020b5de56dSgjelinek 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
1030b5de56dSgjelinek 		zfs_close(zhp);
1040b5de56dSgjelinek 		return (0);
1050b5de56dSgjelinek 	}
1060b5de56dSgjelinek 
1070b5de56dSgjelinek 	cbp = (zfs_mount_data_t *)data;
1080b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mp, sizeof (mp), NULL, NULL,
10999653d4eSeschrock 	    0, B_FALSE) == 0 && strcmp(mp, cbp->match_name) == 0) {
1100b5de56dSgjelinek 		cbp->match_handle = zhp;
1110b5de56dSgjelinek 		return (1);
1120b5de56dSgjelinek 	}
1130b5de56dSgjelinek 
1140b5de56dSgjelinek 	res = zfs_iter_filesystems(zhp, match_mountpoint, data);
1150b5de56dSgjelinek 	zfs_close(zhp);
1160b5de56dSgjelinek 	return (res);
1170b5de56dSgjelinek }
1180b5de56dSgjelinek 
1190b5de56dSgjelinek /*
1200b5de56dSgjelinek  * Get ZFS handle for the specified mount point.
1210b5de56dSgjelinek  */
1220b5de56dSgjelinek static zfs_handle_t *
1230b5de56dSgjelinek mount2zhandle(char *mountpoint)
1240b5de56dSgjelinek {
1250b5de56dSgjelinek 	zfs_mount_data_t	cb;
1260b5de56dSgjelinek 
1270b5de56dSgjelinek 	cb.match_name = mountpoint;
1280b5de56dSgjelinek 	cb.match_handle = NULL;
12999653d4eSeschrock 	(void) zfs_iter_root(g_zfs, match_mountpoint, &cb);
1300b5de56dSgjelinek 	return (cb.match_handle);
1310b5de56dSgjelinek }
1320b5de56dSgjelinek 
1330b5de56dSgjelinek /*
1340b5de56dSgjelinek  * Check if there is already a file system (zfs or any other type) mounted on
1350b5de56dSgjelinek  * path.
1360b5de56dSgjelinek  */
1370b5de56dSgjelinek static boolean_t
1380b5de56dSgjelinek is_mountpnt(char *path)
1390b5de56dSgjelinek {
1400b5de56dSgjelinek 	FILE		*fp;
1410b5de56dSgjelinek 	struct mnttab	entry;
1420b5de56dSgjelinek 
1430b5de56dSgjelinek 	if ((fp = fopen("/etc/mnttab", "r")) == NULL)
1440b5de56dSgjelinek 		return (B_FALSE);
1450b5de56dSgjelinek 
1460b5de56dSgjelinek 	while (getmntent(fp, &entry) == 0) {
1470b5de56dSgjelinek 		if (strcmp(path, entry.mnt_mountp) == 0) {
1480b5de56dSgjelinek 			(void) fclose(fp);
1490b5de56dSgjelinek 			return (B_TRUE);
1500b5de56dSgjelinek 		}
1510b5de56dSgjelinek 	}
1520b5de56dSgjelinek 
1530b5de56dSgjelinek 	(void) fclose(fp);
1540b5de56dSgjelinek 	return (B_FALSE);
1550b5de56dSgjelinek }
1560b5de56dSgjelinek 
1570b5de56dSgjelinek /*
158*ff17c8bfSgjelinek  * Run the brand's pre-snapshot hook before we take a ZFS snapshot of the zone.
1590b5de56dSgjelinek  */
1600b5de56dSgjelinek static int
161*ff17c8bfSgjelinek pre_snapshot(char *presnapbuf)
1620b5de56dSgjelinek {
163*ff17c8bfSgjelinek 	int status;
1640b5de56dSgjelinek 
165*ff17c8bfSgjelinek 	/* No brand-specific handler */
166*ff17c8bfSgjelinek 	if (presnapbuf[0] == '\0')
167*ff17c8bfSgjelinek 		return (Z_OK);
168*ff17c8bfSgjelinek 
169*ff17c8bfSgjelinek 	/* Run the hook */
170*ff17c8bfSgjelinek 	status = do_subproc_interactive(presnapbuf);
171*ff17c8bfSgjelinek 	if ((status = subproc_status(gettext("brand-specific presnapshot"),
172*ff17c8bfSgjelinek 	    status, B_FALSE)) != ZONE_SUBPROC_OK)
1730b5de56dSgjelinek 		return (Z_ERR);
1740b5de56dSgjelinek 
1750b5de56dSgjelinek 	return (Z_OK);
1760b5de56dSgjelinek }
1770b5de56dSgjelinek 
1780b5de56dSgjelinek /*
179*ff17c8bfSgjelinek  * Run the brand's post-snapshot hook after we take a ZFS snapshot of the zone.
1800b5de56dSgjelinek  */
1810b5de56dSgjelinek static int
182*ff17c8bfSgjelinek post_snapshot(char *postsnapbuf)
1830b5de56dSgjelinek {
184*ff17c8bfSgjelinek 	int status;
1850b5de56dSgjelinek 
186*ff17c8bfSgjelinek 	/* No brand-specific handler */
187*ff17c8bfSgjelinek 	if (postsnapbuf[0] == '\0')
188*ff17c8bfSgjelinek 		return (Z_OK);
189*ff17c8bfSgjelinek 
190*ff17c8bfSgjelinek 	/* Run the hook */
191*ff17c8bfSgjelinek 	status = do_subproc_interactive(postsnapbuf);
192*ff17c8bfSgjelinek 	if ((status = subproc_status(gettext("brand-specific postsnapshot"),
193*ff17c8bfSgjelinek 	    status, B_FALSE)) != ZONE_SUBPROC_OK)
1940b5de56dSgjelinek 		return (Z_ERR);
1950b5de56dSgjelinek 
1960b5de56dSgjelinek 	return (Z_OK);
1970b5de56dSgjelinek }
1980b5de56dSgjelinek 
1990b5de56dSgjelinek /*
2000b5de56dSgjelinek  * This is a ZFS snapshot iterator call-back function which returns the
2010b5de56dSgjelinek  * highest number of SUNWzone snapshots that have been taken.
2020b5de56dSgjelinek  */
2030b5de56dSgjelinek static int
2040b5de56dSgjelinek get_snap_max(zfs_handle_t *zhp, void *data)
2050b5de56dSgjelinek {
2060b5de56dSgjelinek 	int			res;
2070b5de56dSgjelinek 	zfs_snapshot_data_t	*cbp;
2080b5de56dSgjelinek 
2090b5de56dSgjelinek 	if (zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) {
2100b5de56dSgjelinek 		zfs_close(zhp);
2110b5de56dSgjelinek 		return (0);
2120b5de56dSgjelinek 	}
2130b5de56dSgjelinek 
2140b5de56dSgjelinek 	cbp = (zfs_snapshot_data_t *)data;
2150b5de56dSgjelinek 
2160b5de56dSgjelinek 	if (strncmp(zfs_get_name(zhp), cbp->match_name, cbp->len) == 0) {
2170b5de56dSgjelinek 		char	*nump;
2180b5de56dSgjelinek 		int	num;
2190b5de56dSgjelinek 
2200b5de56dSgjelinek 		nump = (char *)(zfs_get_name(zhp) + cbp->len);
2210b5de56dSgjelinek 		num = atoi(nump);
2220b5de56dSgjelinek 		if (num > cbp->max)
2230b5de56dSgjelinek 			cbp->max = num;
2240b5de56dSgjelinek 	}
2250b5de56dSgjelinek 
2260b5de56dSgjelinek 	res = zfs_iter_snapshots(zhp, get_snap_max, data);
2270b5de56dSgjelinek 	zfs_close(zhp);
2280b5de56dSgjelinek 	return (res);
2290b5de56dSgjelinek }
2300b5de56dSgjelinek 
2310b5de56dSgjelinek /*
2320b5de56dSgjelinek  * Take a ZFS snapshot to be used for cloning the zone.
2330b5de56dSgjelinek  */
2340b5de56dSgjelinek static int
235*ff17c8bfSgjelinek take_snapshot(zfs_handle_t *zhp, char *snapshot_name, int snap_size,
236*ff17c8bfSgjelinek     char *presnapbuf, char *postsnapbuf)
2370b5de56dSgjelinek {
2380b5de56dSgjelinek 	int			res;
2390b5de56dSgjelinek 	char			template[ZFS_MAXNAMELEN];
2400b5de56dSgjelinek 	zfs_snapshot_data_t	cb;
2410b5de56dSgjelinek 
2420b5de56dSgjelinek 	/*
2430b5de56dSgjelinek 	 * First we need to figure out the next available name for the
2440b5de56dSgjelinek 	 * zone snapshot.  Look through the list of zones snapshots for
2450b5de56dSgjelinek 	 * this file system to determine the maximum snapshot name.
2460b5de56dSgjelinek 	 */
2470b5de56dSgjelinek 	if (snprintf(template, sizeof (template), "%s@SUNWzone",
2480b5de56dSgjelinek 	    zfs_get_name(zhp)) >=  sizeof (template))
2490b5de56dSgjelinek 		return (Z_ERR);
2500b5de56dSgjelinek 
2510b5de56dSgjelinek 	cb.match_name = template;
2520b5de56dSgjelinek 	cb.len = strlen(template);
2530b5de56dSgjelinek 	cb.max = 0;
2540b5de56dSgjelinek 
2550b5de56dSgjelinek 	if (zfs_iter_snapshots(zhp, get_snap_max, &cb) != 0)
2560b5de56dSgjelinek 		return (Z_ERR);
2570b5de56dSgjelinek 
2580b5de56dSgjelinek 	cb.max++;
2590b5de56dSgjelinek 
2600b5de56dSgjelinek 	if (snprintf(snapshot_name, snap_size, "%s@SUNWzone%d",
2610b5de56dSgjelinek 	    zfs_get_name(zhp), cb.max) >= snap_size)
2620b5de56dSgjelinek 		return (Z_ERR);
2630b5de56dSgjelinek 
264*ff17c8bfSgjelinek 	if (pre_snapshot(presnapbuf) != Z_OK)
2650b5de56dSgjelinek 		return (Z_ERR);
2661d452cf5Sahrens 	res = zfs_snapshot(g_zfs, snapshot_name, B_FALSE);
267*ff17c8bfSgjelinek 	if (post_snapshot(postsnapbuf) != Z_OK)
2680b5de56dSgjelinek 		return (Z_ERR);
2690b5de56dSgjelinek 
2700b5de56dSgjelinek 	if (res != 0)
2710b5de56dSgjelinek 		return (Z_ERR);
2720b5de56dSgjelinek 	return (Z_OK);
2730b5de56dSgjelinek }
2740b5de56dSgjelinek 
2750b5de56dSgjelinek /*
2760b5de56dSgjelinek  * We are using an explicit snapshot from some earlier point in time so
277*ff17c8bfSgjelinek  * we need to validate it.  Run the brand specific hook.
2780b5de56dSgjelinek  */
2790b5de56dSgjelinek static int
280*ff17c8bfSgjelinek validate_snapshot(char *snapshot_name, char *snap_path, char *validsnapbuf)
2810b5de56dSgjelinek {
282*ff17c8bfSgjelinek 	int status;
283*ff17c8bfSgjelinek 	char cmdbuf[MAXPATHLEN];
2840b5de56dSgjelinek 
285*ff17c8bfSgjelinek 	/* No brand-specific handler */
286*ff17c8bfSgjelinek 	if (validsnapbuf[0] == '\0')
287*ff17c8bfSgjelinek 		return (Z_OK);
288*ff17c8bfSgjelinek 
289*ff17c8bfSgjelinek 	/* pass args - snapshot_name & snap_path */
290*ff17c8bfSgjelinek 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %s %s", validsnapbuf,
291*ff17c8bfSgjelinek 	    snapshot_name, snap_path) >= sizeof (cmdbuf)) {
292*ff17c8bfSgjelinek 		zerror("Command line too long");
2930b5de56dSgjelinek 		return (Z_ERR);
2940b5de56dSgjelinek 	}
2950b5de56dSgjelinek 
296*ff17c8bfSgjelinek 	/* Run the hook */
297*ff17c8bfSgjelinek 	status = do_subproc_interactive(cmdbuf);
298*ff17c8bfSgjelinek 	if ((status = subproc_status(gettext("brand-specific validatesnapshot"),
299*ff17c8bfSgjelinek 	    status, B_FALSE)) != ZONE_SUBPROC_OK)
3000b5de56dSgjelinek 		return (Z_ERR);
3010b5de56dSgjelinek 
302*ff17c8bfSgjelinek 	return (Z_OK);
3030b5de56dSgjelinek }
3040b5de56dSgjelinek 
3050b5de56dSgjelinek /*
3060b5de56dSgjelinek  * Remove the sw inventory file from inside this zonepath that we picked up out
3070b5de56dSgjelinek  * of the snapshot.
3080b5de56dSgjelinek  */
3090b5de56dSgjelinek static int
3100b5de56dSgjelinek clean_out_clone()
3110b5de56dSgjelinek {
3120b5de56dSgjelinek 	int err;
3130b5de56dSgjelinek 	zone_dochandle_t handle;
3140b5de56dSgjelinek 
3150b5de56dSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
3160b5de56dSgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3170b5de56dSgjelinek 		return (Z_ERR);
3180b5de56dSgjelinek 	}
3190b5de56dSgjelinek 
3200b5de56dSgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
3210b5de56dSgjelinek 		errno = err;
3220b5de56dSgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3230b5de56dSgjelinek 		zonecfg_fini_handle(handle);
3240b5de56dSgjelinek 		return (Z_ERR);
3250b5de56dSgjelinek 	}
3260b5de56dSgjelinek 
3270b5de56dSgjelinek 	zonecfg_rm_detached(handle, B_FALSE);
3280b5de56dSgjelinek 	zonecfg_fini_handle(handle);
3290b5de56dSgjelinek 
3300b5de56dSgjelinek 	return (Z_OK);
3310b5de56dSgjelinek }
3320b5de56dSgjelinek 
3330b5de56dSgjelinek /*
3340b5de56dSgjelinek  * Make a ZFS clone on zonepath from snapshot_name.
3350b5de56dSgjelinek  */
3360b5de56dSgjelinek static int
3370b5de56dSgjelinek clone_snap(char *snapshot_name, char *zonepath)
3380b5de56dSgjelinek {
3390b5de56dSgjelinek 	int		res = Z_OK;
3400b5de56dSgjelinek 	int		err;
3410b5de56dSgjelinek 	zfs_handle_t	*zhp;
3420b5de56dSgjelinek 	zfs_handle_t	*clone;
343e9dbad6fSeschrock 	nvlist_t	*props = NULL;
3440b5de56dSgjelinek 
34599653d4eSeschrock 	if ((zhp = zfs_open(g_zfs, snapshot_name, ZFS_TYPE_SNAPSHOT)) == NULL)
3460b5de56dSgjelinek 		return (Z_NO_ENTRY);
3470b5de56dSgjelinek 
3480b5de56dSgjelinek 	(void) printf(gettext("Cloning snapshot %s\n"), snapshot_name);
3490b5de56dSgjelinek 
350e9dbad6fSeschrock 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0 ||
3515f8e1617Snn35248 	    nvlist_add_string(props, zfs_prop_to_name(ZFS_PROP_SHARENFS),
3525f8e1617Snn35248 	    "off") != 0) {
3535f8e1617Snn35248 		if (props != NULL)
354e9dbad6fSeschrock 			nvlist_free(props);
355e9dbad6fSeschrock 		(void) fprintf(stderr, gettext("could not create ZFS clone "
356e9dbad6fSeschrock 		    "%s: out of memory\n"), zonepath);
357e9dbad6fSeschrock 		return (Z_ERR);
358e9dbad6fSeschrock 	}
359e9dbad6fSeschrock 
360e9dbad6fSeschrock 	err = zfs_clone(zhp, zonepath, props);
3610b5de56dSgjelinek 	zfs_close(zhp);
362e9dbad6fSeschrock 
363e9dbad6fSeschrock 	nvlist_free(props);
364e9dbad6fSeschrock 
3650b5de56dSgjelinek 	if (err != 0)
3660b5de56dSgjelinek 		return (Z_ERR);
3670b5de56dSgjelinek 
3680b5de56dSgjelinek 	/* create the mountpoint if necessary */
369990b4856Slling 	if ((clone = zfs_open(g_zfs, zonepath, ZFS_TYPE_DATASET)) == NULL)
3700b5de56dSgjelinek 		return (Z_ERR);
3710b5de56dSgjelinek 
3720b5de56dSgjelinek 	/*
3730b5de56dSgjelinek 	 * The clone has been created so we need to print a diagnostic
3740b5de56dSgjelinek 	 * message if one of the following steps fails for some reason.
3750b5de56dSgjelinek 	 */
3760b5de56dSgjelinek 	if (zfs_mount(clone, NULL, 0) != 0) {
3770b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not mount ZFS clone "
3780b5de56dSgjelinek 		    "%s\n"), zfs_get_name(clone));
3790b5de56dSgjelinek 		res = Z_ERR;
3800b5de56dSgjelinek 
381e9dbad6fSeschrock 	} else if (clean_out_clone() != Z_OK) {
3820b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not remove the "
3830b5de56dSgjelinek 		    "software inventory from ZFS clone %s\n"),
3840b5de56dSgjelinek 		    zfs_get_name(clone));
3850b5de56dSgjelinek 		res = Z_ERR;
3860b5de56dSgjelinek 	}
3870b5de56dSgjelinek 
3880b5de56dSgjelinek 	zfs_close(clone);
3890b5de56dSgjelinek 	return (res);
3900b5de56dSgjelinek }
3910b5de56dSgjelinek 
3920b5de56dSgjelinek /*
3930b5de56dSgjelinek  * This function takes a zonepath and attempts to determine what the ZFS
3940b5de56dSgjelinek  * file system name (not mountpoint) should be for that path.  We do not
3950b5de56dSgjelinek  * assume that zonepath is an existing directory or ZFS fs since we use
3960b5de56dSgjelinek  * this function as part of the process of creating a new ZFS fs or clone.
3970b5de56dSgjelinek  *
3980b5de56dSgjelinek  * The way this works is that we look at the parent directory of the zonepath
3990b5de56dSgjelinek  * to see if it is a ZFS fs.  If it is, we get the name of that ZFS fs and
4000b5de56dSgjelinek  * append the last component of the zonepath to generate the ZFS name for the
4010b5de56dSgjelinek  * zonepath.  This matches the algorithm that ZFS uses for automatically
4020b5de56dSgjelinek  * mounting a new fs after it is created.
4030b5de56dSgjelinek  *
4040b5de56dSgjelinek  * Although a ZFS fs can be mounted anywhere, we don't worry about handling
4050b5de56dSgjelinek  * all of the complexity that a user could possibly configure with arbitrary
4060b5de56dSgjelinek  * mounts since there is no way to generate a ZFS name from a random path in
4070b5de56dSgjelinek  * the file system.  We only try to handle the automatic mounts that ZFS does
4080b5de56dSgjelinek  * for each file system.  ZFS restricts this so that a new fs must be created
4090b5de56dSgjelinek  * in an existing parent ZFS fs.  It then automatically mounts the new fs
4100b5de56dSgjelinek  * directly under the mountpoint for the parent fs using the last component
4110b5de56dSgjelinek  * of the name as the mountpoint directory.
4120b5de56dSgjelinek  *
4130b5de56dSgjelinek  * For example:
4140b5de56dSgjelinek  *    Name			Mountpoint
4150b5de56dSgjelinek  *    space/eng/dev/test/zone1	/project1/eng/dev/test/zone1
4160b5de56dSgjelinek  *
4170b5de56dSgjelinek  * Return Z_OK if the path mapped to a ZFS file system name, otherwise return
4180b5de56dSgjelinek  * Z_ERR.
4190b5de56dSgjelinek  */
4200b5de56dSgjelinek static int
4210b5de56dSgjelinek path2name(char *zonepath, char *zfs_name, int len)
4220b5de56dSgjelinek {
4230b5de56dSgjelinek 	int		res;
4240b5de56dSgjelinek 	char		*p;
4250b5de56dSgjelinek 	zfs_handle_t	*zhp;
4260b5de56dSgjelinek 
4270b5de56dSgjelinek 	if ((p = strrchr(zonepath, '/')) == NULL)
4280b5de56dSgjelinek 		return (Z_ERR);
4290b5de56dSgjelinek 
4300b5de56dSgjelinek 	/*
4310b5de56dSgjelinek 	 * If the parent directory is not its own ZFS fs, then we can't
4320b5de56dSgjelinek 	 * automatically create a new ZFS fs at the 'zonepath' mountpoint
4330b5de56dSgjelinek 	 * so return an error.
4340b5de56dSgjelinek 	 */
4350b5de56dSgjelinek 	*p = '\0';
4360b5de56dSgjelinek 	zhp = mount2zhandle(zonepath);
4370b5de56dSgjelinek 	*p = '/';
4380b5de56dSgjelinek 	if (zhp == NULL)
4390b5de56dSgjelinek 		return (Z_ERR);
4400b5de56dSgjelinek 
4410b5de56dSgjelinek 	res = snprintf(zfs_name, len, "%s/%s", zfs_get_name(zhp), p + 1);
4420b5de56dSgjelinek 
4430b5de56dSgjelinek 	zfs_close(zhp);
4440b5de56dSgjelinek 	if (res >= len)
4450b5de56dSgjelinek 		return (Z_ERR);
4460b5de56dSgjelinek 
4470b5de56dSgjelinek 	return (Z_OK);
4480b5de56dSgjelinek }
4490b5de56dSgjelinek 
4500b5de56dSgjelinek /*
4510b5de56dSgjelinek  * A ZFS file system iterator call-back function used to determine if the
4520b5de56dSgjelinek  * file system has dependents (snapshots & clones).
4530b5de56dSgjelinek  */
4540b5de56dSgjelinek /* ARGSUSED */
4550b5de56dSgjelinek static int
4560b5de56dSgjelinek has_dependent(zfs_handle_t *zhp, void *data)
4570b5de56dSgjelinek {
4580b5de56dSgjelinek 	zfs_close(zhp);
4590b5de56dSgjelinek 	return (1);
4600b5de56dSgjelinek }
4610b5de56dSgjelinek 
4620b5de56dSgjelinek /*
4630b5de56dSgjelinek  * Given a snapshot name, get the file system path where the snapshot lives.
4640b5de56dSgjelinek  * A snapshot name is of the form fs_name@snap_name.  For example, snapshot
4650b5de56dSgjelinek  * pl/zones/z1@SUNWzone1 would have a path of
4660b5de56dSgjelinek  * /pl/zones/z1/.zfs/snapshot/SUNWzone1.
4670b5de56dSgjelinek  */
4680b5de56dSgjelinek static int
4690b5de56dSgjelinek snap2path(char *snap_name, char *path, int len)
4700b5de56dSgjelinek {
4710b5de56dSgjelinek 	char		*p;
4720b5de56dSgjelinek 	zfs_handle_t	*zhp;
4730b5de56dSgjelinek 	char		mp[ZFS_MAXPROPLEN];
4740b5de56dSgjelinek 
4750b5de56dSgjelinek 	if ((p = strrchr(snap_name, '@')) == NULL)
4760b5de56dSgjelinek 		return (Z_ERR);
4770b5de56dSgjelinek 
4780b5de56dSgjelinek 	/* Get the file system name from the snap_name. */
4790b5de56dSgjelinek 	*p = '\0';
480990b4856Slling 	zhp = zfs_open(g_zfs, snap_name, ZFS_TYPE_DATASET);
4810b5de56dSgjelinek 	*p = '@';
4820b5de56dSgjelinek 	if (zhp == NULL)
4830b5de56dSgjelinek 		return (Z_ERR);
4840b5de56dSgjelinek 
4850b5de56dSgjelinek 	/* Get the file system mount point. */
4860b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mp, sizeof (mp), NULL, NULL,
48799653d4eSeschrock 	    0, B_FALSE) != 0) {
4880b5de56dSgjelinek 		zfs_close(zhp);
4890b5de56dSgjelinek 		return (Z_ERR);
4900b5de56dSgjelinek 	}
4910b5de56dSgjelinek 	zfs_close(zhp);
4920b5de56dSgjelinek 
4930b5de56dSgjelinek 	p++;
4940b5de56dSgjelinek 	if (snprintf(path, len, "%s/.zfs/snapshot/%s", mp, p) >= len)
4950b5de56dSgjelinek 		return (Z_ERR);
4960b5de56dSgjelinek 
4970b5de56dSgjelinek 	return (Z_OK);
4980b5de56dSgjelinek }
4990b5de56dSgjelinek 
5000b5de56dSgjelinek /*
5010b5de56dSgjelinek  * Clone a pre-existing ZFS snapshot, either by making a direct ZFS clone, if
5020b5de56dSgjelinek  * possible, or by copying the data from the snapshot to the zonepath.
5030b5de56dSgjelinek  */
5040b5de56dSgjelinek int
505*ff17c8bfSgjelinek clone_snapshot_zfs(char *snap_name, char *zonepath, char *validatesnap)
5060b5de56dSgjelinek {
5070b5de56dSgjelinek 	int	err = Z_OK;
5080b5de56dSgjelinek 	char	clone_name[MAXPATHLEN];
5090b5de56dSgjelinek 	char	snap_path[MAXPATHLEN];
5100b5de56dSgjelinek 
5110b5de56dSgjelinek 	if (snap2path(snap_name, snap_path, sizeof (snap_path)) != Z_OK) {
5120b5de56dSgjelinek 		(void) fprintf(stderr, gettext("unable to find path for %s.\n"),
5130b5de56dSgjelinek 		    snap_name);
5140b5de56dSgjelinek 		return (Z_ERR);
5150b5de56dSgjelinek 	}
5160b5de56dSgjelinek 
517*ff17c8bfSgjelinek 	if (validate_snapshot(snap_name, snap_path, validatesnap) != Z_OK)
5180b5de56dSgjelinek 		return (Z_NO_ENTRY);
5190b5de56dSgjelinek 
5200b5de56dSgjelinek 	/*
5210b5de56dSgjelinek 	 * The zonepath cannot be ZFS cloned, try to copy the data from
5220b5de56dSgjelinek 	 * within the snapshot to the zonepath.
5230b5de56dSgjelinek 	 */
5240b5de56dSgjelinek 	if (path2name(zonepath, clone_name, sizeof (clone_name)) != Z_OK) {
5250b5de56dSgjelinek 		if ((err = clone_copy(snap_path, zonepath)) == Z_OK)
5260b5de56dSgjelinek 			if (clean_out_clone() != Z_OK)
5270b5de56dSgjelinek 				(void) fprintf(stderr,
5280b5de56dSgjelinek 				    gettext("could not remove the "
5290b5de56dSgjelinek 				    "software inventory from %s\n"), zonepath);
5300b5de56dSgjelinek 
5310b5de56dSgjelinek 		return (err);
5320b5de56dSgjelinek 	}
5330b5de56dSgjelinek 
5340b5de56dSgjelinek 	if ((err = clone_snap(snap_name, clone_name)) != Z_OK) {
5350b5de56dSgjelinek 		if (err != Z_NO_ENTRY) {
5360b5de56dSgjelinek 			/*
5370b5de56dSgjelinek 			 * Cloning the snapshot failed.  Fall back to trying
5380b5de56dSgjelinek 			 * to install the zone by copying from the snapshot.
5390b5de56dSgjelinek 			 */
5400b5de56dSgjelinek 			if ((err = clone_copy(snap_path, zonepath)) == Z_OK)
5410b5de56dSgjelinek 				if (clean_out_clone() != Z_OK)
5420b5de56dSgjelinek 					(void) fprintf(stderr,
5430b5de56dSgjelinek 					    gettext("could not remove the "
5440b5de56dSgjelinek 					    "software inventory from %s\n"),
5450b5de56dSgjelinek 					    zonepath);
5460b5de56dSgjelinek 		} else {
5470b5de56dSgjelinek 			/*
5480b5de56dSgjelinek 			 * The snapshot is unusable for some reason so restore
5490b5de56dSgjelinek 			 * the zone state to configured since we were unable to
5500b5de56dSgjelinek 			 * actually do anything about getting the zone
5510b5de56dSgjelinek 			 * installed.
5520b5de56dSgjelinek 			 */
5530b5de56dSgjelinek 			int tmp;
5540b5de56dSgjelinek 
5550b5de56dSgjelinek 			if ((tmp = zone_set_state(target_zone,
5560b5de56dSgjelinek 			    ZONE_STATE_CONFIGURED)) != Z_OK) {
5570b5de56dSgjelinek 				errno = tmp;
5580b5de56dSgjelinek 				zperror2(target_zone,
5590b5de56dSgjelinek 				    gettext("could not set state"));
5600b5de56dSgjelinek 			}
5610b5de56dSgjelinek 		}
5620b5de56dSgjelinek 	}
5630b5de56dSgjelinek 
5640b5de56dSgjelinek 	return (err);
5650b5de56dSgjelinek }
5660b5de56dSgjelinek 
5670b5de56dSgjelinek /*
5680b5de56dSgjelinek  * Attempt to clone a source_zone to a target zonepath by using a ZFS clone.
5690b5de56dSgjelinek  */
5700b5de56dSgjelinek int
571*ff17c8bfSgjelinek clone_zfs(char *source_zonepath, char *zonepath, char *presnapbuf,
572*ff17c8bfSgjelinek     char *postsnapbuf)
5730b5de56dSgjelinek {
5740b5de56dSgjelinek 	zfs_handle_t	*zhp;
5750b5de56dSgjelinek 	char		clone_name[MAXPATHLEN];
5760b5de56dSgjelinek 	char		snap_name[MAXPATHLEN];
5770b5de56dSgjelinek 
5780b5de56dSgjelinek 	/*
5790b5de56dSgjelinek 	 * Try to get a zfs handle for the source_zonepath.  If this fails
5800b5de56dSgjelinek 	 * the source_zonepath is not ZFS so return an error.
5810b5de56dSgjelinek 	 */
5820b5de56dSgjelinek 	if ((zhp = mount2zhandle(source_zonepath)) == NULL)
5830b5de56dSgjelinek 		return (Z_ERR);
5840b5de56dSgjelinek 
5850b5de56dSgjelinek 	/*
5860b5de56dSgjelinek 	 * Check if there is a file system already mounted on zonepath.  If so,
5870b5de56dSgjelinek 	 * we can't clone to the path so we should fall back to copying.
5880b5de56dSgjelinek 	 */
5890b5de56dSgjelinek 	if (is_mountpnt(zonepath)) {
5900b5de56dSgjelinek 		zfs_close(zhp);
5910b5de56dSgjelinek 		(void) fprintf(stderr,
5920b5de56dSgjelinek 		    gettext("A file system is already mounted on %s,\n"
5930b5de56dSgjelinek 		    "preventing use of a ZFS clone.\n"), zonepath);
5940b5de56dSgjelinek 		return (Z_ERR);
5950b5de56dSgjelinek 	}
5960b5de56dSgjelinek 
5970b5de56dSgjelinek 	/*
5980b5de56dSgjelinek 	 * Instead of using path2name to get the clone name from the zonepath,
5990b5de56dSgjelinek 	 * we could generate a name from the source zone ZFS name.  However,
6000b5de56dSgjelinek 	 * this would mean we would create the clone under the ZFS fs of the
6010b5de56dSgjelinek 	 * source instead of what the zonepath says.  For example,
6020b5de56dSgjelinek 	 *
6030b5de56dSgjelinek 	 * source_zonepath		zonepath
6040b5de56dSgjelinek 	 * /pl/zones/dev/z1		/pl/zones/deploy/z2
6050b5de56dSgjelinek 	 *
6060b5de56dSgjelinek 	 * We don't want the clone to be under "dev", we want it under
6070b5de56dSgjelinek 	 * "deploy", so that we can leverage the normal attribute inheritance
6080b5de56dSgjelinek 	 * that ZFS provides in the fs hierarchy.
6090b5de56dSgjelinek 	 */
6100b5de56dSgjelinek 	if (path2name(zonepath, clone_name, sizeof (clone_name)) != Z_OK) {
6110b5de56dSgjelinek 		zfs_close(zhp);
6120b5de56dSgjelinek 		return (Z_ERR);
6130b5de56dSgjelinek 	}
6140b5de56dSgjelinek 
615*ff17c8bfSgjelinek 	if (take_snapshot(zhp, snap_name, sizeof (snap_name), presnapbuf,
616*ff17c8bfSgjelinek 	    postsnapbuf) != Z_OK) {
6170b5de56dSgjelinek 		zfs_close(zhp);
6180b5de56dSgjelinek 		return (Z_ERR);
6190b5de56dSgjelinek 	}
6200b5de56dSgjelinek 	zfs_close(zhp);
6210b5de56dSgjelinek 
622d9e728a2Sgjelinek 	if (clone_snap(snap_name, clone_name) != Z_OK) {
623d9e728a2Sgjelinek 		/* Clean up the snapshot we just took. */
624d9e728a2Sgjelinek 		if ((zhp = zfs_open(g_zfs, snap_name, ZFS_TYPE_SNAPSHOT))
625d9e728a2Sgjelinek 		    != NULL) {
626d9e728a2Sgjelinek 			if (zfs_unmount(zhp, NULL, 0) == 0)
627d9e728a2Sgjelinek 				(void) zfs_destroy(zhp);
628d9e728a2Sgjelinek 			zfs_close(zhp);
629d9e728a2Sgjelinek 		}
630d9e728a2Sgjelinek 
6310b5de56dSgjelinek 		return (Z_ERR);
632d9e728a2Sgjelinek 	}
6330b5de56dSgjelinek 
6340b5de56dSgjelinek 	(void) printf(gettext("Instead of copying, a ZFS clone has been "
6350b5de56dSgjelinek 	    "created for this zone.\n"));
6360b5de56dSgjelinek 
6370b5de56dSgjelinek 	return (Z_OK);
6380b5de56dSgjelinek }
6390b5de56dSgjelinek 
6400b5de56dSgjelinek /*
6410b5de56dSgjelinek  * Attempt to create a ZFS file system for the specified zonepath.
6420b5de56dSgjelinek  * We either will successfully create a ZFS file system and get it mounted
6430b5de56dSgjelinek  * on the zonepath or we don't.  The caller doesn't care since a regular
6440b5de56dSgjelinek  * directory is used for the zonepath if no ZFS file system is mounted there.
6450b5de56dSgjelinek  */
6460b5de56dSgjelinek void
6470b5de56dSgjelinek create_zfs_zonepath(char *zonepath)
6480b5de56dSgjelinek {
6490b5de56dSgjelinek 	zfs_handle_t	*zhp;
6500b5de56dSgjelinek 	char		zfs_name[MAXPATHLEN];
651e9dbad6fSeschrock 	nvlist_t	*props = NULL;
6520b5de56dSgjelinek 
6530b5de56dSgjelinek 	if (path2name(zonepath, zfs_name, sizeof (zfs_name)) != Z_OK)
6540b5de56dSgjelinek 		return;
6550b5de56dSgjelinek 
656e9dbad6fSeschrock 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0 ||
6575f8e1617Snn35248 	    nvlist_add_string(props, zfs_prop_to_name(ZFS_PROP_SHARENFS),
6585f8e1617Snn35248 	    "off") != 0) {
6595f8e1617Snn35248 		if (props != NULL)
660e9dbad6fSeschrock 			nvlist_free(props);
661e9dbad6fSeschrock 		(void) fprintf(stderr, gettext("cannot create ZFS dataset %s: "
662e9dbad6fSeschrock 		    "out of memory\n"), zfs_name);
663e9dbad6fSeschrock 	}
664e9dbad6fSeschrock 
665e9dbad6fSeschrock 	if (zfs_create(g_zfs, zfs_name, ZFS_TYPE_FILESYSTEM, props) != 0 ||
666990b4856Slling 	    (zhp = zfs_open(g_zfs, zfs_name, ZFS_TYPE_DATASET)) == NULL) {
66799653d4eSeschrock 		(void) fprintf(stderr, gettext("cannot create ZFS dataset %s: "
66899653d4eSeschrock 		    "%s\n"), zfs_name, libzfs_error_description(g_zfs));
669e9dbad6fSeschrock 		nvlist_free(props);
6700b5de56dSgjelinek 		return;
6710b5de56dSgjelinek 	}
6720b5de56dSgjelinek 
673e9dbad6fSeschrock 	nvlist_free(props);
674e9dbad6fSeschrock 
6750b5de56dSgjelinek 	if (zfs_mount(zhp, NULL, 0) != 0) {
67699653d4eSeschrock 		(void) fprintf(stderr, gettext("cannot mount ZFS dataset %s: "
67799653d4eSeschrock 		    "%s\n"), zfs_name, libzfs_error_description(g_zfs));
6780b5de56dSgjelinek 		(void) zfs_destroy(zhp);
6790b5de56dSgjelinek 	} else {
6800b5de56dSgjelinek 		if (chmod(zonepath, S_IRWXU) != 0) {
6810b5de56dSgjelinek 			(void) fprintf(stderr, gettext("file system %s "
6820b5de56dSgjelinek 			    "successfully created, but chmod %o failed: %s\n"),
6830b5de56dSgjelinek 			    zfs_name, S_IRWXU, strerror(errno));
6840b5de56dSgjelinek 			(void) destroy_zfs(zonepath);
6850b5de56dSgjelinek 		} else {
6860b5de56dSgjelinek 			(void) printf(gettext("A ZFS file system has been "
6870b5de56dSgjelinek 			    "created for this zone.\n"));
6880b5de56dSgjelinek 		}
6890b5de56dSgjelinek 	}
6900b5de56dSgjelinek 
6910b5de56dSgjelinek 	zfs_close(zhp);
6920b5de56dSgjelinek }
6930b5de56dSgjelinek 
6940b5de56dSgjelinek /*
6950b5de56dSgjelinek  * If the zonepath is a ZFS file system, attempt to destroy it.  We return Z_OK
6960b5de56dSgjelinek  * if we were able to zfs_destroy the zonepath, otherwise we return Z_ERR
6970b5de56dSgjelinek  * which means the caller should clean up the zonepath in the traditional
6980b5de56dSgjelinek  * way.
6990b5de56dSgjelinek  */
7000b5de56dSgjelinek int
7010b5de56dSgjelinek destroy_zfs(char *zonepath)
7020b5de56dSgjelinek {
7030b5de56dSgjelinek 	zfs_handle_t	*zhp;
7040b5de56dSgjelinek 	boolean_t	is_clone = B_FALSE;
7050b5de56dSgjelinek 	char		origin[ZFS_MAXPROPLEN];
7060b5de56dSgjelinek 
70799653d4eSeschrock 	if ((zhp = mount2zhandle(zonepath)) == NULL)
7080b5de56dSgjelinek 		return (Z_ERR);
7090b5de56dSgjelinek 
7100b5de56dSgjelinek 	/*
7110b5de56dSgjelinek 	 * We can't destroy the file system if it has dependents.
7120b5de56dSgjelinek 	 */
7133bb79becSeschrock 	if (zfs_iter_dependents(zhp, B_TRUE, has_dependent, NULL) != 0 ||
7140b5de56dSgjelinek 	    zfs_unmount(zhp, NULL, 0) != 0) {
7150b5de56dSgjelinek 		zfs_close(zhp);
7160b5de56dSgjelinek 		return (Z_ERR);
7170b5de56dSgjelinek 	}
7180b5de56dSgjelinek 
7190b5de56dSgjelinek 	/*
7200b5de56dSgjelinek 	 * This might be a clone.  Try to get the snapshot so we can attempt
7210b5de56dSgjelinek 	 * to destroy that as well.
7220b5de56dSgjelinek 	 */
7230b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin, sizeof (origin), NULL,
72499653d4eSeschrock 	    NULL, 0, B_FALSE) == 0)
7250b5de56dSgjelinek 		is_clone = B_TRUE;
7260b5de56dSgjelinek 
7270b5de56dSgjelinek 	if (zfs_destroy(zhp) != 0) {
7280b5de56dSgjelinek 		/*
7290b5de56dSgjelinek 		 * If the destroy fails for some reason, try to remount
7300b5de56dSgjelinek 		 * the file system so that we can use "rm -rf" to clean up
7310b5de56dSgjelinek 		 * instead.
7320b5de56dSgjelinek 		 */
7330b5de56dSgjelinek 		(void) zfs_mount(zhp, NULL, 0);
7340b5de56dSgjelinek 		zfs_close(zhp);
7350b5de56dSgjelinek 		return (Z_ERR);
7360b5de56dSgjelinek 	}
7370b5de56dSgjelinek 
738d9e728a2Sgjelinek 	/*
739d9e728a2Sgjelinek 	 * If the zone has ever been moved then the mountpoint dir will not be
740d9e728a2Sgjelinek 	 * cleaned up by the zfs_destroy().  To handle this case try to clean
741d9e728a2Sgjelinek 	 * it up now but don't worry if it fails, that will be normal.
742d9e728a2Sgjelinek 	 */
743d9e728a2Sgjelinek 	(void) rmdir(zonepath);
744d9e728a2Sgjelinek 
7450b5de56dSgjelinek 	(void) printf(gettext("The ZFS file system for this zone has been "
7460b5de56dSgjelinek 	    "destroyed.\n"));
7470b5de56dSgjelinek 
7480b5de56dSgjelinek 	if (is_clone) {
7490b5de56dSgjelinek 		zfs_handle_t	*ohp;
7500b5de56dSgjelinek 
7510b5de56dSgjelinek 		/*
7520b5de56dSgjelinek 		 * Try to clean up the snapshot that the clone was taken from.
7530b5de56dSgjelinek 		 */
75499653d4eSeschrock 		if ((ohp = zfs_open(g_zfs, origin,
75599653d4eSeschrock 		    ZFS_TYPE_SNAPSHOT)) != NULL) {
7563bb79becSeschrock 			if (zfs_iter_dependents(ohp, B_TRUE, has_dependent,
7573bb79becSeschrock 			    NULL) == 0 && zfs_unmount(ohp, NULL, 0) == 0)
7580b5de56dSgjelinek 				(void) zfs_destroy(ohp);
7590b5de56dSgjelinek 			zfs_close(ohp);
7600b5de56dSgjelinek 		}
7610b5de56dSgjelinek 	}
7620b5de56dSgjelinek 
7630b5de56dSgjelinek 	zfs_close(zhp);
7640b5de56dSgjelinek 	return (Z_OK);
7650b5de56dSgjelinek }
7660b5de56dSgjelinek 
7670b5de56dSgjelinek /*
7680b5de56dSgjelinek  * Return true if the path is its own zfs file system.  We determine this
7690b5de56dSgjelinek  * by stat-ing the path to see if it is zfs and stat-ing the parent to see
7700b5de56dSgjelinek  * if it is a different fs.
7710b5de56dSgjelinek  */
7720b5de56dSgjelinek boolean_t
7730b5de56dSgjelinek is_zonepath_zfs(char *zonepath)
7740b5de56dSgjelinek {
7750b5de56dSgjelinek 	int res;
7760b5de56dSgjelinek 	char *path;
7770b5de56dSgjelinek 	char *parent;
7783f2f09c1Sdp 	struct statvfs64 buf1, buf2;
7790b5de56dSgjelinek 
7803f2f09c1Sdp 	if (statvfs64(zonepath, &buf1) != 0)
7810b5de56dSgjelinek 		return (B_FALSE);
7820b5de56dSgjelinek 
7830b5de56dSgjelinek 	if (strcmp(buf1.f_basetype, "zfs") != 0)
7840b5de56dSgjelinek 		return (B_FALSE);
7850b5de56dSgjelinek 
7860b5de56dSgjelinek 	if ((path = strdup(zonepath)) == NULL)
7870b5de56dSgjelinek 		return (B_FALSE);
7880b5de56dSgjelinek 
7890b5de56dSgjelinek 	parent = dirname(path);
7903f2f09c1Sdp 	res = statvfs64(parent, &buf2);
7910b5de56dSgjelinek 	free(path);
7920b5de56dSgjelinek 
7930b5de56dSgjelinek 	if (res != 0)
7940b5de56dSgjelinek 		return (B_FALSE);
7950b5de56dSgjelinek 
7960b5de56dSgjelinek 	if (buf1.f_fsid == buf2.f_fsid)
7970b5de56dSgjelinek 		return (B_FALSE);
7980b5de56dSgjelinek 
7990b5de56dSgjelinek 	return (B_TRUE);
8000b5de56dSgjelinek }
8010b5de56dSgjelinek 
8020b5de56dSgjelinek /*
8030b5de56dSgjelinek  * Implement the fast move of a ZFS file system by simply updating the
8040b5de56dSgjelinek  * mountpoint.  Since it is file system already, we don't have the
8050b5de56dSgjelinek  * issue of cross-file system copying.
8060b5de56dSgjelinek  */
8070b5de56dSgjelinek int
8080b5de56dSgjelinek move_zfs(char *zonepath, char *new_zonepath)
8090b5de56dSgjelinek {
8100b5de56dSgjelinek 	int		ret = Z_ERR;
8110b5de56dSgjelinek 	zfs_handle_t	*zhp;
8120b5de56dSgjelinek 
81399653d4eSeschrock 	if ((zhp = mount2zhandle(zonepath)) == NULL)
8140b5de56dSgjelinek 		return (Z_ERR);
8150b5de56dSgjelinek 
816e9dbad6fSeschrock 	if (zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
817e9dbad6fSeschrock 	    new_zonepath) == 0) {
8180b5de56dSgjelinek 		/*
8190b5de56dSgjelinek 		 * Clean up the old mount point.  We ignore any failure since
8200b5de56dSgjelinek 		 * the zone is already successfully mounted on the new path.
8210b5de56dSgjelinek 		 */
8220b5de56dSgjelinek 		(void) rmdir(zonepath);
8230b5de56dSgjelinek 		ret = Z_OK;
8240b5de56dSgjelinek 	}
8250b5de56dSgjelinek 
8260b5de56dSgjelinek 	zfs_close(zhp);
8270b5de56dSgjelinek 
8280b5de56dSgjelinek 	return (ret);
8290b5de56dSgjelinek }
8300b5de56dSgjelinek 
8310b5de56dSgjelinek /*
8320b5de56dSgjelinek  * Validate that the given dataset exists on the system, and that neither it nor
8330b5de56dSgjelinek  * its children are zvols.
8340b5de56dSgjelinek  *
8350b5de56dSgjelinek  * Note that we don't do anything with the 'zoned' property here.  All
8360b5de56dSgjelinek  * management is done in zoneadmd when the zone is actually rebooted.  This
8370b5de56dSgjelinek  * allows us to automatically set the zoned property even when a zone is
8380b5de56dSgjelinek  * rebooted by the administrator.
8390b5de56dSgjelinek  */
8400b5de56dSgjelinek int
8410b5de56dSgjelinek verify_datasets(zone_dochandle_t handle)
8420b5de56dSgjelinek {
8430b5de56dSgjelinek 	int return_code = Z_OK;
8440b5de56dSgjelinek 	struct zone_dstab dstab;
8450b5de56dSgjelinek 	zfs_handle_t *zhp;
8460b5de56dSgjelinek 	char propbuf[ZFS_MAXPROPLEN];
8470b5de56dSgjelinek 	char source[ZFS_MAXNAMELEN];
848990b4856Slling 	zprop_source_t srctype;
8490b5de56dSgjelinek 
8500b5de56dSgjelinek 	if (zonecfg_setdsent(handle) != Z_OK) {
8510b5de56dSgjelinek 		/*
8520b5de56dSgjelinek 		 * TRANSLATION_NOTE
8530b5de56dSgjelinek 		 * zfs and dataset are literals that should not be translated.
8540b5de56dSgjelinek 		 */
8550b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify zfs datasets: "
8560b5de56dSgjelinek 		    "unable to enumerate datasets\n"));
8570b5de56dSgjelinek 		return (Z_ERR);
8580b5de56dSgjelinek 	}
8590b5de56dSgjelinek 
8600b5de56dSgjelinek 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
8610b5de56dSgjelinek 
86299653d4eSeschrock 		if ((zhp = zfs_open(g_zfs, dstab.zone_dataset_name,
8630b5de56dSgjelinek 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
86499653d4eSeschrock 			(void) fprintf(stderr, gettext("could not verify zfs "
86599653d4eSeschrock 			    "dataset %s: %s\n"), dstab.zone_dataset_name,
86699653d4eSeschrock 			    libzfs_error_description(g_zfs));
8670b5de56dSgjelinek 			return_code = Z_ERR;
8680b5de56dSgjelinek 			continue;
8690b5de56dSgjelinek 		}
8700b5de56dSgjelinek 
8710b5de56dSgjelinek 		if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf,
8720b5de56dSgjelinek 		    sizeof (propbuf), &srctype, source,
8730b5de56dSgjelinek 		    sizeof (source), 0) == 0 &&
874990b4856Slling 		    (srctype == ZPROP_SRC_INHERITED)) {
8750b5de56dSgjelinek 			(void) fprintf(stderr, gettext("could not verify zfs "
8760b5de56dSgjelinek 			    "dataset %s: mountpoint cannot be inherited\n"),
8770b5de56dSgjelinek 			    dstab.zone_dataset_name);
8780b5de56dSgjelinek 			return_code = Z_ERR;
8790b5de56dSgjelinek 			zfs_close(zhp);
8800b5de56dSgjelinek 			continue;
8810b5de56dSgjelinek 		}
8820b5de56dSgjelinek 
8830b5de56dSgjelinek 		if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
8840b5de56dSgjelinek 			(void) fprintf(stderr, gettext("cannot verify zfs "
8850b5de56dSgjelinek 			    "dataset %s: volumes cannot be specified as a "
8860b5de56dSgjelinek 			    "zone dataset resource\n"),
8870b5de56dSgjelinek 			    dstab.zone_dataset_name);
8880b5de56dSgjelinek 			return_code = Z_ERR;
8890b5de56dSgjelinek 		}
8900b5de56dSgjelinek 
8910b5de56dSgjelinek 		if (zfs_iter_children(zhp, check_zvol, NULL) != 0)
8920b5de56dSgjelinek 			return_code = Z_ERR;
8930b5de56dSgjelinek 
8940b5de56dSgjelinek 		zfs_close(zhp);
8950b5de56dSgjelinek 	}
8960b5de56dSgjelinek 	(void) zonecfg_enddsent(handle);
8970b5de56dSgjelinek 
8980b5de56dSgjelinek 	return (return_code);
8990b5de56dSgjelinek }
9000b5de56dSgjelinek 
9010b5de56dSgjelinek /*
9020b5de56dSgjelinek  * Verify that the ZFS dataset exists, and its mountpoint
9030b5de56dSgjelinek  * property is set to "legacy".
9040b5de56dSgjelinek  */
9050b5de56dSgjelinek int
9060b5de56dSgjelinek verify_fs_zfs(struct zone_fstab *fstab)
9070b5de56dSgjelinek {
9080b5de56dSgjelinek 	zfs_handle_t *zhp;
9090b5de56dSgjelinek 	char propbuf[ZFS_MAXPROPLEN];
9100b5de56dSgjelinek 
91199653d4eSeschrock 	if ((zhp = zfs_open(g_zfs, fstab->zone_fs_special,
912990b4856Slling 	    ZFS_TYPE_DATASET)) == NULL) {
9130b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify fs %s: "
9140b5de56dSgjelinek 		    "could not access zfs dataset '%s'\n"),
9150b5de56dSgjelinek 		    fstab->zone_fs_dir, fstab->zone_fs_special);
9160b5de56dSgjelinek 		return (Z_ERR);
9170b5de56dSgjelinek 	}
9180b5de56dSgjelinek 
9190b5de56dSgjelinek 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
9200b5de56dSgjelinek 		(void) fprintf(stderr, gettext("cannot verify fs %s: "
9210b5de56dSgjelinek 		    "'%s' is not a file system\n"),
9220b5de56dSgjelinek 		    fstab->zone_fs_dir, fstab->zone_fs_special);
9230b5de56dSgjelinek 		zfs_close(zhp);
9240b5de56dSgjelinek 		return (Z_ERR);
9250b5de56dSgjelinek 	}
9260b5de56dSgjelinek 
9270b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, sizeof (propbuf),
9280b5de56dSgjelinek 	    NULL, NULL, 0, 0) != 0 || strcmp(propbuf, "legacy") != 0) {
9290b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify fs %s: "
9300b5de56dSgjelinek 		    "zfs '%s' mountpoint is not \"legacy\"\n"),
9310b5de56dSgjelinek 		    fstab->zone_fs_dir, fstab->zone_fs_special);
9320b5de56dSgjelinek 		zfs_close(zhp);
9330b5de56dSgjelinek 		return (Z_ERR);
9340b5de56dSgjelinek 	}
9350b5de56dSgjelinek 
9360b5de56dSgjelinek 	zfs_close(zhp);
93799653d4eSeschrock 	return (Z_OK);
93899653d4eSeschrock }
93999653d4eSeschrock 
94099653d4eSeschrock int
94199653d4eSeschrock init_zfs(void)
94299653d4eSeschrock {
94399653d4eSeschrock 	if ((g_zfs = libzfs_init()) == NULL) {
94499653d4eSeschrock 		(void) fprintf(stderr, gettext("failed to initialize ZFS "
94599653d4eSeschrock 		    "library\n"));
94699653d4eSeschrock 		return (Z_ERR);
94799653d4eSeschrock 	}
94899653d4eSeschrock 
9490b5de56dSgjelinek 	return (Z_OK);
9500b5de56dSgjelinek }
951