Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inithook loop #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions run
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,45 @@
TERM=${TERM:-linux}

INITHOOKS_DEFAULT=/etc/default/inithooks
. $INITHOOKS_DEFAULT
. "$INITHOOKS_DEFAULT"

TKLINFO=/var/lib/turnkey-info

unset PID INITHOOKS_LOGFILE

REDIRECT_OUTPUT=$(echo $REDIRECT_OUTPUT | tr [A-Z] [a-z])
REDIRECT_OUTPUT="$(echo "$REDIRECT_OUTPUT" | tr '[:upper:]' '[:lower:]')"

everyboot_ran() {
# read lastboot time
lastboot_time="$(date '+%s' -d "$(cut -f1 -d. /proc/uptime) seconds ago")"
if [[ -f /run/inithooks/everyboot_ran ]]; then
# time stamp written
prev_lastboot_time="$(</run/inithooks/everyboot_ran)"
if [[ "$lastboot_time" -eq "$prev_lastboot_time" ]]; then
# time stamp of last boot is the same as our last boot, everyboot
# scripts already ran
return 0
else
# time stamp of last boot is different from our last boot, everyboot
# scripts not already ran
echo "$lastboot_time" > /run/inithooks/everyboot_ran
return 1
fi
else
# no time stamp written, haven't run everyboot scripts since last boot
mkdir -p /run/inithooks
echo "$lastboot_time" > /run/inithooks/everyboot_ran
return 1
fi
}

log() {
# log to journal as well as $INITHOOKS_LOGFILE
LEVEL=$1 # err|warn|info|debug
shift
logger -t inithooks -p $LEVEL "$@"
logger -t inithooks -p "$LEVEL" "$@"
[[ -n "$INITHOOKS_LOGFILE" ]] \
&& echo "${LEVEL^^}: $@" >> "$INITHOOKS_LOGFILE"
&& echo "${LEVEL^^}: $*" >> "$INITHOOKS_LOGFILE"
}

if [[ "$REDIRECT_OUTPUT" == "true" ]]; then
Expand All @@ -35,17 +59,17 @@ if [[ "$REDIRECT_OUTPUT" == "true" ]]; then
if [[ ! -f "$TKLINFO/xen" ]]; then
TTY=$(cat /sys/devices/virtual/tty/tty0/active)
[[ -z $TTY ]] && TTY=console
tail -f $INITHOOKS_LOGFILE > /dev/$TTY &
tail -f $INITHOOKS_LOGFILE > /dev/"$TTY" &
PID="$!"
fi
fi

exec_scripts() {
script_dir=$1
[[ -d "$script_dir" ]] || return 0
for SCRIPT in $(find $script_dir -type f -or -type l | sort); do
[[ -e $INITHOOKS_CONF ]] && . $INITHOOKS_CONF
script=$(basename $SCRIPT)
for SCRIPT in $(find "$script_dir" -type f -or -type l | sort); do
[[ -e $INITHOOKS_CONF ]] && . "$INITHOOKS_CONF"
script="$(basename "$SCRIPT")"
if [[ ! -x "$SCRIPT" ]]; then
log warn "[$script] skipping"
continue
Expand Down Expand Up @@ -73,9 +97,12 @@ exec_scripts() {
export INITHOOKS_CONF=$INITHOOKS_CONF

if [[ "${RUN_FIRSTBOOT,,}" == "true" ]]; then
exec_scripts $INITHOOKS_PATH/firstboot.d
exec_scripts "$INITHOOKS_PATH/firstboot.d"
fi

if ! everyboot_ran; then
exec_scripts "$INITHOOKS_PATH/everyboot.d"
fi
exec_scripts $INITHOOKS_PATH/everyboot.d

if [[ -n "$PID" ]]; then
kill -9 $PID || true
Expand Down