Print this page
OS-192 zone_create() warning on headnode

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/zoneadm/zoneadm.c
          +++ new/usr/src/cmd/zoneadm/zoneadm.c
↓ open down ↓ 15 lines elided ↑ open up ↑
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  24   24   * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
  25   25   * Copyright (c) 2015 by Delphix. All rights reserved.
       26 + * Copyright (c) 2011, Joyent Inc. All rights reserved.
  26   27   */
  27   28  
  28   29  /*
  29   30   * zoneadm is a command interpreter for zone administration.  It is all in
  30   31   * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
  31   32   * main() calls parse_and_run() which calls cmd_match(), then invokes the
  32   33   * appropriate command's handler function.  The rest of the program is the
  33   34   * handler functions and their helper functions.
  34   35   *
  35   36   * Some of the helper functions are used largely to simplify I18N: reducing
↓ open down ↓ 58 lines elided ↑ open up ↑
  94   95  /* Reflects kernel zone entries */
  95   96  typedef struct zone_entry {
  96   97          zoneid_t        zid;
  97   98          char            zname[ZONENAME_MAX];
  98   99          char            *zstate_str;
  99  100          zone_state_t    zstate_num;
 100  101          char            zbrand[MAXNAMELEN];
 101  102          char            zroot[MAXPATHLEN];
 102  103          char            zuuid[UUID_PRINTABLE_STRING_LENGTH];
 103  104          zone_iptype_t   ziptype;
      105 +        zoneid_t        zdid;
 104  106  } zone_entry_t;
 105  107  
 106  108  #define CLUSTER_BRAND_NAME      "cluster"
 107  109  
 108  110  static zone_entry_t *zents;
 109  111  static size_t nzents;
 110  112  
 111  113  #define LOOPBACK_IF     "lo0"
 112  114  #define SOCKET_AF(af)   (((af) == AF_UNSPEC) ? AF_INET : (af))
 113  115  
↓ open down ↓ 322 lines elided ↑ open up ↑
 436  438  
 437  439          assert(!(verbose && parsable));
 438  440          if (firsttime && verbose) {
 439  441                  firsttime = B_FALSE;
 440  442                  (void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n",
 441  443                      ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND",
 442  444                      "IP");
 443  445          }
 444  446          if (!verbose) {
 445  447                  char *cp, *clim;
      448 +                char zdid[80];
 446  449  
 447  450                  if (!parsable) {
 448  451                          (void) printf("%s\n", zent->zname);
 449  452                          return;
 450  453                  }
 451  454                  if (zent->zid == ZONE_ID_UNDEFINED)
 452  455                          (void) printf("-");
 453  456                  else
 454  457                          (void) printf("%lu", zent->zid);
 455  458                  (void) printf(":%s:%s:", zent->zname, zent->zstate_str);
 456  459                  cp = zent->zroot;
 457  460                  while ((clim = strchr(cp, ':')) != NULL) {
 458  461                          (void) printf("%.*s\\:", clim - cp, cp);
 459  462                          cp = clim + 1;
 460  463                  }
 461      -                (void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand,
 462      -                    ip_type_str);
      464 +                if (zent->zdid == -1)
      465 +                        zdid[0] = '\0';
      466 +                else
      467 +                        (void) snprintf(zdid, sizeof (zdid), "%d", zent->zdid);
      468 +                (void) printf("%s:%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand,
      469 +                    ip_type_str, zdid);
 463  470                  return;
 464  471          }
 465  472          if (zent->zstate_str != NULL) {
 466  473                  if (zent->zid == ZONE_ID_UNDEFINED)
 467  474                          (void) printf("%*s", ZONEID_WIDTH, "-");
 468  475                  else
 469  476                          (void) printf("%*lu", ZONEID_WIDTH, zent->zid);
 470  477                  (void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname,
 471  478                      zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str);
 472  479          }
