Skip to content

Instantly share code, notes, and snippets.

@fnichol
Created September 12, 2012 05:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fnichol/3704431 to your computer and use it in GitHub Desktop.
Save fnichol/3704431 to your computer and use it in GitHub Desktop.
Ubuntu Postinstall
#!/usr/bin/env bash
# **postinstall.sh** is a script executed after Debian/Ubuntu has been
# installed and restarted. There is no user interaction so all commands must
# be able to run in a non-interactive mode.
#
# If any package install time questions need to be set, you can use
# `preeseed.cfg` to populate the settings.
#
# To run:
#
# wget -P /tmp/ https://raw.github.com/gist/3704431/postinstall.sh && \
# sudo bash /tmp/postinstall.sh
### Setup Variables
# The FQDN dummy host name.
hostname="unassigned-hostname.example.com"
# The non-root user that was created.
account="ubuntu"
# Download URL for smartos-vmtools (currently using a working fork)
vmtools_url="https://nodeload.github.com/prasmussen/smartos-vmtools/tarball/87a577cd39288f9ded4033baf89f6a4c1f64e009"
# Enable truly non interactive apt-get installs
export DEBIAN_FRONTEND=noninteractive
# Determine the platform (i.e. Debian or Ubuntu) and platform version
platform="$(lsb_release -i -s)"
platform_version="$(lsb_release -s -r)"
# Run the script in debug mode
set -x
### Customize Sudoers
# The main non-root user needs to have **password-less** sudo.
# This user belongs to the `admin`/`sudo` group, so we'll change that line.
sed -i -e '/Defaults\s\+env_reset/a Defaults\texempt_group=admin' /etc/sudoers
case "$platform" in
Debian)
sed -i -e 's/%sudo ALL=(ALL) ALL/%sudo ALL=NOPASSWD:ALL/g' /etc/sudoers
;;
Ubuntu)
groupadd -r admin || true
usermod -a -G admin $account
sed -i -e 's/%admin ALL=(ALL) ALL/%admin ALL=NOPASSWD:ALL/g' /etc/sudoers
;;
esac
### Set Hostname
# Set a stub hostname
host_first="$(echo $hostname | cut -d . -f 1)"
hostnames="${hostname} ${host_first}"
domainname=$(echo $hostname | sed "s/^$host_first\.//")
sed -i "s/^.*$/$host_first/" /etc/hostname
if egrep -q "^127.0.1.1[[:space:]]" /etc/hosts >/dev/null ; then
sed -i "s/^\(127[.]0[.]1[.]1[[:space:]]\+\)\(.*\)$/\1${hostnames} /" \
/etc/hosts
else
sed -i "s/^\(127[.]0[.]0[.]1[[:space:]]\+.*\)$/\1\n127.0.1.1 ${hostnames} /" \
/etc/hosts
fi
### Install smartos-vmtools
vmtools_tar=/tmp/smartos-vmtools.tar.gz
# Download and extract tarball
wget -O $vmtools_tar $vmtools_url
mkdir -p ${vmtools_tar%.tar.gz}
tar xfz $vmtools_tar -C ${vmtools_tar%.tar.gz} --strip-components=1
# Run installer and cleanup
(cd ${vmtools_tar%.tar.gz}/src/linux && echo y | ./install-tools.sh)
rm -rf /tmp/smartos-vmtools*
unset vmtools_tar
### Prepare Image
# Install extra packages used by SmartOS hypervisor
apt-get -y install arping acpid
# Unlock root account and remove any pre-exiting password
passwd -u -d root
# Prepare node for imaging
/lib/smartdc/prepare-image
### Clean up
# Remove the build tools to keep things pristine
apt-get -y autoremove
apt-get -y clean
# Add a 2 sec delay to the interface up, to make the dhclient happy
echo "pre-up sleep 2" >> /etc/network/interfaces
# Record when the box was built
date > /etc/box_build_time
# Clean up non-root user files
rm -f /home/${account}/{.bash_history,.lesshst}
# Clean up resolv.conf entries
for resolv in /etc/resolv.conf /etc/resolvconf/resolv.conf.d/original ; do
sed -i \
-e "s/^\(search[[:space:]]\).*$/\1$domainname/" \
-e "s/^\(nameserver[[:space:]]\).*$/\18.8.8.8/" $resolv
done ; unset resolv
# Remove any temporary work files, including the postinstall.sh script
rm -f /tmp/postinstall*.sh
# Shut the node down for snapshoting/cloning/etc.
shutdown -hP now && exit
# And we're done.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment