From ae6a9e1b035c236b0a8d720b81c8af63205854ff Mon Sep 17 00:00:00 2001 From: Ole Petter Date: Mon, 25 Jan 2021 18:01:32 +0100 Subject: [PATCH] Reformat: All files with shfmt Changelog: None Signed-off-by: Ole Petter --- configs/beaglebone_black_base_config | 62 +-- configs/images/raspberrypi_raspbian_config | 16 +- configs/mender_convert_config | 17 +- configs/raspberrypi_config | 170 +++---- configs/rockpro64_config | 20 +- mender-convert | 70 +-- modules/cliparser.sh | 56 +-- modules/config.sh | 5 +- modules/deb.sh | 106 ++--- modules/decompressinput.sh | 108 ++--- modules/disk.sh | 265 ++++++----- modules/git.sh | 3 +- modules/log.sh | 45 +- modules/probe.sh | 420 +++++++++--------- modules/run.sh | 24 +- modules/testscfg.sh | 32 +- modules/zip.sh | 22 +- .../bootstrap-rootfs-overlay-demo-server.sh | 46 +- .../bootstrap-rootfs-overlay-hosted-server.sh | 46 +- ...tstrap-rootfs-overlay-production-server.sh | 58 +-- 20 files changed, 789 insertions(+), 802 deletions(-) diff --git a/configs/beaglebone_black_base_config b/configs/beaglebone_black_base_config index c948d8c..abb0406 100644 --- a/configs/beaglebone_black_base_config +++ b/configs/beaglebone_black_base_config @@ -20,51 +20,51 @@ MENDER_IGNORE_UBOOT_BROKEN_UEFI=1 MENDER_IGNORE_MISSING_EFI_STUB=1 function beaglebone_fix_broken_uefi_uboot() { - log_warn "Attempting to work around broken UEFI support in U-Boot by forcing a newer U-Boot version." + log_warn "Attempting to work around broken UEFI support in U-Boot by forcing a newer U-Boot version." - mkdir -p work/bbb/binaries + mkdir -p work/bbb/binaries - run_and_log_cmd "wget -q ${BEAGLEBONE_BLACK_BINARIES_URL} -P work/bbb/binaries" - run_and_log_cmd "tar xzvf work/bbb/binaries/${BEAGLEBONE_BLACK_BINARIES} -C work/bbb/binaries" + run_and_log_cmd "wget -q ${BEAGLEBONE_BLACK_BINARIES_URL} -P work/bbb/binaries" + run_and_log_cmd "tar xzvf work/bbb/binaries/${BEAGLEBONE_BLACK_BINARIES} -C work/bbb/binaries" - # Place u-boot and MLO into rootfs/boot - run_and_log_cmd "sudo mkdir -p work/rootfs/boot" - run_and_log_cmd "sudo cp work/bbb/binaries/MLO work/boot/" - run_and_log_cmd "sudo cp work/bbb/binaries/u-boot.img work/boot/" + # Place u-boot and MLO into rootfs/boot + run_and_log_cmd "sudo mkdir -p work/rootfs/boot" + run_and_log_cmd "sudo cp work/bbb/binaries/MLO work/boot/" + run_and_log_cmd "sudo cp work/bbb/binaries/u-boot.img work/boot/" - run_and_log_cmd "sudo install -m 755 work/bbb/binaries/fw_printenv work/rootfs/sbin/fw_printenv" - run_and_log_cmd "sudo ln -fs /sbin/fw_printenv work/rootfs/sbin/fw_setenv" + run_and_log_cmd "sudo install -m 755 work/bbb/binaries/fw_printenv work/rootfs/sbin/fw_printenv" + run_and_log_cmd "sudo ln -fs /sbin/fw_printenv work/rootfs/sbin/fw_setenv" } function beaglebone_fix_broken_uefi_kernel() { - log_warn "Attempting to work around kernel without EFI stub by using a newer kernel." + log_warn "Attempting to work around kernel without EFI stub by using a newer kernel." - mkdir -p work/bbb/kernel - run_and_log_cmd "wget -q $BEAGLEBONE_EFI_COMPATIBLE_KERNEL_URL -P work/bbb/kernel" - mkdir -p work/bbb/kernel/extract - run_and_log_cmd "tar xJf work/bbb/kernel/$BEAGLEBONE_EFI_COMPATIBLE_KERNEL -C work/bbb/kernel/extract" - run_and_log_cmd "cp -r work/bbb/kernel/extract/* work/rootfs/" - run_and_log_cmd "ln -sf vmlinuz-$BEAGLEBONE_EFI_COMPATIBLE_KERNEL_VERSION work/rootfs/boot/kernel" - run_and_log_cmd "ln -sf initrd.img-$BEAGLEBONE_EFI_COMPATIBLE_KERNEL_VERSION work/rootfs/boot/initrd" + mkdir -p work/bbb/kernel + run_and_log_cmd "wget -q $BEAGLEBONE_EFI_COMPATIBLE_KERNEL_URL -P work/bbb/kernel" + mkdir -p work/bbb/kernel/extract + run_and_log_cmd "tar xJf work/bbb/kernel/$BEAGLEBONE_EFI_COMPATIBLE_KERNEL -C work/bbb/kernel/extract" + run_and_log_cmd "cp -r work/bbb/kernel/extract/* work/rootfs/" + run_and_log_cmd "ln -sf vmlinuz-$BEAGLEBONE_EFI_COMPATIBLE_KERNEL_VERSION work/rootfs/boot/kernel" + run_and_log_cmd "ln -sf initrd.img-$BEAGLEBONE_EFI_COMPATIBLE_KERNEL_VERSION work/rootfs/boot/initrd" } function beaglebone_fix_broken_uefi() { - if ! is_uboot_with_uefi_support work/boot-gap.bin; then - beaglebone_fix_broken_uefi_uboot - fi - if ! is_efi_compatible_kernel work/rootfs/boot/$kernel_imagetype; then - beaglebone_fix_broken_uefi_kernel - fi + if ! is_uboot_with_uefi_support work/boot-gap.bin; then + beaglebone_fix_broken_uefi_uboot + fi + if ! is_efi_compatible_kernel work/rootfs/boot/$kernel_imagetype; then + beaglebone_fix_broken_uefi_kernel + fi } PLATFORM_MODIFY_HOOKS+=(beaglebone_fix_broken_uefi) function disable_udisks2_service() { - # Mask udisks2.service, otherwise it will mount the inactive part and we - # might write an update while it is mounted which often result in - # corruptions. - # - # TODO: Find a way to only blacklist mmcblk0pX devices instead of masking - # the service. - run_and_log_cmd "sudo ln -sf /dev/null work/rootfs/etc/systemd/system/udisks2.service" + # Mask udisks2.service, otherwise it will mount the inactive part and we + # might write an update while it is mounted which often result in + # corruptions. + # + # TODO: Find a way to only blacklist mmcblk0pX devices instead of masking + # the service. + run_and_log_cmd "sudo ln -sf /dev/null work/rootfs/etc/systemd/system/udisks2.service" } PLATFORM_MODIFY_HOOKS+=(disable_udisks2_service) diff --git a/configs/images/raspberrypi_raspbian_config b/configs/images/raspberrypi_raspbian_config index 7f83bab..8109283 100644 --- a/configs/images/raspberrypi_raspbian_config +++ b/configs/images/raspberrypi_raspbian_config @@ -17,11 +17,11 @@ MENDER_COMPRESS_DISK_IMAGE=lzma # Resize the data partition to fill the remaining space, using parted, with systemd # function grow_data_partition() { - log_info "Adding systemd init script to run parted and resize the data partition on boot" - log_info "to fill all the available space on the storage media" - run_and_log_cmd "sudo mkdir -p work/rpi/etc/systemd/system/" - run_and_log_cmd "sudo mkdir -p work/rootfs/etc/systemd/system/data.mount.wants/" - cat <<-EOF > work/rpi/etc/systemd/system/mender-grow-data.service + log_info "Adding systemd init script to run parted and resize the data partition on boot" + log_info "to fill all the available space on the storage media" + run_and_log_cmd "sudo mkdir -p work/rpi/etc/systemd/system/" + run_and_log_cmd "sudo mkdir -p work/rootfs/etc/systemd/system/data.mount.wants/" + cat <<- EOF > work/rpi/etc/systemd/system/mender-grow-data.service [Unit] Description=Mender service to grow data partition size DefaultDependencies=no @@ -38,10 +38,10 @@ function grow_data_partition() { WantedBy=data.mount EOF - # Install - run_and_log_cmd "cp work/rpi/etc/systemd/system/mender-grow-data.service \ + # Install + run_and_log_cmd "cp work/rpi/etc/systemd/system/mender-grow-data.service \ work/rootfs/etc/systemd/system/" - run_and_log_cmd "ln -sf work/rootfs/etc/systemd/system/mender-grow-data.service \ + run_and_log_cmd "ln -sf work/rootfs/etc/systemd/system/mender-grow-data.service \ work/rootfs/etc/systemd/system/data.mount.wants/" } diff --git a/configs/mender_convert_config b/configs/mender_convert_config index 62bb90b..270d5ea 100644 --- a/configs/mender_convert_config +++ b/configs/mender_convert_config @@ -32,7 +32,6 @@ MENDER_ARTIFACT_COMPRESSION="gzip" # this. MENDER_ENABLE_SYSTEMD=y - # Custom mkfs options for creating the rootfs partition MENDER_ROOT_PART_MKFS_OPTS="" @@ -232,16 +231,16 @@ source configs/mender_grub_config # You might want to override this to e.g provide state-scripts or providing # a private key to sign your artifact. mender_create_artifact() { - local -r device_types="${1}" - local -r artifact_name="${2}" - local -r image_name="${3}" - local -r all_device_types="$(for device_type in $device_types; do echo " --device-type $device_type"; done)" + local -r device_types="${1}" + local -r artifact_name="${2}" + local -r image_name="${3}" + local -r all_device_types="$(for device_type in $device_types; do echo " --device-type $device_type"; done)" - mender_artifact=deploy/${image_name}.mender - log_info "Writing Mender artifact to: ${mender_artifact}" - log_info "This can take up to 20 minutes depending on which compression method is used" + mender_artifact=deploy/${image_name}.mender + log_info "Writing Mender artifact to: ${mender_artifact}" + log_info "This can take up to 20 minutes depending on which compression method is used" - run_and_log_cmd "mender-artifact --compression ${MENDER_ARTIFACT_COMPRESSION} \ + run_and_log_cmd "mender-artifact --compression ${MENDER_ARTIFACT_COMPRESSION} \ write rootfs-image \ --file work/rootfs.img \ --output-path ${mender_artifact} \ diff --git a/configs/raspberrypi_config b/configs/raspberrypi_config index e1a2484..a8b8b7e 100644 --- a/configs/raspberrypi_config +++ b/configs/raspberrypi_config @@ -14,89 +14,89 @@ RASPBERRYPI_BINARIES="${RASPBERRYPI_CONFIG}-2019.01.tar.gz" RASPBERRYPI_BINARIES_URL="${MENDER_STORAGE_URL}/mender-convert/uboot/raspberrypi/${RASPBERRYPI_BINARIES}" function platform_modify() { - mkdir -p work/rpi/binaries - - run_and_log_cmd "wget -q ${RASPBERRYPI_BINARIES_URL} -P work/rpi/binaries" - run_and_log_cmd "tar xzvf work/rpi/binaries/${RASPBERRYPI_BINARIES} -C work/rpi/binaries" - - # By default, we modify cmdline.txt. - # - # In Ubuntu, the file is called btcmd.txt; if this file exists, use it instead - CMDLINE="cmdline.txt" - if [ -f work/boot/btcmd.txt ]; then - CMDLINE="btcmd.txt" - fi - - # Make a copy of Linux kernel arguments and modify. - run_and_log_cmd "cp work/boot/$CMDLINE work/rpi/$CMDLINE" - - # Set a dynamic rootfs part (required for Mender A/B update strategy) - run_and_log_cmd "sed -i 's/\b[ ]root=[^ ]*/ root=\${mender_kernel_root}/' work/rpi/$CMDLINE" - - # Root filesystem can not be resized when the disk is partition according - # to Mender layout, where the rootfs partition is the not last one which - # is a requirement to be able to do an "online" resize. - # - # This disables resize of rootfs on boot but applies the changes to - # cmdline.txt that are performed in the init_resize.sh script. - # - # Extracted from /usr/lib/raspi-config/init_resize.sh - run_and_log_cmd "sed -i 's| init=/usr/lib/raspi-config/init_resize\.sh||' work/rpi/$CMDLINE" - run_and_log_cmd "sed -i 's| sdhci\.debug_quirks2=4||' work/rpi/$CMDLINE" - if ! grep -q splash work/rpi/$CMDLINE; then - run_and_log_cmd "sed -i 's/ quiet//g' work/rpi/$CMDLINE" - fi - - # Update Linux kernel command arguments with our custom configuration - run_and_log_cmd "sudo cp work/rpi/$CMDLINE work/boot/" - - # Mask udisks2.service, otherwise it will mount the inactive part and we - # might write an update while it is mounted which often result in - # corruptions. - # - # TODO: Find a way to only blacklist mmcblk0pX devices instead of masking - # the service. - run_and_log_cmd "sudo ln -sf /dev/null work/rootfs/etc/systemd/system/udisks2.service" - - # Ubuntu Server images actually use U-boot by default on RPi3 and the - # layout is slightly different on the boot partition. That is why we need - # additional logic here to determine what we are converting. - if [ -e work/boot/uboot_rpi_4_32b.bin ] && [ -e work/boot/vmlinuz ]; then - RASPBERRYPI_KERNEL_IMAGE="vmlinuz" - RASPBERRYPI_BOOTLOADER_IMAGE="uboot_rpi_4_32b.bin" - elif [ -e work/boot/uboot.bin ] && [ -e work/boot/vmlinuz ]; then - RASPBERRYPI_KERNEL_IMAGE="vmlinuz" - RASPBERRYPI_BOOTLOADER_IMAGE="uboot.bin" - else - RASPBERRYPI_BOOTLOADER_IMAGE="${RASPBERRYPI_KERNEL_IMAGE}" - fi - - # Extract Linux kernel and install to /boot directory on rootfs if it doesn't already exist. - # If it already exists, the input image is likely already converted so we just want to use - # the existing kernel image. - if [ ! -f "work/rootfs/boot/${MENDER_KERNEL_IMAGETYPE}" ]; then - run_and_log_cmd "sudo cp work/boot/${RASPBERRYPI_KERNEL_IMAGE} work/rootfs/boot/${MENDER_KERNEL_IMAGETYPE}" - fi - - # Replace kernel with U-boot and add boot script - run_and_log_cmd "sudo mkdir -p work/rootfs/uboot" - run_and_log_cmd "sudo cp work/rpi/binaries/u-boot.bin work/boot/${RASPBERRYPI_BOOTLOADER_IMAGE}" - run_and_log_cmd "sudo cp work/rpi/binaries/boot.scr work/boot" - run_and_log_cmd "sudo cp work/rpi/binaries/fw_env.config work/rootfs/etc/" - run_and_log_cmd "sudo cp work/rpi/binaries/uboot-git-log.txt work/boot" - - # Raspberry Pi applications expect to find this on the device and in some - # cases parse the options to determine the functionality. - run_and_log_cmd "sudo ln -fs /uboot/config.txt work/rootfs/boot/config.txt" - run_and_log_cmd "sudo ln -fs /uboot/overlays work/rootfs/boot/overlays" - run_and_log_cmd "sudo ln -fs /uboot/$CMDLINE work/rootfs/boot/$CMDLINE" - - run_and_log_cmd "sudo install -m 755 work/rpi/binaries/fw_printenv work/rootfs/sbin/fw_printenv" - run_and_log_cmd "sudo ln -fs /sbin/fw_printenv work/rootfs/sbin/fw_setenv" - - # Remove original 'resize2fs_once' script and its symbolic link. - if [ -L work/rootfs/etc/rc3.d/S01resize2fs_once ]; then - run_and_log_cmd "sudo unlink work/rootfs/etc/rc3.d/S01resize2fs_once" - fi - run_and_log_cmd "sudo rm -f work/rootfs/etc/init.d/resize2fs_once" + mkdir -p work/rpi/binaries + + run_and_log_cmd "wget -q ${RASPBERRYPI_BINARIES_URL} -P work/rpi/binaries" + run_and_log_cmd "tar xzvf work/rpi/binaries/${RASPBERRYPI_BINARIES} -C work/rpi/binaries" + + # By default, we modify cmdline.txt. + # + # In Ubuntu, the file is called btcmd.txt; if this file exists, use it instead + CMDLINE="cmdline.txt" + if [ -f work/boot/btcmd.txt ]; then + CMDLINE="btcmd.txt" + fi + + # Make a copy of Linux kernel arguments and modify. + run_and_log_cmd "cp work/boot/$CMDLINE work/rpi/$CMDLINE" + + # Set a dynamic rootfs part (required for Mender A/B update strategy) + run_and_log_cmd "sed -i 's/\b[ ]root=[^ ]*/ root=\${mender_kernel_root}/' work/rpi/$CMDLINE" + + # Root filesystem can not be resized when the disk is partition according + # to Mender layout, where the rootfs partition is the not last one which + # is a requirement to be able to do an "online" resize. + # + # This disables resize of rootfs on boot but applies the changes to + # cmdline.txt that are performed in the init_resize.sh script. + # + # Extracted from /usr/lib/raspi-config/init_resize.sh + run_and_log_cmd "sed -i 's| init=/usr/lib/raspi-config/init_resize\.sh||' work/rpi/$CMDLINE" + run_and_log_cmd "sed -i 's| sdhci\.debug_quirks2=4||' work/rpi/$CMDLINE" + if ! grep -q splash work/rpi/$CMDLINE; then + run_and_log_cmd "sed -i 's/ quiet//g' work/rpi/$CMDLINE" + fi + + # Update Linux kernel command arguments with our custom configuration + run_and_log_cmd "sudo cp work/rpi/$CMDLINE work/boot/" + + # Mask udisks2.service, otherwise it will mount the inactive part and we + # might write an update while it is mounted which often result in + # corruptions. + # + # TODO: Find a way to only blacklist mmcblk0pX devices instead of masking + # the service. + run_and_log_cmd "sudo ln -sf /dev/null work/rootfs/etc/systemd/system/udisks2.service" + + # Ubuntu Server images actually use U-boot by default on RPi3 and the + # layout is slightly different on the boot partition. That is why we need + # additional logic here to determine what we are converting. + if [ -e work/boot/uboot_rpi_4_32b.bin ] && [ -e work/boot/vmlinuz ]; then + RASPBERRYPI_KERNEL_IMAGE="vmlinuz" + RASPBERRYPI_BOOTLOADER_IMAGE="uboot_rpi_4_32b.bin" + elif [ -e work/boot/uboot.bin ] && [ -e work/boot/vmlinuz ]; then + RASPBERRYPI_KERNEL_IMAGE="vmlinuz" + RASPBERRYPI_BOOTLOADER_IMAGE="uboot.bin" + else + RASPBERRYPI_BOOTLOADER_IMAGE="${RASPBERRYPI_KERNEL_IMAGE}" + fi + + # Extract Linux kernel and install to /boot directory on rootfs if it doesn't already exist. + # If it already exists, the input image is likely already converted so we just want to use + # the existing kernel image. + if [ ! -f "work/rootfs/boot/${MENDER_KERNEL_IMAGETYPE}" ]; then + run_and_log_cmd "sudo cp work/boot/${RASPBERRYPI_KERNEL_IMAGE} work/rootfs/boot/${MENDER_KERNEL_IMAGETYPE}" + fi + + # Replace kernel with U-boot and add boot script + run_and_log_cmd "sudo mkdir -p work/rootfs/uboot" + run_and_log_cmd "sudo cp work/rpi/binaries/u-boot.bin work/boot/${RASPBERRYPI_BOOTLOADER_IMAGE}" + run_and_log_cmd "sudo cp work/rpi/binaries/boot.scr work/boot" + run_and_log_cmd "sudo cp work/rpi/binaries/fw_env.config work/rootfs/etc/" + run_and_log_cmd "sudo cp work/rpi/binaries/uboot-git-log.txt work/boot" + + # Raspberry Pi applications expect to find this on the device and in some + # cases parse the options to determine the functionality. + run_and_log_cmd "sudo ln -fs /uboot/config.txt work/rootfs/boot/config.txt" + run_and_log_cmd "sudo ln -fs /uboot/overlays work/rootfs/boot/overlays" + run_and_log_cmd "sudo ln -fs /uboot/$CMDLINE work/rootfs/boot/$CMDLINE" + + run_and_log_cmd "sudo install -m 755 work/rpi/binaries/fw_printenv work/rootfs/sbin/fw_printenv" + run_and_log_cmd "sudo ln -fs /sbin/fw_printenv work/rootfs/sbin/fw_setenv" + + # Remove original 'resize2fs_once' script and its symbolic link. + if [ -L work/rootfs/etc/rc3.d/S01resize2fs_once ]; then + run_and_log_cmd "sudo unlink work/rootfs/etc/rc3.d/S01resize2fs_once" + fi + run_and_log_cmd "sudo rm -f work/rootfs/etc/init.d/resize2fs_once" } diff --git a/configs/rockpro64_config b/configs/rockpro64_config index 73daa91..14de804 100644 --- a/configs/rockpro64_config +++ b/configs/rockpro64_config @@ -11,22 +11,22 @@ ROCKPRO64_BINARIES="${ROCKPRO64_CONFIG}-2017.09.tar.gz" ROCKPRO64_BINARIES_URL="${MENDER_STORAGE_URL}/mender-convert/uboot/rockpro64/${ROCKPRO64_BINARIES}" function platform_modify() { - mkdir -p work/rockpro64 + mkdir -p work/rockpro64 - run_and_log_cmd "wget -Nq ${ROCKPRO64_BINARIES_URL} -P work/rockpro64" - run_and_log_cmd "tar xzvf work/rockpro64/${ROCKPRO64_BINARIES} -C work/rockpro64" + run_and_log_cmd "wget -Nq ${ROCKPRO64_BINARIES_URL} -P work/rockpro64" + run_and_log_cmd "tar xzvf work/rockpro64/${ROCKPRO64_BINARIES} -C work/rockpro64" - run_and_log_cmd "sudo cp work/rockpro64/boot.scr work/boot" + run_and_log_cmd "sudo cp work/rockpro64/boot.scr work/boot" - run_and_log_cmd "sudo cp work/rockpro64/fw_env.config work/rootfs/etc/" + run_and_log_cmd "sudo cp work/rockpro64/fw_env.config work/rootfs/etc/" - # It is not possible to resize rootfs part when using Mender. so disable - # the service - run_and_log_cmd "sudo touch work/rootfs/root/.no_rootfs_resize" + # It is not possible to resize rootfs part when using Mender. so disable + # the service + run_and_log_cmd "sudo touch work/rootfs/root/.no_rootfs_resize" } function platform_package() { - log_info "Embedding bootloader in disk image" - run_and_log_cmd "dd if=work/rockpro64/rksd_loader.img of=${img_path} \ + log_info "Embedding bootloader in disk image" + run_and_log_cmd "dd if=work/rockpro64/rksd_loader.img of=${img_path} \ seek=64 conv=notrunc status=none" } diff --git a/mender-convert b/mender-convert index 880a2f3..0021f55 100755 --- a/mender-convert +++ b/mender-convert @@ -38,7 +38,7 @@ MENDER_CONVERT_VERSION="${MENDER_CONVERT_VERSION:-}" # Required env variable ############################################################################### function show_help() { - cat << EOF + cat << EOF mender-convert A tool that takes an existing embedded image (Debian, Ubuntu, Raspbian, etc) @@ -61,21 +61,21 @@ EOF } function show_version() { - echo "Version: ${MENDER_CONVERT_VERSION}" + echo "Version: ${MENDER_CONVERT_VERSION}" } function trap_exit() { - EXIT_CODE=$? - if [[ ${EXIT_CODE} -ne 0 && ${EXIT_CODE} -ne ${FATAL_EXIT_CODE} ]]; then - log_error "mender-convert failed" - [ -e ${MENDER_CONVERT_LOG_FILE} ] && tac ${MENDER_CONVERT_LOG_FILE} | sed '/DEBUG/q' | tac | sed 's/Running/When running/' - log_error "mender-convert exit code: ${EXIT_CODE}" - fi - sudo rm -rf work/* + EXIT_CODE=$? + if [[ ${EXIT_CODE} -ne 0 && ${EXIT_CODE} -ne ${FATAL_EXIT_CODE} ]]; then + log_error "mender-convert failed" + [ -e ${MENDER_CONVERT_LOG_FILE} ] && tac ${MENDER_CONVERT_LOG_FILE} | sed '/DEBUG/q' | tac | sed 's/Running/When running/' + log_error "mender-convert exit code: ${EXIT_CODE}" + fi + sudo rm -rf work/* } function trap_term() { - echo "Program interrupted by user" + echo "Program interrupted by user" } trap trap_term INT TERM @@ -83,40 +83,40 @@ trap trap_exit EXIT source modules/git.sh -if [ -z "$MENDER_CONVERT_VERSION" ];then - MENDER_CONVERT_VERSION=$(git_mender_convert_version) +if [ -z "$MENDER_CONVERT_VERSION" ]; then + MENDER_CONVERT_VERSION=$(git_mender_convert_version) fi # We only handle a selection of the arguments here, the rest are passed on # to the sub-scripts. while (("$#")); do - case "$1" in - -h | --help) - show_help - exit 0 - ;; - -v | --version) - show_version - exit 0 - ;; - *) - break - ;; - esac + case "$1" in + -h | --help) + show_help + exit 0 + ;; + -v | --version) + show_version + exit 0 + ;; + *) + break + ;; + esac done -mender_convert_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +mender_convert_dir=$( cd "$( dirname "${BASH_SOURCE[0]}")" && pwd) if [ "${mender_convert_dir}" != "${PWD}" ]; then - echo "You must execute mender-convert from the root directory: ${mender_convert_dir}" - exit 1 + echo "You must execute mender-convert from the root directory: ${mender_convert_dir}" + exit 1 fi if [ -z "${MENDER_ARTIFACT_NAME}" ]; then - echo "Sorry, it seems that you have not defined MENDER_ARTIFACT_NAME" - echo "You can do this with the following command:" - echo "" - echo -e "\tMENDER_ARTIFACT_NAME=\"release-1\" ./mender-convert" - exit 1 + echo "Sorry, it seems that you have not defined MENDER_ARTIFACT_NAME" + echo "You can do this with the following command:" + echo "" + echo -e "\tMENDER_ARTIFACT_NAME=\"release-1\" ./mender-convert" + exit 1 fi testscfg_init @@ -125,8 +125,8 @@ parse_cli_options "$@" compression_type=$(compression_type "${disk_image}") if [[ ${compression_type} != "none" ]]; then - uncompressed_disk_image=$(decompress_image "${disk_image}" "./work") - append_extraargs="--disk-image ${uncompressed_disk_image}" + uncompressed_disk_image=$(decompress_image "${disk_image}" "./work") + append_extraargs="--disk-image ${uncompressed_disk_image}" fi echo "MENDER_COMPRESS_DISK_IMAGE=${compression_type}" > ${ocfile} prepend_extraargs="--config ${ocfile}" diff --git a/modules/cliparser.sh b/modules/cliparser.sh index c31ed22..c7d7771 100644 --- a/modules/cliparser.sh +++ b/modules/cliparser.sh @@ -13,35 +13,35 @@ # See the License for the specific language governing permissions and # limitations under the License. -function parse_cli_options () { - while (( "$#" )); do - case "$1" in - -o | --overlay) - overlays+=("${2}") - shift 2 - ;; - -c | --config) - configs+=("${2}") - shift 2 - ;; - -d | --disk-image) - disk_image="${2}" - shift 2 - ;; - *) - log_fatal "Sorry but the provided option is not supported: $1" - ;; - esac - done +function parse_cli_options() { + while (("$#")); do + case "$1" in + -o | --overlay) + overlays+=("${2}") + shift 2 + ;; + -c | --config) + configs+=("${2}") + shift 2 + ;; + -d | --disk-image) + disk_image="${2}" + shift 2 + ;; + *) + log_fatal "Sorry but the provided option is not supported: $1" + ;; + esac + done - if [ -z "${disk_image}" ]; then - log_warn "Sorry, but '--disk-image' is a mandatory option" - log_warn "See ./mender-convert --help for more information" - exit 1 - fi + if [ -z "${disk_image}" ]; then + log_warn "Sorry, but '--disk-image' is a mandatory option" + log_warn "See ./mender-convert --help for more information" + exit 1 + fi - if [ ! -e ${disk_image} ]; then - log_fatal "File not found: ${disk_image}" - fi + if [ ! -e ${disk_image} ]; then + log_fatal "File not found: ${disk_image}" + fi } diff --git a/modules/config.sh b/modules/config.sh index 6084b84..9bfffa6 100644 --- a/modules/config.sh +++ b/modules/config.sh @@ -17,11 +17,10 @@ # Read in the array of config files to process read -a configs <<< "${@}" for config in "${configs[@]}"; do - log_info "Using configuration file: ${config}" - source "${config}" + log_info "Using configuration file: ${config}" + source "${config}" done - # Fine grained partition variables override device/number variables where applicable disk_override_partition_variable "MENDER_BOOT_PART_NUMBER" "${MENDER_BOOT_PART}" disk_override_partition_variable "MENDER_ROOTFS_PART_A_NUMBER" "${MENDER_ROOTFS_PART_A}" diff --git a/modules/deb.sh b/modules/deb.sh index de87987..04562d6 100644 --- a/modules/deb.sh +++ b/modules/deb.sh @@ -26,32 +26,32 @@ source modules/log.sh # # @return - Filename of the downloaded package # -function deb_from_repo_dist_get () { - if [[ $# -lt 5 || $# -gt 7 ]]; then - log_fatal "deb_from_repo_dist_get() requires 5 arguments" - fi - local -r download_dir="${1}" - local -r repo_url="${2}" - local -r architecture="${3}" - local -r distribution="${4}" - local -r package="${5}" - local -r component="${6:-main}" +function deb_from_repo_dist_get() { + if [[ $# -lt 5 || $# -gt 7 ]]; then + log_fatal "deb_from_repo_dist_get() requires 5 arguments" + fi + local -r download_dir="${1}" + local -r repo_url="${2}" + local -r architecture="${3}" + local -r distribution="${4}" + local -r package="${5}" + local -r component="${6:-main}" - # Fetch and parse the packages list of the given distribution to find the latest version - local -r packages_url="${repo_url}/dists/${distribution}/${component}/binary-${architecture}/Packages" - run_and_log_cmd "wget -Nq ${packages_url} -P /tmp" + # Fetch and parse the packages list of the given distribution to find the latest version + local -r packages_url="${repo_url}/dists/${distribution}/${component}/binary-${architecture}/Packages" + run_and_log_cmd "wget -Nq ${packages_url} -P /tmp" - local -r deb_package_path=$(grep Filename /tmp/Packages | grep ${package}_ | grep ${architecture} | tail -n1 | sed 's/Filename: //') - if [ -z "${deb_package_path}" ]; then - log_fatal "Couldn't find package ${package} in ${packages_url}" - fi + local -r deb_package_path=$(grep Filename /tmp/Packages | grep ${package}_ | grep ${architecture} | tail -n1 | sed 's/Filename: //') + if [ -z "${deb_package_path}" ]; then + log_fatal "Couldn't find package ${package} in ${packages_url}" + fi - local -r filename=$(basename $deb_package_path) - run_and_log_cmd "wget -Nq ${repo_url}/${deb_package_path} -P ${download_dir}" + local -r filename=$(basename $deb_package_path) + run_and_log_cmd "wget -Nq ${repo_url}/${deb_package_path} -P ${download_dir}" - rm -f /tmp/Packages - log_info "Successfully downloaded ${filename}" - echo ${filename} + rm -f /tmp/Packages + log_info "Successfully downloaded ${filename}" + echo ${filename} } # Download a deb package direcrly from the pool of an APT repository @@ -65,26 +65,26 @@ function deb_from_repo_dist_get () { # # @return - Filename of the downloaded package # -function deb_from_repo_pool_get () { - if [[ $# -ne 5 ]]; then - log_fatal "deb_from_repo_pool_get() requires 5 arguments" - fi - local -r download_dir="${1}" - local -r repo_url="${2}" - local -r architecture="${3}" - local -r package="${4}" - local -r version="${5}" - local -r component="${6:-main}" +function deb_from_repo_pool_get() { + if [[ $# -ne 5 ]]; then + log_fatal "deb_from_repo_pool_get() requires 5 arguments" + fi + local -r download_dir="${1}" + local -r repo_url="${2}" + local -r architecture="${3}" + local -r package="${4}" + local -r version="${5}" + local -r component="${6:-main}" - local -r initial="$(echo $package | head -c 1)" - local -r deb_package_path="pool/${component}/${initial}/${package}/${package}_${version}_${architecture}.deb" + local -r initial="$(echo $package | head -c 1)" + local -r deb_package_path="pool/${component}/${initial}/${package}/${package}_${version}_${architecture}.deb" - local -r filename=$(basename $deb_package_path) - run_and_log_cmd "wget -Nq ${repo_url}/${deb_package_path} -P ${download_dir}" + local -r filename=$(basename $deb_package_path) + run_and_log_cmd "wget -Nq ${repo_url}/${deb_package_path} -P ${download_dir}" - rm -f /tmp/Packages - log_info "Successfully downloaded ${filename}" - echo ${filename} + rm -f /tmp/Packages + log_info "Successfully downloaded ${filename}" + echo ${filename} } # Extract the binary files of a deb package into a directory @@ -92,21 +92,21 @@ function deb_from_repo_pool_get () { # $1 - Deb package # $2 - Dest directory # -function deb_extract_package () { - if [[ $# -ne 2 ]]; then - log_fatal "deb_extract_package() requires 2 arguments" - fi - local -r deb_package="$(pwd)/${1}" - local -r dest_dir="$(pwd)/${2}" +function deb_extract_package() { + if [[ $# -ne 2 ]]; then + log_fatal "deb_extract_package() requires 2 arguments" + fi + local -r deb_package="$(pwd)/${1}" + local -r dest_dir="$(pwd)/${2}" - local -r extract_dir=$(mktemp -d) - cd ${extract_dir} - run_and_log_cmd "ar -xv ${deb_package}" - mkdir -p files - run_and_log_cmd "sudo tar xJf data.tar.xz -C files" - cd - > /dev/null 2>&1 + local -r extract_dir=$(mktemp -d) + cd ${extract_dir} + run_and_log_cmd "ar -xv ${deb_package}" + mkdir -p files + run_and_log_cmd "sudo tar xJf data.tar.xz -C files" + cd - > /dev/null 2>&1 - run_and_log_cmd "sudo rsync --archive --keep-dirlinks --verbose ${extract_dir}/files/ ${dest_dir}" + run_and_log_cmd "sudo rsync --archive --keep-dirlinks --verbose ${extract_dir}/files/ ${dest_dir}" - log_info "Successfully installed $(basename ${filename})" + log_info "Successfully installed $(basename ${filename})" } diff --git a/modules/decompressinput.sh b/modules/decompressinput.sh index 7004ed7..18c1f8e 100644 --- a/modules/decompressinput.sh +++ b/modules/decompressinput.sh @@ -22,28 +22,28 @@ source modules/log.sh # # @return - The MENDER_COMPRESS_IMAGE compression type # -function compression_type () { - if [[ $# -ne 1 ]]; then - log_fatal "compression_type() requires one argument" - fi - local -r disk_image="${1}" - case "${disk_image}" in - *.img | *.sdimg | *.wic | *.rpi-sdimg) - echo "none" - ;; - *.gz) - echo "gzip" - ;; - *.zip) - echo "zip" - ;; - *.xz ) - echo "lzma" - ;; - * ) - log_fatal "Unsupported compression type: ${disk_image}. Please uncompress the image yourself." - ;; - esac +function compression_type() { + if [[ $# -ne 1 ]]; then + log_fatal "compression_type() requires one argument" + fi + local -r disk_image="${1}" + case "${disk_image}" in + *.img | *.sdimg | *.wic | *.rpi-sdimg) + echo "none" + ;; + *.gz) + echo "gzip" + ;; + *.zip) + echo "zip" + ;; + *.xz) + echo "lzma" + ;; + *) + log_fatal "Unsupported compression type: ${disk_image}. Please uncompress the image yourself." + ;; + esac } # Decompresses the given input image @@ -53,36 +53,36 @@ function compression_type () { # # @return - Name of the uncompressed image # -function decompress_image () { - if [[ $# -ne 2 ]]; then - log_fatal "decompress_image() requires an image argument and an output directory" - fi - local -r input_image="${1}" - local -r output_dir="${2}" - local disk_image="${output_dir}/$(basename ${input_image})" - case "$(compression_type ${disk_image})" in - none ) - : - ;; - gzip ) - log_info "Decompressing ${disk_image}..." - disk_image=${disk_image%.gz} - zcat "${input_image}" > "${disk_image}" - ;; - zip ) - log_info "Decompressing ${disk_image}..." - filename="$(zip_get_imgname ${input_image})" - unzip "${input_image}" -d "${output_dir}" &>/dev/null - disk_image="$(dirname ${disk_image})/${filename}" - ;; - lzma ) - log_info "Decompressing ${disk_image}..." - disk_image=${disk_image%.xz} - xzcat "${input_image}" > "${disk_image}" - ;; - * ) - log_fatal "Unsupported input image format: ${input_image}. We support: '.img', '.gz', '.zip', '.xz'." - ;; - esac - echo "${disk_image}" +function decompress_image() { + if [[ $# -ne 2 ]]; then + log_fatal "decompress_image() requires an image argument and an output directory" + fi + local -r input_image="${1}" + local -r output_dir="${2}" + local disk_image="${output_dir}/$(basename ${input_image})" + case "$(compression_type ${disk_image})" in + none) + : + ;; + gzip) + log_info "Decompressing ${disk_image}..." + disk_image=${disk_image%.gz} + zcat "${input_image}" > "${disk_image}" + ;; + zip) + log_info "Decompressing ${disk_image}..." + filename="$(zip_get_imgname ${input_image})" + unzip "${input_image}" -d "${output_dir}" &> /dev/null + disk_image="$(dirname ${disk_image})/${filename}" + ;; + lzma) + log_info "Decompressing ${disk_image}..." + disk_image=${disk_image%.xz} + xzcat "${input_image}" > "${disk_image}" + ;; + *) + log_fatal "Unsupported input image format: ${input_image}. We support: '.img', '.gz', '.zip', '.xz'." + ;; + esac + echo "${disk_image}" } diff --git a/modules/disk.sh b/modules/disk.sh index a21002c..bce4a5f 100644 --- a/modules/disk.sh +++ b/modules/disk.sh @@ -37,7 +37,7 @@ # FLAGS partition flags # SCHEME partition table type (dos, gpt, ...) disk_get_part_value() { - echo "$(partx -o ${3} -g -r --nr ${2} ${1})" + echo "$(partx -o ${3} -g -r --nr ${2} ${1})" } # Prints the partition numbers of all the partitions @@ -48,11 +48,10 @@ disk_get_part_value() { # # $1 - path to disk image disk_get_part_nums() { - partx --show $1 | tail -n +2 | - while read line - do - echo $line | awk '{printf "%d\n", $1}' - done + partx --show $1 | tail -n +2 \ + | while read line; do + echo $line | awk '{printf "%d\n", $1}' + done } # Extract a file system image from a disk image @@ -62,21 +61,21 @@ disk_get_part_nums() { # $3 - size (in 512 blocks) # $4 - path to output file disk_extract_part() { - run_and_log_cmd "dd if=$1 of=$4 skip=$2 bs=512 count=$3 conv=sparse status=none" + run_and_log_cmd "dd if=$1 of=$4 skip=$2 bs=512 count=$3 conv=sparse status=none" } # Convert MiB to number of 512 sectors # # $1 - MiB value disk_mb_to_sectors() { - echo "$(( (${1} * 1024 * 1024) / 512 ))" + echo "$(((${1} * 1024 * 1024) / 512))" } # Convert 512 sectors to MiB # # $1 - number of 512 sectors disk_sectors_to_mb() { - echo "$(( (${1} * 512) / 1024 / 1024 ))" + echo "$(((${1} * 512) / 1024 / 1024))" } # Align value (result is number of 512 sectors) @@ -84,14 +83,14 @@ disk_sectors_to_mb() { # $1 - value to align (number of 512 sectors) # $2 - alignment in bytes disk_align_sectors() { - local size_in_bytes=$(( ${1} * 512)) - local reminder=$(( ${size_in_bytes} % ${2} )) + local size_in_bytes=$((${1} * 512)) + local reminder=$((${size_in_bytes} % ${2})) - if [ $reminder -ne 0 ]; then - size_in_bytes=$(( $size_in_bytes - $reminder + ${2} )) - fi + if [ $reminder -ne 0 ]; then + size_in_bytes=$(($size_in_bytes - $reminder + ${2})) + fi - echo "$(( $size_in_bytes / 512 ))" + echo "$(($size_in_bytes / 512))" } # Write file at offset of another file @@ -100,7 +99,7 @@ disk_align_sectors() { # $2 - destination file # $3 - offset in number of 512 sectors disk_write_at_offset() { - run_and_log_cmd "dd if=${1} of=${2} seek=${3} conv=notrunc status=none" + run_and_log_cmd "dd if=${1} of=${2} seek=${3} conv=notrunc status=none" } # Create file system image from directory content @@ -110,144 +109,138 @@ disk_write_at_offset() { # $3 - image size in sectors # $4 - file system type, e.g ext4, xfs, ... disk_create_file_system_from_folder() { - log_info "Creating a file-system image from: ${1}" + log_info "Creating a file-system image from: ${1}" - run_and_log_cmd "dd if=/dev/zero of=${2} seek=${3} count=0 bs=512 status=none" + run_and_log_cmd "dd if=/dev/zero of=${2} seek=${3} count=0 bs=512 status=none" - case ${1} in - *data/ ) EXTRA_OPTS="${MENDER_DATA_PART_MKFS_OPTS}";; - *rootfs/ ) EXTRA_OPTS="${MENDER_ROOT_PART_MKFS_OPTS}";; - * ) EXTRA_OPTS="";; - esac + case ${1} in + *data/) EXTRA_OPTS="${MENDER_DATA_PART_MKFS_OPTS}" ;; + *rootfs/) EXTRA_OPTS="${MENDER_ROOT_PART_MKFS_OPTS}" ;; + *) EXTRA_OPTS="" ;; + esac - case "$4" in - "ext4") - MKFS_EXT4="/usr/bin/mkfs.ext4" - if [ ! -f ${MKFS_EXT4} ]; then - MKFS_EXT4="/sbin/mkfs.ext4" - fi - run_and_log_cmd "${MKFS_EXT4} -q -F ${2} ${EXTRA_OPTS}" - ;; + case "$4" in + "ext4") + MKFS_EXT4="/usr/bin/mkfs.ext4" + if [ ! -f ${MKFS_EXT4} ]; then + MKFS_EXT4="/sbin/mkfs.ext4" + fi + run_and_log_cmd "${MKFS_EXT4} -q -F ${2} ${EXTRA_OPTS}" + ;; - "xfs") - MKFS_XFS="/usr/bin/mkfs.xfs" - if [ ! -f ${MKFS_XFS} ]; then - MKFS_XFS="/sbin/mkfs.xfs" - fi - run_and_log_cmd "${MKFS_XFS} -q -f ${2} ${EXTRA_OPTS}" - ;; - *) - log_fatal "Unknown file system type specified: ${4}" - ;; - esac + "xfs") + MKFS_XFS="/usr/bin/mkfs.xfs" + if [ ! -f ${MKFS_XFS} ]; then + MKFS_XFS="/sbin/mkfs.xfs" + fi + run_and_log_cmd "${MKFS_XFS} -q -f ${2} ${EXTRA_OPTS}" + ;; + *) + log_fatal "Unknown file system type specified: ${4}" + ;; + esac - run_and_log_cmd "mkdir -p work/output" - run_and_log_cmd "sudo mount ${2} work/output" - run_and_log_cmd "sudo rsync --archive --delete ${1} work/output/" - run_and_log_cmd "sudo umount work/output" + run_and_log_cmd "mkdir -p work/output" + run_and_log_cmd "sudo mount ${2} work/output" + run_and_log_cmd "sudo rsync --archive --delete ${1} work/output/" + run_and_log_cmd "sudo umount work/output" } # Print path to the boot partition filesystem image # disk_boot_part() { - # Why this little dance you might wonder. - # - # Some disk images do not have a boot partition, but our integration does - # require one and this is why there is a difference here. - # - # If input image did not have a boot part it will be generated and be called: - # - # work/boot-generated.vfat - # - # This is mostly done to make it obvious that we have created an empty vfat file. - # - # But if the disk image already had a boot part, we assume that it is the first - # partition that was extracted. - # - # We also need to adjust the part number of the rootfs part depending on if - # boot part was extracted or generated. - boot_part="work/boot-generated.vfat" - if [ ! -f ${boot_part} ]; then - boot_part="work/part-1.fs" - fi - echo "${boot_part}" + # Why this little dance you might wonder. + # + # Some disk images do not have a boot partition, but our integration does + # require one and this is why there is a difference here. + # + # If input image did not have a boot part it will be generated and be called: + # + # work/boot-generated.vfat + # + # This is mostly done to make it obvious that we have created an empty vfat file. + # + # But if the disk image already had a boot part, we assume that it is the first + # partition that was extracted. + # + # We also need to adjust the part number of the rootfs part depending on if + # boot part was extracted or generated. + boot_part="work/boot-generated.vfat" + if [ ! -f ${boot_part} ]; then + boot_part="work/part-1.fs" + fi + echo "${boot_part}" } # Print path to the root partition filesystem image # disk_root_part() { - boot_part="work/boot-generated.vfat" - if [ ! -f ${boot_part} ]; then - root_part="work/part-2.fs" - else - root_part="work/part-1.fs" - fi - echo "${root_part}" + boot_part="work/boot-generated.vfat" + if [ ! -f ${boot_part} ]; then + root_part="work/part-2.fs" + else + root_part="work/part-1.fs" + fi + echo "${root_part}" } - # Check if supplied argument is valid partuuid device path. # Supports both dos and gpt paths # # $1 - partuuid device path disk_is_valid_partuuid_device() { - disk_is_valid_partuuid_gpt_device "$1" || disk_is_valid_partuuid_dos_device "$1" + disk_is_valid_partuuid_gpt_device "$1" || disk_is_valid_partuuid_dos_device "$1" } - # Check if supplied argument is valid gpt partuuid device path. # # Example: /dev/disk/by-partuuid/26445670-f37c-408b-be2c-3ef419866620 # # $1 - gpt partuuid device path disk_is_valid_partuuid_gpt_device() { - echo "${1}" | grep -qE '^/dev/disk/by-partuuid/([0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12})$' + echo "${1}" | grep -qE '^/dev/disk/by-partuuid/([0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12})$' } - # Check if supplied argument is valid dos partuuid device path. # # Example: /dev/disk/by-partuuid/26445670-01 # # $1 - dos partuuid device path disk_is_valid_partuuid_dos_device() { - echo "${1}" | grep -qE '^/dev/disk/by-partuuid/[0-9a-f]{8}-[0-9a-f]{2}$' + echo "${1}" | grep -qE '^/dev/disk/by-partuuid/[0-9a-f]{8}-[0-9a-f]{2}$' } - # Get partuuid from supplied device path. # Supports both dos and gpt paths # # $1 - partuuid device path disk_get_partuuid_from_device() { - if ! disk_is_valid_partuuid_device "${1}"; then - log_fatal "Invalid partuuid device: '${1}'" - fi - echo "${1}" | sed "s:/dev/disk/by-partuuid/::" + if ! disk_is_valid_partuuid_device "${1}"; then + log_fatal "Invalid partuuid device: '${1}'" + fi + echo "${1}" | sed "s:/dev/disk/by-partuuid/::" } - # Get dos disk identifier from supplied device path. # # $1 - dos compatible partuuid device path disk_get_partuuid_dos_diskid_from_device() { - if ! disk_is_valid_partuuid_dos_device "${1}"; then - log_fatal "Invalid dos partuuid device: '${1}'" - fi - partuuid=$(disk_get_partuuid_from_device "${1}") - echo "$partuuid" | cut -d- -f1 + if ! disk_is_valid_partuuid_dos_device "${1}"; then + log_fatal "Invalid dos partuuid device: '${1}'" + fi + partuuid=$(disk_get_partuuid_from_device "${1}") + echo "$partuuid" | cut -d- -f1 } - # Get dos partuuid number from supplied device path. # # $1 - dos compatible partuuid device path disk_get_partuuid_dos_part_number() { - if ! disk_is_valid_partuuid_dos_device "${1}"; then - log_fatal "Invalid dos partuuid device: '${1}'" - fi - partuuid=$(disk_get_partuuid_from_device "${1}") - echo "$partuuid" | cut -d- -f2 + if ! disk_is_valid_partuuid_dos_device "${1}"; then + log_fatal "Invalid dos partuuid device: '${1}'" + fi + partuuid=$(disk_get_partuuid_from_device "${1}") + echo "$partuuid" | cut -d- -f2 } # Get correct device path for current configuration. @@ -256,34 +249,33 @@ disk_get_partuuid_dos_part_number() { # $1 - partition number to use if fine grained variable not set # $2 - fine grained device part variable name disk_get_part_device() { - part="${!2}" - if [ "${MENDER_ENABLE_PARTUUID}" == "y" ]; then - if ! disk_is_valid_partuuid_device "${part}"; then - log_fatal "Invalid partuuid device for ${2}: '${part}'" - fi - else - part="${MENDER_STORAGE_DEVICE_BASE}${1}" - fi - echo "${part}" + part="${!2}" + if [ "${MENDER_ENABLE_PARTUUID}" == "y" ]; then + if ! disk_is_valid_partuuid_device "${part}"; then + log_fatal "Invalid partuuid device for ${2}: '${part}'" + fi + else + part="${MENDER_STORAGE_DEVICE_BASE}${1}" + fi + echo "${part}" } disk_boot_part_device() { - disk_get_part_device "${MENDER_BOOT_PART_NUMBER}" "MENDER_BOOT_PART" + disk_get_part_device "${MENDER_BOOT_PART_NUMBER}" "MENDER_BOOT_PART" } disk_data_part_device() { - disk_get_part_device "${MENDER_DATA_PART_NUMBER}" "MENDER_DATA_PART" + disk_get_part_device "${MENDER_DATA_PART_NUMBER}" "MENDER_DATA_PART" } disk_root_part_a_device() { - disk_get_part_device "${MENDER_ROOTFS_PART_A_NUMBER}" "MENDER_ROOTFS_PART_A" + disk_get_part_device "${MENDER_ROOTFS_PART_A_NUMBER}" "MENDER_ROOTFS_PART_A" } disk_root_part_b_device() { - disk_get_part_device "${MENDER_ROOTFS_PART_B_NUMBER}" "MENDER_ROOTFS_PART_B" + disk_get_part_device "${MENDER_ROOTFS_PART_B_NUMBER}" "MENDER_ROOTFS_PART_B" } - # Get device partition number from device path. # Unrecognized or unsupported device paths will generate an error # @@ -291,28 +283,28 @@ disk_root_part_b_device() { disk_get_device_part_number() { dev_part="unknown" case "$1" in - /dev/nvme*n*p* ) + /dev/nvme*n*p*) dev_part=$(echo $1 | cut -dp -f2) ;; - /dev/mmcblk*p* ) + /dev/mmcblk*p*) dev_part=$(echo $1 | cut -dp -f2) ;; - /dev/[sh]d[a-z][1-9]* ) + /dev/[sh]d[a-z][1-9]*) dev_part=${1##*d[a-z]} ;; - ubi*_* ) + ubi*_*) dev_part=$(echo $1 | cut -d_ -f2) ;; - /dev/disk/by-partuuid/* ) - if disk_is_valid_partuuid_dos_device "$1";then - dev_part=$(disk_get_partuuid_dos_part_number "$1") - dev_part=$((16#${dev_part})) + /dev/disk/by-partuuid/*) + if disk_is_valid_partuuid_dos_device "$1"; then + dev_part=$(disk_get_partuuid_dos_part_number "$1") + dev_part=$((16#${dev_part})) else - log_fatal "partition number does not exist for GPT partuuid: '$1'" + log_fatal "partition number does not exist for GPT partuuid: '$1'" fi ;; esac - part=$(printf "%d" $dev_part 2>/dev/null) + part=$(printf "%d" $dev_part 2> /dev/null) if [ $? = 1 ]; then log_fatal "Could not determine partition number from '${1}'" else @@ -327,19 +319,19 @@ disk_get_device_part_number() { disk_get_device_base() { dev_base="" case "$1" in - /dev/nvme*n*p* ) + /dev/nvme*n*p*) dev_base=$(echo $1 | cut -dp -f1) ;; - /dev/mmcblk*p* ) + /dev/mmcblk*p*) dev_base=$(echo $1 | cut -dp -f1) ;; - /dev/[sh]d[a-z][1-9]* ) + /dev/[sh]d[a-z][1-9]*) dev_base=${1%%[1-9]*} ;; - ubi*_* ) + ubi*_*) dev_base=$(echo $1 | cut -d_ -f1) ;; - /dev/disk/by-partuuid/* ) + /dev/disk/by-partuuid/*) log_fatal "device base does not exist for GPT partuuid: '$1'" ;; esac @@ -350,20 +342,19 @@ disk_get_device_base() { fi } - # Conditionally redefine partition number/device if fine grained variable set. # # $1 - variable name # $2 - variable value disk_override_partition_variable() { - if [ "${MENDER_ENABLE_PARTUUID}" == "y" ]; then - if disk_is_valid_partuuid_dos_device "${2}";then - eval "${1}"=$(disk_get_device_part_number "${2}") - fi - else - if [ -n "${2}" ];then - eval "${1}"=$(disk_get_device_part_number "${2}") - MENDER_STORAGE_DEVICE_BASE=$(disk_get_device_base "${2}") - fi - fi + if [ "${MENDER_ENABLE_PARTUUID}" == "y" ]; then + if disk_is_valid_partuuid_dos_device "${2}"; then + eval "${1}"=$(disk_get_device_part_number "${2}") + fi + else + if [ -n "${2}" ]; then + eval "${1}"=$(disk_get_device_part_number "${2}") + MENDER_STORAGE_DEVICE_BASE=$(disk_get_device_base "${2}") + fi + fi } diff --git a/modules/git.sh b/modules/git.sh index 289e6b8..154f202 100644 --- a/modules/git.sh +++ b/modules/git.sh @@ -14,9 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. - # Print the mender convert version # git_mender_convert_version() { - git describe --tags --dirty --exact-match 2>/dev/null || git rev-parse --short HEAD + git describe --tags --dirty --exact-match 2> /dev/null || git rev-parse --short HEAD } diff --git a/modules/log.sh b/modules/log.sh index 00adc8c..9bab06a 100755 --- a/modules/log.sh +++ b/modules/log.sh @@ -18,7 +18,6 @@ # from sub-directories log_file="${MENDER_CONVERT_LOG_FILE:-${PWD}/work/convert.log}" - # Add some colour to the log messages YELLOW='\033[1;33m' # Warning @@ -28,50 +27,50 @@ NC='\033[0m' # No Color # Log the given message at the given level. function log { - local -r level="$1" - local -r message="$2" - local -r timestamp=$(date +"%Y-%m-%d %H:%M:%S") - local -r script_name="$(basename "$0")" - echo -e "${timestamp} [${level}] [$script_name] ${message}" >> ${log_file} - >&2 echo -e "${timestamp} [${level}] [$script_name] ${message}" + local -r level="$1" + local -r message="$2" + local -r timestamp=$(date +"%Y-%m-%d %H:%M:%S") + local -r script_name="$(basename "$0")" + echo -e "${timestamp} [${level}] [$script_name] ${message}" >> ${log_file} + echo >&2 -e "${timestamp} [${level}] [$script_name] ${message}" } function local_log_debug { - local -r level="DEBUG" - local -r message="$1" - local -r timestamp=$(date +"%Y-%m-%d %H:%M:%S") - local -r script_name="$(basename "$0")" - echo -e "${timestamp} [${level}] [$script_name] ${message}" >> ${log_file} + local -r level="DEBUG" + local -r message="$1" + local -r timestamp=$(date +"%Y-%m-%d %H:%M:%S") + local -r script_name="$(basename "$0")" + echo -e "${timestamp} [${level}] [$script_name] ${message}" >> ${log_file} } # Log the given message at DEBUG level. function log_debug { - local -r message="$1" - local_log_debug "$message" + local -r message="$1" + local_log_debug "$message" } # Log the given message at INFO level. function log_info { - local -r message="$1" - log "INFO" "$message" + local -r message="$1" + log "INFO" "$message" } # Log the given message at WARN level. function log_warn { - local -r message="$1" - log "${YELLOW}WARN${NC}" "$message" + local -r message="$1" + log "${YELLOW}WARN${NC}" "$message" } # Log the given message at ERROR level. function log_error { - local -r message="$1" - log "${RED}ERROR${NC}" "$message" + local -r message="$1" + log "${RED}ERROR${NC}" "$message" } # Log the given message at FATAL level. FATAL_EXIT_CODE=90 function log_fatal { - local -r message="$1" - log "${IRED}FATAL${NC}" "$message" - exit ${FATAL_EXIT_CODE} + local -r message="$1" + log "${IRED}FATAL${NC}" "$message" + exit ${FATAL_EXIT_CODE} } diff --git a/modules/probe.sh b/modules/probe.sh index 5f3f898..4a71aab 100644 --- a/modules/probe.sh +++ b/modules/probe.sh @@ -19,34 +19,34 @@ # No input parameters and these work on the assumption that boot and root parts # are mounted at work/boot and work/rootfs probe_arch() { - # --dereference, means to follow symlinks because 'ls' could be a symlink - # to busybox - file_info="" - for location in bin/ls usr/bin/ls; do - if [ -L work/rootfs/${location} ]; then - location=$(readlink work/rootfs/${location}) - fi - if [ -e work/rootfs/${location} ]; then - file_info=$(file -b --dereference work/rootfs/${location}) - break - fi - done + # --dereference, means to follow symlinks because 'ls' could be a symlink + # to busybox + file_info="" + for location in bin/ls usr/bin/ls; do + if [ -L work/rootfs/${location} ]; then + location=$(readlink work/rootfs/${location}) + fi + if [ -e work/rootfs/${location} ]; then + file_info=$(file -b --dereference work/rootfs/${location}) + break + fi + done - if [ -z "${file_info}" ]; then - log_fatal "Sorry, not able to determine target architecture" - fi + if [ -z "${file_info}" ]; then + log_fatal "Sorry, not able to determine target architecture" + fi - target_arch="unknown" - if grep -q x86-64 <<< "${file_info}"; then - target_arch="x86-64" - elif grep -Eq "ELF 32-bit.*ARM" <<< "${file_info}"; then - target_arch="arm" - elif grep -Eq "ELF 64-bit.*aarch64" <<< "${file_info}"; then - target_arch="aarch64" - else - log_fatal "Unsupported architecture: ${file_info}" - fi - echo "${target_arch}" + target_arch="unknown" + if grep -q x86-64 <<< "${file_info}"; then + target_arch="x86-64" + elif grep -Eq "ELF 32-bit.*ARM" <<< "${file_info}"; then + target_arch="arm" + elif grep -Eq "ELF 64-bit.*aarch64" <<< "${file_info}"; then + target_arch="aarch64" + else + log_fatal "Unsupported architecture: ${file_info}" + fi + echo "${target_arch}" } # Prints GRUB EFI name depending on target architecture @@ -54,23 +54,23 @@ probe_arch() { # No input parameters and these work on the assumption that boot and root parts # are mounted at work/boot and work/rootfs probe_grub_efi_name() { - efi_name="" - arch=$(probe_arch) - case "${arch}" in - "x86-64") - efi_name="grub-efi-bootx64.efi" - ;; - "arm") - efi_name="grub-efi-bootarm.efi" - ;; - "aarch64") - efi_name="grub-efi-bootaa64.efi" - ;; - *) - log_fatal "Unknown arch: ${arch}" - ;; - esac - echo "$efi_name" + efi_name="" + arch=$(probe_arch) + case "${arch}" in + "x86-64") + efi_name="grub-efi-bootx64.efi" + ;; + "arm") + efi_name="grub-efi-bootarm.efi" + ;; + "aarch64") + efi_name="grub-efi-bootaa64.efi" + ;; + *) + log_fatal "Unknown arch: ${arch}" + ;; + esac + echo "$efi_name" } # Prints Debian arch name depending on target architecture @@ -78,23 +78,23 @@ probe_grub_efi_name() { # No input parameters and these work on the assumption that boot and root parts # are mounted at work/boot and work/rootfs probe_debian_arch_name() { - deb_arch="" - arch=$(probe_arch) - case "${arch}" in - "x86-64") - deb_arch="amd64" - ;; - "arm") - deb_arch="armhf" - ;; - "aarch64") - deb_arch="arm64" - ;; - *) - log_fatal "Unknown arch: ${arch}" - ;; - esac - echo "${deb_arch}" + deb_arch="" + arch=$(probe_arch) + case "${arch}" in + "x86-64") + deb_arch="amd64" + ;; + "arm") + deb_arch="armhf" + ;; + "aarch64") + deb_arch="arm64" + ;; + *) + log_fatal "Unknown arch: ${arch}" + ;; + esac + echo "${deb_arch}" } # Prints GRUB EFI target name depending on target architecture @@ -104,23 +104,23 @@ probe_debian_arch_name() { # No input parameters and these work on the assumption that boot and root parts # are mounted at work/boot and work/rootfs probe_grub_efi_target_name() { - efi_target_name="" - arch=$(probe_arch) - case "$arch" in - "x86-64") - efi_target_name="bootx64.efi" - ;; - "arm") - efi_target_name="bootarm.efi" - ;; - "aarch64") - efi_target_name="bootaa64.efi" - ;; - *) - log_fatal "Unknown arch: ${arch}" - ;; - esac - echo "$efi_target_name" + efi_target_name="" + arch=$(probe_arch) + case "$arch" in + "x86-64") + efi_target_name="bootx64.efi" + ;; + "arm") + efi_target_name="bootarm.efi" + ;; + "aarch64") + efi_target_name="bootaa64.efi" + ;; + *) + log_fatal "Unknown arch: ${arch}" + ;; + esac + echo "$efi_target_name" } # Prints path to the Linux kernel image @@ -128,36 +128,36 @@ probe_grub_efi_target_name() { # $1 - directory in which the search is performed # probe_kernel_image() { - kernel_image_path="" - for image in vmlinuz zImage bzImage; do - # Linux kernel image type and naming varies between different platforms. - # - # The wildcard at the end is important, because it is common to suffix the - # Linux kernel version to the image type/name, e.g: - # - # vmlinuz-4.14-x86_64 - # vmlinuz-3.10.0-862.el7.x86_64 - # vmlinuz-4.15.0-20-generic - # - #Search for kernels, resolve symlinks, ignore invalid and select the latest if many - kernels=$(sudo find ${1} -name ${image}* ! -name '*-rescue-*' ! -name '*.old' \ + kernel_image_path="" + for image in vmlinuz zImage bzImage; do + # Linux kernel image type and naming varies between different platforms. + # + # The wildcard at the end is important, because it is common to suffix the + # Linux kernel version to the image type/name, e.g: + # + # vmlinuz-4.14-x86_64 + # vmlinuz-3.10.0-862.el7.x86_64 + # vmlinuz-4.15.0-20-generic + # + #Search for kernels, resolve symlinks, ignore invalid and select the latest if many + kernels=$(sudo find ${1} -name ${image}* ! -name '*-rescue-*' ! -name '*.old' \ -exec readlink -f {} \; | awk -F '/' '{print $NF,$0}' | sort -k1rV | uniq) - kernel_image_path=$(head -n 1 <<< "$kernels" | cut -f2- -d' ') - n=$(wc -l <<< "$kernels") + kernel_image_path=$(head -n 1 <<< "$kernels" | cut -f2- -d' ') + n=$(wc -l <<< "$kernels") - if [ "$n" -gt "1" ];then - msg=$(awk '{printf " %s\n", $2}' <<< ${kernels}) - log_warn "Found multiple kernel images: \n\n${msg}\n" - log_warn "Selecting newest kernel image: \n\n ${kernel_image_path}\n" - log_warn "Set MENDER_GRUB_KERNEL_IMAGETYPE to override selected kernel" - fi + if [ "$n" -gt "1" ]; then + msg=$(awk '{printf " %s\n", $2}' <<< ${kernels}) + log_warn "Found multiple kernel images: \n\n${msg}\n" + log_warn "Selecting newest kernel image: \n\n ${kernel_image_path}\n" + log_warn "Set MENDER_GRUB_KERNEL_IMAGETYPE to override selected kernel" + fi - #kernel_image_path=$(sudo find ${1} -name ${image}* ! -name '*-rescue-*' | sort -rV | head -n 1 ) - if [ -n "${kernel_image_path}" ]; then - break - fi - done - echo "${kernel_image_path}" + #kernel_image_path=$(sudo find ${1} -name ${image}* ! -name '*-rescue-*' | sort -rV | head -n 1 ) + if [ -n "${kernel_image_path}" ]; then + break + fi + done + echo "${kernel_image_path}" } # Prints path to the initrd/initramfs image @@ -165,33 +165,33 @@ probe_kernel_image() { # $1 - directory in which the search is performed # probe_initrd_image() { - initrd_image_path="" - for image in initramfs initrd; do - # initrd/initramfs naming varies between different platforms. - # - # The wildcard at the end is important, because it is common to suffix the - # Linux kernel version to the image name, e.g: - # - # initrd.img-4.15.0-20-generic - # - #Search for initrd, resolve symlinks, ignore invalid and select the latest if many - initrds=$(sudo find ${1} -name ${image}* ! -name '*-rescue-*' ! -name '*.old' \ + initrd_image_path="" + for image in initramfs initrd; do + # initrd/initramfs naming varies between different platforms. + # + # The wildcard at the end is important, because it is common to suffix the + # Linux kernel version to the image name, e.g: + # + # initrd.img-4.15.0-20-generic + # + #Search for initrd, resolve symlinks, ignore invalid and select the latest if many + initrds=$(sudo find ${1} -name ${image}* ! -name '*-rescue-*' ! -name '*.old' \ -exec readlink -f {} \; | awk -F '/' '{print $NF,$0}' | sort -k1rV | uniq) - initrd_image_path=$(head -n 1 <<< "$initrds" | cut -f2- -d' ') - n=$(wc -l <<< "$initrds") + initrd_image_path=$(head -n 1 <<< "$initrds" | cut -f2- -d' ') + n=$(wc -l <<< "$initrds") - if [ "$n" -gt "1" ];then - msg=$(awk '{printf " %s\n", $2}' <<< ${initrds}) - log_warn "Found multiple initrd images: \n\n${msg}\n" - log_warn "Selecting newest initrd: \n\n ${initrd_image_path}\n" - log_warn "Set MENDER_GRUB_INITRD_IMAGETYPE to override selected initrd" - fi + if [ "$n" -gt "1" ]; then + msg=$(awk '{printf " %s\n", $2}' <<< ${initrds}) + log_warn "Found multiple initrd images: \n\n${msg}\n" + log_warn "Selecting newest initrd: \n\n ${initrd_image_path}\n" + log_warn "Set MENDER_GRUB_INITRD_IMAGETYPE to override selected initrd" + fi - if [ -n "${initrd_image_path}" ]; then - break - fi - done - echo "${initrd_image_path}" + if [ -n "${initrd_image_path}" ]; then + break + fi + done + echo "${initrd_image_path}" } # Prints Linux kernel image name @@ -202,36 +202,36 @@ probe_initrd_image() { # No input parameters and these work on the assumption that boot and root parts # are mounted at work/boot and work/rootfs probe_kernel_in_boot_and_root() { - kernel_imagetype_path="" + kernel_imagetype_path="" - # Important to check rootfs/boot first, because it might be possible that - # they are stored in both partitions, and in this case we want to find the - # image in the rootfs first and use that, to avoid copying it over from - # boot part when it is already there. - for boot in work/rootfs/boot work/boot; do - kernel_imagetype_path=$(probe_kernel_image ${boot}) - if [ -n "${kernel_imagetype_path}" ] && [ "${boot}" == "work/boot" ]; then - log_info "Found Linux kernel image in boot part, moving to rootfs/boot" - sudo cp ${kernel_imagetype_path} work/rootfs/boot - break; - elif [ -n "${kernel_imagetype_path}" ]; then - break; - fi - done + # Important to check rootfs/boot first, because it might be possible that + # they are stored in both partitions, and in this case we want to find the + # image in the rootfs first and use that, to avoid copying it over from + # boot part when it is already there. + for boot in work/rootfs/boot work/boot; do + kernel_imagetype_path=$(probe_kernel_image ${boot}) + if [ -n "${kernel_imagetype_path}" ] && [ "${boot}" == "work/boot" ]; then + log_info "Found Linux kernel image in boot part, moving to rootfs/boot" + sudo cp ${kernel_imagetype_path} work/rootfs/boot + break + elif [ -n "${kernel_imagetype_path}" ]; then + break + fi + done - if [ -z "${kernel_imagetype_path}" ]; then - log_warn "Unfortunately we where not able to find the Linux kernel image." - log_fatal "Please specify the image name using MENDER_GRUB_KERNEL_IMAGETYPE" - fi + if [ -z "${kernel_imagetype_path}" ]; then + log_warn "Unfortunately we where not able to find the Linux kernel image." + log_fatal "Please specify the image name using MENDER_GRUB_KERNEL_IMAGETYPE" + fi - log_info "Found Linux kernel image: \n\n\t${kernel_imagetype_path}\n" - kernel_imagetype=$(basename ${kernel_imagetype_path}) + log_info "Found Linux kernel image: \n\n\t${kernel_imagetype_path}\n" + kernel_imagetype=$(basename ${kernel_imagetype_path}) - if [ "${MENDER_GRUB_EFI_INTEGRATION}" == "y" ]; then - check_efi_compatible_kernel ${kernel_imagetype_path} - fi + if [ "${MENDER_GRUB_EFI_INTEGRATION}" == "y" ]; then + check_efi_compatible_kernel ${kernel_imagetype_path} + fi - echo "${kernel_imagetype}" + echo "${kernel_imagetype}" } # Prints initrd/initramfs image name @@ -242,82 +242,82 @@ probe_kernel_in_boot_and_root() { # No input parameters and these work on the assumption that boot and root parts # are mounted at work/boot and work/rootfs probe_initrd_in_boot_and_root() { - initrd_image_path="" + initrd_image_path="" - # Important to check rootfs/boot first, because it might be possible that - # they are stored in both partitions, and in this case we want to find the - # image in the rootfs first and use that, to avoid copying it over from - # boot part when it is already there. - for boot in work/rootfs/boot work/boot; do - initrd_image_path=$(probe_initrd_image ${boot}) - if [ -n "${initrd_image_path}" ] && [ "${boot}" == "work/boot" ]; then - sudo cp ${initrd_image_path} work/rootfs/boot - break; - elif [ -n "${initrd_image_path}" ]; then - break; - fi - done + # Important to check rootfs/boot first, because it might be possible that + # they are stored in both partitions, and in this case we want to find the + # image in the rootfs first and use that, to avoid copying it over from + # boot part when it is already there. + for boot in work/rootfs/boot work/boot; do + initrd_image_path=$(probe_initrd_image ${boot}) + if [ -n "${initrd_image_path}" ] && [ "${boot}" == "work/boot" ]; then + sudo cp ${initrd_image_path} work/rootfs/boot + break + elif [ -n "${initrd_image_path}" ]; then + break + fi + done - if [ -n "${initrd_image_path}" ]; then - log_info "Found initramfs image: \n\n\t${initrd_image_path}\n" - initrd_imagetype=$(basename ${initrd_image_path}) - else - log_info "Unfortunately we where not able to find the initrd image." - log_info "Please specify the image name using MENDER_GRUB_INITRD_IMAGETYPE \ + if [ -n "${initrd_image_path}" ]; then + log_info "Found initramfs image: \n\n\t${initrd_image_path}\n" + initrd_imagetype=$(basename ${initrd_image_path}) + else + log_info "Unfortunately we where not able to find the initrd image." + log_info "Please specify the image name using MENDER_GRUB_INITRD_IMAGETYPE \ (only required if your board is using this)" - initrd_imagetype="" - fi + initrd_imagetype="" + fi - echo "${initrd_imagetype}" + echo "${initrd_imagetype}" } check_for_broken_uboot_uefi_support() { - if ! is_uboot_with_uefi_support "$@"; then - local log_level=log_fatal - if [ "$MENDER_IGNORE_UBOOT_BROKEN_UEFI" = 1 ]; then - log_level=log_warn + if ! is_uboot_with_uefi_support "$@"; then + local log_level=log_fatal + if [ "$MENDER_IGNORE_UBOOT_BROKEN_UEFI" = 1 ]; then + log_level=log_warn + fi + $log_level 'Detected a U-Boot version in the range v2018.09 - v2019.07. These U-Boot versions are known to have broken UEFI support, and therefore the MENDER_GRUB_EFI_INTEGRATION feature is unlikely to work. This only affects newly flashed devices using the partition image (extension ending in "img"). The Mender artifact should still work to upgrade an existing, working device. There are two possible workarounds for this issue: 1) Use either an older or a newer image that works, and use a Mender artifact afterwards to up/down-grade it to the version you want. 2) If the device has a non-UEFI U-Boot port in mender-convert, use that (look for a board specific file in `configs`) . If you want to ignore this error and force creation of the image, set the MENDER_IGNORE_UBOOT_BROKEN_UEFI=1 config option.' fi - $log_level 'Detected a U-Boot version in the range v2018.09 - v2019.07. These U-Boot versions are known to have broken UEFI support, and therefore the MENDER_GRUB_EFI_INTEGRATION feature is unlikely to work. This only affects newly flashed devices using the partition image (extension ending in "img"). The Mender artifact should still work to upgrade an existing, working device. There are two possible workarounds for this issue: 1) Use either an older or a newer image that works, and use a Mender artifact afterwards to up/down-grade it to the version you want. 2) If the device has a non-UEFI U-Boot port in mender-convert, use that (look for a board specific file in `configs`) . If you want to ignore this error and force creation of the image, set the MENDER_IGNORE_UBOOT_BROKEN_UEFI=1 config option.' - fi } is_uboot_with_uefi_support() { - local path="$1" + local path="$1" - # Broken UEFI support in range v2018.09 - v2019.07 (see MEN-2404) - local regex='U-Boot 20(18\.(09|1[0-2])|19\.0[1-7])' + # Broken UEFI support in range v2018.09 - v2019.07 (see MEN-2404) + local regex='U-Boot 20(18\.(09|1[0-2])|19\.0[1-7])' - if egrep -qr "$regex" "$path"; then - return 1 - fi - return 0 + if egrep -qr "$regex" "$path"; then + return 1 + fi + return 0 } check_efi_compatible_kernel() { - if ! is_efi_compatible_kernel "$@"; then - local log_level=log_fatal - if [ "$MENDER_IGNORE_MISSING_EFI_STUB" = 1 ]; then - log_level=log_warn + if ! is_efi_compatible_kernel "$@"; then + local log_level=log_fatal + if [ "$MENDER_IGNORE_MISSING_EFI_STUB" = 1 ]; then + log_level=log_warn + fi + $log_level 'Detected a kernel which does not have an EFI stub. This kernel is not supported when booting with UEFI. Please consider using a U-Boot port if the board has one (look for a board specific file in `configs`), or find a kernel which has the CONFIG_EFI_STUB turned on. To ignore this message and proceed anyway, set the MENDER_IGNORE_MISSING_EFI_STUB=1 config option.' fi - $log_level 'Detected a kernel which does not have an EFI stub. This kernel is not supported when booting with UEFI. Please consider using a U-Boot port if the board has one (look for a board specific file in `configs`), or find a kernel which has the CONFIG_EFI_STUB turned on. To ignore this message and proceed anyway, set the MENDER_IGNORE_MISSING_EFI_STUB=1 config option.' - fi } is_efi_compatible_kernel() { - kernel_path="$1" + kernel_path="$1" - case "$(probe_arch)" in - arm|aarch64) - # On ARM, as of version 2.04, GRUB can only boot kernels which have an EFI - # stub in them. See MEN-2404. - if ! file -k $kernel_path | fgrep -q 'EFI application'; then - return 1 - fi - ;; - *) - # Other platforms are fine. - : - ;; - esac - return 0 + case "$(probe_arch)" in + arm | aarch64) + # On ARM, as of version 2.04, GRUB can only boot kernels which have an EFI + # stub in them. See MEN-2404. + if ! file -k $kernel_path | fgrep -q 'EFI application'; then + return 1 + fi + ;; + *) + # Other platforms are fine. + : + ;; + esac + return 0 } diff --git a/modules/run.sh b/modules/run.sh index d430470..a52efd3 100644 --- a/modules/run.sh +++ b/modules/run.sh @@ -18,16 +18,16 @@ # # $1 - command to run function run_and_log_cmd() { - local -r cmd="${1}" - local -r position="(${BASH_SOURCE[1]}:${BASH_LINENO[0]}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }" - local exit_code=0 - output="$({ eval ${cmd}; } 2>&1)" || exit_code=$? - local log_msg="Running: ${position} \n\r\n\r\t${cmd}" - if [[ "${output}" != "" ]]; then - log_msg="${log_msg}\n\t${output}\n" - fi - log_debug "${log_msg}" - if [[ ${exit_code} -ne 0 ]]; then - exit ${exit_code} - fi + local -r cmd="${1}" + local -r position="(${BASH_SOURCE[1]}:${BASH_LINENO[0]}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }" + local exit_code=0 + output="$({ eval ${cmd}; } 2>&1)" || exit_code=$? + local log_msg="Running: ${position} \n\r\n\r\t${cmd}" + if [[ "${output}" != "" ]]; then + log_msg="${log_msg}\n\t${output}\n" + fi + log_debug "${log_msg}" + if [[ ${exit_code} -ne 0 ]]; then + exit ${exit_code} + fi } diff --git a/modules/testscfg.sh b/modules/testscfg.sh index e6b82ba..f2adee0 100644 --- a/modules/testscfg.sh +++ b/modules/testscfg.sh @@ -15,9 +15,9 @@ # Init tests configuration file # -function testscfg_init () { - rm -f work/testscfg.tmp - touch work/testscfg.tmp +function testscfg_init() { + rm -f work/testscfg.tmp + touch work/testscfg.tmp } # Add key/value to tests configuration file @@ -25,23 +25,23 @@ function testscfg_init () { # $1 - key # $2 - value # -function testscfg_add () { - if [[ $# -ne 2 ]]; then - log_fatal "testscfg_add() requires 2 arguments" - fi - local -r key="${1}" - local -r value="${2}" - echo "${key}=\"${value}\"" >> work/testscfg.tmp +function testscfg_add() { + if [[ $# -ne 2 ]]; then + log_fatal "testscfg_add() requires 2 arguments" + fi + local -r key="${1}" + local -r value="${2}" + echo "${key}=\"${value}\"" >> work/testscfg.tmp } # Save tests configuration file into the given location # # $1 - Filename # -function testscfg_save () { - if [[ $# -ne 1 ]]; then - log_fatal "testscfg_save() requires 1 argument" - fi - local -r filename="${1}" - cp work/testscfg.tmp ${filename} +function testscfg_save() { + if [[ $# -ne 1 ]]; then + log_fatal "testscfg_save() requires 1 argument" + fi + local -r filename="${1}" + cp work/testscfg.tmp ${filename} } diff --git a/modules/zip.sh b/modules/zip.sh index 736bdfe..a1ac960 100644 --- a/modules/zip.sh +++ b/modules/zip.sh @@ -20,15 +20,15 @@ # # @return - Name of the img contained in the archive # -function zip_get_imgname () { - if [[ $# -ne 1 ]]; then - log_fatal "zip_get_imgname requires one argument" - fi - local -r disk_image="${1}" - # Assert that the archive holds only one file - nfiles="$(unzip -l ${disk_image} | awk '{nfiles=$2} END {print nfiles}')" - [[ "$nfiles" -ne 1 ]] && log_fatal "Zip archive has more than one file. Needs to be unzipped by a human. nfiles: $nfiles" - local -r filename="$(unzip -lq ${disk_image} | awk 'NR==3 {filename=$NF} END {print filename}')" - [[ ${filename} == *.img ]] || log_fatal "no img file found in the zip archive ${disk_image}." - echo "$(basename ${filename})" +function zip_get_imgname() { + if [[ $# -ne 1 ]]; then + log_fatal "zip_get_imgname requires one argument" + fi + local -r disk_image="${1}" + # Assert that the archive holds only one file + nfiles="$(unzip -l ${disk_image} | awk '{nfiles=$2} END {print nfiles}')" + [[ "$nfiles" -ne 1 ]] && log_fatal "Zip archive has more than one file. Needs to be unzipped by a human. nfiles: $nfiles" + local -r filename="$(unzip -lq ${disk_image} | awk 'NR==3 {filename=$NF} END {print filename}')" + [[ ${filename} == *.img ]] || log_fatal "no img file found in the zip archive ${disk_image}." + echo "$(basename ${filename})" } diff --git a/scripts/bootstrap-rootfs-overlay-demo-server.sh b/scripts/bootstrap-rootfs-overlay-demo-server.sh index 03ae6e4..c87d999 100755 --- a/scripts/bootstrap-rootfs-overlay-demo-server.sh +++ b/scripts/bootstrap-rootfs-overlay-demo-server.sh @@ -17,39 +17,39 @@ # Exit if any command exits with a non-zero exit status. set -o errexit -root_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )/../" && pwd ) +root_dir=$( cd "$( dirname "${BASH_SOURCE[0]}")/../" && pwd) if [ "${root_dir}" != "${PWD}" ]; then - echo "You must execute $(basename $0) from the root directory: ${root_dir}" - exit 1 + echo "You must execute $(basename $0) from the root directory: ${root_dir}" + exit 1 fi server_ip="" output_dir="" -while (( "$#" )); do - case "$1" in - -o | --output-dir) - output_dir="${2}" - shift 2 - ;; - -s | --server-ip) - server_ip="${2}" - shift 2 - ;; - *) - echo "Sorry but the provided option is not supported: $1" - echo "Usage: $(basename $0) --output-dir --server-ip " - exit 1 - ;; - esac +while (("$#")); do + case "$1" in + -o | --output-dir) + output_dir="${2}" + shift 2 + ;; + -s | --server-ip) + server_ip="${2}" + shift 2 + ;; + *) + echo "Sorry but the provided option is not supported: $1" + echo "Usage: $(basename $0) --output-dir --server-ip " + exit 1 + ;; + esac done if [ -z "${output_dir}" ]; then - echo "Sorry, but you need to provide an output directory using the '-o/--output-dir' option" - exit 1 + echo "Sorry, but you need to provide an output directory using the '-o/--output-dir' option" + exit 1 fi if [ -z "${server_ip}" ]; then - echo "Sorry, but you need to provide a server IP address using the '-s/--server-ip' option" - exit 1 + echo "Sorry, but you need to provide a server IP address using the '-s/--server-ip' option" + exit 1 fi if [ -e ${output_dir} ]; then diff --git a/scripts/bootstrap-rootfs-overlay-hosted-server.sh b/scripts/bootstrap-rootfs-overlay-hosted-server.sh index f95b81f..a6fdb92 100755 --- a/scripts/bootstrap-rootfs-overlay-hosted-server.sh +++ b/scripts/bootstrap-rootfs-overlay-hosted-server.sh @@ -17,40 +17,40 @@ # Exit if any command exits with a non-zero exit status. set -o errexit -root_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )/../" && pwd ) +root_dir=$( cd "$( dirname "${BASH_SOURCE[0]}")/../" && pwd) if [ "${root_dir}" != "${PWD}" ]; then - echo "You must execute $(basename $0) from the root directory: ${root_dir}" - exit 1 + echo "You must execute $(basename $0) from the root directory: ${root_dir}" + exit 1 fi tenant_token="" output_dir="" -while (( "$#" )); do - case "$1" in - -t | --tenant-token) - tenant_token="${2}" - shift 2 - ;; - -o | --output-dir) - output_dir="${2}" - shift 2 - ;; - *) - echo "Sorry but the provided option is not supported: $1" - echo "Usage: $(basename $0) --output-dir ./rootfs_overlay_demo --tenant-token " - exit 1 - ;; - esac +while (("$#")); do + case "$1" in + -t | --tenant-token) + tenant_token="${2}" + shift 2 + ;; + -o | --output-dir) + output_dir="${2}" + shift 2 + ;; + *) + echo "Sorry but the provided option is not supported: $1" + echo "Usage: $(basename $0) --output-dir ./rootfs_overlay_demo --tenant-token " + exit 1 + ;; + esac done if [ -z "${output_dir}" ]; then - echo "Sorry, but you need to provide an output directory using the '-o/--output-dir' option" - exit 1 + echo "Sorry, but you need to provide an output directory using the '-o/--output-dir' option" + exit 1 fi if [ -z "${tenant_token}" ]; then - echo "Sorry, but you need to provide a tenant token using the '-t/--tenant-token' option" - exit 1 + echo "Sorry, but you need to provide a tenant token using the '-t/--tenant-token' option" + exit 1 fi if [ -e ${output_dir} ]; then diff --git a/scripts/bootstrap-rootfs-overlay-production-server.sh b/scripts/bootstrap-rootfs-overlay-production-server.sh index a3c5b7f..212468c 100755 --- a/scripts/bootstrap-rootfs-overlay-production-server.sh +++ b/scripts/bootstrap-rootfs-overlay-production-server.sh @@ -17,44 +17,44 @@ # Exit if any command exits with a non-zero exit status. set -o errexit -root_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )/../" && pwd ) +root_dir=$( cd "$( dirname "${BASH_SOURCE[0]}")/../" && pwd) if [ "${root_dir}" != "${PWD}" ]; then - echo "You must execute $(basename $0) from the root directory: ${root_dir}" - exit 1 + echo "You must execute $(basename $0) from the root directory: ${root_dir}" + exit 1 fi server_url="" output_dir="" -while (( "$#" )); do - case "$1" in - -o | --output-dir) - output_dir="${2}" - shift 2 - ;; - -s | --server-url) - server_url="${2}" - shift 2 - ;; - -S | --server-cert) - server_cert="${2}" - shift 2 - ;; - *) - echo "Sorry but the provided option is not supported: $1" - echo "Usage: $(basename $0) --output-dir ./rootfs_overlay_demo --server-url [--server-cert ]" - exit 1 - ;; - esac +while (("$#")); do + case "$1" in + -o | --output-dir) + output_dir="${2}" + shift 2 + ;; + -s | --server-url) + server_url="${2}" + shift 2 + ;; + -S | --server-cert) + server_cert="${2}" + shift 2 + ;; + *) + echo "Sorry but the provided option is not supported: $1" + echo "Usage: $(basename $0) --output-dir ./rootfs_overlay_demo --server-url [--server-cert ]" + exit 1 + ;; + esac done if [ -z "${output_dir}" ]; then - echo "Sorry, but you need to provide an output directory using the '-o/--output-dir' option" - exit 1 + echo "Sorry, but you need to provide an output directory using the '-o/--output-dir' option" + exit 1 fi if [ -z "${server_url}" ]; then - echo "Sorry, but you need to provide a server URL using the '-s/--server-url' option" - exit 1 + echo "Sorry, but you need to provide a server URL using the '-s/--server-url' option" + exit 1 fi if [ -e ${output_dir} ]; then @@ -70,10 +70,10 @@ cat <<- EOF > ${output_dir}/etc/mender/mender.conf EOF if [ -n "${server_cert}" ]; then - cat <<- EOF >> ${output_dir}/etc/mender/mender.conf + cat <<- EOF >> ${output_dir}/etc/mender/mender.conf "ServerCertificate": "/etc/mender/server.crt", EOF - cp -f "${server_cert}" ${output_dir}/etc/mender/server.crt + cp -f "${server_cert}" ${output_dir}/etc/mender/server.crt fi cat <<- EOF >> ${output_dir}/etc/mender/mender.conf