Skip to content

Instantly share code, notes, and snippets.

@jaretburkett
Last active November 1, 2016 16:01
Show Gist options
  • Save jaretburkett/20701bd469f93e96f08a4ed8ddf305c5 to your computer and use it in GitHub Desktop.
Save jaretburkett/20701bd469f93e96f08a4ed8ddf305c5 to your computer and use it in GitHub Desktop.
Script to put pi in readonly mode
#!/bin/bash
#update apt-get
apt-get update
#replace syslog with busybox
apt-get install busybox-syslogd
dpkg --purge rsyslog
#remove problem packages
apt-get remove --purge logrotate dphys-swapfile
#disable filesystem check and swap
sed -i 's/rootfstype=ext4/rootfstype=ext4 fastboot noswap ro/g' /boot/cmdline.txt
#move some system files to tmp file system
rm -rf /var/lib/dhcp/ /var/run /var/spool /var/lock
ln -s /tmp /var/lib/dhcp; ln -s /tmp /var/run; ln -s /tmp /var/spool; ln -s /tmp /var/lock
#move lock file to tmp file system
sed -i 's/PIDFile=\/run\/dhcpcd.pid/PIDFile=\/var\/run\/dhcpcd.pid/g' /etc/systemd/system/dhcpcd5
#move random seed file to tmp system
rm /var/lib/systemd/random-seed
ln -s /tmp/random-seed /var/lib/systemd/random-seed
#create random seed file on boot
sed -i '/RemainAfterExit=yes/a ExecStartPre=/bin/echo "" >\/tmp\/random-seed' /lib/systemd/system/systemd-random-seed.service
#reload system daemon
systemctl daemon-reload
#setup internal clock
apt-get install ntp
sed -i '/if (command -v fake-hwclock >\/dev\/null 2>&1) ; then/a mount -o remount,rw \/' /etc/cron.hourly/fake-hwclock
sed -i '/fake-hwclock save/a mount -o remount,ro \/' /etc/cron.hourly/fake-hwclock
sed -i 's/driftfile \/var\/lib\/ntp\/ntp.drift/driftfile \/var\/tmp\/ntp.drift/g' /etc/ntp.conf
#remove some startup scripts
insserv -r bootlogs; insserv -r console-setup
#tell the filesystem we are in read only mode
sed -i 's/vfat defaults/vfat defaults,ro/g' /etc/fstab
sed -i 's/ext4 defaults,noatime/ext4 defaults,noatime,ro/g' /etc/fstab
echo 'tmpfs /tmp tmpfs nosuid,nodev 0 0' >> /etc/fstab
echo 'tmpfs /var/log tmpfs nosuid,nodev 0 0' >> /etc/fstab
echo 'tmpfs /var/tmp tmpfs nosuid,nodev 0 0' >> /etc/fstab
#add fancy commands and prompt
echo '# set variable identifying the filesystem you work in (used in the prompt below)' >> /etc/bash.bashrc
echo 'set_bash_prompt(){' >> /etc/bash.bashrc
echo ' fs_mode=$(mount | sed -n -e "s/^\/dev\/.* on \/ .*(\(r[w|o]\).*/\1/p")' >> /etc/bash.bashrc
echo " PS1='\\[\\033[01;32m\\]\\u@\\h\${fs_mode:+(\$fs_mode)}\\[\\033[00m\\]:\\[\\033[01;34m\\]\\w\\[\\033[00m\\]\\$ '" >> /etc/bash.bashrc
echo '}' >> /etc/bash.bashrc
echo "alias ro='sudo mount -o remount,ro / ; sudo mount -o remount,ro /boot'" >> /etc/bash.bashrc
echo "alias rw='sudo mount -o remount,rw / ; sudo mount -o remount,rw /boot'" >> /etc/bash.bashrc
echo '# setup fancy prompt' >> /etc/bash.bashrc
echo 'PROMPT_COMMAND=set_bash_prompt' >> /etc/bash.bashrc
# for logout
echo 'mount -o remount,rw /' >> /etc/bash.bash_logout
echo 'history -a' >> /etc/bash.bash_logout
echo 'fake-hwclock save' >> /etc/bash.bash_logout
echo 'mount -o remount,ro /' >> /etc/bash.bash_logout
echo 'mount -o remount,ro /boot' >> /etc/bash.bash_logout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment