1 (* 'df' command for virtual domains.
2 (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
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.
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.
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.
24 module C = Libvirt.Connect
25 module D = Libvirt.Domain
27 open Virt_df_gettext.Gettext
31 (* Command line argument parsing. *)
32 let set_uri = function "" -> uri := None | u -> uri := Some u in
35 printf "virt-df %s\n" (Libvirt_version.version);
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;
44 let test_mode filename =
45 test_files := filename :: !test_files
48 let argspec = Arg.align [
50 " " ^ s_ "Show all domains (default: only active domains)";
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 "--debug", Arg.Set debug,
58 " " ^ s_ "Debug mode (default: false)";
60 " " ^ s_ "Print sizes in human-readable format";
61 "--human-readable", Arg.Set human,
62 " " ^ s_ "Print sizes in human-readable format";
64 " " ^ s_ "Show inodes instead of blocks";
65 "--inodes", Arg.Set inodes,
66 " " ^ s_ "Show inodes instead of blocks";
67 "-t", Arg.String test_mode,
68 "dev " ^ s_ "(Test mode) Display contents of block device or file";
69 "--version", Arg.Unit version,
70 " " ^ s_ "Display version and exit";
74 raise (Arg.Bad (sprintf (f_ "%s: unknown parameter") str)) in
75 let usage_msg = s_ "virt-df : like 'df', shows disk space used in guests
82 Arg.parse argspec anon_fun usage_msg;
84 let doms : domain list =
85 if !test_files = [] then (
87 (* Connect to the hypervisor. *)
90 try C.connect_readonly ?name ()
92 Libvirt.Virterror err ->
93 prerr_endline (Libvirt.Virterror.to_string err);
94 (* If non-root and no explicit connection URI, print a warning. *)
95 if geteuid () <> 0 && name = None then (
96 print_endline (s_ "NB: If you want to monitor a local Xen hypervisor, you usually need to be root");
100 (* Get the list of active & inactive domains. *)
102 let nr_active_doms = C.num_of_domains conn in
104 Array.to_list (C.list_domains conn nr_active_doms) in
106 List.map (D.lookup_by_id conn) active_doms in
110 let nr_inactive_doms = C.num_of_defined_domains conn in
112 Array.to_list (C.list_defined_domains conn nr_inactive_doms) in
114 List.map (D.lookup_by_name conn) inactive_doms in
115 active_doms @ inactive_doms
119 let xmls = List.map D.get_xml_desc doms in
122 let xmls = List.map Xml.parse_string xmls in
124 (* Return just the XML documents - everything else will be closed
125 * and freed including the connection to the hypervisor.
129 (* Grr.. Need to use a library which has XPATH support (or cduce). *)
132 let nodes, domain_attrs =
134 | Xml.Element ("domain", attrs, children) -> children, attrs
135 | _ -> failwith (s_ "get_xml_desc didn't return <domain/>") in
138 try Some (int_of_string (List.assoc "id" domain_attrs))
139 with Not_found -> None in
141 let rec loop = function
143 failwith (s_ "get_xml_desc returned no <name> node in XML")
144 | Xml.Element ("name", _, [Xml.PCData name]) :: _ -> name
145 | Xml.Element ("name", _, _) :: _ ->
146 failwith (s_ "get_xml_desc returned strange <name> node")
147 | _ :: rest -> loop rest
149 let name = loop nodes in
155 | Xml.Element ("devices", _, devices) -> Some devices
158 List.concat devices in
160 let rec target_dev_of = function
162 | Xml.Element ("target", attrs, _) :: rest ->
163 (try Some (List.assoc "dev" attrs)
164 with Not_found -> target_dev_of rest)
165 | _ :: rest -> target_dev_of rest
168 let rec source_file_of = function
170 | Xml.Element ("source", attrs, _) :: rest ->
171 (try Some (List.assoc "file" attrs)
172 with Not_found -> source_file_of rest)
173 | _ :: rest -> source_file_of rest
176 let rec source_dev_of = function
178 | Xml.Element ("source", attrs, _) :: rest ->
179 (try Some (List.assoc "dev" attrs)
180 with Not_found -> source_dev_of rest)
181 | _ :: rest -> source_dev_of rest
187 | Xml.Element ("disk", attrs, children) ->
189 try Some (List.assoc "type" attrs)
190 with Not_found -> None in
192 try Some (List.assoc "device" attrs)
193 with Not_found -> None in
195 match source_file_of children with
196 | (Some _) as source -> source
197 | None -> source_dev_of children in
198 let target = target_dev_of children in
200 (* We only care about devices where we have
201 * source and target. Ignore CD-ROM devices.
203 (match source, target, device with
204 | _, _, Some "cdrom" -> None (* ignore *)
205 | Some source, Some target, Some device ->
206 (* Try to create a 'device' object for this
207 * device. If it fails, print a warning
208 * and ignore the device.
211 let dev = new block_device source in
213 d_type = typ; d_device = device;
214 d_source = source; d_target = target;
215 d_dev = dev; d_content = `Unknown
218 Unix_error (err, func, param) ->
219 eprintf "%s:%s: %s" func param (error_message err);
222 | _ -> None (* ignore anything else *)
228 { dom_name = name; dom_id = domid;
229 dom_disks = disks; dom_lv_filesystems = [] }
232 (* In test mode (-t option) the user can pass one or more
233 * block devices or filenames (containing partitions/filesystems/etc)
234 * which we use for testing virt-df itself. We create fake domains
240 dom_name = filename; dom_id = None;
243 d_type = Some "disk"; d_device = "disk";
244 d_source = filename; d_target = "hda";
245 d_dev = new block_device filename; d_content = `Unknown;
248 dom_lv_filesystems = []
253 (* HOF to map over disks. *)
254 let map_over_disks doms f =
256 fun ({ dom_disks = disks } as dom) ->
257 let disks = List.map f disks in
258 { dom with dom_disks = disks }
262 (* 'doms' is our list of domains and their guest block devices, and
263 * we've successfully opened each block device. Now probe them
264 * to find out what they contain.
266 let doms = map_over_disks doms (
267 fun ({ d_dev = dev } as disk) ->
268 (* See if it is partitioned first. *)
269 let parts = probe_for_partitions dev in
272 { disk with d_content = `Partitions parts }
274 (* Not partitioned. Does it contain a filesystem? *)
275 let fs = probe_for_filesystem dev in
278 { disk with d_content = `Filesystem fs }
280 (* Not partitioned, no filesystem, is it a PV? *)
281 let pv = probe_for_pv dev in
284 { disk with d_content = `PhysicalVolume lvm_name }
286 disk (* Spare/unknown. *)
289 (* Now we have either detected partitions or a filesystem on each
290 * physical device (or perhaps neither). See what is on those
293 let doms = map_over_disks doms (
295 | ({ d_dev = dev; d_content = `Partitions parts } as disk) ->
298 if p.part_status = Bootable || p.part_status = Nonbootable then (
299 let fs = probe_for_filesystem p.part_dev in
302 { p with part_content = `Filesystem fs }
305 let pv = probe_for_pv p.part_dev in
308 { p with part_content = `PhysicalVolume lvm_name }
310 p (* Spare/unknown. *)
313 let parts = { parts with parts = ps } in
314 { disk with d_content = `Partitions parts }
318 (* LVM filesystem detection
320 * For each domain, look for all disks/partitions which have been
321 * identified as PVs and pass those back to the respective LVM
322 * plugin for LV detection.
324 * (Note - a two-stage process because an LV can be spread over
325 * several PVs, so we have to detect all PVs belonging to a
328 * XXX To deal with RAID (ie. md devices) we will need to loop
329 * around here because RAID is like LVM except that they normally
330 * present as block devices which can be used by LVM.
332 (* First: LV detection. *)
333 let doms = List.map (
334 fun ({ dom_disks = disks } as dom) ->
335 (* Find all physical volumes, can be disks or partitions. *)
336 let pvs_on_disks = List.filter_map (
339 d_content = `PhysicalVolume pv } -> Some (pv, d_dev)
342 let pvs_on_partitions = List.map (
344 | { d_content = `Partitions { parts = parts } } ->
347 | { part_dev = part_dev;
348 part_content = `PhysicalVolume pv } ->
354 let lvs = List.concat (pvs_on_disks :: pvs_on_partitions) in
358 (* Second: filesystem on LV detection. *)
359 let doms = List.map (
361 (* Group the LVs by plug-in type. *)
362 let cmp (a,_) (b,_) = compare a b in
363 let lvs = List.sort ~cmp lvs in
364 let lvs = group_by lvs in
367 List.map (fun (pv, devs) -> list_lvs pv.lvm_plugin_id devs) lvs in
368 let lvs = List.concat lvs in
370 (* lvs is a list of potential LV devices. Now run them through the
371 * probes to see if any contain filesystems.
375 fun ({ lv_dev = dev } as lv) ->
376 match probe_for_filesystem dev with
377 | Some fs -> Some (lv, fs)
381 { dom with dom_lv_filesystems = filesystems }
384 (* Now print the results.
389 let total, used, avail =
390 match !inodes, !human with
391 | false, false -> s_ "1K-blocks", s_ "Used", s_ "Available"
392 | false, true -> s_ "Size", s_ "Used", s_ "Available"
393 | true, _ -> s_ "Inodes", s_ "IUse", s_ "IFree" in
394 printf "%-20s %10s %10s %10s %s\n%!"
395 (s_ "Filesystem") total used avail (s_ "Type") in
397 let printable_size bytes =
398 if bytes < 1024L *^ 1024L then
399 sprintf "%Ld bytes" bytes
400 else if bytes < 1024L *^ 1024L *^ 1024L then
401 sprintf "%.1f MiB" (Int64.to_float (bytes /^ 1024L) /. 1024.)
403 sprintf "%.1f GiB" (Int64.to_float (bytes /^ 1024L /^ 1024L) /. 1024.)
406 (* HOF to iterate over filesystems. *)
407 let iter_over_filesystems doms
408 (f : domain -> ?disk:disk -> ?partno:int -> device -> filesystem ->
411 fun ({ dom_disks = disks; dom_lv_filesystems = filesystems } as dom) ->
412 (* Ordinary filesystems found on disks & partitions. *)
415 | ({ d_content = `Filesystem fs; d_dev = dev } as disk) ->
417 | ({ d_content = `Partitions partitions } as disk) ->
421 | { part_content = `Filesystem fs; part_dev = dev } ->
422 f dom ~disk ~partno:(i+1) dev fs
427 (* LV filesystems. *)
428 List.iter (fun ({lv_dev = dev}, fs) -> f dom dev fs) filesystems
432 (* Print stats for each recognized filesystem. *)
433 let print_stats dom ?disk ?partno dev fs =
434 (* Printable name is like "domain:hda" or "domain:hda1". *)
436 let dom_name = dom.dom_name in
437 (* Get the disk name (eg. "hda") from the domain XML, if
438 * we have it, otherwise use the device name (eg. for LVM).
443 | Some disk -> disk.d_target
447 dom_name ^ ":" ^ disk_name
449 dom_name ^ ":" ^ disk_name ^ string_of_int partno in
450 printf "%-20s " name;
452 if fs.fs_is_swap then (
453 (* Swap partition. *)
456 (fs.fs_block_size *^ fs.fs_blocks_total /^ 1024L) fs.fs_name
459 (printable_size (fs.fs_block_size *^ fs.fs_blocks_total)) fs.fs_name
461 (* Ordinary filesystem. *)
462 if not !inodes then ( (* Block display. *)
463 (* 'df' doesn't count the restricted blocks. *)
464 let blocks_total = fs.fs_blocks_total -^ fs.fs_blocks_reserved in
465 let blocks_avail = fs.fs_blocks_avail -^ fs.fs_blocks_reserved in
466 let blocks_avail = if blocks_avail < 0L then 0L else blocks_avail in
468 if not !human then ( (* Display 1K blocks. *)
469 printf "%10Ld %10Ld %10Ld %s\n"
470 (blocks_total *^ fs.fs_block_size /^ 1024L)
471 (fs.fs_blocks_used *^ fs.fs_block_size /^ 1024L)
472 (blocks_avail *^ fs.fs_block_size /^ 1024L)
474 ) else ( (* Human-readable blocks. *)
475 printf "%10s %10s %10s %s\n"
476 (printable_size (blocks_total *^ fs.fs_block_size))
477 (printable_size (fs.fs_blocks_used *^ fs.fs_block_size))
478 (printable_size (blocks_avail *^ fs.fs_block_size))
481 ) else ( (* Inodes display. *)
482 printf "%10Ld %10Ld %10Ld %s\n"
483 fs.fs_inodes_total fs.fs_inodes_used fs.fs_inodes_avail
488 iter_over_filesystems doms print_stats