diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2011-10-25 12:46:05 +0100 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2011-10-25 12:46:05 +0100 |
commit | 47412f137f2b09dbc2b44b78ba84c0b348e3f402 (patch) | |
tree | 5bfc088fc2d7099b9b32b8e4529b49f158f527fa | |
parent | 1e891d900714b84a84f9c9e79c6e033aa56c0fcc (diff) | |
download | libguestfs-47412f137f2b09dbc2b44b78ba84c0b348e3f402.tar.gz libguestfs-47412f137f2b09dbc2b44b78ba84c0b348e3f402.tar.xz libguestfs-47412f137f2b09dbc2b44b78ba84c0b348e3f402.zip |
New API: part-to-partnum
This converts a partition device name (eg. /dev/sda1) to a partition
number (eg. 1). This is useful in conjunction with the parted APIs
that mostly take a disk device + partnum.
-rw-r--r-- | daemon/devsparts.c | 25 | ||||
-rw-r--r-- | generator/generator_actions.ml | 18 | ||||
-rw-r--r-- | src/MAX_PROC_NR | 2 |
3 files changed, 43 insertions, 2 deletions
diff --git a/daemon/devsparts.c b/daemon/devsparts.c index 52a6d304..79da41b0 100644 --- a/daemon/devsparts.c +++ b/daemon/devsparts.c @@ -220,3 +220,28 @@ do_part_to_dev (const char *part) return r; } + +int +do_part_to_partnum (const char *part) +{ + int err = 1; + size_t n = strlen (part); + + while (n >= 1 && c_isdigit (part[n-1])) { + err = 0; + n--; + } + + if (err) { + reply_with_error ("device name is not a partition"); + return -1; + } + + int r; + if (sscanf (&part[n], "%d", &r) != 1) { + reply_with_error ("could not parse number"); + return -1; + } + + return r; +} diff --git a/generator/generator_actions.ml b/generator/generator_actions.ml index e8a32ac0..ce3382fa 100644 --- a/generator/generator_actions.ml +++ b/generator/generator_actions.ml @@ -5850,7 +5850,9 @@ removes the partition number, returning the device name (eg. \"/dev/sdb\"). The named partition must exist, for example as a string returned -from C<guestfs_list_partitions>."); +from C<guestfs_list_partitions>. + +See also C<guestfs_part_to_partnum>."); ("upload_offset", (RErr, [FileIn "filename"; Dev_or_Path "remotefilename"; Int64 "offset"], []), 273, [Progress], (let md5 = Digest.to_hex (Digest.file "COPYING.LIB") in @@ -6211,6 +6213,20 @@ file C<zdevice>. The C<ctype> and optional C<level> parameters have the same meaning as in C<guestfs_compress_out>."); + ("part_to_partnum", (RInt "partnum", [Device "partition"], []), 293, [], + [InitPartition, Always, TestOutputInt ( + [["part_to_partnum"; "/dev/sda1"]], 1); + InitEmpty, Always, TestLastFail ( + [["part_to_partnum"; "/dev/sda"]])], + "convert partition name to partition number", + "\ +This function takes a partition name (eg. \"/dev/sdb1\") and +returns the partition number (eg. C<1>). + +The named partition must exist, for example as a string returned +from C<guestfs_list_partitions>. + +See also C<guestfs_part_to_dev>."); ] let all_functions = non_daemon_functions @ daemon_functions diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR index f20bd4ef..4438e305 100644 --- a/src/MAX_PROC_NR +++ b/src/MAX_PROC_NR @@ -1 +1 @@ -292 +293 |