simbafs

simbafs / Debian VM Template Clean Scriopt

Last active 1 week ago

Like 0
clean.sh Raw
1#!/usr/bin/env bash
2
3set -euo pipefail
4shopt -s nullglob
5
6if [[ "${EUID}" -ne 0 ]]; then
7 echo "ERROR: This script must be run as root."
8 exit 1
9fi
10
11echo "========================================"
12echo " PVE VM Template Cleanup"
13echo "========================================"
14
15echo "[1/9] Cleaning Cloud-Init state..."
16
17if command -v cloud-init >/dev/null 2>&1; then
18 clean_args=(--logs)
19 clean_help=$(cloud-init clean --help 2>&1 || true)
20
21 if grep -q -- "--machine-id" <<<"${clean_help}"; then
22 clean_args+=(--machine-id)
23 fi
24 if grep -q -- "--seed" <<<"${clean_help}"; then
25 clean_args+=(--seed)
26 fi
27 if grep -q -- "--configs" <<<"${clean_help}"; then
28 clean_args+=(--configs ssh_config network)
29 fi
30
31 cloud-init clean "${clean_args[@]}"
32else
33 echo "cloud-init is not installed; cleaning machine-id directly."
34fi
35
36# Ensure both systemd and legacy dbus machine IDs are empty. On this host the
37# dbus path is a symlink to /etc/machine-id, so it is not modified separately.
38truncate -s 0 /etc/machine-id
39if [[ -f /var/lib/dbus/machine-id && ! -L /var/lib/dbus/machine-id ]]; then
40 truncate -s 0 /var/lib/dbus/machine-id
41fi
42
43echo "[2/9] Removing SSH host keys and generated network configuration..."
44
45rm -f /etc/ssh/ssh_host_*
46rm -f /etc/network/interfaces.d/50-cloud-init
47
48# This file pins the current VM's MAC address and must not be cloned.
49rm -f /etc/udev/rules.d/70-persistent-net.rules
50
51echo "[3/9] Removing clone-specific SSH keys and history..."
52
53find /root /home -maxdepth 3 -type f \
54 \( -name authorized_keys -o -name known_hosts -o \
55 -name .bash_history -o -name .zsh_history -o \
56 -name .fish_history -o -name .python_history -o \
57 -name .mysql_history \) \
58 -delete 2>/dev/null || true
59
60echo "[4/9] Resetting hostname metadata..."
61
62printf '%s\n' localhost > /etc/hostname
63sed -i '/^127\.0\.1\.1[[:space:]]/d' /etc/hosts
64
65echo "[5/9] Cleaning DHCP leases..."
66
67rm -f /var/lib/dhcp/*.leases 2>/dev/null || true
68rm -f /var/lib/dhcp/*.lease 2>/dev/null || true
69rm -f /var/lib/dhclient/*.leases 2>/dev/null || true
70rm -f /var/lib/dhclient/*.lease 2>/dev/null || true
71rm -f /run/systemd/netif/leases/* 2>/dev/null || true
72rm -f /var/lib/NetworkManager/*.lease 2>/dev/null || true
73rm -f /var/lib/NetworkManager/*lease* 2>/dev/null || true
74
75echo "[6/9] Removing systemd random seed and runtime state..."
76
77rm -f /var/lib/systemd/random-seed
78rm -rf /run/cloud-init 2>/dev/null || true
79
80echo "[7/9] Cleaning temporary files and logs..."
81
82find /tmp /var/tmp -mindepth 1 -maxdepth 1 -exec rm -rf -- {} + 2>/dev/null || true
83
84if command -v journalctl >/dev/null 2>&1; then
85 journalctl --rotate 2>/dev/null || true
86 journalctl --vacuum-time=1s 2>/dev/null || true
87fi
88
89find /var/log -type f -name '*.log' -exec truncate -s 0 -- {} + 2>/dev/null || true
90find /var/log -type f -name '*.gz' -delete 2>/dev/null || true
91
92echo "[8/9] Trimming filesystem..."
93
94sync
95if command -v fstrim >/dev/null 2>&1; then
96 fstrim -av || true
97fi
98sync
99
100echo "[9/9] Final checks..."
101
102[[ ! -e /etc/ssh/ssh_host_ed25519_key ]] || {
103 echo "ERROR: SSH host keys still exist."
104 exit 1
105}
106[[ ! -e /etc/udev/rules.d/70-persistent-net.rules ]] || {
107 echo "ERROR: persistent network rule still exists."
108 exit 1
109}
110
111echo
112echo "========================================"
113echo " Cleanup completed"
114echo "========================================"
115echo
116echo "Cleaned:"
117echo " - Cloud-Init instance state, seed, and generated network config"
118echo " - machine-id"
119echo " - SSH host keys and user SSH known/authorized keys"
120echo " - shell history"
121echo " - persistent MAC mapping"
122echo " - hostname metadata"
123echo " - DHCP leases"
124echo " - systemd random seed"
125echo " - temporary files and logs"
126echo
127echo "Preserved:"
128echo " - package installation and package cache"
129echo " - base network configuration in /etc/network/interfaces"
130echo
131echo "DO NOT reboot this VM."
132echo "Power it off and convert it to a PVE template."
133echo