Add 'virt-rescue' command.
[libguestfs.git] / rescue / virt-rescue.pl
1 #!/usr/bin/perl -w
2 # virt-rescue
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 warnings;
20 use strict;
21
22 use Sys::Guestfs;
23 use Sys::Guestfs::Lib qw(open_guest);
24 use Pod::Usage;
25 use Getopt::Long;
26 use Locale::TextDomain 'libguestfs';
27
28 =encoding utf8
29
30 =head1 NAME
31
32 virt-rescue - Run a rescue shell on a virtual machine
33
34 =head1 SYNOPSIS
35
36  virt-rescue [--options] domname
37
38  virt-rescue [--options] disk.img [disk.img ...]
39
40 =head1 DESCRIPTION
41
42 virt-rescue gives you a rescue shell and some simple recovery tools
43 which you can use on a virtual machine disk image.
44
45 After running virt-rescue, what you see under C</> is the recovery
46 appliance.  You must mount the virtual machine's filesystems by hand,
47 eg:
48
49  # lvs
50  LV      VG        Attr   LSize   Origin Snap%  Move Log Copy%  Convert
51  lv_root vg_f11x64 -wi-a-   8.83G
52  lv_swap vg_f11x64 -wi-a- 992.00M
53  # mount /dev/vg_f11x64/lv_root /sysroot
54  # ls /sysroot
55
56 B<Note> that the virtual machine must not be powered on when you use
57 this tool.  Doing so will probably result in disk corruption in the
58 VM.  However if you use the I<--ro> (read only) option, then you can
59 attach a shell to a running machine, but the results might be strange
60 or inconsistent.
61
62 This tool is just designed for quick interactive hacking on a virtual
63 machine.  For more structured access to a virtual machine disk image,
64 you should use L<guestfs(3)>.  To get a structured shell, use
65 L<guestfish(1)>.
66
67 =head1 OPTIONS
68
69 =over 4
70
71 =cut
72
73 my $help;
74
75 =item B<--help>
76
77 Display brief help.
78
79 =cut
80
81 my $version;
82
83 =item B<--version>
84
85 Display version number and exit.
86
87 =cut
88
89 my $uri;
90
91 =item B<--connect URI> | B<-c URI>
92
93 If using libvirt, connect to the given I<URI>.  If omitted, then we
94 connect to the default libvirt hypervisor.
95
96 If you specify guest block devices directly, then libvirt is not used
97 at all.
98
99 =cut
100
101 my $readonly;
102
103 =item B<--ro> | B<-r>
104
105 Open the image read-only.
106
107 =back
108
109 =cut
110
111 GetOptions ("help|?" => \$help,
112             "version" => \$version,
113             "connect|c=s" => \$uri,
114             "ro|r" => \$readonly,
115     ) or pod2usage (2);
116 pod2usage (1) if $help;
117 if ($version) {
118     my $g = Sys::Guestfs->new ();
119     my %h = $g->version ();
120     print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
121     exit
122 }
123
124 pod2usage (__"virt-rescue: no image or VM names rescue given")
125     if @ARGV == 0;
126
127 my @args = (\@ARGV);
128 push @args, address => $uri if $uri;
129 push @args, rw => 1 unless $readonly;
130 my $g = open_guest (@args);
131
132 $g->set_direct (1);
133 $g->set_append ("guestfs_rescue=1");
134
135 $g->launch ();
136
137 exit 0;
138
139 =head1 SEE ALSO
140
141 L<guestfs(3)>,
142 L<guestfish(1)>,
143 L<virt-cat(1)>,
144 L<Sys::Guestfs(3)>,
145 L<Sys::Guestfs::Lib(3)>,
146 L<Sys::Virt(3)>,
147 L<http://libguestfs.org/>.
148
149 =head1 AUTHOR
150
151 Richard W.M. Jones L<http://et.redhat.com/~rjones/>
152
153 =head1 COPYRIGHT
154
155 Copyright (C) 2009 Red Hat Inc.
156
157 This program is free software; you can redistribute it and/or modify
158 it under the terms of the GNU General Public License as published by
159 the Free Software Foundation; either version 2 of the License, or
160 (at your option) any later version.
161
162 This program is distributed in the hope that it will be useful,
163 but WITHOUT ANY WARRANTY; without even the implied warranty of
164 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
165 GNU General Public License for more details.
166
167 You should have received a copy of the GNU General Public License
168 along with this program; if not, write to the Free Software
169 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.