psss / rpms / libguestfs

Forked from rpms/libguestfs 5 years ago
Clone

Blame 0004-df-Minimize-the-number-of-times-we-launch-the-libgue.patch

0882396
From 6be1ba8a63f53b4167262250be1c122c7c08f61d Mon Sep 17 00:00:00 2001
0882396
From: Richard Jones <rjones@redhat.com>
0882396
Date: Fri, 16 Jul 2010 18:11:56 +0100
0882396
Subject: [PATCH] df: Minimize the number of times we launch the libguestfs appliance.
0882396
0882396
This commit greatly improves the performance of the 'virt-df'
0882396
command by batching as many disks as possible onto a single appliance.
0882396
In many situations this means the appliance is launched only once,
0882396
versus one launch per domain as before.
0882396
0882396
However doing it this way is a lot more complex:
0882396
0882396
(1) Because of limits in Linux and virtio-blk, we can only attach
0882396
26 disks maximum at a time to the appliance.
0882396
0882396
(2) We have to use LVM filters (lvm-set-filter) to confine LVM to
0882396
the disks of a single guest.
0882396
(cherry picked from commit 45a4cd79215752234c4a5a47fb4c9c6741b1594c)
0882396
---
0882396
 tools/virt-df |  217 ++++++++++++++++++++++++++++++++++++++++++++++-----------
0882396
 1 files changed, 177 insertions(+), 40 deletions(-)
0882396
0882396
diff --git a/tools/virt-df b/tools/virt-df
0882396
index 45b7869..790dd6a 100755
0882396
--- a/tools/virt-df
0882396
+++ b/tools/virt-df
0882396
@@ -20,12 +20,11 @@ use warnings;
0882396
 use strict;
0882396
 
0882396
 use Sys::Guestfs;
0882396
-use Sys::Guestfs::Lib qw(open_guest get_partitions);
0882396
+use Sys::Guestfs::Lib qw(feature_available);
0882396
 
0882396
 use Pod::Usage;
0882396
 use Getopt::Long;
0882396
-use Data::Dumper;
0882396
-use XML::Writer;
0882396
+use File::Basename qw(basename);
0882396
 use POSIX qw(ceil);
0882396
 
0882396
 use Locale::TextDomain 'libguestfs';
