From 332908ae3d16e3340125adde410e51668242d915 Mon Sep 17 00:00:00 2001 From: JLKwong <61807745+JLKwong@users.noreply.github.com> Date: Fri, 12 Mar 2021 20:02:10 -0800 Subject: [PATCH] `treehouses clone detect` (fixes #2067) (#2083) Co-authored-by: dogi --- README.md | 2 +- _treehouses | 1 + modules/clone.sh | 82 ++++++++++++++++++++++++++++++------------------ modules/help.sh | 2 +- package.json | 2 +- 5 files changed, 55 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 29a75a98f..ec8ee8e66 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ ntp sets rpi to host timing locally or to networkmode outputs the current network mode button gives the gpio pin 18 an action feedback sends feedback -clone [device path] clones the current SD Card onto a secondary SD Card or specified device +clone [detect|device path] clones the current SD Card onto a secondary SD Card or specified device restore [device path] restores a treehouses image to an SD Card or specified device burn [device path] download and burns the latest treehouses image to the SD card or specified device rebootneeded shows if reboot is required to apply changes diff --git a/_treehouses b/_treehouses index c56582185..38d8a2feb 100644 --- a/_treehouses +++ b/_treehouses @@ -47,6 +47,7 @@ treehouses changelog treehouses changelog compare treehouses changelog view treehouses clone +treehouses clone detect treehouses config treehouses config add treehouses config clear diff --git a/modules/clone.sh b/modules/clone.sh index 636d17c91..3b7bbf5cd 100644 --- a/modules/clone.sh +++ b/modules/clone.sh @@ -1,41 +1,58 @@ function clone { - local device a b + local argument a b checkrpi checkroot checkargn $# 1 - device="$1" - if [ -z "$device" ]; then - device="/dev/sdb" - fi - - a=$(fdisk -l |grep /dev/mmcblk0: | grep -P '\d+ (?=bytes)' -o) - #echo "$a - /dev/mmcblk0" - - b=$(fdisk -l |grep "$device": | grep -P '\d+ (?=bytes)' -o) - #echo "$b - /dev/sdb" - - if [ -z "$a" ] || [ -z "$b" ]; then - echo "Error: the device $device wasn't detected. Please use 'fdisk -l' to find the device name." - return 1 - fi - - if [ $b -lt $a ]; then - echo "Error: the device $device is not big enough" - return 1 - fi - - if [ $a -eq $b ] || [ $a -lt $b ]; then - echo "copying...." - echo u > /proc/sysrq-trigger - dd if=/dev/mmcblk0 bs=1M of="$device" status=progress - fi - - echo ; echo "A reboot is needed to re-enable write permissions to OS." + argument="$1" + path="$(lsblk -lp | awk '{print $1}' | grep -o '/dev/sd[a-z]' | sort -u)" + + case $argument in + + "detect") + if [[ $path != *"dev"* ]]; then + echo "Error: Could not detect any devices. Try plugging your device into a different slot." + + else + echo "Detected the following devices:" + echo $path + fi + ;; + + *) + if [ -z "$argument" ]; then + argument="/dev/sdb" + fi + + a=$(fdisk -l |grep /dev/mmcblk0: | grep -P '\d+ (?=bytes)' -o) + #echo "$a - /dev/mmcblk0" + + b=$(fdisk -l |grep "$argument": | grep -P '\d+ (?=bytes)' -o) + #echo "$b - /dev/sdb" + + if [ -z "$a" ] || [ -z "$b" ]; then + echo "Error: the device $argument wasn't detected. Please use '$BASENAME clone detect' to find the device name." + return 1 + fi + + if [ $b -lt $a ]; then + echo "Error: the device $argument is not big enough" + return 1 + fi + + if [ $a -eq $b ] || [ $a -lt $b ]; then + echo "copying...." + echo u > /proc/sysrq-trigger + dd if=/dev/mmcblk0 bs=1M of="$argument" status=progress + fi + + echo ; echo "A reboot is needed to re-enable write permissions to OS." + ;; + esac } function clone_help { echo - echo "Usage: $BASENAME clone [device path]" + echo "Usage: $BASENAME clone [detect|device path]" echo echo "clones your treehouses image to an SDCard" echo @@ -43,7 +60,10 @@ function clone_help { echo " $BASENAME clone" echo " Will clone the current system to /dev/sdb (by default)." echo + echo " $BASENAME clone detect" + echo " Will display the paths for connected devices (e.g. /dev/sda)." + echo echo " $BASENAME clone /dev/sda" - echo " Will clone the current system to /dev/sda" + echo " Will clone the current system to /dev/sda." echo } diff --git a/modules/help.sh b/modules/help.sh index f1efa1592..519abe8b2 100644 --- a/modules/help.sh +++ b/modules/help.sh @@ -61,7 +61,7 @@ ntp sets rpi to host timing locally or to networkmode outputs the current network mode button gives the gpio pin 18 an action feedback sends feedback -clone [device path] clones the current SD card onto a secondary SD card or specified device +clone [detect| device path] clones the current SD card onto a secondary SD card or specified device restore [device path] restores a treehouses image to an SD card or specified device burn [device path] download and burns the latest treehouses image to the SD card or specified device rebootneeded shows if reboot is required to apply changes diff --git a/package.json b/package.json index 859891079..dc7df25df 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@treehouses/cli", - "version": "1.25.21", + "version": "1.25.22", "remote": "4000", "description": "Thin command-line interface for Raspberry Pi low level configuration.", "main": "cli.sh",