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