From d5ed615a64f5e1fea80263b9a5a8e677fc232e31 Mon Sep 17 00:00:00 2001 From: olivier Date: Wed, 29 Apr 2020 14:23:15 +0200 Subject: [PATCH 1/7] Improve the way to detect the 'boot' disk device id for the mac (s1 could be hardcoded for Hypriot images, which also works for official Ubuntu 20.04 RPI images. However, some images like Ubuntu 18.04 of rock64 create the boot disk as '/dev/disk2s6') --- flash | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/flash b/flash index 2cc3484..a7b0abd 100755 --- a/flash +++ b/flash @@ -219,7 +219,9 @@ case "${OSTYPE}" in # @param arg1 the name of the device holding the volume to be mounted # @return _RET mount point name get_boot_mount_point() { - _RET=$(df | grep --color=never "${1}s1" | /usr/bin/sed 's,.*/Volumes,/Volumes,') + local device_id="${1}" + local disk_id=$(mount | grep "${device_id}" | grep msdos | cut -f1 -d' ') + _RET=$(df | grep --color=never "${disk_id}" | /usr/bin/sed 's,.*/Volumes,/Volumes,') } # Wait for the new created disk to be available From 031d7f35e04b83f015e797cb03679bd9684c2638 Mon Sep 17 00:00:00 2001 From: olivier Date: Wed, 29 Apr 2020 19:23:54 +0200 Subject: [PATCH 2/7] Improve the way to detect the 'boot' disk device id for linux (1 could be hardcoded for Hypriot images, but not for other images) --- flash | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/flash b/flash index a7b0abd..6ef9131 100755 --- a/flash +++ b/flash @@ -416,16 +416,11 @@ case "${OSTYPE}" in # # @param arg1 the disk name containing the partition find_boot_dev_name() { - if beginswith /dev/mmcblk "${1}" ;then - _RET="${1}p1" - elif beginswith /dev/loop "${1}" ;then - _RET="${1}p1" - else - _RET="${1}1" - fi + local device_id="${1}" + local disk_id=$(sudo fdisk "${device_id}" -l -o device,type 2>/dev/null | grep "Microsoft" | head -n 1 | cut -f1 -d' ') + _RET="${disk_id}" } - # Unmount a disk # # @param arg1 the disk to unmount From ef06f4d382fd47b3dd5dfacc45f797dbaad77b89 Mon Sep 17 00:00:00 2001 From: olivier Date: Wed, 29 Apr 2020 19:44:21 +0200 Subject: [PATCH 3/7] Fix shellcheck issues --- flash | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/flash b/flash index 6ef9131..d805e5d 100755 --- a/flash +++ b/flash @@ -220,7 +220,8 @@ case "${OSTYPE}" in # @return _RET mount point name get_boot_mount_point() { local device_id="${1}" - local disk_id=$(mount | grep "${device_id}" | grep msdos | cut -f1 -d' ') + local disk_id + disk_id=$(mount | grep "${device_id}" | grep msdos | cut -f1 -d' ') _RET=$(df | grep --color=never "${disk_id}" | /usr/bin/sed 's,.*/Volumes,/Volumes,') } @@ -417,7 +418,8 @@ case "${OSTYPE}" in # @param arg1 the disk name containing the partition find_boot_dev_name() { local device_id="${1}" - local disk_id=$(sudo fdisk "${device_id}" -l -o device,type 2>/dev/null | grep "Microsoft" | head -n 1 | cut -f1 -d' ') + local disk_id + disk_id=$(sudo fdisk "${device_id}" -l -o device,type 2>/dev/null | grep "Microsoft" | head -n 1 | cut -f1 -d' ') _RET="${disk_id}" } From 384b1dcf7d86ab955e80511b86dbe9198e65b112 Mon Sep 17 00:00:00 2001 From: olivier Date: Sun, 3 May 2020 11:02:20 +0200 Subject: [PATCH 4/7] Add package-lock.json to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 91d3ea3..509ec32 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ loo wifi.yml flash-wifi.sh flash.sh +package-lock.json From 9f755a3765f23c3923aa211152edd62bc99d5720 Mon Sep 17 00:00:00 2001 From: olivier Date: Tue, 2 Jun 2020 13:02:09 +0200 Subject: [PATCH 5/7] Align code with master + improve linux find_boot_dev_name method implementation --- flash | 67 +++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/flash b/flash index 2cc3484..1449b2f 100755 --- a/flash +++ b/flash @@ -141,7 +141,6 @@ fi case "${OSTYPE}" in darwin*) - alias grep="grep --color=never" size_opt="-f %z" bs_size=1m @@ -153,12 +152,16 @@ case "${OSTYPE}" in validate_yaml() { set +e - if _RET=$(ruby -e "require 'yaml';YAML.load_file('$1')" 2>&1); then - set -e - return 0 + if [[ $(sed -n '/^#cloud-config/p;q' "$1") ]]; then + if _RET=$(ruby -e "require 'yaml';YAML.load_file('$1')" 2>&1); then + set -e + return 0 + fi + echo "File $1 is not a valid YAML file!" + echo "$_RET" | grep --color=never -v "from " + else + echo "File $1 is not a valid YAML file! It must contain #cloud-config in the first line." fi - echo "File $1 is not a valid YAML file!" - echo "$_RET" | grep -v "from " set -e false } @@ -183,7 +186,7 @@ case "${OSTYPE}" in # Show in the standard output the devices that are a likely # destination for the tool to write an image to. show_devices() { - diskutil list | grep FDisk_partition_scheme | awk 'NF>1{print $NF}' + diskutil list | grep --color=never FDisk_partition_scheme | awk 'NF>1{print $NF}' } # Check that the target device can be written. It will return 0 in @@ -197,7 +200,7 @@ case "${OSTYPE}" in _RET=1 return fi - readonlymedia=$(diskutil info "$disk" | grep "Read-Only Media" | awk 'NF>1{print $NF}') + readonlymedia=$(diskutil info "$disk" | grep --color=never "Read-Only Media" | awk 'NF>1{print $NF}') if [[ $readonlymedia == "No" ]] ; then _RET=1 else @@ -219,7 +222,10 @@ case "${OSTYPE}" in # @param arg1 the name of the device holding the volume to be mounted # @return _RET mount point name get_boot_mount_point() { - _RET=$(df | grep --color=never "${1}s1" | /usr/bin/sed 's,.*/Volumes,/Volumes,') + local device_id="${1}" + local disk_id=$(mount | grep "${device_id}" | grep msdos | cut -f1 -d' ') + _RET=$(df | grep --color=never "${disk_id}" | /usr/bin/sed 's,.*/Volumes,/Volumes,') + # _RET=$(df | grep --color=never "${1}s1" | /usr/bin/sed 's,.*/Volumes,/Volumes,') } # Wait for the new created disk to be available @@ -232,7 +238,7 @@ case "${OSTYPE}" in mv "$rawdisk" "${rawdisk}.readonly.dmg" hdiutil convert "${rawdisk}.readonly.dmg" -format UDRW -o "$rawdisk" rm -f "${rawdisk}.readonly.dmg" - disk=$(hdiutil attach "$rawdisk" | grep FAT | sed 's/s1 .*$//') + disk=$(hdiutil attach "$rawdisk" | grep --color=never FAT | sed 's/s1 .*$//') echo mounted FAT partition to "$disk" if [ "$disk" == "" ]; then echo Failed attaching "$rawdisk" @@ -331,8 +337,16 @@ case "${OSTYPE}" in } validate_yaml() { - ## NO-OP in Linux - true + set +e + if [[ $(sed -n '/^#cloud-config/p;q' "$1") ]]; then + # no further yaml validation on Linux right now + set -e + return 0 + else + echo "File $1 is not a valid YAML file! It must contain #cloud-config in the first line." + fi + set -e + false } # Try to identify the most likely device that the user will use to @@ -340,7 +354,7 @@ case "${OSTYPE}" in # # @return _RET the name of the device to use autodetect_device() { - _RET=$(lsblk -p -n -o NAME -d | grep mmcblk || true) + _RET=$(lsblk -p -n -o NAME -d | grep --color=never mmcblk || true) } # Show in the standard output the devices that are a likely @@ -385,7 +399,7 @@ case "${OSTYPE}" in return fi - if sudo hdparm -r "$disk" | grep -q off; then + if sudo hdparm -r "$disk" | grep --color=never -q off; then _RET=1 else _RET=0 @@ -414,13 +428,22 @@ case "${OSTYPE}" in # # @param arg1 the disk name containing the partition find_boot_dev_name() { - if beginswith /dev/mmcblk "${1}" ;then - _RET="${1}p1" - elif beginswith /dev/loop "${1}" ;then - _RET="${1}p1" - else - _RET="${1}1" + local device_id="${1}" + local disk_id=$(sudo fdisk "${device_id}" -l -o device,type 2>/dev/null | grep "Microsoft" | head -n 1 | cut -f1 -d' ') + if [[ "$disk_id" == "" ]]; then + disk_id=$(sudo fdisk "${device_id}" -l -o device,type 2>/dev/null | grep "FAT32" | head -n 1 | cut -f1 -d' ') + fi + if [[ "$disk_id" == "" ]]; then + if beginswith /dev/mmcblk "${device_id}" ;then + disk_id="${device_id}p1" + elif beginswith /dev/loop "${device_id}" ;then + disk_id="${device_id}p1" + else + disk_id="${device_id}1" + fi fi + # echo "disk_id: $disk_id" + _RET="${disk_id}" } @@ -428,7 +451,7 @@ case "${OSTYPE}" in # # @param arg1 the disk to unmount umount_disk() { - for i in $(df |grep "$1" | awk '{print $1}') + for i in $(df |grep --color=never "$1" | awk '{print $1}') do sudo umount "$i" done @@ -585,7 +608,7 @@ if [[ -z $CONFIGURE_ONLY ]] ; then command -v 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=$(unzip -l "${image}" | grep --color=never -v Archive: | grep --color=never img | awk 'NF>1{print $NF}') image="/tmp/${image}" echo "Use ${image}" fi From 6adf31cdff73587c4c84b4c457a3dfdc8c8ba36c Mon Sep 17 00:00:00 2001 From: olivier Date: Tue, 2 Jun 2020 13:12:57 +0200 Subject: [PATCH 6/7] Fix spellchecks issues --- flash | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/flash b/flash index 1449b2f..121fc19 100755 --- a/flash +++ b/flash @@ -222,8 +222,10 @@ case "${OSTYPE}" in # @param arg1 the name of the device holding the volume to be mounted # @return _RET mount point name get_boot_mount_point() { - local device_id="${1}" - local disk_id=$(mount | grep "${device_id}" | grep msdos | cut -f1 -d' ') + local device_id + device_id="${1}" + local disk_id + disk_id=$(mount | grep "${device_id}" | grep msdos | cut -f1 -d' ') _RET=$(df | grep --color=never "${disk_id}" | /usr/bin/sed 's,.*/Volumes,/Volumes,') # _RET=$(df | grep --color=never "${1}s1" | /usr/bin/sed 's,.*/Volumes,/Volumes,') } @@ -428,8 +430,10 @@ case "${OSTYPE}" in # # @param arg1 the disk name containing the partition find_boot_dev_name() { - local device_id="${1}" - local disk_id=$(sudo fdisk "${device_id}" -l -o device,type 2>/dev/null | grep "Microsoft" | head -n 1 | cut -f1 -d' ') + local device_id + device_id="${1}" + local disk_id + disk_id=$(sudo fdisk "${device_id}" -l -o device,type 2>/dev/null | grep "Microsoft" | head -n 1 | cut -f1 -d' ') if [[ "$disk_id" == "" ]]; then disk_id=$(sudo fdisk "${device_id}" -l -o device,type 2>/dev/null | grep "FAT32" | head -n 1 | cut -f1 -d' ') fi From 552a513c3e713d42b4f20537c4cce93753a2f0ea Mon Sep 17 00:00:00 2001 From: olivier Date: Tue, 2 Jun 2020 13:38:24 +0200 Subject: [PATCH 7/7] Don't stop of errors --- flash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flash b/flash index 121fc19..1da7a85 100755 --- a/flash +++ b/flash @@ -433,9 +433,9 @@ case "${OSTYPE}" in local device_id device_id="${1}" local disk_id - disk_id=$(sudo fdisk "${device_id}" -l -o device,type 2>/dev/null | grep "Microsoft" | head -n 1 | cut -f1 -d' ') + disk_id=$(sudo fdisk "${device_id}" -l -o device,type 2>/dev/null | grep "Microsoft" | head -n 1 | cut -f1 -d' ') || true if [[ "$disk_id" == "" ]]; then - disk_id=$(sudo fdisk "${device_id}" -l -o device,type 2>/dev/null | grep "FAT32" | head -n 1 | cut -f1 -d' ') + disk_id=$(sudo fdisk "${device_id}" -l -o device,type 2>/dev/null | grep "FAT32" | head -n 1 | cut -f1 -d' ') || true fi if [[ "$disk_id" == "" ]]; then if beginswith /dev/mmcblk "${device_id}" ;then