40 lines
1.1 KiB
Shell
Executable file
40 lines
1.1 KiB
Shell
Executable file
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
[[ $# -eq 2 ]] || {
|
|
echo "usage: clear-native-recovery-beacon.sh <disk> <clear-template>" >&2
|
|
exit 64
|
|
}
|
|
|
|
disk=$1
|
|
clear_template=$2
|
|
[[ -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=68 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=68 count=2 \
|
|
conv=notrunc,fsync status=none
|
|
dd if="${disk}" of="${readback}" bs=512 skip=68 count=2 status=none
|
|
cmp "${readback}" "${clear_template}"
|
|
echo "GUANGHU_NATIVE_RECOVERY_BEACON_CLEARED"
|