Add PV detection framework.
[virt-top.git] / virt-df / virt_df_main.ml
1 (* 'df' command for virtual domains.
2    (C) Copyright 2007 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 module C = Libvirt.Connect
25 module D = Libvirt.Domain
26
27 open Virt_df_gettext.Gettext
28 open Virt_df
29
30 let () =
31   (* Command line argument parsing. *)
32   let set_uri = function "" -> uri := None | u -> uri := Some u in
33
34   let version () =
35     printf "virt-df %s\n" (Libvirt_version.version);
36
37     let major, minor, release =
38       let v, _ = Libvirt.get_version () in
39       v / 1_000_000, (v / 1_000) mod 1_000, v mod 1_000 in
40     printf "libvirt %d.%d.%d\n" major minor release;
41     exit 0
42   in
43
44   let test_mode filename =
45     test_files := filename :: !test_files
46   in
47
48   let argspec = Arg.align [
49     "-a", Arg.Set all,
50       " " ^ s_ "Show all domains (default: only active domains)";
51     "--all", Arg.Set all,
52       " " ^ s_ "Show all domains (default: only active domains)";
53     "-c", Arg.String set_uri,
54       "uri " ^ s_ "Connect to URI (default: Xen)";
55     "--connect", Arg.String set_uri,
56       "uri " ^ s_ "Connect to URI (default: Xen)";
57     "-h", Arg.Set human,
58       " " ^ s_ "Print sizes in human-readable format";
59     "--human-readable", Arg.Set human,
60       " " ^ s_ "Print sizes in human-readable format";
61     "-i", Arg.Set inodes,
62       " " ^ s_ "Show inodes instead of blocks";
63     "--inodes", Arg.Set inodes,
64       " " ^ s_ "Show inodes instead of blocks";
65     "-t", Arg.String test_mode,
66       "dev" ^ s_ "(Test mode) Display contents of block device or file";
67     "--version", Arg.Unit version,
68       " " ^ s_ "Display version and exit";
69   ] in
70
71   let anon_fun str =
72     raise (Arg.Bad (sprintf (f_ "%s: unknown parameter") str)) in
73   let usage_msg = s_ "virt-df : like 'df', shows disk space used in guests
74
75 SUMMARY
76   virt-df [-options]
77
78 OPTIONS" in
79
80   Arg.parse argspec anon_fun usage_msg;
81
82   let doms : domain list =
83     if !test_files = [] then (
84       let xmls =
85         (* Connect to the hypervisor. *)
86         let conn =
87           let name = !uri in
88           try C.connect_readonly ?name ()
89           with
90             Libvirt.Virterror err ->
91               prerr_endline (Libvirt.Virterror.to_string err);
92               (* If non-root and no explicit connection URI, print a warning. *)
93               if geteuid () <> 0 && name = None then (
94                 print_endline (s_ "NB: If you want to monitor a local Xen hypervisor, you usually need to be root");
95               );
96               exit 1 in
97
98         (* Get the list of active & inactive domains. *)
99         let doms =
100           let nr_active_doms = C.num_of_domains conn in
101           let active_doms =
102             Array.to_list (C.list_domains conn nr_active_doms) in
103           let active_doms =
104             List.map (D.lookup_by_id conn) active_doms in
105           if not !all then
106             active_doms
107           else (
108             let nr_inactive_doms = C.num_of_defined_domains conn in
109             let inactive_doms =
110               Array.to_list (C.list_defined_domains conn nr_inactive_doms) in
111             let inactive_doms =
112               List.map (D.lookup_by_name conn) inactive_doms in
113             active_doms @ inactive_doms
114           ) in
115
116         (* Get their XML. *)
117         let xmls = List.map D.get_xml_desc doms in
118
119         (* Parse the XML. *)
120         let xmls = List.map Xml.parse_string xmls in
121
122         (* Return just the XML documents - everything else will be closed
123          * and freed including the connection to the hypervisor.
124          *)
125         xmls in
126
127       (* Grr.. Need to use a library which has XPATH support (or cduce). *)
128       List.map (
129         fun xml ->
130           let nodes, domain_attrs =
131             match xml with
132             | Xml.Element ("domain", attrs, children) -> children, attrs
133             | _ -> failwith (s_ "get_xml_desc didn't return <domain/>") in
134
135           let domid =
136             try Some (int_of_string (List.assoc "id" domain_attrs))
137             with Not_found -> None in
138
139           let rec loop = function
140             | [] ->
141                 failwith (s_ "get_xml_desc returned no <name> node in XML")
142             | Xml.Element ("name", _, [Xml.PCData name]) :: _ -> name
143             | Xml.Element ("name", _, _) :: _ ->
144                 failwith (s_ "get_xml_desc returned strange <name> node")
145             | _ :: rest -> loop rest
146           in
147           let name = loop nodes in
148
149           let devices =
150             let devices =
151               List.filter_map (
152                 function
153                 | Xml.Element ("devices", _, devices) -> Some devices
154                 | _ -> None
155               ) nodes in
156             List.concat devices in
157
158           let rec target_dev_of = function
159             | [] -> None
160             | Xml.Element ("target", attrs, _) :: rest ->
161                 (try Some (List.assoc "dev" attrs)
162                  with Not_found -> target_dev_of rest)
163             | _ :: rest -> target_dev_of rest
164           in
165
166           let rec source_file_of = function
167             | [] -> None
168             | Xml.Element ("source", attrs, _) :: rest ->
169                 (try Some (List.assoc "file" attrs)
170                  with Not_found -> source_file_of rest)
171             | _ :: rest -> source_file_of rest
172           in
173
174           let rec source_dev_of = function
175             | [] -> None
176             | Xml.Element ("source", attrs, _) :: rest ->
177                 (try Some (List.assoc "dev" attrs)
178                  with Not_found -> source_dev_of rest)
179             | _ :: rest -> source_dev_of rest
180           in
181
182           let disks =
183             List.filter_map (
184               function
185               | Xml.Element ("disk", attrs, children) ->
186                   let typ =
187                     try Some (List.assoc "type" attrs)
188                     with Not_found -> None in
189                   let device =
190                     try Some (List.assoc "device" attrs)
191                     with Not_found -> None in
192                   let source =
193                     match source_file_of children with
194                     | (Some _) as source -> source
195                     | None -> source_dev_of children in
196                   let target = target_dev_of children in
197
198                   (* We only care about devices where we have
199                    * source and target.  Ignore CD-ROM devices.
200                    *)
201                   (match source, target, device with
202                    | _, _, Some "cdrom" -> None (* ignore *)
203                    | Some source, Some target, Some device ->
204                        (* Try to create a 'device' object for this
205                         * device.  If it fails, print a warning
206                         * and ignore the device.
207                         *)
208                        (try
209                           let dev = new block_device source in
210                           Some {
211                             d_type = typ; d_device = device;
212                             d_source = source; d_target = target;
213                             d_dev = dev; d_content = `Unknown
214                           }
215                         with
216                           Unix_error (err, func, param) ->
217                             eprintf "%s:%s: %s" func param (error_message err);
218                             None
219                        )
220                    | _ -> None (* ignore anything else *)
221                   )
222
223               | _ -> None
224             ) devices in
225
226           { dom_name = name; dom_id = domid; dom_disks = disks }
227       ) xmls
228     ) else (
229       (* In test mode (-t option) the user can pass one or more
230        * block devices or filenames (containing partitions/filesystems/etc)
231        * which we use for testing virt-df itself.  We create fake domains
232        * from these.
233        *)
234       List.map (
235         fun filename ->
236           {
237             dom_name = filename; dom_id = None;
238             dom_disks = [
239               {
240                 d_type = Some "disk"; d_device = "disk";
241                 d_source = filename; d_target = "hda";
242                 d_dev = new block_device filename; d_content = `Unknown;
243               }
244             ]
245           }
246       ) !test_files
247     ) in
248
249   (* HOF to map over disks. *)
250   let map_over_disks doms f =
251     List.map (
252       fun ({ dom_disks = disks } as dom) ->
253         let disks = List.map f disks in
254         { dom with dom_disks = disks }
255     ) doms
256   in
257
258   (* 'doms' is our list of domains and their guest block devices, and
259    * we've successfully opened each block device.  Now probe them
260    * to find out what they contain.
261    *)
262   let doms = map_over_disks doms (
263     fun ({ d_dev = dev } as disk) ->
264       (* See if it is partitioned first. *)
265       let parts = probe_for_partitions dev in
266       match parts with
267       | Some parts ->
268           { disk with d_content = `Partitions parts }
269       | None ->
270           (* Not partitioned.  Does it contain a filesystem? *)
271           let fs = probe_for_filesystem dev in
272           match fs with
273           | Some fs ->
274               { disk with d_content = `Filesystem fs }
275           | None ->
276               (* Not partitioned, no filesystem, is it a PV? *)
277               let pv = probe_for_pv dev in
278               match pv with
279               | Some lvm_name ->
280                   { disk with d_content = `PhysicalVolume lvm_name }
281               | None ->
282                   disk (* Spare/unknown. *)
283   ) in
284
285   (* Now we have either detected partitions or a filesystem on each
286    * physical device (or perhaps neither).  See what is on those
287    * partitions.
288    *)
289   let doms = map_over_disks doms (
290     function
291     | ({ d_dev = dev; d_content = `Partitions parts } as disk) ->
292         let ps = List.map (
293           fun p ->
294             if p.part_status = Bootable || p.part_status = Nonbootable then (
295               let fs = probe_for_filesystem p.part_dev in
296               match fs with
297               | Some fs ->
298                   { p with part_content = `Filesystem fs }
299               | None ->
300                   (* Is it a PV? *)
301                   let pv = probe_for_pv p.part_dev in
302                   match pv with
303                   | Some lvm_name ->
304                       { p with part_content = `PhysicalVolume lvm_name }
305                   | None ->
306                       p (* Spare/unknown. *)
307             ) else p
308         ) parts.parts in
309         let parts = { parts with parts = ps } in
310         { disk with d_content = `Partitions parts }
311     | disk -> disk
312   ) in
313
314   (* XXX LVM filesystem detection ... *)
315
316
317
318
319
320   (* Print the title. *)
321   let () =
322     let total, used, avail =
323       match !inodes, !human with
324       | false, false -> s_ "1K-blocks", s_ "Used", s_ "Available"
325       | false, true -> s_ "Size", s_ "Used", s_ "Available"
326       | true, _ -> s_ "Inodes", s_ "IUse", s_ "IFree" in
327     printf "%-20s %10s %10s %10s %s\n%!"
328       (s_ "Filesystem") total used avail (s_ "Type") in
329
330   let printable_size bytes =
331     if bytes < 1024L *^ 1024L then
332       sprintf "%Ld bytes" bytes
333     else if bytes < 1024L *^ 1024L *^ 1024L then
334       sprintf "%.1f MiB" (Int64.to_float (bytes /^ 1024L) /. 1024.)
335     else
336       sprintf "%.1f GiB" (Int64.to_float (bytes /^ 1024L /^ 1024L) /. 1024.)
337   in
338
339   (* HOF to iterate over filesystems. *)
340   let iter_over_filesystems doms f =
341     List.iter (
342       fun ({ dom_disks = disks } as dom) ->
343         List.iter (
344           function
345           | ({ d_content = `Filesystem fs } as disk) ->
346               f dom disk None fs
347           | ({ d_content = `Partitions partitions } as disk) ->
348               List.iteri (
349                 fun i ->
350                   function
351                   | ({ part_content = `Filesystem fs } as part) ->
352                       f dom disk (Some (part, i)) fs
353                   | _ -> ()
354               ) partitions.parts
355           | _ -> ()
356         ) disks
357     ) doms
358   in
359
360   (* Print stats for each recognized filesystem. *)
361   let print_stats dom disk part fs =
362     (* Printable name is like "domain:hda" or "domain:hda1". *)
363     let name =
364       let dom_name = dom.dom_name in
365       let d_target = disk.d_target in
366       match part with
367       | None ->
368           dom_name ^ ":" ^ d_target
369       | Some (_, pnum) ->
370           dom_name ^ ":" ^ d_target ^ string_of_int pnum in
371     printf "%-20s " name;
372
373     if fs.fs_is_swap then (
374       (* Swap partition. *)
375       if not !human then
376         printf "%10Ld                       %s\n"
377           (fs.fs_block_size *^ fs.fs_blocks_total /^ 1024L) fs.fs_name
378       else
379         printf "%10s                       %s\n"
380           (printable_size (fs.fs_block_size *^ fs.fs_blocks_total)) fs.fs_name
381     ) else (
382       (* Ordinary filesystem. *)
383       if not !inodes then (             (* Block display. *)
384         (* 'df' doesn't count the restricted blocks. *)
385         let blocks_total = fs.fs_blocks_total -^ fs.fs_blocks_reserved in
386         let blocks_avail = fs.fs_blocks_avail -^ fs.fs_blocks_reserved in
387         let blocks_avail = if blocks_avail < 0L then 0L else blocks_avail in
388
389         if not !human then (            (* Display 1K blocks. *)
390           printf "%10Ld %10Ld %10Ld %s\n"
391             (blocks_total *^ fs.fs_block_size /^ 1024L)
392             (fs.fs_blocks_used *^ fs.fs_block_size /^ 1024L)
393             (blocks_avail *^ fs.fs_block_size /^ 1024L)
394             fs.fs_name
395         ) else (                        (* Human-readable blocks. *)
396           printf "%10s %10s %10s %s\n"
397             (printable_size (blocks_total *^ fs.fs_block_size))
398             (printable_size (fs.fs_blocks_used *^ fs.fs_block_size))
399             (printable_size (blocks_avail *^ fs.fs_block_size))
400             fs.fs_name
401         )
402       ) else (                          (* Inodes display. *)
403         printf "%10Ld %10Ld %10Ld %s\n"
404           fs.fs_inodes_total fs.fs_inodes_used fs.fs_inodes_avail
405           fs.fs_name
406       )
407     )
408   in
409   iter_over_filesystems doms print_stats