#!/bin/bash
dir=$(pwd)
for i in $(seq 1 5)
do
     vmname="www${i}"
     mntdir="/mnt/${vmname}"

     # is already mounted?
     df $mntdir 2> /dev/null | grep -q $mntdir 2> /dev/null
     rc=$?
     if [ $rc -eq 0 ]; then
          umount $mntdir
          rc=$?
          if [ $rc -ne 0 ]; then
               echo Target is busy. $mntdir
               exit 1
          fi
     fi

     # loop mount
     [ ! -d $mntdir ] && mkdir $mntdir
     mount -o loop ${dir}/${vmname}/sda1.img $mntdir
     rc=$?
     if [ $rc -ne 0 ]; then
          echo Can not mount. $mntdir
          exit 1
     fi

     # setting hostname
cat > ${mntdir}/etc/sysconfig/network << EOF
HOSTNAME=${vmname}.example.xen
NOZEROCONF=yes
EOF

# generate /etc/hosts
cat > ${mntdir}/etc/hosts << EOF
127.0.0.1 localhost.localdomain localhost
192.168.1.1 www1.example.xen www1
192.168.1.2 www2.example.xen www2
192.168.1.3 www3.example.xen www3
192.168.1.4 www4.example.xen www4
192.168.1.5 www5.example.xen www5
192.168.1.100 www.example.xen www
EOF

# disable IPv6
cat > ${mntdir}/etc/modprobe.conf << EOF
alias net-pf-10 off
EOF

     # setting interface
cat > ${mntdir}/etc/sysconfig/network-scripts/ifcfg-eth0 << EOF
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.1.${i}
NETMASK=255.255.255.0
EOF

     # create contents file
cat > ${mntdir}/var/www/html/who_are_you.html << EOF
<html><body>
<h1>hostname: ${vmname}.example.xen (192.168.1.${i})</h1>
<p>Status is healthy.</p>
</body></html>
EOF
chmod 644 ${mntdir}/var/www/html/who_are_you.html

     umount ${mntdir}
done