Print this page
PSARC 2008/290 lofi mount
6384817 Need persistent lofi based mounts and direct mount(1m) support for lofi
| 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
|
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
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 2008 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 -#pragma ident "@(#)vplat.c 1.60 08/04/08 SMI"
27 +#pragma ident "@(#)vplat.c 1.61 08/05/07 SMI"
28 28
29 29 /*
30 30 * This module contains functions used to bring up and tear down the
31 31 * Virtual Platform: [un]mounting file-systems, [un]plumbing network
32 32 * interfaces, [un]configuring devices, establishing resource controls,
33 33 * and creating/destroying the zone in the kernel. These actions, on
34 34 * the way up, ready the zone; on the way down, they halt the zone.
35 35 * See the much longer block comment at the beginning of zoneadmd.c
36 36 * for a bigger picture of how the whole program functions.
37 37 *
38 38 * This module also has primary responsibility for the layout of "scratch
39 39 * zones." These are mounted, but inactive, zones that are used during
40 40 * operating system upgrade and potentially other administrative action. The
41 41 * scratch zone environment is similar to the miniroot environment. The zone's
42 42 * actual root is mounted read-write on /a, and the standard paths (/usr,
43 43 * /sbin, /lib) all lead to read-only copies of the running system's binaries.
44 44 * This allows the administrative tools to manipulate the zone using "-R /a"
45 45 * without relying on any binaries in the zone itself.
46 46 *
47 47 * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
48 48 * environment), then we must resolve the lofs mounts used there to uncover
49 49 * writable (unshared) resources. Shared resources, though, are always
50 50 * read-only. In addition, if the "same" zone with a different root path is
51 51 * currently running, then "/b" inside the zone points to the running zone's
52 52 * root. This allows LU to synchronize configuration files during the upgrade
53 53 * process.
54 54 *
55 55 * To construct this environment, this module creates a tmpfs mount on
56 56 * $ZONEPATH/lu. Inside this scratch area, the miniroot-like environment as
57 57 * described above is constructed on the fly. The zone is then created using
58 58 * $ZONEPATH/lu as the root.
59 59 *
60 60 * Note that scratch zones are inactive. The zone's bits are not running and
61 61 * likely cannot be run correctly until upgrade is done. Init is not running
62 62 * there, nor is SMF. Because of this, the "mounted" state of a scratch zone
63 63 * is not a part of the usual halt/ready/boot state machine.
64 64 */
65 65
66 66 #include <sys/param.h>
67 67 #include <sys/mount.h>
68 68 #include <sys/mntent.h>
69 69 #include <sys/socket.h>
70 70 #include <sys/utsname.h>
71 71 #include <sys/types.h>
72 72 #include <sys/stat.h>
73 73 #include <sys/sockio.h>
74 74 #include <sys/stropts.h>
75 75 #include <sys/conf.h>
76 76
77 77 #include <sys/dlpi.h>
78 78 #include <libdlpi.h>
79 79 #include <libdllink.h>
80 80 #include <libdlvlan.h>
81 81
82 82 #include <inet/tcp.h>
83 83 #include <arpa/inet.h>
84 84 #include <netinet/in.h>
85 85 #include <net/route.h>
86 86
87 87 #include <stdio.h>
88 88 #include <errno.h>
89 89 #include <fcntl.h>
90 90 #include <unistd.h>
91 91 #include <rctl.h>
92 92 #include <stdlib.h>
93 93 #include <string.h>
94 94 #include <strings.h>
95 95 #include <wait.h>
96 96 #include <limits.h>
97 97 #include <libgen.h>
98 98 #include <libzfs.h>
99 99 #include <libdevinfo.h>
100 100 #include <zone.h>
101 101 #include <assert.h>
102 102 #include <libcontract.h>
103 103 #include <libcontract_priv.h>
104 104 #include <uuid/uuid.h>
105 105
106 106 #include <sys/mntio.h>
107 107 #include <sys/mnttab.h>
108 108 #include <sys/fs/autofs.h> /* for _autofssys() */
109 109 #include <sys/fs/lofs_info.h>
110 110 #include <sys/fs/zfs.h>
111 111
112 112 #include <pool.h>
113 113 #include <sys/pool.h>
114 114 #include <sys/priocntl.h>
115 115
116 116 #include <libbrand.h>
117 117 #include <sys/brand.h>
118 118 #include <libzonecfg.h>
119 119 #include <synch.h>
120 120
121 121 #include "zoneadmd.h"
122 122 #include <tsol/label.h>
123 123 #include <libtsnet.h>
124 124 #include <sys/priv.h>
125 125
126 126 #define V4_ADDR_LEN 32
127 127 #define V6_ADDR_LEN 128
128 128
129 129 #define IPD_DEFAULT_OPTS \
130 130 MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
131 131
132 132 #define DFSTYPES "/etc/dfs/fstypes"
133 133 #define MAXTNZLEN 2048
134 134
135 135 #define ALT_MOUNT(mount_cmd) ((mount_cmd) != Z_MNT_BOOT)
136 136
137 137 /* for routing socket */
138 138 static int rts_seqno = 0;
139 139
140 140 /* mangled zone name when mounting in an alternate root environment */
141 141 static char kernzone[ZONENAME_MAX];
142 142
143 143 /* array of cached mount entries for resolve_lofs */
144 144 static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
145 145
146 146 /* for Trusted Extensions */
147 147 static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
148 148 static int tsol_mounts(zlog_t *, char *, char *);
149 149 static void tsol_unmounts(zlog_t *, char *);
150 150
151 151 static m_label_t *zlabel = NULL;
152 152 static m_label_t *zid_label = NULL;
153 153 static priv_set_t *zprivs = NULL;
154 154
155 155 /* from libsocket, not in any header file */
156 156 extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
157 157
158 158 /*
159 159 * An optimization for build_mnttable: reallocate (and potentially copy the
160 160 * data) only once every N times through the loop.
161 161 */
162 162 #define MNTTAB_HUNK 32
163 163
164 164 /*
165 165 * Private autofs system call
166 166 */
167 167 extern int _autofssys(int, void *);
168 168
169 169 static int
170 170 autofs_cleanup(zoneid_t zoneid)
171 171 {
172 172 /*
173 173 * Ask autofs to unmount all trigger nodes in the given zone.
174 174 */
175 175 return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
176 176 }
177 177
178 178 static void
179 179 free_mnttable(struct mnttab *mnt_array, uint_t nelem)
180 180 {
181 181 uint_t i;
182 182
183 183 if (mnt_array == NULL)
184 184 return;
185 185 for (i = 0; i < nelem; i++) {
186 186 free(mnt_array[i].mnt_mountp);
187 187 free(mnt_array[i].mnt_fstype);
188 188 free(mnt_array[i].mnt_special);
189 189 free(mnt_array[i].mnt_mntopts);
190 190 assert(mnt_array[i].mnt_time == NULL);
191 191 }
192 192 free(mnt_array);
193 193 }
194 194
195 195 /*
196 196 * Build the mount table for the zone rooted at "zroot", storing the resulting
197 197 * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
198 198 * array in "nelemp".
199 199 */
200 200 static int
201 201 build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
202 202 struct mnttab **mnt_arrayp, uint_t *nelemp)
203 203 {
204 204 struct mnttab mnt;
205 205 struct mnttab *mnts;
206 206 struct mnttab *mnp;
207 207 uint_t nmnt;
208 208
209 209 rewind(mnttab);
210 210 resetmnttab(mnttab);
211 211 nmnt = 0;
212 212 mnts = NULL;
213 213 while (getmntent(mnttab, &mnt) == 0) {
214 214 struct mnttab *tmp_array;
215 215
216 216 if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
217 217 continue;
218 218 if (nmnt % MNTTAB_HUNK == 0) {
219 219 tmp_array = realloc(mnts,
220 220 (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
221 221 if (tmp_array == NULL) {
222 222 free_mnttable(mnts, nmnt);
223 223 return (-1);
224 224 }
225 225 mnts = tmp_array;
226 226 }
227 227 mnp = &mnts[nmnt++];
228 228
229 229 /*
230 230 * Zero out any fields we're not using.
231 231 */
232 232 (void) memset(mnp, 0, sizeof (*mnp));
233 233
234 234 if (mnt.mnt_special != NULL)
235 235 mnp->mnt_special = strdup(mnt.mnt_special);
236 236 if (mnt.mnt_mntopts != NULL)
237 237 mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
238 238 mnp->mnt_mountp = strdup(mnt.mnt_mountp);
239 239 mnp->mnt_fstype = strdup(mnt.mnt_fstype);
240 240 if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
241 241 (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
242 242 mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
243 243 zerror(zlogp, B_TRUE, "memory allocation failed");
244 244 free_mnttable(mnts, nmnt);
245 245 return (-1);
246 246 }
247 247 }
248 248 *mnt_arrayp = mnts;
249 249 *nelemp = nmnt;
250 250 return (0);
251 251 }
252 252
253 253 /*
254 254 * This is an optimization. The resolve_lofs function is used quite frequently
255 255 * to manipulate file paths, and on a machine with a large number of zones,
256 256 * there will be a huge number of mounted file systems. Thus, we trigger a
257 257 * reread of the list of mount points
258 258 */
259 259 static void
260 260 lofs_discard_mnttab(void)
261 261 {
262 262 free_mnttable(resolve_lofs_mnts,
263 263 resolve_lofs_mnt_max - resolve_lofs_mnts);
264 264 resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
265 265 }
266 266
267 267 static int
268 268 lofs_read_mnttab(zlog_t *zlogp)
269 269 {
270 270 FILE *mnttab;
271 271 uint_t nmnts;
272 272
273 273 if ((mnttab = fopen(MNTTAB, "r")) == NULL)
274 274 return (-1);
275 275 if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
276 276 &nmnts) == -1) {
277 277 (void) fclose(mnttab);
278 278 return (-1);
279 279 }
280 280 (void) fclose(mnttab);
281 281 resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
282 282 return (0);
283 283 }
284 284
285 285 /*
286 286 * This function loops over potential loopback mounts and symlinks in a given
287 287 * path and resolves them all down to an absolute path.
288 288 */
289 289 void
290 290 resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
291 291 {
292 292 int len, arlen;
293 293 const char *altroot;
294 294 char tmppath[MAXPATHLEN];
295 295 boolean_t outside_altroot;
296 296
297 297 if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
298 298 return;
299 299 tmppath[len] = '\0';
300 300 (void) strlcpy(path, tmppath, sizeof (tmppath));
301 301
302 302 /* This happens once per zoneadmd operation. */
303 303 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
304 304 return;
305 305
306 306 altroot = zonecfg_get_root();
307 307 arlen = strlen(altroot);
308 308 outside_altroot = B_FALSE;
309 309 for (;;) {
310 310 struct mnttab *mnp;
311 311
312 312 /* Search in reverse order to find longest match */
313 313 for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
314 314 mnp--) {
315 315 if (mnp->mnt_fstype == NULL ||
316 316 mnp->mnt_mountp == NULL ||
317 317 mnp->mnt_special == NULL)
318 318 continue;
319 319 len = strlen(mnp->mnt_mountp);
320 320 if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
321 321 (path[len] == '/' || path[len] == '\0'))
322 322 break;
323 323 }
324 324 if (mnp < resolve_lofs_mnts)
325 325 break;
326 326 /* If it's not a lofs then we're done */
327 327 if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
328 328 break;
329 329 if (outside_altroot) {
330 330 char *cp;
331 331 int olen = sizeof (MNTOPT_RO) - 1;
332 332
333 333 /*
334 334 * If we run into a read-only mount outside of the
335 335 * alternate root environment, then the user doesn't
336 336 * want this path to be made read-write.
337 337 */
338 338 if (mnp->mnt_mntopts != NULL &&
339 339 (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
340 340 NULL &&
341 341 (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
342 342 (cp[olen] == '\0' || cp[olen] == ',')) {
343 343 break;
344 344 }
345 345 } else if (arlen > 0 &&
346 346 (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
347 347 (mnp->mnt_special[arlen] != '\0' &&
348 348 mnp->mnt_special[arlen] != '/'))) {
349 349 outside_altroot = B_TRUE;
350 350 }
351 351 /* use temporary buffer because new path might be longer */
352 352 (void) snprintf(tmppath, sizeof (tmppath), "%s%s",
353 353 mnp->mnt_special, path + len);
354 354 if ((len = resolvepath(tmppath, path, pathlen)) == -1)
355 355 break;
356 356 path[len] = '\0';
357 357 }
358 358 }
359 359
360 360 /*
361 361 * For a regular mount, check if a replacement lofs mount is needed because the
362 362 * referenced device is already mounted somewhere.
363 363 */
364 364 static int
365 365 check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
366 366 {
367 367 struct mnttab *mnp;
368 368 zone_fsopt_t *optptr, *onext;
369 369
370 370 /* This happens once per zoneadmd operation. */
371 371 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
372 372 return (-1);
373 373
374 374 /*
375 375 * If this special node isn't already in use, then it's ours alone;
376 376 * no need to worry about conflicting mounts.
377 377 */
378 378 for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
379 379 mnp++) {
380 380 if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
381 381 break;
382 382 }
383 383 if (mnp >= resolve_lofs_mnt_max)
384 384 return (0);
385 385
386 386 /*
387 387 * Convert this duplicate mount into a lofs mount.
388 388 */
389 389 (void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
390 390 sizeof (fsptr->zone_fs_special));
391 391 (void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
392 392 sizeof (fsptr->zone_fs_type));
393 393 fsptr->zone_fs_raw[0] = '\0';
394 394
395 395 /*
396 396 * Discard all but one of the original options and set that to be the
397 397 * same set of options used for inherit package directory resources.
398 398 */
399 399 optptr = fsptr->zone_fs_options;
400 400 if (optptr == NULL) {
401 401 optptr = malloc(sizeof (*optptr));
402 402 if (optptr == NULL) {
403 403 zerror(zlogp, B_TRUE, "cannot mount %s",
404 404 fsptr->zone_fs_dir);
405 405 return (-1);
406 406 }
407 407 } else {
408 408 while ((onext = optptr->zone_fsopt_next) != NULL) {
409 409 optptr->zone_fsopt_next = onext->zone_fsopt_next;
410 410 free(onext);
411 411 }
412 412 }
413 413 (void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS);
414 414 optptr->zone_fsopt_next = NULL;
415 415 fsptr->zone_fs_options = optptr;
416 416 return (0);
417 417 }
418 418
419 419 int
420 420 make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
421 421 uid_t userid, gid_t groupid)
422 422 {
423 423 char path[MAXPATHLEN];
424 424 struct stat st;
425 425
426 426 if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
427 427 sizeof (path)) {
428 428 zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
429 429 subdir);
430 430 return (-1);
431 431 }
432 432
433 433 if (lstat(path, &st) == 0) {
434 434 /*
435 435 * We don't check the file mode since presumably the zone
436 436 * administrator may have had good reason to change the mode,
437 437 * and we don't need to second guess him.
438 438 */
439 439 if (!S_ISDIR(st.st_mode)) {
440 440 if (is_system_labeled() &&
441 441 S_ISREG(st.st_mode)) {
442 442 /*
443 443 * The need to mount readonly copies of
444 444 * global zone /etc/ files is unique to
445 445 * Trusted Extensions.
446 446 */
447 447 if (strncmp(subdir, "/etc/",
448 448 strlen("/etc/")) != 0) {
449 449 zerror(zlogp, B_FALSE,
450 450 "%s is not in /etc", path);
451 451 return (-1);
452 452 }
453 453 } else {
454 454 zerror(zlogp, B_FALSE,
455 455 "%s is not a directory", path);
456 456 return (-1);
457 457 }
458 458 }
459 459 return (0);
460 460 }
461 461
462 462 if (mkdirp(path, mode) != 0) {
463 463 if (errno == EROFS)
464 464 zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
465 465 "a read-only file system in this local zone.\nMake "
466 466 "sure %s exists in the global zone.", path, subdir);
467 467 else
468 468 zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
469 469 return (-1);
470 470 }
471 471
472 472 (void) chown(path, userid, groupid);
473 473 return (0);
474 474 }
475 475
476 476 static void
477 477 free_remote_fstypes(char **types)
478 478 {
479 479 uint_t i;
480 480
481 481 if (types == NULL)
482 482 return;
483 483 for (i = 0; types[i] != NULL; i++)
484 484 free(types[i]);
485 485 free(types);
486 486 }
487 487
488 488 static char **
489 489 get_remote_fstypes(zlog_t *zlogp)
490 490 {
491 491 char **types = NULL;
492 492 FILE *fp;
493 493 char buf[MAXPATHLEN];
494 494 char fstype[MAXPATHLEN];
495 495 uint_t lines = 0;
496 496 uint_t i;
497 497
498 498 if ((fp = fopen(DFSTYPES, "r")) == NULL) {
499 499 zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
500 500 return (NULL);
501 501 }
502 502 /*
503 503 * Count the number of lines
504 504 */
505 505 while (fgets(buf, sizeof (buf), fp) != NULL)
506 506 lines++;
507 507 if (lines == 0) /* didn't read anything; empty file */
508 508 goto out;
509 509 rewind(fp);
510 510 /*
511 511 * Allocate enough space for a NULL-terminated array.
512 512 */
513 513 types = calloc(lines + 1, sizeof (char *));
514 514 if (types == NULL) {
515 515 zerror(zlogp, B_TRUE, "memory allocation failed");
516 516 goto out;
517 517 }
518 518 i = 0;
519 519 while (fgets(buf, sizeof (buf), fp) != NULL) {
520 520 /* LINTED - fstype is big enough to hold buf */
521 521 if (sscanf(buf, "%s", fstype) == 0) {
522 522 zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
523 523 free_remote_fstypes(types);
524 524 types = NULL;
525 525 goto out;
526 526 }
527 527 types[i] = strdup(fstype);
528 528 if (types[i] == NULL) {
529 529 zerror(zlogp, B_TRUE, "memory allocation failed");
530 530 free_remote_fstypes(types);
531 531 types = NULL;
532 532 goto out;
533 533 }
534 534 i++;
535 535 }
536 536 out:
537 537 (void) fclose(fp);
538 538 return (types);
539 539 }
540 540
541 541 static boolean_t
542 542 is_remote_fstype(const char *fstype, char *const *remote_fstypes)
543 543 {
544 544 uint_t i;
545 545
546 546 if (remote_fstypes == NULL)
547 547 return (B_FALSE);
548 548 for (i = 0; remote_fstypes[i] != NULL; i++) {
549 549 if (strcmp(remote_fstypes[i], fstype) == 0)
550 550 return (B_TRUE);
551 551 }
552 552 return (B_FALSE);
553 553 }
554 554
555 555 /*
556 556 * This converts a zone root path (normally of the form .../root) to a Live
557 557 * Upgrade scratch zone root (of the form .../lu).
558 558 */
559 559 static void
560 560 root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
561 561 {
562 562 assert(zone_isnative || zone_iscluster);
563 563
564 564 if (!isresolved && zonecfg_in_alt_root())
565 565 resolve_lofs(zlogp, zroot, zrootlen);
566 566 (void) strcpy(strrchr(zroot, '/') + 1, "lu");
567 567 }
568 568
569 569 /*
570 570 * The general strategy for unmounting filesystems is as follows:
571 571 *
572 572 * - Remote filesystems may be dead, and attempting to contact them as
573 573 * part of a regular unmount may hang forever; we want to always try to
574 574 * forcibly unmount such filesystems and only fall back to regular
575 575 * unmounts if the filesystem doesn't support forced unmounts.
576 576 *
577 577 * - We don't want to unnecessarily corrupt metadata on local
578 578 * filesystems (ie UFS), so we want to start off with graceful unmounts,
579 579 * and only escalate to doing forced unmounts if we get stuck.
580 580 *
581 581 * We start off walking backwards through the mount table. This doesn't
582 582 * give us strict ordering but ensures that we try to unmount submounts
583 583 * first. We thus limit the number of failed umount2(2) calls.
584 584 *
585 585 * The mechanism for determining if we're stuck is to count the number
586 586 * of failed unmounts each iteration through the mount table. This
587 587 * gives us an upper bound on the number of filesystems which remain
588 588 * mounted (autofs trigger nodes are dealt with separately). If at the
589 589 * end of one unmount+autofs_cleanup cycle we still have the same number
590 590 * of mounts that we started out with, we're stuck and try a forced
591 591 * unmount. If that fails (filesystem doesn't support forced unmounts)
592 592 * then we bail and are unable to teardown the zone. If it succeeds,
593 593 * we're no longer stuck so we continue with our policy of trying
594 594 * graceful mounts first.
595 595 *
596 596 * Zone must be down (ie, no processes or threads active).
597 597 */
598 598 static int
599 599 unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
600 600 {
601 601 int error = 0;
602 602 FILE *mnttab;
603 603 struct mnttab *mnts;
604 604 uint_t nmnt;
605 605 char zroot[MAXPATHLEN + 1];
606 606 size_t zrootlen;
607 607 uint_t oldcount = UINT_MAX;
608 608 boolean_t stuck = B_FALSE;
609 609 char **remote_fstypes = NULL;
610 610
611 611 if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
612 612 zerror(zlogp, B_FALSE, "unable to determine zone root");
613 613 return (-1);
614 614 }
615 615 if (unmount_cmd)
616 616 root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
617 617
618 618 (void) strcat(zroot, "/");
619 619 zrootlen = strlen(zroot);
620 620
621 621 /*
622 622 * For Trusted Extensions unmount each higher level zone's mount
623 623 * of our zone's /export/home
624 624 */
625 625 if (!unmount_cmd)
626 626 tsol_unmounts(zlogp, zone_name);
627 627
628 628 if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
629 629 zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
630 630 return (-1);
631 631 }
632 632 /*
633 633 * Use our hacky mntfs ioctl so we see everything, even mounts with
634 634 * MS_NOMNTTAB.
635 635 */
636 636 if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
637 637 zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
638 638 error++;
639 639 goto out;
640 640 }
641 641
642 642 /*
643 643 * Build the list of remote fstypes so we know which ones we
644 644 * should forcibly unmount.
645 645 */
646 646 remote_fstypes = get_remote_fstypes(zlogp);
647 647 for (; /* ever */; ) {
648 648 uint_t newcount = 0;
649 649 boolean_t unmounted;
650 650 struct mnttab *mnp;
651 651 char *path;
652 652 uint_t i;
653 653
654 654 mnts = NULL;
655 655 nmnt = 0;
656 656 /*
657 657 * MNTTAB gives us a way to walk through mounted
658 658 * filesystems; we need to be able to walk them in
659 659 * reverse order, so we build a list of all mounted
660 660 * filesystems.
661 661 */
662 662 if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
663 663 &nmnt) != 0) {
664 664 error++;
665 665 goto out;
666 666 }
667 667 for (i = 0; i < nmnt; i++) {
668 668 mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
669 669 path = mnp->mnt_mountp;
670 670 unmounted = B_FALSE;
671 671 /*
672 672 * Try forced unmount first for remote filesystems.
673 673 *
674 674 * Not all remote filesystems support forced unmounts,
675 675 * so if this fails (ENOTSUP) we'll continue on
676 676 * and try a regular unmount.
677 677 */
678 678 if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
679 679 if (umount2(path, MS_FORCE) == 0)
680 680 unmounted = B_TRUE;
681 681 }
682 682 /*
683 683 * Try forced unmount if we're stuck.
684 684 */
685 685 if (stuck) {
686 686 if (umount2(path, MS_FORCE) == 0) {
687 687 unmounted = B_TRUE;
688 688 stuck = B_FALSE;
689 689 } else {
690 690 /*
691 691 * The first failure indicates a
692 692 * mount we won't be able to get
693 693 * rid of automatically, so we
694 694 * bail.
695 695 */
696 696 error++;
697 697 zerror(zlogp, B_FALSE,
698 698 "unable to unmount '%s'", path);
699 699 free_mnttable(mnts, nmnt);
700 700 goto out;
701 701 }
702 702 }
703 703 /*
704 704 * Try regular unmounts for everything else.
705 705 */
706 706 if (!unmounted && umount2(path, 0) != 0)
707 707 newcount++;
708 708 }
709 709 free_mnttable(mnts, nmnt);
710 710
711 711 if (newcount == 0)
712 712 break;
713 713 if (newcount >= oldcount) {
714 714 /*
715 715 * Last round didn't unmount anything; we're stuck and
716 716 * should start trying forced unmounts.
717 717 */
718 718 stuck = B_TRUE;
719 719 }
720 720 oldcount = newcount;
721 721
722 722 /*
723 723 * Autofs doesn't let you unmount its trigger nodes from
724 724 * userland so we have to tell the kernel to cleanup for us.
725 725 */
726 726 if (autofs_cleanup(zoneid) != 0) {
727 727 zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
728 728 error++;
729 729 goto out;
730 730 }
731 731 }
732 732
733 733 out:
734 734 free_remote_fstypes(remote_fstypes);
735 735 (void) fclose(mnttab);
736 736 return (error ? -1 : 0);
737 737 }
738 738
739 739 static int
740 740 fs_compare(const void *m1, const void *m2)
741 741 {
742 742 struct zone_fstab *i = (struct zone_fstab *)m1;
743 743 struct zone_fstab *j = (struct zone_fstab *)m2;
744 744
745 745 return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
746 746 }
747 747
748 748 /*
749 749 * Fork and exec (and wait for) the mentioned binary with the provided
750 750 * arguments. Returns (-1) if something went wrong with fork(2) or exec(2),
751 751 * returns the exit status otherwise.
752 752 *
753 753 * If we were unable to exec the provided pathname (for whatever
754 754 * reason), we return the special token ZEXIT_EXEC. The current value
755 755 * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
756 756 * consumers of this function; any future consumers must make sure this
757 757 * remains the case.
758 758 */
759 759 static int
760 760 forkexec(zlog_t *zlogp, const char *path, char *const argv[])
761 761 {
762 762 pid_t child_pid;
763 763 int child_status = 0;
764 764
765 765 /*
766 766 * Do not let another thread localize a message while we are forking.
767 767 */
768 768 (void) mutex_lock(&msglock);
769 769 child_pid = fork();
770 770 (void) mutex_unlock(&msglock);
771 771 if (child_pid == -1) {
772 772 zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
773 773 return (-1);
774 774 } else if (child_pid == 0) {
775 775 closefrom(0);
776 776 /* redirect stdin, stdout & stderr to /dev/null */
777 777 (void) open("/dev/null", O_RDONLY); /* stdin */
778 778 (void) open("/dev/null", O_WRONLY); /* stdout */
779 779 (void) open("/dev/null", O_WRONLY); /* stderr */
780 780 (void) execv(path, argv);
781 781 /*
782 782 * Since we are in the child, there is no point calling zerror()
783 783 * since there is nobody waiting to consume it. So exit with a
784 784 * special code that the parent will recognize and call zerror()
785 785 * accordingly.
786 786 */
787 787
788 788 _exit(ZEXIT_EXEC);
789 789 } else {
790 790 (void) waitpid(child_pid, &child_status, 0);
791 791 }
792 792
793 793 if (WIFSIGNALED(child_status)) {
794 794 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
795 795 "signal %d", path, WTERMSIG(child_status));
796 796 return (-1);
|
↓ open down ↓ |
759 lines elided |
↑ open up ↑ |
797 797 }
798 798 assert(WIFEXITED(child_status));
799 799 if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
800 800 zerror(zlogp, B_FALSE, "failed to exec %s", path);
801 801 return (-1);
802 802 }
803 803 return (WEXITSTATUS(child_status));
804 804 }
805 805
806 806 static int
807 +isregfile(const char *path)
808 +{
809 + struct stat64 st;
810 +
811 + if (stat64(path, &st) == -1)
812 + return (-1);
813 +
814 + return (S_ISREG(st.st_mode));
815 +}
816 +
817 +static int
807 818 dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
808 819 {
809 820 char cmdbuf[MAXPATHLEN];
810 821 char *argv[4];
811 822 int status;
812 823
813 824 /*
814 825 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
815 826 * that would cost us an extra fork/exec without buying us anything.
816 827 */
817 828 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
818 829 >= sizeof (cmdbuf)) {
819 830 zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
820 831 return (-1);
821 832 }
822 833
834 + /*
835 + * If it doesn't exist, that's OK: we verified this previously.
836 + */
837 + if (isregfile(cmdbuf) == -1)
838 + return (0);
839 +
823 840 argv[0] = "fsck";
824 841 argv[1] = "-m";
825 842 argv[2] = (char *)rawdev;
826 843 argv[3] = NULL;
827 844
828 845 status = forkexec(zlogp, cmdbuf, argv);
829 846 if (status == 0 || status == -1)
830 847 return (status);
831 848 zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
832 849 "run fsck manually", rawdev, status);
833 850 return (-1);
834 851 }
835 852
836 853 static int
837 854 domount(zlog_t *zlogp, const char *fstype, const char *opts,
838 855 const char *special, const char *directory)
839 856 {
840 857 char cmdbuf[MAXPATHLEN];
841 858 char *argv[6];
842 859 int status;
843 860
844 861 /*
845 862 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
846 863 * that would cost us an extra fork/exec without buying us anything.
847 864 */
848 865 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
849 866 >= sizeof (cmdbuf)) {
850 867 zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
851 868 return (-1);
852 869 }
853 870 argv[0] = "mount";
854 871 if (opts[0] == '\0') {
855 872 argv[1] = (char *)special;
856 873 argv[2] = (char *)directory;
857 874 argv[3] = NULL;
858 875 } else {
859 876 argv[1] = "-o";
860 877 argv[2] = (char *)opts;
861 878 argv[3] = (char *)special;
862 879 argv[4] = (char *)directory;
863 880 argv[5] = NULL;
864 881 }
865 882
866 883 status = forkexec(zlogp, cmdbuf, argv);
867 884 if (status == 0 || status == -1)
868 885 return (status);
869 886 if (opts[0] == '\0')
870 887 zerror(zlogp, B_FALSE, "\"%s %s %s\" "
871 888 "failed with exit code %d",
872 889 cmdbuf, special, directory, status);
873 890 else
874 891 zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
875 892 "failed with exit code %d",
876 893 cmdbuf, opts, special, directory, status);
877 894 return (-1);
878 895 }
879 896
880 897 /*
881 898 * Check if a given mount point path exists.
882 899 * If it does, make sure it doesn't contain any symlinks.
883 900 * Note that if "leaf" is false we're checking an intermediate
884 901 * component of the mount point path, so it must be a directory.
885 902 * If "leaf" is true, then we're checking the entire mount point
886 903 * path, so the mount point itself can be anything aside from a
887 904 * symbolic link.
888 905 *
889 906 * If the path is invalid then a negative value is returned. If the
890 907 * path exists and is a valid mount point path then 0 is returned.
891 908 * If the path doesn't exist return a positive value.
892 909 */
893 910 static int
894 911 valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
895 912 {
896 913 struct stat statbuf;
897 914 char respath[MAXPATHLEN];
898 915 int res;
899 916
900 917 if (lstat(path, &statbuf) != 0) {
901 918 if (errno == ENOENT)
902 919 return (1);
903 920 zerror(zlogp, B_TRUE, "can't stat %s", path);
904 921 return (-1);
905 922 }
906 923 if (S_ISLNK(statbuf.st_mode)) {
907 924 zerror(zlogp, B_FALSE, "%s is a symlink", path);
908 925 return (-1);
909 926 }
910 927 if (!leaf && !S_ISDIR(statbuf.st_mode)) {
911 928 zerror(zlogp, B_FALSE, "%s is not a directory", path);
912 929 return (-1);
913 930 }
914 931 if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
915 932 zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
916 933 return (-1);
917 934 }
918 935 respath[res] = '\0';
919 936 if (strcmp(path, respath) != 0) {
920 937 /*
921 938 * We don't like ".."s, "."s, or "//"s throwing us off
922 939 */
923 940 zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
924 941 return (-1);
925 942 }
926 943 return (0);
927 944 }
928 945
929 946 /*
930 947 * Validate a mount point path. A valid mount point path is an
931 948 * absolute path that either doesn't exist, or, if it does exists it
932 949 * must be an absolute canonical path that doesn't have any symbolic
933 950 * links in it. The target of a mount point path can be any filesystem
934 951 * object. (Different filesystems can support different mount points,
935 952 * for example "lofs" and "mntfs" both support files and directories
936 953 * while "ufs" just supports directories.)
937 954 *
938 955 * If the path is invalid then a negative value is returned. If the
939 956 * path exists and is a valid mount point path then 0 is returned.
940 957 * If the path doesn't exist return a positive value.
941 958 */
942 959 int
943 960 valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
944 961 const char *dir, const char *fstype)
945 962 {
946 963 char abspath[MAXPATHLEN], *slashp, *slashp_next;
947 964 int rv;
948 965
949 966 /*
950 967 * Sanity check the target mount point path.
951 968 * It must be a non-null string that starts with a '/'.
952 969 */
953 970 if (dir[0] != '/') {
954 971 if (spec[0] == '\0') {
955 972 /*
956 973 * This must be an invalid ipd entry (see comments
957 974 * in mount_filesystems_ipdent()).
958 975 */
959 976 zerror(zlogp, B_FALSE,
960 977 "invalid inherit-pkg-dir entry: \"%s\"", dir);
961 978 } else {
962 979 /* Something went wrong. */
963 980 zerror(zlogp, B_FALSE, "invalid mount directory, "
964 981 "type: \"%s\", special: \"%s\", dir: \"%s\"",
965 982 fstype, spec, dir);
966 983 }
967 984 return (-1);
968 985 }
969 986
970 987 /*
971 988 * Join rootpath and dir. Make sure abspath ends with '/', this
972 989 * is added to all paths (even non-directory paths) to allow us
973 990 * to detect the end of paths below. If the path already ends
974 991 * in a '/', then that's ok too (although we'll fail the
975 992 * cannonical path check in valid_mount_point()).
976 993 */
977 994 if (snprintf(abspath, sizeof (abspath),
978 995 "%s%s/", rootpath, dir) >= sizeof (abspath)) {
979 996 zerror(zlogp, B_FALSE, "pathname %s%s is too long",
980 997 rootpath, dir);
981 998 return (-1);
982 999 }
983 1000
984 1001 /*
985 1002 * Starting with rootpath, verify the mount path one component
986 1003 * at a time. Continue until we've evaluated all of abspath.
987 1004 */
988 1005 slashp = &abspath[strlen(rootpath)];
989 1006 assert(*slashp == '/');
990 1007 do {
991 1008 slashp_next = strchr(slashp + 1, '/');
992 1009 *slashp = '\0';
993 1010 if (slashp_next != NULL) {
994 1011 /* This is an intermediary mount path component. */
995 1012 rv = valid_mount_point(zlogp, abspath, B_FALSE);
996 1013 } else {
997 1014 /* This is the last component of the mount path. */
998 1015 rv = valid_mount_point(zlogp, abspath, B_TRUE);
999 1016 }
1000 1017 if (rv < 0)
1001 1018 return (rv);
1002 1019 *slashp = '/';
1003 1020 } while ((slashp = slashp_next) != NULL);
1004 1021 return (rv);
1005 1022 }
1006 1023
1007 1024 static int
1008 1025 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
1009 1026 {
1010 1027 di_prof_t prof = arg;
1011 1028
1012 1029 if (name == NULL)
1013 1030 return (di_prof_add_dev(prof, match));
1014 1031 return (di_prof_add_map(prof, match, name));
1015 1032 }
1016 1033
1017 1034 static int
1018 1035 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
1019 1036 {
1020 1037 di_prof_t prof = arg;
1021 1038
1022 1039 return (di_prof_add_symlink(prof, source, target));
1023 1040 }
1024 1041
1025 1042 static int
1026 1043 get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
1027 1044 {
1028 1045 zone_dochandle_t handle;
1029 1046
1030 1047 if ((handle = zonecfg_init_handle()) == NULL) {
1031 1048 zerror(zlogp, B_TRUE, "getting zone configuration handle");
1032 1049 return (-1);
1033 1050 }
1034 1051 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1035 1052 zerror(zlogp, B_FALSE, "invalid configuration");
1036 1053 zonecfg_fini_handle(handle);
1037 1054 return (-1);
1038 1055 }
1039 1056 if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
1040 1057 zerror(zlogp, B_FALSE, "invalid ip-type configuration");
1041 1058 zonecfg_fini_handle(handle);
1042 1059 return (-1);
1043 1060 }
1044 1061 zonecfg_fini_handle(handle);
1045 1062 return (0);
1046 1063 }
1047 1064
1048 1065 /*
1049 1066 * Apply the standard lists of devices/symlinks/mappings and the user-specified
1050 1067 * list of devices (via zonecfg) to the /dev filesystem. The filesystem will
1051 1068 * use these as a profile/filter to determine what exists in /dev.
1052 1069 */
1053 1070 static int
1054 1071 mount_one_dev(zlog_t *zlogp, char *devpath)
1055 1072 {
1056 1073 char brand[MAXNAMELEN];
1057 1074 zone_dochandle_t handle = NULL;
1058 1075 brand_handle_t bh = NULL;
1059 1076 struct zone_devtab ztab;
1060 1077 di_prof_t prof = NULL;
1061 1078 int err;
1062 1079 int retval = -1;
1063 1080 zone_iptype_t iptype;
1064 1081 const char *curr_iptype;
1065 1082
1066 1083 if (di_prof_init(devpath, &prof)) {
1067 1084 zerror(zlogp, B_TRUE, "failed to initialize profile");
1068 1085 goto cleanup;
1069 1086 }
1070 1087
1071 1088 /* Get a handle to the brand info for this zone */
1072 1089 if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
1073 1090 (bh = brand_open(brand)) == NULL) {
1074 1091 zerror(zlogp, B_FALSE, "unable to determine zone brand");
1075 1092 goto cleanup;
1076 1093 }
1077 1094
1078 1095 if (get_iptype(zlogp, &iptype) < 0) {
1079 1096 zerror(zlogp, B_TRUE, "unable to determine ip-type");
1080 1097 goto cleanup;
1081 1098 }
1082 1099 switch (iptype) {
1083 1100 case ZS_SHARED:
1084 1101 curr_iptype = "shared";
1085 1102 break;
1086 1103 case ZS_EXCLUSIVE:
1087 1104 curr_iptype = "exclusive";
1088 1105 break;
1089 1106 }
1090 1107
1091 1108 if (brand_platform_iter_devices(bh, zone_name,
1092 1109 mount_one_dev_device_cb, prof, curr_iptype) != 0) {
1093 1110 zerror(zlogp, B_TRUE, "failed to add standard device");
1094 1111 goto cleanup;
1095 1112 }
1096 1113
1097 1114 if (brand_platform_iter_link(bh,
1098 1115 mount_one_dev_symlink_cb, prof) != 0) {
1099 1116 zerror(zlogp, B_TRUE, "failed to add standard symlink");
1100 1117 goto cleanup;
1101 1118 }
1102 1119
1103 1120 /* Add user-specified devices and directories */
1104 1121 if ((handle = zonecfg_init_handle()) == NULL) {
1105 1122 zerror(zlogp, B_FALSE, "can't initialize zone handle");
1106 1123 goto cleanup;
1107 1124 }
1108 1125 if (err = zonecfg_get_handle(zone_name, handle)) {
1109 1126 zerror(zlogp, B_FALSE, "can't get handle for zone "
1110 1127 "%s: %s", zone_name, zonecfg_strerror(err));
1111 1128 goto cleanup;
1112 1129 }
1113 1130 if (err = zonecfg_setdevent(handle)) {
1114 1131 zerror(zlogp, B_FALSE, "%s: %s", zone_name,
1115 1132 zonecfg_strerror(err));
1116 1133 goto cleanup;
1117 1134 }
1118 1135 while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
1119 1136 if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
1120 1137 zerror(zlogp, B_TRUE, "failed to add "
1121 1138 "user-specified device");
1122 1139 goto cleanup;
1123 1140 }
1124 1141 }
1125 1142 (void) zonecfg_enddevent(handle);
1126 1143
1127 1144 /* Send profile to kernel */
1128 1145 if (di_prof_commit(prof)) {
1129 1146 zerror(zlogp, B_TRUE, "failed to commit profile");
1130 1147 goto cleanup;
1131 1148 }
1132 1149
1133 1150 retval = 0;
1134 1151
1135 1152 cleanup:
1136 1153 if (bh != NULL)
1137 1154 brand_close(bh);
1138 1155 if (handle != NULL)
1139 1156 zonecfg_fini_handle(handle);
1140 1157 if (prof)
1141 1158 di_prof_fini(prof);
1142 1159 return (retval);
1143 1160 }
1144 1161
1145 1162 static int
1146 1163 mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath)
1147 1164 {
1148 1165 char path[MAXPATHLEN];
1149 1166 char specpath[MAXPATHLEN];
1150 1167 char optstr[MAX_MNTOPT_STR];
1151 1168 zone_fsopt_t *optptr;
1152 1169 int rv;
1153 1170
1154 1171 if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
1155 1172 fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
1156 1173 zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
1157 1174 rootpath, fsptr->zone_fs_dir);
1158 1175 return (-1);
1159 1176 } else if (rv > 0) {
1160 1177 /* The mount point path doesn't exist, create it now. */
1161 1178 if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
1162 1179 DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
1163 1180 DEFAULT_DIR_GROUP) != 0) {
1164 1181 zerror(zlogp, B_FALSE, "failed to create mount point");
1165 1182 return (-1);
1166 1183 }
1167 1184
1168 1185 /*
1169 1186 * Now this might seem weird, but we need to invoke
1170 1187 * valid_mount_path() again. Why? Because it checks
1171 1188 * to make sure that the mount point path is canonical,
1172 1189 * which it can only do if the path exists, so now that
1173 1190 * we've created the path we have to verify it again.
1174 1191 */
1175 1192 if ((rv = valid_mount_path(zlogp, rootpath,
1176 1193 fsptr->zone_fs_special, fsptr->zone_fs_dir,
1177 1194 fsptr->zone_fs_type)) < 0) {
1178 1195 zerror(zlogp, B_FALSE,
1179 1196 "%s%s is not a valid mount point",
1180 1197 rootpath, fsptr->zone_fs_dir);
1181 1198 return (-1);
1182 1199 }
1183 1200 }
1184 1201
1185 1202 (void) snprintf(path, sizeof (path), "%s%s", rootpath,
1186 1203 fsptr->zone_fs_dir);
1187 1204
1188 1205 if (strlen(fsptr->zone_fs_special) == 0) {
1189 1206 /*
1190 1207 * A zero-length special is how we distinguish IPDs from
1191 1208 * general-purpose FSs. Make sure it mounts from a place that
1192 1209 * can be seen via the alternate zone's root.
1193 1210 */
1194 1211 if (snprintf(specpath, sizeof (specpath), "%s%s",
1195 1212 zonecfg_get_root(), fsptr->zone_fs_dir) >=
1196 1213 sizeof (specpath)) {
1197 1214 zerror(zlogp, B_FALSE, "cannot mount %s: path too "
1198 1215 "long in alternate root", fsptr->zone_fs_dir);
1199 1216 return (-1);
1200 1217 }
1201 1218 if (zonecfg_in_alt_root())
1202 1219 resolve_lofs(zlogp, specpath, sizeof (specpath));
1203 1220 if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS,
1204 1221 specpath, path) != 0) {
1205 1222 zerror(zlogp, B_TRUE, "failed to loopback mount %s",
1206 1223 specpath);
1207 1224 return (-1);
1208 1225 }
1209 1226 return (0);
1210 1227 }
1211 1228
1212 1229 /*
1213 1230 * In general the strategy here is to do just as much verification as
1214 1231 * necessary to avoid crashing or otherwise doing something bad; if the
1215 1232 * administrator initiated the operation via zoneadm(1m), he'll get
1216 1233 * auto-verification which will let him know what's wrong. If he
1217 1234 * modifies the zone configuration of a running zone and doesn't attempt
1218 1235 * to verify that it's OK we won't crash but won't bother trying to be
1219 1236 * too helpful either. zoneadm verify is only a couple keystrokes away.
1220 1237 */
1221 1238 if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
1222 1239 zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
1223 1240 "invalid file-system type %s", fsptr->zone_fs_special,
1224 1241 fsptr->zone_fs_dir, fsptr->zone_fs_type);
1225 1242 return (-1);
1226 1243 }
1227 1244
1228 1245 /*
1229 1246 * If we're looking at an alternate root environment, then construct
1230 1247 * read-only loopback mounts as necessary. Note that any special
1231 1248 * paths for lofs zone mounts in an alternate root must have
1232 1249 * already been pre-pended with any alternate root path by the
1233 1250 * time we get here.
1234 1251 */
1235 1252 if (zonecfg_in_alt_root()) {
1236 1253 struct stat64 st;
1237 1254
1238 1255 if (stat64(fsptr->zone_fs_special, &st) != -1 &&
1239 1256 S_ISBLK(st.st_mode)) {
1240 1257 /*
1241 1258 * If we're going to mount a block device we need
1242 1259 * to check if that device is already mounted
1243 1260 * somewhere else, and if so, do a lofs mount
1244 1261 * of the device instead of a direct mount
1245 1262 */
1246 1263 if (check_lofs_needed(zlogp, fsptr) == -1)
1247 1264 return (-1);
1248 1265 } else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1249 1266 /*
1250 1267 * For lofs mounts, the special node is inside the
1251 1268 * alternate root. We need lofs resolution for
1252 1269 * this case in order to get at the underlying
1253 1270 * read-write path.
|
↓ open down ↓ |
421 lines elided |
↑ open up ↑ |
1254 1271 */
1255 1272 resolve_lofs(zlogp, fsptr->zone_fs_special,
1256 1273 sizeof (fsptr->zone_fs_special));
1257 1274 }
1258 1275 }
1259 1276
1260 1277 /*
1261 1278 * Run 'fsck -m' if there's a device to fsck.
1262 1279 */
1263 1280 if (fsptr->zone_fs_raw[0] != '\0' &&
1264 - dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0)
1281 + dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
1265 1282 return (-1);
1283 + } else if (isregfile(fsptr->zone_fs_special) == 1 &&
1284 + dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
1285 + return (-1);
1286 + }
1266 1287
1267 1288 /*
1268 1289 * Build up mount option string.
1269 1290 */
1270 1291 optstr[0] = '\0';
1271 1292 if (fsptr->zone_fs_options != NULL) {
1272 1293 (void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
1273 1294 sizeof (optstr));
1274 1295 for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
1275 1296 optptr != NULL; optptr = optptr->zone_fsopt_next) {
1276 1297 (void) strlcat(optstr, ",", sizeof (optstr));
1277 1298 (void) strlcat(optstr, optptr->zone_fsopt_opt,
1278 1299 sizeof (optstr));
1279 1300 }
1280 1301 }
1281 1302
1282 1303 if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
1283 1304 fsptr->zone_fs_special, path)) != 0)
1284 1305 return (rv);
1285 1306
1286 1307 /*
1287 1308 * The mount succeeded. If this was not a mount of /dev then
1288 1309 * we're done.
1289 1310 */
1290 1311 if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
1291 1312 return (0);
1292 1313
1293 1314 /*
1294 1315 * We just mounted an instance of a /dev filesystem, so now we
1295 1316 * need to configure it.
1296 1317 */
1297 1318 return (mount_one_dev(zlogp, path));
1298 1319 }
1299 1320
1300 1321 static void
1301 1322 free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
1302 1323 {
1303 1324 uint_t i;
1304 1325
1305 1326 if (fsarray == NULL)
1306 1327 return;
1307 1328 for (i = 0; i < nelem; i++)
1308 1329 zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
1309 1330 free(fsarray);
1310 1331 }
1311 1332
1312 1333 /*
1313 1334 * This function initiates the creation of a small Solaris Environment for
1314 1335 * scratch zone. The Environment creation process is split up into two
1315 1336 * functions(build_mounted_pre_var() and build_mounted_post_var()). It
1316 1337 * is done this way because:
1317 1338 * We need to have both /etc and /var in the root of the scratchzone.
1318 1339 * We loopback mount zone's own /etc and /var into the root of the
1319 1340 * scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
1320 1341 * need to delay the mount of /var till the zone's root gets populated.
1321 1342 * So mounting of localdirs[](/etc and /var) have been moved to the
1322 1343 * build_mounted_post_var() which gets called only after the zone
1323 1344 * specific filesystems are mounted.
1324 1345 *
1325 1346 * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
1326 1347 * does not loopback mount the zone's own /etc and /var into the root of the
1327 1348 * scratch zone.
1328 1349 */
1329 1350 static boolean_t
1330 1351 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
1331 1352 size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1332 1353 {
1333 1354 char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1334 1355 const char **cpp;
1335 1356 static const char *mkdirs[] = {
1336 1357 "/system", "/system/contract", "/system/object", "/proc",
1337 1358 "/dev", "/tmp", "/a", NULL
1338 1359 };
1339 1360 char *altstr;
1340 1361 FILE *fp;
1341 1362 uuid_t uuid;
1342 1363
1343 1364 assert(zone_isnative || zone_iscluster);
1344 1365
1345 1366 resolve_lofs(zlogp, rootpath, rootlen);
1346 1367 (void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
1347 1368 resolve_lofs(zlogp, luroot, lurootlen);
1348 1369 (void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1349 1370 (void) symlink("./usr/bin", tmp);
1350 1371
1351 1372 /*
1352 1373 * These are mostly special mount points; not handled here. (See
1353 1374 * zone_mount_early.)
1354 1375 */
1355 1376 for (cpp = mkdirs; *cpp != NULL; cpp++) {
1356 1377 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1357 1378 if (mkdir(tmp, 0755) != 0) {
1358 1379 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1359 1380 return (B_FALSE);
1360 1381 }
1361 1382 }
1362 1383 /*
1363 1384 * This is here to support lucopy. If there's an instance of this same
1364 1385 * zone on the current running system, then we mount its root up as
1365 1386 * read-only inside the scratch zone.
1366 1387 */
1367 1388 (void) zonecfg_get_uuid(zone_name, uuid);
1368 1389 altstr = strdup(zonecfg_get_root());
1369 1390 if (altstr == NULL) {
1370 1391 zerror(zlogp, B_TRUE, "memory allocation failed");
1371 1392 return (B_FALSE);
1372 1393 }
1373 1394 zonecfg_set_root("");
1374 1395 (void) strlcpy(tmp, zone_name, sizeof (tmp));
1375 1396 (void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1376 1397 if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1377 1398 strcmp(fromdir, rootpath) != 0) {
1378 1399 (void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1379 1400 if (mkdir(tmp, 0755) != 0) {
1380 1401 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1381 1402 return (B_FALSE);
1382 1403 }
1383 1404 if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir,
1384 1405 tmp) != 0) {
1385 1406 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1386 1407 fromdir);
1387 1408 return (B_FALSE);
1388 1409 }
1389 1410 }
1390 1411 zonecfg_set_root(altstr);
1391 1412 free(altstr);
1392 1413
1393 1414 if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1394 1415 zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1395 1416 return (B_FALSE);
1396 1417 }
1397 1418 (void) ftruncate(fileno(fp), 0);
1398 1419 if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1399 1420 zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1400 1421 }
1401 1422 zonecfg_close_scratch(fp);
1402 1423 (void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1403 1424 if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1404 1425 return (B_FALSE);
1405 1426 (void) strlcpy(rootpath, tmp, rootlen);
1406 1427 return (B_TRUE);
1407 1428 }
1408 1429
1409 1430
1410 1431 static boolean_t
1411 1432 build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
1412 1433 const char *luroot)
1413 1434 {
1414 1435 char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1415 1436 const char **cpp;
1416 1437 const char **loopdirs;
1417 1438 const char **tmpdirs;
1418 1439 static const char *localdirs[] = {
1419 1440 "/etc", "/var", NULL
1420 1441 };
1421 1442 static const char *scr_loopdirs[] = {
1422 1443 "/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1423 1444 "/usr", NULL
1424 1445 };
1425 1446 static const char *upd_loopdirs[] = {
1426 1447 "/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
1427 1448 "/usr", "/var", NULL
1428 1449 };
1429 1450 static const char *scr_tmpdirs[] = {
1430 1451 "/tmp", "/var/run", NULL
1431 1452 };
1432 1453 static const char *upd_tmpdirs[] = {
1433 1454 "/tmp", "/var/run", "/var/tmp", NULL
1434 1455 };
1435 1456 struct stat st;
1436 1457
1437 1458 if (mount_cmd == Z_MNT_SCRATCH) {
1438 1459 /*
1439 1460 * These are mounted read-write from the zone undergoing
1440 1461 * upgrade. We must be careful not to 'leak' things from the
1441 1462 * main system into the zone, and this accomplishes that goal.
1442 1463 */
1443 1464 for (cpp = localdirs; *cpp != NULL; cpp++) {
1444 1465 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
1445 1466 *cpp);
1446 1467 (void) snprintf(fromdir, sizeof (fromdir), "%s%s",
1447 1468 rootpath, *cpp);
1448 1469 if (mkdir(tmp, 0755) != 0) {
1449 1470 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1450 1471 return (B_FALSE);
1451 1472 }
1452 1473 if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
1453 1474 != 0) {
1454 1475 zerror(zlogp, B_TRUE, "cannot mount %s on %s",
1455 1476 tmp, *cpp);
1456 1477 return (B_FALSE);
1457 1478 }
1458 1479 }
1459 1480 }
1460 1481
1461 1482 if (mount_cmd == Z_MNT_UPDATE)
1462 1483 loopdirs = upd_loopdirs;
1463 1484 else
1464 1485 loopdirs = scr_loopdirs;
1465 1486
1466 1487 /*
1467 1488 * These are things mounted read-only from the running system because
1468 1489 * they contain binaries that must match system.
1469 1490 */
1470 1491 for (cpp = loopdirs; *cpp != NULL; cpp++) {
1471 1492 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1472 1493 if (mkdir(tmp, 0755) != 0) {
1473 1494 if (errno != EEXIST) {
1474 1495 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1475 1496 return (B_FALSE);
1476 1497 }
1477 1498 if (lstat(tmp, &st) != 0) {
1478 1499 zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1479 1500 return (B_FALSE);
1480 1501 }
1481 1502 /*
1482 1503 * Ignore any non-directories encountered. These are
1483 1504 * things that have been converted into symlinks
1484 1505 * (/etc/fs and /etc/lib) and no longer need a lofs
1485 1506 * fixup.
1486 1507 */
1487 1508 if (!S_ISDIR(st.st_mode))
1488 1509 continue;
1489 1510 }
1490 1511 if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp,
1491 1512 tmp) != 0) {
1492 1513 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1493 1514 *cpp);
1494 1515 return (B_FALSE);
1495 1516 }
1496 1517 }
1497 1518
1498 1519 if (mount_cmd == Z_MNT_UPDATE)
1499 1520 tmpdirs = upd_tmpdirs;
1500 1521 else
1501 1522 tmpdirs = scr_tmpdirs;
1502 1523
1503 1524 /*
1504 1525 * These are things with tmpfs mounted inside.
1505 1526 */
1506 1527 for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1507 1528 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1508 1529 if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
1509 1530 errno != EEXIST) {
1510 1531 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1511 1532 return (B_FALSE);
1512 1533 }
1513 1534
1514 1535 /*
1515 1536 * We could set the mode for /tmp when we do the mkdir but
1516 1537 * since that can be modified by the umask we will just set
1517 1538 * the correct mode for /tmp now.
1518 1539 */
1519 1540 if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
1520 1541 zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
1521 1542 return (B_FALSE);
1522 1543 }
1523 1544
1524 1545 if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1525 1546 zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1526 1547 return (B_FALSE);
1527 1548 }
1528 1549 }
1529 1550 return (B_TRUE);
1530 1551 }
1531 1552
1532 1553 typedef struct plat_gmount_cb_data {
1533 1554 zlog_t *pgcd_zlogp;
1534 1555 struct zone_fstab **pgcd_fs_tab;
1535 1556 int *pgcd_num_fs;
1536 1557 } plat_gmount_cb_data_t;
1537 1558
1538 1559 /*
1539 1560 * plat_gmount_cb() is a callback function invoked by libbrand to iterate
1540 1561 * through all global brand platform mounts.
1541 1562 */
1542 1563 int
1543 1564 plat_gmount_cb(void *data, const char *spec, const char *dir,
1544 1565 const char *fstype, const char *opt)
1545 1566 {
1546 1567 plat_gmount_cb_data_t *cp = data;
1547 1568 zlog_t *zlogp = cp->pgcd_zlogp;
1548 1569 struct zone_fstab *fs_ptr = *cp->pgcd_fs_tab;
1549 1570 int num_fs = *cp->pgcd_num_fs;
1550 1571 struct zone_fstab *fsp, *tmp_ptr;
1551 1572
1552 1573 num_fs++;
1553 1574 if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
1554 1575 zerror(zlogp, B_TRUE, "memory allocation failed");
1555 1576 return (-1);
1556 1577 }
1557 1578
1558 1579 fs_ptr = tmp_ptr;
1559 1580 fsp = &fs_ptr[num_fs - 1];
1560 1581
1561 1582 /* update the callback struct passed in */
1562 1583 *cp->pgcd_fs_tab = fs_ptr;
1563 1584 *cp->pgcd_num_fs = num_fs;
1564 1585
1565 1586 fsp->zone_fs_raw[0] = '\0';
1566 1587 (void) strlcpy(fsp->zone_fs_special, spec,
1567 1588 sizeof (fsp->zone_fs_special));
1568 1589 (void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
1569 1590 (void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
1570 1591 fsp->zone_fs_options = NULL;
1571 1592 if ((opt != NULL) &&
1572 1593 (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
1573 1594 zerror(zlogp, B_FALSE, "error adding property");
1574 1595 return (-1);
1575 1596 }
1576 1597
1577 1598 return (0);
1578 1599 }
1579 1600
1580 1601 static int
1581 1602 mount_filesystems_ipdent(zone_dochandle_t handle, zlog_t *zlogp,
1582 1603 struct zone_fstab **fs_tabp, int *num_fsp)
1583 1604 {
1584 1605 struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
1585 1606 int num_fs;
1586 1607
1587 1608 num_fs = *num_fsp;
1588 1609 fs_ptr = *fs_tabp;
1589 1610
1590 1611 if (zonecfg_setipdent(handle) != Z_OK) {
1591 1612 zerror(zlogp, B_FALSE, "invalid configuration");
1592 1613 return (-1);
1593 1614 }
1594 1615 while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
1595 1616 num_fs++;
1596 1617 if ((tmp_ptr = realloc(fs_ptr,
1597 1618 num_fs * sizeof (*tmp_ptr))) == NULL) {
1598 1619 zerror(zlogp, B_TRUE, "memory allocation failed");
1599 1620 (void) zonecfg_endipdent(handle);
1600 1621 return (-1);
1601 1622 }
1602 1623
1603 1624 /* update the pointers passed in */
1604 1625 *fs_tabp = tmp_ptr;
1605 1626 *num_fsp = num_fs;
1606 1627
1607 1628 /*
1608 1629 * IPDs logically only have a mount point; all other properties
1609 1630 * are implied.
1610 1631 */
1611 1632 fs_ptr = tmp_ptr;
1612 1633 fsp = &fs_ptr[num_fs - 1];
1613 1634 (void) strlcpy(fsp->zone_fs_dir,
1614 1635 fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
1615 1636 fsp->zone_fs_special[0] = '\0';
1616 1637 fsp->zone_fs_raw[0] = '\0';
1617 1638 fsp->zone_fs_type[0] = '\0';
1618 1639 fsp->zone_fs_options = NULL;
1619 1640 }
1620 1641 (void) zonecfg_endipdent(handle);
1621 1642 return (0);
1622 1643 }
1623 1644
1624 1645 static int
1625 1646 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
1626 1647 struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
1627 1648 {
1628 1649 struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
1629 1650 int num_fs;
1630 1651
1631 1652 num_fs = *num_fsp;
1632 1653 fs_ptr = *fs_tabp;
1633 1654
1634 1655 if (zonecfg_setfsent(handle) != Z_OK) {
1635 1656 zerror(zlogp, B_FALSE, "invalid configuration");
1636 1657 return (-1);
1637 1658 }
1638 1659 while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
1639 1660 /*
1640 1661 * ZFS filesystems will not be accessible under an alternate
1641 1662 * root, since the pool will not be known. Ignore them in this
1642 1663 * case.
1643 1664 */
1644 1665 if (ALT_MOUNT(mount_cmd) &&
1645 1666 strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
1646 1667 continue;
1647 1668
1648 1669 num_fs++;
1649 1670 if ((tmp_ptr = realloc(fs_ptr,
1650 1671 num_fs * sizeof (*tmp_ptr))) == NULL) {
1651 1672 zerror(zlogp, B_TRUE, "memory allocation failed");
1652 1673 (void) zonecfg_endfsent(handle);
1653 1674 return (-1);
1654 1675 }
1655 1676 /* update the pointers passed in */
1656 1677 *fs_tabp = tmp_ptr;
1657 1678 *num_fsp = num_fs;
1658 1679
1659 1680 fs_ptr = tmp_ptr;
1660 1681 fsp = &fs_ptr[num_fs - 1];
1661 1682 (void) strlcpy(fsp->zone_fs_dir,
1662 1683 fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
1663 1684 (void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
1664 1685 sizeof (fsp->zone_fs_raw));
1665 1686 (void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
1666 1687 sizeof (fsp->zone_fs_type));
1667 1688 fsp->zone_fs_options = fstab.zone_fs_options;
1668 1689
1669 1690 /*
1670 1691 * For all lofs mounts, make sure that the 'special'
1671 1692 * entry points inside the alternate root. The
1672 1693 * source path for a lofs mount in a given zone needs
1673 1694 * to be relative to the root of the boot environment
1674 1695 * that contains the zone. Note that we don't do this
1675 1696 * for non-lofs mounts since they will have a device
1676 1697 * as a backing store and device paths must always be
1677 1698 * specified relative to the current boot environment.
1678 1699 */
1679 1700 fsp->zone_fs_special[0] = '\0';
1680 1701 if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
1681 1702 (void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
1682 1703 sizeof (fsp->zone_fs_special));
1683 1704 }
1684 1705 (void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
1685 1706 sizeof (fsp->zone_fs_special));
1686 1707 }
1687 1708 (void) zonecfg_endfsent(handle);
1688 1709 return (0);
1689 1710 }
1690 1711
1691 1712 static int
1692 1713 mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
1693 1714 {
1694 1715 char rootpath[MAXPATHLEN];
1695 1716 char zonepath[MAXPATHLEN];
1696 1717 char brand[MAXNAMELEN];
1697 1718 char luroot[MAXPATHLEN];
1698 1719 int i, num_fs = 0;
1699 1720 struct zone_fstab *fs_ptr = NULL;
1700 1721 zone_dochandle_t handle = NULL;
1701 1722 zone_state_t zstate;
1702 1723 brand_handle_t bh;
1703 1724 plat_gmount_cb_data_t cb;
1704 1725
1705 1726 if (zone_get_state(zone_name, &zstate) != Z_OK ||
1706 1727 (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
1707 1728 zerror(zlogp, B_FALSE,
1708 1729 "zone must be in '%s' or '%s' state to mount file-systems",
1709 1730 zone_state_str(ZONE_STATE_READY),
1710 1731 zone_state_str(ZONE_STATE_MOUNTED));
1711 1732 goto bad;
1712 1733 }
1713 1734
1714 1735 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
1715 1736 zerror(zlogp, B_TRUE, "unable to determine zone path");
1716 1737 goto bad;
1717 1738 }
1718 1739
1719 1740 if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
1720 1741 zerror(zlogp, B_TRUE, "unable to determine zone root");
1721 1742 goto bad;
1722 1743 }
1723 1744
1724 1745 if ((handle = zonecfg_init_handle()) == NULL) {
1725 1746 zerror(zlogp, B_TRUE, "getting zone configuration handle");
1726 1747 goto bad;
1727 1748 }
1728 1749 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
1729 1750 zonecfg_setfsent(handle) != Z_OK) {
1730 1751 zerror(zlogp, B_FALSE, "invalid configuration");
1731 1752 goto bad;
1732 1753 }
1733 1754
1734 1755 /* Get a handle to the brand info for this zone */
1735 1756 if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
1736 1757 (bh = brand_open(brand)) == NULL) {
1737 1758 zerror(zlogp, B_FALSE, "unable to determine zone brand");
1738 1759 zonecfg_fini_handle(handle);
1739 1760 return (-1);
1740 1761 }
1741 1762
1742 1763 /*
1743 1764 * Get the list of global filesystems to mount from the brand
1744 1765 * configuration.
1745 1766 */
1746 1767 cb.pgcd_zlogp = zlogp;
1747 1768 cb.pgcd_fs_tab = &fs_ptr;
1748 1769 cb.pgcd_num_fs = &num_fs;
1749 1770 if (brand_platform_iter_gmounts(bh, zonepath,
1750 1771 plat_gmount_cb, &cb) != 0) {
1751 1772 zerror(zlogp, B_FALSE, "unable to mount filesystems");
1752 1773 brand_close(bh);
1753 1774 zonecfg_fini_handle(handle);
1754 1775 return (-1);
1755 1776 }
1756 1777 brand_close(bh);
1757 1778
1758 1779 /*
1759 1780 * Iterate through the rest of the filesystems, first the IPDs, then
1760 1781 * the general FSs. Sort them all, then mount them in sorted order.
1761 1782 * This is to make sure the higher level directories (e.g., /usr)
1762 1783 * get mounted before any beneath them (e.g., /usr/local).
1763 1784 */
1764 1785 if (mount_filesystems_ipdent(handle, zlogp, &fs_ptr, &num_fs) != 0)
1765 1786 goto bad;
1766 1787
1767 1788 if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
1768 1789 mount_cmd) != 0)
1769 1790 goto bad;
1770 1791
1771 1792 zonecfg_fini_handle(handle);
1772 1793 handle = NULL;
1773 1794
1774 1795 /*
1775 1796 * Normally when we mount a zone all the zone filesystems
1776 1797 * get mounted relative to rootpath, which is usually
1777 1798 * <zonepath>/root. But when mounting a zone for administration
1778 1799 * purposes via the zone "mount" state, build_mounted_pre_var()
1779 1800 * updates rootpath to be <zonepath>/lu/a so we'll mount all
1780 1801 * the zones filesystems there instead.
1781 1802 *
1782 1803 * build_mounted_pre_var() and build_mounted_post_var() will
1783 1804 * also do some extra work to create directories and lofs mount
1784 1805 * a bunch of global zone file system paths into <zonepath>/lu.
1785 1806 *
1786 1807 * This allows us to be able to enter the zone (now rooted at
1787 1808 * <zonepath>/lu) and run the upgrade/patch tools that are in the
1788 1809 * global zone and have them upgrade the to-be-modified zone's
1789 1810 * files mounted on /a. (Which mirrors the existing standard
1790 1811 * upgrade environment.)
1791 1812 *
1792 1813 * There is of course one catch. When doing the upgrade
1793 1814 * we need <zoneroot>/lu/dev to be the /dev filesystem
1794 1815 * for the zone and we don't want to have any /dev filesystem
1795 1816 * mounted at <zoneroot>/lu/a/dev. Since /dev is specified
1796 1817 * as a normal zone filesystem by default we'll try to mount
1797 1818 * it at <zoneroot>/lu/a/dev, so we have to detect this
1798 1819 * case and instead mount it at <zoneroot>/lu/dev.
1799 1820 *
1800 1821 * All this work is done in three phases:
1801 1822 * 1) Create and populate lu directory (build_mounted_pre_var()).
1802 1823 * 2) Mount the required filesystems as per the zone configuration.
1803 1824 * 3) Set up the rest of the scratch zone environment
1804 1825 * (build_mounted_post_var()).
1805 1826 */
1806 1827 if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
1807 1828 rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1808 1829 goto bad;
1809 1830
1810 1831 qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
1811 1832
1812 1833 for (i = 0; i < num_fs; i++) {
1813 1834 if (ALT_MOUNT(mount_cmd) &&
1814 1835 strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
1815 1836 size_t slen = strlen(rootpath) - 2;
1816 1837
1817 1838 /*
1818 1839 * By default we'll try to mount /dev as /a/dev
1819 1840 * but /dev is special and always goes at the top
1820 1841 * so strip the trailing '/a' from the rootpath.
1821 1842 */
1822 1843 assert(zone_isnative || zone_iscluster);
1823 1844 assert(strcmp(&rootpath[slen], "/a") == 0);
1824 1845 rootpath[slen] = '\0';
1825 1846 if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
1826 1847 goto bad;
1827 1848 rootpath[slen] = '/';
1828 1849 continue;
1829 1850 }
1830 1851 if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
1831 1852 goto bad;
1832 1853 }
1833 1854 if (ALT_MOUNT(mount_cmd) &&
1834 1855 !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
1835 1856 goto bad;
1836 1857
1837 1858 /*
1838 1859 * For Trusted Extensions cross-mount each lower level /export/home
1839 1860 */
1840 1861 if (mount_cmd == Z_MNT_BOOT &&
1841 1862 tsol_mounts(zlogp, zone_name, rootpath) != 0)
1842 1863 goto bad;
1843 1864
1844 1865 free_fs_data(fs_ptr, num_fs);
1845 1866
1846 1867 /*
1847 1868 * Everything looks fine.
1848 1869 */
1849 1870 return (0);
1850 1871
1851 1872 bad:
1852 1873 if (handle != NULL)
1853 1874 zonecfg_fini_handle(handle);
1854 1875 free_fs_data(fs_ptr, num_fs);
1855 1876 return (-1);
1856 1877 }
1857 1878
1858 1879 /* caller makes sure neither parameter is NULL */
1859 1880 static int
1860 1881 addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
1861 1882 {
1862 1883 int prefixlen;
1863 1884
1864 1885 prefixlen = atoi(prefixstr);
1865 1886 if (prefixlen < 0 || prefixlen > maxprefixlen)
1866 1887 return (1);
1867 1888 while (prefixlen > 0) {
1868 1889 if (prefixlen >= 8) {
1869 1890 *maskstr++ = 0xFF;
1870 1891 prefixlen -= 8;
1871 1892 continue;
1872 1893 }
1873 1894 *maskstr |= 1 << (8 - prefixlen);
1874 1895 prefixlen--;
1875 1896 }
1876 1897 return (0);
1877 1898 }
1878 1899
1879 1900 /*
1880 1901 * Tear down all interfaces belonging to the given zone. This should
1881 1902 * be called with the zone in a state other than "running", so that
1882 1903 * interfaces can't be assigned to the zone after this returns.
1883 1904 *
1884 1905 * If anything goes wrong, log an error message and return an error.
1885 1906 */
1886 1907 static int
1887 1908 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
1888 1909 {
1889 1910 struct lifnum lifn;
1890 1911 struct lifconf lifc;
1891 1912 struct lifreq *lifrp, lifrl;
1892 1913 int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
1893 1914 int num_ifs, s, i, ret_code = 0;
1894 1915 uint_t bufsize;
1895 1916 char *buf = NULL;
1896 1917
1897 1918 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1898 1919 zerror(zlogp, B_TRUE, "could not get socket");
1899 1920 ret_code = -1;
1900 1921 goto bad;
1901 1922 }
1902 1923 lifn.lifn_family = AF_UNSPEC;
1903 1924 lifn.lifn_flags = (int)lifc_flags;
1904 1925 if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
1905 1926 zerror(zlogp, B_TRUE,
1906 1927 "could not determine number of network interfaces");
1907 1928 ret_code = -1;
1908 1929 goto bad;
1909 1930 }
1910 1931 num_ifs = lifn.lifn_count;
1911 1932 bufsize = num_ifs * sizeof (struct lifreq);
1912 1933 if ((buf = malloc(bufsize)) == NULL) {
1913 1934 zerror(zlogp, B_TRUE, "memory allocation failed");
1914 1935 ret_code = -1;
1915 1936 goto bad;
1916 1937 }
1917 1938 lifc.lifc_family = AF_UNSPEC;
1918 1939 lifc.lifc_flags = (int)lifc_flags;
1919 1940 lifc.lifc_len = bufsize;
1920 1941 lifc.lifc_buf = buf;
1921 1942 if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1922 1943 zerror(zlogp, B_TRUE, "could not get configured network "
1923 1944 "interfaces");
1924 1945 ret_code = -1;
1925 1946 goto bad;
1926 1947 }
1927 1948 lifrp = lifc.lifc_req;
1928 1949 for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
1929 1950 (void) close(s);
1930 1951 if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
1931 1952 0) {
1932 1953 zerror(zlogp, B_TRUE, "%s: could not get socket",
1933 1954 lifrl.lifr_name);
1934 1955 ret_code = -1;
1935 1956 continue;
1936 1957 }
1937 1958 (void) memset(&lifrl, 0, sizeof (lifrl));
1938 1959 (void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
1939 1960 sizeof (lifrl.lifr_name));
1940 1961 if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1941 1962 if (errno == ENXIO)
1942 1963 /*
1943 1964 * Interface may have been removed by admin or
1944 1965 * another zone halting.
1945 1966 */
1946 1967 continue;
1947 1968 zerror(zlogp, B_TRUE,
1948 1969 "%s: could not determine the zone to which this "
1949 1970 "network interface is bound", lifrl.lifr_name);
1950 1971 ret_code = -1;
1951 1972 continue;
1952 1973 }
1953 1974 if (lifrl.lifr_zoneid == zone_id) {
1954 1975 if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
1955 1976 zerror(zlogp, B_TRUE,
1956 1977 "%s: could not remove network interface",
1957 1978 lifrl.lifr_name);
1958 1979 ret_code = -1;
1959 1980 continue;
1960 1981 }
1961 1982 }
1962 1983 }
1963 1984 bad:
1964 1985 if (s > 0)
1965 1986 (void) close(s);
1966 1987 if (buf)
1967 1988 free(buf);
1968 1989 return (ret_code);
1969 1990 }
1970 1991
1971 1992 static union sockunion {
1972 1993 struct sockaddr sa;
1973 1994 struct sockaddr_in sin;
1974 1995 struct sockaddr_dl sdl;
1975 1996 struct sockaddr_in6 sin6;
1976 1997 } so_dst, so_ifp;
1977 1998
1978 1999 static struct {
1979 2000 struct rt_msghdr hdr;
1980 2001 char space[512];
1981 2002 } rtmsg;
1982 2003
1983 2004 static int
1984 2005 salen(struct sockaddr *sa)
1985 2006 {
1986 2007 switch (sa->sa_family) {
1987 2008 case AF_INET:
1988 2009 return (sizeof (struct sockaddr_in));
1989 2010 case AF_LINK:
1990 2011 return (sizeof (struct sockaddr_dl));
1991 2012 case AF_INET6:
1992 2013 return (sizeof (struct sockaddr_in6));
1993 2014 default:
1994 2015 return (sizeof (struct sockaddr));
1995 2016 }
1996 2017 }
1997 2018
1998 2019 #define ROUNDUP_LONG(a) \
1999 2020 ((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
2000 2021
2001 2022 /*
2002 2023 * Look up which zone is using a given IP address. The address in question
2003 2024 * is expected to have been stuffed into the structure to which lifr points
2004 2025 * via a previous SIOCGLIFADDR ioctl().
2005 2026 *
2006 2027 * This is done using black router socket magic.
2007 2028 *
2008 2029 * Return the name of the zone on success or NULL on failure.
2009 2030 *
2010 2031 * This is a lot of code for a simple task; a new ioctl request to take care
2011 2032 * of this might be a useful RFE.
2012 2033 */
2013 2034
2014 2035 static char *
2015 2036 who_is_using(zlog_t *zlogp, struct lifreq *lifr)
2016 2037 {
2017 2038 static char answer[ZONENAME_MAX];
2018 2039 pid_t pid;
2019 2040 int s, rlen, l, i;
2020 2041 char *cp = rtmsg.space;
2021 2042 struct sockaddr_dl *ifp = NULL;
2022 2043 struct sockaddr *sa;
2023 2044 char save_if_name[LIFNAMSIZ];
2024 2045
2025 2046 answer[0] = '\0';
2026 2047
2027 2048 pid = getpid();
2028 2049 if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
2029 2050 zerror(zlogp, B_TRUE, "could not get routing socket");
2030 2051 return (NULL);
2031 2052 }
2032 2053
2033 2054 if (lifr->lifr_addr.ss_family == AF_INET) {
2034 2055 struct sockaddr_in *sin4;
2035 2056
2036 2057 so_dst.sa.sa_family = AF_INET;
2037 2058 sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
2038 2059 so_dst.sin.sin_addr = sin4->sin_addr;
2039 2060 } else {
2040 2061 struct sockaddr_in6 *sin6;
2041 2062
2042 2063 so_dst.sa.sa_family = AF_INET6;
2043 2064 sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
2044 2065 so_dst.sin6.sin6_addr = sin6->sin6_addr;
2045 2066 }
2046 2067
2047 2068 so_ifp.sa.sa_family = AF_LINK;
2048 2069
2049 2070 (void) memset(&rtmsg, 0, sizeof (rtmsg));
2050 2071 rtmsg.hdr.rtm_type = RTM_GET;
2051 2072 rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
2052 2073 rtmsg.hdr.rtm_version = RTM_VERSION;
2053 2074 rtmsg.hdr.rtm_seq = ++rts_seqno;
2054 2075 rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
2055 2076
2056 2077 l = ROUNDUP_LONG(salen(&so_dst.sa));
2057 2078 (void) memmove(cp, &(so_dst), l);
2058 2079 cp += l;
2059 2080 l = ROUNDUP_LONG(salen(&so_ifp.sa));
2060 2081 (void) memmove(cp, &(so_ifp), l);
2061 2082 cp += l;
2062 2083
2063 2084 rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
2064 2085
2065 2086 if ((rlen = write(s, &rtmsg, l)) < 0) {
2066 2087 zerror(zlogp, B_TRUE, "writing to routing socket");
2067 2088 return (NULL);
2068 2089 } else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
2069 2090 zerror(zlogp, B_TRUE,
2070 2091 "write to routing socket got only %d for len\n", rlen);
2071 2092 return (NULL);
2072 2093 }
2073 2094 do {
2074 2095 l = read(s, &rtmsg, sizeof (rtmsg));
2075 2096 } while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
2076 2097 rtmsg.hdr.rtm_pid != pid));
2077 2098 if (l < 0) {
2078 2099 zerror(zlogp, B_TRUE, "reading from routing socket");
2079 2100 return (NULL);
2080 2101 }
2081 2102
2082 2103 if (rtmsg.hdr.rtm_version != RTM_VERSION) {
2083 2104 zerror(zlogp, B_FALSE,
2084 2105 "routing message version %d not understood",
2085 2106 rtmsg.hdr.rtm_version);
2086 2107 return (NULL);
2087 2108 }
2088 2109 if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
2089 2110 zerror(zlogp, B_FALSE, "message length mismatch, "
2090 2111 "expected %d bytes, returned %d bytes",
2091 2112 rtmsg.hdr.rtm_msglen, l);
2092 2113 return (NULL);
2093 2114 }
2094 2115 if (rtmsg.hdr.rtm_errno != 0) {
2095 2116 errno = rtmsg.hdr.rtm_errno;
2096 2117 zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
2097 2118 return (NULL);
2098 2119 }
2099 2120 if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
2100 2121 zerror(zlogp, B_FALSE, "network interface not found");
2101 2122 return (NULL);
2102 2123 }
2103 2124 cp = ((char *)(&rtmsg.hdr + 1));
2104 2125 for (i = 1; i != 0; i <<= 1) {
2105 2126 /* LINTED E_BAD_PTR_CAST_ALIGN */
2106 2127 sa = (struct sockaddr *)cp;
2107 2128 if (i != RTA_IFP) {
2108 2129 if ((i & rtmsg.hdr.rtm_addrs) != 0)
2109 2130 cp += ROUNDUP_LONG(salen(sa));
2110 2131 continue;
2111 2132 }
2112 2133 if (sa->sa_family == AF_LINK &&
2113 2134 ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
2114 2135 ifp = (struct sockaddr_dl *)sa;
2115 2136 break;
2116 2137 }
2117 2138 if (ifp == NULL) {
2118 2139 zerror(zlogp, B_FALSE, "network interface could not be "
2119 2140 "determined");
2120 2141 return (NULL);
2121 2142 }
2122 2143
2123 2144 /*
2124 2145 * We need to set the I/F name to what we got above, then do the
2125 2146 * appropriate ioctl to get its zone name. But lifr->lifr_name is
2126 2147 * used by the calling function to do a REMOVEIF, so if we leave the
2127 2148 * "good" zone's I/F name in place, *that* I/F will be removed instead
2128 2149 * of the bad one. So we save the old (bad) I/F name before over-
2129 2150 * writing it and doing the ioctl, then restore it after the ioctl.
2130 2151 */
2131 2152 (void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
2132 2153 (void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
2133 2154 lifr->lifr_name[ifp->sdl_nlen] = '\0';
2134 2155 i = ioctl(s, SIOCGLIFZONE, lifr);
2135 2156 (void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
2136 2157 if (i < 0) {
2137 2158 zerror(zlogp, B_TRUE,
2138 2159 "%s: could not determine the zone network interface "
2139 2160 "belongs to", lifr->lifr_name);
2140 2161 return (NULL);
2141 2162 }
2142 2163 if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
2143 2164 (void) snprintf(answer, sizeof (answer), "%d",
2144 2165 lifr->lifr_zoneid);
2145 2166
2146 2167 if (strlen(answer) > 0)
2147 2168 return (answer);
2148 2169 return (NULL);
2149 2170 }
2150 2171
2151 2172 typedef struct mcast_rtmsg_s {
2152 2173 struct rt_msghdr m_rtm;
2153 2174 union {
2154 2175 struct {
2155 2176 struct sockaddr_in m_dst;
2156 2177 struct sockaddr_in m_gw;
2157 2178 struct sockaddr_in m_netmask;
2158 2179 } m_v4;
2159 2180 struct {
2160 2181 struct sockaddr_in6 m_dst;
2161 2182 struct sockaddr_in6 m_gw;
2162 2183 struct sockaddr_in6 m_netmask;
2163 2184 } m_v6;
2164 2185 } m_u;
2165 2186 } mcast_rtmsg_t;
2166 2187 #define m_dst4 m_u.m_v4.m_dst
2167 2188 #define m_dst6 m_u.m_v6.m_dst
2168 2189 #define m_gw4 m_u.m_v4.m_gw
2169 2190 #define m_gw6 m_u.m_v6.m_gw
2170 2191 #define m_netmask4 m_u.m_v4.m_netmask
2171 2192 #define m_netmask6 m_u.m_v6.m_netmask
2172 2193
2173 2194 /*
2174 2195 * Configures a single interface: a new virtual interface is added, based on
2175 2196 * the physical interface nwiftabptr->zone_nwif_physical, with the address
2176 2197 * specified in nwiftabptr->zone_nwif_address, for zone zone_id. Note that
2177 2198 * the "address" can be an IPv6 address (with a /prefixlength required), an
2178 2199 * IPv4 address (with a /prefixlength optional), or a name; for the latter,
2179 2200 * an IPv4 name-to-address resolution will be attempted.
2180 2201 *
2181 2202 * A default interface route for multicast is created on the first IPv4 and
2182 2203 * IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively.
2183 2204 * This should really be done in the init scripts if we ever allow zones to
2184 2205 * modify the routing tables.
2185 2206 *
2186 2207 * If anything goes wrong, we log an detailed error message, attempt to tear
2187 2208 * down whatever we set up and return an error.
2188 2209 */
2189 2210 static int
2190 2211 configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
2191 2212 struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp,
2192 2213 boolean_t *mcast_rt_v6_setp)
2193 2214 {
2194 2215 struct lifreq lifr;
2195 2216 struct sockaddr_in netmask4;
2196 2217 struct sockaddr_in6 netmask6;
2197 2218 struct in_addr in4;
2198 2219 struct in6_addr in6;
2199 2220 sa_family_t af;
2200 2221 char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
2201 2222 mcast_rtmsg_t mcast_rtmsg;
2202 2223 int s;
2203 2224 int rs;
2204 2225 int rlen;
2205 2226 boolean_t got_netmask = B_FALSE;
2206 2227 char addrstr4[INET_ADDRSTRLEN];
2207 2228 int res;
2208 2229
2209 2230 res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
2210 2231 if (res != Z_OK) {
2211 2232 zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
2212 2233 nwiftabptr->zone_nwif_address);
2213 2234 return (-1);
2214 2235 }
2215 2236 af = lifr.lifr_addr.ss_family;
2216 2237 if (af == AF_INET)
2217 2238 in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
2218 2239 else
2219 2240 in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr;
2220 2241
2221 2242 if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
2222 2243 zerror(zlogp, B_TRUE, "could not get socket");
2223 2244 return (-1);
2224 2245 }
2225 2246
2226 2247 (void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
2227 2248 sizeof (lifr.lifr_name));
2228 2249 if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
2229 2250 /*
2230 2251 * Here, we know that the interface can't be brought up.
2231 2252 * A similar warning message was already printed out to
2232 2253 * the console by zoneadm(1M) so instead we log the
2233 2254 * message to syslog and continue.
2234 2255 */
2235 2256 zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
2236 2257 "'%s' which may not be present/plumbed in the "
2237 2258 "global zone.", lifr.lifr_name);
2238 2259 (void) close(s);
2239 2260 return (Z_OK);
2240 2261 }
2241 2262
2242 2263 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
2243 2264 zerror(zlogp, B_TRUE,
2244 2265 "%s: could not set IP address to %s",
2245 2266 lifr.lifr_name, nwiftabptr->zone_nwif_address);
2246 2267 goto bad;
2247 2268 }
2248 2269
2249 2270 /* Preserve literal IPv4 address for later potential printing. */
2250 2271 if (af == AF_INET)
2251 2272 (void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
2252 2273
2253 2274 lifr.lifr_zoneid = zone_id;
2254 2275 if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2255 2276 zerror(zlogp, B_TRUE, "%s: could not place network interface "
2256 2277 "into zone", lifr.lifr_name);
2257 2278 goto bad;
2258 2279 }
2259 2280
2260 2281 if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
2261 2282 got_netmask = B_TRUE; /* default setting will be correct */
2262 2283 } else {
2263 2284 if (af == AF_INET) {
2264 2285 /*
2265 2286 * The IPv4 netmask can be determined either
2266 2287 * directly if a prefix length was supplied with
2267 2288 * the address or via the netmasks database. Not
2268 2289 * being able to determine it is a common failure,
2269 2290 * but it often is not fatal to operation of the
2270 2291 * interface. In that case, a warning will be
2271 2292 * printed after the rest of the interface's
2272 2293 * parameters have been configured.
2273 2294 */
2274 2295 (void) memset(&netmask4, 0, sizeof (netmask4));
2275 2296 if (slashp != NULL) {
2276 2297 if (addr2netmask(slashp + 1, V4_ADDR_LEN,
2277 2298 (uchar_t *)&netmask4.sin_addr) != 0) {
2278 2299 *slashp = '/';
2279 2300 zerror(zlogp, B_FALSE,
2280 2301 "%s: invalid prefix length in %s",
2281 2302 lifr.lifr_name,
2282 2303 nwiftabptr->zone_nwif_address);
2283 2304 goto bad;
2284 2305 }
2285 2306 got_netmask = B_TRUE;
2286 2307 } else if (getnetmaskbyaddr(in4,
2287 2308 &netmask4.sin_addr) == 0) {
2288 2309 got_netmask = B_TRUE;
2289 2310 }
2290 2311 if (got_netmask) {
2291 2312 netmask4.sin_family = af;
2292 2313 (void) memcpy(&lifr.lifr_addr, &netmask4,
2293 2314 sizeof (netmask4));
2294 2315 }
2295 2316 } else {
2296 2317 (void) memset(&netmask6, 0, sizeof (netmask6));
2297 2318 if (addr2netmask(slashp + 1, V6_ADDR_LEN,
2298 2319 (uchar_t *)&netmask6.sin6_addr) != 0) {
2299 2320 *slashp = '/';
2300 2321 zerror(zlogp, B_FALSE,
2301 2322 "%s: invalid prefix length in %s",
2302 2323 lifr.lifr_name,
2303 2324 nwiftabptr->zone_nwif_address);
2304 2325 goto bad;
2305 2326 }
2306 2327 got_netmask = B_TRUE;
2307 2328 netmask6.sin6_family = af;
2308 2329 (void) memcpy(&lifr.lifr_addr, &netmask6,
2309 2330 sizeof (netmask6));
2310 2331 }
2311 2332 if (got_netmask &&
2312 2333 ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
2313 2334 zerror(zlogp, B_TRUE, "%s: could not set netmask",
2314 2335 lifr.lifr_name);
2315 2336 goto bad;
2316 2337 }
2317 2338
2318 2339 /*
2319 2340 * This doesn't set the broadcast address at all. Rather, it
2320 2341 * gets, then sets the interface's address, relying on the fact
2321 2342 * that resetting the address will reset the broadcast address.
2322 2343 */
2323 2344 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
2324 2345 zerror(zlogp, B_TRUE, "%s: could not get address",
2325 2346 lifr.lifr_name);
2326 2347 goto bad;
2327 2348 }
2328 2349 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
2329 2350 zerror(zlogp, B_TRUE,
2330 2351 "%s: could not reset broadcast address",
2331 2352 lifr.lifr_name);
2332 2353 goto bad;
2333 2354 }
2334 2355 }
2335 2356
2336 2357 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2337 2358 zerror(zlogp, B_TRUE, "%s: could not get flags",
2338 2359 lifr.lifr_name);
2339 2360 goto bad;
2340 2361 }
2341 2362 lifr.lifr_flags |= IFF_UP;
2342 2363 if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
2343 2364 int save_errno = errno;
2344 2365 char *zone_using;
2345 2366
2346 2367 /*
2347 2368 * If we failed with something other than EADDRNOTAVAIL,
2348 2369 * then skip to the end. Otherwise, look up our address,
2349 2370 * then call a function to determine which zone is already
2350 2371 * using that address.
2351 2372 */
2352 2373 if (errno != EADDRNOTAVAIL) {
2353 2374 zerror(zlogp, B_TRUE,
2354 2375 "%s: could not bring network interface up",
2355 2376 lifr.lifr_name);
2356 2377 goto bad;
2357 2378 }
2358 2379 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
2359 2380 zerror(zlogp, B_TRUE, "%s: could not get address",
2360 2381 lifr.lifr_name);
2361 2382 goto bad;
2362 2383 }
2363 2384 zone_using = who_is_using(zlogp, &lifr);
2364 2385 errno = save_errno;
2365 2386 if (zone_using == NULL)
2366 2387 zerror(zlogp, B_TRUE,
2367 2388 "%s: could not bring network interface up",
2368 2389 lifr.lifr_name);
2369 2390 else
2370 2391 zerror(zlogp, B_TRUE, "%s: could not bring network "
2371 2392 "interface up: address in use by zone '%s'",
2372 2393 lifr.lifr_name, zone_using);
2373 2394 goto bad;
2374 2395 }
2375 2396 if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET &&
2376 2397 mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) ||
2377 2398 (af == AF_INET6 &&
2378 2399 mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) {
2379 2400 rs = socket(PF_ROUTE, SOCK_RAW, 0);
2380 2401 if (rs < 0) {
2381 2402 zerror(zlogp, B_TRUE, "%s: could not create "
2382 2403 "routing socket", lifr.lifr_name);
2383 2404 goto bad;
2384 2405 }
2385 2406 (void) shutdown(rs, 0);
2386 2407 (void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t));
2387 2408 mcast_rtmsg.m_rtm.rtm_msglen = sizeof (struct rt_msghdr) +
2388 2409 3 * (af == AF_INET ? sizeof (struct sockaddr_in) :
2389 2410 sizeof (struct sockaddr_in6));
2390 2411 mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION;
2391 2412 mcast_rtmsg.m_rtm.rtm_type = RTM_ADD;
2392 2413 mcast_rtmsg.m_rtm.rtm_flags = RTF_UP;
2393 2414 mcast_rtmsg.m_rtm.rtm_addrs =
2394 2415 RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2395 2416 mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno;
2396 2417 if (af == AF_INET) {
2397 2418 mcast_rtmsg.m_dst4.sin_family = AF_INET;
2398 2419 mcast_rtmsg.m_dst4.sin_addr.s_addr =
2399 2420 htonl(INADDR_UNSPEC_GROUP);
2400 2421 mcast_rtmsg.m_gw4.sin_family = AF_INET;
2401 2422 mcast_rtmsg.m_gw4.sin_addr = in4;
2402 2423 mcast_rtmsg.m_netmask4.sin_family = AF_INET;
2403 2424 mcast_rtmsg.m_netmask4.sin_addr.s_addr =
2404 2425 htonl(IN_CLASSD_NET);
2405 2426 } else {
2406 2427 mcast_rtmsg.m_dst6.sin6_family = AF_INET6;
2407 2428 mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU;
2408 2429 mcast_rtmsg.m_gw6.sin6_family = AF_INET6;
2409 2430 mcast_rtmsg.m_gw6.sin6_addr = in6;
2410 2431 mcast_rtmsg.m_netmask6.sin6_family = AF_INET6;
2411 2432 mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU;
2412 2433 }
2413 2434 rlen = write(rs, (char *)&mcast_rtmsg,
2414 2435 mcast_rtmsg.m_rtm.rtm_msglen);
2415 2436 /*
2416 2437 * The write to the multicast socket will fail if the
2417 2438 * interface belongs to a failed IPMP group. This is a
2418 2439 * non-fatal error and the zone will continue booting.
2419 2440 * While the zone is running, if any interface in the
2420 2441 * failed IPMP group recovers, the zone will fallback to
2421 2442 * using that interface.
2422 2443 */
2423 2444 if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) {
2424 2445 if (rlen < 0) {
2425 2446 zerror(zlogp, B_TRUE, "WARNING: network "
2426 2447 "interface '%s' not available as default "
2427 2448 "for multicast.", lifr.lifr_name);
2428 2449 } else {
2429 2450 zerror(zlogp, B_FALSE, "WARNING: network "
2430 2451 "interface '%s' not available as default "
2431 2452 "for multicast; routing socket returned "
2432 2453 "unexpected %d bytes.",
2433 2454 lifr.lifr_name, rlen);
2434 2455 }
2435 2456 } else {
2436 2457
2437 2458 if (af == AF_INET) {
2438 2459 *mcast_rt_v4_setp = B_TRUE;
2439 2460 } else {
2440 2461 *mcast_rt_v6_setp = B_TRUE;
2441 2462 }
2442 2463 }
2443 2464 (void) close(rs);
2444 2465 }
2445 2466
2446 2467 if (!got_netmask) {
2447 2468 /*
2448 2469 * A common, but often non-fatal problem, is that the system
2449 2470 * cannot find the netmask for an interface address. This is
2450 2471 * often caused by it being only in /etc/inet/netmasks, but
2451 2472 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
2452 2473 * in that. This doesn't show up at boot because the netmask
2453 2474 * is obtained from /etc/inet/netmasks when no network
2454 2475 * interfaces are up, but isn't consulted when NIS/NIS+ is
2455 2476 * available. We warn the user here that something like this
2456 2477 * has happened and we're just running with