10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211645Scomay 220Sstevel@tonic-gate /* 23*3356Sgjelinek * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * This module contains functions used to bring up and tear down the 310Sstevel@tonic-gate * Virtual Platform: [un]mounting file-systems, [un]plumbing network 320Sstevel@tonic-gate * interfaces, [un]configuring devices, establishing resource controls, 330Sstevel@tonic-gate * and creating/destroying the zone in the kernel. These actions, on 340Sstevel@tonic-gate * the way up, ready the zone; on the way down, they halt the zone. 350Sstevel@tonic-gate * See the much longer block comment at the beginning of zoneadmd.c 360Sstevel@tonic-gate * for a bigger picture of how the whole program functions. 37766Scarlsonj * 38766Scarlsonj * This module also has primary responsibility for the layout of "scratch 39766Scarlsonj * zones." These are mounted, but inactive, zones that are used during 40766Scarlsonj * operating system upgrade and potentially other administrative action. The 41766Scarlsonj * scratch zone environment is similar to the miniroot environment. The zone's 42766Scarlsonj * actual root is mounted read-write on /a, and the standard paths (/usr, 43766Scarlsonj * /sbin, /lib) all lead to read-only copies of the running system's binaries. 44766Scarlsonj * This allows the administrative tools to manipulate the zone using "-R /a" 45766Scarlsonj * without relying on any binaries in the zone itself. 46766Scarlsonj * 47766Scarlsonj * If the scratch zone is on an alternate root (Live Upgrade [LU] boot 48766Scarlsonj * environment), then we must resolve the lofs mounts used there to uncover 49766Scarlsonj * writable (unshared) resources. Shared resources, though, are always 50766Scarlsonj * read-only. In addition, if the "same" zone with a different root path is 51766Scarlsonj * currently running, then "/b" inside the zone points to the running zone's 52766Scarlsonj * root. This allows LU to synchronize configuration files during the upgrade 53766Scarlsonj * process. 54766Scarlsonj * 55766Scarlsonj * To construct this environment, this module creates a tmpfs mount on 56766Scarlsonj * $ZONEPATH/lu. Inside this scratch area, the miniroot-like environment as 57766Scarlsonj * described above is constructed on the fly. The zone is then created using 58766Scarlsonj * $ZONEPATH/lu as the root. 59766Scarlsonj * 60766Scarlsonj * Note that scratch zones are inactive. The zone's bits are not running and 61766Scarlsonj * likely cannot be run correctly until upgrade is done. Init is not running 62766Scarlsonj * there, nor is SMF. Because of this, the "mounted" state of a scratch zone 63766Scarlsonj * is not a part of the usual halt/ready/boot state machine. 640Sstevel@tonic-gate */ 650Sstevel@tonic-gate 660Sstevel@tonic-gate #include <sys/param.h> 670Sstevel@tonic-gate #include <sys/mount.h> 680Sstevel@tonic-gate #include <sys/mntent.h> 690Sstevel@tonic-gate #include <sys/socket.h> 700Sstevel@tonic-gate #include <sys/utsname.h> 710Sstevel@tonic-gate #include <sys/types.h> 720Sstevel@tonic-gate #include <sys/stat.h> 730Sstevel@tonic-gate #include <sys/sockio.h> 740Sstevel@tonic-gate #include <sys/stropts.h> 750Sstevel@tonic-gate #include <sys/conf.h> 760Sstevel@tonic-gate 770Sstevel@tonic-gate #include <inet/tcp.h> 780Sstevel@tonic-gate #include <arpa/inet.h> 790Sstevel@tonic-gate #include <netinet/in.h> 800Sstevel@tonic-gate #include <net/route.h> 810Sstevel@tonic-gate 820Sstevel@tonic-gate #include <stdio.h> 830Sstevel@tonic-gate #include <errno.h> 840Sstevel@tonic-gate #include <fcntl.h> 850Sstevel@tonic-gate #include <unistd.h> 860Sstevel@tonic-gate #include <rctl.h> 870Sstevel@tonic-gate #include <stdlib.h> 880Sstevel@tonic-gate #include <string.h> 890Sstevel@tonic-gate #include <strings.h> 900Sstevel@tonic-gate #include <wait.h> 910Sstevel@tonic-gate #include <limits.h> 920Sstevel@tonic-gate #include <libgen.h> 93789Sahrens #include <libzfs.h> 942621Sllai1 #include <libdevinfo.h> 950Sstevel@tonic-gate #include <zone.h> 960Sstevel@tonic-gate #include <assert.h> 972303Scarlsonj #include <libcontract.h> 982303Scarlsonj #include <libcontract_priv.h> 992303Scarlsonj #include <uuid/uuid.h> 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate #include <sys/mntio.h> 1020Sstevel@tonic-gate #include <sys/mnttab.h> 1030Sstevel@tonic-gate #include <sys/fs/autofs.h> /* for _autofssys() */ 1040Sstevel@tonic-gate #include <sys/fs/lofs_info.h> 105789Sahrens #include <sys/fs/zfs.h> 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate #include <pool.h> 1080Sstevel@tonic-gate #include <sys/pool.h> 1093247Sgjelinek #include <sys/priocntl.h> 1100Sstevel@tonic-gate 1112712Snn35248 #include <libbrand.h> 1122712Snn35248 #include <sys/brand.h> 1130Sstevel@tonic-gate #include <libzonecfg.h> 1142170Sevanl #include <synch.h> 1152611Svp157776 1160Sstevel@tonic-gate #include "zoneadmd.h" 1171676Sjpk #include <tsol/label.h> 1181676Sjpk #include <libtsnet.h> 1191676Sjpk #include <sys/priv.h> 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate #define V4_ADDR_LEN 32 1220Sstevel@tonic-gate #define V6_ADDR_LEN 128 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate /* 0755 is the default directory mode. */ 1250Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 1260Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate #define IPD_DEFAULT_OPTS \ 1290Sstevel@tonic-gate MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate #define DFSTYPES "/etc/dfs/fstypes" 1321676Sjpk #define MAXTNZLEN 2048 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate /* for routing socket */ 1350Sstevel@tonic-gate static int rts_seqno = 0; 1360Sstevel@tonic-gate 137766Scarlsonj /* mangled zone name when mounting in an alternate root environment */ 138766Scarlsonj static char kernzone[ZONENAME_MAX]; 139766Scarlsonj 140766Scarlsonj /* array of cached mount entries for resolve_lofs */ 141766Scarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max; 142766Scarlsonj 1431676Sjpk /* for Trusted Extensions */ 1441676Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *); 1451676Sjpk static int tsol_mounts(zlog_t *, char *, char *); 1461676Sjpk static void tsol_unmounts(zlog_t *, char *); 1471676Sjpk static m_label_t *zlabel = NULL; 1481676Sjpk static m_label_t *zid_label = NULL; 1491676Sjpk static priv_set_t *zprivs = NULL; 1501676Sjpk 1510Sstevel@tonic-gate /* from libsocket, not in any header file */ 1520Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *); 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate /* 155766Scarlsonj * An optimization for build_mnttable: reallocate (and potentially copy the 156766Scarlsonj * data) only once every N times through the loop. 157766Scarlsonj */ 158766Scarlsonj #define MNTTAB_HUNK 32 159766Scarlsonj 160766Scarlsonj /* 1610Sstevel@tonic-gate * Private autofs system call 1620Sstevel@tonic-gate */ 1630Sstevel@tonic-gate extern int _autofssys(int, void *); 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate static int 1660Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid) 1670Sstevel@tonic-gate { 1680Sstevel@tonic-gate /* 1690Sstevel@tonic-gate * Ask autofs to unmount all trigger nodes in the given zone. 1700Sstevel@tonic-gate */ 1710Sstevel@tonic-gate return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid)); 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate 174766Scarlsonj static void 175766Scarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem) 176766Scarlsonj { 177766Scarlsonj uint_t i; 178766Scarlsonj 179766Scarlsonj if (mnt_array == NULL) 180766Scarlsonj return; 181766Scarlsonj for (i = 0; i < nelem; i++) { 182766Scarlsonj free(mnt_array[i].mnt_mountp); 183766Scarlsonj free(mnt_array[i].mnt_fstype); 184766Scarlsonj free(mnt_array[i].mnt_special); 185766Scarlsonj free(mnt_array[i].mnt_mntopts); 186766Scarlsonj assert(mnt_array[i].mnt_time == NULL); 187766Scarlsonj } 188766Scarlsonj free(mnt_array); 189766Scarlsonj } 190766Scarlsonj 191766Scarlsonj /* 192766Scarlsonj * Build the mount table for the zone rooted at "zroot", storing the resulting 193766Scarlsonj * array of struct mnttabs in "mnt_arrayp" and the number of elements in the 194766Scarlsonj * array in "nelemp". 195766Scarlsonj */ 196766Scarlsonj static int 197766Scarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab, 198766Scarlsonj struct mnttab **mnt_arrayp, uint_t *nelemp) 199766Scarlsonj { 200766Scarlsonj struct mnttab mnt; 201766Scarlsonj struct mnttab *mnts; 202766Scarlsonj struct mnttab *mnp; 203766Scarlsonj uint_t nmnt; 204766Scarlsonj 205766Scarlsonj rewind(mnttab); 206766Scarlsonj resetmnttab(mnttab); 207766Scarlsonj nmnt = 0; 208766Scarlsonj mnts = NULL; 209766Scarlsonj while (getmntent(mnttab, &mnt) == 0) { 210766Scarlsonj struct mnttab *tmp_array; 211766Scarlsonj 212766Scarlsonj if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0) 213766Scarlsonj continue; 214766Scarlsonj if (nmnt % MNTTAB_HUNK == 0) { 215766Scarlsonj tmp_array = realloc(mnts, 216766Scarlsonj (nmnt + MNTTAB_HUNK) * sizeof (*mnts)); 217766Scarlsonj if (tmp_array == NULL) { 218766Scarlsonj free_mnttable(mnts, nmnt); 219766Scarlsonj return (-1); 220766Scarlsonj } 221766Scarlsonj mnts = tmp_array; 222766Scarlsonj } 223766Scarlsonj mnp = &mnts[nmnt++]; 224766Scarlsonj 225766Scarlsonj /* 226766Scarlsonj * Zero out any fields we're not using. 227766Scarlsonj */ 228766Scarlsonj (void) memset(mnp, 0, sizeof (*mnp)); 229766Scarlsonj 230766Scarlsonj if (mnt.mnt_special != NULL) 231766Scarlsonj mnp->mnt_special = strdup(mnt.mnt_special); 232766Scarlsonj if (mnt.mnt_mntopts != NULL) 233766Scarlsonj mnp->mnt_mntopts = strdup(mnt.mnt_mntopts); 234766Scarlsonj mnp->mnt_mountp = strdup(mnt.mnt_mountp); 235766Scarlsonj mnp->mnt_fstype = strdup(mnt.mnt_fstype); 236766Scarlsonj if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) || 237766Scarlsonj (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) || 238766Scarlsonj mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) { 239766Scarlsonj zerror(zlogp, B_TRUE, "memory allocation failed"); 240766Scarlsonj free_mnttable(mnts, nmnt); 241766Scarlsonj return (-1); 242766Scarlsonj } 243766Scarlsonj } 244766Scarlsonj *mnt_arrayp = mnts; 245766Scarlsonj *nelemp = nmnt; 246766Scarlsonj return (0); 247766Scarlsonj } 248766Scarlsonj 249766Scarlsonj /* 250766Scarlsonj * This is an optimization. The resolve_lofs function is used quite frequently 251766Scarlsonj * to manipulate file paths, and on a machine with a large number of zones, 252766Scarlsonj * there will be a huge number of mounted file systems. Thus, we trigger a 253766Scarlsonj * reread of the list of mount points 254766Scarlsonj */ 255766Scarlsonj static void 256766Scarlsonj lofs_discard_mnttab(void) 257766Scarlsonj { 258766Scarlsonj free_mnttable(resolve_lofs_mnts, 259766Scarlsonj resolve_lofs_mnt_max - resolve_lofs_mnts); 260766Scarlsonj resolve_lofs_mnts = resolve_lofs_mnt_max = NULL; 261766Scarlsonj } 262766Scarlsonj 263766Scarlsonj static int 264766Scarlsonj lofs_read_mnttab(zlog_t *zlogp) 265766Scarlsonj { 266766Scarlsonj FILE *mnttab; 267766Scarlsonj uint_t nmnts; 268766Scarlsonj 269766Scarlsonj if ((mnttab = fopen(MNTTAB, "r")) == NULL) 270766Scarlsonj return (-1); 271766Scarlsonj if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts, 272766Scarlsonj &nmnts) == -1) { 273766Scarlsonj (void) fclose(mnttab); 274766Scarlsonj return (-1); 275766Scarlsonj } 276766Scarlsonj (void) fclose(mnttab); 277766Scarlsonj resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts; 278766Scarlsonj return (0); 279766Scarlsonj } 280766Scarlsonj 281766Scarlsonj /* 282766Scarlsonj * This function loops over potential loopback mounts and symlinks in a given 283766Scarlsonj * path and resolves them all down to an absolute path. 284766Scarlsonj */ 285766Scarlsonj static void 286766Scarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen) 287766Scarlsonj { 288766Scarlsonj int len, arlen; 289766Scarlsonj const char *altroot; 290766Scarlsonj char tmppath[MAXPATHLEN]; 291766Scarlsonj boolean_t outside_altroot; 292766Scarlsonj 293766Scarlsonj if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1) 294766Scarlsonj return; 295766Scarlsonj tmppath[len] = '\0'; 296766Scarlsonj (void) strlcpy(path, tmppath, sizeof (tmppath)); 297766Scarlsonj 298766Scarlsonj /* This happens once per zoneadmd operation. */ 299766Scarlsonj if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 300766Scarlsonj return; 301766Scarlsonj 302766Scarlsonj altroot = zonecfg_get_root(); 303766Scarlsonj arlen = strlen(altroot); 304766Scarlsonj outside_altroot = B_FALSE; 305766Scarlsonj for (;;) { 306766Scarlsonj struct mnttab *mnp; 307766Scarlsonj 3083079Sdminer /* Search in reverse order to find longest match */ 3093079Sdminer for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts; 3103079Sdminer mnp--) { 311766Scarlsonj if (mnp->mnt_fstype == NULL || 312766Scarlsonj mnp->mnt_mountp == NULL || 3133079Sdminer mnp->mnt_special == NULL) 314766Scarlsonj continue; 315766Scarlsonj len = strlen(mnp->mnt_mountp); 316766Scarlsonj if (strncmp(mnp->mnt_mountp, path, len) == 0 && 317766Scarlsonj (path[len] == '/' || path[len] == '\0')) 318766Scarlsonj break; 319766Scarlsonj } 3203079Sdminer if (mnp < resolve_lofs_mnts) 3213079Sdminer break; 3223079Sdminer /* If it's not a lofs then we're done */ 3233079Sdminer if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0) 324766Scarlsonj break; 325766Scarlsonj if (outside_altroot) { 326766Scarlsonj char *cp; 327766Scarlsonj int olen = sizeof (MNTOPT_RO) - 1; 328766Scarlsonj 329766Scarlsonj /* 330766Scarlsonj * If we run into a read-only mount outside of the 331766Scarlsonj * alternate root environment, then the user doesn't 332766Scarlsonj * want this path to be made read-write. 333766Scarlsonj */ 334766Scarlsonj if (mnp->mnt_mntopts != NULL && 335766Scarlsonj (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) != 336766Scarlsonj NULL && 337766Scarlsonj (cp == mnp->mnt_mntopts || cp[-1] == ',') && 338766Scarlsonj (cp[olen] == '\0' || cp[olen] == ',')) { 339766Scarlsonj break; 340766Scarlsonj } 341766Scarlsonj } else if (arlen > 0 && 342766Scarlsonj (strncmp(mnp->mnt_special, altroot, arlen) != 0 || 343766Scarlsonj (mnp->mnt_special[arlen] != '\0' && 344766Scarlsonj mnp->mnt_special[arlen] != '/'))) { 345766Scarlsonj outside_altroot = B_TRUE; 346766Scarlsonj } 347766Scarlsonj /* use temporary buffer because new path might be longer */ 348766Scarlsonj (void) snprintf(tmppath, sizeof (tmppath), "%s%s", 349766Scarlsonj mnp->mnt_special, path + len); 350766Scarlsonj if ((len = resolvepath(tmppath, path, pathlen)) == -1) 351766Scarlsonj break; 352766Scarlsonj path[len] = '\0'; 353766Scarlsonj } 354766Scarlsonj } 355766Scarlsonj 356766Scarlsonj /* 357766Scarlsonj * For a regular mount, check if a replacement lofs mount is needed because the 358766Scarlsonj * referenced device is already mounted somewhere. 359766Scarlsonj */ 360766Scarlsonj static int 361766Scarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr) 362766Scarlsonj { 363766Scarlsonj struct mnttab *mnp; 364766Scarlsonj zone_fsopt_t *optptr, *onext; 365766Scarlsonj 366766Scarlsonj /* This happens once per zoneadmd operation. */ 367766Scarlsonj if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 368766Scarlsonj return (-1); 369766Scarlsonj 370766Scarlsonj /* 371766Scarlsonj * If this special node isn't already in use, then it's ours alone; 372766Scarlsonj * no need to worry about conflicting mounts. 373766Scarlsonj */ 374766Scarlsonj for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; 375766Scarlsonj mnp++) { 376766Scarlsonj if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0) 377766Scarlsonj break; 378766Scarlsonj } 379766Scarlsonj if (mnp >= resolve_lofs_mnt_max) 380766Scarlsonj return (0); 381766Scarlsonj 382766Scarlsonj /* 383766Scarlsonj * Convert this duplicate mount into a lofs mount. 384766Scarlsonj */ 385766Scarlsonj (void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp, 386766Scarlsonj sizeof (fsptr->zone_fs_special)); 387766Scarlsonj (void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS, 388766Scarlsonj sizeof (fsptr->zone_fs_type)); 389766Scarlsonj fsptr->zone_fs_raw[0] = '\0'; 390766Scarlsonj 391766Scarlsonj /* 392766Scarlsonj * Discard all but one of the original options and set that to be the 393766Scarlsonj * same set of options used for inherit package directory resources. 394766Scarlsonj */ 395766Scarlsonj optptr = fsptr->zone_fs_options; 396766Scarlsonj if (optptr == NULL) { 397766Scarlsonj optptr = malloc(sizeof (*optptr)); 398766Scarlsonj if (optptr == NULL) { 399766Scarlsonj zerror(zlogp, B_TRUE, "cannot mount %s", 400766Scarlsonj fsptr->zone_fs_dir); 401766Scarlsonj return (-1); 402766Scarlsonj } 403766Scarlsonj } else { 404766Scarlsonj while ((onext = optptr->zone_fsopt_next) != NULL) { 405766Scarlsonj optptr->zone_fsopt_next = onext->zone_fsopt_next; 406766Scarlsonj free(onext); 407766Scarlsonj } 408766Scarlsonj } 409766Scarlsonj (void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS); 410766Scarlsonj optptr->zone_fsopt_next = NULL; 411766Scarlsonj fsptr->zone_fs_options = optptr; 412766Scarlsonj return (0); 413766Scarlsonj } 414766Scarlsonj 4150Sstevel@tonic-gate static int 4160Sstevel@tonic-gate make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode) 4170Sstevel@tonic-gate { 4180Sstevel@tonic-gate char path[MAXPATHLEN]; 4190Sstevel@tonic-gate struct stat st; 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) > 4220Sstevel@tonic-gate sizeof (path)) { 4230Sstevel@tonic-gate zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix, 4240Sstevel@tonic-gate subdir); 4250Sstevel@tonic-gate return (-1); 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate if (lstat(path, &st) == 0) { 4290Sstevel@tonic-gate /* 4300Sstevel@tonic-gate * We don't check the file mode since presumably the zone 4310Sstevel@tonic-gate * administrator may have had good reason to change the mode, 4320Sstevel@tonic-gate * and we don't need to second guess him. 4330Sstevel@tonic-gate */ 4340Sstevel@tonic-gate if (!S_ISDIR(st.st_mode)) { 4351676Sjpk if (is_system_labeled() && 4361676Sjpk S_ISREG(st.st_mode)) { 4371676Sjpk /* 4381676Sjpk * The need to mount readonly copies of 4391676Sjpk * global zone /etc/ files is unique to 4401676Sjpk * Trusted Extensions. 4411676Sjpk */ 4421676Sjpk if (strncmp(subdir, "/etc/", 4431676Sjpk strlen("/etc/")) != 0) { 4441676Sjpk zerror(zlogp, B_FALSE, 4451676Sjpk "%s is not in /etc", path); 4461676Sjpk return (-1); 4471676Sjpk } 4481676Sjpk } else { 4491676Sjpk zerror(zlogp, B_FALSE, 4501676Sjpk "%s is not a directory", path); 4511676Sjpk return (-1); 4521676Sjpk } 4530Sstevel@tonic-gate } 4540Sstevel@tonic-gate } else if (mkdirp(path, mode) != 0) { 4550Sstevel@tonic-gate if (errno == EROFS) 4560Sstevel@tonic-gate zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on " 4570Sstevel@tonic-gate "a read-only file system in this local zone.\nMake " 4580Sstevel@tonic-gate "sure %s exists in the global zone.", path, subdir); 4590Sstevel@tonic-gate else 4600Sstevel@tonic-gate zerror(zlogp, B_TRUE, "mkdirp of %s failed", path); 4610Sstevel@tonic-gate return (-1); 4620Sstevel@tonic-gate } 4630Sstevel@tonic-gate return (0); 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate static void 4670Sstevel@tonic-gate free_remote_fstypes(char **types) 4680Sstevel@tonic-gate { 4690Sstevel@tonic-gate uint_t i; 4700Sstevel@tonic-gate 4710Sstevel@tonic-gate if (types == NULL) 4720Sstevel@tonic-gate return; 4730Sstevel@tonic-gate for (i = 0; types[i] != NULL; i++) 4740Sstevel@tonic-gate free(types[i]); 4750Sstevel@tonic-gate free(types); 4760Sstevel@tonic-gate } 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate static char ** 4790Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp) 4800Sstevel@tonic-gate { 4810Sstevel@tonic-gate char **types = NULL; 4820Sstevel@tonic-gate FILE *fp; 4830Sstevel@tonic-gate char buf[MAXPATHLEN]; 4840Sstevel@tonic-gate char fstype[MAXPATHLEN]; 4850Sstevel@tonic-gate uint_t lines = 0; 4860Sstevel@tonic-gate uint_t i; 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate if ((fp = fopen(DFSTYPES, "r")) == NULL) { 4890Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES); 4900Sstevel@tonic-gate return (NULL); 4910Sstevel@tonic-gate } 4920Sstevel@tonic-gate /* 4930Sstevel@tonic-gate * Count the number of lines 4940Sstevel@tonic-gate */ 4950Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fp) != NULL) 4960Sstevel@tonic-gate lines++; 4970Sstevel@tonic-gate if (lines == 0) /* didn't read anything; empty file */ 4980Sstevel@tonic-gate goto out; 4990Sstevel@tonic-gate rewind(fp); 5000Sstevel@tonic-gate /* 5010Sstevel@tonic-gate * Allocate enough space for a NULL-terminated array. 5020Sstevel@tonic-gate */ 5030Sstevel@tonic-gate types = calloc(lines + 1, sizeof (char *)); 5040Sstevel@tonic-gate if (types == NULL) { 5050Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 5060Sstevel@tonic-gate goto out; 5070Sstevel@tonic-gate } 5080Sstevel@tonic-gate i = 0; 5090Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fp) != NULL) { 5100Sstevel@tonic-gate /* LINTED - fstype is big enough to hold buf */ 5110Sstevel@tonic-gate if (sscanf(buf, "%s", fstype) == 0) { 5120Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES); 5130Sstevel@tonic-gate free_remote_fstypes(types); 5140Sstevel@tonic-gate types = NULL; 5150Sstevel@tonic-gate goto out; 5160Sstevel@tonic-gate } 5170Sstevel@tonic-gate types[i] = strdup(fstype); 5180Sstevel@tonic-gate if (types[i] == NULL) { 5190Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 5200Sstevel@tonic-gate free_remote_fstypes(types); 5210Sstevel@tonic-gate types = NULL; 5220Sstevel@tonic-gate goto out; 5230Sstevel@tonic-gate } 5240Sstevel@tonic-gate i++; 5250Sstevel@tonic-gate } 5260Sstevel@tonic-gate out: 5270Sstevel@tonic-gate (void) fclose(fp); 5280Sstevel@tonic-gate return (types); 5290Sstevel@tonic-gate } 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate static boolean_t 5320Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes) 5330Sstevel@tonic-gate { 5340Sstevel@tonic-gate uint_t i; 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate if (remote_fstypes == NULL) 5370Sstevel@tonic-gate return (B_FALSE); 5380Sstevel@tonic-gate for (i = 0; remote_fstypes[i] != NULL; i++) { 5390Sstevel@tonic-gate if (strcmp(remote_fstypes[i], fstype) == 0) 5400Sstevel@tonic-gate return (B_TRUE); 5410Sstevel@tonic-gate } 5420Sstevel@tonic-gate return (B_FALSE); 5430Sstevel@tonic-gate } 5440Sstevel@tonic-gate 545766Scarlsonj /* 546766Scarlsonj * This converts a zone root path (normally of the form .../root) to a Live 547766Scarlsonj * Upgrade scratch zone root (of the form .../lu). 548766Scarlsonj */ 5490Sstevel@tonic-gate static void 550766Scarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved) 5510Sstevel@tonic-gate { 5522712Snn35248 assert(zone_isnative); 5532712Snn35248 554766Scarlsonj if (!isresolved && zonecfg_in_alt_root()) 555766Scarlsonj resolve_lofs(zlogp, zroot, zrootlen); 556766Scarlsonj (void) strcpy(strrchr(zroot, '/') + 1, "lu"); 5570Sstevel@tonic-gate } 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate /* 5600Sstevel@tonic-gate * The general strategy for unmounting filesystems is as follows: 5610Sstevel@tonic-gate * 5620Sstevel@tonic-gate * - Remote filesystems may be dead, and attempting to contact them as 5630Sstevel@tonic-gate * part of a regular unmount may hang forever; we want to always try to 5640Sstevel@tonic-gate * forcibly unmount such filesystems and only fall back to regular 5650Sstevel@tonic-gate * unmounts if the filesystem doesn't support forced unmounts. 5660Sstevel@tonic-gate * 5670Sstevel@tonic-gate * - We don't want to unnecessarily corrupt metadata on local 5680Sstevel@tonic-gate * filesystems (ie UFS), so we want to start off with graceful unmounts, 5690Sstevel@tonic-gate * and only escalate to doing forced unmounts if we get stuck. 5700Sstevel@tonic-gate * 5710Sstevel@tonic-gate * We start off walking backwards through the mount table. This doesn't 5720Sstevel@tonic-gate * give us strict ordering but ensures that we try to unmount submounts 5730Sstevel@tonic-gate * first. We thus limit the number of failed umount2(2) calls. 5740Sstevel@tonic-gate * 5750Sstevel@tonic-gate * The mechanism for determining if we're stuck is to count the number 5760Sstevel@tonic-gate * of failed unmounts each iteration through the mount table. This 5770Sstevel@tonic-gate * gives us an upper bound on the number of filesystems which remain 5780Sstevel@tonic-gate * mounted (autofs trigger nodes are dealt with separately). If at the 5790Sstevel@tonic-gate * end of one unmount+autofs_cleanup cycle we still have the same number 5800Sstevel@tonic-gate * of mounts that we started out with, we're stuck and try a forced 5810Sstevel@tonic-gate * unmount. If that fails (filesystem doesn't support forced unmounts) 5820Sstevel@tonic-gate * then we bail and are unable to teardown the zone. If it succeeds, 5830Sstevel@tonic-gate * we're no longer stuck so we continue with our policy of trying 5840Sstevel@tonic-gate * graceful mounts first. 5850Sstevel@tonic-gate * 5860Sstevel@tonic-gate * Zone must be down (ie, no processes or threads active). 5870Sstevel@tonic-gate */ 5880Sstevel@tonic-gate static int 589766Scarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd) 5900Sstevel@tonic-gate { 5910Sstevel@tonic-gate int error = 0; 5920Sstevel@tonic-gate FILE *mnttab; 5930Sstevel@tonic-gate struct mnttab *mnts; 5940Sstevel@tonic-gate uint_t nmnt; 5950Sstevel@tonic-gate char zroot[MAXPATHLEN + 1]; 5960Sstevel@tonic-gate size_t zrootlen; 5970Sstevel@tonic-gate uint_t oldcount = UINT_MAX; 5980Sstevel@tonic-gate boolean_t stuck = B_FALSE; 5990Sstevel@tonic-gate char **remote_fstypes = NULL; 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 6020Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to determine zone root"); 6030Sstevel@tonic-gate return (-1); 6040Sstevel@tonic-gate } 605766Scarlsonj if (unmount_cmd) 606766Scarlsonj root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE); 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate (void) strcat(zroot, "/"); 6090Sstevel@tonic-gate zrootlen = strlen(zroot); 6100Sstevel@tonic-gate 6111676Sjpk /* 6121676Sjpk * For Trusted Extensions unmount each higher level zone's mount 6131676Sjpk * of our zone's /export/home 6141676Sjpk */ 6151769Scarlsonj if (!unmount_cmd) 6161769Scarlsonj tsol_unmounts(zlogp, zone_name); 6171676Sjpk 6180Sstevel@tonic-gate if ((mnttab = fopen(MNTTAB, "r")) == NULL) { 6190Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB); 6200Sstevel@tonic-gate return (-1); 6210Sstevel@tonic-gate } 6220Sstevel@tonic-gate /* 6230Sstevel@tonic-gate * Use our hacky mntfs ioctl so we see everything, even mounts with 6240Sstevel@tonic-gate * MS_NOMNTTAB. 6250Sstevel@tonic-gate */ 6260Sstevel@tonic-gate if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) { 6270Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB); 6280Sstevel@tonic-gate error++; 6290Sstevel@tonic-gate goto out; 6300Sstevel@tonic-gate } 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate /* 6330Sstevel@tonic-gate * Build the list of remote fstypes so we know which ones we 6340Sstevel@tonic-gate * should forcibly unmount. 6350Sstevel@tonic-gate */ 6360Sstevel@tonic-gate remote_fstypes = get_remote_fstypes(zlogp); 6370Sstevel@tonic-gate for (; /* ever */; ) { 6380Sstevel@tonic-gate uint_t newcount = 0; 6390Sstevel@tonic-gate boolean_t unmounted; 6400Sstevel@tonic-gate struct mnttab *mnp; 6410Sstevel@tonic-gate char *path; 6420Sstevel@tonic-gate uint_t i; 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate mnts = NULL; 6450Sstevel@tonic-gate nmnt = 0; 6460Sstevel@tonic-gate /* 6470Sstevel@tonic-gate * MNTTAB gives us a way to walk through mounted 6480Sstevel@tonic-gate * filesystems; we need to be able to walk them in 6490Sstevel@tonic-gate * reverse order, so we build a list of all mounted 6500Sstevel@tonic-gate * filesystems. 6510Sstevel@tonic-gate */ 6520Sstevel@tonic-gate if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts, 6530Sstevel@tonic-gate &nmnt) != 0) { 6540Sstevel@tonic-gate error++; 6550Sstevel@tonic-gate goto out; 6560Sstevel@tonic-gate } 6570Sstevel@tonic-gate for (i = 0; i < nmnt; i++) { 6580Sstevel@tonic-gate mnp = &mnts[nmnt - i - 1]; /* access in reverse order */ 6590Sstevel@tonic-gate path = mnp->mnt_mountp; 6600Sstevel@tonic-gate unmounted = B_FALSE; 6610Sstevel@tonic-gate /* 6620Sstevel@tonic-gate * Try forced unmount first for remote filesystems. 6630Sstevel@tonic-gate * 6640Sstevel@tonic-gate * Not all remote filesystems support forced unmounts, 6650Sstevel@tonic-gate * so if this fails (ENOTSUP) we'll continue on 6660Sstevel@tonic-gate * and try a regular unmount. 6670Sstevel@tonic-gate */ 6680Sstevel@tonic-gate if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) { 6690Sstevel@tonic-gate if (umount2(path, MS_FORCE) == 0) 6700Sstevel@tonic-gate unmounted = B_TRUE; 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate /* 6730Sstevel@tonic-gate * Try forced unmount if we're stuck. 6740Sstevel@tonic-gate */ 6750Sstevel@tonic-gate if (stuck) { 6760Sstevel@tonic-gate if (umount2(path, MS_FORCE) == 0) { 6770Sstevel@tonic-gate unmounted = B_TRUE; 6780Sstevel@tonic-gate stuck = B_FALSE; 6790Sstevel@tonic-gate } else { 6800Sstevel@tonic-gate /* 6810Sstevel@tonic-gate * The first failure indicates a 6820Sstevel@tonic-gate * mount we won't be able to get 6830Sstevel@tonic-gate * rid of automatically, so we 6840Sstevel@tonic-gate * bail. 6850Sstevel@tonic-gate */ 6860Sstevel@tonic-gate error++; 6870Sstevel@tonic-gate zerror(zlogp, B_FALSE, 6880Sstevel@tonic-gate "unable to unmount '%s'", path); 6890Sstevel@tonic-gate free_mnttable(mnts, nmnt); 6900Sstevel@tonic-gate goto out; 6910Sstevel@tonic-gate } 6920Sstevel@tonic-gate } 6930Sstevel@tonic-gate /* 6940Sstevel@tonic-gate * Try regular unmounts for everything else. 6950Sstevel@tonic-gate */ 6960Sstevel@tonic-gate if (!unmounted && umount2(path, 0) != 0) 6970Sstevel@tonic-gate newcount++; 6980Sstevel@tonic-gate } 6990Sstevel@tonic-gate free_mnttable(mnts, nmnt); 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate if (newcount == 0) 7020Sstevel@tonic-gate break; 7030Sstevel@tonic-gate if (newcount >= oldcount) { 7040Sstevel@tonic-gate /* 7050Sstevel@tonic-gate * Last round didn't unmount anything; we're stuck and 7060Sstevel@tonic-gate * should start trying forced unmounts. 7070Sstevel@tonic-gate */ 7080Sstevel@tonic-gate stuck = B_TRUE; 7090Sstevel@tonic-gate } 7100Sstevel@tonic-gate oldcount = newcount; 7110Sstevel@tonic-gate 7120Sstevel@tonic-gate /* 7130Sstevel@tonic-gate * Autofs doesn't let you unmount its trigger nodes from 7140Sstevel@tonic-gate * userland so we have to tell the kernel to cleanup for us. 7150Sstevel@tonic-gate */ 7160Sstevel@tonic-gate if (autofs_cleanup(zoneid) != 0) { 7170Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to remove autofs nodes"); 7180Sstevel@tonic-gate error++; 7190Sstevel@tonic-gate goto out; 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate } 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate out: 7240Sstevel@tonic-gate free_remote_fstypes(remote_fstypes); 7250Sstevel@tonic-gate (void) fclose(mnttab); 7260Sstevel@tonic-gate return (error ? -1 : 0); 7270Sstevel@tonic-gate } 7280Sstevel@tonic-gate 7290Sstevel@tonic-gate static int 7300Sstevel@tonic-gate fs_compare(const void *m1, const void *m2) 7310Sstevel@tonic-gate { 7320Sstevel@tonic-gate struct zone_fstab *i = (struct zone_fstab *)m1; 7330Sstevel@tonic-gate struct zone_fstab *j = (struct zone_fstab *)m2; 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate return (strcmp(i->zone_fs_dir, j->zone_fs_dir)); 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate /* 7390Sstevel@tonic-gate * Fork and exec (and wait for) the mentioned binary with the provided 7400Sstevel@tonic-gate * arguments. Returns (-1) if something went wrong with fork(2) or exec(2), 7410Sstevel@tonic-gate * returns the exit status otherwise. 7420Sstevel@tonic-gate * 7430Sstevel@tonic-gate * If we were unable to exec the provided pathname (for whatever 7440Sstevel@tonic-gate * reason), we return the special token ZEXIT_EXEC. The current value 7450Sstevel@tonic-gate * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the 7460Sstevel@tonic-gate * consumers of this function; any future consumers must make sure this 7470Sstevel@tonic-gate * remains the case. 7480Sstevel@tonic-gate */ 7490Sstevel@tonic-gate static int 7500Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[]) 7510Sstevel@tonic-gate { 7520Sstevel@tonic-gate pid_t child_pid; 7530Sstevel@tonic-gate int child_status = 0; 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate /* 7560Sstevel@tonic-gate * Do not let another thread localize a message while we are forking. 7570Sstevel@tonic-gate */ 7580Sstevel@tonic-gate (void) mutex_lock(&msglock); 7590Sstevel@tonic-gate child_pid = fork(); 7600Sstevel@tonic-gate (void) mutex_unlock(&msglock); 7610Sstevel@tonic-gate if (child_pid == -1) { 7620Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]); 7630Sstevel@tonic-gate return (-1); 7640Sstevel@tonic-gate } else if (child_pid == 0) { 7650Sstevel@tonic-gate closefrom(0); 7661915Sgjelinek /* redirect stdin, stdout & stderr to /dev/null */ 7671915Sgjelinek (void) open("/dev/null", O_RDONLY); /* stdin */ 7681915Sgjelinek (void) open("/dev/null", O_WRONLY); /* stdout */ 7691915Sgjelinek (void) open("/dev/null", O_WRONLY); /* stderr */ 7700Sstevel@tonic-gate (void) execv(path, argv); 7710Sstevel@tonic-gate /* 7720Sstevel@tonic-gate * Since we are in the child, there is no point calling zerror() 7730Sstevel@tonic-gate * since there is nobody waiting to consume it. So exit with a 7740Sstevel@tonic-gate * special code that the parent will recognize and call zerror() 7750Sstevel@tonic-gate * accordingly. 7760Sstevel@tonic-gate */ 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate _exit(ZEXIT_EXEC); 7790Sstevel@tonic-gate } else { 7800Sstevel@tonic-gate (void) waitpid(child_pid, &child_status, 0); 7810Sstevel@tonic-gate } 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate if (WIFSIGNALED(child_status)) { 7840Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to " 7850Sstevel@tonic-gate "signal %d", path, WTERMSIG(child_status)); 7860Sstevel@tonic-gate return (-1); 7870Sstevel@tonic-gate } 7880Sstevel@tonic-gate assert(WIFEXITED(child_status)); 7890Sstevel@tonic-gate if (WEXITSTATUS(child_status) == ZEXIT_EXEC) { 7900Sstevel@tonic-gate zerror(zlogp, B_FALSE, "failed to exec %s", path); 7910Sstevel@tonic-gate return (-1); 7920Sstevel@tonic-gate } 7930Sstevel@tonic-gate return (WEXITSTATUS(child_status)); 7940Sstevel@tonic-gate } 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate static int 7970Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev) 7980Sstevel@tonic-gate { 7990Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 8000Sstevel@tonic-gate char *argv[4]; 8010Sstevel@tonic-gate int status; 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate /* 8040Sstevel@tonic-gate * We could alternatively have called /usr/sbin/fsck -F <fstype>, but 8050Sstevel@tonic-gate * that would cost us an extra fork/exec without buying us anything. 8060Sstevel@tonic-gate */ 8070Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype) 8082712Snn35248 >= sizeof (cmdbuf)) { 8090Sstevel@tonic-gate zerror(zlogp, B_FALSE, "file-system type %s too long", fstype); 8100Sstevel@tonic-gate return (-1); 8110Sstevel@tonic-gate } 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate argv[0] = "fsck"; 8140Sstevel@tonic-gate argv[1] = "-m"; 8150Sstevel@tonic-gate argv[2] = (char *)rawdev; 8160Sstevel@tonic-gate argv[3] = NULL; 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate status = forkexec(zlogp, cmdbuf, argv); 8190Sstevel@tonic-gate if (status == 0 || status == -1) 8200Sstevel@tonic-gate return (status); 8210Sstevel@tonic-gate zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; " 8220Sstevel@tonic-gate "run fsck manually", rawdev, status); 8230Sstevel@tonic-gate return (-1); 8240Sstevel@tonic-gate } 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate static int 8270Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts, 8280Sstevel@tonic-gate const char *special, const char *directory) 8290Sstevel@tonic-gate { 8300Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 8310Sstevel@tonic-gate char *argv[6]; 8320Sstevel@tonic-gate int status; 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate /* 8350Sstevel@tonic-gate * We could alternatively have called /usr/sbin/mount -F <fstype>, but 8360Sstevel@tonic-gate * that would cost us an extra fork/exec without buying us anything. 8370Sstevel@tonic-gate */ 8380Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype) 8392712Snn35248 >= sizeof (cmdbuf)) { 8400Sstevel@tonic-gate zerror(zlogp, B_FALSE, "file-system type %s too long", fstype); 8410Sstevel@tonic-gate return (-1); 8420Sstevel@tonic-gate } 8430Sstevel@tonic-gate argv[0] = "mount"; 8440Sstevel@tonic-gate if (opts[0] == '\0') { 8450Sstevel@tonic-gate argv[1] = (char *)special; 8460Sstevel@tonic-gate argv[2] = (char *)directory; 8470Sstevel@tonic-gate argv[3] = NULL; 8480Sstevel@tonic-gate } else { 8490Sstevel@tonic-gate argv[1] = "-o"; 8500Sstevel@tonic-gate argv[2] = (char *)opts; 8510Sstevel@tonic-gate argv[3] = (char *)special; 8520Sstevel@tonic-gate argv[4] = (char *)directory; 8530Sstevel@tonic-gate argv[5] = NULL; 8540Sstevel@tonic-gate } 8550Sstevel@tonic-gate 8560Sstevel@tonic-gate status = forkexec(zlogp, cmdbuf, argv); 8570Sstevel@tonic-gate if (status == 0 || status == -1) 8580Sstevel@tonic-gate return (status); 8590Sstevel@tonic-gate if (opts[0] == '\0') 8600Sstevel@tonic-gate zerror(zlogp, B_FALSE, "\"%s %s %s\" " 8610Sstevel@tonic-gate "failed with exit code %d", 8620Sstevel@tonic-gate cmdbuf, special, directory, status); 8630Sstevel@tonic-gate else 8640Sstevel@tonic-gate zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" " 8650Sstevel@tonic-gate "failed with exit code %d", 8660Sstevel@tonic-gate cmdbuf, opts, special, directory, status); 8670Sstevel@tonic-gate return (-1); 8680Sstevel@tonic-gate } 8690Sstevel@tonic-gate 8700Sstevel@tonic-gate /* 8710Sstevel@tonic-gate * Make sure if a given path exists, it is not a sym-link, and is a directory. 8720Sstevel@tonic-gate */ 8730Sstevel@tonic-gate static int 8740Sstevel@tonic-gate check_path(zlog_t *zlogp, const char *path) 8750Sstevel@tonic-gate { 8760Sstevel@tonic-gate struct stat statbuf; 8770Sstevel@tonic-gate char respath[MAXPATHLEN]; 8780Sstevel@tonic-gate int res; 8790Sstevel@tonic-gate 8800Sstevel@tonic-gate if (lstat(path, &statbuf) != 0) { 8810Sstevel@tonic-gate if (errno == ENOENT) 8820Sstevel@tonic-gate return (0); 8830Sstevel@tonic-gate zerror(zlogp, B_TRUE, "can't stat %s", path); 8840Sstevel@tonic-gate return (-1); 8850Sstevel@tonic-gate } 8860Sstevel@tonic-gate if (S_ISLNK(statbuf.st_mode)) { 8870Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is a symlink", path); 8880Sstevel@tonic-gate return (-1); 8890Sstevel@tonic-gate } 8900Sstevel@tonic-gate if (!S_ISDIR(statbuf.st_mode)) { 8911676Sjpk if (is_system_labeled() && S_ISREG(statbuf.st_mode)) { 8921676Sjpk /* 8931676Sjpk * The need to mount readonly copies of 8941676Sjpk * global zone /etc/ files is unique to 8951676Sjpk * Trusted Extensions. 8961676Sjpk * The check for /etc/ via strstr() is to 8971676Sjpk * allow paths like $ZONEROOT/etc/passwd 8981676Sjpk */ 8991676Sjpk if (strstr(path, "/etc/") == NULL) { 9001676Sjpk zerror(zlogp, B_FALSE, 9011676Sjpk "%s is not in /etc", path); 9021676Sjpk return (-1); 9031676Sjpk } 9041676Sjpk } else { 9051676Sjpk zerror(zlogp, B_FALSE, "%s is not a directory", path); 9061676Sjpk return (-1); 9071676Sjpk } 9080Sstevel@tonic-gate } 9090Sstevel@tonic-gate if ((res = resolvepath(path, respath, sizeof (respath))) == -1) { 9100Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to resolve path %s", path); 9110Sstevel@tonic-gate return (-1); 9120Sstevel@tonic-gate } 9130Sstevel@tonic-gate respath[res] = '\0'; 9140Sstevel@tonic-gate if (strcmp(path, respath) != 0) { 9150Sstevel@tonic-gate /* 9160Sstevel@tonic-gate * We don't like ".."s and "."s throwing us off 9170Sstevel@tonic-gate */ 9180Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not a canonical path", path); 9190Sstevel@tonic-gate return (-1); 9200Sstevel@tonic-gate } 9210Sstevel@tonic-gate return (0); 9220Sstevel@tonic-gate } 9230Sstevel@tonic-gate 9240Sstevel@tonic-gate /* 9250Sstevel@tonic-gate * Check every component of rootpath/relpath. If any component fails (ie, 9260Sstevel@tonic-gate * exists but isn't the canonical path to a directory), it is returned in 9270Sstevel@tonic-gate * badpath, which is assumed to be at least of size MAXPATHLEN. 9280Sstevel@tonic-gate * 9290Sstevel@tonic-gate * Relpath must begin with '/'. 9300Sstevel@tonic-gate */ 9310Sstevel@tonic-gate static boolean_t 9320Sstevel@tonic-gate valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *relpath) 9330Sstevel@tonic-gate { 9340Sstevel@tonic-gate char abspath[MAXPATHLEN], *slashp; 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate /* 9370Sstevel@tonic-gate * Make sure abspath has at least one '/' after its rootpath 9380Sstevel@tonic-gate * component, and ends with '/'. 9390Sstevel@tonic-gate */ 9402712Snn35248 if (snprintf(abspath, sizeof (abspath), "%s%s/", rootpath, relpath) >= 9410Sstevel@tonic-gate sizeof (abspath)) { 9420Sstevel@tonic-gate zerror(zlogp, B_FALSE, "pathname %s%s is too long", rootpath, 9430Sstevel@tonic-gate relpath); 9440Sstevel@tonic-gate return (B_FALSE); 9450Sstevel@tonic-gate } 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate slashp = &abspath[strlen(rootpath)]; 9480Sstevel@tonic-gate assert(*slashp == '/'); 9490Sstevel@tonic-gate do { 9500Sstevel@tonic-gate *slashp = '\0'; 9510Sstevel@tonic-gate if (check_path(zlogp, abspath) != 0) 9520Sstevel@tonic-gate return (B_FALSE); 9530Sstevel@tonic-gate *slashp = '/'; 9540Sstevel@tonic-gate slashp++; 9550Sstevel@tonic-gate } while ((slashp = strchr(slashp, '/')) != NULL); 9560Sstevel@tonic-gate return (B_TRUE); 9570Sstevel@tonic-gate } 9580Sstevel@tonic-gate 9590Sstevel@tonic-gate static int 9602712Snn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name) 9612712Snn35248 { 9622712Snn35248 di_prof_t prof = arg; 9632712Snn35248 9642712Snn35248 if (name == NULL) 9652712Snn35248 return (di_prof_add_dev(prof, match)); 9662712Snn35248 return (di_prof_add_map(prof, match, name)); 9672712Snn35248 } 9682712Snn35248 9692712Snn35248 static int 9702712Snn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target) 9712712Snn35248 { 9722712Snn35248 di_prof_t prof = arg; 9732712Snn35248 9742712Snn35248 return (di_prof_add_symlink(prof, source, target)); 9752712Snn35248 } 9762712Snn35248 9772712Snn35248 /* 9782712Snn35248 * Apply the standard lists of devices/symlinks/mappings and the user-specified 9792712Snn35248 * list of devices (via zonecfg) to the /dev filesystem. The filesystem will 9802712Snn35248 * use these as a profile/filter to determine what exists in /dev. 9812712Snn35248 */ 9822712Snn35248 static int 9832712Snn35248 mount_one_dev(zlog_t *zlogp, char *devpath) 9842712Snn35248 { 9852712Snn35248 char brand[MAXNAMELEN]; 9862712Snn35248 zone_dochandle_t handle = NULL; 9872727Sedp brand_handle_t bh = NULL; 9882712Snn35248 struct zone_devtab ztab; 9892712Snn35248 di_prof_t prof = NULL; 9902712Snn35248 int err; 9912712Snn35248 int retval = -1; 9922712Snn35248 9932712Snn35248 if (di_prof_init(devpath, &prof)) { 9942712Snn35248 zerror(zlogp, B_TRUE, "failed to initialize profile"); 9952712Snn35248 goto cleanup; 9962712Snn35248 } 9972712Snn35248 9982712Snn35248 /* Get a handle to the brand info for this zone */ 9992712Snn35248 if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) || 10002727Sedp (bh = brand_open(brand)) == NULL) { 10012712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 10022712Snn35248 goto cleanup; 10032712Snn35248 } 10042712Snn35248 10052727Sedp if (brand_platform_iter_devices(bh, zone_name, 10062712Snn35248 mount_one_dev_device_cb, prof) != 0) { 10072712Snn35248 zerror(zlogp, B_TRUE, "failed to add standard device"); 10082712Snn35248 goto cleanup; 10092712Snn35248 } 10102712Snn35248 10112727Sedp if (brand_platform_iter_link(bh, 10122712Snn35248 mount_one_dev_symlink_cb, prof) != 0) { 10132712Snn35248 zerror(zlogp, B_TRUE, "failed to add standard symlink"); 10142712Snn35248 goto cleanup; 10152712Snn35248 } 10162712Snn35248 10172712Snn35248 /* Add user-specified devices and directories */ 10182712Snn35248 if ((handle = zonecfg_init_handle()) == NULL) { 10192712Snn35248 zerror(zlogp, B_FALSE, "can't initialize zone handle"); 10202712Snn35248 goto cleanup; 10212712Snn35248 } 10222712Snn35248 if (err = zonecfg_get_handle(zone_name, handle)) { 10232712Snn35248 zerror(zlogp, B_FALSE, "can't get handle for zone " 10242712Snn35248 "%s: %s", zone_name, zonecfg_strerror(err)); 10252712Snn35248 goto cleanup; 10262712Snn35248 } 10272712Snn35248 if (err = zonecfg_setdevent(handle)) { 10282712Snn35248 zerror(zlogp, B_FALSE, "%s: %s", zone_name, 10292712Snn35248 zonecfg_strerror(err)); 10302712Snn35248 goto cleanup; 10312712Snn35248 } 10322712Snn35248 while (zonecfg_getdevent(handle, &ztab) == Z_OK) { 10332712Snn35248 if (di_prof_add_dev(prof, ztab.zone_dev_match)) { 10342712Snn35248 zerror(zlogp, B_TRUE, "failed to add " 10352712Snn35248 "user-specified device"); 10362712Snn35248 goto cleanup; 10372712Snn35248 } 10382712Snn35248 } 10392712Snn35248 (void) zonecfg_enddevent(handle); 10402712Snn35248 10412712Snn35248 /* Send profile to kernel */ 10422712Snn35248 if (di_prof_commit(prof)) { 10432712Snn35248 zerror(zlogp, B_TRUE, "failed to commit profile"); 10442712Snn35248 goto cleanup; 10452712Snn35248 } 10462712Snn35248 10472712Snn35248 retval = 0; 10482712Snn35248 10492712Snn35248 cleanup: 10502727Sedp if (bh != NULL) 10512727Sedp brand_close(bh); 10522712Snn35248 if (handle) 10532712Snn35248 zonecfg_fini_handle(handle); 10542712Snn35248 if (prof) 10552712Snn35248 di_prof_fini(prof); 10562712Snn35248 return (retval); 10572712Snn35248 } 10582712Snn35248 10592712Snn35248 static int 10600Sstevel@tonic-gate mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath) 10610Sstevel@tonic-gate { 10622712Snn35248 char path[MAXPATHLEN]; 10632712Snn35248 char specpath[MAXPATHLEN]; 10642712Snn35248 char optstr[MAX_MNTOPT_STR]; 10650Sstevel@tonic-gate zone_fsopt_t *optptr; 10662712Snn35248 int rv; 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate if (!valid_mount_path(zlogp, rootpath, fsptr->zone_fs_dir)) { 10690Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s%s is not a valid mount point", 10700Sstevel@tonic-gate rootpath, fsptr->zone_fs_dir); 10710Sstevel@tonic-gate return (-1); 10720Sstevel@tonic-gate } 10730Sstevel@tonic-gate 10740Sstevel@tonic-gate if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir, 10750Sstevel@tonic-gate DEFAULT_DIR_MODE) != 0) 10760Sstevel@tonic-gate return (-1); 10770Sstevel@tonic-gate 10780Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", rootpath, 10790Sstevel@tonic-gate fsptr->zone_fs_dir); 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate if (strlen(fsptr->zone_fs_special) == 0) { 10820Sstevel@tonic-gate /* 10830Sstevel@tonic-gate * A zero-length special is how we distinguish IPDs from 1084766Scarlsonj * general-purpose FSs. Make sure it mounts from a place that 1085766Scarlsonj * can be seen via the alternate zone's root. 10860Sstevel@tonic-gate */ 1087766Scarlsonj if (snprintf(specpath, sizeof (specpath), "%s%s", 1088766Scarlsonj zonecfg_get_root(), fsptr->zone_fs_dir) >= 1089766Scarlsonj sizeof (specpath)) { 1090766Scarlsonj zerror(zlogp, B_FALSE, "cannot mount %s: path too " 1091766Scarlsonj "long in alternate root", fsptr->zone_fs_dir); 1092766Scarlsonj return (-1); 1093766Scarlsonj } 1094766Scarlsonj if (zonecfg_in_alt_root()) 1095766Scarlsonj resolve_lofs(zlogp, specpath, sizeof (specpath)); 10960Sstevel@tonic-gate if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, 1097766Scarlsonj specpath, path) != 0) { 10980Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to loopback mount %s", 1099766Scarlsonj specpath); 11000Sstevel@tonic-gate return (-1); 11010Sstevel@tonic-gate } 11020Sstevel@tonic-gate return (0); 11030Sstevel@tonic-gate } 11040Sstevel@tonic-gate 11050Sstevel@tonic-gate /* 11060Sstevel@tonic-gate * In general the strategy here is to do just as much verification as 11070Sstevel@tonic-gate * necessary to avoid crashing or otherwise doing something bad; if the 11080Sstevel@tonic-gate * administrator initiated the operation via zoneadm(1m), he'll get 11090Sstevel@tonic-gate * auto-verification which will let him know what's wrong. If he 11100Sstevel@tonic-gate * modifies the zone configuration of a running zone and doesn't attempt 11110Sstevel@tonic-gate * to verify that it's OK we won't crash but won't bother trying to be 11120Sstevel@tonic-gate * too helpful either. zoneadm verify is only a couple keystrokes away. 11130Sstevel@tonic-gate */ 11140Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) { 11150Sstevel@tonic-gate zerror(zlogp, B_FALSE, "cannot mount %s on %s: " 11160Sstevel@tonic-gate "invalid file-system type %s", fsptr->zone_fs_special, 11170Sstevel@tonic-gate fsptr->zone_fs_dir, fsptr->zone_fs_type); 11180Sstevel@tonic-gate return (-1); 11190Sstevel@tonic-gate } 11200Sstevel@tonic-gate 11210Sstevel@tonic-gate /* 1122766Scarlsonj * If we're looking at an alternate root environment, then construct 1123766Scarlsonj * read-only loopback mounts as necessary. For all lofs mounts, make 1124766Scarlsonj * sure that the 'special' entry points inside the alternate root. (We 1125766Scarlsonj * don't do this with other mounts, as devfs isn't in the alternate 1126766Scarlsonj * root, and we need to assume the device environment is roughly the 1127766Scarlsonj * same.) 1128766Scarlsonj */ 1129766Scarlsonj if (zonecfg_in_alt_root()) { 1130766Scarlsonj struct stat64 st; 1131766Scarlsonj 1132766Scarlsonj if (stat64(fsptr->zone_fs_special, &st) != -1 && 11332772Scarlsonj S_ISBLK(st.st_mode)) { 11342772Scarlsonj if (check_lofs_needed(zlogp, fsptr) == -1) 11352772Scarlsonj return (-1); 11362772Scarlsonj } else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) { 1137766Scarlsonj if (snprintf(specpath, sizeof (specpath), "%s%s", 1138766Scarlsonj zonecfg_get_root(), fsptr->zone_fs_special) >= 1139766Scarlsonj sizeof (specpath)) { 1140766Scarlsonj zerror(zlogp, B_FALSE, "cannot mount %s: path " 1141766Scarlsonj "too long in alternate root", 1142766Scarlsonj fsptr->zone_fs_special); 1143766Scarlsonj return (-1); 1144766Scarlsonj } 1145766Scarlsonj resolve_lofs(zlogp, specpath, sizeof (specpath)); 1146766Scarlsonj (void) strlcpy(fsptr->zone_fs_special, specpath, 1147766Scarlsonj sizeof (fsptr->zone_fs_special)); 1148766Scarlsonj } 1149766Scarlsonj } 1150766Scarlsonj 1151766Scarlsonj /* 11520Sstevel@tonic-gate * Run 'fsck -m' if there's a device to fsck. 11530Sstevel@tonic-gate */ 11540Sstevel@tonic-gate if (fsptr->zone_fs_raw[0] != '\0' && 11550Sstevel@tonic-gate dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) 11560Sstevel@tonic-gate return (-1); 11570Sstevel@tonic-gate 11580Sstevel@tonic-gate /* 11590Sstevel@tonic-gate * Build up mount option string. 11600Sstevel@tonic-gate */ 11610Sstevel@tonic-gate optstr[0] = '\0'; 11620Sstevel@tonic-gate if (fsptr->zone_fs_options != NULL) { 11630Sstevel@tonic-gate (void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt, 11640Sstevel@tonic-gate sizeof (optstr)); 11650Sstevel@tonic-gate for (optptr = fsptr->zone_fs_options->zone_fsopt_next; 11660Sstevel@tonic-gate optptr != NULL; optptr = optptr->zone_fsopt_next) { 11670Sstevel@tonic-gate (void) strlcat(optstr, ",", sizeof (optstr)); 11680Sstevel@tonic-gate (void) strlcat(optstr, optptr->zone_fsopt_opt, 11690Sstevel@tonic-gate sizeof (optstr)); 11700Sstevel@tonic-gate } 11710Sstevel@tonic-gate } 11722712Snn35248 11732712Snn35248 if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr, 11742712Snn35248 fsptr->zone_fs_special, path)) != 0) 11752712Snn35248 return (rv); 11762712Snn35248 11772712Snn35248 /* 11782712Snn35248 * The mount succeeded. If this was not a mount of /dev then 11792712Snn35248 * we're done. 11802712Snn35248 */ 11812712Snn35248 if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0) 11822712Snn35248 return (0); 11832712Snn35248 11842712Snn35248 /* 11852712Snn35248 * We just mounted an instance of a /dev filesystem, so now we 11862712Snn35248 * need to configure it. 11872712Snn35248 */ 11882712Snn35248 return (mount_one_dev(zlogp, path)); 11890Sstevel@tonic-gate } 11900Sstevel@tonic-gate 11910Sstevel@tonic-gate static void 11920Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem) 11930Sstevel@tonic-gate { 11940Sstevel@tonic-gate uint_t i; 11950Sstevel@tonic-gate 11960Sstevel@tonic-gate if (fsarray == NULL) 11970Sstevel@tonic-gate return; 11980Sstevel@tonic-gate for (i = 0; i < nelem; i++) 11990Sstevel@tonic-gate zonecfg_free_fs_option_list(fsarray[i].zone_fs_options); 12000Sstevel@tonic-gate free(fsarray); 12010Sstevel@tonic-gate } 12020Sstevel@tonic-gate 1203766Scarlsonj /* 12042653Svp157776 * This function initiates the creation of a small Solaris Environment for 12052653Svp157776 * scratch zone. The Environment creation process is split up into two 12062653Svp157776 * functions(build_mounted_pre_var() and build_mounted_post_var()). It 12072653Svp157776 * is done this way because: 12082653Svp157776 * We need to have both /etc and /var in the root of the scratchzone. 12092653Svp157776 * We loopback mount zone's own /etc and /var into the root of the 12102653Svp157776 * scratch zone. Unlike /etc, /var can be a seperate filesystem. So we 12112653Svp157776 * need to delay the mount of /var till the zone's root gets populated. 12122653Svp157776 * So mounting of localdirs[](/etc and /var) have been moved to the 12132653Svp157776 * build_mounted_post_var() which gets called only after the zone 12142653Svp157776 * specific filesystems are mounted. 1215766Scarlsonj */ 1216766Scarlsonj static boolean_t 12172653Svp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath, 12183071Svp157776 size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen) 1219766Scarlsonj { 1220766Scarlsonj char tmp[MAXPATHLEN], fromdir[MAXPATHLEN]; 1221766Scarlsonj const char **cpp; 1222766Scarlsonj static const char *mkdirs[] = { 12232592Sdp "/system", "/system/contract", "/system/object", "/proc", 12242592Sdp "/dev", "/tmp", "/a", NULL 1225766Scarlsonj }; 12262653Svp157776 char *altstr; 1227766Scarlsonj FILE *fp; 1228766Scarlsonj uuid_t uuid; 1229766Scarlsonj 12302712Snn35248 assert(zone_isnative); 12312712Snn35248 1232766Scarlsonj resolve_lofs(zlogp, rootpath, rootlen); 12333071Svp157776 (void) snprintf(luroot, lurootlen, "%s/lu", zonepath); 12343071Svp157776 resolve_lofs(zlogp, luroot, lurootlen); 1235766Scarlsonj (void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot); 1236766Scarlsonj (void) symlink("./usr/bin", tmp); 1237766Scarlsonj 1238766Scarlsonj /* 1239766Scarlsonj * These are mostly special mount points; not handled here. (See 1240766Scarlsonj * zone_mount_early.) 1241766Scarlsonj */ 1242766Scarlsonj for (cpp = mkdirs; *cpp != NULL; cpp++) { 1243766Scarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1244766Scarlsonj if (mkdir(tmp, 0755) != 0) { 1245766Scarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1246766Scarlsonj return (B_FALSE); 1247766Scarlsonj } 1248766Scarlsonj } 12492653Svp157776 /* 12502653Svp157776 * This is here to support lucopy. If there's an instance of this same 12512653Svp157776 * zone on the current running system, then we mount its root up as 12522653Svp157776 * read-only inside the scratch zone. 12532653Svp157776 */ 12542653Svp157776 (void) zonecfg_get_uuid(zone_name, uuid); 12552653Svp157776 altstr = strdup(zonecfg_get_root()); 12562653Svp157776 if (altstr == NULL) { 12572653Svp157776 zerror(zlogp, B_TRUE, "memory allocation failed"); 12582653Svp157776 return (B_FALSE); 12592653Svp157776 } 12602653Svp157776 zonecfg_set_root(""); 12612653Svp157776 (void) strlcpy(tmp, zone_name, sizeof (tmp)); 12622653Svp157776 (void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp)); 12632653Svp157776 if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK && 12642653Svp157776 strcmp(fromdir, rootpath) != 0) { 12652653Svp157776 (void) snprintf(tmp, sizeof (tmp), "%s/b", luroot); 12662653Svp157776 if (mkdir(tmp, 0755) != 0) { 12672653Svp157776 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 12682653Svp157776 return (B_FALSE); 12692653Svp157776 } 12702653Svp157776 if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir, 12712653Svp157776 tmp) != 0) { 12722653Svp157776 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 12732653Svp157776 fromdir); 12742653Svp157776 return (B_FALSE); 12752653Svp157776 } 12762653Svp157776 } 12772653Svp157776 zonecfg_set_root(altstr); 12782653Svp157776 free(altstr); 12792653Svp157776 12802653Svp157776 if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) { 12812653Svp157776 zerror(zlogp, B_TRUE, "cannot open zone mapfile"); 12822653Svp157776 return (B_FALSE); 12832653Svp157776 } 12842653Svp157776 (void) ftruncate(fileno(fp), 0); 12852653Svp157776 if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) { 12862653Svp157776 zerror(zlogp, B_TRUE, "cannot add zone mapfile entry"); 12872653Svp157776 } 12882653Svp157776 zonecfg_close_scratch(fp); 12892653Svp157776 (void) snprintf(tmp, sizeof (tmp), "%s/a", luroot); 12902653Svp157776 if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0) 12912653Svp157776 return (B_FALSE); 12922653Svp157776 (void) strlcpy(rootpath, tmp, rootlen); 12932653Svp157776 return (B_TRUE); 12942653Svp157776 } 12952653Svp157776 12962653Svp157776 12972653Svp157776 static boolean_t 12983071Svp157776 build_mounted_post_var(zlog_t *zlogp, char *rootpath, const char *luroot) 12992653Svp157776 { 13002653Svp157776 char tmp[MAXPATHLEN], fromdir[MAXPATHLEN]; 13012653Svp157776 const char **cpp; 13022653Svp157776 static const char *localdirs[] = { 13032653Svp157776 "/etc", "/var", NULL 13042653Svp157776 }; 13052653Svp157776 static const char *loopdirs[] = { 13062653Svp157776 "/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform", 13072653Svp157776 "/usr", NULL 13082653Svp157776 }; 13092653Svp157776 static const char *tmpdirs[] = { 13102653Svp157776 "/tmp", "/var/run", NULL 13112653Svp157776 }; 13122653Svp157776 struct stat st; 13132653Svp157776 1314766Scarlsonj /* 1315766Scarlsonj * These are mounted read-write from the zone undergoing upgrade. We 1316766Scarlsonj * must be careful not to 'leak' things from the main system into the 1317766Scarlsonj * zone, and this accomplishes that goal. 1318766Scarlsonj */ 1319766Scarlsonj for (cpp = localdirs; *cpp != NULL; cpp++) { 1320766Scarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1321766Scarlsonj (void) snprintf(fromdir, sizeof (fromdir), "%s%s", rootpath, 1322766Scarlsonj *cpp); 1323766Scarlsonj if (mkdir(tmp, 0755) != 0) { 1324766Scarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1325766Scarlsonj return (B_FALSE); 1326766Scarlsonj } 1327766Scarlsonj if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp) != 0) { 1328766Scarlsonj zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1329766Scarlsonj *cpp); 1330766Scarlsonj return (B_FALSE); 1331766Scarlsonj } 1332766Scarlsonj } 1333766Scarlsonj 1334766Scarlsonj /* 1335766Scarlsonj * These are things mounted read-only from the running system because 1336766Scarlsonj * they contain binaries that must match system. 1337766Scarlsonj */ 1338766Scarlsonj for (cpp = loopdirs; *cpp != NULL; cpp++) { 1339766Scarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1340766Scarlsonj if (mkdir(tmp, 0755) != 0) { 1341766Scarlsonj if (errno != EEXIST) { 1342766Scarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1343766Scarlsonj return (B_FALSE); 1344766Scarlsonj } 1345766Scarlsonj if (lstat(tmp, &st) != 0) { 1346766Scarlsonj zerror(zlogp, B_TRUE, "cannot stat %s", tmp); 1347766Scarlsonj return (B_FALSE); 1348766Scarlsonj } 1349766Scarlsonj /* 1350766Scarlsonj * Ignore any non-directories encountered. These are 1351766Scarlsonj * things that have been converted into symlinks 1352766Scarlsonj * (/etc/fs and /etc/lib) and no longer need a lofs 1353766Scarlsonj * fixup. 1354766Scarlsonj */ 1355766Scarlsonj if (!S_ISDIR(st.st_mode)) 1356766Scarlsonj continue; 1357766Scarlsonj } 1358766Scarlsonj if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp, 1359766Scarlsonj tmp) != 0) { 1360766Scarlsonj zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1361766Scarlsonj *cpp); 1362766Scarlsonj return (B_FALSE); 1363766Scarlsonj } 1364766Scarlsonj } 1365766Scarlsonj 1366766Scarlsonj /* 1367766Scarlsonj * These are things with tmpfs mounted inside. 1368766Scarlsonj */ 1369766Scarlsonj for (cpp = tmpdirs; *cpp != NULL; cpp++) { 1370766Scarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1371766Scarlsonj if (mkdir(tmp, 0755) != 0 && errno != EEXIST) { 1372766Scarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1373766Scarlsonj return (B_FALSE); 1374766Scarlsonj } 1375766Scarlsonj if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) { 1376766Scarlsonj zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp); 1377766Scarlsonj return (B_FALSE); 1378766Scarlsonj } 1379766Scarlsonj } 1380766Scarlsonj return (B_TRUE); 1381766Scarlsonj } 1382766Scarlsonj 13832712Snn35248 typedef struct plat_gmount_cb_data { 13842712Snn35248 zlog_t *pgcd_zlogp; 13852712Snn35248 struct zone_fstab **pgcd_fs_tab; 13862712Snn35248 int *pgcd_num_fs; 13872712Snn35248 } plat_gmount_cb_data_t; 13882712Snn35248 13892712Snn35248 /* 13902712Snn35248 * plat_gmount_cb() is a callback function invoked by libbrand to iterate 13912712Snn35248 * through all global brand platform mounts. 13922712Snn35248 */ 13932712Snn35248 int 13942712Snn35248 plat_gmount_cb(void *data, const char *spec, const char *dir, 13952712Snn35248 const char *fstype, const char *opt) 13962712Snn35248 { 13972712Snn35248 plat_gmount_cb_data_t *cp = data; 13982712Snn35248 zlog_t *zlogp = cp->pgcd_zlogp; 13992712Snn35248 struct zone_fstab *fs_ptr = *cp->pgcd_fs_tab; 14002712Snn35248 int num_fs = *cp->pgcd_num_fs; 14012712Snn35248 struct zone_fstab *fsp, *tmp_ptr; 14022712Snn35248 14032712Snn35248 num_fs++; 14042712Snn35248 if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) { 14052712Snn35248 zerror(zlogp, B_TRUE, "memory allocation failed"); 14062712Snn35248 return (-1); 14072712Snn35248 } 14082712Snn35248 14092712Snn35248 fs_ptr = tmp_ptr; 14102712Snn35248 fsp = &fs_ptr[num_fs - 1]; 14112712Snn35248 14122712Snn35248 /* update the callback struct passed in */ 14132712Snn35248 *cp->pgcd_fs_tab = fs_ptr; 14142712Snn35248 *cp->pgcd_num_fs = num_fs; 14152712Snn35248 14162712Snn35248 fsp->zone_fs_raw[0] = '\0'; 14172712Snn35248 (void) strlcpy(fsp->zone_fs_special, spec, 14182712Snn35248 sizeof (fsp->zone_fs_special)); 14192712Snn35248 (void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir)); 14202712Snn35248 (void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type)); 14212712Snn35248 fsp->zone_fs_options = NULL; 14222712Snn35248 if (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK) { 14232712Snn35248 zerror(zlogp, B_FALSE, "error adding property"); 14242712Snn35248 return (-1); 14252712Snn35248 } 14262712Snn35248 14272712Snn35248 return (0); 14282712Snn35248 } 14292712Snn35248 14302712Snn35248 static int 14312712Snn35248 mount_filesystems_ipdent(zone_dochandle_t handle, zlog_t *zlogp, 14322712Snn35248 struct zone_fstab **fs_tabp, int *num_fsp) 14332712Snn35248 { 14342712Snn35248 struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab; 14352712Snn35248 int num_fs; 14362712Snn35248 14372712Snn35248 num_fs = *num_fsp; 14382712Snn35248 fs_ptr = *fs_tabp; 14392712Snn35248 14402712Snn35248 if (zonecfg_setipdent(handle) != Z_OK) { 14412712Snn35248 zerror(zlogp, B_FALSE, "invalid configuration"); 14422712Snn35248 return (-1); 14432712Snn35248 } 14442712Snn35248 while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 14452712Snn35248 num_fs++; 14462712Snn35248 if ((tmp_ptr = realloc(fs_ptr, 14472712Snn35248 num_fs * sizeof (*tmp_ptr))) == NULL) { 14482712Snn35248 zerror(zlogp, B_TRUE, "memory allocation failed"); 14492712Snn35248 (void) zonecfg_endipdent(handle); 14502712Snn35248 return (-1); 14512712Snn35248 } 14522712Snn35248 14532712Snn35248 /* update the pointers passed in */ 14542712Snn35248 *fs_tabp = tmp_ptr; 14552712Snn35248 *num_fsp = num_fs; 14562712Snn35248 14572712Snn35248 /* 14582712Snn35248 * IPDs logically only have a mount point; all other properties 14592712Snn35248 * are implied. 14602712Snn35248 */ 14612712Snn35248 fs_ptr = tmp_ptr; 14622712Snn35248 fsp = &fs_ptr[num_fs - 1]; 14632712Snn35248 (void) strlcpy(fsp->zone_fs_dir, 14642712Snn35248 fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir)); 14652712Snn35248 fsp->zone_fs_special[0] = '\0'; 14662712Snn35248 fsp->zone_fs_raw[0] = '\0'; 14672712Snn35248 fsp->zone_fs_type[0] = '\0'; 14682712Snn35248 fsp->zone_fs_options = NULL; 14692712Snn35248 } 14702712Snn35248 (void) zonecfg_endipdent(handle); 14712712Snn35248 return (0); 14722712Snn35248 } 14732712Snn35248 14742712Snn35248 static int 14752712Snn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp, 14762712Snn35248 struct zone_fstab **fs_tabp, int *num_fsp, int mount_cmd) 14772712Snn35248 { 14782712Snn35248 struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab; 14792712Snn35248 int num_fs; 14802712Snn35248 14812712Snn35248 num_fs = *num_fsp; 14822712Snn35248 fs_ptr = *fs_tabp; 14832712Snn35248 14842712Snn35248 if (zonecfg_setfsent(handle) != Z_OK) { 14852712Snn35248 zerror(zlogp, B_FALSE, "invalid configuration"); 14862712Snn35248 return (-1); 14872712Snn35248 } 14882712Snn35248 while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 14892712Snn35248 /* 14902712Snn35248 * ZFS filesystems will not be accessible under an alternate 14912712Snn35248 * root, since the pool will not be known. Ignore them in this 14922712Snn35248 * case. 14932712Snn35248 */ 14942712Snn35248 if (mount_cmd && strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0) 14952712Snn35248 continue; 14962712Snn35248 14972712Snn35248 num_fs++; 14982712Snn35248 if ((tmp_ptr = realloc(fs_ptr, 14992712Snn35248 num_fs * sizeof (*tmp_ptr))) == NULL) { 15002712Snn35248 zerror(zlogp, B_TRUE, "memory allocation failed"); 15012712Snn35248 (void) zonecfg_endfsent(handle); 15022712Snn35248 return (-1); 15032712Snn35248 } 15042712Snn35248 /* update the pointers passed in */ 15052712Snn35248 *fs_tabp = tmp_ptr; 15062712Snn35248 *num_fsp = num_fs; 15072712Snn35248 15082712Snn35248 fs_ptr = tmp_ptr; 15092712Snn35248 fsp = &fs_ptr[num_fs - 1]; 15102712Snn35248 (void) strlcpy(fsp->zone_fs_dir, 15112712Snn35248 fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir)); 15122712Snn35248 (void) strlcpy(fsp->zone_fs_special, fstab.zone_fs_special, 15132712Snn35248 sizeof (fsp->zone_fs_special)); 15142712Snn35248 (void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw, 15152712Snn35248 sizeof (fsp->zone_fs_raw)); 15162712Snn35248 (void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type, 15172712Snn35248 sizeof (fsp->zone_fs_type)); 15182712Snn35248 fsp->zone_fs_options = fstab.zone_fs_options; 15192712Snn35248 } 15202712Snn35248 (void) zonecfg_endfsent(handle); 15212712Snn35248 return (0); 15222712Snn35248 } 15232712Snn35248 15240Sstevel@tonic-gate static int 1525766Scarlsonj mount_filesystems(zlog_t *zlogp, boolean_t mount_cmd) 15260Sstevel@tonic-gate { 15272712Snn35248 char rootpath[MAXPATHLEN]; 15282712Snn35248 char zonepath[MAXPATHLEN]; 15292712Snn35248 char brand[MAXNAMELEN]; 15303071Svp157776 char luroot[MAXPATHLEN]; 15312712Snn35248 int i, num_fs = 0; 15322712Snn35248 struct zone_fstab *fs_ptr = NULL; 15330Sstevel@tonic-gate zone_dochandle_t handle = NULL; 15340Sstevel@tonic-gate zone_state_t zstate; 15352727Sedp brand_handle_t bh; 15362712Snn35248 plat_gmount_cb_data_t cb; 15370Sstevel@tonic-gate 15380Sstevel@tonic-gate if (zone_get_state(zone_name, &zstate) != Z_OK || 1539766Scarlsonj (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) { 15400Sstevel@tonic-gate zerror(zlogp, B_FALSE, 1541766Scarlsonj "zone must be in '%s' or '%s' state to mount file-systems", 1542766Scarlsonj zone_state_str(ZONE_STATE_READY), 1543766Scarlsonj zone_state_str(ZONE_STATE_MOUNTED)); 15440Sstevel@tonic-gate goto bad; 15450Sstevel@tonic-gate } 15460Sstevel@tonic-gate 15470Sstevel@tonic-gate if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 15480Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone path"); 15490Sstevel@tonic-gate goto bad; 15500Sstevel@tonic-gate } 15510Sstevel@tonic-gate 15520Sstevel@tonic-gate if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) { 15530Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone root"); 15540Sstevel@tonic-gate goto bad; 15550Sstevel@tonic-gate } 15560Sstevel@tonic-gate 15570Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 15581645Scomay zerror(zlogp, B_TRUE, "getting zone configuration handle"); 15590Sstevel@tonic-gate goto bad; 15600Sstevel@tonic-gate } 15610Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK || 15620Sstevel@tonic-gate zonecfg_setfsent(handle) != Z_OK) { 15630Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration"); 15640Sstevel@tonic-gate goto bad; 15650Sstevel@tonic-gate } 15660Sstevel@tonic-gate 15672712Snn35248 /* Get a handle to the brand info for this zone */ 15682712Snn35248 if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) || 15692727Sedp (bh = brand_open(brand)) == NULL) { 15702712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 15712712Snn35248 return (-1); 15722712Snn35248 } 15732712Snn35248 15742712Snn35248 /* 15752712Snn35248 * Get the list of global filesystems to mount from the brand 15762712Snn35248 * configuration. 15772712Snn35248 */ 15782712Snn35248 cb.pgcd_zlogp = zlogp; 15792712Snn35248 cb.pgcd_fs_tab = &fs_ptr; 15802712Snn35248 cb.pgcd_num_fs = &num_fs; 15812727Sedp if (brand_platform_iter_gmounts(bh, zonepath, 15822712Snn35248 plat_gmount_cb, &cb) != 0) { 15832712Snn35248 zerror(zlogp, B_FALSE, "unable to mount filesystems"); 15842727Sedp brand_close(bh); 15852712Snn35248 return (-1); 15862712Snn35248 } 15872727Sedp brand_close(bh); 15882712Snn35248 15890Sstevel@tonic-gate /* 15900Sstevel@tonic-gate * Iterate through the rest of the filesystems, first the IPDs, then 15910Sstevel@tonic-gate * the general FSs. Sort them all, then mount them in sorted order. 15920Sstevel@tonic-gate * This is to make sure the higher level directories (e.g., /usr) 15930Sstevel@tonic-gate * get mounted before any beneath them (e.g., /usr/local). 15940Sstevel@tonic-gate */ 15952712Snn35248 if (mount_filesystems_ipdent(handle, zlogp, &fs_ptr, &num_fs) != 0) 15960Sstevel@tonic-gate goto bad; 15972712Snn35248 15982712Snn35248 if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs, 15992712Snn35248 mount_cmd) != 0) 16002712Snn35248 goto bad; 16012712Snn35248 16020Sstevel@tonic-gate zonecfg_fini_handle(handle); 16030Sstevel@tonic-gate handle = NULL; 16040Sstevel@tonic-gate 1605766Scarlsonj /* 16062712Snn35248 * Normally when we mount a zone all the zone filesystems 16072712Snn35248 * get mounted relative to rootpath, which is usually 16082712Snn35248 * <zonepath>/root. But when mounting a zone for administration 16092712Snn35248 * purposes via the zone "mount" state, build_mounted_pre_var() 16102712Snn35248 * updates rootpath to be <zonepath>/lu/a so we'll mount all 16112712Snn35248 * the zones filesystems there instead. 16122712Snn35248 * 16132712Snn35248 * build_mounted_pre_var() and build_mounted_post_var() will 16142712Snn35248 * also do some extra work to create directories and lofs mount 16152712Snn35248 * a bunch of global zone file system paths into <zonepath>/lu. 16162712Snn35248 * 16172712Snn35248 * This allows us to be able to enter the zone (now rooted at 16182712Snn35248 * <zonepath>/lu) and run the upgrade/patch tools that are in the 16192712Snn35248 * global zone and have them upgrade the to-be-modified zone's 16202712Snn35248 * files mounted on /a. (Which mirrors the existing standard 16212712Snn35248 * upgrade environment.) 16222712Snn35248 * 16232712Snn35248 * There is of course one catch. When doing the upgrade 16242712Snn35248 * we need <zoneroot>/lu/dev to be the /dev filesystem 16252712Snn35248 * for the zone and we don't want to have any /dev filesystem 16262712Snn35248 * mounted at <zoneroot>/lu/a/dev. Since /dev is specified 16272712Snn35248 * as a normal zone filesystem by default we'll try to mount 16282712Snn35248 * it at <zoneroot>/lu/a/dev, so we have to detect this 16292712Snn35248 * case and instead mount it at <zoneroot>/lu/dev. 16302712Snn35248 * 16312712Snn35248 * All this work is done in three phases: 16322653Svp157776 * 1) Create and populate lu directory (build_mounted_pre_var()). 16332653Svp157776 * 2) Mount the required filesystems as per the zone configuration. 16342653Svp157776 * 3) Set up the rest of the scratch zone environment 16352653Svp157776 * (build_mounted_post_var()). 1636766Scarlsonj */ 1637766Scarlsonj if (mount_cmd && 16382653Svp157776 !build_mounted_pre_var(zlogp, 16393071Svp157776 rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot))) 1640766Scarlsonj goto bad; 1641766Scarlsonj 16420Sstevel@tonic-gate qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare); 16432712Snn35248 16440Sstevel@tonic-gate for (i = 0; i < num_fs; i++) { 16452712Snn35248 if (mount_cmd && 16462712Snn35248 strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) { 16472712Snn35248 size_t slen = strlen(rootpath) - 2; 16482712Snn35248 16492712Snn35248 /* 16502712Snn35248 * By default we'll try to mount /dev as /a/dev 16512712Snn35248 * but /dev is special and always goes at the top 16522712Snn35248 * so strip the trailing '/a' from the rootpath. 16532712Snn35248 */ 16542712Snn35248 assert(zone_isnative); 16552712Snn35248 assert(strcmp(&rootpath[slen], "/a") == 0); 16562712Snn35248 rootpath[slen] = '\0'; 16572712Snn35248 if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0) 16582712Snn35248 goto bad; 16592712Snn35248 rootpath[slen] = '/'; 16602712Snn35248 continue; 16612712Snn35248 } 16620Sstevel@tonic-gate if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0) 16630Sstevel@tonic-gate goto bad; 16640Sstevel@tonic-gate } 16652653Svp157776 if (mount_cmd && 16663071Svp157776 !build_mounted_post_var(zlogp, rootpath, luroot)) 16672653Svp157776 goto bad; 16681676Sjpk 16691676Sjpk /* 16701676Sjpk * For Trusted Extensions cross-mount each lower level /export/home 16711676Sjpk */ 16721769Scarlsonj if (!mount_cmd && tsol_mounts(zlogp, zone_name, rootpath) != 0) 16731676Sjpk goto bad; 16741676Sjpk 16750Sstevel@tonic-gate free_fs_data(fs_ptr, num_fs); 16760Sstevel@tonic-gate 16770Sstevel@tonic-gate /* 16780Sstevel@tonic-gate * Everything looks fine. 16790Sstevel@tonic-gate */ 16800Sstevel@tonic-gate return (0); 16810Sstevel@tonic-gate 16820Sstevel@tonic-gate bad: 16830Sstevel@tonic-gate if (handle != NULL) 16840Sstevel@tonic-gate zonecfg_fini_handle(handle); 16850Sstevel@tonic-gate free_fs_data(fs_ptr, num_fs); 16860Sstevel@tonic-gate return (-1); 16870Sstevel@tonic-gate } 16880Sstevel@tonic-gate 16890Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */ 16900Sstevel@tonic-gate static int 16910Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr) 16920Sstevel@tonic-gate { 16930Sstevel@tonic-gate int prefixlen; 16940Sstevel@tonic-gate 16950Sstevel@tonic-gate prefixlen = atoi(prefixstr); 16960Sstevel@tonic-gate if (prefixlen < 0 || prefixlen > maxprefixlen) 16970Sstevel@tonic-gate return (1); 16980Sstevel@tonic-gate while (prefixlen > 0) { 16990Sstevel@tonic-gate if (prefixlen >= 8) { 17000Sstevel@tonic-gate *maskstr++ = 0xFF; 17010Sstevel@tonic-gate prefixlen -= 8; 17020Sstevel@tonic-gate continue; 17030Sstevel@tonic-gate } 17040Sstevel@tonic-gate *maskstr |= 1 << (8 - prefixlen); 17050Sstevel@tonic-gate prefixlen--; 17060Sstevel@tonic-gate } 17070Sstevel@tonic-gate return (0); 17080Sstevel@tonic-gate } 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate /* 17110Sstevel@tonic-gate * Tear down all interfaces belonging to the given zone. This should 17120Sstevel@tonic-gate * be called with the zone in a state other than "running", so that 17130Sstevel@tonic-gate * interfaces can't be assigned to the zone after this returns. 17140Sstevel@tonic-gate * 17150Sstevel@tonic-gate * If anything goes wrong, log an error message and return an error. 17160Sstevel@tonic-gate */ 17170Sstevel@tonic-gate static int 17180Sstevel@tonic-gate unconfigure_network_interfaces(zlog_t *zlogp, zoneid_t zone_id) 17190Sstevel@tonic-gate { 17200Sstevel@tonic-gate struct lifnum lifn; 17210Sstevel@tonic-gate struct lifconf lifc; 17220Sstevel@tonic-gate struct lifreq *lifrp, lifrl; 17230Sstevel@tonic-gate int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES; 17240Sstevel@tonic-gate int num_ifs, s, i, ret_code = 0; 17250Sstevel@tonic-gate uint_t bufsize; 17260Sstevel@tonic-gate char *buf = NULL; 17270Sstevel@tonic-gate 17280Sstevel@tonic-gate if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 17290Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get socket"); 17300Sstevel@tonic-gate ret_code = -1; 17310Sstevel@tonic-gate goto bad; 17320Sstevel@tonic-gate } 17330Sstevel@tonic-gate lifn.lifn_family = AF_UNSPEC; 17340Sstevel@tonic-gate lifn.lifn_flags = (int)lifc_flags; 17350Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) { 17360Sstevel@tonic-gate zerror(zlogp, B_TRUE, 17370Sstevel@tonic-gate "could not determine number of interfaces"); 17380Sstevel@tonic-gate ret_code = -1; 17390Sstevel@tonic-gate goto bad; 17400Sstevel@tonic-gate } 17410Sstevel@tonic-gate num_ifs = lifn.lifn_count; 17420Sstevel@tonic-gate bufsize = num_ifs * sizeof (struct lifreq); 17430Sstevel@tonic-gate if ((buf = malloc(bufsize)) == NULL) { 17440Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 17450Sstevel@tonic-gate ret_code = -1; 17460Sstevel@tonic-gate goto bad; 17470Sstevel@tonic-gate } 17480Sstevel@tonic-gate lifc.lifc_family = AF_UNSPEC; 17490Sstevel@tonic-gate lifc.lifc_flags = (int)lifc_flags; 17500Sstevel@tonic-gate lifc.lifc_len = bufsize; 17510Sstevel@tonic-gate lifc.lifc_buf = buf; 17520Sstevel@tonic-gate if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) { 17530Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get configured interfaces"); 17540Sstevel@tonic-gate ret_code = -1; 17550Sstevel@tonic-gate goto bad; 17560Sstevel@tonic-gate } 17570Sstevel@tonic-gate lifrp = lifc.lifc_req; 17580Sstevel@tonic-gate for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) { 17590Sstevel@tonic-gate (void) close(s); 17600Sstevel@tonic-gate if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) < 17610Sstevel@tonic-gate 0) { 17620Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get socket", 17630Sstevel@tonic-gate lifrl.lifr_name); 17640Sstevel@tonic-gate ret_code = -1; 17650Sstevel@tonic-gate continue; 17660Sstevel@tonic-gate } 17670Sstevel@tonic-gate (void) memset(&lifrl, 0, sizeof (lifrl)); 17680Sstevel@tonic-gate (void) strncpy(lifrl.lifr_name, lifrp->lifr_name, 17690Sstevel@tonic-gate sizeof (lifrl.lifr_name)); 17700Sstevel@tonic-gate if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) { 17713251Ssl108498 if (errno == ENXIO) 17723251Ssl108498 /* 17733251Ssl108498 * Interface may have been removed by admin or 17743251Ssl108498 * another zone halting. 17753251Ssl108498 */ 17763251Ssl108498 continue; 17770Sstevel@tonic-gate zerror(zlogp, B_TRUE, 17783251Ssl108498 "%s: could not determine the zone to which this " 17793251Ssl108498 "interface is bound", lifrl.lifr_name); 17800Sstevel@tonic-gate ret_code = -1; 17810Sstevel@tonic-gate continue; 17820Sstevel@tonic-gate } 17830Sstevel@tonic-gate if (lifrl.lifr_zoneid == zone_id) { 17840Sstevel@tonic-gate if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) { 17850Sstevel@tonic-gate zerror(zlogp, B_TRUE, 17860Sstevel@tonic-gate "%s: could not remove interface", 17870Sstevel@tonic-gate lifrl.lifr_name); 17880Sstevel@tonic-gate ret_code = -1; 17890Sstevel@tonic-gate continue; 17900Sstevel@tonic-gate } 17910Sstevel@tonic-gate } 17920Sstevel@tonic-gate } 17930Sstevel@tonic-gate bad: 17940Sstevel@tonic-gate if (s > 0) 17950Sstevel@tonic-gate (void) close(s); 17960Sstevel@tonic-gate if (buf) 17970Sstevel@tonic-gate free(buf); 17980Sstevel@tonic-gate return (ret_code); 17990Sstevel@tonic-gate } 18000Sstevel@tonic-gate 18010Sstevel@tonic-gate static union sockunion { 18020Sstevel@tonic-gate struct sockaddr sa; 18030Sstevel@tonic-gate struct sockaddr_in sin; 18040Sstevel@tonic-gate struct sockaddr_dl sdl; 18050Sstevel@tonic-gate struct sockaddr_in6 sin6; 18060Sstevel@tonic-gate } so_dst, so_ifp; 18070Sstevel@tonic-gate 18080Sstevel@tonic-gate static struct { 18090Sstevel@tonic-gate struct rt_msghdr hdr; 18100Sstevel@tonic-gate char space[512]; 18110Sstevel@tonic-gate } rtmsg; 18120Sstevel@tonic-gate 18130Sstevel@tonic-gate static int 18140Sstevel@tonic-gate salen(struct sockaddr *sa) 18150Sstevel@tonic-gate { 18160Sstevel@tonic-gate switch (sa->sa_family) { 18170Sstevel@tonic-gate case AF_INET: 18180Sstevel@tonic-gate return (sizeof (struct sockaddr_in)); 18190Sstevel@tonic-gate case AF_LINK: 18200Sstevel@tonic-gate return (sizeof (struct sockaddr_dl)); 18210Sstevel@tonic-gate case AF_INET6: 18220Sstevel@tonic-gate return (sizeof (struct sockaddr_in6)); 18230Sstevel@tonic-gate default: 18240Sstevel@tonic-gate return (sizeof (struct sockaddr)); 18250Sstevel@tonic-gate } 18260Sstevel@tonic-gate } 18270Sstevel@tonic-gate 18280Sstevel@tonic-gate #define ROUNDUP_LONG(a) \ 18290Sstevel@tonic-gate ((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long)) 18300Sstevel@tonic-gate 18310Sstevel@tonic-gate /* 18320Sstevel@tonic-gate * Look up which zone is using a given IP address. The address in question 18330Sstevel@tonic-gate * is expected to have been stuffed into the structure to which lifr points 18340Sstevel@tonic-gate * via a previous SIOCGLIFADDR ioctl(). 18350Sstevel@tonic-gate * 18360Sstevel@tonic-gate * This is done using black router socket magic. 18370Sstevel@tonic-gate * 18380Sstevel@tonic-gate * Return the name of the zone on success or NULL on failure. 18390Sstevel@tonic-gate * 18400Sstevel@tonic-gate * This is a lot of code for a simple task; a new ioctl request to take care 18410Sstevel@tonic-gate * of this might be a useful RFE. 18420Sstevel@tonic-gate */ 18430Sstevel@tonic-gate 18440Sstevel@tonic-gate static char * 18450Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr) 18460Sstevel@tonic-gate { 18470Sstevel@tonic-gate static char answer[ZONENAME_MAX]; 18480Sstevel@tonic-gate pid_t pid; 18490Sstevel@tonic-gate int s, rlen, l, i; 18500Sstevel@tonic-gate char *cp = rtmsg.space; 18510Sstevel@tonic-gate struct sockaddr_dl *ifp = NULL; 18520Sstevel@tonic-gate struct sockaddr *sa; 18530Sstevel@tonic-gate char save_if_name[LIFNAMSIZ]; 18540Sstevel@tonic-gate 18550Sstevel@tonic-gate answer[0] = '\0'; 18560Sstevel@tonic-gate 18570Sstevel@tonic-gate pid = getpid(); 18580Sstevel@tonic-gate if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) { 18590Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get routing socket"); 18600Sstevel@tonic-gate return (NULL); 18610Sstevel@tonic-gate } 18620Sstevel@tonic-gate 18630Sstevel@tonic-gate if (lifr->lifr_addr.ss_family == AF_INET) { 18640Sstevel@tonic-gate struct sockaddr_in *sin4; 18650Sstevel@tonic-gate 18660Sstevel@tonic-gate so_dst.sa.sa_family = AF_INET; 18670Sstevel@tonic-gate sin4 = (struct sockaddr_in *)&lifr->lifr_addr; 18680Sstevel@tonic-gate so_dst.sin.sin_addr = sin4->sin_addr; 18690Sstevel@tonic-gate } else { 18700Sstevel@tonic-gate struct sockaddr_in6 *sin6; 18710Sstevel@tonic-gate 18720Sstevel@tonic-gate so_dst.sa.sa_family = AF_INET6; 18730Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr; 18740Sstevel@tonic-gate so_dst.sin6.sin6_addr = sin6->sin6_addr; 18750Sstevel@tonic-gate } 18760Sstevel@tonic-gate 18770Sstevel@tonic-gate so_ifp.sa.sa_family = AF_LINK; 18780Sstevel@tonic-gate 18790Sstevel@tonic-gate (void) memset(&rtmsg, 0, sizeof (rtmsg)); 18800Sstevel@tonic-gate rtmsg.hdr.rtm_type = RTM_GET; 18810Sstevel@tonic-gate rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST; 18820Sstevel@tonic-gate rtmsg.hdr.rtm_version = RTM_VERSION; 18830Sstevel@tonic-gate rtmsg.hdr.rtm_seq = ++rts_seqno; 18840Sstevel@tonic-gate rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST; 18850Sstevel@tonic-gate 18860Sstevel@tonic-gate l = ROUNDUP_LONG(salen(&so_dst.sa)); 18870Sstevel@tonic-gate (void) memmove(cp, &(so_dst), l); 18880Sstevel@tonic-gate cp += l; 18890Sstevel@tonic-gate l = ROUNDUP_LONG(salen(&so_ifp.sa)); 18900Sstevel@tonic-gate (void) memmove(cp, &(so_ifp), l); 18910Sstevel@tonic-gate cp += l; 18920Sstevel@tonic-gate 18930Sstevel@tonic-gate rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg; 18940Sstevel@tonic-gate 18950Sstevel@tonic-gate if ((rlen = write(s, &rtmsg, l)) < 0) { 18960Sstevel@tonic-gate zerror(zlogp, B_TRUE, "writing to routing socket"); 18970Sstevel@tonic-gate return (NULL); 18980Sstevel@tonic-gate } else if (rlen < (int)rtmsg.hdr.rtm_msglen) { 18990Sstevel@tonic-gate zerror(zlogp, B_TRUE, 19000Sstevel@tonic-gate "write to routing socket got only %d for len\n", rlen); 19010Sstevel@tonic-gate return (NULL); 19020Sstevel@tonic-gate } 19030Sstevel@tonic-gate do { 19040Sstevel@tonic-gate l = read(s, &rtmsg, sizeof (rtmsg)); 19050Sstevel@tonic-gate } while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno || 19060Sstevel@tonic-gate rtmsg.hdr.rtm_pid != pid)); 19070Sstevel@tonic-gate if (l < 0) { 19080Sstevel@tonic-gate zerror(zlogp, B_TRUE, "reading from routing socket"); 19090Sstevel@tonic-gate return (NULL); 19100Sstevel@tonic-gate } 19110Sstevel@tonic-gate 19120Sstevel@tonic-gate if (rtmsg.hdr.rtm_version != RTM_VERSION) { 19130Sstevel@tonic-gate zerror(zlogp, B_FALSE, 19140Sstevel@tonic-gate "routing message version %d not understood", 19150Sstevel@tonic-gate rtmsg.hdr.rtm_version); 19160Sstevel@tonic-gate return (NULL); 19170Sstevel@tonic-gate } 19180Sstevel@tonic-gate if (rtmsg.hdr.rtm_msglen != (ushort_t)l) { 19190Sstevel@tonic-gate zerror(zlogp, B_FALSE, "message length mismatch, " 19200Sstevel@tonic-gate "expected %d bytes, returned %d bytes", 19210Sstevel@tonic-gate rtmsg.hdr.rtm_msglen, l); 19220Sstevel@tonic-gate return (NULL); 19230Sstevel@tonic-gate } 19240Sstevel@tonic-gate if (rtmsg.hdr.rtm_errno != 0) { 19250Sstevel@tonic-gate errno = rtmsg.hdr.rtm_errno; 19260Sstevel@tonic-gate zerror(zlogp, B_TRUE, "RTM_GET routing socket message"); 19270Sstevel@tonic-gate return (NULL); 19280Sstevel@tonic-gate } 19290Sstevel@tonic-gate if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) { 19300Sstevel@tonic-gate zerror(zlogp, B_FALSE, "interface not found"); 19310Sstevel@tonic-gate return (NULL); 19320Sstevel@tonic-gate } 19330Sstevel@tonic-gate cp = ((char *)(&rtmsg.hdr + 1)); 19340Sstevel@tonic-gate for (i = 1; i != 0; i <<= 1) { 19350Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 19360Sstevel@tonic-gate sa = (struct sockaddr *)cp; 19370Sstevel@tonic-gate if (i != RTA_IFP) { 19380Sstevel@tonic-gate if ((i & rtmsg.hdr.rtm_addrs) != 0) 19390Sstevel@tonic-gate cp += ROUNDUP_LONG(salen(sa)); 19400Sstevel@tonic-gate continue; 19410Sstevel@tonic-gate } 19420Sstevel@tonic-gate if (sa->sa_family == AF_LINK && 19430Sstevel@tonic-gate ((struct sockaddr_dl *)sa)->sdl_nlen != 0) 19440Sstevel@tonic-gate ifp = (struct sockaddr_dl *)sa; 19450Sstevel@tonic-gate break; 19460Sstevel@tonic-gate } 19470Sstevel@tonic-gate if (ifp == NULL) { 19480Sstevel@tonic-gate zerror(zlogp, B_FALSE, "interface could not be determined"); 19490Sstevel@tonic-gate return (NULL); 19500Sstevel@tonic-gate } 19510Sstevel@tonic-gate 19520Sstevel@tonic-gate /* 19530Sstevel@tonic-gate * We need to set the I/F name to what we got above, then do the 19540Sstevel@tonic-gate * appropriate ioctl to get its zone name. But lifr->lifr_name is 19550Sstevel@tonic-gate * used by the calling function to do a REMOVEIF, so if we leave the 19560Sstevel@tonic-gate * "good" zone's I/F name in place, *that* I/F will be removed instead 19570Sstevel@tonic-gate * of the bad one. So we save the old (bad) I/F name before over- 19580Sstevel@tonic-gate * writing it and doing the ioctl, then restore it after the ioctl. 19590Sstevel@tonic-gate */ 19600Sstevel@tonic-gate (void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name)); 19610Sstevel@tonic-gate (void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen); 19620Sstevel@tonic-gate lifr->lifr_name[ifp->sdl_nlen] = '\0'; 19630Sstevel@tonic-gate i = ioctl(s, SIOCGLIFZONE, lifr); 19640Sstevel@tonic-gate (void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name)); 19650Sstevel@tonic-gate if (i < 0) { 19660Sstevel@tonic-gate zerror(zlogp, B_TRUE, 19670Sstevel@tonic-gate "%s: could not determine the zone interface belongs to", 19680Sstevel@tonic-gate lifr->lifr_name); 19690Sstevel@tonic-gate return (NULL); 19700Sstevel@tonic-gate } 19710Sstevel@tonic-gate if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0) 19720Sstevel@tonic-gate (void) snprintf(answer, sizeof (answer), "%d", 19730Sstevel@tonic-gate lifr->lifr_zoneid); 19740Sstevel@tonic-gate 19750Sstevel@tonic-gate if (strlen(answer) > 0) 19760Sstevel@tonic-gate return (answer); 19770Sstevel@tonic-gate return (NULL); 19780Sstevel@tonic-gate } 19790Sstevel@tonic-gate 19800Sstevel@tonic-gate typedef struct mcast_rtmsg_s { 19810Sstevel@tonic-gate struct rt_msghdr m_rtm; 19820Sstevel@tonic-gate union { 19830Sstevel@tonic-gate struct { 19840Sstevel@tonic-gate struct sockaddr_in m_dst; 19850Sstevel@tonic-gate struct sockaddr_in m_gw; 19860Sstevel@tonic-gate struct sockaddr_in m_netmask; 19870Sstevel@tonic-gate } m_v4; 19880Sstevel@tonic-gate struct { 19890Sstevel@tonic-gate struct sockaddr_in6 m_dst; 19900Sstevel@tonic-gate struct sockaddr_in6 m_gw; 19910Sstevel@tonic-gate struct sockaddr_in6 m_netmask; 19920Sstevel@tonic-gate } m_v6; 19930Sstevel@tonic-gate } m_u; 19940Sstevel@tonic-gate } mcast_rtmsg_t; 19950Sstevel@tonic-gate #define m_dst4 m_u.m_v4.m_dst 19960Sstevel@tonic-gate #define m_dst6 m_u.m_v6.m_dst 19970Sstevel@tonic-gate #define m_gw4 m_u.m_v4.m_gw 19980Sstevel@tonic-gate #define m_gw6 m_u.m_v6.m_gw 19990Sstevel@tonic-gate #define m_netmask4 m_u.m_v4.m_netmask 20000Sstevel@tonic-gate #define m_netmask6 m_u.m_v6.m_netmask 20010Sstevel@tonic-gate 20020Sstevel@tonic-gate /* 20030Sstevel@tonic-gate * Configures a single interface: a new virtual interface is added, based on 20040Sstevel@tonic-gate * the physical interface nwiftabptr->zone_nwif_physical, with the address 20050Sstevel@tonic-gate * specified in nwiftabptr->zone_nwif_address, for zone zone_id. Note that 20060Sstevel@tonic-gate * the "address" can be an IPv6 address (with a /prefixlength required), an 20070Sstevel@tonic-gate * IPv4 address (with a /prefixlength optional), or a name; for the latter, 20080Sstevel@tonic-gate * an IPv4 name-to-address resolution will be attempted. 20090Sstevel@tonic-gate * 20100Sstevel@tonic-gate * A default interface route for multicast is created on the first IPv4 and 20110Sstevel@tonic-gate * IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively. 20120Sstevel@tonic-gate * This should really be done in the init scripts if we ever allow zones to 20130Sstevel@tonic-gate * modify the routing tables. 20140Sstevel@tonic-gate * 20150Sstevel@tonic-gate * If anything goes wrong, we log an detailed error message, attempt to tear 20160Sstevel@tonic-gate * down whatever we set up and return an error. 20170Sstevel@tonic-gate */ 20180Sstevel@tonic-gate static int 20190Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id, 20200Sstevel@tonic-gate struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp, 20210Sstevel@tonic-gate boolean_t *mcast_rt_v6_setp) 20220Sstevel@tonic-gate { 20230Sstevel@tonic-gate struct lifreq lifr; 20240Sstevel@tonic-gate struct sockaddr_in netmask4; 20250Sstevel@tonic-gate struct sockaddr_in6 netmask6; 20260Sstevel@tonic-gate struct in_addr in4; 20270Sstevel@tonic-gate struct in6_addr in6; 20280Sstevel@tonic-gate sa_family_t af; 20290Sstevel@tonic-gate char *slashp = strchr(nwiftabptr->zone_nwif_address, '/'); 20300Sstevel@tonic-gate mcast_rtmsg_t mcast_rtmsg; 20310Sstevel@tonic-gate int s; 20320Sstevel@tonic-gate int rs; 20330Sstevel@tonic-gate int rlen; 20340Sstevel@tonic-gate boolean_t got_netmask = B_FALSE; 20350Sstevel@tonic-gate char addrstr4[INET_ADDRSTRLEN]; 20360Sstevel@tonic-gate int res; 20370Sstevel@tonic-gate 20380Sstevel@tonic-gate res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr); 20390Sstevel@tonic-gate if (res != Z_OK) { 20400Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res), 20410Sstevel@tonic-gate nwiftabptr->zone_nwif_address); 20420Sstevel@tonic-gate return (-1); 20430Sstevel@tonic-gate } 20440Sstevel@tonic-gate af = lifr.lifr_addr.ss_family; 20450Sstevel@tonic-gate if (af == AF_INET) 20460Sstevel@tonic-gate in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr; 20470Sstevel@tonic-gate else 20480Sstevel@tonic-gate in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr; 20490Sstevel@tonic-gate 20500Sstevel@tonic-gate if ((s = socket(af, SOCK_DGRAM, 0)) < 0) { 20510Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get socket"); 20520Sstevel@tonic-gate return (-1); 20530Sstevel@tonic-gate } 20540Sstevel@tonic-gate 20550Sstevel@tonic-gate (void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical, 20560Sstevel@tonic-gate sizeof (lifr.lifr_name)); 20570Sstevel@tonic-gate if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) { 20582611Svp157776 /* 20592611Svp157776 * Here, we know that the interface can't be brought up. 20602611Svp157776 * A similar warning message was already printed out to 20612611Svp157776 * the console by zoneadm(1M) so instead we log the 20622611Svp157776 * message to syslog and continue. 20632611Svp157776 */ 20642611Svp157776 zerror(&logsys, B_TRUE, "WARNING: skipping interface " 20652611Svp157776 "'%s' which may not be present/plumbed in the " 20662611Svp157776 "global zone.", lifr.lifr_name); 20670Sstevel@tonic-gate (void) close(s); 20682611Svp157776 return (Z_OK); 20690Sstevel@tonic-gate } 20700Sstevel@tonic-gate 20710Sstevel@tonic-gate if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 20720Sstevel@tonic-gate zerror(zlogp, B_TRUE, 20730Sstevel@tonic-gate "%s: could not set IP address to %s", 20740Sstevel@tonic-gate lifr.lifr_name, nwiftabptr->zone_nwif_address); 20750Sstevel@tonic-gate goto bad; 20760Sstevel@tonic-gate } 20770Sstevel@tonic-gate 20780Sstevel@tonic-gate /* Preserve literal IPv4 address for later potential printing. */ 20790Sstevel@tonic-gate if (af == AF_INET) 20800Sstevel@tonic-gate (void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN); 20810Sstevel@tonic-gate 20820Sstevel@tonic-gate lifr.lifr_zoneid = zone_id; 20830Sstevel@tonic-gate if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) { 20840Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not place interface into zone", 20850Sstevel@tonic-gate lifr.lifr_name); 20860Sstevel@tonic-gate goto bad; 20870Sstevel@tonic-gate } 20880Sstevel@tonic-gate 20890Sstevel@tonic-gate if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) { 20900Sstevel@tonic-gate got_netmask = B_TRUE; /* default setting will be correct */ 20910Sstevel@tonic-gate } else { 20920Sstevel@tonic-gate if (af == AF_INET) { 20930Sstevel@tonic-gate /* 20940Sstevel@tonic-gate * The IPv4 netmask can be determined either 20950Sstevel@tonic-gate * directly if a prefix length was supplied with 20960Sstevel@tonic-gate * the address or via the netmasks database. Not 20970Sstevel@tonic-gate * being able to determine it is a common failure, 20980Sstevel@tonic-gate * but it often is not fatal to operation of the 20990Sstevel@tonic-gate * interface. In that case, a warning will be 21000Sstevel@tonic-gate * printed after the rest of the interface's 21010Sstevel@tonic-gate * parameters have been configured. 21020Sstevel@tonic-gate */ 21030Sstevel@tonic-gate (void) memset(&netmask4, 0, sizeof (netmask4)); 21040Sstevel@tonic-gate if (slashp != NULL) { 21050Sstevel@tonic-gate if (addr2netmask(slashp + 1, V4_ADDR_LEN, 21060Sstevel@tonic-gate (uchar_t *)&netmask4.sin_addr) != 0) { 21070Sstevel@tonic-gate *slashp = '/'; 21080Sstevel@tonic-gate zerror(zlogp, B_FALSE, 21090Sstevel@tonic-gate "%s: invalid prefix length in %s", 21100Sstevel@tonic-gate lifr.lifr_name, 21110Sstevel@tonic-gate nwiftabptr->zone_nwif_address); 21120Sstevel@tonic-gate goto bad; 21130Sstevel@tonic-gate } 21140Sstevel@tonic-gate got_netmask = B_TRUE; 21150Sstevel@tonic-gate } else if (getnetmaskbyaddr(in4, 21160Sstevel@tonic-gate &netmask4.sin_addr) == 0) { 21170Sstevel@tonic-gate got_netmask = B_TRUE; 21180Sstevel@tonic-gate } 21190Sstevel@tonic-gate if (got_netmask) { 21200Sstevel@tonic-gate netmask4.sin_family = af; 21210Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &netmask4, 21220Sstevel@tonic-gate sizeof (netmask4)); 21230Sstevel@tonic-gate } 21240Sstevel@tonic-gate } else { 21250Sstevel@tonic-gate (void) memset(&netmask6, 0, sizeof (netmask6)); 21260Sstevel@tonic-gate if (addr2netmask(slashp + 1, V6_ADDR_LEN, 21270Sstevel@tonic-gate (uchar_t *)&netmask6.sin6_addr) != 0) { 21280Sstevel@tonic-gate *slashp = '/'; 21290Sstevel@tonic-gate zerror(zlogp, B_FALSE, 21300Sstevel@tonic-gate "%s: invalid prefix length in %s", 21310Sstevel@tonic-gate lifr.lifr_name, 21320Sstevel@tonic-gate nwiftabptr->zone_nwif_address); 21330Sstevel@tonic-gate goto bad; 21340Sstevel@tonic-gate } 21350Sstevel@tonic-gate got_netmask = B_TRUE; 21360Sstevel@tonic-gate netmask6.sin6_family = af; 21370Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &netmask6, 21380Sstevel@tonic-gate sizeof (netmask6)); 21390Sstevel@tonic-gate } 21400Sstevel@tonic-gate if (got_netmask && 21410Sstevel@tonic-gate ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) { 21420Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not set netmask", 21430Sstevel@tonic-gate lifr.lifr_name); 21440Sstevel@tonic-gate goto bad; 21450Sstevel@tonic-gate } 21460Sstevel@tonic-gate 21470Sstevel@tonic-gate /* 21480Sstevel@tonic-gate * This doesn't set the broadcast address at all. Rather, it 21490Sstevel@tonic-gate * gets, then sets the interface's address, relying on the fact 21500Sstevel@tonic-gate * that resetting the address will reset the broadcast address. 21510Sstevel@tonic-gate */ 21520Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 21530Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get address", 21540Sstevel@tonic-gate lifr.lifr_name); 21550Sstevel@tonic-gate goto bad; 21560Sstevel@tonic-gate } 21570Sstevel@tonic-gate if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 21580Sstevel@tonic-gate zerror(zlogp, B_TRUE, 21590Sstevel@tonic-gate "%s: could not reset broadcast address", 21600Sstevel@tonic-gate lifr.lifr_name); 21610Sstevel@tonic-gate goto bad; 21620Sstevel@tonic-gate } 21630Sstevel@tonic-gate } 21640Sstevel@tonic-gate 21650Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 21660Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get flags", 21670Sstevel@tonic-gate lifr.lifr_name); 21680Sstevel@tonic-gate goto bad; 21690Sstevel@tonic-gate } 21700Sstevel@tonic-gate lifr.lifr_flags |= IFF_UP; 21710Sstevel@tonic-gate if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) { 21720Sstevel@tonic-gate int save_errno = errno; 21730Sstevel@tonic-gate char *zone_using; 21740Sstevel@tonic-gate 21750Sstevel@tonic-gate /* 21760Sstevel@tonic-gate * If we failed with something other than EADDRNOTAVAIL, 21770Sstevel@tonic-gate * then skip to the end. Otherwise, look up our address, 21780Sstevel@tonic-gate * then call a function to determine which zone is already 21790Sstevel@tonic-gate * using that address. 21800Sstevel@tonic-gate */ 21810Sstevel@tonic-gate if (errno != EADDRNOTAVAIL) { 21820Sstevel@tonic-gate zerror(zlogp, B_TRUE, 21830Sstevel@tonic-gate "%s: could not bring interface up", lifr.lifr_name); 21840Sstevel@tonic-gate goto bad; 21850Sstevel@tonic-gate } 21860Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 21870Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get address", 21880Sstevel@tonic-gate lifr.lifr_name); 21890Sstevel@tonic-gate goto bad; 21900Sstevel@tonic-gate } 21910Sstevel@tonic-gate zone_using = who_is_using(zlogp, &lifr); 21920Sstevel@tonic-gate errno = save_errno; 21930Sstevel@tonic-gate if (zone_using == NULL) 21940Sstevel@tonic-gate zerror(zlogp, B_TRUE, 21950Sstevel@tonic-gate "%s: could not bring interface up", lifr.lifr_name); 21960Sstevel@tonic-gate else 21970Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not bring interface " 21980Sstevel@tonic-gate "up: address in use by zone '%s'", lifr.lifr_name, 21990Sstevel@tonic-gate zone_using); 22000Sstevel@tonic-gate goto bad; 22010Sstevel@tonic-gate } 22020Sstevel@tonic-gate if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET && 22030Sstevel@tonic-gate mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) || 22040Sstevel@tonic-gate (af == AF_INET6 && 22050Sstevel@tonic-gate mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) { 22060Sstevel@tonic-gate rs = socket(PF_ROUTE, SOCK_RAW, 0); 22070Sstevel@tonic-gate if (rs < 0) { 22080Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not create " 22090Sstevel@tonic-gate "routing socket", lifr.lifr_name); 22100Sstevel@tonic-gate goto bad; 22110Sstevel@tonic-gate } 22120Sstevel@tonic-gate (void) shutdown(rs, 0); 22130Sstevel@tonic-gate (void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t)); 22140Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_msglen = sizeof (struct rt_msghdr) + 22150Sstevel@tonic-gate 3 * (af == AF_INET ? sizeof (struct sockaddr_in) : 22160Sstevel@tonic-gate sizeof (struct sockaddr_in6)); 22170Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION; 22180Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_type = RTM_ADD; 22190Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_flags = RTF_UP; 22200Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_addrs = 22210Sstevel@tonic-gate RTA_DST | RTA_GATEWAY | RTA_NETMASK; 22220Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno; 22230Sstevel@tonic-gate if (af == AF_INET) { 22240Sstevel@tonic-gate mcast_rtmsg.m_dst4.sin_family = AF_INET; 22250Sstevel@tonic-gate mcast_rtmsg.m_dst4.sin_addr.s_addr = 22260Sstevel@tonic-gate htonl(INADDR_UNSPEC_GROUP); 22270Sstevel@tonic-gate mcast_rtmsg.m_gw4.sin_family = AF_INET; 22280Sstevel@tonic-gate mcast_rtmsg.m_gw4.sin_addr = in4; 22290Sstevel@tonic-gate mcast_rtmsg.m_netmask4.sin_family = AF_INET; 22300Sstevel@tonic-gate mcast_rtmsg.m_netmask4.sin_addr.s_addr = 22310Sstevel@tonic-gate htonl(IN_CLASSD_NET); 22320Sstevel@tonic-gate } else { 22330Sstevel@tonic-gate mcast_rtmsg.m_dst6.sin6_family = AF_INET6; 22340Sstevel@tonic-gate mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU; 22350Sstevel@tonic-gate mcast_rtmsg.m_gw6.sin6_family = AF_INET6; 22360Sstevel@tonic-gate mcast_rtmsg.m_gw6.sin6_addr = in6; 22370Sstevel@tonic-gate mcast_rtmsg.m_netmask6.sin6_family = AF_INET6; 22380Sstevel@tonic-gate mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU; 22390Sstevel@tonic-gate } 22400Sstevel@tonic-gate rlen = write(rs, (char *)&mcast_rtmsg, 22410Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_msglen); 22422611Svp157776 /* 22432611Svp157776 * The write to the multicast socket will fail if the 22442611Svp157776 * interface belongs to a failed IPMP group. This is a 22452611Svp157776 * non-fatal error and the zone will continue booting. 22462611Svp157776 * While the zone is running, if any interface in the 22472611Svp157776 * failed IPMP group recovers, the zone will fallback to 22482611Svp157776 * using that interface. 22492611Svp157776 */ 22500Sstevel@tonic-gate if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) { 22510Sstevel@tonic-gate if (rlen < 0) { 22522611Svp157776 zerror(zlogp, B_TRUE, "WARNING: interface " 22532611Svp157776 "'%s' not available as default for " 22542611Svp157776 "multicast.", lifr.lifr_name); 22550Sstevel@tonic-gate } else { 22562611Svp157776 zerror(zlogp, B_FALSE, "WARNING: interface " 22572611Svp157776 "'%s' not available as default for " 22582611Svp157776 "multicast; routing socket returned " 22592611Svp157776 "unexpected %d bytes.", 22602611Svp157776 lifr.lifr_name, rlen); 22610Sstevel@tonic-gate } 22620Sstevel@tonic-gate } else { 22632611Svp157776 22642611Svp157776 if (af == AF_INET) { 22652611Svp157776 *mcast_rt_v4_setp = B_TRUE; 22662611Svp157776 } else { 22672611Svp157776 *mcast_rt_v6_setp = B_TRUE; 22682611Svp157776 } 22690Sstevel@tonic-gate } 22700Sstevel@tonic-gate (void) close(rs); 22710Sstevel@tonic-gate } 22720Sstevel@tonic-gate 22730Sstevel@tonic-gate if (!got_netmask) { 22740Sstevel@tonic-gate /* 22750Sstevel@tonic-gate * A common, but often non-fatal problem, is that the system 22760Sstevel@tonic-gate * cannot find the netmask for an interface address. This is 22770Sstevel@tonic-gate * often caused by it being only in /etc/inet/netmasks, but 22780Sstevel@tonic-gate * /etc/nsswitch.conf says to use NIS or NIS+ and it's not 22790Sstevel@tonic-gate * in that. This doesn't show up at boot because the netmask 22800Sstevel@tonic-gate * is obtained from /etc/inet/netmasks when no network 22810Sstevel@tonic-gate * interfaces are up, but isn't consulted when NIS/NIS+ is 22820Sstevel@tonic-gate * available. We warn the user here that something like this 22830Sstevel@tonic-gate * has happened and we're just running with a default and 22840Sstevel@tonic-gate * possible incorrect netmask. 22850Sstevel@tonic-gate */ 22860Sstevel@tonic-gate char buffer[INET6_ADDRSTRLEN]; 22870Sstevel@tonic-gate void *addr; 22880Sstevel@tonic-gate 22890Sstevel@tonic-gate if (af == AF_INET) 22900Sstevel@tonic-gate addr = &((struct sockaddr_in *) 22910Sstevel@tonic-gate (&lifr.lifr_addr))->sin_addr; 22920Sstevel@tonic-gate else 22930Sstevel@tonic-gate addr = &((struct sockaddr_in6 *) 22940Sstevel@tonic-gate (&lifr.lifr_addr))->sin6_addr; 22950Sstevel@tonic-gate 22960Sstevel@tonic-gate /* Find out what netmask interface is going to be using */ 22970Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 || 22980Sstevel@tonic-gate inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) 22990Sstevel@tonic-gate goto bad; 23000Sstevel@tonic-gate zerror(zlogp, B_FALSE, 23010Sstevel@tonic-gate "WARNING: %s: no matching subnet found in netmasks(4) for " 23020Sstevel@tonic-gate "%s; using default of %s.", 23030Sstevel@tonic-gate lifr.lifr_name, addrstr4, buffer); 23040Sstevel@tonic-gate } 23050Sstevel@tonic-gate 23060Sstevel@tonic-gate (void) close(s); 23070Sstevel@tonic-gate return (Z_OK); 23080Sstevel@tonic-gate bad: 23090Sstevel@tonic-gate (void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr); 23100Sstevel@tonic-gate (void) close(s); 23110Sstevel@tonic-gate return (-1); 23120Sstevel@tonic-gate } 23130Sstevel@tonic-gate 23140Sstevel@tonic-gate /* 23150Sstevel@tonic-gate * Sets up network interfaces based on information from the zone configuration. 23160Sstevel@tonic-gate * An IPv4 loopback interface is set up "for free", modeling the global system. 23170Sstevel@tonic-gate * If any of the configuration interfaces were IPv6, then an IPv6 loopback 23180Sstevel@tonic-gate * address is set up as well. 23190Sstevel@tonic-gate * 23200Sstevel@tonic-gate * If anything goes wrong, we log a general error message, attempt to tear down 23210Sstevel@tonic-gate * whatever we set up, and return an error. 23220Sstevel@tonic-gate */ 23230Sstevel@tonic-gate static int 23240Sstevel@tonic-gate configure_network_interfaces(zlog_t *zlogp) 23250Sstevel@tonic-gate { 23260Sstevel@tonic-gate zone_dochandle_t handle; 23270Sstevel@tonic-gate struct zone_nwiftab nwiftab, loopback_iftab; 23280Sstevel@tonic-gate boolean_t saw_v6 = B_FALSE; 23290Sstevel@tonic-gate boolean_t mcast_rt_v4_set = B_FALSE; 23300Sstevel@tonic-gate boolean_t mcast_rt_v6_set = B_FALSE; 23310Sstevel@tonic-gate zoneid_t zoneid; 23320Sstevel@tonic-gate 23330Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) { 23340Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid"); 23350Sstevel@tonic-gate return (-1); 23360Sstevel@tonic-gate } 23370Sstevel@tonic-gate 23380Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 23390Sstevel@tonic-gate zerror(zlogp, B_TRUE, "getting zone configuration handle"); 23400Sstevel@tonic-gate return (-1); 23410Sstevel@tonic-gate } 23420Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 23430Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration"); 23440Sstevel@tonic-gate zonecfg_fini_handle(handle); 23450Sstevel@tonic-gate return (-1); 23460Sstevel@tonic-gate } 23470Sstevel@tonic-gate if (zonecfg_setnwifent(handle) == Z_OK) { 23480Sstevel@tonic-gate for (;;) { 23490Sstevel@tonic-gate struct in6_addr in6; 23500Sstevel@tonic-gate 23510Sstevel@tonic-gate if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK) 23520Sstevel@tonic-gate break; 23530Sstevel@tonic-gate if (configure_one_interface(zlogp, zoneid, 23540Sstevel@tonic-gate &nwiftab, &mcast_rt_v4_set, &mcast_rt_v6_set) != 23550Sstevel@tonic-gate Z_OK) { 23560Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 23570Sstevel@tonic-gate zonecfg_fini_handle(handle); 23580Sstevel@tonic-gate return (-1); 23590Sstevel@tonic-gate } 23600Sstevel@tonic-gate if (inet_pton(AF_INET6, nwiftab.zone_nwif_address, 23610Sstevel@tonic-gate &in6) == 1) 23620Sstevel@tonic-gate saw_v6 = B_TRUE; 23630Sstevel@tonic-gate } 23640Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 23650Sstevel@tonic-gate } 23660Sstevel@tonic-gate zonecfg_fini_handle(handle); 23670Sstevel@tonic-gate (void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0", 23680Sstevel@tonic-gate sizeof (loopback_iftab.zone_nwif_physical)); 23690Sstevel@tonic-gate (void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1", 23700Sstevel@tonic-gate sizeof (loopback_iftab.zone_nwif_address)); 23710Sstevel@tonic-gate if (configure_one_interface(zlogp, zoneid, &loopback_iftab, NULL, NULL) 23720Sstevel@tonic-gate != Z_OK) { 23730Sstevel@tonic-gate return (-1); 23740Sstevel@tonic-gate } 23750Sstevel@tonic-gate if (saw_v6) { 23760Sstevel@tonic-gate (void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128", 23770Sstevel@tonic-gate sizeof (loopback_iftab.zone_nwif_address)); 23780Sstevel@tonic-gate if (configure_one_interface(zlogp, zoneid, 23790Sstevel@tonic-gate &loopback_iftab, NULL, NULL) != Z_OK) { 23800Sstevel@tonic-gate return (-1); 23810Sstevel@tonic-gate } 23820Sstevel@tonic-gate } 23830Sstevel@tonic-gate return (0); 23840Sstevel@tonic-gate } 23850Sstevel@tonic-gate 23860Sstevel@tonic-gate static int 23870Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid, 23880Sstevel@tonic-gate const struct sockaddr_storage *local, const struct sockaddr_storage *remote) 23890Sstevel@tonic-gate { 23900Sstevel@tonic-gate int fd; 23910Sstevel@tonic-gate struct strioctl ioc; 23920Sstevel@tonic-gate tcp_ioc_abort_conn_t conn; 23930Sstevel@tonic-gate int error; 23940Sstevel@tonic-gate 23950Sstevel@tonic-gate conn.ac_local = *local; 23960Sstevel@tonic-gate conn.ac_remote = *remote; 23970Sstevel@tonic-gate conn.ac_start = TCPS_SYN_SENT; 23980Sstevel@tonic-gate conn.ac_end = TCPS_TIME_WAIT; 23990Sstevel@tonic-gate conn.ac_zoneid = zoneid; 24000Sstevel@tonic-gate 24010Sstevel@tonic-gate ioc.ic_cmd = TCP_IOC_ABORT_CONN; 24020Sstevel@tonic-gate ioc.ic_timout = -1; /* infinite timeout */ 24030Sstevel@tonic-gate ioc.ic_len = sizeof (conn); 24040Sstevel@tonic-gate ioc.ic_dp = (char *)&conn; 24050Sstevel@tonic-gate 24060Sstevel@tonic-gate if ((fd = open("/dev/tcp", O_RDONLY)) < 0) { 24070Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp"); 24080Sstevel@tonic-gate return (-1); 24090Sstevel@tonic-gate } 24100Sstevel@tonic-gate 24110Sstevel@tonic-gate error = ioctl(fd, I_STR, &ioc); 24120Sstevel@tonic-gate (void) close(fd); 24130Sstevel@tonic-gate if (error == 0 || errno == ENOENT) /* ENOENT is not an error */ 24140Sstevel@tonic-gate return (0); 24150Sstevel@tonic-gate return (-1); 24160Sstevel@tonic-gate } 24170Sstevel@tonic-gate 24180Sstevel@tonic-gate static int 24190Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid) 24200Sstevel@tonic-gate { 24210Sstevel@tonic-gate struct sockaddr_storage l, r; 24220Sstevel@tonic-gate struct sockaddr_in *local, *remote; 24230Sstevel@tonic-gate struct sockaddr_in6 *local6, *remote6; 24240Sstevel@tonic-gate int error; 24250Sstevel@tonic-gate 24260Sstevel@tonic-gate /* 24270Sstevel@tonic-gate * Abort IPv4 connections. 24280Sstevel@tonic-gate */ 24290Sstevel@tonic-gate bzero(&l, sizeof (*local)); 24300Sstevel@tonic-gate local = (struct sockaddr_in *)&l; 24310Sstevel@tonic-gate local->sin_family = AF_INET; 24320Sstevel@tonic-gate local->sin_addr.s_addr = INADDR_ANY; 24330Sstevel@tonic-gate local->sin_port = 0; 24340Sstevel@tonic-gate 24350Sstevel@tonic-gate bzero(&r, sizeof (*remote)); 24360Sstevel@tonic-gate remote = (struct sockaddr_in *)&r; 24370Sstevel@tonic-gate remote->sin_family = AF_INET; 24380Sstevel@tonic-gate remote->sin_addr.s_addr = INADDR_ANY; 24390Sstevel@tonic-gate remote->sin_port = 0; 24400Sstevel@tonic-gate 24410Sstevel@tonic-gate if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0) 24420Sstevel@tonic-gate return (error); 24430Sstevel@tonic-gate 24440Sstevel@tonic-gate /* 24450Sstevel@tonic-gate * Abort IPv6 connections. 24460Sstevel@tonic-gate */ 24470Sstevel@tonic-gate bzero(&l, sizeof (*local6)); 24480Sstevel@tonic-gate local6 = (struct sockaddr_in6 *)&l; 24490Sstevel@tonic-gate local6->sin6_family = AF_INET6; 24500Sstevel@tonic-gate local6->sin6_port = 0; 24510Sstevel@tonic-gate local6->sin6_addr = in6addr_any; 24520Sstevel@tonic-gate 24530Sstevel@tonic-gate bzero(&r, sizeof (*remote6)); 24540Sstevel@tonic-gate remote6 = (struct sockaddr_in6 *)&r; 24550Sstevel@tonic-gate remote6->sin6_family = AF_INET6; 24560Sstevel@tonic-gate remote6->sin6_port = 0; 24570Sstevel@tonic-gate remote6->sin6_addr = in6addr_any; 24580Sstevel@tonic-gate 24590Sstevel@tonic-gate if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0) 24600Sstevel@tonic-gate return (error); 24610Sstevel@tonic-gate return (0); 24620Sstevel@tonic-gate } 24630Sstevel@tonic-gate 24640Sstevel@tonic-gate static int 24651645Scomay get_privset(zlog_t *zlogp, priv_set_t *privs, boolean_t mount_cmd) 24661645Scomay { 24671645Scomay int error = -1; 24681645Scomay zone_dochandle_t handle; 24691645Scomay char *privname = NULL; 24701645Scomay 24711645Scomay if ((handle = zonecfg_init_handle()) == NULL) { 24721645Scomay zerror(zlogp, B_TRUE, "getting zone configuration handle"); 24731645Scomay return (-1); 24741645Scomay } 24751645Scomay if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 24761645Scomay zerror(zlogp, B_FALSE, "invalid configuration"); 24771645Scomay zonecfg_fini_handle(handle); 24781645Scomay return (-1); 24791645Scomay } 24801645Scomay 24812712Snn35248 if (mount_cmd) { 24822712Snn35248 if (zonecfg_default_privset(privs) == Z_OK) 24832712Snn35248 return (0); 24842712Snn35248 zerror(zlogp, B_FALSE, 24852712Snn35248 "failed to determine the zone's default privilege set"); 24862712Snn35248 zonecfg_fini_handle(handle); 24872712Snn35248 return (-1); 24882712Snn35248 } 24892712Snn35248 24901645Scomay switch (zonecfg_get_privset(handle, privs, &privname)) { 24911645Scomay case Z_OK: 24921645Scomay error = 0; 24931645Scomay break; 24941645Scomay case Z_PRIV_PROHIBITED: 24951645Scomay zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted " 24961645Scomay "within the zone's privilege set", privname); 24971645Scomay break; 24981645Scomay case Z_PRIV_REQUIRED: 24991645Scomay zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing " 25001645Scomay "from the zone's privilege set", privname); 25011645Scomay break; 25021645Scomay case Z_PRIV_UNKNOWN: 25031645Scomay zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified " 25041645Scomay "in the zone's privilege set", privname); 25051645Scomay break; 25061645Scomay default: 25071645Scomay zerror(zlogp, B_FALSE, "failed to determine the zone's " 25081645Scomay "privilege set"); 25091645Scomay break; 25101645Scomay } 25111645Scomay 25121645Scomay free(privname); 25131645Scomay zonecfg_fini_handle(handle); 25141645Scomay return (error); 25151645Scomay } 25161645Scomay 25171645Scomay static int 25180Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep) 25190Sstevel@tonic-gate { 25200Sstevel@tonic-gate nvlist_t *nvl = NULL; 25210Sstevel@tonic-gate char *nvl_packed = NULL; 25220Sstevel@tonic-gate size_t nvl_size = 0; 25230Sstevel@tonic-gate nvlist_t **nvlv = NULL; 25240Sstevel@tonic-gate int rctlcount = 0; 25250Sstevel@tonic-gate int error = -1; 25260Sstevel@tonic-gate zone_dochandle_t handle; 25270Sstevel@tonic-gate struct zone_rctltab rctltab; 25280Sstevel@tonic-gate rctlblk_t *rctlblk = NULL; 25290Sstevel@tonic-gate 25300Sstevel@tonic-gate *bufp = NULL; 25310Sstevel@tonic-gate *bufsizep = 0; 25320Sstevel@tonic-gate 25330Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 25340Sstevel@tonic-gate zerror(zlogp, B_TRUE, "getting zone configuration handle"); 25350Sstevel@tonic-gate return (-1); 25360Sstevel@tonic-gate } 25370Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 25380Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration"); 25390Sstevel@tonic-gate zonecfg_fini_handle(handle); 25400Sstevel@tonic-gate return (-1); 25410Sstevel@tonic-gate } 25420Sstevel@tonic-gate 25430Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 25440Sstevel@tonic-gate if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 25450Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc"); 25460Sstevel@tonic-gate goto out; 25470Sstevel@tonic-gate } 25480Sstevel@tonic-gate 25490Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 25500Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent"); 25510Sstevel@tonic-gate goto out; 25520Sstevel@tonic-gate } 25530Sstevel@tonic-gate 25540Sstevel@tonic-gate if ((rctlblk = malloc(rctlblk_size())) == NULL) { 25550Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 25560Sstevel@tonic-gate goto out; 25570Sstevel@tonic-gate } 25580Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 25590Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 25600Sstevel@tonic-gate uint_t i, count; 25610Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 25620Sstevel@tonic-gate 25630Sstevel@tonic-gate /* zoneadm should have already warned about unknown rctls. */ 25640Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 25650Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 25660Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 25670Sstevel@tonic-gate continue; 25680Sstevel@tonic-gate } 25690Sstevel@tonic-gate count = 0; 25700Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 25710Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 25720Sstevel@tonic-gate count++; 25730Sstevel@tonic-gate } 25740Sstevel@tonic-gate if (count == 0) { /* ignore */ 25750Sstevel@tonic-gate continue; /* Nothing to free */ 25760Sstevel@tonic-gate } 25770Sstevel@tonic-gate if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL) 25780Sstevel@tonic-gate goto out; 25790Sstevel@tonic-gate i = 0; 25800Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 25810Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next, i++) { 25820Sstevel@tonic-gate if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) { 25830Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", 25840Sstevel@tonic-gate "nvlist_alloc"); 25850Sstevel@tonic-gate goto out; 25860Sstevel@tonic-gate } 25870Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 25880Sstevel@tonic-gate != Z_OK) { 25890Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid rctl value: " 25900Sstevel@tonic-gate "(priv=%s,limit=%s,action=%s)", 25910Sstevel@tonic-gate rctlval->zone_rctlval_priv, 25920Sstevel@tonic-gate rctlval->zone_rctlval_limit, 25930Sstevel@tonic-gate rctlval->zone_rctlval_action); 25940Sstevel@tonic-gate goto out; 25950Sstevel@tonic-gate } 25960Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 25970Sstevel@tonic-gate zerror(zlogp, B_FALSE, 25980Sstevel@tonic-gate "(priv=%s,limit=%s,action=%s) is not a " 25990Sstevel@tonic-gate "valid value for rctl '%s'", 26000Sstevel@tonic-gate rctlval->zone_rctlval_priv, 26010Sstevel@tonic-gate rctlval->zone_rctlval_limit, 26020Sstevel@tonic-gate rctlval->zone_rctlval_action, 26030Sstevel@tonic-gate name); 26040Sstevel@tonic-gate goto out; 26050Sstevel@tonic-gate } 26060Sstevel@tonic-gate if (nvlist_add_uint64(nvlv[i], "privilege", 26071645Scomay rctlblk_get_privilege(rctlblk)) != 0) { 26080Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", 26090Sstevel@tonic-gate "nvlist_add_uint64"); 26100Sstevel@tonic-gate goto out; 26110Sstevel@tonic-gate } 26120Sstevel@tonic-gate if (nvlist_add_uint64(nvlv[i], "limit", 26131645Scomay rctlblk_get_value(rctlblk)) != 0) { 26140Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", 26150Sstevel@tonic-gate "nvlist_add_uint64"); 26160Sstevel@tonic-gate goto out; 26170Sstevel@tonic-gate } 26180Sstevel@tonic-gate if (nvlist_add_uint64(nvlv[i], "action", 26190Sstevel@tonic-gate (uint_t)rctlblk_get_local_action(rctlblk, NULL)) 26200Sstevel@tonic-gate != 0) { 26210Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", 26220Sstevel@tonic-gate "nvlist_add_uint64"); 26230Sstevel@tonic-gate goto out; 26240Sstevel@tonic-gate } 26250Sstevel@tonic-gate } 26260Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 26270Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 26280Sstevel@tonic-gate if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count) 26290Sstevel@tonic-gate != 0) { 26300Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", 26310Sstevel@tonic-gate "nvlist_add_nvlist_array"); 26320Sstevel@tonic-gate goto out; 26330Sstevel@tonic-gate } 26340Sstevel@tonic-gate for (i = 0; i < count; i++) 26350Sstevel@tonic-gate nvlist_free(nvlv[i]); 26360Sstevel@tonic-gate free(nvlv); 26370Sstevel@tonic-gate nvlv = NULL; 26380Sstevel@tonic-gate rctlcount++; 26390Sstevel@tonic-gate } 26400Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 26410Sstevel@tonic-gate 26420Sstevel@tonic-gate if (rctlcount == 0) { 26430Sstevel@tonic-gate error = 0; 26440Sstevel@tonic-gate goto out; 26450Sstevel@tonic-gate } 26460Sstevel@tonic-gate if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0) 26470Sstevel@tonic-gate != 0) { 26480Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack"); 26490Sstevel@tonic-gate goto out; 26500Sstevel@tonic-gate } 26510Sstevel@tonic-gate 26520Sstevel@tonic-gate error = 0; 26530Sstevel@tonic-gate *bufp = nvl_packed; 26540Sstevel@tonic-gate *bufsizep = nvl_size; 26550Sstevel@tonic-gate 26560Sstevel@tonic-gate out: 26570Sstevel@tonic-gate free(rctlblk); 26580Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 26590Sstevel@tonic-gate if (error && nvl_packed != NULL) 26600Sstevel@tonic-gate free(nvl_packed); 26610Sstevel@tonic-gate if (nvl != NULL) 26620Sstevel@tonic-gate nvlist_free(nvl); 26630Sstevel@tonic-gate if (nvlv != NULL) 26640Sstevel@tonic-gate free(nvlv); 26650Sstevel@tonic-gate if (handle != NULL) 26660Sstevel@tonic-gate zonecfg_fini_handle(handle); 26670Sstevel@tonic-gate return (error); 26680Sstevel@tonic-gate } 26690Sstevel@tonic-gate 26700Sstevel@tonic-gate static int 2671789Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep) 2672789Sahrens { 2673789Sahrens zone_dochandle_t handle; 2674789Sahrens struct zone_dstab dstab; 2675789Sahrens size_t total, offset, len; 2676789Sahrens int error = -1; 2677789Sahrens char *str; 2678789Sahrens 2679789Sahrens *bufp = NULL; 2680789Sahrens *bufsizep = 0; 2681789Sahrens 2682789Sahrens if ((handle = zonecfg_init_handle()) == NULL) { 2683789Sahrens zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2684789Sahrens return (-1); 2685789Sahrens } 2686789Sahrens if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2687789Sahrens zerror(zlogp, B_FALSE, "invalid configuration"); 2688789Sahrens zonecfg_fini_handle(handle); 2689789Sahrens return (-1); 2690789Sahrens } 2691789Sahrens 2692789Sahrens if (zonecfg_setdsent(handle) != Z_OK) { 2693789Sahrens zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent"); 2694789Sahrens goto out; 2695789Sahrens } 2696789Sahrens 2697789Sahrens total = 0; 2698789Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) 2699789Sahrens total += strlen(dstab.zone_dataset_name) + 1; 2700789Sahrens (void) zonecfg_enddsent(handle); 2701789Sahrens 2702789Sahrens if (total == 0) { 2703789Sahrens error = 0; 2704789Sahrens goto out; 2705789Sahrens } 2706789Sahrens 2707789Sahrens if ((str = malloc(total)) == NULL) { 2708789Sahrens zerror(zlogp, B_TRUE, "memory allocation failed"); 2709789Sahrens goto out; 2710789Sahrens } 2711789Sahrens 2712789Sahrens if (zonecfg_setdsent(handle) != Z_OK) { 2713789Sahrens zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent"); 2714789Sahrens goto out; 2715789Sahrens } 2716789Sahrens offset = 0; 2717789Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 2718789Sahrens len = strlen(dstab.zone_dataset_name); 2719789Sahrens (void) strlcpy(str + offset, dstab.zone_dataset_name, 2720789Sahrens sizeof (dstab.zone_dataset_name) - offset); 2721789Sahrens offset += len; 2722789Sahrens if (offset != total - 1) 2723789Sahrens str[offset++] = ','; 2724789Sahrens } 2725789Sahrens (void) zonecfg_enddsent(handle); 2726789Sahrens 2727789Sahrens error = 0; 2728789Sahrens *bufp = str; 2729789Sahrens *bufsizep = total; 2730789Sahrens 2731789Sahrens out: 2732789Sahrens if (error != 0 && str != NULL) 2733789Sahrens free(str); 2734789Sahrens if (handle != NULL) 2735789Sahrens zonecfg_fini_handle(handle); 2736789Sahrens 2737789Sahrens return (error); 2738789Sahrens } 2739789Sahrens 2740789Sahrens static int 2741789Sahrens validate_datasets(zlog_t *zlogp) 2742789Sahrens { 2743789Sahrens zone_dochandle_t handle; 2744789Sahrens struct zone_dstab dstab; 2745789Sahrens zfs_handle_t *zhp; 27462082Seschrock libzfs_handle_t *hdl; 2747789Sahrens 2748789Sahrens if ((handle = zonecfg_init_handle()) == NULL) { 2749789Sahrens zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2750789Sahrens return (-1); 2751789Sahrens } 2752789Sahrens if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2753789Sahrens zerror(zlogp, B_FALSE, "invalid configuration"); 2754789Sahrens zonecfg_fini_handle(handle); 2755789Sahrens return (-1); 2756789Sahrens } 2757789Sahrens 2758789Sahrens if (zonecfg_setdsent(handle) != Z_OK) { 2759789Sahrens zerror(zlogp, B_FALSE, "invalid configuration"); 2760789Sahrens zonecfg_fini_handle(handle); 2761789Sahrens return (-1); 2762789Sahrens } 2763789Sahrens 27642082Seschrock if ((hdl = libzfs_init()) == NULL) { 27652082Seschrock zerror(zlogp, B_FALSE, "opening ZFS library"); 27662082Seschrock zonecfg_fini_handle(handle); 27672082Seschrock return (-1); 27682082Seschrock } 2769789Sahrens 2770789Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 2771789Sahrens 27722082Seschrock if ((zhp = zfs_open(hdl, dstab.zone_dataset_name, 2773789Sahrens ZFS_TYPE_FILESYSTEM)) == NULL) { 2774789Sahrens zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'", 2775789Sahrens dstab.zone_dataset_name); 2776789Sahrens zonecfg_fini_handle(handle); 27772082Seschrock libzfs_fini(hdl); 2778789Sahrens return (-1); 2779789Sahrens } 2780789Sahrens 2781789Sahrens /* 2782789Sahrens * Automatically set the 'zoned' property. We check the value 2783789Sahrens * first because we'll get EPERM if it is already set. 2784789Sahrens */ 2785789Sahrens if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 27862676Seschrock zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED), 27872676Seschrock "on") != 0) { 2788789Sahrens zerror(zlogp, B_FALSE, "cannot set 'zoned' " 2789789Sahrens "property for ZFS dataset '%s'\n", 2790789Sahrens dstab.zone_dataset_name); 2791789Sahrens zonecfg_fini_handle(handle); 2792789Sahrens zfs_close(zhp); 27932082Seschrock libzfs_fini(hdl); 2794789Sahrens return (-1); 2795789Sahrens } 2796789Sahrens 2797789Sahrens zfs_close(zhp); 2798789Sahrens } 2799789Sahrens (void) zonecfg_enddsent(handle); 2800789Sahrens 2801789Sahrens zonecfg_fini_handle(handle); 28022082Seschrock libzfs_fini(hdl); 2803789Sahrens 2804789Sahrens return (0); 2805789Sahrens } 2806789Sahrens 28071676Sjpk /* 28081676Sjpk * Mount lower level home directories into/from current zone 28091676Sjpk * Share exported directories specified in dfstab for zone 28101676Sjpk */ 28111676Sjpk static int 28121676Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath) 28131676Sjpk { 28141676Sjpk zoneid_t *zids = NULL; 28151676Sjpk priv_set_t *zid_privs; 28161676Sjpk const priv_impl_info_t *ip = NULL; 28171676Sjpk uint_t nzents_saved; 28181676Sjpk uint_t nzents; 28191676Sjpk int i; 28201676Sjpk char readonly[] = "ro"; 28211676Sjpk struct zone_fstab lower_fstab; 28221676Sjpk char *argv[4]; 28231676Sjpk 28241676Sjpk if (!is_system_labeled()) 28251676Sjpk return (0); 28261676Sjpk 28271676Sjpk if (zid_label == NULL) { 28281676Sjpk zid_label = m_label_alloc(MAC_LABEL); 28291676Sjpk if (zid_label == NULL) 28301676Sjpk return (-1); 28311676Sjpk } 28321676Sjpk 28331676Sjpk /* Make sure our zone has an /export/home dir */ 28341676Sjpk (void) make_one_dir(zlogp, rootpath, "/export/home", 28351676Sjpk DEFAULT_DIR_MODE); 28361676Sjpk 28371676Sjpk lower_fstab.zone_fs_raw[0] = '\0'; 28381676Sjpk (void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS, 28391676Sjpk sizeof (lower_fstab.zone_fs_type)); 28401676Sjpk lower_fstab.zone_fs_options = NULL; 28411676Sjpk (void) zonecfg_add_fs_option(&lower_fstab, readonly); 28421676Sjpk 28431676Sjpk /* 28441676Sjpk * Get the list of zones from the kernel 28451676Sjpk */ 28461676Sjpk if (zone_list(NULL, &nzents) != 0) { 28471676Sjpk zerror(zlogp, B_TRUE, "unable to list zones"); 28481676Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 28491676Sjpk return (-1); 28501676Sjpk } 28511676Sjpk again: 28521676Sjpk if (nzents == 0) { 28531676Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 28541676Sjpk return (-1); 28551676Sjpk } 28561676Sjpk 28571676Sjpk zids = malloc(nzents * sizeof (zoneid_t)); 28581676Sjpk if (zids == NULL) { 28592267Sdp zerror(zlogp, B_TRUE, "memory allocation failed"); 28601676Sjpk return (-1); 28611676Sjpk } 28621676Sjpk nzents_saved = nzents; 28631676Sjpk 28641676Sjpk if (zone_list(zids, &nzents) != 0) { 28651676Sjpk zerror(zlogp, B_TRUE, "unable to list zones"); 28661676Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 28671676Sjpk free(zids); 28681676Sjpk return (-1); 28691676Sjpk } 28701676Sjpk if (nzents != nzents_saved) { 28711676Sjpk /* list changed, try again */ 28721676Sjpk free(zids); 28731676Sjpk goto again; 28741676Sjpk } 28751676Sjpk 28761676Sjpk ip = getprivimplinfo(); 28771676Sjpk if ((zid_privs = priv_allocset()) == NULL) { 28781676Sjpk zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 28791676Sjpk zonecfg_free_fs_option_list( 28801676Sjpk lower_fstab.zone_fs_options); 28811676Sjpk free(zids); 28821676Sjpk return (-1); 28831676Sjpk } 28841676Sjpk 28851676Sjpk for (i = 0; i < nzents; i++) { 28861676Sjpk char zid_name[ZONENAME_MAX]; 28871676Sjpk zone_state_t zid_state; 28881676Sjpk char zid_rpath[MAXPATHLEN]; 28891676Sjpk struct stat stat_buf; 28901676Sjpk 28911676Sjpk if (zids[i] == GLOBAL_ZONEID) 28921676Sjpk continue; 28931676Sjpk 28941676Sjpk if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1) 28951676Sjpk continue; 28961676Sjpk 28971676Sjpk /* 28981676Sjpk * Do special setup for the zone we are booting 28991676Sjpk */ 29001676Sjpk if (strcmp(zid_name, zone_name) == 0) { 29011676Sjpk struct zone_fstab autofs_fstab; 29021676Sjpk char map_path[MAXPATHLEN]; 29031676Sjpk int fd; 29041676Sjpk 29051676Sjpk /* 29061676Sjpk * Create auto_home_<zone> map for this zone 29072712Snn35248 * in the global zone. The non-global zone entry 29081676Sjpk * will be created by automount when the zone 29091676Sjpk * is booted. 29101676Sjpk */ 29111676Sjpk 29121676Sjpk (void) snprintf(autofs_fstab.zone_fs_special, 29131676Sjpk MAXPATHLEN, "auto_home_%s", zid_name); 29141676Sjpk 29151676Sjpk (void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN, 29161676Sjpk "/zone/%s/home", zid_name); 29171676Sjpk 29181676Sjpk (void) snprintf(map_path, sizeof (map_path), 29191676Sjpk "/etc/%s", autofs_fstab.zone_fs_special); 29201676Sjpk /* 29211676Sjpk * If the map file doesn't exist create a template 29221676Sjpk */ 29231676Sjpk if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL, 29241676Sjpk S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) { 29251676Sjpk int len; 29261676Sjpk char map_rec[MAXPATHLEN]; 29271676Sjpk 29281676Sjpk len = snprintf(map_rec, sizeof (map_rec), 29291676Sjpk "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n", 29301676Sjpk autofs_fstab.zone_fs_special, rootpath); 29311676Sjpk (void) write(fd, map_rec, len); 29321676Sjpk (void) close(fd); 29331676Sjpk } 29341676Sjpk 29351676Sjpk /* 29361676Sjpk * Mount auto_home_<zone> in the global zone if absent. 29371676Sjpk * If it's already of type autofs, then 29381676Sjpk * don't mount it again. 29391676Sjpk */ 29401676Sjpk if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) || 29411676Sjpk strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) { 29421676Sjpk char optstr[] = "indirect,ignore,nobrowse"; 29431676Sjpk 29441676Sjpk (void) make_one_dir(zlogp, "", 29451676Sjpk autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE); 29461676Sjpk 29471676Sjpk /* 29481676Sjpk * Mount will fail if automounter has already 29491676Sjpk * processed the auto_home_<zonename> map 29501676Sjpk */ 29511676Sjpk (void) domount(zlogp, MNTTYPE_AUTOFS, optstr, 29521676Sjpk autofs_fstab.zone_fs_special, 29531676Sjpk autofs_fstab.zone_fs_dir); 29541676Sjpk } 29551676Sjpk continue; 29561676Sjpk } 29571676Sjpk 29581676Sjpk 29591676Sjpk if (zone_get_state(zid_name, &zid_state) != Z_OK || 29601769Scarlsonj (zid_state != ZONE_STATE_READY && 29611769Scarlsonj zid_state != ZONE_STATE_RUNNING)) 29621676Sjpk /* Skip over zones without mounted filesystems */ 29631676Sjpk continue; 29641676Sjpk 29651676Sjpk if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label, 29661676Sjpk sizeof (m_label_t)) < 0) 29671676Sjpk /* Skip over zones with unspecified label */ 29681676Sjpk continue; 29691676Sjpk 29701676Sjpk if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath, 29711676Sjpk sizeof (zid_rpath)) == -1) 29721676Sjpk /* Skip over zones with bad path */ 29731676Sjpk continue; 29741676Sjpk 29751676Sjpk if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs, 29761676Sjpk sizeof (priv_chunk_t) * ip->priv_setsize) == -1) 29771676Sjpk /* Skip over zones with bad privs */ 29781676Sjpk continue; 29791676Sjpk 29801676Sjpk /* 29811676Sjpk * Reading down is valid according to our label model 29821676Sjpk * but some customers want to disable it because it 29831676Sjpk * allows execute down and other possible attacks. 29841676Sjpk * Therefore, we restrict this feature to zones that 29851676Sjpk * have the NET_MAC_AWARE privilege which is required 29861676Sjpk * for NFS read-down semantics. 29871676Sjpk */ 29881676Sjpk if ((bldominates(zlabel, zid_label)) && 29891676Sjpk (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) { 29901676Sjpk /* 29911676Sjpk * Our zone dominates this one. 29921676Sjpk * Create a lofs mount from lower zone's /export/home 29931676Sjpk */ 29941676Sjpk (void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN, 29951676Sjpk "%s/zone/%s/export/home", rootpath, zid_name); 29961676Sjpk 29971676Sjpk /* 29981676Sjpk * If the target is already an LOFS mount 29991676Sjpk * then don't do it again. 30001676Sjpk */ 30011676Sjpk if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) || 30021676Sjpk strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) { 30031676Sjpk 30041676Sjpk if (snprintf(lower_fstab.zone_fs_special, 30051676Sjpk MAXPATHLEN, "%s/export", 30061676Sjpk zid_rpath) > MAXPATHLEN) 30071676Sjpk continue; 30081676Sjpk 30091676Sjpk /* 30101676Sjpk * Make sure the lower-level home exists 30111676Sjpk */ 30121676Sjpk if (make_one_dir(zlogp, 30131676Sjpk lower_fstab.zone_fs_special, 30141676Sjpk "/home", DEFAULT_DIR_MODE) != 0) 30151676Sjpk continue; 30161676Sjpk 30171676Sjpk (void) strlcat(lower_fstab.zone_fs_special, 30181676Sjpk "/home", MAXPATHLEN); 30191676Sjpk 30201676Sjpk /* 30211676Sjpk * Mount can fail because the lower-level 30221676Sjpk * zone may have already done a mount up. 30231676Sjpk */ 30241676Sjpk (void) mount_one(zlogp, &lower_fstab, ""); 30251676Sjpk } 30261676Sjpk } else if ((bldominates(zid_label, zlabel)) && 30271676Sjpk (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) { 30281676Sjpk /* 30291676Sjpk * This zone dominates our zone. 30301676Sjpk * Create a lofs mount from our zone's /export/home 30311676Sjpk */ 30321676Sjpk if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN, 30331676Sjpk "%s/zone/%s/export/home", zid_rpath, 30341676Sjpk zone_name) > MAXPATHLEN) 30351676Sjpk continue; 30361676Sjpk 30371676Sjpk /* 30381676Sjpk * If the target is already an LOFS mount 30391676Sjpk * then don't do it again. 30401676Sjpk */ 30411676Sjpk if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) || 30421676Sjpk strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) { 30431676Sjpk 30441676Sjpk (void) snprintf(lower_fstab.zone_fs_special, 30451676Sjpk MAXPATHLEN, "%s/export/home", rootpath); 30461676Sjpk 30471676Sjpk /* 30481676Sjpk * Mount can fail because the higher-level 30491676Sjpk * zone may have already done a mount down. 30501676Sjpk */ 30511676Sjpk (void) mount_one(zlogp, &lower_fstab, ""); 30521676Sjpk } 30531676Sjpk } 30541676Sjpk } 30551676Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 30561676Sjpk priv_freeset(zid_privs); 30571676Sjpk free(zids); 30581676Sjpk 30591676Sjpk /* 30601676Sjpk * Now share any exported directories from this zone. 30611676Sjpk * Each zone can have its own dfstab. 30621676Sjpk */ 30631676Sjpk 30641676Sjpk argv[0] = "zoneshare"; 30651676Sjpk argv[1] = "-z"; 30661676Sjpk argv[2] = zone_name; 30671676Sjpk argv[3] = NULL; 30681676Sjpk 30691676Sjpk (void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv); 30701676Sjpk /* Don't check for errors since they don't affect the zone */ 30711676Sjpk 30721676Sjpk return (0); 30731676Sjpk } 30741676Sjpk 30751676Sjpk /* 30761676Sjpk * Unmount lofs mounts from higher level zones 30771676Sjpk * Unshare nfs exported directories 30781676Sjpk */ 30791676Sjpk static void 30801676Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name) 30811676Sjpk { 30821676Sjpk zoneid_t *zids = NULL; 30831676Sjpk uint_t nzents_saved; 30841676Sjpk uint_t nzents; 30851676Sjpk int i; 30861676Sjpk char *argv[4]; 30871676Sjpk char path[MAXPATHLEN]; 30881676Sjpk 30891676Sjpk if (!is_system_labeled()) 30901676Sjpk return; 30911676Sjpk 30921676Sjpk /* 30931676Sjpk * Get the list of zones from the kernel 30941676Sjpk */ 30951676Sjpk if (zone_list(NULL, &nzents) != 0) { 30961676Sjpk return; 30971676Sjpk } 30981676Sjpk 30991676Sjpk if (zid_label == NULL) { 31001676Sjpk zid_label = m_label_alloc(MAC_LABEL); 31011676Sjpk if (zid_label == NULL) 31021676Sjpk return; 31031676Sjpk } 31041676Sjpk 31051676Sjpk again: 31061676Sjpk if (nzents == 0) 31071676Sjpk return; 31081676Sjpk 31091676Sjpk zids = malloc(nzents * sizeof (zoneid_t)); 31101676Sjpk if (zids == NULL) { 31112267Sdp zerror(zlogp, B_TRUE, "memory allocation failed"); 31121676Sjpk return; 31131676Sjpk } 31141676Sjpk nzents_saved = nzents; 31151676Sjpk 31161676Sjpk if (zone_list(zids, &nzents) != 0) { 31171676Sjpk free(zids); 31181676Sjpk return; 31191676Sjpk } 31201676Sjpk if (nzents != nzents_saved) { 31211676Sjpk /* list changed, try again */ 31221676Sjpk free(zids); 31231676Sjpk goto again; 31241676Sjpk } 31251676Sjpk 31261676Sjpk for (i = 0; i < nzents; i++) { 31271676Sjpk char zid_name[ZONENAME_MAX]; 31281676Sjpk zone_state_t zid_state; 31291676Sjpk char zid_rpath[MAXPATHLEN]; 31301676Sjpk 31311676Sjpk if (zids[i] == GLOBAL_ZONEID) 31321676Sjpk continue; 31331676Sjpk 31341676Sjpk if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1) 31351676Sjpk continue; 31361676Sjpk 31371676Sjpk /* 31381676Sjpk * Skip the zone we are halting 31391676Sjpk */ 31401676Sjpk if (strcmp(zid_name, zone_name) == 0) 31411676Sjpk continue; 31421676Sjpk 31431676Sjpk if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state, 31441676Sjpk sizeof (zid_state)) < 0) || 31451676Sjpk (zid_state < ZONE_IS_READY)) 31461676Sjpk /* Skip over zones without mounted filesystems */ 31471676Sjpk continue; 31481676Sjpk 31491676Sjpk if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label, 31501676Sjpk sizeof (m_label_t)) < 0) 31511676Sjpk /* Skip over zones with unspecified label */ 31521676Sjpk continue; 31531676Sjpk 31541676Sjpk if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath, 31551676Sjpk sizeof (zid_rpath)) == -1) 31561676Sjpk /* Skip over zones with bad path */ 31571676Sjpk continue; 31581676Sjpk 31591676Sjpk if (zlabel != NULL && bldominates(zid_label, zlabel)) { 31601676Sjpk /* 31611676Sjpk * This zone dominates our zone. 31621676Sjpk * Unmount the lofs mount of our zone's /export/home 31631676Sjpk */ 31641676Sjpk 31651676Sjpk if (snprintf(path, MAXPATHLEN, 31661676Sjpk "%s/zone/%s/export/home", zid_rpath, 31671676Sjpk zone_name) > MAXPATHLEN) 31681676Sjpk continue; 31691676Sjpk 31701676Sjpk /* Skip over mount failures */ 31711676Sjpk (void) umount(path); 31721676Sjpk } 31731676Sjpk } 31741676Sjpk free(zids); 31751676Sjpk 31761676Sjpk /* 31771676Sjpk * Unmount global zone autofs trigger for this zone 31781676Sjpk */ 31791676Sjpk (void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name); 31801676Sjpk /* Skip over mount failures */ 31811676Sjpk (void) umount(path); 31821676Sjpk 31831676Sjpk /* 31841676Sjpk * Next unshare any exported directories from this zone. 31851676Sjpk */ 31861676Sjpk 31871676Sjpk argv[0] = "zoneunshare"; 31881676Sjpk argv[1] = "-z"; 31891676Sjpk argv[2] = zone_name; 31901676Sjpk argv[3] = NULL; 31911676Sjpk 31921676Sjpk (void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv); 31931676Sjpk /* Don't check for errors since they don't affect the zone */ 31941676Sjpk 31951676Sjpk /* 31961676Sjpk * Finally, deallocate any devices in the zone. 31971676Sjpk */ 31981676Sjpk 31991676Sjpk argv[0] = "deallocate"; 32001676Sjpk argv[1] = "-Isz"; 32011676Sjpk argv[2] = zone_name; 32021676Sjpk argv[3] = NULL; 32031676Sjpk 32041676Sjpk (void) forkexec(zlogp, "/usr/sbin/deallocate", argv); 32051676Sjpk /* Don't check for errors since they don't affect the zone */ 32061676Sjpk } 32071676Sjpk 32081676Sjpk /* 32091676Sjpk * Fetch the Trusted Extensions label and multi-level ports (MLPs) for 32101676Sjpk * this zone. 32111676Sjpk */ 32121676Sjpk static tsol_zcent_t * 32131676Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs) 32141676Sjpk { 32151676Sjpk FILE *fp; 32161676Sjpk tsol_zcent_t *zcent = NULL; 32171676Sjpk char line[MAXTNZLEN]; 32181676Sjpk 32191676Sjpk if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) { 32201676Sjpk zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH); 32211676Sjpk return (NULL); 32221676Sjpk } 32231676Sjpk 32241676Sjpk while (fgets(line, sizeof (line), fp) != NULL) { 32251676Sjpk /* 32261676Sjpk * Check for malformed database 32271676Sjpk */ 32281676Sjpk if (strlen(line) == MAXTNZLEN - 1) 32291676Sjpk break; 32301676Sjpk if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL) 32311676Sjpk continue; 32321676Sjpk if (strcmp(zcent->zc_name, zone_name) == 0) 32331676Sjpk break; 32341676Sjpk tsol_freezcent(zcent); 32351676Sjpk zcent = NULL; 32361676Sjpk } 32371676Sjpk (void) fclose(fp); 32381676Sjpk 32391676Sjpk if (zcent == NULL) { 32401676Sjpk zerror(zlogp, B_FALSE, "zone requires a label assignment. " 32411676Sjpk "See tnzonecfg(4)"); 32421676Sjpk } else { 32431676Sjpk if (zlabel == NULL) 32441676Sjpk zlabel = m_label_alloc(MAC_LABEL); 32451676Sjpk /* 32461676Sjpk * Save this zone's privileges for later read-down processing 32471676Sjpk */ 32481676Sjpk if ((zprivs = priv_allocset()) == NULL) { 32491676Sjpk zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 32501676Sjpk return (NULL); 32511676Sjpk } else { 32521676Sjpk priv_copyset(privs, zprivs); 32531676Sjpk } 32541676Sjpk } 32551676Sjpk return (zcent); 32561676Sjpk } 32571676Sjpk 32581676Sjpk /* 32591676Sjpk * Add the Trusted Extensions multi-level ports for this zone. 32601676Sjpk */ 32611676Sjpk static void 32621676Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent) 32631676Sjpk { 32641676Sjpk tsol_mlp_t *mlp; 32651676Sjpk tsol_mlpent_t tsme; 32661676Sjpk 32671676Sjpk if (!is_system_labeled()) 32681676Sjpk return; 32691676Sjpk 32701676Sjpk tsme.tsme_zoneid = zoneid; 32711676Sjpk tsme.tsme_flags = 0; 32721676Sjpk for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) { 32731676Sjpk tsme.tsme_mlp = *mlp; 32741676Sjpk if (tnmlp(TNDB_LOAD, &tsme) != 0) { 32751676Sjpk zerror(zlogp, B_TRUE, "cannot set zone-specific MLP " 32761676Sjpk "on %d-%d/%d", mlp->mlp_port, 32771676Sjpk mlp->mlp_port_upper, mlp->mlp_ipp); 32781676Sjpk } 32791676Sjpk } 32801676Sjpk 32811676Sjpk tsme.tsme_flags = TSOL_MEF_SHARED; 32821676Sjpk for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) { 32831676Sjpk tsme.tsme_mlp = *mlp; 32841676Sjpk if (tnmlp(TNDB_LOAD, &tsme) != 0) { 32851676Sjpk zerror(zlogp, B_TRUE, "cannot set shared MLP " 32861676Sjpk "on %d-%d/%d", mlp->mlp_port, 32871676Sjpk mlp->mlp_port_upper, mlp->mlp_ipp); 32881676Sjpk } 32891676Sjpk } 32901676Sjpk } 32911676Sjpk 32921676Sjpk static void 32931676Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid) 32941676Sjpk { 32951676Sjpk tsol_mlpent_t tsme; 32961676Sjpk 32971676Sjpk if (!is_system_labeled()) 32981676Sjpk return; 32991676Sjpk 33001676Sjpk (void) memset(&tsme, 0, sizeof (tsme)); 33011676Sjpk tsme.tsme_zoneid = zoneid; 33021676Sjpk if (tnmlp(TNDB_FLUSH, &tsme) != 0) 33031676Sjpk zerror(zlogp, B_TRUE, "cannot flush MLPs"); 33041676Sjpk } 33051676Sjpk 33060Sstevel@tonic-gate int 33070Sstevel@tonic-gate prtmount(const char *fs, void *x) { 33080Sstevel@tonic-gate zerror((zlog_t *)x, B_FALSE, " %s", fs); 33090Sstevel@tonic-gate return (0); 33100Sstevel@tonic-gate } 33110Sstevel@tonic-gate 3312766Scarlsonj /* 3313766Scarlsonj * Look for zones running on the main system that are using this root (or any 3314766Scarlsonj * subdirectory of it). Return B_TRUE and print an error if a conflicting zone 3315766Scarlsonj * is found or if we can't tell. 3316766Scarlsonj */ 3317766Scarlsonj static boolean_t 3318766Scarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath) 33190Sstevel@tonic-gate { 3320766Scarlsonj zoneid_t *zids = NULL; 3321766Scarlsonj uint_t nzids = 0; 3322766Scarlsonj boolean_t retv; 3323766Scarlsonj int rlen, zlen; 3324766Scarlsonj char zroot[MAXPATHLEN]; 3325766Scarlsonj char zonename[ZONENAME_MAX]; 3326766Scarlsonj 3327766Scarlsonj for (;;) { 3328766Scarlsonj nzids += 10; 3329766Scarlsonj zids = malloc(nzids * sizeof (*zids)); 3330766Scarlsonj if (zids == NULL) { 33312267Sdp zerror(zlogp, B_TRUE, "memory allocation failed"); 3332766Scarlsonj return (B_TRUE); 3333766Scarlsonj } 3334766Scarlsonj if (zone_list(zids, &nzids) == 0) 3335766Scarlsonj break; 3336766Scarlsonj free(zids); 3337766Scarlsonj } 3338766Scarlsonj retv = B_FALSE; 3339766Scarlsonj rlen = strlen(rootpath); 3340766Scarlsonj while (nzids > 0) { 3341766Scarlsonj /* 3342766Scarlsonj * Ignore errors; they just mean that the zone has disappeared 3343766Scarlsonj * while we were busy. 3344766Scarlsonj */ 3345766Scarlsonj if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot, 3346766Scarlsonj sizeof (zroot)) == -1) 3347766Scarlsonj continue; 3348766Scarlsonj zlen = strlen(zroot); 3349766Scarlsonj if (zlen > rlen) 3350766Scarlsonj zlen = rlen; 3351766Scarlsonj if (strncmp(rootpath, zroot, zlen) == 0 && 3352766Scarlsonj (zroot[zlen] == '\0' || zroot[zlen] == '/') && 3353766Scarlsonj (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) { 3354766Scarlsonj if (getzonenamebyid(zids[nzids], zonename, 3355766Scarlsonj sizeof (zonename)) == -1) 3356766Scarlsonj (void) snprintf(zonename, sizeof (zonename), 3357766Scarlsonj "id %d", (int)zids[nzids]); 3358766Scarlsonj zerror(zlogp, B_FALSE, 3359766Scarlsonj "zone root %s already in use by zone %s", 3360766Scarlsonj rootpath, zonename); 3361766Scarlsonj retv = B_TRUE; 3362766Scarlsonj break; 3363766Scarlsonj } 3364766Scarlsonj } 3365766Scarlsonj free(zids); 3366766Scarlsonj return (retv); 3367766Scarlsonj } 3368766Scarlsonj 3369766Scarlsonj /* 3370766Scarlsonj * Search for loopback mounts that use this same source node (same device and 3371766Scarlsonj * inode). Return B_TRUE if there is one or if we can't tell. 3372766Scarlsonj */ 3373766Scarlsonj static boolean_t 3374766Scarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath) 3375766Scarlsonj { 3376766Scarlsonj struct stat64 rst, zst; 3377766Scarlsonj struct mnttab *mnp; 3378766Scarlsonj 3379766Scarlsonj if (stat64(rootpath, &rst) == -1) { 3380766Scarlsonj zerror(zlogp, B_TRUE, "can't stat %s", rootpath); 3381766Scarlsonj return (B_TRUE); 3382766Scarlsonj } 3383766Scarlsonj if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 3384766Scarlsonj return (B_TRUE); 3385766Scarlsonj for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) { 3386766Scarlsonj if (mnp->mnt_fstype == NULL || 3387766Scarlsonj strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0) 3388766Scarlsonj continue; 3389766Scarlsonj /* We're looking at a loopback mount. Stat it. */ 3390766Scarlsonj if (mnp->mnt_special != NULL && 3391766Scarlsonj stat64(mnp->mnt_special, &zst) != -1 && 3392766Scarlsonj rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) { 3393766Scarlsonj zerror(zlogp, B_FALSE, 3394766Scarlsonj "zone root %s is reachable through %s", 3395766Scarlsonj rootpath, mnp->mnt_mountp); 3396766Scarlsonj return (B_TRUE); 3397766Scarlsonj } 3398766Scarlsonj } 3399766Scarlsonj return (B_FALSE); 3400766Scarlsonj } 3401766Scarlsonj 34023247Sgjelinek /* 34033247Sgjelinek * Set memory cap and pool info for the zone's resource management 34043247Sgjelinek * configuration. 34053247Sgjelinek */ 34063247Sgjelinek static int 34073247Sgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid) 34083247Sgjelinek { 34093247Sgjelinek int res; 34103247Sgjelinek uint64_t tmp; 34113247Sgjelinek struct zone_mcaptab mcap; 34123247Sgjelinek char sched[MAXNAMELEN]; 34133247Sgjelinek zone_dochandle_t handle = NULL; 34143247Sgjelinek char pool_err[128]; 34153247Sgjelinek 34163247Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 34173247Sgjelinek zerror(zlogp, B_TRUE, "getting zone configuration handle"); 34183247Sgjelinek return (Z_BAD_HANDLE); 34193247Sgjelinek } 34203247Sgjelinek 34213247Sgjelinek if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) { 34223247Sgjelinek zerror(zlogp, B_FALSE, "invalid configuration"); 34233247Sgjelinek zonecfg_fini_handle(handle); 34243247Sgjelinek return (res); 34253247Sgjelinek } 34263247Sgjelinek 34273247Sgjelinek /* 34283247Sgjelinek * If a memory cap is configured, set the cap in the kernel using 34293247Sgjelinek * zone_setattr() and make sure the rcapd SMF service is enabled. 34303247Sgjelinek */ 34313247Sgjelinek if (zonecfg_getmcapent(handle, &mcap) == Z_OK) { 34323247Sgjelinek uint64_t num; 34333247Sgjelinek char smf_err[128]; 34343247Sgjelinek 34353247Sgjelinek num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10); 34363247Sgjelinek if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) { 34373247Sgjelinek zerror(zlogp, B_TRUE, "could not set zone memory cap"); 34383247Sgjelinek zonecfg_fini_handle(handle); 34393247Sgjelinek return (Z_INVAL); 34403247Sgjelinek } 34413247Sgjelinek 34423247Sgjelinek if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) { 34433247Sgjelinek zerror(zlogp, B_FALSE, "enabling system/rcap service " 34443247Sgjelinek "failed: %s", smf_err); 34453247Sgjelinek zonecfg_fini_handle(handle); 34463247Sgjelinek return (Z_INVAL); 34473247Sgjelinek } 34483247Sgjelinek } 34493247Sgjelinek 34503247Sgjelinek /* Get the scheduling class set in the zone configuration. */ 34513247Sgjelinek if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK && 34523247Sgjelinek strlen(sched) > 0) { 34533247Sgjelinek if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched, 34543247Sgjelinek strlen(sched)) == -1) 34553247Sgjelinek zerror(zlogp, B_TRUE, "WARNING: unable to set the " 34563247Sgjelinek "default scheduling class"); 34573247Sgjelinek 34583247Sgjelinek } else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp) 34593247Sgjelinek == Z_OK) { 34603247Sgjelinek /* 34613247Sgjelinek * If the zone has the zone.cpu-shares rctl set then we want to 34623247Sgjelinek * use the Fair Share Scheduler (FSS) for processes in the 34633247Sgjelinek * zone. Check what scheduling class the zone would be running 34643247Sgjelinek * in by default so we can print a warning and modify the class 34653247Sgjelinek * if we wouldn't be using FSS. 34663247Sgjelinek */ 34673247Sgjelinek char class_name[PC_CLNMSZ]; 34683247Sgjelinek 34693247Sgjelinek if (zonecfg_get_dflt_sched_class(handle, class_name, 34703247Sgjelinek sizeof (class_name)) != Z_OK) { 34713247Sgjelinek zerror(zlogp, B_FALSE, "WARNING: unable to determine " 34723247Sgjelinek "the zone's scheduling class"); 34733247Sgjelinek 34743247Sgjelinek } else if (strcmp("FSS", class_name) != 0) { 34753247Sgjelinek zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares " 34763247Sgjelinek "rctl is set but\nFSS is not the default " 34773247Sgjelinek "scheduling class for\nthis zone. FSS will be " 34783247Sgjelinek "used for processes\nin the zone but to get the " 34793247Sgjelinek "full benefit of FSS,\nit should be the default " 34803247Sgjelinek "scheduling class.\nSee dispadmin(1M) for more " 34813247Sgjelinek "details."); 34823247Sgjelinek 34833247Sgjelinek if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS", 34843247Sgjelinek strlen("FSS")) == -1) 34853247Sgjelinek zerror(zlogp, B_TRUE, "WARNING: unable to set " 34863247Sgjelinek "zone scheduling class to FSS"); 34873247Sgjelinek } 34883247Sgjelinek } 34893247Sgjelinek 34903247Sgjelinek /* 34913247Sgjelinek * The next few blocks of code attempt to set up temporary pools as 34923247Sgjelinek * well as persistent pools. In all cases we call the functions 34933247Sgjelinek * unconditionally. Within each funtion the code will check if the 34943247Sgjelinek * zone is actually configured for a temporary pool or persistent pool 34953247Sgjelinek * and just return if there is nothing to do. 34963247Sgjelinek * 34973247Sgjelinek * If we are rebooting we want to attempt to reuse any temporary pool 34983247Sgjelinek * that was previously set up. zonecfg_bind_tmp_pool() will do the 34993247Sgjelinek * right thing in all cases (reuse or create) based on the current 35003247Sgjelinek * zonecfg. 35013247Sgjelinek */ 35023247Sgjelinek if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err, 35033247Sgjelinek sizeof (pool_err))) != Z_OK) { 35043247Sgjelinek if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND) 35053247Sgjelinek zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting " 35063247Sgjelinek "cannot be instantiated", zonecfg_strerror(res), 35073247Sgjelinek pool_err); 35083247Sgjelinek else 35093247Sgjelinek zerror(zlogp, B_FALSE, "could not bind zone to " 35103247Sgjelinek "temporary pool: %s", zonecfg_strerror(res)); 35113247Sgjelinek zonecfg_fini_handle(handle); 35123247Sgjelinek return (Z_POOL_BIND); 35133247Sgjelinek } 35143247Sgjelinek 35153247Sgjelinek /* 35163247Sgjelinek * Check if we need to warn about poold not being enabled. 35173247Sgjelinek */ 35183247Sgjelinek if (zonecfg_warn_poold(handle)) { 35193247Sgjelinek zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has " 35203247Sgjelinek "been specified\nbut the dynamic pool service is not " 35213247Sgjelinek "enabled.\nThe system will not dynamically adjust the\n" 35223247Sgjelinek "processor allocation within the specified range\n" 35233247Sgjelinek "until svc:/system/pools/dynamic is enabled.\n" 35243247Sgjelinek "See poold(1M)."); 35253247Sgjelinek } 35263247Sgjelinek 35273247Sgjelinek /* The following is a warning, not an error. */ 35283247Sgjelinek if ((res = zonecfg_bind_pool(handle, zoneid, pool_err, 35293247Sgjelinek sizeof (pool_err))) != Z_OK) { 35303247Sgjelinek if (res == Z_POOL_BIND) 35313247Sgjelinek zerror(zlogp, B_FALSE, "WARNING: unable to bind to " 35323247Sgjelinek "pool '%s'; using default pool.", pool_err); 35333247Sgjelinek else if (res == Z_POOL) 35343247Sgjelinek zerror(zlogp, B_FALSE, "WARNING: %s: %s", 35353247Sgjelinek zonecfg_strerror(res), pool_err); 35363247Sgjelinek else 35373247Sgjelinek zerror(zlogp, B_FALSE, "WARNING: %s", 35383247Sgjelinek zonecfg_strerror(res)); 35393247Sgjelinek } 35403247Sgjelinek 35413247Sgjelinek zonecfg_fini_handle(handle); 35423247Sgjelinek return (Z_OK); 35433247Sgjelinek } 35443247Sgjelinek 3545766Scarlsonj zoneid_t 3546766Scarlsonj vplat_create(zlog_t *zlogp, boolean_t mount_cmd) 3547766Scarlsonj { 3548766Scarlsonj zoneid_t rval = -1; 35490Sstevel@tonic-gate priv_set_t *privs; 35500Sstevel@tonic-gate char rootpath[MAXPATHLEN]; 35512712Snn35248 char modname[MAXPATHLEN]; 35522712Snn35248 struct brand_attr attr; 35532727Sedp brand_handle_t bh; 35540Sstevel@tonic-gate char *rctlbuf = NULL; 3555766Scarlsonj size_t rctlbufsz = 0; 3556789Sahrens char *zfsbuf = NULL; 3557789Sahrens size_t zfsbufsz = 0; 3558766Scarlsonj zoneid_t zoneid = -1; 35590Sstevel@tonic-gate int xerr; 3560766Scarlsonj char *kzone; 3561766Scarlsonj FILE *fp = NULL; 35621676Sjpk tsol_zcent_t *zcent = NULL; 35631676Sjpk int match = 0; 35641676Sjpk int doi = 0; 35650Sstevel@tonic-gate 35660Sstevel@tonic-gate if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) { 35670Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone root"); 35680Sstevel@tonic-gate return (-1); 35690Sstevel@tonic-gate } 3570766Scarlsonj if (zonecfg_in_alt_root()) 3571766Scarlsonj resolve_lofs(zlogp, rootpath, sizeof (rootpath)); 35720Sstevel@tonic-gate 35730Sstevel@tonic-gate if ((privs = priv_allocset()) == NULL) { 35740Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 35750Sstevel@tonic-gate return (-1); 35760Sstevel@tonic-gate } 35770Sstevel@tonic-gate priv_emptyset(privs); 35781645Scomay if (get_privset(zlogp, privs, mount_cmd) != 0) 35790Sstevel@tonic-gate goto error; 35801645Scomay 3581766Scarlsonj if (!mount_cmd && get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) { 35820Sstevel@tonic-gate zerror(zlogp, B_FALSE, "Unable to get list of rctls"); 35830Sstevel@tonic-gate goto error; 35840Sstevel@tonic-gate } 35851645Scomay 3586789Sahrens if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) { 3587789Sahrens zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets"); 3588789Sahrens goto error; 3589789Sahrens } 35900Sstevel@tonic-gate 35911769Scarlsonj if (!mount_cmd && is_system_labeled()) { 35921676Sjpk zcent = get_zone_label(zlogp, privs); 35931769Scarlsonj if (zcent != NULL) { 35941676Sjpk match = zcent->zc_match; 35951676Sjpk doi = zcent->zc_doi; 35961676Sjpk *zlabel = zcent->zc_label; 35971676Sjpk } else { 35981676Sjpk goto error; 35991676Sjpk } 36001676Sjpk } 36011676Sjpk 3602766Scarlsonj kzone = zone_name; 3603766Scarlsonj 3604766Scarlsonj /* 3605766Scarlsonj * We must do this scan twice. First, we look for zones running on the 3606766Scarlsonj * main system that are using this root (or any subdirectory of it). 3607766Scarlsonj * Next, we reduce to the shortest path and search for loopback mounts 3608766Scarlsonj * that use this same source node (same device and inode). 3609766Scarlsonj */ 3610766Scarlsonj if (duplicate_zone_root(zlogp, rootpath)) 3611766Scarlsonj goto error; 3612766Scarlsonj if (duplicate_reachable_path(zlogp, rootpath)) 3613766Scarlsonj goto error; 3614766Scarlsonj 3615766Scarlsonj if (mount_cmd) { 36162712Snn35248 assert(zone_isnative); 3617766Scarlsonj root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE); 3618766Scarlsonj 3619766Scarlsonj /* 3620766Scarlsonj * Forge up a special root for this zone. When a zone is 3621766Scarlsonj * mounted, we can't let the zone have its own root because the 3622766Scarlsonj * tools that will be used in this "scratch zone" need access 3623766Scarlsonj * to both the zone's resources and the running machine's 3624766Scarlsonj * executables. 3625766Scarlsonj * 3626766Scarlsonj * Note that the mkdir here also catches read-only filesystems. 3627766Scarlsonj */ 3628766Scarlsonj if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) { 3629766Scarlsonj zerror(zlogp, B_TRUE, "cannot create %s", rootpath); 3630766Scarlsonj goto error; 3631766Scarlsonj } 3632766Scarlsonj if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0) 3633766Scarlsonj goto error; 3634766Scarlsonj } 3635766Scarlsonj 3636766Scarlsonj if (zonecfg_in_alt_root()) { 3637766Scarlsonj /* 3638766Scarlsonj * If we are mounting up a zone in an alternate root partition, 3639766Scarlsonj * then we have some additional work to do before starting the 3640766Scarlsonj * zone. First, resolve the root path down so that we're not 3641766Scarlsonj * fooled by duplicates. Then forge up an internal name for 3642766Scarlsonj * the zone. 3643766Scarlsonj */ 3644766Scarlsonj if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) { 3645766Scarlsonj zerror(zlogp, B_TRUE, "cannot open mapfile"); 3646766Scarlsonj goto error; 3647766Scarlsonj } 3648766Scarlsonj if (zonecfg_lock_scratch(fp) != 0) { 3649766Scarlsonj zerror(zlogp, B_TRUE, "cannot lock mapfile"); 3650766Scarlsonj goto error; 3651766Scarlsonj } 3652766Scarlsonj if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(), 3653766Scarlsonj NULL, 0) == 0) { 3654766Scarlsonj zerror(zlogp, B_FALSE, "scratch zone already running"); 3655766Scarlsonj goto error; 3656766Scarlsonj } 3657766Scarlsonj /* This is the preferred name */ 3658766Scarlsonj (void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s", 3659766Scarlsonj zone_name); 3660766Scarlsonj srandom(getpid()); 3661766Scarlsonj while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL, 3662766Scarlsonj 0) == 0) { 3663766Scarlsonj /* This is just an arbitrary name; note "." usage */ 3664766Scarlsonj (void) snprintf(kernzone, sizeof (kernzone), 3665766Scarlsonj "SUNWlu.%08lX%08lX", random(), random()); 3666766Scarlsonj } 3667766Scarlsonj kzone = kernzone; 3668766Scarlsonj } 3669766Scarlsonj 36700Sstevel@tonic-gate xerr = 0; 3671766Scarlsonj if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf, 36721676Sjpk rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel)) == -1) { 36730Sstevel@tonic-gate if (xerr == ZE_AREMOUNTS) { 36740Sstevel@tonic-gate if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) { 36750Sstevel@tonic-gate zerror(zlogp, B_FALSE, 36760Sstevel@tonic-gate "An unknown file-system is mounted on " 36770Sstevel@tonic-gate "a subdirectory of %s", rootpath); 36780Sstevel@tonic-gate } else { 36790Sstevel@tonic-gate 36800Sstevel@tonic-gate zerror(zlogp, B_FALSE, 36810Sstevel@tonic-gate "These file-systems are mounted on " 36820Sstevel@tonic-gate "subdirectories of %s:", rootpath); 36830Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, 36840Sstevel@tonic-gate prtmount, zlogp); 36850Sstevel@tonic-gate } 36860Sstevel@tonic-gate } else if (xerr == ZE_CHROOTED) { 36870Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s: " 36880Sstevel@tonic-gate "cannot create a zone from a chrooted " 36890Sstevel@tonic-gate "environment", "zone_create"); 36900Sstevel@tonic-gate } else { 36910Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "zone_create"); 36920Sstevel@tonic-gate } 36930Sstevel@tonic-gate goto error; 36940Sstevel@tonic-gate } 3695766Scarlsonj 3696766Scarlsonj if (zonecfg_in_alt_root() && 3697766Scarlsonj zonecfg_add_scratch(fp, zone_name, kernzone, 3698766Scarlsonj zonecfg_get_root()) == -1) { 3699766Scarlsonj zerror(zlogp, B_TRUE, "cannot add mapfile entry"); 3700766Scarlsonj goto error; 3701766Scarlsonj } 3702766Scarlsonj 37032712Snn35248 if ((zone_get_brand(zone_name, attr.ba_brandname, 37042712Snn35248 MAXNAMELEN) != Z_OK) || 37052727Sedp (bh = brand_open(attr.ba_brandname)) == NULL) { 37062712Snn35248 zerror(zlogp, B_FALSE, "unable to determine brand name"); 37072712Snn35248 return (-1); 37082712Snn35248 } 37092712Snn35248 37102712Snn35248 /* 37112712Snn35248 * If this brand requires any kernel support, now is the time to 37122712Snn35248 * get it loaded and initialized. 37132712Snn35248 */ 37142727Sedp if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) { 37152712Snn35248 zerror(zlogp, B_FALSE, "unable to determine brand kernel " 37162712Snn35248 "module"); 37172712Snn35248 return (-1); 37182712Snn35248 } 37192712Snn35248 37202712Snn35248 if (strlen(modname) > 0) { 37212712Snn35248 (void) strlcpy(attr.ba_modname, modname, MAXPATHLEN); 37222712Snn35248 if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr, 37232712Snn35248 sizeof (attr) != 0)) { 37242712Snn35248 zerror(zlogp, B_TRUE, "could not set zone brand " 37252712Snn35248 "attribute."); 37262712Snn35248 goto error; 37272712Snn35248 } 37282712Snn35248 } 37292712Snn35248 37300Sstevel@tonic-gate /* 37313247Sgjelinek * The following actions are not performed when merely mounting a zone 37323247Sgjelinek * for administrative use. 37330Sstevel@tonic-gate */ 37343247Sgjelinek if (!mount_cmd) { 37353247Sgjelinek if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK) { 37363247Sgjelinek (void) zone_shutdown(zoneid); 37373247Sgjelinek goto error; 37383247Sgjelinek } 37393247Sgjelinek 37401769Scarlsonj set_mlps(zlogp, zoneid, zcent); 37413247Sgjelinek } 37423247Sgjelinek 3743766Scarlsonj rval = zoneid; 3744766Scarlsonj zoneid = -1; 3745766Scarlsonj 37460Sstevel@tonic-gate error: 3747766Scarlsonj if (zoneid != -1) 3748766Scarlsonj (void) zone_destroy(zoneid); 37490Sstevel@tonic-gate if (rctlbuf != NULL) 37500Sstevel@tonic-gate free(rctlbuf); 37510Sstevel@tonic-gate priv_freeset(privs); 3752766Scarlsonj if (fp != NULL) 3753766Scarlsonj zonecfg_close_scratch(fp); 3754766Scarlsonj lofs_discard_mnttab(); 37551676Sjpk if (zcent != NULL) 37561676Sjpk tsol_freezcent(zcent); 37570Sstevel@tonic-gate return (rval); 37580Sstevel@tonic-gate } 37590Sstevel@tonic-gate 37602303Scarlsonj /* 37612303Scarlsonj * Enter the zone and write a /etc/zones/index file there. This allows 37622303Scarlsonj * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone 37632303Scarlsonj * details from inside the zone. 37642303Scarlsonj */ 37652303Scarlsonj static void 37662303Scarlsonj write_index_file(zoneid_t zoneid) 37672303Scarlsonj { 37682303Scarlsonj FILE *zef; 37692303Scarlsonj FILE *zet; 37702303Scarlsonj struct zoneent *zep; 37712303Scarlsonj pid_t child; 37722303Scarlsonj int tmpl_fd; 37732303Scarlsonj ctid_t ct; 37742303Scarlsonj int fd; 37752303Scarlsonj char uuidstr[UUID_PRINTABLE_STRING_LENGTH]; 37762303Scarlsonj 37772303Scarlsonj /* Locate the zone entry in the global zone's index file */ 37782303Scarlsonj if ((zef = setzoneent()) == NULL) 37792303Scarlsonj return; 37802303Scarlsonj while ((zep = getzoneent_private(zef)) != NULL) { 37812303Scarlsonj if (strcmp(zep->zone_name, zone_name) == 0) 37822303Scarlsonj break; 37832303Scarlsonj free(zep); 37842303Scarlsonj } 37852303Scarlsonj endzoneent(zef); 37862303Scarlsonj if (zep == NULL) 37872303Scarlsonj return; 37882303Scarlsonj 37892303Scarlsonj if ((tmpl_fd = init_template()) == -1) { 37902303Scarlsonj free(zep); 37912303Scarlsonj return; 37922303Scarlsonj } 37932303Scarlsonj 37942303Scarlsonj if ((child = fork()) == -1) { 37952303Scarlsonj (void) ct_tmpl_clear(tmpl_fd); 37962303Scarlsonj (void) close(tmpl_fd); 37972303Scarlsonj free(zep); 37982303Scarlsonj return; 37992303Scarlsonj } 38002303Scarlsonj 38012303Scarlsonj /* parent waits for child to finish */ 38022303Scarlsonj if (child != 0) { 38032303Scarlsonj free(zep); 38042303Scarlsonj if (contract_latest(&ct) == -1) 38052303Scarlsonj ct = -1; 38062303Scarlsonj (void) ct_tmpl_clear(tmpl_fd); 38072303Scarlsonj (void) close(tmpl_fd); 38082303Scarlsonj (void) waitpid(child, NULL, 0); 38092303Scarlsonj (void) contract_abandon_id(ct); 38102303Scarlsonj return; 38112303Scarlsonj } 38122303Scarlsonj 38132303Scarlsonj /* child enters zone and sets up index file */ 38142303Scarlsonj (void) ct_tmpl_clear(tmpl_fd); 38152303Scarlsonj if (zone_enter(zoneid) != -1) { 38162303Scarlsonj (void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE); 38172303Scarlsonj (void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID, 38182303Scarlsonj ZONE_CONFIG_GID); 38192303Scarlsonj fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC, 38202303Scarlsonj ZONE_INDEX_MODE); 38212303Scarlsonj if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) { 38222303Scarlsonj (void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID); 38232303Scarlsonj if (uuid_is_null(zep->zone_uuid)) 38242303Scarlsonj uuidstr[0] = '\0'; 38252303Scarlsonj else 38262303Scarlsonj uuid_unparse(zep->zone_uuid, uuidstr); 38272303Scarlsonj (void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name, 38282303Scarlsonj zone_state_str(zep->zone_state), 38292303Scarlsonj uuidstr); 38302303Scarlsonj (void) fclose(zet); 38312303Scarlsonj } 38322303Scarlsonj } 38332303Scarlsonj _exit(0); 38342303Scarlsonj } 38352303Scarlsonj 38360Sstevel@tonic-gate int 38372303Scarlsonj vplat_bringup(zlog_t *zlogp, boolean_t mount_cmd, zoneid_t zoneid) 38380Sstevel@tonic-gate { 38392712Snn35248 char zonepath[MAXPATHLEN]; 38402503Sdp 3841789Sahrens if (!mount_cmd && validate_datasets(zlogp) != 0) { 3842789Sahrens lofs_discard_mnttab(); 3843789Sahrens return (-1); 3844789Sahrens } 3845789Sahrens 38462712Snn35248 /* 38472712Snn35248 * Before we try to mount filesystems we need to create the 38482712Snn35248 * attribute backing store for /dev 38492712Snn35248 */ 38502712Snn35248 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 38512712Snn35248 lofs_discard_mnttab(); 38522712Snn35248 return (-1); 38532712Snn35248 } 38543071Svp157776 38553071Svp157776 resolve_lofs(zlogp, zonepath, sizeof (zonepath)); 38562712Snn35248 if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE) != 0) { 38572712Snn35248 lofs_discard_mnttab(); 38582712Snn35248 return (-1); 38592712Snn35248 } 38602712Snn35248 38612621Sllai1 if (mount_filesystems(zlogp, mount_cmd) != 0) { 3862766Scarlsonj lofs_discard_mnttab(); 38630Sstevel@tonic-gate return (-1); 3864766Scarlsonj } 38652621Sllai1 38662621Sllai1 if (!mount_cmd && configure_network_interfaces(zlogp) != 0) { 3867766Scarlsonj lofs_discard_mnttab(); 38680Sstevel@tonic-gate return (-1); 3869766Scarlsonj } 38702303Scarlsonj 38712303Scarlsonj write_index_file(zoneid); 38722303Scarlsonj 3873766Scarlsonj lofs_discard_mnttab(); 38740Sstevel@tonic-gate return (0); 38750Sstevel@tonic-gate } 38760Sstevel@tonic-gate 3877766Scarlsonj static int 3878766Scarlsonj lu_root_teardown(zlog_t *zlogp) 3879766Scarlsonj { 3880766Scarlsonj char zroot[MAXPATHLEN]; 3881766Scarlsonj 38822712Snn35248 assert(zone_isnative); 38832712Snn35248 3884766Scarlsonj if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 3885766Scarlsonj zerror(zlogp, B_FALSE, "unable to determine zone root"); 3886766Scarlsonj return (-1); 3887766Scarlsonj } 3888766Scarlsonj root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE); 3889766Scarlsonj 3890766Scarlsonj /* 3891766Scarlsonj * At this point, the processes are gone, the filesystems (save the 3892766Scarlsonj * root) are unmounted, and the zone is on death row. But there may 3893766Scarlsonj * still be creds floating about in the system that reference the 3894766Scarlsonj * zone_t, and which pin down zone_rootvp causing this call to fail 3895766Scarlsonj * with EBUSY. Thus, we try for a little while before just giving up. 3896766Scarlsonj * (How I wish this were not true, and umount2 just did the right 3897766Scarlsonj * thing, or tmpfs supported MS_FORCE This is a gross hack.) 3898766Scarlsonj */ 3899766Scarlsonj if (umount2(zroot, MS_FORCE) != 0) { 3900766Scarlsonj if (errno == ENOTSUP && umount2(zroot, 0) == 0) 3901766Scarlsonj goto unmounted; 3902766Scarlsonj if (errno == EBUSY) { 3903766Scarlsonj int tries = 10; 3904766Scarlsonj 3905766Scarlsonj while (--tries >= 0) { 3906766Scarlsonj (void) sleep(1); 3907766Scarlsonj if (umount2(zroot, 0) == 0) 3908766Scarlsonj goto unmounted; 3909766Scarlsonj if (errno != EBUSY) 3910766Scarlsonj break; 3911766Scarlsonj } 3912766Scarlsonj } 3913766Scarlsonj zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot); 3914766Scarlsonj return (-1); 3915766Scarlsonj } 3916766Scarlsonj unmounted: 3917766Scarlsonj 3918766Scarlsonj /* 3919766Scarlsonj * Only zones in an alternate root environment have scratch zone 3920766Scarlsonj * entries. 3921766Scarlsonj */ 3922766Scarlsonj if (zonecfg_in_alt_root()) { 3923766Scarlsonj FILE *fp; 3924766Scarlsonj int retv; 3925766Scarlsonj 3926766Scarlsonj if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 3927766Scarlsonj zerror(zlogp, B_TRUE, "cannot open mapfile"); 3928766Scarlsonj return (-1); 3929766Scarlsonj } 3930766Scarlsonj retv = -1; 3931766Scarlsonj if (zonecfg_lock_scratch(fp) != 0) 3932766Scarlsonj zerror(zlogp, B_TRUE, "cannot lock mapfile"); 3933766Scarlsonj else if (zonecfg_delete_scratch(fp, kernzone) != 0) 3934766Scarlsonj zerror(zlogp, B_TRUE, "cannot delete map entry"); 3935766Scarlsonj else 3936766Scarlsonj retv = 0; 3937766Scarlsonj zonecfg_close_scratch(fp); 3938766Scarlsonj return (retv); 3939766Scarlsonj } else { 3940766Scarlsonj return (0); 3941766Scarlsonj } 3942766Scarlsonj } 3943766Scarlsonj 39440Sstevel@tonic-gate int 39453247Sgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting) 39460Sstevel@tonic-gate { 3947766Scarlsonj char *kzone; 39480Sstevel@tonic-gate zoneid_t zoneid; 39493247Sgjelinek int res; 39503247Sgjelinek char pool_err[128]; 39512712Snn35248 char zroot[MAXPATHLEN]; 39522712Snn35248 char cmdbuf[MAXPATHLEN]; 39532712Snn35248 char brand[MAXNAMELEN]; 39542727Sedp brand_handle_t bh = NULL; 39550Sstevel@tonic-gate 3956766Scarlsonj kzone = zone_name; 3957766Scarlsonj if (zonecfg_in_alt_root()) { 3958766Scarlsonj FILE *fp; 3959766Scarlsonj 3960766Scarlsonj if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 3961766Scarlsonj zerror(zlogp, B_TRUE, "unable to open map file"); 3962766Scarlsonj goto error; 3963766Scarlsonj } 3964766Scarlsonj if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(), 3965766Scarlsonj kernzone, sizeof (kernzone)) != 0) { 3966766Scarlsonj zerror(zlogp, B_FALSE, "unable to find scratch zone"); 3967766Scarlsonj zonecfg_close_scratch(fp); 3968766Scarlsonj goto error; 3969766Scarlsonj } 3970766Scarlsonj zonecfg_close_scratch(fp); 3971766Scarlsonj kzone = kernzone; 3972766Scarlsonj } 3973766Scarlsonj 3974766Scarlsonj if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) { 39750Sstevel@tonic-gate if (!bringup_failure_recovery) 39760Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid"); 3977766Scarlsonj if (unmount_cmd) 3978766Scarlsonj (void) lu_root_teardown(zlogp); 39790Sstevel@tonic-gate goto error; 39800Sstevel@tonic-gate } 39810Sstevel@tonic-gate 39820Sstevel@tonic-gate if (zone_shutdown(zoneid) != 0) { 39830Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to shutdown zone"); 39840Sstevel@tonic-gate goto error; 39850Sstevel@tonic-gate } 39860Sstevel@tonic-gate 39872712Snn35248 /* Get the path to the root of this zone */ 39882712Snn35248 if (zone_get_zonepath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 39892712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone root"); 39902712Snn35248 goto error; 39912712Snn35248 } 39922712Snn35248 39932712Snn35248 /* Get a handle to the brand info for this zone */ 39942712Snn35248 if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) || 39952727Sedp (bh = brand_open(brand)) == NULL) { 39962712Snn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 39972712Snn35248 return (-1); 39982712Snn35248 } 39992712Snn35248 /* 40002712Snn35248 * If there is a brand 'halt' callback, execute it now to give the 40012712Snn35248 * brand a chance to cleanup any custom configuration. 40022712Snn35248 */ 40032712Snn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 40042727Sedp if (brand_get_halt(bh, zone_name, zroot, cmdbuf + EXEC_LEN, 40052712Snn35248 sizeof (cmdbuf) - EXEC_LEN, 0, NULL) < 0) { 40062727Sedp brand_close(bh); 40072712Snn35248 zerror(zlogp, B_FALSE, "unable to determine branded zone's " 40082712Snn35248 "halt callback."); 40092712Snn35248 goto error; 40102712Snn35248 } 40112727Sedp brand_close(bh); 40122712Snn35248 40132712Snn35248 if ((strlen(cmdbuf) > EXEC_LEN) && 40142712Snn35248 (do_subproc(zlogp, cmdbuf) != Z_OK)) { 40152712Snn35248 zerror(zlogp, B_FALSE, "%s failed", cmdbuf); 40162712Snn35248 goto error; 40172712Snn35248 } 40182712Snn35248 4019766Scarlsonj if (!unmount_cmd && 4020766Scarlsonj unconfigure_network_interfaces(zlogp, zoneid) != 0) { 40210Sstevel@tonic-gate zerror(zlogp, B_FALSE, 40220Sstevel@tonic-gate "unable to unconfigure network interfaces in zone"); 40230Sstevel@tonic-gate goto error; 40240Sstevel@tonic-gate } 40250Sstevel@tonic-gate 4026766Scarlsonj if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) { 40270Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to abort TCP connections"); 40280Sstevel@tonic-gate goto error; 40290Sstevel@tonic-gate } 40300Sstevel@tonic-gate 40312621Sllai1 /* destroy zconsole before umount /dev */ 40322621Sllai1 if (!unmount_cmd) 40332621Sllai1 destroy_console_slave(); 40342621Sllai1 4035766Scarlsonj if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) { 40360Sstevel@tonic-gate zerror(zlogp, B_FALSE, 40370Sstevel@tonic-gate "unable to unmount file systems in zone"); 40380Sstevel@tonic-gate goto error; 40390Sstevel@tonic-gate } 40400Sstevel@tonic-gate 40413247Sgjelinek /* 4042*3356Sgjelinek * If we are rebooting then we normally don't want to destroy an 4043*3356Sgjelinek * existing temporary pool at this point so that we can just reuse it 4044*3356Sgjelinek * when the zone boots back up. However, it is also possible we were 4045*3356Sgjelinek * running with a temporary pool and the zone configuration has been 4046*3356Sgjelinek * modified to no longer use a temporary pool. In that case we need 4047*3356Sgjelinek * to destroy the temporary pool now. This case looks like the case 4048*3356Sgjelinek * where we never had a temporary pool configured but 4049*3356Sgjelinek * zonecfg_destroy_tmp_pool will do the right thing either way. 40503247Sgjelinek */ 4051*3356Sgjelinek if (!unmount_cmd) { 4052*3356Sgjelinek boolean_t destroy_tmp_pool = B_TRUE; 4053*3356Sgjelinek 4054*3356Sgjelinek if (rebooting) { 4055*3356Sgjelinek struct zone_psettab pset_tab; 4056*3356Sgjelinek zone_dochandle_t handle; 4057*3356Sgjelinek 4058*3356Sgjelinek if ((handle = zonecfg_init_handle()) != NULL && 4059*3356Sgjelinek zonecfg_get_handle(zone_name, handle) == Z_OK && 4060*3356Sgjelinek zonecfg_lookup_pset(handle, &pset_tab) == Z_OK) 4061*3356Sgjelinek destroy_tmp_pool = B_FALSE; 4062*3356Sgjelinek } 4063*3356Sgjelinek 4064*3356Sgjelinek if (destroy_tmp_pool) { 4065*3356Sgjelinek if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err, 4066*3356Sgjelinek sizeof (pool_err))) != Z_OK) { 4067*3356Sgjelinek if (res == Z_POOL) 4068*3356Sgjelinek zerror(zlogp, B_FALSE, pool_err); 4069*3356Sgjelinek } 40703247Sgjelinek } 40713247Sgjelinek } 40723247Sgjelinek 40731676Sjpk remove_mlps(zlogp, zoneid); 40741676Sjpk 40750Sstevel@tonic-gate if (zone_destroy(zoneid) != 0) { 40760Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to destroy zone"); 40770Sstevel@tonic-gate goto error; 40780Sstevel@tonic-gate } 40790Sstevel@tonic-gate 4080766Scarlsonj /* 4081766Scarlsonj * Special teardown for alternate boot environments: remove the tmpfs 4082766Scarlsonj * root for the zone and then remove it from the map file. 4083766Scarlsonj */ 4084766Scarlsonj if (unmount_cmd && lu_root_teardown(zlogp) != 0) 4085766Scarlsonj goto error; 4086766Scarlsonj 4087766Scarlsonj lofs_discard_mnttab(); 40880Sstevel@tonic-gate return (0); 40890Sstevel@tonic-gate 40900Sstevel@tonic-gate error: 4091766Scarlsonj lofs_discard_mnttab(); 40920Sstevel@tonic-gate return (-1); 40930Sstevel@tonic-gate } 4094