X-Git-Url: http://git.annexia.org/?p=virt-top.git;a=blobdiff_plain;f=virt-df%2Fvirt_df.ml;h=350d535a8ef26082096d49706f5e2528dada272b;hp=4a5f0ed34977cc52fba0ca2f477ddc54bfecad69;hb=f9d99e63272520c19bddc9e4b47731d57e5658e2;hpb=5616e76a5a01656aa0dcc323fcd1fcd77764e638 diff --git a/virt-df/virt_df.ml b/virt-df/virt_df.ml index 4a5f0ed..350d535 100644 --- a/virt-df/virt_df.ml +++ b/virt-df/virt_df.ml @@ -1,5 +1,20 @@ (* 'df' command for virtual domains. - * $Id$ + (C) Copyright 2007-2008 Richard W.M. Jones, Red Hat Inc. + http://libvirt.org/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *) open Printf @@ -22,6 +37,7 @@ let (/^) = Int64.div let uri = ref None let inodes = ref false let human = ref false +let all = ref false (* Maximum number of extended partitions possible. *) let max_extended_partitions = 100 @@ -89,7 +105,7 @@ let rec probe_device dom_name target source = let size = (LargeFile.fstat fd).LargeFile.st_size in let size = size /^ sector_size in (* Size in sectors. *) - print_device dom_name target source size; + (*print_device dom_name target source size;*) let partitions = probe_mbr fd in @@ -109,9 +125,9 @@ let rec probe_device dom_name target source = None ) partitions in let stats = List.filter_map (fun x -> x) stats in - print_stats stats + print_stats dom_name stats ) else (* Not an MBR, assume it's a single partition. *) - print_stats [target, probe_partition target None fd 0L size]; + print_stats dom_name [target, probe_partition target None fd 0L size]; close fd @@ -228,57 +244,59 @@ and probe_partition target part_type fd start size = ProbeFailed (sprintf "unsupported partition type %02x" part_type) -and print_stats statss = +and print_stats dom_name statss = List.iter ( - function - (* Swap partition. *) - | (target, Swap { swap_name = swap_name; - swap_block_size = block_size; - swap_blocks_total = blocks_total }) -> - if not !human then - printf "\t%s %Ld %s\n" - target (block_size *^ blocks_total /^ 1024L) swap_name - else - printf "\t%s %s %s\n" - target (printable_size (block_size *^ blocks_total)) swap_name - - (* Ordinary filesystem. *) - | (target, Filesystem stats) -> - printf "\t%s " target; - - if not !inodes then ( (* Block display. *) - (* 'df' doesn't count the restricted blocks. *) - let blocks_total = - stats.fs_blocks_total -^ stats.fs_blocks_reserved in - let blocks_avail = - stats.fs_blocks_avail -^ stats.fs_blocks_reserved in - let blocks_avail = - if blocks_avail < 0L then 0L else blocks_avail in - - if not !human then ( (* Display 1K blocks. *) - printf "%Ld %Ld %Ld %s\n" - (blocks_total *^ stats.fs_block_size /^ 1024L) - (stats.fs_blocks_used *^ stats.fs_block_size /^ 1024L) - (blocks_avail *^ stats.fs_block_size /^ 1024L) - stats.fs_name - ) else ( (* Human-readable blocks. *) - printf "%s %s %s %s\n" - (printable_size (blocks_total *^ stats.fs_block_size)) - (printable_size (stats.fs_blocks_used *^ stats.fs_block_size)) - (printable_size (blocks_avail *^ stats.fs_block_size)) + fun (target, fs_probe_t) -> + let dom_target = dom_name ^ ":" ^ target in + printf "%-20s " dom_target; + + match fs_probe_t with + (* Swap partition. *) + | Swap { swap_name = swap_name; + swap_block_size = block_size; + swap_blocks_total = blocks_total } -> + if not !human then + printf "%10Ld %s\n" + (block_size *^ blocks_total /^ 1024L) swap_name + else + printf "%10s %s\n" + (printable_size (block_size *^ blocks_total)) swap_name + + (* Ordinary filesystem. *) + | Filesystem stats -> + if not !inodes then ( (* Block display. *) + (* 'df' doesn't count the restricted blocks. *) + let blocks_total = + stats.fs_blocks_total -^ stats.fs_blocks_reserved in + let blocks_avail = + stats.fs_blocks_avail -^ stats.fs_blocks_reserved in + let blocks_avail = + if blocks_avail < 0L then 0L else blocks_avail in + + if not !human then ( (* Display 1K blocks. *) + printf "%10Ld %10Ld %10Ld %s\n" + (blocks_total *^ stats.fs_block_size /^ 1024L) + (stats.fs_blocks_used *^ stats.fs_block_size /^ 1024L) + (blocks_avail *^ stats.fs_block_size /^ 1024L) + stats.fs_name + ) else ( (* Human-readable blocks. *) + printf "%10s %10s %10s %s\n" + (printable_size (blocks_total *^ stats.fs_block_size)) + (printable_size (stats.fs_blocks_used *^ stats.fs_block_size)) + (printable_size (blocks_avail *^ stats.fs_block_size)) + stats.fs_name + ) + ) else ( (* Inodes display. *) + printf "%10Ld %10Ld %10Ld %s\n" + stats.fs_inodes_total stats.fs_inodes_used stats.fs_inodes_avail stats.fs_name ) - ) else ( (* Inodes display. *) - printf "%Ld %Ld %Ld %s\n" - stats.fs_inodes_total stats.fs_inodes_used stats.fs_inodes_avail - stats.fs_name - ) - (* Unsupported filesystem or other failure. *) - | (target, ProbeFailed reason) -> - printf "\t%s %s\n" target reason + (* Unsupported filesystem or other failure. *) + | ProbeFailed reason -> + printf " %s\n" reason - | (_, ProbeIgnore) -> () + | ProbeIgnore -> () ) statss (* Target is something like "hda" and size is the size in sectors. *) @@ -308,13 +326,26 @@ let main () = (* Command line argument parsing. *) let set_uri = function "" -> uri := None | u -> uri := Some u in + let version () = + printf "virt-df %s\n" (Libvirt_version.version); + + let major, minor, release = + let v, _ = Libvirt.get_version () in + v / 1_000_000, (v / 1_000) mod 1_000, v mod 1_000 in + printf "libvirt %d.%d.%d\n" major minor release; + exit 0 + in + let argspec = Arg.align [ + "-a", Arg.Set all, " Show all domains (default: only active domains)"; + "--all", Arg.Set all, " Show all domains (default: only active domains)"; "-c", Arg.String set_uri, "uri Connect to URI (default: Xen)"; "--connect", Arg.String set_uri, "uri Connect to URI (default: Xen)"; "-h", Arg.Set human, " Print sizes in human-readable format"; "--human-readable", Arg.Set human, " Print sizes in human-readable format"; "-i", Arg.Set inodes, " Show inodes instead of blocks"; "--inodes", Arg.Set inodes, " Show inodes instead of blocks"; + "--version", Arg.Unit version, " Display version and exit"; ] in let anon_fun str = raise (Arg.Bad (str ^ ": unknown parameter")) in @@ -346,11 +377,15 @@ OPTIONS" in let nr_active_doms = C.num_of_domains conn in let active_doms = Array.to_list (C.list_domains conn nr_active_doms) in let active_doms = List.map (D.lookup_by_id conn) active_doms in - let nr_inactive_doms = C.num_of_defined_domains conn in - let inactive_doms = - Array.to_list (C.list_defined_domains conn nr_inactive_doms) in - let inactive_doms = List.map (D.lookup_by_name conn) inactive_doms in - active_doms @ inactive_doms in + if not !all then + active_doms + else ( + let nr_inactive_doms = C.num_of_defined_domains conn in + let inactive_doms = + Array.to_list (C.list_defined_domains conn nr_inactive_doms) in + let inactive_doms = List.map (D.lookup_by_name conn) inactive_doms in + active_doms @ inactive_doms + ) in (* Get their XML. *) let xmls = List.map D.get_xml_desc doms in @@ -445,6 +480,16 @@ OPTIONS" in { dom_name = name; dom_id = domid; dom_disks = disks } ) xmls in + (* Print the title. *) + let () = + let total, used, avail = + match !inodes, !human with + | false, false -> "1K-blocks", "Used", "Available" + | false, true -> "Size", "Used", "Available" + | true, _ -> "Inodes", "IUse", "IFree" in + printf "%-20s %10s %10s %10s %s\n%!" + "Filesystem" total used avail "Type" in + (* Probe the devices. *) List.iter ( fun { dom_name = dom_name; dom_disks = dom_disks } ->