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 /* 230b5de56dSgjelinek * Copyright 2006 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 /* 1580b5de56dSgjelinek * Perform any necessary housekeeping tasks we need to do before we take 1590b5de56dSgjelinek * a ZFS snapshot of the zone. What this really entails is that we are 1600b5de56dSgjelinek * taking a sw inventory of the source zone, like we do when we detach, 1610b5de56dSgjelinek * so that there is the XML manifest in the snapshot. We use that to 1620b5de56dSgjelinek * validate the snapshot if it is the source of a clone at some later time. 1630b5de56dSgjelinek */ 1640b5de56dSgjelinek static int 1650b5de56dSgjelinek pre_snapshot(char *source_zone) 1660b5de56dSgjelinek { 1670b5de56dSgjelinek int err; 1680b5de56dSgjelinek zone_dochandle_t handle; 1690b5de56dSgjelinek 1700b5de56dSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 1710b5de56dSgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 1720b5de56dSgjelinek return (Z_ERR); 1730b5de56dSgjelinek } 1740b5de56dSgjelinek 1750b5de56dSgjelinek if ((err = zonecfg_get_handle(source_zone, handle)) != Z_OK) { 1760b5de56dSgjelinek errno = err; 1770b5de56dSgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 1780b5de56dSgjelinek zonecfg_fini_handle(handle); 1790b5de56dSgjelinek return (Z_ERR); 1800b5de56dSgjelinek } 1810b5de56dSgjelinek 1820b5de56dSgjelinek if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) { 1830b5de56dSgjelinek errno = err; 1840b5de56dSgjelinek zperror(gettext("getting the software version information " 1850b5de56dSgjelinek "failed"), B_TRUE); 1860b5de56dSgjelinek zonecfg_fini_handle(handle); 1870b5de56dSgjelinek return (Z_ERR); 1880b5de56dSgjelinek } 1890b5de56dSgjelinek 1908cd327d5Sgjelinek if ((err = zonecfg_detach_save(handle, 0)) != Z_OK) { 1910b5de56dSgjelinek errno = err; 1920b5de56dSgjelinek zperror(gettext("saving the software version manifest failed"), 1930b5de56dSgjelinek B_TRUE); 1940b5de56dSgjelinek zonecfg_fini_handle(handle); 1950b5de56dSgjelinek return (Z_ERR); 1960b5de56dSgjelinek } 1970b5de56dSgjelinek 1980b5de56dSgjelinek zonecfg_fini_handle(handle); 1990b5de56dSgjelinek return (Z_OK); 2000b5de56dSgjelinek } 2010b5de56dSgjelinek 2020b5de56dSgjelinek /* 2030b5de56dSgjelinek * Perform any necessary housekeeping tasks we need to do after we take 2040b5de56dSgjelinek * a ZFS snapshot of the zone. What this really entails is removing the 2050b5de56dSgjelinek * sw inventory XML file from the zone. It is still in the snapshot where 2060b5de56dSgjelinek * we want it, but we don't want it in the source zone itself. 2070b5de56dSgjelinek */ 2080b5de56dSgjelinek static int 2090b5de56dSgjelinek post_snapshot(char *source_zone) 2100b5de56dSgjelinek { 2110b5de56dSgjelinek int err; 2120b5de56dSgjelinek zone_dochandle_t handle; 2130b5de56dSgjelinek 2140b5de56dSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 2150b5de56dSgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 2160b5de56dSgjelinek return (Z_ERR); 2170b5de56dSgjelinek } 2180b5de56dSgjelinek 2190b5de56dSgjelinek if ((err = zonecfg_get_handle(source_zone, handle)) != Z_OK) { 2200b5de56dSgjelinek errno = err; 2210b5de56dSgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 2220b5de56dSgjelinek zonecfg_fini_handle(handle); 2230b5de56dSgjelinek return (Z_ERR); 2240b5de56dSgjelinek } 2250b5de56dSgjelinek 2260b5de56dSgjelinek zonecfg_rm_detached(handle, B_FALSE); 2270b5de56dSgjelinek zonecfg_fini_handle(handle); 2280b5de56dSgjelinek 2290b5de56dSgjelinek return (Z_OK); 2300b5de56dSgjelinek } 2310b5de56dSgjelinek 2320b5de56dSgjelinek /* 2330b5de56dSgjelinek * This is a ZFS snapshot iterator call-back function which returns the 2340b5de56dSgjelinek * highest number of SUNWzone snapshots that have been taken. 2350b5de56dSgjelinek */ 2360b5de56dSgjelinek static int 2370b5de56dSgjelinek get_snap_max(zfs_handle_t *zhp, void *data) 2380b5de56dSgjelinek { 2390b5de56dSgjelinek int res; 2400b5de56dSgjelinek zfs_snapshot_data_t *cbp; 2410b5de56dSgjelinek 2420b5de56dSgjelinek if (zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) { 2430b5de56dSgjelinek zfs_close(zhp); 2440b5de56dSgjelinek return (0); 2450b5de56dSgjelinek } 2460b5de56dSgjelinek 2470b5de56dSgjelinek cbp = (zfs_snapshot_data_t *)data; 2480b5de56dSgjelinek 2490b5de56dSgjelinek if (strncmp(zfs_get_name(zhp), cbp->match_name, cbp->len) == 0) { 2500b5de56dSgjelinek char *nump; 2510b5de56dSgjelinek int num; 2520b5de56dSgjelinek 2530b5de56dSgjelinek nump = (char *)(zfs_get_name(zhp) + cbp->len); 2540b5de56dSgjelinek num = atoi(nump); 2550b5de56dSgjelinek if (num > cbp->max) 2560b5de56dSgjelinek cbp->max = num; 2570b5de56dSgjelinek } 2580b5de56dSgjelinek 2590b5de56dSgjelinek res = zfs_iter_snapshots(zhp, get_snap_max, data); 2600b5de56dSgjelinek zfs_close(zhp); 2610b5de56dSgjelinek return (res); 2620b5de56dSgjelinek } 2630b5de56dSgjelinek 2640b5de56dSgjelinek /* 2650b5de56dSgjelinek * Take a ZFS snapshot to be used for cloning the zone. 2660b5de56dSgjelinek */ 2670b5de56dSgjelinek static int 2680b5de56dSgjelinek take_snapshot(char *source_zone, zfs_handle_t *zhp, char *snapshot_name, 2690b5de56dSgjelinek int snap_size) 2700b5de56dSgjelinek { 2710b5de56dSgjelinek int res; 2720b5de56dSgjelinek char template[ZFS_MAXNAMELEN]; 2730b5de56dSgjelinek zfs_snapshot_data_t cb; 2740b5de56dSgjelinek 2750b5de56dSgjelinek /* 2760b5de56dSgjelinek * First we need to figure out the next available name for the 2770b5de56dSgjelinek * zone snapshot. Look through the list of zones snapshots for 2780b5de56dSgjelinek * this file system to determine the maximum snapshot name. 2790b5de56dSgjelinek */ 2800b5de56dSgjelinek if (snprintf(template, sizeof (template), "%s@SUNWzone", 2810b5de56dSgjelinek zfs_get_name(zhp)) >= sizeof (template)) 2820b5de56dSgjelinek return (Z_ERR); 2830b5de56dSgjelinek 2840b5de56dSgjelinek cb.match_name = template; 2850b5de56dSgjelinek cb.len = strlen(template); 2860b5de56dSgjelinek cb.max = 0; 2870b5de56dSgjelinek 2880b5de56dSgjelinek if (zfs_iter_snapshots(zhp, get_snap_max, &cb) != 0) 2890b5de56dSgjelinek return (Z_ERR); 2900b5de56dSgjelinek 2910b5de56dSgjelinek cb.max++; 2920b5de56dSgjelinek 2930b5de56dSgjelinek if (snprintf(snapshot_name, snap_size, "%s@SUNWzone%d", 2940b5de56dSgjelinek zfs_get_name(zhp), cb.max) >= snap_size) 2950b5de56dSgjelinek return (Z_ERR); 2960b5de56dSgjelinek 2970b5de56dSgjelinek if (pre_snapshot(source_zone) != Z_OK) 2980b5de56dSgjelinek return (Z_ERR); 2991d452cf5Sahrens res = zfs_snapshot(g_zfs, snapshot_name, B_FALSE); 3000b5de56dSgjelinek if (post_snapshot(source_zone) != Z_OK) 3010b5de56dSgjelinek return (Z_ERR); 3020b5de56dSgjelinek 3030b5de56dSgjelinek if (res != 0) 3040b5de56dSgjelinek return (Z_ERR); 3050b5de56dSgjelinek return (Z_OK); 3060b5de56dSgjelinek } 3070b5de56dSgjelinek 3080b5de56dSgjelinek /* 3090b5de56dSgjelinek * We are using an explicit snapshot from some earlier point in time so 3100b5de56dSgjelinek * we need to validate it. This involves checking the sw inventory that 3110b5de56dSgjelinek * we took when we made the snapshot to verify that the current sw config 3120b5de56dSgjelinek * on the host is still valid to run a zone made from this snapshot. 3130b5de56dSgjelinek */ 3140b5de56dSgjelinek static int 3150b5de56dSgjelinek validate_snapshot(char *snapshot_name, char *snap_path) 3160b5de56dSgjelinek { 3170b5de56dSgjelinek int err; 3180b5de56dSgjelinek zone_dochandle_t handle; 3190b5de56dSgjelinek zone_dochandle_t athandle = NULL; 3200b5de56dSgjelinek 3210b5de56dSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 3220b5de56dSgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3230b5de56dSgjelinek return (Z_ERR); 3240b5de56dSgjelinek } 3250b5de56dSgjelinek 3260b5de56dSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 3270b5de56dSgjelinek errno = err; 3280b5de56dSgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3290b5de56dSgjelinek zonecfg_fini_handle(handle); 3300b5de56dSgjelinek return (Z_ERR); 3310b5de56dSgjelinek } 3320b5de56dSgjelinek 3330b5de56dSgjelinek if ((athandle = zonecfg_init_handle()) == NULL) { 3340b5de56dSgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3350b5de56dSgjelinek goto done; 3360b5de56dSgjelinek } 3370b5de56dSgjelinek 3380b5de56dSgjelinek if ((err = zonecfg_get_attach_handle(snap_path, target_zone, B_TRUE, 3390b5de56dSgjelinek athandle)) != Z_OK) { 3400b5de56dSgjelinek if (err == Z_NO_ZONE) 3410b5de56dSgjelinek (void) fprintf(stderr, gettext("snapshot %s was not " 3420b5de56dSgjelinek "taken\n\tby a 'zoneadm clone' command. It can " 3430b5de56dSgjelinek "not be used to clone zones.\n"), snapshot_name); 3440b5de56dSgjelinek else 3450b5de56dSgjelinek (void) fprintf(stderr, gettext("snapshot %s is " 3460b5de56dSgjelinek "out-dated\n\tIt can no longer be used to clone " 3470b5de56dSgjelinek "zones on this system.\n"), snapshot_name); 3480b5de56dSgjelinek goto done; 3490b5de56dSgjelinek } 3500b5de56dSgjelinek 3510b5de56dSgjelinek /* Get the detach information for the locally defined zone. */ 3520b5de56dSgjelinek if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) { 3530b5de56dSgjelinek errno = err; 3540b5de56dSgjelinek zperror(gettext("getting the attach information failed"), 3550b5de56dSgjelinek B_TRUE); 3560b5de56dSgjelinek goto done; 3570b5de56dSgjelinek } 3580b5de56dSgjelinek 3590b5de56dSgjelinek if ((err = sw_cmp(handle, athandle, SW_CMP_SILENT)) != Z_OK) 3600b5de56dSgjelinek (void) fprintf(stderr, gettext("snapshot %s is out-dated\n\t" 3610b5de56dSgjelinek "It can no longer be used to clone zones on this " 3620b5de56dSgjelinek "system.\n"), snapshot_name); 3630b5de56dSgjelinek 3640b5de56dSgjelinek done: 3650b5de56dSgjelinek zonecfg_fini_handle(handle); 3660b5de56dSgjelinek if (athandle != NULL) 3670b5de56dSgjelinek zonecfg_fini_handle(athandle); 3680b5de56dSgjelinek 3690b5de56dSgjelinek return (err); 3700b5de56dSgjelinek } 3710b5de56dSgjelinek 3720b5de56dSgjelinek /* 3730b5de56dSgjelinek * Remove the sw inventory file from inside this zonepath that we picked up out 3740b5de56dSgjelinek * of the snapshot. 3750b5de56dSgjelinek */ 3760b5de56dSgjelinek static int 3770b5de56dSgjelinek clean_out_clone() 3780b5de56dSgjelinek { 3790b5de56dSgjelinek int err; 3800b5de56dSgjelinek zone_dochandle_t handle; 3810b5de56dSgjelinek 3820b5de56dSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 3830b5de56dSgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3840b5de56dSgjelinek return (Z_ERR); 3850b5de56dSgjelinek } 3860b5de56dSgjelinek 3870b5de56dSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 3880b5de56dSgjelinek errno = err; 3890b5de56dSgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3900b5de56dSgjelinek zonecfg_fini_handle(handle); 3910b5de56dSgjelinek return (Z_ERR); 3920b5de56dSgjelinek } 3930b5de56dSgjelinek 3940b5de56dSgjelinek zonecfg_rm_detached(handle, B_FALSE); 3950b5de56dSgjelinek zonecfg_fini_handle(handle); 3960b5de56dSgjelinek 3970b5de56dSgjelinek return (Z_OK); 3980b5de56dSgjelinek } 3990b5de56dSgjelinek 4000b5de56dSgjelinek /* 4010b5de56dSgjelinek * Make a ZFS clone on zonepath from snapshot_name. 4020b5de56dSgjelinek */ 4030b5de56dSgjelinek static int 4040b5de56dSgjelinek clone_snap(char *snapshot_name, char *zonepath) 4050b5de56dSgjelinek { 4060b5de56dSgjelinek int res = Z_OK; 4070b5de56dSgjelinek int err; 4080b5de56dSgjelinek zfs_handle_t *zhp; 4090b5de56dSgjelinek zfs_handle_t *clone; 410*e9dbad6fSeschrock nvlist_t *props = NULL; 4110b5de56dSgjelinek 41299653d4eSeschrock if ((zhp = zfs_open(g_zfs, snapshot_name, ZFS_TYPE_SNAPSHOT)) == NULL) 4130b5de56dSgjelinek return (Z_NO_ENTRY); 4140b5de56dSgjelinek 4150b5de56dSgjelinek (void) printf(gettext("Cloning snapshot %s\n"), snapshot_name); 4160b5de56dSgjelinek 417*e9dbad6fSeschrock if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0 || 418*e9dbad6fSeschrock nvlist_add_boolean_value(props, 419*e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_SHARENFS), B_FALSE) != 0) { 420*e9dbad6fSeschrock nvlist_free(props); 421*e9dbad6fSeschrock (void) fprintf(stderr, gettext("could not create ZFS clone " 422*e9dbad6fSeschrock "%s: out of memory\n"), zonepath); 423*e9dbad6fSeschrock return (Z_ERR); 424*e9dbad6fSeschrock } 425*e9dbad6fSeschrock 426*e9dbad6fSeschrock err = zfs_clone(zhp, zonepath, props); 4270b5de56dSgjelinek zfs_close(zhp); 428*e9dbad6fSeschrock 429*e9dbad6fSeschrock nvlist_free(props); 430*e9dbad6fSeschrock 4310b5de56dSgjelinek if (err != 0) 4320b5de56dSgjelinek return (Z_ERR); 4330b5de56dSgjelinek 4340b5de56dSgjelinek /* create the mountpoint if necessary */ 43599653d4eSeschrock if ((clone = zfs_open(g_zfs, zonepath, ZFS_TYPE_ANY)) == NULL) 4360b5de56dSgjelinek return (Z_ERR); 4370b5de56dSgjelinek 4380b5de56dSgjelinek /* 4390b5de56dSgjelinek * The clone has been created so we need to print a diagnostic 4400b5de56dSgjelinek * message if one of the following steps fails for some reason. 4410b5de56dSgjelinek */ 4420b5de56dSgjelinek if (zfs_mount(clone, NULL, 0) != 0) { 4430b5de56dSgjelinek (void) fprintf(stderr, gettext("could not mount ZFS clone " 4440b5de56dSgjelinek "%s\n"), zfs_get_name(clone)); 4450b5de56dSgjelinek res = Z_ERR; 4460b5de56dSgjelinek 447*e9dbad6fSeschrock } else if (clean_out_clone() != Z_OK) { 4480b5de56dSgjelinek (void) fprintf(stderr, gettext("could not remove the " 4490b5de56dSgjelinek "software inventory from ZFS clone %s\n"), 4500b5de56dSgjelinek zfs_get_name(clone)); 4510b5de56dSgjelinek res = Z_ERR; 4520b5de56dSgjelinek } 4530b5de56dSgjelinek 4540b5de56dSgjelinek zfs_close(clone); 4550b5de56dSgjelinek return (res); 4560b5de56dSgjelinek } 4570b5de56dSgjelinek 4580b5de56dSgjelinek /* 4590b5de56dSgjelinek * This function takes a zonepath and attempts to determine what the ZFS 4600b5de56dSgjelinek * file system name (not mountpoint) should be for that path. We do not 4610b5de56dSgjelinek * assume that zonepath is an existing directory or ZFS fs since we use 4620b5de56dSgjelinek * this function as part of the process of creating a new ZFS fs or clone. 4630b5de56dSgjelinek * 4640b5de56dSgjelinek * The way this works is that we look at the parent directory of the zonepath 4650b5de56dSgjelinek * to see if it is a ZFS fs. If it is, we get the name of that ZFS fs and 4660b5de56dSgjelinek * append the last component of the zonepath to generate the ZFS name for the 4670b5de56dSgjelinek * zonepath. This matches the algorithm that ZFS uses for automatically 4680b5de56dSgjelinek * mounting a new fs after it is created. 4690b5de56dSgjelinek * 4700b5de56dSgjelinek * Although a ZFS fs can be mounted anywhere, we don't worry about handling 4710b5de56dSgjelinek * all of the complexity that a user could possibly configure with arbitrary 4720b5de56dSgjelinek * mounts since there is no way to generate a ZFS name from a random path in 4730b5de56dSgjelinek * the file system. We only try to handle the automatic mounts that ZFS does 4740b5de56dSgjelinek * for each file system. ZFS restricts this so that a new fs must be created 4750b5de56dSgjelinek * in an existing parent ZFS fs. It then automatically mounts the new fs 4760b5de56dSgjelinek * directly under the mountpoint for the parent fs using the last component 4770b5de56dSgjelinek * of the name as the mountpoint directory. 4780b5de56dSgjelinek * 4790b5de56dSgjelinek * For example: 4800b5de56dSgjelinek * Name Mountpoint 4810b5de56dSgjelinek * space/eng/dev/test/zone1 /project1/eng/dev/test/zone1 4820b5de56dSgjelinek * 4830b5de56dSgjelinek * Return Z_OK if the path mapped to a ZFS file system name, otherwise return 4840b5de56dSgjelinek * Z_ERR. 4850b5de56dSgjelinek */ 4860b5de56dSgjelinek static int 4870b5de56dSgjelinek path2name(char *zonepath, char *zfs_name, int len) 4880b5de56dSgjelinek { 4890b5de56dSgjelinek int res; 4900b5de56dSgjelinek char *p; 4910b5de56dSgjelinek zfs_handle_t *zhp; 4920b5de56dSgjelinek 4930b5de56dSgjelinek if ((p = strrchr(zonepath, '/')) == NULL) 4940b5de56dSgjelinek return (Z_ERR); 4950b5de56dSgjelinek 4960b5de56dSgjelinek /* 4970b5de56dSgjelinek * If the parent directory is not its own ZFS fs, then we can't 4980b5de56dSgjelinek * automatically create a new ZFS fs at the 'zonepath' mountpoint 4990b5de56dSgjelinek * so return an error. 5000b5de56dSgjelinek */ 5010b5de56dSgjelinek *p = '\0'; 5020b5de56dSgjelinek zhp = mount2zhandle(zonepath); 5030b5de56dSgjelinek *p = '/'; 5040b5de56dSgjelinek if (zhp == NULL) 5050b5de56dSgjelinek return (Z_ERR); 5060b5de56dSgjelinek 5070b5de56dSgjelinek res = snprintf(zfs_name, len, "%s/%s", zfs_get_name(zhp), p + 1); 5080b5de56dSgjelinek 5090b5de56dSgjelinek zfs_close(zhp); 5100b5de56dSgjelinek if (res >= len) 5110b5de56dSgjelinek return (Z_ERR); 5120b5de56dSgjelinek 5130b5de56dSgjelinek return (Z_OK); 5140b5de56dSgjelinek } 5150b5de56dSgjelinek 5160b5de56dSgjelinek /* 5170b5de56dSgjelinek * A ZFS file system iterator call-back function used to determine if the 5180b5de56dSgjelinek * file system has dependents (snapshots & clones). 5190b5de56dSgjelinek */ 5200b5de56dSgjelinek /* ARGSUSED */ 5210b5de56dSgjelinek static int 5220b5de56dSgjelinek has_dependent(zfs_handle_t *zhp, void *data) 5230b5de56dSgjelinek { 5240b5de56dSgjelinek zfs_close(zhp); 5250b5de56dSgjelinek return (1); 5260b5de56dSgjelinek } 5270b5de56dSgjelinek 5280b5de56dSgjelinek /* 5290b5de56dSgjelinek * Given a snapshot name, get the file system path where the snapshot lives. 5300b5de56dSgjelinek * A snapshot name is of the form fs_name@snap_name. For example, snapshot 5310b5de56dSgjelinek * pl/zones/z1@SUNWzone1 would have a path of 5320b5de56dSgjelinek * /pl/zones/z1/.zfs/snapshot/SUNWzone1. 5330b5de56dSgjelinek */ 5340b5de56dSgjelinek static int 5350b5de56dSgjelinek snap2path(char *snap_name, char *path, int len) 5360b5de56dSgjelinek { 5370b5de56dSgjelinek char *p; 5380b5de56dSgjelinek zfs_handle_t *zhp; 5390b5de56dSgjelinek char mp[ZFS_MAXPROPLEN]; 5400b5de56dSgjelinek 5410b5de56dSgjelinek if ((p = strrchr(snap_name, '@')) == NULL) 5420b5de56dSgjelinek return (Z_ERR); 5430b5de56dSgjelinek 5440b5de56dSgjelinek /* Get the file system name from the snap_name. */ 5450b5de56dSgjelinek *p = '\0'; 54699653d4eSeschrock zhp = zfs_open(g_zfs, snap_name, ZFS_TYPE_ANY); 5470b5de56dSgjelinek *p = '@'; 5480b5de56dSgjelinek if (zhp == NULL) 5490b5de56dSgjelinek return (Z_ERR); 5500b5de56dSgjelinek 5510b5de56dSgjelinek /* Get the file system mount point. */ 5520b5de56dSgjelinek if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mp, sizeof (mp), NULL, NULL, 55399653d4eSeschrock 0, B_FALSE) != 0) { 5540b5de56dSgjelinek zfs_close(zhp); 5550b5de56dSgjelinek return (Z_ERR); 5560b5de56dSgjelinek } 5570b5de56dSgjelinek zfs_close(zhp); 5580b5de56dSgjelinek 5590b5de56dSgjelinek p++; 5600b5de56dSgjelinek if (snprintf(path, len, "%s/.zfs/snapshot/%s", mp, p) >= len) 5610b5de56dSgjelinek return (Z_ERR); 5620b5de56dSgjelinek 5630b5de56dSgjelinek return (Z_OK); 5640b5de56dSgjelinek } 5650b5de56dSgjelinek 5660b5de56dSgjelinek /* 5670b5de56dSgjelinek * Clone a pre-existing ZFS snapshot, either by making a direct ZFS clone, if 5680b5de56dSgjelinek * possible, or by copying the data from the snapshot to the zonepath. 5690b5de56dSgjelinek */ 5700b5de56dSgjelinek int 5710b5de56dSgjelinek clone_snapshot_zfs(char *snap_name, char *zonepath) 5720b5de56dSgjelinek { 5730b5de56dSgjelinek int err = Z_OK; 5740b5de56dSgjelinek char clone_name[MAXPATHLEN]; 5750b5de56dSgjelinek char snap_path[MAXPATHLEN]; 5760b5de56dSgjelinek 5770b5de56dSgjelinek if (snap2path(snap_name, snap_path, sizeof (snap_path)) != Z_OK) { 5780b5de56dSgjelinek (void) fprintf(stderr, gettext("unable to find path for %s.\n"), 5790b5de56dSgjelinek snap_name); 5800b5de56dSgjelinek return (Z_ERR); 5810b5de56dSgjelinek } 5820b5de56dSgjelinek 5830b5de56dSgjelinek if (validate_snapshot(snap_name, snap_path) != Z_OK) 5840b5de56dSgjelinek return (Z_NO_ENTRY); 5850b5de56dSgjelinek 5860b5de56dSgjelinek /* 5870b5de56dSgjelinek * The zonepath cannot be ZFS cloned, try to copy the data from 5880b5de56dSgjelinek * within the snapshot to the zonepath. 5890b5de56dSgjelinek */ 5900b5de56dSgjelinek if (path2name(zonepath, clone_name, sizeof (clone_name)) != Z_OK) { 5910b5de56dSgjelinek if ((err = clone_copy(snap_path, zonepath)) == Z_OK) 5920b5de56dSgjelinek if (clean_out_clone() != Z_OK) 5930b5de56dSgjelinek (void) fprintf(stderr, 5940b5de56dSgjelinek gettext("could not remove the " 5950b5de56dSgjelinek "software inventory from %s\n"), zonepath); 5960b5de56dSgjelinek 5970b5de56dSgjelinek return (err); 5980b5de56dSgjelinek } 5990b5de56dSgjelinek 6000b5de56dSgjelinek if ((err = clone_snap(snap_name, clone_name)) != Z_OK) { 6010b5de56dSgjelinek if (err != Z_NO_ENTRY) { 6020b5de56dSgjelinek /* 6030b5de56dSgjelinek * Cloning the snapshot failed. Fall back to trying 6040b5de56dSgjelinek * to install the zone by copying from the snapshot. 6050b5de56dSgjelinek */ 6060b5de56dSgjelinek if ((err = clone_copy(snap_path, zonepath)) == Z_OK) 6070b5de56dSgjelinek if (clean_out_clone() != Z_OK) 6080b5de56dSgjelinek (void) fprintf(stderr, 6090b5de56dSgjelinek gettext("could not remove the " 6100b5de56dSgjelinek "software inventory from %s\n"), 6110b5de56dSgjelinek zonepath); 6120b5de56dSgjelinek } else { 6130b5de56dSgjelinek /* 6140b5de56dSgjelinek * The snapshot is unusable for some reason so restore 6150b5de56dSgjelinek * the zone state to configured since we were unable to 6160b5de56dSgjelinek * actually do anything about getting the zone 6170b5de56dSgjelinek * installed. 6180b5de56dSgjelinek */ 6190b5de56dSgjelinek int tmp; 6200b5de56dSgjelinek 6210b5de56dSgjelinek if ((tmp = zone_set_state(target_zone, 6220b5de56dSgjelinek ZONE_STATE_CONFIGURED)) != Z_OK) { 6230b5de56dSgjelinek errno = tmp; 6240b5de56dSgjelinek zperror2(target_zone, 6250b5de56dSgjelinek gettext("could not set state")); 6260b5de56dSgjelinek } 6270b5de56dSgjelinek } 6280b5de56dSgjelinek } 6290b5de56dSgjelinek 6300b5de56dSgjelinek return (err); 6310b5de56dSgjelinek } 6320b5de56dSgjelinek 6330b5de56dSgjelinek /* 6340b5de56dSgjelinek * Attempt to clone a source_zone to a target zonepath by using a ZFS clone. 6350b5de56dSgjelinek */ 6360b5de56dSgjelinek int 6370b5de56dSgjelinek clone_zfs(char *source_zone, char *source_zonepath, char *zonepath) 6380b5de56dSgjelinek { 6390b5de56dSgjelinek zfs_handle_t *zhp; 6400b5de56dSgjelinek char clone_name[MAXPATHLEN]; 6410b5de56dSgjelinek char snap_name[MAXPATHLEN]; 6420b5de56dSgjelinek 6430b5de56dSgjelinek /* 6440b5de56dSgjelinek * Try to get a zfs handle for the source_zonepath. If this fails 6450b5de56dSgjelinek * the source_zonepath is not ZFS so return an error. 6460b5de56dSgjelinek */ 6470b5de56dSgjelinek if ((zhp = mount2zhandle(source_zonepath)) == NULL) 6480b5de56dSgjelinek return (Z_ERR); 6490b5de56dSgjelinek 6500b5de56dSgjelinek /* 6510b5de56dSgjelinek * Check if there is a file system already mounted on zonepath. If so, 6520b5de56dSgjelinek * we can't clone to the path so we should fall back to copying. 6530b5de56dSgjelinek */ 6540b5de56dSgjelinek if (is_mountpnt(zonepath)) { 6550b5de56dSgjelinek zfs_close(zhp); 6560b5de56dSgjelinek (void) fprintf(stderr, 6570b5de56dSgjelinek gettext("A file system is already mounted on %s,\n" 6580b5de56dSgjelinek "preventing use of a ZFS clone.\n"), zonepath); 6590b5de56dSgjelinek return (Z_ERR); 6600b5de56dSgjelinek } 6610b5de56dSgjelinek 6620b5de56dSgjelinek /* 6630b5de56dSgjelinek * Instead of using path2name to get the clone name from the zonepath, 6640b5de56dSgjelinek * we could generate a name from the source zone ZFS name. However, 6650b5de56dSgjelinek * this would mean we would create the clone under the ZFS fs of the 6660b5de56dSgjelinek * source instead of what the zonepath says. For example, 6670b5de56dSgjelinek * 6680b5de56dSgjelinek * source_zonepath zonepath 6690b5de56dSgjelinek * /pl/zones/dev/z1 /pl/zones/deploy/z2 6700b5de56dSgjelinek * 6710b5de56dSgjelinek * We don't want the clone to be under "dev", we want it under 6720b5de56dSgjelinek * "deploy", so that we can leverage the normal attribute inheritance 6730b5de56dSgjelinek * that ZFS provides in the fs hierarchy. 6740b5de56dSgjelinek */ 6750b5de56dSgjelinek if (path2name(zonepath, clone_name, sizeof (clone_name)) != Z_OK) { 6760b5de56dSgjelinek zfs_close(zhp); 6770b5de56dSgjelinek return (Z_ERR); 6780b5de56dSgjelinek } 6790b5de56dSgjelinek 6800b5de56dSgjelinek if (take_snapshot(source_zone, zhp, snap_name, sizeof (snap_name)) 6810b5de56dSgjelinek != Z_OK) { 6820b5de56dSgjelinek zfs_close(zhp); 6830b5de56dSgjelinek return (Z_ERR); 6840b5de56dSgjelinek } 6850b5de56dSgjelinek zfs_close(zhp); 6860b5de56dSgjelinek 6870b5de56dSgjelinek if (clone_snap(snap_name, clone_name) != Z_OK) 6880b5de56dSgjelinek return (Z_ERR); 6890b5de56dSgjelinek 6900b5de56dSgjelinek (void) printf(gettext("Instead of copying, a ZFS clone has been " 6910b5de56dSgjelinek "created for this zone.\n")); 6920b5de56dSgjelinek 6930b5de56dSgjelinek return (Z_OK); 6940b5de56dSgjelinek } 6950b5de56dSgjelinek 6960b5de56dSgjelinek /* 6970b5de56dSgjelinek * Attempt to create a ZFS file system for the specified zonepath. 6980b5de56dSgjelinek * We either will successfully create a ZFS file system and get it mounted 6990b5de56dSgjelinek * on the zonepath or we don't. The caller doesn't care since a regular 7000b5de56dSgjelinek * directory is used for the zonepath if no ZFS file system is mounted there. 7010b5de56dSgjelinek */ 7020b5de56dSgjelinek void 7030b5de56dSgjelinek create_zfs_zonepath(char *zonepath) 7040b5de56dSgjelinek { 7050b5de56dSgjelinek zfs_handle_t *zhp; 7060b5de56dSgjelinek char zfs_name[MAXPATHLEN]; 707*e9dbad6fSeschrock nvlist_t *props = NULL; 7080b5de56dSgjelinek 7090b5de56dSgjelinek if (path2name(zonepath, zfs_name, sizeof (zfs_name)) != Z_OK) 7100b5de56dSgjelinek return; 7110b5de56dSgjelinek 712*e9dbad6fSeschrock if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0 || 713*e9dbad6fSeschrock nvlist_add_boolean_value(props, zfs_prop_to_name(ZFS_PROP_SHARENFS), 714*e9dbad6fSeschrock B_FALSE) != 0) { 715*e9dbad6fSeschrock nvlist_free(props); 716*e9dbad6fSeschrock (void) fprintf(stderr, gettext("cannot create ZFS dataset %s: " 717*e9dbad6fSeschrock "out of memory\n"), zfs_name); 718*e9dbad6fSeschrock } 719*e9dbad6fSeschrock 720*e9dbad6fSeschrock if (zfs_create(g_zfs, zfs_name, ZFS_TYPE_FILESYSTEM, props) != 0 || 72199653d4eSeschrock (zhp = zfs_open(g_zfs, zfs_name, ZFS_TYPE_ANY)) == NULL) { 72299653d4eSeschrock (void) fprintf(stderr, gettext("cannot create ZFS dataset %s: " 72399653d4eSeschrock "%s\n"), zfs_name, libzfs_error_description(g_zfs)); 724*e9dbad6fSeschrock nvlist_free(props); 7250b5de56dSgjelinek return; 7260b5de56dSgjelinek } 7270b5de56dSgjelinek 728*e9dbad6fSeschrock nvlist_free(props); 729*e9dbad6fSeschrock 7300b5de56dSgjelinek if (zfs_mount(zhp, NULL, 0) != 0) { 73199653d4eSeschrock (void) fprintf(stderr, gettext("cannot mount ZFS dataset %s: " 73299653d4eSeschrock "%s\n"), zfs_name, libzfs_error_description(g_zfs)); 7330b5de56dSgjelinek (void) zfs_destroy(zhp); 7340b5de56dSgjelinek } else { 7350b5de56dSgjelinek if (chmod(zonepath, S_IRWXU) != 0) { 7360b5de56dSgjelinek (void) fprintf(stderr, gettext("file system %s " 7370b5de56dSgjelinek "successfully created, but chmod %o failed: %s\n"), 7380b5de56dSgjelinek zfs_name, S_IRWXU, strerror(errno)); 7390b5de56dSgjelinek (void) destroy_zfs(zonepath); 7400b5de56dSgjelinek } else { 7410b5de56dSgjelinek (void) printf(gettext("A ZFS file system has been " 7420b5de56dSgjelinek "created for this zone.\n")); 7430b5de56dSgjelinek } 7440b5de56dSgjelinek } 7450b5de56dSgjelinek 7460b5de56dSgjelinek zfs_close(zhp); 7470b5de56dSgjelinek } 7480b5de56dSgjelinek 7490b5de56dSgjelinek /* 7500b5de56dSgjelinek * If the zonepath is a ZFS file system, attempt to destroy it. We return Z_OK 7510b5de56dSgjelinek * if we were able to zfs_destroy the zonepath, otherwise we return Z_ERR 7520b5de56dSgjelinek * which means the caller should clean up the zonepath in the traditional 7530b5de56dSgjelinek * way. 7540b5de56dSgjelinek */ 7550b5de56dSgjelinek int 7560b5de56dSgjelinek destroy_zfs(char *zonepath) 7570b5de56dSgjelinek { 7580b5de56dSgjelinek zfs_handle_t *zhp; 7590b5de56dSgjelinek boolean_t is_clone = B_FALSE; 7600b5de56dSgjelinek char origin[ZFS_MAXPROPLEN]; 7610b5de56dSgjelinek 76299653d4eSeschrock if ((zhp = mount2zhandle(zonepath)) == NULL) 7630b5de56dSgjelinek return (Z_ERR); 7640b5de56dSgjelinek 7650b5de56dSgjelinek /* 7660b5de56dSgjelinek * We can't destroy the file system if it has dependents. 7670b5de56dSgjelinek */ 7683bb79becSeschrock if (zfs_iter_dependents(zhp, B_TRUE, has_dependent, NULL) != 0 || 7690b5de56dSgjelinek zfs_unmount(zhp, NULL, 0) != 0) { 7700b5de56dSgjelinek zfs_close(zhp); 7710b5de56dSgjelinek return (Z_ERR); 7720b5de56dSgjelinek } 7730b5de56dSgjelinek 7740b5de56dSgjelinek /* 7750b5de56dSgjelinek * This might be a clone. Try to get the snapshot so we can attempt 7760b5de56dSgjelinek * to destroy that as well. 7770b5de56dSgjelinek */ 7780b5de56dSgjelinek if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin, sizeof (origin), NULL, 77999653d4eSeschrock NULL, 0, B_FALSE) == 0) 7800b5de56dSgjelinek is_clone = B_TRUE; 7810b5de56dSgjelinek 7820b5de56dSgjelinek if (zfs_destroy(zhp) != 0) { 7830b5de56dSgjelinek /* 7840b5de56dSgjelinek * If the destroy fails for some reason, try to remount 7850b5de56dSgjelinek * the file system so that we can use "rm -rf" to clean up 7860b5de56dSgjelinek * instead. 7870b5de56dSgjelinek */ 7880b5de56dSgjelinek (void) zfs_mount(zhp, NULL, 0); 7890b5de56dSgjelinek zfs_close(zhp); 7900b5de56dSgjelinek return (Z_ERR); 7910b5de56dSgjelinek } 7920b5de56dSgjelinek 7930b5de56dSgjelinek (void) printf(gettext("The ZFS file system for this zone has been " 7940b5de56dSgjelinek "destroyed.\n")); 7950b5de56dSgjelinek 7960b5de56dSgjelinek if (is_clone) { 7970b5de56dSgjelinek zfs_handle_t *ohp; 7980b5de56dSgjelinek 7990b5de56dSgjelinek /* 8000b5de56dSgjelinek * Try to clean up the snapshot that the clone was taken from. 8010b5de56dSgjelinek */ 80299653d4eSeschrock if ((ohp = zfs_open(g_zfs, origin, 80399653d4eSeschrock ZFS_TYPE_SNAPSHOT)) != NULL) { 8043bb79becSeschrock if (zfs_iter_dependents(ohp, B_TRUE, has_dependent, 8053bb79becSeschrock NULL) == 0 && zfs_unmount(ohp, NULL, 0) == 0) 8060b5de56dSgjelinek (void) zfs_destroy(ohp); 8070b5de56dSgjelinek zfs_close(ohp); 8080b5de56dSgjelinek } 8090b5de56dSgjelinek } 8100b5de56dSgjelinek 8110b5de56dSgjelinek zfs_close(zhp); 8120b5de56dSgjelinek return (Z_OK); 8130b5de56dSgjelinek } 8140b5de56dSgjelinek 8150b5de56dSgjelinek /* 8160b5de56dSgjelinek * Return true if the path is its own zfs file system. We determine this 8170b5de56dSgjelinek * by stat-ing the path to see if it is zfs and stat-ing the parent to see 8180b5de56dSgjelinek * if it is a different fs. 8190b5de56dSgjelinek */ 8200b5de56dSgjelinek boolean_t 8210b5de56dSgjelinek is_zonepath_zfs(char *zonepath) 8220b5de56dSgjelinek { 8230b5de56dSgjelinek int res; 8240b5de56dSgjelinek char *path; 8250b5de56dSgjelinek char *parent; 8263f2f09c1Sdp struct statvfs64 buf1, buf2; 8270b5de56dSgjelinek 8283f2f09c1Sdp if (statvfs64(zonepath, &buf1) != 0) 8290b5de56dSgjelinek return (B_FALSE); 8300b5de56dSgjelinek 8310b5de56dSgjelinek if (strcmp(buf1.f_basetype, "zfs") != 0) 8320b5de56dSgjelinek return (B_FALSE); 8330b5de56dSgjelinek 8340b5de56dSgjelinek if ((path = strdup(zonepath)) == NULL) 8350b5de56dSgjelinek return (B_FALSE); 8360b5de56dSgjelinek 8370b5de56dSgjelinek parent = dirname(path); 8383f2f09c1Sdp res = statvfs64(parent, &buf2); 8390b5de56dSgjelinek free(path); 8400b5de56dSgjelinek 8410b5de56dSgjelinek if (res != 0) 8420b5de56dSgjelinek return (B_FALSE); 8430b5de56dSgjelinek 8440b5de56dSgjelinek if (buf1.f_fsid == buf2.f_fsid) 8450b5de56dSgjelinek return (B_FALSE); 8460b5de56dSgjelinek 8470b5de56dSgjelinek return (B_TRUE); 8480b5de56dSgjelinek } 8490b5de56dSgjelinek 8500b5de56dSgjelinek /* 8510b5de56dSgjelinek * Implement the fast move of a ZFS file system by simply updating the 8520b5de56dSgjelinek * mountpoint. Since it is file system already, we don't have the 8530b5de56dSgjelinek * issue of cross-file system copying. 8540b5de56dSgjelinek */ 8550b5de56dSgjelinek int 8560b5de56dSgjelinek move_zfs(char *zonepath, char *new_zonepath) 8570b5de56dSgjelinek { 8580b5de56dSgjelinek int ret = Z_ERR; 8590b5de56dSgjelinek zfs_handle_t *zhp; 8600b5de56dSgjelinek 86199653d4eSeschrock if ((zhp = mount2zhandle(zonepath)) == NULL) 8620b5de56dSgjelinek return (Z_ERR); 8630b5de56dSgjelinek 864*e9dbad6fSeschrock if (zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 865*e9dbad6fSeschrock new_zonepath) == 0) { 8660b5de56dSgjelinek /* 8670b5de56dSgjelinek * Clean up the old mount point. We ignore any failure since 8680b5de56dSgjelinek * the zone is already successfully mounted on the new path. 8690b5de56dSgjelinek */ 8700b5de56dSgjelinek (void) rmdir(zonepath); 8710b5de56dSgjelinek ret = Z_OK; 8720b5de56dSgjelinek } 8730b5de56dSgjelinek 8740b5de56dSgjelinek zfs_close(zhp); 8750b5de56dSgjelinek 8760b5de56dSgjelinek return (ret); 8770b5de56dSgjelinek } 8780b5de56dSgjelinek 8790b5de56dSgjelinek /* 8800b5de56dSgjelinek * Validate that the given dataset exists on the system, and that neither it nor 8810b5de56dSgjelinek * its children are zvols. 8820b5de56dSgjelinek * 8830b5de56dSgjelinek * Note that we don't do anything with the 'zoned' property here. All 8840b5de56dSgjelinek * management is done in zoneadmd when the zone is actually rebooted. This 8850b5de56dSgjelinek * allows us to automatically set the zoned property even when a zone is 8860b5de56dSgjelinek * rebooted by the administrator. 8870b5de56dSgjelinek */ 8880b5de56dSgjelinek int 8890b5de56dSgjelinek verify_datasets(zone_dochandle_t handle) 8900b5de56dSgjelinek { 8910b5de56dSgjelinek int return_code = Z_OK; 8920b5de56dSgjelinek struct zone_dstab dstab; 8930b5de56dSgjelinek zfs_handle_t *zhp; 8940b5de56dSgjelinek char propbuf[ZFS_MAXPROPLEN]; 8950b5de56dSgjelinek char source[ZFS_MAXNAMELEN]; 8960b5de56dSgjelinek zfs_source_t srctype; 8970b5de56dSgjelinek 8980b5de56dSgjelinek if (zonecfg_setdsent(handle) != Z_OK) { 8990b5de56dSgjelinek /* 9000b5de56dSgjelinek * TRANSLATION_NOTE 9010b5de56dSgjelinek * zfs and dataset are literals that should not be translated. 9020b5de56dSgjelinek */ 9030b5de56dSgjelinek (void) fprintf(stderr, gettext("could not verify zfs datasets: " 9040b5de56dSgjelinek "unable to enumerate datasets\n")); 9050b5de56dSgjelinek return (Z_ERR); 9060b5de56dSgjelinek } 9070b5de56dSgjelinek 9080b5de56dSgjelinek while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 9090b5de56dSgjelinek 91099653d4eSeschrock if ((zhp = zfs_open(g_zfs, dstab.zone_dataset_name, 9110b5de56dSgjelinek ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) { 91299653d4eSeschrock (void) fprintf(stderr, gettext("could not verify zfs " 91399653d4eSeschrock "dataset %s: %s\n"), dstab.zone_dataset_name, 91499653d4eSeschrock libzfs_error_description(g_zfs)); 9150b5de56dSgjelinek return_code = Z_ERR; 9160b5de56dSgjelinek continue; 9170b5de56dSgjelinek } 9180b5de56dSgjelinek 9190b5de56dSgjelinek if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, 9200b5de56dSgjelinek sizeof (propbuf), &srctype, source, 9210b5de56dSgjelinek sizeof (source), 0) == 0 && 9220b5de56dSgjelinek (srctype == ZFS_SRC_INHERITED)) { 9230b5de56dSgjelinek (void) fprintf(stderr, gettext("could not verify zfs " 9240b5de56dSgjelinek "dataset %s: mountpoint cannot be inherited\n"), 9250b5de56dSgjelinek dstab.zone_dataset_name); 9260b5de56dSgjelinek return_code = Z_ERR; 9270b5de56dSgjelinek zfs_close(zhp); 9280b5de56dSgjelinek continue; 9290b5de56dSgjelinek } 9300b5de56dSgjelinek 9310b5de56dSgjelinek if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) { 9320b5de56dSgjelinek (void) fprintf(stderr, gettext("cannot verify zfs " 9330b5de56dSgjelinek "dataset %s: volumes cannot be specified as a " 9340b5de56dSgjelinek "zone dataset resource\n"), 9350b5de56dSgjelinek dstab.zone_dataset_name); 9360b5de56dSgjelinek return_code = Z_ERR; 9370b5de56dSgjelinek } 9380b5de56dSgjelinek 9390b5de56dSgjelinek if (zfs_iter_children(zhp, check_zvol, NULL) != 0) 9400b5de56dSgjelinek return_code = Z_ERR; 9410b5de56dSgjelinek 9420b5de56dSgjelinek zfs_close(zhp); 9430b5de56dSgjelinek } 9440b5de56dSgjelinek (void) zonecfg_enddsent(handle); 9450b5de56dSgjelinek 9460b5de56dSgjelinek return (return_code); 9470b5de56dSgjelinek } 9480b5de56dSgjelinek 9490b5de56dSgjelinek /* 9500b5de56dSgjelinek * Verify that the ZFS dataset exists, and its mountpoint 9510b5de56dSgjelinek * property is set to "legacy". 9520b5de56dSgjelinek */ 9530b5de56dSgjelinek int 9540b5de56dSgjelinek verify_fs_zfs(struct zone_fstab *fstab) 9550b5de56dSgjelinek { 9560b5de56dSgjelinek zfs_handle_t *zhp; 9570b5de56dSgjelinek char propbuf[ZFS_MAXPROPLEN]; 9580b5de56dSgjelinek 95999653d4eSeschrock if ((zhp = zfs_open(g_zfs, fstab->zone_fs_special, 96099653d4eSeschrock ZFS_TYPE_ANY)) == NULL) { 9610b5de56dSgjelinek (void) fprintf(stderr, gettext("could not verify fs %s: " 9620b5de56dSgjelinek "could not access zfs dataset '%s'\n"), 9630b5de56dSgjelinek fstab->zone_fs_dir, fstab->zone_fs_special); 9640b5de56dSgjelinek return (Z_ERR); 9650b5de56dSgjelinek } 9660b5de56dSgjelinek 9670b5de56dSgjelinek if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 9680b5de56dSgjelinek (void) fprintf(stderr, gettext("cannot verify fs %s: " 9690b5de56dSgjelinek "'%s' is not a file system\n"), 9700b5de56dSgjelinek fstab->zone_fs_dir, fstab->zone_fs_special); 9710b5de56dSgjelinek zfs_close(zhp); 9720b5de56dSgjelinek return (Z_ERR); 9730b5de56dSgjelinek } 9740b5de56dSgjelinek 9750b5de56dSgjelinek if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, propbuf, sizeof (propbuf), 9760b5de56dSgjelinek NULL, NULL, 0, 0) != 0 || strcmp(propbuf, "legacy") != 0) { 9770b5de56dSgjelinek (void) fprintf(stderr, gettext("could not verify fs %s: " 9780b5de56dSgjelinek "zfs '%s' mountpoint is not \"legacy\"\n"), 9790b5de56dSgjelinek fstab->zone_fs_dir, fstab->zone_fs_special); 9800b5de56dSgjelinek zfs_close(zhp); 9810b5de56dSgjelinek return (Z_ERR); 9820b5de56dSgjelinek } 9830b5de56dSgjelinek 9840b5de56dSgjelinek zfs_close(zhp); 98599653d4eSeschrock return (Z_OK); 98699653d4eSeschrock } 98799653d4eSeschrock 98899653d4eSeschrock int 98999653d4eSeschrock init_zfs(void) 99099653d4eSeschrock { 99199653d4eSeschrock if ((g_zfs = libzfs_init()) == NULL) { 99299653d4eSeschrock (void) fprintf(stderr, gettext("failed to initialize ZFS " 99399653d4eSeschrock "library\n")); 99499653d4eSeschrock return (Z_ERR); 99599653d4eSeschrock } 99699653d4eSeschrock 9970b5de56dSgjelinek return (Z_OK); 9980b5de56dSgjelinek } 999