#!/bin/sh
#
# @package      hubzero-mw-service
# @file         mkvztemplate
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @copyright    Copyright (c) 2008-2015 HUBzero Foundation, LLC.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2008-2015 HUBzero Foundation, LLC.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.
#

ARCH=$1
DEBRELEASE=$2
HUBRELEASE=$3

if [ `/usr/bin/id -u` != 0 ]; then echo "Must be root to make and install the maxwell openvz templates"; exit 100; fi

if [ "${DEBRELEASE}" = "etch" ]; then
    DEBMAJOR=4
elif [ "${DEBRELEASE}" = "lenny" ]; then
    DEBMAJOR=5
elif [ "${DEBRELEASE}" = "squeeze" ]; then
    DEBMAJOR=6
elif [ "${DEBRELEASE}" = "wheezy" ]; then
    DEBMAJOR=7
elif [ "${DEBRELEASE}" = "jessie" ]; then
    DEBMAJOR=8
else
	echo "Unrecognized or no debian release [${DEBRELEASE}] requested."
	echo "Usage: mkvztemplate [i386|amd64] [etch|lenny|squeeze|wheezy|jessie] [lenny|buck|manny|shira|diego|ellie|sid|...]"
	exit 1
fi

if [ "${ARCH}" != "i386" -a "${ARCH}" != "amd64" ]; then
	echo "Unrecognized or no architecture [${ARCH}] requested."
	echo "Usage: mkvztemplate [i386|amd64] [etch|lenny|squeeze|wheezy|jessie] [lenny|buck|manny|shira|diego|ellie|sid|...]"
	exit 1
fi

if [ "${HUBRELEASE}" = "" ]; then
	echo "No HUBzero release requested."
	echo "Usage: mkvztemplate [i386|amd64] [etch|lenny|squeeze|wheezy|jessie] [lenny|buck|manny|shira|diego|ellie|sid|...]"
	exit 1
fi

if [ "${HUBRELEASE}" != "lenny" -a "${HUBRELEASE}" != "buck" -a "${HUBRELEASE}" != "manny" -a "${HUBRELEASE}" != "sid" -a "${HUBRELEASE}" != "shira" -a "${HUBRELEASE}" != "diego" -a "${HUBRELEASE}" != "ellie" ]; then
	echo "WARNING: Unrecognized [${HUBRELEASE}] requested, continuing anyway."
fi

# Currently HUBRELEASE is only used when writing out the 
# apt sources.list for hubzero packages. If we use it for any
# other logic in the future the following may
# need to be changed to set something like HUBDIST

if [ "${HUBRELEASE}" = "shira" ]; then

	if [ ${DEBMAJOR} -lt 6 ]; then
		echo "HUBzero release '${HUBRELEASE}' is not compatible with Debian release '${DEBRELEASE}'"
		exit 1
	fi

	HUBRELEASE="shira-deb${DEBMAJOR}"

elif [ "${HUBRELEASE}" = "diego" ]; then

	EXTRA_PACKAGES=hubzero-tigervnc-server

	if [ ${DEBMAJOR} -lt 6 ]; then
		echo "HUBzero release '${HUBRELEASE}' is not compatible with Debian release '${DEBRELEASE}'"
		exit 1
	fi

	HUBRELEASE="diego-deb${DEBMAJOR}"

elif [ "${HUBRELEASE}" = "ellie" ]; then

	EXTRA_PACKAGES=hubzero-tigervnc-server

	if [ ${DEBMAJOR} -lt 6 ]; then
		echo "HUBzero release '${HUBRELEASE}' is not compatible with Debian release '${DEBRELEASE}'"
		exit 1
	fi

	HUBRELEASE="ellie-deb${DEBMAJOR}"
fi

MIRROR="http://http.us.debian.org/debian/"
SECMIRROR="http://security.debian.org/"

if [ "${DEBRELEASE}" = "etch" ]; then
	DEBVERSION=4.0
	MIRROR="http://archive.debian.org/debian/"
	SECMIRROR="http://archive.debian.org/debian-security/"
fi

if [ "${DEBRELEASE}" = "lenny" ]; then
	DEBVERSION=5.0
	MIRROR="http://archive.debian.org/debian/"
	SECMIRROR="http://archive.debian.org/debian-security/"
fi

