Print this page
OS-1576 Sundry uninitialised variables
Reviewed by: Robert Mustacchi <rm@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/zoneadmd/vplat.c
+++ new/usr/src/cmd/zoneadmd/vplat.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 (c) 2012, Joyent Inc. All rights reserved.
25 25 */
26 26
27 27 /*
28 28 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
29 29 */
30 30
31 31 /*
32 32 * This module contains functions used to bring up and tear down the
33 33 * Virtual Platform: [un]mounting file-systems, [un]plumbing network
34 34 * interfaces, [un]configuring devices, establishing resource controls,
35 35 * and creating/destroying the zone in the kernel. These actions, on
36 36 * the way up, ready the zone; on the way down, they halt the zone.
37 37 * See the much longer block comment at the beginning of zoneadmd.c
38 38 * for a bigger picture of how the whole program functions.
39 39 *
40 40 * This module also has primary responsibility for the layout of "scratch
41 41 * zones." These are mounted, but inactive, zones that are used during
42 42 * operating system upgrade and potentially other administrative action. The
43 43 * scratch zone environment is similar to the miniroot environment. The zone's
44 44 * actual root is mounted read-write on /a, and the standard paths (/usr,
45 45 * /sbin, /lib) all lead to read-only copies of the running system's binaries.
46 46 * This allows the administrative tools to manipulate the zone using "-R /a"
47 47 * without relying on any binaries in the zone itself.
48 48 *
49 49 * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
50 50 * environment), then we must resolve the lofs mounts used there to uncover
51 51 * writable (unshared) resources. Shared resources, though, are always
52 52 * read-only. In addition, if the "same" zone with a different root path is
53 53 * currently running, then "/b" inside the zone points to the running zone's
54 54 * root. This allows LU to synchronize configuration files during the upgrade
55 55 * process.
56 56 *
57 57 * To construct this environment, this module creates a tmpfs mount on
58 58 * $ZONEPATH/lu. Inside this scratch area, the miniroot-like environment as
59 59 * described above is constructed on the fly. The zone is then created using
60 60 * $ZONEPATH/lu as the root.
61 61 *
62 62 * Note that scratch zones are inactive. The zone's bits are not running and
63 63 * likely cannot be run correctly until upgrade is done. Init is not running
64 64 * there, nor is SMF. Because of this, the "mounted" state of a scratch zone
65 65 * is not a part of the usual halt/ready/boot state machine.
66 66 */
67 67
68 68 #include <sys/param.h>
69 69 #include <sys/mount.h>
70 70 #include <sys/mntent.h>
71 71 #include <sys/socket.h>
72 72 #include <sys/utsname.h>
73 73 #include <sys/types.h>
74 74 #include <sys/stat.h>
75 75 #include <sys/sockio.h>
76 76 #include <sys/stropts.h>
77 77 #include <sys/conf.h>
78 78 #include <sys/systeminfo.h>
79 79
80 80 #include <libdlpi.h>
81 81 #include <libdllink.h>
82 82 #include <libdlvlan.h>
83 83
84 84 #include <inet/tcp.h>
85 85 #include <arpa/inet.h>
86 86 #include <netinet/in.h>
87 87 #include <net/route.h>
88 88
89 89 #include <stdio.h>
90 90 #include <errno.h>
91 91 #include <fcntl.h>
92 92 #include <unistd.h>
93 93 #include <rctl.h>
94 94 #include <stdlib.h>
95 95 #include <string.h>
96 96 #include <strings.h>
97 97 #include <wait.h>
98 98 #include <limits.h>
99 99 #include <libgen.h>
100 100 #include <libzfs.h>
101 101 #include <libdevinfo.h>
102 102 #include <zone.h>
103 103 #include <assert.h>
104 104 #include <libcontract.h>
105 105 #include <libcontract_priv.h>
106 106 #include <uuid/uuid.h>
107 107
108 108 #include <sys/mntio.h>
109 109 #include <sys/mnttab.h>
110 110 #include <sys/fs/autofs.h> /* for _autofssys() */
111 111 #include <sys/fs/lofs_info.h>
112 112 #include <sys/fs/zfs.h>
113 113
114 114 #include <pool.h>
115 115 #include <sys/pool.h>
116 116 #include <sys/priocntl.h>
117 117
118 118 #include <libbrand.h>
119 119 #include <sys/brand.h>
120 120 #include <libzonecfg.h>
121 121 #include <synch.h>
122 122
123 123 #include "zoneadmd.h"
124 124 #include <tsol/label.h>
125 125 #include <libtsnet.h>
126 126 #include <sys/priv.h>
127 127 #include <libinetutil.h>
128 128
129 129 #define V4_ADDR_LEN 32
130 130 #define V6_ADDR_LEN 128
131 131
132 132 #define RESOURCE_DEFAULT_OPTS \
133 133 MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
134 134
135 135 #define DFSTYPES "/etc/dfs/fstypes"
136 136 #define MAXTNZLEN 2048
137 137
138 138 #define ALT_MOUNT(mount_cmd) ((mount_cmd) != Z_MNT_BOOT)
139 139
140 140 /* a reasonable estimate for the number of lwps per process */
141 141 #define LWPS_PER_PROCESS 10
142 142
143 143 /* for routing socket */
144 144 static int rts_seqno = 0;
145 145
146 146 /* mangled zone name when mounting in an alternate root environment */
147 147 static char kernzone[ZONENAME_MAX];
148 148
149 149 /* array of cached mount entries for resolve_lofs */
150 150 static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
151 151
152 152 /* for Trusted Extensions */
153 153 static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
154 154 static int tsol_mounts(zlog_t *, char *, char *);
155 155 static void tsol_unmounts(zlog_t *, char *);
156 156
157 157 static m_label_t *zlabel = NULL;
158 158 static m_label_t *zid_label = NULL;
159 159 static priv_set_t *zprivs = NULL;
160 160
161 161 /* from libsocket, not in any header file */
162 162 extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
163 163
164 164 /* from zoneadmd */
165 165 extern char query_hook[];
166 166 extern char post_statechg_hook[];
167 167
168 168 /*
169 169 * For each "net" resource configured in zonecfg, we track a zone_addr_list_t
170 170 * node in a linked list that is sorted by linkid. The list is constructed as
171 171 * the xml configuration file is parsed, and the information
172 172 * contained in each node is added to the kernel before the zone is
173 173 * booted, to be retrieved and applied from within the exclusive-IP NGZ
174 174 * on boot.
175 175 */
176 176 typedef struct zone_addr_list {
177 177 struct zone_addr_list *za_next;
178 178 datalink_id_t za_linkid; /* datalink_id_t of interface */
179 179 struct zone_nwiftab za_nwiftab; /* address, defrouter properties */
180 180 } zone_addr_list_t;
181 181
182 182 /*
183 183 * An optimization for build_mnttable: reallocate (and potentially copy the
184 184 * data) only once every N times through the loop.
185 185 */
186 186 #define MNTTAB_HUNK 32
187 187
188 188 /* some handy macros */
189 189 #define SIN(s) ((struct sockaddr_in *)s)
190 190 #define SIN6(s) ((struct sockaddr_in6 *)s)
191 191
192 192 /*
193 193 * Private autofs system call
194 194 */
195 195 extern int _autofssys(int, void *);
196 196
197 197 static int
198 198 autofs_cleanup(zoneid_t zoneid)
199 199 {
200 200 /*
201 201 * Ask autofs to unmount all trigger nodes in the given zone.
202 202 */
203 203 return (_autofssys(AUTOFS_UNMOUNTALL, (void *)((uintptr_t)zoneid)));
204 204 }
205 205
206 206 static void
207 207 free_mnttable(struct mnttab *mnt_array, uint_t nelem)
208 208 {
209 209 uint_t i;
210 210
211 211 if (mnt_array == NULL)
212 212 return;
213 213 for (i = 0; i < nelem; i++) {
214 214 free(mnt_array[i].mnt_mountp);
215 215 free(mnt_array[i].mnt_fstype);
216 216 free(mnt_array[i].mnt_special);
217 217 free(mnt_array[i].mnt_mntopts);
218 218 assert(mnt_array[i].mnt_time == NULL);
219 219 }
220 220 free(mnt_array);
221 221 }
222 222
223 223 /*
224 224 * Build the mount table for the zone rooted at "zroot", storing the resulting
225 225 * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
226 226 * array in "nelemp".
227 227 */
228 228 static int
229 229 build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
230 230 struct mnttab **mnt_arrayp, uint_t *nelemp)
231 231 {
232 232 struct mnttab mnt;
233 233 struct mnttab *mnts;
234 234 struct mnttab *mnp;
235 235 uint_t nmnt;
236 236
237 237 rewind(mnttab);
238 238 resetmnttab(mnttab);
239 239 nmnt = 0;
240 240 mnts = NULL;
241 241 while (getmntent(mnttab, &mnt) == 0) {
242 242 struct mnttab *tmp_array;
243 243
244 244 if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
245 245 continue;
246 246 if (nmnt % MNTTAB_HUNK == 0) {
247 247 tmp_array = realloc(mnts,
248 248 (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
249 249 if (tmp_array == NULL) {
250 250 free_mnttable(mnts, nmnt);
251 251 return (-1);
252 252 }
253 253 mnts = tmp_array;
254 254 }
255 255 mnp = &mnts[nmnt++];
256 256
257 257 /*
258 258 * Zero out any fields we're not using.
259 259 */
260 260 (void) memset(mnp, 0, sizeof (*mnp));
261 261
262 262 if (mnt.mnt_special != NULL)
263 263 mnp->mnt_special = strdup(mnt.mnt_special);
264 264 if (mnt.mnt_mntopts != NULL)
265 265 mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
266 266 mnp->mnt_mountp = strdup(mnt.mnt_mountp);
267 267 mnp->mnt_fstype = strdup(mnt.mnt_fstype);
268 268 if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
269 269 (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
270 270 mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
271 271 zerror(zlogp, B_TRUE, "memory allocation failed");
272 272 free_mnttable(mnts, nmnt);
273 273 return (-1);
274 274 }
275 275 }
276 276 *mnt_arrayp = mnts;
277 277 *nelemp = nmnt;
278 278 return (0);
279 279 }
280 280
281 281 /*
282 282 * This is an optimization. The resolve_lofs function is used quite frequently
283 283 * to manipulate file paths, and on a machine with a large number of zones,
284 284 * there will be a huge number of mounted file systems. Thus, we trigger a
285 285 * reread of the list of mount points
286 286 */
287 287 static void
288 288 lofs_discard_mnttab(void)
289 289 {
290 290 free_mnttable(resolve_lofs_mnts,
291 291 resolve_lofs_mnt_max - resolve_lofs_mnts);
292 292 resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
293 293 }
294 294
295 295 static int
296 296 lofs_read_mnttab(zlog_t *zlogp)
297 297 {
298 298 FILE *mnttab;
299 299 uint_t nmnts;
300 300
301 301 if ((mnttab = fopen(MNTTAB, "r")) == NULL)
302 302 return (-1);
303 303 if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
304 304 &nmnts) == -1) {
305 305 (void) fclose(mnttab);
306 306 return (-1);
307 307 }
308 308 (void) fclose(mnttab);
309 309 resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
310 310 return (0);
311 311 }
312 312
313 313 /*
314 314 * This function loops over potential loopback mounts and symlinks in a given
315 315 * path and resolves them all down to an absolute path.
316 316 */
317 317 void
318 318 resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
319 319 {
320 320 int len, arlen;
321 321 const char *altroot;
322 322 char tmppath[MAXPATHLEN];
323 323 boolean_t outside_altroot;
324 324
325 325 if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
326 326 return;
327 327 tmppath[len] = '\0';
328 328 (void) strlcpy(path, tmppath, sizeof (tmppath));
329 329
330 330 /* This happens once per zoneadmd operation. */
331 331 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
332 332 return;
333 333
334 334 altroot = zonecfg_get_root();
335 335 arlen = strlen(altroot);
336 336 outside_altroot = B_FALSE;
337 337 for (;;) {
338 338 struct mnttab *mnp;
339 339
340 340 /* Search in reverse order to find longest match */
341 341 for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
342 342 mnp--) {
343 343 if (mnp->mnt_fstype == NULL ||
344 344 mnp->mnt_mountp == NULL ||
345 345 mnp->mnt_special == NULL)
346 346 continue;
347 347 len = strlen(mnp->mnt_mountp);
348 348 if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
349 349 (path[len] == '/' || path[len] == '\0'))
350 350 break;
351 351 }
352 352 if (mnp < resolve_lofs_mnts)
353 353 break;
354 354 /* If it's not a lofs then we're done */
355 355 if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
356 356 break;
357 357 if (outside_altroot) {
358 358 char *cp;
359 359 int olen = sizeof (MNTOPT_RO) - 1;
360 360
361 361 /*
362 362 * If we run into a read-only mount outside of the
363 363 * alternate root environment, then the user doesn't
364 364 * want this path to be made read-write.
365 365 */
366 366 if (mnp->mnt_mntopts != NULL &&
367 367 (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
368 368 NULL &&
369 369 (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
370 370 (cp[olen] == '\0' || cp[olen] == ',')) {
371 371 break;
372 372 }
373 373 } else if (arlen > 0 &&
374 374 (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
375 375 (mnp->mnt_special[arlen] != '\0' &&
376 376 mnp->mnt_special[arlen] != '/'))) {
377 377 outside_altroot = B_TRUE;
378 378 }
379 379 /* use temporary buffer because new path might be longer */
380 380 (void) snprintf(tmppath, sizeof (tmppath), "%s%s",
381 381 mnp->mnt_special, path + len);
382 382 if ((len = resolvepath(tmppath, path, pathlen)) == -1)
383 383 break;
384 384 path[len] = '\0';
385 385 }
386 386 }
387 387
388 388 /*
389 389 * For a regular mount, check if a replacement lofs mount is needed because the
390 390 * referenced device is already mounted somewhere.
391 391 */
392 392 static int
393 393 check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
394 394 {
395 395 struct mnttab *mnp;
396 396 zone_fsopt_t *optptr, *onext;
397 397
398 398 /* This happens once per zoneadmd operation. */
399 399 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
400 400 return (-1);
401 401
402 402 /*
403 403 * If this special node isn't already in use, then it's ours alone;
404 404 * no need to worry about conflicting mounts.
405 405 */
406 406 for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
407 407 mnp++) {
408 408 if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
409 409 break;
410 410 }
411 411 if (mnp >= resolve_lofs_mnt_max)
412 412 return (0);
413 413
414 414 /*
415 415 * Convert this duplicate mount into a lofs mount.
416 416 */
417 417 (void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
418 418 sizeof (fsptr->zone_fs_special));
419 419 (void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
420 420 sizeof (fsptr->zone_fs_type));
421 421 fsptr->zone_fs_raw[0] = '\0';
422 422
423 423 /*
424 424 * Discard all but one of the original options and set that to our
425 425 * default set of options used for resources.
426 426 */
427 427 optptr = fsptr->zone_fs_options;
428 428 if (optptr == NULL) {
429 429 optptr = malloc(sizeof (*optptr));
430 430 if (optptr == NULL) {
431 431 zerror(zlogp, B_TRUE, "cannot mount %s",
432 432 fsptr->zone_fs_dir);
433 433 return (-1);
434 434 }
435 435 } else {
436 436 while ((onext = optptr->zone_fsopt_next) != NULL) {
437 437 optptr->zone_fsopt_next = onext->zone_fsopt_next;
438 438 free(onext);
439 439 }
440 440 }
441 441 (void) strcpy(optptr->zone_fsopt_opt, RESOURCE_DEFAULT_OPTS);
442 442 optptr->zone_fsopt_next = NULL;
443 443 fsptr->zone_fs_options = optptr;
444 444 return (0);
445 445 }
446 446
447 447 int
448 448 make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
449 449 uid_t userid, gid_t groupid)
450 450 {
451 451 char path[MAXPATHLEN];
452 452 struct stat st;
453 453
454 454 if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
455 455 sizeof (path)) {
456 456 zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
457 457 subdir);
458 458 return (-1);
459 459 }
460 460
461 461 if (lstat(path, &st) == 0) {
462 462 /*
463 463 * We don't check the file mode since presumably the zone
464 464 * administrator may have had good reason to change the mode,
465 465 * and we don't need to second guess him.
466 466 */
467 467 if (!S_ISDIR(st.st_mode)) {
468 468 if (S_ISREG(st.st_mode)) {
469 469 /*
470 470 * Allow readonly mounts of /etc/ files; this
471 471 * is needed most by Trusted Extensions.
472 472 */
473 473 if (strncmp(subdir, "/etc/",
474 474 strlen("/etc/")) != 0) {
475 475 zerror(zlogp, B_FALSE,
476 476 "%s is not in /etc", path);
477 477 return (-1);
478 478 }
479 479 } else {
480 480 zerror(zlogp, B_FALSE,
481 481 "%s is not a directory", path);
482 482 return (-1);
483 483 }
484 484 }
485 485 return (0);
486 486 }
487 487
488 488 if (mkdirp(path, mode) != 0) {
489 489 if (errno == EROFS)
490 490 zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
491 491 "a read-only file system in this local zone.\nMake "
492 492 "sure %s exists in the global zone.", path, subdir);
493 493 else
494 494 zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
495 495 return (-1);
496 496 }
497 497
498 498 (void) chown(path, userid, groupid);
499 499 return (0);
500 500 }
501 501
502 502 static void
503 503 free_remote_fstypes(char **types)
504 504 {
505 505 uint_t i;
506 506
507 507 if (types == NULL)
508 508 return;
509 509 for (i = 0; types[i] != NULL; i++)
510 510 free(types[i]);
511 511 free(types);
512 512 }
513 513
514 514 static char **
515 515 get_remote_fstypes(zlog_t *zlogp)
516 516 {
517 517 char **types = NULL;
518 518 FILE *fp;
519 519 char buf[MAXPATHLEN];
520 520 char fstype[MAXPATHLEN];
521 521 uint_t lines = 0;
522 522 uint_t i;
523 523
524 524 if ((fp = fopen(DFSTYPES, "r")) == NULL) {
525 525 zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
526 526 return (NULL);
527 527 }
528 528 /*
529 529 * Count the number of lines
530 530 */
531 531 while (fgets(buf, sizeof (buf), fp) != NULL)
532 532 lines++;
533 533 if (lines == 0) /* didn't read anything; empty file */
534 534 goto out;
535 535 rewind(fp);
536 536 /*
537 537 * Allocate enough space for a NULL-terminated array.
538 538 */
539 539 types = calloc(lines + 1, sizeof (char *));
540 540 if (types == NULL) {
541 541 zerror(zlogp, B_TRUE, "memory allocation failed");
542 542 goto out;
543 543 }
544 544 i = 0;
545 545 while (fgets(buf, sizeof (buf), fp) != NULL) {
546 546 /* LINTED - fstype is big enough to hold buf */
547 547 if (sscanf(buf, "%s", fstype) == 0) {
548 548 zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
549 549 free_remote_fstypes(types);
550 550 types = NULL;
551 551 goto out;
552 552 }
553 553 types[i] = strdup(fstype);
554 554 if (types[i] == NULL) {
555 555 zerror(zlogp, B_TRUE, "memory allocation failed");
556 556 free_remote_fstypes(types);
557 557 types = NULL;
558 558 goto out;
559 559 }
560 560 i++;
561 561 }
562 562 out:
563 563 (void) fclose(fp);
564 564 return (types);
565 565 }
566 566
567 567 static boolean_t
568 568 is_remote_fstype(const char *fstype, char *const *remote_fstypes)
569 569 {
570 570 uint_t i;
571 571
572 572 if (remote_fstypes == NULL)
573 573 return (B_FALSE);
574 574 for (i = 0; remote_fstypes[i] != NULL; i++) {
575 575 if (strcmp(remote_fstypes[i], fstype) == 0)
576 576 return (B_TRUE);
577 577 }
578 578 return (B_FALSE);
579 579 }
580 580
581 581 /*
582 582 * This converts a zone root path (normally of the form .../root) to a Live
583 583 * Upgrade scratch zone root (of the form .../lu).
584 584 */
585 585 static void
586 586 root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
587 587 {
588 588 if (!isresolved && zonecfg_in_alt_root())
589 589 resolve_lofs(zlogp, zroot, zrootlen);
590 590 (void) strcpy(strrchr(zroot, '/') + 1, "lu");
591 591 }
592 592
593 593 /*
594 594 * Perform brand-specific cleanup if we are unable to unmount a FS.
595 595 */
596 596 static void
597 597 brand_umount_cleanup(zlog_t *zlogp, char *path)
598 598 {
599 599 char cmdbuf[2 * MAXPATHLEN];
600 600
601 601 if (post_statechg_hook[0] == '\0')
602 602 return;
603 603
604 604 if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %d %d %s", post_statechg_hook,
605 605 ZONE_STATE_DOWN, Z_UNMOUNT, path) > sizeof (cmdbuf))
606 606 return;
607 607
608 608 (void) do_subproc(zlogp, cmdbuf, NULL, B_FALSE);
609 609 }
610 610
611 611 /*
612 612 * The general strategy for unmounting filesystems is as follows:
613 613 *
614 614 * - Remote filesystems may be dead, and attempting to contact them as
615 615 * part of a regular unmount may hang forever; we want to always try to
616 616 * forcibly unmount such filesystems and only fall back to regular
617 617 * unmounts if the filesystem doesn't support forced unmounts.
618 618 *
619 619 * - We don't want to unnecessarily corrupt metadata on local
620 620 * filesystems (ie UFS), so we want to start off with graceful unmounts,
621 621 * and only escalate to doing forced unmounts if we get stuck.
622 622 *
623 623 * We start off walking backwards through the mount table. This doesn't
624 624 * give us strict ordering but ensures that we try to unmount submounts
625 625 * first. We thus limit the number of failed umount2(2) calls.
626 626 *
627 627 * The mechanism for determining if we're stuck is to count the number
628 628 * of failed unmounts each iteration through the mount table. This
629 629 * gives us an upper bound on the number of filesystems which remain
630 630 * mounted (autofs trigger nodes are dealt with separately). If at the
631 631 * end of one unmount+autofs_cleanup cycle we still have the same number
632 632 * of mounts that we started out with, we're stuck and try a forced
633 633 * unmount. If that fails (filesystem doesn't support forced unmounts)
634 634 * then we bail and are unable to teardown the zone. If it succeeds,
635 635 * we're no longer stuck so we continue with our policy of trying
636 636 * graceful mounts first.
637 637 *
638 638 * Zone must be down (ie, no processes or threads active).
639 639 */
640 640 static int
641 641 unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
642 642 {
643 643 int error = 0;
644 644 int fail = 0;
645 645 FILE *mnttab;
646 646 struct mnttab *mnts;
647 647 uint_t nmnt;
648 648 char zroot[MAXPATHLEN + 1];
649 649 size_t zrootlen;
650 650 uint_t oldcount = UINT_MAX;
651 651 boolean_t stuck = B_FALSE;
652 652 char **remote_fstypes = NULL;
653 653
654 654 if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
655 655 zerror(zlogp, B_FALSE, "unable to determine zone root");
656 656 return (-1);
657 657 }
658 658 if (unmount_cmd)
659 659 root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
660 660
661 661 (void) strcat(zroot, "/");
662 662 zrootlen = strlen(zroot);
663 663
664 664 /*
665 665 * For Trusted Extensions unmount each higher level zone's mount
666 666 * of our zone's /export/home
667 667 */
668 668 if (!unmount_cmd)
669 669 tsol_unmounts(zlogp, zone_name);
670 670
671 671 if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
672 672 zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
673 673 return (-1);
674 674 }
675 675 /*
676 676 * Use our hacky mntfs ioctl so we see everything, even mounts with
677 677 * MS_NOMNTTAB.
678 678 */
679 679 if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
680 680 zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
681 681 error++;
682 682 goto out;
683 683 }
684 684
685 685 /*
686 686 * Build the list of remote fstypes so we know which ones we
687 687 * should forcibly unmount.
688 688 */
689 689 remote_fstypes = get_remote_fstypes(zlogp);
690 690 for (; /* ever */; ) {
691 691 uint_t newcount = 0;
692 692 boolean_t unmounted;
693 693 struct mnttab *mnp;
694 694 char *path;
695 695 uint_t i;
696 696
697 697 mnts = NULL;
698 698 nmnt = 0;
699 699 /*
700 700 * MNTTAB gives us a way to walk through mounted
701 701 * filesystems; we need to be able to walk them in
702 702 * reverse order, so we build a list of all mounted
703 703 * filesystems.
704 704 */
705 705 if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
706 706 &nmnt) != 0) {
707 707 error++;
708 708 goto out;
709 709 }
710 710 for (i = 0; i < nmnt; i++) {
711 711 mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
712 712 path = mnp->mnt_mountp;
713 713 unmounted = B_FALSE;
714 714 /*
715 715 * Try forced unmount first for remote filesystems.
716 716 *
717 717 * Not all remote filesystems support forced unmounts,
718 718 * so if this fails (ENOTSUP) we'll continue on
719 719 * and try a regular unmount.
720 720 */
721 721 if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
722 722 if (umount2(path, MS_FORCE) == 0)
723 723 unmounted = B_TRUE;
724 724 }
725 725 /*
726 726 * Try forced unmount if we're stuck.
727 727 */
728 728 if (stuck) {
729 729 if (umount2(path, MS_FORCE) == 0) {
730 730 unmounted = B_TRUE;
731 731 stuck = B_FALSE;
732 732 fail = 0;
733 733 } else {
734 734 /*
735 735 * We may hit a failure here if there
736 736 * is an app in the GZ with an open
737 737 * pipe into the zone (commonly into
738 738 * the zone's /var/run). This type
739 739 * of app will notice the closed
740 740 * connection and cleanup, but it may
741 741 * take a while and we have no easy
742 742 * way to notice that. To deal with
743 743 * this case, we will wait and retry
744 744 * a few times before we give up.
745 745 */
746 746 fail++;
747 747 if (fail < 16) {
748 748 zerror(zlogp, B_FALSE,
749 749 "unable to unmount '%s', "
750 750 "retrying in 1 second",
751 751 path);
752 752 (void) sleep(1);
753 753 } else if (fail > 17) {
754 754 error++;
755 755 zerror(zlogp, B_FALSE,
756 756 "unable to unmount '%s'",
757 757 path);
758 758 free_mnttable(mnts, nmnt);
759 759 goto out;
760 760 } else {
761 761 /* Try the hook 2 times */
762 762 brand_umount_cleanup(zlogp,
763 763 path);
764 764 }
765 765 }
766 766 }
767 767 /*
768 768 * Try regular unmounts for everything else.
769 769 */
770 770 if (!unmounted && umount2(path, 0) != 0)
771 771 newcount++;
772 772 }
773 773 free_mnttable(mnts, nmnt);
774 774
775 775 if (newcount == 0)
776 776 break;
777 777 if (newcount >= oldcount) {
778 778 /*
779 779 * Last round didn't unmount anything; we're stuck and
780 780 * should start trying forced unmounts.
781 781 */
782 782 stuck = B_TRUE;
783 783 }
784 784 oldcount = newcount;
785 785
786 786 /*
787 787 * Autofs doesn't let you unmount its trigger nodes from
788 788 * userland so we have to tell the kernel to cleanup for us.
789 789 */
790 790 if (autofs_cleanup(zoneid) != 0) {
791 791 zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
792 792 error++;
793 793 goto out;
794 794 }
795 795 }
796 796
797 797 out:
798 798 free_remote_fstypes(remote_fstypes);
799 799 (void) fclose(mnttab);
800 800 return (error ? -1 : 0);
801 801 }
802 802
803 803 static int
804 804 fs_compare(const void *m1, const void *m2)
805 805 {
806 806 struct zone_fstab *i = (struct zone_fstab *)m1;
807 807 struct zone_fstab *j = (struct zone_fstab *)m2;
808 808
809 809 return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
810 810 }
811 811
812 812 /*
813 813 * Fork and exec (and wait for) the mentioned binary with the provided
814 814 * arguments. Returns (-1) if something went wrong with fork(2) or exec(2),
815 815 * returns the exit status otherwise.
816 816 *
817 817 * If we were unable to exec the provided pathname (for whatever
818 818 * reason), we return the special token ZEXIT_EXEC. The current value
819 819 * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
820 820 * consumers of this function; any future consumers must make sure this
821 821 * remains the case.
822 822 */
823 823 static int
824 824 forkexec(zlog_t *zlogp, const char *path, char *const argv[])
825 825 {
826 826 pid_t child_pid;
827 827 int child_status = 0;
828 828
829 829 /*
830 830 * Do not let another thread localize a message while we are forking.
831 831 */
832 832 (void) mutex_lock(&msglock);
833 833 child_pid = fork();
834 834 (void) mutex_unlock(&msglock);
835 835 if (child_pid == -1) {
836 836 zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
837 837 return (-1);
838 838 } else if (child_pid == 0) {
839 839 closefrom(0);
840 840 /* redirect stdin, stdout & stderr to /dev/null */
841 841 (void) open("/dev/null", O_RDONLY); /* stdin */
842 842 (void) open("/dev/null", O_WRONLY); /* stdout */
843 843 (void) open("/dev/null", O_WRONLY); /* stderr */
844 844 (void) execv(path, argv);
845 845 /*
846 846 * Since we are in the child, there is no point calling zerror()
847 847 * since there is nobody waiting to consume it. So exit with a
848 848 * special code that the parent will recognize and call zerror()
849 849 * accordingly.
850 850 */
851 851
852 852 _exit(ZEXIT_EXEC);
853 853 } else {
854 854 (void) waitpid(child_pid, &child_status, 0);
855 855 }
856 856
857 857 if (WIFSIGNALED(child_status)) {
858 858 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
859 859 "signal %d", path, WTERMSIG(child_status));
860 860 return (-1);
861 861 }
862 862 assert(WIFEXITED(child_status));
863 863 if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
864 864 zerror(zlogp, B_FALSE, "failed to exec %s", path);
865 865 return (-1);
866 866 }
867 867 return (WEXITSTATUS(child_status));
868 868 }
869 869
870 870 static int
871 871 isregfile(const char *path)
872 872 {
873 873 struct stat64 st;
874 874
875 875 if (stat64(path, &st) == -1)
876 876 return (-1);
877 877
878 878 return (S_ISREG(st.st_mode));
879 879 }
880 880
881 881 static int
882 882 dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
883 883 {
884 884 char cmdbuf[MAXPATHLEN];
885 885 char *argv[5];
886 886 int status;
887 887
888 888 /*
889 889 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
890 890 * that would cost us an extra fork/exec without buying us anything.
891 891 */
892 892 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
893 893 >= sizeof (cmdbuf)) {
894 894 zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
895 895 return (-1);
896 896 }
897 897
898 898 /*
899 899 * If it doesn't exist, that's OK: we verified this previously
900 900 * in zoneadm.
901 901 */
902 902 if (isregfile(cmdbuf) == -1)
903 903 return (0);
904 904
905 905 argv[0] = "fsck";
906 906 argv[1] = "-o";
907 907 argv[2] = "p";
908 908 argv[3] = (char *)rawdev;
909 909 argv[4] = NULL;
910 910
911 911 status = forkexec(zlogp, cmdbuf, argv);
912 912 if (status == 0 || status == -1)
913 913 return (status);
914 914 zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
915 915 "run fsck manually", rawdev, status);
916 916 return (-1);
917 917 }
918 918
919 919 static int
920 920 domount(zlog_t *zlogp, const char *fstype, const char *opts,
921 921 const char *special, const char *directory)
922 922 {
923 923 char cmdbuf[MAXPATHLEN];
924 924 char *argv[6];
925 925 int status;
926 926
927 927 /*
928 928 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
929 929 * that would cost us an extra fork/exec without buying us anything.
930 930 */
931 931 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
932 932 >= sizeof (cmdbuf)) {
933 933 zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
934 934 return (-1);
935 935 }
936 936 argv[0] = "mount";
937 937 if (opts[0] == '\0') {
938 938 argv[1] = (char *)special;
939 939 argv[2] = (char *)directory;
940 940 argv[3] = NULL;
941 941 } else {
942 942 argv[1] = "-o";
943 943 argv[2] = (char *)opts;
944 944 argv[3] = (char *)special;
945 945 argv[4] = (char *)directory;
946 946 argv[5] = NULL;
947 947 }
948 948
949 949 status = forkexec(zlogp, cmdbuf, argv);
950 950 if (status == 0 || status == -1)
951 951 return (status);
952 952 if (opts[0] == '\0')
953 953 zerror(zlogp, B_FALSE, "\"%s %s %s\" "
954 954 "failed with exit code %d",
955 955 cmdbuf, special, directory, status);
956 956 else
957 957 zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
958 958 "failed with exit code %d",
959 959 cmdbuf, opts, special, directory, status);
960 960 return (-1);
961 961 }
962 962
963 963 /*
964 964 * Check if a given mount point path exists.
965 965 * If it does, make sure it doesn't contain any symlinks.
966 966 * Note that if "leaf" is false we're checking an intermediate
967 967 * component of the mount point path, so it must be a directory.
968 968 * If "leaf" is true, then we're checking the entire mount point
969 969 * path, so the mount point itself can be anything aside from a
970 970 * symbolic link.
971 971 *
972 972 * If the path is invalid then a negative value is returned. If the
973 973 * path exists and is a valid mount point path then 0 is returned.
974 974 * If the path doesn't exist return a positive value.
975 975 */
976 976 static int
977 977 valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
978 978 {
979 979 struct stat statbuf;
980 980 char respath[MAXPATHLEN];
981 981 int res;
982 982
983 983 if (lstat(path, &statbuf) != 0) {
984 984 if (errno == ENOENT)
985 985 return (1);
986 986 zerror(zlogp, B_TRUE, "can't stat %s", path);
987 987 return (-1);
988 988 }
989 989 if (S_ISLNK(statbuf.st_mode)) {
990 990 zerror(zlogp, B_FALSE, "%s is a symlink", path);
991 991 return (-1);
992 992 }
993 993 if (!leaf && !S_ISDIR(statbuf.st_mode)) {
994 994 zerror(zlogp, B_FALSE, "%s is not a directory", path);
995 995 return (-1);
996 996 }
997 997 if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
998 998 zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
999 999 return (-1);
1000 1000 }
1001 1001 respath[res] = '\0';
1002 1002 if (strcmp(path, respath) != 0) {
1003 1003 /*
1004 1004 * We don't like ".."s, "."s, or "//"s throwing us off
1005 1005 */
1006 1006 zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
1007 1007 return (-1);
1008 1008 }
1009 1009 return (0);
1010 1010 }
1011 1011
1012 1012 /*
1013 1013 * Validate a mount point path. A valid mount point path is an
1014 1014 * absolute path that either doesn't exist, or, if it does exists it
1015 1015 * must be an absolute canonical path that doesn't have any symbolic
1016 1016 * links in it. The target of a mount point path can be any filesystem
1017 1017 * object. (Different filesystems can support different mount points,
1018 1018 * for example "lofs" and "mntfs" both support files and directories
1019 1019 * while "ufs" just supports directories.)
1020 1020 *
1021 1021 * If the path is invalid then a negative value is returned. If the
1022 1022 * path exists and is a valid mount point path then 0 is returned.
1023 1023 * If the path doesn't exist return a positive value.
1024 1024 */
1025 1025 int
1026 1026 valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
1027 1027 const char *dir, const char *fstype)
1028 1028 {
1029 1029 char abspath[MAXPATHLEN], *slashp, *slashp_next;
1030 1030 int rv;
1031 1031
1032 1032 /*
1033 1033 * Sanity check the target mount point path.
1034 1034 * It must be a non-null string that starts with a '/'.
1035 1035 */
1036 1036 if (dir[0] != '/') {
1037 1037 /* Something went wrong. */
1038 1038 zerror(zlogp, B_FALSE, "invalid mount directory, "
1039 1039 "type: \"%s\", special: \"%s\", dir: \"%s\"",
1040 1040 fstype, spec, dir);
1041 1041 return (-1);
1042 1042 }
1043 1043
1044 1044 /*
1045 1045 * Join rootpath and dir. Make sure abspath ends with '/', this
1046 1046 * is added to all paths (even non-directory paths) to allow us
1047 1047 * to detect the end of paths below. If the path already ends
1048 1048 * in a '/', then that's ok too (although we'll fail the
1049 1049 * cannonical path check in valid_mount_point()).
1050 1050 */
1051 1051 if (snprintf(abspath, sizeof (abspath),
1052 1052 "%s%s/", rootpath, dir) >= sizeof (abspath)) {
1053 1053 zerror(zlogp, B_FALSE, "pathname %s%s is too long",
1054 1054 rootpath, dir);
1055 1055 return (-1);
1056 1056 }
1057 1057
1058 1058 /*
1059 1059 * Starting with rootpath, verify the mount path one component
1060 1060 * at a time. Continue until we've evaluated all of abspath.
1061 1061 */
1062 1062 slashp = &abspath[strlen(rootpath)];
1063 1063 assert(*slashp == '/');
1064 1064 do {
1065 1065 slashp_next = strchr(slashp + 1, '/');
1066 1066 *slashp = '\0';
1067 1067 if (slashp_next != NULL) {
1068 1068 /* This is an intermediary mount path component. */
1069 1069 rv = valid_mount_point(zlogp, abspath, B_FALSE);
1070 1070 } else {
1071 1071 /* This is the last component of the mount path. */
1072 1072 rv = valid_mount_point(zlogp, abspath, B_TRUE);
1073 1073 }
1074 1074 if (rv < 0)
1075 1075 return (rv);
1076 1076 *slashp = '/';
1077 1077 } while ((slashp = slashp_next) != NULL);
1078 1078 return (rv);
1079 1079 }
1080 1080
1081 1081 static int
1082 1082 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
1083 1083 {
1084 1084 di_prof_t prof = arg;
1085 1085
1086 1086 if (name == NULL)
1087 1087 return (di_prof_add_dev(prof, match));
1088 1088 return (di_prof_add_map(prof, match, name));
1089 1089 }
1090 1090
1091 1091 static int
1092 1092 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
1093 1093 {
1094 1094 di_prof_t prof = arg;
1095 1095
1096 1096 return (di_prof_add_symlink(prof, source, target));
1097 1097 }
1098 1098
1099 1099 int
1100 1100 vplat_get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
1101 1101 {
1102 1102 if (zonecfg_get_iptype(snap_hndl, iptypep) != Z_OK) {
1103 1103 zerror(zlogp, B_FALSE, "invalid ip-type configuration");
1104 1104 return (-1);
1105 1105 }
1106 1106 return (0);
1107 1107 }
1108 1108
1109 1109 /*
1110 1110 * Apply the standard lists of devices/symlinks/mappings and the user-specified
1111 1111 * list of devices (via zonecfg) to the /dev filesystem. The filesystem will
1112 1112 * use these as a profile/filter to determine what exists in /dev.
1113 1113 */
↓ open down ↓ |
1113 lines elided |
↑ open up ↑ |
1114 1114 static int
1115 1115 mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd)
1116 1116 {
1117 1117 char brand[MAXNAMELEN];
1118 1118 brand_handle_t bh = NULL;
1119 1119 struct zone_devtab ztab;
1120 1120 di_prof_t prof = NULL;
1121 1121 int err;
1122 1122 int retval = -1;
1123 1123 zone_iptype_t iptype;
1124 - const char *curr_iptype;
1124 + const char *curr_iptype = NULL;
1125 1125
1126 1126 if (di_prof_init(devpath, &prof)) {
1127 1127 zerror(zlogp, B_TRUE, "failed to initialize profile");
1128 1128 goto cleanup;
1129 1129 }
1130 1130
1131 1131 /*
1132 1132 * Get a handle to the brand info for this zone.
1133 1133 * If we are mounting the zone, then we must always use the default
1134 1134 * brand device mounts.
1135 1135 */
1136 1136 if (ALT_MOUNT(mount_cmd)) {
1137 1137 (void) strlcpy(brand, default_brand, sizeof (brand));
1138 1138 } else {
1139 1139 (void) strlcpy(brand, brand_name, sizeof (brand));
1140 1140 }
1141 1141
1142 1142 if ((bh = brand_open(brand)) == NULL) {
1143 1143 zerror(zlogp, B_FALSE, "unable to determine zone brand");
1144 1144 goto cleanup;
1145 1145 }
1146 1146
1147 1147 if (vplat_get_iptype(zlogp, &iptype) < 0) {
1148 1148 zerror(zlogp, B_TRUE, "unable to determine ip-type");
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
1149 1149 goto cleanup;
1150 1150 }
1151 1151 switch (iptype) {
1152 1152 case ZS_SHARED:
1153 1153 curr_iptype = "shared";
1154 1154 break;
1155 1155 case ZS_EXCLUSIVE:
1156 1156 curr_iptype = "exclusive";
1157 1157 break;
1158 1158 }
1159 + if (curr_iptype == NULL)
1160 + abort();
1159 1161
1160 1162 if (brand_platform_iter_devices(bh, zone_name,
1161 1163 mount_one_dev_device_cb, prof, curr_iptype) != 0) {
1162 1164 zerror(zlogp, B_TRUE, "failed to add standard device");
1163 1165 goto cleanup;
1164 1166 }
1165 1167
1166 1168 if (brand_platform_iter_link(bh,
1167 1169 mount_one_dev_symlink_cb, prof) != 0) {
1168 1170 zerror(zlogp, B_TRUE, "failed to add standard symlink");
1169 1171 goto cleanup;
1170 1172 }
1171 1173
1172 1174 /* Add user-specified devices and directories */
1173 1175 if ((err = zonecfg_setdevent(snap_hndl)) != 0) {
1174 1176 zerror(zlogp, B_FALSE, "%s: %s", zone_name,
1175 1177 zonecfg_strerror(err));
1176 1178 goto cleanup;
1177 1179 }
1178 1180 while (zonecfg_getdevent(snap_hndl, &ztab) == Z_OK) {
1179 1181 if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
1180 1182 zerror(zlogp, B_TRUE, "failed to add "
1181 1183 "user-specified device");
1182 1184 goto cleanup;
1183 1185 }
1184 1186 }
1185 1187 (void) zonecfg_enddevent(snap_hndl);
1186 1188
1187 1189 /* Send profile to kernel */
1188 1190 if (di_prof_commit(prof)) {
1189 1191 zerror(zlogp, B_TRUE, "failed to commit profile");
1190 1192 goto cleanup;
1191 1193 }
1192 1194
1193 1195 retval = 0;
1194 1196
1195 1197 cleanup:
1196 1198 if (bh != NULL)
1197 1199 brand_close(bh);
1198 1200 if (prof)
1199 1201 di_prof_fini(prof);
1200 1202 return (retval);
1201 1203 }
1202 1204
1203 1205 static int
1204 1206 mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath,
1205 1207 zone_mnt_t mount_cmd)
1206 1208 {
1207 1209 char path[MAXPATHLEN];
1208 1210 char optstr[MAX_MNTOPT_STR];
1209 1211 zone_fsopt_t *optptr;
1210 1212 int rv;
1211 1213
1212 1214 if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
1213 1215 fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
1214 1216 zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
1215 1217 rootpath, fsptr->zone_fs_dir);
1216 1218 return (-1);
1217 1219 } else if (rv > 0) {
1218 1220 /* The mount point path doesn't exist, create it now. */
1219 1221 if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
1220 1222 DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
1221 1223 DEFAULT_DIR_GROUP) != 0) {
1222 1224 zerror(zlogp, B_FALSE, "failed to create mount point");
1223 1225 return (-1);
1224 1226 }
1225 1227
1226 1228 /*
1227 1229 * Now this might seem weird, but we need to invoke
1228 1230 * valid_mount_path() again. Why? Because it checks
1229 1231 * to make sure that the mount point path is canonical,
1230 1232 * which it can only do if the path exists, so now that
1231 1233 * we've created the path we have to verify it again.
1232 1234 */
1233 1235 if ((rv = valid_mount_path(zlogp, rootpath,
1234 1236 fsptr->zone_fs_special, fsptr->zone_fs_dir,
1235 1237 fsptr->zone_fs_type)) < 0) {
1236 1238 zerror(zlogp, B_FALSE,
1237 1239 "%s%s is not a valid mount point",
1238 1240 rootpath, fsptr->zone_fs_dir);
1239 1241 return (-1);
1240 1242 }
1241 1243 }
1242 1244
1243 1245 (void) snprintf(path, sizeof (path), "%s%s", rootpath,
1244 1246 fsptr->zone_fs_dir);
1245 1247
1246 1248 /*
1247 1249 * In general the strategy here is to do just as much verification as
1248 1250 * necessary to avoid crashing or otherwise doing something bad; if the
1249 1251 * administrator initiated the operation via zoneadm(1m), he'll get
1250 1252 * auto-verification which will let him know what's wrong. If he
1251 1253 * modifies the zone configuration of a running zone and doesn't attempt
1252 1254 * to verify that it's OK we won't crash but won't bother trying to be
1253 1255 * too helpful either. zoneadm verify is only a couple keystrokes away.
1254 1256 */
1255 1257 if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
1256 1258 zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
1257 1259 "invalid file-system type %s", fsptr->zone_fs_special,
1258 1260 fsptr->zone_fs_dir, fsptr->zone_fs_type);
1259 1261 return (-1);
1260 1262 }
1261 1263
1262 1264 /*
1263 1265 * If we're looking at an alternate root environment, then construct
1264 1266 * read-only loopback mounts as necessary. Note that any special
1265 1267 * paths for lofs zone mounts in an alternate root must have
1266 1268 * already been pre-pended with any alternate root path by the
1267 1269 * time we get here.
1268 1270 */
1269 1271 if (zonecfg_in_alt_root()) {
1270 1272 struct stat64 st;
1271 1273
1272 1274 if (stat64(fsptr->zone_fs_special, &st) != -1 &&
1273 1275 S_ISBLK(st.st_mode)) {
1274 1276 /*
1275 1277 * If we're going to mount a block device we need
1276 1278 * to check if that device is already mounted
1277 1279 * somewhere else, and if so, do a lofs mount
1278 1280 * of the device instead of a direct mount
1279 1281 */
1280 1282 if (check_lofs_needed(zlogp, fsptr) == -1)
1281 1283 return (-1);
1282 1284 } else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1283 1285 /*
1284 1286 * For lofs mounts, the special node is inside the
1285 1287 * alternate root. We need lofs resolution for
1286 1288 * this case in order to get at the underlying
1287 1289 * read-write path.
1288 1290 */
1289 1291 resolve_lofs(zlogp, fsptr->zone_fs_special,
1290 1292 sizeof (fsptr->zone_fs_special));
1291 1293 }
1292 1294 }
1293 1295
1294 1296 /*
1295 1297 * Run 'fsck -m' if there's a device to fsck.
1296 1298 */
1297 1299 if (fsptr->zone_fs_raw[0] != '\0' &&
1298 1300 dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
1299 1301 return (-1);
1300 1302 } else if (isregfile(fsptr->zone_fs_special) == 1 &&
1301 1303 dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
1302 1304 return (-1);
1303 1305 }
1304 1306
1305 1307 /*
1306 1308 * Build up mount option string.
1307 1309 */
1308 1310 optstr[0] = '\0';
1309 1311 if (fsptr->zone_fs_options != NULL) {
1310 1312 (void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
1311 1313 sizeof (optstr));
1312 1314 for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
1313 1315 optptr != NULL; optptr = optptr->zone_fsopt_next) {
1314 1316 (void) strlcat(optstr, ",", sizeof (optstr));
1315 1317 (void) strlcat(optstr, optptr->zone_fsopt_opt,
1316 1318 sizeof (optstr));
1317 1319 }
1318 1320 }
1319 1321
1320 1322 if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
1321 1323 fsptr->zone_fs_special, path)) != 0)
1322 1324 return (rv);
1323 1325
1324 1326 /*
1325 1327 * The mount succeeded. If this was not a mount of /dev then
1326 1328 * we're done.
1327 1329 */
1328 1330 if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
1329 1331 return (0);
1330 1332
1331 1333 /*
1332 1334 * We just mounted an instance of a /dev filesystem, so now we
1333 1335 * need to configure it.
1334 1336 */
1335 1337 return (mount_one_dev(zlogp, path, mount_cmd));
1336 1338 }
1337 1339
1338 1340 static void
1339 1341 free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
1340 1342 {
1341 1343 uint_t i;
1342 1344
1343 1345 if (fsarray == NULL)
1344 1346 return;
1345 1347 for (i = 0; i < nelem; i++)
1346 1348 zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
1347 1349 free(fsarray);
1348 1350 }
1349 1351
1350 1352 /*
1351 1353 * This function initiates the creation of a small Solaris Environment for
1352 1354 * scratch zone. The Environment creation process is split up into two
1353 1355 * functions(build_mounted_pre_var() and build_mounted_post_var()). It
1354 1356 * is done this way because:
1355 1357 * We need to have both /etc and /var in the root of the scratchzone.
1356 1358 * We loopback mount zone's own /etc and /var into the root of the
1357 1359 * scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
1358 1360 * need to delay the mount of /var till the zone's root gets populated.
1359 1361 * So mounting of localdirs[](/etc and /var) have been moved to the
1360 1362 * build_mounted_post_var() which gets called only after the zone
1361 1363 * specific filesystems are mounted.
1362 1364 *
1363 1365 * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
1364 1366 * does not loopback mount the zone's own /etc and /var into the root of the
1365 1367 * scratch zone.
1366 1368 */
1367 1369 static boolean_t
1368 1370 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
1369 1371 size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1370 1372 {
1371 1373 char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1372 1374 const char **cpp;
1373 1375 static const char *mkdirs[] = {
1374 1376 "/system", "/system/contract", "/system/object", "/proc",
1375 1377 "/dev", "/tmp", "/a", NULL
1376 1378 };
1377 1379 char *altstr;
1378 1380 FILE *fp;
1379 1381 uuid_t uuid;
1380 1382
1381 1383 resolve_lofs(zlogp, rootpath, rootlen);
1382 1384 (void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
1383 1385 resolve_lofs(zlogp, luroot, lurootlen);
1384 1386 (void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1385 1387 (void) symlink("./usr/bin", tmp);
1386 1388
1387 1389 /*
1388 1390 * These are mostly special mount points; not handled here. (See
1389 1391 * zone_mount_early.)
1390 1392 */
1391 1393 for (cpp = mkdirs; *cpp != NULL; cpp++) {
1392 1394 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1393 1395 if (mkdir(tmp, 0755) != 0) {
1394 1396 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1395 1397 return (B_FALSE);
1396 1398 }
1397 1399 }
1398 1400 /*
1399 1401 * This is here to support lucopy. If there's an instance of this same
1400 1402 * zone on the current running system, then we mount its root up as
1401 1403 * read-only inside the scratch zone.
1402 1404 */
1403 1405 (void) zonecfg_get_uuid(zone_name, uuid);
1404 1406 altstr = strdup(zonecfg_get_root());
1405 1407 if (altstr == NULL) {
1406 1408 zerror(zlogp, B_TRUE, "memory allocation failed");
1407 1409 return (B_FALSE);
1408 1410 }
1409 1411 zonecfg_set_root("");
1410 1412 (void) strlcpy(tmp, zone_name, sizeof (tmp));
1411 1413 (void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1412 1414 if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1413 1415 strcmp(fromdir, rootpath) != 0) {
1414 1416 (void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1415 1417 if (mkdir(tmp, 0755) != 0) {
1416 1418 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1417 1419 return (B_FALSE);
1418 1420 }
1419 1421 if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, fromdir,
1420 1422 tmp) != 0) {
1421 1423 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1422 1424 fromdir);
1423 1425 return (B_FALSE);
1424 1426 }
1425 1427 }
1426 1428 zonecfg_set_root(altstr);
1427 1429 free(altstr);
1428 1430
1429 1431 if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1430 1432 zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1431 1433 return (B_FALSE);
1432 1434 }
1433 1435 (void) ftruncate(fileno(fp), 0);
1434 1436 if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1435 1437 zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1436 1438 }
1437 1439 zonecfg_close_scratch(fp);
1438 1440 (void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1439 1441 if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1440 1442 return (B_FALSE);
1441 1443 (void) strlcpy(rootpath, tmp, rootlen);
1442 1444 return (B_TRUE);
1443 1445 }
1444 1446
1445 1447
1446 1448 static boolean_t
1447 1449 build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
1448 1450 const char *luroot)
1449 1451 {
1450 1452 char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1451 1453 const char **cpp;
1452 1454 const char **loopdirs;
1453 1455 const char **tmpdirs;
1454 1456 static const char *localdirs[] = {
1455 1457 "/etc", "/var", NULL
1456 1458 };
1457 1459 static const char *scr_loopdirs[] = {
1458 1460 "/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1459 1461 "/usr", NULL
1460 1462 };
1461 1463 static const char *upd_loopdirs[] = {
1462 1464 "/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
1463 1465 "/usr", "/var", NULL
1464 1466 };
1465 1467 static const char *scr_tmpdirs[] = {
1466 1468 "/tmp", "/var/run", NULL
1467 1469 };
1468 1470 static const char *upd_tmpdirs[] = {
1469 1471 "/tmp", "/var/run", "/var/tmp", NULL
1470 1472 };
1471 1473 struct stat st;
1472 1474
1473 1475 if (mount_cmd == Z_MNT_SCRATCH) {
1474 1476 /*
1475 1477 * These are mounted read-write from the zone undergoing
1476 1478 * upgrade. We must be careful not to 'leak' things from the
1477 1479 * main system into the zone, and this accomplishes that goal.
1478 1480 */
1479 1481 for (cpp = localdirs; *cpp != NULL; cpp++) {
1480 1482 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
1481 1483 *cpp);
1482 1484 (void) snprintf(fromdir, sizeof (fromdir), "%s%s",
1483 1485 rootpath, *cpp);
1484 1486 if (mkdir(tmp, 0755) != 0) {
1485 1487 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1486 1488 return (B_FALSE);
1487 1489 }
1488 1490 if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
1489 1491 != 0) {
1490 1492 zerror(zlogp, B_TRUE, "cannot mount %s on %s",
1491 1493 tmp, *cpp);
1492 1494 return (B_FALSE);
1493 1495 }
1494 1496 }
1495 1497 }
1496 1498
1497 1499 if (mount_cmd == Z_MNT_UPDATE)
1498 1500 loopdirs = upd_loopdirs;
1499 1501 else
1500 1502 loopdirs = scr_loopdirs;
1501 1503
1502 1504 /*
1503 1505 * These are things mounted read-only from the running system because
1504 1506 * they contain binaries that must match system.
1505 1507 */
1506 1508 for (cpp = loopdirs; *cpp != NULL; cpp++) {
1507 1509 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1508 1510 if (mkdir(tmp, 0755) != 0) {
1509 1511 if (errno != EEXIST) {
1510 1512 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1511 1513 return (B_FALSE);
1512 1514 }
1513 1515 if (lstat(tmp, &st) != 0) {
1514 1516 zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1515 1517 return (B_FALSE);
1516 1518 }
1517 1519 /*
1518 1520 * Ignore any non-directories encountered. These are
1519 1521 * things that have been converted into symlinks
1520 1522 * (/etc/fs and /etc/lib) and no longer need a lofs
1521 1523 * fixup.
1522 1524 */
1523 1525 if (!S_ISDIR(st.st_mode))
1524 1526 continue;
1525 1527 }
1526 1528 if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, *cpp,
1527 1529 tmp) != 0) {
1528 1530 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1529 1531 *cpp);
1530 1532 return (B_FALSE);
1531 1533 }
1532 1534 }
1533 1535
1534 1536 if (mount_cmd == Z_MNT_UPDATE)
1535 1537 tmpdirs = upd_tmpdirs;
1536 1538 else
1537 1539 tmpdirs = scr_tmpdirs;
1538 1540
1539 1541 /*
1540 1542 * These are things with tmpfs mounted inside.
1541 1543 */
1542 1544 for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1543 1545 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1544 1546 if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
1545 1547 errno != EEXIST) {
1546 1548 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1547 1549 return (B_FALSE);
1548 1550 }
1549 1551
1550 1552 /*
1551 1553 * We could set the mode for /tmp when we do the mkdir but
1552 1554 * since that can be modified by the umask we will just set
1553 1555 * the correct mode for /tmp now.
1554 1556 */
1555 1557 if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
1556 1558 zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
1557 1559 return (B_FALSE);
1558 1560 }
1559 1561
1560 1562 if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1561 1563 zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1562 1564 return (B_FALSE);
1563 1565 }
1564 1566 }
1565 1567 return (B_TRUE);
1566 1568 }
1567 1569
1568 1570 typedef struct plat_gmount_cb_data {
1569 1571 zlog_t *pgcd_zlogp;
1570 1572 struct zone_fstab **pgcd_fs_tab;
1571 1573 int *pgcd_num_fs;
1572 1574 } plat_gmount_cb_data_t;
1573 1575
1574 1576 /*
1575 1577 * plat_gmount_cb() is a callback function invoked by libbrand to iterate
1576 1578 * through all global brand platform mounts.
1577 1579 */
1578 1580 int
1579 1581 plat_gmount_cb(void *data, const char *spec, const char *dir,
1580 1582 const char *fstype, const char *opt)
1581 1583 {
1582 1584 plat_gmount_cb_data_t *cp = data;
1583 1585 zlog_t *zlogp = cp->pgcd_zlogp;
1584 1586 struct zone_fstab *fs_ptr = *cp->pgcd_fs_tab;
1585 1587 int num_fs = *cp->pgcd_num_fs;
1586 1588 struct zone_fstab *fsp, *tmp_ptr;
1587 1589
1588 1590 num_fs++;
1589 1591 if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
1590 1592 zerror(zlogp, B_TRUE, "memory allocation failed");
1591 1593 return (-1);
1592 1594 }
1593 1595
1594 1596 fs_ptr = tmp_ptr;
1595 1597 fsp = &fs_ptr[num_fs - 1];
1596 1598
1597 1599 /* update the callback struct passed in */
1598 1600 *cp->pgcd_fs_tab = fs_ptr;
1599 1601 *cp->pgcd_num_fs = num_fs;
1600 1602
1601 1603 fsp->zone_fs_raw[0] = '\0';
1602 1604 (void) strlcpy(fsp->zone_fs_special, spec,
1603 1605 sizeof (fsp->zone_fs_special));
1604 1606 (void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
1605 1607 (void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
1606 1608 fsp->zone_fs_options = NULL;
1607 1609 if ((opt != NULL) &&
1608 1610 (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
1609 1611 zerror(zlogp, B_FALSE, "error adding property");
1610 1612 return (-1);
1611 1613 }
1612 1614
1613 1615 return (0);
1614 1616 }
1615 1617
1616 1618 static int
1617 1619 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
1618 1620 struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
1619 1621 {
1620 1622 struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
1621 1623 int num_fs;
1622 1624
1623 1625 num_fs = *num_fsp;
1624 1626 fs_ptr = *fs_tabp;
1625 1627
1626 1628 if (zonecfg_setfsent(handle) != Z_OK) {
1627 1629 zerror(zlogp, B_FALSE, "invalid configuration");
1628 1630 return (-1);
1629 1631 }
1630 1632 while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
1631 1633 /*
1632 1634 * ZFS filesystems will not be accessible under an alternate
1633 1635 * root, since the pool will not be known. Ignore them in this
1634 1636 * case.
1635 1637 */
1636 1638 if (ALT_MOUNT(mount_cmd) &&
1637 1639 strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
1638 1640 continue;
1639 1641
1640 1642 num_fs++;
1641 1643 if ((tmp_ptr = realloc(fs_ptr,
1642 1644 num_fs * sizeof (*tmp_ptr))) == NULL) {
1643 1645 zerror(zlogp, B_TRUE, "memory allocation failed");
1644 1646 (void) zonecfg_endfsent(handle);
1645 1647 return (-1);
1646 1648 }
1647 1649 /* update the pointers passed in */
1648 1650 *fs_tabp = tmp_ptr;
1649 1651 *num_fsp = num_fs;
1650 1652
1651 1653 fs_ptr = tmp_ptr;
1652 1654 fsp = &fs_ptr[num_fs - 1];
1653 1655 (void) strlcpy(fsp->zone_fs_dir,
1654 1656 fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
1655 1657 (void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
1656 1658 sizeof (fsp->zone_fs_raw));
1657 1659 (void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
1658 1660 sizeof (fsp->zone_fs_type));
1659 1661 fsp->zone_fs_options = fstab.zone_fs_options;
1660 1662
1661 1663 /*
1662 1664 * For all lofs mounts, make sure that the 'special'
1663 1665 * entry points inside the alternate root. The
1664 1666 * source path for a lofs mount in a given zone needs
1665 1667 * to be relative to the root of the boot environment
1666 1668 * that contains the zone. Note that we don't do this
1667 1669 * for non-lofs mounts since they will have a device
1668 1670 * as a backing store and device paths must always be
1669 1671 * specified relative to the current boot environment.
1670 1672 */
1671 1673 fsp->zone_fs_special[0] = '\0';
1672 1674 if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
1673 1675 (void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
1674 1676 sizeof (fsp->zone_fs_special));
1675 1677 }
1676 1678 (void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
1677 1679 sizeof (fsp->zone_fs_special));
1678 1680 }
1679 1681 (void) zonecfg_endfsent(handle);
1680 1682 return (0);
1681 1683 }
1682 1684
1683 1685 static int
1684 1686 mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
1685 1687 {
1686 1688 char rootpath[MAXPATHLEN];
1687 1689 char zonepath[MAXPATHLEN];
1688 1690 char brand[MAXNAMELEN];
1689 1691 char luroot[MAXPATHLEN];
1690 1692 int i, num_fs = 0;
1691 1693 struct zone_fstab *fs_ptr = NULL;
1692 1694 zone_state_t zstate;
1693 1695 brand_handle_t bh;
1694 1696 plat_gmount_cb_data_t cb;
1695 1697
1696 1698 if (zone_get_state(zone_name, &zstate) != Z_OK ||
1697 1699 (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
1698 1700 zerror(zlogp, B_FALSE,
1699 1701 "zone must be in '%s' or '%s' state to mount file-systems",
1700 1702 zone_state_str(ZONE_STATE_READY),
1701 1703 zone_state_str(ZONE_STATE_MOUNTED));
1702 1704 goto bad;
1703 1705 }
1704 1706
1705 1707 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
1706 1708 zerror(zlogp, B_TRUE, "unable to determine zone path");
1707 1709 goto bad;
1708 1710 }
1709 1711
1710 1712 if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
1711 1713 zerror(zlogp, B_TRUE, "unable to determine zone root");
1712 1714 goto bad;
1713 1715 }
1714 1716
1715 1717 if (zonecfg_setfsent(snap_hndl) != Z_OK) {
1716 1718 zerror(zlogp, B_FALSE, "invalid configuration");
1717 1719 goto bad;
1718 1720 }
1719 1721
1720 1722 /*
1721 1723 * If we are mounting the zone, then we must always use the default
1722 1724 * brand global mounts.
1723 1725 */
1724 1726 if (ALT_MOUNT(mount_cmd)) {
1725 1727 (void) strlcpy(brand, default_brand, sizeof (brand));
1726 1728 } else {
1727 1729 (void) strlcpy(brand, brand_name, sizeof (brand));
1728 1730 }
1729 1731
1730 1732 /* Get a handle to the brand info for this zone */
1731 1733 if ((bh = brand_open(brand)) == NULL) {
1732 1734 zerror(zlogp, B_FALSE, "unable to determine zone brand");
1733 1735 return (-1);
1734 1736 }
1735 1737
1736 1738 /*
1737 1739 * Get the list of global filesystems to mount from the brand
1738 1740 * configuration.
1739 1741 */
1740 1742 cb.pgcd_zlogp = zlogp;
1741 1743 cb.pgcd_fs_tab = &fs_ptr;
1742 1744 cb.pgcd_num_fs = &num_fs;
1743 1745 if (brand_platform_iter_gmounts(bh, zonepath,
1744 1746 plat_gmount_cb, &cb) != 0) {
1745 1747 zerror(zlogp, B_FALSE, "unable to mount filesystems");
1746 1748 brand_close(bh);
1747 1749 return (-1);
1748 1750 }
1749 1751 brand_close(bh);
1750 1752
1751 1753 /*
1752 1754 * Iterate through the rest of the filesystems. Sort them all,
1753 1755 * then mount them in sorted order. This is to make sure the
1754 1756 * higher level directories (e.g., /usr) get mounted before
1755 1757 * any beneath them (e.g., /usr/local).
1756 1758 */
1757 1759 if (mount_filesystems_fsent(snap_hndl, zlogp, &fs_ptr, &num_fs,
1758 1760 mount_cmd) != 0)
1759 1761 goto bad;
1760 1762
1761 1763 /*
1762 1764 * Normally when we mount a zone all the zone filesystems
1763 1765 * get mounted relative to rootpath, which is usually
1764 1766 * <zonepath>/root. But when mounting a zone for administration
1765 1767 * purposes via the zone "mount" state, build_mounted_pre_var()
1766 1768 * updates rootpath to be <zonepath>/lu/a so we'll mount all
1767 1769 * the zones filesystems there instead.
1768 1770 *
1769 1771 * build_mounted_pre_var() and build_mounted_post_var() will
1770 1772 * also do some extra work to create directories and lofs mount
1771 1773 * a bunch of global zone file system paths into <zonepath>/lu.
1772 1774 *
1773 1775 * This allows us to be able to enter the zone (now rooted at
1774 1776 * <zonepath>/lu) and run the upgrade/patch tools that are in the
1775 1777 * global zone and have them upgrade the to-be-modified zone's
1776 1778 * files mounted on /a. (Which mirrors the existing standard
1777 1779 * upgrade environment.)
1778 1780 *
1779 1781 * There is of course one catch. When doing the upgrade
1780 1782 * we need <zoneroot>/lu/dev to be the /dev filesystem
1781 1783 * for the zone and we don't want to have any /dev filesystem
1782 1784 * mounted at <zoneroot>/lu/a/dev. Since /dev is specified
1783 1785 * as a normal zone filesystem by default we'll try to mount
1784 1786 * it at <zoneroot>/lu/a/dev, so we have to detect this
1785 1787 * case and instead mount it at <zoneroot>/lu/dev.
1786 1788 *
1787 1789 * All this work is done in three phases:
1788 1790 * 1) Create and populate lu directory (build_mounted_pre_var()).
1789 1791 * 2) Mount the required filesystems as per the zone configuration.
1790 1792 * 3) Set up the rest of the scratch zone environment
1791 1793 * (build_mounted_post_var()).
1792 1794 */
1793 1795 if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
1794 1796 rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1795 1797 goto bad;
1796 1798
1797 1799 qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
1798 1800
1799 1801 for (i = 0; i < num_fs; i++) {
1800 1802 if (ALT_MOUNT(mount_cmd) &&
1801 1803 strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
1802 1804 size_t slen = strlen(rootpath) - 2;
1803 1805
1804 1806 /*
1805 1807 * By default we'll try to mount /dev as /a/dev
1806 1808 * but /dev is special and always goes at the top
1807 1809 * so strip the trailing '/a' from the rootpath.
1808 1810 */
1809 1811 assert(strcmp(&rootpath[slen], "/a") == 0);
1810 1812 rootpath[slen] = '\0';
1811 1813 if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd)
1812 1814 != 0)
1813 1815 goto bad;
1814 1816 rootpath[slen] = '/';
1815 1817 continue;
1816 1818 }
1817 1819 if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0)
1818 1820 goto bad;
1819 1821 }
1820 1822 if (ALT_MOUNT(mount_cmd) &&
1821 1823 !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
1822 1824 goto bad;
1823 1825
1824 1826 /*
1825 1827 * For Trusted Extensions cross-mount each lower level /export/home
1826 1828 */
1827 1829 if (mount_cmd == Z_MNT_BOOT &&
1828 1830 tsol_mounts(zlogp, zone_name, rootpath) != 0)
1829 1831 goto bad;
1830 1832
1831 1833 free_fs_data(fs_ptr, num_fs);
1832 1834
1833 1835 /*
1834 1836 * Everything looks fine.
1835 1837 */
1836 1838 return (0);
1837 1839
1838 1840 bad:
1839 1841 free_fs_data(fs_ptr, num_fs);
1840 1842 return (-1);
1841 1843 }
1842 1844
1843 1845 /* caller makes sure neither parameter is NULL */
1844 1846 static int
1845 1847 addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
1846 1848 {
1847 1849 int prefixlen;
1848 1850
1849 1851 prefixlen = atoi(prefixstr);
1850 1852 if (prefixlen < 0 || prefixlen > maxprefixlen)
1851 1853 return (1);
1852 1854 while (prefixlen > 0) {
1853 1855 if (prefixlen >= 8) {
1854 1856 *maskstr++ = 0xFF;
1855 1857 prefixlen -= 8;
1856 1858 continue;
1857 1859 }
1858 1860 *maskstr |= 1 << (8 - prefixlen);
1859 1861 prefixlen--;
1860 1862 }
1861 1863 return (0);
1862 1864 }
1863 1865
1864 1866 /*
1865 1867 * Tear down all interfaces belonging to the given zone. This should
1866 1868 * be called with the zone in a state other than "running", so that
1867 1869 * interfaces can't be assigned to the zone after this returns.
1868 1870 *
1869 1871 * If anything goes wrong, log an error message and return an error.
1870 1872 */
1871 1873 static int
1872 1874 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
1873 1875 {
1874 1876 struct lifnum lifn;
1875 1877 struct lifconf lifc;
1876 1878 struct lifreq *lifrp, lifrl;
1877 1879 int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
1878 1880 int num_ifs, s, i, ret_code = 0;
1879 1881 uint_t bufsize;
1880 1882 char *buf = NULL;
1881 1883
1882 1884 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1883 1885 zerror(zlogp, B_TRUE, "could not get socket");
1884 1886 ret_code = -1;
1885 1887 goto bad;
1886 1888 }
1887 1889 lifn.lifn_family = AF_UNSPEC;
1888 1890 lifn.lifn_flags = (int)lifc_flags;
1889 1891 if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
1890 1892 zerror(zlogp, B_TRUE,
1891 1893 "could not determine number of network interfaces");
1892 1894 ret_code = -1;
1893 1895 goto bad;
1894 1896 }
1895 1897 num_ifs = lifn.lifn_count;
1896 1898 bufsize = num_ifs * sizeof (struct lifreq);
1897 1899 if ((buf = malloc(bufsize)) == NULL) {
1898 1900 zerror(zlogp, B_TRUE, "memory allocation failed");
1899 1901 ret_code = -1;
1900 1902 goto bad;
1901 1903 }
1902 1904 lifc.lifc_family = AF_UNSPEC;
1903 1905 lifc.lifc_flags = (int)lifc_flags;
1904 1906 lifc.lifc_len = bufsize;
1905 1907 lifc.lifc_buf = buf;
1906 1908 if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1907 1909 zerror(zlogp, B_TRUE, "could not get configured network "
1908 1910 "interfaces");
1909 1911 ret_code = -1;
1910 1912 goto bad;
1911 1913 }
1912 1914 lifrp = lifc.lifc_req;
1913 1915 for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
1914 1916 (void) close(s);
1915 1917 if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
1916 1918 0) {
1917 1919 zerror(zlogp, B_TRUE, "%s: could not get socket",
1918 1920 lifrl.lifr_name);
1919 1921 ret_code = -1;
1920 1922 continue;
1921 1923 }
1922 1924 (void) memset(&lifrl, 0, sizeof (lifrl));
1923 1925 (void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
1924 1926 sizeof (lifrl.lifr_name));
1925 1927 if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1926 1928 if (errno == ENXIO)
1927 1929 /*
1928 1930 * Interface may have been removed by admin or
1929 1931 * another zone halting.
1930 1932 */
1931 1933 continue;
1932 1934 zerror(zlogp, B_TRUE,
1933 1935 "%s: could not determine the zone to which this "
1934 1936 "network interface is bound", lifrl.lifr_name);
1935 1937 ret_code = -1;
1936 1938 continue;
1937 1939 }
1938 1940 if (lifrl.lifr_zoneid == zone_id) {
1939 1941 if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
1940 1942 zerror(zlogp, B_TRUE,
1941 1943 "%s: could not remove network interface",
1942 1944 lifrl.lifr_name);
1943 1945 ret_code = -1;
1944 1946 continue;
1945 1947 }
1946 1948 }
1947 1949 }
1948 1950 bad:
1949 1951 if (s > 0)
1950 1952 (void) close(s);
1951 1953 if (buf)
1952 1954 free(buf);
1953 1955 return (ret_code);
1954 1956 }
1955 1957
1956 1958 static union sockunion {
1957 1959 struct sockaddr sa;
1958 1960 struct sockaddr_in sin;
1959 1961 struct sockaddr_dl sdl;
1960 1962 struct sockaddr_in6 sin6;
1961 1963 } so_dst, so_ifp;
1962 1964
1963 1965 static struct {
1964 1966 struct rt_msghdr hdr;
1965 1967 char space[512];
1966 1968 } rtmsg;
1967 1969
1968 1970 static int
1969 1971 salen(struct sockaddr *sa)
1970 1972 {
1971 1973 switch (sa->sa_family) {
1972 1974 case AF_INET:
1973 1975 return (sizeof (struct sockaddr_in));
1974 1976 case AF_LINK:
1975 1977 return (sizeof (struct sockaddr_dl));
1976 1978 case AF_INET6:
1977 1979 return (sizeof (struct sockaddr_in6));
1978 1980 default:
1979 1981 return (sizeof (struct sockaddr));
1980 1982 }
1981 1983 }
1982 1984
1983 1985 #define ROUNDUP_LONG(a) \
1984 1986 ((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
1985 1987
1986 1988 /*
1987 1989 * Look up which zone is using a given IP address. The address in question
1988 1990 * is expected to have been stuffed into the structure to which lifr points
1989 1991 * via a previous SIOCGLIFADDR ioctl().
1990 1992 *
1991 1993 * This is done using black router socket magic.
1992 1994 *
1993 1995 * Return the name of the zone on success or NULL on failure.
1994 1996 *
1995 1997 * This is a lot of code for a simple task; a new ioctl request to take care
1996 1998 * of this might be a useful RFE.
1997 1999 */
1998 2000
1999 2001 static char *
2000 2002 who_is_using(zlog_t *zlogp, struct lifreq *lifr)
2001 2003 {
2002 2004 static char answer[ZONENAME_MAX];
2003 2005 pid_t pid;
2004 2006 int s, rlen, l, i;
2005 2007 char *cp = rtmsg.space;
2006 2008 struct sockaddr_dl *ifp = NULL;
2007 2009 struct sockaddr *sa;
2008 2010 char save_if_name[LIFNAMSIZ];
2009 2011
2010 2012 answer[0] = '\0';
2011 2013
2012 2014 pid = getpid();
2013 2015 if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
2014 2016 zerror(zlogp, B_TRUE, "could not get routing socket");
2015 2017 return (NULL);
2016 2018 }
2017 2019
2018 2020 if (lifr->lifr_addr.ss_family == AF_INET) {
2019 2021 struct sockaddr_in *sin4;
2020 2022
2021 2023 so_dst.sa.sa_family = AF_INET;
2022 2024 sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
2023 2025 so_dst.sin.sin_addr = sin4->sin_addr;
2024 2026 } else {
2025 2027 struct sockaddr_in6 *sin6;
2026 2028
2027 2029 so_dst.sa.sa_family = AF_INET6;
2028 2030 sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
2029 2031 so_dst.sin6.sin6_addr = sin6->sin6_addr;
2030 2032 }
2031 2033
2032 2034 so_ifp.sa.sa_family = AF_LINK;
2033 2035
2034 2036 (void) memset(&rtmsg, 0, sizeof (rtmsg));
2035 2037 rtmsg.hdr.rtm_type = RTM_GET;
2036 2038 rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
2037 2039 rtmsg.hdr.rtm_version = RTM_VERSION;
2038 2040 rtmsg.hdr.rtm_seq = ++rts_seqno;
2039 2041 rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
2040 2042
2041 2043 l = ROUNDUP_LONG(salen(&so_dst.sa));
2042 2044 (void) memmove(cp, &(so_dst), l);
2043 2045 cp += l;
2044 2046 l = ROUNDUP_LONG(salen(&so_ifp.sa));
2045 2047 (void) memmove(cp, &(so_ifp), l);
2046 2048 cp += l;
2047 2049
2048 2050 rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
2049 2051
2050 2052 if ((rlen = write(s, &rtmsg, l)) < 0) {
2051 2053 zerror(zlogp, B_TRUE, "writing to routing socket");
2052 2054 return (NULL);
2053 2055 } else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
2054 2056 zerror(zlogp, B_TRUE,
2055 2057 "write to routing socket got only %d for len\n", rlen);
2056 2058 return (NULL);
2057 2059 }
2058 2060 do {
2059 2061 l = read(s, &rtmsg, sizeof (rtmsg));
2060 2062 } while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
2061 2063 rtmsg.hdr.rtm_pid != pid));
2062 2064 if (l < 0) {
2063 2065 zerror(zlogp, B_TRUE, "reading from routing socket");
2064 2066 return (NULL);
2065 2067 }
2066 2068
2067 2069 if (rtmsg.hdr.rtm_version != RTM_VERSION) {
2068 2070 zerror(zlogp, B_FALSE,
2069 2071 "routing message version %d not understood",
2070 2072 rtmsg.hdr.rtm_version);
2071 2073 return (NULL);
2072 2074 }
2073 2075 if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
2074 2076 zerror(zlogp, B_FALSE, "message length mismatch, "
2075 2077 "expected %d bytes, returned %d bytes",
2076 2078 rtmsg.hdr.rtm_msglen, l);
2077 2079 return (NULL);
2078 2080 }
2079 2081 if (rtmsg.hdr.rtm_errno != 0) {
2080 2082 errno = rtmsg.hdr.rtm_errno;
2081 2083 zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
2082 2084 return (NULL);
2083 2085 }
2084 2086 if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
2085 2087 zerror(zlogp, B_FALSE, "network interface not found");
2086 2088 return (NULL);
2087 2089 }
2088 2090 cp = ((char *)(&rtmsg.hdr + 1));
2089 2091 for (i = 1; i != 0; i <<= 1) {
2090 2092 /* LINTED E_BAD_PTR_CAST_ALIGN */
2091 2093 sa = (struct sockaddr *)cp;
2092 2094 if (i != RTA_IFP) {
2093 2095 if ((i & rtmsg.hdr.rtm_addrs) != 0)
2094 2096 cp += ROUNDUP_LONG(salen(sa));
2095 2097 continue;
2096 2098 }
2097 2099 if (sa->sa_family == AF_LINK &&
2098 2100 ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
2099 2101 ifp = (struct sockaddr_dl *)sa;
2100 2102 break;
2101 2103 }
2102 2104 if (ifp == NULL) {
2103 2105 zerror(zlogp, B_FALSE, "network interface could not be "
2104 2106 "determined");
2105 2107 return (NULL);
2106 2108 }
2107 2109
2108 2110 /*
2109 2111 * We need to set the I/F name to what we got above, then do the
2110 2112 * appropriate ioctl to get its zone name. But lifr->lifr_name is
2111 2113 * used by the calling function to do a REMOVEIF, so if we leave the
2112 2114 * "good" zone's I/F name in place, *that* I/F will be removed instead
2113 2115 * of the bad one. So we save the old (bad) I/F name before over-
2114 2116 * writing it and doing the ioctl, then restore it after the ioctl.
2115 2117 */
2116 2118 (void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
2117 2119 (void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
2118 2120 lifr->lifr_name[ifp->sdl_nlen] = '\0';
2119 2121 i = ioctl(s, SIOCGLIFZONE, lifr);
2120 2122 (void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
2121 2123 if (i < 0) {
2122 2124 zerror(zlogp, B_TRUE,
2123 2125 "%s: could not determine the zone network interface "
2124 2126 "belongs to", lifr->lifr_name);
2125 2127 return (NULL);
2126 2128 }
2127 2129 if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
2128 2130 (void) snprintf(answer, sizeof (answer), "%d",
2129 2131 lifr->lifr_zoneid);
2130 2132
2131 2133 if (strlen(answer) > 0)
2132 2134 return (answer);
2133 2135 return (NULL);
2134 2136 }
2135 2137
2136 2138 /*
2137 2139 * Configures a single interface: a new virtual interface is added, based on
2138 2140 * the physical interface nwiftabptr->zone_nwif_physical, with the address
2139 2141 * specified in nwiftabptr->zone_nwif_address, for zone zone_id. Note that
2140 2142 * the "address" can be an IPv6 address (with a /prefixlength required), an
2141 2143 * IPv4 address (with a /prefixlength optional), or a name; for the latter,
2142 2144 * an IPv4 name-to-address resolution will be attempted.
2143 2145 *
2144 2146 * If anything goes wrong, we log an detailed error message, attempt to tear
2145 2147 * down whatever we set up and return an error.
2146 2148 */
2147 2149 static int
2148 2150 configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
2149 2151 struct zone_nwiftab *nwiftabptr)
2150 2152 {
2151 2153 struct lifreq lifr;
2152 2154 struct sockaddr_in netmask4;
2153 2155 struct sockaddr_in6 netmask6;
2154 2156 struct sockaddr_storage laddr;
2155 2157 struct in_addr in4;
2156 2158 sa_family_t af;
2157 2159 char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
2158 2160 int s;
2159 2161 boolean_t got_netmask = B_FALSE;
2160 2162 boolean_t is_loopback = B_FALSE;
2161 2163 char addrstr4[INET_ADDRSTRLEN];
2162 2164 int res;
2163 2165
2164 2166 res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
2165 2167 if (res != Z_OK) {
2166 2168 zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
2167 2169 nwiftabptr->zone_nwif_address);
2168 2170 return (-1);
2169 2171 }
2170 2172 af = lifr.lifr_addr.ss_family;
2171 2173 if (af == AF_INET)
2172 2174 in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
2173 2175 if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
2174 2176 zerror(zlogp, B_TRUE, "could not get socket");
2175 2177 return (-1);
2176 2178 }
2177 2179
2178 2180 /*
2179 2181 * This is a similar kind of "hack" like in addif() to get around
2180 2182 * the problem of SIOCLIFADDIF. The problem is that this ioctl
2181 2183 * does not include the netmask when adding a logical interface.
2182 2184 * To get around this problem, we first add the logical interface
2183 2185 * with a 0 address. After that, we set the netmask if provided.
2184 2186 * Finally we set the interface address.
2185 2187 */
2186 2188 laddr = lifr.lifr_addr;
2187 2189 (void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
2188 2190 sizeof (lifr.lifr_name));
2189 2191 (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
2190 2192
2191 2193 if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
2192 2194 /*
2193 2195 * Here, we know that the interface can't be brought up.
2194 2196 */
2195 2197 (void) close(s);
2196 2198 return (Z_OK);
2197 2199 }
2198 2200
2199 2201 /* Preserve literal IPv4 address for later potential printing. */
2200 2202 if (af == AF_INET)
2201 2203 (void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
2202 2204
2203 2205 lifr.lifr_zoneid = zone_id;
2204 2206 if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2205 2207 zerror(zlogp, B_TRUE, "%s: could not place network interface "
2206 2208 "into zone", lifr.lifr_name);
2207 2209 goto bad;
2208 2210 }
2209 2211
2210 2212 /*
2211 2213 * Loopback interface will use the default netmask assigned, if no
2212 2214 * netmask is found.
2213 2215 */
2214 2216 if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
2215 2217 is_loopback = B_TRUE;
2216 2218 }
2217 2219 if (af == AF_INET) {
2218 2220 /*
2219 2221 * The IPv4 netmask can be determined either
2220 2222 * directly if a prefix length was supplied with
2221 2223 * the address or via the netmasks database. Not
2222 2224 * being able to determine it is a common failure,
2223 2225 * but it often is not fatal to operation of the
2224 2226 * interface. In that case, a warning will be
2225 2227 * printed after the rest of the interface's
2226 2228 * parameters have been configured.
2227 2229 */
2228 2230 (void) memset(&netmask4, 0, sizeof (netmask4));
2229 2231 if (slashp != NULL) {
2230 2232 if (addr2netmask(slashp + 1, V4_ADDR_LEN,
2231 2233 (uchar_t *)&netmask4.sin_addr) != 0) {
2232 2234 *slashp = '/';
2233 2235 zerror(zlogp, B_FALSE,
2234 2236 "%s: invalid prefix length in %s",
2235 2237 lifr.lifr_name,
2236 2238 nwiftabptr->zone_nwif_address);
2237 2239 goto bad;
2238 2240 }
2239 2241 got_netmask = B_TRUE;
2240 2242 } else if (getnetmaskbyaddr(in4,
2241 2243 &netmask4.sin_addr) == 0) {
2242 2244 got_netmask = B_TRUE;
2243 2245 }
2244 2246 if (got_netmask) {
2245 2247 netmask4.sin_family = af;
2246 2248 (void) memcpy(&lifr.lifr_addr, &netmask4,
2247 2249 sizeof (netmask4));
2248 2250 }
2249 2251 } else {
2250 2252 (void) memset(&netmask6, 0, sizeof (netmask6));
2251 2253 if (addr2netmask(slashp + 1, V6_ADDR_LEN,
2252 2254 (uchar_t *)&netmask6.sin6_addr) != 0) {
2253 2255 *slashp = '/';
2254 2256 zerror(zlogp, B_FALSE,
2255 2257 "%s: invalid prefix length in %s",
2256 2258 lifr.lifr_name,
2257 2259 nwiftabptr->zone_nwif_address);
2258 2260 goto bad;
2259 2261 }
2260 2262 got_netmask = B_TRUE;
2261 2263 netmask6.sin6_family = af;
2262 2264 (void) memcpy(&lifr.lifr_addr, &netmask6,
2263 2265 sizeof (netmask6));
2264 2266 }
2265 2267 if (got_netmask &&
2266 2268 ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
2267 2269 zerror(zlogp, B_TRUE, "%s: could not set netmask",
2268 2270 lifr.lifr_name);
2269 2271 goto bad;
2270 2272 }
2271 2273
2272 2274 /* Set the interface address */
2273 2275 lifr.lifr_addr = laddr;
2274 2276 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
2275 2277 zerror(zlogp, B_TRUE,
2276 2278 "%s: could not set IP address to %s",
2277 2279 lifr.lifr_name, nwiftabptr->zone_nwif_address);
2278 2280 goto bad;
2279 2281 }
2280 2282
2281 2283 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2282 2284 zerror(zlogp, B_TRUE, "%s: could not get flags",
2283 2285 lifr.lifr_name);
2284 2286 goto bad;
2285 2287 }
2286 2288 lifr.lifr_flags |= IFF_UP;
2287 2289 if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
2288 2290 int save_errno = errno;
2289 2291 char *zone_using;
2290 2292
2291 2293 /*
2292 2294 * If we failed with something other than EADDRNOTAVAIL,
2293 2295 * then skip to the end. Otherwise, look up our address,
2294 2296 * then call a function to determine which zone is already
2295 2297 * using that address.
2296 2298 */
2297 2299 if (errno != EADDRNOTAVAIL) {
2298 2300 zerror(zlogp, B_TRUE,
2299 2301 "%s: could not bring network interface up",
2300 2302 lifr.lifr_name);
2301 2303 goto bad;
2302 2304 }
2303 2305 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
2304 2306 zerror(zlogp, B_TRUE, "%s: could not get address",
2305 2307 lifr.lifr_name);
2306 2308 goto bad;
2307 2309 }
2308 2310 zone_using = who_is_using(zlogp, &lifr);
2309 2311 errno = save_errno;
2310 2312 if (zone_using == NULL)
2311 2313 zerror(zlogp, B_TRUE,
2312 2314 "%s: could not bring network interface up",
2313 2315 lifr.lifr_name);
2314 2316 else
2315 2317 zerror(zlogp, B_TRUE, "%s: could not bring network "
2316 2318 "interface up: address in use by zone '%s'",
2317 2319 lifr.lifr_name, zone_using);
2318 2320 goto bad;
2319 2321 }
2320 2322
2321 2323 if (!got_netmask && !is_loopback) {
2322 2324 /*
2323 2325 * A common, but often non-fatal problem, is that the system
2324 2326 * cannot find the netmask for an interface address. This is
2325 2327 * often caused by it being only in /etc/inet/netmasks, but
2326 2328 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
2327 2329 * in that. This doesn't show up at boot because the netmask
2328 2330 * is obtained from /etc/inet/netmasks when no network
2329 2331 * interfaces are up, but isn't consulted when NIS/NIS+ is
2330 2332 * available. We warn the user here that something like this
2331 2333 * has happened and we're just running with a default and
2332 2334 * possible incorrect netmask.
2333 2335 */
2334 2336 char buffer[INET6_ADDRSTRLEN];
2335 2337 void *addr;
2336 2338 const char *nomatch = "no matching subnet found in netmasks(4)";
2337 2339
2338 2340 if (af == AF_INET)
2339 2341 addr = &((struct sockaddr_in *)
2340 2342 (&lifr.lifr_addr))->sin_addr;
2341 2343 else
2342 2344 addr = &((struct sockaddr_in6 *)
2343 2345 (&lifr.lifr_addr))->sin6_addr;
2344 2346
2345 2347 /*
2346 2348 * Find out what netmask the interface is going to be using.
2347 2349 * If we just brought up an IPMP data address on an underlying
2348 2350 * interface above, the address will have already migrated, so
2349 2351 * the SIOCGLIFNETMASK won't be able to find it (but we need
2350 2352 * to bring the address up to get the actual netmask). Just
2351 2353 * omit printing the actual netmask in this corner-case.
2352 2354 */
2353 2355 if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
2354 2356 inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) {
2355 2357 zerror(zlogp, B_FALSE, "WARNING: %s; using default.",
2356 2358 nomatch);
2357 2359 } else {
2358 2360 zerror(zlogp, B_FALSE,
2359 2361 "WARNING: %s: %s: %s; using default of %s.",
2360 2362 lifr.lifr_name, nomatch, addrstr4, buffer);
2361 2363 }
2362 2364 }
2363 2365
2364 2366 /*
2365 2367 * If a default router was specified for this interface
2366 2368 * set the route now. Ignore if already set.
2367 2369 */
2368 2370 if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
2369 2371 int status;
2370 2372 char *argv[7];
2371 2373
2372 2374 argv[0] = "route";
2373 2375 argv[1] = "add";
2374 2376 argv[2] = "-ifp";
2375 2377 argv[3] = nwiftabptr->zone_nwif_physical;
2376 2378 argv[4] = "default";
2377 2379 argv[5] = nwiftabptr->zone_nwif_defrouter;
2378 2380 argv[6] = NULL;
2379 2381
2380 2382 status = forkexec(zlogp, "/usr/sbin/route", argv);
2381 2383 if (status != 0 && status != EEXIST)
2382 2384 zerror(zlogp, B_FALSE, "Unable to set route for "
2383 2385 "interface %s to %s\n",
2384 2386 nwiftabptr->zone_nwif_physical,
2385 2387 nwiftabptr->zone_nwif_defrouter);
2386 2388 }
2387 2389
2388 2390 (void) close(s);
2389 2391 return (Z_OK);
2390 2392 bad:
2391 2393 (void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
2392 2394 (void) close(s);
2393 2395 return (-1);
2394 2396 }
2395 2397
2396 2398 /*
2397 2399 * Sets up network interfaces based on information from the zone configuration.
2398 2400 * IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global
2399 2401 * system.
2400 2402 *
2401 2403 * If anything goes wrong, we log a general error message, attempt to tear down
2402 2404 * whatever we set up, and return an error.
2403 2405 */
2404 2406 static int
2405 2407 configure_shared_network_interfaces(zlog_t *zlogp)
2406 2408 {
2407 2409 struct zone_nwiftab nwiftab, loopback_iftab;
2408 2410 zoneid_t zoneid;
2409 2411
2410 2412 if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
2411 2413 zerror(zlogp, B_TRUE, "unable to get zoneid");
2412 2414 return (-1);
2413 2415 }
2414 2416
2415 2417 if (zonecfg_setnwifent(snap_hndl) == Z_OK) {
2416 2418 for (;;) {
2417 2419 if (zonecfg_getnwifent(snap_hndl, &nwiftab) != Z_OK)
2418 2420 break;
2419 2421 nwifent_free_attrs(&nwiftab);
2420 2422 if (configure_one_interface(zlogp, zoneid, &nwiftab) !=
2421 2423 Z_OK) {
2422 2424 (void) zonecfg_endnwifent(snap_hndl);
2423 2425 return (-1);
2424 2426 }
2425 2427 }
2426 2428 (void) zonecfg_endnwifent(snap_hndl);
2427 2429 }
2428 2430 if (is_system_labeled()) {
2429 2431 /*
2430 2432 * Labeled zones share the loopback interface
2431 2433 * so it is not plumbed for shared stack instances.
2432 2434 */
2433 2435 return (0);
2434 2436 }
2435 2437 (void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
2436 2438 sizeof (loopback_iftab.zone_nwif_physical));
2437 2439 (void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
2438 2440 sizeof (loopback_iftab.zone_nwif_address));
2439 2441 loopback_iftab.zone_nwif_defrouter[0] = '\0';
2440 2442 if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
2441 2443 return (-1);
2442 2444
2443 2445 /* Always plumb up the IPv6 loopback interface. */
2444 2446 (void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
2445 2447 sizeof (loopback_iftab.zone_nwif_address));
2446 2448 if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
2447 2449 return (-1);
2448 2450 return (0);
2449 2451 }
2450 2452
2451 2453 static void
2452 2454 zdlerror(zlog_t *zlogp, dladm_status_t err, const char *dlname, const char *str)
2453 2455 {
2454 2456 char errmsg[DLADM_STRSIZE];
2455 2457
2456 2458 (void) dladm_status2str(err, errmsg);
2457 2459 zerror(zlogp, B_FALSE, "%s '%s': %s", str, dlname, errmsg);
2458 2460 }
2459 2461
2460 2462 static int
2461 2463 add_datalink(zlog_t *zlogp, char *zone_name, datalink_id_t linkid, char *dlname)
2462 2464 {
2463 2465 dladm_status_t err;
2464 2466 boolean_t cpuset, poolset;
2465 2467 char *poolp;
2466 2468
2467 2469 /* First check if it's in use by global zone. */
2468 2470 if (zonecfg_ifname_exists(AF_INET, dlname) ||
2469 2471 zonecfg_ifname_exists(AF_INET6, dlname)) {
2470 2472 zerror(zlogp, B_FALSE, "WARNING: skipping network interface "
2471 2473 "'%s' which is used in the global zone", dlname);
2472 2474 return (-1);
2473 2475 }
2474 2476
2475 2477 /* Set zoneid of this link. */
2476 2478 err = dladm_set_linkprop(dld_handle, linkid, "zone", &zone_name, 1,
2477 2479 DLADM_OPT_ACTIVE);
2478 2480 if (err != DLADM_STATUS_OK) {
2479 2481 zdlerror(zlogp, err, dlname,
2480 2482 "WARNING: unable to add network interface");
2481 2483 return (-1);
2482 2484 }
2483 2485
2484 2486 /*
2485 2487 * Set the pool of this link if the zone has a pool and
2486 2488 * neither the cpus nor the pool datalink property is
2487 2489 * already set.
2488 2490 */
2489 2491 err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
2490 2492 "cpus", &cpuset);
2491 2493 if (err != DLADM_STATUS_OK) {
2492 2494 zdlerror(zlogp, err, dlname,
2493 2495 "WARNING: unable to check if cpus link property is set");
2494 2496 }
2495 2497 err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
2496 2498 "pool", &poolset);
2497 2499 if (err != DLADM_STATUS_OK) {
2498 2500 zdlerror(zlogp, err, dlname,
2499 2501 "WARNING: unable to check if pool link property is set");
2500 2502 }
2501 2503
2502 2504 if ((strlen(pool_name) != 0) && !cpuset && !poolset) {
2503 2505 poolp = pool_name;
2504 2506 err = dladm_set_linkprop(dld_handle, linkid, "pool",
2505 2507 &poolp, 1, DLADM_OPT_ACTIVE);
2506 2508 if (err != DLADM_STATUS_OK) {
2507 2509 zerror(zlogp, B_FALSE, "WARNING: unable to set "
2508 2510 "pool %s to datalink %s", pool_name, dlname);
2509 2511 bzero(pool_name, sizeof (pool_name));
2510 2512 }
2511 2513 } else {
2512 2514 bzero(pool_name, sizeof (pool_name));
2513 2515 }
2514 2516 return (0);
2515 2517 }
2516 2518
2517 2519 static boolean_t
2518 2520 sockaddr_to_str(sa_family_t af, const struct sockaddr *sockaddr,
2519 2521 char *straddr, size_t len)
2520 2522 {
2521 2523 struct sockaddr_in *sin;
2522 2524 struct sockaddr_in6 *sin6;
2523 2525 const char *str = NULL;
2524 2526
2525 2527 if (af == AF_INET) {
2526 2528 /* LINTED E_BAD_PTR_CAST_ALIGN */
2527 2529 sin = SIN(sockaddr);
2528 2530 str = inet_ntop(AF_INET, (void *)&sin->sin_addr, straddr, len);
2529 2531 } else if (af == AF_INET6) {
2530 2532 /* LINTED E_BAD_PTR_CAST_ALIGN */
2531 2533 sin6 = SIN6(sockaddr);
2532 2534 str = inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, straddr,
2533 2535 len);
2534 2536 }
2535 2537
2536 2538 return (str != NULL);
2537 2539 }
2538 2540
2539 2541 static int
2540 2542 ipv4_prefixlen(struct sockaddr_in *sin)
2541 2543 {
2542 2544 struct sockaddr_in *m;
2543 2545 struct sockaddr_storage mask;
2544 2546
2545 2547 m = SIN(&mask);
2546 2548 m->sin_family = AF_INET;
2547 2549 if (getnetmaskbyaddr(sin->sin_addr, &m->sin_addr) == 0) {
2548 2550 return (mask2plen((struct sockaddr *)&mask));
2549 2551 } else if (IN_CLASSA(htonl(sin->sin_addr.s_addr))) {
2550 2552 return (8);
2551 2553 } else if (IN_CLASSB(ntohl(sin->sin_addr.s_addr))) {
2552 2554 return (16);
2553 2555 } else if (IN_CLASSC(ntohl(sin->sin_addr.s_addr))) {
2554 2556 return (24);
2555 2557 }
2556 2558 return (0);
2557 2559 }
2558 2560
2559 2561 static int
2560 2562 zone_setattr_network(int type, zoneid_t zoneid, datalink_id_t linkid,
2561 2563 void *buf, size_t bufsize)
2562 2564 {
2563 2565 zone_net_data_t *zndata;
2564 2566 size_t znsize;
2565 2567 int err;
2566 2568
2567 2569 znsize = sizeof (*zndata) + bufsize;
2568 2570 zndata = calloc(1, znsize);
2569 2571 if (zndata == NULL)
2570 2572 return (ENOMEM);
2571 2573 zndata->zn_type = type;
2572 2574 zndata->zn_len = bufsize;
2573 2575 zndata->zn_linkid = linkid;
2574 2576 bcopy(buf, zndata->zn_val, zndata->zn_len);
2575 2577 err = zone_setattr(zoneid, ZONE_ATTR_NETWORK, zndata, znsize);
2576 2578 free(zndata);
2577 2579 return (err);
2578 2580 }
2579 2581
2580 2582 static int
2581 2583 add_net_for_linkid(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *start)
2582 2584 {
2583 2585 struct lifreq lifr;
2584 2586 char **astr, *address;
2585 2587 dladm_status_t dlstatus;
2586 2588 char *ip_nospoof = "ip-nospoof";
2587 2589 int nnet, naddr, err = 0, j;
2588 2590 size_t zlen, cpleft;
2589 2591 zone_addr_list_t *ptr, *end;
2590 2592 char tmp[INET6_ADDRSTRLEN], *maskstr;
2591 2593 char *zaddr, *cp;
2592 2594 struct in6_addr *routes = NULL;
2593 2595 boolean_t is_set;
2594 2596 datalink_id_t linkid;
2595 2597
2596 2598 assert(start != NULL);
2597 2599 naddr = 0; /* number of addresses */
2598 2600 nnet = 0; /* number of net resources */
2599 2601 linkid = start->za_linkid;
2600 2602 for (ptr = start; ptr != NULL && ptr->za_linkid == linkid;
2601 2603 ptr = ptr->za_next) {
2602 2604 nnet++;
2603 2605 }
2604 2606 end = ptr;
2605 2607 zlen = nnet * (INET6_ADDRSTRLEN + 1);
2606 2608 astr = calloc(1, nnet * sizeof (uintptr_t));
2607 2609 zaddr = calloc(1, zlen);
2608 2610 if (astr == NULL || zaddr == NULL) {
2609 2611 err = ENOMEM;
2610 2612 goto done;
2611 2613 }
2612 2614 cp = zaddr;
2613 2615 cpleft = zlen;
2614 2616 j = 0;
2615 2617 for (ptr = start; ptr != end; ptr = ptr->za_next) {
2616 2618 address = ptr->za_nwiftab.zone_nwif_allowed_address;
2617 2619 if (address[0] == '\0')
2618 2620 continue;
2619 2621 (void) snprintf(tmp, sizeof (tmp), "%s", address);
2620 2622 /*
2621 2623 * Validate the data. zonecfg_valid_net_address() clobbers
2622 2624 * the /<mask> in the address string.
2623 2625 */
2624 2626 if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
2625 2627 zerror(zlogp, B_FALSE, "invalid address [%s]\n",
2626 2628 address);
2627 2629 err = EINVAL;
2628 2630 goto done;
2629 2631 }
2630 2632 /*
2631 2633 * convert any hostnames to numeric address strings.
2632 2634 */
2633 2635 if (!sockaddr_to_str(lifr.lifr_addr.ss_family,
2634 2636 (const struct sockaddr *)&lifr.lifr_addr, cp, cpleft)) {
2635 2637 err = EINVAL;
2636 2638 goto done;
2637 2639 }
2638 2640 /*
2639 2641 * make a copy of the numeric string for the data needed
2640 2642 * by the "allowed-ips" datalink property.
2641 2643 */
2642 2644 astr[j] = strdup(cp);
2643 2645 if (astr[j] == NULL) {
2644 2646 err = ENOMEM;
2645 2647 goto done;
2646 2648 }
2647 2649 j++;
2648 2650 /*
2649 2651 * compute the default netmask from the address, if necessary
2650 2652 */
2651 2653 if ((maskstr = strchr(tmp, '/')) == NULL) {
2652 2654 int prefixlen;
2653 2655
2654 2656 if (lifr.lifr_addr.ss_family == AF_INET) {
2655 2657 prefixlen = ipv4_prefixlen(
2656 2658 SIN(&lifr.lifr_addr));
2657 2659 } else {
2658 2660 struct sockaddr_in6 *sin6;
2659 2661
2660 2662 sin6 = SIN6(&lifr.lifr_addr);
2661 2663 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2662 2664 prefixlen = 10;
2663 2665 else
2664 2666 prefixlen = 64;
2665 2667 }
2666 2668 (void) snprintf(tmp, sizeof (tmp), "%d", prefixlen);
2667 2669 maskstr = tmp;
2668 2670 } else {
2669 2671 maskstr++;
2670 2672 }
2671 2673 /* append the "/<netmask>" */
2672 2674 (void) strlcat(cp, "/", cpleft);
2673 2675 (void) strlcat(cp, maskstr, cpleft);
2674 2676 (void) strlcat(cp, ",", cpleft);
2675 2677 cp += strnlen(cp, zlen);
2676 2678 cpleft = &zaddr[INET6_ADDRSTRLEN] - cp;
2677 2679 }
2678 2680 naddr = j; /* the actual number of addresses in the net resource */
2679 2681 assert(naddr <= nnet);
2680 2682
2681 2683 /*
2682 2684 * zonecfg has already verified that the defrouter property can only
2683 2685 * be set if there is at least one address defined for the net resource.
2684 2686 * If j is 0, there are no addresses defined, and therefore no routers
2685 2687 * to configure, and we are done at that point.
2686 2688 */
2687 2689 if (j == 0)
2688 2690 goto done;
2689 2691
2690 2692 /* over-write last ',' with '\0' */
2691 2693 zaddr[strnlen(zaddr, zlen) + 1] = '\0';
2692 2694
2693 2695 /*
2694 2696 * First make sure L3 protection is not already set on the link.
2695 2697 */
2696 2698 dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
2697 2699 "protection", &is_set);
2698 2700 if (dlstatus != DLADM_STATUS_OK) {
2699 2701 err = EINVAL;
2700 2702 zerror(zlogp, B_FALSE, "unable to check if protection is set");
2701 2703 goto done;
2702 2704 }
2703 2705 if (is_set) {
2704 2706 err = EINVAL;
2705 2707 zerror(zlogp, B_FALSE, "Protection is already set");
2706 2708 goto done;
2707 2709 }
2708 2710 dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
2709 2711 "allowed-ips", &is_set);
2710 2712 if (dlstatus != DLADM_STATUS_OK) {
2711 2713 err = EINVAL;
2712 2714 zerror(zlogp, B_FALSE, "unable to check if allowed-ips is set");
2713 2715 goto done;
2714 2716 }
2715 2717 if (is_set) {
2716 2718 zerror(zlogp, B_FALSE, "allowed-ips is already set");
2717 2719 err = EINVAL;
2718 2720 goto done;
2719 2721 }
2720 2722
2721 2723 /*
2722 2724 * Enable ip-nospoof for the link, and add address to the allowed-ips
2723 2725 * list.
2724 2726 */
2725 2727 dlstatus = dladm_set_linkprop(dld_handle, linkid, "protection",
2726 2728 &ip_nospoof, 1, DLADM_OPT_ACTIVE);
2727 2729 if (dlstatus != DLADM_STATUS_OK) {
2728 2730 zerror(zlogp, B_FALSE, "could not set protection\n");
2729 2731 err = EINVAL;
2730 2732 goto done;
2731 2733 }
2732 2734 dlstatus = dladm_set_linkprop(dld_handle, linkid, "allowed-ips",
2733 2735 astr, naddr, DLADM_OPT_ACTIVE);
2734 2736 if (dlstatus != DLADM_STATUS_OK) {
2735 2737 zerror(zlogp, B_FALSE, "could not set allowed-ips\n");
2736 2738 err = EINVAL;
2737 2739 goto done;
2738 2740 }
2739 2741
2740 2742 /* now set the address in the data-store */
2741 2743 err = zone_setattr_network(ZONE_NETWORK_ADDRESS, zoneid, linkid,
2742 2744 zaddr, strnlen(zaddr, zlen) + 1);
2743 2745 if (err != 0)
2744 2746 goto done;
2745 2747
2746 2748 /*
2747 2749 * add the defaultrouters
2748 2750 */
2749 2751 routes = calloc(1, nnet * sizeof (*routes));
2750 2752 j = 0;
2751 2753 for (ptr = start; ptr != end; ptr = ptr->za_next) {
2752 2754 address = ptr->za_nwiftab.zone_nwif_defrouter;
2753 2755 if (address[0] == '\0')
2754 2756 continue;
2755 2757 if (strchr(address, '/') == NULL && strchr(address, ':') != 0) {
2756 2758 /*
2757 2759 * zonecfg_valid_net_address() expects numeric IPv6
2758 2760 * addresses to have a CIDR format netmask.
2759 2761 */
2760 2762 (void) snprintf(tmp, sizeof (tmp), "/%d", V6_ADDR_LEN);
2761 2763 (void) strlcat(address, tmp, INET6_ADDRSTRLEN);
2762 2764 }
2763 2765 if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
2764 2766 zerror(zlogp, B_FALSE,
2765 2767 "invalid router [%s]\n", address);
2766 2768 err = EINVAL;
2767 2769 goto done;
2768 2770 }
2769 2771 if (lifr.lifr_addr.ss_family == AF_INET6) {
2770 2772 routes[j] = SIN6(&lifr.lifr_addr)->sin6_addr;
2771 2773 } else {
2772 2774 IN6_INADDR_TO_V4MAPPED(&SIN(&lifr.lifr_addr)->sin_addr,
2773 2775 &routes[j]);
2774 2776 }
2775 2777 j++;
2776 2778 }
2777 2779 assert(j <= nnet);
2778 2780 if (j > 0) {
2779 2781 err = zone_setattr_network(ZONE_NETWORK_DEFROUTER, zoneid,
2780 2782 linkid, routes, j * sizeof (*routes));
2781 2783 }
2782 2784 done:
2783 2785 free(routes);
2784 2786 for (j = 0; j < naddr; j++)
2785 2787 free(astr[j]);
2786 2788 free(astr);
2787 2789 free(zaddr);
2788 2790 return (err);
2789 2791
2790 2792 }
2791 2793
2792 2794 static int
2793 2795 add_net(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *zalist)
2794 2796 {
2795 2797 zone_addr_list_t *ptr;
2796 2798 datalink_id_t linkid;
2797 2799 int err;
2798 2800
2799 2801 if (zalist == NULL)
2800 2802 return (0);
2801 2803
2802 2804 linkid = zalist->za_linkid;
2803 2805
2804 2806 err = add_net_for_linkid(zlogp, zoneid, zalist);
2805 2807 if (err != 0)
2806 2808 return (err);
2807 2809
2808 2810 for (ptr = zalist; ptr != NULL; ptr = ptr->za_next) {
2809 2811 if (ptr->za_linkid == linkid)
2810 2812 continue;
2811 2813 linkid = ptr->za_linkid;
2812 2814 err = add_net_for_linkid(zlogp, zoneid, ptr);
2813 2815 if (err != 0)
2814 2816 return (err);
2815 2817 }
2816 2818 return (0);
2817 2819 }
2818 2820
2819 2821 /*
2820 2822 * Add "new" to the list of network interfaces to be configured by
2821 2823 * add_net on zone boot in "old". The list of interfaces in "old" is
2822 2824 * sorted by datalink_id_t, with interfaces sorted FIFO for a given
2823 2825 * datalink_id_t.
2824 2826 *
2825 2827 * Returns the merged list of IP interfaces containing "old" and "new"
2826 2828 */
2827 2829 static zone_addr_list_t *
2828 2830 add_ip_interface(zone_addr_list_t *old, zone_addr_list_t *new)
2829 2831 {
2830 2832 zone_addr_list_t *ptr, *next;
2831 2833 datalink_id_t linkid = new->za_linkid;
2832 2834
2833 2835 assert(old != new);
2834 2836
2835 2837 if (old == NULL)
2836 2838 return (new);
2837 2839 for (ptr = old; ptr != NULL; ptr = ptr->za_next) {
2838 2840 if (ptr->za_linkid == linkid)
2839 2841 break;
2840 2842 }
2841 2843 if (ptr == NULL) {
2842 2844 /* linkid does not already exist, add to the beginning */
2843 2845 new->za_next = old;
2844 2846 return (new);
2845 2847 }
2846 2848 /*
2847 2849 * adding to the middle of the list; ptr points at the first
2848 2850 * occurrence of linkid. Find the last occurrence.
2849 2851 */
2850 2852 while ((next = ptr->za_next) != NULL) {
2851 2853 if (next->za_linkid != linkid)
2852 2854 break;
2853 2855 ptr = next;
2854 2856 }
2855 2857 /* insert new after ptr */
2856 2858 new->za_next = next;
2857 2859 ptr->za_next = new;
2858 2860 return (old);
2859 2861 }
2860 2862
2861 2863 void
2862 2864 free_ip_interface(zone_addr_list_t *zalist)
2863 2865 {
2864 2866 zone_addr_list_t *ptr, *new;
2865 2867
2866 2868 for (ptr = zalist; ptr != NULL; ) {
2867 2869 new = ptr;
2868 2870 ptr = ptr->za_next;
2869 2871 free(new);
2870 2872 }
2871 2873 }
2872 2874
2873 2875 /*
2874 2876 * Add the kernel access control information for the interface names.
2875 2877 * If anything goes wrong, we log a general error message, attempt to tear down
2876 2878 * whatever we set up, and return an error.
2877 2879 */
2878 2880 static int
2879 2881 configure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
2880 2882 {
2881 2883 struct zone_nwiftab nwiftab;
2882 2884 char rootpath[MAXPATHLEN];
2883 2885 char path[MAXPATHLEN];
2884 2886 datalink_id_t linkid;
2885 2887 di_prof_t prof = NULL;
2886 2888 boolean_t added = B_FALSE;
2887 2889 zone_addr_list_t *zalist = NULL, *new;
2888 2890
2889 2891 if (zonecfg_setnwifent(snap_hndl) != Z_OK)
2890 2892 return (0);
2891 2893
2892 2894 for (;;) {
2893 2895 if (zonecfg_getnwifent(snap_hndl, &nwiftab) != Z_OK)
2894 2896 break;
2895 2897
2896 2898 nwifent_free_attrs(&nwiftab);
2897 2899 if (prof == NULL) {
2898 2900 if (zone_get_devroot(zone_name, rootpath,
2899 2901 sizeof (rootpath)) != Z_OK) {
2900 2902 (void) zonecfg_endnwifent(snap_hndl);
2901 2903 zerror(zlogp, B_TRUE,
2902 2904 "unable to determine dev root");
2903 2905 return (-1);
2904 2906 }
2905 2907 (void) snprintf(path, sizeof (path), "%s%s", rootpath,
2906 2908 "/dev");
2907 2909 if (di_prof_init(path, &prof) != 0) {
2908 2910 (void) zonecfg_endnwifent(snap_hndl);
2909 2911 zerror(zlogp, B_TRUE,
2910 2912 "failed to initialize profile");
2911 2913 return (-1);
2912 2914 }
2913 2915 }
2914 2916
2915 2917 /*
2916 2918 * Create the /dev entry for backward compatibility.
2917 2919 * Only create the /dev entry if it's not in use.
2918 2920 * Note that the zone still boots when the assigned
2919 2921 * interface is inaccessible, used by others, etc.
2920 2922 * Also, when vanity naming is used, some interface do
2921 2923 * do not have corresponding /dev node names (for example,
2922 2924 * vanity named aggregations). The /dev entry is not
2923 2925 * created in that case. The /dev/net entry is always
2924 2926 * accessible.
2925 2927 */
2926 2928 if (dladm_name2info(dld_handle, nwiftab.zone_nwif_physical,
2927 2929 &linkid, NULL, NULL, NULL) == DLADM_STATUS_OK &&
2928 2930 add_datalink(zlogp, zone_name, linkid,
2929 2931 nwiftab.zone_nwif_physical) == 0) {
2930 2932 added = B_TRUE;
2931 2933 } else {
2932 2934 /*
2933 2935 * Failed to add network device, but the brand hook
2934 2936 * might be doing this for us, so keep silent.
2935 2937 */
2936 2938 continue;
2937 2939 }
2938 2940 /* set up the new IP interface, and add them all later */
2939 2941 new = malloc(sizeof (*new));
2940 2942 if (new == NULL) {
2941 2943 zerror(zlogp, B_TRUE, "no memory for %s",
2942 2944 nwiftab.zone_nwif_physical);
2943 2945 free_ip_interface(zalist);
2944 2946 }
2945 2947 bzero(new, sizeof (*new));
2946 2948 new->za_nwiftab = nwiftab;
2947 2949 new->za_linkid = linkid;
2948 2950 zalist = add_ip_interface(zalist, new);
2949 2951 }
2950 2952 if (zalist != NULL) {
2951 2953 if ((errno = add_net(zlogp, zoneid, zalist)) != 0) {
2952 2954 (void) zonecfg_endnwifent(snap_hndl);
2953 2955 zerror(zlogp, B_TRUE, "failed to add address");
2954 2956 free_ip_interface(zalist);
2955 2957 return (-1);
2956 2958 }
2957 2959 free_ip_interface(zalist);
2958 2960 }
2959 2961 (void) zonecfg_endnwifent(snap_hndl);
2960 2962
2961 2963 if (prof != NULL && added) {
2962 2964 if (di_prof_commit(prof) != 0) {
2963 2965 zerror(zlogp, B_TRUE, "failed to commit profile");
2964 2966 return (-1);
2965 2967 }
2966 2968 }
2967 2969 if (prof != NULL)
2968 2970 di_prof_fini(prof);
2969 2971
2970 2972 return (0);
2971 2973 }
2972 2974
2973 2975 static int
2974 2976 remove_datalink_pool(zlog_t *zlogp, zoneid_t zoneid)
2975 2977 {
2976 2978 ushort_t flags;
2977 2979 zone_iptype_t iptype;
2978 2980 int i, dlnum = 0;
2979 2981 datalink_id_t *dllink, *dllinks = NULL;
2980 2982 dladm_status_t err;
2981 2983
2982 2984 if (strlen(pool_name) == 0)
2983 2985 return (0);
2984 2986
2985 2987 if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
2986 2988 sizeof (flags)) < 0) {
2987 2989 if (vplat_get_iptype(zlogp, &iptype) < 0) {
2988 2990 zerror(zlogp, B_FALSE, "unable to determine ip-type");
2989 2991 return (-1);
2990 2992 }
2991 2993 } else {
2992 2994 if (flags & ZF_NET_EXCL)
2993 2995 iptype = ZS_EXCLUSIVE;
2994 2996 else
2995 2997 iptype = ZS_SHARED;
2996 2998 }
2997 2999
2998 3000 if (iptype == ZS_EXCLUSIVE) {
2999 3001 /*
3000 3002 * Get the datalink count and for each datalink,
3001 3003 * attempt to clear the pool property and clear
3002 3004 * the pool_name.
3003 3005 */
3004 3006 if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
3005 3007 zerror(zlogp, B_TRUE, "unable to count network "
3006 3008 "interfaces");
3007 3009 return (-1);
3008 3010 }
3009 3011
3010 3012 if (dlnum == 0)
3011 3013 return (0);
3012 3014
3013 3015 if ((dllinks = malloc(dlnum * sizeof (datalink_id_t)))
3014 3016 == NULL) {
3015 3017 zerror(zlogp, B_TRUE, "memory allocation failed");
3016 3018 return (-1);
3017 3019 }
3018 3020 if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
3019 3021 zerror(zlogp, B_TRUE, "unable to list network "
3020 3022 "interfaces");
3021 3023 return (-1);
3022 3024 }
3023 3025
3024 3026 bzero(pool_name, sizeof (pool_name));
3025 3027 for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
3026 3028 err = dladm_set_linkprop(dld_handle, *dllink, "pool",
3027 3029 NULL, 0, DLADM_OPT_ACTIVE);
3028 3030 if (err != DLADM_STATUS_OK) {
3029 3031 zerror(zlogp, B_TRUE,
3030 3032 "WARNING: unable to clear pool");
3031 3033 }
3032 3034 }
3033 3035 free(dllinks);
3034 3036 }
3035 3037 return (0);
3036 3038 }
3037 3039
3038 3040 static int
3039 3041 remove_datalink_protect(zlog_t *zlogp, zoneid_t zoneid)
3040 3042 {
3041 3043 ushort_t flags;
3042 3044 zone_iptype_t iptype;
3043 3045 int i, dlnum = 0;
3044 3046 dladm_status_t dlstatus;
3045 3047 datalink_id_t *dllink, *dllinks = NULL;
3046 3048
3047 3049 if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
3048 3050 sizeof (flags)) < 0) {
3049 3051 if (vplat_get_iptype(zlogp, &iptype) < 0) {
3050 3052 zerror(zlogp, B_FALSE, "unable to determine ip-type");
3051 3053 return (-1);
3052 3054 }
3053 3055 } else {
3054 3056 if (flags & ZF_NET_EXCL)
3055 3057 iptype = ZS_EXCLUSIVE;
3056 3058 else
3057 3059 iptype = ZS_SHARED;
3058 3060 }
3059 3061
3060 3062 if (iptype != ZS_EXCLUSIVE)
3061 3063 return (0);
3062 3064
3063 3065 /*
3064 3066 * Get the datalink count and for each datalink,
3065 3067 * attempt to clear the pool property and clear
3066 3068 * the pool_name.
3067 3069 */
3068 3070 if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
3069 3071 zerror(zlogp, B_TRUE, "unable to count network interfaces");
3070 3072 return (-1);
3071 3073 }
3072 3074
3073 3075 if (dlnum == 0)
3074 3076 return (0);
3075 3077
3076 3078 if ((dllinks = malloc(dlnum * sizeof (datalink_id_t))) == NULL) {
3077 3079 zerror(zlogp, B_TRUE, "memory allocation failed");
3078 3080 return (-1);
3079 3081 }
3080 3082 if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
3081 3083 zerror(zlogp, B_TRUE, "unable to list network interfaces");
3082 3084 free(dllinks);
3083 3085 return (-1);
3084 3086 }
3085 3087
3086 3088 for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
3087 3089 char dlerr[DLADM_STRSIZE];
3088 3090
3089 3091 dlstatus = dladm_set_linkprop(dld_handle, *dllink,
3090 3092 "protection", NULL, 0, DLADM_OPT_ACTIVE);
3091 3093 if (dlstatus == DLADM_STATUS_NOTFOUND) {
3092 3094 /* datalink does not belong to the GZ */
3093 3095 continue;
3094 3096 }
3095 3097 if (dlstatus != DLADM_STATUS_OK)
3096 3098 zerror(zlogp, B_FALSE,
3097 3099 "clear 'protection' link property: %s",
3098 3100 dladm_status2str(dlstatus, dlerr));
3099 3101
3100 3102 dlstatus = dladm_set_linkprop(dld_handle, *dllink,
3101 3103 "allowed-ips", NULL, 0, DLADM_OPT_ACTIVE);
3102 3104 if (dlstatus != DLADM_STATUS_OK)
3103 3105 zerror(zlogp, B_FALSE,
3104 3106 "clear 'allowed-ips' link property: %s",
3105 3107 dladm_status2str(dlstatus, dlerr));
3106 3108 }
3107 3109 free(dllinks);
3108 3110 return (0);
3109 3111 }
3110 3112
3111 3113 static int
3112 3114 tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
3113 3115 const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
3114 3116 {
3115 3117 int fd;
3116 3118 struct strioctl ioc;
3117 3119 tcp_ioc_abort_conn_t conn;
3118 3120 int error;
3119 3121
3120 3122 conn.ac_local = *local;
3121 3123 conn.ac_remote = *remote;
3122 3124 conn.ac_start = TCPS_SYN_SENT;
3123 3125 conn.ac_end = TCPS_TIME_WAIT;
3124 3126 conn.ac_zoneid = zoneid;
3125 3127
3126 3128 ioc.ic_cmd = TCP_IOC_ABORT_CONN;
3127 3129 ioc.ic_timout = -1; /* infinite timeout */
3128 3130 ioc.ic_len = sizeof (conn);
3129 3131 ioc.ic_dp = (char *)&conn;
3130 3132
3131 3133 if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
3132 3134 zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
3133 3135 return (-1);
3134 3136 }
3135 3137
3136 3138 error = ioctl(fd, I_STR, &ioc);
3137 3139 (void) close(fd);
3138 3140 if (error == 0 || errno == ENOENT) /* ENOENT is not an error */
3139 3141 return (0);
3140 3142 return (-1);
3141 3143 }
3142 3144
3143 3145 static int
3144 3146 tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
3145 3147 {
3146 3148 struct sockaddr_storage l, r;
3147 3149 struct sockaddr_in *local, *remote;
3148 3150 struct sockaddr_in6 *local6, *remote6;
3149 3151 int error;
3150 3152
3151 3153 /*
3152 3154 * Abort IPv4 connections.
3153 3155 */
3154 3156 bzero(&l, sizeof (*local));
3155 3157 local = (struct sockaddr_in *)&l;
3156 3158 local->sin_family = AF_INET;
3157 3159 local->sin_addr.s_addr = INADDR_ANY;
3158 3160 local->sin_port = 0;
3159 3161
3160 3162 bzero(&r, sizeof (*remote));
3161 3163 remote = (struct sockaddr_in *)&r;
3162 3164 remote->sin_family = AF_INET;
3163 3165 remote->sin_addr.s_addr = INADDR_ANY;
3164 3166 remote->sin_port = 0;
3165 3167
3166 3168 if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
3167 3169 return (error);
3168 3170
3169 3171 /*
3170 3172 * Abort IPv6 connections.
3171 3173 */
3172 3174 bzero(&l, sizeof (*local6));
3173 3175 local6 = (struct sockaddr_in6 *)&l;
3174 3176 local6->sin6_family = AF_INET6;
3175 3177 local6->sin6_port = 0;
3176 3178 local6->sin6_addr = in6addr_any;
3177 3179
3178 3180 bzero(&r, sizeof (*remote6));
3179 3181 remote6 = (struct sockaddr_in6 *)&r;
3180 3182 remote6->sin6_family = AF_INET6;
3181 3183 remote6->sin6_port = 0;
3182 3184 remote6->sin6_addr = in6addr_any;
3183 3185
3184 3186 if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
3185 3187 return (error);
3186 3188 return (0);
↓ open down ↓ |
2018 lines elided |
↑ open up ↑ |
3187 3189 }
3188 3190
3189 3191 static int
3190 3192 get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
3191 3193 {
3192 3194 int error = -1;
3193 3195 char *privname = NULL;
3194 3196
3195 3197 if (ALT_MOUNT(mount_cmd)) {
3196 3198 zone_iptype_t iptype;
3197 - const char *curr_iptype;
3199 + const char *curr_iptype = NULL;
3198 3200
3199 3201 if (zonecfg_get_iptype(snap_hndl, &iptype) != Z_OK) {
3200 3202 zerror(zlogp, B_TRUE, "unable to determine ip-type");
3201 3203 return (-1);
3202 3204 }
3203 3205
3204 3206 switch (iptype) {
3205 3207 case ZS_SHARED:
3206 3208 curr_iptype = "shared";
3207 3209 break;
3208 3210 case ZS_EXCLUSIVE:
3209 3211 curr_iptype = "exclusive";
3210 3212 break;
3211 3213 }
3212 3214
3213 3215 if (zonecfg_default_privset(privs, curr_iptype) == Z_OK)
3214 3216 return (0);
3215 3217
3216 3218 zerror(zlogp, B_FALSE,
3217 3219 "failed to determine the zone's default privilege set");
3218 3220 return (-1);
3219 3221 }
3220 3222
3221 3223 switch (zonecfg_get_privset(snap_hndl, privs, &privname)) {
3222 3224 case Z_OK:
3223 3225 error = 0;
3224 3226 break;
3225 3227 case Z_PRIV_PROHIBITED:
3226 3228 zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
3227 3229 "within the zone's privilege set", privname);
3228 3230 break;
3229 3231 case Z_PRIV_REQUIRED:
3230 3232 zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
3231 3233 "from the zone's privilege set", privname);
3232 3234 break;
3233 3235 case Z_PRIV_UNKNOWN:
3234 3236 zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
3235 3237 "in the zone's privilege set", privname);
3236 3238 break;
3237 3239 default:
3238 3240 zerror(zlogp, B_FALSE, "failed to determine the zone's "
3239 3241 "privilege set");
3240 3242 break;
3241 3243 }
3242 3244
3243 3245 free(privname);
3244 3246 return (error);
3245 3247 }
3246 3248
3247 3249 static int
3248 3250 get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3249 3251 {
3250 3252 nvlist_t *nvl = NULL;
3251 3253 char *nvl_packed = NULL;
3252 3254 size_t nvl_size = 0;
3253 3255 nvlist_t **nvlv = NULL;
3254 3256 int rctlcount = 0;
3255 3257 int error = -1;
3256 3258 struct zone_rctltab rctltab;
3257 3259 rctlblk_t *rctlblk = NULL;
3258 3260 uint64_t maxlwps;
3259 3261 uint64_t maxprocs;
3260 3262
3261 3263 *bufp = NULL;
3262 3264 *bufsizep = 0;
3263 3265
3264 3266 rctltab.zone_rctl_valptr = NULL;
3265 3267 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
3266 3268 zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
3267 3269 goto out;
3268 3270 }
3269 3271
3270 3272 /*
3271 3273 * Allow the administrator to control both the maximum number of
3272 3274 * process table slots and the maximum number of lwps with just the
3273 3275 * max-processes property. If only the max-processes property is set,
3274 3276 * we add a max-lwps property with a limit derived from max-processes.
3275 3277 */
3276 3278 if (zonecfg_get_aliased_rctl(snap_hndl, ALIAS_MAXPROCS, &maxprocs)
3277 3279 == Z_OK &&
3278 3280 zonecfg_get_aliased_rctl(snap_hndl, ALIAS_MAXLWPS, &maxlwps)
3279 3281 == Z_NO_ENTRY) {
3280 3282 if (zonecfg_set_aliased_rctl(snap_hndl, ALIAS_MAXLWPS,
3281 3283 maxprocs * LWPS_PER_PROCESS) != Z_OK) {
3282 3284 zerror(zlogp, B_FALSE, "unable to set max-lwps alias");
3283 3285 goto out;
3284 3286 }
3285 3287 }
3286 3288
3287 3289 if (zonecfg_setrctlent(snap_hndl) != Z_OK) {
3288 3290 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
3289 3291 goto out;
3290 3292 }
3291 3293
3292 3294 if ((rctlblk = malloc(rctlblk_size())) == NULL) {
3293 3295 zerror(zlogp, B_TRUE, "memory allocation failed");
3294 3296 goto out;
3295 3297 }
3296 3298 while (zonecfg_getrctlent(snap_hndl, &rctltab) == Z_OK) {
3297 3299 struct zone_rctlvaltab *rctlval;
3298 3300 uint_t i, count;
3299 3301 const char *name = rctltab.zone_rctl_name;
3300 3302
3301 3303 /* zoneadm should have already warned about unknown rctls. */
3302 3304 if (!zonecfg_is_rctl(name)) {
3303 3305 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
3304 3306 rctltab.zone_rctl_valptr = NULL;
3305 3307 continue;
3306 3308 }
3307 3309 count = 0;
3308 3310 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
3309 3311 rctlval = rctlval->zone_rctlval_next) {
3310 3312 count++;
3311 3313 }
3312 3314 if (count == 0) { /* ignore */
3313 3315 continue; /* Nothing to free */
3314 3316 }
3315 3317 if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
3316 3318 goto out;
3317 3319 i = 0;
3318 3320 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
3319 3321 rctlval = rctlval->zone_rctlval_next, i++) {
3320 3322 if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
3321 3323 zerror(zlogp, B_TRUE, "%s failed",
3322 3324 "nvlist_alloc");
3323 3325 goto out;
3324 3326 }
3325 3327 if (zonecfg_construct_rctlblk(rctlval, rctlblk)
3326 3328 != Z_OK) {
3327 3329 zerror(zlogp, B_FALSE, "invalid rctl value: "
3328 3330 "(priv=%s,limit=%s,action=%s)",
3329 3331 rctlval->zone_rctlval_priv,
3330 3332 rctlval->zone_rctlval_limit,
3331 3333 rctlval->zone_rctlval_action);
3332 3334 goto out;
3333 3335 }
3334 3336 if (!zonecfg_valid_rctl(name, rctlblk)) {
3335 3337 zerror(zlogp, B_FALSE,
3336 3338 "(priv=%s,limit=%s,action=%s) is not a "
3337 3339 "valid value for rctl '%s'",
3338 3340 rctlval->zone_rctlval_priv,
3339 3341 rctlval->zone_rctlval_limit,
3340 3342 rctlval->zone_rctlval_action,
3341 3343 name);
3342 3344 goto out;
3343 3345 }
3344 3346 if (nvlist_add_uint64(nvlv[i], "privilege",
3345 3347 rctlblk_get_privilege(rctlblk)) != 0) {
3346 3348 zerror(zlogp, B_FALSE, "%s failed",
3347 3349 "nvlist_add_uint64");
3348 3350 goto out;
3349 3351 }
3350 3352 if (nvlist_add_uint64(nvlv[i], "limit",
3351 3353 rctlblk_get_value(rctlblk)) != 0) {
3352 3354 zerror(zlogp, B_FALSE, "%s failed",
3353 3355 "nvlist_add_uint64");
3354 3356 goto out;
3355 3357 }
3356 3358 if (nvlist_add_uint64(nvlv[i], "action",
3357 3359 (uint_t)rctlblk_get_local_action(rctlblk, NULL))
3358 3360 != 0) {
3359 3361 zerror(zlogp, B_FALSE, "%s failed",
3360 3362 "nvlist_add_uint64");
3361 3363 goto out;
3362 3364 }
3363 3365 }
3364 3366 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
3365 3367 rctltab.zone_rctl_valptr = NULL;
3366 3368 if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
3367 3369 != 0) {
3368 3370 zerror(zlogp, B_FALSE, "%s failed",
3369 3371 "nvlist_add_nvlist_array");
3370 3372 goto out;
3371 3373 }
3372 3374 for (i = 0; i < count; i++)
3373 3375 nvlist_free(nvlv[i]);
3374 3376 free(nvlv);
3375 3377 nvlv = NULL;
3376 3378 rctlcount++;
3377 3379 }
3378 3380 (void) zonecfg_endrctlent(snap_hndl);
3379 3381
3380 3382 if (rctlcount == 0) {
3381 3383 error = 0;
3382 3384 goto out;
3383 3385 }
3384 3386 if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
3385 3387 != 0) {
3386 3388 zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
3387 3389 goto out;
3388 3390 }
3389 3391
3390 3392 error = 0;
3391 3393 *bufp = nvl_packed;
3392 3394 *bufsizep = nvl_size;
3393 3395
3394 3396 out:
3395 3397 free(rctlblk);
3396 3398 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
3397 3399 if (error && nvl_packed != NULL)
3398 3400 free(nvl_packed);
3399 3401 if (nvl != NULL)
3400 3402 nvlist_free(nvl);
3401 3403 if (nvlv != NULL)
3402 3404 free(nvlv);
3403 3405 return (error);
3404 3406 }
3405 3407
3406 3408 static int
3407 3409 get_implicit_datasets(zlog_t *zlogp, char **retstr)
3408 3410 {
3409 3411 char cmdbuf[2 * MAXPATHLEN];
3410 3412
3411 3413 if (query_hook[0] == '\0')
3412 3414 return (0);
3413 3415
3414 3416 if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook)
3415 3417 > sizeof (cmdbuf))
3416 3418 return (-1);
3417 3419
3418 3420 if (do_subproc(zlogp, cmdbuf, retstr, B_FALSE) != 0)
3419 3421 return (-1);
3420 3422
3421 3423 return (0);
3422 3424 }
3423 3425
3424 3426 static int
3425 3427 get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3426 3428 {
3427 3429 struct zone_dstab dstab;
3428 3430 size_t total, offset, len;
3429 3431 int error = -1;
3430 3432 char *str = NULL;
3431 3433 char *implicit_datasets = NULL;
3432 3434 int implicit_len = 0;
3433 3435
3434 3436 *bufp = NULL;
3435 3437 *bufsizep = 0;
3436 3438
3437 3439 if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) {
3438 3440 zerror(zlogp, B_FALSE, "getting implicit datasets failed");
3439 3441 goto out;
3440 3442 }
3441 3443
3442 3444 if (zonecfg_setdsent(snap_hndl) != Z_OK) {
3443 3445 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3444 3446 goto out;
3445 3447 }
3446 3448
3447 3449 total = 0;
3448 3450 while (zonecfg_getdsent(snap_hndl, &dstab) == Z_OK)
3449 3451 total += strlen(dstab.zone_dataset_name) + 1;
3450 3452 (void) zonecfg_enddsent(snap_hndl);
3451 3453
3452 3454 if (implicit_datasets != NULL)
3453 3455 implicit_len = strlen(implicit_datasets);
3454 3456 if (implicit_len > 0)
3455 3457 total += implicit_len + 1;
3456 3458
3457 3459 if (total == 0) {
3458 3460 error = 0;
3459 3461 goto out;
3460 3462 }
3461 3463
3462 3464 if ((str = malloc(total)) == NULL) {
3463 3465 zerror(zlogp, B_TRUE, "memory allocation failed");
3464 3466 goto out;
3465 3467 }
3466 3468
3467 3469 if (zonecfg_setdsent(snap_hndl) != Z_OK) {
3468 3470 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3469 3471 goto out;
3470 3472 }
3471 3473 offset = 0;
3472 3474 while (zonecfg_getdsent(snap_hndl, &dstab) == Z_OK) {
3473 3475 len = strlen(dstab.zone_dataset_name);
3474 3476 (void) strlcpy(str + offset, dstab.zone_dataset_name,
3475 3477 total - offset);
3476 3478 offset += len;
3477 3479 if (offset < total - 1)
3478 3480 str[offset++] = ',';
3479 3481 }
3480 3482 (void) zonecfg_enddsent(snap_hndl);
3481 3483
3482 3484 if (implicit_len > 0)
3483 3485 (void) strlcpy(str + offset, implicit_datasets, total - offset);
3484 3486
3485 3487 error = 0;
3486 3488 *bufp = str;
3487 3489 *bufsizep = total;
3488 3490
3489 3491 out:
3490 3492 if (error != 0 && str != NULL)
3491 3493 free(str);
3492 3494 if (implicit_datasets != NULL)
3493 3495 free(implicit_datasets);
3494 3496
3495 3497 return (error);
3496 3498 }
3497 3499
3498 3500 static int
3499 3501 validate_datasets(zlog_t *zlogp)
3500 3502 {
3501 3503 struct zone_dstab dstab;
3502 3504 zfs_handle_t *zhp;
3503 3505 libzfs_handle_t *hdl;
3504 3506
3505 3507 if (zonecfg_setdsent(snap_hndl) != Z_OK) {
3506 3508 zerror(zlogp, B_FALSE, "invalid configuration");
3507 3509 return (-1);
3508 3510 }
3509 3511
3510 3512 if ((hdl = libzfs_init()) == NULL) {
3511 3513 zerror(zlogp, B_FALSE, "opening ZFS library");
3512 3514 return (-1);
3513 3515 }
3514 3516
3515 3517 while (zonecfg_getdsent(snap_hndl, &dstab) == Z_OK) {
3516 3518
3517 3519 if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3518 3520 ZFS_TYPE_FILESYSTEM)) == NULL) {
3519 3521 zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3520 3522 dstab.zone_dataset_name);
3521 3523 libzfs_fini(hdl);
3522 3524 return (-1);
3523 3525 }
3524 3526
3525 3527 /*
3526 3528 * Automatically set the 'zoned' property. We check the value
3527 3529 * first because we'll get EPERM if it is already set.
3528 3530 */
3529 3531 if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
3530 3532 zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
3531 3533 "on") != 0) {
3532 3534 zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3533 3535 "property for ZFS dataset '%s'\n",
3534 3536 dstab.zone_dataset_name);
3535 3537 zfs_close(zhp);
3536 3538 libzfs_fini(hdl);
3537 3539 return (-1);
3538 3540 }
3539 3541
3540 3542 zfs_close(zhp);
3541 3543 }
3542 3544 (void) zonecfg_enddsent(snap_hndl);
3543 3545
3544 3546 libzfs_fini(hdl);
3545 3547
3546 3548 return (0);
3547 3549 }
3548 3550
3549 3551 /*
3550 3552 * Return true if the path is its own zfs file system. We determine this
3551 3553 * by stat-ing the path to see if it is zfs and stat-ing the parent to see
3552 3554 * if it is a different fs.
3553 3555 */
3554 3556 boolean_t
3555 3557 is_zonepath_zfs(char *zonepath)
3556 3558 {
3557 3559 int res;
3558 3560 char *path;
3559 3561 char *parent;
3560 3562 struct statvfs64 buf1, buf2;
3561 3563
3562 3564 if (statvfs64(zonepath, &buf1) != 0)
3563 3565 return (B_FALSE);
3564 3566
3565 3567 if (strcmp(buf1.f_basetype, "zfs") != 0)
3566 3568 return (B_FALSE);
3567 3569
3568 3570 if ((path = strdup(zonepath)) == NULL)
3569 3571 return (B_FALSE);
3570 3572
3571 3573 parent = dirname(path);
3572 3574 res = statvfs64(parent, &buf2);
3573 3575 free(path);
3574 3576
3575 3577 if (res != 0)
3576 3578 return (B_FALSE);
3577 3579
3578 3580 if (buf1.f_fsid == buf2.f_fsid)
3579 3581 return (B_FALSE);
3580 3582
3581 3583 return (B_TRUE);
3582 3584 }
3583 3585
3584 3586 /*
3585 3587 * Verify the MAC label in the root dataset for the zone.
3586 3588 * If the label exists, it must match the label configured for the zone.
3587 3589 * Otherwise if there's no label on the dataset, create one here.
3588 3590 */
3589 3591
3590 3592 static int
3591 3593 validate_rootds_label(zlog_t *zlogp, char *rootpath, m_label_t *zone_sl)
3592 3594 {
3593 3595 int error = -1;
3594 3596 zfs_handle_t *zhp;
3595 3597 libzfs_handle_t *hdl;
3596 3598 m_label_t ds_sl;
3597 3599 char zonepath[MAXPATHLEN];
3598 3600 char ds_hexsl[MAXNAMELEN];
3599 3601
3600 3602 if (!is_system_labeled())
3601 3603 return (0);
3602 3604
3603 3605 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
3604 3606 zerror(zlogp, B_TRUE, "unable to determine zone path");
3605 3607 return (-1);
3606 3608 }
3607 3609
3608 3610 if (!is_zonepath_zfs(zonepath))
3609 3611 return (0);
3610 3612
3611 3613 if ((hdl = libzfs_init()) == NULL) {
3612 3614 zerror(zlogp, B_FALSE, "opening ZFS library");
3613 3615 return (-1);
3614 3616 }
3615 3617
3616 3618 if ((zhp = zfs_path_to_zhandle(hdl, rootpath,
3617 3619 ZFS_TYPE_FILESYSTEM)) == NULL) {
3618 3620 zerror(zlogp, B_FALSE, "cannot open ZFS dataset for path '%s'",
3619 3621 rootpath);
3620 3622 libzfs_fini(hdl);
3621 3623 return (-1);
3622 3624 }
3623 3625
3624 3626 /* Get the mlslabel property if it exists. */
3625 3627 if ((zfs_prop_get(zhp, ZFS_PROP_MLSLABEL, ds_hexsl, MAXNAMELEN,
3626 3628 NULL, NULL, 0, B_TRUE) != 0) ||
3627 3629 (strcmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)) {
3628 3630 char *str2 = NULL;
3629 3631
3630 3632 /*
3631 3633 * No label on the dataset (or default only); create one.
3632 3634 * (Only do this automatic labeling for the labeled brand.)
3633 3635 */
3634 3636 if (strcmp(brand_name, LABELED_BRAND_NAME) != 0) {
3635 3637 error = 0;
3636 3638 goto out;
3637 3639 }
3638 3640
3639 3641 error = l_to_str_internal(zone_sl, &str2);
3640 3642 if (error)
3641 3643 goto out;
3642 3644 if (str2 == NULL) {
3643 3645 error = -1;
3644 3646 goto out;
3645 3647 }
3646 3648 if ((error = zfs_prop_set(zhp,
3647 3649 zfs_prop_to_name(ZFS_PROP_MLSLABEL), str2)) != 0) {
3648 3650 zerror(zlogp, B_FALSE, "cannot set 'mlslabel' "
3649 3651 "property for root dataset at '%s'\n", rootpath);
3650 3652 }
3651 3653 free(str2);
3652 3654 goto out;
3653 3655 }
3654 3656
3655 3657 /* Convert the retrieved dataset label to binary form. */
3656 3658 error = hexstr_to_label(ds_hexsl, &ds_sl);
3657 3659 if (error) {
3658 3660 zerror(zlogp, B_FALSE, "invalid 'mlslabel' "
3659 3661 "property on root dataset at '%s'\n", rootpath);
3660 3662 goto out; /* exit with error */
3661 3663 }
3662 3664
3663 3665 /*
3664 3666 * Perform a MAC check by comparing the zone label with the
3665 3667 * dataset label.
3666 3668 */
3667 3669 error = (!blequal(zone_sl, &ds_sl));
3668 3670 if (error)
3669 3671 zerror(zlogp, B_FALSE, "Rootpath dataset has mismatched label");
3670 3672 out:
3671 3673 zfs_close(zhp);
3672 3674 libzfs_fini(hdl);
3673 3675
3674 3676 return (error);
3675 3677 }
3676 3678
3677 3679 /*
3678 3680 * Mount lower level home directories into/from current zone
3679 3681 * Share exported directories specified in dfstab for zone
3680 3682 */
3681 3683 static int
3682 3684 tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
3683 3685 {
3684 3686 zoneid_t *zids = NULL;
3685 3687 priv_set_t *zid_privs;
3686 3688 const priv_impl_info_t *ip = NULL;
3687 3689 uint_t nzents_saved;
3688 3690 uint_t nzents;
3689 3691 int i;
3690 3692 char readonly[] = "ro";
3691 3693 struct zone_fstab lower_fstab;
3692 3694 char *argv[4];
3693 3695
3694 3696 if (!is_system_labeled())
3695 3697 return (0);
3696 3698
3697 3699 if (zid_label == NULL) {
3698 3700 zid_label = m_label_alloc(MAC_LABEL);
3699 3701 if (zid_label == NULL)
3700 3702 return (-1);
3701 3703 }
3702 3704
3703 3705 /* Make sure our zone has an /export/home dir */
3704 3706 (void) make_one_dir(zlogp, rootpath, "/export/home",
3705 3707 DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
3706 3708
3707 3709 lower_fstab.zone_fs_raw[0] = '\0';
3708 3710 (void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
3709 3711 sizeof (lower_fstab.zone_fs_type));
3710 3712 lower_fstab.zone_fs_options = NULL;
3711 3713 (void) zonecfg_add_fs_option(&lower_fstab, readonly);
3712 3714
3713 3715 /*
3714 3716 * Get the list of zones from the kernel
3715 3717 */
3716 3718 if (zone_list(NULL, &nzents) != 0) {
3717 3719 zerror(zlogp, B_TRUE, "unable to list zones");
3718 3720 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3719 3721 return (-1);
3720 3722 }
3721 3723 again:
3722 3724 if (nzents == 0) {
3723 3725 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3724 3726 return (-1);
3725 3727 }
3726 3728
3727 3729 zids = malloc(nzents * sizeof (zoneid_t));
3728 3730 if (zids == NULL) {
3729 3731 zerror(zlogp, B_TRUE, "memory allocation failed");
3730 3732 return (-1);
3731 3733 }
3732 3734 nzents_saved = nzents;
3733 3735
3734 3736 if (zone_list(zids, &nzents) != 0) {
3735 3737 zerror(zlogp, B_TRUE, "unable to list zones");
3736 3738 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3737 3739 free(zids);
3738 3740 return (-1);
3739 3741 }
3740 3742 if (nzents != nzents_saved) {
3741 3743 /* list changed, try again */
3742 3744 free(zids);
3743 3745 goto again;
3744 3746 }
3745 3747
3746 3748 ip = getprivimplinfo();
3747 3749 if ((zid_privs = priv_allocset()) == NULL) {
3748 3750 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
3749 3751 zonecfg_free_fs_option_list(
3750 3752 lower_fstab.zone_fs_options);
3751 3753 free(zids);
3752 3754 return (-1);
3753 3755 }
3754 3756
3755 3757 for (i = 0; i < nzents; i++) {
3756 3758 char zid_name[ZONENAME_MAX];
3757 3759 zone_state_t zid_state;
3758 3760 char zid_rpath[MAXPATHLEN];
3759 3761 struct stat stat_buf;
3760 3762
3761 3763 if (zids[i] == GLOBAL_ZONEID)
3762 3764 continue;
3763 3765
3764 3766 if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
3765 3767 continue;
3766 3768
3767 3769 /*
3768 3770 * Do special setup for the zone we are booting
3769 3771 */
3770 3772 if (strcmp(zid_name, zone_name) == 0) {
3771 3773 struct zone_fstab autofs_fstab;
3772 3774 char map_path[MAXPATHLEN];
3773 3775 int fd;
3774 3776
3775 3777 /*
3776 3778 * Create auto_home_<zone> map for this zone
3777 3779 * in the global zone. The non-global zone entry
3778 3780 * will be created by automount when the zone
3779 3781 * is booted.
3780 3782 */
3781 3783
3782 3784 (void) snprintf(autofs_fstab.zone_fs_special,
3783 3785 MAXPATHLEN, "auto_home_%s", zid_name);
3784 3786
3785 3787 (void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
3786 3788 "/zone/%s/home", zid_name);
3787 3789
3788 3790 (void) snprintf(map_path, sizeof (map_path),
3789 3791 "/etc/%s", autofs_fstab.zone_fs_special);
3790 3792 /*
3791 3793 * If the map file doesn't exist create a template
3792 3794 */
3793 3795 if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
3794 3796 S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
3795 3797 int len;
3796 3798 char map_rec[MAXPATHLEN];
3797 3799
3798 3800 len = snprintf(map_rec, sizeof (map_rec),
3799 3801 "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
3800 3802 autofs_fstab.zone_fs_special, rootpath);
3801 3803 (void) write(fd, map_rec, len);
3802 3804 (void) close(fd);
3803 3805 }
3804 3806
3805 3807 /*
3806 3808 * Mount auto_home_<zone> in the global zone if absent.
3807 3809 * If it's already of type autofs, then
3808 3810 * don't mount it again.
3809 3811 */
3810 3812 if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
3811 3813 strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
3812 3814 char optstr[] = "indirect,ignore,nobrowse";
3813 3815
3814 3816 (void) make_one_dir(zlogp, "",
3815 3817 autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
3816 3818 DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
3817 3819
3818 3820 /*
3819 3821 * Mount will fail if automounter has already
3820 3822 * processed the auto_home_<zonename> map
3821 3823 */
3822 3824 (void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
3823 3825 autofs_fstab.zone_fs_special,
3824 3826 autofs_fstab.zone_fs_dir);
3825 3827 }
3826 3828 continue;
3827 3829 }
3828 3830
3829 3831
3830 3832 if (zone_get_state(zid_name, &zid_state) != Z_OK ||
3831 3833 (zid_state != ZONE_STATE_READY &&
3832 3834 zid_state != ZONE_STATE_RUNNING))
3833 3835 /* Skip over zones without mounted filesystems */
3834 3836 continue;
3835 3837
3836 3838 if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
3837 3839 sizeof (m_label_t)) < 0)
3838 3840 /* Skip over zones with unspecified label */
3839 3841 continue;
3840 3842
3841 3843 if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
3842 3844 sizeof (zid_rpath)) == -1)
3843 3845 /* Skip over zones with bad path */
3844 3846 continue;
3845 3847
3846 3848 if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
3847 3849 sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
3848 3850 /* Skip over zones with bad privs */
3849 3851 continue;
3850 3852
3851 3853 /*
3852 3854 * Reading down is valid according to our label model
3853 3855 * but some customers want to disable it because it
3854 3856 * allows execute down and other possible attacks.
3855 3857 * Therefore, we restrict this feature to zones that
3856 3858 * have the NET_MAC_AWARE privilege which is required
3857 3859 * for NFS read-down semantics.
3858 3860 */
3859 3861 if ((bldominates(zlabel, zid_label)) &&
3860 3862 (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
3861 3863 /*
3862 3864 * Our zone dominates this one.
3863 3865 * Create a lofs mount from lower zone's /export/home
3864 3866 */
3865 3867 (void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
3866 3868 "%s/zone/%s/export/home", rootpath, zid_name);
3867 3869
3868 3870 /*
3869 3871 * If the target is already an LOFS mount
3870 3872 * then don't do it again.
3871 3873 */
3872 3874 if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
3873 3875 strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
3874 3876
3875 3877 if (snprintf(lower_fstab.zone_fs_special,
3876 3878 MAXPATHLEN, "%s/export",
3877 3879 zid_rpath) > MAXPATHLEN)
3878 3880 continue;
3879 3881
3880 3882 /*
3881 3883 * Make sure the lower-level home exists
3882 3884 */
3883 3885 if (make_one_dir(zlogp,
3884 3886 lower_fstab.zone_fs_special, "/home",
3885 3887 DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
3886 3888 DEFAULT_DIR_GROUP) != 0)
3887 3889 continue;
3888 3890
3889 3891 (void) strlcat(lower_fstab.zone_fs_special,
3890 3892 "/home", MAXPATHLEN);
3891 3893
3892 3894 /*
3893 3895 * Mount can fail because the lower-level
3894 3896 * zone may have already done a mount up.
3895 3897 */
3896 3898 (void) mount_one(zlogp, &lower_fstab, "",
3897 3899 Z_MNT_BOOT);
3898 3900 }
3899 3901 } else if ((bldominates(zid_label, zlabel)) &&
3900 3902 (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
3901 3903 /*
3902 3904 * This zone dominates our zone.
3903 3905 * Create a lofs mount from our zone's /export/home
3904 3906 */
3905 3907 if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
3906 3908 "%s/zone/%s/export/home", zid_rpath,
3907 3909 zone_name) > MAXPATHLEN)
3908 3910 continue;
3909 3911
3910 3912 /*
3911 3913 * If the target is already an LOFS mount
3912 3914 * then don't do it again.
3913 3915 */
3914 3916 if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
3915 3917 strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
3916 3918
3917 3919 (void) snprintf(lower_fstab.zone_fs_special,
3918 3920 MAXPATHLEN, "%s/export/home", rootpath);
3919 3921
3920 3922 /*
3921 3923 * Mount can fail because the higher-level
3922 3924 * zone may have already done a mount down.
3923 3925 */
3924 3926 (void) mount_one(zlogp, &lower_fstab, "",
3925 3927 Z_MNT_BOOT);
3926 3928 }
3927 3929 }
3928 3930 }
3929 3931 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3930 3932 priv_freeset(zid_privs);
3931 3933 free(zids);
3932 3934
3933 3935 /*
3934 3936 * Now share any exported directories from this zone.
3935 3937 * Each zone can have its own dfstab.
3936 3938 */
3937 3939
3938 3940 argv[0] = "zoneshare";
3939 3941 argv[1] = "-z";
3940 3942 argv[2] = zone_name;
3941 3943 argv[3] = NULL;
3942 3944
3943 3945 (void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
3944 3946 /* Don't check for errors since they don't affect the zone */
3945 3947
3946 3948 return (0);
3947 3949 }
3948 3950
3949 3951 /*
3950 3952 * Unmount lofs mounts from higher level zones
3951 3953 * Unshare nfs exported directories
3952 3954 */
3953 3955 static void
3954 3956 tsol_unmounts(zlog_t *zlogp, char *zone_name)
3955 3957 {
3956 3958 zoneid_t *zids = NULL;
3957 3959 uint_t nzents_saved;
3958 3960 uint_t nzents;
3959 3961 int i;
3960 3962 char *argv[4];
3961 3963 char path[MAXPATHLEN];
3962 3964
3963 3965 if (!is_system_labeled())
3964 3966 return;
3965 3967
3966 3968 /*
3967 3969 * Get the list of zones from the kernel
3968 3970 */
3969 3971 if (zone_list(NULL, &nzents) != 0) {
3970 3972 return;
3971 3973 }
3972 3974
3973 3975 if (zid_label == NULL) {
3974 3976 zid_label = m_label_alloc(MAC_LABEL);
3975 3977 if (zid_label == NULL)
3976 3978 return;
3977 3979 }
3978 3980
3979 3981 again:
3980 3982 if (nzents == 0)
3981 3983 return;
3982 3984
3983 3985 zids = malloc(nzents * sizeof (zoneid_t));
3984 3986 if (zids == NULL) {
3985 3987 zerror(zlogp, B_TRUE, "memory allocation failed");
3986 3988 return;
3987 3989 }
3988 3990 nzents_saved = nzents;
3989 3991
3990 3992 if (zone_list(zids, &nzents) != 0) {
3991 3993 free(zids);
3992 3994 return;
3993 3995 }
3994 3996 if (nzents != nzents_saved) {
3995 3997 /* list changed, try again */
3996 3998 free(zids);
3997 3999 goto again;
3998 4000 }
3999 4001
4000 4002 for (i = 0; i < nzents; i++) {
4001 4003 char zid_name[ZONENAME_MAX];
4002 4004 zone_state_t zid_state;
4003 4005 char zid_rpath[MAXPATHLEN];
4004 4006
4005 4007 if (zids[i] == GLOBAL_ZONEID)
4006 4008 continue;
4007 4009
4008 4010 if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
4009 4011 continue;
4010 4012
4011 4013 /*
4012 4014 * Skip the zone we are halting
4013 4015 */
4014 4016 if (strcmp(zid_name, zone_name) == 0)
4015 4017 continue;
4016 4018
4017 4019 if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
4018 4020 sizeof (zid_state)) < 0) ||
4019 4021 (zid_state < ZONE_IS_READY))
4020 4022 /* Skip over zones without mounted filesystems */
4021 4023 continue;
4022 4024
4023 4025 if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
4024 4026 sizeof (m_label_t)) < 0)
4025 4027 /* Skip over zones with unspecified label */
4026 4028 continue;
4027 4029
4028 4030 if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
4029 4031 sizeof (zid_rpath)) == -1)
4030 4032 /* Skip over zones with bad path */
4031 4033 continue;
4032 4034
4033 4035 if (zlabel != NULL && bldominates(zid_label, zlabel)) {
4034 4036 /*
4035 4037 * This zone dominates our zone.
4036 4038 * Unmount the lofs mount of our zone's /export/home
4037 4039 */
4038 4040
4039 4041 if (snprintf(path, MAXPATHLEN,
4040 4042 "%s/zone/%s/export/home", zid_rpath,
4041 4043 zone_name) > MAXPATHLEN)
4042 4044 continue;
4043 4045
4044 4046 /* Skip over mount failures */
4045 4047 (void) umount(path);
4046 4048 }
4047 4049 }
4048 4050 free(zids);
4049 4051
4050 4052 /*
4051 4053 * Unmount global zone autofs trigger for this zone
4052 4054 */
4053 4055 (void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
4054 4056 /* Skip over mount failures */
4055 4057 (void) umount(path);
4056 4058
4057 4059 /*
4058 4060 * Next unshare any exported directories from this zone.
4059 4061 */
4060 4062
4061 4063 argv[0] = "zoneunshare";
4062 4064 argv[1] = "-z";
4063 4065 argv[2] = zone_name;
4064 4066 argv[3] = NULL;
4065 4067
4066 4068 (void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
4067 4069 /* Don't check for errors since they don't affect the zone */
4068 4070
4069 4071 /*
4070 4072 * Finally, deallocate any devices in the zone.
4071 4073 */
4072 4074
4073 4075 argv[0] = "deallocate";
4074 4076 argv[1] = "-Isz";
4075 4077 argv[2] = zone_name;
4076 4078 argv[3] = NULL;
4077 4079
4078 4080 (void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
4079 4081 /* Don't check for errors since they don't affect the zone */
4080 4082 }
4081 4083
4082 4084 /*
4083 4085 * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
4084 4086 * this zone.
4085 4087 */
4086 4088 static tsol_zcent_t *
4087 4089 get_zone_label(zlog_t *zlogp, priv_set_t *privs)
4088 4090 {
4089 4091 FILE *fp;
4090 4092 tsol_zcent_t *zcent = NULL;
4091 4093 char line[MAXTNZLEN];
4092 4094
4093 4095 if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
4094 4096 zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
4095 4097 return (NULL);
4096 4098 }
4097 4099
4098 4100 while (fgets(line, sizeof (line), fp) != NULL) {
4099 4101 /*
4100 4102 * Check for malformed database
4101 4103 */
4102 4104 if (strlen(line) == MAXTNZLEN - 1)
4103 4105 break;
4104 4106 if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
4105 4107 continue;
4106 4108 if (strcmp(zcent->zc_name, zone_name) == 0)
4107 4109 break;
4108 4110 tsol_freezcent(zcent);
4109 4111 zcent = NULL;
4110 4112 }
4111 4113 (void) fclose(fp);
4112 4114
4113 4115 if (zcent == NULL) {
4114 4116 zerror(zlogp, B_FALSE, "zone requires a label assignment. "
4115 4117 "See tnzonecfg(4)");
4116 4118 } else {
4117 4119 if (zlabel == NULL)
4118 4120 zlabel = m_label_alloc(MAC_LABEL);
4119 4121 /*
4120 4122 * Save this zone's privileges for later read-down processing
4121 4123 */
4122 4124 if ((zprivs = priv_allocset()) == NULL) {
4123 4125 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
4124 4126 return (NULL);
4125 4127 } else {
4126 4128 priv_copyset(privs, zprivs);
4127 4129 }
4128 4130 }
4129 4131 return (zcent);
4130 4132 }
4131 4133
4132 4134 /*
4133 4135 * Add the Trusted Extensions multi-level ports for this zone.
4134 4136 */
4135 4137 static void
4136 4138 set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
4137 4139 {
4138 4140 tsol_mlp_t *mlp;
4139 4141 tsol_mlpent_t tsme;
4140 4142
4141 4143 if (!is_system_labeled())
4142 4144 return;
4143 4145
4144 4146 tsme.tsme_zoneid = zoneid;
4145 4147 tsme.tsme_flags = 0;
4146 4148 for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
4147 4149 tsme.tsme_mlp = *mlp;
4148 4150 if (tnmlp(TNDB_LOAD, &tsme) != 0) {
4149 4151 zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
4150 4152 "on %d-%d/%d", mlp->mlp_port,
4151 4153 mlp->mlp_port_upper, mlp->mlp_ipp);
4152 4154 }
4153 4155 }
4154 4156
4155 4157 tsme.tsme_flags = TSOL_MEF_SHARED;
4156 4158 for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
4157 4159 tsme.tsme_mlp = *mlp;
4158 4160 if (tnmlp(TNDB_LOAD, &tsme) != 0) {
4159 4161 zerror(zlogp, B_TRUE, "cannot set shared MLP "
4160 4162 "on %d-%d/%d", mlp->mlp_port,
4161 4163 mlp->mlp_port_upper, mlp->mlp_ipp);
4162 4164 }
4163 4165 }
4164 4166 }
4165 4167
4166 4168 static void
4167 4169 remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
4168 4170 {
4169 4171 tsol_mlpent_t tsme;
4170 4172
4171 4173 if (!is_system_labeled())
4172 4174 return;
4173 4175
4174 4176 (void) memset(&tsme, 0, sizeof (tsme));
4175 4177 tsme.tsme_zoneid = zoneid;
4176 4178 if (tnmlp(TNDB_FLUSH, &tsme) != 0)
4177 4179 zerror(zlogp, B_TRUE, "cannot flush MLPs");
4178 4180 }
4179 4181
4180 4182 int
4181 4183 prtmount(const struct mnttab *fs, void *x) {
4182 4184 zerror((zlog_t *)x, B_FALSE, " %s", fs->mnt_mountp);
4183 4185 return (0);
4184 4186 }
4185 4187
4186 4188 /*
4187 4189 * Look for zones running on the main system that are using this root (or any
4188 4190 * subdirectory of it). Return B_TRUE and print an error if a conflicting zone
4189 4191 * is found or if we can't tell.
4190 4192 */
4191 4193 static boolean_t
4192 4194 duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
4193 4195 {
4194 4196 zoneid_t *zids = NULL;
4195 4197 uint_t nzids = 0;
4196 4198 boolean_t retv;
4197 4199 int rlen, zlen;
4198 4200 char zroot[MAXPATHLEN];
4199 4201 char zonename[ZONENAME_MAX];
4200 4202
4201 4203 for (;;) {
4202 4204 nzids += 10;
4203 4205 zids = malloc(nzids * sizeof (*zids));
4204 4206 if (zids == NULL) {
4205 4207 zerror(zlogp, B_TRUE, "memory allocation failed");
4206 4208 return (B_TRUE);
4207 4209 }
4208 4210 if (zone_list(zids, &nzids) == 0)
4209 4211 break;
4210 4212 free(zids);
4211 4213 }
4212 4214 retv = B_FALSE;
4213 4215 rlen = strlen(rootpath);
4214 4216 while (nzids > 0) {
4215 4217 /*
4216 4218 * Ignore errors; they just mean that the zone has disappeared
4217 4219 * while we were busy.
4218 4220 */
4219 4221 if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
4220 4222 sizeof (zroot)) == -1)
4221 4223 continue;
4222 4224 zlen = strlen(zroot);
4223 4225 if (zlen > rlen)
4224 4226 zlen = rlen;
4225 4227 if (strncmp(rootpath, zroot, zlen) == 0 &&
4226 4228 (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
4227 4229 (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
4228 4230 if (getzonenamebyid(zids[nzids], zonename,
4229 4231 sizeof (zonename)) == -1)
4230 4232 (void) snprintf(zonename, sizeof (zonename),
4231 4233 "id %d", (int)zids[nzids]);
4232 4234 zerror(zlogp, B_FALSE,
4233 4235 "zone root %s already in use by zone %s",
4234 4236 rootpath, zonename);
4235 4237 retv = B_TRUE;
4236 4238 break;
4237 4239 }
4238 4240 }
4239 4241 free(zids);
4240 4242 return (retv);
4241 4243 }
4242 4244
4243 4245 /*
4244 4246 * Search for loopback mounts that use this same source node (same device and
4245 4247 * inode). Return B_TRUE if there is one or if we can't tell.
4246 4248 */
4247 4249 static boolean_t
4248 4250 duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
4249 4251 {
4250 4252 struct stat64 rst, zst;
4251 4253 struct mnttab *mnp;
4252 4254
4253 4255 if (stat64(rootpath, &rst) == -1) {
4254 4256 zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
4255 4257 return (B_TRUE);
4256 4258 }
4257 4259 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
4258 4260 return (B_TRUE);
4259 4261 for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
4260 4262 if (mnp->mnt_fstype == NULL ||
4261 4263 strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
4262 4264 continue;
4263 4265 /* We're looking at a loopback mount. Stat it. */
4264 4266 if (mnp->mnt_special != NULL &&
4265 4267 stat64(mnp->mnt_special, &zst) != -1 &&
4266 4268 rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
4267 4269 zerror(zlogp, B_FALSE,
4268 4270 "zone root %s is reachable through %s",
4269 4271 rootpath, mnp->mnt_mountp);
4270 4272 return (B_TRUE);
4271 4273 }
4272 4274 }
4273 4275 return (B_FALSE);
4274 4276 }
4275 4277
4276 4278 /*
4277 4279 * Set pool info for the zone's resource management configuration.
4278 4280 */
4279 4281 static int
4280 4282 setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
4281 4283 {
4282 4284 int res;
4283 4285 uint64_t tmp;
4284 4286 char sched[MAXNAMELEN];
4285 4287 char pool_err[128];
4286 4288
4287 4289 /* Get the scheduling class set in the zone configuration. */
4288 4290 if (zonecfg_get_sched_class(snap_hndl, sched, sizeof (sched)) == Z_OK &&
4289 4291 strlen(sched) > 0) {
4290 4292 if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
4291 4293 strlen(sched)) == -1)
4292 4294 zerror(zlogp, B_TRUE, "WARNING: unable to set the "
4293 4295 "default scheduling class");
4294 4296
4295 4297 } else if (zonecfg_get_aliased_rctl(snap_hndl, ALIAS_SHARES, &tmp)
4296 4298 == Z_OK) {
4297 4299 /*
4298 4300 * If the zone has the zone.cpu-shares rctl set then we want to
4299 4301 * use the Fair Share Scheduler (FSS) for processes in the
4300 4302 * zone. Check what scheduling class the zone would be running
4301 4303 * in by default so we can print a warning and modify the class
4302 4304 * if we wouldn't be using FSS.
4303 4305 */
4304 4306 char class_name[PC_CLNMSZ];
4305 4307
4306 4308 if (zonecfg_get_dflt_sched_class(snap_hndl, class_name,
4307 4309 sizeof (class_name)) != Z_OK) {
4308 4310 zerror(zlogp, B_FALSE, "WARNING: unable to determine "
4309 4311 "the zone's scheduling class");
4310 4312
4311 4313 } else if (strcmp("FSS", class_name) != 0) {
4312 4314 zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
4313 4315 "rctl is set but\nFSS is not the default "
4314 4316 "scheduling class for\nthis zone. FSS will be "
4315 4317 "used for processes\nin the zone but to get the "
4316 4318 "full benefit of FSS,\nit should be the default "
4317 4319 "scheduling class.\nSee dispadmin(1M) for more "
4318 4320 "details.");
4319 4321
4320 4322 if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
4321 4323 strlen("FSS")) == -1)
4322 4324 zerror(zlogp, B_TRUE, "WARNING: unable to set "
4323 4325 "zone scheduling class to FSS");
4324 4326 }
4325 4327 }
4326 4328
4327 4329 /*
4328 4330 * The next few blocks of code attempt to set up temporary pools as
4329 4331 * well as persistent pools. In all cases we call the functions
4330 4332 * unconditionally. Within each funtion the code will check if the
4331 4333 * zone is actually configured for a temporary pool or persistent pool
4332 4334 * and just return if there is nothing to do.
4333 4335 *
4334 4336 * If we are rebooting we want to attempt to reuse any temporary pool
4335 4337 * that was previously set up. zonecfg_bind_tmp_pool() will do the
4336 4338 * right thing in all cases (reuse or create) based on the current
4337 4339 * zonecfg.
4338 4340 */
4339 4341 if ((res = zonecfg_bind_tmp_pool(snap_hndl, zoneid, pool_err,
4340 4342 sizeof (pool_err))) != Z_OK) {
4341 4343 if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
4342 4344 zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
4343 4345 "cannot be instantiated", zonecfg_strerror(res),
4344 4346 pool_err);
4345 4347 else
4346 4348 zerror(zlogp, B_FALSE, "could not bind zone to "
4347 4349 "temporary pool: %s", zonecfg_strerror(res));
4348 4350 return (Z_POOL_BIND);
4349 4351 }
4350 4352
4351 4353 /*
4352 4354 * Check if we need to warn about poold not being enabled.
4353 4355 */
4354 4356 if (zonecfg_warn_poold(snap_hndl)) {
4355 4357 zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
4356 4358 "been specified\nbut the dynamic pool service is not "
4357 4359 "enabled.\nThe system will not dynamically adjust the\n"
4358 4360 "processor allocation within the specified range\n"
4359 4361 "until svc:/system/pools/dynamic is enabled.\n"
4360 4362 "See poold(1M).");
4361 4363 }
4362 4364
4363 4365 /* The following is a warning, not an error. */
4364 4366 if ((res = zonecfg_bind_pool(snap_hndl, zoneid, pool_err,
4365 4367 sizeof (pool_err))) != Z_OK) {
4366 4368 if (res == Z_POOL_BIND)
4367 4369 zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
4368 4370 "pool '%s'; using default pool.", pool_err);
4369 4371 else if (res == Z_POOL)
4370 4372 zerror(zlogp, B_FALSE, "WARNING: %s: %s",
4371 4373 zonecfg_strerror(res), pool_err);
4372 4374 else
4373 4375 zerror(zlogp, B_FALSE, "WARNING: %s",
4374 4376 zonecfg_strerror(res));
4375 4377 }
4376 4378
4377 4379 /* Update saved pool name in case it has changed */
4378 4380 (void) zonecfg_get_poolname(snap_hndl, zone_name, pool_name,
4379 4381 sizeof (pool_name));
4380 4382
4381 4383 return (Z_OK);
4382 4384 }
4383 4385
4384 4386 static void
4385 4387 report_prop_err(zlog_t *zlogp, const char *name, const char *value, int res)
4386 4388 {
4387 4389 switch (res) {
4388 4390 case Z_TOO_BIG:
4389 4391 zerror(zlogp, B_FALSE, "%s property value is too large.", name);
4390 4392 break;
4391 4393
4392 4394 case Z_INVALID_PROPERTY:
4393 4395 zerror(zlogp, B_FALSE, "%s property value \"%s\" is not valid",
4394 4396 name, value);
4395 4397 break;
4396 4398
4397 4399 default:
4398 4400 zerror(zlogp, B_TRUE, "fetching property %s: %d", name, res);
4399 4401 break;
4400 4402 }
4401 4403 }
4402 4404
4403 4405 /*
4404 4406 * Sets the hostid of the new zone based on its configured value. The zone's
4405 4407 * zone_t structure must already exist in kernel memory. 'zlogp' refers to the
4406 4408 * log used to report errors and warnings and must be non-NULL. 'zone_namep'
4407 4409 * is the name of the new zone and must be non-NULL. 'zoneid' is the numeric
4408 4410 * ID of the new zone.
4409 4411 *
4410 4412 * This function returns zero on success and a nonzero error code on failure.
4411 4413 */
4412 4414 static int
4413 4415 setup_zone_hostid(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
4414 4416 {
4415 4417 int res;
4416 4418 char hostidp[HW_HOSTID_LEN];
4417 4419 unsigned int hostid;
4418 4420
4419 4421 res = zonecfg_get_hostid(handle, hostidp, sizeof (hostidp));
4420 4422
4421 4423 if (res == Z_BAD_PROPERTY) {
4422 4424 return (Z_OK);
4423 4425 } else if (res != Z_OK) {
4424 4426 report_prop_err(zlogp, "hostid", hostidp, res);
4425 4427 return (res);
4426 4428 }
4427 4429
4428 4430 hostid = (unsigned int)strtoul(hostidp, NULL, 16);
4429 4431 if ((res = zone_setattr(zoneid, ZONE_ATTR_HOSTID, &hostid,
4430 4432 sizeof (hostid))) != 0) {
4431 4433 zerror(zlogp, B_TRUE,
4432 4434 "zone hostid is not valid: %s: %d", hostidp, res);
4433 4435 return (Z_SYSTEM);
4434 4436 }
4435 4437
4436 4438 return (res);
4437 4439 }
4438 4440
4439 4441 static int
4440 4442 setup_zone_fs_allowed(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
4441 4443 {
4442 4444 char fsallowedp[ZONE_FS_ALLOWED_MAX];
4443 4445 int res;
4444 4446
4445 4447 res = zonecfg_get_fs_allowed(handle, fsallowedp, sizeof (fsallowedp));
4446 4448
4447 4449 if (res == Z_BAD_PROPERTY) {
4448 4450 return (Z_OK);
4449 4451 } else if (res != Z_OK) {
4450 4452 report_prop_err(zlogp, "fs-allowed", fsallowedp, res);
4451 4453 return (res);
4452 4454 }
4453 4455
4454 4456 if (zone_setattr(zoneid, ZONE_ATTR_FS_ALLOWED, &fsallowedp,
4455 4457 sizeof (fsallowedp)) != 0) {
4456 4458 zerror(zlogp, B_TRUE,
4457 4459 "fs-allowed couldn't be set: %s: %d", fsallowedp, res);
4458 4460 return (Z_SYSTEM);
4459 4461 }
4460 4462
4461 4463 return (res);
4462 4464 }
4463 4465
4464 4466 static int
4465 4467 setup_zone_attrs(zlog_t *zlogp, zoneid_t zoneid)
4466 4468 {
4467 4469 int res = Z_OK;
4468 4470
4469 4471 if ((res = setup_zone_hostid(snap_hndl, zlogp, zoneid)) != Z_OK)
4470 4472 goto out;
4471 4473
4472 4474 if ((res = setup_zone_fs_allowed(snap_hndl, zlogp, zoneid)) != Z_OK)
4473 4475 goto out;
4474 4476
4475 4477 out:
4476 4478 return (res);
4477 4479 }
4478 4480
4479 4481 /*
4480 4482 * The zone_did is a persistent debug ID. Each zone should have a unique ID
4481 4483 * in the kernel. This is used for things like DTrace which want to monitor
4482 4484 * zones across reboots. They can't use the zoneid since that changes on
4483 4485 * each boot.
4484 4486 */
4485 4487 zoneid_t
4486 4488 vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zone_did)
4487 4489 {
4488 4490 zoneid_t rval = -1;
4489 4491 priv_set_t *privs;
4490 4492 char rootpath[MAXPATHLEN];
4491 4493 char *rctlbuf = NULL;
↓ open down ↓ |
1284 lines elided |
↑ open up ↑ |
4492 4494 size_t rctlbufsz = 0;
4493 4495 char *zfsbuf = NULL;
4494 4496 size_t zfsbufsz = 0;
4495 4497 zoneid_t zoneid = -1;
4496 4498 int xerr;
4497 4499 char *kzone;
4498 4500 FILE *fp = NULL;
4499 4501 tsol_zcent_t *zcent = NULL;
4500 4502 int match = 0;
4501 4503 int doi = 0;
4502 - int flags;
4504 + int flags = -1;
4503 4505 zone_iptype_t iptype;
4504 4506
4505 4507 if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
4506 4508 zerror(zlogp, B_TRUE, "unable to determine zone root");
4507 4509 return (-1);
4508 4510 }
4509 4511 if (zonecfg_in_alt_root())
4510 4512 resolve_lofs(zlogp, rootpath, sizeof (rootpath));
4511 4513
4512 4514 if (vplat_get_iptype(zlogp, &iptype) < 0) {
4513 4515 zerror(zlogp, B_TRUE, "unable to determine ip-type");
↓ open down ↓ |
1 lines elided |
↑ open up ↑ |
4514 4516 return (-1);
4515 4517 }
4516 4518 switch (iptype) {
4517 4519 case ZS_SHARED:
4518 4520 flags = 0;
4519 4521 break;
4520 4522 case ZS_EXCLUSIVE:
4521 4523 flags = ZCF_NET_EXCL;
4522 4524 break;
4523 4525 }
4526 + if (flags == -1)
4527 + abort();
4524 4528
4525 4529 if ((privs = priv_allocset()) == NULL) {
4526 4530 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
4527 4531 return (-1);
4528 4532 }
4529 4533 priv_emptyset(privs);
4530 4534 if (get_privset(zlogp, privs, mount_cmd) != 0)
4531 4535 goto error;
4532 4536
4533 4537 if (mount_cmd == Z_MNT_BOOT &&
4534 4538 get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
4535 4539 zerror(zlogp, B_FALSE, "Unable to get list of rctls");
4536 4540 goto error;
4537 4541 }
4538 4542
4539 4543 if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4540 4544 zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4541 4545 goto error;
4542 4546 }
4543 4547
4544 4548 if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
4545 4549 zcent = get_zone_label(zlogp, privs);
4546 4550 if (zcent != NULL) {
4547 4551 match = zcent->zc_match;
4548 4552 doi = zcent->zc_doi;
4549 4553 *zlabel = zcent->zc_label;
4550 4554 } else {
4551 4555 goto error;
4552 4556 }
4553 4557 if (validate_rootds_label(zlogp, rootpath, zlabel) != 0)
4554 4558 goto error;
4555 4559 }
4556 4560
4557 4561 kzone = zone_name;
4558 4562
4559 4563 /*
4560 4564 * We must do this scan twice. First, we look for zones running on the
4561 4565 * main system that are using this root (or any subdirectory of it).
4562 4566 * Next, we reduce to the shortest path and search for loopback mounts
4563 4567 * that use this same source node (same device and inode).
4564 4568 */
4565 4569 if (duplicate_zone_root(zlogp, rootpath))
4566 4570 goto error;
4567 4571 if (duplicate_reachable_path(zlogp, rootpath))
4568 4572 goto error;
4569 4573
4570 4574 if (ALT_MOUNT(mount_cmd)) {
4571 4575 root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4572 4576
4573 4577 /*
4574 4578 * Forge up a special root for this zone. When a zone is
4575 4579 * mounted, we can't let the zone have its own root because the
4576 4580 * tools that will be used in this "scratch zone" need access
4577 4581 * to both the zone's resources and the running machine's
4578 4582 * executables.
4579 4583 *
4580 4584 * Note that the mkdir here also catches read-only filesystems.
4581 4585 */
4582 4586 if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4583 4587 zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4584 4588 goto error;
4585 4589 }
4586 4590 if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4587 4591 goto error;
4588 4592 }
4589 4593
4590 4594 if (zonecfg_in_alt_root()) {
4591 4595 /*
4592 4596 * If we are mounting up a zone in an alternate root partition,
4593 4597 * then we have some additional work to do before starting the
4594 4598 * zone. First, resolve the root path down so that we're not
4595 4599 * fooled by duplicates. Then forge up an internal name for
4596 4600 * the zone.
4597 4601 */
4598 4602 if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4599 4603 zerror(zlogp, B_TRUE, "cannot open mapfile");
4600 4604 goto error;
4601 4605 }
4602 4606 if (zonecfg_lock_scratch(fp) != 0) {
4603 4607 zerror(zlogp, B_TRUE, "cannot lock mapfile");
4604 4608 goto error;
4605 4609 }
4606 4610 if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4607 4611 NULL, 0) == 0) {
4608 4612 zerror(zlogp, B_FALSE, "scratch zone already running");
4609 4613 goto error;
4610 4614 }
4611 4615 /* This is the preferred name */
4612 4616 (void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4613 4617 zone_name);
4614 4618 srandom(getpid());
4615 4619 while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4616 4620 0) == 0) {
4617 4621 /* This is just an arbitrary name; note "." usage */
4618 4622 (void) snprintf(kernzone, sizeof (kernzone),
4619 4623 "SUNWlu.%08lX%08lX", random(), random());
4620 4624 }
4621 4625 kzone = kernzone;
4622 4626 }
4623 4627
4624 4628 xerr = 0;
4625 4629 if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
4626 4630 rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
4627 4631 flags, zone_did)) == -1) {
4628 4632 if (xerr == ZE_AREMOUNTS) {
4629 4633 if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
4630 4634 zerror(zlogp, B_FALSE,
4631 4635 "An unknown file-system is mounted on "
4632 4636 "a subdirectory of %s", rootpath);
4633 4637 } else {
4634 4638
4635 4639 zerror(zlogp, B_FALSE,
4636 4640 "These file-systems are mounted on "
4637 4641 "subdirectories of %s:", rootpath);
4638 4642 (void) zonecfg_find_mounts(rootpath,
4639 4643 prtmount, zlogp);
4640 4644 }
4641 4645 } else if (xerr == ZE_CHROOTED) {
4642 4646 zerror(zlogp, B_FALSE, "%s: "
4643 4647 "cannot create a zone from a chrooted "
4644 4648 "environment", "zone_create");
4645 4649 } else if (xerr == ZE_LABELINUSE) {
4646 4650 char zonename[ZONENAME_MAX];
4647 4651 (void) getzonenamebyid(getzoneidbylabel(zlabel),
4648 4652 zonename, ZONENAME_MAX);
4649 4653 zerror(zlogp, B_FALSE, "The zone label is already "
4650 4654 "used by the zone '%s'.", zonename);
4651 4655 } else {
4652 4656 zerror(zlogp, B_TRUE, "%s failed", "zone_create");
4653 4657 }
4654 4658 goto error;
4655 4659 }
4656 4660
4657 4661 if (zonecfg_in_alt_root() &&
4658 4662 zonecfg_add_scratch(fp, zone_name, kernzone,
4659 4663 zonecfg_get_root()) == -1) {
4660 4664 zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4661 4665 goto error;
4662 4666 }
4663 4667
4664 4668 /*
4665 4669 * The following actions are not performed when merely mounting a zone
4666 4670 * for administrative use.
4667 4671 */
4668 4672 if (mount_cmd == Z_MNT_BOOT) {
4669 4673 brand_handle_t bh;
4670 4674 struct brand_attr attr;
4671 4675 char modname[MAXPATHLEN];
4672 4676
4673 4677 if (setup_zone_attrs(zlogp, zoneid) != Z_OK)
4674 4678 goto error;
4675 4679
4676 4680 if ((bh = brand_open(brand_name)) == NULL) {
4677 4681 zerror(zlogp, B_FALSE,
4678 4682 "unable to determine brand name");
4679 4683 goto error;
4680 4684 }
4681 4685
4682 4686 if (!is_system_labeled() &&
4683 4687 (strcmp(brand_name, LABELED_BRAND_NAME) == 0)) {
4684 4688 brand_close(bh);
4685 4689 zerror(zlogp, B_FALSE,
4686 4690 "cannot boot labeled zone on unlabeled system");
4687 4691 goto error;
4688 4692 }
4689 4693
4690 4694 /*
4691 4695 * If this brand requires any kernel support, now is the time to
4692 4696 * get it loaded and initialized.
4693 4697 */
4694 4698 if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
4695 4699 brand_close(bh);
4696 4700 zerror(zlogp, B_FALSE,
4697 4701 "unable to determine brand kernel module");
4698 4702 goto error;
4699 4703 }
4700 4704 brand_close(bh);
4701 4705
4702 4706 if (strlen(modname) > 0) {
4703 4707 (void) strlcpy(attr.ba_brandname, brand_name,
4704 4708 sizeof (attr.ba_brandname));
4705 4709 (void) strlcpy(attr.ba_modname, modname,
4706 4710 sizeof (attr.ba_modname));
4707 4711 if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
4708 4712 sizeof (attr) != 0)) {
4709 4713 zerror(zlogp, B_TRUE,
4710 4714 "could not set zone brand attribute.");
4711 4715 goto error;
4712 4716 }
4713 4717 }
4714 4718
4715 4719 if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK)
4716 4720 goto error;
4717 4721
4718 4722 set_mlps(zlogp, zoneid, zcent);
4719 4723 }
4720 4724
4721 4725 rval = zoneid;
4722 4726 zoneid = -1;
4723 4727
4724 4728 error:
4725 4729 if (zoneid != -1) {
4726 4730 (void) zone_shutdown(zoneid);
4727 4731 (void) zone_destroy(zoneid);
4728 4732 }
4729 4733 if (rctlbuf != NULL)
4730 4734 free(rctlbuf);
4731 4735 if (zfsbuf != NULL)
4732 4736 free(zfsbuf);
4733 4737 priv_freeset(privs);
4734 4738 if (fp != NULL)
4735 4739 zonecfg_close_scratch(fp);
4736 4740 lofs_discard_mnttab();
4737 4741 if (zcent != NULL)
4738 4742 tsol_freezcent(zcent);
4739 4743 return (rval);
4740 4744 }
4741 4745
4742 4746 /*
4743 4747 * Enter the zone and write a /etc/zones/index file there. This allows
4744 4748 * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
4745 4749 * details from inside the zone.
4746 4750 */
4747 4751 static void
4748 4752 write_index_file(zoneid_t zoneid)
4749 4753 {
4750 4754 FILE *zef;
4751 4755 FILE *zet;
4752 4756 struct zoneent *zep;
4753 4757 pid_t child;
4754 4758 int tmpl_fd;
4755 4759 ctid_t ct;
4756 4760 int fd;
4757 4761 char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
4758 4762
4759 4763 /* Locate the zone entry in the global zone's index file */
4760 4764 if ((zef = setzoneent()) == NULL)
4761 4765 return;
4762 4766 while ((zep = getzoneent_private(zef)) != NULL) {
4763 4767 if (strcmp(zep->zone_name, zone_name) == 0)
4764 4768 break;
4765 4769 free(zep);
4766 4770 }
4767 4771 endzoneent(zef);
4768 4772 if (zep == NULL)
4769 4773 return;
4770 4774
4771 4775 if ((tmpl_fd = init_template()) == -1) {
4772 4776 free(zep);
4773 4777 return;
4774 4778 }
4775 4779
4776 4780 if ((child = fork()) == -1) {
4777 4781 (void) ct_tmpl_clear(tmpl_fd);
4778 4782 (void) close(tmpl_fd);
4779 4783 free(zep);
4780 4784 return;
4781 4785 }
4782 4786
4783 4787 /* parent waits for child to finish */
4784 4788 if (child != 0) {
4785 4789 free(zep);
4786 4790 if (contract_latest(&ct) == -1)
4787 4791 ct = -1;
4788 4792 (void) ct_tmpl_clear(tmpl_fd);
4789 4793 (void) close(tmpl_fd);
4790 4794 (void) waitpid(child, NULL, 0);
4791 4795 (void) contract_abandon_id(ct);
4792 4796 return;
4793 4797 }
4794 4798
4795 4799 /* child enters zone and sets up index file */
4796 4800 (void) ct_tmpl_clear(tmpl_fd);
4797 4801 if (zone_enter(zoneid) != -1) {
4798 4802 (void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
4799 4803 (void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
4800 4804 ZONE_CONFIG_GID);
4801 4805 fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
4802 4806 ZONE_INDEX_MODE);
4803 4807 if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
4804 4808 (void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
4805 4809 if (uuid_is_null(zep->zone_uuid))
4806 4810 uuidstr[0] = '\0';
4807 4811 else
4808 4812 uuid_unparse(zep->zone_uuid, uuidstr);
4809 4813 (void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
4810 4814 zone_state_str(zep->zone_state),
4811 4815 uuidstr);
4812 4816 (void) fclose(zet);
4813 4817 }
4814 4818 }
4815 4819 _exit(0);
4816 4820 }
4817 4821
4818 4822 int
4819 4823 vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
4820 4824 {
4821 4825 char zonepath[MAXPATHLEN];
4822 4826
4823 4827 if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
4824 4828 lofs_discard_mnttab();
4825 4829 return (-1);
4826 4830 }
4827 4831
4828 4832 /*
4829 4833 * Before we try to mount filesystems we need to create the
4830 4834 * attribute backing store for /dev
4831 4835 */
4832 4836 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
4833 4837 lofs_discard_mnttab();
4834 4838 return (-1);
4835 4839 }
4836 4840 resolve_lofs(zlogp, zonepath, sizeof (zonepath));
4837 4841
4838 4842 /* Make /dev directory owned by root, grouped sys */
4839 4843 if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
4840 4844 0, 3) != 0) {
4841 4845 lofs_discard_mnttab();
4842 4846 return (-1);
4843 4847 }
4844 4848
4845 4849 if (mount_filesystems(zlogp, mount_cmd) != 0) {
4846 4850 lofs_discard_mnttab();
4847 4851 return (-1);
4848 4852 }
4849 4853
4850 4854 if (mount_cmd == Z_MNT_BOOT) {
4851 4855 zone_iptype_t iptype;
4852 4856
4853 4857 if (vplat_get_iptype(zlogp, &iptype) < 0) {
4854 4858 zerror(zlogp, B_TRUE, "unable to determine ip-type");
4855 4859 lofs_discard_mnttab();
4856 4860 return (-1);
4857 4861 }
4858 4862
4859 4863 switch (iptype) {
4860 4864 case ZS_SHARED:
4861 4865 /* Always do this to make lo0 get configured */
4862 4866 if (configure_shared_network_interfaces(zlogp) != 0) {
4863 4867 lofs_discard_mnttab();
4864 4868 return (-1);
↓ open down ↓ |
331 lines elided |
↑ open up ↑ |
4865 4869 }
4866 4870 break;
4867 4871 case ZS_EXCLUSIVE:
4868 4872 if (configure_exclusive_network_interfaces(zlogp,
4869 4873 zoneid) !=
4870 4874 0) {
4871 4875 lofs_discard_mnttab();
4872 4876 return (-1);
4873 4877 }
4874 4878 break;
4879 + default:
4880 + abort();
4875 4881 }
4876 4882 }
4877 4883
4878 4884 write_index_file(zoneid);
4879 4885
4880 4886 lofs_discard_mnttab();
4881 4887 return (0);
4882 4888 }
4883 4889
4884 4890 static int
4885 4891 lu_root_teardown(zlog_t *zlogp)
4886 4892 {
4887 4893 char zroot[MAXPATHLEN];
4888 4894
4889 4895 if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
4890 4896 zerror(zlogp, B_FALSE, "unable to determine zone root");
4891 4897 return (-1);
4892 4898 }
4893 4899 root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
4894 4900
4895 4901 /*
4896 4902 * At this point, the processes are gone, the filesystems (save the
4897 4903 * root) are unmounted, and the zone is on death row. But there may
4898 4904 * still be creds floating about in the system that reference the
4899 4905 * zone_t, and which pin down zone_rootvp causing this call to fail
4900 4906 * with EBUSY. Thus, we try for a little while before just giving up.
4901 4907 * (How I wish this were not true, and umount2 just did the right
4902 4908 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
4903 4909 */
4904 4910 if (umount2(zroot, MS_FORCE) != 0) {
4905 4911 if (errno == ENOTSUP && umount2(zroot, 0) == 0)
4906 4912 goto unmounted;
4907 4913 if (errno == EBUSY) {
4908 4914 int tries = 10;
4909 4915
4910 4916 while (--tries >= 0) {
4911 4917 (void) sleep(1);
4912 4918 if (umount2(zroot, 0) == 0)
4913 4919 goto unmounted;
4914 4920 if (errno != EBUSY)
4915 4921 break;
4916 4922 }
4917 4923 }
4918 4924 zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
4919 4925 return (-1);
4920 4926 }
4921 4927 unmounted:
4922 4928
4923 4929 /*
4924 4930 * Only zones in an alternate root environment have scratch zone
4925 4931 * entries.
4926 4932 */
4927 4933 if (zonecfg_in_alt_root()) {
4928 4934 FILE *fp;
4929 4935 int retv;
4930 4936
4931 4937 if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4932 4938 zerror(zlogp, B_TRUE, "cannot open mapfile");
4933 4939 return (-1);
4934 4940 }
4935 4941 retv = -1;
4936 4942 if (zonecfg_lock_scratch(fp) != 0)
4937 4943 zerror(zlogp, B_TRUE, "cannot lock mapfile");
4938 4944 else if (zonecfg_delete_scratch(fp, kernzone) != 0)
4939 4945 zerror(zlogp, B_TRUE, "cannot delete map entry");
4940 4946 else
4941 4947 retv = 0;
4942 4948 zonecfg_close_scratch(fp);
4943 4949 return (retv);
4944 4950 } else {
4945 4951 return (0);
4946 4952 }
4947 4953 }
4948 4954
4949 4955 int
4950 4956 vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting,
4951 4957 boolean_t debug)
4952 4958 {
4953 4959 char *kzone;
4954 4960 zoneid_t zoneid;
4955 4961 int res;
4956 4962 char pool_err[128];
4957 4963 char zpath[MAXPATHLEN];
4958 4964 char cmdbuf[MAXPATHLEN];
4959 4965 brand_handle_t bh = NULL;
4960 4966 dladm_status_t status;
4961 4967 char errmsg[DLADM_STRSIZE];
4962 4968 ushort_t flags;
4963 4969
4964 4970 kzone = zone_name;
4965 4971 if (zonecfg_in_alt_root()) {
4966 4972 FILE *fp;
4967 4973
4968 4974 if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4969 4975 zerror(zlogp, B_TRUE, "unable to open map file");
4970 4976 goto error;
4971 4977 }
4972 4978 if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4973 4979 kernzone, sizeof (kernzone)) != 0) {
4974 4980 zerror(zlogp, B_FALSE, "unable to find scratch zone");
4975 4981 zonecfg_close_scratch(fp);
4976 4982 goto error;
4977 4983 }
4978 4984 zonecfg_close_scratch(fp);
4979 4985 kzone = kernzone;
4980 4986 }
4981 4987
4982 4988 if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
4983 4989 if (!bringup_failure_recovery)
4984 4990 zerror(zlogp, B_TRUE, "unable to get zoneid");
4985 4991 if (unmount_cmd)
4986 4992 (void) lu_root_teardown(zlogp);
4987 4993 goto error;
4988 4994 }
4989 4995
4990 4996 if (remove_datalink_pool(zlogp, zoneid) != 0)
4991 4997 zerror(zlogp, B_FALSE, "unable clear datalink pool property");
4992 4998
4993 4999 if (remove_datalink_protect(zlogp, zoneid) != 0)
4994 5000 zerror(zlogp, B_FALSE,
4995 5001 "unable clear datalink protect property");
4996 5002
4997 5003 /*
4998 5004 * The datalinks assigned to the zone will be removed from the NGZ as
4999 5005 * part of zone_shutdown() so that we need to remove protect/pool etc.
5000 5006 * before zone_shutdown(). Even if the shutdown itself fails, the zone
5001 5007 * will not be able to violate any constraints applied because the
5002 5008 * datalinks are no longer available to the zone.
5003 5009 */
5004 5010 if (zone_shutdown(zoneid) != 0) {
5005 5011 zerror(zlogp, B_TRUE, "unable to shutdown zone");
5006 5012 goto error;
5007 5013 }
5008 5014
5009 5015 /* Get the zonepath of this zone */
5010 5016 if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
5011 5017 zerror(zlogp, B_FALSE, "unable to determine zone path");
5012 5018 goto error;
5013 5019 }
5014 5020
5015 5021 /* Get a handle to the brand info for this zone */
5016 5022 if ((bh = brand_open(brand_name)) == NULL) {
5017 5023 zerror(zlogp, B_FALSE, "unable to determine zone brand");
5018 5024 return (-1);
5019 5025 }
5020 5026 /*
5021 5027 * If there is a brand 'halt' callback, execute it now to give the
5022 5028 * brand a chance to cleanup any custom configuration.
5023 5029 */
5024 5030 (void) strcpy(cmdbuf, EXEC_PREFIX);
5025 5031 if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
5026 5032 sizeof (cmdbuf) - EXEC_LEN) < 0) {
5027 5033 brand_close(bh);
5028 5034 zerror(zlogp, B_FALSE, "unable to determine branded zone's "
5029 5035 "halt callback.");
5030 5036 goto error;
5031 5037 }
5032 5038 brand_close(bh);
5033 5039
5034 5040 if ((strlen(cmdbuf) > EXEC_LEN) &&
5035 5041 (do_subproc(zlogp, cmdbuf, NULL, debug) != Z_OK)) {
5036 5042 zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
5037 5043 goto error;
5038 5044 }
5039 5045
5040 5046 if (!unmount_cmd) {
5041 5047 zone_iptype_t iptype;
5042 5048
5043 5049 if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
5044 5050 sizeof (flags)) < 0) {
5045 5051 if (vplat_get_iptype(zlogp, &iptype) < 0) {
5046 5052 zerror(zlogp, B_TRUE, "unable to determine "
5047 5053 "ip-type");
5048 5054 goto error;
5049 5055 }
5050 5056 } else {
5051 5057 if (flags & ZF_NET_EXCL)
5052 5058 iptype = ZS_EXCLUSIVE;
5053 5059 else
5054 5060 iptype = ZS_SHARED;
5055 5061 }
5056 5062
5057 5063 switch (iptype) {
5058 5064 case ZS_SHARED:
5059 5065 if (unconfigure_shared_network_interfaces(zlogp,
5060 5066 zoneid) != 0) {
5061 5067 zerror(zlogp, B_FALSE, "unable to unconfigure "
5062 5068 "network interfaces in zone");
5063 5069 goto error;
5064 5070 }
5065 5071 break;
5066 5072 case ZS_EXCLUSIVE:
5067 5073 status = dladm_zone_halt(dld_handle, zoneid);
5068 5074 if (status != DLADM_STATUS_OK) {
5069 5075 zerror(zlogp, B_FALSE, "unable to notify "
5070 5076 "dlmgmtd of zone halt: %s",
5071 5077 dladm_status2str(status, errmsg));
5072 5078 }
5073 5079 break;
5074 5080 }
5075 5081 }
5076 5082
5077 5083 if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
5078 5084 zerror(zlogp, B_TRUE, "unable to abort TCP connections");
5079 5085 goto error;
5080 5086 }
5081 5087
5082 5088 if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
5083 5089 zerror(zlogp, B_FALSE,
5084 5090 "unable to unmount file systems in zone");
5085 5091 goto error;
5086 5092 }
5087 5093
5088 5094 /*
5089 5095 * If we are rebooting then we normally don't want to destroy an
5090 5096 * existing temporary pool at this point so that we can just reuse it
5091 5097 * when the zone boots back up. However, it is also possible we were
5092 5098 * running with a temporary pool and the zone configuration has been
5093 5099 * modified to no longer use a temporary pool. In that case we need
5094 5100 * to destroy the temporary pool now. This case looks like the case
5095 5101 * where we never had a temporary pool configured but
5096 5102 * zonecfg_destroy_tmp_pool will do the right thing either way.
5097 5103 */
5098 5104 if (!unmount_cmd) {
5099 5105 boolean_t destroy_tmp_pool = B_TRUE;
5100 5106
5101 5107 if (rebooting) {
5102 5108 struct zone_psettab pset_tab;
5103 5109
5104 5110 if (zonecfg_lookup_pset(snap_hndl, &pset_tab) == Z_OK)
5105 5111 destroy_tmp_pool = B_FALSE;
5106 5112 }
5107 5113
5108 5114 if (destroy_tmp_pool) {
5109 5115 if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
5110 5116 sizeof (pool_err))) != Z_OK) {
5111 5117 if (res == Z_POOL)
5112 5118 zerror(zlogp, B_FALSE, pool_err);
5113 5119 }
5114 5120 }
5115 5121 }
5116 5122
5117 5123 remove_mlps(zlogp, zoneid);
5118 5124
5119 5125 if (zone_destroy(zoneid) != 0) {
5120 5126 zerror(zlogp, B_TRUE, "unable to destroy zone");
5121 5127 goto error;
5122 5128 }
5123 5129
5124 5130 /*
5125 5131 * Special teardown for alternate boot environments: remove the tmpfs
5126 5132 * root for the zone and then remove it from the map file.
5127 5133 */
5128 5134 if (unmount_cmd && lu_root_teardown(zlogp) != 0)
5129 5135 goto error;
5130 5136
5131 5137 lofs_discard_mnttab();
5132 5138 return (0);
5133 5139
5134 5140 error:
5135 5141 lofs_discard_mnttab();
5136 5142 return (-1);
5137 5143 }
↓ open down ↓ |
253 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX