Skip to content

Instantly share code, notes, and snippets.

@TomasKulhanek
Forked from jdowning/vagrant-clean.sh
Last active April 3, 2020 03:21
Show Gist options
  • Save TomasKulhanek/fb3554f1265fb9b9fc71310a2678a0bc to your computer and use it in GitHub Desktop.
Save TomasKulhanek/fb3554f1265fb9b9fc71310a2678a0bc to your computer and use it in GitHub Desktop.
Script to clean up RHEL based (Centos, Fedora,Scientific Linux) Vagrant box before packaging
#!/bin/bash
# This script zeroes out any space not needed for packaging a new RHEL based Vagrant base box.
# It is modified version of https://gist.github.com/justindowning/5670884
# Run the following command in a root shell:
#
# bash <(curl -s https://gist.githubusercontent.com/TomasKulhanek/fb3554f1265fb9b9fc71310a2678a0bc/raw/vagrant-clean.sh)
function print_green {
echo -e "\e[32m${1}\e[0m"
}
print_green 'Clean Yum'
yum -y remove linux-firmware
yum -y remove iwl*
yum -y autoremove
yum clean all
rm -rf /var/cache/yum
print_green 'Cleanup bash history'
unset HISTFILE
[ -f /root/.bash_history ] && rm /root/.bash_history
[ -f /home/vagrant/.bash_history ] && rm /home/vagrant/.bash_history
rm -rf /home/vagrant/.cache /home/vagrant/.local /root/.cache /root/.local
print_green 'Cleanup log files'
find /var/log -type f | while read f; do echo -ne '' > $f; done
print_green 'Whiteout root'
count=`df --sync -kP / | tail -n1 | awk -F ' ' '{print $4}'`
let count--
dd if=/dev/zero of=/tmp/whitespace bs=1024 count=$count
rm /tmp/whitespace
print_green 'Whiteout /boot'
count=`df --sync -kP /boot | tail -n1 | awk -F ' ' '{print $4}'`
let count--
dd if=/dev/zero of=/boot/whitespace bs=1024 count=$count;
rm /boot/whitespace
print_green 'Whiteout swap'
swappart=`cat /proc/swaps | tail -n1 | awk -F ' ' '{print $1}'`
swapoff $swappart
dd if=/dev/zero of=$swappart
mkswap -f $swappart
swapon $swappart
print_green 'Zero out disk'
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY
print_green 'Vagrant cleanup complete'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment