* luci: replace all "OpenWRT" occurences with "OpenWrt"
[project/luci.git] / modules / admin-core / root / sbin / luci-flash
1 #!/bin/sh
2 . /etc/functions.sh
3
4 # initialize defaults
5 RAMFS_COPY_BIN="/usr/bin/awk"       # extra programs for temporary ramfs root
6 RAMFS_COPY_DATA=""      # extra data files
7 export KEEP_PATTERN=""
8 export VERBOSE=0
9
10 # parse options
11 while [ -n "$1" ]; do
12         case "$1" in
13                 -k)
14                                 shift
15                                 export SAVE_CONFIG=1
16                                 export KEEP_PATTERN="$1"
17                 ;;
18                 -*)
19                         echo "Invalid option: $1"
20                         exit 1
21                 ;;
22                 *) break;;
23         esac
24         shift;
25 done
26
27 export CONFFILES=/tmp/sysupgrade.conffiles
28 export CONF_TAR=/tmp/sysupgrade.tgz
29
30 [ -f $CONFFILES ] && rm $CONFFILES
31 [ -f $CONF_TAR ]  && rm $CONF_TAR
32
33 export ARGV="$*"
34 export ARGC="$#"
35
36 [ -z "$ARGV" ] && {
37         cat <<EOF
38 Usage: $0 [options] <image file or URL>
39
40 Options:
41         -k      <"file 1, file 2, ..."> Files to be kept
42 EOF
43         exit 1
44 }
45
46 add_pattern_conffiles() {
47         local file="$1"
48         find $KEEP_PATTERN >> "$file" 2>/dev/null
49         return 0
50 }
51
52 # hooks
53 sysupgrade_image_check="platform_check_image"
54 sysupgrade_init_conffiles=""
55
56 [ -n "$KEEP_PATTERN" ] && append sysupgrade_init_conffiles "add_pattern_conffiles"
57
58 include /lib/upgrade
59
60 do_save_conffiles() {
61         [ -z "$(rootfs_type)" ] && {
62                 echo "Cannot save config while running from ramdisk."
63                 exit 3
64                 return 0
65         }
66         run_hooks "$CONFFILES" $sysupgrade_init_conffiles
67
68         v "Saving config files..."
69         [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
70         tar c${TAR_V}zf "$CONF_TAR" -T "$CONFFILES" 2>/dev/null
71 }
72
73 type platform_check_image >/dev/null 2>/dev/null || {
74         echo "Firmware upgrade is not implemented for this platform."
75         exit 1
76 }
77
78 for check in $sysupgrade_image_check; do
79         ( eval "$check \"\$ARGV\"" ) || {
80                 echo "Image check '$check' failed."
81                 exit 2
82         }
83 done
84
85 [ -n "$sysupgrade_init_conffiles" ] && do_save_conffiles
86 run_hooks "" $sysupgrade_pre_upgrade
87
88 v "Switching to ramdisk..."
89 run_ramfs '. /etc/functions.sh; include /lib/upgrade; do_upgrade'