Guest User

OpenWrt sysupgrade scripts

a guest
Nov 30th, 2011
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 23.79 KB | None | 0 0
  1. root@router:~# cat /lib/upgrade/common.sh
  2. #!/bin/sh
  3.  
  4. RAM_ROOT=/tmp/root
  5.  
  6. ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
  7. libs() { ldd $* | awk '{print $3}'; }
  8.  
  9. install_file() { # <file> [ <file> ... ]
  10.         for file in "$@"; do
  11.                 dest="$RAM_ROOT/$file"
  12.                 [ -f $file -a ! -f $dest ] && {
  13.                         dir="$(dirname $dest)"
  14.                         mkdir -p "$dir"
  15.                         cp $file $dest
  16.                 }
  17.         done
  18. }
  19.  
  20. install_bin() { # <file> [ <symlink> ... ]
  21.         src=$1
  22.         files=$1
  23.         [ -x "$src" ] && files="$src $(libs $src)"
  24.         install_file $files
  25.         [ -e /lib/ld-linux.so.3 ] && {
  26.                 install_file /lib/ld-linux.so.3
  27.         }
  28.         shift
  29.         for link in "$@"; do {
  30.                 dest="$RAM_ROOT/$link"
  31.                 dir="$(dirname $dest)"
  32.                 mkdir -p "$dir"
  33.                 [ -f "$dest" ] || ln -s $src $dest
  34.         }; done
  35. }
  36.  
  37. pivot() { # <new_root> <old_root>
  38.         mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1
  39.         mkdir -p $1$2 $1/proc $1/dev $1/tmp $1/overlay && \
  40.         mount -o move /proc $1/proc && \
  41.         pivot_root $1 $1$2 || {
  42.         umount $1 $1
  43.                 return 1
  44.         }
  45.         mount -o move $2/dev /dev
  46.         mount -o move $2/tmp /tmp
  47.         mount -o move $2/overlay /overlay 2>&-
  48.         return 0
  49. }
  50.  
  51. run_ramfs() { # <command> [...]
  52.         install_bin /bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount /sbin/pivot_root /usr/bin/wget /sbin/reboot /bin/sync /bin/dd /bin/grep /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump /bin/sleep /bin/zcat /usr/bin/bzcat
  53.         install_bin /sbin/mtd
  54.         for file in $RAMFS_COPY_BIN; do
  55.                 install_bin $file
  56.         done
  57.         install_file /etc/resolv.conf /etc/functions.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA
  58.  
  59.         pivot $RAM_ROOT /mnt || {
  60.                 echo "Failed to switch over to ramfs. Please reboot."
  61.                 exit 1
  62.         }
  63.  
  64.         mount -o remount,ro /mnt
  65.         umount -l /mnt
  66.  
  67.         grep /overlay /proc/mounts > /dev/null && {
  68.                 mount -o remount,ro /overlay
  69.                 umount -l /overlay
  70.         }
  71.  
  72.         # spawn a new shell from ramdisk to reduce the probability of cache issues
  73.         exec /bin/busybox ash -c "$*"
  74. }
  75.  
  76. run_hooks() {
  77.         local arg="$1"; shift
  78.         for func in "$@"; do
  79.                 eval "$func $arg"
  80.         done
  81. }
  82.  
  83. ask_bool() {
  84.         local default="$1"; shift;
  85.         local answer="$default"
  86.  
  87.         [ "$INTERACTIVE" -eq 1 ] && {
  88.                 case "$default" in
  89.                         0) echo -n "$* (y/N): ";;
  90.                         *) echo -n "$* (Y/n): ";;
  91.                 esac
  92.                 read answer
  93.                 case "$answer" in
  94.                         y*) answer=1;;
  95.                         n*) answer=0;;
  96.                         *) answer="$default";;
  97.                 esac
  98.         }
  99.         [ "$answer" -gt 0 ]
  100. }
  101.  
  102. v() {
  103.         [ "$VERBOSE" -ge 1 ] && echo "$@"
  104. }
  105.  
  106. rootfs_type() {
  107.         mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
  108. }
  109.  
  110. get_image() { # <source> [ <command> ]
  111.         local from="$1"
  112.         local conc="$2"
  113.         local cmd
  114.  
  115.         case "$from" in
  116.                 http://*|ftp://*) cmd="wget -O- -q";;
  117.                 *) cmd="cat";;
  118.         esac
  119.         if [ -z "$conc" ]; then
  120.                 local magic="$(eval $cmd $from | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
  121.                 case "$magic" in
  122.                         1f8b) conc="zcat";;
  123.                         425a) conc="bzcat";;
  124.                 esac
  125.         fi
  126.  
  127.         eval "$cmd $from ${conc:+| $conc}"
  128. }
  129.  
  130. get_magic_word() {
  131.         get_image "$@" | dd bs=2 count=1 2>/dev/null | hexdump -v -n 2 -e '1/1 "%02x"'
  132. }
  133.  
  134. get_magic_long() {
  135.         get_image "$@" | dd bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
  136. }
  137.  
  138. refresh_mtd_partitions() {
  139.         mtd refresh rootfs
  140. }
  141.  
  142. jffs2_copy_config() {
  143.         if grep rootfs_data /proc/mtd >/dev/null; then
  144.                 # squashfs+jffs2
  145.                 mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
  146.         else
  147.                 # jffs2
  148.                 mtd jffs2write "$CONF_TAR" rootfs
  149.         fi
  150. }
  151.  
  152. default_do_upgrade() {
  153.         sync
  154.         if [ "$SAVE_CONFIG" -eq 1 -a -z "$USE_REFRESH" ]; then
  155.                 get_image "$1" | mtd -j "$CONF_TAR" write - "${PART_NAME:-image}"
  156.         else
  157.                 get_image "$1" | mtd write - "${PART_NAME:-image}"
  158.         fi
  159. }
  160.  
  161. do_upgrade() {
  162.         v "Performing system upgrade..."
  163.         if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
  164.                 platform_do_upgrade "$ARGV"
  165.         else
  166.                 default_do_upgrade "$ARGV"
  167.         fi
  168.  
  169.         [ "$SAVE_CONFIG" -eq 1 -a -n "$USE_REFRESH" ] && {
  170.                 v "Refreshing partitions"
  171.                 if type 'platform_refresh_partitions' >/dev/null 2>/dev/null; then
  172.                         platform_refresh_partitions
  173.                 else
  174.                         refresh_mtd_partitions
  175.                 fi
  176.                 if type 'platform_copy_config' >/dev/null 2>/dev/null; then
  177.                         platform_copy_config
  178.                 else
  179.                         jffs2_copy_config
  180.                 fi
  181.         }
  182.         v "Upgrade completed"
  183.         [ -n "$DELAY" ] && sleep "$DELAY"
  184.         ask_bool 1 "Reboot" && {
  185.                 v "Rebooting system..."
  186.                 reboot -f
  187.                 sleep 5
  188.                 echo b 2>/dev/null >/proc/sysrq-trigger
  189.         }
  190. }
  191. root@router:~# cat /lib/upgrade/platform.sh
  192. #
  193. # Copyright (C) 2009 OpenWrt.org
  194. #
  195.  
  196. . /lib/ar71xx.sh
  197.  
  198. PART_NAME=firmware
  199. RAMFS_COPY_DATA=/lib/ar71xx.sh
  200.  
  201. CI_BLKSZ=65536
  202. CI_LDADR=0x80060000
  203.  
  204. platform_find_partitions() {
  205.         local first dev size erasesize name
  206.         while read dev size erasesize name; do
  207.                 name=${name#'"'}; name=${name%'"'}
  208.                 case "$name" in
  209.                         vmlinux.bin.l7|kernel|linux|rootfs)
  210.                                 if [ -z "$first" ]; then
  211.                                         first="$name"
  212.                                 else
  213.                                         echo "$erasesize:$first:$name"
  214.                                         break
  215.                                 fi
  216.                         ;;
  217.                 esac
  218.         done < /proc/mtd
  219. }
  220.  
  221. platform_find_kernelpart() {
  222.         local part
  223.         for part in "${1%:*}" "${1#*:}"; do
  224.                 case "$part" in
  225.                         vmlinux.bin.l7|kernel|linux)
  226.                                 echo "$part"
  227.                                 break
  228.                         ;;
  229.                 esac
  230.         done
  231. }
  232.  
  233. platform_do_upgrade_combined() {
  234.         local partitions=$(platform_find_partitions)
  235.         local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
  236.         local erase_size=$((0x${partitions%%:*})); partitions="${partitions#*:}"
  237.         local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
  238.         local kern_blocks=$(($kern_length / $CI_BLKSZ))
  239.         local root_blocks=$((0x$(dd if="$1" bs=2 skip=5 count=4 2>/dev/null) / $CI_BLKSZ))
  240.  
  241.         if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
  242.            [ ${kern_blocks:-0} -gt 0 ] && \
  243.            [ ${root_blocks:-0} -gt ${kern_blocks:-0} ] && \
  244.            [ ${erase_size:-0} -gt 0 ];
  245.         then
  246.                 local append=""
  247.                 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
  248.  
  249.                 ( dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null; \
  250.                   dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null ) | \
  251.                         mtd -r $append -F$kernelpart:$kern_length:$CI_LDADR,rootfs write - $partitions
  252.         fi
  253. }
  254.  
  255. platform_check_image() {
  256.         local board=$(ar71xx_board_name)
  257.         local magic="$(get_magic_word "$1")"
  258.         local magic_long="$(get_magic_long "$1")"
  259.  
  260.         [ "$ARGC" -gt 1 ] && return 1
  261.  
  262.         case "$board" in
  263.         ap121 | ap121-mini | ap96 | db120 | zcn-1523h-2 | zcn-1523h-5)
  264.                 [ "$magic" != "68737173" -a "$magic" != "19852003" ] && {
  265.                         echo "Invalid image type."
  266.                         return 1
  267.                 }
  268.                 return 0
  269.                 ;;
  270.         ap81 | ap83 | dir-600-a1 | dir-615-c1 | dir-825-b1 | mzk-w04nu | mzk-w300nh | tew-632brp | wrt400n | bullet-m | nanostation-m | rocket-m | wzr-hp-g300nh | wzr-hp-ag300h | whr-hp-g300n | nbg460n_550n_550nh | unifi )
  271.                 [ "$magic" != "2705" ] && {
  272.                         echo "Invalid image type."
  273.                         return 1
  274.                 }
  275.                 return 0
  276.                 ;;
  277.         tl-mr3220 | tl-mr3420 | tl-wa901nd | tl-wa901nd-v2 | tl-wr703n | tl-wr741nd | tl-wr841n-v1 | tl-wr941nd | tl-wr1043nd)
  278.                 [ "$magic" != "0100" ] && {
  279.                         echo "Invalid image type."
  280.                         return 1
  281.                 }
  282.                 return 0
  283.                 ;;
  284.         wndr3700)
  285.                 [ "$magic_long" != "33373030" ] && {
  286.                         echo "Invalid image type."
  287.                         return 1
  288.                 }
  289.                 return 0
  290.                 ;;
  291.         wndr3700v2)
  292.                 [ "$magic_long" != "33373031" ] && {
  293.                         echo "Invalid image type."
  294.                         return 1
  295.                 }
  296.                 return 0
  297.                 ;;
  298.         wrt160nl)
  299.                 [ "$magic" != "4e4c" ] && {
  300.                         echo "Invalid image type."
  301.                         return 1
  302.                 }
  303.                 return 0
  304.                 ;;
  305.         routerstation | routerstation-pro | ls-sr71 | pb42 | pb44 | eap7660d | ja76pf )
  306.                 [ "$magic" != "4349" ] && {
  307.                         echo "Invalid image. Use *-sysupgrade.bin files on this board"
  308.                         return 1
  309.                 }
  310.  
  311.                 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
  312.                 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
  313.  
  314.                 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
  315.                         return 0
  316.                 else
  317.                         echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
  318.                         return 1
  319.                 fi
  320.                 return 0
  321.                 ;;
  322.         esac
  323.  
  324.         echo "Sysupgrade is not yet supported on $board."
  325.         return 1
  326. }
  327.  
  328. platform_do_upgrade() {
  329.         local board=$(ar71xx_board_name)
  330.  
  331.         case "$board" in
  332.         routerstation | routerstation-pro | ls-sr71 | eap7660d | ja76pf )
  333.                 platform_do_upgrade_combined "$ARGV"
  334.                 ;;
  335.         *)
  336.                 default_do_upgrade "$ARGV"
  337.                 ;;
  338.         esac
  339. }
  340.  
  341. disable_watchdog() {
  342.         killall watchdog
  343.         ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
  344.                 echo 'Could not disable watchdog'
  345.                 return 1
  346.         }
  347. }
  348.  
  349. append sysupgrade_pre_upgrade disable_watchdog
  350. root@router:~# cat /etc/functions.sh
  351. #!/bin/sh
  352. # Copyright (C) 2006 OpenWrt.org
  353. # Copyright (C) 2006 Fokus Fraunhofer <[email protected]>
  354.  
  355.  
  356. debug () {
  357.         ${DEBUG:-:} "$@"
  358. }
  359. mount() {
  360.         busybox mount "$@"
  361. }
  362.  
  363. # newline
  364. N="
  365. "
  366.  
  367. _C=0
  368. NO_EXPORT=1
  369. LOAD_STATE=1
  370. LIST_SEP=" "
  371.  
  372. hotplug_dev() {
  373.         env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug-call net
  374. }
  375.  
  376. append() {
  377.         local var="$1"
  378.         local value="$2"
  379.         local sep="${3:- }"
  380.  
  381.         eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
  382. }
  383.  
  384. list_contains() {
  385.         local var="$1"
  386.         local str="$2"
  387.         local val
  388.  
  389.         eval "val=\" \${$var} \""
  390.         [ "${val%% $str *}" != "$val" ]
  391. }
  392.  
  393. list_remove() {
  394.         local var="$1"
  395.         local remove="$2"
  396.         local val
  397.  
  398.         eval "val=\" \${$var} \""
  399.         val1="${val%% $remove *}"
  400.         [ "$val1" = "$val" ] && return
  401.         val2="${val##* $remove }"
  402.         [ "$val2" = "$val" ] && return
  403.         val="${val1## } ${val2%% }"
  404.         val="${val%% }"
  405.         eval "export ${NO_EXPORT:+-n} -- \"$var=\$val\""
  406. }
  407.  
  408. config_load() {
  409.         [ -n "$IPKG_INSTROOT" ] && return 0
  410.         uci_load "$@"
  411. }
  412.  
  413. reset_cb() {
  414.         config_cb() { return 0; }
  415.         option_cb() { return 0; }
  416.         list_cb() { return 0; }
  417. }
  418. reset_cb
  419.  
  420. package() {
  421.         return 0
  422. }
  423.  
  424. config () {
  425.         local cfgtype="$1"
  426.         local name="$2"
  427.  
  428.         export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
  429.         name="${name:-cfg$CONFIG_NUM_SECTIONS}"
  430.         append CONFIG_SECTIONS "$name"
  431.         [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"
  432.         export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"
  433.         export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
  434. }
  435.  
  436. option () {
  437.         local varname="$1"; shift
  438.         local value="$*"
  439.  
  440.         export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value"
  441.         [ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*"
  442. }
  443.  
  444. list() {
  445.         local varname="$1"; shift
  446.         local value="$*"
  447.         local len
  448.  
  449.         config_get len "$CONFIG_SECTION" "${varname}_LENGTH" 0
  450.         [ $len = 0 ] && append CONFIG_LIST_STATE "${CONFIG_SECTION}_${varname}"
  451.         len=$(($len + 1))
  452.         config_set "$CONFIG_SECTION" "${varname}_ITEM$len" "$value"
  453.         config_set "$CONFIG_SECTION" "${varname}_LENGTH" "$len"
  454.         append "CONFIG_${CONFIG_SECTION}_${varname}" "$value" "$LIST_SEP"
  455.         list_cb "$varname" "$*"
  456. }
  457.  
  458. config_rename() {
  459.         local OLD="$1"
  460.         local NEW="$2"
  461.         local oldvar
  462.         local newvar
  463.  
  464.         [ -n "$OLD" -a -n "$NEW" ] || return
  465.         for oldvar in `set | grep ^CONFIG_${OLD}_ | \
  466.                 sed -e 's/\(.*\)=.*$/\1/'` ; do
  467.                 newvar="CONFIG_${NEW}_${oldvar##CONFIG_${OLD}_}"
  468.                 eval "export ${NO_EXPORT:+-n} \"$newvar=\${$oldvar}\""
  469.                 unset "$oldvar"
  470.         done
  471.         export ${NO_EXPORT:+-n} CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , $NEW ,")"
  472.  
  473.         [ "$CONFIG_SECTION" = "$OLD" ] && export ${NO_EXPORT:+-n} CONFIG_SECTION="$NEW"
  474. }
  475.  
  476. config_unset() {
  477.         config_set "$1" "$2" ""
  478. }
  479.  
  480. config_clear() {
  481.         local SECTION="$1"
  482.         local oldvar
  483.  
  484.         list_remove CONFIG_SECTIONS "$SECTION"
  485.         export ${NO_EXPORT:+-n} CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
  486.  
  487.         for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \
  488.                 sed -e 's/\(.*\)=.*$/\1/'` ; do
  489.                 unset $oldvar
  490.         done
  491. }
  492.  
  493. # config_get <variable> <section> <option> [<default>]
  494. # config_get <section> <option>
  495. config_get() {
  496.         case "$3" in
  497.                 "") eval echo "\${CONFIG_${1}_${2}:-\${4}}";;
  498.                 *)  eval export ${NO_EXPORT:+-n} -- "${1}=\${CONFIG_${2}_${3}:-\${4}}";;
  499.         esac
  500. }
  501.  
  502. # config_get_bool <variable> <section> <option> [<default>]
  503. config_get_bool() {
  504.         local _tmp
  505.         config_get _tmp "$2" "$3" "$4"
  506.         case "$_tmp" in
  507.                 1|on|true|enabled) _tmp=1;;
  508.                 0|off|false|disabled) _tmp=0;;
  509.                 *) _tmp="$4";;
  510.         esac
  511.         export ${NO_EXPORT:+-n} "$1=$_tmp"
  512. }
  513.  
  514. config_set() {
  515.         local section="$1"
  516.         local option="$2"
  517.         local value="$3"
  518.         local old_section="$CONFIG_SECTION"
  519.  
  520.         CONFIG_SECTION="$section"
  521.         option "$option" "$value"
  522.         CONFIG_SECTION="$old_section"
  523. }
  524.  
  525. config_foreach() {
  526.         local function="$1"
  527.         [ "$#" -ge 1 ] && shift
  528.         local type="$1"
  529.         [ "$#" -ge 1 ] && shift
  530.         local section cfgtype
  531.  
  532.         [ -z "$CONFIG_SECTIONS" ] && return 0
  533.         for section in ${CONFIG_SECTIONS}; do
  534.                 config_get cfgtype "$section" TYPE
  535.                 [ -n "$type" -a "x$cfgtype" != "x$type" ] && continue
  536.                 eval "$function \"\$section\" \"\$@\""
  537.         done
  538. }
  539.  
  540. config_list_foreach() {
  541.         [ "$#" -ge 3 ] || return 0
  542.         local section="$1"; shift
  543.         local option="$1"; shift
  544.         local function="$1"; shift
  545.         local val
  546.         local len
  547.         local c=1
  548.  
  549.         config_get len "${section}" "${option}_LENGTH"
  550.         [ -z "$len" ] && return 0
  551.         while [ $c -le "$len" ]; do
  552.                 config_get val "${section}" "${option}_ITEM$c"
  553.                 eval "$function \"\$val\" \"$@\""
  554.                 c="$(($c + 1))"
  555.         done
  556. }
  557.  
  558. load_modules() {
  559.         [ -d /etc/modules.d ] && {
  560.                 cd /etc/modules.d
  561.                 sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
  562.         }
  563. }
  564.  
  565. include() {
  566.         local file
  567.  
  568.         for file in $(ls $1/*.sh 2>/dev/null); do
  569.                 . $file
  570.         done
  571. }
  572.  
  573. find_mtd_part() {
  574.         local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
  575.         local PREFIX=/dev/mtdblock
  576.  
  577.         PART="${PART##mtd}"
  578.         [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
  579.         echo "${PART:+$PREFIX$PART}"
  580. }
  581.  
  582. strtok() { # <string> { <variable> [<separator>] ... }
  583.         local tmp
  584.         local val="$1"
  585.         local count=0
  586.  
  587.         shift
  588.  
  589.         while [ $# -gt 1 ]; do
  590.                 tmp="${val%%$2*}"
  591.  
  592.                 [ "$tmp" = "$val" ] && break
  593.  
  594.                 val="${val#$tmp$2}"
  595.  
  596.                 export ${NO_EXPORT:+-n} "$1=$tmp"; count=$((count+1))
  597.                 shift 2
  598.         done
  599.  
  600.         if [ $# -gt 0 -a -n "$val" ]; then
  601.                 export ${NO_EXPORT:+-n} "$1=$val"; count=$((count+1))
  602.         fi
  603.  
  604.         return $count
  605. }
  606.  
  607.  
  608. jffs2_mark_erase() {
  609.         local part="$(find_mtd_part "$1")"
  610.         [ -z "$part" ] && {
  611.                 echo Partition not found.
  612.                 return 1
  613.         }
  614.         echo -e "\xde\xad\xc0\xde" | mtd -qq write - "$1"
  615. }
  616.  
  617. uci_apply_defaults() {
  618.         cd /etc/uci-defaults || return 0
  619.         files="$(ls)"
  620.         [ -z "$files" ] && return 0
  621.         mkdir -p /tmp/.uci
  622.         for file in $files; do
  623.                 ( . "./$(basename $file)" ) && rm -f "$file"
  624.         done
  625.         uci commit
  626. }
  627.  
  628. service_kill() {
  629.         local name="${1}"
  630.         local pid="${2:-$(pidof "$name")}"
  631.         local grace="${3:-5}"
  632.  
  633.         [ -f "$pid" ] && pid="$(head -n1 "$pid" 2>/dev/null)"
  634.  
  635.         for pid in $pid; do
  636.                 [ -d "/proc/$pid" ] || continue
  637.                 local try=0
  638.                 kill -TERM $pid 2>/dev/null && \
  639.                         while grep -qs "$name" "/proc/$pid/cmdline" && [ $((try++)) -lt $grace ]; do sleep 1; done
  640.                 kill -KILL $pid 2>/dev/null && \
  641.                         while grep -qs "$name" "/proc/$pid/cmdline"; do sleep 1; done
  642.         done
  643. }
  644.  
  645.  
  646. pi_include() {
  647.         if [ -f "/tmp/overlay/$1" ]; then
  648.                 . "/tmp/overlay/$1"
  649.         elif [ -f "$1" ]; then
  650.                 . "$1"
  651.         elif [ -d "/tmp/overlay/$1" ]; then
  652.                 if [ -n "$(ls /tmp/overlay/$1/*.sh 2>/dev/null)" ]; then
  653.                         for src_script in /tmp/overlay/$1/*.sh; do
  654.                                 . "$src_script"
  655.                         done
  656.                 fi
  657.         elif [ -d "$1" ]; then
  658.                 if [ -n "$(ls $1/*.sh 2>/dev/null)" ]; then
  659.                         for src_script in $1/*.sh; do
  660.                                 . "$src_script"
  661.                         done
  662.                 fi
  663.         else
  664.                 echo "WARNING: $1 not found"
  665.                 return 1
  666.         fi
  667.         return 0
  668. }
  669.  
  670. [ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh
  671. root@router:~# cat /sbin/sysupgrade
  672. #!/bin/sh
  673. . /etc/functions.sh
  674.  
  675. # initialize defaults
  676. RAMFS_COPY_BIN=""       # extra programs for temporary ramfs root
  677. RAMFS_COPY_DATA=""      # extra data files
  678. export INTERACTIVE=0
  679. export VERBOSE=1
  680. export SAVE_CONFIG=1
  681. export SAVE_OVERLAY=0
  682. export DELAY=
  683. export CONF_IMAGE=
  684. export HELP=0
  685.  
  686. # parse options
  687. while [ -n "$1" ]; do
  688.         case "$1" in
  689.                 -i) export INTERACTIVE=1;;
  690.                 -d) export DELAY="$2"; shift;;
  691.                 -v) export VERBOSE="$(($VERBOSE + 1))";;
  692.                 -q) export VERBOSE="$(($VERBOSE - 1))";;
  693.                 -n) export SAVE_CONFIG=0;;
  694.                 -c) export SAVE_OVERLAY=1;;
  695.                 -f) export CONF_IMAGE="$2"; shift;;
  696.                 -h|--help) export HELP=1; break;;
  697.                 -*)
  698.                         echo "Invalid option: $1"
  699.                         exit 1
  700.                 ;;
  701.                 *) break;;
  702.         esac
  703.         shift;
  704. done
  705.  
  706. export CONFFILES=/tmp/sysupgrade.conffiles
  707. export CONF_TAR=/tmp/sysupgrade.tgz
  708.  
  709. export ARGV="$*"
  710. export ARGC="$#"
  711.  
  712. [ -z "$ARGV" -o $HELP -gt 0 ] && {
  713.         cat <<EOF
  714. Usage: $0 [options] <image file or URL>
  715.  
  716. Options:
  717.         -d <delay>   add a delay before rebooting
  718.         -f <config>  restore configuration from .tar.gz (file or url)
  719.         -i           interactive mode
  720.         -c           attempt to preserve all changed files in /etc/
  721.         -n           do not save configuration over reflash
  722.         -q           less verbose
  723.         -v           more verbose
  724.         -h / --help  display this help
  725.  
  726. EOF
  727.         exit 1
  728. }
  729.  
  730. add_uci_conffiles() {
  731.         local file="$1"
  732.         ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
  733.                 /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
  734.                 -type f 2>/dev/null;
  735.           opkg list-changed-conffiles ) | sort -u > "$file"
  736.         return 0
  737. }
  738.  
  739. add_overlayfiles() {
  740.         local file="$1"
  741.         find /overlay/etc/ -type f | sed \
  742.                 -e 's,^/overlay/,/,' \
  743.                 -e '\,/META_[a-zA-Z0-9]*$,d' \
  744.                 -e '\,/functions.sh$,d' \
  745.                 -e '\,/[^/]*-opkg$,d' \
  746.         > "$file"
  747.         return 0
  748. }
  749.  
  750. # hooks
  751. sysupgrade_image_check="platform_check_image"
  752. [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \
  753.         sysupgrade_init_conffiles="add_uci_conffiles" || \
  754.         sysupgrade_init_conffiles="add_overlayfiles"
  755.  
  756. include /lib/upgrade
  757.  
  758. do_save_conffiles() {
  759.         [ -z "$(rootfs_type)" ] && {
  760.                 echo "Cannot save config while running from ramdisk."
  761.                 ask_bool 0 "Abort" && exit
  762.                 return 0
  763.         }
  764.         run_hooks "$CONFFILES" $sysupgrade_init_conffiles
  765.         ask_bool 0 "Edit config file list" && vi "$CONFFILES"
  766.  
  767.         v "Saving config files..."
  768.         [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  769.         tar c${TAR_V}zf "$CONF_TAR" -T "$CONFFILES" 2>/dev/null
  770. }
  771.  
  772. type platform_check_image >/dev/null 2>/dev/null || {
  773.         echo "Firmware upgrade is not implemented for this platform."
  774.         exit 1
  775. }
  776.  
  777. for check in $sysupgrade_image_check; do
  778.         ( eval "$check \"\$ARGV\"" ) || {
  779.                 echo "Image check '$check' failed."
  780.                 exit 1
  781.         }
  782. done
  783.  
  784. if [ -n "$CONF_IMAGE" ]; then
  785.         case "$(get_magic_word $CONF_IMAGE cat)" in
  786.                 # .gz files
  787.                 1f8b) ;;
  788.                 *)
  789.                         echo "Invalid config file. Please use only .tar.gz files"
  790.                         exit 1
  791.                 ;;
  792.         esac
  793.         get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
  794.         export SAVE_CONFIG=1
  795. elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
  796.         do_save_conffiles
  797.         export SAVE_CONFIG=1
  798. else
  799.         export SAVE_CONFIG=0
  800. fi
  801. run_hooks "" $sysupgrade_pre_upgrade
  802.  
  803. if [ -n "$(rootfs_type)" ]; then
  804.         v "Switching to ramdisk..."
  805.         run_ramfs '. /etc/functions.sh; include /lib/upgrade; do_upgrade'
  806. else
  807.         do_upgrade
  808. fi
  809.  
Advertisement
Add Comment
Please, Sign In to add comment