Restructure library for dealing with block mappings.
[virt-df.git] / lib / diskimage.ml
1 (* Diskimage library for reading disk images.
2    (C) Copyright 2007-2008 Richard W.M. Jones, Red Hat Inc.
3    http://libvirt.org/
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *)
19
20 open Printf
21 open ExtList
22 open Unix
23
24 include Diskimage_utils
25
26 (* Use as the natural block size for disk images, but really we should
27  * use the 'blockdev -getbsz' command to find the real block size.
28  *)
29 let disk_block_size = 512
30
31 let partition_types = [
32   Diskimage_mbr.plugin_id,
33     ("MBR", Diskimage_mbr.probe);
34 ]
35
36 let filesystem_types = [
37   Diskimage_ext2.plugin_id,
38     ("Linux ext2/3", Diskimage_ext2.probe);
39   Diskimage_linux_swap.plugin_id,
40     ("Linux swap", Diskimage_linux_swap.probe);
41 ]
42
43 let lvm_types = [
44   Diskimage_lvm2.plugin_id,
45     ("Linux LVM2", Diskimage_lvm2.probe, Diskimage_lvm2.list);
46 ]
47
48 let name_of_parts id =
49   let name, _ = List.assoc id partition_types in
50   name
51 let name_of_filesystem id =
52   let name, _ = List.assoc id filesystem_types in
53   name
54 let name_of_lvm id =
55   let name, _, _ = List.assoc id lvm_types in
56   name
57
58 (* Probe a device for partitions.  Returns [Some parts] or [None]. *)
59 let probe_for_partitions dev =
60   if !debug then eprintf "probing for partitions on %s ...\n%!" dev#name;
61   let rec loop = function
62     | [] -> None
63     | (parts_plugin_id, (_, probe_fn)) :: rest ->
64         try Some (probe_fn dev)
65         with Not_found -> loop rest
66   in
67   let r = loop partition_types in
68   if !debug then (
69     match r with
70     | None -> eprintf "no partitions found on %s\n%!" dev#name
71     | Some { parts_plugin_id = name; parts = parts } ->
72         eprintf "found %d %s partitions on %s\n"
73           (List.length parts) name dev#name
74   );
75   r
76
77 (* Probe a device for a filesystem.  Returns [Some fs] or [None]. *)
78 let probe_for_filesystem dev =
79   if !debug then eprintf "probing for a filesystem on %s ...\n%!" dev#name;
80   let rec loop = function
81     | [] -> None
82     | (fs_name, (_, probe_fn)) :: rest ->
83         try Some (probe_fn dev)
84         with Not_found -> loop rest
85   in
86   let r = loop filesystem_types in
87   if !debug then (
88     match r with
89     | None -> eprintf "no filesystem found on %s\n%!" dev#name
90     | Some fs ->
91         eprintf "found a filesystem on %s:\n" dev#name;
92         eprintf "\t%s\n%!" fs.fs_plugin_id
93   );
94   r
95
96 (* Probe a device for a PV.  Returns [Some lvm_name] or [None]. *)
97 let probe_for_pv dev =
98   if !debug then eprintf "probing if %s is a PV ...\n%!" dev#name;
99   let rec loop = function
100     | [] -> None
101     | (lvm_name, (_, probe_fn, _)) :: rest ->
102         try Some (probe_fn lvm_name dev)
103         with Not_found -> loop rest
104   in
105   let r = loop lvm_types in
106   if !debug then (
107     match r with
108     | None -> eprintf "no PV found on %s\n%!" dev#name
109     | Some { lvm_plugin_id = name } ->
110         eprintf "%s contains a %s PV\n%!" dev#name name
111   );
112   r
113
114 let list_lvs lvm_name devs =
115   let _, _, list_lvs_fn = List.assoc lvm_name lvm_types in
116   list_lvs_fn devs
117
118 (* Create machine description. *)
119 let open_machine name disks =
120   let disks = List.map (
121     fun (name, path) ->
122       let dev = new block_device path disk_block_size (* XXX *) in
123       { d_name = name; d_dev = dev; d_content = `Unknown }
124   ) disks in
125   { m_name = name; m_disks = disks; m_lv_filesystems = [] }
126
127 let close_machine { m_disks = m_disks } =
128   (* Only close the disks, assume all other devices are derived from them. *)
129   List.iter (fun { d_dev = d_dev } -> d_dev#close ()) m_disks
130
131 let scan_machine ({ m_disks = m_disks } as machine) =
132   let m_disks = List.map (
133     fun ({ d_dev = dev } as disk) ->
134       let dev = (dev :> device) in
135       (* See if it is partitioned first. *)
136       let parts = probe_for_partitions dev in
137       match parts with
138       | Some parts ->
139           { disk with d_content = `Partitions parts }
140       | None ->
141           (* Not partitioned.  Does it contain a filesystem? *)
142           let fs = probe_for_filesystem dev in
143           match fs with
144           | Some fs ->
145               { disk with d_content = `Filesystem fs }
146           | None ->
147               (* Not partitioned, no filesystem, is it a PV? *)
148               let pv = probe_for_pv dev in
149               match pv with
150               | Some lvm_name ->
151                   { disk with d_content = `PhysicalVolume lvm_name }
152               | None ->
153                   disk (* Spare/unknown. *)
154   ) m_disks in
155
156   (* Now we have either detected partitions or a filesystem on each
157    * physical device (or perhaps neither).  See what is on those
158    * partitions.
159    *)
160   let m_disks = List.map (
161     function
162     | ({ d_dev = dev; d_content = `Partitions parts } as disk) ->
163         let ps = List.map (
164           fun p ->
165             if p.part_status = Bootable || p.part_status = Nonbootable then (
166               let fs = probe_for_filesystem p.part_dev in
167               match fs with
168               | Some fs ->
169                   { p with part_content = `Filesystem fs }
170               | None ->
171                   (* Is it a PV? *)
172                   let pv = probe_for_pv p.part_dev in
173                   match pv with
174                   | Some lvm_name ->
175                       { p with part_content = `PhysicalVolume lvm_name }
176                   | None ->
177                       p (* Spare/unknown. *)
178             ) else p
179         ) parts.parts in
180         let parts = { parts with parts = ps } in
181         { disk with d_content = `Partitions parts }
182     | disk -> disk
183   ) m_disks in
184
185   (* LVM filesystem detection
186    *
187    * Look for all disks/partitions which have been identified as PVs
188    * and pass those back to the respective LVM plugin for LV detection.
189    *
190    * (Note - a two-stage process because an LV can be spread over
191    * several PVs, so we have to detect all PVs belonging to a
192    * domain first).
193    *
194    * XXX To deal with RAID (ie. md devices) we will need to loop
195    * around here because RAID is like LVM except that they normally
196    * present as block devices which can be used by LVM.
197    *)
198   (* First: LV detection.
199    * Find all physical volumes, can be disks or partitions.
200    *)
201   let pvs_on_disks = List.filter_map (
202     function
203     | { d_dev = d_dev;
204         d_content = `PhysicalVolume pv } -> Some (pv, (d_dev :> device))
205     | _ -> None
206   ) m_disks in
207   let pvs_on_partitions = List.map (
208     function
209     | { d_content = `Partitions { parts = parts } } ->
210         List.filter_map (
211           function
212           | { part_dev = part_dev;
213               part_content = `PhysicalVolume pv } ->
214               Some (pv, part_dev)
215           | _ -> None
216         ) parts
217     | _ -> []
218   ) m_disks in
219   let lvs = List.concat (pvs_on_disks :: pvs_on_partitions) in
220
221   (* Second: filesystem on LV detection.
222    * Group the LVs by plug-in type.
223    *)
224   let cmp (a,_) (b,_) = compare a b in
225   let lvs = List.sort ~cmp lvs in
226   let lvs = group_by lvs in
227
228   let lvs =
229     List.map (fun (pv, devs) -> list_lvs pv.lvm_plugin_id devs) lvs in
230   let lvs = List.concat lvs in
231
232   (* lvs is a list of potential LV devices.  Now run them through the
233    * probes to see if any contain filesystems.
234    *)
235   let filesystems =
236     List.filter_map (
237       fun ({ lv_dev = dev } as lv) ->
238         match probe_for_filesystem dev with
239         | Some fs -> Some (lv, fs)
240         | None -> None
241     ) lvs in
242
243   { machine with
244       m_disks = m_disks;
245       m_lv_filesystems = filesystems }