Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
New API: part_get_mbr_part_type for showing partition type
This patch will add support for getting partition type
of a partiton numbered device.

Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
  • Loading branch information
chenhanxiao authored and rwmjones committed Mar 24, 2015
1 parent af9aa2e commit 0c396a4
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
106 changes: 106 additions & 0 deletions daemon/parted.c
Expand Up @@ -1030,3 +1030,109 @@ do_part_get_name (const char *device, int partnum)
reply_with_error ("cannot get the partition name from '%s' layouts", parttype);
return NULL;
}

char *
do_part_get_mbr_part_type (const char *device, int partnum)
{
CLEANUP_FREE char *parttype;
char *part_type;

parttype = do_part_get_parttype (device);
if (parttype == NULL)
return NULL;

/* machine parseable output by 'parted -m' did not provide
* partition type info.
* Use traditional style.
*/
CLEANUP_FREE char *out = print_partition_table (device, PARTED_OPT_NO_M);
if (!out)
return NULL;

CLEANUP_FREE_STRING_LIST char **lines = split_lines (out);

if (!lines)
return NULL;

size_t start = 0, end = 0, row;

for (row = 0; lines[row] != NULL; ++row)
if (STRPREFIX (lines[row], "Number")) {
start = row + 1;
break;
}

if (start == 0) {
reply_with_error ("parted output has no \"Number\" line");
return NULL;
}

for (row = start; lines[row] != NULL; ++row)
if (STREQ (lines[row], "")) {
end = row;
break;
}

if (end == 0) {
reply_with_error ("parted output has no blank after end of table");
return NULL;
}

/* Now parse the lines. */
size_t i;
int64_t temp_int64;
int part_num;
char temp_type[16] = {'\0'};
for (i = 0, row = start; row < end; ++i, ++row) {
if (STREQ (parttype, "gpt")) {
memcpy (temp_type, "primary", strlen("primary"));
if (sscanf (lines[row], "%d%" SCNi64 "B%" SCNi64 "B%" SCNi64 "B",
&part_num,
&temp_int64,
&temp_int64,
&temp_int64) != 4) {
reply_with_error ("could not parse row from output of parted print command: %s", lines[row]);
return NULL;
}
} else {
if (sscanf (lines[row], "%d%" SCNi64 "B%" SCNi64 "B%" SCNi64 "B" "%15s",
&part_num,
&temp_int64,
&temp_int64,
&temp_int64,
temp_type) != 5) {
reply_with_error ("could not parse row from output of parted print command: %s", lines[row]);
return NULL;
}
}

if (part_num != partnum)
continue;

if (STRPREFIX (temp_type, "primary")) {
part_type = strdup("primary");
if (part_type == NULL)
goto error;
} else if (STRPREFIX (temp_type, "logical")) {
part_type = strdup("logical");
if (part_type == NULL)
goto error;
} else if (STRPREFIX (temp_type, "extended")) {
part_type = strdup("extended");
if (part_type == NULL)
goto error;
} else
goto error;

return part_type;
}

if (row == end) {
reply_with_error ("could not find partnum: %d", partnum);
return NULL;
}

error:
reply_with_error ("strdup failed");
return NULL;
}
26 changes: 26 additions & 0 deletions generator/actions.ml
Expand Up @@ -12549,6 +12549,32 @@ This enable skinny metadata extent refs." };
This is used to create an image of a btrfs filesystem.
All data will be zeroed, but metadata and the like is preserved." };

{ defaults with
name = "part_get_mbr_part_type";
style = RString "partitiontype", [Device "device"; Int "partnum"], [];
proc_nr = Some 454;
tests = [
InitEmpty, Always, TestResultString (
[["part_init"; "/dev/sda"; "mbr"];
["part_add"; "/dev/sda"; "p"; "64"; "204799"];
["part_add"; "/dev/sda"; "e"; "204800"; "614400"];
["part_add"; "/dev/sda"; "l"; "204864"; "205988"];
["part_get_mbr_part_type"; "/dev/sda"; "5"]], "logical"), [];
InitEmpty, Always, TestResultString (
[["part_init"; "/dev/sda"; "mbr"];
["part_add"; "/dev/sda"; "p"; "64"; "204799"];
["part_add"; "/dev/sda"; "e"; "204800"; "614400"];
["part_add"; "/dev/sda"; "l"; "204864"; "205988"];
["part_get_mbr_part_type"; "/dev/sda"; "2"]], "extended"), []
];

shortdesc = "get the MBR partition type";
longdesc = "\
This returns the partition type of an MBR partition
numbered C<partnum> on device C<device>.
It returns C<primary>, C<logical>, or C<extended>." };

]

(* Non-API meta-commands available only in guestfish.
Expand Down
2 changes: 1 addition & 1 deletion src/MAX_PROC_NR
@@ -1 +1 @@
453
454

0 comments on commit 0c396a4

Please sign in to comment.