summaryrefslogtreecommitdiffstats
path: root/os-plugins/plugins/xen/files/xen.examples/scripts/xmclone.sh
blob: fd31249bd49a6b34b82d8424788a825dd5c4c642 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
#! /bin/bash

#     Script to clone Xen Dom-Us.
#     Based on XenClone by Glen Davis; rewritten by Bob Brandt.
#     Further extended and restructured by Manfred Hollstein.
#     Copyright (C) 2007  Manfred Hollstein, SUSE / Novell Inc.
#
#     This program is free software; you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 2 of the License, or
#     any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with this program; if not, write to the Free Software
#     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
#     USA.

#
# Defaults
#
VERSION=0.4.5
XEN_CONFIGS=/etc/xen/vm/
XEN_BASE=/var/lib/xen/images/
SOURCE=
DESTINATION=
DUPLICATE=0
HOSTNAME=
IP=
MAC=
PART=2
DOMU_IS_FILE_BASED=
DOMU_ROOTDEV=
FORCE=no


#
# Subroutines used in this script
#

# Display the usage information for this script
function usage ()
{
	cat << EOF
Usage: ${0##*/} [-h|--help] [-v|--version] [--force] [-c dir] [-b dir] [-d]
		    [-n hostname] [-i address] [-m address]
		    [-p number-of-root-partition-within-domU]
		    [-t target-device]
		    SourcedomU NewdomU

Clones a domU, and gives a new Host name, MAC address and possibly IP address.
Once finished the new domU should boot without any additional configuration.
Currently works with single NIC, and basic bridge setup. Tested with cloning
a SLES10 install created from the SLES10 YaST Xen module.

  -h, --help	       Display this help message.
  -v, --version	       Display the version of this program.
  --force	       Silently overwrite files when destination already exists.
  -c		       Xen configuration directory which defaults to:
		       $XEN_CONFIGS
  -b		       Xen image base directory which defaults to:
		       $XEN_BASE
  -d		       Duplicate only, do not modify attributes.
  -n		       Hostname to be used, if not specified the NewdomU name
		       will be used.
  -i		       IP address to be used, if not specified the IP address
		       will not be changed.
  -m		       MAC address to be used, if not specified a psuedo-random
		       address will be used based on the ip address with the
		       format: 00:16:3e:BB:CC:DD
		       Where BB,CC,DD are the Hex octals of the IP address.
  -p		       This script assumes that the second partition on any
		       writable disk of the domU to be cloned holds the root
		       file system in which attributes have to be changed.
		       If this is not the case for you, you can specify the
		       partition number using this flag.
  -t		       If the SourcedomU uses a block device for its root/
		       boot directory, you need to specify the new block
		       device for NewdomU.

From XENSource Networking WIKI (http://wiki.xensource.com/xenwiki/XenNetworking)
Virtualised network interfaces in domains are given Ethernet MAC addresses. When
choosing MAC addresses to use, ensure you choose a unicast address. That is, one
with the low bit of the first octet set to zero. For example, an address
starting aa: is OK but ab: is not.
It is best to keep to the range of addresses declared to be "locally assigned"
(rather than allocated globally to hardware vendors). These have the second
lowest bit set to one in the first octet. For example, aa: is OK, a8: isn't.

Exit status is 0 if OK, 1 if minor problems, 2 if serious trouble.
EOF
}

# Display the version information for this script
function version ()
{
	cat << EOF
${0##*/} (Xen VM clone utility) $VERSION
${0##*/} comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it under the terms
of the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.

Written by Manfred Hollstein, based on work by Glen Davis and Bob Brandt.
EOF
}

# Find/Replace text within a file
function replace ()
{
	sed -i -e "s!$1!$2!g" "$3"
}

#
# Find the first entry that names a writable image or device, assuming
# the Dom-U is going to boot from it:
#
function get_first_writable_image ()
{
	local i

	for i in $@
	do
		case $i in
		    # Match the first entry like "'phy:/dev/md0,xvda,w',"
		    *",w'," )
			# Strip off the leading "'" character:
			i="${i#*\'}"
		    	# Strip off the final trailing "',"
			echo "${i%*\',}"
			return
			;;
		esac
	done
}

#
# Extract just the protocol and the file/device name part from a disk entry:
#
function extract_proto_and_filename ()
{
	echo "$1" | cut -d, -f1
}


#
# Make sure this script is run by the superuser root
#
if [ `id -u` -ne 0 ]
then
	echo "You must be root to run this script!" >&2
	exit 1
fi


#
# Process the parameters
#
# Must look for double -- arguments before calling getopts
#
if [ "$1" = "--version" ]
then
	version
	exit 0
fi
if [ "$1" = "--help" ]
then
	usage
	exit 0
fi
if [ "$1" = "--force" ]
then
	FORCE=yes
	shift
fi
while getopts ":b:c:dhi:m:n:p:t:v" opt
do
	case $opt in
	    b )
		XEN_BASE=$OPTARG
		;;
	    c )
		XEN_CONFIGS=$OPTARG
		;;
	    d )
		DUPLICATE=1
		;;
	    h )
		usage
		exit 1
		;;
	    i )
		IP=$OPTARG
		;;
	    m )
		MAC=$OPTARG
		;;
	    n )
		HOSTNAME=$OPTARG
		;;
	    p )
		PART=$OPTARG
		;;
	    t )
		DOMU_ROOTDEV=$OPTARG
		;;
	    v )
		version
		exit 0
		;;
	esac
