change from /jffs to /overlay, patch from Daniel Dickinson
[openwrt.git] / package / base-files / files / lib / upgrade / common.sh
1 #!/bin/sh
2
3 RAM_ROOT=/tmp/root
4
5 ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
6 libs() { ldd $* | awk '{print $3}'; }
7
8 install_file() { # <file> [ <file> ... ]
9 for file in "$@"; do
10 dest="$RAM_ROOT/$file"
11 [ -f $file -a ! -f $dest ] && {
12 dir="$(dirname $dest)"
13 mkdir -p "$dir"
14 cp $file $dest
15 }
16 done
17 }
18
19 install_bin() { # <file> [ <symlink> ... ]
20 src=$1
21 files=$1
22 [ -x "$src" ] && files="$src $(libs $src)"
23 install_file $files
24 [ -e /lib/ld-linux.so.3 ] && {
25 install_file /lib/ld-linux.so.3
26 }
27 shift
28 for link in "$@"; do {
29 dest="$RAM_ROOT/$link"
30 dir="$(dirname $dest)"
31 mkdir -p "$dir"
32 [ -f "$dest" ] || ln -s $src $dest
33 }; done
34 }
35
36 pivot() { # <new_root> <old_root>
37 mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1
38 mkdir -p $1$2 $1/proc $1/dev $1/tmp $1/overlay && \
39 mount -o move /proc $1/proc && \
40 pivot_root $1 $1$2 || {
41 umount $1 $1
42 return 1
43 }
44 mount -o move $2/dev /dev
45 mount -o move $2/tmp /tmp
46 mount -o move $2/overlay /overlay 2>&-
47 return 0
48 }
49
50 run_ramfs() { # <command> [...]
51 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
52 install_bin /sbin/mtd
53 for file in $RAMFS_COPY_BIN; do
54 install_bin $file
55 done
56 install_file /etc/resolv.conf /etc/functions.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA
57
58 pivot $RAM_ROOT /mnt || {
59 echo "Failed to switch over to ramfs. Please reboot."
60 exit 1
61 }
62
63 mount -o remount,ro /mnt
64 umount -l /mnt
65
66 grep /overlay /proc/mounts > /dev/null && {
67 mount -o remount,ro /overlay
68 umount -l /overlay
69 }
70
71 # spawn a new shell from ramdisk to reduce the probability of cache issues
72 exec /bin/busybox ash -c "$*"
73 }
74
75 run_hooks() {
76 local arg="$1"; shift
77 for func in "$@"; do
78 eval "$func $arg"
79 done
80 }
81
82 ask_bool() {
83 local default="$1"; shift;
84 local answer="$default"
85
86 [ "$INTERACTIVE" -eq 1 ] && {
87 case "$default" in
88 0) echo -n "$* (y/N): ";;
89 *) echo -n "$* (Y/n): ";;
90 esac
91 read answer
92 case "$answer" in
93 y*) answer=1;;
94 n*) answer=0;;
95 *) answer="$default";;
96 esac
97 }
98 [ "$answer" -gt 0 ]
99 }
100
101 v() {
102 [ "$VERBOSE" -ge 1 ] && echo "$@"
103 }
104
105 rootfs_type() {
106 mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
107 }
108
109 get_image() { # <source> [ <command> ]
110 local from="$1"
111 local conc="$2"
112 local cmd
113
114 case "$from" in
115 http://*|ftp://*) cmd="wget -O- -q";;
116 *) cmd="cat";;
117 esac
118 if [ -z "$conc" ]; then
119 local magic="$(eval $cmd $from | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
120 case "$magic" in
121 1f8b) conc="zcat";;
122 425a) conc="bzcat";;
123 esac
124 fi
125
126 eval "$cmd $from ${conc:+| $conc}"
127 }
128
129 get_magic_word() {
130 get_image "$@" | dd bs=2 count=1 2>/dev/null | hexdump -v -n 2 -e '1/1 "%02x"'
131 }
132
133 refresh_mtd_partitions() {
134 mtd refresh rootfs
135 }
136
137 jffs2_copy_config() {
138 if grep rootfs_data /proc/mtd >/dev/null; then
139 # squashfs+jffs2
140 mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
141 else
142 # jffs2
143 mtd jffs2write "$CONF_TAR" rootfs
144 fi
145 }
146
147 default_do_upgrade() {
148 sync
149 if [ "$SAVE_CONFIG" -eq 1 -a -z "$USE_REFRESH" ]; then
150 get_image "$1" | mtd -j "$CONF_TAR" write - "${PART_NAME:-image}"
151 else
152 get_image "$1" | mtd write - "${PART_NAME:-image}"
153 fi
154 }
155
156 do_upgrade() {
157 v "Performing system upgrade..."
158 if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
159 platform_do_upgrade "$ARGV"
160 else
161 default_do_upgrade "$ARGV"
162 fi
163
164 [ "$SAVE_CONFIG" -eq 1 -a -n "$USE_REFRESH" ] && {
165 v "Refreshing partitions"
166 if type 'platform_refresh_partitions' >/dev/null 2>/dev/null; then
167 platform_refresh_partitions
168 else
169 refresh_mtd_partitions
170 fi
171 if type 'platform_copy_config' >/dev/null 2>/dev/null; then
172 platform_copy_config
173 else
174 jffs2_copy_config
175 fi
176 }
177 v "Upgrade completed"
178 [ -n "$DELAY" ] && sleep "$DELAY"
179 ask_bool 1 "Reboot" && {
180 v "Rebooting system..."
181 reboot
182 sleep 5
183 echo b 2>/dev/null >/proc/sysrq-trigger
184 }
185 }
This page took 0.048556 seconds and 5 git commands to generate.