3 # libguestfs virt-sysprep tool
4 # Copyright (C) 2011 Red Hat Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 program="virt-sysprep"
22 version="@PACKAGE_VERSION@"
26 --long help,add:,connect:,domain:,enable:,format::,hostname:,list-operations,verbose,version \
29 echo "$program: problem parsing the command line arguments"
34 # This array accumulates the arguments we pass through to guestfish.
36 guestfish[0]="guestfish"
38 guestfish[2]="--listen"
45 hostname_param=localhost.localdomain
50 echo " $program [--options] -d domname"
51 echo " $program [--options] -a disk.img [-a disk.img ...]"
53 echo "Read $program(1) man page for more information."
55 echo "NOTE: $program modifies the guest or disk image *in place*."
76 if [ -n "$enable" ]; then
77 echo "error: --enable option can only be given once"
84 guestfish[i++]="--format=$2"
86 guestfish[i++]="--format"
102 echo "$program $version"
111 echo "Internal error!"
116 # Different sysprep operations that can be enabled. Default is to
117 # enable all of these, although some of them are only done on certain
118 # guest types (see details below).
119 if [ -z "$enable" ]; then
123 udev_persistent_net=yes
124 elif [ "$enable" = "list" ]; then
128 echo "udev-persistent-net"
131 for opt in $(echo "$enable" | sed 's/,/ /g'); do
133 hostname) hostname=yes ;;
134 net-hwaddr) net_hwaddr=yes ;;
135 ssh-hostkeys) ssh_hostkeys=yes ;;
136 udev-persistent-net) udev_persistent_net=yes ;;
138 echo "error: unknown --enable feature: $opt"
144 # Make sure there were no extra parameters on the command line.
145 if [ $# -gt 0 ]; then
146 echo "error: $program: extra parameters on the command line"
151 # Did the user specify at least one -a or -d option?
152 if [ $add_params -eq 0 ]; then
153 echo "error: $program: you need at least one -a or -d option"
158 # end of command line parsing
159 #----------------------------------------------------------------------
163 if [ "$verbose" = "yes" ]; then
164 echo command: "${guestfish[@]}"
167 # Create a temporary directory for general purpose use during operations.
168 tmpdir="$(mktemp -d)"
172 eval $("${guestfish[@]}")
173 if [ -z "$GUESTFISH_PID" ]; then
174 echo "$program: guestfish didn't start up, see error messages above"
180 kill $GUESTFISH_PID >/dev/null 2>&1 ||:
185 # Launch back-end, inspect for operating systems, and get the guest
187 root=$(guestfish --remote inspect-get-roots)
189 if [ "$root" = "" ]; then
190 echo "$program: no operating system was found on this disk"
194 if [ "$verbose" = "yes" ]; then
198 # Get the guest type.
199 type="$(guestfish --remote -- -inspect-get-type $root)"
201 if [ "$type" = "linux" ]; then
202 distro="$(guestfish --remote -- -inspect-get-distro $root)"
205 if [ "$type" = "windows" ]; then
206 systemroot="$(guestfish --remote -- -inspect-get-windows-systemroot $root)"
209 #----------------------------------------------------------------------
212 # erase_line filename regex
214 # Erase line(s) in a file that match the given regex.
217 guestfish --remote -- download "$1" "$tmpdir/file"
218 sed "/$2/d" < "$tmpdir/file" > "$tmpdir/file.1"
219 guestfish --remote -- upload "$tmpdir/file.1" "$1"
224 # Remove files. Doesn't fail if no files exist. Note the wildcard
225 # parameter cannot contain spaces or characters that need special
229 files=$(guestfish --remote -- glob-expand "$1")
231 guestfish --remote -- rm "$f"
237 # Remove a single file. No error if the file doesn't exist or is not
241 t=$(guestfish --remote -- is-file "$1")
242 if [ "$t" = "true" ]; then
243 guestfish --remote -- rm "$1"
247 #----------------------------------------------------------------------
248 # The sysprep operations.
250 if [ "$hostname" = "yes" ]; then
251 case "$type/$distro" in
253 guestfish --remote -- \
254 download /etc/sysconfig/network "$tmpdir/network"
255 echo "HOSTNAME=$hostname_param" > "$tmpdir/network.1"
256 sed '/^HOSTNAME=/d' < "$tmpdir/network" >> "$tmpdir/network.1"
257 guestfish --remote -- \
258 upload "$tmpdir/network.1" /etc/sysconfig/network ;;
259 linux/debian|linux/ubuntu)
260 guestfish --remote -- write /etc/hostname "$hostname_param"
264 if [ "$net_hwaddr" = "yes" ]; then
265 case "$type/$distro" in
267 # XXX these filenames can have spaces and untrusted chars in them!
268 files=$(guestfish --remote -- glob-expand '/etc/sysconfig/network-scripts/ifcfg-*')
270 erase_line "$f" "^HWADDR="
275 if [ "$ssh_hostkeys" = "yes" -a "$type" != "windows" ]; then
276 rm_files "/etc/ssh/*_host_*"
279 if [ "$udev_persistent_net" = "yes" -a "$type" = "linux" ]; then
280 rm_file /etc/udev/rules.d/70-persistent-net.rules
283 # Clean up and close down.
285 guestfish --remote umount-all
286 guestfish --remote sync
287 guestfish --remote exit