17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21ffbafc53Scomay 227c478bd9Sstevel@tonic-gate /* 23ea8dc4b6Seschrock * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * This module contains functions used to bring up and tear down the 317c478bd9Sstevel@tonic-gate * Virtual Platform: [un]mounting file-systems, [un]plumbing network 327c478bd9Sstevel@tonic-gate * interfaces, [un]configuring devices, establishing resource controls, 337c478bd9Sstevel@tonic-gate * and creating/destroying the zone in the kernel. These actions, on 347c478bd9Sstevel@tonic-gate * the way up, ready the zone; on the way down, they halt the zone. 357c478bd9Sstevel@tonic-gate * See the much longer block comment at the beginning of zoneadmd.c 367c478bd9Sstevel@tonic-gate * for a bigger picture of how the whole program functions. 37108322fbScarlsonj * 38108322fbScarlsonj * This module also has primary responsibility for the layout of "scratch 39108322fbScarlsonj * zones." These are mounted, but inactive, zones that are used during 40108322fbScarlsonj * operating system upgrade and potentially other administrative action. The 41108322fbScarlsonj * scratch zone environment is similar to the miniroot environment. The zone's 42108322fbScarlsonj * actual root is mounted read-write on /a, and the standard paths (/usr, 43108322fbScarlsonj * /sbin, /lib) all lead to read-only copies of the running system's binaries. 44108322fbScarlsonj * This allows the administrative tools to manipulate the zone using "-R /a" 45108322fbScarlsonj * without relying on any binaries in the zone itself. 46108322fbScarlsonj * 47108322fbScarlsonj * If the scratch zone is on an alternate root (Live Upgrade [LU] boot 48108322fbScarlsonj * environment), then we must resolve the lofs mounts used there to uncover 49108322fbScarlsonj * writable (unshared) resources. Shared resources, though, are always 50108322fbScarlsonj * read-only. In addition, if the "same" zone with a different root path is 51108322fbScarlsonj * currently running, then "/b" inside the zone points to the running zone's 52108322fbScarlsonj * root. This allows LU to synchronize configuration files during the upgrade 53108322fbScarlsonj * process. 54108322fbScarlsonj * 55108322fbScarlsonj * To construct this environment, this module creates a tmpfs mount on 56108322fbScarlsonj * $ZONEPATH/lu. Inside this scratch area, the miniroot-like environment as 57108322fbScarlsonj * described above is constructed on the fly. The zone is then created using 58108322fbScarlsonj * $ZONEPATH/lu as the root. 59108322fbScarlsonj * 60108322fbScarlsonj * Note that scratch zones are inactive. The zone's bits are not running and 61108322fbScarlsonj * likely cannot be run correctly until upgrade is done. Init is not running 62108322fbScarlsonj * there, nor is SMF. Because of this, the "mounted" state of a scratch zone 63108322fbScarlsonj * is not a part of the usual halt/ready/boot state machine. 647c478bd9Sstevel@tonic-gate */ 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate #include <sys/param.h> 677c478bd9Sstevel@tonic-gate #include <sys/mount.h> 687c478bd9Sstevel@tonic-gate #include <sys/mntent.h> 697c478bd9Sstevel@tonic-gate #include <sys/socket.h> 707c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 717c478bd9Sstevel@tonic-gate #include <sys/types.h> 727c478bd9Sstevel@tonic-gate #include <sys/stat.h> 737c478bd9Sstevel@tonic-gate #include <sys/sockio.h> 747c478bd9Sstevel@tonic-gate #include <sys/stropts.h> 757c478bd9Sstevel@tonic-gate #include <sys/conf.h> 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate #include <inet/tcp.h> 787c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 797c478bd9Sstevel@tonic-gate #include <netinet/in.h> 807c478bd9Sstevel@tonic-gate #include <net/route.h> 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate #include <stdio.h> 837c478bd9Sstevel@tonic-gate #include <errno.h> 847c478bd9Sstevel@tonic-gate #include <fcntl.h> 857c478bd9Sstevel@tonic-gate #include <unistd.h> 867c478bd9Sstevel@tonic-gate #include <rctl.h> 877c478bd9Sstevel@tonic-gate #include <stdlib.h> 887c478bd9Sstevel@tonic-gate #include <string.h> 897c478bd9Sstevel@tonic-gate #include <strings.h> 907c478bd9Sstevel@tonic-gate #include <wait.h> 917c478bd9Sstevel@tonic-gate #include <limits.h> 927c478bd9Sstevel@tonic-gate #include <libgen.h> 93fa9e4066Sahrens #include <libzfs.h> 94facf4a8dSllai1 #include <libdevinfo.h> 957c478bd9Sstevel@tonic-gate #include <zone.h> 967c478bd9Sstevel@tonic-gate #include <assert.h> 97555afedfScarlsonj #include <libcontract.h> 98555afedfScarlsonj #include <libcontract_priv.h> 99555afedfScarlsonj #include <uuid/uuid.h> 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate #include <sys/mntio.h> 1027c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> 1037c478bd9Sstevel@tonic-gate #include <sys/fs/autofs.h> /* for _autofssys() */ 1047c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h> 105fa9e4066Sahrens #include <sys/fs/zfs.h> 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate #include <pool.h> 1087c478bd9Sstevel@tonic-gate #include <sys/pool.h> 109*0209230bSgjelinek #include <sys/priocntl.h> 1107c478bd9Sstevel@tonic-gate 1119acbbeafSnn35248 #include <libbrand.h> 1129acbbeafSnn35248 #include <sys/brand.h> 1137c478bd9Sstevel@tonic-gate #include <libzonecfg.h> 11439d3e169Sevanl #include <synch.h> 11522321485Svp157776 1167c478bd9Sstevel@tonic-gate #include "zoneadmd.h" 11745916cd2Sjpk #include <tsol/label.h> 11845916cd2Sjpk #include <libtsnet.h> 11945916cd2Sjpk #include <sys/priv.h> 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate #define V4_ADDR_LEN 32 1227c478bd9Sstevel@tonic-gate #define V6_ADDR_LEN 128 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */ 1257c478bd9Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 1267c478bd9Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate #define IPD_DEFAULT_OPTS \ 1297c478bd9Sstevel@tonic-gate MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate #define DFSTYPES "/etc/dfs/fstypes" 13245916cd2Sjpk #define MAXTNZLEN 2048 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate /* for routing socket */ 1357c478bd9Sstevel@tonic-gate static int rts_seqno = 0; 1367c478bd9Sstevel@tonic-gate 137108322fbScarlsonj /* mangled zone name when mounting in an alternate root environment */ 138108322fbScarlsonj static char kernzone[ZONENAME_MAX]; 139108322fbScarlsonj 140108322fbScarlsonj /* array of cached mount entries for resolve_lofs */ 141108322fbScarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max; 142108322fbScarlsonj 14345916cd2Sjpk /* for Trusted Extensions */ 14445916cd2Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *); 14545916cd2Sjpk static int tsol_mounts(zlog_t *, char *, char *); 14645916cd2Sjpk static void tsol_unmounts(zlog_t *, char *); 14745916cd2Sjpk static m_label_t *zlabel = NULL; 14845916cd2Sjpk static m_label_t *zid_label = NULL; 14945916cd2Sjpk static priv_set_t *zprivs = NULL; 15045916cd2Sjpk 1517c478bd9Sstevel@tonic-gate /* from libsocket, not in any header file */ 1527c478bd9Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *); 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate /* 155108322fbScarlsonj * An optimization for build_mnttable: reallocate (and potentially copy the 156108322fbScarlsonj * data) only once every N times through the loop. 157108322fbScarlsonj */ 158108322fbScarlsonj #define MNTTAB_HUNK 32 159108322fbScarlsonj 160108322fbScarlsonj /* 1617c478bd9Sstevel@tonic-gate * Private autofs system call 1627c478bd9Sstevel@tonic-gate */ 1637c478bd9Sstevel@tonic-gate extern int _autofssys(int, void *); 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate static int 1667c478bd9Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid) 1677c478bd9Sstevel@tonic-gate { 1687c478bd9Sstevel@tonic-gate /* 1697c478bd9Sstevel@tonic-gate * Ask autofs to unmount all trigger nodes in the given zone. 1707c478bd9Sstevel@tonic-gate */ 1717c478bd9Sstevel@tonic-gate return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid)); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate 174108322fbScarlsonj static void 175108322fbScarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem) 176108322fbScarlsonj { 177108322fbScarlsonj uint_t i; 178108322fbScarlsonj 179108322fbScarlsonj if (mnt_array == NULL) 180108322fbScarlsonj return; 181108322fbScarlsonj for (i = 0; i < nelem; i++) { 182108322fbScarlsonj free(mnt_array[i].mnt_mountp); 183108322fbScarlsonj free(mnt_array[i].mnt_fstype); 184108322fbScarlsonj free(mnt_array[i].mnt_special); 185108322fbScarlsonj free(mnt_array[i].mnt_mntopts); 186108322fbScarlsonj assert(mnt_array[i].mnt_time == NULL); 187108322fbScarlsonj } 188108322fbScarlsonj free(mnt_array); 189108322fbScarlsonj } 190108322fbScarlsonj 191108322fbScarlsonj /* 192108322fbScarlsonj * Build the mount table for the zone rooted at "zroot", storing the resulting 193108322fbScarlsonj * array of struct mnttabs in "mnt_arrayp" and the number of elements in the 194108322fbScarlsonj * array in "nelemp". 195108322fbScarlsonj */ 196108322fbScarlsonj static int 197108322fbScarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab, 198108322fbScarlsonj struct mnttab **mnt_arrayp, uint_t *nelemp) 199108322fbScarlsonj { 200108322fbScarlsonj struct mnttab mnt; 201108322fbScarlsonj struct mnttab *mnts; 202108322fbScarlsonj struct mnttab *mnp; 203108322fbScarlsonj uint_t nmnt; 204108322fbScarlsonj 205108322fbScarlsonj rewind(mnttab); 206108322fbScarlsonj resetmnttab(mnttab); 207108322fbScarlsonj nmnt = 0; 208108322fbScarlsonj mnts = NULL; 209108322fbScarlsonj while (getmntent(mnttab, &mnt) == 0) { 210108322fbScarlsonj struct mnttab *tmp_array; 211108322fbScarlsonj 212108322fbScarlsonj if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0) 213108322fbScarlsonj continue; 214108322fbScarlsonj if (nmnt % MNTTAB_HUNK == 0) { 215108322fbScarlsonj tmp_array = realloc(mnts, 216108322fbScarlsonj (nmnt + MNTTAB_HUNK) * sizeof (*mnts)); 217108322fbScarlsonj if (tmp_array == NULL) { 218108322fbScarlsonj free_mnttable(mnts, nmnt); 219108322fbScarlsonj return (-1); 220108322fbScarlsonj } 221108322fbScarlsonj mnts = tmp_array; 222108322fbScarlsonj } 223108322fbScarlsonj mnp = &mnts[nmnt++]; 224108322fbScarlsonj 225108322fbScarlsonj /* 226108322fbScarlsonj * Zero out any fields we're not using. 227108322fbScarlsonj */ 228108322fbScarlsonj (void) memset(mnp, 0, sizeof (*mnp)); 229108322fbScarlsonj 230108322fbScarlsonj if (mnt.mnt_special != NULL) 231108322fbScarlsonj mnp->mnt_special = strdup(mnt.mnt_special); 232108322fbScarlsonj if (mnt.mnt_mntopts != NULL) 233108322fbScarlsonj mnp->mnt_mntopts = strdup(mnt.mnt_mntopts); 234108322fbScarlsonj mnp->mnt_mountp = strdup(mnt.mnt_mountp); 235108322fbScarlsonj mnp->mnt_fstype = strdup(mnt.mnt_fstype); 236108322fbScarlsonj if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) || 237108322fbScarlsonj (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) || 238108322fbScarlsonj mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) { 239108322fbScarlsonj zerror(zlogp, B_TRUE, "memory allocation failed"); 240108322fbScarlsonj free_mnttable(mnts, nmnt); 241108322fbScarlsonj return (-1); 242108322fbScarlsonj } 243108322fbScarlsonj } 244108322fbScarlsonj *mnt_arrayp = mnts; 245108322fbScarlsonj *nelemp = nmnt; 246108322fbScarlsonj return (0); 247108322fbScarlsonj } 248108322fbScarlsonj 249108322fbScarlsonj /* 250108322fbScarlsonj * This is an optimization. The resolve_lofs function is used quite frequently 251108322fbScarlsonj * to manipulate file paths, and on a machine with a large number of zones, 252108322fbScarlsonj * there will be a huge number of mounted file systems. Thus, we trigger a 253108322fbScarlsonj * reread of the list of mount points 254108322fbScarlsonj */ 255108322fbScarlsonj static void 256108322fbScarlsonj lofs_discard_mnttab(void) 257108322fbScarlsonj { 258108322fbScarlsonj free_mnttable(resolve_lofs_mnts, 259108322fbScarlsonj resolve_lofs_mnt_max - resolve_lofs_mnts); 260108322fbScarlsonj resolve_lofs_mnts = resolve_lofs_mnt_max = NULL; 261108322fbScarlsonj } 262108322fbScarlsonj 263108322fbScarlsonj static int 264108322fbScarlsonj lofs_read_mnttab(zlog_t *zlogp) 265108322fbScarlsonj { 266108322fbScarlsonj FILE *mnttab; 267108322fbScarlsonj uint_t nmnts; 268108322fbScarlsonj 269108322fbScarlsonj if ((mnttab = fopen(MNTTAB, "r")) == NULL) 270108322fbScarlsonj return (-1); 271108322fbScarlsonj if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts, 272108322fbScarlsonj &nmnts) == -1) { 273108322fbScarlsonj (void) fclose(mnttab); 274108322fbScarlsonj return (-1); 275108322fbScarlsonj } 276108322fbScarlsonj (void) fclose(mnttab); 277108322fbScarlsonj resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts; 278108322fbScarlsonj return (0); 279108322fbScarlsonj } 280108322fbScarlsonj 281108322fbScarlsonj /* 282108322fbScarlsonj * This function loops over potential loopback mounts and symlinks in a given 283108322fbScarlsonj * path and resolves them all down to an absolute path. 284108322fbScarlsonj */ 285108322fbScarlsonj static void 286108322fbScarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen) 287108322fbScarlsonj { 288108322fbScarlsonj int len, arlen; 289108322fbScarlsonj const char *altroot; 290108322fbScarlsonj char tmppath[MAXPATHLEN]; 291108322fbScarlsonj boolean_t outside_altroot; 292108322fbScarlsonj 293108322fbScarlsonj if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1) 294108322fbScarlsonj return; 295108322fbScarlsonj tmppath[len] = '\0'; 296108322fbScarlsonj (void) strlcpy(path, tmppath, sizeof (tmppath)); 297108322fbScarlsonj 298108322fbScarlsonj /* This happens once per zoneadmd operation. */ 299108322fbScarlsonj if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 300108322fbScarlsonj return; 301108322fbScarlsonj 302108322fbScarlsonj altroot = zonecfg_get_root(); 303108322fbScarlsonj arlen = strlen(altroot); 304108322fbScarlsonj outside_altroot = B_FALSE; 305108322fbScarlsonj for (;;) { 306108322fbScarlsonj struct mnttab *mnp; 307108322fbScarlsonj 3081e9d8f9fSdminer /* Search in reverse order to find longest match */ 3091e9d8f9fSdminer for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts; 3101e9d8f9fSdminer mnp--) { 311108322fbScarlsonj if (mnp->mnt_fstype == NULL || 312108322fbScarlsonj mnp->mnt_mountp == NULL || 3131e9d8f9fSdminer mnp->mnt_special == NULL) 314108322fbScarlsonj continue; 315108322fbScarlsonj len = strlen(mnp->mnt_mountp); 316108322fbScarlsonj if (strncmp(mnp->mnt_mountp, path, len) == 0 && 317108322fbScarlsonj (path[len] == '/' || path[len] == '\0')) 318108322fbScarlsonj break; 319108322fbScarlsonj } 3201e9d8f9fSdminer if (mnp < resolve_lofs_mnts) 3211e9d8f9fSdminer break; 3221e9d8f9fSdminer /* If it's not a lofs then we're done */ 3231e9d8f9fSdminer if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0) 324108322fbScarlsonj break; 325108322fbScarlsonj if (outside_altroot) { 326108322fbScarlsonj char *cp; 327108322fbScarlsonj int olen = sizeof (MNTOPT_RO) - 1; 328108322fbScarlsonj 329108322fbScarlsonj /* 330108322fbScarlsonj * If we run into a read-only mount outside of the 331108322fbScarlsonj * alternate root environment, then the user doesn't 332108322fbScarlsonj * want this path to be made read-write. 333108322fbScarlsonj */ 334108322fbScarlsonj if (mnp->mnt_mntopts != NULL && 335108322fbScarlsonj (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) != 336108322fbScarlsonj NULL && 337108322fbScarlsonj (cp == mnp->mnt_mntopts || cp[-1] == ',') && 338108322fbScarlsonj (cp[olen] == '\0' || cp[olen] == ',')) { 339108322fbScarlsonj break; 340108322fbScarlsonj } 341108322fbScarlsonj } else if (arlen > 0 && 342108322fbScarlsonj (strncmp(mnp->mnt_special, altroot, arlen) != 0 || 343108322fbScarlsonj (mnp->mnt_special[arlen] != '\0' && 344108322fbScarlsonj mnp->mnt_special[arlen] != '/'))) { 345108322fbScarlsonj outside_altroot = B_TRUE; 346108322fbScarlsonj } 347108322fbScarlsonj /* use temporary buffer because new path might be longer */ 348108322fbScarlsonj (void) snprintf(tmppath, sizeof (tmppath), "%s%s", 349108322fbScarlsonj mnp->mnt_special, path + len); 350108322fbScarlsonj if ((len = resolvepath(tmppath, path, pathlen)) == -1) 351108322fbScarlsonj break; 352108322fbScarlsonj path[len] = '\0'; 353108322fbScarlsonj } 354108322fbScarlsonj } 355108322fbScarlsonj 356108322fbScarlsonj /* 357108322fbScarlsonj * For a regular mount, check if a replacement lofs mount is needed because the 358108322fbScarlsonj * referenced device is already mounted somewhere. 359108322fbScarlsonj */ 360108322fbScarlsonj static int 361108322fbScarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr) 362108322fbScarlsonj { 363108322fbScarlsonj struct mnttab *mnp; 364108322fbScarlsonj zone_fsopt_t *optptr, *onext; 365108322fbScarlsonj 366108322fbScarlsonj /* This happens once per zoneadmd operation. */ 367108322fbScarlsonj if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 368108322fbScarlsonj return (-1); 369108322fbScarlsonj 370108322fbScarlsonj /* 371108322fbScarlsonj * If this special node isn't already in use, then it's ours alone; 372108322fbScarlsonj * no need to worry about conflicting mounts. 373108322fbScarlsonj */ 374108322fbScarlsonj for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; 375108322fbScarlsonj mnp++) { 376108322fbScarlsonj if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0) 377108322fbScarlsonj break; 378108322fbScarlsonj } 379108322fbScarlsonj if (mnp >= resolve_lofs_mnt_max) 380108322fbScarlsonj return (0); 381108322fbScarlsonj 382108322fbScarlsonj /* 383108322fbScarlsonj * Convert this duplicate mount into a lofs mount. 384108322fbScarlsonj */ 385108322fbScarlsonj (void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp, 386108322fbScarlsonj sizeof (fsptr->zone_fs_special)); 387108322fbScarlsonj (void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS, 388108322fbScarlsonj sizeof (fsptr->zone_fs_type)); 389108322fbScarlsonj fsptr->zone_fs_raw[0] = '\0'; 390108322fbScarlsonj 391108322fbScarlsonj /* 392108322fbScarlsonj * Discard all but one of the original options and set that to be the 393108322fbScarlsonj * same set of options used for inherit package directory resources. 394108322fbScarlsonj */ 395108322fbScarlsonj optptr = fsptr->zone_fs_options; 396108322fbScarlsonj if (optptr == NULL) { 397108322fbScarlsonj optptr = malloc(sizeof (*optptr)); 398108322fbScarlsonj if (optptr == NULL) { 399108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot mount %s", 400108322fbScarlsonj fsptr->zone_fs_dir); 401108322fbScarlsonj return (-1); 402108322fbScarlsonj } 403108322fbScarlsonj } else { 404108322fbScarlsonj while ((onext = optptr->zone_fsopt_next) != NULL) { 405108322fbScarlsonj optptr->zone_fsopt_next = onext->zone_fsopt_next; 406108322fbScarlsonj free(onext); 407108322fbScarlsonj } 408108322fbScarlsonj } 409108322fbScarlsonj (void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS); 410108322fbScarlsonj optptr->zone_fsopt_next = NULL; 411108322fbScarlsonj fsptr->zone_fs_options = optptr; 412108322fbScarlsonj return (0); 413108322fbScarlsonj } 414108322fbScarlsonj 4157c478bd9Sstevel@tonic-gate static int 4167c478bd9Sstevel@tonic-gate make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode) 4177c478bd9Sstevel@tonic-gate { 4187c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 4197c478bd9Sstevel@tonic-gate struct stat st; 4207c478bd9Sstevel@tonic-gate 4217c478bd9Sstevel@tonic-gate if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) > 4227c478bd9Sstevel@tonic-gate sizeof (path)) { 4237c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix, 4247c478bd9Sstevel@tonic-gate subdir); 4257c478bd9Sstevel@tonic-gate return (-1); 4267c478bd9Sstevel@tonic-gate } 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate if (lstat(path, &st) == 0) { 4297c478bd9Sstevel@tonic-gate /* 4307c478bd9Sstevel@tonic-gate * We don't check the file mode since presumably the zone 4317c478bd9Sstevel@tonic-gate * administrator may have had good reason to change the mode, 4327c478bd9Sstevel@tonic-gate * and we don't need to second guess him. 4337c478bd9Sstevel@tonic-gate */ 4347c478bd9Sstevel@tonic-gate if (!S_ISDIR(st.st_mode)) { 43545916cd2Sjpk if (is_system_labeled() && 43645916cd2Sjpk S_ISREG(st.st_mode)) { 43745916cd2Sjpk /* 43845916cd2Sjpk * The need to mount readonly copies of 43945916cd2Sjpk * global zone /etc/ files is unique to 44045916cd2Sjpk * Trusted Extensions. 44145916cd2Sjpk */ 44245916cd2Sjpk if (strncmp(subdir, "/etc/", 44345916cd2Sjpk strlen("/etc/")) != 0) { 44445916cd2Sjpk zerror(zlogp, B_FALSE, 44545916cd2Sjpk "%s is not in /etc", path); 4467c478bd9Sstevel@tonic-gate return (-1); 4477c478bd9Sstevel@tonic-gate } 44845916cd2Sjpk } else { 44945916cd2Sjpk zerror(zlogp, B_FALSE, 45045916cd2Sjpk "%s is not a directory", path); 45145916cd2Sjpk return (-1); 45245916cd2Sjpk } 45345916cd2Sjpk } 4547c478bd9Sstevel@tonic-gate } else if (mkdirp(path, mode) != 0) { 4557c478bd9Sstevel@tonic-gate if (errno == EROFS) 4567c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on " 4577c478bd9Sstevel@tonic-gate "a read-only file system in this local zone.\nMake " 4587c478bd9Sstevel@tonic-gate "sure %s exists in the global zone.", path, subdir); 4597c478bd9Sstevel@tonic-gate else 4607c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "mkdirp of %s failed", path); 4617c478bd9Sstevel@tonic-gate return (-1); 4627c478bd9Sstevel@tonic-gate } 4637c478bd9Sstevel@tonic-gate return (0); 4647c478bd9Sstevel@tonic-gate } 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate static void 4677c478bd9Sstevel@tonic-gate free_remote_fstypes(char **types) 4687c478bd9Sstevel@tonic-gate { 4697c478bd9Sstevel@tonic-gate uint_t i; 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate if (types == NULL) 4727c478bd9Sstevel@tonic-gate return; 4737c478bd9Sstevel@tonic-gate for (i = 0; types[i] != NULL; i++) 4747c478bd9Sstevel@tonic-gate free(types[i]); 4757c478bd9Sstevel@tonic-gate free(types); 4767c478bd9Sstevel@tonic-gate } 4777c478bd9Sstevel@tonic-gate 4787c478bd9Sstevel@tonic-gate static char ** 4797c478bd9Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp) 4807c478bd9Sstevel@tonic-gate { 4817c478bd9Sstevel@tonic-gate char **types = NULL; 4827c478bd9Sstevel@tonic-gate FILE *fp; 4837c478bd9Sstevel@tonic-gate char buf[MAXPATHLEN]; 4847c478bd9Sstevel@tonic-gate char fstype[MAXPATHLEN]; 4857c478bd9Sstevel@tonic-gate uint_t lines = 0; 4867c478bd9Sstevel@tonic-gate uint_t i; 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate if ((fp = fopen(DFSTYPES, "r")) == NULL) { 4897c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES); 4907c478bd9Sstevel@tonic-gate return (NULL); 4917c478bd9Sstevel@tonic-gate } 4927c478bd9Sstevel@tonic-gate /* 4937c478bd9Sstevel@tonic-gate * Count the number of lines 4947c478bd9Sstevel@tonic-gate */ 4957c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fp) != NULL) 4967c478bd9Sstevel@tonic-gate lines++; 4977c478bd9Sstevel@tonic-gate if (lines == 0) /* didn't read anything; empty file */ 4987c478bd9Sstevel@tonic-gate goto out; 4997c478bd9Sstevel@tonic-gate rewind(fp); 5007c478bd9Sstevel@tonic-gate /* 5017c478bd9Sstevel@tonic-gate * Allocate enough space for a NULL-terminated array. 5027c478bd9Sstevel@tonic-gate */ 5037c478bd9Sstevel@tonic-gate types = calloc(lines + 1, sizeof (char *)); 5047c478bd9Sstevel@tonic-gate if (types == NULL) { 5057c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 5067c478bd9Sstevel@tonic-gate goto out; 5077c478bd9Sstevel@tonic-gate } 5087c478bd9Sstevel@tonic-gate i = 0; 5097c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fp) != NULL) { 5107c478bd9Sstevel@tonic-gate /* LINTED - fstype is big enough to hold buf */ 5117c478bd9Sstevel@tonic-gate if (sscanf(buf, "%s", fstype) == 0) { 5127c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES); 5137c478bd9Sstevel@tonic-gate free_remote_fstypes(types); 5147c478bd9Sstevel@tonic-gate types = NULL; 5157c478bd9Sstevel@tonic-gate goto out; 5167c478bd9Sstevel@tonic-gate } 5177c478bd9Sstevel@tonic-gate types[i] = strdup(fstype); 5187c478bd9Sstevel@tonic-gate if (types[i] == NULL) { 5197c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 5207c478bd9Sstevel@tonic-gate free_remote_fstypes(types); 5217c478bd9Sstevel@tonic-gate types = NULL; 5227c478bd9Sstevel@tonic-gate goto out; 5237c478bd9Sstevel@tonic-gate } 5247c478bd9Sstevel@tonic-gate i++; 5257c478bd9Sstevel@tonic-gate } 5267c478bd9Sstevel@tonic-gate out: 5277c478bd9Sstevel@tonic-gate (void) fclose(fp); 5287c478bd9Sstevel@tonic-gate return (types); 5297c478bd9Sstevel@tonic-gate } 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate static boolean_t 5327c478bd9Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes) 5337c478bd9Sstevel@tonic-gate { 5347c478bd9Sstevel@tonic-gate uint_t i; 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate if (remote_fstypes == NULL) 5377c478bd9Sstevel@tonic-gate return (B_FALSE); 5387c478bd9Sstevel@tonic-gate for (i = 0; remote_fstypes[i] != NULL; i++) { 5397c478bd9Sstevel@tonic-gate if (strcmp(remote_fstypes[i], fstype) == 0) 5407c478bd9Sstevel@tonic-gate return (B_TRUE); 5417c478bd9Sstevel@tonic-gate } 5427c478bd9Sstevel@tonic-gate return (B_FALSE); 5437c478bd9Sstevel@tonic-gate } 5447c478bd9Sstevel@tonic-gate 545108322fbScarlsonj /* 546108322fbScarlsonj * This converts a zone root path (normally of the form .../root) to a Live 547108322fbScarlsonj * Upgrade scratch zone root (of the form .../lu). 548108322fbScarlsonj */ 5497c478bd9Sstevel@tonic-gate static void 550108322fbScarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved) 5517c478bd9Sstevel@tonic-gate { 5529acbbeafSnn35248 assert(zone_isnative); 5539acbbeafSnn35248 554108322fbScarlsonj if (!isresolved && zonecfg_in_alt_root()) 555108322fbScarlsonj resolve_lofs(zlogp, zroot, zrootlen); 556108322fbScarlsonj (void) strcpy(strrchr(zroot, '/') + 1, "lu"); 5577c478bd9Sstevel@tonic-gate } 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate /* 5607c478bd9Sstevel@tonic-gate * The general strategy for unmounting filesystems is as follows: 5617c478bd9Sstevel@tonic-gate * 5627c478bd9Sstevel@tonic-gate * - Remote filesystems may be dead, and attempting to contact them as 5637c478bd9Sstevel@tonic-gate * part of a regular unmount may hang forever; we want to always try to 5647c478bd9Sstevel@tonic-gate * forcibly unmount such filesystems and only fall back to regular 5657c478bd9Sstevel@tonic-gate * unmounts if the filesystem doesn't support forced unmounts. 5667c478bd9Sstevel@tonic-gate * 5677c478bd9Sstevel@tonic-gate * - We don't want to unnecessarily corrupt metadata on local 5687c478bd9Sstevel@tonic-gate * filesystems (ie UFS), so we want to start off with graceful unmounts, 5697c478bd9Sstevel@tonic-gate * and only escalate to doing forced unmounts if we get stuck. 5707c478bd9Sstevel@tonic-gate * 5717c478bd9Sstevel@tonic-gate * We start off walking backwards through the mount table. This doesn't 5727c478bd9Sstevel@tonic-gate * give us strict ordering but ensures that we try to unmount submounts 5737c478bd9Sstevel@tonic-gate * first. We thus limit the number of failed umount2(2) calls. 5747c478bd9Sstevel@tonic-gate * 5757c478bd9Sstevel@tonic-gate * The mechanism for determining if we're stuck is to count the number 5767c478bd9Sstevel@tonic-gate * of failed unmounts each iteration through the mount table. This 5777c478bd9Sstevel@tonic-gate * gives us an upper bound on the number of filesystems which remain 5787c478bd9Sstevel@tonic-gate * mounted (autofs trigger nodes are dealt with separately). If at the 5797c478bd9Sstevel@tonic-gate * end of one unmount+autofs_cleanup cycle we still have the same number 5807c478bd9Sstevel@tonic-gate * of mounts that we started out with, we're stuck and try a forced 5817c478bd9Sstevel@tonic-gate * unmount. If that fails (filesystem doesn't support forced unmounts) 5827c478bd9Sstevel@tonic-gate * then we bail and are unable to teardown the zone. If it succeeds, 5837c478bd9Sstevel@tonic-gate * we're no longer stuck so we continue with our policy of trying 5847c478bd9Sstevel@tonic-gate * graceful mounts first. 5857c478bd9Sstevel@tonic-gate * 5867c478bd9Sstevel@tonic-gate * Zone must be down (ie, no processes or threads active). 5877c478bd9Sstevel@tonic-gate */ 5887c478bd9Sstevel@tonic-gate static int 589108322fbScarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd) 5907c478bd9Sstevel@tonic-gate { 5917c478bd9Sstevel@tonic-gate int error = 0; 5927c478bd9Sstevel@tonic-gate FILE *mnttab; 5937c478bd9Sstevel@tonic-gate struct mnttab *mnts; 5947c478bd9Sstevel@tonic-gate uint_t nmnt; 5957c478bd9Sstevel@tonic-gate char zroot[MAXPATHLEN + 1]; 5967c478bd9Sstevel@tonic-gate size_t zrootlen; 5977c478bd9Sstevel@tonic-gate uint_t oldcount = UINT_MAX; 5987c478bd9Sstevel@tonic-gate boolean_t stuck = B_FALSE; 5997c478bd9Sstevel@tonic-gate char **remote_fstypes = NULL; 6007c478bd9Sstevel@tonic-gate 6017c478bd9Sstevel@tonic-gate if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 6027c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to determine zone root"); 6037c478bd9Sstevel@tonic-gate return (-1); 6047c478bd9Sstevel@tonic-gate } 605108322fbScarlsonj if (unmount_cmd) 606108322fbScarlsonj root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE); 6077c478bd9Sstevel@tonic-gate 6087c478bd9Sstevel@tonic-gate (void) strcat(zroot, "/"); 6097c478bd9Sstevel@tonic-gate zrootlen = strlen(zroot); 6107c478bd9Sstevel@tonic-gate 61145916cd2Sjpk /* 61245916cd2Sjpk * For Trusted Extensions unmount each higher level zone's mount 61345916cd2Sjpk * of our zone's /export/home 61445916cd2Sjpk */ 61548451833Scarlsonj if (!unmount_cmd) 61645916cd2Sjpk tsol_unmounts(zlogp, zone_name); 61745916cd2Sjpk 6187c478bd9Sstevel@tonic-gate if ((mnttab = fopen(MNTTAB, "r")) == NULL) { 6197c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB); 6207c478bd9Sstevel@tonic-gate return (-1); 6217c478bd9Sstevel@tonic-gate } 6227c478bd9Sstevel@tonic-gate /* 6237c478bd9Sstevel@tonic-gate * Use our hacky mntfs ioctl so we see everything, even mounts with 6247c478bd9Sstevel@tonic-gate * MS_NOMNTTAB. 6257c478bd9Sstevel@tonic-gate */ 6267c478bd9Sstevel@tonic-gate if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) { 6277c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB); 6287c478bd9Sstevel@tonic-gate error++; 6297c478bd9Sstevel@tonic-gate goto out; 6307c478bd9Sstevel@tonic-gate } 6317c478bd9Sstevel@tonic-gate 6327c478bd9Sstevel@tonic-gate /* 6337c478bd9Sstevel@tonic-gate * Build the list of remote fstypes so we know which ones we 6347c478bd9Sstevel@tonic-gate * should forcibly unmount. 6357c478bd9Sstevel@tonic-gate */ 6367c478bd9Sstevel@tonic-gate remote_fstypes = get_remote_fstypes(zlogp); 6377c478bd9Sstevel@tonic-gate for (; /* ever */; ) { 6387c478bd9Sstevel@tonic-gate uint_t newcount = 0; 6397c478bd9Sstevel@tonic-gate boolean_t unmounted; 6407c478bd9Sstevel@tonic-gate struct mnttab *mnp; 6417c478bd9Sstevel@tonic-gate char *path; 6427c478bd9Sstevel@tonic-gate uint_t i; 6437c478bd9Sstevel@tonic-gate 6447c478bd9Sstevel@tonic-gate mnts = NULL; 6457c478bd9Sstevel@tonic-gate nmnt = 0; 6467c478bd9Sstevel@tonic-gate /* 6477c478bd9Sstevel@tonic-gate * MNTTAB gives us a way to walk through mounted 6487c478bd9Sstevel@tonic-gate * filesystems; we need to be able to walk them in 6497c478bd9Sstevel@tonic-gate * reverse order, so we build a list of all mounted 6507c478bd9Sstevel@tonic-gate * filesystems. 6517c478bd9Sstevel@tonic-gate */ 6527c478bd9Sstevel@tonic-gate if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts, 6537c478bd9Sstevel@tonic-gate &nmnt) != 0) { 6547c478bd9Sstevel@tonic-gate error++; 6557c478bd9Sstevel@tonic-gate goto out; 6567c478bd9Sstevel@tonic-gate } 6577c478bd9Sstevel@tonic-gate for (i = 0; i < nmnt; i++) { 6587c478bd9Sstevel@tonic-gate mnp = &mnts[nmnt - i - 1]; /* access in reverse order */ 6597c478bd9Sstevel@tonic-gate path = mnp->mnt_mountp; 6607c478bd9Sstevel@tonic-gate unmounted = B_FALSE; 6617c478bd9Sstevel@tonic-gate /* 6627c478bd9Sstevel@tonic-gate * Try forced unmount first for remote filesystems. 6637c478bd9Sstevel@tonic-gate * 6647c478bd9Sstevel@tonic-gate * Not all remote filesystems support forced unmounts, 6657c478bd9Sstevel@tonic-gate * so if this fails (ENOTSUP) we'll continue on 6667c478bd9Sstevel@tonic-gate * and try a regular unmount. 6677c478bd9Sstevel@tonic-gate */ 6687c478bd9Sstevel@tonic-gate if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) { 6697c478bd9Sstevel@tonic-gate if (umount2(path, MS_FORCE) == 0) 6707c478bd9Sstevel@tonic-gate unmounted = B_TRUE; 6717c478bd9Sstevel@tonic-gate } 6727c478bd9Sstevel@tonic-gate /* 6737c478bd9Sstevel@tonic-gate * Try forced unmount if we're stuck. 6747c478bd9Sstevel@tonic-gate */ 6757c478bd9Sstevel@tonic-gate if (stuck) { 6767c478bd9Sstevel@tonic-gate if (umount2(path, MS_FORCE) == 0) { 6777c478bd9Sstevel@tonic-gate unmounted = B_TRUE; 6787c478bd9Sstevel@tonic-gate stuck = B_FALSE; 6797c478bd9Sstevel@tonic-gate } else { 6807c478bd9Sstevel@tonic-gate /* 6817c478bd9Sstevel@tonic-gate * The first failure indicates a 6827c478bd9Sstevel@tonic-gate * mount we won't be able to get 6837c478bd9Sstevel@tonic-gate * rid of automatically, so we 6847c478bd9Sstevel@tonic-gate * bail. 6857c478bd9Sstevel@tonic-gate */ 6867c478bd9Sstevel@tonic-gate error++; 6877c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 6887c478bd9Sstevel@tonic-gate "unable to unmount '%s'", path); 6897c478bd9Sstevel@tonic-gate free_mnttable(mnts, nmnt); 6907c478bd9Sstevel@tonic-gate goto out; 6917c478bd9Sstevel@tonic-gate } 6927c478bd9Sstevel@tonic-gate } 6937c478bd9Sstevel@tonic-gate /* 6947c478bd9Sstevel@tonic-gate * Try regular unmounts for everything else. 6957c478bd9Sstevel@tonic-gate */ 6967c478bd9Sstevel@tonic-gate if (!unmounted && umount2(path, 0) != 0) 6977c478bd9Sstevel@tonic-gate newcount++; 6987c478bd9Sstevel@tonic-gate } 6997c478bd9Sstevel@tonic-gate free_mnttable(mnts, nmnt); 7007c478bd9Sstevel@tonic-gate 7017c478bd9Sstevel@tonic-gate if (newcount == 0) 7027c478bd9Sstevel@tonic-gate break; 7037c478bd9Sstevel@tonic-gate if (newcount >= oldcount) { 7047c478bd9Sstevel@tonic-gate /* 7057c478bd9Sstevel@tonic-gate * Last round didn't unmount anything; we're stuck and 7067c478bd9Sstevel@tonic-gate * should start trying forced unmounts. 7077c478bd9Sstevel@tonic-gate */ 7087c478bd9Sstevel@tonic-gate stuck = B_TRUE; 7097c478bd9Sstevel@tonic-gate } 7107c478bd9Sstevel@tonic-gate oldcount = newcount; 7117c478bd9Sstevel@tonic-gate 7127c478bd9Sstevel@tonic-gate /* 7137c478bd9Sstevel@tonic-gate * Autofs doesn't let you unmount its trigger nodes from 7147c478bd9Sstevel@tonic-gate * userland so we have to tell the kernel to cleanup for us. 7157c478bd9Sstevel@tonic-gate */ 7167c478bd9Sstevel@tonic-gate if (autofs_cleanup(zoneid) != 0) { 7177c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to remove autofs nodes"); 7187c478bd9Sstevel@tonic-gate error++; 7197c478bd9Sstevel@tonic-gate goto out; 7207c478bd9Sstevel@tonic-gate } 7217c478bd9Sstevel@tonic-gate } 7227c478bd9Sstevel@tonic-gate 7237c478bd9Sstevel@tonic-gate out: 7247c478bd9Sstevel@tonic-gate free_remote_fstypes(remote_fstypes); 7257c478bd9Sstevel@tonic-gate (void) fclose(mnttab); 7267c478bd9Sstevel@tonic-gate return (error ? -1 : 0); 7277c478bd9Sstevel@tonic-gate } 7287c478bd9Sstevel@tonic-gate 7297c478bd9Sstevel@tonic-gate static int 7307c478bd9Sstevel@tonic-gate fs_compare(const void *m1, const void *m2) 7317c478bd9Sstevel@tonic-gate { 7327c478bd9Sstevel@tonic-gate struct zone_fstab *i = (struct zone_fstab *)m1; 7337c478bd9Sstevel@tonic-gate struct zone_fstab *j = (struct zone_fstab *)m2; 7347c478bd9Sstevel@tonic-gate 7357c478bd9Sstevel@tonic-gate return (strcmp(i->zone_fs_dir, j->zone_fs_dir)); 7367c478bd9Sstevel@tonic-gate } 7377c478bd9Sstevel@tonic-gate 7387c478bd9Sstevel@tonic-gate /* 7397c478bd9Sstevel@tonic-gate * Fork and exec (and wait for) the mentioned binary with the provided 7407c478bd9Sstevel@tonic-gate * arguments. Returns (-1) if something went wrong with fork(2) or exec(2), 7417c478bd9Sstevel@tonic-gate * returns the exit status otherwise. 7427c478bd9Sstevel@tonic-gate * 7437c478bd9Sstevel@tonic-gate * If we were unable to exec the provided pathname (for whatever 7447c478bd9Sstevel@tonic-gate * reason), we return the special token ZEXIT_EXEC. The current value 7457c478bd9Sstevel@tonic-gate * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the 7467c478bd9Sstevel@tonic-gate * consumers of this function; any future consumers must make sure this 7477c478bd9Sstevel@tonic-gate * remains the case. 7487c478bd9Sstevel@tonic-gate */ 7497c478bd9Sstevel@tonic-gate static int 7507c478bd9Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[]) 7517c478bd9Sstevel@tonic-gate { 7527c478bd9Sstevel@tonic-gate pid_t child_pid; 7537c478bd9Sstevel@tonic-gate int child_status = 0; 7547c478bd9Sstevel@tonic-gate 7557c478bd9Sstevel@tonic-gate /* 7567c478bd9Sstevel@tonic-gate * Do not let another thread localize a message while we are forking. 7577c478bd9Sstevel@tonic-gate */ 7587c478bd9Sstevel@tonic-gate (void) mutex_lock(&msglock); 7597c478bd9Sstevel@tonic-gate child_pid = fork(); 7607c478bd9Sstevel@tonic-gate (void) mutex_unlock(&msglock); 7617c478bd9Sstevel@tonic-gate if (child_pid == -1) { 7627c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]); 7637c478bd9Sstevel@tonic-gate return (-1); 7647c478bd9Sstevel@tonic-gate } else if (child_pid == 0) { 7657c478bd9Sstevel@tonic-gate closefrom(0); 7661390a385Sgjelinek /* redirect stdin, stdout & stderr to /dev/null */ 7671390a385Sgjelinek (void) open("/dev/null", O_RDONLY); /* stdin */ 7681390a385Sgjelinek (void) open("/dev/null", O_WRONLY); /* stdout */ 7691390a385Sgjelinek (void) open("/dev/null", O_WRONLY); /* stderr */ 7707c478bd9Sstevel@tonic-gate (void) execv(path, argv); 7717c478bd9Sstevel@tonic-gate /* 7727c478bd9Sstevel@tonic-gate * Since we are in the child, there is no point calling zerror() 7737c478bd9Sstevel@tonic-gate * since there is nobody waiting to consume it. So exit with a 7747c478bd9Sstevel@tonic-gate * special code that the parent will recognize and call zerror() 7757c478bd9Sstevel@tonic-gate * accordingly. 7767c478bd9Sstevel@tonic-gate */ 7777c478bd9Sstevel@tonic-gate 7787c478bd9Sstevel@tonic-gate _exit(ZEXIT_EXEC); 7797c478bd9Sstevel@tonic-gate } else { 7807c478bd9Sstevel@tonic-gate (void) waitpid(child_pid, &child_status, 0); 7817c478bd9Sstevel@tonic-gate } 7827c478bd9Sstevel@tonic-gate 7837c478bd9Sstevel@tonic-gate if (WIFSIGNALED(child_status)) { 7847c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to " 7857c478bd9Sstevel@tonic-gate "signal %d", path, WTERMSIG(child_status)); 7867c478bd9Sstevel@tonic-gate return (-1); 7877c478bd9Sstevel@tonic-gate } 7887c478bd9Sstevel@tonic-gate assert(WIFEXITED(child_status)); 7897c478bd9Sstevel@tonic-gate if (WEXITSTATUS(child_status) == ZEXIT_EXEC) { 7907c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "failed to exec %s", path); 7917c478bd9Sstevel@tonic-gate return (-1); 7927c478bd9Sstevel@tonic-gate } 7937c478bd9Sstevel@tonic-gate return (WEXITSTATUS(child_status)); 7947c478bd9Sstevel@tonic-gate } 7957c478bd9Sstevel@tonic-gate 7967c478bd9Sstevel@tonic-gate static int 7977c478bd9Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev) 7987c478bd9Sstevel@tonic-gate { 7997c478bd9Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 8007c478bd9Sstevel@tonic-gate char *argv[4]; 8017c478bd9Sstevel@tonic-gate int status; 8027c478bd9Sstevel@tonic-gate 8037c478bd9Sstevel@tonic-gate /* 8047c478bd9Sstevel@tonic-gate * We could alternatively have called /usr/sbin/fsck -F <fstype>, but 8057c478bd9Sstevel@tonic-gate * that would cost us an extra fork/exec without buying us anything. 8067c478bd9Sstevel@tonic-gate */ 8077c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype) 8089acbbeafSnn35248 >= sizeof (cmdbuf)) { 8097c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "file-system type %s too long", fstype); 8107c478bd9Sstevel@tonic-gate return (-1); 8117c478bd9Sstevel@tonic-gate } 8127c478bd9Sstevel@tonic-gate 8137c478bd9Sstevel@tonic-gate argv[0] = "fsck"; 8147c478bd9Sstevel@tonic-gate argv[1] = "-m"; 8157c478bd9Sstevel@tonic-gate argv[2] = (char *)rawdev; 8167c478bd9Sstevel@tonic-gate argv[3] = NULL; 8177c478bd9Sstevel@tonic-gate 8187c478bd9Sstevel@tonic-gate status = forkexec(zlogp, cmdbuf, argv); 8197c478bd9Sstevel@tonic-gate if (status == 0 || status == -1) 8207c478bd9Sstevel@tonic-gate return (status); 8217c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; " 8227c478bd9Sstevel@tonic-gate "run fsck manually", rawdev, status); 8237c478bd9Sstevel@tonic-gate return (-1); 8247c478bd9Sstevel@tonic-gate } 8257c478bd9Sstevel@tonic-gate 8267c478bd9Sstevel@tonic-gate static int 8277c478bd9Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts, 8287c478bd9Sstevel@tonic-gate const char *special, const char *directory) 8297c478bd9Sstevel@tonic-gate { 8307c478bd9Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 8317c478bd9Sstevel@tonic-gate char *argv[6]; 8327c478bd9Sstevel@tonic-gate int status; 8337c478bd9Sstevel@tonic-gate 8347c478bd9Sstevel@tonic-gate /* 8357c478bd9Sstevel@tonic-gate * We could alternatively have called /usr/sbin/mount -F <fstype>, but 8367c478bd9Sstevel@tonic-gate * that would cost us an extra fork/exec without buying us anything. 8377c478bd9Sstevel@tonic-gate */ 8387c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype) 8399acbbeafSnn35248 >= sizeof (cmdbuf)) { 8407c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "file-system type %s too long", fstype); 8417c478bd9Sstevel@tonic-gate return (-1); 8427c478bd9Sstevel@tonic-gate } 8437c478bd9Sstevel@tonic-gate argv[0] = "mount"; 8447c478bd9Sstevel@tonic-gate if (opts[0] == '\0') { 8457c478bd9Sstevel@tonic-gate argv[1] = (char *)special; 8467c478bd9Sstevel@tonic-gate argv[2] = (char *)directory; 8477c478bd9Sstevel@tonic-gate argv[3] = NULL; 8487c478bd9Sstevel@tonic-gate } else { 8497c478bd9Sstevel@tonic-gate argv[1] = "-o"; 8507c478bd9Sstevel@tonic-gate argv[2] = (char *)opts; 8517c478bd9Sstevel@tonic-gate argv[3] = (char *)special; 8527c478bd9Sstevel@tonic-gate argv[4] = (char *)directory; 8537c478bd9Sstevel@tonic-gate argv[5] = NULL; 8547c478bd9Sstevel@tonic-gate } 8557c478bd9Sstevel@tonic-gate 8567c478bd9Sstevel@tonic-gate status = forkexec(zlogp, cmdbuf, argv); 8577c478bd9Sstevel@tonic-gate if (status == 0 || status == -1) 8587c478bd9Sstevel@tonic-gate return (status); 8597c478bd9Sstevel@tonic-gate if (opts[0] == '\0') 8607c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "\"%s %s %s\" " 8617c478bd9Sstevel@tonic-gate "failed with exit code %d", 8627c478bd9Sstevel@tonic-gate cmdbuf, special, directory, status); 8637c478bd9Sstevel@tonic-gate else 8647c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" " 8657c478bd9Sstevel@tonic-gate "failed with exit code %d", 8667c478bd9Sstevel@tonic-gate cmdbuf, opts, special, directory, status); 8677c478bd9Sstevel@tonic-gate return (-1); 8687c478bd9Sstevel@tonic-gate } 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate /* 8717c478bd9Sstevel@tonic-gate * Make sure if a given path exists, it is not a sym-link, and is a directory. 8727c478bd9Sstevel@tonic-gate */ 8737c478bd9Sstevel@tonic-gate static int 8747c478bd9Sstevel@tonic-gate check_path(zlog_t *zlogp, const char *path) 8757c478bd9Sstevel@tonic-gate { 8767c478bd9Sstevel@tonic-gate struct stat statbuf; 8777c478bd9Sstevel@tonic-gate char respath[MAXPATHLEN]; 8787c478bd9Sstevel@tonic-gate int res; 8797c478bd9Sstevel@tonic-gate 8807c478bd9Sstevel@tonic-gate if (lstat(path, &statbuf) != 0) { 8817c478bd9Sstevel@tonic-gate if (errno == ENOENT) 8827c478bd9Sstevel@tonic-gate return (0); 8837c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "can't stat %s", path); 8847c478bd9Sstevel@tonic-gate return (-1); 8857c478bd9Sstevel@tonic-gate } 8867c478bd9Sstevel@tonic-gate if (S_ISLNK(statbuf.st_mode)) { 8877c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is a symlink", path); 8887c478bd9Sstevel@tonic-gate return (-1); 8897c478bd9Sstevel@tonic-gate } 8907c478bd9Sstevel@tonic-gate if (!S_ISDIR(statbuf.st_mode)) { 89145916cd2Sjpk if (is_system_labeled() && S_ISREG(statbuf.st_mode)) { 89245916cd2Sjpk /* 89345916cd2Sjpk * The need to mount readonly copies of 89445916cd2Sjpk * global zone /etc/ files is unique to 89545916cd2Sjpk * Trusted Extensions. 89645916cd2Sjpk * The check for /etc/ via strstr() is to 89745916cd2Sjpk * allow paths like $ZONEROOT/etc/passwd 89845916cd2Sjpk */ 89945916cd2Sjpk if (strstr(path, "/etc/") == NULL) { 90045916cd2Sjpk zerror(zlogp, B_FALSE, 90145916cd2Sjpk "%s is not in /etc", path); 90245916cd2Sjpk return (-1); 90345916cd2Sjpk } 90445916cd2Sjpk } else { 9057c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not a directory", path); 9067c478bd9Sstevel@tonic-gate return (-1); 9077c478bd9Sstevel@tonic-gate } 90845916cd2Sjpk } 9097c478bd9Sstevel@tonic-gate if ((res = resolvepath(path, respath, sizeof (respath))) == -1) { 9107c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to resolve path %s", path); 9117c478bd9Sstevel@tonic-gate return (-1); 9127c478bd9Sstevel@tonic-gate } 9137c478bd9Sstevel@tonic-gate respath[res] = '\0'; 9147c478bd9Sstevel@tonic-gate if (strcmp(path, respath) != 0) { 9157c478bd9Sstevel@tonic-gate /* 9167c478bd9Sstevel@tonic-gate * We don't like ".."s and "."s throwing us off 9177c478bd9Sstevel@tonic-gate */ 9187c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not a canonical path", path); 9197c478bd9Sstevel@tonic-gate return (-1); 9207c478bd9Sstevel@tonic-gate } 9217c478bd9Sstevel@tonic-gate return (0); 9227c478bd9Sstevel@tonic-gate } 9237c478bd9Sstevel@tonic-gate 9247c478bd9Sstevel@tonic-gate /* 9257c478bd9Sstevel@tonic-gate * Check every component of rootpath/relpath. If any component fails (ie, 9267c478bd9Sstevel@tonic-gate * exists but isn't the canonical path to a directory), it is returned in 9277c478bd9Sstevel@tonic-gate * badpath, which is assumed to be at least of size MAXPATHLEN. 9287c478bd9Sstevel@tonic-gate * 9297c478bd9Sstevel@tonic-gate * Relpath must begin with '/'. 9307c478bd9Sstevel@tonic-gate */ 9317c478bd9Sstevel@tonic-gate static boolean_t 9327c478bd9Sstevel@tonic-gate valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *relpath) 9337c478bd9Sstevel@tonic-gate { 9347c478bd9Sstevel@tonic-gate char abspath[MAXPATHLEN], *slashp; 9357c478bd9Sstevel@tonic-gate 9367c478bd9Sstevel@tonic-gate /* 9377c478bd9Sstevel@tonic-gate * Make sure abspath has at least one '/' after its rootpath 9387c478bd9Sstevel@tonic-gate * component, and ends with '/'. 9397c478bd9Sstevel@tonic-gate */ 9409acbbeafSnn35248 if (snprintf(abspath, sizeof (abspath), "%s%s/", rootpath, relpath) >= 9417c478bd9Sstevel@tonic-gate sizeof (abspath)) { 9427c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "pathname %s%s is too long", rootpath, 9437c478bd9Sstevel@tonic-gate relpath); 9447c478bd9Sstevel@tonic-gate return (B_FALSE); 9457c478bd9Sstevel@tonic-gate } 9467c478bd9Sstevel@tonic-gate 9477c478bd9Sstevel@tonic-gate slashp = &abspath[strlen(rootpath)]; 9487c478bd9Sstevel@tonic-gate assert(*slashp == '/'); 9497c478bd9Sstevel@tonic-gate do { 9507c478bd9Sstevel@tonic-gate *slashp = '\0'; 9517c478bd9Sstevel@tonic-gate if (check_path(zlogp, abspath) != 0) 9527c478bd9Sstevel@tonic-gate return (B_FALSE); 9537c478bd9Sstevel@tonic-gate *slashp = '/'; 9547c478bd9Sstevel@tonic-gate slashp++; 9557c478bd9Sstevel@tonic-gate } while ((slashp = strchr(slashp, '/')) != NULL); 9567c478bd9Sstevel@tonic-gate return (B_TRUE); 9577c478bd9Sstevel@tonic-gate } 9587c478bd9Sstevel@tonic-gate 9597c478bd9Sstevel@tonic-gate static int 9609acbbeafSnn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name) 9619acbbeafSnn35248 { 9629acbbeafSnn35248 di_prof_t prof = arg; 9639acbbeafSnn35248 9649acbbeafSnn35248 if (name == NULL) 9659acbbeafSnn35248 return (di_prof_add_dev(prof, match)); 9669acbbeafSnn35248 return (di_prof_add_map(prof, match, name)); 9679acbbeafSnn35248 } 9689acbbeafSnn35248 9699acbbeafSnn35248 static int 9709acbbeafSnn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target) 9719acbbeafSnn35248 { 9729acbbeafSnn35248 di_prof_t prof = arg; 9739acbbeafSnn35248 9749acbbeafSnn35248 return (di_prof_add_symlink(prof, source, target)); 9759acbbeafSnn35248 } 9769acbbeafSnn35248 9779acbbeafSnn35248 /* 9789acbbeafSnn35248 * Apply the standard lists of devices/symlinks/mappings and the user-specified 9799acbbeafSnn35248 * list of devices (via zonecfg) to the /dev filesystem. The filesystem will 9809acbbeafSnn35248 * use these as a profile/filter to determine what exists in /dev. 9819acbbeafSnn35248 */ 9829acbbeafSnn35248 static int 9839acbbeafSnn35248 mount_one_dev(zlog_t *zlogp, char *devpath) 9849acbbeafSnn35248 { 9859acbbeafSnn35248 char brand[MAXNAMELEN]; 9869acbbeafSnn35248 zone_dochandle_t handle = NULL; 987123807fbSedp brand_handle_t bh = NULL; 9889acbbeafSnn35248 struct zone_devtab ztab; 9899acbbeafSnn35248 di_prof_t prof = NULL; 9909acbbeafSnn35248 int err; 9919acbbeafSnn35248 int retval = -1; 9929acbbeafSnn35248 9939acbbeafSnn35248 if (di_prof_init(devpath, &prof)) { 9949acbbeafSnn35248 zerror(zlogp, B_TRUE, "failed to initialize profile"); 9959acbbeafSnn35248 goto cleanup; 9969acbbeafSnn35248 } 9979acbbeafSnn35248 9989acbbeafSnn35248 /* Get a handle to the brand info for this zone */ 9999acbbeafSnn35248 if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) || 1000123807fbSedp (bh = brand_open(brand)) == NULL) { 10019acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 10029acbbeafSnn35248 goto cleanup; 10039acbbeafSnn35248 } 10049acbbeafSnn35248 1005123807fbSedp if (brand_platform_iter_devices(bh, zone_name, 10069acbbeafSnn35248 mount_one_dev_device_cb, prof) != 0) { 10079acbbeafSnn35248 zerror(zlogp, B_TRUE, "failed to add standard device"); 10089acbbeafSnn35248 goto cleanup; 10099acbbeafSnn35248 } 10109acbbeafSnn35248 1011123807fbSedp if (brand_platform_iter_link(bh, 10129acbbeafSnn35248 mount_one_dev_symlink_cb, prof) != 0) { 10139acbbeafSnn35248 zerror(zlogp, B_TRUE, "failed to add standard symlink"); 10149acbbeafSnn35248 goto cleanup; 10159acbbeafSnn35248 } 10169acbbeafSnn35248 10179acbbeafSnn35248 /* Add user-specified devices and directories */ 10189acbbeafSnn35248 if ((handle = zonecfg_init_handle()) == NULL) { 10199acbbeafSnn35248 zerror(zlogp, B_FALSE, "can't initialize zone handle"); 10209acbbeafSnn35248 goto cleanup; 10219acbbeafSnn35248 } 10229acbbeafSnn35248 if (err = zonecfg_get_handle(zone_name, handle)) { 10239acbbeafSnn35248 zerror(zlogp, B_FALSE, "can't get handle for zone " 10249acbbeafSnn35248 "%s: %s", zone_name, zonecfg_strerror(err)); 10259acbbeafSnn35248 goto cleanup; 10269acbbeafSnn35248 } 10279acbbeafSnn35248 if (err = zonecfg_setdevent(handle)) { 10289acbbeafSnn35248 zerror(zlogp, B_FALSE, "%s: %s", zone_name, 10299acbbeafSnn35248 zonecfg_strerror(err)); 10309acbbeafSnn35248 goto cleanup; 10319acbbeafSnn35248 } 10329acbbeafSnn35248 while (zonecfg_getdevent(handle, &ztab) == Z_OK) { 10339acbbeafSnn35248 if (di_prof_add_dev(prof, ztab.zone_dev_match)) { 10349acbbeafSnn35248 zerror(zlogp, B_TRUE, "failed to add " 10359acbbeafSnn35248 "user-specified device"); 10369acbbeafSnn35248 goto cleanup; 10379acbbeafSnn35248 } 10389acbbeafSnn35248 } 10399acbbeafSnn35248 (void) zonecfg_enddevent(handle); 10409acbbeafSnn35248 10419acbbeafSnn35248 /* Send profile to kernel */ 10429acbbeafSnn35248 if (di_prof_commit(prof)) { 10439acbbeafSnn35248 zerror(zlogp, B_TRUE, "failed to commit profile"); 10449acbbeafSnn35248 goto cleanup; 10459acbbeafSnn35248 } 10469acbbeafSnn35248 10479acbbeafSnn35248 retval = 0; 10489acbbeafSnn35248 10499acbbeafSnn35248 cleanup: 1050123807fbSedp if (bh != NULL) 1051123807fbSedp brand_close(bh); 10529acbbeafSnn35248 if (handle) 10539acbbeafSnn35248 zonecfg_fini_handle(handle); 10549acbbeafSnn35248 if (prof) 10559acbbeafSnn35248 di_prof_fini(prof); 10569acbbeafSnn35248 return (retval); 10579acbbeafSnn35248 } 10589acbbeafSnn35248 10599acbbeafSnn35248 static int 10607c478bd9Sstevel@tonic-gate mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath) 10617c478bd9Sstevel@tonic-gate { 10627c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 1063108322fbScarlsonj char specpath[MAXPATHLEN]; 10647c478bd9Sstevel@tonic-gate char optstr[MAX_MNTOPT_STR]; 10657c478bd9Sstevel@tonic-gate zone_fsopt_t *optptr; 10669acbbeafSnn35248 int rv; 10677c478bd9Sstevel@tonic-gate 10687c478bd9Sstevel@tonic-gate if (!valid_mount_path(zlogp, rootpath, fsptr->zone_fs_dir)) { 10697c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s%s is not a valid mount point", 10707c478bd9Sstevel@tonic-gate rootpath, fsptr->zone_fs_dir); 10717c478bd9Sstevel@tonic-gate return (-1); 10727c478bd9Sstevel@tonic-gate } 10737c478bd9Sstevel@tonic-gate 10747c478bd9Sstevel@tonic-gate if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir, 10757c478bd9Sstevel@tonic-gate DEFAULT_DIR_MODE) != 0) 10767c478bd9Sstevel@tonic-gate return (-1); 10777c478bd9Sstevel@tonic-gate 10787c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", rootpath, 10797c478bd9Sstevel@tonic-gate fsptr->zone_fs_dir); 10807c478bd9Sstevel@tonic-gate 10817c478bd9Sstevel@tonic-gate if (strlen(fsptr->zone_fs_special) == 0) { 10827c478bd9Sstevel@tonic-gate /* 10837c478bd9Sstevel@tonic-gate * A zero-length special is how we distinguish IPDs from 1084108322fbScarlsonj * general-purpose FSs. Make sure it mounts from a place that 1085108322fbScarlsonj * can be seen via the alternate zone's root. 10867c478bd9Sstevel@tonic-gate */ 1087108322fbScarlsonj if (snprintf(specpath, sizeof (specpath), "%s%s", 1088108322fbScarlsonj zonecfg_get_root(), fsptr->zone_fs_dir) >= 1089108322fbScarlsonj sizeof (specpath)) { 1090108322fbScarlsonj zerror(zlogp, B_FALSE, "cannot mount %s: path too " 1091108322fbScarlsonj "long in alternate root", fsptr->zone_fs_dir); 1092108322fbScarlsonj return (-1); 1093108322fbScarlsonj } 1094108322fbScarlsonj if (zonecfg_in_alt_root()) 1095108322fbScarlsonj resolve_lofs(zlogp, specpath, sizeof (specpath)); 10967c478bd9Sstevel@tonic-gate if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, 1097108322fbScarlsonj specpath, path) != 0) { 10987c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to loopback mount %s", 1099108322fbScarlsonj specpath); 11007c478bd9Sstevel@tonic-gate return (-1); 11017c478bd9Sstevel@tonic-gate } 11027c478bd9Sstevel@tonic-gate return (0); 11037c478bd9Sstevel@tonic-gate } 11047c478bd9Sstevel@tonic-gate 11057c478bd9Sstevel@tonic-gate /* 11067c478bd9Sstevel@tonic-gate * In general the strategy here is to do just as much verification as 11077c478bd9Sstevel@tonic-gate * necessary to avoid crashing or otherwise doing something bad; if the 11087c478bd9Sstevel@tonic-gate * administrator initiated the operation via zoneadm(1m), he'll get 11097c478bd9Sstevel@tonic-gate * auto-verification which will let him know what's wrong. If he 11107c478bd9Sstevel@tonic-gate * modifies the zone configuration of a running zone and doesn't attempt 11117c478bd9Sstevel@tonic-gate * to verify that it's OK we won't crash but won't bother trying to be 11127c478bd9Sstevel@tonic-gate * too helpful either. zoneadm verify is only a couple keystrokes away. 11137c478bd9Sstevel@tonic-gate */ 11147c478bd9Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) { 11157c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "cannot mount %s on %s: " 11167c478bd9Sstevel@tonic-gate "invalid file-system type %s", fsptr->zone_fs_special, 11177c478bd9Sstevel@tonic-gate fsptr->zone_fs_dir, fsptr->zone_fs_type); 11187c478bd9Sstevel@tonic-gate return (-1); 11197c478bd9Sstevel@tonic-gate } 11207c478bd9Sstevel@tonic-gate 11217c478bd9Sstevel@tonic-gate /* 1122108322fbScarlsonj * If we're looking at an alternate root environment, then construct 1123108322fbScarlsonj * read-only loopback mounts as necessary. For all lofs mounts, make 1124108322fbScarlsonj * sure that the 'special' entry points inside the alternate root. (We 1125108322fbScarlsonj * don't do this with other mounts, as devfs isn't in the alternate 1126108322fbScarlsonj * root, and we need to assume the device environment is roughly the 1127108322fbScarlsonj * same.) 1128108322fbScarlsonj */ 1129108322fbScarlsonj if (zonecfg_in_alt_root()) { 1130108322fbScarlsonj struct stat64 st; 1131108322fbScarlsonj 1132108322fbScarlsonj if (stat64(fsptr->zone_fs_special, &st) != -1 && 113368eeb376Scarlsonj S_ISBLK(st.st_mode)) { 113468eeb376Scarlsonj if (check_lofs_needed(zlogp, fsptr) == -1) 1135108322fbScarlsonj return (-1); 113668eeb376Scarlsonj } else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) { 1137108322fbScarlsonj if (snprintf(specpath, sizeof (specpath), "%s%s", 1138108322fbScarlsonj zonecfg_get_root(), fsptr->zone_fs_special) >= 1139108322fbScarlsonj sizeof (specpath)) { 1140108322fbScarlsonj zerror(zlogp, B_FALSE, "cannot mount %s: path " 1141108322fbScarlsonj "too long in alternate root", 1142108322fbScarlsonj fsptr->zone_fs_special); 1143108322fbScarlsonj return (-1); 1144108322fbScarlsonj } 1145108322fbScarlsonj resolve_lofs(zlogp, specpath, sizeof (specpath)); 1146108322fbScarlsonj (void) strlcpy(fsptr->zone_fs_special, specpath, 1147108322fbScarlsonj sizeof (fsptr->zone_fs_special)); 1148108322fbScarlsonj } 1149108322fbScarlsonj } 1150108322fbScarlsonj 1151108322fbScarlsonj /* 11527c478bd9Sstevel@tonic-gate * Run 'fsck -m' if there's a device to fsck. 11537c478bd9Sstevel@tonic-gate */ 11547c478bd9Sstevel@tonic-gate if (fsptr->zone_fs_raw[0] != '\0' && 11557c478bd9Sstevel@tonic-gate dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) 11567c478bd9Sstevel@tonic-gate return (-1); 11577c478bd9Sstevel@tonic-gate 11587c478bd9Sstevel@tonic-gate /* 11597c478bd9Sstevel@tonic-gate * Build up mount option string. 11607c478bd9Sstevel@tonic-gate */ 11617c478bd9Sstevel@tonic-gate optstr[0] = '\0'; 11627c478bd9Sstevel@tonic-gate if (fsptr->zone_fs_options != NULL) { 11637c478bd9Sstevel@tonic-gate (void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt, 11647c478bd9Sstevel@tonic-gate sizeof (optstr)); 11657c478bd9Sstevel@tonic-gate for (optptr = fsptr->zone_fs_options->zone_fsopt_next; 11667c478bd9Sstevel@tonic-gate optptr != NULL; optptr = optptr->zone_fsopt_next) { 11677c478bd9Sstevel@tonic-gate (void) strlcat(optstr, ",", sizeof (optstr)); 11687c478bd9Sstevel@tonic-gate (void) strlcat(optstr, optptr->zone_fsopt_opt, 11697c478bd9Sstevel@tonic-gate sizeof (optstr)); 11707c478bd9Sstevel@tonic-gate } 11717c478bd9Sstevel@tonic-gate } 11729acbbeafSnn35248 11739acbbeafSnn35248 if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr, 11749acbbeafSnn35248 fsptr->zone_fs_special, path)) != 0) 11759acbbeafSnn35248 return (rv); 11769acbbeafSnn35248 11779acbbeafSnn35248 /* 11789acbbeafSnn35248 * The mount succeeded. If this was not a mount of /dev then 11799acbbeafSnn35248 * we're done. 11809acbbeafSnn35248 */ 11819acbbeafSnn35248 if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0) 11829acbbeafSnn35248 return (0); 11839acbbeafSnn35248 11849acbbeafSnn35248 /* 11859acbbeafSnn35248 * We just mounted an instance of a /dev filesystem, so now we 11869acbbeafSnn35248 * need to configure it. 11879acbbeafSnn35248 */ 11889acbbeafSnn35248 return (mount_one_dev(zlogp, path)); 11897c478bd9Sstevel@tonic-gate } 11907c478bd9Sstevel@tonic-gate 11917c478bd9Sstevel@tonic-gate static void 11927c478bd9Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem) 11937c478bd9Sstevel@tonic-gate { 11947c478bd9Sstevel@tonic-gate uint_t i; 11957c478bd9Sstevel@tonic-gate 11967c478bd9Sstevel@tonic-gate if (fsarray == NULL) 11977c478bd9Sstevel@tonic-gate return; 11987c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) 11997c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(fsarray[i].zone_fs_options); 12007c478bd9Sstevel@tonic-gate free(fsarray); 12017c478bd9Sstevel@tonic-gate } 12027c478bd9Sstevel@tonic-gate 1203108322fbScarlsonj /* 1204f4368d3dSvp157776 * This function initiates the creation of a small Solaris Environment for 1205f4368d3dSvp157776 * scratch zone. The Environment creation process is split up into two 1206f4368d3dSvp157776 * functions(build_mounted_pre_var() and build_mounted_post_var()). It 1207f4368d3dSvp157776 * is done this way because: 1208f4368d3dSvp157776 * We need to have both /etc and /var in the root of the scratchzone. 1209f4368d3dSvp157776 * We loopback mount zone's own /etc and /var into the root of the 1210f4368d3dSvp157776 * scratch zone. Unlike /etc, /var can be a seperate filesystem. So we 1211f4368d3dSvp157776 * need to delay the mount of /var till the zone's root gets populated. 1212f4368d3dSvp157776 * So mounting of localdirs[](/etc and /var) have been moved to the 1213f4368d3dSvp157776 * build_mounted_post_var() which gets called only after the zone 1214f4368d3dSvp157776 * specific filesystems are mounted. 1215108322fbScarlsonj */ 1216108322fbScarlsonj static boolean_t 1217f4368d3dSvp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath, 121816bca938Svp157776 size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen) 1219108322fbScarlsonj { 1220108322fbScarlsonj char tmp[MAXPATHLEN], fromdir[MAXPATHLEN]; 1221108322fbScarlsonj const char **cpp; 1222108322fbScarlsonj static const char *mkdirs[] = { 12233f604e0fSdp "/system", "/system/contract", "/system/object", "/proc", 12243f604e0fSdp "/dev", "/tmp", "/a", NULL 1225108322fbScarlsonj }; 1226108322fbScarlsonj char *altstr; 1227f4368d3dSvp157776 FILE *fp; 1228108322fbScarlsonj uuid_t uuid; 1229108322fbScarlsonj 12309acbbeafSnn35248 assert(zone_isnative); 12319acbbeafSnn35248 1232108322fbScarlsonj resolve_lofs(zlogp, rootpath, rootlen); 123316bca938Svp157776 (void) snprintf(luroot, lurootlen, "%s/lu", zonepath); 123416bca938Svp157776 resolve_lofs(zlogp, luroot, lurootlen); 1235108322fbScarlsonj (void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot); 1236108322fbScarlsonj (void) symlink("./usr/bin", tmp); 1237108322fbScarlsonj 1238108322fbScarlsonj /* 1239108322fbScarlsonj * These are mostly special mount points; not handled here. (See 1240108322fbScarlsonj * zone_mount_early.) 1241108322fbScarlsonj */ 1242108322fbScarlsonj for (cpp = mkdirs; *cpp != NULL; cpp++) { 1243108322fbScarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1244108322fbScarlsonj if (mkdir(tmp, 0755) != 0) { 1245108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1246108322fbScarlsonj return (B_FALSE); 1247108322fbScarlsonj } 1248108322fbScarlsonj } 1249f4368d3dSvp157776 /* 1250f4368d3dSvp157776 * This is here to support lucopy. If there's an instance of this same 1251f4368d3dSvp157776 * zone on the current running system, then we mount its root up as 1252f4368d3dSvp157776 * read-only inside the scratch zone. 1253f4368d3dSvp157776 */ 1254f4368d3dSvp157776 (void) zonecfg_get_uuid(zone_name, uuid); 1255f4368d3dSvp157776 altstr = strdup(zonecfg_get_root()); 1256f4368d3dSvp157776 if (altstr == NULL) { 1257f4368d3dSvp157776 zerror(zlogp, B_TRUE, "memory allocation failed"); 1258f4368d3dSvp157776 return (B_FALSE); 1259f4368d3dSvp157776 } 1260f4368d3dSvp157776 zonecfg_set_root(""); 1261f4368d3dSvp157776 (void) strlcpy(tmp, zone_name, sizeof (tmp)); 1262f4368d3dSvp157776 (void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp)); 1263f4368d3dSvp157776 if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK && 1264f4368d3dSvp157776 strcmp(fromdir, rootpath) != 0) { 1265f4368d3dSvp157776 (void) snprintf(tmp, sizeof (tmp), "%s/b", luroot); 1266f4368d3dSvp157776 if (mkdir(tmp, 0755) != 0) { 1267f4368d3dSvp157776 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1268f4368d3dSvp157776 return (B_FALSE); 1269f4368d3dSvp157776 } 1270f4368d3dSvp157776 if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir, 1271f4368d3dSvp157776 tmp) != 0) { 1272f4368d3dSvp157776 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1273f4368d3dSvp157776 fromdir); 1274f4368d3dSvp157776 return (B_FALSE); 1275f4368d3dSvp157776 } 1276f4368d3dSvp157776 } 1277f4368d3dSvp157776 zonecfg_set_root(altstr); 1278f4368d3dSvp157776 free(altstr); 1279f4368d3dSvp157776 1280f4368d3dSvp157776 if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) { 1281f4368d3dSvp157776 zerror(zlogp, B_TRUE, "cannot open zone mapfile"); 1282f4368d3dSvp157776 return (B_FALSE); 1283f4368d3dSvp157776 } 1284f4368d3dSvp157776 (void) ftruncate(fileno(fp), 0); 1285f4368d3dSvp157776 if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) { 1286f4368d3dSvp157776 zerror(zlogp, B_TRUE, "cannot add zone mapfile entry"); 1287f4368d3dSvp157776 } 1288f4368d3dSvp157776 zonecfg_close_scratch(fp); 1289f4368d3dSvp157776 (void) snprintf(tmp, sizeof (tmp), "%s/a", luroot); 1290f4368d3dSvp157776 if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0) 1291f4368d3dSvp157776 return (B_FALSE); 1292f4368d3dSvp157776 (void) strlcpy(rootpath, tmp, rootlen); 1293f4368d3dSvp157776 return (B_TRUE); 1294f4368d3dSvp157776 } 1295f4368d3dSvp157776 1296f4368d3dSvp157776 1297f4368d3dSvp157776 static boolean_t 129816bca938Svp157776 build_mounted_post_var(zlog_t *zlogp, char *rootpath, const char *luroot) 1299f4368d3dSvp157776 { 1300f4368d3dSvp157776 char tmp[MAXPATHLEN], fromdir[MAXPATHLEN]; 1301f4368d3dSvp157776 const char **cpp; 1302f4368d3dSvp157776 static const char *localdirs[] = { 1303f4368d3dSvp157776 "/etc", "/var", NULL 1304f4368d3dSvp157776 }; 1305f4368d3dSvp157776 static const char *loopdirs[] = { 1306f4368d3dSvp157776 "/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform", 1307f4368d3dSvp157776 "/usr", NULL 1308f4368d3dSvp157776 }; 1309f4368d3dSvp157776 static const char *tmpdirs[] = { 1310f4368d3dSvp157776 "/tmp", "/var/run", NULL 1311f4368d3dSvp157776 }; 1312f4368d3dSvp157776 struct stat st; 1313f4368d3dSvp157776 1314108322fbScarlsonj /* 1315108322fbScarlsonj * These are mounted read-write from the zone undergoing upgrade. We 1316108322fbScarlsonj * must be careful not to 'leak' things from the main system into the 1317108322fbScarlsonj * zone, and this accomplishes that goal. 1318108322fbScarlsonj */ 1319108322fbScarlsonj for (cpp = localdirs; *cpp != NULL; cpp++) { 1320108322fbScarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1321108322fbScarlsonj (void) snprintf(fromdir, sizeof (fromdir), "%s%s", rootpath, 1322108322fbScarlsonj *cpp); 1323108322fbScarlsonj if (mkdir(tmp, 0755) != 0) { 1324108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1325108322fbScarlsonj return (B_FALSE); 1326108322fbScarlsonj } 1327108322fbScarlsonj if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp) != 0) { 1328108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1329108322fbScarlsonj *cpp); 1330108322fbScarlsonj return (B_FALSE); 1331108322fbScarlsonj } 1332108322fbScarlsonj } 1333108322fbScarlsonj 1334108322fbScarlsonj /* 1335108322fbScarlsonj * These are things mounted read-only from the running system because 1336108322fbScarlsonj * they contain binaries that must match system. 1337108322fbScarlsonj */ 1338108322fbScarlsonj for (cpp = loopdirs; *cpp != NULL; cpp++) { 1339108322fbScarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1340108322fbScarlsonj if (mkdir(tmp, 0755) != 0) { 1341108322fbScarlsonj if (errno != EEXIST) { 1342108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1343108322fbScarlsonj return (B_FALSE); 1344108322fbScarlsonj } 1345108322fbScarlsonj if (lstat(tmp, &st) != 0) { 1346108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot stat %s", tmp); 1347108322fbScarlsonj return (B_FALSE); 1348108322fbScarlsonj } 1349108322fbScarlsonj /* 1350108322fbScarlsonj * Ignore any non-directories encountered. These are 1351108322fbScarlsonj * things that have been converted into symlinks 1352108322fbScarlsonj * (/etc/fs and /etc/lib) and no longer need a lofs 1353108322fbScarlsonj * fixup. 1354108322fbScarlsonj */ 1355108322fbScarlsonj if (!S_ISDIR(st.st_mode)) 1356108322fbScarlsonj continue; 1357108322fbScarlsonj } 1358108322fbScarlsonj if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp, 1359108322fbScarlsonj tmp) != 0) { 1360108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1361108322fbScarlsonj *cpp); 1362108322fbScarlsonj return (B_FALSE); 1363108322fbScarlsonj } 1364108322fbScarlsonj } 1365108322fbScarlsonj 1366108322fbScarlsonj /* 1367108322fbScarlsonj * These are things with tmpfs mounted inside. 1368108322fbScarlsonj */ 1369108322fbScarlsonj for (cpp = tmpdirs; *cpp != NULL; cpp++) { 1370108322fbScarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1371108322fbScarlsonj if (mkdir(tmp, 0755) != 0 && errno != EEXIST) { 1372108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1373108322fbScarlsonj return (B_FALSE); 1374108322fbScarlsonj } 1375108322fbScarlsonj if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) { 1376108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp); 1377108322fbScarlsonj return (B_FALSE); 1378108322fbScarlsonj } 1379108322fbScarlsonj } 1380108322fbScarlsonj return (B_TRUE); 1381108322fbScarlsonj } 1382108322fbScarlsonj 13839acbbeafSnn35248 typedef struct plat_gmount_cb_data { 13849acbbeafSnn35248 zlog_t *pgcd_zlogp; 13859acbbeafSnn35248 struct zone_fstab **pgcd_fs_tab; 13869acbbeafSnn35248 int *pgcd_num_fs; 13879acbbeafSnn35248 } plat_gmount_cb_data_t; 13889acbbeafSnn35248 13899acbbeafSnn35248 /* 13909acbbeafSnn35248 * plat_gmount_cb() is a callback function invoked by libbrand to iterate 13919acbbeafSnn35248 * through all global brand platform mounts. 13929acbbeafSnn35248 */ 13939acbbeafSnn35248 int 13949acbbeafSnn35248 plat_gmount_cb(void *data, const char *spec, const char *dir, 13959acbbeafSnn35248 const char *fstype, const char *opt) 13969acbbeafSnn35248 { 13979acbbeafSnn35248 plat_gmount_cb_data_t *cp = data; 13989acbbeafSnn35248 zlog_t *zlogp = cp->pgcd_zlogp; 13999acbbeafSnn35248 struct zone_fstab *fs_ptr = *cp->pgcd_fs_tab; 14009acbbeafSnn35248 int num_fs = *cp->pgcd_num_fs; 14019acbbeafSnn35248 struct zone_fstab *fsp, *tmp_ptr; 14029acbbeafSnn35248 14039acbbeafSnn35248 num_fs++; 14049acbbeafSnn35248 if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) { 14059acbbeafSnn35248 zerror(zlogp, B_TRUE, "memory allocation failed"); 14069acbbeafSnn35248 return (-1); 14079acbbeafSnn35248 } 14089acbbeafSnn35248 14099acbbeafSnn35248 fs_ptr = tmp_ptr; 14109acbbeafSnn35248 fsp = &fs_ptr[num_fs - 1]; 14119acbbeafSnn35248 14129acbbeafSnn35248 /* update the callback struct passed in */ 14139acbbeafSnn35248 *cp->pgcd_fs_tab = fs_ptr; 14149acbbeafSnn35248 *cp->pgcd_num_fs = num_fs; 14159acbbeafSnn35248 14169acbbeafSnn35248 fsp->zone_fs_raw[0] = '\0'; 14179acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_special, spec, 14189acbbeafSnn35248 sizeof (fsp->zone_fs_special)); 14199acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir)); 14209acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type)); 14219acbbeafSnn35248 fsp->zone_fs_options = NULL; 14229acbbeafSnn35248 if (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK) { 14239acbbeafSnn35248 zerror(zlogp, B_FALSE, "error adding property"); 14249acbbeafSnn35248 return (-1); 14259acbbeafSnn35248 } 14269acbbeafSnn35248 14279acbbeafSnn35248 return (0); 14289acbbeafSnn35248 } 14299acbbeafSnn35248 14309acbbeafSnn35248 static int 14319acbbeafSnn35248 mount_filesystems_ipdent(zone_dochandle_t handle, zlog_t *zlogp, 14329acbbeafSnn35248 struct zone_fstab **fs_tabp, int *num_fsp) 14339acbbeafSnn35248 { 14349acbbeafSnn35248 struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab; 14359acbbeafSnn35248 int num_fs; 14369acbbeafSnn35248 14379acbbeafSnn35248 num_fs = *num_fsp; 14389acbbeafSnn35248 fs_ptr = *fs_tabp; 14399acbbeafSnn35248 14409acbbeafSnn35248 if (zonecfg_setipdent(handle) != Z_OK) { 14419acbbeafSnn35248 zerror(zlogp, B_FALSE, "invalid configuration"); 14429acbbeafSnn35248 return (-1); 14439acbbeafSnn35248 } 14449acbbeafSnn35248 while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 14459acbbeafSnn35248 num_fs++; 14469acbbeafSnn35248 if ((tmp_ptr = realloc(fs_ptr, 14479acbbeafSnn35248 num_fs * sizeof (*tmp_ptr))) == NULL) { 14489acbbeafSnn35248 zerror(zlogp, B_TRUE, "memory allocation failed"); 14499acbbeafSnn35248 (void) zonecfg_endipdent(handle); 14509acbbeafSnn35248 return (-1); 14519acbbeafSnn35248 } 14529acbbeafSnn35248 14539acbbeafSnn35248 /* update the pointers passed in */ 14549acbbeafSnn35248 *fs_tabp = tmp_ptr; 14559acbbeafSnn35248 *num_fsp = num_fs; 14569acbbeafSnn35248 14579acbbeafSnn35248 /* 14589acbbeafSnn35248 * IPDs logically only have a mount point; all other properties 14599acbbeafSnn35248 * are implied. 14609acbbeafSnn35248 */ 14619acbbeafSnn35248 fs_ptr = tmp_ptr; 14629acbbeafSnn35248 fsp = &fs_ptr[num_fs - 1]; 14639acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_dir, 14649acbbeafSnn35248 fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir)); 14659acbbeafSnn35248 fsp->zone_fs_special[0] = '\0'; 14669acbbeafSnn35248 fsp->zone_fs_raw[0] = '\0'; 14679acbbeafSnn35248 fsp->zone_fs_type[0] = '\0'; 14689acbbeafSnn35248 fsp->zone_fs_options = NULL; 14699acbbeafSnn35248 } 14709acbbeafSnn35248 (void) zonecfg_endipdent(handle); 14719acbbeafSnn35248 return (0); 14729acbbeafSnn35248 } 14739acbbeafSnn35248 14749acbbeafSnn35248 static int 14759acbbeafSnn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp, 14769acbbeafSnn35248 struct zone_fstab **fs_tabp, int *num_fsp, int mount_cmd) 14779acbbeafSnn35248 { 14789acbbeafSnn35248 struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab; 14799acbbeafSnn35248 int num_fs; 14809acbbeafSnn35248 14819acbbeafSnn35248 num_fs = *num_fsp; 14829acbbeafSnn35248 fs_ptr = *fs_tabp; 14839acbbeafSnn35248 14849acbbeafSnn35248 if (zonecfg_setfsent(handle) != Z_OK) { 14859acbbeafSnn35248 zerror(zlogp, B_FALSE, "invalid configuration"); 14869acbbeafSnn35248 return (-1); 14879acbbeafSnn35248 } 14889acbbeafSnn35248 while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 14899acbbeafSnn35248 /* 14909acbbeafSnn35248 * ZFS filesystems will not be accessible under an alternate 14919acbbeafSnn35248 * root, since the pool will not be known. Ignore them in this 14929acbbeafSnn35248 * case. 14939acbbeafSnn35248 */ 14949acbbeafSnn35248 if (mount_cmd && strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0) 14959acbbeafSnn35248 continue; 14969acbbeafSnn35248 14979acbbeafSnn35248 num_fs++; 14989acbbeafSnn35248 if ((tmp_ptr = realloc(fs_ptr, 14999acbbeafSnn35248 num_fs * sizeof (*tmp_ptr))) == NULL) { 15009acbbeafSnn35248 zerror(zlogp, B_TRUE, "memory allocation failed"); 15019acbbeafSnn35248 (void) zonecfg_endfsent(handle); 15029acbbeafSnn35248 return (-1); 15039acbbeafSnn35248 } 15049acbbeafSnn35248 /* update the pointers passed in */ 15059acbbeafSnn35248 *fs_tabp = tmp_ptr; 15069acbbeafSnn35248 *num_fsp = num_fs; 15079acbbeafSnn35248 15089acbbeafSnn35248 fs_ptr = tmp_ptr; 15099acbbeafSnn35248 fsp = &fs_ptr[num_fs - 1]; 15109acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_dir, 15119acbbeafSnn35248 fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir)); 15129acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_special, fstab.zone_fs_special, 15139acbbeafSnn35248 sizeof (fsp->zone_fs_special)); 15149acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw, 15159acbbeafSnn35248 sizeof (fsp->zone_fs_raw)); 15169acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type, 15179acbbeafSnn35248 sizeof (fsp->zone_fs_type)); 15189acbbeafSnn35248 fsp->zone_fs_options = fstab.zone_fs_options; 15199acbbeafSnn35248 } 15209acbbeafSnn35248 (void) zonecfg_endfsent(handle); 15219acbbeafSnn35248 return (0); 15229acbbeafSnn35248 } 15239acbbeafSnn35248 15247c478bd9Sstevel@tonic-gate static int 1525108322fbScarlsonj mount_filesystems(zlog_t *zlogp, boolean_t mount_cmd) 15267c478bd9Sstevel@tonic-gate { 15277c478bd9Sstevel@tonic-gate char rootpath[MAXPATHLEN]; 15287c478bd9Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 15299acbbeafSnn35248 char brand[MAXNAMELEN]; 153016bca938Svp157776 char luroot[MAXPATHLEN]; 15319acbbeafSnn35248 int i, num_fs = 0; 15329acbbeafSnn35248 struct zone_fstab *fs_ptr = NULL; 15337c478bd9Sstevel@tonic-gate zone_dochandle_t handle = NULL; 15347c478bd9Sstevel@tonic-gate zone_state_t zstate; 1535123807fbSedp brand_handle_t bh; 15369acbbeafSnn35248 plat_gmount_cb_data_t cb; 15377c478bd9Sstevel@tonic-gate 15387c478bd9Sstevel@tonic-gate if (zone_get_state(zone_name, &zstate) != Z_OK || 1539108322fbScarlsonj (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) { 15407c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 1541108322fbScarlsonj "zone must be in '%s' or '%s' state to mount file-systems", 1542108322fbScarlsonj zone_state_str(ZONE_STATE_READY), 1543108322fbScarlsonj zone_state_str(ZONE_STATE_MOUNTED)); 15447c478bd9Sstevel@tonic-gate goto bad; 15457c478bd9Sstevel@tonic-gate } 15467c478bd9Sstevel@tonic-gate 15477c478bd9Sstevel@tonic-gate if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 15487c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone path"); 15497c478bd9Sstevel@tonic-gate goto bad; 15507c478bd9Sstevel@tonic-gate } 15517c478bd9Sstevel@tonic-gate 15527c478bd9Sstevel@tonic-gate if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) { 15537c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone root"); 15547c478bd9Sstevel@tonic-gate goto bad; 15557c478bd9Sstevel@tonic-gate } 15567c478bd9Sstevel@tonic-gate 15577c478bd9Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 1558ffbafc53Scomay zerror(zlogp, B_TRUE, "getting zone configuration handle"); 15597c478bd9Sstevel@tonic-gate goto bad; 15607c478bd9Sstevel@tonic-gate } 15617c478bd9Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK || 15627c478bd9Sstevel@tonic-gate zonecfg_setfsent(handle) != Z_OK) { 15637c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration"); 15647c478bd9Sstevel@tonic-gate goto bad; 15657c478bd9Sstevel@tonic-gate } 15667c478bd9Sstevel@tonic-gate 15679acbbeafSnn35248 /* Get a handle to the brand info for this zone */ 15689acbbeafSnn35248 if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) || 1569123807fbSedp (bh = brand_open(brand)) == NULL) { 15709acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 15719acbbeafSnn35248 return (-1); 15729acbbeafSnn35248 } 15739acbbeafSnn35248 15749acbbeafSnn35248 /* 15759acbbeafSnn35248 * Get the list of global filesystems to mount from the brand 15769acbbeafSnn35248 * configuration. 15779acbbeafSnn35248 */ 15789acbbeafSnn35248 cb.pgcd_zlogp = zlogp; 15799acbbeafSnn35248 cb.pgcd_fs_tab = &fs_ptr; 15809acbbeafSnn35248 cb.pgcd_num_fs = &num_fs; 1581123807fbSedp if (brand_platform_iter_gmounts(bh, zonepath, 15829acbbeafSnn35248 plat_gmount_cb, &cb) != 0) { 15839acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to mount filesystems"); 1584123807fbSedp brand_close(bh); 15859acbbeafSnn35248 return (-1); 15869acbbeafSnn35248 } 1587123807fbSedp brand_close(bh); 15889acbbeafSnn35248 15897c478bd9Sstevel@tonic-gate /* 15907c478bd9Sstevel@tonic-gate * Iterate through the rest of the filesystems, first the IPDs, then 15917c478bd9Sstevel@tonic-gate * the general FSs. Sort them all, then mount them in sorted order. 15927c478bd9Sstevel@tonic-gate * This is to make sure the higher level directories (e.g., /usr) 15937c478bd9Sstevel@tonic-gate * get mounted before any beneath them (e.g., /usr/local). 15947c478bd9Sstevel@tonic-gate */ 15959acbbeafSnn35248 if (mount_filesystems_ipdent(handle, zlogp, &fs_ptr, &num_fs) != 0) 15967c478bd9Sstevel@tonic-gate goto bad; 15977c478bd9Sstevel@tonic-gate 15989acbbeafSnn35248 if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs, 15999acbbeafSnn35248 mount_cmd) != 0) 16007c478bd9Sstevel@tonic-gate goto bad; 1601fa9e4066Sahrens 16027c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 16037c478bd9Sstevel@tonic-gate handle = NULL; 16047c478bd9Sstevel@tonic-gate 1605108322fbScarlsonj /* 16069acbbeafSnn35248 * Normally when we mount a zone all the zone filesystems 16079acbbeafSnn35248 * get mounted relative to rootpath, which is usually 16089acbbeafSnn35248 * <zonepath>/root. But when mounting a zone for administration 16099acbbeafSnn35248 * purposes via the zone "mount" state, build_mounted_pre_var() 16109acbbeafSnn35248 * updates rootpath to be <zonepath>/lu/a so we'll mount all 16119acbbeafSnn35248 * the zones filesystems there instead. 16129acbbeafSnn35248 * 16139acbbeafSnn35248 * build_mounted_pre_var() and build_mounted_post_var() will 16149acbbeafSnn35248 * also do some extra work to create directories and lofs mount 16159acbbeafSnn35248 * a bunch of global zone file system paths into <zonepath>/lu. 16169acbbeafSnn35248 * 16179acbbeafSnn35248 * This allows us to be able to enter the zone (now rooted at 16189acbbeafSnn35248 * <zonepath>/lu) and run the upgrade/patch tools that are in the 16199acbbeafSnn35248 * global zone and have them upgrade the to-be-modified zone's 16209acbbeafSnn35248 * files mounted on /a. (Which mirrors the existing standard 16219acbbeafSnn35248 * upgrade environment.) 16229acbbeafSnn35248 * 16239acbbeafSnn35248 * There is of course one catch. When doing the upgrade 16249acbbeafSnn35248 * we need <zoneroot>/lu/dev to be the /dev filesystem 16259acbbeafSnn35248 * for the zone and we don't want to have any /dev filesystem 16269acbbeafSnn35248 * mounted at <zoneroot>/lu/a/dev. Since /dev is specified 16279acbbeafSnn35248 * as a normal zone filesystem by default we'll try to mount 16289acbbeafSnn35248 * it at <zoneroot>/lu/a/dev, so we have to detect this 16299acbbeafSnn35248 * case and instead mount it at <zoneroot>/lu/dev. 16309acbbeafSnn35248 * 16319acbbeafSnn35248 * All this work is done in three phases: 1632f4368d3dSvp157776 * 1) Create and populate lu directory (build_mounted_pre_var()). 1633f4368d3dSvp157776 * 2) Mount the required filesystems as per the zone configuration. 1634f4368d3dSvp157776 * 3) Set up the rest of the scratch zone environment 1635f4368d3dSvp157776 * (build_mounted_post_var()). 1636108322fbScarlsonj */ 1637108322fbScarlsonj if (mount_cmd && 1638f4368d3dSvp157776 !build_mounted_pre_var(zlogp, 163916bca938Svp157776 rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot))) 1640108322fbScarlsonj goto bad; 1641108322fbScarlsonj 16427c478bd9Sstevel@tonic-gate qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare); 16439acbbeafSnn35248 16447c478bd9Sstevel@tonic-gate for (i = 0; i < num_fs; i++) { 16459acbbeafSnn35248 if (mount_cmd && 16469acbbeafSnn35248 strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) { 16479acbbeafSnn35248 size_t slen = strlen(rootpath) - 2; 16489acbbeafSnn35248 16499acbbeafSnn35248 /* 16509acbbeafSnn35248 * By default we'll try to mount /dev as /a/dev 16519acbbeafSnn35248 * but /dev is special and always goes at the top 16529acbbeafSnn35248 * so strip the trailing '/a' from the rootpath. 16539acbbeafSnn35248 */ 16549acbbeafSnn35248 assert(zone_isnative); 16559acbbeafSnn35248 assert(strcmp(&rootpath[slen], "/a") == 0); 16569acbbeafSnn35248 rootpath[slen] = '\0'; 16579acbbeafSnn35248 if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0) 16589acbbeafSnn35248 goto bad; 16599acbbeafSnn35248 rootpath[slen] = '/'; 16609acbbeafSnn35248 continue; 16619acbbeafSnn35248 } 16627c478bd9Sstevel@tonic-gate if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0) 16637c478bd9Sstevel@tonic-gate goto bad; 16647c478bd9Sstevel@tonic-gate } 1665f4368d3dSvp157776 if (mount_cmd && 166616bca938Svp157776 !build_mounted_post_var(zlogp, rootpath, luroot)) 1667f4368d3dSvp157776 goto bad; 166845916cd2Sjpk 166945916cd2Sjpk /* 167045916cd2Sjpk * For Trusted Extensions cross-mount each lower level /export/home 167145916cd2Sjpk */ 167248451833Scarlsonj if (!mount_cmd && tsol_mounts(zlogp, zone_name, rootpath) != 0) 167345916cd2Sjpk goto bad; 167445916cd2Sjpk 16757c478bd9Sstevel@tonic-gate free_fs_data(fs_ptr, num_fs); 16767c478bd9Sstevel@tonic-gate 16777c478bd9Sstevel@tonic-gate /* 16787c478bd9Sstevel@tonic-gate * Everything looks fine. 16797c478bd9Sstevel@tonic-gate */ 16807c478bd9Sstevel@tonic-gate return (0); 16817c478bd9Sstevel@tonic-gate 16827c478bd9Sstevel@tonic-gate bad: 16837c478bd9Sstevel@tonic-gate if (handle != NULL) 16847c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 16857c478bd9Sstevel@tonic-gate free_fs_data(fs_ptr, num_fs); 16867c478bd9Sstevel@tonic-gate return (-1); 16877c478bd9Sstevel@tonic-gate } 16887c478bd9Sstevel@tonic-gate 16897c478bd9Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */ 16907c478bd9Sstevel@tonic-gate static int 16917c478bd9Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr) 16927c478bd9Sstevel@tonic-gate { 16937c478bd9Sstevel@tonic-gate int prefixlen; 16947c478bd9Sstevel@tonic-gate 16957c478bd9Sstevel@tonic-gate prefixlen = atoi(prefixstr); 16967c478bd9Sstevel@tonic-gate if (prefixlen < 0 || prefixlen > maxprefixlen) 16977c478bd9Sstevel@tonic-gate return (1); 16987c478bd9Sstevel@tonic-gate while (prefixlen > 0) { 16997c478bd9Sstevel@tonic-gate if (prefixlen >= 8) { 17007c478bd9Sstevel@tonic-gate *maskstr++ = 0xFF; 17017c478bd9Sstevel@tonic-gate prefixlen -= 8; 17027c478bd9Sstevel@tonic-gate continue; 17037c478bd9Sstevel@tonic-gate } 17047c478bd9Sstevel@tonic-gate *maskstr |= 1 << (8 - prefixlen); 17057c478bd9Sstevel@tonic-gate prefixlen--; 17067c478bd9Sstevel@tonic-gate } 17077c478bd9Sstevel@tonic-gate return (0); 17087c478bd9Sstevel@tonic-gate } 17097c478bd9Sstevel@tonic-gate 17107c478bd9Sstevel@tonic-gate /* 17117c478bd9Sstevel@tonic-gate * Tear down all interfaces belonging to the given zone. This should 17127c478bd9Sstevel@tonic-gate * be called with the zone in a state other than "running", so that 17137c478bd9Sstevel@tonic-gate * interfaces can't be assigned to the zone after this returns. 17147c478bd9Sstevel@tonic-gate * 17157c478bd9Sstevel@tonic-gate * If anything goes wrong, log an error message and return an error. 17167c478bd9Sstevel@tonic-gate */ 17177c478bd9Sstevel@tonic-gate static int 17187c478bd9Sstevel@tonic-gate unconfigure_network_interfaces(zlog_t *zlogp, zoneid_t zone_id) 17197c478bd9Sstevel@tonic-gate { 17207c478bd9Sstevel@tonic-gate struct lifnum lifn; 17217c478bd9Sstevel@tonic-gate struct lifconf lifc; 17227c478bd9Sstevel@tonic-gate struct lifreq *lifrp, lifrl; 17237c478bd9Sstevel@tonic-gate int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES; 17247c478bd9Sstevel@tonic-gate int num_ifs, s, i, ret_code = 0; 17257c478bd9Sstevel@tonic-gate uint_t bufsize; 17267c478bd9Sstevel@tonic-gate char *buf = NULL; 17277c478bd9Sstevel@tonic-gate 17287c478bd9Sstevel@tonic-gate if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 17297c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get socket"); 17307c478bd9Sstevel@tonic-gate ret_code = -1; 17317c478bd9Sstevel@tonic-gate goto bad; 17327c478bd9Sstevel@tonic-gate } 17337c478bd9Sstevel@tonic-gate lifn.lifn_family = AF_UNSPEC; 17347c478bd9Sstevel@tonic-gate lifn.lifn_flags = (int)lifc_flags; 17357c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) { 17367c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 17377c478bd9Sstevel@tonic-gate "could not determine number of interfaces"); 17387c478bd9Sstevel@tonic-gate ret_code = -1; 17397c478bd9Sstevel@tonic-gate goto bad; 17407c478bd9Sstevel@tonic-gate } 17417c478bd9Sstevel@tonic-gate num_ifs = lifn.lifn_count; 17427c478bd9Sstevel@tonic-gate bufsize = num_ifs * sizeof (struct lifreq); 17437c478bd9Sstevel@tonic-gate if ((buf = malloc(bufsize)) == NULL) { 17447c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 17457c478bd9Sstevel@tonic-gate ret_code = -1; 17467c478bd9Sstevel@tonic-gate goto bad; 17477c478bd9Sstevel@tonic-gate } 17487c478bd9Sstevel@tonic-gate lifc.lifc_family = AF_UNSPEC; 17497c478bd9Sstevel@tonic-gate lifc.lifc_flags = (int)lifc_flags; 17507c478bd9Sstevel@tonic-gate lifc.lifc_len = bufsize; 17517c478bd9Sstevel@tonic-gate lifc.lifc_buf = buf; 17527c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) { 17537c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get configured interfaces"); 17547c478bd9Sstevel@tonic-gate ret_code = -1; 17557c478bd9Sstevel@tonic-gate goto bad; 17567c478bd9Sstevel@tonic-gate } 17577c478bd9Sstevel@tonic-gate lifrp = lifc.lifc_req; 17587c478bd9Sstevel@tonic-gate for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) { 17597c478bd9Sstevel@tonic-gate (void) close(s); 17607c478bd9Sstevel@tonic-gate if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) < 17617c478bd9Sstevel@tonic-gate 0) { 17627c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get socket", 17637c478bd9Sstevel@tonic-gate lifrl.lifr_name); 17647c478bd9Sstevel@tonic-gate ret_code = -1; 17657c478bd9Sstevel@tonic-gate continue; 17667c478bd9Sstevel@tonic-gate } 17677c478bd9Sstevel@tonic-gate (void) memset(&lifrl, 0, sizeof (lifrl)); 17687c478bd9Sstevel@tonic-gate (void) strncpy(lifrl.lifr_name, lifrp->lifr_name, 17697c478bd9Sstevel@tonic-gate sizeof (lifrl.lifr_name)); 17707c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) { 17717c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 17727c478bd9Sstevel@tonic-gate "%s: could not determine zone interface belongs to", 17737c478bd9Sstevel@tonic-gate lifrl.lifr_name); 17747c478bd9Sstevel@tonic-gate ret_code = -1; 17757c478bd9Sstevel@tonic-gate continue; 17767c478bd9Sstevel@tonic-gate } 17777c478bd9Sstevel@tonic-gate if (lifrl.lifr_zoneid == zone_id) { 17787c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) { 17797c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 17807c478bd9Sstevel@tonic-gate "%s: could not remove interface", 17817c478bd9Sstevel@tonic-gate lifrl.lifr_name); 17827c478bd9Sstevel@tonic-gate ret_code = -1; 17837c478bd9Sstevel@tonic-gate continue; 17847c478bd9Sstevel@tonic-gate } 17857c478bd9Sstevel@tonic-gate } 17867c478bd9Sstevel@tonic-gate } 17877c478bd9Sstevel@tonic-gate bad: 17887c478bd9Sstevel@tonic-gate if (s > 0) 17897c478bd9Sstevel@tonic-gate (void) close(s); 17907c478bd9Sstevel@tonic-gate if (buf) 17917c478bd9Sstevel@tonic-gate free(buf); 17927c478bd9Sstevel@tonic-gate return (ret_code); 17937c478bd9Sstevel@tonic-gate } 17947c478bd9Sstevel@tonic-gate 17957c478bd9Sstevel@tonic-gate static union sockunion { 17967c478bd9Sstevel@tonic-gate struct sockaddr sa; 17977c478bd9Sstevel@tonic-gate struct sockaddr_in sin; 17987c478bd9Sstevel@tonic-gate struct sockaddr_dl sdl; 17997c478bd9Sstevel@tonic-gate struct sockaddr_in6 sin6; 18007c478bd9Sstevel@tonic-gate } so_dst, so_ifp; 18017c478bd9Sstevel@tonic-gate 18027c478bd9Sstevel@tonic-gate static struct { 18037c478bd9Sstevel@tonic-gate struct rt_msghdr hdr; 18047c478bd9Sstevel@tonic-gate char space[512]; 18057c478bd9Sstevel@tonic-gate } rtmsg; 18067c478bd9Sstevel@tonic-gate 18077c478bd9Sstevel@tonic-gate static int 18087c478bd9Sstevel@tonic-gate salen(struct sockaddr *sa) 18097c478bd9Sstevel@tonic-gate { 18107c478bd9Sstevel@tonic-gate switch (sa->sa_family) { 18117c478bd9Sstevel@tonic-gate case AF_INET: 18127c478bd9Sstevel@tonic-gate return (sizeof (struct sockaddr_in)); 18137c478bd9Sstevel@tonic-gate case AF_LINK: 18147c478bd9Sstevel@tonic-gate return (sizeof (struct sockaddr_dl)); 18157c478bd9Sstevel@tonic-gate case AF_INET6: 18167c478bd9Sstevel@tonic-gate return (sizeof (struct sockaddr_in6)); 18177c478bd9Sstevel@tonic-gate default: 18187c478bd9Sstevel@tonic-gate return (sizeof (struct sockaddr)); 18197c478bd9Sstevel@tonic-gate } 18207c478bd9Sstevel@tonic-gate } 18217c478bd9Sstevel@tonic-gate 18227c478bd9Sstevel@tonic-gate #define ROUNDUP_LONG(a) \ 18237c478bd9Sstevel@tonic-gate ((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long)) 18247c478bd9Sstevel@tonic-gate 18257c478bd9Sstevel@tonic-gate /* 18267c478bd9Sstevel@tonic-gate * Look up which zone is using a given IP address. The address in question 18277c478bd9Sstevel@tonic-gate * is expected to have been stuffed into the structure to which lifr points 18287c478bd9Sstevel@tonic-gate * via a previous SIOCGLIFADDR ioctl(). 18297c478bd9Sstevel@tonic-gate * 18307c478bd9Sstevel@tonic-gate * This is done using black router socket magic. 18317c478bd9Sstevel@tonic-gate * 18327c478bd9Sstevel@tonic-gate * Return the name of the zone on success or NULL on failure. 18337c478bd9Sstevel@tonic-gate * 18347c478bd9Sstevel@tonic-gate * This is a lot of code for a simple task; a new ioctl request to take care 18357c478bd9Sstevel@tonic-gate * of this might be a useful RFE. 18367c478bd9Sstevel@tonic-gate */ 18377c478bd9Sstevel@tonic-gate 18387c478bd9Sstevel@tonic-gate static char * 18397c478bd9Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr) 18407c478bd9Sstevel@tonic-gate { 18417c478bd9Sstevel@tonic-gate static char answer[ZONENAME_MAX]; 18427c478bd9Sstevel@tonic-gate pid_t pid; 18437c478bd9Sstevel@tonic-gate int s, rlen, l, i; 18447c478bd9Sstevel@tonic-gate char *cp = rtmsg.space; 18457c478bd9Sstevel@tonic-gate struct sockaddr_dl *ifp = NULL; 18467c478bd9Sstevel@tonic-gate struct sockaddr *sa; 18477c478bd9Sstevel@tonic-gate char save_if_name[LIFNAMSIZ]; 18487c478bd9Sstevel@tonic-gate 18497c478bd9Sstevel@tonic-gate answer[0] = '\0'; 18507c478bd9Sstevel@tonic-gate 18517c478bd9Sstevel@tonic-gate pid = getpid(); 18527c478bd9Sstevel@tonic-gate if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) { 18537c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get routing socket"); 18547c478bd9Sstevel@tonic-gate return (NULL); 18557c478bd9Sstevel@tonic-gate } 18567c478bd9Sstevel@tonic-gate 18577c478bd9Sstevel@tonic-gate if (lifr->lifr_addr.ss_family == AF_INET) { 18587c478bd9Sstevel@tonic-gate struct sockaddr_in *sin4; 18597c478bd9Sstevel@tonic-gate 18607c478bd9Sstevel@tonic-gate so_dst.sa.sa_family = AF_INET; 18617c478bd9Sstevel@tonic-gate sin4 = (struct sockaddr_in *)&lifr->lifr_addr; 18627c478bd9Sstevel@tonic-gate so_dst.sin.sin_addr = sin4->sin_addr; 18637c478bd9Sstevel@tonic-gate } else { 18647c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 18657c478bd9Sstevel@tonic-gate 18667c478bd9Sstevel@tonic-gate so_dst.sa.sa_family = AF_INET6; 18677c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr; 18687c478bd9Sstevel@tonic-gate so_dst.sin6.sin6_addr = sin6->sin6_addr; 18697c478bd9Sstevel@tonic-gate } 18707c478bd9Sstevel@tonic-gate 18717c478bd9Sstevel@tonic-gate so_ifp.sa.sa_family = AF_LINK; 18727c478bd9Sstevel@tonic-gate 18737c478bd9Sstevel@tonic-gate (void) memset(&rtmsg, 0, sizeof (rtmsg)); 18747c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_type = RTM_GET; 18757c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST; 18767c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_version = RTM_VERSION; 18777c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_seq = ++rts_seqno; 18787c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST; 18797c478bd9Sstevel@tonic-gate 18807c478bd9Sstevel@tonic-gate l = ROUNDUP_LONG(salen(&so_dst.sa)); 18817c478bd9Sstevel@tonic-gate (void) memmove(cp, &(so_dst), l); 18827c478bd9Sstevel@tonic-gate cp += l; 18837c478bd9Sstevel@tonic-gate l = ROUNDUP_LONG(salen(&so_ifp.sa)); 18847c478bd9Sstevel@tonic-gate (void) memmove(cp, &(so_ifp), l); 18857c478bd9Sstevel@tonic-gate cp += l; 18867c478bd9Sstevel@tonic-gate 18877c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg; 18887c478bd9Sstevel@tonic-gate 18897c478bd9Sstevel@tonic-gate if ((rlen = write(s, &rtmsg, l)) < 0) { 18907c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "writing to routing socket"); 18917c478bd9Sstevel@tonic-gate return (NULL); 18927c478bd9Sstevel@tonic-gate } else if (rlen < (int)rtmsg.hdr.rtm_msglen) { 18937c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 18947c478bd9Sstevel@tonic-gate "write to routing socket got only %d for len\n", rlen); 18957c478bd9Sstevel@tonic-gate return (NULL); 18967c478bd9Sstevel@tonic-gate } 18977c478bd9Sstevel@tonic-gate do { 18987c478bd9Sstevel@tonic-gate l = read(s, &rtmsg, sizeof (rtmsg)); 18997c478bd9Sstevel@tonic-gate } while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno || 19007c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_pid != pid)); 19017c478bd9Sstevel@tonic-gate if (l < 0) { 19027c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "reading from routing socket"); 19037c478bd9Sstevel@tonic-gate return (NULL); 19047c478bd9Sstevel@tonic-gate } 19057c478bd9Sstevel@tonic-gate 19067c478bd9Sstevel@tonic-gate if (rtmsg.hdr.rtm_version != RTM_VERSION) { 19077c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 19087c478bd9Sstevel@tonic-gate "routing message version %d not understood", 19097c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_version); 19107c478bd9Sstevel@tonic-gate return (NULL); 19117c478bd9Sstevel@tonic-gate } 19127c478bd9Sstevel@tonic-gate if (rtmsg.hdr.rtm_msglen != (ushort_t)l) { 19137c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "message length mismatch, " 19147c478bd9Sstevel@tonic-gate "expected %d bytes, returned %d bytes", 19157c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_msglen, l); 19167c478bd9Sstevel@tonic-gate return (NULL); 19177c478bd9Sstevel@tonic-gate } 19187c478bd9Sstevel@tonic-gate if (rtmsg.hdr.rtm_errno != 0) { 19197c478bd9Sstevel@tonic-gate errno = rtmsg.hdr.rtm_errno; 19207c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "RTM_GET routing socket message"); 19217c478bd9Sstevel@tonic-gate return (NULL); 19227c478bd9Sstevel@tonic-gate } 19237c478bd9Sstevel@tonic-gate if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) { 19247c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "interface not found"); 19257c478bd9Sstevel@tonic-gate return (NULL); 19267c478bd9Sstevel@tonic-gate } 19277c478bd9Sstevel@tonic-gate cp = ((char *)(&rtmsg.hdr + 1)); 19287c478bd9Sstevel@tonic-gate for (i = 1; i != 0; i <<= 1) { 19297c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 19307c478bd9Sstevel@tonic-gate sa = (struct sockaddr *)cp; 19317c478bd9Sstevel@tonic-gate if (i != RTA_IFP) { 19327c478bd9Sstevel@tonic-gate if ((i & rtmsg.hdr.rtm_addrs) != 0) 19337c478bd9Sstevel@tonic-gate cp += ROUNDUP_LONG(salen(sa)); 19347c478bd9Sstevel@tonic-gate continue; 19357c478bd9Sstevel@tonic-gate } 19367c478bd9Sstevel@tonic-gate if (sa->sa_family == AF_LINK && 19377c478bd9Sstevel@tonic-gate ((struct sockaddr_dl *)sa)->sdl_nlen != 0) 19387c478bd9Sstevel@tonic-gate ifp = (struct sockaddr_dl *)sa; 19397c478bd9Sstevel@tonic-gate break; 19407c478bd9Sstevel@tonic-gate } 19417c478bd9Sstevel@tonic-gate if (ifp == NULL) { 19427c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "interface could not be determined"); 19437c478bd9Sstevel@tonic-gate return (NULL); 19447c478bd9Sstevel@tonic-gate } 19457c478bd9Sstevel@tonic-gate 19467c478bd9Sstevel@tonic-gate /* 19477c478bd9Sstevel@tonic-gate * We need to set the I/F name to what we got above, then do the 19487c478bd9Sstevel@tonic-gate * appropriate ioctl to get its zone name. But lifr->lifr_name is 19497c478bd9Sstevel@tonic-gate * used by the calling function to do a REMOVEIF, so if we leave the 19507c478bd9Sstevel@tonic-gate * "good" zone's I/F name in place, *that* I/F will be removed instead 19517c478bd9Sstevel@tonic-gate * of the bad one. So we save the old (bad) I/F name before over- 19527c478bd9Sstevel@tonic-gate * writing it and doing the ioctl, then restore it after the ioctl. 19537c478bd9Sstevel@tonic-gate */ 19547c478bd9Sstevel@tonic-gate (void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name)); 19557c478bd9Sstevel@tonic-gate (void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen); 19567c478bd9Sstevel@tonic-gate lifr->lifr_name[ifp->sdl_nlen] = '\0'; 19577c478bd9Sstevel@tonic-gate i = ioctl(s, SIOCGLIFZONE, lifr); 19587c478bd9Sstevel@tonic-gate (void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name)); 19597c478bd9Sstevel@tonic-gate if (i < 0) { 19607c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 19617c478bd9Sstevel@tonic-gate "%s: could not determine the zone interface belongs to", 19627c478bd9Sstevel@tonic-gate lifr->lifr_name); 19637c478bd9Sstevel@tonic-gate return (NULL); 19647c478bd9Sstevel@tonic-gate } 19657c478bd9Sstevel@tonic-gate if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0) 19667c478bd9Sstevel@tonic-gate (void) snprintf(answer, sizeof (answer), "%d", 19677c478bd9Sstevel@tonic-gate lifr->lifr_zoneid); 19687c478bd9Sstevel@tonic-gate 19697c478bd9Sstevel@tonic-gate if (strlen(answer) > 0) 19707c478bd9Sstevel@tonic-gate return (answer); 19717c478bd9Sstevel@tonic-gate return (NULL); 19727c478bd9Sstevel@tonic-gate } 19737c478bd9Sstevel@tonic-gate 19747c478bd9Sstevel@tonic-gate typedef struct mcast_rtmsg_s { 19757c478bd9Sstevel@tonic-gate struct rt_msghdr m_rtm; 19767c478bd9Sstevel@tonic-gate union { 19777c478bd9Sstevel@tonic-gate struct { 19787c478bd9Sstevel@tonic-gate struct sockaddr_in m_dst; 19797c478bd9Sstevel@tonic-gate struct sockaddr_in m_gw; 19807c478bd9Sstevel@tonic-gate struct sockaddr_in m_netmask; 19817c478bd9Sstevel@tonic-gate } m_v4; 19827c478bd9Sstevel@tonic-gate struct { 19837c478bd9Sstevel@tonic-gate struct sockaddr_in6 m_dst; 19847c478bd9Sstevel@tonic-gate struct sockaddr_in6 m_gw; 19857c478bd9Sstevel@tonic-gate struct sockaddr_in6 m_netmask; 19867c478bd9Sstevel@tonic-gate } m_v6; 19877c478bd9Sstevel@tonic-gate } m_u; 19887c478bd9Sstevel@tonic-gate } mcast_rtmsg_t; 19897c478bd9Sstevel@tonic-gate #define m_dst4 m_u.m_v4.m_dst 19907c478bd9Sstevel@tonic-gate #define m_dst6 m_u.m_v6.m_dst 19917c478bd9Sstevel@tonic-gate #define m_gw4 m_u.m_v4.m_gw 19927c478bd9Sstevel@tonic-gate #define m_gw6 m_u.m_v6.m_gw 19937c478bd9Sstevel@tonic-gate #define m_netmask4 m_u.m_v4.m_netmask 19947c478bd9Sstevel@tonic-gate #define m_netmask6 m_u.m_v6.m_netmask 19957c478bd9Sstevel@tonic-gate 19967c478bd9Sstevel@tonic-gate /* 19977c478bd9Sstevel@tonic-gate * Configures a single interface: a new virtual interface is added, based on 19987c478bd9Sstevel@tonic-gate * the physical interface nwiftabptr->zone_nwif_physical, with the address 19997c478bd9Sstevel@tonic-gate * specified in nwiftabptr->zone_nwif_address, for zone zone_id. Note that 20007c478bd9Sstevel@tonic-gate * the "address" can be an IPv6 address (with a /prefixlength required), an 20017c478bd9Sstevel@tonic-gate * IPv4 address (with a /prefixlength optional), or a name; for the latter, 20027c478bd9Sstevel@tonic-gate * an IPv4 name-to-address resolution will be attempted. 20037c478bd9Sstevel@tonic-gate * 20047c478bd9Sstevel@tonic-gate * A default interface route for multicast is created on the first IPv4 and 20057c478bd9Sstevel@tonic-gate * IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively. 20067c478bd9Sstevel@tonic-gate * This should really be done in the init scripts if we ever allow zones to 20077c478bd9Sstevel@tonic-gate * modify the routing tables. 20087c478bd9Sstevel@tonic-gate * 20097c478bd9Sstevel@tonic-gate * If anything goes wrong, we log an detailed error message, attempt to tear 20107c478bd9Sstevel@tonic-gate * down whatever we set up and return an error. 20117c478bd9Sstevel@tonic-gate */ 20127c478bd9Sstevel@tonic-gate static int 20137c478bd9Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id, 20147c478bd9Sstevel@tonic-gate struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp, 20157c478bd9Sstevel@tonic-gate boolean_t *mcast_rt_v6_setp) 20167c478bd9Sstevel@tonic-gate { 20177c478bd9Sstevel@tonic-gate struct lifreq lifr; 20187c478bd9Sstevel@tonic-gate struct sockaddr_in netmask4; 20197c478bd9Sstevel@tonic-gate struct sockaddr_in6 netmask6; 20207c478bd9Sstevel@tonic-gate struct in_addr in4; 20217c478bd9Sstevel@tonic-gate struct in6_addr in6; 20227c478bd9Sstevel@tonic-gate sa_family_t af; 20237c478bd9Sstevel@tonic-gate char *slashp = strchr(nwiftabptr->zone_nwif_address, '/'); 20247c478bd9Sstevel@tonic-gate mcast_rtmsg_t mcast_rtmsg; 20257c478bd9Sstevel@tonic-gate int s; 20267c478bd9Sstevel@tonic-gate int rs; 20277c478bd9Sstevel@tonic-gate int rlen; 20287c478bd9Sstevel@tonic-gate boolean_t got_netmask = B_FALSE; 20297c478bd9Sstevel@tonic-gate char addrstr4[INET_ADDRSTRLEN]; 20307c478bd9Sstevel@tonic-gate int res; 20317c478bd9Sstevel@tonic-gate 20327c478bd9Sstevel@tonic-gate res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr); 20337c478bd9Sstevel@tonic-gate if (res != Z_OK) { 20347c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res), 20357c478bd9Sstevel@tonic-gate nwiftabptr->zone_nwif_address); 20367c478bd9Sstevel@tonic-gate return (-1); 20377c478bd9Sstevel@tonic-gate } 20387c478bd9Sstevel@tonic-gate af = lifr.lifr_addr.ss_family; 20397c478bd9Sstevel@tonic-gate if (af == AF_INET) 20407c478bd9Sstevel@tonic-gate in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr; 20417c478bd9Sstevel@tonic-gate else 20427c478bd9Sstevel@tonic-gate in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr; 20437c478bd9Sstevel@tonic-gate 20447c478bd9Sstevel@tonic-gate if ((s = socket(af, SOCK_DGRAM, 0)) < 0) { 20457c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get socket"); 20467c478bd9Sstevel@tonic-gate return (-1); 20477c478bd9Sstevel@tonic-gate } 20487c478bd9Sstevel@tonic-gate 20497c478bd9Sstevel@tonic-gate (void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical, 20507c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_name)); 20517c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) { 205222321485Svp157776 /* 205322321485Svp157776 * Here, we know that the interface can't be brought up. 205422321485Svp157776 * A similar warning message was already printed out to 205522321485Svp157776 * the console by zoneadm(1M) so instead we log the 205622321485Svp157776 * message to syslog and continue. 205722321485Svp157776 */ 205822321485Svp157776 zerror(&logsys, B_TRUE, "WARNING: skipping interface " 205922321485Svp157776 "'%s' which may not be present/plumbed in the " 206022321485Svp157776 "global zone.", lifr.lifr_name); 20617c478bd9Sstevel@tonic-gate (void) close(s); 206222321485Svp157776 return (Z_OK); 20637c478bd9Sstevel@tonic-gate } 20647c478bd9Sstevel@tonic-gate 20657c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 20667c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 20677c478bd9Sstevel@tonic-gate "%s: could not set IP address to %s", 20687c478bd9Sstevel@tonic-gate lifr.lifr_name, nwiftabptr->zone_nwif_address); 20697c478bd9Sstevel@tonic-gate goto bad; 20707c478bd9Sstevel@tonic-gate } 20717c478bd9Sstevel@tonic-gate 20727c478bd9Sstevel@tonic-gate /* Preserve literal IPv4 address for later potential printing. */ 20737c478bd9Sstevel@tonic-gate if (af == AF_INET) 20747c478bd9Sstevel@tonic-gate (void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN); 20757c478bd9Sstevel@tonic-gate 20767c478bd9Sstevel@tonic-gate lifr.lifr_zoneid = zone_id; 20777c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) { 20787c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not place interface into zone", 20797c478bd9Sstevel@tonic-gate lifr.lifr_name); 20807c478bd9Sstevel@tonic-gate goto bad; 20817c478bd9Sstevel@tonic-gate } 20827c478bd9Sstevel@tonic-gate 20837c478bd9Sstevel@tonic-gate if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) { 20847c478bd9Sstevel@tonic-gate got_netmask = B_TRUE; /* default setting will be correct */ 20857c478bd9Sstevel@tonic-gate } else { 20867c478bd9Sstevel@tonic-gate if (af == AF_INET) { 20877c478bd9Sstevel@tonic-gate /* 20887c478bd9Sstevel@tonic-gate * The IPv4 netmask can be determined either 20897c478bd9Sstevel@tonic-gate * directly if a prefix length was supplied with 20907c478bd9Sstevel@tonic-gate * the address or via the netmasks database. Not 20917c478bd9Sstevel@tonic-gate * being able to determine it is a common failure, 20927c478bd9Sstevel@tonic-gate * but it often is not fatal to operation of the 20937c478bd9Sstevel@tonic-gate * interface. In that case, a warning will be 20947c478bd9Sstevel@tonic-gate * printed after the rest of the interface's 20957c478bd9Sstevel@tonic-gate * parameters have been configured. 20967c478bd9Sstevel@tonic-gate */ 20977c478bd9Sstevel@tonic-gate (void) memset(&netmask4, 0, sizeof (netmask4)); 20987c478bd9Sstevel@tonic-gate if (slashp != NULL) { 20997c478bd9Sstevel@tonic-gate if (addr2netmask(slashp + 1, V4_ADDR_LEN, 21007c478bd9Sstevel@tonic-gate (uchar_t *)&netmask4.sin_addr) != 0) { 21017c478bd9Sstevel@tonic-gate *slashp = '/'; 21027c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 21037c478bd9Sstevel@tonic-gate "%s: invalid prefix length in %s", 21047c478bd9Sstevel@tonic-gate lifr.lifr_name, 21057c478bd9Sstevel@tonic-gate nwiftabptr->zone_nwif_address); 21067c478bd9Sstevel@tonic-gate goto bad; 21077c478bd9Sstevel@tonic-gate } 21087c478bd9Sstevel@tonic-gate got_netmask = B_TRUE; 21097c478bd9Sstevel@tonic-gate } else if (getnetmaskbyaddr(in4, 21107c478bd9Sstevel@tonic-gate &netmask4.sin_addr) == 0) { 21117c478bd9Sstevel@tonic-gate got_netmask = B_TRUE; 21127c478bd9Sstevel@tonic-gate } 21137c478bd9Sstevel@tonic-gate if (got_netmask) { 21147c478bd9Sstevel@tonic-gate netmask4.sin_family = af; 21157c478bd9Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &netmask4, 21167c478bd9Sstevel@tonic-gate sizeof (netmask4)); 21177c478bd9Sstevel@tonic-gate } 21187c478bd9Sstevel@tonic-gate } else { 21197c478bd9Sstevel@tonic-gate (void) memset(&netmask6, 0, sizeof (netmask6)); 21207c478bd9Sstevel@tonic-gate if (addr2netmask(slashp + 1, V6_ADDR_LEN, 21217c478bd9Sstevel@tonic-gate (uchar_t *)&netmask6.sin6_addr) != 0) { 21227c478bd9Sstevel@tonic-gate *slashp = '/'; 21237c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 21247c478bd9Sstevel@tonic-gate "%s: invalid prefix length in %s", 21257c478bd9Sstevel@tonic-gate lifr.lifr_name, 21267c478bd9Sstevel@tonic-gate nwiftabptr->zone_nwif_address); 21277c478bd9Sstevel@tonic-gate goto bad; 21287c478bd9Sstevel@tonic-gate } 21297c478bd9Sstevel@tonic-gate got_netmask = B_TRUE; 21307c478bd9Sstevel@tonic-gate netmask6.sin6_family = af; 21317c478bd9Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &netmask6, 21327c478bd9Sstevel@tonic-gate sizeof (netmask6)); 21337c478bd9Sstevel@tonic-gate } 21347c478bd9Sstevel@tonic-gate if (got_netmask && 21357c478bd9Sstevel@tonic-gate ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) { 21367c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not set netmask", 21377c478bd9Sstevel@tonic-gate lifr.lifr_name); 21387c478bd9Sstevel@tonic-gate goto bad; 21397c478bd9Sstevel@tonic-gate } 21407c478bd9Sstevel@tonic-gate 21417c478bd9Sstevel@tonic-gate /* 21427c478bd9Sstevel@tonic-gate * This doesn't set the broadcast address at all. Rather, it 21437c478bd9Sstevel@tonic-gate * gets, then sets the interface's address, relying on the fact 21447c478bd9Sstevel@tonic-gate * that resetting the address will reset the broadcast address. 21457c478bd9Sstevel@tonic-gate */ 21467c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 21477c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get address", 21487c478bd9Sstevel@tonic-gate lifr.lifr_name); 21497c478bd9Sstevel@tonic-gate goto bad; 21507c478bd9Sstevel@tonic-gate } 21517c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 21527c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 21537c478bd9Sstevel@tonic-gate "%s: could not reset broadcast address", 21547c478bd9Sstevel@tonic-gate lifr.lifr_name); 21557c478bd9Sstevel@tonic-gate goto bad; 21567c478bd9Sstevel@tonic-gate } 21577c478bd9Sstevel@tonic-gate } 21587c478bd9Sstevel@tonic-gate 21597c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 21607c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get flags", 21617c478bd9Sstevel@tonic-gate lifr.lifr_name); 21627c478bd9Sstevel@tonic-gate goto bad; 21637c478bd9Sstevel@tonic-gate } 21647c478bd9Sstevel@tonic-gate lifr.lifr_flags |= IFF_UP; 21657c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) { 21667c478bd9Sstevel@tonic-gate int save_errno = errno; 21677c478bd9Sstevel@tonic-gate char *zone_using; 21687c478bd9Sstevel@tonic-gate 21697c478bd9Sstevel@tonic-gate /* 21707c478bd9Sstevel@tonic-gate * If we failed with something other than EADDRNOTAVAIL, 21717c478bd9Sstevel@tonic-gate * then skip to the end. Otherwise, look up our address, 21727c478bd9Sstevel@tonic-gate * then call a function to determine which zone is already 21737c478bd9Sstevel@tonic-gate * using that address. 21747c478bd9Sstevel@tonic-gate */ 21757c478bd9Sstevel@tonic-gate if (errno != EADDRNOTAVAIL) { 21767c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 21777c478bd9Sstevel@tonic-gate "%s: could not bring interface up", lifr.lifr_name); 21787c478bd9Sstevel@tonic-gate goto bad; 21797c478bd9Sstevel@tonic-gate } 21807c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 21817c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get address", 21827c478bd9Sstevel@tonic-gate lifr.lifr_name); 21837c478bd9Sstevel@tonic-gate goto bad; 21847c478bd9Sstevel@tonic-gate } 21857c478bd9Sstevel@tonic-gate zone_using = who_is_using(zlogp, &lifr); 21867c478bd9Sstevel@tonic-gate errno = save_errno; 21877c478bd9Sstevel@tonic-gate if (zone_using == NULL) 21887c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 21897c478bd9Sstevel@tonic-gate "%s: could not bring interface up", lifr.lifr_name); 21907c478bd9Sstevel@tonic-gate else 21917c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not bring interface " 21927c478bd9Sstevel@tonic-gate "up: address in use by zone '%s'", lifr.lifr_name, 21937c478bd9Sstevel@tonic-gate zone_using); 21947c478bd9Sstevel@tonic-gate goto bad; 21957c478bd9Sstevel@tonic-gate } 21967c478bd9Sstevel@tonic-gate if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET && 21977c478bd9Sstevel@tonic-gate mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) || 21987c478bd9Sstevel@tonic-gate (af == AF_INET6 && 21997c478bd9Sstevel@tonic-gate mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) { 22007c478bd9Sstevel@tonic-gate rs = socket(PF_ROUTE, SOCK_RAW, 0); 22017c478bd9Sstevel@tonic-gate if (rs < 0) { 22027c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not create " 22037c478bd9Sstevel@tonic-gate "routing socket", lifr.lifr_name); 22047c478bd9Sstevel@tonic-gate goto bad; 22057c478bd9Sstevel@tonic-gate } 22067c478bd9Sstevel@tonic-gate (void) shutdown(rs, 0); 22077c478bd9Sstevel@tonic-gate (void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t)); 22087c478bd9Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_msglen = sizeof (struct rt_msghdr) + 22097c478bd9Sstevel@tonic-gate 3 * (af == AF_INET ? sizeof (struct sockaddr_in) : 22107c478bd9Sstevel@tonic-gate sizeof (struct sockaddr_in6)); 22117c478bd9Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION; 22127c478bd9Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_type = RTM_ADD; 22137c478bd9Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_flags = RTF_UP; 22147c478bd9Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_addrs = 22157c478bd9Sstevel@tonic-gate RTA_DST | RTA_GATEWAY | RTA_NETMASK; 22167c478bd9Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno; 22177c478bd9Sstevel@tonic-gate if (af == AF_INET) { 22187c478bd9Sstevel@tonic-gate mcast_rtmsg.m_dst4.sin_family = AF_INET; 22197c478bd9Sstevel@tonic-gate mcast_rtmsg.m_dst4.sin_addr.s_addr = 22207c478bd9Sstevel@tonic-gate htonl(INADDR_UNSPEC_GROUP); 22217c478bd9Sstevel@tonic-gate mcast_rtmsg.m_gw4.sin_family = AF_INET; 22227c478bd9Sstevel@tonic-gate mcast_rtmsg.m_gw4.sin_addr = in4; 22237c478bd9Sstevel@tonic-gate mcast_rtmsg.m_netmask4.sin_family = AF_INET; 22247c478bd9Sstevel@tonic-gate mcast_rtmsg.m_netmask4.sin_addr.s_addr = 22257c478bd9Sstevel@tonic-gate htonl(IN_CLASSD_NET); 22267c478bd9Sstevel@tonic-gate } else { 22277c478bd9Sstevel@tonic-gate mcast_rtmsg.m_dst6.sin6_family = AF_INET6; 22287c478bd9Sstevel@tonic-gate mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU; 22297c478bd9Sstevel@tonic-gate mcast_rtmsg.m_gw6.sin6_family = AF_INET6; 22307c478bd9Sstevel@tonic-gate mcast_rtmsg.m_gw6.sin6_addr = in6; 22317c478bd9Sstevel@tonic-gate mcast_rtmsg.m_netmask6.sin6_family = AF_INET6; 22327c478bd9Sstevel@tonic-gate mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU; 22337c478bd9Sstevel@tonic-gate } 22347c478bd9Sstevel@tonic-gate rlen = write(rs, (char *)&mcast_rtmsg, 22357c478bd9Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_msglen); 223622321485Svp157776 /* 223722321485Svp157776 * The write to the multicast socket will fail if the 223822321485Svp157776 * interface belongs to a failed IPMP group. This is a 223922321485Svp157776 * non-fatal error and the zone will continue booting. 224022321485Svp157776 * While the zone is running, if any interface in the 224122321485Svp157776 * failed IPMP group recovers, the zone will fallback to 224222321485Svp157776 * using that interface. 224322321485Svp157776 */ 22447c478bd9Sstevel@tonic-gate if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) { 22457c478bd9Sstevel@tonic-gate if (rlen < 0) { 224622321485Svp157776 zerror(zlogp, B_TRUE, "WARNING: interface " 224722321485Svp157776 "'%s' not available as default for " 224822321485Svp157776 "multicast.", lifr.lifr_name); 22497c478bd9Sstevel@tonic-gate } else { 225022321485Svp157776 zerror(zlogp, B_FALSE, "WARNING: interface " 225122321485Svp157776 "'%s' not available as default for " 225222321485Svp157776 "multicast; routing socket returned " 225322321485Svp157776 "unexpected %d bytes.", 225422321485Svp157776 lifr.lifr_name, rlen); 22557c478bd9Sstevel@tonic-gate } 225622321485Svp157776 } else { 225722321485Svp157776 22587c478bd9Sstevel@tonic-gate if (af == AF_INET) { 22597c478bd9Sstevel@tonic-gate *mcast_rt_v4_setp = B_TRUE; 22607c478bd9Sstevel@tonic-gate } else { 22617c478bd9Sstevel@tonic-gate *mcast_rt_v6_setp = B_TRUE; 22627c478bd9Sstevel@tonic-gate } 226322321485Svp157776 } 22647c478bd9Sstevel@tonic-gate (void) close(rs); 22657c478bd9Sstevel@tonic-gate } 22667c478bd9Sstevel@tonic-gate 22677c478bd9Sstevel@tonic-gate if (!got_netmask) { 22687c478bd9Sstevel@tonic-gate /* 22697c478bd9Sstevel@tonic-gate * A common, but often non-fatal problem, is that the system 22707c478bd9Sstevel@tonic-gate * cannot find the netmask for an interface address. This is 22717c478bd9Sstevel@tonic-gate * often caused by it being only in /etc/inet/netmasks, but 22727c478bd9Sstevel@tonic-gate * /etc/nsswitch.conf says to use NIS or NIS+ and it's not 22737c478bd9Sstevel@tonic-gate * in that. This doesn't show up at boot because the netmask 22747c478bd9Sstevel@tonic-gate * is obtained from /etc/inet/netmasks when no network 22757c478bd9Sstevel@tonic-gate * interfaces are up, but isn't consulted when NIS/NIS+ is 22767c478bd9Sstevel@tonic-gate * available. We warn the user here that something like this 22777c478bd9Sstevel@tonic-gate * has happened and we're just running with a default and 22787c478bd9Sstevel@tonic-gate * possible incorrect netmask. 22797c478bd9Sstevel@tonic-gate */ 22807c478bd9Sstevel@tonic-gate char buffer[INET6_ADDRSTRLEN]; 22817c478bd9Sstevel@tonic-gate void *addr; 22827c478bd9Sstevel@tonic-gate 22837c478bd9Sstevel@tonic-gate if (af == AF_INET) 22847c478bd9Sstevel@tonic-gate addr = &((struct sockaddr_in *) 22857c478bd9Sstevel@tonic-gate (&lifr.lifr_addr))->sin_addr; 22867c478bd9Sstevel@tonic-gate else 22877c478bd9Sstevel@tonic-gate addr = &((struct sockaddr_in6 *) 22887c478bd9Sstevel@tonic-gate (&lifr.lifr_addr))->sin6_addr; 22897c478bd9Sstevel@tonic-gate 22907c478bd9Sstevel@tonic-gate /* Find out what netmask interface is going to be using */ 22917c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 || 22927c478bd9Sstevel@tonic-gate inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) 22937c478bd9Sstevel@tonic-gate goto bad; 22947c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 22957c478bd9Sstevel@tonic-gate "WARNING: %s: no matching subnet found in netmasks(4) for " 22967c478bd9Sstevel@tonic-gate "%s; using default of %s.", 22977c478bd9Sstevel@tonic-gate lifr.lifr_name, addrstr4, buffer); 22987c478bd9Sstevel@tonic-gate } 22997c478bd9Sstevel@tonic-gate 23007c478bd9Sstevel@tonic-gate (void) close(s); 23017c478bd9Sstevel@tonic-gate return (Z_OK); 23027c478bd9Sstevel@tonic-gate bad: 23037c478bd9Sstevel@tonic-gate (void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr); 23047c478bd9Sstevel@tonic-gate (void) close(s); 23057c478bd9Sstevel@tonic-gate return (-1); 23067c478bd9Sstevel@tonic-gate } 23077c478bd9Sstevel@tonic-gate 23087c478bd9Sstevel@tonic-gate /* 23097c478bd9Sstevel@tonic-gate * Sets up network interfaces based on information from the zone configuration. 23107c478bd9Sstevel@tonic-gate * An IPv4 loopback interface is set up "for free", modeling the global system. 23117c478bd9Sstevel@tonic-gate * If any of the configuration interfaces were IPv6, then an IPv6 loopback 23127c478bd9Sstevel@tonic-gate * address is set up as well. 23137c478bd9Sstevel@tonic-gate * 23147c478bd9Sstevel@tonic-gate * If anything goes wrong, we log a general error message, attempt to tear down 23157c478bd9Sstevel@tonic-gate * whatever we set up, and return an error. 23167c478bd9Sstevel@tonic-gate */ 23177c478bd9Sstevel@tonic-gate static int 23187c478bd9Sstevel@tonic-gate configure_network_interfaces(zlog_t *zlogp) 23197c478bd9Sstevel@tonic-gate { 23207c478bd9Sstevel@tonic-gate zone_dochandle_t handle; 23217c478bd9Sstevel@tonic-gate struct zone_nwiftab nwiftab, loopback_iftab; 23227c478bd9Sstevel@tonic-gate boolean_t saw_v6 = B_FALSE; 23237c478bd9Sstevel@tonic-gate boolean_t mcast_rt_v4_set = B_FALSE; 23247c478bd9Sstevel@tonic-gate boolean_t mcast_rt_v6_set = B_FALSE; 23257c478bd9Sstevel@tonic-gate zoneid_t zoneid; 23267c478bd9Sstevel@tonic-gate 23277c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) { 23287c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid"); 23297c478bd9Sstevel@tonic-gate return (-1); 23307c478bd9Sstevel@tonic-gate } 23317c478bd9Sstevel@tonic-gate 23327c478bd9Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 23337c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "getting zone configuration handle"); 23347c478bd9Sstevel@tonic-gate return (-1); 23357c478bd9Sstevel@tonic-gate } 23367c478bd9Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 23377c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration"); 23387c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 23397c478bd9Sstevel@tonic-gate return (-1); 23407c478bd9Sstevel@tonic-gate } 23417c478bd9Sstevel@tonic-gate if (zonecfg_setnwifent(handle) == Z_OK) { 23427c478bd9Sstevel@tonic-gate for (;;) { 23437c478bd9Sstevel@tonic-gate struct in6_addr in6; 23447c478bd9Sstevel@tonic-gate 23457c478bd9Sstevel@tonic-gate if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK) 23467c478bd9Sstevel@tonic-gate break; 23477c478bd9Sstevel@tonic-gate if (configure_one_interface(zlogp, zoneid, 23487c478bd9Sstevel@tonic-gate &nwiftab, &mcast_rt_v4_set, &mcast_rt_v6_set) != 23497c478bd9Sstevel@tonic-gate Z_OK) { 23507c478bd9Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 23517c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 23527c478bd9Sstevel@tonic-gate return (-1); 23537c478bd9Sstevel@tonic-gate } 23547c478bd9Sstevel@tonic-gate if (inet_pton(AF_INET6, nwiftab.zone_nwif_address, 23557c478bd9Sstevel@tonic-gate &in6) == 1) 23567c478bd9Sstevel@tonic-gate saw_v6 = B_TRUE; 23577c478bd9Sstevel@tonic-gate } 23587c478bd9Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 23597c478bd9Sstevel@tonic-gate } 23607c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 23617c478bd9Sstevel@tonic-gate (void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0", 23627c478bd9Sstevel@tonic-gate sizeof (loopback_iftab.zone_nwif_physical)); 23637c478bd9Sstevel@tonic-gate (void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1", 23647c478bd9Sstevel@tonic-gate sizeof (loopback_iftab.zone_nwif_address)); 23657c478bd9Sstevel@tonic-gate if (configure_one_interface(zlogp, zoneid, &loopback_iftab, NULL, NULL) 23667c478bd9Sstevel@tonic-gate != Z_OK) { 23677c478bd9Sstevel@tonic-gate return (-1); 23687c478bd9Sstevel@tonic-gate } 23697c478bd9Sstevel@tonic-gate if (saw_v6) { 23707c478bd9Sstevel@tonic-gate (void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128", 23717c478bd9Sstevel@tonic-gate sizeof (loopback_iftab.zone_nwif_address)); 23727c478bd9Sstevel@tonic-gate if (configure_one_interface(zlogp, zoneid, 23737c478bd9Sstevel@tonic-gate &loopback_iftab, NULL, NULL) != Z_OK) { 23747c478bd9Sstevel@tonic-gate return (-1); 23757c478bd9Sstevel@tonic-gate } 23767c478bd9Sstevel@tonic-gate } 23777c478bd9Sstevel@tonic-gate return (0); 23787c478bd9Sstevel@tonic-gate } 23797c478bd9Sstevel@tonic-gate 23807c478bd9Sstevel@tonic-gate static int 23817c478bd9Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid, 23827c478bd9Sstevel@tonic-gate const struct sockaddr_storage *local, const struct sockaddr_storage *remote) 23837c478bd9Sstevel@tonic-gate { 23847c478bd9Sstevel@tonic-gate int fd; 23857c478bd9Sstevel@tonic-gate struct strioctl ioc; 23867c478bd9Sstevel@tonic-gate tcp_ioc_abort_conn_t conn; 23877c478bd9Sstevel@tonic-gate int error; 23887c478bd9Sstevel@tonic-gate 23897c478bd9Sstevel@tonic-gate conn.ac_local = *local; 23907c478bd9Sstevel@tonic-gate conn.ac_remote = *remote; 23917c478bd9Sstevel@tonic-gate conn.ac_start = TCPS_SYN_SENT; 23927c478bd9Sstevel@tonic-gate conn.ac_end = TCPS_TIME_WAIT; 23937c478bd9Sstevel@tonic-gate conn.ac_zoneid = zoneid; 23947c478bd9Sstevel@tonic-gate 23957c478bd9Sstevel@tonic-gate ioc.ic_cmd = TCP_IOC_ABORT_CONN; 23967c478bd9Sstevel@tonic-gate ioc.ic_timout = -1; /* infinite timeout */ 23977c478bd9Sstevel@tonic-gate ioc.ic_len = sizeof (conn); 23987c478bd9Sstevel@tonic-gate ioc.ic_dp = (char *)&conn; 23997c478bd9Sstevel@tonic-gate 24007c478bd9Sstevel@tonic-gate if ((fd = open("/dev/tcp", O_RDONLY)) < 0) { 24017c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp"); 24027c478bd9Sstevel@tonic-gate return (-1); 24037c478bd9Sstevel@tonic-gate } 24047c478bd9Sstevel@tonic-gate 24057c478bd9Sstevel@tonic-gate error = ioctl(fd, I_STR, &ioc); 24067c478bd9Sstevel@tonic-gate (void) close(fd); 24077c478bd9Sstevel@tonic-gate if (error == 0 || errno == ENOENT) /* ENOENT is not an error */ 24087c478bd9Sstevel@tonic-gate return (0); 24097c478bd9Sstevel@tonic-gate return (-1); 24107c478bd9Sstevel@tonic-gate } 24117c478bd9Sstevel@tonic-gate 24127c478bd9Sstevel@tonic-gate static int 24137c478bd9Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid) 24147c478bd9Sstevel@tonic-gate { 24157c478bd9Sstevel@tonic-gate struct sockaddr_storage l, r; 24167c478bd9Sstevel@tonic-gate struct sockaddr_in *local, *remote; 24177c478bd9Sstevel@tonic-gate struct sockaddr_in6 *local6, *remote6; 24187c478bd9Sstevel@tonic-gate int error; 24197c478bd9Sstevel@tonic-gate 24207c478bd9Sstevel@tonic-gate /* 24217c478bd9Sstevel@tonic-gate * Abort IPv4 connections. 24227c478bd9Sstevel@tonic-gate */ 24237c478bd9Sstevel@tonic-gate bzero(&l, sizeof (*local)); 24247c478bd9Sstevel@tonic-gate local = (struct sockaddr_in *)&l; 24257c478bd9Sstevel@tonic-gate local->sin_family = AF_INET; 24267c478bd9Sstevel@tonic-gate local->sin_addr.s_addr = INADDR_ANY; 24277c478bd9Sstevel@tonic-gate local->sin_port = 0; 24287c478bd9Sstevel@tonic-gate 24297c478bd9Sstevel@tonic-gate bzero(&r, sizeof (*remote)); 24307c478bd9Sstevel@tonic-gate remote = (struct sockaddr_in *)&r; 24317c478bd9Sstevel@tonic-gate remote->sin_family = AF_INET; 24327c478bd9Sstevel@tonic-gate remote->sin_addr.s_addr = INADDR_ANY; 24337c478bd9Sstevel@tonic-gate remote->sin_port = 0; 24347c478bd9Sstevel@tonic-gate 24357c478bd9Sstevel@tonic-gate if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0) 24367c478bd9Sstevel@tonic-gate return (error); 24377c478bd9Sstevel@tonic-gate 24387c478bd9Sstevel@tonic-gate /* 24397c478bd9Sstevel@tonic-gate * Abort IPv6 connections. 24407c478bd9Sstevel@tonic-gate */ 24417c478bd9Sstevel@tonic-gate bzero(&l, sizeof (*local6)); 24427c478bd9Sstevel@tonic-gate local6 = (struct sockaddr_in6 *)&l; 24437c478bd9Sstevel@tonic-gate local6->sin6_family = AF_INET6; 24447c478bd9Sstevel@tonic-gate local6->sin6_port = 0; 24457c478bd9Sstevel@tonic-gate local6->sin6_addr = in6addr_any; 24467c478bd9Sstevel@tonic-gate 24477c478bd9Sstevel@tonic-gate bzero(&r, sizeof (*remote6)); 24487c478bd9Sstevel@tonic-gate remote6 = (struct sockaddr_in6 *)&r; 24497c478bd9Sstevel@tonic-gate remote6->sin6_family = AF_INET6; 24507c478bd9Sstevel@tonic-gate remote6->sin6_port = 0; 24517c478bd9Sstevel@tonic-gate remote6->sin6_addr = in6addr_any; 24527c478bd9Sstevel@tonic-gate 24537c478bd9Sstevel@tonic-gate if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0) 24547c478bd9Sstevel@tonic-gate return (error); 24557c478bd9Sstevel@tonic-gate return (0); 24567c478bd9Sstevel@tonic-gate } 24577c478bd9Sstevel@tonic-gate 24587c478bd9Sstevel@tonic-gate static int 2459ffbafc53Scomay get_privset(zlog_t *zlogp, priv_set_t *privs, boolean_t mount_cmd) 2460ffbafc53Scomay { 2461ffbafc53Scomay int error = -1; 2462ffbafc53Scomay zone_dochandle_t handle; 2463ffbafc53Scomay char *privname = NULL; 2464ffbafc53Scomay 2465ffbafc53Scomay if ((handle = zonecfg_init_handle()) == NULL) { 2466ffbafc53Scomay zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2467ffbafc53Scomay return (-1); 2468ffbafc53Scomay } 2469ffbafc53Scomay if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2470ffbafc53Scomay zerror(zlogp, B_FALSE, "invalid configuration"); 2471ffbafc53Scomay zonecfg_fini_handle(handle); 2472ffbafc53Scomay return (-1); 2473ffbafc53Scomay } 2474ffbafc53Scomay 24759acbbeafSnn35248 if (mount_cmd) { 24769acbbeafSnn35248 if (zonecfg_default_privset(privs) == Z_OK) 24779acbbeafSnn35248 return (0); 24789acbbeafSnn35248 zerror(zlogp, B_FALSE, 24799acbbeafSnn35248 "failed to determine the zone's default privilege set"); 24809acbbeafSnn35248 zonecfg_fini_handle(handle); 24819acbbeafSnn35248 return (-1); 24829acbbeafSnn35248 } 24839acbbeafSnn35248 2484ffbafc53Scomay switch (zonecfg_get_privset(handle, privs, &privname)) { 2485ffbafc53Scomay case Z_OK: 2486ffbafc53Scomay error = 0; 2487ffbafc53Scomay break; 2488ffbafc53Scomay case Z_PRIV_PROHIBITED: 2489ffbafc53Scomay zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted " 2490ffbafc53Scomay "within the zone's privilege set", privname); 2491ffbafc53Scomay break; 2492ffbafc53Scomay case Z_PRIV_REQUIRED: 2493ffbafc53Scomay zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing " 2494ffbafc53Scomay "from the zone's privilege set", privname); 2495ffbafc53Scomay break; 2496ffbafc53Scomay case Z_PRIV_UNKNOWN: 2497ffbafc53Scomay zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified " 2498ffbafc53Scomay "in the zone's privilege set", privname); 2499ffbafc53Scomay break; 2500ffbafc53Scomay default: 2501ffbafc53Scomay zerror(zlogp, B_FALSE, "failed to determine the zone's " 2502ffbafc53Scomay "privilege set"); 2503ffbafc53Scomay break; 2504ffbafc53Scomay } 2505ffbafc53Scomay 2506ffbafc53Scomay free(privname); 2507ffbafc53Scomay zonecfg_fini_handle(handle); 2508ffbafc53Scomay return (error); 2509ffbafc53Scomay } 2510ffbafc53Scomay 2511ffbafc53Scomay static int 25127c478bd9Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep) 25137c478bd9Sstevel@tonic-gate { 25147c478bd9Sstevel@tonic-gate nvlist_t *nvl = NULL; 25157c478bd9Sstevel@tonic-gate char *nvl_packed = NULL; 25167c478bd9Sstevel@tonic-gate size_t nvl_size = 0; 25177c478bd9Sstevel@tonic-gate nvlist_t **nvlv = NULL; 25187c478bd9Sstevel@tonic-gate int rctlcount = 0; 25197c478bd9Sstevel@tonic-gate int error = -1; 25207c478bd9Sstevel@tonic-gate zone_dochandle_t handle; 25217c478bd9Sstevel@tonic-gate struct zone_rctltab rctltab; 25227c478bd9Sstevel@tonic-gate rctlblk_t *rctlblk = NULL; 25237c478bd9Sstevel@tonic-gate 25247c478bd9Sstevel@tonic-gate *bufp = NULL; 25257c478bd9Sstevel@tonic-gate *bufsizep = 0; 25267c478bd9Sstevel@tonic-gate 25277c478bd9Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 25287c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "getting zone configuration handle"); 25297c478bd9Sstevel@tonic-gate return (-1); 25307c478bd9Sstevel@tonic-gate } 25317c478bd9Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 25327c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration"); 25337c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 25347c478bd9Sstevel@tonic-gate return (-1); 25357c478bd9Sstevel@tonic-gate } 25367c478bd9Sstevel@tonic-gate 25377c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 25387c478bd9Sstevel@tonic-gate if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 25397c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc"); 25407c478bd9Sstevel@tonic-gate goto out; 25417c478bd9Sstevel@tonic-gate } 25427c478bd9Sstevel@tonic-gate 25437c478bd9Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 25447c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent"); 25457c478bd9Sstevel@tonic-gate goto out; 25467c478bd9Sstevel@tonic-gate } 25477c478bd9Sstevel@tonic-gate 25487c478bd9Sstevel@tonic-gate if ((rctlblk = malloc(rctlblk_size())) == NULL) { 25497c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 25507c478bd9Sstevel@tonic-gate goto out; 25517c478bd9Sstevel@tonic-gate } 25527c478bd9Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 25537c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 25547c478bd9Sstevel@tonic-gate uint_t i, count; 25557c478bd9Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 25567c478bd9Sstevel@tonic-gate 25577c478bd9Sstevel@tonic-gate /* zoneadm should have already warned about unknown rctls. */ 25587c478bd9Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 25597c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 25607c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 25617c478bd9Sstevel@tonic-gate continue; 25627c478bd9Sstevel@tonic-gate } 25637c478bd9Sstevel@tonic-gate count = 0; 25647c478bd9Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 25657c478bd9Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 25667c478bd9Sstevel@tonic-gate count++; 25677c478bd9Sstevel@tonic-gate } 25687c478bd9Sstevel@tonic-gate if (count == 0) { /* ignore */ 25697c478bd9Sstevel@tonic-gate continue; /* Nothing to free */ 25707c478bd9Sstevel@tonic-gate } 25717c478bd9Sstevel@tonic-gate if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL) 25727c478bd9Sstevel@tonic-gate goto out; 25737c478bd9Sstevel@tonic-gate i = 0; 25747c478bd9Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 25757c478bd9Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next, i++) { 25767c478bd9Sstevel@tonic-gate if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) { 25777c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", 25787c478bd9Sstevel@tonic-gate "nvlist_alloc"); 25797c478bd9Sstevel@tonic-gate goto out; 25807c478bd9Sstevel@tonic-gate } 25817c478bd9Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 25827c478bd9Sstevel@tonic-gate != Z_OK) { 25837c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid rctl value: " 25847c478bd9Sstevel@tonic-gate "(priv=%s,limit=%s,action=%s)", 25857c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 25867c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 25877c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action); 25887c478bd9Sstevel@tonic-gate goto out; 25897c478bd9Sstevel@tonic-gate } 25907c478bd9Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 25917c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 25927c478bd9Sstevel@tonic-gate "(priv=%s,limit=%s,action=%s) is not a " 25937c478bd9Sstevel@tonic-gate "valid value for rctl '%s'", 25947c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 25957c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 25967c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action, 25977c478bd9Sstevel@tonic-gate name); 25987c478bd9Sstevel@tonic-gate goto out; 25997c478bd9Sstevel@tonic-gate } 26007c478bd9Sstevel@tonic-gate if (nvlist_add_uint64(nvlv[i], "privilege", 26017c478bd9Sstevel@tonic-gate rctlblk_get_privilege(rctlblk)) != 0) { 26027c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", 26037c478bd9Sstevel@tonic-gate "nvlist_add_uint64"); 26047c478bd9Sstevel@tonic-gate goto out; 26057c478bd9Sstevel@tonic-gate } 26067c478bd9Sstevel@tonic-gate if (nvlist_add_uint64(nvlv[i], "limit", 26077c478bd9Sstevel@tonic-gate rctlblk_get_value(rctlblk)) != 0) { 26087c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", 26097c478bd9Sstevel@tonic-gate "nvlist_add_uint64"); 26107c478bd9Sstevel@tonic-gate goto out; 26117c478bd9Sstevel@tonic-gate } 26127c478bd9Sstevel@tonic-gate if (nvlist_add_uint64(nvlv[i], "action", 26137c478bd9Sstevel@tonic-gate (uint_t)rctlblk_get_local_action(rctlblk, NULL)) 26147c478bd9Sstevel@tonic-gate != 0) { 26157c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", 26167c478bd9Sstevel@tonic-gate "nvlist_add_uint64"); 26177c478bd9Sstevel@tonic-gate goto out; 26187c478bd9Sstevel@tonic-gate } 26197c478bd9Sstevel@tonic-gate } 26207c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 26217c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 26227c478bd9Sstevel@tonic-gate if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count) 26237c478bd9Sstevel@tonic-gate != 0) { 26247c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", 26257c478bd9Sstevel@tonic-gate "nvlist_add_nvlist_array"); 26267c478bd9Sstevel@tonic-gate goto out; 26277c478bd9Sstevel@tonic-gate } 26287c478bd9Sstevel@tonic-gate for (i = 0; i < count; i++) 26297c478bd9Sstevel@tonic-gate nvlist_free(nvlv[i]); 26307c478bd9Sstevel@tonic-gate free(nvlv); 26317c478bd9Sstevel@tonic-gate nvlv = NULL; 26327c478bd9Sstevel@tonic-gate rctlcount++; 26337c478bd9Sstevel@tonic-gate } 26347c478bd9Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 26357c478bd9Sstevel@tonic-gate 26367c478bd9Sstevel@tonic-gate if (rctlcount == 0) { 26377c478bd9Sstevel@tonic-gate error = 0; 26387c478bd9Sstevel@tonic-gate goto out; 26397c478bd9Sstevel@tonic-gate } 26407c478bd9Sstevel@tonic-gate if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0) 26417c478bd9Sstevel@tonic-gate != 0) { 26427c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack"); 26437c478bd9Sstevel@tonic-gate goto out; 26447c478bd9Sstevel@tonic-gate } 26457c478bd9Sstevel@tonic-gate 26467c478bd9Sstevel@tonic-gate error = 0; 26477c478bd9Sstevel@tonic-gate *bufp = nvl_packed; 26487c478bd9Sstevel@tonic-gate *bufsizep = nvl_size; 26497c478bd9Sstevel@tonic-gate 26507c478bd9Sstevel@tonic-gate out: 26517c478bd9Sstevel@tonic-gate free(rctlblk); 26527c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 26537c478bd9Sstevel@tonic-gate if (error && nvl_packed != NULL) 26547c478bd9Sstevel@tonic-gate free(nvl_packed); 26557c478bd9Sstevel@tonic-gate if (nvl != NULL) 26567c478bd9Sstevel@tonic-gate nvlist_free(nvl); 26577c478bd9Sstevel@tonic-gate if (nvlv != NULL) 26587c478bd9Sstevel@tonic-gate free(nvlv); 26597c478bd9Sstevel@tonic-gate if (handle != NULL) 26607c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 26617c478bd9Sstevel@tonic-gate return (error); 26627c478bd9Sstevel@tonic-gate } 26637c478bd9Sstevel@tonic-gate 26647c478bd9Sstevel@tonic-gate static int 2665fa9e4066Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep) 2666fa9e4066Sahrens { 2667fa9e4066Sahrens zone_dochandle_t handle; 2668fa9e4066Sahrens struct zone_dstab dstab; 2669fa9e4066Sahrens size_t total, offset, len; 2670fa9e4066Sahrens int error = -1; 2671fa9e4066Sahrens char *str; 2672fa9e4066Sahrens 2673fa9e4066Sahrens *bufp = NULL; 2674fa9e4066Sahrens *bufsizep = 0; 2675fa9e4066Sahrens 2676fa9e4066Sahrens if ((handle = zonecfg_init_handle()) == NULL) { 2677fa9e4066Sahrens zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2678fa9e4066Sahrens return (-1); 2679fa9e4066Sahrens } 2680fa9e4066Sahrens if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2681fa9e4066Sahrens zerror(zlogp, B_FALSE, "invalid configuration"); 2682fa9e4066Sahrens zonecfg_fini_handle(handle); 2683fa9e4066Sahrens return (-1); 2684fa9e4066Sahrens } 2685fa9e4066Sahrens 2686fa9e4066Sahrens if (zonecfg_setdsent(handle) != Z_OK) { 2687fa9e4066Sahrens zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent"); 2688fa9e4066Sahrens goto out; 2689fa9e4066Sahrens } 2690fa9e4066Sahrens 2691fa9e4066Sahrens total = 0; 2692fa9e4066Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) 2693fa9e4066Sahrens total += strlen(dstab.zone_dataset_name) + 1; 2694fa9e4066Sahrens (void) zonecfg_enddsent(handle); 2695fa9e4066Sahrens 2696fa9e4066Sahrens if (total == 0) { 2697fa9e4066Sahrens error = 0; 2698fa9e4066Sahrens goto out; 2699fa9e4066Sahrens } 2700fa9e4066Sahrens 2701fa9e4066Sahrens if ((str = malloc(total)) == NULL) { 2702fa9e4066Sahrens zerror(zlogp, B_TRUE, "memory allocation failed"); 2703fa9e4066Sahrens goto out; 2704fa9e4066Sahrens } 2705fa9e4066Sahrens 2706fa9e4066Sahrens if (zonecfg_setdsent(handle) != Z_OK) { 2707fa9e4066Sahrens zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent"); 2708fa9e4066Sahrens goto out; 2709fa9e4066Sahrens } 2710fa9e4066Sahrens offset = 0; 2711fa9e4066Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 2712fa9e4066Sahrens len = strlen(dstab.zone_dataset_name); 2713fa9e4066Sahrens (void) strlcpy(str + offset, dstab.zone_dataset_name, 2714fa9e4066Sahrens sizeof (dstab.zone_dataset_name) - offset); 2715fa9e4066Sahrens offset += len; 2716fa9e4066Sahrens if (offset != total - 1) 2717fa9e4066Sahrens str[offset++] = ','; 2718fa9e4066Sahrens } 2719fa9e4066Sahrens (void) zonecfg_enddsent(handle); 2720fa9e4066Sahrens 2721fa9e4066Sahrens error = 0; 2722fa9e4066Sahrens *bufp = str; 2723fa9e4066Sahrens *bufsizep = total; 2724fa9e4066Sahrens 2725fa9e4066Sahrens out: 2726fa9e4066Sahrens if (error != 0 && str != NULL) 2727fa9e4066Sahrens free(str); 2728fa9e4066Sahrens if (handle != NULL) 2729fa9e4066Sahrens zonecfg_fini_handle(handle); 2730fa9e4066Sahrens 2731fa9e4066Sahrens return (error); 2732fa9e4066Sahrens } 2733fa9e4066Sahrens 2734fa9e4066Sahrens static int 2735fa9e4066Sahrens validate_datasets(zlog_t *zlogp) 2736fa9e4066Sahrens { 2737fa9e4066Sahrens zone_dochandle_t handle; 2738fa9e4066Sahrens struct zone_dstab dstab; 2739fa9e4066Sahrens zfs_handle_t *zhp; 274099653d4eSeschrock libzfs_handle_t *hdl; 2741fa9e4066Sahrens 2742fa9e4066Sahrens if ((handle = zonecfg_init_handle()) == NULL) { 2743fa9e4066Sahrens zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2744fa9e4066Sahrens return (-1); 2745fa9e4066Sahrens } 2746fa9e4066Sahrens if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2747fa9e4066Sahrens zerror(zlogp, B_FALSE, "invalid configuration"); 2748fa9e4066Sahrens zonecfg_fini_handle(handle); 2749fa9e4066Sahrens return (-1); 2750fa9e4066Sahrens } 2751fa9e4066Sahrens 2752fa9e4066Sahrens if (zonecfg_setdsent(handle) != Z_OK) { 2753fa9e4066Sahrens zerror(zlogp, B_FALSE, "invalid configuration"); 2754fa9e4066Sahrens zonecfg_fini_handle(handle); 2755fa9e4066Sahrens return (-1); 2756fa9e4066Sahrens } 2757fa9e4066Sahrens 275899653d4eSeschrock if ((hdl = libzfs_init()) == NULL) { 275999653d4eSeschrock zerror(zlogp, B_FALSE, "opening ZFS library"); 276099653d4eSeschrock zonecfg_fini_handle(handle); 276199653d4eSeschrock return (-1); 276299653d4eSeschrock } 2763fa9e4066Sahrens 2764fa9e4066Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 2765fa9e4066Sahrens 276699653d4eSeschrock if ((zhp = zfs_open(hdl, dstab.zone_dataset_name, 2767fa9e4066Sahrens ZFS_TYPE_FILESYSTEM)) == NULL) { 2768fa9e4066Sahrens zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'", 2769fa9e4066Sahrens dstab.zone_dataset_name); 2770fa9e4066Sahrens zonecfg_fini_handle(handle); 277199653d4eSeschrock libzfs_fini(hdl); 2772fa9e4066Sahrens return (-1); 2773fa9e4066Sahrens } 2774fa9e4066Sahrens 2775fa9e4066Sahrens /* 2776fa9e4066Sahrens * Automatically set the 'zoned' property. We check the value 2777fa9e4066Sahrens * first because we'll get EPERM if it is already set. 2778fa9e4066Sahrens */ 2779fa9e4066Sahrens if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 2780e9dbad6fSeschrock zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED), 2781e9dbad6fSeschrock "on") != 0) { 2782fa9e4066Sahrens zerror(zlogp, B_FALSE, "cannot set 'zoned' " 2783fa9e4066Sahrens "property for ZFS dataset '%s'\n", 2784fa9e4066Sahrens dstab.zone_dataset_name); 2785fa9e4066Sahrens zonecfg_fini_handle(handle); 2786fa9e4066Sahrens zfs_close(zhp); 278799653d4eSeschrock libzfs_fini(hdl); 2788fa9e4066Sahrens return (-1); 2789fa9e4066Sahrens } 2790fa9e4066Sahrens 2791fa9e4066Sahrens zfs_close(zhp); 2792fa9e4066Sahrens } 2793fa9e4066Sahrens (void) zonecfg_enddsent(handle); 2794fa9e4066Sahrens 2795fa9e4066Sahrens zonecfg_fini_handle(handle); 279699653d4eSeschrock libzfs_fini(hdl); 2797fa9e4066Sahrens 2798fa9e4066Sahrens return (0); 2799fa9e4066Sahrens } 2800fa9e4066Sahrens 280145916cd2Sjpk /* 280245916cd2Sjpk * Mount lower level home directories into/from current zone 280345916cd2Sjpk * Share exported directories specified in dfstab for zone 280445916cd2Sjpk */ 280545916cd2Sjpk static int 280645916cd2Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath) 280745916cd2Sjpk { 280845916cd2Sjpk zoneid_t *zids = NULL; 280945916cd2Sjpk priv_set_t *zid_privs; 281045916cd2Sjpk const priv_impl_info_t *ip = NULL; 281145916cd2Sjpk uint_t nzents_saved; 281245916cd2Sjpk uint_t nzents; 281345916cd2Sjpk int i; 281445916cd2Sjpk char readonly[] = "ro"; 281545916cd2Sjpk struct zone_fstab lower_fstab; 281645916cd2Sjpk char *argv[4]; 281745916cd2Sjpk 281845916cd2Sjpk if (!is_system_labeled()) 281945916cd2Sjpk return (0); 282045916cd2Sjpk 282145916cd2Sjpk if (zid_label == NULL) { 282245916cd2Sjpk zid_label = m_label_alloc(MAC_LABEL); 282345916cd2Sjpk if (zid_label == NULL) 282445916cd2Sjpk return (-1); 282545916cd2Sjpk } 282645916cd2Sjpk 282745916cd2Sjpk /* Make sure our zone has an /export/home dir */ 282845916cd2Sjpk (void) make_one_dir(zlogp, rootpath, "/export/home", 282945916cd2Sjpk DEFAULT_DIR_MODE); 283045916cd2Sjpk 283145916cd2Sjpk lower_fstab.zone_fs_raw[0] = '\0'; 283245916cd2Sjpk (void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS, 283345916cd2Sjpk sizeof (lower_fstab.zone_fs_type)); 283445916cd2Sjpk lower_fstab.zone_fs_options = NULL; 283545916cd2Sjpk (void) zonecfg_add_fs_option(&lower_fstab, readonly); 283645916cd2Sjpk 283745916cd2Sjpk /* 283845916cd2Sjpk * Get the list of zones from the kernel 283945916cd2Sjpk */ 284045916cd2Sjpk if (zone_list(NULL, &nzents) != 0) { 284145916cd2Sjpk zerror(zlogp, B_TRUE, "unable to list zones"); 284245916cd2Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 284345916cd2Sjpk return (-1); 284445916cd2Sjpk } 284545916cd2Sjpk again: 284645916cd2Sjpk if (nzents == 0) { 284745916cd2Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 284845916cd2Sjpk return (-1); 284945916cd2Sjpk } 285045916cd2Sjpk 285145916cd2Sjpk zids = malloc(nzents * sizeof (zoneid_t)); 285245916cd2Sjpk if (zids == NULL) { 28533f2f09c1Sdp zerror(zlogp, B_TRUE, "memory allocation failed"); 285445916cd2Sjpk return (-1); 285545916cd2Sjpk } 285645916cd2Sjpk nzents_saved = nzents; 285745916cd2Sjpk 285845916cd2Sjpk if (zone_list(zids, &nzents) != 0) { 285945916cd2Sjpk zerror(zlogp, B_TRUE, "unable to list zones"); 286045916cd2Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 286145916cd2Sjpk free(zids); 286245916cd2Sjpk return (-1); 286345916cd2Sjpk } 286445916cd2Sjpk if (nzents != nzents_saved) { 286545916cd2Sjpk /* list changed, try again */ 286645916cd2Sjpk free(zids); 286745916cd2Sjpk goto again; 286845916cd2Sjpk } 286945916cd2Sjpk 287045916cd2Sjpk ip = getprivimplinfo(); 287145916cd2Sjpk if ((zid_privs = priv_allocset()) == NULL) { 287245916cd2Sjpk zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 287345916cd2Sjpk zonecfg_free_fs_option_list( 287445916cd2Sjpk lower_fstab.zone_fs_options); 287545916cd2Sjpk free(zids); 287645916cd2Sjpk return (-1); 287745916cd2Sjpk } 287845916cd2Sjpk 287945916cd2Sjpk for (i = 0; i < nzents; i++) { 288045916cd2Sjpk char zid_name[ZONENAME_MAX]; 288145916cd2Sjpk zone_state_t zid_state; 288245916cd2Sjpk char zid_rpath[MAXPATHLEN]; 288345916cd2Sjpk struct stat stat_buf; 288445916cd2Sjpk 288545916cd2Sjpk if (zids[i] == GLOBAL_ZONEID) 288645916cd2Sjpk continue; 288745916cd2Sjpk 288845916cd2Sjpk if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1) 288945916cd2Sjpk continue; 289045916cd2Sjpk 289145916cd2Sjpk /* 289245916cd2Sjpk * Do special setup for the zone we are booting 289345916cd2Sjpk */ 289445916cd2Sjpk if (strcmp(zid_name, zone_name) == 0) { 289545916cd2Sjpk struct zone_fstab autofs_fstab; 289645916cd2Sjpk char map_path[MAXPATHLEN]; 289745916cd2Sjpk int fd; 289845916cd2Sjpk 289945916cd2Sjpk /* 290045916cd2Sjpk * Create auto_home_<zone> map for this zone 29019acbbeafSnn35248 * in the global zone. The non-global zone entry 290245916cd2Sjpk * will be created by automount when the zone 290345916cd2Sjpk * is booted. 290445916cd2Sjpk */ 290545916cd2Sjpk 290645916cd2Sjpk (void) snprintf(autofs_fstab.zone_fs_special, 290745916cd2Sjpk MAXPATHLEN, "auto_home_%s", zid_name); 290845916cd2Sjpk 290945916cd2Sjpk (void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN, 291045916cd2Sjpk "/zone/%s/home", zid_name); 291145916cd2Sjpk 291245916cd2Sjpk (void) snprintf(map_path, sizeof (map_path), 291345916cd2Sjpk "/etc/%s", autofs_fstab.zone_fs_special); 291445916cd2Sjpk /* 291545916cd2Sjpk * If the map file doesn't exist create a template 291645916cd2Sjpk */ 291745916cd2Sjpk if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL, 291845916cd2Sjpk S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) { 291945916cd2Sjpk int len; 292045916cd2Sjpk char map_rec[MAXPATHLEN]; 292145916cd2Sjpk 292245916cd2Sjpk len = snprintf(map_rec, sizeof (map_rec), 292345916cd2Sjpk "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n", 292445916cd2Sjpk autofs_fstab.zone_fs_special, rootpath); 292545916cd2Sjpk (void) write(fd, map_rec, len); 292645916cd2Sjpk (void) close(fd); 292745916cd2Sjpk } 292845916cd2Sjpk 292945916cd2Sjpk /* 293045916cd2Sjpk * Mount auto_home_<zone> in the global zone if absent. 293145916cd2Sjpk * If it's already of type autofs, then 293245916cd2Sjpk * don't mount it again. 293345916cd2Sjpk */ 293445916cd2Sjpk if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) || 293545916cd2Sjpk strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) { 293645916cd2Sjpk char optstr[] = "indirect,ignore,nobrowse"; 293745916cd2Sjpk 293845916cd2Sjpk (void) make_one_dir(zlogp, "", 293945916cd2Sjpk autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE); 294045916cd2Sjpk 294145916cd2Sjpk /* 294245916cd2Sjpk * Mount will fail if automounter has already 294345916cd2Sjpk * processed the auto_home_<zonename> map 294445916cd2Sjpk */ 294545916cd2Sjpk (void) domount(zlogp, MNTTYPE_AUTOFS, optstr, 294645916cd2Sjpk autofs_fstab.zone_fs_special, 294745916cd2Sjpk autofs_fstab.zone_fs_dir); 294845916cd2Sjpk } 294945916cd2Sjpk continue; 295045916cd2Sjpk } 295145916cd2Sjpk 295245916cd2Sjpk 295345916cd2Sjpk if (zone_get_state(zid_name, &zid_state) != Z_OK || 295448451833Scarlsonj (zid_state != ZONE_STATE_READY && 295548451833Scarlsonj zid_state != ZONE_STATE_RUNNING)) 295645916cd2Sjpk /* Skip over zones without mounted filesystems */ 295745916cd2Sjpk continue; 295845916cd2Sjpk 295945916cd2Sjpk if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label, 296045916cd2Sjpk sizeof (m_label_t)) < 0) 296145916cd2Sjpk /* Skip over zones with unspecified label */ 296245916cd2Sjpk continue; 296345916cd2Sjpk 296445916cd2Sjpk if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath, 296545916cd2Sjpk sizeof (zid_rpath)) == -1) 296645916cd2Sjpk /* Skip over zones with bad path */ 296745916cd2Sjpk continue; 296845916cd2Sjpk 296945916cd2Sjpk if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs, 297045916cd2Sjpk sizeof (priv_chunk_t) * ip->priv_setsize) == -1) 297145916cd2Sjpk /* Skip over zones with bad privs */ 297245916cd2Sjpk continue; 297345916cd2Sjpk 297445916cd2Sjpk /* 297545916cd2Sjpk * Reading down is valid according to our label model 297645916cd2Sjpk * but some customers want to disable it because it 297745916cd2Sjpk * allows execute down and other possible attacks. 297845916cd2Sjpk * Therefore, we restrict this feature to zones that 297945916cd2Sjpk * have the NET_MAC_AWARE privilege which is required 298045916cd2Sjpk * for NFS read-down semantics. 298145916cd2Sjpk */ 298245916cd2Sjpk if ((bldominates(zlabel, zid_label)) && 298345916cd2Sjpk (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) { 298445916cd2Sjpk /* 298545916cd2Sjpk * Our zone dominates this one. 298645916cd2Sjpk * Create a lofs mount from lower zone's /export/home 298745916cd2Sjpk */ 298845916cd2Sjpk (void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN, 298945916cd2Sjpk "%s/zone/%s/export/home", rootpath, zid_name); 299045916cd2Sjpk 299145916cd2Sjpk /* 299245916cd2Sjpk * If the target is already an LOFS mount 299345916cd2Sjpk * then don't do it again. 299445916cd2Sjpk */ 299545916cd2Sjpk if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) || 299645916cd2Sjpk strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) { 299745916cd2Sjpk 299845916cd2Sjpk if (snprintf(lower_fstab.zone_fs_special, 299945916cd2Sjpk MAXPATHLEN, "%s/export", 300045916cd2Sjpk zid_rpath) > MAXPATHLEN) 300145916cd2Sjpk continue; 300245916cd2Sjpk 300345916cd2Sjpk /* 300445916cd2Sjpk * Make sure the lower-level home exists 300545916cd2Sjpk */ 300645916cd2Sjpk if (make_one_dir(zlogp, 300745916cd2Sjpk lower_fstab.zone_fs_special, 300845916cd2Sjpk "/home", DEFAULT_DIR_MODE) != 0) 300945916cd2Sjpk continue; 301045916cd2Sjpk 301145916cd2Sjpk (void) strlcat(lower_fstab.zone_fs_special, 301245916cd2Sjpk "/home", MAXPATHLEN); 301345916cd2Sjpk 301445916cd2Sjpk /* 301545916cd2Sjpk * Mount can fail because the lower-level 301645916cd2Sjpk * zone may have already done a mount up. 301745916cd2Sjpk */ 301845916cd2Sjpk (void) mount_one(zlogp, &lower_fstab, ""); 301945916cd2Sjpk } 302045916cd2Sjpk } else if ((bldominates(zid_label, zlabel)) && 302145916cd2Sjpk (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) { 302245916cd2Sjpk /* 302345916cd2Sjpk * This zone dominates our zone. 302445916cd2Sjpk * Create a lofs mount from our zone's /export/home 302545916cd2Sjpk */ 302645916cd2Sjpk if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN, 302745916cd2Sjpk "%s/zone/%s/export/home", zid_rpath, 302845916cd2Sjpk zone_name) > MAXPATHLEN) 302945916cd2Sjpk continue; 303045916cd2Sjpk 303145916cd2Sjpk /* 303245916cd2Sjpk * If the target is already an LOFS mount 303345916cd2Sjpk * then don't do it again. 303445916cd2Sjpk */ 303545916cd2Sjpk if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) || 303645916cd2Sjpk strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) { 303745916cd2Sjpk 303845916cd2Sjpk (void) snprintf(lower_fstab.zone_fs_special, 303945916cd2Sjpk MAXPATHLEN, "%s/export/home", rootpath); 304045916cd2Sjpk 304145916cd2Sjpk /* 304245916cd2Sjpk * Mount can fail because the higher-level 304345916cd2Sjpk * zone may have already done a mount down. 304445916cd2Sjpk */ 304545916cd2Sjpk (void) mount_one(zlogp, &lower_fstab, ""); 304645916cd2Sjpk } 304745916cd2Sjpk } 304845916cd2Sjpk } 304945916cd2Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 305045916cd2Sjpk priv_freeset(zid_privs); 305145916cd2Sjpk free(zids); 305245916cd2Sjpk 305345916cd2Sjpk /* 305445916cd2Sjpk * Now share any exported directories from this zone. 305545916cd2Sjpk * Each zone can have its own dfstab. 305645916cd2Sjpk */ 305745916cd2Sjpk 305845916cd2Sjpk argv[0] = "zoneshare"; 305945916cd2Sjpk argv[1] = "-z"; 306045916cd2Sjpk argv[2] = zone_name; 306145916cd2Sjpk argv[3] = NULL; 306245916cd2Sjpk 306345916cd2Sjpk (void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv); 306445916cd2Sjpk /* Don't check for errors since they don't affect the zone */ 306545916cd2Sjpk 306645916cd2Sjpk return (0); 306745916cd2Sjpk } 306845916cd2Sjpk 306945916cd2Sjpk /* 307045916cd2Sjpk * Unmount lofs mounts from higher level zones 307145916cd2Sjpk * Unshare nfs exported directories 307245916cd2Sjpk */ 307345916cd2Sjpk static void 307445916cd2Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name) 307545916cd2Sjpk { 307645916cd2Sjpk zoneid_t *zids = NULL; 307745916cd2Sjpk uint_t nzents_saved; 307845916cd2Sjpk uint_t nzents; 307945916cd2Sjpk int i; 308045916cd2Sjpk char *argv[4]; 308145916cd2Sjpk char path[MAXPATHLEN]; 308245916cd2Sjpk 308345916cd2Sjpk if (!is_system_labeled()) 308445916cd2Sjpk return; 308545916cd2Sjpk 308645916cd2Sjpk /* 308745916cd2Sjpk * Get the list of zones from the kernel 308845916cd2Sjpk */ 308945916cd2Sjpk if (zone_list(NULL, &nzents) != 0) { 309045916cd2Sjpk return; 309145916cd2Sjpk } 309245916cd2Sjpk 309345916cd2Sjpk if (zid_label == NULL) { 309445916cd2Sjpk zid_label = m_label_alloc(MAC_LABEL); 309545916cd2Sjpk if (zid_label == NULL) 309645916cd2Sjpk return; 309745916cd2Sjpk } 309845916cd2Sjpk 309945916cd2Sjpk again: 310045916cd2Sjpk if (nzents == 0) 310145916cd2Sjpk return; 310245916cd2Sjpk 310345916cd2Sjpk zids = malloc(nzents * sizeof (zoneid_t)); 310445916cd2Sjpk if (zids == NULL) { 31053f2f09c1Sdp zerror(zlogp, B_TRUE, "memory allocation failed"); 310645916cd2Sjpk return; 310745916cd2Sjpk } 310845916cd2Sjpk nzents_saved = nzents; 310945916cd2Sjpk 311045916cd2Sjpk if (zone_list(zids, &nzents) != 0) { 311145916cd2Sjpk free(zids); 311245916cd2Sjpk return; 311345916cd2Sjpk } 311445916cd2Sjpk if (nzents != nzents_saved) { 311545916cd2Sjpk /* list changed, try again */ 311645916cd2Sjpk free(zids); 311745916cd2Sjpk goto again; 311845916cd2Sjpk } 311945916cd2Sjpk 312045916cd2Sjpk for (i = 0; i < nzents; i++) { 312145916cd2Sjpk char zid_name[ZONENAME_MAX]; 312245916cd2Sjpk zone_state_t zid_state; 312345916cd2Sjpk char zid_rpath[MAXPATHLEN]; 312445916cd2Sjpk 312545916cd2Sjpk if (zids[i] == GLOBAL_ZONEID) 312645916cd2Sjpk continue; 312745916cd2Sjpk 312845916cd2Sjpk if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1) 312945916cd2Sjpk continue; 313045916cd2Sjpk 313145916cd2Sjpk /* 313245916cd2Sjpk * Skip the zone we are halting 313345916cd2Sjpk */ 313445916cd2Sjpk if (strcmp(zid_name, zone_name) == 0) 313545916cd2Sjpk continue; 313645916cd2Sjpk 313745916cd2Sjpk if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state, 313845916cd2Sjpk sizeof (zid_state)) < 0) || 313945916cd2Sjpk (zid_state < ZONE_IS_READY)) 314045916cd2Sjpk /* Skip over zones without mounted filesystems */ 314145916cd2Sjpk continue; 314245916cd2Sjpk 314345916cd2Sjpk if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label, 314445916cd2Sjpk sizeof (m_label_t)) < 0) 314545916cd2Sjpk /* Skip over zones with unspecified label */ 314645916cd2Sjpk continue; 314745916cd2Sjpk 314845916cd2Sjpk if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath, 314945916cd2Sjpk sizeof (zid_rpath)) == -1) 315045916cd2Sjpk /* Skip over zones with bad path */ 315145916cd2Sjpk continue; 315245916cd2Sjpk 315345916cd2Sjpk if (zlabel != NULL && bldominates(zid_label, zlabel)) { 315445916cd2Sjpk /* 315545916cd2Sjpk * This zone dominates our zone. 315645916cd2Sjpk * Unmount the lofs mount of our zone's /export/home 315745916cd2Sjpk */ 315845916cd2Sjpk 315945916cd2Sjpk if (snprintf(path, MAXPATHLEN, 316045916cd2Sjpk "%s/zone/%s/export/home", zid_rpath, 316145916cd2Sjpk zone_name) > MAXPATHLEN) 316245916cd2Sjpk continue; 316345916cd2Sjpk 316445916cd2Sjpk /* Skip over mount failures */ 316545916cd2Sjpk (void) umount(path); 316645916cd2Sjpk } 316745916cd2Sjpk } 316845916cd2Sjpk free(zids); 316945916cd2Sjpk 317045916cd2Sjpk /* 317145916cd2Sjpk * Unmount global zone autofs trigger for this zone 317245916cd2Sjpk */ 317345916cd2Sjpk (void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name); 317445916cd2Sjpk /* Skip over mount failures */ 317545916cd2Sjpk (void) umount(path); 317645916cd2Sjpk 317745916cd2Sjpk /* 317845916cd2Sjpk * Next unshare any exported directories from this zone. 317945916cd2Sjpk */ 318045916cd2Sjpk 318145916cd2Sjpk argv[0] = "zoneunshare"; 318245916cd2Sjpk argv[1] = "-z"; 318345916cd2Sjpk argv[2] = zone_name; 318445916cd2Sjpk argv[3] = NULL; 318545916cd2Sjpk 318645916cd2Sjpk (void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv); 318745916cd2Sjpk /* Don't check for errors since they don't affect the zone */ 318845916cd2Sjpk 318945916cd2Sjpk /* 319045916cd2Sjpk * Finally, deallocate any devices in the zone. 319145916cd2Sjpk */ 319245916cd2Sjpk 319345916cd2Sjpk argv[0] = "deallocate"; 319445916cd2Sjpk argv[1] = "-Isz"; 319545916cd2Sjpk argv[2] = zone_name; 319645916cd2Sjpk argv[3] = NULL; 319745916cd2Sjpk 319845916cd2Sjpk (void) forkexec(zlogp, "/usr/sbin/deallocate", argv); 319945916cd2Sjpk /* Don't check for errors since they don't affect the zone */ 320045916cd2Sjpk } 320145916cd2Sjpk 320245916cd2Sjpk /* 320345916cd2Sjpk * Fetch the Trusted Extensions label and multi-level ports (MLPs) for 320445916cd2Sjpk * this zone. 320545916cd2Sjpk */ 320645916cd2Sjpk static tsol_zcent_t * 320745916cd2Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs) 320845916cd2Sjpk { 320945916cd2Sjpk FILE *fp; 321045916cd2Sjpk tsol_zcent_t *zcent = NULL; 321145916cd2Sjpk char line[MAXTNZLEN]; 321245916cd2Sjpk 321345916cd2Sjpk if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) { 321445916cd2Sjpk zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH); 321545916cd2Sjpk return (NULL); 321645916cd2Sjpk } 321745916cd2Sjpk 321845916cd2Sjpk while (fgets(line, sizeof (line), fp) != NULL) { 321945916cd2Sjpk /* 322045916cd2Sjpk * Check for malformed database 322145916cd2Sjpk */ 322245916cd2Sjpk if (strlen(line) == MAXTNZLEN - 1) 322345916cd2Sjpk break; 322445916cd2Sjpk if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL) 322545916cd2Sjpk continue; 322645916cd2Sjpk if (strcmp(zcent->zc_name, zone_name) == 0) 322745916cd2Sjpk break; 322845916cd2Sjpk tsol_freezcent(zcent); 322945916cd2Sjpk zcent = NULL; 323045916cd2Sjpk } 323145916cd2Sjpk (void) fclose(fp); 323245916cd2Sjpk 323345916cd2Sjpk if (zcent == NULL) { 323445916cd2Sjpk zerror(zlogp, B_FALSE, "zone requires a label assignment. " 323545916cd2Sjpk "See tnzonecfg(4)"); 323645916cd2Sjpk } else { 323745916cd2Sjpk if (zlabel == NULL) 323845916cd2Sjpk zlabel = m_label_alloc(MAC_LABEL); 323945916cd2Sjpk /* 324045916cd2Sjpk * Save this zone's privileges for later read-down processing 324145916cd2Sjpk */ 324245916cd2Sjpk if ((zprivs = priv_allocset()) == NULL) { 324345916cd2Sjpk zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 324445916cd2Sjpk return (NULL); 324545916cd2Sjpk } else { 324645916cd2Sjpk priv_copyset(privs, zprivs); 324745916cd2Sjpk } 324845916cd2Sjpk } 324945916cd2Sjpk return (zcent); 325045916cd2Sjpk } 325145916cd2Sjpk 325245916cd2Sjpk /* 325345916cd2Sjpk * Add the Trusted Extensions multi-level ports for this zone. 325445916cd2Sjpk */ 325545916cd2Sjpk static void 325645916cd2Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent) 325745916cd2Sjpk { 325845916cd2Sjpk tsol_mlp_t *mlp; 325945916cd2Sjpk tsol_mlpent_t tsme; 326045916cd2Sjpk 326145916cd2Sjpk if (!is_system_labeled()) 326245916cd2Sjpk return; 326345916cd2Sjpk 326445916cd2Sjpk tsme.tsme_zoneid = zoneid; 326545916cd2Sjpk tsme.tsme_flags = 0; 326645916cd2Sjpk for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) { 326745916cd2Sjpk tsme.tsme_mlp = *mlp; 326845916cd2Sjpk if (tnmlp(TNDB_LOAD, &tsme) != 0) { 326945916cd2Sjpk zerror(zlogp, B_TRUE, "cannot set zone-specific MLP " 327045916cd2Sjpk "on %d-%d/%d", mlp->mlp_port, 327145916cd2Sjpk mlp->mlp_port_upper, mlp->mlp_ipp); 327245916cd2Sjpk } 327345916cd2Sjpk } 327445916cd2Sjpk 327545916cd2Sjpk tsme.tsme_flags = TSOL_MEF_SHARED; 327645916cd2Sjpk for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) { 327745916cd2Sjpk tsme.tsme_mlp = *mlp; 327845916cd2Sjpk if (tnmlp(TNDB_LOAD, &tsme) != 0) { 327945916cd2Sjpk zerror(zlogp, B_TRUE, "cannot set shared MLP " 328045916cd2Sjpk "on %d-%d/%d", mlp->mlp_port, 328145916cd2Sjpk mlp->mlp_port_upper, mlp->mlp_ipp); 328245916cd2Sjpk } 328345916cd2Sjpk } 328445916cd2Sjpk } 328545916cd2Sjpk 328645916cd2Sjpk static void 328745916cd2Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid) 328845916cd2Sjpk { 328945916cd2Sjpk tsol_mlpent_t tsme; 329045916cd2Sjpk 329145916cd2Sjpk if (!is_system_labeled()) 329245916cd2Sjpk return; 329345916cd2Sjpk 329445916cd2Sjpk (void) memset(&tsme, 0, sizeof (tsme)); 329545916cd2Sjpk tsme.tsme_zoneid = zoneid; 329645916cd2Sjpk if (tnmlp(TNDB_FLUSH, &tsme) != 0) 329745916cd2Sjpk zerror(zlogp, B_TRUE, "cannot flush MLPs"); 329845916cd2Sjpk } 329945916cd2Sjpk 33007c478bd9Sstevel@tonic-gate int 33017c478bd9Sstevel@tonic-gate prtmount(const char *fs, void *x) { 33027c478bd9Sstevel@tonic-gate zerror((zlog_t *)x, B_FALSE, " %s", fs); 33037c478bd9Sstevel@tonic-gate return (0); 33047c478bd9Sstevel@tonic-gate } 33057c478bd9Sstevel@tonic-gate 3306108322fbScarlsonj /* 3307108322fbScarlsonj * Look for zones running on the main system that are using this root (or any 3308108322fbScarlsonj * subdirectory of it). Return B_TRUE and print an error if a conflicting zone 3309108322fbScarlsonj * is found or if we can't tell. 3310108322fbScarlsonj */ 3311108322fbScarlsonj static boolean_t 3312108322fbScarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath) 33137c478bd9Sstevel@tonic-gate { 3314108322fbScarlsonj zoneid_t *zids = NULL; 3315108322fbScarlsonj uint_t nzids = 0; 3316108322fbScarlsonj boolean_t retv; 3317108322fbScarlsonj int rlen, zlen; 3318108322fbScarlsonj char zroot[MAXPATHLEN]; 3319108322fbScarlsonj char zonename[ZONENAME_MAX]; 3320108322fbScarlsonj 3321108322fbScarlsonj for (;;) { 3322108322fbScarlsonj nzids += 10; 3323108322fbScarlsonj zids = malloc(nzids * sizeof (*zids)); 3324108322fbScarlsonj if (zids == NULL) { 33253f2f09c1Sdp zerror(zlogp, B_TRUE, "memory allocation failed"); 3326108322fbScarlsonj return (B_TRUE); 3327108322fbScarlsonj } 3328108322fbScarlsonj if (zone_list(zids, &nzids) == 0) 3329108322fbScarlsonj break; 3330108322fbScarlsonj free(zids); 3331108322fbScarlsonj } 3332108322fbScarlsonj retv = B_FALSE; 3333108322fbScarlsonj rlen = strlen(rootpath); 3334108322fbScarlsonj while (nzids > 0) { 3335108322fbScarlsonj /* 3336108322fbScarlsonj * Ignore errors; they just mean that the zone has disappeared 3337108322fbScarlsonj * while we were busy. 3338108322fbScarlsonj */ 3339108322fbScarlsonj if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot, 3340108322fbScarlsonj sizeof (zroot)) == -1) 3341108322fbScarlsonj continue; 3342108322fbScarlsonj zlen = strlen(zroot); 3343108322fbScarlsonj if (zlen > rlen) 3344108322fbScarlsonj zlen = rlen; 3345108322fbScarlsonj if (strncmp(rootpath, zroot, zlen) == 0 && 3346108322fbScarlsonj (zroot[zlen] == '\0' || zroot[zlen] == '/') && 3347108322fbScarlsonj (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) { 3348108322fbScarlsonj if (getzonenamebyid(zids[nzids], zonename, 3349108322fbScarlsonj sizeof (zonename)) == -1) 3350108322fbScarlsonj (void) snprintf(zonename, sizeof (zonename), 3351108322fbScarlsonj "id %d", (int)zids[nzids]); 3352108322fbScarlsonj zerror(zlogp, B_FALSE, 3353108322fbScarlsonj "zone root %s already in use by zone %s", 3354108322fbScarlsonj rootpath, zonename); 3355108322fbScarlsonj retv = B_TRUE; 3356108322fbScarlsonj break; 3357108322fbScarlsonj } 3358108322fbScarlsonj } 3359108322fbScarlsonj free(zids); 3360108322fbScarlsonj return (retv); 3361108322fbScarlsonj } 3362108322fbScarlsonj 3363108322fbScarlsonj /* 3364108322fbScarlsonj * Search for loopback mounts that use this same source node (same device and 3365108322fbScarlsonj * inode). Return B_TRUE if there is one or if we can't tell. 3366108322fbScarlsonj */ 3367108322fbScarlsonj static boolean_t 3368108322fbScarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath) 3369108322fbScarlsonj { 3370108322fbScarlsonj struct stat64 rst, zst; 3371108322fbScarlsonj struct mnttab *mnp; 3372108322fbScarlsonj 3373108322fbScarlsonj if (stat64(rootpath, &rst) == -1) { 3374108322fbScarlsonj zerror(zlogp, B_TRUE, "can't stat %s", rootpath); 3375108322fbScarlsonj return (B_TRUE); 3376108322fbScarlsonj } 3377108322fbScarlsonj if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 3378108322fbScarlsonj return (B_TRUE); 3379108322fbScarlsonj for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) { 3380108322fbScarlsonj if (mnp->mnt_fstype == NULL || 3381108322fbScarlsonj strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0) 3382108322fbScarlsonj continue; 3383108322fbScarlsonj /* We're looking at a loopback mount. Stat it. */ 3384108322fbScarlsonj if (mnp->mnt_special != NULL && 3385108322fbScarlsonj stat64(mnp->mnt_special, &zst) != -1 && 3386108322fbScarlsonj rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) { 3387108322fbScarlsonj zerror(zlogp, B_FALSE, 3388108322fbScarlsonj "zone root %s is reachable through %s", 3389108322fbScarlsonj rootpath, mnp->mnt_mountp); 3390108322fbScarlsonj return (B_TRUE); 3391108322fbScarlsonj } 3392108322fbScarlsonj } 3393108322fbScarlsonj return (B_FALSE); 3394108322fbScarlsonj } 3395108322fbScarlsonj 3396*0209230bSgjelinek /* 3397*0209230bSgjelinek * Set memory cap and pool info for the zone's resource management 3398*0209230bSgjelinek * configuration. 3399*0209230bSgjelinek */ 3400*0209230bSgjelinek static int 3401*0209230bSgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid) 3402*0209230bSgjelinek { 3403*0209230bSgjelinek int res; 3404*0209230bSgjelinek uint64_t tmp; 3405*0209230bSgjelinek struct zone_mcaptab mcap; 3406*0209230bSgjelinek char sched[MAXNAMELEN]; 3407*0209230bSgjelinek zone_dochandle_t handle = NULL; 3408*0209230bSgjelinek char pool_err[128]; 3409*0209230bSgjelinek 3410*0209230bSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 3411*0209230bSgjelinek zerror(zlogp, B_TRUE, "getting zone configuration handle"); 3412*0209230bSgjelinek return (Z_BAD_HANDLE); 3413*0209230bSgjelinek } 3414*0209230bSgjelinek 3415*0209230bSgjelinek if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) { 3416*0209230bSgjelinek zerror(zlogp, B_FALSE, "invalid configuration"); 3417*0209230bSgjelinek zonecfg_fini_handle(handle); 3418*0209230bSgjelinek return (res); 3419*0209230bSgjelinek } 3420*0209230bSgjelinek 3421*0209230bSgjelinek /* 3422*0209230bSgjelinek * If a memory cap is configured, set the cap in the kernel using 3423*0209230bSgjelinek * zone_setattr() and make sure the rcapd SMF service is enabled. 3424*0209230bSgjelinek */ 3425*0209230bSgjelinek if (zonecfg_getmcapent(handle, &mcap) == Z_OK) { 3426*0209230bSgjelinek uint64_t num; 3427*0209230bSgjelinek char smf_err[128]; 3428*0209230bSgjelinek 3429*0209230bSgjelinek num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10); 3430*0209230bSgjelinek if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) { 3431*0209230bSgjelinek zerror(zlogp, B_TRUE, "could not set zone memory cap"); 3432*0209230bSgjelinek zonecfg_fini_handle(handle); 3433*0209230bSgjelinek return (Z_INVAL); 3434*0209230bSgjelinek } 3435*0209230bSgjelinek 3436*0209230bSgjelinek if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) { 3437*0209230bSgjelinek zerror(zlogp, B_FALSE, "enabling system/rcap service " 3438*0209230bSgjelinek "failed: %s", smf_err); 3439*0209230bSgjelinek zonecfg_fini_handle(handle); 3440*0209230bSgjelinek return (Z_INVAL); 3441*0209230bSgjelinek } 3442*0209230bSgjelinek } 3443*0209230bSgjelinek 3444*0209230bSgjelinek /* Get the scheduling class set in the zone configuration. */ 3445*0209230bSgjelinek if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK && 3446*0209230bSgjelinek strlen(sched) > 0) { 3447*0209230bSgjelinek if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched, 3448*0209230bSgjelinek strlen(sched)) == -1) 3449*0209230bSgjelinek zerror(zlogp, B_TRUE, "WARNING: unable to set the " 3450*0209230bSgjelinek "default scheduling class"); 3451*0209230bSgjelinek 3452*0209230bSgjelinek } else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp) 3453*0209230bSgjelinek == Z_OK) { 3454*0209230bSgjelinek /* 3455*0209230bSgjelinek * If the zone has the zone.cpu-shares rctl set then we want to 3456*0209230bSgjelinek * use the Fair Share Scheduler (FSS) for processes in the 3457*0209230bSgjelinek * zone. Check what scheduling class the zone would be running 3458*0209230bSgjelinek * in by default so we can print a warning and modify the class 3459*0209230bSgjelinek * if we wouldn't be using FSS. 3460*0209230bSgjelinek */ 3461*0209230bSgjelinek char class_name[PC_CLNMSZ]; 3462*0209230bSgjelinek 3463*0209230bSgjelinek if (zonecfg_get_dflt_sched_class(handle, class_name, 3464*0209230bSgjelinek sizeof (class_name)) != Z_OK) { 3465*0209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: unable to determine " 3466*0209230bSgjelinek "the zone's scheduling class"); 3467*0209230bSgjelinek 3468*0209230bSgjelinek } else if (strcmp("FSS", class_name) != 0) { 3469*0209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares " 3470*0209230bSgjelinek "rctl is set but\nFSS is not the default " 3471*0209230bSgjelinek "scheduling class for\nthis zone. FSS will be " 3472*0209230bSgjelinek "used for processes\nin the zone but to get the " 3473*0209230bSgjelinek "full benefit of FSS,\nit should be the default " 3474*0209230bSgjelinek "scheduling class.\nSee dispadmin(1M) for more " 3475*0209230bSgjelinek "details."); 3476*0209230bSgjelinek 3477*0209230bSgjelinek if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS", 3478*0209230bSgjelinek strlen("FSS")) == -1) 3479*0209230bSgjelinek zerror(zlogp, B_TRUE, "WARNING: unable to set " 3480*0209230bSgjelinek "zone scheduling class to FSS"); 3481*0209230bSgjelinek } 3482*0209230bSgjelinek } 3483*0209230bSgjelinek 3484*0209230bSgjelinek /* 3485*0209230bSgjelinek * The next few blocks of code attempt to set up temporary pools as 3486*0209230bSgjelinek * well as persistent pools. In all cases we call the functions 3487*0209230bSgjelinek * unconditionally. Within each funtion the code will check if the 3488*0209230bSgjelinek * zone is actually configured for a temporary pool or persistent pool 3489*0209230bSgjelinek * and just return if there is nothing to do. 3490*0209230bSgjelinek * 3491*0209230bSgjelinek * If we are rebooting we want to attempt to reuse any temporary pool 3492*0209230bSgjelinek * that was previously set up. zonecfg_bind_tmp_pool() will do the 3493*0209230bSgjelinek * right thing in all cases (reuse or create) based on the current 3494*0209230bSgjelinek * zonecfg. 3495*0209230bSgjelinek */ 3496*0209230bSgjelinek if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err, 3497*0209230bSgjelinek sizeof (pool_err))) != Z_OK) { 3498*0209230bSgjelinek if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND) 3499*0209230bSgjelinek zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting " 3500*0209230bSgjelinek "cannot be instantiated", zonecfg_strerror(res), 3501*0209230bSgjelinek pool_err); 3502*0209230bSgjelinek else 3503*0209230bSgjelinek zerror(zlogp, B_FALSE, "could not bind zone to " 3504*0209230bSgjelinek "temporary pool: %s", zonecfg_strerror(res)); 3505*0209230bSgjelinek zonecfg_fini_handle(handle); 3506*0209230bSgjelinek return (Z_POOL_BIND); 3507*0209230bSgjelinek } 3508*0209230bSgjelinek 3509*0209230bSgjelinek /* 3510*0209230bSgjelinek * Check if we need to warn about poold not being enabled. 3511*0209230bSgjelinek */ 3512*0209230bSgjelinek if (zonecfg_warn_poold(handle)) { 3513*0209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has " 3514*0209230bSgjelinek "been specified\nbut the dynamic pool service is not " 3515*0209230bSgjelinek "enabled.\nThe system will not dynamically adjust the\n" 3516*0209230bSgjelinek "processor allocation within the specified range\n" 3517*0209230bSgjelinek "until svc:/system/pools/dynamic is enabled.\n" 3518*0209230bSgjelinek "See poold(1M)."); 3519*0209230bSgjelinek } 3520*0209230bSgjelinek 3521*0209230bSgjelinek /* The following is a warning, not an error. */ 3522*0209230bSgjelinek if ((res = zonecfg_bind_pool(handle, zoneid, pool_err, 3523*0209230bSgjelinek sizeof (pool_err))) != Z_OK) { 3524*0209230bSgjelinek if (res == Z_POOL_BIND) 3525*0209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: unable to bind to " 3526*0209230bSgjelinek "pool '%s'; using default pool.", pool_err); 3527*0209230bSgjelinek else if (res == Z_POOL) 3528*0209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: %s: %s", 3529*0209230bSgjelinek zonecfg_strerror(res), pool_err); 3530*0209230bSgjelinek else 3531*0209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: %s", 3532*0209230bSgjelinek zonecfg_strerror(res)); 3533*0209230bSgjelinek } 3534*0209230bSgjelinek 3535*0209230bSgjelinek zonecfg_fini_handle(handle); 3536*0209230bSgjelinek return (Z_OK); 3537*0209230bSgjelinek } 3538*0209230bSgjelinek 3539108322fbScarlsonj zoneid_t 3540108322fbScarlsonj vplat_create(zlog_t *zlogp, boolean_t mount_cmd) 3541108322fbScarlsonj { 3542108322fbScarlsonj zoneid_t rval = -1; 35437c478bd9Sstevel@tonic-gate priv_set_t *privs; 35447c478bd9Sstevel@tonic-gate char rootpath[MAXPATHLEN]; 35459acbbeafSnn35248 char modname[MAXPATHLEN]; 35469acbbeafSnn35248 struct brand_attr attr; 3547123807fbSedp brand_handle_t bh; 35487c478bd9Sstevel@tonic-gate char *rctlbuf = NULL; 3549108322fbScarlsonj size_t rctlbufsz = 0; 3550fa9e4066Sahrens char *zfsbuf = NULL; 3551fa9e4066Sahrens size_t zfsbufsz = 0; 3552108322fbScarlsonj zoneid_t zoneid = -1; 35537c478bd9Sstevel@tonic-gate int xerr; 3554108322fbScarlsonj char *kzone; 3555108322fbScarlsonj FILE *fp = NULL; 355645916cd2Sjpk tsol_zcent_t *zcent = NULL; 355745916cd2Sjpk int match = 0; 355845916cd2Sjpk int doi = 0; 35597c478bd9Sstevel@tonic-gate 35607c478bd9Sstevel@tonic-gate if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) { 35617c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone root"); 35627c478bd9Sstevel@tonic-gate return (-1); 35637c478bd9Sstevel@tonic-gate } 3564108322fbScarlsonj if (zonecfg_in_alt_root()) 3565108322fbScarlsonj resolve_lofs(zlogp, rootpath, sizeof (rootpath)); 35667c478bd9Sstevel@tonic-gate 35677c478bd9Sstevel@tonic-gate if ((privs = priv_allocset()) == NULL) { 35687c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 35697c478bd9Sstevel@tonic-gate return (-1); 35707c478bd9Sstevel@tonic-gate } 35717c478bd9Sstevel@tonic-gate priv_emptyset(privs); 3572ffbafc53Scomay if (get_privset(zlogp, privs, mount_cmd) != 0) 35737c478bd9Sstevel@tonic-gate goto error; 3574ffbafc53Scomay 3575108322fbScarlsonj if (!mount_cmd && get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) { 35767c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "Unable to get list of rctls"); 35777c478bd9Sstevel@tonic-gate goto error; 35787c478bd9Sstevel@tonic-gate } 3579ffbafc53Scomay 3580fa9e4066Sahrens if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) { 3581fa9e4066Sahrens zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets"); 3582fa9e4066Sahrens goto error; 3583fa9e4066Sahrens } 35847c478bd9Sstevel@tonic-gate 358548451833Scarlsonj if (!mount_cmd && is_system_labeled()) { 358645916cd2Sjpk zcent = get_zone_label(zlogp, privs); 358748451833Scarlsonj if (zcent != NULL) { 358845916cd2Sjpk match = zcent->zc_match; 358945916cd2Sjpk doi = zcent->zc_doi; 359045916cd2Sjpk *zlabel = zcent->zc_label; 359145916cd2Sjpk } else { 359245916cd2Sjpk goto error; 359345916cd2Sjpk } 359445916cd2Sjpk } 359545916cd2Sjpk 3596108322fbScarlsonj kzone = zone_name; 3597108322fbScarlsonj 3598108322fbScarlsonj /* 3599108322fbScarlsonj * We must do this scan twice. First, we look for zones running on the 3600108322fbScarlsonj * main system that are using this root (or any subdirectory of it). 3601108322fbScarlsonj * Next, we reduce to the shortest path and search for loopback mounts 3602108322fbScarlsonj * that use this same source node (same device and inode). 3603108322fbScarlsonj */ 3604108322fbScarlsonj if (duplicate_zone_root(zlogp, rootpath)) 3605108322fbScarlsonj goto error; 3606108322fbScarlsonj if (duplicate_reachable_path(zlogp, rootpath)) 3607108322fbScarlsonj goto error; 3608108322fbScarlsonj 3609108322fbScarlsonj if (mount_cmd) { 36109acbbeafSnn35248 assert(zone_isnative); 3611108322fbScarlsonj root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE); 3612108322fbScarlsonj 3613108322fbScarlsonj /* 3614108322fbScarlsonj * Forge up a special root for this zone. When a zone is 3615108322fbScarlsonj * mounted, we can't let the zone have its own root because the 3616108322fbScarlsonj * tools that will be used in this "scratch zone" need access 3617108322fbScarlsonj * to both the zone's resources and the running machine's 3618108322fbScarlsonj * executables. 3619108322fbScarlsonj * 3620108322fbScarlsonj * Note that the mkdir here also catches read-only filesystems. 3621108322fbScarlsonj */ 3622108322fbScarlsonj if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) { 3623108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot create %s", rootpath); 3624108322fbScarlsonj goto error; 3625108322fbScarlsonj } 3626108322fbScarlsonj if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0) 3627108322fbScarlsonj goto error; 3628108322fbScarlsonj } 3629108322fbScarlsonj 3630108322fbScarlsonj if (zonecfg_in_alt_root()) { 3631108322fbScarlsonj /* 3632108322fbScarlsonj * If we are mounting up a zone in an alternate root partition, 3633108322fbScarlsonj * then we have some additional work to do before starting the 3634108322fbScarlsonj * zone. First, resolve the root path down so that we're not 3635108322fbScarlsonj * fooled by duplicates. Then forge up an internal name for 3636108322fbScarlsonj * the zone. 3637108322fbScarlsonj */ 3638108322fbScarlsonj if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) { 3639108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot open mapfile"); 3640108322fbScarlsonj goto error; 3641108322fbScarlsonj } 3642108322fbScarlsonj if (zonecfg_lock_scratch(fp) != 0) { 3643108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot lock mapfile"); 3644108322fbScarlsonj goto error; 3645108322fbScarlsonj } 3646108322fbScarlsonj if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(), 3647108322fbScarlsonj NULL, 0) == 0) { 3648108322fbScarlsonj zerror(zlogp, B_FALSE, "scratch zone already running"); 3649108322fbScarlsonj goto error; 3650108322fbScarlsonj } 3651108322fbScarlsonj /* This is the preferred name */ 3652108322fbScarlsonj (void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s", 3653108322fbScarlsonj zone_name); 3654108322fbScarlsonj srandom(getpid()); 3655108322fbScarlsonj while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL, 3656108322fbScarlsonj 0) == 0) { 3657108322fbScarlsonj /* This is just an arbitrary name; note "." usage */ 3658108322fbScarlsonj (void) snprintf(kernzone, sizeof (kernzone), 3659108322fbScarlsonj "SUNWlu.%08lX%08lX", random(), random()); 3660108322fbScarlsonj } 3661108322fbScarlsonj kzone = kernzone; 3662108322fbScarlsonj } 3663108322fbScarlsonj 36647c478bd9Sstevel@tonic-gate xerr = 0; 3665108322fbScarlsonj if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf, 366645916cd2Sjpk rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel)) == -1) { 36677c478bd9Sstevel@tonic-gate if (xerr == ZE_AREMOUNTS) { 36687c478bd9Sstevel@tonic-gate if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) { 36697c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 36707c478bd9Sstevel@tonic-gate "An unknown file-system is mounted on " 36717c478bd9Sstevel@tonic-gate "a subdirectory of %s", rootpath); 36727c478bd9Sstevel@tonic-gate } else { 36737c478bd9Sstevel@tonic-gate 36747c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 36757c478bd9Sstevel@tonic-gate "These file-systems are mounted on " 36767c478bd9Sstevel@tonic-gate "subdirectories of %s:", rootpath); 36777c478bd9Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, 36787c478bd9Sstevel@tonic-gate prtmount, zlogp); 36797c478bd9Sstevel@tonic-gate } 36807c478bd9Sstevel@tonic-gate } else if (xerr == ZE_CHROOTED) { 36817c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s: " 36827c478bd9Sstevel@tonic-gate "cannot create a zone from a chrooted " 36837c478bd9Sstevel@tonic-gate "environment", "zone_create"); 36847c478bd9Sstevel@tonic-gate } else { 36857c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "zone_create"); 36867c478bd9Sstevel@tonic-gate } 36877c478bd9Sstevel@tonic-gate goto error; 36887c478bd9Sstevel@tonic-gate } 3689108322fbScarlsonj 3690108322fbScarlsonj if (zonecfg_in_alt_root() && 3691108322fbScarlsonj zonecfg_add_scratch(fp, zone_name, kernzone, 3692108322fbScarlsonj zonecfg_get_root()) == -1) { 3693108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot add mapfile entry"); 3694108322fbScarlsonj goto error; 3695108322fbScarlsonj } 3696108322fbScarlsonj 36979acbbeafSnn35248 if ((zone_get_brand(zone_name, attr.ba_brandname, 36989acbbeafSnn35248 MAXNAMELEN) != Z_OK) || 3699123807fbSedp (bh = brand_open(attr.ba_brandname)) == NULL) { 37009acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine brand name"); 37019acbbeafSnn35248 return (-1); 37029acbbeafSnn35248 } 37039acbbeafSnn35248 37049acbbeafSnn35248 /* 37059acbbeafSnn35248 * If this brand requires any kernel support, now is the time to 37069acbbeafSnn35248 * get it loaded and initialized. 37079acbbeafSnn35248 */ 3708123807fbSedp if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) { 37099acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine brand kernel " 37109acbbeafSnn35248 "module"); 37119acbbeafSnn35248 return (-1); 37129acbbeafSnn35248 } 37139acbbeafSnn35248 37149acbbeafSnn35248 if (strlen(modname) > 0) { 37159acbbeafSnn35248 (void) strlcpy(attr.ba_modname, modname, MAXPATHLEN); 37169acbbeafSnn35248 if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr, 37179acbbeafSnn35248 sizeof (attr) != 0)) { 37189acbbeafSnn35248 zerror(zlogp, B_TRUE, "could not set zone brand " 37199acbbeafSnn35248 "attribute."); 37209acbbeafSnn35248 goto error; 37219acbbeafSnn35248 } 37229acbbeafSnn35248 } 37239acbbeafSnn35248 37247c478bd9Sstevel@tonic-gate /* 3725*0209230bSgjelinek * The following actions are not performed when merely mounting a zone 3726*0209230bSgjelinek * for administrative use. 37277c478bd9Sstevel@tonic-gate */ 3728*0209230bSgjelinek if (!mount_cmd) { 3729*0209230bSgjelinek if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK) { 3730*0209230bSgjelinek (void) zone_shutdown(zoneid); 3731*0209230bSgjelinek goto error; 3732*0209230bSgjelinek } 3733*0209230bSgjelinek 373445916cd2Sjpk set_mlps(zlogp, zoneid, zcent); 3735*0209230bSgjelinek } 3736*0209230bSgjelinek 3737108322fbScarlsonj rval = zoneid; 3738108322fbScarlsonj zoneid = -1; 3739108322fbScarlsonj 37407c478bd9Sstevel@tonic-gate error: 3741108322fbScarlsonj if (zoneid != -1) 3742108322fbScarlsonj (void) zone_destroy(zoneid); 37437c478bd9Sstevel@tonic-gate if (rctlbuf != NULL) 37447c478bd9Sstevel@tonic-gate free(rctlbuf); 37457c478bd9Sstevel@tonic-gate priv_freeset(privs); 3746108322fbScarlsonj if (fp != NULL) 3747108322fbScarlsonj zonecfg_close_scratch(fp); 3748108322fbScarlsonj lofs_discard_mnttab(); 374945916cd2Sjpk if (zcent != NULL) 375045916cd2Sjpk tsol_freezcent(zcent); 37517c478bd9Sstevel@tonic-gate return (rval); 37527c478bd9Sstevel@tonic-gate } 37537c478bd9Sstevel@tonic-gate 3754555afedfScarlsonj /* 3755555afedfScarlsonj * Enter the zone and write a /etc/zones/index file there. This allows 3756555afedfScarlsonj * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone 3757555afedfScarlsonj * details from inside the zone. 3758555afedfScarlsonj */ 3759555afedfScarlsonj static void 3760555afedfScarlsonj write_index_file(zoneid_t zoneid) 3761555afedfScarlsonj { 3762555afedfScarlsonj FILE *zef; 3763555afedfScarlsonj FILE *zet; 3764555afedfScarlsonj struct zoneent *zep; 3765555afedfScarlsonj pid_t child; 3766555afedfScarlsonj int tmpl_fd; 3767555afedfScarlsonj ctid_t ct; 3768555afedfScarlsonj int fd; 3769555afedfScarlsonj char uuidstr[UUID_PRINTABLE_STRING_LENGTH]; 3770555afedfScarlsonj 3771555afedfScarlsonj /* Locate the zone entry in the global zone's index file */ 3772555afedfScarlsonj if ((zef = setzoneent()) == NULL) 3773555afedfScarlsonj return; 3774555afedfScarlsonj while ((zep = getzoneent_private(zef)) != NULL) { 3775555afedfScarlsonj if (strcmp(zep->zone_name, zone_name) == 0) 3776555afedfScarlsonj break; 3777555afedfScarlsonj free(zep); 3778555afedfScarlsonj } 3779555afedfScarlsonj endzoneent(zef); 3780555afedfScarlsonj if (zep == NULL) 3781555afedfScarlsonj return; 3782555afedfScarlsonj 3783555afedfScarlsonj if ((tmpl_fd = init_template()) == -1) { 3784555afedfScarlsonj free(zep); 3785555afedfScarlsonj return; 3786555afedfScarlsonj } 3787555afedfScarlsonj 3788555afedfScarlsonj if ((child = fork()) == -1) { 3789555afedfScarlsonj (void) ct_tmpl_clear(tmpl_fd); 3790555afedfScarlsonj (void) close(tmpl_fd); 3791555afedfScarlsonj free(zep); 3792555afedfScarlsonj return; 3793555afedfScarlsonj } 3794555afedfScarlsonj 3795555afedfScarlsonj /* parent waits for child to finish */ 3796555afedfScarlsonj if (child != 0) { 3797555afedfScarlsonj free(zep); 3798555afedfScarlsonj if (contract_latest(&ct) == -1) 3799555afedfScarlsonj ct = -1; 3800555afedfScarlsonj (void) ct_tmpl_clear(tmpl_fd); 3801555afedfScarlsonj (void) close(tmpl_fd); 3802555afedfScarlsonj (void) waitpid(child, NULL, 0); 3803555afedfScarlsonj (void) contract_abandon_id(ct); 3804555afedfScarlsonj return; 3805555afedfScarlsonj } 3806555afedfScarlsonj 3807555afedfScarlsonj /* child enters zone and sets up index file */ 3808555afedfScarlsonj (void) ct_tmpl_clear(tmpl_fd); 3809555afedfScarlsonj if (zone_enter(zoneid) != -1) { 3810555afedfScarlsonj (void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE); 3811555afedfScarlsonj (void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID, 3812555afedfScarlsonj ZONE_CONFIG_GID); 3813555afedfScarlsonj fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC, 3814555afedfScarlsonj ZONE_INDEX_MODE); 3815555afedfScarlsonj if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) { 3816555afedfScarlsonj (void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID); 3817555afedfScarlsonj if (uuid_is_null(zep->zone_uuid)) 3818555afedfScarlsonj uuidstr[0] = '\0'; 3819555afedfScarlsonj else 3820555afedfScarlsonj uuid_unparse(zep->zone_uuid, uuidstr); 3821555afedfScarlsonj (void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name, 3822555afedfScarlsonj zone_state_str(zep->zone_state), 3823555afedfScarlsonj uuidstr); 3824555afedfScarlsonj (void) fclose(zet); 3825555afedfScarlsonj } 3826555afedfScarlsonj } 3827555afedfScarlsonj _exit(0); 3828555afedfScarlsonj } 3829555afedfScarlsonj 38307c478bd9Sstevel@tonic-gate int 3831555afedfScarlsonj vplat_bringup(zlog_t *zlogp, boolean_t mount_cmd, zoneid_t zoneid) 38327c478bd9Sstevel@tonic-gate { 38339acbbeafSnn35248 char zonepath[MAXPATHLEN]; 38345749802bSdp 3835fa9e4066Sahrens if (!mount_cmd && validate_datasets(zlogp) != 0) { 3836fa9e4066Sahrens lofs_discard_mnttab(); 3837fa9e4066Sahrens return (-1); 3838fa9e4066Sahrens } 3839fa9e4066Sahrens 38409acbbeafSnn35248 /* 38419acbbeafSnn35248 * Before we try to mount filesystems we need to create the 38429acbbeafSnn35248 * attribute backing store for /dev 38439acbbeafSnn35248 */ 38449acbbeafSnn35248 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 38459acbbeafSnn35248 lofs_discard_mnttab(); 38469acbbeafSnn35248 return (-1); 38479acbbeafSnn35248 } 384816bca938Svp157776 384916bca938Svp157776 resolve_lofs(zlogp, zonepath, sizeof (zonepath)); 38509acbbeafSnn35248 if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE) != 0) { 3851108322fbScarlsonj lofs_discard_mnttab(); 38527c478bd9Sstevel@tonic-gate return (-1); 3853108322fbScarlsonj } 3854facf4a8dSllai1 38559acbbeafSnn35248 if (mount_filesystems(zlogp, mount_cmd) != 0) { 3856facf4a8dSllai1 lofs_discard_mnttab(); 3857facf4a8dSllai1 return (-1); 3858facf4a8dSllai1 } 3859facf4a8dSllai1 3860facf4a8dSllai1 if (!mount_cmd && configure_network_interfaces(zlogp) != 0) { 3861108322fbScarlsonj lofs_discard_mnttab(); 38627c478bd9Sstevel@tonic-gate return (-1); 3863108322fbScarlsonj } 3864555afedfScarlsonj 3865555afedfScarlsonj write_index_file(zoneid); 3866555afedfScarlsonj 3867108322fbScarlsonj lofs_discard_mnttab(); 38687c478bd9Sstevel@tonic-gate return (0); 38697c478bd9Sstevel@tonic-gate } 38707c478bd9Sstevel@tonic-gate 3871108322fbScarlsonj static int 3872108322fbScarlsonj lu_root_teardown(zlog_t *zlogp) 38737c478bd9Sstevel@tonic-gate { 3874108322fbScarlsonj char zroot[MAXPATHLEN]; 3875108322fbScarlsonj 38769acbbeafSnn35248 assert(zone_isnative); 38779acbbeafSnn35248 3878108322fbScarlsonj if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 3879108322fbScarlsonj zerror(zlogp, B_FALSE, "unable to determine zone root"); 3880108322fbScarlsonj return (-1); 3881108322fbScarlsonj } 3882108322fbScarlsonj root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE); 3883108322fbScarlsonj 3884108322fbScarlsonj /* 3885108322fbScarlsonj * At this point, the processes are gone, the filesystems (save the 3886108322fbScarlsonj * root) are unmounted, and the zone is on death row. But there may 3887108322fbScarlsonj * still be creds floating about in the system that reference the 3888108322fbScarlsonj * zone_t, and which pin down zone_rootvp causing this call to fail 3889108322fbScarlsonj * with EBUSY. Thus, we try for a little while before just giving up. 3890108322fbScarlsonj * (How I wish this were not true, and umount2 just did the right 3891108322fbScarlsonj * thing, or tmpfs supported MS_FORCE This is a gross hack.) 3892108322fbScarlsonj */ 3893108322fbScarlsonj if (umount2(zroot, MS_FORCE) != 0) { 3894108322fbScarlsonj if (errno == ENOTSUP && umount2(zroot, 0) == 0) 3895108322fbScarlsonj goto unmounted; 3896108322fbScarlsonj if (errno == EBUSY) { 3897108322fbScarlsonj int tries = 10; 3898108322fbScarlsonj 3899108322fbScarlsonj while (--tries >= 0) { 3900108322fbScarlsonj (void) sleep(1); 3901108322fbScarlsonj if (umount2(zroot, 0) == 0) 3902108322fbScarlsonj goto unmounted; 3903108322fbScarlsonj if (errno != EBUSY) 3904108322fbScarlsonj break; 3905108322fbScarlsonj } 3906108322fbScarlsonj } 3907108322fbScarlsonj zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot); 3908108322fbScarlsonj return (-1); 3909108322fbScarlsonj } 3910108322fbScarlsonj unmounted: 3911108322fbScarlsonj 3912108322fbScarlsonj /* 3913108322fbScarlsonj * Only zones in an alternate root environment have scratch zone 3914108322fbScarlsonj * entries. 3915108322fbScarlsonj */ 3916108322fbScarlsonj if (zonecfg_in_alt_root()) { 3917108322fbScarlsonj FILE *fp; 3918108322fbScarlsonj int retv; 3919108322fbScarlsonj 3920108322fbScarlsonj if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 3921108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot open mapfile"); 3922108322fbScarlsonj return (-1); 3923108322fbScarlsonj } 3924108322fbScarlsonj retv = -1; 3925108322fbScarlsonj if (zonecfg_lock_scratch(fp) != 0) 3926108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot lock mapfile"); 3927108322fbScarlsonj else if (zonecfg_delete_scratch(fp, kernzone) != 0) 3928108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot delete map entry"); 3929108322fbScarlsonj else 3930108322fbScarlsonj retv = 0; 3931108322fbScarlsonj zonecfg_close_scratch(fp); 3932108322fbScarlsonj return (retv); 3933108322fbScarlsonj } else { 3934108322fbScarlsonj return (0); 3935108322fbScarlsonj } 3936108322fbScarlsonj } 3937108322fbScarlsonj 3938108322fbScarlsonj int 3939*0209230bSgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting) 3940108322fbScarlsonj { 3941108322fbScarlsonj char *kzone; 39427c478bd9Sstevel@tonic-gate zoneid_t zoneid; 3943*0209230bSgjelinek int res; 3944*0209230bSgjelinek char pool_err[128]; 39459acbbeafSnn35248 char zroot[MAXPATHLEN]; 39469acbbeafSnn35248 char cmdbuf[MAXPATHLEN]; 39479acbbeafSnn35248 char brand[MAXNAMELEN]; 3948123807fbSedp brand_handle_t bh = NULL; 39497c478bd9Sstevel@tonic-gate 3950108322fbScarlsonj kzone = zone_name; 3951108322fbScarlsonj if (zonecfg_in_alt_root()) { 3952108322fbScarlsonj FILE *fp; 3953108322fbScarlsonj 3954108322fbScarlsonj if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 3955108322fbScarlsonj zerror(zlogp, B_TRUE, "unable to open map file"); 3956108322fbScarlsonj goto error; 3957108322fbScarlsonj } 3958108322fbScarlsonj if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(), 3959108322fbScarlsonj kernzone, sizeof (kernzone)) != 0) { 3960108322fbScarlsonj zerror(zlogp, B_FALSE, "unable to find scratch zone"); 3961108322fbScarlsonj zonecfg_close_scratch(fp); 3962108322fbScarlsonj goto error; 3963108322fbScarlsonj } 3964108322fbScarlsonj zonecfg_close_scratch(fp); 3965108322fbScarlsonj kzone = kernzone; 3966108322fbScarlsonj } 3967108322fbScarlsonj 3968108322fbScarlsonj if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) { 39697c478bd9Sstevel@tonic-gate if (!bringup_failure_recovery) 39707c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid"); 3971108322fbScarlsonj if (unmount_cmd) 3972108322fbScarlsonj (void) lu_root_teardown(zlogp); 39737c478bd9Sstevel@tonic-gate goto error; 39747c478bd9Sstevel@tonic-gate } 39757c478bd9Sstevel@tonic-gate 39767c478bd9Sstevel@tonic-gate if (zone_shutdown(zoneid) != 0) { 39777c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to shutdown zone"); 39787c478bd9Sstevel@tonic-gate goto error; 39797c478bd9Sstevel@tonic-gate } 39807c478bd9Sstevel@tonic-gate 39819acbbeafSnn35248 /* Get the path to the root of this zone */ 39829acbbeafSnn35248 if (zone_get_zonepath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 39839acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine zone root"); 39849acbbeafSnn35248 goto error; 39859acbbeafSnn35248 } 39869acbbeafSnn35248 39879acbbeafSnn35248 /* Get a handle to the brand info for this zone */ 39889acbbeafSnn35248 if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) || 3989123807fbSedp (bh = brand_open(brand)) == NULL) { 39909acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 39919acbbeafSnn35248 return (-1); 39929acbbeafSnn35248 } 39939acbbeafSnn35248 /* 39949acbbeafSnn35248 * If there is a brand 'halt' callback, execute it now to give the 39959acbbeafSnn35248 * brand a chance to cleanup any custom configuration. 39969acbbeafSnn35248 */ 39979acbbeafSnn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 3998123807fbSedp if (brand_get_halt(bh, zone_name, zroot, cmdbuf + EXEC_LEN, 39999acbbeafSnn35248 sizeof (cmdbuf) - EXEC_LEN, 0, NULL) < 0) { 4000123807fbSedp brand_close(bh); 40019acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine branded zone's " 40029acbbeafSnn35248 "halt callback."); 40039acbbeafSnn35248 goto error; 40049acbbeafSnn35248 } 4005123807fbSedp brand_close(bh); 40069acbbeafSnn35248 40079acbbeafSnn35248 if ((strlen(cmdbuf) > EXEC_LEN) && 40089acbbeafSnn35248 (do_subproc(zlogp, cmdbuf) != Z_OK)) { 40099acbbeafSnn35248 zerror(zlogp, B_FALSE, "%s failed", cmdbuf); 40109acbbeafSnn35248 goto error; 40119acbbeafSnn35248 } 40129acbbeafSnn35248 4013108322fbScarlsonj if (!unmount_cmd && 4014108322fbScarlsonj unconfigure_network_interfaces(zlogp, zoneid) != 0) { 40157c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 40167c478bd9Sstevel@tonic-gate "unable to unconfigure network interfaces in zone"); 40177c478bd9Sstevel@tonic-gate goto error; 40187c478bd9Sstevel@tonic-gate } 40197c478bd9Sstevel@tonic-gate 4020108322fbScarlsonj if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) { 40217c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to abort TCP connections"); 40227c478bd9Sstevel@tonic-gate goto error; 40237c478bd9Sstevel@tonic-gate } 40247c478bd9Sstevel@tonic-gate 4025facf4a8dSllai1 /* destroy zconsole before umount /dev */ 4026facf4a8dSllai1 if (!unmount_cmd) 4027facf4a8dSllai1 destroy_console_slave(); 4028facf4a8dSllai1 4029108322fbScarlsonj if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) { 40307c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 40317c478bd9Sstevel@tonic-gate "unable to unmount file systems in zone"); 40327c478bd9Sstevel@tonic-gate goto error; 40337c478bd9Sstevel@tonic-gate } 40347c478bd9Sstevel@tonic-gate 4035*0209230bSgjelinek /* 4036*0209230bSgjelinek * If we are rebooting then we don't want to destroy an existing 4037*0209230bSgjelinek * temporary pool at this point so that we can just reuse it when the 4038*0209230bSgjelinek * zone boots back up. 4039*0209230bSgjelinek */ 4040*0209230bSgjelinek if (!unmount_cmd && !rebooting) { 4041*0209230bSgjelinek if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err, 4042*0209230bSgjelinek sizeof (pool_err))) != Z_OK) { 4043*0209230bSgjelinek if (res == Z_POOL) 4044*0209230bSgjelinek zerror(zlogp, B_FALSE, pool_err); 4045*0209230bSgjelinek } 4046*0209230bSgjelinek } 4047*0209230bSgjelinek 404845916cd2Sjpk remove_mlps(zlogp, zoneid); 404945916cd2Sjpk 40507c478bd9Sstevel@tonic-gate if (zone_destroy(zoneid) != 0) { 40517c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to destroy zone"); 40527c478bd9Sstevel@tonic-gate goto error; 40537c478bd9Sstevel@tonic-gate } 4054108322fbScarlsonj 4055108322fbScarlsonj /* 4056108322fbScarlsonj * Special teardown for alternate boot environments: remove the tmpfs 4057108322fbScarlsonj * root for the zone and then remove it from the map file. 4058108322fbScarlsonj */ 4059108322fbScarlsonj if (unmount_cmd && lu_root_teardown(zlogp) != 0) 4060108322fbScarlsonj goto error; 4061108322fbScarlsonj 4062108322fbScarlsonj lofs_discard_mnttab(); 40637c478bd9Sstevel@tonic-gate return (0); 40647c478bd9Sstevel@tonic-gate 40657c478bd9Sstevel@tonic-gate error: 4066108322fbScarlsonj lofs_discard_mnttab(); 40677c478bd9Sstevel@tonic-gate return (-1); 40687c478bd9Sstevel@tonic-gate } 4069