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.
23 use Sys::Guestfs::Lib qw(open_guest get_partitions resolve_windows_path
24 inspect_all_partitions inspect_partition
25 inspect_operating_systems mount_operating_system);
28 use Locale::TextDomain 'libguestfs';
34 virt-tar - Extract or upload files to a virtual machine
38 virt-tar [--options] -x domname directory tarball
40 virt-tar [--options] -u domname tarball directory
42 virt-tar [--options] disk.img [disk.img ...] -x directory tarball
44 virt-tar [--options] disk.img [disk.img ...] -u tarball directory
48 Download C</home> from the VM into a local tarball:
50 virt-tar -x domname /home home.tar
52 virt-tar -zx domname /home home.tar.gz
54 Upload a local tarball and unpack it inside C</tmp> in the VM:
56 virt-tar -u domname uploadstuff.tar /tmp
58 virt-tar -zu domname uploadstuff.tar.gz /tmp
62 You must I<not> use C<virt-tar> with the C<-u> option (upload) on live
63 virtual machines. If you do this, you risk disk corruption in the VM.
64 C<virt-tar> tries to stop you from doing this, but doesn't catch all
67 You can use C<-x> (extract) on live virtual machines, but you might
68 get inconsistent results or errors if there is filesystem activity
69 inside the VM. If the live VM is synched and quiescent, then
70 C<virt-tar> will usually work, but the only way to guarantee
71 consistent results is if the virtual machine is shut down.
75 C<virt-tar> is a general purpose archive tool for downloading and
76 uploading parts of a guest filesystem. There are many possibilities:
77 making backups, uploading data files, snooping on guest activity,
78 fixing or customizing guests, etc.
80 If you want to just view a single file, use L<virt-cat(1)>. If you
81 just want to edit a single file, use L<virt-edit(1)>. For more
82 complex cases you should look at the L<guestfish(1)> tool.
84 There are two modes of operation: C<-x> (eXtract) downloads a
85 directory and its contents (recursively) from the virtual machine into
86 a local tarball. C<-u> uploads from a local tarball, unpacking it
87 into a directory inside the virtual machine. You cannot use these two
90 In addition, you may need to use the C<-z> (gZip) option to enable
91 compression. When uploading, you have to specify C<-z> if the upload
92 file is compressed because virt-tar won't detect this on its own.
94 C<virt-tar> can only handle tar (optionally gzipped) format tarballs.
95 For example it cannot do PKZip files or bzip2 compression. If you
96 want that then you'll have to rebuild the tarballs yourself. (This is
97 a limitation of the L<libguestfs(3)> API).
117 Display version number and exit.
123 =item B<--connect URI> | B<-c URI>
125 If using libvirt, connect to the given I<URI>. If omitted, then we
126 connect to the default libvirt hypervisor.
128 If you specify guest block devices directly, then libvirt is not used
135 =item B<-x> | B<--extract> | B<--download>
137 =item B<-u> | B<--upload>
139 Use C<-x> to extract (download) a directory from a virtual machine
142 Use C<-u> to upload and unpack from a local tarball into a virtual
143 machine. Please read the L</WARNING> section above before using this
146 You must specify exactly one of these options.
152 =item B<-z> | B<--gzip>
154 Specify that the input or output tarball is gzip-compressed.
162 die __"virt-tar: extract/upload mode specified twice on the command line\n"
169 die __"virt-tar: extract/upload mode specified twice on the command line\n"
174 Getopt::Long::Configure ("bundling");
175 GetOptions ("help|?" => \$help,
176 "version" => \$version,
177 "connect|c=s" => \$uri,
178 "extract|download|x" => \&set_mode_x,
179 "upload|u" => \&set_mode_u,
182 pod2usage (1) if $help;
184 my $g = Sys::Guestfs->new ();
185 my %h = $g->version ();
186 print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
190 pod2usage (__"virt-tar: no image, VM names, directory or filename given")
193 die __"virt-tar: either -x or -u must be specified on the command line\n"
196 # Note: 'pop' reads arguments right to left.
197 my ($tarball, $directory);
199 $tarball = pop @ARGV;
200 $directory = pop @ARGV;
201 } else { # $mode eq "u"
202 $directory = pop @ARGV;
203 $tarball = pop @ARGV;
204 die __x("virt-tar: {tarball}: file not found\n",
205 tarball => $tarball) unless -f $tarball;
207 die __x("virt-tar: {dir}: directory name must start with '/' character\n",
209 unless substr ($directory, 0, 1) eq "/";
212 push @args, address => $uri if $uri;
213 push @args, rw => 1 if $mode eq "u";
215 my $g = open_guest (@args);
218 # List of possible filesystems.
219 my @partitions = get_partitions ($g);
221 # Now query each one to build up a picture of what's in it.
223 inspect_all_partitions ($g, \@partitions,
224 use_windows_registry => 0);
226 my $oses = inspect_operating_systems ($g, \%fses);
228 my @roots = keys %$oses;
229 die __"multiboot operating systems are not supported by virt-tar\n" if @roots > 1;
230 my $root_dev = $roots[0];
232 my $os = $oses->{$root_dev};
233 mount_operating_system ($g, $os, $mode eq "u" ? 0 : 1);
235 # Do the tar command.
238 $g->tgz_out ($directory, $tarball);
240 $g->tar_out ($directory, $tarball);
242 } else { # mode eq "u"
244 $g->tgz_in ($tarball, $directory);
246 $g->tar_in ($tarball, $directory);
264 L<Sys::Guestfs::Lib(3)>,
266 L<http://libguestfs.org/>.
270 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
274 Copyright (C) 2009 Red Hat Inc.
276 This program is free software; you can redistribute it and/or modify
277 it under the terms of the GNU General Public License as published by
278 the Free Software Foundation; either version 2 of the License, or
279 (at your option) any later version.
281 This program is distributed in the hope that it will be useful,
282 but WITHOUT ANY WARRANTY; without even the implied warranty of
283 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
284 GNU General Public License for more details.
286 You should have received a copy of the GNU General Public License
287 along with this program; if not, write to the Free Software
288 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.