Update
authorRichard Jones <rjones@trick.home.annexia.org>
Thu, 24 Sep 2009 16:41:04 +0000 (17:41 +0100)
committerRichard Jones <rjones@trick.home.annexia.org>
Thu, 24 Sep 2009 16:41:04 +0000 (17:41 +0100)
tools/virt-tools-get-key.pl
tools/virt-uname.pl

index 3dce2db..6885256 100755 (executable)
@@ -45,7 +45,7 @@ C<virsh list --all>).
 =head2 KEY CACHE
 
 The cache is described in detail in L<virt-tools(8)>.  In brief, if
-C<@LOCALSTATEDIR@/lib/virt-tools/keys/E<lt>UUIDE<gt>> exists (where
+C<@localstatedir@/lib/virt-tools/keys/E<lt>UUIDE<gt>> exists (where
 E<lt>UUIDE<gt> is the guest's UUID), then the contents of that file
 are returned directly.  Otherwise we will try to create this file
 after reading the key so that we don't have to read the key out of the
@@ -105,14 +105,35 @@ if ($version) {
 
 die __"no domain name or UUID listed on the command line\n" unless @ARGV == 1;
 
-my $g;
+my ($g, $conn, $dom);
 
 if ($uri) {
-    $g = open_guest (\@ARGV, address => $uri);
+    ($g, $conn, $dom) = open_guest (\@ARGV, address => $uri);
 } else {
-    $g = open_guest (\@ARGV);
+    ($g, $conn, $dom) = open_guest (\@ARGV);
 }
 
+my $uuid = $dom->get_uuid_string ();
+
+undef $dom;
+undef $conn;
+
+# See if the UUID exists in the cache already.
+print STDERR "checking for UUID $uuid in the cache directory\n" if $verbose;
+
+my $cachedir = "@localstatedir@/lib/virt-tools/keys";
+if (-r "$cachedir/$uuid") {
+    print STDERR "$cachedir/$uuid exists, returning contents\n" if $verbose;
+    open FILE, "$cachedir/$uuid" or die "$cachedir/$uuid: $!";
+    my $key = <FILE>;
+    chomp $key;
+    close FILE;
+    print $key, "\n";
+    exit 0;
+}
+
+print STDERR "$cachedir/$uuid not found, looking inside guest\n" if $verbose;
+
 $g->launch ();
 
 # Don't care about mountpoints.  Instead, just look for a
@@ -122,7 +143,6 @@ my @partitions = get_partitions ($g);
 
 my $key;
 
-SEARCH:
 foreach my $partition (@partitions) {
     eval {
        $g->mount_ro ($partition, "/");
@@ -131,11 +151,12 @@ foreach my $partition (@partitions) {
        foreach $dir (@dirs) {
            if ($g->is_dir ($dir) && $g->is_file ("$dir/key")) {
                $key = $g->cat ("$dir/key");
-               last SEARCH;
+               last;
            }
        }
     };
     $g->umount_all ();
+    last if $key;
 }
 
 undef $g;
@@ -144,6 +165,13 @@ die __x("{n}: no key found in guest.\nDoes it have the virt-tool-guest package i
        n => $ARGV[0])
     unless $key;
 
+print STDERR "try to write key to $cachedir/$uuid\n" if $verbose;
+
+if (open FILE, ">$cachedir/$uuid") {
+    print FILE $key;
+    close FILE
+}
+
 print $key;
 
 exit 0;
index ee1ced3..70bbd34 100755 (executable)
@@ -326,7 +326,7 @@ guest side.
 
 The guest writes various static, mostly unchanging, information into
 its own directory.  On Linux the directory is
-C<@LOCALSTATEDIR@/lib/virt-tools/> and under Windows it is
+C<@localstatedir@/lib/virt-tools/> and under Windows it is
 C<%systemroot%\virttool\>.  In the discussion below, this
 communications directory is referred to as C<$GUESTCOMMSDIR>.
 
@@ -426,13 +426,13 @@ verify its operation:
 
 C<virt-tools-get-key> caches the keys of guests that it has seen
 before so it doesn't have to read them each time.  The cache is in
-C<@LOCALSTATEDIR@/lib/virt-tools/keys/> (in the host).
+C<@localstatedir@/lib/virt-tools/keys/> (in the host).
 
 You can just delete the files in this directory at any time, I<or> you
 can drop a file in here which contains the key of a guest.
 
 To do this, create a file
-C<@LOCALSTATEDIR@/lib/virt-tools/keys/E<lt>UUIDE<gt>> where
+C<@localstatedir@/lib/virt-tools/keys/E<lt>UUIDE<gt>> where
 E<lt>UUIDE<gt> is the guest's UUID as displayed by this command:
 
  virsh domuuid <name>
@@ -487,7 +487,7 @@ command by hand.
 
 C<virt-tools-get-transport> caches the transports of guests that it
 has seen before so it doesn't have to determine them each time.  The
-cache is in C<@LOCALSTATEDIR@/lib/virt-tools/transports/> (in the
+cache is in C<@localstatedir@/lib/virt-tools/transports/> (in the
 host).
 
 As for the L</KEY CACHE>, this directory is just some files that are