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