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 /* 23edfa49ffS * Copyright 2009 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> 677c478bd9Sstevel@tonic-gate #include <fcntl.h> 687c478bd9Sstevel@tonic-gate #include <door.h> 697c478bd9Sstevel@tonic-gate #include <macros.h> 707c478bd9Sstevel@tonic-gate #include <libgen.h> 71865e09a4Sgjelinek #include <fnmatch.h> 72e7f3c547Sgjelinek #include <sys/modctl.h> 739acbbeafSnn35248 #include <libbrand.h> 740209230bSgjelinek #include <libscf.h> 757ef01d19Sgjelinek #include <procfs.h> 76d9e728a2Sgjelinek #include <strings.h> 777c478bd9Sstevel@tonic-gate #include <pool.h> 787c478bd9Sstevel@tonic-gate #include <sys/pool.h> 790209230bSgjelinek #include <sys/priocntl.h> 800209230bSgjelinek #include <sys/fsspriocntl.h> 812b24ab6bSSebastien Roy #include <libdladm.h> 822b24ab6bSSebastien Roy #include <libdllink.h> 837c478bd9Sstevel@tonic-gate 840b5de56dSgjelinek #include "zoneadm.h" 850b5de56dSgjelinek 867c478bd9Sstevel@tonic-gate #define MAXARGS 8 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate /* Reflects kernel zone entries */ 897c478bd9Sstevel@tonic-gate typedef struct zone_entry { 907c478bd9Sstevel@tonic-gate zoneid_t zid; 917c478bd9Sstevel@tonic-gate char zname[ZONENAME_MAX]; 927c478bd9Sstevel@tonic-gate char *zstate_str; 937c478bd9Sstevel@tonic-gate zone_state_t zstate_num; 949acbbeafSnn35248 char zbrand[MAXNAMELEN]; 957c478bd9Sstevel@tonic-gate char zroot[MAXPATHLEN]; 96555afedfScarlsonj char zuuid[UUID_PRINTABLE_STRING_LENGTH]; 97f4b3ec61Sdh155122 zone_iptype_t ziptype; 987c478bd9Sstevel@tonic-gate } zone_entry_t; 997c478bd9Sstevel@tonic-gate 10084561e8cStd153743 #define CLUSTER_BRAND_NAME "cluster" 10184561e8cStd153743 1027c478bd9Sstevel@tonic-gate static zone_entry_t *zents; 1037c478bd9Sstevel@tonic-gate static size_t nzents; 1047c478bd9Sstevel@tonic-gate 1051390a385Sgjelinek #define LOOPBACK_IF "lo0" 1061390a385Sgjelinek #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af)) 1071390a385Sgjelinek 1081390a385Sgjelinek struct net_if { 1091390a385Sgjelinek char *name; 1101390a385Sgjelinek int af; 1111390a385Sgjelinek }; 1121390a385Sgjelinek 1137c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */ 1147c478bd9Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 1157c478bd9Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate struct cmd { 1187c478bd9Sstevel@tonic-gate uint_t cmd_num; /* command number */ 1197c478bd9Sstevel@tonic-gate char *cmd_name; /* command name */ 1207c478bd9Sstevel@tonic-gate char *short_usage; /* short form help */ 1217c478bd9Sstevel@tonic-gate int (*handler)(int argc, char *argv[]); /* function to call */ 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate }; 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate #define SHELP_HELP "help" 1263f2f09c1Sdp #define SHELP_BOOT "boot [-- boot_arguments]" 1277c478bd9Sstevel@tonic-gate #define SHELP_HALT "halt" 1287c478bd9Sstevel@tonic-gate #define SHELP_READY "ready" 1293f2f09c1Sdp #define SHELP_REBOOT "reboot [-- boot_arguments]" 1307c478bd9Sstevel@tonic-gate #define SHELP_LIST "list [-cipv]" 1317c478bd9Sstevel@tonic-gate #define SHELP_VERIFY "verify" 1329acbbeafSnn35248 #define SHELP_INSTALL "install [-x nodataset] [brand-specific args]" 133ff17c8bfSgjelinek #define SHELP_UNINSTALL "uninstall [-F] [brand-specific args]" 134ff17c8bfSgjelinek #define SHELP_CLONE "clone [-m method] [-s <ZFS snapshot>] "\ 135ff17c8bfSgjelinek "[brand-specific args] zonename" 136865e09a4Sgjelinek #define SHELP_MOVE "move zonepath" 137ff17c8bfSgjelinek #define SHELP_DETACH "detach [-n] [brand-specific args]" 138ff17c8bfSgjelinek #define SHELP_ATTACH "attach [-F] [-n <path>] [brand-specific args]" 139555afedfScarlsonj #define SHELP_MARK "mark incomplete" 1407c478bd9Sstevel@tonic-gate 1419acbbeafSnn35248 #define EXEC_PREFIX "exec " 1429acbbeafSnn35248 #define EXEC_LEN (strlen(EXEC_PREFIX)) 1439acbbeafSnn35248 #define RMCOMMAND "/usr/bin/rm -rf" 1449acbbeafSnn35248 1459acbbeafSnn35248 static int cleanup_zonepath(char *, boolean_t); 1469acbbeafSnn35248 147f4b3ec61Sdh155122 1487c478bd9Sstevel@tonic-gate static int help_func(int argc, char *argv[]); 1497c478bd9Sstevel@tonic-gate static int ready_func(int argc, char *argv[]); 1507c478bd9Sstevel@tonic-gate static int boot_func(int argc, char *argv[]); 1517c478bd9Sstevel@tonic-gate static int halt_func(int argc, char *argv[]); 1527c478bd9Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]); 1537c478bd9Sstevel@tonic-gate static int list_func(int argc, char *argv[]); 1547c478bd9Sstevel@tonic-gate static int verify_func(int argc, char *argv[]); 1557c478bd9Sstevel@tonic-gate static int install_func(int argc, char *argv[]); 1567c478bd9Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]); 157108322fbScarlsonj static int mount_func(int argc, char *argv[]); 158108322fbScarlsonj static int unmount_func(int argc, char *argv[]); 159865e09a4Sgjelinek static int clone_func(int argc, char *argv[]); 160865e09a4Sgjelinek static int move_func(int argc, char *argv[]); 161ee519a1fSgjelinek static int detach_func(int argc, char *argv[]); 162ee519a1fSgjelinek static int attach_func(int argc, char *argv[]); 163555afedfScarlsonj static int mark_func(int argc, char *argv[]); 1640209230bSgjelinek static int apply_func(int argc, char *argv[]); 165fbbfbc6eSjv227347 static int sysboot_func(int argc, char *argv[]); 1667c478bd9Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running, 1679acbbeafSnn35248 boolean_t unsafe_when_running, boolean_t force); 1687c478bd9Sstevel@tonic-gate static int cmd_match(char *cmd); 169ce28b40eSzt129084 static int verify_details(int, char *argv[]); 170ce28b40eSzt129084 static int verify_brand(zone_dochandle_t, int, char *argv[]); 171ce28b40eSzt129084 static int invoke_brand_handler(int, char *argv[]); 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate static struct cmd cmdtab[] = { 1747c478bd9Sstevel@tonic-gate { CMD_HELP, "help", SHELP_HELP, help_func }, 1757c478bd9Sstevel@tonic-gate { CMD_BOOT, "boot", SHELP_BOOT, boot_func }, 1767c478bd9Sstevel@tonic-gate { CMD_HALT, "halt", SHELP_HALT, halt_func }, 1777c478bd9Sstevel@tonic-gate { CMD_READY, "ready", SHELP_READY, ready_func }, 1787c478bd9Sstevel@tonic-gate { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func }, 1797c478bd9Sstevel@tonic-gate { CMD_LIST, "list", SHELP_LIST, list_func }, 1807c478bd9Sstevel@tonic-gate { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func }, 1817c478bd9Sstevel@tonic-gate { CMD_INSTALL, "install", SHELP_INSTALL, install_func }, 1827c478bd9Sstevel@tonic-gate { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL, 183108322fbScarlsonj uninstall_func }, 184865e09a4Sgjelinek /* mount and unmount are private commands for admin/install */ 185108322fbScarlsonj { CMD_MOUNT, "mount", NULL, mount_func }, 186865e09a4Sgjelinek { CMD_UNMOUNT, "unmount", NULL, unmount_func }, 187865e09a4Sgjelinek { CMD_CLONE, "clone", SHELP_CLONE, clone_func }, 188ee519a1fSgjelinek { CMD_MOVE, "move", SHELP_MOVE, move_func }, 189ee519a1fSgjelinek { CMD_DETACH, "detach", SHELP_DETACH, detach_func }, 190555afedfScarlsonj { CMD_ATTACH, "attach", SHELP_ATTACH, attach_func }, 1910209230bSgjelinek { CMD_MARK, "mark", SHELP_MARK, mark_func }, 192fbbfbc6eSjv227347 { CMD_APPLY, "apply", NULL, apply_func }, 193fbbfbc6eSjv227347 { CMD_SYSBOOT, "sysboot", NULL, sysboot_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]; 201*e5816e35SEdward Pilatowicz static char default_brand[MAXPATHLEN]; 2027c478bd9Sstevel@tonic-gate static char *locale; 2030b5de56dSgjelinek char *target_zone; 204555afedfScarlsonj static char *target_uuid; 2057c478bd9Sstevel@tonic-gate 2060b5de56dSgjelinek char * 2077c478bd9Sstevel@tonic-gate cmd_to_str(int cmd_num) 2087c478bd9Sstevel@tonic-gate { 2097c478bd9Sstevel@tonic-gate assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2107c478bd9Sstevel@tonic-gate return (cmdtab[cmd_num].cmd_name); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */ 2147c478bd9Sstevel@tonic-gate static char * 2157c478bd9Sstevel@tonic-gate long_help(int cmd_num) 2167c478bd9Sstevel@tonic-gate { 2177e362f58Scomay assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2187c478bd9Sstevel@tonic-gate switch (cmd_num) { 2197c478bd9Sstevel@tonic-gate case CMD_HELP: 2207c478bd9Sstevel@tonic-gate return (gettext("Print usage message.")); 2217c478bd9Sstevel@tonic-gate case CMD_BOOT: 2223f2f09c1Sdp return (gettext("Activates (boots) specified zone. See " 2233f2f09c1Sdp "zoneadm(1m) for valid boot\n\targuments.")); 2247c478bd9Sstevel@tonic-gate case CMD_HALT: 2259e518655Sgjelinek return (gettext("Halts specified zone, bypassing shutdown " 2269e518655Sgjelinek "scripts and removing runtime\n\tresources of the zone.")); 2277c478bd9Sstevel@tonic-gate case CMD_READY: 2289e518655Sgjelinek return (gettext("Prepares a zone for running applications but " 2299e518655Sgjelinek "does not start any user\n\tprocesses in the zone.")); 2307c478bd9Sstevel@tonic-gate case CMD_REBOOT: 2319e518655Sgjelinek return (gettext("Restarts the zone (equivalent to a halt / " 2323f2f09c1Sdp "boot sequence).\n\tFails if the zone is not active. " 2333f2f09c1Sdp "See zoneadm(1m) for valid boot\n\targuments.")); 2347c478bd9Sstevel@tonic-gate case CMD_LIST: 2357c478bd9Sstevel@tonic-gate return (gettext("Lists the current zones, or a " 2367c478bd9Sstevel@tonic-gate "specific zone if indicated. By default,\n\tall " 2377c478bd9Sstevel@tonic-gate "running zones are listed, though this can be " 2387c478bd9Sstevel@tonic-gate "expanded to all\n\tinstalled zones with the -i " 2397c478bd9Sstevel@tonic-gate "option or all configured zones with the\n\t-c " 240555afedfScarlsonj "option. When used with the general -z <zone> and/or -u " 241555afedfScarlsonj "<uuid-match>\n\toptions, lists only the specified " 242555afedfScarlsonj "matching zone, but lists it\n\tregardless of its state, " 243555afedfScarlsonj "and the -i and -c options are disallowed. The\n\t-v " 244555afedfScarlsonj "option can be used to display verbose information: zone " 245555afedfScarlsonj "name, id,\n\tcurrent state, root directory and options. " 246555afedfScarlsonj "The -p option can be used\n\tto request machine-parsable " 247555afedfScarlsonj "output. The -v and -p options are mutually\n\texclusive." 248555afedfScarlsonj " If neither -v nor -p is used, just the zone name is " 249555afedfScarlsonj "listed.")); 2507c478bd9Sstevel@tonic-gate case CMD_VERIFY: 2517c478bd9Sstevel@tonic-gate return (gettext("Check to make sure the configuration " 2527c478bd9Sstevel@tonic-gate "can safely be instantiated\n\ton the machine: " 2537c478bd9Sstevel@tonic-gate "physical network interfaces exist, etc.")); 2547c478bd9Sstevel@tonic-gate case CMD_INSTALL: 2550b5de56dSgjelinek return (gettext("Install the configuration on to the system. " 2560b5de56dSgjelinek "The -x nodataset option\n\tcan be used to prevent the " 2570b5de56dSgjelinek "creation of a new ZFS file system for the\n\tzone " 2589acbbeafSnn35248 "(assuming the zonepath is within a ZFS file system).\n\t" 2599acbbeafSnn35248 "All other arguments are passed to the brand installation " 260ff17c8bfSgjelinek "function;\n\tsee brands(5) for more information.")); 2617c478bd9Sstevel@tonic-gate case CMD_UNINSTALL: 2629e518655Sgjelinek return (gettext("Uninstall the configuration from the system. " 263ff17c8bfSgjelinek "The -F flag can be used\n\tto force the action. All " 264ff17c8bfSgjelinek "other arguments are passed to the brand\n\tuninstall " 265ff17c8bfSgjelinek "function; see brands(5) for more information.")); 266865e09a4Sgjelinek case CMD_CLONE: 2670b5de56dSgjelinek return (gettext("Clone the installation of another zone. " 2680b5de56dSgjelinek "The -m option can be used to\n\tspecify 'copy' which " 2690b5de56dSgjelinek "forces a copy of the source zone. The -s option\n\t" 2700b5de56dSgjelinek "can be used to specify the name of a ZFS snapshot " 2710b5de56dSgjelinek "that was taken from\n\ta previous clone command. The " 2720b5de56dSgjelinek "snapshot will be used as the source\n\tinstead of " 273ff17c8bfSgjelinek "creating a new ZFS snapshot. All other arguments are " 274ff17c8bfSgjelinek "passed\n\tto the brand clone function; see " 275ff17c8bfSgjelinek "brands(5) for more information.")); 276865e09a4Sgjelinek case CMD_MOVE: 277865e09a4Sgjelinek return (gettext("Move the zone to a new zonepath.")); 2789e518655Sgjelinek case CMD_DETACH: 2799e518655Sgjelinek return (gettext("Detach the zone from the system. The zone " 2809e518655Sgjelinek "state is changed to\n\t'configured' (but the files under " 2819e518655Sgjelinek "the zonepath are untouched).\n\tThe zone can subsequently " 2829e518655Sgjelinek "be attached, or can be moved to another\n\tsystem and " 2838cd327d5Sgjelinek "attached there. The -n option can be used to specify\n\t" 2848cd327d5Sgjelinek "'no-execute' mode. When -n is used, the information " 2858cd327d5Sgjelinek "needed to attach\n\tthe zone is sent to standard output " 286ff17c8bfSgjelinek "but the zone is not actually\n\tdetached. All other " 287ff17c8bfSgjelinek "arguments are passed to the brand detach function;\n\tsee " 288ff17c8bfSgjelinek "brands(5) for more information.")); 2899e518655Sgjelinek case CMD_ATTACH: 2909e518655Sgjelinek return (gettext("Attach the zone to the system. The zone " 2919e518655Sgjelinek "state must be 'configured'\n\tprior to attach; upon " 2929e518655Sgjelinek "successful completion, the zone state will be\n\t" 2939e518655Sgjelinek "'installed'. The system software on the current " 2949e518655Sgjelinek "system must be\n\tcompatible with the software on the " 295ff17c8bfSgjelinek "zone's original system.\n\tSpecify -F " 2966cfd72c6Sgjelinek "to force the attach and skip software compatibility " 2976cfd72c6Sgjelinek "tests.\n\tThe -n option can be used to specify " 2986cfd72c6Sgjelinek "'no-execute' mode. When -n is\n\tused, the information " 2996cfd72c6Sgjelinek "needed to attach the zone is read from the\n\tspecified " 3006cfd72c6Sgjelinek "path and the configuration is only validated. The path " 301ff17c8bfSgjelinek "can\n\tbe '-' to specify standard input. The -F and -n " 302ff17c8bfSgjelinek "options are mutually\n\texclusive. All other arguments " 303ff17c8bfSgjelinek "are passed to the brand attach\n\tfunction; see " 304ff17c8bfSgjelinek "brands(5) for more information.")); 305555afedfScarlsonj case CMD_MARK: 306555afedfScarlsonj return (gettext("Set the state of the zone. This can be used " 307555afedfScarlsonj "to force the zone\n\tstate to 'incomplete' " 308555afedfScarlsonj "administratively if some activity has rendered\n\tthe " 309555afedfScarlsonj "zone permanently unusable. The only valid state that " 310555afedfScarlsonj "may be\n\tspecified is 'incomplete'.")); 311108322fbScarlsonj default: 312108322fbScarlsonj return (""); 3137c478bd9Sstevel@tonic-gate } 3147c478bd9Sstevel@tonic-gate /* NOTREACHED */ 3157e362f58Scomay return (NULL); 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate /* 3197c478bd9Sstevel@tonic-gate * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for 3207c478bd9Sstevel@tonic-gate * unexpected errors. 3217c478bd9Sstevel@tonic-gate */ 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate static int 3247c478bd9Sstevel@tonic-gate usage(boolean_t explicit) 3257c478bd9Sstevel@tonic-gate { 3267c478bd9Sstevel@tonic-gate int i; 3277c478bd9Sstevel@tonic-gate FILE *fd = explicit ? stdout : stderr; 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname); 330555afedfScarlsonj (void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n", 331555afedfScarlsonj execname); 332555afedfScarlsonj (void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname, 3337c478bd9Sstevel@tonic-gate gettext("subcommand")); 3347c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands")); 3357c478bd9Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 336108322fbScarlsonj if (cmdtab[i].short_usage == NULL) 337108322fbScarlsonj continue; 3387c478bd9Sstevel@tonic-gate (void) fprintf(fd, "%s\n", cmdtab[i].short_usage); 3397c478bd9Sstevel@tonic-gate if (explicit) 3407c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\t%s\n\n", long_help(i)); 3417c478bd9Sstevel@tonic-gate } 3427c478bd9Sstevel@tonic-gate if (!explicit) 3437c478bd9Sstevel@tonic-gate (void) fputs("\n", fd); 3447c478bd9Sstevel@tonic-gate return (Z_USAGE); 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate static void 3487c478bd9Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num) 3497c478bd9Sstevel@tonic-gate { 3507c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage); 3517c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s\n", long_help(cmd_num)); 3527c478bd9Sstevel@tonic-gate } 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate /* 3557c478bd9Sstevel@tonic-gate * zperror() is like perror(3c) except that this also prints the executable 3567c478bd9Sstevel@tonic-gate * name at the start of the message, and takes a boolean indicating whether 3577c478bd9Sstevel@tonic-gate * to call libc'c strerror() or that from libzonecfg. 3587c478bd9Sstevel@tonic-gate */ 3597c478bd9Sstevel@tonic-gate 3600b5de56dSgjelinek void 3617c478bd9Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error) 3627c478bd9Sstevel@tonic-gate { 3637c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", execname, str, 3647c478bd9Sstevel@tonic-gate zonecfg_error ? zonecfg_strerror(errno) : strerror(errno)); 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate /* 3687c478bd9Sstevel@tonic-gate * zperror2() is very similar to zperror() above, except it also prints a 3697c478bd9Sstevel@tonic-gate * supplied zone name after the executable. 3707c478bd9Sstevel@tonic-gate * 3717c478bd9Sstevel@tonic-gate * All current consumers of this function want libzonecfg's strerror() rather 3727c478bd9Sstevel@tonic-gate * than libc's; if this ever changes, this function can be made more generic 3737c478bd9Sstevel@tonic-gate * like zperror() above. 3747c478bd9Sstevel@tonic-gate */ 3757c478bd9Sstevel@tonic-gate 3760b5de56dSgjelinek void 3777c478bd9Sstevel@tonic-gate zperror2(const char *zone, const char *str) 3787c478bd9Sstevel@tonic-gate { 3797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str, 3807c478bd9Sstevel@tonic-gate zonecfg_strerror(errno)); 3817c478bd9Sstevel@tonic-gate } 3827c478bd9Sstevel@tonic-gate 3837c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */ 3840b5de56dSgjelinek void 3857c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...) 3867c478bd9Sstevel@tonic-gate { 3877c478bd9Sstevel@tonic-gate va_list alist; 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate va_start(alist, fmt); 3907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", execname); 3917c478bd9Sstevel@tonic-gate if (target_zone != NULL) 3927c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "zone '%s': ", target_zone); 3937c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, alist); 3947c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 3957c478bd9Sstevel@tonic-gate va_end(alist); 3967c478bd9Sstevel@tonic-gate } 3977c478bd9Sstevel@tonic-gate 3987c478bd9Sstevel@tonic-gate static void * 3997c478bd9Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize) 4007c478bd9Sstevel@tonic-gate { 4017c478bd9Sstevel@tonic-gate void *r = calloc(nelem, elsize); 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate if (r == NULL) { 4047c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), 4057c478bd9Sstevel@tonic-gate (ulong_t)nelem * elsize, strerror(errno)); 4067c478bd9Sstevel@tonic-gate exit(Z_ERR); 4077c478bd9Sstevel@tonic-gate } 4087c478bd9Sstevel@tonic-gate return (r); 4097c478bd9Sstevel@tonic-gate } 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate static void 4127c478bd9Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable) 4137c478bd9Sstevel@tonic-gate { 4147c478bd9Sstevel@tonic-gate static boolean_t firsttime = B_TRUE; 415f4b3ec61Sdh155122 char *ip_type_str; 416f4b3ec61Sdh155122 4172766118bS /* Skip a zone that shutdown while we were collecting data. */ 4182766118bS if (zent->zname[0] == '\0') 4192766118bS return; 4202766118bS 421f4b3ec61Sdh155122 if (zent->ziptype == ZS_EXCLUSIVE) 422f4b3ec61Sdh155122 ip_type_str = "excl"; 423f4b3ec61Sdh155122 else 424f4b3ec61Sdh155122 ip_type_str = "shared"; 4257c478bd9Sstevel@tonic-gate 4267c478bd9Sstevel@tonic-gate assert(!(verbose && parsable)); 4277c478bd9Sstevel@tonic-gate if (firsttime && verbose) { 4287c478bd9Sstevel@tonic-gate firsttime = B_FALSE; 429f4b3ec61Sdh155122 (void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n", 430f4b3ec61Sdh155122 ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND", 431f4b3ec61Sdh155122 "IP"); 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate if (!verbose) { 434555afedfScarlsonj char *cp, *clim; 435555afedfScarlsonj 4367c478bd9Sstevel@tonic-gate if (!parsable) { 4377c478bd9Sstevel@tonic-gate (void) printf("%s\n", zent->zname); 4387c478bd9Sstevel@tonic-gate return; 4397c478bd9Sstevel@tonic-gate } 4407c478bd9Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4417c478bd9Sstevel@tonic-gate (void) printf("-"); 4427c478bd9Sstevel@tonic-gate else 4437c478bd9Sstevel@tonic-gate (void) printf("%lu", zent->zid); 444555afedfScarlsonj (void) printf(":%s:%s:", zent->zname, zent->zstate_str); 445555afedfScarlsonj cp = zent->zroot; 446555afedfScarlsonj while ((clim = strchr(cp, ':')) != NULL) { 447555afedfScarlsonj (void) printf("%.*s\\:", clim - cp, cp); 448555afedfScarlsonj cp = clim + 1; 449555afedfScarlsonj } 450f4b3ec61Sdh155122 (void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand, 451f4b3ec61Sdh155122 ip_type_str); 4527c478bd9Sstevel@tonic-gate return; 4537c478bd9Sstevel@tonic-gate } 4547c478bd9Sstevel@tonic-gate if (zent->zstate_str != NULL) { 4557c478bd9Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4567c478bd9Sstevel@tonic-gate (void) printf("%*s", ZONEID_WIDTH, "-"); 4577c478bd9Sstevel@tonic-gate else 4587c478bd9Sstevel@tonic-gate (void) printf("%*lu", ZONEID_WIDTH, zent->zid); 459f4b3ec61Sdh155122 (void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname, 460f4b3ec61Sdh155122 zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str); 4617c478bd9Sstevel@tonic-gate } 4627c478bd9Sstevel@tonic-gate } 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate static int 465108322fbScarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent) 4667c478bd9Sstevel@tonic-gate { 46745916cd2Sjpk char root[MAXPATHLEN], *cp; 4687c478bd9Sstevel@tonic-gate int err; 469555afedfScarlsonj uuid_t uuid; 4702766118bS zone_dochandle_t handle; 4717c478bd9Sstevel@tonic-gate 4727c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname)); 4737c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot)); 4749acbbeafSnn35248 (void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand)); 4757c478bd9Sstevel@tonic-gate zent->zstate_str = "???"; 4767c478bd9Sstevel@tonic-gate 477108322fbScarlsonj zent->zid = zid; 4787c478bd9Sstevel@tonic-gate 479555afedfScarlsonj if (zonecfg_get_uuid(zone_name, uuid) == Z_OK && 480555afedfScarlsonj !uuid_is_null(uuid)) 481555afedfScarlsonj uuid_unparse(uuid, zent->zuuid); 482555afedfScarlsonj else 483555afedfScarlsonj zent->zuuid[0] = '\0'; 484555afedfScarlsonj 48545916cd2Sjpk /* 48645916cd2Sjpk * For labeled zones which query the zone path of lower-level 48745916cd2Sjpk * zones, the path needs to be adjusted to drop the final 48845916cd2Sjpk * "/root" component. This adjusted path is then useful 48945916cd2Sjpk * for reading down any exported directories from the 49045916cd2Sjpk * lower-level zone. 49145916cd2Sjpk */ 49245916cd2Sjpk if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) { 49345916cd2Sjpk if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot, 49445916cd2Sjpk sizeof (zent->zroot)) == -1) { 49545916cd2Sjpk zperror2(zent->zname, 49645916cd2Sjpk gettext("could not get zone path.")); 49745916cd2Sjpk return (Z_ERR); 49845916cd2Sjpk } 49945916cd2Sjpk cp = zent->zroot + strlen(zent->zroot) - 5; 50045916cd2Sjpk if (cp > zent->zroot && strcmp(cp, "/root") == 0) 50145916cd2Sjpk *cp = 0; 50245916cd2Sjpk } else { 50345916cd2Sjpk if ((err = zone_get_zonepath(zent->zname, root, 50445916cd2Sjpk sizeof (root))) != Z_OK) { 5057c478bd9Sstevel@tonic-gate errno = err; 50645916cd2Sjpk zperror2(zent->zname, 50745916cd2Sjpk gettext("could not get zone path.")); 5087c478bd9Sstevel@tonic-gate return (Z_ERR); 5097c478bd9Sstevel@tonic-gate } 5107c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zroot, root, sizeof (zent->zroot)); 51145916cd2Sjpk } 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) { 5147c478bd9Sstevel@tonic-gate errno = err; 5157c478bd9Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get state")); 5167c478bd9Sstevel@tonic-gate return (Z_ERR); 5177c478bd9Sstevel@tonic-gate } 5187c478bd9Sstevel@tonic-gate zent->zstate_str = zone_state_str(zent->zstate_num); 519bafa7067Snn35248 520bafa7067Snn35248 /* 521bafa7067Snn35248 * A zone's brand is only available in the .xml file describing it, 522bafa7067Snn35248 * which is only visible to the global zone. This causes 523bafa7067Snn35248 * zone_get_brand() to fail when called from within a non-global 524bafa7067Snn35248 * zone. Fortunately we only do this on labeled systems, where we 525bafa7067Snn35248 * know all zones are native. 526bafa7067Snn35248 */ 527bafa7067Snn35248 if (getzoneid() != GLOBAL_ZONEID) { 528bafa7067Snn35248 assert(is_system_labeled() != 0); 529*e5816e35SEdward Pilatowicz (void) strlcpy(zent->zbrand, default_brand, 530bafa7067Snn35248 sizeof (zent->zbrand)); 531bafa7067Snn35248 } else if (zone_get_brand(zent->zname, zent->zbrand, 5329acbbeafSnn35248 sizeof (zent->zbrand)) != Z_OK) { 5339acbbeafSnn35248 zperror2(zent->zname, gettext("could not get brand name")); 5349acbbeafSnn35248 return (Z_ERR); 5359acbbeafSnn35248 } 5367c478bd9Sstevel@tonic-gate 537f4b3ec61Sdh155122 /* 538f4b3ec61Sdh155122 * Get ip type of the zone. 539f4b3ec61Sdh155122 * Note for global zone, ZS_SHARED is set always. 540f4b3ec61Sdh155122 */ 541f4b3ec61Sdh155122 if (zid == GLOBAL_ZONEID) { 542f4b3ec61Sdh155122 zent->ziptype = ZS_SHARED; 5432766118bS return (Z_OK); 5442766118bS } 545f4b3ec61Sdh155122 5462766118bS /* 5472766118bS * There is a race condition where the zone could boot while 5482766118bS * we're walking the index file. In this case the zone state 5492766118bS * could be seen as running from the call above, but the zoneid 5502766118bS * would be undefined. 5512766118bS * 5522766118bS * There is also a race condition where the zone could shutdown after 5532766118bS * we got its running state above. This is also not an error and 5542766118bS * we fall back to getting the ziptype from the zone configuration. 5552766118bS */ 5562766118bS if (zent->zstate_num == ZONE_STATE_RUNNING && 5572766118bS zid != ZONE_ID_UNDEFINED) { 558f4b3ec61Sdh155122 ushort_t flags; 559f4b3ec61Sdh155122 560f4b3ec61Sdh155122 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, 5612766118bS sizeof (flags)) >= 0) { 562f4b3ec61Sdh155122 if (flags & ZF_NET_EXCL) 563f4b3ec61Sdh155122 zent->ziptype = ZS_EXCLUSIVE; 564f4b3ec61Sdh155122 else 565f4b3ec61Sdh155122 zent->ziptype = ZS_SHARED; 5662766118bS return (Z_OK); 5672766118bS } 5682766118bS } 569f4b3ec61Sdh155122 570f4b3ec61Sdh155122 if ((handle = zonecfg_init_handle()) == NULL) { 5712766118bS zperror2(zent->zname, gettext("could not init handle")); 572f4b3ec61Sdh155122 return (Z_ERR); 573f4b3ec61Sdh155122 } 5742766118bS if ((err = zonecfg_get_handle(zent->zname, handle)) != Z_OK) { 5752766118bS zperror2(zent->zname, gettext("could not get handle")); 576f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 577f4b3ec61Sdh155122 return (Z_ERR); 578f4b3ec61Sdh155122 } 579f4b3ec61Sdh155122 5802766118bS if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) != Z_OK) { 5812766118bS zperror2(zent->zname, gettext("could not get ip-type")); 582f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 583f4b3ec61Sdh155122 return (Z_ERR); 584f4b3ec61Sdh155122 } 585f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 586f4b3ec61Sdh155122 5877c478bd9Sstevel@tonic-gate return (Z_OK); 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate /* 5917c478bd9Sstevel@tonic-gate * fetch_zents() calls zone_list(2) to find out how many zones are running 5927c478bd9Sstevel@tonic-gate * (which is stored in the global nzents), then calls zone_list(2) again 5937c478bd9Sstevel@tonic-gate * to fetch the list of running zones (stored in the global zents). This 5947c478bd9Sstevel@tonic-gate * function may be called multiple times, so if zents is already set, we 5957c478bd9Sstevel@tonic-gate * return immediately to save work. 5962766118bS * 5972766118bS * Note that the data about running zones can change while this function 5982766118bS * is running, so its possible that the list of zones will have empty slots 5992766118bS * at the end. 6007c478bd9Sstevel@tonic-gate */ 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate static int 603108322fbScarlsonj fetch_zents(void) 6047c478bd9Sstevel@tonic-gate { 6057c478bd9Sstevel@tonic-gate zoneid_t *zids = NULL; 6067c478bd9Sstevel@tonic-gate uint_t nzents_saved; 607108322fbScarlsonj int i, retv; 608108322fbScarlsonj FILE *fp; 609108322fbScarlsonj boolean_t inaltroot; 610108322fbScarlsonj zone_entry_t *zentp; 6117c478bd9Sstevel@tonic-gate 6127c478bd9Sstevel@tonic-gate if (nzents > 0) 6137c478bd9Sstevel@tonic-gate return (Z_OK); 6147c478bd9Sstevel@tonic-gate 6157c478bd9Sstevel@tonic-gate if (zone_list(NULL, &nzents) != 0) { 6167c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zoneid list"), B_FALSE); 6177c478bd9Sstevel@tonic-gate return (Z_ERR); 6187c478bd9Sstevel@tonic-gate } 6197c478bd9Sstevel@tonic-gate 6207c478bd9Sstevel@tonic-gate again: 6217c478bd9Sstevel@tonic-gate if (nzents == 0) 6227c478bd9Sstevel@tonic-gate return (Z_OK); 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate zids = safe_calloc(nzents, sizeof (zoneid_t)); 6257c478bd9Sstevel@tonic-gate nzents_saved = nzents; 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate if (zone_list(zids, &nzents) != 0) { 6287c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zone list"), B_FALSE); 6297c478bd9Sstevel@tonic-gate free(zids); 6307c478bd9Sstevel@tonic-gate return (Z_ERR); 6317c478bd9Sstevel@tonic-gate } 6327c478bd9Sstevel@tonic-gate if (nzents != nzents_saved) { 6337c478bd9Sstevel@tonic-gate /* list changed, try again */ 6347c478bd9Sstevel@tonic-gate free(zids); 6357c478bd9Sstevel@tonic-gate goto again; 6367c478bd9Sstevel@tonic-gate } 6377c478bd9Sstevel@tonic-gate 6387c478bd9Sstevel@tonic-gate zents = safe_calloc(nzents, sizeof (zone_entry_t)); 6397c478bd9Sstevel@tonic-gate 640108322fbScarlsonj inaltroot = zonecfg_in_alt_root(); 641108322fbScarlsonj if (inaltroot) 642108322fbScarlsonj fp = zonecfg_open_scratch("", B_FALSE); 643108322fbScarlsonj else 644108322fbScarlsonj fp = NULL; 645108322fbScarlsonj zentp = zents; 646108322fbScarlsonj retv = Z_OK; 6477c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 6487c478bd9Sstevel@tonic-gate char name[ZONENAME_MAX]; 649108322fbScarlsonj char altname[ZONENAME_MAX]; 6507c478bd9Sstevel@tonic-gate 651108322fbScarlsonj if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) { 6522766118bS /* 6532766118bS * There is a race condition where the zone may have 6542766118bS * shutdown since we retrieved the number of running 6552766118bS * zones above. This is not an error, there will be 6562766118bS * an empty slot at the end of the list. 6572766118bS */ 658108322fbScarlsonj continue; 6597c478bd9Sstevel@tonic-gate } 660108322fbScarlsonj if (zonecfg_is_scratch(name)) { 661108322fbScarlsonj /* Ignore scratch zones by default */ 662108322fbScarlsonj if (!inaltroot) 663108322fbScarlsonj continue; 664108322fbScarlsonj if (fp == NULL || 665108322fbScarlsonj zonecfg_reverse_scratch(fp, name, altname, 666108322fbScarlsonj sizeof (altname), NULL, 0) == -1) { 6675ee84fbdSgjelinek zerror(gettext("could not resolve scratch " 668108322fbScarlsonj "zone %s"), name); 669108322fbScarlsonj retv = Z_ERR; 670108322fbScarlsonj continue; 671108322fbScarlsonj } 672108322fbScarlsonj (void) strcpy(name, altname); 673108322fbScarlsonj } else { 674108322fbScarlsonj /* Ignore non-scratch when in an alternate root */ 675108322fbScarlsonj if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0) 676108322fbScarlsonj continue; 677108322fbScarlsonj } 678108322fbScarlsonj if (lookup_zone_info(name, zids[i], zentp) != Z_OK) { 6792766118bS /* 6802766118bS * There is a race condition where the zone may have 6812766118bS * shutdown since we retrieved the number of running 6822766118bS * zones above. This is not an error, there will be 6832766118bS * an empty slot at the end of the list. 6842766118bS */ 685108322fbScarlsonj continue; 686108322fbScarlsonj } 687108322fbScarlsonj zentp++; 688108322fbScarlsonj } 689108322fbScarlsonj nzents = zentp - zents; 690108322fbScarlsonj if (fp != NULL) 691108322fbScarlsonj zonecfg_close_scratch(fp); 6927c478bd9Sstevel@tonic-gate 6937c478bd9Sstevel@tonic-gate free(zids); 694108322fbScarlsonj return (retv); 6957c478bd9Sstevel@tonic-gate } 6967c478bd9Sstevel@tonic-gate 697108322fbScarlsonj static int 6987c478bd9Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable) 6997c478bd9Sstevel@tonic-gate { 7007c478bd9Sstevel@tonic-gate int i; 7017c478bd9Sstevel@tonic-gate zone_entry_t zent; 7027c478bd9Sstevel@tonic-gate FILE *cookie; 7037c478bd9Sstevel@tonic-gate char *name; 7047c478bd9Sstevel@tonic-gate 7057c478bd9Sstevel@tonic-gate /* 7067c478bd9Sstevel@tonic-gate * First get the list of running zones from the kernel and print them. 7077c478bd9Sstevel@tonic-gate * If that is all we need, then return. 7087c478bd9Sstevel@tonic-gate */ 709108322fbScarlsonj if ((i = fetch_zents()) != Z_OK) { 7107c478bd9Sstevel@tonic-gate /* 7117c478bd9Sstevel@tonic-gate * No need for error messages; fetch_zents() has already taken 7127c478bd9Sstevel@tonic-gate * care of this. 7137c478bd9Sstevel@tonic-gate */ 714108322fbScarlsonj return (i); 7157c478bd9Sstevel@tonic-gate } 716108322fbScarlsonj for (i = 0; i < nzents; i++) 7177c478bd9Sstevel@tonic-gate zone_print(&zents[i], verbose, parsable); 7187c478bd9Sstevel@tonic-gate if (min_state >= ZONE_STATE_RUNNING) 719108322fbScarlsonj return (Z_OK); 7207c478bd9Sstevel@tonic-gate /* 7217c478bd9Sstevel@tonic-gate * Next, get the full list of zones from the configuration, skipping 7227c478bd9Sstevel@tonic-gate * any we have already printed. 7237c478bd9Sstevel@tonic-gate */ 7247c478bd9Sstevel@tonic-gate cookie = setzoneent(); 7257c478bd9Sstevel@tonic-gate while ((name = getzoneent(cookie)) != NULL) { 7267c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7277c478bd9Sstevel@tonic-gate if (strcmp(zents[i].zname, name) == 0) 7287c478bd9Sstevel@tonic-gate break; 7297c478bd9Sstevel@tonic-gate } 7307c478bd9Sstevel@tonic-gate if (i < nzents) { 7317c478bd9Sstevel@tonic-gate free(name); 7327c478bd9Sstevel@tonic-gate continue; 7337c478bd9Sstevel@tonic-gate } 734108322fbScarlsonj if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) { 7357c478bd9Sstevel@tonic-gate free(name); 7367c478bd9Sstevel@tonic-gate continue; 7377c478bd9Sstevel@tonic-gate } 7387c478bd9Sstevel@tonic-gate free(name); 7397c478bd9Sstevel@tonic-gate if (zent.zstate_num >= min_state) 7407c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 7417c478bd9Sstevel@tonic-gate } 7427c478bd9Sstevel@tonic-gate endzoneent(cookie); 743108322fbScarlsonj return (Z_OK); 7447c478bd9Sstevel@tonic-gate } 7457c478bd9Sstevel@tonic-gate 746c9f134eaSjv227347 /* 747c9f134eaSjv227347 * Retrieve a zone entry by name. Returns NULL if no such zone exists. 748c9f134eaSjv227347 */ 7497c478bd9Sstevel@tonic-gate static zone_entry_t * 750c9f134eaSjv227347 lookup_running_zone(const char *str) 7517c478bd9Sstevel@tonic-gate { 7527c478bd9Sstevel@tonic-gate int i; 7537c478bd9Sstevel@tonic-gate 7547c478bd9Sstevel@tonic-gate if (fetch_zents() != Z_OK) 7557c478bd9Sstevel@tonic-gate return (NULL); 7567c478bd9Sstevel@tonic-gate 7577c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7587c478bd9Sstevel@tonic-gate if (strcmp(str, zents[i].zname) == 0) 7597c478bd9Sstevel@tonic-gate return (&zents[i]); 7607c478bd9Sstevel@tonic-gate } 7617c478bd9Sstevel@tonic-gate return (NULL); 7627c478bd9Sstevel@tonic-gate } 7637c478bd9Sstevel@tonic-gate 7647c478bd9Sstevel@tonic-gate /* 7657c478bd9Sstevel@tonic-gate * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if 7667c478bd9Sstevel@tonic-gate * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect). 7677c478bd9Sstevel@tonic-gate */ 7687c478bd9Sstevel@tonic-gate static boolean_t 7697c478bd9Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file) 7707c478bd9Sstevel@tonic-gate { 7717c478bd9Sstevel@tonic-gate char *str; 7727c478bd9Sstevel@tonic-gate 7737c478bd9Sstevel@tonic-gate assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR || 7747c478bd9Sstevel@tonic-gate bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP || 7757c478bd9Sstevel@tonic-gate bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH); 7767c478bd9Sstevel@tonic-gate /* 7777c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 7787c478bd9Sstevel@tonic-gate * The strings below will be used as part of a larger message, 7797c478bd9Sstevel@tonic-gate * either: 7807c478bd9Sstevel@tonic-gate * (file name) must be (owner|group|world) (read|writ|execut)able 7817c478bd9Sstevel@tonic-gate * or 7827c478bd9Sstevel@tonic-gate * (file name) must not be (owner|group|world) (read|writ|execut)able 7837c478bd9Sstevel@tonic-gate */ 7847c478bd9Sstevel@tonic-gate switch (bit) { 7857c478bd9Sstevel@tonic-gate case S_IRUSR: 7867c478bd9Sstevel@tonic-gate str = gettext("owner readable"); 7877c478bd9Sstevel@tonic-gate break; 7887c478bd9Sstevel@tonic-gate case S_IWUSR: 7897c478bd9Sstevel@tonic-gate str = gettext("owner writable"); 7907c478bd9Sstevel@tonic-gate break; 7917c478bd9Sstevel@tonic-gate case S_IXUSR: 7927c478bd9Sstevel@tonic-gate str = gettext("owner executable"); 7937c478bd9Sstevel@tonic-gate break; 7947c478bd9Sstevel@tonic-gate case S_IRGRP: 7957c478bd9Sstevel@tonic-gate str = gettext("group readable"); 7967c478bd9Sstevel@tonic-gate break; 7977c478bd9Sstevel@tonic-gate case S_IWGRP: 7987c478bd9Sstevel@tonic-gate str = gettext("group writable"); 7997c478bd9Sstevel@tonic-gate break; 8007c478bd9Sstevel@tonic-gate case S_IXGRP: 8017c478bd9Sstevel@tonic-gate str = gettext("group executable"); 8027c478bd9Sstevel@tonic-gate break; 8037c478bd9Sstevel@tonic-gate case S_IROTH: 8047c478bd9Sstevel@tonic-gate str = gettext("world readable"); 8057c478bd9Sstevel@tonic-gate break; 8067c478bd9Sstevel@tonic-gate case S_IWOTH: 8077c478bd9Sstevel@tonic-gate str = gettext("world writable"); 8087c478bd9Sstevel@tonic-gate break; 8097c478bd9Sstevel@tonic-gate case S_IXOTH: 8107c478bd9Sstevel@tonic-gate str = gettext("world executable"); 8117c478bd9Sstevel@tonic-gate break; 8127c478bd9Sstevel@tonic-gate } 8137c478bd9Sstevel@tonic-gate if ((mode & bit) == (on ? 0 : bit)) { 8147c478bd9Sstevel@tonic-gate /* 8157c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 8167c478bd9Sstevel@tonic-gate * The first parameter below is a file name; the second 8177c478bd9Sstevel@tonic-gate * is one of the "(owner|group|world) (read|writ|execut)able" 8187c478bd9Sstevel@tonic-gate * strings from above. 8197c478bd9Sstevel@tonic-gate */ 8207c478bd9Sstevel@tonic-gate /* 8217c478bd9Sstevel@tonic-gate * The code below could be simplified but not in a way 8227c478bd9Sstevel@tonic-gate * that would easily translate to non-English locales. 8237c478bd9Sstevel@tonic-gate */ 8247c478bd9Sstevel@tonic-gate if (on) { 8257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must be %s.\n"), 8267c478bd9Sstevel@tonic-gate file, str); 8277c478bd9Sstevel@tonic-gate } else { 8287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must not be %s.\n"), 8297c478bd9Sstevel@tonic-gate file, str); 8307c478bd9Sstevel@tonic-gate } 8317c478bd9Sstevel@tonic-gate return (B_TRUE); 8327c478bd9Sstevel@tonic-gate } 8337c478bd9Sstevel@tonic-gate return (B_FALSE); 8347c478bd9Sstevel@tonic-gate } 8357c478bd9Sstevel@tonic-gate 8367c478bd9Sstevel@tonic-gate /* 8377c478bd9Sstevel@tonic-gate * We want to make sure that no zone has its zone path as a child node 8387c478bd9Sstevel@tonic-gate * (in the directory sense) of any other. We do that by comparing this 8397c478bd9Sstevel@tonic-gate * zone's path to the path of all other (non-global) zones. The comparison 8407c478bd9Sstevel@tonic-gate * in each case is simple: add '/' to the end of the path, then do a 8417c478bd9Sstevel@tonic-gate * strncmp() of the two paths, using the length of the shorter one. 8427c478bd9Sstevel@tonic-gate */ 8437c478bd9Sstevel@tonic-gate 8447c478bd9Sstevel@tonic-gate static int 8457c478bd9Sstevel@tonic-gate crosscheck_zonepaths(char *path) 8467c478bd9Sstevel@tonic-gate { 8477c478bd9Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 8487c478bd9Sstevel@tonic-gate char path_copy[MAXPATHLEN]; /* copy of original path */ 8497c478bd9Sstevel@tonic-gate char rpath_copy[MAXPATHLEN]; /* copy of original rpath */ 8507c478bd9Sstevel@tonic-gate struct zoneent *ze; 8517c478bd9Sstevel@tonic-gate int res, err; 8527c478bd9Sstevel@tonic-gate FILE *cookie; 8537c478bd9Sstevel@tonic-gate 8547c478bd9Sstevel@tonic-gate cookie = setzoneent(); 8557c478bd9Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 8567c478bd9Sstevel@tonic-gate /* Skip zones which are not installed. */ 8577c478bd9Sstevel@tonic-gate if (ze->zone_state < ZONE_STATE_INSTALLED) { 8587c478bd9Sstevel@tonic-gate free(ze); 8597c478bd9Sstevel@tonic-gate continue; 8607c478bd9Sstevel@tonic-gate } 8617c478bd9Sstevel@tonic-gate /* Skip the global zone and the current target zone. */ 8627c478bd9Sstevel@tonic-gate if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 || 8637c478bd9Sstevel@tonic-gate strcmp(ze->zone_name, target_zone) == 0) { 8647c478bd9Sstevel@tonic-gate free(ze); 8657c478bd9Sstevel@tonic-gate continue; 8667c478bd9Sstevel@tonic-gate } 8677c478bd9Sstevel@tonic-gate if (strlen(ze->zone_path) == 0) { 8687c478bd9Sstevel@tonic-gate /* old index file without path, fall back */ 8697c478bd9Sstevel@tonic-gate if ((err = zone_get_zonepath(ze->zone_name, 8707c478bd9Sstevel@tonic-gate ze->zone_path, sizeof (ze->zone_path))) != Z_OK) { 8717c478bd9Sstevel@tonic-gate errno = err; 8727c478bd9Sstevel@tonic-gate zperror2(ze->zone_name, 8737c478bd9Sstevel@tonic-gate gettext("could not get zone path")); 8747c478bd9Sstevel@tonic-gate free(ze); 8757c478bd9Sstevel@tonic-gate continue; 8767c478bd9Sstevel@tonic-gate } 8777c478bd9Sstevel@tonic-gate } 878108322fbScarlsonj (void) snprintf(path_copy, sizeof (path_copy), "%s%s", 879108322fbScarlsonj zonecfg_get_root(), ze->zone_path); 880108322fbScarlsonj res = resolvepath(path_copy, rpath, sizeof (rpath)); 8817c478bd9Sstevel@tonic-gate if (res == -1) { 8827c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 883108322fbScarlsonj zperror(path_copy, B_FALSE); 8847c478bd9Sstevel@tonic-gate free(ze); 8857c478bd9Sstevel@tonic-gate return (Z_ERR); 8867c478bd9Sstevel@tonic-gate } 8877c478bd9Sstevel@tonic-gate (void) printf(gettext("WARNING: zone %s is installed, " 8887c478bd9Sstevel@tonic-gate "but its %s %s does not exist.\n"), ze->zone_name, 889108322fbScarlsonj "zonepath", path_copy); 8907c478bd9Sstevel@tonic-gate free(ze); 8917c478bd9Sstevel@tonic-gate continue; 8927c478bd9Sstevel@tonic-gate } 8937c478bd9Sstevel@tonic-gate rpath[res] = '\0'; 8947c478bd9Sstevel@tonic-gate (void) snprintf(path_copy, sizeof (path_copy), "%s/", path); 8957c478bd9Sstevel@tonic-gate (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath); 8967c478bd9Sstevel@tonic-gate if (strncmp(path_copy, rpath_copy, 8977c478bd9Sstevel@tonic-gate min(strlen(path_copy), strlen(rpath_copy))) == 0) { 8985ee84fbdSgjelinek /* 8995ee84fbdSgjelinek * TRANSLATION_NOTE 9005ee84fbdSgjelinek * zonepath is a literal that should not be translated. 9015ee84fbdSgjelinek */ 9027c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s zonepath (%s) and " 9037c478bd9Sstevel@tonic-gate "%s zonepath (%s) overlap.\n"), 9047c478bd9Sstevel@tonic-gate target_zone, path, ze->zone_name, rpath); 9057c478bd9Sstevel@tonic-gate free(ze); 9067c478bd9Sstevel@tonic-gate return (Z_ERR); 9077c478bd9Sstevel@tonic-gate } 9087c478bd9Sstevel@tonic-gate free(ze); 9097c478bd9Sstevel@tonic-gate } 9107c478bd9Sstevel@tonic-gate endzoneent(cookie); 9117c478bd9Sstevel@tonic-gate return (Z_OK); 9127c478bd9Sstevel@tonic-gate } 9137c478bd9Sstevel@tonic-gate 9147c478bd9Sstevel@tonic-gate static int 9157c478bd9Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num) 9167c478bd9Sstevel@tonic-gate { 9177c478bd9Sstevel@tonic-gate int res; /* result of last library/system call */ 9187c478bd9Sstevel@tonic-gate boolean_t err = B_FALSE; /* have we run into an error? */ 9197c478bd9Sstevel@tonic-gate struct stat stbuf; 9203f2f09c1Sdp struct statvfs64 vfsbuf; 9217c478bd9Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 9227c478bd9Sstevel@tonic-gate char ppath[MAXPATHLEN]; /* parent path */ 9237c478bd9Sstevel@tonic-gate char rppath[MAXPATHLEN]; /* resolved parent path */ 9247c478bd9Sstevel@tonic-gate char rootpath[MAXPATHLEN]; /* root path */ 9257c478bd9Sstevel@tonic-gate zone_state_t state; 9267c478bd9Sstevel@tonic-gate 9277c478bd9Sstevel@tonic-gate if (path[0] != '/') { 9287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9297c478bd9Sstevel@tonic-gate gettext("%s is not an absolute path.\n"), path); 9307c478bd9Sstevel@tonic-gate return (Z_ERR); 9317c478bd9Sstevel@tonic-gate } 9327c478bd9Sstevel@tonic-gate if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) { 9337c478bd9Sstevel@tonic-gate if ((errno != ENOENT) || 934865e09a4Sgjelinek (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL && 935865e09a4Sgjelinek cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) { 9367c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 9377c478bd9Sstevel@tonic-gate return (Z_ERR); 9387c478bd9Sstevel@tonic-gate } 9397c478bd9Sstevel@tonic-gate if (cmd_num == CMD_VERIFY) { 9405ee84fbdSgjelinek /* 9415ee84fbdSgjelinek * TRANSLATION_NOTE 9425ee84fbdSgjelinek * zoneadm is a literal that should not be translated. 9435ee84fbdSgjelinek */ 9447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("WARNING: %s does not " 9455ee84fbdSgjelinek "exist, so it could not be verified.\nWhen " 9465ee84fbdSgjelinek "'zoneadm %s' is run, '%s' will try to create\n%s, " 9475ee84fbdSgjelinek "and '%s' will be tried again,\nbut the '%s' may " 9485ee84fbdSgjelinek "fail if:\nthe parent directory of %s is group- or " 9495ee84fbdSgjelinek "other-writable\nor\n%s overlaps with any other " 9507c478bd9Sstevel@tonic-gate "installed zones.\n"), path, 9517c478bd9Sstevel@tonic-gate cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL), 9527c478bd9Sstevel@tonic-gate path, cmd_to_str(CMD_VERIFY), 9537c478bd9Sstevel@tonic-gate cmd_to_str(CMD_VERIFY), path, path); 9547c478bd9Sstevel@tonic-gate return (Z_OK); 9557c478bd9Sstevel@tonic-gate } 9567c478bd9Sstevel@tonic-gate /* 9577c478bd9Sstevel@tonic-gate * The zonepath is supposed to be mode 700 but its 9587c478bd9Sstevel@tonic-gate * parent(s) 755. So use 755 on the mkdirp() then 9597c478bd9Sstevel@tonic-gate * chmod() the zonepath itself to 700. 9607c478bd9Sstevel@tonic-gate */ 9617c478bd9Sstevel@tonic-gate if (mkdirp(path, DEFAULT_DIR_MODE) < 0) { 9627c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 9637c478bd9Sstevel@tonic-gate return (Z_ERR); 9647c478bd9Sstevel@tonic-gate } 9657c478bd9Sstevel@tonic-gate /* 9667c478bd9Sstevel@tonic-gate * If the chmod() fails, report the error, but might 9677c478bd9Sstevel@tonic-gate * as well continue the verify procedure. 9687c478bd9Sstevel@tonic-gate */ 9697c478bd9Sstevel@tonic-gate if (chmod(path, S_IRWXU) != 0) 9707c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 9717c478bd9Sstevel@tonic-gate /* 9727c478bd9Sstevel@tonic-gate * Since the mkdir() succeeded, we should not have to 9737c478bd9Sstevel@tonic-gate * worry about a subsequent ENOENT, thus this should 9747c478bd9Sstevel@tonic-gate * only recurse once. 9757c478bd9Sstevel@tonic-gate */ 976865e09a4Sgjelinek return (validate_zonepath(path, cmd_num)); 9777c478bd9Sstevel@tonic-gate } 9787c478bd9Sstevel@tonic-gate rpath[res] = '\0'; 9797c478bd9Sstevel@tonic-gate if (strcmp(path, rpath) != 0) { 9807c478bd9Sstevel@tonic-gate errno = Z_RESOLVED_PATH; 9817c478bd9Sstevel@tonic-gate zperror(path, B_TRUE); 9827c478bd9Sstevel@tonic-gate return (Z_ERR); 9837c478bd9Sstevel@tonic-gate } 9847c478bd9Sstevel@tonic-gate if ((res = stat(rpath, &stbuf)) != 0) { 9857c478bd9Sstevel@tonic-gate zperror(rpath, B_FALSE); 9867c478bd9Sstevel@tonic-gate return (Z_ERR); 9877c478bd9Sstevel@tonic-gate } 9887c478bd9Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 9897c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 9907c478bd9Sstevel@tonic-gate rpath); 9917c478bd9Sstevel@tonic-gate return (Z_ERR); 9927c478bd9Sstevel@tonic-gate } 9934fceebdfSblakej if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) { 9947c478bd9Sstevel@tonic-gate (void) printf(gettext("WARNING: %s is on a temporary " 9950b5de56dSgjelinek "file system.\n"), rpath); 9967c478bd9Sstevel@tonic-gate } 9977c478bd9Sstevel@tonic-gate if (crosscheck_zonepaths(rpath) != Z_OK) 9987c478bd9Sstevel@tonic-gate return (Z_ERR); 9997c478bd9Sstevel@tonic-gate /* 10007c478bd9Sstevel@tonic-gate * Try to collect and report as many minor errors as possible 10017c478bd9Sstevel@tonic-gate * before returning, so the user can learn everything that needs 10027c478bd9Sstevel@tonic-gate * to be fixed up front. 10037c478bd9Sstevel@tonic-gate */ 10047c478bd9Sstevel@tonic-gate if (stbuf.st_uid != 0) { 10057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 10067c478bd9Sstevel@tonic-gate rpath); 10077c478bd9Sstevel@tonic-gate err = B_TRUE; 10087c478bd9Sstevel@tonic-gate } 10097c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath); 10107c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath); 10117c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath); 10127c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath); 10137c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath); 10147c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath); 10157c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath); 10167c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath); 10177c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath); 10187c478bd9Sstevel@tonic-gate 10197c478bd9Sstevel@tonic-gate (void) snprintf(ppath, sizeof (ppath), "%s/..", path); 10207c478bd9Sstevel@tonic-gate if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) { 10217c478bd9Sstevel@tonic-gate zperror(ppath, B_FALSE); 10227c478bd9Sstevel@tonic-gate return (Z_ERR); 10237c478bd9Sstevel@tonic-gate } 10247c478bd9Sstevel@tonic-gate rppath[res] = '\0'; 10257c478bd9Sstevel@tonic-gate if ((res = stat(rppath, &stbuf)) != 0) { 10267c478bd9Sstevel@tonic-gate zperror(rppath, B_FALSE); 10277c478bd9Sstevel@tonic-gate return (Z_ERR); 10287c478bd9Sstevel@tonic-gate } 10297c478bd9Sstevel@tonic-gate /* theoretically impossible */ 10307c478bd9Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 10317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 10327c478bd9Sstevel@tonic-gate rppath); 10337c478bd9Sstevel@tonic-gate return (Z_ERR); 10347c478bd9Sstevel@tonic-gate } 10357c478bd9Sstevel@tonic-gate if (stbuf.st_uid != 0) { 10367c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 10377c478bd9Sstevel@tonic-gate rppath); 10387c478bd9Sstevel@tonic-gate err = B_TRUE; 10397c478bd9Sstevel@tonic-gate } 10407c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath); 10417c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath); 10427c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath); 10437c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath); 10447c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath); 10457c478bd9Sstevel@tonic-gate if (strcmp(rpath, rppath) == 0) { 10467c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is its own parent.\n"), 10477c478bd9Sstevel@tonic-gate rppath); 10487c478bd9Sstevel@tonic-gate err = B_TRUE; 10497c478bd9Sstevel@tonic-gate } 10507c478bd9Sstevel@tonic-gate 10513f2f09c1Sdp if (statvfs64(rpath, &vfsbuf) != 0) { 10527c478bd9Sstevel@tonic-gate zperror(rpath, B_FALSE); 10537c478bd9Sstevel@tonic-gate return (Z_ERR); 10547c478bd9Sstevel@tonic-gate } 1055b5abaf04Sgjelinek if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) { 10565ee84fbdSgjelinek /* 10575ee84fbdSgjelinek * TRANSLATION_NOTE 10585ee84fbdSgjelinek * Zonepath and NFS are literals that should not be translated. 10595ee84fbdSgjelinek */ 10605ee84fbdSgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on an NFS " 10610b5de56dSgjelinek "mounted file system.\n" 10620b5de56dSgjelinek "\tA local file system must be used.\n"), rpath); 1063b5abaf04Sgjelinek return (Z_ERR); 1064b5abaf04Sgjelinek } 1065b5abaf04Sgjelinek if (vfsbuf.f_flag & ST_NOSUID) { 10665ee84fbdSgjelinek /* 10675ee84fbdSgjelinek * TRANSLATION_NOTE 10685ee84fbdSgjelinek * Zonepath and nosuid are literals that should not be 10695ee84fbdSgjelinek * translated. 10705ee84fbdSgjelinek */ 1071b5abaf04Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on a nosuid " 10720b5de56dSgjelinek "file system.\n"), rpath); 10737c478bd9Sstevel@tonic-gate return (Z_ERR); 10747c478bd9Sstevel@tonic-gate } 10757c478bd9Sstevel@tonic-gate 10767c478bd9Sstevel@tonic-gate if ((res = zone_get_state(target_zone, &state)) != Z_OK) { 10777c478bd9Sstevel@tonic-gate errno = res; 10787c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get state")); 10797c478bd9Sstevel@tonic-gate return (Z_ERR); 10807c478bd9Sstevel@tonic-gate } 10817c478bd9Sstevel@tonic-gate /* 10827c478bd9Sstevel@tonic-gate * The existence of the root path is only bad in the configured state, 10837c478bd9Sstevel@tonic-gate * as it is *supposed* to be there at the installed and later states. 1084ee519a1fSgjelinek * However, the root path is expected to be there if the zone is 1085ee519a1fSgjelinek * detached. 10867c478bd9Sstevel@tonic-gate * State/command mismatches are caught earlier in verify_details(). 10877c478bd9Sstevel@tonic-gate */ 1088ee519a1fSgjelinek if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) { 10897c478bd9Sstevel@tonic-gate if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >= 10907c478bd9Sstevel@tonic-gate sizeof (rootpath)) { 10915ee84fbdSgjelinek /* 10925ee84fbdSgjelinek * TRANSLATION_NOTE 10935ee84fbdSgjelinek * Zonepath is a literal that should not be translated. 10945ee84fbdSgjelinek */ 10957c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 10967c478bd9Sstevel@tonic-gate gettext("Zonepath %s is too long.\n"), rpath); 10977c478bd9Sstevel@tonic-gate return (Z_ERR); 10987c478bd9Sstevel@tonic-gate } 10997c478bd9Sstevel@tonic-gate if ((res = stat(rootpath, &stbuf)) == 0) { 1100c4a45622S if (zonecfg_detached(rpath)) { 1101ee519a1fSgjelinek (void) fprintf(stderr, 1102ee519a1fSgjelinek gettext("Cannot %s detached " 1103ee519a1fSgjelinek "zone.\nUse attach or remove %s " 1104ee519a1fSgjelinek "directory.\n"), cmd_to_str(cmd_num), 1105ee519a1fSgjelinek rpath); 11067c478bd9Sstevel@tonic-gate return (Z_ERR); 11077c478bd9Sstevel@tonic-gate } 1108c4a45622S 1109c4a45622S /* Not detached, check if it really looks ok. */ 1110c4a45622S 1111c4a45622S if (!S_ISDIR(stbuf.st_mode)) { 1112c4a45622S (void) fprintf(stderr, gettext("%s is not a " 1113c4a45622S "directory.\n"), rootpath); 1114c4a45622S return (Z_ERR); 1115c4a45622S } 1116c4a45622S 1117c4a45622S if (stbuf.st_uid != 0) { 1118c4a45622S (void) fprintf(stderr, gettext("%s is not " 1119c4a45622S "owned by root.\n"), rootpath); 1120c4a45622S return (Z_ERR); 1121c4a45622S } 1122c4a45622S 1123c4a45622S if ((stbuf.st_mode & 0777) != 0755) { 1124c4a45622S (void) fprintf(stderr, gettext("%s mode is not " 1125c4a45622S "0755.\n"), rootpath); 1126c4a45622S return (Z_ERR); 1127c4a45622S } 1128c4a45622S } 11297c478bd9Sstevel@tonic-gate } 11307c478bd9Sstevel@tonic-gate 11317c478bd9Sstevel@tonic-gate return (err ? Z_ERR : Z_OK); 11327c478bd9Sstevel@tonic-gate } 11337c478bd9Sstevel@tonic-gate 11347c478bd9Sstevel@tonic-gate static int 1135ce28b40eSzt129084 invoke_brand_handler(int cmd_num, char *argv[]) 1136ce28b40eSzt129084 { 1137ce28b40eSzt129084 zone_dochandle_t handle; 1138ce28b40eSzt129084 int err; 1139ce28b40eSzt129084 1140ce28b40eSzt129084 if ((handle = zonecfg_init_handle()) == NULL) { 1141ce28b40eSzt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 1142ce28b40eSzt129084 return (Z_ERR); 1143ce28b40eSzt129084 } 1144ce28b40eSzt129084 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 1145ce28b40eSzt129084 errno = err; 1146ce28b40eSzt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 1147ce28b40eSzt129084 zonecfg_fini_handle(handle); 1148ce28b40eSzt129084 return (Z_ERR); 1149ce28b40eSzt129084 } 1150ce28b40eSzt129084 if (verify_brand(handle, cmd_num, argv) != Z_OK) { 1151ce28b40eSzt129084 zonecfg_fini_handle(handle); 1152ce28b40eSzt129084 return (Z_ERR); 1153ce28b40eSzt129084 } 1154ce28b40eSzt129084 zonecfg_fini_handle(handle); 1155ce28b40eSzt129084 return (Z_OK); 1156ce28b40eSzt129084 } 1157ce28b40eSzt129084 1158ce28b40eSzt129084 static int 11597c478bd9Sstevel@tonic-gate ready_func(int argc, char *argv[]) 11607c478bd9Sstevel@tonic-gate { 11617c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 11627c478bd9Sstevel@tonic-gate int arg; 11637c478bd9Sstevel@tonic-gate 1164108322fbScarlsonj if (zonecfg_in_alt_root()) { 1165108322fbScarlsonj zerror(gettext("cannot ready zone in alternate root")); 1166108322fbScarlsonj return (Z_ERR); 1167108322fbScarlsonj } 1168108322fbScarlsonj 11697c478bd9Sstevel@tonic-gate optind = 0; 11707c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 11717c478bd9Sstevel@tonic-gate switch (arg) { 11727c478bd9Sstevel@tonic-gate case '?': 11737c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 11747c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 11757c478bd9Sstevel@tonic-gate default: 11767c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 11777c478bd9Sstevel@tonic-gate return (Z_USAGE); 11787c478bd9Sstevel@tonic-gate } 11797c478bd9Sstevel@tonic-gate } 11807c478bd9Sstevel@tonic-gate if (argc > optind) { 11817c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 11827c478bd9Sstevel@tonic-gate return (Z_USAGE); 11837c478bd9Sstevel@tonic-gate } 11849acbbeafSnn35248 if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE) 11859acbbeafSnn35248 != Z_OK) 11867c478bd9Sstevel@tonic-gate return (Z_ERR); 1187ce28b40eSzt129084 if (verify_details(CMD_READY, argv) != Z_OK) 11887c478bd9Sstevel@tonic-gate return (Z_ERR); 11897c478bd9Sstevel@tonic-gate 11907c478bd9Sstevel@tonic-gate zarg.cmd = Z_READY; 1191ff17c8bfSgjelinek if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) { 11927c478bd9Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 11937c478bd9Sstevel@tonic-gate return (Z_ERR); 11947c478bd9Sstevel@tonic-gate } 11957c478bd9Sstevel@tonic-gate return (Z_OK); 11967c478bd9Sstevel@tonic-gate } 11977c478bd9Sstevel@tonic-gate 11987c478bd9Sstevel@tonic-gate static int 11997c478bd9Sstevel@tonic-gate boot_func(int argc, char *argv[]) 12007c478bd9Sstevel@tonic-gate { 12017c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 12029acbbeafSnn35248 boolean_t force = B_FALSE; 12037c478bd9Sstevel@tonic-gate int arg; 12047c478bd9Sstevel@tonic-gate 1205108322fbScarlsonj if (zonecfg_in_alt_root()) { 1206108322fbScarlsonj zerror(gettext("cannot boot zone in alternate root")); 1207108322fbScarlsonj return (Z_ERR); 1208108322fbScarlsonj } 1209108322fbScarlsonj 12107c478bd9Sstevel@tonic-gate zarg.bootbuf[0] = '\0'; 12117c478bd9Sstevel@tonic-gate 12127c478bd9Sstevel@tonic-gate /* 12133f2f09c1Sdp * The following getopt processes arguments to zone boot; that 12143f2f09c1Sdp * is to say, the [here] portion of the argument string: 12153f2f09c1Sdp * 12163f2f09c1Sdp * zoneadm -z myzone boot [here] -- -v -m verbose 12173f2f09c1Sdp * 12183f2f09c1Sdp * Where [here] can either be nothing, -? (in which case we bail 12199acbbeafSnn35248 * and print usage), -f (a private option to indicate that the 12209acbbeafSnn35248 * boot operation should be 'forced'), or -s. Support for -s is 12219acbbeafSnn35248 * vestigal and obsolete, but is retained because it was a 12229acbbeafSnn35248 * documented interface and there are known consumers including 12239acbbeafSnn35248 * admin/install; the proper way to specify boot arguments like -s 12249acbbeafSnn35248 * is: 12253f2f09c1Sdp * 12263f2f09c1Sdp * zoneadm -z myzone boot -- -s -v -m verbose. 12277c478bd9Sstevel@tonic-gate */ 12287c478bd9Sstevel@tonic-gate optind = 0; 12299acbbeafSnn35248 while ((arg = getopt(argc, argv, "?fs")) != EOF) { 12307c478bd9Sstevel@tonic-gate switch (arg) { 12317c478bd9Sstevel@tonic-gate case '?': 12327c478bd9Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 12337c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 12347c478bd9Sstevel@tonic-gate case 's': 12357c478bd9Sstevel@tonic-gate (void) strlcpy(zarg.bootbuf, "-s", 12367c478bd9Sstevel@tonic-gate sizeof (zarg.bootbuf)); 12377c478bd9Sstevel@tonic-gate break; 12389acbbeafSnn35248 case 'f': 12399acbbeafSnn35248 force = B_TRUE; 12409acbbeafSnn35248 break; 12417c478bd9Sstevel@tonic-gate default: 12427c478bd9Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 12437c478bd9Sstevel@tonic-gate return (Z_USAGE); 12447c478bd9Sstevel@tonic-gate } 12457c478bd9Sstevel@tonic-gate } 12463f2f09c1Sdp 12473f2f09c1Sdp for (; optind < argc; optind++) { 12483f2f09c1Sdp if (strlcat(zarg.bootbuf, argv[optind], 12493f2f09c1Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 12503f2f09c1Sdp zerror(gettext("Boot argument list too long")); 12513f2f09c1Sdp return (Z_ERR); 12527c478bd9Sstevel@tonic-gate } 12533f2f09c1Sdp if (optind < argc - 1) 12543f2f09c1Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 12553f2f09c1Sdp sizeof (zarg.bootbuf)) { 12563f2f09c1Sdp zerror(gettext("Boot argument list too long")); 12573f2f09c1Sdp return (Z_ERR); 12583f2f09c1Sdp } 12593f2f09c1Sdp } 12609acbbeafSnn35248 if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force) 12619acbbeafSnn35248 != Z_OK) 12627c478bd9Sstevel@tonic-gate return (Z_ERR); 1263ce28b40eSzt129084 if (verify_details(CMD_BOOT, argv) != Z_OK) 12647c478bd9Sstevel@tonic-gate return (Z_ERR); 12659acbbeafSnn35248 zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT; 1266ff17c8bfSgjelinek if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) { 12677c478bd9Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 12687c478bd9Sstevel@tonic-gate return (Z_ERR); 12697c478bd9Sstevel@tonic-gate } 12700209230bSgjelinek 12717c478bd9Sstevel@tonic-gate return (Z_OK); 12727c478bd9Sstevel@tonic-gate } 12737c478bd9Sstevel@tonic-gate 12747c478bd9Sstevel@tonic-gate static void 12757c478bd9Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr) 12767c478bd9Sstevel@tonic-gate { 12777c478bd9Sstevel@tonic-gate ssize_t result; 1278555afedfScarlsonj uuid_t uuid; 1279555afedfScarlsonj FILE *fp; 1280f4b3ec61Sdh155122 ushort_t flags; 1281555afedfScarlsonj 1282555afedfScarlsonj (void) memset(zeptr, 0, sizeof (*zeptr)); 12837c478bd9Sstevel@tonic-gate 12847c478bd9Sstevel@tonic-gate zeptr->zid = zid; 1285555afedfScarlsonj 12867c478bd9Sstevel@tonic-gate /* 12877c478bd9Sstevel@tonic-gate * Since we're looking up our own (non-global) zone name, 12887c478bd9Sstevel@tonic-gate * we can be assured that it will succeed. 12897c478bd9Sstevel@tonic-gate */ 12907c478bd9Sstevel@tonic-gate result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname)); 12917c478bd9Sstevel@tonic-gate assert(result >= 0); 1292555afedfScarlsonj if (zonecfg_is_scratch(zeptr->zname) && 1293555afedfScarlsonj (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) { 1294555afedfScarlsonj (void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname, 1295555afedfScarlsonj sizeof (zeptr->zname), NULL, 0); 1296555afedfScarlsonj zonecfg_close_scratch(fp); 1297555afedfScarlsonj } 1298555afedfScarlsonj 1299555afedfScarlsonj if (is_system_labeled()) { 130045916cd2Sjpk (void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot, 130145916cd2Sjpk sizeof (zeptr->zroot)); 13029acbbeafSnn35248 (void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME, 13039acbbeafSnn35248 sizeof (zeptr->zbrand)); 1304555afedfScarlsonj } else { 1305555afedfScarlsonj (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot)); 13069acbbeafSnn35248 (void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand, 13079acbbeafSnn35248 sizeof (zeptr->zbrand)); 130845916cd2Sjpk } 1309555afedfScarlsonj 13107c478bd9Sstevel@tonic-gate zeptr->zstate_str = "running"; 1311555afedfScarlsonj if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK && 1312555afedfScarlsonj !uuid_is_null(uuid)) 1313555afedfScarlsonj uuid_unparse(uuid, zeptr->zuuid); 1314f4b3ec61Sdh155122 1315f4b3ec61Sdh155122 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) { 1316f4b3ec61Sdh155122 zperror2(zeptr->zname, gettext("could not get zone flags")); 1317f4b3ec61Sdh155122 exit(Z_ERR); 1318f4b3ec61Sdh155122 } 1319f4b3ec61Sdh155122 if (flags & ZF_NET_EXCL) 1320f4b3ec61Sdh155122 zeptr->ziptype = ZS_EXCLUSIVE; 1321f4b3ec61Sdh155122 else 1322f4b3ec61Sdh155122 zeptr->ziptype = ZS_SHARED; 13237c478bd9Sstevel@tonic-gate } 13247c478bd9Sstevel@tonic-gate 13257c478bd9Sstevel@tonic-gate static int 13267c478bd9Sstevel@tonic-gate list_func(int argc, char *argv[]) 13277c478bd9Sstevel@tonic-gate { 13287c478bd9Sstevel@tonic-gate zone_entry_t *zentp, zent; 1329108322fbScarlsonj int arg, retv; 13307c478bd9Sstevel@tonic-gate boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE; 13317c478bd9Sstevel@tonic-gate zone_state_t min_state = ZONE_STATE_RUNNING; 13327c478bd9Sstevel@tonic-gate zoneid_t zone_id = getzoneid(); 13337c478bd9Sstevel@tonic-gate 13347c478bd9Sstevel@tonic-gate if (target_zone == NULL) { 13357c478bd9Sstevel@tonic-gate /* all zones: default view to running but allow override */ 13367c478bd9Sstevel@tonic-gate optind = 0; 13377c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?cipv")) != EOF) { 13387c478bd9Sstevel@tonic-gate switch (arg) { 13397c478bd9Sstevel@tonic-gate case '?': 13407c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 13417c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 1342475d78a4Sjonb /* 1343475d78a4Sjonb * The 'i' and 'c' options are not mutually 1344475d78a4Sjonb * exclusive so if 'c' is given, then min_state 1345475d78a4Sjonb * is set to 0 (ZONE_STATE_CONFIGURED) which is 1346475d78a4Sjonb * the lowest possible state. If 'i' is given, 1347475d78a4Sjonb * then min_state is set to be the lowest state 1348475d78a4Sjonb * so far. 1349475d78a4Sjonb */ 13507c478bd9Sstevel@tonic-gate case 'c': 13517c478bd9Sstevel@tonic-gate min_state = ZONE_STATE_CONFIGURED; 13527c478bd9Sstevel@tonic-gate break; 13537c478bd9Sstevel@tonic-gate case 'i': 1354475d78a4Sjonb min_state = min(ZONE_STATE_INSTALLED, 1355475d78a4Sjonb min_state); 1356475d78a4Sjonb 13577c478bd9Sstevel@tonic-gate break; 13587c478bd9Sstevel@tonic-gate case 'p': 13597c478bd9Sstevel@tonic-gate parsable = B_TRUE; 13607c478bd9Sstevel@tonic-gate break; 13617c478bd9Sstevel@tonic-gate case 'v': 13627c478bd9Sstevel@tonic-gate verbose = B_TRUE; 13637c478bd9Sstevel@tonic-gate break; 13647c478bd9Sstevel@tonic-gate default: 13657c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 13667c478bd9Sstevel@tonic-gate return (Z_USAGE); 13677c478bd9Sstevel@tonic-gate } 13687c478bd9Sstevel@tonic-gate } 13697c478bd9Sstevel@tonic-gate if (parsable && verbose) { 13707c478bd9Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 13717c478bd9Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 13727c478bd9Sstevel@tonic-gate return (Z_ERR); 13737c478bd9Sstevel@tonic-gate } 137445916cd2Sjpk if (zone_id == GLOBAL_ZONEID || is_system_labeled()) { 1375108322fbScarlsonj retv = zone_print_list(min_state, verbose, parsable); 13767c478bd9Sstevel@tonic-gate } else { 13777c478bd9Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 13789acbbeafSnn35248 retv = Z_OK; 13797c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 13807c478bd9Sstevel@tonic-gate } 1381108322fbScarlsonj return (retv); 13827c478bd9Sstevel@tonic-gate } 13837c478bd9Sstevel@tonic-gate 13847c478bd9Sstevel@tonic-gate /* 13857c478bd9Sstevel@tonic-gate * Specific target zone: disallow -i/-c suboptions. 13867c478bd9Sstevel@tonic-gate */ 13877c478bd9Sstevel@tonic-gate optind = 0; 13887c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?pv")) != EOF) { 13897c478bd9Sstevel@tonic-gate switch (arg) { 13907c478bd9Sstevel@tonic-gate case '?': 13917c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 13927c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 13937c478bd9Sstevel@tonic-gate case 'p': 13947c478bd9Sstevel@tonic-gate parsable = B_TRUE; 13957c478bd9Sstevel@tonic-gate break; 13967c478bd9Sstevel@tonic-gate case 'v': 13977c478bd9Sstevel@tonic-gate verbose = B_TRUE; 13987c478bd9Sstevel@tonic-gate break; 13997c478bd9Sstevel@tonic-gate default: 14007c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14017c478bd9Sstevel@tonic-gate return (Z_USAGE); 14027c478bd9Sstevel@tonic-gate } 14037c478bd9Sstevel@tonic-gate } 14047c478bd9Sstevel@tonic-gate if (parsable && verbose) { 14057c478bd9Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 14067c478bd9Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 14077c478bd9Sstevel@tonic-gate return (Z_ERR); 14087c478bd9Sstevel@tonic-gate } 14097c478bd9Sstevel@tonic-gate if (argc > optind) { 14107c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 14117c478bd9Sstevel@tonic-gate return (Z_USAGE); 14127c478bd9Sstevel@tonic-gate } 14132ec67e04Sgjelinek if (zone_id != GLOBAL_ZONEID && !is_system_labeled()) { 14147c478bd9Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 14157c478bd9Sstevel@tonic-gate /* 14167c478bd9Sstevel@tonic-gate * main() will issue a Z_NO_ZONE error if it cannot get an 14177c478bd9Sstevel@tonic-gate * id for target_zone, which in a non-global zone should 14187c478bd9Sstevel@tonic-gate * happen for any zone name except `zonename`. Thus we 14197c478bd9Sstevel@tonic-gate * assert() that here but don't otherwise check. 14207c478bd9Sstevel@tonic-gate */ 14217c478bd9Sstevel@tonic-gate assert(strcmp(zent.zname, target_zone) == 0); 14227c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 14237c478bd9Sstevel@tonic-gate output = B_TRUE; 14247c478bd9Sstevel@tonic-gate } else if ((zentp = lookup_running_zone(target_zone)) != NULL) { 14257c478bd9Sstevel@tonic-gate zone_print(zentp, verbose, parsable); 14267c478bd9Sstevel@tonic-gate output = B_TRUE; 1427108322fbScarlsonj } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED, 1428108322fbScarlsonj &zent) == Z_OK) { 14297c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 14307c478bd9Sstevel@tonic-gate output = B_TRUE; 14317c478bd9Sstevel@tonic-gate } 1432ce28b40eSzt129084 1433ce28b40eSzt129084 /* 1434ce28b40eSzt129084 * Invoke brand-specific handler. Note that we do this 1435db628066Sgjelinek * only if we're in the global zone, and target_zone is specified 1436db628066Sgjelinek * and it is not the global zone. 1437ce28b40eSzt129084 */ 1438db628066Sgjelinek if (zone_id == GLOBAL_ZONEID && target_zone != NULL && 1439db628066Sgjelinek strcmp(target_zone, GLOBAL_ZONENAME) != 0) 1440ce28b40eSzt129084 if (invoke_brand_handler(CMD_LIST, argv) != Z_OK) 1441ce28b40eSzt129084 return (Z_ERR); 1442ce28b40eSzt129084 14437c478bd9Sstevel@tonic-gate return (output ? Z_OK : Z_ERR); 14447c478bd9Sstevel@tonic-gate } 14457c478bd9Sstevel@tonic-gate 1446ff17c8bfSgjelinek int 1447c75cc341S do_subproc(char *cmdbuf) 14489acbbeafSnn35248 { 14499acbbeafSnn35248 void (*saveint)(int); 14509acbbeafSnn35248 void (*saveterm)(int); 14519acbbeafSnn35248 void (*savequit)(int); 14529acbbeafSnn35248 void (*savehup)(int); 14539acbbeafSnn35248 int pid, child, status; 14549acbbeafSnn35248 14559acbbeafSnn35248 if ((child = vfork()) == 0) { 14569acbbeafSnn35248 (void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL); 14579acbbeafSnn35248 } 14589acbbeafSnn35248 14599acbbeafSnn35248 if (child == -1) 14609acbbeafSnn35248 return (-1); 14619acbbeafSnn35248 14629acbbeafSnn35248 saveint = sigset(SIGINT, SIG_IGN); 14639acbbeafSnn35248 saveterm = sigset(SIGTERM, SIG_IGN); 14649acbbeafSnn35248 savequit = sigset(SIGQUIT, SIG_IGN); 14659acbbeafSnn35248 savehup = sigset(SIGHUP, SIG_IGN); 14669acbbeafSnn35248 14679acbbeafSnn35248 while ((pid = waitpid(child, &status, 0)) != child && pid != -1) 14689acbbeafSnn35248 ; 14699acbbeafSnn35248 14709acbbeafSnn35248 (void) sigset(SIGINT, saveint); 14719acbbeafSnn35248 (void) sigset(SIGTERM, saveterm); 14729acbbeafSnn35248 (void) sigset(SIGQUIT, savequit); 14739acbbeafSnn35248 (void) sigset(SIGHUP, savehup); 14749acbbeafSnn35248 14759acbbeafSnn35248 return (pid == -1 ? -1 : status); 14769acbbeafSnn35248 } 14779acbbeafSnn35248 1478ff17c8bfSgjelinek int 14799acbbeafSnn35248 subproc_status(const char *cmd, int status, boolean_t verbose_failure) 14807c478bd9Sstevel@tonic-gate { 14817c478bd9Sstevel@tonic-gate if (WIFEXITED(status)) { 14827c478bd9Sstevel@tonic-gate int exit_code = WEXITSTATUS(status); 14837c478bd9Sstevel@tonic-gate 14849acbbeafSnn35248 if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK)) 14857c478bd9Sstevel@tonic-gate zerror(gettext("'%s' failed with exit code %d."), cmd, 14867c478bd9Sstevel@tonic-gate exit_code); 14879acbbeafSnn35248 14889acbbeafSnn35248 return (exit_code); 14897c478bd9Sstevel@tonic-gate } else if (WIFSIGNALED(status)) { 14907c478bd9Sstevel@tonic-gate int signal = WTERMSIG(status); 14917c478bd9Sstevel@tonic-gate char sigstr[SIG2STR_MAX]; 14927c478bd9Sstevel@tonic-gate 14937c478bd9Sstevel@tonic-gate if (sig2str(signal, sigstr) == 0) { 14947c478bd9Sstevel@tonic-gate zerror(gettext("'%s' terminated by signal SIG%s."), cmd, 14957c478bd9Sstevel@tonic-gate sigstr); 14967c478bd9Sstevel@tonic-gate } else { 14977c478bd9Sstevel@tonic-gate zerror(gettext("'%s' terminated by an unknown signal."), 14987c478bd9Sstevel@tonic-gate cmd); 14997c478bd9Sstevel@tonic-gate } 15007c478bd9Sstevel@tonic-gate } else { 15017c478bd9Sstevel@tonic-gate zerror(gettext("'%s' failed for unknown reasons."), cmd); 15027c478bd9Sstevel@tonic-gate } 15039acbbeafSnn35248 15049acbbeafSnn35248 /* 15059acbbeafSnn35248 * Assume a subprocess that died due to a signal or an unknown error 15069acbbeafSnn35248 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the 15079acbbeafSnn35248 * user will likely need to do some manual cleanup. 15089acbbeafSnn35248 */ 15099acbbeafSnn35248 return (ZONE_SUBPROC_FATAL); 15107c478bd9Sstevel@tonic-gate } 15117c478bd9Sstevel@tonic-gate 15127c478bd9Sstevel@tonic-gate /* 15137c478bd9Sstevel@tonic-gate * Various sanity checks; make sure: 15147c478bd9Sstevel@tonic-gate * 1. We're in the global zone. 15157c478bd9Sstevel@tonic-gate * 2. The calling user has sufficient privilege. 15167c478bd9Sstevel@tonic-gate * 3. The target zone is neither the global zone nor anything starting with 15177c478bd9Sstevel@tonic-gate * "SUNW". 15187c478bd9Sstevel@tonic-gate * 4a. If we're looking for a 'not running' (i.e., configured or installed) 15197c478bd9Sstevel@tonic-gate * zone, the name service knows about it. 15207c478bd9Sstevel@tonic-gate * 4b. For some operations which expect a zone not to be running, that it is 15217c478bd9Sstevel@tonic-gate * not already running (or ready). 15227c478bd9Sstevel@tonic-gate */ 15237c478bd9Sstevel@tonic-gate static int 15247c478bd9Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running, 15259acbbeafSnn35248 boolean_t unsafe_when_running, boolean_t force) 15267c478bd9Sstevel@tonic-gate { 15277c478bd9Sstevel@tonic-gate zone_entry_t *zent; 15287c478bd9Sstevel@tonic-gate priv_set_t *privset; 15299acbbeafSnn35248 zone_state_t state, min_state; 1530108322fbScarlsonj char kernzone[ZONENAME_MAX]; 1531108322fbScarlsonj FILE *fp; 15327c478bd9Sstevel@tonic-gate 15337c478bd9Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) { 1534ffbafc53Scomay switch (cmd_num) { 1535ffbafc53Scomay case CMD_HALT: 1536ffbafc53Scomay zerror(gettext("use %s to %s this zone."), "halt(1M)", 15377c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 1538ffbafc53Scomay break; 1539ffbafc53Scomay case CMD_REBOOT: 1540ffbafc53Scomay zerror(gettext("use %s to %s this zone."), 1541ffbafc53Scomay "reboot(1M)", cmd_to_str(cmd_num)); 1542ffbafc53Scomay break; 1543ffbafc53Scomay default: 1544ffbafc53Scomay zerror(gettext("must be in the global zone to %s a " 1545ffbafc53Scomay "zone."), cmd_to_str(cmd_num)); 1546ffbafc53Scomay break; 1547ffbafc53Scomay } 15487c478bd9Sstevel@tonic-gate return (Z_ERR); 15497c478bd9Sstevel@tonic-gate } 15507c478bd9Sstevel@tonic-gate 15517c478bd9Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 15527c478bd9Sstevel@tonic-gate zerror(gettext("%s failed"), "priv_allocset"); 15537c478bd9Sstevel@tonic-gate return (Z_ERR); 15547c478bd9Sstevel@tonic-gate } 15557c478bd9Sstevel@tonic-gate 15567c478bd9Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 15577c478bd9Sstevel@tonic-gate zerror(gettext("%s failed"), "getppriv"); 15587c478bd9Sstevel@tonic-gate priv_freeset(privset); 15597c478bd9Sstevel@tonic-gate return (Z_ERR); 15607c478bd9Sstevel@tonic-gate } 15617c478bd9Sstevel@tonic-gate 15627c478bd9Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 15637c478bd9Sstevel@tonic-gate zerror(gettext("only a privileged user may %s a zone."), 15647c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 15657c478bd9Sstevel@tonic-gate priv_freeset(privset); 15667c478bd9Sstevel@tonic-gate return (Z_ERR); 15677c478bd9Sstevel@tonic-gate } 15687c478bd9Sstevel@tonic-gate priv_freeset(privset); 15697c478bd9Sstevel@tonic-gate 15707c478bd9Sstevel@tonic-gate if (zone == NULL) { 15717c478bd9Sstevel@tonic-gate zerror(gettext("no zone specified")); 15727c478bd9Sstevel@tonic-gate return (Z_ERR); 15737c478bd9Sstevel@tonic-gate } 15747c478bd9Sstevel@tonic-gate 15757c478bd9Sstevel@tonic-gate if (strcmp(zone, GLOBAL_ZONENAME) == 0) { 15767c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for the global zone."), 15777c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 15787c478bd9Sstevel@tonic-gate return (Z_ERR); 15797c478bd9Sstevel@tonic-gate } 15807c478bd9Sstevel@tonic-gate 15817c478bd9Sstevel@tonic-gate if (strncmp(zone, "SUNW", 4) == 0) { 15827c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for zones starting " 15837c478bd9Sstevel@tonic-gate "with SUNW."), cmd_to_str(cmd_num)); 15847c478bd9Sstevel@tonic-gate return (Z_ERR); 15857c478bd9Sstevel@tonic-gate } 15867c478bd9Sstevel@tonic-gate 1587108322fbScarlsonj if (!zonecfg_in_alt_root()) { 1588108322fbScarlsonj zent = lookup_running_zone(zone); 1589108322fbScarlsonj } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 1590108322fbScarlsonj zent = NULL; 1591108322fbScarlsonj } else { 1592108322fbScarlsonj if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(), 1593108322fbScarlsonj kernzone, sizeof (kernzone)) == 0) 1594108322fbScarlsonj zent = lookup_running_zone(kernzone); 1595108322fbScarlsonj else 1596108322fbScarlsonj zent = NULL; 1597108322fbScarlsonj zonecfg_close_scratch(fp); 1598108322fbScarlsonj } 1599108322fbScarlsonj 16007c478bd9Sstevel@tonic-gate /* 16017c478bd9Sstevel@tonic-gate * Look up from the kernel for 'running' zones. 16027c478bd9Sstevel@tonic-gate */ 16039acbbeafSnn35248 if (running && !force) { 16047c478bd9Sstevel@tonic-gate if (zent == NULL) { 16057c478bd9Sstevel@tonic-gate zerror(gettext("not running")); 16067c478bd9Sstevel@tonic-gate return (Z_ERR); 16077c478bd9Sstevel@tonic-gate } 16087c478bd9Sstevel@tonic-gate } else { 16097c478bd9Sstevel@tonic-gate int err; 16107c478bd9Sstevel@tonic-gate 16117c478bd9Sstevel@tonic-gate if (unsafe_when_running && zent != NULL) { 16127c478bd9Sstevel@tonic-gate /* check whether the zone is ready or running */ 16137c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, 16147c478bd9Sstevel@tonic-gate &zent->zstate_num)) != Z_OK) { 16157c478bd9Sstevel@tonic-gate errno = err; 16167c478bd9Sstevel@tonic-gate zperror2(zent->zname, 16177c478bd9Sstevel@tonic-gate gettext("could not get state")); 16187c478bd9Sstevel@tonic-gate /* can't tell, so hedge */ 16197c478bd9Sstevel@tonic-gate zent->zstate_str = "ready/running"; 16207c478bd9Sstevel@tonic-gate } else { 16217c478bd9Sstevel@tonic-gate zent->zstate_str = 16227c478bd9Sstevel@tonic-gate zone_state_str(zent->zstate_num); 16237c478bd9Sstevel@tonic-gate } 16247c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for %s zones."), 16257c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num), zent->zstate_str); 16267c478bd9Sstevel@tonic-gate return (Z_ERR); 16277c478bd9Sstevel@tonic-gate } 16287c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zone, &state)) != Z_OK) { 16297c478bd9Sstevel@tonic-gate errno = err; 16307c478bd9Sstevel@tonic-gate zperror2(zone, gettext("could not get state")); 16317c478bd9Sstevel@tonic-gate return (Z_ERR); 16327c478bd9Sstevel@tonic-gate } 16337c478bd9Sstevel@tonic-gate switch (cmd_num) { 16347c478bd9Sstevel@tonic-gate case CMD_UNINSTALL: 16357c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 16367c478bd9Sstevel@tonic-gate zerror(gettext("is already in state '%s'."), 16377c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_CONFIGURED)); 16387c478bd9Sstevel@tonic-gate return (Z_ERR); 16397c478bd9Sstevel@tonic-gate } 16407c478bd9Sstevel@tonic-gate break; 1641ee519a1fSgjelinek case CMD_ATTACH: 1642edfa49ffS if (state == ZONE_STATE_INSTALLED) { 1643edfa49ffS zerror(gettext("is already %s."), 1644edfa49ffS zone_state_str(ZONE_STATE_INSTALLED)); 1645edfa49ffS return (Z_ERR); 1646edfa49ffS } else if (state == ZONE_STATE_INCOMPLETE && !force) { 1647edfa49ffS zerror(gettext("zone is %s; %s required."), 1648edfa49ffS zone_state_str(ZONE_STATE_INCOMPLETE), 1649edfa49ffS cmd_to_str(CMD_UNINSTALL)); 1650edfa49ffS return (Z_ERR); 1651edfa49ffS } 1652edfa49ffS break; 1653865e09a4Sgjelinek case CMD_CLONE: 16547c478bd9Sstevel@tonic-gate case CMD_INSTALL: 16557c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_INSTALLED) { 16567c478bd9Sstevel@tonic-gate zerror(gettext("is already %s."), 16577c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED)); 16587c478bd9Sstevel@tonic-gate return (Z_ERR); 16597c478bd9Sstevel@tonic-gate } else if (state == ZONE_STATE_INCOMPLETE) { 16607c478bd9Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 16617c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 16627c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 16637c478bd9Sstevel@tonic-gate return (Z_ERR); 16647c478bd9Sstevel@tonic-gate } 16657c478bd9Sstevel@tonic-gate break; 1666ee519a1fSgjelinek case CMD_DETACH: 1667865e09a4Sgjelinek case CMD_MOVE: 16687c478bd9Sstevel@tonic-gate case CMD_READY: 16697c478bd9Sstevel@tonic-gate case CMD_BOOT: 1670108322fbScarlsonj case CMD_MOUNT: 1671555afedfScarlsonj case CMD_MARK: 16729acbbeafSnn35248 if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) && 16739acbbeafSnn35248 force) 16749acbbeafSnn35248 min_state = ZONE_STATE_INCOMPLETE; 1675edfa49ffS else if (cmd_num == CMD_MARK) 1676edfa49ffS min_state = ZONE_STATE_CONFIGURED; 16779acbbeafSnn35248 else 16789acbbeafSnn35248 min_state = ZONE_STATE_INSTALLED; 16799acbbeafSnn35248 16809acbbeafSnn35248 if (state < min_state) { 16817c478bd9Sstevel@tonic-gate zerror(gettext("must be %s before %s."), 16829acbbeafSnn35248 zone_state_str(min_state), 16837c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 16847c478bd9Sstevel@tonic-gate return (Z_ERR); 16857c478bd9Sstevel@tonic-gate } 16867c478bd9Sstevel@tonic-gate break; 16877c478bd9Sstevel@tonic-gate case CMD_VERIFY: 16887c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_INCOMPLETE) { 16897c478bd9Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 16907c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 16917c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 16927c478bd9Sstevel@tonic-gate return (Z_ERR); 16937c478bd9Sstevel@tonic-gate } 16947c478bd9Sstevel@tonic-gate break; 1695108322fbScarlsonj case CMD_UNMOUNT: 1696108322fbScarlsonj if (state != ZONE_STATE_MOUNTED) { 1697108322fbScarlsonj zerror(gettext("must be %s before %s."), 1698108322fbScarlsonj zone_state_str(ZONE_STATE_MOUNTED), 1699108322fbScarlsonj cmd_to_str(cmd_num)); 1700108322fbScarlsonj return (Z_ERR); 1701108322fbScarlsonj } 1702108322fbScarlsonj break; 1703fbbfbc6eSjv227347 case CMD_SYSBOOT: 1704fbbfbc6eSjv227347 if (state != ZONE_STATE_INSTALLED) { 1705fbbfbc6eSjv227347 zerror(gettext("%s operation is invalid for %s " 1706fbbfbc6eSjv227347 "zones."), cmd_to_str(cmd_num), 1707fbbfbc6eSjv227347 zone_state_str(state)); 1708fbbfbc6eSjv227347 return (Z_ERR); 1709fbbfbc6eSjv227347 } 1710fbbfbc6eSjv227347 break; 17117c478bd9Sstevel@tonic-gate } 17127c478bd9Sstevel@tonic-gate } 17137c478bd9Sstevel@tonic-gate return (Z_OK); 17147c478bd9Sstevel@tonic-gate } 17157c478bd9Sstevel@tonic-gate 17167c478bd9Sstevel@tonic-gate static int 17177c478bd9Sstevel@tonic-gate halt_func(int argc, char *argv[]) 17187c478bd9Sstevel@tonic-gate { 17197c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 17207c478bd9Sstevel@tonic-gate int arg; 17217c478bd9Sstevel@tonic-gate 1722108322fbScarlsonj if (zonecfg_in_alt_root()) { 1723108322fbScarlsonj zerror(gettext("cannot halt zone in alternate root")); 1724108322fbScarlsonj return (Z_ERR); 1725108322fbScarlsonj } 1726108322fbScarlsonj 17277c478bd9Sstevel@tonic-gate optind = 0; 17287c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 17297c478bd9Sstevel@tonic-gate switch (arg) { 17307c478bd9Sstevel@tonic-gate case '?': 17317c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 17327c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 17337c478bd9Sstevel@tonic-gate default: 17347c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 17357c478bd9Sstevel@tonic-gate return (Z_USAGE); 17367c478bd9Sstevel@tonic-gate } 17377c478bd9Sstevel@tonic-gate } 17387c478bd9Sstevel@tonic-gate if (argc > optind) { 17397c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 17407c478bd9Sstevel@tonic-gate return (Z_USAGE); 17417c478bd9Sstevel@tonic-gate } 17427c478bd9Sstevel@tonic-gate /* 17437c478bd9Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 17447c478bd9Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 17457c478bd9Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 17467c478bd9Sstevel@tonic-gate */ 17479acbbeafSnn35248 if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE) 17489acbbeafSnn35248 != Z_OK) 17497c478bd9Sstevel@tonic-gate return (Z_ERR); 17507c478bd9Sstevel@tonic-gate 1751ce28b40eSzt129084 /* 1752ce28b40eSzt129084 * Invoke brand-specific handler. 1753ce28b40eSzt129084 */ 1754ce28b40eSzt129084 if (invoke_brand_handler(CMD_HALT, argv) != Z_OK) 1755ce28b40eSzt129084 return (Z_ERR); 1756ce28b40eSzt129084 17577c478bd9Sstevel@tonic-gate zarg.cmd = Z_HALT; 1758ff17c8bfSgjelinek return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale, 1759ff17c8bfSgjelinek B_TRUE) == 0) ? Z_OK : Z_ERR); 17607c478bd9Sstevel@tonic-gate } 17617c478bd9Sstevel@tonic-gate 17627c478bd9Sstevel@tonic-gate static int 17637c478bd9Sstevel@tonic-gate reboot_func(int argc, char *argv[]) 17647c478bd9Sstevel@tonic-gate { 17657c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 17667c478bd9Sstevel@tonic-gate int arg; 17677c478bd9Sstevel@tonic-gate 1768108322fbScarlsonj if (zonecfg_in_alt_root()) { 1769108322fbScarlsonj zerror(gettext("cannot reboot zone in alternate root")); 1770108322fbScarlsonj return (Z_ERR); 1771108322fbScarlsonj } 1772108322fbScarlsonj 17737c478bd9Sstevel@tonic-gate optind = 0; 17747c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 17757c478bd9Sstevel@tonic-gate switch (arg) { 17767c478bd9Sstevel@tonic-gate case '?': 17777c478bd9Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 17787c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 17797c478bd9Sstevel@tonic-gate default: 17807c478bd9Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 17817c478bd9Sstevel@tonic-gate return (Z_USAGE); 17827c478bd9Sstevel@tonic-gate } 17837c478bd9Sstevel@tonic-gate } 17843f2f09c1Sdp 17853f2f09c1Sdp zarg.bootbuf[0] = '\0'; 17863f2f09c1Sdp for (; optind < argc; optind++) { 17873f2f09c1Sdp if (strlcat(zarg.bootbuf, argv[optind], 17883f2f09c1Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 17893f2f09c1Sdp zerror(gettext("Boot argument list too long")); 17903f2f09c1Sdp return (Z_ERR); 17917c478bd9Sstevel@tonic-gate } 17923f2f09c1Sdp if (optind < argc - 1) 17933f2f09c1Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 17943f2f09c1Sdp sizeof (zarg.bootbuf)) { 17953f2f09c1Sdp zerror(gettext("Boot argument list too long")); 17963f2f09c1Sdp return (Z_ERR); 17973f2f09c1Sdp } 17983f2f09c1Sdp } 17993f2f09c1Sdp 18003f2f09c1Sdp 18017c478bd9Sstevel@tonic-gate /* 18027c478bd9Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 18037c478bd9Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 18047c478bd9Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 18057c478bd9Sstevel@tonic-gate */ 18069acbbeafSnn35248 if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE) 18079acbbeafSnn35248 != Z_OK) 18087c478bd9Sstevel@tonic-gate return (Z_ERR); 1809ce28b40eSzt129084 if (verify_details(CMD_REBOOT, argv) != Z_OK) 1810b5abaf04Sgjelinek return (Z_ERR); 18117c478bd9Sstevel@tonic-gate 18127c478bd9Sstevel@tonic-gate zarg.cmd = Z_REBOOT; 1813ff17c8bfSgjelinek return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) == 0) 1814ff17c8bfSgjelinek ? Z_OK : Z_ERR); 1815ff17c8bfSgjelinek } 1816ff17c8bfSgjelinek 1817ff17c8bfSgjelinek static int 1818ff17c8bfSgjelinek get_hook(brand_handle_t bh, char *cmd, size_t len, int (*bp)(brand_handle_t, 1819ff17c8bfSgjelinek const char *, const char *, char *, size_t), char *zonename, char *zonepath) 1820ff17c8bfSgjelinek { 1821ff17c8bfSgjelinek if (strlcpy(cmd, EXEC_PREFIX, len) >= len) 1822ff17c8bfSgjelinek return (Z_ERR); 1823ff17c8bfSgjelinek 1824ff17c8bfSgjelinek if (bp(bh, zonename, zonepath, cmd + EXEC_LEN, len - EXEC_LEN) != 0) 1825ff17c8bfSgjelinek return (Z_ERR); 1826ff17c8bfSgjelinek 1827ff17c8bfSgjelinek if (strlen(cmd) <= EXEC_LEN) 1828ff17c8bfSgjelinek cmd[0] = '\0'; 1829ff17c8bfSgjelinek 1830ff17c8bfSgjelinek return (Z_OK); 18317c478bd9Sstevel@tonic-gate } 18327c478bd9Sstevel@tonic-gate 18337c478bd9Sstevel@tonic-gate static int 1834ce28b40eSzt129084 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[]) 18359acbbeafSnn35248 { 18369acbbeafSnn35248 char cmdbuf[MAXPATHLEN]; 18379acbbeafSnn35248 int err; 18389acbbeafSnn35248 char zonepath[MAXPATHLEN]; 1839123807fbSedp brand_handle_t bh = NULL; 1840ce28b40eSzt129084 int status, i; 18419acbbeafSnn35248 18429acbbeafSnn35248 /* 18439acbbeafSnn35248 * Fetch the verify command from the brand configuration. 18449acbbeafSnn35248 * "exec" the command so that the returned status is that of 18459acbbeafSnn35248 * the command and not the shell. 18469acbbeafSnn35248 */ 1847ff17c8bfSgjelinek if (handle == NULL) { 1848ff17c8bfSgjelinek (void) strlcpy(zonepath, "-", sizeof (zonepath)); 1849ff17c8bfSgjelinek } else if ((err = zonecfg_get_zonepath(handle, zonepath, 1850ff17c8bfSgjelinek sizeof (zonepath))) != Z_OK) { 18519acbbeafSnn35248 errno = err; 1852ce28b40eSzt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 18539acbbeafSnn35248 return (Z_ERR); 18549acbbeafSnn35248 } 1855123807fbSedp if ((bh = brand_open(target_brand)) == NULL) { 18569acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 18579acbbeafSnn35248 return (Z_ERR); 18589acbbeafSnn35248 } 18599acbbeafSnn35248 18609acbbeafSnn35248 /* 18619acbbeafSnn35248 * If the brand has its own verification routine, execute it now. 1862ce28b40eSzt129084 * The verification routine validates the intended zoneadm 1863ce28b40eSzt129084 * operation for the specific brand. The zoneadm subcommand and 1864ce28b40eSzt129084 * all its arguments are passed to the routine. 18659acbbeafSnn35248 */ 1866ff17c8bfSgjelinek err = get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_verify_adm, 1867ff17c8bfSgjelinek target_zone, zonepath); 1868123807fbSedp brand_close(bh); 1869ff17c8bfSgjelinek if (err != Z_OK) 1870ce28b40eSzt129084 return (Z_BRAND_ERROR); 1871ff17c8bfSgjelinek if (cmdbuf[0] == '\0') 1872ce28b40eSzt129084 return (Z_OK); 1873ce28b40eSzt129084 1874ce28b40eSzt129084 if (strlcat(cmdbuf, cmd_to_str(cmd_num), 1875ce28b40eSzt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf)) 1876ce28b40eSzt129084 return (Z_ERR); 1877ce28b40eSzt129084 1878ce28b40eSzt129084 /* Build the argv string */ 1879ce28b40eSzt129084 i = 0; 1880ce28b40eSzt129084 while (argv[i] != NULL) { 1881ce28b40eSzt129084 if ((strlcat(cmdbuf, " ", 1882ce28b40eSzt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf)) || 1883ce28b40eSzt129084 (strlcat(cmdbuf, argv[i++], 1884ce28b40eSzt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf))) 1885ce28b40eSzt129084 return (Z_ERR); 1886ce28b40eSzt129084 } 1887ce28b40eSzt129084 1888d9e728a2Sgjelinek status = do_subproc(cmdbuf); 18899acbbeafSnn35248 err = subproc_status(gettext("brand-specific verification"), 18909acbbeafSnn35248 status, B_FALSE); 18919acbbeafSnn35248 1892ce28b40eSzt129084 return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR); 18939acbbeafSnn35248 } 18949acbbeafSnn35248 18959acbbeafSnn35248 static int 18967c478bd9Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle) 18977c478bd9Sstevel@tonic-gate { 18987c478bd9Sstevel@tonic-gate struct zone_rctltab rctltab; 18997c478bd9Sstevel@tonic-gate size_t rbs = rctlblk_size(); 19007c478bd9Sstevel@tonic-gate rctlblk_t *rctlblk; 19017c478bd9Sstevel@tonic-gate int error = Z_INVAL; 19027c478bd9Sstevel@tonic-gate 19037c478bd9Sstevel@tonic-gate if ((rctlblk = malloc(rbs)) == NULL) { 19047c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rbs, 19057c478bd9Sstevel@tonic-gate strerror(errno)); 19067c478bd9Sstevel@tonic-gate return (Z_NOMEM); 19077c478bd9Sstevel@tonic-gate } 19087c478bd9Sstevel@tonic-gate 19097c478bd9Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 19107c478bd9Sstevel@tonic-gate zerror(gettext("zonecfg_setrctlent failed")); 19117c478bd9Sstevel@tonic-gate free(rctlblk); 19127c478bd9Sstevel@tonic-gate return (error); 19137c478bd9Sstevel@tonic-gate } 19147c478bd9Sstevel@tonic-gate 19157c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 19167c478bd9Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 19177c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 19187c478bd9Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 19197c478bd9Sstevel@tonic-gate 19207c478bd9Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 19217c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: Ignoring unrecognized rctl " 19227c478bd9Sstevel@tonic-gate "'%s'."), name); 19237c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 19247c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 19257c478bd9Sstevel@tonic-gate continue; 19267c478bd9Sstevel@tonic-gate } 19277c478bd9Sstevel@tonic-gate 19287c478bd9Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 19297c478bd9Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 19307c478bd9Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 19317c478bd9Sstevel@tonic-gate != Z_OK) { 19327c478bd9Sstevel@tonic-gate zerror(gettext("invalid rctl value: " 19337c478bd9Sstevel@tonic-gate "(priv=%s,limit=%s,action%s)"), 19347c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 19357c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 19367c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action); 19377c478bd9Sstevel@tonic-gate goto out; 19387c478bd9Sstevel@tonic-gate } 19397c478bd9Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 19407c478bd9Sstevel@tonic-gate zerror(gettext("(priv=%s,limit=%s,action=%s) " 19417c478bd9Sstevel@tonic-gate "is not a valid value for rctl '%s'"), 19427c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 19437c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 19447c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action, 19457c478bd9Sstevel@tonic-gate name); 19467c478bd9Sstevel@tonic-gate goto out; 19477c478bd9Sstevel@tonic-gate } 19487c478bd9Sstevel@tonic-gate } 19497c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 19507c478bd9Sstevel@tonic-gate } 19517c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 19527c478bd9Sstevel@tonic-gate error = Z_OK; 19537c478bd9Sstevel@tonic-gate out: 19547c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 19557c478bd9Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 19567c478bd9Sstevel@tonic-gate free(rctlblk); 19577c478bd9Sstevel@tonic-gate return (error); 19587c478bd9Sstevel@tonic-gate } 19597c478bd9Sstevel@tonic-gate 19607c478bd9Sstevel@tonic-gate static int 19617c478bd9Sstevel@tonic-gate verify_pool(zone_dochandle_t handle) 19627c478bd9Sstevel@tonic-gate { 19637c478bd9Sstevel@tonic-gate char poolname[MAXPATHLEN]; 19647c478bd9Sstevel@tonic-gate pool_conf_t *poolconf; 19657c478bd9Sstevel@tonic-gate pool_t *pool; 19667c478bd9Sstevel@tonic-gate int status; 19677c478bd9Sstevel@tonic-gate int error; 19687c478bd9Sstevel@tonic-gate 19697c478bd9Sstevel@tonic-gate /* 19707c478bd9Sstevel@tonic-gate * This ends up being very similar to the check done in zoneadmd. 19717c478bd9Sstevel@tonic-gate */ 19727c478bd9Sstevel@tonic-gate error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 19737c478bd9Sstevel@tonic-gate if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 19747c478bd9Sstevel@tonic-gate /* 19757c478bd9Sstevel@tonic-gate * No pool specified. 19767c478bd9Sstevel@tonic-gate */ 19777c478bd9Sstevel@tonic-gate return (0); 19787c478bd9Sstevel@tonic-gate } 19797c478bd9Sstevel@tonic-gate if (error != Z_OK) { 19807c478bd9Sstevel@tonic-gate zperror(gettext("Unable to retrieve pool name from " 19817c478bd9Sstevel@tonic-gate "configuration"), B_TRUE); 19827c478bd9Sstevel@tonic-gate return (error); 19837c478bd9Sstevel@tonic-gate } 19847c478bd9Sstevel@tonic-gate /* 19857c478bd9Sstevel@tonic-gate * Don't do anything if pools aren't enabled. 19867c478bd9Sstevel@tonic-gate */ 19877c478bd9Sstevel@tonic-gate if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 19887c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pools facility not active; " 19897c478bd9Sstevel@tonic-gate "zone will not be bound to pool '%s'."), poolname); 19907c478bd9Sstevel@tonic-gate return (Z_OK); 19917c478bd9Sstevel@tonic-gate } 19927c478bd9Sstevel@tonic-gate /* 19937c478bd9Sstevel@tonic-gate * Try to provide a sane error message if the requested pool doesn't 19947c478bd9Sstevel@tonic-gate * exist. It isn't clear that pools-related failures should 19957c478bd9Sstevel@tonic-gate * necessarily translate to a failure to verify the zone configuration, 19967c478bd9Sstevel@tonic-gate * hence they are not considered errors. 19977c478bd9Sstevel@tonic-gate */ 19987c478bd9Sstevel@tonic-gate if ((poolconf = pool_conf_alloc()) == NULL) { 19997c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_alloc failed; " 20007c478bd9Sstevel@tonic-gate "using default pool")); 20017c478bd9Sstevel@tonic-gate return (Z_OK); 20027c478bd9Sstevel@tonic-gate } 20037c478bd9Sstevel@tonic-gate if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 20047c478bd9Sstevel@tonic-gate PO_SUCCESS) { 20057c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_open failed; " 20067c478bd9Sstevel@tonic-gate "using default pool")); 20077c478bd9Sstevel@tonic-gate pool_conf_free(poolconf); 20087c478bd9Sstevel@tonic-gate return (Z_OK); 20097c478bd9Sstevel@tonic-gate } 20107c478bd9Sstevel@tonic-gate pool = pool_get_pool(poolconf, poolname); 20117c478bd9Sstevel@tonic-gate (void) pool_conf_close(poolconf); 20127c478bd9Sstevel@tonic-gate pool_conf_free(poolconf); 20137c478bd9Sstevel@tonic-gate if (pool == NULL) { 20147c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool '%s' not found. " 20157c478bd9Sstevel@tonic-gate "using default pool"), poolname); 20167c478bd9Sstevel@tonic-gate } 20177c478bd9Sstevel@tonic-gate 20187c478bd9Sstevel@tonic-gate return (Z_OK); 20197c478bd9Sstevel@tonic-gate } 20207c478bd9Sstevel@tonic-gate 20217c478bd9Sstevel@tonic-gate static int 2022b5abaf04Sgjelinek verify_ipd(zone_dochandle_t handle) 2023b5abaf04Sgjelinek { 2024b5abaf04Sgjelinek int return_code = Z_OK; 2025b5abaf04Sgjelinek struct zone_fstab fstab; 2026b5abaf04Sgjelinek struct stat st; 2027b5abaf04Sgjelinek char specdir[MAXPATHLEN]; 2028b5abaf04Sgjelinek 2029b5abaf04Sgjelinek if (zonecfg_setipdent(handle) != Z_OK) { 20305ee84fbdSgjelinek /* 20315ee84fbdSgjelinek * TRANSLATION_NOTE 20325ee84fbdSgjelinek * inherit-pkg-dirs is a literal that should not be translated. 20335ee84fbdSgjelinek */ 20345ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify " 2035b5abaf04Sgjelinek "inherit-pkg-dirs: unable to enumerate mounts\n")); 2036b5abaf04Sgjelinek return (Z_ERR); 2037b5abaf04Sgjelinek } 2038b5abaf04Sgjelinek while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 2039b5abaf04Sgjelinek /* 2040b5abaf04Sgjelinek * Verify fs_dir exists. 2041b5abaf04Sgjelinek */ 2042b5abaf04Sgjelinek (void) snprintf(specdir, sizeof (specdir), "%s%s", 2043b5abaf04Sgjelinek zonecfg_get_root(), fstab.zone_fs_dir); 2044b5abaf04Sgjelinek if (stat(specdir, &st) != 0) { 20455ee84fbdSgjelinek /* 20465ee84fbdSgjelinek * TRANSLATION_NOTE 20475ee84fbdSgjelinek * inherit-pkg-dir is a literal that should not be 20485ee84fbdSgjelinek * translated. 20495ee84fbdSgjelinek */ 20505ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify " 2051b5abaf04Sgjelinek "inherit-pkg-dir %s: %s\n"), 2052b5abaf04Sgjelinek fstab.zone_fs_dir, strerror(errno)); 2053b5abaf04Sgjelinek return_code = Z_ERR; 2054b5abaf04Sgjelinek } 2055b5abaf04Sgjelinek if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 20565ee84fbdSgjelinek /* 20575ee84fbdSgjelinek * TRANSLATION_NOTE 20585ee84fbdSgjelinek * inherit-pkg-dir and NFS are literals that should 20595ee84fbdSgjelinek * not be translated. 20605ee84fbdSgjelinek */ 2061b5abaf04Sgjelinek (void) fprintf(stderr, gettext("cannot verify " 20620b5de56dSgjelinek "inherit-pkg-dir %s: NFS mounted file system.\n" 20630b5de56dSgjelinek "\tA local file system must be used.\n"), 2064b5abaf04Sgjelinek fstab.zone_fs_dir); 2065b5abaf04Sgjelinek return_code = Z_ERR; 2066b5abaf04Sgjelinek } 2067b5abaf04Sgjelinek } 2068b5abaf04Sgjelinek (void) zonecfg_endipdent(handle); 2069b5abaf04Sgjelinek 2070b5abaf04Sgjelinek return (return_code); 2071b5abaf04Sgjelinek } 2072b5abaf04Sgjelinek 207320c8013fSlling /* 207420c8013fSlling * Verify that the special device/file system exists and is valid. 207520c8013fSlling */ 207620c8013fSlling static int 207720c8013fSlling verify_fs_special(struct zone_fstab *fstab) 207820c8013fSlling { 207993239addSjohnlev struct stat64 st; 208020c8013fSlling 20816cc813faSgjelinek /* 20826cc813faSgjelinek * This validation is really intended for standard zone administration. 20836cc813faSgjelinek * If we are in a mini-root or some other upgrade situation where 20846cc813faSgjelinek * we are using the scratch zone, just by-pass this. 20856cc813faSgjelinek */ 20866cc813faSgjelinek if (zonecfg_in_alt_root()) 20876cc813faSgjelinek return (Z_OK); 20886cc813faSgjelinek 208920c8013fSlling if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0) 209020c8013fSlling return (verify_fs_zfs(fstab)); 209120c8013fSlling 209293239addSjohnlev if (stat64(fstab->zone_fs_special, &st) != 0) { 20935c358068Slling (void) fprintf(stderr, gettext("could not verify fs " 209420c8013fSlling "%s: could not access %s: %s\n"), fstab->zone_fs_dir, 209520c8013fSlling fstab->zone_fs_special, strerror(errno)); 209620c8013fSlling return (Z_ERR); 209720c8013fSlling } 209820c8013fSlling 209920c8013fSlling if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 210020c8013fSlling /* 210120c8013fSlling * TRANSLATION_NOTE 210220c8013fSlling * fs and NFS are literals that should 210320c8013fSlling * not be translated. 210420c8013fSlling */ 210520c8013fSlling (void) fprintf(stderr, gettext("cannot verify " 21060b5de56dSgjelinek "fs %s: NFS mounted file system.\n" 21070b5de56dSgjelinek "\tA local file system must be used.\n"), 210820c8013fSlling fstab->zone_fs_special); 210920c8013fSlling return (Z_ERR); 211020c8013fSlling } 211120c8013fSlling 211220c8013fSlling return (Z_OK); 211320c8013fSlling } 211420c8013fSlling 2115b5abaf04Sgjelinek static int 211693239addSjohnlev isregfile(const char *path) 211793239addSjohnlev { 211893239addSjohnlev struct stat64 st; 211993239addSjohnlev 212093239addSjohnlev if (stat64(path, &st) == -1) 212193239addSjohnlev return (-1); 212293239addSjohnlev 212393239addSjohnlev return (S_ISREG(st.st_mode)); 212493239addSjohnlev } 212593239addSjohnlev 212693239addSjohnlev static int 21277c478bd9Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle) 21287c478bd9Sstevel@tonic-gate { 21297c478bd9Sstevel@tonic-gate int return_code = Z_OK; 21307c478bd9Sstevel@tonic-gate struct zone_fstab fstab; 21317c478bd9Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 21327c478bd9Sstevel@tonic-gate struct stat st; 21337c478bd9Sstevel@tonic-gate 21347c478bd9Sstevel@tonic-gate /* 21357c478bd9Sstevel@tonic-gate * No need to verify inherit-pkg-dir fs types, as their type is 21367c478bd9Sstevel@tonic-gate * implicitly lofs, which is known. Therefore, the types are only 21377c478bd9Sstevel@tonic-gate * verified for regular file systems below. 21387c478bd9Sstevel@tonic-gate * 21397c478bd9Sstevel@tonic-gate * Since the actual mount point is not known until the dependent mounts 21407c478bd9Sstevel@tonic-gate * are performed, we don't attempt any path validation here: that will 21417c478bd9Sstevel@tonic-gate * happen later when zoneadmd actually does the mounts. 21427c478bd9Sstevel@tonic-gate */ 21437c478bd9Sstevel@tonic-gate if (zonecfg_setfsent(handle) != Z_OK) { 21440b5de56dSgjelinek (void) fprintf(stderr, gettext("could not verify file systems: " 21457c478bd9Sstevel@tonic-gate "unable to enumerate mounts\n")); 21467c478bd9Sstevel@tonic-gate return (Z_ERR); 21477c478bd9Sstevel@tonic-gate } 21487c478bd9Sstevel@tonic-gate while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 21497c478bd9Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) { 21507c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 21517c478bd9Sstevel@tonic-gate "type %s is not allowed.\n"), fstab.zone_fs_dir, 21527c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 21537c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21547c478bd9Sstevel@tonic-gate goto next_fs; 21557c478bd9Sstevel@tonic-gate } 21567c478bd9Sstevel@tonic-gate /* 21577c478bd9Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/mount exists. 21587c478bd9Sstevel@tonic-gate */ 21597c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", 21607c478bd9Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 21617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 21627c478bd9Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 21637c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 21647c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21657c478bd9Sstevel@tonic-gate goto next_fs; 21667c478bd9Sstevel@tonic-gate } 21677c478bd9Sstevel@tonic-gate if (stat(cmdbuf, &st) != 0) { 21685ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 21695ee84fbdSgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 21707c478bd9Sstevel@tonic-gate cmdbuf, strerror(errno)); 21717c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21727c478bd9Sstevel@tonic-gate goto next_fs; 21737c478bd9Sstevel@tonic-gate } 21747c478bd9Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 21755ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 21765ee84fbdSgjelinek "%s: %s is not a regular file\n"), 21775ee84fbdSgjelinek fstab.zone_fs_dir, cmdbuf); 21787c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21797c478bd9Sstevel@tonic-gate goto next_fs; 21807c478bd9Sstevel@tonic-gate } 21817c478bd9Sstevel@tonic-gate /* 218293239addSjohnlev * If zone_fs_raw is set, verify that there's an fsck 218393239addSjohnlev * binary for it. If zone_fs_raw is not set, and it's 218493239addSjohnlev * not a regular file (lofi mount), and there's an fsck 218593239addSjohnlev * binary for it, complain. 21867c478bd9Sstevel@tonic-gate */ 21877c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", 21887c478bd9Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 21897c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 21907c478bd9Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 21917c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 21927c478bd9Sstevel@tonic-gate return_code = Z_ERR; 21937c478bd9Sstevel@tonic-gate goto next_fs; 21947c478bd9Sstevel@tonic-gate } 21957c478bd9Sstevel@tonic-gate if (fstab.zone_fs_raw[0] != '\0' && 21967c478bd9Sstevel@tonic-gate (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) { 21977c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 21987c478bd9Sstevel@tonic-gate "'raw' device specified but " 21997c478bd9Sstevel@tonic-gate "no fsck executable exists for %s\n"), 22007c478bd9Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 22017c478bd9Sstevel@tonic-gate return_code = Z_ERR; 22027c478bd9Sstevel@tonic-gate goto next_fs; 220393239addSjohnlev } else if (fstab.zone_fs_raw[0] == '\0' && 220493239addSjohnlev stat(cmdbuf, &st) == 0 && 220593239addSjohnlev isregfile(fstab.zone_fs_special) != 1) { 220693239addSjohnlev (void) fprintf(stderr, gettext("could not verify fs " 220793239addSjohnlev "%s: must specify 'raw' device for %s " 220893239addSjohnlev "file systems\n"), 220993239addSjohnlev fstab.zone_fs_dir, fstab.zone_fs_type); 221093239addSjohnlev return_code = Z_ERR; 221193239addSjohnlev goto next_fs; 22127c478bd9Sstevel@tonic-gate } 221320c8013fSlling 221420c8013fSlling /* Verify fs_special. */ 221520c8013fSlling if ((return_code = verify_fs_special(&fstab)) != Z_OK) 2216b5abaf04Sgjelinek goto next_fs; 221720c8013fSlling 221820c8013fSlling /* Verify fs_raw. */ 2219b5abaf04Sgjelinek if (fstab.zone_fs_raw[0] != '\0' && 2220b5abaf04Sgjelinek stat(fstab.zone_fs_raw, &st) != 0) { 22215ee84fbdSgjelinek /* 22225ee84fbdSgjelinek * TRANSLATION_NOTE 22235ee84fbdSgjelinek * fs is a literal that should not be translated. 22245ee84fbdSgjelinek */ 22255ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 22265ee84fbdSgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 2227b5abaf04Sgjelinek fstab.zone_fs_raw, strerror(errno)); 2228b5abaf04Sgjelinek return_code = Z_ERR; 2229b5abaf04Sgjelinek goto next_fs; 2230b5abaf04Sgjelinek } 22317c478bd9Sstevel@tonic-gate next_fs: 22327c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(fstab.zone_fs_options); 22337c478bd9Sstevel@tonic-gate } 22347c478bd9Sstevel@tonic-gate (void) zonecfg_endfsent(handle); 22357c478bd9Sstevel@tonic-gate 22367c478bd9Sstevel@tonic-gate return (return_code); 22377c478bd9Sstevel@tonic-gate } 22387c478bd9Sstevel@tonic-gate 22397c478bd9Sstevel@tonic-gate static int 2240ffbafc53Scomay verify_limitpriv(zone_dochandle_t handle) 2241ffbafc53Scomay { 2242ffbafc53Scomay char *privname = NULL; 2243ffbafc53Scomay int err; 2244ffbafc53Scomay priv_set_t *privs; 2245ffbafc53Scomay 2246ffbafc53Scomay if ((privs = priv_allocset()) == NULL) { 2247ffbafc53Scomay zperror(gettext("failed to allocate privilege set"), B_FALSE); 2248ffbafc53Scomay return (Z_NOMEM); 2249ffbafc53Scomay } 2250ffbafc53Scomay err = zonecfg_get_privset(handle, privs, &privname); 2251ffbafc53Scomay switch (err) { 2252ffbafc53Scomay case Z_OK: 2253ffbafc53Scomay break; 2254ffbafc53Scomay case Z_PRIV_PROHIBITED: 2255ffbafc53Scomay (void) fprintf(stderr, gettext("privilege \"%s\" is not " 2256ffbafc53Scomay "permitted within the zone's privilege set\n"), privname); 2257ffbafc53Scomay break; 2258ffbafc53Scomay case Z_PRIV_REQUIRED: 2259ffbafc53Scomay (void) fprintf(stderr, gettext("required privilege \"%s\" is " 2260ffbafc53Scomay "missing from the zone's privilege set\n"), privname); 2261ffbafc53Scomay break; 2262ffbafc53Scomay case Z_PRIV_UNKNOWN: 2263ffbafc53Scomay (void) fprintf(stderr, gettext("unknown privilege \"%s\" " 2264ffbafc53Scomay "specified in the zone's privilege set\n"), privname); 2265ffbafc53Scomay break; 2266ffbafc53Scomay default: 2267ffbafc53Scomay zperror( 2268ffbafc53Scomay gettext("failed to determine the zone's privilege set"), 2269ffbafc53Scomay B_TRUE); 2270ffbafc53Scomay break; 2271ffbafc53Scomay } 2272ffbafc53Scomay free(privname); 2273ffbafc53Scomay priv_freeset(privs); 2274ffbafc53Scomay return (err); 2275ffbafc53Scomay } 2276ffbafc53Scomay 22771390a385Sgjelinek static void 22781390a385Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list) 22791390a385Sgjelinek { 22801390a385Sgjelinek int i; 22811390a385Sgjelinek 22821390a385Sgjelinek for (i = 0; i < if_cnt; i++) { 22831390a385Sgjelinek free(if_list[i]->name); 22841390a385Sgjelinek free(if_list[i]); 22851390a385Sgjelinek } 22861390a385Sgjelinek free(if_list); 22871390a385Sgjelinek } 22881390a385Sgjelinek 22891390a385Sgjelinek /* 22901390a385Sgjelinek * Get a list of the network interfaces, along with their address families, 22911390a385Sgjelinek * that are plumbed in the global zone. See if_tcp(7p) for a description 22921390a385Sgjelinek * of the ioctls used here. 22931390a385Sgjelinek */ 22941390a385Sgjelinek static int 22951390a385Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list) 22961390a385Sgjelinek { 22971390a385Sgjelinek int s; 22981390a385Sgjelinek int i; 22991390a385Sgjelinek int res = Z_OK; 23001390a385Sgjelinek int space_needed; 23011390a385Sgjelinek int cnt = 0; 23021390a385Sgjelinek struct lifnum if_num; 23031390a385Sgjelinek struct lifconf if_conf; 23041390a385Sgjelinek struct lifreq *if_reqp; 23051390a385Sgjelinek char *if_buf; 23061390a385Sgjelinek struct net_if **local_ifs = NULL; 23071390a385Sgjelinek 23081390a385Sgjelinek *if_cnt = 0; 23091390a385Sgjelinek *if_list = NULL; 23101390a385Sgjelinek 23111390a385Sgjelinek if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0) 23121390a385Sgjelinek return (Z_ERR); 23131390a385Sgjelinek 23141390a385Sgjelinek /* 23151390a385Sgjelinek * Come back here in the unlikely event that the number of interfaces 23161390a385Sgjelinek * increases between the time we get the count and the time we do the 23171390a385Sgjelinek * SIOCGLIFCONF ioctl. 23181390a385Sgjelinek */ 23191390a385Sgjelinek retry: 23201390a385Sgjelinek /* Get the number of interfaces. */ 23211390a385Sgjelinek if_num.lifn_family = AF_UNSPEC; 23221390a385Sgjelinek if_num.lifn_flags = LIFC_NOXMIT; 23231390a385Sgjelinek if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) { 23241390a385Sgjelinek (void) close(s); 23251390a385Sgjelinek return (Z_ERR); 23261390a385Sgjelinek } 23271390a385Sgjelinek 23281390a385Sgjelinek /* Get the interface configuration list. */ 23291390a385Sgjelinek space_needed = if_num.lifn_count * sizeof (struct lifreq); 23301390a385Sgjelinek if ((if_buf = malloc(space_needed)) == NULL) { 23311390a385Sgjelinek (void) close(s); 23321390a385Sgjelinek return (Z_ERR); 23331390a385Sgjelinek } 23341390a385Sgjelinek if_conf.lifc_family = AF_UNSPEC; 23351390a385Sgjelinek if_conf.lifc_flags = LIFC_NOXMIT; 23361390a385Sgjelinek if_conf.lifc_len = space_needed; 23371390a385Sgjelinek if_conf.lifc_buf = if_buf; 23381390a385Sgjelinek if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) { 23391390a385Sgjelinek free(if_buf); 23401390a385Sgjelinek /* 23411390a385Sgjelinek * SIOCGLIFCONF returns EINVAL if the buffer we passed in is 23421390a385Sgjelinek * too small. In this case go back and get the new if cnt. 23431390a385Sgjelinek */ 23441390a385Sgjelinek if (errno == EINVAL) 23451390a385Sgjelinek goto retry; 23461390a385Sgjelinek 23471390a385Sgjelinek (void) close(s); 23481390a385Sgjelinek return (Z_ERR); 23491390a385Sgjelinek } 23501390a385Sgjelinek (void) close(s); 23511390a385Sgjelinek 23521390a385Sgjelinek /* Get the name and address family for each interface. */ 23531390a385Sgjelinek if_reqp = if_conf.lifc_req; 23541390a385Sgjelinek for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) { 23551390a385Sgjelinek struct net_if **p; 23561390a385Sgjelinek struct lifreq req; 23571390a385Sgjelinek 23581390a385Sgjelinek if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) { 23591390a385Sgjelinek if_reqp++; 23601390a385Sgjelinek continue; 23611390a385Sgjelinek } 23621390a385Sgjelinek 23631390a385Sgjelinek if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family), 23641390a385Sgjelinek SOCK_DGRAM, 0)) == -1) { 23651390a385Sgjelinek res = Z_ERR; 23661390a385Sgjelinek break; 23671390a385Sgjelinek } 23681390a385Sgjelinek 23691390a385Sgjelinek (void) strncpy(req.lifr_name, if_reqp->lifr_name, 23701390a385Sgjelinek sizeof (req.lifr_name)); 23711390a385Sgjelinek if (ioctl(s, SIOCGLIFADDR, &req) < 0) { 23721390a385Sgjelinek (void) close(s); 23731390a385Sgjelinek if_reqp++; 23741390a385Sgjelinek continue; 23751390a385Sgjelinek } 23761390a385Sgjelinek 23771390a385Sgjelinek if ((p = (struct net_if **)realloc(local_ifs, 23781390a385Sgjelinek sizeof (struct net_if *) * (cnt + 1))) == NULL) { 23791390a385Sgjelinek res = Z_ERR; 23801390a385Sgjelinek break; 23811390a385Sgjelinek } 23821390a385Sgjelinek local_ifs = p; 23831390a385Sgjelinek 23841390a385Sgjelinek if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) { 23851390a385Sgjelinek res = Z_ERR; 23861390a385Sgjelinek break; 23871390a385Sgjelinek } 23881390a385Sgjelinek 23891390a385Sgjelinek if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name)) 23901390a385Sgjelinek == NULL) { 23911390a385Sgjelinek free(local_ifs[cnt]); 23921390a385Sgjelinek res = Z_ERR; 23931390a385Sgjelinek break; 23941390a385Sgjelinek } 23951390a385Sgjelinek local_ifs[cnt]->af = req.lifr_addr.ss_family; 23961390a385Sgjelinek cnt++; 23971390a385Sgjelinek 23981390a385Sgjelinek (void) close(s); 23991390a385Sgjelinek if_reqp++; 24001390a385Sgjelinek } 24011390a385Sgjelinek 24021390a385Sgjelinek free(if_buf); 24031390a385Sgjelinek 24041390a385Sgjelinek if (res != Z_OK) { 24051390a385Sgjelinek free_local_netifs(cnt, local_ifs); 24061390a385Sgjelinek } else { 24071390a385Sgjelinek *if_cnt = cnt; 24081390a385Sgjelinek *if_list = local_ifs; 24091390a385Sgjelinek } 24101390a385Sgjelinek 24111390a385Sgjelinek return (res); 24121390a385Sgjelinek } 24131390a385Sgjelinek 24141390a385Sgjelinek static char * 24151390a385Sgjelinek af2str(int af) 24161390a385Sgjelinek { 24171390a385Sgjelinek switch (af) { 24181390a385Sgjelinek case AF_INET: 24191390a385Sgjelinek return ("IPv4"); 24201390a385Sgjelinek case AF_INET6: 24211390a385Sgjelinek return ("IPv6"); 24221390a385Sgjelinek default: 24231390a385Sgjelinek return ("Unknown"); 24241390a385Sgjelinek } 24251390a385Sgjelinek } 24261390a385Sgjelinek 24271390a385Sgjelinek /* 24281390a385Sgjelinek * Cross check the network interface name and address family with the 24291390a385Sgjelinek * interfaces that are set up in the global zone so that we can print the 24301390a385Sgjelinek * appropriate error message. 24311390a385Sgjelinek */ 24321390a385Sgjelinek static void 24331390a385Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg) 24341390a385Sgjelinek { 24351390a385Sgjelinek int i; 24361390a385Sgjelinek int local_if_cnt = 0; 24371390a385Sgjelinek struct net_if **local_ifs = NULL; 24381390a385Sgjelinek boolean_t found_if = B_FALSE; 24391390a385Sgjelinek boolean_t found_af = B_FALSE; 24401390a385Sgjelinek 24411390a385Sgjelinek if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) { 24421390a385Sgjelinek (void) fprintf(stderr, 24431390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 24441390a385Sgjelinek "net", "address", addr, "physical", phys, msg); 24451390a385Sgjelinek return; 24461390a385Sgjelinek } 24471390a385Sgjelinek 24481390a385Sgjelinek for (i = 0; i < local_if_cnt; i++) { 24491390a385Sgjelinek if (strcmp(phys, local_ifs[i]->name) == 0) { 24501390a385Sgjelinek found_if = B_TRUE; 24511390a385Sgjelinek if (af == local_ifs[i]->af) { 24521390a385Sgjelinek found_af = B_TRUE; 24531390a385Sgjelinek break; 24541390a385Sgjelinek } 24551390a385Sgjelinek } 24561390a385Sgjelinek } 24571390a385Sgjelinek 24581390a385Sgjelinek free_local_netifs(local_if_cnt, local_ifs); 24591390a385Sgjelinek 24601390a385Sgjelinek if (!found_if) { 24611390a385Sgjelinek (void) fprintf(stderr, 24621390a385Sgjelinek gettext("could not verify %s %s=%s\n\t" 24631390a385Sgjelinek "network interface %s is not plumbed in the global zone\n"), 24641390a385Sgjelinek "net", "physical", phys, phys); 24651390a385Sgjelinek return; 24661390a385Sgjelinek } 24671390a385Sgjelinek 24681390a385Sgjelinek /* 24691390a385Sgjelinek * Print this error if we were unable to find the address family 24701390a385Sgjelinek * for this interface. If the af variable is not initialized to 24711390a385Sgjelinek * to something meaningful by the caller (not AF_UNSPEC) then we 24721390a385Sgjelinek * also skip this message since it wouldn't be informative. 24731390a385Sgjelinek */ 24741390a385Sgjelinek if (!found_af && af != AF_UNSPEC) { 24751390a385Sgjelinek (void) fprintf(stderr, 24761390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\tthe %s address " 2477f4b3ec61Sdh155122 "family is not configured on this network interface in " 2478f4b3ec61Sdh155122 "the\n\tglobal zone\n"), 24791390a385Sgjelinek "net", "address", addr, "physical", phys, af2str(af)); 24801390a385Sgjelinek return; 24811390a385Sgjelinek } 24821390a385Sgjelinek 24831390a385Sgjelinek (void) fprintf(stderr, 24841390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 24851390a385Sgjelinek "net", "address", addr, "physical", phys, msg); 24861390a385Sgjelinek } 24871390a385Sgjelinek 2488ffbafc53Scomay static int 2489ce28b40eSzt129084 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[]) 24907c478bd9Sstevel@tonic-gate { 24917c478bd9Sstevel@tonic-gate struct zone_nwiftab nwiftab; 24927c478bd9Sstevel@tonic-gate int return_code = Z_OK; 24937c478bd9Sstevel@tonic-gate int err; 2494108322fbScarlsonj boolean_t in_alt_root; 2495f4b3ec61Sdh155122 zone_iptype_t iptype; 24962b24ab6bSSebastien Roy dladm_handle_t dh; 24972b24ab6bSSebastien Roy dladm_status_t status; 24982b24ab6bSSebastien Roy datalink_id_t linkid; 24992b24ab6bSSebastien Roy char errmsg[DLADM_STRSIZE]; 25007c478bd9Sstevel@tonic-gate 2501108322fbScarlsonj in_alt_root = zonecfg_in_alt_root(); 2502108322fbScarlsonj if (in_alt_root) 2503108322fbScarlsonj goto no_net; 2504108322fbScarlsonj 2505f4b3ec61Sdh155122 if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) { 2506f4b3ec61Sdh155122 errno = err; 2507f4b3ec61Sdh155122 zperror(cmd_to_str(cmd_num), B_TRUE); 2508f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 2509f4b3ec61Sdh155122 return (Z_ERR); 2510f4b3ec61Sdh155122 } 25117c478bd9Sstevel@tonic-gate if ((err = zonecfg_setnwifent(handle)) != Z_OK) { 25127c478bd9Sstevel@tonic-gate errno = err; 25137c478bd9Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 25147c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 25157c478bd9Sstevel@tonic-gate return (Z_ERR); 25167c478bd9Sstevel@tonic-gate } 25177c478bd9Sstevel@tonic-gate while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) { 25187c478bd9Sstevel@tonic-gate struct lifreq lifr; 25191390a385Sgjelinek sa_family_t af = AF_UNSPEC; 2520f4b3ec61Sdh155122 char dl_owner_zname[ZONENAME_MAX]; 2521f4b3ec61Sdh155122 zoneid_t dl_owner_zid; 2522f4b3ec61Sdh155122 zoneid_t target_zid; 2523f4b3ec61Sdh155122 int res; 25247c478bd9Sstevel@tonic-gate 25257c478bd9Sstevel@tonic-gate /* skip any loopback interfaces */ 25267c478bd9Sstevel@tonic-gate if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0) 25277c478bd9Sstevel@tonic-gate continue; 2528f4b3ec61Sdh155122 switch (iptype) { 2529f4b3ec61Sdh155122 case ZS_SHARED: 2530f4b3ec61Sdh155122 if ((res = zonecfg_valid_net_address( 2531f4b3ec61Sdh155122 nwiftab.zone_nwif_address, &lifr)) != Z_OK) { 25321390a385Sgjelinek print_net_err(nwiftab.zone_nwif_physical, 25331390a385Sgjelinek nwiftab.zone_nwif_address, af, 25341390a385Sgjelinek zonecfg_strerror(res)); 25357c478bd9Sstevel@tonic-gate return_code = Z_ERR; 25367c478bd9Sstevel@tonic-gate continue; 25377c478bd9Sstevel@tonic-gate } 25387c478bd9Sstevel@tonic-gate af = lifr.lifr_addr.ss_family; 2539f4b3ec61Sdh155122 if (!zonecfg_ifname_exists(af, 2540f4b3ec61Sdh155122 nwiftab.zone_nwif_physical)) { 254122321485Svp157776 /* 2542f4b3ec61Sdh155122 * The interface failed to come up. We continue 2543f4b3ec61Sdh155122 * on anyway for the sake of consistency: a 2544f4b3ec61Sdh155122 * zone is not shut down if the interface fails 2545f4b3ec61Sdh155122 * any time after boot, nor does the global zone 2546f4b3ec61Sdh155122 * fail to boot if an interface fails. 254722321485Svp157776 */ 254822321485Svp157776 (void) fprintf(stderr, 2549f4b3ec61Sdh155122 gettext("WARNING: skipping network " 2550f4b3ec61Sdh155122 "interface '%s' which may not be " 2551f4b3ec61Sdh155122 "present/plumbed in the global " 2552f4b3ec61Sdh155122 "zone.\n"), 255322321485Svp157776 nwiftab.zone_nwif_physical); 25547c478bd9Sstevel@tonic-gate } 2555f4b3ec61Sdh155122 break; 2556f4b3ec61Sdh155122 case ZS_EXCLUSIVE: 2557f4b3ec61Sdh155122 /* Warning if it exists for either IPv4 or IPv6 */ 2558f4b3ec61Sdh155122 2559f4b3ec61Sdh155122 if (zonecfg_ifname_exists(AF_INET, 2560f4b3ec61Sdh155122 nwiftab.zone_nwif_physical) || 2561f4b3ec61Sdh155122 zonecfg_ifname_exists(AF_INET6, 2562f4b3ec61Sdh155122 nwiftab.zone_nwif_physical)) { 2563f4b3ec61Sdh155122 (void) fprintf(stderr, 2564f4b3ec61Sdh155122 gettext("WARNING: skipping network " 2565f4b3ec61Sdh155122 "interface '%s' which is used in the " 2566f4b3ec61Sdh155122 "global zone.\n"), 2567f4b3ec61Sdh155122 nwiftab.zone_nwif_physical); 2568f4b3ec61Sdh155122 break; 2569f4b3ec61Sdh155122 } 2570948f2876Sss150715 2571f4b3ec61Sdh155122 /* 25722b24ab6bSSebastien Roy * Verify that the datalink exists and that it isn't 25732b24ab6bSSebastien Roy * already assigned to a zone. 2574f4b3ec61Sdh155122 */ 25752b24ab6bSSebastien Roy if ((status = dladm_open(&dh)) == DLADM_STATUS_OK) { 25762b24ab6bSSebastien Roy status = dladm_name2info(dh, 25772b24ab6bSSebastien Roy nwiftab.zone_nwif_physical, &linkid, NULL, 25782b24ab6bSSebastien Roy NULL, NULL); 25792b24ab6bSSebastien Roy dladm_close(dh); 25802b24ab6bSSebastien Roy } 25812b24ab6bSSebastien Roy if (status != DLADM_STATUS_OK) { 2582f4b3ec61Sdh155122 (void) fprintf(stderr, 2583f4b3ec61Sdh155122 gettext("WARNING: skipping network " 25842b24ab6bSSebastien Roy "interface '%s': %s\n"), 2585948f2876Sss150715 nwiftab.zone_nwif_physical, 25862b24ab6bSSebastien Roy dladm_status2str(status, errmsg)); 2587f4b3ec61Sdh155122 break; 2588f4b3ec61Sdh155122 } 2589f4b3ec61Sdh155122 dl_owner_zid = ALL_ZONES; 25902b24ab6bSSebastien Roy if (zone_check_datalink(&dl_owner_zid, linkid) != 0) 2591f4b3ec61Sdh155122 break; 2592f4b3ec61Sdh155122 2593f4b3ec61Sdh155122 /* 2594f4b3ec61Sdh155122 * If the zone being verified is 2595f4b3ec61Sdh155122 * running and owns the interface 2596f4b3ec61Sdh155122 */ 2597f4b3ec61Sdh155122 target_zid = getzoneidbyname(target_zone); 2598f4b3ec61Sdh155122 if (target_zid == dl_owner_zid) 2599f4b3ec61Sdh155122 break; 2600f4b3ec61Sdh155122 2601f4b3ec61Sdh155122 /* Zone id match failed, use name to check */ 2602f4b3ec61Sdh155122 if (getzonenamebyid(dl_owner_zid, dl_owner_zname, 2603f4b3ec61Sdh155122 ZONENAME_MAX) < 0) { 2604f4b3ec61Sdh155122 /* No name, show ID instead */ 2605f4b3ec61Sdh155122 (void) snprintf(dl_owner_zname, ZONENAME_MAX, 2606f4b3ec61Sdh155122 "<%d>", dl_owner_zid); 2607f4b3ec61Sdh155122 } else if (strcmp(dl_owner_zname, target_zone) == 0) 2608f4b3ec61Sdh155122 break; 2609f4b3ec61Sdh155122 2610f4b3ec61Sdh155122 /* 2611f4b3ec61Sdh155122 * Note here we only report a warning that 2612f4b3ec61Sdh155122 * the interface is already in use by another 2613f4b3ec61Sdh155122 * running zone, and the verify process just 2614f4b3ec61Sdh155122 * goes on, if the interface is still in use 2615f4b3ec61Sdh155122 * when this zone really boots up, zoneadmd 2616f4b3ec61Sdh155122 * will find it. If the name of the zone which 2617f4b3ec61Sdh155122 * owns this interface cannot be determined, 2618f4b3ec61Sdh155122 * then it is not possible to determine if there 2619f4b3ec61Sdh155122 * is a conflict so just report it as a warning. 2620f4b3ec61Sdh155122 */ 2621f4b3ec61Sdh155122 (void) fprintf(stderr, 2622f4b3ec61Sdh155122 gettext("WARNING: skipping network interface " 2623f4b3ec61Sdh155122 "'%s' which is used by the non-global zone " 2624f4b3ec61Sdh155122 "'%s'.\n"), nwiftab.zone_nwif_physical, 2625f4b3ec61Sdh155122 dl_owner_zname); 2626f4b3ec61Sdh155122 break; 2627f4b3ec61Sdh155122 } 26287c478bd9Sstevel@tonic-gate } 26297c478bd9Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 2630108322fbScarlsonj no_net: 26317c478bd9Sstevel@tonic-gate 2632e7f3c547Sgjelinek /* verify that lofs has not been excluded from the kernel */ 26338cd327d5Sgjelinek if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH || 26348cd327d5Sgjelinek cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) && 26358cd327d5Sgjelinek modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) { 2636e7f3c547Sgjelinek if (errno == ENXIO) 2637e7f3c547Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2638e7f3c547Sgjelinek "lofs(7FS): possibly excluded in /etc/system\n")); 2639e7f3c547Sgjelinek else 2640e7f3c547Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2641e7f3c547Sgjelinek "lofs(7FS): %s\n"), strerror(errno)); 2642e7f3c547Sgjelinek return_code = Z_ERR; 2643e7f3c547Sgjelinek } 2644e7f3c547Sgjelinek 26457c478bd9Sstevel@tonic-gate if (verify_filesystems(handle) != Z_OK) 26467c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2647b5abaf04Sgjelinek if (verify_ipd(handle) != Z_OK) 2648b5abaf04Sgjelinek return_code = Z_ERR; 2649108322fbScarlsonj if (!in_alt_root && verify_rctls(handle) != Z_OK) 26507c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2651108322fbScarlsonj if (!in_alt_root && verify_pool(handle) != Z_OK) 26527c478bd9Sstevel@tonic-gate return_code = Z_ERR; 2653ce28b40eSzt129084 if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK) 26549acbbeafSnn35248 return_code = Z_ERR; 2655fa9e4066Sahrens if (!in_alt_root && verify_datasets(handle) != Z_OK) 2656fa9e4066Sahrens return_code = Z_ERR; 2657ffbafc53Scomay 2658ffbafc53Scomay /* 2659ffbafc53Scomay * As the "mount" command is used for patching/upgrading of zones 2660ffbafc53Scomay * or other maintenance processes, the zone's privilege set is not 2661ffbafc53Scomay * checked in this case. Instead, the default, safe set of 2662ffbafc53Scomay * privileges will be used when this zone is created in the 2663ffbafc53Scomay * kernel. 2664ffbafc53Scomay */ 2665ffbafc53Scomay if (!in_alt_root && cmd_num != CMD_MOUNT && 2666ffbafc53Scomay verify_limitpriv(handle) != Z_OK) 2667ffbafc53Scomay return_code = Z_ERR; 26688cd327d5Sgjelinek 26698cd327d5Sgjelinek return (return_code); 26708cd327d5Sgjelinek } 26718cd327d5Sgjelinek 26728cd327d5Sgjelinek static int 2673ce28b40eSzt129084 verify_details(int cmd_num, char *argv[]) 26748cd327d5Sgjelinek { 26758cd327d5Sgjelinek zone_dochandle_t handle; 26768cd327d5Sgjelinek char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN]; 26778cd327d5Sgjelinek int return_code = Z_OK; 26788cd327d5Sgjelinek int err; 26798cd327d5Sgjelinek 26808cd327d5Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 26818cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 26828cd327d5Sgjelinek return (Z_ERR); 26838cd327d5Sgjelinek } 26848cd327d5Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 26858cd327d5Sgjelinek errno = err; 26868cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 26878cd327d5Sgjelinek zonecfg_fini_handle(handle); 26888cd327d5Sgjelinek return (Z_ERR); 26898cd327d5Sgjelinek } 26908cd327d5Sgjelinek if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 26918cd327d5Sgjelinek Z_OK) { 26928cd327d5Sgjelinek errno = err; 26938cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 26948cd327d5Sgjelinek zonecfg_fini_handle(handle); 26958cd327d5Sgjelinek return (Z_ERR); 26968cd327d5Sgjelinek } 26978cd327d5Sgjelinek /* 26988cd327d5Sgjelinek * zonecfg_get_zonepath() gets its data from the XML repository. 26998cd327d5Sgjelinek * Verify this against the index file, which is checked first by 27008cd327d5Sgjelinek * zone_get_zonepath(). If they don't match, bail out. 27018cd327d5Sgjelinek */ 27028cd327d5Sgjelinek if ((err = zone_get_zonepath(target_zone, checkpath, 27038cd327d5Sgjelinek sizeof (checkpath))) != Z_OK) { 27048cd327d5Sgjelinek errno = err; 27058cd327d5Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 2706e767a340Sgjelinek zonecfg_fini_handle(handle); 27078cd327d5Sgjelinek return (Z_ERR); 27088cd327d5Sgjelinek } 27098cd327d5Sgjelinek if (strcmp(zonepath, checkpath) != 0) { 27108cd327d5Sgjelinek /* 27118cd327d5Sgjelinek * TRANSLATION_NOTE 27128cd327d5Sgjelinek * XML and zonepath are literals that should not be translated. 27138cd327d5Sgjelinek */ 27148cd327d5Sgjelinek (void) fprintf(stderr, gettext("The XML repository has " 27158cd327d5Sgjelinek "zonepath '%s',\nbut the index file has zonepath '%s'.\n" 27168cd327d5Sgjelinek "These must match, so fix the incorrect entry.\n"), 27178cd327d5Sgjelinek zonepath, checkpath); 2718e767a340Sgjelinek zonecfg_fini_handle(handle); 27198cd327d5Sgjelinek return (Z_ERR); 27208cd327d5Sgjelinek } 2721ca733e25S if (cmd_num != CMD_ATTACH && 2722ca733e25S validate_zonepath(zonepath, cmd_num) != Z_OK) { 27238cd327d5Sgjelinek (void) fprintf(stderr, gettext("could not verify zonepath %s " 27248cd327d5Sgjelinek "because of the above errors.\n"), zonepath); 27258cd327d5Sgjelinek return_code = Z_ERR; 27268cd327d5Sgjelinek } 27278cd327d5Sgjelinek 2728ce28b40eSzt129084 if (verify_handle(cmd_num, handle, argv) != Z_OK) 27298cd327d5Sgjelinek return_code = Z_ERR; 27308cd327d5Sgjelinek 27317c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 27327c478bd9Sstevel@tonic-gate if (return_code == Z_ERR) 27337c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 27347c478bd9Sstevel@tonic-gate gettext("%s: zone %s failed to verify\n"), 27357c478bd9Sstevel@tonic-gate execname, target_zone); 27367c478bd9Sstevel@tonic-gate return (return_code); 27377c478bd9Sstevel@tonic-gate } 27387c478bd9Sstevel@tonic-gate 27397c478bd9Sstevel@tonic-gate static int 27407c478bd9Sstevel@tonic-gate verify_func(int argc, char *argv[]) 27417c478bd9Sstevel@tonic-gate { 27427c478bd9Sstevel@tonic-gate int arg; 27437c478bd9Sstevel@tonic-gate 27447c478bd9Sstevel@tonic-gate optind = 0; 27457c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 27467c478bd9Sstevel@tonic-gate switch (arg) { 27477c478bd9Sstevel@tonic-gate case '?': 27487c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 27497c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 27507c478bd9Sstevel@tonic-gate default: 27517c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 27527c478bd9Sstevel@tonic-gate return (Z_USAGE); 27537c478bd9Sstevel@tonic-gate } 27547c478bd9Sstevel@tonic-gate } 27557c478bd9Sstevel@tonic-gate if (argc > optind) { 27567c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 27577c478bd9Sstevel@tonic-gate return (Z_USAGE); 27587c478bd9Sstevel@tonic-gate } 27599acbbeafSnn35248 if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE) 27609acbbeafSnn35248 != Z_OK) 27617c478bd9Sstevel@tonic-gate return (Z_ERR); 2762ce28b40eSzt129084 return (verify_details(CMD_VERIFY, argv)); 27637c478bd9Sstevel@tonic-gate } 27647c478bd9Sstevel@tonic-gate 27659acbbeafSnn35248 static int 2766ff17c8bfSgjelinek addoptions(char *buf, char *argv[], size_t len) 2767ff17c8bfSgjelinek { 2768ff17c8bfSgjelinek int i = 0; 2769ff17c8bfSgjelinek 2770ff17c8bfSgjelinek if (buf[0] == '\0') 2771ff17c8bfSgjelinek return (Z_OK); 2772ff17c8bfSgjelinek 2773ff17c8bfSgjelinek while (argv[i] != NULL) { 2774ff17c8bfSgjelinek if (strlcat(buf, " ", len) >= len || 2775ff17c8bfSgjelinek strlcat(buf, argv[i++], len) >= len) { 2776ff17c8bfSgjelinek zerror("Command line too long"); 2777ff17c8bfSgjelinek return (Z_ERR); 2778ff17c8bfSgjelinek } 2779ff17c8bfSgjelinek } 2780ff17c8bfSgjelinek 2781ff17c8bfSgjelinek return (Z_OK); 2782ff17c8bfSgjelinek } 2783ff17c8bfSgjelinek 2784ff17c8bfSgjelinek static int 27859acbbeafSnn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize) 27869acbbeafSnn35248 { 27879acbbeafSnn35248 char optstring[4]; 27889acbbeafSnn35248 27899acbbeafSnn35248 if (opt > 0) 27909acbbeafSnn35248 (void) sprintf(optstring, " -%c", opt); 27919acbbeafSnn35248 else 27929acbbeafSnn35248 (void) strcpy(optstring, " "); 27939acbbeafSnn35248 27949acbbeafSnn35248 if ((strlcat(buf, optstring, bufsize) > bufsize)) 27959acbbeafSnn35248 return (Z_ERR); 2796ff17c8bfSgjelinek 27979acbbeafSnn35248 if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize)) 27989acbbeafSnn35248 return (Z_ERR); 2799ff17c8bfSgjelinek 28009acbbeafSnn35248 return (Z_OK); 28019acbbeafSnn35248 } 28027c478bd9Sstevel@tonic-gate 2803ff17c8bfSgjelinek /* ARGSUSED */ 28047c478bd9Sstevel@tonic-gate static int 28057c478bd9Sstevel@tonic-gate install_func(int argc, char *argv[]) 28067c478bd9Sstevel@tonic-gate { 28079acbbeafSnn35248 char cmdbuf[MAXPATHLEN]; 28081100f00dSgjelinek char postcmdbuf[MAXPATHLEN]; 28097c478bd9Sstevel@tonic-gate int lockfd; 28109acbbeafSnn35248 int arg, err, subproc_err; 28117c478bd9Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 2812123807fbSedp brand_handle_t bh = NULL; 28137c478bd9Sstevel@tonic-gate int status; 28140b5de56dSgjelinek boolean_t nodataset = B_FALSE; 28151100f00dSgjelinek boolean_t do_postinstall = B_FALSE; 2816ff17c8bfSgjelinek boolean_t brand_help = B_FALSE; 28179acbbeafSnn35248 char opts[128]; 28189acbbeafSnn35248 28199acbbeafSnn35248 if (target_zone == NULL) { 28209acbbeafSnn35248 sub_usage(SHELP_INSTALL, CMD_INSTALL); 28219acbbeafSnn35248 return (Z_USAGE); 28229acbbeafSnn35248 } 28237c478bd9Sstevel@tonic-gate 2824108322fbScarlsonj if (zonecfg_in_alt_root()) { 2825108322fbScarlsonj zerror(gettext("cannot install zone in alternate root")); 2826108322fbScarlsonj return (Z_ERR); 2827108322fbScarlsonj } 2828108322fbScarlsonj 28299acbbeafSnn35248 if ((err = zone_get_zonepath(target_zone, zonepath, 28309acbbeafSnn35248 sizeof (zonepath))) != Z_OK) { 28319acbbeafSnn35248 errno = err; 28329acbbeafSnn35248 zperror2(target_zone, gettext("could not get zone path")); 28339acbbeafSnn35248 return (Z_ERR); 28349acbbeafSnn35248 } 28359acbbeafSnn35248 28369acbbeafSnn35248 /* Fetch the install command from the brand configuration. */ 2837123807fbSedp if ((bh = brand_open(target_brand)) == NULL) { 28389acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 28399acbbeafSnn35248 return (Z_ERR); 28409acbbeafSnn35248 } 28419acbbeafSnn35248 2842ff17c8bfSgjelinek if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_install, 2843ff17c8bfSgjelinek target_zone, zonepath) != Z_OK) { 28449acbbeafSnn35248 zerror("invalid brand configuration: missing install resource"); 2845123807fbSedp brand_close(bh); 28469acbbeafSnn35248 return (Z_ERR); 28479acbbeafSnn35248 } 28489acbbeafSnn35248 2849ff17c8bfSgjelinek if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postinstall, 2850ff17c8bfSgjelinek target_zone, zonepath) != Z_OK) { 28511100f00dSgjelinek zerror("invalid brand configuration: missing postinstall " 28521100f00dSgjelinek "resource"); 28531100f00dSgjelinek brand_close(bh); 28541100f00dSgjelinek return (Z_ERR); 28551100f00dSgjelinek } 28561100f00dSgjelinek 2857ff17c8bfSgjelinek if (postcmdbuf[0] != '\0') 2858ff17c8bfSgjelinek do_postinstall = B_TRUE; 2859ff17c8bfSgjelinek 28609acbbeafSnn35248 (void) strcpy(opts, "?x:"); 28619acbbeafSnn35248 /* 28629acbbeafSnn35248 * Fetch the list of recognized command-line options from 28639acbbeafSnn35248 * the brand configuration file. 28649acbbeafSnn35248 */ 2865123807fbSedp if (brand_get_installopts(bh, opts + strlen(opts), 28669acbbeafSnn35248 sizeof (opts) - strlen(opts)) != 0) { 28679acbbeafSnn35248 zerror("invalid brand configuration: missing " 28689acbbeafSnn35248 "install options resource"); 2869123807fbSedp brand_close(bh); 28709acbbeafSnn35248 return (Z_ERR); 28719acbbeafSnn35248 } 2872ff17c8bfSgjelinek 2873123807fbSedp brand_close(bh); 28749acbbeafSnn35248 2875ff17c8bfSgjelinek if (cmdbuf[0] == '\0') { 2876ff17c8bfSgjelinek zerror("Missing brand install command"); 2877ff17c8bfSgjelinek return (Z_ERR); 2878ff17c8bfSgjelinek } 2879ff17c8bfSgjelinek 2880ff17c8bfSgjelinek /* Check the argv string for args we handle internally */ 28817c478bd9Sstevel@tonic-gate optind = 0; 2882ff17c8bfSgjelinek opterr = 0; 28839acbbeafSnn35248 while ((arg = getopt(argc, argv, opts)) != EOF) { 28847c478bd9Sstevel@tonic-gate switch (arg) { 28857c478bd9Sstevel@tonic-gate case '?': 2886ff17c8bfSgjelinek if (optopt == '?') { 28877c478bd9Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 2888ff17c8bfSgjelinek brand_help = B_TRUE; 28890b5de56dSgjelinek } 2890ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 28910b5de56dSgjelinek break; 2892ff17c8bfSgjelinek case 'x': 2893ff17c8bfSgjelinek /* Handle this option internally, don't pass to brand */ 2894ff17c8bfSgjelinek if (strcmp(optarg, "nodataset") == 0) { 2895ff17c8bfSgjelinek /* Handle this option internally */ 2896ff17c8bfSgjelinek nodataset = B_TRUE; 2897ff17c8bfSgjelinek } 2898ff17c8bfSgjelinek continue; 28997c478bd9Sstevel@tonic-gate default: 2900ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 2901ff17c8bfSgjelinek break; 29027c478bd9Sstevel@tonic-gate } 29039acbbeafSnn35248 29049acbbeafSnn35248 /* 2905ff17c8bfSgjelinek * Append the option to the command line passed to the 2906ff17c8bfSgjelinek * brand-specific install and postinstall routines. 29079acbbeafSnn35248 */ 2908ff17c8bfSgjelinek if (addopt(cmdbuf, optopt, optarg, sizeof (cmdbuf)) != Z_OK) { 29099acbbeafSnn35248 zerror("Install command line too long"); 29109acbbeafSnn35248 return (Z_ERR); 29117c478bd9Sstevel@tonic-gate } 2912ff17c8bfSgjelinek if (addopt(postcmdbuf, optopt, optarg, sizeof (postcmdbuf)) 2913ff17c8bfSgjelinek != Z_OK) { 29141100f00dSgjelinek zerror("Post-Install command line too long"); 29151100f00dSgjelinek return (Z_ERR); 29161100f00dSgjelinek } 29179acbbeafSnn35248 } 29189acbbeafSnn35248 29199acbbeafSnn35248 for (; optind < argc; optind++) { 2920ff17c8bfSgjelinek if (addopt(cmdbuf, 0, argv[optind], sizeof (cmdbuf)) != Z_OK) { 29219acbbeafSnn35248 zerror("Install command line too long"); 29229acbbeafSnn35248 return (Z_ERR); 29239acbbeafSnn35248 } 2924ff17c8bfSgjelinek 2925ff17c8bfSgjelinek if (addopt(postcmdbuf, 0, argv[optind], sizeof (postcmdbuf)) 2926ff17c8bfSgjelinek != Z_OK) { 29271100f00dSgjelinek zerror("Post-Install command line too long"); 29281100f00dSgjelinek return (Z_ERR); 29291100f00dSgjelinek } 29309acbbeafSnn35248 } 29319acbbeafSnn35248 2932ff17c8bfSgjelinek if (!brand_help) { 2933ff17c8bfSgjelinek if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE, 2934ff17c8bfSgjelinek B_FALSE) != Z_OK) 29357c478bd9Sstevel@tonic-gate return (Z_ERR); 2936ce28b40eSzt129084 if (verify_details(CMD_INSTALL, argv) != Z_OK) 29377c478bd9Sstevel@tonic-gate return (Z_ERR); 29387c478bd9Sstevel@tonic-gate 2939ff17c8bfSgjelinek if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) { 2940ff17c8bfSgjelinek zerror(gettext("another %s may have an operation in " 2941ff17c8bfSgjelinek "progress."), "zoneadm"); 29427c478bd9Sstevel@tonic-gate return (Z_ERR); 29437c478bd9Sstevel@tonic-gate } 29447c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 29457c478bd9Sstevel@tonic-gate if (err != Z_OK) { 29467c478bd9Sstevel@tonic-gate errno = err; 29477c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 29487c478bd9Sstevel@tonic-gate goto done; 29497c478bd9Sstevel@tonic-gate } 29507c478bd9Sstevel@tonic-gate 29519acbbeafSnn35248 if (!nodataset) 29529acbbeafSnn35248 create_zfs_zonepath(zonepath); 29537c478bd9Sstevel@tonic-gate } 29547c478bd9Sstevel@tonic-gate 2955c75cc341S status = do_subproc(cmdbuf); 29569acbbeafSnn35248 if ((subproc_err = 29579acbbeafSnn35248 subproc_status(gettext("brand-specific installation"), status, 29589acbbeafSnn35248 B_FALSE)) != ZONE_SUBPROC_OK) { 2959ff17c8bfSgjelinek if (subproc_err == ZONE_SUBPROC_USAGE && !brand_help) { 2960ff17c8bfSgjelinek sub_usage(SHELP_INSTALL, CMD_INSTALL); 2961ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 2962ff17c8bfSgjelinek return (Z_ERR); 2963ff17c8bfSgjelinek } 29649acbbeafSnn35248 err = Z_ERR; 29657c478bd9Sstevel@tonic-gate goto done; 29669acbbeafSnn35248 } 29677c478bd9Sstevel@tonic-gate 2968ff17c8bfSgjelinek if (brand_help) 2969ff17c8bfSgjelinek return (Z_OK); 2970ff17c8bfSgjelinek 29717c478bd9Sstevel@tonic-gate if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 29727c478bd9Sstevel@tonic-gate errno = err; 29737c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 29747c478bd9Sstevel@tonic-gate goto done; 29757c478bd9Sstevel@tonic-gate } 29767c478bd9Sstevel@tonic-gate 29771100f00dSgjelinek if (do_postinstall) { 29781100f00dSgjelinek status = do_subproc(postcmdbuf); 29791100f00dSgjelinek 29801100f00dSgjelinek if ((subproc_err = 29811100f00dSgjelinek subproc_status(gettext("brand-specific post-install"), 29821100f00dSgjelinek status, B_FALSE)) != ZONE_SUBPROC_OK) { 29831100f00dSgjelinek err = Z_ERR; 29841100f00dSgjelinek (void) zone_set_state(target_zone, 29851100f00dSgjelinek ZONE_STATE_INCOMPLETE); 29861100f00dSgjelinek } 29871100f00dSgjelinek } 29881100f00dSgjelinek 29897c478bd9Sstevel@tonic-gate done: 29909acbbeafSnn35248 /* 2991ff17c8bfSgjelinek * If the install script exited with ZONE_SUBPROC_NOTCOMPLETE, try to 2992ff17c8bfSgjelinek * clean up the zone and leave the zone in the CONFIGURED state so that 2993ff17c8bfSgjelinek * another install can be attempted without requiring an uninstall 2994ff17c8bfSgjelinek * first. 29959acbbeafSnn35248 */ 2996ff17c8bfSgjelinek if (subproc_err == ZONE_SUBPROC_NOTCOMPLETE) { 29979acbbeafSnn35248 if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 29989acbbeafSnn35248 errno = err; 29999acbbeafSnn35248 zperror2(target_zone, 30009acbbeafSnn35248 gettext("cleaning up zonepath failed")); 30019acbbeafSnn35248 } else if ((err = zone_set_state(target_zone, 30029acbbeafSnn35248 ZONE_STATE_CONFIGURED)) != Z_OK) { 30039acbbeafSnn35248 errno = err; 30049acbbeafSnn35248 zperror2(target_zone, gettext("could not set state")); 30059acbbeafSnn35248 } 30069acbbeafSnn35248 } 30079acbbeafSnn35248 3008ff17c8bfSgjelinek if (!brand_help) 3009ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 30107c478bd9Sstevel@tonic-gate return ((err == Z_OK) ? Z_OK : Z_ERR); 30117c478bd9Sstevel@tonic-gate } 30127c478bd9Sstevel@tonic-gate 30137c478bd9Sstevel@tonic-gate /* 3014865e09a4Sgjelinek * Check that the inherited pkg dirs are the same for the clone and its source. 3015865e09a4Sgjelinek * The easiest way to do that is check that the list of ipds is the same 3016865e09a4Sgjelinek * by matching each one against the other. This algorithm should be fine since 3017865e09a4Sgjelinek * the list of ipds should not be that long. 3018865e09a4Sgjelinek */ 3019865e09a4Sgjelinek static int 3020865e09a4Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone, 3021865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3022865e09a4Sgjelinek { 3023865e09a4Sgjelinek int err; 3024865e09a4Sgjelinek int res = Z_OK; 3025865e09a4Sgjelinek int s_cnt = 0; 3026865e09a4Sgjelinek int t_cnt = 0; 3027865e09a4Sgjelinek struct zone_fstab s_fstab; 3028865e09a4Sgjelinek struct zone_fstab t_fstab; 3029865e09a4Sgjelinek 3030865e09a4Sgjelinek /* 3031865e09a4Sgjelinek * First check the source of the clone against the target. 3032865e09a4Sgjelinek */ 3033865e09a4Sgjelinek if ((err = zonecfg_setipdent(s_handle)) != Z_OK) { 3034865e09a4Sgjelinek errno = err; 3035865e09a4Sgjelinek zperror2(source_zone, gettext("could not enumerate " 3036865e09a4Sgjelinek "inherit-pkg-dirs")); 3037865e09a4Sgjelinek return (Z_ERR); 3038865e09a4Sgjelinek } 3039865e09a4Sgjelinek 3040865e09a4Sgjelinek while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) { 3041865e09a4Sgjelinek boolean_t match = B_FALSE; 3042865e09a4Sgjelinek 3043865e09a4Sgjelinek s_cnt++; 3044865e09a4Sgjelinek 3045865e09a4Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 3046865e09a4Sgjelinek errno = err; 3047865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate " 3048865e09a4Sgjelinek "inherit-pkg-dirs")); 3049865e09a4Sgjelinek (void) zonecfg_endipdent(s_handle); 3050865e09a4Sgjelinek return (Z_ERR); 3051865e09a4Sgjelinek } 3052865e09a4Sgjelinek 3053865e09a4Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) { 3054865e09a4Sgjelinek if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir) 3055865e09a4Sgjelinek == 0) { 3056865e09a4Sgjelinek match = B_TRUE; 3057865e09a4Sgjelinek break; 3058865e09a4Sgjelinek } 3059865e09a4Sgjelinek } 3060865e09a4Sgjelinek (void) zonecfg_endipdent(t_handle); 3061865e09a4Sgjelinek 3062865e09a4Sgjelinek if (!match) { 3063865e09a4Sgjelinek (void) fprintf(stderr, gettext("inherit-pkg-dir " 3064865e09a4Sgjelinek "'%s' is not configured in zone %s.\n"), 3065865e09a4Sgjelinek s_fstab.zone_fs_dir, target_zone); 3066865e09a4Sgjelinek res = Z_ERR; 3067865e09a4Sgjelinek } 3068865e09a4Sgjelinek } 3069865e09a4Sgjelinek 3070865e09a4Sgjelinek (void) zonecfg_endipdent(s_handle); 3071865e09a4Sgjelinek 3072865e09a4Sgjelinek /* skip the next check if we already have errors */ 3073865e09a4Sgjelinek if (res == Z_ERR) 3074865e09a4Sgjelinek return (res); 3075865e09a4Sgjelinek 3076865e09a4Sgjelinek /* 3077865e09a4Sgjelinek * Now check the number of ipds in the target so we can verify 3078865e09a4Sgjelinek * that the source is not a subset of the target. 3079865e09a4Sgjelinek */ 3080865e09a4Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 3081865e09a4Sgjelinek errno = err; 3082865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate " 3083865e09a4Sgjelinek "inherit-pkg-dirs")); 3084865e09a4Sgjelinek return (Z_ERR); 3085865e09a4Sgjelinek } 3086865e09a4Sgjelinek 3087865e09a4Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) 3088865e09a4Sgjelinek t_cnt++; 3089865e09a4Sgjelinek 3090865e09a4Sgjelinek (void) zonecfg_endipdent(t_handle); 3091865e09a4Sgjelinek 3092865e09a4Sgjelinek if (t_cnt != s_cnt) { 3093865e09a4Sgjelinek (void) fprintf(stderr, gettext("Zone %s is configured " 3094865e09a4Sgjelinek "with inherit-pkg-dirs that are not configured in zone " 3095865e09a4Sgjelinek "%s.\n"), target_zone, source_zone); 3096865e09a4Sgjelinek res = Z_ERR; 3097865e09a4Sgjelinek } 3098865e09a4Sgjelinek 3099865e09a4Sgjelinek return (res); 3100865e09a4Sgjelinek } 3101865e09a4Sgjelinek 3102865e09a4Sgjelinek static void 3103865e09a4Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone, 3104865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3105865e09a4Sgjelinek { 3106865e09a4Sgjelinek int err; 3107865e09a4Sgjelinek struct zone_devtab s_devtab; 3108865e09a4Sgjelinek struct zone_devtab t_devtab; 3109865e09a4Sgjelinek 3110865e09a4Sgjelinek if ((err = zonecfg_setdevent(t_handle)) != Z_OK) { 3111865e09a4Sgjelinek errno = err; 3112865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate devices")); 3113865e09a4Sgjelinek return; 3114865e09a4Sgjelinek } 3115865e09a4Sgjelinek 3116865e09a4Sgjelinek while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) { 3117865e09a4Sgjelinek if ((err = zonecfg_setdevent(s_handle)) != Z_OK) { 3118865e09a4Sgjelinek errno = err; 3119865e09a4Sgjelinek zperror2(source_zone, 3120865e09a4Sgjelinek gettext("could not enumerate devices")); 3121865e09a4Sgjelinek (void) zonecfg_enddevent(t_handle); 3122865e09a4Sgjelinek return; 3123865e09a4Sgjelinek } 3124865e09a4Sgjelinek 3125865e09a4Sgjelinek while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) { 3126865e09a4Sgjelinek /* 3127865e09a4Sgjelinek * Use fnmatch to catch the case where wildcards 3128865e09a4Sgjelinek * were used in one zone and the other has an 3129865e09a4Sgjelinek * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs. 3130865e09a4Sgjelinek * /dev/\*dsk/c0t0d0s6). 3131865e09a4Sgjelinek */ 3132865e09a4Sgjelinek if (fnmatch(t_devtab.zone_dev_match, 3133865e09a4Sgjelinek s_devtab.zone_dev_match, FNM_PATHNAME) == 0 || 3134865e09a4Sgjelinek fnmatch(s_devtab.zone_dev_match, 3135865e09a4Sgjelinek t_devtab.zone_dev_match, FNM_PATHNAME) == 0) { 3136865e09a4Sgjelinek (void) fprintf(stderr, 3137865e09a4Sgjelinek gettext("WARNING: device '%s' " 3138865e09a4Sgjelinek "is configured in both zones.\n"), 3139865e09a4Sgjelinek t_devtab.zone_dev_match); 3140865e09a4Sgjelinek break; 3141865e09a4Sgjelinek } 3142865e09a4Sgjelinek } 3143865e09a4Sgjelinek (void) zonecfg_enddevent(s_handle); 3144865e09a4Sgjelinek } 3145865e09a4Sgjelinek 3146865e09a4Sgjelinek (void) zonecfg_enddevent(t_handle); 3147865e09a4Sgjelinek } 3148865e09a4Sgjelinek 3149865e09a4Sgjelinek /* 3150865e09a4Sgjelinek * Check if the specified mount option (opt) is contained within the 3151865e09a4Sgjelinek * options string. 3152865e09a4Sgjelinek */ 3153865e09a4Sgjelinek static boolean_t 3154865e09a4Sgjelinek opt_match(char *opt, char *options) 3155865e09a4Sgjelinek { 3156865e09a4Sgjelinek char *p; 3157865e09a4Sgjelinek char *lastp; 3158865e09a4Sgjelinek 3159865e09a4Sgjelinek if ((p = strtok_r(options, ",", &lastp)) != NULL) { 3160865e09a4Sgjelinek if (strcmp(p, opt) == 0) 3161865e09a4Sgjelinek return (B_TRUE); 3162865e09a4Sgjelinek while ((p = strtok_r(NULL, ",", &lastp)) != NULL) { 3163865e09a4Sgjelinek if (strcmp(p, opt) == 0) 3164865e09a4Sgjelinek return (B_TRUE); 3165865e09a4Sgjelinek } 3166865e09a4Sgjelinek } 3167865e09a4Sgjelinek 3168865e09a4Sgjelinek return (B_FALSE); 3169865e09a4Sgjelinek } 3170865e09a4Sgjelinek 31710b5de56dSgjelinek #define RW_LOFS "WARNING: read-write lofs file system on '%s' is configured " \ 3172865e09a4Sgjelinek "in both zones.\n" 3173865e09a4Sgjelinek 3174865e09a4Sgjelinek static void 3175865e09a4Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab) 3176865e09a4Sgjelinek { 3177865e09a4Sgjelinek /* 3178865e09a4Sgjelinek * It is ok to have shared lofs mounted fs but we want to warn if 3179865e09a4Sgjelinek * either is rw since this will effect the other zone. 3180865e09a4Sgjelinek */ 3181865e09a4Sgjelinek if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) { 3182865e09a4Sgjelinek zone_fsopt_t *optp; 3183865e09a4Sgjelinek 3184865e09a4Sgjelinek /* The default is rw so no options means rw */ 3185865e09a4Sgjelinek if (t_fstab->zone_fs_options == NULL || 3186865e09a4Sgjelinek s_fstab->zone_fs_options == NULL) { 3187865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 3188865e09a4Sgjelinek t_fstab->zone_fs_special); 3189865e09a4Sgjelinek return; 3190865e09a4Sgjelinek } 3191865e09a4Sgjelinek 3192865e09a4Sgjelinek for (optp = s_fstab->zone_fs_options; optp != NULL; 3193865e09a4Sgjelinek optp = optp->zone_fsopt_next) { 3194865e09a4Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 3195865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 3196865e09a4Sgjelinek s_fstab->zone_fs_special); 3197865e09a4Sgjelinek return; 3198865e09a4Sgjelinek } 3199865e09a4Sgjelinek } 3200865e09a4Sgjelinek 3201865e09a4Sgjelinek for (optp = t_fstab->zone_fs_options; optp != NULL; 3202865e09a4Sgjelinek optp = optp->zone_fsopt_next) { 3203865e09a4Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 3204865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 3205865e09a4Sgjelinek t_fstab->zone_fs_special); 3206865e09a4Sgjelinek return; 3207865e09a4Sgjelinek } 3208865e09a4Sgjelinek } 3209865e09a4Sgjelinek 3210865e09a4Sgjelinek return; 3211865e09a4Sgjelinek } 3212865e09a4Sgjelinek 3213865e09a4Sgjelinek /* 3214865e09a4Sgjelinek * TRANSLATION_NOTE 32150b5de56dSgjelinek * The first variable is the file system type and the second is 32160b5de56dSgjelinek * the file system special device. For example, 32170b5de56dSgjelinek * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ... 3218865e09a4Sgjelinek */ 32190b5de56dSgjelinek (void) fprintf(stderr, gettext("WARNING: %s file system on '%s' " 3220865e09a4Sgjelinek "is configured in both zones.\n"), t_fstab->zone_fs_type, 3221865e09a4Sgjelinek t_fstab->zone_fs_special); 3222865e09a4Sgjelinek } 3223865e09a4Sgjelinek 3224865e09a4Sgjelinek static void 3225865e09a4Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone, 3226865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3227865e09a4Sgjelinek { 3228865e09a4Sgjelinek int err; 3229865e09a4Sgjelinek struct zone_fstab s_fstab; 3230865e09a4Sgjelinek struct zone_fstab t_fstab; 3231865e09a4Sgjelinek 3232865e09a4Sgjelinek if ((err = zonecfg_setfsent(t_handle)) != Z_OK) { 3233865e09a4Sgjelinek errno = err; 3234865e09a4Sgjelinek zperror2(target_zone, 32350b5de56dSgjelinek gettext("could not enumerate file systems")); 3236865e09a4Sgjelinek return; 3237865e09a4Sgjelinek } 3238865e09a4Sgjelinek 3239865e09a4Sgjelinek while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) { 3240865e09a4Sgjelinek if ((err = zonecfg_setfsent(s_handle)) != Z_OK) { 3241865e09a4Sgjelinek errno = err; 3242865e09a4Sgjelinek zperror2(source_zone, 32430b5de56dSgjelinek gettext("could not enumerate file systems")); 3244865e09a4Sgjelinek (void) zonecfg_endfsent(t_handle); 3245865e09a4Sgjelinek return; 3246865e09a4Sgjelinek } 3247865e09a4Sgjelinek 3248865e09a4Sgjelinek while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) { 3249865e09a4Sgjelinek if (strcmp(t_fstab.zone_fs_special, 3250865e09a4Sgjelinek s_fstab.zone_fs_special) == 0) { 3251865e09a4Sgjelinek print_fs_warnings(&s_fstab, &t_fstab); 3252865e09a4Sgjelinek break; 3253865e09a4Sgjelinek } 3254865e09a4Sgjelinek } 3255865e09a4Sgjelinek (void) zonecfg_endfsent(s_handle); 3256865e09a4Sgjelinek } 3257865e09a4Sgjelinek 3258865e09a4Sgjelinek (void) zonecfg_endfsent(t_handle); 3259865e09a4Sgjelinek } 3260865e09a4Sgjelinek 3261865e09a4Sgjelinek /* 3262865e09a4Sgjelinek * We don't catch the case where you used the same IP address but 3263865e09a4Sgjelinek * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128. 3264865e09a4Sgjelinek * However, we're not going to worry about that but we will check for 3265865e09a4Sgjelinek * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24) 3266865e09a4Sgjelinek * and handle that case as a match. 3267865e09a4Sgjelinek */ 3268865e09a4Sgjelinek static void 3269865e09a4Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone, 3270865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3271865e09a4Sgjelinek { 3272865e09a4Sgjelinek int err; 3273865e09a4Sgjelinek struct zone_nwiftab s_nwiftab; 3274865e09a4Sgjelinek struct zone_nwiftab t_nwiftab; 3275865e09a4Sgjelinek 3276865e09a4Sgjelinek if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) { 3277865e09a4Sgjelinek errno = err; 3278865e09a4Sgjelinek zperror2(target_zone, 3279865e09a4Sgjelinek gettext("could not enumerate network interfaces")); 3280865e09a4Sgjelinek return; 3281865e09a4Sgjelinek } 3282865e09a4Sgjelinek 3283865e09a4Sgjelinek while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) { 3284865e09a4Sgjelinek char *p; 3285865e09a4Sgjelinek 3286865e09a4Sgjelinek /* remove an (optional) netmask from the address */ 3287865e09a4Sgjelinek if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL) 3288865e09a4Sgjelinek *p = '\0'; 3289865e09a4Sgjelinek 3290865e09a4Sgjelinek if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) { 3291865e09a4Sgjelinek errno = err; 3292865e09a4Sgjelinek zperror2(source_zone, 3293865e09a4Sgjelinek gettext("could not enumerate network interfaces")); 3294865e09a4Sgjelinek (void) zonecfg_endnwifent(t_handle); 3295865e09a4Sgjelinek return; 3296865e09a4Sgjelinek } 3297865e09a4Sgjelinek 3298865e09a4Sgjelinek while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) { 3299865e09a4Sgjelinek /* remove an (optional) netmask from the address */ 3300865e09a4Sgjelinek if ((p = strchr(s_nwiftab.zone_nwif_address, '/')) 3301865e09a4Sgjelinek != NULL) 3302865e09a4Sgjelinek *p = '\0'; 3303865e09a4Sgjelinek 3304f4b3ec61Sdh155122 /* For exclusive-IP zones, address is not specified. */ 3305f4b3ec61Sdh155122 if (strlen(s_nwiftab.zone_nwif_address) == 0) 3306f4b3ec61Sdh155122 continue; 3307f4b3ec61Sdh155122 3308865e09a4Sgjelinek if (strcmp(t_nwiftab.zone_nwif_address, 3309865e09a4Sgjelinek s_nwiftab.zone_nwif_address) == 0) { 3310865e09a4Sgjelinek (void) fprintf(stderr, 3311865e09a4Sgjelinek gettext("WARNING: network address '%s' " 3312865e09a4Sgjelinek "is configured in both zones.\n"), 3313865e09a4Sgjelinek t_nwiftab.zone_nwif_address); 3314865e09a4Sgjelinek break; 3315865e09a4Sgjelinek } 3316865e09a4Sgjelinek } 3317865e09a4Sgjelinek (void) zonecfg_endnwifent(s_handle); 3318865e09a4Sgjelinek } 3319865e09a4Sgjelinek 3320865e09a4Sgjelinek (void) zonecfg_endnwifent(t_handle); 3321865e09a4Sgjelinek } 3322865e09a4Sgjelinek 3323865e09a4Sgjelinek static void 33249acbbeafSnn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source, 33259acbbeafSnn35248 zone_dochandle_t t_handle, char *target) 3326865e09a4Sgjelinek { 3327865e09a4Sgjelinek int err; 3328865e09a4Sgjelinek struct zone_dstab s_dstab; 3329865e09a4Sgjelinek struct zone_dstab t_dstab; 3330865e09a4Sgjelinek 3331865e09a4Sgjelinek if ((err = zonecfg_setdsent(t_handle)) != Z_OK) { 3332865e09a4Sgjelinek errno = err; 33339acbbeafSnn35248 zperror2(target, gettext("could not enumerate datasets")); 3334865e09a4Sgjelinek return; 3335865e09a4Sgjelinek } 3336865e09a4Sgjelinek 3337865e09a4Sgjelinek while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) { 3338865e09a4Sgjelinek if ((err = zonecfg_setdsent(s_handle)) != Z_OK) { 3339865e09a4Sgjelinek errno = err; 33409acbbeafSnn35248 zperror2(source, 3341865e09a4Sgjelinek gettext("could not enumerate datasets")); 3342865e09a4Sgjelinek (void) zonecfg_enddsent(t_handle); 3343865e09a4Sgjelinek return; 3344865e09a4Sgjelinek } 3345865e09a4Sgjelinek 3346865e09a4Sgjelinek while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) { 3347865e09a4Sgjelinek if (strcmp(t_dstab.zone_dataset_name, 3348865e09a4Sgjelinek s_dstab.zone_dataset_name) == 0) { 33499acbbeafSnn35248 target_zone = source; 33509acbbeafSnn35248 zerror(gettext("WARNING: dataset '%s' " 3351865e09a4Sgjelinek "is configured in both zones.\n"), 3352865e09a4Sgjelinek t_dstab.zone_dataset_name); 3353865e09a4Sgjelinek break; 3354865e09a4Sgjelinek } 3355865e09a4Sgjelinek } 3356865e09a4Sgjelinek (void) zonecfg_enddsent(s_handle); 3357865e09a4Sgjelinek } 3358865e09a4Sgjelinek 3359865e09a4Sgjelinek (void) zonecfg_enddsent(t_handle); 3360865e09a4Sgjelinek } 3361865e09a4Sgjelinek 33629acbbeafSnn35248 /* 33639acbbeafSnn35248 * Check that the clone and its source have the same brand type. 33649acbbeafSnn35248 */ 33659acbbeafSnn35248 static int 33669acbbeafSnn35248 valid_brand_clone(char *source_zone, char *target_zone) 33679acbbeafSnn35248 { 3368123807fbSedp brand_handle_t bh; 33699acbbeafSnn35248 char source_brand[MAXNAMELEN]; 33709acbbeafSnn35248 33719acbbeafSnn35248 if ((zone_get_brand(source_zone, source_brand, 33729acbbeafSnn35248 sizeof (source_brand))) != Z_OK) { 33739acbbeafSnn35248 (void) fprintf(stderr, "%s: zone '%s': %s\n", 33749acbbeafSnn35248 execname, source_zone, gettext("missing or invalid brand")); 33759acbbeafSnn35248 return (Z_ERR); 33769acbbeafSnn35248 } 33779acbbeafSnn35248 33789acbbeafSnn35248 if (strcmp(source_brand, target_brand) != NULL) { 33799acbbeafSnn35248 (void) fprintf(stderr, 33809acbbeafSnn35248 gettext("%s: Zones '%s' and '%s' have different brand " 33819acbbeafSnn35248 "types.\n"), execname, source_zone, target_zone); 33829acbbeafSnn35248 return (Z_ERR); 33839acbbeafSnn35248 } 33849acbbeafSnn35248 3385123807fbSedp if ((bh = brand_open(target_brand)) == NULL) { 33869acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 33879acbbeafSnn35248 return (Z_ERR); 33889acbbeafSnn35248 } 3389123807fbSedp brand_close(bh); 33909acbbeafSnn35248 return (Z_OK); 33919acbbeafSnn35248 } 33929acbbeafSnn35248 3393865e09a4Sgjelinek static int 3394865e09a4Sgjelinek validate_clone(char *source_zone, char *target_zone) 3395865e09a4Sgjelinek { 3396865e09a4Sgjelinek int err = Z_OK; 3397865e09a4Sgjelinek zone_dochandle_t s_handle; 3398865e09a4Sgjelinek zone_dochandle_t t_handle; 3399865e09a4Sgjelinek 3400865e09a4Sgjelinek if ((t_handle = zonecfg_init_handle()) == NULL) { 3401865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3402865e09a4Sgjelinek return (Z_ERR); 3403865e09a4Sgjelinek } 3404865e09a4Sgjelinek if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) { 3405865e09a4Sgjelinek errno = err; 3406865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3407865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3408865e09a4Sgjelinek return (Z_ERR); 3409865e09a4Sgjelinek } 3410865e09a4Sgjelinek 3411865e09a4Sgjelinek if ((s_handle = zonecfg_init_handle()) == NULL) { 3412865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3413865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3414865e09a4Sgjelinek return (Z_ERR); 3415865e09a4Sgjelinek } 3416865e09a4Sgjelinek if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) { 3417865e09a4Sgjelinek errno = err; 3418865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3419865e09a4Sgjelinek goto done; 3420865e09a4Sgjelinek } 3421865e09a4Sgjelinek 34229acbbeafSnn35248 /* verify new zone has same brand type */ 34239acbbeafSnn35248 err = valid_brand_clone(source_zone, target_zone); 34249acbbeafSnn35248 if (err != Z_OK) 34259acbbeafSnn35248 goto done; 34269acbbeafSnn35248 3427865e09a4Sgjelinek /* verify new zone has same inherit-pkg-dirs */ 3428865e09a4Sgjelinek err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone); 3429865e09a4Sgjelinek 3430865e09a4Sgjelinek /* warn about imported fs's which are the same */ 3431865e09a4Sgjelinek warn_fs_match(s_handle, source_zone, t_handle, target_zone); 3432865e09a4Sgjelinek 3433865e09a4Sgjelinek /* warn about imported IP addresses which are the same */ 3434865e09a4Sgjelinek warn_ip_match(s_handle, source_zone, t_handle, target_zone); 3435865e09a4Sgjelinek 3436865e09a4Sgjelinek /* warn about imported devices which are the same */ 3437865e09a4Sgjelinek warn_dev_match(s_handle, source_zone, t_handle, target_zone); 3438865e09a4Sgjelinek 3439865e09a4Sgjelinek /* warn about imported datasets which are the same */ 3440865e09a4Sgjelinek warn_dataset_match(s_handle, source_zone, t_handle, target_zone); 3441865e09a4Sgjelinek 3442865e09a4Sgjelinek done: 3443865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3444865e09a4Sgjelinek zonecfg_fini_handle(s_handle); 3445865e09a4Sgjelinek 3446865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3447865e09a4Sgjelinek } 3448865e09a4Sgjelinek 3449865e09a4Sgjelinek static int 3450865e09a4Sgjelinek copy_zone(char *src, char *dst) 3451865e09a4Sgjelinek { 3452865e09a4Sgjelinek boolean_t out_null = B_FALSE; 3453865e09a4Sgjelinek int status; 3454865e09a4Sgjelinek char *outfile; 3455865e09a4Sgjelinek char cmdbuf[MAXPATHLEN * 2 + 128]; 3456865e09a4Sgjelinek 3457865e09a4Sgjelinek if ((outfile = tempnam("/var/log", "zone")) == NULL) { 3458865e09a4Sgjelinek outfile = "/dev/null"; 3459865e09a4Sgjelinek out_null = B_TRUE; 3460865e09a4Sgjelinek } 3461865e09a4Sgjelinek 34620b5de56dSgjelinek /* 34630b5de56dSgjelinek * Use find to get the list of files to copy. We need to skip 34640b5de56dSgjelinek * files of type "socket" since cpio can't handle those but that 34650b5de56dSgjelinek * should be ok since the app will recreate the socket when it runs. 34660b5de56dSgjelinek * We also need to filter out anything under the .zfs subdir. Since 34670b5de56dSgjelinek * find is running depth-first, we need the extra egrep to filter .zfs. 34680b5de56dSgjelinek */ 3469865e09a4Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 34700b5de56dSgjelinek "cd %s && /usr/bin/find . -type s -prune -o -depth -print | " 347107b574eeSgjelinek "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | " 3472865e09a4Sgjelinek "/usr/bin/cpio -pdmuP@ %s > %s 2>&1", 3473865e09a4Sgjelinek src, dst, outfile); 3474865e09a4Sgjelinek 3475865e09a4Sgjelinek status = do_subproc(cmdbuf); 3476865e09a4Sgjelinek 34779acbbeafSnn35248 if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) { 3478865e09a4Sgjelinek if (!out_null) 3479865e09a4Sgjelinek (void) fprintf(stderr, gettext("\nThe copy failed.\n" 3480865e09a4Sgjelinek "More information can be found in %s\n"), outfile); 34819acbbeafSnn35248 return (Z_ERR); 3482865e09a4Sgjelinek } 3483865e09a4Sgjelinek 3484865e09a4Sgjelinek if (!out_null) 3485865e09a4Sgjelinek (void) unlink(outfile); 3486865e09a4Sgjelinek 3487865e09a4Sgjelinek return (Z_OK); 3488865e09a4Sgjelinek } 3489865e09a4Sgjelinek 3490865e09a4Sgjelinek /* ARGSUSED */ 34910b5de56dSgjelinek static int 3492865e09a4Sgjelinek zfm_print(const char *p, void *r) { 3493865e09a4Sgjelinek zerror(" %s\n", p); 3494865e09a4Sgjelinek return (0); 3495865e09a4Sgjelinek } 3496865e09a4Sgjelinek 34970b5de56dSgjelinek int 34980b5de56dSgjelinek clone_copy(char *source_zonepath, char *zonepath) 34990b5de56dSgjelinek { 35000b5de56dSgjelinek int err; 35010b5de56dSgjelinek 35020b5de56dSgjelinek /* Don't clone the zone if anything is still mounted there */ 35030b5de56dSgjelinek if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) { 35040b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 35050b5de56dSgjelinek "subdirectories of %s.\n"), source_zonepath); 35060b5de56dSgjelinek (void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL); 35070b5de56dSgjelinek return (Z_ERR); 35080b5de56dSgjelinek } 35090b5de56dSgjelinek 35100b5de56dSgjelinek /* 35110b5de56dSgjelinek * Attempt to create a ZFS fs for the zonepath. As usual, we don't 35120b5de56dSgjelinek * care if this works or not since we always have the default behavior 35130b5de56dSgjelinek * of a simple directory for the zonepath. 35140b5de56dSgjelinek */ 35150b5de56dSgjelinek create_zfs_zonepath(zonepath); 35160b5de56dSgjelinek 35170b5de56dSgjelinek (void) printf(gettext("Copying %s..."), source_zonepath); 35180b5de56dSgjelinek (void) fflush(stdout); 35190b5de56dSgjelinek 35200b5de56dSgjelinek err = copy_zone(source_zonepath, zonepath); 35210b5de56dSgjelinek 35220b5de56dSgjelinek (void) printf("\n"); 35230b5de56dSgjelinek 35240b5de56dSgjelinek return (err); 35250b5de56dSgjelinek } 35260b5de56dSgjelinek 3527865e09a4Sgjelinek static int 3528865e09a4Sgjelinek clone_func(int argc, char *argv[]) 3529865e09a4Sgjelinek { 3530865e09a4Sgjelinek char *source_zone = NULL; 3531865e09a4Sgjelinek int lockfd; 3532865e09a4Sgjelinek int err, arg; 3533865e09a4Sgjelinek char zonepath[MAXPATHLEN]; 3534865e09a4Sgjelinek char source_zonepath[MAXPATHLEN]; 3535865e09a4Sgjelinek zone_state_t state; 3536865e09a4Sgjelinek zone_entry_t *zent; 35370b5de56dSgjelinek char *method = NULL; 35380b5de56dSgjelinek char *snapshot = NULL; 3539ff17c8bfSgjelinek char cmdbuf[MAXPATHLEN]; 3540ff17c8bfSgjelinek char postcmdbuf[MAXPATHLEN]; 3541ff17c8bfSgjelinek char presnapbuf[MAXPATHLEN]; 3542ff17c8bfSgjelinek char postsnapbuf[MAXPATHLEN]; 3543ff17c8bfSgjelinek char validsnapbuf[MAXPATHLEN]; 3544ff17c8bfSgjelinek brand_handle_t bh = NULL; 3545ff17c8bfSgjelinek int status; 3546ff17c8bfSgjelinek boolean_t brand_help = B_FALSE; 3547865e09a4Sgjelinek 3548865e09a4Sgjelinek if (zonecfg_in_alt_root()) { 3549865e09a4Sgjelinek zerror(gettext("cannot clone zone in alternate root")); 3550865e09a4Sgjelinek return (Z_ERR); 3551865e09a4Sgjelinek } 3552865e09a4Sgjelinek 3553ff17c8bfSgjelinek /* Check the argv string for args we handle internally */ 3554865e09a4Sgjelinek optind = 0; 3555ff17c8bfSgjelinek opterr = 0; 3556ff17c8bfSgjelinek while ((arg = getopt(argc, argv, "?m:s:")) != EOF) { 3557865e09a4Sgjelinek switch (arg) { 3558865e09a4Sgjelinek case '?': 3559ff17c8bfSgjelinek if (optopt == '?') { 3560865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3561ff17c8bfSgjelinek brand_help = B_TRUE; 3562ff17c8bfSgjelinek } 3563ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 3564ff17c8bfSgjelinek break; 3565865e09a4Sgjelinek case 'm': 3566865e09a4Sgjelinek method = optarg; 3567865e09a4Sgjelinek break; 35680b5de56dSgjelinek case 's': 35690b5de56dSgjelinek snapshot = optarg; 35700b5de56dSgjelinek break; 3571865e09a4Sgjelinek default: 3572ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 3573ff17c8bfSgjelinek break; 3574ff17c8bfSgjelinek } 3575ff17c8bfSgjelinek } 3576ff17c8bfSgjelinek 3577ff17c8bfSgjelinek if (argc != (optind + 1)) { 3578865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3579865e09a4Sgjelinek return (Z_USAGE); 3580865e09a4Sgjelinek } 3581ff17c8bfSgjelinek 3582865e09a4Sgjelinek source_zone = argv[optind]; 3583ff17c8bfSgjelinek 3584ff17c8bfSgjelinek if (!brand_help) { 3585ff17c8bfSgjelinek if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE, 3586ff17c8bfSgjelinek B_FALSE) != Z_OK) 3587865e09a4Sgjelinek return (Z_ERR); 3588ce28b40eSzt129084 if (verify_details(CMD_CLONE, argv) != Z_OK) 3589865e09a4Sgjelinek return (Z_ERR); 3590865e09a4Sgjelinek 3591865e09a4Sgjelinek /* 3592865e09a4Sgjelinek * We also need to do some extra validation on the source zone. 3593865e09a4Sgjelinek */ 3594865e09a4Sgjelinek if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) { 3595ff17c8bfSgjelinek zerror(gettext("%s operation is invalid for the " 3596ff17c8bfSgjelinek "global zone."), cmd_to_str(CMD_CLONE)); 3597865e09a4Sgjelinek return (Z_ERR); 3598865e09a4Sgjelinek } 3599865e09a4Sgjelinek 3600865e09a4Sgjelinek if (strncmp(source_zone, "SUNW", 4) == 0) { 3601ff17c8bfSgjelinek zerror(gettext("%s operation is invalid for zones " 3602ff17c8bfSgjelinek "starting with SUNW."), cmd_to_str(CMD_CLONE)); 3603865e09a4Sgjelinek return (Z_ERR); 3604865e09a4Sgjelinek } 3605865e09a4Sgjelinek 3606865e09a4Sgjelinek zent = lookup_running_zone(source_zone); 3607865e09a4Sgjelinek if (zent != NULL) { 3608865e09a4Sgjelinek /* check whether the zone is ready or running */ 3609ff17c8bfSgjelinek if ((err = zone_get_state(zent->zname, 3610ff17c8bfSgjelinek &zent->zstate_num)) != Z_OK) { 3611865e09a4Sgjelinek errno = err; 3612ff17c8bfSgjelinek zperror2(zent->zname, gettext("could not get " 3613ff17c8bfSgjelinek "state")); 3614865e09a4Sgjelinek /* can't tell, so hedge */ 3615865e09a4Sgjelinek zent->zstate_str = "ready/running"; 3616865e09a4Sgjelinek } else { 3617ff17c8bfSgjelinek zent->zstate_str = 3618ff17c8bfSgjelinek zone_state_str(zent->zstate_num); 3619865e09a4Sgjelinek } 3620865e09a4Sgjelinek zerror(gettext("%s operation is invalid for %s zones."), 3621865e09a4Sgjelinek cmd_to_str(CMD_CLONE), zent->zstate_str); 3622865e09a4Sgjelinek return (Z_ERR); 3623865e09a4Sgjelinek } 3624865e09a4Sgjelinek 3625865e09a4Sgjelinek if ((err = zone_get_state(source_zone, &state)) != Z_OK) { 3626865e09a4Sgjelinek errno = err; 3627865e09a4Sgjelinek zperror2(source_zone, gettext("could not get state")); 3628865e09a4Sgjelinek return (Z_ERR); 3629865e09a4Sgjelinek } 3630865e09a4Sgjelinek if (state != ZONE_STATE_INSTALLED) { 3631865e09a4Sgjelinek (void) fprintf(stderr, 3632865e09a4Sgjelinek gettext("%s: zone %s is %s; %s is required.\n"), 3633865e09a4Sgjelinek execname, source_zone, zone_state_str(state), 3634865e09a4Sgjelinek zone_state_str(ZONE_STATE_INSTALLED)); 3635865e09a4Sgjelinek return (Z_ERR); 3636865e09a4Sgjelinek } 3637865e09a4Sgjelinek 3638865e09a4Sgjelinek /* 3639865e09a4Sgjelinek * The source zone checks out ok, continue with the clone. 3640865e09a4Sgjelinek */ 3641865e09a4Sgjelinek 3642865e09a4Sgjelinek if (validate_clone(source_zone, target_zone) != Z_OK) 3643865e09a4Sgjelinek return (Z_ERR); 3644865e09a4Sgjelinek 3645ff17c8bfSgjelinek if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) { 3646ff17c8bfSgjelinek zerror(gettext("another %s may have an operation in " 3647ff17c8bfSgjelinek "progress."), "zoneadm"); 3648865e09a4Sgjelinek return (Z_ERR); 3649865e09a4Sgjelinek } 3650ff17c8bfSgjelinek } 3651865e09a4Sgjelinek 3652865e09a4Sgjelinek if ((err = zone_get_zonepath(source_zone, source_zonepath, 3653865e09a4Sgjelinek sizeof (source_zonepath))) != Z_OK) { 3654865e09a4Sgjelinek errno = err; 3655865e09a4Sgjelinek zperror2(source_zone, gettext("could not get zone path")); 3656865e09a4Sgjelinek goto done; 3657865e09a4Sgjelinek } 3658865e09a4Sgjelinek 3659865e09a4Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 3660865e09a4Sgjelinek != Z_OK) { 3661865e09a4Sgjelinek errno = err; 3662865e09a4Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 3663865e09a4Sgjelinek goto done; 3664865e09a4Sgjelinek } 3665865e09a4Sgjelinek 3666ff17c8bfSgjelinek /* 3667ff17c8bfSgjelinek * Fetch the clone and postclone hooks from the brand configuration. 3668ff17c8bfSgjelinek */ 3669ff17c8bfSgjelinek if ((bh = brand_open(target_brand)) == NULL) { 3670ff17c8bfSgjelinek zerror(gettext("missing or invalid brand")); 3671ff17c8bfSgjelinek err = Z_ERR; 3672ff17c8bfSgjelinek goto done; 3673ff17c8bfSgjelinek } 3674ff17c8bfSgjelinek 3675ff17c8bfSgjelinek if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_clone, target_zone, 3676ff17c8bfSgjelinek zonepath) != Z_OK) { 3677ff17c8bfSgjelinek zerror("invalid brand configuration: missing clone resource"); 3678ff17c8bfSgjelinek brand_close(bh); 3679ff17c8bfSgjelinek err = Z_ERR; 3680ff17c8bfSgjelinek goto done; 3681ff17c8bfSgjelinek } 3682ff17c8bfSgjelinek 3683ff17c8bfSgjelinek if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postclone, 3684ff17c8bfSgjelinek target_zone, zonepath) != Z_OK) { 3685ff17c8bfSgjelinek zerror("invalid brand configuration: missing postclone " 3686ff17c8bfSgjelinek "resource"); 3687ff17c8bfSgjelinek brand_close(bh); 3688ff17c8bfSgjelinek err = Z_ERR; 3689ff17c8bfSgjelinek goto done; 3690ff17c8bfSgjelinek } 3691ff17c8bfSgjelinek 3692ff17c8bfSgjelinek if (get_hook(bh, presnapbuf, sizeof (presnapbuf), brand_get_presnap, 3693ff17c8bfSgjelinek source_zone, source_zonepath) != Z_OK) { 3694ff17c8bfSgjelinek zerror("invalid brand configuration: missing presnap " 3695ff17c8bfSgjelinek "resource"); 3696ff17c8bfSgjelinek brand_close(bh); 3697ff17c8bfSgjelinek err = Z_ERR; 3698ff17c8bfSgjelinek goto done; 3699ff17c8bfSgjelinek } 3700ff17c8bfSgjelinek 3701ff17c8bfSgjelinek if (get_hook(bh, postsnapbuf, sizeof (postsnapbuf), brand_get_postsnap, 3702ff17c8bfSgjelinek source_zone, source_zonepath) != Z_OK) { 3703ff17c8bfSgjelinek zerror("invalid brand configuration: missing postsnap " 3704ff17c8bfSgjelinek "resource"); 3705ff17c8bfSgjelinek brand_close(bh); 3706ff17c8bfSgjelinek err = Z_ERR; 3707ff17c8bfSgjelinek goto done; 3708ff17c8bfSgjelinek } 3709ff17c8bfSgjelinek 3710ff17c8bfSgjelinek if (get_hook(bh, validsnapbuf, sizeof (validsnapbuf), 3711ff17c8bfSgjelinek brand_get_validatesnap, target_zone, zonepath) != Z_OK) { 3712ff17c8bfSgjelinek zerror("invalid brand configuration: missing validatesnap " 3713ff17c8bfSgjelinek "resource"); 3714ff17c8bfSgjelinek brand_close(bh); 3715ff17c8bfSgjelinek err = Z_ERR; 3716ff17c8bfSgjelinek goto done; 3717ff17c8bfSgjelinek } 3718ff17c8bfSgjelinek brand_close(bh); 3719ff17c8bfSgjelinek 3720ff17c8bfSgjelinek /* Append all options to clone hook. */ 3721ff17c8bfSgjelinek if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) { 3722ff17c8bfSgjelinek err = Z_ERR; 3723ff17c8bfSgjelinek goto done; 3724ff17c8bfSgjelinek } 3725ff17c8bfSgjelinek 3726ff17c8bfSgjelinek /* Append all options to postclone hook. */ 3727ff17c8bfSgjelinek if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK) { 3728ff17c8bfSgjelinek err = Z_ERR; 3729ff17c8bfSgjelinek goto done; 3730ff17c8bfSgjelinek } 3731ff17c8bfSgjelinek 3732ff17c8bfSgjelinek if (!brand_help) { 3733865e09a4Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE)) 3734865e09a4Sgjelinek != Z_OK) { 3735865e09a4Sgjelinek errno = err; 3736865e09a4Sgjelinek zperror2(target_zone, gettext("could not set state")); 3737865e09a4Sgjelinek goto done; 3738865e09a4Sgjelinek } 3739ff17c8bfSgjelinek } 3740ff17c8bfSgjelinek 3741ff17c8bfSgjelinek /* 3742ff17c8bfSgjelinek * The clone hook is optional. If it exists, use the hook for 3743ff17c8bfSgjelinek * cloning, otherwise use the built-in clone support 3744ff17c8bfSgjelinek */ 3745ff17c8bfSgjelinek if (cmdbuf[0] != '\0') { 3746ff17c8bfSgjelinek /* Run the clone hook */ 3747c75cc341S status = do_subproc(cmdbuf); 3748ff17c8bfSgjelinek if ((status = subproc_status(gettext("brand-specific clone"), 3749ff17c8bfSgjelinek status, B_FALSE)) != ZONE_SUBPROC_OK) { 3750ff17c8bfSgjelinek if (status == ZONE_SUBPROC_USAGE && !brand_help) 3751ff17c8bfSgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3752ff17c8bfSgjelinek err = Z_ERR; 3753ff17c8bfSgjelinek goto done; 3754ff17c8bfSgjelinek } 3755ff17c8bfSgjelinek 3756ff17c8bfSgjelinek if (brand_help) 3757ff17c8bfSgjelinek return (Z_OK); 3758ff17c8bfSgjelinek 3759ff17c8bfSgjelinek } else { 3760ff17c8bfSgjelinek /* If just help, we're done since there is no brand help. */ 3761ff17c8bfSgjelinek if (brand_help) 3762ff17c8bfSgjelinek return (Z_OK); 3763ff17c8bfSgjelinek 3764ff17c8bfSgjelinek /* Run the built-in clone support. */ 3765ff17c8bfSgjelinek 3766ff17c8bfSgjelinek /* The only explicit built-in method is "copy". */ 3767ff17c8bfSgjelinek if (method != NULL && strcmp(method, "copy") != 0) { 3768ff17c8bfSgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3769ff17c8bfSgjelinek err = Z_USAGE; 3770ff17c8bfSgjelinek goto done; 3771ff17c8bfSgjelinek } 3772865e09a4Sgjelinek 37730b5de56dSgjelinek if (snapshot != NULL) { 3774ff17c8bfSgjelinek err = clone_snapshot_zfs(snapshot, zonepath, 3775ff17c8bfSgjelinek validsnapbuf); 37760b5de56dSgjelinek } else { 37770b5de56dSgjelinek /* 3778ff17c8bfSgjelinek * We always copy the clone unless the source is ZFS 3779ff17c8bfSgjelinek * and a ZFS clone worked. We fallback to copying if 3780ff17c8bfSgjelinek * the ZFS clone fails for some reason. 37810b5de56dSgjelinek */ 37820b5de56dSgjelinek err = Z_ERR; 37830b5de56dSgjelinek if (method == NULL && is_zonepath_zfs(source_zonepath)) 3784ff17c8bfSgjelinek err = clone_zfs(source_zonepath, zonepath, 3785ff17c8bfSgjelinek presnapbuf, postsnapbuf); 3786865e09a4Sgjelinek 37875cd08338Sgjelinek if (err != Z_OK) 37880b5de56dSgjelinek err = clone_copy(source_zonepath, zonepath); 37890b5de56dSgjelinek } 3790ff17c8bfSgjelinek } 3791865e09a4Sgjelinek 3792ff17c8bfSgjelinek if (err == Z_OK && postcmdbuf[0] != '\0') { 3793ff17c8bfSgjelinek status = do_subproc(postcmdbuf); 3794ff17c8bfSgjelinek if ((err = subproc_status("postclone", status, B_FALSE)) 3795ff17c8bfSgjelinek != ZONE_SUBPROC_OK) { 3796ff17c8bfSgjelinek zerror(gettext("post-clone configuration failed.")); 3797ff17c8bfSgjelinek err = Z_ERR; 3798ff17c8bfSgjelinek } 3799ff17c8bfSgjelinek } 3800865e09a4Sgjelinek 3801865e09a4Sgjelinek done: 38029acbbeafSnn35248 /* 38039acbbeafSnn35248 * If everything went well, we mark the zone as installed. 38049acbbeafSnn35248 */ 38059acbbeafSnn35248 if (err == Z_OK) { 38069acbbeafSnn35248 err = zone_set_state(target_zone, ZONE_STATE_INSTALLED); 38079acbbeafSnn35248 if (err != Z_OK) { 38089acbbeafSnn35248 errno = err; 38099acbbeafSnn35248 zperror2(target_zone, gettext("could not set state")); 38109acbbeafSnn35248 } 38119acbbeafSnn35248 } 3812ff17c8bfSgjelinek if (!brand_help) 3813ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 3814865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3815865e09a4Sgjelinek } 3816865e09a4Sgjelinek 381707b574eeSgjelinek /* 38180b5de56dSgjelinek * Used when removing a zonepath after uninstalling or cleaning up after 38190b5de56dSgjelinek * the move subcommand. This handles a zonepath that has non-standard 38200b5de56dSgjelinek * contents so that we will only cleanup the stuff we know about and leave 38210b5de56dSgjelinek * any user data alone. 382207b574eeSgjelinek * 38230b5de56dSgjelinek * If the "all" parameter is true then we should remove the whole zonepath 38240b5de56dSgjelinek * even if it has non-standard files/directories in it. This can be used when 38250b5de56dSgjelinek * we need to cleanup after moving the zonepath across file systems. 38260b5de56dSgjelinek * 38270b5de56dSgjelinek * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND 38280b5de56dSgjelinek * and not the shell. 382907b574eeSgjelinek */ 383007b574eeSgjelinek static int 38310b5de56dSgjelinek cleanup_zonepath(char *zonepath, boolean_t all) 383207b574eeSgjelinek { 383307b574eeSgjelinek int status; 38340b5de56dSgjelinek int i; 38350b5de56dSgjelinek boolean_t non_std = B_FALSE; 38360b5de56dSgjelinek struct dirent *dp; 38370b5de56dSgjelinek DIR *dirp; 3838d9e728a2Sgjelinek /* 3839d9e728a2Sgjelinek * The SUNWattached.xml file is expected since it might 3840d9e728a2Sgjelinek * exist if the zone was force-attached after a 3841d9e728a2Sgjelinek * migration. 3842d9e728a2Sgjelinek */ 3843d9e728a2Sgjelinek char *std_entries[] = {"dev", "lu", "root", 3844d9e728a2Sgjelinek "SUNWattached.xml", NULL}; 38450b5de56dSgjelinek /* (MAXPATHLEN * 3) is for the 3 std_entries dirs */ 38460b5de56dSgjelinek char cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64]; 384707b574eeSgjelinek 384807b574eeSgjelinek /* 38490b5de56dSgjelinek * We shouldn't need these checks but lets be paranoid since we 38500b5de56dSgjelinek * could blow away the whole system here if we got the wrong zonepath. 385107b574eeSgjelinek */ 38520b5de56dSgjelinek if (*zonepath == NULL || strcmp(zonepath, "/") == 0) { 38530b5de56dSgjelinek (void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath); 38540b5de56dSgjelinek return (Z_INVAL); 38550b5de56dSgjelinek } 38560b5de56dSgjelinek 385707b574eeSgjelinek /* 38580b5de56dSgjelinek * If the dirpath is already gone (maybe it was manually removed) then 38590b5de56dSgjelinek * we just return Z_OK so that the cleanup is successful. 386007b574eeSgjelinek */ 38610b5de56dSgjelinek if ((dirp = opendir(zonepath)) == NULL) 38620b5de56dSgjelinek return (Z_OK); 38630b5de56dSgjelinek 38640b5de56dSgjelinek /* 38650b5de56dSgjelinek * Look through the zonepath directory to see if there are any 38660b5de56dSgjelinek * non-standard files/dirs. Also skip .zfs since that might be 38670b5de56dSgjelinek * there but we'll handle ZFS file systems as a special case. 38680b5de56dSgjelinek */ 38690b5de56dSgjelinek while ((dp = readdir(dirp)) != NULL) { 38700b5de56dSgjelinek if (strcmp(dp->d_name, ".") == 0 || 38710b5de56dSgjelinek strcmp(dp->d_name, "..") == 0 || 38720b5de56dSgjelinek strcmp(dp->d_name, ".zfs") == 0) 38730b5de56dSgjelinek continue; 38740b5de56dSgjelinek 38750b5de56dSgjelinek for (i = 0; std_entries[i] != NULL; i++) 38760b5de56dSgjelinek if (strcmp(dp->d_name, std_entries[i]) == 0) 38770b5de56dSgjelinek break; 38780b5de56dSgjelinek 38790b5de56dSgjelinek if (std_entries[i] == NULL) 38800b5de56dSgjelinek non_std = B_TRUE; 38810b5de56dSgjelinek } 38820b5de56dSgjelinek (void) closedir(dirp); 38830b5de56dSgjelinek 38840b5de56dSgjelinek if (!all && non_std) { 38850b5de56dSgjelinek /* 38860b5de56dSgjelinek * There are extra, non-standard directories/files in the 38870b5de56dSgjelinek * zonepath so we don't want to remove the zonepath. We 38880b5de56dSgjelinek * just want to remove the standard directories and leave 38890b5de56dSgjelinek * the user data alone. 38900b5de56dSgjelinek */ 38910b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND); 38920b5de56dSgjelinek 38930b5de56dSgjelinek for (i = 0; std_entries[i] != NULL; i++) { 38940b5de56dSgjelinek char tmpbuf[MAXPATHLEN]; 38950b5de56dSgjelinek 38960b5de56dSgjelinek if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s", 38970b5de56dSgjelinek zonepath, std_entries[i]) >= sizeof (tmpbuf) || 38980b5de56dSgjelinek strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >= 38990b5de56dSgjelinek sizeof (cmdbuf)) { 39000b5de56dSgjelinek (void) fprintf(stderr, 39010b5de56dSgjelinek gettext("path is too long\n")); 39020b5de56dSgjelinek return (Z_INVAL); 39030b5de56dSgjelinek } 390407b574eeSgjelinek } 390507b574eeSgjelinek 390607b574eeSgjelinek status = do_subproc(cmdbuf); 390707b574eeSgjelinek 39080b5de56dSgjelinek (void) fprintf(stderr, gettext("WARNING: Unable to completely " 39090b5de56dSgjelinek "remove %s\nbecause it contains additional user data. " 39100b5de56dSgjelinek "Only the standard directory\nentries have been " 39110b5de56dSgjelinek "removed.\n"), 39120b5de56dSgjelinek zonepath); 391307b574eeSgjelinek 39149acbbeafSnn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == 39159acbbeafSnn35248 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR); 39160b5de56dSgjelinek } 39170b5de56dSgjelinek 39180b5de56dSgjelinek /* 39190b5de56dSgjelinek * There is nothing unexpected in the zonepath, try to get rid of the 39200b5de56dSgjelinek * whole zonepath directory. 39210b5de56dSgjelinek * 39220b5de56dSgjelinek * If the zonepath is its own zfs file system, try to destroy the 39230b5de56dSgjelinek * file system. If that fails for some reason (e.g. it has clones) 39240b5de56dSgjelinek * then we'll just remove the contents of the zonepath. 39250b5de56dSgjelinek */ 39260b5de56dSgjelinek if (is_zonepath_zfs(zonepath)) { 39270b5de56dSgjelinek if (destroy_zfs(zonepath) == Z_OK) 39280b5de56dSgjelinek return (Z_OK); 39290b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND 39300b5de56dSgjelinek " %s/*", zonepath); 39310b5de56dSgjelinek status = do_subproc(cmdbuf); 39329acbbeafSnn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == 39339acbbeafSnn35248 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR); 39340b5de56dSgjelinek } 39350b5de56dSgjelinek 39360b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 39370b5de56dSgjelinek zonepath); 39380b5de56dSgjelinek status = do_subproc(cmdbuf); 39399acbbeafSnn35248 39409acbbeafSnn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK) 39419acbbeafSnn35248 ? Z_OK : Z_ERR); 394207b574eeSgjelinek } 394307b574eeSgjelinek 3944865e09a4Sgjelinek static int 3945865e09a4Sgjelinek move_func(int argc, char *argv[]) 3946865e09a4Sgjelinek { 3947865e09a4Sgjelinek char *new_zonepath = NULL; 3948865e09a4Sgjelinek int lockfd; 3949865e09a4Sgjelinek int err, arg; 3950865e09a4Sgjelinek char zonepath[MAXPATHLEN]; 3951865e09a4Sgjelinek zone_dochandle_t handle; 3952865e09a4Sgjelinek boolean_t fast; 39530b5de56dSgjelinek boolean_t is_zfs = B_FALSE; 39540b5de56dSgjelinek struct dirent *dp; 39550b5de56dSgjelinek DIR *dirp; 39560b5de56dSgjelinek boolean_t empty = B_TRUE; 3957865e09a4Sgjelinek boolean_t revert; 3958865e09a4Sgjelinek struct stat zonepath_buf; 3959865e09a4Sgjelinek struct stat new_zonepath_buf; 3960865e09a4Sgjelinek 3961865e09a4Sgjelinek if (zonecfg_in_alt_root()) { 3962865e09a4Sgjelinek zerror(gettext("cannot move zone in alternate root")); 3963865e09a4Sgjelinek return (Z_ERR); 3964865e09a4Sgjelinek } 3965865e09a4Sgjelinek 3966865e09a4Sgjelinek optind = 0; 3967865e09a4Sgjelinek if ((arg = getopt(argc, argv, "?")) != EOF) { 3968865e09a4Sgjelinek switch (arg) { 3969865e09a4Sgjelinek case '?': 3970865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 3971865e09a4Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 3972865e09a4Sgjelinek default: 3973865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 3974865e09a4Sgjelinek return (Z_USAGE); 3975865e09a4Sgjelinek } 3976865e09a4Sgjelinek } 3977865e09a4Sgjelinek if (argc != (optind + 1)) { 3978865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 3979865e09a4Sgjelinek return (Z_USAGE); 3980865e09a4Sgjelinek } 3981865e09a4Sgjelinek new_zonepath = argv[optind]; 39829acbbeafSnn35248 if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE) 39839acbbeafSnn35248 != Z_OK) 3984865e09a4Sgjelinek return (Z_ERR); 3985ce28b40eSzt129084 if (verify_details(CMD_MOVE, argv) != Z_OK) 3986865e09a4Sgjelinek return (Z_ERR); 3987865e09a4Sgjelinek 3988865e09a4Sgjelinek /* 3989865e09a4Sgjelinek * Check out the new zonepath. This has the side effect of creating 3990865e09a4Sgjelinek * a directory for the new zonepath. We depend on this later when we 39910b5de56dSgjelinek * stat to see if we are doing a cross file system move or not. 3992865e09a4Sgjelinek */ 3993865e09a4Sgjelinek if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK) 3994865e09a4Sgjelinek return (Z_ERR); 3995865e09a4Sgjelinek 3996865e09a4Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 3997865e09a4Sgjelinek != Z_OK) { 3998865e09a4Sgjelinek errno = err; 3999865e09a4Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 4000865e09a4Sgjelinek return (Z_ERR); 4001865e09a4Sgjelinek } 4002865e09a4Sgjelinek 4003865e09a4Sgjelinek if (stat(zonepath, &zonepath_buf) == -1) { 4004865e09a4Sgjelinek zperror(gettext("could not stat zone path"), B_FALSE); 4005865e09a4Sgjelinek return (Z_ERR); 4006865e09a4Sgjelinek } 4007865e09a4Sgjelinek 4008865e09a4Sgjelinek if (stat(new_zonepath, &new_zonepath_buf) == -1) { 4009865e09a4Sgjelinek zperror(gettext("could not stat new zone path"), B_FALSE); 4010865e09a4Sgjelinek return (Z_ERR); 4011865e09a4Sgjelinek } 4012865e09a4Sgjelinek 40130b5de56dSgjelinek /* 40140b5de56dSgjelinek * Check if the destination directory is empty. 40150b5de56dSgjelinek */ 40160b5de56dSgjelinek if ((dirp = opendir(new_zonepath)) == NULL) { 40170b5de56dSgjelinek zperror(gettext("could not open new zone path"), B_FALSE); 40180b5de56dSgjelinek return (Z_ERR); 40190b5de56dSgjelinek } 40200b5de56dSgjelinek while ((dp = readdir(dirp)) != (struct dirent *)0) { 40210b5de56dSgjelinek if (strcmp(dp->d_name, ".") == 0 || 40220b5de56dSgjelinek strcmp(dp->d_name, "..") == 0) 40230b5de56dSgjelinek continue; 40240b5de56dSgjelinek empty = B_FALSE; 40250b5de56dSgjelinek break; 40260b5de56dSgjelinek } 40270b5de56dSgjelinek (void) closedir(dirp); 40280b5de56dSgjelinek 40290b5de56dSgjelinek /* Error if there is anything in the destination directory. */ 40300b5de56dSgjelinek if (!empty) { 40310b5de56dSgjelinek (void) fprintf(stderr, gettext("could not move zone to %s: " 40320b5de56dSgjelinek "directory not empty\n"), new_zonepath); 40330b5de56dSgjelinek return (Z_ERR); 40340b5de56dSgjelinek } 40350b5de56dSgjelinek 4036865e09a4Sgjelinek /* Don't move the zone if anything is still mounted there */ 4037865e09a4Sgjelinek if (zonecfg_find_mounts(zonepath, NULL, NULL)) { 40380b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 4039865e09a4Sgjelinek "subdirectories of %s.\n"), zonepath); 4040865e09a4Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 4041865e09a4Sgjelinek return (Z_ERR); 4042865e09a4Sgjelinek } 4043865e09a4Sgjelinek 4044865e09a4Sgjelinek /* 4045865e09a4Sgjelinek * Check if we are moving in the same file system and can do a fast 4046865e09a4Sgjelinek * move or if we are crossing file systems and have to copy the data. 4047865e09a4Sgjelinek */ 4048865e09a4Sgjelinek fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev); 4049865e09a4Sgjelinek 4050865e09a4Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 4051865e09a4Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 4052865e09a4Sgjelinek return (Z_ERR); 4053865e09a4Sgjelinek } 4054865e09a4Sgjelinek 4055865e09a4Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 4056865e09a4Sgjelinek errno = err; 4057865e09a4Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 4058865e09a4Sgjelinek zonecfg_fini_handle(handle); 4059865e09a4Sgjelinek return (Z_ERR); 4060865e09a4Sgjelinek } 4061865e09a4Sgjelinek 4062ff17c8bfSgjelinek if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) { 4063865e09a4Sgjelinek zerror(gettext("another %s may have an operation in progress."), 4064865e09a4Sgjelinek "zoneadm"); 4065865e09a4Sgjelinek zonecfg_fini_handle(handle); 4066865e09a4Sgjelinek return (Z_ERR); 4067865e09a4Sgjelinek } 4068865e09a4Sgjelinek 4069865e09a4Sgjelinek /* 40700b5de56dSgjelinek * We're making some file system changes now so we have to clean up 40710b5de56dSgjelinek * the file system before we are done. This will either clean up the 4072865e09a4Sgjelinek * new zonepath if the zonecfg update failed or it will clean up the 4073865e09a4Sgjelinek * old zonepath if everything is ok. 4074865e09a4Sgjelinek */ 4075865e09a4Sgjelinek revert = B_TRUE; 4076865e09a4Sgjelinek 40770b5de56dSgjelinek if (is_zonepath_zfs(zonepath) && 40780b5de56dSgjelinek move_zfs(zonepath, new_zonepath) != Z_ERR) { 40790b5de56dSgjelinek is_zfs = B_TRUE; 40800b5de56dSgjelinek 40810b5de56dSgjelinek } else if (fast) { 4082865e09a4Sgjelinek /* same file system, use rename for a quick move */ 4083865e09a4Sgjelinek 4084865e09a4Sgjelinek /* 4085865e09a4Sgjelinek * Remove the new_zonepath directory that got created above 4086865e09a4Sgjelinek * during the validation. It gets in the way of the rename. 4087865e09a4Sgjelinek */ 4088865e09a4Sgjelinek if (rmdir(new_zonepath) != 0) { 4089865e09a4Sgjelinek zperror(gettext("could not rmdir new zone path"), 4090865e09a4Sgjelinek B_FALSE); 4091865e09a4Sgjelinek zonecfg_fini_handle(handle); 4092ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 4093865e09a4Sgjelinek return (Z_ERR); 4094865e09a4Sgjelinek } 4095865e09a4Sgjelinek 4096865e09a4Sgjelinek if (rename(zonepath, new_zonepath) != 0) { 4097865e09a4Sgjelinek /* 4098865e09a4Sgjelinek * If this fails we don't need to do all of the 4099865e09a4Sgjelinek * cleanup that happens for the rest of the code 4100865e09a4Sgjelinek * so just return from this error. 4101865e09a4Sgjelinek */ 4102865e09a4Sgjelinek zperror(gettext("could not move zone"), B_FALSE); 4103865e09a4Sgjelinek zonecfg_fini_handle(handle); 4104ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 4105865e09a4Sgjelinek return (Z_ERR); 4106865e09a4Sgjelinek } 4107865e09a4Sgjelinek 4108865e09a4Sgjelinek } else { 41090b5de56dSgjelinek /* 41100b5de56dSgjelinek * Attempt to create a ZFS fs for the new zonepath. As usual, 41110b5de56dSgjelinek * we don't care if this works or not since we always have the 41120b5de56dSgjelinek * default behavior of a simple directory for the zonepath. 41130b5de56dSgjelinek */ 41140b5de56dSgjelinek create_zfs_zonepath(new_zonepath); 41150b5de56dSgjelinek 4116865e09a4Sgjelinek (void) printf(gettext( 41170b5de56dSgjelinek "Moving across file systems; copying zonepath %s..."), 4118865e09a4Sgjelinek zonepath); 4119865e09a4Sgjelinek (void) fflush(stdout); 4120865e09a4Sgjelinek 4121865e09a4Sgjelinek err = copy_zone(zonepath, new_zonepath); 4122865e09a4Sgjelinek 4123865e09a4Sgjelinek (void) printf("\n"); 4124865e09a4Sgjelinek if (err != Z_OK) 4125865e09a4Sgjelinek goto done; 4126865e09a4Sgjelinek } 4127865e09a4Sgjelinek 4128865e09a4Sgjelinek if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) { 4129865e09a4Sgjelinek errno = err; 4130865e09a4Sgjelinek zperror(gettext("could not set new zonepath"), B_TRUE); 4131865e09a4Sgjelinek goto done; 4132865e09a4Sgjelinek } 4133865e09a4Sgjelinek 4134865e09a4Sgjelinek if ((err = zonecfg_save(handle)) != Z_OK) { 4135865e09a4Sgjelinek errno = err; 4136865e09a4Sgjelinek zperror(gettext("zonecfg save failed"), B_TRUE); 4137865e09a4Sgjelinek goto done; 4138865e09a4Sgjelinek } 4139865e09a4Sgjelinek 4140865e09a4Sgjelinek revert = B_FALSE; 4141865e09a4Sgjelinek 4142865e09a4Sgjelinek done: 4143865e09a4Sgjelinek zonecfg_fini_handle(handle); 4144ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 4145865e09a4Sgjelinek 4146865e09a4Sgjelinek /* 41470b5de56dSgjelinek * Clean up the file system based on how things went. We either 4148865e09a4Sgjelinek * clean up the new zonepath if the operation failed for some reason 4149865e09a4Sgjelinek * or we clean up the old zonepath if everything is ok. 4150865e09a4Sgjelinek */ 4151865e09a4Sgjelinek if (revert) { 4152865e09a4Sgjelinek /* The zonecfg update failed, cleanup the new zonepath. */ 41530b5de56dSgjelinek if (is_zfs) { 41540b5de56dSgjelinek if (move_zfs(new_zonepath, zonepath) == Z_ERR) { 41550b5de56dSgjelinek (void) fprintf(stderr, gettext("could not " 41560b5de56dSgjelinek "restore zonepath, the zfs mountpoint is " 41570b5de56dSgjelinek "set as:\n%s\n"), new_zonepath); 41580b5de56dSgjelinek /* 41590b5de56dSgjelinek * err is already != Z_OK since we're reverting 41600b5de56dSgjelinek */ 41610b5de56dSgjelinek } 41620b5de56dSgjelinek 41630b5de56dSgjelinek } else if (fast) { 4164865e09a4Sgjelinek if (rename(new_zonepath, zonepath) != 0) { 4165865e09a4Sgjelinek zperror(gettext("could not restore zonepath"), 4166865e09a4Sgjelinek B_FALSE); 4167865e09a4Sgjelinek /* 4168865e09a4Sgjelinek * err is already != Z_OK since we're reverting 4169865e09a4Sgjelinek */ 4170865e09a4Sgjelinek } 4171865e09a4Sgjelinek } else { 4172865e09a4Sgjelinek (void) printf(gettext("Cleaning up zonepath %s..."), 4173865e09a4Sgjelinek new_zonepath); 4174865e09a4Sgjelinek (void) fflush(stdout); 41750b5de56dSgjelinek err = cleanup_zonepath(new_zonepath, B_TRUE); 4176865e09a4Sgjelinek (void) printf("\n"); 4177865e09a4Sgjelinek 417807b574eeSgjelinek if (err != Z_OK) { 4179865e09a4Sgjelinek errno = err; 4180865e09a4Sgjelinek zperror(gettext("could not remove new " 4181865e09a4Sgjelinek "zonepath"), B_TRUE); 4182865e09a4Sgjelinek } else { 4183865e09a4Sgjelinek /* 4184865e09a4Sgjelinek * Because we're reverting we know the mainline 4185865e09a4Sgjelinek * code failed but we just reused the err 4186865e09a4Sgjelinek * variable so we reset it back to Z_ERR. 4187865e09a4Sgjelinek */ 4188865e09a4Sgjelinek err = Z_ERR; 4189865e09a4Sgjelinek } 4190865e09a4Sgjelinek } 4191865e09a4Sgjelinek 4192865e09a4Sgjelinek } else { 4193865e09a4Sgjelinek /* The move was successful, cleanup the old zonepath. */ 41940b5de56dSgjelinek if (!is_zfs && !fast) { 4195865e09a4Sgjelinek (void) printf( 4196865e09a4Sgjelinek gettext("Cleaning up zonepath %s..."), zonepath); 4197865e09a4Sgjelinek (void) fflush(stdout); 41980b5de56dSgjelinek err = cleanup_zonepath(zonepath, B_TRUE); 4199865e09a4Sgjelinek (void) printf("\n"); 4200865e09a4Sgjelinek 420107b574eeSgjelinek if (err != Z_OK) { 4202865e09a4Sgjelinek errno = err; 4203865e09a4Sgjelinek zperror(gettext("could not remove zonepath"), 4204865e09a4Sgjelinek B_TRUE); 4205865e09a4Sgjelinek } 4206865e09a4Sgjelinek } 4207865e09a4Sgjelinek } 4208865e09a4Sgjelinek 4209865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 4210865e09a4Sgjelinek } 4211865e09a4Sgjelinek 4212ff17c8bfSgjelinek /* ARGSUSED */ 4213ee519a1fSgjelinek static int 4214ee519a1fSgjelinek detach_func(int argc, char *argv[]) 4215ee519a1fSgjelinek { 421671443f5aS int lockfd = -1; 4217ee519a1fSgjelinek int err, arg; 4218ee519a1fSgjelinek char zonepath[MAXPATHLEN]; 421937774979Sgjelinek char cmdbuf[MAXPATHLEN]; 4220ff17c8bfSgjelinek char precmdbuf[MAXPATHLEN]; 42218cd327d5Sgjelinek boolean_t execute = B_TRUE; 4222ff17c8bfSgjelinek boolean_t brand_help = B_FALSE; 422337774979Sgjelinek brand_handle_t bh = NULL; 4224ff17c8bfSgjelinek int status; 4225ee519a1fSgjelinek 4226ee519a1fSgjelinek if (zonecfg_in_alt_root()) { 4227ee519a1fSgjelinek zerror(gettext("cannot detach zone in alternate root")); 4228ee519a1fSgjelinek return (Z_ERR); 4229ee519a1fSgjelinek } 4230ee519a1fSgjelinek 4231ff17c8bfSgjelinek /* Check the argv string for args we handle internally */ 4232ee519a1fSgjelinek optind = 0; 4233ff17c8bfSgjelinek opterr = 0; 4234ff17c8bfSgjelinek while ((arg = getopt(argc, argv, "?n")) != EOF) { 4235ee519a1fSgjelinek switch (arg) { 4236ee519a1fSgjelinek case '?': 4237ff17c8bfSgjelinek if (optopt == '?') { 4238ee519a1fSgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 4239ff17c8bfSgjelinek brand_help = B_TRUE; 4240ff17c8bfSgjelinek } 4241ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 4242ff17c8bfSgjelinek break; 42438cd327d5Sgjelinek case 'n': 42448cd327d5Sgjelinek execute = B_FALSE; 42458cd327d5Sgjelinek break; 4246ee519a1fSgjelinek default: 4247ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 4248ff17c8bfSgjelinek break; 4249ee519a1fSgjelinek } 4250ee519a1fSgjelinek } 42519acbbeafSnn35248 4252ff17c8bfSgjelinek if (brand_help) 4253ff17c8bfSgjelinek execute = B_FALSE; 4254ff17c8bfSgjelinek 42558cd327d5Sgjelinek if (execute) { 42569acbbeafSnn35248 if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE, 42579acbbeafSnn35248 B_FALSE) != Z_OK) 4258ee519a1fSgjelinek return (Z_ERR); 4259ce28b40eSzt129084 if (verify_details(CMD_DETACH, argv) != Z_OK) 4260ee519a1fSgjelinek return (Z_ERR); 42618cd327d5Sgjelinek } else { 42628cd327d5Sgjelinek /* 42638cd327d5Sgjelinek * We want a dry-run to work for a non-privileged user so we 42648cd327d5Sgjelinek * only do minimal validation. 42658cd327d5Sgjelinek */ 42668cd327d5Sgjelinek if (target_zone == NULL) { 42678cd327d5Sgjelinek zerror(gettext("no zone specified")); 42688cd327d5Sgjelinek return (Z_ERR); 42698cd327d5Sgjelinek } 42708cd327d5Sgjelinek 42718cd327d5Sgjelinek if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) { 42728cd327d5Sgjelinek zerror(gettext("%s operation is invalid for the " 42738cd327d5Sgjelinek "global zone."), cmd_to_str(CMD_DETACH)); 42748cd327d5Sgjelinek return (Z_ERR); 42758cd327d5Sgjelinek } 42768cd327d5Sgjelinek } 4277ee519a1fSgjelinek 4278ee519a1fSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 4279ee519a1fSgjelinek != Z_OK) { 4280ee519a1fSgjelinek errno = err; 4281ee519a1fSgjelinek zperror2(target_zone, gettext("could not get zone path")); 4282ee519a1fSgjelinek return (Z_ERR); 4283ee519a1fSgjelinek } 4284ee519a1fSgjelinek 4285ff17c8bfSgjelinek /* Fetch the detach and predetach hooks from the brand configuration. */ 428637774979Sgjelinek if ((bh = brand_open(target_brand)) == NULL) { 428737774979Sgjelinek zerror(gettext("missing or invalid brand")); 428837774979Sgjelinek return (Z_ERR); 428937774979Sgjelinek } 429037774979Sgjelinek 4291ff17c8bfSgjelinek if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_detach, target_zone, 4292ff17c8bfSgjelinek zonepath) != Z_OK) { 4293ff17c8bfSgjelinek zerror("invalid brand configuration: missing detach resource"); 4294ff17c8bfSgjelinek brand_close(bh); 4295ff17c8bfSgjelinek return (Z_ERR); 4296ff17c8bfSgjelinek } 4297ff17c8bfSgjelinek 4298ff17c8bfSgjelinek if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_predetach, 4299ff17c8bfSgjelinek target_zone, zonepath) != Z_OK) { 430037774979Sgjelinek zerror("invalid brand configuration: missing predetach " 430137774979Sgjelinek "resource"); 430237774979Sgjelinek brand_close(bh); 430337774979Sgjelinek return (Z_ERR); 430437774979Sgjelinek } 430537774979Sgjelinek brand_close(bh); 430637774979Sgjelinek 4307ff17c8bfSgjelinek /* Append all options to predetach hook. */ 4308ff17c8bfSgjelinek if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK) 430937774979Sgjelinek return (Z_ERR); 431037774979Sgjelinek 4311ff17c8bfSgjelinek /* Append all options to detach hook. */ 4312ff17c8bfSgjelinek if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) 431337774979Sgjelinek return (Z_ERR); 431437774979Sgjelinek 4315ff17c8bfSgjelinek if (execute && zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) { 4316ee519a1fSgjelinek zerror(gettext("another %s may have an operation in progress."), 4317ee519a1fSgjelinek "zoneadm"); 4318ee519a1fSgjelinek return (Z_ERR); 4319ee519a1fSgjelinek } 4320ee519a1fSgjelinek 4321ff17c8bfSgjelinek /* If we have a brand predetach hook, run it. */ 4322ff17c8bfSgjelinek if (!brand_help && precmdbuf[0] != '\0') { 4323ff17c8bfSgjelinek status = do_subproc(precmdbuf); 4324ff17c8bfSgjelinek if (subproc_status(gettext("brand-specific predetach"), 4325ff17c8bfSgjelinek status, B_FALSE) != ZONE_SUBPROC_OK) { 4326ff17c8bfSgjelinek 432771443f5aS if (execute) { 432871443f5aS assert(lockfd >= 0); 4329ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 433071443f5aS lockfd = -1; 433171443f5aS } 433271443f5aS 433371443f5aS assert(lockfd == -1); 4334ff17c8bfSgjelinek return (Z_ERR); 4335ff17c8bfSgjelinek } 4336ff17c8bfSgjelinek } 4337ff17c8bfSgjelinek 4338ff17c8bfSgjelinek if (cmdbuf[0] != '\0') { 4339ff17c8bfSgjelinek /* Run the detach hook */ 4340c75cc341S status = do_subproc(cmdbuf); 4341ff17c8bfSgjelinek if ((status = subproc_status(gettext("brand-specific detach"), 4342ff17c8bfSgjelinek status, B_FALSE)) != ZONE_SUBPROC_OK) { 4343ff17c8bfSgjelinek if (status == ZONE_SUBPROC_USAGE && !brand_help) 4344ff17c8bfSgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 4345ff17c8bfSgjelinek 434671443f5aS if (execute) { 434771443f5aS assert(lockfd >= 0); 4348ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 434971443f5aS lockfd = -1; 435071443f5aS } 4351ff17c8bfSgjelinek 435271443f5aS assert(lockfd == -1); 4353ff17c8bfSgjelinek return (Z_ERR); 4354ff17c8bfSgjelinek } 4355ff17c8bfSgjelinek 4356ff17c8bfSgjelinek } else { 435771443f5aS zone_dochandle_t handle; 435871443f5aS 4359ff17c8bfSgjelinek /* If just help, we're done since there is no brand help. */ 436071443f5aS if (brand_help) { 436171443f5aS assert(lockfd == -1); 4362ff17c8bfSgjelinek return (Z_OK); 436371443f5aS } 4364ff17c8bfSgjelinek 4365ff17c8bfSgjelinek /* 4366ff17c8bfSgjelinek * Run the built-in detach support. Just generate a simple 4367ff17c8bfSgjelinek * zone definition XML file and detach. 4368ff17c8bfSgjelinek */ 4369ff17c8bfSgjelinek 4370ff17c8bfSgjelinek /* Don't detach the zone if anything is still mounted there */ 4371ff17c8bfSgjelinek if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) { 4372ff17c8bfSgjelinek (void) fprintf(stderr, gettext("These file systems are " 4373ff17c8bfSgjelinek "mounted on subdirectories of %s.\n"), zonepath); 4374ff17c8bfSgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 4375ff17c8bfSgjelinek err = ZONE_SUBPROC_NOTCOMPLETE; 4376ff17c8bfSgjelinek goto done; 4377ff17c8bfSgjelinek } 4378ff17c8bfSgjelinek 4379ff17c8bfSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 4380ff17c8bfSgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 4381ff17c8bfSgjelinek err = ZONE_SUBPROC_NOTCOMPLETE; 4382ff17c8bfSgjelinek goto done; 4383ff17c8bfSgjelinek } 4384ff17c8bfSgjelinek 4385ff17c8bfSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 4386ee519a1fSgjelinek errno = err; 4387ff17c8bfSgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 4388ff17c8bfSgjelinek 438971443f5aS } else if ((err = zonecfg_detach_save(handle, 4390ff17c8bfSgjelinek (execute ? 0 : ZONE_DRY_RUN))) != Z_OK) { 4391ff17c8bfSgjelinek errno = err; 4392ff17c8bfSgjelinek zperror(gettext("saving the detach manifest failed"), 4393ee519a1fSgjelinek B_TRUE); 4394ee519a1fSgjelinek } 4395ee519a1fSgjelinek 4396ff17c8bfSgjelinek zonecfg_fini_handle(handle); 439771443f5aS if (err != Z_OK) 439871443f5aS goto done; 4399ee519a1fSgjelinek } 4400ee519a1fSgjelinek 44018cd327d5Sgjelinek /* 44028cd327d5Sgjelinek * Set the zone state back to configured unless we are running with the 44038cd327d5Sgjelinek * no-execute option. 44048cd327d5Sgjelinek */ 44058cd327d5Sgjelinek if (execute && (err = zone_set_state(target_zone, 44068cd327d5Sgjelinek ZONE_STATE_CONFIGURED)) != Z_OK) { 4407ee519a1fSgjelinek errno = err; 4408ee519a1fSgjelinek zperror(gettext("could not reset state"), B_TRUE); 4409ee519a1fSgjelinek } 4410ee519a1fSgjelinek 4411ee519a1fSgjelinek done: 441271443f5aS if (execute) { 441371443f5aS assert(lockfd >= 0); 4414ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 441571443f5aS lockfd = -1; 441671443f5aS } 4417ee519a1fSgjelinek 441871443f5aS assert(lockfd == -1); 4419ee519a1fSgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 4420ee519a1fSgjelinek } 4421ee519a1fSgjelinek 4422ee519a1fSgjelinek /* 4423ff17c8bfSgjelinek * Determine the brand when doing a dry-run attach. The zone does not have to 4424ff17c8bfSgjelinek * exist, so we have to read the incoming manifest to determine the zone's 4425ff17c8bfSgjelinek * brand. 4426ff17c8bfSgjelinek * 4427ff17c8bfSgjelinek * Because the manifest has to be processed twice; once to determine the brand 4428ff17c8bfSgjelinek * and once to do the brand-specific attach logic, we always read it into a tmp 4429ff17c8bfSgjelinek * file. This handles the manifest coming from stdin or a regular file. The 4430ff17c8bfSgjelinek * tmpname parameter returns the name of the temporary file that the manifest 4431ff17c8bfSgjelinek * was read into. 4432ee519a1fSgjelinek */ 4433ee519a1fSgjelinek static int 4434ff17c8bfSgjelinek dryrun_get_brand(char *manifest_path, char *tmpname, int size) 44358cd327d5Sgjelinek { 44368cd327d5Sgjelinek int fd; 44378cd327d5Sgjelinek int err; 4438ff17c8bfSgjelinek int res = Z_OK; 44398cd327d5Sgjelinek zone_dochandle_t local_handle; 44408cd327d5Sgjelinek zone_dochandle_t rem_handle = NULL; 4441ff17c8bfSgjelinek int len; 4442ff17c8bfSgjelinek int ofd; 4443ff17c8bfSgjelinek char buf[512]; 44448cd327d5Sgjelinek 44458cd327d5Sgjelinek if (strcmp(manifest_path, "-") == 0) { 4446ff17c8bfSgjelinek fd = STDIN_FILENO; 4447ff17c8bfSgjelinek } else { 4448ff17c8bfSgjelinek if ((fd = open(manifest_path, O_RDONLY)) < 0) { 4449ff17c8bfSgjelinek if (getcwd(buf, sizeof (buf)) == NULL) 4450ff17c8bfSgjelinek (void) strlcpy(buf, "/", sizeof (buf)); 4451ff17c8bfSgjelinek zerror(gettext("could not open manifest path %s%s: %s"), 4452ff17c8bfSgjelinek (*manifest_path == '/' ? "" : buf), manifest_path, 4453ff17c8bfSgjelinek strerror(errno)); 4454ff17c8bfSgjelinek return (Z_ERR); 4455ff17c8bfSgjelinek } 4456ff17c8bfSgjelinek } 4457ff17c8bfSgjelinek 4458ff17c8bfSgjelinek (void) snprintf(tmpname, size, "/var/run/zone.%d", getpid()); 4459ff17c8bfSgjelinek 4460ff17c8bfSgjelinek if ((ofd = open(tmpname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) { 4461ff17c8bfSgjelinek zperror(gettext("could not save manifest"), B_FALSE); 4462ff17c8bfSgjelinek (void) close(fd); 4463ff17c8bfSgjelinek return (Z_ERR); 4464ff17c8bfSgjelinek } 4465ff17c8bfSgjelinek 4466ff17c8bfSgjelinek while ((len = read(fd, buf, sizeof (buf))) > 0) { 4467ff17c8bfSgjelinek if (write(ofd, buf, len) == -1) { 4468ff17c8bfSgjelinek zperror(gettext("could not save manifest"), B_FALSE); 4469ff17c8bfSgjelinek (void) close(ofd); 4470ff17c8bfSgjelinek (void) close(fd); 4471ff17c8bfSgjelinek return (Z_ERR); 4472ff17c8bfSgjelinek } 4473ff17c8bfSgjelinek } 4474ff17c8bfSgjelinek 4475ff17c8bfSgjelinek if (close(ofd) != 0) { 4476ff17c8bfSgjelinek zperror(gettext("could not save manifest"), B_FALSE); 4477ff17c8bfSgjelinek (void) close(fd); 4478ff17c8bfSgjelinek return (Z_ERR); 4479ff17c8bfSgjelinek } 4480ff17c8bfSgjelinek 4481ff17c8bfSgjelinek (void) close(fd); 4482ff17c8bfSgjelinek 4483ff17c8bfSgjelinek if ((fd = open(tmpname, O_RDONLY)) < 0) { 44848cd327d5Sgjelinek zperror(gettext("could not open manifest path"), B_FALSE); 44858cd327d5Sgjelinek return (Z_ERR); 44868cd327d5Sgjelinek } 44878cd327d5Sgjelinek 44888cd327d5Sgjelinek if ((local_handle = zonecfg_init_handle()) == NULL) { 44898cd327d5Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 44908cd327d5Sgjelinek res = Z_ERR; 44918cd327d5Sgjelinek goto done; 44928cd327d5Sgjelinek } 44938cd327d5Sgjelinek 44948cd327d5Sgjelinek if ((rem_handle = zonecfg_init_handle()) == NULL) { 44958cd327d5Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 44968cd327d5Sgjelinek res = Z_ERR; 44978cd327d5Sgjelinek goto done; 44988cd327d5Sgjelinek } 44998cd327d5Sgjelinek 45008cd327d5Sgjelinek if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle)) 45018cd327d5Sgjelinek != Z_OK) { 45028cd327d5Sgjelinek res = Z_ERR; 4503d9e728a2Sgjelinek 4504d9e728a2Sgjelinek if (err == Z_INVALID_DOCUMENT) { 4505d9e728a2Sgjelinek struct stat st; 4506d9e728a2Sgjelinek char buf[6]; 4507d9e728a2Sgjelinek 4508d9e728a2Sgjelinek if (strcmp(manifest_path, "-") == 0) { 4509d9e728a2Sgjelinek zerror(gettext("Input is not a valid XML " 4510d9e728a2Sgjelinek "file")); 4511d9e728a2Sgjelinek goto done; 4512d9e728a2Sgjelinek } 4513d9e728a2Sgjelinek 4514d9e728a2Sgjelinek if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) { 4515d9e728a2Sgjelinek zerror(gettext("%s is not an XML file"), 4516d9e728a2Sgjelinek manifest_path); 4517d9e728a2Sgjelinek goto done; 4518d9e728a2Sgjelinek } 4519d9e728a2Sgjelinek 4520d9e728a2Sgjelinek bzero(buf, sizeof (buf)); 4521d9e728a2Sgjelinek (void) lseek(fd, 0L, SEEK_SET); 4522d9e728a2Sgjelinek if (read(fd, buf, sizeof (buf) - 1) < 0 || 4523d9e728a2Sgjelinek strncmp(buf, "<?xml", 5) != 0) 4524d9e728a2Sgjelinek zerror(gettext("%s is not an XML file"), 4525d9e728a2Sgjelinek manifest_path); 4526d9e728a2Sgjelinek else 4527d9e728a2Sgjelinek zerror(gettext("Cannot attach to an earlier " 4528d9e728a2Sgjelinek "release of the operating system")); 4529d9e728a2Sgjelinek } else { 4530d9e728a2Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4531d9e728a2Sgjelinek } 45328cd327d5Sgjelinek goto done; 45338cd327d5Sgjelinek } 45348cd327d5Sgjelinek 4535ff17c8bfSgjelinek /* Retrieve remote handle brand type. */ 4536e2482d1aSgjelinek if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand)) 4537e2482d1aSgjelinek != Z_OK) { 4538e2482d1aSgjelinek zerror(gettext("missing or invalid brand")); 4539e2482d1aSgjelinek exit(Z_ERR); 4540e2482d1aSgjelinek } 45418cd327d5Sgjelinek 45428cd327d5Sgjelinek done: 45438cd327d5Sgjelinek zonecfg_fini_handle(local_handle); 45448cd327d5Sgjelinek zonecfg_fini_handle(rem_handle); 4545ff17c8bfSgjelinek (void) close(fd); 45468cd327d5Sgjelinek 45478cd327d5Sgjelinek return ((res == Z_OK) ? Z_OK : Z_ERR); 45488cd327d5Sgjelinek } 45498cd327d5Sgjelinek 45506cfd72c6Sgjelinek /* ARGSUSED */ 4551ee519a1fSgjelinek static int 4552ee519a1fSgjelinek attach_func(int argc, char *argv[]) 4553ee519a1fSgjelinek { 455471443f5aS int lockfd = -1; 4555ee519a1fSgjelinek int err, arg; 4556ee519a1fSgjelinek boolean_t force = B_FALSE; 4557ee519a1fSgjelinek zone_dochandle_t handle; 4558ee519a1fSgjelinek char zonepath[MAXPATHLEN]; 455937774979Sgjelinek char cmdbuf[MAXPATHLEN]; 4560ff17c8bfSgjelinek char postcmdbuf[MAXPATHLEN]; 45618cd327d5Sgjelinek boolean_t execute = B_TRUE; 4562ff17c8bfSgjelinek boolean_t brand_help = B_FALSE; 45638cd327d5Sgjelinek char *manifest_path; 4564ff17c8bfSgjelinek char tmpmanifest[80]; 4565ff17c8bfSgjelinek int manifest_pos; 456637774979Sgjelinek brand_handle_t bh = NULL; 4567ff17c8bfSgjelinek int status; 4568edfa49ffS int last_index = 0; 4569edfa49ffS int offset; 4570edfa49ffS char *up; 4571edfa49ffS boolean_t forced_update = B_FALSE; 4572ee519a1fSgjelinek 4573ee519a1fSgjelinek if (zonecfg_in_alt_root()) { 4574ee519a1fSgjelinek zerror(gettext("cannot attach zone in alternate root")); 4575ee519a1fSgjelinek return (Z_ERR); 4576ee519a1fSgjelinek } 4577ee519a1fSgjelinek 4578ff17c8bfSgjelinek /* Check the argv string for args we handle internally */ 4579ee519a1fSgjelinek optind = 0; 4580ff17c8bfSgjelinek opterr = 0; 4581edfa49ffS while ((arg = getopt(argc, argv, "?Fn:U")) != EOF) { 4582ee519a1fSgjelinek switch (arg) { 4583ee519a1fSgjelinek case '?': 4584ff17c8bfSgjelinek if (optopt == '?') { 4585ee519a1fSgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 4586ff17c8bfSgjelinek brand_help = B_TRUE; 4587ff17c8bfSgjelinek } 4588ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 4589ff17c8bfSgjelinek break; 4590ee519a1fSgjelinek case 'F': 4591ee519a1fSgjelinek force = B_TRUE; 4592ee519a1fSgjelinek break; 45938cd327d5Sgjelinek case 'n': 45948cd327d5Sgjelinek execute = B_FALSE; 45958cd327d5Sgjelinek manifest_path = optarg; 4596ff17c8bfSgjelinek manifest_pos = optind - 1; 45976cfd72c6Sgjelinek break; 4598edfa49ffS case 'U': 4599edfa49ffS /* 4600edfa49ffS * Undocumented 'force update' option for p2v update on 4601edfa49ffS * attach when zone is in the incomplete state. Change 4602edfa49ffS * the option back to 'u' and set forced_update flag. 4603edfa49ffS */ 4604edfa49ffS if (optind == last_index) 4605edfa49ffS offset = optind; 4606edfa49ffS else 4607edfa49ffS offset = optind - 1; 4608edfa49ffS if ((up = index(argv[offset], 'U')) != NULL) 4609edfa49ffS *up = 'u'; 4610edfa49ffS forced_update = B_TRUE; 4611edfa49ffS break; 4612ee519a1fSgjelinek default: 4613ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 4614ff17c8bfSgjelinek break; 4615ee519a1fSgjelinek } 4616edfa49ffS last_index = optind; 4617ee519a1fSgjelinek } 46188cd327d5Sgjelinek 4619ff17c8bfSgjelinek if (brand_help) { 4620ff17c8bfSgjelinek force = B_FALSE; 4621ff17c8bfSgjelinek execute = B_TRUE; 4622ff17c8bfSgjelinek } 4623ff17c8bfSgjelinek 4624ff17c8bfSgjelinek /* dry-run and force flags are mutually exclusive */ 4625ff17c8bfSgjelinek if (!execute && force) { 4626ff17c8bfSgjelinek zerror(gettext("-F and -n flags are mutually exclusive")); 46276cfd72c6Sgjelinek return (Z_ERR); 46286cfd72c6Sgjelinek } 46296cfd72c6Sgjelinek 46308cd327d5Sgjelinek /* 4631ff17c8bfSgjelinek * If the no-execute option was specified, we don't do validation and 4632ff17c8bfSgjelinek * need to figure out the brand, since there is no zone required to be 46338cd327d5Sgjelinek * configured for this option. 46348cd327d5Sgjelinek */ 4635ff17c8bfSgjelinek if (execute) { 4636ff17c8bfSgjelinek if (!brand_help) { 4637ff17c8bfSgjelinek if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, 4638edfa49ffS B_TRUE, forced_update) != Z_OK) 4639ee519a1fSgjelinek return (Z_ERR); 4640ce28b40eSzt129084 if (verify_details(CMD_ATTACH, argv) != Z_OK) 4641ee519a1fSgjelinek return (Z_ERR); 4642ff17c8bfSgjelinek } 4643ee519a1fSgjelinek 4644ff17c8bfSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, 4645ff17c8bfSgjelinek sizeof (zonepath))) != Z_OK) { 4646ee519a1fSgjelinek errno = err; 4647ff17c8bfSgjelinek zperror2(target_zone, 4648ff17c8bfSgjelinek gettext("could not get zone path")); 4649ee519a1fSgjelinek return (Z_ERR); 4650ee519a1fSgjelinek } 4651ff17c8bfSgjelinek } else { 4652ff17c8bfSgjelinek if (dryrun_get_brand(manifest_path, tmpmanifest, 4653ff17c8bfSgjelinek sizeof (tmpmanifest)) != Z_OK) 4654ff17c8bfSgjelinek return (Z_ERR); 4655ff17c8bfSgjelinek 4656ff17c8bfSgjelinek argv[manifest_pos] = tmpmanifest; 4657ff17c8bfSgjelinek target_zone = "-"; 4658ff17c8bfSgjelinek (void) strlcpy(zonepath, "-", sizeof (zonepath)); 4659ff17c8bfSgjelinek 4660ff17c8bfSgjelinek /* Run the brand's verify_adm hook. */ 4661ff17c8bfSgjelinek if (verify_brand(NULL, CMD_ATTACH, argv) != Z_OK) 4662ff17c8bfSgjelinek return (Z_ERR); 4663ff17c8bfSgjelinek } 4664ff17c8bfSgjelinek 4665ff17c8bfSgjelinek /* 4666ff17c8bfSgjelinek * Fetch the attach and postattach hooks from the brand configuration. 4667ff17c8bfSgjelinek */ 466837774979Sgjelinek if ((bh = brand_open(target_brand)) == NULL) { 466937774979Sgjelinek zerror(gettext("missing or invalid brand")); 467037774979Sgjelinek return (Z_ERR); 467137774979Sgjelinek } 467237774979Sgjelinek 4673ff17c8bfSgjelinek if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_attach, target_zone, 4674ff17c8bfSgjelinek zonepath) != Z_OK) { 4675ff17c8bfSgjelinek zerror("invalid brand configuration: missing attach resource"); 4676ff17c8bfSgjelinek brand_close(bh); 4677ff17c8bfSgjelinek return (Z_ERR); 4678ff17c8bfSgjelinek } 4679ff17c8bfSgjelinek 4680ff17c8bfSgjelinek if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postattach, 4681ff17c8bfSgjelinek target_zone, zonepath) != Z_OK) { 468237774979Sgjelinek zerror("invalid brand configuration: missing postattach " 468337774979Sgjelinek "resource"); 468437774979Sgjelinek brand_close(bh); 468537774979Sgjelinek return (Z_ERR); 468637774979Sgjelinek } 468737774979Sgjelinek brand_close(bh); 468837774979Sgjelinek 4689ff17c8bfSgjelinek /* Append all options to attach hook. */ 4690ff17c8bfSgjelinek if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) 469137774979Sgjelinek return (Z_ERR); 469237774979Sgjelinek 4693ff17c8bfSgjelinek /* Append all options to postattach hook. */ 4694ff17c8bfSgjelinek if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK) 4695ff17c8bfSgjelinek return (Z_ERR); 4696ff17c8bfSgjelinek 4697ff17c8bfSgjelinek if (execute && !brand_help) { 4698ff17c8bfSgjelinek if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) { 4699ff17c8bfSgjelinek zerror(gettext("another %s may have an operation in " 4700ff17c8bfSgjelinek "progress."), "zoneadm"); 4701ee519a1fSgjelinek return (Z_ERR); 4702ee519a1fSgjelinek } 4703ff17c8bfSgjelinek } 4704ee519a1fSgjelinek 470571443f5aS if (!force) { 470671443f5aS /* 470771443f5aS * Not a force-attach, so we need to actually do the work. 470871443f5aS */ 4709ff17c8bfSgjelinek if (cmdbuf[0] != '\0') { 4710ff17c8bfSgjelinek /* Run the attach hook */ 4711edfa49ffS status = do_subproc(cmdbuf); 471271443f5aS if ((status = subproc_status(gettext("brand-specific " 471371443f5aS "attach"), status, B_FALSE)) != ZONE_SUBPROC_OK) { 4714ff17c8bfSgjelinek if (status == ZONE_SUBPROC_USAGE && !brand_help) 4715ff17c8bfSgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 4716ff17c8bfSgjelinek 471771443f5aS if (execute && !brand_help) { 4718edfa49ffS assert(zonecfg_lock_file_held(&lockfd)); 471971443f5aS zonecfg_release_lock_file(target_zone, 472071443f5aS lockfd); 472171443f5aS lockfd = -1; 4722ee519a1fSgjelinek } 4723ee519a1fSgjelinek 472471443f5aS assert(lockfd == -1); 472571443f5aS return (Z_ERR); 472671443f5aS } 4727ee519a1fSgjelinek } 4728ee519a1fSgjelinek 47299acbbeafSnn35248 /* 4730ff17c8bfSgjelinek * Else run the built-in attach support. 4731ff17c8bfSgjelinek * This is a no-op since there is nothing to validate. 47329acbbeafSnn35248 */ 4733ff17c8bfSgjelinek 4734ff17c8bfSgjelinek /* If dry-run or help, then we're done. */ 4735ff17c8bfSgjelinek if (!execute || brand_help) { 4736ff17c8bfSgjelinek if (!execute) 4737ff17c8bfSgjelinek (void) unlink(tmpmanifest); 473871443f5aS assert(lockfd == -1); 4739ff17c8bfSgjelinek return (Z_OK); 47409acbbeafSnn35248 } 474171443f5aS } 47429acbbeafSnn35248 4743ca733e25S /* Now we can validate that the zonepath exists. */ 4744ca733e25S if (validate_zonepath(zonepath, CMD_ATTACH) != Z_OK) { 4745ca733e25S (void) fprintf(stderr, gettext("could not verify zonepath %s " 4746ca733e25S "because of the above errors.\n"), zonepath); 4747ca733e25S 4748ca733e25S assert(zonecfg_lock_file_held(&lockfd)); 4749ca733e25S zonecfg_release_lock_file(target_zone, lockfd); 4750ca733e25S return (Z_ERR); 4751ca733e25S } 4752ca733e25S 475371443f5aS if ((handle = zonecfg_init_handle()) == NULL) { 475471443f5aS zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 475571443f5aS err = Z_ERR; 475671443f5aS } else if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 475771443f5aS errno = err; 475871443f5aS zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 475971443f5aS zonecfg_fini_handle(handle); 476071443f5aS } else { 4761ee519a1fSgjelinek zonecfg_rm_detached(handle, force); 476271443f5aS zonecfg_fini_handle(handle); 476371443f5aS } 4764ee519a1fSgjelinek 476571443f5aS if (err == Z_OK && 476671443f5aS (err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 4767ee519a1fSgjelinek errno = err; 4768ee519a1fSgjelinek zperror(gettext("could not reset state"), B_TRUE); 4769ee519a1fSgjelinek } 4770ee519a1fSgjelinek 4771edfa49ffS assert(zonecfg_lock_file_held(&lockfd)); 4772ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 477371443f5aS lockfd = -1; 4774ee519a1fSgjelinek 477537774979Sgjelinek /* If we have a brand postattach hook, run it. */ 4776ff17c8bfSgjelinek if (err == Z_OK && !force && postcmdbuf[0] != '\0') { 4777ff17c8bfSgjelinek status = do_subproc(postcmdbuf); 477837774979Sgjelinek if (subproc_status(gettext("brand-specific postattach"), 477937774979Sgjelinek status, B_FALSE) != ZONE_SUBPROC_OK) { 478037774979Sgjelinek if ((err = zone_set_state(target_zone, 478137774979Sgjelinek ZONE_STATE_CONFIGURED)) != Z_OK) { 478237774979Sgjelinek errno = err; 478337774979Sgjelinek zperror(gettext("could not reset state"), 478437774979Sgjelinek B_TRUE); 478537774979Sgjelinek } 478637774979Sgjelinek } 478737774979Sgjelinek } 478837774979Sgjelinek 478971443f5aS assert(lockfd == -1); 4790ee519a1fSgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 4791ee519a1fSgjelinek } 4792ee519a1fSgjelinek 4793865e09a4Sgjelinek /* 47947c478bd9Sstevel@tonic-gate * On input, TRUE => yes, FALSE => no. 47957c478bd9Sstevel@tonic-gate * On return, TRUE => 1, FALSE => 0, could not ask => -1. 47967c478bd9Sstevel@tonic-gate */ 47977c478bd9Sstevel@tonic-gate 47987c478bd9Sstevel@tonic-gate static int 47997c478bd9Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question) 48007c478bd9Sstevel@tonic-gate { 48017c478bd9Sstevel@tonic-gate char line[64]; /* should be large enough to answer yes or no */ 48027c478bd9Sstevel@tonic-gate 48037c478bd9Sstevel@tonic-gate if (!isatty(STDIN_FILENO)) 48047c478bd9Sstevel@tonic-gate return (-1); 48057c478bd9Sstevel@tonic-gate for (;;) { 48067c478bd9Sstevel@tonic-gate (void) printf("%s (%s)? ", question, 48077c478bd9Sstevel@tonic-gate default_answer ? "[y]/n" : "y/[n]"); 48087c478bd9Sstevel@tonic-gate if (fgets(line, sizeof (line), stdin) == NULL || 48097c478bd9Sstevel@tonic-gate line[0] == '\n') 48107c478bd9Sstevel@tonic-gate return (default_answer ? 1 : 0); 48117c478bd9Sstevel@tonic-gate if (tolower(line[0]) == 'y') 48127c478bd9Sstevel@tonic-gate return (1); 48137c478bd9Sstevel@tonic-gate if (tolower(line[0]) == 'n') 48147c478bd9Sstevel@tonic-gate return (0); 48157c478bd9Sstevel@tonic-gate } 48167c478bd9Sstevel@tonic-gate } 48177c478bd9Sstevel@tonic-gate 4818ff17c8bfSgjelinek /* ARGSUSED */ 48197c478bd9Sstevel@tonic-gate static int 48207c478bd9Sstevel@tonic-gate uninstall_func(int argc, char *argv[]) 48217c478bd9Sstevel@tonic-gate { 48227c478bd9Sstevel@tonic-gate char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */ 48230b5de56dSgjelinek char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN]; 482437774979Sgjelinek char cmdbuf[MAXPATHLEN]; 4825ff17c8bfSgjelinek char precmdbuf[MAXPATHLEN]; 48267c478bd9Sstevel@tonic-gate boolean_t force = B_FALSE; 48277c478bd9Sstevel@tonic-gate int lockfd, answer; 48287c478bd9Sstevel@tonic-gate int err, arg; 4829ff17c8bfSgjelinek boolean_t brand_help = B_FALSE; 483037774979Sgjelinek brand_handle_t bh = NULL; 4831ff17c8bfSgjelinek int status; 48327c478bd9Sstevel@tonic-gate 4833108322fbScarlsonj if (zonecfg_in_alt_root()) { 4834108322fbScarlsonj zerror(gettext("cannot uninstall zone in alternate root")); 4835108322fbScarlsonj return (Z_ERR); 4836108322fbScarlsonj } 4837108322fbScarlsonj 4838ff17c8bfSgjelinek /* Check the argv string for args we handle internally */ 48397c478bd9Sstevel@tonic-gate optind = 0; 4840ff17c8bfSgjelinek opterr = 0; 48417c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?F")) != EOF) { 48427c478bd9Sstevel@tonic-gate switch (arg) { 48437c478bd9Sstevel@tonic-gate case '?': 4844ff17c8bfSgjelinek if (optopt == '?') { 48457c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 4846ff17c8bfSgjelinek brand_help = B_TRUE; 4847ff17c8bfSgjelinek } 4848ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 4849ff17c8bfSgjelinek break; 48507c478bd9Sstevel@tonic-gate case 'F': 48517c478bd9Sstevel@tonic-gate force = B_TRUE; 48527c478bd9Sstevel@tonic-gate break; 48537c478bd9Sstevel@tonic-gate default: 4854ff17c8bfSgjelinek /* Ignore unknown options - may be brand specific. */ 4855ff17c8bfSgjelinek break; 48567c478bd9Sstevel@tonic-gate } 48577c478bd9Sstevel@tonic-gate } 48587c478bd9Sstevel@tonic-gate 4859ff17c8bfSgjelinek if (!brand_help) { 4860ff17c8bfSgjelinek if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE, 4861ff17c8bfSgjelinek B_FALSE) != Z_OK) 48627c478bd9Sstevel@tonic-gate return (Z_ERR); 48637c478bd9Sstevel@tonic-gate 4864ce28b40eSzt129084 /* 4865ce28b40eSzt129084 * Invoke brand-specific handler. 4866ce28b40eSzt129084 */ 4867ce28b40eSzt129084 if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK) 4868ce28b40eSzt129084 return (Z_ERR); 4869ce28b40eSzt129084 48707c478bd9Sstevel@tonic-gate if (!force) { 48717c478bd9Sstevel@tonic-gate (void) snprintf(line, sizeof (line), 48727c478bd9Sstevel@tonic-gate gettext("Are you sure you want to %s zone %s"), 48737c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL), target_zone); 48747c478bd9Sstevel@tonic-gate if ((answer = ask_yesno(B_FALSE, line)) == 0) { 48757c478bd9Sstevel@tonic-gate return (Z_OK); 48767c478bd9Sstevel@tonic-gate } else if (answer == -1) { 48777c478bd9Sstevel@tonic-gate zerror(gettext("Input not from terminal and -F " 48787c478bd9Sstevel@tonic-gate "not specified: %s not done."), 48797c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 48807c478bd9Sstevel@tonic-gate return (Z_ERR); 48817c478bd9Sstevel@tonic-gate } 48827c478bd9Sstevel@tonic-gate } 4883ff17c8bfSgjelinek } 48847c478bd9Sstevel@tonic-gate 48850b5de56dSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, 48860b5de56dSgjelinek sizeof (zonepath))) != Z_OK) { 48877c478bd9Sstevel@tonic-gate errno = err; 48887c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 48897c478bd9Sstevel@tonic-gate return (Z_ERR); 48907c478bd9Sstevel@tonic-gate } 4891ff17c8bfSgjelinek 4892ff17c8bfSgjelinek /* 4893ff17c8bfSgjelinek * Fetch the uninstall and preuninstall hooks from the brand 4894ff17c8bfSgjelinek * configuration. 4895ff17c8bfSgjelinek */ 4896ff17c8bfSgjelinek if ((bh = brand_open(target_brand)) == NULL) { 4897ff17c8bfSgjelinek zerror(gettext("missing or invalid brand")); 4898ff17c8bfSgjelinek return (Z_ERR); 4899ff17c8bfSgjelinek } 4900ff17c8bfSgjelinek 4901ff17c8bfSgjelinek if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_uninstall, 4902ff17c8bfSgjelinek target_zone, zonepath) != Z_OK) { 4903ff17c8bfSgjelinek zerror("invalid brand configuration: missing uninstall " 4904ff17c8bfSgjelinek "resource"); 4905ff17c8bfSgjelinek brand_close(bh); 4906ff17c8bfSgjelinek return (Z_ERR); 4907ff17c8bfSgjelinek } 4908ff17c8bfSgjelinek 4909ff17c8bfSgjelinek if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_preuninstall, 4910ff17c8bfSgjelinek target_zone, zonepath) != Z_OK) { 4911ff17c8bfSgjelinek zerror("invalid brand configuration: missing preuninstall " 4912ff17c8bfSgjelinek "resource"); 4913ff17c8bfSgjelinek brand_close(bh); 4914ff17c8bfSgjelinek return (Z_ERR); 4915ff17c8bfSgjelinek } 4916ff17c8bfSgjelinek brand_close(bh); 4917ff17c8bfSgjelinek 4918ff17c8bfSgjelinek /* Append all options to preuninstall hook. */ 4919ff17c8bfSgjelinek if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK) 4920ff17c8bfSgjelinek return (Z_ERR); 4921ff17c8bfSgjelinek 4922ff17c8bfSgjelinek /* Append all options to uninstall hook. */ 4923ff17c8bfSgjelinek if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) 4924ff17c8bfSgjelinek return (Z_ERR); 4925ff17c8bfSgjelinek 4926ff17c8bfSgjelinek if (!brand_help) { 49277c478bd9Sstevel@tonic-gate if ((err = zone_get_rootpath(target_zone, rootpath, 49287c478bd9Sstevel@tonic-gate sizeof (rootpath))) != Z_OK) { 49297c478bd9Sstevel@tonic-gate errno = err; 4930ff17c8bfSgjelinek zperror2(target_zone, gettext("could not get root " 4931ff17c8bfSgjelinek "path")); 49327c478bd9Sstevel@tonic-gate return (Z_ERR); 49337c478bd9Sstevel@tonic-gate } 49347c478bd9Sstevel@tonic-gate 49357c478bd9Sstevel@tonic-gate /* 4936ff17c8bfSgjelinek * If there seems to be a zoneadmd running for this zone, call 4937ff17c8bfSgjelinek * it to tell it that an uninstall is happening; if all goes 4938ff17c8bfSgjelinek * well it will then shut itself down. 49397c478bd9Sstevel@tonic-gate */ 4940ff17c8bfSgjelinek if (zonecfg_ping_zoneadmd(target_zone) == Z_OK) { 49417c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 49427c478bd9Sstevel@tonic-gate zarg.cmd = Z_NOTE_UNINSTALLING; 4943ff17c8bfSgjelinek /* we don't care too much if this fails, just plow on */ 4944ff17c8bfSgjelinek (void) zonecfg_call_zoneadmd(target_zone, &zarg, locale, 4945ff17c8bfSgjelinek B_TRUE); 49467c478bd9Sstevel@tonic-gate } 49477c478bd9Sstevel@tonic-gate 4948ff17c8bfSgjelinek if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) { 4949ff17c8bfSgjelinek zerror(gettext("another %s may have an operation in " 4950ff17c8bfSgjelinek "progress."), "zoneadm"); 49517c478bd9Sstevel@tonic-gate return (Z_ERR); 49527c478bd9Sstevel@tonic-gate } 49537c478bd9Sstevel@tonic-gate 49547c478bd9Sstevel@tonic-gate /* Don't uninstall the zone if anything is mounted there */ 49557c478bd9Sstevel@tonic-gate err = zonecfg_find_mounts(rootpath, NULL, NULL); 49567c478bd9Sstevel@tonic-gate if (err) { 49570b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 49587c478bd9Sstevel@tonic-gate "subdirectories of %s.\n"), rootpath); 49597c478bd9Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, zfm_print, NULL); 4960ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 49617c478bd9Sstevel@tonic-gate return (Z_ERR); 49627c478bd9Sstevel@tonic-gate } 496337774979Sgjelinek } 496437774979Sgjelinek 4965ff17c8bfSgjelinek /* If we have a brand preuninstall hook, run it. */ 4966ff17c8bfSgjelinek if (!brand_help && precmdbuf[0] != '\0') { 496737774979Sgjelinek status = do_subproc(cmdbuf); 496837774979Sgjelinek if (subproc_status(gettext("brand-specific preuninstall"), 496937774979Sgjelinek status, B_FALSE) != ZONE_SUBPROC_OK) { 4970ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 497137774979Sgjelinek return (Z_ERR); 497237774979Sgjelinek } 497337774979Sgjelinek } 497437774979Sgjelinek 4975ff17c8bfSgjelinek if (!brand_help) { 49767c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 49777c478bd9Sstevel@tonic-gate if (err != Z_OK) { 49787c478bd9Sstevel@tonic-gate errno = err; 49797c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 49807c478bd9Sstevel@tonic-gate goto bad; 49817c478bd9Sstevel@tonic-gate } 4982ff17c8bfSgjelinek } 49837c478bd9Sstevel@tonic-gate 4984ff17c8bfSgjelinek /* 4985ff17c8bfSgjelinek * If there is a brand uninstall hook, use it, otherwise use the 4986ff17c8bfSgjelinek * built-in uninstall code. 4987ff17c8bfSgjelinek */ 4988ff17c8bfSgjelinek if (cmdbuf[0] != '\0') { 4989ff17c8bfSgjelinek /* Run the uninstall hook */ 4990c75cc341S status = do_subproc(cmdbuf); 4991ff17c8bfSgjelinek if ((status = subproc_status(gettext("brand-specific " 4992ff17c8bfSgjelinek "uninstall"), status, B_FALSE)) != ZONE_SUBPROC_OK) { 4993ff17c8bfSgjelinek if (status == ZONE_SUBPROC_USAGE && !brand_help) 4994ff17c8bfSgjelinek sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 4995ff17c8bfSgjelinek if (!brand_help) 4996ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 4997ff17c8bfSgjelinek return (Z_ERR); 4998ff17c8bfSgjelinek } 4999ff17c8bfSgjelinek 5000ff17c8bfSgjelinek if (brand_help) 5001ff17c8bfSgjelinek return (Z_OK); 5002ff17c8bfSgjelinek } else { 5003ff17c8bfSgjelinek /* If just help, we're done since there is no brand help. */ 5004ff17c8bfSgjelinek if (brand_help) 5005ff17c8bfSgjelinek return (Z_OK); 5006ff17c8bfSgjelinek 5007ff17c8bfSgjelinek /* Run the built-in uninstall support. */ 50080b5de56dSgjelinek if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 50090b5de56dSgjelinek errno = err; 5010ff17c8bfSgjelinek zperror2(target_zone, gettext("cleaning up zonepath " 5011ff17c8bfSgjelinek "failed")); 50127c478bd9Sstevel@tonic-gate goto bad; 50130b5de56dSgjelinek } 5014ff17c8bfSgjelinek } 50150b5de56dSgjelinek 50167c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 50177c478bd9Sstevel@tonic-gate if (err != Z_OK) { 50187c478bd9Sstevel@tonic-gate errno = err; 50197c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not reset state")); 50207c478bd9Sstevel@tonic-gate } 50217c478bd9Sstevel@tonic-gate bad: 5022ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 50237c478bd9Sstevel@tonic-gate return (err); 50247c478bd9Sstevel@tonic-gate } 50257c478bd9Sstevel@tonic-gate 5026108322fbScarlsonj /* ARGSUSED */ 5027108322fbScarlsonj static int 5028108322fbScarlsonj mount_func(int argc, char *argv[]) 5029108322fbScarlsonj { 5030108322fbScarlsonj zone_cmd_arg_t zarg; 50319acbbeafSnn35248 boolean_t force = B_FALSE; 50329acbbeafSnn35248 int arg; 5033108322fbScarlsonj 50349acbbeafSnn35248 /* 50359acbbeafSnn35248 * The only supported subargument to the "mount" subcommand is 50369acbbeafSnn35248 * "-f", which forces us to mount a zone in the INCOMPLETE state. 50379acbbeafSnn35248 */ 50389acbbeafSnn35248 optind = 0; 50399acbbeafSnn35248 if ((arg = getopt(argc, argv, "f")) != EOF) { 50409acbbeafSnn35248 switch (arg) { 50419acbbeafSnn35248 case 'f': 50429acbbeafSnn35248 force = B_TRUE; 50439acbbeafSnn35248 break; 50449acbbeafSnn35248 default: 5045108322fbScarlsonj return (Z_USAGE); 50469acbbeafSnn35248 } 50479acbbeafSnn35248 } 50489acbbeafSnn35248 if (argc > optind) 50499acbbeafSnn35248 return (Z_USAGE); 50509acbbeafSnn35248 50519acbbeafSnn35248 if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force) 50529acbbeafSnn35248 != Z_OK) 5053108322fbScarlsonj return (Z_ERR); 5054ce28b40eSzt129084 if (verify_details(CMD_MOUNT, argv) != Z_OK) 5055108322fbScarlsonj return (Z_ERR); 5056108322fbScarlsonj 50579acbbeafSnn35248 zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT; 50586cfd72c6Sgjelinek zarg.bootbuf[0] = '\0'; 5059ff17c8bfSgjelinek if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) { 5060108322fbScarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 5061108322fbScarlsonj return (Z_ERR); 5062108322fbScarlsonj } 5063108322fbScarlsonj return (Z_OK); 5064108322fbScarlsonj } 5065108322fbScarlsonj 5066108322fbScarlsonj /* ARGSUSED */ 5067108322fbScarlsonj static int 5068108322fbScarlsonj unmount_func(int argc, char *argv[]) 5069108322fbScarlsonj { 5070108322fbScarlsonj zone_cmd_arg_t zarg; 5071108322fbScarlsonj 5072108322fbScarlsonj if (argc > 0) 5073108322fbScarlsonj return (Z_USAGE); 50749acbbeafSnn35248 if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE) 50759acbbeafSnn35248 != Z_OK) 5076108322fbScarlsonj return (Z_ERR); 5077108322fbScarlsonj 5078108322fbScarlsonj zarg.cmd = Z_UNMOUNT; 5079ff17c8bfSgjelinek if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) { 5080108322fbScarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 5081108322fbScarlsonj return (Z_ERR); 5082108322fbScarlsonj } 5083108322fbScarlsonj return (Z_OK); 5084108322fbScarlsonj } 5085108322fbScarlsonj 50867c478bd9Sstevel@tonic-gate static int 5087555afedfScarlsonj mark_func(int argc, char *argv[]) 5088555afedfScarlsonj { 5089555afedfScarlsonj int err, lockfd; 5090edfa49ffS int arg; 5091edfa49ffS boolean_t force = B_FALSE; 5092edfa49ffS int state; 5093555afedfScarlsonj 5094edfa49ffS optind = 0; 5095edfa49ffS opterr = 0; 5096edfa49ffS while ((arg = getopt(argc, argv, "F")) != EOF) { 5097edfa49ffS switch (arg) { 5098edfa49ffS case 'F': 5099edfa49ffS force = B_TRUE; 5100edfa49ffS break; 5101edfa49ffS default: 5102555afedfScarlsonj return (Z_USAGE); 5103edfa49ffS } 5104edfa49ffS } 5105edfa49ffS 5106edfa49ffS if (argc != (optind + 1)) 5107edfa49ffS return (Z_USAGE); 5108edfa49ffS 5109edfa49ffS if (strcmp(argv[optind], "configured") == 0) 5110edfa49ffS state = ZONE_STATE_CONFIGURED; 5111edfa49ffS else if (strcmp(argv[optind], "incomplete") == 0) 5112edfa49ffS state = ZONE_STATE_INCOMPLETE; 5113edfa49ffS else if (strcmp(argv[optind], "installed") == 0) 5114edfa49ffS state = ZONE_STATE_INSTALLED; 5115edfa49ffS else 5116edfa49ffS return (Z_USAGE); 5117edfa49ffS 5118edfa49ffS if (state != ZONE_STATE_INCOMPLETE && !force) 5119edfa49ffS return (Z_USAGE); 5120edfa49ffS 5121edfa49ffS if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_TRUE, B_FALSE) 51229acbbeafSnn35248 != Z_OK) 5123555afedfScarlsonj return (Z_ERR); 5124555afedfScarlsonj 5125ce28b40eSzt129084 /* 5126ce28b40eSzt129084 * Invoke brand-specific handler. 5127ce28b40eSzt129084 */ 5128ce28b40eSzt129084 if (invoke_brand_handler(CMD_MARK, argv) != Z_OK) 5129ce28b40eSzt129084 return (Z_ERR); 5130ce28b40eSzt129084 5131ff17c8bfSgjelinek if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) { 5132555afedfScarlsonj zerror(gettext("another %s may have an operation in progress."), 5133555afedfScarlsonj "zoneadm"); 5134555afedfScarlsonj return (Z_ERR); 5135555afedfScarlsonj } 5136555afedfScarlsonj 5137edfa49ffS err = zone_set_state(target_zone, state); 5138555afedfScarlsonj if (err != Z_OK) { 5139555afedfScarlsonj errno = err; 5140555afedfScarlsonj zperror2(target_zone, gettext("could not set state")); 5141555afedfScarlsonj } 5142ff17c8bfSgjelinek zonecfg_release_lock_file(target_zone, lockfd); 5143555afedfScarlsonj 5144555afedfScarlsonj return (err); 5145555afedfScarlsonj } 5146555afedfScarlsonj 51470209230bSgjelinek /* 51480209230bSgjelinek * Check what scheduling class we're running under and print a warning if 51490209230bSgjelinek * we're not using FSS. 51500209230bSgjelinek */ 51510209230bSgjelinek static int 51520209230bSgjelinek check_sched_fss(zone_dochandle_t handle) 51530209230bSgjelinek { 51540209230bSgjelinek char class_name[PC_CLNMSZ]; 51550209230bSgjelinek 51560209230bSgjelinek if (zonecfg_get_dflt_sched_class(handle, class_name, 51570209230bSgjelinek sizeof (class_name)) != Z_OK) { 51580209230bSgjelinek zerror(gettext("WARNING: unable to determine the zone's " 51590209230bSgjelinek "scheduling class")); 51600209230bSgjelinek } else if (strcmp("FSS", class_name) != 0) { 51610209230bSgjelinek zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n" 51620209230bSgjelinek "FSS is not the default scheduling class for this zone. " 51630209230bSgjelinek "FSS will be\nused for processes in the zone but to get " 51640209230bSgjelinek "the full benefit of FSS,\nit should be the default " 51650209230bSgjelinek "scheduling class. See dispadmin(1M) for\nmore details.")); 51660209230bSgjelinek return (Z_SYSTEM); 51670209230bSgjelinek } 51680209230bSgjelinek 51690209230bSgjelinek return (Z_OK); 51700209230bSgjelinek } 51710209230bSgjelinek 51720209230bSgjelinek static int 51730209230bSgjelinek check_cpu_shares_sched(zone_dochandle_t handle) 51740209230bSgjelinek { 51750209230bSgjelinek int err; 51760209230bSgjelinek int res = Z_OK; 51770209230bSgjelinek struct zone_rctltab rctl; 51780209230bSgjelinek 51790209230bSgjelinek if ((err = zonecfg_setrctlent(handle)) != Z_OK) { 51800209230bSgjelinek errno = err; 51810209230bSgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 51820209230bSgjelinek return (err); 51830209230bSgjelinek } 51840209230bSgjelinek 51850209230bSgjelinek while (zonecfg_getrctlent(handle, &rctl) == Z_OK) { 51860209230bSgjelinek if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) { 51870209230bSgjelinek if (check_sched_fss(handle) != Z_OK) 51880209230bSgjelinek res = Z_SYSTEM; 51890209230bSgjelinek break; 51900209230bSgjelinek } 51910209230bSgjelinek } 51920209230bSgjelinek 51930209230bSgjelinek (void) zonecfg_endrctlent(handle); 51940209230bSgjelinek 51950209230bSgjelinek return (res); 51960209230bSgjelinek } 51970209230bSgjelinek 51980209230bSgjelinek /* 51997ef01d19Sgjelinek * Check if there is a mix of processes running in different pools within the 52007ef01d19Sgjelinek * zone. This is currently only going to be called for the global zone from 52017ef01d19Sgjelinek * apply_func but that could be generalized in the future. 52027ef01d19Sgjelinek */ 52037ef01d19Sgjelinek static boolean_t 52047ef01d19Sgjelinek mixed_pools(zoneid_t zoneid) 52057ef01d19Sgjelinek { 52067ef01d19Sgjelinek DIR *dirp; 52077ef01d19Sgjelinek dirent_t *dent; 52087ef01d19Sgjelinek boolean_t mixed = B_FALSE; 52097ef01d19Sgjelinek boolean_t poolid_set = B_FALSE; 52107ef01d19Sgjelinek poolid_t last_poolid = 0; 52117ef01d19Sgjelinek 52127ef01d19Sgjelinek if ((dirp = opendir("/proc")) == NULL) { 52137ef01d19Sgjelinek zerror(gettext("could not open /proc")); 52147ef01d19Sgjelinek return (B_FALSE); 52157ef01d19Sgjelinek } 52167ef01d19Sgjelinek 52177ef01d19Sgjelinek while ((dent = readdir(dirp)) != NULL) { 52187ef01d19Sgjelinek int procfd; 52197ef01d19Sgjelinek psinfo_t ps; 52207ef01d19Sgjelinek char procpath[MAXPATHLEN]; 52217ef01d19Sgjelinek 52227ef01d19Sgjelinek if (dent->d_name[0] == '.') 52237ef01d19Sgjelinek continue; 52247ef01d19Sgjelinek 52257ef01d19Sgjelinek (void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo", 52267ef01d19Sgjelinek dent->d_name); 52277ef01d19Sgjelinek 52287ef01d19Sgjelinek if ((procfd = open(procpath, O_RDONLY)) == -1) 52297ef01d19Sgjelinek continue; 52307ef01d19Sgjelinek 52317ef01d19Sgjelinek if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) { 52327ef01d19Sgjelinek /* skip processes in other zones and system processes */ 52337ef01d19Sgjelinek if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) { 52347ef01d19Sgjelinek (void) close(procfd); 52357ef01d19Sgjelinek continue; 52367ef01d19Sgjelinek } 52377ef01d19Sgjelinek 52387ef01d19Sgjelinek if (poolid_set) { 52397ef01d19Sgjelinek if (ps.pr_poolid != last_poolid) 52407ef01d19Sgjelinek mixed = B_TRUE; 52417ef01d19Sgjelinek } else { 52427ef01d19Sgjelinek last_poolid = ps.pr_poolid; 52437ef01d19Sgjelinek poolid_set = B_TRUE; 52447ef01d19Sgjelinek } 52457ef01d19Sgjelinek } 52467ef01d19Sgjelinek 52477ef01d19Sgjelinek (void) close(procfd); 52487ef01d19Sgjelinek 52497ef01d19Sgjelinek if (mixed) 52507ef01d19Sgjelinek break; 52517ef01d19Sgjelinek } 52527ef01d19Sgjelinek 52537ef01d19Sgjelinek (void) closedir(dirp); 52547ef01d19Sgjelinek 52557ef01d19Sgjelinek return (mixed); 52567ef01d19Sgjelinek } 52577ef01d19Sgjelinek 52587ef01d19Sgjelinek /* 52597ef01d19Sgjelinek * Check if a persistent or temporary pool is configured for the zone. 52607ef01d19Sgjelinek * This is currently only going to be called for the global zone from 52617ef01d19Sgjelinek * apply_func but that could be generalized in the future. 52627ef01d19Sgjelinek */ 52637ef01d19Sgjelinek static boolean_t 52647ef01d19Sgjelinek pool_configured(zone_dochandle_t handle) 52657ef01d19Sgjelinek { 52667ef01d19Sgjelinek int err1, err2; 52677ef01d19Sgjelinek struct zone_psettab pset_tab; 52687ef01d19Sgjelinek char poolname[MAXPATHLEN]; 52697ef01d19Sgjelinek 52707ef01d19Sgjelinek err1 = zonecfg_lookup_pset(handle, &pset_tab); 52717ef01d19Sgjelinek err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 52727ef01d19Sgjelinek 52737ef01d19Sgjelinek if (err1 == Z_NO_ENTRY && 52747ef01d19Sgjelinek (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0))) 52757ef01d19Sgjelinek return (B_FALSE); 52767ef01d19Sgjelinek 52777ef01d19Sgjelinek return (B_TRUE); 52787ef01d19Sgjelinek } 52797ef01d19Sgjelinek 52807ef01d19Sgjelinek /* 52810209230bSgjelinek * This is an undocumented interface which is currently only used to apply 52820209230bSgjelinek * the global zone resource management settings when the system boots. 52830209230bSgjelinek * This function does not yet properly handle updating a running system so 52840209230bSgjelinek * any projects running in the zone would be trashed if this function 52850209230bSgjelinek * were to run after the zone had booted. It also does not reset any 52860209230bSgjelinek * rctl settings that were removed from zonecfg. There is still work to be 52870209230bSgjelinek * done before we can properly support dynamically updating the resource 52880209230bSgjelinek * management settings for a running zone (global or non-global). Thus, this 52890209230bSgjelinek * functionality is undocumented for now. 52900209230bSgjelinek */ 52910209230bSgjelinek /* ARGSUSED */ 52920209230bSgjelinek static int 52930209230bSgjelinek apply_func(int argc, char *argv[]) 52940209230bSgjelinek { 52950209230bSgjelinek int err; 52960209230bSgjelinek int res = Z_OK; 52970209230bSgjelinek priv_set_t *privset; 52980209230bSgjelinek zoneid_t zoneid; 52990209230bSgjelinek zone_dochandle_t handle; 53000209230bSgjelinek struct zone_mcaptab mcap; 53010209230bSgjelinek char pool_err[128]; 53020209230bSgjelinek 53030209230bSgjelinek zoneid = getzoneid(); 53040209230bSgjelinek 53050209230bSgjelinek if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID || 53060209230bSgjelinek target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0) 53070209230bSgjelinek return (usage(B_FALSE)); 53080209230bSgjelinek 53090209230bSgjelinek if ((privset = priv_allocset()) == NULL) { 53100209230bSgjelinek zerror(gettext("%s failed"), "priv_allocset"); 53110209230bSgjelinek return (Z_ERR); 53120209230bSgjelinek } 53130209230bSgjelinek 53140209230bSgjelinek if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 53150209230bSgjelinek zerror(gettext("%s failed"), "getppriv"); 53160209230bSgjelinek priv_freeset(privset); 53170209230bSgjelinek return (Z_ERR); 53180209230bSgjelinek } 53190209230bSgjelinek 53200209230bSgjelinek if (priv_isfullset(privset) == B_FALSE) { 53210209230bSgjelinek (void) usage(B_FALSE); 53220209230bSgjelinek priv_freeset(privset); 53230209230bSgjelinek return (Z_ERR); 53240209230bSgjelinek } 53250209230bSgjelinek priv_freeset(privset); 53260209230bSgjelinek 53270209230bSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 53280209230bSgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 53290209230bSgjelinek return (Z_ERR); 53300209230bSgjelinek } 53310209230bSgjelinek 53320209230bSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 53330209230bSgjelinek errno = err; 53340209230bSgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 53350209230bSgjelinek zonecfg_fini_handle(handle); 53360209230bSgjelinek return (Z_ERR); 53370209230bSgjelinek } 53380209230bSgjelinek 53390209230bSgjelinek /* specific error msgs are printed within apply_rctls */ 53400209230bSgjelinek if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) { 53410209230bSgjelinek errno = err; 53420209230bSgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 53430209230bSgjelinek res = Z_ERR; 53440209230bSgjelinek } 53450209230bSgjelinek 53460209230bSgjelinek if ((err = check_cpu_shares_sched(handle)) != Z_OK) 53470209230bSgjelinek res = Z_ERR; 53480209230bSgjelinek 53497ef01d19Sgjelinek if (pool_configured(handle)) { 53507ef01d19Sgjelinek if (mixed_pools(zoneid)) { 53517ef01d19Sgjelinek zerror(gettext("Zone is using multiple resource " 53527ef01d19Sgjelinek "pools. The pool\nconfiguration cannot be " 53537ef01d19Sgjelinek "applied without rebooting.")); 53547ef01d19Sgjelinek res = Z_ERR; 53557ef01d19Sgjelinek } else { 53567ef01d19Sgjelinek 53570209230bSgjelinek /* 53587ef01d19Sgjelinek * The next two blocks of code attempt to set up 53597ef01d19Sgjelinek * temporary pools as well as persistent pools. In 53607ef01d19Sgjelinek * both cases we call the functions unconditionally. 53617ef01d19Sgjelinek * Within each funtion the code will check if the zone 53627ef01d19Sgjelinek * is actually configured for a temporary pool or 53637ef01d19Sgjelinek * persistent pool and just return if there is nothing 53647ef01d19Sgjelinek * to do. 53650209230bSgjelinek */ 53667ef01d19Sgjelinek if ((err = zonecfg_bind_tmp_pool(handle, zoneid, 53677ef01d19Sgjelinek pool_err, sizeof (pool_err))) != Z_OK) { 53687ef01d19Sgjelinek if (err == Z_POOL || err == Z_POOL_CREATE || 53697ef01d19Sgjelinek err == Z_POOL_BIND) 53707ef01d19Sgjelinek zerror("%s: %s", zonecfg_strerror(err), 53717ef01d19Sgjelinek pool_err); 53720209230bSgjelinek else 53737ef01d19Sgjelinek zerror(gettext("could not bind zone to " 53747ef01d19Sgjelinek "temporary pool: %s"), 53757ef01d19Sgjelinek zonecfg_strerror(err)); 53760209230bSgjelinek res = Z_ERR; 53770209230bSgjelinek } 53780209230bSgjelinek 53790209230bSgjelinek if ((err = zonecfg_bind_pool(handle, zoneid, pool_err, 53800209230bSgjelinek sizeof (pool_err))) != Z_OK) { 53810209230bSgjelinek if (err == Z_POOL || err == Z_POOL_BIND) 53827ef01d19Sgjelinek zerror("%s: %s", zonecfg_strerror(err), 53837ef01d19Sgjelinek pool_err); 53840209230bSgjelinek else 53850209230bSgjelinek zerror("%s", zonecfg_strerror(err)); 53860209230bSgjelinek } 53877ef01d19Sgjelinek } 53887ef01d19Sgjelinek } 53890209230bSgjelinek 53900209230bSgjelinek /* 53910209230bSgjelinek * If a memory cap is configured, set the cap in the kernel using 53920209230bSgjelinek * zone_setattr() and make sure the rcapd SMF service is enabled. 53930209230bSgjelinek */ 53940209230bSgjelinek if (zonecfg_getmcapent(handle, &mcap) == Z_OK) { 53950209230bSgjelinek uint64_t num; 53960209230bSgjelinek char smf_err[128]; 53970209230bSgjelinek 53980209230bSgjelinek num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10); 53990209230bSgjelinek if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) { 54000209230bSgjelinek zerror(gettext("could not set zone memory cap")); 54010209230bSgjelinek res = Z_ERR; 54020209230bSgjelinek } 54030209230bSgjelinek 54040209230bSgjelinek if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) { 54050209230bSgjelinek zerror(gettext("enabling system/rcap service failed: " 54060209230bSgjelinek "%s"), smf_err); 54070209230bSgjelinek res = Z_ERR; 54080209230bSgjelinek } 54090209230bSgjelinek } 54100209230bSgjelinek 54110209230bSgjelinek zonecfg_fini_handle(handle); 54120209230bSgjelinek 54130209230bSgjelinek return (res); 54140209230bSgjelinek } 54150209230bSgjelinek 5416fbbfbc6eSjv227347 /* 5417fbbfbc6eSjv227347 * This is an undocumented interface that is invoked by the zones SMF service 5418fbbfbc6eSjv227347 * for installed zones that won't automatically boot. 5419fbbfbc6eSjv227347 */ 5420fbbfbc6eSjv227347 /* ARGSUSED */ 5421fbbfbc6eSjv227347 static int 5422fbbfbc6eSjv227347 sysboot_func(int argc, char *argv[]) 5423fbbfbc6eSjv227347 { 5424fbbfbc6eSjv227347 int err; 5425fbbfbc6eSjv227347 zone_dochandle_t zone_handle; 5426fbbfbc6eSjv227347 brand_handle_t brand_handle; 5427fbbfbc6eSjv227347 char cmdbuf[MAXPATHLEN]; 5428fbbfbc6eSjv227347 char zonepath[MAXPATHLEN]; 5429fbbfbc6eSjv227347 5430fbbfbc6eSjv227347 /* 5431fbbfbc6eSjv227347 * This subcommand can only be executed in the global zone on non-global 5432fbbfbc6eSjv227347 * zones. 5433fbbfbc6eSjv227347 */ 5434fbbfbc6eSjv227347 if (zonecfg_in_alt_root()) 5435fbbfbc6eSjv227347 return (usage(B_FALSE)); 5436fbbfbc6eSjv227347 if (sanity_check(target_zone, CMD_SYSBOOT, B_FALSE, B_TRUE, B_FALSE) != 5437fbbfbc6eSjv227347 Z_OK) 5438fbbfbc6eSjv227347 return (Z_ERR); 5439fbbfbc6eSjv227347 5440fbbfbc6eSjv227347 /* 5441fbbfbc6eSjv227347 * Fetch the sysboot hook from the target zone's brand. 5442fbbfbc6eSjv227347 */ 5443fbbfbc6eSjv227347 if ((zone_handle = zonecfg_init_handle()) == NULL) { 5444fbbfbc6eSjv227347 zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE); 5445fbbfbc6eSjv227347 return (Z_ERR); 5446fbbfbc6eSjv227347 } 5447fbbfbc6eSjv227347 if ((err = zonecfg_get_handle(target_zone, zone_handle)) != Z_OK) { 5448fbbfbc6eSjv227347 errno = err; 5449fbbfbc6eSjv227347 zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE); 5450fbbfbc6eSjv227347 zonecfg_fini_handle(zone_handle); 5451fbbfbc6eSjv227347 return (Z_ERR); 5452fbbfbc6eSjv227347 } 5453fbbfbc6eSjv227347 if ((err = zonecfg_get_zonepath(zone_handle, zonepath, 5454fbbfbc6eSjv227347 sizeof (zonepath))) != Z_OK) { 5455fbbfbc6eSjv227347 errno = err; 5456fbbfbc6eSjv227347 zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE); 5457fbbfbc6eSjv227347 zonecfg_fini_handle(zone_handle); 5458fbbfbc6eSjv227347 return (Z_ERR); 5459fbbfbc6eSjv227347 } 5460fbbfbc6eSjv227347 if ((brand_handle = brand_open(target_brand)) == NULL) { 5461fbbfbc6eSjv227347 zerror(gettext("missing or invalid brand during %s operation: " 5462fbbfbc6eSjv227347 "%s"), cmd_to_str(CMD_SYSBOOT), target_brand); 5463fbbfbc6eSjv227347 zonecfg_fini_handle(zone_handle); 5464fbbfbc6eSjv227347 return (Z_ERR); 5465fbbfbc6eSjv227347 } 5466fbbfbc6eSjv227347 err = get_hook(brand_handle, cmdbuf, sizeof (cmdbuf), brand_get_sysboot, 5467fbbfbc6eSjv227347 target_zone, zonepath); 5468fbbfbc6eSjv227347 brand_close(brand_handle); 5469fbbfbc6eSjv227347 zonecfg_fini_handle(zone_handle); 5470fbbfbc6eSjv227347 if (err != Z_OK) { 5471fbbfbc6eSjv227347 zerror(gettext("unable to get brand hook from brand %s for %s " 5472fbbfbc6eSjv227347 "operation"), target_brand, cmd_to_str(CMD_SYSBOOT)); 5473fbbfbc6eSjv227347 return (Z_ERR); 5474fbbfbc6eSjv227347 } 5475fbbfbc6eSjv227347 5476fbbfbc6eSjv227347 /* 5477fbbfbc6eSjv227347 * If the hook wasn't defined (which is OK), then indicate success and 5478fbbfbc6eSjv227347 * return. Otherwise, execute the hook. 5479fbbfbc6eSjv227347 */ 5480fbbfbc6eSjv227347 if (cmdbuf[0] != '\0') 5481fbbfbc6eSjv227347 return ((subproc_status(gettext("brand sysboot operation"), 5482fbbfbc6eSjv227347 do_subproc(cmdbuf), B_FALSE) == ZONE_SUBPROC_OK) ? Z_OK : 5483fbbfbc6eSjv227347 Z_BRAND_ERROR); 5484fbbfbc6eSjv227347 return (Z_OK); 5485fbbfbc6eSjv227347 } 5486fbbfbc6eSjv227347 5487555afedfScarlsonj static int 54887c478bd9Sstevel@tonic-gate help_func(int argc, char *argv[]) 54897c478bd9Sstevel@tonic-gate { 54907c478bd9Sstevel@tonic-gate int arg, cmd_num; 54917c478bd9Sstevel@tonic-gate 54927c478bd9Sstevel@tonic-gate if (argc == 0) { 54937c478bd9Sstevel@tonic-gate (void) usage(B_TRUE); 54947c478bd9Sstevel@tonic-gate return (Z_OK); 54957c478bd9Sstevel@tonic-gate } 54967c478bd9Sstevel@tonic-gate optind = 0; 54977c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 54987c478bd9Sstevel@tonic-gate switch (arg) { 54997c478bd9Sstevel@tonic-gate case '?': 55007c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 55017c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 55027c478bd9Sstevel@tonic-gate default: 55037c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 55047c478bd9Sstevel@tonic-gate return (Z_USAGE); 55057c478bd9Sstevel@tonic-gate } 55067c478bd9Sstevel@tonic-gate } 55077c478bd9Sstevel@tonic-gate while (optind < argc) { 5508394a25e2Scarlsonj /* Private commands have NULL short_usage; omit them */ 5509394a25e2Scarlsonj if ((cmd_num = cmd_match(argv[optind])) < 0 || 5510394a25e2Scarlsonj cmdtab[cmd_num].short_usage == NULL) { 55117c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 55127c478bd9Sstevel@tonic-gate return (Z_USAGE); 55137c478bd9Sstevel@tonic-gate } 55147c478bd9Sstevel@tonic-gate sub_usage(cmdtab[cmd_num].short_usage, cmd_num); 55157c478bd9Sstevel@tonic-gate optind++; 55167c478bd9Sstevel@tonic-gate } 55177c478bd9Sstevel@tonic-gate return (Z_OK); 55187c478bd9Sstevel@tonic-gate } 55197c478bd9Sstevel@tonic-gate 55207c478bd9Sstevel@tonic-gate /* 55217c478bd9Sstevel@tonic-gate * Returns: CMD_MIN thru CMD_MAX on success, -1 on error 55227c478bd9Sstevel@tonic-gate */ 55237c478bd9Sstevel@tonic-gate 55247c478bd9Sstevel@tonic-gate static int 55257c478bd9Sstevel@tonic-gate cmd_match(char *cmd) 55267c478bd9Sstevel@tonic-gate { 55277c478bd9Sstevel@tonic-gate int i; 55287c478bd9Sstevel@tonic-gate 55297c478bd9Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 55307c478bd9Sstevel@tonic-gate /* return only if there is an exact match */ 55317c478bd9Sstevel@tonic-gate if (strcmp(cmd, cmdtab[i].cmd_name) == 0) 55327c478bd9Sstevel@tonic-gate return (cmdtab[i].cmd_num); 55337c478bd9Sstevel@tonic-gate } 55347c478bd9Sstevel@tonic-gate return (-1); 55357c478bd9Sstevel@tonic-gate } 55367c478bd9Sstevel@tonic-gate 55377c478bd9Sstevel@tonic-gate static int 55387c478bd9Sstevel@tonic-gate parse_and_run(int argc, char *argv[]) 55397c478bd9Sstevel@tonic-gate { 55407c478bd9Sstevel@tonic-gate int i = cmd_match(argv[0]); 55417c478bd9Sstevel@tonic-gate 55427c478bd9Sstevel@tonic-gate if (i < 0) 55437c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 55447c478bd9Sstevel@tonic-gate return (cmdtab[i].handler(argc - 1, &(argv[1]))); 55457c478bd9Sstevel@tonic-gate } 55467c478bd9Sstevel@tonic-gate 55477c478bd9Sstevel@tonic-gate static char * 55487c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname) 55497c478bd9Sstevel@tonic-gate { 55507c478bd9Sstevel@tonic-gate char *last_slash, *execbasename; 55517c478bd9Sstevel@tonic-gate 55527c478bd9Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 55537c478bd9Sstevel@tonic-gate for (;;) { 55547c478bd9Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 55557c478bd9Sstevel@tonic-gate if (last_slash == NULL) { 55567c478bd9Sstevel@tonic-gate execbasename = execfullname; 55577c478bd9Sstevel@tonic-gate break; 55587c478bd9Sstevel@tonic-gate } else { 55597c478bd9Sstevel@tonic-gate execbasename = last_slash + 1; 55607c478bd9Sstevel@tonic-gate if (*execbasename == '\0') { 55617c478bd9Sstevel@tonic-gate *last_slash = '\0'; 55627c478bd9Sstevel@tonic-gate continue; 55637c478bd9Sstevel@tonic-gate } 55647c478bd9Sstevel@tonic-gate break; 55657c478bd9Sstevel@tonic-gate } 55667c478bd9Sstevel@tonic-gate } 55677c478bd9Sstevel@tonic-gate return (execbasename); 55687c478bd9Sstevel@tonic-gate } 55697c478bd9Sstevel@tonic-gate 55707c478bd9Sstevel@tonic-gate int 55717c478bd9Sstevel@tonic-gate main(int argc, char **argv) 55727c478bd9Sstevel@tonic-gate { 55737c478bd9Sstevel@tonic-gate int arg; 55747c478bd9Sstevel@tonic-gate zoneid_t zid; 5575108322fbScarlsonj struct stat st; 55769acbbeafSnn35248 char *zone_lock_env; 55779acbbeafSnn35248 int err; 55787c478bd9Sstevel@tonic-gate 55797c478bd9Sstevel@tonic-gate if ((locale = setlocale(LC_ALL, "")) == NULL) 55807c478bd9Sstevel@tonic-gate locale = "C"; 55817c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 55827c478bd9Sstevel@tonic-gate setbuf(stdout, NULL); 55837c478bd9Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_IGN); 55847c478bd9Sstevel@tonic-gate execname = get_execbasename(argv[0]); 55857c478bd9Sstevel@tonic-gate target_zone = NULL; 55867c478bd9Sstevel@tonic-gate if (chdir("/") != 0) { 55877c478bd9Sstevel@tonic-gate zerror(gettext("could not change directory to /.")); 55887c478bd9Sstevel@tonic-gate exit(Z_ERR); 55897c478bd9Sstevel@tonic-gate } 5590d0e4f536SSudheer A /* 5591d0e4f536SSudheer A * Use the default system mask rather than anything that may have been 5592d0e4f536SSudheer A * set by the caller. 5593d0e4f536SSudheer A */ 5594d0e4f536SSudheer A (void) umask(CMASK); 55957c478bd9Sstevel@tonic-gate 559699653d4eSeschrock if (init_zfs() != Z_OK) 559799653d4eSeschrock exit(Z_ERR); 559899653d4eSeschrock 5599555afedfScarlsonj while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) { 56007c478bd9Sstevel@tonic-gate switch (arg) { 56017c478bd9Sstevel@tonic-gate case '?': 56027c478bd9Sstevel@tonic-gate return (usage(B_TRUE)); 5603555afedfScarlsonj case 'u': 5604555afedfScarlsonj target_uuid = optarg; 5605555afedfScarlsonj break; 56067c478bd9Sstevel@tonic-gate case 'z': 56077c478bd9Sstevel@tonic-gate target_zone = optarg; 56087c478bd9Sstevel@tonic-gate break; 5609108322fbScarlsonj case 'R': /* private option for admin/install use */ 5610108322fbScarlsonj if (*optarg != '/') { 5611108322fbScarlsonj zerror(gettext("root path must be absolute.")); 5612108322fbScarlsonj exit(Z_ERR); 5613108322fbScarlsonj } 5614108322fbScarlsonj if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) { 5615108322fbScarlsonj zerror( 5616108322fbScarlsonj gettext("root path must be a directory.")); 5617108322fbScarlsonj exit(Z_ERR); 5618108322fbScarlsonj } 5619108322fbScarlsonj zonecfg_set_root(optarg); 5620108322fbScarlsonj break; 56217c478bd9Sstevel@tonic-gate default: 56227c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 56237c478bd9Sstevel@tonic-gate } 56247c478bd9Sstevel@tonic-gate } 56257c478bd9Sstevel@tonic-gate 56267c478bd9Sstevel@tonic-gate if (optind >= argc) 56277c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 5628555afedfScarlsonj 5629555afedfScarlsonj if (target_uuid != NULL && *target_uuid != '\0') { 5630555afedfScarlsonj uuid_t uuid; 5631555afedfScarlsonj static char newtarget[ZONENAME_MAX]; 5632555afedfScarlsonj 5633555afedfScarlsonj if (uuid_parse(target_uuid, uuid) == -1) { 5634555afedfScarlsonj zerror(gettext("illegal UUID value specified")); 5635555afedfScarlsonj exit(Z_ERR); 5636555afedfScarlsonj } 5637555afedfScarlsonj if (zonecfg_get_name_by_uuid(uuid, newtarget, 5638555afedfScarlsonj sizeof (newtarget)) == Z_OK) 5639555afedfScarlsonj target_zone = newtarget; 5640555afedfScarlsonj } 5641555afedfScarlsonj 56427c478bd9Sstevel@tonic-gate if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) { 56437c478bd9Sstevel@tonic-gate errno = Z_NO_ZONE; 56447c478bd9Sstevel@tonic-gate zperror(target_zone, B_TRUE); 56457c478bd9Sstevel@tonic-gate exit(Z_ERR); 56467c478bd9Sstevel@tonic-gate } 56479acbbeafSnn35248 56489acbbeafSnn35248 /* 56499acbbeafSnn35248 * See if we have inherited the right to manipulate this zone from 56509acbbeafSnn35248 * a zoneadm instance in our ancestry. If so, set zone_lock_cnt to 56519acbbeafSnn35248 * indicate it. If not, make that explicit in our environment. 56529acbbeafSnn35248 */ 5653ff17c8bfSgjelinek zonecfg_init_lock_file(target_zone, &zone_lock_env); 56549acbbeafSnn35248 5655*e5816e35SEdward Pilatowicz /* Figure out what the system's default brand is */ 5656*e5816e35SEdward Pilatowicz if (zonecfg_default_brand(default_brand, 5657*e5816e35SEdward Pilatowicz sizeof (default_brand)) != Z_OK) { 5658*e5816e35SEdward Pilatowicz zerror(gettext("unable to determine default brand")); 5659*e5816e35SEdward Pilatowicz return (Z_ERR); 5660*e5816e35SEdward Pilatowicz } 5661*e5816e35SEdward Pilatowicz 56629acbbeafSnn35248 /* 56639acbbeafSnn35248 * If we are going to be operating on a single zone, retrieve its 56649acbbeafSnn35248 * brand type and determine whether it is native or not. 56659acbbeafSnn35248 */ 56669acbbeafSnn35248 if ((target_zone != NULL) && 56672ad45a84Sjv227347 (strcmp(target_zone, GLOBAL_ZONENAME) != 0)) { 56689acbbeafSnn35248 if (zone_get_brand(target_zone, target_brand, 56699acbbeafSnn35248 sizeof (target_brand)) != Z_OK) { 56709acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 56719acbbeafSnn35248 exit(Z_ERR); 56729acbbeafSnn35248 } 567362868012SSteve Lawrence /* 567462868012SSteve Lawrence * In the alternate root environment, the only supported 567562868012SSteve Lawrence * operations are mount and unmount. In this case, just treat 567662868012SSteve Lawrence * the zone as native if it is cluster. Cluster zones can be 567762868012SSteve Lawrence * native for the purpose of LU or upgrade, and the cluster 567862868012SSteve Lawrence * brand may not exist in the miniroot (such as in net install 567962868012SSteve Lawrence * upgrade). 568062868012SSteve Lawrence */ 568162868012SSteve Lawrence if (strcmp(target_brand, CLUSTER_BRAND_NAME) == 0) { 568262868012SSteve Lawrence if (zonecfg_in_alt_root()) { 5683*e5816e35SEdward Pilatowicz (void) strlcpy(target_brand, default_brand, 568462868012SSteve Lawrence sizeof (target_brand)); 568562868012SSteve Lawrence } 568662868012SSteve Lawrence } 56879acbbeafSnn35248 } 56889acbbeafSnn35248 56899acbbeafSnn35248 err = parse_and_run(argc - optind, &argv[optind]); 56909acbbeafSnn35248 56919acbbeafSnn35248 return (err); 56927c478bd9Sstevel@tonic-gate } 5693