sfdisk: add --reorder

Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1232707
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2015-09-02 14:49:36 +02:00
parent cd150a429d
commit 9a17d9460a
3 changed files with 63 additions and 2 deletions

View File

@ -110,6 +110,9 @@ 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, Change the GPT partition UUID. If \fIuuid\fR is not specified,
then print the current partition UUID. then print the current partition UUID.
.TP .TP
.BR \-r , " \-\-reorder " \fIdevice
Fix partitions order by start offset.
.TP
.BR \-s , " \-\-show\-size " [ \fIdevice ...] .BR \-s , " \-\-show\-size " [ \fIdevice ...]
List the sizes of all or the specified devices. List the sizes of all or the specified devices.
.TP .TP

View File

@ -73,6 +73,7 @@ enum {
ACT_LIST, ACT_LIST,
ACT_LIST_FREE, ACT_LIST_FREE,
ACT_LIST_TYPES, ACT_LIST_TYPES,
ACT_REORDER,
ACT_SHOW_SIZE, ACT_SHOW_SIZE,
ACT_SHOW_GEOM, ACT_SHOW_GEOM,
ACT_VERIFY, ACT_VERIFY,
@ -643,6 +644,35 @@ static int command_activate(struct sfdisk *sf, int argc, char **argv)
return rc; return rc;
} }
/*
* sfdisk --reorder <device>
*/
static int command_reorder(struct sfdisk *sf, int argc, char **argv)
{
const char *devname = NULL;
int rc;
if (argc)
devname = argv[0];
if (!devname)
errx(EXIT_FAILURE, _("no disk device specified"));
rc = fdisk_assign_device(sf->cxt, devname, 0); /* read-write */
if (rc)
err(EXIT_FAILURE, _("cannot open %s"), devname);
if (sf->backup)
backup_partition_table(sf, devname);
if (fdisk_reorder_partitions(sf->cxt) == 1) /* unchnaged */
rc = fdisk_deassign_device(sf->cxt, 1);
else
rc = write_changes(sf);
return rc;
}
/* /*
* sfdisk --dump <device> * sfdisk --dump <device>
*/ */
@ -1436,6 +1466,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out); fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out);
fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out); fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out);
fputs(_(" -F, --list-free [<dev> ...] list unpartitions free areas of each device\n"), out); fputs(_(" -F, --list-free [<dev> ...] list unpartitions free areas of each device\n"), out);
fputs(_(" -r, --reorder <dev> fix partitions order (by start offset)\n"), out);
fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out); fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out);
fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out); fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out);
fputs(_(" -V, --verify [<dev> ...] test whether partitions seem correct\n"), out); fputs(_(" -V, --verify [<dev> ...] test whether partitions seem correct\n"), out);
@ -1525,6 +1556,7 @@ int main(int argc, char *argv[])
{ "no-reread", no_argument, NULL, OPT_NOREREAD }, { "no-reread", no_argument, NULL, OPT_NOREREAD },
{ "output", required_argument, NULL, 'o' }, { "output", required_argument, NULL, 'o' },
{ "partno", required_argument, NULL, 'N' }, { "partno", required_argument, NULL, 'N' },
{ "reorder", no_argument, NULL, 'r' },
{ "show-size", no_argument, NULL, 's' }, { "show-size", no_argument, NULL, 's' },
{ "show-geometry", no_argument, NULL, 'g' }, { "show-geometry", no_argument, NULL, 'g' },
{ "quiet", no_argument, NULL, 'q' }, { "quiet", no_argument, NULL, 'q' },
@ -1551,7 +1583,7 @@ int main(int argc, char *argv[])
textdomain(PACKAGE); textdomain(PACKAGE);
atexit(close_stdout); atexit(close_stdout);
while ((c = getopt_long(argc, argv, "aAbcdfFghJlLo:O:nN:qsTu:vVX:Y:", while ((c = getopt_long(argc, argv, "aAbcdfFghJlLo:O:nN:qrsTu:vVX:Y:",
longopts, &longidx)) != -1) { longopts, &longidx)) != -1) {
switch(c) { switch(c) {
case 'A': case 'A':
@ -1614,6 +1646,9 @@ int main(int argc, char *argv[])
case 'q': case 'q':
sf->quiet = 1; sf->quiet = 1;
break; break;
case 'r':
sf->act = ACT_REORDER;
break;
case 's': case 's':
sf->act = ACT_SHOW_SIZE; sf->act = ACT_SHOW_SIZE;
break; break;
@ -1734,6 +1769,9 @@ int main(int argc, char *argv[])
rc = command_partattrs(sf, argc - optind, argv + optind); rc = command_partattrs(sf, argc - optind, argv + optind);
break; break;
case ACT_REORDER:
rc = command_reorder(sf, argc - optind, argv + optind);
break;
} }
sfdisk_deinit(sf); sfdisk_deinit(sf);

View File

@ -97,7 +97,6 @@ ${TS_DEVICE}2 : start=8192, size=6144, uuid=44b51def-5f04-465a-91aa-2889a62d8e4
${TS_DEVICE}3 : start=14336, size=6144, uuid=643e1d0d-bc02-4ced-b83b-86121062858f ${TS_DEVICE}3 : start=14336, size=6144, uuid=643e1d0d-bc02-4ced-b83b-86121062858f
${TS_DEVICE}4 : start=20480, size=81887,uuid=d2a29b0a-fdee-40c3-9bae-b9fa782c986c, name="GPT is the best" ${TS_DEVICE}4 : start=20480, size=81887,uuid=d2a29b0a-fdee-40c3-9bae-b9fa782c986c, name="GPT is the best"
EOF EOF
ts_fdisk_clean $TS_DEVICE
udevadm settle udevadm settle
$TS_CMD_SFDISK --list -o START,END,SIZE,UUID,TYPE,NAME ${TS_DEVICE} >> $TS_OUTPUT 2>&1 $TS_CMD_SFDISK --list -o START,END,SIZE,UUID,TYPE,NAME ${TS_DEVICE} >> $TS_OUTPUT 2>&1
ts_fdisk_clean $TS_DEVICE ts_fdisk_clean $TS_DEVICE
@ -136,5 +135,26 @@ udevadm settle
ts_finalize_subtest ts_finalize_subtest
ts_init_subtest "reorder"
$TS_CMD_WIPEFS -a ${TS_DEVICE} &> /dev/null
udevadm settle
$TS_CMD_SFDISK ${TS_DEVICE} >> $TS_OUTPUT 2>&1 <<EOF
unit: sectors
label: gpt
label-id: 3b8559db-33af-43e9-befc-c331d829b539
${TS_DEVICE}1 : start=8192, size=6144
${TS_DEVICE}2 : start=2048, size=6144
${TS_DEVICE}3 : start=20480, size=81887
${TS_DEVICE}4 : start=14336, size=6144
EOF
udevadm settle
$TS_CMD_SFDISK --reorder ${TS_DEVICE} >> $TS_OUTPUT 2>&1
$TS_CMD_SFDISK --list ${TS_DEVICE} >> $TS_OUTPUT 2>&1
ts_fdisk_clean $TS_DEVICE
udevadm settle
ts_finalize_subtest
rmmod scsi_debug rmmod scsi_debug
ts_finalize ts_finalize