Use tables of callbacks for the functions.
[virt-df.git] / lib / diskimage.ml
index f2b5823..6c2ef01 100644 (file)
@@ -34,21 +34,21 @@ let disk_block_size = ~^512
 (* The plug-ins. *)
 let partition_types = [
   Diskimage_mbr.plugin_id,
-    ("MBR", Diskimage_mbr.probe);
+    ("MBR", Diskimage_mbr.callbacks);
 ]
 
 let filesystem_types = [
   Diskimage_ext2.plugin_id,
-    ("Linux ext2/3", Diskimage_ext2.probe);
+    ("Linux ext2/3", Diskimage_ext2.callbacks);
   Diskimage_linux_swap.plugin_id,
-    ("Linux swap", Diskimage_linux_swap.probe);
+    ("Linux swap", Diskimage_linux_swap.callbacks);
   Diskimage_linux_swsuspend.plugin_id,
-    ("Linux s/w suspend", Diskimage_linux_swsuspend.probe);
+    ("Linux s/w suspend", Diskimage_linux_swsuspend.callbacks);
 ]
 
 let lvm_types = [
   Diskimage_lvm2.plugin_id,
-    ("Linux LVM2", Diskimage_lvm2.probe, Diskimage_lvm2.list);
+    ("Linux LVM2", Diskimage_lvm2.callbacks);
 ]
 
 let name_of_parts id =
@@ -58,7 +58,7 @@ let name_of_filesystem id =
   let name, _ = List.assoc id filesystem_types in
   name
 let name_of_lvm id =
-  let name, _, _ = List.assoc id lvm_types in
+  let name, _ = List.assoc id lvm_types in
   name
 
 (* Probe a device for partitions.  Returns [Some parts] or [None]. *)
@@ -66,8 +66,8 @@ let probe_for_partitions dev =
   if !debug then eprintf "probing for partitions on %s ...\n%!" dev#name;
   let rec loop = function
     | [] -> None
-    | (parts_plugin_id, (_, probe_fn)) :: rest ->
-       try Some (probe_fn dev)
+    | (parts_plugin_id, (_, cb)) :: rest ->
+       try Some (cb.parts_cb_probe dev)
        with Not_found -> loop rest
   in
   let r = loop partition_types in
@@ -80,13 +80,17 @@ let probe_for_partitions dev =
   );
   r
 
+let parts_offset_is_free ({ parts_plugin_id = parts_name } as parts) offset =
+  let _, cb = List.assoc parts_name partition_types in
+  cb.parts_cb_offset_is_free parts offset
+
 (* Probe a device for a filesystem.  Returns [Some fs] or [None]. *)
 let probe_for_filesystem dev =
   if !debug then eprintf "probing for a filesystem on %s ...\n%!" dev#name;
   let rec loop = function
     | [] -> None
-    | (fs_name, (_, probe_fn)) :: rest ->
-       try Some (probe_fn dev)
+    | (fs_name, (_, cb)) :: rest ->
+       try Some (cb.fs_cb_probe dev)
        with Not_found -> loop rest
   in
   let r = loop filesystem_types in
@@ -99,13 +103,17 @@ let probe_for_filesystem dev =
   );
   r
 
+let fs_offset_is_free ({ fs_plugin_id = fs_name } as fs) offset =
+  let _, cb = List.assoc fs_name filesystem_types in
+  cb.fs_cb_offset_is_free fs offset
+
 (* Probe a device for a PV.  Returns [Some lvm_name] or [None]. *)
 let probe_for_pv dev =
   if !debug then eprintf "probing if %s is a PV ...\n%!" dev#name;
   let rec loop = function
     | [] -> None
-    | (lvm_name, (_, probe_fn, _)) :: rest ->
-       try Some (probe_fn lvm_name dev)
+    | (lvm_name, (_, cb)) :: rest ->
+       try Some (cb.lvm_cb_probe lvm_name dev)
        with Not_found -> loop rest
   in
   let r = loop lvm_types in
@@ -118,8 +126,12 @@ let probe_for_pv dev =
   r
 
 let list_lvs lvm_name devs =
-  let _, _, list_lvs_fn = List.assoc lvm_name lvm_types in
-  list_lvs_fn devs
+  let _, cb = List.assoc lvm_name lvm_types in
+  cb.lvm_cb_list_lvs devs
+
+let lvm_offset_is_free ({ lvm_plugin_id = lvm_name } as pv) offset =
+  let _, cb = List.assoc lvm_name lvm_types in
+  cb.lvm_cb_offset_is_free pv offset
 
 (*----------------------------------------------------------------------*)
 (* Create machine description. *)
@@ -700,3 +712,15 @@ let get_owners_lookup machine ownership (disk : block_device) =
     List.map (
       fun (owner, owner_offset) -> (owner, offset -^ owner_offset)
     ) owners
+
+(* Find out if a disk offset is free.
+ * Current algorithm just checks that at least one owner says
+ * it is free.  We could be smarter about this.
+ *)
+let offset_is_free owners =
+  List.exists (
+    function
+    | `Filesystem fs, offset -> fs_offset_is_free fs offset
+    | `Partitions parts, offset -> parts_offset_is_free parts offset
+    | `PhysicalVolume pv, offset -> lvm_offset_is_free pv offset
+  ) owners