0005-sfdisk-add-disk-id-to-change-disk-UUID-ID.patch 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. From 65e27d545cb54ac63536a8b6e7d5def180ddb5b7 Mon Sep 17 00:00:00 2001
  2. From: Karel Zak <kzak@redhat.com>
  3. Date: Tue, 14 Jan 2020 11:50:46 +0100
  4. Subject: [PATCH] sfdisk: add --disk-id to change disk UUID/ID
  5. Addresses: https://github.com/karelzak/util-linux/issues/916
  6. Signed-off-by: Karel Zak <kzak@redhat.com>
  7. Signed-off-by: Carlos Santos <unixmania@gmail.com>
  8. ---
  9. disk-utils/sfdisk.8 | 5 +++++
  10. disk-utils/sfdisk.c | 54 +++++++++++++++++++++++++++++++++++++++++++++
  11. 2 files changed, 59 insertions(+)
  12. diff --git a/disk-utils/sfdisk.8 b/disk-utils/sfdisk.8
  13. index 3ff5dd4e6..9ee71e81a 100644
  14. --- a/disk-utils/sfdisk.8
  15. +++ b/disk-utils/sfdisk.8
  16. @@ -152,6 +152,11 @@ or a GUID for GPT. For backward compatibility the options \fB\-c\fR and
  17. Change the GPT partition UUID. If \fIuuid\fR is not specified,
  18. then print the current partition UUID.
  19. .TP
  20. +.BR "\-\-disk\-id \fIdevice " [ \fIid ]
  21. +Change the disk identifier. If \fIid\fR is not specified,
  22. +then print the current identifier. The identifier is UUID for GPT
  23. +or unsigned integer for MBR.
  24. +.TP
  25. .BR \-r , " \-\-reorder " \fIdevice
  26. Renumber the partitions, ordering them by their start offset.
  27. .TP
  28. diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c
  29. index bb6e1c6df..0db797b2d 100644
  30. --- a/disk-utils/sfdisk.c
  31. +++ b/disk-utils/sfdisk.c
  32. @@ -86,6 +86,7 @@ enum {
  33. ACT_PARTUUID,
  34. ACT_PARTLABEL,
  35. ACT_PARTATTRS,
  36. + ACT_DISKID,
  37. ACT_DELETE
  38. };
  39. @@ -1327,6 +1328,46 @@ static int command_partattrs(struct sfdisk *sf, int argc, char **argv)
  40. return write_changes(sf);
  41. }
  42. +/*
  43. + * sfdisk --disk-id <device> [<str>]
  44. + */
  45. +static int command_diskid(struct sfdisk *sf, int argc, char **argv)
  46. +{
  47. + const char *devname = NULL;
  48. + char *str = NULL;
  49. +
  50. + if (!argc)
  51. + errx(EXIT_FAILURE, _("no disk device specified"));
  52. + devname = argv[0];
  53. +
  54. + if (argc == 2)
  55. + str = argv[1];
  56. + else if (argc > 2)
  57. + errx(EXIT_FAILURE, _("unexpected arguments"));
  58. +
  59. + if (fdisk_assign_device(sf->cxt, devname, !str) != 0)
  60. + err(EXIT_FAILURE, _("cannot open %s"), devname);
  61. +
  62. + /* print */
  63. + if (!str) {
  64. + fdisk_get_disklabel_id(sf->cxt, &str);
  65. + if (str)
  66. + printf("%s\n", str);
  67. + free(str);
  68. + fdisk_deassign_device(sf->cxt, 1);
  69. + return 0;
  70. + }
  71. +
  72. + /* change */
  73. + if (sf->backup)
  74. + backup_partition_table(sf, devname);
  75. +
  76. + if (fdisk_set_disklabel_id_from_string(sf->cxt, str) != 0)
  77. + errx(EXIT_FAILURE, _("%s: failed to set disklabel ID"), devname);
  78. +
  79. + return write_changes(sf);
  80. +}
  81. +
  82. static void sfdisk_print_partition(struct sfdisk *sf, size_t n)
  83. {
  84. struct fdisk_partition *pa = NULL;
  85. @@ -1941,6 +1982,9 @@ static void __attribute__((__noreturn__)) usage(void)
  86. fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out);
  87. fputs(_(" --part-attrs <dev> <part> [<str>] print or change partition attributes\n"), out);
  88. + fputs(USAGE_SEPARATOR, out);
  89. + fputs(_(" --disk-id <dev> [<str>] print or change disk label ID (UUID)\n"), out);
  90. +
  91. fputs(USAGE_SEPARATOR, out);
  92. fputs(_(" <dev> device (usually disk) path\n"), out);
  93. fputs(_(" <part> partition number\n"), out);
  94. @@ -2007,6 +2051,7 @@ int main(int argc, char *argv[])
  95. OPT_PARTLABEL,
  96. OPT_PARTTYPE,
  97. OPT_PARTATTRS,
  98. + OPT_DISKID,
  99. OPT_BYTES,
  100. OPT_COLOR,
  101. OPT_MOVEDATA,
  102. @@ -2052,6 +2097,8 @@ int main(int argc, char *argv[])
  103. { "part-type", no_argument, NULL, OPT_PARTTYPE },
  104. { "part-attrs", no_argument, NULL, OPT_PARTATTRS },
  105. + { "disk-id", no_argument, NULL, OPT_DISKID },
  106. +
  107. { "show-pt-geometry", no_argument, NULL, 'G' }, /* deprecated */
  108. { "unit", required_argument, NULL, 'u' }, /* deprecated */
  109. { "Linux", no_argument, NULL, 'L' }, /* deprecated */
  110. @@ -2192,6 +2239,9 @@ int main(int argc, char *argv[])
  111. case OPT_PARTATTRS:
  112. sf->act = ACT_PARTATTRS;
  113. break;
  114. + case OPT_DISKID:
  115. + sf->act = ACT_DISKID;
  116. + break;
  117. case OPT_NOREREAD:
  118. sf->noreread = 1;
  119. break;
  120. @@ -2296,6 +2346,10 @@ int main(int argc, char *argv[])
  121. rc = command_partattrs(sf, argc - optind, argv + optind);
  122. break;
  123. + case ACT_DISKID:
  124. + rc = command_diskid(sf, argc - optind, argv + optind);
  125. + break;
  126. +
  127. case ACT_REORDER:
  128. rc = command_reorder(sf, argc - optind, argv + optind);
  129. break;
  130. --
  131. 2.18.2