if [ "${DEBRELEASE}" = "squeeze" ]; then
	DEBVERSION=6.0
	EXTRA_APT="deb ${MIRROR} squeeze-lts main contrib non-free"
fi

if [ "${DEBRELEASE}" = "wheezy" ]; then
	DEBVERSION=7.0
	EXTRA_APT="deb ${MIRROR} wheezy-updates main contrib non-free"
fi

if [ "${DEBRELEASE}" = "jessie" ]; then
	DEBVERSION=8.0
	EXTRA_APT="deb ${MIRROR} jessie-updates main contrib non-free"
fi

VZROOT=/var/lib/vz
VZTEMPLATEDIR=${VZROOT}/template
MYTEMPLATE=debian-${DEBVERSION}-${ARCH}-maxwell
MYTEMPLATEDIR=${VZTEMPLATEDIR}/${MYTEMPLATE}

if [ ! -d ${VZTEMPLATEDIR} ]; then
	echo "Template directory does not exist: ${VZTEMPLATEDIR}"
	exit 2;
fi

if [ ! -d ${MYTEMPLATEDIR} ]; then

	mkdir -p ${MYTEMPLATEDIR}

	if [ ! -d ${MYTEMPLATEDIR} ]; then
        	echo "Unable to create template root directory: ${MYTEMPLATEDIR}"
        	exit 4;
	fi
fi

if [ -f ${VZROOT}/template/cache/${MYTEMPLATE}.tar.gz ]; then
	echo "Template ${VZROOT}/template/cache/${MYTEMPLATE}.tar.gz already exists."
	echo ""
	echo "Extracting files."
	echo ""
	/bin/tar --checkpoint=5000 -xzpf ${VZROOT}/template/cache/${MYTEMPLATE}.tar.gz -C ${MYTEMPLATEDIR}
else

echo "Building new template ${VZROOT}/template/cache/${MYTEMPLATE}.tar.gz"

# etch's debootstrap doesn't know about squeeze. etch/lenny/squeeze/wheezy/jessie seem
# to share the same support script in newer versions so we just make a link here
if [ "${DEBRELEASE}" = "squeeze" ]
then
    if [ -d /usr/lib/debootstrap -a ! -f /usr/lib/debootstrap/scripts/squeeze ]
    then
	    SCRIPT="/usr/lib/debootstrap/scripts/lenny"
    fi
fi

# etch's debootstrap doesn't know about wheezy. etch/lenny/squeeze/wheezy/jessie seem
# to share the same support script in newer versions so we just make a link here
if [ "${DEBRELEASE}" = "wheezy" ]
then
    if [ -d /usr/lib/debootstrap -a ! -f /usr/lib/debootstrap/scripts/wheezy ]
    then
	    SCRIPT="/usr/lib/debootstrap/scripts/lenny"
    fi
fi

# etch's debootstrap doesn't know about jessie. etch/lenny/squeeze/wheezy/jessie seem
# to share the same support script in newer versions so we just make a link here
if [ "${DEBRELEASE}" = "jessie" ]
then
    if [ -d /usr/lib/debootstrap -a ! -f /usr/lib/debootstrap/scripts/jessie ]
    then
	    SCRIPT="/usr/lib/debootstrap/scripts/lenny"
    elif [ -d /usr/share/debootstrap -a ! -f /usr/share/debootstrap/scripts/jessie ]
    then
	    SCRIPT="/usr/share/debootstrap/scripts/wheezy"
    fi
fi

# older version of dpkg-deb (RHEL6) don't support xz compressed binary packages
# we can tell debootstrap to use alternative extractor (ar) support in this case
XZSUPPORT=`dpkg-deb --help | sed -n 's/.* extreme (\(xz\)).*/\1/p'`

if [ "${XZSUPPORT}" != "xz" ]
then
EXTRACTOR_OVERRIDE=--extractor=ar
fi

# Make iniital debian bootstrap image
/usr/sbin/debootstrap ${EXTRACTOR_OVERRIDE} --arch ${ARCH} --include=locales ${DEBRELEASE} ${MYTEMPLATEDIR} ${MIRROR} ${SCRIPT}

if [ $? != 0 ]
then
    echo "debootstrap failed to run successfully"
    exit 3
fi

# Configure apt
/bin/cat <<EOF > ${MYTEMPLATEDIR}/etc/apt/sources.list
deb ${MIRROR} ${DEBRELEASE} main contrib non-free
deb-src ${MIRROR} ${DEBRELEASE} main contrib non-free
deb ${SECMIRROR} ${DEBRELEASE}/updates main contrib non-free
deb-src ${SECMIRROR} ${DEBRELEASE}/updates main contrib non-free
${EXTRA_APT}
EOF

# Get signing key for HUBzero repositories
wget http://packages.hubzero.org/deb/hubzero-signing-key.asc -O ${MYTEMPLATEDIR}/root/hubzero-signing-key.asc
echo "/usr/bin/apt-key add /root/hubzero-signing-key.asc" | chroot ${MYTEMPLATEDIR}
/bin/rm ${MYTEMPLATEDIR}/root/hubzero-signing-key.asc

# Remove unneeded gettys
# Debian jessie doesn't use inittab and starts getttys on demand
if [ -f ${MYTEMPLATEDIR}/etc/inittab ]
then
/bin/sed -i -e '/getty/d' ${MYTEMPLATEDIR}/etc/inittab
fi

# Disable root account
/bin/sed -i -e 's/^root::/root:!:'/g ${MYTEMPLATEDIR}/etc/shadow

# Link mtab to /proc/mounts
/bin/rm -f ${MYTEMPLATEDIR}/etc/mtab
/bin/ln -s /proc/mounts ${MYTEMPLATEDIR}/etc/mtab

# In Lenny, /dev/ lack the pty devices needed during install, so the kernel fs
# devpts needs to be mounted
if [ "${DEBRELEASE}" = "lenny" ]
then
	echo "mount -t devpts devpts -o noexec,nosuid,gid=5,mode=620 /dev/pts" | chroot ${MYTEMPLATEDIR}
fi

# In Squeeze, /proc needs to be mounted for openjdk installation (!)
# Not sure about why the jdk is required now...
if [ "${DEBRELEASE}" = "lenny" -o "${DEBRELEASE}" = "squeeze" -o "${DEBRELEASE}" = "wheezy" -o "${DEBRELEASE}" = "jessie" ]
then
	echo "mount -t proc none /proc" | chroot ${MYTEMPLATEDIR}
fi

# Configure locale information
/bin/cat << EOF > ${MYTEMPLATEDIR}/etc/locale.gen
en_US.UTF-8 UTF-8
en_US ISO-8859-1
EOF
echo "/usr/sbin/locale-gen" | chroot ${MYTEMPLATEDIR}

# Update package list
echo "apt-get update -y" | chroot ${MYTEMPLATEDIR}

# Temporarily install a policy-rc.d script to prevent package installation
# from running init.d scripts
/bin/cat << EOF > ${MYTEMPLATEDIR}/usr/sbin/policy-rc.d
#!/bin/sh
exit 101
EOF
/bin/chmod +x ${MYTEMPLATEDIR}/usr/sbin/policy-rc.d

# Bug in lenny packages give (probably benign) errors about missing font 
#     directory, we make link here to silence the error. Proper fix 
#     is probably a change in a configuration file somewhere to not reference 
#     those directories.
if [ "${DEBRELEASE}" = "lenny" ]
then
echo "mkdir -p /etc/X11/fonts/X11R7" | chroot ${MYTEMPLATEDIR}
echo "ln -s /usr/share/fonts/X11/Type1/ /etc/X11/fonts/X11R7/Type1" | chroot ${MYTEMPLATEDIR}
fi

# Bug in etch installation has a dependency with fontconfig that gets 
#     resolved if it is installed seperately and first
# Bug in etch packages give (probably benign) errors about missing font 
#     directories, we make links here to silence the error. Proper fix 
#     is probably a change in a configuration file somewhere to not reference 
#     those directories.
if [ "${DEBRELEASE}" = "etch" ]
then
echo "mkdir -p /usr/lib/X11/fonts" | chroot ${MYTEMPLATEDIR}
echo "mkdir -p /etc/X11/fonts/X11R7" | chroot ${MYTEMPLATEDIR}
echo "ln -s /usr/share/fonts/X11/100dpi/ /usr/lib/X11/fonts/100dpi" | chroot ${MYTEMPLATEDIR}
echo "ln -s /usr/share/fonts/X11/75dpi/ /usr/lib/X11/fonts/75dpi" | chroot ${MYTEMPLATEDIR}
echo "ln -s /usr/share/fonts/X11/cyrillic/ /usr/lib/X11/fonts/cyrillic" | chroot ${MYTEMPLATEDIR}
echo "ln -s /usr/share/fonts/X11/misc/ /usr/lib/X11/fonts/misc" | chroot ${MYTEMPLATEDIR}
echo "ln -s /usr/share/fonts/X11/Type1/ /usr/lib/X11/fonts/Type1" | chroot ${MYTEMPLATEDIR}
echo "ln -s /usr/share/fonts/X11/Type1/ /etc/X11/fonts/X11R7/Type1" | chroot ${MYTEMPLATEDIR}
echo "ln -s /usr/share/fonts/truetype /usr/lib/X11/fonts/truetype" | chroot ${MYTEMPLATEDIR}
echo "apt-get install -y fontconfig" | chroot ${MYTEMPLATEDIR}
fi

# Install openssh and basic vnc server support packages
echo "apt-get install -y vnc4server fontconfig xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-scalable ttf-bitstream-vera ssh openssh-server" | chroot ${MYTEMPLATEDIR}

# Bug in etch packages give (probably benign) errors about missing font 
#     directories, we make links here to silence the error. Proper fix 
#     is probably a change in a configuration file somewhere to not reference 
#     those directories.
if [ "${DEBRELEASE}" = "etch" ]; then
echo "ln -s /usr/share/fonts/truetype /usr/share/fonts/X11/truetype" | chroot ${MYTEMPLATEDIR}
echo "apt-get install -y x-ttcidfont-conf ttf-freefont unifont msttcorefonts" | chroot ${MYTEMPLATEDIR}
fi

if [ "${DEBRELEASE}" = "lenny" -o "${DEBRELEASE}" = "squeeze" ]; then
echo "apt-get install -y x-ttcidfont-conf ttf-freefont ttf-unifont ttf-mscorefonts-installer" | chroot ${MYTEMPLATEDIR}
fi

if [ "${DEBRELEASE}" = "wheezy" -o "${DEBRELEASE}" = "jessie" ]; then
echo "apt-get install -y fonts-freefont-ttf ttf-unifont ttf-mscorefonts-installer" | chroot ${MYTEMPLATEDIR}
fi

echo "if [ -d /usr/share/fonts/truetype/msttcorefonts ]; then mkfontdir /usr/share/fonts/truetype/msttcorefonts; mkfontscale /usr/share/fonts/truetype/msttcorefonts; fi" | chroot ${MYTEMPLATEDIR}
echo "if [ -d /usr/share/fonts/truetype/ttf-bitstream-vera ]; then mkfontdir /usr/share/fonts/truetype/ttf-bitstream-vera; mkfontscale /usr/share/fonts/truetype/ttf-bitstream-vera; fi" | chroot ${MYTEMPLATEDIR}
echo "if [ -d /usr/share/fonts/truetype/unifont ]; then mkfontdir /usr/share/fonts/truetype/unifont; mkfontscale /usr/share/fonts/truetype/unifont; fi" | chroot ${MYTEMPLATEDIR}
echo "if [ -d /usr/share/fonts/truetype/dejavu ]; then mkfontdir /usr/share/fonts/truetype/dejavu; mkfontscale /usr/share/fonts/truetype/dejavu; fi" | chroot ${MYTEMPLATEDIR}
echo "if [ -d /usr/share/fonts/truetype/liberation ]; then mkfontdir /usr/share/fonts/truetype/liberation; mkfontscale /usr/share/fonts/truetype/liberation; fi" | chroot ${MYTEMPLATEDIR}
echo "if [ -d /usr/share/fonts/truetype/freefont ]; then mkfontdir /usr/share/fonts/truetype/freefont; mkfontscale /usr/share/fonts/truetype/freefont; fi" | chroot ${MYTEMPLATEDIR}

# Configure default network for container
echo "localhost" > ${MYTEMPLATEDIR}/etc/hostname
echo "127.0.0.1 localhost.localdomain localhost" > ${MYTEMPLATEDIR}/etc/hosts
echo "session optional pam_umask.so umask=0027" >> ${MYTEMPLATEDIR}/etc/pam.d/common-account

