fcf1fd2b1ffe020eee42464aa5c76f785ad653b1
[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 Virt_df_gettext.Gettext
25 open Virt_df
26
27 open Virt_df_lvm2_metadata
28
29 let plugin_name = "LVM2"
30
31 let sector_size = 512
32 let sector_size64 = 512L
33
34 (* Probe to see if it's an LVM2 PV. *)
35 let rec probe_pv lvm_plugin_id dev =
36   try
37     let uuid, _ = read_pv_label dev in
38     if debug then
39       eprintf "LVM2 detected PV UUID %s\n%!" uuid;
40     { lvm_plugin_id = lvm_plugin_id; pv_uuid = uuid }
41   with exn ->
42     if debug then prerr_endline (Printexc.to_string exn);
43     raise Not_found
44
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.
50    *)
51   let bits = dev#read_bitstring 0L (9 * sector_size) in
52
53   (*Bitmatch.hexdump_bitstring stdout bits;*)
54
55   bitmatch bits with
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" ->
69
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
73      * we don't want.
74      *)
75     let metadata_length = metadata_length -* 1_l in
76
77     let metadata = read_metadata dev metadata_offset metadata_length in
78
79     let uuid = Bitmatch.string_of_bitstring uuid in
80
81     uuid, metadata
82
83   | _ ->
84     invalid_arg
85       (sprintf "LVM2: read_pv_label: %s: not an LVM2 physical volume" dev#name)
86
87 and read_metadata dev offset32 len32 =
88   if debug then
89     eprintf "metadata: offset 0x%lx len %ld bytes\n%!" offset32 len32;
90
91   (* Check the offset and length are sensible. *)
92   let offset64 =
93     if offset32 <= Int32.max_int then Int64.of_int32 offset32
94     else invalid_arg "LVM2: read_metadata: metadata offset too large" in
95   let len64 =
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
98
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";
102
103   (* If it is outside the disk boundaries, this will throw an exception,
104    * otherwise it will read and return the metadata string.
105    *)
106   dev#read offset64 (Int64.to_int len64)
107
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.
112  *)
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.
116    *)
117   let pvs = List.map read_pv_label devs in
118
119   (* Parse the metadata using the external lexer/parser. *)
120   let pvs = List.map (
121     fun (uuid, metadata) ->
122       eprintf "parsing: %s\n<<<<\n" metadata;
123       uuid, Virt_df_lvm2_lexer.parse_lvm2_metadata_from_string metadata
124   ) pvs in
125
126   (* Print the parsed metadata. *)
127   List.iter (
128     fun (uuid, metadata) ->
129       eprintf "metadata for UUID %s:\n" uuid;
130       output_metadata stderr metadata
131   ) pvs;
132
133   []
134
135
136
137
138
139   
140
141 (* Register with main code. *)
142 let () =
143   lvm_type_register plugin_name probe_pv list_lvs