xref: /titanic_51/usr/src/cmd/zoneadm/zfs.c (revision 401f278d817f55ca385f31d9e40db6deac6220e8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the functions used to support the ZFS integration
31  * with zones.  This includes validation (e.g. zonecfg dataset), cloning,
32  * file system creation and destruction.
33  */
34 
35 #include <stdio.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <locale.h>
40 #include <libintl.h>
41 #include <sys/stat.h>
42 #include <sys/statvfs.h>
43 #include <libgen.h>
44 #include <libzonecfg.h>
45 #include <sys/mnttab.h>
46 #include <libzfs.h>
47 
48 #include "zoneadm.h"
49 
50 libzfs_handle_t *g_zfs;
51 
52 typedef struct zfs_mount_data {
53 	char		*match_name;
54 	zfs_handle_t	*match_handle;
55 } zfs_mount_data_t;
56 
57 typedef struct zfs_snapshot_data {
58 	char	*match_name;
59 	int	len;
60 	int	max;
61 } zfs_snapshot_data_t;
62 
63 /*
64  * A ZFS file system iterator call-back function which is used to validate
65  * datasets imported into the zone.
66  */
67 /* ARGSUSED */
68 static int
69 check_zvol(zfs_handle_t *zhp, void *unused)
70 {
71 	int ret;
72 
73 	if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
74 		/*
75 		 * TRANSLATION_NOTE
76 		 * zfs and dataset are literals that should not be translated.
77 		 */
78 		(void) fprintf(stderr, gettext("cannot verify zfs dataset %s: "
79 		    "volumes cannot be specified as a zone dataset resource\n"),
80 		    zfs_get_name(zhp));
81 		ret = -1;
82 	} else {
83 		ret = zfs_iter_children(zhp, check_zvol, NULL);
84 	}
85 
86 	zfs_close(zhp);
87 
88 	return (ret);
89 }
90 
91 /*
92  * A ZFS file system iterator call-back function which returns the
93  * zfs_handle_t for a ZFS file system on the specified mount point.
94  */
95 static int
96 match_mountpoint(zfs_handle_t *zhp, void *data)
97 {
98 	int			res;
99 	zfs_mount_data_t	*cbp;
100 	char			mp[ZFS_MAXPROPLEN];
101 
102 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
103 		zfs_close(zhp);
104 		return (0);
105 	}
106 
107 	cbp = (zfs_mount_data_t *)data;
108 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mp, sizeof (mp), NULL, NULL,
109 	    0, B_FALSE) == 0 && strcmp(mp, cbp->match_name) == 0) {
110 		cbp->match_handle = zhp;
111 		return (1);
112 	}
113 
114 	res = zfs_iter_filesystems(zhp, match_mountpoint, data);
115 	zfs_close(zhp);
116 	return (res);
117 }
118 
119 /*
120  * Get ZFS handle for the specified mount point.
121  */
122 static zfs_handle_t *
123 mount2zhandle(char *mountpoint)
124 {
125 	zfs_mount_data_t	cb;
126 
127 	cb.match_name = mountpoint;
128 	cb.match_handle = NULL;
129 	(void) zfs_iter_root(g_zfs, match_mountpoint, &cb);
130 	return (cb.match_handle);
131 }
132 
133 /*
134  * Check if there is already a file system (zfs or any other type) mounted on
135  * path.
136  */
137 static boolean_t
138 is_mountpnt(char *path)
139 {
140 	FILE		*fp;
141 	struct mnttab	entry;
142 
143 	if ((fp = fopen("/etc/mnttab", "r")) == NULL)
144 		return (B_FALSE);
145 
146 	while (getmntent(fp, &entry) == 0) {
147 		if (strcmp(path, entry.mnt_mountp) == 0) {
148 			(void) fclose(fp);
149 			return (B_TRUE);
150 		}
151 	}
152 
153 	(void) fclose(fp);
154 	return (B_FALSE);
155 }
156 
157 /*
158  * Perform any necessary housekeeping tasks we need to do before we take
159  * a ZFS snapshot of the zone.  What this really entails is that we are
160  * taking a sw inventory of the source zone, like we do when we detach,
161  * so that there is the XML manifest in the snapshot.  We use that to
162  * validate the snapshot if it is the source of a clone at some later time.
163  */
164 static int
165 pre_snapshot(char *source_zone)
166 {
167 	int err;
168 	zone_dochandle_t handle;
169 
170 	if ((handle = zonecfg_init_handle()) == NULL) {
171 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
172 		return (Z_ERR);
173 	}
174 
175 	if ((err = zonecfg_get_handle(source_zone, handle)) != Z_OK) {
176 		errno = err;
177 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
178 		zonecfg_fini_handle(handle);
179 		return (Z_ERR);
180 	}
181 
182 	if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) {
183 		errno = err;
184 		zperror(gettext("getting the software version information "
185 		    "failed"), B_TRUE);
186 		zonecfg_fini_handle(handle);
187 		return (Z_ERR);
188 	}
189 
190 	if ((err = zonecfg_detach_save(handle, 0)) != Z_OK) {
191 		errno = err;
192 		zperror(gettext("saving the software version manifest failed"),
193 		    B_TRUE);
194 		zonecfg_fini_handle(handle);
195 		return (Z_ERR);
196 	}
197 
198 	zonecfg_fini_handle(handle);
199 	return (Z_OK);
200 }
201 
202 /*
203  * Perform any necessary housekeeping tasks we need to do after we take
204  * a ZFS snapshot of the zone.  What this really entails is removing the
205  * sw inventory XML file from the zone.  It is still in the snapshot where
206  * we want it, but we don't want it in the source zone itself.
207  */
208 static int
209 post_snapshot(char *source_zone)
210 {
211 	int err;
212 	zone_dochandle_t handle;
213 
214 	if ((handle = zonecfg_init_handle()) == NULL) {
215 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
216 		return (Z_ERR);
217 	}
218 
219 	if ((err = zonecfg_get_handle(source_zone, handle)) != Z_OK) {
220 		errno = err;
221 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
222 		zonecfg_fini_handle(handle);
223 		return (Z_ERR);
224 	}
225 
226 	zonecfg_rm_detached(handle, B_FALSE);
227 	zonecfg_fini_handle(handle);
228 
229 	return (Z_OK);
230 }
231 
232 /*
233  * This is a ZFS snapshot iterator call-back function which returns the
234  * highest number of SUNWzone snapshots that have been taken.
235  */
236 static int
237 get_snap_max(zfs_handle_t *zhp, void *data)
238 {
239 	int			res;
240 	zfs_snapshot_data_t	*cbp;
241 
242 	if (zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) {
243 		zfs_close(zhp);
244 		return (0);
245 	}
246 
247 	cbp = (zfs_snapshot_data_t *)data;
248 
249 	if (strncmp(zfs_get_name(zhp), cbp->match_name, cbp->len) == 0) {
250 		char	*nump;
251 		int	num;
252 
253 		nump = (char *)(zfs_get_name(zhp) + cbp->len);
254 		num = atoi(nump);
255 		if (num > cbp->max)
256 			cbp->max = num;
257 	}
258 
259 	res = zfs_iter_snapshots(zhp, get_snap_max, data);
260 	zfs_close(zhp);
261 	return (res);
262 }
263 
264 /*
265  * Take a ZFS snapshot to be used for cloning the zone.
266  */
267 static int
268 take_snapshot(char *source_zone, zfs_handle_t *zhp, char *snapshot_name,
269     int snap_size)
270 {
271 	int			res;
272 	char			template[ZFS_MAXNAMELEN];
273 	zfs_snapshot_data_t	cb;
274 
275 	/*
276 	 * First we need to figure out the next available name for the
277 	 * zone snapshot.  Look through the list of zones snapshots for
278 	 * this file system to determine the maximum snapshot name.
279 	 */
280 	if (snprintf(template, sizeof (template), "%s@SUNWzone",
281 	    zfs_get_name(zhp)) >=  sizeof (template))
282 		return (Z_ERR);
283 
284 	cb.match_name = template;
285 	cb.len = strlen(template);
286 	cb.max = 0;
287 
288 	if (zfs_iter_snapshots(zhp, get_snap_max, &cb) != 0)
289 		return (Z_ERR);
290 
291 	cb.max++;
292 
293 	if (snprintf(snapshot_name, snap_size, "%s@SUNWzone%d",
294 	    zfs_get_name(zhp), cb.max) >= snap_size)
295 		return (Z_ERR);
296 
297 	if (pre_snapshot(source_zone) != Z_OK)
298 		return (Z_ERR);
299 	res = zfs_snapshot(g_zfs, snapshot_name, B_FALSE);
300 	if (post_snapshot(source_zone) != Z_OK)
301 		return (Z_ERR);
302 
303 	if (res != 0)
304 		return (Z_ERR);
305 	return (Z_OK);
306 }
307 
308 /*
309  * We are using an explicit snapshot from some earlier point in time so
310  * we need to validate it.  This involves checking the sw inventory that
311  * we took when we made the snapshot to verify that the current sw config
312  * on the host is still valid to run a zone made from this snapshot.
313  */
314 static int
315 validate_snapshot(char *snapshot_name, char *snap_path)
316 {
317 	int err;
318 	zone_dochandle_t handle;
319 	zone_dochandle_t athandle = NULL;
320 
321 	if ((handle = zonecfg_init_handle()) == NULL) {
322 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
323 		return (Z_ERR);
324 	}
325 
326 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
327 		errno = err;
328 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
329 		zonecfg_fini_handle(handle);
330 		return (Z_ERR);
331 	}
332 
333 	if ((athandle = zonecfg_init_handle()) == NULL) {
334 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
335 		goto done;
336 	}
337 
338 	if ((err = zonecfg_get_attach_handle(snap_path, target_zone, B_TRUE,
339 	    athandle)) != Z_OK) {
340 		if (err == Z_NO_ZONE)
341 			(void) fprintf(stderr, gettext("snapshot %s was not "
342 			    "taken\n\tby a 'zoneadm clone' command.  It can "
343 			    "not be used to clone zones.\n"), snapshot_name);
344 		else
345 			(void) fprintf(stderr, gettext("snapshot %s is "
346 			    "out-dated\n\tIt can no longer be used to clone "
347 			    "zones on this system.\n"), snapshot_name);
348 		goto done;
349 	}
350 
351 	/* Get the detach information for the locally defined zone. */
352 	if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) {
353 		errno = err;
354 		zperror(gettext("getting the attach information failed"),
355 		    B_TRUE);
356 		goto done;
357 	}
358 
359 	if ((err = sw_cmp(handle, athandle, SW_CMP_SILENT)) != Z_OK)
360 		(void) fprintf(stderr, gettext("snapshot %s is out-dated\n\t"
361 		    "It can no longer be used to clone zones on this "
362 		    "system.\n"), snapshot_name);
363 
364 done:
365 	zonecfg_fini_handle(handle);
366 	if (athandle != NULL)
367 		zonecfg_fini_handle(athandle);
368 
369 	return (err);
370 }
371 
372 /*
373  * Remove the sw inventory file from inside this zonepath that we picked up out
374  * of the snapshot.
375  */
376 static int
377 clean_out_clone()
378 {
379 	int err;
380 	zone_dochandle_t handle;
381 
382 	if ((handle = zonecfg_init_handle()) == NULL) {
383 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
384 		return (Z_ERR);
385 	}
386 
387 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
388 		errno = err;
389 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
390 		zonecfg_fini_handle(handle);
391 		return (Z_ERR);
392 	}
393 
394 	zonecfg_rm_detached(handle, B_FALSE);
395 	zonecfg_fini_handle(handle);
396 
397 	return (Z_OK);
398 }
399 
400 /*
401  * Make a ZFS clone on zonepath from snapshot_name.
402  */
403 static int
404 clone_snap(char *snapshot_name, char *zonepath)
405 {
406 	int		res = Z_OK;
407 	int		err;
408 	zfs_handle_t	*zhp;
409 	zfs_handle_t	*clone;
410 	nvlist_t	*props = NULL;
411 
412 	if ((zhp = zfs_open(g_zfs, snapshot_name, ZFS_TYPE_SNAPSHOT)) == NULL)
413 		return (Z_NO_ENTRY);
414 
415 	(void) printf(gettext("Cloning snapshot %s\n"), snapshot_name);
416 
417 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0 ||
418 	    nvlist_add_string(props, zfs_prop_to_name(ZFS_PROP_SHARENFS),
419 	    "off") != 0) {
420 		if (props != NULL)
421 			nvlist_free(props);
422 		(void) fprintf(stderr, gettext("could not create ZFS clone "
423 		    "%s: out of memory\n"), zonepath);
424 		return (Z_ERR);
425 	}
426 
427 	err = zfs_clone(zhp, zonepath, props);
428 	zfs_close(zhp);
429 
430 	nvlist_free(props);
431 
432 	if (err != 0)
433 		return (Z_ERR);
434 
435 	/* create the mountpoint if necessary */
436 	if ((clone = zfs_open(g_zfs, zonepath, ZFS_TYPE_ANY)) == NULL)
437 		return (Z_ERR);
438 
439 	/*
440 	 * The clone has been created so we need to print a diagnostic
441 	 * message if one of the following steps fails for some reason.
442 	 */
443 	if (zfs_mount(clone, NULL, 0) != 0) {
444 		(void) fprintf(stderr, gettext("could not mount ZFS clone "
445 		    "%s\n"), zfs_get_name(clone));
446 		res = Z_ERR;
447 
448 	} else if (clean_out_clone() != Z_OK) {
449 		(void) fprintf(stderr, gettext("could not remove the "
450 		    "software inventory from ZFS clone %s\n"),
451 		    zfs_get_name(clone));
452 		res = Z_ERR;
453 	}
454 
455 	zfs_close(clone);
456 	return (res);
457 }
458 
459 /*
460  * This function takes a zonepath and attempts to determine what the ZFS
461  * file system name (not mountpoint) should be for that path.  We do not
462  * assume that zonepath is an existing directory or ZFS fs since we use
463  * this function as part of the process of creating a new ZFS fs or clone.
464  *
465  * The way this works is that we look at the parent directory of the zonepath
466  * to see if it is a ZFS fs.  If it is, we get the name of that ZFS fs and
467  * append the last component of the zonepath to generate the ZFS name for the
468  * zonepath.  This matches the algorithm that ZFS uses for automatically
469  * mounting a new fs after it is created.
470  *
471  * Although a ZFS fs can be mounted anywhere, we don't worry about handling
472  * all of the complexity that a user could possibly configure with arbitrary
473  * mounts since there is no way to generate a ZFS name from a random path in
474  * the file system.  We only try to handle the automatic mounts that ZFS does
475  * for each file system.  ZFS restricts this so that a new fs must be created
476  * in an existing parent ZFS fs.  It then automatically mounts the new fs
477  * directly under the mountpoint for the parent fs using the last component
478  * of the name as the mountpoint directory.
479  *
480  * For example:
481  *    Name			Mountpoint
482  *    space/eng/dev/test/zone1	/project1/eng/dev/test/zone1
483  *
484  * Return Z_OK if the path mapped to a ZFS file system name, otherwise return
485  * Z_ERR.
486  */
487 static int
488 path2name(char *zonepath, char *zfs_name, int len)
489 {
490 	int		res;
491 	char		*p;
492 	zfs_handle_t	*zhp;
493 
494 	if ((p = strrchr(zonepath, '/')) == NULL)
495 		return (Z_ERR);
496 
497 	/*
498 	 * If the parent directory is not its own ZFS fs, then we can't
499 	 * automatically create a new ZFS fs at the 'zonepath' mountpoint
500 	 * so return an error.
501 	 */
502 	*p = '\0';
503 	zhp = mount2zhandle(zonepath);
504 	*p = '/';
505 	if (zhp == NULL)
506 		return (Z_ERR);
507 
508 	res = snprintf(zfs_name, len, "%s/%s", zfs_get_name(zhp), p + 1);
509 
510 	zfs_close(zhp);
511 	if (res >= len)
512 		return (Z_ERR);
513 
514 	return (Z_OK);
515 }
516 
517 /*
518  * A ZFS file system iterator call-back function used to determine if the
519  * file system has dependents (snapshots & clones).
520  */
521 /* ARGSUSED */
522 static int
523 has_dependent(zfs_handle_t *zhp, void *data)
524 {
525 	zfs_close(zhp);
526 	return (1);
527 }
528 
529 /*
530  * Given a snapshot name, get the file system path where the snapshot lives.
531  * A snapshot name is of the form fs_name@snap_name.  For example, snapshot
532  * pl/zones/z1@SUNWzone1 would have a path of
533  * /pl/zones/z1/.zfs/snapshot/SUNWzone1.
534  */
535 static int
536 snap2path(char *snap_name, char *path, int len)
537 {
538 	char		*p;
539 	zfs_handle_t	*zhp;
540 	char		mp[ZFS_MAXPROPLEN];
541 
542 	if ((p = strrchr(snap_name, '@')) == NULL)
543 		return (Z_ERR);
544 
545 	/* Get the file system name from the snap_name. */
546 	*p = '\0';
547 	zhp = zfs_open(g_zfs, snap_name, ZFS_TYPE_ANY);
548 	*p = '@';
549 	if (zhp == NULL)
550 		return (Z_ERR);
551 
552 	/* Get the file system mount point. */
553 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mp, sizeof (mp), NULL, NULL,
554 	    0, B_FALSE) != 0) {
555 		zfs_close(zhp);
556 		return (Z_ERR);
557 	}
558 	zfs_close(zhp);
559 
560 	p++;
561 	if (snprintf(path, len, "%s/.zfs/snapshot/%s", mp, p) >= len)
562 		return (Z_ERR);
563 
564 	return (Z_OK);
565 }
566 
567 /*
568  * Clone a pre-existing ZFS snapshot, either by making a direct ZFS clone, if
569  * possible, or by copying the data from the snapshot to the zonepath.
570  */
571 int
572 clone_snapshot_zfs(char *snap_name, char *zonepath)
573 {
574 	int	err = Z_OK;
575 	char	clone_name[MAXPATHLEN];
576 	char	snap_path[MAXPATHLEN];
577 
578 	if (snap2path(snap_name, snap_path, sizeof (snap_path)) != Z_OK) {
579 		(void) fprintf(stderr, gettext("unable to find path for %s.\n"),
580 		    snap_name);
581 		return (Z_ERR);
582 	}
583 
584 	if (validate_snapshot(snap_name, snap_path) != Z_OK)
585 		return (Z_NO_ENTRY);
586 
587 	/*
588 	 * The zonepath cannot be ZFS cloned, try to copy the data from
589 	 * within the snapshot to the zonepath.
590 	 */
591 	if (path2name(zonepath, clone_name, sizeof (clone_name)) != Z_OK) {
592 		if ((err = clone_copy(snap_path, zonepath)) == Z_OK)
593 			if (clean_out_clone() != Z_OK)
594 				(void) fprintf(stderr,
595 				    gettext("could not remove the "
596 				    "software inventory from %s\n"), zonepath);
597 
598 		return (err);
599 	}
600 
601 	if ((err = clone_snap(snap_name, clone_name)) != Z_OK) {
602 		if (err != Z_NO_ENTRY) {
603 			/*
604 			 * Cloning the snapshot failed.  Fall back to trying
605 			 * to install the zone by copying from the snapshot.
606 			 */
607 			if ((err = clone_copy(snap_path, zonepath)) == Z_OK)
608 				if (clean_out_clone() != Z_OK)
609 					(void) fprintf(stderr,
610 					    gettext("could not remove the "
611 					    "software inventory from %s\n"),
612 					    zonepath);
613 		} else {
614 			/*
615 			 * The snapshot is unusable for some reason so restore
616 			 * the zone state to configured since we were unable to
617 			 * actually do anything about getting the zone
618 			 * installed.
619 			 */
620 			int tmp;
621 
622 			if ((tmp = zone_set_state(target_zone,
623 			    ZONE_STATE_CONFIGURED)) != Z_OK) {
624 				errno = tmp;
625 				zperror2(target_zone,
626 				    gettext("could not set state"));
627 			}
628 		}
629 	}
630 
631 	return (err);
632 }
633 
634 /*
635  * Attempt to clone a source_zone to a target zonepath by using a ZFS clone.
636  */
637 int
638 clone_zfs(char *source_zone, char *source_zonepath, char *zonepath)
639 {
640 	zfs_handle_t	*zhp;
641 	char		clone_name[MAXPATHLEN];
642 	char		snap_name[MAXPATHLEN];
643 
644 	/*
645 	 * Try to get a zfs handle for the source_zonepath.  If this fails
646 	 * the source_zonepath is not ZFS so return an error.
647 	 */
648 	if ((zhp = mount2zhandle(source_zonepath)) == NULL)
649 		return (Z_ERR);
650 
651 	/*
652 	 * Check if there is a file system already mounted on zonepath.  If so,
653 	 * we can't clone to the path so we should fall back to copying.
654 	 */
655 	if (is_mountpnt(zonepath)) {
656 		zfs_close(zhp);
657 		(void) fprintf(stderr,
658 		    gettext("A file system is already mounted on %s,\n"
659 		    "preventing use of a ZFS clone.\n"), zonepath);
660 		return (Z_ERR);
661 	}
662 
663 	/*
664 	 * Instead of using path2name to get the clone name from the zonepath,
665 	 * we could generate a name from the source zone ZFS name.  However,
666 	 * this would mean we would create the clone under the ZFS fs of the
667 	 * source instead of what the zonepath says.  For example,
668 	 *
669 	 * source_zonepath		zonepath
670 	 * /pl/zones/dev/z1		/pl/zones/deploy/z2
671 	 *
672 	 * We don't want the clone to be under "dev", we want it under
673 	 * "deploy", so that we can leverage the normal attribute inheritance
674 	 * that ZFS provides in the fs hierarchy.
675 	 */
676 	if (path2name(zonepath, clone_name, sizeof (clone_name)) != Z_OK) {
677 		zfs_close(zhp);
678 		return (Z_ERR);
679 	}
680 
681 	if (take_snapshot(source_zone, zhp, snap_name, sizeof (snap_name))
682 	    != Z_OK) {
683 		zfs_close(zhp);
684 		return (Z_ERR);
685 	}
686 	zfs_close(zhp);
687 
688 	if (clone_snap(snap_name, clone_name) != Z_OK)
689 		return (Z_ERR);
690 
691 	(void) printf(gettext("Instead of copying, a ZFS clone has been "
692 	    "created for this zone.\n"));
693 
694 	return (Z_OK);
695 }
696 
697 /*
698  * Attempt to create a ZFS file system for the specified zonepath.
699  * We either will successfully create a ZFS file system and get it mounted
700  * on the zonepath or we don't.  The caller doesn't care since a regular
701  * directory is used for the zonepath if no ZFS file system is mounted there.
702  */
703 void
704 create_zfs_zonepath(char *zonepath)
705 {
706 	zfs_handle_t	*zhp;
707 	char		zfs_name[MAXPATHLEN];
708 	nvlist_t	*props = NULL;
709 
710 	if (path2name(zonepath, zfs_name, sizeof (zfs_name)) != Z_OK)
711 		return;
712 
713 	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0 ||
714 	    nvlist_add_string(props, zfs_prop_to_name(ZFS_PROP_SHARENFS),
715 	    "off") != 0) {
716 		if (props != NULL)
717 			nvlist_free(props);
718 		(void) fprintf(stderr, gettext("cannot create ZFS dataset %s: "
719 		    "out of memory\n"), zfs_name);
720 	}
721 
722 	if (zfs_create(g_zfs, zfs_name, ZFS_TYPE_FILESYSTEM, props) != 0 ||
723 	    (zhp = zfs_open(g_zfs, zfs_name, ZFS_TYPE_ANY)) == NULL) {
724 		(void) fprintf(stderr, gettext("cannot create ZFS dataset %s: "
725 		    "%s\n"), zfs_name, libzfs_error_description(g_zfs));
726 		nvlist_free(props);
727 		return;
728 	}
729 
730 	nvlist_free(props);
731 
732 	if (zfs_mount(zhp, NULL, 0) != 0) {
733 		(void) fprintf(stderr, gettext("cannot mount ZFS dataset %s: "
734 		    "%s\n"), zfs_name, libzfs_error_description(g_zfs));
735 		(void) zfs_destroy(zhp);
736 	} else {
737 		if (chmod(zonepath, S_IRWXU) != 0) {
738 			(void) fprintf(stderr, gettext("file system %s "
739 			    "successfully created, but chmod %o failed: %s\n"),
740 			    zfs_name, S_IRWXU, strerror(errno));
741 			(void) destroy_zfs(zonepath);
742 		} else {
743 			(void) printf(gettext("A ZFS file system has been "
744 			    "created for this zone.\n"));
745 		}
746 	}
747 
748 	zfs_close(zhp);
749 }
750 
751 /*
752  * If the zonepath is a ZFS file system, attempt to destroy it.  We return Z_OK
753  * if we were able to zfs_destroy the zonepath, otherwise we return Z_ERR
754  * which means the caller should clean up the zonepath in the traditional
755  * way.
756  */
757 int
758 destroy_zfs(char *zonepath)
759 {
760 	zfs_handle_t	*zhp;
761 	boolean_t	is_clone = B_FALSE;
762 	char		origin[ZFS_MAXPROPLEN];
763 
764 	if ((zhp = mount2zhandle(zonepath)) == NULL)
765 		return (Z_ERR);
766 
767 	/*
768 	 * We can't destroy the file system if it has dependents.
769 	 */
770 	if (zfs_iter_dependents(zhp, B_TRUE, has_dependent, NULL) != 0 ||
771 	    zfs_unmount(zhp, NULL, 0) != 0) {
772 		zfs_close(zhp);
773 		return (Z_ERR);
774 	}
775 
776 	/*
777 	 * This might be a clone.  Try to get the snapshot so we can attempt
778 	 * to destroy that as well.
779 	 */
780 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin, sizeof (origin), NULL,
781 	    NULL, 0, B_FALSE) == 0)
782 		is_clone = B_TRUE;
783 
784 	if (zfs_destroy(zhp) != 0) {
785 		/*
786 		 * If the destroy fails for some reason, try to remount
787 		 * the file system so that we can use "rm -rf" to clean up
788 		 * instead.
789 		 */
790 		(void) zfs_mount(zhp, NULL, 0);
791 		zfs_close(zhp);
792 		return (Z_ERR);
793 	}
794 
795 	(void) printf(gettext("The ZFS file system for this zone has been "
796 	    "destroyed.\n"));
797 
798 	if (is_clone) {
799 		zfs_handle_t	*ohp;
800 
801 		/*
802 		 * Try to clean up the snapshot that the clone was taken from.
803 		 */
804 		if ((ohp = zfs_open(g_zfs, origin,
805 		    ZFS_TYPE_SNAPSHOT)) != NULL) {
806 			if (zfs_iter_dependents(ohp, B_TRUE, has_dependent,
807 			    NULL) == 0 && zfs_unmount(ohp, NULL, 0) == 0)
808 				(void) zfs_destroy(ohp);
809 			zfs_close(ohp);
810 		}
811 	}
812 
813 	zfs_close(zhp);
814 	return (Z_OK);
815 }
816 
817 /*
818  * Return true if the path is its own zfs file system.  We determine this
819  * by stat-ing the path to see if it is zfs and stat-ing the parent to see
820  * if it is a different fs.
821  */
822 boolean_t
823 is_zonepath_zfs(char *zonepath)
824 {
825 	int res;
826 	char *path;
827 	char *parent;
828 	struct statvfs64 buf1, buf2;
829 
830 	if (statvfs64(zonepath, &buf1) != 0)
831 		return (B_FALSE);
832 
833 	if (strcmp(buf1.f_basetype, "zfs") != 0)
834 		return (B_FALSE);
835 
836 	if ((path = strdup(zonepath)) == NULL)
837 		return (B_FALSE);
838 
839 	parent = dirname(path);
840 	res = statvfs64(parent, &buf2);
841 	free(path);
842 
843 	if (res != 0)
844 		return (B_FALSE);
845 
846 	if (buf1.f_fsid == buf2.f_fsid)
847 		return (B_FALSE);
848 
849 	return (B_TRUE);
850 }
851 
852 /*
853  * Implement the fast move of a ZFS file system by simply updating the
854  * mountpoint.  Since it is file system already, we don't have the
855  * issue of cross-file system copying.
856  */
857 int
858 move_zfs(char *zonepath, char *new_zonepath)
859 {
860 	int		ret = Z_ERR;
861 	zfs_handle_t	*zhp;
862 
863 	if ((zhp = mount2zhandle(zonepath)) == NULL)
864 		return (Z_ERR);
865 
866 	if (zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
867 	    new_zonepath) == 0) {
868 		/*
869 		 * Clean up the old mount point.  We ignore any failure since
870 		 * the zone is already successfully mounted on the new path.
871 		 */
872 		(void) rmdir(zonepath);
873 		ret = Z_OK;
874 	}
875 
876 	zfs_close(zhp);
877 
878 	return (ret);
879 }
880 
881 /*
882  * Validate that the given dataset exists on the system, and that neither it nor
883  * its children are zvols.
884  *
885  * Note that we don't do anything with the 'zoned' property here.  All
886  * management is done in zoneadmd when the zone is actually rebooted.  This
887  * allows us to automatically set the zoned property even when a zone is
888  * rebooted by the administrator.
889  */
890 int
891 verify_datasets(zone_dochandle_t handle)
892 {
893 	int return_code = Z_OK;
894 	struct zone_dstab dstab;
895 	zfs_handle_t *zhp;
896 	char propbuf[ZFS_MAXPROPLEN];
897 	char source[ZFS_MAXNAMELEN];
898 	zfs_source_t srctype;
899 
900 	if (zonecfg_setdsent(handle) != Z_OK) {
901 		/*
902 		 * TRANSLATION_NOTE
903 		 * zfs and dataset are literals that should not be translated.
904 		 */
905 		(void) fprintf(stderr, gettext("could not verify zfs datasets: "
906 		    "unable to enumerate datasets\n"));
907 		return (Z_ERR);
908 	}
909 
910 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
911 
912 		if ((zhp = zfs_open(g_zfs, dstab.zone_dataset_name,
913 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
914 			(void) fprintf(stderr, gettext("could not verify zfs "
915 			    "dataset %s: %s\n"), dstab.zone_dataset_name,
916 			    libzfs_error_description(g_zfs));
917 			return_code = Z_ERR;
918 			continue;
919 		}
920 
921 		if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf,
922 		    sizeof (propbuf), &srctype, source,
923 		    sizeof (source), 0) == 0 &&
924 		    (srctype == ZFS_SRC_INHERITED)) {
925 			(void) fprintf(stderr, gettext("could not verify zfs "
926 			    "dataset %s: mountpoint cannot be inherited\n"),
927 			    dstab.zone_dataset_name);
928 			return_code = Z_ERR;
929 			zfs_close(zhp);
930 			continue;
931 		}
932 
933 		if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
934 			(void) fprintf(stderr, gettext("cannot verify zfs "
935 			    "dataset %s: volumes cannot be specified as a "
936 			    "zone dataset resource\n"),
937 			    dstab.zone_dataset_name);
938 			return_code = Z_ERR;
939 		}
940 
941 		if (zfs_iter_children(zhp, check_zvol, NULL) != 0)
942 			return_code = Z_ERR;
943 
944 		zfs_close(zhp);
945 	}
946 	(void) zonecfg_enddsent(handle);
947 
948 	return (return_code);
949 }
950 
951 /*
952  * Verify that the ZFS dataset exists, and its mountpoint
953  * property is set to "legacy".
954  */
955 int
956 verify_fs_zfs(struct zone_fstab *fstab)
957 {
958 	zfs_handle_t *zhp;
959 	char propbuf[ZFS_MAXPROPLEN];
960 
961 	if ((zhp = zfs_open(g_zfs, fstab->zone_fs_special,
962 	    ZFS_TYPE_ANY)) == NULL) {
963 		(void) fprintf(stderr, gettext("could not verify fs %s: "
964 		    "could not access zfs dataset '%s'\n"),
965 		    fstab->zone_fs_dir, fstab->zone_fs_special);
966 		return (Z_ERR);
967 	}
968 
969 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
970 		(void) fprintf(stderr, gettext("cannot verify fs %s: "
971 		    "'%s' is not a file system\n"),
972 		    fstab->zone_fs_dir, fstab->zone_fs_special);
973 		zfs_close(zhp);
974 		return (Z_ERR);
975 	}
976 
977 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, sizeof (propbuf),
978 	    NULL, NULL, 0, 0) != 0 || strcmp(propbuf, "legacy") != 0) {
979 		(void) fprintf(stderr, gettext("could not verify fs %s: "
980 		    "zfs '%s' mountpoint is not \"legacy\"\n"),
981 		    fstab->zone_fs_dir, fstab->zone_fs_special);
982 		zfs_close(zhp);
983 		return (Z_ERR);
984 	}
985 
986 	zfs_close(zhp);
987 	return (Z_OK);
988 }
989 
990 int
991 init_zfs(void)
992 {
993 	if ((g_zfs = libzfs_init()) == NULL) {
994 		(void) fprintf(stderr, gettext("failed to initialize ZFS "
995 		    "library\n"));
996 		return (Z_ERR);
997 	}
998 
999 	return (Z_OK);
1000 }
1001