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 You must I<not> use C<virt-edit> on live virtual machines. If you do
46 this, you risk disk corruption in the VM. C<virt-edit> tries to stop
47 you from doing this, but doesn't catch all cases.
51 C<virt-edit> is a command line tool to edit C<file> where C<file>
52 exists in the named virtual machine (or disk image).
54 If you want to just view a file, use L<virt-cat(1)>. For more complex
55 cases you should look at the L<guestfish(1)> tool.
59 virt-edit mydomain /boot/grub/grub.conf
61 virt-edit mydomain /etc/passwd
81 Display version number and exit.
87 =item B<--connect URI> | B<-c URI>
89 If using libvirt, connect to the given I<URI>. If omitted, then we
90 connect to the default libvirt hypervisor.
92 If you specify guest block devices directly, then libvirt is not used
99 GetOptions ("help|?" => \$help,
100 "version" => \$version,
101 "connect|c=s" => \$uri,
103 pod2usage (1) if $help;
105 my $g = Sys::Guestfs->new ();
106 my %h = $g->version ();
107 print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
111 pod2usage (__"virt-edit: no image, VM names or filenames to edit given")
114 my $filename = pop @ARGV;
118 $g = open_guest (\@ARGV, address => $uri, rw => 1);
120 $g = open_guest (\@ARGV, rw => 1);
125 # List of possible filesystems.
126 my @partitions = get_partitions ($g);
128 # Now query each one to build up a picture of what's in it.
130 inspect_all_partitions ($g, \@partitions,
131 use_windows_registry => 0);
133 my $oses = inspect_operating_systems ($g, \%fses);
135 my @roots = keys %$oses;
136 die __"no root device found in this operating system image" if @roots == 0;
137 die __"multiboot operating systems are not supported by virt-edit" if @roots > 1;
138 my $root_dev = $roots[0];
140 my $os = $oses->{$root_dev};
141 mount_operating_system ($g, $os, 0);
143 my ($fh, $tempname) = tempfile ();
145 # Allow this to fail in case eg. the file does not exist.
146 $g->download($filename, $tempname);
148 my $oldctime = (stat ($tempname))[10];
150 my $editor = $ENV{EDITOR};
152 system ("$editor $tempname") == 0
153 or die "edit failed: $editor: $?";
155 my $newctime = (stat ($tempname))[10];
157 if ($oldctime != $newctime) {
158 $g->upload ($tempname, $filename)
160 print __"File not changed.\n";
170 =head1 ENVIRONMENT VARIABLES
176 If set, this string is used as the editor. It may contain arguments,
179 If not set, C<vi> is used.
189 L<Sys::Guestfs::Lib(3)>,
191 L<http://libguestfs.org/>.
195 Richard W.M. Jones L<http://et.redhat.com/~rjones/>
199 Copyright (C) 2009 Red Hat Inc.
201 This program is free software; you can redistribute it and/or modify
202 it under the terms of the GNU General Public License as published by
203 the Free Software Foundation; either version 2 of the License, or
204 (at your option) any later version.
206 This program is distributed in the hope that it will be useful,
207 but WITHOUT ANY WARRANTY; without even the implied warranty of
208 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
209 GNU General Public License for more details.
211 You should have received a copy of the GNU General Public License
212 along with this program; if not, write to the Free Software
213 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.