#!/bin/sh # Set the time zone for this boot from the kernel command line's # brintos.tz=, the browser's zone. The image links /etc/localtime to # /run/wabuntu/localtime and /etc/timezone to /run/wabuntu/timezone, so a # read-only root takes the zone too. While /etc/localtime is that link the zone # is automatic. Once the user picks one, timedated replaces the link, and the # choice is left alone. A zone that is malformed or not installed falls back to # UTC with one line on stderr. Run once per boot, after /run is mounted. # # The paths can be moved, so that a test can run the step on scratch files. CMDLINE=${BRINTOS_TZ_CMDLINE:-/proc/cmdline} ETC=${BRINTOS_TZ_ETC:-/etc} RUN=${BRINTOS_TZ_RUN:-/run/wabuntu} ZONEINFO=${BRINTOS_TZ_ZONEINFO:-/usr/share/zoneinfo} # Shell builtins wherever they will do: every program started here costs about # 0.1 s. # The last brintos.tz= token wins, as on any kernel command line. zone= if read -r _line 2>/dev/null <"$CMDLINE"; then set -f for _t in $_line; do case $_t in brintos.tz=*) zone=${_t#brintos.tz=} ;; esac done set +f fi # A name tzfile(5) can look up: components of letters, digits, _, + and -, # joined by single slashes; nothing that could leave the zoneinfo tree. valid_name() { case $1 in '' | /* | */ | *//* | *[!A-Za-z0-9_+/-]*) return 1 ;; esac return 0 } # A compiled zone, not a directory or one of the tables beside the zones. is_tzif() { [ -f "$ZONEINFO/$1" ] || return 1 _magic= read -r -n 4 _magic <"$ZONEINFO/$1" 2>/dev/null [ "$_magic" = TZif ] } [ -d "$RUN" ] || mkdir -p "$RUN" || exit 1 _link=$(readlink "$ETC/localtime" 2>/dev/null) case $_link in /*) _to=$_link ;; *) _to=$ETC/$_link ;; esac # The image build rewrites every absolute link as a relative one, so the link # into $RUN is matched by where it leads, not by how it is spelled. if [ -z "$_link" ] || [ "${_to##*/}" != localtime ] || ! [ "${_to%/*}" -ef "$RUN" ]; then # The user's choice. Name it in /etc/timezone too, for the readers that # take the zone's name from there. case $_link in */zoneinfo/*) printf '%s\n' "${_link#*/zoneinfo/}" >"$RUN/timezone" ;; esac exit 0 fi why= if [ -z "$zone" ]; then why="no brintos.tz= on the kernel command line" elif ! valid_name "$zone"; then why="brintos.tz=$zone is not a zone name" elif ! is_tzif "$zone"; then why="$ZONEINFO/$zone is not installed" fi if [ -n "$why" ]; then zone=UTC # The first boot of a machine opened from no browser carries no token; # that is not worth a line. case $why in no\ *) ;; *) echo "wabuntu-timezone: $why; using UTC" >&2 ;; esac fi ln -sfn "$ZONEINFO/$zone" "$RUN/localtime" || exit 1 printf '%s\n' "$zone" >"$RUN/timezone"