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

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