Last active
June 16, 2021 16:34
-
-
Save kity-linuxero/1488854cca60eb2ac785ce5f510a1841 to your computer and use it in GitHub Desktop.
pihole-FTL to RAM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# Copy this archive to /etc/init.d/pihole-FTL and later run | |
# chmod +x /etc/init.d/pihole-FTL | |
# systemctl daemon-reload | |
# | |
# Assuming /tmp is a tmpfs | |
# | |
### BEGIN INIT INFO | |
# Provides: pihole-FTL | |
# Required-Start: $remote_fs $syslog $network | |
# Required-Stop: $remote_fs $syslog $network | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: pihole-FTL daemon | |
# Description: Enable service provided by pihole-FTL daemon | |
### END INIT INFO | |
FTLUSER=pihole | |
PIDFILE=/run/pihole-FTL.pid | |
DBFILE=/etc/pihole/pihole-FTL.db | |
DBFILE_TMP=/tmp/pihole-FTL.db | |
is_running() { | |
pgrep -o "pihole-FTL" > /dev/null 2>&1 | |
} | |
# Start the service | |
start() { | |
if is_running; then | |
echo "pihole-FTL is already running" | |
else | |
# Touch files to ensure they exist (create if non-existing, preserve if existing) | |
touch /var/log/pihole-FTL.log /var/log/pihole.log | |
touch /run/pihole-FTL.pid /run/pihole-FTL.port | |
touch /etc/pihole/dhcp.leases | |
mkdir -p /run/pihole | |
mkdir -p /var/log/pihole | |
chown pihole:pihole /run/pihole /var/log/pihole | |
# Remove possible leftovers from previous pihole-FTL processes | |
rm -f /dev/shm/FTL-* 2> /dev/null | |
rm /run/pihole/FTL.sock 2> /dev/null | |
# Ensure that permissions are set so that pihole-FTL can edit all necessary files | |
chown pihole:pihole /run/pihole-FTL.pid /run/pihole-FTL.port | |
chown pihole:pihole /etc/pihole /etc/pihole/dhcp.leases 2> /dev/null | |
chown pihole:pihole /var/log/pihole-FTL.log /var/log/pihole.log | |
chmod 0644 /var/log/pihole-FTL.log /run/pihole-FTL.pid /run/pihole-FTL.port /var/log/pihole.log | |
# Chown database files to the user FTL runs as. We ignore errors as the files may not (yet) exist | |
chown pihole:pihole $DBFILE /etc/pihole/gravity.db 2> /dev/null | |
# Added by ME! ####### | |
if [ ! -f $DBFILE_TMP ]; then | |
cp $DBFILE $DBFILE_TMP | |
chown pihole:pihole $DBFILE_TMP | |
fi | |
###################### | |
if setcap CAP_NET_BIND_SERVICE,CAP_NET_RAW,CAP_NET_ADMIN,CAP_SYS_NICE+eip "$(which pihole-FTL)"; then | |
su -s /bin/sh -c "/usr/bin/pihole-FTL" "$FTLUSER" | |
else | |
echo "Warning: Starting pihole-FTL as root because setting capabilities is not supported on this system" | |
pihole-FTL | |
fi | |
echo | |
fi | |
} | |
# Stop the service | |
stop() { | |
if is_running; then | |
pkill -o pihole-FTL | |
for i in {1..5}; do | |
if ! is_running; then | |
break | |
fi | |
echo -n "." | |
sleep 1 | |
done | |
echo | |
if is_running; then | |
echo "Not stopped; may still be shutting down or shutdown may have failed, killing now" | |
pkill -o -9 pihole-FTL | |
exit 1 | |
else | |
cp -rf $DBFILE_TMP $DBFILE | |
rm -rf $DB_FILE_TMP | |
echo "Stopped" | |
fi | |
else | |
echo "Not running" | |
fi | |
echo | |
} | |
# Indicate the service status | |
status() { | |
if is_running; then | |
echo "[ ok ] pihole-FTL is running" | |
exit 0 | |
else | |
echo "[ ] pihole-FTL is not running" | |
exit 1 | |
fi | |
} | |
### main logic ### | |
case "$1" in | |
stop) | |
stop | |
;; | |
status) | |
status | |
;; | |
start|restart|reload|condrestart) | |
stop | |
start | |
;; | |
*) | |
echo $"Usage: $0 {start|stop|restart|reload|status}" | |
exit 1 | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment