Implement 'offset_is_free'.
let size = disk#size in (* Size in bytes. *)
let nr_blocks = size /^ blocksize in (* Number of disk sectors. *)
- (* Get the lookup function for this disk. *)
- let lookup = Diskimage.get_owners_lookup machine ownership disk in
+ if !Diskimage.debug then
+ eprintf "Writing disk %s (%s sectors) ...\n%!"
+ disk#name (Int63.to_string nr_blocks);
- (* Lookup each sector. *)
- for blk = 0 to nr_blocks-1 do
- ignore (lookup blk)
- done
- ) machine.Diskimage.m_disks;
+ (* Get the lookup function for this disk. *)
+ let lookup_offset =
+ Diskimage.get_owners_lookup machine ownership disk in
+
+ (* Convenience function to look up a block and test freeness. *)
+ let block_is_free blk =
+ let offset = blk *^ blocksize in
+ Diskimage.offset_is_free (lookup_offset offset)
+ in
+
+ (* Look up owners for each sector in turn. *)
+ let rec loop blk =
+ if blk < nr_blocks then (
+ (* The current sector (blk) is either free or not free. Look
+ * for a stretch of sectors which are the same.
+ *)
+ let current_free = block_is_free blk in
+ let rec find_end blk =
+ if blk < nr_blocks then (
+ if block_is_free blk = current_free then
+ find_end (Int63.succ blk)
+ else
+ blk
+ ) else
+ nr_blocks (* End of the disk image. *)
+ in
+ let end_blk = find_end (Int63.succ blk) in
+
+ (* Current stretch is from blk .. end_blk-1. *)
+ if !Diskimage.debug then
+ eprintf " %s stretch %s to %s-1 (%s bytes)\n"
+ (if current_free then "free" else "used")
+ (Int63.to_string blk) (Int63.to_string end_blk)
+ (Int63.to_string ((end_blk-^blk) *^ blocksize));
+
+
+
+
+ loop end_blk
+ )
+ in
+ loop ~^0
+ ) machine.Diskimage.m_disks
(* 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 =
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]. *)
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
);
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
);
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
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. *)
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
creates a tree structure which allows ownership to be determined
in just a few steps. *)
+val offset_is_free : (owner * Int63.t) list -> bool
+ (** [offset_is_free owners] tests if the offset is free (unused).
+
+ If an offset is free, then it may be discarded without
+ changing the semantics of the disk image. In normal cases
+ this extends to the end of the current block, but blocksize
+ will differ for each owner, so what this really means is
+ tricky in practice. *)
+
(** {2 Debugging} *)
val debug : bool ref
| { _ } ->
raise Not_found (* Not an EXT2/3 superblock. *)
+
+let offset_is_free _ _ = false
+
+let callbacks = {
+ fs_cb_probe = probe;
+ fs_cb_offset_is_free = offset_is_free;
+}
(**/**)
val plugin_id : string
-val probe : Diskimage_utils.device -> Diskimage_utils.filesystem
+val callbacks : Diskimage_utils.fs_cb
| { _ } ->
raise Not_found (* Not Linux swapspace. *)
+
+(* Linux swap space is always 'free', apart from the superblock.
+ * Compare diskimage_linux_swsuspend.ml
+ *)
+let offset_is_free _ offset = offset >= blocksize
+
+let callbacks = {
+ fs_cb_probe = probe;
+ fs_cb_offset_is_free = offset_is_free;
+}
(**/**)
val plugin_id : string
-val probe : Diskimage_utils.device -> Diskimage_utils.filesystem
+val callbacks : Diskimage_utils.fs_cb
| { _ } ->
raise Not_found (* Not Linux software suspend. *)
+
+(* Linux software suspend image is never free.
+ * Compare diskimage_linux_swap.ml
+ *)
+let offset_is_free _ _ = false
+
+let callbacks = {
+ fs_cb_probe = probe;
+ fs_cb_offset_is_free = offset_is_free;
+}
(**/**)
val plugin_id : string
-val probe : Diskimage_utils.device -> Diskimage_utils.filesystem
+val callbacks : Diskimage_utils.fs_cb
* (as devices) and return them. Note that we don't try to detect
* what is on these LVs - that will be done in the main code.
*)
-let rec list devs =
+let rec list_lvs devs =
(* Read the UUID and metadata (again) from each device to end up with
* an assoc list of PVs, keyed on the UUID.
*)
(* Return the list of LV devices. *)
lvs
+
+(* XXX We don't currently have enough information in the PV
+ * structure to determine quickly which blocks are used. Need
+ * to store the parsed metadata in the structure ...
+ *)
+let offset_is_free _ _ = false
+
+let callbacks = {
+ lvm_cb_probe = probe;
+ lvm_cb_list_lvs = list_lvs;
+ lvm_cb_offset_is_free = offset_is_free;
+}
(**/**)
val plugin_id : string
-
-val probe :
- Diskimage_utils.lvm_plugin_id -> Diskimage_utils.device ->
- Diskimage_utils.pv
-val list : Diskimage_utils.device list -> Diskimage_utils.lv list
+val callbacks : Diskimage_utils.lvm_cb
if u32 >= 0l then i64
else Int64.add i64 0x1_0000_0000_L
*)
+
+(* XXX We don't currently keep enough data in the parts structure
+ * to allow us to reconstruct missing partition table entries.
+ *)
+let offset_is_free _ _ = false
+
+let callbacks = {
+ parts_cb_probe = probe;
+ parts_cb_offset_is_free = offset_is_free;
+}
(**/**)
val plugin_id : string
-val probe : Diskimage_utils.device -> Diskimage_utils.partitions
+val callbacks : Diskimage_utils.parts_cb
and fs_plugin_id = string
and lvm_plugin_id = string
+type parts_cb = {
+ parts_cb_probe : device -> partitions;
+ parts_cb_offset_is_free : partitions -> Int63.t -> bool;
+}
+
+type fs_cb = {
+ fs_cb_probe : device -> filesystem;
+ fs_cb_offset_is_free : filesystem -> Int63.t -> bool;
+}
+
+type lvm_cb = {
+ lvm_cb_probe : lvm_plugin_id -> device -> pv;
+ lvm_cb_list_lvs : device list -> lv list;
+ lvm_cb_offset_is_free : pv -> Int63.t -> bool;
+}
+
(* Convert a UUID (containing '-' chars) to canonical form. *)
let canonical_uuid uuid =
let uuid' = String.make 32 ' ' in
and fs_plugin_id = string
and lvm_plugin_id = string
+(** {2 Table of callbacks from each type of plug-in} *)
+
+type parts_cb = {
+ parts_cb_probe : device -> partitions;
+ parts_cb_offset_is_free : partitions -> Int63.t -> bool;
+}
+
+type fs_cb = {
+ fs_cb_probe : device -> filesystem;
+ fs_cb_offset_is_free : filesystem -> Int63.t -> bool;
+}
+
+type lvm_cb = {
+ lvm_cb_probe : lvm_plugin_id -> device -> pv;
+ lvm_cb_list_lvs : device list -> lv list;
+ lvm_cb_offset_is_free : pv -> Int63.t -> bool;
+}
+
(** {2 Internal functions used by the plug-ins} *)
val canonical_uuid : string -> string