====== Prepare a Linux VM as a Template ======
===== Ubuntu =====
https://jimangel.io/post/create-a-vm-template-ubuntu-18.04/
curl -sL https://raw.githubusercontent.com/jimangel/ubuntu-18.04-scripts/master/prepare-ubuntu-18.04-template.sh | sudo -E bash -
===== RHEL Systems (RedHat, CentOS, etc.) =====
http://lonesysadmin.net/2013/03/26/preparing-linux-template-vms/
Shutdown the GUI, if there is one:
init 3
:!: The next step means you may or may not be able to connect to the VM remotely once instantiated.
Set the networking to DHCP mode so new VMs don't step on others once instantiated:
sed -i '/^\(HWADDR\|UUID\|BOOTPROTO\|IPADDR\|NETMASK\|NM_CONTROLLED\|ONBOOT\)=/d' /etc/sysconfig/network-scripts/ifcfg-eth[012]
echo -e "BOOTPROTO=dhcp\nNM_CONTROLLED=no\nONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0
Clean out the junk:
yum clean all
logrotate -f /etc/logrotate.conf
rm -f /var/log/*-???????? /var/log/*.gz
cat /dev/null > /var/log/audit/audit.log
cat /dev/null > /var/log/wtmp
rm -f /etc/udev/rules.d/70*
rm -rf /tmp/*
rm -rf /var/tmp/*
rm -rf /etc/ssh/*key*
rm -rf ~root/.bash_history
unset HISTFILE
===== Zero the Filesystem =====
This is an **optional** script for RHEL systems to zero out the filesystem.
:!: Don't do this on thinly provisioned VMs (i.e. VHDs on a XenServer NFS SR).
FIXME UNTESTED
#!/bin/sh
# Determine the version of RHEL
COND=`grep -i Taroon /etc/redhat-release`
if [ "$COND" = "" ]; then
export PREFIX="/usr/sbin"
else
export PREFIX="/sbin"
fi
FileSystem=`grep ext /etc/mtab| awk -F" " '{ print $2 }'`
for i in $FileSystem
do
echo $i
number=`df -B 512 $i | awk -F" " '{print $3}' | grep -v Used`
echo $number
percent=$(echo "scale=0; $number * 98 / 100" | bc )
echo $percent
dd count=`echo $percent` if=/dev/zero of=`echo $i`/zf
/bin/sync
sleep 15
rm -f $i/zf
done
VolumeGroup=`$PREFIX/vgdisplay | grep Name | awk -F" " '{ print $3 }'`
for j in $VolumeGroup
do
echo $j
$PREFIX/lvcreate -l `$PREFIX/vgdisplay $j | grep Free | awk -F" " '{ print $5 }'` -n zero $j
if [ -a /dev/$j/zero ]; then
cat /dev/zero > /dev/$j/zero
/bin/sync
sleep 15
$PREFIX/lvremove -f /dev/$j/zero
fi
done