3 # Copyright (C) 2009-2010 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);
27 use Locale::TextDomain 'libguestfs';
33 virt-tar - Extract or upload files to a virtual machine
37 virt-tar [--options] -x domname directory tarball
39 virt-tar [--options] -u domname tarball directory
41 virt-tar [--options] disk.img [disk.img ...] -x directory tarball
43 virt-tar [--options] disk.img [disk.img ...] -u tarball directory
47 This tool is obsolete. Use L<virt-copy-in(1)>, L<virt-copy-out(1)>,
48 L<virt-tar-in(1)>, L<virt-tar-out(1)> as replacements.
52 Download C</home> from the VM into a local tarball:
54 virt-tar -x domname /home home.tar
56 virt-tar -zx domname /home home.tar.gz
58 Upload a local tarball and unpack it inside C</tmp> in the VM:
60 virt-tar -u domname uploadstuff.tar /tmp
62 virt-tar -zu domname uploadstuff.tar.gz /tmp
66 You must I<not> use C<virt-tar> with the I<-u> option (upload) on live
67 virtual machines. If you do this, you risk disk corruption in the VM.
68 C<virt-tar> tries to stop you from doing this, but doesn't catch all
71 You can use I<-x> (extract) on live virtual machines, but you might
72 get inconsistent results or errors if there is filesystem activity
73 inside the VM. If the live VM is synched and quiescent, then
74 C<virt-tar> will usually work, but the only way to guarantee
75 consistent results is if the virtual machine is shut down.
79 C<virt-tar> is a general purpose archive tool for downloading and
80 uploading parts of a guest filesystem. There are many possibilities:
81 making backups, uploading data files, snooping on guest activity,
82 fixing or customizing guests, etc.
84 If you want to just view a single file, use L<virt-cat(1)>. If you
85 just want to edit a single file, use L<virt-edit(1)>. For more
86 complex cases you should look at the L<guestfish(1)> tool.
88 There are two modes of operation: I<-x> (eXtract) downloads a
89 directory and its contents (recursively) from the virtual machine into
90 a local tarball. I<-u> uploads from a local tarball, unpacking it
91 into a directory inside the virtual machine. You cannot use these two
94 In addition, you may need to use the I<-z> (gZip) option to enable
95 compression. When uploading, you have to specify I<-z> if the upload
96 file is compressed because virt-tar won't detect this on its own.
98 C<virt-tar> can only handle tar (optionally gzipped) format tarballs.
99 For example it cannot do PKZip files or bzip2 compression. If you
100 want that then you'll have to rebuild the tarballs yourself. (This is
101 a limitation of the L<libguestfs(3)> API).
121 Display version number and exit.
127 =item B<--connect URI> | B<-c URI>
129 If using libvirt, connect to the given I<URI>. If omitted, then we
130 connect to the default libvirt hypervisor.
132 If you specify guest block devices directly, then libvirt is not used
139 =item B<--format> raw
141 Specify the format of disk images given on the command line. If this
142 is omitted then the format is autodetected from the content of the
145 If disk images are requested from libvirt, then this program asks
146 libvirt for this information. In this case, the value of the format
147 parameter is ignored.
149 If working with untrusted raw-format guest disk images, you should
150 ensure the format is always specified.
156 =item B<-x> | B<--extract> | B<--download>
158 =item B<-u> | B<--upload>
160 Use I<-x> to extract (download) a directory from a virtual machine
163 Use I<-u> to upload and unpack from a local tarball into a virtual
164 machine. Please read the L</WARNING> section above before using this
167 You must specify exactly one of these options.
173 =item B<-z> | B<--gzip>
175 Specify that the input or output tarball is gzip-compressed.
183 die __"virt-tar: extract/upload mode specified twice on the command line\n"
190 die __"virt-tar: extract/upload mode specified twice on the command line\n"
195 Getopt::Long::Configure ("bundling");
196 GetOptions ("help|?" => \$help,
197 "version" => \$version,
198 "connect|c=s" => \$uri,
199 "format=s" => \$format,
200 "extract|download|x" => \&set_mode_x,
201 "upload|u" => \&set_mode_u,
204 pod2usage (1) if $help;
206 my $g = Sys::Guestfs->new ();
207 my %h = $g->version ();
208 print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
212 pod2usage (__"virt-tar: no image, VM names, directory or filename given")
215 die __"virt-tar: either -x or -u must be specified on the command line\n"
218 # Note: 'pop' reads arguments right to left.
219 my ($tarball, $directory);
221 $tarball = pop @ARGV;
222 $directory = pop @ARGV;
223 } else { # $mode eq "u"
224 $directory = pop @ARGV;
225 $tarball = pop @ARGV;
226 die __x("virt-tar: {tarball}: file not found\n",
227 tarball => $tarball) unless -f $tarball;
229 die __x("virt-tar: {dir}: directory name must start with '/' character\n",
231 unless substr ($directory, 0, 1) eq "/";
234 push @args, address => $uri if $uri;
235 push @args, rw => 1 if $mode eq "u";
236 push @args, format => $format if defined $format;
238 my $g = open_guest (@args);
241 my @roots = $g->inspect_os ();
243 die __x("{prog}: No operating system could be detected inside this disk image.\n\nThis may be because the file is not a disk image, or is not a virtual machine\nimage, or because the OS type is not understood by libguestfs.\n\nIf you feel this is an error, please file a bug report including as much\ninformation about the disk image as possible.\n",
244 prog => basename ($0));
247 die __x("{prog}: multiboot operating systems are not supported.\n",
248 prog => basename ($0))
250 my %fses = $g->inspect_get_mountpoints ($roots[0]);
251 my @fses = sort { length $a <=> length $b } keys %fses;
252 my $mountopts = $mode eq "u" ? "" : "ro";
254 $g->mount_options ($mountopts, $fses{$_}, $_);
257 # Do the tar command.
260 $g->tgz_out ($directory, $tarball);
262 $g->tar_out ($directory, $tarball);
264 } else { # mode eq "u"
266 $g->tgz_in ($tarball, $directory);
268 $g->tar_in ($tarball, $directory);
281 Libvirt guest names can contain arbitrary characters, some of which
282 have meaning to the shell such as C<#> and space. You may need to
283 quote or escape these characters on the command line. See the shell
284 manual page L<sh(1)> for details.
297 L<Sys::Guestfs::Lib(3)>,
299 L<http://libguestfs.org/>.
303 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
307 Copyright (C) 2009 Red Hat Inc.
309 This program is free software; you can redistribute it and/or modify
310 it under the terms of the GNU General Public License as published by
311 the Free Software Foundation; either version 2 of the License, or
312 (at your option) any later version.
314 This program is distributed in the hope that it will be useful,
315 but WITHOUT ANY WARRANTY; without even the implied warranty of
316 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
317 GNU General Public License for more details.
319 You should have received a copy of the GNU General Public License
320 along with this program; if not, write to the Free Software
321 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.