sfdisk: add --part-attrs

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2014-10-02 15:49:24 +02:00
parent c77ba5316f
commit bc9e854709
2 changed files with 74 additions and 1 deletions

View File

@ -66,6 +66,13 @@ List geometry of all or specified devices.
List partitions of all or specified devices. This command can be used List partitions of all or specified devices. This command can be used
together with \fB\-\-verify\fR. together with \fB\-\-verify\fR.
.TP .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] .BR \-\-part\-label " " \fIdevice\fR " " \fIpartno\fR " "[\fIlabel\fR]
Change GPT partition name (label). If \fIlabel\fR no specified then print the current Change GPT partition name (label). If \fIlabel\fR no specified then print the current
partition label. partition label.
@ -255,7 +262,8 @@ in bytes and the size is aligned according to device I/O limits.
Mark partition as bootable. Mark partition as bootable.
.TP .TP
.B attrs=<string> .B attrs=<string>
Partition attributes. Partition attributes, usually GPT partition attribute bits. See --part-attrs for
more details about GPT bits string format.
.TP .TP
.B uuid=<string> .B uuid=<string>
GPT partition UUID. GPT partition UUID.

View File

@ -72,6 +72,7 @@ enum {
ACT_PARTTYPE, ACT_PARTTYPE,
ACT_PARTUUID, ACT_PARTUUID,
ACT_PARTLABEL, ACT_PARTLABEL,
ACT_PARTATTRS,
}; };
struct sfdisk { struct sfdisk {
@ -821,6 +822,60 @@ static int command_partlabel(struct sfdisk *sf, int argc, char **argv)
return write_changes(sf); return write_changes(sf);
} }
/*
* sfdisk --part-attrs <device> <partno> [<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) static void sfdisk_print_partition(struct sfdisk *sf, size_t n)
{ {
struct fdisk_partition *pa = NULL; struct fdisk_partition *pa = NULL;
@ -1240,6 +1295,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_(" --part-label <dev> <part> [<str>] print or change partition label\n"), out); fputs(_(" --part-label <dev> <part> [<str>] print or change partition label\n"), out);
fputs(_(" --part-type <dev> <part> [<type>] print or change partition type\n"), out); fputs(_(" --part-type <dev> <part> [<type>] print or change partition type\n"), out);
fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out); 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(USAGE_SEPARATOR, out);
fputs(_(" <dev> device (usually disk) path\n"), out); fputs(_(" <dev> device (usually disk) path\n"), out);
@ -1284,6 +1340,7 @@ int main(int argc, char *argv[])
OPT_PARTUUID, OPT_PARTUUID,
OPT_PARTLABEL, OPT_PARTLABEL,
OPT_PARTTYPE, OPT_PARTTYPE,
OPT_PARTATTRS,
}; };
static const struct option longopts[] = { static const struct option longopts[] = {
@ -1309,6 +1366,7 @@ int main(int argc, char *argv[])
{ "part-uuid", no_argument, NULL, OPT_PARTUUID }, { "part-uuid", no_argument, NULL, OPT_PARTUUID },
{ "part-label", no_argument, NULL, OPT_PARTLABEL }, { "part-label", no_argument, NULL, OPT_PARTLABEL },
{ "part-type", no_argument, NULL, OPT_PARTTYPE }, { "part-type", no_argument, NULL, OPT_PARTTYPE },
{ "part-attrs", no_argument, NULL, OPT_PARTATTRS },
{ "unit", required_argument, NULL, 'u' }, /* deprecated */ { "unit", required_argument, NULL, 'u' }, /* deprecated */
{ "Linux", no_argument, NULL, 'L' }, /* deprecated */ { "Linux", no_argument, NULL, 'L' }, /* deprecated */
@ -1410,6 +1468,9 @@ int main(int argc, char *argv[])
case OPT_PARTLABEL: case OPT_PARTLABEL:
sf->act = ACT_PARTLABEL; sf->act = ACT_PARTLABEL;
break; break;
case OPT_PARTATTRS:
sf->act = ACT_PARTATTRS;
break;
case OPT_NOREREAD: case OPT_NOREREAD:
sf->noreread = 1; sf->noreread = 1;
break; break;
@ -1471,6 +1532,10 @@ int main(int argc, char *argv[])
rc = command_partlabel(sf, argc - optind, argv + optind); rc = command_partlabel(sf, argc - optind, argv + optind);
break; break;
case ACT_PARTATTRS:
rc = command_partattrs(sf, argc - optind, argv + optind);
break;
} }
sfdisk_deinit(sf); sfdisk_deinit(sf);