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 /* 23ff17c8bfSgjelinek * 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> 4711506c41Sgjelinek #include <sys/mntent.h> 480b5de56dSgjelinek 490b5de56dSgjelinek #include "zoneadm.h" 500b5de56dSgjelinek 5199653d4eSeschrock libzfs_handle_t *g_zfs; 520b5de56dSgjelinek 530b5de56dSgjelinek typedef struct zfs_mount_data { 540b5de56dSgjelinek char *match_name; 550b5de56dSgjelinek zfs_handle_t *match_handle; 560b5de56dSgjelinek } zfs_mount_data_t; 570b5de56dSgjelinek 580b5de56dSgjelinek typedef struct zfs_snapshot_data { 590b5de56dSgjelinek char *match_name; 600b5de56dSgjelinek int len; 610b5de56dSgjelinek int max; 620b5de56dSgjelinek } zfs_snapshot_data_t; 630b5de56dSgjelinek 640b5de56dSgjelinek /* 650b5de56dSgjelinek * A ZFS file system iterator call-back function which is used to validate 660b5de56dSgjelinek * datasets imported into the zone. 670b5de56dSgjelinek */ 680b5de56dSgjelinek /* ARGSUSED */ 690b5de56dSgjelinek static int 700b5de56dSgjelinek check_zvol(zfs_handle_t *zhp, void *unused) 710b5de56dSgjelinek { 720b5de56dSgjelinek int ret; 730b5de56dSgjelinek 740b5de56dSgjelinek if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) { 750b5de56dSgjelinek /* 760b5de56dSgjelinek * TRANSLATION_NOTE 770b5de56dSgjelinek * zfs and dataset are literals that should not be translated. 780b5de56dSgjelinek */ 790b5de56dSgjelinek (void) fprintf(stderr, gettext("cannot verify zfs dataset %s: " 800b5de56dSgjelinek "volumes cannot be specified as a zone dataset resource\n"), 810b5de56dSgjelinek zfs_get_name(zhp)); 820b5de56dSgjelinek ret = -1; 830b5de56dSgjelinek } else { 840b5de56dSgjelinek ret = zfs_iter_children(zhp, check_zvol, NULL); 850b5de56dSgjelinek } 860b5de56dSgjelinek 870b5de56dSgjelinek zfs_close(zhp); 880b5de56dSgjelinek 890b5de56dSgjelinek return (ret); 900b5de56dSgjelinek } 910b5de56dSgjelinek 920b5de56dSgjelinek /* 930b5de56dSgjelinek * A ZFS file system iterator call-back function which returns the 940b5de56dSgjelinek * zfs_handle_t for a ZFS file system on the specified mount point. 950b5de56dSgjelinek */ 960b5de56dSgjelinek static int 970b5de56dSgjelinek match_mountpoint(zfs_handle_t *zhp, void *data) 980b5de56dSgjelinek { 990b5de56dSgjelinek int res; 1000b5de56dSgjelinek zfs_mount_data_t *cbp; 1010b5de56dSgjelinek char mp[ZFS_MAXPROPLEN]; 1020b5de56dSgjelinek 1030b5de56dSgjelinek if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 1040b5de56dSgjelinek zfs_close(zhp); 1050b5de56dSgjelinek return (0); 1060b5de56dSgjelinek } 1070b5de56dSgjelinek 10811506c41Sgjelinek /* First check if the dataset is mounted. */ 10911506c41Sgjelinek if (zfs_prop_get(zhp, ZFS_PROP_MOUNTED, mp, sizeof (mp), NULL, NULL, 11011506c41Sgjelinek 0, B_FALSE) != 0 || strcmp(mp, "no") == 0) { 11111506c41Sgjelinek zfs_close(zhp); 11211506c41Sgjelinek return (0); 11311506c41Sgjelinek } 11411506c41Sgjelinek 11511506c41Sgjelinek /* Now check mount point. */ 1160b5de56dSgjelinek if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mp, sizeof (mp), NULL, NULL, 11711506c41Sgjelinek 0, B_FALSE) != 0) { 11811506c41Sgjelinek zfs_close(zhp); 11911506c41Sgjelinek return (0); 12011506c41Sgjelinek } 12111506c41Sgjelinek 12211506c41Sgjelinek cbp = (zfs_mount_data_t *)data; 12311506c41Sgjelinek 12411506c41Sgjelinek if (strcmp(mp, "legacy") == 0) { 12511506c41Sgjelinek /* If legacy, must look in mnttab for mountpoint. */ 12611506c41Sgjelinek FILE *fp; 12711506c41Sgjelinek struct mnttab entry; 12811506c41Sgjelinek const char *nm; 12911506c41Sgjelinek 13011506c41Sgjelinek nm = zfs_get_name(zhp); 13111506c41Sgjelinek if ((fp = fopen(MNTTAB, "r")) == NULL) { 13211506c41Sgjelinek zfs_close(zhp); 13311506c41Sgjelinek return (0); 13411506c41Sgjelinek } 13511506c41Sgjelinek 13611506c41Sgjelinek while (getmntent(fp, &entry) == 0) { 13711506c41Sgjelinek if (strcmp(nm, entry.mnt_special) == 0) { 13811506c41Sgjelinek if (strcmp(entry.mnt_mountp, cbp->match_name) 13911506c41Sgjelinek == 0) { 14011506c41Sgjelinek (void) fclose(fp); 14111506c41Sgjelinek cbp->match_handle = zhp; 14211506c41Sgjelinek return (1); 14311506c41Sgjelinek } 14411506c41Sgjelinek break; 14511506c41Sgjelinek } 14611506c41Sgjelinek } 14711506c41Sgjelinek (void) fclose(fp); 14811506c41Sgjelinek 14911506c41Sgjelinek } else if (strcmp(mp, cbp->match_name) == 0) { 1500b5de56dSgjelinek cbp->match_handle = zhp; 1510b5de56dSgjelinek return (1); 1520b5de56dSgjelinek } 1530b5de56dSgjelinek 15411506c41Sgjelinek /* Iterate over any nested datasets. */ 1550b5de56dSgjelinek res = zfs_iter_filesystems(zhp, match_mountpoint, data); 1560b5de56dSgjelinek zfs_close(zhp); 1570b5de56dSgjelinek return (res); 1580b5de56dSgjelinek } 1590b5de56dSgjelinek 1600b5de56dSgjelinek /* 1610b5de56dSgjelinek * Get ZFS handle for the specified mount point. 1620b5de56dSgjelinek */ 1630b5de56dSgjelinek static zfs_handle_t * 1640b5de56dSgjelinek mount2zhandle(char *mountpoint) 1650b5de56dSgjelinek { 1660b5de56dSgjelinek zfs_mount_data_t cb; 1670b5de56dSgjelinek 1680b5de56dSgjelinek cb.match_name = mountpoint; 1690b5de56dSgjelinek cb.match_handle = NULL; 17099653d4eSeschrock (void) zfs_iter_root(g_zfs, match_mountpoint, &cb); 1710b5de56dSgjelinek return (cb.match_handle); 1720b5de56dSgjelinek } 1730b5de56dSgjelinek 1740b5de56dSgjelinek /* 1750b5de56dSgjelinek * Check if there is already a file system (zfs or any other type) mounted on 1760b5de56dSgjelinek * path. 1770b5de56dSgjelinek */ 1780b5de56dSgjelinek static boolean_t 1790b5de56dSgjelinek is_mountpnt(char *path) 1800b5de56dSgjelinek { 1810b5de56dSgjelinek FILE *fp; 1820b5de56dSgjelinek struct mnttab entry; 1830b5de56dSgjelinek 18411506c41Sgjelinek if ((fp = fopen(MNTTAB, "r")) == NULL) 1850b5de56dSgjelinek return (B_FALSE); 1860b5de56dSgjelinek 1870b5de56dSgjelinek while (getmntent(fp, &entry) == 0) { 1880b5de56dSgjelinek if (strcmp(path, entry.mnt_mountp) == 0) { 1890b5de56dSgjelinek (void) fclose(fp); 1900b5de56dSgjelinek return (B_TRUE); 1910b5de56dSgjelinek } 1920b5de56dSgjelinek } 1930b5de56dSgjelinek 1940b5de56dSgjelinek (void) fclose(fp); 1950b5de56dSgjelinek return (B_FALSE); 1960b5de56dSgjelinek } 1970b5de56dSgjelinek 1980b5de56dSgjelinek /* 199ff17c8bfSgjelinek * Run the brand's pre-snapshot hook before we take a ZFS snapshot of the zone. 2000b5de56dSgjelinek */ 2010b5de56dSgjelinek static int 202ff17c8bfSgjelinek pre_snapshot(char *presnapbuf) 2030b5de56dSgjelinek { 204ff17c8bfSgjelinek int status; 2050b5de56dSgjelinek 206ff17c8bfSgjelinek /* No brand-specific handler */ 207ff17c8bfSgjelinek if (presnapbuf[0] == '\0') 208ff17c8bfSgjelinek return (Z_OK); 209ff17c8bfSgjelinek 210ff17c8bfSgjelinek /* Run the hook */ 211ff17c8bfSgjelinek status = do_subproc_interactive(presnapbuf); 212ff17c8bfSgjelinek if ((status = subproc_status(gettext("brand-specific presnapshot"), 213ff17c8bfSgjelinek status, B_FALSE)) != ZONE_SUBPROC_OK) 2140b5de56dSgjelinek return (Z_ERR); 2150b5de56dSgjelinek 2160b5de56dSgjelinek return (Z_OK); 2170b5de56dSgjelinek } 2180b5de56dSgjelinek 2190b5de56dSgjelinek /* 220ff17c8bfSgjelinek * Run the brand's post-snapshot hook after we take a ZFS snapshot of the zone. 2210b5de56dSgjelinek */ 2220b5de56dSgjelinek static int 223ff17c8bfSgjelinek post_snapshot(char *postsnapbuf) 2240b5de56dSgjelinek { 225ff17c8bfSgjelinek int status; 2260b5de56dSgjelinek 227ff17c8bfSgjelinek /* No brand-specific handler */ 228ff17c8bfSgjelinek if (postsnapbuf[0] == '\0') 229ff17c8bfSgjelinek return (Z_OK); 230ff17c8bfSgjelinek 231ff17c8bfSgjelinek /* Run the hook */ 232ff17c8bfSgjelinek status = do_subproc_interactive(postsnapbuf); 233ff17c8bfSgjelinek if ((status = subproc_status(gettext("brand-specific postsnapshot"), 234ff17c8bfSgjelinek status, B_FALSE)) != ZONE_SUBPROC_OK) 2350b5de56dSgjelinek return (Z_ERR); 2360b5de56dSgjelinek 2370b5de56dSgjelinek return (Z_OK); 2380b5de56dSgjelinek } 2390b5de56dSgjelinek 2400b5de56dSgjelinek /* 2410b5de56dSgjelinek * This is a ZFS snapshot iterator call-back function which returns the 2420b5de56dSgjelinek * highest number of SUNWzone snapshots that have been taken. 2430b5de56dSgjelinek */ 2440b5de56dSgjelinek static int 2450b5de56dSgjelinek get_snap_max(zfs_handle_t *zhp, void *data) 2460b5de56dSgjelinek { 2470b5de56dSgjelinek int res; 2480b5de56dSgjelinek zfs_snapshot_data_t *cbp; 2490b5de56dSgjelinek 2500b5de56dSgjelinek if (zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) { 2510b5de56dSgjelinek zfs_close(zhp); 2520b5de56dSgjelinek return (0); 2530b5de56dSgjelinek } 2540b5de56dSgjelinek 2550b5de56dSgjelinek cbp = (zfs_snapshot_data_t *)data; 2560b5de56dSgjelinek 2570b5de56dSgjelinek if (strncmp(zfs_get_name(zhp), cbp->match_name, cbp->len) == 0) { 2580b5de56dSgjelinek char *nump; 2590b5de56dSgjelinek int num; 2600b5de56dSgjelinek 2610b5de56dSgjelinek nump = (char *)(zfs_get_name(zhp) + cbp->len); 2620b5de56dSgjelinek num = atoi(nump); 2630b5de56dSgjelinek if (num > cbp->max) 2640b5de56dSgjelinek cbp->max = num; 2650b5de56dSgjelinek } 2660b5de56dSgjelinek 2670b5de56dSgjelinek res = zfs_iter_snapshots(zhp, get_snap_max, data); 2680b5de56dSgjelinek zfs_close(zhp); 2690b5de56dSgjelinek return (res); 2700b5de56dSgjelinek } 2710b5de56dSgjelinek 2720b5de56dSgjelinek /* 2730b5de56dSgjelinek * Take a ZFS snapshot to be used for cloning the zone. 2740b5de56dSgjelinek */ 2750b5de56dSgjelinek static int 276ff17c8bfSgjelinek take_snapshot(zfs_handle_t *zhp, char *snapshot_name, int snap_size, 277ff17c8bfSgjelinek char *presnapbuf, char *postsnapbuf) 2780b5de56dSgjelinek { 2790b5de56dSgjelinek int res; 2800b5de56dSgjelinek char template[ZFS_MAXNAMELEN]; 2810b5de56dSgjelinek zfs_snapshot_data_t cb; 2820b5de56dSgjelinek 2830b5de56dSgjelinek /* 2840b5de56dSgjelinek * First we need to figure out the next available name for the 2850b5de56dSgjelinek * zone snapshot. Look through the list of zones snapshots for 2860b5de56dSgjelinek * this file system to determine the maximum snapshot name. 2870b5de56dSgjelinek */ 2880b5de56dSgjelinek if (snprintf(template, sizeof (template), "%s@SUNWzone", 2890b5de56dSgjelinek zfs_get_name(zhp)) >= sizeof (template)) 2900b5de56dSgjelinek return (Z_ERR); 2910b5de56dSgjelinek 2920b5de56dSgjelinek cb.match_name = template; 2930b5de56dSgjelinek cb.len = strlen(template); 2940b5de56dSgjelinek cb.max = 0; 2950b5de56dSgjelinek 2960b5de56dSgjelinek if (zfs_iter_snapshots(zhp, get_snap_max, &cb) != 0) 2970b5de56dSgjelinek return (Z_ERR); 2980b5de56dSgjelinek 2990b5de56dSgjelinek cb.max++; 3000b5de56dSgjelinek 3010b5de56dSgjelinek if (snprintf(snapshot_name, snap_size, "%s@SUNWzone%d", 3020b5de56dSgjelinek zfs_get_name(zhp), cb.max) >= snap_size) 3030b5de56dSgjelinek return (Z_ERR); 3040b5de56dSgjelinek 305ff17c8bfSgjelinek if (pre_snapshot(presnapbuf) != Z_OK) 3060b5de56dSgjelinek return (Z_ERR); 307*bb0ade09Sahrens res = zfs_snapshot(g_zfs, snapshot_name, B_FALSE, NULL); 308ff17c8bfSgjelinek if (post_snapshot(postsnapbuf) != Z_OK) 3090b5de56dSgjelinek return (Z_ERR); 3100b5de56dSgjelinek 3110b5de56dSgjelinek if (res != 0) 3120b5de56dSgjelinek return (Z_ERR); 3130b5de56dSgjelinek return (Z_OK); 3140b5de56dSgjelinek } 3150b5de56dSgjelinek 3160b5de56dSgjelinek /* 3170b5de56dSgjelinek * We are using an explicit snapshot from some earlier point in time so 318ff17c8bfSgjelinek * we need to validate it. Run the brand specific hook. 3190b5de56dSgjelinek */ 3200b5de56dSgjelinek static int 321ff17c8bfSgjelinek validate_snapshot(char *snapshot_name, char *snap_path, char *validsnapbuf) 3220b5de56dSgjelinek { 323ff17c8bfSgjelinek int status; 324ff17c8bfSgjelinek char cmdbuf[MAXPATHLEN]; 3250b5de56dSgjelinek 326ff17c8bfSgjelinek /* No brand-specific handler */ 327ff17c8bfSgjelinek if (validsnapbuf[0] == '\0') 328ff17c8bfSgjelinek return (Z_OK); 329ff17c8bfSgjelinek 330ff17c8bfSgjelinek /* pass args - snapshot_name & snap_path */ 331ff17c8bfSgjelinek if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %s %s", validsnapbuf, 332ff17c8bfSgjelinek snapshot_name, snap_path) >= sizeof (cmdbuf)) { 333ff17c8bfSgjelinek zerror("Command line too long"); 3340b5de56dSgjelinek return (Z_ERR); 3350b5de56dSgjelinek } 3360b5de56dSgjelinek 337ff17c8bfSgjelinek /* Run the hook */ 338ff17c8bfSgjelinek status = do_subproc_interactive(cmdbuf); 339ff17c8bfSgjelinek if ((status = subproc_status(gettext("brand-specific validatesnapshot"), 340ff17c8bfSgjelinek status, B_FALSE)) != ZONE_SUBPROC_OK) 3410b5de56dSgjelinek return (Z_ERR); 3420b5de56dSgjelinek 343ff17c8bfSgjelinek return (Z_OK); 3440b5de56dSgjelinek } 3450b5de56dSgjelinek 3460b5de56dSgjelinek /* 3470b5de56dSgjelinek * Remove the sw inventory file from inside this zonepath that we picked up out 3480b5de56dSgjelinek * of the snapshot. 3490b5de56dSgjelinek */ 3500b5de56dSgjelinek static int 3510b5de56dSgjelinek clean_out_clone() 3520b5de56dSgjelinek { 3530b5de56dSgjelinek int err; 3540b5de56dSgjelinek zone_dochandle_t handle; 3550b5de56dSgjelinek 3560b5de56dSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 3570b5de56dSgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3580b5de56dSgjelinek return (Z_ERR); 3590b5de56dSgjelinek } 3600b5de56dSgjelinek 3610b5de56dSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 3620b5de56dSgjelinek errno = err; 3630b5de56dSgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3640b5de56dSgjelinek zonecfg_fini_handle(handle); 3650b5de56dSgjelinek return (Z_ERR); 3660b5de56dSgjelinek } 3670b5de56dSgjelinek 3680b5de56dSgjelinek zonecfg_rm_detached(handle, B_FALSE); 3690b5de56dSgjelinek zonecfg_fini_handle(handle); 3700b5de56dSgjelinek 3710b5de56dSgjelinek return (Z_OK); 3720b5de56dSgjelinek } 3730b5de56dSgjelinek 3740b5de56dSgjelinek /* 3750b5de56dSgjelinek * Make a ZFS clone on zonepath from snapshot_name. 3760b5de56dSgjelinek */ 3770b5de56dSgjelinek static int 3780b5de56dSgjelinek clone_snap(char *snapshot_name, char *zonepath) 3790b5de56dSgjelinek { 3800b5de56dSgjelinek int res = Z_OK; 3810b5de56dSgjelinek int err; 3820b5de56dSgjelinek zfs_handle_t *zhp; 3830b5de56dSgjelinek zfs_handle_t *clone; 384e9dbad6fSeschrock nvlist_t *props = NULL; 3850b5de56dSgjelinek 38699653d4eSeschrock if ((zhp = zfs_open(g_zfs, snapshot_name, ZFS_TYPE_SNAPSHOT)) == NULL) 3870b5de56dSgjelinek return (Z_NO_ENTRY); 3880b5de56dSgjelinek 3890b5de56dSgjelinek (void) printf(gettext("Cloning snapshot %s\n"), snapshot_name); 3900b5de56dSgjelinek 391e9dbad6fSeschrock if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0 || 3925f8e1617Snn35248 nvlist_add_string(props, zfs_prop_to_name(ZFS_PROP_SHARENFS), 3935f8e1617Snn35248 "off") != 0) { 3945f8e1617Snn35248 if (props != NULL) 395e9dbad6fSeschrock nvlist_free(props); 396e9dbad6fSeschrock (void) fprintf(stderr, gettext("could not create ZFS clone " 397e9dbad6fSeschrock "%s: out of memory\n"), zonepath); 398e9dbad6fSeschrock return (Z_ERR); 399e9dbad6fSeschrock } 400e9dbad6fSeschrock 401e9dbad6fSeschrock err = zfs_clone(zhp, zonepath, props); 4020b5de56dSgjelinek zfs_close(zhp); 403e9dbad6fSeschrock 404e9dbad6fSeschrock nvlist_free(props); 405e9dbad6fSeschrock 4060b5de56dSgjelinek if (err != 0) 4070b5de56dSgjelinek return (Z_ERR); 4080b5de56dSgjelinek 4090b5de56dSgjelinek /* create the mountpoint if necessary */ 410990b4856Slling if ((clone = zfs_open(g_zfs, zonepath, ZFS_TYPE_DATASET)) == NULL) 4110b5de56dSgjelinek return (Z_ERR); 4120b5de56dSgjelinek 4130b5de56dSgjelinek /* 4140b5de56dSgjelinek * The clone has been created so we need to print a diagnostic 4150b5de56dSgjelinek * message if one of the following steps fails for some reason. 4160b5de56dSgjelinek */ 4170b5de56dSgjelinek if (zfs_mount(clone, NULL, 0) != 0) { 4180b5de56dSgjelinek (void) fprintf(stderr, gettext("could not mount ZFS clone " 4190b5de56dSgjelinek "%s\n"), zfs_get_name(clone)); 4200b5de56dSgjelinek res = Z_ERR; 4210b5de56dSgjelinek 422e9dbad6fSeschrock } else if (clean_out_clone() != Z_OK) { 4230b5de56dSgjelinek (void) fprintf(stderr, gettext("could not remove the " 4240b5de56dSgjelinek "software inventory from ZFS clone %s\n"), 4250b5de56dSgjelinek zfs_get_name(clone)); 4260b5de56dSgjelinek res = Z_ERR; 4270b5de56dSgjelinek } 4280b5de56dSgjelinek 4290b5de56dSgjelinek zfs_close(clone); 4300b5de56dSgjelinek return (res); 4310b5de56dSgjelinek } 4320b5de56dSgjelinek 4330b5de56dSgjelinek /* 4340b5de56dSgjelinek * This function takes a zonepath and attempts to determine what the ZFS 4350b5de56dSgjelinek * file system name (not mountpoint) should be for that path. We do not 4360b5de56dSgjelinek * assume that zonepath is an existing directory or ZFS fs since we use 4370b5de56dSgjelinek * this function as part of the process of creating a new ZFS fs or clone. 4380b5de56dSgjelinek * 4390b5de56dSgjelinek * The way this works is that we look at the parent directory of the zonepath 4400b5de56dSgjelinek * to see if it is a ZFS fs. If it is, we get the name of that ZFS fs and 4410b5de56dSgjelinek * append the last component of the zonepath to generate the ZFS name for the 4420b5de56dSgjelinek * zonepath. This matches the algorithm that ZFS uses for automatically 4430b5de56dSgjelinek * mounting a new fs after it is created. 4440b5de56dSgjelinek * 4450b5de56dSgjelinek * Although a ZFS fs can be mounted anywhere, we don't worry about handling 4460b5de56dSgjelinek * all of the complexity that a user could possibly configure with arbitrary 4470b5de56dSgjelinek * mounts since there is no way to generate a ZFS name from a random path in 4480b5de56dSgjelinek * the file system. We only try to handle the automatic mounts that ZFS does 4490b5de56dSgjelinek * for each file system. ZFS restricts this so that a new fs must be created 4500b5de56dSgjelinek * in an existing parent ZFS fs. It then automatically mounts the new fs 4510b5de56dSgjelinek * directly under the mountpoint for the parent fs using the last component 4520b5de56dSgjelinek * of the name as the mountpoint directory. 4530b5de56dSgjelinek * 4540b5de56dSgjelinek * For example: 4550b5de56dSgjelinek * Name Mountpoint 4560b5de56dSgjelinek * space/eng/dev/test/zone1 /project1/eng/dev/test/zone1 4570b5de56dSgjelinek * 4580b5de56dSgjelinek * Return Z_OK if the path mapped to a ZFS file system name, otherwise return 4590b5de56dSgjelinek * Z_ERR. 4600b5de56dSgjelinek */ 4610b5de56dSgjelinek static int 4620b5de56dSgjelinek path2name(char *zonepath, char *zfs_name, int len) 4630b5de56dSgjelinek { 4640b5de56dSgjelinek int res; 46511506c41Sgjelinek char *bnm, *dnm, *dname, *bname; 4660b5de56dSgjelinek zfs_handle_t *zhp; 46711506c41Sgjelinek struct stat stbuf; 4680b5de56dSgjelinek 4690b5de56dSgjelinek /* 47011506c41Sgjelinek * We need two tmp strings to handle paths directly in / (e.g. /foo) 47111506c41Sgjelinek * since dirname will overwrite the first char after "/" in this case. 4720b5de56dSgjelinek */ 47311506c41Sgjelinek if ((bnm = strdup(zonepath)) == NULL) 4740b5de56dSgjelinek return (Z_ERR); 4750b5de56dSgjelinek 47611506c41Sgjelinek if ((dnm = strdup(zonepath)) == NULL) { 47711506c41Sgjelinek free(bnm); 47811506c41Sgjelinek return (Z_ERR); 47911506c41Sgjelinek } 4800b5de56dSgjelinek 48111506c41Sgjelinek bname = basename(bnm); 48211506c41Sgjelinek dname = dirname(dnm); 48311506c41Sgjelinek 48411506c41Sgjelinek /* 48511506c41Sgjelinek * This is a quick test to save iterating over all of the zfs datasets 48611506c41Sgjelinek * on the system (which can be a lot). If the parent dir is not in a 48711506c41Sgjelinek * ZFS fs, then we're done. 48811506c41Sgjelinek */ 48911506c41Sgjelinek if (stat(dname, &stbuf) != 0 || !S_ISDIR(stbuf.st_mode) || 49011506c41Sgjelinek strcmp(stbuf.st_fstype, MNTTYPE_ZFS) != 0) { 49111506c41Sgjelinek free(bnm); 49211506c41Sgjelinek free(dnm); 49311506c41Sgjelinek return (Z_ERR); 49411506c41Sgjelinek } 49511506c41Sgjelinek 49611506c41Sgjelinek /* See if the parent directory is its own ZFS dataset. */ 49711506c41Sgjelinek if ((zhp = mount2zhandle(dname)) == NULL) { 49811506c41Sgjelinek /* 49911506c41Sgjelinek * The parent is not a ZFS dataset so we can't automatically 50011506c41Sgjelinek * create a dataset on the given path. 50111506c41Sgjelinek */ 50211506c41Sgjelinek free(bnm); 50311506c41Sgjelinek free(dnm); 50411506c41Sgjelinek return (Z_ERR); 50511506c41Sgjelinek } 50611506c41Sgjelinek 50711506c41Sgjelinek res = snprintf(zfs_name, len, "%s/%s", zfs_get_name(zhp), bname); 50811506c41Sgjelinek 50911506c41Sgjelinek free(bnm); 51011506c41Sgjelinek free(dnm); 5110b5de56dSgjelinek zfs_close(zhp); 5120b5de56dSgjelinek if (res >= len) 5130b5de56dSgjelinek return (Z_ERR); 5140b5de56dSgjelinek 5150b5de56dSgjelinek return (Z_OK); 5160b5de56dSgjelinek } 5170b5de56dSgjelinek 5180b5de56dSgjelinek /* 5190b5de56dSgjelinek * A ZFS file system iterator call-back function used to determine if the 5200b5de56dSgjelinek * file system has dependents (snapshots & clones). 5210b5de56dSgjelinek */ 5220b5de56dSgjelinek /* ARGSUSED */ 5230b5de56dSgjelinek static int 5240b5de56dSgjelinek has_dependent(zfs_handle_t *zhp, void *data) 5250b5de56dSgjelinek { 5260b5de56dSgjelinek zfs_close(zhp); 5270b5de56dSgjelinek return (1); 5280b5de56dSgjelinek } 5290b5de56dSgjelinek 5300b5de56dSgjelinek /* 5310b5de56dSgjelinek * Given a snapshot name, get the file system path where the snapshot lives. 5320b5de56dSgjelinek * A snapshot name is of the form fs_name@snap_name. For example, snapshot 5330b5de56dSgjelinek * pl/zones/z1@SUNWzone1 would have a path of 5340b5de56dSgjelinek * /pl/zones/z1/.zfs/snapshot/SUNWzone1. 5350b5de56dSgjelinek */ 5360b5de56dSgjelinek static int 5370b5de56dSgjelinek snap2path(char *snap_name, char *path, int len) 5380b5de56dSgjelinek { 5390b5de56dSgjelinek char *p; 5400b5de56dSgjelinek zfs_handle_t *zhp; 5410b5de56dSgjelinek char mp[ZFS_MAXPROPLEN]; 5420b5de56dSgjelinek 5430b5de56dSgjelinek if ((p = strrchr(snap_name, '@')) == NULL) 5440b5de56dSgjelinek return (Z_ERR); 5450b5de56dSgjelinek 5460b5de56dSgjelinek /* Get the file system name from the snap_name. */ 5470b5de56dSgjelinek *p = '\0'; 548990b4856Slling zhp = zfs_open(g_zfs, snap_name, ZFS_TYPE_DATASET); 5490b5de56dSgjelinek *p = '@'; 5500b5de56dSgjelinek if (zhp == NULL) 5510b5de56dSgjelinek return (Z_ERR); 5520b5de56dSgjelinek 5530b5de56dSgjelinek /* Get the file system mount point. */ 5540b5de56dSgjelinek if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mp, sizeof (mp), NULL, NULL, 55599653d4eSeschrock 0, B_FALSE) != 0) { 5560b5de56dSgjelinek zfs_close(zhp); 5570b5de56dSgjelinek return (Z_ERR); 5580b5de56dSgjelinek } 5590b5de56dSgjelinek zfs_close(zhp); 5600b5de56dSgjelinek 5610b5de56dSgjelinek p++; 5620b5de56dSgjelinek if (snprintf(path, len, "%s/.zfs/snapshot/%s", mp, p) >= len) 5630b5de56dSgjelinek return (Z_ERR); 5640b5de56dSgjelinek 5650b5de56dSgjelinek return (Z_OK); 5660b5de56dSgjelinek } 5670b5de56dSgjelinek 5680b5de56dSgjelinek /* 5690b5de56dSgjelinek * Clone a pre-existing ZFS snapshot, either by making a direct ZFS clone, if 5700b5de56dSgjelinek * possible, or by copying the data from the snapshot to the zonepath. 5710b5de56dSgjelinek */ 5720b5de56dSgjelinek int 573ff17c8bfSgjelinek clone_snapshot_zfs(char *snap_name, char *zonepath, char *validatesnap) 5740b5de56dSgjelinek { 5750b5de56dSgjelinek int err = Z_OK; 5760b5de56dSgjelinek char clone_name[MAXPATHLEN]; 5770b5de56dSgjelinek char snap_path[MAXPATHLEN]; 5780b5de56dSgjelinek 5790b5de56dSgjelinek if (snap2path(snap_name, snap_path, sizeof (snap_path)) != Z_OK) { 5800b5de56dSgjelinek (void) fprintf(stderr, gettext("unable to find path for %s.\n"), 5810b5de56dSgjelinek snap_name); 5820b5de56dSgjelinek return (Z_ERR); 5830b5de56dSgjelinek } 5840b5de56dSgjelinek 585ff17c8bfSgjelinek if (validate_snapshot(snap_name, snap_path, validatesnap) != Z_OK) 5860b5de56dSgjelinek return (Z_NO_ENTRY); 5870b5de56dSgjelinek 5880b5de56dSgjelinek /* 5890b5de56dSgjelinek * The zonepath cannot be ZFS cloned, try to copy the data from 5900b5de56dSgjelinek * within the snapshot to the zonepath. 5910b5de56dSgjelinek */ 5920b5de56dSgjelinek if (path2name(zonepath, clone_name, sizeof (clone_name)) != Z_OK) { 5930b5de56dSgjelinek if ((err = clone_copy(snap_path, zonepath)) == Z_OK) 5940b5de56dSgjelinek if (clean_out_clone() != Z_OK) 5950b5de56dSgjelinek (void) fprintf(stderr, 5960b5de56dSgjelinek gettext("could not remove the " 5970b5de56dSgjelinek "software inventory from %s\n"), zonepath); 5980b5de56dSgjelinek 5990b5de56dSgjelinek return (err); 6000b5de56dSgjelinek } 6010b5de56dSgjelinek 6020b5de56dSgjelinek if ((err = clone_snap(snap_name, clone_name)) != Z_OK) { 6030b5de56dSgjelinek if (err != Z_NO_ENTRY) { 6040b5de56dSgjelinek /* 6050b5de56dSgjelinek * Cloning the snapshot failed. Fall back to trying 6060b5de56dSgjelinek * to install the zone by copying from the snapshot. 6070b5de56dSgjelinek */ 6080b5de56dSgjelinek if ((err = clone_copy(snap_path, zonepath)) == Z_OK) 6090b5de56dSgjelinek if (clean_out_clone() != Z_OK) 6100b5de56dSgjelinek (void) fprintf(stderr, 6110b5de56dSgjelinek gettext("could not remove the " 6120b5de56dSgjelinek "software inventory from %s\n"), 6130b5de56dSgjelinek zonepath); 6140b5de56dSgjelinek } else { 6150b5de56dSgjelinek /* 6160b5de56dSgjelinek * The snapshot is unusable for some reason so restore 6170b5de56dSgjelinek * the zone state to configured since we were unable to 6180b5de56dSgjelinek * actually do anything about getting the zone 6190b5de56dSgjelinek * installed. 6200b5de56dSgjelinek */ 6210b5de56dSgjelinek int tmp; 6220b5de56dSgjelinek 6230b5de56dSgjelinek if ((tmp = zone_set_state(target_zone, 6240b5de56dSgjelinek ZONE_STATE_CONFIGURED)) != Z_OK) { 6250b5de56dSgjelinek errno = tmp; 6260b5de56dSgjelinek zperror2(target_zone, 6270b5de56dSgjelinek gettext("could not set state")); 6280b5de56dSgjelinek } 6290b5de56dSgjelinek } 6300b5de56dSgjelinek } 6310b5de56dSgjelinek 6320b5de56dSgjelinek return (err); 6330b5de56dSgjelinek } 6340b5de56dSgjelinek 6350b5de56dSgjelinek /* 6360b5de56dSgjelinek * Attempt to clone a source_zone to a target zonepath by using a ZFS clone. 6370b5de56dSgjelinek */ 6380b5de56dSgjelinek int 639ff17c8bfSgjelinek clone_zfs(char *source_zonepath, char *zonepath, char *presnapbuf, 640ff17c8bfSgjelinek char *postsnapbuf) 6410b5de56dSgjelinek { 6420b5de56dSgjelinek zfs_handle_t *zhp; 6430b5de56dSgjelinek char clone_name[MAXPATHLEN]; 6440b5de56dSgjelinek char snap_name[MAXPATHLEN]; 6450b5de56dSgjelinek 6460b5de56dSgjelinek /* 6470b5de56dSgjelinek * Try to get a zfs handle for the source_zonepath. If this fails 6480b5de56dSgjelinek * the source_zonepath is not ZFS so return an error. 6490b5de56dSgjelinek */ 6500b5de56dSgjelinek if ((zhp = mount2zhandle(source_zonepath)) == NULL) 6510b5de56dSgjelinek return (Z_ERR); 6520b5de56dSgjelinek 6530b5de56dSgjelinek /* 6540b5de56dSgjelinek * Check if there is a file system already mounted on zonepath. If so, 6550b5de56dSgjelinek * we can't clone to the path so we should fall back to copying. 6560b5de56dSgjelinek */ 6570b5de56dSgjelinek if (is_mountpnt(zonepath)) { 6580b5de56dSgjelinek zfs_close(zhp); 6590b5de56dSgjelinek (void) fprintf(stderr, 6600b5de56dSgjelinek gettext("A file system is already mounted on %s,\n" 6610b5de56dSgjelinek "preventing use of a ZFS clone.\n"), zonepath); 6620b5de56dSgjelinek return (Z_ERR); 6630b5de56dSgjelinek } 6640b5de56dSgjelinek 6650b5de56dSgjelinek /* 6660b5de56dSgjelinek * Instead of using path2name to get the clone name from the zonepath, 6670b5de56dSgjelinek * we could generate a name from the source zone ZFS name. However, 6680b5de56dSgjelinek * this would mean we would create the clone under the ZFS fs of the 6690b5de56dSgjelinek * source instead of what the zonepath says. For example, 6700b5de56dSgjelinek * 6710b5de56dSgjelinek * source_zonepath zonepath 6720b5de56dSgjelinek * /pl/zones/dev/z1 /pl/zones/deploy/z2 6730b5de56dSgjelinek * 6740b5de56dSgjelinek * We don't want the clone to be under "dev", we want it under 6750b5de56dSgjelinek * "deploy", so that we can leverage the normal attribute inheritance 6760b5de56dSgjelinek * that ZFS provides in the fs hierarchy. 6770b5de56dSgjelinek */ 6780b5de56dSgjelinek if (path2name(zonepath, clone_name, sizeof (clone_name)) != Z_OK) { 6790b5de56dSgjelinek zfs_close(zhp); 6800b5de56dSgjelinek return (Z_ERR); 6810b5de56dSgjelinek } 6820b5de56dSgjelinek 683ff17c8bfSgjelinek if (take_snapshot(zhp, snap_name, sizeof (snap_name), presnapbuf, 684ff17c8bfSgjelinek postsnapbuf) != Z_OK) { 6850b5de56dSgjelinek zfs_close(zhp); 6860b5de56dSgjelinek return (Z_ERR); 6870b5de56dSgjelinek } 6880b5de56dSgjelinek zfs_close(zhp); 6890b5de56dSgjelinek 690d9e728a2Sgjelinek if (clone_snap(snap_name, clone_name) != Z_OK) { 691d9e728a2Sgjelinek /* Clean up the snapshot we just took. */ 692d9e728a2Sgjelinek if ((zhp = zfs_open(g_zfs, snap_name, ZFS_TYPE_SNAPSHOT)) 693d9e728a2Sgjelinek != NULL) { 694d9e728a2Sgjelinek if (zfs_unmount(zhp, NULL, 0) == 0) 695d9e728a2Sgjelinek (void) zfs_destroy(zhp); 696d9e728a2Sgjelinek zfs_close(zhp); 697d9e728a2Sgjelinek } 698d9e728a2Sgjelinek 6990b5de56dSgjelinek return (Z_ERR); 700d9e728a2Sgjelinek } 7010b5de56dSgjelinek 7020b5de56dSgjelinek (void) printf(gettext("Instead of copying, a ZFS clone has been " 7030b5de56dSgjelinek "created for this zone.\n")); 7040b5de56dSgjelinek 7050b5de56dSgjelinek return (Z_OK); 7060b5de56dSgjelinek } 7070b5de56dSgjelinek 7080b5de56dSgjelinek /* 7090b5de56dSgjelinek * Attempt to create a ZFS file system for the specified zonepath. 7100b5de56dSgjelinek * We either will successfully create a ZFS file system and get it mounted 7110b5de56dSgjelinek * on the zonepath or we don't. The caller doesn't care since a regular 7120b5de56dSgjelinek * directory is used for the zonepath if no ZFS file system is mounted there. 7130b5de56dSgjelinek */ 7140b5de56dSgjelinek void 7150b5de56dSgjelinek create_zfs_zonepath(char *zonepath) 7160b5de56dSgjelinek { 7170b5de56dSgjelinek zfs_handle_t *zhp; 7180b5de56dSgjelinek char zfs_name[MAXPATHLEN]; 719e9dbad6fSeschrock nvlist_t *props = NULL; 7200b5de56dSgjelinek 7210b5de56dSgjelinek if (path2name(zonepath, zfs_name, sizeof (zfs_name)) != Z_OK) 7220b5de56dSgjelinek return; 7230b5de56dSgjelinek 724e9dbad6fSeschrock if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0 || 7255f8e1617Snn35248 nvlist_add_string(props, zfs_prop_to_name(ZFS_PROP_SHARENFS), 7265f8e1617Snn35248 "off") != 0) { 7275f8e1617Snn35248 if (props != NULL) 728e9dbad6fSeschrock nvlist_free(props); 729e9dbad6fSeschrock (void) fprintf(stderr, gettext("cannot create ZFS dataset %s: " 730e9dbad6fSeschrock "out of memory\n"), zfs_name); 731e9dbad6fSeschrock } 732e9dbad6fSeschrock 733e9dbad6fSeschrock if (zfs_create(g_zfs, zfs_name, ZFS_TYPE_FILESYSTEM, props) != 0 || 734990b4856Slling (zhp = zfs_open(g_zfs, zfs_name, ZFS_TYPE_DATASET)) == NULL) { 73599653d4eSeschrock (void) fprintf(stderr, gettext("cannot create ZFS dataset %s: " 73699653d4eSeschrock "%s\n"), zfs_name, libzfs_error_description(g_zfs)); 737e9dbad6fSeschrock nvlist_free(props); 7380b5de56dSgjelinek return; 7390b5de56dSgjelinek } 7400b5de56dSgjelinek 741e9dbad6fSeschrock nvlist_free(props); 742e9dbad6fSeschrock 7430b5de56dSgjelinek if (zfs_mount(zhp, NULL, 0) != 0) { 74499653d4eSeschrock (void) fprintf(stderr, gettext("cannot mount ZFS dataset %s: " 74599653d4eSeschrock "%s\n"), zfs_name, libzfs_error_description(g_zfs)); 7460b5de56dSgjelinek (void) zfs_destroy(zhp); 7470b5de56dSgjelinek } else { 7480b5de56dSgjelinek if (chmod(zonepath, S_IRWXU) != 0) { 7490b5de56dSgjelinek (void) fprintf(stderr, gettext("file system %s " 7500b5de56dSgjelinek "successfully created, but chmod %o failed: %s\n"), 7510b5de56dSgjelinek zfs_name, S_IRWXU, strerror(errno)); 7520b5de56dSgjelinek (void) destroy_zfs(zonepath); 7530b5de56dSgjelinek } else { 7540b5de56dSgjelinek (void) printf(gettext("A ZFS file system has been " 7550b5de56dSgjelinek "created for this zone.\n")); 7560b5de56dSgjelinek } 7570b5de56dSgjelinek } 7580b5de56dSgjelinek 7590b5de56dSgjelinek zfs_close(zhp); 7600b5de56dSgjelinek } 7610b5de56dSgjelinek 7620b5de56dSgjelinek /* 7630b5de56dSgjelinek * If the zonepath is a ZFS file system, attempt to destroy it. We return Z_OK 7640b5de56dSgjelinek * if we were able to zfs_destroy the zonepath, otherwise we return Z_ERR 7650b5de56dSgjelinek * which means the caller should clean up the zonepath in the traditional 7660b5de56dSgjelinek * way. 7670b5de56dSgjelinek */ 7680b5de56dSgjelinek int 7690b5de56dSgjelinek destroy_zfs(char *zonepath) 7700b5de56dSgjelinek { 7710b5de56dSgjelinek zfs_handle_t *zhp; 7720b5de56dSgjelinek boolean_t is_clone = B_FALSE; 7730b5de56dSgjelinek char origin[ZFS_MAXPROPLEN]; 7740b5de56dSgjelinek 77599653d4eSeschrock if ((zhp = mount2zhandle(zonepath)) == NULL) 7760b5de56dSgjelinek return (Z_ERR); 7770b5de56dSgjelinek 7780b5de56dSgjelinek /* 7790b5de56dSgjelinek * We can't destroy the file system if it has dependents. 7800b5de56dSgjelinek */ 7813bb79becSeschrock if (zfs_iter_dependents(zhp, B_TRUE, has_dependent, NULL) != 0 || 7820b5de56dSgjelinek zfs_unmount(zhp, NULL, 0) != 0) { 7830b5de56dSgjelinek zfs_close(zhp); 7840b5de56dSgjelinek return (Z_ERR); 7850b5de56dSgjelinek } 7860b5de56dSgjelinek 7870b5de56dSgjelinek /* 7880b5de56dSgjelinek * This might be a clone. Try to get the snapshot so we can attempt 7890b5de56dSgjelinek * to destroy that as well. 7900b5de56dSgjelinek */ 7910b5de56dSgjelinek if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin, sizeof (origin), NULL, 79299653d4eSeschrock NULL, 0, B_FALSE) == 0) 7930b5de56dSgjelinek is_clone = B_TRUE; 7940b5de56dSgjelinek 7950b5de56dSgjelinek if (zfs_destroy(zhp) != 0) { 7960b5de56dSgjelinek /* 7970b5de56dSgjelinek * If the destroy fails for some reason, try to remount 7980b5de56dSgjelinek * the file system so that we can use "rm -rf" to clean up 7990b5de56dSgjelinek * instead. 8000b5de56dSgjelinek */ 8010b5de56dSgjelinek (void) zfs_mount(zhp, NULL, 0); 8020b5de56dSgjelinek zfs_close(zhp); 8030b5de56dSgjelinek return (Z_ERR); 8040b5de56dSgjelinek } 8050b5de56dSgjelinek 806d9e728a2Sgjelinek /* 807d9e728a2Sgjelinek * If the zone has ever been moved then the mountpoint dir will not be 808d9e728a2Sgjelinek * cleaned up by the zfs_destroy(). To handle this case try to clean 809d9e728a2Sgjelinek * it up now but don't worry if it fails, that will be normal. 810d9e728a2Sgjelinek */ 811d9e728a2Sgjelinek (void) rmdir(zonepath); 812d9e728a2Sgjelinek 8130b5de56dSgjelinek (void) printf(gettext("The ZFS file system for this zone has been " 8140b5de56dSgjelinek "destroyed.\n")); 8150b5de56dSgjelinek 8160b5de56dSgjelinek if (is_clone) { 8170b5de56dSgjelinek zfs_handle_t *ohp; 8180b5de56dSgjelinek 8190b5de56dSgjelinek /* 8200b5de56dSgjelinek * Try to clean up the snapshot that the clone was taken from. 8210b5de56dSgjelinek */ 82299653d4eSeschrock if ((ohp = zfs_open(g_zfs, origin, 82399653d4eSeschrock ZFS_TYPE_SNAPSHOT)) != NULL) { 8243bb79becSeschrock if (zfs_iter_dependents(ohp, B_TRUE, has_dependent, 8253bb79becSeschrock NULL) == 0 && zfs_unmount(ohp, NULL, 0) == 0) 8260b5de56dSgjelinek (void) zfs_destroy(ohp); 8270b5de56dSgjelinek zfs_close(ohp); 8280b5de56dSgjelinek } 8290b5de56dSgjelinek } 8300b5de56dSgjelinek 8310b5de56dSgjelinek zfs_close(zhp); 8320b5de56dSgjelinek return (Z_OK); 8330b5de56dSgjelinek } 8340b5de56dSgjelinek 8350b5de56dSgjelinek /* 8360b5de56dSgjelinek * Return true if the path is its own zfs file system. We determine this 8370b5de56dSgjelinek * by stat-ing the path to see if it is zfs and stat-ing the parent to see 8380b5de56dSgjelinek * if it is a different fs. 8390b5de56dSgjelinek */ 8400b5de56dSgjelinek boolean_t 8410b5de56dSgjelinek is_zonepath_zfs(char *zonepath) 8420b5de56dSgjelinek { 8430b5de56dSgjelinek int res; 8440b5de56dSgjelinek char *path; 8450b5de56dSgjelinek char *parent; 8463f2f09c1Sdp struct statvfs64 buf1, buf2; 8470b5de56dSgjelinek 8483f2f09c1Sdp if (statvfs64(zonepath, &buf1) != 0) 8490b5de56dSgjelinek return (B_FALSE); 8500b5de56dSgjelinek 8510b5de56dSgjelinek if (strcmp(buf1.f_basetype, "zfs") != 0) 8520b5de56dSgjelinek return (B_FALSE); 8530b5de56dSgjelinek 8540b5de56dSgjelinek if ((path = strdup(zonepath)) == NULL) 8550b5de56dSgjelinek return (B_FALSE); 8560b5de56dSgjelinek 8570b5de56dSgjelinek parent = dirname(path); 8583f2f09c1Sdp res = statvfs64(parent, &buf2); 8590b5de56dSgjelinek free(path); 8600b5de56dSgjelinek 8610b5de56dSgjelinek if (res != 0) 8620b5de56dSgjelinek return (B_FALSE); 8630b5de56dSgjelinek 8640b5de56dSgjelinek if (buf1.f_fsid == buf2.f_fsid) 8650b5de56dSgjelinek return (B_FALSE); 8660b5de56dSgjelinek 8670b5de56dSgjelinek return (B_TRUE); 8680b5de56dSgjelinek } 8690b5de56dSgjelinek 8700b5de56dSgjelinek /* 8710b5de56dSgjelinek * Implement the fast move of a ZFS file system by simply updating the 8720b5de56dSgjelinek * mountpoint. Since it is file system already, we don't have the 8730b5de56dSgjelinek * issue of cross-file system copying. 8740b5de56dSgjelinek */ 8750b5de56dSgjelinek int 8760b5de56dSgjelinek move_zfs(char *zonepath, char *new_zonepath) 8770b5de56dSgjelinek { 8780b5de56dSgjelinek int ret = Z_ERR; 8790b5de56dSgjelinek zfs_handle_t *zhp; 8800b5de56dSgjelinek 88199653d4eSeschrock if ((zhp = mount2zhandle(zonepath)) == NULL) 8820b5de56dSgjelinek return (Z_ERR); 8830b5de56dSgjelinek 884e9dbad6fSeschrock if (zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 885e9dbad6fSeschrock new_zonepath) == 0) { 8860b5de56dSgjelinek /* 8870b5de56dSgjelinek * Clean up the old mount point. We ignore any failure since 8880b5de56dSgjelinek * the zone is already successfully mounted on the new path. 8890b5de56dSgjelinek */ 8900b5de56dSgjelinek (void) rmdir(zonepath); 8910b5de56dSgjelinek ret = Z_OK; 8920b5de56dSgjelinek } 8930b5de56dSgjelinek 8940b5de56dSgjelinek zfs_close(zhp); 8950b5de56dSgjelinek 8960b5de56dSgjelinek return (ret); 8970b5de56dSgjelinek } 8980b5de56dSgjelinek 8990b5de56dSgjelinek /* 9000b5de56dSgjelinek * Validate that the given dataset exists on the system, and that neither it nor 9010b5de56dSgjelinek * its children are zvols. 9020b5de56dSgjelinek * 9030b5de56dSgjelinek * Note that we don't do anything with the 'zoned' property here. All 9040b5de56dSgjelinek * management is done in zoneadmd when the zone is actually rebooted. This 9050b5de56dSgjelinek * allows us to automatically set the zoned property even when a zone is 9060b5de56dSgjelinek * rebooted by the administrator. 9070b5de56dSgjelinek */ 9080b5de56dSgjelinek int 9090b5de56dSgjelinek verify_datasets(zone_dochandle_t handle) 9100b5de56dSgjelinek { 9110b5de56dSgjelinek int return_code = Z_OK; 9120b5de56dSgjelinek struct zone_dstab dstab; 9130b5de56dSgjelinek zfs_handle_t *zhp; 9140b5de56dSgjelinek char propbuf[ZFS_MAXPROPLEN]; 9150b5de56dSgjelinek char source[ZFS_MAXNAMELEN]; 916990b4856Slling zprop_source_t srctype; 9170b5de56dSgjelinek 9180b5de56dSgjelinek if (zonecfg_setdsent(handle) != Z_OK) { 9190b5de56dSgjelinek /* 9200b5de56dSgjelinek * TRANSLATION_NOTE 9210b5de56dSgjelinek * zfs and dataset are literals that should not be translated. 9220b5de56dSgjelinek */ 9230b5de56dSgjelinek (void) fprintf(stderr, gettext("could not verify zfs datasets: " 9240b5de56dSgjelinek "unable to enumerate datasets\n")); 9250b5de56dSgjelinek return (Z_ERR); 9260b5de56dSgjelinek } 9270b5de56dSgjelinek 9280b5de56dSgjelinek while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 9290b5de56dSgjelinek 93099653d4eSeschrock if ((zhp = zfs_open(g_zfs, dstab.zone_dataset_name, 9310b5de56dSgjelinek ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) { 93299653d4eSeschrock (void) fprintf(stderr, gettext("could not verify zfs " 93399653d4eSeschrock "dataset %s: %s\n"), dstab.zone_dataset_name, 93499653d4eSeschrock libzfs_error_description(g_zfs)); 9350b5de56dSgjelinek return_code = Z_ERR; 9360b5de56dSgjelinek continue; 9370b5de56dSgjelinek } 9380b5de56dSgjelinek 9390b5de56dSgjelinek if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, 9400b5de56dSgjelinek sizeof (propbuf), &srctype, source, 9410b5de56dSgjelinek sizeof (source), 0) == 0 && 942990b4856Slling (srctype == ZPROP_SRC_INHERITED)) { 9430b5de56dSgjelinek (void) fprintf(stderr, gettext("could not verify zfs " 9440b5de56dSgjelinek "dataset %s: mountpoint cannot be inherited\n"), 9450b5de56dSgjelinek dstab.zone_dataset_name); 9460b5de56dSgjelinek return_code = Z_ERR; 9470b5de56dSgjelinek zfs_close(zhp); 9480b5de56dSgjelinek continue; 9490b5de56dSgjelinek } 9500b5de56dSgjelinek 9510b5de56dSgjelinek if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) { 9520b5de56dSgjelinek (void) fprintf(stderr, gettext("cannot verify zfs " 9530b5de56dSgjelinek "dataset %s: volumes cannot be specified as a " 9540b5de56dSgjelinek "zone dataset resource\n"), 9550b5de56dSgjelinek dstab.zone_dataset_name); 9560b5de56dSgjelinek return_code = Z_ERR; 9570b5de56dSgjelinek } 9580b5de56dSgjelinek 9590b5de56dSgjelinek if (zfs_iter_children(zhp, check_zvol, NULL) != 0) 9600b5de56dSgjelinek return_code = Z_ERR; 9610b5de56dSgjelinek 9620b5de56dSgjelinek zfs_close(zhp); 9630b5de56dSgjelinek } 9640b5de56dSgjelinek (void) zonecfg_enddsent(handle); 9650b5de56dSgjelinek 9660b5de56dSgjelinek return (return_code); 9670b5de56dSgjelinek } 9680b5de56dSgjelinek 9690b5de56dSgjelinek /* 9700b5de56dSgjelinek * Verify that the ZFS dataset exists, and its mountpoint 9710b5de56dSgjelinek * property is set to "legacy". 9720b5de56dSgjelinek */ 9730b5de56dSgjelinek int 9740b5de56dSgjelinek verify_fs_zfs(struct zone_fstab *fstab) 9750b5de56dSgjelinek { 9760b5de56dSgjelinek zfs_handle_t *zhp; 9770b5de56dSgjelinek char propbuf[ZFS_MAXPROPLEN]; 9780b5de56dSgjelinek 97999653d4eSeschrock if ((zhp = zfs_open(g_zfs, fstab->zone_fs_special, 980990b4856Slling ZFS_TYPE_DATASET)) == NULL) { 9810b5de56dSgjelinek (void) fprintf(stderr, gettext("could not verify fs %s: " 9820b5de56dSgjelinek "could not access zfs dataset '%s'\n"), 9830b5de56dSgjelinek fstab->zone_fs_dir, fstab->zone_fs_special); 9840b5de56dSgjelinek return (Z_ERR); 9850b5de56dSgjelinek } 9860b5de56dSgjelinek 9870b5de56dSgjelinek if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 9880b5de56dSgjelinek (void) fprintf(stderr, gettext("cannot verify fs %s: " 9890b5de56dSgjelinek "'%s' is not a file system\n"), 9900b5de56dSgjelinek fstab->zone_fs_dir, fstab->zone_fs_special); 9910b5de56dSgjelinek zfs_close(zhp); 9920b5de56dSgjelinek return (Z_ERR); 9930b5de56dSgjelinek } 9940b5de56dSgjelinek 9950b5de56dSgjelinek if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, sizeof (propbuf), 9960b5de56dSgjelinek NULL, NULL, 0, 0) != 0 || strcmp(propbuf, "legacy") != 0) { 9970b5de56dSgjelinek (void) fprintf(stderr, gettext("could not verify fs %s: " 9980b5de56dSgjelinek "zfs '%s' mountpoint is not \"legacy\"\n"), 9990b5de56dSgjelinek fstab->zone_fs_dir, fstab->zone_fs_special); 10000b5de56dSgjelinek zfs_close(zhp); 10010b5de56dSgjelinek return (Z_ERR); 10020b5de56dSgjelinek } 10030b5de56dSgjelinek 10040b5de56dSgjelinek zfs_close(zhp); 100599653d4eSeschrock return (Z_OK); 100699653d4eSeschrock } 100799653d4eSeschrock 100899653d4eSeschrock int 100999653d4eSeschrock init_zfs(void) 101099653d4eSeschrock { 101199653d4eSeschrock if ((g_zfs = libzfs_init()) == NULL) { 101299653d4eSeschrock (void) fprintf(stderr, gettext("failed to initialize ZFS " 101399653d4eSeschrock "library\n")); 101499653d4eSeschrock return (Z_ERR); 101599653d4eSeschrock } 101699653d4eSeschrock 10170b5de56dSgjelinek return (Z_OK); 10180b5de56dSgjelinek } 1019