#!/usr/bin/env bash # # A Linux system generalizer to prep for problem-free cloning # Copyright (C) 2016 Aaron Ball # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # # Description # ----------- # # **Danger, Will Robinson! Danger!** # This script is catastrophic! # # This script will generalize the system it is executed on, and remove itself # afterwards, leaving no trace. # # The intent is to produce a linux system that can be cloned without having # duplicate mac addresses, duplicate ssh host keys, strange unexpected shell # histories, etc. # # NOTE: This only fully works on Centos 6 and 7 presently. # # Set the log file # If not set, logs will be output to stdout #logfile=/root/cleanup.log function log { timestamp=$(date '+%F %T') if [[ -z ${logfile} ]]; then # Output to stdout if no logfile is specified echo "[${timestamp}]: ${*}" else # Logfile var set. Output there. echo "[${timestamp}]: ${*}" >> ${logfile} fi } function main { if [[ $(id -u) != 0 ]]; then echo "This script must be run as root, which you are not." return 1 fi log "Initializing cleanup" log "Removing ssh host_keys" rm -f /etc/ssh/ssh_host_* log "Preventing root command history from being written" unset HISTFILE log "Cleanup of root history file" rm -f /root/.bash_history log "Removing hard-coded mac addresses." service network stop local ifcfg_files=/etc/sysconfig/network-scripts/ifcfg-e* sed -i '/^HWADDR=.*$/d' ${ifcfg_files} log "Cleaning up nic udev rules..." # Centos 6 udev net rules file local net_6udev='/etc/udev/rules.d/70-persistent-net.rules' [[ -f "${net_6udev}" ]] && rm -f "${net_6udev}" # CentOS 7 udev net rules file local net_7udev='/etc/udev/rules.d/70-persistent-ipoib.rules' [[ -f "${net_7udev}" ]] && rm -f "${net_7udev}" # Delete myself (don't worry, I'll still exist in your memories/ram) rm -f ${mypath} log "Cleanup complete. Shutting down in 5 seconds." # Sleep for a few seconds so the user has a chance to cancel sleep 5 # Shutdown shutdown -h now } main ${@}