3 # Copyright (C) 2009 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.
21 use Sys::Guestfs::Lib qw(open_guest get_partitions);
24 use Locale::TextDomain 'virt-tools';
30 virt-tools-get-key - virt-tools helper to get the guest's key
34 virt-tools-get-key [--options] domname
38 This helper program is used by L<virt-tools(8)> to get the guest's
39 secret key. If you don't know anything about this, you probably want
40 to start by reading L<virt-tools(8)>. Otherwise read on.
42 The single command line argument should be a libvirt domain name (see
47 The cache is described in detail in L<virt-tools(8)>. In brief, if
48 C<@localstatedir@/lib/virt-tools/keys/E<lt>UUIDE<gt>> exists (where
49 E<lt>UUIDE<gt> is the guest's UUID), then the contents of that file
50 are returned directly. Otherwise we will try to create this file
51 after reading the key so that we don't have to read the key out of the
52 guest's filesystem each time.
72 Display version number and exit.
78 =item B<--connect URI> | B<-c URI>
80 If using libvirt, connect to the given I<URI>. If omitted, then we
81 connect to the default libvirt hypervisor.
87 =item B<--verbose> | B<-v>
89 Enable verbose messages, useful for debugging.
95 GetOptions ("help|?" => \$help,
96 "version" => \$version,
97 "connect|c=s" => \$uri,
98 "verbose|v" => \$verbose,
100 pod2usage (1) if $help;
102 print "@PACKAGE_STRING@\n";
106 die __"no domain name or UUID listed on the command line\n" unless @ARGV == 1;
108 my ($g, $conn, $dom);
111 ($g, $conn, $dom) = open_guest (\@ARGV, address => $uri);
113 ($g, $conn, $dom) = open_guest (\@ARGV);
116 my $uuid = $dom->get_uuid_string ();
121 # See if the UUID exists in the cache already.
122 print STDERR "checking for UUID $uuid in the cache directory\n" if $verbose;
124 my $cachedir = "@localstatedir@/lib/virt-tools/keys";
125 if (-r "$cachedir/$uuid") {
126 print STDERR "$cachedir/$uuid exists, returning contents\n" if $verbose;
127 open FILE, "$cachedir/$uuid" or die "$cachedir/$uuid: $!";
135 print STDERR "$cachedir/$uuid not found, looking inside guest\n" if $verbose;
139 # Don't care about mountpoints. Instead, just look for a
140 # directory with one of a selection of names on one of the
141 # partitions that we found.
142 my @partitions = get_partitions ($g);
146 foreach my $partition (@partitions) {
148 $g->mount_ro ($partition, "/");
150 my @dirs = ("/var/lib/virt-tools", "/lib/virt-tools");
151 foreach $dir (@dirs) {
152 if ($g->is_dir ($dir) && $g->is_file ("$dir/key")) {
153 $key = $g->cat ("$dir/key");
164 die __x("{n}: no key found in guest.\nDoes it have the virt-tool-guest package installed?\n",
168 print STDERR "try to write key to $cachedir/$uuid\n" if $verbose;
170 if (open FILE, ">$cachedir/$uuid") {
185 L<Sys::Guestfs::Lib(3)>,
187 L<http://libguestfs.org/>.
195 Richard W.M. Jones (C<rjones at redhat dot com>)
199 Matthew Booth (C<mbooth at redhat dot com>)
205 Copyright (C) 2009 Red Hat Inc.
207 This program is free software; you can redistribute it and/or modify
208 it under the terms of the GNU General Public License as published by
209 the Free Software Foundation; either version 2 of the License, or
210 (at your option) any later version.
212 This program is distributed in the hope that it will be useful,
213 but WITHOUT ANY WARRANTY; without even the implied warranty of
214 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
215 GNU General Public License for more details.
217 You should have received a copy of the GNU General Public License
218 along with this program; if not, write to the Free Software
219 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.