From 70fccbb4f2a6ca8a234864c7912496745dead9bf Mon Sep 17 00:00:00 2001 From: Hangfan Li Date: Fri, 5 Sep 2025 15:40:18 +0800 Subject: [PATCH] optimize logging --- Makefile | 1 + functions.mk | 7 +++---- tools/build_rootfs.sh | 2 ++ tools/fs_helper/ext4.sh | 8 ++++---- tools/fs_helper/vfat.sh | 4 ++-- tools/image_creation_helper.sh | 2 +- tools/logging.sh | 8 ++++++++ tools/partition_table_helper/gptcfg.sh | 8 ++++---- tools/partition_table_helper/guid.sh | 7 ++++--- tools/receipe_helper.sh | 6 +++--- 10 files changed, 32 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 678f4f9..18b4e06 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ default: all # Configurable options CONTAINER_CMD ?= docker CONTAINER_IMAGE_TAG ?= imagebuilder-openeuler-24.03-lts +LOG_LEVEL ?= INFO define stack_include #$$(info Makefile "$$(lastword $$(MAKEFILE_STACK))" includes "$1") diff --git a/functions.mk b/functions.mk index b5db9eb..d29da09 100644 --- a/functions.mk +++ b/functions.mk @@ -107,7 +107,7 @@ $$($$(DISTRO_NAME)_$$(RELEASE_NAME)_$$(RECEIPE_NAME)_ARTIFACT): | $(STAGING_DIR) /bin/sh -c ' \ mkdir /tmp/rootfs && \ cd /image-builder/tools && \ - ./build_rootfs.sh -i /image-builder/receipe -o /tmp/rootfs -p $$(PACKAGE_MANAGER) && \ + LOG_LEVEL=$(LOG_LEVEL) ./build_rootfs.sh -i /image-builder/receipe -o /tmp/rootfs -p $$(PACKAGE_MANAGER) && \ export OUTPUT_ARCHIVE=/image-builder/output/$$$$(notdir $$$$@) && \ export ROOTFS_DIR=/tmp/rootfs && \ $$(COMPRESSION_CMD)' @@ -178,9 +178,8 @@ $$(DISTRO_NAME)-$$(RELEASE_NAME)-$$(1)-$$(DEVICE_NAME)-$$(PROFILE_NAME): $(STAGI -v $$($$(DISTRO_NAME)_$$(RELEASE_NAME)_$$(1)_ARTIFACT):/image-builder/$$(notdir $$($$(DISTRO_NAME)_$$(RELEASE_NAME)_$$(1)_ARTIFACT)):ro \ -v $(DIST_DIR)/$$(DISTRO_NAME)/$$(RELEASE_NAME)/$$(DEVICE_NAME):/image-builder/output \ $(CONTAINER_IMAGE_TAG) \ - /bin/sh -c ' set -x && \ - cd /image-builder/tools && \ - LOG_LEVEL=DEBUG ./build_bsp.sh -i /image-builder/receipe -s /image-builder/$$(notdir $$($$(DISTRO_NAME)_$$(RELEASE_NAME)_$$(1)_ARTIFACT)) -o /image-builder/output/$$(DISTRO_NAME)-$$(RELEASE_NAME)-$$(1)-$$(DEVICE_NAME)-$$(PROFILE_NAME) -p $$(PACKAGE_MANAGER)' + /bin/sh -c 'cd /image-builder/tools && \ + LOG_LEVEL=$(LOG_LEVEL) ./build_bsp.sh -i /image-builder/receipe -s /image-builder/$$(notdir $$($$(DISTRO_NAME)_$$(RELEASE_NAME)_$$(1)_ARTIFACT)) -o /image-builder/output/$$(DISTRO_NAME)-$$(RELEASE_NAME)-$$(1)-$$(DEVICE_NAME)-$$(PROFILE_NAME) -p $$(PACKAGE_MANAGER)' ifneq ($$$$(wildcard $$(BSP_PROFILE_DIR)/extensions/$$(1)/.),) # corresponding extension exists diff --git a/tools/build_rootfs.sh b/tools/build_rootfs.sh index af3595e..d5abdbc 100755 --- a/tools/build_rootfs.sh +++ b/tools/build_rootfs.sh @@ -88,6 +88,8 @@ main() { generate_rootfs + logi "RootFS generated at: ${ROOTFS_DIR}" + exit 0 } diff --git a/tools/fs_helper/ext4.sh b/tools/fs_helper/ext4.sh index 998a299..6fee070 100644 --- a/tools/fs_helper/ext4.sh +++ b/tools/fs_helper/ext4.sh @@ -15,14 +15,14 @@ filesystem_set_uuid_ext4() { loge "Invalid ext4 serial number: ${uuid}" exit 1 fi - tune2fs -U "${uuid}" "${device}" + logcmd tune2fs -U "${uuid}" "${device}" } filesystem_make_fs_ext4() { - mkfs.ext4 "${1}" + logcmd mkfs.ext4 "${1}" } filesystem_shrink_ext4() { - e2fsck -y -f "${1}" - for i in $(seq 1 10); do resize2fs -M "${1}"; done + logcmd e2fsck -y -f "${1}" + for i in $(seq 1 10); do logcmd resize2fs -M "${1}"; done } diff --git a/tools/fs_helper/vfat.sh b/tools/fs_helper/vfat.sh index 00ee17e..e2a00fd 100644 --- a/tools/fs_helper/vfat.sh +++ b/tools/fs_helper/vfat.sh @@ -15,9 +15,9 @@ filesystem_set_uuid_vfat() { loge "Invalid vfat serial number: ${uuid}" exit 1 fi - mlabel -N "$(echo "${uuid}" | tr -d '-')" -i "${device}" + logcmd mlabel -N "$(echo "${uuid}" | tr -d '-')" -i "${device}" } filesystem_make_fs_vfat() { - mkfs.vfat "${1}" + logcmd mkfs.vfat "${1}" } diff --git a/tools/image_creation_helper.sh b/tools/image_creation_helper.sh index 2b25e85..3a2da08 100644 --- a/tools/image_creation_helper.sh +++ b/tools/image_creation_helper.sh @@ -77,7 +77,7 @@ create_fs_mountpoint_content() { prepare_fs_blocks # Restore plain rootfs onto structured mountpoint logi "Copy files" - rsync -ah "${ROOTFS_DIR}/" "${ROOTFS_MNT}" + logcmd rsync -avh "${ROOTFS_DIR}/" "${ROOTFS_MNT}" # Umount and shrink (if necessary) post_fs_blocks diff --git a/tools/logging.sh b/tools/logging.sh index 1eab804..b31eb49 100644 --- a/tools/logging.sh +++ b/tools/logging.sh @@ -35,6 +35,14 @@ __logprint() { __logprintraw "${formated_msg}" } +logcmd() { + if [ "$(__logparselevel)" -le "$LOGLEVEL_DEBUG" ]; then + { "$@" ;} + else + { "$@" >/dev/null 2>&1;} + fi +} + logd() { if [ "$(__logparselevel)" -le "$LOGLEVEL_DEBUG" ]; then __logprint "[DEBUG]" "$1" diff --git a/tools/partition_table_helper/gptcfg.sh b/tools/partition_table_helper/gptcfg.sh index 640d56f..fff4d4a 100755 --- a/tools/partition_table_helper/gptcfg.sh +++ b/tools/partition_table_helper/gptcfg.sh @@ -85,7 +85,7 @@ handle_set_uuid() { new_uuid="$(uuidgen)" fi logd "Setting new UUID for part ${partid}: ${new_uuid}" - 1>/dev/null sgdisk -u "${partid}:${new_uuid}" "${TARGET_FILE}" + logcmd sgdisk -u "${partid}:${new_uuid}" "${TARGET_FILE}" } handle_set_type() { @@ -121,13 +121,13 @@ handle_set_type() { " done logd "Set type code for part ${partid}: ${typecode}" - 1>/dev/null sgdisk -t "${partid}:${typecode}" "${TARGET_FILE}" + logcmd sgdisk -t "${partid}:${typecode}" "${TARGET_FILE}" printf '%s' "${typeattrbits}" | while read -r attrbit ; do if [ -z "${attrbit}" ]; then continue fi logd "Set partition attribute bit for part ${partid}: ${attrbit}" - 1>/dev/null sgdisk -A "${partid}:set:${attrbit}" "${TARGET_FILE}" + logcmd sgdisk -A "${partid}:set:${attrbit}" "${TARGET_FILE}" done } @@ -143,7 +143,7 @@ handle_set_name() { exit 2 fi partname="$1"; shift 1 - 1>/dev/null sgdisk -c "${partid}:${partname}" "${TARGET_FILE}" + logcmd sgdisk -c "${partid}:${partname}" "${TARGET_FILE}" } print_usage() { diff --git a/tools/partition_table_helper/guid.sh b/tools/partition_table_helper/guid.sh index 3b9110b..2158b83 100644 --- a/tools/partition_table_helper/guid.sh +++ b/tools/partition_table_helper/guid.sh @@ -161,7 +161,7 @@ produce_image_GUID() { rm -f "${OUTPUT_IMAGE}" logi "Allocating ${final_disk_size}B for ${OUTPUT_IMAGE}" truncate -s "${final_disk_size}" "${OUTPUT_IMAGE}" - sgdisk -Z "${OUTPUT_IMAGE}" >/dev/null 2>&1 + logcmd sgdisk -Z "${OUTPUT_IMAGE}" # sgdisk operates on files that may have different sector size with out target image SECTOR_SIZE="$(sgdisk -p "${OUTPUT_IMAGE}" | sed -rn 's/Sector size \(logical\): ([0-9]+) bytes/\1/p')" # Make sure our offset alignment satisfies sector size @@ -180,7 +180,7 @@ produce_image_GUID() { fi curr_part_offset="PARTITION_${part_idx}_OFFSET" curr_part_offset="${!curr_part_offset}" - sgdisk --new "${part_idx}:$(( curr_part_offset / SECTOR_SIZE )):+$(( curr_part_size / SECTOR_SIZE ))" "${OUTPUT_IMAGE}" >/dev/null 2>&1 + logcmd sgdisk --new "${part_idx}:$(( curr_part_offset / SECTOR_SIZE )):+$(( curr_part_size / SECTOR_SIZE ))" "${OUTPUT_IMAGE}" curr_part_uuid="PARTITION_${part_idx}_UUID" curr_part_uuid="${!curr_part_uuid}" ./partition_table_helper/gptcfg.sh "${OUTPUT_IMAGE}" set-uuid "${part_idx}" "${curr_part_uuid}" @@ -196,7 +196,8 @@ produce_image_GUID() { # Filling contents curr_part_content="PARTITION_${part_idx}_CONTENT" curr_part_content="${!curr_part_content}" - dd conv=notrunc,sparse status=none \ + logi "Filling partition with ${curr_part_content}" + logcmd dd conv=notrunc,sparse \ if="${curr_part_content}" \ of="${OUTPUT_IMAGE}" \ bs="${GUID_TABLE_SECTOR_SIZE}" \ diff --git a/tools/receipe_helper.sh b/tools/receipe_helper.sh index dd420c1..4f6f448 100644 --- a/tools/receipe_helper.sh +++ b/tools/receipe_helper.sh @@ -33,7 +33,7 @@ ${line}/entry.sh" } install_package_cleanup_yum() { - yum --forcearch riscv64 --installroot="${ROOTFS_DIR}" clean all + logcmd yum --forcearch riscv64 --installroot="${ROOTFS_DIR}" clean all } install_package_cleanup() { @@ -48,7 +48,7 @@ install_package_cleanup() { install_package_list_yum() { # shellcheck shell=dash disable=SC2086 - yum --forcearch riscv64 --installroot="${ROOTFS_DIR}" -y install $1 + logcmd yum --forcearch riscv64 --installroot="${ROOTFS_DIR}" -y install $1 } install_package_list() { @@ -63,7 +63,7 @@ install_package_list() { install_package_group_yum() { # shellcheck shell=dash disable=SC2086 - yum --forcearch riscv64 --installroot="${ROOTFS_DIR}" -y groupinstall $1 + logcmd yum --forcearch riscv64 --installroot="${ROOTFS_DIR}" -y groupinstall $1 } install_package_group() {