Print this page
2594 implement graceful shutdown for local zones in zoneadm
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/zoneadm/zoneadm.c
+++ new/usr/src/cmd/zoneadm/zoneadm.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 + * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
24 25 */
25 26
26 27 /*
27 28 * zoneadm is a command interpreter for zone administration. It is all in
28 29 * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
29 30 * main() calls parse_and_run() which calls cmd_match(), then invokes the
30 31 * appropriate command's handler function. The rest of the program is the
31 32 * handler functions and their helper functions.
32 33 *
33 34 * Some of the helper functions are used largely to simplify I18N: reducing
34 35 * the need for translation notes. This is particularly true of many of
35 36 * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
36 37 * than zerror(gettext("foo failed")) with a translation note indicating
37 38 * that "foo" need not be translated.
38 39 */
39 40
40 41 #include <stdio.h>
41 42 #include <errno.h>
42 43 #include <unistd.h>
43 44 #include <signal.h>
44 45 #include <stdarg.h>
45 46 #include <ctype.h>
46 47 #include <stdlib.h>
47 48 #include <string.h>
48 49 #include <wait.h>
49 50 #include <zone.h>
50 51 #include <priv.h>
51 52 #include <locale.h>
52 53 #include <libintl.h>
53 54 #include <libzonecfg.h>
54 55 #include <bsm/adt.h>
55 56 #include <sys/brand.h>
56 57 #include <sys/param.h>
57 58 #include <sys/types.h>
58 59 #include <sys/stat.h>
59 60 #include <sys/statvfs.h>
60 61 #include <assert.h>
61 62 #include <sys/sockio.h>
62 63 #include <sys/mntent.h>
63 64 #include <limits.h>
64 65 #include <dirent.h>
65 66 #include <uuid/uuid.h>
66 67 #include <fcntl.h>
67 68 #include <door.h>
68 69 #include <macros.h>
69 70 #include <libgen.h>
70 71 #include <fnmatch.h>
71 72 #include <sys/modctl.h>
72 73 #include <libbrand.h>
73 74 #include <libscf.h>
74 75 #include <procfs.h>
75 76 #include <strings.h>
76 77 #include <pool.h>
77 78 #include <sys/pool.h>
78 79 #include <sys/priocntl.h>
79 80 #include <sys/fsspriocntl.h>
80 81 #include <libdladm.h>
81 82 #include <libdllink.h>
82 83 #include <pwd.h>
83 84 #include <auth_list.h>
84 85 #include <auth_attr.h>
85 86 #include <secdb.h>
86 87
87 88 #include "zoneadm.h"
88 89
89 90 #define MAXARGS 8
90 91 #define SOURCE_ZONE (CMD_MAX + 1)
91 92
92 93 /* Reflects kernel zone entries */
93 94 typedef struct zone_entry {
94 95 zoneid_t zid;
95 96 char zname[ZONENAME_MAX];
96 97 char *zstate_str;
97 98 zone_state_t zstate_num;
98 99 char zbrand[MAXNAMELEN];
99 100 char zroot[MAXPATHLEN];
100 101 char zuuid[UUID_PRINTABLE_STRING_LENGTH];
101 102 zone_iptype_t ziptype;
102 103 } zone_entry_t;
103 104
104 105 #define CLUSTER_BRAND_NAME "cluster"
105 106
106 107 static zone_entry_t *zents;
107 108 static size_t nzents;
108 109
109 110 #define LOOPBACK_IF "lo0"
110 111 #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af))
111 112
112 113 struct net_if {
113 114 char *name;
114 115 int af;
115 116 };
116 117
117 118 /* 0755 is the default directory mode. */
118 119 #define DEFAULT_DIR_MODE \
119 120 (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
120 121
121 122 struct cmd {
122 123 uint_t cmd_num; /* command number */
↓ open down ↓ |
89 lines elided |
↑ open up ↑ |
123 124 char *cmd_name; /* command name */
124 125 char *short_usage; /* short form help */
125 126 int (*handler)(int argc, char *argv[]); /* function to call */
126 127
127 128 };
128 129
129 130 #define SHELP_HELP "help"
130 131 #define SHELP_BOOT "boot [-- boot_arguments]"
131 132 #define SHELP_HALT "halt"
132 133 #define SHELP_READY "ready"
134 +#define SHELP_SHUTDOWN "shutdown [-r [-- boot_arguments]]"
133 135 #define SHELP_REBOOT "reboot [-- boot_arguments]"
134 136 #define SHELP_LIST "list [-cipv]"
135 137 #define SHELP_VERIFY "verify"
136 138 #define SHELP_INSTALL "install [brand-specific args]"
137 139 #define SHELP_UNINSTALL "uninstall [-F] [brand-specific args]"
138 140 #define SHELP_CLONE "clone [-m method] [-s <ZFS snapshot>] "\
139 141 "[brand-specific args] zonename"
140 142 #define SHELP_MOVE "move zonepath"
141 143 #define SHELP_DETACH "detach [-n] [brand-specific args]"
142 144 #define SHELP_ATTACH "attach [-F] [-n <path>] [brand-specific args]"
143 145 #define SHELP_MARK "mark incomplete"
144 146
↓ open down ↓ |
2 lines elided |
↑ open up ↑ |
145 147 #define EXEC_PREFIX "exec "
146 148 #define EXEC_LEN (strlen(EXEC_PREFIX))
147 149 #define RMCOMMAND "/usr/bin/rm -rf"
148 150
149 151 static int cleanup_zonepath(char *, boolean_t);
150 152
151 153
152 154 static int help_func(int argc, char *argv[]);
153 155 static int ready_func(int argc, char *argv[]);
154 156 static int boot_func(int argc, char *argv[]);
157 +static int shutdown_func(int argc, char *argv[]);
155 158 static int halt_func(int argc, char *argv[]);
156 159 static int reboot_func(int argc, char *argv[]);
157 160 static int list_func(int argc, char *argv[]);
158 161 static int verify_func(int argc, char *argv[]);
159 162 static int install_func(int argc, char *argv[]);
160 163 static int uninstall_func(int argc, char *argv[]);
161 164 static int mount_func(int argc, char *argv[]);
162 165 static int unmount_func(int argc, char *argv[]);
163 166 static int clone_func(int argc, char *argv[]);
164 167 static int move_func(int argc, char *argv[]);
165 168 static int detach_func(int argc, char *argv[]);
166 169 static int attach_func(int argc, char *argv[]);
167 170 static int mark_func(int argc, char *argv[]);
168 171 static int apply_func(int argc, char *argv[]);
169 172 static int sysboot_func(int argc, char *argv[]);
170 173 static int sanity_check(char *zone, int cmd_num, boolean_t running,
171 174 boolean_t unsafe_when_running, boolean_t force);
↓ open down ↓ |
7 lines elided |
↑ open up ↑ |
172 175 static int cmd_match(char *cmd);
173 176 static int verify_details(int, char *argv[]);
174 177 static int verify_brand(zone_dochandle_t, int, char *argv[]);
175 178 static int invoke_brand_handler(int, char *argv[]);
176 179
177 180 static struct cmd cmdtab[] = {
178 181 { CMD_HELP, "help", SHELP_HELP, help_func },
179 182 { CMD_BOOT, "boot", SHELP_BOOT, boot_func },
180 183 { CMD_HALT, "halt", SHELP_HALT, halt_func },
181 184 { CMD_READY, "ready", SHELP_READY, ready_func },
185 + { CMD_SHUTDOWN, "shutdown", SHELP_SHUTDOWN, shutdown_func },
182 186 { CMD_REBOOT, "reboot", SHELP_REBOOT, reboot_func },
183 187 { CMD_LIST, "list", SHELP_LIST, list_func },
184 188 { CMD_VERIFY, "verify", SHELP_VERIFY, verify_func },
185 189 { CMD_INSTALL, "install", SHELP_INSTALL, install_func },
186 190 { CMD_UNINSTALL, "uninstall", SHELP_UNINSTALL,
187 191 uninstall_func },
188 192 /* mount and unmount are private commands for admin/install */
189 193 { CMD_MOUNT, "mount", NULL, mount_func },
190 194 { CMD_UNMOUNT, "unmount", NULL, unmount_func },
191 195 { CMD_CLONE, "clone", SHELP_CLONE, clone_func },
192 196 { CMD_MOVE, "move", SHELP_MOVE, move_func },
193 197 { CMD_DETACH, "detach", SHELP_DETACH, detach_func },
194 198 { CMD_ATTACH, "attach", SHELP_ATTACH, attach_func },
195 199 { CMD_MARK, "mark", SHELP_MARK, mark_func },
196 200 { CMD_APPLY, "apply", NULL, apply_func },
197 201 { CMD_SYSBOOT, "sysboot", NULL, sysboot_func }
198 202 };
199 203
200 204 /* global variables */
201 205
202 206 /* set early in main(), never modified thereafter, used all over the place */
203 207 static char *execname;
204 208 static char target_brand[MAXNAMELEN];
205 209 static char default_brand[MAXPATHLEN];
206 210 static char *locale;
207 211 char *target_zone;
208 212 static char *target_uuid;
209 213 char *username;
210 214
211 215 char *
212 216 cmd_to_str(int cmd_num)
213 217 {
214 218 assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
215 219 return (cmdtab[cmd_num].cmd_name);
216 220 }
217 221
218 222 /* This is a separate function because of gettext() wrapping. */
219 223 static char *
220 224 long_help(int cmd_num)
221 225 {
222 226 assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
223 227 switch (cmd_num) {
224 228 case CMD_HELP:
↓ open down ↓ |
33 lines elided |
↑ open up ↑ |
225 229 return (gettext("Print usage message."));
226 230 case CMD_BOOT:
227 231 return (gettext("Activates (boots) specified zone. See "
228 232 "zoneadm(1m) for valid boot\n\targuments."));
229 233 case CMD_HALT:
230 234 return (gettext("Halts specified zone, bypassing shutdown "
231 235 "scripts and removing runtime\n\tresources of the zone."));
232 236 case CMD_READY:
233 237 return (gettext("Prepares a zone for running applications but "
234 238 "does not start any user\n\tprocesses in the zone."));
239 + case CMD_SHUTDOWN:
240 + return (gettext("Gracefully shutdown the zone or reboot if "
241 + "the '-r' option is specified.\n\t"
242 + "See zoneadm(1m) for valid boot arguments."));
235 243 case CMD_REBOOT:
236 244 return (gettext("Restarts the zone (equivalent to a halt / "
237 245 "boot sequence).\n\tFails if the zone is not active. "
238 246 "See zoneadm(1m) for valid boot\n\targuments."));
239 247 case CMD_LIST:
240 248 return (gettext("Lists the current zones, or a "
241 249 "specific zone if indicated. By default,\n\tall "
242 250 "running zones are listed, though this can be "
243 251 "expanded to all\n\tinstalled zones with the -i "
244 252 "option or all configured zones with the\n\t-c "
245 253 "option. When used with the general -z <zone> and/or -u "
246 254 "<uuid-match>\n\toptions, lists only the specified "
247 255 "matching zone, but lists it\n\tregardless of its state, "
248 256 "and the -i and -c options are disallowed. The\n\t-v "
249 257 "option can be used to display verbose information: zone "
250 258 "name, id,\n\tcurrent state, root directory and options. "
251 259 "The -p option can be used\n\tto request machine-parsable "
252 260 "output. The -v and -p options are mutually\n\texclusive."
253 261 " If neither -v nor -p is used, just the zone name is "
254 262 "listed."));
255 263 case CMD_VERIFY:
256 264 return (gettext("Check to make sure the configuration "
257 265 "can safely be instantiated\n\ton the machine: "
258 266 "physical network interfaces exist, etc."));
259 267 case CMD_INSTALL:
260 268 return (gettext("Install the configuration on to the system. "
261 269 "All arguments are passed to the brand installation "
262 270 "function;\n\tsee brands(5) for more information."));
263 271 case CMD_UNINSTALL:
264 272 return (gettext("Uninstall the configuration from the system. "
265 273 "The -F flag can be used\n\tto force the action. All "
266 274 "other arguments are passed to the brand\n\tuninstall "
267 275 "function; see brands(5) for more information."));
268 276 case CMD_CLONE:
269 277 return (gettext("Clone the installation of another zone. "
270 278 "The -m option can be used to\n\tspecify 'copy' which "
271 279 "forces a copy of the source zone. The -s option\n\t"
272 280 "can be used to specify the name of a ZFS snapshot "
273 281 "that was taken from\n\ta previous clone command. The "
274 282 "snapshot will be used as the source\n\tinstead of "
275 283 "creating a new ZFS snapshot. All other arguments are "
276 284 "passed\n\tto the brand clone function; see "
277 285 "brands(5) for more information."));
278 286 case CMD_MOVE:
279 287 return (gettext("Move the zone to a new zonepath."));
280 288 case CMD_DETACH:
281 289 return (gettext("Detach the zone from the system. The zone "
282 290 "state is changed to\n\t'configured' (but the files under "
283 291 "the zonepath are untouched).\n\tThe zone can subsequently "
284 292 "be attached, or can be moved to another\n\tsystem and "
285 293 "attached there. The -n option can be used to specify\n\t"
286 294 "'no-execute' mode. When -n is used, the information "
287 295 "needed to attach\n\tthe zone is sent to standard output "
288 296 "but the zone is not actually\n\tdetached. All other "
289 297 "arguments are passed to the brand detach function;\n\tsee "
290 298 "brands(5) for more information."));
291 299 case CMD_ATTACH:
292 300 return (gettext("Attach the zone to the system. The zone "
293 301 "state must be 'configured'\n\tprior to attach; upon "
294 302 "successful completion, the zone state will be\n\t"
295 303 "'installed'. The system software on the current "
296 304 "system must be\n\tcompatible with the software on the "
297 305 "zone's original system.\n\tSpecify -F "
298 306 "to force the attach and skip software compatibility "
299 307 "tests.\n\tThe -n option can be used to specify "
300 308 "'no-execute' mode. When -n is\n\tused, the information "
301 309 "needed to attach the zone is read from the\n\tspecified "
302 310 "path and the configuration is only validated. The path "
303 311 "can\n\tbe '-' to specify standard input. The -F and -n "
304 312 "options are mutually\n\texclusive. All other arguments "
305 313 "are passed to the brand attach\n\tfunction; see "
306 314 "brands(5) for more information."));
307 315 case CMD_MARK:
308 316 return (gettext("Set the state of the zone. This can be used "
309 317 "to force the zone\n\tstate to 'incomplete' "
310 318 "administratively if some activity has rendered\n\tthe "
311 319 "zone permanently unusable. The only valid state that "
312 320 "may be\n\tspecified is 'incomplete'."));
313 321 default:
314 322 return ("");
315 323 }
316 324 /* NOTREACHED */
317 325 return (NULL);
318 326 }
319 327
320 328 /*
321 329 * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
322 330 * unexpected errors.
323 331 */
324 332
325 333 static int
326 334 usage(boolean_t explicit)
327 335 {
328 336 int i;
329 337 FILE *fd = explicit ? stdout : stderr;
330 338
331 339 (void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
332 340 (void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n",
333 341 execname);
334 342 (void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname,
335 343 gettext("subcommand"));
336 344 (void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
337 345 for (i = CMD_MIN; i <= CMD_MAX; i++) {
338 346 if (cmdtab[i].short_usage == NULL)
339 347 continue;
340 348 (void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
341 349 if (explicit)
342 350 (void) fprintf(fd, "\t%s\n\n", long_help(i));
343 351 }
344 352 if (!explicit)
345 353 (void) fputs("\n", fd);
346 354 return (Z_USAGE);
347 355 }
348 356
349 357 static void
350 358 sub_usage(char *short_usage, int cmd_num)
351 359 {
352 360 (void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
353 361 (void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
354 362 }
355 363
356 364 /*
357 365 * zperror() is like perror(3c) except that this also prints the executable
358 366 * name at the start of the message, and takes a boolean indicating whether
359 367 * to call libc'c strerror() or that from libzonecfg.
360 368 */
361 369
362 370 void
363 371 zperror(const char *str, boolean_t zonecfg_error)
364 372 {
365 373 (void) fprintf(stderr, "%s: %s: %s\n", execname, str,
366 374 zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
367 375 }
368 376
369 377 /*
370 378 * zperror2() is very similar to zperror() above, except it also prints a
371 379 * supplied zone name after the executable.
372 380 *
373 381 * All current consumers of this function want libzonecfg's strerror() rather
374 382 * than libc's; if this ever changes, this function can be made more generic
375 383 * like zperror() above.
376 384 */
377 385
378 386 void
379 387 zperror2(const char *zone, const char *str)
380 388 {
381 389 (void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
382 390 zonecfg_strerror(errno));
383 391 }
384 392
385 393 /* PRINTFLIKE1 */
386 394 void
387 395 zerror(const char *fmt, ...)
388 396 {
389 397 va_list alist;
390 398
391 399 va_start(alist, fmt);
392 400 (void) fprintf(stderr, "%s: ", execname);
393 401 if (target_zone != NULL)
394 402 (void) fprintf(stderr, "zone '%s': ", target_zone);
395 403 (void) vfprintf(stderr, fmt, alist);
396 404 (void) fprintf(stderr, "\n");
397 405 va_end(alist);
398 406 }
399 407
400 408 static void *
401 409 safe_calloc(size_t nelem, size_t elsize)
402 410 {
403 411 void *r = calloc(nelem, elsize);
404 412
405 413 if (r == NULL) {
406 414 zerror(gettext("failed to allocate %lu bytes: %s"),
407 415 (ulong_t)nelem * elsize, strerror(errno));
408 416 exit(Z_ERR);
409 417 }
410 418 return (r);
411 419 }
412 420
413 421 static void
414 422 zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
415 423 {
416 424 static boolean_t firsttime = B_TRUE;
417 425 char *ip_type_str;
418 426
419 427 /* Skip a zone that shutdown while we were collecting data. */
420 428 if (zent->zname[0] == '\0')
421 429 return;
422 430
423 431 if (zent->ziptype == ZS_EXCLUSIVE)
424 432 ip_type_str = "excl";
425 433 else
426 434 ip_type_str = "shared";
427 435
428 436 assert(!(verbose && parsable));
429 437 if (firsttime && verbose) {
430 438 firsttime = B_FALSE;
431 439 (void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n",
432 440 ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND",
433 441 "IP");
434 442 }
435 443 if (!verbose) {
436 444 char *cp, *clim;
437 445
438 446 if (!parsable) {
439 447 (void) printf("%s\n", zent->zname);
440 448 return;
441 449 }
442 450 if (zent->zid == ZONE_ID_UNDEFINED)
443 451 (void) printf("-");
444 452 else
445 453 (void) printf("%lu", zent->zid);
446 454 (void) printf(":%s:%s:", zent->zname, zent->zstate_str);
447 455 cp = zent->zroot;
448 456 while ((clim = strchr(cp, ':')) != NULL) {
449 457 (void) printf("%.*s\\:", clim - cp, cp);
450 458 cp = clim + 1;
451 459 }
452 460 (void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand,
453 461 ip_type_str);
454 462 return;
455 463 }
456 464 if (zent->zstate_str != NULL) {
457 465 if (zent->zid == ZONE_ID_UNDEFINED)
458 466 (void) printf("%*s", ZONEID_WIDTH, "-");
459 467 else
460 468 (void) printf("%*lu", ZONEID_WIDTH, zent->zid);
461 469 (void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname,
462 470 zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str);
463 471 }
464 472 }
465 473
466 474 static int
467 475 lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent)
468 476 {
469 477 char root[MAXPATHLEN], *cp;
470 478 int err;
471 479 uuid_t uuid;
472 480 zone_dochandle_t handle;
473 481
474 482 (void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
475 483 (void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
476 484 (void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand));
477 485 zent->zstate_str = "???";
478 486
479 487 zent->zid = zid;
480 488
481 489 if (zonecfg_get_uuid(zone_name, uuid) == Z_OK &&
482 490 !uuid_is_null(uuid))
483 491 uuid_unparse(uuid, zent->zuuid);
484 492 else
485 493 zent->zuuid[0] = '\0';
486 494
487 495 /*
488 496 * For labeled zones which query the zone path of lower-level
489 497 * zones, the path needs to be adjusted to drop the final
490 498 * "/root" component. This adjusted path is then useful
491 499 * for reading down any exported directories from the
492 500 * lower-level zone.
493 501 */
494 502 if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) {
495 503 if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot,
496 504 sizeof (zent->zroot)) == -1) {
497 505 zperror2(zent->zname,
498 506 gettext("could not get zone path."));
499 507 return (Z_ERR);
500 508 }
501 509 cp = zent->zroot + strlen(zent->zroot) - 5;
502 510 if (cp > zent->zroot && strcmp(cp, "/root") == 0)
503 511 *cp = 0;
504 512 } else {
505 513 if ((err = zone_get_zonepath(zent->zname, root,
506 514 sizeof (root))) != Z_OK) {
507 515 errno = err;
508 516 zperror2(zent->zname,
509 517 gettext("could not get zone path."));
510 518 return (Z_ERR);
511 519 }
512 520 (void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
513 521 }
514 522
515 523 if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
516 524 errno = err;
517 525 zperror2(zent->zname, gettext("could not get state"));
518 526 return (Z_ERR);
519 527 }
520 528 zent->zstate_str = zone_state_str(zent->zstate_num);
521 529
522 530 /*
523 531 * A zone's brand is only available in the .xml file describing it,
524 532 * which is only visible to the global zone. This causes
525 533 * zone_get_brand() to fail when called from within a non-global
526 534 * zone. Fortunately we only do this on labeled systems, where we
527 535 * know all zones are native.
528 536 */
529 537 if (getzoneid() != GLOBAL_ZONEID) {
530 538 assert(is_system_labeled() != 0);
531 539 (void) strlcpy(zent->zbrand, default_brand,
532 540 sizeof (zent->zbrand));
533 541 } else if (zone_get_brand(zent->zname, zent->zbrand,
534 542 sizeof (zent->zbrand)) != Z_OK) {
535 543 zperror2(zent->zname, gettext("could not get brand name"));
536 544 return (Z_ERR);
537 545 }
538 546
539 547 /*
540 548 * Get ip type of the zone.
541 549 * Note for global zone, ZS_SHARED is set always.
542 550 */
543 551 if (zid == GLOBAL_ZONEID) {
544 552 zent->ziptype = ZS_SHARED;
545 553 return (Z_OK);
546 554 }
547 555
548 556 /*
549 557 * There is a race condition where the zone could boot while
550 558 * we're walking the index file. In this case the zone state
551 559 * could be seen as running from the call above, but the zoneid
552 560 * would be undefined.
553 561 *
554 562 * There is also a race condition where the zone could shutdown after
555 563 * we got its running state above. This is also not an error and
556 564 * we fall back to getting the ziptype from the zone configuration.
557 565 */
558 566 if (zent->zstate_num == ZONE_STATE_RUNNING &&
559 567 zid != ZONE_ID_UNDEFINED) {
560 568 ushort_t flags;
561 569
562 570 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags,
563 571 sizeof (flags)) >= 0) {
564 572 if (flags & ZF_NET_EXCL)
565 573 zent->ziptype = ZS_EXCLUSIVE;
566 574 else
567 575 zent->ziptype = ZS_SHARED;
568 576 return (Z_OK);
569 577 }
570 578 }
571 579
572 580 if ((handle = zonecfg_init_handle()) == NULL) {
573 581 zperror2(zent->zname, gettext("could not init handle"));
574 582 return (Z_ERR);
575 583 }
576 584 if ((err = zonecfg_get_handle(zent->zname, handle)) != Z_OK) {
577 585 zperror2(zent->zname, gettext("could not get handle"));
578 586 zonecfg_fini_handle(handle);
579 587 return (Z_ERR);
580 588 }
581 589
582 590 if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) != Z_OK) {
583 591 zperror2(zent->zname, gettext("could not get ip-type"));
584 592 zonecfg_fini_handle(handle);
585 593 return (Z_ERR);
586 594 }
587 595 zonecfg_fini_handle(handle);
588 596
589 597 return (Z_OK);
590 598 }
591 599
592 600 /*
593 601 * fetch_zents() calls zone_list(2) to find out how many zones are running
594 602 * (which is stored in the global nzents), then calls zone_list(2) again
595 603 * to fetch the list of running zones (stored in the global zents). This
596 604 * function may be called multiple times, so if zents is already set, we
597 605 * return immediately to save work.
598 606 *
599 607 * Note that the data about running zones can change while this function
600 608 * is running, so its possible that the list of zones will have empty slots
601 609 * at the end.
602 610 */
603 611
604 612 static int
605 613 fetch_zents(void)
606 614 {
607 615 zoneid_t *zids = NULL;
608 616 uint_t nzents_saved;
609 617 int i, retv;
610 618 FILE *fp;
611 619 boolean_t inaltroot;
612 620 zone_entry_t *zentp;
613 621 const char *altroot;
614 622
615 623 if (nzents > 0)
616 624 return (Z_OK);
617 625
618 626 if (zone_list(NULL, &nzents) != 0) {
619 627 zperror(gettext("failed to get zoneid list"), B_FALSE);
620 628 return (Z_ERR);
621 629 }
622 630
623 631 again:
624 632 if (nzents == 0)
625 633 return (Z_OK);
626 634
627 635 zids = safe_calloc(nzents, sizeof (zoneid_t));
628 636 nzents_saved = nzents;
629 637
630 638 if (zone_list(zids, &nzents) != 0) {
631 639 zperror(gettext("failed to get zone list"), B_FALSE);
632 640 free(zids);
633 641 return (Z_ERR);
634 642 }
635 643 if (nzents != nzents_saved) {
636 644 /* list changed, try again */
637 645 free(zids);
638 646 goto again;
639 647 }
640 648
641 649 zents = safe_calloc(nzents, sizeof (zone_entry_t));
642 650
643 651 inaltroot = zonecfg_in_alt_root();
644 652 if (inaltroot) {
645 653 fp = zonecfg_open_scratch("", B_FALSE);
646 654 altroot = zonecfg_get_root();
647 655 } else {
648 656 fp = NULL;
649 657 }
650 658 zentp = zents;
651 659 retv = Z_OK;
652 660 for (i = 0; i < nzents; i++) {
653 661 char name[ZONENAME_MAX];
654 662 char altname[ZONENAME_MAX];
655 663 char rev_altroot[MAXPATHLEN];
656 664
657 665 if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
658 666 /*
659 667 * There is a race condition where the zone may have
660 668 * shutdown since we retrieved the number of running
661 669 * zones above. This is not an error, there will be
662 670 * an empty slot at the end of the list.
663 671 */
664 672 continue;
665 673 }
666 674 if (zonecfg_is_scratch(name)) {
667 675 /* Ignore scratch zones by default */
668 676 if (!inaltroot)
669 677 continue;
670 678 if (fp == NULL ||
671 679 zonecfg_reverse_scratch(fp, name, altname,
672 680 sizeof (altname), rev_altroot,
673 681 sizeof (rev_altroot)) == -1) {
674 682 zerror(gettext("could not resolve scratch "
675 683 "zone %s"), name);
676 684 retv = Z_ERR;
677 685 continue;
678 686 }
679 687 /* Ignore zones in other alternate roots */
680 688 if (strcmp(rev_altroot, altroot) != 0)
681 689 continue;
682 690 (void) strcpy(name, altname);
683 691 } else {
684 692 /* Ignore non-scratch when in an alternate root */
685 693 if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
686 694 continue;
687 695 }
688 696 if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
689 697 /*
690 698 * There is a race condition where the zone may have
691 699 * shutdown since we retrieved the number of running
692 700 * zones above. This is not an error, there will be
693 701 * an empty slot at the end of the list.
694 702 */
695 703 continue;
696 704 }
697 705 zentp++;
698 706 }
699 707 nzents = zentp - zents;
700 708 if (fp != NULL)
701 709 zonecfg_close_scratch(fp);
702 710
703 711 free(zids);
704 712 return (retv);
705 713 }
706 714
707 715 static int
708 716 zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
709 717 {
710 718 int i;
711 719 zone_entry_t zent;
712 720 FILE *cookie;
713 721 char *name;
714 722
715 723 /*
716 724 * First get the list of running zones from the kernel and print them.
717 725 * If that is all we need, then return.
718 726 */
719 727 if ((i = fetch_zents()) != Z_OK) {
720 728 /*
721 729 * No need for error messages; fetch_zents() has already taken
722 730 * care of this.
723 731 */
724 732 return (i);
725 733 }
726 734 for (i = 0; i < nzents; i++)
727 735 zone_print(&zents[i], verbose, parsable);
728 736 if (min_state >= ZONE_STATE_RUNNING)
729 737 return (Z_OK);
730 738 /*
731 739 * Next, get the full list of zones from the configuration, skipping
732 740 * any we have already printed.
733 741 */
734 742 cookie = setzoneent();
735 743 while ((name = getzoneent(cookie)) != NULL) {
736 744 for (i = 0; i < nzents; i++) {
737 745 if (strcmp(zents[i].zname, name) == 0)
738 746 break;
739 747 }
740 748 if (i < nzents) {
741 749 free(name);
742 750 continue;
743 751 }
744 752 if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
745 753 free(name);
746 754 continue;
747 755 }
748 756 free(name);
749 757 if (zent.zstate_num >= min_state)
750 758 zone_print(&zent, verbose, parsable);
751 759 }
752 760 endzoneent(cookie);
753 761 return (Z_OK);
754 762 }
755 763
756 764 /*
757 765 * Retrieve a zone entry by name. Returns NULL if no such zone exists.
758 766 */
759 767 static zone_entry_t *
760 768 lookup_running_zone(const char *str)
761 769 {
762 770 int i;
763 771
764 772 if (fetch_zents() != Z_OK)
765 773 return (NULL);
766 774
767 775 for (i = 0; i < nzents; i++) {
768 776 if (strcmp(str, zents[i].zname) == 0)
769 777 return (&zents[i]);
770 778 }
771 779 return (NULL);
772 780 }
773 781
774 782 /*
775 783 * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
776 784 * B_FALSE, it should be off. Return B_TRUE if the mode is bad (incorrect).
777 785 */
778 786 static boolean_t
779 787 bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
780 788 {
781 789 char *str;
782 790
783 791 assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
784 792 bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
785 793 bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
786 794 /*
787 795 * TRANSLATION_NOTE
788 796 * The strings below will be used as part of a larger message,
789 797 * either:
790 798 * (file name) must be (owner|group|world) (read|writ|execut)able
791 799 * or
792 800 * (file name) must not be (owner|group|world) (read|writ|execut)able
793 801 */
794 802 switch (bit) {
795 803 case S_IRUSR:
796 804 str = gettext("owner readable");
797 805 break;
798 806 case S_IWUSR:
799 807 str = gettext("owner writable");
800 808 break;
801 809 case S_IXUSR:
802 810 str = gettext("owner executable");
803 811 break;
804 812 case S_IRGRP:
805 813 str = gettext("group readable");
806 814 break;
807 815 case S_IWGRP:
808 816 str = gettext("group writable");
809 817 break;
810 818 case S_IXGRP:
811 819 str = gettext("group executable");
812 820 break;
813 821 case S_IROTH:
814 822 str = gettext("world readable");
815 823 break;
816 824 case S_IWOTH:
817 825 str = gettext("world writable");
818 826 break;
819 827 case S_IXOTH:
820 828 str = gettext("world executable");
821 829 break;
822 830 }
823 831 if ((mode & bit) == (on ? 0 : bit)) {
824 832 /*
825 833 * TRANSLATION_NOTE
826 834 * The first parameter below is a file name; the second
827 835 * is one of the "(owner|group|world) (read|writ|execut)able"
828 836 * strings from above.
829 837 */
830 838 /*
831 839 * The code below could be simplified but not in a way
832 840 * that would easily translate to non-English locales.
833 841 */
834 842 if (on) {
835 843 (void) fprintf(stderr, gettext("%s must be %s.\n"),
836 844 file, str);
837 845 } else {
838 846 (void) fprintf(stderr, gettext("%s must not be %s.\n"),
839 847 file, str);
840 848 }
841 849 return (B_TRUE);
842 850 }
843 851 return (B_FALSE);
844 852 }
845 853
846 854 /*
847 855 * We want to make sure that no zone has its zone path as a child node
848 856 * (in the directory sense) of any other. We do that by comparing this
849 857 * zone's path to the path of all other (non-global) zones. The comparison
850 858 * in each case is simple: add '/' to the end of the path, then do a
851 859 * strncmp() of the two paths, using the length of the shorter one.
852 860 */
853 861
854 862 static int
855 863 crosscheck_zonepaths(char *path)
856 864 {
857 865 char rpath[MAXPATHLEN]; /* resolved path */
858 866 char path_copy[MAXPATHLEN]; /* copy of original path */
859 867 char rpath_copy[MAXPATHLEN]; /* copy of original rpath */
860 868 struct zoneent *ze;
861 869 int res, err;
862 870 FILE *cookie;
863 871
864 872 cookie = setzoneent();
865 873 while ((ze = getzoneent_private(cookie)) != NULL) {
866 874 /* Skip zones which are not installed. */
867 875 if (ze->zone_state < ZONE_STATE_INSTALLED) {
868 876 free(ze);
869 877 continue;
870 878 }
871 879 /* Skip the global zone and the current target zone. */
872 880 if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
873 881 strcmp(ze->zone_name, target_zone) == 0) {
874 882 free(ze);
875 883 continue;
876 884 }
877 885 if (strlen(ze->zone_path) == 0) {
878 886 /* old index file without path, fall back */
879 887 if ((err = zone_get_zonepath(ze->zone_name,
880 888 ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
881 889 errno = err;
882 890 zperror2(ze->zone_name,
883 891 gettext("could not get zone path"));
884 892 free(ze);
885 893 continue;
886 894 }
887 895 }
888 896 (void) snprintf(path_copy, sizeof (path_copy), "%s%s",
889 897 zonecfg_get_root(), ze->zone_path);
890 898 res = resolvepath(path_copy, rpath, sizeof (rpath));
891 899 if (res == -1) {
892 900 if (errno != ENOENT) {
893 901 zperror(path_copy, B_FALSE);
894 902 free(ze);
895 903 return (Z_ERR);
896 904 }
897 905 (void) printf(gettext("WARNING: zone %s is installed, "
898 906 "but its %s %s does not exist.\n"), ze->zone_name,
899 907 "zonepath", path_copy);
900 908 free(ze);
901 909 continue;
902 910 }
903 911 rpath[res] = '\0';
904 912 (void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
905 913 (void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
906 914 if (strncmp(path_copy, rpath_copy,
907 915 min(strlen(path_copy), strlen(rpath_copy))) == 0) {
908 916 /*
909 917 * TRANSLATION_NOTE
910 918 * zonepath is a literal that should not be translated.
911 919 */
912 920 (void) fprintf(stderr, gettext("%s zonepath (%s) and "
913 921 "%s zonepath (%s) overlap.\n"),
914 922 target_zone, path, ze->zone_name, rpath);
915 923 free(ze);
916 924 return (Z_ERR);
917 925 }
918 926 free(ze);
919 927 }
920 928 endzoneent(cookie);
921 929 return (Z_OK);
922 930 }
923 931
924 932 static int
925 933 validate_zonepath(char *path, int cmd_num)
926 934 {
927 935 int res; /* result of last library/system call */
928 936 boolean_t err = B_FALSE; /* have we run into an error? */
929 937 struct stat stbuf;
930 938 struct statvfs64 vfsbuf;
931 939 char rpath[MAXPATHLEN]; /* resolved path */
932 940 char ppath[MAXPATHLEN]; /* parent path */
933 941 char rppath[MAXPATHLEN]; /* resolved parent path */
934 942 char rootpath[MAXPATHLEN]; /* root path */
935 943 zone_state_t state;
936 944
937 945 if (path[0] != '/') {
938 946 (void) fprintf(stderr,
939 947 gettext("%s is not an absolute path.\n"), path);
940 948 return (Z_ERR);
941 949 }
942 950 if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
943 951 if ((errno != ENOENT) ||
944 952 (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL &&
945 953 cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) {
946 954 zperror(path, B_FALSE);
947 955 return (Z_ERR);
948 956 }
949 957 if (cmd_num == CMD_VERIFY) {
950 958 /*
951 959 * TRANSLATION_NOTE
952 960 * zoneadm is a literal that should not be translated.
953 961 */
954 962 (void) fprintf(stderr, gettext("WARNING: %s does not "
955 963 "exist, so it could not be verified.\nWhen "
956 964 "'zoneadm %s' is run, '%s' will try to create\n%s, "
957 965 "and '%s' will be tried again,\nbut the '%s' may "
958 966 "fail if:\nthe parent directory of %s is group- or "
959 967 "other-writable\nor\n%s overlaps with any other "
960 968 "installed zones.\n"), path,
961 969 cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
962 970 path, cmd_to_str(CMD_VERIFY),
963 971 cmd_to_str(CMD_VERIFY), path, path);
964 972 return (Z_OK);
965 973 }
966 974 /*
967 975 * The zonepath is supposed to be mode 700 but its
968 976 * parent(s) 755. So use 755 on the mkdirp() then
969 977 * chmod() the zonepath itself to 700.
970 978 */
971 979 if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
972 980 zperror(path, B_FALSE);
973 981 return (Z_ERR);
974 982 }
975 983 /*
976 984 * If the chmod() fails, report the error, but might
977 985 * as well continue the verify procedure.
978 986 */
979 987 if (chmod(path, S_IRWXU) != 0)
980 988 zperror(path, B_FALSE);
981 989 /*
982 990 * Since the mkdir() succeeded, we should not have to
983 991 * worry about a subsequent ENOENT, thus this should
984 992 * only recurse once.
985 993 */
986 994 return (validate_zonepath(path, cmd_num));
987 995 }
988 996 rpath[res] = '\0';
989 997 if (strcmp(path, rpath) != 0) {
990 998 errno = Z_RESOLVED_PATH;
991 999 zperror(path, B_TRUE);
992 1000 return (Z_ERR);
993 1001 }
994 1002 if ((res = stat(rpath, &stbuf)) != 0) {
995 1003 zperror(rpath, B_FALSE);
996 1004 return (Z_ERR);
997 1005 }
998 1006 if (!S_ISDIR(stbuf.st_mode)) {
999 1007 (void) fprintf(stderr, gettext("%s is not a directory.\n"),
1000 1008 rpath);
1001 1009 return (Z_ERR);
1002 1010 }
1003 1011 if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) {
1004 1012 (void) printf(gettext("WARNING: %s is on a temporary "
1005 1013 "file system.\n"), rpath);
1006 1014 }
1007 1015 if (crosscheck_zonepaths(rpath) != Z_OK)
1008 1016 return (Z_ERR);
1009 1017 /*
1010 1018 * Try to collect and report as many minor errors as possible
1011 1019 * before returning, so the user can learn everything that needs
1012 1020 * to be fixed up front.
1013 1021 */
1014 1022 if (stbuf.st_uid != 0) {
1015 1023 (void) fprintf(stderr, gettext("%s is not owned by root.\n"),
1016 1024 rpath);
1017 1025 err = B_TRUE;
1018 1026 }
1019 1027 err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
1020 1028 err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
1021 1029 err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
1022 1030 err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
1023 1031 err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
1024 1032 err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
1025 1033 err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
1026 1034 err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
1027 1035 err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
1028 1036
1029 1037 (void) snprintf(ppath, sizeof (ppath), "%s/..", path);
1030 1038 if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
1031 1039 zperror(ppath, B_FALSE);
1032 1040 return (Z_ERR);
1033 1041 }
1034 1042 rppath[res] = '\0';
1035 1043 if ((res = stat(rppath, &stbuf)) != 0) {
1036 1044 zperror(rppath, B_FALSE);
1037 1045 return (Z_ERR);
1038 1046 }
1039 1047 /* theoretically impossible */
1040 1048 if (!S_ISDIR(stbuf.st_mode)) {
1041 1049 (void) fprintf(stderr, gettext("%s is not a directory.\n"),
1042 1050 rppath);
1043 1051 return (Z_ERR);
1044 1052 }
1045 1053 if (stbuf.st_uid != 0) {
1046 1054 (void) fprintf(stderr, gettext("%s is not owned by root.\n"),
1047 1055 rppath);
1048 1056 err = B_TRUE;
1049 1057 }
1050 1058 err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
1051 1059 err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
1052 1060 err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
1053 1061 err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
1054 1062 err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
1055 1063 if (strcmp(rpath, rppath) == 0) {
1056 1064 (void) fprintf(stderr, gettext("%s is its own parent.\n"),
1057 1065 rppath);
1058 1066 err = B_TRUE;
1059 1067 }
1060 1068
1061 1069 if (statvfs64(rpath, &vfsbuf) != 0) {
1062 1070 zperror(rpath, B_FALSE);
1063 1071 return (Z_ERR);
1064 1072 }
1065 1073 if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) {
1066 1074 /*
1067 1075 * TRANSLATION_NOTE
1068 1076 * Zonepath and NFS are literals that should not be translated.
1069 1077 */
1070 1078 (void) fprintf(stderr, gettext("Zonepath %s is on an NFS "
1071 1079 "mounted file system.\n"
1072 1080 "\tA local file system must be used.\n"), rpath);
1073 1081 return (Z_ERR);
1074 1082 }
1075 1083 if (vfsbuf.f_flag & ST_NOSUID) {
1076 1084 /*
1077 1085 * TRANSLATION_NOTE
1078 1086 * Zonepath and nosuid are literals that should not be
1079 1087 * translated.
1080 1088 */
1081 1089 (void) fprintf(stderr, gettext("Zonepath %s is on a nosuid "
1082 1090 "file system.\n"), rpath);
1083 1091 return (Z_ERR);
1084 1092 }
1085 1093
1086 1094 if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
1087 1095 errno = res;
1088 1096 zperror2(target_zone, gettext("could not get state"));
1089 1097 return (Z_ERR);
1090 1098 }
1091 1099 /*
1092 1100 * The existence of the root path is only bad in the configured state,
1093 1101 * as it is *supposed* to be there at the installed and later states.
1094 1102 * However, the root path is expected to be there if the zone is
1095 1103 * detached.
1096 1104 * State/command mismatches are caught earlier in verify_details().
1097 1105 */
1098 1106 if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) {
1099 1107 if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
1100 1108 sizeof (rootpath)) {
1101 1109 /*
1102 1110 * TRANSLATION_NOTE
1103 1111 * Zonepath is a literal that should not be translated.
1104 1112 */
1105 1113 (void) fprintf(stderr,
1106 1114 gettext("Zonepath %s is too long.\n"), rpath);
1107 1115 return (Z_ERR);
1108 1116 }
1109 1117 if ((res = stat(rootpath, &stbuf)) == 0) {
1110 1118 if (zonecfg_detached(rpath)) {
1111 1119 (void) fprintf(stderr,
1112 1120 gettext("Cannot %s detached "
1113 1121 "zone.\nUse attach or remove %s "
1114 1122 "directory.\n"), cmd_to_str(cmd_num),
1115 1123 rpath);
1116 1124 return (Z_ERR);
1117 1125 }
1118 1126
1119 1127 /* Not detached, check if it really looks ok. */
1120 1128
1121 1129 if (!S_ISDIR(stbuf.st_mode)) {
1122 1130 (void) fprintf(stderr, gettext("%s is not a "
1123 1131 "directory.\n"), rootpath);
1124 1132 return (Z_ERR);
1125 1133 }
1126 1134
1127 1135 if (stbuf.st_uid != 0) {
1128 1136 (void) fprintf(stderr, gettext("%s is not "
1129 1137 "owned by root.\n"), rootpath);
1130 1138 return (Z_ERR);
1131 1139 }
1132 1140
1133 1141 if ((stbuf.st_mode & 0777) != 0755) {
1134 1142 (void) fprintf(stderr, gettext("%s mode is not "
1135 1143 "0755.\n"), rootpath);
1136 1144 return (Z_ERR);
1137 1145 }
1138 1146 }
1139 1147 }
1140 1148
1141 1149 return (err ? Z_ERR : Z_OK);
1142 1150 }
1143 1151
1144 1152 static int
1145 1153 invoke_brand_handler(int cmd_num, char *argv[])
1146 1154 {
1147 1155 zone_dochandle_t handle;
1148 1156 int err;
1149 1157
1150 1158 if ((handle = zonecfg_init_handle()) == NULL) {
1151 1159 zperror(cmd_to_str(cmd_num), B_TRUE);
1152 1160 return (Z_ERR);
1153 1161 }
1154 1162 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
1155 1163 errno = err;
1156 1164 zperror(cmd_to_str(cmd_num), B_TRUE);
1157 1165 zonecfg_fini_handle(handle);
1158 1166 return (Z_ERR);
1159 1167 }
1160 1168 if (verify_brand(handle, cmd_num, argv) != Z_OK) {
1161 1169 zonecfg_fini_handle(handle);
1162 1170 return (Z_ERR);
1163 1171 }
1164 1172 zonecfg_fini_handle(handle);
1165 1173 return (Z_OK);
1166 1174 }
1167 1175
1168 1176 static int
1169 1177 ready_func(int argc, char *argv[])
1170 1178 {
1171 1179 zone_cmd_arg_t zarg;
1172 1180 int arg;
1173 1181
1174 1182 if (zonecfg_in_alt_root()) {
1175 1183 zerror(gettext("cannot ready zone in alternate root"));
1176 1184 return (Z_ERR);
1177 1185 }
1178 1186
1179 1187 optind = 0;
1180 1188 if ((arg = getopt(argc, argv, "?")) != EOF) {
1181 1189 switch (arg) {
1182 1190 case '?':
1183 1191 sub_usage(SHELP_READY, CMD_READY);
1184 1192 return (optopt == '?' ? Z_OK : Z_USAGE);
1185 1193 default:
1186 1194 sub_usage(SHELP_READY, CMD_READY);
1187 1195 return (Z_USAGE);
1188 1196 }
1189 1197 }
1190 1198 if (argc > optind) {
1191 1199 sub_usage(SHELP_READY, CMD_READY);
1192 1200 return (Z_USAGE);
1193 1201 }
1194 1202 if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE)
1195 1203 != Z_OK)
1196 1204 return (Z_ERR);
1197 1205 if (verify_details(CMD_READY, argv) != Z_OK)
1198 1206 return (Z_ERR);
1199 1207
1200 1208 zarg.cmd = Z_READY;
1201 1209 if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
1202 1210 zerror(gettext("call to %s failed"), "zoneadmd");
1203 1211 return (Z_ERR);
1204 1212 }
1205 1213 return (Z_OK);
1206 1214 }
1207 1215
1208 1216 static int
1209 1217 boot_func(int argc, char *argv[])
1210 1218 {
1211 1219 zone_cmd_arg_t zarg;
1212 1220 boolean_t force = B_FALSE;
1213 1221 int arg;
1214 1222
1215 1223 if (zonecfg_in_alt_root()) {
1216 1224 zerror(gettext("cannot boot zone in alternate root"));
1217 1225 return (Z_ERR);
1218 1226 }
1219 1227
1220 1228 zarg.bootbuf[0] = '\0';
1221 1229
1222 1230 /*
1223 1231 * The following getopt processes arguments to zone boot; that
1224 1232 * is to say, the [here] portion of the argument string:
1225 1233 *
1226 1234 * zoneadm -z myzone boot [here] -- -v -m verbose
1227 1235 *
1228 1236 * Where [here] can either be nothing, -? (in which case we bail
1229 1237 * and print usage), -f (a private option to indicate that the
1230 1238 * boot operation should be 'forced'), or -s. Support for -s is
1231 1239 * vestigal and obsolete, but is retained because it was a
1232 1240 * documented interface and there are known consumers including
1233 1241 * admin/install; the proper way to specify boot arguments like -s
1234 1242 * is:
1235 1243 *
1236 1244 * zoneadm -z myzone boot -- -s -v -m verbose.
1237 1245 */
1238 1246 optind = 0;
1239 1247 while ((arg = getopt(argc, argv, "?fs")) != EOF) {
1240 1248 switch (arg) {
1241 1249 case '?':
1242 1250 sub_usage(SHELP_BOOT, CMD_BOOT);
1243 1251 return (optopt == '?' ? Z_OK : Z_USAGE);
1244 1252 case 's':
1245 1253 (void) strlcpy(zarg.bootbuf, "-s",
1246 1254 sizeof (zarg.bootbuf));
1247 1255 break;
1248 1256 case 'f':
1249 1257 force = B_TRUE;
1250 1258 break;
1251 1259 default:
1252 1260 sub_usage(SHELP_BOOT, CMD_BOOT);
1253 1261 return (Z_USAGE);
1254 1262 }
1255 1263 }
1256 1264
1257 1265 for (; optind < argc; optind++) {
1258 1266 if (strlcat(zarg.bootbuf, argv[optind],
1259 1267 sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
1260 1268 zerror(gettext("Boot argument list too long"));
1261 1269 return (Z_ERR);
1262 1270 }
1263 1271 if (optind < argc - 1)
1264 1272 if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
1265 1273 sizeof (zarg.bootbuf)) {
1266 1274 zerror(gettext("Boot argument list too long"));
1267 1275 return (Z_ERR);
1268 1276 }
1269 1277 }
1270 1278 if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force)
1271 1279 != Z_OK)
1272 1280 return (Z_ERR);
1273 1281 if (verify_details(CMD_BOOT, argv) != Z_OK)
1274 1282 return (Z_ERR);
1275 1283 zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT;
1276 1284 if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
1277 1285 zerror(gettext("call to %s failed"), "zoneadmd");
1278 1286 return (Z_ERR);
1279 1287 }
1280 1288
1281 1289 return (Z_OK);
1282 1290 }
1283 1291
1284 1292 static void
1285 1293 fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
1286 1294 {
1287 1295 ssize_t result;
1288 1296 uuid_t uuid;
1289 1297 FILE *fp;
1290 1298 ushort_t flags;
1291 1299
1292 1300 (void) memset(zeptr, 0, sizeof (*zeptr));
1293 1301
1294 1302 zeptr->zid = zid;
1295 1303
1296 1304 /*
1297 1305 * Since we're looking up our own (non-global) zone name,
1298 1306 * we can be assured that it will succeed.
1299 1307 */
1300 1308 result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
1301 1309 assert(result >= 0);
1302 1310 if (zonecfg_is_scratch(zeptr->zname) &&
1303 1311 (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
1304 1312 (void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname,
1305 1313 sizeof (zeptr->zname), NULL, 0);
1306 1314 zonecfg_close_scratch(fp);
1307 1315 }
1308 1316
1309 1317 if (is_system_labeled()) {
1310 1318 (void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot,
1311 1319 sizeof (zeptr->zroot));
1312 1320 (void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME,
1313 1321 sizeof (zeptr->zbrand));
1314 1322 } else {
1315 1323 (void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
1316 1324 (void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand,
1317 1325 sizeof (zeptr->zbrand));
1318 1326 }
1319 1327
1320 1328 zeptr->zstate_str = "running";
1321 1329 if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK &&
1322 1330 !uuid_is_null(uuid))
1323 1331 uuid_unparse(uuid, zeptr->zuuid);
1324 1332
1325 1333 if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) {
1326 1334 zperror2(zeptr->zname, gettext("could not get zone flags"));
1327 1335 exit(Z_ERR);
1328 1336 }
1329 1337 if (flags & ZF_NET_EXCL)
1330 1338 zeptr->ziptype = ZS_EXCLUSIVE;
1331 1339 else
1332 1340 zeptr->ziptype = ZS_SHARED;
1333 1341 }
1334 1342
1335 1343 static int
1336 1344 list_func(int argc, char *argv[])
1337 1345 {
1338 1346 zone_entry_t *zentp, zent;
1339 1347 int arg, retv;
1340 1348 boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
1341 1349 zone_state_t min_state = ZONE_STATE_RUNNING;
1342 1350 zoneid_t zone_id = getzoneid();
1343 1351
1344 1352 if (target_zone == NULL) {
1345 1353 /* all zones: default view to running but allow override */
1346 1354 optind = 0;
1347 1355 while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
1348 1356 switch (arg) {
1349 1357 case '?':
1350 1358 sub_usage(SHELP_LIST, CMD_LIST);
1351 1359 return (optopt == '?' ? Z_OK : Z_USAGE);
1352 1360 /*
1353 1361 * The 'i' and 'c' options are not mutually
1354 1362 * exclusive so if 'c' is given, then min_state
1355 1363 * is set to 0 (ZONE_STATE_CONFIGURED) which is
1356 1364 * the lowest possible state. If 'i' is given,
1357 1365 * then min_state is set to be the lowest state
1358 1366 * so far.
1359 1367 */
1360 1368 case 'c':
1361 1369 min_state = ZONE_STATE_CONFIGURED;
1362 1370 break;
1363 1371 case 'i':
1364 1372 min_state = min(ZONE_STATE_INSTALLED,
1365 1373 min_state);
1366 1374
1367 1375 break;
1368 1376 case 'p':
1369 1377 parsable = B_TRUE;
1370 1378 break;
1371 1379 case 'v':
1372 1380 verbose = B_TRUE;
1373 1381 break;
1374 1382 default:
1375 1383 sub_usage(SHELP_LIST, CMD_LIST);
1376 1384 return (Z_USAGE);
1377 1385 }
1378 1386 }
1379 1387 if (parsable && verbose) {
1380 1388 zerror(gettext("%s -p and -v are mutually exclusive."),
1381 1389 cmd_to_str(CMD_LIST));
1382 1390 return (Z_ERR);
1383 1391 }
1384 1392 if (zone_id == GLOBAL_ZONEID || is_system_labeled()) {
1385 1393 retv = zone_print_list(min_state, verbose, parsable);
1386 1394 } else {
1387 1395 fake_up_local_zone(zone_id, &zent);
1388 1396 retv = Z_OK;
1389 1397 zone_print(&zent, verbose, parsable);
1390 1398 }
1391 1399 return (retv);
1392 1400 }
1393 1401
1394 1402 /*
1395 1403 * Specific target zone: disallow -i/-c suboptions.
1396 1404 */
1397 1405 optind = 0;
1398 1406 while ((arg = getopt(argc, argv, "?pv")) != EOF) {
1399 1407 switch (arg) {
1400 1408 case '?':
1401 1409 sub_usage(SHELP_LIST, CMD_LIST);
1402 1410 return (optopt == '?' ? Z_OK : Z_USAGE);
1403 1411 case 'p':
1404 1412 parsable = B_TRUE;
1405 1413 break;
1406 1414 case 'v':
1407 1415 verbose = B_TRUE;
1408 1416 break;
1409 1417 default:
1410 1418 sub_usage(SHELP_LIST, CMD_LIST);
1411 1419 return (Z_USAGE);
1412 1420 }
1413 1421 }
1414 1422 if (parsable && verbose) {
1415 1423 zerror(gettext("%s -p and -v are mutually exclusive."),
1416 1424 cmd_to_str(CMD_LIST));
1417 1425 return (Z_ERR);
1418 1426 }
1419 1427 if (argc > optind) {
1420 1428 sub_usage(SHELP_LIST, CMD_LIST);
1421 1429 return (Z_USAGE);
1422 1430 }
1423 1431 if (zone_id != GLOBAL_ZONEID && !is_system_labeled()) {
1424 1432 fake_up_local_zone(zone_id, &zent);
1425 1433 /*
1426 1434 * main() will issue a Z_NO_ZONE error if it cannot get an
1427 1435 * id for target_zone, which in a non-global zone should
1428 1436 * happen for any zone name except `zonename`. Thus we
1429 1437 * assert() that here but don't otherwise check.
1430 1438 */
1431 1439 assert(strcmp(zent.zname, target_zone) == 0);
1432 1440 zone_print(&zent, verbose, parsable);
1433 1441 output = B_TRUE;
1434 1442 } else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
1435 1443 zone_print(zentp, verbose, parsable);
1436 1444 output = B_TRUE;
1437 1445 } else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1438 1446 &zent) == Z_OK) {
1439 1447 zone_print(&zent, verbose, parsable);
1440 1448 output = B_TRUE;
1441 1449 }
1442 1450
1443 1451 /*
1444 1452 * Invoke brand-specific handler. Note that we do this
1445 1453 * only if we're in the global zone, and target_zone is specified
1446 1454 * and it is not the global zone.
1447 1455 */
1448 1456 if (zone_id == GLOBAL_ZONEID && target_zone != NULL &&
1449 1457 strcmp(target_zone, GLOBAL_ZONENAME) != 0)
1450 1458 if (invoke_brand_handler(CMD_LIST, argv) != Z_OK)
1451 1459 return (Z_ERR);
1452 1460
1453 1461 return (output ? Z_OK : Z_ERR);
1454 1462 }
1455 1463
1456 1464 int
1457 1465 do_subproc(char *cmdbuf)
1458 1466 {
1459 1467 void (*saveint)(int);
1460 1468 void (*saveterm)(int);
1461 1469 void (*savequit)(int);
1462 1470 void (*savehup)(int);
1463 1471 int pid, child, status;
1464 1472
1465 1473 if ((child = vfork()) == 0) {
1466 1474 (void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL);
1467 1475 }
1468 1476
1469 1477 if (child == -1)
1470 1478 return (-1);
1471 1479
1472 1480 saveint = sigset(SIGINT, SIG_IGN);
1473 1481 saveterm = sigset(SIGTERM, SIG_IGN);
1474 1482 savequit = sigset(SIGQUIT, SIG_IGN);
1475 1483 savehup = sigset(SIGHUP, SIG_IGN);
1476 1484
1477 1485 while ((pid = waitpid(child, &status, 0)) != child && pid != -1)
1478 1486 ;
1479 1487
1480 1488 (void) sigset(SIGINT, saveint);
1481 1489 (void) sigset(SIGTERM, saveterm);
1482 1490 (void) sigset(SIGQUIT, savequit);
1483 1491 (void) sigset(SIGHUP, savehup);
1484 1492
1485 1493 return (pid == -1 ? -1 : status);
1486 1494 }
1487 1495
1488 1496 int
1489 1497 subproc_status(const char *cmd, int status, boolean_t verbose_failure)
1490 1498 {
1491 1499 if (WIFEXITED(status)) {
1492 1500 int exit_code = WEXITSTATUS(status);
1493 1501
1494 1502 if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK))
1495 1503 zerror(gettext("'%s' failed with exit code %d."), cmd,
1496 1504 exit_code);
1497 1505
1498 1506 return (exit_code);
1499 1507 } else if (WIFSIGNALED(status)) {
1500 1508 int signal = WTERMSIG(status);
1501 1509 char sigstr[SIG2STR_MAX];
1502 1510
1503 1511 if (sig2str(signal, sigstr) == 0) {
1504 1512 zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
1505 1513 sigstr);
1506 1514 } else {
1507 1515 zerror(gettext("'%s' terminated by an unknown signal."),
1508 1516 cmd);
1509 1517 }
1510 1518 } else {
1511 1519 zerror(gettext("'%s' failed for unknown reasons."), cmd);
1512 1520 }
1513 1521
1514 1522 /*
1515 1523 * Assume a subprocess that died due to a signal or an unknown error
1516 1524 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the
1517 1525 * user will likely need to do some manual cleanup.
1518 1526 */
1519 1527 return (ZONE_SUBPROC_FATAL);
1520 1528 }
1521 1529
1522 1530 static int
1523 1531 auth_check(char *user, char *zone, int cmd_num)
1524 1532 {
1525 1533 char authname[MAXAUTHS];
1526 1534
↓ open down ↓ |
1282 lines elided |
↑ open up ↑ |
1527 1535 switch (cmd_num) {
1528 1536 case CMD_LIST:
1529 1537 case CMD_HELP:
1530 1538 return (Z_OK);
1531 1539 case SOURCE_ZONE:
1532 1540 (void) strlcpy(authname, ZONE_CLONEFROM_AUTH, MAXAUTHS);
1533 1541 break;
1534 1542 case CMD_BOOT:
1535 1543 case CMD_HALT:
1536 1544 case CMD_READY:
1545 + case CMD_SHUTDOWN:
1537 1546 case CMD_REBOOT:
1538 1547 case CMD_SYSBOOT:
1539 1548 case CMD_VERIFY:
1540 1549 case CMD_INSTALL:
1541 1550 case CMD_UNINSTALL:
1542 1551 case CMD_MOUNT:
1543 1552 case CMD_UNMOUNT:
1544 1553 case CMD_CLONE:
1545 1554 case CMD_MOVE:
1546 1555 case CMD_DETACH:
1547 1556 case CMD_ATTACH:
1548 1557 case CMD_MARK:
1549 1558 case CMD_APPLY:
1550 1559 default:
1551 1560 (void) strlcpy(authname, ZONE_MANAGE_AUTH, MAXAUTHS);
1552 1561 break;
1553 1562 }
1554 1563 (void) strlcat(authname, KV_OBJECT, MAXAUTHS);
1555 1564 (void) strlcat(authname, zone, MAXAUTHS);
1556 1565 if (chkauthattr(authname, user) == 0) {
1557 1566 return (Z_ERR);
1558 1567 } else {
1559 1568 /*
1560 1569 * Some subcommands, e.g. install, run subcommands,
1561 1570 * e.g. sysidcfg, that require a real uid of root,
1562 1571 * so switch to root, here.
1563 1572 */
1564 1573 if (setuid(0) == -1) {
1565 1574 zperror(gettext("insufficient privilege"), B_TRUE);
1566 1575 return (Z_ERR);
1567 1576 }
1568 1577 return (Z_OK);
1569 1578 }
1570 1579 }
1571 1580
1572 1581 /*
1573 1582 * Various sanity checks; make sure:
1574 1583 * 1. We're in the global zone.
1575 1584 * 2. The calling user has sufficient privilege.
1576 1585 * 3. The target zone is neither the global zone nor anything starting with
1577 1586 * "SUNW".
1578 1587 * 4a. If we're looking for a 'not running' (i.e., configured or installed)
1579 1588 * zone, the name service knows about it.
1580 1589 * 4b. For some operations which expect a zone not to be running, that it is
1581 1590 * not already running (or ready).
1582 1591 */
1583 1592 static int
1584 1593 sanity_check(char *zone, int cmd_num, boolean_t running,
1585 1594 boolean_t unsafe_when_running, boolean_t force)
1586 1595 {
1587 1596 zone_entry_t *zent;
1588 1597 priv_set_t *privset;
↓ open down ↓ |
42 lines elided |
↑ open up ↑ |
1589 1598 zone_state_t state, min_state;
1590 1599 char kernzone[ZONENAME_MAX];
1591 1600 FILE *fp;
1592 1601
1593 1602 if (getzoneid() != GLOBAL_ZONEID) {
1594 1603 switch (cmd_num) {
1595 1604 case CMD_HALT:
1596 1605 zerror(gettext("use %s to %s this zone."), "halt(1M)",
1597 1606 cmd_to_str(cmd_num));
1598 1607 break;
1608 + case CMD_SHUTDOWN:
1609 + zerror(gettext("use %s to %s this zone."),
1610 + "shutdown(1M)", cmd_to_str(cmd_num));
1611 + break;
1599 1612 case CMD_REBOOT:
1600 1613 zerror(gettext("use %s to %s this zone."),
1601 1614 "reboot(1M)", cmd_to_str(cmd_num));
1602 1615 break;
1603 1616 default:
1604 1617 zerror(gettext("must be in the global zone to %s a "
1605 1618 "zone."), cmd_to_str(cmd_num));
1606 1619 break;
1607 1620 }
1608 1621 return (Z_ERR);
1609 1622 }
1610 1623
1611 1624 if ((privset = priv_allocset()) == NULL) {
1612 1625 zerror(gettext("%s failed"), "priv_allocset");
1613 1626 return (Z_ERR);
1614 1627 }
1615 1628
1616 1629 if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
1617 1630 zerror(gettext("%s failed"), "getppriv");
1618 1631 priv_freeset(privset);
1619 1632 return (Z_ERR);
1620 1633 }
1621 1634
1622 1635 if (priv_isfullset(privset) == B_FALSE) {
1623 1636 zerror(gettext("only a privileged user may %s a zone."),
1624 1637 cmd_to_str(cmd_num));
1625 1638 priv_freeset(privset);
1626 1639 return (Z_ERR);
1627 1640 }
1628 1641 priv_freeset(privset);
1629 1642
1630 1643 if (zone == NULL) {
1631 1644 zerror(gettext("no zone specified"));
1632 1645 return (Z_ERR);
1633 1646 }
1634 1647
1635 1648 if (auth_check(username, zone, cmd_num) == Z_ERR) {
1636 1649 zerror(gettext("User %s is not authorized to %s this zone."),
1637 1650 username, cmd_to_str(cmd_num));
1638 1651 return (Z_ERR);
1639 1652 }
1640 1653
1641 1654 if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
1642 1655 zerror(gettext("%s operation is invalid for the global zone."),
1643 1656 cmd_to_str(cmd_num));
1644 1657 return (Z_ERR);
1645 1658 }
1646 1659
1647 1660 if (strncmp(zone, "SUNW", 4) == 0) {
1648 1661 zerror(gettext("%s operation is invalid for zones starting "
1649 1662 "with SUNW."), cmd_to_str(cmd_num));
1650 1663 return (Z_ERR);
1651 1664 }
1652 1665
1653 1666 if (!zonecfg_in_alt_root()) {
1654 1667 zent = lookup_running_zone(zone);
1655 1668 } else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1656 1669 zent = NULL;
1657 1670 } else {
1658 1671 if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1659 1672 kernzone, sizeof (kernzone)) == 0)
1660 1673 zent = lookup_running_zone(kernzone);
1661 1674 else
1662 1675 zent = NULL;
1663 1676 zonecfg_close_scratch(fp);
1664 1677 }
1665 1678
1666 1679 /*
1667 1680 * Look up from the kernel for 'running' zones.
1668 1681 */
1669 1682 if (running && !force) {
1670 1683 if (zent == NULL) {
1671 1684 zerror(gettext("not running"));
1672 1685 return (Z_ERR);
1673 1686 }
1674 1687 } else {
1675 1688 int err;
1676 1689
1677 1690 if (unsafe_when_running && zent != NULL) {
1678 1691 /* check whether the zone is ready or running */
1679 1692 if ((err = zone_get_state(zent->zname,
1680 1693 &zent->zstate_num)) != Z_OK) {
1681 1694 errno = err;
1682 1695 zperror2(zent->zname,
1683 1696 gettext("could not get state"));
1684 1697 /* can't tell, so hedge */
1685 1698 zent->zstate_str = "ready/running";
1686 1699 } else {
1687 1700 zent->zstate_str =
1688 1701 zone_state_str(zent->zstate_num);
1689 1702 }
1690 1703 zerror(gettext("%s operation is invalid for %s zones."),
1691 1704 cmd_to_str(cmd_num), zent->zstate_str);
1692 1705 return (Z_ERR);
1693 1706 }
1694 1707 if ((err = zone_get_state(zone, &state)) != Z_OK) {
1695 1708 errno = err;
1696 1709 zperror2(zone, gettext("could not get state"));
1697 1710 return (Z_ERR);
1698 1711 }
1699 1712 switch (cmd_num) {
1700 1713 case CMD_UNINSTALL:
1701 1714 if (state == ZONE_STATE_CONFIGURED) {
1702 1715 zerror(gettext("is already in state '%s'."),
1703 1716 zone_state_str(ZONE_STATE_CONFIGURED));
1704 1717 return (Z_ERR);
1705 1718 }
1706 1719 break;
1707 1720 case CMD_ATTACH:
1708 1721 if (state == ZONE_STATE_INSTALLED) {
1709 1722 zerror(gettext("is already %s."),
1710 1723 zone_state_str(ZONE_STATE_INSTALLED));
1711 1724 return (Z_ERR);
1712 1725 } else if (state == ZONE_STATE_INCOMPLETE && !force) {
1713 1726 zerror(gettext("zone is %s; %s required."),
1714 1727 zone_state_str(ZONE_STATE_INCOMPLETE),
1715 1728 cmd_to_str(CMD_UNINSTALL));
1716 1729 return (Z_ERR);
1717 1730 }
1718 1731 break;
1719 1732 case CMD_CLONE:
1720 1733 case CMD_INSTALL:
1721 1734 if (state == ZONE_STATE_INSTALLED) {
1722 1735 zerror(gettext("is already %s."),
1723 1736 zone_state_str(ZONE_STATE_INSTALLED));
1724 1737 return (Z_ERR);
1725 1738 } else if (state == ZONE_STATE_INCOMPLETE) {
1726 1739 zerror(gettext("zone is %s; %s required."),
1727 1740 zone_state_str(ZONE_STATE_INCOMPLETE),
1728 1741 cmd_to_str(CMD_UNINSTALL));
1729 1742 return (Z_ERR);
1730 1743 }
1731 1744 break;
1732 1745 case CMD_DETACH:
1733 1746 case CMD_MOVE:
1734 1747 case CMD_READY:
1735 1748 case CMD_BOOT:
1736 1749 case CMD_MOUNT:
1737 1750 case CMD_MARK:
1738 1751 if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) &&
1739 1752 force)
1740 1753 min_state = ZONE_STATE_INCOMPLETE;
1741 1754 else if (cmd_num == CMD_MARK)
1742 1755 min_state = ZONE_STATE_CONFIGURED;
1743 1756 else
1744 1757 min_state = ZONE_STATE_INSTALLED;
1745 1758
1746 1759 if (state < min_state) {
1747 1760 zerror(gettext("must be %s before %s."),
1748 1761 zone_state_str(min_state),
1749 1762 cmd_to_str(cmd_num));
1750 1763 return (Z_ERR);
1751 1764 }
1752 1765 break;
1753 1766 case CMD_VERIFY:
1754 1767 if (state == ZONE_STATE_INCOMPLETE) {
1755 1768 zerror(gettext("zone is %s; %s required."),
1756 1769 zone_state_str(ZONE_STATE_INCOMPLETE),
1757 1770 cmd_to_str(CMD_UNINSTALL));
1758 1771 return (Z_ERR);
1759 1772 }
1760 1773 break;
1761 1774 case CMD_UNMOUNT:
1762 1775 if (state != ZONE_STATE_MOUNTED) {
1763 1776 zerror(gettext("must be %s before %s."),
1764 1777 zone_state_str(ZONE_STATE_MOUNTED),
1765 1778 cmd_to_str(cmd_num));
1766 1779 return (Z_ERR);
1767 1780 }
1768 1781 break;
1769 1782 case CMD_SYSBOOT:
1770 1783 if (state != ZONE_STATE_INSTALLED) {
1771 1784 zerror(gettext("%s operation is invalid for %s "
1772 1785 "zones."), cmd_to_str(cmd_num),
1773 1786 zone_state_str(state));
1774 1787 return (Z_ERR);
1775 1788 }
1776 1789 break;
1777 1790 }
1778 1791 }
1779 1792 return (Z_OK);
1780 1793 }
1781 1794
1782 1795 static int
1783 1796 halt_func(int argc, char *argv[])
1784 1797 {
1785 1798 zone_cmd_arg_t zarg;
1786 1799 int arg;
1787 1800
1788 1801 if (zonecfg_in_alt_root()) {
1789 1802 zerror(gettext("cannot halt zone in alternate root"));
1790 1803 return (Z_ERR);
1791 1804 }
1792 1805
1793 1806 optind = 0;
1794 1807 if ((arg = getopt(argc, argv, "?")) != EOF) {
1795 1808 switch (arg) {
1796 1809 case '?':
1797 1810 sub_usage(SHELP_HALT, CMD_HALT);
1798 1811 return (optopt == '?' ? Z_OK : Z_USAGE);
1799 1812 default:
1800 1813 sub_usage(SHELP_HALT, CMD_HALT);
1801 1814 return (Z_USAGE);
1802 1815 }
1803 1816 }
1804 1817 if (argc > optind) {
1805 1818 sub_usage(SHELP_HALT, CMD_HALT);
1806 1819 return (Z_USAGE);
1807 1820 }
1808 1821 /*
1809 1822 * zoneadmd should be the one to decide whether or not to proceed,
1810 1823 * so even though it seems that the fourth parameter below should
1811 1824 * perhaps be B_TRUE, it really shouldn't be.
1812 1825 */
1813 1826 if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE)
1814 1827 != Z_OK)
1815 1828 return (Z_ERR);
1816 1829
1817 1830 /*
↓ open down ↓ |
209 lines elided |
↑ open up ↑ |
1818 1831 * Invoke brand-specific handler.
1819 1832 */
1820 1833 if (invoke_brand_handler(CMD_HALT, argv) != Z_OK)
1821 1834 return (Z_ERR);
1822 1835
1823 1836 zarg.cmd = Z_HALT;
1824 1837 return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale,
1825 1838 B_TRUE) == 0) ? Z_OK : Z_ERR);
1826 1839 }
1827 1840
1841 +static int
1842 +shutdown_func(int argc, char *argv[])
1843 +{
1844 + zone_cmd_arg_t zarg;
1845 + int arg;
1846 + boolean_t reboot = B_FALSE;
1847 +
1848 + zarg.cmd = Z_SHUTDOWN;
1849 +
1850 + if (zonecfg_in_alt_root()) {
1851 + zerror(gettext("cannot shut down zone in alternate root"));
1852 + return (Z_ERR);
1853 + }
1854 +
1855 + optind = 0;
1856 + while ((arg = getopt(argc, argv, "?r")) != EOF) {
1857 + switch (arg) {
1858 + case '?':
1859 + sub_usage(SHELP_SHUTDOWN, CMD_SHUTDOWN);
1860 + return (optopt == '?' ? Z_OK : Z_USAGE);
1861 + case 'r':
1862 + reboot = B_TRUE;
1863 + break;
1864 + default:
1865 + sub_usage(SHELP_SHUTDOWN, CMD_SHUTDOWN);
1866 + return (Z_USAGE);
1867 + }
1868 + }
1869 +
1870 + zarg.bootbuf[0] = '\0';
1871 + for (; optind < argc; optind++) {
1872 + if (strlcat(zarg.bootbuf, argv[optind],
1873 + sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
1874 + zerror(gettext("Boot argument list too long"));
1875 + return (Z_ERR);
1876 + }
1877 + if (optind < argc - 1)
1878 + if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
1879 + sizeof (zarg.bootbuf)) {
1880 + zerror(gettext("Boot argument list too long"));
1881 + return (Z_ERR);
1882 + }
1883 + }
1884 +
1885 + /*
1886 + * zoneadmd should be the one to decide whether or not to proceed,
1887 + * so even though it seems that the third parameter below should
1888 + * perhaps be B_TRUE, it really shouldn't be.
1889 + */
1890 + if (sanity_check(target_zone, CMD_SHUTDOWN, B_TRUE, B_FALSE, B_FALSE)
1891 + != Z_OK)
1892 + return (Z_ERR);
1893 +
1894 + if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != Z_OK)
1895 + return (Z_ERR);
1896 +
1897 + if (reboot) {
1898 + if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE,
1899 + B_FALSE) != Z_OK)
1900 + return (Z_ERR);
1901 +
1902 + zarg.cmd = Z_BOOT;
1903 + if (zonecfg_call_zoneadmd(target_zone, &zarg, locale,
1904 + B_TRUE) != Z_OK)
1905 + return (Z_ERR);
1906 + }
1907 + return (Z_OK);
1908 +}
1909 +
1828 1910 static int
1829 1911 reboot_func(int argc, char *argv[])
1830 1912 {
1831 1913 zone_cmd_arg_t zarg;
1832 1914 int arg;
1833 1915
1834 1916 if (zonecfg_in_alt_root()) {
1835 1917 zerror(gettext("cannot reboot zone in alternate root"));
1836 1918 return (Z_ERR);
1837 1919 }
1838 1920
1839 1921 optind = 0;
1840 1922 if ((arg = getopt(argc, argv, "?")) != EOF) {
1841 1923 switch (arg) {
1842 1924 case '?':
1843 1925 sub_usage(SHELP_REBOOT, CMD_REBOOT);
1844 1926 return (optopt == '?' ? Z_OK : Z_USAGE);
1845 1927 default:
1846 1928 sub_usage(SHELP_REBOOT, CMD_REBOOT);
1847 1929 return (Z_USAGE);
1848 1930 }
1849 1931 }
1850 1932
1851 1933 zarg.bootbuf[0] = '\0';
1852 1934 for (; optind < argc; optind++) {
1853 1935 if (strlcat(zarg.bootbuf, argv[optind],
1854 1936 sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
1855 1937 zerror(gettext("Boot argument list too long"));
1856 1938 return (Z_ERR);
1857 1939 }
1858 1940 if (optind < argc - 1)
1859 1941 if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
1860 1942 sizeof (zarg.bootbuf)) {
1861 1943 zerror(gettext("Boot argument list too long"));
1862 1944 return (Z_ERR);
1863 1945 }
1864 1946 }
1865 1947
1866 1948
1867 1949 /*
1868 1950 * zoneadmd should be the one to decide whether or not to proceed,
1869 1951 * so even though it seems that the fourth parameter below should
1870 1952 * perhaps be B_TRUE, it really shouldn't be.
1871 1953 */
1872 1954 if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE)
1873 1955 != Z_OK)
1874 1956 return (Z_ERR);
1875 1957 if (verify_details(CMD_REBOOT, argv) != Z_OK)
1876 1958 return (Z_ERR);
1877 1959
1878 1960 zarg.cmd = Z_REBOOT;
1879 1961 return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) == 0)
1880 1962 ? Z_OK : Z_ERR);
1881 1963 }
1882 1964
1883 1965 static int
1884 1966 get_hook(brand_handle_t bh, char *cmd, size_t len, int (*bp)(brand_handle_t,
1885 1967 const char *, const char *, char *, size_t), char *zonename, char *zonepath)
1886 1968 {
1887 1969 if (strlcpy(cmd, EXEC_PREFIX, len) >= len)
1888 1970 return (Z_ERR);
1889 1971
1890 1972 if (bp(bh, zonename, zonepath, cmd + EXEC_LEN, len - EXEC_LEN) != 0)
1891 1973 return (Z_ERR);
1892 1974
1893 1975 if (strlen(cmd) <= EXEC_LEN)
1894 1976 cmd[0] = '\0';
1895 1977
1896 1978 return (Z_OK);
1897 1979 }
1898 1980
1899 1981 static int
1900 1982 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[])
1901 1983 {
1902 1984 char cmdbuf[MAXPATHLEN];
1903 1985 int err;
1904 1986 char zonepath[MAXPATHLEN];
1905 1987 brand_handle_t bh = NULL;
1906 1988 int status, i;
1907 1989
1908 1990 /*
1909 1991 * Fetch the verify command from the brand configuration.
1910 1992 * "exec" the command so that the returned status is that of
1911 1993 * the command and not the shell.
1912 1994 */
1913 1995 if (handle == NULL) {
1914 1996 (void) strlcpy(zonepath, "-", sizeof (zonepath));
1915 1997 } else if ((err = zonecfg_get_zonepath(handle, zonepath,
1916 1998 sizeof (zonepath))) != Z_OK) {
1917 1999 errno = err;
1918 2000 zperror(cmd_to_str(cmd_num), B_TRUE);
1919 2001 return (Z_ERR);
1920 2002 }
1921 2003 if ((bh = brand_open(target_brand)) == NULL) {
1922 2004 zerror(gettext("missing or invalid brand"));
1923 2005 return (Z_ERR);
1924 2006 }
1925 2007
1926 2008 /*
1927 2009 * If the brand has its own verification routine, execute it now.
1928 2010 * The verification routine validates the intended zoneadm
1929 2011 * operation for the specific brand. The zoneadm subcommand and
1930 2012 * all its arguments are passed to the routine.
1931 2013 */
1932 2014 err = get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_verify_adm,
1933 2015 target_zone, zonepath);
1934 2016 brand_close(bh);
1935 2017 if (err != Z_OK)
1936 2018 return (Z_BRAND_ERROR);
1937 2019 if (cmdbuf[0] == '\0')
1938 2020 return (Z_OK);
1939 2021
1940 2022 if (strlcat(cmdbuf, cmd_to_str(cmd_num),
1941 2023 sizeof (cmdbuf)) >= sizeof (cmdbuf))
1942 2024 return (Z_ERR);
1943 2025
1944 2026 /* Build the argv string */
1945 2027 i = 0;
1946 2028 while (argv[i] != NULL) {
1947 2029 if ((strlcat(cmdbuf, " ",
1948 2030 sizeof (cmdbuf)) >= sizeof (cmdbuf)) ||
1949 2031 (strlcat(cmdbuf, argv[i++],
1950 2032 sizeof (cmdbuf)) >= sizeof (cmdbuf)))
1951 2033 return (Z_ERR);
1952 2034 }
1953 2035
1954 2036 status = do_subproc(cmdbuf);
1955 2037 err = subproc_status(gettext("brand-specific verification"),
1956 2038 status, B_FALSE);
1957 2039
1958 2040 return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR);
1959 2041 }
1960 2042
1961 2043 static int
1962 2044 verify_rctls(zone_dochandle_t handle)
1963 2045 {
1964 2046 struct zone_rctltab rctltab;
1965 2047 size_t rbs = rctlblk_size();
1966 2048 rctlblk_t *rctlblk;
1967 2049 int error = Z_INVAL;
1968 2050
1969 2051 if ((rctlblk = malloc(rbs)) == NULL) {
1970 2052 zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
1971 2053 strerror(errno));
1972 2054 return (Z_NOMEM);
1973 2055 }
1974 2056
1975 2057 if (zonecfg_setrctlent(handle) != Z_OK) {
1976 2058 zerror(gettext("zonecfg_setrctlent failed"));
1977 2059 free(rctlblk);
1978 2060 return (error);
1979 2061 }
1980 2062
1981 2063 rctltab.zone_rctl_valptr = NULL;
1982 2064 while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
1983 2065 struct zone_rctlvaltab *rctlval;
1984 2066 const char *name = rctltab.zone_rctl_name;
1985 2067
1986 2068 if (!zonecfg_is_rctl(name)) {
1987 2069 zerror(gettext("WARNING: Ignoring unrecognized rctl "
1988 2070 "'%s'."), name);
1989 2071 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
1990 2072 rctltab.zone_rctl_valptr = NULL;
1991 2073 continue;
1992 2074 }
1993 2075
1994 2076 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
1995 2077 rctlval = rctlval->zone_rctlval_next) {
1996 2078 if (zonecfg_construct_rctlblk(rctlval, rctlblk)
1997 2079 != Z_OK) {
1998 2080 zerror(gettext("invalid rctl value: "
1999 2081 "(priv=%s,limit=%s,action%s)"),
2000 2082 rctlval->zone_rctlval_priv,
2001 2083 rctlval->zone_rctlval_limit,
2002 2084 rctlval->zone_rctlval_action);
2003 2085 goto out;
2004 2086 }
2005 2087 if (!zonecfg_valid_rctl(name, rctlblk)) {
2006 2088 zerror(gettext("(priv=%s,limit=%s,action=%s) "
2007 2089 "is not a valid value for rctl '%s'"),
2008 2090 rctlval->zone_rctlval_priv,
2009 2091 rctlval->zone_rctlval_limit,
2010 2092 rctlval->zone_rctlval_action,
2011 2093 name);
2012 2094 goto out;
2013 2095 }
2014 2096 }
2015 2097 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
2016 2098 }
2017 2099 rctltab.zone_rctl_valptr = NULL;
2018 2100 error = Z_OK;
2019 2101 out:
2020 2102 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
2021 2103 (void) zonecfg_endrctlent(handle);
2022 2104 free(rctlblk);
2023 2105 return (error);
2024 2106 }
2025 2107
2026 2108 static int
2027 2109 verify_pool(zone_dochandle_t handle)
2028 2110 {
2029 2111 char poolname[MAXPATHLEN];
2030 2112 pool_conf_t *poolconf;
2031 2113 pool_t *pool;
2032 2114 int status;
2033 2115 int error;
2034 2116
2035 2117 /*
2036 2118 * This ends up being very similar to the check done in zoneadmd.
2037 2119 */
2038 2120 error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
2039 2121 if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
2040 2122 /*
2041 2123 * No pool specified.
2042 2124 */
2043 2125 return (0);
2044 2126 }
2045 2127 if (error != Z_OK) {
2046 2128 zperror(gettext("Unable to retrieve pool name from "
2047 2129 "configuration"), B_TRUE);
2048 2130 return (error);
2049 2131 }
2050 2132 /*
2051 2133 * Don't do anything if pools aren't enabled.
2052 2134 */
2053 2135 if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
2054 2136 zerror(gettext("WARNING: pools facility not active; "
2055 2137 "zone will not be bound to pool '%s'."), poolname);
2056 2138 return (Z_OK);
2057 2139 }
2058 2140 /*
2059 2141 * Try to provide a sane error message if the requested pool doesn't
2060 2142 * exist. It isn't clear that pools-related failures should
2061 2143 * necessarily translate to a failure to verify the zone configuration,
2062 2144 * hence they are not considered errors.
2063 2145 */
2064 2146 if ((poolconf = pool_conf_alloc()) == NULL) {
2065 2147 zerror(gettext("WARNING: pool_conf_alloc failed; "
2066 2148 "using default pool"));
2067 2149 return (Z_OK);
2068 2150 }
2069 2151 if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
2070 2152 PO_SUCCESS) {
2071 2153 zerror(gettext("WARNING: pool_conf_open failed; "
2072 2154 "using default pool"));
2073 2155 pool_conf_free(poolconf);
2074 2156 return (Z_OK);
2075 2157 }
2076 2158 pool = pool_get_pool(poolconf, poolname);
2077 2159 (void) pool_conf_close(poolconf);
2078 2160 pool_conf_free(poolconf);
2079 2161 if (pool == NULL) {
2080 2162 zerror(gettext("WARNING: pool '%s' not found. "
2081 2163 "using default pool"), poolname);
2082 2164 }
2083 2165
2084 2166 return (Z_OK);
2085 2167 }
2086 2168
2087 2169 /*
2088 2170 * Verify that the special device/file system exists and is valid.
2089 2171 */
2090 2172 static int
2091 2173 verify_fs_special(struct zone_fstab *fstab)
2092 2174 {
2093 2175 struct stat64 st;
2094 2176
2095 2177 /*
2096 2178 * This validation is really intended for standard zone administration.
2097 2179 * If we are in a mini-root or some other upgrade situation where
2098 2180 * we are using the scratch zone, just by-pass this.
2099 2181 */
2100 2182 if (zonecfg_in_alt_root())
2101 2183 return (Z_OK);
2102 2184
2103 2185 if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0)
2104 2186 return (verify_fs_zfs(fstab));
2105 2187
2106 2188 if (stat64(fstab->zone_fs_special, &st) != 0) {
2107 2189 (void) fprintf(stderr, gettext("could not verify fs "
2108 2190 "%s: could not access %s: %s\n"), fstab->zone_fs_dir,
2109 2191 fstab->zone_fs_special, strerror(errno));
2110 2192 return (Z_ERR);
2111 2193 }
2112 2194
2113 2195 if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
2114 2196 /*
2115 2197 * TRANSLATION_NOTE
2116 2198 * fs and NFS are literals that should
2117 2199 * not be translated.
2118 2200 */
2119 2201 (void) fprintf(stderr, gettext("cannot verify "
2120 2202 "fs %s: NFS mounted file system.\n"
2121 2203 "\tA local file system must be used.\n"),
2122 2204 fstab->zone_fs_special);
2123 2205 return (Z_ERR);
2124 2206 }
2125 2207
2126 2208 return (Z_OK);
2127 2209 }
2128 2210
2129 2211 static int
2130 2212 isregfile(const char *path)
2131 2213 {
2132 2214 struct stat64 st;
2133 2215
2134 2216 if (stat64(path, &st) == -1)
2135 2217 return (-1);
2136 2218
2137 2219 return (S_ISREG(st.st_mode));
2138 2220 }
2139 2221
2140 2222 static int
2141 2223 verify_filesystems(zone_dochandle_t handle)
2142 2224 {
2143 2225 int return_code = Z_OK;
2144 2226 struct zone_fstab fstab;
2145 2227 char cmdbuf[MAXPATHLEN];
2146 2228 struct stat st;
2147 2229
2148 2230 /*
2149 2231 * Since the actual mount point is not known until the dependent mounts
2150 2232 * are performed, we don't attempt any path validation here: that will
2151 2233 * happen later when zoneadmd actually does the mounts.
2152 2234 */
2153 2235 if (zonecfg_setfsent(handle) != Z_OK) {
2154 2236 (void) fprintf(stderr, gettext("could not verify file systems: "
2155 2237 "unable to enumerate mounts\n"));
2156 2238 return (Z_ERR);
2157 2239 }
2158 2240 while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
2159 2241 if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
2160 2242 (void) fprintf(stderr, gettext("cannot verify fs %s: "
2161 2243 "type %s is not allowed.\n"), fstab.zone_fs_dir,
2162 2244 fstab.zone_fs_type);
2163 2245 return_code = Z_ERR;
2164 2246 goto next_fs;
2165 2247 }
2166 2248 /*
2167 2249 * Verify /usr/lib/fs/<fstype>/mount exists.
2168 2250 */
2169 2251 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
2170 2252 fstab.zone_fs_type) > sizeof (cmdbuf)) {
2171 2253 (void) fprintf(stderr, gettext("cannot verify fs %s: "
2172 2254 "type %s is too long.\n"), fstab.zone_fs_dir,
2173 2255 fstab.zone_fs_type);
2174 2256 return_code = Z_ERR;
2175 2257 goto next_fs;
2176 2258 }
2177 2259 if (stat(cmdbuf, &st) != 0) {
2178 2260 (void) fprintf(stderr, gettext("could not verify fs "
2179 2261 "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2180 2262 cmdbuf, strerror(errno));
2181 2263 return_code = Z_ERR;
2182 2264 goto next_fs;
2183 2265 }
2184 2266 if (!S_ISREG(st.st_mode)) {
2185 2267 (void) fprintf(stderr, gettext("could not verify fs "
2186 2268 "%s: %s is not a regular file\n"),
2187 2269 fstab.zone_fs_dir, cmdbuf);
2188 2270 return_code = Z_ERR;
2189 2271 goto next_fs;
2190 2272 }
2191 2273 /*
2192 2274 * If zone_fs_raw is set, verify that there's an fsck
2193 2275 * binary for it. If zone_fs_raw is not set, and it's
2194 2276 * not a regular file (lofi mount), and there's an fsck
2195 2277 * binary for it, complain.
2196 2278 */
2197 2279 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
2198 2280 fstab.zone_fs_type) > sizeof (cmdbuf)) {
2199 2281 (void) fprintf(stderr, gettext("cannot verify fs %s: "
2200 2282 "type %s is too long.\n"), fstab.zone_fs_dir,
2201 2283 fstab.zone_fs_type);
2202 2284 return_code = Z_ERR;
2203 2285 goto next_fs;
2204 2286 }
2205 2287 if (fstab.zone_fs_raw[0] != '\0' &&
2206 2288 (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
2207 2289 (void) fprintf(stderr, gettext("cannot verify fs %s: "
2208 2290 "'raw' device specified but "
2209 2291 "no fsck executable exists for %s\n"),
2210 2292 fstab.zone_fs_dir, fstab.zone_fs_type);
2211 2293 return_code = Z_ERR;
2212 2294 goto next_fs;
2213 2295 } else if (fstab.zone_fs_raw[0] == '\0' &&
2214 2296 stat(cmdbuf, &st) == 0 &&
2215 2297 isregfile(fstab.zone_fs_special) != 1) {
2216 2298 (void) fprintf(stderr, gettext("could not verify fs "
2217 2299 "%s: must specify 'raw' device for %s "
2218 2300 "file systems\n"),
2219 2301 fstab.zone_fs_dir, fstab.zone_fs_type);
2220 2302 return_code = Z_ERR;
2221 2303 goto next_fs;
2222 2304 }
2223 2305
2224 2306 /* Verify fs_special. */
2225 2307 if ((return_code = verify_fs_special(&fstab)) != Z_OK)
2226 2308 goto next_fs;
2227 2309
2228 2310 /* Verify fs_raw. */
2229 2311 if (fstab.zone_fs_raw[0] != '\0' &&
2230 2312 stat(fstab.zone_fs_raw, &st) != 0) {
2231 2313 /*
2232 2314 * TRANSLATION_NOTE
2233 2315 * fs is a literal that should not be translated.
2234 2316 */
2235 2317 (void) fprintf(stderr, gettext("could not verify fs "
2236 2318 "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2237 2319 fstab.zone_fs_raw, strerror(errno));
2238 2320 return_code = Z_ERR;
2239 2321 goto next_fs;
2240 2322 }
2241 2323 next_fs:
2242 2324 zonecfg_free_fs_option_list(fstab.zone_fs_options);
2243 2325 }
2244 2326 (void) zonecfg_endfsent(handle);
2245 2327
2246 2328 return (return_code);
2247 2329 }
2248 2330
2249 2331 static int
2250 2332 verify_limitpriv(zone_dochandle_t handle)
2251 2333 {
2252 2334 char *privname = NULL;
2253 2335 int err;
2254 2336 priv_set_t *privs;
2255 2337
2256 2338 if ((privs = priv_allocset()) == NULL) {
2257 2339 zperror(gettext("failed to allocate privilege set"), B_FALSE);
2258 2340 return (Z_NOMEM);
2259 2341 }
2260 2342 err = zonecfg_get_privset(handle, privs, &privname);
2261 2343 switch (err) {
2262 2344 case Z_OK:
2263 2345 break;
2264 2346 case Z_PRIV_PROHIBITED:
2265 2347 (void) fprintf(stderr, gettext("privilege \"%s\" is not "
2266 2348 "permitted within the zone's privilege set\n"), privname);
2267 2349 break;
2268 2350 case Z_PRIV_REQUIRED:
2269 2351 (void) fprintf(stderr, gettext("required privilege \"%s\" is "
2270 2352 "missing from the zone's privilege set\n"), privname);
2271 2353 break;
2272 2354 case Z_PRIV_UNKNOWN:
2273 2355 (void) fprintf(stderr, gettext("unknown privilege \"%s\" "
2274 2356 "specified in the zone's privilege set\n"), privname);
2275 2357 break;
2276 2358 default:
2277 2359 zperror(
2278 2360 gettext("failed to determine the zone's privilege set"),
2279 2361 B_TRUE);
2280 2362 break;
2281 2363 }
2282 2364 free(privname);
2283 2365 priv_freeset(privs);
2284 2366 return (err);
2285 2367 }
2286 2368
2287 2369 static void
2288 2370 free_local_netifs(int if_cnt, struct net_if **if_list)
2289 2371 {
2290 2372 int i;
2291 2373
2292 2374 for (i = 0; i < if_cnt; i++) {
2293 2375 free(if_list[i]->name);
2294 2376 free(if_list[i]);
2295 2377 }
2296 2378 free(if_list);
2297 2379 }
2298 2380
2299 2381 /*
2300 2382 * Get a list of the network interfaces, along with their address families,
2301 2383 * that are plumbed in the global zone. See if_tcp(7p) for a description
2302 2384 * of the ioctls used here.
2303 2385 */
2304 2386 static int
2305 2387 get_local_netifs(int *if_cnt, struct net_if ***if_list)
2306 2388 {
2307 2389 int s;
2308 2390 int i;
2309 2391 int res = Z_OK;
2310 2392 int space_needed;
2311 2393 int cnt = 0;
2312 2394 struct lifnum if_num;
2313 2395 struct lifconf if_conf;
2314 2396 struct lifreq *if_reqp;
2315 2397 char *if_buf;
2316 2398 struct net_if **local_ifs = NULL;
2317 2399
2318 2400 *if_cnt = 0;
2319 2401 *if_list = NULL;
2320 2402
2321 2403 if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0)
2322 2404 return (Z_ERR);
2323 2405
2324 2406 /*
2325 2407 * Come back here in the unlikely event that the number of interfaces
2326 2408 * increases between the time we get the count and the time we do the
2327 2409 * SIOCGLIFCONF ioctl.
2328 2410 */
2329 2411 retry:
2330 2412 /* Get the number of interfaces. */
2331 2413 if_num.lifn_family = AF_UNSPEC;
2332 2414 if_num.lifn_flags = LIFC_NOXMIT;
2333 2415 if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) {
2334 2416 (void) close(s);
2335 2417 return (Z_ERR);
2336 2418 }
2337 2419
2338 2420 /* Get the interface configuration list. */
2339 2421 space_needed = if_num.lifn_count * sizeof (struct lifreq);
2340 2422 if ((if_buf = malloc(space_needed)) == NULL) {
2341 2423 (void) close(s);
2342 2424 return (Z_ERR);
2343 2425 }
2344 2426 if_conf.lifc_family = AF_UNSPEC;
2345 2427 if_conf.lifc_flags = LIFC_NOXMIT;
2346 2428 if_conf.lifc_len = space_needed;
2347 2429 if_conf.lifc_buf = if_buf;
2348 2430 if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) {
2349 2431 free(if_buf);
2350 2432 /*
2351 2433 * SIOCGLIFCONF returns EINVAL if the buffer we passed in is
2352 2434 * too small. In this case go back and get the new if cnt.
2353 2435 */
2354 2436 if (errno == EINVAL)
2355 2437 goto retry;
2356 2438
2357 2439 (void) close(s);
2358 2440 return (Z_ERR);
2359 2441 }
2360 2442 (void) close(s);
2361 2443
2362 2444 /* Get the name and address family for each interface. */
2363 2445 if_reqp = if_conf.lifc_req;
2364 2446 for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) {
2365 2447 struct net_if **p;
2366 2448 struct lifreq req;
2367 2449
2368 2450 if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) {
2369 2451 if_reqp++;
2370 2452 continue;
2371 2453 }
2372 2454
2373 2455 if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family),
2374 2456 SOCK_DGRAM, 0)) == -1) {
2375 2457 res = Z_ERR;
2376 2458 break;
2377 2459 }
2378 2460
2379 2461 (void) strncpy(req.lifr_name, if_reqp->lifr_name,
2380 2462 sizeof (req.lifr_name));
2381 2463 if (ioctl(s, SIOCGLIFADDR, &req) < 0) {
2382 2464 (void) close(s);
2383 2465 if_reqp++;
2384 2466 continue;
2385 2467 }
2386 2468
2387 2469 if ((p = (struct net_if **)realloc(local_ifs,
2388 2470 sizeof (struct net_if *) * (cnt + 1))) == NULL) {
2389 2471 res = Z_ERR;
2390 2472 break;
2391 2473 }
2392 2474 local_ifs = p;
2393 2475
2394 2476 if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) {
2395 2477 res = Z_ERR;
2396 2478 break;
2397 2479 }
2398 2480
2399 2481 if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name))
2400 2482 == NULL) {
2401 2483 free(local_ifs[cnt]);
2402 2484 res = Z_ERR;
2403 2485 break;
2404 2486 }
2405 2487 local_ifs[cnt]->af = req.lifr_addr.ss_family;
2406 2488 cnt++;
2407 2489
2408 2490 (void) close(s);
2409 2491 if_reqp++;
2410 2492 }
2411 2493
2412 2494 free(if_buf);
2413 2495
2414 2496 if (res != Z_OK) {
2415 2497 free_local_netifs(cnt, local_ifs);
2416 2498 } else {
2417 2499 *if_cnt = cnt;
2418 2500 *if_list = local_ifs;
2419 2501 }
2420 2502
2421 2503 return (res);
2422 2504 }
2423 2505
2424 2506 static char *
2425 2507 af2str(int af)
2426 2508 {
2427 2509 switch (af) {
2428 2510 case AF_INET:
2429 2511 return ("IPv4");
2430 2512 case AF_INET6:
2431 2513 return ("IPv6");
2432 2514 default:
2433 2515 return ("Unknown");
2434 2516 }
2435 2517 }
2436 2518
2437 2519 /*
2438 2520 * Cross check the network interface name and address family with the
2439 2521 * interfaces that are set up in the global zone so that we can print the
2440 2522 * appropriate error message.
2441 2523 */
2442 2524 static void
2443 2525 print_net_err(char *phys, char *addr, int af, char *msg)
2444 2526 {
2445 2527 int i;
2446 2528 int local_if_cnt = 0;
2447 2529 struct net_if **local_ifs = NULL;
2448 2530 boolean_t found_if = B_FALSE;
2449 2531 boolean_t found_af = B_FALSE;
2450 2532
2451 2533 if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) {
2452 2534 (void) fprintf(stderr,
2453 2535 gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
2454 2536 "net", "address", addr, "physical", phys, msg);
2455 2537 return;
2456 2538 }
2457 2539
2458 2540 for (i = 0; i < local_if_cnt; i++) {
2459 2541 if (strcmp(phys, local_ifs[i]->name) == 0) {
2460 2542 found_if = B_TRUE;
2461 2543 if (af == local_ifs[i]->af) {
2462 2544 found_af = B_TRUE;
2463 2545 break;
2464 2546 }
2465 2547 }
2466 2548 }
2467 2549
2468 2550 free_local_netifs(local_if_cnt, local_ifs);
2469 2551
2470 2552 if (!found_if) {
2471 2553 (void) fprintf(stderr,
2472 2554 gettext("could not verify %s %s=%s\n\t"
2473 2555 "network interface %s is not plumbed in the global zone\n"),
2474 2556 "net", "physical", phys, phys);
2475 2557 return;
2476 2558 }
2477 2559
2478 2560 /*
2479 2561 * Print this error if we were unable to find the address family
2480 2562 * for this interface. If the af variable is not initialized to
2481 2563 * to something meaningful by the caller (not AF_UNSPEC) then we
2482 2564 * also skip this message since it wouldn't be informative.
2483 2565 */
2484 2566 if (!found_af && af != AF_UNSPEC) {
2485 2567 (void) fprintf(stderr,
2486 2568 gettext("could not verify %s %s=%s %s=%s\n\tthe %s address "
2487 2569 "family is not configured on this network interface in "
2488 2570 "the\n\tglobal zone\n"),
2489 2571 "net", "address", addr, "physical", phys, af2str(af));
2490 2572 return;
2491 2573 }
2492 2574
2493 2575 (void) fprintf(stderr,
2494 2576 gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
2495 2577 "net", "address", addr, "physical", phys, msg);
2496 2578 }
2497 2579
2498 2580 static int
2499 2581 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[])
2500 2582 {
2501 2583 struct zone_nwiftab nwiftab;
2502 2584 int return_code = Z_OK;
2503 2585 int err;
2504 2586 boolean_t in_alt_root;
2505 2587 zone_iptype_t iptype;
2506 2588 dladm_handle_t dh;
2507 2589 dladm_status_t status;
2508 2590 datalink_id_t linkid;
2509 2591 char errmsg[DLADM_STRSIZE];
2510 2592
2511 2593 in_alt_root = zonecfg_in_alt_root();
2512 2594 if (in_alt_root)
2513 2595 goto no_net;
2514 2596
2515 2597 if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) {
2516 2598 errno = err;
2517 2599 zperror(cmd_to_str(cmd_num), B_TRUE);
2518 2600 zonecfg_fini_handle(handle);
2519 2601 return (Z_ERR);
2520 2602 }
2521 2603 if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
2522 2604 errno = err;
2523 2605 zperror(cmd_to_str(cmd_num), B_TRUE);
2524 2606 zonecfg_fini_handle(handle);
2525 2607 return (Z_ERR);
2526 2608 }
2527 2609 while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
2528 2610 struct lifreq lifr;
2529 2611 sa_family_t af = AF_UNSPEC;
2530 2612 char dl_owner_zname[ZONENAME_MAX];
2531 2613 zoneid_t dl_owner_zid;
2532 2614 zoneid_t target_zid;
2533 2615 int res;
2534 2616
2535 2617 /* skip any loopback interfaces */
2536 2618 if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
2537 2619 continue;
2538 2620 switch (iptype) {
2539 2621 case ZS_SHARED:
2540 2622 if ((res = zonecfg_valid_net_address(
2541 2623 nwiftab.zone_nwif_address, &lifr)) != Z_OK) {
2542 2624 print_net_err(nwiftab.zone_nwif_physical,
2543 2625 nwiftab.zone_nwif_address, af,
2544 2626 zonecfg_strerror(res));
2545 2627 return_code = Z_ERR;
2546 2628 continue;
2547 2629 }
2548 2630 af = lifr.lifr_addr.ss_family;
2549 2631 if (!zonecfg_ifname_exists(af,
2550 2632 nwiftab.zone_nwif_physical)) {
2551 2633 /*
2552 2634 * The interface failed to come up. We continue
2553 2635 * on anyway for the sake of consistency: a
2554 2636 * zone is not shut down if the interface fails
2555 2637 * any time after boot, nor does the global zone
2556 2638 * fail to boot if an interface fails.
2557 2639 */
2558 2640 (void) fprintf(stderr,
2559 2641 gettext("WARNING: skipping network "
2560 2642 "interface '%s' which may not be "
2561 2643 "present/plumbed in the global "
2562 2644 "zone.\n"),
2563 2645 nwiftab.zone_nwif_physical);
2564 2646 }
2565 2647 break;
2566 2648 case ZS_EXCLUSIVE:
2567 2649 /* Warning if it exists for either IPv4 or IPv6 */
2568 2650
2569 2651 if (zonecfg_ifname_exists(AF_INET,
2570 2652 nwiftab.zone_nwif_physical) ||
2571 2653 zonecfg_ifname_exists(AF_INET6,
2572 2654 nwiftab.zone_nwif_physical)) {
2573 2655 (void) fprintf(stderr,
2574 2656 gettext("WARNING: skipping network "
2575 2657 "interface '%s' which is used in the "
2576 2658 "global zone.\n"),
2577 2659 nwiftab.zone_nwif_physical);
2578 2660 break;
2579 2661 }
2580 2662
2581 2663 /*
2582 2664 * Verify that the datalink exists and that it isn't
2583 2665 * already assigned to a zone.
2584 2666 */
2585 2667 if ((status = dladm_open(&dh)) == DLADM_STATUS_OK) {
2586 2668 status = dladm_name2info(dh,
2587 2669 nwiftab.zone_nwif_physical, &linkid, NULL,
2588 2670 NULL, NULL);
2589 2671 dladm_close(dh);
2590 2672 }
2591 2673 if (status != DLADM_STATUS_OK) {
2592 2674 (void) fprintf(stderr,
2593 2675 gettext("WARNING: skipping network "
2594 2676 "interface '%s': %s\n"),
2595 2677 nwiftab.zone_nwif_physical,
2596 2678 dladm_status2str(status, errmsg));
2597 2679 break;
2598 2680 }
2599 2681 dl_owner_zid = ALL_ZONES;
2600 2682 if (zone_check_datalink(&dl_owner_zid, linkid) != 0)
2601 2683 break;
2602 2684
2603 2685 /*
2604 2686 * If the zone being verified is
2605 2687 * running and owns the interface
2606 2688 */
2607 2689 target_zid = getzoneidbyname(target_zone);
2608 2690 if (target_zid == dl_owner_zid)
2609 2691 break;
2610 2692
2611 2693 /* Zone id match failed, use name to check */
2612 2694 if (getzonenamebyid(dl_owner_zid, dl_owner_zname,
2613 2695 ZONENAME_MAX) < 0) {
2614 2696 /* No name, show ID instead */
2615 2697 (void) snprintf(dl_owner_zname, ZONENAME_MAX,
2616 2698 "<%d>", dl_owner_zid);
2617 2699 } else if (strcmp(dl_owner_zname, target_zone) == 0)
2618 2700 break;
2619 2701
2620 2702 /*
2621 2703 * Note here we only report a warning that
2622 2704 * the interface is already in use by another
2623 2705 * running zone, and the verify process just
2624 2706 * goes on, if the interface is still in use
2625 2707 * when this zone really boots up, zoneadmd
2626 2708 * will find it. If the name of the zone which
2627 2709 * owns this interface cannot be determined,
2628 2710 * then it is not possible to determine if there
2629 2711 * is a conflict so just report it as a warning.
2630 2712 */
2631 2713 (void) fprintf(stderr,
2632 2714 gettext("WARNING: skipping network interface "
2633 2715 "'%s' which is used by the non-global zone "
2634 2716 "'%s'.\n"), nwiftab.zone_nwif_physical,
2635 2717 dl_owner_zname);
2636 2718 break;
2637 2719 }
2638 2720 }
2639 2721 (void) zonecfg_endnwifent(handle);
2640 2722 no_net:
2641 2723
2642 2724 /* verify that lofs has not been excluded from the kernel */
2643 2725 if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH ||
2644 2726 cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) &&
2645 2727 modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) {
2646 2728 if (errno == ENXIO)
2647 2729 (void) fprintf(stderr, gettext("could not verify "
2648 2730 "lofs(7FS): possibly excluded in /etc/system\n"));
2649 2731 else
2650 2732 (void) fprintf(stderr, gettext("could not verify "
2651 2733 "lofs(7FS): %s\n"), strerror(errno));
2652 2734 return_code = Z_ERR;
2653 2735 }
2654 2736
2655 2737 if (verify_filesystems(handle) != Z_OK)
2656 2738 return_code = Z_ERR;
2657 2739 if (!in_alt_root && verify_rctls(handle) != Z_OK)
2658 2740 return_code = Z_ERR;
2659 2741 if (!in_alt_root && verify_pool(handle) != Z_OK)
2660 2742 return_code = Z_ERR;
2661 2743 if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK)
2662 2744 return_code = Z_ERR;
2663 2745 if (!in_alt_root && verify_datasets(handle) != Z_OK)
2664 2746 return_code = Z_ERR;
2665 2747
2666 2748 /*
2667 2749 * As the "mount" command is used for patching/upgrading of zones
2668 2750 * or other maintenance processes, the zone's privilege set is not
2669 2751 * checked in this case. Instead, the default, safe set of
2670 2752 * privileges will be used when this zone is created in the
2671 2753 * kernel.
2672 2754 */
2673 2755 if (!in_alt_root && cmd_num != CMD_MOUNT &&
2674 2756 verify_limitpriv(handle) != Z_OK)
2675 2757 return_code = Z_ERR;
2676 2758
2677 2759 return (return_code);
2678 2760 }
2679 2761
2680 2762 static int
2681 2763 verify_details(int cmd_num, char *argv[])
2682 2764 {
2683 2765 zone_dochandle_t handle;
2684 2766 char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
2685 2767 int return_code = Z_OK;
2686 2768 int err;
2687 2769
2688 2770 if ((handle = zonecfg_init_handle()) == NULL) {
2689 2771 zperror(cmd_to_str(cmd_num), B_TRUE);
2690 2772 return (Z_ERR);
2691 2773 }
2692 2774 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
2693 2775 errno = err;
2694 2776 zperror(cmd_to_str(cmd_num), B_TRUE);
2695 2777 zonecfg_fini_handle(handle);
2696 2778 return (Z_ERR);
2697 2779 }
2698 2780 if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
2699 2781 Z_OK) {
2700 2782 errno = err;
2701 2783 zperror(cmd_to_str(cmd_num), B_TRUE);
2702 2784 zonecfg_fini_handle(handle);
2703 2785 return (Z_ERR);
2704 2786 }
2705 2787 /*
2706 2788 * zonecfg_get_zonepath() gets its data from the XML repository.
2707 2789 * Verify this against the index file, which is checked first by
2708 2790 * zone_get_zonepath(). If they don't match, bail out.
2709 2791 */
2710 2792 if ((err = zone_get_zonepath(target_zone, checkpath,
2711 2793 sizeof (checkpath))) != Z_OK) {
2712 2794 errno = err;
2713 2795 zperror2(target_zone, gettext("could not get zone path"));
2714 2796 zonecfg_fini_handle(handle);
2715 2797 return (Z_ERR);
2716 2798 }
2717 2799 if (strcmp(zonepath, checkpath) != 0) {
2718 2800 /*
2719 2801 * TRANSLATION_NOTE
2720 2802 * XML and zonepath are literals that should not be translated.
2721 2803 */
2722 2804 (void) fprintf(stderr, gettext("The XML repository has "
2723 2805 "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
2724 2806 "These must match, so fix the incorrect entry.\n"),
2725 2807 zonepath, checkpath);
2726 2808 zonecfg_fini_handle(handle);
2727 2809 return (Z_ERR);
2728 2810 }
2729 2811 if (cmd_num != CMD_ATTACH &&
2730 2812 validate_zonepath(zonepath, cmd_num) != Z_OK) {
2731 2813 (void) fprintf(stderr, gettext("could not verify zonepath %s "
2732 2814 "because of the above errors.\n"), zonepath);
2733 2815 return_code = Z_ERR;
2734 2816 }
2735 2817
2736 2818 if (verify_handle(cmd_num, handle, argv) != Z_OK)
2737 2819 return_code = Z_ERR;
2738 2820
2739 2821 zonecfg_fini_handle(handle);
2740 2822 if (return_code == Z_ERR)
2741 2823 (void) fprintf(stderr,
2742 2824 gettext("%s: zone %s failed to verify\n"),
2743 2825 execname, target_zone);
2744 2826 return (return_code);
2745 2827 }
2746 2828
2747 2829 static int
2748 2830 verify_func(int argc, char *argv[])
2749 2831 {
2750 2832 int arg;
2751 2833
2752 2834 optind = 0;
2753 2835 if ((arg = getopt(argc, argv, "?")) != EOF) {
2754 2836 switch (arg) {
2755 2837 case '?':
2756 2838 sub_usage(SHELP_VERIFY, CMD_VERIFY);
2757 2839 return (optopt == '?' ? Z_OK : Z_USAGE);
2758 2840 default:
2759 2841 sub_usage(SHELP_VERIFY, CMD_VERIFY);
2760 2842 return (Z_USAGE);
2761 2843 }
2762 2844 }
2763 2845 if (argc > optind) {
2764 2846 sub_usage(SHELP_VERIFY, CMD_VERIFY);
2765 2847 return (Z_USAGE);
2766 2848 }
2767 2849 if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE)
2768 2850 != Z_OK)
2769 2851 return (Z_ERR);
2770 2852 return (verify_details(CMD_VERIFY, argv));
2771 2853 }
2772 2854
2773 2855 static int
2774 2856 addoptions(char *buf, char *argv[], size_t len)
2775 2857 {
2776 2858 int i = 0;
2777 2859
2778 2860 if (buf[0] == '\0')
2779 2861 return (Z_OK);
2780 2862
2781 2863 while (argv[i] != NULL) {
2782 2864 if (strlcat(buf, " ", len) >= len ||
2783 2865 strlcat(buf, argv[i++], len) >= len) {
2784 2866 zerror("Command line too long");
2785 2867 return (Z_ERR);
2786 2868 }
2787 2869 }
2788 2870
2789 2871 return (Z_OK);
2790 2872 }
2791 2873
2792 2874 static int
2793 2875 addopt(char *buf, int opt, char *optarg, size_t bufsize)
2794 2876 {
2795 2877 char optstring[4];
2796 2878
2797 2879 if (opt > 0)
2798 2880 (void) sprintf(optstring, " -%c", opt);
2799 2881 else
2800 2882 (void) strcpy(optstring, " ");
2801 2883
2802 2884 if ((strlcat(buf, optstring, bufsize) > bufsize))
2803 2885 return (Z_ERR);
2804 2886
2805 2887 if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize))
2806 2888 return (Z_ERR);
2807 2889
2808 2890 return (Z_OK);
2809 2891 }
2810 2892
2811 2893 /* ARGSUSED */
2812 2894 static int
2813 2895 install_func(int argc, char *argv[])
2814 2896 {
2815 2897 char cmdbuf[MAXPATHLEN];
2816 2898 char postcmdbuf[MAXPATHLEN];
2817 2899 int lockfd;
2818 2900 int arg, err, subproc_err;
2819 2901 char zonepath[MAXPATHLEN];
2820 2902 brand_handle_t bh = NULL;
2821 2903 int status;
2822 2904 boolean_t do_postinstall = B_FALSE;
2823 2905 boolean_t brand_help = B_FALSE;
2824 2906 char opts[128];
2825 2907
2826 2908 if (target_zone == NULL) {
2827 2909 sub_usage(SHELP_INSTALL, CMD_INSTALL);
2828 2910 return (Z_USAGE);
2829 2911 }
2830 2912
2831 2913 if (zonecfg_in_alt_root()) {
2832 2914 zerror(gettext("cannot install zone in alternate root"));
2833 2915 return (Z_ERR);
2834 2916 }
2835 2917
2836 2918 if ((err = zone_get_zonepath(target_zone, zonepath,
2837 2919 sizeof (zonepath))) != Z_OK) {
2838 2920 errno = err;
2839 2921 zperror2(target_zone, gettext("could not get zone path"));
2840 2922 return (Z_ERR);
2841 2923 }
2842 2924
2843 2925 /* Fetch the install command from the brand configuration. */
2844 2926 if ((bh = brand_open(target_brand)) == NULL) {
2845 2927 zerror(gettext("missing or invalid brand"));
2846 2928 return (Z_ERR);
2847 2929 }
2848 2930
2849 2931 if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_install,
2850 2932 target_zone, zonepath) != Z_OK) {
2851 2933 zerror("invalid brand configuration: missing install resource");
2852 2934 brand_close(bh);
2853 2935 return (Z_ERR);
2854 2936 }
2855 2937
2856 2938 if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postinstall,
2857 2939 target_zone, zonepath) != Z_OK) {
2858 2940 zerror("invalid brand configuration: missing postinstall "
2859 2941 "resource");
2860 2942 brand_close(bh);
2861 2943 return (Z_ERR);
2862 2944 }
2863 2945
2864 2946 if (postcmdbuf[0] != '\0')
2865 2947 do_postinstall = B_TRUE;
2866 2948
2867 2949 (void) strcpy(opts, "?x:");
2868 2950 /*
2869 2951 * Fetch the list of recognized command-line options from
2870 2952 * the brand configuration file.
2871 2953 */
2872 2954 if (brand_get_installopts(bh, opts + strlen(opts),
2873 2955 sizeof (opts) - strlen(opts)) != 0) {
2874 2956 zerror("invalid brand configuration: missing "
2875 2957 "install options resource");
2876 2958 brand_close(bh);
2877 2959 return (Z_ERR);
2878 2960 }
2879 2961
2880 2962 brand_close(bh);
2881 2963
2882 2964 if (cmdbuf[0] == '\0') {
2883 2965 zerror("Missing brand install command");
2884 2966 return (Z_ERR);
2885 2967 }
2886 2968
2887 2969 /* Check the argv string for args we handle internally */
2888 2970 optind = 0;
2889 2971 opterr = 0;
2890 2972 while ((arg = getopt(argc, argv, opts)) != EOF) {
2891 2973 switch (arg) {
2892 2974 case '?':
2893 2975 if (optopt == '?') {
2894 2976 sub_usage(SHELP_INSTALL, CMD_INSTALL);
2895 2977 brand_help = B_TRUE;
2896 2978 }
2897 2979 /* Ignore unknown options - may be brand specific. */
2898 2980 break;
2899 2981 default:
2900 2982 /* Ignore unknown options - may be brand specific. */
2901 2983 break;
2902 2984 }
2903 2985
2904 2986 /*
2905 2987 * Append the option to the command line passed to the
2906 2988 * brand-specific install and postinstall routines.
2907 2989 */
2908 2990 if (addopt(cmdbuf, optopt, optarg, sizeof (cmdbuf)) != Z_OK) {
2909 2991 zerror("Install command line too long");
2910 2992 return (Z_ERR);
2911 2993 }
2912 2994 if (addopt(postcmdbuf, optopt, optarg, sizeof (postcmdbuf))
2913 2995 != Z_OK) {
2914 2996 zerror("Post-Install command line too long");
2915 2997 return (Z_ERR);
2916 2998 }
2917 2999 }
2918 3000
2919 3001 for (; optind < argc; optind++) {
2920 3002 if (addopt(cmdbuf, 0, argv[optind], sizeof (cmdbuf)) != Z_OK) {
2921 3003 zerror("Install command line too long");
2922 3004 return (Z_ERR);
2923 3005 }
2924 3006
2925 3007 if (addopt(postcmdbuf, 0, argv[optind], sizeof (postcmdbuf))
2926 3008 != Z_OK) {
2927 3009 zerror("Post-Install command line too long");
2928 3010 return (Z_ERR);
2929 3011 }
2930 3012 }
2931 3013
2932 3014 if (!brand_help) {
2933 3015 if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE,
2934 3016 B_FALSE) != Z_OK)
2935 3017 return (Z_ERR);
2936 3018 if (verify_details(CMD_INSTALL, argv) != Z_OK)
2937 3019 return (Z_ERR);
2938 3020
2939 3021 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
2940 3022 zerror(gettext("another %s may have an operation in "
2941 3023 "progress."), "zoneadm");
2942 3024 return (Z_ERR);
2943 3025 }
2944 3026 err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
2945 3027 if (err != Z_OK) {
2946 3028 errno = err;
2947 3029 zperror2(target_zone, gettext("could not set state"));
2948 3030 goto done;
2949 3031 }
2950 3032
2951 3033 create_zfs_zonepath(zonepath);
2952 3034 }
2953 3035
2954 3036 status = do_subproc(cmdbuf);
2955 3037 if ((subproc_err =
2956 3038 subproc_status(gettext("brand-specific installation"), status,
2957 3039 B_FALSE)) != ZONE_SUBPROC_OK) {
2958 3040 if (subproc_err == ZONE_SUBPROC_USAGE && !brand_help) {
2959 3041 sub_usage(SHELP_INSTALL, CMD_INSTALL);
2960 3042 zonecfg_release_lock_file(target_zone, lockfd);
2961 3043 return (Z_ERR);
2962 3044 }
2963 3045 errno = subproc_err;
2964 3046 err = Z_ERR;
2965 3047 goto done;
2966 3048 }
2967 3049
2968 3050 if (brand_help)
2969 3051 return (Z_OK);
2970 3052
2971 3053 if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
2972 3054 errno = err;
2973 3055 zperror2(target_zone, gettext("could not set state"));
2974 3056 goto done;
2975 3057 }
2976 3058
2977 3059 if (do_postinstall) {
2978 3060 status = do_subproc(postcmdbuf);
2979 3061
2980 3062 if ((subproc_err =
2981 3063 subproc_status(gettext("brand-specific post-install"),
2982 3064 status, B_FALSE)) != ZONE_SUBPROC_OK) {
2983 3065 errno = subproc_err;
2984 3066 err = Z_ERR;
2985 3067 (void) zone_set_state(target_zone,
2986 3068 ZONE_STATE_INCOMPLETE);
2987 3069 }
2988 3070 }
2989 3071
2990 3072 done:
2991 3073 /*
2992 3074 * If the install script exited with ZONE_SUBPROC_NOTCOMPLETE, try to
2993 3075 * clean up the zone and leave the zone in the CONFIGURED state so that
2994 3076 * another install can be attempted without requiring an uninstall
2995 3077 * first.
2996 3078 */
2997 3079 if (subproc_err == ZONE_SUBPROC_NOTCOMPLETE) {
2998 3080 int temp_err;
2999 3081
3000 3082 if ((temp_err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
3001 3083 errno = err = temp_err;
3002 3084 zperror2(target_zone,
3003 3085 gettext("cleaning up zonepath failed"));
3004 3086 } else if ((temp_err = zone_set_state(target_zone,
3005 3087 ZONE_STATE_CONFIGURED)) != Z_OK) {
3006 3088 errno = err = temp_err;
3007 3089 zperror2(target_zone, gettext("could not set state"));
3008 3090 }
3009 3091 }
3010 3092
3011 3093 if (!brand_help)
3012 3094 zonecfg_release_lock_file(target_zone, lockfd);
3013 3095 return ((err == Z_OK) ? Z_OK : Z_ERR);
3014 3096 }
3015 3097
3016 3098 static void
3017 3099 warn_dev_match(zone_dochandle_t s_handle, char *source_zone,
3018 3100 zone_dochandle_t t_handle, char *target_zone)
3019 3101 {
3020 3102 int err;
3021 3103 struct zone_devtab s_devtab;
3022 3104 struct zone_devtab t_devtab;
3023 3105
3024 3106 if ((err = zonecfg_setdevent(t_handle)) != Z_OK) {
3025 3107 errno = err;
3026 3108 zperror2(target_zone, gettext("could not enumerate devices"));
3027 3109 return;
3028 3110 }
3029 3111
3030 3112 while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) {
3031 3113 if ((err = zonecfg_setdevent(s_handle)) != Z_OK) {
3032 3114 errno = err;
3033 3115 zperror2(source_zone,
3034 3116 gettext("could not enumerate devices"));
3035 3117 (void) zonecfg_enddevent(t_handle);
3036 3118 return;
3037 3119 }
3038 3120
3039 3121 while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) {
3040 3122 /*
3041 3123 * Use fnmatch to catch the case where wildcards
3042 3124 * were used in one zone and the other has an
3043 3125 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
3044 3126 * /dev/\*dsk/c0t0d0s6).
3045 3127 */
3046 3128 if (fnmatch(t_devtab.zone_dev_match,
3047 3129 s_devtab.zone_dev_match, FNM_PATHNAME) == 0 ||
3048 3130 fnmatch(s_devtab.zone_dev_match,
3049 3131 t_devtab.zone_dev_match, FNM_PATHNAME) == 0) {
3050 3132 (void) fprintf(stderr,
3051 3133 gettext("WARNING: device '%s' "
3052 3134 "is configured in both zones.\n"),
3053 3135 t_devtab.zone_dev_match);
3054 3136 break;
3055 3137 }
3056 3138 }
3057 3139 (void) zonecfg_enddevent(s_handle);
3058 3140 }
3059 3141
3060 3142 (void) zonecfg_enddevent(t_handle);
3061 3143 }
3062 3144
3063 3145 /*
3064 3146 * Check if the specified mount option (opt) is contained within the
3065 3147 * options string.
3066 3148 */
3067 3149 static boolean_t
3068 3150 opt_match(char *opt, char *options)
3069 3151 {
3070 3152 char *p;
3071 3153 char *lastp;
3072 3154
3073 3155 if ((p = strtok_r(options, ",", &lastp)) != NULL) {
3074 3156 if (strcmp(p, opt) == 0)
3075 3157 return (B_TRUE);
3076 3158 while ((p = strtok_r(NULL, ",", &lastp)) != NULL) {
3077 3159 if (strcmp(p, opt) == 0)
3078 3160 return (B_TRUE);
3079 3161 }
3080 3162 }
3081 3163
3082 3164 return (B_FALSE);
3083 3165 }
3084 3166
3085 3167 #define RW_LOFS "WARNING: read-write lofs file system on '%s' is configured " \
3086 3168 "in both zones.\n"
3087 3169
3088 3170 static void
3089 3171 print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab)
3090 3172 {
3091 3173 /*
3092 3174 * It is ok to have shared lofs mounted fs but we want to warn if
3093 3175 * either is rw since this will effect the other zone.
3094 3176 */
3095 3177 if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) {
3096 3178 zone_fsopt_t *optp;
3097 3179
3098 3180 /* The default is rw so no options means rw */
3099 3181 if (t_fstab->zone_fs_options == NULL ||
3100 3182 s_fstab->zone_fs_options == NULL) {
3101 3183 (void) fprintf(stderr, gettext(RW_LOFS),
3102 3184 t_fstab->zone_fs_special);
3103 3185 return;
3104 3186 }
3105 3187
3106 3188 for (optp = s_fstab->zone_fs_options; optp != NULL;
3107 3189 optp = optp->zone_fsopt_next) {
3108 3190 if (opt_match("rw", optp->zone_fsopt_opt)) {
3109 3191 (void) fprintf(stderr, gettext(RW_LOFS),
3110 3192 s_fstab->zone_fs_special);
3111 3193 return;
3112 3194 }
3113 3195 }
3114 3196
3115 3197 for (optp = t_fstab->zone_fs_options; optp != NULL;
3116 3198 optp = optp->zone_fsopt_next) {
3117 3199 if (opt_match("rw", optp->zone_fsopt_opt)) {
3118 3200 (void) fprintf(stderr, gettext(RW_LOFS),
3119 3201 t_fstab->zone_fs_special);
3120 3202 return;
3121 3203 }
3122 3204 }
3123 3205
3124 3206 return;
3125 3207 }
3126 3208
3127 3209 /*
3128 3210 * TRANSLATION_NOTE
3129 3211 * The first variable is the file system type and the second is
3130 3212 * the file system special device. For example,
3131 3213 * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ...
3132 3214 */
3133 3215 (void) fprintf(stderr, gettext("WARNING: %s file system on '%s' "
3134 3216 "is configured in both zones.\n"), t_fstab->zone_fs_type,
3135 3217 t_fstab->zone_fs_special);
3136 3218 }
3137 3219
3138 3220 static void
3139 3221 warn_fs_match(zone_dochandle_t s_handle, char *source_zone,
3140 3222 zone_dochandle_t t_handle, char *target_zone)
3141 3223 {
3142 3224 int err;
3143 3225 struct zone_fstab s_fstab;
3144 3226 struct zone_fstab t_fstab;
3145 3227
3146 3228 if ((err = zonecfg_setfsent(t_handle)) != Z_OK) {
3147 3229 errno = err;
3148 3230 zperror2(target_zone,
3149 3231 gettext("could not enumerate file systems"));
3150 3232 return;
3151 3233 }
3152 3234
3153 3235 while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) {
3154 3236 if ((err = zonecfg_setfsent(s_handle)) != Z_OK) {
3155 3237 errno = err;
3156 3238 zperror2(source_zone,
3157 3239 gettext("could not enumerate file systems"));
3158 3240 (void) zonecfg_endfsent(t_handle);
3159 3241 return;
3160 3242 }
3161 3243
3162 3244 while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) {
3163 3245 if (strcmp(t_fstab.zone_fs_special,
3164 3246 s_fstab.zone_fs_special) == 0) {
3165 3247 print_fs_warnings(&s_fstab, &t_fstab);
3166 3248 break;
3167 3249 }
3168 3250 }
3169 3251 (void) zonecfg_endfsent(s_handle);
3170 3252 }
3171 3253
3172 3254 (void) zonecfg_endfsent(t_handle);
3173 3255 }
3174 3256
3175 3257 /*
3176 3258 * We don't catch the case where you used the same IP address but
3177 3259 * it is not an exact string match. For example, 192.9.0.128 vs. 192.09.0.128.
3178 3260 * However, we're not going to worry about that but we will check for
3179 3261 * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
3180 3262 * and handle that case as a match.
3181 3263 */
3182 3264 static void
3183 3265 warn_ip_match(zone_dochandle_t s_handle, char *source_zone,
3184 3266 zone_dochandle_t t_handle, char *target_zone)
3185 3267 {
3186 3268 int err;
3187 3269 struct zone_nwiftab s_nwiftab;
3188 3270 struct zone_nwiftab t_nwiftab;
3189 3271
3190 3272 if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) {
3191 3273 errno = err;
3192 3274 zperror2(target_zone,
3193 3275 gettext("could not enumerate network interfaces"));
3194 3276 return;
3195 3277 }
3196 3278
3197 3279 while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) {
3198 3280 char *p;
3199 3281
3200 3282 /* remove an (optional) netmask from the address */
3201 3283 if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL)
3202 3284 *p = '\0';
3203 3285
3204 3286 if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) {
3205 3287 errno = err;
3206 3288 zperror2(source_zone,
3207 3289 gettext("could not enumerate network interfaces"));
3208 3290 (void) zonecfg_endnwifent(t_handle);
3209 3291 return;
3210 3292 }
3211 3293
3212 3294 while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) {
3213 3295 /* remove an (optional) netmask from the address */
3214 3296 if ((p = strchr(s_nwiftab.zone_nwif_address, '/'))
3215 3297 != NULL)
3216 3298 *p = '\0';
3217 3299
3218 3300 /* For exclusive-IP zones, address is not specified. */
3219 3301 if (strlen(s_nwiftab.zone_nwif_address) == 0)
3220 3302 continue;
3221 3303
3222 3304 if (strcmp(t_nwiftab.zone_nwif_address,
3223 3305 s_nwiftab.zone_nwif_address) == 0) {
3224 3306 (void) fprintf(stderr,
3225 3307 gettext("WARNING: network address '%s' "
3226 3308 "is configured in both zones.\n"),
3227 3309 t_nwiftab.zone_nwif_address);
3228 3310 break;
3229 3311 }
3230 3312 }
3231 3313 (void) zonecfg_endnwifent(s_handle);
3232 3314 }
3233 3315
3234 3316 (void) zonecfg_endnwifent(t_handle);
3235 3317 }
3236 3318
3237 3319 static void
3238 3320 warn_dataset_match(zone_dochandle_t s_handle, char *source,
3239 3321 zone_dochandle_t t_handle, char *target)
3240 3322 {
3241 3323 int err;
3242 3324 struct zone_dstab s_dstab;
3243 3325 struct zone_dstab t_dstab;
3244 3326
3245 3327 if ((err = zonecfg_setdsent(t_handle)) != Z_OK) {
3246 3328 errno = err;
3247 3329 zperror2(target, gettext("could not enumerate datasets"));
3248 3330 return;
3249 3331 }
3250 3332
3251 3333 while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) {
3252 3334 if ((err = zonecfg_setdsent(s_handle)) != Z_OK) {
3253 3335 errno = err;
3254 3336 zperror2(source,
3255 3337 gettext("could not enumerate datasets"));
3256 3338 (void) zonecfg_enddsent(t_handle);
3257 3339 return;
3258 3340 }
3259 3341
3260 3342 while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) {
3261 3343 if (strcmp(t_dstab.zone_dataset_name,
3262 3344 s_dstab.zone_dataset_name) == 0) {
3263 3345 target_zone = source;
3264 3346 zerror(gettext("WARNING: dataset '%s' "
3265 3347 "is configured in both zones.\n"),
3266 3348 t_dstab.zone_dataset_name);
3267 3349 break;
3268 3350 }
3269 3351 }
3270 3352 (void) zonecfg_enddsent(s_handle);
3271 3353 }
3272 3354
3273 3355 (void) zonecfg_enddsent(t_handle);
3274 3356 }
3275 3357
3276 3358 /*
3277 3359 * Check that the clone and its source have the same brand type.
3278 3360 */
3279 3361 static int
3280 3362 valid_brand_clone(char *source_zone, char *target_zone)
3281 3363 {
3282 3364 brand_handle_t bh;
3283 3365 char source_brand[MAXNAMELEN];
3284 3366
3285 3367 if ((zone_get_brand(source_zone, source_brand,
3286 3368 sizeof (source_brand))) != Z_OK) {
3287 3369 (void) fprintf(stderr, "%s: zone '%s': %s\n",
3288 3370 execname, source_zone, gettext("missing or invalid brand"));
3289 3371 return (Z_ERR);
3290 3372 }
3291 3373
3292 3374 if (strcmp(source_brand, target_brand) != 0) {
3293 3375 (void) fprintf(stderr,
3294 3376 gettext("%s: Zones '%s' and '%s' have different brand "
3295 3377 "types.\n"), execname, source_zone, target_zone);
3296 3378 return (Z_ERR);
3297 3379 }
3298 3380
3299 3381 if ((bh = brand_open(target_brand)) == NULL) {
3300 3382 zerror(gettext("missing or invalid brand"));
3301 3383 return (Z_ERR);
3302 3384 }
3303 3385 brand_close(bh);
3304 3386 return (Z_OK);
3305 3387 }
3306 3388
3307 3389 static int
3308 3390 validate_clone(char *source_zone, char *target_zone)
3309 3391 {
3310 3392 int err = Z_OK;
3311 3393 zone_dochandle_t s_handle;
3312 3394 zone_dochandle_t t_handle;
3313 3395
3314 3396 if ((t_handle = zonecfg_init_handle()) == NULL) {
3315 3397 zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3316 3398 return (Z_ERR);
3317 3399 }
3318 3400 if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) {
3319 3401 errno = err;
3320 3402 zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3321 3403 zonecfg_fini_handle(t_handle);
3322 3404 return (Z_ERR);
3323 3405 }
3324 3406
3325 3407 if ((s_handle = zonecfg_init_handle()) == NULL) {
3326 3408 zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3327 3409 zonecfg_fini_handle(t_handle);
3328 3410 return (Z_ERR);
3329 3411 }
3330 3412 if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) {
3331 3413 errno = err;
3332 3414 zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3333 3415 goto done;
3334 3416 }
3335 3417
3336 3418 /* verify new zone has same brand type */
3337 3419 err = valid_brand_clone(source_zone, target_zone);
3338 3420 if (err != Z_OK)
3339 3421 goto done;
3340 3422
3341 3423 /* warn about imported fs's which are the same */
3342 3424 warn_fs_match(s_handle, source_zone, t_handle, target_zone);
3343 3425
3344 3426 /* warn about imported IP addresses which are the same */
3345 3427 warn_ip_match(s_handle, source_zone, t_handle, target_zone);
3346 3428
3347 3429 /* warn about imported devices which are the same */
3348 3430 warn_dev_match(s_handle, source_zone, t_handle, target_zone);
3349 3431
3350 3432 /* warn about imported datasets which are the same */
3351 3433 warn_dataset_match(s_handle, source_zone, t_handle, target_zone);
3352 3434
3353 3435 done:
3354 3436 zonecfg_fini_handle(t_handle);
3355 3437 zonecfg_fini_handle(s_handle);
3356 3438
3357 3439 return ((err == Z_OK) ? Z_OK : Z_ERR);
3358 3440 }
3359 3441
3360 3442 static int
3361 3443 copy_zone(char *src, char *dst)
3362 3444 {
3363 3445 boolean_t out_null = B_FALSE;
3364 3446 int status;
3365 3447 char *outfile;
3366 3448 char cmdbuf[MAXPATHLEN * 2 + 128];
3367 3449
3368 3450 if ((outfile = tempnam("/var/log", "zone")) == NULL) {
3369 3451 outfile = "/dev/null";
3370 3452 out_null = B_TRUE;
3371 3453 }
3372 3454
3373 3455 /*
3374 3456 * Use find to get the list of files to copy. We need to skip
3375 3457 * files of type "socket" since cpio can't handle those but that
3376 3458 * should be ok since the app will recreate the socket when it runs.
3377 3459 * We also need to filter out anything under the .zfs subdir. Since
3378 3460 * find is running depth-first, we need the extra egrep to filter .zfs.
3379 3461 */
3380 3462 (void) snprintf(cmdbuf, sizeof (cmdbuf),
3381 3463 "cd %s && /usr/bin/find . -type s -prune -o -depth -print | "
3382 3464 "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
3383 3465 "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
3384 3466 src, dst, outfile);
3385 3467
3386 3468 status = do_subproc(cmdbuf);
3387 3469
3388 3470 if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) {
3389 3471 if (!out_null)
3390 3472 (void) fprintf(stderr, gettext("\nThe copy failed.\n"
3391 3473 "More information can be found in %s\n"), outfile);
3392 3474 return (Z_ERR);
3393 3475 }
3394 3476
3395 3477 if (!out_null)
3396 3478 (void) unlink(outfile);
3397 3479
3398 3480 return (Z_OK);
3399 3481 }
3400 3482
3401 3483 /* ARGSUSED */
3402 3484 int
3403 3485 zfm_print(const struct mnttab *p, void *r) {
3404 3486 zerror(" %s\n", p->mnt_mountp);
3405 3487 return (0);
3406 3488 }
3407 3489
3408 3490 int
3409 3491 clone_copy(char *source_zonepath, char *zonepath)
3410 3492 {
3411 3493 int err;
3412 3494
3413 3495 /* Don't clone the zone if anything is still mounted there */
3414 3496 if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) {
3415 3497 zerror(gettext("These file systems are mounted on "
3416 3498 "subdirectories of %s.\n"), source_zonepath);
3417 3499 (void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL);
3418 3500 return (Z_ERR);
3419 3501 }
3420 3502
3421 3503 /*
3422 3504 * Attempt to create a ZFS fs for the zonepath. As usual, we don't
3423 3505 * care if this works or not since we always have the default behavior
3424 3506 * of a simple directory for the zonepath.
3425 3507 */
3426 3508 create_zfs_zonepath(zonepath);
3427 3509
3428 3510 (void) printf(gettext("Copying %s..."), source_zonepath);
3429 3511 (void) fflush(stdout);
3430 3512
3431 3513 err = copy_zone(source_zonepath, zonepath);
3432 3514
3433 3515 (void) printf("\n");
3434 3516
3435 3517 return (err);
3436 3518 }
3437 3519
3438 3520 static int
3439 3521 clone_func(int argc, char *argv[])
3440 3522 {
3441 3523 char *source_zone = NULL;
3442 3524 int lockfd;
3443 3525 int err, arg;
3444 3526 char zonepath[MAXPATHLEN];
3445 3527 char source_zonepath[MAXPATHLEN];
3446 3528 zone_state_t state;
3447 3529 zone_entry_t *zent;
3448 3530 char *method = NULL;
3449 3531 char *snapshot = NULL;
3450 3532 char cmdbuf[MAXPATHLEN];
3451 3533 char postcmdbuf[MAXPATHLEN];
3452 3534 char presnapbuf[MAXPATHLEN];
3453 3535 char postsnapbuf[MAXPATHLEN];
3454 3536 char validsnapbuf[MAXPATHLEN];
3455 3537 brand_handle_t bh = NULL;
3456 3538 int status;
3457 3539 boolean_t brand_help = B_FALSE;
3458 3540
3459 3541 if (zonecfg_in_alt_root()) {
3460 3542 zerror(gettext("cannot clone zone in alternate root"));
3461 3543 return (Z_ERR);
3462 3544 }
3463 3545
3464 3546 /* Check the argv string for args we handle internally */
3465 3547 optind = 0;
3466 3548 opterr = 0;
3467 3549 while ((arg = getopt(argc, argv, "?m:s:")) != EOF) {
3468 3550 switch (arg) {
3469 3551 case '?':
3470 3552 if (optopt == '?') {
3471 3553 sub_usage(SHELP_CLONE, CMD_CLONE);
3472 3554 brand_help = B_TRUE;
3473 3555 }
3474 3556 /* Ignore unknown options - may be brand specific. */
3475 3557 break;
3476 3558 case 'm':
3477 3559 method = optarg;
3478 3560 break;
3479 3561 case 's':
3480 3562 snapshot = optarg;
3481 3563 break;
3482 3564 default:
3483 3565 /* Ignore unknown options - may be brand specific. */
3484 3566 break;
3485 3567 }
3486 3568 }
3487 3569
3488 3570 if (argc != (optind + 1)) {
3489 3571 sub_usage(SHELP_CLONE, CMD_CLONE);
3490 3572 return (Z_USAGE);
3491 3573 }
3492 3574
3493 3575 source_zone = argv[optind];
3494 3576
3495 3577 if (!brand_help) {
3496 3578 if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE,
3497 3579 B_FALSE) != Z_OK)
3498 3580 return (Z_ERR);
3499 3581 if (verify_details(CMD_CLONE, argv) != Z_OK)
3500 3582 return (Z_ERR);
3501 3583
3502 3584 /*
3503 3585 * We also need to do some extra validation on the source zone.
3504 3586 */
3505 3587 if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) {
3506 3588 zerror(gettext("%s operation is invalid for the "
3507 3589 "global zone."), cmd_to_str(CMD_CLONE));
3508 3590 return (Z_ERR);
3509 3591 }
3510 3592
3511 3593 if (strncmp(source_zone, "SUNW", 4) == 0) {
3512 3594 zerror(gettext("%s operation is invalid for zones "
3513 3595 "starting with SUNW."), cmd_to_str(CMD_CLONE));
3514 3596 return (Z_ERR);
3515 3597 }
3516 3598
3517 3599 if (auth_check(username, source_zone, SOURCE_ZONE) == Z_ERR) {
3518 3600 zerror(gettext("%s operation is invalid because "
3519 3601 "user is not authorized to read source zone."),
3520 3602 cmd_to_str(CMD_CLONE));
3521 3603 return (Z_ERR);
3522 3604 }
3523 3605
3524 3606 zent = lookup_running_zone(source_zone);
3525 3607 if (zent != NULL) {
3526 3608 /* check whether the zone is ready or running */
3527 3609 if ((err = zone_get_state(zent->zname,
3528 3610 &zent->zstate_num)) != Z_OK) {
3529 3611 errno = err;
3530 3612 zperror2(zent->zname, gettext("could not get "
3531 3613 "state"));
3532 3614 /* can't tell, so hedge */
3533 3615 zent->zstate_str = "ready/running";
3534 3616 } else {
3535 3617 zent->zstate_str =
3536 3618 zone_state_str(zent->zstate_num);
3537 3619 }
3538 3620 zerror(gettext("%s operation is invalid for %s zones."),
3539 3621 cmd_to_str(CMD_CLONE), zent->zstate_str);
3540 3622 return (Z_ERR);
3541 3623 }
3542 3624
3543 3625 if ((err = zone_get_state(source_zone, &state)) != Z_OK) {
3544 3626 errno = err;
3545 3627 zperror2(source_zone, gettext("could not get state"));
3546 3628 return (Z_ERR);
3547 3629 }
3548 3630 if (state != ZONE_STATE_INSTALLED) {
3549 3631 (void) fprintf(stderr,
3550 3632 gettext("%s: zone %s is %s; %s is required.\n"),
3551 3633 execname, source_zone, zone_state_str(state),
3552 3634 zone_state_str(ZONE_STATE_INSTALLED));
3553 3635 return (Z_ERR);
3554 3636 }
3555 3637
3556 3638 /*
3557 3639 * The source zone checks out ok, continue with the clone.
3558 3640 */
3559 3641
3560 3642 if (validate_clone(source_zone, target_zone) != Z_OK)
3561 3643 return (Z_ERR);
3562 3644
3563 3645 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
3564 3646 zerror(gettext("another %s may have an operation in "
3565 3647 "progress."), "zoneadm");
3566 3648 return (Z_ERR);
3567 3649 }
3568 3650 }
3569 3651
3570 3652 if ((err = zone_get_zonepath(source_zone, source_zonepath,
3571 3653 sizeof (source_zonepath))) != Z_OK) {
3572 3654 errno = err;
3573 3655 zperror2(source_zone, gettext("could not get zone path"));
3574 3656 goto done;
3575 3657 }
3576 3658
3577 3659 if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
3578 3660 != Z_OK) {
3579 3661 errno = err;
3580 3662 zperror2(target_zone, gettext("could not get zone path"));
3581 3663 goto done;
3582 3664 }
3583 3665
3584 3666 /*
3585 3667 * Fetch the clone and postclone hooks from the brand configuration.
3586 3668 */
3587 3669 if ((bh = brand_open(target_brand)) == NULL) {
3588 3670 zerror(gettext("missing or invalid brand"));
3589 3671 err = Z_ERR;
3590 3672 goto done;
3591 3673 }
3592 3674
3593 3675 if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_clone, target_zone,
3594 3676 zonepath) != Z_OK) {
3595 3677 zerror("invalid brand configuration: missing clone resource");
3596 3678 brand_close(bh);
3597 3679 err = Z_ERR;
3598 3680 goto done;
3599 3681 }
3600 3682
3601 3683 if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postclone,
3602 3684 target_zone, zonepath) != Z_OK) {
3603 3685 zerror("invalid brand configuration: missing postclone "
3604 3686 "resource");
3605 3687 brand_close(bh);
3606 3688 err = Z_ERR;
3607 3689 goto done;
3608 3690 }
3609 3691
3610 3692 if (get_hook(bh, presnapbuf, sizeof (presnapbuf), brand_get_presnap,
3611 3693 source_zone, source_zonepath) != Z_OK) {
3612 3694 zerror("invalid brand configuration: missing presnap "
3613 3695 "resource");
3614 3696 brand_close(bh);
3615 3697 err = Z_ERR;
3616 3698 goto done;
3617 3699 }
3618 3700
3619 3701 if (get_hook(bh, postsnapbuf, sizeof (postsnapbuf), brand_get_postsnap,
3620 3702 source_zone, source_zonepath) != Z_OK) {
3621 3703 zerror("invalid brand configuration: missing postsnap "
3622 3704 "resource");
3623 3705 brand_close(bh);
3624 3706 err = Z_ERR;
3625 3707 goto done;
3626 3708 }
3627 3709
3628 3710 if (get_hook(bh, validsnapbuf, sizeof (validsnapbuf),
3629 3711 brand_get_validatesnap, target_zone, zonepath) != Z_OK) {
3630 3712 zerror("invalid brand configuration: missing validatesnap "
3631 3713 "resource");
3632 3714 brand_close(bh);
3633 3715 err = Z_ERR;
3634 3716 goto done;
3635 3717 }
3636 3718 brand_close(bh);
3637 3719
3638 3720 /* Append all options to clone hook. */
3639 3721 if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) {
3640 3722 err = Z_ERR;
3641 3723 goto done;
3642 3724 }
3643 3725
3644 3726 /* Append all options to postclone hook. */
3645 3727 if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK) {
3646 3728 err = Z_ERR;
3647 3729 goto done;
3648 3730 }
3649 3731
3650 3732 if (!brand_help) {
3651 3733 if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE))
3652 3734 != Z_OK) {
3653 3735 errno = err;
3654 3736 zperror2(target_zone, gettext("could not set state"));
3655 3737 goto done;
3656 3738 }
3657 3739 }
3658 3740
3659 3741 /*
3660 3742 * The clone hook is optional. If it exists, use the hook for
3661 3743 * cloning, otherwise use the built-in clone support
3662 3744 */
3663 3745 if (cmdbuf[0] != '\0') {
3664 3746 /* Run the clone hook */
3665 3747 status = do_subproc(cmdbuf);
3666 3748 if ((status = subproc_status(gettext("brand-specific clone"),
3667 3749 status, B_FALSE)) != ZONE_SUBPROC_OK) {
3668 3750 if (status == ZONE_SUBPROC_USAGE && !brand_help)
3669 3751 sub_usage(SHELP_CLONE, CMD_CLONE);
3670 3752 err = Z_ERR;
3671 3753 goto done;
3672 3754 }
3673 3755
3674 3756 if (brand_help)
3675 3757 return (Z_OK);
3676 3758
3677 3759 } else {
3678 3760 /* If just help, we're done since there is no brand help. */
3679 3761 if (brand_help)
3680 3762 return (Z_OK);
3681 3763
3682 3764 /* Run the built-in clone support. */
3683 3765
3684 3766 /* The only explicit built-in method is "copy". */
3685 3767 if (method != NULL && strcmp(method, "copy") != 0) {
3686 3768 sub_usage(SHELP_CLONE, CMD_CLONE);
3687 3769 err = Z_USAGE;
3688 3770 goto done;
3689 3771 }
3690 3772
3691 3773 if (snapshot != NULL) {
3692 3774 err = clone_snapshot_zfs(snapshot, zonepath,
3693 3775 validsnapbuf);
3694 3776 } else {
3695 3777 /*
3696 3778 * We always copy the clone unless the source is ZFS
3697 3779 * and a ZFS clone worked. We fallback to copying if
3698 3780 * the ZFS clone fails for some reason.
3699 3781 */
3700 3782 err = Z_ERR;
3701 3783 if (method == NULL && is_zonepath_zfs(source_zonepath))
3702 3784 err = clone_zfs(source_zonepath, zonepath,
3703 3785 presnapbuf, postsnapbuf);
3704 3786
3705 3787 if (err != Z_OK)
3706 3788 err = clone_copy(source_zonepath, zonepath);
3707 3789 }
3708 3790 }
3709 3791
3710 3792 if (err == Z_OK && postcmdbuf[0] != '\0') {
3711 3793 status = do_subproc(postcmdbuf);
3712 3794 if ((err = subproc_status("postclone", status, B_FALSE))
3713 3795 != ZONE_SUBPROC_OK) {
3714 3796 zerror(gettext("post-clone configuration failed."));
3715 3797 err = Z_ERR;
3716 3798 }
3717 3799 }
3718 3800
3719 3801 done:
3720 3802 /*
3721 3803 * If everything went well, we mark the zone as installed.
3722 3804 */
3723 3805 if (err == Z_OK) {
3724 3806 err = zone_set_state(target_zone, ZONE_STATE_INSTALLED);
3725 3807 if (err != Z_OK) {
3726 3808 errno = err;
3727 3809 zperror2(target_zone, gettext("could not set state"));
3728 3810 }
3729 3811 }
3730 3812 if (!brand_help)
3731 3813 zonecfg_release_lock_file(target_zone, lockfd);
3732 3814 return ((err == Z_OK) ? Z_OK : Z_ERR);
3733 3815 }
3734 3816
3735 3817 /*
3736 3818 * Used when removing a zonepath after uninstalling or cleaning up after
3737 3819 * the move subcommand. This handles a zonepath that has non-standard
3738 3820 * contents so that we will only cleanup the stuff we know about and leave
3739 3821 * any user data alone.
3740 3822 *
3741 3823 * If the "all" parameter is true then we should remove the whole zonepath
3742 3824 * even if it has non-standard files/directories in it. This can be used when
3743 3825 * we need to cleanup after moving the zonepath across file systems.
3744 3826 *
3745 3827 * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND
3746 3828 * and not the shell.
3747 3829 */
3748 3830 static int
3749 3831 cleanup_zonepath(char *zonepath, boolean_t all)
3750 3832 {
3751 3833 int status;
3752 3834 int i;
3753 3835 boolean_t non_std = B_FALSE;
3754 3836 struct dirent *dp;
3755 3837 DIR *dirp;
3756 3838 /*
3757 3839 * The SUNWattached.xml file is expected since it might
3758 3840 * exist if the zone was force-attached after a
3759 3841 * migration.
3760 3842 */
3761 3843 char *std_entries[] = {"dev", "lu", "root",
3762 3844 "SUNWattached.xml", NULL};
3763 3845 /* (MAXPATHLEN * 3) is for the 3 std_entries dirs */
3764 3846 char cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64];
3765 3847
3766 3848 /*
3767 3849 * We shouldn't need these checks but lets be paranoid since we
3768 3850 * could blow away the whole system here if we got the wrong zonepath.
3769 3851 */
3770 3852 if (*zonepath == NULL || strcmp(zonepath, "/") == 0) {
3771 3853 (void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath);
3772 3854 return (Z_INVAL);
3773 3855 }
3774 3856
3775 3857 /*
3776 3858 * If the dirpath is already gone (maybe it was manually removed) then
3777 3859 * we just return Z_OK so that the cleanup is successful.
3778 3860 */
3779 3861 if ((dirp = opendir(zonepath)) == NULL)
3780 3862 return (Z_OK);
3781 3863
3782 3864 /*
3783 3865 * Look through the zonepath directory to see if there are any
3784 3866 * non-standard files/dirs. Also skip .zfs since that might be
3785 3867 * there but we'll handle ZFS file systems as a special case.
3786 3868 */
3787 3869 while ((dp = readdir(dirp)) != NULL) {
3788 3870 if (strcmp(dp->d_name, ".") == 0 ||
3789 3871 strcmp(dp->d_name, "..") == 0 ||
3790 3872 strcmp(dp->d_name, ".zfs") == 0)
3791 3873 continue;
3792 3874
3793 3875 for (i = 0; std_entries[i] != NULL; i++)
3794 3876 if (strcmp(dp->d_name, std_entries[i]) == 0)
3795 3877 break;
3796 3878
3797 3879 if (std_entries[i] == NULL)
3798 3880 non_std = B_TRUE;
3799 3881 }
3800 3882 (void) closedir(dirp);
3801 3883
3802 3884 if (!all && non_std) {
3803 3885 /*
3804 3886 * There are extra, non-standard directories/files in the
3805 3887 * zonepath so we don't want to remove the zonepath. We
3806 3888 * just want to remove the standard directories and leave
3807 3889 * the user data alone.
3808 3890 */
3809 3891 (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND);
3810 3892
3811 3893 for (i = 0; std_entries[i] != NULL; i++) {
3812 3894 char tmpbuf[MAXPATHLEN];
3813 3895
3814 3896 if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s",
3815 3897 zonepath, std_entries[i]) >= sizeof (tmpbuf) ||
3816 3898 strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >=
3817 3899 sizeof (cmdbuf)) {
3818 3900 (void) fprintf(stderr,
3819 3901 gettext("path is too long\n"));
3820 3902 return (Z_INVAL);
3821 3903 }
3822 3904 }
3823 3905
3824 3906 status = do_subproc(cmdbuf);
3825 3907
3826 3908 (void) fprintf(stderr, gettext("WARNING: Unable to completely "
3827 3909 "remove %s\nbecause it contains additional user data. "
3828 3910 "Only the standard directory\nentries have been "
3829 3911 "removed.\n"),
3830 3912 zonepath);
3831 3913
3832 3914 return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
3833 3915 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
3834 3916 }
3835 3917
3836 3918 /*
3837 3919 * There is nothing unexpected in the zonepath, try to get rid of the
3838 3920 * whole zonepath directory.
3839 3921 *
3840 3922 * If the zonepath is its own zfs file system, try to destroy the
3841 3923 * file system. If that fails for some reason (e.g. it has clones)
3842 3924 * then we'll just remove the contents of the zonepath.
3843 3925 */
3844 3926 if (is_zonepath_zfs(zonepath)) {
3845 3927 if (destroy_zfs(zonepath) == Z_OK)
3846 3928 return (Z_OK);
3847 3929 (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND
3848 3930 " %s/*", zonepath);
3849 3931 status = do_subproc(cmdbuf);
3850 3932 return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
3851 3933 ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
3852 3934 }
3853 3935
3854 3936 (void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
3855 3937 zonepath);
3856 3938 status = do_subproc(cmdbuf);
3857 3939
3858 3940 return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK)
3859 3941 ? Z_OK : Z_ERR);
3860 3942 }
3861 3943
3862 3944 static int
3863 3945 move_func(int argc, char *argv[])
3864 3946 {
3865 3947 char *new_zonepath = NULL;
3866 3948 int lockfd;
3867 3949 int err, arg;
3868 3950 char zonepath[MAXPATHLEN];
3869 3951 zone_dochandle_t handle;
3870 3952 boolean_t fast;
3871 3953 boolean_t is_zfs = B_FALSE;
3872 3954 boolean_t root_fs_mounted = B_FALSE;
3873 3955 struct dirent *dp;
3874 3956 DIR *dirp;
3875 3957 boolean_t empty = B_TRUE;
3876 3958 boolean_t revert;
3877 3959 struct stat zonepath_buf;
3878 3960 struct stat new_zonepath_buf;
3879 3961 zone_mounts_t mounts;
3880 3962
3881 3963 if (zonecfg_in_alt_root()) {
3882 3964 zerror(gettext("cannot move zone in alternate root"));
3883 3965 return (Z_ERR);
3884 3966 }
3885 3967
3886 3968 optind = 0;
3887 3969 if ((arg = getopt(argc, argv, "?")) != EOF) {
3888 3970 switch (arg) {
3889 3971 case '?':
3890 3972 sub_usage(SHELP_MOVE, CMD_MOVE);
3891 3973 return (optopt == '?' ? Z_OK : Z_USAGE);
3892 3974 default:
3893 3975 sub_usage(SHELP_MOVE, CMD_MOVE);
3894 3976 return (Z_USAGE);
3895 3977 }
3896 3978 }
3897 3979 if (argc != (optind + 1)) {
3898 3980 sub_usage(SHELP_MOVE, CMD_MOVE);
3899 3981 return (Z_USAGE);
3900 3982 }
3901 3983 new_zonepath = argv[optind];
3902 3984 if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE)
3903 3985 != Z_OK)
3904 3986 return (Z_ERR);
3905 3987 if (verify_details(CMD_MOVE, argv) != Z_OK)
3906 3988 return (Z_ERR);
3907 3989
3908 3990 /*
3909 3991 * Check out the new zonepath. This has the side effect of creating
3910 3992 * a directory for the new zonepath. We depend on this later when we
3911 3993 * stat to see if we are doing a cross file system move or not.
3912 3994 */
3913 3995 if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK)
3914 3996 return (Z_ERR);
3915 3997
3916 3998 if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
3917 3999 != Z_OK) {
3918 4000 errno = err;
3919 4001 zperror2(target_zone, gettext("could not get zone path"));
3920 4002 return (Z_ERR);
3921 4003 }
3922 4004
3923 4005 if (stat(zonepath, &zonepath_buf) == -1) {
3924 4006 zperror(gettext("could not stat zone path"), B_FALSE);
3925 4007 return (Z_ERR);
3926 4008 }
3927 4009
3928 4010 if (stat(new_zonepath, &new_zonepath_buf) == -1) {
3929 4011 zperror(gettext("could not stat new zone path"), B_FALSE);
3930 4012 return (Z_ERR);
3931 4013 }
3932 4014
3933 4015 /*
3934 4016 * Check if the destination directory is empty.
3935 4017 */
3936 4018 if ((dirp = opendir(new_zonepath)) == NULL) {
3937 4019 zperror(gettext("could not open new zone path"), B_FALSE);
3938 4020 return (Z_ERR);
3939 4021 }
3940 4022 while ((dp = readdir(dirp)) != (struct dirent *)0) {
3941 4023 if (strcmp(dp->d_name, ".") == 0 ||
3942 4024 strcmp(dp->d_name, "..") == 0)
3943 4025 continue;
3944 4026 empty = B_FALSE;
3945 4027 break;
3946 4028 }
3947 4029 (void) closedir(dirp);
3948 4030
3949 4031 /* Error if there is anything in the destination directory. */
3950 4032 if (!empty) {
3951 4033 (void) fprintf(stderr, gettext("could not move zone to %s: "
3952 4034 "directory not empty\n"), new_zonepath);
3953 4035 return (Z_ERR);
3954 4036 }
3955 4037
3956 4038 /*
3957 4039 * Collect information about mounts within the zone's zonepath.
3958 4040 * Overlay mounts on the zone's root directory are erroneous.
3959 4041 * Bail if we encounter any unexpected mounts.
3960 4042 */
3961 4043 if (zone_mounts_init(&mounts, zonepath) != 0)
3962 4044 return (Z_ERR);
3963 4045 if (mounts.num_root_overlay_mounts != 0) {
3964 4046 zerror(gettext("%d overlay mount(s) detected on %s/root."),
3965 4047 mounts.num_root_overlay_mounts, zonepath);
3966 4048 goto err_and_mounts_destroy;
3967 4049 }
3968 4050 if (mounts.num_unexpected_mounts != 0)
3969 4051 goto err_and_mounts_destroy;
3970 4052
3971 4053 /*
3972 4054 * Check if we are moving in the same file system and can do a fast
3973 4055 * move or if we are crossing file systems and have to copy the data.
3974 4056 */
3975 4057 fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev);
3976 4058
3977 4059 if ((handle = zonecfg_init_handle()) == NULL) {
3978 4060 zperror(cmd_to_str(CMD_MOVE), B_TRUE);
3979 4061 goto err_and_mounts_destroy;
3980 4062 }
3981 4063
3982 4064 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
3983 4065 errno = err;
3984 4066 zperror(cmd_to_str(CMD_MOVE), B_TRUE);
3985 4067 goto err_and_fini_handle;
3986 4068 }
3987 4069
3988 4070 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
3989 4071 zerror(gettext("another %s may have an operation in progress."),
3990 4072 "zoneadm");
3991 4073 goto err_and_fini_handle;
3992 4074 }
3993 4075
3994 4076 /*
3995 4077 * Unmount the zone's root filesystem before we move the zone's
3996 4078 * zonepath.
3997 4079 */
3998 4080 if (zone_unmount_rootfs(&mounts, zonepath, B_FALSE) != 0)
3999 4081 goto err_and_rele_lockfile;
4000 4082
4001 4083 /*
4002 4084 * We're making some file system changes now so we have to clean up
4003 4085 * the file system before we are done. This will either clean up the
4004 4086 * new zonepath if the zonecfg update failed or it will clean up the
4005 4087 * old zonepath if everything is ok.
4006 4088 */
4007 4089 revert = B_TRUE;
4008 4090
4009 4091 if (is_zonepath_zfs(zonepath) &&
4010 4092 move_zfs(zonepath, new_zonepath) != Z_ERR) {
4011 4093 is_zfs = B_TRUE;
4012 4094
4013 4095 } else if (fast) {
4014 4096 /* same file system, use rename for a quick move */
4015 4097
4016 4098 /*
4017 4099 * Remove the new_zonepath directory that got created above
4018 4100 * during the validation. It gets in the way of the rename.
4019 4101 */
4020 4102 if (rmdir(new_zonepath) != 0) {
4021 4103 zperror(gettext("could not rmdir new zone path"),
4022 4104 B_FALSE);
4023 4105 (void) zone_mount_rootfs(&mounts, zonepath);
4024 4106 goto err_and_rele_lockfile;
4025 4107 }
4026 4108
4027 4109 if (rename(zonepath, new_zonepath) != 0) {
4028 4110 /*
4029 4111 * If this fails we don't need to do all of the
4030 4112 * cleanup that happens for the rest of the code
4031 4113 * so just return from this error.
4032 4114 */
4033 4115 zperror(gettext("could not move zone"), B_FALSE);
4034 4116 (void) zone_mount_rootfs(&mounts, zonepath);
4035 4117 goto err_and_rele_lockfile;
4036 4118 }
4037 4119
4038 4120 } else {
4039 4121 /*
4040 4122 * Attempt to create a ZFS fs for the new zonepath. As usual,
4041 4123 * we don't care if this works or not since we always have the
4042 4124 * default behavior of a simple directory for the zonepath.
4043 4125 */
4044 4126 create_zfs_zonepath(new_zonepath);
4045 4127
4046 4128 (void) printf(gettext(
4047 4129 "Moving across file systems; copying zonepath %s..."),
4048 4130 zonepath);
4049 4131 (void) fflush(stdout);
4050 4132
4051 4133 err = copy_zone(zonepath, new_zonepath);
4052 4134
4053 4135 (void) printf("\n");
4054 4136 if (err != Z_OK)
4055 4137 goto done;
4056 4138 }
4057 4139
4058 4140 /*
4059 4141 * Mount the zone's root filesystem in the new zonepath if there was
4060 4142 * a root mount prior to the move.
4061 4143 */
4062 4144 if (zone_mount_rootfs(&mounts, new_zonepath) != 0) {
4063 4145 err = Z_ERR;
4064 4146 goto done;
4065 4147 }
4066 4148 root_fs_mounted = B_TRUE;
4067 4149
4068 4150 if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) {
4069 4151 errno = err;
4070 4152 zperror(gettext("could not set new zonepath"), B_TRUE);
4071 4153 goto done;
4072 4154 }
4073 4155
4074 4156 if ((err = zonecfg_save(handle)) != Z_OK) {
4075 4157 errno = err;
4076 4158 zperror(gettext("zonecfg save failed"), B_TRUE);
4077 4159 goto done;
4078 4160 }
4079 4161
4080 4162 revert = B_FALSE;
4081 4163
4082 4164 done:
4083 4165 zonecfg_fini_handle(handle);
4084 4166 zonecfg_release_lock_file(target_zone, lockfd);
4085 4167
4086 4168 /*
4087 4169 * Clean up the file system based on how things went. We either
4088 4170 * clean up the new zonepath if the operation failed for some reason
4089 4171 * or we clean up the old zonepath if everything is ok.
4090 4172 */
4091 4173 if (revert) {
4092 4174 /*
4093 4175 * Check for the unlikely scenario in which the zone's
4094 4176 * zonepath and its root file system moved but libzonecfg
4095 4177 * couldn't save the new zonepath to the zone's configuration
4096 4178 * file. The mounted root filesystem must be unmounted before
4097 4179 * zoneadm restores the zone's zonepath.
4098 4180 */
4099 4181 if (root_fs_mounted && zone_unmount_rootfs(&mounts,
4100 4182 new_zonepath, B_TRUE) != 0) {
4101 4183 /*
4102 4184 * We can't forcibly unmount the zone's root file system
4103 4185 * from the new zonepath. Bail!
4104 4186 */
4105 4187 zerror(gettext("fatal error: cannot unmount %s/root\n"),
4106 4188 new_zonepath);
4107 4189 goto err_and_mounts_destroy;
4108 4190 }
4109 4191
4110 4192 /* The zonecfg update failed, cleanup the new zonepath. */
4111 4193 if (is_zfs) {
4112 4194 if (move_zfs(new_zonepath, zonepath) == Z_ERR) {
4113 4195 (void) fprintf(stderr, gettext("could not "
4114 4196 "restore zonepath, the zfs mountpoint is "
4115 4197 "set as:\n%s\n"), new_zonepath);
4116 4198 /*
4117 4199 * err is already != Z_OK since we're reverting
4118 4200 */
4119 4201 } else {
4120 4202 (void) zone_mount_rootfs(&mounts, zonepath);
4121 4203 }
4122 4204 } else if (fast) {
4123 4205 if (rename(new_zonepath, zonepath) != 0) {
4124 4206 zperror(gettext("could not restore zonepath"),
4125 4207 B_FALSE);
4126 4208 /*
4127 4209 * err is already != Z_OK since we're reverting
4128 4210 */
4129 4211 } else {
4130 4212 (void) zone_mount_rootfs(&mounts, zonepath);
4131 4213 }
4132 4214 } else {
4133 4215 (void) printf(gettext("Cleaning up zonepath %s..."),
4134 4216 new_zonepath);
4135 4217 (void) fflush(stdout);
4136 4218 err = cleanup_zonepath(new_zonepath, B_TRUE);
4137 4219 (void) printf("\n");
4138 4220
4139 4221 if (err != Z_OK) {
4140 4222 errno = err;
4141 4223 zperror(gettext("could not remove new "
4142 4224 "zonepath"), B_TRUE);
4143 4225 } else {
4144 4226 /*
4145 4227 * Because we're reverting we know the mainline
4146 4228 * code failed but we just reused the err
4147 4229 * variable so we reset it back to Z_ERR.
4148 4230 */
4149 4231 err = Z_ERR;
4150 4232 }
4151 4233
4152 4234 (void) zone_mount_rootfs(&mounts, zonepath);
4153 4235 }
4154 4236 } else {
4155 4237 /* The move was successful, cleanup the old zonepath. */
4156 4238 if (!is_zfs && !fast) {
4157 4239 (void) printf(
4158 4240 gettext("Cleaning up zonepath %s..."), zonepath);
4159 4241 (void) fflush(stdout);
4160 4242 err = cleanup_zonepath(zonepath, B_TRUE);
4161 4243 (void) printf("\n");
4162 4244
4163 4245 if (err != Z_OK) {
4164 4246 errno = err;
4165 4247 zperror(gettext("could not remove zonepath"),
4166 4248 B_TRUE);
4167 4249 }
4168 4250 }
4169 4251 }
4170 4252
4171 4253 zone_mounts_destroy(&mounts);
4172 4254 return ((err == Z_OK) ? Z_OK : Z_ERR);
4173 4255
4174 4256 err_and_rele_lockfile:
4175 4257 zonecfg_release_lock_file(target_zone, lockfd);
4176 4258 err_and_fini_handle:
4177 4259 zonecfg_fini_handle(handle);
4178 4260 err_and_mounts_destroy:
4179 4261 zone_mounts_destroy(&mounts);
4180 4262 return (Z_ERR);
4181 4263 }
4182 4264
4183 4265 /* ARGSUSED */
4184 4266 static int
4185 4267 detach_func(int argc, char *argv[])
4186 4268 {
4187 4269 int lockfd = -1;
4188 4270 int err, arg;
4189 4271 char zonepath[MAXPATHLEN];
4190 4272 char cmdbuf[MAXPATHLEN];
4191 4273 char precmdbuf[MAXPATHLEN];
4192 4274 boolean_t execute = B_TRUE;
4193 4275 boolean_t brand_help = B_FALSE;
4194 4276 brand_handle_t bh = NULL;
4195 4277 int status;
4196 4278
4197 4279 if (zonecfg_in_alt_root()) {
4198 4280 zerror(gettext("cannot detach zone in alternate root"));
4199 4281 return (Z_ERR);
4200 4282 }
4201 4283
4202 4284 /* Check the argv string for args we handle internally */
4203 4285 optind = 0;
4204 4286 opterr = 0;
4205 4287 while ((arg = getopt(argc, argv, "?n")) != EOF) {
4206 4288 switch (arg) {
4207 4289 case '?':
4208 4290 if (optopt == '?') {
4209 4291 sub_usage(SHELP_DETACH, CMD_DETACH);
4210 4292 brand_help = B_TRUE;
4211 4293 }
4212 4294 /* Ignore unknown options - may be brand specific. */
4213 4295 break;
4214 4296 case 'n':
4215 4297 execute = B_FALSE;
4216 4298 break;
4217 4299 default:
4218 4300 /* Ignore unknown options - may be brand specific. */
4219 4301 break;
4220 4302 }
4221 4303 }
4222 4304
4223 4305 if (brand_help)
4224 4306 execute = B_FALSE;
4225 4307
4226 4308 if (execute) {
4227 4309 if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE,
4228 4310 B_FALSE) != Z_OK)
4229 4311 return (Z_ERR);
4230 4312 if (verify_details(CMD_DETACH, argv) != Z_OK)
4231 4313 return (Z_ERR);
4232 4314 } else {
4233 4315 /*
4234 4316 * We want a dry-run to work for a non-privileged user so we
4235 4317 * only do minimal validation.
4236 4318 */
4237 4319 if (target_zone == NULL) {
4238 4320 zerror(gettext("no zone specified"));
4239 4321 return (Z_ERR);
4240 4322 }
4241 4323
4242 4324 if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) {
4243 4325 zerror(gettext("%s operation is invalid for the "
4244 4326 "global zone."), cmd_to_str(CMD_DETACH));
4245 4327 return (Z_ERR);
4246 4328 }
4247 4329 }
4248 4330
4249 4331 if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
4250 4332 != Z_OK) {
4251 4333 errno = err;
4252 4334 zperror2(target_zone, gettext("could not get zone path"));
4253 4335 return (Z_ERR);
4254 4336 }
4255 4337
4256 4338 /* Fetch the detach and predetach hooks from the brand configuration. */
4257 4339 if ((bh = brand_open(target_brand)) == NULL) {
4258 4340 zerror(gettext("missing or invalid brand"));
4259 4341 return (Z_ERR);
4260 4342 }
4261 4343
4262 4344 if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_detach, target_zone,
4263 4345 zonepath) != Z_OK) {
4264 4346 zerror("invalid brand configuration: missing detach resource");
4265 4347 brand_close(bh);
4266 4348 return (Z_ERR);
4267 4349 }
4268 4350
4269 4351 if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_predetach,
4270 4352 target_zone, zonepath) != Z_OK) {
4271 4353 zerror("invalid brand configuration: missing predetach "
4272 4354 "resource");
4273 4355 brand_close(bh);
4274 4356 return (Z_ERR);
4275 4357 }
4276 4358 brand_close(bh);
4277 4359
4278 4360 /* Append all options to predetach hook. */
4279 4361 if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
4280 4362 return (Z_ERR);
4281 4363
4282 4364 /* Append all options to detach hook. */
4283 4365 if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
4284 4366 return (Z_ERR);
4285 4367
4286 4368 if (execute && zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
4287 4369 zerror(gettext("another %s may have an operation in progress."),
4288 4370 "zoneadm");
4289 4371 return (Z_ERR);
4290 4372 }
4291 4373
4292 4374 /* If we have a brand predetach hook, run it. */
4293 4375 if (!brand_help && precmdbuf[0] != '\0') {
4294 4376 status = do_subproc(precmdbuf);
4295 4377 if (subproc_status(gettext("brand-specific predetach"),
4296 4378 status, B_FALSE) != ZONE_SUBPROC_OK) {
4297 4379
4298 4380 if (execute) {
4299 4381 assert(lockfd >= 0);
4300 4382 zonecfg_release_lock_file(target_zone, lockfd);
4301 4383 lockfd = -1;
4302 4384 }
4303 4385
4304 4386 assert(lockfd == -1);
4305 4387 return (Z_ERR);
4306 4388 }
4307 4389 }
4308 4390
4309 4391 if (cmdbuf[0] != '\0') {
4310 4392 /* Run the detach hook */
4311 4393 status = do_subproc(cmdbuf);
4312 4394 if ((status = subproc_status(gettext("brand-specific detach"),
4313 4395 status, B_FALSE)) != ZONE_SUBPROC_OK) {
4314 4396 if (status == ZONE_SUBPROC_USAGE && !brand_help)
4315 4397 sub_usage(SHELP_DETACH, CMD_DETACH);
4316 4398
4317 4399 if (execute) {
4318 4400 assert(lockfd >= 0);
4319 4401 zonecfg_release_lock_file(target_zone, lockfd);
4320 4402 lockfd = -1;
4321 4403 }
4322 4404
4323 4405 assert(lockfd == -1);
4324 4406 return (Z_ERR);
4325 4407 }
4326 4408
4327 4409 } else {
4328 4410 zone_dochandle_t handle;
4329 4411
4330 4412 /* If just help, we're done since there is no brand help. */
4331 4413 if (brand_help) {
4332 4414 assert(lockfd == -1);
4333 4415 return (Z_OK);
4334 4416 }
4335 4417
4336 4418 /*
4337 4419 * Run the built-in detach support. Just generate a simple
4338 4420 * zone definition XML file and detach.
4339 4421 */
4340 4422
4341 4423 /* Don't detach the zone if anything is still mounted there */
4342 4424 if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) {
4343 4425 (void) fprintf(stderr, gettext("These file systems are "
4344 4426 "mounted on subdirectories of %s.\n"), zonepath);
4345 4427 (void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
4346 4428 err = ZONE_SUBPROC_NOTCOMPLETE;
4347 4429 goto done;
4348 4430 }
4349 4431
4350 4432 if ((handle = zonecfg_init_handle()) == NULL) {
4351 4433 zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4352 4434 err = ZONE_SUBPROC_NOTCOMPLETE;
4353 4435 goto done;
4354 4436 }
4355 4437
4356 4438 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4357 4439 errno = err;
4358 4440 zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4359 4441
4360 4442 } else if ((err = zonecfg_detach_save(handle,
4361 4443 (execute ? 0 : ZONE_DRY_RUN))) != Z_OK) {
4362 4444 errno = err;
4363 4445 zperror(gettext("saving the detach manifest failed"),
4364 4446 B_TRUE);
4365 4447 }
4366 4448
4367 4449 zonecfg_fini_handle(handle);
4368 4450 if (err != Z_OK)
4369 4451 goto done;
4370 4452 }
4371 4453
4372 4454 /*
4373 4455 * Set the zone state back to configured unless we are running with the
4374 4456 * no-execute option.
4375 4457 */
4376 4458 if (execute && (err = zone_set_state(target_zone,
4377 4459 ZONE_STATE_CONFIGURED)) != Z_OK) {
4378 4460 errno = err;
4379 4461 zperror(gettext("could not reset state"), B_TRUE);
4380 4462 }
4381 4463
4382 4464 done:
4383 4465 if (execute) {
4384 4466 assert(lockfd >= 0);
4385 4467 zonecfg_release_lock_file(target_zone, lockfd);
4386 4468 lockfd = -1;
4387 4469 }
4388 4470
4389 4471 assert(lockfd == -1);
4390 4472 return ((err == Z_OK) ? Z_OK : Z_ERR);
4391 4473 }
4392 4474
4393 4475 /*
4394 4476 * Determine the brand when doing a dry-run attach. The zone does not have to
4395 4477 * exist, so we have to read the incoming manifest to determine the zone's
4396 4478 * brand.
4397 4479 *
4398 4480 * Because the manifest has to be processed twice; once to determine the brand
4399 4481 * and once to do the brand-specific attach logic, we always read it into a tmp
4400 4482 * file. This handles the manifest coming from stdin or a regular file. The
4401 4483 * tmpname parameter returns the name of the temporary file that the manifest
4402 4484 * was read into.
4403 4485 */
4404 4486 static int
4405 4487 dryrun_get_brand(char *manifest_path, char *tmpname, int size)
4406 4488 {
4407 4489 int fd;
4408 4490 int err;
4409 4491 int res = Z_OK;
4410 4492 zone_dochandle_t local_handle;
4411 4493 zone_dochandle_t rem_handle = NULL;
4412 4494 int len;
4413 4495 int ofd;
4414 4496 char buf[512];
4415 4497
4416 4498 if (strcmp(manifest_path, "-") == 0) {
4417 4499 fd = STDIN_FILENO;
4418 4500 } else {
4419 4501 if ((fd = open(manifest_path, O_RDONLY)) < 0) {
4420 4502 if (getcwd(buf, sizeof (buf)) == NULL)
4421 4503 (void) strlcpy(buf, "/", sizeof (buf));
4422 4504 zerror(gettext("could not open manifest path %s%s: %s"),
4423 4505 (*manifest_path == '/' ? "" : buf), manifest_path,
4424 4506 strerror(errno));
4425 4507 return (Z_ERR);
4426 4508 }
4427 4509 }
4428 4510
4429 4511 (void) snprintf(tmpname, size, "/var/run/zone.%d", getpid());
4430 4512
4431 4513 if ((ofd = open(tmpname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
4432 4514 zperror(gettext("could not save manifest"), B_FALSE);
4433 4515 (void) close(fd);
4434 4516 return (Z_ERR);
4435 4517 }
4436 4518
4437 4519 while ((len = read(fd, buf, sizeof (buf))) > 0) {
4438 4520 if (write(ofd, buf, len) == -1) {
4439 4521 zperror(gettext("could not save manifest"), B_FALSE);
4440 4522 (void) close(ofd);
4441 4523 (void) close(fd);
4442 4524 return (Z_ERR);
4443 4525 }
4444 4526 }
4445 4527
4446 4528 if (close(ofd) != 0) {
4447 4529 zperror(gettext("could not save manifest"), B_FALSE);
4448 4530 (void) close(fd);
4449 4531 return (Z_ERR);
4450 4532 }
4451 4533
4452 4534 (void) close(fd);
4453 4535
4454 4536 if ((fd = open(tmpname, O_RDONLY)) < 0) {
4455 4537 zperror(gettext("could not open manifest path"), B_FALSE);
4456 4538 return (Z_ERR);
4457 4539 }
4458 4540
4459 4541 if ((local_handle = zonecfg_init_handle()) == NULL) {
4460 4542 zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4461 4543 res = Z_ERR;
4462 4544 goto done;
4463 4545 }
4464 4546
4465 4547 if ((rem_handle = zonecfg_init_handle()) == NULL) {
4466 4548 zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4467 4549 res = Z_ERR;
4468 4550 goto done;
4469 4551 }
4470 4552
4471 4553 if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle))
4472 4554 != Z_OK) {
4473 4555 res = Z_ERR;
4474 4556
4475 4557 if (err == Z_INVALID_DOCUMENT) {
4476 4558 struct stat st;
4477 4559 char buf[6];
4478 4560
4479 4561 if (strcmp(manifest_path, "-") == 0) {
4480 4562 zerror(gettext("Input is not a valid XML "
4481 4563 "file"));
4482 4564 goto done;
4483 4565 }
4484 4566
4485 4567 if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) {
4486 4568 zerror(gettext("%s is not an XML file"),
4487 4569 manifest_path);
4488 4570 goto done;
4489 4571 }
4490 4572
4491 4573 bzero(buf, sizeof (buf));
4492 4574 (void) lseek(fd, 0L, SEEK_SET);
4493 4575 if (read(fd, buf, sizeof (buf) - 1) < 0 ||
4494 4576 strncmp(buf, "<?xml", 5) != 0)
4495 4577 zerror(gettext("%s is not an XML file"),
4496 4578 manifest_path);
4497 4579 else
4498 4580 zerror(gettext("Cannot attach to an earlier "
4499 4581 "release of the operating system"));
4500 4582 } else {
4501 4583 zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4502 4584 }
4503 4585 goto done;
4504 4586 }
4505 4587
4506 4588 /* Retrieve remote handle brand type. */
4507 4589 if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand))
4508 4590 != Z_OK) {
4509 4591 zerror(gettext("missing or invalid brand"));
4510 4592 exit(Z_ERR);
4511 4593 }
4512 4594
4513 4595 done:
4514 4596 zonecfg_fini_handle(local_handle);
4515 4597 zonecfg_fini_handle(rem_handle);
4516 4598 (void) close(fd);
4517 4599
4518 4600 return ((res == Z_OK) ? Z_OK : Z_ERR);
4519 4601 }
4520 4602
4521 4603 /* ARGSUSED */
4522 4604 static int
4523 4605 attach_func(int argc, char *argv[])
4524 4606 {
4525 4607 int lockfd = -1;
4526 4608 int err, arg;
4527 4609 boolean_t force = B_FALSE;
4528 4610 zone_dochandle_t handle;
4529 4611 char zonepath[MAXPATHLEN];
4530 4612 char cmdbuf[MAXPATHLEN];
4531 4613 char postcmdbuf[MAXPATHLEN];
4532 4614 boolean_t execute = B_TRUE;
4533 4615 boolean_t brand_help = B_FALSE;
4534 4616 char *manifest_path;
4535 4617 char tmpmanifest[80];
4536 4618 int manifest_pos;
4537 4619 brand_handle_t bh = NULL;
4538 4620 int status;
4539 4621 int last_index = 0;
4540 4622 int offset;
4541 4623 char *up;
4542 4624 boolean_t forced_update = B_FALSE;
4543 4625
4544 4626 if (zonecfg_in_alt_root()) {
4545 4627 zerror(gettext("cannot attach zone in alternate root"));
4546 4628 return (Z_ERR);
4547 4629 }
4548 4630
4549 4631 /* Check the argv string for args we handle internally */
4550 4632 optind = 0;
4551 4633 opterr = 0;
4552 4634 while ((arg = getopt(argc, argv, "?Fn:U")) != EOF) {
4553 4635 switch (arg) {
4554 4636 case '?':
4555 4637 if (optopt == '?') {
4556 4638 sub_usage(SHELP_ATTACH, CMD_ATTACH);
4557 4639 brand_help = B_TRUE;
4558 4640 }
4559 4641 /* Ignore unknown options - may be brand specific. */
4560 4642 break;
4561 4643 case 'F':
4562 4644 force = B_TRUE;
4563 4645 break;
4564 4646 case 'n':
4565 4647 execute = B_FALSE;
4566 4648 manifest_path = optarg;
4567 4649 manifest_pos = optind - 1;
4568 4650 break;
4569 4651 case 'U':
4570 4652 /*
4571 4653 * Undocumented 'force update' option for p2v update on
4572 4654 * attach when zone is in the incomplete state. Change
4573 4655 * the option back to 'u' and set forced_update flag.
4574 4656 */
4575 4657 if (optind == last_index)
4576 4658 offset = optind;
4577 4659 else
4578 4660 offset = optind - 1;
4579 4661 if ((up = index(argv[offset], 'U')) != NULL)
4580 4662 *up = 'u';
4581 4663 forced_update = B_TRUE;
4582 4664 break;
4583 4665 default:
4584 4666 /* Ignore unknown options - may be brand specific. */
4585 4667 break;
4586 4668 }
4587 4669 last_index = optind;
4588 4670 }
4589 4671
4590 4672 if (brand_help) {
4591 4673 force = B_FALSE;
4592 4674 execute = B_TRUE;
4593 4675 }
4594 4676
4595 4677 /* dry-run and force flags are mutually exclusive */
4596 4678 if (!execute && force) {
4597 4679 zerror(gettext("-F and -n flags are mutually exclusive"));
4598 4680 return (Z_ERR);
4599 4681 }
4600 4682
4601 4683 /*
4602 4684 * If the no-execute option was specified, we don't do validation and
4603 4685 * need to figure out the brand, since there is no zone required to be
4604 4686 * configured for this option.
4605 4687 */
4606 4688 if (execute) {
4607 4689 if (!brand_help) {
4608 4690 if (sanity_check(target_zone, CMD_ATTACH, B_FALSE,
4609 4691 B_TRUE, forced_update) != Z_OK)
4610 4692 return (Z_ERR);
4611 4693 if (verify_details(CMD_ATTACH, argv) != Z_OK)
4612 4694 return (Z_ERR);
4613 4695 }
4614 4696
4615 4697 if ((err = zone_get_zonepath(target_zone, zonepath,
4616 4698 sizeof (zonepath))) != Z_OK) {
4617 4699 errno = err;
4618 4700 zperror2(target_zone,
4619 4701 gettext("could not get zone path"));
4620 4702 return (Z_ERR);
4621 4703 }
4622 4704 } else {
4623 4705 if (dryrun_get_brand(manifest_path, tmpmanifest,
4624 4706 sizeof (tmpmanifest)) != Z_OK)
4625 4707 return (Z_ERR);
4626 4708
4627 4709 argv[manifest_pos] = tmpmanifest;
4628 4710 target_zone = "-";
4629 4711 (void) strlcpy(zonepath, "-", sizeof (zonepath));
4630 4712
4631 4713 /* Run the brand's verify_adm hook. */
4632 4714 if (verify_brand(NULL, CMD_ATTACH, argv) != Z_OK)
4633 4715 return (Z_ERR);
4634 4716 }
4635 4717
4636 4718 /*
4637 4719 * Fetch the attach and postattach hooks from the brand configuration.
4638 4720 */
4639 4721 if ((bh = brand_open(target_brand)) == NULL) {
4640 4722 zerror(gettext("missing or invalid brand"));
4641 4723 return (Z_ERR);
4642 4724 }
4643 4725
4644 4726 if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_attach, target_zone,
4645 4727 zonepath) != Z_OK) {
4646 4728 zerror("invalid brand configuration: missing attach resource");
4647 4729 brand_close(bh);
4648 4730 return (Z_ERR);
4649 4731 }
4650 4732
4651 4733 if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postattach,
4652 4734 target_zone, zonepath) != Z_OK) {
4653 4735 zerror("invalid brand configuration: missing postattach "
4654 4736 "resource");
4655 4737 brand_close(bh);
4656 4738 return (Z_ERR);
4657 4739 }
4658 4740 brand_close(bh);
4659 4741
4660 4742 /* Append all options to attach hook. */
4661 4743 if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
4662 4744 return (Z_ERR);
4663 4745
4664 4746 /* Append all options to postattach hook. */
4665 4747 if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK)
4666 4748 return (Z_ERR);
4667 4749
4668 4750 if (execute && !brand_help) {
4669 4751 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
4670 4752 zerror(gettext("another %s may have an operation in "
4671 4753 "progress."), "zoneadm");
4672 4754 return (Z_ERR);
4673 4755 }
4674 4756 }
4675 4757
4676 4758 if (!force) {
4677 4759 /*
4678 4760 * Not a force-attach, so we need to actually do the work.
4679 4761 */
4680 4762 if (cmdbuf[0] != '\0') {
4681 4763 /* Run the attach hook */
4682 4764 status = do_subproc(cmdbuf);
4683 4765 if ((status = subproc_status(gettext("brand-specific "
4684 4766 "attach"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
4685 4767 if (status == ZONE_SUBPROC_USAGE && !brand_help)
4686 4768 sub_usage(SHELP_ATTACH, CMD_ATTACH);
4687 4769
4688 4770 if (execute && !brand_help) {
4689 4771 assert(zonecfg_lock_file_held(&lockfd));
4690 4772 zonecfg_release_lock_file(target_zone,
4691 4773 lockfd);
4692 4774 lockfd = -1;
4693 4775 }
4694 4776
4695 4777 assert(lockfd == -1);
4696 4778 return (Z_ERR);
4697 4779 }
4698 4780 }
4699 4781
4700 4782 /*
4701 4783 * Else run the built-in attach support.
4702 4784 * This is a no-op since there is nothing to validate.
4703 4785 */
4704 4786
4705 4787 /* If dry-run or help, then we're done. */
4706 4788 if (!execute || brand_help) {
4707 4789 if (!execute)
4708 4790 (void) unlink(tmpmanifest);
4709 4791 assert(lockfd == -1);
4710 4792 return (Z_OK);
4711 4793 }
4712 4794 }
4713 4795
4714 4796 /* Now we can validate that the zonepath exists. */
4715 4797 if (validate_zonepath(zonepath, CMD_ATTACH) != Z_OK) {
4716 4798 (void) fprintf(stderr, gettext("could not verify zonepath %s "
4717 4799 "because of the above errors.\n"), zonepath);
4718 4800
4719 4801 assert(zonecfg_lock_file_held(&lockfd));
4720 4802 zonecfg_release_lock_file(target_zone, lockfd);
4721 4803 return (Z_ERR);
4722 4804 }
4723 4805
4724 4806 if ((handle = zonecfg_init_handle()) == NULL) {
4725 4807 zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4726 4808 err = Z_ERR;
4727 4809 } else if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4728 4810 errno = err;
4729 4811 zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4730 4812 zonecfg_fini_handle(handle);
4731 4813 } else {
4732 4814 zonecfg_rm_detached(handle, force);
4733 4815 zonecfg_fini_handle(handle);
4734 4816 }
4735 4817
4736 4818 if (err == Z_OK &&
4737 4819 (err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
4738 4820 errno = err;
4739 4821 zperror(gettext("could not reset state"), B_TRUE);
4740 4822 }
4741 4823
4742 4824 assert(zonecfg_lock_file_held(&lockfd));
4743 4825 zonecfg_release_lock_file(target_zone, lockfd);
4744 4826 lockfd = -1;
4745 4827
4746 4828 /* If we have a brand postattach hook, run it. */
4747 4829 if (err == Z_OK && !force && postcmdbuf[0] != '\0') {
4748 4830 status = do_subproc(postcmdbuf);
4749 4831 if (subproc_status(gettext("brand-specific postattach"),
4750 4832 status, B_FALSE) != ZONE_SUBPROC_OK) {
4751 4833 if ((err = zone_set_state(target_zone,
4752 4834 ZONE_STATE_CONFIGURED)) != Z_OK) {
4753 4835 errno = err;
4754 4836 zperror(gettext("could not reset state"),
4755 4837 B_TRUE);
4756 4838 }
4757 4839 }
4758 4840 }
4759 4841
4760 4842 assert(lockfd == -1);
4761 4843 return ((err == Z_OK) ? Z_OK : Z_ERR);
4762 4844 }
4763 4845
4764 4846 /*
4765 4847 * On input, TRUE => yes, FALSE => no.
4766 4848 * On return, TRUE => 1, FALSE => 0, could not ask => -1.
4767 4849 */
4768 4850
4769 4851 static int
4770 4852 ask_yesno(boolean_t default_answer, const char *question)
4771 4853 {
4772 4854 char line[64]; /* should be large enough to answer yes or no */
4773 4855
4774 4856 if (!isatty(STDIN_FILENO))
4775 4857 return (-1);
4776 4858 for (;;) {
4777 4859 (void) printf("%s (%s)? ", question,
4778 4860 default_answer ? "[y]/n" : "y/[n]");
4779 4861 if (fgets(line, sizeof (line), stdin) == NULL ||
4780 4862 line[0] == '\n')
4781 4863 return (default_answer ? 1 : 0);
4782 4864 if (tolower(line[0]) == 'y')
4783 4865 return (1);
4784 4866 if (tolower(line[0]) == 'n')
4785 4867 return (0);
4786 4868 }
4787 4869 }
4788 4870
4789 4871 /* ARGSUSED */
4790 4872 static int
4791 4873 uninstall_func(int argc, char *argv[])
4792 4874 {
4793 4875 char line[ZONENAME_MAX + 128]; /* Enough for "Are you sure ..." */
4794 4876 char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN];
4795 4877 char cmdbuf[MAXPATHLEN];
4796 4878 char precmdbuf[MAXPATHLEN];
4797 4879 boolean_t force = B_FALSE;
4798 4880 int lockfd, answer;
4799 4881 int err, arg;
4800 4882 boolean_t brand_help = B_FALSE;
4801 4883 brand_handle_t bh = NULL;
4802 4884 int status;
4803 4885
4804 4886 if (zonecfg_in_alt_root()) {
4805 4887 zerror(gettext("cannot uninstall zone in alternate root"));
4806 4888 return (Z_ERR);
4807 4889 }
4808 4890
4809 4891 /* Check the argv string for args we handle internally */
4810 4892 optind = 0;
4811 4893 opterr = 0;
4812 4894 while ((arg = getopt(argc, argv, "?F")) != EOF) {
4813 4895 switch (arg) {
4814 4896 case '?':
4815 4897 if (optopt == '?') {
4816 4898 sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
4817 4899 brand_help = B_TRUE;
4818 4900 }
4819 4901 /* Ignore unknown options - may be brand specific. */
4820 4902 break;
4821 4903 case 'F':
4822 4904 force = B_TRUE;
4823 4905 break;
4824 4906 default:
4825 4907 /* Ignore unknown options - may be brand specific. */
4826 4908 break;
4827 4909 }
4828 4910 }
4829 4911
4830 4912 if (!brand_help) {
4831 4913 if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE,
4832 4914 B_FALSE) != Z_OK)
4833 4915 return (Z_ERR);
4834 4916
4835 4917 /*
4836 4918 * Invoke brand-specific handler.
4837 4919 */
4838 4920 if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK)
4839 4921 return (Z_ERR);
4840 4922
4841 4923 if (!force) {
4842 4924 (void) snprintf(line, sizeof (line),
4843 4925 gettext("Are you sure you want to %s zone %s"),
4844 4926 cmd_to_str(CMD_UNINSTALL), target_zone);
4845 4927 if ((answer = ask_yesno(B_FALSE, line)) == 0) {
4846 4928 return (Z_OK);
4847 4929 } else if (answer == -1) {
4848 4930 zerror(gettext("Input not from terminal and -F "
4849 4931 "not specified: %s not done."),
4850 4932 cmd_to_str(CMD_UNINSTALL));
4851 4933 return (Z_ERR);
4852 4934 }
4853 4935 }
4854 4936 }
4855 4937
4856 4938 if ((err = zone_get_zonepath(target_zone, zonepath,
4857 4939 sizeof (zonepath))) != Z_OK) {
4858 4940 errno = err;
4859 4941 zperror2(target_zone, gettext("could not get zone path"));
4860 4942 return (Z_ERR);
4861 4943 }
4862 4944
4863 4945 /*
4864 4946 * Fetch the uninstall and preuninstall hooks from the brand
4865 4947 * configuration.
4866 4948 */
4867 4949 if ((bh = brand_open(target_brand)) == NULL) {
4868 4950 zerror(gettext("missing or invalid brand"));
4869 4951 return (Z_ERR);
4870 4952 }
4871 4953
4872 4954 if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_uninstall,
4873 4955 target_zone, zonepath) != Z_OK) {
4874 4956 zerror("invalid brand configuration: missing uninstall "
4875 4957 "resource");
4876 4958 brand_close(bh);
4877 4959 return (Z_ERR);
4878 4960 }
4879 4961
4880 4962 if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_preuninstall,
4881 4963 target_zone, zonepath) != Z_OK) {
4882 4964 zerror("invalid brand configuration: missing preuninstall "
4883 4965 "resource");
4884 4966 brand_close(bh);
4885 4967 return (Z_ERR);
4886 4968 }
4887 4969 brand_close(bh);
4888 4970
4889 4971 /* Append all options to preuninstall hook. */
4890 4972 if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
4891 4973 return (Z_ERR);
4892 4974
4893 4975 /* Append all options to uninstall hook. */
4894 4976 if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
4895 4977 return (Z_ERR);
4896 4978
4897 4979 if (!brand_help) {
4898 4980 if ((err = zone_get_rootpath(target_zone, rootpath,
4899 4981 sizeof (rootpath))) != Z_OK) {
4900 4982 errno = err;
4901 4983 zperror2(target_zone, gettext("could not get root "
4902 4984 "path"));
4903 4985 return (Z_ERR);
4904 4986 }
4905 4987
4906 4988 /*
4907 4989 * If there seems to be a zoneadmd running for this zone, call
4908 4990 * it to tell it that an uninstall is happening; if all goes
4909 4991 * well it will then shut itself down.
4910 4992 */
4911 4993 if (zonecfg_ping_zoneadmd(target_zone) == Z_OK) {
4912 4994 zone_cmd_arg_t zarg;
4913 4995 zarg.cmd = Z_NOTE_UNINSTALLING;
4914 4996 /* we don't care too much if this fails, just plow on */
4915 4997 (void) zonecfg_call_zoneadmd(target_zone, &zarg, locale,
4916 4998 B_TRUE);
4917 4999 }
4918 5000
4919 5001 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
4920 5002 zerror(gettext("another %s may have an operation in "
4921 5003 "progress."), "zoneadm");
4922 5004 return (Z_ERR);
4923 5005 }
4924 5006
4925 5007 /* Don't uninstall the zone if anything is mounted there */
4926 5008 err = zonecfg_find_mounts(rootpath, NULL, NULL);
4927 5009 if (err) {
4928 5010 zerror(gettext("These file systems are mounted on "
4929 5011 "subdirectories of %s.\n"), rootpath);
4930 5012 (void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
4931 5013 zonecfg_release_lock_file(target_zone, lockfd);
4932 5014 return (Z_ERR);
4933 5015 }
4934 5016 }
4935 5017
4936 5018 /* If we have a brand preuninstall hook, run it. */
4937 5019 if (!brand_help && precmdbuf[0] != '\0') {
4938 5020 status = do_subproc(precmdbuf);
4939 5021 if (subproc_status(gettext("brand-specific preuninstall"),
4940 5022 status, B_FALSE) != ZONE_SUBPROC_OK) {
4941 5023 zonecfg_release_lock_file(target_zone, lockfd);
4942 5024 return (Z_ERR);
4943 5025 }
4944 5026 }
4945 5027
4946 5028 if (!brand_help) {
4947 5029 err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
4948 5030 if (err != Z_OK) {
4949 5031 errno = err;
4950 5032 zperror2(target_zone, gettext("could not set state"));
4951 5033 goto bad;
4952 5034 }
4953 5035 }
4954 5036
4955 5037 /*
4956 5038 * If there is a brand uninstall hook, use it, otherwise use the
4957 5039 * built-in uninstall code.
4958 5040 */
4959 5041 if (cmdbuf[0] != '\0') {
4960 5042 /* Run the uninstall hook */
4961 5043 status = do_subproc(cmdbuf);
4962 5044 if ((status = subproc_status(gettext("brand-specific "
4963 5045 "uninstall"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
4964 5046 if (status == ZONE_SUBPROC_USAGE && !brand_help)
4965 5047 sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
4966 5048 if (!brand_help)
4967 5049 zonecfg_release_lock_file(target_zone, lockfd);
4968 5050 return (Z_ERR);
4969 5051 }
4970 5052
4971 5053 if (brand_help)
4972 5054 return (Z_OK);
4973 5055 } else {
4974 5056 /* If just help, we're done since there is no brand help. */
4975 5057 if (brand_help)
4976 5058 return (Z_OK);
4977 5059
4978 5060 /* Run the built-in uninstall support. */
4979 5061 if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
4980 5062 errno = err;
4981 5063 zperror2(target_zone, gettext("cleaning up zonepath "
4982 5064 "failed"));
4983 5065 goto bad;
4984 5066 }
4985 5067 }
4986 5068
4987 5069 err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
4988 5070 if (err != Z_OK) {
4989 5071 errno = err;
4990 5072 zperror2(target_zone, gettext("could not reset state"));
4991 5073 }
4992 5074 bad:
4993 5075 zonecfg_release_lock_file(target_zone, lockfd);
4994 5076 return (err);
4995 5077 }
4996 5078
4997 5079 /* ARGSUSED */
4998 5080 static int
4999 5081 mount_func(int argc, char *argv[])
5000 5082 {
5001 5083 zone_cmd_arg_t zarg;
5002 5084 boolean_t force = B_FALSE;
5003 5085 int arg;
5004 5086
5005 5087 /*
5006 5088 * The only supported subargument to the "mount" subcommand is
5007 5089 * "-f", which forces us to mount a zone in the INCOMPLETE state.
5008 5090 */
5009 5091 optind = 0;
5010 5092 if ((arg = getopt(argc, argv, "f")) != EOF) {
5011 5093 switch (arg) {
5012 5094 case 'f':
5013 5095 force = B_TRUE;
5014 5096 break;
5015 5097 default:
5016 5098 return (Z_USAGE);
5017 5099 }
5018 5100 }
5019 5101 if (argc > optind)
5020 5102 return (Z_USAGE);
5021 5103
5022 5104 if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force)
5023 5105 != Z_OK)
5024 5106 return (Z_ERR);
5025 5107 if (verify_details(CMD_MOUNT, argv) != Z_OK)
5026 5108 return (Z_ERR);
5027 5109
5028 5110 zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT;
5029 5111 zarg.bootbuf[0] = '\0';
5030 5112 if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5031 5113 zerror(gettext("call to %s failed"), "zoneadmd");
5032 5114 return (Z_ERR);
5033 5115 }
5034 5116 return (Z_OK);
5035 5117 }
5036 5118
5037 5119 /* ARGSUSED */
5038 5120 static int
5039 5121 unmount_func(int argc, char *argv[])
5040 5122 {
5041 5123 zone_cmd_arg_t zarg;
5042 5124
5043 5125 if (argc > 0)
5044 5126 return (Z_USAGE);
5045 5127 if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE)
5046 5128 != Z_OK)
5047 5129 return (Z_ERR);
5048 5130
5049 5131 zarg.cmd = Z_UNMOUNT;
5050 5132 if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5051 5133 zerror(gettext("call to %s failed"), "zoneadmd");
5052 5134 return (Z_ERR);
5053 5135 }
5054 5136 return (Z_OK);
5055 5137 }
5056 5138
5057 5139 static int
5058 5140 mark_func(int argc, char *argv[])
5059 5141 {
5060 5142 int err, lockfd;
5061 5143 int arg;
5062 5144 boolean_t force = B_FALSE;
5063 5145 int state;
5064 5146
5065 5147 optind = 0;
5066 5148 opterr = 0;
5067 5149 while ((arg = getopt(argc, argv, "F")) != EOF) {
5068 5150 switch (arg) {
5069 5151 case 'F':
5070 5152 force = B_TRUE;
5071 5153 break;
5072 5154 default:
5073 5155 return (Z_USAGE);
5074 5156 }
5075 5157 }
5076 5158
5077 5159 if (argc != (optind + 1))
5078 5160 return (Z_USAGE);
5079 5161
5080 5162 if (strcmp(argv[optind], "configured") == 0)
5081 5163 state = ZONE_STATE_CONFIGURED;
5082 5164 else if (strcmp(argv[optind], "incomplete") == 0)
5083 5165 state = ZONE_STATE_INCOMPLETE;
5084 5166 else if (strcmp(argv[optind], "installed") == 0)
5085 5167 state = ZONE_STATE_INSTALLED;
5086 5168 else
5087 5169 return (Z_USAGE);
5088 5170
5089 5171 if (state != ZONE_STATE_INCOMPLETE && !force)
5090 5172 return (Z_USAGE);
5091 5173
5092 5174 if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_TRUE, B_FALSE)
5093 5175 != Z_OK)
5094 5176 return (Z_ERR);
5095 5177
5096 5178 /*
5097 5179 * Invoke brand-specific handler.
5098 5180 */
5099 5181 if (invoke_brand_handler(CMD_MARK, argv) != Z_OK)
5100 5182 return (Z_ERR);
5101 5183
5102 5184 if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
5103 5185 zerror(gettext("another %s may have an operation in progress."),
5104 5186 "zoneadm");
5105 5187 return (Z_ERR);
5106 5188 }
5107 5189
5108 5190 err = zone_set_state(target_zone, state);
5109 5191 if (err != Z_OK) {
5110 5192 errno = err;
5111 5193 zperror2(target_zone, gettext("could not set state"));
5112 5194 }
5113 5195 zonecfg_release_lock_file(target_zone, lockfd);
5114 5196
5115 5197 return (err);
5116 5198 }
5117 5199
5118 5200 /*
5119 5201 * Check what scheduling class we're running under and print a warning if
5120 5202 * we're not using FSS.
5121 5203 */
5122 5204 static int
5123 5205 check_sched_fss(zone_dochandle_t handle)
5124 5206 {
5125 5207 char class_name[PC_CLNMSZ];
5126 5208
5127 5209 if (zonecfg_get_dflt_sched_class(handle, class_name,
5128 5210 sizeof (class_name)) != Z_OK) {
5129 5211 zerror(gettext("WARNING: unable to determine the zone's "
5130 5212 "scheduling class"));
5131 5213 } else if (strcmp("FSS", class_name) != 0) {
5132 5214 zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n"
5133 5215 "FSS is not the default scheduling class for this zone. "
5134 5216 "FSS will be\nused for processes in the zone but to get "
5135 5217 "the full benefit of FSS,\nit should be the default "
5136 5218 "scheduling class. See dispadmin(1M) for\nmore details."));
5137 5219 return (Z_SYSTEM);
5138 5220 }
5139 5221
5140 5222 return (Z_OK);
5141 5223 }
5142 5224
5143 5225 static int
5144 5226 check_cpu_shares_sched(zone_dochandle_t handle)
5145 5227 {
5146 5228 int err;
5147 5229 int res = Z_OK;
5148 5230 struct zone_rctltab rctl;
5149 5231
5150 5232 if ((err = zonecfg_setrctlent(handle)) != Z_OK) {
5151 5233 errno = err;
5152 5234 zperror(cmd_to_str(CMD_APPLY), B_TRUE);
5153 5235 return (err);
5154 5236 }
5155 5237
5156 5238 while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
5157 5239 if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) {
5158 5240 if (check_sched_fss(handle) != Z_OK)
5159 5241 res = Z_SYSTEM;
5160 5242 break;
5161 5243 }
5162 5244 }
5163 5245
5164 5246 (void) zonecfg_endrctlent(handle);
5165 5247
5166 5248 return (res);
5167 5249 }
5168 5250
5169 5251 /*
5170 5252 * Check if there is a mix of processes running in different pools within the
5171 5253 * zone. This is currently only going to be called for the global zone from
5172 5254 * apply_func but that could be generalized in the future.
5173 5255 */
5174 5256 static boolean_t
5175 5257 mixed_pools(zoneid_t zoneid)
5176 5258 {
5177 5259 DIR *dirp;
5178 5260 dirent_t *dent;
5179 5261 boolean_t mixed = B_FALSE;
5180 5262 boolean_t poolid_set = B_FALSE;
5181 5263 poolid_t last_poolid = 0;
5182 5264
5183 5265 if ((dirp = opendir("/proc")) == NULL) {
5184 5266 zerror(gettext("could not open /proc"));
5185 5267 return (B_FALSE);
5186 5268 }
5187 5269
5188 5270 while ((dent = readdir(dirp)) != NULL) {
5189 5271 int procfd;
5190 5272 psinfo_t ps;
5191 5273 char procpath[MAXPATHLEN];
5192 5274
5193 5275 if (dent->d_name[0] == '.')
5194 5276 continue;
5195 5277
5196 5278 (void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo",
5197 5279 dent->d_name);
5198 5280
5199 5281 if ((procfd = open(procpath, O_RDONLY)) == -1)
5200 5282 continue;
5201 5283
5202 5284 if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) {
5203 5285 /* skip processes in other zones and system processes */
5204 5286 if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) {
5205 5287 (void) close(procfd);
5206 5288 continue;
5207 5289 }
5208 5290
5209 5291 if (poolid_set) {
5210 5292 if (ps.pr_poolid != last_poolid)
5211 5293 mixed = B_TRUE;
5212 5294 } else {
5213 5295 last_poolid = ps.pr_poolid;
5214 5296 poolid_set = B_TRUE;
5215 5297 }
5216 5298 }
5217 5299
5218 5300 (void) close(procfd);
5219 5301
5220 5302 if (mixed)
5221 5303 break;
5222 5304 }
5223 5305
5224 5306 (void) closedir(dirp);
5225 5307
5226 5308 return (mixed);
5227 5309 }
5228 5310
5229 5311 /*
5230 5312 * Check if a persistent or temporary pool is configured for the zone.
5231 5313 * This is currently only going to be called for the global zone from
5232 5314 * apply_func but that could be generalized in the future.
5233 5315 */
5234 5316 static boolean_t
5235 5317 pool_configured(zone_dochandle_t handle)
5236 5318 {
5237 5319 int err1, err2;
5238 5320 struct zone_psettab pset_tab;
5239 5321 char poolname[MAXPATHLEN];
5240 5322
5241 5323 err1 = zonecfg_lookup_pset(handle, &pset_tab);
5242 5324 err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname));
5243 5325
5244 5326 if (err1 == Z_NO_ENTRY &&
5245 5327 (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0)))
5246 5328 return (B_FALSE);
5247 5329
5248 5330 return (B_TRUE);
5249 5331 }
5250 5332
5251 5333 /*
5252 5334 * This is an undocumented interface which is currently only used to apply
5253 5335 * the global zone resource management settings when the system boots.
5254 5336 * This function does not yet properly handle updating a running system so
5255 5337 * any projects running in the zone would be trashed if this function
5256 5338 * were to run after the zone had booted. It also does not reset any
5257 5339 * rctl settings that were removed from zonecfg. There is still work to be
5258 5340 * done before we can properly support dynamically updating the resource
5259 5341 * management settings for a running zone (global or non-global). Thus, this
5260 5342 * functionality is undocumented for now.
5261 5343 */
5262 5344 /* ARGSUSED */
5263 5345 static int
5264 5346 apply_func(int argc, char *argv[])
5265 5347 {
5266 5348 int err;
5267 5349 int res = Z_OK;
5268 5350 priv_set_t *privset;
5269 5351 zoneid_t zoneid;
5270 5352 zone_dochandle_t handle;
5271 5353 struct zone_mcaptab mcap;
5272 5354 char pool_err[128];
5273 5355
5274 5356 zoneid = getzoneid();
5275 5357
5276 5358 if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID ||
5277 5359 target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0)
5278 5360 return (usage(B_FALSE));
5279 5361
5280 5362 if ((privset = priv_allocset()) == NULL) {
5281 5363 zerror(gettext("%s failed"), "priv_allocset");
5282 5364 return (Z_ERR);
5283 5365 }
5284 5366
5285 5367 if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
5286 5368 zerror(gettext("%s failed"), "getppriv");
5287 5369 priv_freeset(privset);
5288 5370 return (Z_ERR);
5289 5371 }
5290 5372
5291 5373 if (priv_isfullset(privset) == B_FALSE) {
5292 5374 (void) usage(B_FALSE);
5293 5375 priv_freeset(privset);
5294 5376 return (Z_ERR);
5295 5377 }
5296 5378 priv_freeset(privset);
5297 5379
5298 5380 if ((handle = zonecfg_init_handle()) == NULL) {
5299 5381 zperror(cmd_to_str(CMD_APPLY), B_TRUE);
5300 5382 return (Z_ERR);
5301 5383 }
5302 5384
5303 5385 if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
5304 5386 errno = err;
5305 5387 zperror(cmd_to_str(CMD_APPLY), B_TRUE);
5306 5388 zonecfg_fini_handle(handle);
5307 5389 return (Z_ERR);
5308 5390 }
5309 5391
5310 5392 /* specific error msgs are printed within apply_rctls */
5311 5393 if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) {
5312 5394 errno = err;
5313 5395 zperror(cmd_to_str(CMD_APPLY), B_TRUE);
5314 5396 res = Z_ERR;
5315 5397 }
5316 5398
5317 5399 if ((err = check_cpu_shares_sched(handle)) != Z_OK)
5318 5400 res = Z_ERR;
5319 5401
5320 5402 if (pool_configured(handle)) {
5321 5403 if (mixed_pools(zoneid)) {
5322 5404 zerror(gettext("Zone is using multiple resource "
5323 5405 "pools. The pool\nconfiguration cannot be "
5324 5406 "applied without rebooting."));
5325 5407 res = Z_ERR;
5326 5408 } else {
5327 5409
5328 5410 /*
5329 5411 * The next two blocks of code attempt to set up
5330 5412 * temporary pools as well as persistent pools. In
5331 5413 * both cases we call the functions unconditionally.
5332 5414 * Within each funtion the code will check if the zone
5333 5415 * is actually configured for a temporary pool or
5334 5416 * persistent pool and just return if there is nothing
5335 5417 * to do.
5336 5418 */
5337 5419 if ((err = zonecfg_bind_tmp_pool(handle, zoneid,
5338 5420 pool_err, sizeof (pool_err))) != Z_OK) {
5339 5421 if (err == Z_POOL || err == Z_POOL_CREATE ||
5340 5422 err == Z_POOL_BIND)
5341 5423 zerror("%s: %s", zonecfg_strerror(err),
5342 5424 pool_err);
5343 5425 else
5344 5426 zerror(gettext("could not bind zone to "
5345 5427 "temporary pool: %s"),
5346 5428 zonecfg_strerror(err));
5347 5429 res = Z_ERR;
5348 5430 }
5349 5431
5350 5432 if ((err = zonecfg_bind_pool(handle, zoneid, pool_err,
5351 5433 sizeof (pool_err))) != Z_OK) {
5352 5434 if (err == Z_POOL || err == Z_POOL_BIND)
5353 5435 zerror("%s: %s", zonecfg_strerror(err),
5354 5436 pool_err);
5355 5437 else
5356 5438 zerror("%s", zonecfg_strerror(err));
5357 5439 }
5358 5440 }
5359 5441 }
5360 5442
5361 5443 /*
5362 5444 * If a memory cap is configured, set the cap in the kernel using
5363 5445 * zone_setattr() and make sure the rcapd SMF service is enabled.
5364 5446 */
5365 5447 if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
5366 5448 uint64_t num;
5367 5449 char smf_err[128];
5368 5450
5369 5451 num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10);
5370 5452 if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
5371 5453 zerror(gettext("could not set zone memory cap"));
5372 5454 res = Z_ERR;
5373 5455 }
5374 5456
5375 5457 if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
5376 5458 zerror(gettext("enabling system/rcap service failed: "
5377 5459 "%s"), smf_err);
5378 5460 res = Z_ERR;
5379 5461 }
5380 5462 }
5381 5463
5382 5464 zonecfg_fini_handle(handle);
5383 5465
5384 5466 return (res);
5385 5467 }
5386 5468
5387 5469 /*
5388 5470 * This is an undocumented interface that is invoked by the zones SMF service
5389 5471 * for installed zones that won't automatically boot.
5390 5472 */
5391 5473 /* ARGSUSED */
5392 5474 static int
5393 5475 sysboot_func(int argc, char *argv[])
5394 5476 {
5395 5477 int err;
5396 5478 zone_dochandle_t zone_handle;
5397 5479 brand_handle_t brand_handle;
5398 5480 char cmdbuf[MAXPATHLEN];
5399 5481 char zonepath[MAXPATHLEN];
5400 5482
5401 5483 /*
5402 5484 * This subcommand can only be executed in the global zone on non-global
5403 5485 * zones.
5404 5486 */
5405 5487 if (zonecfg_in_alt_root())
5406 5488 return (usage(B_FALSE));
5407 5489 if (sanity_check(target_zone, CMD_SYSBOOT, B_FALSE, B_TRUE, B_FALSE) !=
5408 5490 Z_OK)
5409 5491 return (Z_ERR);
5410 5492
5411 5493 /*
5412 5494 * Fetch the sysboot hook from the target zone's brand.
5413 5495 */
5414 5496 if ((zone_handle = zonecfg_init_handle()) == NULL) {
5415 5497 zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
5416 5498 return (Z_ERR);
5417 5499 }
5418 5500 if ((err = zonecfg_get_handle(target_zone, zone_handle)) != Z_OK) {
5419 5501 errno = err;
5420 5502 zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
5421 5503 zonecfg_fini_handle(zone_handle);
5422 5504 return (Z_ERR);
5423 5505 }
5424 5506 if ((err = zonecfg_get_zonepath(zone_handle, zonepath,
5425 5507 sizeof (zonepath))) != Z_OK) {
5426 5508 errno = err;
5427 5509 zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
5428 5510 zonecfg_fini_handle(zone_handle);
5429 5511 return (Z_ERR);
5430 5512 }
5431 5513 if ((brand_handle = brand_open(target_brand)) == NULL) {
5432 5514 zerror(gettext("missing or invalid brand during %s operation: "
5433 5515 "%s"), cmd_to_str(CMD_SYSBOOT), target_brand);
5434 5516 zonecfg_fini_handle(zone_handle);
5435 5517 return (Z_ERR);
5436 5518 }
5437 5519 err = get_hook(brand_handle, cmdbuf, sizeof (cmdbuf), brand_get_sysboot,
5438 5520 target_zone, zonepath);
5439 5521 brand_close(brand_handle);
5440 5522 zonecfg_fini_handle(zone_handle);
5441 5523 if (err != Z_OK) {
5442 5524 zerror(gettext("unable to get brand hook from brand %s for %s "
5443 5525 "operation"), target_brand, cmd_to_str(CMD_SYSBOOT));
5444 5526 return (Z_ERR);
5445 5527 }
5446 5528
5447 5529 /*
5448 5530 * If the hook wasn't defined (which is OK), then indicate success and
5449 5531 * return. Otherwise, execute the hook.
5450 5532 */
5451 5533 if (cmdbuf[0] != '\0')
5452 5534 return ((subproc_status(gettext("brand sysboot operation"),
5453 5535 do_subproc(cmdbuf), B_FALSE) == ZONE_SUBPROC_OK) ? Z_OK :
5454 5536 Z_BRAND_ERROR);
5455 5537 return (Z_OK);
5456 5538 }
5457 5539
5458 5540 static int
5459 5541 help_func(int argc, char *argv[])
5460 5542 {
5461 5543 int arg, cmd_num;
5462 5544
5463 5545 if (argc == 0) {
5464 5546 (void) usage(B_TRUE);
5465 5547 return (Z_OK);
5466 5548 }
5467 5549 optind = 0;
5468 5550 if ((arg = getopt(argc, argv, "?")) != EOF) {
5469 5551 switch (arg) {
5470 5552 case '?':
5471 5553 sub_usage(SHELP_HELP, CMD_HELP);
5472 5554 return (optopt == '?' ? Z_OK : Z_USAGE);
5473 5555 default:
5474 5556 sub_usage(SHELP_HELP, CMD_HELP);
5475 5557 return (Z_USAGE);
5476 5558 }
5477 5559 }
5478 5560 while (optind < argc) {
5479 5561 /* Private commands have NULL short_usage; omit them */
5480 5562 if ((cmd_num = cmd_match(argv[optind])) < 0 ||
5481 5563 cmdtab[cmd_num].short_usage == NULL) {
5482 5564 sub_usage(SHELP_HELP, CMD_HELP);
5483 5565 return (Z_USAGE);
5484 5566 }
5485 5567 sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
5486 5568 optind++;
5487 5569 }
5488 5570 return (Z_OK);
5489 5571 }
5490 5572
5491 5573 /*
5492 5574 * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
5493 5575 */
5494 5576
5495 5577 static int
5496 5578 cmd_match(char *cmd)
5497 5579 {
5498 5580 int i;
5499 5581
5500 5582 for (i = CMD_MIN; i <= CMD_MAX; i++) {
5501 5583 /* return only if there is an exact match */
5502 5584 if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
5503 5585 return (cmdtab[i].cmd_num);
5504 5586 }
5505 5587 return (-1);
5506 5588 }
5507 5589
5508 5590 static int
5509 5591 parse_and_run(int argc, char *argv[])
5510 5592 {
5511 5593 int i = cmd_match(argv[0]);
5512 5594
5513 5595 if (i < 0)
5514 5596 return (usage(B_FALSE));
5515 5597 return (cmdtab[i].handler(argc - 1, &(argv[1])));
5516 5598 }
5517 5599
5518 5600 static char *
5519 5601 get_execbasename(char *execfullname)
5520 5602 {
5521 5603 char *last_slash, *execbasename;
5522 5604
5523 5605 /* guard against '/' at end of command invocation */
5524 5606 for (;;) {
5525 5607 last_slash = strrchr(execfullname, '/');
5526 5608 if (last_slash == NULL) {
5527 5609 execbasename = execfullname;
5528 5610 break;
5529 5611 } else {
5530 5612 execbasename = last_slash + 1;
5531 5613 if (*execbasename == '\0') {
5532 5614 *last_slash = '\0';
5533 5615 continue;
5534 5616 }
5535 5617 break;
5536 5618 }
5537 5619 }
5538 5620 return (execbasename);
5539 5621 }
5540 5622
5541 5623 static char *
5542 5624 get_username()
5543 5625 {
5544 5626 uid_t uid;
5545 5627 struct passwd *nptr;
5546 5628
5547 5629
5548 5630 /*
5549 5631 * Authorizations are checked to restrict access based on the
5550 5632 * requested operation and zone name, It is assumed that the
5551 5633 * program is running with all privileges, but that the real
5552 5634 * user ID is that of the user or role on whose behalf we are
5553 5635 * operating. So we start by getting the username that will be
5554 5636 * used for subsequent authorization checks.
5555 5637 */
5556 5638
5557 5639 uid = getuid();
5558 5640 if ((nptr = getpwuid(uid)) == NULL) {
5559 5641 zerror(gettext("could not get user name."));
5560 5642 exit(Z_ERR);
5561 5643 }
5562 5644 return (nptr->pw_name);
5563 5645 }
5564 5646
5565 5647 int
5566 5648 main(int argc, char **argv)
5567 5649 {
5568 5650 int arg;
5569 5651 zoneid_t zid;
5570 5652 struct stat st;
5571 5653 char *zone_lock_env;
5572 5654 int err;
5573 5655
5574 5656 if ((locale = setlocale(LC_ALL, "")) == NULL)
5575 5657 locale = "C";
5576 5658 (void) textdomain(TEXT_DOMAIN);
5577 5659 setbuf(stdout, NULL);
5578 5660 (void) sigset(SIGHUP, SIG_IGN);
5579 5661 execname = get_execbasename(argv[0]);
5580 5662 username = get_username();
5581 5663 target_zone = NULL;
5582 5664 if (chdir("/") != 0) {
5583 5665 zerror(gettext("could not change directory to /."));
5584 5666 exit(Z_ERR);
5585 5667 }
5586 5668
5587 5669 /*
5588 5670 * Use the default system mask rather than anything that may have been
5589 5671 * set by the caller.
5590 5672 */
5591 5673 (void) umask(CMASK);
5592 5674
5593 5675 if (init_zfs() != Z_OK)
5594 5676 exit(Z_ERR);
5595 5677
5596 5678 while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) {
5597 5679 switch (arg) {
5598 5680 case '?':
5599 5681 return (usage(B_TRUE));
5600 5682 case 'u':
5601 5683 target_uuid = optarg;
5602 5684 break;
5603 5685 case 'z':
5604 5686 target_zone = optarg;
5605 5687 break;
5606 5688 case 'R': /* private option for admin/install use */
5607 5689 if (*optarg != '/') {
5608 5690 zerror(gettext("root path must be absolute."));
5609 5691 exit(Z_ERR);
5610 5692 }
5611 5693 if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
5612 5694 zerror(
5613 5695 gettext("root path must be a directory."));
5614 5696 exit(Z_ERR);
5615 5697 }
5616 5698 zonecfg_set_root(optarg);
5617 5699 break;
5618 5700 default:
5619 5701 return (usage(B_FALSE));
5620 5702 }
5621 5703 }
5622 5704
5623 5705 if (optind >= argc)
5624 5706 return (usage(B_FALSE));
5625 5707
5626 5708 if (target_uuid != NULL && *target_uuid != '\0') {
5627 5709 uuid_t uuid;
5628 5710 static char newtarget[ZONENAME_MAX];
5629 5711
5630 5712 if (uuid_parse(target_uuid, uuid) == -1) {
5631 5713 zerror(gettext("illegal UUID value specified"));
5632 5714 exit(Z_ERR);
5633 5715 }
5634 5716 if (zonecfg_get_name_by_uuid(uuid, newtarget,
5635 5717 sizeof (newtarget)) == Z_OK)
5636 5718 target_zone = newtarget;
5637 5719 }
5638 5720
5639 5721 if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
5640 5722 errno = Z_NO_ZONE;
5641 5723 zperror(target_zone, B_TRUE);
5642 5724 exit(Z_ERR);
5643 5725 }
5644 5726
5645 5727 /*
5646 5728 * See if we have inherited the right to manipulate this zone from
5647 5729 * a zoneadm instance in our ancestry. If so, set zone_lock_cnt to
5648 5730 * indicate it. If not, make that explicit in our environment.
5649 5731 */
5650 5732 zonecfg_init_lock_file(target_zone, &zone_lock_env);
5651 5733
5652 5734 /* Figure out what the system's default brand is */
5653 5735 if (zonecfg_default_brand(default_brand,
5654 5736 sizeof (default_brand)) != Z_OK) {
5655 5737 zerror(gettext("unable to determine default brand"));
5656 5738 return (Z_ERR);
5657 5739 }
5658 5740
5659 5741 /*
5660 5742 * If we are going to be operating on a single zone, retrieve its
5661 5743 * brand type and determine whether it is native or not.
5662 5744 */
5663 5745 if ((target_zone != NULL) &&
5664 5746 (strcmp(target_zone, GLOBAL_ZONENAME) != 0)) {
5665 5747 if (zone_get_brand(target_zone, target_brand,
5666 5748 sizeof (target_brand)) != Z_OK) {
5667 5749 zerror(gettext("missing or invalid brand"));
5668 5750 exit(Z_ERR);
5669 5751 }
5670 5752 /*
5671 5753 * In the alternate root environment, the only supported
5672 5754 * operations are mount and unmount. In this case, just treat
5673 5755 * the zone as native if it is cluster. Cluster zones can be
5674 5756 * native for the purpose of LU or upgrade, and the cluster
5675 5757 * brand may not exist in the miniroot (such as in net install
5676 5758 * upgrade).
5677 5759 */
5678 5760 if (strcmp(target_brand, CLUSTER_BRAND_NAME) == 0) {
5679 5761 if (zonecfg_in_alt_root()) {
5680 5762 (void) strlcpy(target_brand, default_brand,
5681 5763 sizeof (target_brand));
5682 5764 }
5683 5765 }
5684 5766 }
5685 5767
5686 5768 err = parse_and_run(argc - optind, &argv[optind]);
5687 5769
5688 5770 return (err);
5689 5771 }
↓ open down ↓ |
3852 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX