Skip to content

Commit

Permalink
auto-provisioning: improve behavior with power cuts
Browse files Browse the repository at this point in the history
Some users when auto-provisioning, power down the system once they see
the auto-provisioning service exit successfully. However, in some cases
this can cause the files created from the provisioning process to not be
fully written to disk, resulting in 0-byte files upon next boot.

Therefore, add an explicit sync after the files are written to improve
the behavior of the auto-provisioning in these cases.

Related-to: TOR-3743

Signed-off-by: Jeremias Cordoba <[email protected]>
  • Loading branch information
jsrc27 committed Jan 31, 2025
1 parent 86c8ef6 commit 46ceffb
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,33 @@ register_device() {
}

write_credentials() {
local temp_dir src_hash hash

log "Updating device credentials"

rm -Rf $SOTA_CRED_DIR && mkdir -p $SOTA_CRED_DIR
if ! unzip device.zip -d $SOTA_CRED_DIR >/dev/null; then
temp_dir="${SOTA_CRED_DIR}.tmp"
rm -Rf ${temp_dir} && mkdir -p ${temp_dir}
if ! unzip device.zip -d ${temp_dir} >/dev/null; then
exit_error "Failed extracting credentials file"
fi
sync

# Double-check hashes for extra validation
for file in $(unzip -l device.zip | awk '/-----/ {p = ++p % 2; next} p {print $NF}'); do
src_hash=$(unzip -p device.zip ${file} | sha256sum | cut -d ' ' -f1)
actual_hash=$(sha256sum ${temp_dir}/${file} | cut -d ' ' -f1)
if [ "$src_hash" != "$actual_hash" ]; then
rm -rf ${temp_dir}
exit_error "Hash mismatch on file $file"
fi
done

rm -rf $SOTA_BASE_DIR/sql.db
rm -rf ${SOTA_CRED_DIR}
mv -f ${temp_dir} ${SOTA_CRED_DIR}

rm -rf $CONFIG_FILE
rm -rf ${SOTA_BASE_DIR}/sql.db
rm -rf ${CONFIG_FILE}
sync
}

restart_services() {
Expand Down

0 comments on commit 46ceffb

Please sign in to comment.