#!/usr/bin/env bash set -euo pipefail [[ $# -ge 2 && $# -le 3 ]] || { echo "usage: clear-native-recovery-beacon.sh [beacon-lba]" >&2 exit 64 } disk=$1 clear_template=$2 beacon_lba=${3:-68} [[ ${beacon_lba} =~ ^[0-9]+$ ]] [[ -f ${clear_template} ]] [[ $(stat -c %s "${clear_template}" 2>/dev/null || stat -f %z "${clear_template}") -eq 1024 ]] if [[ -b ${disk} ]]; then [[ ${EUID} -eq 0 ]] [[ $(readlink -f "${disk}") == /dev/vda ]] elif [[ ! -f ${disk} ]]; then echo "recovery beacon target is neither /dev/vda nor a test disk" >&2 exit 65 fi readback=$(mktemp) trap 'rm -f "${readback}"' EXIT dd if="${disk}" of="${readback}" bs=512 skip="${beacon_lba}" count=2 status=none if cmp -s "${readback}" "${clear_template}"; then echo "GUANGHU_NATIVE_RECOVERY_BEACON_ALREADY_CLEAR" exit 0 fi if ! grep -aFq 'guanghu_recovery=ubuntu' "${readback}"; then echo "unknown data occupies the recovery beacon sectors" >&2 exit 65 fi dd if="${clear_template}" of="${disk}" bs=512 seek="${beacon_lba}" count=2 \ conv=notrunc,fsync status=none dd if="${disk}" of="${readback}" bs=512 skip="${beacon_lba}" count=2 status=none cmp "${readback}" "${clear_template}" echo "GUANGHU_NATIVE_RECOVERY_BEACON_CLEARED"