LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Blogs > zhjim
User Name
Password

Notices

Rate this Entry

Script to create a modified xen domU

Posted 02-04-2010 at 03:06 AM by zhjim

Just for me and all that can use it. Heres a script I wrote to create a domU filesystem from a directory. All values are taken from the xen config script for the domU and are later sed'ed.

Code:
#!/bin/bash
if [ $# -ne 1 ]; then
        echo "Usage: $0 /path/to/domU_config " #/dev/name | /path/to/dir"
        exit -1
fi

CONF=$1
TARGET=$2
IMG=./new_strap

#HOSTNAME=$(grep "name" $CONF)
#DEVICE=$(grep "disk" $CONF)
#IP=$(grep "vif" $CONF)

for i in name disk vif netmask gateway; do
        DUMMY=$(grep "^[::space::]*$i" $CONF)
        DUMMY=$(echo $DUMMY | cut -d = -f 2-)


        # Get middle of "
        DUMMY=$(echo $DUMMY | sed 's/^.*"\(.*\)".*$/\1/g')      #DUMMY=${DUMMY#*\"}; #DUMMY=${DUMMY%*\"}
        # Get middle of '
        DUMMY=$(echo $DUMMY | sed "s/^.*'\(.*\)'.*$/\1/g")      #DUMMY=${DUMMY#*\'}; #DUMMY=${DUMMY%*\'}


        #echo $DUMMY
        #echo "$i: $DUMMY"
        eval $i=$DUMMY
        #${!i}=\${$DUMMY}
done

# nicen disk
disk=$(echo $disk | cut -d ':' -f 2)
disk=$(echo $disk | cut -d ',' -f 1)

# nicen ip
#vif=$(echo $vif | cut -d '=' -f 2)
#vif=$(echo $vif | cut -d ',' -f 1)
vif=$(echo $vif | sed 's/.*ip=\(.*\)/\1/g')

DEST=$disk
#echo $DEST
if [ ! -e $DEST ]; then
        echo 'Destination does not exists!'
        exit -1
fi
# See if there already is a file system and if its inuse
tune2fs -l $DEST > /tmp/tune2fs-disk
START=$(grep 'First inode:' /tmp/tune2fs-disk | awk '{ print $3}')
FREE=$(grep 'Free inodes:' /tmp/tune2fs-disk | awk '{print $3}')
COUNT=$(grep 'Inode count:' /tmp/tune2fs-disk | awk '{print $3}')
if [ $COUNT -ne $((START + FREE)) ]; then
        echo 'There allready is a filesystem on partition ' $DEST
        echo 'and there are inodes in use.'
        echo 'Not doing it!'
        exit -1
fi
# got all data
# Do it
START=$(date +%s)
echo "Creating image with following options"
echo "---------------------------"
echo "Name: $name"
echo "partition: $disk"
echo "IP: $vif"
echo "Mask: $netmask"
echo "Gateway: $gateway"
echo "---------------------------"
echo "Are these correct (y/n)"
read further
if [ $further != "y" ]; then
        echo "Bailing out"
        exit 0
fi
echo "About to create filesystem."
echo "ALL DATA WILL BE LOST"
echo "Continue (y/n)"
read further
if [ $further != "y" ]; then
        echo "Bailing out"
        exit 0
fi

#exit 0

# Mount and copy
echo "Creating filesystem"
umount $disk 2> /dev/null
mke2fs -jq $disk
mkdir /media/$DEST -p
mount $disk /media$DEST
echo "Copying files"
cp -a ./$IMG/* /media/$DEST

# Sed em all
sed -ibak -e "s/%hostname%/$name/" /media/$DEST/etc/hostname
sed -ibak -e "s/%hostname%/$name/" -e "s/%ip%/$vif/" /media/$DEST/etc/hosts
sed -ibak -e "s/%ip%/$vif/" /media/$DEST/etc/network/interfaces
sed -ibak -e "s/%mask%/$netmask/" /media/$DEST/etc/network/interfaces
sed -ibak -e "s/%gw%/$gateway/" /media/$DEST/etc/network/interfaces
sed -ibak -e "s/%hostname%/$name/" /media/$DEST/etc/exim4/update-exim4.conf.conf

umount $disk
rm /media/$(dirname $DEST) -r
END=$(date +%s)
TAKEN=$((END - START))
echo "Took $TAKEN seconds to generate image"
Posted in Uncategorized
Views 779 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 06:47 AM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration