From c828effd23635a6164a720aa90bc09ea85fefa5e Mon Sep 17 00:00:00 2001 From: Artem Smirnov Date: Fri, 21 Sep 2018 19:19:48 +0300 Subject: [PATCH] image_builder: split image_config.sh & standardization headers --- image_builder/autosizer.sh | 8 +- image_builder/build.sh | 8 +- image_builder/echo_stamp.sh | 26 ++++ image_builder/get_new_image.sh | 32 +++++ image_builder/image_config.sh | 153 +--------------------- image_builder/publish_image.sh | 64 +++++++++ image_builder/resize_fs.sh | 57 ++++++++ image_builder/scripts/builder.sh | 8 +- image_builder/scripts/hardware_setup.sh | 3 +- image_builder/scripts/init_image.sh | 10 +- image_builder/scripts/init_rpi.sh | 10 +- image_builder/scripts/network_setup.sh | 8 +- image_builder/scripts/ros_install.sh | 8 +- image_builder/scripts/software_install.sh | 8 +- 14 files changed, 247 insertions(+), 156 deletions(-) create mode 100644 image_builder/echo_stamp.sh create mode 100644 image_builder/get_new_image.sh create mode 100644 image_builder/publish_image.sh create mode 100644 image_builder/resize_fs.sh diff --git a/image_builder/autosizer.sh b/image_builder/autosizer.sh index 92e3c118..2e8298fa 100755 --- a/image_builder/autosizer.sh +++ b/image_builder/autosizer.sh @@ -1,5 +1,11 @@ -#!/bin/bash +#! /usr/bin/env bash +# +# Script for image configure +# @urpylka Artem Smirnov +# + +# Exit immidiately on non-zero result set -e echo_bold() { diff --git a/image_builder/build.sh b/image_builder/build.sh index a14bdad4..c7e51d48 100755 --- a/image_builder/build.sh +++ b/image_builder/build.sh @@ -1,5 +1,11 @@ #! /usr/bin/env bash +# +# Script for image configure +# @urpylka Artem Smirnov +# + +# Exit immidiately on non-zero result set -e export DEBIAN_FRONTEND=${DEBIAN_FRONTEND:='noninteractive'} @@ -26,7 +32,7 @@ git clone ${TARGET_REPO} --single-branch --branch ${TARGET_REF} --depth 1 ${REPO export IMAGE_VERSION="${TARGET_REF}_$(date '+%Y%m%d_%H%M%S')" export IMAGE_PATH="$(pwd)/image/$(basename -s ".git" ${TARGET_REPO})_${IMAGE_VERSION}.img" -./image_config.sh get_image ${IMAGE_PATH} $(jq '.source_image' -r ${TARGET_CONFIG}) +./get_new_image ${IMAGE_PATH} $(jq '.source_image' -r ${TARGET_CONFIG}) REGISTER=':arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-arm-static:' if [[ $(arch) != 'armv7l' ]]; then diff --git a/image_builder/echo_stamp.sh b/image_builder/echo_stamp.sh new file mode 100644 index 00000000..2fe68eeb --- /dev/null +++ b/image_builder/echo_stamp.sh @@ -0,0 +1,26 @@ +#! /usr/bin/env bash + +# +# Script for image configure +# @urpylka Artem Smirnov +# + +echo_stamp() { + # TEMPLATE: echo_stamp + # TYPE: SUCCESS, ERROR, INFO + + # More info there https://www.shellhacks.com/ru/bash-colors/ + + TEXT="$(date) | $1" + TEXT="\e[1m$TEXT\e[0m" # BOLD + + case "$2" in + SUCCESS) + TEXT="\e[32m${TEXT}\e[0m";; # GREEN + ERROR) + TEXT="\e[31m${TEXT}\e[0m";; # RED + *) + TEXT="\e[34m${TEXT}\e[0m";; # BLUE + esac + echo -e ${TEXT} +} \ No newline at end of file diff --git a/image_builder/get_new_image.sh b/image_builder/get_new_image.sh new file mode 100644 index 00000000..9883f2e5 --- /dev/null +++ b/image_builder/get_new_image.sh @@ -0,0 +1,32 @@ +#! /usr/bin/env bash + +# +# Script for image configure +# @urpylka Artem Smirnov +# @dvornikov-aa Andrey Dvornikov +# + +# Exit immidiately on non-zero result +set -e + +source echo_stamp.sh + +get_image() { + # TEMPLATE: get_image + local BUILD_DIR=$(dirname $1) + local RPI_ZIP_NAME=$(basename $2) + if [ ! -e "${BUILD_DIR}/${RPI_ZIP_NAME}" ]; + then + echo_stamp "1. Downloading original Linux distribution" + wget -nv -O ${BUILD_DIR}/${RPI_ZIP_NAME} $2 > /dev/null \ + && echo_stamp "Downloading complete" "SUCCESS" + else + echo_stamp "1. Linux distribution already donwloaded" + fi + echo_stamp "2. Unzipping Linux distribution image" + local RPI_IMAGE_NAME=$(echo ${RPI_ZIP_NAME} | sed 's/zip/img/') + unzip -p ${BUILD_DIR}/${RPI_ZIP_NAME} ${RPI_IMAGE_NAME} > $1 + echo_stamp "Unzipping complete" "SUCCESS" +} + +get_image $1 $2 diff --git a/image_builder/image_config.sh b/image_builder/image_config.sh index ad292508..947f5b82 100755 --- a/image_builder/image_config.sh +++ b/image_builder/image_config.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#! /usr/bin/env bash # # Script for image configure @@ -6,6 +6,11 @@ # @dvornikov-aa Andrey Dvornikov # +# Exit immidiately on non-zero result +set -e + +source echo_stamp.sh + # This script doesn't work on Ubuntu because OS`s losetup does not consist --partscan (-P). # Idea: use `mount -o loop,offset` @@ -14,90 +19,6 @@ # mount -t ext4 -o loop,offset=$((94208 * 512)) image/clever_qemu_test_2_20180822_163141.img "$REPO_DIR" # mount -t vfat -o loop,offset=$((8192 * 512)) image/clever_qemu_test_2_20180822_163141.img "$REPO_DIR/boot" -# Exit immidiately on non-zero result -set -e - -echo_stamp() { - # TEMPLATE: echo_stamp - # TYPE: SUCCESS, ERROR, INFO - - # More info there https://www.shellhacks.com/ru/bash-colors/ - - TEXT="$(date) | $1" - TEXT="\e[1m$TEXT\e[0m" # BOLD - - case "$2" in - SUCCESS) - TEXT="\e[32m${TEXT}\e[0m";; # GREEN - ERROR) - TEXT="\e[31m${TEXT}\e[0m";; # RED - *) - TEXT="\e[34m${TEXT}\e[0m";; # BLUE - esac - echo -e ${TEXT} -} - -get_image() { - # TEMPLATE: get_image - local BUILD_DIR=$(dirname $1) - local RPI_ZIP_NAME=$(basename $2) - if [ ! -e "${BUILD_DIR}/${RPI_ZIP_NAME}" ]; - then - echo_stamp "1. Downloading original Linux distribution" - wget -nv -O ${BUILD_DIR}/${RPI_ZIP_NAME} $2 > /dev/null \ - && echo_stamp "Downloading complete" "SUCCESS" - else - echo_stamp "1. Linux distribution already donwloaded" - fi - echo_stamp "2. Unzipping Linux distribution image" - local RPI_IMAGE_NAME=$(echo ${RPI_ZIP_NAME} | sed 's/zip/img/') - unzip -p ${BUILD_DIR}/${RPI_ZIP_NAME} ${RPI_IMAGE_NAME} > $1 - echo_stamp "Unzipping complete" "SUCCESS" -} - -resize_fs() { - # TEMPLATE: resize_fs - - set +e - - # https://ru.wikipedia.org/wiki/%D0%A0%D0%B0%D0%B7%D1%80%D0%B5%D0%B6%D1%91%D0%BD%D0%BD%D1%8B%D0%B9_%D1%84%D0%B0%D0%B9%D0%BB - - # https://raspberrypi.stackexchange.com/questions/13137/how-can-i-mount-a-raspberry-pi-linux-distro-image - # fdisk -l 2017-11-29-raspbian-stretch-lite.img - # https://www.stableit.ru/2011/05/losetup.html - # -f : losetup сам выбрал loop (минуя занятые) - # -P : losetup монтирует разделы в образе как отдельные подразделы, - # например /dev/loop0p1 и /dev/loop0p2 - # --show : печатает имя устройства, например /dev/loop4 - - # http://karelzak.blogspot.ru/2015/05/resize-by-sfdisk.html - # ", +" : expand partition for volume size - # -N 2 : select second partition for work - - # There is a risk that sfdisk will ask for a disk remount to update partition table - # TODO: Check sfdisk exit code - - echo_stamp "Truncate image" \ - && truncate -s$2 $1 \ - && echo_stamp "Mount loop-image: $1" \ - && local DEV_IMAGE=$(losetup -Pf $1 --show) \ - && sleep 0.5 \ - && echo ", +" | sfdisk -N 2 ${DEV_IMAGE} \ - && sleep 0.5 \ - && losetup -d ${DEV_IMAGE} \ - && sleep 0.5 \ - && local DEV_IMAGE=$(losetup -Pf $1 --show) \ - && sleep 0.5 \ - && echo_stamp "Check & repair filesystem after expand partition" \ - && e2fsck -fvy "${DEV_IMAGE}p2" \ - && echo_stamp "Expand filesystem" \ - && resize2fs "${DEV_IMAGE}p2" \ - && echo_stamp "Umount loop-image" \ - && losetup -d ${DEV_IMAGE} - - set -e -} - execute() { # TEMPLATE: execute <...> @@ -220,56 +141,6 @@ umount_system() { losetup -d $2 } -publish_image() { - # TEMPLATE: publish_image_bash - - # https://developer.github.com/v3/repos/releases/ - - IMAGE_NAME=$(basename $1) - BUILD_DIR=$(dirname $1) - - echo_stamp "Zip image" - if [ ! -e "${BUILD_DIR}/${IMAGE_NAME}.zip" ]; - then - cd ${BUILD_DIR} && zip ${IMAGE_NAME}.zip ${IMAGE_NAME} - echo_stamp "Zipping complete!" "SUCCESS" - else - echo_stamp "Zip-archive already created" - cd ${BUILD_DIR} && rm ${IMAGE_NAME}.zip && zip ${IMAGE_NAME}.zip ${IMAGE_NAME} \ - && echo_stamp "Old archive was deleted & create new" "SUCCESS" - fi - - echo_stamp "Upload image" - local IMAGE_LINK=$($2 $3 ${BUILD_DIR}/${IMAGE_NAME}.zip) \ - && echo_stamp "Upload copmlete!" "SUCCESS" - - echo_stamp "Meashure size of zip-image" - local IMAGE_SIZE=$(du -sh ${BUILD_DIR}/${IMAGE_NAME}.zip | awk '{ print $1 }') \ - && echo_stamp "Meashuring copmlete!" "SUCCESS" - - echo_stamp "Meashure hash-sum of zip-image" - local IMAGE_HASH=$(sha256sum ${BUILD_DIR}/${IMAGE_NAME}.zip | awk '{ print $1 }') \ - && echo_stamp "Meashuring copmlete!" "SUCCESS" - - echo "" - echo "\$5: $5" - echo "" - - echo_stamp "Post message to GH" - local NEW_RELEASE_BODY="### Download\n* [${IMAGE_NAME}.zip]($IMAGE_LINK) ($IMAGE_SIZE)\nsha256: $IMAGE_HASH\n\n$5" - local DATA="{ \"body\":\"$NEW_RELEASE_BODY\" }" - - echo "" - echo "\$DATA: $DATA" - echo "" - - local GH_LOGIN=$(cat $3 | jq '.github.login' -r) - local GH_PASS=$(cat $3 | jq '.github.password' -r) - local GH_URL=$(cat $3 | jq '.github.url' -r) - curl -d "$DATA" -u "$GH_LOGIN:$GH_PASS" --request PATCH $GH_URL$4 \ - && echo_stamp "Post message to GH copmlete!" "SUCCESS" -} - if [ $(whoami) != "root" ]; then echo "" \ && echo "********************************************************************" \ @@ -284,18 +155,6 @@ for ((i=1; i<=$#; i++)); do echo "\$$i: ${!i}"; done echo "================================================================================" case "$1" in - get_image) - # get_image - get_image $2 $3;; - - resize_fs) - # resize_fs - resize_fs $2 $3;; - - publish_image) - # publish_image - publish_image $2 $3 $4 $5 "$6";; - execute) # execute [] [...] execute $2 $3 ${@:4};; diff --git a/image_builder/publish_image.sh b/image_builder/publish_image.sh new file mode 100644 index 00000000..017701be --- /dev/null +++ b/image_builder/publish_image.sh @@ -0,0 +1,64 @@ +#! /usr/bin/env bash + +# +# Script for image configure +# @urpylka Artem Smirnov +# @dvornikov-aa Andrey Dvornikov +# + +# Exit immidiately on non-zero result +set -e + +source echo_stamp.sh + +publish_image() { + # TEMPLATE: publish_image_bash + + # https://developer.github.com/v3/repos/releases/ + + IMAGE_NAME=$(basename $1) + BUILD_DIR=$(dirname $1) + + echo_stamp "Zip image" + if [ ! -e "${BUILD_DIR}/${IMAGE_NAME}.zip" ]; + then + cd ${BUILD_DIR} && zip ${IMAGE_NAME}.zip ${IMAGE_NAME} + echo_stamp "Zipping complete!" "SUCCESS" + else + echo_stamp "Zip-archive already created" + cd ${BUILD_DIR} && rm ${IMAGE_NAME}.zip && zip ${IMAGE_NAME}.zip ${IMAGE_NAME} \ + && echo_stamp "Old archive was deleted & create new" "SUCCESS" + fi + + echo_stamp "Upload image" + local IMAGE_LINK=$($2 $3 ${BUILD_DIR}/${IMAGE_NAME}.zip) \ + && echo_stamp "Upload copmlete!" "SUCCESS" + + echo_stamp "Meashure size of zip-image" + local IMAGE_SIZE=$(du -sh ${BUILD_DIR}/${IMAGE_NAME}.zip | awk '{ print $1 }') \ + && echo_stamp "Meashuring copmlete!" "SUCCESS" + + echo_stamp "Meashure hash-sum of zip-image" + local IMAGE_HASH=$(sha256sum ${BUILD_DIR}/${IMAGE_NAME}.zip | awk '{ print $1 }') \ + && echo_stamp "Meashuring copmlete!" "SUCCESS" + + echo "" + echo "\$5: $5" + echo "" + + echo_stamp "Post message to GH" + local NEW_RELEASE_BODY="### Download\n* [${IMAGE_NAME}.zip](${IMAGE_LINK}) (${IMAGE_SIZE})\nsha256: ${IMAGE_HASH}\n\n$5" + local DATA="{ \"body\":\"${NEW_RELEASE_BODY}\" }" + + echo "" + echo "\$DATA: $DATA" + echo "" + + local GH_LOGIN=$(cat $3 | jq '.github.login' -r) + local GH_PASS=$(cat $3 | jq '.github.password' -r) + local GH_URL=$(cat $3 | jq '.github.url' -r) + curl -d "$DATA" -u "${GH_LOGIN}:${GH_PASS}" --request PATCH ${GH_URL}$4 \ + && echo_stamp "Post message to GH copmlete!" "SUCCESS" +} + +publish_image $1 $2 $3 $4 "$5" diff --git a/image_builder/resize_fs.sh b/image_builder/resize_fs.sh new file mode 100644 index 00000000..e7ec3c0d --- /dev/null +++ b/image_builder/resize_fs.sh @@ -0,0 +1,57 @@ +#! /usr/bin/env bash + +# +# Script for image configure +# @urpylka Artem Smirnov +# @dvornikov-aa Andrey Dvornikov +# + +# Exit immidiately on non-zero result +set -e + +source echo_stamp.sh + +resize_fs() { + # TEMPLATE: resize_fs + + set +e + + # https://ru.wikipedia.org/wiki/%D0%A0%D0%B0%D0%B7%D1%80%D0%B5%D0%B6%D1%91%D0%BD%D0%BD%D1%8B%D0%B9_%D1%84%D0%B0%D0%B9%D0%BB + + # https://raspberrypi.stackexchange.com/questions/13137/how-can-i-mount-a-raspberry-pi-linux-distro-image + # fdisk -l 2017-11-29-raspbian-stretch-lite.img + # https://www.stableit.ru/2011/05/losetup.html + # -f : losetup сам выбрал loop (минуя занятые) + # -P : losetup монтирует разделы в образе как отдельные подразделы, + # например /dev/loop0p1 и /dev/loop0p2 + # --show : печатает имя устройства, например /dev/loop4 + + # http://karelzak.blogspot.ru/2015/05/resize-by-sfdisk.html + # ", +" : expand partition for volume size + # -N 2 : select second partition for work + + # There is a risk that sfdisk will ask for a disk remount to update partition table + # TODO: Check sfdisk exit code + + echo_stamp "Truncate image" \ + && truncate -s$2 $1 \ + && echo_stamp "Mount loop-image: $1" \ + && local DEV_IMAGE=$(losetup -Pf $1 --show) \ + && sleep 0.5 \ + && echo ", +" | sfdisk -N 2 ${DEV_IMAGE} \ + && sleep 0.5 \ + && losetup -d ${DEV_IMAGE} \ + && sleep 0.5 \ + && local DEV_IMAGE=$(losetup -Pf $1 --show) \ + && sleep 0.5 \ + && echo_stamp "Check & repair filesystem after expand partition" \ + && e2fsck -fvy "${DEV_IMAGE}p2" \ + && echo_stamp "Expand filesystem" \ + && resize2fs "${DEV_IMAGE}p2" \ + && echo_stamp "Umount loop-image" \ + && losetup -d ${DEV_IMAGE} + + set -e +} + +resize_fs $1 $2 diff --git a/image_builder/scripts/builder.sh b/image_builder/scripts/builder.sh index 7103f173..ccc030ff 100755 --- a/image_builder/scripts/builder.sh +++ b/image_builder/scripts/builder.sh @@ -1,11 +1,17 @@ #! /usr/bin/env bash +# +# Script for image configure +# @urpylka Artem Smirnov +# + +# Exit immidiately on non-zero result set -e cd ${IMAGE_BUILDER} # Make free space -./image_config.sh resize_fs ${IMAGE_PATH} '7G' +./resize_fs.sh ${IMAGE_PATH} '7G' ./image_config.sh copy_to_chroot ${IMAGE_PATH} ${SCRIPTS_DIR}'/init_rpi.sh' '/root/' ./image_config.sh copy_to_chroot ${IMAGE_PATH} ${SCRIPTS_DIR}'/hardware_setup.sh' '/root/' diff --git a/image_builder/scripts/hardware_setup.sh b/image_builder/scripts/hardware_setup.sh index d147cfd8..7ad390ec 100755 --- a/image_builder/scripts/hardware_setup.sh +++ b/image_builder/scripts/hardware_setup.sh @@ -1,10 +1,11 @@ -#!/bin/bash +#! /usr/bin/env bash # # Script for image configure # @urpylka Artem Smirnov # +# Exit immidiately on non-zero result set -e echo_stamp() { diff --git a/image_builder/scripts/init_image.sh b/image_builder/scripts/init_image.sh index 86d6e8cf..6e120a68 100755 --- a/image_builder/scripts/init_image.sh +++ b/image_builder/scripts/init_image.sh @@ -1,4 +1,12 @@ -#!/bin/bash +#! /usr/bin/env bash + +# +# Script for image configure +# @urpylka Artem Smirnov +# + +# Exit immidiately on non-zero result +set -e ################################################################################################################################## # Image initialisation diff --git a/image_builder/scripts/init_rpi.sh b/image_builder/scripts/init_rpi.sh index dbe18a43..8e61fd1f 100755 --- a/image_builder/scripts/init_rpi.sh +++ b/image_builder/scripts/init_rpi.sh @@ -1,4 +1,12 @@ -#!/bin/bash +#! /usr/bin/env bash + +# +# Script for image configure +# @urpylka Artem Smirnov +# + +# Exit immidiately on non-zero result +set -e echo_stamp() { # TEMPLATE: echo_stamp diff --git a/image_builder/scripts/network_setup.sh b/image_builder/scripts/network_setup.sh index b8344902..7a6a22ca 100755 --- a/image_builder/scripts/network_setup.sh +++ b/image_builder/scripts/network_setup.sh @@ -1,5 +1,11 @@ -#!/bin/bash +#! /usr/bin/env bash +# +# Script for image configure +# @urpylka Artem Smirnov +# + +# Exit immidiately on non-zero result set -e echo_stamp() { diff --git a/image_builder/scripts/ros_install.sh b/image_builder/scripts/ros_install.sh index 0e4dbb71..ee222ba1 100755 --- a/image_builder/scripts/ros_install.sh +++ b/image_builder/scripts/ros_install.sh @@ -1,5 +1,11 @@ -#!/usr/bin/env bash +#! /usr/bin/env bash +# +# Script for image configure +# @urpylka Artem Smirnov +# + +# Exit immidiately on non-zero result set -e echo_stamp() { diff --git a/image_builder/scripts/software_install.sh b/image_builder/scripts/software_install.sh index 29828630..a077b5ff 100755 --- a/image_builder/scripts/software_install.sh +++ b/image_builder/scripts/software_install.sh @@ -1,5 +1,11 @@ -#!/usr/bin/env bash +#! /usr/bin/env bash +# +# Script for image configure +# @urpylka Artem Smirnov +# + +# Exit immidiately on non-zero result set -e ##################################################################################################################################