cleanup.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env bash
  2. #
  3. # cleanup.sh — strip all instance-specific state and secrets from the image.
  4. #
  5. # Runs LAST. The output image must contain no panel database, no credentials,
  6. # no SSH host keys, and no baked authorized_keys. Fails the build if any of
  7. # those survive.
  8. set -euo pipefail
  9. echo "[cleanup] removing panel database, credentials and first-boot sentinel..."
  10. rm -f /etc/x-ui/x-ui.db /etc/x-ui/x-ui.db-* 2> /dev/null || true
  11. rm -f /etc/x-ui/install-result.env /etc/x-ui/credentials.txt 2> /dev/null || true
  12. rm -f /etc/x-ui/.firstboot-done 2> /dev/null || true
  13. echo "[cleanup] removing SSH host keys (regenerated on first boot)..."
  14. rm -f /etc/ssh/ssh_host_* 2> /dev/null || true
  15. echo "[cleanup] removing any baked authorized_keys..."
  16. rm -f /root/.ssh/authorized_keys 2> /dev/null || true
  17. find /home -maxdepth 3 -name authorized_keys -type f -delete 2> /dev/null || true
  18. echo "[cleanup] resetting machine-id..."
  19. truncate -s 0 /etc/machine-id 2> /dev/null || true
  20. rm -f /var/lib/dbus/machine-id 2> /dev/null || true
  21. ln -sf /etc/machine-id /var/lib/dbus/machine-id 2> /dev/null || true
  22. echo "[cleanup] resetting cloud-init so it re-runs on the real first boot..."
  23. cloud-init clean --logs --seed > /dev/null 2>&1 || rm -rf /var/lib/cloud/* 2> /dev/null || true
  24. echo "[cleanup] truncating logs, history and package caches..."
  25. find /var/log -type f -exec truncate -s 0 {} + 2> /dev/null || true
  26. rm -rf /var/lib/x-ui /var/log/x-ui/* 2> /dev/null || true
  27. apt-get clean || true
  28. rm -rf /var/lib/apt/lists/* 2> /dev/null || true
  29. rm -f /root/.bash_history 2> /dev/null || true
  30. find /home -maxdepth 3 -name .bash_history -type f -delete 2> /dev/null || true
  31. rm -rf /tmp/firstboot 2> /dev/null || true
  32. echo "[cleanup] verifying the image is clean..."
  33. fail=0
  34. for f in /etc/x-ui/x-ui.db /etc/x-ui/credentials.txt /etc/x-ui/install-result.env /etc/x-ui/.firstboot-done; do
  35. if [ -e "$f" ]; then
  36. echo "[cleanup] FATAL: $f is present in the image" >&2
  37. fail=1
  38. fi
  39. done
  40. if ls /etc/ssh/ssh_host_* > /dev/null 2>&1; then
  41. echo "[cleanup] FATAL: SSH host keys present in the image" >&2
  42. fail=1
  43. fi
  44. if [ -e /root/.ssh/authorized_keys ]; then
  45. echo "[cleanup] FATAL: /root/.ssh/authorized_keys present in the image" >&2
  46. fail=1
  47. fi
  48. if [ "$fail" -ne 0 ]; then
  49. exit 1
  50. fi
  51. echo "[cleanup] OK — no DB, no credentials, no host keys, no authorized_keys."