1 (* 'df' command for virtual domains.
3 (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
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.
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.
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.
27 open Diskimage_lvm2_metadata
29 let plugin_id = "LVM2"
32 let sector_size64 = 512L
34 (*----------------------------------------------------------------------*)
35 (* Block device which can do linear maps, same as the kernel dm-linear.c *)
36 class linear_map_device name extent_size segments =
37 (* The segments are passed containing (start_extent, extent_count, ...)
38 * but it's easier to deal with (start_extent, end_extent, ...) so
41 let segments = List.map
42 (fun (start_extent, extent_count, dev, pvoffset) ->
43 (start_extent, start_extent +^ extent_count, dev, pvoffset)
46 (* Calculate the size of the device (in bytes). Note that because
47 * of the random nature of the mapping this doesn't imply that we can
48 * satisfy any read request up to the full size.
52 (List.map (fun (_, end_extent, _, _) -> end_extent) segments) in
53 let size = size_in_extents *^ extent_size in
59 (* The natural blocksize for LVM devices is the extent size.
60 * NB. Throws a runtime exception if the extent size is bigger
61 * than an int (only likely to matter on 32 bit).
63 method blocksize = Int64.to_int extent_size
65 (* Map block (extent) i to the underlying device. *)
67 if i < 0L || i >= size_in_extents then
68 invalid_arg "linear_map_device: read outside device";
70 let rec loop = function
73 | (start_extent, end_extent, dev, pvoffset) :: rest ->
74 if start_extent <= i && i < end_extent then
75 [dev, (pvoffset +^ i) *^ extent_size]
81 (* NB. Use the superclass #read method. *)
84 (*----------------------------------------------------------------------*)
85 (* Probe to see if it's an LVM2 PV. *)
86 let rec probe lvm_plugin_id dev =
88 let uuid, _ = read_pv_label dev in
90 eprintf "LVM2 detected PV UUID %s\n%!" uuid;
91 { lvm_plugin_id = lvm_plugin_id; pv_uuid = uuid }
93 if !debug then prerr_endline (Printexc.to_string exn);
96 and read_pv_label dev =
97 (* Load the first 8 sectors. I found by experimentation that
98 * the second sector contains the header ("LABELONE" etc) and
99 * the nineth sector contains some additional information about
100 * the location of the current metadata.
102 let bits = dev#read_bitstring 0L (9 * sector_size) in
104 (*Bitmatch.hexdump_bitstring stdout bits;*)
109 sector0 : sector_size*8 : bitstring;
112 "LABELONE" : 64 : string; (* "LABELONE" *)
113 _ : 128 : bitstring; (* Seems to contain something. *)
114 "LVM2 001" : 64 : string; (* "LVM2 001" *)
115 uuid : 256 : string; (* UUID *)
116 endsect : (sector_size-64)*8 : bitstring; (* to end of second sector *)
119 sectors234567 : sector_size*8 * 6 : bitstring;
122 _ : 320 : bitstring; (* start of sector 8 *)
123 metadata_offset : 32 : littleendian; (* metadata offset *)
125 metadata_length : 32 : littleendian (* length of metadata (bytes) *)
128 (* Metadata offset is relative to end of PV label. *)
129 let metadata_offset = metadata_offset +* 0x1000_l in
130 (* Metadata length appears to include the trailing \000 which
133 let metadata_length = metadata_length -* 1_l in
135 let metadata = read_metadata dev metadata_offset metadata_length in
141 (sprintf "LVM2: read_pv_label: %s: not an LVM2 physical volume"
144 and read_metadata dev offset32 len32 =
146 eprintf "metadata: offset 0x%lx len %ld bytes\n%!" offset32 len32;
148 (* Check the offset and length are sensible. *)
150 if offset32 <= Int32.max_int then Int64.of_int32 offset32
151 else invalid_arg "LVM2: read_metadata: metadata offset too large" in
153 if len32 <= 2_147_483_647_l then Int64.of_int32 len32
154 else invalid_arg "LVM2: read_metadata: metadata length too large" in
156 if offset64 <= 0x1200L || offset64 >= dev#size
157 || len64 <= 0L || offset64 +^ len64 >= dev#size then
158 invalid_arg "LVM2: read_metadata: bad metadata offset or length";
160 (* If it is outside the disk boundaries, this will throw an exception,
161 * otherwise it will read and return the metadata string.
163 dev#read offset64 (Int64.to_int len64)
165 (*----------------------------------------------------------------------*)
166 (* We are passed a list of devices which we previously identified
167 * as PVs belonging to us. From these produce a list of all LVs
168 * (as devices) and return them. Note that we don't try to detect
169 * what is on these LVs - that will be done in the main code.
172 (* Read the UUID and metadata (again) from each device to end up with
173 * an assoc list of PVs, keyed on the UUID.
177 let uuid, metadata = read_pv_label dev in
178 (uuid, (metadata, dev))
181 (* Parse the metadata using the external lexer/parser. *)
183 fun (uuid, (metadata, dev)) ->
184 uuid, (Diskimage_lvm2_lexer.parse_lvm2_metadata_from_string metadata,
188 (* Print the parsed metadata. *)
191 fun (uuid, (metadata, dev)) ->
192 eprintf "metadata for PV UUID %s on %s:\n" uuid dev#name;
193 output_metadata stderr metadata
196 (* Scan for volume groups. The first entry in the metadata
197 * appears to be the volume group name. This gives us a
198 * list of VGs and the metadata for each underlying PV.
203 | pvuuid, (((vgname, Metadata vgmeta) :: _), dev) ->
204 Some (vgname, (pvuuid, vgmeta))
208 let cmp ((a:string),_) ((b:string),_) = compare a b in
209 let vgnames = List.sort ~cmp vgnames in
210 let vgs = group_by vgnames in
212 (* Note that the metadata is supposed to be duplicated
213 * identically across all PVs (for redundancy purposes).
214 * In theory we should check this and use the 'seqno'
215 * field to find the latest metadata if it doesn't match,
216 * but in fact we don't check this.
219 fun (vgname, metas) ->
220 let pvuuids = List.map fst metas in
221 let _, vgmeta = List.hd metas in (* just pick any metadata *)
222 vgname, (pvuuids, vgmeta)) vgs in
227 fun (vgname, (pvuuids, vgmeta)) ->
228 eprintf "VG %s is on PVs: %s\n%!" vgname (String.concat "," pvuuids)
231 (* Some useful getter functions. If these can't get a value
232 * from the metadata or if the type is wrong they raise Not_found.
234 let rec get_int64 field meta =
235 match List.assoc field meta with
237 | _ -> raise Not_found
238 and get_int field meta min max =
239 match List.assoc field meta with
240 | Int i when Int64.of_int min <= i && i <= Int64.of_int max ->
242 | _ -> raise Not_found
243 and get_string field meta =
244 match List.assoc field meta with
246 | _ -> raise Not_found
247 and get_meta field meta =
248 match List.assoc field meta with
250 | _ -> raise Not_found
251 and get_stripes field meta = (* List of (string,int) pairs. *)
252 match List.assoc field meta with
254 let rec loop = function
256 | String pvname :: Int offset :: xs ->
257 (pvname, offset) :: loop xs
258 | _ -> raise Not_found
261 | _ -> raise Not_found
264 (* The volume groups refer to the physical volumes using their
265 * own naming system ("pv0", "pv1", etc.) instead of PV UUIDs.
267 * Each PV also has a start (in sectors) & count (in extents)
268 * of the writable area (the bit after the superblock and metadata)
269 * which normally starts at sector 384.
271 * Create a PV device (simple offset + size) and a map from PV
272 * names to these devices.
275 fun (vgname, (pvuuids, vgmeta)) ->
276 let pvdevs, extent_size =
278 (* NB: extent_size is in sectors here - we convert to bytes. *)
279 let extent_size = get_int "extent_size" vgmeta 0 (1024*1024) in
280 let extent_size = Int64.of_int extent_size *^ sector_size64 in
282 (* Get the physical_volumes section of the metadata. *)
283 let pvdevs = get_meta "physical_volumes" vgmeta in
287 | (pvname, Metadata meta) ->
289 let pvuuid = get_string "id" meta in
290 let pvuuid = canonical_uuid pvuuid in
292 (* Get the underlying physical device. *)
293 let _, dev = List.assoc pvuuid pvs in
295 (* Construct a PV device. *)
296 let pe_start = get_int64 "pe_start" meta in
297 let pe_start = pe_start *^ sector_size64 in
298 let pe_count = get_int64 "pe_count" meta in
299 let pe_count = pe_count *^ extent_size in
303 pe_start pe_count (* start, size in bytes *)
304 (* don't really have a natural block size ... *)
305 (Int64.to_int extent_size)
306 dev (* underlying device *) in
311 ) pvdevs, extent_size
313 (* Something went wrong - just return an empty map. *)
314 Not_found -> [], 0L in
315 (vgname, (pvuuids, vgmeta, pvdevs, extent_size))
318 (* Scan for logical volumes. Each VG contains several LVs.
319 * This gives us a list of LVs within each VG (hence extends
323 fun (vgname, (pvuuids, vgmeta, pvdevs, extent_size)) ->
326 let lvs = get_meta "logical_volumes" vgmeta in
327 let lvs = List.filter_map (
329 | lvname, Metadata lvmeta ->
331 let segment_count = get_int "segment_count" lvmeta 0 1024 in
333 (* Get the segments for this LV. *)
334 let segments = range 1 (segment_count+1) in
337 (fun i -> get_meta ("segment" ^ string_of_int i) lvmeta)
344 get_int64 "start_extent" segmeta in
346 get_int64 "extent_count" segmeta in
347 let segtype = get_string "type" segmeta in
349 (* Can only handle striped segments at the
352 if segtype <> "striped" then raise Not_found;
355 get_int "stripe_count" segmeta 0 1024 in
356 let stripes = get_stripes "stripes" segmeta in
358 if List.length stripes <> stripe_count then
361 (* Can only handle linear striped segments at
364 if stripe_count <> 1 then raise Not_found;
365 let pvname, pvoffset = List.hd stripes in
367 (start_extent, extent_count, pvname, pvoffset)
370 Some (lvname, segments)
372 (* Something went wrong with segments - omit this LV. *)
380 (* Something went wrong - assume no LVs found. *)
382 (vgname, (pvuuids, vgmeta, pvdevs, extent_size, lvs))
388 fun (vgname, (pvuuids, vgmeta, pvdevs, extent_size, lvs)) ->
389 eprintf "VG %s: (extent_size = %Ld bytes)\n" vgname extent_size;
391 fun (lvname, segments) ->
392 eprintf " %s/%s:\n" vgname lvname;
394 fun (start_extent, extent_count, pvname, pvoffset) ->
395 eprintf " start %Ld count %Ld at %s:%Ld\n"
396 start_extent extent_count pvname pvoffset
403 (* Finally we can set up devices for the LVs. *)
406 fun (vgname, (pvuuid, vgmeta, pvdevs, extent_size, lvs)) ->
409 fun (lvname, segments) ->
410 let name = vgname ^ "/" ^ lvname in
411 let segments = List.map (
412 fun (start_extent, extent_count, pvname, pvoffset) ->
413 (* Get the PV device. *)
414 let pvdev = List.assoc pvname pvdevs in
416 (* Extents mapped to: *)
417 (start_extent, extent_count, pvdev, pvoffset)
420 (* Create a linear mapping device. *)
421 let lv_dev = new linear_map_device name extent_size segments in
428 let lvs = List.concat lvs in
430 (* Return the list of LV devices. *)