File cloneprep.sh of Package cloneprep

#!/bin/sh
#
# A simple script which cleans out unique data from a SLE system so it can
# be cloned without issues.
#
# This should be run before taking an image of a machine.
#
# It will configure the standard yast firstboot to run on the next startup.
#
# A lot of code was adaped from Whitfield School
#
# matt@skiptons.net
# don@vosburgs.org
#

# Define some script variables

BACKUPEXT=".cloneprep_saved"
ZLMUNIQUE="/etc/zmd/deviceid /etc/zmd/secret"
ZYPPCRED="/etc/zypp/credentials.d/NCCcredentials"

if [ -e /etc/YaST2/firstboot.xml ]; then
  echo ""
else
	echo "The firstboot package appears to be missing. Please make sure you have installed the yast2-firstboot package, and re-run cloneprep."
	exit
fi

# Determine whether this is SLE 10 or newer by checking
if [ -e /etc/init.d/firstboot ]; then
	echo "Detected SLES 10"
	REV="10"
else
	echo "Detected SLES 11 or newer"
	REV="11"
fi

if [ $REV = "10" ]; then
	NETNAMESFILE=/etc/udev/rules.d/30-net_persistent_names.rules
else    
	NETNAMESFILE=/etc/udev/rules.d/70-persistent-net.rules
fi

echo "= Would you like to have cloneprep remove networking configuration (yast2-firstboot can prompt you to set it up)? Y/N:"
read ANS1

if [ $ANS1 = "y" -o $ANS1 = "Y" -o $ANS1 = "Yes" -o $ANS1 = "yes" ]; then
	IFCFGDELETE="/etc/sysconfig/network/ifcfg-eth* /etc/sysconfig/network/ifcfg-wlan*"
else
	IFCFGDELETE=""
fi
  
clear

echo "============ Would you like to run ClonePrep now? Y/N:"
read ANS

if [ $ANS = "y" -o $ANS = "Y" -o $ANS = "Yes" -o $ANS = "yes" ]; then
	# place holder to continue
	echo
else
	echo "Exiting!!"
	exit
fi

# clean out any persistent naming rules
echo "============ Cleaning out persistent names in $NETNAMESFILE"
sed --in-place=$BACKUPEXT s/SUBSYSTEM.*$//g $NETNAMESFILE

# Remove the machine-specific ifcfg files
echo "============ Removing networking files (if selected) $IFCFGDELETE"
#echo "Would Delete: $IFCFGDELETE"
rm $IFCFGDELETE  > /dev/null 2>&1 


# Define functions to parse files and swap byid strings with dev paths
do_byid_dev_swap() {
	file=$1
	num=1
	cat $file |
	while read line
	do
		#echo "Line $num: $line"
		TEST=`echo $line |grep by-id`
		if [ "$TEST" ]; then
			BYID=`echo $line | grep -o by-id.* |awk '{print $1}'|awk -F/ '{print $2}'`
		#	echo $BYID
			DEV=`file /dev/disk/by-id/$BYID | awk '{print $5}' | awk -F/ '{print $3}' |awk -F\' '{print $1}'`
			FULLBYID=`echo "/dev/disk/by-id/$BYID" |tr / @`
		#	echo $FULLBYID
			LINETR=`echo $line |tr / @`
		#	echo $LINETR
		#	echo "disk by-id: $BYID = device: $DEV"
			NEWLINE=`echo $LINETR | sed s/"$FULLBYID"/"\/dev\/$DEV"/ |tr @ /`
		#	echo $NEWLINE
			sed -i "${num}c $NEWLINE" $file
		fi
		let num=num+1
	done
}
parse_byid_dev_file() {
	file=$1
	DOSWAP=1
	while [ $DOSWAP == 1 ]
	do
		FILECHECK=`grep by-id $file`
		if [ "$FILECHECK" ]; then
			#echo "by-id is in there"
			do_byid_dev_swap $file
		else
			#echo "by-id clean"
			DOSWAP=0
		fi
	done
}

# Actually swap the byid strings with dev strings in /boot/grub/menu.lst
echo "============ Setting boot menu to use disks by device name"
parse_byid_dev_file /boot/grub/menu.lst
# Actually swap the byid strings with dev strings in /etc/fstab
echo "============ Setting fstab to use disks by device name"
parse_byid_dev_file /etc/fstab

echo "======== Removing previously entered machine ID or registered credentials"

if [ $REV = "10" ]; then
	# Remove the registered ZENWorks service so that reregistration succeeds
	echo "============ Unregistering from ZLM"
	rug sd `rug sl |grep ZENworks |awk '{print $1}'`

	echo "============ Stopping ZMD"
	/etc/init.d/novell-zmd stop

	echo "============ Removing ZLM device information $ZLMUNIQUE"
#	echo "Would Delete: $ZLMUNIQUE"
	rm $ZLMUNIQUE > /dev/null 2>&1
else
	echo "================= Removing zypper credentials"
	rm $ZYPPCRED > /dev/null 2>&1
fi

# Create the flag file that tells the system to run firstboot
echo "============ Setting system to run first boot "
if [ $REV = 10 ]; then
        chkconfig firstboot on
	touch /etc/reconfig_system
else
	touch /var/lib/YaST2/reconfig_system
fi

echo "============  System has now been prepared for imaging! - Shutting down"
sleep 3

shutdown -h 0

# Changelog:
#
# 01-20-2011  Don Vosburg <don@vosburgs.org>
# - Modified heavily to allow it to run on either SLE10 or SLE11 
# - Removed ZLM checks
#
# 07-12-2008 Don Vosburg <don@vosburgs.org>
# - Corrected shutdown time - set to immediate
# 03-20-2008  Don Vosburg <don@vosburgs.org>
#	- Initial packaged version, for SLE only
openSUSE Build Service is sponsored by