Skip to content

Commit

Permalink
Fixes & update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
urpylka committed Aug 27, 2020
1 parent 951fdf4 commit 4d07c5c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# img-tool

Utilities for execute commands (amd64 & armhf) & resize the Raspbian images. Due to docker scripts it can work on Linux, macOS & etc.
The tool-bundle to execute commands (amd64 & armhf) & resize the Raspbian images. Due to docker scripts it can work on Linux, macOS & etc.

1. Now it only works with **Raspbian OS images** (due to filesystem partition scheme). Plans to make a more universal version (w arch-configs) for **Orange Pi**, **Nano Pi** and etc.

2. All plans of development you can find at [Github Projects](https://github.com/urpylka/img-tool/projects/1).

3. You can use it manualy or by scripts (example of using at [`img-builder`](https://github.com/urpylka/img-builder) project). Also this project is using in [COEX CLOVER](https://github.com/copterexpress/clover).

## API v0.6
## API v0.6.1

For easy using the docker image you need to make alias:

```bash
alias img-tool='docker run --privileged -it --rm -v $(pwd):/mnt urpylka/img-tool:0.6 img-tool'
alias img-tool='docker run --privileged -it --rm -v $(pwd):/mnt urpylka/img-tool:0.6.1 img-tool'
```

* You can add this command to `~/.bash_profile` or `~/.bashrc` to make it permanent.
Expand Down Expand Up @@ -47,7 +47,7 @@ The docker image consists **img-tool** script. It can be used for:
For automatic minimize the image use:

```bash
img-tool <IMG_PATH> size $(img-tool <IMG_PATH> size | head -1 | cut -b 15-)
img-tool <IMG_PATH> size $(img-tool <IMG_PATH> size | grep "IMG_MIN_SIZE" | cut -b 15-)
```

4. The script also consists additional functions who is needed for builds: `get_repo_ver`, `get_repo_url`, `get_repo_name`, `rich_echo`, `travis_retry`.
Expand All @@ -63,10 +63,10 @@ The docker image consists **img-tool** script. It can be used for:
## Build own version

```bash
docker build -t img-tool:local .
docker build -t urpylka/img-tool:local .
# Alias for ease using
alias img-tool='docker run --privileged -it --rm -v $(pwd):/mnt img-tool:local img-tool'
alias img-tool='docker run --privileged -it --rm -v $(pwd):/mnt urpylka/img-tool:local img-tool'
```

### Requirements in the Docker image
Expand Down
45 changes: 30 additions & 15 deletions img-tool
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

get_repo_ver() {
# TEMPLATE: get_repo_ver <REPO_PATH>
# If TRAVIS_TAG is not setted it will take the tag from git
# If TRAVIS_TAG is not set it will take the tag from git
echo ${TRAVIS_TAG:=$(cd ${1} && git log --format=%h -1)}
}

Expand Down Expand Up @@ -156,7 +156,11 @@ mount_image() {
# mount_image is executing function from arguments
# <FUNCTION> <MOUNT_POINT> [...]

local IMG_PATH=${1}
set -e

local IMG_PATH="${1}"
local FUNCTION="${2}"
local ARGS="${@:3}"

# It sets DEV_IMAGE and FILE_TYPE
get_block_device ${IMG_PATH}
Expand Down Expand Up @@ -193,19 +197,23 @@ mount_image() {
;;
esac

if [[ -z ${2} ]]; then
rich_echo "The function isn't setted" "RED"
if [[ -z ${FUNCTION} ]]; then
rich_echo "The function isn't set" "RED"
local RET=1
else
${2} ${MOUNT_POINT} ${@:3}
set +e
${FUNCTION} ${MOUNT_POINT} ${ARGS}
local RET=$?
set -e
fi

rich_echo "Unmount chroot rootfs and boot partition: ${MOUNT_POINT}" "BLUE"
travis_retry umount -fl ${MOUNT_POINT}
rmdir ${MOUNT_POINT}

loout

rich_echo "mount_image RET code: ${RET}" "BLUE"
return ${RET}
}

Expand Down Expand Up @@ -262,7 +270,7 @@ add_settings() {
&& rich_echo "OK" "GREEN" \
|| (rich_echo "Failed" "RED"; exit 1)

rich_echo "Copy DNS records " "BLUE" 1 \
rich_echo "Copy DNS records... " "BLUE" 1 \
&& cp -L /etc/resolv.conf ${MOUNT_POINT}/etc/resolv.conf \
&& rich_echo "OK" "GREEN" \
|| (rich_echo "Failed" "RED"; exit 1)
Expand All @@ -271,10 +279,15 @@ add_settings() {
load() {
# This func downloads & unzip zip-image
# TEMPLATE: load <IMG_PATH> <RPI_ZIP_URL>

[[ -z ${1} ]] && (echo "load: Wasn't set IMG_PATH"; exit 1)
[[ -z ${2} ]] && (echo "load: Wasn't set RPI_ZIP_URL"; exit 1)

local IMG_PATH=$1
local RPI_ZIP_URL=$2
local IMG_DIR=$(dirname $1)
local RPI_ZIP_NAME=$(basename $2)

local IMG_DIR=$(dirname ${IMG_PATH})
local RPI_ZIP_NAME=$(basename ${RPI_ZIP_URL})
local RPI_IMAGE_NAME=$(echo ${RPI_ZIP_NAME} | sed 's/zip/img/')

curl -I ${RPI_ZIP_URL} 2>/dev/null | grep "content-type: application/zip" 1>/dev/null \
Expand All @@ -297,22 +310,25 @@ load() {
}

exec() {
# TEMPLATE: exec <MOUNT_POINT> <EXECUTE_FILE> [...]
# TEMPLATE: exec <MOUNT_POINT> <EXECUTING_FILE> [...]

local MOUNT_POINT=${1}
local EXECUTING_FILE=${2}
local ARGS="${@:3}"

add_settings ${MOUNT_POINT}

local RET=0
set +e
mv ${MOUNT_POINT}/etc/ld.so.preload ${MOUNT_POINT}/etc/ld.so.preload.temp-moved

if [[ $# > 1 ]]; then
local SCRIPT_NAME="$(basename ${2}).$(tr -dc 'A-F0-9' < /dev/urandom | dd bs=1 count=7 2>/dev/null)"
local SCRIPT_NAME="$(basename ${EXECUTING_FILE}).$(tr -dc 'A-F0-9' < /dev/urandom | dd bs=1 count=7 2>/dev/null)"
local SCRIPT_PATH="/root/${SCRIPT_NAME}"

cp "${2}" "${MOUNT_POINT}/${SCRIPT_PATH}"
cp "${EXECUTING_FILE}" "${MOUNT_POINT}/${SCRIPT_PATH}"
rich_echo "Running the script" "BLUE"
chroot ${MOUNT_POINT} /bin/sh -c "${SCRIPT_PATH} ${@:3}" || RET=1
chroot ${MOUNT_POINT} /bin/sh -c "${SCRIPT_PATH} ${ARGS}" || RET=1
rm "${MOUNT_POINT}/${SCRIPT_PATH}"
else
# https://wiki.archlinux.org/index.php/Change_root_(%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9)
Expand Down Expand Up @@ -394,7 +410,6 @@ size() {
local CUR_IMG_SIZE=$((${LINUX_PART_END} + 512))
echo "CUR_IMG_SIZE: ${CUR_IMG_SIZE}"


if [[ ${FILE_TYPE} == "file" ]]; then
local OBJ_IMG_SIZE=`du -sb ${IMG_PATH} | awk -F' ' '{ print $1 }'`
# https://prowebmastering.ru/linux-uznat-razmer-papok-i-failov-v-terminale.html
Expand Down Expand Up @@ -527,14 +542,14 @@ get_version() {

help() {
echo "=========================================="
rich_echo "img-tool (version 0.6)" "BLUE"
rich_echo "img-tool (version 0.6.1)" "BLUE"
echo "=========================================="
echo "img-tool <IMAGE> exec [ <SCRIPT> [...] ]"
# TODO: Add ability executing commands from stdin
echo "img-tool <IMAGE> copy <MOVE_FILE> <MOVE_TO>"
echo "img-tool <IMAGE> size [ <NEW_SIZE> ]"
echo "To minimize image:"
echo "img-tool <IMAGE> size \$(img-tool <IMAGE> size | head -1 | cut -b 15-)"
echo "img-tool <IMAGE> size \$(img-tool <IMAGE> size | grep \"IMG_MIN_SIZE\" | cut -b 15-)"
echo "img-tool <IMAGE> load <RPI_ZIP_URL>"
echo "=========================================="
rich_echo "Additional functions:" "BLUE"
Expand Down

0 comments on commit 4d07c5c

Please sign in to comment.