# add lo interface, needed by vzctl > 3.0.24 because /etc/init.d/network restart doesn't bring up lo
if ! grep -qw lo ${MYTEMPLATEDIR}/etc/network/interfaces; then
    echo "auto lo" >> ${MYTEMPLATEDIR}/etc/network/interfaces
    echo "iface lo inet loopback" >> ${MYTEMPLATEDIR}/etc/network/interfaces
fi

# Install hubzero packages needed in session containers
#     The are no middleware packages for etch, used to be installed manually
if [ "${DEBRELEASE}" != "etch" ]; then
  /bin/cat <<EOF >> ${MYTEMPLATEDIR}/etc/apt/sources.list
deb http://packages.hubzero.org/deb ${HUBRELEASE} main contrib non-free
EOF
  echo "apt-get update -y" | chroot ${MYTEMPLATEDIR}
  echo "apt-get install -y ${EXTRA_PACKAGES} vnc4server hubzero-mw-session hubzero-use" | chroot ${MYTEMPLATEDIR}
fi

# remove manual policy-rc.d, a hubzero-policy-rc.d package should get installed for future updates
rm -f ${MYTEMPLATEDIR}/usr/sbin/policy-rc.d

# Clean out logs, apt cache, etc...
echo "apt-get clean" | chroot ${MYTEMPLATEDIR}
rm -f ${MYTEMPLATEDIR}/root/.bash_history
rm -f ${MYTEMPLATEDIR}/var/log/bootstrap.log
rm -f ${MYTEMPLATEDIR}/var/log/dpkg.log
rm -f ${MYTEMPLATEDIR}/var/log/fontconfig.log
rm -f ${MYTEMPLATEDIR}/var/log/apt/term.log
rm -f ${MYTEMPLATEDIR}/var/log/apt/history.log
rm -f ${MYTEMPLATEDIR}/var/log/alternatives.log

# Setup ssh environments
# I don't think this is needed anymore (if ever)
#/bin/mkdir -p ${MYTEMPLATEDIR}/root/.ssh
#/bin/chmod 700 ${MYTEMPLATEDIR}/root/.ssh
#/bin/cat << EOF > ${MYTEMPLATEDIR}/etc/rc2.d/S15ssh_gen_host_keys
##!/bin/sh
#/usr/bin/ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''
#/usr/bin/ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''
#rm -f \$0
#EOF
#/bin/chmod a+x ${MYTEMPLATEDIR}/etc/rc2.d/S15ssh_gen_host_keys

# Don't print message of the day inside containers
if [ -f ${MYTEMPLATEDIR}/etc/pam.d/sshd ]; then
  sed -i '/pam_motd.so/d' ${MYTEMPLATEDIR}/etc/pam.d/sshd
fi

# In Lenny, /dev/ lacks the pty devices needed during install, so the kernel fs
# devpts needs to be mounted. Unmount when we are done with it.
if [ "${DEBRELEASE}" = "lenny" ]; then
  echo "/bin/umount /dev/pts" | chroot ${MYTEMPLATEDIR}
  echo "/bin/umount /proc" | chroot ${MYTEMPLATEDIR}
fi

# /proc was mounted for openjdk installation
if [ "${DEBRELEASE}" = "lenny" -o "${DEBRELEASE}" = "squeeze" -o "${DEBRELEASE}" = "wheezy" -o "${DEBRELEASE}" = "jessie" ]
then
echo "/bin/umount /proc" | chroot ${MYTEMPLATEDIR}
fi

# Automatic builds on RHEL6 systems via AWS CloudFormation
# failed because sysfs was mounted in the template.
# It is unclear how this happens, but we test for
# it here and unmount it if it is there.

if  mountpoint ${MYTEMPLATEDIR}/sys > /dev/null
then
    echo "/bin/umount /sys" | chroot ${MYTEMPLATEDIR}
fi

# debootstrap on RHEL6 creates a really outdated /dev population
# let's manually populate a basic debian dev tree here

