Skip to content

Instantly share code, notes, and snippets.

@itskenny0
Created April 11, 2018 08:45
Show Gist options
  • Save itskenny0/94ba6025bd0c40bffd581d6d042e5e68 to your computer and use it in GitHub Desktop.
Save itskenny0/94ba6025bd0c40bffd581d6d042e5e68 to your computer and use it in GitHub Desktop.
build.sh - build Ubuntu VM with ZeroTier and customization from the official Ubuntu Cloud images
#!/bin/bash
CIURL="https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img"
ZTURL="https://download.zerotier.com/zerotier-one-x64.deb"
PWD=$(pwd)
TEMP=$(mktemp -d)
HOSTNAME=$1
ROOTPW=abcd1234
# output colors
CYAN=$(echo -ne "\033[00;36m")
GREEN=$(echo -en '\033[01;32m')
MAG=$(echo -en '\033[01;35m')
YEL=$(echo -en '\033[00;33m')
NOCOL=$(echo -ne "\E[0m")
function logStep {
echo "${MAG}[$(date)]${NOCOL} $@"
}
# mount image
logStep "Downloading base image"
wget --show-progress -qOdisk.qcow2 $CIURL
logStep "Mounting image"
guestmount -a disk.qcow2 -m /dev/sda1 $TEMP
logStep "Mounting system folders"
# chroot mounts
mount --bind /dev $TEMP/dev
#mount --bind /dev/pts $TEMP/dev/pts
#mount --bind /sys $TEMP/sys
#mount --bind /proc $TEMP/proc
#mount --bind /run $TEMP/run
logStep "Adding resolv.conf"
unlink $TEMP/etc/resolv.conf
cp /etc/resolv.conf $TEMP/etc/resolv.conf
logStep "Setting hostname"
chroot $TEMP hostnamectl set-hostname $HOSTNAME
sed -i "s/127.0.0.1 localhost/127.0.0.1 localhost $HOSTNAME/" $TEMP/etc/hosts
echo $HOSTNAME >$TEMP/etc/hostname
logStep "Downloading ZeroTier Debian package"
wget --show-progress -qO $TEMP/root/zerotier.deb $ZTURL
logStep "Set root password"
echo "root:$ROOTPW" | chroot $TEMP chpasswd
# install salt-minion, zerotier, remove cloud-init
logStep "apt update"
chroot $TEMP apt-get -yqq update
logStep "Installing salt-minion"
chroot $TEMP apt-get -yqq install salt-minion
logStep "Removing cloud-init"
chroot $TEMP apt-get -yqq autoremove cloud-init
logStep "Installing zerotier-one"
chroot $TEMP dpkg -i /root/zerotier.deb
rm $TEMP/root/zerotier.deb
logStep "Copying ZeroTier configuration from host"
# copy zerotier network config from host
mkdir -p $TEMP/var/lib/zerotier-one/
cp -R /var/lib/zerotier-one/networks.d $TEMP/var/lib/zerotier-one/
echo "f3c87f2a65e40ba4=zt0" >$TEMP/var/lib/zerotier-one/devicemap
logStep "Configuring network interface"
echo >>$TEMP/etc/network/interfaces
echo "auto ens3" >>$TEMP/etc/network/interfaces
echo "iface ens3 inet dhcp" >>$TEMP/etc/network/interfaces
# start bash for customization
logStep "Starting bash shell for customization; exit shell to finish building image"
chroot $TEMP bash
sleep 5
logStep "Unmounting system folders"
# umount stuff
#umount $TEMP/run
#umount $TEMP/dev/pts
umount $TEMP/dev
#umount $TEMP/sys
#umount $TEMP/proc
#umount $TEMP/run
logStep "Unmounting image"
# unmount
umount $TEMP
rmdir $TEMP
logStep "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment