git: a4c5803ecd03 - main - hastd: Allow overriding the node name

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Tue, 03 Nov 2026 12:17:30 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=a4c5803ecd03d3a95eedd3bd988f89784ec1a8d4

commit a4c5803ecd03d3a95eedd3bd988f89784ec1a8d4
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-11-03 12:07:29 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-11-03 12:07:29 +0000

    hastd: Allow overriding the node name
    
    Normally, hastd will use the hostname, hostuuid, or hostid as node name.
    Allow overriding this with a new -n command-line option.  The argument
    is treated as a replacement for the hostname, but also disables the
    hostuuid and hostid logic.  This allows running multiple nodes on the
    same system, which is useful for testing.
    
    Differential Revision:  https://reviews.freebsd.org/D59675
---
 sbin/hastctl/hastctl.8 | 12 +++++++++++-
 sbin/hastctl/hastctl.c | 30 ++++++++++++++++++------------
 sbin/hastd/hast.h      |  2 ++
 sbin/hastd/hastd.8     |  8 +++++++-
 sbin/hastd/hastd.c     | 11 ++++++++---
 sbin/hastd/parse.y     | 46 +++++++++++++++++++++++++++++++++++++++-------
 6 files changed, 85 insertions(+), 24 deletions(-)

diff --git a/sbin/hastctl/hastctl.8 b/sbin/hastctl/hastctl.8
index f696858a8071..219b1f9dc3d0 100644
--- a/sbin/hastctl/hastctl.8
+++ b/sbin/hastctl/hastctl.8
@@ -24,7 +24,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd September 8, 2016
+.Dd October 1, 2026
 .Dt HASTCTL 8
 .Os
 .Sh NAME
@@ -38,27 +38,32 @@
 .Op Fl e Ar extentsize
 .Op Fl k Ar keepdirty
 .Op Fl m Ar mediasize
+.Op Fl n Ar nodename
 .Ar name ...
 .Nm
 .Cm role
 .Op Fl d
 .Op Fl c Ar config
+.Op Fl n Ar nodename
 .Aq init | primary | secondary
 .Ar all | name ...
 .Nm
 .Cm list
 .Op Fl d
 .Op Fl c Ar config
+.Op Fl n Ar nodename
 .Op Ar all | name ...
 .Nm
 .Cm status
 .Op Fl d
 .Op Fl c Ar config
+.Op Fl n Ar nodename
 .Op Ar all | name ...
 .Nm
 .Cm dump
 .Op Fl d
 .Op Fl c Ar config
+.Op Fl n Ar nodename
 .Op Ar all | name ...
 .Sh DESCRIPTION
 The
@@ -114,6 +119,11 @@ dirty is
 Size of the smaller provider used as backend storage on both nodes.
 This option can be omitted if node providers have the same size on both
 sides.
+.It Fl n Ar nodename
+Specify a node name to use instead of the current host name as
+returned by
+.Xr gethostname 3 .
+This is mostly useful for testing.
 .El
 .Pp
 If size is suffixed with a k, M, G or T, it is taken as a kilobyte,
