On Friday, 14 July 2017 15:39:28 CEST Richard W.M. Jones wrote:
+let print_partition_table ~add_m_option device =
+ udev_settle ();
+
+ let args = ref [] in
+ if add_m_option then push_back args "-m";
+ push_back args "-s";
+ push_back args "--";
+ push_back args device;
+ push_back args "unit";
+ push_back args "b";
+ push_back args "print";
+
+ let out =
+ try command "parted" !args
+ with
+ (* Translate "unrecognised disk label" into an errno code. *)
+ Failure str when String.find str "unrecognised disk label" >= 0
->
+ raise (Unix.Unix_error (Unix.EINVAL, "parted", device ^ ": "
^ str)) in
+
+ udev_settle ();
+
+ (* Split the output into lines. *)
+ let out = String.trim out in
+ let lines = String.nsplit "\n" out in
+
+ (* lines[0] is "BYT;", lines[1] is the device line which we ignore,
+ * lines[2..] are the partitions themselves.
+ *)
+ match lines with
+ | "BYT;" :: _ :: lines -> lines
+ | [] | [_] ->
+ failwith "too few rows of output from 'parted print' command"
+ | _ ->
+ failwith "did not see 'BYT;' magic value in 'parted print'
command"
Note the first two lines with "BYT;", and the device name must be
filtered only when running in machine-parseable mode, otherwise the
match above will fail (since the output is very different in
non-machine-parseable mode).
The other option is making this function always use -m, and implement a
separate print_partition_table only in case part_get_mbr_part_type is
ported to OCaml (since it's the single user of it).
diff --git a/daemon/parted.mli b/daemon/parted.mli
index 33eb6d30d..057d7e8c7 100644
--- a/daemon/parted.mli
+++ b/daemon/parted.mli
@@ -16,4 +16,12 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*)
+type partition = {
+ part_num : int32;
+ part_start : int64;
+ part_end : int64;
+ part_size : int64;
+}
Is this needed? Could Structs.partition be used below?
--
Pino Toscano