2023-02-16 00:48:53 +01:00
#!/bin/bash -e
2023-07-26 23:50:18 +02:00
if [ " ${ PH_VERBOSE :- 0 } " -gt 0 ] ; then
set -x
2023-02-16 00:48:53 +01:00
fi
2023-07-26 23:50:18 +02:00
trap stop TERM INT QUIT HUP ERR
2023-02-16 00:48:53 +01:00
2023-07-26 23:50:18 +02:00
start( ) {
2023-02-16 00:48:53 +01:00
2023-07-26 23:50:18 +02:00
# The below functions are all contained in bash_functions.sh
# shellcheck source=/dev/null
. /usr/bin/bash_functions.sh
2023-02-16 00:48:53 +01:00
2023-07-26 23:50:18 +02:00
echo " [i] Starting docker specific checks & setup for docker pihole/pihole"
2023-02-16 00:48:53 +01:00
2023-07-26 23:50:18 +02:00
# TODO:
#if [ ! -f /.piholeFirstBoot ] ; then
# echo " [i] Not first container startup so not running docker's setup, re-create container to run setup again"
#else
# regular_setup_functions
#fi
2023-07-23 19:54:21 +02:00
2023-07-26 23:50:18 +02:00
# Initial checks
# ===========================
2023-07-23 19:54:21 +02:00
2023-07-26 23:50:18 +02:00
# If PIHOLE_UID is set, modify the pihole user's id to match
if [ -n " ${ PIHOLE_UID } " ] ; then
currentId = $( id -u pihole)
if [ [ ${ currentId } -ne ${ PIHOLE_UID } ] ] ; then
echo " [i] Changing ID for user: pihole ( ${ currentId } => ${ PIHOLE_UID } ) "
usermod -o -u ${ PIHOLE_UID } pihole
else
echo " [i] ID for user pihole is already ${ PIHOLE_UID } , no need to change "
fi
2023-07-26 21:51:57 +02:00
fi
2023-02-16 00:48:53 +01:00
2023-07-26 23:50:18 +02:00
# If PIHOLE_GID is set, modify the pihole group's id to match
if [ -n " ${ PIHOLE_GID } " ] ; then
currentId = $( id -g pihole)
if [ [ ${ currentId } -ne ${ PIHOLE_GID } ] ] ; then
echo " [i] Changing ID for group: pihole ( ${ currentId } => ${ PIHOLE_GID } ) "
groupmod -o -g ${ PIHOLE_GID } pihole
else
echo " [i] ID for group pihole is already ${ PIHOLE_GID } , no need to change "
fi
fi
2023-02-16 00:48:53 +01:00
2023-07-26 23:50:18 +02:00
ensure_basic_configuration
2023-10-20 14:14:08 +02:00
setup_web_password
2023-02-16 00:48:53 +01:00
2023-10-20 14:14:08 +02:00
# [ -f /.piholeFirstBoot ] && rm /.piholeFirstBoot
2023-02-16 00:48:53 +01:00
2023-09-03 14:21:00 +02:00
# Install additional packages inside the container if requested
if [ -n " ${ ADDITIONAL_PACKAGES } " ] ; then
echo " [i] Fetching APK repository metadata."
if ! apk update; then
echo " [i] Failed to fetch APK repository metadata."
else
2023-09-25 21:00:22 +02:00
echo " [i] Installing additional packages: ${ ADDITIONAL_PACKAGES } . "
2023-09-03 14:21:00 +02:00
# shellcheck disable=SC2086
if ! apk add --no-cache ${ ADDITIONAL_PACKAGES } ; then
echo " [i] Failed to install additional packages."
fi
fi
echo ""
2023-07-26 23:50:18 +02:00
fi
2023-07-09 18:09:32 +02:00
2023-07-26 23:50:18 +02:00
# Remove possible leftovers from previous pihole-FTL processes
rm -f /dev/shm/FTL-* 2>/dev/null
rm -f /run/pihole/FTL.sock
2023-02-16 00:48:53 +01:00
2023-07-26 23:50:18 +02:00
# Start crond for scheduled scripts (logrotate, pihole flush, gravity update etc)
# Randomize gravity update time
sed -i " s/59 1 / $(( 1 + RANDOM % 58 )) $(( 3 + RANDOM % 2 )) / " /crontab.txt
# Randomize update checker time
sed -i " s/59 17/ $(( 1 + RANDOM % 58 )) $(( 12 + RANDOM % 8 )) / " /crontab.txt
/usr/bin/crontab /crontab.txt
2023-07-03 23:54:49 +02:00
2023-07-26 23:50:18 +02:00
/usr/sbin/crond
2023-02-16 00:48:53 +01:00
2023-08-09 23:13:28 +02:00
#migrate Database if needed:
2023-07-26 23:50:18 +02:00
gravityDBfile = $( getFTLConfigValue files.gravity)
2023-08-09 23:13:28 +02:00
if [ ! -f " ${ gravityDBfile } " ] ; then
if [ -n " ${ SKIPGRAVITYONBOOT } " ] ; then
2023-07-26 23:50:18 +02:00
echo " SKIPGRAVITYONBOOT is set, however ${ gravityDBfile } does not exist (Likely due to a fresh volume). This is a required file for Pi-hole to operate. "
echo " Ignoring SKIPGRAVITYONBOOT on this occasion."
2023-08-09 23:13:28 +02:00
unset SKIPGRAVITYONBOOT
2023-07-26 23:50:18 +02:00
fi
2023-08-09 23:13:28 +02:00
else
# TODO: Revisit this path if we move to a multistage build
source /etc/.pihole/advanced/Scripts/database_migration/gravity-db.sh
upgrade_gravityDB " ${ gravityDBfile } " "/etc/pihole"
fi
if [ -n " ${ SKIPGRAVITYONBOOT } " ] ; then
2023-09-03 14:02:50 +02:00
echo " [i] Skipping Gravity Database Update."
2023-08-09 23:13:28 +02:00
else
pihole -g
fi
2023-07-26 23:50:18 +02:00
pihole updatechecker
2023-10-20 14:14:08 +02:00
echo " [i] Docker start setup complete"
echo ""
echo " [i] pihole-FTL ( $FTL_CMD ) will be started as ${ DNSMASQ_USER } "
echo ""
2023-07-26 23:50:18 +02:00
# Start pihole-FTL
2023-10-20 14:14:08 +02:00
fix_capabilities
2023-07-26 23:50:18 +02:00
sh /opt/pihole/pihole-FTL-prestart.sh
capsh --user= $DNSMASQ_USER --keep= 1 -- -c " /usr/bin/pihole-FTL $FTL_CMD >/dev/null " &
2023-09-03 14:02:50 +02:00
if [ " ${ TAIL_FTL_LOG :- 1 } " -eq 1 ] ; then
2023-09-10 21:42:10 +02:00
tail -f /var/log/pihole/FTL.log &
2023-09-03 14:02:50 +02:00
else
echo " [i] FTL log output is disabled. Remove the Environment variable TAIL_FTL_LOG, or set it to 1 to enable FTL log output."
fi
2023-07-26 23:50:18 +02:00
# https://stackoverflow.com/a/49511035
wait $!
# Notes on above:
# - DNSMASQ_USER default of pihole is in Dockerfile & can be overwritten by runtime container env
# - /var/log/pihole/pihole*.log has FTL's output that no-daemon would normally print in FG too
# prevent duplicating it in docker logs by sending to dev null
}
stop( ) {
# Ensure pihole-FTL shuts down cleanly on SIGTERM/SIGINT
ftl_pid = $( pgrep pihole-FTL)
killall --signal 15 pihole-FTL
# Wait for pihole-FTL to exit
while test -d /proc/" ${ ftl_pid } " ; do
sleep 0.5
done
2023-08-10 00:31:39 +02:00
# If we are running pytest, keep the container alive for a little longer
# to allow the tests to complete
if [ [ ${ PYTEST } ] ] ; then
sleep 10
fi
2023-07-26 23:50:18 +02:00
exit
}
start