Last active
December 13, 2021 06:59
-
-
Save jdowning/5670884 to your computer and use it in GitHub Desktop.
Script to clean up Ubuntu Vagrant box before packaging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script zeroes out any space not needed for packaging a new Ubuntu Vagrant base box. | |
# Run the following command in a root shell: | |
# | |
# bash <(curl -s https://gist.github.com/justindowning/5670884/raw/vagrant-clean.sh) | |
function print_green { | |
echo -e "\e[32m${1}\e[0m" | |
} | |
print_green 'Clean Apt' | |
apt-get -y autoremove | |
aptitude clean | |
aptitude autoclean | |
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 | |
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!' |
I did swapoff Filename
and removed swap file
you might want to try a 'sync' between the dd and rm -f
Zero out the free space to save space in the final image:
dd if=/dev/zero of=/EMPTY bs=1M
sync
rm -f /EMPTY
sync
If you put @ in your print_green command instead of 1, then you can do
print_green this should output in green
and not have to do the very ugly
print_green 'this should output in green'
This script is like strapping a rocket to your back and going for a sled ride.
Main issues with it are:
- The swap zeroing bit does not check that there is actually a configured swap device, if there isn't it will think the swap device is a file called
Filename
. - The script does not allow for partition as files instead of devices. If a swap file is encountered, it will be grown to the size of the available free space. While on a roll, multiple swap devices are rare but perfectly legit.
Fix for the above two might be something like:
for swappart in $(grep partition /proc/swaps | awk -F ' ' '{print $1}')
do
swapoff $swappart
dd if=/dev/zero of=$swappart
mkswap -f $swappart
swapon $swappart
done
(swap file support left as an execise to the reader)
- The "whiteout of root" step is done over a file in /tmp. /tmp is not always on the root partition, indeed it can be in virtual memory using the tmpfs file system. Inexplicably, this one will try to whiteout 1 block less then available, meanwhile....
- The 'Zero out disk' step is explicitly going to cause a
dd: error writing ‘/EMPTY’: No space left on device
error every time. This is actually expected and should be suppressed or warned as it's surprising. Indeed one of the commenter above was surprised that they ran into the error and thought that was what failed for them. Appending a2>/dev/null
to the command-line would suppress that shocking warning.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After running this script on ubuntu box it filled disk space 100% to the point I can't event shut it down.
dd: error writing ‘/EMPTY’: No space left on device
Now I have a
Filename
file 37GB which I cannot delete even as super user.Any ideas?