17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5ee519a1fSgjelinek * Common Development and Distribution License (the "License"). 6ee519a1fSgjelinek * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217e362f58Scomay 227c478bd9Sstevel@tonic-gate /* 236cfd72c6Sgjelinek * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * zoneadm is a command interpreter for zone administration. It is all in 317c478bd9Sstevel@tonic-gate * C (i.e., no lex/yacc), and all the argument passing is argc/argv based. 327c478bd9Sstevel@tonic-gate * main() calls parse_and_run() which calls cmd_match(), then invokes the 337c478bd9Sstevel@tonic-gate * appropriate command's handler function. The rest of the program is the 347c478bd9Sstevel@tonic-gate * handler functions and their helper functions. 357c478bd9Sstevel@tonic-gate * 367c478bd9Sstevel@tonic-gate * Some of the helper functions are used largely to simplify I18N: reducing 377c478bd9Sstevel@tonic-gate * the need for translation notes. This is particularly true of many of 387c478bd9Sstevel@tonic-gate * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather 397c478bd9Sstevel@tonic-gate * than zerror(gettext("foo failed")) with a translation note indicating 407c478bd9Sstevel@tonic-gate * that "foo" need not be translated. 417c478bd9Sstevel@tonic-gate */ 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include <stdio.h> 447c478bd9Sstevel@tonic-gate #include <errno.h> 457c478bd9Sstevel@tonic-gate #include <unistd.h> 467c478bd9Sstevel@tonic-gate #include <signal.h> 477c478bd9Sstevel@tonic-gate #include <stdarg.h> 487c478bd9Sstevel@tonic-gate #include <ctype.h> 497c478bd9Sstevel@tonic-gate #include <stdlib.h> 507c478bd9Sstevel@tonic-gate #include <string.h> 517c478bd9Sstevel@tonic-gate #include <wait.h> 527c478bd9Sstevel@tonic-gate #include <zone.h> 537c478bd9Sstevel@tonic-gate #include <priv.h> 547c478bd9Sstevel@tonic-gate #include <locale.h> 557c478bd9Sstevel@tonic-gate #include <libintl.h> 567c478bd9Sstevel@tonic-gate #include <libzonecfg.h> 577c478bd9Sstevel@tonic-gate #include <bsm/adt.h> 589acbbeafSnn35248 #include <sys/brand.h> 597c478bd9Sstevel@tonic-gate #include <sys/param.h> 607c478bd9Sstevel@tonic-gate #include <sys/types.h> 617c478bd9Sstevel@tonic-gate #include <sys/stat.h> 627c478bd9Sstevel@tonic-gate #include <sys/statvfs.h> 637c478bd9Sstevel@tonic-gate #include <assert.h> 647c478bd9Sstevel@tonic-gate #include <sys/sockio.h> 657c478bd9Sstevel@tonic-gate #include <sys/mntent.h> 667c478bd9Sstevel@tonic-gate #include <limits.h> 670b5de56dSgjelinek #include <dirent.h> 68555afedfScarlsonj #include <uuid/uuid.h> 69948f2876Sss150715 #include <libdlpi.h> 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate #include <fcntl.h> 727c478bd9Sstevel@tonic-gate #include <door.h> 737c478bd9Sstevel@tonic-gate #include <macros.h> 747c478bd9Sstevel@tonic-gate #include <libgen.h> 75865e09a4Sgjelinek #include <fnmatch.h> 76e7f3c547Sgjelinek #include <sys/modctl.h> 779acbbeafSnn35248 #include <libbrand.h> 780209230bSgjelinek #include <libscf.h> 797ef01d19Sgjelinek #include <procfs.h> 80d9e728a2Sgjelinek #include <strings.h> 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate #include <pool.h> 837c478bd9Sstevel@tonic-gate #include <sys/pool.h> 840209230bSgjelinek #include <sys/priocntl.h> 850209230bSgjelinek #include <sys/fsspriocntl.h> 867c478bd9Sstevel@tonic-gate 870b5de56dSgjelinek #include "zoneadm.h" 880b5de56dSgjelinek 897c478bd9Sstevel@tonic-gate #define MAXARGS 8 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate /* Reflects kernel zone entries */ 927c478bd9Sstevel@tonic-gate typedef struct zone_entry { 937c478bd9Sstevel@tonic-gate zoneid_t zid; 947c478bd9Sstevel@tonic-gate char zname[ZONENAME_MAX]; 957c478bd9Sstevel@tonic-gate char *zstate_str; 967c478bd9Sstevel@tonic-gate zone_state_t zstate_num; 979acbbeafSnn35248 char zbrand[MAXNAMELEN]; 987c478bd9Sstevel@tonic-gate char zroot[MAXPATHLEN]; 99555afedfScarlsonj char zuuid[UUID_PRINTABLE_STRING_LENGTH]; 100f4b3ec61Sdh155122 zone_iptype_t ziptype; 1017c478bd9Sstevel@tonic-gate } zone_entry_t; 1027c478bd9Sstevel@tonic-gate 10384561e8cStd153743 #define CLUSTER_BRAND_NAME "cluster" 10484561e8cStd153743 1057c478bd9Sstevel@tonic-gate static zone_entry_t *zents; 1067c478bd9Sstevel@tonic-gate static size_t nzents; 1079acbbeafSnn35248 static boolean_t is_native_zone = B_TRUE; 10884561e8cStd153743 static boolean_t is_cluster_zone = B_FALSE; 1097c478bd9Sstevel@tonic-gate 1101390a385Sgjelinek #define LOOPBACK_IF "lo0" 1111390a385Sgjelinek #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af)) 1121390a385Sgjelinek 1131390a385Sgjelinek struct net_if { 1141390a385Sgjelinek char *name; 1151390a385Sgjelinek int af; 1161390a385Sgjelinek }; 1171390a385Sgjelinek 1187c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */ 1197c478bd9Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 1207c478bd9Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate struct cmd { 1237c478bd9Sstevel@tonic-gate uint_t cmd_num; /* command number */ 1247c478bd9Sstevel@tonic-gate char *cmd_name; /* command name */ 1257c478bd9Sstevel@tonic-gate char *short_usage; /* short form help */ 1267c478bd9Sstevel@tonic-gate int (*handler)(int argc, char *argv[]); /* function to call */ 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate }; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate #define SHELP_HELP "help" 1313f2f09c1Sdp #define SHELP_BOOT "boot [-- boot_arguments]" 1327c478bd9Sstevel@tonic-gate #define SHELP_HALT "halt" 1337c478bd9Sstevel@tonic-gate #define SHELP_READY "ready" 1343f2f09c1Sdp #define SHELP_REBOOT "reboot [-- boot_arguments]" 1357c478bd9Sstevel@tonic-gate #define SHELP_LIST "list [-cipv]" 1367c478bd9Sstevel@tonic-gate #define SHELP_VERIFY "verify" 1379acbbeafSnn35248 #define SHELP_INSTALL "install [-x nodataset] [brand-specific args]" 1387c478bd9Sstevel@tonic-gate #define SHELP_UNINSTALL "uninstall [-F]" 1390b5de56dSgjelinek #define SHELP_CLONE "clone [-m method] [-s <ZFS snapshot>] zonename" 140865e09a4Sgjelinek #define SHELP_MOVE "move zonepath" 1418cd327d5Sgjelinek #define SHELP_DETACH "detach [-n]" 1426cfd72c6Sgjelinek #define SHELP_ATTACH "attach [-F] [-n <path>] [-u]" 143555afedfScarlsonj #define SHELP_MARK "mark incomplete" 1447c478bd9Sstevel@tonic-gate 1459acbbeafSnn35248 #define EXEC_PREFIX "exec " 1469acbbeafSnn35248 #define EXEC_LEN (strlen(EXEC_PREFIX)) 1479acbbeafSnn35248 #define RMCOMMAND "/usr/bin/rm -rf" 1489acbbeafSnn35248 1499acbbeafSnn35248 static int cleanup_zonepath(char *, boolean_t); 1509acbbeafSnn35248 151f4b3ec61Sdh155122 1527c478bd9Sstevel@tonic-gate static int help_func(int argc, char *argv[]); 1537c478bd9Sstevel@tonic-gate static int ready_func(int argc, char *argv[]); 1547c478bd9Sstevel@tonic-gate static int boot_func(int argc, char *argv[]); 1557c478bd9Sstevel@tonic-gate static int halt_func(int argc, char *argv[]); 1567c478bd9Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]); 1577c478bd9Sstevel@tonic-gate static int list_func(int argc, char *argv[]); 1587c478bd9Sstevel@tonic-gate static int verify_func(int argc, char *argv[]); 1597c478bd9Sstevel@tonic-gate static int install_func(int argc, char *argv[]); 1607c478bd9Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]); 161108322fbScarlsonj static int mount_func(int argc, char *argv[]); 162108322fbScarlsonj static int unmount_func(int argc, char *argv[]); 163865e09a4Sgjelinek static int clone_func(int argc, char *argv[]); 164865e09a4Sgjelinek static int move_func(int argc, char *argv[]); 165ee519a1fSgjelinek static int detach_func(int argc, char *argv[]); 166ee519a1fSgjelinek static int attach_func(int argc, char *argv[]); 167555afedfScarlsonj static int mark_func(int argc, char *argv[]); 1680209230bSgjelinek static int apply_func(int argc, char *argv[]); 1697c478bd9Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running, 1709acbbeafSnn35248 boolean_t unsafe_when_running, boolean_t force); 1717c478bd9Sstevel@tonic-gate static int cmd_match(char *cmd); 172ce28b40eSzt129084 static int verify_details(int, char *argv[]); 173ce28b40eSzt129084 static int verify_brand(zone_dochandle_t, int, char *argv[]); 174ce28b40eSzt129084 static int invoke_brand_handler(int, char *argv[]); 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate static struct cmd cmdtab[] = { 1777c478bd9Sstevel@tonic-gate { CMD_HELP, "help", SHELP_HELP, help_func }, 1787c478bd9Sstevel@tonic-gate { CMD_BOOT, "boot", SHELP_BOOT, boot_func }, 1797c478bd9Sstevel@tonic-gate { CMD_HALT, "halt", SHELP_HALT, halt_func }, 1807c478bd9Sstevel@tonic-gate { CMD_READY, "ready", SHELP_READY, ready_func }, 1817c478bd9Sstevel@tonic-gate { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func }, 1827c478bd9Sstevel@tonic-gate { CMD_LIST, "list", SHELP_LIST, list_func }, 1837c478bd9Sstevel@tonic-gate { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func }, 1847c478bd9Sstevel@tonic-gate { CMD_INSTALL, "install", SHELP_INSTALL, install_func }, 1857c478bd9Sstevel@tonic-gate { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL, 186108322fbScarlsonj uninstall_func }, 187865e09a4Sgjelinek /* mount and unmount are private commands for admin/install */ 188108322fbScarlsonj { CMD_MOUNT, "mount", NULL, mount_func }, 189865e09a4Sgjelinek { CMD_UNMOUNT, "unmount", NULL, unmount_func }, 190865e09a4Sgjelinek { CMD_CLONE, "clone", SHELP_CLONE, clone_func }, 191ee519a1fSgjelinek { CMD_MOVE, "move", SHELP_MOVE, move_func }, 192ee519a1fSgjelinek { CMD_DETACH, "detach", SHELP_DETACH, detach_func }, 193555afedfScarlsonj { CMD_ATTACH, "attach", SHELP_ATTACH, attach_func }, 1940209230bSgjelinek { CMD_MARK, "mark", SHELP_MARK, mark_func }, 1950209230bSgjelinek { CMD_APPLY, "apply", NULL, apply_func } 1967c478bd9Sstevel@tonic-gate }; 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate /* global variables */ 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */ 2017c478bd9Sstevel@tonic-gate static char *execname; 2029acbbeafSnn35248 static char target_brand[MAXNAMELEN]; 2037c478bd9Sstevel@tonic-gate static char *locale; 2040b5de56dSgjelinek char *target_zone; 205555afedfScarlsonj static char *target_uuid; 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate /* used in do_subproc() and signal handler */ 2087c478bd9Sstevel@tonic-gate static volatile boolean_t child_killed; 2096cfd72c6Sgjelinek /* used in attach_func() and signal handler */ 2106cfd72c6Sgjelinek static volatile boolean_t attach_interupted; 2119acbbeafSnn35248 static int do_subproc_cnt = 0; 2129acbbeafSnn35248 2139acbbeafSnn35248 /* 2149acbbeafSnn35248 * Used to indicate whether this zoneadm instance has another zoneadm 2159acbbeafSnn35248 * instance in its ancestry. 2169acbbeafSnn35248 */ 2179acbbeafSnn35248 static boolean_t zoneadm_is_nested = B_FALSE; 2189acbbeafSnn35248 2199acbbeafSnn35248 /* used to track nested zone-lock operations */ 2209acbbeafSnn35248 static int zone_lock_cnt = 0; 2219acbbeafSnn35248 2229acbbeafSnn35248 /* used to communicate lock status to children */ 2239acbbeafSnn35248 #define LOCK_ENV_VAR "_ZONEADM_LOCK_HELD" 2249acbbeafSnn35248 static char zoneadm_lock_held[] = LOCK_ENV_VAR"=1"; 2259acbbeafSnn35248 static char zoneadm_lock_not_held[] = LOCK_ENV_VAR"=0"; 2267c478bd9Sstevel@tonic-gate 2270b5de56dSgjelinek char * 2287c478bd9Sstevel@tonic-gate cmd_to_str(int cmd_num) 2297c478bd9Sstevel@tonic-gate { 2307c478bd9Sstevel@tonic-gate assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2317c478bd9Sstevel@tonic-gate return (cmdtab[cmd_num].cmd_name); 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */ 2357c478bd9Sstevel@tonic-gate static char * 2367c478bd9Sstevel@tonic-gate long_help(int cmd_num) 2377c478bd9Sstevel@tonic-gate { 2387e362f58Scomay assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX); 2397c478bd9Sstevel@tonic-gate switch (cmd_num) { 2407c478bd9Sstevel@tonic-gate case CMD_HELP: 2417c478bd9Sstevel@tonic-gate return (gettext("Print usage message.")); 2427c478bd9Sstevel@tonic-gate case CMD_BOOT: 2433f2f09c1Sdp return (gettext("Activates (boots) specified zone. See " 2443f2f09c1Sdp "zoneadm(1m) for valid boot\n\targuments.")); 2457c478bd9Sstevel@tonic-gate case CMD_HALT: 2469e518655Sgjelinek return (gettext("Halts specified zone, bypassing shutdown " 2479e518655Sgjelinek "scripts and removing runtime\n\tresources of the zone.")); 2487c478bd9Sstevel@tonic-gate case CMD_READY: 2499e518655Sgjelinek return (gettext("Prepares a zone for running applications but " 2509e518655Sgjelinek "does not start any user\n\tprocesses in the zone.")); 2517c478bd9Sstevel@tonic-gate case CMD_REBOOT: 2529e518655Sgjelinek return (gettext("Restarts the zone (equivalent to a halt / " 2533f2f09c1Sdp "boot sequence).\n\tFails if the zone is not active. " 2543f2f09c1Sdp "See zoneadm(1m) for valid boot\n\targuments.")); 2557c478bd9Sstevel@tonic-gate case CMD_LIST: 2567c478bd9Sstevel@tonic-gate return (gettext("Lists the current zones, or a " 2577c478bd9Sstevel@tonic-gate "specific zone if indicated. By default,\n\tall " 2587c478bd9Sstevel@tonic-gate "running zones are listed, though this can be " 2597c478bd9Sstevel@tonic-gate "expanded to all\n\tinstalled zones with the -i " 2607c478bd9Sstevel@tonic-gate "option or all configured zones with the\n\t-c " 261555afedfScarlsonj "option. When used with the general -z <zone> and/or -u " 262555afedfScarlsonj "<uuid-match>\n\toptions, lists only the specified " 263555afedfScarlsonj "matching zone, but lists it\n\tregardless of its state, " 264555afedfScarlsonj "and the -i and -c options are disallowed. The\n\t-v " 265555afedfScarlsonj "option can be used to display verbose information: zone " 266555afedfScarlsonj "name, id,\n\tcurrent state, root directory and options. " 267555afedfScarlsonj "The -p option can be used\n\tto request machine-parsable " 268555afedfScarlsonj "output. The -v and -p options are mutually\n\texclusive." 269555afedfScarlsonj " If neither -v nor -p is used, just the zone name is " 270555afedfScarlsonj "listed.")); 2717c478bd9Sstevel@tonic-gate case CMD_VERIFY: 2727c478bd9Sstevel@tonic-gate return (gettext("Check to make sure the configuration " 2737c478bd9Sstevel@tonic-gate "can safely be instantiated\n\ton the machine: " 2747c478bd9Sstevel@tonic-gate "physical network interfaces exist, etc.")); 2757c478bd9Sstevel@tonic-gate case CMD_INSTALL: 2760b5de56dSgjelinek return (gettext("Install the configuration on to the system. " 2770b5de56dSgjelinek "The -x nodataset option\n\tcan be used to prevent the " 2780b5de56dSgjelinek "creation of a new ZFS file system for the\n\tzone " 2799acbbeafSnn35248 "(assuming the zonepath is within a ZFS file system).\n\t" 2809acbbeafSnn35248 "All other arguments are passed to the brand installation " 2819acbbeafSnn35248 "function;\n\tsee brand(4) for more information.")); 2827c478bd9Sstevel@tonic-gate case CMD_UNINSTALL: 2839e518655Sgjelinek return (gettext("Uninstall the configuration from the system. " 2849e518655Sgjelinek "The -F flag can be used\n\tto force the action.")); 285865e09a4Sgjelinek case CMD_CLONE: 2860b5de56dSgjelinek return (gettext("Clone the installation of another zone. " 2870b5de56dSgjelinek "The -m option can be used to\n\tspecify 'copy' which " 2880b5de56dSgjelinek "forces a copy of the source zone. The -s option\n\t" 2890b5de56dSgjelinek "can be used to specify the name of a ZFS snapshot " 2900b5de56dSgjelinek "that was taken from\n\ta previous clone command. The " 2910b5de56dSgjelinek "snapshot will be used as the source\n\tinstead of " 2920b5de56dSgjelinek "creating a new ZFS snapshot.")); 293865e09a4Sgjelinek case CMD_MOVE: 294865e09a4Sgjelinek return (gettext("Move the zone to a new zonepath.")); 2959e518655Sgjelinek case CMD_DETACH: 2969e518655Sgjelinek return (gettext("Detach the zone from the system. The zone " 2979e518655Sgjelinek "state is changed to\n\t'configured' (but the files under " 2989e518655Sgjelinek "the zonepath are untouched).\n\tThe zone can subsequently " 2999e518655Sgjelinek "be attached, or can be moved to another\n\tsystem and " 3008cd327d5Sgjelinek "attached there. The -n option can be used to specify\n\t" 3018cd327d5Sgjelinek "'no-execute' mode. When -n is used, the information " 3028cd327d5Sgjelinek "needed to attach\n\tthe zone is sent to standard output " 3038cd327d5Sgjelinek "but the zone is not actually\n\tdetached.")); 3049e518655Sgjelinek case CMD_ATTACH: 3059e518655Sgjelinek return (gettext("Attach the zone to the system. The zone " 3069e518655Sgjelinek "state must be 'configured'\n\tprior to attach; upon " 3079e518655Sgjelinek "successful completion, the zone state will be\n\t" 3089e518655Sgjelinek "'installed'. The system software on the current " 3099e518655Sgjelinek "system must be\n\tcompatible with the software on the " 3106cfd72c6Sgjelinek "zone's original system or use\n\tthe -u option to update " 3116cfd72c6Sgjelinek "the zone to the current system software.\n\tSpecify -F " 3126cfd72c6Sgjelinek "to force the attach and skip software compatibility " 3136cfd72c6Sgjelinek "tests.\n\tThe -n option can be used to specify " 3146cfd72c6Sgjelinek "'no-execute' mode. When -n is\n\tused, the information " 3156cfd72c6Sgjelinek "needed to attach the zone is read from the\n\tspecified " 3166cfd72c6Sgjelinek "path and the configuration is only validated. The path " 3176cfd72c6Sgjelinek "can\n\tbe '-' to specify standard input. The -F, -n and " 3186cfd72c6Sgjelinek "-u options are\n\tmutually exclusive.")); 319555afedfScarlsonj case CMD_MARK: 320555afedfScarlsonj return (gettext("Set the state of the zone. This can be used " 321555afedfScarlsonj "to force the zone\n\tstate to 'incomplete' " 322555afedfScarlsonj "administratively if some activity has rendered\n\tthe " 323555afedfScarlsonj "zone permanently unusable. The only valid state that " 324555afedfScarlsonj "may be\n\tspecified is 'incomplete'.")); 325108322fbScarlsonj default: 326108322fbScarlsonj return (""); 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate /* NOTREACHED */ 3297e362f58Scomay return (NULL); 3307c478bd9Sstevel@tonic-gate } 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate /* 3337c478bd9Sstevel@tonic-gate * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for 3347c478bd9Sstevel@tonic-gate * unexpected errors. 3357c478bd9Sstevel@tonic-gate */ 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate static int 3387c478bd9Sstevel@tonic-gate usage(boolean_t explicit) 3397c478bd9Sstevel@tonic-gate { 3407c478bd9Sstevel@tonic-gate int i; 3417c478bd9Sstevel@tonic-gate FILE *fd = explicit ? stdout : stderr; 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname); 344555afedfScarlsonj (void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n", 345555afedfScarlsonj execname); 346555afedfScarlsonj (void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname, 3477c478bd9Sstevel@tonic-gate gettext("subcommand")); 3487c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands")); 3497c478bd9Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 350108322fbScarlsonj if (cmdtab[i].short_usage == NULL) 351108322fbScarlsonj continue; 3527c478bd9Sstevel@tonic-gate (void) fprintf(fd, "%s\n", cmdtab[i].short_usage); 3537c478bd9Sstevel@tonic-gate if (explicit) 3547c478bd9Sstevel@tonic-gate (void) fprintf(fd, "\t%s\n\n", long_help(i)); 3557c478bd9Sstevel@tonic-gate } 3567c478bd9Sstevel@tonic-gate if (!explicit) 3577c478bd9Sstevel@tonic-gate (void) fputs("\n", fd); 3587c478bd9Sstevel@tonic-gate return (Z_USAGE); 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate static void 3627c478bd9Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num) 3637c478bd9Sstevel@tonic-gate { 3647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage); 3657c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s\n", long_help(cmd_num)); 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate /* 3697c478bd9Sstevel@tonic-gate * zperror() is like perror(3c) except that this also prints the executable 3707c478bd9Sstevel@tonic-gate * name at the start of the message, and takes a boolean indicating whether 3717c478bd9Sstevel@tonic-gate * to call libc'c strerror() or that from libzonecfg. 3727c478bd9Sstevel@tonic-gate */ 3737c478bd9Sstevel@tonic-gate 3740b5de56dSgjelinek void 3757c478bd9Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error) 3767c478bd9Sstevel@tonic-gate { 3777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", execname, str, 3787c478bd9Sstevel@tonic-gate zonecfg_error ? zonecfg_strerror(errno) : strerror(errno)); 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate /* 3827c478bd9Sstevel@tonic-gate * zperror2() is very similar to zperror() above, except it also prints a 3837c478bd9Sstevel@tonic-gate * supplied zone name after the executable. 3847c478bd9Sstevel@tonic-gate * 3857c478bd9Sstevel@tonic-gate * All current consumers of this function want libzonecfg's strerror() rather 3867c478bd9Sstevel@tonic-gate * than libc's; if this ever changes, this function can be made more generic 3877c478bd9Sstevel@tonic-gate * like zperror() above. 3887c478bd9Sstevel@tonic-gate */ 3897c478bd9Sstevel@tonic-gate 3900b5de56dSgjelinek void 3917c478bd9Sstevel@tonic-gate zperror2(const char *zone, const char *str) 3927c478bd9Sstevel@tonic-gate { 3937c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str, 3947c478bd9Sstevel@tonic-gate zonecfg_strerror(errno)); 3957c478bd9Sstevel@tonic-gate } 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */ 3980b5de56dSgjelinek void 3997c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...) 4007c478bd9Sstevel@tonic-gate { 4017c478bd9Sstevel@tonic-gate va_list alist; 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate va_start(alist, fmt); 4047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", execname); 4057c478bd9Sstevel@tonic-gate if (target_zone != NULL) 4067c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "zone '%s': ", target_zone); 4077c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, alist); 4087c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 4097c478bd9Sstevel@tonic-gate va_end(alist); 4107c478bd9Sstevel@tonic-gate } 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate static void * 4137c478bd9Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize) 4147c478bd9Sstevel@tonic-gate { 4157c478bd9Sstevel@tonic-gate void *r = calloc(nelem, elsize); 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate if (r == NULL) { 4187c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), 4197c478bd9Sstevel@tonic-gate (ulong_t)nelem * elsize, strerror(errno)); 4207c478bd9Sstevel@tonic-gate exit(Z_ERR); 4217c478bd9Sstevel@tonic-gate } 4227c478bd9Sstevel@tonic-gate return (r); 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate static void 4267c478bd9Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable) 4277c478bd9Sstevel@tonic-gate { 4287c478bd9Sstevel@tonic-gate static boolean_t firsttime = B_TRUE; 429f4b3ec61Sdh155122 char *ip_type_str; 430f4b3ec61Sdh155122 431f4b3ec61Sdh155122 if (zent->ziptype == ZS_EXCLUSIVE) 432f4b3ec61Sdh155122 ip_type_str = "excl"; 433f4b3ec61Sdh155122 else 434f4b3ec61Sdh155122 ip_type_str = "shared"; 4357c478bd9Sstevel@tonic-gate 4367c478bd9Sstevel@tonic-gate assert(!(verbose && parsable)); 4377c478bd9Sstevel@tonic-gate if (firsttime && verbose) { 4387c478bd9Sstevel@tonic-gate firsttime = B_FALSE; 439f4b3ec61Sdh155122 (void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n", 440f4b3ec61Sdh155122 ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND", 441f4b3ec61Sdh155122 "IP"); 4427c478bd9Sstevel@tonic-gate } 4437c478bd9Sstevel@tonic-gate if (!verbose) { 444555afedfScarlsonj char *cp, *clim; 445555afedfScarlsonj 4467c478bd9Sstevel@tonic-gate if (!parsable) { 4477c478bd9Sstevel@tonic-gate (void) printf("%s\n", zent->zname); 4487c478bd9Sstevel@tonic-gate return; 4497c478bd9Sstevel@tonic-gate } 4507c478bd9Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4517c478bd9Sstevel@tonic-gate (void) printf("-"); 4527c478bd9Sstevel@tonic-gate else 4537c478bd9Sstevel@tonic-gate (void) printf("%lu", zent->zid); 454555afedfScarlsonj (void) printf(":%s:%s:", zent->zname, zent->zstate_str); 455555afedfScarlsonj cp = zent->zroot; 456555afedfScarlsonj while ((clim = strchr(cp, ':')) != NULL) { 457555afedfScarlsonj (void) printf("%.*s\\:", clim - cp, cp); 458555afedfScarlsonj cp = clim + 1; 459555afedfScarlsonj } 460f4b3ec61Sdh155122 (void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand, 461f4b3ec61Sdh155122 ip_type_str); 4627c478bd9Sstevel@tonic-gate return; 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate if (zent->zstate_str != NULL) { 4657c478bd9Sstevel@tonic-gate if (zent->zid == ZONE_ID_UNDEFINED) 4667c478bd9Sstevel@tonic-gate (void) printf("%*s", ZONEID_WIDTH, "-"); 4677c478bd9Sstevel@tonic-gate else 4687c478bd9Sstevel@tonic-gate (void) printf("%*lu", ZONEID_WIDTH, zent->zid); 469f4b3ec61Sdh155122 (void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname, 470f4b3ec61Sdh155122 zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str); 4717c478bd9Sstevel@tonic-gate } 4727c478bd9Sstevel@tonic-gate } 4737c478bd9Sstevel@tonic-gate 4747c478bd9Sstevel@tonic-gate static int 475108322fbScarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent) 4767c478bd9Sstevel@tonic-gate { 47745916cd2Sjpk char root[MAXPATHLEN], *cp; 4787c478bd9Sstevel@tonic-gate int err; 479555afedfScarlsonj uuid_t uuid; 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname)); 4827c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot)); 4839acbbeafSnn35248 (void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand)); 4847c478bd9Sstevel@tonic-gate zent->zstate_str = "???"; 4857c478bd9Sstevel@tonic-gate 486108322fbScarlsonj zent->zid = zid; 4877c478bd9Sstevel@tonic-gate 488555afedfScarlsonj if (zonecfg_get_uuid(zone_name, uuid) == Z_OK && 489555afedfScarlsonj !uuid_is_null(uuid)) 490555afedfScarlsonj uuid_unparse(uuid, zent->zuuid); 491555afedfScarlsonj else 492555afedfScarlsonj zent->zuuid[0] = '\0'; 493555afedfScarlsonj 49445916cd2Sjpk /* 49545916cd2Sjpk * For labeled zones which query the zone path of lower-level 49645916cd2Sjpk * zones, the path needs to be adjusted to drop the final 49745916cd2Sjpk * "/root" component. This adjusted path is then useful 49845916cd2Sjpk * for reading down any exported directories from the 49945916cd2Sjpk * lower-level zone. 50045916cd2Sjpk */ 50145916cd2Sjpk if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) { 50245916cd2Sjpk if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot, 50345916cd2Sjpk sizeof (zent->zroot)) == -1) { 50445916cd2Sjpk zperror2(zent->zname, 50545916cd2Sjpk gettext("could not get zone path.")); 50645916cd2Sjpk return (Z_ERR); 50745916cd2Sjpk } 50845916cd2Sjpk cp = zent->zroot + strlen(zent->zroot) - 5; 50945916cd2Sjpk if (cp > zent->zroot && strcmp(cp, "/root") == 0) 51045916cd2Sjpk *cp = 0; 51145916cd2Sjpk } else { 51245916cd2Sjpk if ((err = zone_get_zonepath(zent->zname, root, 51345916cd2Sjpk sizeof (root))) != Z_OK) { 5147c478bd9Sstevel@tonic-gate errno = err; 51545916cd2Sjpk zperror2(zent->zname, 51645916cd2Sjpk gettext("could not get zone path.")); 5177c478bd9Sstevel@tonic-gate return (Z_ERR); 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate (void) strlcpy(zent->zroot, root, sizeof (zent->zroot)); 52045916cd2Sjpk } 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) { 5237c478bd9Sstevel@tonic-gate errno = err; 5247c478bd9Sstevel@tonic-gate zperror2(zent->zname, gettext("could not get state")); 5257c478bd9Sstevel@tonic-gate return (Z_ERR); 5267c478bd9Sstevel@tonic-gate } 5277c478bd9Sstevel@tonic-gate zent->zstate_str = zone_state_str(zent->zstate_num); 528bafa7067Snn35248 529bafa7067Snn35248 /* 530bafa7067Snn35248 * A zone's brand is only available in the .xml file describing it, 531bafa7067Snn35248 * which is only visible to the global zone. This causes 532bafa7067Snn35248 * zone_get_brand() to fail when called from within a non-global 533bafa7067Snn35248 * zone. Fortunately we only do this on labeled systems, where we 534bafa7067Snn35248 * know all zones are native. 535bafa7067Snn35248 */ 536bafa7067Snn35248 if (getzoneid() != GLOBAL_ZONEID) { 537bafa7067Snn35248 assert(is_system_labeled() != 0); 538bafa7067Snn35248 (void) strlcpy(zent->zbrand, NATIVE_BRAND_NAME, 539bafa7067Snn35248 sizeof (zent->zbrand)); 540bafa7067Snn35248 } else if (zone_get_brand(zent->zname, zent->zbrand, 5419acbbeafSnn35248 sizeof (zent->zbrand)) != Z_OK) { 5429acbbeafSnn35248 zperror2(zent->zname, gettext("could not get brand name")); 5439acbbeafSnn35248 return (Z_ERR); 5449acbbeafSnn35248 } 5457c478bd9Sstevel@tonic-gate 546f4b3ec61Sdh155122 /* 547f4b3ec61Sdh155122 * Get ip type of the zone. 548f4b3ec61Sdh155122 * Note for global zone, ZS_SHARED is set always. 549f4b3ec61Sdh155122 */ 550f4b3ec61Sdh155122 if (zid == GLOBAL_ZONEID) { 551f4b3ec61Sdh155122 zent->ziptype = ZS_SHARED; 552f4b3ec61Sdh155122 } else { 553f4b3ec61Sdh155122 554f4b3ec61Sdh155122 if (zent->zstate_num == ZONE_STATE_RUNNING) { 555f4b3ec61Sdh155122 ushort_t flags; 556f4b3ec61Sdh155122 557f4b3ec61Sdh155122 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, 558f4b3ec61Sdh155122 sizeof (flags)) < 0) { 559f4b3ec61Sdh155122 zperror2(zent->zname, 560f4b3ec61Sdh155122 gettext("could not get zone flags")); 561f4b3ec61Sdh155122 return (Z_ERR); 562f4b3ec61Sdh155122 } 563f4b3ec61Sdh155122 if (flags & ZF_NET_EXCL) 564f4b3ec61Sdh155122 zent->ziptype = ZS_EXCLUSIVE; 565f4b3ec61Sdh155122 else 566f4b3ec61Sdh155122 zent->ziptype = ZS_SHARED; 567f4b3ec61Sdh155122 } else { 568f4b3ec61Sdh155122 zone_dochandle_t handle; 569f4b3ec61Sdh155122 570f4b3ec61Sdh155122 if ((handle = zonecfg_init_handle()) == NULL) { 571f4b3ec61Sdh155122 zperror2(zent->zname, 572f4b3ec61Sdh155122 gettext("could not init handle")); 573f4b3ec61Sdh155122 return (Z_ERR); 574f4b3ec61Sdh155122 } 575f4b3ec61Sdh155122 if ((err = zonecfg_get_handle(zent->zname, handle)) 576f4b3ec61Sdh155122 != Z_OK) { 577f4b3ec61Sdh155122 zperror2(zent->zname, 578f4b3ec61Sdh155122 gettext("could not get handle")); 579f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 580f4b3ec61Sdh155122 return (Z_ERR); 581f4b3ec61Sdh155122 } 582f4b3ec61Sdh155122 583f4b3ec61Sdh155122 if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) 584f4b3ec61Sdh155122 != Z_OK) { 585f4b3ec61Sdh155122 zperror2(zent->zname, 586f4b3ec61Sdh155122 gettext("could not get ip-type")); 587f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 588f4b3ec61Sdh155122 return (Z_ERR); 589f4b3ec61Sdh155122 } 590f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 591f4b3ec61Sdh155122 } 592f4b3ec61Sdh155122 } 593f4b3ec61Sdh155122 5947c478bd9Sstevel@tonic-gate return (Z_OK); 5957c478bd9Sstevel@tonic-gate } 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate /* 5987c478bd9Sstevel@tonic-gate * fetch_zents() calls zone_list(2) to find out how many zones are running 5997c478bd9Sstevel@tonic-gate * (which is stored in the global nzents), then calls zone_list(2) again 6007c478bd9Sstevel@tonic-gate * to fetch the list of running zones (stored in the global zents). This 6017c478bd9Sstevel@tonic-gate * function may be called multiple times, so if zents is already set, we 6027c478bd9Sstevel@tonic-gate * return immediately to save work. 6037c478bd9Sstevel@tonic-gate */ 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate static int 606108322fbScarlsonj fetch_zents(void) 6077c478bd9Sstevel@tonic-gate { 6087c478bd9Sstevel@tonic-gate zoneid_t *zids = NULL; 6097c478bd9Sstevel@tonic-gate uint_t nzents_saved; 610108322fbScarlsonj int i, retv; 611108322fbScarlsonj FILE *fp; 612108322fbScarlsonj boolean_t inaltroot; 613108322fbScarlsonj zone_entry_t *zentp; 6147c478bd9Sstevel@tonic-gate 6157c478bd9Sstevel@tonic-gate if (nzents > 0) 6167c478bd9Sstevel@tonic-gate return (Z_OK); 6177c478bd9Sstevel@tonic-gate 6187c478bd9Sstevel@tonic-gate if (zone_list(NULL, &nzents) != 0) { 6197c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zoneid list"), B_FALSE); 6207c478bd9Sstevel@tonic-gate return (Z_ERR); 6217c478bd9Sstevel@tonic-gate } 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate again: 6247c478bd9Sstevel@tonic-gate if (nzents == 0) 6257c478bd9Sstevel@tonic-gate return (Z_OK); 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate zids = safe_calloc(nzents, sizeof (zoneid_t)); 6287c478bd9Sstevel@tonic-gate nzents_saved = nzents; 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate if (zone_list(zids, &nzents) != 0) { 6317c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zone list"), B_FALSE); 6327c478bd9Sstevel@tonic-gate free(zids); 6337c478bd9Sstevel@tonic-gate return (Z_ERR); 6347c478bd9Sstevel@tonic-gate } 6357c478bd9Sstevel@tonic-gate if (nzents != nzents_saved) { 6367c478bd9Sstevel@tonic-gate /* list changed, try again */ 6377c478bd9Sstevel@tonic-gate free(zids); 6387c478bd9Sstevel@tonic-gate goto again; 6397c478bd9Sstevel@tonic-gate } 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate zents = safe_calloc(nzents, sizeof (zone_entry_t)); 6427c478bd9Sstevel@tonic-gate 643108322fbScarlsonj inaltroot = zonecfg_in_alt_root(); 644108322fbScarlsonj if (inaltroot) 645108322fbScarlsonj fp = zonecfg_open_scratch("", B_FALSE); 646108322fbScarlsonj else 647108322fbScarlsonj fp = NULL; 648108322fbScarlsonj zentp = zents; 649108322fbScarlsonj retv = Z_OK; 6507c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 6517c478bd9Sstevel@tonic-gate char name[ZONENAME_MAX]; 652108322fbScarlsonj char altname[ZONENAME_MAX]; 6537c478bd9Sstevel@tonic-gate 654108322fbScarlsonj if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) { 6557c478bd9Sstevel@tonic-gate zperror(gettext("failed to get zone name"), B_FALSE); 656108322fbScarlsonj retv = Z_ERR; 657108322fbScarlsonj continue; 6587c478bd9Sstevel@tonic-gate } 659108322fbScarlsonj if (zonecfg_is_scratch(name)) { 660108322fbScarlsonj /* Ignore scratch zones by default */ 661108322fbScarlsonj if (!inaltroot) 662108322fbScarlsonj continue; 663108322fbScarlsonj if (fp == NULL || 664108322fbScarlsonj zonecfg_reverse_scratch(fp, name, altname, 665108322fbScarlsonj sizeof (altname), NULL, 0) == -1) { 6665ee84fbdSgjelinek zerror(gettext("could not resolve scratch " 667108322fbScarlsonj "zone %s"), name); 668108322fbScarlsonj retv = Z_ERR; 669108322fbScarlsonj continue; 670108322fbScarlsonj } 671108322fbScarlsonj (void) strcpy(name, altname); 672108322fbScarlsonj } else { 673108322fbScarlsonj /* Ignore non-scratch when in an alternate root */ 674108322fbScarlsonj if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0) 675108322fbScarlsonj continue; 676108322fbScarlsonj } 677108322fbScarlsonj if (lookup_zone_info(name, zids[i], zentp) != Z_OK) { 678108322fbScarlsonj zerror(gettext("failed to get zone data")); 679108322fbScarlsonj retv = Z_ERR; 680108322fbScarlsonj continue; 681108322fbScarlsonj } 682108322fbScarlsonj zentp++; 683108322fbScarlsonj } 684108322fbScarlsonj nzents = zentp - zents; 685108322fbScarlsonj if (fp != NULL) 686108322fbScarlsonj zonecfg_close_scratch(fp); 6877c478bd9Sstevel@tonic-gate 6887c478bd9Sstevel@tonic-gate free(zids); 689108322fbScarlsonj return (retv); 6907c478bd9Sstevel@tonic-gate } 6917c478bd9Sstevel@tonic-gate 692108322fbScarlsonj static int 6937c478bd9Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable) 6947c478bd9Sstevel@tonic-gate { 6957c478bd9Sstevel@tonic-gate int i; 6967c478bd9Sstevel@tonic-gate zone_entry_t zent; 6977c478bd9Sstevel@tonic-gate FILE *cookie; 6987c478bd9Sstevel@tonic-gate char *name; 6997c478bd9Sstevel@tonic-gate 7007c478bd9Sstevel@tonic-gate /* 7017c478bd9Sstevel@tonic-gate * First get the list of running zones from the kernel and print them. 7027c478bd9Sstevel@tonic-gate * If that is all we need, then return. 7037c478bd9Sstevel@tonic-gate */ 704108322fbScarlsonj if ((i = fetch_zents()) != Z_OK) { 7057c478bd9Sstevel@tonic-gate /* 7067c478bd9Sstevel@tonic-gate * No need for error messages; fetch_zents() has already taken 7077c478bd9Sstevel@tonic-gate * care of this. 7087c478bd9Sstevel@tonic-gate */ 709108322fbScarlsonj return (i); 7107c478bd9Sstevel@tonic-gate } 711108322fbScarlsonj for (i = 0; i < nzents; i++) 7127c478bd9Sstevel@tonic-gate zone_print(&zents[i], verbose, parsable); 7137c478bd9Sstevel@tonic-gate if (min_state >= ZONE_STATE_RUNNING) 714108322fbScarlsonj return (Z_OK); 7157c478bd9Sstevel@tonic-gate /* 7167c478bd9Sstevel@tonic-gate * Next, get the full list of zones from the configuration, skipping 7177c478bd9Sstevel@tonic-gate * any we have already printed. 7187c478bd9Sstevel@tonic-gate */ 7197c478bd9Sstevel@tonic-gate cookie = setzoneent(); 7207c478bd9Sstevel@tonic-gate while ((name = getzoneent(cookie)) != NULL) { 7217c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7227c478bd9Sstevel@tonic-gate if (strcmp(zents[i].zname, name) == 0) 7237c478bd9Sstevel@tonic-gate break; 7247c478bd9Sstevel@tonic-gate } 7257c478bd9Sstevel@tonic-gate if (i < nzents) { 7267c478bd9Sstevel@tonic-gate free(name); 7277c478bd9Sstevel@tonic-gate continue; 7287c478bd9Sstevel@tonic-gate } 729108322fbScarlsonj if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) { 7307c478bd9Sstevel@tonic-gate free(name); 7317c478bd9Sstevel@tonic-gate continue; 7327c478bd9Sstevel@tonic-gate } 7337c478bd9Sstevel@tonic-gate free(name); 7347c478bd9Sstevel@tonic-gate if (zent.zstate_num >= min_state) 7357c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 7367c478bd9Sstevel@tonic-gate } 7377c478bd9Sstevel@tonic-gate endzoneent(cookie); 738108322fbScarlsonj return (Z_OK); 7397c478bd9Sstevel@tonic-gate } 7407c478bd9Sstevel@tonic-gate 7417c478bd9Sstevel@tonic-gate static zone_entry_t * 7427c478bd9Sstevel@tonic-gate lookup_running_zone(char *str) 7437c478bd9Sstevel@tonic-gate { 7447c478bd9Sstevel@tonic-gate zoneid_t zoneid; 7457c478bd9Sstevel@tonic-gate char *cp; 7467c478bd9Sstevel@tonic-gate int i; 7477c478bd9Sstevel@tonic-gate 7487c478bd9Sstevel@tonic-gate if (fetch_zents() != Z_OK) 7497c478bd9Sstevel@tonic-gate return (NULL); 7507c478bd9Sstevel@tonic-gate 7517c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7527c478bd9Sstevel@tonic-gate if (strcmp(str, zents[i].zname) == 0) 7537c478bd9Sstevel@tonic-gate return (&zents[i]); 7547c478bd9Sstevel@tonic-gate } 7557c478bd9Sstevel@tonic-gate errno = 0; 7567c478bd9Sstevel@tonic-gate zoneid = strtol(str, &cp, 0); 7577c478bd9Sstevel@tonic-gate if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID || 7587c478bd9Sstevel@tonic-gate errno != 0 || *cp != '\0') 7597c478bd9Sstevel@tonic-gate return (NULL); 7607c478bd9Sstevel@tonic-gate for (i = 0; i < nzents; i++) { 7617c478bd9Sstevel@tonic-gate if (zoneid == zents[i].zid) 7627c478bd9Sstevel@tonic-gate return (&zents[i]); 7637c478bd9Sstevel@tonic-gate } 7647c478bd9Sstevel@tonic-gate return (NULL); 7657c478bd9Sstevel@tonic-gate } 7667c478bd9Sstevel@tonic-gate 7677c478bd9Sstevel@tonic-gate /* 7687c478bd9Sstevel@tonic-gate * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if 7697c478bd9Sstevel@tonic-gate * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect). 7707c478bd9Sstevel@tonic-gate */ 7717c478bd9Sstevel@tonic-gate static boolean_t 7727c478bd9Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file) 7737c478bd9Sstevel@tonic-gate { 7747c478bd9Sstevel@tonic-gate char *str; 7757c478bd9Sstevel@tonic-gate 7767c478bd9Sstevel@tonic-gate assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR || 7777c478bd9Sstevel@tonic-gate bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP || 7787c478bd9Sstevel@tonic-gate bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH); 7797c478bd9Sstevel@tonic-gate /* 7807c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 7817c478bd9Sstevel@tonic-gate * The strings below will be used as part of a larger message, 7827c478bd9Sstevel@tonic-gate * either: 7837c478bd9Sstevel@tonic-gate * (file name) must be (owner|group|world) (read|writ|execut)able 7847c478bd9Sstevel@tonic-gate * or 7857c478bd9Sstevel@tonic-gate * (file name) must not be (owner|group|world) (read|writ|execut)able 7867c478bd9Sstevel@tonic-gate */ 7877c478bd9Sstevel@tonic-gate switch (bit) { 7887c478bd9Sstevel@tonic-gate case S_IRUSR: 7897c478bd9Sstevel@tonic-gate str = gettext("owner readable"); 7907c478bd9Sstevel@tonic-gate break; 7917c478bd9Sstevel@tonic-gate case S_IWUSR: 7927c478bd9Sstevel@tonic-gate str = gettext("owner writable"); 7937c478bd9Sstevel@tonic-gate break; 7947c478bd9Sstevel@tonic-gate case S_IXUSR: 7957c478bd9Sstevel@tonic-gate str = gettext("owner executable"); 7967c478bd9Sstevel@tonic-gate break; 7977c478bd9Sstevel@tonic-gate case S_IRGRP: 7987c478bd9Sstevel@tonic-gate str = gettext("group readable"); 7997c478bd9Sstevel@tonic-gate break; 8007c478bd9Sstevel@tonic-gate case S_IWGRP: 8017c478bd9Sstevel@tonic-gate str = gettext("group writable"); 8027c478bd9Sstevel@tonic-gate break; 8037c478bd9Sstevel@tonic-gate case S_IXGRP: 8047c478bd9Sstevel@tonic-gate str = gettext("group executable"); 8057c478bd9Sstevel@tonic-gate break; 8067c478bd9Sstevel@tonic-gate case S_IROTH: 8077c478bd9Sstevel@tonic-gate str = gettext("world readable"); 8087c478bd9Sstevel@tonic-gate break; 8097c478bd9Sstevel@tonic-gate case S_IWOTH: 8107c478bd9Sstevel@tonic-gate str = gettext("world writable"); 8117c478bd9Sstevel@tonic-gate break; 8127c478bd9Sstevel@tonic-gate case S_IXOTH: 8137c478bd9Sstevel@tonic-gate str = gettext("world executable"); 8147c478bd9Sstevel@tonic-gate break; 8157c478bd9Sstevel@tonic-gate } 8167c478bd9Sstevel@tonic-gate if ((mode & bit) == (on ? 0 : bit)) { 8177c478bd9Sstevel@tonic-gate /* 8187c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 8197c478bd9Sstevel@tonic-gate * The first parameter below is a file name; the second 8207c478bd9Sstevel@tonic-gate * is one of the "(owner|group|world) (read|writ|execut)able" 8217c478bd9Sstevel@tonic-gate * strings from above. 8227c478bd9Sstevel@tonic-gate */ 8237c478bd9Sstevel@tonic-gate /* 8247c478bd9Sstevel@tonic-gate * The code below could be simplified but not in a way 8257c478bd9Sstevel@tonic-gate * that would easily translate to non-English locales. 8267c478bd9Sstevel@tonic-gate */ 8277c478bd9Sstevel@tonic-gate if (on) { 8287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must be %s.\n"), 8297c478bd9Sstevel@tonic-gate file, str); 8307c478bd9Sstevel@tonic-gate } else { 8317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must not be %s.\n"), 8327c478bd9Sstevel@tonic-gate file, str); 8337c478bd9Sstevel@tonic-gate } 8347c478bd9Sstevel@tonic-gate return (B_TRUE); 8357c478bd9Sstevel@tonic-gate } 8367c478bd9Sstevel@tonic-gate return (B_FALSE); 8377c478bd9Sstevel@tonic-gate } 8387c478bd9Sstevel@tonic-gate 8397c478bd9Sstevel@tonic-gate /* 8407c478bd9Sstevel@tonic-gate * We want to make sure that no zone has its zone path as a child node 8417c478bd9Sstevel@tonic-gate * (in the directory sense) of any other. We do that by comparing this 8427c478bd9Sstevel@tonic-gate * zone's path to the path of all other (non-global) zones. The comparison 8437c478bd9Sstevel@tonic-gate * in each case is simple: add '/' to the end of the path, then do a 8447c478bd9Sstevel@tonic-gate * strncmp() of the two paths, using the length of the shorter one. 8457c478bd9Sstevel@tonic-gate */ 8467c478bd9Sstevel@tonic-gate 8477c478bd9Sstevel@tonic-gate static int 8487c478bd9Sstevel@tonic-gate crosscheck_zonepaths(char *path) 8497c478bd9Sstevel@tonic-gate { 8507c478bd9Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 8517c478bd9Sstevel@tonic-gate char path_copy[MAXPATHLEN]; /* copy of original path */ 8527c478bd9Sstevel@tonic-gate char rpath_copy[MAXPATHLEN]; /* copy of original rpath */ 8537c478bd9Sstevel@tonic-gate struct zoneent *ze; 8547c478bd9Sstevel@tonic-gate int res, err; 8557c478bd9Sstevel@tonic-gate FILE *cookie; 8567c478bd9Sstevel@tonic-gate 8577c478bd9Sstevel@tonic-gate cookie = setzoneent(); 8587c478bd9Sstevel@tonic-gate while ((ze = getzoneent_private(cookie)) != NULL) { 8597c478bd9Sstevel@tonic-gate /* Skip zones which are not installed. */ 8607c478bd9Sstevel@tonic-gate if (ze->zone_state < ZONE_STATE_INSTALLED) { 8617c478bd9Sstevel@tonic-gate free(ze); 8627c478bd9Sstevel@tonic-gate continue; 8637c478bd9Sstevel@tonic-gate } 8647c478bd9Sstevel@tonic-gate /* Skip the global zone and the current target zone. */ 8657c478bd9Sstevel@tonic-gate if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 || 8667c478bd9Sstevel@tonic-gate strcmp(ze->zone_name, target_zone) == 0) { 8677c478bd9Sstevel@tonic-gate free(ze); 8687c478bd9Sstevel@tonic-gate continue; 8697c478bd9Sstevel@tonic-gate } 8707c478bd9Sstevel@tonic-gate if (strlen(ze->zone_path) == 0) { 8717c478bd9Sstevel@tonic-gate /* old index file without path, fall back */ 8727c478bd9Sstevel@tonic-gate if ((err = zone_get_zonepath(ze->zone_name, 8737c478bd9Sstevel@tonic-gate ze->zone_path, sizeof (ze->zone_path))) != Z_OK) { 8747c478bd9Sstevel@tonic-gate errno = err; 8757c478bd9Sstevel@tonic-gate zperror2(ze->zone_name, 8767c478bd9Sstevel@tonic-gate gettext("could not get zone path")); 8777c478bd9Sstevel@tonic-gate free(ze); 8787c478bd9Sstevel@tonic-gate continue; 8797c478bd9Sstevel@tonic-gate } 8807c478bd9Sstevel@tonic-gate } 881108322fbScarlsonj (void) snprintf(path_copy, sizeof (path_copy), "%s%s", 882108322fbScarlsonj zonecfg_get_root(), ze->zone_path); 883108322fbScarlsonj res = resolvepath(path_copy, rpath, sizeof (rpath)); 8847c478bd9Sstevel@tonic-gate if (res == -1) { 8857c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 886108322fbScarlsonj zperror(path_copy, B_FALSE); 8877c478bd9Sstevel@tonic-gate free(ze); 8887c478bd9Sstevel@tonic-gate return (Z_ERR); 8897c478bd9Sstevel@tonic-gate } 8907c478bd9Sstevel@tonic-gate (void) printf(gettext("WARNING: zone %s is installed, " 8917c478bd9Sstevel@tonic-gate "but its %s %s does not exist.\n"), ze->zone_name, 892108322fbScarlsonj "zonepath", path_copy); 8937c478bd9Sstevel@tonic-gate free(ze); 8947c478bd9Sstevel@tonic-gate continue; 8957c478bd9Sstevel@tonic-gate } 8967c478bd9Sstevel@tonic-gate rpath[res] = '\0'; 8977c478bd9Sstevel@tonic-gate (void) snprintf(path_copy, sizeof (path_copy), "%s/", path); 8987c478bd9Sstevel@tonic-gate (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath); 8997c478bd9Sstevel@tonic-gate if (strncmp(path_copy, rpath_copy, 9007c478bd9Sstevel@tonic-gate min(strlen(path_copy), strlen(rpath_copy))) == 0) { 9015ee84fbdSgjelinek /* 9025ee84fbdSgjelinek * TRANSLATION_NOTE 9035ee84fbdSgjelinek * zonepath is a literal that should not be translated. 9045ee84fbdSgjelinek */ 9057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s zonepath (%s) and " 9067c478bd9Sstevel@tonic-gate "%s zonepath (%s) overlap.\n"), 9077c478bd9Sstevel@tonic-gate target_zone, path, ze->zone_name, rpath); 9087c478bd9Sstevel@tonic-gate free(ze); 9097c478bd9Sstevel@tonic-gate return (Z_ERR); 9107c478bd9Sstevel@tonic-gate } 9117c478bd9Sstevel@tonic-gate free(ze); 9127c478bd9Sstevel@tonic-gate } 9137c478bd9Sstevel@tonic-gate endzoneent(cookie); 9147c478bd9Sstevel@tonic-gate return (Z_OK); 9157c478bd9Sstevel@tonic-gate } 9167c478bd9Sstevel@tonic-gate 9177c478bd9Sstevel@tonic-gate static int 9187c478bd9Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num) 9197c478bd9Sstevel@tonic-gate { 9207c478bd9Sstevel@tonic-gate int res; /* result of last library/system call */ 9217c478bd9Sstevel@tonic-gate boolean_t err = B_FALSE; /* have we run into an error? */ 9227c478bd9Sstevel@tonic-gate struct stat stbuf; 9233f2f09c1Sdp struct statvfs64 vfsbuf; 9247c478bd9Sstevel@tonic-gate char rpath[MAXPATHLEN]; /* resolved path */ 9257c478bd9Sstevel@tonic-gate char ppath[MAXPATHLEN]; /* parent path */ 9267c478bd9Sstevel@tonic-gate char rppath[MAXPATHLEN]; /* resolved parent path */ 9277c478bd9Sstevel@tonic-gate char rootpath[MAXPATHLEN]; /* root path */ 9287c478bd9Sstevel@tonic-gate zone_state_t state; 9297c478bd9Sstevel@tonic-gate 9307c478bd9Sstevel@tonic-gate if (path[0] != '/') { 9317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9327c478bd9Sstevel@tonic-gate gettext("%s is not an absolute path.\n"), path); 9337c478bd9Sstevel@tonic-gate return (Z_ERR); 9347c478bd9Sstevel@tonic-gate } 9357c478bd9Sstevel@tonic-gate if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) { 9367c478bd9Sstevel@tonic-gate if ((errno != ENOENT) || 937865e09a4Sgjelinek (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL && 938865e09a4Sgjelinek cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) { 9397c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 9407c478bd9Sstevel@tonic-gate return (Z_ERR); 9417c478bd9Sstevel@tonic-gate } 9427c478bd9Sstevel@tonic-gate if (cmd_num == CMD_VERIFY) { 9435ee84fbdSgjelinek /* 9445ee84fbdSgjelinek * TRANSLATION_NOTE 9455ee84fbdSgjelinek * zoneadm is a literal that should not be translated. 9465ee84fbdSgjelinek */ 9477c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("WARNING: %s does not " 9485ee84fbdSgjelinek "exist, so it could not be verified.\nWhen " 9495ee84fbdSgjelinek "'zoneadm %s' is run, '%s' will try to create\n%s, " 9505ee84fbdSgjelinek "and '%s' will be tried again,\nbut the '%s' may " 9515ee84fbdSgjelinek "fail if:\nthe parent directory of %s is group- or " 9525ee84fbdSgjelinek "other-writable\nor\n%s overlaps with any other " 9537c478bd9Sstevel@tonic-gate "installed zones.\n"), path, 9547c478bd9Sstevel@tonic-gate cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL), 9557c478bd9Sstevel@tonic-gate path, cmd_to_str(CMD_VERIFY), 9567c478bd9Sstevel@tonic-gate cmd_to_str(CMD_VERIFY), path, path); 9577c478bd9Sstevel@tonic-gate return (Z_OK); 9587c478bd9Sstevel@tonic-gate } 9597c478bd9Sstevel@tonic-gate /* 9607c478bd9Sstevel@tonic-gate * The zonepath is supposed to be mode 700 but its 9617c478bd9Sstevel@tonic-gate * parent(s) 755. So use 755 on the mkdirp() then 9627c478bd9Sstevel@tonic-gate * chmod() the zonepath itself to 700. 9637c478bd9Sstevel@tonic-gate */ 9647c478bd9Sstevel@tonic-gate if (mkdirp(path, DEFAULT_DIR_MODE) < 0) { 9657c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 9667c478bd9Sstevel@tonic-gate return (Z_ERR); 9677c478bd9Sstevel@tonic-gate } 9687c478bd9Sstevel@tonic-gate /* 9697c478bd9Sstevel@tonic-gate * If the chmod() fails, report the error, but might 9707c478bd9Sstevel@tonic-gate * as well continue the verify procedure. 9717c478bd9Sstevel@tonic-gate */ 9727c478bd9Sstevel@tonic-gate if (chmod(path, S_IRWXU) != 0) 9737c478bd9Sstevel@tonic-gate zperror(path, B_FALSE); 9747c478bd9Sstevel@tonic-gate /* 9757c478bd9Sstevel@tonic-gate * Since the mkdir() succeeded, we should not have to 9767c478bd9Sstevel@tonic-gate * worry about a subsequent ENOENT, thus this should 9777c478bd9Sstevel@tonic-gate * only recurse once. 9787c478bd9Sstevel@tonic-gate */ 979865e09a4Sgjelinek return (validate_zonepath(path, cmd_num)); 9807c478bd9Sstevel@tonic-gate } 9817c478bd9Sstevel@tonic-gate rpath[res] = '\0'; 9827c478bd9Sstevel@tonic-gate if (strcmp(path, rpath) != 0) { 9837c478bd9Sstevel@tonic-gate errno = Z_RESOLVED_PATH; 9847c478bd9Sstevel@tonic-gate zperror(path, B_TRUE); 9857c478bd9Sstevel@tonic-gate return (Z_ERR); 9867c478bd9Sstevel@tonic-gate } 9877c478bd9Sstevel@tonic-gate if ((res = stat(rpath, &stbuf)) != 0) { 9887c478bd9Sstevel@tonic-gate zperror(rpath, B_FALSE); 9897c478bd9Sstevel@tonic-gate return (Z_ERR); 9907c478bd9Sstevel@tonic-gate } 9917c478bd9Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 9927c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 9937c478bd9Sstevel@tonic-gate rpath); 9947c478bd9Sstevel@tonic-gate return (Z_ERR); 9957c478bd9Sstevel@tonic-gate } 9964fceebdfSblakej if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) { 9977c478bd9Sstevel@tonic-gate (void) printf(gettext("WARNING: %s is on a temporary " 9980b5de56dSgjelinek "file system.\n"), rpath); 9997c478bd9Sstevel@tonic-gate } 10007c478bd9Sstevel@tonic-gate if (crosscheck_zonepaths(rpath) != Z_OK) 10017c478bd9Sstevel@tonic-gate return (Z_ERR); 10027c478bd9Sstevel@tonic-gate /* 10037c478bd9Sstevel@tonic-gate * Try to collect and report as many minor errors as possible 10047c478bd9Sstevel@tonic-gate * before returning, so the user can learn everything that needs 10057c478bd9Sstevel@tonic-gate * to be fixed up front. 10067c478bd9Sstevel@tonic-gate */ 10077c478bd9Sstevel@tonic-gate if (stbuf.st_uid != 0) { 10087c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 10097c478bd9Sstevel@tonic-gate rpath); 10107c478bd9Sstevel@tonic-gate err = B_TRUE; 10117c478bd9Sstevel@tonic-gate } 10127c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath); 10137c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath); 10147c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath); 10157c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath); 10167c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath); 10177c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath); 10187c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath); 10197c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath); 10207c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath); 10217c478bd9Sstevel@tonic-gate 10227c478bd9Sstevel@tonic-gate (void) snprintf(ppath, sizeof (ppath), "%s/..", path); 10237c478bd9Sstevel@tonic-gate if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) { 10247c478bd9Sstevel@tonic-gate zperror(ppath, B_FALSE); 10257c478bd9Sstevel@tonic-gate return (Z_ERR); 10267c478bd9Sstevel@tonic-gate } 10277c478bd9Sstevel@tonic-gate rppath[res] = '\0'; 10287c478bd9Sstevel@tonic-gate if ((res = stat(rppath, &stbuf)) != 0) { 10297c478bd9Sstevel@tonic-gate zperror(rppath, B_FALSE); 10307c478bd9Sstevel@tonic-gate return (Z_ERR); 10317c478bd9Sstevel@tonic-gate } 10327c478bd9Sstevel@tonic-gate /* theoretically impossible */ 10337c478bd9Sstevel@tonic-gate if (!S_ISDIR(stbuf.st_mode)) { 10347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not a directory.\n"), 10357c478bd9Sstevel@tonic-gate rppath); 10367c478bd9Sstevel@tonic-gate return (Z_ERR); 10377c478bd9Sstevel@tonic-gate } 10387c478bd9Sstevel@tonic-gate if (stbuf.st_uid != 0) { 10397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not owned by root.\n"), 10407c478bd9Sstevel@tonic-gate rppath); 10417c478bd9Sstevel@tonic-gate err = B_TRUE; 10427c478bd9Sstevel@tonic-gate } 10437c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath); 10447c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath); 10457c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath); 10467c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath); 10477c478bd9Sstevel@tonic-gate err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath); 10487c478bd9Sstevel@tonic-gate if (strcmp(rpath, rppath) == 0) { 10497c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is its own parent.\n"), 10507c478bd9Sstevel@tonic-gate rppath); 10517c478bd9Sstevel@tonic-gate err = B_TRUE; 10527c478bd9Sstevel@tonic-gate } 10537c478bd9Sstevel@tonic-gate 10543f2f09c1Sdp if (statvfs64(rpath, &vfsbuf) != 0) { 10557c478bd9Sstevel@tonic-gate zperror(rpath, B_FALSE); 10567c478bd9Sstevel@tonic-gate return (Z_ERR); 10577c478bd9Sstevel@tonic-gate } 1058b5abaf04Sgjelinek if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) { 10595ee84fbdSgjelinek /* 10605ee84fbdSgjelinek * TRANSLATION_NOTE 10615ee84fbdSgjelinek * Zonepath and NFS are literals that should not be translated. 10625ee84fbdSgjelinek */ 10635ee84fbdSgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on an NFS " 10640b5de56dSgjelinek "mounted file system.\n" 10650b5de56dSgjelinek "\tA local file system must be used.\n"), rpath); 1066b5abaf04Sgjelinek return (Z_ERR); 1067b5abaf04Sgjelinek } 1068b5abaf04Sgjelinek if (vfsbuf.f_flag & ST_NOSUID) { 10695ee84fbdSgjelinek /* 10705ee84fbdSgjelinek * TRANSLATION_NOTE 10715ee84fbdSgjelinek * Zonepath and nosuid are literals that should not be 10725ee84fbdSgjelinek * translated. 10735ee84fbdSgjelinek */ 1074b5abaf04Sgjelinek (void) fprintf(stderr, gettext("Zonepath %s is on a nosuid " 10750b5de56dSgjelinek "file system.\n"), rpath); 10767c478bd9Sstevel@tonic-gate return (Z_ERR); 10777c478bd9Sstevel@tonic-gate } 10787c478bd9Sstevel@tonic-gate 10797c478bd9Sstevel@tonic-gate if ((res = zone_get_state(target_zone, &state)) != Z_OK) { 10807c478bd9Sstevel@tonic-gate errno = res; 10817c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get state")); 10827c478bd9Sstevel@tonic-gate return (Z_ERR); 10837c478bd9Sstevel@tonic-gate } 10847c478bd9Sstevel@tonic-gate /* 10857c478bd9Sstevel@tonic-gate * The existence of the root path is only bad in the configured state, 10867c478bd9Sstevel@tonic-gate * as it is *supposed* to be there at the installed and later states. 1087ee519a1fSgjelinek * However, the root path is expected to be there if the zone is 1088ee519a1fSgjelinek * detached. 10897c478bd9Sstevel@tonic-gate * State/command mismatches are caught earlier in verify_details(). 10907c478bd9Sstevel@tonic-gate */ 1091ee519a1fSgjelinek if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) { 10927c478bd9Sstevel@tonic-gate if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >= 10937c478bd9Sstevel@tonic-gate sizeof (rootpath)) { 10945ee84fbdSgjelinek /* 10955ee84fbdSgjelinek * TRANSLATION_NOTE 10965ee84fbdSgjelinek * Zonepath is a literal that should not be translated. 10975ee84fbdSgjelinek */ 10987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 10997c478bd9Sstevel@tonic-gate gettext("Zonepath %s is too long.\n"), rpath); 11007c478bd9Sstevel@tonic-gate return (Z_ERR); 11017c478bd9Sstevel@tonic-gate } 11027c478bd9Sstevel@tonic-gate if ((res = stat(rootpath, &stbuf)) == 0) { 1103ee519a1fSgjelinek if (zonecfg_detached(rpath)) 1104ee519a1fSgjelinek (void) fprintf(stderr, 1105ee519a1fSgjelinek gettext("Cannot %s detached " 1106ee519a1fSgjelinek "zone.\nUse attach or remove %s " 1107ee519a1fSgjelinek "directory.\n"), cmd_to_str(cmd_num), 1108ee519a1fSgjelinek rpath); 1109ee519a1fSgjelinek else 1110ee519a1fSgjelinek (void) fprintf(stderr, 1111ee519a1fSgjelinek gettext("Rootpath %s exists; " 1112ee519a1fSgjelinek "remove or move aside prior to %s.\n"), 1113ee519a1fSgjelinek rootpath, cmd_to_str(cmd_num)); 11147c478bd9Sstevel@tonic-gate return (Z_ERR); 11157c478bd9Sstevel@tonic-gate } 11167c478bd9Sstevel@tonic-gate } 11177c478bd9Sstevel@tonic-gate 11187c478bd9Sstevel@tonic-gate return (err ? Z_ERR : Z_OK); 11197c478bd9Sstevel@tonic-gate } 11207c478bd9Sstevel@tonic-gate 11219acbbeafSnn35248 /* 11229acbbeafSnn35248 * The following two routines implement a simple locking mechanism to 11239acbbeafSnn35248 * ensure that only one instance of zoneadm at a time is able to manipulate 11249acbbeafSnn35248 * a given zone. The lock is built on top of an fcntl(2) lock of 11259acbbeafSnn35248 * [<altroot>]/var/run/zones/<zonename>.zoneadm.lock. If a zoneadm instance 11269acbbeafSnn35248 * can grab that lock, it is allowed to manipulate the zone. 11279acbbeafSnn35248 * 11289acbbeafSnn35248 * Since zoneadm may call external applications which in turn invoke 11299acbbeafSnn35248 * zoneadm again, we introduce the notion of "lock inheritance". Any 11309acbbeafSnn35248 * instance of zoneadm that has another instance in its ancestry is assumed 11319acbbeafSnn35248 * to be acting on behalf of the original zoneadm, and is thus allowed to 11329acbbeafSnn35248 * manipulate its zone. 11339acbbeafSnn35248 * 11349acbbeafSnn35248 * This inheritance is implemented via the _ZONEADM_LOCK_HELD environment 11359acbbeafSnn35248 * variable. When zoneadm is granted a lock on its zone, this environment 11369acbbeafSnn35248 * variable is set to 1. When it releases the lock, the variable is set to 11379acbbeafSnn35248 * 0. Since a child process inherits its parent's environment, checking 11389acbbeafSnn35248 * the state of this variable indicates whether or not any ancestor owns 11399acbbeafSnn35248 * the lock. 11409acbbeafSnn35248 */ 11417c478bd9Sstevel@tonic-gate static void 11427c478bd9Sstevel@tonic-gate release_lock_file(int lockfd) 11437c478bd9Sstevel@tonic-gate { 11449acbbeafSnn35248 /* 11459acbbeafSnn35248 * If we are cleaning up from a failed attempt to lock the zone for 11469acbbeafSnn35248 * the first time, we might have a zone_lock_cnt of 0. In that 11479acbbeafSnn35248 * error case, we don't want to do anything but close the lock 11489acbbeafSnn35248 * file. 11499acbbeafSnn35248 */ 11509acbbeafSnn35248 assert(zone_lock_cnt >= 0); 11519acbbeafSnn35248 if (zone_lock_cnt > 0) { 11529acbbeafSnn35248 assert(getenv(LOCK_ENV_VAR) != NULL); 11539acbbeafSnn35248 assert(atoi(getenv(LOCK_ENV_VAR)) == 1); 11549acbbeafSnn35248 if (--zone_lock_cnt > 0) { 11559acbbeafSnn35248 assert(lockfd == -1); 11569acbbeafSnn35248 return; 11579acbbeafSnn35248 } 11589acbbeafSnn35248 if (putenv(zoneadm_lock_not_held) != 0) { 11599acbbeafSnn35248 zperror(target_zone, B_TRUE); 11609acbbeafSnn35248 exit(Z_ERR); 11619acbbeafSnn35248 } 11629acbbeafSnn35248 } 11639acbbeafSnn35248 assert(lockfd >= 0); 11647c478bd9Sstevel@tonic-gate (void) close(lockfd); 11657c478bd9Sstevel@tonic-gate } 11667c478bd9Sstevel@tonic-gate 11677c478bd9Sstevel@tonic-gate static int 11687c478bd9Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd) 11697c478bd9Sstevel@tonic-gate { 11707c478bd9Sstevel@tonic-gate char pathbuf[PATH_MAX]; 11717c478bd9Sstevel@tonic-gate struct flock flock; 11727c478bd9Sstevel@tonic-gate 11739acbbeafSnn35248 /* 11749acbbeafSnn35248 * If we already have the lock, we can skip this expensive song 11759acbbeafSnn35248 * and dance. 11769acbbeafSnn35248 */ 11779acbbeafSnn35248 if (zone_lock_cnt > 0) { 11789acbbeafSnn35248 zone_lock_cnt++; 11799acbbeafSnn35248 *lockfd = -1; 11809acbbeafSnn35248 return (Z_OK); 11819acbbeafSnn35248 } 11829acbbeafSnn35248 assert(getenv(LOCK_ENV_VAR) != NULL); 11839acbbeafSnn35248 assert(atoi(getenv(LOCK_ENV_VAR)) == 0); 11849acbbeafSnn35248 1185108322fbScarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(), 1186108322fbScarlsonj ZONES_TMPDIR) >= sizeof (pathbuf)) { 1187108322fbScarlsonj zerror(gettext("alternate root path is too long")); 1188108322fbScarlsonj return (Z_ERR); 1189108322fbScarlsonj } 1190108322fbScarlsonj if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) { 1191108322fbScarlsonj zerror(gettext("could not mkdir %s: %s"), pathbuf, 11927c478bd9Sstevel@tonic-gate strerror(errno)); 11937c478bd9Sstevel@tonic-gate return (Z_ERR); 11947c478bd9Sstevel@tonic-gate } 1195108322fbScarlsonj (void) chmod(pathbuf, S_IRWXU); 11967c478bd9Sstevel@tonic-gate 11977c478bd9Sstevel@tonic-gate /* 11987c478bd9Sstevel@tonic-gate * One of these lock files is created for each zone (when needed). 11997c478bd9Sstevel@tonic-gate * The lock files are not cleaned up (except on system reboot), 12007c478bd9Sstevel@tonic-gate * but since there is only one per zone, there is no resource 12017c478bd9Sstevel@tonic-gate * starvation issue. 12027c478bd9Sstevel@tonic-gate */ 1203108322fbScarlsonj if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock", 1204108322fbScarlsonj zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) { 1205108322fbScarlsonj zerror(gettext("alternate root path is too long")); 1206108322fbScarlsonj return (Z_ERR); 1207108322fbScarlsonj } 12087c478bd9Sstevel@tonic-gate if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { 12097c478bd9Sstevel@tonic-gate zerror(gettext("could not open %s: %s"), pathbuf, 12107c478bd9Sstevel@tonic-gate strerror(errno)); 12117c478bd9Sstevel@tonic-gate return (Z_ERR); 12127c478bd9Sstevel@tonic-gate } 12137c478bd9Sstevel@tonic-gate /* 12147c478bd9Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmds 12157c478bd9Sstevel@tonic-gate */ 12167c478bd9Sstevel@tonic-gate flock.l_type = F_WRLCK; 12177c478bd9Sstevel@tonic-gate flock.l_whence = SEEK_SET; 12187c478bd9Sstevel@tonic-gate flock.l_start = (off_t)0; 12197c478bd9Sstevel@tonic-gate flock.l_len = (off_t)0; 12209acbbeafSnn35248 if ((fcntl(*lockfd, F_SETLKW, &flock) < 0) || 12219acbbeafSnn35248 (putenv(zoneadm_lock_held) != 0)) { 12227c478bd9Sstevel@tonic-gate zerror(gettext("unable to lock %s: %s"), pathbuf, 12237c478bd9Sstevel@tonic-gate strerror(errno)); 12247c478bd9Sstevel@tonic-gate release_lock_file(*lockfd); 12257c478bd9Sstevel@tonic-gate return (Z_ERR); 12267c478bd9Sstevel@tonic-gate } 12279acbbeafSnn35248 zone_lock_cnt = 1; 12287c478bd9Sstevel@tonic-gate return (Z_OK); 12297c478bd9Sstevel@tonic-gate } 12307c478bd9Sstevel@tonic-gate 1231108322fbScarlsonj static boolean_t 12327c478bd9Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer) 12337c478bd9Sstevel@tonic-gate { 1234108322fbScarlsonj return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH, 1235108322fbScarlsonj zonecfg_get_root(), zone_name) < PATH_MAX); 12367c478bd9Sstevel@tonic-gate } 12377c478bd9Sstevel@tonic-gate 12387c478bd9Sstevel@tonic-gate /* 12397c478bd9Sstevel@tonic-gate * system daemons are not audited. For the global zone, this occurs 12407c478bd9Sstevel@tonic-gate * "naturally" since init is started with the default audit 12417c478bd9Sstevel@tonic-gate * characteristics. Since zoneadmd is a system daemon and it starts 12427c478bd9Sstevel@tonic-gate * init for a zone, it is necessary to clear out the audit 12437c478bd9Sstevel@tonic-gate * characteristics inherited from whomever started zoneadmd. This is 12447c478bd9Sstevel@tonic-gate * indicated by the audit id, which is set from the ruid parameter of 12457c478bd9Sstevel@tonic-gate * adt_set_user(), below. 12467c478bd9Sstevel@tonic-gate */ 12477c478bd9Sstevel@tonic-gate 12487c478bd9Sstevel@tonic-gate static void 12497c478bd9Sstevel@tonic-gate prepare_audit_context() 12507c478bd9Sstevel@tonic-gate { 12517c478bd9Sstevel@tonic-gate adt_session_data_t *ah; 12527c478bd9Sstevel@tonic-gate char *failure = gettext("audit failure: %s"); 12537c478bd9Sstevel@tonic-gate 12547c478bd9Sstevel@tonic-gate if (adt_start_session(&ah, NULL, 0)) { 12557c478bd9Sstevel@tonic-gate zerror(failure, strerror(errno)); 12567c478bd9Sstevel@tonic-gate return; 12577c478bd9Sstevel@tonic-gate } 12587c478bd9Sstevel@tonic-gate if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT, 12597c478bd9Sstevel@tonic-gate ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) { 12607c478bd9Sstevel@tonic-gate zerror(failure, strerror(errno)); 12617c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 12627c478bd9Sstevel@tonic-gate return; 12637c478bd9Sstevel@tonic-gate } 12647c478bd9Sstevel@tonic-gate if (adt_set_proc(ah)) 12657c478bd9Sstevel@tonic-gate zerror(failure, strerror(errno)); 12667c478bd9Sstevel@tonic-gate 12677c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 12687c478bd9Sstevel@tonic-gate } 12697c478bd9Sstevel@tonic-gate 12707c478bd9Sstevel@tonic-gate static int 12717c478bd9Sstevel@tonic-gate start_zoneadmd(const char *zone_name) 12727c478bd9Sstevel@tonic-gate { 12737c478bd9Sstevel@tonic-gate char doorpath[PATH_MAX]; 12747c478bd9Sstevel@tonic-gate pid_t child_pid; 12757c478bd9Sstevel@tonic-gate int error = Z_ERR; 12767c478bd9Sstevel@tonic-gate int doorfd, lockfd; 12777c478bd9Sstevel@tonic-gate struct door_info info; 12787c478bd9Sstevel@tonic-gate 1279108322fbScarlsonj if (!get_doorname(zone_name, doorpath)) 1280108322fbScarlsonj return (Z_ERR); 12817c478bd9Sstevel@tonic-gate 12827c478bd9Sstevel@tonic-gate if (grab_lock_file(zone_name, &lockfd) != Z_OK) 12837c478bd9Sstevel@tonic-gate return (Z_ERR); 12847c478bd9Sstevel@tonic-gate 12857c478bd9Sstevel@tonic-gate /* 12867c478bd9Sstevel@tonic-gate * Now that we have the lock, re-confirm that the daemon is 12877c478bd9Sstevel@tonic-gate * *not* up and working fine. If it is still down, we have a green 12887c478bd9Sstevel@tonic-gate * light to start it. 12897c478bd9Sstevel@tonic-gate */ 12907c478bd9Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 12917c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 12927c478bd9Sstevel@tonic-gate zperror(doorpath, B_FALSE); 12937c478bd9Sstevel@tonic-gate goto out; 12947c478bd9Sstevel@tonic-gate } 12957c478bd9Sstevel@tonic-gate } else { 12967c478bd9Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 12977c478bd9Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 12987c478bd9Sstevel@tonic-gate error = Z_OK; 12997c478bd9Sstevel@tonic-gate (void) close(doorfd); 13007c478bd9Sstevel@tonic-gate goto out; 13017c478bd9Sstevel@tonic-gate } 13027c478bd9Sstevel@tonic-gate (void) close(doorfd); 13037c478bd9Sstevel@tonic-gate } 13047c478bd9Sstevel@tonic-gate 13057c478bd9Sstevel@tonic-gate if ((child_pid = fork()) == -1) { 13067c478bd9Sstevel@tonic-gate zperror(gettext("could not fork"), B_FALSE); 13077c478bd9Sstevel@tonic-gate goto out; 13087c478bd9Sstevel@tonic-gate } else if (child_pid == 0) { 1309108322fbScarlsonj const char *argv[6], **ap; 13107c478bd9Sstevel@tonic-gate 1311108322fbScarlsonj /* child process */ 13127c478bd9Sstevel@tonic-gate prepare_audit_context(); 13137c478bd9Sstevel@tonic-gate 1314108322fbScarlsonj ap = argv; 1315108322fbScarlsonj *ap++ = "zoneadmd"; 1316108322fbScarlsonj *ap++ = "-z"; 1317108322fbScarlsonj *ap++ = zone_name; 1318108322fbScarlsonj if (zonecfg_in_alt_root()) { 1319108322fbScarlsonj *ap++ = "-R"; 1320108322fbScarlsonj *ap++ = zonecfg_get_root(); 1321108322fbScarlsonj } 1322108322fbScarlsonj *ap = NULL; 1323108322fbScarlsonj 1324108322fbScarlsonj (void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv); 13255ee84fbdSgjelinek /* 13265ee84fbdSgjelinek * TRANSLATION_NOTE 13275ee84fbdSgjelinek * zoneadmd is a literal that should not be translated. 13285ee84fbdSgjelinek */ 13297c478bd9Sstevel@tonic-gate zperror(gettext("could not exec zoneadmd"), B_FALSE); 13307c478bd9Sstevel@tonic-gate _exit(Z_ERR); 13317c478bd9Sstevel@tonic-gate } else { 13327c478bd9Sstevel@tonic-gate /* parent process */ 13337c478bd9Sstevel@tonic-gate pid_t retval; 13347c478bd9Sstevel@tonic-gate int pstatus = 0; 13357c478bd9Sstevel@tonic-gate 13367c478bd9Sstevel@tonic-gate do { 13377c478bd9Sstevel@tonic-gate retval = waitpid(child_pid, &pstatus, 0); 13387c478bd9Sstevel@tonic-gate } while (retval != child_pid); 13397c478bd9Sstevel@tonic-gate if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) && 13407c478bd9Sstevel@tonic-gate WEXITSTATUS(pstatus) != 0)) { 13417c478bd9Sstevel@tonic-gate zerror(gettext("could not start %s"), "zoneadmd"); 13427c478bd9Sstevel@tonic-gate goto out; 13437c478bd9Sstevel@tonic-gate } 13447c478bd9Sstevel@tonic-gate } 13457c478bd9Sstevel@tonic-gate error = Z_OK; 13467c478bd9Sstevel@tonic-gate out: 13477c478bd9Sstevel@tonic-gate release_lock_file(lockfd); 13487c478bd9Sstevel@tonic-gate return (error); 13497c478bd9Sstevel@tonic-gate } 13507c478bd9Sstevel@tonic-gate 13517c478bd9Sstevel@tonic-gate static int 13527c478bd9Sstevel@tonic-gate ping_zoneadmd(const char *zone_name) 13537c478bd9Sstevel@tonic-gate { 13547c478bd9Sstevel@tonic-gate char doorpath[PATH_MAX]; 13557c478bd9Sstevel@tonic-gate int doorfd; 13567c478bd9Sstevel@tonic-gate struct door_info info; 13577c478bd9Sstevel@tonic-gate 1358108322fbScarlsonj if (!get_doorname(zone_name, doorpath)) 1359108322fbScarlsonj return (Z_ERR); 13607c478bd9Sstevel@tonic-gate 13617c478bd9Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 13627c478bd9Sstevel@tonic-gate return (Z_ERR); 13637c478bd9Sstevel@tonic-gate } 13647c478bd9Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 && 13657c478bd9Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) { 13667c478bd9Sstevel@tonic-gate (void) close(doorfd); 13677c478bd9Sstevel@tonic-gate return (Z_OK); 13687c478bd9Sstevel@tonic-gate } 13697c478bd9Sstevel@tonic-gate (void) close(doorfd); 13707c478bd9Sstevel@tonic-gate return (Z_ERR); 13717c478bd9Sstevel@tonic-gate } 13727c478bd9Sstevel@tonic-gate 13737c478bd9Sstevel@tonic-gate static int 13747c478bd9Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg) 13757c478bd9Sstevel@tonic-gate { 13767c478bd9Sstevel@tonic-gate char doorpath[PATH_MAX]; 13777c478bd9Sstevel@tonic-gate int doorfd, result; 13787c478bd9Sstevel@tonic-gate door_arg_t darg; 13797c478bd9Sstevel@tonic-gate 13807c478bd9Sstevel@tonic-gate zoneid_t zoneid; 13817c478bd9Sstevel@tonic-gate uint64_t uniqid = 0; 13827c478bd9Sstevel@tonic-gate 13837c478bd9Sstevel@tonic-gate zone_cmd_rval_t *rvalp; 13847c478bd9Sstevel@tonic-gate size_t rlen; 13857c478bd9Sstevel@tonic-gate char *cp, *errbuf; 13867c478bd9Sstevel@tonic-gate 13877c478bd9Sstevel@tonic-gate rlen = getpagesize(); 13887c478bd9Sstevel@tonic-gate if ((rvalp = malloc(rlen)) == NULL) { 13897c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rlen, 13907c478bd9Sstevel@tonic-gate strerror(errno)); 13917c478bd9Sstevel@tonic-gate return (-1); 13927c478bd9Sstevel@tonic-gate } 13937c478bd9Sstevel@tonic-gate 13947c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) { 13957c478bd9Sstevel@tonic-gate (void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid, 13967c478bd9Sstevel@tonic-gate sizeof (uniqid)); 13977c478bd9Sstevel@tonic-gate } 13987c478bd9Sstevel@tonic-gate arg->uniqid = uniqid; 13997c478bd9Sstevel@tonic-gate (void) strlcpy(arg->locale, locale, sizeof (arg->locale)); 1400108322fbScarlsonj if (!get_doorname(zone_name, doorpath)) { 1401108322fbScarlsonj zerror(gettext("alternate root path is too long")); 1402108322fbScarlsonj free(rvalp); 1403108322fbScarlsonj return (-1); 1404108322fbScarlsonj } 14057c478bd9Sstevel@tonic-gate 14067c478bd9Sstevel@tonic-gate /* 14077c478bd9Sstevel@tonic-gate * Loop trying to start zoneadmd; if something goes seriously 14087c478bd9Sstevel@tonic-gate * wrong we break out and fail. 14097c478bd9Sstevel@tonic-gate */ 14107c478bd9Sstevel@tonic-gate for (;;) { 14117c478bd9Sstevel@tonic-gate if (start_zoneadmd(zone_name) != Z_OK) 14127c478bd9Sstevel@tonic-gate break; 14137c478bd9Sstevel@tonic-gate 14147c478bd9Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 14157c478bd9Sstevel@tonic-gate zperror(gettext("failed to open zone door"), B_FALSE); 14167c478bd9Sstevel@tonic-gate break; 14177c478bd9Sstevel@tonic-gate } 14187c478bd9Sstevel@tonic-gate 14197c478bd9Sstevel@tonic-gate darg.data_ptr = (char *)arg; 14207c478bd9Sstevel@tonic-gate darg.data_size = sizeof (*arg); 14217c478bd9Sstevel@tonic-gate darg.desc_ptr = NULL; 14227c478bd9Sstevel@tonic-gate darg.desc_num = 0; 14237c478bd9Sstevel@tonic-gate darg.rbuf = (char *)rvalp; 14247c478bd9Sstevel@tonic-gate darg.rsize = rlen; 14257c478bd9Sstevel@tonic-gate if (door_call(doorfd, &darg) != 0) { 14267c478bd9Sstevel@tonic-gate (void) close(doorfd); 14277c478bd9Sstevel@tonic-gate /* 14287c478bd9Sstevel@tonic-gate * We'll get EBADF if the door has been revoked. 14297c478bd9Sstevel@tonic-gate */ 14307c478bd9Sstevel@tonic-gate if (errno != EBADF) { 14317c478bd9Sstevel@tonic-gate zperror(gettext("door_call failed"), B_FALSE); 14327c478bd9Sstevel@tonic-gate break; 14337c478bd9Sstevel@tonic-gate } 14347c478bd9Sstevel@tonic-gate continue; /* take another lap */ 14357c478bd9Sstevel@tonic-gate } 14367c478bd9Sstevel@tonic-gate (void) close(doorfd); 14377c478bd9Sstevel@tonic-gate 14387c478bd9Sstevel@tonic-gate if (darg.data_size == 0) { 14397c478bd9Sstevel@tonic-gate /* Door server is going away; kick it again. */ 14407c478bd9Sstevel@tonic-gate continue; 14417c478bd9Sstevel@tonic-gate } 14427c478bd9Sstevel@tonic-gate 14437c478bd9Sstevel@tonic-gate errbuf = rvalp->errbuf; 14447c478bd9Sstevel@tonic-gate while (*errbuf != '\0') { 14457c478bd9Sstevel@tonic-gate /* 14467c478bd9Sstevel@tonic-gate * Remove any newlines since zerror() 14477c478bd9Sstevel@tonic-gate * will append one automatically. 14487c478bd9Sstevel@tonic-gate */ 14497c478bd9Sstevel@tonic-gate cp = strchr(errbuf, '\n'); 14507c478bd9Sstevel@tonic-gate if (cp != NULL) 14517c478bd9Sstevel@tonic-gate *cp = '\0'; 14527c478bd9Sstevel@tonic-gate zerror("%s", errbuf); 14537c478bd9Sstevel@tonic-gate if (cp == NULL) 14547c478bd9Sstevel@tonic-gate break; 14557c478bd9Sstevel@tonic-gate errbuf = cp + 1; 14567c478bd9Sstevel@tonic-gate } 14577c478bd9Sstevel@tonic-gate result = rvalp->rval == 0 ? 0 : -1; 14587c478bd9Sstevel@tonic-gate free(rvalp); 14597c478bd9Sstevel@tonic-gate return (result); 14607c478bd9Sstevel@tonic-gate } 14617c478bd9Sstevel@tonic-gate 14627c478bd9Sstevel@tonic-gate free(rvalp); 14637c478bd9Sstevel@tonic-gate return (-1); 14647c478bd9Sstevel@tonic-gate } 14657c478bd9Sstevel@tonic-gate 14667c478bd9Sstevel@tonic-gate static int 1467ce28b40eSzt129084 invoke_brand_handler(int cmd_num, char *argv[]) 1468ce28b40eSzt129084 { 1469ce28b40eSzt129084 zone_dochandle_t handle; 1470ce28b40eSzt129084 int err; 1471ce28b40eSzt129084 1472ce28b40eSzt129084 if ((handle = zonecfg_init_handle()) == NULL) { 1473ce28b40eSzt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 1474ce28b40eSzt129084 return (Z_ERR); 1475ce28b40eSzt129084 } 1476ce28b40eSzt129084 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 1477ce28b40eSzt129084 errno = err; 1478ce28b40eSzt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 1479ce28b40eSzt129084 zonecfg_fini_handle(handle); 1480ce28b40eSzt129084 return (Z_ERR); 1481ce28b40eSzt129084 } 1482ce28b40eSzt129084 if (verify_brand(handle, cmd_num, argv) != Z_OK) { 1483ce28b40eSzt129084 zonecfg_fini_handle(handle); 1484ce28b40eSzt129084 return (Z_ERR); 1485ce28b40eSzt129084 } 1486ce28b40eSzt129084 zonecfg_fini_handle(handle); 1487ce28b40eSzt129084 return (Z_OK); 1488ce28b40eSzt129084 } 1489ce28b40eSzt129084 1490ce28b40eSzt129084 static int 14917c478bd9Sstevel@tonic-gate ready_func(int argc, char *argv[]) 14927c478bd9Sstevel@tonic-gate { 14937c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 14947c478bd9Sstevel@tonic-gate int arg; 14957c478bd9Sstevel@tonic-gate 1496108322fbScarlsonj if (zonecfg_in_alt_root()) { 1497108322fbScarlsonj zerror(gettext("cannot ready zone in alternate root")); 1498108322fbScarlsonj return (Z_ERR); 1499108322fbScarlsonj } 1500108322fbScarlsonj 15017c478bd9Sstevel@tonic-gate optind = 0; 15027c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 15037c478bd9Sstevel@tonic-gate switch (arg) { 15047c478bd9Sstevel@tonic-gate case '?': 15057c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 15067c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 15077c478bd9Sstevel@tonic-gate default: 15087c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 15097c478bd9Sstevel@tonic-gate return (Z_USAGE); 15107c478bd9Sstevel@tonic-gate } 15117c478bd9Sstevel@tonic-gate } 15127c478bd9Sstevel@tonic-gate if (argc > optind) { 15137c478bd9Sstevel@tonic-gate sub_usage(SHELP_READY, CMD_READY); 15147c478bd9Sstevel@tonic-gate return (Z_USAGE); 15157c478bd9Sstevel@tonic-gate } 15169acbbeafSnn35248 if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE) 15179acbbeafSnn35248 != Z_OK) 15187c478bd9Sstevel@tonic-gate return (Z_ERR); 1519ce28b40eSzt129084 if (verify_details(CMD_READY, argv) != Z_OK) 15207c478bd9Sstevel@tonic-gate return (Z_ERR); 15217c478bd9Sstevel@tonic-gate 15227c478bd9Sstevel@tonic-gate zarg.cmd = Z_READY; 15237c478bd9Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 15247c478bd9Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 15257c478bd9Sstevel@tonic-gate return (Z_ERR); 15267c478bd9Sstevel@tonic-gate } 15277c478bd9Sstevel@tonic-gate return (Z_OK); 15287c478bd9Sstevel@tonic-gate } 15297c478bd9Sstevel@tonic-gate 15307c478bd9Sstevel@tonic-gate static int 15317c478bd9Sstevel@tonic-gate boot_func(int argc, char *argv[]) 15327c478bd9Sstevel@tonic-gate { 15337c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 15349acbbeafSnn35248 boolean_t force = B_FALSE; 15357c478bd9Sstevel@tonic-gate int arg; 15367c478bd9Sstevel@tonic-gate 1537108322fbScarlsonj if (zonecfg_in_alt_root()) { 1538108322fbScarlsonj zerror(gettext("cannot boot zone in alternate root")); 1539108322fbScarlsonj return (Z_ERR); 1540108322fbScarlsonj } 1541108322fbScarlsonj 15427c478bd9Sstevel@tonic-gate zarg.bootbuf[0] = '\0'; 15437c478bd9Sstevel@tonic-gate 15447c478bd9Sstevel@tonic-gate /* 15453f2f09c1Sdp * The following getopt processes arguments to zone boot; that 15463f2f09c1Sdp * is to say, the [here] portion of the argument string: 15473f2f09c1Sdp * 15483f2f09c1Sdp * zoneadm -z myzone boot [here] -- -v -m verbose 15493f2f09c1Sdp * 15503f2f09c1Sdp * Where [here] can either be nothing, -? (in which case we bail 15519acbbeafSnn35248 * and print usage), -f (a private option to indicate that the 15529acbbeafSnn35248 * boot operation should be 'forced'), or -s. Support for -s is 15539acbbeafSnn35248 * vestigal and obsolete, but is retained because it was a 15549acbbeafSnn35248 * documented interface and there are known consumers including 15559acbbeafSnn35248 * admin/install; the proper way to specify boot arguments like -s 15569acbbeafSnn35248 * is: 15573f2f09c1Sdp * 15583f2f09c1Sdp * zoneadm -z myzone boot -- -s -v -m verbose. 15597c478bd9Sstevel@tonic-gate */ 15607c478bd9Sstevel@tonic-gate optind = 0; 15619acbbeafSnn35248 while ((arg = getopt(argc, argv, "?fs")) != EOF) { 15627c478bd9Sstevel@tonic-gate switch (arg) { 15637c478bd9Sstevel@tonic-gate case '?': 15647c478bd9Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 15657c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 15667c478bd9Sstevel@tonic-gate case 's': 15677c478bd9Sstevel@tonic-gate (void) strlcpy(zarg.bootbuf, "-s", 15687c478bd9Sstevel@tonic-gate sizeof (zarg.bootbuf)); 15697c478bd9Sstevel@tonic-gate break; 15709acbbeafSnn35248 case 'f': 15719acbbeafSnn35248 force = B_TRUE; 15729acbbeafSnn35248 break; 15737c478bd9Sstevel@tonic-gate default: 15747c478bd9Sstevel@tonic-gate sub_usage(SHELP_BOOT, CMD_BOOT); 15757c478bd9Sstevel@tonic-gate return (Z_USAGE); 15767c478bd9Sstevel@tonic-gate } 15777c478bd9Sstevel@tonic-gate } 15783f2f09c1Sdp 15793f2f09c1Sdp for (; optind < argc; optind++) { 15803f2f09c1Sdp if (strlcat(zarg.bootbuf, argv[optind], 15813f2f09c1Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 15823f2f09c1Sdp zerror(gettext("Boot argument list too long")); 15833f2f09c1Sdp return (Z_ERR); 15847c478bd9Sstevel@tonic-gate } 15853f2f09c1Sdp if (optind < argc - 1) 15863f2f09c1Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 15873f2f09c1Sdp sizeof (zarg.bootbuf)) { 15883f2f09c1Sdp zerror(gettext("Boot argument list too long")); 15893f2f09c1Sdp return (Z_ERR); 15903f2f09c1Sdp } 15913f2f09c1Sdp } 15929acbbeafSnn35248 if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force) 15939acbbeafSnn35248 != Z_OK) 15947c478bd9Sstevel@tonic-gate return (Z_ERR); 1595ce28b40eSzt129084 if (verify_details(CMD_BOOT, argv) != Z_OK) 15967c478bd9Sstevel@tonic-gate return (Z_ERR); 15979acbbeafSnn35248 zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT; 15987c478bd9Sstevel@tonic-gate if (call_zoneadmd(target_zone, &zarg) != 0) { 15997c478bd9Sstevel@tonic-gate zerror(gettext("call to %s failed"), "zoneadmd"); 16007c478bd9Sstevel@tonic-gate return (Z_ERR); 16017c478bd9Sstevel@tonic-gate } 16020209230bSgjelinek 16037c478bd9Sstevel@tonic-gate return (Z_OK); 16047c478bd9Sstevel@tonic-gate } 16057c478bd9Sstevel@tonic-gate 16067c478bd9Sstevel@tonic-gate static void 16077c478bd9Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr) 16087c478bd9Sstevel@tonic-gate { 16097c478bd9Sstevel@tonic-gate ssize_t result; 1610555afedfScarlsonj uuid_t uuid; 1611555afedfScarlsonj FILE *fp; 1612f4b3ec61Sdh155122 ushort_t flags; 1613555afedfScarlsonj 1614555afedfScarlsonj (void) memset(zeptr, 0, sizeof (*zeptr)); 16157c478bd9Sstevel@tonic-gate 16167c478bd9Sstevel@tonic-gate zeptr->zid = zid; 1617555afedfScarlsonj 16187c478bd9Sstevel@tonic-gate /* 16197c478bd9Sstevel@tonic-gate * Since we're looking up our own (non-global) zone name, 16207c478bd9Sstevel@tonic-gate * we can be assured that it will succeed. 16217c478bd9Sstevel@tonic-gate */ 16227c478bd9Sstevel@tonic-gate result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname)); 16237c478bd9Sstevel@tonic-gate assert(result >= 0); 1624555afedfScarlsonj if (zonecfg_is_scratch(zeptr->zname) && 1625555afedfScarlsonj (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) { 1626555afedfScarlsonj (void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname, 1627555afedfScarlsonj sizeof (zeptr->zname), NULL, 0); 1628555afedfScarlsonj zonecfg_close_scratch(fp); 1629555afedfScarlsonj } 1630555afedfScarlsonj 1631555afedfScarlsonj if (is_system_labeled()) { 163245916cd2Sjpk (void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot, 163345916cd2Sjpk sizeof (zeptr->zroot)); 16349acbbeafSnn35248 (void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME, 16359acbbeafSnn35248 sizeof (zeptr->zbrand)); 1636555afedfScarlsonj } else { 1637555afedfScarlsonj (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot)); 16389acbbeafSnn35248 (void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand, 16399acbbeafSnn35248 sizeof (zeptr->zbrand)); 164045916cd2Sjpk } 1641555afedfScarlsonj 16427c478bd9Sstevel@tonic-gate zeptr->zstate_str = "running"; 1643555afedfScarlsonj if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK && 1644555afedfScarlsonj !uuid_is_null(uuid)) 1645555afedfScarlsonj uuid_unparse(uuid, zeptr->zuuid); 1646f4b3ec61Sdh155122 1647f4b3ec61Sdh155122 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) { 1648f4b3ec61Sdh155122 zperror2(zeptr->zname, gettext("could not get zone flags")); 1649f4b3ec61Sdh155122 exit(Z_ERR); 1650f4b3ec61Sdh155122 } 1651f4b3ec61Sdh155122 if (flags & ZF_NET_EXCL) 1652f4b3ec61Sdh155122 zeptr->ziptype = ZS_EXCLUSIVE; 1653f4b3ec61Sdh155122 else 1654f4b3ec61Sdh155122 zeptr->ziptype = ZS_SHARED; 16557c478bd9Sstevel@tonic-gate } 16567c478bd9Sstevel@tonic-gate 16577c478bd9Sstevel@tonic-gate static int 16587c478bd9Sstevel@tonic-gate list_func(int argc, char *argv[]) 16597c478bd9Sstevel@tonic-gate { 16607c478bd9Sstevel@tonic-gate zone_entry_t *zentp, zent; 1661108322fbScarlsonj int arg, retv; 16627c478bd9Sstevel@tonic-gate boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE; 16637c478bd9Sstevel@tonic-gate zone_state_t min_state = ZONE_STATE_RUNNING; 16647c478bd9Sstevel@tonic-gate zoneid_t zone_id = getzoneid(); 16657c478bd9Sstevel@tonic-gate 16667c478bd9Sstevel@tonic-gate if (target_zone == NULL) { 16677c478bd9Sstevel@tonic-gate /* all zones: default view to running but allow override */ 16687c478bd9Sstevel@tonic-gate optind = 0; 16697c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?cipv")) != EOF) { 16707c478bd9Sstevel@tonic-gate switch (arg) { 16717c478bd9Sstevel@tonic-gate case '?': 16727c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 16737c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 1674475d78a4Sjonb /* 1675475d78a4Sjonb * The 'i' and 'c' options are not mutually 1676475d78a4Sjonb * exclusive so if 'c' is given, then min_state 1677475d78a4Sjonb * is set to 0 (ZONE_STATE_CONFIGURED) which is 1678475d78a4Sjonb * the lowest possible state. If 'i' is given, 1679475d78a4Sjonb * then min_state is set to be the lowest state 1680475d78a4Sjonb * so far. 1681475d78a4Sjonb */ 16827c478bd9Sstevel@tonic-gate case 'c': 16837c478bd9Sstevel@tonic-gate min_state = ZONE_STATE_CONFIGURED; 16847c478bd9Sstevel@tonic-gate break; 16857c478bd9Sstevel@tonic-gate case 'i': 1686475d78a4Sjonb min_state = min(ZONE_STATE_INSTALLED, 1687475d78a4Sjonb min_state); 1688475d78a4Sjonb 16897c478bd9Sstevel@tonic-gate break; 16907c478bd9Sstevel@tonic-gate case 'p': 16917c478bd9Sstevel@tonic-gate parsable = B_TRUE; 16927c478bd9Sstevel@tonic-gate break; 16937c478bd9Sstevel@tonic-gate case 'v': 16947c478bd9Sstevel@tonic-gate verbose = B_TRUE; 16957c478bd9Sstevel@tonic-gate break; 16967c478bd9Sstevel@tonic-gate default: 16977c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 16987c478bd9Sstevel@tonic-gate return (Z_USAGE); 16997c478bd9Sstevel@tonic-gate } 17007c478bd9Sstevel@tonic-gate } 17017c478bd9Sstevel@tonic-gate if (parsable && verbose) { 17027c478bd9Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 17037c478bd9Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 17047c478bd9Sstevel@tonic-gate return (Z_ERR); 17057c478bd9Sstevel@tonic-gate } 170645916cd2Sjpk if (zone_id == GLOBAL_ZONEID || is_system_labeled()) { 1707108322fbScarlsonj retv = zone_print_list(min_state, verbose, parsable); 17087c478bd9Sstevel@tonic-gate } else { 17097c478bd9Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 17109acbbeafSnn35248 retv = Z_OK; 17117c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 17127c478bd9Sstevel@tonic-gate } 1713108322fbScarlsonj return (retv); 17147c478bd9Sstevel@tonic-gate } 17157c478bd9Sstevel@tonic-gate 17167c478bd9Sstevel@tonic-gate /* 17177c478bd9Sstevel@tonic-gate * Specific target zone: disallow -i/-c suboptions. 17187c478bd9Sstevel@tonic-gate */ 17197c478bd9Sstevel@tonic-gate optind = 0; 17207c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?pv")) != EOF) { 17217c478bd9Sstevel@tonic-gate switch (arg) { 17227c478bd9Sstevel@tonic-gate case '?': 17237c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 17247c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 17257c478bd9Sstevel@tonic-gate case 'p': 17267c478bd9Sstevel@tonic-gate parsable = B_TRUE; 17277c478bd9Sstevel@tonic-gate break; 17287c478bd9Sstevel@tonic-gate case 'v': 17297c478bd9Sstevel@tonic-gate verbose = B_TRUE; 17307c478bd9Sstevel@tonic-gate break; 17317c478bd9Sstevel@tonic-gate default: 17327c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 17337c478bd9Sstevel@tonic-gate return (Z_USAGE); 17347c478bd9Sstevel@tonic-gate } 17357c478bd9Sstevel@tonic-gate } 17367c478bd9Sstevel@tonic-gate if (parsable && verbose) { 17377c478bd9Sstevel@tonic-gate zerror(gettext("%s -p and -v are mutually exclusive."), 17387c478bd9Sstevel@tonic-gate cmd_to_str(CMD_LIST)); 17397c478bd9Sstevel@tonic-gate return (Z_ERR); 17407c478bd9Sstevel@tonic-gate } 17417c478bd9Sstevel@tonic-gate if (argc > optind) { 17427c478bd9Sstevel@tonic-gate sub_usage(SHELP_LIST, CMD_LIST); 17437c478bd9Sstevel@tonic-gate return (Z_USAGE); 17447c478bd9Sstevel@tonic-gate } 17452ec67e04Sgjelinek if (zone_id != GLOBAL_ZONEID && !is_system_labeled()) { 17467c478bd9Sstevel@tonic-gate fake_up_local_zone(zone_id, &zent); 17477c478bd9Sstevel@tonic-gate /* 17487c478bd9Sstevel@tonic-gate * main() will issue a Z_NO_ZONE error if it cannot get an 17497c478bd9Sstevel@tonic-gate * id for target_zone, which in a non-global zone should 17507c478bd9Sstevel@tonic-gate * happen for any zone name except `zonename`. Thus we 17517c478bd9Sstevel@tonic-gate * assert() that here but don't otherwise check. 17527c478bd9Sstevel@tonic-gate */ 17537c478bd9Sstevel@tonic-gate assert(strcmp(zent.zname, target_zone) == 0); 17547c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 17557c478bd9Sstevel@tonic-gate output = B_TRUE; 17567c478bd9Sstevel@tonic-gate } else if ((zentp = lookup_running_zone(target_zone)) != NULL) { 17577c478bd9Sstevel@tonic-gate zone_print(zentp, verbose, parsable); 17587c478bd9Sstevel@tonic-gate output = B_TRUE; 1759108322fbScarlsonj } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED, 1760108322fbScarlsonj &zent) == Z_OK) { 17617c478bd9Sstevel@tonic-gate zone_print(&zent, verbose, parsable); 17627c478bd9Sstevel@tonic-gate output = B_TRUE; 17637c478bd9Sstevel@tonic-gate } 1764ce28b40eSzt129084 1765ce28b40eSzt129084 /* 1766ce28b40eSzt129084 * Invoke brand-specific handler. Note that we do this 1767db628066Sgjelinek * only if we're in the global zone, and target_zone is specified 1768db628066Sgjelinek * and it is not the global zone. 1769ce28b40eSzt129084 */ 1770db628066Sgjelinek if (zone_id == GLOBAL_ZONEID && target_zone != NULL && 1771db628066Sgjelinek strcmp(target_zone, GLOBAL_ZONENAME) != 0) 1772ce28b40eSzt129084 if (invoke_brand_handler(CMD_LIST, argv) != Z_OK) 1773ce28b40eSzt129084 return (Z_ERR); 1774ce28b40eSzt129084 17757c478bd9Sstevel@tonic-gate return (output ? Z_OK : Z_ERR); 17767c478bd9Sstevel@tonic-gate } 17777c478bd9Sstevel@tonic-gate 17787c478bd9Sstevel@tonic-gate static void 17797c478bd9Sstevel@tonic-gate sigterm(int sig) 17807c478bd9Sstevel@tonic-gate { 17817c478bd9Sstevel@tonic-gate /* 17827c478bd9Sstevel@tonic-gate * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop, 17837c478bd9Sstevel@tonic-gate * then propagate the signal to our process group. 17847c478bd9Sstevel@tonic-gate */ 17859acbbeafSnn35248 assert(sig == SIGINT || sig == SIGTERM); 17867c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 17877c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_IGN); 17887c478bd9Sstevel@tonic-gate (void) kill(0, sig); 17897c478bd9Sstevel@tonic-gate child_killed = B_TRUE; 17907c478bd9Sstevel@tonic-gate } 17917c478bd9Sstevel@tonic-gate 17927c478bd9Sstevel@tonic-gate static int 17937c478bd9Sstevel@tonic-gate do_subproc(char *cmdbuf) 17947c478bd9Sstevel@tonic-gate { 17957c478bd9Sstevel@tonic-gate char inbuf[1024]; /* arbitrary large amount */ 17967c478bd9Sstevel@tonic-gate FILE *file; 17977c478bd9Sstevel@tonic-gate 17989acbbeafSnn35248 do_subproc_cnt++; 17997c478bd9Sstevel@tonic-gate child_killed = B_FALSE; 18007c478bd9Sstevel@tonic-gate /* 18017c478bd9Sstevel@tonic-gate * We use popen(3c) to launch child processes for [un]install; 18027c478bd9Sstevel@tonic-gate * this library call does not return a PID, so we have to kill 18037c478bd9Sstevel@tonic-gate * the whole process group. To avoid killing our parent, we 18047c478bd9Sstevel@tonic-gate * become a process group leader here. But doing so can wreak 18057c478bd9Sstevel@tonic-gate * havoc with reading from stdin when launched by a non-job-control 18067c478bd9Sstevel@tonic-gate * shell, so we close stdin and reopen it as /dev/null first. 18077c478bd9Sstevel@tonic-gate */ 18087c478bd9Sstevel@tonic-gate (void) close(STDIN_FILENO); 18099acbbeafSnn35248 (void) openat(STDIN_FILENO, "/dev/null", O_RDONLY); 18109acbbeafSnn35248 if (!zoneadm_is_nested) 18117c478bd9Sstevel@tonic-gate (void) setpgid(0, 0); 18127c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, sigterm); 18137c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, sigterm); 18147c478bd9Sstevel@tonic-gate file = popen(cmdbuf, "r"); 18157c478bd9Sstevel@tonic-gate for (;;) { 18167c478bd9Sstevel@tonic-gate if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL) 18177c478bd9Sstevel@tonic-gate break; 18187c478bd9Sstevel@tonic-gate (void) fputs(inbuf, stdout); 18197c478bd9Sstevel@tonic-gate } 18207c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, SIG_DFL); 18217c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_DFL); 18227c478bd9Sstevel@tonic-gate return (pclose(file)); 18237c478bd9Sstevel@tonic-gate } 18247c478bd9Sstevel@tonic-gate 18257c478bd9Sstevel@tonic-gate static int 18269acbbeafSnn35248 do_subproc_interactive(char *cmdbuf) 18279acbbeafSnn35248 { 18289acbbeafSnn35248 void (*saveint)(int); 18299acbbeafSnn35248 void (*saveterm)(int); 18309acbbeafSnn35248 void (*savequit)(int); 18319acbbeafSnn35248 void (*savehup)(int); 18329acbbeafSnn35248 int pid, child, status; 18339acbbeafSnn35248 18349acbbeafSnn35248 /* 18359acbbeafSnn35248 * do_subproc() links stdin to /dev/null, which would break any 18369acbbeafSnn35248 * interactive subprocess we try to launch here. Similarly, we 18379acbbeafSnn35248 * can't have been launched as a subprocess ourselves. 18389acbbeafSnn35248 */ 18399acbbeafSnn35248 assert(do_subproc_cnt == 0 && !zoneadm_is_nested); 18409acbbeafSnn35248 18419acbbeafSnn35248 if ((child = vfork()) == 0) { 18429acbbeafSnn35248 (void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL); 18439acbbeafSnn35248 } 18449acbbeafSnn35248 18459acbbeafSnn35248 if (child == -1) 18469acbbeafSnn35248 return (-1); 18479acbbeafSnn35248 18489acbbeafSnn35248 saveint = sigset(SIGINT, SIG_IGN); 18499acbbeafSnn35248 saveterm = sigset(SIGTERM, SIG_IGN); 18509acbbeafSnn35248 savequit = sigset(SIGQUIT, SIG_IGN); 18519acbbeafSnn35248 savehup = sigset(SIGHUP, SIG_IGN); 18529acbbeafSnn35248 18539acbbeafSnn35248 while ((pid = waitpid(child, &status, 0)) != child && pid != -1) 18549acbbeafSnn35248 ; 18559acbbeafSnn35248 18569acbbeafSnn35248 (void) sigset(SIGINT, saveint); 18579acbbeafSnn35248 (void) sigset(SIGTERM, saveterm); 18589acbbeafSnn35248 (void) sigset(SIGQUIT, savequit); 18599acbbeafSnn35248 (void) sigset(SIGHUP, savehup); 18609acbbeafSnn35248 18619acbbeafSnn35248 return (pid == -1 ? -1 : status); 18629acbbeafSnn35248 } 18639acbbeafSnn35248 18649acbbeafSnn35248 static int 18659acbbeafSnn35248 subproc_status(const char *cmd, int status, boolean_t verbose_failure) 18667c478bd9Sstevel@tonic-gate { 18677c478bd9Sstevel@tonic-gate if (WIFEXITED(status)) { 18687c478bd9Sstevel@tonic-gate int exit_code = WEXITSTATUS(status); 18697c478bd9Sstevel@tonic-gate 18709acbbeafSnn35248 if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK)) 18717c478bd9Sstevel@tonic-gate zerror(gettext("'%s' failed with exit code %d."), cmd, 18727c478bd9Sstevel@tonic-gate exit_code); 18739acbbeafSnn35248 18749acbbeafSnn35248 return (exit_code); 18757c478bd9Sstevel@tonic-gate } else if (WIFSIGNALED(status)) { 18767c478bd9Sstevel@tonic-gate int signal = WTERMSIG(status); 18777c478bd9Sstevel@tonic-gate char sigstr[SIG2STR_MAX]; 18787c478bd9Sstevel@tonic-gate 18797c478bd9Sstevel@tonic-gate if (sig2str(signal, sigstr) == 0) { 18807c478bd9Sstevel@tonic-gate zerror(gettext("'%s' terminated by signal SIG%s."), cmd, 18817c478bd9Sstevel@tonic-gate sigstr); 18827c478bd9Sstevel@tonic-gate } else { 18837c478bd9Sstevel@tonic-gate zerror(gettext("'%s' terminated by an unknown signal."), 18847c478bd9Sstevel@tonic-gate cmd); 18857c478bd9Sstevel@tonic-gate } 18867c478bd9Sstevel@tonic-gate } else { 18877c478bd9Sstevel@tonic-gate zerror(gettext("'%s' failed for unknown reasons."), cmd); 18887c478bd9Sstevel@tonic-gate } 18899acbbeafSnn35248 18909acbbeafSnn35248 /* 18919acbbeafSnn35248 * Assume a subprocess that died due to a signal or an unknown error 18929acbbeafSnn35248 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the 18939acbbeafSnn35248 * user will likely need to do some manual cleanup. 18949acbbeafSnn35248 */ 18959acbbeafSnn35248 return (ZONE_SUBPROC_FATAL); 18967c478bd9Sstevel@tonic-gate } 18977c478bd9Sstevel@tonic-gate 18987c478bd9Sstevel@tonic-gate /* 18997c478bd9Sstevel@tonic-gate * Various sanity checks; make sure: 19007c478bd9Sstevel@tonic-gate * 1. We're in the global zone. 19017c478bd9Sstevel@tonic-gate * 2. The calling user has sufficient privilege. 19027c478bd9Sstevel@tonic-gate * 3. The target zone is neither the global zone nor anything starting with 19037c478bd9Sstevel@tonic-gate * "SUNW". 19047c478bd9Sstevel@tonic-gate * 4a. If we're looking for a 'not running' (i.e., configured or installed) 19057c478bd9Sstevel@tonic-gate * zone, the name service knows about it. 19067c478bd9Sstevel@tonic-gate * 4b. For some operations which expect a zone not to be running, that it is 19077c478bd9Sstevel@tonic-gate * not already running (or ready). 19087c478bd9Sstevel@tonic-gate */ 19097c478bd9Sstevel@tonic-gate static int 19107c478bd9Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running, 19119acbbeafSnn35248 boolean_t unsafe_when_running, boolean_t force) 19127c478bd9Sstevel@tonic-gate { 19137c478bd9Sstevel@tonic-gate zone_entry_t *zent; 19147c478bd9Sstevel@tonic-gate priv_set_t *privset; 19159acbbeafSnn35248 zone_state_t state, min_state; 1916108322fbScarlsonj char kernzone[ZONENAME_MAX]; 1917108322fbScarlsonj FILE *fp; 19187c478bd9Sstevel@tonic-gate 19197c478bd9Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) { 1920ffbafc53Scomay switch (cmd_num) { 1921ffbafc53Scomay case CMD_HALT: 1922ffbafc53Scomay zerror(gettext("use %s to %s this zone."), "halt(1M)", 19237c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 1924ffbafc53Scomay break; 1925ffbafc53Scomay case CMD_REBOOT: 1926ffbafc53Scomay zerror(gettext("use %s to %s this zone."), 1927ffbafc53Scomay "reboot(1M)", cmd_to_str(cmd_num)); 1928ffbafc53Scomay break; 1929ffbafc53Scomay default: 1930ffbafc53Scomay zerror(gettext("must be in the global zone to %s a " 1931ffbafc53Scomay "zone."), cmd_to_str(cmd_num)); 1932ffbafc53Scomay break; 1933ffbafc53Scomay } 19347c478bd9Sstevel@tonic-gate return (Z_ERR); 19357c478bd9Sstevel@tonic-gate } 19367c478bd9Sstevel@tonic-gate 19377c478bd9Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) { 19387c478bd9Sstevel@tonic-gate zerror(gettext("%s failed"), "priv_allocset"); 19397c478bd9Sstevel@tonic-gate return (Z_ERR); 19407c478bd9Sstevel@tonic-gate } 19417c478bd9Sstevel@tonic-gate 19427c478bd9Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 19437c478bd9Sstevel@tonic-gate zerror(gettext("%s failed"), "getppriv"); 19447c478bd9Sstevel@tonic-gate priv_freeset(privset); 19457c478bd9Sstevel@tonic-gate return (Z_ERR); 19467c478bd9Sstevel@tonic-gate } 19477c478bd9Sstevel@tonic-gate 19487c478bd9Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) { 19497c478bd9Sstevel@tonic-gate zerror(gettext("only a privileged user may %s a zone."), 19507c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 19517c478bd9Sstevel@tonic-gate priv_freeset(privset); 19527c478bd9Sstevel@tonic-gate return (Z_ERR); 19537c478bd9Sstevel@tonic-gate } 19547c478bd9Sstevel@tonic-gate priv_freeset(privset); 19557c478bd9Sstevel@tonic-gate 19567c478bd9Sstevel@tonic-gate if (zone == NULL) { 19577c478bd9Sstevel@tonic-gate zerror(gettext("no zone specified")); 19587c478bd9Sstevel@tonic-gate return (Z_ERR); 19597c478bd9Sstevel@tonic-gate } 19607c478bd9Sstevel@tonic-gate 19617c478bd9Sstevel@tonic-gate if (strcmp(zone, GLOBAL_ZONENAME) == 0) { 19627c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for the global zone."), 19637c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 19647c478bd9Sstevel@tonic-gate return (Z_ERR); 19657c478bd9Sstevel@tonic-gate } 19667c478bd9Sstevel@tonic-gate 19677c478bd9Sstevel@tonic-gate if (strncmp(zone, "SUNW", 4) == 0) { 19687c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for zones starting " 19697c478bd9Sstevel@tonic-gate "with SUNW."), cmd_to_str(cmd_num)); 19707c478bd9Sstevel@tonic-gate return (Z_ERR); 19717c478bd9Sstevel@tonic-gate } 19727c478bd9Sstevel@tonic-gate 197384561e8cStd153743 if (!is_native_zone && !is_cluster_zone && cmd_num == CMD_MOUNT) { 19749acbbeafSnn35248 zerror(gettext("%s operation is invalid for branded zones."), 19759acbbeafSnn35248 cmd_to_str(cmd_num)); 19769acbbeafSnn35248 return (Z_ERR); 19779acbbeafSnn35248 } 19789acbbeafSnn35248 1979108322fbScarlsonj if (!zonecfg_in_alt_root()) { 1980108322fbScarlsonj zent = lookup_running_zone(zone); 1981108322fbScarlsonj } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 1982108322fbScarlsonj zent = NULL; 1983108322fbScarlsonj } else { 1984108322fbScarlsonj if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(), 1985108322fbScarlsonj kernzone, sizeof (kernzone)) == 0) 1986108322fbScarlsonj zent = lookup_running_zone(kernzone); 1987108322fbScarlsonj else 1988108322fbScarlsonj zent = NULL; 1989108322fbScarlsonj zonecfg_close_scratch(fp); 1990108322fbScarlsonj } 1991108322fbScarlsonj 19927c478bd9Sstevel@tonic-gate /* 19937c478bd9Sstevel@tonic-gate * Look up from the kernel for 'running' zones. 19947c478bd9Sstevel@tonic-gate */ 19959acbbeafSnn35248 if (running && !force) { 19967c478bd9Sstevel@tonic-gate if (zent == NULL) { 19977c478bd9Sstevel@tonic-gate zerror(gettext("not running")); 19987c478bd9Sstevel@tonic-gate return (Z_ERR); 19997c478bd9Sstevel@tonic-gate } 20007c478bd9Sstevel@tonic-gate } else { 20017c478bd9Sstevel@tonic-gate int err; 20027c478bd9Sstevel@tonic-gate 20037c478bd9Sstevel@tonic-gate if (unsafe_when_running && zent != NULL) { 20047c478bd9Sstevel@tonic-gate /* check whether the zone is ready or running */ 20057c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zent->zname, 20067c478bd9Sstevel@tonic-gate &zent->zstate_num)) != Z_OK) { 20077c478bd9Sstevel@tonic-gate errno = err; 20087c478bd9Sstevel@tonic-gate zperror2(zent->zname, 20097c478bd9Sstevel@tonic-gate gettext("could not get state")); 20107c478bd9Sstevel@tonic-gate /* can't tell, so hedge */ 20117c478bd9Sstevel@tonic-gate zent->zstate_str = "ready/running"; 20127c478bd9Sstevel@tonic-gate } else { 20137c478bd9Sstevel@tonic-gate zent->zstate_str = 20147c478bd9Sstevel@tonic-gate zone_state_str(zent->zstate_num); 20157c478bd9Sstevel@tonic-gate } 20167c478bd9Sstevel@tonic-gate zerror(gettext("%s operation is invalid for %s zones."), 20177c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num), zent->zstate_str); 20187c478bd9Sstevel@tonic-gate return (Z_ERR); 20197c478bd9Sstevel@tonic-gate } 20207c478bd9Sstevel@tonic-gate if ((err = zone_get_state(zone, &state)) != Z_OK) { 20217c478bd9Sstevel@tonic-gate errno = err; 20227c478bd9Sstevel@tonic-gate zperror2(zone, gettext("could not get state")); 20237c478bd9Sstevel@tonic-gate return (Z_ERR); 20247c478bd9Sstevel@tonic-gate } 20257c478bd9Sstevel@tonic-gate switch (cmd_num) { 20267c478bd9Sstevel@tonic-gate case CMD_UNINSTALL: 20277c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_CONFIGURED) { 20287c478bd9Sstevel@tonic-gate zerror(gettext("is already in state '%s'."), 20297c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_CONFIGURED)); 20307c478bd9Sstevel@tonic-gate return (Z_ERR); 20317c478bd9Sstevel@tonic-gate } 20327c478bd9Sstevel@tonic-gate break; 2033ee519a1fSgjelinek case CMD_ATTACH: 2034865e09a4Sgjelinek case CMD_CLONE: 20357c478bd9Sstevel@tonic-gate case CMD_INSTALL: 20367c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_INSTALLED) { 20377c478bd9Sstevel@tonic-gate zerror(gettext("is already %s."), 20387c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INSTALLED)); 20397c478bd9Sstevel@tonic-gate return (Z_ERR); 20407c478bd9Sstevel@tonic-gate } else if (state == ZONE_STATE_INCOMPLETE) { 20417c478bd9Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 20427c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 20437c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 20447c478bd9Sstevel@tonic-gate return (Z_ERR); 20457c478bd9Sstevel@tonic-gate } 20467c478bd9Sstevel@tonic-gate break; 2047ee519a1fSgjelinek case CMD_DETACH: 2048865e09a4Sgjelinek case CMD_MOVE: 20497c478bd9Sstevel@tonic-gate case CMD_READY: 20507c478bd9Sstevel@tonic-gate case CMD_BOOT: 2051108322fbScarlsonj case CMD_MOUNT: 2052555afedfScarlsonj case CMD_MARK: 20539acbbeafSnn35248 if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) && 20549acbbeafSnn35248 force) 20559acbbeafSnn35248 min_state = ZONE_STATE_INCOMPLETE; 20569acbbeafSnn35248 else 20579acbbeafSnn35248 min_state = ZONE_STATE_INSTALLED; 20589acbbeafSnn35248 20599acbbeafSnn35248 if (force && cmd_num == CMD_BOOT && is_native_zone) { 20609acbbeafSnn35248 zerror(gettext("Only branded zones may be " 20619acbbeafSnn35248 "force-booted.")); 20629acbbeafSnn35248 return (Z_ERR); 20639acbbeafSnn35248 } 20649acbbeafSnn35248 20659acbbeafSnn35248 if (state < min_state) { 20667c478bd9Sstevel@tonic-gate zerror(gettext("must be %s before %s."), 20679acbbeafSnn35248 zone_state_str(min_state), 20687c478bd9Sstevel@tonic-gate cmd_to_str(cmd_num)); 20697c478bd9Sstevel@tonic-gate return (Z_ERR); 20707c478bd9Sstevel@tonic-gate } 20717c478bd9Sstevel@tonic-gate break; 20727c478bd9Sstevel@tonic-gate case CMD_VERIFY: 20737c478bd9Sstevel@tonic-gate if (state == ZONE_STATE_INCOMPLETE) { 20747c478bd9Sstevel@tonic-gate zerror(gettext("zone is %s; %s required."), 20757c478bd9Sstevel@tonic-gate zone_state_str(ZONE_STATE_INCOMPLETE), 20767c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 20777c478bd9Sstevel@tonic-gate return (Z_ERR); 20787c478bd9Sstevel@tonic-gate } 20797c478bd9Sstevel@tonic-gate break; 2080108322fbScarlsonj case CMD_UNMOUNT: 2081108322fbScarlsonj if (state != ZONE_STATE_MOUNTED) { 2082108322fbScarlsonj zerror(gettext("must be %s before %s."), 2083108322fbScarlsonj zone_state_str(ZONE_STATE_MOUNTED), 2084108322fbScarlsonj cmd_to_str(cmd_num)); 2085108322fbScarlsonj return (Z_ERR); 2086108322fbScarlsonj } 2087108322fbScarlsonj break; 20887c478bd9Sstevel@tonic-gate } 20897c478bd9Sstevel@tonic-gate } 20907c478bd9Sstevel@tonic-gate return (Z_OK); 20917c478bd9Sstevel@tonic-gate } 20927c478bd9Sstevel@tonic-gate 20937c478bd9Sstevel@tonic-gate static int 20947c478bd9Sstevel@tonic-gate halt_func(int argc, char *argv[]) 20957c478bd9Sstevel@tonic-gate { 20967c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 20977c478bd9Sstevel@tonic-gate int arg; 20987c478bd9Sstevel@tonic-gate 2099108322fbScarlsonj if (zonecfg_in_alt_root()) { 2100108322fbScarlsonj zerror(gettext("cannot halt zone in alternate root")); 2101108322fbScarlsonj return (Z_ERR); 2102108322fbScarlsonj } 2103108322fbScarlsonj 21047c478bd9Sstevel@tonic-gate optind = 0; 21057c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 21067c478bd9Sstevel@tonic-gate switch (arg) { 21077c478bd9Sstevel@tonic-gate case '?': 21087c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 21097c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 21107c478bd9Sstevel@tonic-gate default: 21117c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 21127c478bd9Sstevel@tonic-gate return (Z_USAGE); 21137c478bd9Sstevel@tonic-gate } 21147c478bd9Sstevel@tonic-gate } 21157c478bd9Sstevel@tonic-gate if (argc > optind) { 21167c478bd9Sstevel@tonic-gate sub_usage(SHELP_HALT, CMD_HALT); 21177c478bd9Sstevel@tonic-gate return (Z_USAGE); 21187c478bd9Sstevel@tonic-gate } 21197c478bd9Sstevel@tonic-gate /* 21207c478bd9Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 21217c478bd9Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 21227c478bd9Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 21237c478bd9Sstevel@tonic-gate */ 21249acbbeafSnn35248 if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE) 21259acbbeafSnn35248 != Z_OK) 21267c478bd9Sstevel@tonic-gate return (Z_ERR); 21277c478bd9Sstevel@tonic-gate 2128ce28b40eSzt129084 /* 2129ce28b40eSzt129084 * Invoke brand-specific handler. 2130ce28b40eSzt129084 */ 2131ce28b40eSzt129084 if (invoke_brand_handler(CMD_HALT, argv) != Z_OK) 2132ce28b40eSzt129084 return (Z_ERR); 2133ce28b40eSzt129084 21347c478bd9Sstevel@tonic-gate zarg.cmd = Z_HALT; 21357c478bd9Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 21367c478bd9Sstevel@tonic-gate } 21377c478bd9Sstevel@tonic-gate 21387c478bd9Sstevel@tonic-gate static int 21397c478bd9Sstevel@tonic-gate reboot_func(int argc, char *argv[]) 21407c478bd9Sstevel@tonic-gate { 21417c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 21427c478bd9Sstevel@tonic-gate int arg; 21437c478bd9Sstevel@tonic-gate 2144108322fbScarlsonj if (zonecfg_in_alt_root()) { 2145108322fbScarlsonj zerror(gettext("cannot reboot zone in alternate root")); 2146108322fbScarlsonj return (Z_ERR); 2147108322fbScarlsonj } 2148108322fbScarlsonj 21497c478bd9Sstevel@tonic-gate optind = 0; 21507c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 21517c478bd9Sstevel@tonic-gate switch (arg) { 21527c478bd9Sstevel@tonic-gate case '?': 21537c478bd9Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 21547c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 21557c478bd9Sstevel@tonic-gate default: 21567c478bd9Sstevel@tonic-gate sub_usage(SHELP_REBOOT, CMD_REBOOT); 21577c478bd9Sstevel@tonic-gate return (Z_USAGE); 21587c478bd9Sstevel@tonic-gate } 21597c478bd9Sstevel@tonic-gate } 21603f2f09c1Sdp 21613f2f09c1Sdp zarg.bootbuf[0] = '\0'; 21623f2f09c1Sdp for (; optind < argc; optind++) { 21633f2f09c1Sdp if (strlcat(zarg.bootbuf, argv[optind], 21643f2f09c1Sdp sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) { 21653f2f09c1Sdp zerror(gettext("Boot argument list too long")); 21663f2f09c1Sdp return (Z_ERR); 21677c478bd9Sstevel@tonic-gate } 21683f2f09c1Sdp if (optind < argc - 1) 21693f2f09c1Sdp if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >= 21703f2f09c1Sdp sizeof (zarg.bootbuf)) { 21713f2f09c1Sdp zerror(gettext("Boot argument list too long")); 21723f2f09c1Sdp return (Z_ERR); 21733f2f09c1Sdp } 21743f2f09c1Sdp } 21753f2f09c1Sdp 21763f2f09c1Sdp 21777c478bd9Sstevel@tonic-gate /* 21787c478bd9Sstevel@tonic-gate * zoneadmd should be the one to decide whether or not to proceed, 21797c478bd9Sstevel@tonic-gate * so even though it seems that the fourth parameter below should 21807c478bd9Sstevel@tonic-gate * perhaps be B_TRUE, it really shouldn't be. 21817c478bd9Sstevel@tonic-gate */ 21829acbbeafSnn35248 if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE) 21839acbbeafSnn35248 != Z_OK) 21847c478bd9Sstevel@tonic-gate return (Z_ERR); 2185ce28b40eSzt129084 if (verify_details(CMD_REBOOT, argv) != Z_OK) 2186b5abaf04Sgjelinek return (Z_ERR); 21877c478bd9Sstevel@tonic-gate 21887c478bd9Sstevel@tonic-gate zarg.cmd = Z_REBOOT; 21897c478bd9Sstevel@tonic-gate return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR); 21907c478bd9Sstevel@tonic-gate } 21917c478bd9Sstevel@tonic-gate 21927c478bd9Sstevel@tonic-gate static int 2193ce28b40eSzt129084 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[]) 21949acbbeafSnn35248 { 21959acbbeafSnn35248 char cmdbuf[MAXPATHLEN]; 21969acbbeafSnn35248 int err; 21979acbbeafSnn35248 char zonepath[MAXPATHLEN]; 2198123807fbSedp brand_handle_t bh = NULL; 2199ce28b40eSzt129084 int status, i; 22009acbbeafSnn35248 22019acbbeafSnn35248 /* 22029acbbeafSnn35248 * Fetch the verify command from the brand configuration. 22039acbbeafSnn35248 * "exec" the command so that the returned status is that of 22049acbbeafSnn35248 * the command and not the shell. 22059acbbeafSnn35248 */ 22069acbbeafSnn35248 if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 22079acbbeafSnn35248 Z_OK) { 22089acbbeafSnn35248 errno = err; 2209ce28b40eSzt129084 zperror(cmd_to_str(cmd_num), B_TRUE); 22109acbbeafSnn35248 return (Z_ERR); 22119acbbeafSnn35248 } 2212123807fbSedp if ((bh = brand_open(target_brand)) == NULL) { 22139acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 22149acbbeafSnn35248 return (Z_ERR); 22159acbbeafSnn35248 } 22169acbbeafSnn35248 22179acbbeafSnn35248 /* 22189acbbeafSnn35248 * If the brand has its own verification routine, execute it now. 2219ce28b40eSzt129084 * The verification routine validates the intended zoneadm 2220ce28b40eSzt129084 * operation for the specific brand. The zoneadm subcommand and 2221ce28b40eSzt129084 * all its arguments are passed to the routine. 22229acbbeafSnn35248 */ 22239acbbeafSnn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 2224123807fbSedp err = brand_get_verify_adm(bh, target_zone, zonepath, 22259acbbeafSnn35248 cmdbuf + EXEC_LEN, sizeof (cmdbuf) - EXEC_LEN, 0, NULL); 2226123807fbSedp brand_close(bh); 2227ce28b40eSzt129084 if (err != 0) 2228ce28b40eSzt129084 return (Z_BRAND_ERROR); 2229ce28b40eSzt129084 if (strlen(cmdbuf) <= EXEC_LEN) 2230ce28b40eSzt129084 return (Z_OK); 2231ce28b40eSzt129084 2232ce28b40eSzt129084 if (strlcat(cmdbuf, cmd_to_str(cmd_num), 2233ce28b40eSzt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf)) 2234ce28b40eSzt129084 return (Z_ERR); 2235ce28b40eSzt129084 2236ce28b40eSzt129084 /* Build the argv string */ 2237ce28b40eSzt129084 i = 0; 2238ce28b40eSzt129084 while (argv[i] != NULL) { 2239ce28b40eSzt129084 if ((strlcat(cmdbuf, " ", 2240ce28b40eSzt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf)) || 2241ce28b40eSzt129084 (strlcat(cmdbuf, argv[i++], 2242ce28b40eSzt129084 sizeof (cmdbuf)) >= sizeof (cmdbuf))) 2243ce28b40eSzt129084 return (Z_ERR); 2244ce28b40eSzt129084 } 2245ce28b40eSzt129084 2246d9e728a2Sgjelinek if (zoneadm_is_nested) 2247d9e728a2Sgjelinek status = do_subproc(cmdbuf); 2248d9e728a2Sgjelinek else 22499acbbeafSnn35248 status = do_subproc_interactive(cmdbuf); 22509acbbeafSnn35248 err = subproc_status(gettext("brand-specific verification"), 22519acbbeafSnn35248 status, B_FALSE); 22529acbbeafSnn35248 2253ce28b40eSzt129084 return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR); 22549acbbeafSnn35248 } 22559acbbeafSnn35248 22569acbbeafSnn35248 static int 22577c478bd9Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle) 22587c478bd9Sstevel@tonic-gate { 22597c478bd9Sstevel@tonic-gate struct zone_rctltab rctltab; 22607c478bd9Sstevel@tonic-gate size_t rbs = rctlblk_size(); 22617c478bd9Sstevel@tonic-gate rctlblk_t *rctlblk; 22627c478bd9Sstevel@tonic-gate int error = Z_INVAL; 22637c478bd9Sstevel@tonic-gate 22647c478bd9Sstevel@tonic-gate if ((rctlblk = malloc(rbs)) == NULL) { 22657c478bd9Sstevel@tonic-gate zerror(gettext("failed to allocate %lu bytes: %s"), rbs, 22667c478bd9Sstevel@tonic-gate strerror(errno)); 22677c478bd9Sstevel@tonic-gate return (Z_NOMEM); 22687c478bd9Sstevel@tonic-gate } 22697c478bd9Sstevel@tonic-gate 22707c478bd9Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 22717c478bd9Sstevel@tonic-gate zerror(gettext("zonecfg_setrctlent failed")); 22727c478bd9Sstevel@tonic-gate free(rctlblk); 22737c478bd9Sstevel@tonic-gate return (error); 22747c478bd9Sstevel@tonic-gate } 22757c478bd9Sstevel@tonic-gate 22767c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 22777c478bd9Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 22787c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 22797c478bd9Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 22807c478bd9Sstevel@tonic-gate 22817c478bd9Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 22827c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: Ignoring unrecognized rctl " 22837c478bd9Sstevel@tonic-gate "'%s'."), name); 22847c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 22857c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 22867c478bd9Sstevel@tonic-gate continue; 22877c478bd9Sstevel@tonic-gate } 22887c478bd9Sstevel@tonic-gate 22897c478bd9Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 22907c478bd9Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 22917c478bd9Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 22927c478bd9Sstevel@tonic-gate != Z_OK) { 22937c478bd9Sstevel@tonic-gate zerror(gettext("invalid rctl value: " 22947c478bd9Sstevel@tonic-gate "(priv=%s,limit=%s,action%s)"), 22957c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 22967c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 22977c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action); 22987c478bd9Sstevel@tonic-gate goto out; 22997c478bd9Sstevel@tonic-gate } 23007c478bd9Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 23017c478bd9Sstevel@tonic-gate zerror(gettext("(priv=%s,limit=%s,action=%s) " 23027c478bd9Sstevel@tonic-gate "is not a valid value for rctl '%s'"), 23037c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 23047c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 23057c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action, 23067c478bd9Sstevel@tonic-gate name); 23077c478bd9Sstevel@tonic-gate goto out; 23087c478bd9Sstevel@tonic-gate } 23097c478bd9Sstevel@tonic-gate } 23107c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 23117c478bd9Sstevel@tonic-gate } 23127c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 23137c478bd9Sstevel@tonic-gate error = Z_OK; 23147c478bd9Sstevel@tonic-gate out: 23157c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 23167c478bd9Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 23177c478bd9Sstevel@tonic-gate free(rctlblk); 23187c478bd9Sstevel@tonic-gate return (error); 23197c478bd9Sstevel@tonic-gate } 23207c478bd9Sstevel@tonic-gate 23217c478bd9Sstevel@tonic-gate static int 23227c478bd9Sstevel@tonic-gate verify_pool(zone_dochandle_t handle) 23237c478bd9Sstevel@tonic-gate { 23247c478bd9Sstevel@tonic-gate char poolname[MAXPATHLEN]; 23257c478bd9Sstevel@tonic-gate pool_conf_t *poolconf; 23267c478bd9Sstevel@tonic-gate pool_t *pool; 23277c478bd9Sstevel@tonic-gate int status; 23287c478bd9Sstevel@tonic-gate int error; 23297c478bd9Sstevel@tonic-gate 23307c478bd9Sstevel@tonic-gate /* 23317c478bd9Sstevel@tonic-gate * This ends up being very similar to the check done in zoneadmd. 23327c478bd9Sstevel@tonic-gate */ 23337c478bd9Sstevel@tonic-gate error = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 23347c478bd9Sstevel@tonic-gate if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 23357c478bd9Sstevel@tonic-gate /* 23367c478bd9Sstevel@tonic-gate * No pool specified. 23377c478bd9Sstevel@tonic-gate */ 23387c478bd9Sstevel@tonic-gate return (0); 23397c478bd9Sstevel@tonic-gate } 23407c478bd9Sstevel@tonic-gate if (error != Z_OK) { 23417c478bd9Sstevel@tonic-gate zperror(gettext("Unable to retrieve pool name from " 23427c478bd9Sstevel@tonic-gate "configuration"), B_TRUE); 23437c478bd9Sstevel@tonic-gate return (error); 23447c478bd9Sstevel@tonic-gate } 23457c478bd9Sstevel@tonic-gate /* 23467c478bd9Sstevel@tonic-gate * Don't do anything if pools aren't enabled. 23477c478bd9Sstevel@tonic-gate */ 23487c478bd9Sstevel@tonic-gate if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 23497c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pools facility not active; " 23507c478bd9Sstevel@tonic-gate "zone will not be bound to pool '%s'."), poolname); 23517c478bd9Sstevel@tonic-gate return (Z_OK); 23527c478bd9Sstevel@tonic-gate } 23537c478bd9Sstevel@tonic-gate /* 23547c478bd9Sstevel@tonic-gate * Try to provide a sane error message if the requested pool doesn't 23557c478bd9Sstevel@tonic-gate * exist. It isn't clear that pools-related failures should 23567c478bd9Sstevel@tonic-gate * necessarily translate to a failure to verify the zone configuration, 23577c478bd9Sstevel@tonic-gate * hence they are not considered errors. 23587c478bd9Sstevel@tonic-gate */ 23597c478bd9Sstevel@tonic-gate if ((poolconf = pool_conf_alloc()) == NULL) { 23607c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_alloc failed; " 23617c478bd9Sstevel@tonic-gate "using default pool")); 23627c478bd9Sstevel@tonic-gate return (Z_OK); 23637c478bd9Sstevel@tonic-gate } 23647c478bd9Sstevel@tonic-gate if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 23657c478bd9Sstevel@tonic-gate PO_SUCCESS) { 23667c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool_conf_open failed; " 23677c478bd9Sstevel@tonic-gate "using default pool")); 23687c478bd9Sstevel@tonic-gate pool_conf_free(poolconf); 23697c478bd9Sstevel@tonic-gate return (Z_OK); 23707c478bd9Sstevel@tonic-gate } 23717c478bd9Sstevel@tonic-gate pool = pool_get_pool(poolconf, poolname); 23727c478bd9Sstevel@tonic-gate (void) pool_conf_close(poolconf); 23737c478bd9Sstevel@tonic-gate pool_conf_free(poolconf); 23747c478bd9Sstevel@tonic-gate if (pool == NULL) { 23757c478bd9Sstevel@tonic-gate zerror(gettext("WARNING: pool '%s' not found. " 23767c478bd9Sstevel@tonic-gate "using default pool"), poolname); 23777c478bd9Sstevel@tonic-gate } 23787c478bd9Sstevel@tonic-gate 23797c478bd9Sstevel@tonic-gate return (Z_OK); 23807c478bd9Sstevel@tonic-gate } 23817c478bd9Sstevel@tonic-gate 23827c478bd9Sstevel@tonic-gate static int 2383b5abaf04Sgjelinek verify_ipd(zone_dochandle_t handle) 2384b5abaf04Sgjelinek { 2385b5abaf04Sgjelinek int return_code = Z_OK; 2386b5abaf04Sgjelinek struct zone_fstab fstab; 2387b5abaf04Sgjelinek struct stat st; 2388b5abaf04Sgjelinek char specdir[MAXPATHLEN]; 2389b5abaf04Sgjelinek 2390b5abaf04Sgjelinek if (zonecfg_setipdent(handle) != Z_OK) { 23915ee84fbdSgjelinek /* 23925ee84fbdSgjelinek * TRANSLATION_NOTE 23935ee84fbdSgjelinek * inherit-pkg-dirs is a literal that should not be translated. 23945ee84fbdSgjelinek */ 23955ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify " 2396b5abaf04Sgjelinek "inherit-pkg-dirs: unable to enumerate mounts\n")); 2397b5abaf04Sgjelinek return (Z_ERR); 2398b5abaf04Sgjelinek } 2399b5abaf04Sgjelinek while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 2400b5abaf04Sgjelinek /* 2401b5abaf04Sgjelinek * Verify fs_dir exists. 2402b5abaf04Sgjelinek */ 2403b5abaf04Sgjelinek (void) snprintf(specdir, sizeof (specdir), "%s%s", 2404b5abaf04Sgjelinek zonecfg_get_root(), fstab.zone_fs_dir); 2405b5abaf04Sgjelinek if (stat(specdir, &st) != 0) { 24065ee84fbdSgjelinek /* 24075ee84fbdSgjelinek * TRANSLATION_NOTE 24085ee84fbdSgjelinek * inherit-pkg-dir is a literal that should not be 24095ee84fbdSgjelinek * translated. 24105ee84fbdSgjelinek */ 24115ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify " 2412b5abaf04Sgjelinek "inherit-pkg-dir %s: %s\n"), 2413b5abaf04Sgjelinek fstab.zone_fs_dir, strerror(errno)); 2414b5abaf04Sgjelinek return_code = Z_ERR; 2415b5abaf04Sgjelinek } 2416b5abaf04Sgjelinek if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 24175ee84fbdSgjelinek /* 24185ee84fbdSgjelinek * TRANSLATION_NOTE 24195ee84fbdSgjelinek * inherit-pkg-dir and NFS are literals that should 24205ee84fbdSgjelinek * not be translated. 24215ee84fbdSgjelinek */ 2422b5abaf04Sgjelinek (void) fprintf(stderr, gettext("cannot verify " 24230b5de56dSgjelinek "inherit-pkg-dir %s: NFS mounted file system.\n" 24240b5de56dSgjelinek "\tA local file system must be used.\n"), 2425b5abaf04Sgjelinek fstab.zone_fs_dir); 2426b5abaf04Sgjelinek return_code = Z_ERR; 2427b5abaf04Sgjelinek } 2428b5abaf04Sgjelinek } 2429b5abaf04Sgjelinek (void) zonecfg_endipdent(handle); 2430b5abaf04Sgjelinek 2431b5abaf04Sgjelinek return (return_code); 2432b5abaf04Sgjelinek } 2433b5abaf04Sgjelinek 243420c8013fSlling /* 243520c8013fSlling * Verify that the special device/file system exists and is valid. 243620c8013fSlling */ 243720c8013fSlling static int 243820c8013fSlling verify_fs_special(struct zone_fstab *fstab) 243920c8013fSlling { 2440*93239addSjohnlev struct stat64 st; 244120c8013fSlling 24426cc813faSgjelinek /* 24436cc813faSgjelinek * This validation is really intended for standard zone administration. 24446cc813faSgjelinek * If we are in a mini-root or some other upgrade situation where 24456cc813faSgjelinek * we are using the scratch zone, just by-pass this. 24466cc813faSgjelinek */ 24476cc813faSgjelinek if (zonecfg_in_alt_root()) 24486cc813faSgjelinek return (Z_OK); 24496cc813faSgjelinek 245020c8013fSlling if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0) 245120c8013fSlling return (verify_fs_zfs(fstab)); 245220c8013fSlling 2453*93239addSjohnlev if (stat64(fstab->zone_fs_special, &st) != 0) { 24545c358068Slling (void) fprintf(stderr, gettext("could not verify fs " 245520c8013fSlling "%s: could not access %s: %s\n"), fstab->zone_fs_dir, 245620c8013fSlling fstab->zone_fs_special, strerror(errno)); 245720c8013fSlling return (Z_ERR); 245820c8013fSlling } 245920c8013fSlling 246020c8013fSlling if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) { 246120c8013fSlling /* 246220c8013fSlling * TRANSLATION_NOTE 246320c8013fSlling * fs and NFS are literals that should 246420c8013fSlling * not be translated. 246520c8013fSlling */ 246620c8013fSlling (void) fprintf(stderr, gettext("cannot verify " 24670b5de56dSgjelinek "fs %s: NFS mounted file system.\n" 24680b5de56dSgjelinek "\tA local file system must be used.\n"), 246920c8013fSlling fstab->zone_fs_special); 247020c8013fSlling return (Z_ERR); 247120c8013fSlling } 247220c8013fSlling 247320c8013fSlling return (Z_OK); 247420c8013fSlling } 247520c8013fSlling 2476b5abaf04Sgjelinek static int 2477*93239addSjohnlev isregfile(const char *path) 2478*93239addSjohnlev { 2479*93239addSjohnlev struct stat64 st; 2480*93239addSjohnlev 2481*93239addSjohnlev if (stat64(path, &st) == -1) 2482*93239addSjohnlev return (-1); 2483*93239addSjohnlev 2484*93239addSjohnlev return (S_ISREG(st.st_mode)); 2485*93239addSjohnlev } 2486*93239addSjohnlev 2487*93239addSjohnlev static int 24887c478bd9Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle) 24897c478bd9Sstevel@tonic-gate { 24907c478bd9Sstevel@tonic-gate int return_code = Z_OK; 24917c478bd9Sstevel@tonic-gate struct zone_fstab fstab; 24927c478bd9Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 24937c478bd9Sstevel@tonic-gate struct stat st; 24947c478bd9Sstevel@tonic-gate 24957c478bd9Sstevel@tonic-gate /* 24967c478bd9Sstevel@tonic-gate * No need to verify inherit-pkg-dir fs types, as their type is 24977c478bd9Sstevel@tonic-gate * implicitly lofs, which is known. Therefore, the types are only 24987c478bd9Sstevel@tonic-gate * verified for regular file systems below. 24997c478bd9Sstevel@tonic-gate * 25007c478bd9Sstevel@tonic-gate * Since the actual mount point is not known until the dependent mounts 25017c478bd9Sstevel@tonic-gate * are performed, we don't attempt any path validation here: that will 25027c478bd9Sstevel@tonic-gate * happen later when zoneadmd actually does the mounts. 25037c478bd9Sstevel@tonic-gate */ 25047c478bd9Sstevel@tonic-gate if (zonecfg_setfsent(handle) != Z_OK) { 25050b5de56dSgjelinek (void) fprintf(stderr, gettext("could not verify file systems: " 25067c478bd9Sstevel@tonic-gate "unable to enumerate mounts\n")); 25077c478bd9Sstevel@tonic-gate return (Z_ERR); 25087c478bd9Sstevel@tonic-gate } 25097c478bd9Sstevel@tonic-gate while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 25107c478bd9Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) { 25117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 25127c478bd9Sstevel@tonic-gate "type %s is not allowed.\n"), fstab.zone_fs_dir, 25137c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 25147c478bd9Sstevel@tonic-gate return_code = Z_ERR; 25157c478bd9Sstevel@tonic-gate goto next_fs; 25167c478bd9Sstevel@tonic-gate } 25177c478bd9Sstevel@tonic-gate /* 25187c478bd9Sstevel@tonic-gate * Verify /usr/lib/fs/<fstype>/mount exists. 25197c478bd9Sstevel@tonic-gate */ 25207c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", 25217c478bd9Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 25227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 25237c478bd9Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 25247c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 25257c478bd9Sstevel@tonic-gate return_code = Z_ERR; 25267c478bd9Sstevel@tonic-gate goto next_fs; 25277c478bd9Sstevel@tonic-gate } 25287c478bd9Sstevel@tonic-gate if (stat(cmdbuf, &st) != 0) { 25295ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 25305ee84fbdSgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 25317c478bd9Sstevel@tonic-gate cmdbuf, strerror(errno)); 25327c478bd9Sstevel@tonic-gate return_code = Z_ERR; 25337c478bd9Sstevel@tonic-gate goto next_fs; 25347c478bd9Sstevel@tonic-gate } 25357c478bd9Sstevel@tonic-gate if (!S_ISREG(st.st_mode)) { 25365ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 25375ee84fbdSgjelinek "%s: %s is not a regular file\n"), 25385ee84fbdSgjelinek fstab.zone_fs_dir, cmdbuf); 25397c478bd9Sstevel@tonic-gate return_code = Z_ERR; 25407c478bd9Sstevel@tonic-gate goto next_fs; 25417c478bd9Sstevel@tonic-gate } 25427c478bd9Sstevel@tonic-gate /* 2543*93239addSjohnlev * If zone_fs_raw is set, verify that there's an fsck 2544*93239addSjohnlev * binary for it. If zone_fs_raw is not set, and it's 2545*93239addSjohnlev * not a regular file (lofi mount), and there's an fsck 2546*93239addSjohnlev * binary for it, complain. 25477c478bd9Sstevel@tonic-gate */ 25487c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", 25497c478bd9Sstevel@tonic-gate fstab.zone_fs_type) > sizeof (cmdbuf)) { 25507c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 25517c478bd9Sstevel@tonic-gate "type %s is too long.\n"), fstab.zone_fs_dir, 25527c478bd9Sstevel@tonic-gate fstab.zone_fs_type); 25537c478bd9Sstevel@tonic-gate return_code = Z_ERR; 25547c478bd9Sstevel@tonic-gate goto next_fs; 25557c478bd9Sstevel@tonic-gate } 25567c478bd9Sstevel@tonic-gate if (fstab.zone_fs_raw[0] != '\0' && 25577c478bd9Sstevel@tonic-gate (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) { 25587c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot verify fs %s: " 25597c478bd9Sstevel@tonic-gate "'raw' device specified but " 25607c478bd9Sstevel@tonic-gate "no fsck executable exists for %s\n"), 25617c478bd9Sstevel@tonic-gate fstab.zone_fs_dir, fstab.zone_fs_type); 25627c478bd9Sstevel@tonic-gate return_code = Z_ERR; 25637c478bd9Sstevel@tonic-gate goto next_fs; 2564*93239addSjohnlev } else if (fstab.zone_fs_raw[0] == '\0' && 2565*93239addSjohnlev stat(cmdbuf, &st) == 0 && 2566*93239addSjohnlev isregfile(fstab.zone_fs_special) != 1) { 2567*93239addSjohnlev (void) fprintf(stderr, gettext("could not verify fs " 2568*93239addSjohnlev "%s: must specify 'raw' device for %s " 2569*93239addSjohnlev "file systems\n"), 2570*93239addSjohnlev fstab.zone_fs_dir, fstab.zone_fs_type); 2571*93239addSjohnlev return_code = Z_ERR; 2572*93239addSjohnlev goto next_fs; 25737c478bd9Sstevel@tonic-gate } 257420c8013fSlling 257520c8013fSlling /* Verify fs_special. */ 257620c8013fSlling if ((return_code = verify_fs_special(&fstab)) != Z_OK) 2577b5abaf04Sgjelinek goto next_fs; 257820c8013fSlling 257920c8013fSlling /* Verify fs_raw. */ 2580b5abaf04Sgjelinek if (fstab.zone_fs_raw[0] != '\0' && 2581b5abaf04Sgjelinek stat(fstab.zone_fs_raw, &st) != 0) { 25825ee84fbdSgjelinek /* 25835ee84fbdSgjelinek * TRANSLATION_NOTE 25845ee84fbdSgjelinek * fs is a literal that should not be translated. 25855ee84fbdSgjelinek */ 25865ee84fbdSgjelinek (void) fprintf(stderr, gettext("could not verify fs " 25875ee84fbdSgjelinek "%s: could not access %s: %s\n"), fstab.zone_fs_dir, 2588b5abaf04Sgjelinek fstab.zone_fs_raw, strerror(errno)); 2589b5abaf04Sgjelinek return_code = Z_ERR; 2590b5abaf04Sgjelinek goto next_fs; 2591b5abaf04Sgjelinek } 25927c478bd9Sstevel@tonic-gate next_fs: 25937c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(fstab.zone_fs_options); 25947c478bd9Sstevel@tonic-gate } 25957c478bd9Sstevel@tonic-gate (void) zonecfg_endfsent(handle); 25967c478bd9Sstevel@tonic-gate 25977c478bd9Sstevel@tonic-gate return (return_code); 25987c478bd9Sstevel@tonic-gate } 25997c478bd9Sstevel@tonic-gate 26007c478bd9Sstevel@tonic-gate static int 2601ffbafc53Scomay verify_limitpriv(zone_dochandle_t handle) 2602ffbafc53Scomay { 2603ffbafc53Scomay char *privname = NULL; 2604ffbafc53Scomay int err; 2605ffbafc53Scomay priv_set_t *privs; 2606ffbafc53Scomay 2607ffbafc53Scomay if ((privs = priv_allocset()) == NULL) { 2608ffbafc53Scomay zperror(gettext("failed to allocate privilege set"), B_FALSE); 2609ffbafc53Scomay return (Z_NOMEM); 2610ffbafc53Scomay } 2611ffbafc53Scomay err = zonecfg_get_privset(handle, privs, &privname); 2612ffbafc53Scomay switch (err) { 2613ffbafc53Scomay case Z_OK: 2614ffbafc53Scomay break; 2615ffbafc53Scomay case Z_PRIV_PROHIBITED: 2616ffbafc53Scomay (void) fprintf(stderr, gettext("privilege \"%s\" is not " 2617ffbafc53Scomay "permitted within the zone's privilege set\n"), privname); 2618ffbafc53Scomay break; 2619ffbafc53Scomay case Z_PRIV_REQUIRED: 2620ffbafc53Scomay (void) fprintf(stderr, gettext("required privilege \"%s\" is " 2621ffbafc53Scomay "missing from the zone's privilege set\n"), privname); 2622ffbafc53Scomay break; 2623ffbafc53Scomay case Z_PRIV_UNKNOWN: 2624ffbafc53Scomay (void) fprintf(stderr, gettext("unknown privilege \"%s\" " 2625ffbafc53Scomay "specified in the zone's privilege set\n"), privname); 2626ffbafc53Scomay break; 2627ffbafc53Scomay default: 2628ffbafc53Scomay zperror( 2629ffbafc53Scomay gettext("failed to determine the zone's privilege set"), 2630ffbafc53Scomay B_TRUE); 2631ffbafc53Scomay break; 2632ffbafc53Scomay } 2633ffbafc53Scomay free(privname); 2634ffbafc53Scomay priv_freeset(privs); 2635ffbafc53Scomay return (err); 2636ffbafc53Scomay } 2637ffbafc53Scomay 26381390a385Sgjelinek static void 26391390a385Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list) 26401390a385Sgjelinek { 26411390a385Sgjelinek int i; 26421390a385Sgjelinek 26431390a385Sgjelinek for (i = 0; i < if_cnt; i++) { 26441390a385Sgjelinek free(if_list[i]->name); 26451390a385Sgjelinek free(if_list[i]); 26461390a385Sgjelinek } 26471390a385Sgjelinek free(if_list); 26481390a385Sgjelinek } 26491390a385Sgjelinek 26501390a385Sgjelinek /* 26511390a385Sgjelinek * Get a list of the network interfaces, along with their address families, 26521390a385Sgjelinek * that are plumbed in the global zone. See if_tcp(7p) for a description 26531390a385Sgjelinek * of the ioctls used here. 26541390a385Sgjelinek */ 26551390a385Sgjelinek static int 26561390a385Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list) 26571390a385Sgjelinek { 26581390a385Sgjelinek int s; 26591390a385Sgjelinek int i; 26601390a385Sgjelinek int res = Z_OK; 26611390a385Sgjelinek int space_needed; 26621390a385Sgjelinek int cnt = 0; 26631390a385Sgjelinek struct lifnum if_num; 26641390a385Sgjelinek struct lifconf if_conf; 26651390a385Sgjelinek struct lifreq *if_reqp; 26661390a385Sgjelinek char *if_buf; 26671390a385Sgjelinek struct net_if **local_ifs = NULL; 26681390a385Sgjelinek 26691390a385Sgjelinek *if_cnt = 0; 26701390a385Sgjelinek *if_list = NULL; 26711390a385Sgjelinek 26721390a385Sgjelinek if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0) 26731390a385Sgjelinek return (Z_ERR); 26741390a385Sgjelinek 26751390a385Sgjelinek /* 26761390a385Sgjelinek * Come back here in the unlikely event that the number of interfaces 26771390a385Sgjelinek * increases between the time we get the count and the time we do the 26781390a385Sgjelinek * SIOCGLIFCONF ioctl. 26791390a385Sgjelinek */ 26801390a385Sgjelinek retry: 26811390a385Sgjelinek /* Get the number of interfaces. */ 26821390a385Sgjelinek if_num.lifn_family = AF_UNSPEC; 26831390a385Sgjelinek if_num.lifn_flags = LIFC_NOXMIT; 26841390a385Sgjelinek if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) { 26851390a385Sgjelinek (void) close(s); 26861390a385Sgjelinek return (Z_ERR); 26871390a385Sgjelinek } 26881390a385Sgjelinek 26891390a385Sgjelinek /* Get the interface configuration list. */ 26901390a385Sgjelinek space_needed = if_num.lifn_count * sizeof (struct lifreq); 26911390a385Sgjelinek if ((if_buf = malloc(space_needed)) == NULL) { 26921390a385Sgjelinek (void) close(s); 26931390a385Sgjelinek return (Z_ERR); 26941390a385Sgjelinek } 26951390a385Sgjelinek if_conf.lifc_family = AF_UNSPEC; 26961390a385Sgjelinek if_conf.lifc_flags = LIFC_NOXMIT; 26971390a385Sgjelinek if_conf.lifc_len = space_needed; 26981390a385Sgjelinek if_conf.lifc_buf = if_buf; 26991390a385Sgjelinek if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) { 27001390a385Sgjelinek free(if_buf); 27011390a385Sgjelinek /* 27021390a385Sgjelinek * SIOCGLIFCONF returns EINVAL if the buffer we passed in is 27031390a385Sgjelinek * too small. In this case go back and get the new if cnt. 27041390a385Sgjelinek */ 27051390a385Sgjelinek if (errno == EINVAL) 27061390a385Sgjelinek goto retry; 27071390a385Sgjelinek 27081390a385Sgjelinek (void) close(s); 27091390a385Sgjelinek return (Z_ERR); 27101390a385Sgjelinek } 27111390a385Sgjelinek (void) close(s); 27121390a385Sgjelinek 27131390a385Sgjelinek /* Get the name and address family for each interface. */ 27141390a385Sgjelinek if_reqp = if_conf.lifc_req; 27151390a385Sgjelinek for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) { 27161390a385Sgjelinek struct net_if **p; 27171390a385Sgjelinek struct lifreq req; 27181390a385Sgjelinek 27191390a385Sgjelinek if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) { 27201390a385Sgjelinek if_reqp++; 27211390a385Sgjelinek continue; 27221390a385Sgjelinek } 27231390a385Sgjelinek 27241390a385Sgjelinek if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family), 27251390a385Sgjelinek SOCK_DGRAM, 0)) == -1) { 27261390a385Sgjelinek res = Z_ERR; 27271390a385Sgjelinek break; 27281390a385Sgjelinek } 27291390a385Sgjelinek 27301390a385Sgjelinek (void) strncpy(req.lifr_name, if_reqp->lifr_name, 27311390a385Sgjelinek sizeof (req.lifr_name)); 27321390a385Sgjelinek if (ioctl(s, SIOCGLIFADDR, &req) < 0) { 27331390a385Sgjelinek (void) close(s); 27341390a385Sgjelinek if_reqp++; 27351390a385Sgjelinek continue; 27361390a385Sgjelinek } 27371390a385Sgjelinek 27381390a385Sgjelinek if ((p = (struct net_if **)realloc(local_ifs, 27391390a385Sgjelinek sizeof (struct net_if *) * (cnt + 1))) == NULL) { 27401390a385Sgjelinek res = Z_ERR; 27411390a385Sgjelinek break; 27421390a385Sgjelinek } 27431390a385Sgjelinek local_ifs = p; 27441390a385Sgjelinek 27451390a385Sgjelinek if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) { 27461390a385Sgjelinek res = Z_ERR; 27471390a385Sgjelinek break; 27481390a385Sgjelinek } 27491390a385Sgjelinek 27501390a385Sgjelinek if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name)) 27511390a385Sgjelinek == NULL) { 27521390a385Sgjelinek free(local_ifs[cnt]); 27531390a385Sgjelinek res = Z_ERR; 27541390a385Sgjelinek break; 27551390a385Sgjelinek } 27561390a385Sgjelinek local_ifs[cnt]->af = req.lifr_addr.ss_family; 27571390a385Sgjelinek cnt++; 27581390a385Sgjelinek 27591390a385Sgjelinek (void) close(s); 27601390a385Sgjelinek if_reqp++; 27611390a385Sgjelinek } 27621390a385Sgjelinek 27631390a385Sgjelinek free(if_buf); 27641390a385Sgjelinek 27651390a385Sgjelinek if (res != Z_OK) { 27661390a385Sgjelinek free_local_netifs(cnt, local_ifs); 27671390a385Sgjelinek } else { 27681390a385Sgjelinek *if_cnt = cnt; 27691390a385Sgjelinek *if_list = local_ifs; 27701390a385Sgjelinek } 27711390a385Sgjelinek 27721390a385Sgjelinek return (res); 27731390a385Sgjelinek } 27741390a385Sgjelinek 27751390a385Sgjelinek static char * 27761390a385Sgjelinek af2str(int af) 27771390a385Sgjelinek { 27781390a385Sgjelinek switch (af) { 27791390a385Sgjelinek case AF_INET: 27801390a385Sgjelinek return ("IPv4"); 27811390a385Sgjelinek case AF_INET6: 27821390a385Sgjelinek return ("IPv6"); 27831390a385Sgjelinek default: 27841390a385Sgjelinek return ("Unknown"); 27851390a385Sgjelinek } 27861390a385Sgjelinek } 27871390a385Sgjelinek 27881390a385Sgjelinek /* 27891390a385Sgjelinek * Cross check the network interface name and address family with the 27901390a385Sgjelinek * interfaces that are set up in the global zone so that we can print the 27911390a385Sgjelinek * appropriate error message. 27921390a385Sgjelinek */ 27931390a385Sgjelinek static void 27941390a385Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg) 27951390a385Sgjelinek { 27961390a385Sgjelinek int i; 27971390a385Sgjelinek int local_if_cnt = 0; 27981390a385Sgjelinek struct net_if **local_ifs = NULL; 27991390a385Sgjelinek boolean_t found_if = B_FALSE; 28001390a385Sgjelinek boolean_t found_af = B_FALSE; 28011390a385Sgjelinek 28021390a385Sgjelinek if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) { 28031390a385Sgjelinek (void) fprintf(stderr, 28041390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 28051390a385Sgjelinek "net", "address", addr, "physical", phys, msg); 28061390a385Sgjelinek return; 28071390a385Sgjelinek } 28081390a385Sgjelinek 28091390a385Sgjelinek for (i = 0; i < local_if_cnt; i++) { 28101390a385Sgjelinek if (strcmp(phys, local_ifs[i]->name) == 0) { 28111390a385Sgjelinek found_if = B_TRUE; 28121390a385Sgjelinek if (af == local_ifs[i]->af) { 28131390a385Sgjelinek found_af = B_TRUE; 28141390a385Sgjelinek break; 28151390a385Sgjelinek } 28161390a385Sgjelinek } 28171390a385Sgjelinek } 28181390a385Sgjelinek 28191390a385Sgjelinek free_local_netifs(local_if_cnt, local_ifs); 28201390a385Sgjelinek 28211390a385Sgjelinek if (!found_if) { 28221390a385Sgjelinek (void) fprintf(stderr, 28231390a385Sgjelinek gettext("could not verify %s %s=%s\n\t" 28241390a385Sgjelinek "network interface %s is not plumbed in the global zone\n"), 28251390a385Sgjelinek "net", "physical", phys, phys); 28261390a385Sgjelinek return; 28271390a385Sgjelinek } 28281390a385Sgjelinek 28291390a385Sgjelinek /* 28301390a385Sgjelinek * Print this error if we were unable to find the address family 28311390a385Sgjelinek * for this interface. If the af variable is not initialized to 28321390a385Sgjelinek * to something meaningful by the caller (not AF_UNSPEC) then we 28331390a385Sgjelinek * also skip this message since it wouldn't be informative. 28341390a385Sgjelinek */ 28351390a385Sgjelinek if (!found_af && af != AF_UNSPEC) { 28361390a385Sgjelinek (void) fprintf(stderr, 28371390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\tthe %s address " 2838f4b3ec61Sdh155122 "family is not configured on this network interface in " 2839f4b3ec61Sdh155122 "the\n\tglobal zone\n"), 28401390a385Sgjelinek "net", "address", addr, "physical", phys, af2str(af)); 28411390a385Sgjelinek return; 28421390a385Sgjelinek } 28431390a385Sgjelinek 28441390a385Sgjelinek (void) fprintf(stderr, 28451390a385Sgjelinek gettext("could not verify %s %s=%s %s=%s\n\t%s\n"), 28461390a385Sgjelinek "net", "address", addr, "physical", phys, msg); 28471390a385Sgjelinek } 28481390a385Sgjelinek 2849ffbafc53Scomay static int 2850ce28b40eSzt129084 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[]) 28517c478bd9Sstevel@tonic-gate { 28527c478bd9Sstevel@tonic-gate struct zone_nwiftab nwiftab; 28537c478bd9Sstevel@tonic-gate int return_code = Z_OK; 28547c478bd9Sstevel@tonic-gate int err; 2855108322fbScarlsonj boolean_t in_alt_root; 2856f4b3ec61Sdh155122 zone_iptype_t iptype; 2857948f2876Sss150715 dlpi_handle_t dh; 28587c478bd9Sstevel@tonic-gate 2859108322fbScarlsonj in_alt_root = zonecfg_in_alt_root(); 2860108322fbScarlsonj if (in_alt_root) 2861108322fbScarlsonj goto no_net; 2862108322fbScarlsonj 2863f4b3ec61Sdh155122 if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) { 2864f4b3ec61Sdh155122 errno = err; 2865f4b3ec61Sdh155122 zperror(cmd_to_str(cmd_num), B_TRUE); 2866f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 2867f4b3ec61Sdh155122 return (Z_ERR); 2868f4b3ec61Sdh155122 } 28697c478bd9Sstevel@tonic-gate if ((err = zonecfg_setnwifent(handle)) != Z_OK) { 28707c478bd9Sstevel@tonic-gate errno = err; 28717c478bd9Sstevel@tonic-gate zperror(cmd_to_str(cmd_num), B_TRUE); 28727c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 28737c478bd9Sstevel@tonic-gate return (Z_ERR); 28747c478bd9Sstevel@tonic-gate } 28757c478bd9Sstevel@tonic-gate while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) { 28767c478bd9Sstevel@tonic-gate struct lifreq lifr; 28771390a385Sgjelinek sa_family_t af = AF_UNSPEC; 2878f4b3ec61Sdh155122 char dl_owner_zname[ZONENAME_MAX]; 2879f4b3ec61Sdh155122 zoneid_t dl_owner_zid; 2880f4b3ec61Sdh155122 zoneid_t target_zid; 2881f4b3ec61Sdh155122 int res; 28827c478bd9Sstevel@tonic-gate 28837c478bd9Sstevel@tonic-gate /* skip any loopback interfaces */ 28847c478bd9Sstevel@tonic-gate if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0) 28857c478bd9Sstevel@tonic-gate continue; 2886f4b3ec61Sdh155122 switch (iptype) { 2887f4b3ec61Sdh155122 case ZS_SHARED: 2888f4b3ec61Sdh155122 if ((res = zonecfg_valid_net_address( 2889f4b3ec61Sdh155122 nwiftab.zone_nwif_address, &lifr)) != Z_OK) { 28901390a385Sgjelinek print_net_err(nwiftab.zone_nwif_physical, 28911390a385Sgjelinek nwiftab.zone_nwif_address, af, 28921390a385Sgjelinek zonecfg_strerror(res)); 28937c478bd9Sstevel@tonic-gate return_code = Z_ERR; 28947c478bd9Sstevel@tonic-gate continue; 28957c478bd9Sstevel@tonic-gate } 28967c478bd9Sstevel@tonic-gate af = lifr.lifr_addr.ss_family; 2897f4b3ec61Sdh155122 if (!zonecfg_ifname_exists(af, 2898f4b3ec61Sdh155122 nwiftab.zone_nwif_physical)) { 289922321485Svp157776 /* 2900f4b3ec61Sdh155122 * The interface failed to come up. We continue 2901f4b3ec61Sdh155122 * on anyway for the sake of consistency: a 2902f4b3ec61Sdh155122 * zone is not shut down if the interface fails 2903f4b3ec61Sdh155122 * any time after boot, nor does the global zone 2904f4b3ec61Sdh155122 * fail to boot if an interface fails. 290522321485Svp157776 */ 290622321485Svp157776 (void) fprintf(stderr, 2907f4b3ec61Sdh155122 gettext("WARNING: skipping network " 2908f4b3ec61Sdh155122 "interface '%s' which may not be " 2909f4b3ec61Sdh155122 "present/plumbed in the global " 2910f4b3ec61Sdh155122 "zone.\n"), 291122321485Svp157776 nwiftab.zone_nwif_physical); 29127c478bd9Sstevel@tonic-gate } 2913f4b3ec61Sdh155122 break; 2914f4b3ec61Sdh155122 case ZS_EXCLUSIVE: 2915f4b3ec61Sdh155122 /* Warning if it exists for either IPv4 or IPv6 */ 2916f4b3ec61Sdh155122 2917f4b3ec61Sdh155122 if (zonecfg_ifname_exists(AF_INET, 2918f4b3ec61Sdh155122 nwiftab.zone_nwif_physical) || 2919f4b3ec61Sdh155122 zonecfg_ifname_exists(AF_INET6, 2920f4b3ec61Sdh155122 nwiftab.zone_nwif_physical)) { 2921f4b3ec61Sdh155122 (void) fprintf(stderr, 2922f4b3ec61Sdh155122 gettext("WARNING: skipping network " 2923f4b3ec61Sdh155122 "interface '%s' which is used in the " 2924f4b3ec61Sdh155122 "global zone.\n"), 2925f4b3ec61Sdh155122 nwiftab.zone_nwif_physical); 2926f4b3ec61Sdh155122 break; 2927f4b3ec61Sdh155122 } 2928948f2876Sss150715 2929f4b3ec61Sdh155122 /* 2930948f2876Sss150715 * Verify that the physical interface can be opened. 2931f4b3ec61Sdh155122 */ 2932948f2876Sss150715 err = dlpi_open(nwiftab.zone_nwif_physical, &dh, 0); 2933948f2876Sss150715 if (err != DLPI_SUCCESS) { 2934f4b3ec61Sdh155122 (void) fprintf(stderr, 2935f4b3ec61Sdh155122 gettext("WARNING: skipping network " 2936948f2876Sss150715 "interface '%s' which cannot be opened: " 2937948f2876Sss150715 "dlpi error (%s).\n"), 2938948f2876Sss150715 nwiftab.zone_nwif_physical, 2939948f2876Sss150715 dlpi_strerror(err)); 2940f4b3ec61Sdh155122 break; 2941f4b3ec61Sdh155122 } else { 2942948f2876Sss150715 dlpi_close(dh); 2943f4b3ec61Sdh155122 } 2944f4b3ec61Sdh155122 /* 2945f4b3ec61Sdh155122 * Verify whether the physical interface is already 2946f4b3ec61Sdh155122 * used by a zone. 2947f4b3ec61Sdh155122 */ 2948f4b3ec61Sdh155122 dl_owner_zid = ALL_ZONES; 2949f4b3ec61Sdh155122 if (zone_check_datalink(&dl_owner_zid, 2950f4b3ec61Sdh155122 nwiftab.zone_nwif_physical) != 0) 2951f4b3ec61Sdh155122 break; 2952f4b3ec61Sdh155122 2953f4b3ec61Sdh155122 /* 2954f4b3ec61Sdh155122 * If the zone being verified is 2955f4b3ec61Sdh155122 * running and owns the interface 2956f4b3ec61Sdh155122 */ 2957f4b3ec61Sdh155122 target_zid = getzoneidbyname(target_zone); 2958f4b3ec61Sdh155122 if (target_zid == dl_owner_zid) 2959f4b3ec61Sdh155122 break; 2960f4b3ec61Sdh155122 2961f4b3ec61Sdh155122 /* Zone id match failed, use name to check */ 2962f4b3ec61Sdh155122 if (getzonenamebyid(dl_owner_zid, dl_owner_zname, 2963f4b3ec61Sdh155122 ZONENAME_MAX) < 0) { 2964f4b3ec61Sdh155122 /* No name, show ID instead */ 2965f4b3ec61Sdh155122 (void) snprintf(dl_owner_zname, ZONENAME_MAX, 2966f4b3ec61Sdh155122 "<%d>", dl_owner_zid); 2967f4b3ec61Sdh155122 } else if (strcmp(dl_owner_zname, target_zone) == 0) 2968f4b3ec61Sdh155122 break; 2969f4b3ec61Sdh155122 2970f4b3ec61Sdh155122 /* 2971f4b3ec61Sdh155122 * Note here we only report a warning that 2972f4b3ec61Sdh155122 * the interface is already in use by another 2973f4b3ec61Sdh155122 * running zone, and the verify process just 2974f4b3ec61Sdh155122 * goes on, if the interface is still in use 2975f4b3ec61Sdh155122 * when this zone really boots up, zoneadmd 2976f4b3ec61Sdh155122 * will find it. If the name of the zone which 2977f4b3ec61Sdh155122 * owns this interface cannot be determined, 2978f4b3ec61Sdh155122 * then it is not possible to determine if there 2979f4b3ec61Sdh155122 * is a conflict so just report it as a warning. 2980f4b3ec61Sdh155122 */ 2981f4b3ec61Sdh155122 (void) fprintf(stderr, 2982f4b3ec61Sdh155122 gettext("WARNING: skipping network interface " 2983f4b3ec61Sdh155122 "'%s' which is used by the non-global zone " 2984f4b3ec61Sdh155122 "'%s'.\n"), nwiftab.zone_nwif_physical, 2985f4b3ec61Sdh155122 dl_owner_zname); 2986f4b3ec61Sdh155122 break; 2987f4b3ec61Sdh155122 } 29887c478bd9Sstevel@tonic-gate } 29897c478bd9Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 2990108322fbScarlsonj no_net: 29917c478bd9Sstevel@tonic-gate 2992e7f3c547Sgjelinek /* verify that lofs has not been excluded from the kernel */ 29938cd327d5Sgjelinek if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH || 29948cd327d5Sgjelinek cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) && 29958cd327d5Sgjelinek modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) { 2996e7f3c547Sgjelinek if (errno == ENXIO) 2997e7f3c547Sgjelinek (void) fprintf(stderr, gettext("could not verify " 2998e7f3c547Sgjelinek "lofs(7FS): possibly excluded in /etc/system\n")); 2999e7f3c547Sgjelinek else 3000e7f3c547Sgjelinek (void) fprintf(stderr, gettext("could not verify " 3001e7f3c547Sgjelinek "lofs(7FS): %s\n"), strerror(errno)); 3002e7f3c547Sgjelinek return_code = Z_ERR; 3003e7f3c547Sgjelinek } 3004e7f3c547Sgjelinek 30057c478bd9Sstevel@tonic-gate if (verify_filesystems(handle) != Z_OK) 30067c478bd9Sstevel@tonic-gate return_code = Z_ERR; 3007b5abaf04Sgjelinek if (verify_ipd(handle) != Z_OK) 3008b5abaf04Sgjelinek return_code = Z_ERR; 3009108322fbScarlsonj if (!in_alt_root && verify_rctls(handle) != Z_OK) 30107c478bd9Sstevel@tonic-gate return_code = Z_ERR; 3011108322fbScarlsonj if (!in_alt_root && verify_pool(handle) != Z_OK) 30127c478bd9Sstevel@tonic-gate return_code = Z_ERR; 3013ce28b40eSzt129084 if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK) 30149acbbeafSnn35248 return_code = Z_ERR; 3015fa9e4066Sahrens if (!in_alt_root && verify_datasets(handle) != Z_OK) 3016fa9e4066Sahrens return_code = Z_ERR; 3017ffbafc53Scomay 3018ffbafc53Scomay /* 3019ffbafc53Scomay * As the "mount" command is used for patching/upgrading of zones 3020ffbafc53Scomay * or other maintenance processes, the zone's privilege set is not 3021ffbafc53Scomay * checked in this case. Instead, the default, safe set of 3022ffbafc53Scomay * privileges will be used when this zone is created in the 3023ffbafc53Scomay * kernel. 3024ffbafc53Scomay */ 3025ffbafc53Scomay if (!in_alt_root && cmd_num != CMD_MOUNT && 3026ffbafc53Scomay verify_limitpriv(handle) != Z_OK) 3027ffbafc53Scomay return_code = Z_ERR; 30288cd327d5Sgjelinek 30298cd327d5Sgjelinek return (return_code); 30308cd327d5Sgjelinek } 30318cd327d5Sgjelinek 30328cd327d5Sgjelinek static int 3033ce28b40eSzt129084 verify_details(int cmd_num, char *argv[]) 30348cd327d5Sgjelinek { 30358cd327d5Sgjelinek zone_dochandle_t handle; 30368cd327d5Sgjelinek char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN]; 30378cd327d5Sgjelinek int return_code = Z_OK; 30388cd327d5Sgjelinek int err; 30398cd327d5Sgjelinek 30408cd327d5Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 30418cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 30428cd327d5Sgjelinek return (Z_ERR); 30438cd327d5Sgjelinek } 30448cd327d5Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 30458cd327d5Sgjelinek errno = err; 30468cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 30478cd327d5Sgjelinek zonecfg_fini_handle(handle); 30488cd327d5Sgjelinek return (Z_ERR); 30498cd327d5Sgjelinek } 30508cd327d5Sgjelinek if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) != 30518cd327d5Sgjelinek Z_OK) { 30528cd327d5Sgjelinek errno = err; 30538cd327d5Sgjelinek zperror(cmd_to_str(cmd_num), B_TRUE); 30548cd327d5Sgjelinek zonecfg_fini_handle(handle); 30558cd327d5Sgjelinek return (Z_ERR); 30568cd327d5Sgjelinek } 30578cd327d5Sgjelinek /* 30588cd327d5Sgjelinek * zonecfg_get_zonepath() gets its data from the XML repository. 30598cd327d5Sgjelinek * Verify this against the index file, which is checked first by 30608cd327d5Sgjelinek * zone_get_zonepath(). If they don't match, bail out. 30618cd327d5Sgjelinek */ 30628cd327d5Sgjelinek if ((err = zone_get_zonepath(target_zone, checkpath, 30638cd327d5Sgjelinek sizeof (checkpath))) != Z_OK) { 30648cd327d5Sgjelinek errno = err; 30658cd327d5Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 3066e767a340Sgjelinek zonecfg_fini_handle(handle); 30678cd327d5Sgjelinek return (Z_ERR); 30688cd327d5Sgjelinek } 30698cd327d5Sgjelinek if (strcmp(zonepath, checkpath) != 0) { 30708cd327d5Sgjelinek /* 30718cd327d5Sgjelinek * TRANSLATION_NOTE 30728cd327d5Sgjelinek * XML and zonepath are literals that should not be translated. 30738cd327d5Sgjelinek */ 30748cd327d5Sgjelinek (void) fprintf(stderr, gettext("The XML repository has " 30758cd327d5Sgjelinek "zonepath '%s',\nbut the index file has zonepath '%s'.\n" 30768cd327d5Sgjelinek "These must match, so fix the incorrect entry.\n"), 30778cd327d5Sgjelinek zonepath, checkpath); 3078e767a340Sgjelinek zonecfg_fini_handle(handle); 30798cd327d5Sgjelinek return (Z_ERR); 30808cd327d5Sgjelinek } 30818cd327d5Sgjelinek if (validate_zonepath(zonepath, cmd_num) != Z_OK) { 30828cd327d5Sgjelinek (void) fprintf(stderr, gettext("could not verify zonepath %s " 30838cd327d5Sgjelinek "because of the above errors.\n"), zonepath); 30848cd327d5Sgjelinek return_code = Z_ERR; 30858cd327d5Sgjelinek } 30868cd327d5Sgjelinek 3087ce28b40eSzt129084 if (verify_handle(cmd_num, handle, argv) != Z_OK) 30888cd327d5Sgjelinek return_code = Z_ERR; 30898cd327d5Sgjelinek 30907c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 30917c478bd9Sstevel@tonic-gate if (return_code == Z_ERR) 30927c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 30937c478bd9Sstevel@tonic-gate gettext("%s: zone %s failed to verify\n"), 30947c478bd9Sstevel@tonic-gate execname, target_zone); 30957c478bd9Sstevel@tonic-gate return (return_code); 30967c478bd9Sstevel@tonic-gate } 30977c478bd9Sstevel@tonic-gate 30987c478bd9Sstevel@tonic-gate static int 30997c478bd9Sstevel@tonic-gate verify_func(int argc, char *argv[]) 31007c478bd9Sstevel@tonic-gate { 31017c478bd9Sstevel@tonic-gate int arg; 31027c478bd9Sstevel@tonic-gate 31037c478bd9Sstevel@tonic-gate optind = 0; 31047c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 31057c478bd9Sstevel@tonic-gate switch (arg) { 31067c478bd9Sstevel@tonic-gate case '?': 31077c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 31087c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 31097c478bd9Sstevel@tonic-gate default: 31107c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 31117c478bd9Sstevel@tonic-gate return (Z_USAGE); 31127c478bd9Sstevel@tonic-gate } 31137c478bd9Sstevel@tonic-gate } 31147c478bd9Sstevel@tonic-gate if (argc > optind) { 31157c478bd9Sstevel@tonic-gate sub_usage(SHELP_VERIFY, CMD_VERIFY); 31167c478bd9Sstevel@tonic-gate return (Z_USAGE); 31177c478bd9Sstevel@tonic-gate } 31189acbbeafSnn35248 if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE) 31199acbbeafSnn35248 != Z_OK) 31207c478bd9Sstevel@tonic-gate return (Z_ERR); 3121ce28b40eSzt129084 return (verify_details(CMD_VERIFY, argv)); 31227c478bd9Sstevel@tonic-gate } 31237c478bd9Sstevel@tonic-gate 31249acbbeafSnn35248 static int 31259acbbeafSnn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize) 31269acbbeafSnn35248 { 31279acbbeafSnn35248 char optstring[4]; 31289acbbeafSnn35248 31299acbbeafSnn35248 if (opt > 0) 31309acbbeafSnn35248 (void) sprintf(optstring, " -%c", opt); 31319acbbeafSnn35248 else 31329acbbeafSnn35248 (void) strcpy(optstring, " "); 31339acbbeafSnn35248 31349acbbeafSnn35248 if ((strlcat(buf, optstring, bufsize) > bufsize)) 31359acbbeafSnn35248 return (Z_ERR); 31369acbbeafSnn35248 if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize)) 31379acbbeafSnn35248 return (Z_ERR); 31389acbbeafSnn35248 return (Z_OK); 31399acbbeafSnn35248 } 31407c478bd9Sstevel@tonic-gate 31417c478bd9Sstevel@tonic-gate static int 31427c478bd9Sstevel@tonic-gate install_func(int argc, char *argv[]) 31437c478bd9Sstevel@tonic-gate { 31449acbbeafSnn35248 char cmdbuf[MAXPATHLEN]; 31451100f00dSgjelinek char postcmdbuf[MAXPATHLEN]; 31467c478bd9Sstevel@tonic-gate int lockfd; 31479acbbeafSnn35248 int arg, err, subproc_err; 31487c478bd9Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 3149123807fbSedp brand_handle_t bh = NULL; 31507c478bd9Sstevel@tonic-gate int status; 31510b5de56dSgjelinek boolean_t nodataset = B_FALSE; 31521100f00dSgjelinek boolean_t do_postinstall = B_FALSE; 31539acbbeafSnn35248 char opts[128]; 31549acbbeafSnn35248 31559acbbeafSnn35248 if (target_zone == NULL) { 31569acbbeafSnn35248 sub_usage(SHELP_INSTALL, CMD_INSTALL); 31579acbbeafSnn35248 return (Z_USAGE); 31589acbbeafSnn35248 } 31597c478bd9Sstevel@tonic-gate 3160108322fbScarlsonj if (zonecfg_in_alt_root()) { 3161108322fbScarlsonj zerror(gettext("cannot install zone in alternate root")); 3162108322fbScarlsonj return (Z_ERR); 3163108322fbScarlsonj } 3164108322fbScarlsonj 31659acbbeafSnn35248 if ((err = zone_get_zonepath(target_zone, zonepath, 31669acbbeafSnn35248 sizeof (zonepath))) != Z_OK) { 31679acbbeafSnn35248 errno = err; 31689acbbeafSnn35248 zperror2(target_zone, gettext("could not get zone path")); 31699acbbeafSnn35248 return (Z_ERR); 31709acbbeafSnn35248 } 31719acbbeafSnn35248 31729acbbeafSnn35248 /* Fetch the install command from the brand configuration. */ 3173123807fbSedp if ((bh = brand_open(target_brand)) == NULL) { 31749acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 31759acbbeafSnn35248 return (Z_ERR); 31769acbbeafSnn35248 } 31779acbbeafSnn35248 31789acbbeafSnn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 3179123807fbSedp if (brand_get_install(bh, target_zone, zonepath, cmdbuf + EXEC_LEN, 31809acbbeafSnn35248 sizeof (cmdbuf) - EXEC_LEN, 0, NULL) != 0) { 31819acbbeafSnn35248 zerror("invalid brand configuration: missing install resource"); 3182123807fbSedp brand_close(bh); 31839acbbeafSnn35248 return (Z_ERR); 31849acbbeafSnn35248 } 31859acbbeafSnn35248 31861100f00dSgjelinek (void) strcpy(postcmdbuf, EXEC_PREFIX); 31871100f00dSgjelinek if (brand_get_postinstall(bh, target_zone, zonepath, 31881100f00dSgjelinek postcmdbuf + EXEC_LEN, sizeof (postcmdbuf) - EXEC_LEN, 0, NULL) 31891100f00dSgjelinek != 0) { 31901100f00dSgjelinek zerror("invalid brand configuration: missing postinstall " 31911100f00dSgjelinek "resource"); 31921100f00dSgjelinek brand_close(bh); 31931100f00dSgjelinek return (Z_ERR); 31941100f00dSgjelinek } else if (strlen(postcmdbuf) > EXEC_LEN) { 31951100f00dSgjelinek do_postinstall = B_TRUE; 31961100f00dSgjelinek } 31971100f00dSgjelinek 31989acbbeafSnn35248 (void) strcpy(opts, "?x:"); 31999acbbeafSnn35248 if (!is_native_zone) { 32009acbbeafSnn35248 /* 32019acbbeafSnn35248 * Fetch the list of recognized command-line options from 32029acbbeafSnn35248 * the brand configuration file. 32039acbbeafSnn35248 */ 3204123807fbSedp if (brand_get_installopts(bh, opts + strlen(opts), 32059acbbeafSnn35248 sizeof (opts) - strlen(opts)) != 0) { 32069acbbeafSnn35248 zerror("invalid brand configuration: missing " 32079acbbeafSnn35248 "install options resource"); 3208123807fbSedp brand_close(bh); 32099acbbeafSnn35248 return (Z_ERR); 32109acbbeafSnn35248 } 32119acbbeafSnn35248 } 3212123807fbSedp brand_close(bh); 32139acbbeafSnn35248 32147c478bd9Sstevel@tonic-gate optind = 0; 32159acbbeafSnn35248 while ((arg = getopt(argc, argv, opts)) != EOF) { 32167c478bd9Sstevel@tonic-gate switch (arg) { 32177c478bd9Sstevel@tonic-gate case '?': 32187c478bd9Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 32197c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 32200b5de56dSgjelinek case 'x': 32210b5de56dSgjelinek if (strcmp(optarg, "nodataset") != 0) { 32220b5de56dSgjelinek sub_usage(SHELP_INSTALL, CMD_INSTALL); 32230b5de56dSgjelinek return (Z_USAGE); 32240b5de56dSgjelinek } 32250b5de56dSgjelinek nodataset = B_TRUE; 32260b5de56dSgjelinek break; 32277c478bd9Sstevel@tonic-gate default: 32289acbbeafSnn35248 if (is_native_zone) { 32297c478bd9Sstevel@tonic-gate sub_usage(SHELP_INSTALL, CMD_INSTALL); 32307c478bd9Sstevel@tonic-gate return (Z_USAGE); 32317c478bd9Sstevel@tonic-gate } 32329acbbeafSnn35248 32339acbbeafSnn35248 /* 32349acbbeafSnn35248 * This option isn't for zoneadm, so append it to 32359acbbeafSnn35248 * the command line passed to the brand-specific 32361100f00dSgjelinek * install and postinstall routines. 32379acbbeafSnn35248 */ 32389acbbeafSnn35248 if (addopt(cmdbuf, optopt, optarg, 32399acbbeafSnn35248 sizeof (cmdbuf)) != Z_OK) { 32409acbbeafSnn35248 zerror("Install command line too long"); 32419acbbeafSnn35248 return (Z_ERR); 32427c478bd9Sstevel@tonic-gate } 32431100f00dSgjelinek if (addopt(postcmdbuf, optopt, optarg, 32441100f00dSgjelinek sizeof (postcmdbuf)) != Z_OK) { 32451100f00dSgjelinek zerror("Post-Install command line too long"); 32461100f00dSgjelinek return (Z_ERR); 32471100f00dSgjelinek } 32489acbbeafSnn35248 break; 32497c478bd9Sstevel@tonic-gate } 32509acbbeafSnn35248 } 32519acbbeafSnn35248 32529acbbeafSnn35248 if (!is_native_zone) { 32539acbbeafSnn35248 for (; optind < argc; optind++) { 32549acbbeafSnn35248 if (addopt(cmdbuf, 0, argv[optind], 32559acbbeafSnn35248 sizeof (cmdbuf)) != Z_OK) { 32569acbbeafSnn35248 zerror("Install command line too long"); 32579acbbeafSnn35248 return (Z_ERR); 32589acbbeafSnn35248 } 32591100f00dSgjelinek if (addopt(postcmdbuf, 0, argv[optind], 32601100f00dSgjelinek sizeof (postcmdbuf)) != Z_OK) { 32611100f00dSgjelinek zerror("Post-Install command line too long"); 32621100f00dSgjelinek return (Z_ERR); 32631100f00dSgjelinek } 32649acbbeafSnn35248 } 32659acbbeafSnn35248 } 32669acbbeafSnn35248 32679acbbeafSnn35248 if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE, B_FALSE) 32689acbbeafSnn35248 != Z_OK) 32697c478bd9Sstevel@tonic-gate return (Z_ERR); 3270ce28b40eSzt129084 if (verify_details(CMD_INSTALL, argv) != Z_OK) 32717c478bd9Sstevel@tonic-gate return (Z_ERR); 32727c478bd9Sstevel@tonic-gate 32737c478bd9Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 32747c478bd9Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 3275865e09a4Sgjelinek "zoneadm"); 32767c478bd9Sstevel@tonic-gate return (Z_ERR); 32777c478bd9Sstevel@tonic-gate } 32787c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 32797c478bd9Sstevel@tonic-gate if (err != Z_OK) { 32807c478bd9Sstevel@tonic-gate errno = err; 32817c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 32827c478bd9Sstevel@tonic-gate goto done; 32837c478bd9Sstevel@tonic-gate } 32847c478bd9Sstevel@tonic-gate 32859acbbeafSnn35248 if (!nodataset) 32869acbbeafSnn35248 create_zfs_zonepath(zonepath); 32879acbbeafSnn35248 32887c478bd9Sstevel@tonic-gate /* 32897c478bd9Sstevel@tonic-gate * According to the Application Packaging Developer's Guide, a 32907c478bd9Sstevel@tonic-gate * "checkinstall" script when included in a package is executed as 32917c478bd9Sstevel@tonic-gate * the user "install", if such a user exists, or by the user 32927c478bd9Sstevel@tonic-gate * "nobody". In order to support this dubious behavior, the path 32937c478bd9Sstevel@tonic-gate * to the zone being constructed is opened up during the life of 32947c478bd9Sstevel@tonic-gate * the command laying down the zone's root file system. Once this 32957c478bd9Sstevel@tonic-gate * has completed, regardless of whether it was successful, the 32967c478bd9Sstevel@tonic-gate * path to the zone is again restricted. 32977c478bd9Sstevel@tonic-gate */ 32987c478bd9Sstevel@tonic-gate if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) { 32997c478bd9Sstevel@tonic-gate zperror(zonepath, B_FALSE); 33007c478bd9Sstevel@tonic-gate err = Z_ERR; 33017c478bd9Sstevel@tonic-gate goto done; 33027c478bd9Sstevel@tonic-gate } 33037c478bd9Sstevel@tonic-gate 33049acbbeafSnn35248 if (is_native_zone) 33057c478bd9Sstevel@tonic-gate status = do_subproc(cmdbuf); 33069acbbeafSnn35248 else 33079acbbeafSnn35248 status = do_subproc_interactive(cmdbuf); 33089acbbeafSnn35248 33097c478bd9Sstevel@tonic-gate if (chmod(zonepath, S_IRWXU) != 0) { 33107c478bd9Sstevel@tonic-gate zperror(zonepath, B_FALSE); 33117c478bd9Sstevel@tonic-gate err = Z_ERR; 33127c478bd9Sstevel@tonic-gate goto done; 33137c478bd9Sstevel@tonic-gate } 33149acbbeafSnn35248 if ((subproc_err = 33159acbbeafSnn35248 subproc_status(gettext("brand-specific installation"), status, 33169acbbeafSnn35248 B_FALSE)) != ZONE_SUBPROC_OK) { 33179acbbeafSnn35248 err = Z_ERR; 33187c478bd9Sstevel@tonic-gate goto done; 33199acbbeafSnn35248 } 33207c478bd9Sstevel@tonic-gate 33217c478bd9Sstevel@tonic-gate if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 33227c478bd9Sstevel@tonic-gate errno = err; 33237c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 33247c478bd9Sstevel@tonic-gate goto done; 33257c478bd9Sstevel@tonic-gate } 33267c478bd9Sstevel@tonic-gate 33271100f00dSgjelinek if (do_postinstall) { 33281100f00dSgjelinek status = do_subproc(postcmdbuf); 33291100f00dSgjelinek 33301100f00dSgjelinek if ((subproc_err = 33311100f00dSgjelinek subproc_status(gettext("brand-specific post-install"), 33321100f00dSgjelinek status, B_FALSE)) != ZONE_SUBPROC_OK) { 33331100f00dSgjelinek err = Z_ERR; 33341100f00dSgjelinek (void) zone_set_state(target_zone, 33351100f00dSgjelinek ZONE_STATE_INCOMPLETE); 33361100f00dSgjelinek } 33371100f00dSgjelinek } 33381100f00dSgjelinek 33397c478bd9Sstevel@tonic-gate done: 33409acbbeafSnn35248 /* 33419acbbeafSnn35248 * If the install script exited with ZONE_SUBPROC_USAGE or 33429acbbeafSnn35248 * ZONE_SUBPROC_NOTCOMPLETE, try to clean up the zone and leave the 33439acbbeafSnn35248 * zone in the CONFIGURED state so that another install can be 33449acbbeafSnn35248 * attempted without requiring an uninstall first. 33459acbbeafSnn35248 */ 33469acbbeafSnn35248 if ((subproc_err == ZONE_SUBPROC_USAGE) || 33479acbbeafSnn35248 (subproc_err == ZONE_SUBPROC_NOTCOMPLETE)) { 33489acbbeafSnn35248 if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 33499acbbeafSnn35248 errno = err; 33509acbbeafSnn35248 zperror2(target_zone, 33519acbbeafSnn35248 gettext("cleaning up zonepath failed")); 33529acbbeafSnn35248 } else if ((err = zone_set_state(target_zone, 33539acbbeafSnn35248 ZONE_STATE_CONFIGURED)) != Z_OK) { 33549acbbeafSnn35248 errno = err; 33559acbbeafSnn35248 zperror2(target_zone, gettext("could not set state")); 33569acbbeafSnn35248 } 33579acbbeafSnn35248 } 33589acbbeafSnn35248 33597c478bd9Sstevel@tonic-gate release_lock_file(lockfd); 33607c478bd9Sstevel@tonic-gate return ((err == Z_OK) ? Z_OK : Z_ERR); 33617c478bd9Sstevel@tonic-gate } 33627c478bd9Sstevel@tonic-gate 33637c478bd9Sstevel@tonic-gate /* 3364865e09a4Sgjelinek * Check that the inherited pkg dirs are the same for the clone and its source. 3365865e09a4Sgjelinek * The easiest way to do that is check that the list of ipds is the same 3366865e09a4Sgjelinek * by matching each one against the other. This algorithm should be fine since 3367865e09a4Sgjelinek * the list of ipds should not be that long. 3368865e09a4Sgjelinek */ 3369865e09a4Sgjelinek static int 3370865e09a4Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone, 3371865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3372865e09a4Sgjelinek { 3373865e09a4Sgjelinek int err; 3374865e09a4Sgjelinek int res = Z_OK; 3375865e09a4Sgjelinek int s_cnt = 0; 3376865e09a4Sgjelinek int t_cnt = 0; 3377865e09a4Sgjelinek struct zone_fstab s_fstab; 3378865e09a4Sgjelinek struct zone_fstab t_fstab; 3379865e09a4Sgjelinek 3380865e09a4Sgjelinek /* 3381865e09a4Sgjelinek * First check the source of the clone against the target. 3382865e09a4Sgjelinek */ 3383865e09a4Sgjelinek if ((err = zonecfg_setipdent(s_handle)) != Z_OK) { 3384865e09a4Sgjelinek errno = err; 3385865e09a4Sgjelinek zperror2(source_zone, gettext("could not enumerate " 3386865e09a4Sgjelinek "inherit-pkg-dirs")); 3387865e09a4Sgjelinek return (Z_ERR); 3388865e09a4Sgjelinek } 3389865e09a4Sgjelinek 3390865e09a4Sgjelinek while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) { 3391865e09a4Sgjelinek boolean_t match = B_FALSE; 3392865e09a4Sgjelinek 3393865e09a4Sgjelinek s_cnt++; 3394865e09a4Sgjelinek 3395865e09a4Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 3396865e09a4Sgjelinek errno = err; 3397865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate " 3398865e09a4Sgjelinek "inherit-pkg-dirs")); 3399865e09a4Sgjelinek (void) zonecfg_endipdent(s_handle); 3400865e09a4Sgjelinek return (Z_ERR); 3401865e09a4Sgjelinek } 3402865e09a4Sgjelinek 3403865e09a4Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) { 3404865e09a4Sgjelinek if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir) 3405865e09a4Sgjelinek == 0) { 3406865e09a4Sgjelinek match = B_TRUE; 3407865e09a4Sgjelinek break; 3408865e09a4Sgjelinek } 3409865e09a4Sgjelinek } 3410865e09a4Sgjelinek (void) zonecfg_endipdent(t_handle); 3411865e09a4Sgjelinek 3412865e09a4Sgjelinek if (!match) { 3413865e09a4Sgjelinek (void) fprintf(stderr, gettext("inherit-pkg-dir " 3414865e09a4Sgjelinek "'%s' is not configured in zone %s.\n"), 3415865e09a4Sgjelinek s_fstab.zone_fs_dir, target_zone); 3416865e09a4Sgjelinek res = Z_ERR; 3417865e09a4Sgjelinek } 3418865e09a4Sgjelinek } 3419865e09a4Sgjelinek 3420865e09a4Sgjelinek (void) zonecfg_endipdent(s_handle); 3421865e09a4Sgjelinek 3422865e09a4Sgjelinek /* skip the next check if we already have errors */ 3423865e09a4Sgjelinek if (res == Z_ERR) 3424865e09a4Sgjelinek return (res); 3425865e09a4Sgjelinek 3426865e09a4Sgjelinek /* 3427865e09a4Sgjelinek * Now check the number of ipds in the target so we can verify 3428865e09a4Sgjelinek * that the source is not a subset of the target. 3429865e09a4Sgjelinek */ 3430865e09a4Sgjelinek if ((err = zonecfg_setipdent(t_handle)) != Z_OK) { 3431865e09a4Sgjelinek errno = err; 3432865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate " 3433865e09a4Sgjelinek "inherit-pkg-dirs")); 3434865e09a4Sgjelinek return (Z_ERR); 3435865e09a4Sgjelinek } 3436865e09a4Sgjelinek 3437865e09a4Sgjelinek while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) 3438865e09a4Sgjelinek t_cnt++; 3439865e09a4Sgjelinek 3440865e09a4Sgjelinek (void) zonecfg_endipdent(t_handle); 3441865e09a4Sgjelinek 3442865e09a4Sgjelinek if (t_cnt != s_cnt) { 3443865e09a4Sgjelinek (void) fprintf(stderr, gettext("Zone %s is configured " 3444865e09a4Sgjelinek "with inherit-pkg-dirs that are not configured in zone " 3445865e09a4Sgjelinek "%s.\n"), target_zone, source_zone); 3446865e09a4Sgjelinek res = Z_ERR; 3447865e09a4Sgjelinek } 3448865e09a4Sgjelinek 3449865e09a4Sgjelinek return (res); 3450865e09a4Sgjelinek } 3451865e09a4Sgjelinek 3452865e09a4Sgjelinek static void 3453865e09a4Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone, 3454865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3455865e09a4Sgjelinek { 3456865e09a4Sgjelinek int err; 3457865e09a4Sgjelinek struct zone_devtab s_devtab; 3458865e09a4Sgjelinek struct zone_devtab t_devtab; 3459865e09a4Sgjelinek 3460865e09a4Sgjelinek if ((err = zonecfg_setdevent(t_handle)) != Z_OK) { 3461865e09a4Sgjelinek errno = err; 3462865e09a4Sgjelinek zperror2(target_zone, gettext("could not enumerate devices")); 3463865e09a4Sgjelinek return; 3464865e09a4Sgjelinek } 3465865e09a4Sgjelinek 3466865e09a4Sgjelinek while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) { 3467865e09a4Sgjelinek if ((err = zonecfg_setdevent(s_handle)) != Z_OK) { 3468865e09a4Sgjelinek errno = err; 3469865e09a4Sgjelinek zperror2(source_zone, 3470865e09a4Sgjelinek gettext("could not enumerate devices")); 3471865e09a4Sgjelinek (void) zonecfg_enddevent(t_handle); 3472865e09a4Sgjelinek return; 3473865e09a4Sgjelinek } 3474865e09a4Sgjelinek 3475865e09a4Sgjelinek while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) { 3476865e09a4Sgjelinek /* 3477865e09a4Sgjelinek * Use fnmatch to catch the case where wildcards 3478865e09a4Sgjelinek * were used in one zone and the other has an 3479865e09a4Sgjelinek * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs. 3480865e09a4Sgjelinek * /dev/\*dsk/c0t0d0s6). 3481865e09a4Sgjelinek */ 3482865e09a4Sgjelinek if (fnmatch(t_devtab.zone_dev_match, 3483865e09a4Sgjelinek s_devtab.zone_dev_match, FNM_PATHNAME) == 0 || 3484865e09a4Sgjelinek fnmatch(s_devtab.zone_dev_match, 3485865e09a4Sgjelinek t_devtab.zone_dev_match, FNM_PATHNAME) == 0) { 3486865e09a4Sgjelinek (void) fprintf(stderr, 3487865e09a4Sgjelinek gettext("WARNING: device '%s' " 3488865e09a4Sgjelinek "is configured in both zones.\n"), 3489865e09a4Sgjelinek t_devtab.zone_dev_match); 3490865e09a4Sgjelinek break; 3491865e09a4Sgjelinek } 3492865e09a4Sgjelinek } 3493865e09a4Sgjelinek (void) zonecfg_enddevent(s_handle); 3494865e09a4Sgjelinek } 3495865e09a4Sgjelinek 3496865e09a4Sgjelinek (void) zonecfg_enddevent(t_handle); 3497865e09a4Sgjelinek } 3498865e09a4Sgjelinek 3499865e09a4Sgjelinek /* 3500865e09a4Sgjelinek * Check if the specified mount option (opt) is contained within the 3501865e09a4Sgjelinek * options string. 3502865e09a4Sgjelinek */ 3503865e09a4Sgjelinek static boolean_t 3504865e09a4Sgjelinek opt_match(char *opt, char *options) 3505865e09a4Sgjelinek { 3506865e09a4Sgjelinek char *p; 3507865e09a4Sgjelinek char *lastp; 3508865e09a4Sgjelinek 3509865e09a4Sgjelinek if ((p = strtok_r(options, ",", &lastp)) != NULL) { 3510865e09a4Sgjelinek if (strcmp(p, opt) == 0) 3511865e09a4Sgjelinek return (B_TRUE); 3512865e09a4Sgjelinek while ((p = strtok_r(NULL, ",", &lastp)) != NULL) { 3513865e09a4Sgjelinek if (strcmp(p, opt) == 0) 3514865e09a4Sgjelinek return (B_TRUE); 3515865e09a4Sgjelinek } 3516865e09a4Sgjelinek } 3517865e09a4Sgjelinek 3518865e09a4Sgjelinek return (B_FALSE); 3519865e09a4Sgjelinek } 3520865e09a4Sgjelinek 35210b5de56dSgjelinek #define RW_LOFS "WARNING: read-write lofs file system on '%s' is configured " \ 3522865e09a4Sgjelinek "in both zones.\n" 3523865e09a4Sgjelinek 3524865e09a4Sgjelinek static void 3525865e09a4Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab) 3526865e09a4Sgjelinek { 3527865e09a4Sgjelinek /* 3528865e09a4Sgjelinek * It is ok to have shared lofs mounted fs but we want to warn if 3529865e09a4Sgjelinek * either is rw since this will effect the other zone. 3530865e09a4Sgjelinek */ 3531865e09a4Sgjelinek if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) { 3532865e09a4Sgjelinek zone_fsopt_t *optp; 3533865e09a4Sgjelinek 3534865e09a4Sgjelinek /* The default is rw so no options means rw */ 3535865e09a4Sgjelinek if (t_fstab->zone_fs_options == NULL || 3536865e09a4Sgjelinek s_fstab->zone_fs_options == NULL) { 3537865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 3538865e09a4Sgjelinek t_fstab->zone_fs_special); 3539865e09a4Sgjelinek return; 3540865e09a4Sgjelinek } 3541865e09a4Sgjelinek 3542865e09a4Sgjelinek for (optp = s_fstab->zone_fs_options; optp != NULL; 3543865e09a4Sgjelinek optp = optp->zone_fsopt_next) { 3544865e09a4Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 3545865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 3546865e09a4Sgjelinek s_fstab->zone_fs_special); 3547865e09a4Sgjelinek return; 3548865e09a4Sgjelinek } 3549865e09a4Sgjelinek } 3550865e09a4Sgjelinek 3551865e09a4Sgjelinek for (optp = t_fstab->zone_fs_options; optp != NULL; 3552865e09a4Sgjelinek optp = optp->zone_fsopt_next) { 3553865e09a4Sgjelinek if (opt_match("rw", optp->zone_fsopt_opt)) { 3554865e09a4Sgjelinek (void) fprintf(stderr, gettext(RW_LOFS), 3555865e09a4Sgjelinek t_fstab->zone_fs_special); 3556865e09a4Sgjelinek return; 3557865e09a4Sgjelinek } 3558865e09a4Sgjelinek } 3559865e09a4Sgjelinek 3560865e09a4Sgjelinek return; 3561865e09a4Sgjelinek } 3562865e09a4Sgjelinek 3563865e09a4Sgjelinek /* 3564865e09a4Sgjelinek * TRANSLATION_NOTE 35650b5de56dSgjelinek * The first variable is the file system type and the second is 35660b5de56dSgjelinek * the file system special device. For example, 35670b5de56dSgjelinek * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ... 3568865e09a4Sgjelinek */ 35690b5de56dSgjelinek (void) fprintf(stderr, gettext("WARNING: %s file system on '%s' " 3570865e09a4Sgjelinek "is configured in both zones.\n"), t_fstab->zone_fs_type, 3571865e09a4Sgjelinek t_fstab->zone_fs_special); 3572865e09a4Sgjelinek } 3573865e09a4Sgjelinek 3574865e09a4Sgjelinek static void 3575865e09a4Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone, 3576865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3577865e09a4Sgjelinek { 3578865e09a4Sgjelinek int err; 3579865e09a4Sgjelinek struct zone_fstab s_fstab; 3580865e09a4Sgjelinek struct zone_fstab t_fstab; 3581865e09a4Sgjelinek 3582865e09a4Sgjelinek if ((err = zonecfg_setfsent(t_handle)) != Z_OK) { 3583865e09a4Sgjelinek errno = err; 3584865e09a4Sgjelinek zperror2(target_zone, 35850b5de56dSgjelinek gettext("could not enumerate file systems")); 3586865e09a4Sgjelinek return; 3587865e09a4Sgjelinek } 3588865e09a4Sgjelinek 3589865e09a4Sgjelinek while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) { 3590865e09a4Sgjelinek if ((err = zonecfg_setfsent(s_handle)) != Z_OK) { 3591865e09a4Sgjelinek errno = err; 3592865e09a4Sgjelinek zperror2(source_zone, 35930b5de56dSgjelinek gettext("could not enumerate file systems")); 3594865e09a4Sgjelinek (void) zonecfg_endfsent(t_handle); 3595865e09a4Sgjelinek return; 3596865e09a4Sgjelinek } 3597865e09a4Sgjelinek 3598865e09a4Sgjelinek while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) { 3599865e09a4Sgjelinek if (strcmp(t_fstab.zone_fs_special, 3600865e09a4Sgjelinek s_fstab.zone_fs_special) == 0) { 3601865e09a4Sgjelinek print_fs_warnings(&s_fstab, &t_fstab); 3602865e09a4Sgjelinek break; 3603865e09a4Sgjelinek } 3604865e09a4Sgjelinek } 3605865e09a4Sgjelinek (void) zonecfg_endfsent(s_handle); 3606865e09a4Sgjelinek } 3607865e09a4Sgjelinek 3608865e09a4Sgjelinek (void) zonecfg_endfsent(t_handle); 3609865e09a4Sgjelinek } 3610865e09a4Sgjelinek 3611865e09a4Sgjelinek /* 3612865e09a4Sgjelinek * We don't catch the case where you used the same IP address but 3613865e09a4Sgjelinek * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128. 3614865e09a4Sgjelinek * However, we're not going to worry about that but we will check for 3615865e09a4Sgjelinek * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24) 3616865e09a4Sgjelinek * and handle that case as a match. 3617865e09a4Sgjelinek */ 3618865e09a4Sgjelinek static void 3619865e09a4Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone, 3620865e09a4Sgjelinek zone_dochandle_t t_handle, char *target_zone) 3621865e09a4Sgjelinek { 3622865e09a4Sgjelinek int err; 3623865e09a4Sgjelinek struct zone_nwiftab s_nwiftab; 3624865e09a4Sgjelinek struct zone_nwiftab t_nwiftab; 3625865e09a4Sgjelinek 3626865e09a4Sgjelinek if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) { 3627865e09a4Sgjelinek errno = err; 3628865e09a4Sgjelinek zperror2(target_zone, 3629865e09a4Sgjelinek gettext("could not enumerate network interfaces")); 3630865e09a4Sgjelinek return; 3631865e09a4Sgjelinek } 3632865e09a4Sgjelinek 3633865e09a4Sgjelinek while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) { 3634865e09a4Sgjelinek char *p; 3635865e09a4Sgjelinek 3636865e09a4Sgjelinek /* remove an (optional) netmask from the address */ 3637865e09a4Sgjelinek if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL) 3638865e09a4Sgjelinek *p = '\0'; 3639865e09a4Sgjelinek 3640865e09a4Sgjelinek if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) { 3641865e09a4Sgjelinek errno = err; 3642865e09a4Sgjelinek zperror2(source_zone, 3643865e09a4Sgjelinek gettext("could not enumerate network interfaces")); 3644865e09a4Sgjelinek (void) zonecfg_endnwifent(t_handle); 3645865e09a4Sgjelinek return; 3646865e09a4Sgjelinek } 3647865e09a4Sgjelinek 3648865e09a4Sgjelinek while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) { 3649865e09a4Sgjelinek /* remove an (optional) netmask from the address */ 3650865e09a4Sgjelinek if ((p = strchr(s_nwiftab.zone_nwif_address, '/')) 3651865e09a4Sgjelinek != NULL) 3652865e09a4Sgjelinek *p = '\0'; 3653865e09a4Sgjelinek 3654f4b3ec61Sdh155122 /* For exclusive-IP zones, address is not specified. */ 3655f4b3ec61Sdh155122 if (strlen(s_nwiftab.zone_nwif_address) == 0) 3656f4b3ec61Sdh155122 continue; 3657f4b3ec61Sdh155122 3658865e09a4Sgjelinek if (strcmp(t_nwiftab.zone_nwif_address, 3659865e09a4Sgjelinek s_nwiftab.zone_nwif_address) == 0) { 3660865e09a4Sgjelinek (void) fprintf(stderr, 3661865e09a4Sgjelinek gettext("WARNING: network address '%s' " 3662865e09a4Sgjelinek "is configured in both zones.\n"), 3663865e09a4Sgjelinek t_nwiftab.zone_nwif_address); 3664865e09a4Sgjelinek break; 3665865e09a4Sgjelinek } 3666865e09a4Sgjelinek } 3667865e09a4Sgjelinek (void) zonecfg_endnwifent(s_handle); 3668865e09a4Sgjelinek } 3669865e09a4Sgjelinek 3670865e09a4Sgjelinek (void) zonecfg_endnwifent(t_handle); 3671865e09a4Sgjelinek } 3672865e09a4Sgjelinek 3673865e09a4Sgjelinek static void 36749acbbeafSnn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source, 36759acbbeafSnn35248 zone_dochandle_t t_handle, char *target) 3676865e09a4Sgjelinek { 3677865e09a4Sgjelinek int err; 3678865e09a4Sgjelinek struct zone_dstab s_dstab; 3679865e09a4Sgjelinek struct zone_dstab t_dstab; 3680865e09a4Sgjelinek 3681865e09a4Sgjelinek if ((err = zonecfg_setdsent(t_handle)) != Z_OK) { 3682865e09a4Sgjelinek errno = err; 36839acbbeafSnn35248 zperror2(target, gettext("could not enumerate datasets")); 3684865e09a4Sgjelinek return; 3685865e09a4Sgjelinek } 3686865e09a4Sgjelinek 3687865e09a4Sgjelinek while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) { 3688865e09a4Sgjelinek if ((err = zonecfg_setdsent(s_handle)) != Z_OK) { 3689865e09a4Sgjelinek errno = err; 36909acbbeafSnn35248 zperror2(source, 3691865e09a4Sgjelinek gettext("could not enumerate datasets")); 3692865e09a4Sgjelinek (void) zonecfg_enddsent(t_handle); 3693865e09a4Sgjelinek return; 3694865e09a4Sgjelinek } 3695865e09a4Sgjelinek 3696865e09a4Sgjelinek while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) { 3697865e09a4Sgjelinek if (strcmp(t_dstab.zone_dataset_name, 3698865e09a4Sgjelinek s_dstab.zone_dataset_name) == 0) { 36999acbbeafSnn35248 target_zone = source; 37009acbbeafSnn35248 zerror(gettext("WARNING: dataset '%s' " 3701865e09a4Sgjelinek "is configured in both zones.\n"), 3702865e09a4Sgjelinek t_dstab.zone_dataset_name); 3703865e09a4Sgjelinek break; 3704865e09a4Sgjelinek } 3705865e09a4Sgjelinek } 3706865e09a4Sgjelinek (void) zonecfg_enddsent(s_handle); 3707865e09a4Sgjelinek } 3708865e09a4Sgjelinek 3709865e09a4Sgjelinek (void) zonecfg_enddsent(t_handle); 3710865e09a4Sgjelinek } 3711865e09a4Sgjelinek 37129acbbeafSnn35248 /* 37139acbbeafSnn35248 * Check that the clone and its source have the same brand type. 37149acbbeafSnn35248 */ 37159acbbeafSnn35248 static int 37169acbbeafSnn35248 valid_brand_clone(char *source_zone, char *target_zone) 37179acbbeafSnn35248 { 3718123807fbSedp brand_handle_t bh; 37199acbbeafSnn35248 char source_brand[MAXNAMELEN]; 37209acbbeafSnn35248 37219acbbeafSnn35248 if ((zone_get_brand(source_zone, source_brand, 37229acbbeafSnn35248 sizeof (source_brand))) != Z_OK) { 37239acbbeafSnn35248 (void) fprintf(stderr, "%s: zone '%s': %s\n", 37249acbbeafSnn35248 execname, source_zone, gettext("missing or invalid brand")); 37259acbbeafSnn35248 return (Z_ERR); 37269acbbeafSnn35248 } 37279acbbeafSnn35248 37289acbbeafSnn35248 if (strcmp(source_brand, target_brand) != NULL) { 37299acbbeafSnn35248 (void) fprintf(stderr, 37309acbbeafSnn35248 gettext("%s: Zones '%s' and '%s' have different brand " 37319acbbeafSnn35248 "types.\n"), execname, source_zone, target_zone); 37329acbbeafSnn35248 return (Z_ERR); 37339acbbeafSnn35248 } 37349acbbeafSnn35248 3735123807fbSedp if ((bh = brand_open(target_brand)) == NULL) { 37369acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 37379acbbeafSnn35248 return (Z_ERR); 37389acbbeafSnn35248 } 3739123807fbSedp brand_close(bh); 37409acbbeafSnn35248 return (Z_OK); 37419acbbeafSnn35248 } 37429acbbeafSnn35248 3743865e09a4Sgjelinek static int 3744865e09a4Sgjelinek validate_clone(char *source_zone, char *target_zone) 3745865e09a4Sgjelinek { 3746865e09a4Sgjelinek int err = Z_OK; 3747865e09a4Sgjelinek zone_dochandle_t s_handle; 3748865e09a4Sgjelinek zone_dochandle_t t_handle; 3749865e09a4Sgjelinek 3750865e09a4Sgjelinek if ((t_handle = zonecfg_init_handle()) == NULL) { 3751865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3752865e09a4Sgjelinek return (Z_ERR); 3753865e09a4Sgjelinek } 3754865e09a4Sgjelinek if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) { 3755865e09a4Sgjelinek errno = err; 3756865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3757865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3758865e09a4Sgjelinek return (Z_ERR); 3759865e09a4Sgjelinek } 3760865e09a4Sgjelinek 3761865e09a4Sgjelinek if ((s_handle = zonecfg_init_handle()) == NULL) { 3762865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3763865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3764865e09a4Sgjelinek return (Z_ERR); 3765865e09a4Sgjelinek } 3766865e09a4Sgjelinek if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) { 3767865e09a4Sgjelinek errno = err; 3768865e09a4Sgjelinek zperror(cmd_to_str(CMD_CLONE), B_TRUE); 3769865e09a4Sgjelinek goto done; 3770865e09a4Sgjelinek } 3771865e09a4Sgjelinek 37729acbbeafSnn35248 /* verify new zone has same brand type */ 37739acbbeafSnn35248 err = valid_brand_clone(source_zone, target_zone); 37749acbbeafSnn35248 if (err != Z_OK) 37759acbbeafSnn35248 goto done; 37769acbbeafSnn35248 3777865e09a4Sgjelinek /* verify new zone has same inherit-pkg-dirs */ 3778865e09a4Sgjelinek err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone); 3779865e09a4Sgjelinek 3780865e09a4Sgjelinek /* warn about imported fs's which are the same */ 3781865e09a4Sgjelinek warn_fs_match(s_handle, source_zone, t_handle, target_zone); 3782865e09a4Sgjelinek 3783865e09a4Sgjelinek /* warn about imported IP addresses which are the same */ 3784865e09a4Sgjelinek warn_ip_match(s_handle, source_zone, t_handle, target_zone); 3785865e09a4Sgjelinek 3786865e09a4Sgjelinek /* warn about imported devices which are the same */ 3787865e09a4Sgjelinek warn_dev_match(s_handle, source_zone, t_handle, target_zone); 3788865e09a4Sgjelinek 3789865e09a4Sgjelinek /* warn about imported datasets which are the same */ 3790865e09a4Sgjelinek warn_dataset_match(s_handle, source_zone, t_handle, target_zone); 3791865e09a4Sgjelinek 3792865e09a4Sgjelinek done: 3793865e09a4Sgjelinek zonecfg_fini_handle(t_handle); 3794865e09a4Sgjelinek zonecfg_fini_handle(s_handle); 3795865e09a4Sgjelinek 3796865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 3797865e09a4Sgjelinek } 3798865e09a4Sgjelinek 3799865e09a4Sgjelinek static int 3800865e09a4Sgjelinek copy_zone(char *src, char *dst) 3801865e09a4Sgjelinek { 3802865e09a4Sgjelinek boolean_t out_null = B_FALSE; 3803865e09a4Sgjelinek int status; 3804865e09a4Sgjelinek char *outfile; 3805865e09a4Sgjelinek char cmdbuf[MAXPATHLEN * 2 + 128]; 3806865e09a4Sgjelinek 3807865e09a4Sgjelinek if ((outfile = tempnam("/var/log", "zone")) == NULL) { 3808865e09a4Sgjelinek outfile = "/dev/null"; 3809865e09a4Sgjelinek out_null = B_TRUE; 3810865e09a4Sgjelinek } 3811865e09a4Sgjelinek 38120b5de56dSgjelinek /* 38130b5de56dSgjelinek * Use find to get the list of files to copy. We need to skip 38140b5de56dSgjelinek * files of type "socket" since cpio can't handle those but that 38150b5de56dSgjelinek * should be ok since the app will recreate the socket when it runs. 38160b5de56dSgjelinek * We also need to filter out anything under the .zfs subdir. Since 38170b5de56dSgjelinek * find is running depth-first, we need the extra egrep to filter .zfs. 38180b5de56dSgjelinek */ 3819865e09a4Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 38200b5de56dSgjelinek "cd %s && /usr/bin/find . -type s -prune -o -depth -print | " 382107b574eeSgjelinek "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | " 3822865e09a4Sgjelinek "/usr/bin/cpio -pdmuP@ %s > %s 2>&1", 3823865e09a4Sgjelinek src, dst, outfile); 3824865e09a4Sgjelinek 3825865e09a4Sgjelinek status = do_subproc(cmdbuf); 3826865e09a4Sgjelinek 38279acbbeafSnn35248 if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) { 3828865e09a4Sgjelinek if (!out_null) 3829865e09a4Sgjelinek (void) fprintf(stderr, gettext("\nThe copy failed.\n" 3830865e09a4Sgjelinek "More information can be found in %s\n"), outfile); 38319acbbeafSnn35248 return (Z_ERR); 3832865e09a4Sgjelinek } 3833865e09a4Sgjelinek 3834865e09a4Sgjelinek if (!out_null) 3835865e09a4Sgjelinek (void) unlink(outfile); 3836865e09a4Sgjelinek 3837865e09a4Sgjelinek return (Z_OK); 3838865e09a4Sgjelinek } 3839865e09a4Sgjelinek 3840865e09a4Sgjelinek static int 38419acbbeafSnn35248 zone_postclone(char *zonepath) 3842865e09a4Sgjelinek { 38439acbbeafSnn35248 char cmdbuf[MAXPATHLEN]; 3844865e09a4Sgjelinek int status; 3845123807fbSedp brand_handle_t bh; 38469acbbeafSnn35248 int err = Z_OK; 38475cd08338Sgjelinek 38485cd08338Sgjelinek /* 38499acbbeafSnn35248 * Fetch the post-clone command, if any, from the brand 38509acbbeafSnn35248 * configuration. 385145916cd2Sjpk */ 3852123807fbSedp if ((bh = brand_open(target_brand)) == NULL) { 38539acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 38545cd08338Sgjelinek return (Z_ERR); 3855865e09a4Sgjelinek } 38569acbbeafSnn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 3857123807fbSedp err = brand_get_postclone(bh, target_zone, zonepath, cmdbuf + EXEC_LEN, 38589acbbeafSnn35248 sizeof (cmdbuf) - EXEC_LEN, 0, NULL); 3859123807fbSedp brand_close(bh); 3860865e09a4Sgjelinek 38619acbbeafSnn35248 if (err == 0 && strlen(cmdbuf) > EXEC_LEN) { 38625cd08338Sgjelinek status = do_subproc(cmdbuf); 38639acbbeafSnn35248 if ((err = subproc_status("postclone", status, B_FALSE)) 38649acbbeafSnn35248 != ZONE_SUBPROC_OK) { 38659acbbeafSnn35248 zerror(gettext("post-clone configuration failed.")); 38669acbbeafSnn35248 err = Z_ERR; 38679acbbeafSnn35248 } 3868865e09a4Sgjelinek } 3869865e09a4Sgjelinek 38709acbbeafSnn35248 return (err); 38715cd08338Sgjelinek } 3872865e09a4Sgjelinek 3873865e09a4Sgjelinek /* ARGSUSED */ 38740b5de56dSgjelinek static int 3875865e09a4Sgjelinek zfm_print(const char *p, void *r) { 3876865e09a4Sgjelinek zerror(" %s\n", p); 3877865e09a4Sgjelinek return (0); 3878865e09a4Sgjelinek } 3879865e09a4Sgjelinek 38800b5de56dSgjelinek int 38810b5de56dSgjelinek clone_copy(char *source_zonepath, char *zonepath) 38820b5de56dSgjelinek { 38830b5de56dSgjelinek int err; 38840b5de56dSgjelinek 38850b5de56dSgjelinek /* Don't clone the zone if anything is still mounted there */ 38860b5de56dSgjelinek if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) { 38870b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 38880b5de56dSgjelinek "subdirectories of %s.\n"), source_zonepath); 38890b5de56dSgjelinek (void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL); 38900b5de56dSgjelinek return (Z_ERR); 38910b5de56dSgjelinek } 38920b5de56dSgjelinek 38930b5de56dSgjelinek /* 38940b5de56dSgjelinek * Attempt to create a ZFS fs for the zonepath. As usual, we don't 38950b5de56dSgjelinek * care if this works or not since we always have the default behavior 38960b5de56dSgjelinek * of a simple directory for the zonepath. 38970b5de56dSgjelinek */ 38980b5de56dSgjelinek create_zfs_zonepath(zonepath); 38990b5de56dSgjelinek 39000b5de56dSgjelinek (void) printf(gettext("Copying %s..."), source_zonepath); 39010b5de56dSgjelinek (void) fflush(stdout); 39020b5de56dSgjelinek 39030b5de56dSgjelinek err = copy_zone(source_zonepath, zonepath); 39040b5de56dSgjelinek 39050b5de56dSgjelinek (void) printf("\n"); 39060b5de56dSgjelinek 39070b5de56dSgjelinek return (err); 39080b5de56dSgjelinek } 39090b5de56dSgjelinek 3910865e09a4Sgjelinek static int 3911865e09a4Sgjelinek clone_func(int argc, char *argv[]) 3912865e09a4Sgjelinek { 3913865e09a4Sgjelinek char *source_zone = NULL; 3914865e09a4Sgjelinek int lockfd; 3915865e09a4Sgjelinek int err, arg; 3916865e09a4Sgjelinek char zonepath[MAXPATHLEN]; 3917865e09a4Sgjelinek char source_zonepath[MAXPATHLEN]; 3918865e09a4Sgjelinek zone_state_t state; 3919865e09a4Sgjelinek zone_entry_t *zent; 39200b5de56dSgjelinek char *method = NULL; 39210b5de56dSgjelinek char *snapshot = NULL; 3922865e09a4Sgjelinek 3923865e09a4Sgjelinek if (zonecfg_in_alt_root()) { 3924865e09a4Sgjelinek zerror(gettext("cannot clone zone in alternate root")); 3925865e09a4Sgjelinek return (Z_ERR); 3926865e09a4Sgjelinek } 3927865e09a4Sgjelinek 3928865e09a4Sgjelinek optind = 0; 39290b5de56dSgjelinek if ((arg = getopt(argc, argv, "?m:s:")) != EOF) { 3930865e09a4Sgjelinek switch (arg) { 3931865e09a4Sgjelinek case '?': 3932865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3933865e09a4Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 3934865e09a4Sgjelinek case 'm': 3935865e09a4Sgjelinek method = optarg; 3936865e09a4Sgjelinek break; 39370b5de56dSgjelinek case 's': 39380b5de56dSgjelinek snapshot = optarg; 39390b5de56dSgjelinek break; 3940865e09a4Sgjelinek default: 3941865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3942865e09a4Sgjelinek return (Z_USAGE); 3943865e09a4Sgjelinek } 3944865e09a4Sgjelinek } 39450b5de56dSgjelinek if (argc != (optind + 1) || 39460b5de56dSgjelinek (method != NULL && strcmp(method, "copy") != 0)) { 3947865e09a4Sgjelinek sub_usage(SHELP_CLONE, CMD_CLONE); 3948865e09a4Sgjelinek return (Z_USAGE); 3949865e09a4Sgjelinek } 3950865e09a4Sgjelinek source_zone = argv[optind]; 39519acbbeafSnn35248 if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE, B_FALSE) 39529acbbeafSnn35248 != Z_OK) 3953865e09a4Sgjelinek return (Z_ERR); 3954ce28b40eSzt129084 if (verify_details(CMD_CLONE, argv) != Z_OK) 3955865e09a4Sgjelinek return (Z_ERR); 3956865e09a4Sgjelinek 3957865e09a4Sgjelinek /* 3958865e09a4Sgjelinek * We also need to do some extra validation on the source zone. 3959865e09a4Sgjelinek */ 3960865e09a4Sgjelinek if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) { 3961865e09a4Sgjelinek zerror(gettext("%s operation is invalid for the global zone."), 3962865e09a4Sgjelinek cmd_to_str(CMD_CLONE)); 3963865e09a4Sgjelinek return (Z_ERR); 3964865e09a4Sgjelinek } 3965865e09a4Sgjelinek 3966865e09a4Sgjelinek if (strncmp(source_zone, "SUNW", 4) == 0) { 3967865e09a4Sgjelinek zerror(gettext("%s operation is invalid for zones starting " 3968865e09a4Sgjelinek "with SUNW."), cmd_to_str(CMD_CLONE)); 3969865e09a4Sgjelinek return (Z_ERR); 3970865e09a4Sgjelinek } 3971865e09a4Sgjelinek 3972865e09a4Sgjelinek zent = lookup_running_zone(source_zone); 3973865e09a4Sgjelinek if (zent != NULL) { 3974865e09a4Sgjelinek /* check whether the zone is ready or running */ 3975865e09a4Sgjelinek if ((err = zone_get_state(zent->zname, &zent->zstate_num)) 3976865e09a4Sgjelinek != Z_OK) { 3977865e09a4Sgjelinek errno = err; 3978865e09a4Sgjelinek zperror2(zent->zname, gettext("could not get state")); 3979865e09a4Sgjelinek /* can't tell, so hedge */ 3980865e09a4Sgjelinek zent->zstate_str = "ready/running"; 3981865e09a4Sgjelinek } else { 3982865e09a4Sgjelinek zent->zstate_str = zone_state_str(zent->zstate_num); 3983865e09a4Sgjelinek } 3984865e09a4Sgjelinek zerror(gettext("%s operation is invalid for %s zones."), 3985865e09a4Sgjelinek cmd_to_str(CMD_CLONE), zent->zstate_str); 3986865e09a4Sgjelinek return (Z_ERR); 3987865e09a4Sgjelinek } 3988865e09a4Sgjelinek 3989865e09a4Sgjelinek if ((err = zone_get_state(source_zone, &state)) != Z_OK) { 3990865e09a4Sgjelinek errno = err; 3991865e09a4Sgjelinek zperror2(source_zone, gettext("could not get state")); 3992865e09a4Sgjelinek return (Z_ERR); 3993865e09a4Sgjelinek } 3994865e09a4Sgjelinek if (state != ZONE_STATE_INSTALLED) { 3995865e09a4Sgjelinek (void) fprintf(stderr, 3996865e09a4Sgjelinek gettext("%s: zone %s is %s; %s is required.\n"), 3997865e09a4Sgjelinek execname, source_zone, zone_state_str(state), 3998865e09a4Sgjelinek zone_state_str(ZONE_STATE_INSTALLED)); 3999865e09a4Sgjelinek return (Z_ERR); 4000865e09a4Sgjelinek } 4001865e09a4Sgjelinek 4002865e09a4Sgjelinek /* 4003865e09a4Sgjelinek * The source zone checks out ok, continue with the clone. 4004865e09a4Sgjelinek */ 4005865e09a4Sgjelinek 4006865e09a4Sgjelinek if (validate_clone(source_zone, target_zone) != Z_OK) 4007865e09a4Sgjelinek return (Z_ERR); 4008865e09a4Sgjelinek 4009865e09a4Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 4010865e09a4Sgjelinek zerror(gettext("another %s may have an operation in progress."), 4011865e09a4Sgjelinek "zoneadm"); 4012865e09a4Sgjelinek return (Z_ERR); 4013865e09a4Sgjelinek } 4014865e09a4Sgjelinek 4015865e09a4Sgjelinek if ((err = zone_get_zonepath(source_zone, source_zonepath, 4016865e09a4Sgjelinek sizeof (source_zonepath))) != Z_OK) { 4017865e09a4Sgjelinek errno = err; 4018865e09a4Sgjelinek zperror2(source_zone, gettext("could not get zone path")); 4019865e09a4Sgjelinek goto done; 4020865e09a4Sgjelinek } 4021865e09a4Sgjelinek 4022865e09a4Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 4023865e09a4Sgjelinek != Z_OK) { 4024865e09a4Sgjelinek errno = err; 4025865e09a4Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 4026865e09a4Sgjelinek goto done; 4027865e09a4Sgjelinek } 4028865e09a4Sgjelinek 4029865e09a4Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE)) 4030865e09a4Sgjelinek != Z_OK) { 4031865e09a4Sgjelinek errno = err; 4032865e09a4Sgjelinek zperror2(target_zone, gettext("could not set state")); 4033865e09a4Sgjelinek goto done; 4034865e09a4Sgjelinek } 4035865e09a4Sgjelinek 40360b5de56dSgjelinek if (snapshot != NULL) { 40370b5de56dSgjelinek err = clone_snapshot_zfs(snapshot, zonepath); 40380b5de56dSgjelinek } else { 40390b5de56dSgjelinek /* 40400b5de56dSgjelinek * We always copy the clone unless the source is ZFS and a 40410b5de56dSgjelinek * ZFS clone worked. We fallback to copying if the ZFS clone 40420b5de56dSgjelinek * fails for some reason. 40430b5de56dSgjelinek */ 40440b5de56dSgjelinek err = Z_ERR; 40450b5de56dSgjelinek if (method == NULL && is_zonepath_zfs(source_zonepath)) 40460b5de56dSgjelinek err = clone_zfs(source_zone, source_zonepath, zonepath); 4047865e09a4Sgjelinek 40485cd08338Sgjelinek if (err != Z_OK) 40490b5de56dSgjelinek err = clone_copy(source_zonepath, zonepath); 40500b5de56dSgjelinek } 4051865e09a4Sgjelinek 40529acbbeafSnn35248 /* 40539acbbeafSnn35248 * Trusted Extensions requires that cloned zones use the same sysid 40549acbbeafSnn35248 * configuration, so it is not appropriate to perform any 40559acbbeafSnn35248 * post-clone reconfiguration. 40569acbbeafSnn35248 */ 40579acbbeafSnn35248 if ((err == Z_OK) && !is_system_labeled()) 40589acbbeafSnn35248 err = zone_postclone(zonepath); 4059865e09a4Sgjelinek 4060865e09a4Sgjelinek done: 40619acbbeafSnn35248 /* 40629acbbeafSnn35248 * If everything went well, we mark the zone as installed. 40639acbbeafSnn35248 */ 40649acbbeafSnn35248 if (err == Z_OK) { 40659acbbeafSnn35248 err = zone_set_state(target_zone, ZONE_STATE_INSTALLED); 40669acbbeafSnn35248 if (err != Z_OK) { 40679acbbeafSnn35248 errno = err; 40689acbbeafSnn35248 zperror2(target_zone, gettext("could not set state")); 40699acbbeafSnn35248 } 40709acbbeafSnn35248 } 4071865e09a4Sgjelinek release_lock_file(lockfd); 4072865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 4073865e09a4Sgjelinek } 4074865e09a4Sgjelinek 407507b574eeSgjelinek /* 40760b5de56dSgjelinek * Used when removing a zonepath after uninstalling or cleaning up after 40770b5de56dSgjelinek * the move subcommand. This handles a zonepath that has non-standard 40780b5de56dSgjelinek * contents so that we will only cleanup the stuff we know about and leave 40790b5de56dSgjelinek * any user data alone. 408007b574eeSgjelinek * 40810b5de56dSgjelinek * If the "all" parameter is true then we should remove the whole zonepath 40820b5de56dSgjelinek * even if it has non-standard files/directories in it. This can be used when 40830b5de56dSgjelinek * we need to cleanup after moving the zonepath across file systems. 40840b5de56dSgjelinek * 40850b5de56dSgjelinek * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND 40860b5de56dSgjelinek * and not the shell. 408707b574eeSgjelinek */ 408807b574eeSgjelinek static int 40890b5de56dSgjelinek cleanup_zonepath(char *zonepath, boolean_t all) 409007b574eeSgjelinek { 409107b574eeSgjelinek int status; 40920b5de56dSgjelinek int i; 40930b5de56dSgjelinek boolean_t non_std = B_FALSE; 40940b5de56dSgjelinek struct dirent *dp; 40950b5de56dSgjelinek DIR *dirp; 4096d9e728a2Sgjelinek /* 4097d9e728a2Sgjelinek * The SUNWattached.xml file is expected since it might 4098d9e728a2Sgjelinek * exist if the zone was force-attached after a 4099d9e728a2Sgjelinek * migration. 4100d9e728a2Sgjelinek */ 4101d9e728a2Sgjelinek char *std_entries[] = {"dev", "lu", "root", 4102d9e728a2Sgjelinek "SUNWattached.xml", NULL}; 41030b5de56dSgjelinek /* (MAXPATHLEN * 3) is for the 3 std_entries dirs */ 41040b5de56dSgjelinek char cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64]; 410507b574eeSgjelinek 410607b574eeSgjelinek /* 41070b5de56dSgjelinek * We shouldn't need these checks but lets be paranoid since we 41080b5de56dSgjelinek * could blow away the whole system here if we got the wrong zonepath. 410907b574eeSgjelinek */ 41100b5de56dSgjelinek if (*zonepath == NULL || strcmp(zonepath, "/") == 0) { 41110b5de56dSgjelinek (void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath); 41120b5de56dSgjelinek return (Z_INVAL); 41130b5de56dSgjelinek } 41140b5de56dSgjelinek 411507b574eeSgjelinek /* 41160b5de56dSgjelinek * If the dirpath is already gone (maybe it was manually removed) then 41170b5de56dSgjelinek * we just return Z_OK so that the cleanup is successful. 411807b574eeSgjelinek */ 41190b5de56dSgjelinek if ((dirp = opendir(zonepath)) == NULL) 41200b5de56dSgjelinek return (Z_OK); 41210b5de56dSgjelinek 41220b5de56dSgjelinek /* 41230b5de56dSgjelinek * Look through the zonepath directory to see if there are any 41240b5de56dSgjelinek * non-standard files/dirs. Also skip .zfs since that might be 41250b5de56dSgjelinek * there but we'll handle ZFS file systems as a special case. 41260b5de56dSgjelinek */ 41270b5de56dSgjelinek while ((dp = readdir(dirp)) != NULL) { 41280b5de56dSgjelinek if (strcmp(dp->d_name, ".") == 0 || 41290b5de56dSgjelinek strcmp(dp->d_name, "..") == 0 || 41300b5de56dSgjelinek strcmp(dp->d_name, ".zfs") == 0) 41310b5de56dSgjelinek continue; 41320b5de56dSgjelinek 41330b5de56dSgjelinek for (i = 0; std_entries[i] != NULL; i++) 41340b5de56dSgjelinek if (strcmp(dp->d_name, std_entries[i]) == 0) 41350b5de56dSgjelinek break; 41360b5de56dSgjelinek 41370b5de56dSgjelinek if (std_entries[i] == NULL) 41380b5de56dSgjelinek non_std = B_TRUE; 41390b5de56dSgjelinek } 41400b5de56dSgjelinek (void) closedir(dirp); 41410b5de56dSgjelinek 41420b5de56dSgjelinek if (!all && non_std) { 41430b5de56dSgjelinek /* 41440b5de56dSgjelinek * There are extra, non-standard directories/files in the 41450b5de56dSgjelinek * zonepath so we don't want to remove the zonepath. We 41460b5de56dSgjelinek * just want to remove the standard directories and leave 41470b5de56dSgjelinek * the user data alone. 41480b5de56dSgjelinek */ 41490b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND); 41500b5de56dSgjelinek 41510b5de56dSgjelinek for (i = 0; std_entries[i] != NULL; i++) { 41520b5de56dSgjelinek char tmpbuf[MAXPATHLEN]; 41530b5de56dSgjelinek 41540b5de56dSgjelinek if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s", 41550b5de56dSgjelinek zonepath, std_entries[i]) >= sizeof (tmpbuf) || 41560b5de56dSgjelinek strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >= 41570b5de56dSgjelinek sizeof (cmdbuf)) { 41580b5de56dSgjelinek (void) fprintf(stderr, 41590b5de56dSgjelinek gettext("path is too long\n")); 41600b5de56dSgjelinek return (Z_INVAL); 41610b5de56dSgjelinek } 416207b574eeSgjelinek } 416307b574eeSgjelinek 416407b574eeSgjelinek status = do_subproc(cmdbuf); 416507b574eeSgjelinek 41660b5de56dSgjelinek (void) fprintf(stderr, gettext("WARNING: Unable to completely " 41670b5de56dSgjelinek "remove %s\nbecause it contains additional user data. " 41680b5de56dSgjelinek "Only the standard directory\nentries have been " 41690b5de56dSgjelinek "removed.\n"), 41700b5de56dSgjelinek zonepath); 417107b574eeSgjelinek 41729acbbeafSnn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == 41739acbbeafSnn35248 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR); 41740b5de56dSgjelinek } 41750b5de56dSgjelinek 41760b5de56dSgjelinek /* 41770b5de56dSgjelinek * There is nothing unexpected in the zonepath, try to get rid of the 41780b5de56dSgjelinek * whole zonepath directory. 41790b5de56dSgjelinek * 41800b5de56dSgjelinek * If the zonepath is its own zfs file system, try to destroy the 41810b5de56dSgjelinek * file system. If that fails for some reason (e.g. it has clones) 41820b5de56dSgjelinek * then we'll just remove the contents of the zonepath. 41830b5de56dSgjelinek */ 41840b5de56dSgjelinek if (is_zonepath_zfs(zonepath)) { 41850b5de56dSgjelinek if (destroy_zfs(zonepath) == Z_OK) 41860b5de56dSgjelinek return (Z_OK); 41870b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND 41880b5de56dSgjelinek " %s/*", zonepath); 41890b5de56dSgjelinek status = do_subproc(cmdbuf); 41909acbbeafSnn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == 41919acbbeafSnn35248 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR); 41920b5de56dSgjelinek } 41930b5de56dSgjelinek 41940b5de56dSgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s", 41950b5de56dSgjelinek zonepath); 41960b5de56dSgjelinek status = do_subproc(cmdbuf); 41979acbbeafSnn35248 41989acbbeafSnn35248 return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK) 41999acbbeafSnn35248 ? Z_OK : Z_ERR); 420007b574eeSgjelinek } 420107b574eeSgjelinek 4202865e09a4Sgjelinek static int 4203865e09a4Sgjelinek move_func(int argc, char *argv[]) 4204865e09a4Sgjelinek { 4205865e09a4Sgjelinek char *new_zonepath = NULL; 4206865e09a4Sgjelinek int lockfd; 4207865e09a4Sgjelinek int err, arg; 4208865e09a4Sgjelinek char zonepath[MAXPATHLEN]; 4209865e09a4Sgjelinek zone_dochandle_t handle; 4210865e09a4Sgjelinek boolean_t fast; 42110b5de56dSgjelinek boolean_t is_zfs = B_FALSE; 42120b5de56dSgjelinek struct dirent *dp; 42130b5de56dSgjelinek DIR *dirp; 42140b5de56dSgjelinek boolean_t empty = B_TRUE; 4215865e09a4Sgjelinek boolean_t revert; 4216865e09a4Sgjelinek struct stat zonepath_buf; 4217865e09a4Sgjelinek struct stat new_zonepath_buf; 4218865e09a4Sgjelinek 4219865e09a4Sgjelinek if (zonecfg_in_alt_root()) { 4220865e09a4Sgjelinek zerror(gettext("cannot move zone in alternate root")); 4221865e09a4Sgjelinek return (Z_ERR); 4222865e09a4Sgjelinek } 4223865e09a4Sgjelinek 4224865e09a4Sgjelinek optind = 0; 4225865e09a4Sgjelinek if ((arg = getopt(argc, argv, "?")) != EOF) { 4226865e09a4Sgjelinek switch (arg) { 4227865e09a4Sgjelinek case '?': 4228865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 4229865e09a4Sgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 4230865e09a4Sgjelinek default: 4231865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 4232865e09a4Sgjelinek return (Z_USAGE); 4233865e09a4Sgjelinek } 4234865e09a4Sgjelinek } 4235865e09a4Sgjelinek if (argc != (optind + 1)) { 4236865e09a4Sgjelinek sub_usage(SHELP_MOVE, CMD_MOVE); 4237865e09a4Sgjelinek return (Z_USAGE); 4238865e09a4Sgjelinek } 4239865e09a4Sgjelinek new_zonepath = argv[optind]; 42409acbbeafSnn35248 if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE) 42419acbbeafSnn35248 != Z_OK) 4242865e09a4Sgjelinek return (Z_ERR); 4243ce28b40eSzt129084 if (verify_details(CMD_MOVE, argv) != Z_OK) 4244865e09a4Sgjelinek return (Z_ERR); 4245865e09a4Sgjelinek 4246865e09a4Sgjelinek /* 4247865e09a4Sgjelinek * Check out the new zonepath. This has the side effect of creating 4248865e09a4Sgjelinek * a directory for the new zonepath. We depend on this later when we 42490b5de56dSgjelinek * stat to see if we are doing a cross file system move or not. 4250865e09a4Sgjelinek */ 4251865e09a4Sgjelinek if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK) 4252865e09a4Sgjelinek return (Z_ERR); 4253865e09a4Sgjelinek 4254865e09a4Sgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 4255865e09a4Sgjelinek != Z_OK) { 4256865e09a4Sgjelinek errno = err; 4257865e09a4Sgjelinek zperror2(target_zone, gettext("could not get zone path")); 4258865e09a4Sgjelinek return (Z_ERR); 4259865e09a4Sgjelinek } 4260865e09a4Sgjelinek 4261865e09a4Sgjelinek if (stat(zonepath, &zonepath_buf) == -1) { 4262865e09a4Sgjelinek zperror(gettext("could not stat zone path"), B_FALSE); 4263865e09a4Sgjelinek return (Z_ERR); 4264865e09a4Sgjelinek } 4265865e09a4Sgjelinek 4266865e09a4Sgjelinek if (stat(new_zonepath, &new_zonepath_buf) == -1) { 4267865e09a4Sgjelinek zperror(gettext("could not stat new zone path"), B_FALSE); 4268865e09a4Sgjelinek return (Z_ERR); 4269865e09a4Sgjelinek } 4270865e09a4Sgjelinek 42710b5de56dSgjelinek /* 42720b5de56dSgjelinek * Check if the destination directory is empty. 42730b5de56dSgjelinek */ 42740b5de56dSgjelinek if ((dirp = opendir(new_zonepath)) == NULL) { 42750b5de56dSgjelinek zperror(gettext("could not open new zone path"), B_FALSE); 42760b5de56dSgjelinek return (Z_ERR); 42770b5de56dSgjelinek } 42780b5de56dSgjelinek while ((dp = readdir(dirp)) != (struct dirent *)0) { 42790b5de56dSgjelinek if (strcmp(dp->d_name, ".") == 0 || 42800b5de56dSgjelinek strcmp(dp->d_name, "..") == 0) 42810b5de56dSgjelinek continue; 42820b5de56dSgjelinek empty = B_FALSE; 42830b5de56dSgjelinek break; 42840b5de56dSgjelinek } 42850b5de56dSgjelinek (void) closedir(dirp); 42860b5de56dSgjelinek 42870b5de56dSgjelinek /* Error if there is anything in the destination directory. */ 42880b5de56dSgjelinek if (!empty) { 42890b5de56dSgjelinek (void) fprintf(stderr, gettext("could not move zone to %s: " 42900b5de56dSgjelinek "directory not empty\n"), new_zonepath); 42910b5de56dSgjelinek return (Z_ERR); 42920b5de56dSgjelinek } 42930b5de56dSgjelinek 4294865e09a4Sgjelinek /* Don't move the zone if anything is still mounted there */ 4295865e09a4Sgjelinek if (zonecfg_find_mounts(zonepath, NULL, NULL)) { 42960b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 4297865e09a4Sgjelinek "subdirectories of %s.\n"), zonepath); 4298865e09a4Sgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 4299865e09a4Sgjelinek return (Z_ERR); 4300865e09a4Sgjelinek } 4301865e09a4Sgjelinek 4302865e09a4Sgjelinek /* 4303865e09a4Sgjelinek * Check if we are moving in the same file system and can do a fast 4304865e09a4Sgjelinek * move or if we are crossing file systems and have to copy the data. 4305865e09a4Sgjelinek */ 4306865e09a4Sgjelinek fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev); 4307865e09a4Sgjelinek 4308865e09a4Sgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 4309865e09a4Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 4310865e09a4Sgjelinek return (Z_ERR); 4311865e09a4Sgjelinek } 4312865e09a4Sgjelinek 4313865e09a4Sgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 4314865e09a4Sgjelinek errno = err; 4315865e09a4Sgjelinek zperror(cmd_to_str(CMD_MOVE), B_TRUE); 4316865e09a4Sgjelinek zonecfg_fini_handle(handle); 4317865e09a4Sgjelinek return (Z_ERR); 4318865e09a4Sgjelinek } 4319865e09a4Sgjelinek 4320865e09a4Sgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 4321865e09a4Sgjelinek zerror(gettext("another %s may have an operation in progress."), 4322865e09a4Sgjelinek "zoneadm"); 4323865e09a4Sgjelinek zonecfg_fini_handle(handle); 4324865e09a4Sgjelinek return (Z_ERR); 4325865e09a4Sgjelinek } 4326865e09a4Sgjelinek 4327865e09a4Sgjelinek /* 43280b5de56dSgjelinek * We're making some file system changes now so we have to clean up 43290b5de56dSgjelinek * the file system before we are done. This will either clean up the 4330865e09a4Sgjelinek * new zonepath if the zonecfg update failed or it will clean up the 4331865e09a4Sgjelinek * old zonepath if everything is ok. 4332865e09a4Sgjelinek */ 4333865e09a4Sgjelinek revert = B_TRUE; 4334865e09a4Sgjelinek 43350b5de56dSgjelinek if (is_zonepath_zfs(zonepath) && 43360b5de56dSgjelinek move_zfs(zonepath, new_zonepath) != Z_ERR) { 43370b5de56dSgjelinek is_zfs = B_TRUE; 43380b5de56dSgjelinek 43390b5de56dSgjelinek } else if (fast) { 4340865e09a4Sgjelinek /* same file system, use rename for a quick move */ 4341865e09a4Sgjelinek 4342865e09a4Sgjelinek /* 4343865e09a4Sgjelinek * Remove the new_zonepath directory that got created above 4344865e09a4Sgjelinek * during the validation. It gets in the way of the rename. 4345865e09a4Sgjelinek */ 4346865e09a4Sgjelinek if (rmdir(new_zonepath) != 0) { 4347865e09a4Sgjelinek zperror(gettext("could not rmdir new zone path"), 4348865e09a4Sgjelinek B_FALSE); 4349865e09a4Sgjelinek zonecfg_fini_handle(handle); 4350865e09a4Sgjelinek release_lock_file(lockfd); 4351865e09a4Sgjelinek return (Z_ERR); 4352865e09a4Sgjelinek } 4353865e09a4Sgjelinek 4354865e09a4Sgjelinek if (rename(zonepath, new_zonepath) != 0) { 4355865e09a4Sgjelinek /* 4356865e09a4Sgjelinek * If this fails we don't need to do all of the 4357865e09a4Sgjelinek * cleanup that happens for the rest of the code 4358865e09a4Sgjelinek * so just return from this error. 4359865e09a4Sgjelinek */ 4360865e09a4Sgjelinek zperror(gettext("could not move zone"), B_FALSE); 4361865e09a4Sgjelinek zonecfg_fini_handle(handle); 4362865e09a4Sgjelinek release_lock_file(lockfd); 4363865e09a4Sgjelinek return (Z_ERR); 4364865e09a4Sgjelinek } 4365865e09a4Sgjelinek 4366865e09a4Sgjelinek } else { 43670b5de56dSgjelinek /* 43680b5de56dSgjelinek * Attempt to create a ZFS fs for the new zonepath. As usual, 43690b5de56dSgjelinek * we don't care if this works or not since we always have the 43700b5de56dSgjelinek * default behavior of a simple directory for the zonepath. 43710b5de56dSgjelinek */ 43720b5de56dSgjelinek create_zfs_zonepath(new_zonepath); 43730b5de56dSgjelinek 4374865e09a4Sgjelinek (void) printf(gettext( 43750b5de56dSgjelinek "Moving across file systems; copying zonepath %s..."), 4376865e09a4Sgjelinek zonepath); 4377865e09a4Sgjelinek (void) fflush(stdout); 4378865e09a4Sgjelinek 4379865e09a4Sgjelinek err = copy_zone(zonepath, new_zonepath); 4380865e09a4Sgjelinek 4381865e09a4Sgjelinek (void) printf("\n"); 4382865e09a4Sgjelinek if (err != Z_OK) 4383865e09a4Sgjelinek goto done; 4384865e09a4Sgjelinek } 4385865e09a4Sgjelinek 4386865e09a4Sgjelinek if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) { 4387865e09a4Sgjelinek errno = err; 4388865e09a4Sgjelinek zperror(gettext("could not set new zonepath"), B_TRUE); 4389865e09a4Sgjelinek goto done; 4390865e09a4Sgjelinek } 4391865e09a4Sgjelinek 4392865e09a4Sgjelinek if ((err = zonecfg_save(handle)) != Z_OK) { 4393865e09a4Sgjelinek errno = err; 4394865e09a4Sgjelinek zperror(gettext("zonecfg save failed"), B_TRUE); 4395865e09a4Sgjelinek goto done; 4396865e09a4Sgjelinek } 4397865e09a4Sgjelinek 4398865e09a4Sgjelinek revert = B_FALSE; 4399865e09a4Sgjelinek 4400865e09a4Sgjelinek done: 4401865e09a4Sgjelinek zonecfg_fini_handle(handle); 4402865e09a4Sgjelinek release_lock_file(lockfd); 4403865e09a4Sgjelinek 4404865e09a4Sgjelinek /* 44050b5de56dSgjelinek * Clean up the file system based on how things went. We either 4406865e09a4Sgjelinek * clean up the new zonepath if the operation failed for some reason 4407865e09a4Sgjelinek * or we clean up the old zonepath if everything is ok. 4408865e09a4Sgjelinek */ 4409865e09a4Sgjelinek if (revert) { 4410865e09a4Sgjelinek /* The zonecfg update failed, cleanup the new zonepath. */ 44110b5de56dSgjelinek if (is_zfs) { 44120b5de56dSgjelinek if (move_zfs(new_zonepath, zonepath) == Z_ERR) { 44130b5de56dSgjelinek (void) fprintf(stderr, gettext("could not " 44140b5de56dSgjelinek "restore zonepath, the zfs mountpoint is " 44150b5de56dSgjelinek "set as:\n%s\n"), new_zonepath); 44160b5de56dSgjelinek /* 44170b5de56dSgjelinek * err is already != Z_OK since we're reverting 44180b5de56dSgjelinek */ 44190b5de56dSgjelinek } 44200b5de56dSgjelinek 44210b5de56dSgjelinek } else if (fast) { 4422865e09a4Sgjelinek if (rename(new_zonepath, zonepath) != 0) { 4423865e09a4Sgjelinek zperror(gettext("could not restore zonepath"), 4424865e09a4Sgjelinek B_FALSE); 4425865e09a4Sgjelinek /* 4426865e09a4Sgjelinek * err is already != Z_OK since we're reverting 4427865e09a4Sgjelinek */ 4428865e09a4Sgjelinek } 4429865e09a4Sgjelinek } else { 4430865e09a4Sgjelinek (void) printf(gettext("Cleaning up zonepath %s..."), 4431865e09a4Sgjelinek new_zonepath); 4432865e09a4Sgjelinek (void) fflush(stdout); 44330b5de56dSgjelinek err = cleanup_zonepath(new_zonepath, B_TRUE); 4434865e09a4Sgjelinek (void) printf("\n"); 4435865e09a4Sgjelinek 443607b574eeSgjelinek if (err != Z_OK) { 4437865e09a4Sgjelinek errno = err; 4438865e09a4Sgjelinek zperror(gettext("could not remove new " 4439865e09a4Sgjelinek "zonepath"), B_TRUE); 4440865e09a4Sgjelinek } else { 4441865e09a4Sgjelinek /* 4442865e09a4Sgjelinek * Because we're reverting we know the mainline 4443865e09a4Sgjelinek * code failed but we just reused the err 4444865e09a4Sgjelinek * variable so we reset it back to Z_ERR. 4445865e09a4Sgjelinek */ 4446865e09a4Sgjelinek err = Z_ERR; 4447865e09a4Sgjelinek } 4448865e09a4Sgjelinek } 4449865e09a4Sgjelinek 4450865e09a4Sgjelinek } else { 4451865e09a4Sgjelinek /* The move was successful, cleanup the old zonepath. */ 44520b5de56dSgjelinek if (!is_zfs && !fast) { 4453865e09a4Sgjelinek (void) printf( 4454865e09a4Sgjelinek gettext("Cleaning up zonepath %s..."), zonepath); 4455865e09a4Sgjelinek (void) fflush(stdout); 44560b5de56dSgjelinek err = cleanup_zonepath(zonepath, B_TRUE); 4457865e09a4Sgjelinek (void) printf("\n"); 4458865e09a4Sgjelinek 445907b574eeSgjelinek if (err != Z_OK) { 4460865e09a4Sgjelinek errno = err; 4461865e09a4Sgjelinek zperror(gettext("could not remove zonepath"), 4462865e09a4Sgjelinek B_TRUE); 4463865e09a4Sgjelinek } 4464865e09a4Sgjelinek } 4465865e09a4Sgjelinek } 4466865e09a4Sgjelinek 4467865e09a4Sgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 4468865e09a4Sgjelinek } 4469865e09a4Sgjelinek 4470ee519a1fSgjelinek static int 4471ee519a1fSgjelinek detach_func(int argc, char *argv[]) 4472ee519a1fSgjelinek { 4473ee519a1fSgjelinek int lockfd; 4474ee519a1fSgjelinek int err, arg; 4475ee519a1fSgjelinek char zonepath[MAXPATHLEN]; 447637774979Sgjelinek char cmdbuf[MAXPATHLEN]; 4477ee519a1fSgjelinek zone_dochandle_t handle; 44788cd327d5Sgjelinek boolean_t execute = B_TRUE; 447937774979Sgjelinek brand_handle_t bh = NULL; 4480ee519a1fSgjelinek 4481ee519a1fSgjelinek if (zonecfg_in_alt_root()) { 4482ee519a1fSgjelinek zerror(gettext("cannot detach zone in alternate root")); 4483ee519a1fSgjelinek return (Z_ERR); 4484ee519a1fSgjelinek } 4485ee519a1fSgjelinek 4486ee519a1fSgjelinek optind = 0; 44878cd327d5Sgjelinek if ((arg = getopt(argc, argv, "?n")) != EOF) { 4488ee519a1fSgjelinek switch (arg) { 4489ee519a1fSgjelinek case '?': 4490ee519a1fSgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 4491ee519a1fSgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 44928cd327d5Sgjelinek case 'n': 44938cd327d5Sgjelinek execute = B_FALSE; 44948cd327d5Sgjelinek break; 4495ee519a1fSgjelinek default: 4496ee519a1fSgjelinek sub_usage(SHELP_DETACH, CMD_DETACH); 4497ee519a1fSgjelinek return (Z_USAGE); 4498ee519a1fSgjelinek } 4499ee519a1fSgjelinek } 45009acbbeafSnn35248 45018cd327d5Sgjelinek if (execute) { 45029acbbeafSnn35248 if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE, 45039acbbeafSnn35248 B_FALSE) != Z_OK) 4504ee519a1fSgjelinek return (Z_ERR); 4505ce28b40eSzt129084 if (verify_details(CMD_DETACH, argv) != Z_OK) 4506ee519a1fSgjelinek return (Z_ERR); 45078cd327d5Sgjelinek } else { 45088cd327d5Sgjelinek /* 45098cd327d5Sgjelinek * We want a dry-run to work for a non-privileged user so we 45108cd327d5Sgjelinek * only do minimal validation. 45118cd327d5Sgjelinek */ 45128cd327d5Sgjelinek if (target_zone == NULL) { 45138cd327d5Sgjelinek zerror(gettext("no zone specified")); 45148cd327d5Sgjelinek return (Z_ERR); 45158cd327d5Sgjelinek } 45168cd327d5Sgjelinek 45178cd327d5Sgjelinek if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) { 45188cd327d5Sgjelinek zerror(gettext("%s operation is invalid for the " 45198cd327d5Sgjelinek "global zone."), cmd_to_str(CMD_DETACH)); 45208cd327d5Sgjelinek return (Z_ERR); 45218cd327d5Sgjelinek } 45228cd327d5Sgjelinek } 4523ee519a1fSgjelinek 4524ee519a1fSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 4525ee519a1fSgjelinek != Z_OK) { 4526ee519a1fSgjelinek errno = err; 4527ee519a1fSgjelinek zperror2(target_zone, gettext("could not get zone path")); 4528ee519a1fSgjelinek return (Z_ERR); 4529ee519a1fSgjelinek } 4530ee519a1fSgjelinek 4531ee519a1fSgjelinek /* Don't detach the zone if anything is still mounted there */ 45328cd327d5Sgjelinek if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) { 45330b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 4534ee519a1fSgjelinek "subdirectories of %s.\n"), zonepath); 4535ee519a1fSgjelinek (void) zonecfg_find_mounts(zonepath, zfm_print, NULL); 4536ee519a1fSgjelinek return (Z_ERR); 4537ee519a1fSgjelinek } 4538ee519a1fSgjelinek 4539ee519a1fSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 4540ee519a1fSgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 4541ee519a1fSgjelinek return (Z_ERR); 4542ee519a1fSgjelinek } 4543ee519a1fSgjelinek 4544ee519a1fSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 4545ee519a1fSgjelinek errno = err; 4546ee519a1fSgjelinek zperror(cmd_to_str(CMD_DETACH), B_TRUE); 4547ee519a1fSgjelinek zonecfg_fini_handle(handle); 4548ee519a1fSgjelinek return (Z_ERR); 4549ee519a1fSgjelinek } 4550ee519a1fSgjelinek 455137774979Sgjelinek /* Fetch the predetach hook from the brand configuration. */ 455237774979Sgjelinek if ((bh = brand_open(target_brand)) == NULL) { 455337774979Sgjelinek zerror(gettext("missing or invalid brand")); 455437774979Sgjelinek return (Z_ERR); 455537774979Sgjelinek } 455637774979Sgjelinek 455737774979Sgjelinek (void) strcpy(cmdbuf, EXEC_PREFIX); 455837774979Sgjelinek if (brand_get_predetach(bh, target_zone, zonepath, cmdbuf + EXEC_LEN, 455937774979Sgjelinek sizeof (cmdbuf) - EXEC_LEN, 0, NULL) != 0) { 456037774979Sgjelinek zerror("invalid brand configuration: missing predetach " 456137774979Sgjelinek "resource"); 456237774979Sgjelinek brand_close(bh); 456337774979Sgjelinek return (Z_ERR); 456437774979Sgjelinek } 456537774979Sgjelinek brand_close(bh); 456637774979Sgjelinek 456737774979Sgjelinek /* If we have a brand predetach hook, run it. */ 456837774979Sgjelinek if (strlen(cmdbuf) > EXEC_LEN) { 456937774979Sgjelinek int status; 457037774979Sgjelinek 457137774979Sgjelinek /* If this is a dry-run, pass that flag to the hook. */ 457237774979Sgjelinek if (!execute && addopt(cmdbuf, 0, "-n", sizeof (cmdbuf)) 457337774979Sgjelinek != Z_OK) { 457437774979Sgjelinek zerror("Predetach command line too long"); 457537774979Sgjelinek return (Z_ERR); 457637774979Sgjelinek } 457737774979Sgjelinek 457837774979Sgjelinek status = do_subproc(cmdbuf); 457937774979Sgjelinek if (subproc_status(gettext("brand-specific predetach"), 458037774979Sgjelinek status, B_FALSE) != ZONE_SUBPROC_OK) { 458137774979Sgjelinek return (Z_ERR); 458237774979Sgjelinek } 458337774979Sgjelinek } 458437774979Sgjelinek 45858cd327d5Sgjelinek if (execute && grab_lock_file(target_zone, &lockfd) != Z_OK) { 4586ee519a1fSgjelinek zerror(gettext("another %s may have an operation in progress."), 4587ee519a1fSgjelinek "zoneadm"); 4588ee519a1fSgjelinek zonecfg_fini_handle(handle); 4589ee519a1fSgjelinek return (Z_ERR); 4590ee519a1fSgjelinek } 4591ee519a1fSgjelinek 4592ee519a1fSgjelinek if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) { 4593ee519a1fSgjelinek errno = err; 4594ee519a1fSgjelinek zperror(gettext("getting the detach information failed"), 4595ee519a1fSgjelinek B_TRUE); 4596ee519a1fSgjelinek goto done; 4597ee519a1fSgjelinek } 4598ee519a1fSgjelinek 45998cd327d5Sgjelinek if ((err = zonecfg_detach_save(handle, (execute ? 0 : ZONE_DRY_RUN))) 46008cd327d5Sgjelinek != Z_OK) { 4601ee519a1fSgjelinek errno = err; 4602ee519a1fSgjelinek zperror(gettext("saving the detach manifest failed"), B_TRUE); 4603ee519a1fSgjelinek goto done; 4604ee519a1fSgjelinek } 4605ee519a1fSgjelinek 46068cd327d5Sgjelinek /* 46078cd327d5Sgjelinek * Set the zone state back to configured unless we are running with the 46088cd327d5Sgjelinek * no-execute option. 46098cd327d5Sgjelinek */ 46108cd327d5Sgjelinek if (execute && (err = zone_set_state(target_zone, 46118cd327d5Sgjelinek ZONE_STATE_CONFIGURED)) != Z_OK) { 4612ee519a1fSgjelinek errno = err; 4613ee519a1fSgjelinek zperror(gettext("could not reset state"), B_TRUE); 4614ee519a1fSgjelinek } 4615ee519a1fSgjelinek 4616ee519a1fSgjelinek done: 4617ee519a1fSgjelinek zonecfg_fini_handle(handle); 46188cd327d5Sgjelinek if (execute) 4619ee519a1fSgjelinek release_lock_file(lockfd); 4620ee519a1fSgjelinek 4621ee519a1fSgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 4622ee519a1fSgjelinek } 4623ee519a1fSgjelinek 4624ee519a1fSgjelinek /* 4625ee519a1fSgjelinek * During attach we go through and fix up the /dev entries for the zone 4626ee519a1fSgjelinek * we are attaching. In order to regenerate /dev with the correct devices, 4627ee519a1fSgjelinek * the old /dev will be removed, the zone readied (which generates a new 4628ee519a1fSgjelinek * /dev) then halted, then we use the info from the manifest to update 4629ee519a1fSgjelinek * the modes, owners, etc. on the new /dev. 4630ee519a1fSgjelinek */ 4631ee519a1fSgjelinek static int 4632ee519a1fSgjelinek dev_fix(zone_dochandle_t handle) 4633ee519a1fSgjelinek { 4634ee519a1fSgjelinek int res; 4635ee519a1fSgjelinek int err; 4636ee519a1fSgjelinek int status; 4637ee519a1fSgjelinek struct zone_devpermtab devtab; 4638ee519a1fSgjelinek zone_cmd_arg_t zarg; 4639ee519a1fSgjelinek char devpath[MAXPATHLEN]; 4640ee519a1fSgjelinek /* 6: "exec " and " " */ 4641ee519a1fSgjelinek char cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6]; 4642ee519a1fSgjelinek 4643ee519a1fSgjelinek if ((res = zonecfg_get_zonepath(handle, devpath, sizeof (devpath))) 4644ee519a1fSgjelinek != Z_OK) 4645ee519a1fSgjelinek return (res); 4646ee519a1fSgjelinek 4647ee519a1fSgjelinek if (strlcat(devpath, "/dev", sizeof (devpath)) >= sizeof (devpath)) 4648ee519a1fSgjelinek return (Z_TOO_BIG); 4649ee519a1fSgjelinek 4650ee519a1fSgjelinek /* 4651ee519a1fSgjelinek * "exec" the command so that the returned status is that of 4652ee519a1fSgjelinek * RMCOMMAND and not the shell. 4653ee519a1fSgjelinek */ 46549acbbeafSnn35248 (void) snprintf(cmdbuf, sizeof (cmdbuf), EXEC_PREFIX RMCOMMAND " %s", 4655ee519a1fSgjelinek devpath); 4656ee519a1fSgjelinek status = do_subproc(cmdbuf); 46579acbbeafSnn35248 if ((err = subproc_status(RMCOMMAND, status, B_TRUE)) != 46589acbbeafSnn35248 ZONE_SUBPROC_OK) { 4659ee519a1fSgjelinek (void) fprintf(stderr, 4660ee519a1fSgjelinek gettext("could not remove existing /dev\n")); 4661ee519a1fSgjelinek return (Z_ERR); 4662ee519a1fSgjelinek } 4663ee519a1fSgjelinek 4664ee519a1fSgjelinek /* In order to ready the zone, it must be in the installed state */ 4665ee519a1fSgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 4666ee519a1fSgjelinek errno = err; 4667ee519a1fSgjelinek zperror(gettext("could not reset state"), B_TRUE); 4668ee519a1fSgjelinek return (Z_ERR); 4669ee519a1fSgjelinek } 4670ee519a1fSgjelinek 4671ee519a1fSgjelinek /* We have to ready the zone to regen the dev tree */ 4672ee519a1fSgjelinek zarg.cmd = Z_READY; 4673ee519a1fSgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 4674ee519a1fSgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 46750209230bSgjelinek /* attempt to restore zone to configured state */ 46760209230bSgjelinek (void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 4677ee519a1fSgjelinek return (Z_ERR); 4678ee519a1fSgjelinek } 4679ee519a1fSgjelinek 4680ee519a1fSgjelinek zarg.cmd = Z_HALT; 4681ee519a1fSgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 4682ee519a1fSgjelinek zerror(gettext("call to %s failed"), "zoneadmd"); 46830209230bSgjelinek /* attempt to restore zone to configured state */ 46840209230bSgjelinek (void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 4685ee519a1fSgjelinek return (Z_ERR); 4686ee519a1fSgjelinek } 4687ee519a1fSgjelinek 46880209230bSgjelinek /* attempt to restore zone to configured state */ 46890209230bSgjelinek (void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 46900209230bSgjelinek 4691ee519a1fSgjelinek if (zonecfg_setdevperment(handle) != Z_OK) { 4692ee519a1fSgjelinek (void) fprintf(stderr, 4693ee519a1fSgjelinek gettext("unable to enumerate device entries\n")); 4694ee519a1fSgjelinek return (Z_ERR); 4695ee519a1fSgjelinek } 4696ee519a1fSgjelinek 4697ee519a1fSgjelinek while (zonecfg_getdevperment(handle, &devtab) == Z_OK) { 4698ee519a1fSgjelinek int err; 4699ee519a1fSgjelinek 4700ee519a1fSgjelinek if ((err = zonecfg_devperms_apply(handle, 4701ee519a1fSgjelinek devtab.zone_devperm_name, devtab.zone_devperm_uid, 4702ee519a1fSgjelinek devtab.zone_devperm_gid, devtab.zone_devperm_mode, 4703ee519a1fSgjelinek devtab.zone_devperm_acl)) != Z_OK && err != Z_INVAL) 4704ee519a1fSgjelinek (void) fprintf(stderr, gettext("error updating device " 4705ee519a1fSgjelinek "%s: %s\n"), devtab.zone_devperm_name, 4706ee519a1fSgjelinek zonecfg_strerror(err)); 4707ee519a1fSgjelinek 4708ee519a1fSgjelinek free(devtab.zone_devperm_acl); 4709ee519a1fSgjelinek } 4710ee519a1fSgjelinek 4711ee519a1fSgjelinek (void) zonecfg_enddevperment(handle); 4712ee519a1fSgjelinek 4713ee519a1fSgjelinek return (Z_OK); 4714ee519a1fSgjelinek } 4715ee519a1fSgjelinek 47168cd327d5Sgjelinek /* 47178cd327d5Sgjelinek * Validate attaching a zone but don't actually do the work. The zone 47188cd327d5Sgjelinek * does not have to exist, so there is some complexity getting a new zone 47198cd327d5Sgjelinek * configuration set up so that we can perform the validation. This is 47208cd327d5Sgjelinek * handled within zonecfg_attach_manifest() which returns two handles; one 47218cd327d5Sgjelinek * for the the full configuration to validate (rem_handle) and the other 47228cd327d5Sgjelinek * (local_handle) containing only the zone configuration derived from the 47238cd327d5Sgjelinek * manifest. 47248cd327d5Sgjelinek */ 47258cd327d5Sgjelinek static int 4726ce28b40eSzt129084 dryrun_attach(char *manifest_path, char *argv[]) 47278cd327d5Sgjelinek { 47288cd327d5Sgjelinek int fd; 47298cd327d5Sgjelinek int err; 47308cd327d5Sgjelinek int res; 47318cd327d5Sgjelinek zone_dochandle_t local_handle; 47328cd327d5Sgjelinek zone_dochandle_t rem_handle = NULL; 47338cd327d5Sgjelinek 47348cd327d5Sgjelinek if (strcmp(manifest_path, "-") == 0) { 47358cd327d5Sgjelinek fd = 0; 47368cd327d5Sgjelinek } else if ((fd = open(manifest_path, O_RDONLY)) < 0) { 47378cd327d5Sgjelinek zperror(gettext("could not open manifest path"), B_FALSE); 47388cd327d5Sgjelinek return (Z_ERR); 47398cd327d5Sgjelinek } 47408cd327d5Sgjelinek 47418cd327d5Sgjelinek if ((local_handle = zonecfg_init_handle()) == NULL) { 47428cd327d5Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 47438cd327d5Sgjelinek res = Z_ERR; 47448cd327d5Sgjelinek goto done; 47458cd327d5Sgjelinek } 47468cd327d5Sgjelinek 47478cd327d5Sgjelinek if ((rem_handle = zonecfg_init_handle()) == NULL) { 47488cd327d5Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 47498cd327d5Sgjelinek res = Z_ERR; 47508cd327d5Sgjelinek goto done; 47518cd327d5Sgjelinek } 47528cd327d5Sgjelinek 47538cd327d5Sgjelinek if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle)) 47548cd327d5Sgjelinek != Z_OK) { 47558cd327d5Sgjelinek res = Z_ERR; 4756d9e728a2Sgjelinek 4757d9e728a2Sgjelinek if (err == Z_INVALID_DOCUMENT) { 4758d9e728a2Sgjelinek struct stat st; 4759d9e728a2Sgjelinek char buf[6]; 4760d9e728a2Sgjelinek 4761d9e728a2Sgjelinek if (strcmp(manifest_path, "-") == 0) { 4762d9e728a2Sgjelinek zerror(gettext("Input is not a valid XML " 4763d9e728a2Sgjelinek "file")); 4764d9e728a2Sgjelinek goto done; 4765d9e728a2Sgjelinek } 4766d9e728a2Sgjelinek 4767d9e728a2Sgjelinek if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) { 4768d9e728a2Sgjelinek zerror(gettext("%s is not an XML file"), 4769d9e728a2Sgjelinek manifest_path); 4770d9e728a2Sgjelinek goto done; 4771d9e728a2Sgjelinek } 4772d9e728a2Sgjelinek 4773d9e728a2Sgjelinek bzero(buf, sizeof (buf)); 4774d9e728a2Sgjelinek (void) lseek(fd, 0L, SEEK_SET); 4775d9e728a2Sgjelinek if (read(fd, buf, sizeof (buf) - 1) < 0 || 4776d9e728a2Sgjelinek strncmp(buf, "<?xml", 5) != 0) 4777d9e728a2Sgjelinek zerror(gettext("%s is not an XML file"), 4778d9e728a2Sgjelinek manifest_path); 4779d9e728a2Sgjelinek else 4780d9e728a2Sgjelinek zerror(gettext("Cannot attach to an earlier " 4781d9e728a2Sgjelinek "release of the operating system")); 4782d9e728a2Sgjelinek } else { 4783d9e728a2Sgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 4784d9e728a2Sgjelinek } 47858cd327d5Sgjelinek goto done; 47868cd327d5Sgjelinek } 47878cd327d5Sgjelinek 4788e2482d1aSgjelinek /* 4789e2482d1aSgjelinek * Retrieve remote handle brand type and determine whether it is 4790e2482d1aSgjelinek * native or not. 4791e2482d1aSgjelinek */ 4792e2482d1aSgjelinek if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand)) 4793e2482d1aSgjelinek != Z_OK) { 4794e2482d1aSgjelinek zerror(gettext("missing or invalid brand")); 4795e2482d1aSgjelinek exit(Z_ERR); 4796e2482d1aSgjelinek } 4797e2482d1aSgjelinek is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0); 479884561e8cStd153743 is_cluster_zone = 479984561e8cStd153743 (strcmp(target_brand, CLUSTER_BRAND_NAME) == 0); 4800e2482d1aSgjelinek 4801ce28b40eSzt129084 res = verify_handle(CMD_ATTACH, local_handle, argv); 48028cd327d5Sgjelinek 48038cd327d5Sgjelinek /* Get the detach information for the locally defined zone. */ 48048cd327d5Sgjelinek if ((err = zonecfg_get_detach_info(local_handle, B_FALSE)) != Z_OK) { 48058cd327d5Sgjelinek errno = err; 48068cd327d5Sgjelinek zperror(gettext("getting the attach information failed"), 48078cd327d5Sgjelinek B_TRUE); 48088cd327d5Sgjelinek res = Z_ERR; 48098cd327d5Sgjelinek } else { 48108cd327d5Sgjelinek /* sw_cmp prints error msgs as necessary */ 48118cd327d5Sgjelinek if (sw_cmp(local_handle, rem_handle, SW_CMP_NONE) != Z_OK) 48128cd327d5Sgjelinek res = Z_ERR; 48138cd327d5Sgjelinek } 48148cd327d5Sgjelinek 48158cd327d5Sgjelinek done: 48168cd327d5Sgjelinek if (strcmp(manifest_path, "-") != 0) 48178cd327d5Sgjelinek (void) close(fd); 48188cd327d5Sgjelinek 48198cd327d5Sgjelinek zonecfg_fini_handle(local_handle); 48208cd327d5Sgjelinek zonecfg_fini_handle(rem_handle); 48218cd327d5Sgjelinek 48228cd327d5Sgjelinek return ((res == Z_OK) ? Z_OK : Z_ERR); 48238cd327d5Sgjelinek } 48248cd327d5Sgjelinek 4825f1adb09eSgjelinek /* 4826f1adb09eSgjelinek * Attempt to generate the information we need to make the zone look like 4827f1adb09eSgjelinek * it was properly detached by using the pkg information contained within 4828f1adb09eSgjelinek * the zone itself. 4829f1adb09eSgjelinek * 4830f1adb09eSgjelinek * We will perform a dry-run detach within the zone to generate the xml file. 4831f1adb09eSgjelinek * To do this we need to be able to get a handle on the zone so we can see 4832f1adb09eSgjelinek * how it is configured. In order to get a handle, we need a copy of the 4833f1adb09eSgjelinek * zone configuration within the zone. Since the zone's configuration is 4834f1adb09eSgjelinek * not available within the zone itself, we need to temporarily copy it into 4835f1adb09eSgjelinek * the zone. 4836f1adb09eSgjelinek * 4837f1adb09eSgjelinek * The sequence of actions we are doing is this: 4838f1adb09eSgjelinek * [set zone state to installed] 4839f1adb09eSgjelinek * [mount zone] 4840f1adb09eSgjelinek * zlogin {zone} </etc/zones/{zone}.xml 'cat >/etc/zones/{zone}.xml' 4841f1adb09eSgjelinek * zlogin {zone} 'zoneadm -z {zone} detach -n' >{zonepath}/SUNWdetached.xml 4842f1adb09eSgjelinek * zlogin {zone} 'rm -f /etc/zones/{zone}.xml' 4843f1adb09eSgjelinek * [unmount zone] 4844f1adb09eSgjelinek * [set zone state to configured] 4845f1adb09eSgjelinek * 4846f1adb09eSgjelinek * The successful result of this function is that we will have a 4847f1adb09eSgjelinek * SUNWdetached.xml file in the zonepath and we can use that to attach the zone. 4848f1adb09eSgjelinek */ 4849f1adb09eSgjelinek static boolean_t 4850f1adb09eSgjelinek gen_detach_info(char *zonepath) 4851f1adb09eSgjelinek { 4852f1adb09eSgjelinek int status; 4853f1adb09eSgjelinek boolean_t mounted = B_FALSE; 4854f1adb09eSgjelinek boolean_t res = B_FALSE; 4855f1adb09eSgjelinek char cmdbuf[2 * MAXPATHLEN]; 4856f1adb09eSgjelinek 4857f1adb09eSgjelinek /* 4858f1adb09eSgjelinek * The zone has to be installed to mount and zlogin. Temporarily set 4859f1adb09eSgjelinek * the state to 'installed'. 4860f1adb09eSgjelinek */ 4861f1adb09eSgjelinek if (zone_set_state(target_zone, ZONE_STATE_INSTALLED) != Z_OK) 4862f1adb09eSgjelinek return (B_FALSE); 4863f1adb09eSgjelinek 4864f1adb09eSgjelinek /* Mount the zone so we can zlogin. */ 4865f1adb09eSgjelinek if (mount_func(0, NULL) != Z_OK) 4866f1adb09eSgjelinek goto cleanup; 4867f1adb09eSgjelinek mounted = B_TRUE; 4868f1adb09eSgjelinek 4869f1adb09eSgjelinek /* 4870f1adb09eSgjelinek * We need to copy the zones xml configuration file into the 4871f1adb09eSgjelinek * zone so we can get a handle for the zone while running inside 4872f1adb09eSgjelinek * the zone. 4873f1adb09eSgjelinek */ 4874f1adb09eSgjelinek if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/sbin/zlogin -S %s " 4875f1adb09eSgjelinek "</etc/zones/%s.xml '/usr/bin/cat >/etc/zones/%s.xml'", 4876f1adb09eSgjelinek target_zone, target_zone, target_zone) >= sizeof (cmdbuf)) 4877f1adb09eSgjelinek goto cleanup; 4878f1adb09eSgjelinek 4879f1adb09eSgjelinek status = do_subproc_interactive(cmdbuf); 4880f1adb09eSgjelinek if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) 4881f1adb09eSgjelinek goto cleanup; 4882f1adb09eSgjelinek 4883f1adb09eSgjelinek /* Now run the detach command within the mounted zone. */ 4884f1adb09eSgjelinek if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/sbin/zlogin -S %s " 4885f1adb09eSgjelinek "'/usr/sbin/zoneadm -z %s detach -n' >%s/SUNWdetached.xml", 4886f1adb09eSgjelinek target_zone, target_zone, zonepath) >= sizeof (cmdbuf)) 4887f1adb09eSgjelinek goto cleanup; 4888f1adb09eSgjelinek 4889f1adb09eSgjelinek status = do_subproc_interactive(cmdbuf); 4890f1adb09eSgjelinek if (subproc_status("detach", status, B_TRUE) != ZONE_SUBPROC_OK) 4891f1adb09eSgjelinek goto cleanup; 4892f1adb09eSgjelinek 4893f1adb09eSgjelinek res = B_TRUE; 4894f1adb09eSgjelinek 4895f1adb09eSgjelinek cleanup: 4896f1adb09eSgjelinek /* Cleanup from the previous actions. */ 4897f1adb09eSgjelinek if (mounted) { 4898f1adb09eSgjelinek if (snprintf(cmdbuf, sizeof (cmdbuf), 4899f1adb09eSgjelinek "/usr/sbin/zlogin -S %s '/usr/bin/rm -f /etc/zones/%s.xml'", 4900f1adb09eSgjelinek target_zone, target_zone) >= sizeof (cmdbuf)) { 4901f1adb09eSgjelinek res = B_FALSE; 4902f1adb09eSgjelinek } else { 49036cfd72c6Sgjelinek status = do_subproc_interactive(cmdbuf); 4904f1adb09eSgjelinek if (subproc_status("rm", status, B_TRUE) 4905f1adb09eSgjelinek != ZONE_SUBPROC_OK) 4906f1adb09eSgjelinek res = B_FALSE; 4907f1adb09eSgjelinek } 4908f1adb09eSgjelinek 4909f1adb09eSgjelinek if (unmount_func(0, NULL) != Z_OK) 4910f1adb09eSgjelinek res = B_FALSE; 4911f1adb09eSgjelinek } 4912f1adb09eSgjelinek 4913f1adb09eSgjelinek if (zone_set_state(target_zone, ZONE_STATE_CONFIGURED) != Z_OK) 4914f1adb09eSgjelinek res = B_FALSE; 4915f1adb09eSgjelinek 4916f1adb09eSgjelinek return (res); 4917f1adb09eSgjelinek } 4918f1adb09eSgjelinek 49196cfd72c6Sgjelinek /* 49206cfd72c6Sgjelinek * The zone needs to be updated so set it up for the update and initiate the 49216cfd72c6Sgjelinek * update within the scratch zone. First set the state to incomplete so we can 49226cfd72c6Sgjelinek * force-mount the zone for the update operation. We pass the -U option to the 49236cfd72c6Sgjelinek * mount so that the scratch zone is mounted without the zone's /etc and /var 49246cfd72c6Sgjelinek * being lofs mounted back into the scratch zone root. This is done by 49256cfd72c6Sgjelinek * overloading the bootbuf string in the zone_cmd_arg_t to pass -U as an option 49266cfd72c6Sgjelinek * to the mount cmd. 49276cfd72c6Sgjelinek */ 49286cfd72c6Sgjelinek static int 49296cfd72c6Sgjelinek attach_update(zone_dochandle_t handle, char *zonepath) 49306cfd72c6Sgjelinek { 49316cfd72c6Sgjelinek int err; 49326cfd72c6Sgjelinek int update_res; 49336cfd72c6Sgjelinek int status; 49346cfd72c6Sgjelinek zone_cmd_arg_t zarg; 49356cfd72c6Sgjelinek FILE *fp; 49366cfd72c6Sgjelinek struct zone_fstab fstab; 49376cfd72c6Sgjelinek char cmdbuf[(4 * MAXPATHLEN) + 20]; 49386cfd72c6Sgjelinek 49396cfd72c6Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE)) 49406cfd72c6Sgjelinek != Z_OK) { 49416cfd72c6Sgjelinek errno = err; 49426cfd72c6Sgjelinek zperror(gettext("could not set state"), B_TRUE); 49436cfd72c6Sgjelinek return (Z_ERR); 49446cfd72c6Sgjelinek } 49456cfd72c6Sgjelinek 49466cfd72c6Sgjelinek zarg.cmd = Z_FORCEMOUNT; 49476cfd72c6Sgjelinek (void) strlcpy(zarg.bootbuf, "-U", sizeof (zarg.bootbuf)); 49486cfd72c6Sgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 49496cfd72c6Sgjelinek zerror(gettext("could not mount zone")); 49506cfd72c6Sgjelinek 49516cfd72c6Sgjelinek /* We reset the state since the zone wasn't modified yet. */ 49526cfd72c6Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED)) 49536cfd72c6Sgjelinek != Z_OK) { 49546cfd72c6Sgjelinek errno = err; 49556cfd72c6Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 49566cfd72c6Sgjelinek } 49576cfd72c6Sgjelinek return (Z_ERR); 49586cfd72c6Sgjelinek } 49596cfd72c6Sgjelinek 49606cfd72c6Sgjelinek /* 49616cfd72c6Sgjelinek * Move data files generated by sw_up_to_date() into the scratch 49626cfd72c6Sgjelinek * zone's /tmp. 49636cfd72c6Sgjelinek */ 49646cfd72c6Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec /usr/bin/mv " 49656cfd72c6Sgjelinek "%s/pkg_add %s/pkg_rm %s/lu/tmp", 49666cfd72c6Sgjelinek zonepath, zonepath, zonepath); 49676cfd72c6Sgjelinek 49686cfd72c6Sgjelinek status = do_subproc_interactive(cmdbuf); 49696cfd72c6Sgjelinek if (subproc_status("mv", status, B_TRUE) != ZONE_SUBPROC_OK) { 49706cfd72c6Sgjelinek zperror(gettext("could not mv data files"), B_FALSE); 49716cfd72c6Sgjelinek goto fail; 49726cfd72c6Sgjelinek } 49736cfd72c6Sgjelinek 49746cfd72c6Sgjelinek /* 49756cfd72c6Sgjelinek * Save list of inherit-pkg-dirs into zone. Since the file is in 49766cfd72c6Sgjelinek * /tmp we don't have to worry about deleting it. 49776cfd72c6Sgjelinek */ 49786cfd72c6Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), "%s/lu/tmp/inherited", 49796cfd72c6Sgjelinek zonepath); 49806cfd72c6Sgjelinek if ((fp = fopen(cmdbuf, "w")) == NULL) { 49816cfd72c6Sgjelinek zperror(gettext("could not save inherit-pkg-dirs"), B_FALSE); 49826cfd72c6Sgjelinek goto fail; 49836cfd72c6Sgjelinek } 49846cfd72c6Sgjelinek if (zonecfg_setipdent(handle) != Z_OK) { 49856cfd72c6Sgjelinek zperror(gettext("could not enumerate inherit-pkg-dirs"), 49866cfd72c6Sgjelinek B_TRUE); 49876cfd72c6Sgjelinek goto fail; 49886cfd72c6Sgjelinek } 49896cfd72c6Sgjelinek while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 49906cfd72c6Sgjelinek if (fprintf(fp, "%s\n", fstab.zone_fs_dir) < 0) { 49916cfd72c6Sgjelinek zperror(gettext("could not save inherit-pkg-dirs"), 49926cfd72c6Sgjelinek B_FALSE); 49936cfd72c6Sgjelinek (void) fclose(fp); 49946cfd72c6Sgjelinek goto fail; 49956cfd72c6Sgjelinek } 49966cfd72c6Sgjelinek } 49976cfd72c6Sgjelinek (void) zonecfg_endipdent(handle); 49986cfd72c6Sgjelinek if (fclose(fp) != 0) { 49996cfd72c6Sgjelinek zperror(gettext("could not save inherit-pkg-dirs"), B_FALSE); 50006cfd72c6Sgjelinek goto fail; 50016cfd72c6Sgjelinek } 50026cfd72c6Sgjelinek 50036cfd72c6Sgjelinek /* run the updater inside the scratch zone */ 50046cfd72c6Sgjelinek (void) snprintf(cmdbuf, sizeof (cmdbuf), 50056cfd72c6Sgjelinek "exec /usr/sbin/zlogin -S %s " 50066cfd72c6Sgjelinek "/usr/lib/brand/native/attach_update %s", target_zone, target_zone); 50076cfd72c6Sgjelinek 50086cfd72c6Sgjelinek update_res = Z_OK; 50096cfd72c6Sgjelinek status = do_subproc_interactive(cmdbuf); 50106cfd72c6Sgjelinek if (subproc_status("attach_update", status, B_TRUE) 50116cfd72c6Sgjelinek != ZONE_SUBPROC_OK) { 50126cfd72c6Sgjelinek zerror(gettext("could not update zone")); 50136cfd72c6Sgjelinek update_res = Z_ERR; 50146cfd72c6Sgjelinek } 50156cfd72c6Sgjelinek 50166cfd72c6Sgjelinek zarg.cmd = Z_UNMOUNT; 50176cfd72c6Sgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) { 50186cfd72c6Sgjelinek zerror(gettext("could not unmount zone")); 50196cfd72c6Sgjelinek return (Z_ERR); 50206cfd72c6Sgjelinek } 50216cfd72c6Sgjelinek 50226cfd72c6Sgjelinek /* 50236cfd72c6Sgjelinek * If the update script within the scratch zone failed for some reason 50246cfd72c6Sgjelinek * we will now leave the zone in the incomplete state since we no 50256cfd72c6Sgjelinek * longer know the state of the files within the zonepath. 50266cfd72c6Sgjelinek */ 50276cfd72c6Sgjelinek if (update_res == Z_ERR) 50286cfd72c6Sgjelinek return (Z_ERR); 50296cfd72c6Sgjelinek 50306cfd72c6Sgjelinek zonecfg_rm_detached(handle, B_FALSE); 50316cfd72c6Sgjelinek 50326cfd72c6Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 50336cfd72c6Sgjelinek errno = err; 50346cfd72c6Sgjelinek zperror(gettext("could not set state"), B_TRUE); 50356cfd72c6Sgjelinek return (Z_ERR); 50366cfd72c6Sgjelinek } 50376cfd72c6Sgjelinek 50386cfd72c6Sgjelinek return (Z_OK); 50396cfd72c6Sgjelinek 50406cfd72c6Sgjelinek fail: 50416cfd72c6Sgjelinek zarg.cmd = Z_UNMOUNT; 50426cfd72c6Sgjelinek if (call_zoneadmd(target_zone, &zarg) != 0) 50436cfd72c6Sgjelinek zerror(gettext("could not unmount zone")); 50446cfd72c6Sgjelinek 50456cfd72c6Sgjelinek /* We reset the state since the zone wasn't modified yet. */ 50466cfd72c6Sgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED)) 50476cfd72c6Sgjelinek != Z_OK) { 50486cfd72c6Sgjelinek errno = err; 50496cfd72c6Sgjelinek zperror(gettext("could not reset state"), B_TRUE); 50506cfd72c6Sgjelinek } 50516cfd72c6Sgjelinek 50526cfd72c6Sgjelinek return (Z_ERR); 50536cfd72c6Sgjelinek } 50546cfd72c6Sgjelinek 50556cfd72c6Sgjelinek /* ARGSUSED */ 50566cfd72c6Sgjelinek static void 50576cfd72c6Sgjelinek sigcleanup(int sig) 50586cfd72c6Sgjelinek { 50596cfd72c6Sgjelinek attach_interupted = B_TRUE; 50606cfd72c6Sgjelinek } 50616cfd72c6Sgjelinek 50626cfd72c6Sgjelinek 5063ee519a1fSgjelinek static int 5064ee519a1fSgjelinek attach_func(int argc, char *argv[]) 5065ee519a1fSgjelinek { 5066ee519a1fSgjelinek int lockfd; 5067ee519a1fSgjelinek int err, arg; 5068ee519a1fSgjelinek boolean_t force = B_FALSE; 5069ee519a1fSgjelinek zone_dochandle_t handle; 5070ee519a1fSgjelinek zone_dochandle_t athandle = NULL; 5071ee519a1fSgjelinek char zonepath[MAXPATHLEN]; 50729acbbeafSnn35248 char brand[MAXNAMELEN], atbrand[MAXNAMELEN]; 507337774979Sgjelinek char cmdbuf[MAXPATHLEN]; 50748cd327d5Sgjelinek boolean_t execute = B_TRUE; 5075f1adb09eSgjelinek boolean_t retried = B_FALSE; 50766cfd72c6Sgjelinek boolean_t update = B_FALSE; 50778cd327d5Sgjelinek char *manifest_path; 507837774979Sgjelinek brand_handle_t bh = NULL; 5079ee519a1fSgjelinek 5080ee519a1fSgjelinek if (zonecfg_in_alt_root()) { 5081ee519a1fSgjelinek zerror(gettext("cannot attach zone in alternate root")); 5082ee519a1fSgjelinek return (Z_ERR); 5083ee519a1fSgjelinek } 5084ee519a1fSgjelinek 5085ee519a1fSgjelinek optind = 0; 50866cfd72c6Sgjelinek if ((arg = getopt(argc, argv, "?Fn:u")) != EOF) { 5087ee519a1fSgjelinek switch (arg) { 5088ee519a1fSgjelinek case '?': 5089ee519a1fSgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 5090ee519a1fSgjelinek return (optopt == '?' ? Z_OK : Z_USAGE); 5091ee519a1fSgjelinek case 'F': 5092ee519a1fSgjelinek force = B_TRUE; 5093ee519a1fSgjelinek break; 50948cd327d5Sgjelinek case 'n': 50958cd327d5Sgjelinek execute = B_FALSE; 50968cd327d5Sgjelinek manifest_path = optarg; 50978cd327d5Sgjelinek break; 50986cfd72c6Sgjelinek case 'u': 50996cfd72c6Sgjelinek update = B_TRUE; 51006cfd72c6Sgjelinek break; 5101ee519a1fSgjelinek default: 5102ee519a1fSgjelinek sub_usage(SHELP_ATTACH, CMD_ATTACH); 5103ee519a1fSgjelinek return (Z_USAGE); 5104ee519a1fSgjelinek } 5105ee519a1fSgjelinek } 51068cd327d5Sgjelinek 51076cfd72c6Sgjelinek /* dry-run and update flags are mutually exclusive */ 51086cfd72c6Sgjelinek if (!execute && update) { 51096cfd72c6Sgjelinek zerror(gettext("-n and -u flags are mutually exclusive")); 51106cfd72c6Sgjelinek return (Z_ERR); 51116cfd72c6Sgjelinek } 51126cfd72c6Sgjelinek 51138cd327d5Sgjelinek /* 51148cd327d5Sgjelinek * If the no-execute option was specified, we need to branch down 51158cd327d5Sgjelinek * a completely different path since there is no zone required to be 51168cd327d5Sgjelinek * configured for this option. 51178cd327d5Sgjelinek */ 51188cd327d5Sgjelinek if (!execute) 5119ce28b40eSzt129084 return (dryrun_attach(manifest_path, argv)); 51208cd327d5Sgjelinek 51219acbbeafSnn35248 if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, B_TRUE, B_FALSE) 51229acbbeafSnn35248 != Z_OK) 5123ee519a1fSgjelinek return (Z_ERR); 5124ce28b40eSzt129084 if (verify_details(CMD_ATTACH, argv) != Z_OK) 5125ee519a1fSgjelinek return (Z_ERR); 5126ee519a1fSgjelinek 5127ee519a1fSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath))) 5128ee519a1fSgjelinek != Z_OK) { 5129ee519a1fSgjelinek errno = err; 5130ee519a1fSgjelinek zperror2(target_zone, gettext("could not get zone path")); 5131ee519a1fSgjelinek return (Z_ERR); 5132ee519a1fSgjelinek } 5133ee519a1fSgjelinek 5134ee519a1fSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 5135ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 5136ee519a1fSgjelinek return (Z_ERR); 5137ee519a1fSgjelinek } 5138ee519a1fSgjelinek 5139ee519a1fSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 5140ee519a1fSgjelinek errno = err; 5141ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 5142ee519a1fSgjelinek zonecfg_fini_handle(handle); 5143ee519a1fSgjelinek return (Z_ERR); 5144ee519a1fSgjelinek } 5145ee519a1fSgjelinek 514637774979Sgjelinek /* Fetch the postattach hook from the brand configuration. */ 514737774979Sgjelinek if ((bh = brand_open(target_brand)) == NULL) { 514837774979Sgjelinek zerror(gettext("missing or invalid brand")); 514937774979Sgjelinek return (Z_ERR); 515037774979Sgjelinek } 515137774979Sgjelinek 515237774979Sgjelinek (void) strcpy(cmdbuf, EXEC_PREFIX); 515337774979Sgjelinek if (brand_get_postattach(bh, target_zone, zonepath, cmdbuf + EXEC_LEN, 515437774979Sgjelinek sizeof (cmdbuf) - EXEC_LEN, 0, NULL) != 0) { 515537774979Sgjelinek zerror("invalid brand configuration: missing postattach " 515637774979Sgjelinek "resource"); 515737774979Sgjelinek brand_close(bh); 515837774979Sgjelinek return (Z_ERR); 515937774979Sgjelinek } 516037774979Sgjelinek brand_close(bh); 516137774979Sgjelinek 516237774979Sgjelinek /* If we have a brand postattach hook and the force flag, append it. */ 516337774979Sgjelinek if (strlen(cmdbuf) > EXEC_LEN && force) { 516437774979Sgjelinek if (addopt(cmdbuf, 0, "-F", sizeof (cmdbuf)) != Z_OK) { 516537774979Sgjelinek zerror("Postattach command line too long"); 516637774979Sgjelinek return (Z_ERR); 516737774979Sgjelinek } 516837774979Sgjelinek } 516937774979Sgjelinek 5170ee519a1fSgjelinek if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 5171ee519a1fSgjelinek zerror(gettext("another %s may have an operation in progress."), 5172ee519a1fSgjelinek "zoneadm"); 5173ee519a1fSgjelinek zonecfg_fini_handle(handle); 5174ee519a1fSgjelinek return (Z_ERR); 5175ee519a1fSgjelinek } 5176ee519a1fSgjelinek 5177ee519a1fSgjelinek if (force) 5178ee519a1fSgjelinek goto forced; 5179ee519a1fSgjelinek 5180ee519a1fSgjelinek if ((athandle = zonecfg_init_handle()) == NULL) { 5181ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 5182ee519a1fSgjelinek goto done; 5183ee519a1fSgjelinek } 5184ee519a1fSgjelinek 5185f1adb09eSgjelinek retry: 5186ee519a1fSgjelinek if ((err = zonecfg_get_attach_handle(zonepath, target_zone, B_TRUE, 5187ee519a1fSgjelinek athandle)) != Z_OK) { 5188f1adb09eSgjelinek if (err == Z_NO_ZONE) { 5189f1adb09eSgjelinek /* 5190f1adb09eSgjelinek * Zone was not detached. Try to fall back to getting 5191f1adb09eSgjelinek * the needed information from within the zone. 5192f1adb09eSgjelinek * However, we can only try to generate the attach 5193f1adb09eSgjelinek * information for native branded zones, so fail if the 5194f1adb09eSgjelinek * zone is not native. 5195f1adb09eSgjelinek */ 5196f1adb09eSgjelinek if (!is_native_zone) { 5197f1adb09eSgjelinek zerror(gettext("Not a detached zone.")); 5198f1adb09eSgjelinek goto done; 5199f1adb09eSgjelinek } 5200f1adb09eSgjelinek 5201f1adb09eSgjelinek if (!retried) { 5202f1adb09eSgjelinek zerror(gettext("The zone was not properly " 5203f1adb09eSgjelinek "detached.\n\tAttempting to attach " 5204f1adb09eSgjelinek "anyway.")); 5205f1adb09eSgjelinek if (gen_detach_info(zonepath)) { 5206f1adb09eSgjelinek retried = B_TRUE; 5207f1adb09eSgjelinek goto retry; 5208f1adb09eSgjelinek } 5209f1adb09eSgjelinek } 5210f1adb09eSgjelinek zerror(gettext("Cannot generate the information " 5211f1adb09eSgjelinek "needed to attach this zone.")); 5212f1adb09eSgjelinek } else if (err == Z_INVALID_DOCUMENT) { 5213ee519a1fSgjelinek zerror(gettext("Cannot attach to an earlier release " 5214ee519a1fSgjelinek "of the operating system")); 5215f1adb09eSgjelinek } else { 5216ee519a1fSgjelinek zperror(cmd_to_str(CMD_ATTACH), B_TRUE); 5217f1adb09eSgjelinek } 5218ee519a1fSgjelinek goto done; 5219ee519a1fSgjelinek } 5220ee519a1fSgjelinek 5221ee519a1fSgjelinek /* Get the detach information for the locally defined zone. */ 5222ee519a1fSgjelinek if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) { 5223ee519a1fSgjelinek errno = err; 5224ee519a1fSgjelinek zperror(gettext("getting the attach information failed"), 5225ee519a1fSgjelinek B_TRUE); 5226ee519a1fSgjelinek goto done; 5227ee519a1fSgjelinek } 5228ee519a1fSgjelinek 52299acbbeafSnn35248 /* 52309acbbeafSnn35248 * Ensure that the detached and locally defined zones are both of 52319acbbeafSnn35248 * the same brand. 52329acbbeafSnn35248 */ 52339acbbeafSnn35248 if ((zonecfg_get_brand(handle, brand, sizeof (brand)) != 0) || 52349acbbeafSnn35248 (zonecfg_get_brand(athandle, atbrand, sizeof (atbrand)) != 0)) { 52359acbbeafSnn35248 err = Z_ERR; 52369acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 52379acbbeafSnn35248 goto done; 52389acbbeafSnn35248 } 52399acbbeafSnn35248 52409acbbeafSnn35248 if (strcmp(atbrand, brand) != NULL) { 52419acbbeafSnn35248 err = Z_ERR; 52429acbbeafSnn35248 zerror(gettext("Trying to attach a '%s' zone to a '%s' " 52439acbbeafSnn35248 "configuration."), atbrand, brand); 52449acbbeafSnn35248 goto done; 52459acbbeafSnn35248 } 52469acbbeafSnn35248 52476cfd72c6Sgjelinek /* 52486cfd72c6Sgjelinek * If we're doing an update on attach, and the zone does need to be 52496cfd72c6Sgjelinek * updated, then run the update. 52506cfd72c6Sgjelinek */ 52516cfd72c6Sgjelinek if (update) { 52526cfd72c6Sgjelinek char fname[MAXPATHLEN]; 52536cfd72c6Sgjelinek 52546cfd72c6Sgjelinek (void) sigset(SIGINT, sigcleanup); 52556cfd72c6Sgjelinek 52566cfd72c6Sgjelinek if ((err = sw_up_to_date(handle, athandle, zonepath)) != Z_OK) { 52576cfd72c6Sgjelinek if (err != Z_FATAL && !attach_interupted) { 52586cfd72c6Sgjelinek err = Z_FATAL; 52596cfd72c6Sgjelinek err = attach_update(handle, zonepath); 52606cfd72c6Sgjelinek } 52616cfd72c6Sgjelinek if (!attach_interupted || err == Z_OK) 52626cfd72c6Sgjelinek goto done; 52636cfd72c6Sgjelinek } 52646cfd72c6Sgjelinek 52656cfd72c6Sgjelinek (void) sigset(SIGINT, SIG_DFL); 52666cfd72c6Sgjelinek 52676cfd72c6Sgjelinek /* clean up data files left behind by sw_up_to_date() */ 52686cfd72c6Sgjelinek (void) snprintf(fname, sizeof (fname), "%s/pkg_add", zonepath); 52696cfd72c6Sgjelinek (void) unlink(fname); 52706cfd72c6Sgjelinek (void) snprintf(fname, sizeof (fname), "%s/pkg_rm", zonepath); 52716cfd72c6Sgjelinek (void) unlink(fname); 52726cfd72c6Sgjelinek 52736cfd72c6Sgjelinek if (attach_interupted) { 52746cfd72c6Sgjelinek err = Z_FATAL; 52756cfd72c6Sgjelinek goto done; 52766cfd72c6Sgjelinek } 52776cfd72c6Sgjelinek 52786cfd72c6Sgjelinek } else { 5279ee519a1fSgjelinek /* sw_cmp prints error msgs as necessary */ 52800b5de56dSgjelinek if ((err = sw_cmp(handle, athandle, SW_CMP_NONE)) != Z_OK) 5281ee519a1fSgjelinek goto done; 5282ee519a1fSgjelinek 5283ee519a1fSgjelinek if ((err = dev_fix(athandle)) != Z_OK) 5284ee519a1fSgjelinek goto done; 52856cfd72c6Sgjelinek } 5286ee519a1fSgjelinek 5287ee519a1fSgjelinek forced: 5288ee519a1fSgjelinek 5289ee519a1fSgjelinek zonecfg_rm_detached(handle, force); 5290ee519a1fSgjelinek 5291ee519a1fSgjelinek if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) { 5292ee519a1fSgjelinek errno = err; 5293ee519a1fSgjelinek zperror(gettext("could not reset state"), B_TRUE); 5294ee519a1fSgjelinek } 5295ee519a1fSgjelinek 5296ee519a1fSgjelinek done: 5297ee519a1fSgjelinek zonecfg_fini_handle(handle); 5298ee519a1fSgjelinek release_lock_file(lockfd); 5299ee519a1fSgjelinek if (athandle != NULL) 5300ee519a1fSgjelinek zonecfg_fini_handle(athandle); 5301ee519a1fSgjelinek 530237774979Sgjelinek /* If we have a brand postattach hook, run it. */ 530337774979Sgjelinek if (err == Z_OK && strlen(cmdbuf) > EXEC_LEN) { 530437774979Sgjelinek int status; 530537774979Sgjelinek 530637774979Sgjelinek status = do_subproc(cmdbuf); 530737774979Sgjelinek if (subproc_status(gettext("brand-specific postattach"), 530837774979Sgjelinek status, B_FALSE) != ZONE_SUBPROC_OK) { 530937774979Sgjelinek if ((err = zone_set_state(target_zone, 531037774979Sgjelinek ZONE_STATE_CONFIGURED)) != Z_OK) { 531137774979Sgjelinek errno = err; 531237774979Sgjelinek zperror(gettext("could not reset state"), 531337774979Sgjelinek B_TRUE); 531437774979Sgjelinek } 531537774979Sgjelinek return (Z_ERR); 531637774979Sgjelinek } 531737774979Sgjelinek } 531837774979Sgjelinek 5319ee519a1fSgjelinek return ((err == Z_OK) ? Z_OK : Z_ERR); 5320ee519a1fSgjelinek } 5321ee519a1fSgjelinek 5322865e09a4Sgjelinek /* 53237c478bd9Sstevel@tonic-gate * On input, TRUE => yes, FALSE => no. 53247c478bd9Sstevel@tonic-gate * On return, TRUE => 1, FALSE => 0, could not ask => -1. 53257c478bd9Sstevel@tonic-gate */ 53267c478bd9Sstevel@tonic-gate 53277c478bd9Sstevel@tonic-gate static int 53287c478bd9Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question) 53297c478bd9Sstevel@tonic-gate { 53307c478bd9Sstevel@tonic-gate char line[64]; /* should be large enough to answer yes or no */ 53317c478bd9Sstevel@tonic-gate 53327c478bd9Sstevel@tonic-gate if (!isatty(STDIN_FILENO)) 53337c478bd9Sstevel@tonic-gate return (-1); 53347c478bd9Sstevel@tonic-gate for (;;) { 53357c478bd9Sstevel@tonic-gate (void) printf("%s (%s)? ", question, 53367c478bd9Sstevel@tonic-gate default_answer ? "[y]/n" : "y/[n]"); 53377c478bd9Sstevel@tonic-gate if (fgets(line, sizeof (line), stdin) == NULL || 53387c478bd9Sstevel@tonic-gate line[0] == '\n') 53397c478bd9Sstevel@tonic-gate return (default_answer ? 1 : 0); 53407c478bd9Sstevel@tonic-gate if (tolower(line[0]) == 'y') 53417c478bd9Sstevel@tonic-gate return (1); 53427c478bd9Sstevel@tonic-gate if (tolower(line[0]) == 'n') 53437c478bd9Sstevel@tonic-gate return (0); 53447c478bd9Sstevel@tonic-gate } 53457c478bd9Sstevel@tonic-gate } 53467c478bd9Sstevel@tonic-gate 53477c478bd9Sstevel@tonic-gate static int 53487c478bd9Sstevel@tonic-gate uninstall_func(int argc, char *argv[]) 53497c478bd9Sstevel@tonic-gate { 53507c478bd9Sstevel@tonic-gate char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */ 53510b5de56dSgjelinek char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN]; 535237774979Sgjelinek char cmdbuf[MAXPATHLEN]; 53537c478bd9Sstevel@tonic-gate boolean_t force = B_FALSE; 53547c478bd9Sstevel@tonic-gate int lockfd, answer; 53557c478bd9Sstevel@tonic-gate int err, arg; 535637774979Sgjelinek brand_handle_t bh = NULL; 53577c478bd9Sstevel@tonic-gate 5358108322fbScarlsonj if (zonecfg_in_alt_root()) { 5359108322fbScarlsonj zerror(gettext("cannot uninstall zone in alternate root")); 5360108322fbScarlsonj return (Z_ERR); 5361108322fbScarlsonj } 5362108322fbScarlsonj 53637c478bd9Sstevel@tonic-gate optind = 0; 53647c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, "?F")) != EOF) { 53657c478bd9Sstevel@tonic-gate switch (arg) { 53667c478bd9Sstevel@tonic-gate case '?': 53677c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 53687c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 53697c478bd9Sstevel@tonic-gate case 'F': 53707c478bd9Sstevel@tonic-gate force = B_TRUE; 53717c478bd9Sstevel@tonic-gate break; 53727c478bd9Sstevel@tonic-gate default: 53737c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 53747c478bd9Sstevel@tonic-gate return (Z_USAGE); 53757c478bd9Sstevel@tonic-gate } 53767c478bd9Sstevel@tonic-gate } 53777c478bd9Sstevel@tonic-gate if (argc > optind) { 53787c478bd9Sstevel@tonic-gate sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL); 53797c478bd9Sstevel@tonic-gate return (Z_USAGE); 53807c478bd9Sstevel@tonic-gate } 53817c478bd9Sstevel@tonic-gate 53829acbbeafSnn35248 if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE, B_FALSE) 53839acbbeafSnn35248 != Z_OK) 53847c478bd9Sstevel@tonic-gate return (Z_ERR); 53857c478bd9Sstevel@tonic-gate 5386ce28b40eSzt129084 /* 5387ce28b40eSzt129084 * Invoke brand-specific handler. 5388ce28b40eSzt129084 */ 5389ce28b40eSzt129084 if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK) 5390ce28b40eSzt129084 return (Z_ERR); 5391ce28b40eSzt129084 53927c478bd9Sstevel@tonic-gate if (!force) { 53937c478bd9Sstevel@tonic-gate (void) snprintf(line, sizeof (line), 53947c478bd9Sstevel@tonic-gate gettext("Are you sure you want to %s zone %s"), 53957c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL), target_zone); 53967c478bd9Sstevel@tonic-gate if ((answer = ask_yesno(B_FALSE, line)) == 0) { 53977c478bd9Sstevel@tonic-gate return (Z_OK); 53987c478bd9Sstevel@tonic-gate } else if (answer == -1) { 53997c478bd9Sstevel@tonic-gate zerror(gettext("Input not from terminal and -F " 54007c478bd9Sstevel@tonic-gate "not specified: %s not done."), 54017c478bd9Sstevel@tonic-gate cmd_to_str(CMD_UNINSTALL)); 54027c478bd9Sstevel@tonic-gate return (Z_ERR); 54037c478bd9Sstevel@tonic-gate } 54047c478bd9Sstevel@tonic-gate } 54057c478bd9Sstevel@tonic-gate 54060b5de56dSgjelinek if ((err = zone_get_zonepath(target_zone, zonepath, 54070b5de56dSgjelinek sizeof (zonepath))) != Z_OK) { 54087c478bd9Sstevel@tonic-gate errno = err; 54097c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get zone path")); 54107c478bd9Sstevel@tonic-gate return (Z_ERR); 54117c478bd9Sstevel@tonic-gate } 54127c478bd9Sstevel@tonic-gate if ((err = zone_get_rootpath(target_zone, rootpath, 54137c478bd9Sstevel@tonic-gate sizeof (rootpath))) != Z_OK) { 54147c478bd9Sstevel@tonic-gate errno = err; 54157c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not get root path")); 54167c478bd9Sstevel@tonic-gate return (Z_ERR); 54177c478bd9Sstevel@tonic-gate } 54187c478bd9Sstevel@tonic-gate 54197c478bd9Sstevel@tonic-gate /* 54207c478bd9Sstevel@tonic-gate * If there seems to be a zoneadmd running for this zone, call it 54217c478bd9Sstevel@tonic-gate * to tell it that an uninstall is happening; if all goes well it 54227c478bd9Sstevel@tonic-gate * will then shut itself down. 54237c478bd9Sstevel@tonic-gate */ 54247c478bd9Sstevel@tonic-gate if (ping_zoneadmd(target_zone) == Z_OK) { 54257c478bd9Sstevel@tonic-gate zone_cmd_arg_t zarg; 54267c478bd9Sstevel@tonic-gate zarg.cmd = Z_NOTE_UNINSTALLING; 54277c478bd9Sstevel@tonic-gate /* we don't care too much if this fails... just plow on */ 54287c478bd9Sstevel@tonic-gate (void) call_zoneadmd(target_zone, &zarg); 54297c478bd9Sstevel@tonic-gate } 54307c478bd9Sstevel@tonic-gate 54317c478bd9Sstevel@tonic-gate if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 54327c478bd9Sstevel@tonic-gate zerror(gettext("another %s may have an operation in progress."), 5433865e09a4Sgjelinek "zoneadm"); 54347c478bd9Sstevel@tonic-gate return (Z_ERR); 54357c478bd9Sstevel@tonic-gate } 54367c478bd9Sstevel@tonic-gate 54377c478bd9Sstevel@tonic-gate /* Don't uninstall the zone if anything is mounted there */ 54387c478bd9Sstevel@tonic-gate err = zonecfg_find_mounts(rootpath, NULL, NULL); 54397c478bd9Sstevel@tonic-gate if (err) { 54400b5de56dSgjelinek zerror(gettext("These file systems are mounted on " 54417c478bd9Sstevel@tonic-gate "subdirectories of %s.\n"), rootpath); 54427c478bd9Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, zfm_print, NULL); 54437c478bd9Sstevel@tonic-gate return (Z_ERR); 54447c478bd9Sstevel@tonic-gate } 54457c478bd9Sstevel@tonic-gate 544637774979Sgjelinek /* Fetch the uninstall hook from the brand configuration. */ 544737774979Sgjelinek if ((bh = brand_open(target_brand)) == NULL) { 544837774979Sgjelinek zerror(gettext("missing or invalid brand")); 544937774979Sgjelinek return (Z_ERR); 545037774979Sgjelinek } 545137774979Sgjelinek 545237774979Sgjelinek (void) strcpy(cmdbuf, EXEC_PREFIX); 545337774979Sgjelinek if (brand_get_preuninstall(bh, target_zone, zonepath, 545437774979Sgjelinek cmdbuf + EXEC_LEN, sizeof (cmdbuf) - EXEC_LEN, 0, NULL) 545537774979Sgjelinek != 0) { 545637774979Sgjelinek zerror("invalid brand configuration: missing preuninstall " 545737774979Sgjelinek "resource"); 545837774979Sgjelinek brand_close(bh); 545937774979Sgjelinek return (Z_ERR); 546037774979Sgjelinek } 546137774979Sgjelinek brand_close(bh); 546237774979Sgjelinek 546337774979Sgjelinek if (strlen(cmdbuf) > EXEC_LEN) { 546437774979Sgjelinek int status; 546537774979Sgjelinek 546637774979Sgjelinek /* If we have the force flag, append it. */ 546737774979Sgjelinek if (force && addopt(cmdbuf, 0, "-F", sizeof (cmdbuf)) != Z_OK) { 546837774979Sgjelinek zerror("Preuninstall command line too long"); 546937774979Sgjelinek return (Z_ERR); 547037774979Sgjelinek } 547137774979Sgjelinek 547237774979Sgjelinek status = do_subproc(cmdbuf); 547337774979Sgjelinek if (subproc_status(gettext("brand-specific preuninstall"), 547437774979Sgjelinek status, B_FALSE) != ZONE_SUBPROC_OK) { 547537774979Sgjelinek return (Z_ERR); 547637774979Sgjelinek } 547737774979Sgjelinek } 547837774979Sgjelinek 54797c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 54807c478bd9Sstevel@tonic-gate if (err != Z_OK) { 54817c478bd9Sstevel@tonic-gate errno = err; 54827c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not set state")); 54837c478bd9Sstevel@tonic-gate goto bad; 54847c478bd9Sstevel@tonic-gate } 54857c478bd9Sstevel@tonic-gate 54860b5de56dSgjelinek if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) { 54870b5de56dSgjelinek errno = err; 54880b5de56dSgjelinek zperror2(target_zone, gettext("cleaning up zonepath failed")); 54897c478bd9Sstevel@tonic-gate goto bad; 54900b5de56dSgjelinek } 54910b5de56dSgjelinek 54927c478bd9Sstevel@tonic-gate err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED); 54937c478bd9Sstevel@tonic-gate if (err != Z_OK) { 54947c478bd9Sstevel@tonic-gate errno = err; 54957c478bd9Sstevel@tonic-gate zperror2(target_zone, gettext("could not reset state")); 54967c478bd9Sstevel@tonic-gate } 54977c478bd9Sstevel@tonic-gate bad: 54987c478bd9Sstevel@tonic-gate release_lock_file(lockfd); 54997c478bd9Sstevel@tonic-gate return (err); 55007c478bd9Sstevel@tonic-gate } 55017c478bd9Sstevel@tonic-gate 5502108322fbScarlsonj /* ARGSUSED */ 5503108322fbScarlsonj static int 5504108322fbScarlsonj mount_func(int argc, char *argv[]) 5505108322fbScarlsonj { 5506108322fbScarlsonj zone_cmd_arg_t zarg; 55079acbbeafSnn35248 boolean_t force = B_FALSE; 55089acbbeafSnn35248 int arg; 5509108322fbScarlsonj 55109acbbeafSnn35248 /* 55119acbbeafSnn35248 * The only supported subargument to the "mount" subcommand is 55129acbbeafSnn35248 * "-f", which forces us to mount a zone in the INCOMPLETE state. 55139acbbeafSnn35248 */ 55149acbbeafSnn35248 optind = 0; 55159acbbeafSnn35248 if ((arg = getopt(argc, argv, "f")) != EOF) { 55169acbbeafSnn35248 switch (arg) { 55179acbbeafSnn35248 case 'f': 55189acbbeafSnn35248 force = B_TRUE; 55199acbbeafSnn35248 break; 55209acbbeafSnn35248 default: 5521108322fbScarlsonj return (Z_USAGE); 55229acbbeafSnn35248 } 55239acbbeafSnn35248 } 55249acbbeafSnn35248 if (argc > optind) 55259acbbeafSnn35248 return (Z_USAGE); 55269acbbeafSnn35248 55279acbbeafSnn35248 if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force) 55289acbbeafSnn35248 != Z_OK) 5529108322fbScarlsonj return (Z_ERR); 5530ce28b40eSzt129084 if (verify_details(CMD_MOUNT, argv) != Z_OK) 5531108322fbScarlsonj return (Z_ERR); 5532108322fbScarlsonj 55339acbbeafSnn35248 zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT; 55346cfd72c6Sgjelinek zarg.bootbuf[0] = '\0'; 5535108322fbScarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 5536108322fbScarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 5537108322fbScarlsonj return (Z_ERR); 5538108322fbScarlsonj } 5539108322fbScarlsonj return (Z_OK); 5540108322fbScarlsonj } 5541108322fbScarlsonj 5542108322fbScarlsonj /* ARGSUSED */ 5543108322fbScarlsonj static int 5544108322fbScarlsonj unmount_func(int argc, char *argv[]) 5545108322fbScarlsonj { 5546108322fbScarlsonj zone_cmd_arg_t zarg; 5547108322fbScarlsonj 5548108322fbScarlsonj if (argc > 0) 5549108322fbScarlsonj return (Z_USAGE); 55509acbbeafSnn35248 if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE) 55519acbbeafSnn35248 != Z_OK) 5552108322fbScarlsonj return (Z_ERR); 5553108322fbScarlsonj 5554108322fbScarlsonj zarg.cmd = Z_UNMOUNT; 5555108322fbScarlsonj if (call_zoneadmd(target_zone, &zarg) != 0) { 5556108322fbScarlsonj zerror(gettext("call to %s failed"), "zoneadmd"); 5557108322fbScarlsonj return (Z_ERR); 5558108322fbScarlsonj } 5559108322fbScarlsonj return (Z_OK); 5560108322fbScarlsonj } 5561108322fbScarlsonj 55627c478bd9Sstevel@tonic-gate static int 5563555afedfScarlsonj mark_func(int argc, char *argv[]) 5564555afedfScarlsonj { 5565555afedfScarlsonj int err, lockfd; 5566555afedfScarlsonj 5567555afedfScarlsonj if (argc != 1 || strcmp(argv[0], "incomplete") != 0) 5568555afedfScarlsonj return (Z_USAGE); 55699acbbeafSnn35248 if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_FALSE, B_FALSE) 55709acbbeafSnn35248 != Z_OK) 5571555afedfScarlsonj return (Z_ERR); 5572555afedfScarlsonj 5573ce28b40eSzt129084 /* 5574ce28b40eSzt129084 * Invoke brand-specific handler. 5575ce28b40eSzt129084 */ 5576ce28b40eSzt129084 if (invoke_brand_handler(CMD_MARK, argv) != Z_OK) 5577ce28b40eSzt129084 return (Z_ERR); 5578ce28b40eSzt129084 5579555afedfScarlsonj if (grab_lock_file(target_zone, &lockfd) != Z_OK) { 5580555afedfScarlsonj zerror(gettext("another %s may have an operation in progress."), 5581555afedfScarlsonj "zoneadm"); 5582555afedfScarlsonj return (Z_ERR); 5583555afedfScarlsonj } 5584555afedfScarlsonj 5585555afedfScarlsonj err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE); 5586555afedfScarlsonj if (err != Z_OK) { 5587555afedfScarlsonj errno = err; 5588555afedfScarlsonj zperror2(target_zone, gettext("could not set state")); 5589555afedfScarlsonj } 5590555afedfScarlsonj release_lock_file(lockfd); 5591555afedfScarlsonj 5592555afedfScarlsonj return (err); 5593555afedfScarlsonj } 5594555afedfScarlsonj 55950209230bSgjelinek /* 55960209230bSgjelinek * Check what scheduling class we're running under and print a warning if 55970209230bSgjelinek * we're not using FSS. 55980209230bSgjelinek */ 55990209230bSgjelinek static int 56000209230bSgjelinek check_sched_fss(zone_dochandle_t handle) 56010209230bSgjelinek { 56020209230bSgjelinek char class_name[PC_CLNMSZ]; 56030209230bSgjelinek 56040209230bSgjelinek if (zonecfg_get_dflt_sched_class(handle, class_name, 56050209230bSgjelinek sizeof (class_name)) != Z_OK) { 56060209230bSgjelinek zerror(gettext("WARNING: unable to determine the zone's " 56070209230bSgjelinek "scheduling class")); 56080209230bSgjelinek } else if (strcmp("FSS", class_name) != 0) { 56090209230bSgjelinek zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n" 56100209230bSgjelinek "FSS is not the default scheduling class for this zone. " 56110209230bSgjelinek "FSS will be\nused for processes in the zone but to get " 56120209230bSgjelinek "the full benefit of FSS,\nit should be the default " 56130209230bSgjelinek "scheduling class. See dispadmin(1M) for\nmore details.")); 56140209230bSgjelinek return (Z_SYSTEM); 56150209230bSgjelinek } 56160209230bSgjelinek 56170209230bSgjelinek return (Z_OK); 56180209230bSgjelinek } 56190209230bSgjelinek 56200209230bSgjelinek static int 56210209230bSgjelinek check_cpu_shares_sched(zone_dochandle_t handle) 56220209230bSgjelinek { 56230209230bSgjelinek int err; 56240209230bSgjelinek int res = Z_OK; 56250209230bSgjelinek struct zone_rctltab rctl; 56260209230bSgjelinek 56270209230bSgjelinek if ((err = zonecfg_setrctlent(handle)) != Z_OK) { 56280209230bSgjelinek errno = err; 56290209230bSgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 56300209230bSgjelinek return (err); 56310209230bSgjelinek } 56320209230bSgjelinek 56330209230bSgjelinek while (zonecfg_getrctlent(handle, &rctl) == Z_OK) { 56340209230bSgjelinek if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) { 56350209230bSgjelinek if (check_sched_fss(handle) != Z_OK) 56360209230bSgjelinek res = Z_SYSTEM; 56370209230bSgjelinek break; 56380209230bSgjelinek } 56390209230bSgjelinek } 56400209230bSgjelinek 56410209230bSgjelinek (void) zonecfg_endrctlent(handle); 56420209230bSgjelinek 56430209230bSgjelinek return (res); 56440209230bSgjelinek } 56450209230bSgjelinek 56460209230bSgjelinek /* 56477ef01d19Sgjelinek * Check if there is a mix of processes running in different pools within the 56487ef01d19Sgjelinek * zone. This is currently only going to be called for the global zone from 56497ef01d19Sgjelinek * apply_func but that could be generalized in the future. 56507ef01d19Sgjelinek */ 56517ef01d19Sgjelinek static boolean_t 56527ef01d19Sgjelinek mixed_pools(zoneid_t zoneid) 56537ef01d19Sgjelinek { 56547ef01d19Sgjelinek DIR *dirp; 56557ef01d19Sgjelinek dirent_t *dent; 56567ef01d19Sgjelinek boolean_t mixed = B_FALSE; 56577ef01d19Sgjelinek boolean_t poolid_set = B_FALSE; 56587ef01d19Sgjelinek poolid_t last_poolid = 0; 56597ef01d19Sgjelinek 56607ef01d19Sgjelinek if ((dirp = opendir("/proc")) == NULL) { 56617ef01d19Sgjelinek zerror(gettext("could not open /proc")); 56627ef01d19Sgjelinek return (B_FALSE); 56637ef01d19Sgjelinek } 56647ef01d19Sgjelinek 56657ef01d19Sgjelinek while ((dent = readdir(dirp)) != NULL) { 56667ef01d19Sgjelinek int procfd; 56677ef01d19Sgjelinek psinfo_t ps; 56687ef01d19Sgjelinek char procpath[MAXPATHLEN]; 56697ef01d19Sgjelinek 56707ef01d19Sgjelinek if (dent->d_name[0] == '.') 56717ef01d19Sgjelinek continue; 56727ef01d19Sgjelinek 56737ef01d19Sgjelinek (void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo", 56747ef01d19Sgjelinek dent->d_name); 56757ef01d19Sgjelinek 56767ef01d19Sgjelinek if ((procfd = open(procpath, O_RDONLY)) == -1) 56777ef01d19Sgjelinek continue; 56787ef01d19Sgjelinek 56797ef01d19Sgjelinek if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) { 56807ef01d19Sgjelinek /* skip processes in other zones and system processes */ 56817ef01d19Sgjelinek if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) { 56827ef01d19Sgjelinek (void) close(procfd); 56837ef01d19Sgjelinek continue; 56847ef01d19Sgjelinek } 56857ef01d19Sgjelinek 56867ef01d19Sgjelinek if (poolid_set) { 56877ef01d19Sgjelinek if (ps.pr_poolid != last_poolid) 56887ef01d19Sgjelinek mixed = B_TRUE; 56897ef01d19Sgjelinek } else { 56907ef01d19Sgjelinek last_poolid = ps.pr_poolid; 56917ef01d19Sgjelinek poolid_set = B_TRUE; 56927ef01d19Sgjelinek } 56937ef01d19Sgjelinek } 56947ef01d19Sgjelinek 56957ef01d19Sgjelinek (void) close(procfd); 56967ef01d19Sgjelinek 56977ef01d19Sgjelinek if (mixed) 56987ef01d19Sgjelinek break; 56997ef01d19Sgjelinek } 57007ef01d19Sgjelinek 57017ef01d19Sgjelinek (void) closedir(dirp); 57027ef01d19Sgjelinek 57037ef01d19Sgjelinek return (mixed); 57047ef01d19Sgjelinek } 57057ef01d19Sgjelinek 57067ef01d19Sgjelinek /* 57077ef01d19Sgjelinek * Check if a persistent or temporary pool is configured for the zone. 57087ef01d19Sgjelinek * This is currently only going to be called for the global zone from 57097ef01d19Sgjelinek * apply_func but that could be generalized in the future. 57107ef01d19Sgjelinek */ 57117ef01d19Sgjelinek static boolean_t 57127ef01d19Sgjelinek pool_configured(zone_dochandle_t handle) 57137ef01d19Sgjelinek { 57147ef01d19Sgjelinek int err1, err2; 57157ef01d19Sgjelinek struct zone_psettab pset_tab; 57167ef01d19Sgjelinek char poolname[MAXPATHLEN]; 57177ef01d19Sgjelinek 57187ef01d19Sgjelinek err1 = zonecfg_lookup_pset(handle, &pset_tab); 57197ef01d19Sgjelinek err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname)); 57207ef01d19Sgjelinek 57217ef01d19Sgjelinek if (err1 == Z_NO_ENTRY && 57227ef01d19Sgjelinek (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0))) 57237ef01d19Sgjelinek return (B_FALSE); 57247ef01d19Sgjelinek 57257ef01d19Sgjelinek return (B_TRUE); 57267ef01d19Sgjelinek } 57277ef01d19Sgjelinek 57287ef01d19Sgjelinek /* 57290209230bSgjelinek * This is an undocumented interface which is currently only used to apply 57300209230bSgjelinek * the global zone resource management settings when the system boots. 57310209230bSgjelinek * This function does not yet properly handle updating a running system so 57320209230bSgjelinek * any projects running in the zone would be trashed if this function 57330209230bSgjelinek * were to run after the zone had booted. It also does not reset any 57340209230bSgjelinek * rctl settings that were removed from zonecfg. There is still work to be 57350209230bSgjelinek * done before we can properly support dynamically updating the resource 57360209230bSgjelinek * management settings for a running zone (global or non-global). Thus, this 57370209230bSgjelinek * functionality is undocumented for now. 57380209230bSgjelinek */ 57390209230bSgjelinek /* ARGSUSED */ 57400209230bSgjelinek static int 57410209230bSgjelinek apply_func(int argc, char *argv[]) 57420209230bSgjelinek { 57430209230bSgjelinek int err; 57440209230bSgjelinek int res = Z_OK; 57450209230bSgjelinek priv_set_t *privset; 57460209230bSgjelinek zoneid_t zoneid; 57470209230bSgjelinek zone_dochandle_t handle; 57480209230bSgjelinek struct zone_mcaptab mcap; 57490209230bSgjelinek char pool_err[128]; 57500209230bSgjelinek 57510209230bSgjelinek zoneid = getzoneid(); 57520209230bSgjelinek 57530209230bSgjelinek if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID || 57540209230bSgjelinek target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0) 57550209230bSgjelinek return (usage(B_FALSE)); 57560209230bSgjelinek 57570209230bSgjelinek if ((privset = priv_allocset()) == NULL) { 57580209230bSgjelinek zerror(gettext("%s failed"), "priv_allocset"); 57590209230bSgjelinek return (Z_ERR); 57600209230bSgjelinek } 57610209230bSgjelinek 57620209230bSgjelinek if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 57630209230bSgjelinek zerror(gettext("%s failed"), "getppriv"); 57640209230bSgjelinek priv_freeset(privset); 57650209230bSgjelinek return (Z_ERR); 57660209230bSgjelinek } 57670209230bSgjelinek 57680209230bSgjelinek if (priv_isfullset(privset) == B_FALSE) { 57690209230bSgjelinek (void) usage(B_FALSE); 57700209230bSgjelinek priv_freeset(privset); 57710209230bSgjelinek return (Z_ERR); 57720209230bSgjelinek } 57730209230bSgjelinek priv_freeset(privset); 57740209230bSgjelinek 57750209230bSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 57760209230bSgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 57770209230bSgjelinek return (Z_ERR); 57780209230bSgjelinek } 57790209230bSgjelinek 57800209230bSgjelinek if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) { 57810209230bSgjelinek errno = err; 57820209230bSgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 57830209230bSgjelinek zonecfg_fini_handle(handle); 57840209230bSgjelinek return (Z_ERR); 57850209230bSgjelinek } 57860209230bSgjelinek 57870209230bSgjelinek /* specific error msgs are printed within apply_rctls */ 57880209230bSgjelinek if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) { 57890209230bSgjelinek errno = err; 57900209230bSgjelinek zperror(cmd_to_str(CMD_APPLY), B_TRUE); 57910209230bSgjelinek res = Z_ERR; 57920209230bSgjelinek } 57930209230bSgjelinek 57940209230bSgjelinek if ((err = check_cpu_shares_sched(handle)) != Z_OK) 57950209230bSgjelinek res = Z_ERR; 57960209230bSgjelinek 57977ef01d19Sgjelinek if (pool_configured(handle)) { 57987ef01d19Sgjelinek if (mixed_pools(zoneid)) { 57997ef01d19Sgjelinek zerror(gettext("Zone is using multiple resource " 58007ef01d19Sgjelinek "pools. The pool\nconfiguration cannot be " 58017ef01d19Sgjelinek "applied without rebooting.")); 58027ef01d19Sgjelinek res = Z_ERR; 58037ef01d19Sgjelinek } else { 58047ef01d19Sgjelinek 58050209230bSgjelinek /* 58067ef01d19Sgjelinek * The next two blocks of code attempt to set up 58077ef01d19Sgjelinek * temporary pools as well as persistent pools. In 58087ef01d19Sgjelinek * both cases we call the functions unconditionally. 58097ef01d19Sgjelinek * Within each funtion the code will check if the zone 58107ef01d19Sgjelinek * is actually configured for a temporary pool or 58117ef01d19Sgjelinek * persistent pool and just return if there is nothing 58127ef01d19Sgjelinek * to do. 58130209230bSgjelinek */ 58147ef01d19Sgjelinek if ((err = zonecfg_bind_tmp_pool(handle, zoneid, 58157ef01d19Sgjelinek pool_err, sizeof (pool_err))) != Z_OK) { 58167ef01d19Sgjelinek if (err == Z_POOL || err == Z_POOL_CREATE || 58177ef01d19Sgjelinek err == Z_POOL_BIND) 58187ef01d19Sgjelinek zerror("%s: %s", zonecfg_strerror(err), 58197ef01d19Sgjelinek pool_err); 58200209230bSgjelinek else 58217ef01d19Sgjelinek zerror(gettext("could not bind zone to " 58227ef01d19Sgjelinek "temporary pool: %s"), 58237ef01d19Sgjelinek zonecfg_strerror(err)); 58240209230bSgjelinek res = Z_ERR; 58250209230bSgjelinek } 58260209230bSgjelinek 58270209230bSgjelinek if ((err = zonecfg_bind_pool(handle, zoneid, pool_err, 58280209230bSgjelinek sizeof (pool_err))) != Z_OK) { 58290209230bSgjelinek if (err == Z_POOL || err == Z_POOL_BIND) 58307ef01d19Sgjelinek zerror("%s: %s", zonecfg_strerror(err), 58317ef01d19Sgjelinek pool_err); 58320209230bSgjelinek else 58330209230bSgjelinek zerror("%s", zonecfg_strerror(err)); 58340209230bSgjelinek } 58357ef01d19Sgjelinek } 58367ef01d19Sgjelinek } 58370209230bSgjelinek 58380209230bSgjelinek /* 58390209230bSgjelinek * If a memory cap is configured, set the cap in the kernel using 58400209230bSgjelinek * zone_setattr() and make sure the rcapd SMF service is enabled. 58410209230bSgjelinek */ 58420209230bSgjelinek if (zonecfg_getmcapent(handle, &mcap) == Z_OK) { 58430209230bSgjelinek uint64_t num; 58440209230bSgjelinek char smf_err[128]; 58450209230bSgjelinek 58460209230bSgjelinek num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10); 58470209230bSgjelinek if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) { 58480209230bSgjelinek zerror(gettext("could not set zone memory cap")); 58490209230bSgjelinek res = Z_ERR; 58500209230bSgjelinek } 58510209230bSgjelinek 58520209230bSgjelinek if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) { 58530209230bSgjelinek zerror(gettext("enabling system/rcap service failed: " 58540209230bSgjelinek "%s"), smf_err); 58550209230bSgjelinek res = Z_ERR; 58560209230bSgjelinek } 58570209230bSgjelinek } 58580209230bSgjelinek 58590209230bSgjelinek zonecfg_fini_handle(handle); 58600209230bSgjelinek 58610209230bSgjelinek return (res); 58620209230bSgjelinek } 58630209230bSgjelinek 5864555afedfScarlsonj static int 58657c478bd9Sstevel@tonic-gate help_func(int argc, char *argv[]) 58667c478bd9Sstevel@tonic-gate { 58677c478bd9Sstevel@tonic-gate int arg, cmd_num; 58687c478bd9Sstevel@tonic-gate 58697c478bd9Sstevel@tonic-gate if (argc == 0) { 58707c478bd9Sstevel@tonic-gate (void) usage(B_TRUE); 58717c478bd9Sstevel@tonic-gate return (Z_OK); 58727c478bd9Sstevel@tonic-gate } 58737c478bd9Sstevel@tonic-gate optind = 0; 58747c478bd9Sstevel@tonic-gate if ((arg = getopt(argc, argv, "?")) != EOF) { 58757c478bd9Sstevel@tonic-gate switch (arg) { 58767c478bd9Sstevel@tonic-gate case '?': 58777c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 58787c478bd9Sstevel@tonic-gate return (optopt == '?' ? Z_OK : Z_USAGE); 58797c478bd9Sstevel@tonic-gate default: 58807c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 58817c478bd9Sstevel@tonic-gate return (Z_USAGE); 58827c478bd9Sstevel@tonic-gate } 58837c478bd9Sstevel@tonic-gate } 58847c478bd9Sstevel@tonic-gate while (optind < argc) { 5885394a25e2Scarlsonj /* Private commands have NULL short_usage; omit them */ 5886394a25e2Scarlsonj if ((cmd_num = cmd_match(argv[optind])) < 0 || 5887394a25e2Scarlsonj cmdtab[cmd_num].short_usage == NULL) { 58887c478bd9Sstevel@tonic-gate sub_usage(SHELP_HELP, CMD_HELP); 58897c478bd9Sstevel@tonic-gate return (Z_USAGE); 58907c478bd9Sstevel@tonic-gate } 58917c478bd9Sstevel@tonic-gate sub_usage(cmdtab[cmd_num].short_usage, cmd_num); 58927c478bd9Sstevel@tonic-gate optind++; 58937c478bd9Sstevel@tonic-gate } 58947c478bd9Sstevel@tonic-gate return (Z_OK); 58957c478bd9Sstevel@tonic-gate } 58967c478bd9Sstevel@tonic-gate 58977c478bd9Sstevel@tonic-gate /* 58987c478bd9Sstevel@tonic-gate * Returns: CMD_MIN thru CMD_MAX on success, -1 on error 58997c478bd9Sstevel@tonic-gate */ 59007c478bd9Sstevel@tonic-gate 59017c478bd9Sstevel@tonic-gate static int 59027c478bd9Sstevel@tonic-gate cmd_match(char *cmd) 59037c478bd9Sstevel@tonic-gate { 59047c478bd9Sstevel@tonic-gate int i; 59057c478bd9Sstevel@tonic-gate 59067c478bd9Sstevel@tonic-gate for (i = CMD_MIN; i <= CMD_MAX; i++) { 59077c478bd9Sstevel@tonic-gate /* return only if there is an exact match */ 59087c478bd9Sstevel@tonic-gate if (strcmp(cmd, cmdtab[i].cmd_name) == 0) 59097c478bd9Sstevel@tonic-gate return (cmdtab[i].cmd_num); 59107c478bd9Sstevel@tonic-gate } 59117c478bd9Sstevel@tonic-gate return (-1); 59127c478bd9Sstevel@tonic-gate } 59137c478bd9Sstevel@tonic-gate 59147c478bd9Sstevel@tonic-gate static int 59157c478bd9Sstevel@tonic-gate parse_and_run(int argc, char *argv[]) 59167c478bd9Sstevel@tonic-gate { 59177c478bd9Sstevel@tonic-gate int i = cmd_match(argv[0]); 59187c478bd9Sstevel@tonic-gate 59197c478bd9Sstevel@tonic-gate if (i < 0) 59207c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 59217c478bd9Sstevel@tonic-gate return (cmdtab[i].handler(argc - 1, &(argv[1]))); 59227c478bd9Sstevel@tonic-gate } 59237c478bd9Sstevel@tonic-gate 59247c478bd9Sstevel@tonic-gate static char * 59257c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname) 59267c478bd9Sstevel@tonic-gate { 59277c478bd9Sstevel@tonic-gate char *last_slash, *execbasename; 59287c478bd9Sstevel@tonic-gate 59297c478bd9Sstevel@tonic-gate /* guard against '/' at end of command invocation */ 59307c478bd9Sstevel@tonic-gate for (;;) { 59317c478bd9Sstevel@tonic-gate last_slash = strrchr(execfullname, '/'); 59327c478bd9Sstevel@tonic-gate if (last_slash == NULL) { 59337c478bd9Sstevel@tonic-gate execbasename = execfullname; 59347c478bd9Sstevel@tonic-gate break; 59357c478bd9Sstevel@tonic-gate } else { 59367c478bd9Sstevel@tonic-gate execbasename = last_slash + 1; 59377c478bd9Sstevel@tonic-gate if (*execbasename == '\0') { 59387c478bd9Sstevel@tonic-gate *last_slash = '\0'; 59397c478bd9Sstevel@tonic-gate continue; 59407c478bd9Sstevel@tonic-gate } 59417c478bd9Sstevel@tonic-gate break; 59427c478bd9Sstevel@tonic-gate } 59437c478bd9Sstevel@tonic-gate } 59447c478bd9Sstevel@tonic-gate return (execbasename); 59457c478bd9Sstevel@tonic-gate } 59467c478bd9Sstevel@tonic-gate 59477c478bd9Sstevel@tonic-gate int 59487c478bd9Sstevel@tonic-gate main(int argc, char **argv) 59497c478bd9Sstevel@tonic-gate { 59507c478bd9Sstevel@tonic-gate int arg; 59517c478bd9Sstevel@tonic-gate zoneid_t zid; 5952108322fbScarlsonj struct stat st; 59539acbbeafSnn35248 char *zone_lock_env; 59549acbbeafSnn35248 int err; 59557c478bd9Sstevel@tonic-gate 59567c478bd9Sstevel@tonic-gate if ((locale = setlocale(LC_ALL, "")) == NULL) 59577c478bd9Sstevel@tonic-gate locale = "C"; 59587c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 59597c478bd9Sstevel@tonic-gate setbuf(stdout, NULL); 59607c478bd9Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_IGN); 59617c478bd9Sstevel@tonic-gate execname = get_execbasename(argv[0]); 59627c478bd9Sstevel@tonic-gate target_zone = NULL; 59637c478bd9Sstevel@tonic-gate if (chdir("/") != 0) { 59647c478bd9Sstevel@tonic-gate zerror(gettext("could not change directory to /.")); 59657c478bd9Sstevel@tonic-gate exit(Z_ERR); 59667c478bd9Sstevel@tonic-gate } 59677c478bd9Sstevel@tonic-gate 596899653d4eSeschrock if (init_zfs() != Z_OK) 596999653d4eSeschrock exit(Z_ERR); 597099653d4eSeschrock 5971555afedfScarlsonj while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) { 59727c478bd9Sstevel@tonic-gate switch (arg) { 59737c478bd9Sstevel@tonic-gate case '?': 59747c478bd9Sstevel@tonic-gate return (usage(B_TRUE)); 5975555afedfScarlsonj case 'u': 5976555afedfScarlsonj target_uuid = optarg; 5977555afedfScarlsonj break; 59787c478bd9Sstevel@tonic-gate case 'z': 59797c478bd9Sstevel@tonic-gate target_zone = optarg; 59807c478bd9Sstevel@tonic-gate break; 5981108322fbScarlsonj case 'R': /* private option for admin/install use */ 5982108322fbScarlsonj if (*optarg != '/') { 5983108322fbScarlsonj zerror(gettext("root path must be absolute.")); 5984108322fbScarlsonj exit(Z_ERR); 5985108322fbScarlsonj } 5986108322fbScarlsonj if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) { 5987108322fbScarlsonj zerror( 5988108322fbScarlsonj gettext("root path must be a directory.")); 5989108322fbScarlsonj exit(Z_ERR); 5990108322fbScarlsonj } 5991108322fbScarlsonj zonecfg_set_root(optarg); 5992108322fbScarlsonj break; 59937c478bd9Sstevel@tonic-gate default: 59947c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 59957c478bd9Sstevel@tonic-gate } 59967c478bd9Sstevel@tonic-gate } 59977c478bd9Sstevel@tonic-gate 59987c478bd9Sstevel@tonic-gate if (optind >= argc) 59997c478bd9Sstevel@tonic-gate return (usage(B_FALSE)); 6000555afedfScarlsonj 6001555afedfScarlsonj if (target_uuid != NULL && *target_uuid != '\0') { 6002555afedfScarlsonj uuid_t uuid; 6003555afedfScarlsonj static char newtarget[ZONENAME_MAX]; 6004555afedfScarlsonj 6005555afedfScarlsonj if (uuid_parse(target_uuid, uuid) == -1) { 6006555afedfScarlsonj zerror(gettext("illegal UUID value specified")); 6007555afedfScarlsonj exit(Z_ERR); 6008555afedfScarlsonj } 6009555afedfScarlsonj if (zonecfg_get_name_by_uuid(uuid, newtarget, 6010555afedfScarlsonj sizeof (newtarget)) == Z_OK) 6011555afedfScarlsonj target_zone = newtarget; 6012555afedfScarlsonj } 6013555afedfScarlsonj 60147c478bd9Sstevel@tonic-gate if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) { 60157c478bd9Sstevel@tonic-gate errno = Z_NO_ZONE; 60167c478bd9Sstevel@tonic-gate zperror(target_zone, B_TRUE); 60177c478bd9Sstevel@tonic-gate exit(Z_ERR); 60187c478bd9Sstevel@tonic-gate } 60199acbbeafSnn35248 60209acbbeafSnn35248 /* 60219acbbeafSnn35248 * See if we have inherited the right to manipulate this zone from 60229acbbeafSnn35248 * a zoneadm instance in our ancestry. If so, set zone_lock_cnt to 60239acbbeafSnn35248 * indicate it. If not, make that explicit in our environment. 60249acbbeafSnn35248 */ 60259acbbeafSnn35248 zone_lock_env = getenv(LOCK_ENV_VAR); 60269acbbeafSnn35248 if (zone_lock_env == NULL) { 60279acbbeafSnn35248 if (putenv(zoneadm_lock_not_held) != 0) { 60289acbbeafSnn35248 zperror(target_zone, B_TRUE); 60299acbbeafSnn35248 exit(Z_ERR); 60309acbbeafSnn35248 } 60319acbbeafSnn35248 } else { 60329acbbeafSnn35248 zoneadm_is_nested = B_TRUE; 60339acbbeafSnn35248 if (atoi(zone_lock_env) == 1) 60349acbbeafSnn35248 zone_lock_cnt = 1; 60359acbbeafSnn35248 } 60369acbbeafSnn35248 60379acbbeafSnn35248 /* 60389acbbeafSnn35248 * If we are going to be operating on a single zone, retrieve its 60399acbbeafSnn35248 * brand type and determine whether it is native or not. 60409acbbeafSnn35248 */ 60419acbbeafSnn35248 if ((target_zone != NULL) && 60429acbbeafSnn35248 (strcmp(target_zone, GLOBAL_ZONENAME) != NULL)) { 60439acbbeafSnn35248 if (zone_get_brand(target_zone, target_brand, 60449acbbeafSnn35248 sizeof (target_brand)) != Z_OK) { 60459acbbeafSnn35248 zerror(gettext("missing or invalid brand")); 60469acbbeafSnn35248 exit(Z_ERR); 60479acbbeafSnn35248 } 60489acbbeafSnn35248 is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0); 604984561e8cStd153743 is_cluster_zone = 605084561e8cStd153743 (strcmp(target_brand, CLUSTER_BRAND_NAME) == 0); 60519acbbeafSnn35248 } 60529acbbeafSnn35248 60539acbbeafSnn35248 err = parse_and_run(argc - optind, &argv[optind]); 60549acbbeafSnn35248 60559acbbeafSnn35248 return (err); 60567c478bd9Sstevel@tonic-gate } 6057