-
-
Save harmon/2c8f2119e8c588c036a6 to your computer and use it in GitHub Desktop.
Better postinstall.sh
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 | |
# Setup a debian based vagrant machine | |
TO_CLEAN_PKGS=() | |
nullify_free_space() { | |
echo 'clearing free space' | |
dd if=/dev/zero of=/EMPTY bs=1M | |
rm -f /EMPTY | |
} | |
clear_logs() { | |
local LOGPATH='/var/log/' | |
local logFile='' | |
local IFS=$'\n' | |
echo 'clearing logs' | |
# remove all the gz archives | |
find $LOGPATH -type f -iname "*.gz" -exec rm -v {} \; | |
while read logFile; do | |
# clear log files to 0 size | |
echo "truncate: $logFile" | |
: >${logFile} | |
done < <(find $LOGPATH -type f) | |
} | |
nullify_swap() { | |
# thanks to: http://vstone.eu/reducing-vagrant-box-size/ | |
local swappart=$(cat /proc/swaps | tail -n1 | awk -F ' ' '{print $1}') | |
echo 'emptying swap space' | |
swapoff $swappart | |
dd if=/dev/zero of=$swappart bs=1M | |
mkswap $swappart | |
swapon $swappart | |
} | |
virtualbox_guest_install() { | |
local VGA='VBoxGuestAdditions' | |
cd /tmp | |
wget http://download.virtualbox.org/virtualbox/$VBOX_VERSION/VBoxGuestAdditions_$VBOX_VERSION.iso | |
mount -o loop VBoxGuestAdditions_$VBOX_VERSION.iso /mnt | |
sh /mnt/VBoxLinuxAdditions.run | |
umount /mnt | |
rm VBoxGuestAdditions_$VBOX_VERSION.iso | |
# bug on virtualbox 4.3.10 | |
# provide for universal solution | |
if [ -d "/opt/${VGA}-${VBOX_VERSION}" -a ! -e "/usr/lib/${VGA}" ]; then | |
ln -s /opt/${VGA}-${VBOX_VERSION}/lib/${VGA} /usr/lib/${VGA} | |
fi | |
} | |
clear_shell_histories() { | |
local HOME_PATHS=( '/root' '/home/vagrant' ) | |
local HIST_FILES=( '.zsh_history' '.bash_history' ) | |
local curpath='' | |
for curpath in "${HOME_PATHS[@]}"; do | |
local hist_file='' | |
for hist_file in "${HIST_FILES[@]}"; do | |
local abs_hist_file="${curpath}/${hist_file}" | |
[ -f "${abs_hist_file}" ] && { | |
echo "truncate: $abs_hist_file" | |
:>$abs_hist_file | |
} | |
done | |
done | |
} | |
install_ruby() { | |
# install dependencies | |
apt-get -y install zlib1g-dev libssl-dev libc6-dev libncurses5-dev | |
# due to problems on openssl on debian based distributions, install the | |
# openssl package from tvm and point to this location using the configure | |
# flags | |
# | |
# install rvm | |
apt-get -y install curl | |
curl -sSL https://get.rvm.io | /bin/bash | |
source /etc/profile | |
rvm pkg install openssl | |
# get ruby archive and install | |
wget http://ftp.ruby-lang.org/pub/ruby/ruby-1.8.7-p334.tar.gz | |
tar xvzf ruby-1.8.7-p334.tar.gz | |
cd ruby-1.8.7-p334 | |
# segmentation faults on older versions of gcc | |
# use these cflags to compile everything correctly | |
./configure CFLAGS="-O2 -fno-tree-dce -fno-optimize-sibling-calls" \ | |
--prefix="/opt/ruby" --with-openssl-dir="/usr/local/rvm/usr" | |
make | |
make install | |
cd .. | |
rm -rf ruby-1.8.7-p334* | |
# add to clean packages | |
TO_CLEAN_PKGS+=( 'libncurses5-dev' 'libssl-dev' 'zlib1g-dev' 'libc6-dev' ) | |
} | |
install_rubygems() { | |
# Install RubyGems 1.7.2 | |
wget http://production.cf.rubygems.org/rubygems/rubygems-1.7.2.tgz | |
tar xzf rubygems-1.7.2.tgz | |
cd rubygems-1.7.2 | |
/opt/ruby/bin/ruby setup.rb | |
cd .. | |
rm -rf rubygems-1.7.2* | |
} | |
clean_packages() { | |
# remove dev packages | |
apt-get -y remove linux-headers-$(uname -r) build-essential | |
apt-get -y remove "${TO_CLEAN_PKGS[@]}" | |
# remove dependencies on removed packages | |
apt-get -y autoremove | |
} | |
VBOX_VERSION_PATH="/tmp/vbox_version" | |
VBOX_VERSION=$(cat "$VBOX_VERSION_PATH") | |
if [ -z "$VBOX_VERSION" ]; then | |
echo "no version $VBOX_VERSION_PATH found." | |
touch $VBOX_VERSION_PATH | |
exit 1 | |
fi | |
# Apt-install various things necessary for Ruby, guest additions, | |
# etc., and remove optional things to trim down the machine. | |
apt-get -y update | |
apt-get -y remove apparmor | |
apt-get -y install linux-headers-$(uname -r) build-essential | |
apt-get -y install zlib1g zlib1g-dev libxml2 libxml2-dev libxslt-dev libssl-dev openssl libreadline5-dev | |
apt-get clean | |
# Remove this file to avoid dhclient issues with networking | |
rm -f /etc/udev/rules.d/70-persistent-net.rules | |
# Setup sudo to allow no-password sudo for "admin". Additionally, | |
# make "admin" an exempt group so that the PATH is inherited. | |
cp /etc/sudoers /etc/sudoers.orig | |
sed -i -e '/Defaults\s\+env_reset/a Defaults\texempt_group=admin' /etc/sudoers | |
sed -i -e 's/%admin ALL=(ALL) ALL/%admin ALL=NOPASSWD:ALL/g' /etc/sudoers | |
# Install NFS client | |
apt-get -y install nfs-common | |
# Install Ruby from source in /opt so that users of Vagrant | |
# can install their own Rubies using packages or however. | |
# We must install the 1.8.x series since Puppet doesn't support | |
# Ruby 1.9 yet. | |
install_ruby | |
install_rubygems | |
# Install Chef & Puppet | |
/opt/ruby/bin/gem install chef --no-ri --no-rdoc | |
/opt/ruby/bin/gem install puppet --no-ri --no-rdoc | |
# Add /opt/ruby/bin to the global path as the last resort so | |
# Ruby, RubyGems, and Chef/Puppet are visible | |
echo 'PATH=$PATH:/opt/ruby/bin/'> /etc/profile.d/vagrantruby.sh | |
# Install insecure Vagrant SSH keys | |
mkdir /home/vagrant/.ssh | |
chmod 700 /home/vagrant/.ssh | |
cd /home/vagrant/.ssh | |
wget --no-check-certificate 'http://github.com/mitchellh/vagrant/raw/master/keys/vagrant.pub' -O authorized_keys | |
chown -R vagrant /home/vagrant/.ssh | |
# Install VirtualBox guest additions | |
virtualbox_guest_install | |
# Remove items used for building, since they aren't needed anymore | |
clean_packages | |
# clear certain files and logs | |
clear_logs | |
clear_shell_histories | |
# Zero free space to aid VM compression | |
nullify_free_space | |
# nullify swap space | |
nullify_swap | |
# Removing leftover leases and persistent rules | |
echo "cleaning up dhcp leases" | |
rm /var/lib/dhcp3/* | |
# Make sure Udev doesn't block our network | |
# http://6.ptmc.org/?p=164 | |
echo "cleaning up udev rules" | |
rm /etc/udev/rules.d/70-persistent-net.rules | |
mkdir /etc/udev/rules.d/70-persistent-net.rules | |
rm -rf /dev/.udev/ | |
rm /lib/udev/rules.d/75-persistent-net-generator.rules | |
echo "Adding a 2 sec delay to the interface up, to make the dhclient happy" | |
echo "pre-up sleep 2" >> /etc/network/interfaces | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment