Allow the user to specify the filesystem UUID, similar to mkfs.ext4's -U
option.

Signed-off-by: Andrew Price <anpr...@redhat.com>
---
 gfs2/man/mkfs.gfs2.8  |  6 ++++++
 gfs2/mkfs/main_mkfs.c | 19 +++++++++++++++++--
 tests/mkfs.at         | 10 ++++++++++
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/gfs2/man/mkfs.gfs2.8 b/gfs2/man/mkfs.gfs2.8
index 58742dea..0ddc074e 100644
--- a/gfs2/man/mkfs.gfs2.8
+++ b/gfs2/man/mkfs.gfs2.8
@@ -123,6 +123,12 @@ unique file system name used to distinguish this gfs2 file 
system.  Valid
 \fIclustername\fRs and \fIlockspace\fRs may only contain alphanumeric
 characters, hyphens (-) and underscores (_).
 .TP
+\fB-U\fP \fIUUID\fR
+Specify the filesystem UUID. The argument must be string of hexadecimal digits
+separated by hyphens, of the form "1b4e28ba-2fa1-11d2-883f-b9a761bde3fb". If
+this option is omitted, the filesystem's UUID is randomly generated. Note that
+no attempt is made to prevent UUID clashes between filesystems on the system.
+.TP
 \fB-V\fP
 Print program version information, then exit.
 .TP
diff --git a/gfs2/mkfs/main_mkfs.c b/gfs2/mkfs/main_mkfs.c
index 37ed5d08..f552f934 100644
--- a/gfs2/mkfs/main_mkfs.c
+++ b/gfs2/mkfs/main_mkfs.c
@@ -52,6 +52,7 @@ static void print_usage(const char *prog_name)
            "-q", NULL,          _("Don't print anything"),
            "-r", _("<size>"),   _("Size of resource groups, in megabytes"),
            "-t", _("<name>"),   _("Name of the lock table"),
+           "-U", _("<UUID>"),   _("The UUID of the file system"),
            "-V", NULL,          _("Display program version information, then 
exit"),
            NULL, NULL, NULL /* Must be kept at the end */
        };
@@ -121,6 +122,7 @@ struct mkfs_opts {
        int journals;
        const char *lockproto;
        const char *locktable;
+       const char *uuid;
        struct mkfs_dev dev;
        unsigned discard:1;
 
@@ -137,6 +139,7 @@ struct mkfs_opts {
        unsigned got_device:1;
        unsigned got_topol:1;
        unsigned got_format:1;
+       unsigned got_uuid:1;
 
        unsigned override:1;
        unsigned quiet:1;
@@ -352,7 +355,7 @@ static int opts_get(int argc, char *argv[], struct 
mkfs_opts *opts)
        int ret;
        int c;
        while (1) {
-               c = getopt(argc, argv, "-b:c:DhJ:j:KOo:p:qr:t:V");
+               c = getopt(argc, argv, "-b:c:DhJ:j:KOo:p:qr:t:U:V");
                if (c == -1)
                        break;
 
@@ -405,6 +408,10 @@ static int opts_get(int argc, char *argv[], struct 
mkfs_opts *opts)
                        if (ret != 0)
                                return ret;
                        break;
+               case 'U':
+                       opts->uuid = optarg;
+                       opts->got_uuid = 1;
+                       break;
                case 'V':
                        printf("mkfs.gfs2 %s (built %s %s)\n", VERSION,
                               __DATE__, __TIME__);
@@ -1062,7 +1069,15 @@ static int sbd_init(struct lgfs2_sbd *sdp, struct 
mkfs_opts *opts, unsigned bsiz
        sdp->sd_multihost_format = GFS2_FORMAT_MULTI;
        sdp->sd_bsize = bsize;
        sdp->sd_bsize_shift = ffs(bsize) - 1;
-       uuid_generate(sdp->sd_uuid);
+
+       if (opts->got_uuid) {
+               int err = uuid_parse(opts->uuid, sdp->sd_uuid);
+               if (err != 0) {
+                       fprintf(stderr, _("Failed to parse UUID option."));
+                       return -1;
+               }
+       } else
+               uuid_generate(sdp->sd_uuid);
 
        if (lgfs2_compute_constants(sdp)) {
                perror(_("Failed to compute file system constants"));
diff --git a/tests/mkfs.at b/tests/mkfs.at
index 09a3ed29..298daf3d 100644
--- a/tests/mkfs.at
+++ b/tests/mkfs.at
@@ -188,3 +188,13 @@ GFS_TGT_SIZE(64M)
 AT_CHECK([$GFS_MKFS -p lock_nolock -j2 $GFS_TGT], 0, [ignore], [ignore])
 AT_CHECK([fsck.gfs2 -n $GFS_TGT], 0, [ignore], [ignore])
 AT_CLEANUP
+
+AT_SETUP([UUID option])
+AT_KEYWORDS(mkfs.gfs2 mkfs)
+GFS_TGT_REGEN
+AT_CHECK([$GFS_MKFS -p lock_nolock $GFS_TGT -U], 255, [ignore], [ignore])
+AT_CHECK([$GFS_MKFS -p lock_nolock -U 42 $GFS_TGT], 255, [ignore], [ignore])
+AT_CHECK([$GFS_MKFS -p lock_nolock -U 1b4e28ba-2fa1-11d2-883f-b9a761bde3fb4 
$GFS_TGT], 255, [ignore], [ignore])
+AT_CHECK([$GFS_MKFS -p lock_nolock -U 1b4e28ba-2fa1-11d2-883f-b9a761bde3f 
$GFS_TGT], 255, [ignore], [ignore])
+GFS_FSCK_CHECK([$GFS_MKFS -p lock_nolock -U 
1b4e28ba-2fa1-11d2-883f-b9a761bde3fb $GFS_TGT])
+AT_CLEANUP
-- 
2.36.1

Reply via email to