Skip to content

Instantly share code, notes, and snippets.

@mylesagray
Created June 5, 2019 12:06
Show Gist options
  • Save mylesagray/e3635893820661f0dd58d76599e47e9c to your computer and use it in GitHub Desktop.
Save mylesagray/e3635893820661f0dd58d76599e47e9c to your computer and use it in GitHub Desktop.
#!/bin/bash
# Update the system, cleans out all of the cloud-init cache, disable and remove cloud-init customisations
sudo apt update
sudo apt install open-vm-tools -y
sudo apt upgrade -y
sudo cloud-init clean --logs
sudo touch /etc/cloud/cloud-init.disabled
sudo rm -rf /etc/netplan/50-cloud-init.yaml
sudo apt purge cloud-init -y
sudo apt autoremove -y
echo "Housekeeping done"
# Don't clear /tmp
sudo sed -i 's/D \/tmp 1777 root root -/#D \/tmp 1777 root root -/g' /usr/lib/tmpfiles.d/tmp.conf
# Remove cloud-init and rely on dbus for open-vm-tools
sudo sed -i 's/Before=cloud-init-local.service/After=dbus.service/g' /lib/systemd/system/open-vm-tools.service
echo "Cloud-init disabled"
# cleanup current ssh keys so templated VMs get fresh key
sudo rm -f /etc/ssh/ssh_host_*
echo "SSH host keys cleared"
# add check for ssh keys on reboot...regenerate if neccessary
sudo tee /etc/rc.local >/dev/null <<EOL
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "" 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.
test -f /etc/ssh/ssh_host_dsa_key || dpkg-reconfigure openssh-server
exit 0
EOL
# make the script executable
sudo chmod +x /etc/rc.local
echo "SSH host keys regen script added"
# cleanup apt
sudo apt clean
echo "Apt cleaned"
# reset the machine-id (DHCP leases in 18.04 are generated based on this... not MAC...)
echo "" | sudo tee /etc/machine-id >/dev/null
echo "Machine-id reset"
# disable swap for K8s
sudo swapoff --all
sudo sed -ri '/\sswap\s/s/^#?/#/' /etc/fstab
echo "Swap disabled"
# cleanup shell history and shutdown for templating
history -c
history -w
echo "History cleaned"
sudo shutdown -h now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment