2026-08-03 10:04:41 +08:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
2026-08-03 21:22:53 +08:00
|
|
|
[[ $# -ge 2 && $# -le 3 ]] || {
|
|
|
|
|
echo "usage: clear-native-recovery-beacon.sh <disk> <clear-template> [beacon-lba]" >&2
|
2026-08-03 10:04:41 +08:00
|
|
|
exit 64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disk=$1
|
|
|
|
|
clear_template=$2
|
2026-08-03 21:22:53 +08:00
|
|
|
beacon_lba=${3:-68}
|
|
|
|
|
[[ ${beacon_lba} =~ ^[0-9]+$ ]]
|
2026-08-03 10:04:41 +08:00
|
|
|
[[ -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
|
2026-08-03 21:22:53 +08:00
|
|
|
dd if="${disk}" of="${readback}" bs=512 skip="${beacon_lba}" count=2 status=none
|
2026-08-03 10:04:41 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2026-08-03 21:22:53 +08:00
|
|
|
dd if="${clear_template}" of="${disk}" bs=512 seek="${beacon_lba}" count=2 \
|
2026-08-03 10:04:41 +08:00
|
|
|
conv=notrunc,fsync status=none
|
2026-08-03 21:22:53 +08:00
|
|
|
dd if="${disk}" of="${readback}" bs=512 skip="${beacon_lba}" count=2 status=none
|
2026-08-03 10:04:41 +08:00
|
|
|
cmp "${readback}" "${clear_template}"
|
|
|
|
|
echo "GUANGHU_NATIVE_RECOVERY_BEACON_CLEARED"
|