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@"
24 # Uncomment this to see every shell command that is executed.
29 --long help,add:,connect:,domain:,enable:,format::,hostname:,list-operations,selinux-relabel,no-selinux-relabel,verbose,version \
32 echo "$program: problem parsing the command line arguments"
37 # This array accumulates the arguments we pass through to guestmount.
44 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 params[i++]="--format=$2"
86 params[i++]="--format"
100 --no-selinux-relabel)
108 echo "$program $version"
111 # Can't pass the -x option directly to guestmount because
112 # that stops guestmount from forking, which means we can't
113 # coordinate with guestmount when it has finished
114 # initializing. So instead set just the underlying option
115 # in libguestfs by exporting LIBGUESTFS_TRACE.
116 # Unfortunately (a) this omits FUSE calls, but don't worry
117 # about that for now, and more importantly (b) trace
118 # messages disappear into never-never land after the fork.
119 export LIBGUESTFS_TRACE=1
125 echo "Internal error!"
130 # Different sysprep operations that can be enabled. Default is to
131 # enable all of these, although some of them are only done on certain
132 # guest types (see details below).
133 if [ -z "$enable" ]; then
134 dhcp_client_state=yes
135 dhcp_server_state=yes
143 udev_persistent_net=yes
145 elif [ "$enable" = "list" ]; then
146 echo "dhcp-client-state"
147 echo "dhcp-server-state"
155 echo "udev-persistent-net"
159 for opt in $(echo "$enable" | sed 's/,/ /g'); do
161 dhcp-client-state) dhcp_client_state=yes ;;
162 dhcp-server-state) dhcp_server_state=yes ;;
163 hostname) hostname=yes ;;
164 logfiles) logfiles=yes ;;
165 net-hwaddr) net_hwaddr=yes ;;
166 random-seed) random_seed=yes ;;
167 rhn-systemid) rhn_systemid=yes ;;
168 smolt-uuid) smolt_uuid=yes ;;
169 ssh-hostkeys) ssh_hostkeys=yes ;;
170 udev-persistent-net) udev_persistent_net=yes ;;
171 yum-uuid) yum_uuid=yes ;;
173 echo "error: unknown --enable feature: $opt"
179 # Make sure there were no extra parameters on the command line.
180 if [ $# -gt 0 ]; then
181 echo "error: $program: extra parameters on the command line"
186 # Did the user specify at least one -a or -d option?
187 if [ $add_params -eq 0 ]; then
188 echo "error: $program: you need at least one -a or -d option"
193 # end of command line parsing
194 #----------------------------------------------------------------------
198 if [ "$verbose" = "yes" ]; then
199 echo params: "${params[@]}"
202 # Create a temporary directory for general purpose use during operations.
203 tmpdir="$(mktemp -d)"
207 if [ -d $tmpdir/mnt ]; then
208 fusermount -u $tmpdir/mnt >/dev/null 2>&1 ||:
212 trap cleanup EXIT ERR
214 # Run virt-inspector and grab inspection information about this guest.
215 virt-inspector "${params[@]}" > $tmpdir/xml
216 xmlstarlet sel -t -c \
217 "string(/operatingsystems/operatingsystem[position()=1]/name)" \
218 $tmpdir/xml > $tmpdir/type
219 xmlstarlet sel -t -c \
220 "string(/operatingsystems/operatingsystem[position()=1]/distro)" \
221 $tmpdir/xml > $tmpdir/distro ||:
222 xmlstarlet sel -t -c \
223 "string(/operatingsystems/operatingsystem[position()=1]/package_format)" \
224 $tmpdir/xml > $tmpdir/package_format ||:
225 xmlstarlet sel -t -c \
226 "string(/operatingsystems/operatingsystem[position()=1]/package_management)" \
227 $tmpdir/xml > $tmpdir/package_management ||:
229 type="$(cat $tmpdir/type)"
230 distro="$(cat $tmpdir/distro)"
231 package_format="$(cat $tmpdir/package_format)"
232 package_management="$(cat $tmpdir/package_management)"
236 guestmount --rw -i "${params[@]}" $tmpdir/mnt
240 #----------------------------------------------------------------------
241 # The sysprep operations.
243 if [ "$dhcp_client_state" = "yes" ]; then
246 rm -rf $mnt/var/lib/dhclient/*
248 rm -rf $mnt/var/lib/dhcp/*
253 if [ "$dhcp_server_state" = "yes" ]; then
256 rm -rf $mnt/var/lib/dhcpd/*
261 if [ "$hostname" = "yes" ]; then
262 case "$type/$distro" in
264 echo "HOSTNAME=$hostname_param" > $mnt/etc/sysconfig/network.new
265 sed '/^HOSTNAME=/d' < $mnt/etc/sysconfig/network >> $mnt/etc/sysconfig/network.new
266 mv -f $mnt/etc/sysconfig/network.new $mnt/etc/sysconfig/network
269 linux/debian|linux/ubuntu)
270 echo "$hostname_param" > $mnt/etc/hostname
276 if [ "$logfiles" = "yes" ]; then
279 rm -rf $mnt/var/log/*.log*
280 rm -rf $mnt/var/log/audit/*
281 rm -rf $mnt/var/log/btmp*
282 rm -rf $mnt/var/log/cron*
283 rm -rf $mnt/var/log/dmesg*
284 rm -rf $mnt/var/log/lastlog*
285 rm -rf $mnt/var/log/maillog*
286 rm -rf $mnt/var/log/mail/*
287 rm -rf $mnt/var/log/messages*
288 rm -rf $mnt/var/log/secure*
289 rm -rf $mnt/var/log/spooler*
290 rm -rf $mnt/var/log/tallylog*
291 rm -rf $mnt/var/log/wtmp*
296 if [ "$net_hwaddr" = "yes" ]; then
297 case "$type/$distro" in
299 if [ -d $mnt/etc/sysconfig/network-scripts ]; then
302 sed '/^HWADDR=/d' < "$1" > "$1.new"
306 find $mnt/etc/sysconfig/network-scripts \
307 -name 'ifcfg-*' -type f \
308 -exec bash -c 'rm_hwaddr "$0"' {} \;
315 if [ "$random_seed" = "yes" -a "$type" = "linux" ]; then
317 if [ -f $mnt/var/lib/random-seed ]; then
319 f=$mnt/var/lib/random-seed
320 elif [ -f $mnt/var/lib/urandom/random-seed ]; then
322 f=$mnt/var/lib/urandom/random-seed
325 dd if=/dev/random of="$f" bs=8 count=1 conv=nocreat,notrunc 2>/dev/null
329 if [ "$rhn_systemid" = "yes" -a "$type/$distro" = "linux/rhel" ]; then
330 rm -f $mnt/etc/sysconfig/rhn/systemid
333 if [ "$smolt_uuid" = "yes" -a "$type" = "linux" ]; then
334 rm -f $mnt/etc/sysconfig/hw-uuid
335 rm -f $mnt/etc/smolt/uuid
336 rm -f $mnt/etc/smolt/hw-uuid
339 if [ "$ssh_hostkeys" = "yes" -a "$type" != "windows" ]; then
340 rm -rf $mnt/etc/ssh/*_host_*
343 if [ "$udev_persistent_net" = "yes" -a "$type" = "linux" ]; then
344 rm -f $mnt/etc/udev/rules.d/70-persistent-net.rules
347 if [ "$yum_uuid" = "yes" -a "$package_management" = "yum" ]; then
348 rm -f $mnt/var/lib/yum/uuid
351 #----------------------------------------------------------------------
352 # Clean up and close down.
354 # If we created any new files and the guest uses SELinux, then we have
355 # to relabel the filesystem on boot. Could do with a better way to
356 # test "guest uses SELinux" (XXX).
357 case "$selinux_relabel/$created_files" in
359 touch $mnt/.autorelabel;;
361 case "$type/$distro" in
362 linux/fedora|linux/rhel|linux/centos|linux/scientificlinux|linux/redhat-based)
363 touch $mnt/.autorelabel
371 fusermount -u $tmpdir/mnt