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 File::Temp qw/tempfile/;
29 use Locale::TextDomain 'libguestfs';
35 virt-edit - Edit a file in a virtual machine
39 virt-edit [--options] domname file
41 virt-edit [--options] disk.img [disk.img ...] file
45 C<virt-edit> is a command line tool to edit C<file> where C<file>
46 exists in the named virtual machine (or disk image).
48 B<Note> you must I<not> use virt-edit on live virtual machines. If
49 you do this, you risk disk corruption in the VM.
51 If you want to just view a file, use L<virt-cat(1)>. For more complex
52 cases you should look at the L<guestfish(1)> tool.
56 virt-edit mydomain /boot/grub/grub.conf
58 virt-edit mydomain /etc/passwd
78 Display version number and exit.
84 =item B<--connect URI> | B<-c URI>
86 If using libvirt, connect to the given I<URI>. If omitted, then we
87 connect to the default libvirt hypervisor.
89 If you specify guest block devices directly, then libvirt is not used
96 GetOptions ("help|?" => \$help,
97 "version" => \$version,
98 "connect|c=s" => \$uri,
100 pod2usage (1) if $help;
102 my $g = Sys::Guestfs->new ();
103 my %h = $g->version ();
104 print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
108 pod2usage (__"virt-edit: no image, VM names or filenames to edit given")
111 my $filename = pop @ARGV;
115 $g = open_guest (\@ARGV, address => $uri, rw => 1);
117 $g = open_guest (\@ARGV, rw => 1);
122 # List of possible filesystems.
123 my @partitions = get_partitions ($g);
125 # Now query each one to build up a picture of what's in it.
127 inspect_all_partitions ($g, \@partitions,
128 use_windows_registry => 0);
130 my $oses = inspect_operating_systems ($g, \%fses);
132 my @roots = keys %$oses;
133 die __"no root device found in this operating system image" if @roots == 0;
134 die __"multiboot operating systems are not supported by virt-edit" if @roots > 1;
135 my $root_dev = $roots[0];
137 my $os = $oses->{$root_dev};
138 mount_operating_system ($g, $os, 0);
140 my ($fh, $tempname) = tempfile ();
142 # Allow this to fail in case eg. the file does not exist.
143 $g->download($filename, $tempname);
145 my $oldctime = (stat ($tempname))[10];
147 my $editor = $ENV{EDITOR};
149 system ("$editor $tempname") == 0
150 or die "edit failed: $editor: $?";
152 my $newctime = (stat ($tempname))[10];
154 if ($oldctime != $newctime) {
155 $g->upload ($tempname, $filename)
157 print __"File not changed.\n";
167 =head1 ENVIRONMENT VARIABLES
173 If set, this string is used as the editor. It may contain arguments,
176 If not set, C<vi> is used.
186 L<Sys::Guestfs::Lib(3)>,
188 L<http://libguestfs.org/>.
192 Richard W.M. Jones L<http://et.redhat.com/~rjones/>
196 Copyright (C) 2009 Red Hat Inc.
198 This program is free software; you can redistribute it and/or modify
199 it under the terms of the GNU General Public License as published by
200 the Free Software Foundation; either version 2 of the License, or
201 (at your option) any later version.
203 This program is distributed in the hope that it will be useful,
204 but WITHOUT ANY WARRANTY; without even the implied warranty of
205 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
206 GNU General Public License for more details.
208 You should have received a copy of the GNU General Public License
209 along with this program; if not, write to the Free Software
210 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.