diff --git a/sbin/hastctl/hastctl.c b/sbin/hastctl/hastctl.c
index 229af1dae74d..788c22572a67 100644
--- a/sbin/hastctl/hastctl.c
+++ b/sbin/hastctl/hastctl.c
@@ -66,22 +66,25 @@ enum {
 static __dead2 void
 usage(void)
 {
-
 	fprintf(stderr,
 	    "usage: %s create [-d] [-c config] [-e extentsize] [-k keepdirty]\n"
-	    "\t\t[-m mediasize] name ...\n",
+	    "\t\t[-m mediasize] [-n nodename] name ...\n",
 	    getprogname());
 	fprintf(stderr,
-	    "       %s role [-d] [-c config] <init | primary | secondary> all | name ...\n",
+	    "       %s role [-d] [-c config] [-n nodename] "
+	    "<init | primary | secondary> all | name ...\n",
 	    getprogname());
 	fprintf(stderr,
-	    "       %s list [-d] [-c config] [all | name ...]\n",
+	    "       %s list [-d] [-c config] [-n nodename] "
+	    "[all | name ...]\n",
 	    getprogname());
 	fprintf(stderr,
-	    "       %s status [-d] [-c config] [all | name ...]\n",
+	    "       %s status [-d] [-c config] [-n nodename] "
+	    "[all | name ...]\n",
 	    getprogname());
 	fprintf(stderr,
-	    "       %s dump [-d] [-c config] [all | name ...]\n",
+	    "       %s dump [-d] [-c config] [-n nodename] "
+	    "[all | name ...]\n",
 	    getprogname());
 	exit(EX_USAGE);
 }
@@ -418,22 +421,22 @@ main(int argc, char *argv[])
 
 	if (strcmp(argv[1], "create") == 0) {
 		cmd = CMD_CREATE;
-		optstr = "c:de:k:m:h";
+		optstr = "c:de:hk:m:n:";
 	} else if (strcmp(argv[1], "role") == 0) {
 		cmd = CMD_ROLE;
-		optstr = "c:dh";
+		optstr = "c:dhn:";
 	} else if (strcmp(argv[1], "list") == 0) {
 		cmd = CMD_LIST;
-		optstr = "c:dh";
+		optstr = "c:dhn:";
 	} else if (strcmp(argv[1], "status") == 0) {
 		cmd = CMD_STATUS;
-		optstr = "c:dh";
+		optstr = "c:dhn:";
 	} else if (strcmp(argv[1], "dump") == 0) {
 		cmd = CMD_DUMP;
-		optstr = "c:dh";
+		optstr = "c:dhn:";
 	} else if (strcmp(argv[1], "stop") == 0) {
 		cmd = CMD_STOP;
-		optstr = "c:dh";
+		optstr = "c:dhn:";
 	} else
 		usage();
 
@@ -465,6 +468,9 @@ main(int argc, char *argv[])
 			if (expand_number(optarg, &mediasize) == -1)
 				errx(EX_USAGE, "Invalid mediasize");
 			break;
+		case 'n':
+			nodename = optarg;
+			break;
 		case 'h':
 		default:
 			usage();
diff --git a/sbin/hastd/hast.h b/sbin/hastd/hast.h
index a91993fc398e..2a23944d0af6 100644
--- a/sbin/hastd/hast.h
+++ b/sbin/hastd/hast.h
@@ -267,4 +267,6 @@ struct hast_resource {
 struct hastd_config *yy_config_parse(const char *config, bool exitonerror);
 void yy_config_free(struct hastd_config *config);
 
+extern const char *nodename;
+
 #endif	/* !_HAST_H_ */
diff --git a/sbin/hastd/hastd.8 b/sbin/hastd/hastd.8
index 05a2d9b5e4e9..3c3088507ff9 100644
--- a/sbin/hastd/hastd.8
+++ b/sbin/hastd/hastd.8
@@ -24,7 +24,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd December 21, 2019
+.Dd October 1, 2026
 .Dt HASTD 8
 .Os
 .Sh NAME
@@ -34,6 +34,7 @@
 .Nm
 .Op Fl dFh
 .Op Fl c Ar config
+.Op Fl n Ar nodename
 .Op Fl P Ar pidfile
 .Sh DESCRIPTION
 The
@@ -161,6 +162,11 @@ starts in the background.
 Print the
 .Nm
 usage message.
+.It Fl n Ar nodename
+Specify a node name to use instead of the current host name as
+returned by
+.Xr gethostname 3 .
+This is mostly useful for testing.
 .It Fl P Ar pidfile
 Specify alternative location of a file where main process PID will be
 stored.
diff --git a/sbin/hastd/hastd.c b/sbin/hastd/hastd.c
index 1569dc9f8697..c5d3bb00c281 100644
--- a/sbin/hastd/hastd.c
+++ b/sbin/hastd/hastd.c
@@ -78,8 +78,10 @@ static bool foreground;
 static void
 usage(void)
 {
-
-	errx(EX_USAGE, "[-dFh] [-c config] [-P pidfile]");
+	fprintf(stderr,
+	    "usage: %s [-dFh] [-c config] [-n nodename] [-P pidfile]\n",
+	    getprogname());
+	exit(1);
 }
 
 static void
@@ -1197,7 +1199,7 @@ main(int argc, char *argv[])
 	for (;;) {
 		int ch;
 
-		ch = getopt(argc, argv, "c:dFhP:");
+		ch = getopt(argc, argv, "c:dFhn:P:");
 		if (ch == -1)
 			break;
 		switch (ch) {
@@ -1207,6 +1209,9 @@ main(int argc, char *argv[])
 		case 'd':
 			debuglevel++;
 			break;
+		case 'n':
+			nodename = optarg;
+			break;
 		case 'F':
 			foreground = true;
 			break;
diff --git a/sbin/hastd/parse.y b/sbin/hastd/parse.y
index 6cba61d74738..719ca834309d 100644
--- a/sbin/hastd/parse.y
+++ b/sbin/hastd/parse.y
@@ -55,6 +55,8 @@ extern int lineno;
 extern FILE *yyin;
 extern char *yytext;
 
+const char *nodename;
+
 static struct hastd_config *lconfig;
 static struct hast_resource *curres;
 static bool mynode, hadmynode;
@@ -731,7 +733,23 @@ isitme(const char *name)
 	char buf[MAXHOSTNAMELEN];
 	unsigned long hostid;
 	char *pos;
-	size_t bufsize;
+	size_t bufsize, len;
+
+	/*
+	 * A static node name overrides all others.
+	 */
+	if (nodename != NULL) {
+		if (strcmp(nodename, name) == 0)
+			return (1);
+		pos = strchr(nodename, '.');
+		if (pos != NULL && pos != nodename) {
+			len = pos - nodename;
+			if (len == strlen(name) &&
+			    strncmp(nodename, name, len) == 0)
+				return (1);
+		}
+		return (0);
+	}
 
 	/*
 	 * First check if the given name matches our full hostname.
@@ -747,9 +765,10 @@ isitme(const char *name)
 	 * Check if it matches first part of the host name.
 	 */
 	pos = strchr(buf, '.');
-	if (pos != NULL && (size_t)(pos - buf) == strlen(name) &&
-	    strncmp(buf, name, pos - buf) == 0) {
-		return (1);
+	if (pos != NULL && pos != buf) {
+		len = pos - buf;
+		if (len == strlen(name) && strncmp(buf, name, len) == 0)
+			return (1);
 	}
 
 	/*
@@ -801,7 +820,20 @@ node_names(char **namesp)
 	char buf[MAXHOSTNAMELEN];
 	unsigned long hostid;
 	char *pos;
-	size_t bufsize;
+	size_t bufsize, len;
+
+	/* A static node name overrides all others. */
+	if (nodename != NULL) {
+		pos = strchr(nodename, '.');
+		if (pos != NULL && pos != nodename) {
+			len = pos - nodename;
+			(void)strlcpy(names, nodename, MIN(len + 1, sizeof(names)));
+			(void)strlcat(names, ", ", sizeof(names));
+		}
+		(void)strlcat(names, nodename, sizeof(names));
+		*namesp = names;
+		return (0);
+	}
 
 	if (gethostname(buf, sizeof(buf)) < 0) {
 		pjdlog_errno(LOG_ERR, "gethostname() failed");
@@ -811,8 +843,8 @@ node_names(char **namesp)
 	/* First component of the host name. */
 	pos = strchr(buf, '.');
 	if (pos != NULL && pos != buf) {
-		(void)strlcpy(names, buf, MIN((size_t)(pos - buf + 1),
-		    sizeof(names)));
+		len = pos - buf;
+		(void)strlcpy(names, buf, MIN(len + 1, sizeof(names)));
 		(void)strlcat(names, ", ", sizeof(names));
 	}