Skip to content

Instantly share code, notes, and snippets.

@tjarrett
Last active April 3, 2022 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save tjarrett/b4f5e1b5359951c4a336fe1f5941c5dc to your computer and use it in GitHub Desktop.
Save tjarrett/b4f5e1b5359951c4a336fe1f5941c5dc to your computer and use it in GitHub Desktop.
For minimizing disk size of Centos 7 virtualbox machines for use with vagrant
#!/bin/bash
# Source: http://vstone.eu/reducing-vagrant-box-size/
# Modified by: Jason Yang [jason.yang at mercatustechnologies.com]
function pause() {
read -p "$*"
}
cat - << EOWARNING
WARNING: This script will fill up your left over disk space.
DO NOT RUN THIS WHEN YOUR VIRTUAL HD IS RAW!!!!!!
You should NOT do this on a running system.
This is purely for making vagrant boxes damn small.
EOWARNING
pause "Press [Enter] key to continue... or [Ctrl-C] to abort."
echo 'Uninstall unnecessary packages'
yum erase -y dkms kernel-devel* kernel-headers
yum groupremove -y "Development Tools"
echo 'Remove unnecessary locales'
cd /usr/share/locale
find . -maxdepth 1 ! -name 'en_US' -type d -exec rm -Rf {} \;
echo 'Cleaning up yum cache'
yum clean all
echo 'Cleanup bash history'
# Unset the HISTFILE environment variable so that the history
# for the current session isn't saved when you exit
unset HISTFILE
# Remove all bash histories
find / -type f -name .bash_history -exec cp /dev/null {} \; -exec echo "Clearing history: {}" \;
echo 'Cleanup log files'
find /var/log -type f -exec cp /dev/null {} \; -exec echo "Truncating log file: {}" \;
echo '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;
echo '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;
swappart=`cat /proc/swaps | tail -n1 | awk -F ' ' '{print $1}'`;
echo "Truncating swap file: $swappart"
swapoff $swappart;
dd if=/dev/zero of=$swappart;
mkswap $swappart;
swapon $swappart;
@feload
Copy link

feload commented Sep 9, 2018

Oh man, thank you for publishing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment