Skip to content

Commit

Permalink
treehouses clone detect (fixes #2067) (#2083)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <dogi@users.noreply.github.com>
  • Loading branch information
JLKwong and dogi authored Mar 13, 2021
1 parent 81f6e82 commit 332908a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ ntp <local|internet> sets rpi to host timing locally or to
networkmode outputs the current network mode
button <off|bluetooth> gives the gpio pin 18 an action
feedback <message> 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
Expand Down
1 change: 1 addition & 0 deletions _treehouses
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 51 additions & 31 deletions modules/clone.sh
Original file line number Diff line number Diff line change
@@ -1,49 +1,69 @@
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
echo "Example:"
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
}
2 changes: 1 addition & 1 deletion modules/help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ ntp <local|internet> sets rpi to host timing locally or to
networkmode outputs the current network mode
button <off|bluetooth> gives the gpio pin 18 an action
feedback <message> 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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 332908a

Please sign in to comment.