Skip to content

Instantly share code, notes, and snippets.

@1rabbit
Last active June 4, 2018 18:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1rabbit/c7d7bce0e14124145040b350bfedaf32 to your computer and use it in GitHub Desktop.
Save 1rabbit/c7d7bce0e14124145040b350bfedaf32 to your computer and use it in GitHub Desktop.
AutoRPI 1.0 Automatic headless image maker for Raspbian Lite
#!/bin/sh
#Start with "source autorpi.sh"
#AutoRPI 1.0 Automatic headless image maker for Raspbian Lite
#2018 Designdesk.org
#Run as root for mount/unmount rights
#Options
basedir="$HOME/AutoRpi"
advanced=;username=;passhashed=;passwd=;wlanset=;wlanssid=;wlanpsk=;hostname=;sshkey=;layout=;resize=;extend=;sndledoff=;skip=;
###Optional automatic setup like this :### username="turtle"; passhashed=y ; passwd="\$1\$VaoXRUzMB/VyxP6VnT7Fbs9y9LjXEFFDd" ; wlanssid="hAfpFKX" ; wlanpsk="HMhMYzyIpy5u0mr0SKBCxVb6lN4lc3NaptfktNNAHv+geUcmQgb" ; hostname="tinycomputer" ; sshkey="ssh-rsa /84r14PMuU7Xv4PLeAiHsQzVXc7zSInsZuVBFIyhJGHU+iC7fITU3kzLvMG2ZzB6A/Zp/+lJu+6IjSGcFFo4vx84ZJ2Rj9OUoFWjE2qMlGwKmr== user@yourpc" ; layout="us" ; resize=y ; extend=y ; sndledoff=y ; advanced=y ; wlanset=y ; skip=y
#Prompts
echo -e "\nAutoRpi 1.0 Automatic headless image maker for Raspbian Lite\n2017 Designdesk.org"
if [ "$skip" != "y" ];then echo -e "\n\nFields marked with * are mandatory\n" ;fi
if [ -z "$advanced" ];then read -p " Advanced Mode y/N : " advanced ;fi
#Basic
if [ -z "$username" ];then read -p "* Username : " username ; if [ -z $username ];then return;fi;fi
if [ -z "$passwd" ];then read -p "* Password : " passwd ; if [ -z $passwd ];then return;fi;fi
if [ -z "$wlanset" ];then read -p " Setup wireless lan network Y/n : " wlanset ; if [ -z $wlanset ];then wlanset=y ;fi;fi
if [ "$wlanset" = "y" ];then if [ -z "$wlanssid" ];then read -p "* Wireless network name (SSID) : " wlanssid ;fi
if [ -z "$wlanpsk" ];then read -p "* Wireless network passphrase (PSK) : " wlanpsk ;fi;fi
#Advanced
if [ "$advanced" = "y" ];then
if [ -z "$hostname" ];then read -p "* Hostname for your raspberry pi : " hostname ;fi
if [ -z "$sshkey" ];then read -p " Enter your public ssh key : " sshkey ; if [ -z $sshkey ];then sshkey=n ;fi
if [ -z "$keyonly" ];then read -p " Restrict ssh to key only login y/N : " keyonly ;fi;fi
if [ -z "$layout" ];then read -p " Keyboard Layout choice (default=uk) : " layout ; if [ -z $layout ];then layout=uk ;fi;fi
if [ -z "$resize" ];then read -p " Disable Auto-Resize y/N : " resize
if [ -z "$extend" ];then read -p " Extend system partition by 1go Y/n : " extend ; if [ -z $extend ];then extend=y ;fi;fi;fi
if [ -z "$sndledoff" ];then read -p " Disable Audio and Led y/N : " sndledoff ;fi
else hostname=raspberrypi ;fi; imgname=`echo $hostname".img"`
#Creating directories
mkdir -p $basedir/0/ ; if [ -z "$skip" ];then mkdir -p $basedir/$hostname/etc ; mkdir -p $basedir/$hostname/home ; mkdir -p $basedir/$hostname/root ;else mkdir -p $basedir/$hostname ;fi; mkdir -p $basedir/mnt/boot ; mkdir -p $basedir/mnt/system ; cd $basedir/ ; echo
if [ -z "$skip" ];then read -p "You can add config files / personal files to the etc, home and root directories in : $basedir/$hostname/ before you continue [OK]";fi
#Downloading last raspbian, install pigz for faster decompression and unpack zip
if [ ! -f $basedir/raspbian_lite.zip ]; then echo -e "\nDownloading latest raspbian image and installing pigz for faster decompression\n" ; wget -O raspbian_lite.zip https://downloads.raspberrypi.org/raspbian_lite_latest ; apt-get -y install pigz ; else echo -e "Using previously downloaded raspbian image, make sure its up to date" ;fi
echo -e "\nExtracting ... This can take a few minutes\n" ; pigz -dk $basedir/raspbian_lite.zip && mv $basedir/raspbian_lite $basedir/$imgname
#Mount boot/system partition
mount -o loop,offset=$((8192 * 512)) $basedir/$imgname $basedir/mnt/boot
mount -o loop,offset=$((98304 * 512)) $basedir/$imgname $basedir/mnt/system
#System settings
#Changing default hostname
echo $hostname > $basedir/mnt/system/etc/hostname && sed -i '/127.0.1.1/d' $basedir/mnt/system/etc/hosts && echo "127.0.1.1 $hostname" >> $basedir/mnt/system/etc/hosts
#Changing username/Apply permissions
mv $basedir/mnt/system/home/pi $basedir/mnt/system/home/$username && sed -i '/exit 0/d' $basedir/mnt/system/etc/rc.local
echo "cd /etc && sed -i."\$"(date +'%y%m%d_%H%M%S') 's/"\\"bpi"\\"b/"$username"/g' passwd group shadow gshadow sudoers systemd/system/autologin@.service sudoers.d/* && chown -R "$username":"$username" /home/"$username"/ && chmod 700 /home/"$username"/.ssh/ && chmod 600 /home/"$username"/.ssh/* && sed -i '/#TODEL/d' /etc/rc.local" >> $basedir/mnt/system/etc/rc.local
echo "exit 0" >> $basedir/mnt/system/etc/rc.local
#Changing password
if [ "$passhashed" != "y" ];then passwd=`openssl passwd -1 "$passwd"` ;fi; sed -i '/pi:/d' $basedir/mnt/system/etc/shadow && echo "$username"":""$passwd"":17228:0:99999:7:::" >> $basedir/mnt/system/etc/shadow
#Changing keyboard layout
sed -i '/XKBLAYOUT=/d' $basedir/mnt/system/etc/default/keyboard && echo "XKBLAYOUT=""\""$layout"\"" >> $basedir/mnt/system/etc/default/keyboard
#Setup wireless lan network
if [ "$wlanset" = "y" ];then sed -i -- 's/allow-hotplug wlan0/auto wlan0/g' $basedir/mnt/system/etc/network/interfaces && sed -i -- 's/wlan0 inet manual/wlan0 inet dhcp/g' $basedir/mnt/system/etc/network/interfaces
echo -e "\nnetwork={\nssid="\"""$wlanssid""\""\npsk="\"""$wlanpsk""\""\n}" >> $basedir/mnt/system/etc/wpa_supplicant/wpa_supplicant.conf ;fi
#Add public ssh key
if [ "$sshkey" != "n" ];then mkdir -p $basedir/mnt/system/home/$username/.ssh/ && echo "$sshkey" >> $basedir/mnt/system/home/$username/.ssh/authorized_keys ;fi
#Restrict ssh to unique non-root user/key only login
if [ "$keyonly" = "y" ];then sed -i -- 's/PermitRootLogin without-password/PermitRootLogin no/g' $basedir/mnt/system/etc/ssh/sshd_config ; sed -i -- 's/#PasswordAuthentication yes/PasswordAuthentication no/g' $basedir/mnt/system/etc/ssh/sshd_config ; echo -e "AllowUsers "$username"\nUseDNS no" >> $basedir/mnt/system/etc/ssh/sshd_config ;fi
#Boot settings
#Enable ssh
echo > $basedir/mnt/boot/ssh
#Auto-resize off
if [ "$resize" = "y" ];then sed -i -- 's/ init=\/usr\/lib\/raspi-config\/init_resize.sh//g' $basedir/mnt/boot/cmdline.txt ;fi
#Audio and led off
if [ "$sndledoff" = "y" ];then sed -i '/dtparam=audio/d' $basedir/mnt/boot/config.txt && echo -e "dtparam=audio=off\n\n# Disable Led\ndtparam=act_led_trigger=none\ndtparam=act_led_activelow=on" >> $basedir/mnt/boot/config.txt ;fi
#/Boot settings
#Copy directories with config files / personal files to image
cp -r $basedir/0/. $basedir/mnt/system
cp -r $basedir/$hostname/. $basedir/mnt/system
#Unmounting image
cd && umount $basedir/mnt/boot
umount $basedir/mnt/system
#Extend size of the partition - Add 1go of space to the image
if [ "$extend" = "y" ];then dd if=/dev/zero bs=1M count=1024 >> $basedir/$imgname && parted $basedir/$imgname resizepart 2 100% && fdisk -lu $basedir/$imgname ;fi
echo -e "Image processing finished. Ready to write to memory card\n\n$basedir/$imgname"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment