Skip to content

Commit

Permalink
migrator: log directly to persistent storage
Browse files Browse the repository at this point in the history
The initramfs logs default to being written to tmpfs at /run. The
migrator hook also writes messages to this location, which is later
copied to persistent storage by resin-init-flasher if present. However,
any failure before resin-init-flasher, such as failing to copy
installation binaries to memory, will result in the file not being
written to persistent storage for debugging.

Write the log file to persistent storage directly in the migrator hook
in case of failure before resin-init-flasher, but also write to tmpfs
and copy to the new boot partition filesystem in the flasher script.

This ensures the migration logfile is always available on persistent
storage no matter where a failure occurs.

Change-type: patch
Signed-off-by: Joseph Kogut <[email protected]>
  • Loading branch information
jakogut committed Jul 18, 2024
1 parent 9e771e6 commit 6e8f57b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
26 changes: 25 additions & 1 deletion meta-balena-common/recipes-core/initrdscripts/files/migrate
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

# ensure logs are written and filesystems are unmounted before exiting
cleanup() {
disable_persistent_logging

sync
umount -a
}
Expand Down Expand Up @@ -117,8 +119,21 @@ cp_memsafe() {
wait $pid
}

disable_persistent_logging() {
# stop redirection and kill tee before unmounting
exec 1>&3 2>&4
# shellcheck disable=SC2009
kill "$(ps | grep "${logfile}" | awk 'NR==1{print $1}')"
}

# Main module function
migrate_run() {
# create a migration logfile in boot partition
logfile=migration_$(date +"%Y%m%dT%H%M").log
# duplicate stdout/stderr descriptors for later restoration
exec 3>&1 4>&2
exec &> >(tee "${FLASH_BOOT_MOUNT}/${logfile}")

# Find the raw image in the rootfs partition
image=$(find "${ROOTFS_DIR}" -xdev -type f -name "${BALENA_IMAGE}")
kernel_images=$(find "${ROOTFS_DIR}" -xdev -type f -name "@@KERNEL_IMAGETYPE@@*")
Expand Down Expand Up @@ -186,7 +201,16 @@ migrate_run() {
_config_json_name=$(basename "${CONFIG_PATH}")
cp "${FLASH_BOOT_MOUNT}/${_config_json_name}" "${CONFIG_PATH}"

umount "${FLASH_BOOT_MOUNT}" > /dev/null || true
# disable writing of persistent log to flasher boot partition and
# unmount before migrating, as any block level writes while the
# partition is mounted can leave the filesystem in an inconsistent
# state
disable_persistent_logging
# attempt unmount several times, as it may not succeed immediately, but
# don't try forever, as it may leave the device unreachable
if ! timeout 10 bash -c "until umount ${FLASH_BOOT_MOUNT}; do sleep 1; done"; then
fail "Failed to umount ${FLASH_BOOT_MOUNT}"
fi
sync "${EXTERNAL_DEVICE_BOOT_PART_MOUNTPOINT}"
# Unmount the rootfs as we are going to program over
umount "${ROOTFS_DIR}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ info "Board specific flash procedure..."
info "Log end"
# Preserve logs when running from initramfs
if [ -n "${INITRAMFS_LOGFILE}" ] && [ -f "${INITRAMFS_LOGFILE}" ]; then
cp -fv "${INITRAMFS_LOGFILE}" "${INTERNAL_DEVICE_BOOT_PART_MOUNTPOINT}/migration_$(date +"%Y%m%dT%H%M")"
cp -fv "${INITRAMFS_LOGFILE}" "${INTERNAL_DEVICE_BOOT_PART_MOUNTPOINT}/migration_$(date +"%Y%m%dT%H%M").log"
fi

umount $INTERNAL_DEVICE_BOOT_PART_MOUNTPOINT
Expand Down

0 comments on commit 6e8f57b

Please sign in to comment.