|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | + |
| 4 | +if [ ! -d "/testcase" ]; then |
| 5 | + echo "Error: Testcase path '/testcase' does not exist - ensure this only runs in a Testcase Debian VM." |
| 6 | + exit 1 |
| 7 | +fi |
| 8 | + |
| 9 | +if [ "$(id -u)" -ne 0 ]; then |
| 10 | + echo "Error: This script must be run as root." |
| 11 | + exit 1 |
| 12 | +fi |
| 13 | + |
| 14 | +echo "Preparing VM for Template use" |
| 15 | + |
| 16 | +set -x |
| 17 | +set -e |
| 18 | + |
| 19 | +echo "--- Updating System ---" |
| 20 | +apt update && apt dist-upgrade -y |
| 21 | +apt autoremove -y |
| 22 | +apt clean |
| 23 | + |
| 24 | +# This overrides the default DUID behavior to use the MAC address. |
| 25 | +mkdir -p /etc/systemd/network |
| 26 | + |
| 27 | +cat <<EOF > /etc/systemd/network/99-default.link |
| 28 | +[Match] |
| 29 | +OriginalName=* |
| 30 | +
|
| 31 | +[Link] |
| 32 | +MACAddressPolicy=persistent |
| 33 | +EOF |
| 34 | + |
| 35 | +cat <<EOF > /etc/systemd/network/99-dhcp-mac.network |
| 36 | +[Match] |
| 37 | +Name=e* |
| 38 | +
|
| 39 | +[Network] |
| 40 | +DHCP=ipv4 |
| 41 | +
|
| 42 | +[DHCPv4] |
| 43 | +ClientIdentifier=mac |
| 44 | +EOF |
| 45 | + |
| 46 | +# If netplan exists, we append the mac identifier to the config. |
| 47 | +if command -v netplan > /dev/null; then |
| 48 | + echo "Netplan detected. Applying fix..." |
| 49 | + # We can't easily sed YAML, so we ensure a file exists with the override |
| 50 | + # This might require manual checking if you have complex netplan configs, |
| 51 | + # but for standard templates, this ensures future renders use MAC. |
| 52 | + grep -q "dhcp-identifier: mac" /etc/netplan/*.yaml || echo "WARNING: Please manually add 'dhcp-identifier: mac' to your /etc/netplan/ config if you use Netplan." |
| 53 | +fi |
| 54 | + |
| 55 | +# Remove the machine-id file and create an empty one. |
| 56 | +# Systemd will generate a new unique ID on the next boot. |
| 57 | +rm -f /etc/machine-id |
| 58 | +touch /etc/machine-id |
| 59 | +rm -f /var/lib/dbus/machine-id |
| 60 | +ln -s /etc/machine-id /var/lib/dbus/machine-id |
| 61 | +rm -f /var/lib/systemd/random-seed |
| 62 | +rm -f /var/lib/systemd/duid |
| 63 | +rm -f /var/lib/dhcp/* |
| 64 | +rm -f /var/lib/NetworkManager/*.lease |
| 65 | + |
| 66 | +# For Standard Debian (ISC-DHCP-Client / ifupdown). |
| 67 | +if [ -f /etc/dhcp/dhclient.conf ]; then |
| 68 | + # Remove old entry if exists to avoid duplicates |
| 69 | + sed -i '/send dhcp-client-identifier/d' /etc/dhcp/dhclient.conf |
| 70 | + echo 'send dhcp-client-identifier = hardware;' >> /etc/dhcp/dhclient.conf |
| 71 | +fi |
| 72 | + |
| 73 | +# Clean Cloud-init |
| 74 | +if dpkg -l | grep -q cloud-init; then |
| 75 | + echo "Cloud-init detected. Cleaning logs..." |
| 76 | + cloud-init clean --logs --seed |
| 77 | + # Remove generated network configs so they regenerate on next boot |
| 78 | + rm -f /etc/network/interfaces.d/50-cloud-init |
| 79 | + rm -f /etc/netplan/50-cloud-init.yaml |
| 80 | +else |
| 81 | + echo "Cloud-init not installed. Skipping." |
| 82 | +fi |
| 83 | + |
| 84 | +# Keys will be regenerated on the first boot |
| 85 | +rm -f /etc/ssh/ssh_host_* |
| 86 | + |
| 87 | +# Clear audit logs, wtmp, btmp and other log files to reduce image size |
| 88 | +truncate -s 0 /var/log/wtmp |
| 89 | +truncate -s 0 /var/log/btmp |
| 90 | +truncate -s 0 /var/log/lastlog |
| 91 | +find /var/log -type f -name "*.log" -exec truncate -s 0 {} \; |
| 92 | +find /var/log -type f -name "*.gz" -delete |
| 93 | + |
| 94 | +# Clean history |
| 95 | +history -c |
| 96 | +unset HISTFILE |
| 97 | +rm -f /root/.bash_history |
| 98 | +rm -f /home/*/.bash_history |
| 99 | + |
| 100 | +# Delete this script |
| 101 | +rm -f "$0" |
0 commit comments