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.
23 module C = Libvirt.Connect
24 module D = Libvirt.Domain
28 open Virt_df_gettext.Gettext
32 (* Command line argument parsing. *)
33 let set_uri = function "" -> uri := None | u -> uri := Some u in
36 printf "virt-df %s\n" (Libvirt_version.version);
38 let major, minor, release =
39 let v, _ = Libvirt.get_version () in
40 v / 1_000_000, (v / 1_000) mod 1_000, v mod 1_000 in
41 printf "libvirt %d.%d.%d\n" major minor release;
45 let test_mode filename = test_files := filename :: !test_files in
47 let argspec = Arg.align [
49 " " ^ s_ "Show all domains (default: only active domains)";
51 " " ^ s_ "Show all domains (default: only active domains)";
52 "-c", Arg.String set_uri,
53 "uri " ^ s_ "Connect to URI (default: Xen)";
54 "--connect", Arg.String set_uri,
55 "uri " ^ s_ "Connect to URI (default: Xen)";
56 "--csv", Arg.Set csv_mode,
57 " " ^ s_ "Write results in CSV format";
58 "--debug", Arg.Set Diskimage.debug,
59 " " ^ s_ "Debug mode (default: false)";
61 " " ^ s_ "Print sizes in human-readable format";
62 "--human-readable", Arg.Set human,
63 " " ^ s_ "Print sizes in human-readable format";
65 " " ^ s_ "Show inodes instead of blocks";
66 "--inodes", Arg.Set inodes,
67 " " ^ s_ "Show inodes instead of blocks";
68 "-t", Arg.String test_mode,
69 "dev " ^ s_ "(Test mode) Display contents of block device or file";
70 "--version", Arg.Unit version,
71 " " ^ s_ "Display version and exit";
75 raise (Arg.Bad (sprintf (f_ "%s: unknown parameter") str)) in
76 let usage_msg = s_ "virt-df : like 'df', shows disk space used in guests
83 Arg.parse argspec anon_fun usage_msg;
85 (* Set up CSV support. *)
88 fun _ -> assert false (* Should never happen. *)
92 prerr_endline (s_ "CSV is not supported in this build of virt-df");
98 (* name target dev_path *)
99 let doms : (string * (string * string) list) list =
100 if !test_files = [] then (
102 (* Connect to the hypervisor. *)
105 try C.connect_readonly ?name ()
107 Libvirt.Virterror err ->
108 prerr_endline (Libvirt.Virterror.to_string err);
109 (* If non-root and no explicit connection URI, print a warning. *)
110 if Unix.geteuid () <> 0 && name = None then (
111 print_endline (s_ "NB: If you want to monitor a local Xen hypervisor, you usually need to be root");
115 (* Get the list of active & inactive domains. *)
117 let nr_active_doms = C.num_of_domains conn in
119 Array.to_list (C.list_domains conn nr_active_doms) in
121 List.map (D.lookup_by_id conn) active_doms in
125 let nr_inactive_doms = C.num_of_defined_domains conn in
127 Array.to_list (C.list_defined_domains conn nr_inactive_doms) in
129 List.map (D.lookup_by_name conn) inactive_doms in
130 active_doms @ inactive_doms
134 let xmls = List.map D.get_xml_desc doms in
137 let xmls = List.map Xml.parse_string xmls in
139 (* Return just the XML documents - everything else will be closed
140 * and freed including the connection to the hypervisor.
144 (* Grr.. Need to use a library which has XPATH support (or cduce). *)
147 let nodes, domain_attrs =
149 | Xml.Element ("domain", attrs, children) -> children, attrs
150 | _ -> failwith (s_ "get_xml_desc didn't return <domain/>") in
153 try Some (int_of_string (List.assoc "id" domain_attrs))
154 with Not_found -> None in*)
156 let rec loop = function
158 failwith (s_ "get_xml_desc returned no <name> node in XML")
159 | Xml.Element ("name", _, [Xml.PCData name]) :: _ -> name
160 | Xml.Element ("name", _, _) :: _ ->
161 failwith (s_ "get_xml_desc returned strange <name> node")
162 | _ :: rest -> loop rest
164 let name = loop nodes in
170 | Xml.Element ("devices", _, devices) -> Some devices
173 List.concat devices in
175 let rec target_dev_of = function
177 | Xml.Element ("target", attrs, _) :: rest ->
178 (try Some (List.assoc "dev" attrs)
179 with Not_found -> target_dev_of rest)
180 | _ :: rest -> target_dev_of rest
183 let rec source_file_of = function
185 | Xml.Element ("source", attrs, _) :: rest ->
186 (try Some (List.assoc "file" attrs)
187 with Not_found -> source_file_of rest)
188 | _ :: rest -> source_file_of rest
191 let rec source_dev_of = function
193 | Xml.Element ("source", attrs, _) :: rest ->
194 (try Some (List.assoc "dev" attrs)
195 with Not_found -> source_dev_of rest)
196 | _ :: rest -> source_dev_of rest
202 | Xml.Element ("disk", attrs, children) ->
204 try Some (List.assoc "type" attrs)
205 with Not_found -> None in*)
207 try Some (List.assoc "device" attrs)
208 with Not_found -> None in
210 match source_file_of children with
211 | (Some _) as source -> source
212 | None -> source_dev_of children in
213 let target = target_dev_of children in
215 (* We only care about devices where we have
216 * source and target. Ignore CD-ROM devices.
218 (match source, target, device with
219 | _, _, Some "cdrom" -> None (* ignore CD-ROMs *)
220 | Some source, Some target, _ -> Some (target, source)
221 | _ -> None (* ignore anything else *)
230 (* In test mode (-t option) the user can pass one or more
231 * block devices or filenames (containing partitions/filesystems/etc)
232 * which we use for testing virt-df itself. We create fake domains
237 filename, ["hda", filename]
241 (* Convert these to Diskimage library 'machine's. *)
242 let machines = List.filter_map (
244 try Some (Diskimage.open_machine name disks)
245 with Unix.Unix_error (err, func, param) ->
246 eprintf "%s:%s: %s" func param (Unix.error_message err);
251 let machines = List.map Diskimage.scan_machine machines in
253 (*----------------------------------------------------------------------*)
254 (* Now print the results. *)
256 (* Print the title. *)
258 let total, used, avail =
259 match !inodes, !human with
260 | false, false -> s_ "1K-blocks", s_ "Used", s_ "Available"
261 | false, true -> s_ "Size", s_ "Used", s_ "Available"
262 | true, _ -> s_ "Inodes", s_ "IUse", s_ "IFree" in
263 if not !csv_mode then
264 printf "%-32s %10s %10s %10s %s\n%!"
265 (s_ "Filesystem") total used avail (s_ "Type")
267 csv_write [ "Filesystem"; total; used; avail; "Type" ] in
269 let printable_size bytes =
270 if bytes < ~^1024 *^ ~^1024 then
271 sprintf "%s bytes" (Int63.to_string bytes)
272 else if bytes < ~^1024 *^ ~^1024 *^ ~^1024 then
273 sprintf "%.1f MiB" (Int63.to_float (bytes /^ ~^1024) /. 1024.)
275 sprintf "%.1f GiB" (Int63.to_float (bytes /^ ~^1024 /^ ~^1024) /. 1024.)
278 (* HOF to iterate over filesystems. *)
279 let iter_over_filesystems machines
280 (f : Diskimage.machine -> ?disk:Diskimage.disk -> ?partno:int ->
281 Diskimage.device -> Diskimage.filesystem ->
284 fun ({ Diskimage.m_disks = disks;
285 m_lv_filesystems = filesystems } as dom) ->
286 (* Ordinary filesystems found on disks & partitions. *)
289 | ({ Diskimage.d_content = `Filesystem fs; d_dev = dev } as disk) ->
290 f dom ~disk (dev :> Diskimage.device) fs
291 | ({ Diskimage.d_content = `Partitions partitions } as disk) ->
295 | { Diskimage.part_content = `Filesystem fs;
297 f dom ~disk ~partno:(i+1) dev fs
299 ) partitions.Diskimage.parts
302 (* LV filesystems. *)
304 fun ({Diskimage.lv_dev = dev}, fs) -> f dom dev fs
309 (* Printable name is like "domain:hda" or "domain:hda1". *)
310 let printable_name machine ?disk ?partno dev =
311 let m_name = machine.Diskimage.m_name in
312 (* Get the disk name (eg. "hda") from the domain XML, if
313 * we have it, otherwise use the device name (eg. for LVM).
318 | Some disk -> disk.Diskimage.d_name
322 m_name ^ ":" ^ disk_name
324 m_name ^ ":" ^ disk_name ^ string_of_int partno
327 (* Print stats for each recognized filesystem. *)
328 let print_stats machine ?disk ?partno dev fs =
329 let name = printable_name machine ?disk ?partno dev in
330 printf "%-32s " name;
333 Diskimage.fs_blocksize = fs_blocksize;
334 fs_blocks_total = fs_blocks_total;
335 fs_is_swap = fs_is_swap;
336 fs_blocks_reserved = fs_blocks_reserved;
337 fs_blocks_avail = fs_blocks_avail;
338 fs_blocks_used = fs_blocks_used;
339 fs_inodes_total = fs_inodes_total;
340 fs_inodes_reserved = fs_inodes_reserved;
341 fs_inodes_avail = fs_inodes_avail;
342 fs_inodes_used = fs_inodes_used
345 let fs_name = Diskimage.name_of_filesystem fs in
348 (* Swap partition. *)
351 (Int63.to_string (fs_blocksize *^ fs_blocks_total /^ ~^1024))
355 (printable_size (fs_blocksize *^ fs_blocks_total))
358 (* Ordinary filesystem. *)
359 if not !inodes then ( (* Block display. *)
360 (* 'df' doesn't count the restricted blocks. *)
361 let blocks_total = fs_blocks_total -^ fs_blocks_reserved in
362 let blocks_avail = fs_blocks_avail -^ fs_blocks_reserved in
363 let blocks_avail = if blocks_avail < ~^0 then ~^0 else blocks_avail in
365 if not !human then ( (* Display 1K blocks. *)
366 printf "%10s %10s %10s %s\n"
367 (Int63.to_string (blocks_total *^ fs_blocksize /^ ~^1024))
368 (Int63.to_string (fs_blocks_used *^ fs_blocksize /^ ~^1024))
369 (Int63.to_string (blocks_avail *^ fs_blocksize /^ ~^1024))
371 ) else ( (* Human-readable blocks. *)
372 printf "%10s %10s %10s %s\n"
373 (printable_size (blocks_total *^ fs_blocksize))
374 (printable_size (fs_blocks_used *^ fs_blocksize))
375 (printable_size (blocks_avail *^ fs_blocksize))
378 ) else ( (* Inodes display. *)
379 printf "%10s %10s %10s %s\n"
380 (Int63.to_string fs_inodes_total)
381 (Int63.to_string fs_inodes_used)
382 (Int63.to_string fs_inodes_avail)
388 (* Alternate version of print_stats which writes to a CSV file.
389 * We ignore the human-readable option because we assume that
390 * the data will be post-processed by something.
392 let print_stats_csv machine ?disk ?partno dev fs =
393 let name = printable_name machine ?disk ?partno dev in
396 Diskimage.fs_blocksize = fs_blocksize;
397 fs_blocks_total = fs_blocks_total;
398 fs_is_swap = fs_is_swap;
399 fs_blocks_reserved = fs_blocks_reserved;
400 fs_blocks_avail = fs_blocks_avail;
401 fs_blocks_used = fs_blocks_used;
402 fs_inodes_total = fs_inodes_total;
403 fs_inodes_reserved = fs_inodes_reserved;
404 fs_inodes_avail = fs_inodes_avail;
405 fs_inodes_used = fs_inodes_used
408 let fs_name = Diskimage.name_of_filesystem fs in
412 (* Swap partition. *)
413 [ Int63.to_string (fs_blocksize *^ fs_blocks_total /^ ~^1024);
416 (* Ordinary filesystem. *)
417 if not !inodes then ( (* 1K block display. *)
418 (* 'df' doesn't count the restricted blocks. *)
419 let blocks_total = fs_blocks_total -^ fs_blocks_reserved in
420 let blocks_avail = fs_blocks_avail -^ fs_blocks_reserved in
421 let blocks_avail = if blocks_avail < ~^0 then ~^0 else blocks_avail in
423 [ Int63.to_string (blocks_total *^ fs_blocksize /^ ~^1024);
424 Int63.to_string (fs_blocks_used *^ fs_blocksize /^ ~^1024);
425 Int63.to_string (blocks_avail *^ fs_blocksize /^ ~^1024) ]
426 ) else ( (* Inodes display. *)
427 [ Int63.to_string fs_inodes_total;
428 Int63.to_string fs_inodes_used;
429 Int63.to_string fs_inodes_avail ]
433 let row = name :: row @ [fs_name] in
437 iter_over_filesystems machines
438 (if not !csv_mode then print_stats else print_stats_csv)