Associate opaque plugin ID with each major structure.
[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 let partition_types = [
27   Diskimage_mbr.plugin_id,
28     ("MBR", Diskimage_mbr.probe);
29 ]
30
31 let filesystem_types = [
32   Diskimage_ext2.plugin_id,
33     ("Linux ext2/3", Diskimage_ext2.probe);
34   Diskimage_linux_swap.plugin_id,
35     ("Linux swap", Diskimage_linux_swap.probe);
36 ]
37
38 let lvm_types = [
39   Diskimage_lvm2.plugin_id,
40     ("Linux LVM2", Diskimage_lvm2.probe, Diskimage_lvm2.list);
41 ]
42
43 let name_of_parts id =
44   let name, _ = List.assoc id partition_types in
45   name
46 let name_of_filesystem id =
47   let name, _ = List.assoc id filesystem_types in
48   name
49 let name_of_lvm id =
50   let name, _, _ = List.assoc id lvm_types in
51   name
52
53 (* Probe a device for partitions.  Returns [Some parts] or [None]. *)
54 let probe_for_partitions dev =
55   if !debug then eprintf "probing for partitions on %s ...\n%!" dev#name;
56   let rec loop = function
57     | [] -> None
58     | (parts_plugin_id, (_, probe_fn)) :: rest ->
59         try Some (probe_fn dev)
60         with Not_found -> loop rest
61   in
62   let r = loop partition_types in
63   if !debug then (
64     match r with
65     | None -> eprintf "no partitions found on %s\n%!" dev#name
66     | Some { parts_plugin_id = name; parts = parts } ->
67         eprintf "found %d %s partitions on %s\n"
68           (List.length parts) name dev#name
69   );
70   r
71
72 (* Probe a device for a filesystem.  Returns [Some fs] or [None]. *)
73 let probe_for_filesystem dev =
74   if !debug then eprintf "probing for a filesystem on %s ...\n%!" dev#name;
75   let rec loop = function
76     | [] -> None
77     | (fs_name, (_, probe_fn)) :: rest ->
78         try Some (probe_fn dev)
79         with Not_found -> loop rest
80   in
81   let r = loop filesystem_types in
82   if !debug then (
83     match r with
84     | None -> eprintf "no filesystem found on %s\n%!" dev#name
85     | Some fs ->
86         eprintf "found a filesystem on %s:\n" dev#name;
87         eprintf "\t%s\n%!" fs.fs_plugin_id
88   );
89   r
90
91 (* Probe a device for a PV.  Returns [Some lvm_name] or [None]. *)
92 let probe_for_pv dev =
93   if !debug then eprintf "probing if %s is a PV ...\n%!" dev#name;
94   let rec loop = function
95     | [] -> None
96     | (lvm_name, (_, probe_fn, _)) :: rest ->
97         try Some (probe_fn lvm_name dev)
98         with Not_found -> loop rest
99   in
100   let r = loop lvm_types in
101   if !debug then (
102     match r with
103     | None -> eprintf "no PV found on %s\n%!" dev#name
104     | Some { lvm_plugin_id = name } ->
105         eprintf "%s contains a %s PV\n%!" dev#name name
106   );
107   r
108
109 let list_lvs lvm_name devs =
110   let _, _, list_lvs_fn = List.assoc lvm_name lvm_types in
111   list_lvs_fn devs
112
113 (* Create machine description. *)
114 let open_machine name disks =
115   let disks = List.map (
116     fun (name, path) ->
117       let dev = new block_device path in
118       { d_name = name; d_dev = dev; d_content = `Unknown }
119   ) disks in
120   { m_name = name; m_disks = disks; m_lv_filesystems = [] }
121
122 let close_machine { m_disks = m_disks } =
123   (* Only close the disks, assume all other devices are derived from them. *)
124   List.iter (fun { d_dev = d_dev } -> d_dev#close ()) m_disks
125
126 let scan_machine ({ m_disks = m_disks } as machine) =
127   let m_disks = List.map (
128     fun ({ d_dev = dev } as disk) ->
129       (* See if it is partitioned first. *)
130       let parts = probe_for_partitions dev in
131       match parts with
132       | Some parts ->
133           { disk with d_content = `Partitions parts }
134       | None ->
135           (* Not partitioned.  Does it contain a filesystem? *)
136           let fs = probe_for_filesystem dev in
137           match fs with
138           | Some fs ->
139               { disk with d_content = `Filesystem fs }
140           | None ->
141               (* Not partitioned, no filesystem, is it a PV? *)
142               let pv = probe_for_pv dev in
143               match pv with
144               | Some lvm_name ->
145                   { disk with d_content = `PhysicalVolume lvm_name }
146               | None ->
147                   disk (* Spare/unknown. *)
148   ) m_disks in
149
150   (* Now we have either detected partitions or a filesystem on each
151    * physical device (or perhaps neither).  See what is on those
152    * partitions.
153    *)
154   let m_disks = List.map (
155     function
156     | ({ d_dev = dev; d_content = `Partitions parts } as disk) ->
157         let ps = List.map (
158           fun p ->
159             if p.part_status = Bootable || p.part_status = Nonbootable then (
160               let fs = probe_for_filesystem p.part_dev in
161               match fs with
162               | Some fs ->
163                   { p with part_content = `Filesystem fs }
164               | None ->
165                   (* Is it a PV? *)
166                   let pv = probe_for_pv p.part_dev in
167                   match pv with
168                   | Some lvm_name ->
169                       { p with part_content = `PhysicalVolume lvm_name }
170                   | None ->
171                       p (* Spare/unknown. *)
172             ) else p
173         ) parts.parts in
174         let parts = { parts with parts = ps } in
175         { disk with d_content = `Partitions parts }
176     | disk -> disk
177   ) m_disks in
178
179   (* LVM filesystem detection
180    *
181    * Look for all disks/partitions which have been identified as PVs
182    * and pass those back to the respective LVM plugin for LV detection.
183    *
184    * (Note - a two-stage process because an LV can be spread over
185    * several PVs, so we have to detect all PVs belonging to a
186    * domain first).
187    *
188    * XXX To deal with RAID (ie. md devices) we will need to loop
189    * around here because RAID is like LVM except that they normally
190    * present as block devices which can be used by LVM.
191    *)
192   (* First: LV detection.
193    * Find all physical volumes, can be disks or partitions.
194    *)
195   let pvs_on_disks = List.filter_map (
196     function
197     | { d_dev = d_dev;
198         d_content = `PhysicalVolume pv } -> Some (pv, d_dev)
199     | _ -> None
200   ) m_disks in
201   let pvs_on_partitions = List.map (
202     function
203     | { d_content = `Partitions { parts = parts } } ->
204         List.filter_map (
205           function
206           | { part_dev = part_dev;
207               part_content = `PhysicalVolume pv } ->
208               Some (pv, part_dev)
209           | _ -> None
210         ) parts
211     | _ -> []
212   ) m_disks in
213   let lvs = List.concat (pvs_on_disks :: pvs_on_partitions) in
214
215   (* Second: filesystem on LV detection.
216    * Group the LVs by plug-in type.
217    *)
218   let cmp (a,_) (b,_) = compare a b in
219   let lvs = List.sort ~cmp lvs in
220   let lvs = group_by lvs in
221
222   let lvs =
223     List.map (fun (pv, devs) -> list_lvs pv.lvm_plugin_id devs) lvs in
224   let lvs = List.concat lvs in
225
226   (* lvs is a list of potential LV devices.  Now run them through the
227    * probes to see if any contain filesystems.
228    *)
229   let filesystems =
230     List.filter_map (
231       fun ({ lv_dev = dev } as lv) ->
232         match probe_for_filesystem dev with
233         | Some fs -> Some (lv, fs)
234         | None -> None
235     ) lvs in
236
237   { machine with
238       m_disks = m_disks;
239       m_lv_filesystems = filesystems }