Skip to content

Instantly share code, notes, and snippets.

@solarce
Created February 12, 2009 17:41
Show Gist options
  • Save solarce/62762 to your computer and use it in GitHub Desktop.
Save solarce/62762 to your computer and use it in GitHub Desktop.
#!/Bin/bash
###### VARIABLES FOR CONFIG ############
STORSERVER=karate-kid
SERVER=ris-xen-prod04
BASE=/home/xen
TEMPLATE=nrs-prod-webtemplate
VM=$1
VMIP=$2
TEMPLATEDISKNAME=sdb
LUNNUM=`cat /root/scripts/lunnum.txt`
######### FUNCTIONS ###########
function get_scsi_devices {
dmesg | grep SCSI.*device.*sectors | grep sd | uniq | awk '{print $3}' | cut -f 1 -d: | sort | uniq
}
#create SCSI device list mapping before adding new iSCSI LUN
get_scsi_devices > /root/scripts/scsidevmap.txt
echo "mkdir -p $BASE/$VM"
mkdir -p $BASE/$VM
echo "cp $BASE/$TEMPLATE/$TEMPLATE.cfg $BASE/$VM/$VM.cfg"
cp $BASE/$TEMPLATE/$TEMPLATE.cfg $BASE/$VM/$VM.cfg
#this is a one time operation and will normally be commented out, unless we update the snapshot
#echo "karate-kid zfs snapshot datapool/$SERVER/lun-$TEMPLATE@template"
#ssh karate-kid zfs snapshot datapool/$SERVER/lun-$TEMPLATE@template
#this is the normal cloning operation that needs to happen every time.
echo "ssh $STORSERVER zfs clone datapool/$SERVER/lun-$TEMPLATE@template datapool/$SERVER/lun-$VM"
ssh $STORSERVER zfs clone datapool/$SERVER/lun-$TEMPLATE@template datapool/$SERVER/lun-$VM
echo "ssh $STORSERVER iscsitadm create target -u $LUNNUM -b /dev/zvol/rdsk/datapool/$SERVER/lun-$VM $VM"
ssh $STORSERVER iscsitadm create target -u $LUNNUM -b /dev/zvol/rdsk/datapool/$SERVER/lun-$VM $SERVER
echo "ssh $STORSERVER svcadm restart system/iscsitgt:default"
ssh $STORSERVER svcadm restart system/iscsitgt:default
#discover new LUN(s)
echo "discovering new LUN(s)..."
iscsiadm -m session -R
#discover new disk device ID
get_scsi_devices > /tmp/scsidevicemap.tmp.txt
IFS=$'\n' OLDDEVIDS=(`cat /root/scripts/scsidevmap.txt`)
IFS=$'\n' NEWDEVIDS=(`cat /tmp/scsidevicemap.tmp.txt`)
VMDEVID='sdb'
for x in `seq 0 ${#NEWDEVIDS[@]}`; do
if [ "${NEWDEVIDS[x]}" = "${OLDDEVIDS[x]}" ]; then
echo "."
else
echo "${NEWDEVIDS[x]}"
VMDEVID=${NEWDEVIDS[x]}
fi
done;
#updates config file with raw disk mapped to iSCSI LUN
echo "sed -i 's/'$TEMPLATEDISKNAME'/'$VMDEVID'/g' $BASE/$VM/$VM.cfg"
sed -i 's/'$TEMPLATEDISKNAME'/'$VMDEVID'/g' $BASE/$VM/$VM.cfg
#updates config file with new VM name
echo "sed -i 's/'$TEMPLATE'/'$VM'/g' $BASE/$VM/$VM.cfg"
sed -i 's/'$TEMPLATE'/'$VM'/g' $BASE/$VM/$VM.cfg
sleep 10
#update the sysprep file
echo "making mount point..."
mkdir -p /mnt/xen/${VMDEVID}1
sleep 5
echo "mounting filesystem..."
mount.ntfs-3g /dev/${VMDEVID}1 /mnt/xen/${VMDEVID}1 -o force
echo "updating sysprep.inf..."
echo "sed -i 's/NRS-PROD-WEBTMP/'"$VM"'/g' /mnt/xen/${VMDEVID}1/sysprep/sysprep.inf"
sed -i 's/NRS-PROD-WEBTMP/'"$VM"'/g' /mnt/xen/${VMDEVID}1/sysprep/sysprep.inf
echo "sed -i 's/192.168.2.187/'"$VMIP"'/g' /mnt/xen/${VMDEVID}1/sysprep/sysprep.inf"
sed -i 's/192.168.2.187/'"$VMIP"'/g' /mnt/xen/${VMDEVID}1/sysprep/sysprep.inf
echo "cp /mnt/xen/${VMDEVID}1/sysprep/sysprep.inf /mnt/xen/${VMDEVID}1/sysprep.inf"
cp /mnt/xen/${VMDEVID}1/sysprep/sysprep.inf /mnt/xen/${VMDEVID}1/sysprep.inf
echo "unmounting..."
umount /mnt/xen/${VMDEVID}1
rm -rf /mnt/xen/${VMDEVID}1
#cleanup
echo $(($LUNNUM+1)) > /root/scripts/lunnum.txt
rm -rf /root/scripts/scsidevmap.txt
rm -rf /tmp/scsidevmap.tmp.txt
sleep 20
#add finally...
xm create $BASE/$VM/$VM.cfg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment