[LEDE-DEV] [PATCH] base-files: sysupgrade. Added the ability to use URL as source

Сергеев Сергей adron at yapic.net
Fri Aug 12 06:16:42 PDT 2016


Hi. Ansvers inline:
> From: Sergey Sergeev <adron at yapic.net>
>
>    scp uls(like this scp://adron@192.168.88.6:lede/lede-ar71xx-mikrotik-NAND-512b-squashfs-sysupgrade.bin)
>      is supported too. And you also can specify custom ssh port(...8.88.6:22110:lede...)
>
> Signed-off-by: Sergey Sergeev <adron at yapic.net>
> ---
>   package/base-files/files/lib/upgrade/common.sh | 30 +++++++++++++++++++++-----
>   package/base-files/files/sbin/sysupgrade       | 15 +++++++++++++
>   2 files changed, 40 insertions(+), 5 deletions(-)
>
> diff --git a/package/base-files/files/lib/upgrade/common.sh b/package/base-files/files/lib/upgrade/common.sh
> index ea03f77..0b0ad9a 100644
> --- a/package/base-files/files/lib/upgrade/common.sh
> +++ b/package/base-files/files/lib/upgrade/common.sh
> @@ -180,20 +180,40 @@ get_image() { # <source> [ <command> ]
>   	local from="$1"
>   	local conc="$2"
>   	local cmd
> +	local need_shield=1
>   
>   	case "$from" in
> -		http://*|ftp://*) cmd="wget -O- -q";;
> -		*) cmd="cat";;
> +		scp://*)
> +			cmd="ssh"
> +			local stuff="${from#scp://}"
> +			# extract user name and host
> +			local user_and_host=${stuff%%:*}
> +			# extract custom ssh port(if specified)
> +			local custom_port=${stuff%:*}
> +			custom_port=${custom_port##$user_and_host}
> +			custom_port=${custom_port#:}
> +			[ -n "$custom_port" ] && cmd="$cmd -p $custom_port"
> +			# extract target file for cat
> +			local target_file=${stuff##*:}
> +			# recompile from string
> +			need_shield=0
> +			from="\"$user_and_host\" \"cat $target_file\""
> apart from the port this seems very static and shuld be solvable using
> awk or sed. the code above looks pretty hard to read.
With sed it will be something like this:

local stuff=`echo $from | sed -ne 's;scp://;;p'`
# extract user name and host
local user_and_host=`echo $stuff | sed -ne 's;:.\+;;p'`
# extract custom ssh port(if specified)
local custom_port=`echo $stuff | sed -ne 's;[^:]\+:\([0-9]\+\):.\+;\1;p'`
[ -n "$custom_port" ] && cmd="$cmd -p $custom_port"
# extract target file for cat
local target_file=`echo $stuff | sed -ne 's;.\+:\([^:]\+\);\1;p'`

It seems to me, that the use of the bash string operators looks more 
simple. How do you think?
>
>> +			;;
>> +		http://* | https://* | ftp://*) cmd="wget -O-" ;;
>> +		*) cmd="cat" ;;
>>   	esac
>> +
>> +	# if needed => add shielding quotes to $from var
>> +	[ "$need_shield" -eq "1" ] && from="\"$from\""
>> +
> is there ever a need to *not* have quotes ? it looks liek always having
> them wont harm.
Ok. Quotes are clearly unnecessary. We can do without them.
>
>>   	if [ -z "$conc" ]; then
>> -		local magic="$(eval $cmd \"$from\" 2>/dev/null | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
>> +		local magic="$(eval $cmd $from | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
>>   		case "$magic" in
>>   			1f8b) conc="zcat";;
>>   			425a) conc="bzcat";;
>>   		esac
>>   	fi
>> -
>> -	eval "$cmd \"$from\" 2>/dev/null ${conc:+| $conc}"
>> +	eval "$cmd $from ${conc:+| $conc}"
> why do you remove the shell redirection here ?
I need to show to the user a diagnostic messages (for wget) as well as 
the certificate accept request and enter the password messages (for ssh)
Both wget and ssh are send these messages to stderr
>
> 	John
>
>>   }
>>   
>>   get_magic_word() {
>> diff --git a/package/base-files/files/sbin/sysupgrade b/package/base-files/files/sbin/sysupgrade
>> index 2f441f8..9158363 100755
>> --- a/package/base-files/files/sbin/sysupgrade
>> +++ b/package/base-files/files/sbin/sysupgrade
>> @@ -137,6 +137,21 @@ include /lib/upgrade
>>   
>>   [ "$1" = "nand" ] && nand_upgrade_stage2 $@
>>   
>> +get_if_URL(){
>> +	local url="$1"
>> +	local url_repl_file="/tmp/sysupgrade-URL.bin"
>> +
>> +	case "$url" in
>> +		http://* | https://* | ftp://*)
>> +		get_image "$url" "cat" > $url_repl_file
>> +		ARGV=${ARGV/"$url"/"$url_repl_file"}
>> +		;;
>> +	esac
>> +}
>> +
>> +# get image file from URL if specified
>> +get_if_URL "$ARGV"
>> +
>>   do_save_conffiles() {
>>   	local conf_tar="${1:-$CONF_TAR}"
>>   
>>




More information about the Lede-dev mailing list