X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=tools%2Fvirt-tools-get-key.pl;h=96914697503dab17c6fd1eafe8f910ab75f45f89;hb=f3691aa4f1c48682da967b2f53a32f55e732746e;hp=3dce2dbb4ec48f52c55d4617ac43ac03efb96008;hpb=f653482d5c1d2466b28792777ee7b1e57e27cfdf;p=virt-tools.git diff --git a/tools/virt-tools-get-key.pl b/tools/virt-tools-get-key.pl index 3dce2db..9691469 100755 --- a/tools/virt-tools-get-key.pl +++ b/tools/virt-tools-get-key.pl @@ -45,7 +45,7 @@ C). =head2 KEY CACHE The cache is described in detail in L. In brief, if -C<@LOCALSTATEDIR@/lib/virt-tools/keys/EUUIDE> exists (where +C<@localstatedir@/lib/virt-tools/keys/EUUIDE> exists (where EUUIDE 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 @@ -103,16 +103,37 @@ if ($version) { exit } -die __"no domain name or UUID listed on the command line\n" unless @ARGV == 1; +die __"no domain name 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 = ; + 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;