1#!/bin/sh
2
3RAM_ROOT=/tmp/root
4
5ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
6libs() { ldd $* | awk '{print $3}'; }
7
8install_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
19install_bin() { # <file> [ <symlink> ... ]
20	src=$1
21	files=$1
22	[ -x "$src" ] && files="$src $(libs $src)"
23	install_file $files
24	shift
25	for link in "$@"; do {
26		dest="$RAM_ROOT/$link"
27		dir="$(dirname $dest)"
28		mkdir -p "$dir"
29		[ -f "$dest" ] || ln -s $src $dest
30	}; done
31}
32
33pivot() { # <new_root> <old_root>
34	mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1 && \
35	mkdir -p $1$2 $1/proc $1/dev $1/tmp && \
36	mount -o move /dev $1/dev && \
37	mount -o move /proc $1/proc && \
38	pivot_root $1 $1$2 || {
39		umount $1 $1
40		return 1
41	}
42	mount -o move $2/tmp /tmp
43	#mount -o move $2/jffs /jffs 2>&-
44	return 0
45}
46
47run_ramfs() { # <command> [...]
48	killall smbd proftpd afpd minidlna luns_scan.sh detcable send_wol check_time_machine
49	sync
50	sleep 4
51	alldir=`cat /proc/mounts | grep sd | awk '{print $2}'`
52	for dir in $alldir; do
53		umount $dir
54	done
55	dd if=/dev/zero of=/tmp/tmp11 bs=20M count=1
56	rm /tmp/tmp11 -rf
57	install_bin /bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount /sbin/pivot_root /sbin/reboot /bin/sync /bin/sleep /bin/cat /bin/echo /sbin/ifconfig
58	install_bin /sbin/artmtd
59	install_bin /usr/sbin/nandwrite
60	install_bin /usr/sbin/net-cgi
61	install_file /www/languages-en.js
62	install_file /www/languages-gr.js
63	install_file /www/languages-pt.js
64	install_file /www/languages-ru.js
65	install_file /www/liteblue.gif /www/style/form.css /www/pls_wait.html /www/help.css /www/help.htm /www/upload.gif /www/funcs.js /www/UPG_process.htm /www/upg_get_status.htm /www/spacer.gif /www/menublue.gif /www/rbullet.gif /www/MNU_menu.htm /www/MNU_menu_nolink.htm /www/MNU_menu_wds.htm /www/MNU_menu_wds_nolink.htm /www/AUTO_upgrade_process.htm
66	install_file /etc/resolv.conf
67	install_file /lib/libcrypt.so.0 /lib/libgcc_s.so.1 /lib/libc.so.0 /lib/ld-uClibc.so.0 /lib/libcrypt-0.9.30.1.so /lib/libuClibc-0.9.30.1.so /lib/ld-uClibc-0.9.30.1.so /lib/libm-0.9.30.1.so /lib/libpthread-0.9.30.1.so /lib/libpthread.so.0 /usr/lib/libiconv.so.2.4.0 /usr/lib/libiconv.so /usr/lib/libiconv.so.2 /usr/lib/libconfig.so
68
69	pivot $RAM_ROOT /mnt || {
70		echo "Failed to switch over to ramfs. Please reboot."
71		exit 1
72	}
73
74	# spawn a new shell from ramdisk to reduce the probability of cache issues
75	exec /bin/busybox ash -c "$*"
76
77}
78
79run_ramfs
80
81