↓ open down ↓ 74 lines elided ↑ open up ↑
 547  554  
 548  555          /*
 549  556           * Get ip type of the zone.
 550  557           * Note for global zone, ZS_SHARED is set always.
 551  558           */
 552  559          if (zid == GLOBAL_ZONEID) {
 553  560                  zent->ziptype = ZS_SHARED;
 554  561                  return (Z_OK);
 555  562          }
 556  563  
      564 +        if ((handle = zonecfg_init_handle()) == NULL) {
      565 +                zperror2(zent->zname, gettext("could not init handle"));
      566 +                return (Z_ERR);
      567 +        }
      568 +        if ((err = zonecfg_get_handle(zent->zname, handle)) != Z_OK) {
      569 +                zperror2(zent->zname, gettext("could not get handle"));
      570 +                zonecfg_fini_handle(handle);
      571 +                return (Z_ERR);
      572 +        }
      573 +
      574 +        if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) != Z_OK) {
      575 +                zperror2(zent->zname, gettext("could not get ip-type"));
      576 +                zonecfg_fini_handle(handle);
      577 +                return (Z_ERR);
      578 +        }
      579 +
 557  580          /*
 558  581           * There is a race condition where the zone could boot while
 559  582           * we're walking the index file.  In this case the zone state
 560  583           * could be seen as running from the call above, but the zoneid
 561  584           * would be undefined.
 562  585           *
 563  586           * There is also a race condition where the zone could shutdown after
 564  587           * we got its running state above.  This is also not an error and
 565  588           * we fall back to getting the ziptype from the zone configuration.
 566  589           */
 567  590          if (zent->zstate_num == ZONE_STATE_RUNNING &&
 568  591              zid != ZONE_ID_UNDEFINED) {
 569  592                  ushort_t flags;
 570  593  
 571  594                  if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags,
 572  595                      sizeof (flags)) >= 0) {
 573  596                          if (flags & ZF_NET_EXCL)
 574  597                                  zent->ziptype = ZS_EXCLUSIVE;
 575  598                          else
 576  599                                  zent->ziptype = ZS_SHARED;
 577      -                        return (Z_OK);
 578  600                  }
 579  601          }
 580  602  
 581      -        if ((handle = zonecfg_init_handle()) == NULL) {
 582      -                zperror2(zent->zname, gettext("could not init handle"));
 583      -                return (Z_ERR);
 584      -        }
 585      -        if ((err = zonecfg_get_handle(zent->zname, handle)) != Z_OK) {
 586      -                zperror2(zent->zname, gettext("could not get handle"));
 587      -                zonecfg_fini_handle(handle);
 588      -                return (Z_ERR);
 589      -        }
      603 +        zent->zdid = zonecfg_get_did(handle);
 590  604  
 591      -        if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) != Z_OK) {
 592      -                zperror2(zent->zname, gettext("could not get ip-type"));
 593      -                zonecfg_fini_handle(handle);
 594      -                return (Z_ERR);
 595      -        }
 596  605          zonecfg_fini_handle(handle);
 597  606  
 598  607          return (Z_OK);
 599  608  }
 600  609  
 601  610  /*
 602  611   * fetch_zents() calls zone_list(2) to find out how many zones are running
 603  612   * (which is stored in the global nzents), then calls zone_list(2) again
 604  613   * to fetch the list of running zones (stored in the global zents).  This
 605  614   * function may be called multiple times, so if zents is already set, we
↓ open down ↓ 2179 lines elided ↑ open up ↑
2785 2794           * privileges will be used when this zone is created in the
2786 2795           * kernel.
2787 2796           */
2788 2797          if (!in_alt_root && cmd_num != CMD_MOUNT &&
2789 2798              verify_limitpriv(handle) != Z_OK)
2790 2799                  return_code = Z_ERR;
2791 2800  
2792 2801          return (return_code);
2793 2802  }
2794 2803  
     2804 +/*
     2805 + * Called when readying or booting a zone.  We double check that the zone's
     2806 + * debug ID is set and is unique.  This covers the case of pre-existing zones
     2807 + * with no ID.  Also, its possible that a zone was migrated to this host
     2808 + * and as a result it has a duplicate ID.  In this case we preserve the ID
     2809 + * of the first zone we match on in the index file (since it was there before
     2810 + * the current zone) and we assign a new unique ID to the current zone.
     2811 + * Return true if we assigned a new ID, indicating that the zone configuration
     2812 + * needs to be saved.
     2813 + */
     2814 +static boolean_t
     2815 +verify_fix_did(zone_dochandle_t handle)
     2816 +{
     2817 +        zoneid_t mydid;
     2818 +        zone_entry_t zent;
     2819 +        FILE *cookie;
     2820 +        char *name;
     2821 +        boolean_t fix = B_FALSE;
     2822 +
     2823 +        mydid = zonecfg_get_did(handle);
     2824 +        if (mydid == -1) {
     2825 +                zonecfg_set_did(handle);
     2826 +                return (B_TRUE);
     2827 +        }
     2828 +
     2829 +        /* Get the full list of zones from the configuration. */
     2830 +        cookie = setzoneent();
     2831 +        while ((name = getzoneent(cookie)) != NULL) {
     2832 +                if (strcmp(target_zone, name) == 0) {
     2833 +                        free(name);
     2834 +                        break;  /* Once we find our entry, stop. */
     2835 +                }
     2836 +
     2837 +                if (strcmp(name, "global") == 0 ||
     2838 +                    lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
     2839 +                        free(name);
     2840 +                        continue;
     2841 +                }
     2842 +
     2843 +                free(name);
     2844 +                if (zent.zdid == mydid) {
     2845 +                        fix = B_TRUE;
     2846 +                        break;
     2847 +                }
     2848 +        }
     2849 +        endzoneent(cookie);
     2850 +
     2851 +        if (fix) {
     2852 +                zonecfg_set_did(handle);
     2853 +                return (B_TRUE);
     2854 +        }
     2855 +
     2856 +        return (B_FALSE);
     2857 +}
     2858 +