done
shift $(($OPTIND-1))

if [ $# -ne 2 ]
then
	echo "Illegal number of arguments passed!" >&2
	echo "" >&2
	usage
	exit 1
fi

SOURCE=$1
DESTINATION=$2


#
# Verify the Source and Destination parameters
#
# The source and destination should be relative directory names without
# trailing /'s.  If the source does have a full path, use that path as the
# value for XEN_BASE.  Otherwise remove all but the last part of the path
# for both source and destination
#
SOURCEDIR=${SOURCE%/*}
SOURCEBASE=${SOURCE##*/}
if [ "$SOURCEDIR" != "$SOURCEBASE" ]
then
	XEN_BASE=$SOURCEDIR"/"
	SOURCE=$SOURCEBASE
fi
SOURCE=${SOURCE##*/}
DESTINATION=${DESTINATION##*/}


#
# Verify the Xen Config and Source parameters
#
# Verify the validity of each argument, ask the user if there is a problem
while [ ! -d "$XEN_CONFIGS" ]
do
	cat << EOF >&2
$XEN_CONFIGS either does not exist or is not a directory.
Please enter a valid directory.
EOF
	read -e -p "Xen Configuration Directory? " XEN_CONFIGS
done
while [ ! -d "$XEN_BASE" ]
do
	cat << EOF >&2
$XEN_BASE either does not exist or is not a directory.
Please enter a valid directory.
EOF
	read -e -p "Xen Image Base Directory? " XEN_BASE
done

#
# Directories should have a / after them
#
[ "$XEN_CONFIGS" != "" ] &&	XEN_CONFIGS="${XEN_CONFIGS%/}/"
[ "$XEN_BASE" != "" ] &&	XEN_BASE="${XEN_BASE%/}/"


#
# Verify that actual image and configuration file exist
#
while :
do
	if [ ! -f "$XEN_CONFIGS$SOURCE" ]
	then
		echo "The $XEN_CONFIGS$SOURCE file does not exist." >&2
		echo "Please select a valid file." >&2
		FILES=
		for FILE in `ls $XEN_CONFIGS`
		do
			# If the entry is a file
			[ -f "$XEN_CONFIGS$FILE" ]	&&
			FILES="$FILES $XEN_CONFIGS$FILE"
		done
		if [ -z "$FILES" ]
		then
			echo "There are no suitable files beneath $XEN_CONFIGS" >&2
			exit 1
		fi
		echo "Files beneath $XEN_CONFIGS"
		select FILE in $FILES
		do
			if [ -f "$FILE" ]
			then
				SOURCE=${FILE##*/}
				break
			fi
			echo "Invalid Selection." >&2
		done
	else
		#
		# Figure out what type of image we're using:
		#
		BOOTENTRY=$(get_first_writable_image $(sed -n -e 's,^disk[ 	]*=[ 	]*\[\(.*\)\],\1,p' "$XEN_CONFIGS$SOURCE"))
		case "$BOOTENTRY" in
		    phy:* )
			DOMU_IS_FILE_BASED=no
			;;
		    file:* |	\
		    tap:aio:* )
			DOMU_IS_FILE_BASED=yes
			;;
		    * )
			echo "Don't know how to deal with the boot protocol/device you appear to be using: $BOOTENTRY" >&2
			echo "These are the ones this script supports: \"phy:*\", \"file:*\", \"tap:aio:*\"" >&2
			exit 1
			;;
		esac
	fi


	if [ ${DOMU_IS_FILE_BASED} = yes ]
	then
		if [ ! -d "$XEN_BASE$SOURCE" ]
		then
			echo "The directory $XEN_BASE$SOURCE is invalid." >&2
			echo "Please select another one." >&2
			FILES=
			for FILE in `ls $XEN_BASE`
			do
				# If the entry is a directory and
				#   it is not empty
				[ -d "$XEN_BASE$FILE" ]		&&
				[ "`ls $XEN_BASE$FILE`" != "" ]	&&
				FILES="$FILES $XEN_BASE$FILE"
			done
			if [ -z "$FILES" ]
			then
				echo "There are no suitable directories beneath $XEN_BASE" >&2
				exit 1
			fi
			echo "Directories beneath $XEN_BASE"
			select FILE in $FILES
			do
				if [ -d "$FILE" ]
				then
					SOURCE=${FILE##*/}
					break
				fi
				echo "Invalid Selection." >&2
			done
			continue
		fi
		break

	else	# DomU is using some block device
		while [ ! -b "${DOMU_ROOTDEV}" ] || [ ! -w "${DOMU_ROOTDEV}" ]
		do
			read -e -p "You need to specify a valid block device for the new target DomU: " DOMU_ROOTDEV
		done
		break

	fi
done
BOOTIMAGE="$(echo $BOOTENTRY | awk -F : '{ print $NF }' | sed -e 's:,[^,]*,[^,]*$::')"


#
# Verify that the destination location does not already have an image or
#  config file
#
while [ -z "$DESTINATION" ]
do
	echo "You have not specified a Destination." >&2
	read -e -p "New Destination? " DESTINATION
done
while :
do
	if [ -f "$XEN_CONFIGS$DESTINATION" ] && [ $FORCE = no ]
	then
		echo "The target configuration file $XEN_CONFIGS$DESTINATION already exists!" >&2
		read -e -p "Please select a new Destination? " DESTINATION
	fi
	if [ ${DOMU_IS_FILE_BASED} = yes ]
	then
		if [ -d "$XEN_BASE$DESTINATION" ] && [ $FORCE = no ]
		then
			echo "The target image location $XEN_BASE$DESTINATION already exists!" >&2
			read -p "Please select a new Destination? " DESTINATION
			continue
		fi
	fi
	break
done


#
# Verify the network parameters (if Duplicate Only was not selected)
#
if [ $DUPLICATE -eq 0 ]
then
	if [ -z "$HOSTNAME" ]
	then
		echo "You have not entered a host name.  If you wish to, enter one now." >&2
		read -p "New host name? (Default: $DESTINATION) " HOSTNAME
	fi
	[ -z "$HOSTNAME" ] && HOSTNAME=$DESTINATION

	if [ -z "$IP" ]
	then
		echo "You have not specified an IP Address.  If you wish to change the IP address, enter one now."
		read -p "New IP Address? " IP
	fi
	while [ -n "$IP" ] && [ "${IP/*.*.*.*/ok}" != "ok" ]
	do
		echo "The IP Address you specified is invalid.  If you wish, enter a new one now."
		read -p "New IP Address? " IP
		[ -z "$IP" ] && break
	done

	if [ -z "$MASK" ]
	then
		echo "You have not specified a network mask in bits. Please enter one now. Default is 24 "
		read -p "Network mask? " MASK 
	fi
	while [ -n "$MASK" ] && [ "${MASK/**/ok}" != "ok" ]
	do
		echo "The Network mask you specified is invalid.  If you wish, enter a new one now."
		read -p "Network mask? " MASK
		[ -z "$MASK" ] && MASK=24 
	done

	if [ -z "$MAC" ]
	then
		newMAC=""
		newMACtext="(format 01:23:45:67:89:AB)"
		# If the IP Address is specified and the MAC isn't, generate one.
		if [ -n "$IP" ]
		then
			octal1=${IP%%.*}
			IP=${IP#*.}
			octal2=${IP%%.*}
			IP=${IP#*.}
			octal3=${IP%%.*}
			octal4=${IP#*.}
			IP="$octal1.$octal2.$octal3.$octal4"
			octal1="00"`echo $octal1 16 o p | dc | tr '[:upper:]' '[:lower:]'`
			octal2="00"`echo $octal2 16 o p | dc | tr '[:upper:]' '[:lower:]'`
			octal3="00"`echo $octal3 16 o p | dc | tr '[:upper:]' '[:lower:]'`
			octal4="00"`echo $octal4 16 o p | dc | tr '[:upper:]' '[:lower:]'`
			newMAC="00:16:3e:"${octal2:(-2)}":"${octal3:(-2)}":"${octal4:(-2)}
			newMACtext="(default $newMAC)"
		fi
		echo "You have not specified a MAC Address.  If you wish to change the MAC address, enter one now."
		read -p "New MAC Address? $newMACtext " MAC
		[ -z "$MAC" ] && MAC=$newMAC
	fi

	while [ "$MAC" != "" ] && [ "${MAC/[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]/ok}" != "ok" ]
	do
		echo "The MAC Address you specified is invalid.  If you wish, enter a new one now."
		read -p "New MAC Address? (format 01:23:45:67:89:AB) " MAC
		[ -z "$MAC" ] && break
	done
else
	HOSTNAME=
	IP=
	MASK=
	MAC=
fi


#
# Make sure that the source VM is not running
#
xmid=`xm domid "$SOURCE" 2>/dev/null`
if [ $? -eq 0 ] && [ -n "$xmid" ] && [ -z "${xmid//[[:digit:]]/}" ]
then
	echo "domU $SOURCE is currently running on Xen, please shutdown before cloning." >&2
	echo "The command \"xm shutdown $xmid -w\" will shutdown the domU" >&2
	exit 1
fi


#
# Copy the Xen Config file
#
SOURCECONFIG="$XEN_CONFIGS$SOURCE"
DESTCONFIG="$XEN_CONFIGS$DESTINATION"
echo "Copying Configuration files"
if ! cp -fv "$SOURCECONFIG" "$DESTCONFIG"
then
	echo "The Config file $SOURCECONFIG could not be copied to $DESTCONFIG" >&2
	exit 1
fi


#
# Edit newly copied configuration file
#
echo "Editing config file ($DESTCONFIG), correcting the new domU Name."
if ! replace "$SOURCE" "$DESTINATION" "$DESTCONFIG"
then
	echo "Unable to change the domU name in $DESTCONFIG from $SOURCE to $DESTINATION" >&2
	exit 1
fi

if [ $DUPLICATE -eq 0 ]
then
	oldUUID=`grep "^uuid[ 	]*=.*$" $DESTCONFIG`
	if [ x"${oldUUID}" = x ]
	then
		echo 'uuid="'`uuidgen`'"' >> $DESTCONFIG
	else
		sed -i -e 's,^uuid[ 	]*=.*$,uuid="'`uuidgen`'",' $DESTCONFIG
	fi
fi

if [ $DUPLICATE -eq 0 ] && [ -n "$MAC" ]
then
	# Get the vif line in the config file
	oldMAC=`grep "vif[ 	]*=[ 	]*" $DESTCONFIG`
	# extract everything between the square brackets
	oldMAC=${oldMAC#*[}
	oldMAC=${oldMAC%*]}
	# using the single quotes as delimiters, get the second field
	#  (this script can only deal with one adapter!)
	oldMAC=`echo "$oldMAC" | cut -f2 -d\'`
	# remove the mac= from the beginning
	oldMAC=${oldMAC#mac=*}

	if ! replace "$oldMAC" "$MAC" "$DESTCONFIG"
	then
		echo "Unable to change the MAC address in $DESTCONFIG from ($oldMAC) to ($MAC)" >&2
		exit 1
	fi
fi


#
# Create and Copy image directory
#

if [ $DOMU_IS_FILE_BASED = yes ]
then
	SOURCEXEN="$XEN_BASE$SOURCE/"
	DESTXEN="$XEN_BASE$DESTINATION/"
	echo "Creating the new image directory $DESTXEN"
	if ! mkdir -pv --mode=755 "$DESTXEN"
	then
		echo "Unable to create the directory $DESTXEN" >&2
		exit 1
	fi
	echo "Copying complete image.  (This may take a few minutes!)"

	tar -C $SOURCEXEN -cSf - --exclude=lost+found `cd $SOURCEXEN; echo *` \
	| tar -C $DESTXEN -xvBSpf -
	if [ $? -ne 0 ]
	then
		echo "Unable to copy the images from $SOURCEXEN to $DESTXEN" >&2
		exit 1
	fi
else	# Deal with block devices
	if [ $DUPLICATE -eq 0 ]
	then
		echo "Editing config file ($DESTCONFIG), correcting the new domU root device name."
		if ! replace ":$BOOTIMAGE," ":$DOMU_ROOTDEV," "$DESTCONFIG"
		then
			echo "Unable to change the domU root device name in $DESTCONFIG from $BOOTIMAGE to $DOMU_ROOTDEV" >&2
			exit 1
		fi
	fi
	echo "Copying from source block device ($BOOTIMAGE) to the new target device ($DOMU_ROOTDEV)"
	echo "(This may take a few minutes!)"
	if ! dd if=$BOOTIMAGE of=$DOMU_ROOTDEV bs=4K
	then
		echo "Failed to copy from $BOOTIMAGE to $DOMU_ROOTDEV" >&2
		exit 1
	fi
fi


#
# The rest of the script only applies if we are actually making changes within
# the image
#
if [ $DUPLICATE -eq 0 ]
then
	#
	# Create a temporary directory name
	#
	tmpdir=$(mktemp -d)
	if [ $? -ne 0 ]
	then
		echo "Unable to create temporary directory $tmpdir." >&2
		exit 1
	fi

	if [ $DOMU_IS_FILE_BASED = yes ]
	then
		set -- $(echo $DESTXEN*)
	else
		set -- $(echo $DOMU_ROOTDEV)
	fi

	for DISKIMAGE
	do
		# Silently ignore any directories (lost+found comes to mind):
		[ -d $DISKIMAGE ] && continue

		#
		# Mount the newly copied image file
		#
		loopdev=''
		for dev in /dev/loop*
		do
			if [ ! -b "$dev" ]
			then
				continue
			fi

			status=$(losetup "$dev" 2>/dev/null) || status=''

			if [ ! "$status" ]
			then
				status=$(losetup $dev "$DISKIMAGE")
				if [ ! "$status" ]
				then
					kpartx -a $dev
					loopdev=$dev
					break
				fi
			fi
		done
		if [ ! "$loopdev" ]
		then
			echo "No loopback devices available." >&2
			exit 1
		fi

		echo -n "Trying to mount partition $PART of $DISKIMAGE ... "
		mapperdev=$(echo "$loopdev" | sed -e 's/dev\//dev\/mapper\//g')p$PART
		status=$(mount -o rw $mapperdev "$tmpdir")
		if [ "$status" ]
		then
			kpartx -d $loopdev
			losetup -d $loopdev
			continue
		fi
		echo "succeeded."

		pushd "$tmpdir" > /dev/null

		#
		# Find out if we are looking at SLE10
		#
		SLE10=
		if [ -f etc/SuSE-release ]
		then
			OSVER=`cat etc/SuSE-release | sed -n 1p | awk -F'(' '{ print $1 }' | sed 's/ $//g'`
			if [ "$OSVER" == "openSUSE 10" -o \
				"$OSVER" == "SUSE Linux Enterprise Server 10" -o \
				"$OSVER" == "SUSE Linux Enterprise Desktop 10" ]
			then
				SLE10=1
			fi
		fi

		#
		# Change the Network Configuration in the mounted image file
		#
		if [ -n "$MAC" ]
		then
			if [ -d etc/sysconfig/network/ ]
			then
				echo "Changing the Network configuration in the newly copied image."
				pushd "etc/sysconfig/network/" > /dev/null
				# Find the ifcfg-ethMACADDRESS file in the
				#  newly copied image
				ETH0=`ls | grep ifcfg-eth | cut -f1`
				if [ -z "$ETH0" ]
				then
					echo "Unable to find ethernet file in image file" 2>&1
					cd /tmp; umount "$tmpdir"; rmdir "$tmpdir"
					kpartx -d $loopdev
					losetup -d $loopdev
					exit 1
				fi
				if [ "$SLE10" ]
				then
					mv -f "$ETH0" ifcfg-eth-id-$MAC
				else
					sed -i -e "s,^LLADDR=.*$,LLADDR=\'$MAC\',"   \
						ifcfg-eth0
				fi
				popd > /dev/null
			fi

			if [ -d etc/udev/rules.d/ ]
			then
				# The 30-net_persistent_names.rules or 70-persistent-net.rules
				#  file controls which interface to use.
				# By removing the SUBSYSTEM== lines, we force
				#  the system to recreate it.
				pushd "etc/udev/rules.d/" > /dev/null
				if [ "$SLE10" ]
				then
					sed -i -e "/SUBSYSTEM==/d"	\
						30-net_persistent_names.rules
				else
					sed -i -e "/SUBSYSTEM==/d"	\
						70-persistent-net.rules
				fi
				popd > /dev/null
			fi
		fi

		#
		# Change the IP Address in the mounted image file
		#
		if [ -n "$IP" ]
		then
			if [ -d etc/sysconfig/network/ ]
			then
				echo "Modify the IP Address of the new domU."

				pushd "etc/sysconfig/network/" > /dev/null
				if [ "$SLE10" ]
				then
					sed -i -e "s,^IPADDR=.*$,IPADDR=$IP,"   \
						ifcfg-eth-id-$MAC
				else
					sed -i -e "s,^IPADDR=.*$,IPADDR=$IP/$MASK,"   \
						ifcfg-eth0
				fi
				popd > /dev/null
			fi
		fi

		#
		# Change the HOSTNAME and hosts files in the mounted image file
		#
		if [ -n "$HOSTNAME" ]
		then
			if [ -d "etc/" ]
			then
				echo "Changing HOSTNAME file to $HOSTNAME."

				pushd "etc/" > /dev/null
				# using the period as a delimiter, select the
				#  first entry for the hostname
				oldHOSTNAME=`cut -f1 -d\. HOSTNAME`
				if ! replace "$oldHOSTNAME" "$HOSTNAME" "HOSTNAME"
				then
					echo "Unable to change the HOSTNAME from $oldHOSTNAME to $HOSTNAME" >&2
					cd /tmp; umount "$tmpdir"; rmdir "$tmpdir"
					kpartx -d $loopdev
					losetup -d $loopdev
					exit 1
				fi
				FQDN=`cat HOSTNAME`

				# Add entries for the new domU to /etc/hosts,
				#  if it doesn't already include them:
				if ! egrep -q "[[:space:]]$FQDN[^[:alnum:]]" \
					hosts
				then
					echo "Changing hosts file."
					echo -e "$IP\t$FQDN $HOSTNAME" >> hosts
				fi
				popd > /dev/null
			fi
		fi

		popd > /dev/null
		umount "$tmpdir"
		kpartx -d $loopdev
		losetup -d $loopdev
	done

	rmdir "$tmpdir"
fi

echo "Clone is complete. domU $DESTCONFIG is ready to start!"
exit 0