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.
24 open Virt_df_gettext.Gettext
27 open Virt_df_lvm2_metadata
29 let plugin_name = "LVM2"
32 let sector_size64 = 512L
34 (* Probe to see if it's an LVM2 PV. *)
35 let rec probe_pv lvm_plugin_id dev =
37 let uuid, _ = read_pv_label dev in
39 eprintf "LVM2 detected PV UUID %s\n%!" uuid;
40 { lvm_plugin_id = lvm_plugin_id; pv_uuid = uuid }
42 if debug then prerr_endline (Printexc.to_string exn);
45 and read_pv_label dev =
46 (* Load the first 8 sectors. I found by experimentation that
47 * the second sector contains the header ("LABELONE" etc) and
48 * the nineth sector contains some additional information about
49 * the location of the current metadata.
51 let bits = dev#read_bitstring 0L (9 * sector_size) in
53 (*Bitmatch.hexdump_bitstring stdout bits;*)
56 | sector0 : sector_size*8 : bitstring; (* sector 0 *)
57 labelone : 8*8 : bitstring; (* "LABELONE" *)
58 padding : 16*8 : bitstring; (* Seems to contain something. *)
59 lvm2_ver : 8*8 : bitstring; (* "LVM2 001" *)
60 uuid : 32*8 : bitstring; (* UUID *)
61 padding2 : (sector_size-64)*8 : bitstring; (* to end of second sector *)
62 sector234567 : sector_size*8 * 6 : bitstring; (* sectors 2-6 *)
63 padding3 : 0x28*8 : bitstring; (* start of sector 8 *)
64 metadata_offset : 32 : littleendian;(* metadata offset *)
65 padding4 : 4*8 : bitstring;
66 metadata_length : 32 : littleendian (* length of metadata (bytes) *)
67 when Bitmatch.string_of_bitstring labelone = "LABELONE" &&
68 Bitmatch.string_of_bitstring lvm2_ver = "LVM2 001" ->
70 (* Metadata offset is relative to end of PV label. *)
71 let metadata_offset = metadata_offset +* 0x1000_l in
72 (* Metadata length appears to include the trailing \000 which
75 let metadata_length = metadata_length -* 1_l in
77 let metadata = read_metadata dev metadata_offset metadata_length in
79 let uuid = Bitmatch.string_of_bitstring uuid in
85 (sprintf "LVM2: read_pv_label: %s: not an LVM2 physical volume" dev#name)
87 and read_metadata dev offset32 len32 =
89 eprintf "metadata: offset 0x%lx len %ld bytes\n%!" offset32 len32;
91 (* Check the offset and length are sensible. *)
93 if offset32 <= Int32.max_int then Int64.of_int32 offset32
94 else invalid_arg "LVM2: read_metadata: metadata offset too large" in
96 if len32 <= 2_147_483_647_l then Int64.of_int32 len32
97 else invalid_arg "LVM2: read_metadata: metadata length too large" in
99 if offset64 <= 0x1200L || offset64 >= dev#size
100 || len64 <= 0L || offset64 +^ len64 >= dev#size then
101 invalid_arg "LVM2: read_metadata: bad metadata offset or length";
103 (* If it is outside the disk boundaries, this will throw an exception,
104 * otherwise it will read and return the metadata string.
106 dev#read offset64 (Int64.to_int len64)
108 (* We are passed a list of devices which we previously identified
109 * as PVs belonging to us. From these produce a list of all LVs
110 * (as devices) and return them. Note that we don't try to detect
111 * what is on these LVs - that will be done in the main code.
113 let rec list_lvs devs =
114 (* Read the UUID and metadata (again) from each device to end up with
115 * an assoc list of PVs, keyed on the UUID.
117 let pvs = List.map read_pv_label devs in
119 (* Parse the metadata using the external lexer/parser. *)
121 fun (uuid, metadata) ->
122 eprintf "parsing: %s\n<<<<\n" metadata;
123 uuid, Virt_df_lvm2_lexer.parse_lvm2_metadata_from_string metadata
126 (* Print the parsed metadata. *)
128 fun (uuid, metadata) ->
129 eprintf "metadata for UUID %s:\n" uuid;
130 output_metadata stderr metadata
141 (* Register with main code. *)
143 lvm_type_register plugin_name probe_pv list_lvs