From bc9e8547093beb91d2360c62a0ba324a028d229c Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 2 Oct 2014 15:49:24 +0200 Subject: [PATCH] sfdisk: add --part-attrs Signed-off-by: Karel Zak --- disk-utils/sfdisk.8 | 10 ++++++- disk-utils/sfdisk.c | 65 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/disk-utils/sfdisk.8 b/disk-utils/sfdisk.8 index f0eea948c..d2de5101f 100644 --- a/disk-utils/sfdisk.8 +++ b/disk-utils/sfdisk.8 @@ -66,6 +66,13 @@ List geometry of all or specified devices. List partitions of all or specified devices. This command can be used together with \fB\-\-verify\fR. .TP +.BR \-\-part\-attrs " " \fIdevice\fR " " \fIpartno\fR " "[\fIattrs\fR] +Change GPT partition attribute bits. If \fIattrs\fR no specified then print the current +partition setting. The \fIattrs\fR is comma delimited list of bits. The currently +supported attribute bits are: RequiredPartiton, NoBlockIOProtocol, LegacyBIOSBootable +and GUID specific bits in range from 48 to 63. For example string +"RequiredPartiton,50,51" sets three bits. +.TP .BR \-\-part\-label " " \fIdevice\fR " " \fIpartno\fR " "[\fIlabel\fR] Change GPT partition name (label). If \fIlabel\fR no specified then print the current partition label. @@ -255,7 +262,8 @@ in bytes and the size is aligned according to device I/O limits. Mark partition as bootable. .TP .B attrs= -Partition attributes. +Partition attributes, usually GPT partition attribute bits. See --part-attrs for +more details about GPT bits string format. .TP .B uuid= GPT partition UUID. diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c index b9ae1b35c..754bd6f2e 100644 --- a/disk-utils/sfdisk.c +++ b/disk-utils/sfdisk.c @@ -72,6 +72,7 @@ enum { ACT_PARTTYPE, ACT_PARTUUID, ACT_PARTLABEL, + ACT_PARTATTRS, }; struct sfdisk { @@ -821,6 +822,60 @@ static int command_partlabel(struct sfdisk *sf, int argc, char **argv) return write_changes(sf); } +/* + * sfdisk --part-attrs [] + */ +static int command_partattrs(struct sfdisk *sf, int argc, char **argv) +{ + size_t partno; + struct fdisk_partition *pa = NULL; + const char *devname = NULL, *attrs = NULL; + + if (!argc) + errx(EXIT_FAILURE, _("no disk device specified")); + devname = argv[0]; + + if (argc < 2) + errx(EXIT_FAILURE, _("no partition number specified")); + partno = strtou32_or_err(argv[1], _("failed to parse partition number")); + + if (argc == 3) + attrs = argv[2]; + else if (argc > 3) + errx(EXIT_FAILURE, _("uneexpected arguments")); + + /* read-only if name not given */ + assign_device_partition(sf, devname, partno, !attrs); + + /* print partition name */ + if (!attrs) { + const char *str; + + if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0) + str = fdisk_partition_get_attrs(pa); + if (str) + printf("%s\n", str); + fdisk_unref_partition(pa); + fdisk_deassign_device(sf->cxt, 1); + return 0; + } + + if (sf->backup) + backup_partition_table(sf, devname); + + pa = fdisk_new_partition(); + if (!pa) + err(EXIT_FAILURE, _("failed to allocate partition object")); + + if (fdisk_partition_set_attrs(pa, attrs) != 0 || + fdisk_set_partition(sf->cxt, partno - 1, pa) != 0) + errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition attributes"), + devname, partno); + + fdisk_unref_partition(pa); + return write_changes(sf); +} + static void sfdisk_print_partition(struct sfdisk *sf, size_t n) { struct fdisk_partition *pa = NULL; @@ -1240,6 +1295,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out) fputs(_(" --part-label [] print or change partition label\n"), out); fputs(_(" --part-type [] print or change partition type\n"), out); fputs(_(" --part-uuid [] print or change partition uuid\n"), out); + fputs(_(" --part-attrs [] print or change partition attributes\n"), out); fputs(USAGE_SEPARATOR, out); fputs(_(" device (usually disk) path\n"), out); @@ -1284,6 +1340,7 @@ int main(int argc, char *argv[]) OPT_PARTUUID, OPT_PARTLABEL, OPT_PARTTYPE, + OPT_PARTATTRS, }; static const struct option longopts[] = { @@ -1309,6 +1366,7 @@ int main(int argc, char *argv[]) { "part-uuid", no_argument, NULL, OPT_PARTUUID }, { "part-label", no_argument, NULL, OPT_PARTLABEL }, { "part-type", no_argument, NULL, OPT_PARTTYPE }, + { "part-attrs", no_argument, NULL, OPT_PARTATTRS }, { "unit", required_argument, NULL, 'u' }, /* deprecated */ { "Linux", no_argument, NULL, 'L' }, /* deprecated */ @@ -1410,6 +1468,9 @@ int main(int argc, char *argv[]) case OPT_PARTLABEL: sf->act = ACT_PARTLABEL; break; + case OPT_PARTATTRS: + sf->act = ACT_PARTATTRS; + break; case OPT_NOREREAD: sf->noreread = 1; break; @@ -1471,6 +1532,10 @@ int main(int argc, char *argv[]) rc = command_partlabel(sf, argc - optind, argv + optind); break; + case ACT_PARTATTRS: + rc = command_partattrs(sf, argc - optind, argv + optind); + break; + } sfdisk_deinit(sf);