Implement virt-uptime command.
[virt-tools.git] / tools / virt-tools-get-key.pl
1 #!/usr/bin/perl -w
2 # virt-tools
3 # Copyright (C) 2009 Red Hat Inc.
4 #
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.
9 #
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.
14 #
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.
18
19 use Sys::Virt;
20 use Sys::Guestfs;
21 use Sys::Guestfs::Lib qw(open_guest get_partitions);
22 use Pod::Usage;
23 use Getopt::Long;
24 use Locale::TextDomain 'virt-tools';
25
26 =encoding utf8
27
28 =head1 NAME
29
30 virt-tools-get-key - virt-tools helper to get the guest's key
31
32 =head1 SYNOPSIS
33
34  virt-tools-get-key [--options] domname
35
36 =head1 DESCRIPTION
37
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.
41
42 The single command line argument should be a libvirt domain name (see
43 C<virsh list --all>).
44
45 =head2 KEY CACHE
46
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.
53
54 =head1 OPTIONS
55
56 =over 4
57
58 =cut
59
60 my $help;
61
62 =item B<--help>
63
64 Display brief help.
65
66 =cut
67
68 my $version;
69
70 =item B<--version>
71
72 Display version number and exit.
73
74 =cut
75
76 my $uri;
77
78 =item B<--connect URI> | B<-c URI>
79
80 If using libvirt, connect to the given I<URI>.  If omitted, then we
81 connect to the default libvirt hypervisor.
82
83 =cut
84
85 my $verbose;
86
87 =item B<--verbose> | B<-v>
88
89 Enable verbose messages, useful for debugging.
90
91 =back
92
93 =cut
94
95 GetOptions ("help|?" => \$help,
96             "version" => \$version,
97             "connect|c=s" => \$uri,
98             "verbose|v" => \$verbose,
99     ) or pod2usage (2);
100 pod2usage (1) if $help;
101 if ($version) {
102     print "@PACKAGE_STRING@\n";
103     exit
104 }
105
106 die __"no domain name listed on the command line\n" unless @ARGV == 1;
107
108 my ($g, $conn, $dom);
109
110 if ($uri) {
111     ($g, $conn, $dom) = open_guest (\@ARGV, address => $uri);
112 } else {
113     ($g, $conn, $dom) = open_guest (\@ARGV);
114 }
115
116 my $uuid = $dom->get_uuid_string ();
117
118 undef $dom;
119 undef $conn;
120
121 # See if the UUID exists in the cache already.
122 print STDERR "checking for UUID $uuid in the cache directory\n" if $verbose;
123
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: $!";
128     my $key = <FILE>;
129     chomp $key;
130     close FILE;
131     print $key, "\n";
132     exit 0;
133 }
134
135 print STDERR "$cachedir/$uuid not found, looking inside guest\n" if $verbose;
136
137 $g->launch ();
138
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);
143
144 my $key;
145
146 foreach my $partition (@partitions) {
147     eval {
148         $g->mount_ro ($partition, "/");
149         my $dir;
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");
154                 last;
155             }
156         }
157     };
158     $g->umount_all ();
159     last if $key;
160 }
161
162 undef $g;
163
164 die __x("{n}: no key found in guest.\nDoes it have the virt-tool-guest package installed?\n",
165         n => $ARGV[0])
166     unless $key;
167
168 print STDERR "try to write key to $cachedir/$uuid\n" if $verbose;
169
170 if (open FILE, ">$cachedir/$uuid") {
171     print FILE $key;
172     close FILE
173 }
174
175 print $key;
176
177 exit 0;
178
179 =head1 SEE ALSO
180
181 L<virt-ifconfig(8)>,
182 L<guestfs(3)>,
183 L<guestfish(1)>,
184 L<Sys::Guestfs(3)>,
185 L<Sys::Guestfs::Lib(3)>,
186 L<Sys::Virt(3)>,
187 L<http://libguestfs.org/>.
188
189 =head1 AUTHORS
190
191 =over 4
192
193 =item *
194
195 Richard W.M. Jones (C<rjones at redhat dot com>)
196
197 =item *
198
199 Matthew Booth (C<mbooth at redhat dot com>)
200
201 =back
202
203 =head1 COPYRIGHT
204
205 Copyright (C) 2009 Red Hat Inc.
206
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.
211
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.
216
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.