Read out metadata offset & length from PV header.
[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 let plugin_name = "LVM2"
28
29 let sector_size = 512
30 let sector_size64 = 512L
31
32 (* Probe to see if it's an LVM2 PV.  Look for the "LABELONE" label. *)
33 let rec probe_pv lvm_plugin_id dev =
34   try
35     let uuid, metadata_offset, metadata_length = read_pv_label dev in
36     if debug then
37       eprintf "LVM2 detected UUID %s md offset 0x%lx len %ld\n%!"
38         uuid metadata_offset metadata_length;
39     { lvm_plugin_id = lvm_plugin_id; pv_uuid = uuid }
40   with _ -> raise Not_found
41
42 and read_pv_label dev =
43   (* Load the first 8 sectors.  I found by experimentation that
44    * the second sector contains the header ("LABELONE" etc) and
45    * the nineth sector contains some additional information about
46    * the location of the current metadata.
47    *)
48   let bits = dev#read_bitstring 0L (9 * sector_size) in
49
50   Bitmatch.hexdump_bitstring stdout bits;
51
52   bitmatch bits with
53   | sector0 : sector_size*8 : bitstring; (* sector 0 *)
54     labelone : 8*8 : bitstring;         (* "LABELONE" *)
55     padding : 16*8 : bitstring;         (* Seems to contain something. *)
56     lvm2_ver : 8*8 : bitstring;         (* "LVM2 001" *)
57     uuid : 32*8 : bitstring;            (* UUID *)
58     padding2 : (sector_size-64)*8 : bitstring; (* to end of second sector *)
59     sector234567 : sector_size*8 * 6 : bitstring; (* sectors 2-6 *)
60     padding3 : 0x28*8 : bitstring;      (* start of sector 8 *)
61     metadata_offset : 32 : littleendian;(* metadata offset *)
62     padding4 : 4*8 : bitstring;
63     metadata_length : 32 : littleendian (* length of metadata (bytes) *)
64       when Bitmatch.string_of_bitstring labelone = "LABELONE" &&
65            Bitmatch.string_of_bitstring lvm2_ver = "LVM2 001" ->
66     let metadata_offset = metadata_offset +* 0x1000_l in
67
68     (* Check the metadata offset & length look reasonable for this
69      * device.  Otherwise maybe it's a newer or older header which
70      * we don't really understand properly.
71      *)
72     let () =
73       let size =
74         if dev#size <= Int64.of_int32 Int32.max_int then Int64.to_int32 dev#size
75         else Int32.max_int in
76       if metadata_offset < 0x1200_l || metadata_offset >= size
77         || metadata_length < 0_l || metadata_offset+*metadata_length >= size
78       then
79         invalid_arg "read_pv_label: bad metadata offset or length" in
80
81     Bitmatch.string_of_bitstring uuid, metadata_offset, metadata_length
82
83
84   | _ ->
85     invalid_arg (sprintf "read_pv_label: %s: not an LVM2 physical volume"
86                    dev#name)
87
88 (* We are passed a list of devices which we previously identified
89  * as PVs belonging to us.  From these produce a list of all LVs
90  * (as devices) and return them.  Note that we don't try to detect
91  * what is on these LVs - that will be done in the main code.
92  *)
93 let list_lvs devs =
94   []
95
96 (* Register with main code. *)
97 let () =
98   lvm_type_register plugin_name probe_pv list_lvs