sysupgrade: unmount filesystems before reboot
[openwrt/openwrt.git] / package / base-files / files / lib / upgrade / common.sh
index 9123359d1bc9fd17ea713a4ccd7691b0a7bf57e0..bed9c188f2358017c8b60d46137663c576e25333 100644 (file)
@@ -3,7 +3,7 @@
 RAM_ROOT=/tmp/root
 
 [ -x /usr/bin/ldd ] || ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
-libs() { ldd $* | sed -r 's/(.* => )?(.*) .*/\2/'; }
+libs() { ldd $* 2>/dev/null | sed -r 's/(.* => )?(.*) .*/\2/'; }
 
 install_file() { # <file> [ <file> ... ]
        for file in "$@"; do
@@ -48,15 +48,15 @@ supivot() { # <new_root> <old_root>
 
 run_ramfs() { # <command> [...]
        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/dd /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump  \
+               /sbin/pivot_root /sbin/reboot /bin/sync /bin/dd /bin/grep       \
+               /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" /bin/dd   \
+               /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump          \
                /bin/sleep /bin/zcat /usr/bin/bzcat /usr/bin/printf /usr/bin/wc \
                /bin/cut /usr/bin/printf /bin/sync /bin/mkdir /bin/rmdir        \
-               /bin/rm /usr/bin/basename /bin/kill /bin/chmod
+               /bin/rm /usr/bin/basename /bin/kill /bin/chmod /usr/bin/find
 
+       install_bin /bin/uclient-fetch /bin/wget
        install_bin /sbin/mtd
-       install_bin /sbin/ubi
        install_bin /sbin/mount_root
        install_bin /sbin/snapshot
        install_bin /sbin/snapshot_tool
@@ -68,6 +68,7 @@ run_ramfs() { # <command> [...]
        install_bin /usr/sbin/ubirsvol
        install_bin /usr/sbin/ubirmvol
        install_bin /usr/sbin/ubimkvol
+       install_bin /usr/sbin/partx
        for file in $RAMFS_COPY_BIN; do
                install_bin ${file//:/ }
        done
@@ -126,7 +127,7 @@ kill_remaining() { # [ <signal> ]
                else 
                        case "$name" in
                                # Skip essential services
-                               *procd*|*ash*|*init*|*watchdog*|*ssh*|*dropbear*|*telnet*|*login*|*hostapd*|*wpa_supplicant*|*nas*) : ;;
+                               *procd*|*ash*|*init*|*watchdog*|*ssh*|*dropbear*|*telnet*|*login*|*hostapd*|*wpa_supplicant*|*nas*|*relayd*) : ;;
 
                                # Killable process
                                *)
@@ -185,14 +186,14 @@ get_image() { # <source> [ <command> ]
                *) cmd="cat";;
        esac
        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\" 2>/dev/null | 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\" 2>/dev/null ${conc:+| $conc}"
 }
 
 get_magic_word() {
@@ -213,12 +214,16 @@ jffs2_copy_config() {
        fi
 }
 
+# Flash firmware to MTD partition
+#
+# $(1): path to image
+# $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
 default_do_upgrade() {
        sync
        if [ "$SAVE_CONFIG" -eq 1 ]; then
-               get_image "$1" | mtd $MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}"
+               get_image "$1" "$2" | mtd $MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}"
        else
-               get_image "$1" | mtd write - "${PART_NAME:-image}"
+               get_image "$1" "$2" | mtd write - "${PART_NAME:-image}"
        fi
 }
 
@@ -238,6 +243,7 @@ do_upgrade() {
        [ -n "$DELAY" ] && sleep "$DELAY"
        ask_bool 1 "Reboot" && {
                v "Rebooting system..."
+               umount -a
                reboot -f
                sleep 5
                echo b 2>/dev/null >/proc/sysrq-trigger