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 (* Read method checks which segment the request lies inside and
60 * maps it to the underlying device. If there is no mapping then
61 * we have to return an error.
63 * The request must lie inside a single extent, otherwise this is
64 * also an error (XXX - should lift this restriction, however default
65 * extent size is 4 MB so we probably won't hit this very often).
67 method read offset len =
68 let offset_in_extents = offset /^ extent_size in
70 (* Check we don't cross an extent boundary. *)
71 if (offset +^ Int64.of_int (len-1)) /^ extent_size <> offset_in_extents
72 then invalid_arg "linear_map_device: request crosses extent boundary";
74 if offset_in_extents < 0L || offset_in_extents >= size_in_extents then
75 invalid_arg "linear_map_device: read outside device";
77 let rec loop = function
79 invalid_arg "linear_map_device: offset not mapped"
80 | (start_extent, end_extent, dev, pvoffset) :: rest ->
81 if start_extent <= offset_in_extents &&
82 offset_in_extents < end_extent
83 then dev#read (offset +^ pvoffset *^ extent_size) len
89 (*----------------------------------------------------------------------*)
90 (* Probe to see if it's an LVM2 PV. *)
91 let rec probe lvm_plugin_id dev =
93 let uuid, _ = read_pv_label dev in
95 eprintf "LVM2 detected PV UUID %s\n%!" uuid;
96 { lvm_plugin_id = lvm_plugin_id; pv_uuid = uuid }
98 if !debug then prerr_endline (Printexc.to_string exn);
101 and read_pv_label dev =
102 (* Load the first 8 sectors. I found by experimentation that
103 * the second sector contains the header ("LABELONE" etc) and
104 * the nineth sector contains some additional information about
105 * the location of the current metadata.
107 let bits = dev#read_bitstring 0L (9 * sector_size) in
109 (*Bitmatch.hexdump_bitstring stdout bits;*)
114 sector0 : sector_size*8 : bitstring;
117 "LABELONE" : 64 : string; (* "LABELONE" *)
118 _ : 128 : bitstring; (* Seems to contain something. *)
119 "LVM2 001" : 64 : string; (* "LVM2 001" *)
120 uuid : 256 : string; (* UUID *)
121 endsect : (sector_size-64)*8 : bitstring; (* to end of second sector *)
124 sectors234567 : sector_size*8 * 6 : bitstring;
127 _ : 320 : bitstring; (* start of sector 8 *)
128 metadata_offset : 32 : littleendian; (* metadata offset *)
130 metadata_length : 32 : littleendian (* length of metadata (bytes) *)
133 (* Metadata offset is relative to end of PV label. *)
134 let metadata_offset = metadata_offset +* 0x1000_l in
135 (* Metadata length appears to include the trailing \000 which
138 let metadata_length = metadata_length -* 1_l in
140 let metadata = read_metadata dev metadata_offset metadata_length in
146 (sprintf "LVM2: read_pv_label: %s: not an LVM2 physical volume"
149 and read_metadata dev offset32 len32 =
151 eprintf "metadata: offset 0x%lx len %ld bytes\n%!" offset32 len32;
153 (* Check the offset and length are sensible. *)
155 if offset32 <= Int32.max_int then Int64.of_int32 offset32
156 else invalid_arg "LVM2: read_metadata: metadata offset too large" in
158 if len32 <= 2_147_483_647_l then Int64.of_int32 len32
159 else invalid_arg "LVM2: read_metadata: metadata length too large" in
161 if offset64 <= 0x1200L || offset64 >= dev#size
162 || len64 <= 0L || offset64 +^ len64 >= dev#size then
163 invalid_arg "LVM2: read_metadata: bad metadata offset or length";
165 (* If it is outside the disk boundaries, this will throw an exception,
166 * otherwise it will read and return the metadata string.
168 dev#read offset64 (Int64.to_int len64)
170 (*----------------------------------------------------------------------*)
171 (* We are passed a list of devices which we previously identified
172 * as PVs belonging to us. From these produce a list of all LVs
173 * (as devices) and return them. Note that we don't try to detect
174 * what is on these LVs - that will be done in the main code.
177 (* Read the UUID and metadata (again) from each device to end up with
178 * an assoc list of PVs, keyed on the UUID.
182 let uuid, metadata = read_pv_label dev in
183 (uuid, (metadata, dev))
186 (* Parse the metadata using the external lexer/parser. *)
188 fun (uuid, (metadata, dev)) ->
189 uuid, (Diskimage_lvm2_lexer.parse_lvm2_metadata_from_string metadata,
193 (* Print the parsed metadata. *)
196 fun (uuid, (metadata, dev)) ->
197 eprintf "metadata for PV UUID %s on %s:\n" uuid dev#name;
198 output_metadata stderr metadata
201 (* Scan for volume groups. The first entry in the metadata
202 * appears to be the volume group name. This gives us a
203 * list of VGs and the metadata for each underlying PV.
208 | pvuuid, (((vgname, Metadata vgmeta) :: _), dev) ->
209 Some (vgname, (pvuuid, vgmeta))
213 let cmp ((a:string),_) ((b:string),_) = compare a b in
214 let vgnames = List.sort ~cmp vgnames in
215 let vgs = group_by vgnames in
217 (* Note that the metadata is supposed to be duplicated
218 * identically across all PVs (for redundancy purposes).
219 * In theory we should check this and use the 'seqno'
220 * field to find the latest metadata if it doesn't match,
221 * but in fact we don't check this.
224 fun (vgname, metas) ->
225 let pvuuids = List.map fst metas in
226 let _, vgmeta = List.hd metas in (* just pick any metadata *)
227 vgname, (pvuuids, vgmeta)) vgs in
232 fun (vgname, (pvuuids, vgmeta)) ->
233 eprintf "VG %s is on PVs: %s\n%!" vgname (String.concat "," pvuuids)
236 (* Some useful getter functions. If these can't get a value
237 * from the metadata or if the type is wrong they raise Not_found.
239 let rec get_int64 field meta =
240 match List.assoc field meta with
242 | _ -> raise Not_found
243 and get_int field meta min max =
244 match List.assoc field meta with
245 | Int i when Int64.of_int min <= i && i <= Int64.of_int max ->
247 | _ -> raise Not_found
248 and get_string field meta =
249 match List.assoc field meta with
251 | _ -> raise Not_found
252 and get_meta field meta =
253 match List.assoc field meta with
255 | _ -> raise Not_found
256 and get_stripes field meta = (* List of (string,int) pairs. *)
257 match List.assoc field meta with
259 let rec loop = function
261 | String pvname :: Int offset :: xs ->
262 (pvname, offset) :: loop xs
263 | _ -> raise Not_found
266 | _ -> raise Not_found
269 (* The volume groups refer to the physical volumes using their
270 * own naming system ("pv0", "pv1", etc.) instead of PV UUIDs.
272 * Each PV also has a start (in sectors) & count (in extents)
273 * of the writable area (the bit after the superblock and metadata)
274 * which normally starts at sector 384.
276 * Create a PV device (simple offset + size) and a map from PV
277 * names to these devices.
280 fun (vgname, (pvuuids, vgmeta)) ->
281 let pvdevs, extent_size =
283 (* NB: extent_size is in sectors here - we convert to bytes. *)
284 let extent_size = get_int "extent_size" vgmeta 0 (1024*1024) in
285 let extent_size = Int64.of_int extent_size *^ sector_size64 in
287 (* Get the physical_volumes section of the metadata. *)
288 let pvdevs = get_meta "physical_volumes" vgmeta in
292 | (pvname, Metadata meta) ->
294 let pvuuid = get_string "id" meta in
295 let pvuuid = canonical_uuid pvuuid in
297 (* Get the underlying physical device. *)
298 let _, dev = List.assoc pvuuid pvs in
300 (* Construct a PV device. *)
301 let pe_start = get_int64 "pe_start" meta in
302 let pe_start = pe_start *^ sector_size64 in
303 let pe_count = get_int64 "pe_count" meta in
304 let pe_count = pe_count *^ extent_size in
305 let pvdev = new offset_device pvuuid pe_start pe_count dev in
310 ) pvdevs, extent_size
312 (* Something went wrong - just return an empty map. *)
313 Not_found -> [], 0L in
314 (vgname, (pvuuids, vgmeta, pvdevs, extent_size))
317 (* Scan for logical volumes. Each VG contains several LVs.
318 * This gives us a list of LVs within each VG (hence extends
322 fun (vgname, (pvuuids, vgmeta, pvdevs, extent_size)) ->
325 let lvs = get_meta "logical_volumes" vgmeta in
326 let lvs = List.filter_map (
328 | lvname, Metadata lvmeta ->
330 let segment_count = get_int "segment_count" lvmeta 0 1024 in
332 (* Get the segments for this LV. *)
333 let segments = range 1 (segment_count+1) in
336 (fun i -> get_meta ("segment" ^ string_of_int i) lvmeta)
343 get_int64 "start_extent" segmeta in
345 get_int64 "extent_count" segmeta in
346 let segtype = get_string "type" segmeta in
348 (* Can only handle striped segments at the
351 if segtype <> "striped" then raise Not_found;
354 get_int "stripe_count" segmeta 0 1024 in
355 let stripes = get_stripes "stripes" segmeta in
357 if List.length stripes <> stripe_count then
360 (* Can only handle linear striped segments at
363 if stripe_count <> 1 then raise Not_found;
364 let pvname, pvoffset = List.hd stripes in
366 (start_extent, extent_count, pvname, pvoffset)
369 Some (lvname, segments)
371 (* Something went wrong with segments - omit this LV. *)
379 (* Something went wrong - assume no LVs found. *)
381 (vgname, (pvuuids, vgmeta, pvdevs, extent_size, lvs))
387 fun (vgname, (pvuuids, vgmeta, pvdevs, extent_size, lvs)) ->
388 eprintf "VG %s: (extent_size = %Ld bytes)\n" vgname extent_size;
390 fun (lvname, segments) ->
391 eprintf " %s/%s:\n" vgname lvname;
393 fun (start_extent, extent_count, pvname, pvoffset) ->
394 eprintf " start %Ld count %Ld at %s:%Ld\n"
395 start_extent extent_count pvname pvoffset
402 (* Finally we can set up devices for the LVs. *)
405 fun (vgname, (pvuuid, vgmeta, pvdevs, extent_size, lvs)) ->
408 fun (lvname, segments) ->
409 let name = vgname ^ "/" ^ lvname in
410 let segments = List.map (
411 fun (start_extent, extent_count, pvname, pvoffset) ->
412 (* Get the PV device. *)
413 let pvdev = List.assoc pvname pvdevs in
415 (* Extents mapped to: *)
416 (start_extent, extent_count, pvdev, pvoffset)
419 (* Create a linear mapping device. *)
420 let lv_dev = new linear_map_device name extent_size segments in
427 let lvs = List.concat lvs in
429 (* Return the list of LV devices. *)