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