[parted-devel] [PATCH 2/3] parted: rework 'parted print' so that it works with empty disk
Petr Uzel
petr.uzel at suse.cz
Fri Mar 11 12:50:00 UTC 2011
'parted $dev print' on a device without a disk label used to fail
with an 'unrecognised disk label' error, without printing any useful
information about the disk which are not dependent on the disk label
(disk model, transport, size, sector size, BIOS geometry).
With this patch, parted prints all these information (BIOS geometry only
if 'unit cyl' is specified) and reports success even if the disk does
not have any valid disk label.
As a side efect, 'parted $dev print devices/all/list' now prints
information about all the devices, even if $dev does not have a disk
label.
* parted/parted.c (_print_disk_info): New function.
(do_print): do not immediately fail if the disk label can not be
rocognized, but print disk information and report success instead.
* NEWS (Changes in behavior): Mention it.
Signed-off-by: Petr Uzel <petr.uzel at suse.cz>
---
NEWS | 4 ++
parted/parted.c | 125 ++++++++++++++++++++++++++++++-------------------------
2 files changed, 72 insertions(+), 57 deletions(-)
diff --git a/NEWS b/NEWS
index fc44462..bbcb144 100644
--- a/NEWS
+++ b/NEWS
@@ -40,6 +40,10 @@ GNU parted NEWS -*- outline -*-
libparted: zero-length devices (other than files) are ignored rather than
throwing an exception.
+** Changes in behavior
+
+ "parted $dev print" now prints some information about the device (model,
+ transport, size, sector size) even if it can not recognize the disk label.
* Noteworthy changes in release 2.3 (2010-05-28) [stable]
diff --git a/parted/parted.c b/parted/parted.c
index 08ab4aa..35bfaf3 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -1402,11 +1402,61 @@ _print_disk_geometry (const PedDevice *dev)
free (cyl_size);
}
+static void
+_print_disk_info (const PedDevice *dev, const PedDisk *disk)
+{
+ char *const transport[] = {"unknown", "scsi", "ide", "dac960",
+ "cpqarray", "file", "ataraid", "i2o",
+ "ubd", "dasd", "viodasd", "sx8", "dm",
+ "xvd", "sd/mmc", "virtblk", "aoe",
+ "md"};
+
+ char* start = ped_unit_format (dev, 0);
+ PedUnit default_unit = ped_unit_get_default ();
+ char* end = ped_unit_format_byte (dev, dev->length * dev->sector_size
+ - (default_unit == PED_UNIT_CHS ||
+ default_unit == PED_UNIT_CYLINDER));
+
+ const char* pt_name = disk ? disk->type->name : "unknown";
+
+ if (opt_machine_mode) {
+ switch (default_unit) {
+ case PED_UNIT_CHS: puts ("CHS;");
+ break;
+ case PED_UNIT_CYLINDER: puts ("CYL;");
+ break;
+ default: puts ("BYT;");
+ break;
+
+ }
+ printf ("%s:%s:%s:%lld:%lld:%s:%s;\n",
+ dev->path, end, transport[dev->type],
+ dev->sector_size, dev->phys_sector_size,
+ pt_name, dev->model);
+ } else {
+ printf (_("Model: %s (%s)\n"),
+ dev->model, transport[dev->type]);
+ printf (_("Disk %s: %s\n"), dev->path, end);
+ printf (_("Sector size (logical/physical): %lldB/%lldB\n"),
+ dev->sector_size, dev->phys_sector_size);
+ }
+
+ free (start);
+ free (end);
+
+ if (ped_unit_get_default () == PED_UNIT_CHS
+ || ped_unit_get_default () == PED_UNIT_CYLINDER)
+ _print_disk_geometry (dev);
+
+ if (!opt_machine_mode) {
+ printf (_("Partition Table: %s\n"), pt_name);
+ }
+}
+
static int
do_print (PedDevice** dev)
{
- PedUnit default_unit;
- PedDisk* disk;
+ PedDisk* disk = NULL;
Table* table;
int has_extended;
int has_name;
@@ -1414,11 +1464,6 @@ do_print (PedDevice** dev)
int has_free_arg = 0;
int has_list_arg = 0;
int has_num_arg = 0;
- const char *const transport[] = {"unknown", "scsi", "ide", "dac960",
- "cpqarray", "file", "ataraid", "i2o",
- "ubd", "dasd", "viodasd", "sx8", "dm",
- "xvd", "sd/mmc", "virtblk", "aoe",
- "md"};
char* peek_word;
char* start;
char* end;
@@ -1427,15 +1472,6 @@ do_print (PedDevice** dev)
char* tmp;
wchar_t* table_rendered;
- disk = ped_disk_new (*dev);
- if (!disk)
- goto error;
-
- if (ped_disk_is_flag_available(disk, PED_DISK_CYLINDER_ALIGNMENT))
- if (!ped_disk_set_flag(disk, PED_DISK_CYLINDER_ALIGNMENT,
- alignment == ALIGNMENT_CYLINDER))
- goto error_destroy_disk;
-
peek_word = command_line_peek_word ();
if (peek_word) {
if (strncmp (peek_word, "devices", 7) == 0) {
@@ -1460,6 +1496,14 @@ do_print (PedDevice** dev)
free (peek_word);
}
+ if (!has_devices_arg && !has_list_arg)
+ disk = ped_disk_new (*dev);
+ if (disk &&
+ ped_disk_is_flag_available(disk, PED_DISK_CYLINDER_ALIGNMENT))
+ if (!ped_disk_set_flag(disk, PED_DISK_CYLINDER_ALIGNMENT,
+ alignment == ALIGNMENT_CYLINDER))
+ goto error_destroy_disk;
+
if (has_devices_arg) {
char* dev_name;
PedDevice* current_dev = NULL;
@@ -1491,7 +1535,7 @@ do_print (PedDevice** dev)
else if (has_list_arg)
return _print_list ();
- else if (has_num_arg) {
+ else if (disk && has_num_arg) {
PedPartition* part = NULL;
int status = 0;
if (command_line_get_partition ("", disk, &part))
@@ -1500,52 +1544,17 @@ do_print (PedDevice** dev)
return status;
}
- start = ped_unit_format (*dev, 0);
- default_unit = ped_unit_get_default ();
- end = ped_unit_format_byte (*dev, (*dev)->length * (*dev)->sector_size
- - (default_unit == PED_UNIT_CHS ||
- default_unit == PED_UNIT_CYLINDER));
-
- if (opt_machine_mode) {
- switch (default_unit) {
- case PED_UNIT_CHS: puts ("CHS;");
- break;
- case PED_UNIT_CYLINDER: puts ("CYL;");
- break;
- default: puts ("BYT;");
- break;
-
- }
- printf ("%s:%s:%s:%lld:%lld:%s:%s;\n",
- (*dev)->path, end, transport[(*dev)->type],
- (*dev)->sector_size, (*dev)->phys_sector_size,
- disk->type->name, (*dev)->model);
- } else {
- printf (_("Model: %s (%s)\n"),
- (*dev)->model, transport[(*dev)->type]);
- printf (_("Disk %s: %s\n"), (*dev)->path, end);
- printf (_("Sector size (logical/physical): %lldB/%lldB\n"),
- (*dev)->sector_size, (*dev)->phys_sector_size);
- }
-
- free (start);
- free (end);
-
- if (ped_unit_get_default () == PED_UNIT_CHS
- || ped_unit_get_default () == PED_UNIT_CYLINDER)
- _print_disk_geometry (*dev);
-
- if (!opt_machine_mode) {
- printf (_("Partition Table: %s\n"), disk->type->name);
- putchar ('\n');
- }
+ _print_disk_info (*dev, disk);
+ if (!disk)
+ goto nopt;
+ if (!opt_machine_mode)
+ putchar ('\n');
has_extended = ped_disk_type_check_feature (disk->type,
PED_DISK_TYPE_EXTENDED);
has_name = ped_disk_type_check_feature (disk->type,
PED_DISK_TYPE_PARTITION_NAME);
-
PedPartition* part;
if (!opt_machine_mode) {
StrList *row1;
@@ -1707,6 +1716,8 @@ error_destroy_disk:
ped_disk_destroy (disk);
error:
return 0;
+nopt:
+ return 1;
}
static int
--
1.7.3.4
More information about the parted-devel
mailing list