17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5ee519a1fSgjelinek * Common Development and Distribution License (the "License"). 6ee519a1fSgjelinek * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217e362f58Scomay 227c478bd9Sstevel@tonic-gate /* 237ef01d19Sgjelinek * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * zoneadm is a command interpreter for zone administration. It is all in 317c478bd9Sstevel@tonic-gate * C (i.e., no lex/yacc), and all the argument passing is argc/argv based. 327c478bd9Sstevel@tonic-gate * main() calls parse_and_run() which calls cmd_match(), then invokes the 337c478bd9Sstevel@tonic-gate * appropriate command's handler function. The rest of the program is the 347c478bd9Sstevel@tonic-gate * handler functions and their helper functions. 357c478bd9Sstevel@tonic-gate * 367c478bd9Sstevel@tonic-gate * Some of the helper functions are used largely to simplify I18N: reducing 377c478bd9Sstevel@tonic-gate * the need for translation notes. This is particularly true of many of 387c478bd9Sstevel@tonic-gate * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather 397c478bd9Sstevel@tonic-gate * than zerror(gettext("foo failed")) with a translation note indicating 407c478bd9Sstevel@tonic-gate * that "foo" need not be translated. 417c478bd9Sstevel@tonic-gate */ 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include <stdio.h> 447c478bd9Sstevel@tonic-gate #include <errno.h> 457c478bd9Sstevel@tonic-gate #include <unistd.h> 467c478bd9Sstevel@tonic-gate #include <signal.h> 477c478bd9Sstevel@tonic-gate #include <stdarg.h> 487c478bd9Sstevel@tonic-gate #include <ctype.h> 497c478bd9Sstevel@tonic-gate #include <stdlib.h> 507c478bd9Sstevel@tonic-gate #include <string.h> 517c478bd9Sstevel@tonic-gate #include <wait.h> 527c478bd9Sstevel@tonic-gate #include <zone.h> 537c478bd9Sstevel@tonic-gate #include <priv.h> 547c478bd9Sstevel@tonic-gate #include <locale.h> 557c478bd9Sstevel@tonic-gate #include <libintl.h> 567c478bd9Sstevel@tonic-gate #include <libzonecfg.h> 577c478bd9Sstevel@tonic-gate #include <bsm/adt.h> 589acbbeafSnn35248 #include <sys/brand.h> 597c478bd9Sstevel@tonic-gate #include <sys/param.h> 607c478bd9Sstevel@tonic-gate #include <sys/types.h> 617c478bd9Sstevel@tonic-gate #include <sys/stat.h> 627c478bd9Sstevel@tonic-gate #include <sys/statvfs.h> 637c478bd9Sstevel@tonic-gate #include <assert.h> 647c478bd9Sstevel@tonic-gate #include <sys/sockio.h> 657c478bd9Sstevel@tonic-gate #include <sys/mntent.h> 667c478bd9Sstevel@tonic-gate #include <limits.h> 670b5de56dSgjelinek #include <dirent.h> 68555afedfScarlsonj #include <uuid/uuid.h> 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate #include <fcntl.h> 717c478bd9Sstevel@tonic-gate #include <door.h> 727c478bd9Sstevel@tonic-gate #include <macros.h> 737c478bd9Sstevel@tonic-gate #include <libgen.h> 74865e09a4Sgjelinek #include <fnmatch.h> 75e7f3c547Sgjelinek #include <sys/modctl.h> 769acbbeafSnn35248 #include <libbrand.h> 770209230bSgjelinek #include <libscf.h> 787ef01d19Sgjelinek #include <procfs.h> 79*d9e728a2Sgjelinek #include <strings.h> 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate #include <pool.h> 827c478bd9Sstevel@tonic-gate #include <sys/pool.h> 830209230bSgjelinek #include <sys/priocntl.h> 840209230bSgjelinek #include <sys/fsspriocntl.h> 857c478bd9Sstevel@tonic-gate 860b5de56dSgjelinek #include "zoneadm.h" 870b5de56dSgjelinek 887c478bd9Sstevel@tonic-gate #define MAXARGS 8 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate /* Reflects kernel zone entries */ 917c478bd9Sstevel@tonic-gate typedef struct zone_entry { 927c478bd9Sstevel@tonic-gate zoneid_t zid; 937c478bd9Sstevel@tonic-gate char zname[ZONENAME_MAX]; 947c478bd9Sstevel@tonic-gate char *zstate_str; 957c478bd9Sstevel@tonic-gate zone_state_t zstate_num; 969acbbeafSnn35248 char zbrand[MAXNAMELEN]; 977c478bd9Sstevel@tonic-gate char zroot[MAXPATHLEN]; 98555afedfScarlsonj char zuuid[UUID_PRINTABLE_STRING_LENGTH]; 99f4b3ec61Sdh155122 zone_iptype_t ziptype; 1007c478bd9Sstevel@tonic-gate } zone_entry_t; 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate static zone_entry_t *zents; 1037c478bd9Sstevel@tonic-gate static size_t nzents; 1049acbbeafSnn35248 static boolean_t is_native_zone = B_TRUE; 1057c478bd9Sstevel@tonic-gate 1061390a385Sgjelinek #define LOOPBACK_IF "lo0" 1071390a385Sgjelinek #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af)) 1081390a385Sgjelinek 1091390a385Sgjelinek struct net_if { 1101390a385Sgjelinek char *name; 1111390a385Sgjelinek int af; 1121390a385Sgjelinek }; 1131390a385Sgjelinek 1147c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */ 1157c478bd9Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 1167c478bd9Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate struct cmd { 1197c478bd9Sstevel@tonic-gate uint_t cmd_num; /* command number */ 1207c478bd9Sstevel@tonic-gate char *cmd_name; /* command name */ 1217c478bd9Sstevel@tonic-gate char *short_usage; /* short form help */ 1227c478bd9Sstevel@tonic-gate int (*handler)(int argc, char *argv[]); /* function to call */ 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate }; 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate #define SHELP_HELP "help" 1273f2f09c1Sdp #define SHELP_BOOT "boot [-- boot_arguments]" 1287c478bd9Sstevel@tonic-gate #define SHELP_HALT "halt" 1297c478bd9Sstevel@tonic-gate #define SHELP_READY "ready" 1303f2f09c1Sdp #define SHELP_REBOOT "reboot [-- boot_arguments]" 1317c478bd9Sstevel@tonic-gate #define SHELP_LIST "list [-cipv]" 1327c478bd9Sstevel@tonic-gate #define SHELP_VERIFY "verify" 1339acbbeafSnn35248 #define SHELP_INSTALL "install [-x nodataset] [brand-specific args]" 1347c478bd9Sstevel@tonic-gate #define SHELP_UNINSTALL "uninstall [-F]" 1350b5de56dSgjelinek #define SHELP_CLONE "clone [-m method] [-s <ZFS snapshot>] zonename" 136865e09a4Sgjelinek #define SHELP_MOVE "move zonepath" 1378cd327d5Sgjelinek #define SHELP_DETACH "detach [-n]" 1388cd327d5Sgjelinek #define SHELP_ATTACH "attach [-F] [-n <path>]" 139555afedfScarlsonj #define SHELP_MARK "mark incomplete" 1407c478bd9Sstevel@tonic-gate 1419acbbeafSnn35248 #define EXEC_PREFIX "exec " 1429acbbeafSnn35248 #define EXEC_LEN (strlen(EXEC_PREFIX)) 1439acbbeafSnn35248 #define RMCOMMAND "/usr/bin/rm -rf" 1449acbbeafSnn35248 1459acbbeafSnn35248 static int cleanup_zonepath(char *, boolean_t); 1469acbbeafSnn35248 147f4b3ec61Sdh155122 extern int ifname_open(char *); 148f4b3ec61Sdh155122 1497c478bd9Sstevel@tonic-gate static int help_func(int argc, char *argv[]); 1507c478bd9Sstevel@tonic-gate static int ready_func(int argc, char *argv[]); 1517c478bd9Sstevel@tonic-gate static int boot_func(int argc, char *argv[]); 1527c478bd9Sstevel@tonic-gate static int halt_func(int argc, char *argv[]); 1537c478bd9Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]); 1547c478bd9Sstevel@tonic-gate static int list_func(int argc, char *argv[]); 1557c478bd9Sstevel@tonic-gate static int verify_func(int argc, char *argv[]); 1567c478bd9Sstevel@tonic-gate static int install_func(int argc, char *argv[]); 1577c478bd9Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]); 158108322fbScarlsonj static int mount_func(int argc, char *argv[]); 159108322fbScarlsonj static int unmount_func(int argc, char *argv[]); 160865e09a4Sgjelinek static int clone_func(int argc, char *argv[]); 161865e09a4Sgjelinek static int move_func(int argc, char *argv[]); 162ee519a1fSgjelinek static int detach_func(int argc, char *argv[]); 163ee519a1fSgjelinek static int attach_func(int argc, char *argv[]); 164555afedfScarlsonj static int mark_func(int argc, char *argv[]); 1650209230bSgjelinek static int apply_func(int argc, char *argv[]); 1667c478bd9Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running, 1679acbbeafSnn35248 boolean_t unsafe_when_running, boolean_t force); 1687c478bd9Sstevel@tonic-gate static int cmd_match(char *cmd); 169ce28b40eSzt129084 static int verify_details(int, char *argv[]); 170ce28b40eSzt129084 static int verify_brand(zone_dochandle_t, int, char *argv[]); 171ce28b40eSzt129084 static int invoke_brand_handler(int, char *argv[]); 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate static struct cmd cmdtab[] = { 1747c478bd9Sstevel@tonic-gate { CMD_HELP, "help", SHELP_HELP, help_func }, 1757c478bd9Sstevel@tonic-gate { CMD_BOOT, "boot", SHELP_BOOT, boot_func }, 1767c478bd9Sstevel@tonic-gate { CMD_HALT, "halt", SHELP_HALT, halt_func }, 1777c478bd9Sstevel@tonic-gate { CMD_READY, "ready", SHELP_READY, ready_func }, 1787c478bd9Sstevel@tonic-gate { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func }, 1797c478bd9Sstevel@tonic-gate { CMD_LIST, "list", SHELP_LIST, list_func }, 1807c478bd9Sstevel@tonic-gate { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func }, 1817c478bd9Sstevel@tonic-gate { CMD_INSTALL, "install", SHELP_INSTALL, install_func }, 1827c478bd9Sstevel@tonic-gate { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL, 183108322fbScarlsonj uninstall_func }, 184865e09a4Sgjelinek /* mount and unmount are private commands for admin/install */ 185108322fbScarlsonj { CMD_MOUNT, "mount", NULL, mount_func }, 186865e09a4Sgjelinek { CMD_UNMOUNT, "unmount", NULL, unmount_func }, 187865e09a4Sgjelinek { CMD_CLONE, "clone", SHELP_CLONE, clone_func }, 188ee519a1fSgjelinek { CMD_MOVE, "move", SHELP_MOVE, move_func }, 189ee519a1fSgjelinek { CMD_DETACH, "detach", SHELP_DETACH, detach_func }, 190555afedfScarlsonj { CMD_ATTACH, "attach", SHELP_ATTACH, attach_func }, 1910209230bSgjelinek { CMD_MARK, "mark", SHELP_MARK, mark_func }, 1920209230bSgjelinek { CMD_APPLY, "apply", NULL, apply_func } 1937c478bd9Sstevel@tonic-gate }; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate /* global variables */ 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */ 1987c478bd9Sstevel@tonic-gate static char *execname; 1999acbbeafSnn35248 static char target_brand[MAXNAMELEN]; 2007c478bd9Sstevel@tonic-gate static char *locale; 2010b5de56dSgjelinek char *target_zone; 202555afedfScarlsonj static char *target_uuid; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate /* used in do_subproc() and signal handler */ 2057c478bd9Sstevel@tonic-gate static volatile boolean_t child_killed; 2069acbbeafSnn35248 static int do_subproc_cnt = 0; 2079acbbeafSnn35248 2089acbbeafSnn35248 /* 2099acbbeafSnn35248 * Used to indicate whether this zoneadm instance has another zoneadm 2109acbbeafSnn35248 * instance in its ancestry. 2119acbbeafSnn35248 */ 2129acbbeafSnn35248 static boolean_t zoneadm_is_nested = B_FALSE; 2139acbbeafSnn35248 2149acbbeafSnn35248 /* used to track nested zone-lock operations */ 2159acbbeafSnn35248 static int zone_lock_cnt = 0; 2169acbbeafSnn35248 2179acbbeafSnn35248 /* used to communicate lock status to children */ 2189acbbeafSnn35248 #define LOCK_ENV_VAR "_ZONEADM_LOCK_HELD" 2199acbbeafSnn35248 static char zoneadm_lock_held[] = LOCK_ENV_VAR"=1"; 2209acbbeafSnn35248 static char zoneadm_lock_not_held[] = LOCK_ENV_VAR"=0"; 2217c478bd9Sstevel@tonic-gate 2220b5de56dSgjelinek char * 2237c478bd9Sstevel@tonic-gate cmd_to_str(int cmd_num) 2247c478bd9Sstevel@tonic-gate { 2257c478bd9Sstevel@tonic-gate assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2267c478bd9Sstevel@tonic-gate return (cmdtab[cmd_num].cmd_name); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */ 2307c478bd9Sstevel@tonic-gate static char * 2317c478bd9Sstevel@tonic-gate long_help(int cmd_num) 2327c478bd9Sstevel@tonic-gate { 2337e362f58Scomay assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2347c478bd9Sstevel@tonic-gate switch (cmd_num) { 2357c478bd9Sstevel@tonic-gate case CMD_HELP: 2367c478bd9Sstevel@tonic-gate return (gettext("Print usage message.")); 2377c478bd9Sstevel@tonic-gate case CMD_BOOT: 2383f2f09c1Sdp return (gettext("Activates (boots) specified zone. See " 2393f2f09c1Sdp "zoneadm(1m) for valid boot\n\targuments.")); 2407c478bd9Sstevel@tonic-gate case CMD_HALT: 2419e518655Sgjelinek return (gettext("Halts specified zone, bypassing shutdown " 2429e518655Sgjelinek "scripts and removing runtime\n\tresources of the zone.")); 2437c478bd9Sstevel@tonic-gate case CMD_READY: 2449e518655Sgjelinek return (gettext("Prepares a zone for running applications but " 2459e518655Sgjelinek "does not start any user\n\tprocesses in the zone.")); 2467c478bd9Sstevel@tonic-gate case CMD_REBOOT: 2479e518655Sgjelinek return (gettext("Restarts the zone (equivalent to a halt / " 2483f2f09c1Sdp "boot sequence).\n\tFails if the zone is not active. " 2493f2f09c1Sdp "See zoneadm(1m) for valid boot\n\targuments.")); 2507c478bd9Sstevel@tonic-gate case CMD_LIST: 2517c478bd9Sstevel@tonic-gate return (gettext("Lists the current zones, or a " 2527c478bd9Sstevel@tonic-gate "specific zone if indicated. By default,\n\tall " 2537c478bd9Sstevel@tonic-gate "running zones are listed, though this can be " 2547c478bd9Sstevel@tonic-gate "expanded to all\n\tinstalled zones with the -i " 2557c478bd9Sstevel@tonic-gate "option or all configured zones with the\n\t-c " 256555afedfScarlsonj "option. When used with the general -z <zone> and/or -u " 257555afedfScarlsonj "<uuid-match>\n\toptions, lists only the specified " 258555afedfScarlsonj "matching zone, but lists it\n\tregardless of its state, " 259555afedfScarlsonj "and the -i and -c options are disallowed. The\n\t-v " 260555afedfScarlsonj "option can be used to display verbose information: zone " 261555afedfScarlsonj "name, id,\n\tcurrent state, root directory and options. " 262555afedfScarlsonj "The -p option can be used\n\tto request machine-parsable " 263555afedfScarlsonj "output. The -v and -p options are mutually\n\texclusive." 264555afedfScarlsonj " If neither -v nor -p is used, just the zone name is " 265555afedfScarlsonj "listed.")); 2667c478bd9Sstevel@tonic-gate case CMD_VERIFY: 2677c478bd9Sstevel@tonic-gate return (gettext("Check to make sure the configuration " 2687c478bd9Sstevel@tonic-gate "can safely be instantiated\n\ton the machine: " 2697c478bd9Sstevel@tonic-gate "physical network interfaces exist, etc.")); 2707c478bd9Sstevel@tonic-gate case CMD_INSTALL: 2710b5de56dSgjelinek return (gettext("Install the configuration on to the system. " 2720b5de56dSgjelinek "The -x nodataset option\n\tcan be used to prevent the " 2730b5de56dSgjelinek "creation of a new ZFS file system for the\n\tzone " 2749acbbeafSnn35248 "(assuming the zonepath is within a ZFS file system).\n\t" 2759acbbeafSnn35248 "All other arguments are passed to the brand installation " 2769acbbeafSnn35248 "function;\n\tsee brand(4) for more information.")); 2777c478bd9Sstevel@tonic-gate case CMD_UNINSTALL: 2789e518655Sgjelinek return (gettext("Uninstall the configuration from the system. " 2799e518655Sgjelinek "The -F flag can be used\n\tto force the action.")); 280865e09a4Sgjelinek case CMD_CLONE: 2810b5de56dSgjelinek return (gettext("Clone the installation of another zone. " 2820b5de56dSgjelinek "The -m option can be used to\n\tspecify 'copy' which " 2830b5de56dSgjelinek "forces a copy of the source zone. The -s option\n\t" 2840b5de56dSgjelinek "can be used to specify the name of a ZFS snapshot " 2850b5de56dSgjelinek "that was taken from\n\ta previous clone command. The " 2860b5de56dSgjelinek "snapshot will be used as the source\n\tinstead of " 2870b5de56dSgjelinek "creating a new ZFS snapshot.")); 288865e09a4Sgjelinek case CMD_MOVE: 289865e09a4Sgjelinek return (gettext("Move the zone to a new zonepath.")); 2909e518655Sgjelinek case CMD_DETACH: 2919e518655Sgjelinek return (gettext("Detach the zone from the system. The zone " 2929e518655Sgjelinek "state is changed to\n\t'configured' (but the files under " 2939e518655Sgjelinek "the zonepath are untouched).\n\tThe zone can subsequently " 2949e518655Sgjelinek "be attached, or can be moved to another\n\tsystem and " 2958cd327d5Sgjelinek "attached there. The -n option can be used to specify\n\t" 2968cd327d5Sgjelinek "'no-execute' mode. When -n is used, the information " 2978cd327d5Sgjelinek "needed to attach\n\tthe zone is sent to standard output " 2988cd327d5Sgjelinek "but the zone is not actually\n\tdetached.")); 2999e518655Sgjelinek case CMD_ATTACH: 3009e518655Sgjelinek return (gettext("Attach the zone to the system. The zone " 3019e518655Sgjelinek "state must be 'configured'\n\tprior to attach; upon " 3029e518655Sgjelinek "successful completion, the zone state will be\n\t" 3039e518655Sgjelinek "'installed'. The system software on the current " 3049e518655Sgjelinek "system must be\n\tcompatible with the software on the " 3059e518655Sgjelinek "zone's original system.\n\tSpecify -F to force the attach " 3068cd327d5Sgjelinek "and skip software compatibility tests.\n\tThe -n option " 3078cd327d5Sgjelinek "can be used to specify 'no-execute' mode. When -n is\n\t" 3088cd327d5Sgjelinek "used, the information needed to attach the zone is read " 3098cd327d5Sgjelinek "from the\n\tspecified path and the configuration is only " 3108cd327d5Sgjelinek "validated. The path can\n\tbe '-' to specify standard " 3118cd327d5Sgjelinek "input.")); 312555afedfScarlsonj case CMD_MARK: 313555afedfScarlsonj return (gettext("Set the state of the zone. This can be used " 314555afedfScarlsonj "to force the zone\n\tstate to 'incomplete' " 315555afedfScarlsonj "administratively if some activity has rendered\n\tthe " 316555afedfScarlsonj "zone permanently unusable. The only valid state that " 317555afedfScarlsonj "may be\n\tspecified is 'incomplete'.")); 318108322fbScarlsonj default: 319108322fbScarlsonj return (""); 3207c478bd9Sstevel@tonic-gate } 3217c478bd9Sstevel@tonic-gate /* NOTREACHED */ 3227e362f58Scomay return (NULL); 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate /* 3267c478bd9Sstevel@tonic-gate * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for 3277c478bd9Sstevel@tonic-gate * unexpected errors. 3287c478bd9Sstevel@tonic-gate */ 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate static int 3317c478bd9Sstevel@tonic-gate usage(boolean_t explicit) 3327c478bd9Sstevel@tonic-gate { 3337c478bd9Sstevel@tonic-gate int i; 3347c478bd9Sstevel@tonic-gate FILE *fd = explicit ? stdout : stderr; 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname); 337555afedfScarlsonj (void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n", 338555afedfScarlsonj execname); 339555afedfScarlsonj (void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname, 3407c478bd9Sstevel@tonic-gate gettext("subcommand")); 3417c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands")); 3427c478bd9Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 343108322fbScarlsonj if (cmdtab[i].short_usage == NULL) 344108322fbScarlsonj continue; 3457c478bd9Sstevel@tonic-gate (void) fprintf(fd, "%s\n", cmdtab[i].short_usage); 3467c478bd9Sstevel@tonic-gate if (explicit) 3477c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\t%s\n\n", long_help(i)); 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate if (!explicit) 3507c478bd9Sstevel@tonic-gate (void) fputs("\n", fd); 3517c478bd9Sstevel@tonic-gate return (Z_USAGE); 3527c478bd9Sstevel@tonic-gate } 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate static void 3557c478bd9Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num) 3567c478bd9Sstevel@tonic-gate { 3577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage); 3587c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s\n", long_help(cmd_num)); 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate /* 3627c478bd9Sstevel@tonic-gate * zperror() is like perror(3c) except that this also prints the executable 3637c478bd9Sstevel@tonic-gate * name at the start of the message, and takes a boolean indicating whether 3647c478bd9Sstevel@tonic-gate * to call libc'c strerror() or that from libzonecfg. 3657c478bd9Sstevel@tonic-gate */ 3667c478bd9Sstevel@tonic-gate 3670b5de56dSgjelinek void 3687c478bd9Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error) 3697c478bd9Sstevel@tonic-gate { 3707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", execname, str, 3717c478bd9Sstevel@tonic-gate zonecfg_error ? zonecfg_strerror(errno) : strerror(errno)); 3727c478bd9Sstevel@tonic-gate } 3737c478bd9Sstevel@tonic-gate 3747c478bd9Sstevel@tonic-gate /* 3757c478bd9Sstevel@tonic-gate * zperror2() is very similar to zperror() above, except it also prints a 3767c478bd9Sstevel@tonic-gate * supplied zone name after the executable. 3777c478bd9Sstevel@tonic-gate * 3787c478bd9Sstevel@tonic-gate * All current consumers of this function want libzonecfg's strerror() rather 3797c478bd9Sstevel@tonic-gate * than libc's; if this ever changes, this function can be made more generic 3807c478bd9Sstevel@tonic-gate * like zperror() above. 3817c478bd9Sstevel@tonic-gate */ 3827c478bd9Sstevel@tonic-gate 3830b5de56dSgjelinek void 3847c478bd9Sstevel@tonic-gate zperror2(const char *zone, const char *str) 3857c478bd9Sstevel@tonic-gate { 3867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str, 3877c478bd9Sstevel@tonic-gate zonecfg_strerror(errno)); 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */ 3910b5de56dSgjelinek void 3927c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...) 3937c478bd9Sstevel@tonic-gate { 3947c478bd9Sstevel@tonic-gate va_list alist; 3957c478bd9Sstevel@tonic-gate 3967c478bd9Sstevel@tonic-gate va_start(alist, fmt); 3977c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", execname); 3987c478bd9Sstevel@tonic-gate if (target_zone != NULL) 3997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "zone '%s': ", target_zone); 4007c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, alist); 4017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 4027c478bd9Sstevel@tonic-gate va_end(alist); 4037c478bd9Sstevel@tonic-gate } 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate static void * 4067c478bd9Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize) 4077c478bd9Sstevel@tonic-gate { 4087c478bd9Sstevel@tonic-gate void *r = calloc(nelem, elsize); 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate if (r == NULL) { 4117c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), 4127c478bd9Sstevel@tonic-gate (ulong_t)nelem * elsize, strerror(errno)); 4137c478bd9Sstevel@tonic-gate exit(Z_ERR); 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate return (r); 4167c478bd9Sstevel@tonic-gate } 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate static void 4197c478bd9Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable) 4207c478bd9Sstevel@tonic-gate { 4217c478bd9Sstevel@tonic-gate static boolean_t firsttime = B_TRUE; 422f4b3ec61Sdh155122 char *ip_type_str; 423f4b3ec61Sdh155122 424f4b3ec61Sdh155122 if (zent->ziptype == ZS_EXCLUSIVE) 425f4b3ec61Sdh155122 ip_type_str = "excl"; 426f4b3ec61Sdh155122 else 427f4b3ec61Sdh155122 ip_type_str = "shared"; 4287c478bd9Sstevel@tonic-gate 4297c478bd9Sstevel@tonic-gate assert(!(verbose && parsable)); 4307c478bd9Sstevel@tonic-gate if (firsttime && verbose) { 4317c478bd9Sstevel@tonic-gate firsttime = B_FALSE; 432f4b3ec61Sdh155122 (void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n", 433f4b3ec61Sdh155122 ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND", 434f4b3ec61Sdh155122 "IP"); 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate if (!verbose) { 437555afedfScarlsonj char *cp, *clim; 438555afedfScarlsonj 4397c478bd9Sstevel@tonic-gate if (!parsable) { 4407c478bd9Sstevel@tonic-gate (void) printf("%s\n", zent->zname); 4417c478bd9Sstevel@tonic-gate return; 4427c478bd9Sstevel@tonic-gate } 4437c478bd9Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4447c478bd9Sstevel@tonic-gate (void) printf("-"); 4457c478bd9Sstevel@tonic-gate else 4467c478bd9Sstevel@tonic-gate (void) printf("%lu", zent->zid); 447555afedfScarlsonj (void) printf(":%s:%s:", zent->zname, zent->zstate_str); 448555afedfScarlsonj cp = zent->zroot; 449555afedfScarlsonj while ((clim = strchr(cp, ':')) != NULL) { 450555afedfScarlsonj (void) printf("%.*s\\:", clim - cp, cp); 451555afedfScarlsonj cp = clim + 1; 452555afedfScarlsonj } 453f4b3ec61Sdh155122 (void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand, 454f4b3ec61Sdh155122 ip_type_str); 4557c478bd9Sstevel@tonic-gate return; 4567c478bd9Sstevel@tonic-gate } 4577c478bd9Sstevel@tonic-gate if (zent->zstate_str != NULL) { 4587c478bd9Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4597c478bd9Sstevel@tonic-gate (void) printf("%*s", ZONEID_WIDTH, "-"); 4607c478bd9Sstevel@tonic-gate else 4617c478bd9Sstevel@tonic-gate (void) printf("%*lu", ZONEID_WIDTH, zent->zid); 462f4b3ec61Sdh155122 (void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname, 463f4b3ec61Sdh155122 zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str); 4647c478bd9Sstevel@tonic-gate } 4657c478bd9Sstevel@tonic-gate } 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate static int 468108322fbScarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent) 4697c478bd9Sstevel@tonic-gate { 47045916cd2Sjpk char root[MAXPATHLEN], *cp; 4717c478bd9Sstevel@tonic-gate int err; 472555afedfScarlsonj uuid_t uuid; 4737c478bd9Sstevel@tonic-gate 4747c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname)); 4757c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot)); 4769acbbeafSnn35248 (void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand)); 4777c478bd9Sstevel@tonic-gate zent->zstate_str = "???"; 4787c478bd9Sstevel@tonic-gate 479108322fbScarlsonj zent->zid = zid; 4807c478bd9Sstevel@tonic-gate 481555afedfScarlsonj if (zonecfg_get_uuid(zone_name, uuid) == Z_OK && 482555afedfScarlsonj !uuid_is_null(uuid)) 483555afedfScarlsonj uuid_unparse(uuid, zent->zuuid); 484555afedfScarlsonj else 485555afedfScarlsonj zent->zuuid[0] = '\0'; 486555afedfScarlsonj 48745916cd2Sjpk /* 48845916cd2Sjpk * For labeled zones which query the zone path of lower-level 48945916cd2Sjpk * zones, the path needs to be adjusted to drop the final 49045916cd2Sjpk * "/root" component. This adjusted path is then useful 49145916cd2Sjpk * for reading down any exported directories from the 49245916cd2Sjpk * lower-level zone. 49345916cd2Sjpk */ 49445916cd2Sjpk if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) { 49545916cd2Sjpk if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot, 49645916cd2Sjpk sizeof (zent->zroot)) == -1) { 49745916cd2Sjpk zperror2(zent->zname, 49845916cd2Sjpk gettext("could not get zone path.")); 49945916cd2Sjpk return (Z_ERR); 50045916cd2Sjpk } 50145916cd2Sjpk cp = zent->zroot + strlen(zent->zroot) - 5; 50245916cd2Sjpk if (cp > zent->zroot && strcmp(cp, "/root") == 0) 50345916cd2Sjpk *cp = 0; 50445916cd2Sjpk } else { 50545916cd2Sjpk if ((err = zone_get_zonepath(zent->zname, root, 50645916cd2Sjpk sizeof (root))) != Z_OK) { 5077c478bd9Sstevel@tonic-gate errno = err; 50845916cd2Sjpk zperror2(zent->zname, 50945916cd2Sjpk gettext("could not get zone path.")); 5107c478bd9Sstevel@tonic-gate return (Z_ERR); 5117c478bd9Sstevel@tonic-gate } 5127c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zroot, root, sizeof (zent->zroot)); 51345916cd2Sjpk } 5147c478bd9Sstevel@tonic-gate 5157c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) { 5167c478bd9Sstevel@tonic-gate errno = err; 5177c478bd9Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get state")); 5187c478bd9Sstevel@tonic-gate return (Z_ERR); 5197c478bd9Sstevel@tonic-gate } 5207c478bd9Sstevel@tonic-gate zent->zstate_str = zone_state_str(zent->zstate_num); 521bafa7067Snn35248 522bafa7067Snn35248 /* 523bafa7067Snn35248 * A zone's brand is only available in the .xml file describing it, 524bafa7067Snn35248 * which is only visible to the global zone. This causes 525bafa7067Snn35248 * zone_get_brand() to fail when called from within a non-global 526bafa7067Snn35248 * zone. Fortunately we only do this on labeled systems, where we 527bafa7067Snn35248 * know all zones are native. 528bafa7067Snn35248 */ 529bafa7067Snn35248 if (getzoneid() != GLOBAL_ZONEID) { 530bafa7067Snn35248 assert(is_system_labeled() != 0); 531bafa7067Snn35248 (void) strlcpy(zent->zbrand, NATIVE_BRAND_NAME, 532bafa7067Snn35248 sizeof (zent->zbrand)); 533bafa7067Snn35248 } else if (zone_get_brand(zent->zname, zent->zbrand, 5349acbbeafSnn35248 sizeof (zent->zbrand)) != Z_OK) { 5359acbbeafSnn35248 zperror2(zent->zname, gettext("could not get brand name")); 5369acbbeafSnn35248 return (Z_ERR); 5379acbbeafSnn35248 } 5387c478bd9Sstevel@tonic-gate 539f4b3ec61Sdh155122 /* 540f4b3ec61Sdh155122 * Get ip type of the zone. 541f4b3ec61Sdh155122 * Note for global zone, ZS_SHARED is set always. 542f4b3ec61Sdh155122 */ 543f4b3ec61Sdh155122 if (zid == GLOBAL_ZONEID) { 544f4b3ec61Sdh155122 zent->ziptype = ZS_SHARED; 545f4b3ec61Sdh155122 } else { 546f4b3ec61Sdh155122 547f4b3ec61Sdh155122 if (zent->zstate_num == ZONE_STATE_RUNNING) { 548f4b3ec61Sdh155122 ushort_t flags; 549f4b3ec61Sdh155122 550f4b3ec61Sdh155122 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, 551f4b3ec61Sdh155122 sizeof (flags)) < 0) { 552f4b3ec61Sdh155122 zperror2(zent->zname, 553f4b3ec61Sdh155122 gettext("could not get zone flags")); 554f4b3ec61Sdh155122 return (Z_ERR); 555f4b3ec61Sdh155122 } 556f4b3ec61Sdh155122 if (flags & ZF_NET_EXCL) 557f4b3ec61Sdh155122 zent->ziptype = ZS_EXCLUSIVE; 558f4b3ec61Sdh155122 else 559f4b3ec61Sdh155122 zent->ziptype = ZS_SHARED; 560f4b3ec61Sdh155122 } else { 561f4b3ec61Sdh155122 zone_dochandle_t handle; 562f4b3ec61Sdh155122 563f4b3ec61Sdh155122 if ((handle = zonecfg_init_handle()) == NULL) { 564f4b3ec61Sdh155122 zperror2(zent->zname, 565f4b3ec61Sdh155122 gettext("could not init handle")); 566f4b3ec61Sdh155122 return (Z_ERR); 567f4b3ec61Sdh155122 } 568f4b3ec61Sdh155122 if ((err = zonecfg_get_handle(zent->zname, handle)) 569f4b3ec61Sdh155122 != Z_OK) { 570f4b3ec61Sdh155122 zperror2(zent->zname, 571f4b3ec61Sdh155122 gettext("could not get handle")); 572f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 573f4b3ec61Sdh155122 return (Z_ERR); 574f4b3ec61Sdh155122 } 575f4b3ec61Sdh155122 576f4b3ec61Sdh155122 if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) 577f4b3ec61Sdh155122 != Z_OK) { 578f4b3ec61Sdh155122 zperror2(zent->zname, 579f4b3ec61Sdh155122 gettext("could not get ip-type")); 580f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 581f4b3ec61Sdh155122 return (Z_ERR); 582f4b3ec61Sdh155122 } 583f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 584f4b3ec61Sdh155122 } 585f4b3ec61Sdh155122 } 586f4b3ec61Sdh155122 5877c478bd9Sstevel@tonic-gate return (Z_OK); 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate /* 5917c478bd9Sstevel@tonic-gate * fetch_zents() calls zone_list(2) to find out how many zones are running 5927c478bd9Sstevel@tonic-gate * (which is stored in the global nzents), then calls zone_list(2) again 5937c478bd9Sstevel@tonic-gate * to fetch the list of running zones (stored in the global zents). This 5947c478bd9Sstevel@tonic-gate * function may be called multiple times, so if zents is already set, we 5957c478bd9Sstevel@tonic-gate * return immediately to save work. 5967c478bd9Sstevel@tonic-gate */ 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate static int 599108322fbScarlsonj fetch_zents(void) 6007c478bd9Sstevel@tonic-gate { 6017c478bd9Sstevel@tonic-gate zoneid_t *zids = NULL; 6027c478bd9Sstevel@tonic-gate uint_t nzents_saved; 603108322fbScarlsonj int i, retv; 604108322fbScarlsonj FILE *fp; 605108322fbScarlsonj boolean_t inaltroot; 606108322fbScarlsonj zone_entry_t *zentp; 6077c478bd9Sstevel@tonic-gate 6087c478bd9Sstevel@tonic-gate if (nzents > 0) 6097c478bd9Sstevel@tonic-gate return (Z_OK); 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate if (zone_list(NULL, &nzents) != 0) { 6127c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zoneid list"), B_FALSE); 6137c478bd9Sstevel@tonic-gate return (Z_ERR); 6147c478bd9Sstevel@tonic-gate } 6157c478bd9Sstevel@tonic-gate 6167c478bd9Sstevel@tonic-gate again: 6177c478bd9Sstevel@tonic-gate if (nzents == 0) 6187c478bd9Sstevel@tonic-gate return (Z_OK); 6197c478bd9Sstevel@tonic-gate 6207c478bd9Sstevel@tonic-gate zids = safe_calloc(nzents, sizeof (zoneid_t)); 6217c478bd9Sstevel@tonic-gate nzents_saved = nzents; 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate if (zone_list(zids, &nzents) != 0) { 6247c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zone list"), B_FALSE); 6257c478bd9Sstevel@tonic-gate free(zids); 6267c478bd9Sstevel@tonic-gate return (Z_ERR); 6277c478bd9Sstevel@tonic-gate } 6287c478bd9Sstevel@tonic-gate if (nzents != nzents_saved) { 6297c478bd9Sstevel@tonic-gate /* list changed, try again */ 6307c478bd9Sstevel@tonic-gate free(zids); 6317c478bd9Sstevel@tonic-gate goto again; 6327c478bd9Sstevel@tonic-gate } 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate zents = safe_calloc(nzents, sizeof (zone_entry_t)); 6357c478bd9Sstevel@tonic-gate 636108322fbScarlsonj inaltroot = zonecfg_in_alt_root(); 637108322fbScarlsonj if (inaltroot) 638108322fbScarlsonj fp = zonecfg_open_scratch("", B_FALSE); 639108322fbScarlsonj else 640108322fbScarlsonj fp = NULL; 641108322fbScarlsonj zentp = zents; 642108322fbScarlsonj retv = Z_OK; 6437c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 6447c478bd9Sstevel@tonic-gate char name[ZONENAME_MAX]; 645108322fbScarlsonj char altname[ZONENAME_MAX]; 6467c478bd9Sstevel@tonic-gate 647108322fbScarlsonj if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) { 6487c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zone name"), B_FALSE); 649108322fbScarlsonj retv = Z_ERR; 650108322fbScarlsonj continue; 6517c478bd9Sstevel@tonic-gate } 652108322fbScarlsonj if (zonecfg_is_scratch(name)) { 653108322fbScarlsonj /* Ignore scratch zones by default */ 654108322fbScarlsonj if (!inaltroot) 655108322fbScarlsonj continue; 656108322fbScarlsonj if (fp == NULL || 657108322fbScarlsonj zonecfg_reverse_scratch(fp, name, altname, 658108322fbScarlsonj sizeof (altname), NULL, 0) == -1) { 6595ee84fbdSgjelinek zerror(gettext("could not resolve scratch " 660108322fbScarlsonj "zone %s"), name); 661108322fbScarlsonj retv = Z_ERR; 662108322fbScarlsonj continue; 663108322fbScarlsonj } 664108322fbScarlsonj (void) strcpy(name, altname); 665108322fbScarlsonj } else { 666108322fbScarlsonj /* Ignore non-scratch when in an alternate root */ 667108322fbScarlsonj if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0) 668108322fbScarlsonj continue; 669108322fbScarlsonj } 670108322fbScarlsonj if (lookup_zone_info(name, zids[i], zentp) != Z_OK) { 671108322fbScarlsonj zerror(gettext("failed to get zone data")); 672108322fbScarlsonj retv = Z_ERR; 673108322fbScarlsonj continue; 674108322fbScarlsonj } 675108322fbScarlsonj zentp++; 676108322fbScarlsonj } 677108322fbScarlsonj nzents = zentp - zents; 678108322fbScarlsonj if (fp != NULL) 679108322fbScarlsonj zonecfg_close_scratch(fp); 6807c478bd9Sstevel@tonic-gate 6817c478bd9Sstevel@tonic-gate free(zids); 682108322fbScarlsonj return (retv); 6837c478bd9Sstevel@tonic-gate } 6847c478bd9Sstevel@tonic-gate 685108322fbScarlsonj static int 6867c478bd9Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable) 6877c478bd9Sstevel@tonic-gate { 6887c478bd9Sstevel@tonic-gate int i; 6897c478bd9Sstevel@tonic-gate zone_entry_t zent; 6907c478bd9Sstevel@tonic-gate FILE *cookie; 6917c478bd9Sstevel@tonic-gate char *name; 6927c478bd9Sstevel@tonic-gate 6937c478bd9Sstevel@tonic-gate /* 6947c478bd9Sstevel@tonic-gate * First get the list of running zones from the kernel and print them. 6957c478bd9Sstevel@tonic-gate * If that is all we need, then return. 6967c478bd9Sstevel@tonic-gate */ 697108322fbScarlsonj if ((i = fetch_zents()) != Z_OK) { 6987c478bd9Sstevel@tonic-gate /* 6997c478bd9Sstevel@tonic-gate * No need for error messages; fetch_zents() has already taken 7007c478bd9Sstevel@tonic-gate * care of this. 7017c478bd9Sstevel@tonic-gate */ 702108322fbScarlsonj return (i); 7037c478bd9Sstevel@tonic-gate } 704108322fbScarlsonj for (i = 0; i < nzents; i++) 7057c478bd9Sstevel@tonic-gate zone_print(&zents[i], verbose, parsable); 7067c478bd9Sstevel@tonic-gate if (min_state >= ZONE_STATE_RUNNING) 707108322fbScarlsonj return (Z_OK); 7087c478bd9Sstevel@tonic-gate /* 7097c478bd9Sstevel@tonic-gate * Next, get the full list of zones from the configuration, skipping 7107c478bd9Sstevel@tonic-gate * any we have already printed. 7117c478bd9Sstevel@tonic-gate */ 7127c478bd9Sstevel@tonic-gate cookie = setzoneent(); 7137c478bd9Sstevel@tonic-gate while ((name = getzoneent(cookie)) != NULL) { 7147c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7157c478bd9Sstevel@tonic-gate if (strcmp(zents[i].zname, name) == 0) 7167c478bd9Sstevel@tonic-gate break; 7177c478bd9Sstevel@tonic-gate } 7187c478bd9Sstevel@tonic-gate if (i < nzents) { 7197c478bd9Sstevel@tonic-gate free(name); 7207c478bd9Sstevel@tonic-gate continue; 7217c478bd9Sstevel@tonic-gate } 722108322fbScarlsonj if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) { 7237c478bd9Sstevel@tonic-gate free(name); 7247c478bd9Sstevel@tonic-gate continue; 7257c478bd9Sstevel@tonic-gate } 7267c478bd9Sstevel@tonic-gate free(name); 7277c478bd9Sstevel@tonic-gate if (zent.zstate_num >= min_state) 7287c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 7297c478bd9Sstevel@tonic-gate } 7307c478bd9Sstevel@tonic-gate endzoneent(cookie); 731108322fbScarlsonj return (Z_OK); 7327c478bd9Sstevel@tonic-gate } 7337c478bd9Sstevel@tonic-gate 7347c478bd9Sstevel@tonic-gate static zone_entry_t * 7357c478bd9Sstevel@tonic-gate lookup_running_zone(char *str) 7367c478bd9Sstevel@tonic-gate { 7377c478bd9Sstevel@tonic-gate zoneid_t zoneid; 7387c478bd9Sstevel@tonic-gate char *cp; 7397c478bd9Sstevel@tonic-gate int i; 7407c478bd9Sstevel@tonic-gate 7417c478bd9Sstevel@tonic-gate if (fetch_zents() != Z_OK) 7427c478bd9Sstevel@tonic-gate return (NULL); 7437c478bd9Sstevel@tonic-gate 7447c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7457c478bd9Sstevel@tonic-gate if (strcmp(str, zents[i].zname) == 0) 7467c478bd9Sstevel@tonic-gate return (&zents[i]); 7477c478bd9Sstevel@tonic-gate } 7487c478bd9Sstevel@tonic-gate errno = 0; 7497c478bd9Sstevel@tonic-gate zoneid = strtol(str, &cp, 0); 7507c478bd9Sstevel@tonic-gate if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID || 7517c478bd9Sstevel@tonic-gate errno != 0 || *cp != '\0') 7527c478bd9Sstevel@tonic-gate return (NULL); 7537c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7547c478bd9Sstevel@tonic-gate if (zoneid == zents[i].zid) 7557c478bd9Sstevel@tonic-gate return (&zents[i]); 7567c478bd9Sstevel@tonic-gate } 7577c478bd9Sstevel@tonic-gate return (NULL); 7587c478bd9Sstevel@tonic-gate } 7597c478bd9Sstevel@tonic-gate 7607c478bd9Sstevel@tonic-gate /* 7617c478bd9Sstevel@tonic-gate * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if 7627c478bd9Sstevel@tonic-gate * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect). 7637c478bd9Sstevel@tonic-gate */ 7647c478bd9Sstevel@tonic-gate static boolean_t 7657c478bd9Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file) 7667c478bd9Sstevel@tonic-gate { 7677c478bd9Sstevel@tonic-gate char *str; 7687c478bd9Sstevel@tonic-gate 7697c478bd9Sstevel@tonic-gate assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR || 7707c478bd9Sstevel@tonic-gate bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP || 7717c478bd9Sstevel@tonic-gate bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH); 7727c478bd9Sstevel@tonic-gate /* 7737c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 7747c478bd9Sstevel@tonic-gate * The strings below will be used as part of a larger message, 7757c478bd9Sstevel@tonic-gate * either: 7767c478bd9Sstevel@tonic-gate * (file name) must be (owner|group|world) (read|writ|execut)able 7777c478bd9Sstevel@tonic-gate * or 7787c478bd9Sstevel@tonic-gate * (file name) must not be (owner|group|world) (read|writ|execut)able 7797c478bd9Sstevel@tonic-gate */ 7807c478bd9Sstevel@tonic-gate switch (bit) { 7817c478bd9Sstevel@tonic-gate case S_IRUSR: 7827c478bd9Sstevel@tonic-gate str = gettext("owner readable"); 7837c478bd9Sstevel@tonic-gate break; 7847c478bd9Sstevel@tonic-gate case S_IWUSR: 7857c478bd9Sstevel@tonic-gate str = gettext("owner writable"); 7867c478bd9Sstevel@tonic-gate break; 7877c478bd9Sstevel@tonic-gate case S_IXUSR: 7887c478bd9Sstevel@tonic-gate str = gettext("owner executable"); 7897c478bd9Sstevel@tonic-gate break; 7907c478bd9Sstevel@tonic-gate case S_IRGRP: 7917c478bd9Sstevel@tonic-gate str = gettext("group readable"); 7927c478bd9Sstevel@tonic-gate break; 7937c478bd9Sstevel@tonic-gate case S_IWGRP: 7947c478bd9Sstevel@tonic-gate str = gettext("group writable"); 7957c478bd9Sstevel@tonic-gate break; 7967c478bd9Sstevel@tonic-gate case S_IXGRP: 7977c478bd9Sstevel@tonic-gate str = gettext("group executable"); 7987c478bd9Sstevel@tonic-gate break; 7997c478bd9Sstevel@tonic-gate case S_IROTH: 8007c478bd9Sstevel@tonic-gate str = gettext("world readable"); 8017c478bd9Sstevel@tonic-gate break; 8027c478bd9Sstevel@tonic-gate case S_IWOTH: 8037c478bd9Sstevel@tonic-gate str = gettext("world writable"); 8047c478bd9Sstevel@tonic-gate break; 8057c478bd9Sstevel@tonic-gate case S_IXOTH: 8067c478bd9Sstevel@tonic-gate str = gettext("world executable"); 8077c478bd9Sstevel@tonic-gate break; 8087c478bd9Sstevel@tonic-gate } 8097c478bd9Sstevel@tonic-gate if ((mode & bit) == (on ? 0 : bit)) { 8107c478bd9Sstevel@tonic-gate /* 8117c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 8127c478bd9Sstevel@tonic-gate * The first parameter below is a file name; the second 8137c478bd9Sstevel@tonic-gate * is one of the "(owner|group|world) (read|writ|execut)able" 8147c478bd9Sstevel@tonic-gate * strings from above. 8157c478bd9Sstevel@tonic-gate */ 8167c478bd9Sstevel@tonic-gate /* 8177c478bd9Sstevel@tonic-gate * The code below could be simplified but not in a way 8187c478bd9Sstevel@tonic-gate * that would easily translate to non-English locales. 8197c478bd9Sstevel@tonic-gate */ 8207c478bd9Sstevel@tonic-gate if (on) { 8217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must be %s.\n"), 8227c478bd9Sstevel@tonic-gate file, str); 8237c478bd9Sstevel@tonic-gate } else { 8247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must not be %s.\n"), 8257c478bd9Sstevel@tonic-gate file, str); 8267c478bd9Sstevel@tonic-gate } 8277c478bd9Sstevel@tonic-gate return (B_TRUE); 8287c478bd9Sstevel@tonic-gate } 8297c478bd9Sstevel@tonic-gate return (B_FALSE); 8307c478bd9Sstevel@tonic-gate } 8317c478bd9Sstevel@tonic-gate 8327c478bd9Sstevel@tonic-gate /* 8337c478bd9Sstevel@tonic-gate * We want to make sure that no zone has its zone path as a child node 8347c478bd9Sstevel@tonic-gate * (in the directory sense) of any other. We do that by comparing this 8357c478bd9Sstevel@tonic-gate * zone's path to the path of all other (non-global) zones. The comparison 8367c478bd9Sstevel@tonic-gate * in each case is simple: add '/' to the end of the path, then do a 8377c478bd9Sstevel@tonic-gate * strncmp() of the two paths, using the length of the shorter one. 8387c478bd9Sstevel@tonic-gate */ 8397c478bd9Sstevel@tonic-gate 8407c478bd9Sstevel@tonic-gate static int 8417c478bd9Sstevel@tonic-gate crosscheck_zonepaths(char *path) 8427c478bd9Sstevel@tonic-gate { 8437c478bd9Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 8447c478bd9Sstevel@tonic-gate char path_copy[MAXPATHLEN]; /* copy of original path */ 8457c478bd9Sstevel@tonic-gate char rpath_copy[MAXPATHLEN]; /* copy of original rpath */ 8467c478bd9Sstevel@tonic-gate struct zoneent *ze; 8477c478bd9Sstevel@tonic-gate int res, err; 8487c478bd9Sstevel@tonic-gate FILE *cookie; 8497c478bd9Sstevel@tonic-gate 8507c478bd9Sstevel@tonic-gate cookie = setzoneent(); 8517c478bd9Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 8527c478bd9Sstevel@tonic-gate /* Skip zones which are not installed. */ 8537c478bd9Sstevel@tonic-gate if (ze->zone_state < ZONE_STATE_INSTALLED) { 8547c478bd9Sstevel@tonic-gate free(ze); 8557c478bd9Sstevel@tonic-gate continue; 8567c478bd9Sstevel@tonic-gate } 8577c478bd9Sstevel@tonic-gate /* Skip the global zone and the current target zone. */ 8587c478bd9Sstevel@tonic-gate if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 || 8597c478bd9Sstevel@tonic-gate strcmp(ze->zone_name, target_zone) == 0) { 8607c478bd9Sstevel@tonic-gate free(ze); 8617c478bd9Sstevel@tonic-gate continue; 8627c478bd9Sstevel@tonic-gate } 8637c478bd9Sstevel@tonic-gate if (strlen(ze->zone_path) == 0) { 8647c478bd9Sstevel@tonic-gate /* old index file without path, fall back */ 8657c478bd9Sstevel@tonic-gate if ((err = zone_get_zonepath(ze->zone_name, 8667c478bd9Sstevel@tonic-gate ze->zone_path, sizeof (ze->zone_path))) != Z_OK) { 8677c478bd9Sstevel@tonic-gate errno = err; 8687c478bd9Sstevel@tonic-gate zperror2(ze->zone_name, 8697c478bd9Sstevel@tonic-gate gettext("could not get zone path")); 8707c478bd9Sstevel@tonic-gate free(ze); 8717c478bd9Sstevel@tonic-gate continue; 8727c478bd9Sstevel@tonic-gate } 8737c478bd9Sstevel@tonic-gate } 874108322fbScarlsonj (void) snprintf(path_copy, sizeof (path_copy), "%s%s", 875108322fbScarlsonj zonecfg_get_root(), ze->zone_path); 876108322fbScarlsonj res = resolvepath(path_copy, rpath, sizeof (rpath)); 8777c478bd9Sstevel@tonic-gate if (res == -1) { 8787c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 879108322fbScarlsonj zperror(path_copy, B_FALSE); 8807c478bd9Sstevel@tonic-gate free(ze); 8817c478bd9Sstevel@tonic-gate return (Z_ERR); 8827c478bd9Sstevel@tonic-gate } 8837c478bd9Sstevel@tonic-gate (void) printf(gettext("WARNING: zone %s is installed, " 8847c478bd9Sstevel@tonic-gate "but its %s %s does not exist.\n"), ze->zone_name, 885108322fbScarlsonj "zonepath", path_copy); 8867c478bd9Sstevel@tonic-gate free(ze); 8877c478bd9Sstevel@tonic-gate continue; 8887c478bd9Sstevel@tonic-gate } 8897c478bd9Sstevel@tonic-gate rpath[res] = '\0'; 8907c478bd9Sstevel@tonic-gate (void) snprintf(path_copy, sizeof (path_copy), "%s/", path); 8917c478bd9Sstevel@tonic-gate (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath); 8927c478bd9Sstevel@tonic-gate if (strncmp(path_copy, rpath_copy, 8937c478bd9Sstevel@tonic-gate min(strlen(path_copy), strlen(rpath_copy))) == 0) { 8945ee84fbdSgjelinek /* 8955ee84fbdSgjelinek * TRANSLATION_NOTE 8965ee84fbdSgjelinek * zonepath is a literal that should not be translated. 8975ee84fbdSgjelinek */ 8987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s zonepath (%s) and " 8997c478bd9Sstevel@tonic-gate "%s zonepath (%s) overlap.\n"), 9007c478bd9Sstevel@tonic-gate target_zone, path, ze->zone_name, rpath); 9017c478bd9Sstevel@tonic-gate free(ze); 9027c478bd9Sstevel@tonic-gate return (Z_ERR); 9037c478bd9Sstevel@tonic-gate } 9047c478bd9Sstevel@tonic-gate free(ze); 9057c478bd9Sstevel@tonic-gate } 9067c478bd9Sstevel@tonic-gate endzoneent(cookie); 9077c478bd9Sstevel@tonic-gate return (Z_OK); 9087c478bd9Sstevel@tonic-gate } 9097c478bd9Sstevel@tonic-gate 9107c478bd9Sstevel@tonic-gate static int 9117c478bd9Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num) 9127c478bd9Sstevel@tonic-gate { 9137c478bd9Sstevel@tonic-gate int res; /* result of last library/system call */ 9147c478bd9Sstevel@tonic-gate boolean_t err = B_FALSE; /* have we run into an error? */ 9157c478bd9Sstevel@tonic-gate struct stat stbuf; 9163f2f09c1Sdp struct statvfs64 vfsbuf; 9177c478bd9Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 9187c478bd9Sstevel@tonic-gate char ppath[MAXPATHLEN]; /* parent path */ 9197c478bd9Sstevel@tonic-gate char rppath[MAXPATHLEN]; /* resolved parent path */ 9207c478bd9Sstevel@tonic-gate char rootpath[MAXPATHLEN]; /* root path */ 9217c478bd9Sstevel@tonic-gate zone_state_t state; 9227c478bd9Sstevel@tonic-gate 9237c478bd9Sstevel@tonic-gate if (path[0] != '/') { 9247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9257c478bd9Sstevel@tonic-gate gettext("%s is not an absolute path.\n"), path); 9267c478bd9Sstevel@tonic-gate return (Z_ERR); 9277c478bd9Sstevel@tonic-gate } 9287c478bd9Sstevel@tonic-gate if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) { 9297c478bd9Sstevel@tonic-gate if ((errno != ENOENT) || 930865e09a4Sgjelinek (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL && 931865e09a4Sgjelinek cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) { 9327c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 9337c478bd9Sstevel@tonic-gate return (Z_ERR); 9347c478bd9Sstevel@tonic-gate } 9357c478bd9Sstevel@tonic-gate if (cmd_num == CMD_VERIFY) { 9365ee84fbdSgjelinek /* 9375ee84fbdSgjelinek * TRANSLATION_NOTE 9385ee84fbdSgjelinek * zoneadm is a literal that should not be translated. 9395ee84fbdSgjelinek */ 9407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("WARNING: %s does not " 9415ee84fbdSgjelinek "exist, so it could not be verified.\nWhen " 9425ee84fbdSgjelinek "'zoneadm %s' is run, '%s' will try to create\n%s, " 9435ee84fbdSgjelinek "and '%s' will be tried again,\nbut the '%s' may " 9445ee84fbdSgjelinek "fail if:\nthe parent directory of %s is group- or " 9455ee84fbdSgjelinek "other-writable\nor\n%s overlaps with any other " 9467c478bd9Sstevel@tonic-gate "installed zones.\n"), path, 9477c478bd9Sstevel@tonic-gate cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL), 9487c478bd9Sstevel@tonic-gate path, cmd_to_str(CMD_VERIFY), 9497c478bd9Sstevel@tonic-gate cmd_to_str(CMD_VERIFY), path, path); 9507c478bd9Sstevel@tonic-gate return (Z_OK); 9517c478bd9Sstevel@tonic-gate } 9527c478bd9Sstevel@tonic-gate /* 9537c478bd9Sstevel@tonic-gate * The zonepath is supposed to be mode 700 but its 9547c478bd9Sstevel@tonic-gate * parent(s) 755. So use 755 on the mkdirp() then 9557c478bd9Sstevel@tonic-gate * chmod() the zonepath itself to 700. 9567c478bd9Sstevel@tonic-gate */ 9577c478bd9Sstevel@tonic-gate if (mkdirp(path, DEFAULT_DIR_MODE) < 0) { 9587c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 9597c478bd9Sstevel@tonic-gate return (Z_ERR); 9607c478bd9Sstevel@tonic-gate } 9617c478bd9Sstevel@tonic-gate /* 9627c478bd9Sstevel@tonic-gate * If the chmod() fails, report the error, but might 9637c478bd9Sstevel@tonic-gate * as well continue the verify procedure. 9647c478bd9Sstevel@tonic-gate */ 9657c478bd9Sstevel@tonic-gate if (chmod(path, S_IRWXU) != 0) 9667c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 9677c478bd9Sstevel@tonic-gate /* 9687c478bd9Sstevel@tonic-gate * Since the mkdir() succeeded, we should not have to 9697c478bd9Sstevel@tonic-gate * worry about a subsequent ENOENT, thus this should 9707c478bd9Sstevel@tonic-gate * only recurse once. 9717c478bd9Sstevel@tonic-gate */ 972865e09a4Sgjelinek return (validate_zonepath(path, cmd_num)); 9737c478bd9Sstevel@tonic-gate } 9747c478bd9Sstevel@tonic-gate rpath[res] = '\0'; 9757c478bd9Sstevel@tonic-gate if (strcmp(path, rpath) != 0) { 9767c478bd9Sstevel@tonic-gate errno = Z_RESOLVED_PATH; 9777c478bd9Sstevel@tonic-gate zperror(path, B_TRUE); 9787c478bd9Sstevel@tonic-gate return (Z_ERR); 9797c478bd9Sstevel@tonic-gate } 9807c478bd9Sstevel@tonic-gate if ((res = stat(rpath, &stbuf)) != 0) { 9817c478bd9Sstevel@tonic-gate zperror(rpath, B_FALSE); 9827c478bd9Sstevel@tonic-gate return (Z_ERR); 9837c478bd9Sstevel@tonic-gate } 9847c478bd9Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 9857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 9867c478bd9Sstevel@tonic-gate rpath); 9877c478bd9Sstevel@tonic-gate return (Z_ERR); 9887c478bd9Sstevel@tonic-gate } 9894fceebdfSblakej if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) { 9907c478bd9Sstevel@tonic-gate (void) printf(gettext("WARNING: %s is on a temporary " 9910b5de56dSgjelinek "file system.\n"), rpath); 9927c478bd9Sstevel@tonic-gate } 9937c478bd9Sstevel@tonic-gate if (crosscheck_zonepaths(rpath) != Z_OK) 9947c478bd9Sstevel@tonic-gate return (Z_ERR); 9957c478bd9Sstevel@tonic-gate /* 9967c478bd9Sstevel@tonic-gate * Try to collect and report as many minor errors as possible 9977c478bd9Sstevel@tonic-gate * before returning, so the user can learn everything that needs 9987c478bd9Sstevel@tonic-gate * to be fixed up front. 9997c478bd9Sstevel@tonic-gate */ 10007c478bd9Sstevel@tonic-gate if (stbuf.st_uid != 0) { 10017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 10027c478bd9Sstevel@tonic-gate rpath); 10037c478bd9Sstevel@tonic-gate err = B_TRUE; 10047c478bd9Sstevel@tonic-gate } 10057c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath); 10067c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath); 10077c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath); 10087c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath); 10097c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath); 10107c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath); 10117c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath); 10127c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath); 10137c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath); 10147c478bd9Sstevel@tonic-gate 10157c478bd9Sstevel@tonic-gate (void) snprintf(ppath, sizeof (ppath), "%s/..", path); 10167c478bd9Sstevel@tonic-gate if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) { 10177c478bd9Sstevel@tonic-gate zperror(ppath, B_FALSE); 10187c478bd9Sstevel@tonic-gate return (Z_ERR); 10197c478bd9Sstevel@tonic-gate } 10207c478bd9Sstevel@tonic-gate rppath[res] = '\0'; 10217c478bd9Sstevel@tonic-gate if ((res = stat(rppath, &stbuf)) != 0) { 10227c478bd9Sstevel@tonic-gate zperror(rppath, B_FALSE); 10237c478bd9Sstevel@tonic-gate return (Z_ERR); 10247c478bd9Sstevel@tonic-gate } 10257c478bd9Sstevel@tonic-gate /* theoretically impossible */ 10267c478bd9Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 10277c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 10287c478bd9Sstevel@tonic-gate rppath); 10297c478bd9Sstevel@tonic-gate return (Z_ERR); 10307c478bd9Sstevel@tonic-gate } 10317c478bd9Sstevel@tonic-gate if (stbuf.st_uid != 0) { 10327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 10337c478bd9Sstevel@tonic-gate rppath); 10347c478bd9Sstevel@tonic-gate err = B_TRUE; 10357c478bd9Sstevel@tonic-gate } 10367c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath); 10377c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath); 10387c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath); 10397c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath); 10407c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath); 10417c478bd9Sstevel@tonic-gate if (strcmp(rpath, rppath) == 0) { 10427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is its own parent.\n"), 10437c478bd9Sstevel@tonic-gate rppath); 10447c478bd9Sstevel@tonic-gate err = B_TRUE; 10457c478bd9Sstevel@tonic-gate } 10467c478bd9Sstevel@tonic-gate 10473f2f09c1Sdp if (statvfs64(rpath, &vfsbuf) != 0) { 10487c478bd9Sstevel@tonic-gate zperror(rpath, B_FALSE); 10497c478bd9Sstevel@tonic-gate return (Z_ERR); 10507c478bd9Sstevel@tonic-gate } 1051b5abaf04Sgjelinek if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) { 10525ee84fbdSgjelinek /* 10535ee84fbdSgjelinek * TRANSLATION_NOTE 10545ee84fbdSgjelinek * Zonepath and NFS are literals that should not be translated. 10555ee84fbdSgjelinek */ 10565ee84fbdSgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on an NFS " 10570b5de56dSgjelinek "mounted file system.\n" 10580b5de56dSgjelinek "\tA local file system must be used.\n"), rpath); 1059b5abaf04Sgjelinek return (Z_ERR); 1060b5abaf04Sgjelinek } 1061b5abaf04Sgjelinek if (vfsbuf.f_flag & ST_NOSUID) { 10625ee84fbdSgjelinek /* 10635ee84fbdSgjelinek * TRANSLATION_NOTE 10645ee84fbdSgjelinek * Zonepath and nosuid are literals that should not be 10655ee84fbdSgjelinek * translated. 10665ee84fbdSgjelinek */ 1067b5abaf04Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on a nosuid " 10680b5de56dSgjelinek "file system.\n"), rpath); 10697c478bd9Sstevel@tonic-gate return (Z_ERR); 10707c478bd9Sstevel@tonic-gate } 10717c478bd9Sstevel@tonic-gate 10727c478bd9Sstevel@tonic-gate if ((res = zone_get_state(target_zone, &state)) != Z_OK) { 10737c478bd9Sstevel@tonic-gate errno = res; 10747c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get state")); 10757c478bd9Sstevel@tonic-gate return (Z_ERR); 10767c478bd9Sstevel@tonic-gate } 10777c478bd9Sstevel@tonic-gate /* 10787c478bd9Sstevel@tonic-gate * The existence of the root path is only bad in the configured state, 10797c478bd9Sstevel@tonic-gate * as it is *supposed* to be there at the installed and later states. 1080ee519a1fSgjelinek * However, the root path is expected to be there if the zone is 1081ee519a1fSgjelinek * detached. 10827c478bd9Sstevel@tonic-gate * State/command mismatches are caught earlier in verify_details(). 10837c478bd9Sstevel@tonic-gate */ 1084ee519a1fSgjelinek if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) { 10857c478bd9Sstevel@tonic-gate if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >= 10867c478bd9Sstevel@tonic-gate sizeof (rootpath)) { 10875ee84fbdSgjelinek /* 10885ee84fbdSgjelinek * TRANSLATION_NOTE 10895ee84fbdSgjelinek * Zonepath is a literal that should not be translated. 10905ee84fbdSgjelinek */ 10917c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 10927c478bd9Sstevel@tonic-gate gettext("Zonepath %s is too long.\n"), rpath); 10937c478bd9Sstevel@tonic-gate return (Z_ERR); 10947c478bd9Sstevel@tonic-gate } 10957c478bd9Sstevel@tonic-gate if ((res = stat(rootpath, &stbuf)) == 0) { 1096ee519a1fSgjelinek if (zonecfg_detached(rpath)) 1097ee519a1fSgjelinek (void) fprintf(stderr, 1098ee519a1fSgjelinek gettext("Cannot %s detached " 1099ee519a1fSgjelinek "zone.\nUse attach or remove %s " 1100ee519a1fSgjelinek "directory.\n"), cmd_to_str(cmd_num), 1101ee519a1fSgjelinek rpath); 1102ee519a1fSgjelinek else 1103ee519a1fSgjelinek (void) fprintf(stderr, 1104ee519a1fSgjelinek gettext("Rootpath %s exists; " 1105ee519a1fSgjelinek "remove or move aside prior to %s.\n"), 1106ee519a1fSgjelinek rootpath, cmd_to_str(cmd_num)); 11077c478bd9Sstevel@tonic-gate return (Z_ERR); 11087c478bd9Sstevel@tonic-gate } 11097c478bd9Sstevel@tonic-gate } 11107c478bd9Sstevel@tonic-gate 11117c478bd9Sstevel@tonic-gate return (err ? Z_ERR : Z_OK); 11127c478bd9Sstevel@tonic-gate } 11137c478bd9Sstevel@tonic-gate 11149acbbeafSnn35248 /* 11159acbbeafSnn35248 * The following two routines implement a simple locking mechanism to 11169acbbeafSnn35248 * ensure that only one instance of zoneadm at a time is able to manipulate 11179acbbeafSnn35248 * a given zone. The lock is built on top of an fcntl(2) lock of 11189acbbeafSnn35248 * [<altroot>]/var/run/zones/<zonename>.zoneadm.lock. If a zoneadm instance 11199acbbeafSnn35248 * can grab that lock, it is allowed to manipulate the zone. 11209acbbeafSnn35248 * 11219acbbeafSnn35248 * Since zoneadm may call external applications which in turn invoke 11229acbbeafSnn35248 * zoneadm again, we introduce the notion of "lock inheritance". Any 11239acbbeafSnn35248 * instance of zoneadm that has another instance in its ancestry is assumed 11249acbbeafSnn35248 * to be acting on behalf of the original zoneadm, and is thus allowed to 11259acbbeafSnn35248 * manipulate its zone. 11269acbbeafSnn35248 * 11279acbbeafSnn35248 * This inheritance is implemented via the _ZONEADM_LOCK_HELD environment 11289acbbeafSnn35248 * variable. When zoneadm is granted a lock on its zone, this environment 11299acbbeafSnn35248 * variable is set to 1. When it releases the lock, the variable is set to 11309acbbeafSnn35248 * 0. Since a child process inherits its parent's environment, checking 11319acbbeafSnn35248 * the state of this variable indicates whether or not any ancestor owns 11329acbbeafSnn35248 * the lock. 11339acbbeafSnn35248 */ 11347c478bd9Sstevel@tonic-gate static void 11357c478bd9Sstevel@tonic-gate release_lock_file(int lockfd) 11367c478bd9Sstevel@tonic-gate { 11379acbbeafSnn35248 /* 11389acbbeafSnn35248 * If we are cleaning up from a failed attempt to lock the zone for 11399acbbeafSnn35248 * the first time, we might have a zone_lock_cnt of 0. In that 11409acbbeafSnn35248 * error case, we don't want to do anything but close the lock 11419acbbeafSnn35248 * file. 11429acbbeafSnn35248 */ 11439acbbeafSnn35248 assert(zone_lock_cnt >= 0); 11449acbbeafSnn35248 if (zone_lock_cnt > 0) { 11459acbbeafSnn35248 assert(getenv(LOCK_ENV_VAR) != NULL); 11469acbbeafSnn35248 assert(atoi(getenv(LOCK_ENV_VAR)) == 1); 11479acbbeafSnn35248 if (--zone_lock_cnt > 0) { 11489acbbeafSnn35248 assert(lockfd == -1); 11499acbbeafSnn35248 return; 11509acbbeafSnn35248 } 11519acbbeafSnn35248 if (putenv(zoneadm_lock_not_held) != 0) { 11529acbbeafSnn35248 zperror(target_zone, B_TRUE); 11539acbbeafSnn35248 exit(Z_ERR); 11549acbbeafSnn35248 } 11559acbbeafSnn35248 } 11569acbbeafSnn35248 assert(lockfd >= 0); 11577c478bd9Sstevel@tonic-gate (void) close(lockfd); 11587c478bd9Sstevel@tonic-gate } 11597c478bd9Sstevel@tonic-gate 11607c478bd9Sstevel@tonic-gate static int 11617c478bd9Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd) 11627c478bd9Sstevel@tonic-gate { 11637c478bd9Sstevel@tonic-gate char pathbuf[PATH_MAX]; 11647c478bd9Sstevel@tonic-gate struct flock flock; 11657c478bd9Sstevel@tonic-gate 11669acbbeafSnn35248 /* 11679acbbeafSnn35248 * If we already have the lock, we can skip this expensive song 11689acbbeafSnn35248 * and dance. 11699acbbeafSnn35248 */ 11709acbbeafSnn35248 if (zone_lock_cnt > 0) { 11719acbbeafSnn35248 zone_lock_cnt++; 11729acbbeafSnn35248 *lockfd = -1; 11739acbbeafSnn35248 return (Z_OK); 11749acbbeafSnn35248 } 11759acbbeafSnn35248 assert(getenv(LOCK_ENV_VAR) != NULL); 11769acbbeafSnn35248 assert(atoi(getenv(LOCK_ENV_VAR)) == 0); 11779acbbeafSnn35248 1178108322fbScarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(), 1179108322fbScarlsonj ZONES_TMPDIR) >= sizeof (pathbuf)) { 1180108322fbScarlsonj zerror(gettext("alternate root path is too long")); 1181108322fbScarlsonj return (Z_ERR); 1182108322fbScarlsonj } 1183108322fbScarlsonj if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) { 1184108322fbScarlsonj zerror(gettext("could not mkdir %s: %s"), pathbuf, 11857c478bd9Sstevel@tonic-gate strerror(errno)); 11867c478bd9Sstevel@tonic-gate return (Z_ERR); 11877c478bd9Sstevel@tonic-gate } 1188108322fbScarlsonj (void) chmod(pathbuf, S_IRWXU); 11897c478bd9Sstevel@tonic-gate 11907c478bd9Sstevel@tonic-gate /* 11917c478bd9Sstevel@tonic-gate * One of these lock files is created for each zone (when needed). 11927c478bd9Sstevel@tonic-gate * The lock files are not cleaned up (except on system reboot), 11937c478bd9Sstevel@tonic-gate * but since there is only one per zone, there is no resource 11947c478bd9Sstevel@tonic-gate * starvation issue. 11957c478bd9Sstevel@tonic-gate */ 1196108322fbScarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock", 1197108322fbScarlsonj zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) { 1198108322fbScarlsonj zerror(gettext("alternate root path is too long")); 1199108322fbScarlsonj return (Z_ERR); 1200108322fbScarlsonj } 12017c478bd9Sstevel@tonic-gate if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { 12027c478bd9Sstevel@tonic-gate zerror(gettext("could not open %s: %s"), pathbuf, 12037c478bd9Sstevel@tonic-gate strerror(errno)); 12047c478bd9Sstevel@tonic-gate return (Z_ERR); 12057c478bd9Sstevel@tonic-gate } 12067c478bd9Sstevel@tonic-gate /* 12077c478bd9Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmds 12087c478bd9Sstevel@tonic-gate */ 12097c478bd9Sstevel@tonic-gate flock.l_type = F_WRLCK; 12107c478bd9Sstevel@tonic-gate flock.l_whence = SEEK_SET; 12117c478bd9Sstevel@tonic-gate flock.l_start = (off_t)0; 12127c478bd9Sstevel@tonic-gate flock.l_len = (off_t)0; 12139acbbeafSnn35248 if ((fcntl(*lockfd, F_SETLKW, &flock) < 0) || 12149acbbeafSnn35248 (putenv(zoneadm_lock_held) != 0)) { 12157c478bd9Sstevel@tonic-gate zerror(gettext("unable to lock %s: %s"), pathbuf, 12167c478bd9Sstevel@tonic-gate strerror(errno)); 12177c478bd9Sstevel@tonic-gate release_lock_file(*lockfd); 12187c478bd9Sstevel@tonic-gate return (Z_ERR); 12197c478bd9Sstevel@tonic-gate } 12209acbbeafSnn35248 zone_lock_cnt = 1; 12217c478bd9Sstevel@tonic-gate return (Z_OK); 12227c478bd9Sstevel@tonic-gate } 12237c478bd9Sstevel@tonic-gate 1224108322fbScarlsonj static boolean_t 12257c478bd9Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer) 12267c478bd9Sstevel@tonic-gate { 1227108322fbScarlsonj return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH, 1228108322fbScarlsonj zonecfg_get_root(), zone_name) < PATH_MAX); 12297c478bd9Sstevel@tonic-gate } 12307c478bd9Sstevel@tonic-gate 12317c478bd9Sstevel@tonic-gate /* 12327c478bd9Sstevel@tonic-gate * system daemons are not audited. For the global zone, this occurs 12337c478bd9Sstevel@tonic-gate * "naturally" since init is started with the default audit 12347c478bd9Sstevel@tonic-gate * characteristics. Since zoneadmd is a system daemon and it starts 12357c478bd9Sstevel@tonic-gate * init for a zone, it is necessary to clear out the audit 12367c478bd9Sstevel@tonic-gate * characteristics inherited from whomever started zoneadmd. This is 12377c478bd9Sstevel@tonic-gate * indicated by the audit id, which is set from the ruid parameter of 12387c478bd9Sstevel@tonic-gate * adt_set_user(), below. 12397c478bd9Sstevel@tonic-gate */ 12407c478bd9Sstevel@tonic-gate 12417c478bd9Sstevel@tonic-gate static void 12427c478bd9Sstevel@tonic-gate prepare_audit_context() 12437c478bd9Sstevel@tonic-gate { 12447c478bd9Sstevel@tonic-gate adt_session_data_t *ah; 12457c478bd9Sstevel@tonic-gate char *failure = gettext("audit failure: %s"); 12467c478bd9Sstevel@tonic-gate 12477c478bd9Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 12487c478bd9Sstevel@tonic-gate zerror(failure, strerror(errno)); 12497c478bd9Sstevel@tonic-gate return; 12507c478bd9Sstevel@tonic-gate } 12517c478bd9Sstevel@tonic-gate if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT, 12527c478bd9Sstevel@tonic-gate ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) { 12537c478bd9Sstevel@tonic-gate zerror(failure, strerror(errno)); 12547c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 12557c478bd9Sstevel@tonic-gate return; 12567c478bd9Sstevel@tonic-gate } 12577c478bd9Sstevel@tonic-gate if (adt_set_proc(ah)) 12587c478bd9Sstevel@tonic-gate zerror(failure, strerror(errno)); 12597c478bd9Sstevel@tonic-gate 12607c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 12617c478bd9Sstevel@tonic-gate } 12627c478bd9Sstevel@tonic-gate 12637c478bd9Sstevel@tonic-gate static int 12647c478bd9Sstevel@tonic-gate start_zoneadmd(const char *zone_name) 12657c478bd9Sstevel@tonic-gate { 12667c478bd9Sstevel@tonic-gate char doorpath[PATH_MAX]; 12677c478bd9Sstevel@tonic-gate pid_t child_pid; 12687c478bd9Sstevel@tonic-gate int error = Z_ERR; 12697c478bd9Sstevel@tonic-gate int doorfd, lockfd; 12707c478bd9Sstevel@tonic-gate struct door_info info; 12717c478bd9Sstevel@tonic-gate 1272108322fbScarlsonj if (!get_doorname(zone_name, doorpath)) 1273108322fbScarlsonj return (Z_ERR); 12747c478bd9Sstevel@tonic-gate 12757c478bd9Sstevel@tonic-gate if (grab_lock_file(zone_name, &lockfd) != Z_OK) 12767c478bd9Sstevel@tonic-gate return (Z_ERR); 12777c478bd9Sstevel@tonic-gate 12787c478bd9Sstevel@tonic-gate /* 12797c478bd9Sstevel@tonic-gate * Now that we have the lock, re-confirm that the daemon is 12807c478bd9Sstevel@tonic-gate * *not* up and working fine. If it is still down, we have a green 12817c478bd9Sstevel@tonic-gate * light to start it. 12827c478bd9Sstevel@tonic-gate */ 12837c478bd9Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 12847c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 12857c478bd9Sstevel@tonic-gate zperror(doorpath, B_FALSE); 12867c478bd9Sstevel@tonic-gate goto out; 12877c478bd9Sstevel@tonic-gate } 12887c478bd9Sstevel@tonic-gate } else { 12897c478bd9Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 12907c478bd9Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 12917c478bd9Sstevel@tonic-gate error = Z_OK; 12927c478bd9Sstevel@tonic-gate (void) close(doorfd); 12937c478bd9Sstevel@tonic-gate goto out; 12947c478bd9Sstevel@tonic-gate } 12957c478bd9Sstevel@tonic-gate (void) close(doorfd); 12967c478bd9Sstevel@tonic-gate } 12977c478bd9Sstevel@tonic-gate 12987c478bd9Sstevel@tonic-gate if ((child_pid = fork()) == -1) { 12997c478bd9Sstevel@tonic-gate zperror(gettext("could not fork"), B_FALSE); 13007c478bd9Sstevel@tonic-gate goto out; 13017c478bd9Sstevel@tonic-gate } else if (child_pid == 0) { 1302108322fbScarlsonj const char *argv[6], **ap; 13037c478bd9Sstevel@tonic-gate 1304108322fbScarlsonj /* child process */ 13057c478bd9Sstevel@tonic-gate prepare_audit_context(); 13067c478bd9Sstevel@tonic-gate 1307108322fbScarlsonj ap = argv; 1308108322fbScarlsonj *ap++ = "zoneadmd"; 1309108322fbScarlsonj *ap++ = "-z"; 1310108322fbScarlsonj *ap++ = zone_name; 1311108322fbScarlsonj if (zonecfg_in_alt_root()) { 1312108322fbScarlsonj *ap++ = "-R"; 1313108322fbScarlsonj *ap++ = zonecfg_get_root(); 1314108322fbScarlsonj } 1315108322fbScarlsonj *ap = NULL; 1316108322fbScarlsonj 1317108322fbScarlsonj (void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv); 13185ee84fbdSgjelinek /* 13195ee84fbdSgjelinek * TRANSLATION_NOTE 13205ee84fbdSgjelinek * zoneadmd is a literal that should not be translated. 13215ee84fbdSgjelinek */ 13227c478bd9Sstevel@tonic-gate zperror(gettext("could not exec zoneadmd"), B_FALSE); 13237c478bd9Sstevel@tonic-gate _exit(Z_ERR); 13247c478bd9Sstevel@tonic-gate } else { 13257c478bd9Sstevel@tonic-gate /* parent process */ 13267c478bd9Sstevel@tonic-gate pid_t retval; 13277c478bd9Sstevel@tonic-gate int pstatus = 0; 13287c478bd9Sstevel@tonic-gate 13297c478bd9Sstevel@tonic-gate do { 13307c478bd9Sstevel@tonic-gate retval = waitpid(child_pid, &pstatus, 0); 13317c478bd9Sstevel@tonic-gate } while (retval != child_pid); 13327c478bd9Sstevel@tonic-gate if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) && 13337c478bd9Sstevel@tonic-gate WEXITSTATUS(pstatus) != 0)) { 13347c478bd9Sstevel@tonic-gate zerror(gettext("could not start %s"), "zoneadmd"); 13357c478bd9Sstevel@tonic-gate goto out; 13367c478bd9Sstevel@tonic-gate } 13377c478bd9Sstevel@tonic-gate } 13387c478bd9Sstevel@tonic-gate error = Z_OK; 13397c478bd9Sstevel@tonic-gate out: 13407c478bd9Sstevel@tonic-gate release_lock_file(lockfd); 13417c478bd9Sstevel@tonic-gate return (error); 13427c478bd9Sstevel@tonic-gate } 13437c478bd9Sstevel@tonic-gate 13447c478bd9Sstevel@tonic-gate static int 13457c478bd9Sstevel@tonic-gate ping_zoneadmd(const char *zone_name) 13467c478bd9Sstevel@tonic-gate { 13477c478bd9Sstevel@tonic-gate char doorpath[PATH_MAX]; 13487c478bd9Sstevel@tonic-gate int doorfd; 13497c478bd9Sstevel@tonic-gate struct door_info info; 13507c478bd9Sstevel@tonic-gate 1351108322fbScarlsonj if (!get_doorname(zone_name, doorpath)) 1352108322fbScarlsonj return (Z_ERR); 13537c478bd9Sstevel@tonic-gate 13547c478bd9Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 13557c478bd9Sstevel@tonic-gate return (Z_ERR); 13567c478bd9Sstevel@tonic-gate } 13577c478bd9Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 13587c478bd9Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 13597c478bd9Sstevel@tonic-gate (void) close(doorfd); 13607c478bd9Sstevel@tonic-gate return (Z_OK); 13617c478bd9Sstevel@tonic-gate } 13627c478bd9Sstevel@tonic-gate (void) close(doorfd); 13637c478bd9Sstevel@tonic-gate return (Z_ERR); 13647c478bd9Sstevel@tonic-gate } 13657c478bd9Sstevel@tonic-gate 13667c478bd9Sstevel@tonic-gate static int 13677c478bd9Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg) 13687c478bd9Sstevel@tonic-gate { 13697c478bd9Sstevel@tonic-gate char doorpath[PATH_MAX]; 13707c478bd9Sstevel@tonic-gate int doorfd, result; 13717c478bd9Sstevel@tonic-gate door_arg_t darg; 13727c478bd9Sstevel@tonic-gate 13737c478bd9Sstevel@tonic-gate zoneid_t zoneid; 13747c478bd9Sstevel@tonic-gate uint64_t uniqid = 0; 13757c478bd9Sstevel@tonic-gate 13767c478bd9Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 13777c478bd9Sstevel@tonic-gate size_t rlen; 13787c478bd9Sstevel@tonic-gate char *cp, *errbuf; 13797c478bd9Sstevel@tonic-gate 13807c478bd9Sstevel@tonic-gate rlen = getpagesize(); 13817c478bd9Sstevel@tonic-gate if ((rvalp = malloc(rlen)) == NULL) { 13827c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rlen, 13837c478bd9Sstevel@tonic-gate strerror(errno)); 13847c478bd9Sstevel@tonic-gate return (-1); 13857c478bd9Sstevel@tonic-gate } 13867c478bd9Sstevel@tonic-gate 13877c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) { 13887c478bd9Sstevel@tonic-gate (void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 13897c478bd9Sstevel@tonic-gate sizeof (uniqid)); 13907c478bd9Sstevel@tonic-gate } 13917c478bd9Sstevel@tonic-gate arg->uniqid = uniqid; 13927c478bd9Sstevel@tonic-gate (void) strlcpy(arg->locale, locale, sizeof (arg->locale)); 1393108322fbScarlsonj if (!get_doorname(zone_name, doorpath)) { 1394108322fbScarlsonj zerror(gettext("alternate root path is too long")); 1395108322fbScarlsonj free(rvalp); 1396108322fbScarlsonj return (-1); 1397108322fbScarlsonj } 13987c478bd9Sstevel@tonic-gate 13997c478bd9Sstevel@tonic-gate /* 14007c478bd9Sstevel@tonic-gate * Loop trying to start zoneadmd; if something goes seriously 14017c478bd9Sstevel@tonic-gate * wrong we break out and fail. 14027c478bd9Sstevel@tonic-gate */ 14037c478bd9Sstevel@tonic-gate for (;;) { 14047c478bd9Sstevel@tonic-gate if (start_zoneadmd(zone_name) != Z_OK) 14057c478bd9Sstevel@tonic-gate break; 14067c478bd9Sstevel@tonic-gate 14077c478bd9Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 14087c478bd9Sstevel@tonic-gate zperror(gettext("failed to open zone door"), B_FALSE); 14097c478bd9Sstevel@tonic-gate break; 14107c478bd9Sstevel@tonic-gate } 14117c478bd9Sstevel@tonic-gate 14127c478bd9Sstevel@tonic-gate darg.data_ptr = (char *)arg; 14137c478bd9Sstevel@tonic-gate darg.data_size = sizeof (*arg); 14147c478bd9Sstevel@tonic-gate darg.desc_ptr = NULL; 14157c478bd9Sstevel@tonic-gate darg.desc_num = 0; 14167c478bd9Sstevel@tonic-gate darg.rbuf = (char *)rvalp; 14177c478bd9Sstevel@tonic-gate darg.rsize = rlen; 14187c478bd9Sstevel@tonic-gate if (door_call(doorfd, &darg) != 0) { 14197c478bd9Sstevel@tonic-gate (void) close(doorfd); 14207c478bd9Sstevel@tonic-gate /* 14217c478bd9Sstevel@tonic-gate * We'll get EBADF if the door has been revoked. 14227c478bd9Sstevel@tonic-gate */ 14237c478bd9Sstevel@tonic-gate if (errno != EBADF) { 14247c478bd9Sstevel@tonic-gate zperror(gettext("door_call failed"), B_FALSE); 14257c478bd9Sstevel@tonic-gate break; 14267c478bd9Sstevel@tonic-gate } 14277c478bd9Sstevel@tonic-gate continue; /* take another lap */ 14287c478bd9Sstevel@tonic-gate } 14297c478bd9Sstevel@tonic-gate (void) close(doorfd); 14307c478bd9Sstevel@tonic-gate 14317c478bd9Sstevel@tonic-gate if (darg.data_size == 0) { 14327c478bd9Sstevel@tonic-gate /* Door server is going away; kick it again. */ 14337c478bd9Sstevel@tonic-gate continue; 14347c478bd9Sstevel@tonic-gate } 14357c478bd9Sstevel@tonic-gate 14367c478bd9Sstevel@tonic-gate errbuf = rvalp->errbuf; 14377c478bd9Sstevel@tonic-gate while (*errbuf != '\0') { 14387c478bd9Sstevel@tonic-gate /* 14397c478bd9Sstevel@tonic-gate * Remove any newlines since zerror() 14407c478bd9Sstevel@tonic-gate * will append one automatically. 14417c478bd9Sstevel@tonic-gate */ 14427c478bd9Sstevel@tonic-gate cp = strchr(errbuf, '\n'); 14437c478bd9Sstevel@tonic-gate if (cp != NULL) 14447c478bd9Sstevel@tonic-gate *cp = '\0'; 14457c478bd9Sstevel@tonic-gate zerror("%s", errbuf); 14467c478bd9Sstevel@tonic-gate if (cp == NULL) 14477c478bd9Sstevel@tonic-gate break; 14487c478bd9Sstevel@tonic-gate errbuf = cp + 1; 14497c478bd9Sstevel@tonic-gate } 14507c478bd9Sstevel@tonic-gate result = rvalp->rval == 0 ? 0 : -1; 14517c478bd9Sstevel@tonic-gate free(rvalp); 14527c478bd9Sstevel@tonic-gate return (result); 14537c478bd9Sstevel@tonic-gate } 14547c478bd9Sstevel@tonic-gate 14557c478bd9Sstevel@tonic-gate free(rvalp); 14567c478bd9Sstevel@tonic-gate return (-1); 14577c478bd9Sstevel@tonic-gate } 14587c478bd9Sstevel@tonic-gate 14597c478bd9Sstevel@tonic-gate static int 1460ce28b40eSzt129084 invoke_brand_handler(int cmd_num, char *argv[]) 1461ce28b40eSzt129084 { 1462ce28b40eSzt129084 zone_dochandle_t handle; 1463ce28b40eSzt129084 int err; 1464ce28b40eSzt129084 1465ce28b40eSzt129084 if ((handle = zonecfg_init_handle()) == NULL) { 1466ce28b40eSzt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 1467ce28b40eSzt129084 return (Z_ERR); 1468ce28b40eSzt129084 } 1469ce28b40eSzt129084 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 1470ce28b40eSzt129084 errno = err; 1471ce28b40eSzt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 1472ce28b40eSzt129084 zonecfg_fini_handle(handle); 1473ce28b40eSzt129084 return (Z_ERR); 1474ce28b40eSzt129084 } 1475ce28b40eSzt129084 if (verify_brand(handle, cmd_num, argv) != Z_OK) { 1476ce28b40eSzt129084 zonecfg_fini_handle(handle); 1477ce28b40eSzt129084 return (Z_ERR); 1478ce28b40eSzt129084 } 1479ce28b40eSzt129084 zonecfg_fini_handle(handle); 1480ce28b40eSzt129084 return (Z_OK); 1481ce28b40eSzt129084 } 1482ce28b40eSzt129084 1483ce28b40eSzt129084 static int 14847c478bd9Sstevel@tonic-gate ready_func(int argc, char *argv[]) 14857c478bd9Sstevel@tonic-gate { 14867c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 14877c478bd9Sstevel@tonic-gate int arg; 14887c478bd9Sstevel@tonic-gate 1489108322fbScarlsonj if (zonecfg_in_alt_root()) { 1490108322fbScarlsonj zerror(gettext("cannot ready zone in alternate root")); 1491108322fbScarlsonj return (Z_ERR); 1492108322fbScarlsonj } 1493108322fbScarlsonj 14947c478bd9Sstevel@tonic-gate optind = 0; 14957c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 14967c478bd9Sstevel@tonic-gate switch (arg) { 14977c478bd9Sstevel@tonic-gate case '?': 14987c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 14997c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 15007c478bd9Sstevel@tonic-gate default: 15017c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 15027c478bd9Sstevel@tonic-gate return (Z_USAGE); 15037c478bd9Sstevel@tonic-gate } 15047c478bd9Sstevel@tonic-gate } 15057c478bd9Sstevel@tonic-gate if (argc > optind) { 15067c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 15077c478bd9Sstevel@tonic-gate return (Z_USAGE); 15087c478bd9Sstevel@tonic-gate } 15099acbbeafSnn35248 if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE) 15109acbbeafSnn35248 != Z_OK) 15117c478bd9Sstevel@tonic-gate return (Z_ERR); 1512ce28b40eSzt129084 if (verify_details(CMD_READY, argv) != Z_OK) 15137c478bd9Sstevel@tonic-gate return (Z_ERR); 15147c478bd9Sstevel@tonic-gate 15157c478bd9Sstevel@tonic-gate zarg.cmd = Z_READY; 15167c478bd9Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 15177c478bd9Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 15187c478bd9Sstevel@tonic-gate return (Z_ERR); 15197c478bd9Sstevel@tonic-gate } 15207c478bd9Sstevel@tonic-gate return (Z_OK); 15217c478bd9Sstevel@tonic-gate } 15227c478bd9Sstevel@tonic-gate 15237c478bd9Sstevel@tonic-gate static int 15247c478bd9Sstevel@tonic-gate boot_func(int argc, char *argv[]) 15257c478bd9Sstevel@tonic-gate { 15267c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 15279acbbeafSnn35248 boolean_t force = B_FALSE; 15287c478bd9Sstevel@tonic-gate int arg; 15297c478bd9Sstevel@tonic-gate 1530108322fbScarlsonj if (zonecfg_in_alt_root()) { 1531108322fbScarlsonj zerror(gettext("cannot boot zone in alternate root")); 1532108322fbScarlsonj return (Z_ERR); 1533108322fbScarlsonj } 1534108322fbScarlsonj 15357c478bd9Sstevel@tonic-gate zarg.bootbuf[0] = '\0'; 15367c478bd9Sstevel@tonic-gate 15377c478bd9Sstevel@tonic-gate /* 15383f2f09c1Sdp * The following getopt processes arguments to zone boot; that 15393f2f09c1Sdp * is to say, the [here] portion of the argument string: 15403f2f09c1Sdp * 15413f2f09c1Sdp * zoneadm -z myzone boot [here] -- -v -m verbose 15423f2f09c1Sdp * 15433f2f09c1Sdp * Where [here] can either be nothing, -? (in which case we bail 15449acbbeafSnn35248 * and print usage), -f (a private option to indicate that the 15459acbbeafSnn35248 * boot operation should be 'forced'), or -s. Support for -s is 15469acbbeafSnn35248 * vestigal and obsolete, but is retained because it was a 15479acbbeafSnn35248 * documented interface and there are known consumers including 15489acbbeafSnn35248 * admin/install; the proper way to specify boot arguments like -s 15499acbbeafSnn35248 * is: 15503f2f09c1Sdp * 15513f2f09c1Sdp * zoneadm -z myzone boot -- -s -v -m verbose. 15527c478bd9Sstevel@tonic-gate */ 15537c478bd9Sstevel@tonic-gate optind = 0; 15549acbbeafSnn35248 while ((arg = getopt(argc, argv, "?fs")) != EOF) { 15557c478bd9Sstevel@tonic-gate switch (arg) { 15567c478bd9Sstevel@tonic-gate case '?': 15577c478bd9Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 15587c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 15597c478bd9Sstevel@tonic-gate case 's': 15607c478bd9Sstevel@tonic-gate (void) strlcpy(zarg.bootbuf, "-s", 15617c478bd9Sstevel@tonic-gate sizeof (zarg.bootbuf)); 15627c478bd9Sstevel@tonic-gate break; 15639acbbeafSnn35248 case 'f': 15649acbbeafSnn35248 force = B_TRUE; 15659acbbeafSnn35248 break; 15667c478bd9Sstevel@tonic-gate default: 15677c478bd9Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 15687c478bd9Sstevel@tonic-gate return (Z_USAGE); 15697c478bd9Sstevel@tonic-gate } 15707c478bd9Sstevel@tonic-gate } 15713f2f09c1Sdp 15723f2f09c1Sdp for (; optind < argc; optind++) { 15733f2f09c1Sdp if (strlcat(zarg.bootbuf, argv[optind], 15743f2f09c1Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 15753f2f09c1Sdp zerror(gettext("Boot argument list too long")); 15763f2f09c1Sdp return (Z_ERR); 15777c478bd9Sstevel@tonic-gate } 15783f2f09c1Sdp if (optind < argc - 1) 15793f2f09c1Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 15803f2f09c1Sdp sizeof (zarg.bootbuf)) { 15813f2f09c1Sdp zerror(gettext("Boot argument list too long")); 15823f2f09c1Sdp return (Z_ERR); 15833f2f09c1Sdp } 15843f2f09c1Sdp } 15859acbbeafSnn35248 if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force) 15869acbbeafSnn35248 != Z_OK) 15877c478bd9Sstevel@tonic-gate return (Z_ERR); 1588ce28b40eSzt129084 if (verify_details(CMD_BOOT, argv) != Z_OK) 15897c478bd9Sstevel@tonic-gate return (Z_ERR); 15909acbbeafSnn35248 zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT; 15917c478bd9Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 15927c478bd9Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 15937c478bd9Sstevel@tonic-gate return (Z_ERR); 15947c478bd9Sstevel@tonic-gate } 15950209230bSgjelinek 15967c478bd9Sstevel@tonic-gate return (Z_OK); 15977c478bd9Sstevel@tonic-gate } 15987c478bd9Sstevel@tonic-gate 15997c478bd9Sstevel@tonic-gate static void 16007c478bd9Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr) 16017c478bd9Sstevel@tonic-gate { 16027c478bd9Sstevel@tonic-gate ssize_t result; 1603555afedfScarlsonj uuid_t uuid; 1604555afedfScarlsonj FILE *fp; 1605f4b3ec61Sdh155122 ushort_t flags; 1606555afedfScarlsonj 1607555afedfScarlsonj (void) memset(zeptr, 0, sizeof (*zeptr)); 16087c478bd9Sstevel@tonic-gate 16097c478bd9Sstevel@tonic-gate zeptr->zid = zid; 1610555afedfScarlsonj 16117c478bd9Sstevel@tonic-gate /* 16127c478bd9Sstevel@tonic-gate * Since we're looking up our own (non-global) zone name, 16137c478bd9Sstevel@tonic-gate * we can be assured that it will succeed. 16147c478bd9Sstevel@tonic-gate */ 16157c478bd9Sstevel@tonic-gate result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname)); 16167c478bd9Sstevel@tonic-gate assert(result >= 0); 1617555afedfScarlsonj if (zonecfg_is_scratch(zeptr->zname) && 1618555afedfScarlsonj (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) { 1619555afedfScarlsonj (void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname, 1620555afedfScarlsonj sizeof (zeptr->zname), NULL, 0); 1621555afedfScarlsonj zonecfg_close_scratch(fp); 1622555afedfScarlsonj } 1623555afedfScarlsonj 1624555afedfScarlsonj if (is_system_labeled()) { 162545916cd2Sjpk (void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot, 162645916cd2Sjpk sizeof (zeptr->zroot)); 16279acbbeafSnn35248 (void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME, 16289acbbeafSnn35248 sizeof (zeptr->zbrand)); 1629555afedfScarlsonj } else { 1630555afedfScarlsonj (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot)); 16319acbbeafSnn35248 (void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand, 16329acbbeafSnn35248 sizeof (zeptr->zbrand)); 163345916cd2Sjpk } 1634555afedfScarlsonj 16357c478bd9Sstevel@tonic-gate zeptr->zstate_str = "running"; 1636555afedfScarlsonj if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK && 1637555afedfScarlsonj !uuid_is_null(uuid)) 1638555afedfScarlsonj uuid_unparse(uuid, zeptr->zuuid); 1639f4b3ec61Sdh155122 1640f4b3ec61Sdh155122 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) { 1641f4b3ec61Sdh155122 zperror2(zeptr->zname, gettext("could not get zone flags")); 1642f4b3ec61Sdh155122 exit(Z_ERR); 1643f4b3ec61Sdh155122 } 1644f4b3ec61Sdh155122 if (flags & ZF_NET_EXCL) 1645f4b3ec61Sdh155122 zeptr->ziptype = ZS_EXCLUSIVE; 1646f4b3ec61Sdh155122 else 1647f4b3ec61Sdh155122 zeptr->ziptype = ZS_SHARED; 16487c478bd9Sstevel@tonic-gate } 16497c478bd9Sstevel@tonic-gate 16507c478bd9Sstevel@tonic-gate static int 16517c478bd9Sstevel@tonic-gate list_func(int argc, char *argv[]) 16527c478bd9Sstevel@tonic-gate { 16537c478bd9Sstevel@tonic-gate zone_entry_t *zentp, zent; 1654108322fbScarlsonj int arg, retv; 16557c478bd9Sstevel@tonic-gate boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE; 16567c478bd9Sstevel@tonic-gate zone_state_t min_state = ZONE_STATE_RUNNING; 16577c478bd9Sstevel@tonic-gate zoneid_t zone_id = getzoneid(); 16587c478bd9Sstevel@tonic-gate 16597c478bd9Sstevel@tonic-gate if (target_zone == NULL) { 16607c478bd9Sstevel@tonic-gate /* all zones: default view to running but allow override */ 16617c478bd9Sstevel@tonic-gate optind = 0; 16627c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?cipv")) != EOF) { 16637c478bd9Sstevel@tonic-gate switch (arg) { 16647c478bd9Sstevel@tonic-gate case '?': 16657c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 16667c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 1667475d78a4Sjonb /* 1668475d78a4Sjonb * The 'i' and 'c' options are not mutually 1669475d78a4Sjonb * exclusive so if 'c' is given, then min_state 1670475d78a4Sjonb * is set to 0 (ZONE_STATE_CONFIGURED) which is 1671475d78a4Sjonb * the lowest possible state. If 'i' is given, 1672475d78a4Sjonb * then min_state is set to be the lowest state 1673475d78a4Sjonb * so far. 1674475d78a4Sjonb */ 16757c478bd9Sstevel@tonic-gate case 'c': 16767c478bd9Sstevel@tonic-gate min_state = ZONE_STATE_CONFIGURED; 16777c478bd9Sstevel@tonic-gate break; 16787c478bd9Sstevel@tonic-gate case 'i': 1679475d78a4Sjonb min_state = min(ZONE_STATE_INSTALLED, 1680475d78a4Sjonb min_state); 1681475d78a4Sjonb 16827c478bd9Sstevel@tonic-gate break; 16837c478bd9Sstevel@tonic-gate case 'p': 16847c478bd9Sstevel@tonic-gate parsable = B_TRUE; 16857c478bd9Sstevel@tonic-gate break; 16867c478bd9Sstevel@tonic-gate case 'v': 16877c478bd9Sstevel@tonic-gate verbose = B_TRUE; 16887c478bd9Sstevel@tonic-gate break; 16897c478bd9Sstevel@tonic-gate default: 16907c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 16917c478bd9Sstevel@tonic-gate return (Z_USAGE); 16927c478bd9Sstevel@tonic-gate } 16937c478bd9Sstevel@tonic-gate } 16947c478bd9Sstevel@tonic-gate if (parsable && verbose) { 16957c478bd9Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 16967c478bd9Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 16977c478bd9Sstevel@tonic-gate return (Z_ERR); 16987c478bd9Sstevel@tonic-gate } 169945916cd2Sjpk if (zone_id == GLOBAL_ZONEID || is_system_labeled()) { 1700108322fbScarlsonj retv = zone_print_list(min_state, verbose, parsable); 17017c478bd9Sstevel@tonic-gate } else { 17027c478bd9Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 17039acbbeafSnn35248 retv = Z_OK; 17047c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 17057c478bd9Sstevel@tonic-gate } 1706108322fbScarlsonj return (retv); 17077c478bd9Sstevel@tonic-gate } 17087c478bd9Sstevel@tonic-gate 17097c478bd9Sstevel@tonic-gate /* 17107c478bd9Sstevel@tonic-gate * Specific target zone: disallow -i/-c suboptions. 17117c478bd9Sstevel@tonic-gate */ 17127c478bd9Sstevel@tonic-gate optind = 0; 17137c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?pv")) != EOF) { 17147c478bd9Sstevel@tonic-gate switch (arg) { 17157c478bd9Sstevel@tonic-gate case '?': 17167c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 17177c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 17187c478bd9Sstevel@tonic-gate case 'p': 17197c478bd9Sstevel@tonic-gate parsable = B_TRUE; 17207c478bd9Sstevel@tonic-gate break; 17217c478bd9Sstevel@tonic-gate case 'v': 17227c478bd9Sstevel@tonic-gate verbose = B_TRUE; 17237c478bd9Sstevel@tonic-gate break; 17247c478bd9Sstevel@tonic-gate default: 17257c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 17267c478bd9Sstevel@tonic-gate return (Z_USAGE); 17277c478bd9Sstevel@tonic-gate } 17287c478bd9Sstevel@tonic-gate } 17297c478bd9Sstevel@tonic-gate if (parsable && verbose) { 17307c478bd9Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 17317c478bd9Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 17327c478bd9Sstevel@tonic-gate return (Z_ERR); 17337c478bd9Sstevel@tonic-gate } 17347c478bd9Sstevel@tonic-gate if (argc > optind) { 17357c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 17367c478bd9Sstevel@tonic-gate return (Z_USAGE); 17377c478bd9Sstevel@tonic-gate } 17387c478bd9Sstevel@tonic-gate if (zone_id != GLOBAL_ZONEID) { 17397c478bd9Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 17407c478bd9Sstevel@tonic-gate /* 17417c478bd9Sstevel@tonic-gate * main() will issue a Z_NO_ZONE error if it cannot get an 17427c478bd9Sstevel@tonic-gate * id for target_zone, which in a non-global zone should 17437c478bd9Sstevel@tonic-gate * happen for any zone name except `zonename`. Thus we 17447c478bd9Sstevel@tonic-gate * assert() that here but don't otherwise check. 17457c478bd9Sstevel@tonic-gate */ 17467c478bd9Sstevel@tonic-gate assert(strcmp(zent.zname, target_zone) == 0); 17477c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 17487c478bd9Sstevel@tonic-gate output = B_TRUE; 17497c478bd9Sstevel@tonic-gate } else if ((zentp = lookup_running_zone(target_zone)) != NULL) { 17507c478bd9Sstevel@tonic-gate zone_print(zentp, verbose, parsable); 17517c478bd9Sstevel@tonic-gate output = B_TRUE; 1752108322fbScarlsonj } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED, 1753108322fbScarlsonj &zent) == Z_OK) { 17547c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 17557c478bd9Sstevel@tonic-gate output = B_TRUE; 17567c478bd9Sstevel@tonic-gate } 1757ce28b40eSzt129084 1758ce28b40eSzt129084 /* 1759ce28b40eSzt129084 * Invoke brand-specific handler. Note that we do this 1760db628066Sgjelinek * only if we're in the global zone, and target_zone is specified 1761db628066Sgjelinek * and it is not the global zone. 1762ce28b40eSzt129084 */ 1763db628066Sgjelinek if (zone_id == GLOBAL_ZONEID && target_zone != NULL && 1764db628066Sgjelinek strcmp(target_zone, GLOBAL_ZONENAME) != 0) 1765ce28b40eSzt129084 if (invoke_brand_handler(CMD_LIST, argv) != Z_OK) 1766ce28b40eSzt129084 return (Z_ERR); 1767ce28b40eSzt129084 17687c478bd9Sstevel@tonic-gate return (output ? Z_OK : Z_ERR); 17697c478bd9Sstevel@tonic-gate } 17707c478bd9Sstevel@tonic-gate 17717c478bd9Sstevel@tonic-gate static void 17727c478bd9Sstevel@tonic-gate sigterm(int sig) 17737c478bd9Sstevel@tonic-gate { 17747c478bd9Sstevel@tonic-gate /* 17757c478bd9Sstevel@tonic-gate * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop, 17767c478bd9Sstevel@tonic-gate * then propagate the signal to our process group. 17777c478bd9Sstevel@tonic-gate */ 17789acbbeafSnn35248 assert(sig == SIGINT || sig == SIGTERM); 17797c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 17807c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_IGN); 17817c478bd9Sstevel@tonic-gate (void) kill(0, sig); 17827c478bd9Sstevel@tonic-gate child_killed = B_TRUE; 17837c478bd9Sstevel@tonic-gate } 17847c478bd9Sstevel@tonic-gate 17857c478bd9Sstevel@tonic-gate static int 17867c478bd9Sstevel@tonic-gate do_subproc(char *cmdbuf) 17877c478bd9Sstevel@tonic-gate { 17887c478bd9Sstevel@tonic-gate char inbuf[1024]; /* arbitrary large amount */ 17897c478bd9Sstevel@tonic-gate FILE *file; 17907c478bd9Sstevel@tonic-gate 17919acbbeafSnn35248 do_subproc_cnt++; 17927c478bd9Sstevel@tonic-gate child_killed = B_FALSE; 17937c478bd9Sstevel@tonic-gate /* 17947c478bd9Sstevel@tonic-gate * We use popen(3c) to launch child processes for [un]install; 17957c478bd9Sstevel@tonic-gate * this library call does not return a PID, so we have to kill 17967c478bd9Sstevel@tonic-gate * the whole process group. To avoid killing our parent, we 17977c478bd9Sstevel@tonic-gate * become a process group leader here. But doing so can wreak 17987c478bd9Sstevel@tonic-gate * havoc with reading from stdin when launched by a non-job-control 17997c478bd9Sstevel@tonic-gate * shell, so we close stdin and reopen it as /dev/null first. 18007c478bd9Sstevel@tonic-gate */ 18017c478bd9Sstevel@tonic-gate (void) close(STDIN_FILENO); 18029acbbeafSnn35248 (void) openat(STDIN_FILENO, "/dev/null", O_RDONLY); 18039acbbeafSnn35248 if (!zoneadm_is_nested) 18047c478bd9Sstevel@tonic-gate (void) setpgid(0, 0); 18057c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, sigterm); 18067c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, sigterm); 18077c478bd9Sstevel@tonic-gate file = popen(cmdbuf, "r"); 18087c478bd9Sstevel@tonic-gate for (;;) { 18097c478bd9Sstevel@tonic-gate if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL) 18107c478bd9Sstevel@tonic-gate break; 18117c478bd9Sstevel@tonic-gate (void) fputs(inbuf, stdout); 18127c478bd9Sstevel@tonic-gate } 18137c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, SIG_DFL); 18147c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_DFL); 18157c478bd9Sstevel@tonic-gate return (pclose(file)); 18167c478bd9Sstevel@tonic-gate } 18177c478bd9Sstevel@tonic-gate 18187c478bd9Sstevel@tonic-gate static int 18199acbbeafSnn35248 do_subproc_interactive(char *cmdbuf) 18209acbbeafSnn35248 { 18219acbbeafSnn35248 void (*saveint)(int); 18229acbbeafSnn35248 void (*saveterm)(int); 18239acbbeafSnn35248 void (*savequit)(int); 18249acbbeafSnn35248 void (*savehup)(int); 18259acbbeafSnn35248 int pid, child, status; 18269acbbeafSnn35248 18279acbbeafSnn35248 /* 18289acbbeafSnn35248 * do_subproc() links stdin to /dev/null, which would break any 18299acbbeafSnn35248 * interactive subprocess we try to launch here. Similarly, we 18309acbbeafSnn35248 * can't have been launched as a subprocess ourselves. 18319acbbeafSnn35248 */ 18329acbbeafSnn35248 assert(do_subproc_cnt == 0 && !zoneadm_is_nested); 18339acbbeafSnn35248 18349acbbeafSnn35248 if ((child = vfork()) == 0) { 18359acbbeafSnn35248 (void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL); 18369acbbeafSnn35248 } 18379acbbeafSnn35248 18389acbbeafSnn35248 if (child == -1) 18399acbbeafSnn35248 return (-1); 18409acbbeafSnn35248 18419acbbeafSnn35248 saveint = sigset(SIGINT, SIG_IGN); 18429acbbeafSnn35248 saveterm = sigset(SIGTERM, SIG_IGN); 18439acbbeafSnn35248 savequit = sigset(SIGQUIT, SIG_IGN); 18449acbbeafSnn35248 savehup = sigset(SIGHUP, SIG_IGN); 18459acbbeafSnn35248 18469acbbeafSnn35248 while ((pid = waitpid(child, &status, 0)) != child && pid != -1) 18479acbbeafSnn35248 ; 18489acbbeafSnn35248 18499acbbeafSnn35248 (void) sigset(SIGINT, saveint); 18509acbbeafSnn35248 (void) sigset(SIGTERM, saveterm); 18519acbbeafSnn35248 (void) sigset(SIGQUIT, savequit); 18529acbbeafSnn35248 (void) sigset(SIGHUP, savehup); 18539acbbeafSnn35248 18549acbbeafSnn35248 return (pid == -1 ? -1 : status); 18559acbbeafSnn35248 } 18569acbbeafSnn35248 18579acbbeafSnn35248 static int 18589acbbeafSnn35248 subproc_status(const char *cmd, int status, boolean_t verbose_failure) 18597c478bd9Sstevel@tonic-gate { 18607c478bd9Sstevel@tonic-gate if (WIFEXITED(status)) { 18617c478bd9Sstevel@tonic-gate int exit_code = WEXITSTATUS(status); 18627c478bd9Sstevel@tonic-gate 18639acbbeafSnn35248 if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK)) 18647c478bd9Sstevel@tonic-gate zerror(gettext("'%s' failed with exit code %d."), cmd, 18657c478bd9Sstevel@tonic-gate exit_code); 18669acbbeafSnn35248 18679acbbeafSnn35248 return (exit_code); 18687c478bd9Sstevel@tonic-gate } else if (WIFSIGNALED(status)) { 18697c478bd9Sstevel@tonic-gate int signal = WTERMSIG(status); 18707c478bd9Sstevel@tonic-gate char sigstr[SIG2STR_MAX]; 18717c478bd9Sstevel@tonic-gate 18727c478bd9Sstevel@tonic-gate if (sig2str(signal, sigstr) == 0) { 18737c478bd9Sstevel@tonic-gate zerror(gettext("'%s' terminated by signal SIG%s."), cmd, 18747c478bd9Sstevel@tonic-gate sigstr); 18757c478bd9Sstevel@tonic-gate } else { 18767c478bd9Sstevel@tonic-gate zerror(gettext("'%s' terminated by an unknown signal."), 18777c478bd9Sstevel@tonic-gate cmd); 18787c478bd9Sstevel@tonic-gate } 18797c478bd9Sstevel@tonic-gate } else { 18807c478bd9Sstevel@tonic-gate zerror(gettext("'%s' failed for unknown reasons."), cmd); 18817c478bd9Sstevel@tonic-gate } 18829acbbeafSnn35248 18839acbbeafSnn35248 /* 18849acbbeafSnn35248 * Assume a subprocess that died due to a signal or an unknown error 18859acbbeafSnn35248 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the 18869acbbeafSnn35248 * user will likely need to do some manual cleanup. 18879acbbeafSnn35248 */ 18889acbbeafSnn35248 return (ZONE_SUBPROC_FATAL); 18897c478bd9Sstevel@tonic-gate } 18907c478bd9Sstevel@tonic-gate 18917c478bd9Sstevel@tonic-gate /* 18927c478bd9Sstevel@tonic-gate * Various sanity checks; make sure: 18937c478bd9Sstevel@tonic-gate * 1. We're in the global zone. 18947c478bd9Sstevel@tonic-gate * 2. The calling user has sufficient privilege. 18957c478bd9Sstevel@tonic-gate * 3. The target zone is neither the global zone nor anything starting with 18967c478bd9Sstevel@tonic-gate * "SUNW". 18977c478bd9Sstevel@tonic-gate * 4a. If we're looking for a 'not running' (i.e., configured or installed) 18987c478bd9Sstevel@tonic-gate * zone, the name service knows about it. 18997c478bd9Sstevel@tonic-gate * 4b. For some operations which expect a zone not to be running, that it is 19007c478bd9Sstevel@tonic-gate * not already running (or ready). 19017c478bd9Sstevel@tonic-gate */ 19027c478bd9Sstevel@tonic-gate static int 19037c478bd9Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running, 19049acbbeafSnn35248 boolean_t unsafe_when_running, boolean_t force) 19057c478bd9Sstevel@tonic-gate { 19067c478bd9Sstevel@tonic-gate zone_entry_t *zent; 19077c478bd9Sstevel@tonic-gate priv_set_t *privset; 19089acbbeafSnn35248 zone_state_t state, min_state; 1909108322fbScarlsonj char kernzone[ZONENAME_MAX]; 1910108322fbScarlsonj FILE *fp; 19117c478bd9Sstevel@tonic-gate 19127c478bd9Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) { 1913ffbafc53Scomay switch (cmd_num) { 1914ffbafc53Scomay case CMD_HALT: 1915ffbafc53Scomay zerror(gettext("use %s to %s this zone."), "halt(1M)", 19167c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 1917ffbafc53Scomay break; 1918ffbafc53Scomay case CMD_REBOOT: 1919ffbafc53Scomay zerror(gettext("use %s to %s this zone."), 1920ffbafc53Scomay "reboot(1M)", cmd_to_str(cmd_num)); 1921ffbafc53Scomay break; 1922ffbafc53Scomay default: 1923ffbafc53Scomay zerror(gettext("must be in the global zone to %s a " 1924ffbafc53Scomay "zone."), cmd_to_str(cmd_num)); 1925ffbafc53Scomay break; 1926ffbafc53Scomay } 19277c478bd9Sstevel@tonic-gate return (Z_ERR); 19287c478bd9Sstevel@tonic-gate } 19297c478bd9Sstevel@tonic-gate 19307c478bd9Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 19317c478bd9Sstevel@tonic-gate zerror(gettext("%s failed"), "priv_allocset"); 19327c478bd9Sstevel@tonic-gate return (Z_ERR); 19337c478bd9Sstevel@tonic-gate } 19347c478bd9Sstevel@tonic-gate 19357c478bd9Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 19367c478bd9Sstevel@tonic-gate zerror(gettext("%s failed"), "getppriv"); 19377c478bd9Sstevel@tonic-gate priv_freeset(privset); 19387c478bd9Sstevel@tonic-gate return (Z_ERR); 19397c478bd9Sstevel@tonic-gate } 19407c478bd9Sstevel@tonic-gate 19417c478bd9Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 19427c478bd9Sstevel@tonic-gate zerror(gettext("only a privileged user may %s a zone."), 19437c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 19447c478bd9Sstevel@tonic-gate priv_freeset(privset); 19457c478bd9Sstevel@tonic-gate return (Z_ERR); 19467c478bd9Sstevel@tonic-gate } 19477c478bd9Sstevel@tonic-gate priv_freeset(privset); 19487c478bd9Sstevel@tonic-gate 19497c478bd9Sstevel@tonic-gate if (zone == NULL) { 19507c478bd9Sstevel@tonic-gate zerror(gettext("no zone specified")); 19517c478bd9Sstevel@tonic-gate return (Z_ERR); 19527c478bd9Sstevel@tonic-gate } 19537c478bd9Sstevel@tonic-gate 19547c478bd9Sstevel@tonic-gate if (strcmp(zone, GLOBAL_ZONENAME) == 0) { 19557c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for the global zone."), 19567c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 19577c478bd9Sstevel@tonic-gate return (Z_ERR); 19587c478bd9Sstevel@tonic-gate } 19597c478bd9Sstevel@tonic-gate 19607c478bd9Sstevel@tonic-gate if (strncmp(zone, "SUNW", 4) == 0) { 19617c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for zones starting " 19627c478bd9Sstevel@tonic-gate "with SUNW."), cmd_to_str(cmd_num)); 19637c478bd9Sstevel@tonic-gate return (Z_ERR); 19647c478bd9Sstevel@tonic-gate } 19657c478bd9Sstevel@tonic-gate 19669acbbeafSnn35248 if (!is_native_zone && cmd_num == CMD_MOUNT) { 19679acbbeafSnn35248 zerror(gettext("%s operation is invalid for branded zones."), 19689acbbeafSnn35248 cmd_to_str(cmd_num)); 19699acbbeafSnn35248 return (Z_ERR); 19709acbbeafSnn35248 } 19719acbbeafSnn35248 1972108322fbScarlsonj if (!zonecfg_in_alt_root()) { 1973108322fbScarlsonj zent = lookup_running_zone(zone); 1974108322fbScarlsonj } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 1975108322fbScarlsonj zent = NULL; 1976108322fbScarlsonj } else { 1977108322fbScarlsonj if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(), 1978108322fbScarlsonj kernzone, sizeof (kernzone)) == 0) 1979108322fbScarlsonj zent = lookup_running_zone(kernzone); 1980108322fbScarlsonj else 1981108322fbScarlsonj zent = NULL; 1982108322fbScarlsonj zonecfg_close_scratch(fp); 1983108322fbScarlsonj } 1984108322fbScarlsonj 19857c478bd9Sstevel@tonic-gate /* 19867c478bd9Sstevel@tonic-gate * Look up from the kernel for 'running' zones. 19877c478bd9Sstevel@tonic-gate */ 19889acbbeafSnn35248 if (running && !force) { 19897c478bd9Sstevel@tonic-gate if (zent == NULL) { 19907c478bd9Sstevel@tonic-gate zerror(gettext("not running")); 19917c478bd9Sstevel@tonic-gate return (Z_ERR); 19927c478bd9Sstevel@tonic-gate } 19937c478bd9Sstevel@tonic-gate } else { 19947c478bd9Sstevel@tonic-gate int err; 19957c478bd9Sstevel@tonic-gate 19967c478bd9Sstevel@tonic-gate if (unsafe_when_running && zent != NULL) { 19977c478bd9Sstevel@tonic-gate /* check whether the zone is ready or running */ 19987c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, 19997c478bd9Sstevel@tonic-gate &zent->zstate_num)) != Z_OK) { 20007c478bd9Sstevel@tonic-gate errno = err; 20017c478bd9Sstevel@tonic-gate zperror2(zent->zname, 20027c478bd9Sstevel@tonic-gate gettext("could not get state")); 20037c478bd9Sstevel@tonic-gate /* can't tell, so hedge */ 20047c478bd9Sstevel@tonic-gate zent->zstate_str = "ready/running"; 20057c478bd9Sstevel@tonic-gate } else { 20067c478bd9Sstevel@tonic-gate zent->zstate_str = 20077c478bd9Sstevel@tonic-gate zone_state_str(zent->zstate_num); 20087c478bd9Sstevel@tonic-gate } 20097c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for %s zones."), 20107c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num), zent->zstate_str); 20117c478bd9Sstevel@tonic-gate return (Z_ERR); 20127c478bd9Sstevel@tonic-gate } 20137c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zone, &state)) != Z_OK) { 20147c478bd9Sstevel@tonic-gate errno = err; 20157c478bd9Sstevel@tonic-gate zperror2(zone, gettext("could not get state")); 20167c478bd9Sstevel@tonic-gate return (Z_ERR); 20177c478bd9Sstevel@tonic-gate } 20187c478bd9Sstevel@tonic-gate switch (cmd_num) { 20197c478bd9Sstevel@tonic-gate case CMD_UNINSTALL: 20207c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 20217c478bd9Sstevel@tonic-gate zerror(gettext("is already in state '%s'."), 20227c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_CONFIGURED)); 20237c478bd9Sstevel@tonic-gate return (Z_ERR); 20247c478bd9Sstevel@tonic-gate } 20257c478bd9Sstevel@tonic-gate break; 2026ee519a1fSgjelinek case CMD_ATTACH: 2027865e09a4Sgjelinek case CMD_CLONE: 20287c478bd9Sstevel@tonic-gate case CMD_INSTALL: 20297c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_INSTALLED) { 20307c478bd9Sstevel@tonic-gate zerror(gettext("is already %s."), 20317c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED)); 20327c478bd9Sstevel@tonic-gate return (Z_ERR); 20337c478bd9Sstevel@tonic-gate } else if (state == ZONE_STATE_INCOMPLETE) { 20347c478bd9Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 20357c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 20367c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 20377c478bd9Sstevel@tonic-gate return (Z_ERR); 20387c478bd9Sstevel@tonic-gate } 20397c478bd9Sstevel@tonic-gate break; 2040ee519a1fSgjelinek case CMD_DETACH: 2041865e09a4Sgjelinek case CMD_MOVE: 20427c478bd9Sstevel@tonic-gate case CMD_READY: 20437c478bd9Sstevel@tonic-gate case CMD_BOOT: 2044108322fbScarlsonj case CMD_MOUNT: 2045555afedfScarlsonj case CMD_MARK: 20469acbbeafSnn35248 if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) && 20479acbbeafSnn35248 force) 20489acbbeafSnn35248 min_state = ZONE_STATE_INCOMPLETE; 20499acbbeafSnn35248 else 20509acbbeafSnn35248 min_state = ZONE_STATE_INSTALLED; 20519acbbeafSnn35248 20529acbbeafSnn35248 if (force && cmd_num == CMD_BOOT && is_native_zone) { 20539acbbeafSnn35248 zerror(gettext("Only branded zones may be " 20549acbbeafSnn35248 "force-booted.")); 20559acbbeafSnn35248 return (Z_ERR); 20569acbbeafSnn35248 } 20579acbbeafSnn35248 20589acbbeafSnn35248 if (state < min_state) { 20597c478bd9Sstevel@tonic-gate zerror(gettext("must be %s before %s."), 20609acbbeafSnn35248 zone_state_str(min_state), 20617c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 20627c478bd9Sstevel@tonic-gate return (Z_ERR); 20637c478bd9Sstevel@tonic-gate } 20647c478bd9Sstevel@tonic-gate break; 20657c478bd9Sstevel@tonic-gate case CMD_VERIFY: 20667c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_INCOMPLETE) { 20677c478bd9Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 20687c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 20697c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 20707c478bd9Sstevel@tonic-gate return (Z_ERR); 20717c478bd9Sstevel@tonic-gate } 20727c478bd9Sstevel@tonic-gate break; 2073108322fbScarlsonj case CMD_UNMOUNT: 2074108322fbScarlsonj if (state != ZONE_STATE_MOUNTED) { 2075108322fbScarlsonj zerror(gettext("must be %s before %s."), 2076108322fbScarlsonj zone_state_str(ZONE_STATE_MOUNTED), 2077108322fbScarlsonj cmd_to_str(cmd_num)); 2078108322fbScarlsonj return (Z_ERR); 2079108322fbScarlsonj } 2080108322fbScarlsonj break; 20817c478bd9Sstevel@tonic-gate } 20827c478bd9Sstevel@tonic-gate } 20837c478bd9Sstevel@tonic-gate return (Z_OK); 20847c478bd9Sstevel@tonic-gate } 20857c478bd9Sstevel@tonic-gate 20867c478bd9Sstevel@tonic-gate static int 20877c478bd9Sstevel@tonic-gate halt_func(int argc, char *argv[]) 20887c478bd9Sstevel@tonic-gate { 20897c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 20907c478bd9Sstevel@tonic-gate int arg; 20917c478bd9Sstevel@tonic-gate 2092108322fbScarlsonj if (zonecfg_in_alt_root()) { 2093108322fbScarlsonj zerror(gettext("cannot halt zone in alternate root")); 2094108322fbScarlsonj return (Z_ERR); 2095108322fbScarlsonj } 2096108322fbScarlsonj 20977c478bd9Sstevel@tonic-gate optind = 0; 20987c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 20997c478bd9Sstevel@tonic-gate switch (arg) { 21007c478bd9Sstevel@tonic-gate case '?': 21017c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 21027c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 21037c478bd9Sstevel@tonic-gate default: 21047c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 21057c478bd9Sstevel@tonic-gate return (Z_USAGE); 21067c478bd9Sstevel@tonic-gate } 21077c478bd9Sstevel@tonic-gate } 21087c478bd9Sstevel@tonic-gate if (argc > optind) { 21097c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 21107c478bd9Sstevel@tonic-gate return (Z_USAGE); 21117c478bd9Sstevel@tonic-gate } 21127c478bd9Sstevel@tonic-gate /* 21137c478bd9Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 21147c478bd9Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 21157c478bd9Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 21167c478bd9Sstevel@tonic-gate */ 21179acbbeafSnn35248 if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE) 21189acbbeafSnn35248 != Z_OK) 21197c478bd9Sstevel@tonic-gate return (Z_ERR); 21207c478bd9Sstevel@tonic-gate 2121ce28b40eSzt129084 /* 2122ce28b40eSzt129084 * Invoke brand-specific handler. 2123ce28b40eSzt129084 */ 2124ce28b40eSzt129084 if (invoke_brand_handler(CMD_HALT, argv) != Z_OK) 2125ce28b40eSzt129084 return (Z_ERR); 2126ce28b40eSzt129084 21277c478bd9Sstevel@tonic-gate zarg.cmd = Z_HALT; 21287c478bd9Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 21297c478bd9Sstevel@tonic-gate } 21307c478bd9Sstevel@tonic-gate 21317c478bd9Sstevel@tonic-gate static int 21327c478bd9Sstevel@tonic-gate reboot_func(int argc, char *argv[]) 21337c478bd9Sstevel@tonic-gate { 21347c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 21357c478bd9Sstevel@tonic-gate int arg; 21367c478bd9Sstevel@tonic-gate 2137108322fbScarlsonj if (zonecfg_in_alt_root()) { 2138108322fbScarlsonj zerror(gettext("cannot reboot zone in alternate root")); 2139108322fbScarlsonj return (Z_ERR); 2140108322fbScarlsonj } 2141108322fbScarlsonj 21427c478bd9Sstevel@tonic-gate optind = 0; 21437c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 21447c478bd9Sstevel@tonic-gate switch (arg) { 21457c478bd9Sstevel@tonic-gate case '?': 21467c478bd9Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 21477c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 21487c478bd9Sstevel@tonic-gate default: 21497c478bd9Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 21507c478bd9Sstevel@tonic-gate return (Z_USAGE); 21517c478bd9Sstevel@tonic-gate } 21527c478bd9Sstevel@tonic-gate } 21533f2f09c1Sdp 21543f2f09c1Sdp zarg.bootbuf[0] = '\0'; 21553f2f09c1Sdp for (; optind < argc; optind++) { 21563f2f09c1Sdp if (strlcat(zarg.bootbuf, argv[optind], 21573f2f09c1Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 21583f2f09c1Sdp zerror(gettext("Boot argument list too long")); 21593f2f09c1Sdp return (Z_ERR); 21607c478bd9Sstevel@tonic-gate } 21613f2f09c1Sdp if (optind < argc - 1) 21623f2f09c1Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 21633f2f09c1Sdp sizeof (zarg.bootbuf)) { 21643f2f09c1Sdp zerror(gettext("Boot argument list too long")); 21653f2f09c1Sdp return (Z_ERR); 21663f2f09c1Sdp } 21673f2f09c1Sdp } 21683f2f09c1Sdp 21693f2f09c1Sdp 21707c478bd9Sstevel@tonic-gate /* 21717c478bd9Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 21727c478bd9Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 21737c478bd9Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 21747c478bd9Sstevel@tonic-gate */ 21759acbbeafSnn35248 if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE) 21769acbbeafSnn35248 != Z_OK) 21777c478bd9Sstevel@tonic-gate return (Z_ERR); 2178ce28b40eSzt129084 if (verify_details(CMD_REBOOT, argv) != Z_OK) 2179b5abaf04Sgjelinek return (Z_ERR); 21807c478bd9Sstevel@tonic-gate 21817c478bd9Sstevel@tonic-gate zarg.cmd = Z_REBOOT; 21827c478bd9Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 21837c478bd9Sstevel@tonic-gate } 21847c478bd9Sstevel@tonic-gate 21857c478bd9Sstevel@tonic-gate static int 2186ce28b40eSzt129084 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[]) 21879acbbeafSnn35248 { 21889acbbeafSnn35248 char cmdbuf[MAXPATHLEN]; 21899acbbeafSnn35248 int err; 21909acbbeafSnn35248 char zonepath[MAXPATHLEN]; 2191123807fbSedp brand_handle_t bh = NULL; 2192ce28b40eSzt129084 int status, i; 21939acbbeafSnn35248 21949acbbeafSnn35248 /* 21959acbbeafSnn35248 * Fetch the verify command from the brand configuration. 21969acbbeafSnn35248 * "exec" the command so that the returned status is that of 21979acbbeafSnn35248 * the command and not the shell. 21989acbbeafSnn35248 */ 21999acbbeafSnn35248 if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 22009acbbeafSnn35248 Z_OK) { 22019acbbeafSnn35248 errno = err; 2202ce28b40eSzt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 22039acbbeafSnn35248 return (Z_ERR); 22049acbbeafSnn35248 } 2205123807fbSedp if ((bh = brand_open(target_brand)) == NULL) { 22069acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 22079acbbeafSnn35248 return (Z_ERR); 22089acbbeafSnn35248 } 22099acbbeafSnn35248 22109acbbeafSnn35248 /* 22119acbbeafSnn35248 * If the brand has its own verification routine, execute it now. 2212ce28b40eSzt129084 * The verification routine validates the intended zoneadm 2213ce28b40eSzt129084 * operation for the specific brand. The zoneadm subcommand and 2214ce28b40eSzt129084 * all its arguments are passed to the routine. 22159acbbeafSnn35248 */ 22169acbbeafSnn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 2217123807fbSedp err = brand_get_verify_adm(bh, target_zone, zonepath, 22189acbbeafSnn35248 cmdbuf + EXEC_LEN, sizeof (cmdbuf) - EXEC_LEN, 0, NULL); 2219123807fbSedp brand_close(bh); 2220ce28b40eSzt129084 if (err != 0) 2221ce28b40eSzt129084 return (Z_BRAND_ERROR); 2222ce28b40eSzt129084 if (strlen(cmdbuf) <= EXEC_LEN) 2223ce28b40eSzt129084 return (Z_OK); 2224ce28b40eSzt129084 2225ce28b40eSzt129084 if (strlcat(cmdbuf, cmd_to_str(cmd_num), 2226ce28b40eSzt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf)) 2227ce28b40eSzt129084 return (Z_ERR); 2228ce28b40eSzt129084 2229ce28b40eSzt129084 /* Build the argv string */ 2230ce28b40eSzt129084 i = 0; 2231ce28b40eSzt129084 while (argv[i] != NULL) { 2232ce28b40eSzt129084 if ((strlcat(cmdbuf, " ", 2233ce28b40eSzt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf)) || 2234ce28b40eSzt129084 (strlcat(cmdbuf, argv[i++], 2235ce28b40eSzt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf))) 2236ce28b40eSzt129084 return (Z_ERR); 2237ce28b40eSzt129084 } 2238ce28b40eSzt129084 2239*d9e728a2Sgjelinek if (zoneadm_is_nested) 2240*d9e728a2Sgjelinek status = do_subproc(cmdbuf); 2241*d9e728a2Sgjelinek else 22429acbbeafSnn35248 status = do_subproc_interactive(cmdbuf); 22439acbbeafSnn35248 err = subproc_status(gettext("brand-specific verification"), 22449acbbeafSnn35248 status, B_FALSE); 22459acbbeafSnn35248 2246ce28b40eSzt129084 return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR); 22479acbbeafSnn35248 } 22489acbbeafSnn35248 22499acbbeafSnn35248 static int 22507c478bd9Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle) 22517c478bd9Sstevel@tonic-gate { 22527c478bd9Sstevel@tonic-gate struct zone_rctltab rctltab; 22537c478bd9Sstevel@tonic-gate size_t rbs = rctlblk_size(); 22547c478bd9Sstevel@tonic-gate rctlblk_t *rctlblk; 22557c478bd9Sstevel@tonic-gate int error = Z_INVAL; 22567c478bd9Sstevel@tonic-gate 22577c478bd9Sstevel@tonic-gate if ((rctlblk = malloc(rbs)) == NULL) { 22587c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rbs, 22597c478bd9Sstevel@tonic-gate strerror(errno)); 22607c478bd9Sstevel@tonic-gate return (Z_NOMEM); 22617c478bd9Sstevel@tonic-gate } 22627c478bd9Sstevel@tonic-gate 22637c478bd9Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 22647c478bd9Sstevel@tonic-gate zerror(gettext("zonecfg_setrctlent failed")); 22657c478bd9Sstevel@tonic-gate free(rctlblk); 22667c478bd9Sstevel@tonic-gate return (error); 22677c478bd9Sstevel@tonic-gate } 22687c478bd9Sstevel@tonic-gate 22697c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 22707c478bd9Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 22717c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 22727c478bd9Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 22737c478bd9Sstevel@tonic-gate 22747c478bd9Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 22757c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: Ignoring unrecognized rctl " 22767c478bd9Sstevel@tonic-gate "'%s'."), name); 22777c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 22787c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 22797c478bd9Sstevel@tonic-gate continue; 22807c478bd9Sstevel@tonic-gate } 22817c478bd9Sstevel@tonic-gate 22827c478bd9Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 22837c478bd9Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 22847c478bd9Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 22857c478bd9Sstevel@tonic-gate != Z_OK) { 22867c478bd9Sstevel@tonic-gate zerror(gettext("invalid rctl value: " 22877c478bd9Sstevel@tonic-gate "(priv=%s,limit=%s,action%s)"), 22887c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 22897c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 22907c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action); 22917c478bd9Sstevel@tonic-gate goto out; 22927c478bd9Sstevel@tonic-gate } 22937c478bd9Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 22947c478bd9Sstevel@tonic-gate zerror(gettext("(priv=%s,limit=%s,action=%s) " 22957c478bd9Sstevel@tonic-gate "is not a valid value for rctl '%s'"), 22967c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 22977c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 22987c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action, 22997c478bd9Sstevel@tonic-gate name); 23007c478bd9Sstevel@tonic-gate goto out; 23017c478bd9Sstevel@tonic-gate } 23027c478bd9Sstevel@tonic-gate } 23037c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 23047c478bd9Sstevel@tonic-gate } 23057c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 23067c478bd9Sstevel@tonic-gate error = Z_OK; 23077c478bd9Sstevel@tonic-gate out: 23087c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 23097c478bd9Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 23107c478bd9Sstevel@tonic-gate free(rctlblk); 23117c478bd9Sstevel@tonic-gate return (error); 23127c478bd9Sstevel@tonic-gate } 23137c478bd9Sstevel@tonic-gate 23147c478bd9Sstevel@tonic-gate static int 23157c478bd9Sstevel@tonic-gate verify_pool(zone_dochandle_t handle) 23167c478bd9Sstevel@tonic-gate { 23177c478bd9Sstevel@tonic-gate char poolname[MAXPATHLEN]; 23187c478bd9Sstevel@tonic-gate pool_conf_t *poolconf; 23197c478bd9Sstevel@tonic-gate pool_t *pool; 23207c478bd9Sstevel@tonic-gate int status; 23217c478bd9Sstevel@tonic-gate int error; 23227c478bd9Sstevel@tonic-gate 23237c478bd9Sstevel@tonic-gate /* 23247c478bd9Sstevel@tonic-gate * This ends up being very similar to the check done in zoneadmd. 23257c478bd9Sstevel@tonic-gate */ 23267c478bd9Sstevel@tonic-gate error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 23277c478bd9Sstevel@tonic-gate if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 23287c478bd9Sstevel@tonic-gate /* 23297c478bd9Sstevel@tonic-gate * No pool specified. 23307c478bd9Sstevel@tonic-gate */ 23317c478bd9Sstevel@tonic-gate return (0); 23327c478bd9Sstevel@tonic-gate } 23337c478bd9Sstevel@tonic-gate if (error != Z_OK) { 23347c478bd9Sstevel@tonic-gate zperror(gettext("Unable to retrieve pool name from " 23357c478bd9Sstevel@tonic-gate "configuration"), B_TRUE); 23367c478bd9Sstevel@tonic-gate return (error); 23377c478bd9Sstevel@tonic-gate } 23387c478bd9Sstevel@tonic-gate /* 23397c478bd9Sstevel@tonic-gate * Don't do anything if pools aren't enabled. 23407c478bd9Sstevel@tonic-gate */ 23417c478bd9Sstevel@tonic-gate if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 23427c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pools facility not active; " 23437c478bd9Sstevel@tonic-gate "zone will not be bound to pool '%s'."), poolname); 23447c478bd9Sstevel@tonic-gate return (Z_OK); 23457c478bd9Sstevel@tonic-gate } 23467c478bd9Sstevel@tonic-gate /* 23477c478bd9Sstevel@tonic-gate * Try to provide a sane error message if the requested pool doesn't 23487c478bd9Sstevel@tonic-gate * exist. It isn't clear that pools-related failures should 23497c478bd9Sstevel@tonic-gate * necessarily translate to a failure to verify the zone configuration, 23507c478bd9Sstevel@tonic-gate * hence they are not considered errors. 23517c478bd9Sstevel@tonic-gate */ 23527c478bd9Sstevel@tonic-gate if ((poolconf = pool_conf_alloc()) == NULL) { 23537c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_alloc failed; " 23547c478bd9Sstevel@tonic-gate "using default pool")); 23557c478bd9Sstevel@tonic-gate return (Z_OK); 23567c478bd9Sstevel@tonic-gate } 23577c478bd9Sstevel@tonic-gate if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 23587c478bd9Sstevel@tonic-gate PO_SUCCESS) { 23597c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_open failed; " 23607c478bd9Sstevel@tonic-gate "using default pool")); 23617c478bd9Sstevel@tonic-gate pool_conf_free(poolconf); 23627c478bd9Sstevel@tonic-gate return (Z_OK); 23637c478bd9Sstevel@tonic-gate } 23647c478bd9Sstevel@tonic-gate pool = pool_get_pool(poolconf, poolname); 23657c478bd9Sstevel@tonic-gate (void) pool_conf_close(poolconf); 23667c478bd9Sstevel@tonic-gate pool_conf_free(poolconf); 23677c478bd9Sstevel@tonic-gate if (pool == NULL) { 23687c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool '%s' not found. " 23697c478bd9Sstevel@tonic-gate "using default pool"), poolname); 23707c478bd9Sstevel@tonic-gate } 23717c478bd9Sstevel@tonic-gate 23727c478bd9Sstevel@tonic-gate return (Z_OK); 23737c478bd9Sstevel@tonic-gate } 23747c478bd9Sstevel@tonic-gate 23757c478bd9Sstevel@tonic-gate static int 2376b5abaf04Sgjelinek verify_ipd(zone_dochandle_t handle) 2377b5abaf04Sgjelinek { 2378b5abaf04Sgjelinek int return_code = Z_OK; 2379b5abaf04Sgjelinek struct zone_fstab fstab; 2380b5abaf04Sgjelinek struct stat st; 2381b5abaf04Sgjelinek char specdir[MAXPATHLEN]; 2382b5abaf04Sgjelinek 2383b5abaf04Sgjelinek if (zonecfg_setipdent(handle) != Z_OK) { 23845ee84fbdSgjelinek /* 23855ee84fbdSgjelinek * TRANSLATION_NOTE 23865ee84fbdSgjelinek * inherit-pkg-dirs is a literal that should not be translated. 23875ee84fbdSgjelinek */ 23885ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify " 2389b5abaf04Sgjelinek "inherit-pkg-dirs: unable to enumerate mounts\n")); 2390b5abaf04Sgjelinek return (Z_ERR); 2391b5abaf04Sgjelinek } 2392b5abaf04Sgjelinek while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 2393b5abaf04Sgjelinek /* 2394b5abaf04Sgjelinek * Verify fs_dir exists. 2395b5abaf04Sgjelinek */ 2396b5abaf04Sgjelinek (void) snprintf(specdir, sizeof (specdir), "%s%s", 2397b5abaf04Sgjelinek zonecfg_get_root(), fstab.zone_fs_dir); 2398b5abaf04Sgjelinek if (stat(specdir, &st) != 0) { 23995ee84fbdSgjelinek /* 24005ee84fbdSgjelinek * TRANSLATION_NOTE 24015ee84fbdSgjelinek * inherit-pkg-dir is a literal that should not be 24025ee84fbdSgjelinek * translated. 24035ee84fbdSgjelinek */ 24045ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify " 2405b5abaf04Sgjelinek "inherit-pkg-dir %s: %s\n"), 2406b5abaf04Sgjelinek fstab.zone_fs_dir, strerror(errno)); 2407b5abaf04Sgjelinek return_code = Z_ERR; 2408b5abaf04Sgjelinek } 2409b5abaf04Sgjelinek if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 24105ee84fbdSgjelinek /* 24115ee84fbdSgjelinek * TRANSLATION_NOTE 24125ee84fbdSgjelinek * inherit-pkg-dir and NFS are literals that should 24135ee84fbdSgjelinek * not be translated. 24145ee84fbdSgjelinek */ 2415b5abaf04Sgjelinek (void) fprintf(stderr, gettext("cannot verify " 24160b5de56dSgjelinek "inherit-pkg-dir %s: NFS mounted file system.\n" 24170b5de56dSgjelinek "\tA local file system must be used.\n"), 2418b5abaf04Sgjelinek fstab.zone_fs_dir); 2419b5abaf04Sgjelinek return_code = Z_ERR; 2420b5abaf04Sgjelinek } 2421b5abaf04Sgjelinek } 2422b5abaf04Sgjelinek (void) zonecfg_endipdent(handle); 2423b5abaf04Sgjelinek 2424b5abaf04Sgjelinek return (return_code); 2425b5abaf04Sgjelinek } 2426b5abaf04Sgjelinek 242720c8013fSlling /* 242820c8013fSlling * Verify that the special device/file system exists and is valid. 242920c8013fSlling */ 243020c8013fSlling static int 243120c8013fSlling verify_fs_special(struct zone_fstab *fstab) 243220c8013fSlling { 243320c8013fSlling struct stat st; 243420c8013fSlling 24356cc813faSgjelinek /* 24366cc813faSgjelinek * This validation is really intended for standard zone administration. 24376cc813faSgjelinek * If we are in a mini-root or some other upgrade situation where 24386cc813faSgjelinek * we are using the scratch zone, just by-pass this. 24396cc813faSgjelinek */ 24406cc813faSgjelinek if (zonecfg_in_alt_root()) 24416cc813faSgjelinek return (Z_OK); 24426cc813faSgjelinek 244320c8013fSlling if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0) 244420c8013fSlling return (verify_fs_zfs(fstab)); 244520c8013fSlling 244620c8013fSlling if (stat(fstab->zone_fs_special, &st) != 0) { 24475c358068Slling (void) fprintf(stderr, gettext("could not verify fs " 244820c8013fSlling "%s: could not access %s: %s\n"), fstab->zone_fs_dir, 244920c8013fSlling fstab->zone_fs_special, strerror(errno)); 245020c8013fSlling return (Z_ERR); 245120c8013fSlling } 245220c8013fSlling 245320c8013fSlling if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 245420c8013fSlling /* 245520c8013fSlling * TRANSLATION_NOTE 245620c8013fSlling * fs and NFS are literals that should 245720c8013fSlling * not be translated. 245820c8013fSlling */ 245920c8013fSlling (void) fprintf(stderr, gettext("cannot verify " 24600b5de56dSgjelinek "fs %s: NFS mounted file system.\n" 24610b5de56dSgjelinek "\tA local file system must be used.\n"), 246220c8013fSlling fstab->zone_fs_special); 246320c8013fSlling return (Z_ERR); 246420c8013fSlling } 246520c8013fSlling 246620c8013fSlling return (Z_OK); 246720c8013fSlling } 246820c8013fSlling 2469b5abaf04Sgjelinek static int 24707c478bd9Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle) 24717c478bd9Sstevel@tonic-gate { 24727c478bd9Sstevel@tonic-gate int return_code = Z_OK; 24737c478bd9Sstevel@tonic-gate struct zone_fstab fstab; 24747c478bd9Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 24757c478bd9Sstevel@tonic-gate struct stat st; 24767c478bd9Sstevel@tonic-gate 24777c478bd9Sstevel@tonic-gate /* 24787c478bd9Sstevel@tonic-gate * No need to verify inherit-pkg-dir fs types, as their type is 24797c478bd9Sstevel@tonic-gate * implicitly lofs, which is known. Therefore, the types are only 24807c478bd9Sstevel@tonic-gate * verified for regular file systems below. 24817c478bd9Sstevel@tonic-gate * 24827c478bd9Sstevel@tonic-gate * Since the actual mount point is not known until the dependent mounts 24837c478bd9Sstevel@tonic-gate * are performed, we don't attempt any path validation here: that will 24847c478bd9Sstevel@tonic-gate * happen later when zoneadmd actually does the mounts. 24857c478bd9Sstevel@tonic-gate */ 24867c478bd9Sstevel@tonic-gate if (zonecfg_setfsent(handle) != Z_OK) { 24870b5de56dSgjelinek (void) fprintf(stderr, gettext("could not verify file systems: " 24887c478bd9Sstevel@tonic-gate "unable to enumerate mounts\n")); 24897c478bd9Sstevel@tonic-gate return (Z_ERR); 24907c478bd9Sstevel@tonic-gate } 24917c478bd9Sstevel@tonic-gate while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 24927c478bd9Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) { 24937c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 24947c478bd9Sstevel@tonic-gate "type %s is not allowed.\n"), fstab.zone_fs_dir, 24957c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 24967c478bd9Sstevel@tonic-gate return_code = Z_ERR; 24977c478bd9Sstevel@tonic-gate goto next_fs; 24987c478bd9Sstevel@tonic-gate } 24997c478bd9Sstevel@tonic-gate /* 25007c478bd9Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/mount exists. 25017c478bd9Sstevel@tonic-gate */ 25027c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", 25037c478bd9Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 25047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 25057c478bd9Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 25067c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 25077c478bd9Sstevel@tonic-gate return_code = Z_ERR; 25087c478bd9Sstevel@tonic-gate goto next_fs; 25097c478bd9Sstevel@tonic-gate } 25107c478bd9Sstevel@tonic-gate if (stat(cmdbuf, &st) != 0) { 25115ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 25125ee84fbdSgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 25137c478bd9Sstevel@tonic-gate cmdbuf, strerror(errno)); 25147c478bd9Sstevel@tonic-gate return_code = Z_ERR; 25157c478bd9Sstevel@tonic-gate goto next_fs; 25167c478bd9Sstevel@tonic-gate } 25177c478bd9Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 25185ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 25195ee84fbdSgjelinek "%s: %s is not a regular file\n"), 25205ee84fbdSgjelinek fstab.zone_fs_dir, cmdbuf); 25217c478bd9Sstevel@tonic-gate return_code = Z_ERR; 25227c478bd9Sstevel@tonic-gate goto next_fs; 25237c478bd9Sstevel@tonic-gate } 25247c478bd9Sstevel@tonic-gate /* 25257c478bd9Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is 25267c478bd9Sstevel@tonic-gate * set. 25277c478bd9Sstevel@tonic-gate */ 25287c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", 25297c478bd9Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 25307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 25317c478bd9Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 25327c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 25337c478bd9Sstevel@tonic-gate return_code = Z_ERR; 25347c478bd9Sstevel@tonic-gate goto next_fs; 25357c478bd9Sstevel@tonic-gate } 25367c478bd9Sstevel@tonic-gate if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) { 25375ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 25385ee84fbdSgjelinek "%s: must specify 'raw' device for %s " 25390b5de56dSgjelinek "file systems\n"), 25407c478bd9Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 25417c478bd9Sstevel@tonic-gate return_code = Z_ERR; 25427c478bd9Sstevel@tonic-gate goto next_fs; 25437c478bd9Sstevel@tonic-gate } 25447c478bd9Sstevel@tonic-gate if (fstab.zone_fs_raw[0] != '\0' && 25457c478bd9Sstevel@tonic-gate (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) { 25467c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 25477c478bd9Sstevel@tonic-gate "'raw' device specified but " 25487c478bd9Sstevel@tonic-gate "no fsck executable exists for %s\n"), 25497c478bd9Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 25507c478bd9Sstevel@tonic-gate return_code = Z_ERR; 25517c478bd9Sstevel@tonic-gate goto next_fs; 25527c478bd9Sstevel@tonic-gate } 255320c8013fSlling 255420c8013fSlling /* Verify fs_special. */ 255520c8013fSlling if ((return_code = verify_fs_special(&fstab)) != Z_OK) 2556b5abaf04Sgjelinek goto next_fs; 255720c8013fSlling 255820c8013fSlling /* Verify fs_raw. */ 2559b5abaf04Sgjelinek if (fstab.zone_fs_raw[0] != '\0' && 2560b5abaf04Sgjelinek stat(fstab.zone_fs_raw, &st) != 0) { 25615ee84fbdSgjelinek /* 25625ee84fbdSgjelinek * TRANSLATION_NOTE 25635ee84fbdSgjelinek * fs is a literal that should not be translated. 25645ee84fbdSgjelinek */ 25655ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 25665ee84fbdSgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 2567b5abaf04Sgjelinek fstab.zone_fs_raw, strerror(errno)); 2568b5abaf04Sgjelinek return_code = Z_ERR; 2569b5abaf04Sgjelinek goto next_fs; 2570b5abaf04Sgjelinek } 25717c478bd9Sstevel@tonic-gate next_fs: 25727c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(fstab.zone_fs_options); 25737c478bd9Sstevel@tonic-gate } 25747c478bd9Sstevel@tonic-gate (void) zonecfg_endfsent(handle); 25757c478bd9Sstevel@tonic-gate 25767c478bd9Sstevel@tonic-gate return (return_code); 25777c478bd9Sstevel@tonic-gate } 25787c478bd9Sstevel@tonic-gate 25797c478bd9Sstevel@tonic-gate static int 2580ffbafc53Scomay verify_limitpriv(zone_dochandle_t handle) 2581ffbafc53Scomay { 2582ffbafc53Scomay char *privname = NULL; 2583ffbafc53Scomay int err; 2584ffbafc53Scomay priv_set_t *privs; 2585ffbafc53Scomay 2586ffbafc53Scomay if ((privs = priv_allocset()) == NULL) { 2587ffbafc53Scomay zperror(gettext("failed to allocate privilege set"), B_FALSE); 2588ffbafc53Scomay return (Z_NOMEM); 2589ffbafc53Scomay } 2590ffbafc53Scomay err = zonecfg_get_privset(handle, privs, &privname); 2591ffbafc53Scomay switch (err) { 2592ffbafc53Scomay case Z_OK: 2593ffbafc53Scomay break; 2594ffbafc53Scomay case Z_PRIV_PROHIBITED: 2595ffbafc53Scomay (void) fprintf(stderr, gettext("privilege \"%s\" is not " 2596ffbafc53Scomay "permitted within the zone's privilege set\n"), privname); 2597ffbafc53Scomay break; 2598ffbafc53Scomay case Z_PRIV_REQUIRED: 2599ffbafc53Scomay (void) fprintf(stderr, gettext("required privilege \"%s\" is " 2600ffbafc53Scomay "missing from the zone's privilege set\n"), privname); 2601ffbafc53Scomay break; 2602ffbafc53Scomay case Z_PRIV_UNKNOWN: 2603ffbafc53Scomay (void) fprintf(stderr, gettext("unknown privilege \"%s\" " 2604ffbafc53Scomay "specified in the zone's privilege set\n"), privname); 2605ffbafc53Scomay break; 2606ffbafc53Scomay default: 2607ffbafc53Scomay zperror( 2608ffbafc53Scomay gettext("failed to determine the zone's privilege set"), 2609ffbafc53Scomay B_TRUE); 2610ffbafc53Scomay break; 2611ffbafc53Scomay } 2612ffbafc53Scomay free(privname); 2613ffbafc53Scomay priv_freeset(privs); 2614ffbafc53Scomay return (err); 2615ffbafc53Scomay } 2616ffbafc53Scomay 26171390a385Sgjelinek static void 26181390a385Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list) 26191390a385Sgjelinek { 26201390a385Sgjelinek int i; 26211390a385Sgjelinek 26221390a385Sgjelinek for (i = 0; i < if_cnt; i++) { 26231390a385Sgjelinek free(if_list[i]->name); 26241390a385Sgjelinek free(if_list[i]); 26251390a385Sgjelinek } 26261390a385Sgjelinek free(if_list); 26271390a385Sgjelinek } 26281390a385Sgjelinek 26291390a385Sgjelinek /* 26301390a385Sgjelinek * Get a list of the network interfaces, along with their address families, 26311390a385Sgjelinek * that are plumbed in the global zone. See if_tcp(7p) for a description 26321390a385Sgjelinek * of the ioctls used here. 26331390a385Sgjelinek */ 26341390a385Sgjelinek static int 26351390a385Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list) 26361390a385Sgjelinek { 26371390a385Sgjelinek int s; 26381390a385Sgjelinek int i; 26391390a385Sgjelinek int res = Z_OK; 26401390a385Sgjelinek int space_needed; 26411390a385Sgjelinek int cnt = 0; 26421390a385Sgjelinek struct lifnum if_num; 26431390a385Sgjelinek struct lifconf if_conf; 26441390a385Sgjelinek struct lifreq *if_reqp; 26451390a385Sgjelinek char *if_buf; 26461390a385Sgjelinek struct net_if **local_ifs = NULL; 26471390a385Sgjelinek 26481390a385Sgjelinek *if_cnt = 0; 26491390a385Sgjelinek *if_list = NULL; 26501390a385Sgjelinek 26511390a385Sgjelinek if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0) 26521390a385Sgjelinek return (Z_ERR); 26531390a385Sgjelinek 26541390a385Sgjelinek /* 26551390a385Sgjelinek * Come back here in the unlikely event that the number of interfaces 26561390a385Sgjelinek * increases between the time we get the count and the time we do the 26571390a385Sgjelinek * SIOCGLIFCONF ioctl. 26581390a385Sgjelinek */ 26591390a385Sgjelinek retry: 26601390a385Sgjelinek /* Get the number of interfaces. */ 26611390a385Sgjelinek if_num.lifn_family = AF_UNSPEC; 26621390a385Sgjelinek if_num.lifn_flags = LIFC_NOXMIT; 26631390a385Sgjelinek if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) { 26641390a385Sgjelinek (void) close(s); 26651390a385Sgjelinek return (Z_ERR); 26661390a385Sgjelinek } 26671390a385Sgjelinek 26681390a385Sgjelinek /* Get the interface configuration list. */ 26691390a385Sgjelinek space_needed = if_num.lifn_count * sizeof (struct lifreq); 26701390a385Sgjelinek if ((if_buf = malloc(space_needed)) == NULL) { 26711390a385Sgjelinek (void) close(s); 26721390a385Sgjelinek return (Z_ERR); 26731390a385Sgjelinek } 26741390a385Sgjelinek if_conf.lifc_family = AF_UNSPEC; 26751390a385Sgjelinek if_conf.lifc_flags = LIFC_NOXMIT; 26761390a385Sgjelinek if_conf.lifc_len = space_needed; 26771390a385Sgjelinek if_conf.lifc_buf = if_buf; 26781390a385Sgjelinek if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) { 26791390a385Sgjelinek free(if_buf); 26801390a385Sgjelinek /* 26811390a385Sgjelinek * SIOCGLIFCONF returns EINVAL if the buffer we passed in is 26821390a385Sgjelinek * too small. In this case go back and get the new if cnt. 26831390a385Sgjelinek */ 26841390a385Sgjelinek if (errno == EINVAL) 26851390a385Sgjelinek goto retry; 26861390a385Sgjelinek 26871390a385Sgjelinek (void) close(s); 26881390a385Sgjelinek return (Z_ERR); 26891390a385Sgjelinek } 26901390a385Sgjelinek (void) close(s); 26911390a385Sgjelinek 26921390a385Sgjelinek /* Get the name and address family for each interface. */ 26931390a385Sgjelinek if_reqp = if_conf.lifc_req; 26941390a385Sgjelinek for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) { 26951390a385Sgjelinek struct net_if **p; 26961390a385Sgjelinek struct lifreq req; 26971390a385Sgjelinek 26981390a385Sgjelinek if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) { 26991390a385Sgjelinek if_reqp++; 27001390a385Sgjelinek continue; 27011390a385Sgjelinek } 27021390a385Sgjelinek 27031390a385Sgjelinek if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family), 27041390a385Sgjelinek SOCK_DGRAM, 0)) == -1) { 27051390a385Sgjelinek res = Z_ERR; 27061390a385Sgjelinek break; 27071390a385Sgjelinek } 27081390a385Sgjelinek 27091390a385Sgjelinek (void) strncpy(req.lifr_name, if_reqp->lifr_name, 27101390a385Sgjelinek sizeof (req.lifr_name)); 27111390a385Sgjelinek if (ioctl(s, SIOCGLIFADDR, &req) < 0) { 27121390a385Sgjelinek (void) close(s); 27131390a385Sgjelinek if_reqp++; 27141390a385Sgjelinek continue; 27151390a385Sgjelinek } 27161390a385Sgjelinek 27171390a385Sgjelinek if ((p = (struct net_if **)realloc(local_ifs, 27181390a385Sgjelinek sizeof (struct net_if *) * (cnt + 1))) == NULL) { 27191390a385Sgjelinek res = Z_ERR; 27201390a385Sgjelinek break; 27211390a385Sgjelinek } 27221390a385Sgjelinek local_ifs = p; 27231390a385Sgjelinek 27241390a385Sgjelinek if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) { 27251390a385Sgjelinek res = Z_ERR; 27261390a385Sgjelinek break; 27271390a385Sgjelinek } 27281390a385Sgjelinek 27291390a385Sgjelinek if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name)) 27301390a385Sgjelinek == NULL) { 27311390a385Sgjelinek free(local_ifs[cnt]); 27321390a385Sgjelinek res = Z_ERR; 27331390a385Sgjelinek break; 27341390a385Sgjelinek } 27351390a385Sgjelinek local_ifs[cnt]->af = req.lifr_addr.ss_family; 27361390a385Sgjelinek cnt++; 27371390a385Sgjelinek 27381390a385Sgjelinek (void) close(s); 27391390a385Sgjelinek if_reqp++; 27401390a385Sgjelinek } 27411390a385Sgjelinek 27421390a385Sgjelinek free(if_buf); 27431390a385Sgjelinek 27441390a385Sgjelinek if (res != Z_OK) { 27451390a385Sgjelinek free_local_netifs(cnt, local_ifs); 27461390a385Sgjelinek } else { 27471390a385Sgjelinek *if_cnt = cnt; 27481390a385Sgjelinek *if_list = local_ifs; 27491390a385Sgjelinek } 27501390a385Sgjelinek 27511390a385Sgjelinek return (res); 27521390a385Sgjelinek } 27531390a385Sgjelinek 27541390a385Sgjelinek static char * 27551390a385Sgjelinek af2str(int af) 27561390a385Sgjelinek { 27571390a385Sgjelinek switch (af) { 27581390a385Sgjelinek case AF_INET: 27591390a385Sgjelinek return ("IPv4"); 27601390a385Sgjelinek case AF_INET6: 27611390a385Sgjelinek return ("IPv6"); 27621390a385Sgjelinek default: 27631390a385Sgjelinek return ("Unknown"); 27641390a385Sgjelinek } 27651390a385Sgjelinek } 27661390a385Sgjelinek 27671390a385Sgjelinek /* 27681390a385Sgjelinek * Cross check the network interface name and address family with the 27691390a385Sgjelinek * interfaces that are set up in the global zone so that we can print the 27701390a385Sgjelinek * appropriate error message. 27711390a385Sgjelinek */ 27721390a385Sgjelinek static void 27731390a385Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg) 27741390a385Sgjelinek { 27751390a385Sgjelinek int i; 27761390a385Sgjelinek int local_if_cnt = 0; 27771390a385Sgjelinek struct net_if **local_ifs = NULL; 27781390a385Sgjelinek boolean_t found_if = B_FALSE; 27791390a385Sgjelinek boolean_t found_af = B_FALSE; 27801390a385Sgjelinek 27811390a385Sgjelinek if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) { 27821390a385Sgjelinek (void) fprintf(stderr, 27831390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 27841390a385Sgjelinek "net", "address", addr, "physical", phys, msg); 27851390a385Sgjelinek return; 27861390a385Sgjelinek } 27871390a385Sgjelinek 27881390a385Sgjelinek for (i = 0; i < local_if_cnt; i++) { 27891390a385Sgjelinek if (strcmp(phys, local_ifs[i]->name) == 0) { 27901390a385Sgjelinek found_if = B_TRUE; 27911390a385Sgjelinek if (af == local_ifs[i]->af) { 27921390a385Sgjelinek found_af = B_TRUE; 27931390a385Sgjelinek break; 27941390a385Sgjelinek } 27951390a385Sgjelinek } 27961390a385Sgjelinek } 27971390a385Sgjelinek 27981390a385Sgjelinek free_local_netifs(local_if_cnt, local_ifs); 27991390a385Sgjelinek 28001390a385Sgjelinek if (!found_if) { 28011390a385Sgjelinek (void) fprintf(stderr, 28021390a385Sgjelinek gettext("could not verify %s %s=%s\n\t" 28031390a385Sgjelinek "network interface %s is not plumbed in the global zone\n"), 28041390a385Sgjelinek "net", "physical", phys, phys); 28051390a385Sgjelinek return; 28061390a385Sgjelinek } 28071390a385Sgjelinek 28081390a385Sgjelinek /* 28091390a385Sgjelinek * Print this error if we were unable to find the address family 28101390a385Sgjelinek * for this interface. If the af variable is not initialized to 28111390a385Sgjelinek * to something meaningful by the caller (not AF_UNSPEC) then we 28121390a385Sgjelinek * also skip this message since it wouldn't be informative. 28131390a385Sgjelinek */ 28141390a385Sgjelinek if (!found_af && af != AF_UNSPEC) { 28151390a385Sgjelinek (void) fprintf(stderr, 28161390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\tthe %s address " 2817f4b3ec61Sdh155122 "family is not configured on this network interface in " 2818f4b3ec61Sdh155122 "the\n\tglobal zone\n"), 28191390a385Sgjelinek "net", "address", addr, "physical", phys, af2str(af)); 28201390a385Sgjelinek return; 28211390a385Sgjelinek } 28221390a385Sgjelinek 28231390a385Sgjelinek (void) fprintf(stderr, 28241390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 28251390a385Sgjelinek "net", "address", addr, "physical", phys, msg); 28261390a385Sgjelinek } 28271390a385Sgjelinek 2828ffbafc53Scomay static int 2829ce28b40eSzt129084 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[]) 28307c478bd9Sstevel@tonic-gate { 28317c478bd9Sstevel@tonic-gate struct zone_nwiftab nwiftab; 28327c478bd9Sstevel@tonic-gate int return_code = Z_OK; 28337c478bd9Sstevel@tonic-gate int err; 2834108322fbScarlsonj boolean_t in_alt_root; 2835f4b3ec61Sdh155122 zone_iptype_t iptype; 2836f4b3ec61Sdh155122 int fd; 28377c478bd9Sstevel@tonic-gate 2838108322fbScarlsonj in_alt_root = zonecfg_in_alt_root(); 2839108322fbScarlsonj if (in_alt_root) 2840108322fbScarlsonj goto no_net; 2841108322fbScarlsonj 2842f4b3ec61Sdh155122 if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) { 2843f4b3ec61Sdh155122 errno = err; 2844f4b3ec61Sdh155122 zperror(cmd_to_str(cmd_num), B_TRUE); 2845f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 2846f4b3ec61Sdh155122 return (Z_ERR); 2847f4b3ec61Sdh155122 } 28487c478bd9Sstevel@tonic-gate if ((err = zonecfg_setnwifent(handle)) != Z_OK) { 28497c478bd9Sstevel@tonic-gate errno = err; 28507c478bd9Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 28517c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 28527c478bd9Sstevel@tonic-gate return (Z_ERR); 28537c478bd9Sstevel@tonic-gate } 28547c478bd9Sstevel@tonic-gate while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) { 28557c478bd9Sstevel@tonic-gate struct lifreq lifr; 28561390a385Sgjelinek sa_family_t af = AF_UNSPEC; 2857f4b3ec61Sdh155122 char dl_owner_zname[ZONENAME_MAX]; 2858f4b3ec61Sdh155122 zoneid_t dl_owner_zid; 2859f4b3ec61Sdh155122 zoneid_t target_zid; 2860f4b3ec61Sdh155122 int res; 28617c478bd9Sstevel@tonic-gate 28627c478bd9Sstevel@tonic-gate /* skip any loopback interfaces */ 28637c478bd9Sstevel@tonic-gate if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0) 28647c478bd9Sstevel@tonic-gate continue; 2865f4b3ec61Sdh155122 switch (iptype) { 2866f4b3ec61Sdh155122 case ZS_SHARED: 2867f4b3ec61Sdh155122 if ((res = zonecfg_valid_net_address( 2868f4b3ec61Sdh155122 nwiftab.zone_nwif_address, &lifr)) != Z_OK) { 28691390a385Sgjelinek print_net_err(nwiftab.zone_nwif_physical, 28701390a385Sgjelinek nwiftab.zone_nwif_address, af, 28711390a385Sgjelinek zonecfg_strerror(res)); 28727c478bd9Sstevel@tonic-gate return_code = Z_ERR; 28737c478bd9Sstevel@tonic-gate continue; 28747c478bd9Sstevel@tonic-gate } 28757c478bd9Sstevel@tonic-gate af = lifr.lifr_addr.ss_family; 2876f4b3ec61Sdh155122 if (!zonecfg_ifname_exists(af, 2877f4b3ec61Sdh155122 nwiftab.zone_nwif_physical)) { 287822321485Svp157776 /* 2879f4b3ec61Sdh155122 * The interface failed to come up. We continue 2880f4b3ec61Sdh155122 * on anyway for the sake of consistency: a 2881f4b3ec61Sdh155122 * zone is not shut down if the interface fails 2882f4b3ec61Sdh155122 * any time after boot, nor does the global zone 2883f4b3ec61Sdh155122 * fail to boot if an interface fails. 288422321485Svp157776 */ 288522321485Svp157776 (void) fprintf(stderr, 2886f4b3ec61Sdh155122 gettext("WARNING: skipping network " 2887f4b3ec61Sdh155122 "interface '%s' which may not be " 2888f4b3ec61Sdh155122 "present/plumbed in the global " 2889f4b3ec61Sdh155122 "zone.\n"), 289022321485Svp157776 nwiftab.zone_nwif_physical); 28917c478bd9Sstevel@tonic-gate } 2892f4b3ec61Sdh155122 break; 2893f4b3ec61Sdh155122 case ZS_EXCLUSIVE: 2894f4b3ec61Sdh155122 /* Warning if it exists for either IPv4 or IPv6 */ 2895f4b3ec61Sdh155122 2896f4b3ec61Sdh155122 if (zonecfg_ifname_exists(AF_INET, 2897f4b3ec61Sdh155122 nwiftab.zone_nwif_physical) || 2898f4b3ec61Sdh155122 zonecfg_ifname_exists(AF_INET6, 2899f4b3ec61Sdh155122 nwiftab.zone_nwif_physical)) { 2900f4b3ec61Sdh155122 (void) fprintf(stderr, 2901f4b3ec61Sdh155122 gettext("WARNING: skipping network " 2902f4b3ec61Sdh155122 "interface '%s' which is used in the " 2903f4b3ec61Sdh155122 "global zone.\n"), 2904f4b3ec61Sdh155122 nwiftab.zone_nwif_physical); 2905f4b3ec61Sdh155122 break; 2906f4b3ec61Sdh155122 } 2907f4b3ec61Sdh155122 /* 2908f4b3ec61Sdh155122 * Verify that the physical interface can 2909f4b3ec61Sdh155122 * be opened 2910f4b3ec61Sdh155122 */ 2911f4b3ec61Sdh155122 fd = ifname_open(nwiftab.zone_nwif_physical); 2912f4b3ec61Sdh155122 if (fd == -1) { 2913f4b3ec61Sdh155122 (void) fprintf(stderr, 2914f4b3ec61Sdh155122 gettext("WARNING: skipping network " 2915f4b3ec61Sdh155122 "interface '%s' which cannot be opened.\n"), 2916f4b3ec61Sdh155122 nwiftab.zone_nwif_physical); 2917f4b3ec61Sdh155122 break; 2918f4b3ec61Sdh155122 } else { 2919f4b3ec61Sdh155122 (void) close(fd); 2920f4b3ec61Sdh155122 } 2921f4b3ec61Sdh155122 /* 2922f4b3ec61Sdh155122 * Verify whether the physical interface is already 2923f4b3ec61Sdh155122 * used by a zone. 2924f4b3ec61Sdh155122 */ 2925f4b3ec61Sdh155122 dl_owner_zid = ALL_ZONES; 2926f4b3ec61Sdh155122 if (zone_check_datalink(&dl_owner_zid, 2927f4b3ec61Sdh155122 nwiftab.zone_nwif_physical) != 0) 2928f4b3ec61Sdh155122 break; 2929f4b3ec61Sdh155122 2930f4b3ec61Sdh155122 /* 2931f4b3ec61Sdh155122 * If the zone being verified is 2932f4b3ec61Sdh155122 * running and owns the interface 2933f4b3ec61Sdh155122 */ 2934f4b3ec61Sdh155122 target_zid = getzoneidbyname(target_zone); 2935f4b3ec61Sdh155122 if (target_zid == dl_owner_zid) 2936f4b3ec61Sdh155122 break; 2937f4b3ec61Sdh155122 2938f4b3ec61Sdh155122 /* Zone id match failed, use name to check */ 2939f4b3ec61Sdh155122 if (getzonenamebyid(dl_owner_zid, dl_owner_zname, 2940f4b3ec61Sdh155122 ZONENAME_MAX) < 0) { 2941f4b3ec61Sdh155122 /* No name, show ID instead */ 2942f4b3ec61Sdh155122 (void) snprintf(dl_owner_zname, ZONENAME_MAX, 2943f4b3ec61Sdh155122 "<%d>", dl_owner_zid); 2944f4b3ec61Sdh155122 } else if (strcmp(dl_owner_zname, target_zone) == 0) 2945f4b3ec61Sdh155122 break; 2946f4b3ec61Sdh155122 2947f4b3ec61Sdh155122 /* 2948f4b3ec61Sdh155122 * Note here we only report a warning that 2949f4b3ec61Sdh155122 * the interface is already in use by another 2950f4b3ec61Sdh155122 * running zone, and the verify process just 2951f4b3ec61Sdh155122 * goes on, if the interface is still in use 2952f4b3ec61Sdh155122 * when this zone really boots up, zoneadmd 2953f4b3ec61Sdh155122 * will find it. If the name of the zone which 2954f4b3ec61Sdh155122 * owns this interface cannot be determined, 2955f4b3ec61Sdh155122 * then it is not possible to determine if there 2956f4b3ec61Sdh155122 * is a conflict so just report it as a warning. 2957f4b3ec61Sdh155122 */ 2958f4b3ec61Sdh155122 (void) fprintf(stderr, 2959f4b3ec61Sdh155122 gettext("WARNING: skipping network interface " 2960f4b3ec61Sdh155122 "'%s' which is used by the non-global zone " 2961f4b3ec61Sdh155122 "'%s'.\n"), nwiftab.zone_nwif_physical, 2962f4b3ec61Sdh155122 dl_owner_zname); 2963f4b3ec61Sdh155122 break; 2964f4b3ec61Sdh155122 } 29657c478bd9Sstevel@tonic-gate } 29667c478bd9Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 2967108322fbScarlsonj no_net: 29687c478bd9Sstevel@tonic-gate 2969e7f3c547Sgjelinek /* verify that lofs has not been excluded from the kernel */ 29708cd327d5Sgjelinek if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH || 29718cd327d5Sgjelinek cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) && 29728cd327d5Sgjelinek modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) { 2973e7f3c547Sgjelinek if (errno == ENXIO) 2974e7f3c547Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2975e7f3c547Sgjelinek "lofs(7FS): possibly excluded in /etc/system\n")); 2976e7f3c547Sgjelinek else 2977e7f3c547Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2978e7f3c547Sgjelinek "lofs(7FS): %s\n"), strerror(errno)); 2979e7f3c547Sgjelinek return_code = Z_ERR; 2980e7f3c547Sgjelinek } 2981e7f3c547Sgjelinek 29827c478bd9Sstevel@tonic-gate if (verify_filesystems(handle) != Z_OK) 29837c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2984b5abaf04Sgjelinek if (verify_ipd(handle) != Z_OK) 2985b5abaf04Sgjelinek return_code = Z_ERR; 2986108322fbScarlsonj if (!in_alt_root && verify_rctls(handle) != Z_OK) 29877c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2988108322fbScarlsonj if (!in_alt_root && verify_pool(handle) != Z_OK) 29897c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2990ce28b40eSzt129084 if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK) 29919acbbeafSnn35248 return_code = Z_ERR; 2992fa9e4066Sahrens if (!in_alt_root && verify_datasets(handle) != Z_OK) 2993fa9e4066Sahrens return_code = Z_ERR; 2994ffbafc53Scomay 2995ffbafc53Scomay /* 2996ffbafc53Scomay * As the "mount" command is used for patching/upgrading of zones 2997ffbafc53Scomay * or other maintenance processes, the zone's privilege set is not 2998ffbafc53Scomay * checked in this case. Instead, the default, safe set of 2999ffbafc53Scomay * privileges will be used when this zone is created in the 3000ffbafc53Scomay * kernel. 3001ffbafc53Scomay */ 3002ffbafc53Scomay if (!in_alt_root && cmd_num != CMD_MOUNT && 3003ffbafc53Scomay verify_limitpriv(handle) != Z_OK) 3004ffbafc53Scomay return_code = Z_ERR; 30058cd327d5Sgjelinek 30068cd327d5Sgjelinek return (return_code); 30078cd327d5Sgjelinek } 30088cd327d5Sgjelinek 30098cd327d5Sgjelinek static int 3010ce28b40eSzt129084 verify_details(int cmd_num, char *argv[]) 30118cd327d5Sgjelinek { 30128cd327d5Sgjelinek zone_dochandle_t handle; 30138cd327d5Sgjelinek char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN]; 30148cd327d5Sgjelinek int return_code = Z_OK; 30158cd327d5Sgjelinek int err; 30168cd327d5Sgjelinek 30178cd327d5Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 30188cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 30198cd327d5Sgjelinek return (Z_ERR); 30208cd327d5Sgjelinek } 30218cd327d5Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 30228cd327d5Sgjelinek errno = err; 30238cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 30248cd327d5Sgjelinek zonecfg_fini_handle(handle); 30258cd327d5Sgjelinek return (Z_ERR); 30268cd327d5Sgjelinek } 30278cd327d5Sgjelinek if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 30288cd327d5Sgjelinek Z_OK) { 30298cd327d5Sgjelinek errno = err; 30308cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 30318cd327d5Sgjelinek zonecfg_fini_handle(handle); 30328cd327d5Sgjelinek return (Z_ERR); 30338cd327d5Sgjelinek } 30348cd327d5Sgjelinek /* 30358cd327d5Sgjelinek * zonecfg_get_zonepath() gets its data from the XML repository. 30368cd327d5Sgjelinek * Verify this against the index file, which is checked first by 30378cd327d5Sgjelinek * zone_get_zonepath(). If they don't match, bail out. 30388cd327d5Sgjelinek */ 30398cd327d5Sgjelinek if ((err = zone_get_zonepath(target_zone, checkpath, 30408cd327d5Sgjelinek sizeof (checkpath))) != Z_OK) { 30418cd327d5Sgjelinek errno = err; 30428cd327d5Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 30438cd327d5Sgjelinek return (Z_ERR); 30448cd327d5Sgjelinek } 30458cd327d5Sgjelinek if (strcmp(zonepath, checkpath) != 0) { 30468cd327d5Sgjelinek /* 30478cd327d5Sgjelinek * TRANSLATION_NOTE 30488cd327d5Sgjelinek * XML and zonepath are literals that should not be translated. 30498cd327d5Sgjelinek */ 30508cd327d5Sgjelinek (void) fprintf(stderr, gettext("The XML repository has " 30518cd327d5Sgjelinek "zonepath '%s',\nbut the index file has zonepath '%s'.\n" 30528cd327d5Sgjelinek "These must match, so fix the incorrect entry.\n"), 30538cd327d5Sgjelinek zonepath, checkpath); 30548cd327d5Sgjelinek return (Z_ERR); 30558cd327d5Sgjelinek } 30568cd327d5Sgjelinek if (validate_zonepath(zonepath, cmd_num) != Z_OK) { 30578cd327d5Sgjelinek (void) fprintf(stderr, gettext("could not verify zonepath %s " 30588cd327d5Sgjelinek "because of the above errors.\n"), zonepath); 30598cd327d5Sgjelinek return_code = Z_ERR; 30608cd327d5Sgjelinek } 30618cd327d5Sgjelinek 3062ce28b40eSzt129084 if (verify_handle(cmd_num, handle, argv) != Z_OK) 30638cd327d5Sgjelinek return_code = Z_ERR; 30648cd327d5Sgjelinek 30657c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 30667c478bd9Sstevel@tonic-gate if (return_code == Z_ERR) 30677c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 30687c478bd9Sstevel@tonic-gate gettext("%s: zone %s failed to verify\n"), 30697c478bd9Sstevel@tonic-gate execname, target_zone); 30707c478bd9Sstevel@tonic-gate return (return_code); 30717c478bd9Sstevel@tonic-gate } 30727c478bd9Sstevel@tonic-gate 30737c478bd9Sstevel@tonic-gate static int 30747c478bd9Sstevel@tonic-gate verify_func(int argc, char *argv[]) 30757c478bd9Sstevel@tonic-gate { 30767c478bd9Sstevel@tonic-gate int arg; 30777c478bd9Sstevel@tonic-gate 30787c478bd9Sstevel@tonic-gate optind = 0; 30797c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 30807c478bd9Sstevel@tonic-gate switch (arg) { 30817c478bd9Sstevel@tonic-gate case '?': 30827c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 30837c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 30847c478bd9Sstevel@tonic-gate default: 30857c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 30867c478bd9Sstevel@tonic-gate return (Z_USAGE); 30877c478bd9Sstevel@tonic-gate } 30887c478bd9Sstevel@tonic-gate } 30897c478bd9Sstevel@tonic-gate if (argc > optind) { 30907c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 30917c478bd9Sstevel@tonic-gate return (Z_USAGE); 30927c478bd9Sstevel@tonic-gate } 30939acbbeafSnn35248 if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE) 30949acbbeafSnn35248 != Z_OK) 30957c478bd9Sstevel@tonic-gate return (Z_ERR); 3096ce28b40eSzt129084 return (verify_details(CMD_VERIFY, argv)); 30977c478bd9Sstevel@tonic-gate } 30987c478bd9Sstevel@tonic-gate 30999acbbeafSnn35248 static int 31009acbbeafSnn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize) 31019acbbeafSnn35248 { 31029acbbeafSnn35248 char optstring[4]; 31039acbbeafSnn35248 31049acbbeafSnn35248 if (opt > 0) 31059acbbeafSnn35248 (void) sprintf(optstring, " -%c", opt); 31069acbbeafSnn35248 else 31079acbbeafSnn35248 (void) strcpy(optstring, " "); 31089acbbeafSnn35248 31099acbbeafSnn35248 if ((strlcat(buf, optstring, bufsize) > bufsize)) 31109acbbeafSnn35248 return (Z_ERR); 31119acbbeafSnn35248 if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize)) 31129acbbeafSnn35248 return (Z_ERR); 31139acbbeafSnn35248 return (Z_OK); 31149acbbeafSnn35248 } 31157c478bd9Sstevel@tonic-gate 31167c478bd9Sstevel@tonic-gate static int 31177c478bd9Sstevel@tonic-gate install_func(int argc, char *argv[]) 31187c478bd9Sstevel@tonic-gate { 31199acbbeafSnn35248 char cmdbuf[MAXPATHLEN]; 31207c478bd9Sstevel@tonic-gate int lockfd; 31219acbbeafSnn35248 int arg, err, subproc_err; 31227c478bd9Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 3123123807fbSedp brand_handle_t bh = NULL; 31247c478bd9Sstevel@tonic-gate int status; 31250b5de56dSgjelinek boolean_t nodataset = B_FALSE; 31269acbbeafSnn35248 char opts[128]; 31279acbbeafSnn35248 31289acbbeafSnn35248 if (target_zone == NULL) { 31299acbbeafSnn35248 sub_usage(SHELP_INSTALL, CMD_INSTALL); 31309acbbeafSnn35248 return (Z_USAGE); 31319acbbeafSnn35248 } 31327c478bd9Sstevel@tonic-gate 3133108322fbScarlsonj if (zonecfg_in_alt_root()) { 3134108322fbScarlsonj zerror(gettext("cannot install zone in alternate root")); 3135108322fbScarlsonj return (Z_ERR); 3136108322fbScarlsonj } 3137108322fbScarlsonj 31389acbbeafSnn35248 if ((err = zone_get_zonepath(target_zone, zonepath, 31399acbbeafSnn35248 sizeof (zonepath))) != Z_OK) { 31409acbbeafSnn35248 errno = err; 31419acbbeafSnn35248 zperror2(target_zone, gettext("could not get zone path")); 31429acbbeafSnn35248 return (Z_ERR); 31439acbbeafSnn35248 } 31449acbbeafSnn35248 31459acbbeafSnn35248 /* Fetch the install command from the brand configuration. */ 3146123807fbSedp if ((bh = brand_open(target_brand)) == NULL) { 31479acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 31489acbbeafSnn35248 return (Z_ERR); 31499acbbeafSnn35248 } 31509acbbeafSnn35248 31519acbbeafSnn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 3152123807fbSedp if (brand_get_install(bh, target_zone, zonepath, cmdbuf + EXEC_LEN, 31539acbbeafSnn35248 sizeof (cmdbuf) - EXEC_LEN, 0, NULL) != 0) { 31549acbbeafSnn35248 zerror("invalid brand configuration: missing install resource"); 3155123807fbSedp brand_close(bh); 31569acbbeafSnn35248 return (Z_ERR); 31579acbbeafSnn35248 } 31589acbbeafSnn35248 31599acbbeafSnn35248 (void) strcpy(opts, "?x:"); 31609acbbeafSnn35248 if (!is_native_zone) { 31619acbbeafSnn35248 /* 31629acbbeafSnn35248 * Fetch the list of recognized command-line options from 31639acbbeafSnn35248 * the brand configuration file. 31649acbbeafSnn35248 */ 3165123807fbSedp if (brand_get_installopts(bh, opts + strlen(opts), 31669acbbeafSnn35248 sizeof (opts) - strlen(opts)) != 0) { 31679acbbeafSnn35248 zerror("invalid brand configuration: missing " 31689acbbeafSnn35248 "install options resource"); 3169123807fbSedp brand_close(bh); 31709acbbeafSnn35248 return (Z_ERR); 31719acbbeafSnn35248 } 31729acbbeafSnn35248 } 3173123807fbSedp brand_close(bh); 31749acbbeafSnn35248 31757c478bd9Sstevel@tonic-gate optind = 0; 31769acbbeafSnn35248 while ((arg = getopt(argc, argv, opts)) != EOF) { 31777c478bd9Sstevel@tonic-gate switch (arg) { 31787c478bd9Sstevel@tonic-gate case '?': 31797c478bd9Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 31807c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 31810b5de56dSgjelinek case 'x': 31820b5de56dSgjelinek if (strcmp(optarg, "nodataset") != 0) { 31830b5de56dSgjelinek sub_usage(SHELP_INSTALL, CMD_INSTALL); 31840b5de56dSgjelinek return (Z_USAGE); 31850b5de56dSgjelinek } 31860b5de56dSgjelinek nodataset = B_TRUE; 31870b5de56dSgjelinek break; 31887c478bd9Sstevel@tonic-gate default: 31899acbbeafSnn35248 if (is_native_zone) { 31907c478bd9Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 31917c478bd9Sstevel@tonic-gate return (Z_USAGE); 31927c478bd9Sstevel@tonic-gate } 31939acbbeafSnn35248 31949acbbeafSnn35248 /* 31959acbbeafSnn35248 * This option isn't for zoneadm, so append it to 31969acbbeafSnn35248 * the command line passed to the brand-specific 31979acbbeafSnn35248 * install routine. 31989acbbeafSnn35248 */ 31999acbbeafSnn35248 if (addopt(cmdbuf, optopt, optarg, 32009acbbeafSnn35248 sizeof (cmdbuf)) != Z_OK) { 32019acbbeafSnn35248 zerror("Install command line too long"); 32029acbbeafSnn35248 return (Z_ERR); 32037c478bd9Sstevel@tonic-gate } 32049acbbeafSnn35248 break; 32057c478bd9Sstevel@tonic-gate } 32069acbbeafSnn35248 } 32079acbbeafSnn35248 32089acbbeafSnn35248 if (!is_native_zone) { 32099acbbeafSnn35248 for (; optind < argc; optind++) { 32109acbbeafSnn35248 if (addopt(cmdbuf, 0, argv[optind], 32119acbbeafSnn35248 sizeof (cmdbuf)) != Z_OK) { 32129acbbeafSnn35248 zerror("Install command line too long"); 32139acbbeafSnn35248 return (Z_ERR); 32149acbbeafSnn35248 } 32159acbbeafSnn35248 } 32169acbbeafSnn35248 } 32179acbbeafSnn35248 32189acbbeafSnn35248 if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE, B_FALSE) 32199acbbeafSnn35248 != Z_OK) 32207c478bd9Sstevel@tonic-gate return (Z_ERR); 3221ce28b40eSzt129084 if (verify_details(CMD_INSTALL, argv) != Z_OK) 32227c478bd9Sstevel@tonic-gate return (Z_ERR); 32237c478bd9Sstevel@tonic-gate 32247c478bd9Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 32257c478bd9Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 3226865e09a4Sgjelinek "zoneadm"); 32277c478bd9Sstevel@tonic-gate return (Z_ERR); 32287c478bd9Sstevel@tonic-gate } 32297c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 32307c478bd9Sstevel@tonic-gate if (err != Z_OK) { 32317c478bd9Sstevel@tonic-gate errno = err; 32327c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 32337c478bd9Sstevel@tonic-gate goto done; 32347c478bd9Sstevel@tonic-gate } 32357c478bd9Sstevel@tonic-gate 32369acbbeafSnn35248 if (!nodataset) 32379acbbeafSnn35248 create_zfs_zonepath(zonepath); 32389acbbeafSnn35248 32397c478bd9Sstevel@tonic-gate /* 32407c478bd9Sstevel@tonic-gate * According to the Application Packaging Developer's Guide, a 32417c478bd9Sstevel@tonic-gate * "checkinstall" script when included in a package is executed as 32427c478bd9Sstevel@tonic-gate * the user "install", if such a user exists, or by the user 32437c478bd9Sstevel@tonic-gate * "nobody". In order to support this dubious behavior, the path 32447c478bd9Sstevel@tonic-gate * to the zone being constructed is opened up during the life of 32457c478bd9Sstevel@tonic-gate * the command laying down the zone's root file system. Once this 32467c478bd9Sstevel@tonic-gate * has completed, regardless of whether it was successful, the 32477c478bd9Sstevel@tonic-gate * path to the zone is again restricted. 32487c478bd9Sstevel@tonic-gate */ 32497c478bd9Sstevel@tonic-gate if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) { 32507c478bd9Sstevel@tonic-gate zperror(zonepath, B_FALSE); 32517c478bd9Sstevel@tonic-gate err = Z_ERR; 32527c478bd9Sstevel@tonic-gate goto done; 32537c478bd9Sstevel@tonic-gate } 32547c478bd9Sstevel@tonic-gate 32559acbbeafSnn35248 if (is_native_zone) 32567c478bd9Sstevel@tonic-gate status = do_subproc(cmdbuf); 32579acbbeafSnn35248 else 32589acbbeafSnn35248 status = do_subproc_interactive(cmdbuf); 32599acbbeafSnn35248 32607c478bd9Sstevel@tonic-gate if (chmod(zonepath, S_IRWXU) != 0) { 32617c478bd9Sstevel@tonic-gate zperror(zonepath, B_FALSE); 32627c478bd9Sstevel@tonic-gate err = Z_ERR; 32637c478bd9Sstevel@tonic-gate goto done; 32647c478bd9Sstevel@tonic-gate } 32659acbbeafSnn35248 if ((subproc_err = 32669acbbeafSnn35248 subproc_status(gettext("brand-specific installation"), status, 32679acbbeafSnn35248 B_FALSE)) != ZONE_SUBPROC_OK) { 32689acbbeafSnn35248 err = Z_ERR; 32697c478bd9Sstevel@tonic-gate goto done; 32709acbbeafSnn35248 } 32717c478bd9Sstevel@tonic-gate 32727c478bd9Sstevel@tonic-gate if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 32737c478bd9Sstevel@tonic-gate errno = err; 32747c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 32757c478bd9Sstevel@tonic-gate goto done; 32767c478bd9Sstevel@tonic-gate } 32777c478bd9Sstevel@tonic-gate 32787c478bd9Sstevel@tonic-gate done: 32799acbbeafSnn35248 /* 32809acbbeafSnn35248 * If the install script exited with ZONE_SUBPROC_USAGE or 32819acbbeafSnn35248 * ZONE_SUBPROC_NOTCOMPLETE, try to clean up the zone and leave the 32829acbbeafSnn35248 * zone in the CONFIGURED state so that another install can be 32839acbbeafSnn35248 * attempted without requiring an uninstall first. 32849acbbeafSnn35248 */ 32859acbbeafSnn35248 if ((subproc_err == ZONE_SUBPROC_USAGE) || 32869acbbeafSnn35248 (subproc_err == ZONE_SUBPROC_NOTCOMPLETE)) { 32879acbbeafSnn35248 if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 32889acbbeafSnn35248 errno = err; 32899acbbeafSnn35248 zperror2(target_zone, 32909acbbeafSnn35248 gettext("cleaning up zonepath failed")); 32919acbbeafSnn35248 } else if ((err = zone_set_state(target_zone, 32929acbbeafSnn35248 ZONE_STATE_CONFIGURED)) != Z_OK) { 32939acbbeafSnn35248 errno = err; 32949acbbeafSnn35248 zperror2(target_zone, gettext("could not set state")); 32959acbbeafSnn35248 } 32969acbbeafSnn35248 } 32979acbbeafSnn35248 32987c478bd9Sstevel@tonic-gate release_lock_file(lockfd); 32997c478bd9Sstevel@tonic-gate return ((err == Z_OK) ? Z_OK : Z_ERR); 33007c478bd9Sstevel@tonic-gate } 33017c478bd9Sstevel@tonic-gate 33027c478bd9Sstevel@tonic-gate /* 3303865e09a4Sgjelinek * Check that the inherited pkg dirs are the same for the clone and its source. 3304865e09a4Sgjelinek * The easiest way to do that is check that the list of ipds is the same 3305865e09a4Sgjelinek * by matching each one against the other. This algorithm should be fine since 3306865e09a4Sgjelinek * the list of ipds should not be that long. 3307865e09a4Sgjelinek */ 3308865e09a4Sgjelinek static int 3309865e09a4Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone, 3310865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3311865e09a4Sgjelinek { 3312865e09a4Sgjelinek int err; 3313865e09a4Sgjelinek int res = Z_OK; 3314865e09a4Sgjelinek int s_cnt = 0; 3315865e09a4Sgjelinek int t_cnt = 0; 3316865e09a4Sgjelinek struct zone_fstab s_fstab; 3317865e09a4Sgjelinek struct zone_fstab t_fstab; 3318865e09a4Sgjelinek 3319865e09a4Sgjelinek /* 3320865e09a4Sgjelinek * First check the source of the clone against the target. 3321865e09a4Sgjelinek */ 3322865e09a4Sgjelinek if ((err = zonecfg_setipdent(s_handle)) != Z_OK) { 3323865e09a4Sgjelinek errno = err; 3324865e09a4Sgjelinek zperror2(source_zone, gettext("could not enumerate " 3325865e09a4Sgjelinek "inherit-pkg-dirs")); 3326865e09a4Sgjelinek return (Z_ERR); 3327865e09a4Sgjelinek } 3328865e09a4Sgjelinek 3329865e09a4Sgjelinek while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) { 3330865e09a4Sgjelinek boolean_t match = B_FALSE; 3331865e09a4Sgjelinek 3332865e09a4Sgjelinek s_cnt++; 3333865e09a4Sgjelinek 3334865e09a4Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 3335865e09a4Sgjelinek errno = err; 3336865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate " 3337865e09a4Sgjelinek "inherit-pkg-dirs")); 3338865e09a4Sgjelinek (void) zonecfg_endipdent(s_handle); 3339865e09a4Sgjelinek return (Z_ERR); 3340865e09a4Sgjelinek } 3341865e09a4Sgjelinek 3342865e09a4Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) { 3343865e09a4Sgjelinek if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir) 3344865e09a4Sgjelinek == 0) { 3345865e09a4Sgjelinek match = B_TRUE; 3346865e09a4Sgjelinek break; 3347865e09a4Sgjelinek } 3348865e09a4Sgjelinek } 3349865e09a4Sgjelinek (void) zonecfg_endipdent(t_handle); 3350865e09a4Sgjelinek 3351865e09a4Sgjelinek if (!match) { 3352865e09a4Sgjelinek (void) fprintf(stderr, gettext("inherit-pkg-dir " 3353865e09a4Sgjelinek "'%s' is not configured in zone %s.\n"), 3354865e09a4Sgjelinek s_fstab.zone_fs_dir, target_zone); 3355865e09a4Sgjelinek res = Z_ERR; 3356865e09a4Sgjelinek } 3357865e09a4Sgjelinek } 3358865e09a4Sgjelinek 3359865e09a4Sgjelinek (void) zonecfg_endipdent(s_handle); 3360865e09a4Sgjelinek 3361865e09a4Sgjelinek /* skip the next check if we already have errors */ 3362865e09a4Sgjelinek if (res == Z_ERR) 3363865e09a4Sgjelinek return (res); 3364865e09a4Sgjelinek 3365865e09a4Sgjelinek /* 3366865e09a4Sgjelinek * Now check the number of ipds in the target so we can verify 3367865e09a4Sgjelinek * that the source is not a subset of the target. 3368865e09a4Sgjelinek */ 3369865e09a4Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 3370865e09a4Sgjelinek errno = err; 3371865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate " 3372865e09a4Sgjelinek "inherit-pkg-dirs")); 3373865e09a4Sgjelinek return (Z_ERR); 3374865e09a4Sgjelinek } 3375865e09a4Sgjelinek 3376865e09a4Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) 3377865e09a4Sgjelinek t_cnt++; 3378865e09a4Sgjelinek 3379865e09a4Sgjelinek (void) zonecfg_endipdent(t_handle); 3380865e09a4Sgjelinek 3381865e09a4Sgjelinek if (t_cnt != s_cnt) { 3382865e09a4Sgjelinek (void) fprintf(stderr, gettext("Zone %s is configured " 3383865e09a4Sgjelinek "with inherit-pkg-dirs that are not configured in zone " 3384865e09a4Sgjelinek "%s.\n"), target_zone, source_zone); 3385865e09a4Sgjelinek res = Z_ERR; 3386865e09a4Sgjelinek } 3387865e09a4Sgjelinek 3388865e09a4Sgjelinek return (res); 3389865e09a4Sgjelinek } 3390865e09a4Sgjelinek 3391865e09a4Sgjelinek static void 3392865e09a4Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone, 3393865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3394865e09a4Sgjelinek { 3395865e09a4Sgjelinek int err; 3396865e09a4Sgjelinek struct zone_devtab s_devtab; 3397865e09a4Sgjelinek struct zone_devtab t_devtab; 3398865e09a4Sgjelinek 3399865e09a4Sgjelinek if ((err = zonecfg_setdevent(t_handle)) != Z_OK) { 3400865e09a4Sgjelinek errno = err; 3401865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate devices")); 3402865e09a4Sgjelinek return; 3403865e09a4Sgjelinek } 3404865e09a4Sgjelinek 3405865e09a4Sgjelinek while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) { 3406865e09a4Sgjelinek if ((err = zonecfg_setdevent(s_handle)) != Z_OK) { 3407865e09a4Sgjelinek errno = err; 3408865e09a4Sgjelinek zperror2(source_zone, 3409865e09a4Sgjelinek gettext("could not enumerate devices")); 3410865e09a4Sgjelinek (void) zonecfg_enddevent(t_handle); 3411865e09a4Sgjelinek return; 3412865e09a4Sgjelinek } 3413865e09a4Sgjelinek 3414865e09a4Sgjelinek while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) { 3415865e09a4Sgjelinek /* 3416865e09a4Sgjelinek * Use fnmatch to catch the case where wildcards 3417865e09a4Sgjelinek * were used in one zone and the other has an 3418865e09a4Sgjelinek * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs. 3419865e09a4Sgjelinek * /dev/\*dsk/c0t0d0s6). 3420865e09a4Sgjelinek */ 3421865e09a4Sgjelinek if (fnmatch(t_devtab.zone_dev_match, 3422865e09a4Sgjelinek s_devtab.zone_dev_match, FNM_PATHNAME) == 0 || 3423865e09a4Sgjelinek fnmatch(s_devtab.zone_dev_match, 3424865e09a4Sgjelinek t_devtab.zone_dev_match, FNM_PATHNAME) == 0) { 3425865e09a4Sgjelinek (void) fprintf(stderr, 3426865e09a4Sgjelinek gettext("WARNING: device '%s' " 3427865e09a4Sgjelinek "is configured in both zones.\n"), 3428865e09a4Sgjelinek t_devtab.zone_dev_match); 3429865e09a4Sgjelinek break; 3430865e09a4Sgjelinek } 3431865e09a4Sgjelinek } 3432865e09a4Sgjelinek (void) zonecfg_enddevent(s_handle); 3433865e09a4Sgjelinek } 3434865e09a4Sgjelinek 3435865e09a4Sgjelinek (void) zonecfg_enddevent(t_handle); 3436865e09a4Sgjelinek } 3437865e09a4Sgjelinek 3438865e09a4Sgjelinek /* 3439865e09a4Sgjelinek * Check if the specified mount option (opt) is contained within the 3440865e09a4Sgjelinek * options string. 3441865e09a4Sgjelinek */ 3442865e09a4Sgjelinek static boolean_t 3443865e09a4Sgjelinek opt_match(char *opt, char *options) 3444865e09a4Sgjelinek { 3445865e09a4Sgjelinek char *p; 3446865e09a4Sgjelinek char *lastp; 3447865e09a4Sgjelinek 3448865e09a4Sgjelinek if ((p = strtok_r(options, ",", &lastp)) != NULL) { 3449865e09a4Sgjelinek if (strcmp(p, opt) == 0) 3450865e09a4Sgjelinek return (B_TRUE); 3451865e09a4Sgjelinek while ((p = strtok_r(NULL, ",", &lastp)) != NULL) { 3452865e09a4Sgjelinek if (strcmp(p, opt) == 0) 3453865e09a4Sgjelinek return (B_TRUE); 3454865e09a4Sgjelinek } 3455865e09a4Sgjelinek } 3456865e09a4Sgjelinek 3457865e09a4Sgjelinek return (B_FALSE); 3458865e09a4Sgjelinek } 3459865e09a4Sgjelinek 34600b5de56dSgjelinek #define RW_LOFS "WARNING: read-write lofs file system on '%s' is configured " \ 3461865e09a4Sgjelinek "in both zones.\n" 3462865e09a4Sgjelinek 3463865e09a4Sgjelinek static void 3464865e09a4Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab) 3465865e09a4Sgjelinek { 3466865e09a4Sgjelinek /* 3467865e09a4Sgjelinek * It is ok to have shared lofs mounted fs but we want to warn if 3468865e09a4Sgjelinek * either is rw since this will effect the other zone. 3469865e09a4Sgjelinek */ 3470865e09a4Sgjelinek if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) { 3471865e09a4Sgjelinek zone_fsopt_t *optp; 3472865e09a4Sgjelinek 3473865e09a4Sgjelinek /* The default is rw so no options means rw */ 3474865e09a4Sgjelinek if (t_fstab->zone_fs_options == NULL || 3475865e09a4Sgjelinek s_fstab->zone_fs_options == NULL) { 3476865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 3477865e09a4Sgjelinek t_fstab->zone_fs_special); 3478865e09a4Sgjelinek return; 3479865e09a4Sgjelinek } 3480865e09a4Sgjelinek 3481865e09a4Sgjelinek for (optp = s_fstab->zone_fs_options; optp != NULL; 3482865e09a4Sgjelinek optp = optp->zone_fsopt_next) { 3483865e09a4Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 3484865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 3485865e09a4Sgjelinek s_fstab->zone_fs_special); 3486865e09a4Sgjelinek return; 3487865e09a4Sgjelinek } 3488865e09a4Sgjelinek } 3489865e09a4Sgjelinek 3490865e09a4Sgjelinek for (optp = t_fstab->zone_fs_options; optp != NULL; 3491865e09a4Sgjelinek optp = optp->zone_fsopt_next) { 3492865e09a4Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 3493865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 3494865e09a4Sgjelinek t_fstab->zone_fs_special); 3495865e09a4Sgjelinek return; 3496865e09a4Sgjelinek } 3497865e09a4Sgjelinek } 3498865e09a4Sgjelinek 3499865e09a4Sgjelinek return; 3500865e09a4Sgjelinek } 3501865e09a4Sgjelinek 3502865e09a4Sgjelinek /* 3503865e09a4Sgjelinek * TRANSLATION_NOTE 35040b5de56dSgjelinek * The first variable is the file system type and the second is 35050b5de56dSgjelinek * the file system special device. For example, 35060b5de56dSgjelinek * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ... 3507865e09a4Sgjelinek */ 35080b5de56dSgjelinek (void) fprintf(stderr, gettext("WARNING: %s file system on '%s' " 3509865e09a4Sgjelinek "is configured in both zones.\n"), t_fstab->zone_fs_type, 3510865e09a4Sgjelinek t_fstab->zone_fs_special); 3511865e09a4Sgjelinek } 3512865e09a4Sgjelinek 3513865e09a4Sgjelinek static void 3514865e09a4Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone, 3515865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3516865e09a4Sgjelinek { 3517865e09a4Sgjelinek int err; 3518865e09a4Sgjelinek struct zone_fstab s_fstab; 3519865e09a4Sgjelinek struct zone_fstab t_fstab; 3520865e09a4Sgjelinek 3521865e09a4Sgjelinek if ((err = zonecfg_setfsent(t_handle)) != Z_OK) { 3522865e09a4Sgjelinek errno = err; 3523865e09a4Sgjelinek zperror2(target_zone, 35240b5de56dSgjelinek gettext("could not enumerate file systems")); 3525865e09a4Sgjelinek return; 3526865e09a4Sgjelinek } 3527865e09a4Sgjelinek 3528865e09a4Sgjelinek while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) { 3529865e09a4Sgjelinek if ((err = zonecfg_setfsent(s_handle)) != Z_OK) { 3530865e09a4Sgjelinek errno = err; 3531865e09a4Sgjelinek zperror2(source_zone, 35320b5de56dSgjelinek gettext("could not enumerate file systems")); 3533865e09a4Sgjelinek (void) zonecfg_endfsent(t_handle); 3534865e09a4Sgjelinek return; 3535865e09a4Sgjelinek } 3536865e09a4Sgjelinek 3537865e09a4Sgjelinek while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) { 3538865e09a4Sgjelinek if (strcmp(t_fstab.zone_fs_special, 3539865e09a4Sgjelinek s_fstab.zone_fs_special) == 0) { 3540865e09a4Sgjelinek print_fs_warnings(&s_fstab, &t_fstab); 3541865e09a4Sgjelinek break; 3542865e09a4Sgjelinek } 3543865e09a4Sgjelinek } 3544865e09a4Sgjelinek (void) zonecfg_endfsent(s_handle); 3545865e09a4Sgjelinek } 3546865e09a4Sgjelinek 3547865e09a4Sgjelinek (void) zonecfg_endfsent(t_handle); 3548865e09a4Sgjelinek } 3549865e09a4Sgjelinek 3550865e09a4Sgjelinek /* 3551865e09a4Sgjelinek * We don't catch the case where you used the same IP address but 3552865e09a4Sgjelinek * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128. 3553865e09a4Sgjelinek * However, we're not going to worry about that but we will check for 3554865e09a4Sgjelinek * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24) 3555865e09a4Sgjelinek * and handle that case as a match. 3556865e09a4Sgjelinek */ 3557865e09a4Sgjelinek static void 3558865e09a4Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone, 3559865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3560865e09a4Sgjelinek { 3561865e09a4Sgjelinek int err; 3562865e09a4Sgjelinek struct zone_nwiftab s_nwiftab; 3563865e09a4Sgjelinek struct zone_nwiftab t_nwiftab; 3564865e09a4Sgjelinek 3565865e09a4Sgjelinek if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) { 3566865e09a4Sgjelinek errno = err; 3567865e09a4Sgjelinek zperror2(target_zone, 3568865e09a4Sgjelinek gettext("could not enumerate network interfaces")); 3569865e09a4Sgjelinek return; 3570865e09a4Sgjelinek } 3571865e09a4Sgjelinek 3572865e09a4Sgjelinek while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) { 3573865e09a4Sgjelinek char *p; 3574865e09a4Sgjelinek 3575865e09a4Sgjelinek /* remove an (optional) netmask from the address */ 3576865e09a4Sgjelinek if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL) 3577865e09a4Sgjelinek *p = '\0'; 3578865e09a4Sgjelinek 3579865e09a4Sgjelinek if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) { 3580865e09a4Sgjelinek errno = err; 3581865e09a4Sgjelinek zperror2(source_zone, 3582865e09a4Sgjelinek gettext("could not enumerate network interfaces")); 3583865e09a4Sgjelinek (void) zonecfg_endnwifent(t_handle); 3584865e09a4Sgjelinek return; 3585865e09a4Sgjelinek } 3586865e09a4Sgjelinek 3587865e09a4Sgjelinek while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) { 3588865e09a4Sgjelinek /* remove an (optional) netmask from the address */ 3589865e09a4Sgjelinek if ((p = strchr(s_nwiftab.zone_nwif_address, '/')) 3590865e09a4Sgjelinek != NULL) 3591865e09a4Sgjelinek *p = '\0'; 3592865e09a4Sgjelinek 3593f4b3ec61Sdh155122 /* For exclusive-IP zones, address is not specified. */ 3594f4b3ec61Sdh155122 if (strlen(s_nwiftab.zone_nwif_address) == 0) 3595f4b3ec61Sdh155122 continue; 3596f4b3ec61Sdh155122 3597865e09a4Sgjelinek if (strcmp(t_nwiftab.zone_nwif_address, 3598865e09a4Sgjelinek s_nwiftab.zone_nwif_address) == 0) { 3599865e09a4Sgjelinek (void) fprintf(stderr, 3600865e09a4Sgjelinek gettext("WARNING: network address '%s' " 3601865e09a4Sgjelinek "is configured in both zones.\n"), 3602865e09a4Sgjelinek t_nwiftab.zone_nwif_address); 3603865e09a4Sgjelinek break; 3604865e09a4Sgjelinek } 3605865e09a4Sgjelinek } 3606865e09a4Sgjelinek (void) zonecfg_endnwifent(s_handle); 3607865e09a4Sgjelinek } 3608865e09a4Sgjelinek 3609865e09a4Sgjelinek (void) zonecfg_endnwifent(t_handle); 3610865e09a4Sgjelinek } 3611865e09a4Sgjelinek 3612865e09a4Sgjelinek static void 36139acbbeafSnn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source, 36149acbbeafSnn35248 zone_dochandle_t t_handle, char *target) 3615865e09a4Sgjelinek { 3616865e09a4Sgjelinek int err; 3617865e09a4Sgjelinek struct zone_dstab s_dstab; 3618865e09a4Sgjelinek struct zone_dstab t_dstab; 3619865e09a4Sgjelinek 3620865e09a4Sgjelinek if ((err = zonecfg_setdsent(t_handle)) != Z_OK) { 3621865e09a4Sgjelinek errno = err; 36229acbbeafSnn35248 zperror2(target, gettext("could not enumerate datasets")); 3623865e09a4Sgjelinek return; 3624865e09a4Sgjelinek } 3625865e09a4Sgjelinek 3626865e09a4Sgjelinek while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) { 3627865e09a4Sgjelinek if ((err = zonecfg_setdsent(s_handle)) != Z_OK) { 3628865e09a4Sgjelinek errno = err; 36299acbbeafSnn35248 zperror2(source, 3630865e09a4Sgjelinek gettext("could not enumerate datasets")); 3631865e09a4Sgjelinek (void) zonecfg_enddsent(t_handle); 3632865e09a4Sgjelinek return; 3633865e09a4Sgjelinek } 3634865e09a4Sgjelinek 3635865e09a4Sgjelinek while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) { 3636865e09a4Sgjelinek if (strcmp(t_dstab.zone_dataset_name, 3637865e09a4Sgjelinek s_dstab.zone_dataset_name) == 0) { 36389acbbeafSnn35248 target_zone = source; 36399acbbeafSnn35248 zerror(gettext("WARNING: dataset '%s' " 3640865e09a4Sgjelinek "is configured in both zones.\n"), 3641865e09a4Sgjelinek t_dstab.zone_dataset_name); 3642865e09a4Sgjelinek break; 3643865e09a4Sgjelinek } 3644865e09a4Sgjelinek } 3645865e09a4Sgjelinek (void) zonecfg_enddsent(s_handle); 3646865e09a4Sgjelinek } 3647865e09a4Sgjelinek 3648865e09a4Sgjelinek (void) zonecfg_enddsent(t_handle); 3649865e09a4Sgjelinek } 3650865e09a4Sgjelinek 36519acbbeafSnn35248 /* 36529acbbeafSnn35248 * Check that the clone and its source have the same brand type. 36539acbbeafSnn35248 */ 36549acbbeafSnn35248 static int 36559acbbeafSnn35248 valid_brand_clone(char *source_zone, char *target_zone) 36569acbbeafSnn35248 { 3657123807fbSedp brand_handle_t bh; 36589acbbeafSnn35248 char source_brand[MAXNAMELEN]; 36599acbbeafSnn35248 36609acbbeafSnn35248 if ((zone_get_brand(source_zone, source_brand, 36619acbbeafSnn35248 sizeof (source_brand))) != Z_OK) { 36629acbbeafSnn35248 (void) fprintf(stderr, "%s: zone '%s': %s\n", 36639acbbeafSnn35248 execname, source_zone, gettext("missing or invalid brand")); 36649acbbeafSnn35248 return (Z_ERR); 36659acbbeafSnn35248 } 36669acbbeafSnn35248 36679acbbeafSnn35248 if (strcmp(source_brand, target_brand) != NULL) { 36689acbbeafSnn35248 (void) fprintf(stderr, 36699acbbeafSnn35248 gettext("%s: Zones '%s' and '%s' have different brand " 36709acbbeafSnn35248 "types.\n"), execname, source_zone, target_zone); 36719acbbeafSnn35248 return (Z_ERR); 36729acbbeafSnn35248 } 36739acbbeafSnn35248 3674123807fbSedp if ((bh = brand_open(target_brand)) == NULL) { 36759acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 36769acbbeafSnn35248 return (Z_ERR); 36779acbbeafSnn35248 } 3678123807fbSedp brand_close(bh); 36799acbbeafSnn35248 return (Z_OK); 36809acbbeafSnn35248 } 36819acbbeafSnn35248 3682865e09a4Sgjelinek static int 3683865e09a4Sgjelinek validate_clone(char *source_zone, char *target_zone) 3684865e09a4Sgjelinek { 3685865e09a4Sgjelinek int err = Z_OK; 3686865e09a4Sgjelinek zone_dochandle_t s_handle; 3687865e09a4Sgjelinek zone_dochandle_t t_handle; 3688865e09a4Sgjelinek 3689865e09a4Sgjelinek if ((t_handle = zonecfg_init_handle()) == NULL) { 3690865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3691865e09a4Sgjelinek return (Z_ERR); 3692865e09a4Sgjelinek } 3693865e09a4Sgjelinek if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) { 3694865e09a4Sgjelinek errno = err; 3695865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3696865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3697865e09a4Sgjelinek return (Z_ERR); 3698865e09a4Sgjelinek } 3699865e09a4Sgjelinek 3700865e09a4Sgjelinek if ((s_handle = zonecfg_init_handle()) == NULL) { 3701865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3702865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3703865e09a4Sgjelinek return (Z_ERR); 3704865e09a4Sgjelinek } 3705865e09a4Sgjelinek if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) { 3706865e09a4Sgjelinek errno = err; 3707865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3708865e09a4Sgjelinek goto done; 3709865e09a4Sgjelinek } 3710865e09a4Sgjelinek 37119acbbeafSnn35248 /* verify new zone has same brand type */ 37129acbbeafSnn35248 err = valid_brand_clone(source_zone, target_zone); 37139acbbeafSnn35248 if (err != Z_OK) 37149acbbeafSnn35248 goto done; 37159acbbeafSnn35248 3716865e09a4Sgjelinek /* verify new zone has same inherit-pkg-dirs */ 3717865e09a4Sgjelinek err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone); 3718865e09a4Sgjelinek 3719865e09a4Sgjelinek /* warn about imported fs's which are the same */ 3720865e09a4Sgjelinek warn_fs_match(s_handle, source_zone, t_handle, target_zone); 3721865e09a4Sgjelinek 3722865e09a4Sgjelinek /* warn about imported IP addresses which are the same */ 3723865e09a4Sgjelinek warn_ip_match(s_handle, source_zone, t_handle, target_zone); 3724865e09a4Sgjelinek 3725865e09a4Sgjelinek /* warn about imported devices which are the same */ 3726865e09a4Sgjelinek warn_dev_match(s_handle, source_zone, t_handle, target_zone); 3727865e09a4Sgjelinek 3728865e09a4Sgjelinek /* warn about imported datasets which are the same */ 3729865e09a4Sgjelinek warn_dataset_match(s_handle, source_zone, t_handle, target_zone); 3730865e09a4Sgjelinek 3731865e09a4Sgjelinek done: 3732865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3733865e09a4Sgjelinek zonecfg_fini_handle(s_handle); 3734865e09a4Sgjelinek 3735865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3736865e09a4Sgjelinek } 3737865e09a4Sgjelinek 3738865e09a4Sgjelinek static int 3739865e09a4Sgjelinek copy_zone(char *src, char *dst) 3740865e09a4Sgjelinek { 3741865e09a4Sgjelinek boolean_t out_null = B_FALSE; 3742865e09a4Sgjelinek int status; 3743865e09a4Sgjelinek char *outfile; 3744865e09a4Sgjelinek char cmdbuf[MAXPATHLEN * 2 + 128]; 3745865e09a4Sgjelinek 3746865e09a4Sgjelinek if ((outfile = tempnam("/var/log", "zone")) == NULL) { 3747865e09a4Sgjelinek outfile = "/dev/null"; 3748865e09a4Sgjelinek out_null = B_TRUE; 3749865e09a4Sgjelinek } 3750865e09a4Sgjelinek 37510b5de56dSgjelinek /* 37520b5de56dSgjelinek * Use find to get the list of files to copy. We need to skip 37530b5de56dSgjelinek * files of type "socket" since cpio can't handle those but that 37540b5de56dSgjelinek * should be ok since the app will recreate the socket when it runs. 37550b5de56dSgjelinek * We also need to filter out anything under the .zfs subdir. Since 37560b5de56dSgjelinek * find is running depth-first, we need the extra egrep to filter .zfs. 37570b5de56dSgjelinek */ 3758865e09a4Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 37590b5de56dSgjelinek "cd %s && /usr/bin/find . -type s -prune -o -depth -print | " 376007b574eeSgjelinek "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | " 3761865e09a4Sgjelinek "/usr/bin/cpio -pdmuP@ %s > %s 2>&1", 3762865e09a4Sgjelinek src, dst, outfile); 3763865e09a4Sgjelinek 3764865e09a4Sgjelinek status = do_subproc(cmdbuf); 3765865e09a4Sgjelinek 37669acbbeafSnn35248 if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) { 3767865e09a4Sgjelinek if (!out_null) 3768865e09a4Sgjelinek (void) fprintf(stderr, gettext("\nThe copy failed.\n" 3769865e09a4Sgjelinek "More information can be found in %s\n"), outfile); 37709acbbeafSnn35248 return (Z_ERR); 3771865e09a4Sgjelinek } 3772865e09a4Sgjelinek 3773865e09a4Sgjelinek if (!out_null) 3774865e09a4Sgjelinek (void) unlink(outfile); 3775865e09a4Sgjelinek 3776865e09a4Sgjelinek return (Z_OK); 3777865e09a4Sgjelinek } 3778865e09a4Sgjelinek 3779865e09a4Sgjelinek static int 37809acbbeafSnn35248 zone_postclone(char *zonepath) 3781865e09a4Sgjelinek { 37829acbbeafSnn35248 char cmdbuf[MAXPATHLEN]; 3783865e09a4Sgjelinek int status; 3784123807fbSedp brand_handle_t bh; 37859acbbeafSnn35248 int err = Z_OK; 37865cd08338Sgjelinek 37875cd08338Sgjelinek /* 37889acbbeafSnn35248 * Fetch the post-clone command, if any, from the brand 37899acbbeafSnn35248 * configuration. 379045916cd2Sjpk */ 3791123807fbSedp if ((bh = brand_open(target_brand)) == NULL) { 37929acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 37935cd08338Sgjelinek return (Z_ERR); 3794865e09a4Sgjelinek } 37959acbbeafSnn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 3796123807fbSedp err = brand_get_postclone(bh, target_zone, zonepath, cmdbuf + EXEC_LEN, 37979acbbeafSnn35248 sizeof (cmdbuf) - EXEC_LEN, 0, NULL); 3798123807fbSedp brand_close(bh); 3799865e09a4Sgjelinek 38009acbbeafSnn35248 if (err == 0 && strlen(cmdbuf) > EXEC_LEN) { 38015cd08338Sgjelinek status = do_subproc(cmdbuf); 38029acbbeafSnn35248 if ((err = subproc_status("postclone", status, B_FALSE)) 38039acbbeafSnn35248 != ZONE_SUBPROC_OK) { 38049acbbeafSnn35248 zerror(gettext("post-clone configuration failed.")); 38059acbbeafSnn35248 err = Z_ERR; 38069acbbeafSnn35248 } 3807865e09a4Sgjelinek } 3808865e09a4Sgjelinek 38099acbbeafSnn35248 return (err); 38105cd08338Sgjelinek } 3811865e09a4Sgjelinek 3812865e09a4Sgjelinek /* ARGSUSED */ 38130b5de56dSgjelinek static int 3814865e09a4Sgjelinek zfm_print(const char *p, void *r) { 3815865e09a4Sgjelinek zerror(" %s\n", p); 3816865e09a4Sgjelinek return (0); 3817865e09a4Sgjelinek } 3818865e09a4Sgjelinek 38190b5de56dSgjelinek int 38200b5de56dSgjelinek clone_copy(char *source_zonepath, char *zonepath) 38210b5de56dSgjelinek { 38220b5de56dSgjelinek int err; 38230b5de56dSgjelinek 38240b5de56dSgjelinek /* Don't clone the zone if anything is still mounted there */ 38250b5de56dSgjelinek if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) { 38260b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 38270b5de56dSgjelinek "subdirectories of %s.\n"), source_zonepath); 38280b5de56dSgjelinek (void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL); 38290b5de56dSgjelinek return (Z_ERR); 38300b5de56dSgjelinek } 38310b5de56dSgjelinek 38320b5de56dSgjelinek /* 38330b5de56dSgjelinek * Attempt to create a ZFS fs for the zonepath. As usual, we don't 38340b5de56dSgjelinek * care if this works or not since we always have the default behavior 38350b5de56dSgjelinek * of a simple directory for the zonepath. 38360b5de56dSgjelinek */ 38370b5de56dSgjelinek create_zfs_zonepath(zonepath); 38380b5de56dSgjelinek 38390b5de56dSgjelinek (void) printf(gettext("Copying %s..."), source_zonepath); 38400b5de56dSgjelinek (void) fflush(stdout); 38410b5de56dSgjelinek 38420b5de56dSgjelinek err = copy_zone(source_zonepath, zonepath); 38430b5de56dSgjelinek 38440b5de56dSgjelinek (void) printf("\n"); 38450b5de56dSgjelinek 38460b5de56dSgjelinek return (err); 38470b5de56dSgjelinek } 38480b5de56dSgjelinek 3849865e09a4Sgjelinek static int 3850865e09a4Sgjelinek clone_func(int argc, char *argv[]) 3851865e09a4Sgjelinek { 3852865e09a4Sgjelinek char *source_zone = NULL; 3853865e09a4Sgjelinek int lockfd; 3854865e09a4Sgjelinek int err, arg; 3855865e09a4Sgjelinek char zonepath[MAXPATHLEN]; 3856865e09a4Sgjelinek char source_zonepath[MAXPATHLEN]; 3857865e09a4Sgjelinek zone_state_t state; 3858865e09a4Sgjelinek zone_entry_t *zent; 38590b5de56dSgjelinek char *method = NULL; 38600b5de56dSgjelinek char *snapshot = NULL; 3861865e09a4Sgjelinek 3862865e09a4Sgjelinek if (zonecfg_in_alt_root()) { 3863865e09a4Sgjelinek zerror(gettext("cannot clone zone in alternate root")); 3864865e09a4Sgjelinek return (Z_ERR); 3865865e09a4Sgjelinek } 3866865e09a4Sgjelinek 3867865e09a4Sgjelinek optind = 0; 38680b5de56dSgjelinek if ((arg = getopt(argc, argv, "?m:s:")) != EOF) { 3869865e09a4Sgjelinek switch (arg) { 3870865e09a4Sgjelinek case '?': 3871865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3872865e09a4Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 3873865e09a4Sgjelinek case 'm': 3874865e09a4Sgjelinek method = optarg; 3875865e09a4Sgjelinek break; 38760b5de56dSgjelinek case 's': 38770b5de56dSgjelinek snapshot = optarg; 38780b5de56dSgjelinek break; 3879865e09a4Sgjelinek default: 3880865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3881865e09a4Sgjelinek return (Z_USAGE); 3882865e09a4Sgjelinek } 3883865e09a4Sgjelinek } 38840b5de56dSgjelinek if (argc != (optind + 1) || 38850b5de56dSgjelinek (method != NULL && strcmp(method, "copy") != 0)) { 3886865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3887865e09a4Sgjelinek return (Z_USAGE); 3888865e09a4Sgjelinek } 3889865e09a4Sgjelinek source_zone = argv[optind]; 38909acbbeafSnn35248 if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE, B_FALSE) 38919acbbeafSnn35248 != Z_OK) 3892865e09a4Sgjelinek return (Z_ERR); 3893ce28b40eSzt129084 if (verify_details(CMD_CLONE, argv) != Z_OK) 3894865e09a4Sgjelinek return (Z_ERR); 3895865e09a4Sgjelinek 3896865e09a4Sgjelinek /* 3897865e09a4Sgjelinek * We also need to do some extra validation on the source zone. 3898865e09a4Sgjelinek */ 3899865e09a4Sgjelinek if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) { 3900865e09a4Sgjelinek zerror(gettext("%s operation is invalid for the global zone."), 3901865e09a4Sgjelinek cmd_to_str(CMD_CLONE)); 3902865e09a4Sgjelinek return (Z_ERR); 3903865e09a4Sgjelinek } 3904865e09a4Sgjelinek 3905865e09a4Sgjelinek if (strncmp(source_zone, "SUNW", 4) == 0) { 3906865e09a4Sgjelinek zerror(gettext("%s operation is invalid for zones starting " 3907865e09a4Sgjelinek "with SUNW."), cmd_to_str(CMD_CLONE)); 3908865e09a4Sgjelinek return (Z_ERR); 3909865e09a4Sgjelinek } 3910865e09a4Sgjelinek 3911865e09a4Sgjelinek zent = lookup_running_zone(source_zone); 3912865e09a4Sgjelinek if (zent != NULL) { 3913865e09a4Sgjelinek /* check whether the zone is ready or running */ 3914865e09a4Sgjelinek if ((err = zone_get_state(zent->zname, &zent->zstate_num)) 3915865e09a4Sgjelinek != Z_OK) { 3916865e09a4Sgjelinek errno = err; 3917865e09a4Sgjelinek zperror2(zent->zname, gettext("could not get state")); 3918865e09a4Sgjelinek /* can't tell, so hedge */ 3919865e09a4Sgjelinek zent->zstate_str = "ready/running"; 3920865e09a4Sgjelinek } else { 3921865e09a4Sgjelinek zent->zstate_str = zone_state_str(zent->zstate_num); 3922865e09a4Sgjelinek } 3923865e09a4Sgjelinek zerror(gettext("%s operation is invalid for %s zones."), 3924865e09a4Sgjelinek cmd_to_str(CMD_CLONE), zent->zstate_str); 3925865e09a4Sgjelinek return (Z_ERR); 3926865e09a4Sgjelinek } 3927865e09a4Sgjelinek 3928865e09a4Sgjelinek if ((err = zone_get_state(source_zone, &state)) != Z_OK) { 3929865e09a4Sgjelinek errno = err; 3930865e09a4Sgjelinek zperror2(source_zone, gettext("could not get state")); 3931865e09a4Sgjelinek return (Z_ERR); 3932865e09a4Sgjelinek } 3933865e09a4Sgjelinek if (state != ZONE_STATE_INSTALLED) { 3934865e09a4Sgjelinek (void) fprintf(stderr, 3935865e09a4Sgjelinek gettext("%s: zone %s is %s; %s is required.\n"), 3936865e09a4Sgjelinek execname, source_zone, zone_state_str(state), 3937865e09a4Sgjelinek zone_state_str(ZONE_STATE_INSTALLED)); 3938865e09a4Sgjelinek return (Z_ERR); 3939865e09a4Sgjelinek } 3940865e09a4Sgjelinek 3941865e09a4Sgjelinek /* 3942865e09a4Sgjelinek * The source zone checks out ok, continue with the clone. 3943865e09a4Sgjelinek */ 3944865e09a4Sgjelinek 3945865e09a4Sgjelinek if (validate_clone(source_zone, target_zone) != Z_OK) 3946865e09a4Sgjelinek return (Z_ERR); 3947865e09a4Sgjelinek 3948865e09a4Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 3949865e09a4Sgjelinek zerror(gettext("another %s may have an operation in progress."), 3950865e09a4Sgjelinek "zoneadm"); 3951865e09a4Sgjelinek return (Z_ERR); 3952865e09a4Sgjelinek } 3953865e09a4Sgjelinek 3954865e09a4Sgjelinek if ((err = zone_get_zonepath(source_zone, source_zonepath, 3955865e09a4Sgjelinek sizeof (source_zonepath))) != Z_OK) { 3956865e09a4Sgjelinek errno = err; 3957865e09a4Sgjelinek zperror2(source_zone, gettext("could not get zone path")); 3958865e09a4Sgjelinek goto done; 3959865e09a4Sgjelinek } 3960865e09a4Sgjelinek 3961865e09a4Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 3962865e09a4Sgjelinek != Z_OK) { 3963865e09a4Sgjelinek errno = err; 3964865e09a4Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 3965865e09a4Sgjelinek goto done; 3966865e09a4Sgjelinek } 3967865e09a4Sgjelinek 3968865e09a4Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE)) 3969865e09a4Sgjelinek != Z_OK) { 3970865e09a4Sgjelinek errno = err; 3971865e09a4Sgjelinek zperror2(target_zone, gettext("could not set state")); 3972865e09a4Sgjelinek goto done; 3973865e09a4Sgjelinek } 3974865e09a4Sgjelinek 39750b5de56dSgjelinek if (snapshot != NULL) { 39760b5de56dSgjelinek err = clone_snapshot_zfs(snapshot, zonepath); 39770b5de56dSgjelinek } else { 39780b5de56dSgjelinek /* 39790b5de56dSgjelinek * We always copy the clone unless the source is ZFS and a 39800b5de56dSgjelinek * ZFS clone worked. We fallback to copying if the ZFS clone 39810b5de56dSgjelinek * fails for some reason. 39820b5de56dSgjelinek */ 39830b5de56dSgjelinek err = Z_ERR; 39840b5de56dSgjelinek if (method == NULL && is_zonepath_zfs(source_zonepath)) 39850b5de56dSgjelinek err = clone_zfs(source_zone, source_zonepath, zonepath); 3986865e09a4Sgjelinek 39875cd08338Sgjelinek if (err != Z_OK) 39880b5de56dSgjelinek err = clone_copy(source_zonepath, zonepath); 39890b5de56dSgjelinek } 3990865e09a4Sgjelinek 39919acbbeafSnn35248 /* 39929acbbeafSnn35248 * Trusted Extensions requires that cloned zones use the same sysid 39939acbbeafSnn35248 * configuration, so it is not appropriate to perform any 39949acbbeafSnn35248 * post-clone reconfiguration. 39959acbbeafSnn35248 */ 39969acbbeafSnn35248 if ((err == Z_OK) && !is_system_labeled()) 39979acbbeafSnn35248 err = zone_postclone(zonepath); 3998865e09a4Sgjelinek 3999865e09a4Sgjelinek done: 40009acbbeafSnn35248 /* 40019acbbeafSnn35248 * If everything went well, we mark the zone as installed. 40029acbbeafSnn35248 */ 40039acbbeafSnn35248 if (err == Z_OK) { 40049acbbeafSnn35248 err = zone_set_state(target_zone, ZONE_STATE_INSTALLED); 40059acbbeafSnn35248 if (err != Z_OK) { 40069acbbeafSnn35248 errno = err; 40079acbbeafSnn35248 zperror2(target_zone, gettext("could not set state")); 40089acbbeafSnn35248 } 40099acbbeafSnn35248 } 4010865e09a4Sgjelinek release_lock_file(lockfd); 4011865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 4012865e09a4Sgjelinek } 4013865e09a4Sgjelinek 401407b574eeSgjelinek /* 40150b5de56dSgjelinek * Used when removing a zonepath after uninstalling or cleaning up after 40160b5de56dSgjelinek * the move subcommand. This handles a zonepath that has non-standard 40170b5de56dSgjelinek * contents so that we will only cleanup the stuff we know about and leave 40180b5de56dSgjelinek * any user data alone. 401907b574eeSgjelinek * 40200b5de56dSgjelinek * If the "all" parameter is true then we should remove the whole zonepath 40210b5de56dSgjelinek * even if it has non-standard files/directories in it. This can be used when 40220b5de56dSgjelinek * we need to cleanup after moving the zonepath across file systems. 40230b5de56dSgjelinek * 40240b5de56dSgjelinek * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND 40250b5de56dSgjelinek * and not the shell. 402607b574eeSgjelinek */ 402707b574eeSgjelinek static int 40280b5de56dSgjelinek cleanup_zonepath(char *zonepath, boolean_t all) 402907b574eeSgjelinek { 403007b574eeSgjelinek int status; 40310b5de56dSgjelinek int i; 40320b5de56dSgjelinek boolean_t non_std = B_FALSE; 40330b5de56dSgjelinek struct dirent *dp; 40340b5de56dSgjelinek DIR *dirp; 4035*d9e728a2Sgjelinek /* 4036*d9e728a2Sgjelinek * The SUNWattached.xml file is expected since it might 4037*d9e728a2Sgjelinek * exist if the zone was force-attached after a 4038*d9e728a2Sgjelinek * migration. 4039*d9e728a2Sgjelinek */ 4040*d9e728a2Sgjelinek char *std_entries[] = {"dev", "lu", "root", 4041*d9e728a2Sgjelinek "SUNWattached.xml", NULL}; 40420b5de56dSgjelinek /* (MAXPATHLEN * 3) is for the 3 std_entries dirs */ 40430b5de56dSgjelinek char cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64]; 404407b574eeSgjelinek 404507b574eeSgjelinek /* 40460b5de56dSgjelinek * We shouldn't need these checks but lets be paranoid since we 40470b5de56dSgjelinek * could blow away the whole system here if we got the wrong zonepath. 404807b574eeSgjelinek */ 40490b5de56dSgjelinek if (*zonepath == NULL || strcmp(zonepath, "/") == 0) { 40500b5de56dSgjelinek (void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath); 40510b5de56dSgjelinek return (Z_INVAL); 40520b5de56dSgjelinek } 40530b5de56dSgjelinek 405407b574eeSgjelinek /* 40550b5de56dSgjelinek * If the dirpath is already gone (maybe it was manually removed) then 40560b5de56dSgjelinek * we just return Z_OK so that the cleanup is successful. 405707b574eeSgjelinek */ 40580b5de56dSgjelinek if ((dirp = opendir(zonepath)) == NULL) 40590b5de56dSgjelinek return (Z_OK); 40600b5de56dSgjelinek 40610b5de56dSgjelinek /* 40620b5de56dSgjelinek * Look through the zonepath directory to see if there are any 40630b5de56dSgjelinek * non-standard files/dirs. Also skip .zfs since that might be 40640b5de56dSgjelinek * there but we'll handle ZFS file systems as a special case. 40650b5de56dSgjelinek */ 40660b5de56dSgjelinek while ((dp = readdir(dirp)) != NULL) { 40670b5de56dSgjelinek if (strcmp(dp->d_name, ".") == 0 || 40680b5de56dSgjelinek strcmp(dp->d_name, "..") == 0 || 40690b5de56dSgjelinek strcmp(dp->d_name, ".zfs") == 0) 40700b5de56dSgjelinek continue; 40710b5de56dSgjelinek 40720b5de56dSgjelinek for (i = 0; std_entries[i] != NULL; i++) 40730b5de56dSgjelinek if (strcmp(dp->d_name, std_entries[i]) == 0) 40740b5de56dSgjelinek break; 40750b5de56dSgjelinek 40760b5de56dSgjelinek if (std_entries[i] == NULL) 40770b5de56dSgjelinek non_std = B_TRUE; 40780b5de56dSgjelinek } 40790b5de56dSgjelinek (void) closedir(dirp); 40800b5de56dSgjelinek 40810b5de56dSgjelinek if (!all && non_std) { 40820b5de56dSgjelinek /* 40830b5de56dSgjelinek * There are extra, non-standard directories/files in the 40840b5de56dSgjelinek * zonepath so we don't want to remove the zonepath. We 40850b5de56dSgjelinek * just want to remove the standard directories and leave 40860b5de56dSgjelinek * the user data alone. 40870b5de56dSgjelinek */ 40880b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND); 40890b5de56dSgjelinek 40900b5de56dSgjelinek for (i = 0; std_entries[i] != NULL; i++) { 40910b5de56dSgjelinek char tmpbuf[MAXPATHLEN]; 40920b5de56dSgjelinek 40930b5de56dSgjelinek if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s", 40940b5de56dSgjelinek zonepath, std_entries[i]) >= sizeof (tmpbuf) || 40950b5de56dSgjelinek strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >= 40960b5de56dSgjelinek sizeof (cmdbuf)) { 40970b5de56dSgjelinek (void) fprintf(stderr, 40980b5de56dSgjelinek gettext("path is too long\n")); 40990b5de56dSgjelinek return (Z_INVAL); 41000b5de56dSgjelinek } 410107b574eeSgjelinek } 410207b574eeSgjelinek 410307b574eeSgjelinek status = do_subproc(cmdbuf); 410407b574eeSgjelinek 41050b5de56dSgjelinek (void) fprintf(stderr, gettext("WARNING: Unable to completely " 41060b5de56dSgjelinek "remove %s\nbecause it contains additional user data. " 41070b5de56dSgjelinek "Only the standard directory\nentries have been " 41080b5de56dSgjelinek "removed.\n"), 41090b5de56dSgjelinek zonepath); 411007b574eeSgjelinek 41119acbbeafSnn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == 41129acbbeafSnn35248 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR); 41130b5de56dSgjelinek } 41140b5de56dSgjelinek 41150b5de56dSgjelinek /* 41160b5de56dSgjelinek * There is nothing unexpected in the zonepath, try to get rid of the 41170b5de56dSgjelinek * whole zonepath directory. 41180b5de56dSgjelinek * 41190b5de56dSgjelinek * If the zonepath is its own zfs file system, try to destroy the 41200b5de56dSgjelinek * file system. If that fails for some reason (e.g. it has clones) 41210b5de56dSgjelinek * then we'll just remove the contents of the zonepath. 41220b5de56dSgjelinek */ 41230b5de56dSgjelinek if (is_zonepath_zfs(zonepath)) { 41240b5de56dSgjelinek if (destroy_zfs(zonepath) == Z_OK) 41250b5de56dSgjelinek return (Z_OK); 41260b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND 41270b5de56dSgjelinek " %s/*", zonepath); 41280b5de56dSgjelinek status = do_subproc(cmdbuf); 41299acbbeafSnn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == 41309acbbeafSnn35248 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR); 41310b5de56dSgjelinek } 41320b5de56dSgjelinek 41330b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 41340b5de56dSgjelinek zonepath); 41350b5de56dSgjelinek status = do_subproc(cmdbuf); 41369acbbeafSnn35248 41379acbbeafSnn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK) 41389acbbeafSnn35248 ? Z_OK : Z_ERR); 413907b574eeSgjelinek } 414007b574eeSgjelinek 4141865e09a4Sgjelinek static int 4142865e09a4Sgjelinek move_func(int argc, char *argv[]) 4143865e09a4Sgjelinek { 4144865e09a4Sgjelinek char *new_zonepath = NULL; 4145865e09a4Sgjelinek int lockfd; 4146865e09a4Sgjelinek int err, arg; 4147865e09a4Sgjelinek char zonepath[MAXPATHLEN]; 4148865e09a4Sgjelinek zone_dochandle_t handle; 4149865e09a4Sgjelinek boolean_t fast; 41500b5de56dSgjelinek boolean_t is_zfs = B_FALSE; 41510b5de56dSgjelinek struct dirent *dp; 41520b5de56dSgjelinek DIR *dirp; 41530b5de56dSgjelinek boolean_t empty = B_TRUE; 4154865e09a4Sgjelinek boolean_t revert; 4155865e09a4Sgjelinek struct stat zonepath_buf; 4156865e09a4Sgjelinek struct stat new_zonepath_buf; 4157865e09a4Sgjelinek 4158865e09a4Sgjelinek if (zonecfg_in_alt_root()) { 4159865e09a4Sgjelinek zerror(gettext("cannot move zone in alternate root")); 4160865e09a4Sgjelinek return (Z_ERR); 4161865e09a4Sgjelinek } 4162865e09a4Sgjelinek 4163865e09a4Sgjelinek optind = 0; 4164865e09a4Sgjelinek if ((arg = getopt(argc, argv, "?")) != EOF) { 4165865e09a4Sgjelinek switch (arg) { 4166865e09a4Sgjelinek case '?': 4167865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 4168865e09a4Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 4169865e09a4Sgjelinek default: 4170865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 4171865e09a4Sgjelinek return (Z_USAGE); 4172865e09a4Sgjelinek } 4173865e09a4Sgjelinek } 4174865e09a4Sgjelinek if (argc != (optind + 1)) { 4175865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 4176865e09a4Sgjelinek return (Z_USAGE); 4177865e09a4Sgjelinek } 4178865e09a4Sgjelinek new_zonepath = argv[optind]; 41799acbbeafSnn35248 if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE) 41809acbbeafSnn35248 != Z_OK) 4181865e09a4Sgjelinek return (Z_ERR); 4182ce28b40eSzt129084 if (verify_details(CMD_MOVE, argv) != Z_OK) 4183865e09a4Sgjelinek return (Z_ERR); 4184865e09a4Sgjelinek 4185865e09a4Sgjelinek /* 4186865e09a4Sgjelinek * Check out the new zonepath. This has the side effect of creating 4187865e09a4Sgjelinek * a directory for the new zonepath. We depend on this later when we 41880b5de56dSgjelinek * stat to see if we are doing a cross file system move or not. 4189865e09a4Sgjelinek */ 4190865e09a4Sgjelinek if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK) 4191865e09a4Sgjelinek return (Z_ERR); 4192865e09a4Sgjelinek 4193865e09a4Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 4194865e09a4Sgjelinek != Z_OK) { 4195865e09a4Sgjelinek errno = err; 4196865e09a4Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 4197865e09a4Sgjelinek return (Z_ERR); 4198865e09a4Sgjelinek } 4199865e09a4Sgjelinek 4200865e09a4Sgjelinek if (stat(zonepath, &zonepath_buf) == -1) { 4201865e09a4Sgjelinek zperror(gettext("could not stat zone path"), B_FALSE); 4202865e09a4Sgjelinek return (Z_ERR); 4203865e09a4Sgjelinek } 4204865e09a4Sgjelinek 4205865e09a4Sgjelinek if (stat(new_zonepath, &new_zonepath_buf) == -1) { 4206865e09a4Sgjelinek zperror(gettext("could not stat new zone path"), B_FALSE); 4207865e09a4Sgjelinek return (Z_ERR); 4208865e09a4Sgjelinek } 4209865e09a4Sgjelinek 42100b5de56dSgjelinek /* 42110b5de56dSgjelinek * Check if the destination directory is empty. 42120b5de56dSgjelinek */ 42130b5de56dSgjelinek if ((dirp = opendir(new_zonepath)) == NULL) { 42140b5de56dSgjelinek zperror(gettext("could not open new zone path"), B_FALSE); 42150b5de56dSgjelinek return (Z_ERR); 42160b5de56dSgjelinek } 42170b5de56dSgjelinek while ((dp = readdir(dirp)) != (struct dirent *)0) { 42180b5de56dSgjelinek if (strcmp(dp->d_name, ".") == 0 || 42190b5de56dSgjelinek strcmp(dp->d_name, "..") == 0) 42200b5de56dSgjelinek continue; 42210b5de56dSgjelinek empty = B_FALSE; 42220b5de56dSgjelinek break; 42230b5de56dSgjelinek } 42240b5de56dSgjelinek (void) closedir(dirp); 42250b5de56dSgjelinek 42260b5de56dSgjelinek /* Error if there is anything in the destination directory. */ 42270b5de56dSgjelinek if (!empty) { 42280b5de56dSgjelinek (void) fprintf(stderr, gettext("could not move zone to %s: " 42290b5de56dSgjelinek "directory not empty\n"), new_zonepath); 42300b5de56dSgjelinek return (Z_ERR); 42310b5de56dSgjelinek } 42320b5de56dSgjelinek 4233865e09a4Sgjelinek /* Don't move the zone if anything is still mounted there */ 4234865e09a4Sgjelinek if (zonecfg_find_mounts(zonepath, NULL, NULL)) { 42350b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 4236865e09a4Sgjelinek "subdirectories of %s.\n"), zonepath); 4237865e09a4Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 4238865e09a4Sgjelinek return (Z_ERR); 4239865e09a4Sgjelinek } 4240865e09a4Sgjelinek 4241865e09a4Sgjelinek /* 4242865e09a4Sgjelinek * Check if we are moving in the same file system and can do a fast 4243865e09a4Sgjelinek * move or if we are crossing file systems and have to copy the data. 4244865e09a4Sgjelinek */ 4245865e09a4Sgjelinek fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev); 4246865e09a4Sgjelinek 4247865e09a4Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 4248865e09a4Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 4249865e09a4Sgjelinek return (Z_ERR); 4250865e09a4Sgjelinek } 4251865e09a4Sgjelinek 4252865e09a4Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 4253865e09a4Sgjelinek errno = err; 4254865e09a4Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 4255865e09a4Sgjelinek zonecfg_fini_handle(handle); 4256865e09a4Sgjelinek return (Z_ERR); 4257865e09a4Sgjelinek } 4258865e09a4Sgjelinek 4259865e09a4Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 4260865e09a4Sgjelinek zerror(gettext("another %s may have an operation in progress."), 4261865e09a4Sgjelinek "zoneadm"); 4262865e09a4Sgjelinek zonecfg_fini_handle(handle); 4263865e09a4Sgjelinek return (Z_ERR); 4264865e09a4Sgjelinek } 4265865e09a4Sgjelinek 4266865e09a4Sgjelinek /* 42670b5de56dSgjelinek * We're making some file system changes now so we have to clean up 42680b5de56dSgjelinek * the file system before we are done. This will either clean up the 4269865e09a4Sgjelinek * new zonepath if the zonecfg update failed or it will clean up the 4270865e09a4Sgjelinek * old zonepath if everything is ok. 4271865e09a4Sgjelinek */ 4272865e09a4Sgjelinek revert = B_TRUE; 4273865e09a4Sgjelinek 42740b5de56dSgjelinek if (is_zonepath_zfs(zonepath) && 42750b5de56dSgjelinek move_zfs(zonepath, new_zonepath) != Z_ERR) { 42760b5de56dSgjelinek is_zfs = B_TRUE; 42770b5de56dSgjelinek 42780b5de56dSgjelinek } else if (fast) { 4279865e09a4Sgjelinek /* same file system, use rename for a quick move */ 4280865e09a4Sgjelinek 4281865e09a4Sgjelinek /* 4282865e09a4Sgjelinek * Remove the new_zonepath directory that got created above 4283865e09a4Sgjelinek * during the validation. It gets in the way of the rename. 4284865e09a4Sgjelinek */ 4285865e09a4Sgjelinek if (rmdir(new_zonepath) != 0) { 4286865e09a4Sgjelinek zperror(gettext("could not rmdir new zone path"), 4287865e09a4Sgjelinek B_FALSE); 4288865e09a4Sgjelinek zonecfg_fini_handle(handle); 4289865e09a4Sgjelinek release_lock_file(lockfd); 4290865e09a4Sgjelinek return (Z_ERR); 4291865e09a4Sgjelinek } 4292865e09a4Sgjelinek 4293865e09a4Sgjelinek if (rename(zonepath, new_zonepath) != 0) { 4294865e09a4Sgjelinek /* 4295865e09a4Sgjelinek * If this fails we don't need to do all of the 4296865e09a4Sgjelinek * cleanup that happens for the rest of the code 4297865e09a4Sgjelinek * so just return from this error. 4298865e09a4Sgjelinek */ 4299865e09a4Sgjelinek zperror(gettext("could not move zone"), B_FALSE); 4300865e09a4Sgjelinek zonecfg_fini_handle(handle); 4301865e09a4Sgjelinek release_lock_file(lockfd); 4302865e09a4Sgjelinek return (Z_ERR); 4303865e09a4Sgjelinek } 4304865e09a4Sgjelinek 4305865e09a4Sgjelinek } else { 43060b5de56dSgjelinek /* 43070b5de56dSgjelinek * Attempt to create a ZFS fs for the new zonepath. As usual, 43080b5de56dSgjelinek * we don't care if this works or not since we always have the 43090b5de56dSgjelinek * default behavior of a simple directory for the zonepath. 43100b5de56dSgjelinek */ 43110b5de56dSgjelinek create_zfs_zonepath(new_zonepath); 43120b5de56dSgjelinek 4313865e09a4Sgjelinek (void) printf(gettext( 43140b5de56dSgjelinek "Moving across file systems; copying zonepath %s..."), 4315865e09a4Sgjelinek zonepath); 4316865e09a4Sgjelinek (void) fflush(stdout); 4317865e09a4Sgjelinek 4318865e09a4Sgjelinek err = copy_zone(zonepath, new_zonepath); 4319865e09a4Sgjelinek 4320865e09a4Sgjelinek (void) printf("\n"); 4321865e09a4Sgjelinek if (err != Z_OK) 4322865e09a4Sgjelinek goto done; 4323865e09a4Sgjelinek } 4324865e09a4Sgjelinek 4325865e09a4Sgjelinek if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) { 4326865e09a4Sgjelinek errno = err; 4327865e09a4Sgjelinek zperror(gettext("could not set new zonepath"), B_TRUE); 4328865e09a4Sgjelinek goto done; 4329865e09a4Sgjelinek } 4330865e09a4Sgjelinek 4331865e09a4Sgjelinek if ((err = zonecfg_save(handle)) != Z_OK) { 4332865e09a4Sgjelinek errno = err; 4333865e09a4Sgjelinek zperror(gettext("zonecfg save failed"), B_TRUE); 4334865e09a4Sgjelinek goto done; 4335865e09a4Sgjelinek } 4336865e09a4Sgjelinek 4337865e09a4Sgjelinek revert = B_FALSE; 4338865e09a4Sgjelinek 4339865e09a4Sgjelinek done: 4340865e09a4Sgjelinek zonecfg_fini_handle(handle); 4341865e09a4Sgjelinek release_lock_file(lockfd); 4342865e09a4Sgjelinek 4343865e09a4Sgjelinek /* 43440b5de56dSgjelinek * Clean up the file system based on how things went. We either 4345865e09a4Sgjelinek * clean up the new zonepath if the operation failed for some reason 4346865e09a4Sgjelinek * or we clean up the old zonepath if everything is ok. 4347865e09a4Sgjelinek */ 4348865e09a4Sgjelinek if (revert) { 4349865e09a4Sgjelinek /* The zonecfg update failed, cleanup the new zonepath. */ 43500b5de56dSgjelinek if (is_zfs) { 43510b5de56dSgjelinek if (move_zfs(new_zonepath, zonepath) == Z_ERR) { 43520b5de56dSgjelinek (void) fprintf(stderr, gettext("could not " 43530b5de56dSgjelinek "restore zonepath, the zfs mountpoint is " 43540b5de56dSgjelinek "set as:\n%s\n"), new_zonepath); 43550b5de56dSgjelinek /* 43560b5de56dSgjelinek * err is already != Z_OK since we're reverting 43570b5de56dSgjelinek */ 43580b5de56dSgjelinek } 43590b5de56dSgjelinek 43600b5de56dSgjelinek } else if (fast) { 4361865e09a4Sgjelinek if (rename(new_zonepath, zonepath) != 0) { 4362865e09a4Sgjelinek zperror(gettext("could not restore zonepath"), 4363865e09a4Sgjelinek B_FALSE); 4364865e09a4Sgjelinek /* 4365865e09a4Sgjelinek * err is already != Z_OK since we're reverting 4366865e09a4Sgjelinek */ 4367865e09a4Sgjelinek } 4368865e09a4Sgjelinek } else { 4369865e09a4Sgjelinek (void) printf(gettext("Cleaning up zonepath %s..."), 4370865e09a4Sgjelinek new_zonepath); 4371865e09a4Sgjelinek (void) fflush(stdout); 43720b5de56dSgjelinek err = cleanup_zonepath(new_zonepath, B_TRUE); 4373865e09a4Sgjelinek (void) printf("\n"); 4374865e09a4Sgjelinek 437507b574eeSgjelinek if (err != Z_OK) { 4376865e09a4Sgjelinek errno = err; 4377865e09a4Sgjelinek zperror(gettext("could not remove new " 4378865e09a4Sgjelinek "zonepath"), B_TRUE); 4379865e09a4Sgjelinek } else { 4380865e09a4Sgjelinek /* 4381865e09a4Sgjelinek * Because we're reverting we know the mainline 4382865e09a4Sgjelinek * code failed but we just reused the err 4383865e09a4Sgjelinek * variable so we reset it back to Z_ERR. 4384865e09a4Sgjelinek */ 4385865e09a4Sgjelinek err = Z_ERR; 4386865e09a4Sgjelinek } 4387865e09a4Sgjelinek } 4388865e09a4Sgjelinek 4389865e09a4Sgjelinek } else { 4390865e09a4Sgjelinek /* The move was successful, cleanup the old zonepath. */ 43910b5de56dSgjelinek if (!is_zfs && !fast) { 4392865e09a4Sgjelinek (void) printf( 4393865e09a4Sgjelinek gettext("Cleaning up zonepath %s..."), zonepath); 4394865e09a4Sgjelinek (void) fflush(stdout); 43950b5de56dSgjelinek err = cleanup_zonepath(zonepath, B_TRUE); 4396865e09a4Sgjelinek (void) printf("\n"); 4397865e09a4Sgjelinek 439807b574eeSgjelinek if (err != Z_OK) { 4399865e09a4Sgjelinek errno = err; 4400865e09a4Sgjelinek zperror(gettext("could not remove zonepath"), 4401865e09a4Sgjelinek B_TRUE); 4402865e09a4Sgjelinek } 4403865e09a4Sgjelinek } 4404865e09a4Sgjelinek } 4405865e09a4Sgjelinek 4406865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 4407865e09a4Sgjelinek } 4408865e09a4Sgjelinek 4409ee519a1fSgjelinek static int 4410ee519a1fSgjelinek detach_func(int argc, char *argv[]) 4411ee519a1fSgjelinek { 4412ee519a1fSgjelinek int lockfd; 4413ee519a1fSgjelinek int err, arg; 4414ee519a1fSgjelinek char zonepath[MAXPATHLEN]; 4415ee519a1fSgjelinek zone_dochandle_t handle; 44168cd327d5Sgjelinek boolean_t execute = B_TRUE; 4417ee519a1fSgjelinek 4418ee519a1fSgjelinek if (zonecfg_in_alt_root()) { 4419ee519a1fSgjelinek zerror(gettext("cannot detach zone in alternate root")); 4420ee519a1fSgjelinek return (Z_ERR); 4421ee519a1fSgjelinek } 4422ee519a1fSgjelinek 4423ee519a1fSgjelinek optind = 0; 44248cd327d5Sgjelinek if ((arg = getopt(argc, argv, "?n")) != EOF) { 4425ee519a1fSgjelinek switch (arg) { 4426ee519a1fSgjelinek case '?': 4427ee519a1fSgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 4428ee519a1fSgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 44298cd327d5Sgjelinek case 'n': 44308cd327d5Sgjelinek execute = B_FALSE; 44318cd327d5Sgjelinek break; 4432ee519a1fSgjelinek default: 4433ee519a1fSgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 4434ee519a1fSgjelinek return (Z_USAGE); 4435ee519a1fSgjelinek } 4436ee519a1fSgjelinek } 44379acbbeafSnn35248 44388cd327d5Sgjelinek if (execute) { 44399acbbeafSnn35248 if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE, 44409acbbeafSnn35248 B_FALSE) != Z_OK) 4441ee519a1fSgjelinek return (Z_ERR); 4442ce28b40eSzt129084 if (verify_details(CMD_DETACH, argv) != Z_OK) 4443ee519a1fSgjelinek return (Z_ERR); 44448cd327d5Sgjelinek } else { 44458cd327d5Sgjelinek /* 44468cd327d5Sgjelinek * We want a dry-run to work for a non-privileged user so we 44478cd327d5Sgjelinek * only do minimal validation. 44488cd327d5Sgjelinek */ 44498cd327d5Sgjelinek if (getzoneid() != GLOBAL_ZONEID) { 44508cd327d5Sgjelinek zerror(gettext("must be in the global zone to %s a " 44518cd327d5Sgjelinek "zone."), cmd_to_str(CMD_DETACH)); 44528cd327d5Sgjelinek return (Z_ERR); 44538cd327d5Sgjelinek } 44548cd327d5Sgjelinek 44558cd327d5Sgjelinek if (target_zone == NULL) { 44568cd327d5Sgjelinek zerror(gettext("no zone specified")); 44578cd327d5Sgjelinek return (Z_ERR); 44588cd327d5Sgjelinek } 44598cd327d5Sgjelinek 44608cd327d5Sgjelinek if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) { 44618cd327d5Sgjelinek zerror(gettext("%s operation is invalid for the " 44628cd327d5Sgjelinek "global zone."), cmd_to_str(CMD_DETACH)); 44638cd327d5Sgjelinek return (Z_ERR); 44648cd327d5Sgjelinek } 44658cd327d5Sgjelinek } 4466ee519a1fSgjelinek 4467ee519a1fSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 4468ee519a1fSgjelinek != Z_OK) { 4469ee519a1fSgjelinek errno = err; 4470ee519a1fSgjelinek zperror2(target_zone, gettext("could not get zone path")); 4471ee519a1fSgjelinek return (Z_ERR); 4472ee519a1fSgjelinek } 4473ee519a1fSgjelinek 4474ee519a1fSgjelinek /* Don't detach the zone if anything is still mounted there */ 44758cd327d5Sgjelinek if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) { 44760b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 4477ee519a1fSgjelinek "subdirectories of %s.\n"), zonepath); 4478ee519a1fSgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 4479ee519a1fSgjelinek return (Z_ERR); 4480ee519a1fSgjelinek } 4481ee519a1fSgjelinek 4482ee519a1fSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 4483ee519a1fSgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 4484ee519a1fSgjelinek return (Z_ERR); 4485ee519a1fSgjelinek } 4486ee519a1fSgjelinek 4487ee519a1fSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 4488ee519a1fSgjelinek errno = err; 4489ee519a1fSgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 4490ee519a1fSgjelinek zonecfg_fini_handle(handle); 4491ee519a1fSgjelinek return (Z_ERR); 4492ee519a1fSgjelinek } 4493ee519a1fSgjelinek 44948cd327d5Sgjelinek if (execute && grab_lock_file(target_zone, &lockfd) != Z_OK) { 4495ee519a1fSgjelinek zerror(gettext("another %s may have an operation in progress."), 4496ee519a1fSgjelinek "zoneadm"); 4497ee519a1fSgjelinek zonecfg_fini_handle(handle); 4498ee519a1fSgjelinek return (Z_ERR); 4499ee519a1fSgjelinek } 4500ee519a1fSgjelinek 4501ee519a1fSgjelinek if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) { 4502ee519a1fSgjelinek errno = err; 4503ee519a1fSgjelinek zperror(gettext("getting the detach information failed"), 4504ee519a1fSgjelinek B_TRUE); 4505ee519a1fSgjelinek goto done; 4506ee519a1fSgjelinek } 4507ee519a1fSgjelinek 45088cd327d5Sgjelinek if ((err = zonecfg_detach_save(handle, (execute ? 0 : ZONE_DRY_RUN))) 45098cd327d5Sgjelinek != Z_OK) { 4510ee519a1fSgjelinek errno = err; 4511ee519a1fSgjelinek zperror(gettext("saving the detach manifest failed"), B_TRUE); 4512ee519a1fSgjelinek goto done; 4513ee519a1fSgjelinek } 4514ee519a1fSgjelinek 45158cd327d5Sgjelinek /* 45168cd327d5Sgjelinek * Set the zone state back to configured unless we are running with the 45178cd327d5Sgjelinek * no-execute option. 45188cd327d5Sgjelinek */ 45198cd327d5Sgjelinek if (execute && (err = zone_set_state(target_zone, 45208cd327d5Sgjelinek ZONE_STATE_CONFIGURED)) != Z_OK) { 4521ee519a1fSgjelinek errno = err; 4522ee519a1fSgjelinek zperror(gettext("could not reset state"), B_TRUE); 4523ee519a1fSgjelinek } 4524ee519a1fSgjelinek 4525ee519a1fSgjelinek done: 4526ee519a1fSgjelinek zonecfg_fini_handle(handle); 45278cd327d5Sgjelinek if (execute) 4528ee519a1fSgjelinek release_lock_file(lockfd); 4529ee519a1fSgjelinek 4530ee519a1fSgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 4531ee519a1fSgjelinek } 4532ee519a1fSgjelinek 4533ee519a1fSgjelinek /* 4534ee519a1fSgjelinek * During attach we go through and fix up the /dev entries for the zone 4535ee519a1fSgjelinek * we are attaching. In order to regenerate /dev with the correct devices, 4536ee519a1fSgjelinek * the old /dev will be removed, the zone readied (which generates a new 4537ee519a1fSgjelinek * /dev) then halted, then we use the info from the manifest to update 4538ee519a1fSgjelinek * the modes, owners, etc. on the new /dev. 4539ee519a1fSgjelinek */ 4540ee519a1fSgjelinek static int 4541ee519a1fSgjelinek dev_fix(zone_dochandle_t handle) 4542ee519a1fSgjelinek { 4543ee519a1fSgjelinek int res; 4544ee519a1fSgjelinek int err; 4545ee519a1fSgjelinek int status; 4546ee519a1fSgjelinek struct zone_devpermtab devtab; 4547ee519a1fSgjelinek zone_cmd_arg_t zarg; 4548ee519a1fSgjelinek char devpath[MAXPATHLEN]; 4549ee519a1fSgjelinek /* 6: "exec " and " " */ 4550ee519a1fSgjelinek char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 4551ee519a1fSgjelinek 4552ee519a1fSgjelinek if ((res = zonecfg_get_zonepath(handle, devpath, sizeof (devpath))) 4553ee519a1fSgjelinek != Z_OK) 4554ee519a1fSgjelinek return (res); 4555ee519a1fSgjelinek 4556ee519a1fSgjelinek if (strlcat(devpath, "/dev", sizeof (devpath)) >= sizeof (devpath)) 4557ee519a1fSgjelinek return (Z_TOO_BIG); 4558ee519a1fSgjelinek 4559ee519a1fSgjelinek /* 4560ee519a1fSgjelinek * "exec" the command so that the returned status is that of 4561ee519a1fSgjelinek * RMCOMMAND and not the shell. 4562ee519a1fSgjelinek */ 45639acbbeafSnn35248 (void) snprintf(cmdbuf, sizeof (cmdbuf), EXEC_PREFIX RMCOMMAND " %s", 4564ee519a1fSgjelinek devpath); 4565ee519a1fSgjelinek status = do_subproc(cmdbuf); 45669acbbeafSnn35248 if ((err = subproc_status(RMCOMMAND, status, B_TRUE)) != 45679acbbeafSnn35248 ZONE_SUBPROC_OK) { 4568ee519a1fSgjelinek (void) fprintf(stderr, 4569ee519a1fSgjelinek gettext("could not remove existing /dev\n")); 4570ee519a1fSgjelinek return (Z_ERR); 4571ee519a1fSgjelinek } 4572ee519a1fSgjelinek 4573ee519a1fSgjelinek /* In order to ready the zone, it must be in the installed state */ 4574ee519a1fSgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 4575ee519a1fSgjelinek errno = err; 4576ee519a1fSgjelinek zperror(gettext("could not reset state"), B_TRUE); 4577ee519a1fSgjelinek return (Z_ERR); 4578ee519a1fSgjelinek } 4579ee519a1fSgjelinek 4580ee519a1fSgjelinek /* We have to ready the zone to regen the dev tree */ 4581ee519a1fSgjelinek zarg.cmd = Z_READY; 4582ee519a1fSgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 4583ee519a1fSgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 45840209230bSgjelinek /* attempt to restore zone to configured state */ 45850209230bSgjelinek (void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 4586ee519a1fSgjelinek return (Z_ERR); 4587ee519a1fSgjelinek } 4588ee519a1fSgjelinek 4589ee519a1fSgjelinek zarg.cmd = Z_HALT; 4590ee519a1fSgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 4591ee519a1fSgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 45920209230bSgjelinek /* attempt to restore zone to configured state */ 45930209230bSgjelinek (void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 4594ee519a1fSgjelinek return (Z_ERR); 4595ee519a1fSgjelinek } 4596ee519a1fSgjelinek 45970209230bSgjelinek /* attempt to restore zone to configured state */ 45980209230bSgjelinek (void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 45990209230bSgjelinek 4600ee519a1fSgjelinek if (zonecfg_setdevperment(handle) != Z_OK) { 4601ee519a1fSgjelinek (void) fprintf(stderr, 4602ee519a1fSgjelinek gettext("unable to enumerate device entries\n")); 4603ee519a1fSgjelinek return (Z_ERR); 4604ee519a1fSgjelinek } 4605ee519a1fSgjelinek 4606ee519a1fSgjelinek while (zonecfg_getdevperment(handle, &devtab) == Z_OK) { 4607ee519a1fSgjelinek int err; 4608ee519a1fSgjelinek 4609ee519a1fSgjelinek if ((err = zonecfg_devperms_apply(handle, 4610ee519a1fSgjelinek devtab.zone_devperm_name, devtab.zone_devperm_uid, 4611ee519a1fSgjelinek devtab.zone_devperm_gid, devtab.zone_devperm_mode, 4612ee519a1fSgjelinek devtab.zone_devperm_acl)) != Z_OK && err != Z_INVAL) 4613ee519a1fSgjelinek (void) fprintf(stderr, gettext("error updating device " 4614ee519a1fSgjelinek "%s: %s\n"), devtab.zone_devperm_name, 4615ee519a1fSgjelinek zonecfg_strerror(err)); 4616ee519a1fSgjelinek 4617ee519a1fSgjelinek free(devtab.zone_devperm_acl); 4618ee519a1fSgjelinek } 4619ee519a1fSgjelinek 4620ee519a1fSgjelinek (void) zonecfg_enddevperment(handle); 4621ee519a1fSgjelinek 4622ee519a1fSgjelinek return (Z_OK); 4623ee519a1fSgjelinek } 4624ee519a1fSgjelinek 46258cd327d5Sgjelinek /* 46268cd327d5Sgjelinek * Validate attaching a zone but don't actually do the work. The zone 46278cd327d5Sgjelinek * does not have to exist, so there is some complexity getting a new zone 46288cd327d5Sgjelinek * configuration set up so that we can perform the validation. This is 46298cd327d5Sgjelinek * handled within zonecfg_attach_manifest() which returns two handles; one 46308cd327d5Sgjelinek * for the the full configuration to validate (rem_handle) and the other 46318cd327d5Sgjelinek * (local_handle) containing only the zone configuration derived from the 46328cd327d5Sgjelinek * manifest. 46338cd327d5Sgjelinek */ 46348cd327d5Sgjelinek static int 4635ce28b40eSzt129084 dryrun_attach(char *manifest_path, char *argv[]) 46368cd327d5Sgjelinek { 46378cd327d5Sgjelinek int fd; 46388cd327d5Sgjelinek int err; 46398cd327d5Sgjelinek int res; 46408cd327d5Sgjelinek zone_dochandle_t local_handle; 46418cd327d5Sgjelinek zone_dochandle_t rem_handle = NULL; 46428cd327d5Sgjelinek 46438cd327d5Sgjelinek if (strcmp(manifest_path, "-") == 0) { 46448cd327d5Sgjelinek fd = 0; 46458cd327d5Sgjelinek } else if ((fd = open(manifest_path, O_RDONLY)) < 0) { 46468cd327d5Sgjelinek zperror(gettext("could not open manifest path"), B_FALSE); 46478cd327d5Sgjelinek return (Z_ERR); 46488cd327d5Sgjelinek } 46498cd327d5Sgjelinek 46508cd327d5Sgjelinek if ((local_handle = zonecfg_init_handle()) == NULL) { 46518cd327d5Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 46528cd327d5Sgjelinek res = Z_ERR; 46538cd327d5Sgjelinek goto done; 46548cd327d5Sgjelinek } 46558cd327d5Sgjelinek 46568cd327d5Sgjelinek if ((rem_handle = zonecfg_init_handle()) == NULL) { 46578cd327d5Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 46588cd327d5Sgjelinek res = Z_ERR; 46598cd327d5Sgjelinek goto done; 46608cd327d5Sgjelinek } 46618cd327d5Sgjelinek 46628cd327d5Sgjelinek if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle)) 46638cd327d5Sgjelinek != Z_OK) { 46648cd327d5Sgjelinek res = Z_ERR; 4665*d9e728a2Sgjelinek 4666*d9e728a2Sgjelinek if (err == Z_INVALID_DOCUMENT) { 4667*d9e728a2Sgjelinek struct stat st; 4668*d9e728a2Sgjelinek char buf[6]; 4669*d9e728a2Sgjelinek 4670*d9e728a2Sgjelinek if (strcmp(manifest_path, "-") == 0) { 4671*d9e728a2Sgjelinek zerror(gettext("Input is not a valid XML " 4672*d9e728a2Sgjelinek "file")); 4673*d9e728a2Sgjelinek goto done; 4674*d9e728a2Sgjelinek } 4675*d9e728a2Sgjelinek 4676*d9e728a2Sgjelinek if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) { 4677*d9e728a2Sgjelinek zerror(gettext("%s is not an XML file"), 4678*d9e728a2Sgjelinek manifest_path); 4679*d9e728a2Sgjelinek goto done; 4680*d9e728a2Sgjelinek } 4681*d9e728a2Sgjelinek 4682*d9e728a2Sgjelinek bzero(buf, sizeof (buf)); 4683*d9e728a2Sgjelinek (void) lseek(fd, 0L, SEEK_SET); 4684*d9e728a2Sgjelinek if (read(fd, buf, sizeof (buf) - 1) < 0 || 4685*d9e728a2Sgjelinek strncmp(buf, "<?xml", 5) != 0) 4686*d9e728a2Sgjelinek zerror(gettext("%s is not an XML file"), 4687*d9e728a2Sgjelinek manifest_path); 4688*d9e728a2Sgjelinek else 4689*d9e728a2Sgjelinek zerror(gettext("Cannot attach to an earlier " 4690*d9e728a2Sgjelinek "release of the operating system")); 4691*d9e728a2Sgjelinek } else { 4692*d9e728a2Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4693*d9e728a2Sgjelinek } 46948cd327d5Sgjelinek goto done; 46958cd327d5Sgjelinek } 46968cd327d5Sgjelinek 4697e2482d1aSgjelinek /* 4698e2482d1aSgjelinek * Retrieve remote handle brand type and determine whether it is 4699e2482d1aSgjelinek * native or not. 4700e2482d1aSgjelinek */ 4701e2482d1aSgjelinek if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand)) 4702e2482d1aSgjelinek != Z_OK) { 4703e2482d1aSgjelinek zerror(gettext("missing or invalid brand")); 4704e2482d1aSgjelinek exit(Z_ERR); 4705e2482d1aSgjelinek } 4706e2482d1aSgjelinek is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0); 4707e2482d1aSgjelinek 4708ce28b40eSzt129084 res = verify_handle(CMD_ATTACH, local_handle, argv); 47098cd327d5Sgjelinek 47108cd327d5Sgjelinek /* Get the detach information for the locally defined zone. */ 47118cd327d5Sgjelinek if ((err = zonecfg_get_detach_info(local_handle, B_FALSE)) != Z_OK) { 47128cd327d5Sgjelinek errno = err; 47138cd327d5Sgjelinek zperror(gettext("getting the attach information failed"), 47148cd327d5Sgjelinek B_TRUE); 47158cd327d5Sgjelinek res = Z_ERR; 47168cd327d5Sgjelinek } else { 47178cd327d5Sgjelinek /* sw_cmp prints error msgs as necessary */ 47188cd327d5Sgjelinek if (sw_cmp(local_handle, rem_handle, SW_CMP_NONE) != Z_OK) 47198cd327d5Sgjelinek res = Z_ERR; 47208cd327d5Sgjelinek } 47218cd327d5Sgjelinek 47228cd327d5Sgjelinek done: 47238cd327d5Sgjelinek if (strcmp(manifest_path, "-") != 0) 47248cd327d5Sgjelinek (void) close(fd); 47258cd327d5Sgjelinek 47268cd327d5Sgjelinek zonecfg_fini_handle(local_handle); 47278cd327d5Sgjelinek zonecfg_fini_handle(rem_handle); 47288cd327d5Sgjelinek 47298cd327d5Sgjelinek return ((res == Z_OK) ? Z_OK : Z_ERR); 47308cd327d5Sgjelinek } 47318cd327d5Sgjelinek 4732ee519a1fSgjelinek static int 4733ee519a1fSgjelinek attach_func(int argc, char *argv[]) 4734ee519a1fSgjelinek { 4735ee519a1fSgjelinek int lockfd; 4736ee519a1fSgjelinek int err, arg; 4737ee519a1fSgjelinek boolean_t force = B_FALSE; 4738ee519a1fSgjelinek zone_dochandle_t handle; 4739ee519a1fSgjelinek zone_dochandle_t athandle = NULL; 4740ee519a1fSgjelinek char zonepath[MAXPATHLEN]; 47419acbbeafSnn35248 char brand[MAXNAMELEN], atbrand[MAXNAMELEN]; 47428cd327d5Sgjelinek boolean_t execute = B_TRUE; 47438cd327d5Sgjelinek char *manifest_path; 4744ee519a1fSgjelinek 4745ee519a1fSgjelinek if (zonecfg_in_alt_root()) { 4746ee519a1fSgjelinek zerror(gettext("cannot attach zone in alternate root")); 4747ee519a1fSgjelinek return (Z_ERR); 4748ee519a1fSgjelinek } 4749ee519a1fSgjelinek 4750ee519a1fSgjelinek optind = 0; 47518cd327d5Sgjelinek if ((arg = getopt(argc, argv, "?Fn:")) != EOF) { 4752ee519a1fSgjelinek switch (arg) { 4753ee519a1fSgjelinek case '?': 4754ee519a1fSgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 4755ee519a1fSgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 4756ee519a1fSgjelinek case 'F': 4757ee519a1fSgjelinek force = B_TRUE; 4758ee519a1fSgjelinek break; 47598cd327d5Sgjelinek case 'n': 47608cd327d5Sgjelinek execute = B_FALSE; 47618cd327d5Sgjelinek manifest_path = optarg; 47628cd327d5Sgjelinek break; 4763ee519a1fSgjelinek default: 4764ee519a1fSgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 4765ee519a1fSgjelinek return (Z_USAGE); 4766ee519a1fSgjelinek } 4767ee519a1fSgjelinek } 47688cd327d5Sgjelinek 47698cd327d5Sgjelinek /* 47708cd327d5Sgjelinek * If the no-execute option was specified, we need to branch down 47718cd327d5Sgjelinek * a completely different path since there is no zone required to be 47728cd327d5Sgjelinek * configured for this option. 47738cd327d5Sgjelinek */ 47748cd327d5Sgjelinek if (!execute) 4775ce28b40eSzt129084 return (dryrun_attach(manifest_path, argv)); 47768cd327d5Sgjelinek 47779acbbeafSnn35248 if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, B_TRUE, B_FALSE) 47789acbbeafSnn35248 != Z_OK) 4779ee519a1fSgjelinek return (Z_ERR); 4780ce28b40eSzt129084 if (verify_details(CMD_ATTACH, argv) != Z_OK) 4781ee519a1fSgjelinek return (Z_ERR); 4782ee519a1fSgjelinek 4783ee519a1fSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 4784ee519a1fSgjelinek != Z_OK) { 4785ee519a1fSgjelinek errno = err; 4786ee519a1fSgjelinek zperror2(target_zone, gettext("could not get zone path")); 4787ee519a1fSgjelinek return (Z_ERR); 4788ee519a1fSgjelinek } 4789ee519a1fSgjelinek 4790ee519a1fSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 4791ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4792ee519a1fSgjelinek return (Z_ERR); 4793ee519a1fSgjelinek } 4794ee519a1fSgjelinek 4795ee519a1fSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 4796ee519a1fSgjelinek errno = err; 4797ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4798ee519a1fSgjelinek zonecfg_fini_handle(handle); 4799ee519a1fSgjelinek return (Z_ERR); 4800ee519a1fSgjelinek } 4801ee519a1fSgjelinek 4802ee519a1fSgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 4803ee519a1fSgjelinek zerror(gettext("another %s may have an operation in progress."), 4804ee519a1fSgjelinek "zoneadm"); 4805ee519a1fSgjelinek zonecfg_fini_handle(handle); 4806ee519a1fSgjelinek return (Z_ERR); 4807ee519a1fSgjelinek } 4808ee519a1fSgjelinek 4809ee519a1fSgjelinek if (force) 4810ee519a1fSgjelinek goto forced; 4811ee519a1fSgjelinek 4812ee519a1fSgjelinek if ((athandle = zonecfg_init_handle()) == NULL) { 4813ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4814ee519a1fSgjelinek goto done; 4815ee519a1fSgjelinek } 4816ee519a1fSgjelinek 4817ee519a1fSgjelinek if ((err = zonecfg_get_attach_handle(zonepath, target_zone, B_TRUE, 4818ee519a1fSgjelinek athandle)) != Z_OK) { 4819ee519a1fSgjelinek if (err == Z_NO_ZONE) 4820ee519a1fSgjelinek zerror(gettext("Not a detached zone")); 4821ee519a1fSgjelinek else if (err == Z_INVALID_DOCUMENT) 4822ee519a1fSgjelinek zerror(gettext("Cannot attach to an earlier release " 4823ee519a1fSgjelinek "of the operating system")); 4824ee519a1fSgjelinek else 4825ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4826ee519a1fSgjelinek goto done; 4827ee519a1fSgjelinek } 4828ee519a1fSgjelinek 4829ee519a1fSgjelinek /* Get the detach information for the locally defined zone. */ 4830ee519a1fSgjelinek if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) { 4831ee519a1fSgjelinek errno = err; 4832ee519a1fSgjelinek zperror(gettext("getting the attach information failed"), 4833ee519a1fSgjelinek B_TRUE); 4834ee519a1fSgjelinek goto done; 4835ee519a1fSgjelinek } 4836ee519a1fSgjelinek 48379acbbeafSnn35248 /* 48389acbbeafSnn35248 * Ensure that the detached and locally defined zones are both of 48399acbbeafSnn35248 * the same brand. 48409acbbeafSnn35248 */ 48419acbbeafSnn35248 if ((zonecfg_get_brand(handle, brand, sizeof (brand)) != 0) || 48429acbbeafSnn35248 (zonecfg_get_brand(athandle, atbrand, sizeof (atbrand)) != 0)) { 48439acbbeafSnn35248 err = Z_ERR; 48449acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 48459acbbeafSnn35248 goto done; 48469acbbeafSnn35248 } 48479acbbeafSnn35248 48489acbbeafSnn35248 if (strcmp(atbrand, brand) != NULL) { 48499acbbeafSnn35248 err = Z_ERR; 48509acbbeafSnn35248 zerror(gettext("Trying to attach a '%s' zone to a '%s' " 48519acbbeafSnn35248 "configuration."), atbrand, brand); 48529acbbeafSnn35248 goto done; 48539acbbeafSnn35248 } 48549acbbeafSnn35248 4855ee519a1fSgjelinek /* sw_cmp prints error msgs as necessary */ 48560b5de56dSgjelinek if ((err = sw_cmp(handle, athandle, SW_CMP_NONE)) != Z_OK) 4857ee519a1fSgjelinek goto done; 4858ee519a1fSgjelinek 4859ee519a1fSgjelinek if ((err = dev_fix(athandle)) != Z_OK) 4860ee519a1fSgjelinek goto done; 4861ee519a1fSgjelinek 4862ee519a1fSgjelinek forced: 4863ee519a1fSgjelinek 4864ee519a1fSgjelinek zonecfg_rm_detached(handle, force); 4865ee519a1fSgjelinek 4866ee519a1fSgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 4867ee519a1fSgjelinek errno = err; 4868ee519a1fSgjelinek zperror(gettext("could not reset state"), B_TRUE); 4869ee519a1fSgjelinek } 4870ee519a1fSgjelinek 4871ee519a1fSgjelinek done: 4872ee519a1fSgjelinek zonecfg_fini_handle(handle); 4873ee519a1fSgjelinek release_lock_file(lockfd); 4874ee519a1fSgjelinek if (athandle != NULL) 4875ee519a1fSgjelinek zonecfg_fini_handle(athandle); 4876ee519a1fSgjelinek 4877ee519a1fSgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 4878ee519a1fSgjelinek } 4879ee519a1fSgjelinek 4880865e09a4Sgjelinek /* 48817c478bd9Sstevel@tonic-gate * On input, TRUE => yes, FALSE => no. 48827c478bd9Sstevel@tonic-gate * On return, TRUE => 1, FALSE => 0, could not ask => -1. 48837c478bd9Sstevel@tonic-gate */ 48847c478bd9Sstevel@tonic-gate 48857c478bd9Sstevel@tonic-gate static int 48867c478bd9Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question) 48877c478bd9Sstevel@tonic-gate { 48887c478bd9Sstevel@tonic-gate char line[64]; /* should be large enough to answer yes or no */ 48897c478bd9Sstevel@tonic-gate 48907c478bd9Sstevel@tonic-gate if (!isatty(STDIN_FILENO)) 48917c478bd9Sstevel@tonic-gate return (-1); 48927c478bd9Sstevel@tonic-gate for (;;) { 48937c478bd9Sstevel@tonic-gate (void) printf("%s (%s)? ", question, 48947c478bd9Sstevel@tonic-gate default_answer ? "[y]/n" : "y/[n]"); 48957c478bd9Sstevel@tonic-gate if (fgets(line, sizeof (line), stdin) == NULL || 48967c478bd9Sstevel@tonic-gate line[0] == '\n') 48977c478bd9Sstevel@tonic-gate return (default_answer ? 1 : 0); 48987c478bd9Sstevel@tonic-gate if (tolower(line[0]) == 'y') 48997c478bd9Sstevel@tonic-gate return (1); 49007c478bd9Sstevel@tonic-gate if (tolower(line[0]) == 'n') 49017c478bd9Sstevel@tonic-gate return (0); 49027c478bd9Sstevel@tonic-gate } 49037c478bd9Sstevel@tonic-gate } 49047c478bd9Sstevel@tonic-gate 49057c478bd9Sstevel@tonic-gate static int 49067c478bd9Sstevel@tonic-gate uninstall_func(int argc, char *argv[]) 49077c478bd9Sstevel@tonic-gate { 49087c478bd9Sstevel@tonic-gate char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */ 49090b5de56dSgjelinek char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN]; 49107c478bd9Sstevel@tonic-gate boolean_t force = B_FALSE; 49117c478bd9Sstevel@tonic-gate int lockfd, answer; 49127c478bd9Sstevel@tonic-gate int err, arg; 49137c478bd9Sstevel@tonic-gate 4914108322fbScarlsonj if (zonecfg_in_alt_root()) { 4915108322fbScarlsonj zerror(gettext("cannot uninstall zone in alternate root")); 4916108322fbScarlsonj return (Z_ERR); 4917108322fbScarlsonj } 4918108322fbScarlsonj 49197c478bd9Sstevel@tonic-gate optind = 0; 49207c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?F")) != EOF) { 49217c478bd9Sstevel@tonic-gate switch (arg) { 49227c478bd9Sstevel@tonic-gate case '?': 49237c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 49247c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 49257c478bd9Sstevel@tonic-gate case 'F': 49267c478bd9Sstevel@tonic-gate force = B_TRUE; 49277c478bd9Sstevel@tonic-gate break; 49287c478bd9Sstevel@tonic-gate default: 49297c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 49307c478bd9Sstevel@tonic-gate return (Z_USAGE); 49317c478bd9Sstevel@tonic-gate } 49327c478bd9Sstevel@tonic-gate } 49337c478bd9Sstevel@tonic-gate if (argc > optind) { 49347c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 49357c478bd9Sstevel@tonic-gate return (Z_USAGE); 49367c478bd9Sstevel@tonic-gate } 49377c478bd9Sstevel@tonic-gate 49389acbbeafSnn35248 if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE, B_FALSE) 49399acbbeafSnn35248 != Z_OK) 49407c478bd9Sstevel@tonic-gate return (Z_ERR); 49417c478bd9Sstevel@tonic-gate 4942ce28b40eSzt129084 /* 4943ce28b40eSzt129084 * Invoke brand-specific handler. 4944ce28b40eSzt129084 */ 4945ce28b40eSzt129084 if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK) 4946ce28b40eSzt129084 return (Z_ERR); 4947ce28b40eSzt129084 49487c478bd9Sstevel@tonic-gate if (!force) { 49497c478bd9Sstevel@tonic-gate (void) snprintf(line, sizeof (line), 49507c478bd9Sstevel@tonic-gate gettext("Are you sure you want to %s zone %s"), 49517c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL), target_zone); 49527c478bd9Sstevel@tonic-gate if ((answer = ask_yesno(B_FALSE, line)) == 0) { 49537c478bd9Sstevel@tonic-gate return (Z_OK); 49547c478bd9Sstevel@tonic-gate } else if (answer == -1) { 49557c478bd9Sstevel@tonic-gate zerror(gettext("Input not from terminal and -F " 49567c478bd9Sstevel@tonic-gate "not specified: %s not done."), 49577c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 49587c478bd9Sstevel@tonic-gate return (Z_ERR); 49597c478bd9Sstevel@tonic-gate } 49607c478bd9Sstevel@tonic-gate } 49617c478bd9Sstevel@tonic-gate 49620b5de56dSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, 49630b5de56dSgjelinek sizeof (zonepath))) != Z_OK) { 49647c478bd9Sstevel@tonic-gate errno = err; 49657c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 49667c478bd9Sstevel@tonic-gate return (Z_ERR); 49677c478bd9Sstevel@tonic-gate } 49687c478bd9Sstevel@tonic-gate if ((err = zone_get_rootpath(target_zone, rootpath, 49697c478bd9Sstevel@tonic-gate sizeof (rootpath))) != Z_OK) { 49707c478bd9Sstevel@tonic-gate errno = err; 49717c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get root path")); 49727c478bd9Sstevel@tonic-gate return (Z_ERR); 49737c478bd9Sstevel@tonic-gate } 49747c478bd9Sstevel@tonic-gate 49757c478bd9Sstevel@tonic-gate /* 49767c478bd9Sstevel@tonic-gate * If there seems to be a zoneadmd running for this zone, call it 49777c478bd9Sstevel@tonic-gate * to tell it that an uninstall is happening; if all goes well it 49787c478bd9Sstevel@tonic-gate * will then shut itself down. 49797c478bd9Sstevel@tonic-gate */ 49807c478bd9Sstevel@tonic-gate if (ping_zoneadmd(target_zone) == Z_OK) { 49817c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 49827c478bd9Sstevel@tonic-gate zarg.cmd = Z_NOTE_UNINSTALLING; 49837c478bd9Sstevel@tonic-gate /* we don't care too much if this fails... just plow on */ 49847c478bd9Sstevel@tonic-gate (void) call_zoneadmd(target_zone, &zarg); 49857c478bd9Sstevel@tonic-gate } 49867c478bd9Sstevel@tonic-gate 49877c478bd9Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 49887c478bd9Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 4989865e09a4Sgjelinek "zoneadm"); 49907c478bd9Sstevel@tonic-gate return (Z_ERR); 49917c478bd9Sstevel@tonic-gate } 49927c478bd9Sstevel@tonic-gate 49937c478bd9Sstevel@tonic-gate /* Don't uninstall the zone if anything is mounted there */ 49947c478bd9Sstevel@tonic-gate err = zonecfg_find_mounts(rootpath, NULL, NULL); 49957c478bd9Sstevel@tonic-gate if (err) { 49960b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 49977c478bd9Sstevel@tonic-gate "subdirectories of %s.\n"), rootpath); 49987c478bd9Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, zfm_print, NULL); 49997c478bd9Sstevel@tonic-gate return (Z_ERR); 50007c478bd9Sstevel@tonic-gate } 50017c478bd9Sstevel@tonic-gate 50027c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 50037c478bd9Sstevel@tonic-gate if (err != Z_OK) { 50047c478bd9Sstevel@tonic-gate errno = err; 50057c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 50067c478bd9Sstevel@tonic-gate goto bad; 50077c478bd9Sstevel@tonic-gate } 50087c478bd9Sstevel@tonic-gate 50090b5de56dSgjelinek if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 50100b5de56dSgjelinek errno = err; 50110b5de56dSgjelinek zperror2(target_zone, gettext("cleaning up zonepath failed")); 50127c478bd9Sstevel@tonic-gate goto bad; 50130b5de56dSgjelinek } 50140b5de56dSgjelinek 50157c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 50167c478bd9Sstevel@tonic-gate if (err != Z_OK) { 50177c478bd9Sstevel@tonic-gate errno = err; 50187c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not reset state")); 50197c478bd9Sstevel@tonic-gate } 50207c478bd9Sstevel@tonic-gate bad: 50217c478bd9Sstevel@tonic-gate release_lock_file(lockfd); 50227c478bd9Sstevel@tonic-gate return (err); 50237c478bd9Sstevel@tonic-gate } 50247c478bd9Sstevel@tonic-gate 5025108322fbScarlsonj /* ARGSUSED */ 5026108322fbScarlsonj static int 5027108322fbScarlsonj mount_func(int argc, char *argv[]) 5028108322fbScarlsonj { 5029108322fbScarlsonj zone_cmd_arg_t zarg; 50309acbbeafSnn35248 boolean_t force = B_FALSE; 50319acbbeafSnn35248 int arg; 5032108322fbScarlsonj 50339acbbeafSnn35248 /* 50349acbbeafSnn35248 * The only supported subargument to the "mount" subcommand is 50359acbbeafSnn35248 * "-f", which forces us to mount a zone in the INCOMPLETE state. 50369acbbeafSnn35248 */ 50379acbbeafSnn35248 optind = 0; 50389acbbeafSnn35248 if ((arg = getopt(argc, argv, "f")) != EOF) { 50399acbbeafSnn35248 switch (arg) { 50409acbbeafSnn35248 case 'f': 50419acbbeafSnn35248 force = B_TRUE; 50429acbbeafSnn35248 break; 50439acbbeafSnn35248 default: 5044108322fbScarlsonj return (Z_USAGE); 50459acbbeafSnn35248 } 50469acbbeafSnn35248 } 50479acbbeafSnn35248 if (argc > optind) 50489acbbeafSnn35248 return (Z_USAGE); 50499acbbeafSnn35248 50509acbbeafSnn35248 if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force) 50519acbbeafSnn35248 != Z_OK) 5052108322fbScarlsonj return (Z_ERR); 5053ce28b40eSzt129084 if (verify_details(CMD_MOUNT, argv) != Z_OK) 5054108322fbScarlsonj return (Z_ERR); 5055108322fbScarlsonj 50569acbbeafSnn35248 zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT; 5057108322fbScarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 5058108322fbScarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 5059108322fbScarlsonj return (Z_ERR); 5060108322fbScarlsonj } 5061108322fbScarlsonj return (Z_OK); 5062108322fbScarlsonj } 5063108322fbScarlsonj 5064108322fbScarlsonj /* ARGSUSED */ 5065108322fbScarlsonj static int 5066108322fbScarlsonj unmount_func(int argc, char *argv[]) 5067108322fbScarlsonj { 5068108322fbScarlsonj zone_cmd_arg_t zarg; 5069108322fbScarlsonj 5070108322fbScarlsonj if (argc > 0) 5071108322fbScarlsonj return (Z_USAGE); 50729acbbeafSnn35248 if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE) 50739acbbeafSnn35248 != Z_OK) 5074108322fbScarlsonj return (Z_ERR); 5075108322fbScarlsonj 5076108322fbScarlsonj zarg.cmd = Z_UNMOUNT; 5077108322fbScarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 5078108322fbScarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 5079108322fbScarlsonj return (Z_ERR); 5080108322fbScarlsonj } 5081108322fbScarlsonj return (Z_OK); 5082108322fbScarlsonj } 5083108322fbScarlsonj 50847c478bd9Sstevel@tonic-gate static int 5085555afedfScarlsonj mark_func(int argc, char *argv[]) 5086555afedfScarlsonj { 5087555afedfScarlsonj int err, lockfd; 5088555afedfScarlsonj 5089555afedfScarlsonj if (argc != 1 || strcmp(argv[0], "incomplete") != 0) 5090555afedfScarlsonj return (Z_USAGE); 50919acbbeafSnn35248 if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_FALSE, B_FALSE) 50929acbbeafSnn35248 != Z_OK) 5093555afedfScarlsonj return (Z_ERR); 5094555afedfScarlsonj 5095ce28b40eSzt129084 /* 5096ce28b40eSzt129084 * Invoke brand-specific handler. 5097ce28b40eSzt129084 */ 5098ce28b40eSzt129084 if (invoke_brand_handler(CMD_MARK, argv) != Z_OK) 5099ce28b40eSzt129084 return (Z_ERR); 5100ce28b40eSzt129084 5101555afedfScarlsonj if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 5102555afedfScarlsonj zerror(gettext("another %s may have an operation in progress."), 5103555afedfScarlsonj "zoneadm"); 5104555afedfScarlsonj return (Z_ERR); 5105555afedfScarlsonj } 5106555afedfScarlsonj 5107555afedfScarlsonj err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 5108555afedfScarlsonj if (err != Z_OK) { 5109555afedfScarlsonj errno = err; 5110555afedfScarlsonj zperror2(target_zone, gettext("could not set state")); 5111555afedfScarlsonj } 5112555afedfScarlsonj release_lock_file(lockfd); 5113555afedfScarlsonj 5114555afedfScarlsonj return (err); 5115555afedfScarlsonj } 5116555afedfScarlsonj 51170209230bSgjelinek /* 51180209230bSgjelinek * Check what scheduling class we're running under and print a warning if 51190209230bSgjelinek * we're not using FSS. 51200209230bSgjelinek */ 51210209230bSgjelinek static int 51220209230bSgjelinek check_sched_fss(zone_dochandle_t handle) 51230209230bSgjelinek { 51240209230bSgjelinek char class_name[PC_CLNMSZ]; 51250209230bSgjelinek 51260209230bSgjelinek if (zonecfg_get_dflt_sched_class(handle, class_name, 51270209230bSgjelinek sizeof (class_name)) != Z_OK) { 51280209230bSgjelinek zerror(gettext("WARNING: unable to determine the zone's " 51290209230bSgjelinek "scheduling class")); 51300209230bSgjelinek } else if (strcmp("FSS", class_name) != 0) { 51310209230bSgjelinek zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n" 51320209230bSgjelinek "FSS is not the default scheduling class for this zone. " 51330209230bSgjelinek "FSS will be\nused for processes in the zone but to get " 51340209230bSgjelinek "the full benefit of FSS,\nit should be the default " 51350209230bSgjelinek "scheduling class. See dispadmin(1M) for\nmore details.")); 51360209230bSgjelinek return (Z_SYSTEM); 51370209230bSgjelinek } 51380209230bSgjelinek 51390209230bSgjelinek return (Z_OK); 51400209230bSgjelinek } 51410209230bSgjelinek 51420209230bSgjelinek static int 51430209230bSgjelinek check_cpu_shares_sched(zone_dochandle_t handle) 51440209230bSgjelinek { 51450209230bSgjelinek int err; 51460209230bSgjelinek int res = Z_OK; 51470209230bSgjelinek struct zone_rctltab rctl; 51480209230bSgjelinek 51490209230bSgjelinek if ((err = zonecfg_setrctlent(handle)) != Z_OK) { 51500209230bSgjelinek errno = err; 51510209230bSgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 51520209230bSgjelinek return (err); 51530209230bSgjelinek } 51540209230bSgjelinek 51550209230bSgjelinek while (zonecfg_getrctlent(handle, &rctl) == Z_OK) { 51560209230bSgjelinek if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) { 51570209230bSgjelinek if (check_sched_fss(handle) != Z_OK) 51580209230bSgjelinek res = Z_SYSTEM; 51590209230bSgjelinek break; 51600209230bSgjelinek } 51610209230bSgjelinek } 51620209230bSgjelinek 51630209230bSgjelinek (void) zonecfg_endrctlent(handle); 51640209230bSgjelinek 51650209230bSgjelinek return (res); 51660209230bSgjelinek } 51670209230bSgjelinek 51680209230bSgjelinek /* 51697ef01d19Sgjelinek * Check if there is a mix of processes running in different pools within the 51707ef01d19Sgjelinek * zone. This is currently only going to be called for the global zone from 51717ef01d19Sgjelinek * apply_func but that could be generalized in the future. 51727ef01d19Sgjelinek */ 51737ef01d19Sgjelinek static boolean_t 51747ef01d19Sgjelinek mixed_pools(zoneid_t zoneid) 51757ef01d19Sgjelinek { 51767ef01d19Sgjelinek DIR *dirp; 51777ef01d19Sgjelinek dirent_t *dent; 51787ef01d19Sgjelinek boolean_t mixed = B_FALSE; 51797ef01d19Sgjelinek boolean_t poolid_set = B_FALSE; 51807ef01d19Sgjelinek poolid_t last_poolid = 0; 51817ef01d19Sgjelinek 51827ef01d19Sgjelinek if ((dirp = opendir("/proc")) == NULL) { 51837ef01d19Sgjelinek zerror(gettext("could not open /proc")); 51847ef01d19Sgjelinek return (B_FALSE); 51857ef01d19Sgjelinek } 51867ef01d19Sgjelinek 51877ef01d19Sgjelinek while ((dent = readdir(dirp)) != NULL) { 51887ef01d19Sgjelinek int procfd; 51897ef01d19Sgjelinek psinfo_t ps; 51907ef01d19Sgjelinek char procpath[MAXPATHLEN]; 51917ef01d19Sgjelinek 51927ef01d19Sgjelinek if (dent->d_name[0] == '.') 51937ef01d19Sgjelinek continue; 51947ef01d19Sgjelinek 51957ef01d19Sgjelinek (void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo", 51967ef01d19Sgjelinek dent->d_name); 51977ef01d19Sgjelinek 51987ef01d19Sgjelinek if ((procfd = open(procpath, O_RDONLY)) == -1) 51997ef01d19Sgjelinek continue; 52007ef01d19Sgjelinek 52017ef01d19Sgjelinek if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) { 52027ef01d19Sgjelinek /* skip processes in other zones and system processes */ 52037ef01d19Sgjelinek if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) { 52047ef01d19Sgjelinek (void) close(procfd); 52057ef01d19Sgjelinek continue; 52067ef01d19Sgjelinek } 52077ef01d19Sgjelinek 52087ef01d19Sgjelinek if (poolid_set) { 52097ef01d19Sgjelinek if (ps.pr_poolid != last_poolid) 52107ef01d19Sgjelinek mixed = B_TRUE; 52117ef01d19Sgjelinek } else { 52127ef01d19Sgjelinek last_poolid = ps.pr_poolid; 52137ef01d19Sgjelinek poolid_set = B_TRUE; 52147ef01d19Sgjelinek } 52157ef01d19Sgjelinek } 52167ef01d19Sgjelinek 52177ef01d19Sgjelinek (void) close(procfd); 52187ef01d19Sgjelinek 52197ef01d19Sgjelinek if (mixed) 52207ef01d19Sgjelinek break; 52217ef01d19Sgjelinek } 52227ef01d19Sgjelinek 52237ef01d19Sgjelinek (void) closedir(dirp); 52247ef01d19Sgjelinek 52257ef01d19Sgjelinek return (mixed); 52267ef01d19Sgjelinek } 52277ef01d19Sgjelinek 52287ef01d19Sgjelinek /* 52297ef01d19Sgjelinek * Check if a persistent or temporary pool is configured for the zone. 52307ef01d19Sgjelinek * This is currently only going to be called for the global zone from 52317ef01d19Sgjelinek * apply_func but that could be generalized in the future. 52327ef01d19Sgjelinek */ 52337ef01d19Sgjelinek static boolean_t 52347ef01d19Sgjelinek pool_configured(zone_dochandle_t handle) 52357ef01d19Sgjelinek { 52367ef01d19Sgjelinek int err1, err2; 52377ef01d19Sgjelinek struct zone_psettab pset_tab; 52387ef01d19Sgjelinek char poolname[MAXPATHLEN]; 52397ef01d19Sgjelinek 52407ef01d19Sgjelinek err1 = zonecfg_lookup_pset(handle, &pset_tab); 52417ef01d19Sgjelinek err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 52427ef01d19Sgjelinek 52437ef01d19Sgjelinek if (err1 == Z_NO_ENTRY && 52447ef01d19Sgjelinek (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0))) 52457ef01d19Sgjelinek return (B_FALSE); 52467ef01d19Sgjelinek 52477ef01d19Sgjelinek return (B_TRUE); 52487ef01d19Sgjelinek } 52497ef01d19Sgjelinek 52507ef01d19Sgjelinek /* 52510209230bSgjelinek * This is an undocumented interface which is currently only used to apply 52520209230bSgjelinek * the global zone resource management settings when the system boots. 52530209230bSgjelinek * This function does not yet properly handle updating a running system so 52540209230bSgjelinek * any projects running in the zone would be trashed if this function 52550209230bSgjelinek * were to run after the zone had booted. It also does not reset any 52560209230bSgjelinek * rctl settings that were removed from zonecfg. There is still work to be 52570209230bSgjelinek * done before we can properly support dynamically updating the resource 52580209230bSgjelinek * management settings for a running zone (global or non-global). Thus, this 52590209230bSgjelinek * functionality is undocumented for now. 52600209230bSgjelinek */ 52610209230bSgjelinek /* ARGSUSED */ 52620209230bSgjelinek static int 52630209230bSgjelinek apply_func(int argc, char *argv[]) 52640209230bSgjelinek { 52650209230bSgjelinek int err; 52660209230bSgjelinek int res = Z_OK; 52670209230bSgjelinek priv_set_t *privset; 52680209230bSgjelinek zoneid_t zoneid; 52690209230bSgjelinek zone_dochandle_t handle; 52700209230bSgjelinek struct zone_mcaptab mcap; 52710209230bSgjelinek char pool_err[128]; 52720209230bSgjelinek 52730209230bSgjelinek zoneid = getzoneid(); 52740209230bSgjelinek 52750209230bSgjelinek if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID || 52760209230bSgjelinek target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0) 52770209230bSgjelinek return (usage(B_FALSE)); 52780209230bSgjelinek 52790209230bSgjelinek if ((privset = priv_allocset()) == NULL) { 52800209230bSgjelinek zerror(gettext("%s failed"), "priv_allocset"); 52810209230bSgjelinek return (Z_ERR); 52820209230bSgjelinek } 52830209230bSgjelinek 52840209230bSgjelinek if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 52850209230bSgjelinek zerror(gettext("%s failed"), "getppriv"); 52860209230bSgjelinek priv_freeset(privset); 52870209230bSgjelinek return (Z_ERR); 52880209230bSgjelinek } 52890209230bSgjelinek 52900209230bSgjelinek if (priv_isfullset(privset) == B_FALSE) { 52910209230bSgjelinek (void) usage(B_FALSE); 52920209230bSgjelinek priv_freeset(privset); 52930209230bSgjelinek return (Z_ERR); 52940209230bSgjelinek } 52950209230bSgjelinek priv_freeset(privset); 52960209230bSgjelinek 52970209230bSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 52980209230bSgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 52990209230bSgjelinek return (Z_ERR); 53000209230bSgjelinek } 53010209230bSgjelinek 53020209230bSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 53030209230bSgjelinek errno = err; 53040209230bSgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 53050209230bSgjelinek zonecfg_fini_handle(handle); 53060209230bSgjelinek return (Z_ERR); 53070209230bSgjelinek } 53080209230bSgjelinek 53090209230bSgjelinek /* specific error msgs are printed within apply_rctls */ 53100209230bSgjelinek if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) { 53110209230bSgjelinek errno = err; 53120209230bSgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 53130209230bSgjelinek res = Z_ERR; 53140209230bSgjelinek } 53150209230bSgjelinek 53160209230bSgjelinek if ((err = check_cpu_shares_sched(handle)) != Z_OK) 53170209230bSgjelinek res = Z_ERR; 53180209230bSgjelinek 53197ef01d19Sgjelinek if (pool_configured(handle)) { 53207ef01d19Sgjelinek if (mixed_pools(zoneid)) { 53217ef01d19Sgjelinek zerror(gettext("Zone is using multiple resource " 53227ef01d19Sgjelinek "pools. The pool\nconfiguration cannot be " 53237ef01d19Sgjelinek "applied without rebooting.")); 53247ef01d19Sgjelinek res = Z_ERR; 53257ef01d19Sgjelinek } else { 53267ef01d19Sgjelinek 53270209230bSgjelinek /* 53287ef01d19Sgjelinek * The next two blocks of code attempt to set up 53297ef01d19Sgjelinek * temporary pools as well as persistent pools. In 53307ef01d19Sgjelinek * both cases we call the functions unconditionally. 53317ef01d19Sgjelinek * Within each funtion the code will check if the zone 53327ef01d19Sgjelinek * is actually configured for a temporary pool or 53337ef01d19Sgjelinek * persistent pool and just return if there is nothing 53347ef01d19Sgjelinek * to do. 53350209230bSgjelinek */ 53367ef01d19Sgjelinek if ((err = zonecfg_bind_tmp_pool(handle, zoneid, 53377ef01d19Sgjelinek pool_err, sizeof (pool_err))) != Z_OK) { 53387ef01d19Sgjelinek if (err == Z_POOL || err == Z_POOL_CREATE || 53397ef01d19Sgjelinek err == Z_POOL_BIND) 53407ef01d19Sgjelinek zerror("%s: %s", zonecfg_strerror(err), 53417ef01d19Sgjelinek pool_err); 53420209230bSgjelinek else 53437ef01d19Sgjelinek zerror(gettext("could not bind zone to " 53447ef01d19Sgjelinek "temporary pool: %s"), 53457ef01d19Sgjelinek zonecfg_strerror(err)); 53460209230bSgjelinek res = Z_ERR; 53470209230bSgjelinek } 53480209230bSgjelinek 53490209230bSgjelinek if ((err = zonecfg_bind_pool(handle, zoneid, pool_err, 53500209230bSgjelinek sizeof (pool_err))) != Z_OK) { 53510209230bSgjelinek if (err == Z_POOL || err == Z_POOL_BIND) 53527ef01d19Sgjelinek zerror("%s: %s", zonecfg_strerror(err), 53537ef01d19Sgjelinek pool_err); 53540209230bSgjelinek else 53550209230bSgjelinek zerror("%s", zonecfg_strerror(err)); 53560209230bSgjelinek } 53577ef01d19Sgjelinek } 53587ef01d19Sgjelinek } 53590209230bSgjelinek 53600209230bSgjelinek /* 53610209230bSgjelinek * If a memory cap is configured, set the cap in the kernel using 53620209230bSgjelinek * zone_setattr() and make sure the rcapd SMF service is enabled. 53630209230bSgjelinek */ 53640209230bSgjelinek if (zonecfg_getmcapent(handle, &mcap) == Z_OK) { 53650209230bSgjelinek uint64_t num; 53660209230bSgjelinek char smf_err[128]; 53670209230bSgjelinek 53680209230bSgjelinek num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10); 53690209230bSgjelinek if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) { 53700209230bSgjelinek zerror(gettext("could not set zone memory cap")); 53710209230bSgjelinek res = Z_ERR; 53720209230bSgjelinek } 53730209230bSgjelinek 53740209230bSgjelinek if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) { 53750209230bSgjelinek zerror(gettext("enabling system/rcap service failed: " 53760209230bSgjelinek "%s"), smf_err); 53770209230bSgjelinek res = Z_ERR; 53780209230bSgjelinek } 53790209230bSgjelinek } 53800209230bSgjelinek 53810209230bSgjelinek zonecfg_fini_handle(handle); 53820209230bSgjelinek 53830209230bSgjelinek return (res); 53840209230bSgjelinek } 53850209230bSgjelinek 5386555afedfScarlsonj static int 53877c478bd9Sstevel@tonic-gate help_func(int argc, char *argv[]) 53887c478bd9Sstevel@tonic-gate { 53897c478bd9Sstevel@tonic-gate int arg, cmd_num; 53907c478bd9Sstevel@tonic-gate 53917c478bd9Sstevel@tonic-gate if (argc == 0) { 53927c478bd9Sstevel@tonic-gate (void) usage(B_TRUE); 53937c478bd9Sstevel@tonic-gate return (Z_OK); 53947c478bd9Sstevel@tonic-gate } 53957c478bd9Sstevel@tonic-gate optind = 0; 53967c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 53977c478bd9Sstevel@tonic-gate switch (arg) { 53987c478bd9Sstevel@tonic-gate case '?': 53997c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 54007c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 54017c478bd9Sstevel@tonic-gate default: 54027c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 54037c478bd9Sstevel@tonic-gate return (Z_USAGE); 54047c478bd9Sstevel@tonic-gate } 54057c478bd9Sstevel@tonic-gate } 54067c478bd9Sstevel@tonic-gate while (optind < argc) { 5407394a25e2Scarlsonj /* Private commands have NULL short_usage; omit them */ 5408394a25e2Scarlsonj if ((cmd_num = cmd_match(argv[optind])) < 0 || 5409394a25e2Scarlsonj cmdtab[cmd_num].short_usage == NULL) { 54107c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 54117c478bd9Sstevel@tonic-gate return (Z_USAGE); 54127c478bd9Sstevel@tonic-gate } 54137c478bd9Sstevel@tonic-gate sub_usage(cmdtab[cmd_num].short_usage, cmd_num); 54147c478bd9Sstevel@tonic-gate optind++; 54157c478bd9Sstevel@tonic-gate } 54167c478bd9Sstevel@tonic-gate return (Z_OK); 54177c478bd9Sstevel@tonic-gate } 54187c478bd9Sstevel@tonic-gate 54197c478bd9Sstevel@tonic-gate /* 54207c478bd9Sstevel@tonic-gate * Returns: CMD_MIN thru CMD_MAX on success, -1 on error 54217c478bd9Sstevel@tonic-gate */ 54227c478bd9Sstevel@tonic-gate 54237c478bd9Sstevel@tonic-gate static int 54247c478bd9Sstevel@tonic-gate cmd_match(char *cmd) 54257c478bd9Sstevel@tonic-gate { 54267c478bd9Sstevel@tonic-gate int i; 54277c478bd9Sstevel@tonic-gate 54287c478bd9Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 54297c478bd9Sstevel@tonic-gate /* return only if there is an exact match */ 54307c478bd9Sstevel@tonic-gate if (strcmp(cmd, cmdtab[i].cmd_name) == 0) 54317c478bd9Sstevel@tonic-gate return (cmdtab[i].cmd_num); 54327c478bd9Sstevel@tonic-gate } 54337c478bd9Sstevel@tonic-gate return (-1); 54347c478bd9Sstevel@tonic-gate } 54357c478bd9Sstevel@tonic-gate 54367c478bd9Sstevel@tonic-gate static int 54377c478bd9Sstevel@tonic-gate parse_and_run(int argc, char *argv[]) 54387c478bd9Sstevel@tonic-gate { 54397c478bd9Sstevel@tonic-gate int i = cmd_match(argv[0]); 54407c478bd9Sstevel@tonic-gate 54417c478bd9Sstevel@tonic-gate if (i < 0) 54427c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 54437c478bd9Sstevel@tonic-gate return (cmdtab[i].handler(argc - 1, &(argv[1]))); 54447c478bd9Sstevel@tonic-gate } 54457c478bd9Sstevel@tonic-gate 54467c478bd9Sstevel@tonic-gate static char * 54477c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname) 54487c478bd9Sstevel@tonic-gate { 54497c478bd9Sstevel@tonic-gate char *last_slash, *execbasename; 54507c478bd9Sstevel@tonic-gate 54517c478bd9Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 54527c478bd9Sstevel@tonic-gate for (;;) { 54537c478bd9Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 54547c478bd9Sstevel@tonic-gate if (last_slash == NULL) { 54557c478bd9Sstevel@tonic-gate execbasename = execfullname; 54567c478bd9Sstevel@tonic-gate break; 54577c478bd9Sstevel@tonic-gate } else { 54587c478bd9Sstevel@tonic-gate execbasename = last_slash + 1; 54597c478bd9Sstevel@tonic-gate if (*execbasename == '\0') { 54607c478bd9Sstevel@tonic-gate *last_slash = '\0'; 54617c478bd9Sstevel@tonic-gate continue; 54627c478bd9Sstevel@tonic-gate } 54637c478bd9Sstevel@tonic-gate break; 54647c478bd9Sstevel@tonic-gate } 54657c478bd9Sstevel@tonic-gate } 54667c478bd9Sstevel@tonic-gate return (execbasename); 54677c478bd9Sstevel@tonic-gate } 54687c478bd9Sstevel@tonic-gate 54697c478bd9Sstevel@tonic-gate int 54707c478bd9Sstevel@tonic-gate main(int argc, char **argv) 54717c478bd9Sstevel@tonic-gate { 54727c478bd9Sstevel@tonic-gate int arg; 54737c478bd9Sstevel@tonic-gate zoneid_t zid; 5474108322fbScarlsonj struct stat st; 54759acbbeafSnn35248 char *zone_lock_env; 54769acbbeafSnn35248 int err; 54777c478bd9Sstevel@tonic-gate 54787c478bd9Sstevel@tonic-gate if ((locale = setlocale(LC_ALL, "")) == NULL) 54797c478bd9Sstevel@tonic-gate locale = "C"; 54807c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 54817c478bd9Sstevel@tonic-gate setbuf(stdout, NULL); 54827c478bd9Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_IGN); 54837c478bd9Sstevel@tonic-gate execname = get_execbasename(argv[0]); 54847c478bd9Sstevel@tonic-gate target_zone = NULL; 54857c478bd9Sstevel@tonic-gate if (chdir("/") != 0) { 54867c478bd9Sstevel@tonic-gate zerror(gettext("could not change directory to /.")); 54877c478bd9Sstevel@tonic-gate exit(Z_ERR); 54887c478bd9Sstevel@tonic-gate } 54897c478bd9Sstevel@tonic-gate 549099653d4eSeschrock if (init_zfs() != Z_OK) 549199653d4eSeschrock exit(Z_ERR); 549299653d4eSeschrock 5493555afedfScarlsonj while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) { 54947c478bd9Sstevel@tonic-gate switch (arg) { 54957c478bd9Sstevel@tonic-gate case '?': 54967c478bd9Sstevel@tonic-gate return (usage(B_TRUE)); 5497555afedfScarlsonj case 'u': 5498555afedfScarlsonj target_uuid = optarg; 5499555afedfScarlsonj break; 55007c478bd9Sstevel@tonic-gate case 'z': 55017c478bd9Sstevel@tonic-gate target_zone = optarg; 55027c478bd9Sstevel@tonic-gate break; 5503108322fbScarlsonj case 'R': /* private option for admin/install use */ 5504108322fbScarlsonj if (*optarg != '/') { 5505108322fbScarlsonj zerror(gettext("root path must be absolute.")); 5506108322fbScarlsonj exit(Z_ERR); 5507108322fbScarlsonj } 5508108322fbScarlsonj if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) { 5509108322fbScarlsonj zerror( 5510108322fbScarlsonj gettext("root path must be a directory.")); 5511108322fbScarlsonj exit(Z_ERR); 5512108322fbScarlsonj } 5513108322fbScarlsonj zonecfg_set_root(optarg); 5514108322fbScarlsonj break; 55157c478bd9Sstevel@tonic-gate default: 55167c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 55177c478bd9Sstevel@tonic-gate } 55187c478bd9Sstevel@tonic-gate } 55197c478bd9Sstevel@tonic-gate 55207c478bd9Sstevel@tonic-gate if (optind >= argc) 55217c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 5522555afedfScarlsonj 5523555afedfScarlsonj if (target_uuid != NULL && *target_uuid != '\0') { 5524555afedfScarlsonj uuid_t uuid; 5525555afedfScarlsonj static char newtarget[ZONENAME_MAX]; 5526555afedfScarlsonj 5527555afedfScarlsonj if (uuid_parse(target_uuid, uuid) == -1) { 5528555afedfScarlsonj zerror(gettext("illegal UUID value specified")); 5529555afedfScarlsonj exit(Z_ERR); 5530555afedfScarlsonj } 5531555afedfScarlsonj if (zonecfg_get_name_by_uuid(uuid, newtarget, 5532555afedfScarlsonj sizeof (newtarget)) == Z_OK) 5533555afedfScarlsonj target_zone = newtarget; 5534555afedfScarlsonj } 5535555afedfScarlsonj 55367c478bd9Sstevel@tonic-gate if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) { 55377c478bd9Sstevel@tonic-gate errno = Z_NO_ZONE; 55387c478bd9Sstevel@tonic-gate zperror(target_zone, B_TRUE); 55397c478bd9Sstevel@tonic-gate exit(Z_ERR); 55407c478bd9Sstevel@tonic-gate } 55419acbbeafSnn35248 55429acbbeafSnn35248 /* 55439acbbeafSnn35248 * See if we have inherited the right to manipulate this zone from 55449acbbeafSnn35248 * a zoneadm instance in our ancestry. If so, set zone_lock_cnt to 55459acbbeafSnn35248 * indicate it. If not, make that explicit in our environment. 55469acbbeafSnn35248 */ 55479acbbeafSnn35248 zone_lock_env = getenv(LOCK_ENV_VAR); 55489acbbeafSnn35248 if (zone_lock_env == NULL) { 55499acbbeafSnn35248 if (putenv(zoneadm_lock_not_held) != 0) { 55509acbbeafSnn35248 zperror(target_zone, B_TRUE); 55519acbbeafSnn35248 exit(Z_ERR); 55529acbbeafSnn35248 } 55539acbbeafSnn35248 } else { 55549acbbeafSnn35248 zoneadm_is_nested = B_TRUE; 55559acbbeafSnn35248 if (atoi(zone_lock_env) == 1) 55569acbbeafSnn35248 zone_lock_cnt = 1; 55579acbbeafSnn35248 } 55589acbbeafSnn35248 55599acbbeafSnn35248 /* 55609acbbeafSnn35248 * If we are going to be operating on a single zone, retrieve its 55619acbbeafSnn35248 * brand type and determine whether it is native or not. 55629acbbeafSnn35248 */ 55639acbbeafSnn35248 if ((target_zone != NULL) && 55649acbbeafSnn35248 (strcmp(target_zone, GLOBAL_ZONENAME) != NULL)) { 55659acbbeafSnn35248 if (zone_get_brand(target_zone, target_brand, 55669acbbeafSnn35248 sizeof (target_brand)) != Z_OK) { 55679acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 55689acbbeafSnn35248 exit(Z_ERR); 55699acbbeafSnn35248 } 55709acbbeafSnn35248 is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0); 55719acbbeafSnn35248 } 55729acbbeafSnn35248 55739acbbeafSnn35248 err = parse_and_run(argc - optind, &argv[optind]); 55749acbbeafSnn35248 55759acbbeafSnn35248 return (err); 55767c478bd9Sstevel@tonic-gate } 5577