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