2795 2859  static int
2796 2860  verify_details(int cmd_num, char *argv[])
2797 2861  {
2798 2862          zone_dochandle_t handle;
2799 2863          char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
2800 2864          int return_code = Z_OK;
2801 2865          int err;
2802 2866  
2803 2867          if ((handle = zonecfg_init_handle()) == NULL) {
2804 2868                  zperror(cmd_to_str(cmd_num), B_TRUE);
↓ open down ↓ 39 lines elided ↑ open up ↑
2844 2908          if (cmd_num != CMD_ATTACH &&
2845 2909              validate_zonepath(zonepath, cmd_num) != Z_OK) {
2846 2910                  (void) fprintf(stderr, gettext("could not verify zonepath %s "
2847 2911                      "because of the above errors.\n"), zonepath);
2848 2912                  return_code = Z_ERR;
2849 2913          }
2850 2914  
2851 2915          if (verify_handle(cmd_num, handle, argv) != Z_OK)
2852 2916                  return_code = Z_ERR;
2853 2917  
     2918 +        if (cmd_num == CMD_READY || cmd_num == CMD_BOOT)
     2919 +                if (verify_fix_did(handle))
     2920 +                        if (zonecfg_save(handle) != Z_OK)
     2921 +                                (void) fprintf(stderr, gettext("Could not save "
     2922 +                                    "debug ID.\n"));
     2923 +
2854 2924          zonecfg_fini_handle(handle);
2855 2925          if (return_code == Z_ERR)
2856 2926                  (void) fprintf(stderr,
2857 2927                      gettext("%s: zone %s failed to verify\n"),
2858 2928                      execname, target_zone);
2859 2929          return (return_code);
2860 2930  }
2861 2931  
2862 2932  static int
2863 2933  verify_func(int argc, char *argv[])
↓ open down ↓ 2942 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX