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