In this article we are going to show you how to create a Debian Server Virtual Machine Template based on the work of Thomas Liske over at Templating Debian GNU/Linux. While our process is primarily based on Thomas’s work, we have opted to go the bash scripting route instead of installing the ovfdep tool. It is important to note two things in regards to this article. First, we are not actually creating a VMware Template but something more along the lines of a Sysprep image that can be exported to .ova or imaged using your favorite imaging software. Second, while it has not yet been tested, this process will most likely work on Ubuntu Server as well. Thomas uses the terms dehydration and rehydration to describe the processes of preparing and then deploying the template. We really thought those terms made a lot of sense so we are sticking with them. Below are the scripts we will use to prepare ourĀ server for export or imaging (vm-dehydrate) and then deployment (vm-rehydrate).
vm-dehydrate
Usage: copy the script to the server and make it executable then run the script.
#!/bin/bash # # Sysprep OS for vmware template creation. # # echo "Removing openssh-server's host keys..." rm -vf /etc/ssh/ssh_host_* cat /dev/null > /etc/rc.local cat << EOF >> /etc/rc.local #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. rm -vf /root/vm-dehydrate > /dev/null dpkg-reconfigure openssh-server > /dev/null exit 0 EOF echo "Cleaning up /var/mail..." rm -vf /var/mail/* echo "Clean up apt cache..." find /var/cache/apt/archives -type f -exec rm -vf \{\} \; echo "Clean up ntp..." rm -vf /var/lib/ntp/ntp.drift rm -vf /var/lib/ntp/ntp.conf.dhcp echo "Clean up dhcp leases..." rm -vf /var/lib/dhcp/*.leases* rm -vf /var/lib/dhcp3/*.leases* echo "Clean up udev rules..." rm -vf /etc/udev/rules.d/70-persistent-cd.rules rm -vf /etc/udev/rules.d/70-persistent-net.rules echo "Clean up urandom seed..." rm -vf /var/lib/urandom/random-seed echo "Clean up backups..." rm -vrf /var/backups/*; rm -vf /etc/shadow- /etc/passwd- /etc/group- /etc/gshadow- /etc/subgid- /etc/subuid- echo "Cleaning up /var/log..." find /var/log -type f -name "*.gz" -exec rm -vf \{\} \; find /var/log -type f -name "*.1" -exec rm -vf \{\} \; find /var/log -type f -exec truncate -s0 \{\} \; echo "Compacting drive..." dd if=/dev/zero of=EMPTY bs=1M > /dev/null rm -vf /root/EMPTY echo "Clearing bash history..." cat /dev/null > /root/.bash_history history -c echo "Process complete..." poweroff
vm-rehydrate
Usage: once the template or image has been deployed, copy the script to the server and make it executable then run the script as seen below by only passing the new server host name to the script.
/root/vm-rehydrate NEW-HOSTNAME
#!/bin/bash # # Configure OS after template is deployed. # # echo "Setting hostname..." host=`hostname` new_host=$1 cp /etc/hosts /etc/hosts.bkp cp /etc/hostname /etc/hostname.bkp cp /etc/mailname /etc/mailname.bkp cat /etc/hosts.bkp | sed -e "s/$host/$new_host/g" > /etc/hosts cat /etc/hostname.bkp | sed -e "s/$host/$new_host/g" > /etc/hostname cat /etc/mailname.bkp | sed -e "s/$host/$new_host/g" > /etc/mailname rm -vf /etc/hosts.bkp /etc/hostname.bkp /etc/mailname.bkp echo "Cleaning /etc/rc.local..." cat /dev/null > /etc/rc.local cat << EOF >> /etc/rc.local #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. exit 0 EOF echo "Process complete..." reboot