diff options
author | Olaf Faaland <[email protected]> | 2017-07-18 18:11:08 -0700 |
---|---|---|
committer | Brian Behlendorf <[email protected]> | 2017-07-25 13:22:03 -0400 |
commit | b9373170e3e346733f5666dd80727cb6e05cd5d3 (patch) | |
tree | 943c2d6f5a409170e7e7cd8b5c129d0b9dcab04f /cmd/zgenhostid | |
parent | ffb195c256f8a74a87c3834258ec90c513d66adb (diff) |
Add zgenhostid utility script
Turning the multihost property on requires that a hostid be set to allow
ZFS to determine when a foreign system is attemping to import a pool.
The error message instructing the user to set a hostid refers to
genhostid(1).
Genhostid(1) is not available on SUSE Linux. This commit adds a script
modeled after genhostid(1) for those users.
Zgenhostid checks for an /etc/hostid file; if it does not exist, it
creates one and stores a value. If the user has provided a hostid as an
argument, that value is used. Otherwise, a random hostid is generated
and stored.
This differs from the CENTOS 6/7 versions of genhostid, which overwrite
the /etc/hostid file even though their manpages state otherwise.
A man page for zgenhostid is added. The one for genhostid is in (1), but
I put zgenhostid in (8) because I believe it's more appropriate.
The mmp tests are modified to use zgenhostid to set the hostid instead
of using the spl_hostid module parameter. zgenhostid will not replace
an existing /etc/hostid file, so new mmp_clear_hostid calls are
required.
Reviewed-by: Giuseppe Di Natale <[email protected]>
Reviewed-by: Andreas Dilger <[email protected]>
Reviewed-by: Brian Behlendorf <[email protected]>
Signed-off-by: Olaf Faaland <[email protected]>
Closes #6358
Closes #6379
Diffstat (limited to 'cmd/zgenhostid')
-rw-r--r-- | cmd/zgenhostid/Makefile.am | 1 | ||||
-rwxr-xr-x | cmd/zgenhostid/zgenhostid | 61 |
2 files changed, 62 insertions, 0 deletions
diff --git a/cmd/zgenhostid/Makefile.am b/cmd/zgenhostid/Makefile.am new file mode 100644 index 000000000..69c99ca9d --- /dev/null +++ b/cmd/zgenhostid/Makefile.am @@ -0,0 +1 @@ +dist_bin_SCRIPTS = zgenhostid diff --git a/cmd/zgenhostid/zgenhostid b/cmd/zgenhostid/zgenhostid new file mode 100755 index 000000000..db690eca3 --- /dev/null +++ b/cmd/zgenhostid/zgenhostid @@ -0,0 +1,61 @@ +#!/bin/bash + +# Emulate genhostid(1) available on RHEL/CENTOS, for use on distros +# which do not provide that utility. +# +# Usage: +# zgenhostid +# zgenhostid <value> +# +# If /etc/hostid already exists and is size > 0, the script exits immediately +# and changes nothing. Unlike genhostid, this generates an error message. +# +# The first form generates a random hostid and stores it in /etc/hostid. +# The second form checks that the provided value is between 0x1 and 0xFFFFFFFF +# and if so, stores it in /etc/hostid. This form is not supported by +# genhostid(1). + +hostid_file=/etc/hostid + +function usage { + echo "$0 [value]" + echo "If $hostid_file is not present, store a hostid in it." >&2 + echo "The optional value must be an 8-digit hex number between" >&2 + echo "1 and 2^32-1. If no value is provided, a random one will" >&2 + echo "be generated. The value must be unique among your systems." >&2 +} + +# hostid(1) ignores contents of /etc/hostid if size < 4 bytes. It would +# be better if this checked size >= 4 bytes but it the method must be +# widely portable. +if [ -s $hostid_file ]; then + echo "$hostid_file already exists. No change made." >&2 + exit 1 +fi + +if [ -n "$1" ]; then + host_id=$1 +else + # $RANDOM goes from 0..32k-1 + number=$((((RANDOM % 4) * 32768 + RANDOM) * 32768 + RANDOM)) + host_id=$(printf "%08x" $number) +fi + +if egrep -o '^0{8}$' <<< $host_id >/dev/null 2>&1; then + usage + exit 2 +fi + +if ! egrep -o '^[a-fA-F0-9]{8}$' <<< $host_id >/dev/null 2>&1; then + usage + exit 3 +fi + +a=${host_id:6:2} +b=${host_id:4:2} +c=${host_id:2:2} +d=${host_id:0:2} + +echo -ne \\x$a\\x$b\\x$c\\x$d > $hostid_file + +exit 0 |