0882396
@@ -151,9 +150,22 @@ if ($version) {
0882396
 # RHBZ#600977
0882396
 die __"virt-df: cannot use -h and --csv options together\n" if $human && $csv;
0882396
 
0882396
-# Open the guest handle.
0882396
+# Get the list of domains and block devices.
0882396
+#
0882396
+# We can't use Sys::Guestfs::Lib::open_guest here because we want to
0882396
+# create the libguestfs handle/appliance as few times as possible.
0882396
+#
0882396
+# If virt-df is called with no parameters, then run the libvirt
0882396
+# equivalent of "virsh list --all", get the XML for each domain, and
0882396
+# get the disk devices.
0882396
+#
0882396
+# If virt-df is called with parameters, assume it must either be a
0882396
+# single disk image filename, a list of disk image filenames, or a
0882396
+# single libvirt guest name.  Construct disk devices accordingly.
0882396
 
0882396
-if (@ARGV == 0) {
0882396
+my @domains = ();
0882396
+
0882396
+if (@ARGV == 0) {               # No params, use libvirt.
0882396
     my $conn;
0882396
 
0882396
     if ($uri) {
0882396
@@ -168,61 +180,186 @@ if (@ARGV == 0) {
0882396
     # https://bugzilla.redhat.com/show_bug.cgi?id=538041
0882396
     @doms = grep { $_->get_id () != 0 } @doms;
0882396
 
0882396
-    my @domnames = sort (map { $_->get_name () } @doms);
0882396
+    exit 0 unless @doms;
0882396
+
0882396
+    foreach my $dom (@doms) {
0882396
+        my @disks = get_disks_from_libvirt ($dom);
0882396
+        push @domains, { dom => $dom,
0882396
+                         name => $dom->get_name (),
0882396
+                         disks => \@disks }
0882396
+    }
0882396
+} elsif (@ARGV == 1) {          # One param, could be disk image or domname.
0882396
+    if (-e $ARGV[0]) {
0882396
+        push @domains, { name => basename ($ARGV[0]),
0882396
+                         disks => [ $ARGV[0] ] }
0882396
+    } else {
0882396
+        my $conn;
0882396
 
0882396
-    if (@domnames) {
0882396
-        print_title ();
0882396
-        foreach (@domnames) {
0882396
-            eval { do_df ($_); };
0882396
-            warn $@ if $@;
0882396
+        if ($uri) {
0882396
+            $conn = Sys::Virt->new (readonly => 1, address => $uri);
0882396
+        } else {
0882396
+            $conn = Sys::Virt->new (readonly => 1);
0882396
         }
0882396
+
0882396
+        my $dom = $conn->get_domain_by_name ($ARGV[0])
0882396
+            or die __x("{name} is not the name of a libvirt domain\n",
0882396
+                       name => $ARGV[0]);
0882396
+        my @disks = get_disks_from_libvirt ($dom);
0882396
+        push @domains, { dom => $dom,
0882396
+                         name => $dom->get_name (),
0882396
+                         disks => \@disks }
0882396
     }
0882396
-} else {
0882396
-    print_title ();
0882396
-    do_df (@ARGV);
0882396
+} else {                        # >= 2 params, all disk images.
0882396
+    push @domains, { name => basename ($ARGV[0]),
0882396
+                     disks => \@ARGV }
0882396
 }
0882396
 
0882396
-sub do_df
0882396
+sub get_disks_from_libvirt
0882396
 {
0882396
-    my $g;
0882396
+    my $dom = shift;
0882396
+    my $xml = $dom->get_xml_description ();
0882396
 
0882396
-    if ($uri) {
0882396
-        $g = open_guest (\@_, address => $uri);
0882396
-    } else {
0882396
-        $g = open_guest (\@_);
0882396
+    my $p = XML::XPath->new (xml => $xml);
0882396
+    my @disks = $p->findnodes ('//devices/disk/source/@dev');
0882396
+    push (@disks, $p->findnodes ('//devices/disk/source/@file'));
0882396
+
0882396
+    # Code in Sys::Guestfs::Lib dies here if there are no disks at all.
0882396
+
0882396
+    return map { $_->getData } @disks;
0882396
+}
0882396
+
0882396
+# Sort the domains by name for display.
0882396
+@domains = sort { $a->{name} cmp $b->{name} } @domains;
0882396
+
0882396
+# Since we got this far, we're somewhat sure we're going to
0882396
+# get to print the result, so display the title.
0882396
+print_title ();
0882396
+
0882396
+# To minimize the number of times we have to launch the appliance,
0882396
+# shuffle as many domains together as we can, but not exceeding 26
0882396
+# disks per request.  (26 = # of letters in the English alphabet, and
0882396
+# we are only confident that /dev/sd[a-z] will work because of various
0882396
+# limits).
0882396
+my $n = 0;
0882396
+my @request = ();
0882396
+while (@domains) {
0882396
+    while (@domains) {
0882396
+        my $c = @{$domains[0]->{disks}};
0882396
+        last if $n + $c > 26;
0882396
+        push @request, shift @domains;
0882396
+    }
0882396
+    multi_df (@request);
0882396
+}
0882396
+
0882396
+sub multi_df
0882396
+{
0882396
+    local $_;
0882396
+    my $g = Sys::Guestfs->new ();
0882396
+
0882396
+    my ($d, $disk);
0882396
+
0882396
+    foreach $d (@_) {
0882396
+        foreach $disk (@{$d->{disks}}) {
0882396
+            $g->add_drive_ro ($disk);
0882396
+        }
0882396
     }
0882396
 
0882396
     $g->launch ();
0882396
+    my $has_lvm2 = feature_available ($g, "lvm2");
0882396
 
0882396
-    my @partitions = get_partitions ($g);
0882396
-
0882396
-    # Think of a printable name for this domain.  Just choose the
0882396
-    # first parameter passed to this function, which will work for
0882396
-    # most cases (it'll either be the domain name or the first disk
0882396
-    # image name).
0882396
-    my $domname = $_[0];
0882396
-
0882396
-    # Mount each partition in turn, and if mountable, do a statvfs on it.
0882396
-    foreach my $partition (@partitions) {
0882396
-        my %stat;
0882396
-        eval {
0882396
-            $g->mount_ro ($partition, "/");
0882396
-            %stat = $g->statvfs ("/");
0882396
-        };
0882396
-        if (!$@) {
0882396
-            print_stat ($domname, $partition, \%stat);
0882396
+    my @devices = $g->list_devices ();
0882396
+    my @partitions = $g->list_partitions ();
0882396
+
0882396
+    my $n = 0;
0882396
+    foreach $d (@_) {
0882396
+        my $name = $d->{name};
0882396
+        my $nr_disks = @{$d->{disks}};
0882396
+
0882396
+        # Filter LVM to only the devices applying to the original domain.
0882396
+        my @devs = @devices[$n .. $n+$nr_disks-1];
0882396
+        $g->lvm_set_filter (\@devs) if $has_lvm2;
0882396
+
0882396
+        # Find which whole devices (RHBZ#590167), partitions and LVs
0882396
+        # contain mountable filesystems.  Stat those which are
0882396
+        # mountable, and ignore the others.
0882396
+        foreach (@devs) {
0882396
+            try_df ($name, $g, $_, canonical_dev ($_, $n));
0882396
+        }
0882396
+        foreach (filter_partitions (\@devs, @partitions)) {
0882396
+            try_df ($name, $g, $_, canonical_dev ($_, $n));
0882396
+        }
0882396
+        if ($has_lvm2) {
0882396
+            foreach ($g->lvs ()) {
0882396
+                try_df ($name, $g, $_);
0882396
+            }
0882396
+        }
0882396
+
0882396
+        $n += $nr_disks;
0882396
+    }
0882396
+}
0882396
+
0882396
+sub filter_partitions
0882396
+{
0882396
+    my $devs = shift;
0882396
+    my @devs = @$devs;
0882396
+    my @r;
0882396
+
0882396
+    foreach my $p (@_) {
0882396
+        foreach my $d (@devs) {
0882396
+            if ($p =~ /^$d\d/) {
0882396
+                push @r, $p;
0882396
+                last;
0882396
+            }
0882396
         }
0882396
-        $g->umount_all ();
0882396
     }
0882396
+
0882396
+    return @r;
0882396
+}
0882396
+
0882396
+# Calculate the canonical name for a device.
0882396
+# eg: /dev/vdb1 when offset = 1
0882396
+#     => canonical name is /dev/sda1
0882396
+sub canonical_dev
0882396
+{
0882396
+    local $_;
0882396
+    my $dev = shift;
0882396
+    my $offset = shift;
0882396
+
0882396
+    return $dev unless $dev =~ m{^/dev/.d([a-z])(\d*)$};
0882396
+    my $disk = $1;
0882396
+    my $partnum = $2;
0882396
+
0882396
+    $disk = chr (ord ($disk) - $offset);
0882396
+
0882396
+    return "/dev/sd$disk$partnum"
0882396
+}
0882396
+
0882396
+sub try_df
0882396
+{
0882396
+    local $_;
0882396
+    my $domname = shift;
0882396
+    my $g = shift;
0882396
+    my $dev = shift;
0882396
+    my $display = shift || $dev;
0882396
+
0882396
+    my %stat;
0882396
+    eval {
0882396
+        $g->mount_ro ($dev, "/");
0882396
+        %stat = $g->statvfs ("/");
0882396
+    };
0882396
+    if (!$@) {
0882396
+        print_stat ($domname, $display, \%stat);
0882396
+    }
0882396
+    $g->umount_all ();
0882396
 }
0882396
 
0882396
 sub print_stat
0882396
 {
0882396
     my $domname = shift;
0882396
-    my $partition = shift;
0882396
+    my $dev = shift;
0882396
     my $stat = shift;
0882396
 
0882396
-    my @cols = ($domname, $partition);
0882396
+    my @cols = ($domname, $dev);
0882396
 
0882396
     if (!$inodes) {
0882396
         my $bsize = $stat->{bsize};	# block size
0882396
-- 
0882396
1.7.1
0882396