From 1aaa5e8febb6045b3abd71b4087cb9771d71c4eb Mon Sep 17 00:00:00 2001 From: Luis Miguel Hernanz Date: Wed, 20 Dec 2017 02:23:20 +0100 Subject: [PATCH 1/2] If no image is specified, an existing image will be reconfigured --- Linux/flash | 101 ++++++++++++++++++++++++++++------------------------ README.md | 5 ++- 2 files changed, 59 insertions(+), 47 deletions(-) diff --git a/Linux/flash b/Linux/flash index 3e882fc..a0fa65a 100755 --- a/Linux/flash +++ b/Linux/flash @@ -16,7 +16,7 @@ error() usage() { cat << EOF -usage: $0 [options] name-of-rpi.img +usage: $0 [options] [name-of-rpi.img] Flash a local or remote Raspberry Pi SD card image. @@ -33,6 +33,10 @@ OPTIONS: --userdata|-u Copy this cloud-init file to /boot/user-data --metadata|-m Copy this cloud-init file to /boot/meta-data +If no image is specified, the script will try to configure an existing +image. This is useful to try several configuration without the need to +rewrite the image every time. + For HypriotOS < v1.7.0: The config file device-init.yaml should look like @@ -103,7 +107,8 @@ beginswith() { case $2 in $1*) true;; *) false;; esac; } endswith() { case $2 in *$1) true;; *) false;; esac; } if [ $# -lt 1 ]; then - usage + CONFIGURE_ONLY=1 + echo "Configuration mode. No image will be written" fi if [[ "$1" == "--help" ]]; then @@ -177,39 +182,41 @@ if [ ! -z "${CONFIG_FILE}" ]; then fi fi -if [ -f "/tmp/${filename}" ]; then - image=/tmp/${filename} - echo "Using cached image ${image}" -elif [ -f "/tmp/${filename}.img" ]; then - image=/tmp/${filename}.img - echo "Using cached image ${image}" -else - if beginswith http:// "${image}" || beginswith https:// "${image}"; then - which curl 2>/dev/null || error "Error: curl not found. Aborting" 1 - echo "Downloading ${image} ..." - curl -L --fail -o "/tmp/image.img.${extension}" "${image}" - image=/tmp/image.img.${extension} - fi +if [[ -z $CONFIGURE_ONLY ]] ; then + if [ -f "/tmp/${filename}" ]; then + image=/tmp/${filename} + echo "Using cached image ${image}" + elif [ -f "/tmp/${filename}.img" ]; then + image=/tmp/${filename}.img + echo "Using cached image ${image}" + else + if beginswith http:// "${image}" || beginswith https:// "${image}"; then + which curl 2>/dev/null || error "Error: curl not found. Aborting" 1 + echo "Downloading ${image} ..." + curl -L --fail -o "/tmp/image.img.${extension}" "${image}" + image=/tmp/image.img.${extension} + fi - if beginswith s3:// "${image}" ;then - which aws 2>/dev/null || error "Error: aws not found. Aborting" 1 - echo "Downloading ${image} ..." - aws s3 cp "${image}" "/tmp/image.img.${extension}" - image=/tmp/image.img.${extension} - fi + if beginswith s3:// "${image}" ;then + which aws 2>/dev/null || error "Error: aws not found. Aborting" 1 + echo "Downloading ${image} ..." + aws s3 cp "${image}" "/tmp/image.img.${extension}" + image=/tmp/image.img.${extension} + fi - if [ ! -f "${image}" ]; then - echo "File not found." - exit 10 - fi + if [ ! -f "${image}" ]; then + echo "File not found." + exit 10 + fi - if [[ "$(file "${image}")" == *"Zip archive"* ]]; then - which unzip 2>/dev/null || error "Error: unzip not found. Aborting" 1 - echo "Uncompressing ${image} ..." - unzip -o "${image}" -d /tmp - image=$(unzip -l "${image}" | grep -v Archive: | grep img | awk 'NF>1{print $NF}') - image="/tmp/${image}" - echo "Use ${image}" + if [[ "$(file "${image}")" == *"Zip archive"* ]]; then + which unzip 2>/dev/null || error "Error: unzip not found. Aborting" 1 + echo "Uncompressing ${image} ..." + unzip -o "${image}" -d /tmp + image=$(unzip -l "${image}" | grep -v Archive: | grep img | awk 'NF>1{print $NF}') + image="/tmp/${image}" + echo "Use ${image}" + fi fi fi @@ -256,22 +263,24 @@ while true; do fi done -echo "Flashing ${image} to ${disk} ..." -if [[ -x $(which pv) ]]; then - # this sudo here is used for a login without pv's progress bar - # hiding the password prompt - size=$(sudo stat -c %s "${image}") - pv -s "${size}" < "${image}" | sudo dd bs=1M "of=${disk}" -else - echo "No 'pv' command found, so no progress available." - echo "Press CTRL+T if you want to see the current info of dd command." - sudo dd bs=1M "if=${image}" "of=${disk}" -fi +if [[ -z $CONFIGURE_ONLY ]] ; then + echo "Flashing ${image} to ${disk} ..." + if [[ -x $(which pv) ]]; then + # this sudo here is used for a login without pv's progress bar + # hiding the password prompt + size=$(sudo stat -c %s "${image}") + pv -s "${size}" < "${image}" | sudo dd bs=1M "of=${disk}" + else + echo "No 'pv' command found, so no progress available." + echo "Press CTRL+T if you want to see the current info of dd command." + sudo dd bs=1M "if=${image}" "of=${disk}" + fi -echo "Waiting for device..." -udevadm settle + echo "Waiting for device..." + udevadm settle -sudo hdparm -z "${disk}" + sudo hdparm -z "${disk}" +fi echo "Mounting Disk" boot=/tmp/mnt.$$ diff --git a/README.md b/README.md index fb84f39..5e676b8 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ sudo pip install awscli ```bash $ flash --help -usage: flash [options] name-of-rpi.img +usage: flash [options] [name-of-rpi.img] Flash a local or remote Raspberry Pi SD card image. @@ -88,6 +88,9 @@ OPTIONS: --userdata|-u Copy this cloud-init config file to /boot/user-data --metadata|-m Copy this cloud-init config file to /boot/meta-data ``` +If no image is specified, the script will try to configure an existing +image. This is useful to try several configuration without the need to +rewrite the image every time. ## Configuration From 19552a2e7a6e9e277aeb2e88edd24d3ec96a6ead Mon Sep 17 00:00:00 2001 From: Luis Miguel Hernanz Date: Fri, 29 Dec 2017 02:35:20 +0100 Subject: [PATCH 2/2] Revert to the old behaviour where, when used with no parameters, usage is shown. --- Linux/flash | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Linux/flash b/Linux/flash index a0fa65a..2e0ae8e 100755 --- a/Linux/flash +++ b/Linux/flash @@ -107,8 +107,7 @@ beginswith() { case $2 in $1*) true;; *) false;; esac; } endswith() { case $2 in *$1) true;; *) false;; esac; } if [ $# -lt 1 ]; then - CONFIGURE_ONLY=1 - echo "Configuration mode. No image will be written" + usage fi if [[ "$1" == "--help" ]]; then @@ -116,6 +115,11 @@ if [[ "$1" == "--help" ]]; then fi image=$1 +if [[ -z $image ]]; then + CONFIGURE_ONLY=1 + echo "Configuration mode. No image will be written" +fi + filename=$(basename "${image}") extension="${filename##*.}" filename="${filename%.*}"