Make debug selectable at runtime.
[virt-top.git] / virt-df / virt_df.ml
1 (* 'df' command for virtual domains.
2    (C) Copyright 2007-2008 Richard W.M. Jones, Red Hat Inc.
3    http://libvirt.org/
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *)
19
20 open Printf
21 open ExtList
22 open Unix
23
24 open Virt_df_gettext.Gettext
25
26 let ( +* ) = Int32.add
27 let ( -* ) = Int32.sub
28 let ( ** ) = Int32.mul
29 let ( /* ) = Int32.div
30
31 let ( +^ ) = Int64.add
32 let ( -^ ) = Int64.sub
33 let ( *^ ) = Int64.mul
34 let ( /^ ) = Int64.div
35
36 let debug = ref false
37 let uri = ref None
38 let inodes = ref false
39 let human = ref false
40 let all = ref false
41 let test_files = ref []
42
43 class virtual device =
44 object (self)
45   method virtual read : int64 -> int -> string
46   method virtual size : int64
47   method virtual name : string
48
49   (* Helper method to read a chunk of data into a bitstring. *)
50   method read_bitstring offset len =
51     let str = self#read offset len in
52     (str, 0, len * 8)
53 end
54
55 (* A concrete device which just direct-maps a file or /dev device. *)
56 class block_device filename =
57   let fd = openfile filename [ O_RDONLY ] 0 in
58   let size = (LargeFile.fstat fd).LargeFile.st_size in
59 object (self)
60   inherit device
61   method read offset len =
62     ignore (LargeFile.lseek fd offset SEEK_SET);
63     let str = String.make len '\000' in
64     read fd str 0 len;
65     str
66   method size = size
67   method name = filename
68 end
69
70 (* The null device.  Any attempt to read generates an error. *)
71 let null_device : device =
72 object
73   inherit device
74   method read _ _ = assert false
75   method size = 0L
76   method name = "null"
77 end
78
79 type domain = {
80   dom_name : string;                    (* Domain name. *)
81   dom_id : int option;                  (* Domain ID (if running). *)
82   dom_disks : disk list;                (* Domain disks. *)
83   dom_lv_filesystems : filesystem list; (* Domain LV filesystems. *)
84 }
85 and disk = {
86   (* From the XML ... *)
87   d_type : string option;               (* The <disk type=...> *)
88   d_device : string;                    (* The <disk device=...> (eg "disk") *)
89   d_source : string;                    (* The <source file=... or dev> *)
90   d_target : string;                    (* The <target dev=...> (eg "hda") *)
91
92   (* About the device itself. *)
93   d_dev : device;                       (* Disk device. *)
94   d_content : disk_content;             (* What's on it. *)
95 }
96 and disk_content =
97   [ `Unknown                            (* Not probed or unknown. *)
98   | `Partitions of partitions           (* Contains partitions. *)
99   | `Filesystem of filesystem           (* Contains a filesystem directly. *)
100   | `PhysicalVolume of pv               (* Contains an LVM PV. *)
101   ]
102
103 (* Partitions. *)
104
105 and partitions = {
106   parts_name : string;                  (* Name of partitioning scheme. *)
107   parts : partition list                (* Partitions. *)
108 }
109 and partition = {
110   part_status : partition_status;       (* Bootable, etc. *)
111   part_type : int;                      (* Partition filesystem type. *)
112   part_dev : device;                    (* Partition device. *)
113   part_content : partition_content;     (* What's on it. *)
114 }
115 and partition_status = Bootable | Nonbootable | Malformed | NullEntry
116 and partition_content =
117   [ `Unknown                            (* Not probed or unknown. *)
118   | `Filesystem of filesystem           (* Filesystem. *)
119   | `PhysicalVolume of pv               (* Contains an LVM PV. *)
120   ]
121
122 (* Filesystems (also swap devices). *)
123 and filesystem = {
124   fs_name : string;                     (* Name of filesystem. *)
125   fs_block_size : int64;                (* Block size (bytes). *)
126   fs_blocks_total : int64;              (* Total blocks. *)
127   fs_is_swap : bool;                    (* If swap, following not valid. *)
128   fs_blocks_reserved : int64;           (* Blocks reserved for super-user. *)
129   fs_blocks_avail : int64;              (* Blocks free (available). *)
130   fs_blocks_used : int64;               (* Blocks in use. *)
131   fs_inodes_total : int64;              (* Total inodes. *)
132   fs_inodes_reserved : int64;           (* Inodes reserved for super-user. *)
133   fs_inodes_avail : int64;              (* Inodes free (available). *)
134   fs_inodes_used : int64;               (* Inodes in use. *)
135 }
136
137 (* Physical volumes. *)
138 and pv = {
139   lvm_plugin_id : lvm_plugin_id;        (* The LVM plug-in. *)
140   pv_uuid : string;                     (* UUID. *)
141 }
142
143 (* Logical volumes. *)
144 and lv = {
145   lv_dev : device;                      (* Logical volume device. *)
146 }
147
148 and lvm_plugin_id = string
149
150 (* Convert partition, filesystem types to printable strings for debugging. *)
151 let string_of_partition
152     { part_status = status; part_type = typ; part_dev = dev } =
153   sprintf "%s: %s partition type %d"
154     dev#name
155     (match status with
156      | Bootable -> "bootable"
157      | Nonbootable -> "nonbootable"
158      | Malformed -> "malformed"
159      | NullEntry -> "empty")
160     typ
161
162 let string_of_filesystem { fs_name = name; fs_is_swap = swap } =
163   if not swap then name
164   else name ^ " [swap]"
165
166 (* Register a partition scheme. *)
167 let partition_types = ref []
168 let partition_type_register (parts_name : string) probe_fn =
169   partition_types := (parts_name, probe_fn) :: !partition_types
170
171 (* Probe a device for partitions.  Returns [Some parts] or [None]. *)
172 let probe_for_partitions dev =
173   if !debug then eprintf "probing for partitions on %s ...\n%!" dev#name;
174   let rec loop = function
175     | [] -> None
176     | (parts_name, probe_fn) :: rest ->
177         try Some (probe_fn dev)
178         with Not_found -> loop rest
179   in
180   let r = loop !partition_types in
181   if !debug then (
182     match r with
183     | None -> eprintf "no partitions found on %s\n%!" dev#name
184     | Some { parts_name = name; parts = parts } ->
185         eprintf "found %d %s partitions on %s:\n"
186           (List.length parts) name dev#name;
187         List.iter (fun p -> eprintf "\t%s\n%!" (string_of_partition p)) parts
188   );
189   r
190
191 (* Register a filesystem type (or swap). *)
192 let filesystem_types = ref []
193 let filesystem_type_register (fs_name : string) probe_fn =
194   filesystem_types := (fs_name, probe_fn) :: !filesystem_types
195
196 (* Probe a device for a filesystem.  Returns [Some fs] or [None]. *)
197 let probe_for_filesystem dev =
198   if !debug then eprintf "probing for a filesystem on %s ...\n%!" dev#name;
199   let rec loop = function
200     | [] -> None
201     | (fs_name, probe_fn) :: rest ->
202         try Some (probe_fn dev)
203         with Not_found -> loop rest
204   in
205   let r = loop !filesystem_types in
206   if !debug then (
207     match r with
208     | None -> eprintf "no filesystem found on %s\n%!" dev#name
209     | Some fs ->
210         eprintf "found a filesystem on %s:\n" dev#name;
211         eprintf "\t%s\n%!" (string_of_filesystem fs)
212   );
213   r
214
215 (* Register a volume management type. *)
216 let lvm_types = ref []
217 let lvm_type_register (lvm_name : string) probe_fn list_lvs_fn =
218   lvm_types := (lvm_name, (probe_fn, list_lvs_fn)) :: !lvm_types
219
220 (* Probe a device for a PV.  Returns [Some lvm_name] or [None]. *)
221 let probe_for_pv dev =
222   if !debug then eprintf "probing if %s is a PV ...\n%!" dev#name;
223   let rec loop = function
224     | [] -> None
225     | (lvm_name, (probe_fn, _)) :: rest ->
226         try Some (probe_fn lvm_name dev)
227         with Not_found -> loop rest
228   in
229   let r = loop !lvm_types in
230   if !debug then (
231     match r with
232     | None -> eprintf "no PV found on %s\n%!" dev#name
233     | Some { lvm_plugin_id = name } ->
234         eprintf "%s contains a %s PV\n%!" dev#name name
235   );
236   r
237
238 let list_lvs lvm_name devs =
239   let _, list_lvs_fn = List.assoc lvm_name !lvm_types in
240   list_lvs_fn devs
241
242 (*----------------------------------------------------------------------*)
243
244 (* This version by Isaac Trotts. *)
245 let group_by ?(cmp = Pervasives.compare) ls =
246   let ls' =
247     List.fold_left
248       (fun acc (day1, x1) ->
249          match acc with
250              [] -> [day1, [x1]]
251            | (day2, ls2) :: acctl ->
252                if cmp day1 day2 = 0
253                then (day1, x1 :: ls2) :: acctl
254                else (day1, [x1]) :: acc)
255       []
256       ls
257   in
258   let ls' = List.rev ls' in
259   List.map (fun (x, xs) -> x, List.rev xs) ls'
260
261 let rec range a b =
262   if a < b then a :: range (a+1) b
263   else []