Whamcloud - gitweb
LU-7134 utils: Ensure hostid set for ZFS during mkfs 11/16611/12
authorNathaniel Clark <nathaniel.l.clark@intel.com>
Tue, 15 Sep 2015 19:26:53 +0000 (15:26 -0400)
committerOleg Drokin <oleg.drokin@intel.com>
Tue, 31 May 2016 04:55:34 +0000 (04:55 +0000)
Ensure import protection in ZFS.  spl_hostid must be set to a
non-zero value for it to work correctly.  Error out during mkfs and
tunefs unless override is passed.  Also print error during mount.

Ensure hostid is set in conf-sanity.sh

Signed-off-by: Nathaniel Clark <nathaniel.l.clark@intel.com>
Change-Id: If37318c20e4502a643992e6bc21ac4bc4ad20ed7
Reviewed-on: http://review.whamcloud.com/16611
Reviewed-by: Andreas Dilger <andreas.dilger@intel.com>
Reviewed-by: Alex Zhuravlev <alexey.zhuravlev@intel.com>
Tested-by: Jenkins
Tested-by: Maloo <hpdd-maloo@intel.com>
lustre/doc/mkfs.lustre.8
lustre/doc/tunefs.lustre.8
lustre/tests/test-framework.sh
lustre/utils/mkfs_lustre.c
lustre/utils/mount_lustre.c
lustre/utils/mount_utils.h
lustre/utils/mount_utils_zfs.c

index 9f0bc47..3c256b5 100644 (file)
@@ -147,6 +147,11 @@ avoids having the target try to register as a new target with the MGS.
 .BI \--stripe-count-hint= stripes
 Used for optizing MDT inode size
 .TP
+.BI \--force-nohostid
+Ignore unset hostid for ZFS import protection. To set hostid either set
+spl_hostid parameter for spl.ko or set /etc/hostid, see genhostid(1).
+
+.TP
 .BI \--verbose
 Print more information.
 .TP
index 8e49b45..10228c8 100644 (file)
@@ -75,6 +75,10 @@ Set the NID(s) of the MGS node, required for all targets other than the MGS.
 .BI \--nomgs
 Remove a configuration management service to this target
 .TP
+.BI \--force-nohostid
+Ignore unset hostid for ZFS import protection. To set hostid either set
+spl_hostid option for spl.ko or set /etc/hostid, see genhostid(1).
+.TP
 .BI \--quiet
 Print less information.
 .TP
index b323882..5bde188 100755 (executable)
@@ -1190,6 +1190,15 @@ set_default_debug_facet () {
     set_default_debug_nodes $node
 }
 
