#!/bin/bash this_file="$(realpath -s $0)" this_dir="$(dirname $this_file)" this_user=$SUDO_USER # Ensure running as sudo from home if [ `id -u` -ne 0 ]; then echo Need sudo exit 1 fi cd ~ # Update packages apt update apt install -y qemu-guest-agent chrony # Flush logs logrotate -f /etc/logrotate.conf systemctl stop rsyslog # Cleanup machine ID for regeneration during reboot cat /etc/machine-id >/etc/machine-id test -f /var/lib/dbus/machine-id && { rm /var/lib/dbus/machine-id; ln -s /etc/machine-id /var/lib/dbus/machine-id; } # Cleanup tmp and ssh directories rm -rf /tmp/* rm -rf /var/tmp/* rm -f /etc/ssh/ssh_host_* # Cleanup apt apt clean apt autoremove # Trim filesystem fstrim -av # First boot initialization script, # assuming 'vm-firstboot.sh' in same directory test -f /etc/rc.local && cp -a /etc/rc.local /etc/rc.local.bak cp -a "$this_dir/vm-firstboot.sh" /etc/rc.local chown root:root /etc/rc.local chmod +x /etc/rc.local