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