+set_hostid () {
+    local hostid=${1:-$(hostid)}
+
+    if [ ! -s /etc/hostid ]; then
+       printf $(echo -n $hostid |
+           sed 's/\(..\)\(..\)\(..\)\(..\)/\\x\4\\x\3\\x\2\\x\1/') >/etc/hostid
+    fi
+}
+
 # Facet functions
 mount_facets () {
        local facets=${1:-$(get_facets)}
@@ -3537,6 +3546,9 @@ format_ost() {
 
 formatall() {
        stopall
+       # Set hostid for ZFS/SPL zpool import protection
+       do_rpc_nodes "$(comma_list $(remote_nodes_list))" set_hostid
+
        # We need ldiskfs here, may as well load them all
        load_modules
        [ "$CLIENTONLY" ] && return
index ca7d5f9..456c37e 100644 (file)
@@ -165,6 +165,8 @@ void usage(FILE *out)
                "\t\t--comment=<user comment>: arbitrary string (%d bytes)\n"
                "\t\t--dryrun: report what we would do; don't write to disk\n"
                "\t\t--verbose: e.g. show mkfs progress\n"
+               "\t\t--force-nohostid: Ignore hostid requirement for ZFS "
+                       "import\n"
                "\t\t-V|--version: output build version of the utility and\n"
                "\t\t\texit\n"
                "\t\t--quiet\n",
@@ -294,6 +296,7 @@ int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
                { "servicenode",        required_argument,      NULL, 's' },
                { "network",            required_argument,      NULL, 't' },
                { "comment",            required_argument,      NULL, 'u' },
+               { "force-nohostid",     no_argument,            NULL, 'U' },
                { "verbose",            no_argument,            NULL, 'v' },
                { "version",            no_argument,            NULL, 'V' },
 #ifndef TUNEFS
@@ -468,6 +471,9 @@ int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
                        strscpy(ldd->ldd_userdata, optarg,
                                sizeof(ldd->ldd_userdata));
                        break;
+               case 'U':
+                       mop->mo_flags |= MO_NOHOSTID_CHECK;
+                       break;
                case 'v':
                        verbose++;
                        break;
index dfa65f4..b247327 100644 (file)
@@ -341,6 +341,7 @@ static int clear_update_ondisk(char *source, struct lustre_disk_data *ldd)
        memset(&mkop, 0, sizeof(mkop));
        mkop.mo_ldd = *ldd;
        mkop.mo_ldd.ldd_flags &= ~LDD_F_UPDATE;
+       mkop.mo_flags = MO_NOHOSTID_CHECK; /* Ignore missing hostid */
        if (strlen(source) > sizeof(mkop.mo_device)-1) {
                fatal();
                fprintf(stderr, "Device name too long: %s\n", source);
index 915b48f..ae238eb 100644 (file)
@@ -74,6 +74,7 @@ extern int failover;
 #define MO_FAILOVER            0x04
 #define MO_DRYRUN              0x08
 #define MO_QUOTA               0x10
+#define MO_NOHOSTID_CHECK      0x20
 
 #define MAX_LOOP_DEVICES       16
 #define INDEX_UNASSIGNED       0xFFFF
index b1ca83d..60d5552 100644 (file)
@@ -174,6 +174,45 @@ static int zfs_set_prop_params(zfs_handle_t *zhp, char *params)
        return ret;
 }
 
+static int zfs_check_hostid(struct mkfs_opts *mop)
+{
+       FILE *f;
+       unsigned long hostid;
+       int rc;
+
+       if (strstr(mop->mo_ldd.ldd_params, PARAM_FAILNODE) == NULL)
+               return 0;
+
+       f = fopen("/sys/module/spl/parameters/spl_hostid", "r");
+       if (f == NULL) {
+               fatal();
+               fprintf(stderr, "Failed to open spl_hostid: %s\n",
+                       strerror(errno));
+               return errno;
+       }
+       rc = fscanf(f, "%li", &hostid);
+       fclose(f);
+       if (rc != 1) {
+               fatal();
+               fprintf(stderr, "Failed to read spl_hostid: %d\n", rc);
+               return rc;
+       }
+
+       if (hostid == 0) {
+               if (mop->mo_flags & MO_NOHOSTID_CHECK) {
+                       fprintf(stderr, "WARNING: spl_hostid not set. ZFS has "
+                               "no zpool import protection\n");
+               } else {
+                       fatal();
+                       fprintf(stderr, "spl_hostid not set. See %s(8)",
+                               progname);
+                       return EINVAL;
+               }
+       }
+
+       return 0;
+}
+
 static int osd_check_zfs_setup(void)
 {
        if (osd_zfs_setup == 0) {
@@ -195,7 +234,7 @@ int zfs_write_ldd(struct mkfs_opts *mop)
        int i, ret = EINVAL;
 
        if (osd_check_zfs_setup() == 0)
-               return EINVAL;
+               goto out;
 
        zhp = zfs_open(g_zfs, ds, ZFS_TYPE_FILESYSTEM);
        if (zhp == NULL) {
@@ -203,6 +242,10 @@ int zfs_write_ldd(struct mkfs_opts *mop)
                goto out;
        }
 
+       ret = zfs_check_hostid(mop);
+       if (ret != 0)
+               goto out;
+
        vprint("Writing %s properties\n", ds);
 
        for (i = 0; special_ldd_prop_params[i].zlpb_prop_name != NULL; i++) {
@@ -451,6 +494,10 @@ int zfs_make_lustre(struct mkfs_opts *mop)
                return EINVAL;
        }
 
+       ret = zfs_check_hostid(mop);
+       if (ret != 0)
+               goto out;
+
        pool = strdup(ds);
        if (pool == NULL)
                return ENOMEM;