if [ "${DEBRELEASE}" = "squeeze" -o "${DEBRELEASE}" = "wheezy" -o "${DEBRELEASE}" = "jessie"  ]
then
rm -f ${MYTEMPLATEDIR}/dev/ram0
rm -f ${MYTEMPLATEDIR}/dev/ram1
rm -f ${MYTEMPLATEDIR}/dev/ram2
rm -f ${MYTEMPLATEDIR}/dev/ram3
rm -f ${MYTEMPLATEDIR}/dev/ram4
rm -f ${MYTEMPLATEDIR}/dev/ram5
rm -f ${MYTEMPLATEDIR}/dev/ram6
rm -f ${MYTEMPLATEDIR}/dev/ram7
rm -f ${MYTEMPLATEDIR}/dev/ram8
rm -f ${MYTEMPLATEDIR}/dev/ram9
rm -f ${MYTEMPLATEDIR}/dev/ram10
rm -f ${MYTEMPLATEDIR}/dev/ram11
rm -f ${MYTEMPLATEDIR}/dev/ram12
rm -f ${MYTEMPLATEDIR}/dev/ram13
rm -f ${MYTEMPLATEDIR}/dev/ram14
rm -f ${MYTEMPLATEDIR}/dev/ram15
rm -f ${MYTEMPLATEDIR}/dev/ram16
rm -f ${MYTEMPLATEDIR}/dev/loop0
rm -f ${MYTEMPLATEDIR}/dev/loop1
rm -f ${MYTEMPLATEDIR}/dev/loop2
rm -f ${MYTEMPLATEDIR}/dev/loop3
rm -f ${MYTEMPLATEDIR}/dev/loop4
rm -f ${MYTEMPLATEDIR}/dev/loop5
rm -f ${MYTEMPLATEDIR}/dev/loop6
rm -f ${MYTEMPLATEDIR}/dev/loop7
rm -f ${MYTEMPLATEDIR}/dev/mem
rm -f ${MYTEMPLATEDIR}/dev/kmem
rm -f ${MYTEMPLATEDIR}/dev/port
rm -f ${MYTEMPLATEDIR}/dev/null
rm -f ${MYTEMPLATEDIR}/dev/zero
rm -f ${MYTEMPLATEDIR}/dev/full
rm -f ${MYTEMPLATEDIR}/dev/random
rm -f ${MYTEMPLATEDIR}/dev/urandom
rm -f ${MYTEMPLATEDIR}/dev/tty0
rm -f ${MYTEMPLATEDIR}/dev/tty
rm -f ${MYTEMPLATEDIR}/dev/console
rm -f ${MYTEMPLATEDIR}/dev/ptmx
rm -f ${MYTEMPLATEDIR}/dev/ram
rm -f ${MYTEMPLATEDIR}/dev/stdin
rm -f ${MYTEMPLATEDIR}/dev/stdout
rm -f ${MYTEMPLATEDIR}/dev/stderr
rm -fr ${MYTEMPLATEDIR}/dev/fd
rm -f ${MYTEMPLATEDIR}/dev/core
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram0 b 1 0
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram1 b 1 1
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram2 b 1 2
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram3 b 1 3
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram4 b 1 4
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram5 b 1 5
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram6 b 1 6
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram7 b 1 7
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram8 b 1 8
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram9 b 1 9
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram10 b 1 10
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram11 b 1 11
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram12 b 1 12
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram13 b 1 13
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram14 b 1 14
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram15 b 1 15
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/ram16 b 1 16
chown 0:6 ${MYTEMPLATEDIR}/dev/ram0
chown 0:6 ${MYTEMPLATEDIR}/dev/ram1
chown 0:6 ${MYTEMPLATEDIR}/dev/ram2
chown 0:6 ${MYTEMPLATEDIR}/dev/ram3
chown 0:6 ${MYTEMPLATEDIR}/dev/ram4
chown 0:6 ${MYTEMPLATEDIR}/dev/ram5
chown 0:6 ${MYTEMPLATEDIR}/dev/ram6
chown 0:6 ${MYTEMPLATEDIR}/dev/ram7
chown 0:6 ${MYTEMPLATEDIR}/dev/ram8
chown 0:6 ${MYTEMPLATEDIR}/dev/ram9
chown 0:6 ${MYTEMPLATEDIR}/dev/ram10
chown 0:6 ${MYTEMPLATEDIR}/dev/ram11
chown 0:6 ${MYTEMPLATEDIR}/dev/ram12
chown 0:6 ${MYTEMPLATEDIR}/dev/ram13
chown 0:6 ${MYTEMPLATEDIR}/dev/ram14
chown 0:6 ${MYTEMPLATEDIR}/dev/ram15
chown 0:6 ${MYTEMPLATEDIR}/dev/ram16
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/loop0 b 7 0
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/loop1 b 7 1
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/loop2 b 7 2
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/loop3 b 7 3
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/loop4 b 7 4
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/loop5 b 7 5
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/loop6 b 7 6
mknod --mode=0660 ${MYTEMPLATEDIR}/dev/loop7 b 7 7
chown 0:6 ${MYTEMPLATEDIR}/dev/loop0
chown 0:6 ${MYTEMPLATEDIR}/dev/loop1
chown 0:6 ${MYTEMPLATEDIR}/dev/loop2
chown 0:6 ${MYTEMPLATEDIR}/dev/loop3
chown 0:6 ${MYTEMPLATEDIR}/dev/loop4
chown 0:6 ${MYTEMPLATEDIR}/dev/loop5
chown 0:6 ${MYTEMPLATEDIR}/dev/loop6
chown 0:6 ${MYTEMPLATEDIR}/dev/loop7
mknod --mode=0640 ${MYTEMPLATEDIR}/dev/mem c 1 1
mknod --mode=0640 ${MYTEMPLATEDIR}/dev/kmem c 1 2
mknod --mode=0640 ${MYTEMPLATEDIR}/dev/port c 1 4
chown 0:15 ${MYTEMPLATEDIR}/dev/mem
chown 0:15 ${MYTEMPLATEDIR}/dev/kmem
chown 0:15 ${MYTEMPLATEDIR}/dev/port
mknod --mode=0666 ${MYTEMPLATEDIR}/dev/null c 1 3
mknod --mode=0666 ${MYTEMPLATEDIR}/dev/zero c 1 5
mknod --mode=0666 ${MYTEMPLATEDIR}/dev/full c 1 7
mknod --mode=0666 ${MYTEMPLATEDIR}/dev/random c 1 8
mknod --mode=0666 ${MYTEMPLATEDIR}/dev/urandom c 1 9
mknod --mode=0600 ${MYTEMPLATEDIR}/dev/tty0 c 4 0
mknod --mode=0666 ${MYTEMPLATEDIR}/dev/tty c 5 0
mknod --mode=0600 ${MYTEMPLATEDIR}/dev/console c 5 1
mknod --mode=0666 ${MYTEMPLATEDIR}/dev/ptmx c 5 2
chown 0:5 ${MYTEMPLATEDIR}/dev/console
chown 0:5 ${MYTEMPLATEDIR}/dev/tty0
chown 0:5 ${MYTEMPLATEDIR}/dev/tty
chown 0:5 ${MYTEMPLATEDIR}/dev/ptmx
ln -s ram1 ${MYTEMPLATEDIR}/dev/ram
ln -s fd/2 ${MYTEMPLATEDIR}/dev/stderr
ln -s fd/0 ${MYTEMPLATEDIR}/dev/stdin
ln -s fd/1 ${MYTEMPLATEDIR}/dev/stdout
ln -s /proc/self/fd ${MYTEMPLATEDIR}/dev/fd
ln -s /proc/kcore ${MYTEMPLATEDIR}/dev/core
fi

# Backup created image to a tarball
/bin/tar -zcf ${VZROOT}/template/cache/${MYTEMPLATE}.tar.gz -C ${MYTEMPLATEDIR} .

fi

echo ""
echo "Configuring template."
echo ""

/bin/grep nameserver /etc/resolv.conf > ${MYTEMPLATEDIR}/etc/resolv.conf

# Configure time of day
if [ -f /etc/timezone ]; then
  /bin/cp /etc/timezone ${MYTEMPLATEDIR}/etc/timezone
elif [ -f /etc/sysconfig/clock ]; then
  cat /etc/sysconfig/clock | sed -n -e 's/^\([ ]*ZONE[ ]*=[ ]*"\)\(.*\)\("\).*$/\2/p' > ${MYTEMPLATEDIR}/etc/timezone
fi

echo "dpkg-reconfigure --frontend noninteractive tzdata"  | chroot ${MYTEMPLATEDIR}

# Update package list
echo "apt-get update -y" | chroot ${MYTEMPLATEDIR}


# Upgrade packages
echo "apt-get upgrade -y" | chroot ${MYTEMPLATEDIR}


echo ""
echo "Done."
echo ""
