Check return value from readlink.
[libguestfs.git] / edit / virt-edit
1 #!/usr/bin/perl -w
2 # virt-edit
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 get_partitions resolve_windows_path
24   inspect_all_partitions inspect_partition
25   inspect_operating_systems mount_operating_system);
26 use Pod::Usage;
27 use Getopt::Long;
28 use File::Temp qw/tempfile/;
29 use Locale::TextDomain 'libguestfs';
30
31 =encoding utf8
32
33 =head1 NAME
34
35 virt-edit - Edit a file in a virtual machine
36
37 =head1 SYNOPSIS
38
39  virt-edit [--options] domname file
40
41  virt-edit [--options] disk.img [disk.img ...] file
42
43 =head1 DESCRIPTION
44
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).
47
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.
50
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.
53
54 =head1 EXAMPLES
55
56  virt-edit mydomain /boot/grub/grub.conf
57
58  virt-edit mydomain /etc/passwd
59
60 =head1 OPTIONS
61
62 =over 4
63
64 =cut
65
66 my $help;
67
68 =item B<--help>
69
70 Display brief help.
71
72 =cut
73
74 my $version;
75
76 =item B<--version>
77
78 Display version number and exit.
79
80 =cut
81
82 my $uri;
83
84 =item B<--connect URI> | B<-c URI>
85
86 If using libvirt, connect to the given I<URI>.  If omitted, then we
87 connect to the default libvirt hypervisor.
88
89 If you specify guest block devices directly, then libvirt is not used
90 at all.
91
92 =back
93
94 =cut
95
96 GetOptions ("help|?" => \$help,
97             "version" => \$version,
98             "connect|c=s" => \$uri,
99     ) or pod2usage (2);
100 pod2usage (1) if $help;
101 if ($version) {
102     my $g = Sys::Guestfs->new ();
103     my %h = $g->version ();
104     print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
105     exit
106 }
107
108 pod2usage (__"virt-edit: no image, VM names or filenames to edit given")
109     if @ARGV <= 1;
110
111 my $filename = pop @ARGV;
112
113 my $g;
114 if ($uri) {
115     $g = open_guest (\@ARGV, address => $uri, rw => 1);
116 } else {
117     $g = open_guest (\@ARGV, rw => 1);
118 }
119
120 $g->launch ();
121
122 # List of possible filesystems.
123 my @partitions = get_partitions ($g);
124
125 # Now query each one to build up a picture of what's in it.
126 my %fses =
127     inspect_all_partitions ($g, \@partitions,
128       use_windows_registry => 0);
129
130 my $oses = inspect_operating_systems ($g, \%fses);
131
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];
136
137 my $os = $oses->{$root_dev};
138 mount_operating_system ($g, $os, 0);
139
140 my ($fh, $tempname) = tempfile ();
141
142 # Allow this to fail in case eg. the file does not exist.
143 $g->download($filename, $tempname);
144
145 my $oldctime = (stat ($tempname))[10];
146
147 my $editor = $ENV{EDITOR};
148 $editor ||= "vi";
149 system ("$editor $tempname") == 0
150     or die "edit failed: $editor: $?";
151
152 my $newctime = (stat ($tempname))[10];
153
154 if ($oldctime != $newctime) {
155     $g->upload ($tempname, $filename)
156 } else {
157     print __"File not changed.\n";
158 }
159
160 $g->sync ();
161 $g->umount_all ();
162
163 undef $g;
164
165 exit 0;
166
167 =head1 ENVIRONMENT VARIABLES
168
169 =over 4
170
171 =item C<EDITOR>
172
173 If set, this string is used as the editor.  It may contain arguments,
174 eg. C<"emacs -nw">
175
176 If not set, C<vi> is used.
177
178 =back
179
180 =head1 SEE ALSO
181
182 L<guestfs(3)>,
183 L<guestfish(1)>,
184 L<virt-cat(1)>,
185 L<Sys::Guestfs(3)>,
186 L<Sys::Guestfs::Lib(3)>,
187 L<Sys::Virt(3)>,
188 L<http://libguestfs.org/>.
189
190 =head1 AUTHOR
191
192 Richard W.M. Jones L<http://et.redhat.com/~rjones/>
193
194 =head1 COPYRIGHT
195
196 Copyright (C) 2009 Red Hat Inc.
197
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.
202
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.
207
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.