(** Diskimage library for reading disk images. *) (* (C) Copyright 2007-2008 Richard W.M. Jones, Red Hat Inc. http://libvirt.org/ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *) (** {2 Machine/device model} The "machine/device model" that we currently understand looks like this: {v machines | \--- host partitions / disk image files || guest block devices | +--> guest partitions (eg. using MBR) | | \-(1)->+--- filesystems (eg. ext3) | \--- PVs for LVM ||| VGs and LVs v} (1) Filesystems and PVs may also appear directly on guest block devices. Partition schemes (eg. MBR) and filesystems register themselves with this main module and they are queried first to get an idea of the physical devices, partitions and filesystems potentially available to the guest. Volume management schemes (eg. LVM2) register themselves here and are called later with "spare" physical devices and partitions to see if they contain LVM data. If this results in additional logical volumes then these are checked for filesystems. Swap space is considered to be a dumb filesystem for the purposes of this discussion. *) class virtual device : object method virtual name : string (** Return some printable name for the device. *) method virtual size : int64 (** Return the size of the device in bytes. Note: For some types of devices, the device may have "holes", alignment requirements, etc. so this method doesn't imply that every byte from [0..size-1] is readable. *) method close : unit -> unit (** Close the device. This must be called to fully free up any resources used by the device. *) method virtual read : int64 -> int -> string (** [read offset len] reads len bytes starting at offset. *) method read_bitstring : int64 -> int -> Bitmatch.bitstring (** [read_bitstring] is the same as [read] but returns a pa_bitmatch-style bitstring. *) end (** A virtual (or physical!) device, encapsulating any translation that has to be done to access the device. eg. For partitions there is a simple offset, but for LVM you may need complicated table lookups. Note this very rare use of OOP in OCaml! *) class block_device : string -> object method name : string method size : int64 method close : unit -> unit method read : int64 -> int -> string method read_bitstring : int64 -> int -> Bitmatch.bitstring end (** A concrete device which just direct-maps a file or /dev device. *) class offset_device : string -> int64 -> int64 -> device -> object method name : string method size : int64 method close : unit -> unit method read : int64 -> int -> string method read_bitstring : int64 -> int -> Bitmatch.bitstring end (** A concrete device which maps a linear part of an underlying device. [new offset_device name start size dev] creates a new device which maps bytes from [start] to [start+size-1] of the underlying device [dev] (ie. in this device they appear as bytes [0] to [size-1]). Useful for things like partitions. *) val null_device : device (** The null device. Any attempt to read generates an error. *) type machine = { m_name : string; (** Machine name. *) m_disks : disk list; (** Machine disks. *) m_lv_filesystems : (lv * filesystem) list; (** Machine LV filesystems. *) } (** A 'machine' is just a convenient holder for collections of disks. *) and disk = { d_name : string; (** Device name (eg "hda") *) d_dev : device; (** Disk device. *) d_content : disk_content; (** What's on it. *) } (** A single physical disk image. *) and disk_content = [ `Filesystem of filesystem (** Contains a direct filesystem. *) | `Partitions of partitions (** Contains partitions. *) | `PhysicalVolume of pv (** Contains an LVM PV. *) | `Unknown (** Not probed or unknown. *) ] and partitions = { parts_plugin_id : parts_plugin_id; (** Partitioning scheme. *) parts : partition list; (** Partitions. *) } and partition = { part_status : partition_status; (** Bootable, etc. *) part_type : int; (** Partition filesystem type. *) part_dev : device; (** Partition device. *) part_content : partition_content; (** What's on it. *) } (** Partitions as found on a disk image. *) and partition_status = Bootable | Nonbootable | Malformed | NullEntry and partition_content = [ `Filesystem of filesystem (** Filesystem. *) | `PhysicalVolume of pv (** Contains an LVM PV. *) | `Unknown (** Not probed or unknown. *) ] and filesystem = { fs_plugin_id : fs_plugin_id; (** Filesystem type. *) fs_block_size : int64; (** Block size (bytes). *) fs_blocks_total : int64; (** Total blocks. *) fs_is_swap : bool; (** If swap, following not valid. *) fs_blocks_reserved : int64; (** Blocks reserved for super-user. *) fs_blocks_avail : int64; (** Blocks free (available). *) fs_blocks_used : int64; (** Blocks in use. *) fs_inodes_total : int64; (** Total inodes. *) fs_inodes_reserved : int64; (** Inodes reserved for super-user. *) fs_inodes_avail : int64; (** Inodes free (available). *) fs_inodes_used : int64; (** Inodes in use. *) } (** A filesystem. *) and pv = { lvm_plugin_id : lvm_plugin_id; (** The LVM plug-in which detected this. *) pv_uuid : string; (** UUID. *) } and lv = { lv_dev : device; (** Logical volume device. *) } (** Physical and logical volumes as used by LVM plug-ins. *) and parts_plugin_id and fs_plugin_id and lvm_plugin_id (** Opaque IDs used to refer to the plug-ins. *) val name_of_parts : parts_plugin_id -> string val name_of_filesystem : fs_plugin_id -> string val name_of_lvm : lvm_plugin_id -> string (** Convert plug-in IDs to printable strings. *) (** {2 Scanning functions} *) val open_machine : string -> (string * string) list -> machine (** [open_machine m_name devs] creates a {!machine} containing the devices listed. [devs] is a list of pairs of [(name, path)] elements where [name] is something like ["hda"] and [path] is a path to a disk image or [/dev] device. This function does not do any scanning, so all disk contents are just set to [`Unknown] and there are no LV filesystems in the returned structure. *) val close_machine : machine -> unit (** This is a convenience function which calls the [dev#close] method on any open devices owned by the machine. This just has the effect of closing any file descriptors which are opened by these devices. *) val scan_machine : machine -> machine (** This does a complete scan of all devices owned by a machine, identifying all partitions, filesystems, physical and logical volumes that are known to this library. Returns an updated {!machine} structure with the scan results. *) (** {2 Debugging} *) val debug : bool ref (** If set to true, functions emit debugging information to stderr. *)