314586e780d33793acfa43807de5c448dc968159
[virt-top.git] / virt-df / virt_df_lvm2.ml
1 (* 'df' command for virtual domains.
2
3    (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
4    http://libvirt.org/
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20    Support for LVM2 PVs.
21 *)
22
23 open Printf
24 open ExtList
25
26 open Virt_df_gettext.Gettext
27 open Virt_df
28
29 open Virt_df_lvm2_metadata
30
31 let plugin_name = "LVM2"
32
33 let sector_size = 512
34 let sector_size64 = 512L
35
36 (* Probe to see if it's an LVM2 PV. *)
37 let rec probe_pv lvm_plugin_id dev =
38   try
39     let uuid, _ = read_pv_label dev in
40     if !debug then
41       eprintf "LVM2 detected PV UUID %s\n%!" uuid;
42     { lvm_plugin_id = lvm_plugin_id; pv_uuid = uuid }
43   with exn ->
44     if !debug then prerr_endline (Printexc.to_string exn);
45     raise Not_found
46
47 and read_pv_label dev =
48   (* Load the first 8 sectors.  I found by experimentation that
49    * the second sector contains the header ("LABELONE" etc) and
50    * the nineth sector contains some additional information about
51    * the location of the current metadata.
52    *)
53   let bits = dev#read_bitstring 0L (9 * sector_size) in
54
55   (*Bitmatch.hexdump_bitstring stdout bits;*)
56
57   bitmatch bits with
58   | sector0 : sector_size*8 : bitstring; (* sector 0 *)
59     labelone : 8*8 : bitstring;         (* "LABELONE" *)
60     padding : 16*8 : bitstring;         (* Seems to contain something. *)
61     lvm2_ver : 8*8 : bitstring;         (* "LVM2 001" *)
62     uuid : 32*8 : bitstring;            (* UUID *)
63     padding2 : (sector_size-64)*8 : bitstring; (* to end of second sector *)
64     sector234567 : sector_size*8 * 6 : bitstring; (* sectors 2-6 *)
65     padding3 : 0x28*8 : bitstring;      (* start of sector 8 *)
66     metadata_offset : 32 : littleendian;(* metadata offset *)
67     padding4 : 4*8 : bitstring;
68     metadata_length : 32 : littleendian (* length of metadata (bytes) *)
69       when Bitmatch.string_of_bitstring labelone = "LABELONE" &&
70            Bitmatch.string_of_bitstring lvm2_ver = "LVM2 001" ->
71
72     (* Metadata offset is relative to end of PV label. *)
73     let metadata_offset = metadata_offset +* 0x1000_l in
74     (* Metadata length appears to include the trailing \000 which
75      * we don't want.
76      *)
77     let metadata_length = metadata_length -* 1_l in
78
79     let metadata = read_metadata dev metadata_offset metadata_length in
80
81     let uuid = Bitmatch.string_of_bitstring uuid in
82
83     uuid, metadata
84
85   | _ ->
86     invalid_arg
87       (sprintf "LVM2: read_pv_label: %s: not an LVM2 physical volume" dev#name)
88
89 and read_metadata dev offset32 len32 =
90   if !debug then
91     eprintf "metadata: offset 0x%lx len %ld bytes\n%!" offset32 len32;
92
93   (* Check the offset and length are sensible. *)
94   let offset64 =
95     if offset32 <= Int32.max_int then Int64.of_int32 offset32
96     else invalid_arg "LVM2: read_metadata: metadata offset too large" in
97   let len64 =
98     if len32 <= 2_147_483_647_l then Int64.of_int32 len32
99     else invalid_arg "LVM2: read_metadata: metadata length too large" in
100
101   if offset64 <= 0x1200L || offset64 >= dev#size
102     || len64 <= 0L || offset64 +^ len64 >= dev#size then
103       invalid_arg "LVM2: read_metadata: bad metadata offset or length";
104
105   (* If it is outside the disk boundaries, this will throw an exception,
106    * otherwise it will read and return the metadata string.
107    *)
108   dev#read offset64 (Int64.to_int len64)
109
110 (* We are passed a list of devices which we previously identified
111  * as PVs belonging to us.  From these produce a list of all LVs
112  * (as devices) and return them.  Note that we don't try to detect
113  * what is on these LVs - that will be done in the main code.
114  *)
115 let rec list_lvs devs =
116   (* Read the UUID and metadata (again) from each device to end up with
117    * an assoc list of PVs, keyed on the UUID.
118    *)
119   let pvs = List.map (
120     fun dev ->
121       let uuid, metadata = read_pv_label dev in
122       (uuid, (metadata, dev))
123   ) devs in
124
125   (* Parse the metadata using the external lexer/parser. *)
126   let pvs = List.map (
127     fun (uuid, (metadata, dev)) ->
128       uuid, (Virt_df_lvm2_lexer.parse_lvm2_metadata_from_string metadata,
129              dev)
130   ) pvs in
131
132   (* Print the parsed metadata.
133   List.iter (
134     fun (uuid, (metadata, dev)) ->
135       eprintf "metadata for UUID %s:\n" uuid;
136       output_metadata stderr metadata
137   ) pvs;
138   *)
139
140   (* Scan for volume groups.  The first entry in the metadata
141    * appears to be the volume group name.  This gives us a
142    * list of VGs and the metadata for each underlying PV.
143    *)
144   let vgnames =
145     List.filter_map (
146       function
147       | pvuuid, (((vgname, Metadata vgmeta) :: _), dev) ->
148           Some (vgname, (pvuuid, vgmeta))
149       | _ -> None
150     ) pvs in
151
152   let cmp ((a:string),_) ((b:string),_) = compare a b in
153   let vgnames = List.sort ~cmp vgnames in
154   let vgs = group_by vgnames in
155
156   (* Note that the metadata is supposed to be duplicated
157    * identically across all PVs (for redundancy purposes).
158    * In theory we should check this and use the 'seqno'
159    * field to find the latest metadata if it doesn't match,
160    * but in fact we don't check this.
161    *)
162   let vgs = List.map (
163     fun (vgname, metas) ->
164       let pvuuids = List.map fst metas in
165       let _, vgmeta = List.hd metas in (* just pick any metadata *)
166       vgname, (pvuuids, vgmeta)) vgs in
167
168   (* Print the VGs. *)
169   if !debug then
170     List.iter (
171       fun (vgname, (pvuuids, vgmeta)) ->
172         eprintf "VG %s is on PVs: %s\n%!" vgname (String.concat "," pvuuids)
173     ) vgs;
174
175   (* Some useful getter functions.  If these can't get a value
176    * from the metadata or if the type is wrong they raise Not_found.
177    *)
178   let rec get_int64 field meta =
179     match List.assoc field meta with
180     | Int i -> i
181     | _ -> raise Not_found
182   and get_int field meta min max =
183     match List.assoc field meta with
184     | Int i when Int64.of_int min <= i && i <= Int64.of_int max ->
185         Int64.to_int i
186     | _ -> raise Not_found
187   and get_string field meta =
188     match List.assoc field meta with
189     | String s -> s
190     | _ -> raise Not_found
191   and get_meta field meta =
192     match List.assoc field meta with
193     | Metadata md -> md
194     | _ -> raise Not_found in
195   in
196
197   (* Scan for logical volumes.  Each VG contains several LVs.
198    * This gives us a list of LVs within each VG (hence extends
199    * the vgs variable).
200    *)
201   let vgs = List.map (
202     fun (vgname, (pvuuids, vgmeta)) ->
203       let lvs =
204         try
205           let extent_size = get_int "extent_size" vgmeta 0 (256*1024) in
206           let lvs = get_meta "logical_volumes" vgmeta in
207           let lvs = List.filter_map (
208             function
209             | lvname, Metadata lvmeta ->
210                 (try
211                    let segment_count = get_int "segment_count" lvmeta 0 1024 in
212
213                    (* Get the segments for this LV. *)
214                    let segments = range 1 (segment_count+1) in
215                    let segments =
216                      List.map
217                        (fun i -> get_meta ("segment" ^ string_of_int i) lvmeta)
218                        segments in
219
220                    let segments =
221                      List.map (
222                        fun segmeta ->
223                          let start_extent =
224                            get_int64 "start_extent" segmeta in
225                          let extent_count =
226                            get_int64 "extent_count" segmeta in
227                          let segtype = get_string "type" segmeta in
228                          if segtype <> "striped" then raise Not_found;
229                          let stripe_count =
230                            get_int "stripe_count" segmeta 0 1024 in
231                          (* let stripes =  in *)
232
233                          (start_extent, extent_count, stripe_count)
234                      ) segments in
235
236                    Some (lvname, (lvmeta, segments))
237                  with
238                    (* Something went wrong with segments - omit this LV. *)
239                    Not_found -> None)
240             | _ -> None
241           ) lvs in
242
243           lvs
244         with
245           Not_found ->
246             (* Something went wrong - assume no LVs found. *)
247             [] in
248       (vgname, (pvuuids, vgmeta, lvs))
249   ) vgs in
250
251   (* Print the LVs. *)
252   if !debug then
253     List.iter (
254       fun (vgname, (pvuuids, vgmeta, lvs)) ->
255         let lvnames = List.map fst lvs in
256         eprintf "VG %s contains LVs: %s\n%!" vgname (String.concat ", " lvnames)
257     ) vgs;
258
259   []
260
261 (* Register with main code. *)
262 let () =
263   lvm_type_register plugin_name probe_pv list_lvs