Start virt-tools-get-key.
[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 or UUID listed on the command line\n" unless @ARGV == 1;
107
108 my $g;
109
110 if ($uri) {
111     $g = open_guest (\@ARGV, address => $uri);
112 } else {
113     $g = open_guest (\@ARGV);
114 }
115
116 $g->launch ();
117
118 # Don't care about mountpoints.  Instead, just look for a
119 # directory with one of a selection of names on one of the
120 # partitions that we found.
121 my @partitions = get_partitions ($g);
122
123 my $key;
124
125 SEARCH:
126 foreach my $partition (@partitions) {
127     eval {
128         $g->mount_ro ($partition, "/");
129         my $dir;
130         my @dirs = ("/var/lib/virt-tools", "/lib/virt-tools");
131         foreach $dir (@dirs) {
132             if ($g->is_dir ($dir) && $g->is_file ("$dir/key")) {
133                 $key = $g->cat ("$dir/key");
134                 last SEARCH;
135             }
136         }
137     };
138     $g->umount_all ();
139 }
140
141 undef $g;
142
143 die __x("{n}: no key found in guest.\nDoes it have the virt-tool-guest package installed?\n",
144         n => $ARGV[0])
145     unless $key;
146
147 print $key;
148
149 exit 0;
150
151 =head1 SEE ALSO
152
153 L<virt-ifconfig(8)>,
154 L<guestfs(3)>,
155 L<guestfish(1)>,
156 L<Sys::Guestfs(3)>,
157 L<Sys::Guestfs::Lib(3)>,
158 L<Sys::Virt(3)>,
159 L<http://libguestfs.org/>.
160
161 =head1 AUTHORS
162
163 =over 4
164
165 =item *
166
167 Richard W.M. Jones (C<rjones at redhat dot com>)
168
169 =item *
170
171 Matthew Booth (C<mbooth at redhat dot com>)
172
173 =back
174
175 =head1 COPYRIGHT
176
177 Copyright (C) 2009 Red Hat Inc.
178
179 This program is free software; you can redistribute it and/or modify
180 it under the terms of the GNU General Public License as published by
181 the Free Software Foundation; either version 2 of the License, or
182 (at your option) any later version.
183
184 This program is distributed in the hope that it will be useful,
185 but WITHOUT ANY WARRANTY; without even the implied warranty of
186 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
187 GNU General Public License for more details.
188
189 You should have received a copy of the GNU General Public License
190 along with this program; if not, write to the Free Software
191 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.