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.
29 * zoneadm is a command interpreter for zone administration. It is all in
30 * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
31 * main() calls parse_and_run() which calls cmd_match(), then invokes the
32 * appropriate command's handler function. The rest of the program is the
33 * handler functions and their helper functions.
35 * Some of the helper functions are used largely to simplify I18N: reducing
36 * the need for translation notes. This is particularly true of many of
37 * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
38 * than zerror(gettext("foo failed")) with a translation note indicating
39 * that "foo" need not be translated.
55 #include <libzonecfg.h>
57 #include <sys/brand.h>
58 #include <sys/param.h>
59 #include <sys/types.h>
61 #include <sys/statvfs.h>
63 #include <sys/sockio.h>
64 #include <sys/mntent.h>
67 #include <uuid/uuid.h>
73 #include <sys/modctl.h>
80 #include <sys/priocntl.h>
81 #include <sys/fsspriocntl.h>
83 #include <libdllink.h>
85 #include <auth_list.h>
86 #include <auth_attr.h>
92 #define SOURCE_ZONE (CMD_MAX + 1)
94 /* Reflects kernel zone entries */
95 typedef struct zone_entry
{
97 char zname
[ZONENAME_MAX
];
99 zone_state_t zstate_num
;
100 char zbrand
[MAXNAMELEN
];
101 char zroot
[MAXPATHLEN
];
102 char zuuid
[UUID_PRINTABLE_STRING_LENGTH
];
103 zone_iptype_t ziptype
;
106 #define CLUSTER_BRAND_NAME "cluster"
108 static zone_entry_t
*zents
;
109 static size_t nzents
;
111 #define LOOPBACK_IF "lo0"
112 #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af))
119 /* 0755 is the default directory mode. */
120 #define DEFAULT_DIR_MODE \
121 (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
124 uint_t cmd_num
; /* command number */
125 char *cmd_name
; /* command name */
126 char *short_usage
; /* short form help */
127 int (*handler
)(int argc
, char *argv
[]); /* function to call */
131 #define SHELP_HELP "help"
132 #define SHELP_BOOT "boot [-- boot_arguments]"
133 #define SHELP_HALT "halt"
134 #define SHELP_READY "ready"
135 #define SHELP_SHUTDOWN "shutdown [-r [-- boot_arguments]]"
136 #define SHELP_REBOOT "reboot [-- boot_arguments]"
137 #define SHELP_LIST "list [-cipv]"
138 #define SHELP_VERIFY "verify"
139 #define SHELP_INSTALL "install [brand-specific args]"
140 #define SHELP_UNINSTALL "uninstall [-F] [brand-specific args]"
141 #define SHELP_CLONE "clone [-m method] [-s <ZFS snapshot>] "\
142 "[brand-specific args] zonename"
143 #define SHELP_MOVE "move zonepath"
144 #define SHELP_DETACH "detach [-n] [brand-specific args]"
145 #define SHELP_ATTACH "attach [-F] [-n <path>] [brand-specific args]"
146 #define SHELP_MARK "mark incomplete"
148 #define EXEC_PREFIX "exec "
149 #define EXEC_LEN (strlen(EXEC_PREFIX))
150 #define RMCOMMAND "/usr/bin/rm -rf"
152 static int cleanup_zonepath(char *, boolean_t
);
155 static int help_func(int argc
, char *argv
[]);
156 static int ready_func(int argc
, char *argv
[]);
157 static int boot_func(int argc
, char *argv
[]);
158 static int shutdown_func(int argc
, char *argv
[]);
159 static int halt_func(int argc
, char *argv
[]);
160 static int reboot_func(int argc
, char *argv
[]);
161 static int list_func(int argc
, char *argv
[]);
162 static int verify_func(int argc
, char *argv
[]);
163 static int install_func(int argc
, char *argv
[]);
164 static int uninstall_func(int argc
, char *argv
[]);
165 static int mount_func(int argc
, char *argv
[]);
166 static int unmount_func(int argc
, char *argv
[]);
167 static int clone_func(int argc
, char *argv
[]);
168 static int move_func(int argc
, char *argv
[]);
169 static int detach_func(int argc
, char *argv
[]);
170 static int attach_func(int argc
, char *argv
[]);
171 static int mark_func(int argc
, char *argv
[]);
172 static int apply_func(int argc
, char *argv
[]);
173 static int sysboot_func(int argc
, char *argv
[]);
174 static int sanity_check(char *zone
, int cmd_num
, boolean_t running
,
175 boolean_t unsafe_when_running
, boolean_t force
);
176 static int cmd_match(char *cmd
);
177 static int verify_details(int, char *argv
[]);
178 static int verify_brand(zone_dochandle_t
, int, char *argv
[]);
179 static int invoke_brand_handler(int, char *argv
[]);
181 static struct cmd cmdtab
[] = {
182 { CMD_HELP
, "help", SHELP_HELP
, help_func
},
183 { CMD_BOOT
, "boot", SHELP_BOOT
, boot_func
},
184 { CMD_HALT
, "halt", SHELP_HALT
, halt_func
},
185 { CMD_READY
, "ready", SHELP_READY
, ready_func
},
186 { CMD_SHUTDOWN
, "shutdown", SHELP_SHUTDOWN
, shutdown_func
},
187 { CMD_REBOOT
, "reboot", SHELP_REBOOT
, reboot_func
},
188 { CMD_LIST
, "list", SHELP_LIST
, list_func
},
189 { CMD_VERIFY
, "verify", SHELP_VERIFY
, verify_func
},
190 { CMD_INSTALL
, "install", SHELP_INSTALL
, install_func
},
191 { CMD_UNINSTALL
, "uninstall", SHELP_UNINSTALL
,
193 /* mount and unmount are private commands for admin/install */
194 { CMD_MOUNT
, "mount", NULL
, mount_func
},
195 { CMD_UNMOUNT
, "unmount", NULL
, unmount_func
},
196 { CMD_CLONE
, "clone", SHELP_CLONE
, clone_func
},
197 { CMD_MOVE
, "move", SHELP_MOVE
, move_func
},
198 { CMD_DETACH
, "detach", SHELP_DETACH
, detach_func
},
199 { CMD_ATTACH
, "attach", SHELP_ATTACH
, attach_func
},
200 { CMD_MARK
, "mark", SHELP_MARK
, mark_func
},
201 { CMD_APPLY
, "apply", NULL
, apply_func
},
202 { CMD_SYSBOOT
, "sysboot", NULL
, sysboot_func
}
205 /* global variables */
207 /* set early in main(), never modified thereafter, used all over the place */
208 static char *execname
;
209 static char target_brand
[MAXNAMELEN
];
210 static char default_brand
[MAXPATHLEN
];
213 static char *target_uuid
;
217 cmd_to_str(int cmd_num
)
219 assert(cmd_num
>= CMD_MIN
&& cmd_num
<= CMD_MAX
);
220 return (cmdtab
[cmd_num
].cmd_name
);
223 /* This is a separate function because of gettext() wrapping. */
225 long_help(int cmd_num
)
227 assert(cmd_num
>= CMD_MIN
&& cmd_num
<= CMD_MAX
);
230 return (gettext("Print usage message."));
232 return (gettext("Activates (boots) specified zone. See "
233 "zoneadm(1m) for valid boot\n\targuments."));
235 return (gettext("Halts specified zone, bypassing shutdown "
236 "scripts and removing runtime\n\tresources of the zone."));
238 return (gettext("Prepares a zone for running applications but "
239 "does not start any user\n\tprocesses in the zone."));
241 return (gettext("Gracefully shutdown the zone or reboot if "
242 "the '-r' option is specified.\n\t"
243 "See zoneadm(1m) for valid boot arguments."));
245 return (gettext("Restarts the zone (equivalent to a halt / "
246 "boot sequence).\n\tFails if the zone is not active. "
247 "See zoneadm(1m) for valid boot\n\targuments."));
249 return (gettext("Lists the current zones, or a "
250 "specific zone if indicated. By default,\n\tall "
251 "running zones are listed, though this can be "
252 "expanded to all\n\tinstalled zones with the -i "
253 "option or all configured zones with the\n\t-c "
254 "option. When used with the general -z <zone> and/or -u "
255 "<uuid-match>\n\toptions, lists only the specified "
256 "matching zone, but lists it\n\tregardless of its state, "
257 "and the -i and -c options are disallowed. The\n\t-v "
258 "option can be used to display verbose information: zone "
259 "name, id,\n\tcurrent state, root directory and options. "
260 "The -p option can be used\n\tto request machine-parsable "
261 "output. The -v and -p options are mutually\n\texclusive."
262 " If neither -v nor -p is used, just the zone name is "
265 return (gettext("Check to make sure the configuration "
266 "can safely be instantiated\n\ton the machine: "
267 "physical network interfaces exist, etc."));
269 return (gettext("Install the configuration on to the system. "
270 "All arguments are passed to the brand installation "
271 "function;\n\tsee brands(5) for more information."));
273 return (gettext("Uninstall the configuration from the system. "
274 "The -F flag can be used\n\tto force the action. All "
275 "other arguments are passed to the brand\n\tuninstall "
276 "function; see brands(5) for more information."));
278 return (gettext("Clone the installation of another zone. "
279 "The -m option can be used to\n\tspecify 'copy' which "
280 "forces a copy of the source zone. The -s option\n\t"
281 "can be used to specify the name of a ZFS snapshot "
282 "that was taken from\n\ta previous clone command. The "
283 "snapshot will be used as the source\n\tinstead of "
284 "creating a new ZFS snapshot. All other arguments are "
285 "passed\n\tto the brand clone function; see "
286 "brands(5) for more information."));
288 return (gettext("Move the zone to a new zonepath."));
290 return (gettext("Detach the zone from the system. The zone "
291 "state is changed to\n\t'configured' (but the files under "
292 "the zonepath are untouched).\n\tThe zone can subsequently "
293 "be attached, or can be moved to another\n\tsystem and "
294 "attached there. The -n option can be used to specify\n\t"
295 "'no-execute' mode. When -n is used, the information "
296 "needed to attach\n\tthe zone is sent to standard output "
297 "but the zone is not actually\n\tdetached. All other "
298 "arguments are passed to the brand detach function;\n\tsee "
299 "brands(5) for more information."));
301 return (gettext("Attach the zone to the system. The zone "
302 "state must be 'configured'\n\tprior to attach; upon "
303 "successful completion, the zone state will be\n\t"
304 "'installed'. The system software on the current "
305 "system must be\n\tcompatible with the software on the "
306 "zone's original system.\n\tSpecify -F "
307 "to force the attach and skip software compatibility "
308 "tests.\n\tThe -n option can be used to specify "
309 "'no-execute' mode. When -n is\n\tused, the information "
310 "needed to attach the zone is read from the\n\tspecified "
311 "path and the configuration is only validated. The path "
312 "can\n\tbe '-' to specify standard input. The -F and -n "
313 "options are mutually\n\texclusive. All other arguments "
314 "are passed to the brand attach\n\tfunction; see "
315 "brands(5) for more information."));
317 return (gettext("Set the state of the zone. This can be used "
318 "to force the zone\n\tstate to 'incomplete' "
319 "administratively if some activity has rendered\n\tthe "
320 "zone permanently unusable. The only valid state that "
321 "may be\n\tspecified is 'incomplete'."));
330 * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
335 usage(boolean_t
explicit)
338 FILE *fd
= explicit ? stdout
: stderr
;
340 (void) fprintf(fd
, "%s:\t%s help\n", gettext("usage"), execname
);
341 (void) fprintf(fd
, "\t%s [-z <zone>] [-u <uuid-match>] list\n",
343 (void) fprintf(fd
, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname
,
344 gettext("subcommand"));
345 (void) fprintf(fd
, "\n%s:\n\n", gettext("Subcommands"));
346 for (i
= CMD_MIN
; i
<= CMD_MAX
; i
++) {
347 if (cmdtab
[i
].short_usage
== NULL
)
349 (void) fprintf(fd
, "%s\n", cmdtab
[i
].short_usage
);
351 (void) fprintf(fd
, "\t%s\n\n", long_help(i
));
354 (void) fputs("\n", fd
);
359 sub_usage(char *short_usage
, int cmd_num
)
361 (void) fprintf(stderr
, "%s:\t%s\n", gettext("usage"), short_usage
);
362 (void) fprintf(stderr
, "\t%s\n", long_help(cmd_num
));
366 * zperror() is like perror(3c) except that this also prints the executable
367 * name at the start of the message, and takes a boolean indicating whether
368 * to call libc'c strerror() or that from libzonecfg.
372 zperror(const char *str
, boolean_t zonecfg_error
)
374 (void) fprintf(stderr
, "%s: %s: %s\n", execname
, str
,
375 zonecfg_error
? zonecfg_strerror(errno
) : strerror(errno
));
379 * zperror2() is very similar to zperror() above, except it also prints a
380 * supplied zone name after the executable.
382 * All current consumers of this function want libzonecfg's strerror() rather
383 * than libc's; if this ever changes, this function can be made more generic
384 * like zperror() above.
388 zperror2(const char *zone
, const char *str
)
390 (void) fprintf(stderr
, "%s: %s: %s: %s\n", execname
, zone
, str
,
391 zonecfg_strerror(errno
));
396 zerror(const char *fmt
, ...)
400 va_start(alist
, fmt
);
401 (void) fprintf(stderr
, "%s: ", execname
);
402 if (target_zone
!= NULL
)
403 (void) fprintf(stderr
, "zone '%s': ", target_zone
);
404 (void) vfprintf(stderr
, fmt
, alist
);
405 (void) fprintf(stderr
, "\n");
410 safe_calloc(size_t nelem
, size_t elsize
)
412 void *r
= calloc(nelem
, elsize
);
415 zerror(gettext("failed to allocate %lu bytes: %s"),
416 (ulong_t
)nelem
* elsize
, strerror(errno
));
423 zone_print(zone_entry_t
*zent
, boolean_t verbose
, boolean_t parsable
)
425 static boolean_t firsttime
= B_TRUE
;
428 /* Skip a zone that shutdown while we were collecting data. */
429 if (zent
->zname
[0] == '\0')
432 if (zent
->ziptype
== ZS_EXCLUSIVE
)
433 ip_type_str
= "excl";
435 ip_type_str
= "shared";
437 assert(!(verbose
&& parsable
));
438 if (firsttime
&& verbose
) {
440 (void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n",
441 ZONEID_WIDTH
, "ID", "NAME", "STATUS", "PATH", "BRAND",
448 (void) printf("%s\n", zent
->zname
);
451 if (zent
->zid
== ZONE_ID_UNDEFINED
)
454 (void) printf("%lu", zent
->zid
);
455 (void) printf(":%s:%s:", zent
->zname
, zent
->zstate_str
);
457 while ((clim
= strchr(cp
, ':')) != NULL
) {
458 (void) printf("%.*s\\:", clim
- cp
, cp
);
461 (void) printf("%s:%s:%s:%s\n", cp
, zent
->zuuid
, zent
->zbrand
,
465 if (zent
->zstate_str
!= NULL
) {
466 if (zent
->zid
== ZONE_ID_UNDEFINED
)
467 (void) printf("%*s", ZONEID_WIDTH
, "-");
469 (void) printf("%*lu", ZONEID_WIDTH
, zent
->zid
);
470 (void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent
->zname
,
471 zent
->zstate_str
, zent
->zroot
, zent
->zbrand
, ip_type_str
);
476 lookup_zone_info(const char *zone_name
, zoneid_t zid
, zone_entry_t
*zent
)
478 char root
[MAXPATHLEN
], *cp
;
481 zone_dochandle_t handle
;
483 (void) strlcpy(zent
->zname
, zone_name
, sizeof (zent
->zname
));
484 (void) strlcpy(zent
->zroot
, "???", sizeof (zent
->zroot
));
485 (void) strlcpy(zent
->zbrand
, "???", sizeof (zent
->zbrand
));
486 zent
->zstate_str
= "???";
490 if (zonecfg_get_uuid(zone_name
, uuid
) == Z_OK
&&
492 uuid_unparse(uuid
, zent
->zuuid
);
494 zent
->zuuid
[0] = '\0';
497 * For labeled zones which query the zone path of lower-level
498 * zones, the path needs to be adjusted to drop the final
499 * "/root" component. This adjusted path is then useful
500 * for reading down any exported directories from the
503 if (is_system_labeled() && zent
->zid
!= ZONE_ID_UNDEFINED
) {
504 if (zone_getattr(zent
->zid
, ZONE_ATTR_ROOT
, zent
->zroot
,
505 sizeof (zent
->zroot
)) == -1) {
506 zperror2(zent
->zname
,
507 gettext("could not get zone path."));
510 cp
= zent
->zroot
+ strlen(zent
->zroot
) - 5;
511 if (cp
> zent
->zroot
&& strcmp(cp
, "/root") == 0)
514 if ((err
= zone_get_zonepath(zent
->zname
, root
,
515 sizeof (root
))) != Z_OK
) {
517 zperror2(zent
->zname
,
518 gettext("could not get zone path."));
521 (void) strlcpy(zent
->zroot
, root
, sizeof (zent
->zroot
));
524 if ((err
= zone_get_state(zent
->zname
, &zent
->zstate_num
)) != Z_OK
) {
526 zperror2(zent
->zname
, gettext("could not get state"));
529 zent
->zstate_str
= zone_state_str(zent
->zstate_num
);
532 * A zone's brand is only available in the .xml file describing it,
533 * which is only visible to the global zone. This causes
534 * zone_get_brand() to fail when called from within a non-global
535 * zone. Fortunately we only do this on labeled systems, where we
536 * know all zones are native.
538 if (getzoneid() != GLOBAL_ZONEID
) {
539 assert(is_system_labeled() != 0);
540 (void) strlcpy(zent
->zbrand
, default_brand
,
541 sizeof (zent
->zbrand
));
542 } else if (zone_get_brand(zent
->zname
, zent
->zbrand
,
543 sizeof (zent
->zbrand
)) != Z_OK
) {
544 zperror2(zent
->zname
, gettext("could not get brand name"));
549 * Get ip type of the zone.
550 * Note for global zone, ZS_SHARED is set always.
552 if (zid
== GLOBAL_ZONEID
) {
553 zent
->ziptype
= ZS_SHARED
;
558 * There is a race condition where the zone could boot while
559 * we're walking the index file. In this case the zone state
560 * could be seen as running from the call above, but the zoneid
561 * would be undefined.
563 * There is also a race condition where the zone could shutdown after
564 * we got its running state above. This is also not an error and
565 * we fall back to getting the ziptype from the zone configuration.
567 if (zent
->zstate_num
== ZONE_STATE_RUNNING
&&
568 zid
!= ZONE_ID_UNDEFINED
) {
571 if (zone_getattr(zid
, ZONE_ATTR_FLAGS
, &flags
,
572 sizeof (flags
)) >= 0) {
573 if (flags
& ZF_NET_EXCL
)
574 zent
->ziptype
= ZS_EXCLUSIVE
;
576 zent
->ziptype
= ZS_SHARED
;
581 if ((handle
= zonecfg_init_handle()) == NULL
) {
582 zperror2(zent
->zname
, gettext("could not init handle"));
585 if ((err
= zonecfg_get_handle(zent
->zname
, handle
)) != Z_OK
) {
586 zperror2(zent
->zname
, gettext("could not get handle"));
587 zonecfg_fini_handle(handle
);
591 if ((err
= zonecfg_get_iptype(handle
, &zent
->ziptype
)) != Z_OK
) {
592 zperror2(zent
->zname
, gettext("could not get ip-type"));
593 zonecfg_fini_handle(handle
);
596 zonecfg_fini_handle(handle
);
602 * fetch_zents() calls zone_list(2) to find out how many zones are running
603 * (which is stored in the global nzents), then calls zone_list(2) again
604 * to fetch the list of running zones (stored in the global zents). This
605 * function may be called multiple times, so if zents is already set, we
606 * return immediately to save work.
608 * Note that the data about running zones can change while this function
609 * is running, so its possible that the list of zones will have empty slots
616 zoneid_t
*zids
= NULL
;
627 if (zone_list(NULL
, &nzents
) != 0) {
628 zperror(gettext("failed to get zoneid list"), B_FALSE
);
636 zids
= safe_calloc(nzents
, sizeof (zoneid_t
));
637 nzents_saved
= nzents
;
639 if (zone_list(zids
, &nzents
) != 0) {
640 zperror(gettext("failed to get zone list"), B_FALSE
);
644 if (nzents
!= nzents_saved
) {
645 /* list changed, try again */
650 zents
= safe_calloc(nzents
, sizeof (zone_entry_t
));
652 inaltroot
= zonecfg_in_alt_root();
654 fp
= zonecfg_open_scratch("", B_FALSE
);
655 altroot
= zonecfg_get_root();
661 for (i
= 0; i
< nzents
; i
++) {
662 char name
[ZONENAME_MAX
];
663 char altname
[ZONENAME_MAX
];
664 char rev_altroot
[MAXPATHLEN
];
666 if (getzonenamebyid(zids
[i
], name
, sizeof (name
)) < 0) {
668 * There is a race condition where the zone may have
669 * shutdown since we retrieved the number of running
670 * zones above. This is not an error, there will be
671 * an empty slot at the end of the list.
675 if (zonecfg_is_scratch(name
)) {
676 /* Ignore scratch zones by default */
680 zonecfg_reverse_scratch(fp
, name
, altname
,
681 sizeof (altname
), rev_altroot
,
682 sizeof (rev_altroot
)) == -1) {
683 zerror(gettext("could not resolve scratch "
688 /* Ignore zones in other alternate roots */
689 if (strcmp(rev_altroot
, altroot
) != 0)
691 (void) strcpy(name
, altname
);
693 /* Ignore non-scratch when in an alternate root */
694 if (inaltroot
&& strcmp(name
, GLOBAL_ZONENAME
) != 0)
697 if (lookup_zone_info(name
, zids
[i
], zentp
) != Z_OK
) {
699 * There is a race condition where the zone may have
700 * shutdown since we retrieved the number of running
701 * zones above. This is not an error, there will be
702 * an empty slot at the end of the list.
708 nzents
= zentp
- zents
;
710 zonecfg_close_scratch(fp
);
717 zone_print_list(zone_state_t min_state
, boolean_t verbose
, boolean_t parsable
)
725 * First get the list of running zones from the kernel and print them.
726 * If that is all we need, then return.
728 if ((i
= fetch_zents()) != Z_OK
) {
730 * No need for error messages; fetch_zents() has already taken
735 for (i
= 0; i
< nzents
; i
++)
736 zone_print(&zents
[i
], verbose
, parsable
);
737 if (min_state
>= ZONE_STATE_RUNNING
)
740 * Next, get the full list of zones from the configuration, skipping
741 * any we have already printed.
743 cookie
= setzoneent();
744 while ((name
= getzoneent(cookie
)) != NULL
) {
745 for (i
= 0; i
< nzents
; i
++) {
746 if (strcmp(zents
[i
].zname
, name
) == 0)
753 if (lookup_zone_info(name
, ZONE_ID_UNDEFINED
, &zent
) != Z_OK
) {
758 if (zent
.zstate_num
>= min_state
)
759 zone_print(&zent
, verbose
, parsable
);
766 * Retrieve a zone entry by name. Returns NULL if no such zone exists.
768 static zone_entry_t
*
769 lookup_running_zone(const char *str
)
773 if (fetch_zents() != Z_OK
)
776 for (i
= 0; i
< nzents
; i
++) {
777 if (strcmp(str
, zents
[i
].zname
) == 0)
784 * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
785 * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect).
788 bad_mode_bit(mode_t mode
, mode_t bit
, boolean_t on
, char *file
)
792 assert(bit
== S_IRUSR
|| bit
== S_IWUSR
|| bit
== S_IXUSR
||
793 bit
== S_IRGRP
|| bit
== S_IWGRP
|| bit
== S_IXGRP
||
794 bit
== S_IROTH
|| bit
== S_IWOTH
|| bit
== S_IXOTH
);
797 * The strings below will be used as part of a larger message,
799 * (file name) must be (owner|group|world) (read|writ|execut)able
801 * (file name) must not be (owner|group|world) (read|writ|execut)able
805 str
= gettext("owner readable");
808 str
= gettext("owner writable");
811 str
= gettext("owner executable");
814 str
= gettext("group readable");
817 str
= gettext("group writable");
820 str
= gettext("group executable");
823 str
= gettext("world readable");
826 str
= gettext("world writable");
829 str
= gettext("world executable");
832 if ((mode
& bit
) == (on
? 0 : bit
)) {
835 * The first parameter below is a file name; the second
836 * is one of the "(owner|group|world) (read|writ|execut)able"
837 * strings from above.
840 * The code below could be simplified but not in a way
841 * that would easily translate to non-English locales.
844 (void) fprintf(stderr
, gettext("%s must be %s.\n"),
847 (void) fprintf(stderr
, gettext("%s must not be %s.\n"),
856 * We want to make sure that no zone has its zone path as a child node
857 * (in the directory sense) of any other. We do that by comparing this
858 * zone's path to the path of all other (non-global) zones. The comparison
859 * in each case is simple: add '/' to the end of the path, then do a
860 * strncmp() of the two paths, using the length of the shorter one.
864 crosscheck_zonepaths(char *path
)
866 char rpath
[MAXPATHLEN
]; /* resolved path */
867 char path_copy
[MAXPATHLEN
]; /* copy of original path */
868 char rpath_copy
[MAXPATHLEN
]; /* copy of original rpath */
873 cookie
= setzoneent();
874 while ((ze
= getzoneent_private(cookie
)) != NULL
) {
875 /* Skip zones which are not installed. */
876 if (ze
->zone_state
< ZONE_STATE_INSTALLED
) {
880 /* Skip the global zone and the current target zone. */
881 if (strcmp(ze
->zone_name
, GLOBAL_ZONENAME
) == 0 ||
882 strcmp(ze
->zone_name
, target_zone
) == 0) {
886 if (strlen(ze
->zone_path
) == 0) {
887 /* old index file without path, fall back */
888 if ((err
= zone_get_zonepath(ze
->zone_name
,
889 ze
->zone_path
, sizeof (ze
->zone_path
))) != Z_OK
) {
891 zperror2(ze
->zone_name
,
892 gettext("could not get zone path"));
897 (void) snprintf(path_copy
, sizeof (path_copy
), "%s%s",
898 zonecfg_get_root(), ze
->zone_path
);
899 res
= resolvepath(path_copy
, rpath
, sizeof (rpath
));
901 if (errno
!= ENOENT
) {
902 zperror(path_copy
, B_FALSE
);
906 (void) printf(gettext("WARNING: zone %s is installed, "
907 "but its %s %s does not exist.\n"), ze
->zone_name
,
908 "zonepath", path_copy
);
913 (void) snprintf(path_copy
, sizeof (path_copy
), "%s/", path
);
914 (void) snprintf(rpath_copy
, sizeof (rpath_copy
), "%s/", rpath
);
915 if (strncmp(path_copy
, rpath_copy
,
916 min(strlen(path_copy
), strlen(rpath_copy
))) == 0) {
919 * zonepath is a literal that should not be translated.
921 (void) fprintf(stderr
, gettext("%s zonepath (%s) and "
922 "%s zonepath (%s) overlap.\n"),
923 target_zone
, path
, ze
->zone_name
, rpath
);
934 validate_zonepath(char *path
, int cmd_num
)
936 int res
; /* result of last library/system call */
937 boolean_t err
= B_FALSE
; /* have we run into an error? */
939 struct statvfs64 vfsbuf
;
940 char rpath
[MAXPATHLEN
]; /* resolved path */
941 char ppath
[MAXPATHLEN
]; /* parent path */
942 char rppath
[MAXPATHLEN
]; /* resolved parent path */
943 char rootpath
[MAXPATHLEN
]; /* root path */
946 if (path
[0] != '/') {
947 (void) fprintf(stderr
,
948 gettext("%s is not an absolute path.\n"), path
);
951 if ((res
= resolvepath(path
, rpath
, sizeof (rpath
))) == -1) {
952 if ((errno
!= ENOENT
) ||
953 (cmd_num
!= CMD_VERIFY
&& cmd_num
!= CMD_INSTALL
&&
954 cmd_num
!= CMD_CLONE
&& cmd_num
!= CMD_MOVE
)) {
955 zperror(path
, B_FALSE
);
958 if (cmd_num
== CMD_VERIFY
) {
961 * zoneadm is a literal that should not be translated.
963 (void) fprintf(stderr
, gettext("WARNING: %s does not "
964 "exist, so it could not be verified.\nWhen "
965 "'zoneadm %s' is run, '%s' will try to create\n%s, "
966 "and '%s' will be tried again,\nbut the '%s' may "
967 "fail if:\nthe parent directory of %s is group- or "
968 "other-writable\nor\n%s overlaps with any other "
969 "installed zones.\n"), path
,
970 cmd_to_str(CMD_INSTALL
), cmd_to_str(CMD_INSTALL
),
971 path
, cmd_to_str(CMD_VERIFY
),
972 cmd_to_str(CMD_VERIFY
), path
, path
);
976 * The zonepath is supposed to be mode 700 but its
977 * parent(s) 755. So use 755 on the mkdirp() then
978 * chmod() the zonepath itself to 700.
980 if (mkdirp(path
, DEFAULT_DIR_MODE
) < 0) {
981 zperror(path
, B_FALSE
);
985 * If the chmod() fails, report the error, but might
986 * as well continue the verify procedure.
988 if (chmod(path
, S_IRWXU
) != 0)
989 zperror(path
, B_FALSE
);
991 * Since the mkdir() succeeded, we should not have to
992 * worry about a subsequent ENOENT, thus this should
995 return (validate_zonepath(path
, cmd_num
));
998 if (strcmp(path
, rpath
) != 0) {
999 errno
= Z_RESOLVED_PATH
;
1000 zperror(path
, B_TRUE
);
1003 if ((res
= stat(rpath
, &stbuf
)) != 0) {
1004 zperror(rpath
, B_FALSE
);
1007 if (!S_ISDIR(stbuf
.st_mode
)) {
1008 (void) fprintf(stderr
, gettext("%s is not a directory.\n"),
1012 if (strcmp(stbuf
.st_fstype
, MNTTYPE_TMPFS
) == 0) {
1013 (void) printf(gettext("WARNING: %s is on a temporary "
1014 "file system.\n"), rpath
);
1016 if (crosscheck_zonepaths(rpath
) != Z_OK
)
1019 * Try to collect and report as many minor errors as possible
1020 * before returning, so the user can learn everything that needs
1021 * to be fixed up front.
1023 if (stbuf
.st_uid
!= 0) {
1024 (void) fprintf(stderr
, gettext("%s is not owned by root.\n"),
1028 /* Try to change owner */
1029 if (cmd_num
!= CMD_VERIFY
) {
1030 (void) fprintf(stderr
, gettext("%s: changing owner "
1031 "to root.\n"), rpath
);
1032 if (chown(rpath
, 0, -1) != 0) {
1033 zperror(rpath
, B_FALSE
);
1040 err
|= bad_mode_bit(stbuf
.st_mode
, S_IRUSR
, B_TRUE
, rpath
);
1041 err
|= bad_mode_bit(stbuf
.st_mode
, S_IWUSR
, B_TRUE
, rpath
);
1042 err
|= bad_mode_bit(stbuf
.st_mode
, S_IXUSR
, B_TRUE
, rpath
);
1043 err
|= bad_mode_bit(stbuf
.st_mode
, S_IRGRP
, B_FALSE
, rpath
);
1044 err
|= bad_mode_bit(stbuf
.st_mode
, S_IWGRP
, B_FALSE
, rpath
);
1045 err
|= bad_mode_bit(stbuf
.st_mode
, S_IXGRP
, B_FALSE
, rpath
);
1046 err
|= bad_mode_bit(stbuf
.st_mode
, S_IROTH
, B_FALSE
, rpath
);
1047 err
|= bad_mode_bit(stbuf
.st_mode
, S_IWOTH
, B_FALSE
, rpath
);
1048 err
|= bad_mode_bit(stbuf
.st_mode
, S_IXOTH
, B_FALSE
, rpath
);
1050 /* If the group perms are wrong, fix them */
1051 if (err
&& (cmd_num
!= CMD_VERIFY
)) {
1052 (void) fprintf(stderr
, gettext("%s: changing permissions "
1053 "to 0700.\n"), rpath
);
1054 if (chmod(rpath
, S_IRWXU
) != 0) {
1055 zperror(path
, B_FALSE
);
1061 (void) snprintf(ppath
, sizeof (ppath
), "%s/..", path
);
1062 if ((res
= resolvepath(ppath
, rppath
, sizeof (rppath
))) == -1) {
1063 zperror(ppath
, B_FALSE
);
1067 if ((res
= stat(rppath
, &stbuf
)) != 0) {
1068 zperror(rppath
, B_FALSE
);
1071 /* theoretically impossible */
1072 if (!S_ISDIR(stbuf
.st_mode
)) {
1073 (void) fprintf(stderr
, gettext("%s is not a directory.\n"),
1077 if (stbuf
.st_uid
!= 0) {
1078 (void) fprintf(stderr
, gettext("%s is not owned by root.\n"),
1082 err
|= bad_mode_bit(stbuf
.st_mode
, S_IRUSR
, B_TRUE
, rppath
);
1083 err
|= bad_mode_bit(stbuf
.st_mode
, S_IWUSR
, B_TRUE
, rppath
);
1084 err
|= bad_mode_bit(stbuf
.st_mode
, S_IXUSR
, B_TRUE
, rppath
);
1085 err
|= bad_mode_bit(stbuf
.st_mode
, S_IWGRP
, B_FALSE
, rppath
);
1086 err
|= bad_mode_bit(stbuf
.st_mode
, S_IWOTH
, B_FALSE
, rppath
);
1087 if (strcmp(rpath
, rppath
) == 0) {
1088 (void) fprintf(stderr
, gettext("%s is its own parent.\n"),
1093 if (statvfs64(rpath
, &vfsbuf
) != 0) {
1094 zperror(rpath
, B_FALSE
);
1097 if (strcmp(vfsbuf
.f_basetype
, MNTTYPE_NFS
) == 0) {
1100 * Zonepath and NFS are literals that should not be translated.
1102 (void) fprintf(stderr
, gettext("Zonepath %s is on an NFS "
1103 "mounted file system.\n"
1104 "\tA local file system must be used.\n"), rpath
);
1107 if (vfsbuf
.f_flag
& ST_NOSUID
) {
1110 * Zonepath and nosuid are literals that should not be
1113 (void) fprintf(stderr
, gettext("Zonepath %s is on a nosuid "
1114 "file system.\n"), rpath
);
1118 if ((res
= zone_get_state(target_zone
, &state
)) != Z_OK
) {
1120 zperror2(target_zone
, gettext("could not get state"));
1124 * The existence of the root path is only bad in the configured state,
1125 * as it is *supposed* to be there at the installed and later states.
1126 * However, the root path is expected to be there if the zone is
1128 * State/command mismatches are caught earlier in verify_details().
1130 if (state
== ZONE_STATE_CONFIGURED
&& cmd_num
!= CMD_ATTACH
) {
1131 if (snprintf(rootpath
, sizeof (rootpath
), "%s/root", rpath
) >=
1132 sizeof (rootpath
)) {
1135 * Zonepath is a literal that should not be translated.
1137 (void) fprintf(stderr
,
1138 gettext("Zonepath %s is too long.\n"), rpath
);
1141 if ((res
= stat(rootpath
, &stbuf
)) == 0) {
1142 if (zonecfg_detached(rpath
)) {
1143 (void) fprintf(stderr
,
1144 gettext("Cannot %s detached "
1145 "zone.\nUse attach or remove %s "
1146 "directory.\n"), cmd_to_str(cmd_num
),
1151 /* Not detached, check if it really looks ok. */
1153 if (!S_ISDIR(stbuf
.st_mode
)) {
1154 (void) fprintf(stderr
, gettext("%s is not a "
1155 "directory.\n"), rootpath
);
1159 if (stbuf
.st_uid
!= 0) {
1160 (void) fprintf(stderr
, gettext("%s is not "
1161 "owned by root.\n"), rootpath
);
1165 if ((stbuf
.st_mode
& 0777) != 0755) {
1166 (void) fprintf(stderr
, gettext("%s mode is not "
1167 "0755.\n"), rootpath
);
1173 return (err
? Z_ERR
: Z_OK
);
1177 invoke_brand_handler(int cmd_num
, char *argv
[])
1179 zone_dochandle_t handle
;
1182 if ((handle
= zonecfg_init_handle()) == NULL
) {
1183 zperror(cmd_to_str(cmd_num
), B_TRUE
);
1186 if ((err
= zonecfg_get_handle(target_zone
, handle
)) != Z_OK
) {
1188 zperror(cmd_to_str(cmd_num
), B_TRUE
);
1189 zonecfg_fini_handle(handle
);
1192 if (verify_brand(handle
, cmd_num
, argv
) != Z_OK
) {
1193 zonecfg_fini_handle(handle
);
1196 zonecfg_fini_handle(handle
);
1201 ready_func(int argc
, char *argv
[])
1203 zone_cmd_arg_t zarg
;
1206 if (zonecfg_in_alt_root()) {
1207 zerror(gettext("cannot ready zone in alternate root"));
1212 if ((arg
= getopt(argc
, argv
, "?")) != EOF
) {
1215 sub_usage(SHELP_READY
, CMD_READY
);
1216 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
1218 sub_usage(SHELP_READY
, CMD_READY
);
1222 if (argc
> optind
) {
1223 sub_usage(SHELP_READY
, CMD_READY
);
1226 if (sanity_check(target_zone
, CMD_READY
, B_FALSE
, B_FALSE
, B_FALSE
)
1229 if (verify_details(CMD_READY
, argv
) != Z_OK
)
1233 if (zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
, B_TRUE
) != 0) {
1234 zerror(gettext("call to %s failed"), "zoneadmd");
1241 boot_func(int argc
, char *argv
[])
1243 zone_cmd_arg_t zarg
;
1244 boolean_t force
= B_FALSE
;
1247 if (zonecfg_in_alt_root()) {
1248 zerror(gettext("cannot boot zone in alternate root"));
1252 zarg
.bootbuf
[0] = '\0';
1255 * The following getopt processes arguments to zone boot; that
1256 * is to say, the [here] portion of the argument string:
1258 * zoneadm -z myzone boot [here] -- -v -m verbose
1260 * Where [here] can either be nothing, -? (in which case we bail
1261 * and print usage), -f (a private option to indicate that the
1262 * boot operation should be 'forced'), or -s. Support for -s is
1263 * vestigal and obsolete, but is retained because it was a
1264 * documented interface and there are known consumers including
1265 * admin/install; the proper way to specify boot arguments like -s
1268 * zoneadm -z myzone boot -- -s -v -m verbose.
1271 while ((arg
= getopt(argc
, argv
, "?fs")) != EOF
) {
1274 sub_usage(SHELP_BOOT
, CMD_BOOT
);
1275 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
1277 (void) strlcpy(zarg
.bootbuf
, "-s",
1278 sizeof (zarg
.bootbuf
));
1284 sub_usage(SHELP_BOOT
, CMD_BOOT
);
1289 for (; optind
< argc
; optind
++) {
1290 if (strlcat(zarg
.bootbuf
, argv
[optind
],
1291 sizeof (zarg
.bootbuf
)) >= sizeof (zarg
.bootbuf
)) {
1292 zerror(gettext("Boot argument list too long"));
1295 if (optind
< argc
- 1)
1296 if (strlcat(zarg
.bootbuf
, " ", sizeof (zarg
.bootbuf
)) >=
1297 sizeof (zarg
.bootbuf
)) {
1298 zerror(gettext("Boot argument list too long"));
1302 if (sanity_check(target_zone
, CMD_BOOT
, B_FALSE
, B_FALSE
, force
)
1305 if (verify_details(CMD_BOOT
, argv
) != Z_OK
)
1307 zarg
.cmd
= force
? Z_FORCEBOOT
: Z_BOOT
;
1308 if (zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
, B_TRUE
) != 0) {
1309 zerror(gettext("call to %s failed"), "zoneadmd");
1317 fake_up_local_zone(zoneid_t zid
, zone_entry_t
*zeptr
)
1324 (void) memset(zeptr
, 0, sizeof (*zeptr
));
1329 * Since we're looking up our own (non-global) zone name,
1330 * we can be assured that it will succeed.
1332 result
= getzonenamebyid(zid
, zeptr
->zname
, sizeof (zeptr
->zname
));
1333 assert(result
>= 0);
1334 if (zonecfg_is_scratch(zeptr
->zname
) &&
1335 (fp
= zonecfg_open_scratch("", B_FALSE
)) != NULL
) {
1336 (void) zonecfg_reverse_scratch(fp
, zeptr
->zname
, zeptr
->zname
,
1337 sizeof (zeptr
->zname
), NULL
, 0);
1338 zonecfg_close_scratch(fp
);
1341 if (is_system_labeled()) {
1342 (void) zone_getattr(zid
, ZONE_ATTR_ROOT
, zeptr
->zroot
,
1343 sizeof (zeptr
->zroot
));
1344 (void) strlcpy(zeptr
->zbrand
, NATIVE_BRAND_NAME
,
1345 sizeof (zeptr
->zbrand
));
1347 (void) strlcpy(zeptr
->zroot
, "/", sizeof (zeptr
->zroot
));
1348 (void) zone_getattr(zid
, ZONE_ATTR_BRAND
, zeptr
->zbrand
,
1349 sizeof (zeptr
->zbrand
));
1352 zeptr
->zstate_str
= "running";
1353 if (zonecfg_get_uuid(zeptr
->zname
, uuid
) == Z_OK
&&
1354 !uuid_is_null(uuid
))
1355 uuid_unparse(uuid
, zeptr
->zuuid
);
1357 if (zone_getattr(zid
, ZONE_ATTR_FLAGS
, &flags
, sizeof (flags
)) < 0) {
1358 zperror2(zeptr
->zname
, gettext("could not get zone flags"));
1361 if (flags
& ZF_NET_EXCL
)
1362 zeptr
->ziptype
= ZS_EXCLUSIVE
;
1364 zeptr
->ziptype
= ZS_SHARED
;
1368 list_func(int argc
, char *argv
[])
1370 zone_entry_t
*zentp
, zent
;
1372 boolean_t output
= B_FALSE
, verbose
= B_FALSE
, parsable
= B_FALSE
;
1373 zone_state_t min_state
= ZONE_STATE_RUNNING
;
1374 zoneid_t zone_id
= getzoneid();
1376 if (target_zone
== NULL
) {
1377 /* all zones: default view to running but allow override */
1379 while ((arg
= getopt(argc
, argv
, "?cipv")) != EOF
) {
1382 sub_usage(SHELP_LIST
, CMD_LIST
);
1383 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
1385 * The 'i' and 'c' options are not mutually
1386 * exclusive so if 'c' is given, then min_state
1387 * is set to 0 (ZONE_STATE_CONFIGURED) which is
1388 * the lowest possible state. If 'i' is given,
1389 * then min_state is set to be the lowest state
1393 min_state
= ZONE_STATE_CONFIGURED
;
1396 min_state
= min(ZONE_STATE_INSTALLED
,
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 (zone_id
== GLOBAL_ZONEID
|| is_system_labeled()) {
1417 retv
= zone_print_list(min_state
, verbose
, parsable
);
1419 fake_up_local_zone(zone_id
, &zent
);
1421 zone_print(&zent
, verbose
, parsable
);
1427 * Specific target zone: disallow -i/-c suboptions.
1430 while ((arg
= getopt(argc
, argv
, "?pv")) != EOF
) {
1433 sub_usage(SHELP_LIST
, CMD_LIST
);
1434 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
1442 sub_usage(SHELP_LIST
, CMD_LIST
);
1446 if (parsable
&& verbose
) {
1447 zerror(gettext("%s -p and -v are mutually exclusive."),
1448 cmd_to_str(CMD_LIST
));
1451 if (argc
> optind
) {
1452 sub_usage(SHELP_LIST
, CMD_LIST
);
1455 if (zone_id
!= GLOBAL_ZONEID
&& !is_system_labeled()) {
1456 fake_up_local_zone(zone_id
, &zent
);
1458 * main() will issue a Z_NO_ZONE error if it cannot get an
1459 * id for target_zone, which in a non-global zone should
1460 * happen for any zone name except `zonename`. Thus we
1461 * assert() that here but don't otherwise check.
1463 assert(strcmp(zent
.zname
, target_zone
) == 0);
1464 zone_print(&zent
, verbose
, parsable
);
1466 } else if ((zentp
= lookup_running_zone(target_zone
)) != NULL
) {
1467 zone_print(zentp
, verbose
, parsable
);
1469 } else if (lookup_zone_info(target_zone
, ZONE_ID_UNDEFINED
,
1471 zone_print(&zent
, verbose
, parsable
);
1476 * Invoke brand-specific handler. Note that we do this
1477 * only if we're in the global zone, and target_zone is specified
1478 * and it is not the global zone.
1480 if (zone_id
== GLOBAL_ZONEID
&& target_zone
!= NULL
&&
1481 strcmp(target_zone
, GLOBAL_ZONENAME
) != 0)
1482 if (invoke_brand_handler(CMD_LIST
, argv
) != Z_OK
)
1485 return (output
? Z_OK
: Z_ERR
);
1489 do_subproc(char *cmdbuf
)
1491 void (*saveint
)(int);
1492 void (*saveterm
)(int);
1493 void (*savequit
)(int);
1494 void (*savehup
)(int);
1495 int pid
, child
, status
;
1497 if ((child
= vfork()) == 0) {
1498 (void) execl("/bin/sh", "sh", "-c", cmdbuf
, (char *)NULL
);
1504 saveint
= sigset(SIGINT
, SIG_IGN
);
1505 saveterm
= sigset(SIGTERM
, SIG_IGN
);
1506 savequit
= sigset(SIGQUIT
, SIG_IGN
);
1507 savehup
= sigset(SIGHUP
, SIG_IGN
);
1509 while ((pid
= waitpid(child
, &status
, 0)) != child
&& pid
!= -1)
1512 (void) sigset(SIGINT
, saveint
);
1513 (void) sigset(SIGTERM
, saveterm
);
1514 (void) sigset(SIGQUIT
, savequit
);
1515 (void) sigset(SIGHUP
, savehup
);
1517 return (pid
== -1 ? -1 : status
);
1521 subproc_status(const char *cmd
, int status
, boolean_t verbose_failure
)
1523 if (WIFEXITED(status
)) {
1524 int exit_code
= WEXITSTATUS(status
);
1526 if ((verbose_failure
) && (exit_code
!= ZONE_SUBPROC_OK
))
1527 zerror(gettext("'%s' failed with exit code %d."), cmd
,
1531 } else if (WIFSIGNALED(status
)) {
1532 int signal
= WTERMSIG(status
);
1533 char sigstr
[SIG2STR_MAX
];
1535 if (sig2str(signal
, sigstr
) == 0) {
1536 zerror(gettext("'%s' terminated by signal SIG%s."), cmd
,
1539 zerror(gettext("'%s' terminated by an unknown signal."),
1543 zerror(gettext("'%s' failed for unknown reasons."), cmd
);
1547 * Assume a subprocess that died due to a signal or an unknown error
1548 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the
1549 * user will likely need to do some manual cleanup.
1551 return (ZONE_SUBPROC_FATAL
);
1555 auth_check(char *user
, char *zone
, int cmd_num
)
1557 char authname
[MAXAUTHS
];
1564 (void) strlcpy(authname
, ZONE_CLONEFROM_AUTH
, MAXAUTHS
);
1584 (void) strlcpy(authname
, ZONE_MANAGE_AUTH
, MAXAUTHS
);
1587 (void) strlcat(authname
, KV_OBJECT
, MAXAUTHS
);
1588 (void) strlcat(authname
, zone
, MAXAUTHS
);
1589 if (chkauthattr(authname
, user
) == 0) {
1593 * Some subcommands, e.g. install, run subcommands,
1594 * e.g. sysidcfg, that require a real uid of root,
1595 * so switch to root, here.
1597 if (setuid(0) == -1) {
1598 zperror(gettext("insufficient privilege"), B_TRUE
);
1606 * Various sanity checks; make sure:
1607 * 1. We're in the global zone.
1608 * 2. The calling user has sufficient privilege.
1609 * 3. The target zone is neither the global zone nor anything starting with
1611 * 4a. If we're looking for a 'not running' (i.e., configured or installed)
1612 * zone, the name service knows about it.
1613 * 4b. For some operations which expect a zone not to be running, that it is
1614 * not already running (or ready).
1617 sanity_check(char *zone
, int cmd_num
, boolean_t running
,
1618 boolean_t unsafe_when_running
, boolean_t force
)
1621 priv_set_t
*privset
;
1622 zone_state_t state
, min_state
;
1623 char kernzone
[ZONENAME_MAX
];
1626 if (getzoneid() != GLOBAL_ZONEID
) {
1629 zerror(gettext("use %s to %s this zone."), "halt(1M)",
1630 cmd_to_str(cmd_num
));
1633 zerror(gettext("use %s to %s this zone."),
1634 "shutdown(1M)", cmd_to_str(cmd_num
));
1637 zerror(gettext("use %s to %s this zone."),
1638 "reboot(1M)", cmd_to_str(cmd_num
));
1641 zerror(gettext("must be in the global zone to %s a "
1642 "zone."), cmd_to_str(cmd_num
));
1648 if ((privset
= priv_allocset()) == NULL
) {
1649 zerror(gettext("%s failed"), "priv_allocset");
1653 if (getppriv(PRIV_EFFECTIVE
, privset
) != 0) {
1654 zerror(gettext("%s failed"), "getppriv");
1655 priv_freeset(privset
);
1659 if (priv_isfullset(privset
) == B_FALSE
) {
1660 zerror(gettext("only a privileged user may %s a zone."),
1661 cmd_to_str(cmd_num
));
1662 priv_freeset(privset
);
1665 priv_freeset(privset
);
1668 zerror(gettext("no zone specified"));
1672 if (auth_check(username
, zone
, cmd_num
) == Z_ERR
) {
1673 zerror(gettext("User %s is not authorized to %s this zone."),
1674 username
, cmd_to_str(cmd_num
));
1678 if (strcmp(zone
, GLOBAL_ZONENAME
) == 0) {
1679 zerror(gettext("%s operation is invalid for the global zone."),
1680 cmd_to_str(cmd_num
));
1684 if (strncmp(zone
, "SUNW", 4) == 0) {
1685 zerror(gettext("%s operation is invalid for zones starting "
1686 "with SUNW."), cmd_to_str(cmd_num
));
1690 if (!zonecfg_in_alt_root()) {
1691 zent
= lookup_running_zone(zone
);
1692 } else if ((fp
= zonecfg_open_scratch("", B_FALSE
)) == NULL
) {
1695 if (zonecfg_find_scratch(fp
, zone
, zonecfg_get_root(),
1696 kernzone
, sizeof (kernzone
)) == 0)
1697 zent
= lookup_running_zone(kernzone
);
1700 zonecfg_close_scratch(fp
);
1704 * Look up from the kernel for 'running' zones.
1706 if (running
&& !force
) {
1708 zerror(gettext("not running"));
1714 if (unsafe_when_running
&& zent
!= NULL
) {
1715 /* check whether the zone is ready or running */
1716 if ((err
= zone_get_state(zent
->zname
,
1717 &zent
->zstate_num
)) != Z_OK
) {
1719 zperror2(zent
->zname
,
1720 gettext("could not get state"));
1721 /* can't tell, so hedge */
1722 zent
->zstate_str
= "ready/running";
1725 zone_state_str(zent
->zstate_num
);
1727 zerror(gettext("%s operation is invalid for %s zones."),
1728 cmd_to_str(cmd_num
), zent
->zstate_str
);
1731 if ((err
= zone_get_state(zone
, &state
)) != Z_OK
) {
1733 zperror2(zone
, gettext("could not get state"));
1738 if (state
== ZONE_STATE_CONFIGURED
) {
1739 zerror(gettext("is already in state '%s'."),
1740 zone_state_str(ZONE_STATE_CONFIGURED
));
1745 if (state
== ZONE_STATE_INSTALLED
) {
1746 zerror(gettext("is already %s."),
1747 zone_state_str(ZONE_STATE_INSTALLED
));
1749 } else if (state
== ZONE_STATE_INCOMPLETE
&& !force
) {
1750 zerror(gettext("zone is %s; %s required."),
1751 zone_state_str(ZONE_STATE_INCOMPLETE
),
1752 cmd_to_str(CMD_UNINSTALL
));
1758 if (state
== ZONE_STATE_INSTALLED
) {
1759 zerror(gettext("is already %s."),
1760 zone_state_str(ZONE_STATE_INSTALLED
));
1762 } else if (state
== ZONE_STATE_INCOMPLETE
) {
1763 zerror(gettext("zone is %s; %s required."),
1764 zone_state_str(ZONE_STATE_INCOMPLETE
),
1765 cmd_to_str(CMD_UNINSTALL
));
1775 if ((cmd_num
== CMD_BOOT
|| cmd_num
== CMD_MOUNT
) &&
1777 min_state
= ZONE_STATE_INCOMPLETE
;
1778 else if (cmd_num
== CMD_MARK
)
1779 min_state
= ZONE_STATE_CONFIGURED
;
1781 min_state
= ZONE_STATE_INSTALLED
;
1783 if (state
< min_state
) {
1784 zerror(gettext("must be %s before %s."),
1785 zone_state_str(min_state
),
1786 cmd_to_str(cmd_num
));
1791 if (state
== ZONE_STATE_INCOMPLETE
) {
1792 zerror(gettext("zone is %s; %s required."),
1793 zone_state_str(ZONE_STATE_INCOMPLETE
),
1794 cmd_to_str(CMD_UNINSTALL
));
1799 if (state
!= ZONE_STATE_MOUNTED
) {
1800 zerror(gettext("must be %s before %s."),
1801 zone_state_str(ZONE_STATE_MOUNTED
),
1802 cmd_to_str(cmd_num
));
1807 if (state
!= ZONE_STATE_INSTALLED
) {
1808 zerror(gettext("%s operation is invalid for %s "
1809 "zones."), cmd_to_str(cmd_num
),
1810 zone_state_str(state
));
1820 halt_func(int argc
, char *argv
[])
1822 zone_cmd_arg_t zarg
;
1825 if (zonecfg_in_alt_root()) {
1826 zerror(gettext("cannot halt zone in alternate root"));
1831 if ((arg
= getopt(argc
, argv
, "?")) != EOF
) {
1834 sub_usage(SHELP_HALT
, CMD_HALT
);
1835 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
1837 sub_usage(SHELP_HALT
, CMD_HALT
);
1841 if (argc
> optind
) {
1842 sub_usage(SHELP_HALT
, CMD_HALT
);
1846 * zoneadmd should be the one to decide whether or not to proceed,
1847 * so even though it seems that the fourth parameter below should
1848 * perhaps be B_TRUE, it really shouldn't be.
1850 if (sanity_check(target_zone
, CMD_HALT
, B_FALSE
, B_FALSE
, B_FALSE
)
1855 * Invoke brand-specific handler.
1857 if (invoke_brand_handler(CMD_HALT
, argv
) != Z_OK
)
1861 return ((zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
,
1862 B_TRUE
) == 0) ? Z_OK
: Z_ERR
);
1866 shutdown_func(int argc
, char *argv
[])
1868 zone_cmd_arg_t zarg
;
1870 boolean_t reboot
= B_FALSE
;
1872 zarg
.cmd
= Z_SHUTDOWN
;
1874 if (zonecfg_in_alt_root()) {
1875 zerror(gettext("cannot shut down zone in alternate root"));
1880 while ((arg
= getopt(argc
, argv
, "?r")) != EOF
) {
1883 sub_usage(SHELP_SHUTDOWN
, CMD_SHUTDOWN
);
1884 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
1889 sub_usage(SHELP_SHUTDOWN
, CMD_SHUTDOWN
);
1894 zarg
.bootbuf
[0] = '\0';
1895 for (; optind
< argc
; optind
++) {
1896 if (strlcat(zarg
.bootbuf
, argv
[optind
],
1897 sizeof (zarg
.bootbuf
)) >= sizeof (zarg
.bootbuf
)) {
1898 zerror(gettext("Boot argument list too long"));
1901 if (optind
< argc
- 1)
1902 if (strlcat(zarg
.bootbuf
, " ", sizeof (zarg
.bootbuf
)) >=
1903 sizeof (zarg
.bootbuf
)) {
1904 zerror(gettext("Boot argument list too long"));
1910 * zoneadmd should be the one to decide whether or not to proceed,
1911 * so even though it seems that the third parameter below should
1912 * perhaps be B_TRUE, it really shouldn't be.
1914 if (sanity_check(target_zone
, CMD_SHUTDOWN
, B_TRUE
, B_FALSE
, B_FALSE
)
1918 if (zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
, B_TRUE
) != Z_OK
)
1922 if (sanity_check(target_zone
, CMD_BOOT
, B_FALSE
, B_FALSE
,
1927 if (zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
,
1935 reboot_func(int argc
, char *argv
[])
1937 zone_cmd_arg_t zarg
;
1940 if (zonecfg_in_alt_root()) {
1941 zerror(gettext("cannot reboot zone in alternate root"));
1946 if ((arg
= getopt(argc
, argv
, "?")) != EOF
) {
1949 sub_usage(SHELP_REBOOT
, CMD_REBOOT
);
1950 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
1952 sub_usage(SHELP_REBOOT
, CMD_REBOOT
);
1957 zarg
.bootbuf
[0] = '\0';
1958 for (; optind
< argc
; optind
++) {
1959 if (strlcat(zarg
.bootbuf
, argv
[optind
],
1960 sizeof (zarg
.bootbuf
)) >= sizeof (zarg
.bootbuf
)) {
1961 zerror(gettext("Boot argument list too long"));
1964 if (optind
< argc
- 1)
1965 if (strlcat(zarg
.bootbuf
, " ", sizeof (zarg
.bootbuf
)) >=
1966 sizeof (zarg
.bootbuf
)) {
1967 zerror(gettext("Boot argument list too long"));
1974 * zoneadmd should be the one to decide whether or not to proceed,
1975 * so even though it seems that the fourth parameter below should
1976 * perhaps be B_TRUE, it really shouldn't be.
1978 if (sanity_check(target_zone
, CMD_REBOOT
, B_TRUE
, B_FALSE
, B_FALSE
)
1981 if (verify_details(CMD_REBOOT
, argv
) != Z_OK
)
1984 zarg
.cmd
= Z_REBOOT
;
1985 return ((zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
, B_TRUE
) == 0)
1990 get_hook(brand_handle_t bh
, char *cmd
, size_t len
, int (*bp
)(brand_handle_t
,
1991 const char *, const char *, char *, size_t), char *zonename
, char *zonepath
)
1993 if (strlcpy(cmd
, EXEC_PREFIX
, len
) >= len
)
1996 if (bp(bh
, zonename
, zonepath
, cmd
+ EXEC_LEN
, len
- EXEC_LEN
) != 0)
1999 if (strlen(cmd
) <= EXEC_LEN
)
2006 verify_brand(zone_dochandle_t handle
, int cmd_num
, char *argv
[])
2008 char cmdbuf
[MAXPATHLEN
];
2010 char zonepath
[MAXPATHLEN
];
2011 brand_handle_t bh
= NULL
;
2015 * Fetch the verify command from the brand configuration.
2016 * "exec" the command so that the returned status is that of
2017 * the command and not the shell.
2019 if (handle
== NULL
) {
2020 (void) strlcpy(zonepath
, "-", sizeof (zonepath
));
2021 } else if ((err
= zonecfg_get_zonepath(handle
, zonepath
,
2022 sizeof (zonepath
))) != Z_OK
) {
2024 zperror(cmd_to_str(cmd_num
), B_TRUE
);
2027 if ((bh
= brand_open(target_brand
)) == NULL
) {
2028 zerror(gettext("missing or invalid brand"));
2033 * If the brand has its own verification routine, execute it now.
2034 * The verification routine validates the intended zoneadm
2035 * operation for the specific brand. The zoneadm subcommand and
2036 * all its arguments are passed to the routine.
2038 err
= get_hook(bh
, cmdbuf
, sizeof (cmdbuf
), brand_get_verify_adm
,
2039 target_zone
, zonepath
);
2042 return (Z_BRAND_ERROR
);
2043 if (cmdbuf
[0] == '\0')
2046 if (strlcat(cmdbuf
, cmd_to_str(cmd_num
),
2047 sizeof (cmdbuf
)) >= sizeof (cmdbuf
))
2050 /* Build the argv string */
2052 while (argv
[i
] != NULL
) {
2053 if ((strlcat(cmdbuf
, " ",
2054 sizeof (cmdbuf
)) >= sizeof (cmdbuf
)) ||
2055 (strlcat(cmdbuf
, argv
[i
++],
2056 sizeof (cmdbuf
)) >= sizeof (cmdbuf
)))
2060 status
= do_subproc(cmdbuf
);
2061 err
= subproc_status(gettext("brand-specific verification"),
2064 return ((err
== ZONE_SUBPROC_OK
) ? Z_OK
: Z_BRAND_ERROR
);
2068 verify_rctls(zone_dochandle_t handle
)
2070 struct zone_rctltab rctltab
;
2071 size_t rbs
= rctlblk_size();
2073 int error
= Z_INVAL
;
2075 if ((rctlblk
= malloc(rbs
)) == NULL
) {
2076 zerror(gettext("failed to allocate %lu bytes: %s"), rbs
,
2081 if (zonecfg_setrctlent(handle
) != Z_OK
) {
2082 zerror(gettext("zonecfg_setrctlent failed"));
2087 rctltab
.zone_rctl_valptr
= NULL
;
2088 while (zonecfg_getrctlent(handle
, &rctltab
) == Z_OK
) {
2089 struct zone_rctlvaltab
*rctlval
;
2090 const char *name
= rctltab
.zone_rctl_name
;
2092 if (!zonecfg_is_rctl(name
)) {
2093 zerror(gettext("WARNING: Ignoring unrecognized rctl "
2095 zonecfg_free_rctl_value_list(rctltab
.zone_rctl_valptr
);
2096 rctltab
.zone_rctl_valptr
= NULL
;
2100 for (rctlval
= rctltab
.zone_rctl_valptr
; rctlval
!= NULL
;
2101 rctlval
= rctlval
->zone_rctlval_next
) {
2102 if (zonecfg_construct_rctlblk(rctlval
, rctlblk
)
2104 zerror(gettext("invalid rctl value: "
2105 "(priv=%s,limit=%s,action%s)"),
2106 rctlval
->zone_rctlval_priv
,
2107 rctlval
->zone_rctlval_limit
,
2108 rctlval
->zone_rctlval_action
);
2111 if (!zonecfg_valid_rctl(name
, rctlblk
)) {
2112 zerror(gettext("(priv=%s,limit=%s,action=%s) "
2113 "is not a valid value for rctl '%s'"),
2114 rctlval
->zone_rctlval_priv
,
2115 rctlval
->zone_rctlval_limit
,
2116 rctlval
->zone_rctlval_action
,
2121 zonecfg_free_rctl_value_list(rctltab
.zone_rctl_valptr
);
2123 rctltab
.zone_rctl_valptr
= NULL
;
2126 zonecfg_free_rctl_value_list(rctltab
.zone_rctl_valptr
);
2127 (void) zonecfg_endrctlent(handle
);
2133 verify_pool(zone_dochandle_t handle
)
2135 char poolname
[MAXPATHLEN
];
2136 pool_conf_t
*poolconf
;
2142 * This ends up being very similar to the check done in zoneadmd.
2144 error
= zonecfg_get_pool(handle
, poolname
, sizeof (poolname
));
2145 if (error
== Z_NO_ENTRY
|| (error
== Z_OK
&& strlen(poolname
) == 0)) {
2147 * No pool specified.
2151 if (error
!= Z_OK
) {
2152 zperror(gettext("Unable to retrieve pool name from "
2153 "configuration"), B_TRUE
);
2157 * Don't do anything if pools aren't enabled.
2159 if (pool_get_status(&status
) != PO_SUCCESS
|| status
!= POOL_ENABLED
) {
2160 zerror(gettext("WARNING: pools facility not active; "
2161 "zone will not be bound to pool '%s'."), poolname
);
2165 * Try to provide a sane error message if the requested pool doesn't
2166 * exist. It isn't clear that pools-related failures should
2167 * necessarily translate to a failure to verify the zone configuration,
2168 * hence they are not considered errors.
2170 if ((poolconf
= pool_conf_alloc()) == NULL
) {
2171 zerror(gettext("WARNING: pool_conf_alloc failed; "
2172 "using default pool"));
2175 if (pool_conf_open(poolconf
, pool_dynamic_location(), PO_RDONLY
) !=
2177 zerror(gettext("WARNING: pool_conf_open failed; "
2178 "using default pool"));
2179 pool_conf_free(poolconf
);
2182 pool
= pool_get_pool(poolconf
, poolname
);
2183 (void) pool_conf_close(poolconf
);
2184 pool_conf_free(poolconf
);
2186 zerror(gettext("WARNING: pool '%s' not found. "
2187 "using default pool"), poolname
);
2194 * Verify that the special device/file system exists and is valid.
2197 verify_fs_special(struct zone_fstab
*fstab
)
2202 * This validation is really intended for standard zone administration.
2203 * If we are in a mini-root or some other upgrade situation where
2204 * we are using the scratch zone, just by-pass this.
2206 if (zonecfg_in_alt_root())
2209 if (strcmp(fstab
->zone_fs_type
, MNTTYPE_ZFS
) == 0)
2210 return (verify_fs_zfs(fstab
));
2212 if (stat64(fstab
->zone_fs_special
, &st
) != 0) {
2213 (void) fprintf(stderr
, gettext("could not verify fs "
2214 "%s: could not access %s: %s\n"), fstab
->zone_fs_dir
,
2215 fstab
->zone_fs_special
, strerror(errno
));
2219 if (strcmp(st
.st_fstype
, MNTTYPE_NFS
) == 0) {
2222 * fs and NFS are literals that should
2223 * not be translated.
2225 (void) fprintf(stderr
, gettext("cannot verify "
2226 "fs %s: NFS mounted file system.\n"
2227 "\tA local file system must be used.\n"),
2228 fstab
->zone_fs_special
);
2236 isregfile(const char *path
)
2240 if (stat64(path
, &st
) == -1)
2243 return (S_ISREG(st
.st_mode
));
2247 verify_filesystems(zone_dochandle_t handle
)
2249 int return_code
= Z_OK
;
2250 struct zone_fstab fstab
;
2251 char cmdbuf
[MAXPATHLEN
];
2255 * Since the actual mount point is not known until the dependent mounts
2256 * are performed, we don't attempt any path validation here: that will
2257 * happen later when zoneadmd actually does the mounts.
2259 if (zonecfg_setfsent(handle
) != Z_OK
) {
2260 (void) fprintf(stderr
, gettext("could not verify file systems: "
2261 "unable to enumerate mounts\n"));
2264 while (zonecfg_getfsent(handle
, &fstab
) == Z_OK
) {
2265 if (!zonecfg_valid_fs_type(fstab
.zone_fs_type
)) {
2266 (void) fprintf(stderr
, gettext("cannot verify fs %s: "
2267 "type %s is not allowed.\n"), fstab
.zone_fs_dir
,
2268 fstab
.zone_fs_type
);
2269 return_code
= Z_ERR
;
2273 * Verify /usr/lib/fs/<fstype>/mount exists.
2275 if (snprintf(cmdbuf
, sizeof (cmdbuf
), "/usr/lib/fs/%s/mount",
2276 fstab
.zone_fs_type
) > sizeof (cmdbuf
)) {
2277 (void) fprintf(stderr
, gettext("cannot verify fs %s: "
2278 "type %s is too long.\n"), fstab
.zone_fs_dir
,
2279 fstab
.zone_fs_type
);
2280 return_code
= Z_ERR
;
2283 if (stat(cmdbuf
, &st
) != 0) {
2284 (void) fprintf(stderr
, gettext("could not verify fs "
2285 "%s: could not access %s: %s\n"), fstab
.zone_fs_dir
,
2286 cmdbuf
, strerror(errno
));
2287 return_code
= Z_ERR
;
2290 if (!S_ISREG(st
.st_mode
)) {
2291 (void) fprintf(stderr
, gettext("could not verify fs "
2292 "%s: %s is not a regular file\n"),
2293 fstab
.zone_fs_dir
, cmdbuf
);
2294 return_code
= Z_ERR
;
2298 * If zone_fs_raw is set, verify that there's an fsck
2299 * binary for it. If zone_fs_raw is not set, and it's
2300 * not a regular file (lofi mount), and there's an fsck
2301 * binary for it, complain.
2303 if (snprintf(cmdbuf
, sizeof (cmdbuf
), "/usr/lib/fs/%s/fsck",
2304 fstab
.zone_fs_type
) > sizeof (cmdbuf
)) {
2305 (void) fprintf(stderr
, gettext("cannot verify fs %s: "
2306 "type %s is too long.\n"), fstab
.zone_fs_dir
,
2307 fstab
.zone_fs_type
);
2308 return_code
= Z_ERR
;
2311 if (fstab
.zone_fs_raw
[0] != '\0' &&
2312 (stat(cmdbuf
, &st
) != 0 || !S_ISREG(st
.st_mode
))) {
2313 (void) fprintf(stderr
, gettext("cannot verify fs %s: "
2314 "'raw' device specified but "
2315 "no fsck executable exists for %s\n"),
2316 fstab
.zone_fs_dir
, fstab
.zone_fs_type
);
2317 return_code
= Z_ERR
;
2319 } else if (fstab
.zone_fs_raw
[0] == '\0' &&
2320 stat(cmdbuf
, &st
) == 0 &&
2321 isregfile(fstab
.zone_fs_special
) != 1) {
2322 (void) fprintf(stderr
, gettext("could not verify fs "
2323 "%s: must specify 'raw' device for %s "
2325 fstab
.zone_fs_dir
, fstab
.zone_fs_type
);
2326 return_code
= Z_ERR
;
2330 /* Verify fs_special. */
2331 if ((return_code
= verify_fs_special(&fstab
)) != Z_OK
)
2334 /* Verify fs_raw. */
2335 if (fstab
.zone_fs_raw
[0] != '\0' &&
2336 stat(fstab
.zone_fs_raw
, &st
) != 0) {
2339 * fs is a literal that should not be translated.
2341 (void) fprintf(stderr
, gettext("could not verify fs "
2342 "%s: could not access %s: %s\n"), fstab
.zone_fs_dir
,
2343 fstab
.zone_fs_raw
, strerror(errno
));
2344 return_code
= Z_ERR
;
2348 zonecfg_free_fs_option_list(fstab
.zone_fs_options
);
2350 (void) zonecfg_endfsent(handle
);
2352 return (return_code
);
2356 verify_limitpriv(zone_dochandle_t handle
)
2358 char *privname
= NULL
;
2362 if ((privs
= priv_allocset()) == NULL
) {
2363 zperror(gettext("failed to allocate privilege set"), B_FALSE
);
2366 err
= zonecfg_get_privset(handle
, privs
, &privname
);
2370 case Z_PRIV_PROHIBITED
:
2371 (void) fprintf(stderr
, gettext("privilege \"%s\" is not "
2372 "permitted within the zone's privilege set\n"), privname
);
2374 case Z_PRIV_REQUIRED
:
2375 (void) fprintf(stderr
, gettext("required privilege \"%s\" is "
2376 "missing from the zone's privilege set\n"), privname
);
2378 case Z_PRIV_UNKNOWN
:
2379 (void) fprintf(stderr
, gettext("unknown privilege \"%s\" "
2380 "specified in the zone's privilege set\n"), privname
);
2384 gettext("failed to determine the zone's privilege set"),
2389 priv_freeset(privs
);
2394 free_local_netifs(int if_cnt
, struct net_if
**if_list
)
2398 for (i
= 0; i
< if_cnt
; i
++) {
2399 free(if_list
[i
]->name
);
2406 * Get a list of the network interfaces, along with their address families,
2407 * that are plumbed in the global zone. See if_tcp(7p) for a description
2408 * of the ioctls used here.
2411 get_local_netifs(int *if_cnt
, struct net_if
***if_list
)
2418 struct lifnum if_num
;
2419 struct lifconf if_conf
;
2420 struct lifreq
*if_reqp
;
2422 struct net_if
**local_ifs
= NULL
;
2427 if ((s
= socket(SOCKET_AF(AF_INET
), SOCK_DGRAM
, 0)) < 0)
2431 * Come back here in the unlikely event that the number of interfaces
2432 * increases between the time we get the count and the time we do the
2433 * SIOCGLIFCONF ioctl.
2436 /* Get the number of interfaces. */
2437 if_num
.lifn_family
= AF_UNSPEC
;
2438 if_num
.lifn_flags
= LIFC_NOXMIT
;
2439 if (ioctl(s
, SIOCGLIFNUM
, &if_num
) < 0) {
2444 /* Get the interface configuration list. */
2445 space_needed
= if_num
.lifn_count
* sizeof (struct lifreq
);
2446 if ((if_buf
= malloc(space_needed
)) == NULL
) {
2450 if_conf
.lifc_family
= AF_UNSPEC
;
2451 if_conf
.lifc_flags
= LIFC_NOXMIT
;
2452 if_conf
.lifc_len
= space_needed
;
2453 if_conf
.lifc_buf
= if_buf
;
2454 if (ioctl(s
, SIOCGLIFCONF
, &if_conf
) < 0) {
2457 * SIOCGLIFCONF returns EINVAL if the buffer we passed in is
2458 * too small. In this case go back and get the new if cnt.
2460 if (errno
== EINVAL
)
2468 /* Get the name and address family for each interface. */
2469 if_reqp
= if_conf
.lifc_req
;
2470 for (i
= 0; i
< (if_conf
.lifc_len
/ sizeof (struct lifreq
)); i
++) {
2474 if (strcmp(LOOPBACK_IF
, if_reqp
->lifr_name
) == 0) {
2479 if ((s
= socket(SOCKET_AF(if_reqp
->lifr_addr
.ss_family
),
2480 SOCK_DGRAM
, 0)) == -1) {
2485 (void) strncpy(req
.lifr_name
, if_reqp
->lifr_name
,
2486 sizeof (req
.lifr_name
));
2487 if (ioctl(s
, SIOCGLIFADDR
, &req
) < 0) {
2493 if ((p
= (struct net_if
**)realloc(local_ifs
,
2494 sizeof (struct net_if
*) * (cnt
+ 1))) == NULL
) {
2500 if ((local_ifs
[cnt
] = malloc(sizeof (struct net_if
))) == NULL
) {
2505 if ((local_ifs
[cnt
]->name
= strdup(if_reqp
->lifr_name
))
2507 free(local_ifs
[cnt
]);
2511 local_ifs
[cnt
]->af
= req
.lifr_addr
.ss_family
;
2521 free_local_netifs(cnt
, local_ifs
);
2524 *if_list
= local_ifs
;
2544 * Cross check the network interface name and address family with the
2545 * interfaces that are set up in the global zone so that we can print the
2546 * appropriate error message.
2549 print_net_err(char *phys
, char *addr
, int af
, char *msg
)
2552 int local_if_cnt
= 0;
2553 struct net_if
**local_ifs
= NULL
;
2554 boolean_t found_if
= B_FALSE
;
2555 boolean_t found_af
= B_FALSE
;
2557 if (get_local_netifs(&local_if_cnt
, &local_ifs
) != Z_OK
) {
2558 (void) fprintf(stderr
,
2559 gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
2560 "net", "address", addr
, "physical", phys
, msg
);
2564 for (i
= 0; i
< local_if_cnt
; i
++) {
2565 if (strcmp(phys
, local_ifs
[i
]->name
) == 0) {
2567 if (af
== local_ifs
[i
]->af
) {
2574 free_local_netifs(local_if_cnt
, local_ifs
);
2577 (void) fprintf(stderr
,
2578 gettext("could not verify %s %s=%s\n\t"
2579 "network interface %s is not plumbed in the global zone\n"),
2580 "net", "physical", phys
, phys
);
2585 * Print this error if we were unable to find the address family
2586 * for this interface. If the af variable is not initialized to
2587 * to something meaningful by the caller (not AF_UNSPEC) then we
2588 * also skip this message since it wouldn't be informative.
2590 if (!found_af
&& af
!= AF_UNSPEC
) {
2591 (void) fprintf(stderr
,
2592 gettext("could not verify %s %s=%s %s=%s\n\tthe %s address "
2593 "family is not configured on this network interface in "
2594 "the\n\tglobal zone\n"),
2595 "net", "address", addr
, "physical", phys
, af2str(af
));
2599 (void) fprintf(stderr
,
2600 gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
2601 "net", "address", addr
, "physical", phys
, msg
);
2605 verify_handle(int cmd_num
, zone_dochandle_t handle
, char *argv
[])
2607 struct zone_nwiftab nwiftab
;
2608 int return_code
= Z_OK
;
2610 boolean_t in_alt_root
;
2611 zone_iptype_t iptype
;
2613 dladm_status_t status
;
2614 datalink_id_t linkid
;
2615 char errmsg
[DLADM_STRSIZE
];
2617 in_alt_root
= zonecfg_in_alt_root();
2621 if ((err
= zonecfg_get_iptype(handle
, &iptype
)) != Z_OK
) {
2623 zperror(cmd_to_str(cmd_num
), B_TRUE
);
2624 zonecfg_fini_handle(handle
);
2627 if ((err
= zonecfg_setnwifent(handle
)) != Z_OK
) {
2629 zperror(cmd_to_str(cmd_num
), B_TRUE
);
2630 zonecfg_fini_handle(handle
);
2633 while (zonecfg_getnwifent(handle
, &nwiftab
) == Z_OK
) {
2635 sa_family_t af
= AF_UNSPEC
;
2636 char dl_owner_zname
[ZONENAME_MAX
];
2637 zoneid_t dl_owner_zid
;
2638 zoneid_t target_zid
;
2641 /* skip any loopback interfaces */
2642 if (strcmp(nwiftab
.zone_nwif_physical
, "lo0") == 0)
2646 if ((res
= zonecfg_valid_net_address(
2647 nwiftab
.zone_nwif_address
, &lifr
)) != Z_OK
) {
2648 print_net_err(nwiftab
.zone_nwif_physical
,
2649 nwiftab
.zone_nwif_address
, af
,
2650 zonecfg_strerror(res
));
2651 return_code
= Z_ERR
;
2654 af
= lifr
.lifr_addr
.ss_family
;
2655 if (!zonecfg_ifname_exists(af
,
2656 nwiftab
.zone_nwif_physical
)) {
2658 * The interface failed to come up. We continue
2659 * on anyway for the sake of consistency: a
2660 * zone is not shut down if the interface fails
2661 * any time after boot, nor does the global zone
2662 * fail to boot if an interface fails.
2664 (void) fprintf(stderr
,
2665 gettext("WARNING: skipping network "
2666 "interface '%s' which may not be "
2667 "present/plumbed in the global "
2669 nwiftab
.zone_nwif_physical
);
2673 /* Warning if it exists for either IPv4 or IPv6 */
2675 if (zonecfg_ifname_exists(AF_INET
,
2676 nwiftab
.zone_nwif_physical
) ||
2677 zonecfg_ifname_exists(AF_INET6
,
2678 nwiftab
.zone_nwif_physical
)) {
2679 (void) fprintf(stderr
,
2680 gettext("WARNING: skipping network "
2681 "interface '%s' which is used in the "
2683 nwiftab
.zone_nwif_physical
);
2688 * Verify that the datalink exists and that it isn't
2689 * already assigned to a zone.
2691 if ((status
= dladm_open(&dh
)) == DLADM_STATUS_OK
) {
2692 status
= dladm_name2info(dh
,
2693 nwiftab
.zone_nwif_physical
, &linkid
, NULL
,
2697 if (status
!= DLADM_STATUS_OK
) {
2698 (void) fprintf(stderr
,
2699 gettext("WARNING: skipping network "
2700 "interface '%s': %s\n"),
2701 nwiftab
.zone_nwif_physical
,
2702 dladm_status2str(status
, errmsg
));
2705 dl_owner_zid
= ALL_ZONES
;
2706 if (zone_check_datalink(&dl_owner_zid
, linkid
) != 0)
2710 * If the zone being verified is
2711 * running and owns the interface
2713 target_zid
= getzoneidbyname(target_zone
);
2714 if (target_zid
== dl_owner_zid
)
2717 /* Zone id match failed, use name to check */
2718 if (getzonenamebyid(dl_owner_zid
, dl_owner_zname
,
2719 ZONENAME_MAX
) < 0) {
2720 /* No name, show ID instead */
2721 (void) snprintf(dl_owner_zname
, ZONENAME_MAX
,
2722 "<%d>", dl_owner_zid
);
2723 } else if (strcmp(dl_owner_zname
, target_zone
) == 0)
2727 * Note here we only report a warning that
2728 * the interface is already in use by another
2729 * running zone, and the verify process just
2730 * goes on, if the interface is still in use
2731 * when this zone really boots up, zoneadmd
2732 * will find it. If the name of the zone which
2733 * owns this interface cannot be determined,
2734 * then it is not possible to determine if there
2735 * is a conflict so just report it as a warning.
2737 (void) fprintf(stderr
,
2738 gettext("WARNING: skipping network interface "
2739 "'%s' which is used by the non-global zone "
2740 "'%s'.\n"), nwiftab
.zone_nwif_physical
,
2745 (void) zonecfg_endnwifent(handle
);
2748 /* verify that lofs has not been excluded from the kernel */
2749 if (!(cmd_num
== CMD_DETACH
|| cmd_num
== CMD_ATTACH
||
2750 cmd_num
== CMD_MOVE
|| cmd_num
== CMD_CLONE
) &&
2751 modctl(MODLOAD
, 1, "fs/lofs", NULL
) != 0) {
2753 (void) fprintf(stderr
, gettext("could not verify "
2754 "lofs(7FS): possibly excluded in /etc/system\n"));
2756 (void) fprintf(stderr
, gettext("could not verify "
2757 "lofs(7FS): %s\n"), strerror(errno
));
2758 return_code
= Z_ERR
;
2761 if (verify_filesystems(handle
) != Z_OK
)
2762 return_code
= Z_ERR
;
2763 if (!in_alt_root
&& verify_rctls(handle
) != Z_OK
)
2764 return_code
= Z_ERR
;
2765 if (!in_alt_root
&& verify_pool(handle
) != Z_OK
)
2766 return_code
= Z_ERR
;
2767 if (!in_alt_root
&& verify_brand(handle
, cmd_num
, argv
) != Z_OK
)
2768 return_code
= Z_ERR
;
2769 if (!in_alt_root
&& verify_datasets(handle
) != Z_OK
)
2770 return_code
= Z_ERR
;
2773 * As the "mount" command is used for patching/upgrading of zones
2774 * or other maintenance processes, the zone's privilege set is not
2775 * checked in this case. Instead, the default, safe set of
2776 * privileges will be used when this zone is created in the
2779 if (!in_alt_root
&& cmd_num
!= CMD_MOUNT
&&
2780 verify_limitpriv(handle
) != Z_OK
)
2781 return_code
= Z_ERR
;
2783 return (return_code
);
2787 verify_details(int cmd_num
, char *argv
[])
2789 zone_dochandle_t handle
;
2790 char zonepath
[MAXPATHLEN
], checkpath
[MAXPATHLEN
];
2791 int return_code
= Z_OK
;
2794 if ((handle
= zonecfg_init_handle()) == NULL
) {
2795 zperror(cmd_to_str(cmd_num
), B_TRUE
);
2798 if ((err
= zonecfg_get_handle(target_zone
, handle
)) != Z_OK
) {
2800 zperror(cmd_to_str(cmd_num
), B_TRUE
);
2801 zonecfg_fini_handle(handle
);
2804 if ((err
= zonecfg_get_zonepath(handle
, zonepath
, sizeof (zonepath
))) !=
2807 zperror(cmd_to_str(cmd_num
), B_TRUE
);
2808 zonecfg_fini_handle(handle
);
2812 * zonecfg_get_zonepath() gets its data from the XML repository.
2813 * Verify this against the index file, which is checked first by
2814 * zone_get_zonepath(). If they don't match, bail out.
2816 if ((err
= zone_get_zonepath(target_zone
, checkpath
,
2817 sizeof (checkpath
))) != Z_OK
) {
2819 zperror2(target_zone
, gettext("could not get zone path"));
2820 zonecfg_fini_handle(handle
);
2823 if (strcmp(zonepath
, checkpath
) != 0) {
2826 * XML and zonepath are literals that should not be translated.
2828 (void) fprintf(stderr
, gettext("The XML repository has "
2829 "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
2830 "These must match, so fix the incorrect entry.\n"),
2831 zonepath
, checkpath
);
2832 zonecfg_fini_handle(handle
);
2835 if (cmd_num
!= CMD_ATTACH
&&
2836 validate_zonepath(zonepath
, cmd_num
) != Z_OK
) {
2837 (void) fprintf(stderr
, gettext("could not verify zonepath %s "
2838 "because of the above errors.\n"), zonepath
);
2839 return_code
= Z_ERR
;
2842 if (verify_handle(cmd_num
, handle
, argv
) != Z_OK
)
2843 return_code
= Z_ERR
;
2845 zonecfg_fini_handle(handle
);
2846 if (return_code
== Z_ERR
)
2847 (void) fprintf(stderr
,
2848 gettext("%s: zone %s failed to verify\n"),
2849 execname
, target_zone
);
2850 return (return_code
);
2854 verify_func(int argc
, char *argv
[])
2859 if ((arg
= getopt(argc
, argv
, "?")) != EOF
) {
2862 sub_usage(SHELP_VERIFY
, CMD_VERIFY
);
2863 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
2865 sub_usage(SHELP_VERIFY
, CMD_VERIFY
);
2869 if (argc
> optind
) {
2870 sub_usage(SHELP_VERIFY
, CMD_VERIFY
);
2873 if (sanity_check(target_zone
, CMD_VERIFY
, B_FALSE
, B_FALSE
, B_FALSE
)
2876 return (verify_details(CMD_VERIFY
, argv
));
2880 addoptions(char *buf
, char *argv
[], size_t len
)
2887 while (argv
[i
] != NULL
) {
2888 if (strlcat(buf
, " ", len
) >= len
||
2889 strlcat(buf
, argv
[i
++], len
) >= len
) {
2890 zerror("Command line too long");
2899 addopt(char *buf
, int opt
, char *optarg
, size_t bufsize
)
2904 (void) sprintf(optstring
, " -%c", opt
);
2906 (void) strcpy(optstring
, " ");
2908 if ((strlcat(buf
, optstring
, bufsize
) > bufsize
))
2911 if ((optarg
!= NULL
) && (strlcat(buf
, optarg
, bufsize
) > bufsize
))
2919 install_func(int argc
, char *argv
[])
2921 char cmdbuf
[MAXPATHLEN
];
2922 char postcmdbuf
[MAXPATHLEN
];
2924 int arg
, err
, subproc_err
;
2925 char zonepath
[MAXPATHLEN
];
2926 brand_handle_t bh
= NULL
;
2928 boolean_t do_postinstall
= B_FALSE
;
2929 boolean_t brand_help
= B_FALSE
;
2932 if (target_zone
== NULL
) {
2933 sub_usage(SHELP_INSTALL
, CMD_INSTALL
);
2937 if (zonecfg_in_alt_root()) {
2938 zerror(gettext("cannot install zone in alternate root"));
2942 if ((err
= zone_get_zonepath(target_zone
, zonepath
,
2943 sizeof (zonepath
))) != Z_OK
) {
2945 zperror2(target_zone
, gettext("could not get zone path"));
2949 /* Fetch the install command from the brand configuration. */
2950 if ((bh
= brand_open(target_brand
)) == NULL
) {
2951 zerror(gettext("missing or invalid brand"));
2955 if (get_hook(bh
, cmdbuf
, sizeof (cmdbuf
), brand_get_install
,
2956 target_zone
, zonepath
) != Z_OK
) {
2957 zerror("invalid brand configuration: missing install resource");
2962 if (get_hook(bh
, postcmdbuf
, sizeof (postcmdbuf
), brand_get_postinstall
,
2963 target_zone
, zonepath
) != Z_OK
) {
2964 zerror("invalid brand configuration: missing postinstall "
2970 if (postcmdbuf
[0] != '\0')
2971 do_postinstall
= B_TRUE
;
2973 (void) strcpy(opts
, "?x:");
2975 * Fetch the list of recognized command-line options from
2976 * the brand configuration file.
2978 if (brand_get_installopts(bh
, opts
+ strlen(opts
),
2979 sizeof (opts
) - strlen(opts
)) != 0) {
2980 zerror("invalid brand configuration: missing "
2981 "install options resource");
2988 if (cmdbuf
[0] == '\0') {
2989 zerror("Missing brand install command");
2993 /* Check the argv string for args we handle internally */
2996 while ((arg
= getopt(argc
, argv
, opts
)) != EOF
) {
2999 if (optopt
== '?') {
3000 sub_usage(SHELP_INSTALL
, CMD_INSTALL
);
3001 brand_help
= B_TRUE
;
3003 /* Ignore unknown options - may be brand specific. */
3006 /* Ignore unknown options - may be brand specific. */
3011 * Append the option to the command line passed to the
3012 * brand-specific install and postinstall routines.
3014 if (addopt(cmdbuf
, optopt
, optarg
, sizeof (cmdbuf
)) != Z_OK
) {
3015 zerror("Install command line too long");
3018 if (addopt(postcmdbuf
, optopt
, optarg
, sizeof (postcmdbuf
))
3020 zerror("Post-Install command line too long");
3025 for (; optind
< argc
; optind
++) {
3026 if (addopt(cmdbuf
, 0, argv
[optind
], sizeof (cmdbuf
)) != Z_OK
) {
3027 zerror("Install command line too long");
3031 if (addopt(postcmdbuf
, 0, argv
[optind
], sizeof (postcmdbuf
))
3033 zerror("Post-Install command line too long");
3039 if (sanity_check(target_zone
, CMD_INSTALL
, B_FALSE
, B_TRUE
,
3042 if (verify_details(CMD_INSTALL
, argv
) != Z_OK
)
3045 if (zonecfg_grab_lock_file(target_zone
, &lockfd
) != Z_OK
) {
3046 zerror(gettext("another %s may have an operation in "
3047 "progress."), "zoneadm");
3050 err
= zone_set_state(target_zone
, ZONE_STATE_INCOMPLETE
);
3053 zperror2(target_zone
, gettext("could not set state"));
3057 create_zfs_zonepath(zonepath
);
3060 status
= do_subproc(cmdbuf
);
3062 subproc_status(gettext("brand-specific installation"), status
,
3063 B_FALSE
)) != ZONE_SUBPROC_OK
) {
3064 if (subproc_err
== ZONE_SUBPROC_USAGE
&& !brand_help
) {
3065 sub_usage(SHELP_INSTALL
, CMD_INSTALL
);
3066 zonecfg_release_lock_file(target_zone
, lockfd
);
3069 errno
= subproc_err
;
3077 if ((err
= zone_set_state(target_zone
, ZONE_STATE_INSTALLED
)) != Z_OK
) {
3079 zperror2(target_zone
, gettext("could not set state"));
3083 if (do_postinstall
) {
3084 status
= do_subproc(postcmdbuf
);
3087 subproc_status(gettext("brand-specific post-install"),
3088 status
, B_FALSE
)) != ZONE_SUBPROC_OK
) {
3089 errno
= subproc_err
;
3091 (void) zone_set_state(target_zone
,
3092 ZONE_STATE_INCOMPLETE
);
3098 * If the install script exited with ZONE_SUBPROC_NOTCOMPLETE, try to
3099 * clean up the zone and leave the zone in the CONFIGURED state so that
3100 * another install can be attempted without requiring an uninstall
3103 if (subproc_err
== ZONE_SUBPROC_NOTCOMPLETE
) {
3106 if ((temp_err
= cleanup_zonepath(zonepath
, B_FALSE
)) != Z_OK
) {
3107 errno
= err
= temp_err
;
3108 zperror2(target_zone
,
3109 gettext("cleaning up zonepath failed"));
3110 } else if ((temp_err
= zone_set_state(target_zone
,
3111 ZONE_STATE_CONFIGURED
)) != Z_OK
) {
3112 errno
= err
= temp_err
;
3113 zperror2(target_zone
, gettext("could not set state"));
3118 zonecfg_release_lock_file(target_zone
, lockfd
);
3119 return ((err
== Z_OK
) ? Z_OK
: Z_ERR
);
3123 warn_dev_match(zone_dochandle_t s_handle
, char *source_zone
,
3124 zone_dochandle_t t_handle
, char *target_zone
)
3127 struct zone_devtab s_devtab
;
3128 struct zone_devtab t_devtab
;
3130 if ((err
= zonecfg_setdevent(t_handle
)) != Z_OK
) {
3132 zperror2(target_zone
, gettext("could not enumerate devices"));
3136 while (zonecfg_getdevent(t_handle
, &t_devtab
) == Z_OK
) {
3137 if ((err
= zonecfg_setdevent(s_handle
)) != Z_OK
) {
3139 zperror2(source_zone
,
3140 gettext("could not enumerate devices"));
3141 (void) zonecfg_enddevent(t_handle
);
3145 while (zonecfg_getdevent(s_handle
, &s_devtab
) == Z_OK
) {
3147 * Use fnmatch to catch the case where wildcards
3148 * were used in one zone and the other has an
3149 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
3150 * /dev/\*dsk/c0t0d0s6).
3152 if (fnmatch(t_devtab
.zone_dev_match
,
3153 s_devtab
.zone_dev_match
, FNM_PATHNAME
) == 0 ||
3154 fnmatch(s_devtab
.zone_dev_match
,
3155 t_devtab
.zone_dev_match
, FNM_PATHNAME
) == 0) {
3156 (void) fprintf(stderr
,
3157 gettext("WARNING: device '%s' "
3158 "is configured in both zones.\n"),
3159 t_devtab
.zone_dev_match
);
3163 (void) zonecfg_enddevent(s_handle
);
3166 (void) zonecfg_enddevent(t_handle
);
3170 * Check if the specified mount option (opt) is contained within the
3174 opt_match(char *opt
, char *options
)
3179 if ((p
= strtok_r(options
, ",", &lastp
)) != NULL
) {
3180 if (strcmp(p
, opt
) == 0)
3182 while ((p
= strtok_r(NULL
, ",", &lastp
)) != NULL
) {
3183 if (strcmp(p
, opt
) == 0)
3191 #define RW_LOFS "WARNING: read-write lofs file system on '%s' is configured " \
3195 print_fs_warnings(struct zone_fstab
*s_fstab
, struct zone_fstab
*t_fstab
)
3198 * It is ok to have shared lofs mounted fs but we want to warn if
3199 * either is rw since this will effect the other zone.
3201 if (strcmp(t_fstab
->zone_fs_type
, "lofs") == 0) {
3204 /* The default is rw so no options means rw */
3205 if (t_fstab
->zone_fs_options
== NULL
||
3206 s_fstab
->zone_fs_options
== NULL
) {
3207 (void) fprintf(stderr
, gettext(RW_LOFS
),
3208 t_fstab
->zone_fs_special
);
3212 for (optp
= s_fstab
->zone_fs_options
; optp
!= NULL
;
3213 optp
= optp
->zone_fsopt_next
) {
3214 if (opt_match("rw", optp
->zone_fsopt_opt
)) {
3215 (void) fprintf(stderr
, gettext(RW_LOFS
),
3216 s_fstab
->zone_fs_special
);
3221 for (optp
= t_fstab
->zone_fs_options
; optp
!= NULL
;
3222 optp
= optp
->zone_fsopt_next
) {
3223 if (opt_match("rw", optp
->zone_fsopt_opt
)) {
3224 (void) fprintf(stderr
, gettext(RW_LOFS
),
3225 t_fstab
->zone_fs_special
);
3235 * The first variable is the file system type and the second is
3236 * the file system special device. For example,
3237 * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ...
3239 (void) fprintf(stderr
, gettext("WARNING: %s file system on '%s' "
3240 "is configured in both zones.\n"), t_fstab
->zone_fs_type
,
3241 t_fstab
->zone_fs_special
);
3245 warn_fs_match(zone_dochandle_t s_handle
, char *source_zone
,
3246 zone_dochandle_t t_handle
, char *target_zone
)
3249 struct zone_fstab s_fstab
;
3250 struct zone_fstab t_fstab
;
3252 if ((err
= zonecfg_setfsent(t_handle
)) != Z_OK
) {
3254 zperror2(target_zone
,
3255 gettext("could not enumerate file systems"));
3259 while (zonecfg_getfsent(t_handle
, &t_fstab
) == Z_OK
) {
3260 if ((err
= zonecfg_setfsent(s_handle
)) != Z_OK
) {
3262 zperror2(source_zone
,
3263 gettext("could not enumerate file systems"));
3264 (void) zonecfg_endfsent(t_handle
);
3268 while (zonecfg_getfsent(s_handle
, &s_fstab
) == Z_OK
) {
3269 if (strcmp(t_fstab
.zone_fs_special
,
3270 s_fstab
.zone_fs_special
) == 0) {
3271 print_fs_warnings(&s_fstab
, &t_fstab
);
3275 (void) zonecfg_endfsent(s_handle
);
3278 (void) zonecfg_endfsent(t_handle
);
3282 * We don't catch the case where you used the same IP address but
3283 * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128.
3284 * However, we're not going to worry about that but we will check for
3285 * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
3286 * and handle that case as a match.
3289 warn_ip_match(zone_dochandle_t s_handle
, char *source_zone
,
3290 zone_dochandle_t t_handle
, char *target_zone
)
3293 struct zone_nwiftab s_nwiftab
;
3294 struct zone_nwiftab t_nwiftab
;
3296 if ((err
= zonecfg_setnwifent(t_handle
)) != Z_OK
) {
3298 zperror2(target_zone
,
3299 gettext("could not enumerate network interfaces"));
3303 while (zonecfg_getnwifent(t_handle
, &t_nwiftab
) == Z_OK
) {
3306 /* remove an (optional) netmask from the address */
3307 if ((p
= strchr(t_nwiftab
.zone_nwif_address
, '/')) != NULL
)
3310 if ((err
= zonecfg_setnwifent(s_handle
)) != Z_OK
) {
3312 zperror2(source_zone
,
3313 gettext("could not enumerate network interfaces"));
3314 (void) zonecfg_endnwifent(t_handle
);
3318 while (zonecfg_getnwifent(s_handle
, &s_nwiftab
) == Z_OK
) {
3319 /* remove an (optional) netmask from the address */
3320 if ((p
= strchr(s_nwiftab
.zone_nwif_address
, '/'))
3324 /* For exclusive-IP zones, address is not specified. */
3325 if (strlen(s_nwiftab
.zone_nwif_address
) == 0)
3328 if (strcmp(t_nwiftab
.zone_nwif_address
,
3329 s_nwiftab
.zone_nwif_address
) == 0) {
3330 (void) fprintf(stderr
,
3331 gettext("WARNING: network address '%s' "
3332 "is configured in both zones.\n"),
3333 t_nwiftab
.zone_nwif_address
);
3337 (void) zonecfg_endnwifent(s_handle
);
3340 (void) zonecfg_endnwifent(t_handle
);
3344 warn_dataset_match(zone_dochandle_t s_handle
, char *source
,
3345 zone_dochandle_t t_handle
, char *target
)
3348 struct zone_dstab s_dstab
;
3349 struct zone_dstab t_dstab
;
3351 if ((err
= zonecfg_setdsent(t_handle
)) != Z_OK
) {
3353 zperror2(target
, gettext("could not enumerate datasets"));
3357 while (zonecfg_getdsent(t_handle
, &t_dstab
) == Z_OK
) {
3358 if ((err
= zonecfg_setdsent(s_handle
)) != Z_OK
) {
3361 gettext("could not enumerate datasets"));
3362 (void) zonecfg_enddsent(t_handle
);
3366 while (zonecfg_getdsent(s_handle
, &s_dstab
) == Z_OK
) {
3367 if (strcmp(t_dstab
.zone_dataset_name
,
3368 s_dstab
.zone_dataset_name
) == 0) {
3369 target_zone
= source
;
3370 zerror(gettext("WARNING: dataset '%s' "
3371 "is configured in both zones.\n"),
3372 t_dstab
.zone_dataset_name
);
3376 (void) zonecfg_enddsent(s_handle
);
3379 (void) zonecfg_enddsent(t_handle
);
3383 * Check that the clone and its source have the same brand type.
3386 valid_brand_clone(char *source_zone
, char *target_zone
)
3389 char source_brand
[MAXNAMELEN
];
3391 if ((zone_get_brand(source_zone
, source_brand
,
3392 sizeof (source_brand
))) != Z_OK
) {
3393 (void) fprintf(stderr
, "%s: zone '%s': %s\n",
3394 execname
, source_zone
, gettext("missing or invalid brand"));
3398 if (strcmp(source_brand
, target_brand
) != 0) {
3399 (void) fprintf(stderr
,
3400 gettext("%s: Zones '%s' and '%s' have different brand "
3401 "types.\n"), execname
, source_zone
, target_zone
);
3405 if ((bh
= brand_open(target_brand
)) == NULL
) {
3406 zerror(gettext("missing or invalid brand"));
3414 validate_clone(char *source_zone
, char *target_zone
)
3417 zone_dochandle_t s_handle
;
3418 zone_dochandle_t t_handle
;
3420 if ((t_handle
= zonecfg_init_handle()) == NULL
) {
3421 zperror(cmd_to_str(CMD_CLONE
), B_TRUE
);
3424 if ((err
= zonecfg_get_handle(target_zone
, t_handle
)) != Z_OK
) {
3426 zperror(cmd_to_str(CMD_CLONE
), B_TRUE
);
3427 zonecfg_fini_handle(t_handle
);
3431 if ((s_handle
= zonecfg_init_handle()) == NULL
) {
3432 zperror(cmd_to_str(CMD_CLONE
), B_TRUE
);
3433 zonecfg_fini_handle(t_handle
);
3436 if ((err
= zonecfg_get_handle(source_zone
, s_handle
)) != Z_OK
) {
3438 zperror(cmd_to_str(CMD_CLONE
), B_TRUE
);
3442 /* verify new zone has same brand type */
3443 err
= valid_brand_clone(source_zone
, target_zone
);
3447 /* warn about imported fs's which are the same */
3448 warn_fs_match(s_handle
, source_zone
, t_handle
, target_zone
);
3450 /* warn about imported IP addresses which are the same */
3451 warn_ip_match(s_handle
, source_zone
, t_handle
, target_zone
);
3453 /* warn about imported devices which are the same */
3454 warn_dev_match(s_handle
, source_zone
, t_handle
, target_zone
);
3456 /* warn about imported datasets which are the same */
3457 warn_dataset_match(s_handle
, source_zone
, t_handle
, target_zone
);
3460 zonecfg_fini_handle(t_handle
);
3461 zonecfg_fini_handle(s_handle
);
3463 return ((err
== Z_OK
) ? Z_OK
: Z_ERR
);
3467 copy_zone(char *src
, char *dst
)
3469 boolean_t out_null
= B_FALSE
;
3472 char cmdbuf
[MAXPATHLEN
* 2 + 128];
3474 if ((outfile
= tempnam("/var/log", "zone")) == NULL
) {
3475 outfile
= "/dev/null";
3480 * Use find to get the list of files to copy. We need to skip
3481 * files of type "socket" since cpio can't handle those but that
3482 * should be ok since the app will recreate the socket when it runs.
3483 * We also need to filter out anything under the .zfs subdir. Since
3484 * find is running depth-first, we need the extra egrep to filter .zfs.
3486 (void) snprintf(cmdbuf
, sizeof (cmdbuf
),
3487 "cd %s && /usr/bin/find . -type s -prune -o -depth -print | "
3488 "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
3489 "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
3492 status
= do_subproc(cmdbuf
);
3494 if (subproc_status("copy", status
, B_TRUE
) != ZONE_SUBPROC_OK
) {
3496 (void) fprintf(stderr
, gettext("\nThe copy failed.\n"
3497 "More information can be found in %s\n"), outfile
);
3502 (void) unlink(outfile
);
3509 zfm_print(const struct mnttab
*p
, void *r
)
3511 zerror(" %s\n", p
->mnt_mountp
);
3516 clone_copy(char *source_zonepath
, char *zonepath
)
3520 /* Don't clone the zone if anything is still mounted there */
3521 if (zonecfg_find_mounts(source_zonepath
, NULL
, NULL
)) {
3522 zerror(gettext("These file systems are mounted on "
3523 "subdirectories of %s.\n"), source_zonepath
);
3524 (void) zonecfg_find_mounts(source_zonepath
, zfm_print
, NULL
);
3529 * Attempt to create a ZFS fs for the zonepath. As usual, we don't
3530 * care if this works or not since we always have the default behavior
3531 * of a simple directory for the zonepath.
3533 create_zfs_zonepath(zonepath
);
3535 (void) printf(gettext("Copying %s..."), source_zonepath
);
3536 (void) fflush(stdout
);
3538 err
= copy_zone(source_zonepath
, zonepath
);
3540 (void) printf("\n");
3546 clone_func(int argc
, char *argv
[])
3548 char *source_zone
= NULL
;
3551 char zonepath
[MAXPATHLEN
];
3552 char source_zonepath
[MAXPATHLEN
];
3555 char *method
= NULL
;
3556 char *snapshot
= NULL
;
3557 char cmdbuf
[MAXPATHLEN
];
3558 char postcmdbuf
[MAXPATHLEN
];
3559 char presnapbuf
[MAXPATHLEN
];
3560 char postsnapbuf
[MAXPATHLEN
];
3561 char validsnapbuf
[MAXPATHLEN
];
3562 brand_handle_t bh
= NULL
;
3564 boolean_t brand_help
= B_FALSE
;
3566 if (zonecfg_in_alt_root()) {
3567 zerror(gettext("cannot clone zone in alternate root"));
3571 /* Check the argv string for args we handle internally */
3574 while ((arg
= getopt(argc
, argv
, "?m:s:")) != EOF
) {
3577 if (optopt
== '?') {
3578 sub_usage(SHELP_CLONE
, CMD_CLONE
);
3579 brand_help
= B_TRUE
;
3581 /* Ignore unknown options - may be brand specific. */
3590 /* Ignore unknown options - may be brand specific. */
3595 if (argc
!= (optind
+ 1)) {
3596 sub_usage(SHELP_CLONE
, CMD_CLONE
);
3600 source_zone
= argv
[optind
];
3603 if (sanity_check(target_zone
, CMD_CLONE
, B_FALSE
, B_TRUE
,
3606 if (verify_details(CMD_CLONE
, argv
) != Z_OK
)
3610 * We also need to do some extra validation on the source zone.
3612 if (strcmp(source_zone
, GLOBAL_ZONENAME
) == 0) {
3613 zerror(gettext("%s operation is invalid for the "
3614 "global zone."), cmd_to_str(CMD_CLONE
));
3618 if (strncmp(source_zone
, "SUNW", 4) == 0) {
3619 zerror(gettext("%s operation is invalid for zones "
3620 "starting with SUNW."), cmd_to_str(CMD_CLONE
));
3624 if (auth_check(username
, source_zone
, SOURCE_ZONE
) == Z_ERR
) {
3625 zerror(gettext("%s operation is invalid because "
3626 "user is not authorized to read source zone."),
3627 cmd_to_str(CMD_CLONE
));
3631 zent
= lookup_running_zone(source_zone
);
3633 /* check whether the zone is ready or running */
3634 if ((err
= zone_get_state(zent
->zname
,
3635 &zent
->zstate_num
)) != Z_OK
) {
3637 zperror2(zent
->zname
, gettext("could not get "
3639 /* can't tell, so hedge */
3640 zent
->zstate_str
= "ready/running";
3643 zone_state_str(zent
->zstate_num
);
3645 zerror(gettext("%s operation is invalid for %s zones."),
3646 cmd_to_str(CMD_CLONE
), zent
->zstate_str
);
3650 if ((err
= zone_get_state(source_zone
, &state
)) != Z_OK
) {
3652 zperror2(source_zone
, gettext("could not get state"));
3655 if (state
!= ZONE_STATE_INSTALLED
) {
3656 (void) fprintf(stderr
,
3657 gettext("%s: zone %s is %s; %s is required.\n"),
3658 execname
, source_zone
, zone_state_str(state
),
3659 zone_state_str(ZONE_STATE_INSTALLED
));
3664 * The source zone checks out ok, continue with the clone.
3667 if (validate_clone(source_zone
, target_zone
) != Z_OK
)
3670 if (zonecfg_grab_lock_file(target_zone
, &lockfd
) != Z_OK
) {
3671 zerror(gettext("another %s may have an operation in "
3672 "progress."), "zoneadm");
3677 if ((err
= zone_get_zonepath(source_zone
, source_zonepath
,
3678 sizeof (source_zonepath
))) != Z_OK
) {
3680 zperror2(source_zone
, gettext("could not get zone path"));
3684 if ((err
= zone_get_zonepath(target_zone
, zonepath
, sizeof (zonepath
)))
3687 zperror2(target_zone
, gettext("could not get zone path"));
3692 * Fetch the clone and postclone hooks from the brand configuration.
3694 if ((bh
= brand_open(target_brand
)) == NULL
) {
3695 zerror(gettext("missing or invalid brand"));
3700 if (get_hook(bh
, cmdbuf
, sizeof (cmdbuf
), brand_get_clone
, target_zone
,
3701 zonepath
) != Z_OK
) {
3702 zerror("invalid brand configuration: missing clone resource");
3708 if (get_hook(bh
, postcmdbuf
, sizeof (postcmdbuf
), brand_get_postclone
,
3709 target_zone
, zonepath
) != Z_OK
) {
3710 zerror("invalid brand configuration: missing postclone "
3717 if (get_hook(bh
, presnapbuf
, sizeof (presnapbuf
), brand_get_presnap
,
3718 source_zone
, source_zonepath
) != Z_OK
) {
3719 zerror("invalid brand configuration: missing presnap "
3726 if (get_hook(bh
, postsnapbuf
, sizeof (postsnapbuf
), brand_get_postsnap
,
3727 source_zone
, source_zonepath
) != Z_OK
) {
3728 zerror("invalid brand configuration: missing postsnap "
3735 if (get_hook(bh
, validsnapbuf
, sizeof (validsnapbuf
),
3736 brand_get_validatesnap
, target_zone
, zonepath
) != Z_OK
) {
3737 zerror("invalid brand configuration: missing validatesnap "
3745 /* Append all options to clone hook. */
3746 if (addoptions(cmdbuf
, argv
, sizeof (cmdbuf
)) != Z_OK
) {
3751 /* Append all options to postclone hook. */
3752 if (addoptions(postcmdbuf
, argv
, sizeof (postcmdbuf
)) != Z_OK
) {
3758 if ((err
= zone_set_state(target_zone
, ZONE_STATE_INCOMPLETE
))
3761 zperror2(target_zone
, gettext("could not set state"));
3767 * The clone hook is optional. If it exists, use the hook for
3768 * cloning, otherwise use the built-in clone support
3770 if (cmdbuf
[0] != '\0') {
3771 /* Run the clone hook */
3772 status
= do_subproc(cmdbuf
);
3773 if ((status
= subproc_status(gettext("brand-specific clone"),
3774 status
, B_FALSE
)) != ZONE_SUBPROC_OK
) {
3775 if (status
== ZONE_SUBPROC_USAGE
&& !brand_help
)
3776 sub_usage(SHELP_CLONE
, CMD_CLONE
);
3785 /* If just help, we're done since there is no brand help. */
3789 /* Run the built-in clone support. */
3791 /* The only explicit built-in method is "copy". */
3792 if (method
!= NULL
&& strcmp(method
, "copy") != 0) {
3793 sub_usage(SHELP_CLONE
, CMD_CLONE
);
3798 if (snapshot
!= NULL
) {
3799 err
= clone_snapshot_zfs(snapshot
, zonepath
,
3803 * We always copy the clone unless the source is ZFS
3804 * and a ZFS clone worked. We fallback to copying if
3805 * the ZFS clone fails for some reason.
3808 if (method
== NULL
&& is_zonepath_zfs(source_zonepath
))
3809 err
= clone_zfs(source_zonepath
, zonepath
,
3810 presnapbuf
, postsnapbuf
);
3813 err
= clone_copy(source_zonepath
, zonepath
);
3817 if (err
== Z_OK
&& postcmdbuf
[0] != '\0') {
3818 status
= do_subproc(postcmdbuf
);
3819 if ((err
= subproc_status("postclone", status
, B_FALSE
))
3820 != ZONE_SUBPROC_OK
) {
3821 zerror(gettext("post-clone configuration failed."));
3828 * If everything went well, we mark the zone as installed.
3831 err
= zone_set_state(target_zone
, ZONE_STATE_INSTALLED
);
3834 zperror2(target_zone
, gettext("could not set state"));
3838 zonecfg_release_lock_file(target_zone
, lockfd
);
3839 return ((err
== Z_OK
) ? Z_OK
: Z_ERR
);
3843 * Used when removing a zonepath after uninstalling or cleaning up after
3844 * the move subcommand. This handles a zonepath that has non-standard
3845 * contents so that we will only cleanup the stuff we know about and leave
3846 * any user data alone.
3848 * If the "all" parameter is true then we should remove the whole zonepath
3849 * even if it has non-standard files/directories in it. This can be used when
3850 * we need to cleanup after moving the zonepath across file systems.
3852 * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND
3853 * and not the shell.
3856 cleanup_zonepath(char *zonepath
, boolean_t all
)
3860 boolean_t non_std
= B_FALSE
;
3864 * The SUNWattached.xml file is expected since it might
3865 * exist if the zone was force-attached after a
3868 char *std_entries
[] = {"dev", "lu", "root",
3869 "SUNWattached.xml", NULL
};
3870 /* (MAXPATHLEN * 3) is for the 3 std_entries dirs */
3871 char cmdbuf
[sizeof (RMCOMMAND
) + (MAXPATHLEN
* 3) + 64];
3874 * We shouldn't need these checks but lets be paranoid since we
3875 * could blow away the whole system here if we got the wrong zonepath.
3877 if (*zonepath
== NULL
|| strcmp(zonepath
, "/") == 0) {
3878 (void) fprintf(stderr
, "invalid zonepath '%s'\n", zonepath
);
3883 * If the dirpath is already gone (maybe it was manually removed) then
3884 * we just return Z_OK so that the cleanup is successful.
3886 if ((dirp
= opendir(zonepath
)) == NULL
)
3890 * Look through the zonepath directory to see if there are any
3891 * non-standard files/dirs. Also skip .zfs since that might be
3892 * there but we'll handle ZFS file systems as a special case.
3894 while ((dp
= readdir(dirp
)) != NULL
) {
3895 if (strcmp(dp
->d_name
, ".") == 0 ||
3896 strcmp(dp
->d_name
, "..") == 0 ||
3897 strcmp(dp
->d_name
, ".zfs") == 0)
3900 for (i
= 0; std_entries
[i
] != NULL
; i
++)
3901 if (strcmp(dp
->d_name
, std_entries
[i
]) == 0)
3904 if (std_entries
[i
] == NULL
)
3907 (void) closedir(dirp
);
3909 if (!all
&& non_std
) {
3911 * There are extra, non-standard directories/files in the
3912 * zonepath so we don't want to remove the zonepath. We
3913 * just want to remove the standard directories and leave
3914 * the user data alone.
3916 (void) snprintf(cmdbuf
, sizeof (cmdbuf
), "exec " RMCOMMAND
);
3918 for (i
= 0; std_entries
[i
] != NULL
; i
++) {
3919 char tmpbuf
[MAXPATHLEN
];
3921 if (snprintf(tmpbuf
, sizeof (tmpbuf
), " %s/%s",
3922 zonepath
, std_entries
[i
]) >= sizeof (tmpbuf
) ||
3923 strlcat(cmdbuf
, tmpbuf
, sizeof (cmdbuf
)) >=
3925 (void) fprintf(stderr
,
3926 gettext("path is too long\n"));
3931 status
= do_subproc(cmdbuf
);
3933 (void) fprintf(stderr
, gettext("WARNING: Unable to completely "
3934 "remove %s\nbecause it contains additional user data. "
3935 "Only the standard directory\nentries have been "
3939 return ((subproc_status(RMCOMMAND
, status
, B_TRUE
) ==
3940 ZONE_SUBPROC_OK
) ? Z_OK
: Z_ERR
);
3944 * There is nothing unexpected in the zonepath, try to get rid of the
3945 * whole zonepath directory.
3947 * If the zonepath is its own zfs file system, try to destroy the
3948 * file system. If that fails for some reason (e.g. it has clones)
3949 * then we'll just remove the contents of the zonepath.
3951 if (is_zonepath_zfs(zonepath
)) {
3952 if (destroy_zfs(zonepath
) == Z_OK
)
3954 (void) snprintf(cmdbuf
, sizeof (cmdbuf
), "exec " RMCOMMAND
3956 status
= do_subproc(cmdbuf
);
3957 return ((subproc_status(RMCOMMAND
, status
, B_TRUE
) ==
3958 ZONE_SUBPROC_OK
) ? Z_OK
: Z_ERR
);
3961 (void) snprintf(cmdbuf
, sizeof (cmdbuf
), "exec " RMCOMMAND
" %s",
3963 status
= do_subproc(cmdbuf
);
3965 return ((subproc_status(RMCOMMAND
, status
, B_TRUE
) == ZONE_SUBPROC_OK
)
3970 move_func(int argc
, char *argv
[])
3972 char *new_zonepath
= NULL
;
3975 char zonepath
[MAXPATHLEN
];
3976 zone_dochandle_t handle
;
3978 boolean_t is_zfs
= B_FALSE
;
3979 boolean_t root_fs_mounted
= B_FALSE
;
3982 boolean_t empty
= B_TRUE
;
3984 struct stat zonepath_buf
;
3985 struct stat new_zonepath_buf
;
3986 zone_mounts_t mounts
;
3988 if (zonecfg_in_alt_root()) {
3989 zerror(gettext("cannot move zone in alternate root"));
3994 if ((arg
= getopt(argc
, argv
, "?")) != EOF
) {
3997 sub_usage(SHELP_MOVE
, CMD_MOVE
);
3998 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
4000 sub_usage(SHELP_MOVE
, CMD_MOVE
);
4004 if (argc
!= (optind
+ 1)) {
4005 sub_usage(SHELP_MOVE
, CMD_MOVE
);
4008 new_zonepath
= argv
[optind
];
4009 if (sanity_check(target_zone
, CMD_MOVE
, B_FALSE
, B_TRUE
, B_FALSE
)
4012 if (verify_details(CMD_MOVE
, argv
) != Z_OK
)
4016 * Check out the new zonepath. This has the side effect of creating
4017 * a directory for the new zonepath. We depend on this later when we
4018 * stat to see if we are doing a cross file system move or not.
4020 if (validate_zonepath(new_zonepath
, CMD_MOVE
) != Z_OK
)
4023 if ((err
= zone_get_zonepath(target_zone
, zonepath
, sizeof (zonepath
)))
4026 zperror2(target_zone
, gettext("could not get zone path"));
4030 if (stat(zonepath
, &zonepath_buf
) == -1) {
4031 zperror(gettext("could not stat zone path"), B_FALSE
);
4035 if (stat(new_zonepath
, &new_zonepath_buf
) == -1) {
4036 zperror(gettext("could not stat new zone path"), B_FALSE
);
4041 * Check if the destination directory is empty.
4043 if ((dirp
= opendir(new_zonepath
)) == NULL
) {
4044 zperror(gettext("could not open new zone path"), B_FALSE
);
4047 while ((dp
= readdir(dirp
)) != (struct dirent
*)0) {
4048 if (strcmp(dp
->d_name
, ".") == 0 ||
4049 strcmp(dp
->d_name
, "..") == 0)
4054 (void) closedir(dirp
);
4056 /* Error if there is anything in the destination directory. */
4058 (void) fprintf(stderr
, gettext("could not move zone to %s: "
4059 "directory not empty\n"), new_zonepath
);
4064 * Collect information about mounts within the zone's zonepath.
4065 * Overlay mounts on the zone's root directory are erroneous.
4066 * Bail if we encounter any unexpected mounts.
4068 if (zone_mounts_init(&mounts
, zonepath
) != 0)
4070 if (mounts
.num_root_overlay_mounts
!= 0) {
4071 zerror(gettext("%d overlay mount(s) detected on %s/root."),
4072 mounts
.num_root_overlay_mounts
, zonepath
);
4073 goto err_and_mounts_destroy
;
4075 if (mounts
.num_unexpected_mounts
!= 0)
4076 goto err_and_mounts_destroy
;
4079 * Check if we are moving in the same file system and can do a fast
4080 * move or if we are crossing file systems and have to copy the data.
4082 fast
= (zonepath_buf
.st_dev
== new_zonepath_buf
.st_dev
);
4084 if ((handle
= zonecfg_init_handle()) == NULL
) {
4085 zperror(cmd_to_str(CMD_MOVE
), B_TRUE
);
4086 goto err_and_mounts_destroy
;
4089 if ((err
= zonecfg_get_handle(target_zone
, handle
)) != Z_OK
) {
4091 zperror(cmd_to_str(CMD_MOVE
), B_TRUE
);
4092 goto err_and_fini_handle
;
4095 if (zonecfg_grab_lock_file(target_zone
, &lockfd
) != Z_OK
) {
4096 zerror(gettext("another %s may have an operation in progress."),
4098 goto err_and_fini_handle
;
4102 * Unmount the zone's root filesystem before we move the zone's
4105 if (zone_unmount_rootfs(&mounts
, zonepath
, B_FALSE
) != 0)
4106 goto err_and_rele_lockfile
;
4109 * We're making some file system changes now so we have to clean up
4110 * the file system before we are done. This will either clean up the
4111 * new zonepath if the zonecfg update failed or it will clean up the
4112 * old zonepath if everything is ok.
4116 if (is_zonepath_zfs(zonepath
) &&
4117 move_zfs(zonepath
, new_zonepath
) != Z_ERR
) {
4121 /* same file system, use rename for a quick move */
4124 * Remove the new_zonepath directory that got created above
4125 * during the validation. It gets in the way of the rename.
4127 if (rmdir(new_zonepath
) != 0) {
4128 zperror(gettext("could not rmdir new zone path"),
4130 (void) zone_mount_rootfs(&mounts
, zonepath
);
4131 goto err_and_rele_lockfile
;
4134 if (rename(zonepath
, new_zonepath
) != 0) {
4136 * If this fails we don't need to do all of the
4137 * cleanup that happens for the rest of the code
4138 * so just return from this error.
4140 zperror(gettext("could not move zone"), B_FALSE
);
4141 (void) zone_mount_rootfs(&mounts
, zonepath
);
4142 goto err_and_rele_lockfile
;
4147 * Attempt to create a ZFS fs for the new zonepath. As usual,
4148 * we don't care if this works or not since we always have the
4149 * default behavior of a simple directory for the zonepath.
4151 create_zfs_zonepath(new_zonepath
);
4153 (void) printf(gettext(
4154 "Moving across file systems; copying zonepath %s..."),
4156 (void) fflush(stdout
);
4158 err
= copy_zone(zonepath
, new_zonepath
);
4160 (void) printf("\n");
4166 * Mount the zone's root filesystem in the new zonepath if there was
4167 * a root mount prior to the move.
4169 if (zone_mount_rootfs(&mounts
, new_zonepath
) != 0) {
4173 root_fs_mounted
= B_TRUE
;
4175 if ((err
= zonecfg_set_zonepath(handle
, new_zonepath
)) != Z_OK
) {
4177 zperror(gettext("could not set new zonepath"), B_TRUE
);
4181 if ((err
= zonecfg_save(handle
)) != Z_OK
) {
4183 zperror(gettext("zonecfg save failed"), B_TRUE
);
4190 zonecfg_fini_handle(handle
);
4191 zonecfg_release_lock_file(target_zone
, lockfd
);
4194 * Clean up the file system based on how things went. We either
4195 * clean up the new zonepath if the operation failed for some reason
4196 * or we clean up the old zonepath if everything is ok.
4200 * Check for the unlikely scenario in which the zone's
4201 * zonepath and its root file system moved but libzonecfg
4202 * couldn't save the new zonepath to the zone's configuration
4203 * file. The mounted root filesystem must be unmounted before
4204 * zoneadm restores the zone's zonepath.
4206 if (root_fs_mounted
&& zone_unmount_rootfs(&mounts
,
4207 new_zonepath
, B_TRUE
) != 0) {
4209 * We can't forcibly unmount the zone's root file system
4210 * from the new zonepath. Bail!
4212 zerror(gettext("fatal error: cannot unmount %s/root\n"),
4214 goto err_and_mounts_destroy
;
4217 /* The zonecfg update failed, cleanup the new zonepath. */
4219 if (move_zfs(new_zonepath
, zonepath
) == Z_ERR
) {
4220 (void) fprintf(stderr
, gettext("could not "
4221 "restore zonepath, the zfs mountpoint is "
4222 "set as:\n%s\n"), new_zonepath
);
4224 * err is already != Z_OK since we're reverting
4227 (void) zone_mount_rootfs(&mounts
, zonepath
);
4230 if (rename(new_zonepath
, zonepath
) != 0) {
4231 zperror(gettext("could not restore zonepath"),
4234 * err is already != Z_OK since we're reverting
4237 (void) zone_mount_rootfs(&mounts
, zonepath
);
4240 (void) printf(gettext("Cleaning up zonepath %s..."),
4242 (void) fflush(stdout
);
4243 err
= cleanup_zonepath(new_zonepath
, B_TRUE
);
4244 (void) printf("\n");
4248 zperror(gettext("could not remove new "
4249 "zonepath"), B_TRUE
);
4252 * Because we're reverting we know the mainline
4253 * code failed but we just reused the err
4254 * variable so we reset it back to Z_ERR.
4259 (void) zone_mount_rootfs(&mounts
, zonepath
);
4262 /* The move was successful, cleanup the old zonepath. */
4263 if (!is_zfs
&& !fast
) {
4265 gettext("Cleaning up zonepath %s..."), zonepath
);
4266 (void) fflush(stdout
);
4267 err
= cleanup_zonepath(zonepath
, B_TRUE
);
4268 (void) printf("\n");
4272 zperror(gettext("could not remove zonepath"),
4278 zone_mounts_destroy(&mounts
);
4279 return ((err
== Z_OK
) ? Z_OK
: Z_ERR
);
4281 err_and_rele_lockfile
:
4282 zonecfg_release_lock_file(target_zone
, lockfd
);
4283 err_and_fini_handle
:
4284 zonecfg_fini_handle(handle
);
4285 err_and_mounts_destroy
:
4286 zone_mounts_destroy(&mounts
);
4292 detach_func(int argc
, char *argv
[])
4296 char zonepath
[MAXPATHLEN
];
4297 char cmdbuf
[MAXPATHLEN
];
4298 char precmdbuf
[MAXPATHLEN
];
4299 boolean_t execute
= B_TRUE
;
4300 boolean_t brand_help
= B_FALSE
;
4301 brand_handle_t bh
= NULL
;
4304 if (zonecfg_in_alt_root()) {
4305 zerror(gettext("cannot detach zone in alternate root"));
4309 /* Check the argv string for args we handle internally */
4312 while ((arg
= getopt(argc
, argv
, "?n")) != EOF
) {
4315 if (optopt
== '?') {
4316 sub_usage(SHELP_DETACH
, CMD_DETACH
);
4317 brand_help
= B_TRUE
;
4319 /* Ignore unknown options - may be brand specific. */
4325 /* Ignore unknown options - may be brand specific. */
4334 if (sanity_check(target_zone
, CMD_DETACH
, B_FALSE
, B_TRUE
,
4337 if (verify_details(CMD_DETACH
, argv
) != Z_OK
)
4341 * We want a dry-run to work for a non-privileged user so we
4342 * only do minimal validation.
4344 if (target_zone
== NULL
) {
4345 zerror(gettext("no zone specified"));
4349 if (strcmp(target_zone
, GLOBAL_ZONENAME
) == 0) {
4350 zerror(gettext("%s operation is invalid for the "
4351 "global zone."), cmd_to_str(CMD_DETACH
));
4356 if ((err
= zone_get_zonepath(target_zone
, zonepath
, sizeof (zonepath
)))
4359 zperror2(target_zone
, gettext("could not get zone path"));
4363 /* Fetch the detach and predetach hooks from the brand configuration. */
4364 if ((bh
= brand_open(target_brand
)) == NULL
) {
4365 zerror(gettext("missing or invalid brand"));
4369 if (get_hook(bh
, cmdbuf
, sizeof (cmdbuf
), brand_get_detach
, target_zone
,
4370 zonepath
) != Z_OK
) {
4371 zerror("invalid brand configuration: missing detach resource");
4376 if (get_hook(bh
, precmdbuf
, sizeof (precmdbuf
), brand_get_predetach
,
4377 target_zone
, zonepath
) != Z_OK
) {
4378 zerror("invalid brand configuration: missing predetach "
4385 /* Append all options to predetach hook. */
4386 if (addoptions(precmdbuf
, argv
, sizeof (precmdbuf
)) != Z_OK
)
4389 /* Append all options to detach hook. */
4390 if (addoptions(cmdbuf
, argv
, sizeof (cmdbuf
)) != Z_OK
)
4393 if (execute
&& zonecfg_grab_lock_file(target_zone
, &lockfd
) != Z_OK
) {
4394 zerror(gettext("another %s may have an operation in progress."),
4399 /* If we have a brand predetach hook, run it. */
4400 if (!brand_help
&& precmdbuf
[0] != '\0') {
4401 status
= do_subproc(precmdbuf
);
4402 if (subproc_status(gettext("brand-specific predetach"),
4403 status
, B_FALSE
) != ZONE_SUBPROC_OK
) {
4406 assert(lockfd
>= 0);
4407 zonecfg_release_lock_file(target_zone
, lockfd
);
4411 assert(lockfd
== -1);
4416 if (cmdbuf
[0] != '\0') {
4417 /* Run the detach hook */
4418 status
= do_subproc(cmdbuf
);
4419 if ((status
= subproc_status(gettext("brand-specific detach"),
4420 status
, B_FALSE
)) != ZONE_SUBPROC_OK
) {
4421 if (status
== ZONE_SUBPROC_USAGE
&& !brand_help
)
4422 sub_usage(SHELP_DETACH
, CMD_DETACH
);
4425 assert(lockfd
>= 0);
4426 zonecfg_release_lock_file(target_zone
, lockfd
);
4430 assert(lockfd
== -1);
4435 zone_dochandle_t handle
;
4437 /* If just help, we're done since there is no brand help. */
4439 assert(lockfd
== -1);
4444 * Run the built-in detach support. Just generate a simple
4445 * zone definition XML file and detach.
4448 /* Don't detach the zone if anything is still mounted there */
4449 if (execute
&& zonecfg_find_mounts(zonepath
, NULL
, NULL
)) {
4450 (void) fprintf(stderr
, gettext("These file systems are "
4451 "mounted on subdirectories of %s.\n"), zonepath
);
4452 (void) zonecfg_find_mounts(zonepath
, zfm_print
, NULL
);
4453 err
= ZONE_SUBPROC_NOTCOMPLETE
;
4457 if ((handle
= zonecfg_init_handle()) == NULL
) {
4458 zperror(cmd_to_str(CMD_DETACH
), B_TRUE
);
4459 err
= ZONE_SUBPROC_NOTCOMPLETE
;
4463 if ((err
= zonecfg_get_handle(target_zone
, handle
)) != Z_OK
) {
4465 zperror(cmd_to_str(CMD_DETACH
), B_TRUE
);
4467 } else if ((err
= zonecfg_detach_save(handle
,
4468 (execute
? 0 : ZONE_DRY_RUN
))) != Z_OK
) {
4470 zperror(gettext("saving the detach manifest failed"),
4474 zonecfg_fini_handle(handle
);
4480 * Set the zone state back to configured unless we are running with the
4481 * no-execute option.
4483 if (execute
&& (err
= zone_set_state(target_zone
,
4484 ZONE_STATE_CONFIGURED
)) != Z_OK
) {
4486 zperror(gettext("could not reset state"), B_TRUE
);
4491 assert(lockfd
>= 0);
4492 zonecfg_release_lock_file(target_zone
, lockfd
);
4496 assert(lockfd
== -1);
4497 return ((err
== Z_OK
) ? Z_OK
: Z_ERR
);
4501 * Determine the brand when doing a dry-run attach. The zone does not have to
4502 * exist, so we have to read the incoming manifest to determine the zone's
4505 * Because the manifest has to be processed twice; once to determine the brand
4506 * and once to do the brand-specific attach logic, we always read it into a tmp
4507 * file. This handles the manifest coming from stdin or a regular file. The
4508 * tmpname parameter returns the name of the temporary file that the manifest
4512 dryrun_get_brand(char *manifest_path
, char *tmpname
, int size
)
4517 zone_dochandle_t local_handle
;
4518 zone_dochandle_t rem_handle
= NULL
;
4523 if (strcmp(manifest_path
, "-") == 0) {
4526 if ((fd
= open(manifest_path
, O_RDONLY
)) < 0) {
4527 if (getcwd(buf
, sizeof (buf
)) == NULL
)
4528 (void) strlcpy(buf
, "/", sizeof (buf
));
4529 zerror(gettext("could not open manifest path %s%s: %s"),
4530 (*manifest_path
== '/' ? "" : buf
), manifest_path
,
4536 (void) snprintf(tmpname
, size
, "/var/run/zone.%d", getpid());
4538 if ((ofd
= open(tmpname
, O_WRONLY
| O_CREAT
, S_IRUSR
| S_IWUSR
)) < 0) {
4539 zperror(gettext("could not save manifest"), B_FALSE
);
4544 while ((len
= read(fd
, buf
, sizeof (buf
))) > 0) {
4545 if (write(ofd
, buf
, len
) == -1) {
4546 zperror(gettext("could not save manifest"), B_FALSE
);
4553 if (close(ofd
) != 0) {
4554 zperror(gettext("could not save manifest"), B_FALSE
);
4561 if ((fd
= open(tmpname
, O_RDONLY
)) < 0) {
4562 zperror(gettext("could not open manifest path"), B_FALSE
);
4566 if ((local_handle
= zonecfg_init_handle()) == NULL
) {
4567 zperror(cmd_to_str(CMD_ATTACH
), B_TRUE
);
4572 if ((rem_handle
= zonecfg_init_handle()) == NULL
) {
4573 zperror(cmd_to_str(CMD_ATTACH
), B_TRUE
);
4578 if ((err
= zonecfg_attach_manifest(fd
, local_handle
, rem_handle
))
4582 if (err
== Z_INVALID_DOCUMENT
) {
4586 if (strcmp(manifest_path
, "-") == 0) {
4587 zerror(gettext("Input is not a valid XML "
4592 if (fstat(fd
, &st
) == -1 || !S_ISREG(st
.st_mode
)) {
4593 zerror(gettext("%s is not an XML file"),
4598 bzero(buf
, sizeof (buf
));
4599 (void) lseek(fd
, 0L, SEEK_SET
);
4600 if (read(fd
, buf
, sizeof (buf
) - 1) < 0 ||
4601 strncmp(buf
, "<?xml", 5) != 0)
4602 zerror(gettext("%s is not an XML file"),
4605 zerror(gettext("Cannot attach to an earlier "
4606 "release of the operating system"));
4608 zperror(cmd_to_str(CMD_ATTACH
), B_TRUE
);
4613 /* Retrieve remote handle brand type. */
4614 if (zonecfg_get_brand(rem_handle
, target_brand
, sizeof (target_brand
))
4616 zerror(gettext("missing or invalid brand"));
4621 zonecfg_fini_handle(local_handle
);
4622 zonecfg_fini_handle(rem_handle
);
4625 return ((res
== Z_OK
) ? Z_OK
: Z_ERR
);
4630 attach_func(int argc
, char *argv
[])
4634 boolean_t force
= B_FALSE
;
4635 zone_dochandle_t handle
;
4636 char zonepath
[MAXPATHLEN
];
4637 char cmdbuf
[MAXPATHLEN
];
4638 char postcmdbuf
[MAXPATHLEN
];
4639 boolean_t execute
= B_TRUE
;
4640 boolean_t brand_help
= B_FALSE
;
4641 char *manifest_path
;
4642 char tmpmanifest
[80];
4644 brand_handle_t bh
= NULL
;
4649 boolean_t forced_update
= B_FALSE
;
4651 if (zonecfg_in_alt_root()) {
4652 zerror(gettext("cannot attach zone in alternate root"));
4656 /* Check the argv string for args we handle internally */
4659 while ((arg
= getopt(argc
, argv
, "?Fn:U")) != EOF
) {
4662 if (optopt
== '?') {
4663 sub_usage(SHELP_ATTACH
, CMD_ATTACH
);
4664 brand_help
= B_TRUE
;
4666 /* Ignore unknown options - may be brand specific. */
4673 manifest_path
= optarg
;
4674 manifest_pos
= optind
- 1;
4678 * Undocumented 'force update' option for p2v update on
4679 * attach when zone is in the incomplete state. Change
4680 * the option back to 'u' and set forced_update flag.
4682 if (optind
== last_index
)
4685 offset
= optind
- 1;
4686 if ((up
= index(argv
[offset
], 'U')) != NULL
)
4688 forced_update
= B_TRUE
;
4691 /* Ignore unknown options - may be brand specific. */
4694 last_index
= optind
;
4702 /* dry-run and force flags are mutually exclusive */
4703 if (!execute
&& force
) {
4704 zerror(gettext("-F and -n flags are mutually exclusive"));
4709 * If the no-execute option was specified, we don't do validation and
4710 * need to figure out the brand, since there is no zone required to be
4711 * configured for this option.
4715 if (sanity_check(target_zone
, CMD_ATTACH
, B_FALSE
,
4716 B_TRUE
, forced_update
) != Z_OK
)
4718 if (verify_details(CMD_ATTACH
, argv
) != Z_OK
)
4722 if ((err
= zone_get_zonepath(target_zone
, zonepath
,
4723 sizeof (zonepath
))) != Z_OK
) {
4725 zperror2(target_zone
,
4726 gettext("could not get zone path"));
4730 if (dryrun_get_brand(manifest_path
, tmpmanifest
,
4731 sizeof (tmpmanifest
)) != Z_OK
)
4734 argv
[manifest_pos
] = tmpmanifest
;
4736 (void) strlcpy(zonepath
, "-", sizeof (zonepath
));
4738 /* Run the brand's verify_adm hook. */
4739 if (verify_brand(NULL
, CMD_ATTACH
, argv
) != Z_OK
)
4744 * Fetch the attach and postattach hooks from the brand configuration.
4746 if ((bh
= brand_open(target_brand
)) == NULL
) {
4747 zerror(gettext("missing or invalid brand"));
4751 if (get_hook(bh
, cmdbuf
, sizeof (cmdbuf
), brand_get_attach
, target_zone
,
4752 zonepath
) != Z_OK
) {
4753 zerror("invalid brand configuration: missing attach resource");
4758 if (get_hook(bh
, postcmdbuf
, sizeof (postcmdbuf
), brand_get_postattach
,
4759 target_zone
, zonepath
) != Z_OK
) {
4760 zerror("invalid brand configuration: missing postattach "
4767 /* Append all options to attach hook. */
4768 if (addoptions(cmdbuf
, argv
, sizeof (cmdbuf
)) != Z_OK
)
4771 /* Append all options to postattach hook. */
4772 if (addoptions(postcmdbuf
, argv
, sizeof (postcmdbuf
)) != Z_OK
)
4775 if (execute
&& !brand_help
) {
4776 if (zonecfg_grab_lock_file(target_zone
, &lockfd
) != Z_OK
) {
4777 zerror(gettext("another %s may have an operation in "
4778 "progress."), "zoneadm");
4785 * Not a force-attach, so we need to actually do the work.
4787 if (cmdbuf
[0] != '\0') {
4788 /* Run the attach hook */
4789 status
= do_subproc(cmdbuf
);
4790 if ((status
= subproc_status(gettext("brand-specific "
4791 "attach"), status
, B_FALSE
)) != ZONE_SUBPROC_OK
) {
4792 if (status
== ZONE_SUBPROC_USAGE
&& !brand_help
)
4793 sub_usage(SHELP_ATTACH
, CMD_ATTACH
);
4795 if (execute
&& !brand_help
) {
4796 assert(zonecfg_lock_file_held(&lockfd
));
4797 zonecfg_release_lock_file(target_zone
,
4802 assert(lockfd
== -1);
4808 * Else run the built-in attach support.
4809 * This is a no-op since there is nothing to validate.
4812 /* If dry-run or help, then we're done. */
4813 if (!execute
|| brand_help
) {
4815 (void) unlink(tmpmanifest
);
4816 assert(lockfd
== -1);
4821 /* Now we can validate that the zonepath exists. */
4822 if (validate_zonepath(zonepath
, CMD_ATTACH
) != Z_OK
) {
4823 (void) fprintf(stderr
, gettext("could not verify zonepath %s "
4824 "because of the above errors.\n"), zonepath
);
4826 assert(zonecfg_lock_file_held(&lockfd
));
4827 zonecfg_release_lock_file(target_zone
, lockfd
);
4831 if ((handle
= zonecfg_init_handle()) == NULL
) {
4832 zperror(cmd_to_str(CMD_ATTACH
), B_TRUE
);
4834 } else if ((err
= zonecfg_get_handle(target_zone
, handle
)) != Z_OK
) {
4836 zperror(cmd_to_str(CMD_ATTACH
), B_TRUE
);
4837 zonecfg_fini_handle(handle
);
4839 zonecfg_rm_detached(handle
, force
);
4840 zonecfg_fini_handle(handle
);
4844 (err
= zone_set_state(target_zone
, ZONE_STATE_INSTALLED
)) != Z_OK
) {
4846 zperror(gettext("could not reset state"), B_TRUE
);
4849 assert(zonecfg_lock_file_held(&lockfd
));
4850 zonecfg_release_lock_file(target_zone
, lockfd
);
4853 /* If we have a brand postattach hook, run it. */
4854 if (err
== Z_OK
&& !force
&& postcmdbuf
[0] != '\0') {
4855 status
= do_subproc(postcmdbuf
);
4856 if (subproc_status(gettext("brand-specific postattach"),
4857 status
, B_FALSE
) != ZONE_SUBPROC_OK
) {
4858 if ((err
= zone_set_state(target_zone
,
4859 ZONE_STATE_CONFIGURED
)) != Z_OK
) {
4861 zperror(gettext("could not reset state"),
4867 assert(lockfd
== -1);
4868 return ((err
== Z_OK
) ? Z_OK
: Z_ERR
);
4872 * On input, TRUE => yes, FALSE => no.
4873 * On return, TRUE => 1, FALSE => 0, could not ask => -1.
4877 ask_yesno(boolean_t default_answer
, const char *question
)
4879 char line
[64]; /* should be large enough to answer yes or no */
4881 if (!isatty(STDIN_FILENO
))
4884 (void) printf("%s (%s)? ", question
,
4885 default_answer
? "[y]/n" : "y/[n]");
4886 if (fgets(line
, sizeof (line
), stdin
) == NULL
||
4888 return (default_answer
? 1 : 0);
4889 if (tolower(line
[0]) == 'y')
4891 if (tolower(line
[0]) == 'n')
4898 uninstall_func(int argc
, char *argv
[])
4900 char line
[ZONENAME_MAX
+ 128]; /* Enough for "Are you sure ..." */
4901 char rootpath
[MAXPATHLEN
], zonepath
[MAXPATHLEN
];
4902 char cmdbuf
[MAXPATHLEN
];
4903 char precmdbuf
[MAXPATHLEN
];
4904 boolean_t force
= B_FALSE
;
4907 boolean_t brand_help
= B_FALSE
;
4908 brand_handle_t bh
= NULL
;
4911 if (zonecfg_in_alt_root()) {
4912 zerror(gettext("cannot uninstall zone in alternate root"));
4916 /* Check the argv string for args we handle internally */
4919 while ((arg
= getopt(argc
, argv
, "?F")) != EOF
) {
4922 if (optopt
== '?') {
4923 sub_usage(SHELP_UNINSTALL
, CMD_UNINSTALL
);
4924 brand_help
= B_TRUE
;
4926 /* Ignore unknown options - may be brand specific. */
4932 /* Ignore unknown options - may be brand specific. */
4938 if (sanity_check(target_zone
, CMD_UNINSTALL
, B_FALSE
, B_TRUE
,
4943 * Invoke brand-specific handler.
4945 if (invoke_brand_handler(CMD_UNINSTALL
, argv
) != Z_OK
)
4949 (void) snprintf(line
, sizeof (line
),
4950 gettext("Are you sure you want to %s zone %s"),
4951 cmd_to_str(CMD_UNINSTALL
), target_zone
);
4952 if ((answer
= ask_yesno(B_FALSE
, line
)) == 0) {
4954 } else if (answer
== -1) {
4955 zerror(gettext("Input not from terminal and -F "
4956 "not specified: %s not done."),
4957 cmd_to_str(CMD_UNINSTALL
));
4963 if ((err
= zone_get_zonepath(target_zone
, zonepath
,
4964 sizeof (zonepath
))) != Z_OK
) {
4966 zperror2(target_zone
, gettext("could not get zone path"));
4971 * Fetch the uninstall and preuninstall hooks from the brand
4974 if ((bh
= brand_open(target_brand
)) == NULL
) {
4975 zerror(gettext("missing or invalid brand"));
4979 if (get_hook(bh
, cmdbuf
, sizeof (cmdbuf
), brand_get_uninstall
,
4980 target_zone
, zonepath
) != Z_OK
) {
4981 zerror("invalid brand configuration: missing uninstall "
4987 if (get_hook(bh
, precmdbuf
, sizeof (precmdbuf
), brand_get_preuninstall
,
4988 target_zone
, zonepath
) != Z_OK
) {
4989 zerror("invalid brand configuration: missing preuninstall "
4996 /* Append all options to preuninstall hook. */
4997 if (addoptions(precmdbuf
, argv
, sizeof (precmdbuf
)) != Z_OK
)
5000 /* Append all options to uninstall hook. */
5001 if (addoptions(cmdbuf
, argv
, sizeof (cmdbuf
)) != Z_OK
)
5005 if ((err
= zone_get_rootpath(target_zone
, rootpath
,
5006 sizeof (rootpath
))) != Z_OK
) {
5008 zperror2(target_zone
, gettext("could not get root "
5014 * If there seems to be a zoneadmd running for this zone, call
5015 * it to tell it that an uninstall is happening; if all goes
5016 * well it will then shut itself down.
5018 if (zonecfg_ping_zoneadmd(target_zone
) == Z_OK
) {
5019 zone_cmd_arg_t zarg
;
5020 zarg
.cmd
= Z_NOTE_UNINSTALLING
;
5021 /* we don't care too much if this fails, just plow on */
5022 (void) zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
,
5026 if (zonecfg_grab_lock_file(target_zone
, &lockfd
) != Z_OK
) {
5027 zerror(gettext("another %s may have an operation in "
5028 "progress."), "zoneadm");
5032 /* Don't uninstall the zone if anything is mounted there */
5033 err
= zonecfg_find_mounts(rootpath
, NULL
, NULL
);
5035 zerror(gettext("These file systems are mounted on "
5036 "subdirectories of %s.\n"), rootpath
);
5037 (void) zonecfg_find_mounts(rootpath
, zfm_print
, NULL
);
5038 zonecfg_release_lock_file(target_zone
, lockfd
);
5043 /* If we have a brand preuninstall hook, run it. */
5044 if (!brand_help
&& precmdbuf
[0] != '\0') {
5045 status
= do_subproc(precmdbuf
);
5046 if (subproc_status(gettext("brand-specific preuninstall"),
5047 status
, B_FALSE
) != ZONE_SUBPROC_OK
) {
5048 zonecfg_release_lock_file(target_zone
, lockfd
);
5054 err
= zone_set_state(target_zone
, ZONE_STATE_INCOMPLETE
);
5057 zperror2(target_zone
, gettext("could not set state"));
5063 * If there is a brand uninstall hook, use it, otherwise use the
5064 * built-in uninstall code.
5066 if (cmdbuf
[0] != '\0') {
5067 /* Run the uninstall hook */
5068 status
= do_subproc(cmdbuf
);
5069 if ((status
= subproc_status(gettext("brand-specific "
5070 "uninstall"), status
, B_FALSE
)) != ZONE_SUBPROC_OK
) {
5071 if (status
== ZONE_SUBPROC_USAGE
&& !brand_help
)
5072 sub_usage(SHELP_UNINSTALL
, CMD_UNINSTALL
);
5074 zonecfg_release_lock_file(target_zone
, lockfd
);
5081 /* If just help, we're done since there is no brand help. */
5085 /* Run the built-in uninstall support. */
5086 if ((err
= cleanup_zonepath(zonepath
, B_FALSE
)) != Z_OK
) {
5088 zperror2(target_zone
, gettext("cleaning up zonepath "
5094 err
= zone_set_state(target_zone
, ZONE_STATE_CONFIGURED
);
5097 zperror2(target_zone
, gettext("could not reset state"));
5100 zonecfg_release_lock_file(target_zone
, lockfd
);
5106 mount_func(int argc
, char *argv
[])
5108 zone_cmd_arg_t zarg
;
5109 boolean_t force
= B_FALSE
;
5113 * The only supported subargument to the "mount" subcommand is
5114 * "-f", which forces us to mount a zone in the INCOMPLETE state.
5117 if ((arg
= getopt(argc
, argv
, "f")) != EOF
) {
5129 if (sanity_check(target_zone
, CMD_MOUNT
, B_FALSE
, B_FALSE
, force
)
5132 if (verify_details(CMD_MOUNT
, argv
) != Z_OK
)
5135 zarg
.cmd
= force
? Z_FORCEMOUNT
: Z_MOUNT
;
5136 zarg
.bootbuf
[0] = '\0';
5137 if (zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
, B_TRUE
) != 0) {
5138 zerror(gettext("call to %s failed"), "zoneadmd");
5146 unmount_func(int argc
, char *argv
[])
5148 zone_cmd_arg_t zarg
;
5152 if (sanity_check(target_zone
, CMD_UNMOUNT
, B_FALSE
, B_FALSE
, B_FALSE
)
5156 zarg
.cmd
= Z_UNMOUNT
;
5157 if (zonecfg_call_zoneadmd(target_zone
, &zarg
, locale
, B_TRUE
) != 0) {
5158 zerror(gettext("call to %s failed"), "zoneadmd");
5165 mark_func(int argc
, char *argv
[])
5169 boolean_t force
= B_FALSE
;
5174 while ((arg
= getopt(argc
, argv
, "F")) != EOF
) {
5184 if (argc
!= (optind
+ 1))
5187 if (strcmp(argv
[optind
], "configured") == 0)
5188 state
= ZONE_STATE_CONFIGURED
;
5189 else if (strcmp(argv
[optind
], "incomplete") == 0)
5190 state
= ZONE_STATE_INCOMPLETE
;
5191 else if (strcmp(argv
[optind
], "installed") == 0)
5192 state
= ZONE_STATE_INSTALLED
;
5196 if (state
!= ZONE_STATE_INCOMPLETE
&& !force
)
5199 if (sanity_check(target_zone
, CMD_MARK
, B_FALSE
, B_TRUE
, B_FALSE
)
5204 * Invoke brand-specific handler.
5206 if (invoke_brand_handler(CMD_MARK
, argv
) != Z_OK
)
5209 if (zonecfg_grab_lock_file(target_zone
, &lockfd
) != Z_OK
) {
5210 zerror(gettext("another %s may have an operation in progress."),
5215 err
= zone_set_state(target_zone
, state
);
5218 zperror2(target_zone
, gettext("could not set state"));
5220 zonecfg_release_lock_file(target_zone
, lockfd
);
5226 * Check what scheduling class we're running under and print a warning if
5227 * we're not using FSS.
5230 check_sched_fss(zone_dochandle_t handle
)
5232 char class_name
[PC_CLNMSZ
];
5234 if (zonecfg_get_dflt_sched_class(handle
, class_name
,
5235 sizeof (class_name
)) != Z_OK
) {
5236 zerror(gettext("WARNING: unable to determine the zone's "
5237 "scheduling class"));
5238 } else if (strcmp("FSS", class_name
) != 0) {
5239 zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n"
5240 "FSS is not the default scheduling class for this zone. "
5241 "FSS will be\nused for processes in the zone but to get "
5242 "the full benefit of FSS,\nit should be the default "
5243 "scheduling class. See dispadmin(1M) for\nmore details."));
5251 check_cpu_shares_sched(zone_dochandle_t handle
)
5255 struct zone_rctltab rctl
;
5257 if ((err
= zonecfg_setrctlent(handle
)) != Z_OK
) {
5259 zperror(cmd_to_str(CMD_APPLY
), B_TRUE
);
5263 while (zonecfg_getrctlent(handle
, &rctl
) == Z_OK
) {
5264 if (strcmp(rctl
.zone_rctl_name
, "zone.cpu-shares") == 0) {
5265 if (check_sched_fss(handle
) != Z_OK
)
5271 (void) zonecfg_endrctlent(handle
);
5277 * Check if there is a mix of processes running in different pools within the
5278 * zone. This is currently only going to be called for the global zone from
5279 * apply_func but that could be generalized in the future.
5282 mixed_pools(zoneid_t zoneid
)
5286 boolean_t mixed
= B_FALSE
;
5287 boolean_t poolid_set
= B_FALSE
;
5288 poolid_t last_poolid
= 0;
5290 if ((dirp
= opendir("/proc")) == NULL
) {
5291 zerror(gettext("could not open /proc"));
5295 while ((dent
= readdir(dirp
)) != NULL
) {
5298 char procpath
[MAXPATHLEN
];
5300 if (dent
->d_name
[0] == '.')
5303 (void) snprintf(procpath
, sizeof (procpath
), "/proc/%s/psinfo",
5306 if ((procfd
= open(procpath
, O_RDONLY
)) == -1)
5309 if (read(procfd
, &ps
, sizeof (ps
)) == sizeof (psinfo_t
)) {
5310 /* skip processes in other zones and system processes */
5311 if (zoneid
!= ps
.pr_zoneid
|| ps
.pr_flag
& SSYS
) {
5312 (void) close(procfd
);
5317 if (ps
.pr_poolid
!= last_poolid
)
5320 last_poolid
= ps
.pr_poolid
;
5321 poolid_set
= B_TRUE
;
5325 (void) close(procfd
);
5331 (void) closedir(dirp
);
5337 * Check if a persistent or temporary pool is configured for the zone.
5338 * This is currently only going to be called for the global zone from
5339 * apply_func but that could be generalized in the future.
5342 pool_configured(zone_dochandle_t handle
)
5345 struct zone_psettab pset_tab
;
5346 char poolname
[MAXPATHLEN
];
5348 err1
= zonecfg_lookup_pset(handle
, &pset_tab
);
5349 err2
= zonecfg_get_pool(handle
, poolname
, sizeof (poolname
));
5351 if (err1
== Z_NO_ENTRY
&&
5352 (err2
== Z_NO_ENTRY
|| (err2
== Z_OK
&& strlen(poolname
) == 0)))
5359 * This is an undocumented interface which is currently only used to apply
5360 * the global zone resource management settings when the system boots.
5361 * This function does not yet properly handle updating a running system so
5362 * any projects running in the zone would be trashed if this function
5363 * were to run after the zone had booted. It also does not reset any
5364 * rctl settings that were removed from zonecfg. There is still work to be
5365 * done before we can properly support dynamically updating the resource
5366 * management settings for a running zone (global or non-global). Thus, this
5367 * functionality is undocumented for now.
5371 apply_func(int argc
, char *argv
[])
5375 priv_set_t
*privset
;
5377 zone_dochandle_t handle
;
5378 struct zone_mcaptab mcap
;
5381 zoneid
= getzoneid();
5383 if (zonecfg_in_alt_root() || zoneid
!= GLOBAL_ZONEID
||
5384 target_zone
== NULL
|| strcmp(target_zone
, GLOBAL_ZONENAME
) != 0)
5385 return (usage(B_FALSE
));
5387 if ((privset
= priv_allocset()) == NULL
) {
5388 zerror(gettext("%s failed"), "priv_allocset");
5392 if (getppriv(PRIV_EFFECTIVE
, privset
) != 0) {
5393 zerror(gettext("%s failed"), "getppriv");
5394 priv_freeset(privset
);
5398 if (priv_isfullset(privset
) == B_FALSE
) {
5399 (void) usage(B_FALSE
);
5400 priv_freeset(privset
);
5403 priv_freeset(privset
);
5405 if ((handle
= zonecfg_init_handle()) == NULL
) {
5406 zperror(cmd_to_str(CMD_APPLY
), B_TRUE
);
5410 if ((err
= zonecfg_get_handle(target_zone
, handle
)) != Z_OK
) {
5412 zperror(cmd_to_str(CMD_APPLY
), B_TRUE
);
5413 zonecfg_fini_handle(handle
);
5417 /* specific error msgs are printed within apply_rctls */
5418 if ((err
= zonecfg_apply_rctls(target_zone
, handle
)) != Z_OK
) {
5420 zperror(cmd_to_str(CMD_APPLY
), B_TRUE
);
5424 if ((err
= check_cpu_shares_sched(handle
)) != Z_OK
)
5427 if (pool_configured(handle
)) {
5428 if (mixed_pools(zoneid
)) {
5429 zerror(gettext("Zone is using multiple resource "
5430 "pools. The pool\nconfiguration cannot be "
5431 "applied without rebooting."));
5436 * The next two blocks of code attempt to set up
5437 * temporary pools as well as persistent pools. In
5438 * both cases we call the functions unconditionally.
5439 * Within each funtion the code will check if the zone
5440 * is actually configured for a temporary pool or
5441 * persistent pool and just return if there is nothing
5444 if ((err
= zonecfg_bind_tmp_pool(handle
, zoneid
,
5445 pool_err
, sizeof (pool_err
))) != Z_OK
) {
5446 if (err
== Z_POOL
|| err
== Z_POOL_CREATE
||
5448 zerror("%s: %s", zonecfg_strerror(err
),
5451 zerror(gettext("could not bind zone to "
5452 "temporary pool: %s"),
5453 zonecfg_strerror(err
));
5457 if ((err
= zonecfg_bind_pool(handle
, zoneid
, pool_err
,
5458 sizeof (pool_err
))) != Z_OK
) {
5459 if (err
== Z_POOL
|| err
== Z_POOL_BIND
)
5460 zerror("%s: %s", zonecfg_strerror(err
),
5463 zerror("%s", zonecfg_strerror(err
));
5469 * If a memory cap is configured, set the cap in the kernel using
5470 * zone_setattr() and make sure the rcapd SMF service is enabled.
5472 if (zonecfg_getmcapent(handle
, &mcap
) == Z_OK
) {
5476 num
= (uint64_t)strtoll(mcap
.zone_physmem_cap
, NULL
, 10);
5477 if (zone_setattr(zoneid
, ZONE_ATTR_PHYS_MCAP
, &num
, 0) == -1) {
5478 zerror(gettext("could not set zone memory cap"));
5482 if (zonecfg_enable_rcapd(smf_err
, sizeof (smf_err
)) != Z_OK
) {
5483 zerror(gettext("enabling system/rcap service failed: "
5489 zonecfg_fini_handle(handle
);
5495 * This is an undocumented interface that is invoked by the zones SMF service
5496 * for installed zones that won't automatically boot.
5500 sysboot_func(int argc
, char *argv
[])
5503 zone_dochandle_t zone_handle
;
5504 brand_handle_t brand_handle
;
5505 char cmdbuf
[MAXPATHLEN
];
5506 char zonepath
[MAXPATHLEN
];
5509 * This subcommand can only be executed in the global zone on non-global
5512 if (zonecfg_in_alt_root())
5513 return (usage(B_FALSE
));
5514 if (sanity_check(target_zone
, CMD_SYSBOOT
, B_FALSE
, B_TRUE
, B_FALSE
) !=
5519 * Fetch the sysboot hook from the target zone's brand.
5521 if ((zone_handle
= zonecfg_init_handle()) == NULL
) {
5522 zperror(cmd_to_str(CMD_SYSBOOT
), B_TRUE
);
5525 if ((err
= zonecfg_get_handle(target_zone
, zone_handle
)) != Z_OK
) {
5527 zperror(cmd_to_str(CMD_SYSBOOT
), B_TRUE
);
5528 zonecfg_fini_handle(zone_handle
);
5531 if ((err
= zonecfg_get_zonepath(zone_handle
, zonepath
,
5532 sizeof (zonepath
))) != Z_OK
) {
5534 zperror(cmd_to_str(CMD_SYSBOOT
), B_TRUE
);
5535 zonecfg_fini_handle(zone_handle
);
5538 if ((brand_handle
= brand_open(target_brand
)) == NULL
) {
5539 zerror(gettext("missing or invalid brand during %s operation: "
5540 "%s"), cmd_to_str(CMD_SYSBOOT
), target_brand
);
5541 zonecfg_fini_handle(zone_handle
);
5544 err
= get_hook(brand_handle
, cmdbuf
, sizeof (cmdbuf
), brand_get_sysboot
,
5545 target_zone
, zonepath
);
5546 brand_close(brand_handle
);
5547 zonecfg_fini_handle(zone_handle
);
5549 zerror(gettext("unable to get brand hook from brand %s for %s "
5550 "operation"), target_brand
, cmd_to_str(CMD_SYSBOOT
));
5555 * If the hook wasn't defined (which is OK), then indicate success and
5556 * return. Otherwise, execute the hook.
5558 if (cmdbuf
[0] != '\0')
5559 return ((subproc_status(gettext("brand sysboot operation"),
5560 do_subproc(cmdbuf
), B_FALSE
) == ZONE_SUBPROC_OK
) ? Z_OK
:
5566 help_func(int argc
, char *argv
[])
5571 (void) usage(B_TRUE
);
5575 if ((arg
= getopt(argc
, argv
, "?")) != EOF
) {
5578 sub_usage(SHELP_HELP
, CMD_HELP
);
5579 return (optopt
== '?' ? Z_OK
: Z_USAGE
);
5581 sub_usage(SHELP_HELP
, CMD_HELP
);
5585 while (optind
< argc
) {
5586 /* Private commands have NULL short_usage; omit them */
5587 if ((cmd_num
= cmd_match(argv
[optind
])) < 0 ||
5588 cmdtab
[cmd_num
].short_usage
== NULL
) {
5589 sub_usage(SHELP_HELP
, CMD_HELP
);
5592 sub_usage(cmdtab
[cmd_num
].short_usage
, cmd_num
);
5599 * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
5603 cmd_match(char *cmd
)
5607 for (i
= CMD_MIN
; i
<= CMD_MAX
; i
++) {
5608 /* return only if there is an exact match */
5609 if (strcmp(cmd
, cmdtab
[i
].cmd_name
) == 0)
5610 return (cmdtab
[i
].cmd_num
);
5616 parse_and_run(int argc
, char *argv
[])
5618 int i
= cmd_match(argv
[0]);
5621 return (usage(B_FALSE
));
5622 return (cmdtab
[i
].handler(argc
- 1, &(argv
[1])));
5626 get_execbasename(char *execfullname
)
5628 char *last_slash
, *execbasename
;
5630 /* guard against '/' at end of command invocation */
5632 last_slash
= strrchr(execfullname
, '/');
5633 if (last_slash
== NULL
) {
5634 execbasename
= execfullname
;
5637 execbasename
= last_slash
+ 1;
5638 if (*execbasename
== '\0') {
5645 return (execbasename
);
5652 struct passwd
*nptr
;
5656 * Authorizations are checked to restrict access based on the
5657 * requested operation and zone name, It is assumed that the
5658 * program is running with all privileges, but that the real
5659 * user ID is that of the user or role on whose behalf we are
5660 * operating. So we start by getting the username that will be
5661 * used for subsequent authorization checks.
5665 if ((nptr
= getpwuid(uid
)) == NULL
) {
5666 zerror(gettext("could not get user name."));
5669 return (nptr
->pw_name
);
5673 main(int argc
, char **argv
)
5678 char *zone_lock_env
;
5681 if ((locale
= setlocale(LC_ALL
, "")) == NULL
)
5683 (void) textdomain(TEXT_DOMAIN
);
5684 setbuf(stdout
, NULL
);
5685 (void) sigset(SIGHUP
, SIG_IGN
);
5686 execname
= get_execbasename(argv
[0]);
5687 username
= get_username();
5689 if (chdir("/") != 0) {
5690 zerror(gettext("could not change directory to /."));
5695 * Use the default system mask rather than anything that may have been
5696 * set by the caller.
5698 (void) umask(CMASK
);
5700 if (init_zfs() != Z_OK
)
5703 while ((arg
= getopt(argc
, argv
, "?u:z:R:")) != EOF
) {
5706 return (usage(B_TRUE
));
5708 target_uuid
= optarg
;
5711 target_zone
= optarg
;
5713 case 'R': /* private option for admin/install use */
5714 if (*optarg
!= '/') {
5715 zerror(gettext("root path must be absolute."));
5718 if (stat(optarg
, &st
) == -1 || !S_ISDIR(st
.st_mode
)) {
5720 gettext("root path must be a directory."));
5723 zonecfg_set_root(optarg
);
5726 return (usage(B_FALSE
));
5731 return (usage(B_FALSE
));
5733 if (target_uuid
!= NULL
&& *target_uuid
!= '\0') {
5735 static char newtarget
[ZONENAME_MAX
];
5737 if (uuid_parse(target_uuid
, uuid
) == -1) {
5738 zerror(gettext("illegal UUID value specified"));
5741 if (zonecfg_get_name_by_uuid(uuid
, newtarget
,
5742 sizeof (newtarget
)) == Z_OK
)
5743 target_zone
= newtarget
;
5746 if (target_zone
!= NULL
&& zone_get_id(target_zone
, &zid
) != 0) {
5748 zperror(target_zone
, B_TRUE
);
5753 * See if we have inherited the right to manipulate this zone from
5754 * a zoneadm instance in our ancestry. If so, set zone_lock_cnt to
5755 * indicate it. If not, make that explicit in our environment.
5757 zonecfg_init_lock_file(target_zone
, &zone_lock_env
);
5759 /* Figure out what the system's default brand is */
5760 if (zonecfg_default_brand(default_brand
,
5761 sizeof (default_brand
)) != Z_OK
) {
5762 zerror(gettext("unable to determine default brand"));
5767 * If we are going to be operating on a single zone, retrieve its
5768 * brand type and determine whether it is native or not.
5770 if ((target_zone
!= NULL
) &&
5771 (strcmp(target_zone
, GLOBAL_ZONENAME
) != 0)) {
5772 if (zone_get_brand(target_zone
, target_brand
,
5773 sizeof (target_brand
)) != Z_OK
) {
5774 zerror(gettext("missing or invalid brand"));
5778 * In the alternate root environment, the only supported
5779 * operations are mount and unmount. In this case, just treat
5780 * the zone as native if it is cluster. Cluster zones can be
5781 * native for the purpose of LU or upgrade, and the cluster
5782 * brand may not exist in the miniroot (such as in net install
5785 if (strcmp(target_brand
, CLUSTER_BRAND_NAME
) == 0) {
5786 if (zonecfg_in_alt_root()) {
5787 (void) strlcpy(target_brand
, default_brand
,
5788 sizeof (target_brand
));
5793 err
= parse_and_run(argc
- optind
, &argv
[optind
]);