]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
sfdisk: add --disk-id to change disk UUID/ID
authorKarel Zak <kzak@redhat.com>
Tue, 14 Jan 2020 10:50:46 +0000 (11:50 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 23 Jan 2020 14:50:28 +0000 (15:50 +0100)
Addresses: https://github.com/karelzak/util-linux/issues/916
Signed-off-by: Karel Zak <kzak@redhat.com>
disk-utils/sfdisk.8
disk-utils/sfdisk.c

index 3ff5dd4e66f5d84d26c60176afee70c82606c0fe..9ee71e81a65780a815f531c69af7f99649a7a215 100644 (file)
@@ -152,6 +152,11 @@ or a GUID for GPT.  For backward compatibility the options \fB\-c\fR and
 Change the GPT partition UUID.  If \fIuuid\fR is not specified,
 then print the current partition UUID.
 .TP
+.BR "\-\-disk\-id \fIdevice " [ \fIid ]
+Change the disk identifier.  If \fIid\fR is not specified,
+then print the current identifier.  The identifier is UUID for GPT
+or unsigned integer for MBR.
+.TP
 .BR \-r , " \-\-reorder " \fIdevice
 Renumber the partitions, ordering them by their start offset.
 .TP
index bb6e1c6df0569a5ec56c748f4ceef3a2431e83a0..0db797b2d756fa0d7e235620a794e482708249b0 100644 (file)
@@ -86,6 +86,7 @@ enum {
        ACT_PARTUUID,
        ACT_PARTLABEL,
        ACT_PARTATTRS,
+       ACT_DISKID,
        ACT_DELETE
 };
 
@@ -1327,6 +1328,46 @@ static int command_partattrs(struct sfdisk *sf, int argc, char **argv)
        return write_changes(sf);
 }
 
+/*
+ * sfdisk --disk-id <device> [<str>]
+ */
+static int command_diskid(struct sfdisk *sf, int argc, char **argv)
+{
+       const char *devname = NULL;
+       char *str = NULL;
+
+       if (!argc)
+               errx(EXIT_FAILURE, _("no disk device specified"));
+       devname = argv[0];
+
+       if (argc == 2)
+               str = argv[1];
+       else if (argc > 2)
+               errx(EXIT_FAILURE, _("unexpected arguments"));
+
+       if (fdisk_assign_device(sf->cxt, devname, !str) != 0)
+               err(EXIT_FAILURE, _("cannot open %s"), devname);
+
+       /* print */
+       if (!str) {
+               fdisk_get_disklabel_id(sf->cxt, &str);
+               if (str)
+                       printf("%s\n", str);
+               free(str);
+               fdisk_deassign_device(sf->cxt, 1);
+               return 0;
+       }
+
+       /* change */
+       if (sf->backup)
+               backup_partition_table(sf, devname);
+
+       if (fdisk_set_disklabel_id_from_string(sf->cxt, str) != 0)
+               errx(EXIT_FAILURE, _("%s: failed to set disklabel ID"), devname);
+
+       return write_changes(sf);
+}
+
 static void sfdisk_print_partition(struct sfdisk *sf, size_t n)
 {
        struct fdisk_partition *pa = NULL;
@@ -1941,6 +1982,9 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out);
        fputs(_(" --part-attrs <dev> <part> [<str>] print or change partition attributes\n"), out);
 
+       fputs(USAGE_SEPARATOR, out);
+       fputs(_(" --disk-id <dev> [<str>]           print or change disk label ID (UUID)\n"), out);
+
        fputs(USAGE_SEPARATOR, out);
        fputs(_(" <dev>                     device (usually disk) path\n"), out);
        fputs(_(" <part>                    partition number\n"), out);
@@ -2007,6 +2051,7 @@ int main(int argc, char *argv[])
                OPT_PARTLABEL,
                OPT_PARTTYPE,
                OPT_PARTATTRS,
+               OPT_DISKID,
                OPT_BYTES,
                OPT_COLOR,
                OPT_MOVEDATA,
@@ -2052,6 +2097,8 @@ int main(int argc, char *argv[])
                { "part-type",  no_argument,    NULL, OPT_PARTTYPE },
                { "part-attrs", no_argument,    NULL, OPT_PARTATTRS },
 
+               { "disk-id",    no_argument,    NULL, OPT_DISKID },
+
                { "show-pt-geometry", no_argument, NULL, 'G' },         /* deprecated */
                { "unit",    required_argument, NULL, 'u' },            /* deprecated */
                { "Linux",   no_argument,       NULL, 'L' },            /* deprecated */
@@ -2192,6 +2239,9 @@ int main(int argc, char *argv[])
                case OPT_PARTATTRS:
                        sf->act = ACT_PARTATTRS;
                        break;
+               case OPT_DISKID:
+                       sf->act = ACT_DISKID;
+                       break;
                case OPT_NOREREAD:
                        sf->noreread = 1;
                        break;
@@ -2296,6 +2346,10 @@ int main(int argc, char *argv[])
                rc = command_partattrs(sf, argc - optind, argv + optind);
                break;
 
+       case ACT_DISKID:
+               rc = command_diskid(sf, argc - optind, argv + optind);
+               break;
+
        case ACT_REORDER:
                rc = command_reorder(sf, argc - optind, argv + optind);
                break;