Skip to content

Instantly share code, notes, and snippets.

@erikjoens
Created October 4, 2018 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikjoens/77fcbeb77d7b1576bc9b60ef05d8950a to your computer and use it in GitHub Desktop.
Save erikjoens/77fcbeb77d7b1576bc9b60ef05d8950a to your computer and use it in GitHub Desktop.
#!/bin/bash
# setup.sh will install all necessary files for the compose file to be runnable from a clean rasbian installation.
# Ensure we are root
if [[ $(id -u) -ne 0 ]] ; then echo "Please run as root, eg. \"sudo ./setup.sh\"" ; exit 1 ; fi
echo "---------------------------------------------------------------------------"
echo "This script is intended to be run on a fresh install of RASPBIAN STRETCH LITE"
echo "other usage has not been tested."
echo
echo "It will change hostname, create a new user. Install ntp, busybox-syslogd,"
echo "docker and docker-compose as well as adding persistant symlinks to usb devices"
echo "so they can be used by Home Assistant in a persistant way."
echo "---------------------------------------------------------------------------"
echo
# Confirm continue
echo -n "I'm aware of the risks, CONTINUE? [y/N] "
read
if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
echo "Canceled."
exit 0
fi
clear
# Helper functions
# Given a filename, a regex pattern to match and a replacement string:
# Replace string if found, else no change.
# (# $1 = filename, $2 = pattern to match, $3 = replacement)
replace() {
grep $2 $1 >/dev/null
if [ $? -eq 0 ]; then
# Pattern found; replace in file
sed -i "s/$2/$3/g" $1 >/dev/null
fi
}
# Given a filename, a regex pattern to match and a replacement string:
# If found, perform replacement, else append file w/replacement on new line.
replaceAppend() {
grep $2 $1 >/dev/null
if [ $? -eq 0 ]; then
# Pattern found; replace in file
sed -i "s/$2/$3/g" $1 >/dev/null
else
# Not found; append on new line (silently)
echo $3 | sudo tee -a $1 >/dev/null
fi
}
# Given a filename, a regex pattern to match and a string:
# If found, no change, else append file with string on new line.
append1() {
grep $2 $1 >/dev/null
if [ $? -ne 0 ]; then
# Not found; append on new line (silently)
echo $3 | sudo tee -a $1 >/dev/null
fi
}
# Given a filename, a regex pattern to match and a string:
# If found, no change, else append space + string to last line --
# this is used for the single-line /boot/cmdline.txt file.
append2() {
grep $2 $1 >/dev/null
if [ $? -ne 0 ]; then
# Not found; insert in file before EOF
sed -i "s/\'/ $3/g" $1 >/dev/null
fi
}
# Setup timezone
echo "Setting up timezone to \"Europe/Stockholm\".."
rm /etc/localtime
echo "Europe/Stockholm" > /etc/timezone
dpkg-reconfigure -f noninteractive tzdata
echo
echo "----------------------------------------"
echo
# Setup locale
echo "Setting up locale to \"sv_SE.UTF-8\".."
LOCALE="sv_SE.UTF-8"
if ! LOCALE_LINE="$(grep "^$LOCALE " /usr/share/i18n/SUPPORTED)"; then
echo "Setting up locale failed."
exit 1
fi
ENCODING="$(echo $LOCALE_LINE | cut -f2 -d " ")"
echo "$LOCALE $ENCODING" > /etc/locale.gen
sed -i "s/^\s*LANG=\S*/LANG=$LOCALE/" /etc/default/locale
dpkg-reconfigure -f noninteractive locales
echo
echo "----------------------------------------"
echo
# Choose hostname
CURRENT_HOSTNAME=`cat /etc/hostname | tr -d " \t\n\r"`
echo "Current hostname: $CURRENT_HOSTNAME, you should change this to something better, eg. \"pi-DESCRIPTIVE_LOCATION\""
echo -n "Input new hostname and press [ENTER]: "
read NEW_HOSTNAME
echo -n "New hostname will be \"$NEW_HOSTNAME\", please confirm? [y/N]: "
read
if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
echo "Canceled."
exit 0
fi
echo $NEW_HOSTNAME > /etc/hostname
sed -i "s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\t$NEW_HOSTNAME/g" /etc/hosts
echo
echo "----------------------------------------"
echo
# Create new user
echo -n "Input new username and press [ENTER]: "
read NEW_USER
# Adds some useful stuff to .bashrc skeleton file
echo "alias ll='ls -lah" >> "/etc/skel/.bashrc"
adduser $NEW_USER
for GROUP in adm dialout cdrom sudo audio video plugdev games users netdev input spi i2c gpio; do adduser $NEW_USER $GROUP; done
# Install packages
echo
echo "----------------------------------------"
echo
echo "Checking for updates"
apt update
echo "Upgrading.. (this might take some time)"
apt upgrade -y
echo
echo "----------------------------------------"
echo
echo "Installing Git"
apt -y install git
echo
echo "----------------------------------------"
echo
echo "Installing Docker"
curl -sSL https://get.docker.com | sh
echo "Adds $NEW_USER to docker group"
usermod -aG docker $NEW_USER
echo
echo "----------------------------------------"
echo
echo "Installing ntp and busybox-syslogd.."
apt-get -y --force-yes install ntp busybox-syslogd; dpkg --purge rsyslog
echo
echo "----------------------------------------"
echo
echo "Installing pip and docker-compose"
apt install -y python python-pip
pip install docker-compose
echo
echo "----------------------------------------"
echo
echo -n "Do you want to install PiJuice CLI (headless)? [y/N]: "
read
if [[ "$REPLY" =~ ^(yes|y|Y)$ ]]; then
apt install -y pijuice-base
fi
echo
echo "----------------------------------------"
echo
# Setup persistant device paths
USB_SERIAL_RULES_FILE="/etc/udev/rules.d/99-usb-serial.rules"
echo "Looking for USB devices.."
# Find Z-Wave device
if udevadm info -a -n /dev/ttyACM* | grep -Fq '{idProduct}=="0200"' | head -n1 && udevadm info -a -n /dev/ttyACM* | grep -Fq '{idVendor}=="0658"' | head -n1; then
echo "Z-Wave device found at /dev/ttyACM0, adding symlinking to /dev/zwave"
echo 'SUBSYSTEM=="tty", ATTRS{idVendor}=="0658", ATTRS{idProduct}=="0200", SYMLINK+="zwave"' >> $USB_SERIAL_RULES_FILE
else
echo "Z-Wave device not found!"
fi
# Find RfxTrx
if udevadm info -a -n /dev/ttyUSB0 | grep -Fq '{idProduct}=="6001"' | head -n1 && udevadm info -a -n /dev/ttyUSB0 | grep -Fq '{idVendor}=="0403"' | head -n1; then
echo "RfxTrx device found at /dev/ttyUSB0, adding symlinking to /dev/rfxtrx"
echo 'SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", SYMLINK+="rfxtrx"' >> $USB_SERIAL_RULES_FILE
elif udevadm info -a -n /dev/ttyUSB1 | grep -Fq '{idProduct}=="6001"' | head -n1 && udevadm info -a -n /dev/ttyUSB1 | grep -Fq '{idVendor}=="0403"' | head -n1; then
echo "RfxTrx device found at /dev/ttyUSB1, adding symlinking to /dev/rfxtrx"
echo 'SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", SYMLINK+="rfxtrx"' >> $USB_SERIAL_RULES_FILE
else
echo "RfxTrx device not found!"
fi
# Find conbee
if udevadm info -a -n /dev/ttyUSB0 | grep -Fq '{idProduct}=="6015"' | head -n1 && udevadm info -a -n /dev/ttyUSB0 | grep -Fq '{idVendor}=="0403"' | head -n1; then
echo "Conbee device found at /dev/ttyUSB0, adding symlinking to /dev/conbee"
echo 'SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6015", SYMLINK+="conbee"' >> $USB_SERIAL_RULES_FILE
elif udevadm info -a -n /dev/ttyUSB1 | grep -Fq '{idProduct}=="6015"' | head -n1 && udevadm info -a -n /dev/ttyUSB1 | grep -Fq '{idVendor}=="0403"' | head -n1; then
echo "Conbee device found at /dev/ttyUSB1, adding symlinking to /dev/conbee"
echo 'SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6015", SYMLINK+="conbee"' >> $USB_SERIAL_RULES_FILE
else
echo "Conbee device not found!"
fi
echo
echo "----------------------------------------"
echo
# Mount tmp dirs to tmpfs
echo "Mounting /var/log, /var/tmp and /tmp to tmpfs (RAM memory) to lower SD-card tear.."
append1 /etc/fstab "/var/log" "tmpfs /var/log tmpfs nodev,nosuid 0 0"
append1 /etc/fstab "/var/tmp" "tmpfs /var/tmp tmpfs nodev,nosuid 0 0"
append1 /etc/fstab "\s/tmp" "tmpfs /tmp tmpfs nodev,nosuid 0 0"
mount -a
echo
echo "----------------------------------------"
echo
# Enable UFW
echo "Setting up UFW to allow only SSH on port 22.."
apt install ufw -y
ufw allow ssh
ufw enable
echo
echo "----------------------------------------"
echo
# Setup complete, reboot
echo "Done. Settings take effect on next boot."
echo
echo "New user: $NEW_USER"
echo "New hostname: $NEW_HOSTNAME"
echo
echo "UFW is enabled and currently only SSH (port 22) is allowed."
echo
echo "Remove pi user manually! Use \"deluser -remove-home pi\" for removal of user and home directory."
echo
echo -n "REBOOT NOW? [y/N] "
read
if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
echo "Exiting without reboot."
exit 0
fi
echo "Reboot started..."
reboot
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment