4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2015 by Delphix. All rights reserved.
26 * Copyright (c) 2015 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
30 * zoneadm is a command interpreter for zone administration. It is all in
31 * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
32 * main() calls parse_and_run() which calls cmd_match(), then invokes the
33 * appropriate command's handler function. The rest of the program is the
34 * handler functions and their helper functions.
36 * Some of the helper functions are used largely to simplify I18N: reducing
37 * the need for translation notes. This is particularly true of many of
38 * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
39 * than zerror(gettext("foo failed")) with a translation note indicating
40 * that "foo" need not be translated.
56 #include <libzonecfg.h>
58 #include <sys/brand.h>
59 #include <sys/param.h>
60 #include <sys/types.h>
62 #include <sys/statvfs.h>
64 #include <sys/sockio.h>
65 #include <sys/mntent.h>
68 #include <uuid/uuid.h>
74 #include <sys/modctl.h>
81 #include <sys/priocntl.h>
82 #include <sys/fsspriocntl.h>
84 #include <libdllink.h>
86 #include <auth_list.h>
87 #include <auth_attr.h>
93 #define SOURCE_ZONE (CMD_MAX + 1)
95 /* Reflects kernel zone entries */
96 typedef struct zone_entry
{
98 char zname
[ZONENAME_MAX
];
100 zone_state_t zstate_num
;
101 char zbrand
[MAXNAMELEN
];
102 char zroot
[MAXPATHLEN
];
103 char zuuid
[UUID_PRINTABLE_STRING_LENGTH
];
104 zone_iptype_t ziptype
;
107 #define CLUSTER_BRAND_NAME "cluster"
109 static zone_entry_t
*zents
;
110 static size_t nzents
;
112 #define LOOPBACK_IF "lo0"
113 #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af))
120 /* 0755 is the default directory mode. */
121 #define DEFAULT_DIR_MODE \
122 (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
125 uint_t cmd_num
; /* command number */
126 char *cmd_name
; /* command name */
127 char *short_usage
; /* short form help */
128 int (*handler
)(int argc
, char *argv
[]); /* function to call */
132 #define SHELP_HELP "help"
133 #define SHELP_BOOT "boot [-- boot_arguments]"
134 #define SHELP_HALT "halt"
135 #define SHELP_READY "ready"
136 #define SHELP_SHUTDOWN "shutdown [-r [-- boot_arguments]]"
137 #define SHELP_REBOOT "reboot [-- boot_arguments]"
138 #define SHELP_LIST "list [-cipv]"
139 #define SHELP_VERIFY "verify"
140 #define SHELP_INSTALL "install [brand-specific args]"
141 #define SHELP_UNINSTALL "uninstall [-F] [brand-specific args]"
142 #define SHELP_CLONE "clone [-m method] [-s <ZFS snapshot>] "\
143 "[brand-specific args] zonename"
144 #define SHELP_MOVE "move zonepath"
145 #define SHELP_DETACH "detach [-n] [brand-specific args]"
146 #define SHELP_ATTACH "attach [-F] [-n <path>] [brand-specific args]"
147 #define SHELP_MARK "mark incomplete"
149 #define EXEC_PREFIX "exec "
150 #define EXEC_LEN (strlen(EXEC_PREFIX))
151 #define RMCOMMAND "/usr/bin/rm -rf"
153 static int cleanup_zonepath(char *, boolean_t
);
156 static int help_func(int argc
, char *argv
[]);
157 static int ready_func(int argc
, char *argv
[]);
158 static int boot_func(int argc
, char *argv
[]);
159 static int shutdown_func(int argc
, char *argv
[]);
160 static int halt_func(int argc
, char *argv
[]);
161 static int reboot_func(int argc
, char *argv
[]);
162 static int list_func(int argc
, char *argv
[]);
163 static int verify_func(int argc
, char *argv
[]);
164 static int install_func(int argc
, char *argv
[]);
165 static int uninstall_func(int argc
, char *argv
[]);
166 static int mount_func(int argc
, char *argv
[]);
167 static int unmount_func(int argc
, char *argv
[]);
168 static int clone_func(int argc
, char *argv
[]);
169 static int move_func(int argc
, char *argv
[]);
170 static int detach_func(int argc
, char *argv
[]);
171 static int attach_func(int argc
, char *argv
[]);
172 static int mark_func(int argc
, char *argv
[]);
173 static int apply_func(int argc
, char *argv
[]);
174 static int sysboot_func(int argc
, char *argv
[]);
175 static int sanity_check(char *zone
, int cmd_num
, boolean_t running
,
176 boolean_t unsafe_when_running
, boolean_t force
);
177 static int cmd_match(char *cmd
);
178 static int verify_details(int, char *argv
[]);
179 static int verify_brand(zone_dochandle_t
, int, char *argv
[]);
180 static int invoke_brand_handler(int, char *argv
[]);
182 static struct cmd cmdtab
[] = {
183 { CMD_HELP
, "help", SHELP_HELP
, help_func
},
184 { CMD_BOOT
, "boot", SHELP_BOOT
, boot_func
},
185 { CMD_HALT
, "halt", SHELP_HALT
, halt_func
},
186 { CMD_READY
, "ready", SHELP_READY
, ready_func
},
187 { CMD_SHUTDOWN
, "shutdown", SHELP_SHUTDOWN
, shutdown_func
},
188 { CMD_REBOOT
, "reboot", SHELP_REBOOT
, reboot_func
},
189 { CMD_LIST
, "list", SHELP_LIST
, list_func
},
190 { CMD_VERIFY
, "verify", SHELP_VERIFY
, verify_func
},
191 { CMD_INSTALL
, "install", SHELP_INSTALL
, install_func
},
192 { CMD_UNINSTALL
, "uninstall", SHELP_UNINSTALL
,
194 /* mount and unmount are private commands for admin/install */
195 { CMD_MOUNT
, "mount", NULL
, mount_func
},
196 { CMD_UNMOUNT
, "umount", NULL
, unmount_func
},
197 { CMD_UNMOUNT
, "unmount", NULL
, unmount_func
},
198 { CMD_CLONE
, "clone", SHELP_CLONE
, clone_func
},
199 { CMD_MOVE
, "move", SHELP_MOVE
, move_func
},
200 { CMD_DETACH
, "detach", SHELP_DETACH
, detach_func
},
201 { CMD_ATTACH
, "attach", SHELP_ATTACH
, attach_func
},
202 { CMD_MARK
, "mark", SHELP_MARK
, mark_func
},
203 { CMD_APPLY
, "apply", NULL
, apply_func
},
204 { CMD_SYSBOOT
, "sysboot", NULL
, sysboot_func
}
207 /* global variables */
209 /* set early in main(), never modified thereafter, used all over the place */
210 static char *execname
;
211 static char target_brand
[MAXNAMELEN
];
212 static char default_brand
[MAXPATHLEN
];
215 static char *target_uuid
;
219 cmd_to_str(int cmd_num
)
221 assert(cmd_num
>= CMD_MIN
&& cmd_num
<= CMD_MAX
);
222 return (cmdtab
[cmd_num
].cmd_name
);
225 /* This is a separate function because of gettext() wrapping. */
227 long_help(int cmd_num
)
229 assert(cmd_num
>= CMD_MIN
&& cmd_num
<= CMD_MAX
);
232 return (gettext("Print usage message."));
234 return (gettext("Activates (boots) specified zone. See "
235 "zoneadm(1m) for valid boot\n\targuments."));
237 return (gettext("Halts specified zone, bypassing shutdown "
238 "scripts and removing runtime\n\tresources of the zone."));
240 return (gettext("Prepares a zone for running applications but "
241 "does not start any user\n\tprocesses in the zone."));
243 return (gettext("Gracefully shutdown the zone or reboot if "
244 "the '-r' option is specified.\n\t"
245 "See zoneadm(1m) for valid boot arguments."));
247 return (gettext("Restarts the zone (equivalent to a halt / "
248 "boot sequence).\n\tFails if the zone is not active. "
249 "See zoneadm(1m) for valid boot\n\targuments."));
251 return (gettext("Lists the current zones, or a "
252 "specific zone if indicated. By default,\n\tall "
253 "running zones are listed, though this can be "
254 "expanded to all\n\tinstalled zones with the -i "
255 "option or all configured zones with the\n\t-c "
256 "option. When used with the general -z <zone> and/or -u "
257 "<uuid-match>\n\toptions, lists only the specified "
258 "matching zone, but lists it\n\tregardless of its state, "
259 "and the -i and -c options are disallowed. The\n\t-v "
260 "option can be used to display verbose information: zone "
261 "name, id,\n\tcurrent state, root directory and options. "
262 "The -p option can be used\n\tto request machine-parsable "
263 "output. The -v and -p options are mutually\n\texclusive."
264 " If neither -v nor -p is used, just the zone name is "
267 return (gettext("Check to make sure the configuration "
268 "can safely be instantiated\n\ton the machine: "
269 "physical network interfaces exist, etc."));
271 return (gettext("Install the configuration on to the system. "
272 "All arguments are passed to the brand installation "
273 "function;\n\tsee brands(5) for more information."));
275 return (gettext("Uninstall the configuration from the system. "
276 "The -F flag can be used\n\tto force the action. All "
277 "other arguments are passed to the brand\n\tuninstall "
278 "function; see brands(5) for more information."));
280 return (gettext("Clone the installation of another zone. "
281 "The -m option can be used to\n\tspecify 'copy' which "
282 "forces a copy of the source zone. The -s option\n\t"
283 "can be used to specify the name of a ZFS snapshot "
284 "that was taken from\n\ta previous clone command. The "
285 "snapshot will be used as the source\n\tinstead of "
286 "creating a new ZFS snapshot. All other arguments are "
287 "passed\n\tto the brand clone function; see "
288 "brands(5) for more information."));
290 return (gettext("Move the zone to a new zonepath."));
292 return (gettext("Detach the zone from the system. The zone "
293 "state is changed to\n\t'configured' (but the files under "
294 "the zonepath are untouched).\n\tThe zone can subsequently "
295 "be attached, or can be moved to another\n\tsystem and "
296 "attached there. The -n option can be used to specify\n\t"
297 "'no-execute' mode. When -n is used, the information "
298 "needed to attach\n\tthe zone is sent to standard output "
299 "but the zone is not actually\n\tdetached. All other "
300 "arguments are passed to the brand detach function;\n\tsee "
301 "brands(5) for more information."));
303 return (gettext("Attach the zone to the system. The zone "
304 "state must be 'configured'\n\tprior to attach; upon "
305 "successful completion, the zone state will be\n\t"
306 "'installed'. The system software on the current "
307 "system must be\n\tcompatible with the software on the "
308 "zone's original system.\n\tSpecify -F "
309 "to force the attach and skip software compatibility "
310 "tests.\n\tThe -n option can be used to specify "
311 "'no-execute' mode. When -n is\n\tused, the information "
312 "needed to attach the zone is read from the\n\tspecified "
313 "path and the configuration is only validated. The path "
314 "can\n\tbe '-' to specify standard input. The -F and -n "
315 "options are mutually\n\texclusive. All other arguments "
316 "are passed to the brand attach\n\tfunction; see "
317 "brands(5) for more information."));
319 return (gettext("Set the state of the zone. This can be used "
320 "to force the zone\n\tstate to 'incomplete' "
321 "administratively if some activity has rendered\n\tthe "
322 "zone permanently unusable. The only valid state that "
323 "may be\n\tspecified is 'incomplete'."));
332 * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
337 usage(boolean_t
explicit)
340 FILE *fd
= explicit ? stdout
: stderr
;
342 (void) fprintf(fd
, "%s:\t%s help\n", gettext("usage"), execname
);
343 (void) fprintf(fd
, "\t%s [-z <zone>] [-u <uuid-match>] list\n",
345 (void) fprintf(fd
, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname
,
346 gettext("subcommand"));
347 (void) fprintf(fd
, "\n%s:\n\n", gettext("Subcommands"));
348 for (i
= CMD_MIN
; i
<= CMD_MAX
; i
++) {
349 if (cmdtab
[i
].short_usage
== NULL
)
351 (void) fprintf(fd
, "%s\n", cmdtab
[i
].short_usage
);
353 (void) fprintf(fd
, "\t%s\n\n", long_help(i
));
356 (void) fputs("\n", fd
);
361 sub_usage(char *short_usage
, int cmd_num
)
363 (void) fprintf(stderr
, "%s:\t%s\n", gettext("usage"), short_usage
);
364 (void) fprintf(stderr
, "\t%s\n", long_help(cmd_num
));
368 * zperror() is like perror(3c) except that this also prints the executable
369 * name at the start of the message, and takes a boolean indicating whether
370 * to call libc'c strerror() or that from libzonecfg.
374 zperror(const char *str
, boolean_t zonecfg_error
)
376 (void) fprintf(stderr
, "%s: %s: %s\n", execname
, str
,
377 zonecfg_error
? zonecfg_strerror(errno
) : strerror(errno
));
381 * zperror2() is very similar to zperror() above, except it also prints a
382 * supplied zone name after the executable.
384 * All current consumers of this function want libzonecfg's strerror() rather
385 * than libc's; if this ever changes, this function can be made more generic
386 * like zperror() above.
390 zperror2(const char *zone
, const char *str
)
392 (void) fprintf(stderr
, "%s: %s: %s: %s\n", execname
, zone
, str
,
393 zonecfg_strerror(errno
));
398 zerror(const char *fmt
, ...)
402 va_start(alist
, fmt
);
403 (void) fprintf(stderr
, "%s: ", execname
);
404 if (target_zone
!= NULL
)
405 (void) fprintf(stderr
, "zone '%s': ", target_zone
);
406 (void) vfprintf(stderr
, fmt
, alist
);
407 (void) fprintf(stderr
, "\n");
412 safe_calloc(size_t nelem
, size_t elsize
)
414 void *r
= calloc(nelem
, elsize
);
417 zerror(gettext("failed to allocate %lu bytes: %s"),
418 (ulong_t
)nelem
* elsize
, strerror(errno
));
425 zone_print(zone_entry_t
*zent
, boolean_t verbose
, boolean_t parsable
)
427 static boolean_t firsttime
= B_TRUE
;
430 /* Skip a zone that shutdown while we were collecting data. */
431 if (zent
->zname
[0] == '\0')
434 if (zent
->ziptype
== ZS_EXCLUSIVE
)
435 ip_type_str
= "excl";
437 ip_type_str
= "shared";
439 assert(!(verbose
&& parsable
));
440 if (firsttime
&& verbose
) {
442 (void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n",
443 ZONEID_WIDTH
, "ID", "NAME", "STATUS", "PATH", "BRAND",
450 (void) printf("%s\n", zent
->zname
);
453 if (zent
->zid
== ZONE_ID_UNDEFINED
)
456 (void) printf("%lu", zent
->zid
);
457 (void) printf(":%s:%s:", zent
->zname
, zent
->zstate_str
);
459 while ((clim
= strchr(cp
, ':')) != NULL
) {
460 (void) printf("%.*s\\:", clim
- cp
, cp
);
463 (void) printf("%s:%s:%s:%s\n", cp
, zent
->zuuid
, zent
->zbrand
,
467 if (zent
->zstate_str
!= NULL
) {
468 if (zent
->zid
== ZONE_ID_UNDEFINED
)
469 (void) printf("%*s", ZONEID_WIDTH
, "-");
471 (void) printf("%*lu", ZONEID_WIDTH
, zent
->zid
);
472 (void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent
->zname
,
473 zent
->zstate_str
, zent
->zroot
, zent
->zbrand
, ip_type_str
);
478 lookup_zone_info(const char *zone_name
, zoneid_t zid
, zone_entry_t
*zent
)
480 char root
[MAXPATHLEN
];
483 zone_dochandle_t handle
;
485 (void) strlcpy(zent
->zname
, zone_name
, sizeof (zent
->zname
));
486 (void) strlcpy(zent
->zroot
, "???", sizeof (zent
->zroot
));
487 (void) strlcpy(zent
->zbrand
, "???", sizeof (zent
->zbrand
));
488 zent
->zstate_str
= "???";
492 if (zonecfg_get_uuid(zone_name
, uuid
) == Z_OK
&&
494 uuid_unparse(uuid
, zent
->zuuid
);
496 zent
->zuuid
[0] = '\0';
498 if ((err
= zone_get_zonepath(zent
->zname
, root
,
499 sizeof (root
))) != Z_OK
) {
501 zperror2(zent
->zname
,
502 gettext("could not get zone path."));
505 (void) strlcpy(zent
->zroot
, root
, sizeof (zent
->zroot
));
507 if ((err
= zone_get_state(zent
->zname
, &zent
->zstate_num
)) != Z_OK
) {
509 zperror2(zent
->zname
, gettext("could not get state"));
512 zent
->zstate_str
= zone_state_str(zent
->zstate_num
);
514 if (zone_get_brand(zent
->zname
, zent
->zbrand
,
515 sizeof (zent
->zbrand
)) != Z_OK
) {
516 zperror2(zent
->zname
, gettext("could not get brand name"));
521 * Get ip type of the zone.
522 * Note for global zone, ZS_SHARED is set always.
524 if (zid
== GLOBAL_ZONEID
) {
525 zent
->ziptype
= ZS_SHARED
;
530 * There is a race condition where the zone could boot while
531 * we're walking the index file. In this case the zone state
532 * could be seen as running from the call above, but the zoneid
533 * would be undefined.
535 * There is also a race condition where the zone could shutdown after
536 * we got its running state above. This is also not an error and
537 * we fall back to getting the ziptype from the zone configuration.
539 if (zent
->zstate_num
== ZONE_STATE_RUNNING
&&
540 zid
!= ZONE_ID_UNDEFINED
) {
543 if (zone_getattr(zid
, ZONE_ATTR_FLAGS
, &flags
,
544 sizeof (flags
)) >= 0) {
545 if (flags
& ZF_NET_EXCL
)
546 zent
->ziptype
= ZS_EXCLUSIVE
;
548 zent
->ziptype
= ZS_SHARED
;
553 if ((handle
= zonecfg_init_handle()) == NULL
) {
554 zperror2(zent
->zname
, gettext("could not init handle"));
557 if ((err
= zonecfg_get_handle(zent
->zname
, handle
)) != Z_OK
) {
558 zperror2(zent
->zname
, gettext("could not get handle"));
559 zonecfg_fini_handle(handle
);
563 if ((err
= zonecfg_get_iptype(handle
, &zent
->ziptype
)) != Z_OK
) {
564 zperror2(zent
->zname
, gettext("could not get ip-type"));
565 zonecfg_fini_handle(handle
);
568 zonecfg_fini_handle(handle
);
574 * fetch_zents() calls zone_list(2) to find out how many zones are running
575 * (which is stored in the global nzents), then calls zone_list(2) again
576 * to fetch the list of running zones (stored in the global zents). This
577 * function may be called multiple times, so if zents is already set, we
578 * return immediately to save work.
580 * Note that the data about running zones can change while this function
581 * is running, so its possible that the list of zones will have empty slots
588 zoneid_t
*zids
= NULL
;
599 if (zone_list(NULL
, &nzents
) != 0) {
600 zperror(gettext("failed to get zoneid list"), B_FALSE
);
608 zids
= safe_calloc(nzents
, sizeof (zoneid_t
));
609 nzents_saved
= nzents
;
611 if (zone_list(zids
, &nzents
) != 0) {
612 zperror(gettext("failed to get zone list"), B_FALSE
);
616 if (nzents
!= nzents_saved
) {
617 /* list changed, try again */
622 zents
= safe_calloc(nzents
, sizeof (zone_entry_t
));
624 inaltroot
= zonecfg_in_alt_root();
626 fp
= zonecfg_open_scratch("", B_FALSE
);
627 altroot
= zonecfg_get_root();
633 for (i
= 0; i
< nzents
; i
++) {
634 char name
[ZONENAME_MAX
];
635 char altname
[ZONENAME_MAX
];
636 char rev_altroot
[MAXPATHLEN
];
638 if (getzonenamebyid(zids
[i
], name
, sizeof (name
)) < 0) {
640 * There is a race condition where the zone may have
641 * shutdown since we retrieved the number of running
642 * zones above. This is not an error, there will be
643 * an empty slot at the end of the list.
647 if (zonecfg_is_scratch(name
)) {
648 /* Ignore scratch zones by default */
652 zonecfg_reverse_scratch(fp
, name
, altname
,
653 sizeof (altname
), rev_altroot
,
654 sizeof (rev_altroot
)) == -1) {
655 zerror(gettext("could not resolve scratch "
660 /* Ignore zones in other alternate roots */
661 if (strcmp(rev_altroot
, altroot
) != 0)
663 (void) strcpy(name
, altname
);
665 /* Ignore non-scratch when in an alternate root */
666 if (inaltroot
&& strcmp(name
, GLOBAL_ZONENAME
) != 0)
669 if (lookup_zone_info(name
, zids
[i
], zentp
) != Z_OK
) {
671 * There is a race condition where the zone may have
672 * shutdown since we retrieved the number of running
673 * zones above. This is not an error, there will be
674 * an empty slot at the end of the list.
680 nzents
= zentp
- zents
;
682 zonecfg_close_scratch(fp
);
689 zone_print_list(zone_state_t min_state
, boolean_t verbose
, boolean_t parsable
)
697 * First get the list of running zones from the kernel and print them.
698 * If that is all we need, then return.
700 if ((i
= fetch_zents()) != Z_OK
) {
702 * No need for error messages; fetch_zents() has already taken
707 for (i
= 0; i
< nzents
; i
++)
708 zone_print(&zents
[i
], verbose
, parsable
);
709 if (min_state
>= ZONE_STATE_RUNNING
)
712 * Next, get the full list of zones from the configuration, skipping
713 * any we have already printed.
715 cookie
= setzoneent();
716 while ((name
= getzoneent(cookie
)) != NULL
) {
717 for (i
= 0; i
< nzents
; i
++) {
718 if (strcmp(zents
[i
].zname
, name
) == 0)
725 if (lookup_zone_info(name
, ZONE_ID_UNDEFINED
, &zent
) != Z_OK
) {
730 if (zent
.zstate_num
>= min_state
)
731 zone_print(&zent
, verbose
, parsable
);
738 * Retrieve a zone entry by name. Returns NULL if no such zone exists.
740 static zone_entry_t
*
741 lookup_running_zone(const char *str
)
745 if (fetch_zents() != Z_OK
)
748 for (i
= 0; i
< nzents
; i
++) {
749 if (strcmp(str
, zents
[i
].zname
) == 0)
756 * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
757 * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect).
760 bad_mode_bit(mode_t mode
, mode_t bit
, boolean_t on
, char *file
)
764 assert(bit
== S_IRUSR
|| bit
== S_IWUSR
|| bit
== S_IXUSR
||
765 bit
== S_IRGRP
|| bit
== S_IWGRP
|| bit
== S_IXGRP
||
766 bit
== S_IROTH
|| bit
== S_IWOTH
|| bit
== S_IXOTH
);
769 * The strings below will be used as part of a larger message,
771 * (file name) must be (owner|group|world) (read|writ|execut)able
773 * (file name) must not be (owner|group|world) (read|writ|execut)able
777 str
= gettext("owner readable");
780 str
= gettext("owner writable");
783 str
= gettext("owner executable");
786 str
= gettext("group readable");
789 str
= gettext("group writable");
792 str
= gettext("group executable");
795 str
= gettext("world readable");
798 str
= gettext("world writable");
801 str
= gettext("world executable");
804 if ((mode
& bit
) == (on
? 0 : bit
)) {
807 * The first parameter below is a file name; the second
808 * is one of the "(owner|group|world) (read|writ|execut)able"
809 * strings from above.
812 * The code below could be simplified but not in a way
813 * that would easily translate to non-English locales.
816 (void) fprintf(stderr
, gettext("%s must be %s.\n"),
819 (void) fprintf(stderr
, gettext("%s must not be %s.\n"),
828 * We want to make sure that no zone has its zone path as a child node
829 * (in the directory sense) of any other. We do that by comparing this
830 * zone's path to the path of all other (non-global) zones. The comparison
831 * in each case is simple: add '/' to the end of the path, then do a
832 * strncmp() of the two paths, using the length of the shorter one.
836 crosscheck_zonepaths(char *path
)
838 char rpath
[MAXPATHLEN
]; /* resolved path */
839 char path_copy
[MAXPATHLEN
]; /* copy of original path */
840 char rpath_copy
[MAXPATHLEN
]; /* copy of original rpath */
845 cookie
= setzoneent();
846 while ((ze
= getzoneent_private(cookie
)) != NULL
) {
847 /* Skip zones which are not installed. */
848 if (ze
->zone_state
< ZONE_STATE_INSTALLED
) {
852 /* Skip the global zone and the current target zone. */
853 if (strcmp(ze
->zone_name
, GLOBAL_ZONENAME
) == 0 ||
854 strcmp(ze
->zone_name
, target_zone
) == 0) {
858 if (strlen(ze
->zone_path
) == 0) {
859 /* old index file without path, fall back */
860 if ((err
= zone_get_zonepath(ze
->zone_name
,
861 ze
->zone_path
, sizeof (ze
->zone_path
))) != Z_OK
) {
863 zperror2(ze
->zone_name
,
864 gettext("could not get zone path"));
869 (void) snprintf(path_copy
, sizeof (path_copy
), "%s%s",
870 zonecfg_get_root(), ze
->zone_path
);
871 res
= resolvepath(path_copy
, rpath
, sizeof (rpath
));
873 if (errno
!= ENOENT
) {
874 zperror(path_copy
, B_FALSE
);
878 (void) printf(gettext("WARNING: zone %s is installed, "
879 "but its %s %s does not exist.\n"), ze
->zone_name
,
880 "zonepath", path_copy
);
885 (void) snprintf(path_copy
, sizeof (path_copy
), "%s/", path
);
886 (void) snprintf(rpath_copy
, sizeof (rpath_copy
), "%s/", rpath
);
887 if (strncmp(path_copy
, rpath_copy
,
888 min(strlen(path_copy
), strlen(rpath_copy
))) == 0) {
891 * zonepath is a literal that should not be translated.
893 (void) fprintf(stderr
, gettext("%s zonepath (%s) and "
894 "%s zonepath (%s) overlap.\n"),
895 target_zone
, path
, ze
->zone_name
, rpath
);
906 validate_zonepath(char *path
, int cmd_num
)
908 int res
; /* result of last library/system call */
909 boolean_t err
= B_FALSE
; /* have we run into an error? */
911 struct statvfs64 vfsbuf
;
912 char rpath
[MAXPATHLEN
]; /* resolved path */
913 char ppath
[MAXPATHLEN
]; /* parent path */
914 char rppath
[MAXPATHLEN
]; /* resolved parent path */
915 char rootpath
[MAXPATHLEN
]; /* root path */
918 if (path
[0] != '/') {
919 (void) fprintf(stderr
,
920 gettext("%s is not an absolute path.\n"), path
);
923 if ((res
= resolvepath(path
, rpath
, sizeof (rpath
))) == -1) {
924 if ((errno
!= ENOENT
) ||
925 (cmd_num
!= CMD_VERIFY
&& cmd_num
!= CMD_INSTALL
&&
926 cmd_num
!= CMD_CLONE
&& cmd_num
!= CMD_MOVE
)) {
927 zperror(path
, B_FALSE
);
930 if (cmd_num
== CMD_VERIFY
) {
933 * zoneadm is a literal that should not be translated.
935 (void) fprintf(stderr
, gettext("WARNING: %s does not "
936 "exist, so it could not be verified.\nWhen "
937 "'zoneadm %s' is run, '%s' will try to create\n%s, "
938 "and '%s' will be tried again,\nbut the '%s' may "
939 "fail if:\nthe parent directory of %s is group- or "
940 "other-writable\nor\n%s overlaps with any other "
941 "installed zones.\n"), path
,
942 cmd_to_str(CMD_INSTALL
), cmd_to_str(CMD_INSTALL
),
943 path
, cmd_to_str(CMD_VERIFY
),
944 cmd_to_str(CMD_VERIFY
), path
, path
);
948 * The zonepath is supposed to be mode 700 but its
949 * parent(s) 755. So use 755 on the mkdirp() then
950 * chmod() the zonepath itself to 700.
952 if (mkdirp(path
, DEFAULT_DIR_MODE
) < 0) {
953 zperror(path
, B_FALSE
);
957 * If the chmod() fails, report the error, but might
958 * as well continue the verify procedure.
960 if (chmod(path
, S_IRWXU
) != 0)
961 zperror(path
, B_FALSE
);
963 * Since the mkdir() succeeded, we should not have to
964 * worry about a subsequent ENOENT, thus this should
967 return (validate_zonepath(path
, cmd_num
));
970 if (strcmp(path
, rpath
) != 0) {
971 errno
= Z_RESOLVED_PATH
;
972 zperror(path
, B_TRUE
);
975 if ((res
= stat(rpath
, &stbuf
)) != 0) {
976 zperror(rpath
, B_FALSE
);
979 if (!S_ISDIR(stbuf
.st_mode
)) {
980 (void) fprintf(stderr
, gettext("%s is not a directory.\n"),
984 if (strcmp(stbuf
.st_fstype
, MNTTYPE_TMPFS
) == 0) {
985 (void) printf(gettext("WARNING: %s is on a temporary "
986 "file system.\n"), rpath
);
988 if (crosscheck_zonepaths(rpath
) != Z_OK
)
991 * Try to collect and report as many minor errors as possible
992 * before returning, so the user can learn everything that needs
993 * to be fixed up front.
995 if (stbuf
.st_uid
!= 0) {
996 (void) fprintf(stderr
, gettext("%s is not owned by root.\n"),
1000 /* Try to change owner */
1001 if (cmd_num
!= CMD_VERIFY
) {
1002 (void) fprintf(stderr
, gettext("%s: changing owner "
1003 "to root.\n"), rpath
);
1004 if (chown(rpath
, 0, -1) != 0) {
1005 zperror(rpath
, B_FALSE
);
1012 err
|= bad_mode_bit(stbuf
.st_mode
, S_IRUSR
, B_TRUE
, rpath
);
1013 err
|= bad_mode_bit(stbuf
.st_mode
, S_IWUSR
, B_TRUE
, rpath
);
1014 err
|= bad_mode_bit(stbuf
.st_mode
, S_IXUSR
, B_TRUE
, rpath
);
1015 err
|= bad_mode_bit(stbuf
.st_mode
, S_IRGRP
, B_FALSE
, rpath
);
1016 err
|= bad_mode_bit(stbuf
.st_mode
, S_IWGRP
, B_FALSE
, rpath
);
1017 err
|= bad_mode_bit(stbuf
.st_mode
, S_IXGRP
, B_FALSE
, rpath
);
1018 err
|= bad_mode_bit(stbuf
.st_mode
, S_IROTH
, B_FALSE
, rpath
);
1019 err
|= bad_mode_bit(stbuf
.st_mode
, S_IWOTH
, B_FALSE
, rpath
);
1020 err
|= bad_mode_bit(stbuf
.st_mode
, S_IXOTH
, B_FALSE
, rpath
);
1022 /* If the group perms are wrong, fix them */
1023 if (err
&& (cmd_num
!= CMD_VERIFY
)) {
1024 (void) fprintf(stderr
, gettext("%s: changing permissions "
1025 "to 0700.\n"), rpath
);
1026 if (chmod(rpath
, S_IRWXU
) != 0) {
1027 zperror(path
, B_FALSE
);
1033 (void) snprintf(ppath
, sizeof (ppath
), "%s/..", path
);
1034 if ((res
= resolvepath(ppath
, rppath
, sizeof (rppath
))) == -1) {
1035 zperror(ppath
, B_FALSE
);
1039 if ((res
= stat(rppath
, &stbuf
)) != 0) {
1040 zperror(rppath
, B_FALSE
);
1043 /* theoretically impossible */
1044 if (!S_ISDIR(stbuf
.st_mode
)) {
1045 (void) fprintf(stderr
, gettext("%s is not a directory.\n"),
1049 if (stbuf
.st_uid
!= 0) {
1050 (void) fprintf(stderr
, gettext("%s is not owned by root.\n"),
1054 err
|= bad_mode_bit(stbuf
.st_mode
, S_IRUSR
, B_TRUE
, rppath
);
1055 err
|= bad_mode_bit(stbuf
.st_mode
, S_IWUSR
, B_TRUE
, rppath
);
1056 err
|= bad_mode_bit(stbuf
.st_mode
, S_IXUSR
, B_TRUE
, rppath
);
1057 err
|= bad_mode_bit(stbuf
.st_mode
, S_IWGRP
, B_FALSE
, rppath
);
1058 err
|= bad_mode_bit(stbuf
.st_mode
, S_IWOTH
, B_FALSE
, rppath
);
1059 if (strcmp(rpath
, rppath
) == 0) {
1060 (void) fprintf(stderr
, gettext("%s is its own parent.\n"),
1065 if (statvfs64(rpath
, &vfsbuf
) != 0) {
1066 zperror(rpath
, B_FALSE
);
1069 if (strcmp(vfsbuf
.f_basetype
, MNTTYPE_NFS
) == 0) {
1072 * Zonepath and NFS are literals that should not be translated.
1074 (void) fprintf(stderr
, gettext("Zonepath %s is on an NFS "
1075 "mounted file system.\n"
1076 "\tA local file system must be used.\n"), rpath
);
1079 if (vfsbuf
.f_flag
& ST_NOSUID
) {
1082 * Zonepath and nosuid are literals that should not be
1085 (void) fprintf(stderr
, gettext("Zonepath %s is on a nosuid "
1086 "file system.\n"), rpath
);
1090 if ((res
= zone_get_state(target_zone
, &state
)) != Z_OK
) {
1092 zperror2(target_zone
, gettext("could not get state"));
1096 * The existence of the root path is only bad in the configured state,
1097 * as it is *supposed* to be there at the installed and later states.
1098 * However, the root path is expected to be there if the zone is
1100 * State/command mismatches are caught earlier in verify_details().
1102 if (state
== ZONE_STATE_CONFIGURED
&& cmd_num
!= CMD_ATTACH
) {
1103 if (snprintf(rootpath
, sizeof (rootpath
), "%s/root", rpath
) >=
1104 sizeof (rootpath
)) {
1107 * Zonepath is a literal that should not be translated.
1109 (void) fprintf(stderr
,
1110 gettext("Zonepath %s is too long.\n"), rpath
);
1113 if ((res
= stat(rootpath
, &stbuf
)) == 0) {
1114 if (zonecfg_detached(rpath
)) {
1115 (void) fprintf(stderr
,
1116 gettext("Cannot %s detached "
1117 "zone.\nUse attach or remove %s "
1118 "directory.\n"), cmd_to_str(cmd_num
),
1123 /* Not detached, check if it really looks ok. */
1125 if (!S_ISDIR(stbuf
.st_mode
)) {
1126 (void) fprintf(stderr
, gettext("%s is not a "
1127 "directory.\n"), rootpath
);
1131 if (stbuf
.st_uid
!= 0) {
1132 (void) fprintf(stderr
, gettext("%s is not "
1133 "owned by root.\n"), rootpath
);
1137 if ((stbuf
.st_mode
& 0777) != 0755) {
1138 (void) fprintf(stderr
, gettext("%s mode is not "
1139 "0755.\n"), rootpath
);
1145 return (err
? Z_ERR
: Z_OK
);
1149 invoke_brand_handler(int cmd_num
, char *argv
[])
1151 zone_dochandle_t handle
;
1154 if ((handle
= zonecfg_init_handle()) == NULL
) {
1155 zperror(cmd_to_str(cmd_num
), B_TRUE
);
1158 if ((err
= zonecfg_get_handle(target_zone
, handle
)) != Z_OK
) {
1160 zperror(cmd_to_str(cmd_num
), B_TRUE
);
1161 zonecfg_fini_handle(handle
);
1164 if (verify_brand(handle
, cmd_num
, argv
) != Z_OK
) {
1165 zonecfg_fini_handle(handle
);
1168 zonecfg_fini_handle(handle
);
1173 ready_func(int argc
, char *argv
[])
1175 zone_cmd_arg_t zarg
;
1178 if (zonecfg_in_alt_root()) {
1179 zerror(gettext("cannot ready zone in alternate root"));
1184 if ((arg
= getopt(argc
, argv
, "?")) != EOF
) {
1187 sub_usage(SHELP_READY
, CMD_READY
);
1188 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
1190 sub_usage(SHELP_READY
, CMD_READY
);
1194 if (argc
> optind
) {
1195 sub_usage(SHELP_READY
, CMD_READY
);
1198 if (sanity_check(target_zone
, CMD_READY
, B_FALSE
, B_FALSE
, B_FALSE
)
1201 if (verify_details(CMD_READY
, argv
) != Z_OK
)
1205 if (zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
, B_TRUE
) != 0) {
1206 zerror(gettext("call to %s failed"), "zoneadmd");
1213 boot_func(int argc
, char *argv
[])
1215 zone_cmd_arg_t zarg
;
1216 boolean_t force
= B_FALSE
;
1219 if (zonecfg_in_alt_root()) {
1220 zerror(gettext("cannot boot zone in alternate root"));
1224 zarg
.bootbuf
[0] = '\0';
1227 * The following getopt processes arguments to zone boot; that
1228 * is to say, the [here] portion of the argument string:
1230 * zoneadm -z myzone boot [here] -- -v -m verbose
1232 * Where [here] can either be nothing, -? (in which case we bail
1233 * and print usage), -f (a private option to indicate that the
1234 * boot operation should be 'forced'), or -s. Support for -s is
1235 * vestigal and obsolete, but is retained because it was a
1236 * documented interface and there are known consumers including
1237 * admin/install; the proper way to specify boot arguments like -s
1240 * zoneadm -z myzone boot -- -s -v -m verbose.
1243 while ((arg
= getopt(argc
, argv
, "?fs")) != EOF
) {
1246 sub_usage(SHELP_BOOT
, CMD_BOOT
);
1247 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
1249 (void) strlcpy(zarg
.bootbuf
, "-s",
1250 sizeof (zarg
.bootbuf
));
1256 sub_usage(SHELP_BOOT
, CMD_BOOT
);
1261 for (; optind
< argc
; optind
++) {
1262 if (strlcat(zarg
.bootbuf
, argv
[optind
],
1263 sizeof (zarg
.bootbuf
)) >= sizeof (zarg
.bootbuf
)) {
1264 zerror(gettext("Boot argument list too long"));
1267 if (optind
< argc
- 1)
1268 if (strlcat(zarg
.bootbuf
, " ", sizeof (zarg
.bootbuf
)) >=
1269 sizeof (zarg
.bootbuf
)) {
1270 zerror(gettext("Boot argument list too long"));
1274 if (sanity_check(target_zone
, CMD_BOOT
, B_FALSE
, B_FALSE
, force
)
1277 if (verify_details(CMD_BOOT
, argv
) != Z_OK
)
1279 zarg
.cmd
= force
? Z_FORCEBOOT
: Z_BOOT
;
1280 if (zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
, B_TRUE
) != 0) {
1281 zerror(gettext("call to %s failed"), "zoneadmd");
1289 fake_up_local_zone(zoneid_t zid
, zone_entry_t
*zeptr
)
1296 (void) memset(zeptr
, 0, sizeof (*zeptr
));
1301 * Since we're looking up our own (non-global) zone name,
1302 * we can be assured that it will succeed.
1304 result
= getzonenamebyid(zid
, zeptr
->zname
, sizeof (zeptr
->zname
));
1305 assert(result
>= 0);
1306 if (zonecfg_is_scratch(zeptr
->zname
) &&
1307 (fp
= zonecfg_open_scratch("", B_FALSE
)) != NULL
) {
1308 (void) zonecfg_reverse_scratch(fp
, zeptr
->zname
, zeptr
->zname
,
1309 sizeof (zeptr
->zname
), NULL
, 0);
1310 zonecfg_close_scratch(fp
);
1313 (void) strlcpy(zeptr
->zroot
, "/", sizeof (zeptr
->zroot
));
1314 (void) zone_getattr(zid
, ZONE_ATTR_BRAND
, zeptr
->zbrand
,
1315 sizeof (zeptr
->zbrand
));
1317 zeptr
->zstate_str
= "running";
1318 if (zonecfg_get_uuid(zeptr
->zname
, uuid
) == Z_OK
&&
1319 !uuid_is_null(uuid
))
1320 uuid_unparse(uuid
, zeptr
->zuuid
);
1322 if (zone_getattr(zid
, ZONE_ATTR_FLAGS
, &flags
, sizeof (flags
)) < 0) {
1323 zperror2(zeptr
->zname
, gettext("could not get zone flags"));
1326 if (flags
& ZF_NET_EXCL
)
1327 zeptr
->ziptype
= ZS_EXCLUSIVE
;
1329 zeptr
->ziptype
= ZS_SHARED
;
1333 list_func(int argc
, char *argv
[])
1335 zone_entry_t
*zentp
, zent
;
1337 boolean_t output
= B_FALSE
, verbose
= B_FALSE
, parsable
= B_FALSE
;
1338 zone_state_t min_state
= ZONE_STATE_RUNNING
;
1339 zoneid_t zone_id
= getzoneid();
1341 if (target_zone
== NULL
) {
1342 /* all zones: default view to running but allow override */
1344 while ((arg
= getopt(argc
, argv
, "?cipv")) != EOF
) {
1347 sub_usage(SHELP_LIST
, CMD_LIST
);
1348 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
1350 * The 'i' and 'c' options are not mutually
1351 * exclusive so if 'c' is given, then min_state
1352 * is set to 0 (ZONE_STATE_CONFIGURED) which is
1353 * the lowest possible state. If 'i' is given,
1354 * then min_state is set to be the lowest state
1358 min_state
= ZONE_STATE_CONFIGURED
;
1361 min_state
= min(ZONE_STATE_INSTALLED
,
1372 sub_usage(SHELP_LIST
, CMD_LIST
);
1376 if (parsable
&& verbose
) {
1377 zerror(gettext("%s -p and -v are mutually exclusive."),
1378 cmd_to_str(CMD_LIST
));
1381 if (zone_id
== GLOBAL_ZONEID
) {
1382 retv
= zone_print_list(min_state
, verbose
, parsable
);
1384 fake_up_local_zone(zone_id
, &zent
);
1386 zone_print(&zent
, verbose
, parsable
);
1392 * Specific target zone: disallow -i/-c suboptions.
1395 while ((arg
= getopt(argc
, argv
, "?pv")) != EOF
) {
1398 sub_usage(SHELP_LIST
, CMD_LIST
);
1399 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
1407 sub_usage(SHELP_LIST
, CMD_LIST
);
1411 if (parsable
&& verbose
) {
1412 zerror(gettext("%s -p and -v are mutually exclusive."),
1413 cmd_to_str(CMD_LIST
));
1416 if (argc
> optind
) {
1417 sub_usage(SHELP_LIST
, CMD_LIST
);
1420 if (zone_id
!= GLOBAL_ZONEID
) {
1421 fake_up_local_zone(zone_id
, &zent
);
1423 * main() will issue a Z_NO_ZONE error if it cannot get an
1424 * id for target_zone, which in a non-global zone should
1425 * happen for any zone name except `zonename`. Thus we
1426 * assert() that here but don't otherwise check.
1428 assert(strcmp(zent
.zname
, target_zone
) == 0);
1429 zone_print(&zent
, verbose
, parsable
);
1431 } else if ((zentp
= lookup_running_zone(target_zone
)) != NULL
) {
1432 zone_print(zentp
, verbose
, parsable
);
1434 } else if (lookup_zone_info(target_zone
, ZONE_ID_UNDEFINED
,
1436 zone_print(&zent
, verbose
, parsable
);
1441 * Invoke brand-specific handler. Note that we do this
1442 * only if we're in the global zone, and target_zone is specified
1443 * and it is not the global zone.
1445 if (zone_id
== GLOBAL_ZONEID
&& target_zone
!= NULL
&&
1446 strcmp(target_zone
, GLOBAL_ZONENAME
) != 0)
1447 if (invoke_brand_handler(CMD_LIST
, argv
) != Z_OK
)
1450 return (output
? Z_OK
: Z_ERR
);
1454 do_subproc(char *cmdbuf
)
1456 void (*saveint
)(int);
1457 void (*saveterm
)(int);
1458 void (*savequit
)(int);
1459 void (*savehup
)(int);
1460 int pid
, child
, status
;
1462 if ((child
= vfork()) == 0) {
1463 (void) execl("/bin/sh", "sh", "-c", cmdbuf
, NULL
);
1469 saveint
= sigset(SIGINT
, SIG_IGN
);
1470 saveterm
= sigset(SIGTERM
, SIG_IGN
);
1471 savequit
= sigset(SIGQUIT
, SIG_IGN
);
1472 savehup
= sigset(SIGHUP
, SIG_IGN
);
1474 while ((pid
= waitpid(child
, &status
, 0)) != child
&& pid
!= -1)
1477 (void) sigset(SIGINT
, saveint
);
1478 (void) sigset(SIGTERM
, saveterm
);
1479 (void) sigset(SIGQUIT
, savequit
);
1480 (void) sigset(SIGHUP
, savehup
);
1482 return (pid
== -1 ? -1 : status
);
1486 subproc_status(const char *cmd
, int status
, boolean_t verbose_failure
)
1488 if (WIFEXITED(status
)) {
1489 int exit_code
= WEXITSTATUS(status
);
1491 if ((verbose_failure
) && (exit_code
!= ZONE_SUBPROC_OK
))
1492 zerror(gettext("'%s' failed with exit code %d."), cmd
,
1496 } else if (WIFSIGNALED(status
)) {
1497 int signal
= WTERMSIG(status
);
1498 char sigstr
[SIG2STR_MAX
];
1500 if (sig2str(signal
, sigstr
) == 0) {
1501 zerror(gettext("'%s' terminated by signal SIG%s."), cmd
,
1504 zerror(gettext("'%s' terminated by an unknown signal."),
1508 zerror(gettext("'%s' failed for unknown reasons."), cmd
);
1512 * Assume a subprocess that died due to a signal or an unknown error
1513 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the
1514 * user will likely need to do some manual cleanup.
1516 return (ZONE_SUBPROC_FATAL
);
1520 auth_check(char *user
, char *zone
, int cmd_num
)
1522 char authname
[MAXAUTHS
];
1529 (void) strlcpy(authname
, ZONE_CLONEFROM_AUTH
, MAXAUTHS
);
1549 (void) strlcpy(authname
, ZONE_MANAGE_AUTH
, MAXAUTHS
);
1552 (void) strlcat(authname
, KV_OBJECT
, MAXAUTHS
);
1553 (void) strlcat(authname
, zone
, MAXAUTHS
);
1554 if (chkauthattr(authname
, user
) == 0) {
1558 * Some subcommands, e.g. install, run subcommands,
1559 * e.g. sysidcfg, that require a real uid of root,
1560 * so switch to root, here.
1562 if (setuid(0) == -1) {
1563 zperror(gettext("insufficient privilege"), B_TRUE
);
1571 * Various sanity checks; make sure:
1572 * 1. We're in the global zone.
1573 * 2. The calling user has sufficient privilege.
1574 * 3. The target zone is neither the global zone nor anything starting with
1576 * 4a. If we're looking for a 'not running' (i.e., configured or installed)
1577 * zone, the name service knows about it.
1578 * 4b. For some operations which expect a zone not to be running, that it is
1579 * not already running (or ready).
1582 sanity_check(char *zone
, int cmd_num
, boolean_t running
,
1583 boolean_t unsafe_when_running
, boolean_t force
)
1586 priv_set_t
*privset
;
1587 zone_state_t state
, min_state
;
1588 char kernzone
[ZONENAME_MAX
];
1591 if (getzoneid() != GLOBAL_ZONEID
) {
1594 zerror(gettext("use %s to %s this zone."), "halt(1M)",
1595 cmd_to_str(cmd_num
));
1598 zerror(gettext("use %s to %s this zone."),
1599 "shutdown(1M)", cmd_to_str(cmd_num
));
1602 zerror(gettext("use %s to %s this zone."),
1603 "reboot(1M)", cmd_to_str(cmd_num
));
1606 zerror(gettext("must be in the global zone to %s a "
1607 "zone."), cmd_to_str(cmd_num
));
1613 if ((privset
= priv_allocset()) == NULL
) {
1614 zerror(gettext("%s failed"), "priv_allocset");
1618 if (getppriv(PRIV_EFFECTIVE
, privset
) != 0) {
1619 zerror(gettext("%s failed"), "getppriv");
1620 priv_freeset(privset
);
1624 if (priv_isfullset(privset
) == B_FALSE
) {
1625 zerror(gettext("only a privileged user may %s a zone."),
1626 cmd_to_str(cmd_num
));
1627 priv_freeset(privset
);
1630 priv_freeset(privset
);
1633 zerror(gettext("no zone specified"));
1637 if (auth_check(username
, zone
, cmd_num
) == Z_ERR
) {
1638 zerror(gettext("User %s is not authorized to %s this zone."),
1639 username
, cmd_to_str(cmd_num
));
1643 if (strcmp(zone
, GLOBAL_ZONENAME
) == 0) {
1644 zerror(gettext("%s operation is invalid for the global zone."),
1645 cmd_to_str(cmd_num
));
1649 if (strncmp(zone
, "SUNW", 4) == 0) {
1650 zerror(gettext("%s operation is invalid for zones starting "
1651 "with SUNW."), cmd_to_str(cmd_num
));
1655 if (!zonecfg_in_alt_root()) {
1656 zent
= lookup_running_zone(zone
);
1657 } else if ((fp
= zonecfg_open_scratch("", B_FALSE
)) == NULL
) {
1660 if (zonecfg_find_scratch(fp
, zone
, zonecfg_get_root(),
1661 kernzone
, sizeof (kernzone
)) == 0)
1662 zent
= lookup_running_zone(kernzone
);
1665 zonecfg_close_scratch(fp
);
1669 * Look up from the kernel for 'running' zones.
1671 if (running
&& !force
) {
1673 zerror(gettext("not running"));
1679 if (unsafe_when_running
&& zent
!= NULL
) {
1680 /* check whether the zone is ready or running */
1681 if ((err
= zone_get_state(zent
->zname
,
1682 &zent
->zstate_num
)) != Z_OK
) {
1684 zperror2(zent
->zname
,
1685 gettext("could not get state"));
1686 /* can't tell, so hedge */
1687 zent
->zstate_str
= "ready/running";
1690 zone_state_str(zent
->zstate_num
);
1692 zerror(gettext("%s operation is invalid for %s zones."),
1693 cmd_to_str(cmd_num
), zent
->zstate_str
);
1696 if ((err
= zone_get_state(zone
, &state
)) != Z_OK
) {
1698 zperror2(zone
, gettext("could not get state"));
1703 if (state
== ZONE_STATE_CONFIGURED
) {
1704 zerror(gettext("is already in state '%s'."),
1705 zone_state_str(ZONE_STATE_CONFIGURED
));
1710 if (state
== ZONE_STATE_INSTALLED
) {
1711 zerror(gettext("is already %s."),
1712 zone_state_str(ZONE_STATE_INSTALLED
));
1714 } else if (state
== ZONE_STATE_INCOMPLETE
&& !force
) {
1715 zerror(gettext("zone is %s; %s required."),
1716 zone_state_str(ZONE_STATE_INCOMPLETE
),
1717 cmd_to_str(CMD_UNINSTALL
));
1723 if (state
== ZONE_STATE_INSTALLED
) {
1724 zerror(gettext("is already %s."),
1725 zone_state_str(ZONE_STATE_INSTALLED
));
1727 } else if (state
== ZONE_STATE_INCOMPLETE
) {
1728 zerror(gettext("zone is %s; %s required."),
1729 zone_state_str(ZONE_STATE_INCOMPLETE
),
1730 cmd_to_str(CMD_UNINSTALL
));
1740 if ((cmd_num
== CMD_BOOT
|| cmd_num
== CMD_MOUNT
) &&
1742 min_state
= ZONE_STATE_INCOMPLETE
;
1743 else if (cmd_num
== CMD_MARK
)
1744 min_state
= ZONE_STATE_CONFIGURED
;
1746 min_state
= ZONE_STATE_INSTALLED
;
1748 if (state
< min_state
) {
1749 zerror(gettext("must be %s before %s."),
1750 zone_state_str(min_state
),
1751 cmd_to_str(cmd_num
));
1756 if (state
== ZONE_STATE_INCOMPLETE
) {
1757 zerror(gettext("zone is %s; %s required."),
1758 zone_state_str(ZONE_STATE_INCOMPLETE
),
1759 cmd_to_str(CMD_UNINSTALL
));
1764 if (state
!= ZONE_STATE_MOUNTED
) {
1765 zerror(gettext("must be %s before %s."),
1766 zone_state_str(ZONE_STATE_MOUNTED
),
1767 cmd_to_str(cmd_num
));
1772 if (state
!= ZONE_STATE_INSTALLED
) {
1773 zerror(gettext("%s operation is invalid for %s "
1774 "zones."), cmd_to_str(cmd_num
),
1775 zone_state_str(state
));
1785 halt_func(int argc
, char *argv
[])
1787 zone_cmd_arg_t zarg
;
1790 if (zonecfg_in_alt_root()) {
1791 zerror(gettext("cannot halt zone in alternate root"));
1796 if ((arg
= getopt(argc
, argv
, "?")) != EOF
) {
1799 sub_usage(SHELP_HALT
, CMD_HALT
);
1800 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
1802 sub_usage(SHELP_HALT
, CMD_HALT
);
1806 if (argc
> optind
) {
1807 sub_usage(SHELP_HALT
, CMD_HALT
);
1811 * zoneadmd should be the one to decide whether or not to proceed,
1812 * so even though it seems that the fourth parameter below should
1813 * perhaps be B_TRUE, it really shouldn't be.
1815 if (sanity_check(target_zone
, CMD_HALT
, B_FALSE
, B_FALSE
, B_FALSE
)
1820 * Invoke brand-specific handler.
1822 if (invoke_brand_handler(CMD_HALT
, argv
) != Z_OK
)
1826 return ((zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
,
1827 B_TRUE
) == 0) ? Z_OK
: Z_ERR
);
1831 shutdown_func(int argc
, char *argv
[])
1833 zone_cmd_arg_t zarg
;
1835 boolean_t reboot
= B_FALSE
;
1837 zarg
.cmd
= Z_SHUTDOWN
;
1839 if (zonecfg_in_alt_root()) {
1840 zerror(gettext("cannot shut down zone in alternate root"));
1845 while ((arg
= getopt(argc
, argv
, "?r")) != EOF
) {
1848 sub_usage(SHELP_SHUTDOWN
, CMD_SHUTDOWN
);
1849 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
1854 sub_usage(SHELP_SHUTDOWN
, CMD_SHUTDOWN
);
1859 zarg
.bootbuf
[0] = '\0';
1860 for (; optind
< argc
; optind
++) {
1861 if (strlcat(zarg
.bootbuf
, argv
[optind
],
1862 sizeof (zarg
.bootbuf
)) >= sizeof (zarg
.bootbuf
)) {
1863 zerror(gettext("Boot argument list too long"));
1866 if (optind
< argc
- 1)
1867 if (strlcat(zarg
.bootbuf
, " ", sizeof (zarg
.bootbuf
)) >=
1868 sizeof (zarg
.bootbuf
)) {
1869 zerror(gettext("Boot argument list too long"));
1875 * zoneadmd should be the one to decide whether or not to proceed,
1876 * so even though it seems that the third parameter below should
1877 * perhaps be B_TRUE, it really shouldn't be.
1879 if (sanity_check(target_zone
, CMD_SHUTDOWN
, B_TRUE
, B_FALSE
, B_FALSE
)
1883 if (zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
, B_TRUE
) != Z_OK
)
1887 if (sanity_check(target_zone
, CMD_BOOT
, B_FALSE
, B_FALSE
,
1892 if (zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
,
1900 reboot_func(int argc
, char *argv
[])
1902 zone_cmd_arg_t zarg
;
1905 if (zonecfg_in_alt_root()) {
1906 zerror(gettext("cannot reboot zone in alternate root"));
1911 if ((arg
= getopt(argc
, argv
, "?")) != EOF
) {
1914 sub_usage(SHELP_REBOOT
, CMD_REBOOT
);
1915 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
1917 sub_usage(SHELP_REBOOT
, CMD_REBOOT
);
1922 zarg
.bootbuf
[0] = '\0';
1923 for (; optind
< argc
; optind
++) {
1924 if (strlcat(zarg
.bootbuf
, argv
[optind
],
1925 sizeof (zarg
.bootbuf
)) >= sizeof (zarg
.bootbuf
)) {
1926 zerror(gettext("Boot argument list too long"));
1929 if (optind
< argc
- 1)
1930 if (strlcat(zarg
.bootbuf
, " ", sizeof (zarg
.bootbuf
)) >=
1931 sizeof (zarg
.bootbuf
)) {
1932 zerror(gettext("Boot argument list too long"));
1939 * zoneadmd should be the one to decide whether or not to proceed,
1940 * so even though it seems that the fourth parameter below should
1941 * perhaps be B_TRUE, it really shouldn't be.
1943 if (sanity_check(target_zone
, CMD_REBOOT
, B_TRUE
, B_FALSE
, B_FALSE
)
1946 if (verify_details(CMD_REBOOT
, argv
) != Z_OK
)
1949 zarg
.cmd
= Z_REBOOT
;
1950 return ((zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
, B_TRUE
) == 0)
1955 get_hook(brand_handle_t bh
, char *cmd
, size_t len
, int (*bp
)(brand_handle_t
,
1956 const char *, const char *, char *, size_t), char *zonename
, char *zonepath
)
1958 if (strlcpy(cmd
, EXEC_PREFIX
, len
) >= len
)
1961 if (bp(bh
, zonename
, zonepath
, cmd
+ EXEC_LEN
, len
- EXEC_LEN
) != 0)
1964 if (strlen(cmd
) <= EXEC_LEN
)
1971 verify_brand(zone_dochandle_t handle
, int cmd_num
, char *argv
[])
1973 char cmdbuf
[MAXPATHLEN
];
1975 char zonepath
[MAXPATHLEN
];
1976 brand_handle_t bh
= NULL
;
1980 * Fetch the verify command from the brand configuration.
1981 * "exec" the command so that the returned status is that of
1982 * the command and not the shell.
1984 if (handle
== NULL
) {
1985 (void) strlcpy(zonepath
, "-", sizeof (zonepath
));
1986 } else if ((err
= zonecfg_get_zonepath(handle
, zonepath
,
1987 sizeof (zonepath
))) != Z_OK
) {
1989 zperror(cmd_to_str(cmd_num
), B_TRUE
);
1992 if ((bh
= brand_open(target_brand
)) == NULL
) {
1993 zerror(gettext("missing or invalid brand"));
1998 * If the brand has its own verification routine, execute it now.
1999 * The verification routine validates the intended zoneadm
2000 * operation for the specific brand. The zoneadm subcommand and
2001 * all its arguments are passed to the routine.
2003 err
= get_hook(bh
, cmdbuf
, sizeof (cmdbuf
), brand_get_verify_adm
,
2004 target_zone
, zonepath
);
2007 return (Z_BRAND_ERROR
);
2008 if (cmdbuf
[0] == '\0')
2011 if (strlcat(cmdbuf
, cmd_to_str(cmd_num
),
2012 sizeof (cmdbuf
)) >= sizeof (cmdbuf
))
2015 /* Build the argv string */
2017 while (argv
[i
] != NULL
) {
2018 if ((strlcat(cmdbuf
, " ",
2019 sizeof (cmdbuf
)) >= sizeof (cmdbuf
)) ||
2020 (strlcat(cmdbuf
, argv
[i
++],
2021 sizeof (cmdbuf
)) >= sizeof (cmdbuf
)))
2025 status
= do_subproc(cmdbuf
);
2026 err
= subproc_status(gettext("brand-specific verification"),
2029 return ((err
== ZONE_SUBPROC_OK
) ? Z_OK
: Z_BRAND_ERROR
);
2033 verify_rctls(zone_dochandle_t handle
)
2035 struct zone_rctltab rctltab
;
2036 size_t rbs
= rctlblk_size();
2038 int error
= Z_INVAL
;
2040 if ((rctlblk
= malloc(rbs
)) == NULL
) {
2041 zerror(gettext("failed to allocate %lu bytes: %s"), rbs
,
2046 if (zonecfg_setrctlent(handle
) != Z_OK
) {
2047 zerror(gettext("zonecfg_setrctlent failed"));
2052 rctltab
.zone_rctl_valptr
= NULL
;
2053 while (zonecfg_getrctlent(handle
, &rctltab
) == Z_OK
) {
2054 struct zone_rctlvaltab
*rctlval
;
2055 const char *name
= rctltab
.zone_rctl_name
;
2057 if (!zonecfg_is_rctl(name
)) {
2058 zerror(gettext("WARNING: Ignoring unrecognized rctl "
2060 zonecfg_free_rctl_value_list(rctltab
.zone_rctl_valptr
);
2061 rctltab
.zone_rctl_valptr
= NULL
;
2065 for (rctlval
= rctltab
.zone_rctl_valptr
; rctlval
!= NULL
;
2066 rctlval
= rctlval
->zone_rctlval_next
) {
2067 if (zonecfg_construct_rctlblk(rctlval
, rctlblk
)
2069 zerror(gettext("invalid rctl value: "
2070 "(priv=%s,limit=%s,action%s)"),
2071 rctlval
->zone_rctlval_priv
,
2072 rctlval
->zone_rctlval_limit
,
2073 rctlval
->zone_rctlval_action
);
2076 if (!zonecfg_valid_rctl(name
, rctlblk
)) {
2077 zerror(gettext("(priv=%s,limit=%s,action=%s) "
2078 "is not a valid value for rctl '%s'"),
2079 rctlval
->zone_rctlval_priv
,
2080 rctlval
->zone_rctlval_limit
,
2081 rctlval
->zone_rctlval_action
,
2086 zonecfg_free_rctl_value_list(rctltab
.zone_rctl_valptr
);
2088 rctltab
.zone_rctl_valptr
= NULL
;
2091 zonecfg_free_rctl_value_list(rctltab
.zone_rctl_valptr
);
2092 (void) zonecfg_endrctlent(handle
);
2098 verify_pool(zone_dochandle_t handle
)
2100 char poolname
[MAXPATHLEN
];
2101 pool_conf_t
*poolconf
;
2107 * This ends up being very similar to the check done in zoneadmd.
2109 error
= zonecfg_get_pool(handle
, poolname
, sizeof (poolname
));
2110 if (error
== Z_NO_ENTRY
|| (error
== Z_OK
&& strlen(poolname
) == 0)) {
2112 * No pool specified.
2116 if (error
!= Z_OK
) {
2117 zperror(gettext("Unable to retrieve pool name from "
2118 "configuration"), B_TRUE
);
2122 * Don't do anything if pools aren't enabled.
2124 if (pool_get_status(&status
) != PO_SUCCESS
|| status
!= POOL_ENABLED
) {
2125 zerror(gettext("WARNING: pools facility not active; "
2126 "zone will not be bound to pool '%s'."), poolname
);
2130 * Try to provide a sane error message if the requested pool doesn't
2131 * exist. It isn't clear that pools-related failures should
2132 * necessarily translate to a failure to verify the zone configuration,
2133 * hence they are not considered errors.
2135 if ((poolconf
= pool_conf_alloc()) == NULL
) {
2136 zerror(gettext("WARNING: pool_conf_alloc failed; "
2137 "using default pool"));
2140 if (pool_conf_open(poolconf
, pool_dynamic_location(), PO_RDONLY
) !=
2142 zerror(gettext("WARNING: pool_conf_open failed; "
2143 "using default pool"));
2144 pool_conf_free(poolconf
);
2147 pool
= pool_get_pool(poolconf
, poolname
);
2148 (void) pool_conf_close(poolconf
);
2149 pool_conf_free(poolconf
);
2151 zerror(gettext("WARNING: pool '%s' not found. "
2152 "using default pool"), poolname
);
2159 * Verify that the special device/file system exists and is valid.
2162 verify_fs_special(struct zone_fstab
*fstab
)
2167 * This validation is really intended for standard zone administration.
2168 * If we are in a mini-root or some other upgrade situation where
2169 * we are using the scratch zone, just by-pass this.
2171 if (zonecfg_in_alt_root())
2174 if (strcmp(fstab
->zone_fs_type
, MNTTYPE_ZFS
) == 0)
2175 return (verify_fs_zfs(fstab
));
2177 if (stat64(fstab
->zone_fs_special
, &st
) != 0) {
2178 (void) fprintf(stderr
, gettext("could not verify fs "
2179 "%s: could not access %s: %s\n"), fstab
->zone_fs_dir
,
2180 fstab
->zone_fs_special
, strerror(errno
));
2184 if (strcmp(st
.st_fstype
, MNTTYPE_NFS
) == 0) {
2187 * fs and NFS are literals that should
2188 * not be translated.
2190 (void) fprintf(stderr
, gettext("cannot verify "
2191 "fs %s: NFS mounted file system.\n"
2192 "\tA local file system must be used.\n"),
2193 fstab
->zone_fs_special
);
2201 isregfile(const char *path
)
2205 if (stat64(path
, &st
) == -1)
2208 return (S_ISREG(st
.st_mode
));
2212 verify_filesystems(zone_dochandle_t handle
)
2214 int return_code
= Z_OK
;
2215 struct zone_fstab fstab
;
2216 char cmdbuf
[MAXPATHLEN
];
2220 * Since the actual mount point is not known until the dependent mounts
2221 * are performed, we don't attempt any path validation here: that will
2222 * happen later when zoneadmd actually does the mounts.
2224 if (zonecfg_setfsent(handle
) != Z_OK
) {
2225 (void) fprintf(stderr
, gettext("could not verify file systems: "
2226 "unable to enumerate mounts\n"));
2229 while (zonecfg_getfsent(handle
, &fstab
) == Z_OK
) {
2230 if (!zonecfg_valid_fs_type(fstab
.zone_fs_type
)) {
2231 (void) fprintf(stderr
, gettext("cannot verify fs %s: "
2232 "type %s is not allowed.\n"), fstab
.zone_fs_dir
,
2233 fstab
.zone_fs_type
);
2234 return_code
= Z_ERR
;
2238 * Verify /usr/lib/fs/<fstype>/mount exists.
2240 if (snprintf(cmdbuf
, sizeof (cmdbuf
), "/usr/lib/fs/%s/mount",
2241 fstab
.zone_fs_type
) > sizeof (cmdbuf
)) {
2242 (void) fprintf(stderr
, gettext("cannot verify fs %s: "
2243 "type %s is too long.\n"), fstab
.zone_fs_dir
,
2244 fstab
.zone_fs_type
);
2245 return_code
= Z_ERR
;
2248 if (stat(cmdbuf
, &st
) != 0) {
2249 (void) fprintf(stderr
, gettext("could not verify fs "
2250 "%s: could not access %s: %s\n"), fstab
.zone_fs_dir
,
2251 cmdbuf
, strerror(errno
));
2252 return_code
= Z_ERR
;
2255 if (!S_ISREG(st
.st_mode
)) {
2256 (void) fprintf(stderr
, gettext("could not verify fs "
2257 "%s: %s is not a regular file\n"),
2258 fstab
.zone_fs_dir
, cmdbuf
);
2259 return_code
= Z_ERR
;
2263 * If zone_fs_raw is set, verify that there's an fsck
2264 * binary for it. If zone_fs_raw is not set, and it's
2265 * not a regular file (lofi mount), and there's an fsck
2266 * binary for it, complain.
2268 if (snprintf(cmdbuf
, sizeof (cmdbuf
), "/usr/lib/fs/%s/fsck",
2269 fstab
.zone_fs_type
) > sizeof (cmdbuf
)) {
2270 (void) fprintf(stderr
, gettext("cannot verify fs %s: "
2271 "type %s is too long.\n"), fstab
.zone_fs_dir
,
2272 fstab
.zone_fs_type
);
2273 return_code
= Z_ERR
;
2276 if (fstab
.zone_fs_raw
[0] != '\0' &&
2277 (stat(cmdbuf
, &st
) != 0 || !S_ISREG(st
.st_mode
))) {
2278 (void) fprintf(stderr
, gettext("cannot verify fs %s: "
2279 "'raw' device specified but "
2280 "no fsck executable exists for %s\n"),
2281 fstab
.zone_fs_dir
, fstab
.zone_fs_type
);
2282 return_code
= Z_ERR
;
2284 } else if (fstab
.zone_fs_raw
[0] == '\0' &&
2285 stat(cmdbuf
, &st
) == 0 &&
2286 isregfile(fstab
.zone_fs_special
) != 1) {
2287 (void) fprintf(stderr
, gettext("could not verify fs "
2288 "%s: must specify 'raw' device for %s "
2290 fstab
.zone_fs_dir
, fstab
.zone_fs_type
);
2291 return_code
= Z_ERR
;
2295 /* Verify fs_special. */
2296 if ((return_code
= verify_fs_special(&fstab
)) != Z_OK
)
2299 /* Verify fs_raw. */
2300 if (fstab
.zone_fs_raw
[0] != '\0' &&
2301 stat(fstab
.zone_fs_raw
, &st
) != 0) {
2304 * fs is a literal that should not be translated.
2306 (void) fprintf(stderr
, gettext("could not verify fs "
2307 "%s: could not access %s: %s\n"), fstab
.zone_fs_dir
,
2308 fstab
.zone_fs_raw
, strerror(errno
));
2309 return_code
= Z_ERR
;
2313 zonecfg_free_fs_option_list(fstab
.zone_fs_options
);
2315 (void) zonecfg_endfsent(handle
);
2317 return (return_code
);
2321 verify_limitpriv(zone_dochandle_t handle
)
2323 char *privname
= NULL
;
2327 if ((privs
= priv_allocset()) == NULL
) {
2328 zperror(gettext("failed to allocate privilege set"), B_FALSE
);
2331 err
= zonecfg_get_privset(handle
, privs
, &privname
);
2335 case Z_PRIV_PROHIBITED
:
2336 (void) fprintf(stderr
, gettext("privilege \"%s\" is not "
2337 "permitted within the zone's privilege set\n"), privname
);
2339 case Z_PRIV_REQUIRED
:
2340 (void) fprintf(stderr
, gettext("required privilege \"%s\" is "
2341 "missing from the zone's privilege set\n"), privname
);
2343 case Z_PRIV_UNKNOWN
:
2344 (void) fprintf(stderr
, gettext("unknown privilege \"%s\" "
2345 "specified in the zone's privilege set\n"), privname
);
2349 gettext("failed to determine the zone's privilege set"),
2354 priv_freeset(privs
);
2359 free_local_netifs(int if_cnt
, struct net_if
**if_list
)
2363 for (i
= 0; i
< if_cnt
; i
++) {
2364 free(if_list
[i
]->name
);
2371 * Get a list of the network interfaces, along with their address families,
2372 * that are plumbed in the global zone. See if_tcp(7p) for a description
2373 * of the ioctls used here.
2376 get_local_netifs(int *if_cnt
, struct net_if
***if_list
)
2383 struct lifnum if_num
;
2384 struct lifconf if_conf
;
2385 struct lifreq
*if_reqp
;
2387 struct net_if
**local_ifs
= NULL
;
2392 if ((s
= socket(SOCKET_AF(AF_INET
), SOCK_DGRAM
, 0)) < 0)
2396 * Come back here in the unlikely event that the number of interfaces
2397 * increases between the time we get the count and the time we do the
2398 * SIOCGLIFCONF ioctl.
2401 /* Get the number of interfaces. */
2402 if_num
.lifn_family
= AF_UNSPEC
;
2403 if_num
.lifn_flags
= LIFC_NOXMIT
;
2404 if (ioctl(s
, SIOCGLIFNUM
, &if_num
) < 0) {
2409 /* Get the interface configuration list. */
2410 space_needed
= if_num
.lifn_count
* sizeof (struct lifreq
);
2411 if ((if_buf
= malloc(space_needed
)) == NULL
) {
2415 if_conf
.lifc_family
= AF_UNSPEC
;
2416 if_conf
.lifc_flags
= LIFC_NOXMIT
;
2417 if_conf
.lifc_len
= space_needed
;
2418 if_conf
.lifc_buf
= if_buf
;
2419 if (ioctl(s
, SIOCGLIFCONF
, &if_conf
) < 0) {
2422 * SIOCGLIFCONF returns EINVAL if the buffer we passed in is
2423 * too small. In this case go back and get the new if cnt.
2425 if (errno
== EINVAL
)
2433 /* Get the name and address family for each interface. */
2434 if_reqp
= if_conf
.lifc_req
;
2435 for (i
= 0; i
< (if_conf
.lifc_len
/ sizeof (struct lifreq
)); i
++) {
2439 if (strcmp(LOOPBACK_IF
, if_reqp
->lifr_name
) == 0) {
2444 if ((s
= socket(SOCKET_AF(if_reqp
->lifr_addr
.ss_family
),
2445 SOCK_DGRAM
, 0)) == -1) {
2450 (void) strncpy(req
.lifr_name
, if_reqp
->lifr_name
,
2451 sizeof (req
.lifr_name
));
2452 if (ioctl(s
, SIOCGLIFADDR
, &req
) < 0) {
2458 if ((p
= reallocarray(local_ifs
, cnt
+ 1,
2459 sizeof (struct net_if
*))) == NULL
) {
2465 if ((local_ifs
[cnt
] = malloc(sizeof (struct net_if
))) == NULL
) {
2470 if ((local_ifs
[cnt
]->name
= strdup(if_reqp
->lifr_name
))
2472 free(local_ifs
[cnt
]);
2476 local_ifs
[cnt
]->af
= req
.lifr_addr
.ss_family
;
2486 free_local_netifs(cnt
, local_ifs
);
2489 *if_list
= local_ifs
;
2509 * Cross check the network interface name and address family with the
2510 * interfaces that are set up in the global zone so that we can print the
2511 * appropriate error message.
2514 print_net_err(char *phys
, char *addr
, int af
, char *msg
)
2517 int local_if_cnt
= 0;
2518 struct net_if
**local_ifs
= NULL
;
2519 boolean_t found_if
= B_FALSE
;
2520 boolean_t found_af
= B_FALSE
;
2522 if (get_local_netifs(&local_if_cnt
, &local_ifs
) != Z_OK
) {
2523 (void) fprintf(stderr
,
2524 gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
2525 "net", "address", addr
, "physical", phys
, msg
);
2529 for (i
= 0; i
< local_if_cnt
; i
++) {
2530 if (strcmp(phys
, local_ifs
[i
]->name
) == 0) {
2532 if (af
== local_ifs
[i
]->af
) {
2539 free_local_netifs(local_if_cnt
, local_ifs
);
2542 (void) fprintf(stderr
,
2543 gettext("could not verify %s %s=%s\n\t"
2544 "network interface %s is not plumbed in the global zone\n"),
2545 "net", "physical", phys
, phys
);
2550 * Print this error if we were unable to find the address family
2551 * for this interface. If the af variable is not initialized to
2552 * to something meaningful by the caller (not AF_UNSPEC) then we
2553 * also skip this message since it wouldn't be informative.
2555 if (!found_af
&& af
!= AF_UNSPEC
) {
2556 (void) fprintf(stderr
,
2557 gettext("could not verify %s %s=%s %s=%s\n\tthe %s address "
2558 "family is not configured on this network interface in "
2559 "the\n\tglobal zone\n"),
2560 "net", "address", addr
, "physical", phys
, af2str(af
));
2564 (void) fprintf(stderr
,
2565 gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
2566 "net", "address", addr
, "physical", phys
, msg
);
2570 verify_handle(int cmd_num
, zone_dochandle_t handle
, char *argv
[])
2572 struct zone_nwiftab nwiftab
;
2573 int return_code
= Z_OK
;
2575 boolean_t in_alt_root
;
2576 zone_iptype_t iptype
;
2578 dladm_status_t status
;
2579 datalink_id_t linkid
;
2580 char errmsg
[DLADM_STRSIZE
];
2582 in_alt_root
= zonecfg_in_alt_root();
2586 if ((err
= zonecfg_get_iptype(handle
, &iptype
)) != Z_OK
) {
2588 zperror(cmd_to_str(cmd_num
), B_TRUE
);
2589 zonecfg_fini_handle(handle
);
2592 if ((err
= zonecfg_setnwifent(handle
)) != Z_OK
) {
2594 zperror(cmd_to_str(cmd_num
), B_TRUE
);
2595 zonecfg_fini_handle(handle
);
2598 while (zonecfg_getnwifent(handle
, &nwiftab
) == Z_OK
) {
2600 sa_family_t af
= AF_UNSPEC
;
2601 char dl_owner_zname
[ZONENAME_MAX
];
2602 zoneid_t dl_owner_zid
;
2603 zoneid_t target_zid
;
2606 /* skip any loopback interfaces */
2607 if (strcmp(nwiftab
.zone_nwif_physical
, "lo0") == 0)
2611 if ((res
= zonecfg_valid_net_address(
2612 nwiftab
.zone_nwif_address
, &lifr
)) != Z_OK
) {
2613 print_net_err(nwiftab
.zone_nwif_physical
,
2614 nwiftab
.zone_nwif_address
, af
,
2615 zonecfg_strerror(res
));
2616 return_code
= Z_ERR
;
2619 af
= lifr
.lifr_addr
.ss_family
;
2620 if (!zonecfg_ifname_exists(af
,
2621 nwiftab
.zone_nwif_physical
)) {
2623 * The interface failed to come up. We continue
2624 * on anyway for the sake of consistency: a
2625 * zone is not shut down if the interface fails
2626 * any time after boot, nor does the global zone
2627 * fail to boot if an interface fails.
2629 (void) fprintf(stderr
,
2630 gettext("WARNING: skipping network "
2631 "interface '%s' which may not be "
2632 "present/plumbed in the global "
2634 nwiftab
.zone_nwif_physical
);
2638 /* Warning if it exists for either IPv4 or IPv6 */
2640 if (zonecfg_ifname_exists(AF_INET
,
2641 nwiftab
.zone_nwif_physical
) ||
2642 zonecfg_ifname_exists(AF_INET6
,
2643 nwiftab
.zone_nwif_physical
)) {
2644 (void) fprintf(stderr
,
2645 gettext("WARNING: skipping network "
2646 "interface '%s' which is used in the "
2648 nwiftab
.zone_nwif_physical
);
2653 * Verify that the datalink exists and that it isn't
2654 * already assigned to a zone.
2656 if ((status
= dladm_open(&dh
)) == DLADM_STATUS_OK
) {
2657 status
= dladm_name2info(dh
,
2658 nwiftab
.zone_nwif_physical
, &linkid
, NULL
,
2662 if (status
!= DLADM_STATUS_OK
) {
2663 (void) fprintf(stderr
,
2664 gettext("WARNING: skipping network "
2665 "interface '%s': %s\n"),
2666 nwiftab
.zone_nwif_physical
,
2667 dladm_status2str(status
, errmsg
));
2670 dl_owner_zid
= ALL_ZONES
;
2671 if (zone_check_datalink(&dl_owner_zid
, linkid
) != 0)
2675 * If the zone being verified is
2676 * running and owns the interface
2678 target_zid
= getzoneidbyname(target_zone
);
2679 if (target_zid
== dl_owner_zid
)
2682 /* Zone id match failed, use name to check */
2683 if (getzonenamebyid(dl_owner_zid
, dl_owner_zname
,
2684 ZONENAME_MAX
) < 0) {
2685 /* No name, show ID instead */
2686 (void) snprintf(dl_owner_zname
, ZONENAME_MAX
,
2687 "<%d>", dl_owner_zid
);
2688 } else if (strcmp(dl_owner_zname
, target_zone
) == 0)
2692 * Note here we only report a warning that
2693 * the interface is already in use by another
2694 * running zone, and the verify process just
2695 * goes on, if the interface is still in use
2696 * when this zone really boots up, zoneadmd
2697 * will find it. If the name of the zone which
2698 * owns this interface cannot be determined,
2699 * then it is not possible to determine if there
2700 * is a conflict so just report it as a warning.
2702 (void) fprintf(stderr
,
2703 gettext("WARNING: skipping network interface "
2704 "'%s' which is used by the non-global zone "
2705 "'%s'.\n"), nwiftab
.zone_nwif_physical
,
2710 (void) zonecfg_endnwifent(handle
);
2713 /* verify that lofs has not been excluded from the kernel */
2714 if (!(cmd_num
== CMD_DETACH
|| cmd_num
== CMD_ATTACH
||
2715 cmd_num
== CMD_MOVE
|| cmd_num
== CMD_CLONE
) &&
2716 modctl(MODLOAD
, 1, "fs/lofs", NULL
) != 0) {
2718 (void) fprintf(stderr
, gettext("could not verify "
2719 "lofs(7FS): possibly excluded in /etc/system\n"));
2721 (void) fprintf(stderr
, gettext("could not verify "
2722 "lofs(7FS): %s\n"), strerror(errno
));
2723 return_code
= Z_ERR
;
2726 if (verify_filesystems(handle
) != Z_OK
)
2727 return_code
= Z_ERR
;
2728 if (!in_alt_root
&& verify_rctls(handle
) != Z_OK
)
2729 return_code
= Z_ERR
;
2730 if (!in_alt_root
&& verify_pool(handle
) != Z_OK
)
2731 return_code
= Z_ERR
;
2732 if (!in_alt_root
&& verify_brand(handle
, cmd_num
, argv
) != Z_OK
)
2733 return_code
= Z_ERR
;
2734 if (!in_alt_root
&& verify_datasets(handle
) != Z_OK
)
2735 return_code
= Z_ERR
;
2738 * As the "mount" command is used for patching/upgrading of zones
2739 * or other maintenance processes, the zone's privilege set is not
2740 * checked in this case. Instead, the default, safe set of
2741 * privileges will be used when this zone is created in the
2744 if (!in_alt_root
&& cmd_num
!= CMD_MOUNT
&&
2745 verify_limitpriv(handle
) != Z_OK
)
2746 return_code
= Z_ERR
;
2748 return (return_code
);
2752 verify_details(int cmd_num
, char *argv
[])
2754 zone_dochandle_t handle
;
2755 char zonepath
[MAXPATHLEN
], checkpath
[MAXPATHLEN
];
2756 int return_code
= Z_OK
;
2759 if ((handle
= zonecfg_init_handle()) == NULL
) {
2760 zperror(cmd_to_str(cmd_num
), B_TRUE
);
2763 if ((err
= zonecfg_get_handle(target_zone
, handle
)) != Z_OK
) {
2765 zperror(cmd_to_str(cmd_num
), B_TRUE
);
2766 zonecfg_fini_handle(handle
);
2769 if ((err
= zonecfg_get_zonepath(handle
, zonepath
, sizeof (zonepath
))) !=
2772 zperror(cmd_to_str(cmd_num
), B_TRUE
);
2773 zonecfg_fini_handle(handle
);
2777 * zonecfg_get_zonepath() gets its data from the XML repository.
2778 * Verify this against the index file, which is checked first by
2779 * zone_get_zonepath(). If they don't match, bail out.
2781 if ((err
= zone_get_zonepath(target_zone
, checkpath
,
2782 sizeof (checkpath
))) != Z_OK
) {
2784 zperror2(target_zone
, gettext("could not get zone path"));
2785 zonecfg_fini_handle(handle
);
2788 if (strcmp(zonepath
, checkpath
) != 0) {
2791 * XML and zonepath are literals that should not be translated.
2793 (void) fprintf(stderr
, gettext("The XML repository has "
2794 "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
2795 "These must match, so fix the incorrect entry.\n"),
2796 zonepath
, checkpath
);
2797 zonecfg_fini_handle(handle
);
2800 if (cmd_num
!= CMD_ATTACH
&&
2801 validate_zonepath(zonepath
, cmd_num
) != Z_OK
) {
2802 (void) fprintf(stderr
, gettext("could not verify zonepath %s "
2803 "because of the above errors.\n"), zonepath
);
2804 return_code
= Z_ERR
;
2807 if (verify_handle(cmd_num
, handle
, argv
) != Z_OK
)
2808 return_code
= Z_ERR
;
2810 zonecfg_fini_handle(handle
);
2811 if (return_code
== Z_ERR
)
2812 (void) fprintf(stderr
,
2813 gettext("%s: zone %s failed to verify\n"),
2814 execname
, target_zone
);
2815 return (return_code
);
2819 verify_func(int argc
, char *argv
[])
2824 if ((arg
= getopt(argc
, argv
, "?")) != EOF
) {
2827 sub_usage(SHELP_VERIFY
, CMD_VERIFY
);
2828 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
2830 sub_usage(SHELP_VERIFY
, CMD_VERIFY
);
2834 if (argc
> optind
) {
2835 sub_usage(SHELP_VERIFY
, CMD_VERIFY
);
2838 if (sanity_check(target_zone
, CMD_VERIFY
, B_FALSE
, B_FALSE
, B_FALSE
)
2841 return (verify_details(CMD_VERIFY
, argv
));
2845 addoptions(char *buf
, char *argv
[], size_t len
)
2852 while (argv
[i
] != NULL
) {
2853 if (strlcat(buf
, " ", len
) >= len
||
2854 strlcat(buf
, argv
[i
++], len
) >= len
) {
2855 zerror("Command line too long");
2864 addopt(char *buf
, int opt
, char *optarg
, size_t bufsize
)
2869 (void) sprintf(optstring
, " -%c", opt
);
2871 (void) strcpy(optstring
, " ");
2873 if ((strlcat(buf
, optstring
, bufsize
) > bufsize
))
2876 if ((optarg
!= NULL
) && (strlcat(buf
, optarg
, bufsize
) > bufsize
))
2884 install_func(int argc
, char *argv
[])
2886 char cmdbuf
[MAXPATHLEN
];
2887 char postcmdbuf
[MAXPATHLEN
];
2889 int arg
, err
, subproc_err
;
2890 char zonepath
[MAXPATHLEN
];
2891 brand_handle_t bh
= NULL
;
2893 boolean_t do_postinstall
= B_FALSE
;
2894 boolean_t brand_help
= B_FALSE
;
2897 if (target_zone
== NULL
) {
2898 sub_usage(SHELP_INSTALL
, CMD_INSTALL
);
2902 if (zonecfg_in_alt_root()) {
2903 zerror(gettext("cannot install zone in alternate root"));
2907 if ((err
= zone_get_zonepath(target_zone
, zonepath
,
2908 sizeof (zonepath
))) != Z_OK
) {
2910 zperror2(target_zone
, gettext("could not get zone path"));
2914 /* Fetch the install command from the brand configuration. */
2915 if ((bh
= brand_open(target_brand
)) == NULL
) {
2916 zerror(gettext("missing or invalid brand"));
2920 if (get_hook(bh
, cmdbuf
, sizeof (cmdbuf
), brand_get_install
,
2921 target_zone
, zonepath
) != Z_OK
) {
2922 zerror("invalid brand configuration: missing install resource");
2927 if (get_hook(bh
, postcmdbuf
, sizeof (postcmdbuf
), brand_get_postinstall
,
2928 target_zone
, zonepath
) != Z_OK
) {
2929 zerror("invalid brand configuration: missing postinstall "
2935 if (postcmdbuf
[0] != '\0')
2936 do_postinstall
= B_TRUE
;
2938 (void) strcpy(opts
, "?x:");
2940 * Fetch the list of recognized command-line options from
2941 * the brand configuration file.
2943 if (brand_get_installopts(bh
, opts
+ strlen(opts
),
2944 sizeof (opts
) - strlen(opts
)) != 0) {
2945 zerror("invalid brand configuration: missing "
2946 "install options resource");
2953 if (cmdbuf
[0] == '\0') {
2954 zerror("Missing brand install command");
2958 /* Check the argv string for args we handle internally */
2961 while ((arg
= getopt(argc
, argv
, opts
)) != EOF
) {
2964 if (optopt
== '?') {
2965 sub_usage(SHELP_INSTALL
, CMD_INSTALL
);
2966 brand_help
= B_TRUE
;
2968 /* Ignore unknown options - may be brand specific. */
2971 /* Ignore unknown options - may be brand specific. */
2976 * Append the option to the command line passed to the
2977 * brand-specific install and postinstall routines.
2979 if (addopt(cmdbuf
, optopt
, optarg
, sizeof (cmdbuf
)) != Z_OK
) {
2980 zerror("Install command line too long");
2983 if (addopt(postcmdbuf
, optopt
, optarg
, sizeof (postcmdbuf
))
2985 zerror("Post-Install command line too long");
2990 for (; optind
< argc
; optind
++) {
2991 if (addopt(cmdbuf
, 0, argv
[optind
], sizeof (cmdbuf
)) != Z_OK
) {
2992 zerror("Install command line too long");
2996 if (addopt(postcmdbuf
, 0, argv
[optind
], sizeof (postcmdbuf
))
2998 zerror("Post-Install command line too long");
3004 if (sanity_check(target_zone
, CMD_INSTALL
, B_FALSE
, B_TRUE
,
3007 if (verify_details(CMD_INSTALL
, argv
) != Z_OK
)
3010 if (zonecfg_grab_lock_file(target_zone
, &lockfd
) != Z_OK
) {
3011 zerror(gettext("another %s may have an operation in "
3012 "progress."), "zoneadm");
3015 err
= zone_set_state(target_zone
, ZONE_STATE_INCOMPLETE
);
3018 zperror2(target_zone
, gettext("could not set state"));
3022 create_zfs_zonepath(zonepath
);
3025 status
= do_subproc(cmdbuf
);
3027 subproc_status(gettext("brand-specific installation"), status
,
3028 B_FALSE
)) != ZONE_SUBPROC_OK
) {
3029 if (subproc_err
== ZONE_SUBPROC_USAGE
&& !brand_help
) {
3030 sub_usage(SHELP_INSTALL
, CMD_INSTALL
);
3031 zonecfg_release_lock_file(target_zone
, lockfd
);
3034 errno
= subproc_err
;
3042 if ((err
= zone_set_state(target_zone
, ZONE_STATE_INSTALLED
)) != Z_OK
) {
3044 zperror2(target_zone
, gettext("could not set state"));
3048 if (do_postinstall
) {
3049 status
= do_subproc(postcmdbuf
);
3052 subproc_status(gettext("brand-specific post-install"),
3053 status
, B_FALSE
)) != ZONE_SUBPROC_OK
) {
3054 errno
= subproc_err
;
3056 (void) zone_set_state(target_zone
,
3057 ZONE_STATE_INCOMPLETE
);
3063 * If the install script exited with ZONE_SUBPROC_NOTCOMPLETE, try to
3064 * clean up the zone and leave the zone in the CONFIGURED state so that
3065 * another install can be attempted without requiring an uninstall
3068 if (subproc_err
== ZONE_SUBPROC_NOTCOMPLETE
) {
3071 if ((temp_err
= cleanup_zonepath(zonepath
, B_FALSE
)) != Z_OK
) {
3072 errno
= err
= temp_err
;
3073 zperror2(target_zone
,
3074 gettext("cleaning up zonepath failed"));
3075 } else if ((temp_err
= zone_set_state(target_zone
,
3076 ZONE_STATE_CONFIGURED
)) != Z_OK
) {
3077 errno
= err
= temp_err
;
3078 zperror2(target_zone
, gettext("could not set state"));
3083 zonecfg_release_lock_file(target_zone
, lockfd
);
3084 return ((err
== Z_OK
) ? Z_OK
: Z_ERR
);
3088 warn_dev_match(zone_dochandle_t s_handle
, char *source_zone
,
3089 zone_dochandle_t t_handle
, char *target_zone
)
3092 struct zone_devtab s_devtab
;
3093 struct zone_devtab t_devtab
;
3095 if ((err
= zonecfg_setdevent(t_handle
)) != Z_OK
) {
3097 zperror2(target_zone
, gettext("could not enumerate devices"));
3101 while (zonecfg_getdevent(t_handle
, &t_devtab
) == Z_OK
) {
3102 if ((err
= zonecfg_setdevent(s_handle
)) != Z_OK
) {
3104 zperror2(source_zone
,
3105 gettext("could not enumerate devices"));
3106 (void) zonecfg_enddevent(t_handle
);
3110 while (zonecfg_getdevent(s_handle
, &s_devtab
) == Z_OK
) {
3112 * Use fnmatch to catch the case where wildcards
3113 * were used in one zone and the other has an
3114 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
3115 * /dev/\*dsk/c0t0d0s6).
3117 if (fnmatch(t_devtab
.zone_dev_match
,
3118 s_devtab
.zone_dev_match
, FNM_PATHNAME
) == 0 ||
3119 fnmatch(s_devtab
.zone_dev_match
,
3120 t_devtab
.zone_dev_match
, FNM_PATHNAME
) == 0) {
3121 (void) fprintf(stderr
,
3122 gettext("WARNING: device '%s' "
3123 "is configured in both zones.\n"),
3124 t_devtab
.zone_dev_match
);
3128 (void) zonecfg_enddevent(s_handle
);
3131 (void) zonecfg_enddevent(t_handle
);
3135 * Check if the specified mount option (opt) is contained within the
3139 opt_match(char *opt
, char *options
)
3144 if ((p
= strtok_r(options
, ",", &lastp
)) != NULL
) {
3145 if (strcmp(p
, opt
) == 0)
3147 while ((p
= strtok_r(NULL
, ",", &lastp
)) != NULL
) {
3148 if (strcmp(p
, opt
) == 0)
3156 #define RW_LOFS "WARNING: read-write lofs file system on '%s' is configured " \
3160 print_fs_warnings(struct zone_fstab
*s_fstab
, struct zone_fstab
*t_fstab
)
3163 * It is ok to have shared lofs mounted fs but we want to warn if
3164 * either is rw since this will effect the other zone.
3166 if (strcmp(t_fstab
->zone_fs_type
, "lofs") == 0) {
3169 /* The default is rw so no options means rw */
3170 if (t_fstab
->zone_fs_options
== NULL
||
3171 s_fstab
->zone_fs_options
== NULL
) {
3172 (void) fprintf(stderr
, gettext(RW_LOFS
),
3173 t_fstab
->zone_fs_special
);
3177 for (optp
= s_fstab
->zone_fs_options
; optp
!= NULL
;
3178 optp
= optp
->zone_fsopt_next
) {
3179 if (opt_match("rw", optp
->zone_fsopt_opt
)) {
3180 (void) fprintf(stderr
, gettext(RW_LOFS
),
3181 s_fstab
->zone_fs_special
);
3186 for (optp
= t_fstab
->zone_fs_options
; optp
!= NULL
;
3187 optp
= optp
->zone_fsopt_next
) {
3188 if (opt_match("rw", optp
->zone_fsopt_opt
)) {
3189 (void) fprintf(stderr
, gettext(RW_LOFS
),
3190 t_fstab
->zone_fs_special
);
3200 * The first variable is the file system type and the second is
3201 * the file system special device. For example,
3202 * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ...
3204 (void) fprintf(stderr
, gettext("WARNING: %s file system on '%s' "
3205 "is configured in both zones.\n"), t_fstab
->zone_fs_type
,
3206 t_fstab
->zone_fs_special
);
3210 warn_fs_match(zone_dochandle_t s_handle
, char *source_zone
,
3211 zone_dochandle_t t_handle
, char *target_zone
)
3214 struct zone_fstab s_fstab
;
3215 struct zone_fstab t_fstab
;
3217 if ((err
= zonecfg_setfsent(t_handle
)) != Z_OK
) {
3219 zperror2(target_zone
,
3220 gettext("could not enumerate file systems"));
3224 while (zonecfg_getfsent(t_handle
, &t_fstab
) == Z_OK
) {
3225 if ((err
= zonecfg_setfsent(s_handle
)) != Z_OK
) {
3227 zperror2(source_zone
,
3228 gettext("could not enumerate file systems"));
3229 (void) zonecfg_endfsent(t_handle
);
3233 while (zonecfg_getfsent(s_handle
, &s_fstab
) == Z_OK
) {
3234 if (strcmp(t_fstab
.zone_fs_special
,
3235 s_fstab
.zone_fs_special
) == 0) {
3236 print_fs_warnings(&s_fstab
, &t_fstab
);
3240 (void) zonecfg_endfsent(s_handle
);
3243 (void) zonecfg_endfsent(t_handle
);
3247 * We don't catch the case where you used the same IP address but
3248 * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128.
3249 * However, we're not going to worry about that but we will check for
3250 * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
3251 * and handle that case as a match.
3254 warn_ip_match(zone_dochandle_t s_handle
, char *source_zone
,
3255 zone_dochandle_t t_handle
, char *target_zone
)
3258 struct zone_nwiftab s_nwiftab
;
3259 struct zone_nwiftab t_nwiftab
;
3261 if ((err
= zonecfg_setnwifent(t_handle
)) != Z_OK
) {
3263 zperror2(target_zone
,
3264 gettext("could not enumerate network interfaces"));
3268 while (zonecfg_getnwifent(t_handle
, &t_nwiftab
) == Z_OK
) {
3271 /* remove an (optional) netmask from the address */
3272 if ((p
= strchr(t_nwiftab
.zone_nwif_address
, '/')) != NULL
)
3275 if ((err
= zonecfg_setnwifent(s_handle
)) != Z_OK
) {
3277 zperror2(source_zone
,
3278 gettext("could not enumerate network interfaces"));
3279 (void) zonecfg_endnwifent(t_handle
);
3283 while (zonecfg_getnwifent(s_handle
, &s_nwiftab
) == Z_OK
) {
3284 /* remove an (optional) netmask from the address */
3285 if ((p
= strchr(s_nwiftab
.zone_nwif_address
, '/'))
3289 /* For exclusive-IP zones, address is not specified. */
3290 if (strlen(s_nwiftab
.zone_nwif_address
) == 0)
3293 if (strcmp(t_nwiftab
.zone_nwif_address
,
3294 s_nwiftab
.zone_nwif_address
) == 0) {
3295 (void) fprintf(stderr
,
3296 gettext("WARNING: network address '%s' "
3297 "is configured in both zones.\n"),
3298 t_nwiftab
.zone_nwif_address
);
3302 (void) zonecfg_endnwifent(s_handle
);
3305 (void) zonecfg_endnwifent(t_handle
);
3309 warn_dataset_match(zone_dochandle_t s_handle
, char *source
,
3310 zone_dochandle_t t_handle
, char *target
)
3313 struct zone_dstab s_dstab
;
3314 struct zone_dstab t_dstab
;
3316 if ((err
= zonecfg_setdsent(t_handle
)) != Z_OK
) {
3318 zperror2(target
, gettext("could not enumerate datasets"));
3322 while (zonecfg_getdsent(t_handle
, &t_dstab
) == Z_OK
) {
3323 if ((err
= zonecfg_setdsent(s_handle
)) != Z_OK
) {
3326 gettext("could not enumerate datasets"));
3327 (void) zonecfg_enddsent(t_handle
);
3331 while (zonecfg_getdsent(s_handle
, &s_dstab
) == Z_OK
) {
3332 if (strcmp(t_dstab
.zone_dataset_name
,
3333 s_dstab
.zone_dataset_name
) == 0) {
3334 target_zone
= source
;
3335 zerror(gettext("WARNING: dataset '%s' "
3336 "is configured in both zones.\n"),
3337 t_dstab
.zone_dataset_name
);
3341 (void) zonecfg_enddsent(s_handle
);
3344 (void) zonecfg_enddsent(t_handle
);
3348 * Check that the clone and its source have the same brand type.
3351 valid_brand_clone(char *source_zone
, char *target_zone
)
3354 char source_brand
[MAXNAMELEN
];
3356 if ((zone_get_brand(source_zone
, source_brand
,
3357 sizeof (source_brand
))) != Z_OK
) {
3358 (void) fprintf(stderr
, "%s: zone '%s': %s\n",
3359 execname
, source_zone
, gettext("missing or invalid brand"));
3363 if (strcmp(source_brand
, target_brand
) != 0) {
3364 (void) fprintf(stderr
,
3365 gettext("%s: Zones '%s' and '%s' have different brand "
3366 "types.\n"), execname
, source_zone
, target_zone
);
3370 if ((bh
= brand_open(target_brand
)) == NULL
) {
3371 zerror(gettext("missing or invalid brand"));
3379 validate_clone(char *source_zone
, char *target_zone
)
3382 zone_dochandle_t s_handle
;
3383 zone_dochandle_t t_handle
;
3385 if ((t_handle
= zonecfg_init_handle()) == NULL
) {
3386 zperror(cmd_to_str(CMD_CLONE
), B_TRUE
);
3389 if ((err
= zonecfg_get_handle(target_zone
, t_handle
)) != Z_OK
) {
3391 zperror(cmd_to_str(CMD_CLONE
), B_TRUE
);
3392 zonecfg_fini_handle(t_handle
);
3396 if ((s_handle
= zonecfg_init_handle()) == NULL
) {
3397 zperror(cmd_to_str(CMD_CLONE
), B_TRUE
);
3398 zonecfg_fini_handle(t_handle
);
3401 if ((err
= zonecfg_get_handle(source_zone
, s_handle
)) != Z_OK
) {
3403 zperror(cmd_to_str(CMD_CLONE
), B_TRUE
);
3407 /* verify new zone has same brand type */
3408 err
= valid_brand_clone(source_zone
, target_zone
);
3412 /* warn about imported fs's which are the same */
3413 warn_fs_match(s_handle
, source_zone
, t_handle
, target_zone
);
3415 /* warn about imported IP addresses which are the same */
3416 warn_ip_match(s_handle
, source_zone
, t_handle
, target_zone
);
3418 /* warn about imported devices which are the same */
3419 warn_dev_match(s_handle
, source_zone
, t_handle
, target_zone
);
3421 /* warn about imported datasets which are the same */
3422 warn_dataset_match(s_handle
, source_zone
, t_handle
, target_zone
);
3425 zonecfg_fini_handle(t_handle
);
3426 zonecfg_fini_handle(s_handle
);
3428 return ((err
== Z_OK
) ? Z_OK
: Z_ERR
);
3432 copy_zone(char *src
, char *dst
)
3434 boolean_t out_null
= B_FALSE
;
3437 char cmdbuf
[MAXPATHLEN
* 2 + 128];
3439 if ((outfile
= tempnam("/var/log", "zone")) == NULL
) {
3440 outfile
= "/dev/null";
3445 * Use find to get the list of files to copy. We need to skip
3446 * files of type "socket" since cpio can't handle those but that
3447 * should be ok since the app will recreate the socket when it runs.
3448 * We also need to filter out anything under the .zfs subdir. Since
3449 * find is running depth-first, we need the extra egrep to filter .zfs.
3451 (void) snprintf(cmdbuf
, sizeof (cmdbuf
),
3452 "cd %s && /usr/bin/find . -type s -prune -o -depth -print | "
3453 "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
3454 "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
3457 status
= do_subproc(cmdbuf
);
3459 if (subproc_status("copy", status
, B_TRUE
) != ZONE_SUBPROC_OK
) {
3461 (void) fprintf(stderr
, gettext("\nThe copy failed.\n"
3462 "More information can be found in %s\n"), outfile
);
3467 (void) unlink(outfile
);
3474 zfm_print(const struct mnttab
*p
, void *r
)
3476 zerror(" %s\n", p
->mnt_mountp
);
3481 clone_copy(char *source_zonepath
, char *zonepath
)
3485 /* Don't clone the zone if anything is still mounted there */
3486 if (zonecfg_find_mounts(source_zonepath
, NULL
, NULL
)) {
3487 zerror(gettext("These file systems are mounted on "
3488 "subdirectories of %s.\n"), source_zonepath
);
3489 (void) zonecfg_find_mounts(source_zonepath
, zfm_print
, NULL
);
3494 * Attempt to create a ZFS fs for the zonepath. As usual, we don't
3495 * care if this works or not since we always have the default behavior
3496 * of a simple directory for the zonepath.
3498 create_zfs_zonepath(zonepath
);
3500 (void) printf(gettext("Copying %s..."), source_zonepath
);
3501 (void) fflush(stdout
);
3503 err
= copy_zone(source_zonepath
, zonepath
);
3505 (void) printf("\n");
3511 clone_func(int argc
, char *argv
[])
3513 char *source_zone
= NULL
;
3516 char zonepath
[MAXPATHLEN
];
3517 char source_zonepath
[MAXPATHLEN
];
3520 char *method
= NULL
;
3521 char *snapshot
= NULL
;
3522 char cmdbuf
[MAXPATHLEN
];
3523 char postcmdbuf
[MAXPATHLEN
];
3524 char presnapbuf
[MAXPATHLEN
];
3525 char postsnapbuf
[MAXPATHLEN
];
3526 char validsnapbuf
[MAXPATHLEN
];
3527 brand_handle_t bh
= NULL
;
3529 boolean_t brand_help
= B_FALSE
;
3531 if (zonecfg_in_alt_root()) {
3532 zerror(gettext("cannot clone zone in alternate root"));
3536 /* Check the argv string for args we handle internally */
3539 while ((arg
= getopt(argc
, argv
, "?m:s:")) != EOF
) {
3542 if (optopt
== '?') {
3543 sub_usage(SHELP_CLONE
, CMD_CLONE
);
3544 brand_help
= B_TRUE
;
3546 /* Ignore unknown options - may be brand specific. */
3555 /* Ignore unknown options - may be brand specific. */
3560 if (argc
!= (optind
+ 1)) {
3561 sub_usage(SHELP_CLONE
, CMD_CLONE
);
3565 source_zone
= argv
[optind
];
3568 if (sanity_check(target_zone
, CMD_CLONE
, B_FALSE
, B_TRUE
,
3571 if (verify_details(CMD_CLONE
, argv
) != Z_OK
)
3575 * We also need to do some extra validation on the source zone.
3577 if (strcmp(source_zone
, GLOBAL_ZONENAME
) == 0) {
3578 zerror(gettext("%s operation is invalid for the "
3579 "global zone."), cmd_to_str(CMD_CLONE
));
3583 if (strncmp(source_zone
, "SUNW", 4) == 0) {
3584 zerror(gettext("%s operation is invalid for zones "
3585 "starting with SUNW."), cmd_to_str(CMD_CLONE
));
3589 if (auth_check(username
, source_zone
, SOURCE_ZONE
) == Z_ERR
) {
3590 zerror(gettext("%s operation is invalid because "
3591 "user is not authorized to read source zone."),
3592 cmd_to_str(CMD_CLONE
));
3596 zent
= lookup_running_zone(source_zone
);
3598 /* check whether the zone is ready or running */
3599 if ((err
= zone_get_state(zent
->zname
,
3600 &zent
->zstate_num
)) != Z_OK
) {
3602 zperror2(zent
->zname
, gettext("could not get "
3604 /* can't tell, so hedge */
3605 zent
->zstate_str
= "ready/running";
3608 zone_state_str(zent
->zstate_num
);
3610 zerror(gettext("%s operation is invalid for %s zones."),
3611 cmd_to_str(CMD_CLONE
), zent
->zstate_str
);
3615 if ((err
= zone_get_state(source_zone
, &state
)) != Z_OK
) {
3617 zperror2(source_zone
, gettext("could not get state"));
3620 if (state
!= ZONE_STATE_INSTALLED
) {
3621 (void) fprintf(stderr
,
3622 gettext("%s: zone %s is %s; %s is required.\n"),
3623 execname
, source_zone
, zone_state_str(state
),
3624 zone_state_str(ZONE_STATE_INSTALLED
));
3629 * The source zone checks out ok, continue with the clone.
3632 if (validate_clone(source_zone
, target_zone
) != Z_OK
)
3635 if (zonecfg_grab_lock_file(target_zone
, &lockfd
) != Z_OK
) {
3636 zerror(gettext("another %s may have an operation in "
3637 "progress."), "zoneadm");
3642 if ((err
= zone_get_zonepath(source_zone
, source_zonepath
,
3643 sizeof (source_zonepath
))) != Z_OK
) {
3645 zperror2(source_zone
, gettext("could not get zone path"));
3649 if ((err
= zone_get_zonepath(target_zone
, zonepath
, sizeof (zonepath
)))
3652 zperror2(target_zone
, gettext("could not get zone path"));
3657 * Fetch the clone and postclone hooks from the brand configuration.
3659 if ((bh
= brand_open(target_brand
)) == NULL
) {
3660 zerror(gettext("missing or invalid brand"));
3665 if (get_hook(bh
, cmdbuf
, sizeof (cmdbuf
), brand_get_clone
, target_zone
,
3666 zonepath
) != Z_OK
) {
3667 zerror("invalid brand configuration: missing clone resource");
3673 if (get_hook(bh
, postcmdbuf
, sizeof (postcmdbuf
), brand_get_postclone
,
3674 target_zone
, zonepath
) != Z_OK
) {
3675 zerror("invalid brand configuration: missing postclone "
3682 if (get_hook(bh
, presnapbuf
, sizeof (presnapbuf
), brand_get_presnap
,
3683 source_zone
, source_zonepath
) != Z_OK
) {
3684 zerror("invalid brand configuration: missing presnap "
3691 if (get_hook(bh
, postsnapbuf
, sizeof (postsnapbuf
), brand_get_postsnap
,
3692 source_zone
, source_zonepath
) != Z_OK
) {
3693 zerror("invalid brand configuration: missing postsnap "
3700 if (get_hook(bh
, validsnapbuf
, sizeof (validsnapbuf
),
3701 brand_get_validatesnap
, target_zone
, zonepath
) != Z_OK
) {
3702 zerror("invalid brand configuration: missing validatesnap "
3710 /* Append all options to clone hook. */
3711 if (addoptions(cmdbuf
, argv
, sizeof (cmdbuf
)) != Z_OK
) {
3716 /* Append all options to postclone hook. */
3717 if (addoptions(postcmdbuf
, argv
, sizeof (postcmdbuf
)) != Z_OK
) {
3723 if ((err
= zone_set_state(target_zone
, ZONE_STATE_INCOMPLETE
))
3726 zperror2(target_zone
, gettext("could not set state"));
3732 * The clone hook is optional. If it exists, use the hook for
3733 * cloning, otherwise use the built-in clone support
3735 if (cmdbuf
[0] != '\0') {
3736 /* Run the clone hook */
3737 status
= do_subproc(cmdbuf
);
3738 if ((status
= subproc_status(gettext("brand-specific clone"),
3739 status
, B_FALSE
)) != ZONE_SUBPROC_OK
) {
3740 if (status
== ZONE_SUBPROC_USAGE
&& !brand_help
)
3741 sub_usage(SHELP_CLONE
, CMD_CLONE
);
3750 /* If just help, we're done since there is no brand help. */
3754 /* Run the built-in clone support. */
3756 /* The only explicit built-in method is "copy". */
3757 if (method
!= NULL
&& strcmp(method
, "copy") != 0) {
3758 sub_usage(SHELP_CLONE
, CMD_CLONE
);
3763 if (snapshot
!= NULL
) {
3764 err
= clone_snapshot_zfs(snapshot
, zonepath
,
3768 * We always copy the clone unless the source is ZFS
3769 * and a ZFS clone worked. We fallback to copying if
3770 * the ZFS clone fails for some reason.
3773 if (method
== NULL
&& is_zonepath_zfs(source_zonepath
))
3774 err
= clone_zfs(source_zonepath
, zonepath
,
3775 presnapbuf
, postsnapbuf
);
3778 err
= clone_copy(source_zonepath
, zonepath
);
3782 if (err
== Z_OK
&& postcmdbuf
[0] != '\0') {
3783 status
= do_subproc(postcmdbuf
);
3784 if ((err
= subproc_status("postclone", status
, B_FALSE
))
3785 != ZONE_SUBPROC_OK
) {
3786 zerror(gettext("post-clone configuration failed."));
3793 * If everything went well, we mark the zone as installed.
3796 err
= zone_set_state(target_zone
, ZONE_STATE_INSTALLED
);
3799 zperror2(target_zone
, gettext("could not set state"));
3803 zonecfg_release_lock_file(target_zone
, lockfd
);
3804 return ((err
== Z_OK
) ? Z_OK
: Z_ERR
);
3808 * Used when removing a zonepath after uninstalling or cleaning up after
3809 * the move subcommand. This handles a zonepath that has non-standard
3810 * contents so that we will only cleanup the stuff we know about and leave
3811 * any user data alone.
3813 * If the "all" parameter is true then we should remove the whole zonepath
3814 * even if it has non-standard files/directories in it. This can be used when
3815 * we need to cleanup after moving the zonepath across file systems.
3817 * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND
3818 * and not the shell.
3821 cleanup_zonepath(char *zonepath
, boolean_t all
)
3825 boolean_t non_std
= B_FALSE
;
3829 * The SUNWattached.xml file is expected since it might
3830 * exist if the zone was force-attached after a
3833 char *std_entries
[] = {"dev", "lu", "root",
3834 "SUNWattached.xml", NULL
};
3835 /* (MAXPATHLEN * 3) is for the 3 std_entries dirs */
3836 char cmdbuf
[sizeof (RMCOMMAND
) + (MAXPATHLEN
* 3) + 64];
3839 * We shouldn't need these checks but lets be paranoid since we
3840 * could blow away the whole system here if we got the wrong zonepath.
3842 if (*zonepath
== '\0'|| strcmp(zonepath
, "/") == 0) {
3843 (void) fprintf(stderr
, "invalid zonepath '%s'\n", zonepath
);
3848 * If the dirpath is already gone (maybe it was manually removed) then
3849 * we just return Z_OK so that the cleanup is successful.
3851 if ((dirp
= opendir(zonepath
)) == NULL
)
3855 * Look through the zonepath directory to see if there are any
3856 * non-standard files/dirs. Also skip .zfs since that might be
3857 * there but we'll handle ZFS file systems as a special case.
3859 while ((dp
= readdir(dirp
)) != NULL
) {
3860 if (strcmp(dp
->d_name
, ".") == 0 ||
3861 strcmp(dp
->d_name
, "..") == 0 ||
3862 strcmp(dp
->d_name
, ".zfs") == 0)
3865 for (i
= 0; std_entries
[i
] != NULL
; i
++)
3866 if (strcmp(dp
->d_name
, std_entries
[i
]) == 0)
3869 if (std_entries
[i
] == NULL
)
3872 (void) closedir(dirp
);
3874 if (!all
&& non_std
) {
3876 * There are extra, non-standard directories/files in the
3877 * zonepath so we don't want to remove the zonepath. We
3878 * just want to remove the standard directories and leave
3879 * the user data alone.
3881 (void) snprintf(cmdbuf
, sizeof (cmdbuf
), "exec " RMCOMMAND
);
3883 for (i
= 0; std_entries
[i
] != NULL
; i
++) {
3884 char tmpbuf
[MAXPATHLEN
];
3886 if (snprintf(tmpbuf
, sizeof (tmpbuf
), " %s/%s",
3887 zonepath
, std_entries
[i
]) >= sizeof (tmpbuf
) ||
3888 strlcat(cmdbuf
, tmpbuf
, sizeof (cmdbuf
)) >=
3890 (void) fprintf(stderr
,
3891 gettext("path is too long\n"));
3896 status
= do_subproc(cmdbuf
);
3898 (void) fprintf(stderr
, gettext("WARNING: Unable to completely "
3899 "remove %s\nbecause it contains additional user data. "
3900 "Only the standard directory\nentries have been "
3904 return ((subproc_status(RMCOMMAND
, status
, B_TRUE
) ==
3905 ZONE_SUBPROC_OK
) ? Z_OK
: Z_ERR
);
3909 * There is nothing unexpected in the zonepath, try to get rid of the
3910 * whole zonepath directory.
3912 * If the zonepath is its own zfs file system, try to destroy the
3913 * file system. If that fails for some reason (e.g. it has clones)
3914 * then we'll just remove the contents of the zonepath.
3916 if (is_zonepath_zfs(zonepath
)) {
3917 if (destroy_zfs(zonepath
) == Z_OK
)
3919 (void) snprintf(cmdbuf
, sizeof (cmdbuf
), "exec " RMCOMMAND
3921 status
= do_subproc(cmdbuf
);
3922 return ((subproc_status(RMCOMMAND
, status
, B_TRUE
) ==
3923 ZONE_SUBPROC_OK
) ? Z_OK
: Z_ERR
);
3926 (void) snprintf(cmdbuf
, sizeof (cmdbuf
), "exec " RMCOMMAND
" %s",
3928 status
= do_subproc(cmdbuf
);
3930 return ((subproc_status(RMCOMMAND
, status
, B_TRUE
) == ZONE_SUBPROC_OK
)
3935 move_func(int argc
, char *argv
[])
3937 char *new_zonepath
= NULL
;
3940 char zonepath
[MAXPATHLEN
];
3941 zone_dochandle_t handle
;
3943 boolean_t is_zfs
= B_FALSE
;
3944 boolean_t root_fs_mounted
= B_FALSE
;
3947 boolean_t empty
= B_TRUE
;
3949 struct stat zonepath_buf
;
3950 struct stat new_zonepath_buf
;
3951 zone_mounts_t mounts
;
3953 if (zonecfg_in_alt_root()) {
3954 zerror(gettext("cannot move zone in alternate root"));
3959 if ((arg
= getopt(argc
, argv
, "?")) != EOF
) {
3962 sub_usage(SHELP_MOVE
, CMD_MOVE
);
3963 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
3965 sub_usage(SHELP_MOVE
, CMD_MOVE
);
3969 if (argc
!= (optind
+ 1)) {
3970 sub_usage(SHELP_MOVE
, CMD_MOVE
);
3973 new_zonepath
= argv
[optind
];
3974 if (sanity_check(target_zone
, CMD_MOVE
, B_FALSE
, B_TRUE
, B_FALSE
)
3977 if (verify_details(CMD_MOVE
, argv
) != Z_OK
)
3981 * Check out the new zonepath. This has the side effect of creating
3982 * a directory for the new zonepath. We depend on this later when we
3983 * stat to see if we are doing a cross file system move or not.
3985 if (validate_zonepath(new_zonepath
, CMD_MOVE
) != Z_OK
)
3988 if ((err
= zone_get_zonepath(target_zone
, zonepath
, sizeof (zonepath
)))
3991 zperror2(target_zone
, gettext("could not get zone path"));
3995 if (stat(zonepath
, &zonepath_buf
) == -1) {
3996 zperror(gettext("could not stat zone path"), B_FALSE
);
4000 if (stat(new_zonepath
, &new_zonepath_buf
) == -1) {
4001 zperror(gettext("could not stat new zone path"), B_FALSE
);
4006 * Check if the destination directory is empty.
4008 if ((dirp
= opendir(new_zonepath
)) == NULL
) {
4009 zperror(gettext("could not open new zone path"), B_FALSE
);
4012 while ((dp
= readdir(dirp
)) != NULL
) {
4013 if (strcmp(dp
->d_name
, ".") == 0 ||
4014 strcmp(dp
->d_name
, "..") == 0)
4019 (void) closedir(dirp
);
4021 /* Error if there is anything in the destination directory. */
4023 (void) fprintf(stderr
, gettext("could not move zone to %s: "
4024 "directory not empty\n"), new_zonepath
);
4029 * Collect information about mounts within the zone's zonepath.
4030 * Overlay mounts on the zone's root directory are erroneous.
4031 * Bail if we encounter any unexpected mounts.
4033 if (zone_mounts_init(&mounts
, zonepath
) != 0)
4035 if (mounts
.num_root_overlay_mounts
!= 0) {
4036 zerror(gettext("%d overlay mount(s) detected on %s/root."),
4037 mounts
.num_root_overlay_mounts
, zonepath
);
4038 goto err_and_mounts_destroy
;
4040 if (mounts
.num_unexpected_mounts
!= 0)
4041 goto err_and_mounts_destroy
;
4044 * Check if we are moving in the same file system and can do a fast
4045 * move or if we are crossing file systems and have to copy the data.
4047 fast
= (zonepath_buf
.st_dev
== new_zonepath_buf
.st_dev
);
4049 if ((handle
= zonecfg_init_handle()) == NULL
) {
4050 zperror(cmd_to_str(CMD_MOVE
), B_TRUE
);
4051 goto err_and_mounts_destroy
;
4054 if ((err
= zonecfg_get_handle(target_zone
, handle
)) != Z_OK
) {
4056 zperror(cmd_to_str(CMD_MOVE
), B_TRUE
);
4057 goto err_and_fini_handle
;
4060 if (zonecfg_grab_lock_file(target_zone
, &lockfd
) != Z_OK
) {
4061 zerror(gettext("another %s may have an operation in progress."),
4063 goto err_and_fini_handle
;
4067 * Unmount the zone's root filesystem before we move the zone's
4070 if (zone_unmount_rootfs(&mounts
, zonepath
, B_FALSE
) != 0)
4071 goto err_and_rele_lockfile
;
4074 * We're making some file system changes now so we have to clean up
4075 * the file system before we are done. This will either clean up the
4076 * new zonepath if the zonecfg update failed or it will clean up the
4077 * old zonepath if everything is ok.
4081 if (is_zonepath_zfs(zonepath
) &&
4082 move_zfs(zonepath
, new_zonepath
) != Z_ERR
) {
4086 /* same file system, use rename for a quick move */
4089 * Remove the new_zonepath directory that got created above
4090 * during the validation. It gets in the way of the rename.
4092 if (rmdir(new_zonepath
) != 0) {
4093 zperror(gettext("could not rmdir new zone path"),
4095 (void) zone_mount_rootfs(&mounts
, zonepath
);
4096 goto err_and_rele_lockfile
;
4099 if (rename(zonepath
, new_zonepath
) != 0) {
4101 * If this fails we don't need to do all of the
4102 * cleanup that happens for the rest of the code
4103 * so just return from this error.
4105 zperror(gettext("could not move zone"), B_FALSE
);
4106 (void) zone_mount_rootfs(&mounts
, zonepath
);
4107 goto err_and_rele_lockfile
;
4112 * Attempt to create a ZFS fs for the new zonepath. As usual,
4113 * we don't care if this works or not since we always have the
4114 * default behavior of a simple directory for the zonepath.
4116 create_zfs_zonepath(new_zonepath
);
4118 (void) printf(gettext(
4119 "Moving across file systems; copying zonepath %s..."),
4121 (void) fflush(stdout
);
4123 err
= copy_zone(zonepath
, new_zonepath
);
4125 (void) printf("\n");
4131 * Mount the zone's root filesystem in the new zonepath if there was
4132 * a root mount prior to the move.
4134 if (zone_mount_rootfs(&mounts
, new_zonepath
) != 0) {
4138 root_fs_mounted
= B_TRUE
;
4140 if ((err
= zonecfg_set_zonepath(handle
, new_zonepath
)) != Z_OK
) {
4142 zperror(gettext("could not set new zonepath"), B_TRUE
);
4146 if ((err
= zonecfg_save(handle
)) != Z_OK
) {
4148 zperror(gettext("zonecfg save failed"), B_TRUE
);
4155 zonecfg_fini_handle(handle
);
4156 zonecfg_release_lock_file(target_zone
, lockfd
);
4159 * Clean up the file system based on how things went. We either
4160 * clean up the new zonepath if the operation failed for some reason
4161 * or we clean up the old zonepath if everything is ok.
4165 * Check for the unlikely scenario in which the zone's
4166 * zonepath and its root file system moved but libzonecfg
4167 * couldn't save the new zonepath to the zone's configuration
4168 * file. The mounted root filesystem must be unmounted before
4169 * zoneadm restores the zone's zonepath.
4171 if (root_fs_mounted
&& zone_unmount_rootfs(&mounts
,
4172 new_zonepath
, B_TRUE
) != 0) {
4174 * We can't forcibly unmount the zone's root file system
4175 * from the new zonepath. Bail!
4177 zerror(gettext("fatal error: cannot unmount %s/root\n"),
4179 goto err_and_mounts_destroy
;
4182 /* The zonecfg update failed, cleanup the new zonepath. */
4184 if (move_zfs(new_zonepath
, zonepath
) == Z_ERR
) {
4185 (void) fprintf(stderr
, gettext("could not "
4186 "restore zonepath, the zfs mountpoint is "
4187 "set as:\n%s\n"), new_zonepath
);
4189 * err is already != Z_OK since we're reverting
4192 (void) zone_mount_rootfs(&mounts
, zonepath
);
4195 if (rename(new_zonepath
, zonepath
) != 0) {
4196 zperror(gettext("could not restore zonepath"),
4199 * err is already != Z_OK since we're reverting
4202 (void) zone_mount_rootfs(&mounts
, zonepath
);
4205 (void) printf(gettext("Cleaning up zonepath %s..."),
4207 (void) fflush(stdout
);
4208 err
= cleanup_zonepath(new_zonepath
, B_TRUE
);
4209 (void) printf("\n");
4213 zperror(gettext("could not remove new "
4214 "zonepath"), B_TRUE
);
4217 * Because we're reverting we know the mainline
4218 * code failed but we just reused the err
4219 * variable so we reset it back to Z_ERR.
4224 (void) zone_mount_rootfs(&mounts
, zonepath
);
4227 /* The move was successful, cleanup the old zonepath. */
4228 if (!is_zfs
&& !fast
) {
4230 gettext("Cleaning up zonepath %s..."), zonepath
);
4231 (void) fflush(stdout
);
4232 err
= cleanup_zonepath(zonepath
, B_TRUE
);
4233 (void) printf("\n");
4237 zperror(gettext("could not remove zonepath"),
4243 zone_mounts_destroy(&mounts
);
4244 return ((err
== Z_OK
) ? Z_OK
: Z_ERR
);
4246 err_and_rele_lockfile
:
4247 zonecfg_release_lock_file(target_zone
, lockfd
);
4248 err_and_fini_handle
:
4249 zonecfg_fini_handle(handle
);
4250 err_and_mounts_destroy
:
4251 zone_mounts_destroy(&mounts
);
4257 detach_func(int argc
, char *argv
[])
4261 char zonepath
[MAXPATHLEN
];
4262 char cmdbuf
[MAXPATHLEN
];
4263 char precmdbuf
[MAXPATHLEN
];
4264 boolean_t execute
= B_TRUE
;
4265 boolean_t brand_help
= B_FALSE
;
4266 brand_handle_t bh
= NULL
;
4269 if (zonecfg_in_alt_root()) {
4270 zerror(gettext("cannot detach zone in alternate root"));
4274 /* Check the argv string for args we handle internally */
4277 while ((arg
= getopt(argc
, argv
, "?n")) != EOF
) {
4280 if (optopt
== '?') {
4281 sub_usage(SHELP_DETACH
, CMD_DETACH
);
4282 brand_help
= B_TRUE
;
4284 /* Ignore unknown options - may be brand specific. */
4290 /* Ignore unknown options - may be brand specific. */
4299 if (sanity_check(target_zone
, CMD_DETACH
, B_FALSE
, B_TRUE
,
4302 if (verify_details(CMD_DETACH
, argv
) != Z_OK
)
4306 * We want a dry-run to work for a non-privileged user so we
4307 * only do minimal validation.
4309 if (target_zone
== NULL
) {
4310 zerror(gettext("no zone specified"));
4314 if (strcmp(target_zone
, GLOBAL_ZONENAME
) == 0) {
4315 zerror(gettext("%s operation is invalid for the "
4316 "global zone."), cmd_to_str(CMD_DETACH
));
4321 if ((err
= zone_get_zonepath(target_zone
, zonepath
, sizeof (zonepath
)))
4324 zperror2(target_zone
, gettext("could not get zone path"));
4328 /* Fetch the detach and predetach hooks from the brand configuration. */
4329 if ((bh
= brand_open(target_brand
)) == NULL
) {
4330 zerror(gettext("missing or invalid brand"));
4334 if (get_hook(bh
, cmdbuf
, sizeof (cmdbuf
), brand_get_detach
, target_zone
,
4335 zonepath
) != Z_OK
) {
4336 zerror("invalid brand configuration: missing detach resource");
4341 if (get_hook(bh
, precmdbuf
, sizeof (precmdbuf
), brand_get_predetach
,
4342 target_zone
, zonepath
) != Z_OK
) {
4343 zerror("invalid brand configuration: missing predetach "
4350 /* Append all options to predetach hook. */
4351 if (addoptions(precmdbuf
, argv
, sizeof (precmdbuf
)) != Z_OK
)
4354 /* Append all options to detach hook. */
4355 if (addoptions(cmdbuf
, argv
, sizeof (cmdbuf
)) != Z_OK
)
4358 if (execute
&& zonecfg_grab_lock_file(target_zone
, &lockfd
) != Z_OK
) {
4359 zerror(gettext("another %s may have an operation in progress."),
4364 /* If we have a brand predetach hook, run it. */
4365 if (!brand_help
&& precmdbuf
[0] != '\0') {
4366 status
= do_subproc(precmdbuf
);
4367 if (subproc_status(gettext("brand-specific predetach"),
4368 status
, B_FALSE
) != ZONE_SUBPROC_OK
) {
4371 assert(lockfd
>= 0);
4372 zonecfg_release_lock_file(target_zone
, lockfd
);
4376 assert(lockfd
== -1);
4381 if (cmdbuf
[0] != '\0') {
4382 /* Run the detach hook */
4383 status
= do_subproc(cmdbuf
);
4384 if ((status
= subproc_status(gettext("brand-specific detach"),
4385 status
, B_FALSE
)) != ZONE_SUBPROC_OK
) {
4386 if (status
== ZONE_SUBPROC_USAGE
&& !brand_help
)
4387 sub_usage(SHELP_DETACH
, CMD_DETACH
);
4390 assert(lockfd
>= 0);
4391 zonecfg_release_lock_file(target_zone
, lockfd
);
4395 assert(lockfd
== -1);
4400 zone_dochandle_t handle
;
4402 /* If just help, we're done since there is no brand help. */
4404 assert(lockfd
== -1);
4409 * Run the built-in detach support. Just generate a simple
4410 * zone definition XML file and detach.
4413 /* Don't detach the zone if anything is still mounted there */
4414 if (execute
&& zonecfg_find_mounts(zonepath
, NULL
, NULL
)) {
4415 (void) fprintf(stderr
, gettext("These file systems are "
4416 "mounted on subdirectories of %s.\n"), zonepath
);
4417 (void) zonecfg_find_mounts(zonepath
, zfm_print
, NULL
);
4418 err
= ZONE_SUBPROC_NOTCOMPLETE
;
4422 if ((handle
= zonecfg_init_handle()) == NULL
) {
4423 zperror(cmd_to_str(CMD_DETACH
), B_TRUE
);
4424 err
= ZONE_SUBPROC_NOTCOMPLETE
;
4428 if ((err
= zonecfg_get_handle(target_zone
, handle
)) != Z_OK
) {
4430 zperror(cmd_to_str(CMD_DETACH
), B_TRUE
);
4432 } else if ((err
= zonecfg_detach_save(handle
,
4433 (execute
? 0 : ZONE_DRY_RUN
))) != Z_OK
) {
4435 zperror(gettext("saving the detach manifest failed"),
4439 zonecfg_fini_handle(handle
);
4445 * Set the zone state back to configured unless we are running with the
4446 * no-execute option.
4448 if (execute
&& (err
= zone_set_state(target_zone
,
4449 ZONE_STATE_CONFIGURED
)) != Z_OK
) {
4451 zperror(gettext("could not reset state"), B_TRUE
);
4456 assert(lockfd
>= 0);
4457 zonecfg_release_lock_file(target_zone
, lockfd
);
4461 assert(lockfd
== -1);
4462 return ((err
== Z_OK
) ? Z_OK
: Z_ERR
);
4466 * Determine the brand when doing a dry-run attach. The zone does not have to
4467 * exist, so we have to read the incoming manifest to determine the zone's
4470 * Because the manifest has to be processed twice; once to determine the brand
4471 * and once to do the brand-specific attach logic, we always read it into a tmp
4472 * file. This handles the manifest coming from stdin or a regular file. The
4473 * tmpname parameter returns the name of the temporary file that the manifest
4477 dryrun_get_brand(char *manifest_path
, char *tmpname
, int size
)
4482 zone_dochandle_t local_handle
;
4483 zone_dochandle_t rem_handle
= NULL
;
4488 if (strcmp(manifest_path
, "-") == 0) {
4491 if ((fd
= open(manifest_path
, O_RDONLY
)) < 0) {
4492 if (getcwd(buf
, sizeof (buf
)) == NULL
)
4493 (void) strlcpy(buf
, "/", sizeof (buf
));
4494 zerror(gettext("could not open manifest path %s%s: %s"),
4495 (*manifest_path
== '/' ? "" : buf
), manifest_path
,
4501 (void) snprintf(tmpname
, size
, "/var/run/zone.%d", getpid());
4503 if ((ofd
= open(tmpname
, O_WRONLY
| O_CREAT
, S_IRUSR
| S_IWUSR
)) < 0) {
4504 zperror(gettext("could not save manifest"), B_FALSE
);
4509 while ((len
= read(fd
, buf
, sizeof (buf
))) > 0) {
4510 if (write(ofd
, buf
, len
) == -1) {
4511 zperror(gettext("could not save manifest"), B_FALSE
);
4518 if (close(ofd
) != 0) {
4519 zperror(gettext("could not save manifest"), B_FALSE
);
4526 if ((fd
= open(tmpname
, O_RDONLY
)) < 0) {
4527 zperror(gettext("could not open manifest path"), B_FALSE
);
4531 if ((local_handle
= zonecfg_init_handle()) == NULL
) {
4532 zperror(cmd_to_str(CMD_ATTACH
), B_TRUE
);
4537 if ((rem_handle
= zonecfg_init_handle()) == NULL
) {
4538 zperror(cmd_to_str(CMD_ATTACH
), B_TRUE
);
4543 if ((err
= zonecfg_attach_manifest(fd
, local_handle
, rem_handle
))
4547 if (err
== Z_INVALID_DOCUMENT
) {
4551 if (strcmp(manifest_path
, "-") == 0) {
4552 zerror(gettext("Input is not a valid XML "
4557 if (fstat(fd
, &st
) == -1 || !S_ISREG(st
.st_mode
)) {
4558 zerror(gettext("%s is not an XML file"),
4563 bzero(buf
, sizeof (buf
));
4564 (void) lseek(fd
, 0L, SEEK_SET
);
4565 if (read(fd
, buf
, sizeof (buf
) - 1) < 0 ||
4566 strncmp(buf
, "<?xml", 5) != 0)
4567 zerror(gettext("%s is not an XML file"),
4570 zerror(gettext("Cannot attach to an earlier "
4571 "release of the operating system"));
4573 zperror(cmd_to_str(CMD_ATTACH
), B_TRUE
);
4578 /* Retrieve remote handle brand type. */
4579 if (zonecfg_get_brand(rem_handle
, target_brand
, sizeof (target_brand
))
4581 zerror(gettext("missing or invalid brand"));
4586 zonecfg_fini_handle(local_handle
);
4587 zonecfg_fini_handle(rem_handle
);
4590 return ((res
== Z_OK
) ? Z_OK
: Z_ERR
);
4595 attach_func(int argc
, char *argv
[])
4599 boolean_t force
= B_FALSE
;
4600 zone_dochandle_t handle
;
4601 char zonepath
[MAXPATHLEN
];
4602 char cmdbuf
[MAXPATHLEN
];
4603 char postcmdbuf
[MAXPATHLEN
];
4604 boolean_t execute
= B_TRUE
;
4605 boolean_t brand_help
= B_FALSE
;
4606 char *manifest_path
;
4607 char tmpmanifest
[80];
4609 brand_handle_t bh
= NULL
;
4614 boolean_t forced_update
= B_FALSE
;
4616 if (zonecfg_in_alt_root()) {
4617 zerror(gettext("cannot attach zone in alternate root"));
4621 /* Check the argv string for args we handle internally */
4624 while ((arg
= getopt(argc
, argv
, "?Fn:U")) != EOF
) {
4627 if (optopt
== '?') {
4628 sub_usage(SHELP_ATTACH
, CMD_ATTACH
);
4629 brand_help
= B_TRUE
;
4631 /* Ignore unknown options - may be brand specific. */
4638 manifest_path
= optarg
;
4639 manifest_pos
= optind
- 1;
4643 * Undocumented 'force update' option for p2v update on
4644 * attach when zone is in the incomplete state. Change
4645 * the option back to 'u' and set forced_update flag.
4647 if (optind
== last_index
)
4650 offset
= optind
- 1;
4651 if ((up
= index(argv
[offset
], 'U')) != NULL
)
4653 forced_update
= B_TRUE
;
4656 /* Ignore unknown options - may be brand specific. */
4659 last_index
= optind
;
4667 /* dry-run and force flags are mutually exclusive */
4668 if (!execute
&& force
) {
4669 zerror(gettext("-F and -n flags are mutually exclusive"));
4674 * If the no-execute option was specified, we don't do validation and
4675 * need to figure out the brand, since there is no zone required to be
4676 * configured for this option.
4680 if (sanity_check(target_zone
, CMD_ATTACH
, B_FALSE
,
4681 B_TRUE
, forced_update
) != Z_OK
)
4683 if (verify_details(CMD_ATTACH
, argv
) != Z_OK
)
4687 if ((err
= zone_get_zonepath(target_zone
, zonepath
,
4688 sizeof (zonepath
))) != Z_OK
) {
4690 zperror2(target_zone
,
4691 gettext("could not get zone path"));
4695 if (dryrun_get_brand(manifest_path
, tmpmanifest
,
4696 sizeof (tmpmanifest
)) != Z_OK
)
4699 argv
[manifest_pos
] = tmpmanifest
;
4701 (void) strlcpy(zonepath
, "-", sizeof (zonepath
));
4703 /* Run the brand's verify_adm hook. */
4704 if (verify_brand(NULL
, CMD_ATTACH
, argv
) != Z_OK
)
4709 * Fetch the attach and postattach hooks from the brand configuration.
4711 if ((bh
= brand_open(target_brand
)) == NULL
) {
4712 zerror(gettext("missing or invalid brand"));
4716 if (get_hook(bh
, cmdbuf
, sizeof (cmdbuf
), brand_get_attach
, target_zone
,
4717 zonepath
) != Z_OK
) {
4718 zerror("invalid brand configuration: missing attach resource");
4723 if (get_hook(bh
, postcmdbuf
, sizeof (postcmdbuf
), brand_get_postattach
,
4724 target_zone
, zonepath
) != Z_OK
) {
4725 zerror("invalid brand configuration: missing postattach "
4732 /* Append all options to attach hook. */
4733 if (addoptions(cmdbuf
, argv
, sizeof (cmdbuf
)) != Z_OK
)
4736 /* Append all options to postattach hook. */
4737 if (addoptions(postcmdbuf
, argv
, sizeof (postcmdbuf
)) != Z_OK
)
4740 if (execute
&& !brand_help
) {
4741 if (zonecfg_grab_lock_file(target_zone
, &lockfd
) != Z_OK
) {
4742 zerror(gettext("another %s may have an operation in "
4743 "progress."), "zoneadm");
4750 * Not a force-attach, so we need to actually do the work.
4752 if (cmdbuf
[0] != '\0') {
4753 /* Run the attach hook */
4754 status
= do_subproc(cmdbuf
);
4755 if ((status
= subproc_status(gettext("brand-specific "
4756 "attach"), status
, B_FALSE
)) != ZONE_SUBPROC_OK
) {
4757 if (status
== ZONE_SUBPROC_USAGE
&& !brand_help
)
4758 sub_usage(SHELP_ATTACH
, CMD_ATTACH
);
4760 if (execute
&& !brand_help
) {
4761 assert(zonecfg_lock_file_held(&lockfd
));
4762 zonecfg_release_lock_file(target_zone
,
4767 assert(lockfd
== -1);
4773 * Else run the built-in attach support.
4774 * This is a no-op since there is nothing to validate.
4777 /* If dry-run or help, then we're done. */
4778 if (!execute
|| brand_help
) {
4780 (void) unlink(tmpmanifest
);
4781 assert(lockfd
== -1);
4786 /* Now we can validate that the zonepath exists. */
4787 if (validate_zonepath(zonepath
, CMD_ATTACH
) != Z_OK
) {
4788 (void) fprintf(stderr
, gettext("could not verify zonepath %s "
4789 "because of the above errors.\n"), zonepath
);
4791 assert(zonecfg_lock_file_held(&lockfd
));
4792 zonecfg_release_lock_file(target_zone
, lockfd
);
4796 if ((handle
= zonecfg_init_handle()) == NULL
) {
4797 zperror(cmd_to_str(CMD_ATTACH
), B_TRUE
);
4799 } else if ((err
= zonecfg_get_handle(target_zone
, handle
)) != Z_OK
) {
4801 zperror(cmd_to_str(CMD_ATTACH
), B_TRUE
);
4802 zonecfg_fini_handle(handle
);
4804 zonecfg_rm_detached(handle
, force
);
4805 zonecfg_fini_handle(handle
);
4809 (err
= zone_set_state(target_zone
, ZONE_STATE_INSTALLED
)) != Z_OK
) {
4811 zperror(gettext("could not reset state"), B_TRUE
);
4814 assert(zonecfg_lock_file_held(&lockfd
));
4815 zonecfg_release_lock_file(target_zone
, lockfd
);
4818 /* If we have a brand postattach hook, run it. */
4819 if (err
== Z_OK
&& !force
&& postcmdbuf
[0] != '\0') {
4820 status
= do_subproc(postcmdbuf
);
4821 if (subproc_status(gettext("brand-specific postattach"),
4822 status
, B_FALSE
) != ZONE_SUBPROC_OK
) {
4823 if ((err
= zone_set_state(target_zone
,
4824 ZONE_STATE_CONFIGURED
)) != Z_OK
) {
4826 zperror(gettext("could not reset state"),
4832 assert(lockfd
== -1);
4833 return ((err
== Z_OK
) ? Z_OK
: Z_ERR
);
4837 * On input, TRUE => yes, FALSE => no.
4838 * On return, TRUE => 1, FALSE => 0, could not ask => -1.
4842 ask_yesno(boolean_t default_answer
, const char *question
)
4844 char line
[64]; /* should be large enough to answer yes or no */
4846 if (!isatty(STDIN_FILENO
))
4849 (void) printf("%s (%s)? ", question
,
4850 default_answer
? "[y]/n" : "y/[n]");
4851 if (fgets(line
, sizeof (line
), stdin
) == NULL
||
4853 return (default_answer
? 1 : 0);
4854 if (tolower(line
[0]) == 'y')
4856 if (tolower(line
[0]) == 'n')
4863 uninstall_func(int argc
, char *argv
[])
4865 char line
[ZONENAME_MAX
+ 128]; /* Enough for "Are you sure ..." */
4866 char rootpath
[MAXPATHLEN
], zonepath
[MAXPATHLEN
];
4867 char cmdbuf
[MAXPATHLEN
];
4868 char precmdbuf
[MAXPATHLEN
];
4869 boolean_t force
= B_FALSE
;
4872 boolean_t brand_help
= B_FALSE
;
4873 brand_handle_t bh
= NULL
;
4876 if (zonecfg_in_alt_root()) {
4877 zerror(gettext("cannot uninstall zone in alternate root"));
4881 /* Check the argv string for args we handle internally */
4884 while ((arg
= getopt(argc
, argv
, "?F")) != EOF
) {
4887 if (optopt
== '?') {
4888 sub_usage(SHELP_UNINSTALL
, CMD_UNINSTALL
);
4889 brand_help
= B_TRUE
;
4891 /* Ignore unknown options - may be brand specific. */
4897 /* Ignore unknown options - may be brand specific. */
4903 if (sanity_check(target_zone
, CMD_UNINSTALL
, B_FALSE
, B_TRUE
,
4908 * Invoke brand-specific handler.
4910 if (invoke_brand_handler(CMD_UNINSTALL
, argv
) != Z_OK
)
4914 (void) snprintf(line
, sizeof (line
),
4915 gettext("Are you sure you want to %s zone %s"),
4916 cmd_to_str(CMD_UNINSTALL
), target_zone
);
4917 if ((answer
= ask_yesno(B_FALSE
, line
)) == 0) {
4919 } else if (answer
== -1) {
4920 zerror(gettext("Input not from terminal and -F "
4921 "not specified: %s not done."),
4922 cmd_to_str(CMD_UNINSTALL
));
4928 if ((err
= zone_get_zonepath(target_zone
, zonepath
,
4929 sizeof (zonepath
))) != Z_OK
) {
4931 zperror2(target_zone
, gettext("could not get zone path"));
4936 * Fetch the uninstall and preuninstall hooks from the brand
4939 if ((bh
= brand_open(target_brand
)) == NULL
) {
4940 zerror(gettext("missing or invalid brand"));
4944 if (get_hook(bh
, cmdbuf
, sizeof (cmdbuf
), brand_get_uninstall
,
4945 target_zone
, zonepath
) != Z_OK
) {
4946 zerror("invalid brand configuration: missing uninstall "
4952 if (get_hook(bh
, precmdbuf
, sizeof (precmdbuf
), brand_get_preuninstall
,
4953 target_zone
, zonepath
) != Z_OK
) {
4954 zerror("invalid brand configuration: missing preuninstall "
4961 /* Append all options to preuninstall hook. */
4962 if (addoptions(precmdbuf
, argv
, sizeof (precmdbuf
)) != Z_OK
)
4965 /* Append all options to uninstall hook. */
4966 if (addoptions(cmdbuf
, argv
, sizeof (cmdbuf
)) != Z_OK
)
4970 if ((err
= zone_get_rootpath(target_zone
, rootpath
,
4971 sizeof (rootpath
))) != Z_OK
) {
4973 zperror2(target_zone
, gettext("could not get root "
4979 * If there seems to be a zoneadmd running for this zone, call
4980 * it to tell it that an uninstall is happening; if all goes
4981 * well it will then shut itself down.
4983 if (zonecfg_ping_zoneadmd(target_zone
) == Z_OK
) {
4984 zone_cmd_arg_t zarg
;
4985 zarg
.cmd
= Z_NOTE_UNINSTALLING
;
4986 /* we don't care too much if this fails, just plow on */
4987 (void) zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
,
4991 if (zonecfg_grab_lock_file(target_zone
, &lockfd
) != Z_OK
) {
4992 zerror(gettext("another %s may have an operation in "
4993 "progress."), "zoneadm");
4997 /* Don't uninstall the zone if anything is mounted there */
4998 err
= zonecfg_find_mounts(rootpath
, NULL
, NULL
);
5000 zerror(gettext("These file systems are mounted on "
5001 "subdirectories of %s.\n"), rootpath
);
5002 (void) zonecfg_find_mounts(rootpath
, zfm_print
, NULL
);
5003 zonecfg_release_lock_file(target_zone
, lockfd
);
5008 /* If we have a brand preuninstall hook, run it. */
5009 if (!brand_help
&& precmdbuf
[0] != '\0') {
5010 status
= do_subproc(precmdbuf
);
5011 if (subproc_status(gettext("brand-specific preuninstall"),
5012 status
, B_FALSE
) != ZONE_SUBPROC_OK
) {
5013 zonecfg_release_lock_file(target_zone
, lockfd
);
5019 err
= zone_set_state(target_zone
, ZONE_STATE_INCOMPLETE
);
5022 zperror2(target_zone
, gettext("could not set state"));
5028 * If there is a brand uninstall hook, use it, otherwise use the
5029 * built-in uninstall code.
5031 if (cmdbuf
[0] != '\0') {
5032 /* Run the uninstall hook */
5033 status
= do_subproc(cmdbuf
);
5034 if ((status
= subproc_status(gettext("brand-specific "
5035 "uninstall"), status
, B_FALSE
)) != ZONE_SUBPROC_OK
) {
5036 if (status
== ZONE_SUBPROC_USAGE
&& !brand_help
)
5037 sub_usage(SHELP_UNINSTALL
, CMD_UNINSTALL
);
5039 zonecfg_release_lock_file(target_zone
, lockfd
);
5046 /* If just help, we're done since there is no brand help. */
5050 /* Run the built-in uninstall support. */
5051 if ((err
= cleanup_zonepath(zonepath
, B_FALSE
)) != Z_OK
) {
5053 zperror2(target_zone
, gettext("cleaning up zonepath "
5059 err
= zone_set_state(target_zone
, ZONE_STATE_CONFIGURED
);
5062 zperror2(target_zone
, gettext("could not reset state"));
5065 zonecfg_release_lock_file(target_zone
, lockfd
);
5071 mount_func(int argc
, char *argv
[])
5073 zone_cmd_arg_t zarg
;
5074 boolean_t force
= B_FALSE
;
5078 * The only supported subargument to the "mount" subcommand is
5079 * "-f", which forces us to mount a zone in the INCOMPLETE state.
5082 if ((arg
= getopt(argc
, argv
, "f")) != EOF
) {
5094 if (sanity_check(target_zone
, CMD_MOUNT
, B_FALSE
, B_FALSE
, force
)
5097 if (verify_details(CMD_MOUNT
, argv
) != Z_OK
)
5100 zarg
.cmd
= force
? Z_FORCEMOUNT
: Z_MOUNT
;
5101 zarg
.bootbuf
[0] = '\0';
5102 if (zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
, B_TRUE
) != 0) {
5103 zerror(gettext("call to %s failed"), "zoneadmd");
5111 unmount_func(int argc
, char *argv
[])
5113 zone_cmd_arg_t zarg
;
5117 if (sanity_check(target_zone
, CMD_UNMOUNT
, B_FALSE
, B_FALSE
, B_FALSE
)
5121 zarg
.cmd
= Z_UNMOUNT
;
5122 if (zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
, B_TRUE
) != 0) {
5123 zerror(gettext("call to %s failed"), "zoneadmd");
5130 mark_func(int argc
, char *argv
[])
5134 boolean_t force
= B_FALSE
;
5139 while ((arg
= getopt(argc
, argv
, "F")) != EOF
) {
5149 if (argc
!= (optind
+ 1))
5152 if (strcmp(argv
[optind
], "configured") == 0)
5153 state
= ZONE_STATE_CONFIGURED
;
5154 else if (strcmp(argv
[optind
], "incomplete") == 0)
5155 state
= ZONE_STATE_INCOMPLETE
;
5156 else if (strcmp(argv
[optind
], "installed") == 0)
5157 state
= ZONE_STATE_INSTALLED
;
5161 if (state
!= ZONE_STATE_INCOMPLETE
&& !force
)
5164 if (sanity_check(target_zone
, CMD_MARK
, B_FALSE
, B_TRUE
, B_FALSE
)
5169 * Invoke brand-specific handler.
5171 if (invoke_brand_handler(CMD_MARK
, argv
) != Z_OK
)
5174 if (zonecfg_grab_lock_file(target_zone
, &lockfd
) != Z_OK
) {
5175 zerror(gettext("another %s may have an operation in progress."),
5180 err
= zone_set_state(target_zone
, state
);
5183 zperror2(target_zone
, gettext("could not set state"));
5185 zonecfg_release_lock_file(target_zone
, lockfd
);
5191 * Check what scheduling class we're running under and print a warning if
5192 * we're not using FSS.
5195 check_sched_fss(zone_dochandle_t handle
)
5197 char class_name
[PC_CLNMSZ
];
5199 if (zonecfg_get_dflt_sched_class(handle
, class_name
,
5200 sizeof (class_name
)) != Z_OK
) {
5201 zerror(gettext("WARNING: unable to determine the zone's "
5202 "scheduling class"));
5203 } else if (strcmp("FSS", class_name
) != 0) {
5204 zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n"
5205 "FSS is not the default scheduling class for this zone. "
5206 "FSS will be\nused for processes in the zone but to get "
5207 "the full benefit of FSS,\nit should be the default "
5208 "scheduling class. See dispadmin(1M) for\nmore details."));
5216 check_cpu_shares_sched(zone_dochandle_t handle
)
5220 struct zone_rctltab rctl
;
5222 if ((err
= zonecfg_setrctlent(handle
)) != Z_OK
) {
5224 zperror(cmd_to_str(CMD_APPLY
), B_TRUE
);
5228 while (zonecfg_getrctlent(handle
, &rctl
) == Z_OK
) {
5229 if (strcmp(rctl
.zone_rctl_name
, "zone.cpu-shares") == 0) {
5230 if (check_sched_fss(handle
) != Z_OK
)
5236 (void) zonecfg_endrctlent(handle
);
5242 * Check if there is a mix of processes running in different pools within the
5243 * zone. This is currently only going to be called for the global zone from
5244 * apply_func but that could be generalized in the future.
5247 mixed_pools(zoneid_t zoneid
)
5251 boolean_t mixed
= B_FALSE
;
5252 boolean_t poolid_set
= B_FALSE
;
5253 poolid_t last_poolid
= 0;
5255 if ((dirp
= opendir("/proc")) == NULL
) {
5256 zerror(gettext("could not open /proc"));
5260 while ((dent
= readdir(dirp
)) != NULL
) {
5263 char procpath
[MAXPATHLEN
];
5265 if (dent
->d_name
[0] == '.')
5268 (void) snprintf(procpath
, sizeof (procpath
), "/proc/%s/psinfo",
5271 if ((procfd
= open(procpath
, O_RDONLY
)) == -1)
5274 if (read(procfd
, &ps
, sizeof (ps
)) == sizeof (psinfo_t
)) {
5275 /* skip processes in other zones and system processes */
5276 if (zoneid
!= ps
.pr_zoneid
|| ps
.pr_flag
& SSYS
) {
5277 (void) close(procfd
);
5282 if (ps
.pr_poolid
!= last_poolid
)
5285 last_poolid
= ps
.pr_poolid
;
5286 poolid_set
= B_TRUE
;
5290 (void) close(procfd
);
5296 (void) closedir(dirp
);
5302 * Check if a persistent or temporary pool is configured for the zone.
5303 * This is currently only going to be called for the global zone from
5304 * apply_func but that could be generalized in the future.
5307 pool_configured(zone_dochandle_t handle
)
5310 struct zone_psettab pset_tab
;
5311 char poolname
[MAXPATHLEN
];
5313 err1
= zonecfg_lookup_pset(handle
, &pset_tab
);
5314 err2
= zonecfg_get_pool(handle
, poolname
, sizeof (poolname
));
5316 if (err1
== Z_NO_ENTRY
&&
5317 (err2
== Z_NO_ENTRY
|| (err2
== Z_OK
&& strlen(poolname
) == 0)))
5324 * This is an undocumented interface which is currently only used to apply
5325 * the global zone resource management settings when the system boots.
5326 * This function does not yet properly handle updating a running system so
5327 * any projects running in the zone would be trashed if this function
5328 * were to run after the zone had booted. It also does not reset any
5329 * rctl settings that were removed from zonecfg. There is still work to be
5330 * done before we can properly support dynamically updating the resource
5331 * management settings for a running zone (global or non-global). Thus, this
5332 * functionality is undocumented for now.
5336 apply_func(int argc
, char *argv
[])
5340 priv_set_t
*privset
;
5342 zone_dochandle_t handle
;
5343 struct zone_mcaptab mcap
;
5346 zoneid
= getzoneid();
5348 if (zonecfg_in_alt_root() || zoneid
!= GLOBAL_ZONEID
||
5349 target_zone
== NULL
|| strcmp(target_zone
, GLOBAL_ZONENAME
) != 0)
5350 return (usage(B_FALSE
));
5352 if ((privset
= priv_allocset()) == NULL
) {
5353 zerror(gettext("%s failed"), "priv_allocset");
5357 if (getppriv(PRIV_EFFECTIVE
, privset
) != 0) {
5358 zerror(gettext("%s failed"), "getppriv");
5359 priv_freeset(privset
);
5363 if (priv_isfullset(privset
) == B_FALSE
) {
5364 (void) usage(B_FALSE
);
5365 priv_freeset(privset
);
5368 priv_freeset(privset
);
5370 if ((handle
= zonecfg_init_handle()) == NULL
) {
5371 zperror(cmd_to_str(CMD_APPLY
), B_TRUE
);
5375 if ((err
= zonecfg_get_handle(target_zone
, handle
)) != Z_OK
) {
5377 zperror(cmd_to_str(CMD_APPLY
), B_TRUE
);
5378 zonecfg_fini_handle(handle
);
5382 /* specific error msgs are printed within apply_rctls */
5383 if ((err
= zonecfg_apply_rctls(target_zone
, handle
)) != Z_OK
) {
5385 zperror(cmd_to_str(CMD_APPLY
), B_TRUE
);
5389 if ((err
= check_cpu_shares_sched(handle
)) != Z_OK
)
5392 if (pool_configured(handle
)) {
5393 if (mixed_pools(zoneid
)) {
5394 zerror(gettext("Zone is using multiple resource "
5395 "pools. The pool\nconfiguration cannot be "
5396 "applied without rebooting."));
5401 * The next two blocks of code attempt to set up
5402 * temporary pools as well as persistent pools. In
5403 * both cases we call the functions unconditionally.
5404 * Within each funtion the code will check if the zone
5405 * is actually configured for a temporary pool or
5406 * persistent pool and just return if there is nothing
5409 if ((err
= zonecfg_bind_tmp_pool(handle
, zoneid
,
5410 pool_err
, sizeof (pool_err
))) != Z_OK
) {
5411 if (err
== Z_POOL
|| err
== Z_POOL_CREATE
||
5413 zerror("%s: %s", zonecfg_strerror(err
),
5416 zerror(gettext("could not bind zone to "
5417 "temporary pool: %s"),
5418 zonecfg_strerror(err
));
5422 if ((err
= zonecfg_bind_pool(handle
, zoneid
, pool_err
,
5423 sizeof (pool_err
))) != Z_OK
) {
5424 if (err
== Z_POOL
|| err
== Z_POOL_BIND
)
5425 zerror("%s: %s", zonecfg_strerror(err
),
5428 zerror("%s", zonecfg_strerror(err
));
5434 * If a memory cap is configured, set the cap in the kernel using
5435 * zone_setattr() and make sure the rcapd SMF service is enabled.
5437 if (zonecfg_getmcapent(handle
, &mcap
) == Z_OK
) {
5441 num
= (uint64_t)strtoll(mcap
.zone_physmem_cap
, NULL
, 10);
5442 if (zone_setattr(zoneid
, ZONE_ATTR_PHYS_MCAP
, &num
, 0) == -1) {
5443 zerror(gettext("could not set zone memory cap"));
5447 if (zonecfg_enable_rcapd(smf_err
, sizeof (smf_err
)) != Z_OK
) {
5448 zerror(gettext("enabling system/rcap service failed: "
5454 zonecfg_fini_handle(handle
);
5460 * This is an undocumented interface that is invoked by the zones SMF service
5461 * for installed zones that won't automatically boot.
5465 sysboot_func(int argc
, char *argv
[])
5468 zone_dochandle_t zone_handle
;
5469 brand_handle_t brand_handle
;
5470 char cmdbuf
[MAXPATHLEN
];
5471 char zonepath
[MAXPATHLEN
];
5474 * This subcommand can only be executed in the global zone on non-global
5477 if (zonecfg_in_alt_root())
5478 return (usage(B_FALSE
));
5479 if (sanity_check(target_zone
, CMD_SYSBOOT
, B_FALSE
, B_TRUE
, B_FALSE
) !=
5484 * Fetch the sysboot hook from the target zone's brand.
5486 if ((zone_handle
= zonecfg_init_handle()) == NULL
) {
5487 zperror(cmd_to_str(CMD_SYSBOOT
), B_TRUE
);
5490 if ((err
= zonecfg_get_handle(target_zone
, zone_handle
)) != Z_OK
) {
5492 zperror(cmd_to_str(CMD_SYSBOOT
), B_TRUE
);
5493 zonecfg_fini_handle(zone_handle
);
5496 if ((err
= zonecfg_get_zonepath(zone_handle
, zonepath
,
5497 sizeof (zonepath
))) != Z_OK
) {
5499 zperror(cmd_to_str(CMD_SYSBOOT
), B_TRUE
);
5500 zonecfg_fini_handle(zone_handle
);
5503 if ((brand_handle
= brand_open(target_brand
)) == NULL
) {
5504 zerror(gettext("missing or invalid brand during %s operation: "
5505 "%s"), cmd_to_str(CMD_SYSBOOT
), target_brand
);
5506 zonecfg_fini_handle(zone_handle
);
5509 err
= get_hook(brand_handle
, cmdbuf
, sizeof (cmdbuf
), brand_get_sysboot
,
5510 target_zone
, zonepath
);
5511 brand_close(brand_handle
);
5512 zonecfg_fini_handle(zone_handle
);
5514 zerror(gettext("unable to get brand hook from brand %s for %s "
5515 "operation"), target_brand
, cmd_to_str(CMD_SYSBOOT
));
5520 * If the hook wasn't defined (which is OK), then indicate success and
5521 * return. Otherwise, execute the hook.
5523 if (cmdbuf
[0] != '\0')
5524 return ((subproc_status(gettext("brand sysboot operation"),
5525 do_subproc(cmdbuf
), B_FALSE
) == ZONE_SUBPROC_OK
) ? Z_OK
:
5531 help_func(int argc
, char *argv
[])
5536 (void) usage(B_TRUE
);
5540 if ((arg
= getopt(argc
, argv
, "?")) != EOF
) {
5543 sub_usage(SHELP_HELP
, CMD_HELP
);
5544 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
5546 sub_usage(SHELP_HELP
, CMD_HELP
);
5550 while (optind
< argc
) {
5551 /* Private commands have NULL short_usage; omit them */
5552 if ((cmd_num
= cmd_match(argv
[optind
])) < 0 ||
5553 cmdtab
[cmd_num
].short_usage
== NULL
) {
5554 sub_usage(SHELP_HELP
, CMD_HELP
);
5557 sub_usage(cmdtab
[cmd_num
].short_usage
, cmd_num
);
5564 * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
5568 cmd_match(char *cmd
)
5572 for (i
= CMD_MIN
; i
<= CMD_MAX
; i
++) {
5573 /* return only if there is an exact match */
5574 if (strcmp(cmd
, cmdtab
[i
].cmd_name
) == 0)
5575 return (cmdtab
[i
].cmd_num
);
5581 parse_and_run(int argc
, char *argv
[])
5583 int i
= cmd_match(argv
[0]);
5586 return (usage(B_FALSE
));
5587 return (cmdtab
[i
].handler(argc
- 1, &(argv
[1])));
5591 get_execbasename(char *execfullname
)
5593 char *last_slash
, *execbasename
;
5595 /* guard against '/' at end of command invocation */
5597 last_slash
= strrchr(execfullname
, '/');
5598 if (last_slash
== NULL
) {
5599 execbasename
= execfullname
;
5602 execbasename
= last_slash
+ 1;
5603 if (*execbasename
== '\0') {
5610 return (execbasename
);
5617 struct passwd
*nptr
;
5621 * Authorizations are checked to restrict access based on the
5622 * requested operation and zone name, It is assumed that the
5623 * program is running with all privileges, but that the real
5624 * user ID is that of the user or role on whose behalf we are
5625 * operating. So we start by getting the username that will be
5626 * used for subsequent authorization checks.
5630 if ((nptr
= getpwuid(uid
)) == NULL
) {
5631 zerror(gettext("could not get user name."));
5634 return (nptr
->pw_name
);
5638 main(int argc
, char **argv
)
5643 char *zone_lock_env
;
5646 if ((locale
= setlocale(LC_ALL
, "")) == NULL
)
5648 (void) textdomain(TEXT_DOMAIN
);
5649 setbuf(stdout
, NULL
);
5650 (void) sigset(SIGHUP
, SIG_IGN
);
5651 execname
= get_execbasename(argv
[0]);
5652 username
= get_username();
5654 if (chdir("/") != 0) {
5655 zerror(gettext("could not change directory to /."));
5660 * Use the default system mask rather than anything that may have been
5661 * set by the caller.
5663 (void) umask(CMASK
);
5665 if (init_zfs() != Z_OK
)
5668 while ((arg
= getopt(argc
, argv
, "?u:z:R:")) != EOF
) {
5671 return (usage(B_TRUE
));
5673 target_uuid
= optarg
;
5676 target_zone
= optarg
;
5678 case 'R': /* private option for admin/install use */
5679 if (*optarg
!= '/') {
5680 zerror(gettext("root path must be absolute."));
5683 if (stat(optarg
, &st
) == -1 || !S_ISDIR(st
.st_mode
)) {
5685 gettext("root path must be a directory."));
5688 zonecfg_set_root(optarg
);
5691 return (usage(B_FALSE
));
5696 return (usage(B_FALSE
));
5698 if (target_uuid
!= NULL
&& *target_uuid
!= '\0') {
5700 static char newtarget
[ZONENAME_MAX
];
5702 if (uuid_parse(target_uuid
, uuid
) == -1) {
5703 zerror(gettext("illegal UUID value specified"));
5706 if (zonecfg_get_name_by_uuid(uuid
, newtarget
,
5707 sizeof (newtarget
)) == Z_OK
)
5708 target_zone
= newtarget
;
5711 if (target_zone
!= NULL
&& zone_get_id(target_zone
, &zid
) != 0) {
5713 zperror(target_zone
, B_TRUE
);
5718 * See if we have inherited the right to manipulate this zone from
5719 * a zoneadm instance in our ancestry. If so, set zone_lock_cnt to
5720 * indicate it. If not, make that explicit in our environment.
5722 zonecfg_init_lock_file(target_zone
, &zone_lock_env
);
5724 /* Figure out what the system's default brand is */
5725 if (zonecfg_default_brand(default_brand
,
5726 sizeof (default_brand
)) != Z_OK
) {
5727 zerror(gettext("unable to determine default brand"));
5732 * If we are going to be operating on a single zone, retrieve its
5733 * brand type and determine whether it is native or not.
5735 if ((target_zone
!= NULL
) &&
5736 (strcmp(target_zone
, GLOBAL_ZONENAME
) != 0)) {
5737 if (zone_get_brand(target_zone
, target_brand
,
5738 sizeof (target_brand
)) != Z_OK
) {
5739 zerror(gettext("missing or invalid brand"));
5743 * In the alternate root environment, the only supported
5744 * operations are mount and unmount. In this case, just treat
5745 * the zone as native if it is cluster. Cluster zones can be
5746 * native for the purpose of LU or upgrade, and the cluster
5747 * brand may not exist in the miniroot (such as in net install
5750 if (strcmp(target_brand
, CLUSTER_BRAND_NAME
) == 0) {
5751 if (zonecfg_in_alt_root()) {
5752 (void) strlcpy(target_brand
, default_brand
,
5753 sizeof (target_brand
));
5758 err
= parse_and_run(argc
- optind
, &argv
[optind
]);