#cloud-config # For the boot partition of a Raspberry Pi OS card, next to the user-data # that Raspberry Pi Imager writes (cloud-init's NoCloud datasource reads # both). It carries zerodesk-firstboot.sh: cloud-init writes it to # /var/lib/zerodesk-firstboot, which needs no network, and runs it once # (from /var/lib/cloud/scripts/per-once: Imager's user-data may have a # runcmd of its own, which would replace one here). It reads zerodesk.txt # from the same partition, deletes it and installs ZeroDesk; when that # cannot finish (no network yet, for example) it says so in # /var/log/zerodesk-firstboot.log, and this runs it again: # sudo sh /var/lib/zerodesk-firstboot/zerodesk-firstboot.sh # Made by packaging/raspberrypi/make-vendor-data.sh; do not edit by hand. write_files: - path: /var/lib/zerodesk-firstboot/zerodesk-firstboot.sh owner: root:root permissions: '0700' content: | #!/bin/sh # First boot of a Raspberry Pi card prepared on one computer: Raspberry Pi # Imager wrote Raspberry Pi OS (user and Wi-Fi from its OS customisation), # and zerodesk.cc's "Add a device" gave a zerodesk.txt and a vendor-data # that were copied onto the boot partition. This moves the values of # zerodesk.txt into a folder only root can read, deletes zerodesk.txt, waits # for the network and installs ZeroDesk with those values. The zerodesk.cc # tab that made the file signs in by itself once the device posts its claim. # # vendor-data carries this script (packaging/raspberrypi/make-vendor-data.sh # puts it there): cloud-init writes it to /var/lib/zerodesk-firstboot before # anything needs the network, then runs it once. So it is there to run # again also when the first boot had no network at all. # # zerodesk.txt: key=value lines, '#' comments. Keys (all optional): # claim, tailscale_authkey, cloudflare_tunnel_token, # https (zerodesk|off|domain|server), domain, cloudflare_dns_token, cert_server, # user (the account ZeroDesk runs as; default: the one Imager created) # Values never reach a command line or the log. The log gets install.sh's # own messages ("zerodesk: …" lines on its standard error, which never carry # a value) and, from its result file, only its status, error and # reachability: never the one-time sign-in link or code. # # When the install does not finish (no network yet, say), what is left to do # stays in /var/lib/zerodesk-firstboot (0700, root's) and running it again # does that, and only that: # sudo sh /var/lib/zerodesk-firstboot/zerodesk-firstboot.sh # options the values not stored for the account yet: a value install.sh # stored is taken out, so it is never stored twice (a claim the # zerodesk.cc tab has used is never armed again) # tunnel a Cloudflare Tunnel token is stored for the account and # cloudflared is not known to be installed: until it is, every # run installs it once install.sh has nothing left to do (the # token itself is no longer in options) # stage "cloudflared" once everything but cloudflared is installed: # a run again only installs it, without waiting for GitHub # inflight there while install.sh runs, and until what it stored is taken # out of options; found at the start, the run before stopped # part-way (a power cut, a full disk), so the claim may be stored # and used already: it is taken out before anything else runs, # and nothing runs while that fails # Each of these is written before what depends on it is removed, and a stage # only once what install.sh stored is out of options. The folder is deleted # once nothing is left to do. # # Run by cloud-init (vendor-data) or by hand: sudo sh zerodesk-firstboot.sh # (ZD_BOOT_TXT, ZD_FIRSTBOOT_LOG, ZD_FIRSTBOOT_STATE, ZD_FIRSTBOOT_TRIES, # ZD_FIRSTBOOT_BIN and ZD_RELEASE_URL only exist for # packaging/linux/tests/hooks_test.sh.) set -u umask 077 ZD_RELEASES=${ZD_RELEASE_URL:-https://github.com/richardhxwang/ZeroDesk/releases/download/v0.5.0-beta} LOG=${ZD_FIRSTBOOT_LOG:-/var/log/zerodesk-firstboot.log} STATE=${ZD_FIRSTBOOT_STATE:-/var/lib/zerodesk-firstboot} TRIES=${ZD_FIRSTBOOT_TRIES:-60} log() { echo "zerodesk-firstboot: $*" | tee -a "$LOG" >&2; } again="run it again later: sudo sh $STATE/zerodesk-firstboot.sh" KEYS="claim tailscale_authkey cloudflare_tunnel_token https domain cloudflare_dns_token cert_server" txt="" for f in ${ZD_BOOT_TXT:-/boot/firmware/zerodesk.txt /boot/zerodesk.txt}; do [ -f "$f" ] && { txt=$f; break; } done opts=$STATE/options mkdir -p "$STATE" && chmod 0700 "$STATE" || { log "cannot create $STATE"; exit 1; } if [ -n "$txt" ]; then # only the known keys, without CR (the file may come from Windows) new=$(mktemp "$STATE/options.XXXXXX") || { log "cannot write in $STATE"; exit 1; } tr -d '\r' < "$txt" | while IFS= read -r line; do case "$line" in ''|'#'*) continue ;; esac k=${line%%=*}; v=${line#*=} k=$(echo "$k" | tr -d ' \t' | tr A-Z a-z) case " $KEYS user " in *" $k "*) printf '%s=%s\n' "$k" "$v" >> "$new" ;; esac done mv -f "$new" "$opts" || { rm -f "$new"; log "cannot write $opts"; exit 1; } # a new zerodesk.txt starts over rm -f "$STATE/stage" "$STATE/inflight" # the values are in root's folder now; the copy on the FAT boot partition # (readable by anyone with the card) goes rm -f "$txt" sync log "moved the values of $txt to $STATE and removed it" elif [ -f "$opts" ] || [ -f "$STATE/stage" ] || [ -f "$STATE/tunnel" ]; then log "using what is kept in $STATE from an earlier run" else log "no zerodesk.txt on the boot partition; nothing to do" exit 0 fi # this script next to them, for running it again (vendor-data put it there # already; a copy run by hand puts itself there) if [ -f "$0" ] && [ "$0" != "$STATE/zerodesk-firstboot.sh" ]; then cp "$0" "$STATE/zerodesk-firstboot.sh" 2>/dev/null fi # drop : takes a value out of the kept ones, on disk (synced) before # it returns 0; 1, with options as they were, when that cannot be done (a # full disk) drop() { [ -f "$opts" ] || return 0 grep -q "^$1=" "$opts" || return 0 d=$(mktemp "$STATE/options.XXXXXX") || return 1 grep -v "^$1=" "$opts" > "$d" [ $? -le 1 ] || { rm -f "$d"; return 1; } # 1: nothing else left in it mv -f "$d" "$opts" || { rm -f "$d"; return 1; } sync } # mark [content]: a state file, on disk before it returns 0 mark() { m=$(mktemp "$STATE/$1.XXXXXX") || return 1 { [ -z "${2:-}" ] || printf '%s\n' "$2"; } > "$m" && mv -f "$m" "$STATE/$1" || { rm -f "$m"; return 1; } sync } # record_stored : what install.sh stored, taken out of options; a # stored tunnel token is noted (tunnel) before it goes. 1 when any of it # could not be done: inflight then stays, so the claim is taken out before # the next run passes anything record_stored() { rs_ok=0 for k in $1; do case " $KEYS " in *" $k "*) ;; *) continue ;; esac if [ "$k" = cloudflare_tunnel_token ] && ! mark tunnel; then rs_ok=1; continue # kept in options: stored again next time fi drop "$k" || rs_ok=1 done return $rs_ok } # cloudflared_step: the agent installs cloudflared (as root; 0 also when it # is there already), and starts the tunnel by itself once it is there. # Exits: 0 done (the folder deleted), 9 not installed (stage kept) cloudflared_step() { bin=${ZD_FIRSTBOOT_BIN:-} [ -n "$bin" ] || for b in /usr/local/bin/zerodesk /usr/bin/zerodesk; do [ -x "$b" ] && { bin=$b; break; }; done [ -n "$bin" ] || { log "ZeroDesk is not installed any more; remove $STATE/stage and $STATE/tunnel to install it again"; exit 1; } err=$(mktemp "$STATE/err.XXXXXX") || { log "cannot write in $STATE; $again"; exit 9; } log "installing cloudflared for the Cloudflare Tunnel (everything else is installed)" "$bin" -install-cloudflared > "$err" 2>&1 c_rc=$? keep_log "$err" if [ "$c_rc" = 0 ]; then rm -rf "$STATE" log "cloudflared is installed; the Cloudflare Tunnel starts within a few minutes" exit "${1:-0}" fi log "cloudflared could not be installed (status $c_rc); $again" exit 9 } # keep_log : the lines of install.sh's (or the agent's) standard error # that are its own messages, into the log; any /q/ link in them removed keep_log() { grep '^zerodesk: ' "$1" 2>/dev/null | sed -e 's#/q/[A-Za-z0-9_-]*#/q/(removed)#g' >> "$LOG" } inst="" run="" err="" res="" trap 'rm -f "$inst" "$run" "$err" "$res"' EXIT if [ -f "$STATE/inflight" ]; then if [ -f "$opts" ] && grep -q '^claim=' "$opts"; then drop claim || { log "the run before stopped part-way, and the claim it may have stored cannot be taken out of $STATE (is the disk full?); nothing is run until it can: $again"; exit 1; } log "the run before stopped part-way, so the claim may be in use already and is not stored again; the zerodesk.cc tab may not sign in by itself: sign in with sudo zerodesk -signin-link" fi rm -f "$STATE/inflight" || exit 1 fi # only cloudflared is left: the agent installs it from Cloudflare, so # GitHub need not be reachable [ "$(cat "$STATE/stage" 2>/dev/null)" = cloudflared ] && cloudflared_step # the network (Wi-Fi from Imager) may take a while on the first boot: up to # TRIES tries, each up to 10 seconds, 10 seconds apart (about 20 minutes) i=0 t0=$(date +%s) until curl -fsS -o /dev/null --max-time 10 "$ZD_RELEASES/install.sh" 2>/dev/null; do i=$((i + 1)) if [ "$i" -ge "$TRIES" ]; then m=$(( ($(date +%s) - t0 + 59) / 60 )); [ "$m" -ge 1 ] || m=1 log "no network after $i tries ($m min); $again"; exit 1 fi sleep 10 done err=$(mktemp "$STATE/err.XXXXXX") || exit 1 # install.sh gets the values except user= (a flag of its own) in a file of its own user=$(sed -n 's/^user=//p' "$opts" | tail -n 1) run=$(mktemp "$STATE/run.XXXXXX") && grep -v '^user=' "$opts" > "$run" [ -n "$run" ] && [ -f "$run" ] || { log "cannot write in $STATE"; exit 1; } res=$(mktemp "$STATE/result.XXXXXX") || exit 1 inst=$(mktemp "${TMPDIR:-/tmp}/zerodesk-install.XXXXXX") || exit 1 curl -fsSL --retry 3 -o "$inst" "$ZD_RELEASES/install.sh" || { log "download failed; $again"; exit 1; } : > "$STATE/inflight" && sync || { log "cannot write in $STATE (is the disk full?); $again"; exit 1; } # its standard output (the sign-in lines, which sign in) goes nowhere sh "$inst" --yes --start --options-file "$run" --result-file "$res" ${user:+--user "$user"} > /dev/null 2> "$err" rc=$? keep_log "$err" r_get() { sed -n "s/^$1=//p" "$res" | tail -n 1; } status=$(r_get ZERODESK_STATUS) code=$(r_get ZERODESK_ERROR) reach=$(r_get ZERODESK_REACHABLE) stored=$(r_get ZERODESK_STORED) case "$status" in ok|error) ;; *) status="" ;; esac case "$code" in *[!a-z_]*) code=other ;; esac case "$reach" in yes|no|unknown) ;; *) reach=unknown ;; esac recorded="" if [ -n "$status" ]; then log "install.sh: status=$status error=${code:-none} reachable=$reach" if record_stored "$stored"; then rm -f "$STATE/inflight" && recorded=1 else log "what install.sh stored cannot all be taken out of $STATE (is the disk full?); the claim is taken out before the next run passes anything" fi elif [ "$rc" = 1 ] || [ "$rc" = 2 ] || [ "$rc" = 3 ]; then # no result, from a step before anything is stored rm -f "$STATE/inflight" && recorded=1 fi # (no result otherwise: inflight stays, and the claim is not passed again) case "$rc" in 0|6|7|9) # installed and running with every value (6: HTTPS came later than # the install waited; 7: no one-time link, never shown here) case "$rc" in 6|0) log "install finished with status $rc (see $LOG)" ;; 7) log "install finished with status 7: ZeroDesk runs, but made no sign-in link; the zerodesk.cc tab signs in with the claim, or: sudo zerodesk -signin-link" ;; 9) log "install finished with status 9: ZeroDesk runs, only cloudflared for the Cloudflare Tunnel is not installed yet" ;; esac if [ ! -f "$STATE/tunnel" ] && [ "$rc" != 9 ]; then rm -rf "$STATE"; exit "$rc" fi # cloudflared is left (a token stored now or by an earlier run); # install.sh is not needed again once what it stored is recorded if [ -n "$recorded" ] && mark stage cloudflared; then rm -f "$opts" [ "$rc" = 9 ] && { log "$again"; exit 9; } cloudflared_step "$rc" fi log "cloudflared for the Cloudflare Tunnel is not installed yet; $again" exit 9 ;; *) log "install finished with status $rc (see $LOG); what is left stays in $STATE: $again" ;; esac exit "$rc" - path: /var/lib/cloud/scripts/per-once/zerodesk-firstboot owner: root:root permissions: '0700' content: | #!/bin/sh exec sh /var/lib/zerodesk-firstboot/zerodesk-firstboot.sh