4 # Copyright (C) 2009 Red Hat Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 use XML::XPath::XMLParser;
30 hostinfo-status - check if hostinfo is enabled for a guest
34 hostinfo-status [GUESTNAME [GUESTNAME...]]
38 This command finds out if hostinfo is enabled or disabled for a
43 # hostinfo-status myguest
44 myguest: hostinfo is enabled on serial port 1 (ttyS1, COM2)
46 # hostinfo-status anotherguest
47 anotherguest: hostinfo is disabled
49 If no guest names are listed on the command line, this command
50 displays the status of all guests known to libvirt.
52 If you want to find out if the hostinfo feature is enabled for the
53 host as a whole, you should use this command instead:
55 service hostinfo status
77 Display version number and exit.
83 =item B<--connect URI> | B<-c URI>
85 Connect to libvirt using the given URI. If omitted then
86 we connect to the default libvirt hypervisor.
94 Use this option from scripts to test if hostinfo is enabled
95 for a single guest without the verbose messages:
97 if hostinfo-status --quiet myguest; then
98 echo Yes, hostinfo is enabled for this guest.
100 echo No, hostinfo is not enabled for this guest.
107 GetOptions ("help|?" => \$help,
108 "version" => \$version,
109 "connect|c=s" => \$uri,
112 pod2usage (1) if $help;
122 # Connect to libvirt.
123 my @libvirt_params = (readonly => 1);
124 push @libvirt_params, address => $uri if $uri;
125 my $conn = Sys::Virt->new (@libvirt_params);
126 die "could not connect to libvirt daemon" unless $conn;
128 # Get domains we're going to examine.
132 die "'--quiet' option given with no guest names" if $quiet;
133 @doms = $conn->list_domains ();
134 push @doms, $conn->list_defined_domains ();
136 die "'--quiet' option given with more than one guest name"
137 if $quiet && @names > 1;
138 @doms = map { $conn->get_domain_by_name ($_) } @names;
141 # Examine each domain.
142 foreach my $dom (@doms) {
143 my $name = $dom->get_name;
144 my $xml = $dom->get_xml_description ();
145 my $path = XML::XPath->new (xml => $xml);
147 # Get all <serial type='unix'> devices.
149 $path->findnodes (q{//devices/serial[@type='unix']});
151 # Get the zero/one serial port which looks like a hostinfo device.
152 # It's an error if a domain has more than one of these.
157 foreach my $serial (@serials) {
159 XML::XPath->new (xml =>
160 XML::XPath::XMLParser::as_string ($serial));
162 # Check it's a hostinfo serial port.
164 $spath->findnodes (q{//source[starts-with(@path,"@localstatedir@/lib/hostinfo")]});
168 warn "error: $name: malformed domain XML: multiple <source path> elements\n";
173 unless ($hostinfo_serial) {
174 $hostinfo_serial = $serial;
176 warn "error: $name: domain appears to have multiple hostinfo serial ports\n";
181 # Get the target port.
183 $spath->findnodes (q{//target/@port});
184 if (@targports == 0) {
185 warn "error: $name: malformed domain XML: no <target port> elements\n";
189 if (@targports > 1) {
190 warn "error: $name: malformed domain XML: multiple <target port> elements\n";
194 $hostinfo_port = $targports[0]->getData;
198 $ret = $errors ? 2 : defined ($hostinfo_serial) ? 0 : 1;
200 if ($hostinfo_serial) {
201 if (defined $hostinfo_port) {
202 my $windows_port = $hostinfo_port+1;
203 print "$name: hostinfo is enabled on serial port $hostinfo_port (ttyS$hostinfo_port, COM$windows_port)\n";
205 print "$name: hostinfo is enabled on an undefined serial port\n";
208 print "$name: hostinfo is disabled\n";
218 Normally C<hostinfo-status> returns 0 if the command was successful,
219 or 2 if there was an error.
221 However if you use the C<--quiet> option, it returns 0 if hostinfo
222 is enabled, 1 if hostinfo is disabled, or 2 if there was an error.
226 my $ret = eval { &main };
241 L<http://fedoraproject.org/wiki/Features/Hostinfo>,
242 L<http://libvirt.org/>.
246 Richard W.M. Jones (C<rjones at redhat dot com>)
250 Copyright (C) 2009 Red Hat Inc.
251 L<http://fedoraproject.org/wiki/Features/Hostinfo>
253 This program is free software; you can redistribute it and/or modify
254 it under the terms of the GNU General Public License as published by
255 the Free Software Foundation; either version 2 of the License, or
256 (at your option) any later version.
258 This program is distributed in the hope that it will be useful,
259 but WITHOUT ANY WARRANTY; without even the implied warranty of
260 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
261 GNU General Public License for more details.
263 You should have received a copy of the GNU General Public License
264 along with this program; if not, write to the Free Software
265 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.