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