Skip to content

Instantly share code, notes, and snippets.

@harmon
Forked from JeffreyVdb/postinstall.sh
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save harmon/2c8f2119e8c588c036a6 to your computer and use it in GitHub Desktop.

Select an option

Save harmon/2c8f2119e8c588c036a6 to your computer and use it in GitHub Desktop.

Revisions

  1. Jeffrey revised this gist May 23, 2014. 1 changed file with 31 additions and 3 deletions.
    34 changes: 31 additions & 3 deletions postinstall.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    #!/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
    }
    @@ -65,18 +68,35 @@ clear_shell_histories() {
    }

    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
    --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() {
    @@ -89,6 +109,15 @@ install_rubygems() {
    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
    @@ -144,8 +173,7 @@ chown -R vagrant /home/vagrant/.ssh
    virtualbox_guest_install

    # Remove items used for building, since they aren't needed anymore
    apt-get -y remove linux-headers-$(uname -r) build-essential
    apt-get -y autoremove
    clean_packages

    # clear certain files and logs
    clear_logs
  2. Jeffrey revised this gist May 22, 2014. 1 changed file with 28 additions and 16 deletions.
    44 changes: 28 additions & 16 deletions postinstall.sh
    Original file line number Diff line number Diff line change
    @@ -64,6 +64,31 @@ clear_shell_histories() {
    done
    }

    install_ruby() {
    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
    make
    make install
    cd ..
    rm -rf ruby-1.8.7-p334*
    }

    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*
    }

    VBOX_VERSION_PATH="/tmp/vbox_version"
    VBOX_VERSION=$(cat "$VBOX_VERSION_PATH")
    if [ -z "$VBOX_VERSION" ]; then
    @@ -96,22 +121,9 @@ apt-get -y install nfs-common
    # 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.
    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
    ./configure --prefix=/opt/ruby
    make
    make install
    cd ..
    rm -rf ruby-1.8.7-p334*

    # 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*

    install_ruby
    install_rubygems

    # Install Chef & Puppet
    /opt/ruby/bin/gem install chef --no-ri --no-rdoc
  3. Jeffrey revised this gist May 22, 2014. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions postinstall.sh
    Original file line number Diff line number Diff line change
    @@ -33,12 +33,19 @@ nullify_swap() {
    }

    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() {
  4. Jeffrey revised this gist May 22, 2014. 1 changed file with 76 additions and 9 deletions.
    85 changes: 76 additions & 9 deletions postinstall.sh
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,70 @@
    #!/bin/bash

    nullify_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() {
    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
    }

    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
    }

    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
    @@ -55,21 +122,21 @@ wget --no-check-certificate 'https://github.com/mitchellh/vagrant/raw/master/keys
    chown -R vagrant /home/vagrant/.ssh

    # Install VirtualBox guest additions
    VBOX_VERSION=$(cat /home/vagrant/.vbox_version)
    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
    virtualbox_guest_install

    # Remove items used for building, since they aren't needed anymore
    apt-get -y remove linux-headers-$(uname -r) build-essential
    apt-get -y autoremove

    # clear certain files and logs
    clear_logs
    clear_shell_histories

    # Zero free space to aid VM compression
    dd if=/dev/zero of=/EMPTY bs=1M
    rm -f /EMPTY
    nullify_free_space

    # nullify swap space
    nullify_swap

    # Removing leftover leases and persistent rules
    echo "cleaning up dhcp leases"
  5. @evansd evansd renamed this gist Jun 28, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @evansd evansd created this gist Jun 28, 2011.
    89 changes: 89 additions & 0 deletions gistfile1.bash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,89 @@
    # 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.
    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
    ./configure --prefix=/opt/ruby
    make
    make install
    cd ..
    rm -rf ruby-1.8.7-p334*

    # 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*

    # 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 'https://github.com/mitchellh/vagrant/raw/master/keys/vagrant.pub' -O authorized_keys
    chown -R vagrant /home/vagrant/.ssh

    # Install VirtualBox guest additions
    VBOX_VERSION=$(cat /home/vagrant/.vbox_version)
    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

    # Remove items used for building, since they aren't needed anymore
    apt-get -y remove linux-headers-$(uname -r) build-essential
    apt-get -y autoremove

    # Zero free space to aid VM compression
    dd if=/dev/zero of=/EMPTY bs=1M
    rm -f /EMPTY

    # 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