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 /* 236cfd72c6Sgjelinek * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * zoneadm is a command interpreter for zone administration. It is all in 297c478bd9Sstevel@tonic-gate * C (i.e., no lex/yacc), and all the argument passing is argc/argv based. 307c478bd9Sstevel@tonic-gate * main() calls parse_and_run() which calls cmd_match(), then invokes the 317c478bd9Sstevel@tonic-gate * appropriate command's handler function. The rest of the program is the 327c478bd9Sstevel@tonic-gate * handler functions and their helper functions. 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * Some of the helper functions are used largely to simplify I18N: reducing 357c478bd9Sstevel@tonic-gate * the need for translation notes. This is particularly true of many of 367c478bd9Sstevel@tonic-gate * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather 377c478bd9Sstevel@tonic-gate * than zerror(gettext("foo failed")) with a translation note indicating 387c478bd9Sstevel@tonic-gate * that "foo" need not be translated. 397c478bd9Sstevel@tonic-gate */ 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #include <stdio.h> 427c478bd9Sstevel@tonic-gate #include <errno.h> 437c478bd9Sstevel@tonic-gate #include <unistd.h> 447c478bd9Sstevel@tonic-gate #include <signal.h> 457c478bd9Sstevel@tonic-gate #include <stdarg.h> 467c478bd9Sstevel@tonic-gate #include <ctype.h> 477c478bd9Sstevel@tonic-gate #include <stdlib.h> 487c478bd9Sstevel@tonic-gate #include <string.h> 497c478bd9Sstevel@tonic-gate #include <wait.h> 507c478bd9Sstevel@tonic-gate #include <zone.h> 517c478bd9Sstevel@tonic-gate #include <priv.h> 527c478bd9Sstevel@tonic-gate #include <locale.h> 537c478bd9Sstevel@tonic-gate #include <libintl.h> 547c478bd9Sstevel@tonic-gate #include <libzonecfg.h> 557c478bd9Sstevel@tonic-gate #include <bsm/adt.h> 569acbbeafSnn35248 #include <sys/brand.h> 577c478bd9Sstevel@tonic-gate #include <sys/param.h> 587c478bd9Sstevel@tonic-gate #include <sys/types.h> 597c478bd9Sstevel@tonic-gate #include <sys/stat.h> 607c478bd9Sstevel@tonic-gate #include <sys/statvfs.h> 617c478bd9Sstevel@tonic-gate #include <assert.h> 627c478bd9Sstevel@tonic-gate #include <sys/sockio.h> 637c478bd9Sstevel@tonic-gate #include <sys/mntent.h> 647c478bd9Sstevel@tonic-gate #include <limits.h> 650b5de56dSgjelinek #include <dirent.h> 66555afedfScarlsonj #include <uuid/uuid.h> 67948f2876Sss150715 #include <libdlpi.h> 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate #include <fcntl.h> 707c478bd9Sstevel@tonic-gate #include <door.h> 717c478bd9Sstevel@tonic-gate #include <macros.h> 727c478bd9Sstevel@tonic-gate #include <libgen.h> 73865e09a4Sgjelinek #include <fnmatch.h> 74e7f3c547Sgjelinek #include <sys/modctl.h> 759acbbeafSnn35248 #include <libbrand.h> 760209230bSgjelinek #include <libscf.h> 777ef01d19Sgjelinek #include <procfs.h> 78d9e728a2Sgjelinek #include <strings.h> 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate #include <pool.h> 817c478bd9Sstevel@tonic-gate #include <sys/pool.h> 820209230bSgjelinek #include <sys/priocntl.h> 830209230bSgjelinek #include <sys/fsspriocntl.h> 847c478bd9Sstevel@tonic-gate 850b5de56dSgjelinek #include "zoneadm.h" 860b5de56dSgjelinek 877c478bd9Sstevel@tonic-gate #define MAXARGS 8 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate /* Reflects kernel zone entries */ 907c478bd9Sstevel@tonic-gate typedef struct zone_entry { 917c478bd9Sstevel@tonic-gate zoneid_t zid; 927c478bd9Sstevel@tonic-gate char zname[ZONENAME_MAX]; 937c478bd9Sstevel@tonic-gate char *zstate_str; 947c478bd9Sstevel@tonic-gate zone_state_t zstate_num; 959acbbeafSnn35248 char zbrand[MAXNAMELEN]; 967c478bd9Sstevel@tonic-gate char zroot[MAXPATHLEN]; 97555afedfScarlsonj char zuuid[UUID_PRINTABLE_STRING_LENGTH]; 98f4b3ec61Sdh155122 zone_iptype_t ziptype; 997c478bd9Sstevel@tonic-gate } zone_entry_t; 1007c478bd9Sstevel@tonic-gate 10184561e8cStd153743 #define CLUSTER_BRAND_NAME "cluster" 10284561e8cStd153743 1037c478bd9Sstevel@tonic-gate static zone_entry_t *zents; 1047c478bd9Sstevel@tonic-gate static size_t nzents; 1059acbbeafSnn35248 static boolean_t is_native_zone = B_TRUE; 1067c478bd9Sstevel@tonic-gate 1071390a385Sgjelinek #define LOOPBACK_IF "lo0" 1081390a385Sgjelinek #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af)) 1091390a385Sgjelinek 1101390a385Sgjelinek struct net_if { 1111390a385Sgjelinek char *name; 1121390a385Sgjelinek int af; 1131390a385Sgjelinek }; 1141390a385Sgjelinek 1157c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */ 1167c478bd9Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 1177c478bd9Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate struct cmd { 1207c478bd9Sstevel@tonic-gate uint_t cmd_num; /* command number */ 1217c478bd9Sstevel@tonic-gate char *cmd_name; /* command name */ 1227c478bd9Sstevel@tonic-gate char *short_usage; /* short form help */ 1237c478bd9Sstevel@tonic-gate int (*handler)(int argc, char *argv[]); /* function to call */ 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate }; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate #define SHELP_HELP "help" 1283f2f09c1Sdp #define SHELP_BOOT "boot [-- boot_arguments]" 1297c478bd9Sstevel@tonic-gate #define SHELP_HALT "halt" 1307c478bd9Sstevel@tonic-gate #define SHELP_READY "ready" 1313f2f09c1Sdp #define SHELP_REBOOT "reboot [-- boot_arguments]" 1327c478bd9Sstevel@tonic-gate #define SHELP_LIST "list [-cipv]" 1337c478bd9Sstevel@tonic-gate #define SHELP_VERIFY "verify" 1349acbbeafSnn35248 #define SHELP_INSTALL "install [-x nodataset] [brand-specific args]" 135ff17c8bfSgjelinek #define SHELP_UNINSTALL "uninstall [-F] [brand-specific args]" 136ff17c8bfSgjelinek #define SHELP_CLONE "clone [-m method] [-s <ZFS snapshot>] "\ 137ff17c8bfSgjelinek "[brand-specific args] zonename" 138865e09a4Sgjelinek #define SHELP_MOVE "move zonepath" 139ff17c8bfSgjelinek #define SHELP_DETACH "detach [-n] [brand-specific args]" 140ff17c8bfSgjelinek #define SHELP_ATTACH "attach [-F] [-n <path>] [brand-specific args]" 141555afedfScarlsonj #define SHELP_MARK "mark incomplete" 1427c478bd9Sstevel@tonic-gate 1439acbbeafSnn35248 #define EXEC_PREFIX "exec " 1449acbbeafSnn35248 #define EXEC_LEN (strlen(EXEC_PREFIX)) 1459acbbeafSnn35248 #define RMCOMMAND "/usr/bin/rm -rf" 1469acbbeafSnn35248 1479acbbeafSnn35248 static int cleanup_zonepath(char *, boolean_t); 1489acbbeafSnn35248 149f4b3ec61Sdh155122 1507c478bd9Sstevel@tonic-gate static int help_func(int argc, char *argv[]); 1517c478bd9Sstevel@tonic-gate static int ready_func(int argc, char *argv[]); 1527c478bd9Sstevel@tonic-gate static int boot_func(int argc, char *argv[]); 1537c478bd9Sstevel@tonic-gate static int halt_func(int argc, char *argv[]); 1547c478bd9Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]); 1557c478bd9Sstevel@tonic-gate static int list_func(int argc, char *argv[]); 1567c478bd9Sstevel@tonic-gate static int verify_func(int argc, char *argv[]); 1577c478bd9Sstevel@tonic-gate static int install_func(int argc, char *argv[]); 1587c478bd9Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]); 159108322fbScarlsonj static int mount_func(int argc, char *argv[]); 160108322fbScarlsonj static int unmount_func(int argc, char *argv[]); 161865e09a4Sgjelinek static int clone_func(int argc, char *argv[]); 162865e09a4Sgjelinek static int move_func(int argc, char *argv[]); 163ee519a1fSgjelinek static int detach_func(int argc, char *argv[]); 164ee519a1fSgjelinek static int attach_func(int argc, char *argv[]); 165555afedfScarlsonj static int mark_func(int argc, char *argv[]); 1660209230bSgjelinek static int apply_func(int argc, char *argv[]); 1677c478bd9Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running, 1689acbbeafSnn35248 boolean_t unsafe_when_running, boolean_t force); 1697c478bd9Sstevel@tonic-gate static int cmd_match(char *cmd); 170ce28b40eSzt129084 static int verify_details(int, char *argv[]); 171ce28b40eSzt129084 static int verify_brand(zone_dochandle_t, int, char *argv[]); 172ce28b40eSzt129084 static int invoke_brand_handler(int, char *argv[]); 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate static struct cmd cmdtab[] = { 1757c478bd9Sstevel@tonic-gate { CMD_HELP, "help", SHELP_HELP, help_func }, 1767c478bd9Sstevel@tonic-gate { CMD_BOOT, "boot", SHELP_BOOT, boot_func }, 1777c478bd9Sstevel@tonic-gate { CMD_HALT, "halt", SHELP_HALT, halt_func }, 1787c478bd9Sstevel@tonic-gate { CMD_READY, "ready", SHELP_READY, ready_func }, 1797c478bd9Sstevel@tonic-gate { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func }, 1807c478bd9Sstevel@tonic-gate { CMD_LIST, "list", SHELP_LIST, list_func }, 1817c478bd9Sstevel@tonic-gate { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func }, 1827c478bd9Sstevel@tonic-gate { CMD_INSTALL, "install", SHELP_INSTALL, install_func }, 1837c478bd9Sstevel@tonic-gate { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL, 184108322fbScarlsonj uninstall_func }, 185865e09a4Sgjelinek /* mount and unmount are private commands for admin/install */ 186108322fbScarlsonj { CMD_MOUNT, "mount", NULL, mount_func }, 187865e09a4Sgjelinek { CMD_UNMOUNT, "unmount", NULL, unmount_func }, 188865e09a4Sgjelinek { CMD_CLONE, "clone", SHELP_CLONE, clone_func }, 189ee519a1fSgjelinek { CMD_MOVE, "move", SHELP_MOVE, move_func }, 190ee519a1fSgjelinek { CMD_DETACH, "detach", SHELP_DETACH, detach_func }, 191555afedfScarlsonj { CMD_ATTACH, "attach", SHELP_ATTACH, attach_func }, 1920209230bSgjelinek { CMD_MARK, "mark", SHELP_MARK, mark_func }, 1930209230bSgjelinek { CMD_APPLY, "apply", NULL, apply_func } 1947c478bd9Sstevel@tonic-gate }; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate /* global variables */ 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */ 1997c478bd9Sstevel@tonic-gate static char *execname; 2009acbbeafSnn35248 static char target_brand[MAXNAMELEN]; 2017c478bd9Sstevel@tonic-gate static char *locale; 2020b5de56dSgjelinek char *target_zone; 203555afedfScarlsonj static char *target_uuid; 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate /* used in do_subproc() and signal handler */ 2067c478bd9Sstevel@tonic-gate static volatile boolean_t child_killed; 2079acbbeafSnn35248 static int do_subproc_cnt = 0; 2089acbbeafSnn35248 2099acbbeafSnn35248 /* 2109acbbeafSnn35248 * Used to indicate whether this zoneadm instance has another zoneadm 2119acbbeafSnn35248 * instance in its ancestry. 2129acbbeafSnn35248 */ 2139acbbeafSnn35248 static boolean_t zoneadm_is_nested = B_FALSE; 2149acbbeafSnn35248 2150b5de56dSgjelinek char * 2167c478bd9Sstevel@tonic-gate cmd_to_str(int cmd_num) 2177c478bd9Sstevel@tonic-gate { 2187c478bd9Sstevel@tonic-gate assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2197c478bd9Sstevel@tonic-gate return (cmdtab[cmd_num].cmd_name); 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */ 2237c478bd9Sstevel@tonic-gate static char * 2247c478bd9Sstevel@tonic-gate long_help(int cmd_num) 2257c478bd9Sstevel@tonic-gate { 2267e362f58Scomay assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2277c478bd9Sstevel@tonic-gate switch (cmd_num) { 2287c478bd9Sstevel@tonic-gate case CMD_HELP: 2297c478bd9Sstevel@tonic-gate return (gettext("Print usage message.")); 2307c478bd9Sstevel@tonic-gate case CMD_BOOT: 2313f2f09c1Sdp return (gettext("Activates (boots) specified zone. See " 2323f2f09c1Sdp "zoneadm(1m) for valid boot\n\targuments.")); 2337c478bd9Sstevel@tonic-gate case CMD_HALT: 2349e518655Sgjelinek return (gettext("Halts specified zone, bypassing shutdown " 2359e518655Sgjelinek "scripts and removing runtime\n\tresources of the zone.")); 2367c478bd9Sstevel@tonic-gate case CMD_READY: 2379e518655Sgjelinek return (gettext("Prepares a zone for running applications but " 2389e518655Sgjelinek "does not start any user\n\tprocesses in the zone.")); 2397c478bd9Sstevel@tonic-gate case CMD_REBOOT: 2409e518655Sgjelinek return (gettext("Restarts the zone (equivalent to a halt / " 2413f2f09c1Sdp "boot sequence).\n\tFails if the zone is not active. " 2423f2f09c1Sdp "See zoneadm(1m) for valid boot\n\targuments.")); 2437c478bd9Sstevel@tonic-gate case CMD_LIST: 2447c478bd9Sstevel@tonic-gate return (gettext("Lists the current zones, or a " 2457c478bd9Sstevel@tonic-gate "specific zone if indicated. By default,\n\tall " 2467c478bd9Sstevel@tonic-gate "running zones are listed, though this can be " 2477c478bd9Sstevel@tonic-gate "expanded to all\n\tinstalled zones with the -i " 2487c478bd9Sstevel@tonic-gate "option or all configured zones with the\n\t-c " 249555afedfScarlsonj "option. When used with the general -z <zone> and/or -u " 250555afedfScarlsonj "<uuid-match>\n\toptions, lists only the specified " 251555afedfScarlsonj "matching zone, but lists it\n\tregardless of its state, " 252555afedfScarlsonj "and the -i and -c options are disallowed. The\n\t-v " 253555afedfScarlsonj "option can be used to display verbose information: zone " 254555afedfScarlsonj "name, id,\n\tcurrent state, root directory and options. " 255555afedfScarlsonj "The -p option can be used\n\tto request machine-parsable " 256555afedfScarlsonj "output. The -v and -p options are mutually\n\texclusive." 257555afedfScarlsonj " If neither -v nor -p is used, just the zone name is " 258555afedfScarlsonj "listed.")); 2597c478bd9Sstevel@tonic-gate case CMD_VERIFY: 2607c478bd9Sstevel@tonic-gate return (gettext("Check to make sure the configuration " 2617c478bd9Sstevel@tonic-gate "can safely be instantiated\n\ton the machine: " 2627c478bd9Sstevel@tonic-gate "physical network interfaces exist, etc.")); 2637c478bd9Sstevel@tonic-gate case CMD_INSTALL: 2640b5de56dSgjelinek return (gettext("Install the configuration on to the system. " 2650b5de56dSgjelinek "The -x nodataset option\n\tcan be used to prevent the " 2660b5de56dSgjelinek "creation of a new ZFS file system for the\n\tzone " 2679acbbeafSnn35248 "(assuming the zonepath is within a ZFS file system).\n\t" 2689acbbeafSnn35248 "All other arguments are passed to the brand installation " 269ff17c8bfSgjelinek "function;\n\tsee brands(5) for more information.")); 2707c478bd9Sstevel@tonic-gate case CMD_UNINSTALL: 2719e518655Sgjelinek return (gettext("Uninstall the configuration from the system. " 272ff17c8bfSgjelinek "The -F flag can be used\n\tto force the action. All " 273ff17c8bfSgjelinek "other arguments are passed to the brand\n\tuninstall " 274ff17c8bfSgjelinek "function; see brands(5) for more information.")); 275865e09a4Sgjelinek case CMD_CLONE: 2760b5de56dSgjelinek return (gettext("Clone the installation of another zone. " 2770b5de56dSgjelinek "The -m option can be used to\n\tspecify 'copy' which " 2780b5de56dSgjelinek "forces a copy of the source zone. The -s option\n\t" 2790b5de56dSgjelinek "can be used to specify the name of a ZFS snapshot " 2800b5de56dSgjelinek "that was taken from\n\ta previous clone command. The " 2810b5de56dSgjelinek "snapshot will be used as the source\n\tinstead of " 282ff17c8bfSgjelinek "creating a new ZFS snapshot. All other arguments are " 283ff17c8bfSgjelinek "passed\n\tto the brand clone function; see " 284ff17c8bfSgjelinek "brands(5) for more information.")); 285865e09a4Sgjelinek case CMD_MOVE: 286865e09a4Sgjelinek return (gettext("Move the zone to a new zonepath.")); 2879e518655Sgjelinek case CMD_DETACH: 2889e518655Sgjelinek return (gettext("Detach the zone from the system. The zone " 2899e518655Sgjelinek "state is changed to\n\t'configured' (but the files under " 2909e518655Sgjelinek "the zonepath are untouched).\n\tThe zone can subsequently " 2919e518655Sgjelinek "be attached, or can be moved to another\n\tsystem and " 2928cd327d5Sgjelinek "attached there. The -n option can be used to specify\n\t" 2938cd327d5Sgjelinek "'no-execute' mode. When -n is used, the information " 2948cd327d5Sgjelinek "needed to attach\n\tthe zone is sent to standard output " 295ff17c8bfSgjelinek "but the zone is not actually\n\tdetached. All other " 296ff17c8bfSgjelinek "arguments are passed to the brand detach function;\n\tsee " 297ff17c8bfSgjelinek "brands(5) for more information.")); 2989e518655Sgjelinek case CMD_ATTACH: 2999e518655Sgjelinek return (gettext("Attach the zone to the system. The zone " 3009e518655Sgjelinek "state must be 'configured'\n\tprior to attach; upon " 3019e518655Sgjelinek "successful completion, the zone state will be\n\t" 3029e518655Sgjelinek "'installed'. The system software on the current " 3039e518655Sgjelinek "system must be\n\tcompatible with the software on the " 304ff17c8bfSgjelinek "zone's original system.\n\tSpecify -F " 3056cfd72c6Sgjelinek "to force the attach and skip software compatibility " 3066cfd72c6Sgjelinek "tests.\n\tThe -n option can be used to specify " 3076cfd72c6Sgjelinek "'no-execute' mode. When -n is\n\tused, the information " 3086cfd72c6Sgjelinek "needed to attach the zone is read from the\n\tspecified " 3096cfd72c6Sgjelinek "path and the configuration is only validated. The path " 310ff17c8bfSgjelinek "can\n\tbe '-' to specify standard input. The -F and -n " 311ff17c8bfSgjelinek "options are mutually\n\texclusive. All other arguments " 312ff17c8bfSgjelinek "are passed to the brand attach\n\tfunction; see " 313ff17c8bfSgjelinek "brands(5) for more information.")); 314555afedfScarlsonj case CMD_MARK: 315555afedfScarlsonj return (gettext("Set the state of the zone. This can be used " 316555afedfScarlsonj "to force the zone\n\tstate to 'incomplete' " 317555afedfScarlsonj "administratively if some activity has rendered\n\tthe " 318555afedfScarlsonj "zone permanently unusable. The only valid state that " 319555afedfScarlsonj "may be\n\tspecified is 'incomplete'.")); 320108322fbScarlsonj default: 321108322fbScarlsonj return (""); 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate /* NOTREACHED */ 3247e362f58Scomay return (NULL); 3257c478bd9Sstevel@tonic-gate } 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate /* 3287c478bd9Sstevel@tonic-gate * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for 3297c478bd9Sstevel@tonic-gate * unexpected errors. 3307c478bd9Sstevel@tonic-gate */ 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate static int 3337c478bd9Sstevel@tonic-gate usage(boolean_t explicit) 3347c478bd9Sstevel@tonic-gate { 3357c478bd9Sstevel@tonic-gate int i; 3367c478bd9Sstevel@tonic-gate FILE *fd = explicit ? stdout : stderr; 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname); 339555afedfScarlsonj (void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n", 340555afedfScarlsonj execname); 341555afedfScarlsonj (void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname, 3427c478bd9Sstevel@tonic-gate gettext("subcommand")); 3437c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands")); 3447c478bd9Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 345108322fbScarlsonj if (cmdtab[i].short_usage == NULL) 346108322fbScarlsonj continue; 3477c478bd9Sstevel@tonic-gate (void) fprintf(fd, "%s\n", cmdtab[i].short_usage); 3487c478bd9Sstevel@tonic-gate if (explicit) 3497c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\t%s\n\n", long_help(i)); 3507c478bd9Sstevel@tonic-gate } 3517c478bd9Sstevel@tonic-gate if (!explicit) 3527c478bd9Sstevel@tonic-gate (void) fputs("\n", fd); 3537c478bd9Sstevel@tonic-gate return (Z_USAGE); 3547c478bd9Sstevel@tonic-gate } 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate static void 3577c478bd9Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num) 3587c478bd9Sstevel@tonic-gate { 3597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage); 3607c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s\n", long_help(cmd_num)); 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate /* 3647c478bd9Sstevel@tonic-gate * zperror() is like perror(3c) except that this also prints the executable 3657c478bd9Sstevel@tonic-gate * name at the start of the message, and takes a boolean indicating whether 3667c478bd9Sstevel@tonic-gate * to call libc'c strerror() or that from libzonecfg. 3677c478bd9Sstevel@tonic-gate */ 3687c478bd9Sstevel@tonic-gate 3690b5de56dSgjelinek void 3707c478bd9Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error) 3717c478bd9Sstevel@tonic-gate { 3727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", execname, str, 3737c478bd9Sstevel@tonic-gate zonecfg_error ? zonecfg_strerror(errno) : strerror(errno)); 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate /* 3777c478bd9Sstevel@tonic-gate * zperror2() is very similar to zperror() above, except it also prints a 3787c478bd9Sstevel@tonic-gate * supplied zone name after the executable. 3797c478bd9Sstevel@tonic-gate * 3807c478bd9Sstevel@tonic-gate * All current consumers of this function want libzonecfg's strerror() rather 3817c478bd9Sstevel@tonic-gate * than libc's; if this ever changes, this function can be made more generic 3827c478bd9Sstevel@tonic-gate * like zperror() above. 3837c478bd9Sstevel@tonic-gate */ 3847c478bd9Sstevel@tonic-gate 3850b5de56dSgjelinek void 3867c478bd9Sstevel@tonic-gate zperror2(const char *zone, const char *str) 3877c478bd9Sstevel@tonic-gate { 3887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str, 3897c478bd9Sstevel@tonic-gate zonecfg_strerror(errno)); 3907c478bd9Sstevel@tonic-gate } 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */ 3930b5de56dSgjelinek void 3947c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...) 3957c478bd9Sstevel@tonic-gate { 3967c478bd9Sstevel@tonic-gate va_list alist; 3977c478bd9Sstevel@tonic-gate 3987c478bd9Sstevel@tonic-gate va_start(alist, fmt); 3997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", execname); 4007c478bd9Sstevel@tonic-gate if (target_zone != NULL) 4017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "zone '%s': ", target_zone); 4027c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, alist); 4037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 4047c478bd9Sstevel@tonic-gate va_end(alist); 4057c478bd9Sstevel@tonic-gate } 4067c478bd9Sstevel@tonic-gate 4077c478bd9Sstevel@tonic-gate static void * 4087c478bd9Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize) 4097c478bd9Sstevel@tonic-gate { 4107c478bd9Sstevel@tonic-gate void *r = calloc(nelem, elsize); 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate if (r == NULL) { 4137c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), 4147c478bd9Sstevel@tonic-gate (ulong_t)nelem * elsize, strerror(errno)); 4157c478bd9Sstevel@tonic-gate exit(Z_ERR); 4167c478bd9Sstevel@tonic-gate } 4177c478bd9Sstevel@tonic-gate return (r); 4187c478bd9Sstevel@tonic-gate } 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate static void 4217c478bd9Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable) 4227c478bd9Sstevel@tonic-gate { 4237c478bd9Sstevel@tonic-gate static boolean_t firsttime = B_TRUE; 424f4b3ec61Sdh155122 char *ip_type_str; 425f4b3ec61Sdh155122 426f4b3ec61Sdh155122 if (zent->ziptype == ZS_EXCLUSIVE) 427f4b3ec61Sdh155122 ip_type_str = "excl"; 428f4b3ec61Sdh155122 else 429f4b3ec61Sdh155122 ip_type_str = "shared"; 4307c478bd9Sstevel@tonic-gate 4317c478bd9Sstevel@tonic-gate assert(!(verbose && parsable)); 4327c478bd9Sstevel@tonic-gate if (firsttime && verbose) { 4337c478bd9Sstevel@tonic-gate firsttime = B_FALSE; 434f4b3ec61Sdh155122 (void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n", 435f4b3ec61Sdh155122 ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND", 436f4b3ec61Sdh155122 "IP"); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate if (!verbose) { 439555afedfScarlsonj char *cp, *clim; 440555afedfScarlsonj 4417c478bd9Sstevel@tonic-gate if (!parsable) { 4427c478bd9Sstevel@tonic-gate (void) printf("%s\n", zent->zname); 4437c478bd9Sstevel@tonic-gate return; 4447c478bd9Sstevel@tonic-gate } 4457c478bd9Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4467c478bd9Sstevel@tonic-gate (void) printf("-"); 4477c478bd9Sstevel@tonic-gate else 4487c478bd9Sstevel@tonic-gate (void) printf("%lu", zent->zid); 449555afedfScarlsonj (void) printf(":%s:%s:", zent->zname, zent->zstate_str); 450555afedfScarlsonj cp = zent->zroot; 451555afedfScarlsonj while ((clim = strchr(cp, ':')) != NULL) { 452555afedfScarlsonj (void) printf("%.*s\\:", clim - cp, cp); 453555afedfScarlsonj cp = clim + 1; 454555afedfScarlsonj } 455f4b3ec61Sdh155122 (void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand, 456f4b3ec61Sdh155122 ip_type_str); 4577c478bd9Sstevel@tonic-gate return; 4587c478bd9Sstevel@tonic-gate } 4597c478bd9Sstevel@tonic-gate if (zent->zstate_str != NULL) { 4607c478bd9Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4617c478bd9Sstevel@tonic-gate (void) printf("%*s", ZONEID_WIDTH, "-"); 4627c478bd9Sstevel@tonic-gate else 4637c478bd9Sstevel@tonic-gate (void) printf("%*lu", ZONEID_WIDTH, zent->zid); 464f4b3ec61Sdh155122 (void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname, 465f4b3ec61Sdh155122 zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str); 4667c478bd9Sstevel@tonic-gate } 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate static int 470108322fbScarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent) 4717c478bd9Sstevel@tonic-gate { 47245916cd2Sjpk char root[MAXPATHLEN], *cp; 4737c478bd9Sstevel@tonic-gate int err; 474555afedfScarlsonj uuid_t uuid; 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname)); 4777c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot)); 4789acbbeafSnn35248 (void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand)); 4797c478bd9Sstevel@tonic-gate zent->zstate_str = "???"; 4807c478bd9Sstevel@tonic-gate 481108322fbScarlsonj zent->zid = zid; 4827c478bd9Sstevel@tonic-gate 483555afedfScarlsonj if (zonecfg_get_uuid(zone_name, uuid) == Z_OK && 484555afedfScarlsonj !uuid_is_null(uuid)) 485555afedfScarlsonj uuid_unparse(uuid, zent->zuuid); 486555afedfScarlsonj else 487555afedfScarlsonj zent->zuuid[0] = '\0'; 488555afedfScarlsonj 48945916cd2Sjpk /* 49045916cd2Sjpk * For labeled zones which query the zone path of lower-level 49145916cd2Sjpk * zones, the path needs to be adjusted to drop the final 49245916cd2Sjpk * "/root" component. This adjusted path is then useful 49345916cd2Sjpk * for reading down any exported directories from the 49445916cd2Sjpk * lower-level zone. 49545916cd2Sjpk */ 49645916cd2Sjpk if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) { 49745916cd2Sjpk if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot, 49845916cd2Sjpk sizeof (zent->zroot)) == -1) { 49945916cd2Sjpk zperror2(zent->zname, 50045916cd2Sjpk gettext("could not get zone path.")); 50145916cd2Sjpk return (Z_ERR); 50245916cd2Sjpk } 50345916cd2Sjpk cp = zent->zroot + strlen(zent->zroot) - 5; 50445916cd2Sjpk if (cp > zent->zroot && strcmp(cp, "/root") == 0) 50545916cd2Sjpk *cp = 0; 50645916cd2Sjpk } else { 50745916cd2Sjpk if ((err = zone_get_zonepath(zent->zname, root, 50845916cd2Sjpk sizeof (root))) != Z_OK) { 5097c478bd9Sstevel@tonic-gate errno = err; 51045916cd2Sjpk zperror2(zent->zname, 51145916cd2Sjpk gettext("could not get zone path.")); 5127c478bd9Sstevel@tonic-gate return (Z_ERR); 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zroot, root, sizeof (zent->zroot)); 51545916cd2Sjpk } 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) { 5187c478bd9Sstevel@tonic-gate errno = err; 5197c478bd9Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get state")); 5207c478bd9Sstevel@tonic-gate return (Z_ERR); 5217c478bd9Sstevel@tonic-gate } 5227c478bd9Sstevel@tonic-gate zent->zstate_str = zone_state_str(zent->zstate_num); 523bafa7067Snn35248 524bafa7067Snn35248 /* 525bafa7067Snn35248 * A zone's brand is only available in the .xml file describing it, 526bafa7067Snn35248 * which is only visible to the global zone. This causes 527bafa7067Snn35248 * zone_get_brand() to fail when called from within a non-global 528bafa7067Snn35248 * zone. Fortunately we only do this on labeled systems, where we 529bafa7067Snn35248 * know all zones are native. 530bafa7067Snn35248 */ 531bafa7067Snn35248 if (getzoneid() != GLOBAL_ZONEID) { 532bafa7067Snn35248 assert(is_system_labeled() != 0); 533bafa7067Snn35248 (void) strlcpy(zent->zbrand, NATIVE_BRAND_NAME, 534bafa7067Snn35248 sizeof (zent->zbrand)); 535bafa7067Snn35248 } else if (zone_get_brand(zent->zname, zent->zbrand, 5369acbbeafSnn35248 sizeof (zent->zbrand)) != Z_OK) { 5379acbbeafSnn35248 zperror2(zent->zname, gettext("could not get brand name")); 5389acbbeafSnn35248 return (Z_ERR); 5399acbbeafSnn35248 } 5407c478bd9Sstevel@tonic-gate 541f4b3ec61Sdh155122 /* 542f4b3ec61Sdh155122 * Get ip type of the zone. 543f4b3ec61Sdh155122 * Note for global zone, ZS_SHARED is set always. 544f4b3ec61Sdh155122 */ 545f4b3ec61Sdh155122 if (zid == GLOBAL_ZONEID) { 546f4b3ec61Sdh155122 zent->ziptype = ZS_SHARED; 547f4b3ec61Sdh155122 } else { 548f4b3ec61Sdh155122 549f4b3ec61Sdh155122 if (zent->zstate_num == ZONE_STATE_RUNNING) { 550f4b3ec61Sdh155122 ushort_t flags; 551f4b3ec61Sdh155122 552f4b3ec61Sdh155122 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, 553f4b3ec61Sdh155122 sizeof (flags)) < 0) { 554f4b3ec61Sdh155122 zperror2(zent->zname, 555f4b3ec61Sdh155122 gettext("could not get zone flags")); 556f4b3ec61Sdh155122 return (Z_ERR); 557f4b3ec61Sdh155122 } 558f4b3ec61Sdh155122 if (flags & ZF_NET_EXCL) 559f4b3ec61Sdh155122 zent->ziptype = ZS_EXCLUSIVE; 560f4b3ec61Sdh155122 else 561f4b3ec61Sdh155122 zent->ziptype = ZS_SHARED; 562f4b3ec61Sdh155122 } else { 563f4b3ec61Sdh155122 zone_dochandle_t handle; 564f4b3ec61Sdh155122 565f4b3ec61Sdh155122 if ((handle = zonecfg_init_handle()) == NULL) { 566f4b3ec61Sdh155122 zperror2(zent->zname, 567f4b3ec61Sdh155122 gettext("could not init handle")); 568f4b3ec61Sdh155122 return (Z_ERR); 569f4b3ec61Sdh155122 } 570f4b3ec61Sdh155122 if ((err = zonecfg_get_handle(zent->zname, handle)) 571f4b3ec61Sdh155122 != Z_OK) { 572f4b3ec61Sdh155122 zperror2(zent->zname, 573f4b3ec61Sdh155122 gettext("could not get handle")); 574f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 575f4b3ec61Sdh155122 return (Z_ERR); 576f4b3ec61Sdh155122 } 577f4b3ec61Sdh155122 578f4b3ec61Sdh155122 if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) 579f4b3ec61Sdh155122 != Z_OK) { 580f4b3ec61Sdh155122 zperror2(zent->zname, 581f4b3ec61Sdh155122 gettext("could not get ip-type")); 582f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 583f4b3ec61Sdh155122 return (Z_ERR); 584f4b3ec61Sdh155122 } 585f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 586f4b3ec61Sdh155122 } 587f4b3ec61Sdh155122 } 588f4b3ec61Sdh155122 5897c478bd9Sstevel@tonic-gate return (Z_OK); 5907c478bd9Sstevel@tonic-gate } 5917c478bd9Sstevel@tonic-gate 5927c478bd9Sstevel@tonic-gate /* 5937c478bd9Sstevel@tonic-gate * fetch_zents() calls zone_list(2) to find out how many zones are running 5947c478bd9Sstevel@tonic-gate * (which is stored in the global nzents), then calls zone_list(2) again 5957c478bd9Sstevel@tonic-gate * to fetch the list of running zones (stored in the global zents). This 5967c478bd9Sstevel@tonic-gate * function may be called multiple times, so if zents is already set, we 5977c478bd9Sstevel@tonic-gate * return immediately to save work. 5987c478bd9Sstevel@tonic-gate */ 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate static int 601108322fbScarlsonj fetch_zents(void) 6027c478bd9Sstevel@tonic-gate { 6037c478bd9Sstevel@tonic-gate zoneid_t *zids = NULL; 6047c478bd9Sstevel@tonic-gate uint_t nzents_saved; 605108322fbScarlsonj int i, retv; 606108322fbScarlsonj FILE *fp; 607108322fbScarlsonj boolean_t inaltroot; 608108322fbScarlsonj zone_entry_t *zentp; 6097c478bd9Sstevel@tonic-gate 6107c478bd9Sstevel@tonic-gate if (nzents > 0) 6117c478bd9Sstevel@tonic-gate return (Z_OK); 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate if (zone_list(NULL, &nzents) != 0) { 6147c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zoneid list"), B_FALSE); 6157c478bd9Sstevel@tonic-gate return (Z_ERR); 6167c478bd9Sstevel@tonic-gate } 6177c478bd9Sstevel@tonic-gate 6187c478bd9Sstevel@tonic-gate again: 6197c478bd9Sstevel@tonic-gate if (nzents == 0) 6207c478bd9Sstevel@tonic-gate return (Z_OK); 6217c478bd9Sstevel@tonic-gate 6227c478bd9Sstevel@tonic-gate zids = safe_calloc(nzents, sizeof (zoneid_t)); 6237c478bd9Sstevel@tonic-gate nzents_saved = nzents; 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate if (zone_list(zids, &nzents) != 0) { 6267c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zone list"), B_FALSE); 6277c478bd9Sstevel@tonic-gate free(zids); 6287c478bd9Sstevel@tonic-gate return (Z_ERR); 6297c478bd9Sstevel@tonic-gate } 6307c478bd9Sstevel@tonic-gate if (nzents != nzents_saved) { 6317c478bd9Sstevel@tonic-gate /* list changed, try again */ 6327c478bd9Sstevel@tonic-gate free(zids); 6337c478bd9Sstevel@tonic-gate goto again; 6347c478bd9Sstevel@tonic-gate } 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate zents = safe_calloc(nzents, sizeof (zone_entry_t)); 6377c478bd9Sstevel@tonic-gate 638108322fbScarlsonj inaltroot = zonecfg_in_alt_root(); 639108322fbScarlsonj if (inaltroot) 640108322fbScarlsonj fp = zonecfg_open_scratch("", B_FALSE); 641108322fbScarlsonj else 642108322fbScarlsonj fp = NULL; 643108322fbScarlsonj zentp = zents; 644108322fbScarlsonj retv = Z_OK; 6457c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 6467c478bd9Sstevel@tonic-gate char name[ZONENAME_MAX]; 647108322fbScarlsonj char altname[ZONENAME_MAX]; 6487c478bd9Sstevel@tonic-gate 649108322fbScarlsonj if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) { 6507c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zone name"), B_FALSE); 651108322fbScarlsonj retv = Z_ERR; 652108322fbScarlsonj continue; 6537c478bd9Sstevel@tonic-gate } 654108322fbScarlsonj if (zonecfg_is_scratch(name)) { 655108322fbScarlsonj /* Ignore scratch zones by default */ 656108322fbScarlsonj if (!inaltroot) 657108322fbScarlsonj continue; 658108322fbScarlsonj if (fp == NULL || 659108322fbScarlsonj zonecfg_reverse_scratch(fp, name, altname, 660108322fbScarlsonj sizeof (altname), NULL, 0) == -1) { 6615ee84fbdSgjelinek zerror(gettext("could not resolve scratch " 662108322fbScarlsonj "zone %s"), name); 663108322fbScarlsonj retv = Z_ERR; 664108322fbScarlsonj continue; 665108322fbScarlsonj } 666108322fbScarlsonj (void) strcpy(name, altname); 667108322fbScarlsonj } else { 668108322fbScarlsonj /* Ignore non-scratch when in an alternate root */ 669108322fbScarlsonj if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0) 670108322fbScarlsonj continue; 671108322fbScarlsonj } 672108322fbScarlsonj if (lookup_zone_info(name, zids[i], zentp) != Z_OK) { 673108322fbScarlsonj zerror(gettext("failed to get zone data")); 674108322fbScarlsonj retv = Z_ERR; 675108322fbScarlsonj continue; 676108322fbScarlsonj } 677108322fbScarlsonj zentp++; 678108322fbScarlsonj } 679108322fbScarlsonj nzents = zentp - zents; 680108322fbScarlsonj if (fp != NULL) 681108322fbScarlsonj zonecfg_close_scratch(fp); 6827c478bd9Sstevel@tonic-gate 6837c478bd9Sstevel@tonic-gate free(zids); 684108322fbScarlsonj return (retv); 6857c478bd9Sstevel@tonic-gate } 6867c478bd9Sstevel@tonic-gate 687108322fbScarlsonj static int 6887c478bd9Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable) 6897c478bd9Sstevel@tonic-gate { 6907c478bd9Sstevel@tonic-gate int i; 6917c478bd9Sstevel@tonic-gate zone_entry_t zent; 6927c478bd9Sstevel@tonic-gate FILE *cookie; 6937c478bd9Sstevel@tonic-gate char *name; 6947c478bd9Sstevel@tonic-gate 6957c478bd9Sstevel@tonic-gate /* 6967c478bd9Sstevel@tonic-gate * First get the list of running zones from the kernel and print them. 6977c478bd9Sstevel@tonic-gate * If that is all we need, then return. 6987c478bd9Sstevel@tonic-gate */ 699108322fbScarlsonj if ((i = fetch_zents()) != Z_OK) { 7007c478bd9Sstevel@tonic-gate /* 7017c478bd9Sstevel@tonic-gate * No need for error messages; fetch_zents() has already taken 7027c478bd9Sstevel@tonic-gate * care of this. 7037c478bd9Sstevel@tonic-gate */ 704108322fbScarlsonj return (i); 7057c478bd9Sstevel@tonic-gate } 706108322fbScarlsonj for (i = 0; i < nzents; i++) 7077c478bd9Sstevel@tonic-gate zone_print(&zents[i], verbose, parsable); 7087c478bd9Sstevel@tonic-gate if (min_state >= ZONE_STATE_RUNNING) 709108322fbScarlsonj return (Z_OK); 7107c478bd9Sstevel@tonic-gate /* 7117c478bd9Sstevel@tonic-gate * Next, get the full list of zones from the configuration, skipping 7127c478bd9Sstevel@tonic-gate * any we have already printed. 7137c478bd9Sstevel@tonic-gate */ 7147c478bd9Sstevel@tonic-gate cookie = setzoneent(); 7157c478bd9Sstevel@tonic-gate while ((name = getzoneent(cookie)) != NULL) { 7167c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7177c478bd9Sstevel@tonic-gate if (strcmp(zents[i].zname, name) == 0) 7187c478bd9Sstevel@tonic-gate break; 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate if (i < nzents) { 7217c478bd9Sstevel@tonic-gate free(name); 7227c478bd9Sstevel@tonic-gate continue; 7237c478bd9Sstevel@tonic-gate } 724108322fbScarlsonj if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) { 7257c478bd9Sstevel@tonic-gate free(name); 7267c478bd9Sstevel@tonic-gate continue; 7277c478bd9Sstevel@tonic-gate } 7287c478bd9Sstevel@tonic-gate free(name); 7297c478bd9Sstevel@tonic-gate if (zent.zstate_num >= min_state) 7307c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 7317c478bd9Sstevel@tonic-gate } 7327c478bd9Sstevel@tonic-gate endzoneent(cookie); 733108322fbScarlsonj return (Z_OK); 7347c478bd9Sstevel@tonic-gate } 7357c478bd9Sstevel@tonic-gate 7367c478bd9Sstevel@tonic-gate static zone_entry_t * 7377c478bd9Sstevel@tonic-gate lookup_running_zone(char *str) 7387c478bd9Sstevel@tonic-gate { 7397c478bd9Sstevel@tonic-gate zoneid_t zoneid; 7407c478bd9Sstevel@tonic-gate char *cp; 7417c478bd9Sstevel@tonic-gate int i; 7427c478bd9Sstevel@tonic-gate 7437c478bd9Sstevel@tonic-gate if (fetch_zents() != Z_OK) 7447c478bd9Sstevel@tonic-gate return (NULL); 7457c478bd9Sstevel@tonic-gate 7467c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7477c478bd9Sstevel@tonic-gate if (strcmp(str, zents[i].zname) == 0) 7487c478bd9Sstevel@tonic-gate return (&zents[i]); 7497c478bd9Sstevel@tonic-gate } 7507c478bd9Sstevel@tonic-gate errno = 0; 7517c478bd9Sstevel@tonic-gate zoneid = strtol(str, &cp, 0); 7527c478bd9Sstevel@tonic-gate if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID || 7537c478bd9Sstevel@tonic-gate errno != 0 || *cp != '\0') 7547c478bd9Sstevel@tonic-gate return (NULL); 7557c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7567c478bd9Sstevel@tonic-gate if (zoneid == zents[i].zid) 7577c478bd9Sstevel@tonic-gate return (&zents[i]); 7587c478bd9Sstevel@tonic-gate } 7597c478bd9Sstevel@tonic-gate return (NULL); 7607c478bd9Sstevel@tonic-gate } 7617c478bd9Sstevel@tonic-gate 7627c478bd9Sstevel@tonic-gate /* 7637c478bd9Sstevel@tonic-gate * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if 7647c478bd9Sstevel@tonic-gate * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect). 7657c478bd9Sstevel@tonic-gate */ 7667c478bd9Sstevel@tonic-gate static boolean_t 7677c478bd9Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file) 7687c478bd9Sstevel@tonic-gate { 7697c478bd9Sstevel@tonic-gate char *str; 7707c478bd9Sstevel@tonic-gate 7717c478bd9Sstevel@tonic-gate assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR || 7727c478bd9Sstevel@tonic-gate bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP || 7737c478bd9Sstevel@tonic-gate bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH); 7747c478bd9Sstevel@tonic-gate /* 7757c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 7767c478bd9Sstevel@tonic-gate * The strings below will be used as part of a larger message, 7777c478bd9Sstevel@tonic-gate * either: 7787c478bd9Sstevel@tonic-gate * (file name) must be (owner|group|world) (read|writ|execut)able 7797c478bd9Sstevel@tonic-gate * or 7807c478bd9Sstevel@tonic-gate * (file name) must not be (owner|group|world) (read|writ|execut)able 7817c478bd9Sstevel@tonic-gate */ 7827c478bd9Sstevel@tonic-gate switch (bit) { 7837c478bd9Sstevel@tonic-gate case S_IRUSR: 7847c478bd9Sstevel@tonic-gate str = gettext("owner readable"); 7857c478bd9Sstevel@tonic-gate break; 7867c478bd9Sstevel@tonic-gate case S_IWUSR: 7877c478bd9Sstevel@tonic-gate str = gettext("owner writable"); 7887c478bd9Sstevel@tonic-gate break; 7897c478bd9Sstevel@tonic-gate case S_IXUSR: 7907c478bd9Sstevel@tonic-gate str = gettext("owner executable"); 7917c478bd9Sstevel@tonic-gate break; 7927c478bd9Sstevel@tonic-gate case S_IRGRP: 7937c478bd9Sstevel@tonic-gate str = gettext("group readable"); 7947c478bd9Sstevel@tonic-gate break; 7957c478bd9Sstevel@tonic-gate case S_IWGRP: 7967c478bd9Sstevel@tonic-gate str = gettext("group writable"); 7977c478bd9Sstevel@tonic-gate break; 7987c478bd9Sstevel@tonic-gate case S_IXGRP: 7997c478bd9Sstevel@tonic-gate str = gettext("group executable"); 8007c478bd9Sstevel@tonic-gate break; 8017c478bd9Sstevel@tonic-gate case S_IROTH: 8027c478bd9Sstevel@tonic-gate str = gettext("world readable"); 8037c478bd9Sstevel@tonic-gate break; 8047c478bd9Sstevel@tonic-gate case S_IWOTH: 8057c478bd9Sstevel@tonic-gate str = gettext("world writable"); 8067c478bd9Sstevel@tonic-gate break; 8077c478bd9Sstevel@tonic-gate case S_IXOTH: 8087c478bd9Sstevel@tonic-gate str = gettext("world executable"); 8097c478bd9Sstevel@tonic-gate break; 8107c478bd9Sstevel@tonic-gate } 8117c478bd9Sstevel@tonic-gate if ((mode & bit) == (on ? 0 : bit)) { 8127c478bd9Sstevel@tonic-gate /* 8137c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 8147c478bd9Sstevel@tonic-gate * The first parameter below is a file name; the second 8157c478bd9Sstevel@tonic-gate * is one of the "(owner|group|world) (read|writ|execut)able" 8167c478bd9Sstevel@tonic-gate * strings from above. 8177c478bd9Sstevel@tonic-gate */ 8187c478bd9Sstevel@tonic-gate /* 8197c478bd9Sstevel@tonic-gate * The code below could be simplified but not in a way 8207c478bd9Sstevel@tonic-gate * that would easily translate to non-English locales. 8217c478bd9Sstevel@tonic-gate */ 8227c478bd9Sstevel@tonic-gate if (on) { 8237c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must be %s.\n"), 8247c478bd9Sstevel@tonic-gate file, str); 8257c478bd9Sstevel@tonic-gate } else { 8267c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must not be %s.\n"), 8277c478bd9Sstevel@tonic-gate file, str); 8287c478bd9Sstevel@tonic-gate } 8297c478bd9Sstevel@tonic-gate return (B_TRUE); 8307c478bd9Sstevel@tonic-gate } 8317c478bd9Sstevel@tonic-gate return (B_FALSE); 8327c478bd9Sstevel@tonic-gate } 8337c478bd9Sstevel@tonic-gate 8347c478bd9Sstevel@tonic-gate /* 8357c478bd9Sstevel@tonic-gate * We want to make sure that no zone has its zone path as a child node 8367c478bd9Sstevel@tonic-gate * (in the directory sense) of any other. We do that by comparing this 8377c478bd9Sstevel@tonic-gate * zone's path to the path of all other (non-global) zones. The comparison 8387c478bd9Sstevel@tonic-gate * in each case is simple: add '/' to the end of the path, then do a 8397c478bd9Sstevel@tonic-gate * strncmp() of the two paths, using the length of the shorter one. 8407c478bd9Sstevel@tonic-gate */ 8417c478bd9Sstevel@tonic-gate 8427c478bd9Sstevel@tonic-gate static int 8437c478bd9Sstevel@tonic-gate crosscheck_zonepaths(char *path) 8447c478bd9Sstevel@tonic-gate { 8457c478bd9Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 8467c478bd9Sstevel@tonic-gate char path_copy[MAXPATHLEN]; /* copy of original path */ 8477c478bd9Sstevel@tonic-gate char rpath_copy[MAXPATHLEN]; /* copy of original rpath */ 8487c478bd9Sstevel@tonic-gate struct zoneent *ze; 8497c478bd9Sstevel@tonic-gate int res, err; 8507c478bd9Sstevel@tonic-gate FILE *cookie; 8517c478bd9Sstevel@tonic-gate 8527c478bd9Sstevel@tonic-gate cookie = setzoneent(); 8537c478bd9Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 8547c478bd9Sstevel@tonic-gate /* Skip zones which are not installed. */ 8557c478bd9Sstevel@tonic-gate if (ze->zone_state < ZONE_STATE_INSTALLED) { 8567c478bd9Sstevel@tonic-gate free(ze); 8577c478bd9Sstevel@tonic-gate continue; 8587c478bd9Sstevel@tonic-gate } 8597c478bd9Sstevel@tonic-gate /* Skip the global zone and the current target zone. */ 8607c478bd9Sstevel@tonic-gate if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 || 8617c478bd9Sstevel@tonic-gate strcmp(ze->zone_name, target_zone) == 0) { 8627c478bd9Sstevel@tonic-gate free(ze); 8637c478bd9Sstevel@tonic-gate continue; 8647c478bd9Sstevel@tonic-gate } 8657c478bd9Sstevel@tonic-gate if (strlen(ze->zone_path) == 0) { 8667c478bd9Sstevel@tonic-gate /* old index file without path, fall back */ 8677c478bd9Sstevel@tonic-gate if ((err = zone_get_zonepath(ze->zone_name, 8687c478bd9Sstevel@tonic-gate ze->zone_path, sizeof (ze->zone_path))) != Z_OK) { 8697c478bd9Sstevel@tonic-gate errno = err; 8707c478bd9Sstevel@tonic-gate zperror2(ze->zone_name, 8717c478bd9Sstevel@tonic-gate gettext("could not get zone path")); 8727c478bd9Sstevel@tonic-gate free(ze); 8737c478bd9Sstevel@tonic-gate continue; 8747c478bd9Sstevel@tonic-gate } 8757c478bd9Sstevel@tonic-gate } 876108322fbScarlsonj (void) snprintf(path_copy, sizeof (path_copy), "%s%s", 877108322fbScarlsonj zonecfg_get_root(), ze->zone_path); 878108322fbScarlsonj res = resolvepath(path_copy, rpath, sizeof (rpath)); 8797c478bd9Sstevel@tonic-gate if (res == -1) { 8807c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 881108322fbScarlsonj zperror(path_copy, B_FALSE); 8827c478bd9Sstevel@tonic-gate free(ze); 8837c478bd9Sstevel@tonic-gate return (Z_ERR); 8847c478bd9Sstevel@tonic-gate } 8857c478bd9Sstevel@tonic-gate (void) printf(gettext("WARNING: zone %s is installed, " 8867c478bd9Sstevel@tonic-gate "but its %s %s does not exist.\n"), ze->zone_name, 887108322fbScarlsonj "zonepath", path_copy); 8887c478bd9Sstevel@tonic-gate free(ze); 8897c478bd9Sstevel@tonic-gate continue; 8907c478bd9Sstevel@tonic-gate } 8917c478bd9Sstevel@tonic-gate rpath[res] = '\0'; 8927c478bd9Sstevel@tonic-gate (void) snprintf(path_copy, sizeof (path_copy), "%s/", path); 8937c478bd9Sstevel@tonic-gate (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath); 8947c478bd9Sstevel@tonic-gate if (strncmp(path_copy, rpath_copy, 8957c478bd9Sstevel@tonic-gate min(strlen(path_copy), strlen(rpath_copy))) == 0) { 8965ee84fbdSgjelinek /* 8975ee84fbdSgjelinek * TRANSLATION_NOTE 8985ee84fbdSgjelinek * zonepath is a literal that should not be translated. 8995ee84fbdSgjelinek */ 9007c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s zonepath (%s) and " 9017c478bd9Sstevel@tonic-gate "%s zonepath (%s) overlap.\n"), 9027c478bd9Sstevel@tonic-gate target_zone, path, ze->zone_name, rpath); 9037c478bd9Sstevel@tonic-gate free(ze); 9047c478bd9Sstevel@tonic-gate return (Z_ERR); 9057c478bd9Sstevel@tonic-gate } 9067c478bd9Sstevel@tonic-gate free(ze); 9077c478bd9Sstevel@tonic-gate } 9087c478bd9Sstevel@tonic-gate endzoneent(cookie); 9097c478bd9Sstevel@tonic-gate return (Z_OK); 9107c478bd9Sstevel@tonic-gate } 9117c478bd9Sstevel@tonic-gate 9127c478bd9Sstevel@tonic-gate static int 9137c478bd9Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num) 9147c478bd9Sstevel@tonic-gate { 9157c478bd9Sstevel@tonic-gate int res; /* result of last library/system call */ 9167c478bd9Sstevel@tonic-gate boolean_t err = B_FALSE; /* have we run into an error? */ 9177c478bd9Sstevel@tonic-gate struct stat stbuf; 9183f2f09c1Sdp struct statvfs64 vfsbuf; 9197c478bd9Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 9207c478bd9Sstevel@tonic-gate char ppath[MAXPATHLEN]; /* parent path */ 9217c478bd9Sstevel@tonic-gate char rppath[MAXPATHLEN]; /* resolved parent path */ 9227c478bd9Sstevel@tonic-gate char rootpath[MAXPATHLEN]; /* root path */ 9237c478bd9Sstevel@tonic-gate zone_state_t state; 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate if (path[0] != '/') { 9267c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9277c478bd9Sstevel@tonic-gate gettext("%s is not an absolute path.\n"), path); 9287c478bd9Sstevel@tonic-gate return (Z_ERR); 9297c478bd9Sstevel@tonic-gate } 9307c478bd9Sstevel@tonic-gate if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) { 9317c478bd9Sstevel@tonic-gate if ((errno != ENOENT) || 932865e09a4Sgjelinek (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL && 933865e09a4Sgjelinek cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) { 9347c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 9357c478bd9Sstevel@tonic-gate return (Z_ERR); 9367c478bd9Sstevel@tonic-gate } 9377c478bd9Sstevel@tonic-gate if (cmd_num == CMD_VERIFY) { 9385ee84fbdSgjelinek /* 9395ee84fbdSgjelinek * TRANSLATION_NOTE 9405ee84fbdSgjelinek * zoneadm is a literal that should not be translated. 9415ee84fbdSgjelinek */ 9427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("WARNING: %s does not " 9435ee84fbdSgjelinek "exist, so it could not be verified.\nWhen " 9445ee84fbdSgjelinek "'zoneadm %s' is run, '%s' will try to create\n%s, " 9455ee84fbdSgjelinek "and '%s' will be tried again,\nbut the '%s' may " 9465ee84fbdSgjelinek "fail if:\nthe parent directory of %s is group- or " 9475ee84fbdSgjelinek "other-writable\nor\n%s overlaps with any other " 9487c478bd9Sstevel@tonic-gate "installed zones.\n"), path, 9497c478bd9Sstevel@tonic-gate cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL), 9507c478bd9Sstevel@tonic-gate path, cmd_to_str(CMD_VERIFY), 9517c478bd9Sstevel@tonic-gate cmd_to_str(CMD_VERIFY), path, path); 9527c478bd9Sstevel@tonic-gate return (Z_OK); 9537c478bd9Sstevel@tonic-gate } 9547c478bd9Sstevel@tonic-gate /* 9557c478bd9Sstevel@tonic-gate * The zonepath is supposed to be mode 700 but its 9567c478bd9Sstevel@tonic-gate * parent(s) 755. So use 755 on the mkdirp() then 9577c478bd9Sstevel@tonic-gate * chmod() the zonepath itself to 700. 9587c478bd9Sstevel@tonic-gate */ 9597c478bd9Sstevel@tonic-gate if (mkdirp(path, DEFAULT_DIR_MODE) < 0) { 9607c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 9617c478bd9Sstevel@tonic-gate return (Z_ERR); 9627c478bd9Sstevel@tonic-gate } 9637c478bd9Sstevel@tonic-gate /* 9647c478bd9Sstevel@tonic-gate * If the chmod() fails, report the error, but might 9657c478bd9Sstevel@tonic-gate * as well continue the verify procedure. 9667c478bd9Sstevel@tonic-gate */ 9677c478bd9Sstevel@tonic-gate if (chmod(path, S_IRWXU) != 0) 9687c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 9697c478bd9Sstevel@tonic-gate /* 9707c478bd9Sstevel@tonic-gate * Since the mkdir() succeeded, we should not have to 9717c478bd9Sstevel@tonic-gate * worry about a subsequent ENOENT, thus this should 9727c478bd9Sstevel@tonic-gate * only recurse once. 9737c478bd9Sstevel@tonic-gate */ 974865e09a4Sgjelinek return (validate_zonepath(path, cmd_num)); 9757c478bd9Sstevel@tonic-gate } 9767c478bd9Sstevel@tonic-gate rpath[res] = '\0'; 9777c478bd9Sstevel@tonic-gate if (strcmp(path, rpath) != 0) { 9787c478bd9Sstevel@tonic-gate errno = Z_RESOLVED_PATH; 9797c478bd9Sstevel@tonic-gate zperror(path, B_TRUE); 9807c478bd9Sstevel@tonic-gate return (Z_ERR); 9817c478bd9Sstevel@tonic-gate } 9827c478bd9Sstevel@tonic-gate if ((res = stat(rpath, &stbuf)) != 0) { 9837c478bd9Sstevel@tonic-gate zperror(rpath, B_FALSE); 9847c478bd9Sstevel@tonic-gate return (Z_ERR); 9857c478bd9Sstevel@tonic-gate } 9867c478bd9Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 9877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 9887c478bd9Sstevel@tonic-gate rpath); 9897c478bd9Sstevel@tonic-gate return (Z_ERR); 9907c478bd9Sstevel@tonic-gate } 9914fceebdfSblakej if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) { 9927c478bd9Sstevel@tonic-gate (void) printf(gettext("WARNING: %s is on a temporary " 9930b5de56dSgjelinek "file system.\n"), rpath); 9947c478bd9Sstevel@tonic-gate } 9957c478bd9Sstevel@tonic-gate if (crosscheck_zonepaths(rpath) != Z_OK) 9967c478bd9Sstevel@tonic-gate return (Z_ERR); 9977c478bd9Sstevel@tonic-gate /* 9987c478bd9Sstevel@tonic-gate * Try to collect and report as many minor errors as possible 9997c478bd9Sstevel@tonic-gate * before returning, so the user can learn everything that needs 10007c478bd9Sstevel@tonic-gate * to be fixed up front. 10017c478bd9Sstevel@tonic-gate */ 10027c478bd9Sstevel@tonic-gate if (stbuf.st_uid != 0) { 10037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 10047c478bd9Sstevel@tonic-gate rpath); 10057c478bd9Sstevel@tonic-gate err = B_TRUE; 10067c478bd9Sstevel@tonic-gate } 10077c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath); 10087c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath); 10097c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath); 10107c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath); 10117c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath); 10127c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath); 10137c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath); 10147c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath); 10157c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath); 10167c478bd9Sstevel@tonic-gate 10177c478bd9Sstevel@tonic-gate (void) snprintf(ppath, sizeof (ppath), "%s/..", path); 10187c478bd9Sstevel@tonic-gate if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) { 10197c478bd9Sstevel@tonic-gate zperror(ppath, B_FALSE); 10207c478bd9Sstevel@tonic-gate return (Z_ERR); 10217c478bd9Sstevel@tonic-gate } 10227c478bd9Sstevel@tonic-gate rppath[res] = '\0'; 10237c478bd9Sstevel@tonic-gate if ((res = stat(rppath, &stbuf)) != 0) { 10247c478bd9Sstevel@tonic-gate zperror(rppath, B_FALSE); 10257c478bd9Sstevel@tonic-gate return (Z_ERR); 10267c478bd9Sstevel@tonic-gate } 10277c478bd9Sstevel@tonic-gate /* theoretically impossible */ 10287c478bd9Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 10297c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 10307c478bd9Sstevel@tonic-gate rppath); 10317c478bd9Sstevel@tonic-gate return (Z_ERR); 10327c478bd9Sstevel@tonic-gate } 10337c478bd9Sstevel@tonic-gate if (stbuf.st_uid != 0) { 10347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 10357c478bd9Sstevel@tonic-gate rppath); 10367c478bd9Sstevel@tonic-gate err = B_TRUE; 10377c478bd9Sstevel@tonic-gate } 10387c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath); 10397c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath); 10407c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath); 10417c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath); 10427c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath); 10437c478bd9Sstevel@tonic-gate if (strcmp(rpath, rppath) == 0) { 10447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is its own parent.\n"), 10457c478bd9Sstevel@tonic-gate rppath); 10467c478bd9Sstevel@tonic-gate err = B_TRUE; 10477c478bd9Sstevel@tonic-gate } 10487c478bd9Sstevel@tonic-gate 10493f2f09c1Sdp if (statvfs64(rpath, &vfsbuf) != 0) { 10507c478bd9Sstevel@tonic-gate zperror(rpath, B_FALSE); 10517c478bd9Sstevel@tonic-gate return (Z_ERR); 10527c478bd9Sstevel@tonic-gate } 1053b5abaf04Sgjelinek if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) { 10545ee84fbdSgjelinek /* 10555ee84fbdSgjelinek * TRANSLATION_NOTE 10565ee84fbdSgjelinek * Zonepath and NFS are literals that should not be translated. 10575ee84fbdSgjelinek */ 10585ee84fbdSgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on an NFS " 10590b5de56dSgjelinek "mounted file system.\n" 10600b5de56dSgjelinek "\tA local file system must be used.\n"), rpath); 1061b5abaf04Sgjelinek return (Z_ERR); 1062b5abaf04Sgjelinek } 1063b5abaf04Sgjelinek if (vfsbuf.f_flag & ST_NOSUID) { 10645ee84fbdSgjelinek /* 10655ee84fbdSgjelinek * TRANSLATION_NOTE 10665ee84fbdSgjelinek * Zonepath and nosuid are literals that should not be 10675ee84fbdSgjelinek * translated. 10685ee84fbdSgjelinek */ 1069b5abaf04Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on a nosuid " 10700b5de56dSgjelinek "file system.\n"), rpath); 10717c478bd9Sstevel@tonic-gate return (Z_ERR); 10727c478bd9Sstevel@tonic-gate } 10737c478bd9Sstevel@tonic-gate 10747c478bd9Sstevel@tonic-gate if ((res = zone_get_state(target_zone, &state)) != Z_OK) { 10757c478bd9Sstevel@tonic-gate errno = res; 10767c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get state")); 10777c478bd9Sstevel@tonic-gate return (Z_ERR); 10787c478bd9Sstevel@tonic-gate } 10797c478bd9Sstevel@tonic-gate /* 10807c478bd9Sstevel@tonic-gate * The existence of the root path is only bad in the configured state, 10817c478bd9Sstevel@tonic-gate * as it is *supposed* to be there at the installed and later states. 1082ee519a1fSgjelinek * However, the root path is expected to be there if the zone is 1083ee519a1fSgjelinek * detached. 10847c478bd9Sstevel@tonic-gate * State/command mismatches are caught earlier in verify_details(). 10857c478bd9Sstevel@tonic-gate */ 1086ee519a1fSgjelinek if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) { 10877c478bd9Sstevel@tonic-gate if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >= 10887c478bd9Sstevel@tonic-gate sizeof (rootpath)) { 10895ee84fbdSgjelinek /* 10905ee84fbdSgjelinek * TRANSLATION_NOTE 10915ee84fbdSgjelinek * Zonepath is a literal that should not be translated. 10925ee84fbdSgjelinek */ 10937c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 10947c478bd9Sstevel@tonic-gate gettext("Zonepath %s is too long.\n"), rpath); 10957c478bd9Sstevel@tonic-gate return (Z_ERR); 10967c478bd9Sstevel@tonic-gate } 10977c478bd9Sstevel@tonic-gate if ((res = stat(rootpath, &stbuf)) == 0) { 1098c4a45622S struct dirent *dp; 1099c4a45622S DIR *dirp; 1100c4a45622S boolean_t empty = B_TRUE; 1101c4a45622S 1102c4a45622S if (zonecfg_detached(rpath)) { 1103ee519a1fSgjelinek (void) fprintf(stderr, 1104ee519a1fSgjelinek gettext("Cannot %s detached " 1105ee519a1fSgjelinek "zone.\nUse attach or remove %s " 1106ee519a1fSgjelinek "directory.\n"), cmd_to_str(cmd_num), 1107ee519a1fSgjelinek rpath); 11087c478bd9Sstevel@tonic-gate return (Z_ERR); 11097c478bd9Sstevel@tonic-gate } 1110c4a45622S 1111c4a45622S /* Not detached, check if it really looks ok. */ 1112c4a45622S 1113c4a45622S if (!S_ISDIR(stbuf.st_mode)) { 1114c4a45622S (void) fprintf(stderr, gettext("%s is not a " 1115c4a45622S "directory.\n"), rootpath); 1116c4a45622S return (Z_ERR); 1117c4a45622S } 1118c4a45622S 1119c4a45622S if (stbuf.st_uid != 0) { 1120c4a45622S (void) fprintf(stderr, gettext("%s is not " 1121c4a45622S "owned by root.\n"), rootpath); 1122c4a45622S return (Z_ERR); 1123c4a45622S } 1124c4a45622S 1125c4a45622S if ((stbuf.st_mode & 0777) != 0755) { 1126c4a45622S (void) fprintf(stderr, gettext("%s mode is not " 1127c4a45622S "0755.\n"), rootpath); 1128c4a45622S return (Z_ERR); 1129c4a45622S } 1130c4a45622S 1131c4a45622S if ((dirp = opendir(rootpath)) == NULL) { 1132c4a45622S (void) fprintf(stderr, gettext("Could not " 1133c4a45622S "open rootpath %s\n"), rootpath); 1134c4a45622S return (Z_ERR); 1135c4a45622S } 1136c4a45622S 1137c4a45622S /* Verify that the dir is empty. */ 1138c4a45622S while ((dp = readdir(dirp)) != NULL) { 1139c4a45622S if (strcmp(dp->d_name, ".") == 0 || 1140c4a45622S strcmp(dp->d_name, "..") == 0) 1141c4a45622S continue; 1142c4a45622S 1143c4a45622S empty = B_FALSE; 1144c4a45622S break; 1145c4a45622S } 1146c4a45622S (void) closedir(dirp); 1147c4a45622S 1148c4a45622S if (!empty) { 1149c4a45622S (void) fprintf(stderr, gettext("Rootpath %s " 1150c4a45622S "exists and contains data; remove or move " 1151c4a45622S "aside prior to %s.\n"), rootpath, 1152c4a45622S cmd_to_str(cmd_num)); 1153c4a45622S return (Z_ERR); 1154c4a45622S } 1155c4a45622S 1156c4a45622S } 11577c478bd9Sstevel@tonic-gate } 11587c478bd9Sstevel@tonic-gate 11597c478bd9Sstevel@tonic-gate return (err ? Z_ERR : Z_OK); 11607c478bd9Sstevel@tonic-gate } 11617c478bd9Sstevel@tonic-gate 11627c478bd9Sstevel@tonic-gate static int 1163ce28b40eSzt129084 invoke_brand_handler(int cmd_num, char *argv[]) 1164ce28b40eSzt129084 { 1165ce28b40eSzt129084 zone_dochandle_t handle; 1166ce28b40eSzt129084 int err; 1167ce28b40eSzt129084 1168ce28b40eSzt129084 if ((handle = zonecfg_init_handle()) == NULL) { 1169ce28b40eSzt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 1170ce28b40eSzt129084 return (Z_ERR); 1171ce28b40eSzt129084 } 1172ce28b40eSzt129084 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 1173ce28b40eSzt129084 errno = err; 1174ce28b40eSzt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 1175ce28b40eSzt129084 zonecfg_fini_handle(handle); 1176ce28b40eSzt129084 return (Z_ERR); 1177ce28b40eSzt129084 } 1178ce28b40eSzt129084 if (verify_brand(handle, cmd_num, argv) != Z_OK) { 1179ce28b40eSzt129084 zonecfg_fini_handle(handle); 1180ce28b40eSzt129084 return (Z_ERR); 1181ce28b40eSzt129084 } 1182ce28b40eSzt129084 zonecfg_fini_handle(handle); 1183ce28b40eSzt129084 return (Z_OK); 1184ce28b40eSzt129084 } 1185ce28b40eSzt129084 1186ce28b40eSzt129084 static int 11877c478bd9Sstevel@tonic-gate ready_func(int argc, char *argv[]) 11887c478bd9Sstevel@tonic-gate { 11897c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 11907c478bd9Sstevel@tonic-gate int arg; 11917c478bd9Sstevel@tonic-gate 1192108322fbScarlsonj if (zonecfg_in_alt_root()) { 1193108322fbScarlsonj zerror(gettext("cannot ready zone in alternate root")); 1194108322fbScarlsonj return (Z_ERR); 1195108322fbScarlsonj } 1196108322fbScarlsonj 11977c478bd9Sstevel@tonic-gate optind = 0; 11987c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 11997c478bd9Sstevel@tonic-gate switch (arg) { 12007c478bd9Sstevel@tonic-gate case '?': 12017c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 12027c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 12037c478bd9Sstevel@tonic-gate default: 12047c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 12057c478bd9Sstevel@tonic-gate return (Z_USAGE); 12067c478bd9Sstevel@tonic-gate } 12077c478bd9Sstevel@tonic-gate } 12087c478bd9Sstevel@tonic-gate if (argc > optind) { 12097c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 12107c478bd9Sstevel@tonic-gate return (Z_USAGE); 12117c478bd9Sstevel@tonic-gate } 12129acbbeafSnn35248 if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE) 12139acbbeafSnn35248 != Z_OK) 12147c478bd9Sstevel@tonic-gate return (Z_ERR); 1215ce28b40eSzt129084 if (verify_details(CMD_READY, argv) != Z_OK) 12167c478bd9Sstevel@tonic-gate return (Z_ERR); 12177c478bd9Sstevel@tonic-gate 12187c478bd9Sstevel@tonic-gate zarg.cmd = Z_READY; 1219ff17c8bfSgjelinek if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) { 12207c478bd9Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 12217c478bd9Sstevel@tonic-gate return (Z_ERR); 12227c478bd9Sstevel@tonic-gate } 12237c478bd9Sstevel@tonic-gate return (Z_OK); 12247c478bd9Sstevel@tonic-gate } 12257c478bd9Sstevel@tonic-gate 12267c478bd9Sstevel@tonic-gate static int 12277c478bd9Sstevel@tonic-gate boot_func(int argc, char *argv[]) 12287c478bd9Sstevel@tonic-gate { 12297c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 12309acbbeafSnn35248 boolean_t force = B_FALSE; 12317c478bd9Sstevel@tonic-gate int arg; 12327c478bd9Sstevel@tonic-gate 1233108322fbScarlsonj if (zonecfg_in_alt_root()) { 1234108322fbScarlsonj zerror(gettext("cannot boot zone in alternate root")); 1235108322fbScarlsonj return (Z_ERR); 1236108322fbScarlsonj } 1237108322fbScarlsonj 12387c478bd9Sstevel@tonic-gate zarg.bootbuf[0] = '\0'; 12397c478bd9Sstevel@tonic-gate 12407c478bd9Sstevel@tonic-gate /* 12413f2f09c1Sdp * The following getopt processes arguments to zone boot; that 12423f2f09c1Sdp * is to say, the [here] portion of the argument string: 12433f2f09c1Sdp * 12443f2f09c1Sdp * zoneadm -z myzone boot [here] -- -v -m verbose 12453f2f09c1Sdp * 12463f2f09c1Sdp * Where [here] can either be nothing, -? (in which case we bail 12479acbbeafSnn35248 * and print usage), -f (a private option to indicate that the 12489acbbeafSnn35248 * boot operation should be 'forced'), or -s. Support for -s is 12499acbbeafSnn35248 * vestigal and obsolete, but is retained because it was a 12509acbbeafSnn35248 * documented interface and there are known consumers including 12519acbbeafSnn35248 * admin/install; the proper way to specify boot arguments like -s 12529acbbeafSnn35248 * is: 12533f2f09c1Sdp * 12543f2f09c1Sdp * zoneadm -z myzone boot -- -s -v -m verbose. 12557c478bd9Sstevel@tonic-gate */ 12567c478bd9Sstevel@tonic-gate optind = 0; 12579acbbeafSnn35248 while ((arg = getopt(argc, argv, "?fs")) != EOF) { 12587c478bd9Sstevel@tonic-gate switch (arg) { 12597c478bd9Sstevel@tonic-gate case '?': 12607c478bd9Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 12617c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 12627c478bd9Sstevel@tonic-gate case 's': 12637c478bd9Sstevel@tonic-gate (void) strlcpy(zarg.bootbuf, "-s", 12647c478bd9Sstevel@tonic-gate sizeof (zarg.bootbuf)); 12657c478bd9Sstevel@tonic-gate break; 12669acbbeafSnn35248 case 'f': 12679acbbeafSnn35248 force = B_TRUE; 12689acbbeafSnn35248 break; 12697c478bd9Sstevel@tonic-gate default: 12707c478bd9Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 12717c478bd9Sstevel@tonic-gate return (Z_USAGE); 12727c478bd9Sstevel@tonic-gate } 12737c478bd9Sstevel@tonic-gate } 12743f2f09c1Sdp 12753f2f09c1Sdp for (; optind < argc; optind++) { 12763f2f09c1Sdp if (strlcat(zarg.bootbuf, argv[optind], 12773f2f09c1Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 12783f2f09c1Sdp zerror(gettext("Boot argument list too long")); 12793f2f09c1Sdp return (Z_ERR); 12807c478bd9Sstevel@tonic-gate } 12813f2f09c1Sdp if (optind < argc - 1) 12823f2f09c1Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 12833f2f09c1Sdp sizeof (zarg.bootbuf)) { 12843f2f09c1Sdp zerror(gettext("Boot argument list too long")); 12853f2f09c1Sdp return (Z_ERR); 12863f2f09c1Sdp } 12873f2f09c1Sdp } 12889acbbeafSnn35248 if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force) 12899acbbeafSnn35248 != Z_OK) 12907c478bd9Sstevel@tonic-gate return (Z_ERR); 1291ce28b40eSzt129084 if (verify_details(CMD_BOOT, argv) != Z_OK) 12927c478bd9Sstevel@tonic-gate return (Z_ERR); 12939acbbeafSnn35248 zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT; 1294ff17c8bfSgjelinek if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) { 12957c478bd9Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 12967c478bd9Sstevel@tonic-gate return (Z_ERR); 12977c478bd9Sstevel@tonic-gate } 12980209230bSgjelinek 12997c478bd9Sstevel@tonic-gate return (Z_OK); 13007c478bd9Sstevel@tonic-gate } 13017c478bd9Sstevel@tonic-gate 13027c478bd9Sstevel@tonic-gate static void 13037c478bd9Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr) 13047c478bd9Sstevel@tonic-gate { 13057c478bd9Sstevel@tonic-gate ssize_t result; 1306555afedfScarlsonj uuid_t uuid; 1307555afedfScarlsonj FILE *fp; 1308f4b3ec61Sdh155122 ushort_t flags; 1309555afedfScarlsonj 1310555afedfScarlsonj (void) memset(zeptr, 0, sizeof (*zeptr)); 13117c478bd9Sstevel@tonic-gate 13127c478bd9Sstevel@tonic-gate zeptr->zid = zid; 1313555afedfScarlsonj 13147c478bd9Sstevel@tonic-gate /* 13157c478bd9Sstevel@tonic-gate * Since we're looking up our own (non-global) zone name, 13167c478bd9Sstevel@tonic-gate * we can be assured that it will succeed. 13177c478bd9Sstevel@tonic-gate */ 13187c478bd9Sstevel@tonic-gate result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname)); 13197c478bd9Sstevel@tonic-gate assert(result >= 0); 1320555afedfScarlsonj if (zonecfg_is_scratch(zeptr->zname) && 1321555afedfScarlsonj (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) { 1322555afedfScarlsonj (void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname, 1323555afedfScarlsonj sizeof (zeptr->zname), NULL, 0); 1324555afedfScarlsonj zonecfg_close_scratch(fp); 1325555afedfScarlsonj } 1326555afedfScarlsonj 1327555afedfScarlsonj if (is_system_labeled()) { 132845916cd2Sjpk (void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot, 132945916cd2Sjpk sizeof (zeptr->zroot)); 13309acbbeafSnn35248 (void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME, 13319acbbeafSnn35248 sizeof (zeptr->zbrand)); 1332555afedfScarlsonj } else { 1333555afedfScarlsonj (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot)); 13349acbbeafSnn35248 (void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand, 13359acbbeafSnn35248 sizeof (zeptr->zbrand)); 133645916cd2Sjpk } 1337555afedfScarlsonj 13387c478bd9Sstevel@tonic-gate zeptr->zstate_str = "running"; 1339555afedfScarlsonj if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK && 1340555afedfScarlsonj !uuid_is_null(uuid)) 1341555afedfScarlsonj uuid_unparse(uuid, zeptr->zuuid); 1342f4b3ec61Sdh155122 1343f4b3ec61Sdh155122 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) { 1344f4b3ec61Sdh155122 zperror2(zeptr->zname, gettext("could not get zone flags")); 1345f4b3ec61Sdh155122 exit(Z_ERR); 1346f4b3ec61Sdh155122 } 1347f4b3ec61Sdh155122 if (flags & ZF_NET_EXCL) 1348f4b3ec61Sdh155122 zeptr->ziptype = ZS_EXCLUSIVE; 1349f4b3ec61Sdh155122 else 1350f4b3ec61Sdh155122 zeptr->ziptype = ZS_SHARED; 13517c478bd9Sstevel@tonic-gate } 13527c478bd9Sstevel@tonic-gate 13537c478bd9Sstevel@tonic-gate static int 13547c478bd9Sstevel@tonic-gate list_func(int argc, char *argv[]) 13557c478bd9Sstevel@tonic-gate { 13567c478bd9Sstevel@tonic-gate zone_entry_t *zentp, zent; 1357108322fbScarlsonj int arg, retv; 13587c478bd9Sstevel@tonic-gate boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE; 13597c478bd9Sstevel@tonic-gate zone_state_t min_state = ZONE_STATE_RUNNING; 13607c478bd9Sstevel@tonic-gate zoneid_t zone_id = getzoneid(); 13617c478bd9Sstevel@tonic-gate 13627c478bd9Sstevel@tonic-gate if (target_zone == NULL) { 13637c478bd9Sstevel@tonic-gate /* all zones: default view to running but allow override */ 13647c478bd9Sstevel@tonic-gate optind = 0; 13657c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?cipv")) != EOF) { 13667c478bd9Sstevel@tonic-gate switch (arg) { 13677c478bd9Sstevel@tonic-gate case '?': 13687c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 13697c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 1370475d78a4Sjonb /* 1371475d78a4Sjonb * The 'i' and 'c' options are not mutually 1372475d78a4Sjonb * exclusive so if 'c' is given, then min_state 1373475d78a4Sjonb * is set to 0 (ZONE_STATE_CONFIGURED) which is 1374475d78a4Sjonb * the lowest possible state. If 'i' is given, 1375475d78a4Sjonb * then min_state is set to be the lowest state 1376475d78a4Sjonb * so far. 1377475d78a4Sjonb */ 13787c478bd9Sstevel@tonic-gate case 'c': 13797c478bd9Sstevel@tonic-gate min_state = ZONE_STATE_CONFIGURED; 13807c478bd9Sstevel@tonic-gate break; 13817c478bd9Sstevel@tonic-gate case 'i': 1382475d78a4Sjonb min_state = min(ZONE_STATE_INSTALLED, 1383475d78a4Sjonb min_state); 1384475d78a4Sjonb 13857c478bd9Sstevel@tonic-gate break; 13867c478bd9Sstevel@tonic-gate case 'p': 13877c478bd9Sstevel@tonic-gate parsable = B_TRUE; 13887c478bd9Sstevel@tonic-gate break; 13897c478bd9Sstevel@tonic-gate case 'v': 13907c478bd9Sstevel@tonic-gate verbose = B_TRUE; 13917c478bd9Sstevel@tonic-gate break; 13927c478bd9Sstevel@tonic-gate default: 13937c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 13947c478bd9Sstevel@tonic-gate return (Z_USAGE); 13957c478bd9Sstevel@tonic-gate } 13967c478bd9Sstevel@tonic-gate } 13977c478bd9Sstevel@tonic-gate if (parsable && verbose) { 13987c478bd9Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 13997c478bd9Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 14007c478bd9Sstevel@tonic-gate return (Z_ERR); 14017c478bd9Sstevel@tonic-gate } 140245916cd2Sjpk if (zone_id == GLOBAL_ZONEID || is_system_labeled()) { 1403108322fbScarlsonj retv = zone_print_list(min_state, verbose, parsable); 14047c478bd9Sstevel@tonic-gate } else { 14057c478bd9Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 14069acbbeafSnn35248 retv = Z_OK; 14077c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 14087c478bd9Sstevel@tonic-gate } 1409108322fbScarlsonj return (retv); 14107c478bd9Sstevel@tonic-gate } 14117c478bd9Sstevel@tonic-gate 14127c478bd9Sstevel@tonic-gate /* 14137c478bd9Sstevel@tonic-gate * Specific target zone: disallow -i/-c suboptions. 14147c478bd9Sstevel@tonic-gate */ 14157c478bd9Sstevel@tonic-gate optind = 0; 14167c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?pv")) != EOF) { 14177c478bd9Sstevel@tonic-gate switch (arg) { 14187c478bd9Sstevel@tonic-gate case '?': 14197c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14207c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 14217c478bd9Sstevel@tonic-gate case 'p': 14227c478bd9Sstevel@tonic-gate parsable = B_TRUE; 14237c478bd9Sstevel@tonic-gate break; 14247c478bd9Sstevel@tonic-gate case 'v': 14257c478bd9Sstevel@tonic-gate verbose = B_TRUE; 14267c478bd9Sstevel@tonic-gate break; 14277c478bd9Sstevel@tonic-gate default: 14287c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14297c478bd9Sstevel@tonic-gate return (Z_USAGE); 14307c478bd9Sstevel@tonic-gate } 14317c478bd9Sstevel@tonic-gate } 14327c478bd9Sstevel@tonic-gate if (parsable && verbose) { 14337c478bd9Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 14347c478bd9Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 14357c478bd9Sstevel@tonic-gate return (Z_ERR); 14367c478bd9Sstevel@tonic-gate } 14377c478bd9Sstevel@tonic-gate if (argc > optind) { 14387c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14397c478bd9Sstevel@tonic-gate return (Z_USAGE); 14407c478bd9Sstevel@tonic-gate } 14412ec67e04Sgjelinek if (zone_id != GLOBAL_ZONEID && !is_system_labeled()) { 14427c478bd9Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 14437c478bd9Sstevel@tonic-gate /* 14447c478bd9Sstevel@tonic-gate * main() will issue a Z_NO_ZONE error if it cannot get an 14457c478bd9Sstevel@tonic-gate * id for target_zone, which in a non-global zone should 14467c478bd9Sstevel@tonic-gate * happen for any zone name except `zonename`. Thus we 14477c478bd9Sstevel@tonic-gate * assert() that here but don't otherwise check. 14487c478bd9Sstevel@tonic-gate */ 14497c478bd9Sstevel@tonic-gate assert(strcmp(zent.zname, target_zone) == 0); 14507c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 14517c478bd9Sstevel@tonic-gate output = B_TRUE; 14527c478bd9Sstevel@tonic-gate } else if ((zentp = lookup_running_zone(target_zone)) != NULL) { 14537c478bd9Sstevel@tonic-gate zone_print(zentp, verbose, parsable); 14547c478bd9Sstevel@tonic-gate output = B_TRUE; 1455108322fbScarlsonj } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED, 1456108322fbScarlsonj &zent) == Z_OK) { 14577c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 14587c478bd9Sstevel@tonic-gate output = B_TRUE; 14597c478bd9Sstevel@tonic-gate } 1460ce28b40eSzt129084 1461ce28b40eSzt129084 /* 1462ce28b40eSzt129084 * Invoke brand-specific handler. Note that we do this 1463db628066Sgjelinek * only if we're in the global zone, and target_zone is specified 1464db628066Sgjelinek * and it is not the global zone. 1465ce28b40eSzt129084 */ 1466db628066Sgjelinek if (zone_id == GLOBAL_ZONEID && target_zone != NULL && 1467db628066Sgjelinek strcmp(target_zone, GLOBAL_ZONENAME) != 0) 1468ce28b40eSzt129084 if (invoke_brand_handler(CMD_LIST, argv) != Z_OK) 1469ce28b40eSzt129084 return (Z_ERR); 1470ce28b40eSzt129084 14717c478bd9Sstevel@tonic-gate return (output ? Z_OK : Z_ERR); 14727c478bd9Sstevel@tonic-gate } 14737c478bd9Sstevel@tonic-gate 14747c478bd9Sstevel@tonic-gate static void 14757c478bd9Sstevel@tonic-gate sigterm(int sig) 14767c478bd9Sstevel@tonic-gate { 14777c478bd9Sstevel@tonic-gate /* 14787c478bd9Sstevel@tonic-gate * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop, 14797c478bd9Sstevel@tonic-gate * then propagate the signal to our process group. 14807c478bd9Sstevel@tonic-gate */ 14819acbbeafSnn35248 assert(sig == SIGINT || sig == SIGTERM); 14827c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 14837c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_IGN); 14847c478bd9Sstevel@tonic-gate (void) kill(0, sig); 14857c478bd9Sstevel@tonic-gate child_killed = B_TRUE; 14867c478bd9Sstevel@tonic-gate } 14877c478bd9Sstevel@tonic-gate 14887c478bd9Sstevel@tonic-gate static int 14897c478bd9Sstevel@tonic-gate do_subproc(char *cmdbuf) 14907c478bd9Sstevel@tonic-gate { 14917c478bd9Sstevel@tonic-gate char inbuf[1024]; /* arbitrary large amount */ 14927c478bd9Sstevel@tonic-gate FILE *file; 14937c478bd9Sstevel@tonic-gate 14949acbbeafSnn35248 do_subproc_cnt++; 14957c478bd9Sstevel@tonic-gate child_killed = B_FALSE; 14967c478bd9Sstevel@tonic-gate /* 14977c478bd9Sstevel@tonic-gate * We use popen(3c) to launch child processes for [un]install; 14987c478bd9Sstevel@tonic-gate * this library call does not return a PID, so we have to kill 14997c478bd9Sstevel@tonic-gate * the whole process group. To avoid killing our parent, we 15007c478bd9Sstevel@tonic-gate * become a process group leader here. But doing so can wreak 15017c478bd9Sstevel@tonic-gate * havoc with reading from stdin when launched by a non-job-control 15027c478bd9Sstevel@tonic-gate * shell, so we close stdin and reopen it as /dev/null first. 15037c478bd9Sstevel@tonic-gate */ 15047c478bd9Sstevel@tonic-gate (void) close(STDIN_FILENO); 15059acbbeafSnn35248 (void) openat(STDIN_FILENO, "/dev/null", O_RDONLY); 15069acbbeafSnn35248 if (!zoneadm_is_nested) 15077c478bd9Sstevel@tonic-gate (void) setpgid(0, 0); 15087c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, sigterm); 15097c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, sigterm); 15107c478bd9Sstevel@tonic-gate file = popen(cmdbuf, "r"); 15117c478bd9Sstevel@tonic-gate for (;;) { 15127c478bd9Sstevel@tonic-gate if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL) 15137c478bd9Sstevel@tonic-gate break; 15147c478bd9Sstevel@tonic-gate (void) fputs(inbuf, stdout); 15157c478bd9Sstevel@tonic-gate } 15167c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, SIG_DFL); 15177c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_DFL); 15187c478bd9Sstevel@tonic-gate return (pclose(file)); 15197c478bd9Sstevel@tonic-gate } 15207c478bd9Sstevel@tonic-gate 1521ff17c8bfSgjelinek int 15229acbbeafSnn35248 do_subproc_interactive(char *cmdbuf) 15239acbbeafSnn35248 { 15249acbbeafSnn35248 void (*saveint)(int); 15259acbbeafSnn35248 void (*saveterm)(int); 15269acbbeafSnn35248 void (*savequit)(int); 15279acbbeafSnn35248 void (*savehup)(int); 15289acbbeafSnn35248 int pid, child, status; 15299acbbeafSnn35248 15309acbbeafSnn35248 /* 15319acbbeafSnn35248 * do_subproc() links stdin to /dev/null, which would break any 15329acbbeafSnn35248 * interactive subprocess we try to launch here. Similarly, we 15339acbbeafSnn35248 * can't have been launched as a subprocess ourselves. 15349acbbeafSnn35248 */ 15359acbbeafSnn35248 assert(do_subproc_cnt == 0 && !zoneadm_is_nested); 15369acbbeafSnn35248 15379acbbeafSnn35248 if ((child = vfork()) == 0) { 15389acbbeafSnn35248 (void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL); 15399acbbeafSnn35248 } 15409acbbeafSnn35248 15419acbbeafSnn35248 if (child == -1) 15429acbbeafSnn35248 return (-1); 15439acbbeafSnn35248 15449acbbeafSnn35248 saveint = sigset(SIGINT, SIG_IGN); 15459acbbeafSnn35248 saveterm = sigset(SIGTERM, SIG_IGN); 15469acbbeafSnn35248 savequit = sigset(SIGQUIT, SIG_IGN); 15479acbbeafSnn35248 savehup = sigset(SIGHUP, SIG_IGN); 15489acbbeafSnn35248 15499acbbeafSnn35248 while ((pid = waitpid(child, &status, 0)) != child && pid != -1) 15509acbbeafSnn35248 ; 15519acbbeafSnn35248 15529acbbeafSnn35248 (void) sigset(SIGINT, saveint); 15539acbbeafSnn35248 (void) sigset(SIGTERM, saveterm); 15549acbbeafSnn35248 (void) sigset(SIGQUIT, savequit); 15559acbbeafSnn35248 (void) sigset(SIGHUP, savehup); 15569acbbeafSnn35248 15579acbbeafSnn35248 return (pid == -1 ? -1 : status); 15589acbbeafSnn35248 } 15599acbbeafSnn35248 1560ff17c8bfSgjelinek int 15619acbbeafSnn35248 subproc_status(const char *cmd, int status, boolean_t verbose_failure) 15627c478bd9Sstevel@tonic-gate { 15637c478bd9Sstevel@tonic-gate if (WIFEXITED(status)) { 15647c478bd9Sstevel@tonic-gate int exit_code = WEXITSTATUS(status); 15657c478bd9Sstevel@tonic-gate 15669acbbeafSnn35248 if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK)) 15677c478bd9Sstevel@tonic-gate zerror(gettext("'%s' failed with exit code %d."), cmd, 15687c478bd9Sstevel@tonic-gate exit_code); 15699acbbeafSnn35248 15709acbbeafSnn35248 return (exit_code); 15717c478bd9Sstevel@tonic-gate } else if (WIFSIGNALED(status)) { 15727c478bd9Sstevel@tonic-gate int signal = WTERMSIG(status); 15737c478bd9Sstevel@tonic-gate char sigstr[SIG2STR_MAX]; 15747c478bd9Sstevel@tonic-gate 15757c478bd9Sstevel@tonic-gate if (sig2str(signal, sigstr) == 0) { 15767c478bd9Sstevel@tonic-gate zerror(gettext("'%s' terminated by signal SIG%s."), cmd, 15777c478bd9Sstevel@tonic-gate sigstr); 15787c478bd9Sstevel@tonic-gate } else { 15797c478bd9Sstevel@tonic-gate zerror(gettext("'%s' terminated by an unknown signal."), 15807c478bd9Sstevel@tonic-gate cmd); 15817c478bd9Sstevel@tonic-gate } 15827c478bd9Sstevel@tonic-gate } else { 15837c478bd9Sstevel@tonic-gate zerror(gettext("'%s' failed for unknown reasons."), cmd); 15847c478bd9Sstevel@tonic-gate } 15859acbbeafSnn35248 15869acbbeafSnn35248 /* 15879acbbeafSnn35248 * Assume a subprocess that died due to a signal or an unknown error 15889acbbeafSnn35248 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the 15899acbbeafSnn35248 * user will likely need to do some manual cleanup. 15909acbbeafSnn35248 */ 15919acbbeafSnn35248 return (ZONE_SUBPROC_FATAL); 15927c478bd9Sstevel@tonic-gate } 15937c478bd9Sstevel@tonic-gate 15947c478bd9Sstevel@tonic-gate /* 15957c478bd9Sstevel@tonic-gate * Various sanity checks; make sure: 15967c478bd9Sstevel@tonic-gate * 1. We're in the global zone. 15977c478bd9Sstevel@tonic-gate * 2. The calling user has sufficient privilege. 15987c478bd9Sstevel@tonic-gate * 3. The target zone is neither the global zone nor anything starting with 15997c478bd9Sstevel@tonic-gate * "SUNW". 16007c478bd9Sstevel@tonic-gate * 4a. If we're looking for a 'not running' (i.e., configured or installed) 16017c478bd9Sstevel@tonic-gate * zone, the name service knows about it. 16027c478bd9Sstevel@tonic-gate * 4b. For some operations which expect a zone not to be running, that it is 16037c478bd9Sstevel@tonic-gate * not already running (or ready). 16047c478bd9Sstevel@tonic-gate */ 16057c478bd9Sstevel@tonic-gate static int 16067c478bd9Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running, 16079acbbeafSnn35248 boolean_t unsafe_when_running, boolean_t force) 16087c478bd9Sstevel@tonic-gate { 16097c478bd9Sstevel@tonic-gate zone_entry_t *zent; 16107c478bd9Sstevel@tonic-gate priv_set_t *privset; 16119acbbeafSnn35248 zone_state_t state, min_state; 1612108322fbScarlsonj char kernzone[ZONENAME_MAX]; 1613108322fbScarlsonj FILE *fp; 16147c478bd9Sstevel@tonic-gate 16157c478bd9Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) { 1616ffbafc53Scomay switch (cmd_num) { 1617ffbafc53Scomay case CMD_HALT: 1618ffbafc53Scomay zerror(gettext("use %s to %s this zone."), "halt(1M)", 16197c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 1620ffbafc53Scomay break; 1621ffbafc53Scomay case CMD_REBOOT: 1622ffbafc53Scomay zerror(gettext("use %s to %s this zone."), 1623ffbafc53Scomay "reboot(1M)", cmd_to_str(cmd_num)); 1624ffbafc53Scomay break; 1625ffbafc53Scomay default: 1626ffbafc53Scomay zerror(gettext("must be in the global zone to %s a " 1627ffbafc53Scomay "zone."), cmd_to_str(cmd_num)); 1628ffbafc53Scomay break; 1629ffbafc53Scomay } 16307c478bd9Sstevel@tonic-gate return (Z_ERR); 16317c478bd9Sstevel@tonic-gate } 16327c478bd9Sstevel@tonic-gate 16337c478bd9Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 16347c478bd9Sstevel@tonic-gate zerror(gettext("%s failed"), "priv_allocset"); 16357c478bd9Sstevel@tonic-gate return (Z_ERR); 16367c478bd9Sstevel@tonic-gate } 16377c478bd9Sstevel@tonic-gate 16387c478bd9Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 16397c478bd9Sstevel@tonic-gate zerror(gettext("%s failed"), "getppriv"); 16407c478bd9Sstevel@tonic-gate priv_freeset(privset); 16417c478bd9Sstevel@tonic-gate return (Z_ERR); 16427c478bd9Sstevel@tonic-gate } 16437c478bd9Sstevel@tonic-gate 16447c478bd9Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 16457c478bd9Sstevel@tonic-gate zerror(gettext("only a privileged user may %s a zone."), 16467c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 16477c478bd9Sstevel@tonic-gate priv_freeset(privset); 16487c478bd9Sstevel@tonic-gate return (Z_ERR); 16497c478bd9Sstevel@tonic-gate } 16507c478bd9Sstevel@tonic-gate priv_freeset(privset); 16517c478bd9Sstevel@tonic-gate 16527c478bd9Sstevel@tonic-gate if (zone == NULL) { 16537c478bd9Sstevel@tonic-gate zerror(gettext("no zone specified")); 16547c478bd9Sstevel@tonic-gate return (Z_ERR); 16557c478bd9Sstevel@tonic-gate } 16567c478bd9Sstevel@tonic-gate 16577c478bd9Sstevel@tonic-gate if (strcmp(zone, GLOBAL_ZONENAME) == 0) { 16587c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for the global zone."), 16597c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 16607c478bd9Sstevel@tonic-gate return (Z_ERR); 16617c478bd9Sstevel@tonic-gate } 16627c478bd9Sstevel@tonic-gate 16637c478bd9Sstevel@tonic-gate if (strncmp(zone, "SUNW", 4) == 0) { 16647c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for zones starting " 16657c478bd9Sstevel@tonic-gate "with SUNW."), cmd_to_str(cmd_num)); 16667c478bd9Sstevel@tonic-gate return (Z_ERR); 16677c478bd9Sstevel@tonic-gate } 16687c478bd9Sstevel@tonic-gate 1669108322fbScarlsonj if (!zonecfg_in_alt_root()) { 1670108322fbScarlsonj zent = lookup_running_zone(zone); 1671108322fbScarlsonj } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 1672108322fbScarlsonj zent = NULL; 1673108322fbScarlsonj } else { 1674108322fbScarlsonj if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(), 1675108322fbScarlsonj kernzone, sizeof (kernzone)) == 0) 1676108322fbScarlsonj zent = lookup_running_zone(kernzone); 1677108322fbScarlsonj else 1678108322fbScarlsonj zent = NULL; 1679108322fbScarlsonj zonecfg_close_scratch(fp); 1680108322fbScarlsonj } 1681108322fbScarlsonj 16827c478bd9Sstevel@tonic-gate /* 16837c478bd9Sstevel@tonic-gate * Look up from the kernel for 'running' zones. 16847c478bd9Sstevel@tonic-gate */ 16859acbbeafSnn35248 if (running && !force) { 16867c478bd9Sstevel@tonic-gate if (zent == NULL) { 16877c478bd9Sstevel@tonic-gate zerror(gettext("not running")); 16887c478bd9Sstevel@tonic-gate return (Z_ERR); 16897c478bd9Sstevel@tonic-gate } 16907c478bd9Sstevel@tonic-gate } else { 16917c478bd9Sstevel@tonic-gate int err; 16927c478bd9Sstevel@tonic-gate 16937c478bd9Sstevel@tonic-gate if (unsafe_when_running && zent != NULL) { 16947c478bd9Sstevel@tonic-gate /* check whether the zone is ready or running */ 16957c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, 16967c478bd9Sstevel@tonic-gate &zent->zstate_num)) != Z_OK) { 16977c478bd9Sstevel@tonic-gate errno = err; 16987c478bd9Sstevel@tonic-gate zperror2(zent->zname, 16997c478bd9Sstevel@tonic-gate gettext("could not get state")); 17007c478bd9Sstevel@tonic-gate /* can't tell, so hedge */ 17017c478bd9Sstevel@tonic-gate zent->zstate_str = "ready/running"; 17027c478bd9Sstevel@tonic-gate } else { 17037c478bd9Sstevel@tonic-gate zent->zstate_str = 17047c478bd9Sstevel@tonic-gate zone_state_str(zent->zstate_num); 17057c478bd9Sstevel@tonic-gate } 17067c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for %s zones."), 17077c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num), zent->zstate_str); 17087c478bd9Sstevel@tonic-gate return (Z_ERR); 17097c478bd9Sstevel@tonic-gate } 17107c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zone, &state)) != Z_OK) { 17117c478bd9Sstevel@tonic-gate errno = err; 17127c478bd9Sstevel@tonic-gate zperror2(zone, gettext("could not get state")); 17137c478bd9Sstevel@tonic-gate return (Z_ERR); 17147c478bd9Sstevel@tonic-gate } 17157c478bd9Sstevel@tonic-gate switch (cmd_num) { 17167c478bd9Sstevel@tonic-gate case CMD_UNINSTALL: 17177c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 17187c478bd9Sstevel@tonic-gate zerror(gettext("is already in state '%s'."), 17197c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_CONFIGURED)); 17207c478bd9Sstevel@tonic-gate return (Z_ERR); 17217c478bd9Sstevel@tonic-gate } 17227c478bd9Sstevel@tonic-gate break; 1723ee519a1fSgjelinek case CMD_ATTACH: 1724865e09a4Sgjelinek case CMD_CLONE: 17257c478bd9Sstevel@tonic-gate case CMD_INSTALL: 17267c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_INSTALLED) { 17277c478bd9Sstevel@tonic-gate zerror(gettext("is already %s."), 17287c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED)); 17297c478bd9Sstevel@tonic-gate return (Z_ERR); 17307c478bd9Sstevel@tonic-gate } else if (state == ZONE_STATE_INCOMPLETE) { 17317c478bd9Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 17327c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 17337c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 17347c478bd9Sstevel@tonic-gate return (Z_ERR); 17357c478bd9Sstevel@tonic-gate } 17367c478bd9Sstevel@tonic-gate break; 1737ee519a1fSgjelinek case CMD_DETACH: 1738865e09a4Sgjelinek case CMD_MOVE: 17397c478bd9Sstevel@tonic-gate case CMD_READY: 17407c478bd9Sstevel@tonic-gate case CMD_BOOT: 1741108322fbScarlsonj case CMD_MOUNT: 1742555afedfScarlsonj case CMD_MARK: 17439acbbeafSnn35248 if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) && 17449acbbeafSnn35248 force) 17459acbbeafSnn35248 min_state = ZONE_STATE_INCOMPLETE; 17469acbbeafSnn35248 else 17479acbbeafSnn35248 min_state = ZONE_STATE_INSTALLED; 17489acbbeafSnn35248 17499acbbeafSnn35248 if (force && cmd_num == CMD_BOOT && is_native_zone) { 17509acbbeafSnn35248 zerror(gettext("Only branded zones may be " 17519acbbeafSnn35248 "force-booted.")); 17529acbbeafSnn35248 return (Z_ERR); 17539acbbeafSnn35248 } 17549acbbeafSnn35248 17559acbbeafSnn35248 if (state < min_state) { 17567c478bd9Sstevel@tonic-gate zerror(gettext("must be %s before %s."), 17579acbbeafSnn35248 zone_state_str(min_state), 17587c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 17597c478bd9Sstevel@tonic-gate return (Z_ERR); 17607c478bd9Sstevel@tonic-gate } 17617c478bd9Sstevel@tonic-gate break; 17627c478bd9Sstevel@tonic-gate case CMD_VERIFY: 17637c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_INCOMPLETE) { 17647c478bd9Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 17657c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 17667c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 17677c478bd9Sstevel@tonic-gate return (Z_ERR); 17687c478bd9Sstevel@tonic-gate } 17697c478bd9Sstevel@tonic-gate break; 1770108322fbScarlsonj case CMD_UNMOUNT: 1771108322fbScarlsonj if (state != ZONE_STATE_MOUNTED) { 1772108322fbScarlsonj zerror(gettext("must be %s before %s."), 1773108322fbScarlsonj zone_state_str(ZONE_STATE_MOUNTED), 1774108322fbScarlsonj cmd_to_str(cmd_num)); 1775108322fbScarlsonj return (Z_ERR); 1776108322fbScarlsonj } 1777108322fbScarlsonj break; 17787c478bd9Sstevel@tonic-gate } 17797c478bd9Sstevel@tonic-gate } 17807c478bd9Sstevel@tonic-gate return (Z_OK); 17817c478bd9Sstevel@tonic-gate } 17827c478bd9Sstevel@tonic-gate 17837c478bd9Sstevel@tonic-gate static int 17847c478bd9Sstevel@tonic-gate halt_func(int argc, char *argv[]) 17857c478bd9Sstevel@tonic-gate { 17867c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 17877c478bd9Sstevel@tonic-gate int arg; 17887c478bd9Sstevel@tonic-gate 1789108322fbScarlsonj if (zonecfg_in_alt_root()) { 1790108322fbScarlsonj zerror(gettext("cannot halt zone in alternate root")); 1791108322fbScarlsonj return (Z_ERR); 1792108322fbScarlsonj } 1793108322fbScarlsonj 17947c478bd9Sstevel@tonic-gate optind = 0; 17957c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 17967c478bd9Sstevel@tonic-gate switch (arg) { 17977c478bd9Sstevel@tonic-gate case '?': 17987c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 17997c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 18007c478bd9Sstevel@tonic-gate default: 18017c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 18027c478bd9Sstevel@tonic-gate return (Z_USAGE); 18037c478bd9Sstevel@tonic-gate } 18047c478bd9Sstevel@tonic-gate } 18057c478bd9Sstevel@tonic-gate if (argc > optind) { 18067c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 18077c478bd9Sstevel@tonic-gate return (Z_USAGE); 18087c478bd9Sstevel@tonic-gate } 18097c478bd9Sstevel@tonic-gate /* 18107c478bd9Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 18117c478bd9Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 18127c478bd9Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 18137c478bd9Sstevel@tonic-gate */ 18149acbbeafSnn35248 if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE) 18159acbbeafSnn35248 != Z_OK) 18167c478bd9Sstevel@tonic-gate return (Z_ERR); 18177c478bd9Sstevel@tonic-gate 1818ce28b40eSzt129084 /* 1819ce28b40eSzt129084 * Invoke brand-specific handler. 1820ce28b40eSzt129084 */ 1821ce28b40eSzt129084 if (invoke_brand_handler(CMD_HALT, argv) != Z_OK) 1822ce28b40eSzt129084 return (Z_ERR); 1823ce28b40eSzt129084 18247c478bd9Sstevel@tonic-gate zarg.cmd = Z_HALT; 1825ff17c8bfSgjelinek return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale, 1826ff17c8bfSgjelinek B_TRUE) == 0) ? Z_OK : Z_ERR); 18277c478bd9Sstevel@tonic-gate } 18287c478bd9Sstevel@tonic-gate 18297c478bd9Sstevel@tonic-gate static int 18307c478bd9Sstevel@tonic-gate reboot_func(int argc, char *argv[]) 18317c478bd9Sstevel@tonic-gate { 18327c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 18337c478bd9Sstevel@tonic-gate int arg; 18347c478bd9Sstevel@tonic-gate 1835108322fbScarlsonj if (zonecfg_in_alt_root()) { 1836108322fbScarlsonj zerror(gettext("cannot reboot zone in alternate root")); 1837108322fbScarlsonj return (Z_ERR); 1838108322fbScarlsonj } 1839108322fbScarlsonj 18407c478bd9Sstevel@tonic-gate optind = 0; 18417c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 18427c478bd9Sstevel@tonic-gate switch (arg) { 18437c478bd9Sstevel@tonic-gate case '?': 18447c478bd9Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 18457c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 18467c478bd9Sstevel@tonic-gate default: 18477c478bd9Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 18487c478bd9Sstevel@tonic-gate return (Z_USAGE); 18497c478bd9Sstevel@tonic-gate } 18507c478bd9Sstevel@tonic-gate } 18513f2f09c1Sdp 18523f2f09c1Sdp zarg.bootbuf[0] = '\0'; 18533f2f09c1Sdp for (; optind < argc; optind++) { 18543f2f09c1Sdp if (strlcat(zarg.bootbuf, argv[optind], 18553f2f09c1Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 18563f2f09c1Sdp zerror(gettext("Boot argument list too long")); 18573f2f09c1Sdp return (Z_ERR); 18587c478bd9Sstevel@tonic-gate } 18593f2f09c1Sdp if (optind < argc - 1) 18603f2f09c1Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 18613f2f09c1Sdp sizeof (zarg.bootbuf)) { 18623f2f09c1Sdp zerror(gettext("Boot argument list too long")); 18633f2f09c1Sdp return (Z_ERR); 18643f2f09c1Sdp } 18653f2f09c1Sdp } 18663f2f09c1Sdp 18673f2f09c1Sdp 18687c478bd9Sstevel@tonic-gate /* 18697c478bd9Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 18707c478bd9Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 18717c478bd9Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 18727c478bd9Sstevel@tonic-gate */ 18739acbbeafSnn35248 if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE) 18749acbbeafSnn35248 != Z_OK) 18757c478bd9Sstevel@tonic-gate return (Z_ERR); 1876ce28b40eSzt129084 if (verify_details(CMD_REBOOT, argv) != Z_OK) 1877b5abaf04Sgjelinek return (Z_ERR); 18787c478bd9Sstevel@tonic-gate 18797c478bd9Sstevel@tonic-gate zarg.cmd = Z_REBOOT; 1880ff17c8bfSgjelinek return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) == 0) 1881ff17c8bfSgjelinek ? Z_OK : Z_ERR); 1882ff17c8bfSgjelinek } 1883ff17c8bfSgjelinek 1884ff17c8bfSgjelinek static int 1885ff17c8bfSgjelinek get_hook(brand_handle_t bh, char *cmd, size_t len, int (*bp)(brand_handle_t, 1886ff17c8bfSgjelinek const char *, const char *, char *, size_t), char *zonename, char *zonepath) 1887ff17c8bfSgjelinek { 1888ff17c8bfSgjelinek if (strlcpy(cmd, EXEC_PREFIX, len) >= len) 1889ff17c8bfSgjelinek return (Z_ERR); 1890ff17c8bfSgjelinek 1891ff17c8bfSgjelinek if (bp(bh, zonename, zonepath, cmd + EXEC_LEN, len - EXEC_LEN) != 0) 1892ff17c8bfSgjelinek return (Z_ERR); 1893ff17c8bfSgjelinek 1894ff17c8bfSgjelinek if (strlen(cmd) <= EXEC_LEN) 1895ff17c8bfSgjelinek cmd[0] = '\0'; 1896ff17c8bfSgjelinek 1897ff17c8bfSgjelinek return (Z_OK); 18987c478bd9Sstevel@tonic-gate } 18997c478bd9Sstevel@tonic-gate 19007c478bd9Sstevel@tonic-gate static int 1901ce28b40eSzt129084 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[]) 19029acbbeafSnn35248 { 19039acbbeafSnn35248 char cmdbuf[MAXPATHLEN]; 19049acbbeafSnn35248 int err; 19059acbbeafSnn35248 char zonepath[MAXPATHLEN]; 1906123807fbSedp brand_handle_t bh = NULL; 1907ce28b40eSzt129084 int status, i; 19089acbbeafSnn35248 19099acbbeafSnn35248 /* 19109acbbeafSnn35248 * Fetch the verify command from the brand configuration. 19119acbbeafSnn35248 * "exec" the command so that the returned status is that of 19129acbbeafSnn35248 * the command and not the shell. 19139acbbeafSnn35248 */ 1914ff17c8bfSgjelinek if (handle == NULL) { 1915ff17c8bfSgjelinek (void) strlcpy(zonepath, "-", sizeof (zonepath)); 1916ff17c8bfSgjelinek } else if ((err = zonecfg_get_zonepath(handle, zonepath, 1917ff17c8bfSgjelinek sizeof (zonepath))) != Z_OK) { 19189acbbeafSnn35248 errno = err; 1919ce28b40eSzt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 19209acbbeafSnn35248 return (Z_ERR); 19219acbbeafSnn35248 } 1922123807fbSedp if ((bh = brand_open(target_brand)) == NULL) { 19239acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 19249acbbeafSnn35248 return (Z_ERR); 19259acbbeafSnn35248 } 19269acbbeafSnn35248 19279acbbeafSnn35248 /* 19289acbbeafSnn35248 * If the brand has its own verification routine, execute it now. 1929ce28b40eSzt129084 * The verification routine validates the intended zoneadm 1930ce28b40eSzt129084 * operation for the specific brand. The zoneadm subcommand and 1931ce28b40eSzt129084 * all its arguments are passed to the routine. 19329acbbeafSnn35248 */ 1933ff17c8bfSgjelinek err = get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_verify_adm, 1934ff17c8bfSgjelinek target_zone, zonepath); 1935123807fbSedp brand_close(bh); 1936ff17c8bfSgjelinek if (err != Z_OK) 1937ce28b40eSzt129084 return (Z_BRAND_ERROR); 1938ff17c8bfSgjelinek if (cmdbuf[0] == '\0') 1939ce28b40eSzt129084 return (Z_OK); 1940ce28b40eSzt129084 1941ce28b40eSzt129084 if (strlcat(cmdbuf, cmd_to_str(cmd_num), 1942ce28b40eSzt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf)) 1943ce28b40eSzt129084 return (Z_ERR); 1944ce28b40eSzt129084 1945ce28b40eSzt129084 /* Build the argv string */ 1946ce28b40eSzt129084 i = 0; 1947ce28b40eSzt129084 while (argv[i] != NULL) { 1948ce28b40eSzt129084 if ((strlcat(cmdbuf, " ", 1949ce28b40eSzt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf)) || 1950ce28b40eSzt129084 (strlcat(cmdbuf, argv[i++], 1951ce28b40eSzt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf))) 1952ce28b40eSzt129084 return (Z_ERR); 1953ce28b40eSzt129084 } 1954ce28b40eSzt129084 1955d9e728a2Sgjelinek if (zoneadm_is_nested) 1956d9e728a2Sgjelinek status = do_subproc(cmdbuf); 1957d9e728a2Sgjelinek else 19589acbbeafSnn35248 status = do_subproc_interactive(cmdbuf); 19599acbbeafSnn35248 err = subproc_status(gettext("brand-specific verification"), 19609acbbeafSnn35248 status, B_FALSE); 19619acbbeafSnn35248 1962ce28b40eSzt129084 return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR); 19639acbbeafSnn35248 } 19649acbbeafSnn35248 19659acbbeafSnn35248 static int 19667c478bd9Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle) 19677c478bd9Sstevel@tonic-gate { 19687c478bd9Sstevel@tonic-gate struct zone_rctltab rctltab; 19697c478bd9Sstevel@tonic-gate size_t rbs = rctlblk_size(); 19707c478bd9Sstevel@tonic-gate rctlblk_t *rctlblk; 19717c478bd9Sstevel@tonic-gate int error = Z_INVAL; 19727c478bd9Sstevel@tonic-gate 19737c478bd9Sstevel@tonic-gate if ((rctlblk = malloc(rbs)) == NULL) { 19747c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rbs, 19757c478bd9Sstevel@tonic-gate strerror(errno)); 19767c478bd9Sstevel@tonic-gate return (Z_NOMEM); 19777c478bd9Sstevel@tonic-gate } 19787c478bd9Sstevel@tonic-gate 19797c478bd9Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 19807c478bd9Sstevel@tonic-gate zerror(gettext("zonecfg_setrctlent failed")); 19817c478bd9Sstevel@tonic-gate free(rctlblk); 19827c478bd9Sstevel@tonic-gate return (error); 19837c478bd9Sstevel@tonic-gate } 19847c478bd9Sstevel@tonic-gate 19857c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 19867c478bd9Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 19877c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 19887c478bd9Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 19897c478bd9Sstevel@tonic-gate 19907c478bd9Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 19917c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: Ignoring unrecognized rctl " 19927c478bd9Sstevel@tonic-gate "'%s'."), name); 19937c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 19947c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 19957c478bd9Sstevel@tonic-gate continue; 19967c478bd9Sstevel@tonic-gate } 19977c478bd9Sstevel@tonic-gate 19987c478bd9Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 19997c478bd9Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 20007c478bd9Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 20017c478bd9Sstevel@tonic-gate != Z_OK) { 20027c478bd9Sstevel@tonic-gate zerror(gettext("invalid rctl value: " 20037c478bd9Sstevel@tonic-gate "(priv=%s,limit=%s,action%s)"), 20047c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 20057c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 20067c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action); 20077c478bd9Sstevel@tonic-gate goto out; 20087c478bd9Sstevel@tonic-gate } 20097c478bd9Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 20107c478bd9Sstevel@tonic-gate zerror(gettext("(priv=%s,limit=%s,action=%s) " 20117c478bd9Sstevel@tonic-gate "is not a valid value for rctl '%s'"), 20127c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 20137c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 20147c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action, 20157c478bd9Sstevel@tonic-gate name); 20167c478bd9Sstevel@tonic-gate goto out; 20177c478bd9Sstevel@tonic-gate } 20187c478bd9Sstevel@tonic-gate } 20197c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 20207c478bd9Sstevel@tonic-gate } 20217c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 20227c478bd9Sstevel@tonic-gate error = Z_OK; 20237c478bd9Sstevel@tonic-gate out: 20247c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 20257c478bd9Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 20267c478bd9Sstevel@tonic-gate free(rctlblk); 20277c478bd9Sstevel@tonic-gate return (error); 20287c478bd9Sstevel@tonic-gate } 20297c478bd9Sstevel@tonic-gate 20307c478bd9Sstevel@tonic-gate static int 20317c478bd9Sstevel@tonic-gate verify_pool(zone_dochandle_t handle) 20327c478bd9Sstevel@tonic-gate { 20337c478bd9Sstevel@tonic-gate char poolname[MAXPATHLEN]; 20347c478bd9Sstevel@tonic-gate pool_conf_t *poolconf; 20357c478bd9Sstevel@tonic-gate pool_t *pool; 20367c478bd9Sstevel@tonic-gate int status; 20377c478bd9Sstevel@tonic-gate int error; 20387c478bd9Sstevel@tonic-gate 20397c478bd9Sstevel@tonic-gate /* 20407c478bd9Sstevel@tonic-gate * This ends up being very similar to the check done in zoneadmd. 20417c478bd9Sstevel@tonic-gate */ 20427c478bd9Sstevel@tonic-gate error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 20437c478bd9Sstevel@tonic-gate if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 20447c478bd9Sstevel@tonic-gate /* 20457c478bd9Sstevel@tonic-gate * No pool specified. 20467c478bd9Sstevel@tonic-gate */ 20477c478bd9Sstevel@tonic-gate return (0); 20487c478bd9Sstevel@tonic-gate } 20497c478bd9Sstevel@tonic-gate if (error != Z_OK) { 20507c478bd9Sstevel@tonic-gate zperror(gettext("Unable to retrieve pool name from " 20517c478bd9Sstevel@tonic-gate "configuration"), B_TRUE); 20527c478bd9Sstevel@tonic-gate return (error); 20537c478bd9Sstevel@tonic-gate } 20547c478bd9Sstevel@tonic-gate /* 20557c478bd9Sstevel@tonic-gate * Don't do anything if pools aren't enabled. 20567c478bd9Sstevel@tonic-gate */ 20577c478bd9Sstevel@tonic-gate if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 20587c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pools facility not active; " 20597c478bd9Sstevel@tonic-gate "zone will not be bound to pool '%s'."), poolname); 20607c478bd9Sstevel@tonic-gate return (Z_OK); 20617c478bd9Sstevel@tonic-gate } 20627c478bd9Sstevel@tonic-gate /* 20637c478bd9Sstevel@tonic-gate * Try to provide a sane error message if the requested pool doesn't 20647c478bd9Sstevel@tonic-gate * exist. It isn't clear that pools-related failures should 20657c478bd9Sstevel@tonic-gate * necessarily translate to a failure to verify the zone configuration, 20667c478bd9Sstevel@tonic-gate * hence they are not considered errors. 20677c478bd9Sstevel@tonic-gate */ 20687c478bd9Sstevel@tonic-gate if ((poolconf = pool_conf_alloc()) == NULL) { 20697c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_alloc failed; " 20707c478bd9Sstevel@tonic-gate "using default pool")); 20717c478bd9Sstevel@tonic-gate return (Z_OK); 20727c478bd9Sstevel@tonic-gate } 20737c478bd9Sstevel@tonic-gate if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 20747c478bd9Sstevel@tonic-gate PO_SUCCESS) { 20757c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_open failed; " 20767c478bd9Sstevel@tonic-gate "using default pool")); 20777c478bd9Sstevel@tonic-gate pool_conf_free(poolconf); 20787c478bd9Sstevel@tonic-gate return (Z_OK); 20797c478bd9Sstevel@tonic-gate } 20807c478bd9Sstevel@tonic-gate pool = pool_get_pool(poolconf, poolname); 20817c478bd9Sstevel@tonic-gate (void) pool_conf_close(poolconf); 20827c478bd9Sstevel@tonic-gate pool_conf_free(poolconf); 20837c478bd9Sstevel@tonic-gate if (pool == NULL) { 20847c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool '%s' not found. " 20857c478bd9Sstevel@tonic-gate "using default pool"), poolname); 20867c478bd9Sstevel@tonic-gate } 20877c478bd9Sstevel@tonic-gate 20887c478bd9Sstevel@tonic-gate return (Z_OK); 20897c478bd9Sstevel@tonic-gate } 20907c478bd9Sstevel@tonic-gate 20917c478bd9Sstevel@tonic-gate static int 2092b5abaf04Sgjelinek verify_ipd(zone_dochandle_t handle) 2093b5abaf04Sgjelinek { 2094b5abaf04Sgjelinek int return_code = Z_OK; 2095b5abaf04Sgjelinek struct zone_fstab fstab; 2096b5abaf04Sgjelinek struct stat st; 2097b5abaf04Sgjelinek char specdir[MAXPATHLEN]; 2098b5abaf04Sgjelinek 2099b5abaf04Sgjelinek if (zonecfg_setipdent(handle) != Z_OK) { 21005ee84fbdSgjelinek /* 21015ee84fbdSgjelinek * TRANSLATION_NOTE 21025ee84fbdSgjelinek * inherit-pkg-dirs is a literal that should not be translated. 21035ee84fbdSgjelinek */ 21045ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify " 2105b5abaf04Sgjelinek "inherit-pkg-dirs: unable to enumerate mounts\n")); 2106b5abaf04Sgjelinek return (Z_ERR); 2107b5abaf04Sgjelinek } 2108b5abaf04Sgjelinek while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 2109b5abaf04Sgjelinek /* 2110b5abaf04Sgjelinek * Verify fs_dir exists. 2111b5abaf04Sgjelinek */ 2112b5abaf04Sgjelinek (void) snprintf(specdir, sizeof (specdir), "%s%s", 2113b5abaf04Sgjelinek zonecfg_get_root(), fstab.zone_fs_dir); 2114b5abaf04Sgjelinek if (stat(specdir, &st) != 0) { 21155ee84fbdSgjelinek /* 21165ee84fbdSgjelinek * TRANSLATION_NOTE 21175ee84fbdSgjelinek * inherit-pkg-dir is a literal that should not be 21185ee84fbdSgjelinek * translated. 21195ee84fbdSgjelinek */ 21205ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify " 2121b5abaf04Sgjelinek "inherit-pkg-dir %s: %s\n"), 2122b5abaf04Sgjelinek fstab.zone_fs_dir, strerror(errno)); 2123b5abaf04Sgjelinek return_code = Z_ERR; 2124b5abaf04Sgjelinek } 2125b5abaf04Sgjelinek if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 21265ee84fbdSgjelinek /* 21275ee84fbdSgjelinek * TRANSLATION_NOTE 21285ee84fbdSgjelinek * inherit-pkg-dir and NFS are literals that should 21295ee84fbdSgjelinek * not be translated. 21305ee84fbdSgjelinek */ 2131b5abaf04Sgjelinek (void) fprintf(stderr, gettext("cannot verify " 21320b5de56dSgjelinek "inherit-pkg-dir %s: NFS mounted file system.\n" 21330b5de56dSgjelinek "\tA local file system must be used.\n"), 2134b5abaf04Sgjelinek fstab.zone_fs_dir); 2135b5abaf04Sgjelinek return_code = Z_ERR; 2136b5abaf04Sgjelinek } 2137b5abaf04Sgjelinek } 2138b5abaf04Sgjelinek (void) zonecfg_endipdent(handle); 2139b5abaf04Sgjelinek 2140b5abaf04Sgjelinek return (return_code); 2141b5abaf04Sgjelinek } 2142b5abaf04Sgjelinek 214320c8013fSlling /* 214420c8013fSlling * Verify that the special device/file system exists and is valid. 214520c8013fSlling */ 214620c8013fSlling static int 214720c8013fSlling verify_fs_special(struct zone_fstab *fstab) 214820c8013fSlling { 214993239addSjohnlev struct stat64 st; 215020c8013fSlling 21516cc813faSgjelinek /* 21526cc813faSgjelinek * This validation is really intended for standard zone administration. 21536cc813faSgjelinek * If we are in a mini-root or some other upgrade situation where 21546cc813faSgjelinek * we are using the scratch zone, just by-pass this. 21556cc813faSgjelinek */ 21566cc813faSgjelinek if (zonecfg_in_alt_root()) 21576cc813faSgjelinek return (Z_OK); 21586cc813faSgjelinek 215920c8013fSlling if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0) 216020c8013fSlling return (verify_fs_zfs(fstab)); 216120c8013fSlling 216293239addSjohnlev if (stat64(fstab->zone_fs_special, &st) != 0) { 21635c358068Slling (void) fprintf(stderr, gettext("could not verify fs " 216420c8013fSlling "%s: could not access %s: %s\n"), fstab->zone_fs_dir, 216520c8013fSlling fstab->zone_fs_special, strerror(errno)); 216620c8013fSlling return (Z_ERR); 216720c8013fSlling } 216820c8013fSlling 216920c8013fSlling if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 217020c8013fSlling /* 217120c8013fSlling * TRANSLATION_NOTE 217220c8013fSlling * fs and NFS are literals that should 217320c8013fSlling * not be translated. 217420c8013fSlling */ 217520c8013fSlling (void) fprintf(stderr, gettext("cannot verify " 21760b5de56dSgjelinek "fs %s: NFS mounted file system.\n" 21770b5de56dSgjelinek "\tA local file system must be used.\n"), 217820c8013fSlling fstab->zone_fs_special); 217920c8013fSlling return (Z_ERR); 218020c8013fSlling } 218120c8013fSlling 218220c8013fSlling return (Z_OK); 218320c8013fSlling } 218420c8013fSlling 2185b5abaf04Sgjelinek static int 218693239addSjohnlev isregfile(const char *path) 218793239addSjohnlev { 218893239addSjohnlev struct stat64 st; 218993239addSjohnlev 219093239addSjohnlev if (stat64(path, &st) == -1) 219193239addSjohnlev return (-1); 219293239addSjohnlev 219393239addSjohnlev return (S_ISREG(st.st_mode)); 219493239addSjohnlev } 219593239addSjohnlev 219693239addSjohnlev static int 21977c478bd9Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle) 21987c478bd9Sstevel@tonic-gate { 21997c478bd9Sstevel@tonic-gate int return_code = Z_OK; 22007c478bd9Sstevel@tonic-gate struct zone_fstab fstab; 22017c478bd9Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 22027c478bd9Sstevel@tonic-gate struct stat st; 22037c478bd9Sstevel@tonic-gate 22047c478bd9Sstevel@tonic-gate /* 22057c478bd9Sstevel@tonic-gate * No need to verify inherit-pkg-dir fs types, as their type is 22067c478bd9Sstevel@tonic-gate * implicitly lofs, which is known. Therefore, the types are only 22077c478bd9Sstevel@tonic-gate * verified for regular file systems below. 22087c478bd9Sstevel@tonic-gate * 22097c478bd9Sstevel@tonic-gate * Since the actual mount point is not known until the dependent mounts 22107c478bd9Sstevel@tonic-gate * are performed, we don't attempt any path validation here: that will 22117c478bd9Sstevel@tonic-gate * happen later when zoneadmd actually does the mounts. 22127c478bd9Sstevel@tonic-gate */ 22137c478bd9Sstevel@tonic-gate if (zonecfg_setfsent(handle) != Z_OK) { 22140b5de56dSgjelinek (void) fprintf(stderr, gettext("could not verify file systems: " 22157c478bd9Sstevel@tonic-gate "unable to enumerate mounts\n")); 22167c478bd9Sstevel@tonic-gate return (Z_ERR); 22177c478bd9Sstevel@tonic-gate } 22187c478bd9Sstevel@tonic-gate while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 22197c478bd9Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) { 22207c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 22217c478bd9Sstevel@tonic-gate "type %s is not allowed.\n"), fstab.zone_fs_dir, 22227c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 22237c478bd9Sstevel@tonic-gate return_code = Z_ERR; 22247c478bd9Sstevel@tonic-gate goto next_fs; 22257c478bd9Sstevel@tonic-gate } 22267c478bd9Sstevel@tonic-gate /* 22277c478bd9Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/mount exists. 22287c478bd9Sstevel@tonic-gate */ 22297c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", 22307c478bd9Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 22317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 22327c478bd9Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 22337c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 22347c478bd9Sstevel@tonic-gate return_code = Z_ERR; 22357c478bd9Sstevel@tonic-gate goto next_fs; 22367c478bd9Sstevel@tonic-gate } 22377c478bd9Sstevel@tonic-gate if (stat(cmdbuf, &st) != 0) { 22385ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 22395ee84fbdSgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 22407c478bd9Sstevel@tonic-gate cmdbuf, strerror(errno)); 22417c478bd9Sstevel@tonic-gate return_code = Z_ERR; 22427c478bd9Sstevel@tonic-gate goto next_fs; 22437c478bd9Sstevel@tonic-gate } 22447c478bd9Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 22455ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 22465ee84fbdSgjelinek "%s: %s is not a regular file\n"), 22475ee84fbdSgjelinek fstab.zone_fs_dir, cmdbuf); 22487c478bd9Sstevel@tonic-gate return_code = Z_ERR; 22497c478bd9Sstevel@tonic-gate goto next_fs; 22507c478bd9Sstevel@tonic-gate } 22517c478bd9Sstevel@tonic-gate /* 225293239addSjohnlev * If zone_fs_raw is set, verify that there's an fsck 225393239addSjohnlev * binary for it. If zone_fs_raw is not set, and it's 225493239addSjohnlev * not a regular file (lofi mount), and there's an fsck 225593239addSjohnlev * binary for it, complain. 22567c478bd9Sstevel@tonic-gate */ 22577c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", 22587c478bd9Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 22597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 22607c478bd9Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 22617c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 22627c478bd9Sstevel@tonic-gate return_code = Z_ERR; 22637c478bd9Sstevel@tonic-gate goto next_fs; 22647c478bd9Sstevel@tonic-gate } 22657c478bd9Sstevel@tonic-gate if (fstab.zone_fs_raw[0] != '\0' && 22667c478bd9Sstevel@tonic-gate (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) { 22677c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 22687c478bd9Sstevel@tonic-gate "'raw' device specified but " 22697c478bd9Sstevel@tonic-gate "no fsck executable exists for %s\n"), 22707c478bd9Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 22717c478bd9Sstevel@tonic-gate return_code = Z_ERR; 22727c478bd9Sstevel@tonic-gate goto next_fs; 227393239addSjohnlev } else if (fstab.zone_fs_raw[0] == '\0' && 227493239addSjohnlev stat(cmdbuf, &st) == 0 && 227593239addSjohnlev isregfile(fstab.zone_fs_special) != 1) { 227693239addSjohnlev (void) fprintf(stderr, gettext("could not verify fs " 227793239addSjohnlev "%s: must specify 'raw' device for %s " 227893239addSjohnlev "file systems\n"), 227993239addSjohnlev fstab.zone_fs_dir, fstab.zone_fs_type); 228093239addSjohnlev return_code = Z_ERR; 228193239addSjohnlev goto next_fs; 22827c478bd9Sstevel@tonic-gate } 228320c8013fSlling 228420c8013fSlling /* Verify fs_special. */ 228520c8013fSlling if ((return_code = verify_fs_special(&fstab)) != Z_OK) 2286b5abaf04Sgjelinek goto next_fs; 228720c8013fSlling 228820c8013fSlling /* Verify fs_raw. */ 2289b5abaf04Sgjelinek if (fstab.zone_fs_raw[0] != '\0' && 2290b5abaf04Sgjelinek stat(fstab.zone_fs_raw, &st) != 0) { 22915ee84fbdSgjelinek /* 22925ee84fbdSgjelinek * TRANSLATION_NOTE 22935ee84fbdSgjelinek * fs is a literal that should not be translated. 22945ee84fbdSgjelinek */ 22955ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 22965ee84fbdSgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 2297b5abaf04Sgjelinek fstab.zone_fs_raw, strerror(errno)); 2298b5abaf04Sgjelinek return_code = Z_ERR; 2299b5abaf04Sgjelinek goto next_fs; 2300b5abaf04Sgjelinek } 23017c478bd9Sstevel@tonic-gate next_fs: 23027c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(fstab.zone_fs_options); 23037c478bd9Sstevel@tonic-gate } 23047c478bd9Sstevel@tonic-gate (void) zonecfg_endfsent(handle); 23057c478bd9Sstevel@tonic-gate 23067c478bd9Sstevel@tonic-gate return (return_code); 23077c478bd9Sstevel@tonic-gate } 23087c478bd9Sstevel@tonic-gate 23097c478bd9Sstevel@tonic-gate static int 2310ffbafc53Scomay verify_limitpriv(zone_dochandle_t handle) 2311ffbafc53Scomay { 2312ffbafc53Scomay char *privname = NULL; 2313ffbafc53Scomay int err; 2314ffbafc53Scomay priv_set_t *privs; 2315ffbafc53Scomay 2316ffbafc53Scomay if ((privs = priv_allocset()) == NULL) { 2317ffbafc53Scomay zperror(gettext("failed to allocate privilege set"), B_FALSE); 2318ffbafc53Scomay return (Z_NOMEM); 2319ffbafc53Scomay } 2320ffbafc53Scomay err = zonecfg_get_privset(handle, privs, &privname); 2321ffbafc53Scomay switch (err) { 2322ffbafc53Scomay case Z_OK: 2323ffbafc53Scomay break; 2324ffbafc53Scomay case Z_PRIV_PROHIBITED: 2325ffbafc53Scomay (void) fprintf(stderr, gettext("privilege \"%s\" is not " 2326ffbafc53Scomay "permitted within the zone's privilege set\n"), privname); 2327ffbafc53Scomay break; 2328ffbafc53Scomay case Z_PRIV_REQUIRED: 2329ffbafc53Scomay (void) fprintf(stderr, gettext("required privilege \"%s\" is " 2330ffbafc53Scomay "missing from the zone's privilege set\n"), privname); 2331ffbafc53Scomay break; 2332ffbafc53Scomay case Z_PRIV_UNKNOWN: 2333ffbafc53Scomay (void) fprintf(stderr, gettext("unknown privilege \"%s\" " 2334ffbafc53Scomay "specified in the zone's privilege set\n"), privname); 2335ffbafc53Scomay break; 2336ffbafc53Scomay default: 2337ffbafc53Scomay zperror( 2338ffbafc53Scomay gettext("failed to determine the zone's privilege set"), 2339ffbafc53Scomay B_TRUE); 2340ffbafc53Scomay break; 2341ffbafc53Scomay } 2342ffbafc53Scomay free(privname); 2343ffbafc53Scomay priv_freeset(privs); 2344ffbafc53Scomay return (err); 2345ffbafc53Scomay } 2346ffbafc53Scomay 23471390a385Sgjelinek static void 23481390a385Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list) 23491390a385Sgjelinek { 23501390a385Sgjelinek int i; 23511390a385Sgjelinek 23521390a385Sgjelinek for (i = 0; i < if_cnt; i++) { 23531390a385Sgjelinek free(if_list[i]->name); 23541390a385Sgjelinek free(if_list[i]); 23551390a385Sgjelinek } 23561390a385Sgjelinek free(if_list); 23571390a385Sgjelinek } 23581390a385Sgjelinek 23591390a385Sgjelinek /* 23601390a385Sgjelinek * Get a list of the network interfaces, along with their address families, 23611390a385Sgjelinek * that are plumbed in the global zone. See if_tcp(7p) for a description 23621390a385Sgjelinek * of the ioctls used here. 23631390a385Sgjelinek */ 23641390a385Sgjelinek static int 23651390a385Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list) 23661390a385Sgjelinek { 23671390a385Sgjelinek int s; 23681390a385Sgjelinek int i; 23691390a385Sgjelinek int res = Z_OK; 23701390a385Sgjelinek int space_needed; 23711390a385Sgjelinek int cnt = 0; 23721390a385Sgjelinek struct lifnum if_num; 23731390a385Sgjelinek struct lifconf if_conf; 23741390a385Sgjelinek struct lifreq *if_reqp; 23751390a385Sgjelinek char *if_buf; 23761390a385Sgjelinek struct net_if **local_ifs = NULL; 23771390a385Sgjelinek 23781390a385Sgjelinek *if_cnt = 0; 23791390a385Sgjelinek *if_list = NULL; 23801390a385Sgjelinek 23811390a385Sgjelinek if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0) 23821390a385Sgjelinek return (Z_ERR); 23831390a385Sgjelinek 23841390a385Sgjelinek /* 23851390a385Sgjelinek * Come back here in the unlikely event that the number of interfaces 23861390a385Sgjelinek * increases between the time we get the count and the time we do the 23871390a385Sgjelinek * SIOCGLIFCONF ioctl. 23881390a385Sgjelinek */ 23891390a385Sgjelinek retry: 23901390a385Sgjelinek /* Get the number of interfaces. */ 23911390a385Sgjelinek if_num.lifn_family = AF_UNSPEC; 23921390a385Sgjelinek if_num.lifn_flags = LIFC_NOXMIT; 23931390a385Sgjelinek if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) { 23941390a385Sgjelinek (void) close(s); 23951390a385Sgjelinek return (Z_ERR); 23961390a385Sgjelinek } 23971390a385Sgjelinek 23981390a385Sgjelinek /* Get the interface configuration list. */ 23991390a385Sgjelinek space_needed = if_num.lifn_count * sizeof (struct lifreq); 24001390a385Sgjelinek if ((if_buf = malloc(space_needed)) == NULL) { 24011390a385Sgjelinek (void) close(s); 24021390a385Sgjelinek return (Z_ERR); 24031390a385Sgjelinek } 24041390a385Sgjelinek if_conf.lifc_family = AF_UNSPEC; 24051390a385Sgjelinek if_conf.lifc_flags = LIFC_NOXMIT; 24061390a385Sgjelinek if_conf.lifc_len = space_needed; 24071390a385Sgjelinek if_conf.lifc_buf = if_buf; 24081390a385Sgjelinek if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) { 24091390a385Sgjelinek free(if_buf); 24101390a385Sgjelinek /* 24111390a385Sgjelinek * SIOCGLIFCONF returns EINVAL if the buffer we passed in is 24121390a385Sgjelinek * too small. In this case go back and get the new if cnt. 24131390a385Sgjelinek */ 24141390a385Sgjelinek if (errno == EINVAL) 24151390a385Sgjelinek goto retry; 24161390a385Sgjelinek 24171390a385Sgjelinek (void) close(s); 24181390a385Sgjelinek return (Z_ERR); 24191390a385Sgjelinek } 24201390a385Sgjelinek (void) close(s); 24211390a385Sgjelinek 24221390a385Sgjelinek /* Get the name and address family for each interface. */ 24231390a385Sgjelinek if_reqp = if_conf.lifc_req; 24241390a385Sgjelinek for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) { 24251390a385Sgjelinek struct net_if **p; 24261390a385Sgjelinek struct lifreq req; 24271390a385Sgjelinek 24281390a385Sgjelinek if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) { 24291390a385Sgjelinek if_reqp++; 24301390a385Sgjelinek continue; 24311390a385Sgjelinek } 24321390a385Sgjelinek 24331390a385Sgjelinek if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family), 24341390a385Sgjelinek SOCK_DGRAM, 0)) == -1) { 24351390a385Sgjelinek res = Z_ERR; 24361390a385Sgjelinek break; 24371390a385Sgjelinek } 24381390a385Sgjelinek 24391390a385Sgjelinek (void) strncpy(req.lifr_name, if_reqp->lifr_name, 24401390a385Sgjelinek sizeof (req.lifr_name)); 24411390a385Sgjelinek if (ioctl(s, SIOCGLIFADDR, &req) < 0) { 24421390a385Sgjelinek (void) close(s); 24431390a385Sgjelinek if_reqp++; 24441390a385Sgjelinek continue; 24451390a385Sgjelinek } 24461390a385Sgjelinek 24471390a385Sgjelinek if ((p = (struct net_if **)realloc(local_ifs, 24481390a385Sgjelinek sizeof (struct net_if *) * (cnt + 1))) == NULL) { 24491390a385Sgjelinek res = Z_ERR; 24501390a385Sgjelinek break; 24511390a385Sgjelinek } 24521390a385Sgjelinek local_ifs = p; 24531390a385Sgjelinek 24541390a385Sgjelinek if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) { 24551390a385Sgjelinek res = Z_ERR; 24561390a385Sgjelinek break; 24571390a385Sgjelinek } 24581390a385Sgjelinek 24591390a385Sgjelinek if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name)) 24601390a385Sgjelinek == NULL) { 24611390a385Sgjelinek free(local_ifs[cnt]); 24621390a385Sgjelinek res = Z_ERR; 24631390a385Sgjelinek break; 24641390a385Sgjelinek } 24651390a385Sgjelinek local_ifs[cnt]->af = req.lifr_addr.ss_family; 24661390a385Sgjelinek cnt++; 24671390a385Sgjelinek 24681390a385Sgjelinek (void) close(s); 24691390a385Sgjelinek if_reqp++; 24701390a385Sgjelinek } 24711390a385Sgjelinek 24721390a385Sgjelinek free(if_buf); 24731390a385Sgjelinek 24741390a385Sgjelinek if (res != Z_OK) { 24751390a385Sgjelinek free_local_netifs(cnt, local_ifs); 24761390a385Sgjelinek } else { 24771390a385Sgjelinek *if_cnt = cnt; 24781390a385Sgjelinek *if_list = local_ifs; 24791390a385Sgjelinek } 24801390a385Sgjelinek 24811390a385Sgjelinek return (res); 24821390a385Sgjelinek } 24831390a385Sgjelinek 24841390a385Sgjelinek static char * 24851390a385Sgjelinek af2str(int af) 24861390a385Sgjelinek { 24871390a385Sgjelinek switch (af) { 24881390a385Sgjelinek case AF_INET: 24891390a385Sgjelinek return ("IPv4"); 24901390a385Sgjelinek case AF_INET6: 24911390a385Sgjelinek return ("IPv6"); 24921390a385Sgjelinek default: 24931390a385Sgjelinek return ("Unknown"); 24941390a385Sgjelinek } 24951390a385Sgjelinek } 24961390a385Sgjelinek 24971390a385Sgjelinek /* 24981390a385Sgjelinek * Cross check the network interface name and address family with the 24991390a385Sgjelinek * interfaces that are set up in the global zone so that we can print the 25001390a385Sgjelinek * appropriate error message. 25011390a385Sgjelinek */ 25021390a385Sgjelinek static void 25031390a385Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg) 25041390a385Sgjelinek { 25051390a385Sgjelinek int i; 25061390a385Sgjelinek int local_if_cnt = 0; 25071390a385Sgjelinek struct net_if **local_ifs = NULL; 25081390a385Sgjelinek boolean_t found_if = B_FALSE; 25091390a385Sgjelinek boolean_t found_af = B_FALSE; 25101390a385Sgjelinek 25111390a385Sgjelinek if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) { 25121390a385Sgjelinek (void) fprintf(stderr, 25131390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 25141390a385Sgjelinek "net", "address", addr, "physical", phys, msg); 25151390a385Sgjelinek return; 25161390a385Sgjelinek } 25171390a385Sgjelinek 25181390a385Sgjelinek for (i = 0; i < local_if_cnt; i++) { 25191390a385Sgjelinek if (strcmp(phys, local_ifs[i]->name) == 0) { 25201390a385Sgjelinek found_if = B_TRUE; 25211390a385Sgjelinek if (af == local_ifs[i]->af) { 25221390a385Sgjelinek found_af = B_TRUE; 25231390a385Sgjelinek break; 25241390a385Sgjelinek } 25251390a385Sgjelinek } 25261390a385Sgjelinek } 25271390a385Sgjelinek 25281390a385Sgjelinek free_local_netifs(local_if_cnt, local_ifs); 25291390a385Sgjelinek 25301390a385Sgjelinek if (!found_if) { 25311390a385Sgjelinek (void) fprintf(stderr, 25321390a385Sgjelinek gettext("could not verify %s %s=%s\n\t" 25331390a385Sgjelinek "network interface %s is not plumbed in the global zone\n"), 25341390a385Sgjelinek "net", "physical", phys, phys); 25351390a385Sgjelinek return; 25361390a385Sgjelinek } 25371390a385Sgjelinek 25381390a385Sgjelinek /* 25391390a385Sgjelinek * Print this error if we were unable to find the address family 25401390a385Sgjelinek * for this interface. If the af variable is not initialized to 25411390a385Sgjelinek * to something meaningful by the caller (not AF_UNSPEC) then we 25421390a385Sgjelinek * also skip this message since it wouldn't be informative. 25431390a385Sgjelinek */ 25441390a385Sgjelinek if (!found_af && af != AF_UNSPEC) { 25451390a385Sgjelinek (void) fprintf(stderr, 25461390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\tthe %s address " 2547f4b3ec61Sdh155122 "family is not configured on this network interface in " 2548f4b3ec61Sdh155122 "the\n\tglobal zone\n"), 25491390a385Sgjelinek "net", "address", addr, "physical", phys, af2str(af)); 25501390a385Sgjelinek return; 25511390a385Sgjelinek } 25521390a385Sgjelinek 25531390a385Sgjelinek (void) fprintf(stderr, 25541390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 25551390a385Sgjelinek "net", "address", addr, "physical", phys, msg); 25561390a385Sgjelinek } 25571390a385Sgjelinek 2558ffbafc53Scomay static int 2559ce28b40eSzt129084 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[]) 25607c478bd9Sstevel@tonic-gate { 25617c478bd9Sstevel@tonic-gate struct zone_nwiftab nwiftab; 25627c478bd9Sstevel@tonic-gate int return_code = Z_OK; 25637c478bd9Sstevel@tonic-gate int err; 2564108322fbScarlsonj boolean_t in_alt_root; 2565f4b3ec61Sdh155122 zone_iptype_t iptype; 2566948f2876Sss150715 dlpi_handle_t dh; 25677c478bd9Sstevel@tonic-gate 2568108322fbScarlsonj in_alt_root = zonecfg_in_alt_root(); 2569108322fbScarlsonj if (in_alt_root) 2570108322fbScarlsonj goto no_net; 2571108322fbScarlsonj 2572f4b3ec61Sdh155122 if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) { 2573f4b3ec61Sdh155122 errno = err; 2574f4b3ec61Sdh155122 zperror(cmd_to_str(cmd_num), B_TRUE); 2575f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 2576f4b3ec61Sdh155122 return (Z_ERR); 2577f4b3ec61Sdh155122 } 25787c478bd9Sstevel@tonic-gate if ((err = zonecfg_setnwifent(handle)) != Z_OK) { 25797c478bd9Sstevel@tonic-gate errno = err; 25807c478bd9Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 25817c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 25827c478bd9Sstevel@tonic-gate return (Z_ERR); 25837c478bd9Sstevel@tonic-gate } 25847c478bd9Sstevel@tonic-gate while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) { 25857c478bd9Sstevel@tonic-gate struct lifreq lifr; 25861390a385Sgjelinek sa_family_t af = AF_UNSPEC; 2587f4b3ec61Sdh155122 char dl_owner_zname[ZONENAME_MAX]; 2588f4b3ec61Sdh155122 zoneid_t dl_owner_zid; 2589f4b3ec61Sdh155122 zoneid_t target_zid; 2590f4b3ec61Sdh155122 int res; 25917c478bd9Sstevel@tonic-gate 25927c478bd9Sstevel@tonic-gate /* skip any loopback interfaces */ 25937c478bd9Sstevel@tonic-gate if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0) 25947c478bd9Sstevel@tonic-gate continue; 2595f4b3ec61Sdh155122 switch (iptype) { 2596f4b3ec61Sdh155122 case ZS_SHARED: 2597f4b3ec61Sdh155122 if ((res = zonecfg_valid_net_address( 2598f4b3ec61Sdh155122 nwiftab.zone_nwif_address, &lifr)) != Z_OK) { 25991390a385Sgjelinek print_net_err(nwiftab.zone_nwif_physical, 26001390a385Sgjelinek nwiftab.zone_nwif_address, af, 26011390a385Sgjelinek zonecfg_strerror(res)); 26027c478bd9Sstevel@tonic-gate return_code = Z_ERR; 26037c478bd9Sstevel@tonic-gate continue; 26047c478bd9Sstevel@tonic-gate } 26057c478bd9Sstevel@tonic-gate af = lifr.lifr_addr.ss_family; 2606f4b3ec61Sdh155122 if (!zonecfg_ifname_exists(af, 2607f4b3ec61Sdh155122 nwiftab.zone_nwif_physical)) { 260822321485Svp157776 /* 2609f4b3ec61Sdh155122 * The interface failed to come up. We continue 2610f4b3ec61Sdh155122 * on anyway for the sake of consistency: a 2611f4b3ec61Sdh155122 * zone is not shut down if the interface fails 2612f4b3ec61Sdh155122 * any time after boot, nor does the global zone 2613f4b3ec61Sdh155122 * fail to boot if an interface fails. 261422321485Svp157776 */ 261522321485Svp157776 (void) fprintf(stderr, 2616f4b3ec61Sdh155122 gettext("WARNING: skipping network " 2617f4b3ec61Sdh155122 "interface '%s' which may not be " 2618f4b3ec61Sdh155122 "present/plumbed in the global " 2619f4b3ec61Sdh155122 "zone.\n"), 262022321485Svp157776 nwiftab.zone_nwif_physical); 26217c478bd9Sstevel@tonic-gate } 2622f4b3ec61Sdh155122 break; 2623f4b3ec61Sdh155122 case ZS_EXCLUSIVE: 2624f4b3ec61Sdh155122 /* Warning if it exists for either IPv4 or IPv6 */ 2625f4b3ec61Sdh155122 2626f4b3ec61Sdh155122 if (zonecfg_ifname_exists(AF_INET, 2627f4b3ec61Sdh155122 nwiftab.zone_nwif_physical) || 2628f4b3ec61Sdh155122 zonecfg_ifname_exists(AF_INET6, 2629f4b3ec61Sdh155122 nwiftab.zone_nwif_physical)) { 2630f4b3ec61Sdh155122 (void) fprintf(stderr, 2631f4b3ec61Sdh155122 gettext("WARNING: skipping network " 2632f4b3ec61Sdh155122 "interface '%s' which is used in the " 2633f4b3ec61Sdh155122 "global zone.\n"), 2634f4b3ec61Sdh155122 nwiftab.zone_nwif_physical); 2635f4b3ec61Sdh155122 break; 2636f4b3ec61Sdh155122 } 2637948f2876Sss150715 2638f4b3ec61Sdh155122 /* 2639948f2876Sss150715 * Verify that the physical interface can be opened. 2640f4b3ec61Sdh155122 */ 2641948f2876Sss150715 err = dlpi_open(nwiftab.zone_nwif_physical, &dh, 0); 2642948f2876Sss150715 if (err != DLPI_SUCCESS) { 2643f4b3ec61Sdh155122 (void) fprintf(stderr, 2644f4b3ec61Sdh155122 gettext("WARNING: skipping network " 2645948f2876Sss150715 "interface '%s' which cannot be opened: " 2646948f2876Sss150715 "dlpi error (%s).\n"), 2647948f2876Sss150715 nwiftab.zone_nwif_physical, 2648948f2876Sss150715 dlpi_strerror(err)); 2649f4b3ec61Sdh155122 break; 2650f4b3ec61Sdh155122 } else { 2651948f2876Sss150715 dlpi_close(dh); 2652f4b3ec61Sdh155122 } 2653f4b3ec61Sdh155122 /* 2654f4b3ec61Sdh155122 * Verify whether the physical interface is already 2655f4b3ec61Sdh155122 * used by a zone. 2656f4b3ec61Sdh155122 */ 2657f4b3ec61Sdh155122 dl_owner_zid = ALL_ZONES; 2658f4b3ec61Sdh155122 if (zone_check_datalink(&dl_owner_zid, 2659f4b3ec61Sdh155122 nwiftab.zone_nwif_physical) != 0) 2660f4b3ec61Sdh155122 break; 2661f4b3ec61Sdh155122 2662f4b3ec61Sdh155122 /* 2663f4b3ec61Sdh155122 * If the zone being verified is 2664f4b3ec61Sdh155122 * running and owns the interface 2665f4b3ec61Sdh155122 */ 2666f4b3ec61Sdh155122 target_zid = getzoneidbyname(target_zone); 2667f4b3ec61Sdh155122 if (target_zid == dl_owner_zid) 2668f4b3ec61Sdh155122 break; 2669f4b3ec61Sdh155122 2670f4b3ec61Sdh155122 /* Zone id match failed, use name to check */ 2671f4b3ec61Sdh155122 if (getzonenamebyid(dl_owner_zid, dl_owner_zname, 2672f4b3ec61Sdh155122 ZONENAME_MAX) < 0) { 2673f4b3ec61Sdh155122 /* No name, show ID instead */ 2674f4b3ec61Sdh155122 (void) snprintf(dl_owner_zname, ZONENAME_MAX, 2675f4b3ec61Sdh155122 "<%d>", dl_owner_zid); 2676f4b3ec61Sdh155122 } else if (strcmp(dl_owner_zname, target_zone) == 0) 2677f4b3ec61Sdh155122 break; 2678f4b3ec61Sdh155122 2679f4b3ec61Sdh155122 /* 2680f4b3ec61Sdh155122 * Note here we only report a warning that 2681f4b3ec61Sdh155122 * the interface is already in use by another 2682f4b3ec61Sdh155122 * running zone, and the verify process just 2683f4b3ec61Sdh155122 * goes on, if the interface is still in use 2684f4b3ec61Sdh155122 * when this zone really boots up, zoneadmd 2685f4b3ec61Sdh155122 * will find it. If the name of the zone which 2686f4b3ec61Sdh155122 * owns this interface cannot be determined, 2687f4b3ec61Sdh155122 * then it is not possible to determine if there 2688f4b3ec61Sdh155122 * is a conflict so just report it as a warning. 2689f4b3ec61Sdh155122 */ 2690f4b3ec61Sdh155122 (void) fprintf(stderr, 2691f4b3ec61Sdh155122 gettext("WARNING: skipping network interface " 2692f4b3ec61Sdh155122 "'%s' which is used by the non-global zone " 2693f4b3ec61Sdh155122 "'%s'.\n"), nwiftab.zone_nwif_physical, 2694f4b3ec61Sdh155122 dl_owner_zname); 2695f4b3ec61Sdh155122 break; 2696f4b3ec61Sdh155122 } 26977c478bd9Sstevel@tonic-gate } 26987c478bd9Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 2699108322fbScarlsonj no_net: 27007c478bd9Sstevel@tonic-gate 2701e7f3c547Sgjelinek /* verify that lofs has not been excluded from the kernel */ 27028cd327d5Sgjelinek if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH || 27038cd327d5Sgjelinek cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) && 27048cd327d5Sgjelinek modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) { 2705e7f3c547Sgjelinek if (errno == ENXIO) 2706e7f3c547Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2707e7f3c547Sgjelinek "lofs(7FS): possibly excluded in /etc/system\n")); 2708e7f3c547Sgjelinek else 2709e7f3c547Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2710e7f3c547Sgjelinek "lofs(7FS): %s\n"), strerror(errno)); 2711e7f3c547Sgjelinek return_code = Z_ERR; 2712e7f3c547Sgjelinek } 2713e7f3c547Sgjelinek 27147c478bd9Sstevel@tonic-gate if (verify_filesystems(handle) != Z_OK) 27157c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2716b5abaf04Sgjelinek if (verify_ipd(handle) != Z_OK) 2717b5abaf04Sgjelinek return_code = Z_ERR; 2718108322fbScarlsonj if (!in_alt_root && verify_rctls(handle) != Z_OK) 27197c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2720108322fbScarlsonj if (!in_alt_root && verify_pool(handle) != Z_OK) 27217c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2722ce28b40eSzt129084 if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK) 27239acbbeafSnn35248 return_code = Z_ERR; 2724fa9e4066Sahrens if (!in_alt_root && verify_datasets(handle) != Z_OK) 2725fa9e4066Sahrens return_code = Z_ERR; 2726ffbafc53Scomay 2727ffbafc53Scomay /* 2728ffbafc53Scomay * As the "mount" command is used for patching/upgrading of zones 2729ffbafc53Scomay * or other maintenance processes, the zone's privilege set is not 2730ffbafc53Scomay * checked in this case. Instead, the default, safe set of 2731ffbafc53Scomay * privileges will be used when this zone is created in the 2732ffbafc53Scomay * kernel. 2733ffbafc53Scomay */ 2734ffbafc53Scomay if (!in_alt_root && cmd_num != CMD_MOUNT && 2735ffbafc53Scomay verify_limitpriv(handle) != Z_OK) 2736ffbafc53Scomay return_code = Z_ERR; 27378cd327d5Sgjelinek 27388cd327d5Sgjelinek return (return_code); 27398cd327d5Sgjelinek } 27408cd327d5Sgjelinek 27418cd327d5Sgjelinek static int 2742ce28b40eSzt129084 verify_details(int cmd_num, char *argv[]) 27438cd327d5Sgjelinek { 27448cd327d5Sgjelinek zone_dochandle_t handle; 27458cd327d5Sgjelinek char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN]; 27468cd327d5Sgjelinek int return_code = Z_OK; 27478cd327d5Sgjelinek int err; 27488cd327d5Sgjelinek 27498cd327d5Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 27508cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 27518cd327d5Sgjelinek return (Z_ERR); 27528cd327d5Sgjelinek } 27538cd327d5Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 27548cd327d5Sgjelinek errno = err; 27558cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 27568cd327d5Sgjelinek zonecfg_fini_handle(handle); 27578cd327d5Sgjelinek return (Z_ERR); 27588cd327d5Sgjelinek } 27598cd327d5Sgjelinek if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 27608cd327d5Sgjelinek Z_OK) { 27618cd327d5Sgjelinek errno = err; 27628cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 27638cd327d5Sgjelinek zonecfg_fini_handle(handle); 27648cd327d5Sgjelinek return (Z_ERR); 27658cd327d5Sgjelinek } 27668cd327d5Sgjelinek /* 27678cd327d5Sgjelinek * zonecfg_get_zonepath() gets its data from the XML repository. 27688cd327d5Sgjelinek * Verify this against the index file, which is checked first by 27698cd327d5Sgjelinek * zone_get_zonepath(). If they don't match, bail out. 27708cd327d5Sgjelinek */ 27718cd327d5Sgjelinek if ((err = zone_get_zonepath(target_zone, checkpath, 27728cd327d5Sgjelinek sizeof (checkpath))) != Z_OK) { 27738cd327d5Sgjelinek errno = err; 27748cd327d5Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 2775e767a340Sgjelinek zonecfg_fini_handle(handle); 27768cd327d5Sgjelinek return (Z_ERR); 27778cd327d5Sgjelinek } 27788cd327d5Sgjelinek if (strcmp(zonepath, checkpath) != 0) { 27798cd327d5Sgjelinek /* 27808cd327d5Sgjelinek * TRANSLATION_NOTE 27818cd327d5Sgjelinek * XML and zonepath are literals that should not be translated. 27828cd327d5Sgjelinek */ 27838cd327d5Sgjelinek (void) fprintf(stderr, gettext("The XML repository has " 27848cd327d5Sgjelinek "zonepath '%s',\nbut the index file has zonepath '%s'.\n" 27858cd327d5Sgjelinek "These must match, so fix the incorrect entry.\n"), 27868cd327d5Sgjelinek zonepath, checkpath); 2787e767a340Sgjelinek zonecfg_fini_handle(handle); 27888cd327d5Sgjelinek return (Z_ERR); 27898cd327d5Sgjelinek } 27908cd327d5Sgjelinek if (validate_zonepath(zonepath, cmd_num) != Z_OK) { 27918cd327d5Sgjelinek (void) fprintf(stderr, gettext("could not verify zonepath %s " 27928cd327d5Sgjelinek "because of the above errors.\n"), zonepath); 27938cd327d5Sgjelinek return_code = Z_ERR; 27948cd327d5Sgjelinek } 27958cd327d5Sgjelinek 2796ce28b40eSzt129084 if (verify_handle(cmd_num, handle, argv) != Z_OK) 27978cd327d5Sgjelinek return_code = Z_ERR; 27988cd327d5Sgjelinek 27997c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 28007c478bd9Sstevel@tonic-gate if (return_code == Z_ERR) 28017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 28027c478bd9Sstevel@tonic-gate gettext("%s: zone %s failed to verify\n"), 28037c478bd9Sstevel@tonic-gate execname, target_zone); 28047c478bd9Sstevel@tonic-gate return (return_code); 28057c478bd9Sstevel@tonic-gate } 28067c478bd9Sstevel@tonic-gate 28077c478bd9Sstevel@tonic-gate static int 28087c478bd9Sstevel@tonic-gate verify_func(int argc, char *argv[]) 28097c478bd9Sstevel@tonic-gate { 28107c478bd9Sstevel@tonic-gate int arg; 28117c478bd9Sstevel@tonic-gate 28127c478bd9Sstevel@tonic-gate optind = 0; 28137c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 28147c478bd9Sstevel@tonic-gate switch (arg) { 28157c478bd9Sstevel@tonic-gate case '?': 28167c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 28177c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 28187c478bd9Sstevel@tonic-gate default: 28197c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 28207c478bd9Sstevel@tonic-gate return (Z_USAGE); 28217c478bd9Sstevel@tonic-gate } 28227c478bd9Sstevel@tonic-gate } 28237c478bd9Sstevel@tonic-gate if (argc > optind) { 28247c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 28257c478bd9Sstevel@tonic-gate return (Z_USAGE); 28267c478bd9Sstevel@tonic-gate } 28279acbbeafSnn35248 if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE) 28289acbbeafSnn35248 != Z_OK) 28297c478bd9Sstevel@tonic-gate return (Z_ERR); 2830ce28b40eSzt129084 return (verify_details(CMD_VERIFY, argv)); 28317c478bd9Sstevel@tonic-gate } 28327c478bd9Sstevel@tonic-gate 28339acbbeafSnn35248 static int 2834ff17c8bfSgjelinek addoptions(char *buf, char *argv[], size_t len) 2835ff17c8bfSgjelinek { 2836ff17c8bfSgjelinek int i = 0; 2837ff17c8bfSgjelinek 2838ff17c8bfSgjelinek if (buf[0] == '\0') 2839ff17c8bfSgjelinek return (Z_OK); 2840ff17c8bfSgjelinek 2841ff17c8bfSgjelinek while (argv[i] != NULL) { 2842ff17c8bfSgjelinek if (strlcat(buf, " ", len) >= len || 2843ff17c8bfSgjelinek strlcat(buf, argv[i++], len) >= len) { 2844ff17c8bfSgjelinek zerror("Command line too long"); 2845ff17c8bfSgjelinek return (Z_ERR); 2846ff17c8bfSgjelinek } 2847ff17c8bfSgjelinek } 2848ff17c8bfSgjelinek 2849ff17c8bfSgjelinek return (Z_OK); 2850ff17c8bfSgjelinek } 2851ff17c8bfSgjelinek 2852ff17c8bfSgjelinek static int 28539acbbeafSnn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize) 28549acbbeafSnn35248 { 28559acbbeafSnn35248 char optstring[4]; 28569acbbeafSnn35248 28579acbbeafSnn35248 if (opt > 0) 28589acbbeafSnn35248 (void) sprintf(optstring, " -%c", opt); 28599acbbeafSnn35248 else 28609acbbeafSnn35248 (void) strcpy(optstring, " "); 28619acbbeafSnn35248 28629acbbeafSnn35248 if ((strlcat(buf, optstring, bufsize) > bufsize)) 28639acbbeafSnn35248 return (Z_ERR); 2864ff17c8bfSgjelinek 28659acbbeafSnn35248 if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize)) 28669acbbeafSnn35248 return (Z_ERR); 2867ff17c8bfSgjelinek 28689acbbeafSnn35248 return (Z_OK); 28699acbbeafSnn35248 } 28707c478bd9Sstevel@tonic-gate 2871ff17c8bfSgjelinek /* ARGSUSED */ 28727c478bd9Sstevel@tonic-gate static int 28737c478bd9Sstevel@tonic-gate install_func(int argc, char *argv[]) 28747c478bd9Sstevel@tonic-gate { 28759acbbeafSnn35248 char cmdbuf[MAXPATHLEN]; 28761100f00dSgjelinek char postcmdbuf[MAXPATHLEN]; 28777c478bd9Sstevel@tonic-gate int lockfd; 28789acbbeafSnn35248 int arg, err, subproc_err; 28797c478bd9Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 2880123807fbSedp brand_handle_t bh = NULL; 28817c478bd9Sstevel@tonic-gate int status; 28820b5de56dSgjelinek boolean_t nodataset = B_FALSE; 28831100f00dSgjelinek boolean_t do_postinstall = B_FALSE; 2884ff17c8bfSgjelinek boolean_t brand_help = B_FALSE; 28859acbbeafSnn35248 char opts[128]; 28869acbbeafSnn35248 28879acbbeafSnn35248 if (target_zone == NULL) { 28889acbbeafSnn35248 sub_usage(SHELP_INSTALL, CMD_INSTALL); 28899acbbeafSnn35248 return (Z_USAGE); 28909acbbeafSnn35248 } 28917c478bd9Sstevel@tonic-gate 2892108322fbScarlsonj if (zonecfg_in_alt_root()) { 2893108322fbScarlsonj zerror(gettext("cannot install zone in alternate root")); 2894108322fbScarlsonj return (Z_ERR); 2895108322fbScarlsonj } 2896108322fbScarlsonj 28979acbbeafSnn35248 if ((err = zone_get_zonepath(target_zone, zonepath, 28989acbbeafSnn35248 sizeof (zonepath))) != Z_OK) { 28999acbbeafSnn35248 errno = err; 29009acbbeafSnn35248 zperror2(target_zone, gettext("could not get zone path")); 29019acbbeafSnn35248 return (Z_ERR); 29029acbbeafSnn35248 } 29039acbbeafSnn35248 29049acbbeafSnn35248 /* Fetch the install command from the brand configuration. */ 2905123807fbSedp if ((bh = brand_open(target_brand)) == NULL) { 29069acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 29079acbbeafSnn35248 return (Z_ERR); 29089acbbeafSnn35248 } 29099acbbeafSnn35248 2910ff17c8bfSgjelinek if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_install, 2911ff17c8bfSgjelinek target_zone, zonepath) != Z_OK) { 29129acbbeafSnn35248 zerror("invalid brand configuration: missing install resource"); 2913123807fbSedp brand_close(bh); 29149acbbeafSnn35248 return (Z_ERR); 29159acbbeafSnn35248 } 29169acbbeafSnn35248 2917ff17c8bfSgjelinek if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postinstall, 2918ff17c8bfSgjelinek target_zone, zonepath) != Z_OK) { 29191100f00dSgjelinek zerror("invalid brand configuration: missing postinstall " 29201100f00dSgjelinek "resource"); 29211100f00dSgjelinek brand_close(bh); 29221100f00dSgjelinek return (Z_ERR); 29231100f00dSgjelinek } 29241100f00dSgjelinek 2925ff17c8bfSgjelinek if (postcmdbuf[0] != '\0') 2926ff17c8bfSgjelinek do_postinstall = B_TRUE; 2927ff17c8bfSgjelinek 29289acbbeafSnn35248 (void) strcpy(opts, "?x:"); 29299acbbeafSnn35248 /* 29309acbbeafSnn35248 * Fetch the list of recognized command-line options from 29319acbbeafSnn35248 * the brand configuration file. 29329acbbeafSnn35248 */ 2933123807fbSedp if (brand_get_installopts(bh, opts + strlen(opts), 29349acbbeafSnn35248 sizeof (opts) - strlen(opts)) != 0) { 29359acbbeafSnn35248 zerror("invalid brand configuration: missing " 29369acbbeafSnn35248 "install options resource"); 2937123807fbSedp brand_close(bh); 29389acbbeafSnn35248 return (Z_ERR); 29399acbbeafSnn35248 } 2940ff17c8bfSgjelinek 2941123807fbSedp brand_close(bh); 29429acbbeafSnn35248 2943ff17c8bfSgjelinek if (cmdbuf[0] == '\0') { 2944ff17c8bfSgjelinek zerror("Missing brand install command"); 2945ff17c8bfSgjelinek return (Z_ERR); 2946ff17c8bfSgjelinek } 2947ff17c8bfSgjelinek 2948ff17c8bfSgjelinek /* Check the argv string for args we handle internally */ 29497c478bd9Sstevel@tonic-gate optind = 0; 2950ff17c8bfSgjelinek opterr = 0; 29519acbbeafSnn35248 while ((arg = getopt(argc, argv, opts)) != EOF) { 29527c478bd9Sstevel@tonic-gate switch (arg) { 29537c478bd9Sstevel@tonic-gate case '?': 2954ff17c8bfSgjelinek if (optopt == '?') { 29557c478bd9Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 2956ff17c8bfSgjelinek brand_help = B_TRUE; 29570b5de56dSgjelinek } 2958ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 29590b5de56dSgjelinek break; 2960ff17c8bfSgjelinek case 'x': 2961ff17c8bfSgjelinek /* Handle this option internally, don't pass to brand */ 2962ff17c8bfSgjelinek if (strcmp(optarg, "nodataset") == 0) { 2963ff17c8bfSgjelinek /* Handle this option internally */ 2964ff17c8bfSgjelinek nodataset = B_TRUE; 2965ff17c8bfSgjelinek } 2966ff17c8bfSgjelinek continue; 29677c478bd9Sstevel@tonic-gate default: 2968ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 2969ff17c8bfSgjelinek break; 29707c478bd9Sstevel@tonic-gate } 29719acbbeafSnn35248 29729acbbeafSnn35248 /* 2973ff17c8bfSgjelinek * Append the option to the command line passed to the 2974ff17c8bfSgjelinek * brand-specific install and postinstall routines. 29759acbbeafSnn35248 */ 2976ff17c8bfSgjelinek if (addopt(cmdbuf, optopt, optarg, sizeof (cmdbuf)) != Z_OK) { 29779acbbeafSnn35248 zerror("Install command line too long"); 29789acbbeafSnn35248 return (Z_ERR); 29797c478bd9Sstevel@tonic-gate } 2980ff17c8bfSgjelinek if (addopt(postcmdbuf, optopt, optarg, sizeof (postcmdbuf)) 2981ff17c8bfSgjelinek != Z_OK) { 29821100f00dSgjelinek zerror("Post-Install command line too long"); 29831100f00dSgjelinek return (Z_ERR); 29841100f00dSgjelinek } 29859acbbeafSnn35248 } 29869acbbeafSnn35248 29879acbbeafSnn35248 for (; optind < argc; optind++) { 2988ff17c8bfSgjelinek if (addopt(cmdbuf, 0, argv[optind], sizeof (cmdbuf)) != Z_OK) { 29899acbbeafSnn35248 zerror("Install command line too long"); 29909acbbeafSnn35248 return (Z_ERR); 29919acbbeafSnn35248 } 2992ff17c8bfSgjelinek 2993ff17c8bfSgjelinek if (addopt(postcmdbuf, 0, argv[optind], sizeof (postcmdbuf)) 2994ff17c8bfSgjelinek != Z_OK) { 29951100f00dSgjelinek zerror("Post-Install command line too long"); 29961100f00dSgjelinek return (Z_ERR); 29971100f00dSgjelinek } 29989acbbeafSnn35248 } 29999acbbeafSnn35248 3000ff17c8bfSgjelinek if (!brand_help) { 3001ff17c8bfSgjelinek if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE, 3002ff17c8bfSgjelinek B_FALSE) != Z_OK) 30037c478bd9Sstevel@tonic-gate return (Z_ERR); 3004ce28b40eSzt129084 if (verify_details(CMD_INSTALL, argv) != Z_OK) 30057c478bd9Sstevel@tonic-gate return (Z_ERR); 30067c478bd9Sstevel@tonic-gate 3007ff17c8bfSgjelinek if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) { 3008ff17c8bfSgjelinek zerror(gettext("another %s may have an operation in " 3009ff17c8bfSgjelinek "progress."), "zoneadm"); 30107c478bd9Sstevel@tonic-gate return (Z_ERR); 30117c478bd9Sstevel@tonic-gate } 30127c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 30137c478bd9Sstevel@tonic-gate if (err != Z_OK) { 30147c478bd9Sstevel@tonic-gate errno = err; 30157c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 30167c478bd9Sstevel@tonic-gate goto done; 30177c478bd9Sstevel@tonic-gate } 30187c478bd9Sstevel@tonic-gate 30199acbbeafSnn35248 if (!nodataset) 30209acbbeafSnn35248 create_zfs_zonepath(zonepath); 30217c478bd9Sstevel@tonic-gate } 30227c478bd9Sstevel@tonic-gate 30239acbbeafSnn35248 status = do_subproc_interactive(cmdbuf); 30249acbbeafSnn35248 if ((subproc_err = 30259acbbeafSnn35248 subproc_status(gettext("brand-specific installation"), status, 30269acbbeafSnn35248 B_FALSE)) != ZONE_SUBPROC_OK) { 3027ff17c8bfSgjelinek if (subproc_err == ZONE_SUBPROC_USAGE && !brand_help) { 3028ff17c8bfSgjelinek sub_usage(SHELP_INSTALL, CMD_INSTALL); 3029ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 3030ff17c8bfSgjelinek return (Z_ERR); 3031ff17c8bfSgjelinek } 30329acbbeafSnn35248 err = Z_ERR; 30337c478bd9Sstevel@tonic-gate goto done; 30349acbbeafSnn35248 } 30357c478bd9Sstevel@tonic-gate 3036ff17c8bfSgjelinek if (brand_help) 3037ff17c8bfSgjelinek return (Z_OK); 3038ff17c8bfSgjelinek 30397c478bd9Sstevel@tonic-gate if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 30407c478bd9Sstevel@tonic-gate errno = err; 30417c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 30427c478bd9Sstevel@tonic-gate goto done; 30437c478bd9Sstevel@tonic-gate } 30447c478bd9Sstevel@tonic-gate 30451100f00dSgjelinek if (do_postinstall) { 30461100f00dSgjelinek status = do_subproc(postcmdbuf); 30471100f00dSgjelinek 30481100f00dSgjelinek if ((subproc_err = 30491100f00dSgjelinek subproc_status(gettext("brand-specific post-install"), 30501100f00dSgjelinek status, B_FALSE)) != ZONE_SUBPROC_OK) { 30511100f00dSgjelinek err = Z_ERR; 30521100f00dSgjelinek (void) zone_set_state(target_zone, 30531100f00dSgjelinek ZONE_STATE_INCOMPLETE); 30541100f00dSgjelinek } 30551100f00dSgjelinek } 30561100f00dSgjelinek 30577c478bd9Sstevel@tonic-gate done: 30589acbbeafSnn35248 /* 3059ff17c8bfSgjelinek * If the install script exited with ZONE_SUBPROC_NOTCOMPLETE, try to 3060ff17c8bfSgjelinek * clean up the zone and leave the zone in the CONFIGURED state so that 3061ff17c8bfSgjelinek * another install can be attempted without requiring an uninstall 3062ff17c8bfSgjelinek * first. 30639acbbeafSnn35248 */ 3064ff17c8bfSgjelinek if (subproc_err == ZONE_SUBPROC_NOTCOMPLETE) { 30659acbbeafSnn35248 if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 30669acbbeafSnn35248 errno = err; 30679acbbeafSnn35248 zperror2(target_zone, 30689acbbeafSnn35248 gettext("cleaning up zonepath failed")); 30699acbbeafSnn35248 } else if ((err = zone_set_state(target_zone, 30709acbbeafSnn35248 ZONE_STATE_CONFIGURED)) != Z_OK) { 30719acbbeafSnn35248 errno = err; 30729acbbeafSnn35248 zperror2(target_zone, gettext("could not set state")); 30739acbbeafSnn35248 } 30749acbbeafSnn35248 } 30759acbbeafSnn35248 3076ff17c8bfSgjelinek if (!brand_help) 3077ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 30787c478bd9Sstevel@tonic-gate return ((err == Z_OK) ? Z_OK : Z_ERR); 30797c478bd9Sstevel@tonic-gate } 30807c478bd9Sstevel@tonic-gate 30817c478bd9Sstevel@tonic-gate /* 3082865e09a4Sgjelinek * Check that the inherited pkg dirs are the same for the clone and its source. 3083865e09a4Sgjelinek * The easiest way to do that is check that the list of ipds is the same 3084865e09a4Sgjelinek * by matching each one against the other. This algorithm should be fine since 3085865e09a4Sgjelinek * the list of ipds should not be that long. 3086865e09a4Sgjelinek */ 3087865e09a4Sgjelinek static int 3088865e09a4Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone, 3089865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3090865e09a4Sgjelinek { 3091865e09a4Sgjelinek int err; 3092865e09a4Sgjelinek int res = Z_OK; 3093865e09a4Sgjelinek int s_cnt = 0; 3094865e09a4Sgjelinek int t_cnt = 0; 3095865e09a4Sgjelinek struct zone_fstab s_fstab; 3096865e09a4Sgjelinek struct zone_fstab t_fstab; 3097865e09a4Sgjelinek 3098865e09a4Sgjelinek /* 3099865e09a4Sgjelinek * First check the source of the clone against the target. 3100865e09a4Sgjelinek */ 3101865e09a4Sgjelinek if ((err = zonecfg_setipdent(s_handle)) != Z_OK) { 3102865e09a4Sgjelinek errno = err; 3103865e09a4Sgjelinek zperror2(source_zone, gettext("could not enumerate " 3104865e09a4Sgjelinek "inherit-pkg-dirs")); 3105865e09a4Sgjelinek return (Z_ERR); 3106865e09a4Sgjelinek } 3107865e09a4Sgjelinek 3108865e09a4Sgjelinek while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) { 3109865e09a4Sgjelinek boolean_t match = B_FALSE; 3110865e09a4Sgjelinek 3111865e09a4Sgjelinek s_cnt++; 3112865e09a4Sgjelinek 3113865e09a4Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 3114865e09a4Sgjelinek errno = err; 3115865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate " 3116865e09a4Sgjelinek "inherit-pkg-dirs")); 3117865e09a4Sgjelinek (void) zonecfg_endipdent(s_handle); 3118865e09a4Sgjelinek return (Z_ERR); 3119865e09a4Sgjelinek } 3120865e09a4Sgjelinek 3121865e09a4Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) { 3122865e09a4Sgjelinek if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir) 3123865e09a4Sgjelinek == 0) { 3124865e09a4Sgjelinek match = B_TRUE; 3125865e09a4Sgjelinek break; 3126865e09a4Sgjelinek } 3127865e09a4Sgjelinek } 3128865e09a4Sgjelinek (void) zonecfg_endipdent(t_handle); 3129865e09a4Sgjelinek 3130865e09a4Sgjelinek if (!match) { 3131865e09a4Sgjelinek (void) fprintf(stderr, gettext("inherit-pkg-dir " 3132865e09a4Sgjelinek "'%s' is not configured in zone %s.\n"), 3133865e09a4Sgjelinek s_fstab.zone_fs_dir, target_zone); 3134865e09a4Sgjelinek res = Z_ERR; 3135865e09a4Sgjelinek } 3136865e09a4Sgjelinek } 3137865e09a4Sgjelinek 3138865e09a4Sgjelinek (void) zonecfg_endipdent(s_handle); 3139865e09a4Sgjelinek 3140865e09a4Sgjelinek /* skip the next check if we already have errors */ 3141865e09a4Sgjelinek if (res == Z_ERR) 3142865e09a4Sgjelinek return (res); 3143865e09a4Sgjelinek 3144865e09a4Sgjelinek /* 3145865e09a4Sgjelinek * Now check the number of ipds in the target so we can verify 3146865e09a4Sgjelinek * that the source is not a subset of the target. 3147865e09a4Sgjelinek */ 3148865e09a4Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 3149865e09a4Sgjelinek errno = err; 3150865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate " 3151865e09a4Sgjelinek "inherit-pkg-dirs")); 3152865e09a4Sgjelinek return (Z_ERR); 3153865e09a4Sgjelinek } 3154865e09a4Sgjelinek 3155865e09a4Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) 3156865e09a4Sgjelinek t_cnt++; 3157865e09a4Sgjelinek 3158865e09a4Sgjelinek (void) zonecfg_endipdent(t_handle); 3159865e09a4Sgjelinek 3160865e09a4Sgjelinek if (t_cnt != s_cnt) { 3161865e09a4Sgjelinek (void) fprintf(stderr, gettext("Zone %s is configured " 3162865e09a4Sgjelinek "with inherit-pkg-dirs that are not configured in zone " 3163865e09a4Sgjelinek "%s.\n"), target_zone, source_zone); 3164865e09a4Sgjelinek res = Z_ERR; 3165865e09a4Sgjelinek } 3166865e09a4Sgjelinek 3167865e09a4Sgjelinek return (res); 3168865e09a4Sgjelinek } 3169865e09a4Sgjelinek 3170865e09a4Sgjelinek static void 3171865e09a4Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone, 3172865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3173865e09a4Sgjelinek { 3174865e09a4Sgjelinek int err; 3175865e09a4Sgjelinek struct zone_devtab s_devtab; 3176865e09a4Sgjelinek struct zone_devtab t_devtab; 3177865e09a4Sgjelinek 3178865e09a4Sgjelinek if ((err = zonecfg_setdevent(t_handle)) != Z_OK) { 3179865e09a4Sgjelinek errno = err; 3180865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate devices")); 3181865e09a4Sgjelinek return; 3182865e09a4Sgjelinek } 3183865e09a4Sgjelinek 3184865e09a4Sgjelinek while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) { 3185865e09a4Sgjelinek if ((err = zonecfg_setdevent(s_handle)) != Z_OK) { 3186865e09a4Sgjelinek errno = err; 3187865e09a4Sgjelinek zperror2(source_zone, 3188865e09a4Sgjelinek gettext("could not enumerate devices")); 3189865e09a4Sgjelinek (void) zonecfg_enddevent(t_handle); 3190865e09a4Sgjelinek return; 3191865e09a4Sgjelinek } 3192865e09a4Sgjelinek 3193865e09a4Sgjelinek while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) { 3194865e09a4Sgjelinek /* 3195865e09a4Sgjelinek * Use fnmatch to catch the case where wildcards 3196865e09a4Sgjelinek * were used in one zone and the other has an 3197865e09a4Sgjelinek * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs. 3198865e09a4Sgjelinek * /dev/\*dsk/c0t0d0s6). 3199865e09a4Sgjelinek */ 3200865e09a4Sgjelinek if (fnmatch(t_devtab.zone_dev_match, 3201865e09a4Sgjelinek s_devtab.zone_dev_match, FNM_PATHNAME) == 0 || 3202865e09a4Sgjelinek fnmatch(s_devtab.zone_dev_match, 3203865e09a4Sgjelinek t_devtab.zone_dev_match, FNM_PATHNAME) == 0) { 3204865e09a4Sgjelinek (void) fprintf(stderr, 3205865e09a4Sgjelinek gettext("WARNING: device '%s' " 3206865e09a4Sgjelinek "is configured in both zones.\n"), 3207865e09a4Sgjelinek t_devtab.zone_dev_match); 3208865e09a4Sgjelinek break; 3209865e09a4Sgjelinek } 3210865e09a4Sgjelinek } 3211865e09a4Sgjelinek (void) zonecfg_enddevent(s_handle); 3212865e09a4Sgjelinek } 3213865e09a4Sgjelinek 3214865e09a4Sgjelinek (void) zonecfg_enddevent(t_handle); 3215865e09a4Sgjelinek } 3216865e09a4Sgjelinek 3217865e09a4Sgjelinek /* 3218865e09a4Sgjelinek * Check if the specified mount option (opt) is contained within the 3219865e09a4Sgjelinek * options string. 3220865e09a4Sgjelinek */ 3221865e09a4Sgjelinek static boolean_t 3222865e09a4Sgjelinek opt_match(char *opt, char *options) 3223865e09a4Sgjelinek { 3224865e09a4Sgjelinek char *p; 3225865e09a4Sgjelinek char *lastp; 3226865e09a4Sgjelinek 3227865e09a4Sgjelinek if ((p = strtok_r(options, ",", &lastp)) != NULL) { 3228865e09a4Sgjelinek if (strcmp(p, opt) == 0) 3229865e09a4Sgjelinek return (B_TRUE); 3230865e09a4Sgjelinek while ((p = strtok_r(NULL, ",", &lastp)) != NULL) { 3231865e09a4Sgjelinek if (strcmp(p, opt) == 0) 3232865e09a4Sgjelinek return (B_TRUE); 3233865e09a4Sgjelinek } 3234865e09a4Sgjelinek } 3235865e09a4Sgjelinek 3236865e09a4Sgjelinek return (B_FALSE); 3237865e09a4Sgjelinek } 3238865e09a4Sgjelinek 32390b5de56dSgjelinek #define RW_LOFS "WARNING: read-write lofs file system on '%s' is configured " \ 3240865e09a4Sgjelinek "in both zones.\n" 3241865e09a4Sgjelinek 3242865e09a4Sgjelinek static void 3243865e09a4Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab) 3244865e09a4Sgjelinek { 3245865e09a4Sgjelinek /* 3246865e09a4Sgjelinek * It is ok to have shared lofs mounted fs but we want to warn if 3247865e09a4Sgjelinek * either is rw since this will effect the other zone. 3248865e09a4Sgjelinek */ 3249865e09a4Sgjelinek if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) { 3250865e09a4Sgjelinek zone_fsopt_t *optp; 3251865e09a4Sgjelinek 3252865e09a4Sgjelinek /* The default is rw so no options means rw */ 3253865e09a4Sgjelinek if (t_fstab->zone_fs_options == NULL || 3254865e09a4Sgjelinek s_fstab->zone_fs_options == NULL) { 3255865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 3256865e09a4Sgjelinek t_fstab->zone_fs_special); 3257865e09a4Sgjelinek return; 3258865e09a4Sgjelinek } 3259865e09a4Sgjelinek 3260865e09a4Sgjelinek for (optp = s_fstab->zone_fs_options; optp != NULL; 3261865e09a4Sgjelinek optp = optp->zone_fsopt_next) { 3262865e09a4Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 3263865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 3264865e09a4Sgjelinek s_fstab->zone_fs_special); 3265865e09a4Sgjelinek return; 3266865e09a4Sgjelinek } 3267865e09a4Sgjelinek } 3268865e09a4Sgjelinek 3269865e09a4Sgjelinek for (optp = t_fstab->zone_fs_options; optp != NULL; 3270865e09a4Sgjelinek optp = optp->zone_fsopt_next) { 3271865e09a4Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 3272865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 3273865e09a4Sgjelinek t_fstab->zone_fs_special); 3274865e09a4Sgjelinek return; 3275865e09a4Sgjelinek } 3276865e09a4Sgjelinek } 3277865e09a4Sgjelinek 3278865e09a4Sgjelinek return; 3279865e09a4Sgjelinek } 3280865e09a4Sgjelinek 3281865e09a4Sgjelinek /* 3282865e09a4Sgjelinek * TRANSLATION_NOTE 32830b5de56dSgjelinek * The first variable is the file system type and the second is 32840b5de56dSgjelinek * the file system special device. For example, 32850b5de56dSgjelinek * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ... 3286865e09a4Sgjelinek */ 32870b5de56dSgjelinek (void) fprintf(stderr, gettext("WARNING: %s file system on '%s' " 3288865e09a4Sgjelinek "is configured in both zones.\n"), t_fstab->zone_fs_type, 3289865e09a4Sgjelinek t_fstab->zone_fs_special); 3290865e09a4Sgjelinek } 3291865e09a4Sgjelinek 3292865e09a4Sgjelinek static void 3293865e09a4Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone, 3294865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3295865e09a4Sgjelinek { 3296865e09a4Sgjelinek int err; 3297865e09a4Sgjelinek struct zone_fstab s_fstab; 3298865e09a4Sgjelinek struct zone_fstab t_fstab; 3299865e09a4Sgjelinek 3300865e09a4Sgjelinek if ((err = zonecfg_setfsent(t_handle)) != Z_OK) { 3301865e09a4Sgjelinek errno = err; 3302865e09a4Sgjelinek zperror2(target_zone, 33030b5de56dSgjelinek gettext("could not enumerate file systems")); 3304865e09a4Sgjelinek return; 3305865e09a4Sgjelinek } 3306865e09a4Sgjelinek 3307865e09a4Sgjelinek while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) { 3308865e09a4Sgjelinek if ((err = zonecfg_setfsent(s_handle)) != Z_OK) { 3309865e09a4Sgjelinek errno = err; 3310865e09a4Sgjelinek zperror2(source_zone, 33110b5de56dSgjelinek gettext("could not enumerate file systems")); 3312865e09a4Sgjelinek (void) zonecfg_endfsent(t_handle); 3313865e09a4Sgjelinek return; 3314865e09a4Sgjelinek } 3315865e09a4Sgjelinek 3316865e09a4Sgjelinek while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) { 3317865e09a4Sgjelinek if (strcmp(t_fstab.zone_fs_special, 3318865e09a4Sgjelinek s_fstab.zone_fs_special) == 0) { 3319865e09a4Sgjelinek print_fs_warnings(&s_fstab, &t_fstab); 3320865e09a4Sgjelinek break; 3321865e09a4Sgjelinek } 3322865e09a4Sgjelinek } 3323865e09a4Sgjelinek (void) zonecfg_endfsent(s_handle); 3324865e09a4Sgjelinek } 3325865e09a4Sgjelinek 3326865e09a4Sgjelinek (void) zonecfg_endfsent(t_handle); 3327865e09a4Sgjelinek } 3328865e09a4Sgjelinek 3329865e09a4Sgjelinek /* 3330865e09a4Sgjelinek * We don't catch the case where you used the same IP address but 3331865e09a4Sgjelinek * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128. 3332865e09a4Sgjelinek * However, we're not going to worry about that but we will check for 3333865e09a4Sgjelinek * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24) 3334865e09a4Sgjelinek * and handle that case as a match. 3335865e09a4Sgjelinek */ 3336865e09a4Sgjelinek static void 3337865e09a4Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone, 3338865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3339865e09a4Sgjelinek { 3340865e09a4Sgjelinek int err; 3341865e09a4Sgjelinek struct zone_nwiftab s_nwiftab; 3342865e09a4Sgjelinek struct zone_nwiftab t_nwiftab; 3343865e09a4Sgjelinek 3344865e09a4Sgjelinek if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) { 3345865e09a4Sgjelinek errno = err; 3346865e09a4Sgjelinek zperror2(target_zone, 3347865e09a4Sgjelinek gettext("could not enumerate network interfaces")); 3348865e09a4Sgjelinek return; 3349865e09a4Sgjelinek } 3350865e09a4Sgjelinek 3351865e09a4Sgjelinek while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) { 3352865e09a4Sgjelinek char *p; 3353865e09a4Sgjelinek 3354865e09a4Sgjelinek /* remove an (optional) netmask from the address */ 3355865e09a4Sgjelinek if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL) 3356865e09a4Sgjelinek *p = '\0'; 3357865e09a4Sgjelinek 3358865e09a4Sgjelinek if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) { 3359865e09a4Sgjelinek errno = err; 3360865e09a4Sgjelinek zperror2(source_zone, 3361865e09a4Sgjelinek gettext("could not enumerate network interfaces")); 3362865e09a4Sgjelinek (void) zonecfg_endnwifent(t_handle); 3363865e09a4Sgjelinek return; 3364865e09a4Sgjelinek } 3365865e09a4Sgjelinek 3366865e09a4Sgjelinek while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) { 3367865e09a4Sgjelinek /* remove an (optional) netmask from the address */ 3368865e09a4Sgjelinek if ((p = strchr(s_nwiftab.zone_nwif_address, '/')) 3369865e09a4Sgjelinek != NULL) 3370865e09a4Sgjelinek *p = '\0'; 3371865e09a4Sgjelinek 3372f4b3ec61Sdh155122 /* For exclusive-IP zones, address is not specified. */ 3373f4b3ec61Sdh155122 if (strlen(s_nwiftab.zone_nwif_address) == 0) 3374f4b3ec61Sdh155122 continue; 3375f4b3ec61Sdh155122 3376865e09a4Sgjelinek if (strcmp(t_nwiftab.zone_nwif_address, 3377865e09a4Sgjelinek s_nwiftab.zone_nwif_address) == 0) { 3378865e09a4Sgjelinek (void) fprintf(stderr, 3379865e09a4Sgjelinek gettext("WARNING: network address '%s' " 3380865e09a4Sgjelinek "is configured in both zones.\n"), 3381865e09a4Sgjelinek t_nwiftab.zone_nwif_address); 3382865e09a4Sgjelinek break; 3383865e09a4Sgjelinek } 3384865e09a4Sgjelinek } 3385865e09a4Sgjelinek (void) zonecfg_endnwifent(s_handle); 3386865e09a4Sgjelinek } 3387865e09a4Sgjelinek 3388865e09a4Sgjelinek (void) zonecfg_endnwifent(t_handle); 3389865e09a4Sgjelinek } 3390865e09a4Sgjelinek 3391865e09a4Sgjelinek static void 33929acbbeafSnn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source, 33939acbbeafSnn35248 zone_dochandle_t t_handle, char *target) 3394865e09a4Sgjelinek { 3395865e09a4Sgjelinek int err; 3396865e09a4Sgjelinek struct zone_dstab s_dstab; 3397865e09a4Sgjelinek struct zone_dstab t_dstab; 3398865e09a4Sgjelinek 3399865e09a4Sgjelinek if ((err = zonecfg_setdsent(t_handle)) != Z_OK) { 3400865e09a4Sgjelinek errno = err; 34019acbbeafSnn35248 zperror2(target, gettext("could not enumerate datasets")); 3402865e09a4Sgjelinek return; 3403865e09a4Sgjelinek } 3404865e09a4Sgjelinek 3405865e09a4Sgjelinek while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) { 3406865e09a4Sgjelinek if ((err = zonecfg_setdsent(s_handle)) != Z_OK) { 3407865e09a4Sgjelinek errno = err; 34089acbbeafSnn35248 zperror2(source, 3409865e09a4Sgjelinek gettext("could not enumerate datasets")); 3410865e09a4Sgjelinek (void) zonecfg_enddsent(t_handle); 3411865e09a4Sgjelinek return; 3412865e09a4Sgjelinek } 3413865e09a4Sgjelinek 3414865e09a4Sgjelinek while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) { 3415865e09a4Sgjelinek if (strcmp(t_dstab.zone_dataset_name, 3416865e09a4Sgjelinek s_dstab.zone_dataset_name) == 0) { 34179acbbeafSnn35248 target_zone = source; 34189acbbeafSnn35248 zerror(gettext("WARNING: dataset '%s' " 3419865e09a4Sgjelinek "is configured in both zones.\n"), 3420865e09a4Sgjelinek t_dstab.zone_dataset_name); 3421865e09a4Sgjelinek break; 3422865e09a4Sgjelinek } 3423865e09a4Sgjelinek } 3424865e09a4Sgjelinek (void) zonecfg_enddsent(s_handle); 3425865e09a4Sgjelinek } 3426865e09a4Sgjelinek 3427865e09a4Sgjelinek (void) zonecfg_enddsent(t_handle); 3428865e09a4Sgjelinek } 3429865e09a4Sgjelinek 34309acbbeafSnn35248 /* 34319acbbeafSnn35248 * Check that the clone and its source have the same brand type. 34329acbbeafSnn35248 */ 34339acbbeafSnn35248 static int 34349acbbeafSnn35248 valid_brand_clone(char *source_zone, char *target_zone) 34359acbbeafSnn35248 { 3436123807fbSedp brand_handle_t bh; 34379acbbeafSnn35248 char source_brand[MAXNAMELEN]; 34389acbbeafSnn35248 34399acbbeafSnn35248 if ((zone_get_brand(source_zone, source_brand, 34409acbbeafSnn35248 sizeof (source_brand))) != Z_OK) { 34419acbbeafSnn35248 (void) fprintf(stderr, "%s: zone '%s': %s\n", 34429acbbeafSnn35248 execname, source_zone, gettext("missing or invalid brand")); 34439acbbeafSnn35248 return (Z_ERR); 34449acbbeafSnn35248 } 34459acbbeafSnn35248 34469acbbeafSnn35248 if (strcmp(source_brand, target_brand) != NULL) { 34479acbbeafSnn35248 (void) fprintf(stderr, 34489acbbeafSnn35248 gettext("%s: Zones '%s' and '%s' have different brand " 34499acbbeafSnn35248 "types.\n"), execname, source_zone, target_zone); 34509acbbeafSnn35248 return (Z_ERR); 34519acbbeafSnn35248 } 34529acbbeafSnn35248 3453123807fbSedp if ((bh = brand_open(target_brand)) == NULL) { 34549acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 34559acbbeafSnn35248 return (Z_ERR); 34569acbbeafSnn35248 } 3457123807fbSedp brand_close(bh); 34589acbbeafSnn35248 return (Z_OK); 34599acbbeafSnn35248 } 34609acbbeafSnn35248 3461865e09a4Sgjelinek static int 3462865e09a4Sgjelinek validate_clone(char *source_zone, char *target_zone) 3463865e09a4Sgjelinek { 3464865e09a4Sgjelinek int err = Z_OK; 3465865e09a4Sgjelinek zone_dochandle_t s_handle; 3466865e09a4Sgjelinek zone_dochandle_t t_handle; 3467865e09a4Sgjelinek 3468865e09a4Sgjelinek if ((t_handle = zonecfg_init_handle()) == NULL) { 3469865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3470865e09a4Sgjelinek return (Z_ERR); 3471865e09a4Sgjelinek } 3472865e09a4Sgjelinek if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) { 3473865e09a4Sgjelinek errno = err; 3474865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3475865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3476865e09a4Sgjelinek return (Z_ERR); 3477865e09a4Sgjelinek } 3478865e09a4Sgjelinek 3479865e09a4Sgjelinek if ((s_handle = zonecfg_init_handle()) == NULL) { 3480865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3481865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3482865e09a4Sgjelinek return (Z_ERR); 3483865e09a4Sgjelinek } 3484865e09a4Sgjelinek if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) { 3485865e09a4Sgjelinek errno = err; 3486865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3487865e09a4Sgjelinek goto done; 3488865e09a4Sgjelinek } 3489865e09a4Sgjelinek 34909acbbeafSnn35248 /* verify new zone has same brand type */ 34919acbbeafSnn35248 err = valid_brand_clone(source_zone, target_zone); 34929acbbeafSnn35248 if (err != Z_OK) 34939acbbeafSnn35248 goto done; 34949acbbeafSnn35248 3495865e09a4Sgjelinek /* verify new zone has same inherit-pkg-dirs */ 3496865e09a4Sgjelinek err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone); 3497865e09a4Sgjelinek 3498865e09a4Sgjelinek /* warn about imported fs's which are the same */ 3499865e09a4Sgjelinek warn_fs_match(s_handle, source_zone, t_handle, target_zone); 3500865e09a4Sgjelinek 3501865e09a4Sgjelinek /* warn about imported IP addresses which are the same */ 3502865e09a4Sgjelinek warn_ip_match(s_handle, source_zone, t_handle, target_zone); 3503865e09a4Sgjelinek 3504865e09a4Sgjelinek /* warn about imported devices which are the same */ 3505865e09a4Sgjelinek warn_dev_match(s_handle, source_zone, t_handle, target_zone); 3506865e09a4Sgjelinek 3507865e09a4Sgjelinek /* warn about imported datasets which are the same */ 3508865e09a4Sgjelinek warn_dataset_match(s_handle, source_zone, t_handle, target_zone); 3509865e09a4Sgjelinek 3510865e09a4Sgjelinek done: 3511865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3512865e09a4Sgjelinek zonecfg_fini_handle(s_handle); 3513865e09a4Sgjelinek 3514865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3515865e09a4Sgjelinek } 3516865e09a4Sgjelinek 3517865e09a4Sgjelinek static int 3518865e09a4Sgjelinek copy_zone(char *src, char *dst) 3519865e09a4Sgjelinek { 3520865e09a4Sgjelinek boolean_t out_null = B_FALSE; 3521865e09a4Sgjelinek int status; 3522865e09a4Sgjelinek char *outfile; 3523865e09a4Sgjelinek char cmdbuf[MAXPATHLEN * 2 + 128]; 3524865e09a4Sgjelinek 3525865e09a4Sgjelinek if ((outfile = tempnam("/var/log", "zone")) == NULL) { 3526865e09a4Sgjelinek outfile = "/dev/null"; 3527865e09a4Sgjelinek out_null = B_TRUE; 3528865e09a4Sgjelinek } 3529865e09a4Sgjelinek 35300b5de56dSgjelinek /* 35310b5de56dSgjelinek * Use find to get the list of files to copy. We need to skip 35320b5de56dSgjelinek * files of type "socket" since cpio can't handle those but that 35330b5de56dSgjelinek * should be ok since the app will recreate the socket when it runs. 35340b5de56dSgjelinek * We also need to filter out anything under the .zfs subdir. Since 35350b5de56dSgjelinek * find is running depth-first, we need the extra egrep to filter .zfs. 35360b5de56dSgjelinek */ 3537865e09a4Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 35380b5de56dSgjelinek "cd %s && /usr/bin/find . -type s -prune -o -depth -print | " 353907b574eeSgjelinek "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | " 3540865e09a4Sgjelinek "/usr/bin/cpio -pdmuP@ %s > %s 2>&1", 3541865e09a4Sgjelinek src, dst, outfile); 3542865e09a4Sgjelinek 3543865e09a4Sgjelinek status = do_subproc(cmdbuf); 3544865e09a4Sgjelinek 35459acbbeafSnn35248 if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) { 3546865e09a4Sgjelinek if (!out_null) 3547865e09a4Sgjelinek (void) fprintf(stderr, gettext("\nThe copy failed.\n" 3548865e09a4Sgjelinek "More information can be found in %s\n"), outfile); 35499acbbeafSnn35248 return (Z_ERR); 3550865e09a4Sgjelinek } 3551865e09a4Sgjelinek 3552865e09a4Sgjelinek if (!out_null) 3553865e09a4Sgjelinek (void) unlink(outfile); 3554865e09a4Sgjelinek 3555865e09a4Sgjelinek return (Z_OK); 3556865e09a4Sgjelinek } 3557865e09a4Sgjelinek 3558865e09a4Sgjelinek /* ARGSUSED */ 35590b5de56dSgjelinek static int 3560865e09a4Sgjelinek zfm_print(const char *p, void *r) { 3561865e09a4Sgjelinek zerror(" %s\n", p); 3562865e09a4Sgjelinek return (0); 3563865e09a4Sgjelinek } 3564865e09a4Sgjelinek 35650b5de56dSgjelinek int 35660b5de56dSgjelinek clone_copy(char *source_zonepath, char *zonepath) 35670b5de56dSgjelinek { 35680b5de56dSgjelinek int err; 35690b5de56dSgjelinek 35700b5de56dSgjelinek /* Don't clone the zone if anything is still mounted there */ 35710b5de56dSgjelinek if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) { 35720b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 35730b5de56dSgjelinek "subdirectories of %s.\n"), source_zonepath); 35740b5de56dSgjelinek (void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL); 35750b5de56dSgjelinek return (Z_ERR); 35760b5de56dSgjelinek } 35770b5de56dSgjelinek 35780b5de56dSgjelinek /* 35790b5de56dSgjelinek * Attempt to create a ZFS fs for the zonepath. As usual, we don't 35800b5de56dSgjelinek * care if this works or not since we always have the default behavior 35810b5de56dSgjelinek * of a simple directory for the zonepath. 35820b5de56dSgjelinek */ 35830b5de56dSgjelinek create_zfs_zonepath(zonepath); 35840b5de56dSgjelinek 35850b5de56dSgjelinek (void) printf(gettext("Copying %s..."), source_zonepath); 35860b5de56dSgjelinek (void) fflush(stdout); 35870b5de56dSgjelinek 35880b5de56dSgjelinek err = copy_zone(source_zonepath, zonepath); 35890b5de56dSgjelinek 35900b5de56dSgjelinek (void) printf("\n"); 35910b5de56dSgjelinek 35920b5de56dSgjelinek return (err); 35930b5de56dSgjelinek } 35940b5de56dSgjelinek 3595865e09a4Sgjelinek static int 3596865e09a4Sgjelinek clone_func(int argc, char *argv[]) 3597865e09a4Sgjelinek { 3598865e09a4Sgjelinek char *source_zone = NULL; 3599865e09a4Sgjelinek int lockfd; 3600865e09a4Sgjelinek int err, arg; 3601865e09a4Sgjelinek char zonepath[MAXPATHLEN]; 3602865e09a4Sgjelinek char source_zonepath[MAXPATHLEN]; 3603865e09a4Sgjelinek zone_state_t state; 3604865e09a4Sgjelinek zone_entry_t *zent; 36050b5de56dSgjelinek char *method = NULL; 36060b5de56dSgjelinek char *snapshot = NULL; 3607ff17c8bfSgjelinek char cmdbuf[MAXPATHLEN]; 3608ff17c8bfSgjelinek char postcmdbuf[MAXPATHLEN]; 3609ff17c8bfSgjelinek char presnapbuf[MAXPATHLEN]; 3610ff17c8bfSgjelinek char postsnapbuf[MAXPATHLEN]; 3611ff17c8bfSgjelinek char validsnapbuf[MAXPATHLEN]; 3612ff17c8bfSgjelinek brand_handle_t bh = NULL; 3613ff17c8bfSgjelinek int status; 3614ff17c8bfSgjelinek boolean_t brand_help = B_FALSE; 3615865e09a4Sgjelinek 3616865e09a4Sgjelinek if (zonecfg_in_alt_root()) { 3617865e09a4Sgjelinek zerror(gettext("cannot clone zone in alternate root")); 3618865e09a4Sgjelinek return (Z_ERR); 3619865e09a4Sgjelinek } 3620865e09a4Sgjelinek 3621ff17c8bfSgjelinek /* Check the argv string for args we handle internally */ 3622865e09a4Sgjelinek optind = 0; 3623ff17c8bfSgjelinek opterr = 0; 3624ff17c8bfSgjelinek while ((arg = getopt(argc, argv, "?m:s:")) != EOF) { 3625865e09a4Sgjelinek switch (arg) { 3626865e09a4Sgjelinek case '?': 3627ff17c8bfSgjelinek if (optopt == '?') { 3628865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3629ff17c8bfSgjelinek brand_help = B_TRUE; 3630ff17c8bfSgjelinek } 3631ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 3632ff17c8bfSgjelinek break; 3633865e09a4Sgjelinek case 'm': 3634865e09a4Sgjelinek method = optarg; 3635865e09a4Sgjelinek break; 36360b5de56dSgjelinek case 's': 36370b5de56dSgjelinek snapshot = optarg; 36380b5de56dSgjelinek break; 3639865e09a4Sgjelinek default: 3640ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 3641ff17c8bfSgjelinek break; 3642ff17c8bfSgjelinek } 3643ff17c8bfSgjelinek } 3644ff17c8bfSgjelinek 3645ff17c8bfSgjelinek if (argc != (optind + 1)) { 3646865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3647865e09a4Sgjelinek return (Z_USAGE); 3648865e09a4Sgjelinek } 3649ff17c8bfSgjelinek 3650865e09a4Sgjelinek source_zone = argv[optind]; 3651ff17c8bfSgjelinek 3652ff17c8bfSgjelinek if (!brand_help) { 3653ff17c8bfSgjelinek if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE, 3654ff17c8bfSgjelinek B_FALSE) != Z_OK) 3655865e09a4Sgjelinek return (Z_ERR); 3656ce28b40eSzt129084 if (verify_details(CMD_CLONE, argv) != Z_OK) 3657865e09a4Sgjelinek return (Z_ERR); 3658865e09a4Sgjelinek 3659865e09a4Sgjelinek /* 3660865e09a4Sgjelinek * We also need to do some extra validation on the source zone. 3661865e09a4Sgjelinek */ 3662865e09a4Sgjelinek if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) { 3663ff17c8bfSgjelinek zerror(gettext("%s operation is invalid for the " 3664ff17c8bfSgjelinek "global zone."), cmd_to_str(CMD_CLONE)); 3665865e09a4Sgjelinek return (Z_ERR); 3666865e09a4Sgjelinek } 3667865e09a4Sgjelinek 3668865e09a4Sgjelinek if (strncmp(source_zone, "SUNW", 4) == 0) { 3669ff17c8bfSgjelinek zerror(gettext("%s operation is invalid for zones " 3670ff17c8bfSgjelinek "starting with SUNW."), cmd_to_str(CMD_CLONE)); 3671865e09a4Sgjelinek return (Z_ERR); 3672865e09a4Sgjelinek } 3673865e09a4Sgjelinek 3674865e09a4Sgjelinek zent = lookup_running_zone(source_zone); 3675865e09a4Sgjelinek if (zent != NULL) { 3676865e09a4Sgjelinek /* check whether the zone is ready or running */ 3677ff17c8bfSgjelinek if ((err = zone_get_state(zent->zname, 3678ff17c8bfSgjelinek &zent->zstate_num)) != Z_OK) { 3679865e09a4Sgjelinek errno = err; 3680ff17c8bfSgjelinek zperror2(zent->zname, gettext("could not get " 3681ff17c8bfSgjelinek "state")); 3682865e09a4Sgjelinek /* can't tell, so hedge */ 3683865e09a4Sgjelinek zent->zstate_str = "ready/running"; 3684865e09a4Sgjelinek } else { 3685ff17c8bfSgjelinek zent->zstate_str = 3686ff17c8bfSgjelinek zone_state_str(zent->zstate_num); 3687865e09a4Sgjelinek } 3688865e09a4Sgjelinek zerror(gettext("%s operation is invalid for %s zones."), 3689865e09a4Sgjelinek cmd_to_str(CMD_CLONE), zent->zstate_str); 3690865e09a4Sgjelinek return (Z_ERR); 3691865e09a4Sgjelinek } 3692865e09a4Sgjelinek 3693865e09a4Sgjelinek if ((err = zone_get_state(source_zone, &state)) != Z_OK) { 3694865e09a4Sgjelinek errno = err; 3695865e09a4Sgjelinek zperror2(source_zone, gettext("could not get state")); 3696865e09a4Sgjelinek return (Z_ERR); 3697865e09a4Sgjelinek } 3698865e09a4Sgjelinek if (state != ZONE_STATE_INSTALLED) { 3699865e09a4Sgjelinek (void) fprintf(stderr, 3700865e09a4Sgjelinek gettext("%s: zone %s is %s; %s is required.\n"), 3701865e09a4Sgjelinek execname, source_zone, zone_state_str(state), 3702865e09a4Sgjelinek zone_state_str(ZONE_STATE_INSTALLED)); 3703865e09a4Sgjelinek return (Z_ERR); 3704865e09a4Sgjelinek } 3705865e09a4Sgjelinek 3706865e09a4Sgjelinek /* 3707865e09a4Sgjelinek * The source zone checks out ok, continue with the clone. 3708865e09a4Sgjelinek */ 3709865e09a4Sgjelinek 3710865e09a4Sgjelinek if (validate_clone(source_zone, target_zone) != Z_OK) 3711865e09a4Sgjelinek return (Z_ERR); 3712865e09a4Sgjelinek 3713ff17c8bfSgjelinek if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) { 3714ff17c8bfSgjelinek zerror(gettext("another %s may have an operation in " 3715ff17c8bfSgjelinek "progress."), "zoneadm"); 3716865e09a4Sgjelinek return (Z_ERR); 3717865e09a4Sgjelinek } 3718ff17c8bfSgjelinek } 3719865e09a4Sgjelinek 3720865e09a4Sgjelinek if ((err = zone_get_zonepath(source_zone, source_zonepath, 3721865e09a4Sgjelinek sizeof (source_zonepath))) != Z_OK) { 3722865e09a4Sgjelinek errno = err; 3723865e09a4Sgjelinek zperror2(source_zone, gettext("could not get zone path")); 3724865e09a4Sgjelinek goto done; 3725865e09a4Sgjelinek } 3726865e09a4Sgjelinek 3727865e09a4Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 3728865e09a4Sgjelinek != Z_OK) { 3729865e09a4Sgjelinek errno = err; 3730865e09a4Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 3731865e09a4Sgjelinek goto done; 3732865e09a4Sgjelinek } 3733865e09a4Sgjelinek 3734ff17c8bfSgjelinek /* 3735ff17c8bfSgjelinek * Fetch the clone and postclone hooks from the brand configuration. 3736ff17c8bfSgjelinek */ 3737ff17c8bfSgjelinek if ((bh = brand_open(target_brand)) == NULL) { 3738ff17c8bfSgjelinek zerror(gettext("missing or invalid brand")); 3739ff17c8bfSgjelinek err = Z_ERR; 3740ff17c8bfSgjelinek goto done; 3741ff17c8bfSgjelinek } 3742ff17c8bfSgjelinek 3743ff17c8bfSgjelinek if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_clone, target_zone, 3744ff17c8bfSgjelinek zonepath) != Z_OK) { 3745ff17c8bfSgjelinek zerror("invalid brand configuration: missing clone resource"); 3746ff17c8bfSgjelinek brand_close(bh); 3747ff17c8bfSgjelinek err = Z_ERR; 3748ff17c8bfSgjelinek goto done; 3749ff17c8bfSgjelinek } 3750ff17c8bfSgjelinek 3751ff17c8bfSgjelinek if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postclone, 3752ff17c8bfSgjelinek target_zone, zonepath) != Z_OK) { 3753ff17c8bfSgjelinek zerror("invalid brand configuration: missing postclone " 3754ff17c8bfSgjelinek "resource"); 3755ff17c8bfSgjelinek brand_close(bh); 3756ff17c8bfSgjelinek err = Z_ERR; 3757ff17c8bfSgjelinek goto done; 3758ff17c8bfSgjelinek } 3759ff17c8bfSgjelinek 3760ff17c8bfSgjelinek if (get_hook(bh, presnapbuf, sizeof (presnapbuf), brand_get_presnap, 3761ff17c8bfSgjelinek source_zone, source_zonepath) != Z_OK) { 3762ff17c8bfSgjelinek zerror("invalid brand configuration: missing presnap " 3763ff17c8bfSgjelinek "resource"); 3764ff17c8bfSgjelinek brand_close(bh); 3765ff17c8bfSgjelinek err = Z_ERR; 3766ff17c8bfSgjelinek goto done; 3767ff17c8bfSgjelinek } 3768ff17c8bfSgjelinek 3769ff17c8bfSgjelinek if (get_hook(bh, postsnapbuf, sizeof (postsnapbuf), brand_get_postsnap, 3770ff17c8bfSgjelinek source_zone, source_zonepath) != Z_OK) { 3771ff17c8bfSgjelinek zerror("invalid brand configuration: missing postsnap " 3772ff17c8bfSgjelinek "resource"); 3773ff17c8bfSgjelinek brand_close(bh); 3774ff17c8bfSgjelinek err = Z_ERR; 3775ff17c8bfSgjelinek goto done; 3776ff17c8bfSgjelinek } 3777ff17c8bfSgjelinek 3778ff17c8bfSgjelinek if (get_hook(bh, validsnapbuf, sizeof (validsnapbuf), 3779ff17c8bfSgjelinek brand_get_validatesnap, target_zone, zonepath) != Z_OK) { 3780ff17c8bfSgjelinek zerror("invalid brand configuration: missing validatesnap " 3781ff17c8bfSgjelinek "resource"); 3782ff17c8bfSgjelinek brand_close(bh); 3783ff17c8bfSgjelinek err = Z_ERR; 3784ff17c8bfSgjelinek goto done; 3785ff17c8bfSgjelinek } 3786ff17c8bfSgjelinek brand_close(bh); 3787ff17c8bfSgjelinek 3788ff17c8bfSgjelinek /* Append all options to clone hook. */ 3789ff17c8bfSgjelinek if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) { 3790ff17c8bfSgjelinek err = Z_ERR; 3791ff17c8bfSgjelinek goto done; 3792ff17c8bfSgjelinek } 3793ff17c8bfSgjelinek 3794ff17c8bfSgjelinek /* Append all options to postclone hook. */ 3795ff17c8bfSgjelinek if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK) { 3796ff17c8bfSgjelinek err = Z_ERR; 3797ff17c8bfSgjelinek goto done; 3798ff17c8bfSgjelinek } 3799ff17c8bfSgjelinek 3800ff17c8bfSgjelinek if (!brand_help) { 3801865e09a4Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE)) 3802865e09a4Sgjelinek != Z_OK) { 3803865e09a4Sgjelinek errno = err; 3804865e09a4Sgjelinek zperror2(target_zone, gettext("could not set state")); 3805865e09a4Sgjelinek goto done; 3806865e09a4Sgjelinek } 3807ff17c8bfSgjelinek } 3808ff17c8bfSgjelinek 3809ff17c8bfSgjelinek /* 3810ff17c8bfSgjelinek * The clone hook is optional. If it exists, use the hook for 3811ff17c8bfSgjelinek * cloning, otherwise use the built-in clone support 3812ff17c8bfSgjelinek */ 3813ff17c8bfSgjelinek if (cmdbuf[0] != '\0') { 3814ff17c8bfSgjelinek /* Run the clone hook */ 3815ff17c8bfSgjelinek status = do_subproc_interactive(cmdbuf); 3816ff17c8bfSgjelinek if ((status = subproc_status(gettext("brand-specific clone"), 3817ff17c8bfSgjelinek status, B_FALSE)) != ZONE_SUBPROC_OK) { 3818ff17c8bfSgjelinek if (status == ZONE_SUBPROC_USAGE && !brand_help) 3819ff17c8bfSgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3820ff17c8bfSgjelinek err = Z_ERR; 3821ff17c8bfSgjelinek goto done; 3822ff17c8bfSgjelinek } 3823ff17c8bfSgjelinek 3824ff17c8bfSgjelinek if (brand_help) 3825ff17c8bfSgjelinek return (Z_OK); 3826ff17c8bfSgjelinek 3827ff17c8bfSgjelinek } else { 3828ff17c8bfSgjelinek /* If just help, we're done since there is no brand help. */ 3829ff17c8bfSgjelinek if (brand_help) 3830ff17c8bfSgjelinek return (Z_OK); 3831ff17c8bfSgjelinek 3832ff17c8bfSgjelinek /* Run the built-in clone support. */ 3833ff17c8bfSgjelinek 3834ff17c8bfSgjelinek /* The only explicit built-in method is "copy". */ 3835ff17c8bfSgjelinek if (method != NULL && strcmp(method, "copy") != 0) { 3836ff17c8bfSgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3837ff17c8bfSgjelinek err = Z_USAGE; 3838ff17c8bfSgjelinek goto done; 3839ff17c8bfSgjelinek } 3840865e09a4Sgjelinek 38410b5de56dSgjelinek if (snapshot != NULL) { 3842ff17c8bfSgjelinek err = clone_snapshot_zfs(snapshot, zonepath, 3843ff17c8bfSgjelinek validsnapbuf); 38440b5de56dSgjelinek } else { 38450b5de56dSgjelinek /* 3846ff17c8bfSgjelinek * We always copy the clone unless the source is ZFS 3847ff17c8bfSgjelinek * and a ZFS clone worked. We fallback to copying if 3848ff17c8bfSgjelinek * the ZFS clone fails for some reason. 38490b5de56dSgjelinek */ 38500b5de56dSgjelinek err = Z_ERR; 38510b5de56dSgjelinek if (method == NULL && is_zonepath_zfs(source_zonepath)) 3852ff17c8bfSgjelinek err = clone_zfs(source_zonepath, zonepath, 3853ff17c8bfSgjelinek presnapbuf, postsnapbuf); 3854865e09a4Sgjelinek 38555cd08338Sgjelinek if (err != Z_OK) 38560b5de56dSgjelinek err = clone_copy(source_zonepath, zonepath); 38570b5de56dSgjelinek } 3858ff17c8bfSgjelinek } 3859865e09a4Sgjelinek 3860ff17c8bfSgjelinek if (err == Z_OK && postcmdbuf[0] != '\0') { 3861ff17c8bfSgjelinek status = do_subproc(postcmdbuf); 3862ff17c8bfSgjelinek if ((err = subproc_status("postclone", status, B_FALSE)) 3863ff17c8bfSgjelinek != ZONE_SUBPROC_OK) { 3864ff17c8bfSgjelinek zerror(gettext("post-clone configuration failed.")); 3865ff17c8bfSgjelinek err = Z_ERR; 3866ff17c8bfSgjelinek } 3867ff17c8bfSgjelinek } 3868865e09a4Sgjelinek 3869865e09a4Sgjelinek done: 38709acbbeafSnn35248 /* 38719acbbeafSnn35248 * If everything went well, we mark the zone as installed. 38729acbbeafSnn35248 */ 38739acbbeafSnn35248 if (err == Z_OK) { 38749acbbeafSnn35248 err = zone_set_state(target_zone, ZONE_STATE_INSTALLED); 38759acbbeafSnn35248 if (err != Z_OK) { 38769acbbeafSnn35248 errno = err; 38779acbbeafSnn35248 zperror2(target_zone, gettext("could not set state")); 38789acbbeafSnn35248 } 38799acbbeafSnn35248 } 3880ff17c8bfSgjelinek if (!brand_help) 3881ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 3882865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3883865e09a4Sgjelinek } 3884865e09a4Sgjelinek 388507b574eeSgjelinek /* 38860b5de56dSgjelinek * Used when removing a zonepath after uninstalling or cleaning up after 38870b5de56dSgjelinek * the move subcommand. This handles a zonepath that has non-standard 38880b5de56dSgjelinek * contents so that we will only cleanup the stuff we know about and leave 38890b5de56dSgjelinek * any user data alone. 389007b574eeSgjelinek * 38910b5de56dSgjelinek * If the "all" parameter is true then we should remove the whole zonepath 38920b5de56dSgjelinek * even if it has non-standard files/directories in it. This can be used when 38930b5de56dSgjelinek * we need to cleanup after moving the zonepath across file systems. 38940b5de56dSgjelinek * 38950b5de56dSgjelinek * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND 38960b5de56dSgjelinek * and not the shell. 389707b574eeSgjelinek */ 389807b574eeSgjelinek static int 38990b5de56dSgjelinek cleanup_zonepath(char *zonepath, boolean_t all) 390007b574eeSgjelinek { 390107b574eeSgjelinek int status; 39020b5de56dSgjelinek int i; 39030b5de56dSgjelinek boolean_t non_std = B_FALSE; 39040b5de56dSgjelinek struct dirent *dp; 39050b5de56dSgjelinek DIR *dirp; 3906d9e728a2Sgjelinek /* 3907d9e728a2Sgjelinek * The SUNWattached.xml file is expected since it might 3908d9e728a2Sgjelinek * exist if the zone was force-attached after a 3909d9e728a2Sgjelinek * migration. 3910d9e728a2Sgjelinek */ 3911d9e728a2Sgjelinek char *std_entries[] = {"dev", "lu", "root", 3912d9e728a2Sgjelinek "SUNWattached.xml", NULL}; 39130b5de56dSgjelinek /* (MAXPATHLEN * 3) is for the 3 std_entries dirs */ 39140b5de56dSgjelinek char cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64]; 391507b574eeSgjelinek 391607b574eeSgjelinek /* 39170b5de56dSgjelinek * We shouldn't need these checks but lets be paranoid since we 39180b5de56dSgjelinek * could blow away the whole system here if we got the wrong zonepath. 391907b574eeSgjelinek */ 39200b5de56dSgjelinek if (*zonepath == NULL || strcmp(zonepath, "/") == 0) { 39210b5de56dSgjelinek (void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath); 39220b5de56dSgjelinek return (Z_INVAL); 39230b5de56dSgjelinek } 39240b5de56dSgjelinek 392507b574eeSgjelinek /* 39260b5de56dSgjelinek * If the dirpath is already gone (maybe it was manually removed) then 39270b5de56dSgjelinek * we just return Z_OK so that the cleanup is successful. 392807b574eeSgjelinek */ 39290b5de56dSgjelinek if ((dirp = opendir(zonepath)) == NULL) 39300b5de56dSgjelinek return (Z_OK); 39310b5de56dSgjelinek 39320b5de56dSgjelinek /* 39330b5de56dSgjelinek * Look through the zonepath directory to see if there are any 39340b5de56dSgjelinek * non-standard files/dirs. Also skip .zfs since that might be 39350b5de56dSgjelinek * there but we'll handle ZFS file systems as a special case. 39360b5de56dSgjelinek */ 39370b5de56dSgjelinek while ((dp = readdir(dirp)) != NULL) { 39380b5de56dSgjelinek if (strcmp(dp->d_name, ".") == 0 || 39390b5de56dSgjelinek strcmp(dp->d_name, "..") == 0 || 39400b5de56dSgjelinek strcmp(dp->d_name, ".zfs") == 0) 39410b5de56dSgjelinek continue; 39420b5de56dSgjelinek 39430b5de56dSgjelinek for (i = 0; std_entries[i] != NULL; i++) 39440b5de56dSgjelinek if (strcmp(dp->d_name, std_entries[i]) == 0) 39450b5de56dSgjelinek break; 39460b5de56dSgjelinek 39470b5de56dSgjelinek if (std_entries[i] == NULL) 39480b5de56dSgjelinek non_std = B_TRUE; 39490b5de56dSgjelinek } 39500b5de56dSgjelinek (void) closedir(dirp); 39510b5de56dSgjelinek 39520b5de56dSgjelinek if (!all && non_std) { 39530b5de56dSgjelinek /* 39540b5de56dSgjelinek * There are extra, non-standard directories/files in the 39550b5de56dSgjelinek * zonepath so we don't want to remove the zonepath. We 39560b5de56dSgjelinek * just want to remove the standard directories and leave 39570b5de56dSgjelinek * the user data alone. 39580b5de56dSgjelinek */ 39590b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND); 39600b5de56dSgjelinek 39610b5de56dSgjelinek for (i = 0; std_entries[i] != NULL; i++) { 39620b5de56dSgjelinek char tmpbuf[MAXPATHLEN]; 39630b5de56dSgjelinek 39640b5de56dSgjelinek if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s", 39650b5de56dSgjelinek zonepath, std_entries[i]) >= sizeof (tmpbuf) || 39660b5de56dSgjelinek strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >= 39670b5de56dSgjelinek sizeof (cmdbuf)) { 39680b5de56dSgjelinek (void) fprintf(stderr, 39690b5de56dSgjelinek gettext("path is too long\n")); 39700b5de56dSgjelinek return (Z_INVAL); 39710b5de56dSgjelinek } 397207b574eeSgjelinek } 397307b574eeSgjelinek 397407b574eeSgjelinek status = do_subproc(cmdbuf); 397507b574eeSgjelinek 39760b5de56dSgjelinek (void) fprintf(stderr, gettext("WARNING: Unable to completely " 39770b5de56dSgjelinek "remove %s\nbecause it contains additional user data. " 39780b5de56dSgjelinek "Only the standard directory\nentries have been " 39790b5de56dSgjelinek "removed.\n"), 39800b5de56dSgjelinek zonepath); 398107b574eeSgjelinek 39829acbbeafSnn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == 39839acbbeafSnn35248 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR); 39840b5de56dSgjelinek } 39850b5de56dSgjelinek 39860b5de56dSgjelinek /* 39870b5de56dSgjelinek * There is nothing unexpected in the zonepath, try to get rid of the 39880b5de56dSgjelinek * whole zonepath directory. 39890b5de56dSgjelinek * 39900b5de56dSgjelinek * If the zonepath is its own zfs file system, try to destroy the 39910b5de56dSgjelinek * file system. If that fails for some reason (e.g. it has clones) 39920b5de56dSgjelinek * then we'll just remove the contents of the zonepath. 39930b5de56dSgjelinek */ 39940b5de56dSgjelinek if (is_zonepath_zfs(zonepath)) { 39950b5de56dSgjelinek if (destroy_zfs(zonepath) == Z_OK) 39960b5de56dSgjelinek return (Z_OK); 39970b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND 39980b5de56dSgjelinek " %s/*", zonepath); 39990b5de56dSgjelinek status = do_subproc(cmdbuf); 40009acbbeafSnn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == 40019acbbeafSnn35248 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR); 40020b5de56dSgjelinek } 40030b5de56dSgjelinek 40040b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 40050b5de56dSgjelinek zonepath); 40060b5de56dSgjelinek status = do_subproc(cmdbuf); 40079acbbeafSnn35248 40089acbbeafSnn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK) 40099acbbeafSnn35248 ? Z_OK : Z_ERR); 401007b574eeSgjelinek } 401107b574eeSgjelinek 4012865e09a4Sgjelinek static int 4013865e09a4Sgjelinek move_func(int argc, char *argv[]) 4014865e09a4Sgjelinek { 4015865e09a4Sgjelinek char *new_zonepath = NULL; 4016865e09a4Sgjelinek int lockfd; 4017865e09a4Sgjelinek int err, arg; 4018865e09a4Sgjelinek char zonepath[MAXPATHLEN]; 4019865e09a4Sgjelinek zone_dochandle_t handle; 4020865e09a4Sgjelinek boolean_t fast; 40210b5de56dSgjelinek boolean_t is_zfs = B_FALSE; 40220b5de56dSgjelinek struct dirent *dp; 40230b5de56dSgjelinek DIR *dirp; 40240b5de56dSgjelinek boolean_t empty = B_TRUE; 4025865e09a4Sgjelinek boolean_t revert; 4026865e09a4Sgjelinek struct stat zonepath_buf; 4027865e09a4Sgjelinek struct stat new_zonepath_buf; 4028865e09a4Sgjelinek 4029865e09a4Sgjelinek if (zonecfg_in_alt_root()) { 4030865e09a4Sgjelinek zerror(gettext("cannot move zone in alternate root")); 4031865e09a4Sgjelinek return (Z_ERR); 4032865e09a4Sgjelinek } 4033865e09a4Sgjelinek 4034865e09a4Sgjelinek optind = 0; 4035865e09a4Sgjelinek if ((arg = getopt(argc, argv, "?")) != EOF) { 4036865e09a4Sgjelinek switch (arg) { 4037865e09a4Sgjelinek case '?': 4038865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 4039865e09a4Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 4040865e09a4Sgjelinek default: 4041865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 4042865e09a4Sgjelinek return (Z_USAGE); 4043865e09a4Sgjelinek } 4044865e09a4Sgjelinek } 4045865e09a4Sgjelinek if (argc != (optind + 1)) { 4046865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 4047865e09a4Sgjelinek return (Z_USAGE); 4048865e09a4Sgjelinek } 4049865e09a4Sgjelinek new_zonepath = argv[optind]; 40509acbbeafSnn35248 if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE) 40519acbbeafSnn35248 != Z_OK) 4052865e09a4Sgjelinek return (Z_ERR); 4053ce28b40eSzt129084 if (verify_details(CMD_MOVE, argv) != Z_OK) 4054865e09a4Sgjelinek return (Z_ERR); 4055865e09a4Sgjelinek 4056865e09a4Sgjelinek /* 4057865e09a4Sgjelinek * Check out the new zonepath. This has the side effect of creating 4058865e09a4Sgjelinek * a directory for the new zonepath. We depend on this later when we 40590b5de56dSgjelinek * stat to see if we are doing a cross file system move or not. 4060865e09a4Sgjelinek */ 4061865e09a4Sgjelinek if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK) 4062865e09a4Sgjelinek return (Z_ERR); 4063865e09a4Sgjelinek 4064865e09a4Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 4065865e09a4Sgjelinek != Z_OK) { 4066865e09a4Sgjelinek errno = err; 4067865e09a4Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 4068865e09a4Sgjelinek return (Z_ERR); 4069865e09a4Sgjelinek } 4070865e09a4Sgjelinek 4071865e09a4Sgjelinek if (stat(zonepath, &zonepath_buf) == -1) { 4072865e09a4Sgjelinek zperror(gettext("could not stat zone path"), B_FALSE); 4073865e09a4Sgjelinek return (Z_ERR); 4074865e09a4Sgjelinek } 4075865e09a4Sgjelinek 4076865e09a4Sgjelinek if (stat(new_zonepath, &new_zonepath_buf) == -1) { 4077865e09a4Sgjelinek zperror(gettext("could not stat new zone path"), B_FALSE); 4078865e09a4Sgjelinek return (Z_ERR); 4079865e09a4Sgjelinek } 4080865e09a4Sgjelinek 40810b5de56dSgjelinek /* 40820b5de56dSgjelinek * Check if the destination directory is empty. 40830b5de56dSgjelinek */ 40840b5de56dSgjelinek if ((dirp = opendir(new_zonepath)) == NULL) { 40850b5de56dSgjelinek zperror(gettext("could not open new zone path"), B_FALSE); 40860b5de56dSgjelinek return (Z_ERR); 40870b5de56dSgjelinek } 40880b5de56dSgjelinek while ((dp = readdir(dirp)) != (struct dirent *)0) { 40890b5de56dSgjelinek if (strcmp(dp->d_name, ".") == 0 || 40900b5de56dSgjelinek strcmp(dp->d_name, "..") == 0) 40910b5de56dSgjelinek continue; 40920b5de56dSgjelinek empty = B_FALSE; 40930b5de56dSgjelinek break; 40940b5de56dSgjelinek } 40950b5de56dSgjelinek (void) closedir(dirp); 40960b5de56dSgjelinek 40970b5de56dSgjelinek /* Error if there is anything in the destination directory. */ 40980b5de56dSgjelinek if (!empty) { 40990b5de56dSgjelinek (void) fprintf(stderr, gettext("could not move zone to %s: " 41000b5de56dSgjelinek "directory not empty\n"), new_zonepath); 41010b5de56dSgjelinek return (Z_ERR); 41020b5de56dSgjelinek } 41030b5de56dSgjelinek 4104865e09a4Sgjelinek /* Don't move the zone if anything is still mounted there */ 4105865e09a4Sgjelinek if (zonecfg_find_mounts(zonepath, NULL, NULL)) { 41060b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 4107865e09a4Sgjelinek "subdirectories of %s.\n"), zonepath); 4108865e09a4Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 4109865e09a4Sgjelinek return (Z_ERR); 4110865e09a4Sgjelinek } 4111865e09a4Sgjelinek 4112865e09a4Sgjelinek /* 4113865e09a4Sgjelinek * Check if we are moving in the same file system and can do a fast 4114865e09a4Sgjelinek * move or if we are crossing file systems and have to copy the data. 4115865e09a4Sgjelinek */ 4116865e09a4Sgjelinek fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev); 4117865e09a4Sgjelinek 4118865e09a4Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 4119865e09a4Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 4120865e09a4Sgjelinek return (Z_ERR); 4121865e09a4Sgjelinek } 4122865e09a4Sgjelinek 4123865e09a4Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 4124865e09a4Sgjelinek errno = err; 4125865e09a4Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 4126865e09a4Sgjelinek zonecfg_fini_handle(handle); 4127865e09a4Sgjelinek return (Z_ERR); 4128865e09a4Sgjelinek } 4129865e09a4Sgjelinek 4130ff17c8bfSgjelinek if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) { 4131865e09a4Sgjelinek zerror(gettext("another %s may have an operation in progress."), 4132865e09a4Sgjelinek "zoneadm"); 4133865e09a4Sgjelinek zonecfg_fini_handle(handle); 4134865e09a4Sgjelinek return (Z_ERR); 4135865e09a4Sgjelinek } 4136865e09a4Sgjelinek 4137865e09a4Sgjelinek /* 41380b5de56dSgjelinek * We're making some file system changes now so we have to clean up 41390b5de56dSgjelinek * the file system before we are done. This will either clean up the 4140865e09a4Sgjelinek * new zonepath if the zonecfg update failed or it will clean up the 4141865e09a4Sgjelinek * old zonepath if everything is ok. 4142865e09a4Sgjelinek */ 4143865e09a4Sgjelinek revert = B_TRUE; 4144865e09a4Sgjelinek 41450b5de56dSgjelinek if (is_zonepath_zfs(zonepath) && 41460b5de56dSgjelinek move_zfs(zonepath, new_zonepath) != Z_ERR) { 41470b5de56dSgjelinek is_zfs = B_TRUE; 41480b5de56dSgjelinek 41490b5de56dSgjelinek } else if (fast) { 4150865e09a4Sgjelinek /* same file system, use rename for a quick move */ 4151865e09a4Sgjelinek 4152865e09a4Sgjelinek /* 4153865e09a4Sgjelinek * Remove the new_zonepath directory that got created above 4154865e09a4Sgjelinek * during the validation. It gets in the way of the rename. 4155865e09a4Sgjelinek */ 4156865e09a4Sgjelinek if (rmdir(new_zonepath) != 0) { 4157865e09a4Sgjelinek zperror(gettext("could not rmdir new zone path"), 4158865e09a4Sgjelinek B_FALSE); 4159865e09a4Sgjelinek zonecfg_fini_handle(handle); 4160ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 4161865e09a4Sgjelinek return (Z_ERR); 4162865e09a4Sgjelinek } 4163865e09a4Sgjelinek 4164865e09a4Sgjelinek if (rename(zonepath, new_zonepath) != 0) { 4165865e09a4Sgjelinek /* 4166865e09a4Sgjelinek * If this fails we don't need to do all of the 4167865e09a4Sgjelinek * cleanup that happens for the rest of the code 4168865e09a4Sgjelinek * so just return from this error. 4169865e09a4Sgjelinek */ 4170865e09a4Sgjelinek zperror(gettext("could not move zone"), B_FALSE); 4171865e09a4Sgjelinek zonecfg_fini_handle(handle); 4172ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 4173865e09a4Sgjelinek return (Z_ERR); 4174865e09a4Sgjelinek } 4175865e09a4Sgjelinek 4176865e09a4Sgjelinek } else { 41770b5de56dSgjelinek /* 41780b5de56dSgjelinek * Attempt to create a ZFS fs for the new zonepath. As usual, 41790b5de56dSgjelinek * we don't care if this works or not since we always have the 41800b5de56dSgjelinek * default behavior of a simple directory for the zonepath. 41810b5de56dSgjelinek */ 41820b5de56dSgjelinek create_zfs_zonepath(new_zonepath); 41830b5de56dSgjelinek 4184865e09a4Sgjelinek (void) printf(gettext( 41850b5de56dSgjelinek "Moving across file systems; copying zonepath %s..."), 4186865e09a4Sgjelinek zonepath); 4187865e09a4Sgjelinek (void) fflush(stdout); 4188865e09a4Sgjelinek 4189865e09a4Sgjelinek err = copy_zone(zonepath, new_zonepath); 4190865e09a4Sgjelinek 4191865e09a4Sgjelinek (void) printf("\n"); 4192865e09a4Sgjelinek if (err != Z_OK) 4193865e09a4Sgjelinek goto done; 4194865e09a4Sgjelinek } 4195865e09a4Sgjelinek 4196865e09a4Sgjelinek if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) { 4197865e09a4Sgjelinek errno = err; 4198865e09a4Sgjelinek zperror(gettext("could not set new zonepath"), B_TRUE); 4199865e09a4Sgjelinek goto done; 4200865e09a4Sgjelinek } 4201865e09a4Sgjelinek 4202865e09a4Sgjelinek if ((err = zonecfg_save(handle)) != Z_OK) { 4203865e09a4Sgjelinek errno = err; 4204865e09a4Sgjelinek zperror(gettext("zonecfg save failed"), B_TRUE); 4205865e09a4Sgjelinek goto done; 4206865e09a4Sgjelinek } 4207865e09a4Sgjelinek 4208865e09a4Sgjelinek revert = B_FALSE; 4209865e09a4Sgjelinek 4210865e09a4Sgjelinek done: 4211865e09a4Sgjelinek zonecfg_fini_handle(handle); 4212ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 4213865e09a4Sgjelinek 4214865e09a4Sgjelinek /* 42150b5de56dSgjelinek * Clean up the file system based on how things went. We either 4216865e09a4Sgjelinek * clean up the new zonepath if the operation failed for some reason 4217865e09a4Sgjelinek * or we clean up the old zonepath if everything is ok. 4218865e09a4Sgjelinek */ 4219865e09a4Sgjelinek if (revert) { 4220865e09a4Sgjelinek /* The zonecfg update failed, cleanup the new zonepath. */ 42210b5de56dSgjelinek if (is_zfs) { 42220b5de56dSgjelinek if (move_zfs(new_zonepath, zonepath) == Z_ERR) { 42230b5de56dSgjelinek (void) fprintf(stderr, gettext("could not " 42240b5de56dSgjelinek "restore zonepath, the zfs mountpoint is " 42250b5de56dSgjelinek "set as:\n%s\n"), new_zonepath); 42260b5de56dSgjelinek /* 42270b5de56dSgjelinek * err is already != Z_OK since we're reverting 42280b5de56dSgjelinek */ 42290b5de56dSgjelinek } 42300b5de56dSgjelinek 42310b5de56dSgjelinek } else if (fast) { 4232865e09a4Sgjelinek if (rename(new_zonepath, zonepath) != 0) { 4233865e09a4Sgjelinek zperror(gettext("could not restore zonepath"), 4234865e09a4Sgjelinek B_FALSE); 4235865e09a4Sgjelinek /* 4236865e09a4Sgjelinek * err is already != Z_OK since we're reverting 4237865e09a4Sgjelinek */ 4238865e09a4Sgjelinek } 4239865e09a4Sgjelinek } else { 4240865e09a4Sgjelinek (void) printf(gettext("Cleaning up zonepath %s..."), 4241865e09a4Sgjelinek new_zonepath); 4242865e09a4Sgjelinek (void) fflush(stdout); 42430b5de56dSgjelinek err = cleanup_zonepath(new_zonepath, B_TRUE); 4244865e09a4Sgjelinek (void) printf("\n"); 4245865e09a4Sgjelinek 424607b574eeSgjelinek if (err != Z_OK) { 4247865e09a4Sgjelinek errno = err; 4248865e09a4Sgjelinek zperror(gettext("could not remove new " 4249865e09a4Sgjelinek "zonepath"), B_TRUE); 4250865e09a4Sgjelinek } else { 4251865e09a4Sgjelinek /* 4252865e09a4Sgjelinek * Because we're reverting we know the mainline 4253865e09a4Sgjelinek * code failed but we just reused the err 4254865e09a4Sgjelinek * variable so we reset it back to Z_ERR. 4255865e09a4Sgjelinek */ 4256865e09a4Sgjelinek err = Z_ERR; 4257865e09a4Sgjelinek } 4258865e09a4Sgjelinek } 4259865e09a4Sgjelinek 4260865e09a4Sgjelinek } else { 4261865e09a4Sgjelinek /* The move was successful, cleanup the old zonepath. */ 42620b5de56dSgjelinek if (!is_zfs && !fast) { 4263865e09a4Sgjelinek (void) printf( 4264865e09a4Sgjelinek gettext("Cleaning up zonepath %s..."), zonepath); 4265865e09a4Sgjelinek (void) fflush(stdout); 42660b5de56dSgjelinek err = cleanup_zonepath(zonepath, B_TRUE); 4267865e09a4Sgjelinek (void) printf("\n"); 4268865e09a4Sgjelinek 426907b574eeSgjelinek if (err != Z_OK) { 4270865e09a4Sgjelinek errno = err; 4271865e09a4Sgjelinek zperror(gettext("could not remove zonepath"), 4272865e09a4Sgjelinek B_TRUE); 4273865e09a4Sgjelinek } 4274865e09a4Sgjelinek } 4275865e09a4Sgjelinek } 4276865e09a4Sgjelinek 4277865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 4278865e09a4Sgjelinek } 4279865e09a4Sgjelinek 4280ff17c8bfSgjelinek /* ARGSUSED */ 4281ee519a1fSgjelinek static int 4282ee519a1fSgjelinek detach_func(int argc, char *argv[]) 4283ee519a1fSgjelinek { 4284ee519a1fSgjelinek int lockfd; 4285ee519a1fSgjelinek int err, arg; 4286ee519a1fSgjelinek char zonepath[MAXPATHLEN]; 428737774979Sgjelinek char cmdbuf[MAXPATHLEN]; 4288ff17c8bfSgjelinek char precmdbuf[MAXPATHLEN]; 4289ee519a1fSgjelinek zone_dochandle_t handle; 42908cd327d5Sgjelinek boolean_t execute = B_TRUE; 4291ff17c8bfSgjelinek boolean_t brand_help = B_FALSE; 429237774979Sgjelinek brand_handle_t bh = NULL; 4293ff17c8bfSgjelinek int status; 4294ee519a1fSgjelinek 4295ee519a1fSgjelinek if (zonecfg_in_alt_root()) { 4296ee519a1fSgjelinek zerror(gettext("cannot detach zone in alternate root")); 4297ee519a1fSgjelinek return (Z_ERR); 4298ee519a1fSgjelinek } 4299ee519a1fSgjelinek 4300ff17c8bfSgjelinek /* Check the argv string for args we handle internally */ 4301ee519a1fSgjelinek optind = 0; 4302ff17c8bfSgjelinek opterr = 0; 4303ff17c8bfSgjelinek while ((arg = getopt(argc, argv, "?n")) != EOF) { 4304ee519a1fSgjelinek switch (arg) { 4305ee519a1fSgjelinek case '?': 4306ff17c8bfSgjelinek if (optopt == '?') { 4307ee519a1fSgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 4308ff17c8bfSgjelinek brand_help = B_TRUE; 4309ff17c8bfSgjelinek } 4310ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 4311ff17c8bfSgjelinek break; 43128cd327d5Sgjelinek case 'n': 43138cd327d5Sgjelinek execute = B_FALSE; 43148cd327d5Sgjelinek break; 4315ee519a1fSgjelinek default: 4316ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 4317ff17c8bfSgjelinek break; 4318ee519a1fSgjelinek } 4319ee519a1fSgjelinek } 43209acbbeafSnn35248 4321ff17c8bfSgjelinek if (brand_help) 4322ff17c8bfSgjelinek execute = B_FALSE; 4323ff17c8bfSgjelinek 43248cd327d5Sgjelinek if (execute) { 43259acbbeafSnn35248 if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE, 43269acbbeafSnn35248 B_FALSE) != Z_OK) 4327ee519a1fSgjelinek return (Z_ERR); 4328ce28b40eSzt129084 if (verify_details(CMD_DETACH, argv) != Z_OK) 4329ee519a1fSgjelinek return (Z_ERR); 43308cd327d5Sgjelinek } else { 43318cd327d5Sgjelinek /* 43328cd327d5Sgjelinek * We want a dry-run to work for a non-privileged user so we 43338cd327d5Sgjelinek * only do minimal validation. 43348cd327d5Sgjelinek */ 43358cd327d5Sgjelinek if (target_zone == NULL) { 43368cd327d5Sgjelinek zerror(gettext("no zone specified")); 43378cd327d5Sgjelinek return (Z_ERR); 43388cd327d5Sgjelinek } 43398cd327d5Sgjelinek 43408cd327d5Sgjelinek if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) { 43418cd327d5Sgjelinek zerror(gettext("%s operation is invalid for the " 43428cd327d5Sgjelinek "global zone."), cmd_to_str(CMD_DETACH)); 43438cd327d5Sgjelinek return (Z_ERR); 43448cd327d5Sgjelinek } 43458cd327d5Sgjelinek } 4346ee519a1fSgjelinek 4347ee519a1fSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 4348ee519a1fSgjelinek != Z_OK) { 4349ee519a1fSgjelinek errno = err; 4350ee519a1fSgjelinek zperror2(target_zone, gettext("could not get zone path")); 4351ee519a1fSgjelinek return (Z_ERR); 4352ee519a1fSgjelinek } 4353ee519a1fSgjelinek 4354ee519a1fSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 4355ee519a1fSgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 4356ee519a1fSgjelinek return (Z_ERR); 4357ee519a1fSgjelinek } 4358ee519a1fSgjelinek 4359ee519a1fSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 4360ee519a1fSgjelinek errno = err; 4361ee519a1fSgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 4362ee519a1fSgjelinek zonecfg_fini_handle(handle); 4363ee519a1fSgjelinek return (Z_ERR); 4364ee519a1fSgjelinek } 4365ee519a1fSgjelinek 4366ff17c8bfSgjelinek /* Fetch the detach and predetach hooks from the brand configuration. */ 436737774979Sgjelinek if ((bh = brand_open(target_brand)) == NULL) { 436837774979Sgjelinek zerror(gettext("missing or invalid brand")); 436937774979Sgjelinek return (Z_ERR); 437037774979Sgjelinek } 437137774979Sgjelinek 4372ff17c8bfSgjelinek if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_detach, target_zone, 4373ff17c8bfSgjelinek zonepath) != Z_OK) { 4374ff17c8bfSgjelinek zerror("invalid brand configuration: missing detach resource"); 4375ff17c8bfSgjelinek brand_close(bh); 4376ff17c8bfSgjelinek return (Z_ERR); 4377ff17c8bfSgjelinek } 4378ff17c8bfSgjelinek 4379ff17c8bfSgjelinek if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_predetach, 4380ff17c8bfSgjelinek target_zone, zonepath) != Z_OK) { 438137774979Sgjelinek zerror("invalid brand configuration: missing predetach " 438237774979Sgjelinek "resource"); 438337774979Sgjelinek brand_close(bh); 438437774979Sgjelinek return (Z_ERR); 438537774979Sgjelinek } 438637774979Sgjelinek brand_close(bh); 438737774979Sgjelinek 4388ff17c8bfSgjelinek /* Append all options to predetach hook. */ 4389ff17c8bfSgjelinek if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK) 439037774979Sgjelinek return (Z_ERR); 439137774979Sgjelinek 4392ff17c8bfSgjelinek /* Append all options to detach hook. */ 4393ff17c8bfSgjelinek if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) 439437774979Sgjelinek return (Z_ERR); 439537774979Sgjelinek 4396ff17c8bfSgjelinek if (execute && zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) { 4397ee519a1fSgjelinek zerror(gettext("another %s may have an operation in progress."), 4398ee519a1fSgjelinek "zoneadm"); 4399ee519a1fSgjelinek zonecfg_fini_handle(handle); 4400ee519a1fSgjelinek return (Z_ERR); 4401ee519a1fSgjelinek } 4402ee519a1fSgjelinek 4403ff17c8bfSgjelinek /* If we have a brand predetach hook, run it. */ 4404ff17c8bfSgjelinek if (!brand_help && precmdbuf[0] != '\0') { 4405ff17c8bfSgjelinek status = do_subproc(precmdbuf); 4406ff17c8bfSgjelinek if (subproc_status(gettext("brand-specific predetach"), 4407ff17c8bfSgjelinek status, B_FALSE) != ZONE_SUBPROC_OK) { 4408ff17c8bfSgjelinek 4409ff17c8bfSgjelinek if (execute) 4410ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 4411ff17c8bfSgjelinek return (Z_ERR); 4412ff17c8bfSgjelinek } 4413ff17c8bfSgjelinek } 4414ff17c8bfSgjelinek 4415ff17c8bfSgjelinek if (cmdbuf[0] != '\0') { 4416ff17c8bfSgjelinek /* Run the detach hook */ 4417ff17c8bfSgjelinek status = do_subproc_interactive(cmdbuf); 4418ff17c8bfSgjelinek if ((status = subproc_status(gettext("brand-specific detach"), 4419ff17c8bfSgjelinek status, B_FALSE)) != ZONE_SUBPROC_OK) { 4420ff17c8bfSgjelinek if (status == ZONE_SUBPROC_USAGE && !brand_help) 4421ff17c8bfSgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 4422ff17c8bfSgjelinek 4423ff17c8bfSgjelinek if (execute) 4424ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 4425ff17c8bfSgjelinek 4426ff17c8bfSgjelinek return (Z_ERR); 4427ff17c8bfSgjelinek } 4428ff17c8bfSgjelinek 4429ff17c8bfSgjelinek } else { 4430ff17c8bfSgjelinek /* If just help, we're done since there is no brand help. */ 4431ff17c8bfSgjelinek if (brand_help) 4432ff17c8bfSgjelinek return (Z_OK); 4433ff17c8bfSgjelinek 4434ff17c8bfSgjelinek /* 4435ff17c8bfSgjelinek * Run the built-in detach support. Just generate a simple 4436ff17c8bfSgjelinek * zone definition XML file and detach. 4437ff17c8bfSgjelinek */ 4438ff17c8bfSgjelinek 4439ff17c8bfSgjelinek /* Don't detach the zone if anything is still mounted there */ 4440ff17c8bfSgjelinek if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) { 4441ff17c8bfSgjelinek (void) fprintf(stderr, gettext("These file systems are " 4442ff17c8bfSgjelinek "mounted on subdirectories of %s.\n"), zonepath); 4443ff17c8bfSgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 4444ff17c8bfSgjelinek err = ZONE_SUBPROC_NOTCOMPLETE; 4445ff17c8bfSgjelinek goto done; 4446ff17c8bfSgjelinek } 4447ff17c8bfSgjelinek 4448ff17c8bfSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 4449ff17c8bfSgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 4450ff17c8bfSgjelinek err = ZONE_SUBPROC_NOTCOMPLETE; 4451ff17c8bfSgjelinek goto done; 4452ff17c8bfSgjelinek } 4453ff17c8bfSgjelinek 4454ff17c8bfSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 4455ee519a1fSgjelinek errno = err; 4456ff17c8bfSgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 4457ff17c8bfSgjelinek zonecfg_fini_handle(handle); 4458ff17c8bfSgjelinek goto done; 4459ff17c8bfSgjelinek } 4460ff17c8bfSgjelinek 4461ff17c8bfSgjelinek if ((err = zonecfg_detach_save(handle, 4462ff17c8bfSgjelinek (execute ? 0 : ZONE_DRY_RUN))) != Z_OK) { 4463ff17c8bfSgjelinek errno = err; 4464ff17c8bfSgjelinek zperror(gettext("saving the detach manifest failed"), 4465ee519a1fSgjelinek B_TRUE); 4466ee519a1fSgjelinek goto done; 4467ee519a1fSgjelinek } 4468ee519a1fSgjelinek 4469ff17c8bfSgjelinek zonecfg_fini_handle(handle); 4470ee519a1fSgjelinek } 4471ee519a1fSgjelinek 44728cd327d5Sgjelinek /* 44738cd327d5Sgjelinek * Set the zone state back to configured unless we are running with the 44748cd327d5Sgjelinek * no-execute option. 44758cd327d5Sgjelinek */ 44768cd327d5Sgjelinek if (execute && (err = zone_set_state(target_zone, 44778cd327d5Sgjelinek ZONE_STATE_CONFIGURED)) != Z_OK) { 4478ee519a1fSgjelinek errno = err; 4479ee519a1fSgjelinek zperror(gettext("could not reset state"), B_TRUE); 4480ee519a1fSgjelinek } 4481ee519a1fSgjelinek 4482ee519a1fSgjelinek done: 4483ee519a1fSgjelinek zonecfg_fini_handle(handle); 44848cd327d5Sgjelinek if (execute) 4485ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 4486ee519a1fSgjelinek 4487ee519a1fSgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 4488ee519a1fSgjelinek } 4489ee519a1fSgjelinek 4490ee519a1fSgjelinek /* 4491ff17c8bfSgjelinek * Determine the brand when doing a dry-run attach. The zone does not have to 4492ff17c8bfSgjelinek * exist, so we have to read the incoming manifest to determine the zone's 4493ff17c8bfSgjelinek * brand. 4494ff17c8bfSgjelinek * 4495ff17c8bfSgjelinek * Because the manifest has to be processed twice; once to determine the brand 4496ff17c8bfSgjelinek * and once to do the brand-specific attach logic, we always read it into a tmp 4497ff17c8bfSgjelinek * file. This handles the manifest coming from stdin or a regular file. The 4498ff17c8bfSgjelinek * tmpname parameter returns the name of the temporary file that the manifest 4499ff17c8bfSgjelinek * was read into. 4500ee519a1fSgjelinek */ 4501ee519a1fSgjelinek static int 4502ff17c8bfSgjelinek dryrun_get_brand(char *manifest_path, char *tmpname, int size) 45038cd327d5Sgjelinek { 45048cd327d5Sgjelinek int fd; 45058cd327d5Sgjelinek int err; 4506ff17c8bfSgjelinek int res = Z_OK; 45078cd327d5Sgjelinek zone_dochandle_t local_handle; 45088cd327d5Sgjelinek zone_dochandle_t rem_handle = NULL; 4509ff17c8bfSgjelinek int len; 4510ff17c8bfSgjelinek int ofd; 4511ff17c8bfSgjelinek char buf[512]; 45128cd327d5Sgjelinek 45138cd327d5Sgjelinek if (strcmp(manifest_path, "-") == 0) { 4514ff17c8bfSgjelinek fd = STDIN_FILENO; 4515ff17c8bfSgjelinek } else { 4516ff17c8bfSgjelinek if ((fd = open(manifest_path, O_RDONLY)) < 0) { 4517ff17c8bfSgjelinek if (getcwd(buf, sizeof (buf)) == NULL) 4518ff17c8bfSgjelinek (void) strlcpy(buf, "/", sizeof (buf)); 4519ff17c8bfSgjelinek zerror(gettext("could not open manifest path %s%s: %s"), 4520ff17c8bfSgjelinek (*manifest_path == '/' ? "" : buf), manifest_path, 4521ff17c8bfSgjelinek strerror(errno)); 4522ff17c8bfSgjelinek return (Z_ERR); 4523ff17c8bfSgjelinek } 4524ff17c8bfSgjelinek } 4525ff17c8bfSgjelinek 4526ff17c8bfSgjelinek (void) snprintf(tmpname, size, "/var/run/zone.%d", getpid()); 4527ff17c8bfSgjelinek 4528ff17c8bfSgjelinek if ((ofd = open(tmpname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) { 4529ff17c8bfSgjelinek zperror(gettext("could not save manifest"), B_FALSE); 4530ff17c8bfSgjelinek (void) close(fd); 4531ff17c8bfSgjelinek return (Z_ERR); 4532ff17c8bfSgjelinek } 4533ff17c8bfSgjelinek 4534ff17c8bfSgjelinek while ((len = read(fd, buf, sizeof (buf))) > 0) { 4535ff17c8bfSgjelinek if (write(ofd, buf, len) == -1) { 4536ff17c8bfSgjelinek zperror(gettext("could not save manifest"), B_FALSE); 4537ff17c8bfSgjelinek (void) close(ofd); 4538ff17c8bfSgjelinek (void) close(fd); 4539ff17c8bfSgjelinek return (Z_ERR); 4540ff17c8bfSgjelinek } 4541ff17c8bfSgjelinek } 4542ff17c8bfSgjelinek 4543ff17c8bfSgjelinek if (close(ofd) != 0) { 4544ff17c8bfSgjelinek zperror(gettext("could not save manifest"), B_FALSE); 4545ff17c8bfSgjelinek (void) close(fd); 4546ff17c8bfSgjelinek return (Z_ERR); 4547ff17c8bfSgjelinek } 4548ff17c8bfSgjelinek 4549ff17c8bfSgjelinek (void) close(fd); 4550ff17c8bfSgjelinek 4551ff17c8bfSgjelinek if ((fd = open(tmpname, O_RDONLY)) < 0) { 45528cd327d5Sgjelinek zperror(gettext("could not open manifest path"), B_FALSE); 45538cd327d5Sgjelinek return (Z_ERR); 45548cd327d5Sgjelinek } 45558cd327d5Sgjelinek 45568cd327d5Sgjelinek if ((local_handle = zonecfg_init_handle()) == NULL) { 45578cd327d5Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 45588cd327d5Sgjelinek res = Z_ERR; 45598cd327d5Sgjelinek goto done; 45608cd327d5Sgjelinek } 45618cd327d5Sgjelinek 45628cd327d5Sgjelinek if ((rem_handle = zonecfg_init_handle()) == NULL) { 45638cd327d5Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 45648cd327d5Sgjelinek res = Z_ERR; 45658cd327d5Sgjelinek goto done; 45668cd327d5Sgjelinek } 45678cd327d5Sgjelinek 45688cd327d5Sgjelinek if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle)) 45698cd327d5Sgjelinek != Z_OK) { 45708cd327d5Sgjelinek res = Z_ERR; 4571d9e728a2Sgjelinek 4572d9e728a2Sgjelinek if (err == Z_INVALID_DOCUMENT) { 4573d9e728a2Sgjelinek struct stat st; 4574d9e728a2Sgjelinek char buf[6]; 4575d9e728a2Sgjelinek 4576d9e728a2Sgjelinek if (strcmp(manifest_path, "-") == 0) { 4577d9e728a2Sgjelinek zerror(gettext("Input is not a valid XML " 4578d9e728a2Sgjelinek "file")); 4579d9e728a2Sgjelinek goto done; 4580d9e728a2Sgjelinek } 4581d9e728a2Sgjelinek 4582d9e728a2Sgjelinek if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) { 4583d9e728a2Sgjelinek zerror(gettext("%s is not an XML file"), 4584d9e728a2Sgjelinek manifest_path); 4585d9e728a2Sgjelinek goto done; 4586d9e728a2Sgjelinek } 4587d9e728a2Sgjelinek 4588d9e728a2Sgjelinek bzero(buf, sizeof (buf)); 4589d9e728a2Sgjelinek (void) lseek(fd, 0L, SEEK_SET); 4590d9e728a2Sgjelinek if (read(fd, buf, sizeof (buf) - 1) < 0 || 4591d9e728a2Sgjelinek strncmp(buf, "<?xml", 5) != 0) 4592d9e728a2Sgjelinek zerror(gettext("%s is not an XML file"), 4593d9e728a2Sgjelinek manifest_path); 4594d9e728a2Sgjelinek else 4595d9e728a2Sgjelinek zerror(gettext("Cannot attach to an earlier " 4596d9e728a2Sgjelinek "release of the operating system")); 4597d9e728a2Sgjelinek } else { 4598d9e728a2Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4599d9e728a2Sgjelinek } 46008cd327d5Sgjelinek goto done; 46018cd327d5Sgjelinek } 46028cd327d5Sgjelinek 4603ff17c8bfSgjelinek /* Retrieve remote handle brand type. */ 4604e2482d1aSgjelinek if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand)) 4605e2482d1aSgjelinek != Z_OK) { 4606e2482d1aSgjelinek zerror(gettext("missing or invalid brand")); 4607e2482d1aSgjelinek exit(Z_ERR); 4608e2482d1aSgjelinek } 46098cd327d5Sgjelinek 46108cd327d5Sgjelinek done: 46118cd327d5Sgjelinek zonecfg_fini_handle(local_handle); 46128cd327d5Sgjelinek zonecfg_fini_handle(rem_handle); 4613ff17c8bfSgjelinek (void) close(fd); 46148cd327d5Sgjelinek 46158cd327d5Sgjelinek return ((res == Z_OK) ? Z_OK : Z_ERR); 46168cd327d5Sgjelinek } 46178cd327d5Sgjelinek 46186cfd72c6Sgjelinek /* ARGSUSED */ 4619ee519a1fSgjelinek static int 4620ee519a1fSgjelinek attach_func(int argc, char *argv[]) 4621ee519a1fSgjelinek { 4622ee519a1fSgjelinek int lockfd; 4623ee519a1fSgjelinek int err, arg; 4624ee519a1fSgjelinek boolean_t force = B_FALSE; 4625ee519a1fSgjelinek zone_dochandle_t handle; 4626ee519a1fSgjelinek char zonepath[MAXPATHLEN]; 462737774979Sgjelinek char cmdbuf[MAXPATHLEN]; 4628ff17c8bfSgjelinek char postcmdbuf[MAXPATHLEN]; 46298cd327d5Sgjelinek boolean_t execute = B_TRUE; 4630ff17c8bfSgjelinek boolean_t brand_help = B_FALSE; 46318cd327d5Sgjelinek char *manifest_path; 4632ff17c8bfSgjelinek char tmpmanifest[80]; 4633ff17c8bfSgjelinek int manifest_pos; 463437774979Sgjelinek brand_handle_t bh = NULL; 4635ff17c8bfSgjelinek int status; 4636ee519a1fSgjelinek 4637ee519a1fSgjelinek if (zonecfg_in_alt_root()) { 4638ee519a1fSgjelinek zerror(gettext("cannot attach zone in alternate root")); 4639ee519a1fSgjelinek return (Z_ERR); 4640ee519a1fSgjelinek } 4641ee519a1fSgjelinek 4642ff17c8bfSgjelinek /* Check the argv string for args we handle internally */ 4643ee519a1fSgjelinek optind = 0; 4644ff17c8bfSgjelinek opterr = 0; 4645ff17c8bfSgjelinek while ((arg = getopt(argc, argv, "?Fn:")) != EOF) { 4646ee519a1fSgjelinek switch (arg) { 4647ee519a1fSgjelinek case '?': 4648ff17c8bfSgjelinek if (optopt == '?') { 4649ee519a1fSgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 4650ff17c8bfSgjelinek brand_help = B_TRUE; 4651ff17c8bfSgjelinek } 4652ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 4653ff17c8bfSgjelinek break; 4654ee519a1fSgjelinek case 'F': 4655ee519a1fSgjelinek force = B_TRUE; 4656ee519a1fSgjelinek break; 46578cd327d5Sgjelinek case 'n': 46588cd327d5Sgjelinek execute = B_FALSE; 46598cd327d5Sgjelinek manifest_path = optarg; 4660ff17c8bfSgjelinek manifest_pos = optind - 1; 46616cfd72c6Sgjelinek break; 4662ee519a1fSgjelinek default: 4663ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 4664ff17c8bfSgjelinek break; 4665ee519a1fSgjelinek } 4666ee519a1fSgjelinek } 46678cd327d5Sgjelinek 4668ff17c8bfSgjelinek if (brand_help) { 4669ff17c8bfSgjelinek force = B_FALSE; 4670ff17c8bfSgjelinek execute = B_TRUE; 4671ff17c8bfSgjelinek } 4672ff17c8bfSgjelinek 4673ff17c8bfSgjelinek /* dry-run and force flags are mutually exclusive */ 4674ff17c8bfSgjelinek if (!execute && force) { 4675ff17c8bfSgjelinek zerror(gettext("-F and -n flags are mutually exclusive")); 46766cfd72c6Sgjelinek return (Z_ERR); 46776cfd72c6Sgjelinek } 46786cfd72c6Sgjelinek 46798cd327d5Sgjelinek /* 4680ff17c8bfSgjelinek * If the no-execute option was specified, we don't do validation and 4681ff17c8bfSgjelinek * need to figure out the brand, since there is no zone required to be 46828cd327d5Sgjelinek * configured for this option. 46838cd327d5Sgjelinek */ 4684ff17c8bfSgjelinek if (execute) { 4685ff17c8bfSgjelinek if (!brand_help) { 4686ff17c8bfSgjelinek if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, 4687ff17c8bfSgjelinek B_TRUE, B_FALSE) != Z_OK) 4688ee519a1fSgjelinek return (Z_ERR); 4689ce28b40eSzt129084 if (verify_details(CMD_ATTACH, argv) != Z_OK) 4690ee519a1fSgjelinek return (Z_ERR); 4691ff17c8bfSgjelinek } 4692ee519a1fSgjelinek 4693ff17c8bfSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, 4694ff17c8bfSgjelinek sizeof (zonepath))) != Z_OK) { 4695ee519a1fSgjelinek errno = err; 4696ff17c8bfSgjelinek zperror2(target_zone, 4697ff17c8bfSgjelinek gettext("could not get zone path")); 4698ee519a1fSgjelinek return (Z_ERR); 4699ee519a1fSgjelinek } 4700ee519a1fSgjelinek 4701ee519a1fSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 4702ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4703ee519a1fSgjelinek return (Z_ERR); 4704ee519a1fSgjelinek } 4705ee519a1fSgjelinek 4706ee519a1fSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 4707ee519a1fSgjelinek errno = err; 4708ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4709ee519a1fSgjelinek zonecfg_fini_handle(handle); 4710ee519a1fSgjelinek return (Z_ERR); 4711ee519a1fSgjelinek } 4712ee519a1fSgjelinek 4713ff17c8bfSgjelinek } else { 4714ff17c8bfSgjelinek if (dryrun_get_brand(manifest_path, tmpmanifest, 4715ff17c8bfSgjelinek sizeof (tmpmanifest)) != Z_OK) 4716ff17c8bfSgjelinek return (Z_ERR); 4717ff17c8bfSgjelinek 4718ff17c8bfSgjelinek argv[manifest_pos] = tmpmanifest; 4719ff17c8bfSgjelinek target_zone = "-"; 4720ff17c8bfSgjelinek (void) strlcpy(zonepath, "-", sizeof (zonepath)); 4721ff17c8bfSgjelinek 4722ff17c8bfSgjelinek /* Run the brand's verify_adm hook. */ 4723ff17c8bfSgjelinek if (verify_brand(NULL, CMD_ATTACH, argv) != Z_OK) 4724ff17c8bfSgjelinek return (Z_ERR); 4725ff17c8bfSgjelinek } 4726ff17c8bfSgjelinek 4727ff17c8bfSgjelinek /* 4728ff17c8bfSgjelinek * Fetch the attach and postattach hooks from the brand configuration. 4729ff17c8bfSgjelinek */ 473037774979Sgjelinek if ((bh = brand_open(target_brand)) == NULL) { 473137774979Sgjelinek zerror(gettext("missing or invalid brand")); 473237774979Sgjelinek return (Z_ERR); 473337774979Sgjelinek } 473437774979Sgjelinek 4735ff17c8bfSgjelinek if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_attach, target_zone, 4736ff17c8bfSgjelinek zonepath) != Z_OK) { 4737ff17c8bfSgjelinek zerror("invalid brand configuration: missing attach resource"); 4738ff17c8bfSgjelinek brand_close(bh); 4739ff17c8bfSgjelinek return (Z_ERR); 4740ff17c8bfSgjelinek } 4741ff17c8bfSgjelinek 4742ff17c8bfSgjelinek if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postattach, 4743ff17c8bfSgjelinek target_zone, zonepath) != Z_OK) { 474437774979Sgjelinek zerror("invalid brand configuration: missing postattach " 474537774979Sgjelinek "resource"); 474637774979Sgjelinek brand_close(bh); 474737774979Sgjelinek return (Z_ERR); 474837774979Sgjelinek } 474937774979Sgjelinek brand_close(bh); 475037774979Sgjelinek 4751ff17c8bfSgjelinek /* Append all options to attach hook. */ 4752ff17c8bfSgjelinek if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) 475337774979Sgjelinek return (Z_ERR); 475437774979Sgjelinek 4755ff17c8bfSgjelinek /* Append all options to postattach hook. */ 4756ff17c8bfSgjelinek if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK) 4757ff17c8bfSgjelinek return (Z_ERR); 4758ff17c8bfSgjelinek 4759ff17c8bfSgjelinek if (execute && !brand_help) { 4760ff17c8bfSgjelinek if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) { 4761ff17c8bfSgjelinek zerror(gettext("another %s may have an operation in " 4762ff17c8bfSgjelinek "progress."), "zoneadm"); 4763ee519a1fSgjelinek zonecfg_fini_handle(handle); 4764ee519a1fSgjelinek return (Z_ERR); 4765ee519a1fSgjelinek } 4766ff17c8bfSgjelinek } 4767ee519a1fSgjelinek 4768ee519a1fSgjelinek if (force) 4769ee519a1fSgjelinek goto done; 4770ff17c8bfSgjelinek 4771ff17c8bfSgjelinek if (cmdbuf[0] != '\0') { 4772ff17c8bfSgjelinek /* Run the attach hook */ 4773ff17c8bfSgjelinek status = do_subproc_interactive(cmdbuf); 4774ff17c8bfSgjelinek if ((status = subproc_status(gettext("brand-specific attach"), 4775ff17c8bfSgjelinek status, B_FALSE)) != ZONE_SUBPROC_OK) { 4776ff17c8bfSgjelinek if (status == ZONE_SUBPROC_USAGE && !brand_help) 4777ff17c8bfSgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 4778ff17c8bfSgjelinek 4779ff17c8bfSgjelinek if (execute && !brand_help) 4780ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 4781ff17c8bfSgjelinek 4782ff17c8bfSgjelinek return (Z_ERR); 4783ee519a1fSgjelinek } 4784ee519a1fSgjelinek 4785ee519a1fSgjelinek } 4786ee519a1fSgjelinek 47879acbbeafSnn35248 /* 4788ff17c8bfSgjelinek * Else run the built-in attach support. 4789ff17c8bfSgjelinek * This is a no-op since there is nothing to validate. 47909acbbeafSnn35248 */ 4791ff17c8bfSgjelinek 4792ff17c8bfSgjelinek /* If dry-run or help, then we're done. */ 4793ff17c8bfSgjelinek if (!execute || brand_help) { 4794ff17c8bfSgjelinek if (!execute) 4795ff17c8bfSgjelinek (void) unlink(tmpmanifest); 4796ff17c8bfSgjelinek return (Z_OK); 47979acbbeafSnn35248 } 47989acbbeafSnn35248 4799ff17c8bfSgjelinek done: 4800ee519a1fSgjelinek zonecfg_rm_detached(handle, force); 4801ee519a1fSgjelinek 4802ee519a1fSgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 4803ee519a1fSgjelinek errno = err; 4804ee519a1fSgjelinek zperror(gettext("could not reset state"), B_TRUE); 4805ee519a1fSgjelinek } 4806ee519a1fSgjelinek 4807ee519a1fSgjelinek zonecfg_fini_handle(handle); 4808ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 4809ee519a1fSgjelinek 481037774979Sgjelinek /* If we have a brand postattach hook, run it. */ 4811ff17c8bfSgjelinek if (err == Z_OK && !force && postcmdbuf[0] != '\0') { 4812ff17c8bfSgjelinek status = do_subproc(postcmdbuf); 481337774979Sgjelinek if (subproc_status(gettext("brand-specific postattach"), 481437774979Sgjelinek status, B_FALSE) != ZONE_SUBPROC_OK) { 481537774979Sgjelinek if ((err = zone_set_state(target_zone, 481637774979Sgjelinek ZONE_STATE_CONFIGURED)) != Z_OK) { 481737774979Sgjelinek errno = err; 481837774979Sgjelinek zperror(gettext("could not reset state"), 481937774979Sgjelinek B_TRUE); 482037774979Sgjelinek } 482137774979Sgjelinek return (Z_ERR); 482237774979Sgjelinek } 482337774979Sgjelinek } 482437774979Sgjelinek 4825ee519a1fSgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 4826ee519a1fSgjelinek } 4827ee519a1fSgjelinek 4828865e09a4Sgjelinek /* 48297c478bd9Sstevel@tonic-gate * On input, TRUE => yes, FALSE => no. 48307c478bd9Sstevel@tonic-gate * On return, TRUE => 1, FALSE => 0, could not ask => -1. 48317c478bd9Sstevel@tonic-gate */ 48327c478bd9Sstevel@tonic-gate 48337c478bd9Sstevel@tonic-gate static int 48347c478bd9Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question) 48357c478bd9Sstevel@tonic-gate { 48367c478bd9Sstevel@tonic-gate char line[64]; /* should be large enough to answer yes or no */ 48377c478bd9Sstevel@tonic-gate 48387c478bd9Sstevel@tonic-gate if (!isatty(STDIN_FILENO)) 48397c478bd9Sstevel@tonic-gate return (-1); 48407c478bd9Sstevel@tonic-gate for (;;) { 48417c478bd9Sstevel@tonic-gate (void) printf("%s (%s)? ", question, 48427c478bd9Sstevel@tonic-gate default_answer ? "[y]/n" : "y/[n]"); 48437c478bd9Sstevel@tonic-gate if (fgets(line, sizeof (line), stdin) == NULL || 48447c478bd9Sstevel@tonic-gate line[0] == '\n') 48457c478bd9Sstevel@tonic-gate return (default_answer ? 1 : 0); 48467c478bd9Sstevel@tonic-gate if (tolower(line[0]) == 'y') 48477c478bd9Sstevel@tonic-gate return (1); 48487c478bd9Sstevel@tonic-gate if (tolower(line[0]) == 'n') 48497c478bd9Sstevel@tonic-gate return (0); 48507c478bd9Sstevel@tonic-gate } 48517c478bd9Sstevel@tonic-gate } 48527c478bd9Sstevel@tonic-gate 4853ff17c8bfSgjelinek /* ARGSUSED */ 48547c478bd9Sstevel@tonic-gate static int 48557c478bd9Sstevel@tonic-gate uninstall_func(int argc, char *argv[]) 48567c478bd9Sstevel@tonic-gate { 48577c478bd9Sstevel@tonic-gate char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */ 48580b5de56dSgjelinek char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN]; 485937774979Sgjelinek char cmdbuf[MAXPATHLEN]; 4860ff17c8bfSgjelinek char precmdbuf[MAXPATHLEN]; 48617c478bd9Sstevel@tonic-gate boolean_t force = B_FALSE; 48627c478bd9Sstevel@tonic-gate int lockfd, answer; 48637c478bd9Sstevel@tonic-gate int err, arg; 4864ff17c8bfSgjelinek boolean_t brand_help = B_FALSE; 486537774979Sgjelinek brand_handle_t bh = NULL; 4866ff17c8bfSgjelinek int status; 48677c478bd9Sstevel@tonic-gate 4868108322fbScarlsonj if (zonecfg_in_alt_root()) { 4869108322fbScarlsonj zerror(gettext("cannot uninstall zone in alternate root")); 4870108322fbScarlsonj return (Z_ERR); 4871108322fbScarlsonj } 4872108322fbScarlsonj 4873ff17c8bfSgjelinek /* Check the argv string for args we handle internally */ 48747c478bd9Sstevel@tonic-gate optind = 0; 4875ff17c8bfSgjelinek opterr = 0; 48767c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?F")) != EOF) { 48777c478bd9Sstevel@tonic-gate switch (arg) { 48787c478bd9Sstevel@tonic-gate case '?': 4879ff17c8bfSgjelinek if (optopt == '?') { 48807c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 4881ff17c8bfSgjelinek brand_help = B_TRUE; 4882ff17c8bfSgjelinek } 4883ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 4884ff17c8bfSgjelinek break; 48857c478bd9Sstevel@tonic-gate case 'F': 48867c478bd9Sstevel@tonic-gate force = B_TRUE; 48877c478bd9Sstevel@tonic-gate break; 48887c478bd9Sstevel@tonic-gate default: 4889ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 4890ff17c8bfSgjelinek break; 48917c478bd9Sstevel@tonic-gate } 48927c478bd9Sstevel@tonic-gate } 48937c478bd9Sstevel@tonic-gate 4894ff17c8bfSgjelinek if (!brand_help) { 4895ff17c8bfSgjelinek if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE, 4896ff17c8bfSgjelinek B_FALSE) != Z_OK) 48977c478bd9Sstevel@tonic-gate return (Z_ERR); 48987c478bd9Sstevel@tonic-gate 4899ce28b40eSzt129084 /* 4900ce28b40eSzt129084 * Invoke brand-specific handler. 4901ce28b40eSzt129084 */ 4902ce28b40eSzt129084 if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK) 4903ce28b40eSzt129084 return (Z_ERR); 4904ce28b40eSzt129084 49057c478bd9Sstevel@tonic-gate if (!force) { 49067c478bd9Sstevel@tonic-gate (void) snprintf(line, sizeof (line), 49077c478bd9Sstevel@tonic-gate gettext("Are you sure you want to %s zone %s"), 49087c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL), target_zone); 49097c478bd9Sstevel@tonic-gate if ((answer = ask_yesno(B_FALSE, line)) == 0) { 49107c478bd9Sstevel@tonic-gate return (Z_OK); 49117c478bd9Sstevel@tonic-gate } else if (answer == -1) { 49127c478bd9Sstevel@tonic-gate zerror(gettext("Input not from terminal and -F " 49137c478bd9Sstevel@tonic-gate "not specified: %s not done."), 49147c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 49157c478bd9Sstevel@tonic-gate return (Z_ERR); 49167c478bd9Sstevel@tonic-gate } 49177c478bd9Sstevel@tonic-gate } 4918ff17c8bfSgjelinek } 49197c478bd9Sstevel@tonic-gate 49200b5de56dSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, 49210b5de56dSgjelinek sizeof (zonepath))) != Z_OK) { 49227c478bd9Sstevel@tonic-gate errno = err; 49237c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 49247c478bd9Sstevel@tonic-gate return (Z_ERR); 49257c478bd9Sstevel@tonic-gate } 4926ff17c8bfSgjelinek 4927ff17c8bfSgjelinek /* 4928ff17c8bfSgjelinek * Fetch the uninstall and preuninstall hooks from the brand 4929ff17c8bfSgjelinek * configuration. 4930ff17c8bfSgjelinek */ 4931ff17c8bfSgjelinek if ((bh = brand_open(target_brand)) == NULL) { 4932ff17c8bfSgjelinek zerror(gettext("missing or invalid brand")); 4933ff17c8bfSgjelinek return (Z_ERR); 4934ff17c8bfSgjelinek } 4935ff17c8bfSgjelinek 4936ff17c8bfSgjelinek if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_uninstall, 4937ff17c8bfSgjelinek target_zone, zonepath) != Z_OK) { 4938ff17c8bfSgjelinek zerror("invalid brand configuration: missing uninstall " 4939ff17c8bfSgjelinek "resource"); 4940ff17c8bfSgjelinek brand_close(bh); 4941ff17c8bfSgjelinek return (Z_ERR); 4942ff17c8bfSgjelinek } 4943ff17c8bfSgjelinek 4944ff17c8bfSgjelinek if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_preuninstall, 4945ff17c8bfSgjelinek target_zone, zonepath) != Z_OK) { 4946ff17c8bfSgjelinek zerror("invalid brand configuration: missing preuninstall " 4947ff17c8bfSgjelinek "resource"); 4948ff17c8bfSgjelinek brand_close(bh); 4949ff17c8bfSgjelinek return (Z_ERR); 4950ff17c8bfSgjelinek } 4951ff17c8bfSgjelinek brand_close(bh); 4952ff17c8bfSgjelinek 4953ff17c8bfSgjelinek /* Append all options to preuninstall hook. */ 4954ff17c8bfSgjelinek if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK) 4955ff17c8bfSgjelinek return (Z_ERR); 4956ff17c8bfSgjelinek 4957ff17c8bfSgjelinek /* Append all options to uninstall hook. */ 4958ff17c8bfSgjelinek if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) 4959ff17c8bfSgjelinek return (Z_ERR); 4960ff17c8bfSgjelinek 4961ff17c8bfSgjelinek if (!brand_help) { 49627c478bd9Sstevel@tonic-gate if ((err = zone_get_rootpath(target_zone, rootpath, 49637c478bd9Sstevel@tonic-gate sizeof (rootpath))) != Z_OK) { 49647c478bd9Sstevel@tonic-gate errno = err; 4965ff17c8bfSgjelinek zperror2(target_zone, gettext("could not get root " 4966ff17c8bfSgjelinek "path")); 49677c478bd9Sstevel@tonic-gate return (Z_ERR); 49687c478bd9Sstevel@tonic-gate } 49697c478bd9Sstevel@tonic-gate 49707c478bd9Sstevel@tonic-gate /* 4971ff17c8bfSgjelinek * If there seems to be a zoneadmd running for this zone, call 4972ff17c8bfSgjelinek * it to tell it that an uninstall is happening; if all goes 4973ff17c8bfSgjelinek * well it will then shut itself down. 49747c478bd9Sstevel@tonic-gate */ 4975ff17c8bfSgjelinek if (zonecfg_ping_zoneadmd(target_zone) == Z_OK) { 49767c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 49777c478bd9Sstevel@tonic-gate zarg.cmd = Z_NOTE_UNINSTALLING; 4978ff17c8bfSgjelinek /* we don't care too much if this fails, just plow on */ 4979ff17c8bfSgjelinek (void) zonecfg_call_zoneadmd(target_zone, &zarg, locale, 4980ff17c8bfSgjelinek B_TRUE); 49817c478bd9Sstevel@tonic-gate } 49827c478bd9Sstevel@tonic-gate 4983ff17c8bfSgjelinek if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) { 4984ff17c8bfSgjelinek zerror(gettext("another %s may have an operation in " 4985ff17c8bfSgjelinek "progress."), "zoneadm"); 49867c478bd9Sstevel@tonic-gate return (Z_ERR); 49877c478bd9Sstevel@tonic-gate } 49887c478bd9Sstevel@tonic-gate 49897c478bd9Sstevel@tonic-gate /* Don't uninstall the zone if anything is mounted there */ 49907c478bd9Sstevel@tonic-gate err = zonecfg_find_mounts(rootpath, NULL, NULL); 49917c478bd9Sstevel@tonic-gate if (err) { 49920b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 49937c478bd9Sstevel@tonic-gate "subdirectories of %s.\n"), rootpath); 49947c478bd9Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, zfm_print, NULL); 4995ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 49967c478bd9Sstevel@tonic-gate return (Z_ERR); 49977c478bd9Sstevel@tonic-gate } 499837774979Sgjelinek } 499937774979Sgjelinek 5000ff17c8bfSgjelinek /* If we have a brand preuninstall hook, run it. */ 5001ff17c8bfSgjelinek if (!brand_help && precmdbuf[0] != '\0') { 500237774979Sgjelinek status = do_subproc(cmdbuf); 500337774979Sgjelinek if (subproc_status(gettext("brand-specific preuninstall"), 500437774979Sgjelinek status, B_FALSE) != ZONE_SUBPROC_OK) { 5005ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 500637774979Sgjelinek return (Z_ERR); 500737774979Sgjelinek } 500837774979Sgjelinek } 500937774979Sgjelinek 5010ff17c8bfSgjelinek if (!brand_help) { 50117c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 50127c478bd9Sstevel@tonic-gate if (err != Z_OK) { 50137c478bd9Sstevel@tonic-gate errno = err; 50147c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 50157c478bd9Sstevel@tonic-gate goto bad; 50167c478bd9Sstevel@tonic-gate } 5017ff17c8bfSgjelinek } 50187c478bd9Sstevel@tonic-gate 5019ff17c8bfSgjelinek /* 5020ff17c8bfSgjelinek * If there is a brand uninstall hook, use it, otherwise use the 5021ff17c8bfSgjelinek * built-in uninstall code. 5022ff17c8bfSgjelinek */ 5023ff17c8bfSgjelinek if (cmdbuf[0] != '\0') { 5024ff17c8bfSgjelinek /* Run the uninstall hook */ 5025ff17c8bfSgjelinek status = do_subproc_interactive(cmdbuf); 5026ff17c8bfSgjelinek if ((status = subproc_status(gettext("brand-specific " 5027ff17c8bfSgjelinek "uninstall"), status, B_FALSE)) != ZONE_SUBPROC_OK) { 5028ff17c8bfSgjelinek if (status == ZONE_SUBPROC_USAGE && !brand_help) 5029ff17c8bfSgjelinek sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 5030ff17c8bfSgjelinek if (!brand_help) 5031ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 5032ff17c8bfSgjelinek return (Z_ERR); 5033ff17c8bfSgjelinek } 5034ff17c8bfSgjelinek 5035ff17c8bfSgjelinek if (brand_help) 5036ff17c8bfSgjelinek return (Z_OK); 5037ff17c8bfSgjelinek } else { 5038ff17c8bfSgjelinek /* If just help, we're done since there is no brand help. */ 5039ff17c8bfSgjelinek if (brand_help) 5040ff17c8bfSgjelinek return (Z_OK); 5041ff17c8bfSgjelinek 5042ff17c8bfSgjelinek /* Run the built-in uninstall support. */ 50430b5de56dSgjelinek if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 50440b5de56dSgjelinek errno = err; 5045ff17c8bfSgjelinek zperror2(target_zone, gettext("cleaning up zonepath " 5046ff17c8bfSgjelinek "failed")); 50477c478bd9Sstevel@tonic-gate goto bad; 50480b5de56dSgjelinek } 5049ff17c8bfSgjelinek } 50500b5de56dSgjelinek 50517c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 50527c478bd9Sstevel@tonic-gate if (err != Z_OK) { 50537c478bd9Sstevel@tonic-gate errno = err; 50547c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not reset state")); 50557c478bd9Sstevel@tonic-gate } 50567c478bd9Sstevel@tonic-gate bad: 5057ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 50587c478bd9Sstevel@tonic-gate return (err); 50597c478bd9Sstevel@tonic-gate } 50607c478bd9Sstevel@tonic-gate 5061108322fbScarlsonj /* ARGSUSED */ 5062108322fbScarlsonj static int 5063108322fbScarlsonj mount_func(int argc, char *argv[]) 5064108322fbScarlsonj { 5065108322fbScarlsonj zone_cmd_arg_t zarg; 50669acbbeafSnn35248 boolean_t force = B_FALSE; 50679acbbeafSnn35248 int arg; 5068108322fbScarlsonj 50699acbbeafSnn35248 /* 50709acbbeafSnn35248 * The only supported subargument to the "mount" subcommand is 50719acbbeafSnn35248 * "-f", which forces us to mount a zone in the INCOMPLETE state. 50729acbbeafSnn35248 */ 50739acbbeafSnn35248 optind = 0; 50749acbbeafSnn35248 if ((arg = getopt(argc, argv, "f")) != EOF) { 50759acbbeafSnn35248 switch (arg) { 50769acbbeafSnn35248 case 'f': 50779acbbeafSnn35248 force = B_TRUE; 50789acbbeafSnn35248 break; 50799acbbeafSnn35248 default: 5080108322fbScarlsonj return (Z_USAGE); 50819acbbeafSnn35248 } 50829acbbeafSnn35248 } 50839acbbeafSnn35248 if (argc > optind) 50849acbbeafSnn35248 return (Z_USAGE); 50859acbbeafSnn35248 50869acbbeafSnn35248 if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force) 50879acbbeafSnn35248 != Z_OK) 5088108322fbScarlsonj return (Z_ERR); 5089ce28b40eSzt129084 if (verify_details(CMD_MOUNT, argv) != Z_OK) 5090108322fbScarlsonj return (Z_ERR); 5091108322fbScarlsonj 50929acbbeafSnn35248 zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT; 50936cfd72c6Sgjelinek zarg.bootbuf[0] = '\0'; 5094ff17c8bfSgjelinek if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) { 5095108322fbScarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 5096108322fbScarlsonj return (Z_ERR); 5097108322fbScarlsonj } 5098108322fbScarlsonj return (Z_OK); 5099108322fbScarlsonj } 5100108322fbScarlsonj 5101108322fbScarlsonj /* ARGSUSED */ 5102108322fbScarlsonj static int 5103108322fbScarlsonj unmount_func(int argc, char *argv[]) 5104108322fbScarlsonj { 5105108322fbScarlsonj zone_cmd_arg_t zarg; 5106108322fbScarlsonj 5107108322fbScarlsonj if (argc > 0) 5108108322fbScarlsonj return (Z_USAGE); 51099acbbeafSnn35248 if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE) 51109acbbeafSnn35248 != Z_OK) 5111108322fbScarlsonj return (Z_ERR); 5112108322fbScarlsonj 5113108322fbScarlsonj zarg.cmd = Z_UNMOUNT; 5114ff17c8bfSgjelinek if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) { 5115108322fbScarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 5116108322fbScarlsonj return (Z_ERR); 5117108322fbScarlsonj } 5118108322fbScarlsonj return (Z_OK); 5119108322fbScarlsonj } 5120108322fbScarlsonj 51217c478bd9Sstevel@tonic-gate static int 5122555afedfScarlsonj mark_func(int argc, char *argv[]) 5123555afedfScarlsonj { 5124555afedfScarlsonj int err, lockfd; 5125555afedfScarlsonj 5126555afedfScarlsonj if (argc != 1 || strcmp(argv[0], "incomplete") != 0) 5127555afedfScarlsonj return (Z_USAGE); 51289acbbeafSnn35248 if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_FALSE, B_FALSE) 51299acbbeafSnn35248 != Z_OK) 5130555afedfScarlsonj return (Z_ERR); 5131555afedfScarlsonj 5132ce28b40eSzt129084 /* 5133ce28b40eSzt129084 * Invoke brand-specific handler. 5134ce28b40eSzt129084 */ 5135ce28b40eSzt129084 if (invoke_brand_handler(CMD_MARK, argv) != Z_OK) 5136ce28b40eSzt129084 return (Z_ERR); 5137ce28b40eSzt129084 5138ff17c8bfSgjelinek if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) { 5139555afedfScarlsonj zerror(gettext("another %s may have an operation in progress."), 5140555afedfScarlsonj "zoneadm"); 5141555afedfScarlsonj return (Z_ERR); 5142555afedfScarlsonj } 5143555afedfScarlsonj 5144555afedfScarlsonj err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 5145555afedfScarlsonj if (err != Z_OK) { 5146555afedfScarlsonj errno = err; 5147555afedfScarlsonj zperror2(target_zone, gettext("could not set state")); 5148555afedfScarlsonj } 5149ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 5150555afedfScarlsonj 5151555afedfScarlsonj return (err); 5152555afedfScarlsonj } 5153555afedfScarlsonj 51540209230bSgjelinek /* 51550209230bSgjelinek * Check what scheduling class we're running under and print a warning if 51560209230bSgjelinek * we're not using FSS. 51570209230bSgjelinek */ 51580209230bSgjelinek static int 51590209230bSgjelinek check_sched_fss(zone_dochandle_t handle) 51600209230bSgjelinek { 51610209230bSgjelinek char class_name[PC_CLNMSZ]; 51620209230bSgjelinek 51630209230bSgjelinek if (zonecfg_get_dflt_sched_class(handle, class_name, 51640209230bSgjelinek sizeof (class_name)) != Z_OK) { 51650209230bSgjelinek zerror(gettext("WARNING: unable to determine the zone's " 51660209230bSgjelinek "scheduling class")); 51670209230bSgjelinek } else if (strcmp("FSS", class_name) != 0) { 51680209230bSgjelinek zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n" 51690209230bSgjelinek "FSS is not the default scheduling class for this zone. " 51700209230bSgjelinek "FSS will be\nused for processes in the zone but to get " 51710209230bSgjelinek "the full benefit of FSS,\nit should be the default " 51720209230bSgjelinek "scheduling class. See dispadmin(1M) for\nmore details.")); 51730209230bSgjelinek return (Z_SYSTEM); 51740209230bSgjelinek } 51750209230bSgjelinek 51760209230bSgjelinek return (Z_OK); 51770209230bSgjelinek } 51780209230bSgjelinek 51790209230bSgjelinek static int 51800209230bSgjelinek check_cpu_shares_sched(zone_dochandle_t handle) 51810209230bSgjelinek { 51820209230bSgjelinek int err; 51830209230bSgjelinek int res = Z_OK; 51840209230bSgjelinek struct zone_rctltab rctl; 51850209230bSgjelinek 51860209230bSgjelinek if ((err = zonecfg_setrctlent(handle)) != Z_OK) { 51870209230bSgjelinek errno = err; 51880209230bSgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 51890209230bSgjelinek return (err); 51900209230bSgjelinek } 51910209230bSgjelinek 51920209230bSgjelinek while (zonecfg_getrctlent(handle, &rctl) == Z_OK) { 51930209230bSgjelinek if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) { 51940209230bSgjelinek if (check_sched_fss(handle) != Z_OK) 51950209230bSgjelinek res = Z_SYSTEM; 51960209230bSgjelinek break; 51970209230bSgjelinek } 51980209230bSgjelinek } 51990209230bSgjelinek 52000209230bSgjelinek (void) zonecfg_endrctlent(handle); 52010209230bSgjelinek 52020209230bSgjelinek return (res); 52030209230bSgjelinek } 52040209230bSgjelinek 52050209230bSgjelinek /* 52067ef01d19Sgjelinek * Check if there is a mix of processes running in different pools within the 52077ef01d19Sgjelinek * zone. This is currently only going to be called for the global zone from 52087ef01d19Sgjelinek * apply_func but that could be generalized in the future. 52097ef01d19Sgjelinek */ 52107ef01d19Sgjelinek static boolean_t 52117ef01d19Sgjelinek mixed_pools(zoneid_t zoneid) 52127ef01d19Sgjelinek { 52137ef01d19Sgjelinek DIR *dirp; 52147ef01d19Sgjelinek dirent_t *dent; 52157ef01d19Sgjelinek boolean_t mixed = B_FALSE; 52167ef01d19Sgjelinek boolean_t poolid_set = B_FALSE; 52177ef01d19Sgjelinek poolid_t last_poolid = 0; 52187ef01d19Sgjelinek 52197ef01d19Sgjelinek if ((dirp = opendir("/proc")) == NULL) { 52207ef01d19Sgjelinek zerror(gettext("could not open /proc")); 52217ef01d19Sgjelinek return (B_FALSE); 52227ef01d19Sgjelinek } 52237ef01d19Sgjelinek 52247ef01d19Sgjelinek while ((dent = readdir(dirp)) != NULL) { 52257ef01d19Sgjelinek int procfd; 52267ef01d19Sgjelinek psinfo_t ps; 52277ef01d19Sgjelinek char procpath[MAXPATHLEN]; 52287ef01d19Sgjelinek 52297ef01d19Sgjelinek if (dent->d_name[0] == '.') 52307ef01d19Sgjelinek continue; 52317ef01d19Sgjelinek 52327ef01d19Sgjelinek (void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo", 52337ef01d19Sgjelinek dent->d_name); 52347ef01d19Sgjelinek 52357ef01d19Sgjelinek if ((procfd = open(procpath, O_RDONLY)) == -1) 52367ef01d19Sgjelinek continue; 52377ef01d19Sgjelinek 52387ef01d19Sgjelinek if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) { 52397ef01d19Sgjelinek /* skip processes in other zones and system processes */ 52407ef01d19Sgjelinek if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) { 52417ef01d19Sgjelinek (void) close(procfd); 52427ef01d19Sgjelinek continue; 52437ef01d19Sgjelinek } 52447ef01d19Sgjelinek 52457ef01d19Sgjelinek if (poolid_set) { 52467ef01d19Sgjelinek if (ps.pr_poolid != last_poolid) 52477ef01d19Sgjelinek mixed = B_TRUE; 52487ef01d19Sgjelinek } else { 52497ef01d19Sgjelinek last_poolid = ps.pr_poolid; 52507ef01d19Sgjelinek poolid_set = B_TRUE; 52517ef01d19Sgjelinek } 52527ef01d19Sgjelinek } 52537ef01d19Sgjelinek 52547ef01d19Sgjelinek (void) close(procfd); 52557ef01d19Sgjelinek 52567ef01d19Sgjelinek if (mixed) 52577ef01d19Sgjelinek break; 52587ef01d19Sgjelinek } 52597ef01d19Sgjelinek 52607ef01d19Sgjelinek (void) closedir(dirp); 52617ef01d19Sgjelinek 52627ef01d19Sgjelinek return (mixed); 52637ef01d19Sgjelinek } 52647ef01d19Sgjelinek 52657ef01d19Sgjelinek /* 52667ef01d19Sgjelinek * Check if a persistent or temporary pool is configured for the zone. 52677ef01d19Sgjelinek * This is currently only going to be called for the global zone from 52687ef01d19Sgjelinek * apply_func but that could be generalized in the future. 52697ef01d19Sgjelinek */ 52707ef01d19Sgjelinek static boolean_t 52717ef01d19Sgjelinek pool_configured(zone_dochandle_t handle) 52727ef01d19Sgjelinek { 52737ef01d19Sgjelinek int err1, err2; 52747ef01d19Sgjelinek struct zone_psettab pset_tab; 52757ef01d19Sgjelinek char poolname[MAXPATHLEN]; 52767ef01d19Sgjelinek 52777ef01d19Sgjelinek err1 = zonecfg_lookup_pset(handle, &pset_tab); 52787ef01d19Sgjelinek err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 52797ef01d19Sgjelinek 52807ef01d19Sgjelinek if (err1 == Z_NO_ENTRY && 52817ef01d19Sgjelinek (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0))) 52827ef01d19Sgjelinek return (B_FALSE); 52837ef01d19Sgjelinek 52847ef01d19Sgjelinek return (B_TRUE); 52857ef01d19Sgjelinek } 52867ef01d19Sgjelinek 52877ef01d19Sgjelinek /* 52880209230bSgjelinek * This is an undocumented interface which is currently only used to apply 52890209230bSgjelinek * the global zone resource management settings when the system boots. 52900209230bSgjelinek * This function does not yet properly handle updating a running system so 52910209230bSgjelinek * any projects running in the zone would be trashed if this function 52920209230bSgjelinek * were to run after the zone had booted. It also does not reset any 52930209230bSgjelinek * rctl settings that were removed from zonecfg. There is still work to be 52940209230bSgjelinek * done before we can properly support dynamically updating the resource 52950209230bSgjelinek * management settings for a running zone (global or non-global). Thus, this 52960209230bSgjelinek * functionality is undocumented for now. 52970209230bSgjelinek */ 52980209230bSgjelinek /* ARGSUSED */ 52990209230bSgjelinek static int 53000209230bSgjelinek apply_func(int argc, char *argv[]) 53010209230bSgjelinek { 53020209230bSgjelinek int err; 53030209230bSgjelinek int res = Z_OK; 53040209230bSgjelinek priv_set_t *privset; 53050209230bSgjelinek zoneid_t zoneid; 53060209230bSgjelinek zone_dochandle_t handle; 53070209230bSgjelinek struct zone_mcaptab mcap; 53080209230bSgjelinek char pool_err[128]; 53090209230bSgjelinek 53100209230bSgjelinek zoneid = getzoneid(); 53110209230bSgjelinek 53120209230bSgjelinek if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID || 53130209230bSgjelinek target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0) 53140209230bSgjelinek return (usage(B_FALSE)); 53150209230bSgjelinek 53160209230bSgjelinek if ((privset = priv_allocset()) == NULL) { 53170209230bSgjelinek zerror(gettext("%s failed"), "priv_allocset"); 53180209230bSgjelinek return (Z_ERR); 53190209230bSgjelinek } 53200209230bSgjelinek 53210209230bSgjelinek if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 53220209230bSgjelinek zerror(gettext("%s failed"), "getppriv"); 53230209230bSgjelinek priv_freeset(privset); 53240209230bSgjelinek return (Z_ERR); 53250209230bSgjelinek } 53260209230bSgjelinek 53270209230bSgjelinek if (priv_isfullset(privset) == B_FALSE) { 53280209230bSgjelinek (void) usage(B_FALSE); 53290209230bSgjelinek priv_freeset(privset); 53300209230bSgjelinek return (Z_ERR); 53310209230bSgjelinek } 53320209230bSgjelinek priv_freeset(privset); 53330209230bSgjelinek 53340209230bSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 53350209230bSgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 53360209230bSgjelinek return (Z_ERR); 53370209230bSgjelinek } 53380209230bSgjelinek 53390209230bSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 53400209230bSgjelinek errno = err; 53410209230bSgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 53420209230bSgjelinek zonecfg_fini_handle(handle); 53430209230bSgjelinek return (Z_ERR); 53440209230bSgjelinek } 53450209230bSgjelinek 53460209230bSgjelinek /* specific error msgs are printed within apply_rctls */ 53470209230bSgjelinek if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) { 53480209230bSgjelinek errno = err; 53490209230bSgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 53500209230bSgjelinek res = Z_ERR; 53510209230bSgjelinek } 53520209230bSgjelinek 53530209230bSgjelinek if ((err = check_cpu_shares_sched(handle)) != Z_OK) 53540209230bSgjelinek res = Z_ERR; 53550209230bSgjelinek 53567ef01d19Sgjelinek if (pool_configured(handle)) { 53577ef01d19Sgjelinek if (mixed_pools(zoneid)) { 53587ef01d19Sgjelinek zerror(gettext("Zone is using multiple resource " 53597ef01d19Sgjelinek "pools. The pool\nconfiguration cannot be " 53607ef01d19Sgjelinek "applied without rebooting.")); 53617ef01d19Sgjelinek res = Z_ERR; 53627ef01d19Sgjelinek } else { 53637ef01d19Sgjelinek 53640209230bSgjelinek /* 53657ef01d19Sgjelinek * The next two blocks of code attempt to set up 53667ef01d19Sgjelinek * temporary pools as well as persistent pools. In 53677ef01d19Sgjelinek * both cases we call the functions unconditionally. 53687ef01d19Sgjelinek * Within each funtion the code will check if the zone 53697ef01d19Sgjelinek * is actually configured for a temporary pool or 53707ef01d19Sgjelinek * persistent pool and just return if there is nothing 53717ef01d19Sgjelinek * to do. 53720209230bSgjelinek */ 53737ef01d19Sgjelinek if ((err = zonecfg_bind_tmp_pool(handle, zoneid, 53747ef01d19Sgjelinek pool_err, sizeof (pool_err))) != Z_OK) { 53757ef01d19Sgjelinek if (err == Z_POOL || err == Z_POOL_CREATE || 53767ef01d19Sgjelinek err == Z_POOL_BIND) 53777ef01d19Sgjelinek zerror("%s: %s", zonecfg_strerror(err), 53787ef01d19Sgjelinek pool_err); 53790209230bSgjelinek else 53807ef01d19Sgjelinek zerror(gettext("could not bind zone to " 53817ef01d19Sgjelinek "temporary pool: %s"), 53827ef01d19Sgjelinek zonecfg_strerror(err)); 53830209230bSgjelinek res = Z_ERR; 53840209230bSgjelinek } 53850209230bSgjelinek 53860209230bSgjelinek if ((err = zonecfg_bind_pool(handle, zoneid, pool_err, 53870209230bSgjelinek sizeof (pool_err))) != Z_OK) { 53880209230bSgjelinek if (err == Z_POOL || err == Z_POOL_BIND) 53897ef01d19Sgjelinek zerror("%s: %s", zonecfg_strerror(err), 53907ef01d19Sgjelinek pool_err); 53910209230bSgjelinek else 53920209230bSgjelinek zerror("%s", zonecfg_strerror(err)); 53930209230bSgjelinek } 53947ef01d19Sgjelinek } 53957ef01d19Sgjelinek } 53960209230bSgjelinek 53970209230bSgjelinek /* 53980209230bSgjelinek * If a memory cap is configured, set the cap in the kernel using 53990209230bSgjelinek * zone_setattr() and make sure the rcapd SMF service is enabled. 54000209230bSgjelinek */ 54010209230bSgjelinek if (zonecfg_getmcapent(handle, &mcap) == Z_OK) { 54020209230bSgjelinek uint64_t num; 54030209230bSgjelinek char smf_err[128]; 54040209230bSgjelinek 54050209230bSgjelinek num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10); 54060209230bSgjelinek if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) { 54070209230bSgjelinek zerror(gettext("could not set zone memory cap")); 54080209230bSgjelinek res = Z_ERR; 54090209230bSgjelinek } 54100209230bSgjelinek 54110209230bSgjelinek if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) { 54120209230bSgjelinek zerror(gettext("enabling system/rcap service failed: " 54130209230bSgjelinek "%s"), smf_err); 54140209230bSgjelinek res = Z_ERR; 54150209230bSgjelinek } 54160209230bSgjelinek } 54170209230bSgjelinek 54180209230bSgjelinek zonecfg_fini_handle(handle); 54190209230bSgjelinek 54200209230bSgjelinek return (res); 54210209230bSgjelinek } 54220209230bSgjelinek 5423555afedfScarlsonj static int 54247c478bd9Sstevel@tonic-gate help_func(int argc, char *argv[]) 54257c478bd9Sstevel@tonic-gate { 54267c478bd9Sstevel@tonic-gate int arg, cmd_num; 54277c478bd9Sstevel@tonic-gate 54287c478bd9Sstevel@tonic-gate if (argc == 0) { 54297c478bd9Sstevel@tonic-gate (void) usage(B_TRUE); 54307c478bd9Sstevel@tonic-gate return (Z_OK); 54317c478bd9Sstevel@tonic-gate } 54327c478bd9Sstevel@tonic-gate optind = 0; 54337c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 54347c478bd9Sstevel@tonic-gate switch (arg) { 54357c478bd9Sstevel@tonic-gate case '?': 54367c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 54377c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 54387c478bd9Sstevel@tonic-gate default: 54397c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 54407c478bd9Sstevel@tonic-gate return (Z_USAGE); 54417c478bd9Sstevel@tonic-gate } 54427c478bd9Sstevel@tonic-gate } 54437c478bd9Sstevel@tonic-gate while (optind < argc) { 5444394a25e2Scarlsonj /* Private commands have NULL short_usage; omit them */ 5445394a25e2Scarlsonj if ((cmd_num = cmd_match(argv[optind])) < 0 || 5446394a25e2Scarlsonj cmdtab[cmd_num].short_usage == NULL) { 54477c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 54487c478bd9Sstevel@tonic-gate return (Z_USAGE); 54497c478bd9Sstevel@tonic-gate } 54507c478bd9Sstevel@tonic-gate sub_usage(cmdtab[cmd_num].short_usage, cmd_num); 54517c478bd9Sstevel@tonic-gate optind++; 54527c478bd9Sstevel@tonic-gate } 54537c478bd9Sstevel@tonic-gate return (Z_OK); 54547c478bd9Sstevel@tonic-gate } 54557c478bd9Sstevel@tonic-gate 54567c478bd9Sstevel@tonic-gate /* 54577c478bd9Sstevel@tonic-gate * Returns: CMD_MIN thru CMD_MAX on success, -1 on error 54587c478bd9Sstevel@tonic-gate */ 54597c478bd9Sstevel@tonic-gate 54607c478bd9Sstevel@tonic-gate static int 54617c478bd9Sstevel@tonic-gate cmd_match(char *cmd) 54627c478bd9Sstevel@tonic-gate { 54637c478bd9Sstevel@tonic-gate int i; 54647c478bd9Sstevel@tonic-gate 54657c478bd9Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 54667c478bd9Sstevel@tonic-gate /* return only if there is an exact match */ 54677c478bd9Sstevel@tonic-gate if (strcmp(cmd, cmdtab[i].cmd_name) == 0) 54687c478bd9Sstevel@tonic-gate return (cmdtab[i].cmd_num); 54697c478bd9Sstevel@tonic-gate } 54707c478bd9Sstevel@tonic-gate return (-1); 54717c478bd9Sstevel@tonic-gate } 54727c478bd9Sstevel@tonic-gate 54737c478bd9Sstevel@tonic-gate static int 54747c478bd9Sstevel@tonic-gate parse_and_run(int argc, char *argv[]) 54757c478bd9Sstevel@tonic-gate { 54767c478bd9Sstevel@tonic-gate int i = cmd_match(argv[0]); 54777c478bd9Sstevel@tonic-gate 54787c478bd9Sstevel@tonic-gate if (i < 0) 54797c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 54807c478bd9Sstevel@tonic-gate return (cmdtab[i].handler(argc - 1, &(argv[1]))); 54817c478bd9Sstevel@tonic-gate } 54827c478bd9Sstevel@tonic-gate 54837c478bd9Sstevel@tonic-gate static char * 54847c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname) 54857c478bd9Sstevel@tonic-gate { 54867c478bd9Sstevel@tonic-gate char *last_slash, *execbasename; 54877c478bd9Sstevel@tonic-gate 54887c478bd9Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 54897c478bd9Sstevel@tonic-gate for (;;) { 54907c478bd9Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 54917c478bd9Sstevel@tonic-gate if (last_slash == NULL) { 54927c478bd9Sstevel@tonic-gate execbasename = execfullname; 54937c478bd9Sstevel@tonic-gate break; 54947c478bd9Sstevel@tonic-gate } else { 54957c478bd9Sstevel@tonic-gate execbasename = last_slash + 1; 54967c478bd9Sstevel@tonic-gate if (*execbasename == '\0') { 54977c478bd9Sstevel@tonic-gate *last_slash = '\0'; 54987c478bd9Sstevel@tonic-gate continue; 54997c478bd9Sstevel@tonic-gate } 55007c478bd9Sstevel@tonic-gate break; 55017c478bd9Sstevel@tonic-gate } 55027c478bd9Sstevel@tonic-gate } 55037c478bd9Sstevel@tonic-gate return (execbasename); 55047c478bd9Sstevel@tonic-gate } 55057c478bd9Sstevel@tonic-gate 55067c478bd9Sstevel@tonic-gate int 55077c478bd9Sstevel@tonic-gate main(int argc, char **argv) 55087c478bd9Sstevel@tonic-gate { 55097c478bd9Sstevel@tonic-gate int arg; 55107c478bd9Sstevel@tonic-gate zoneid_t zid; 5511108322fbScarlsonj struct stat st; 55129acbbeafSnn35248 char *zone_lock_env; 55139acbbeafSnn35248 int err; 55147c478bd9Sstevel@tonic-gate 55157c478bd9Sstevel@tonic-gate if ((locale = setlocale(LC_ALL, "")) == NULL) 55167c478bd9Sstevel@tonic-gate locale = "C"; 55177c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 55187c478bd9Sstevel@tonic-gate setbuf(stdout, NULL); 55197c478bd9Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_IGN); 55207c478bd9Sstevel@tonic-gate execname = get_execbasename(argv[0]); 55217c478bd9Sstevel@tonic-gate target_zone = NULL; 55227c478bd9Sstevel@tonic-gate if (chdir("/") != 0) { 55237c478bd9Sstevel@tonic-gate zerror(gettext("could not change directory to /.")); 55247c478bd9Sstevel@tonic-gate exit(Z_ERR); 55257c478bd9Sstevel@tonic-gate } 55267c478bd9Sstevel@tonic-gate 552799653d4eSeschrock if (init_zfs() != Z_OK) 552899653d4eSeschrock exit(Z_ERR); 552999653d4eSeschrock 5530555afedfScarlsonj while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) { 55317c478bd9Sstevel@tonic-gate switch (arg) { 55327c478bd9Sstevel@tonic-gate case '?': 55337c478bd9Sstevel@tonic-gate return (usage(B_TRUE)); 5534555afedfScarlsonj case 'u': 5535555afedfScarlsonj target_uuid = optarg; 5536555afedfScarlsonj break; 55377c478bd9Sstevel@tonic-gate case 'z': 55387c478bd9Sstevel@tonic-gate target_zone = optarg; 55397c478bd9Sstevel@tonic-gate break; 5540108322fbScarlsonj case 'R': /* private option for admin/install use */ 5541108322fbScarlsonj if (*optarg != '/') { 5542108322fbScarlsonj zerror(gettext("root path must be absolute.")); 5543108322fbScarlsonj exit(Z_ERR); 5544108322fbScarlsonj } 5545108322fbScarlsonj if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) { 5546108322fbScarlsonj zerror( 5547108322fbScarlsonj gettext("root path must be a directory.")); 5548108322fbScarlsonj exit(Z_ERR); 5549108322fbScarlsonj } 5550108322fbScarlsonj zonecfg_set_root(optarg); 5551108322fbScarlsonj break; 55527c478bd9Sstevel@tonic-gate default: 55537c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 55547c478bd9Sstevel@tonic-gate } 55557c478bd9Sstevel@tonic-gate } 55567c478bd9Sstevel@tonic-gate 55577c478bd9Sstevel@tonic-gate if (optind >= argc) 55587c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 5559555afedfScarlsonj 5560555afedfScarlsonj if (target_uuid != NULL && *target_uuid != '\0') { 5561555afedfScarlsonj uuid_t uuid; 5562555afedfScarlsonj static char newtarget[ZONENAME_MAX]; 5563555afedfScarlsonj 5564555afedfScarlsonj if (uuid_parse(target_uuid, uuid) == -1) { 5565555afedfScarlsonj zerror(gettext("illegal UUID value specified")); 5566555afedfScarlsonj exit(Z_ERR); 5567555afedfScarlsonj } 5568555afedfScarlsonj if (zonecfg_get_name_by_uuid(uuid, newtarget, 5569555afedfScarlsonj sizeof (newtarget)) == Z_OK) 5570555afedfScarlsonj target_zone = newtarget; 5571555afedfScarlsonj } 5572555afedfScarlsonj 55737c478bd9Sstevel@tonic-gate if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) { 55747c478bd9Sstevel@tonic-gate errno = Z_NO_ZONE; 55757c478bd9Sstevel@tonic-gate zperror(target_zone, B_TRUE); 55767c478bd9Sstevel@tonic-gate exit(Z_ERR); 55777c478bd9Sstevel@tonic-gate } 55789acbbeafSnn35248 55799acbbeafSnn35248 /* 55809acbbeafSnn35248 * See if we have inherited the right to manipulate this zone from 55819acbbeafSnn35248 * a zoneadm instance in our ancestry. If so, set zone_lock_cnt to 55829acbbeafSnn35248 * indicate it. If not, make that explicit in our environment. 55839acbbeafSnn35248 */ 5584ff17c8bfSgjelinek zonecfg_init_lock_file(target_zone, &zone_lock_env); 5585ff17c8bfSgjelinek if (zone_lock_env != NULL) 55869acbbeafSnn35248 zoneadm_is_nested = B_TRUE; 55879acbbeafSnn35248 55889acbbeafSnn35248 /* 55899acbbeafSnn35248 * If we are going to be operating on a single zone, retrieve its 55909acbbeafSnn35248 * brand type and determine whether it is native or not. 55919acbbeafSnn35248 */ 55929acbbeafSnn35248 if ((target_zone != NULL) && 5593*2ad45a84Sjv227347 (strcmp(target_zone, GLOBAL_ZONENAME) != 0)) { 55949acbbeafSnn35248 if (zone_get_brand(target_zone, target_brand, 55959acbbeafSnn35248 sizeof (target_brand)) != Z_OK) { 55969acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 55979acbbeafSnn35248 exit(Z_ERR); 55989acbbeafSnn35248 } 55999acbbeafSnn35248 is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0); 56009acbbeafSnn35248 } 56019acbbeafSnn35248 56029acbbeafSnn35248 err = parse_and_run(argc - optind, &argv[optind]); 56039acbbeafSnn35248 56049acbbeafSnn35248 return (err); 56057c478bd9Sstevel@tonic-gate } 5606