> On Feb 10, 2022, at 1:01 PM, Benjamin Coddington <bcodding@xxxxxxxxxx> wrote: > > The nfsuuid program will either create a new UUID from a random source or > derive it from /etc/machine-id, else it returns a UUID that has already > been written to /etc/nfsuuid or the file specified. This small, > lightweight tool is suitable for execution by systemd-udev in rules to > populate the nfs4 client uniquifier. I like everything but the name. Without context, even if I read NFS protocol specifications, I have no idea what "nfsuuid" does. Possible alternatives: nfshostuniquifier nfsuniquehost nfshostuuid > Signed-off-by: Benjamin Coddington <bcodding@xxxxxxxxxx> > --- > .gitignore | 1 + > aclocal/libuuid.m4 | 17 ++++ > configure.ac | 4 + > tools/Makefile.am | 1 + > tools/nfsuuid/Makefile.am | 8 ++ > tools/nfsuuid/nfsuuid.c | 203 ++++++++++++++++++++++++++++++++++++++ > tools/nfsuuid/nfsuuid.man | 33 +++++++ > 7 files changed, 267 insertions(+) > create mode 100644 aclocal/libuuid.m4 > create mode 100644 tools/nfsuuid/Makefile.am > create mode 100644 tools/nfsuuid/nfsuuid.c > create mode 100644 tools/nfsuuid/nfsuuid.man > > diff --git a/.gitignore b/.gitignore > index c89d1cd2583d..4d63ee93b2dc 100644 > --- a/.gitignore > +++ b/.gitignore > @@ -61,6 +61,7 @@ utils/statd/statd > tools/locktest/testlk > tools/getiversion/getiversion > tools/nfsconf/nfsconf > +tools/nfsuuid/nfsuuid > support/export/mount.h > support/export/mount_clnt.c > support/export/mount_xdr.c > diff --git a/aclocal/libuuid.m4 b/aclocal/libuuid.m4 > new file mode 100644 > index 000000000000..f64085010d1d > --- /dev/null > +++ b/aclocal/libuuid.m4 > @@ -0,0 +1,17 @@ > +AC_DEFUN([AC_LIBUUID], [ > + LIBUUID= > + > + AC_CHECK_LIB([uuid], [uuid_generate_random], [], [AC_MSG_FAILURE( > + [Missing libuuid uuid_generate_random, needed to build nfs4id])]) > + > + AC_CHECK_LIB([uuid], [uuid_generate_sha1], [], [AC_MSG_FAILURE( > + [Missing libuuid uuid_generate_sha1, needed to build nfs4id])]) > + > + AC_CHECK_LIB([uuid], [uuid_unparse], [], [AC_MSG_FAILURE( > + [Missing libuuid uuid_unparse, needed to build nfs4id])]) > + > + AC_CHECK_HEADER([uuid/uuid.h], [], [AC_MSG_FAILURE( > + [Missing uuid/uuid.h, needed to build nfs4id])]) > + > + AC_SUBST([LIBUUID], ["-luuid"]) > +]) > diff --git a/configure.ac b/configure.ac > index 50e9b321dcf3..1342c471f142 100644 > --- a/configure.ac > +++ b/configure.ac > @@ -355,6 +355,9 @@ if test "$enable_nfsv4" = yes; then > dnl check for the keyutils libraries and headers > AC_KEYUTILS > > + dnl check for the libuuid library and headers > + AC_LIBUUID > + > dnl Check for sqlite3 > AC_SQLITE3_VERS > > @@ -740,6 +743,7 @@ AC_CONFIG_FILES([ > tools/nfsdclnts/Makefile > tools/nfsconf/Makefile > tools/nfsdclddb/Makefile > + tools/nfsuuid/Makefile > utils/Makefile > utils/blkmapd/Makefile > utils/nfsdcld/Makefile > diff --git a/tools/Makefile.am b/tools/Makefile.am > index 9b4b0803db39..a12b0f34f4e7 100644 > --- a/tools/Makefile.am > +++ b/tools/Makefile.am > @@ -7,6 +7,7 @@ OPTDIRS += rpcgen > endif > > OPTDIRS += nfsconf > +OPTDIRS += nfsuuid > > if CONFIG_NFSDCLD > OPTDIRS += nfsdclddb > diff --git a/tools/nfsuuid/Makefile.am b/tools/nfsuuid/Makefile.am > new file mode 100644 > index 000000000000..7b3a54c30d50 > --- /dev/null > +++ b/tools/nfsuuid/Makefile.am > @@ -0,0 +1,8 @@ > +## Process this file with automake to produce Makefile.in > + > +man8_MANS = nfsuuid.man > + > +bin_PROGRAMS = nfsuuid > + > +nfsuuid_SOURCES = nfsuuid.c > +nfsuuid_LDADD = $(LIBUUID) > diff --git a/tools/nfsuuid/nfsuuid.c b/tools/nfsuuid/nfsuuid.c > new file mode 100644 > index 000000000000..bbdec59f1afe > --- /dev/null > +++ b/tools/nfsuuid/nfsuuid.c > @@ -0,0 +1,203 @@ > +/* > + * nfsuuid.c -- create and persist uniquifiers for nfs4 clients > + * > + * Copyright (C) 2022 Red Hat, Benjamin Coddington <bcodding@xxxxxxxxxx> > + * > + * This program is free software; you can redistribute it and/or > + * modify it under the terms of the GNU General Public License > + * as published by the Free Software Foundation; either version 2 > + * of the License, or (at your option) any later version. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU General Public License for more details. > + * > + * You should have received a copy of the GNU General Public License > + * along with this program; if not, write to the Free Software > + * Foundation, Inc., 51 Franklin Street, Fifth Floor, > + * Boston, MA 02110-1301, USA. > + */ > + > +#include <stdio.h> > +#include <stdarg.h> > +#include <getopt.h> > +#include <string.h> > +#include <errno.h> > +#include <stdlib.h> > +#include <fcntl.h> > +#include <unistd.h> > +#include <uuid/uuid.h> > + > +#define DEFAULT_ID_FILE "/etc/nfsuuid" > + > +UUID_DEFINE(nfs4_clientid_uuid_template, > + 0xa2, 0x25, 0x68, 0xb2, 0x7a, 0x5f, 0x49, 0x90, > + 0x8f, 0x98, 0xc5, 0xf0, 0x67, 0x78, 0xcc, 0xf1); > + > +static char *prog; > +static char *source = NULL; > +static char *id_file = DEFAULT_ID_FILE; > +static char nfs_unique_id[64]; > +static int replace = 0; > + > +static void usage(void) > +{ > + fprintf(stderr, "usage: %s [-r|--replace] [-f <file> |--file <file> ] [machine]\n", prog); > +} > + > +static void fatal(const char *fmt, ...) > +{ > + int err = errno; > + va_list args; > + char fatal_msg[256] = "fatal: "; > + > + va_start(args, fmt); > + vsnprintf(&fatal_msg[7], 255, fmt, args); > + if (err) > + fprintf(stderr, "%s: %s\n", fatal_msg, strerror(err)); > + else > + fprintf(stderr, "%s\n", fatal_msg); > + exit(-1); > +} > + > +static int read_nfs_unique_id(void) > +{ > + int fd; > + ssize_t len; > + > + if (replace) { > + errno = ENOENT; > + return -1; > + } > + > + fd = open(id_file, O_RDONLY); > + if (fd < 0) > + return fd; > + len = read(fd, nfs_unique_id, 64); > + close(fd); > + return len; > +} > + > +static void write_nfs_unique_id(void) > +{ > + int fd; > + > + fd = open(id_file, O_RDWR|O_TRUNC|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); > + if (fd < 0) > + fatal("could not write id to %s", id_file); > + write(fd, nfs_unique_id, 37); > +} > + > +static void print_nfs_unique_id(void) > +{ > + fprintf(stdout, "%s", nfs_unique_id); > +} > + > +static void check_or_make_id(void) > +{ > + int ret; > + uuid_t uuid; > + > + ret = read_nfs_unique_id(); > + if (ret < 1) { > + if (errno != ENOENT ) > + fatal("reading file %s", id_file); > + uuid_generate_random(uuid); > + uuid_unparse(uuid, nfs_unique_id); > + nfs_unique_id[36] = '\n'; > + nfs_unique_id[37] = '\0'; > + write_nfs_unique_id(); > + } > + print_nfs_unique_id(); > +} > + > +static void check_or_make_id_from_machine(void) > +{ > + int fd, ret; > + char machineid[32]; > + uuid_t uuid; > + > + ret = read_nfs_unique_id(); > + if (ret < 1) { > + if (errno != ENOENT ) > + fatal("reading file %s", id_file); > + > + fd = open("/etc/machine-id", O_RDONLY); > + if (fd < 0) > + fatal("unable to read /etc/machine-id"); > + > + read(fd, machineid, 32); > + close(fd); > + > + uuid_generate_sha1(uuid, nfs4_clientid_uuid_template, machineid, 32); > + uuid_unparse(uuid, nfs_unique_id); > + nfs_unique_id[36] = '\n'; > + nfs_unique_id[37] = '\0'; > + write_nfs_unique_id(); > + } > + print_nfs_unique_id(); > +} > + > +int main(int argc, char **argv) > +{ > + prog = argv[0]; > + > + while (1) { > + int opt, prev_ind; > + int option_index = 0; > + static struct option long_options[] = { > + {"replace", no_argument, 0, 'r' }, > + {"file", required_argument, 0, 'f' }, > + { 0, 0, 0, 0 } > + }; > + > + errno = 0; > + prev_ind = optind; > + opt = getopt_long(argc, argv, ":rf:", long_options, &option_index); > + if (opt == -1) > + break; > + > + /* Let's detect missing options in the middle of an option list */ > + if (optind == prev_ind + 2 && *optarg == '-') { > + opt = ':'; > + --optind; > + } > + > + switch (opt) { > + case 'r': > + replace = 1; > + break; > + case 'f': > + id_file = optarg; > + break; > + case ':': > + usage(); > + fatal("option \"%s\" requires an argument", argv[prev_ind]); > + break; > + case '?': > + usage(); > + fatal("unexpected arg \"%s\"", argv[optind - 1]); > + break; > + } > + } > + > + argc -= optind; > + > + if (argc > 1) { > + usage(); > + fatal("Too many arguments"); > + } > + > + if (argc) > + source = argv[optind++]; > + > + if (!source) > + check_or_make_id(); > + else if (strcmp(source, "machine") == 0) > + check_or_make_id_from_machine(); > + else { > + usage(); > + fatal("unrecognized source %s\n", source); > + } > +} > diff --git a/tools/nfsuuid/nfsuuid.man b/tools/nfsuuid/nfsuuid.man > new file mode 100644 > index 000000000000..856d2f383e0f > --- /dev/null > +++ b/tools/nfsuuid/nfsuuid.man > @@ -0,0 +1,33 @@ > +.\" > +.\" nfsuuid(8) > +.\" > +.TH nfsuuid 8 "10 Feb 2022" > +.SH NAME > +nfsuuid \- Generate or return nfs client id uniquifiers > +.SH SYNOPSIS > +.B nfsuuid [ -r | --replace ] [ -f | --file <file> ] [<source>] > + > +.SH DESCRIPTION > +The > +.B nfsuuid > +command provides a simple utility to help NFS Version 4 clients use unique > +and persistent client id values. The command checks for the existence of a > +file /etc/nfsuuid and returns the first 64 chars read from that file. If > +the file is not found, a UUID is generated from the specified source and > +written to the file and returned. > +.SH OPTIONS > +.TP > +.BR \-r,\ \-\-replace > +Overwrite the <file> with a UUID generated from <source>. > +.TP > +.BR \-f,\ \-\-file > +Use the specified file to lookup or store any generated UUID. If <file> is > +not specified, the default file used is /etc/nfsuuid. > +.SH Sources > +If <source> is not specified, nfsuuid will generate a new random UUID. > + > +If <source> is "machine", nfsuuid will generate a deterministic UUID value > +derived from a sha1 hash of the contents of /etc/machine-id and a static > +key. > +.SH SEE ALSO > +.BR machine-id (5) > -- > 2.31.1 > -- Chuck Lever