fix typos in CONFIG_SITE files
[openwrt/svn-archive/openwrt.git] / openwrt / package / base-files / default / bin / firstboot
1 #!/bin/sh
2 # $Id$
3
4 rom=$(awk '/squashfs/ {print $2}' /proc/mounts)
5 jffs=$(awk '/jffs2/ {print $2}' /proc/mounts)
6
7 dupe() { # <new_root> <old_root>
8 cd $1
9 echo -n "creating directories... "
10 {
11 cd $2
12 find . -xdev -type d
13 echo "./dev ./jffs ./mnt ./proc ./tmp"
14 # xdev skips mounted directories
15 cd $1
16 } | xargs mkdir -p
17 echo "done"
18
19 echo -n "setting up symlinks... "
20 for file in $(cd $2; find . -xdev -type f;); do
21 case "$file" in
22 ./rom/note) ;; #nothing
23 ./etc/config*|\
24 ./usr/lib/ipkg/info/*) cp -af $2/$file $file;;
25 *) ln -sf /rom/${file#./*} $file;;
26 esac
27 done
28 for file in $(cd $2; find . -xdev -type l;); do
29 cp -af $2/${file#./*} $file
30 done
31 echo "done"
32 }
33
34 pivot() { # <new_root> <old_root>
35 mount -o move /proc $1/proc && \
36 pivot_root $1 $1$2 && {
37 mount -o move $2/dev /dev
38 mount -o move $2/tmp /tmp
39 mount -o move $2/jffs /jffs 2>&-
40 return 0
41 }
42 }
43
44 fopivot() { # <rw_root> <ro_root> <dupe?>
45 root=$1
46 {
47 mount -t mini_fo -o base=/,sto=$1 $1 /mnt 2>&- && root=/mnt
48 } || {
49 [ "$3" = "1" ] && {
50 mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1
51 dupe $1 $rom
52 }
53 }
54 pivot $root $2
55 }
56
57 ramoverlay() {
58 mkdir -p /tmp/root
59 fopivot /tmp/root /rom 1
60 }
61
62 # invoked as an executable
63 [ "${0##*/}" = "firstboot" ] && {
64
65 [ -z "$rom" ] && {
66 echo "You do not have a squashfs partition; aborting"
67 echo "(firstboot cannot be run on jffs2 based firmwares)"
68 exit 1
69 }
70
71 [ "$1" = "switch2jffs" ] && {
72 mtd erase OpenWrt
73
74 # try to avoid fs changing while copying
75 mount -o remount,ro none / 2>&-
76
77 # copy ramoverlay to jffs2
78 mount /dev/mtdblock/4 /rom/jffs -t jffs2
79 echo -n "copying files ... "
80 cp -a /tmp/root/* /rom/jffs 2>&-
81 echo "done"
82
83 # switch back to squashfs (temporarily)
84 # and park the ramdisk ontop of /tmp/root
85 pivot /rom /mnt
86 mount -o move /mnt /tmp/root
87
88 # /jffs is the overlay
89 # /rom is the readonly
90 fopivot /jffs /rom
91
92 # try to get rid of /tmp/root
93 # this will almost always fail
94 umount /tmp/root 2>&-
95
96 # fs is clean
97 jffs2root --clean
98 exit 0
99 }
100
101 # script run manually
102 [ \! -z "$jffs" ] && {
103 echo "firstboot has already been run"
104 echo "jffs2 partition is mounted, only resetting files"
105 grep mini_fo /proc/filesystems >&-
106 [ $? != 0 ] && {
107 dupe $jffs $rom
108 exit 0
109 } || {
110 rm -rf $jffs/* 2>&-
111 mount -o remount $jffs / 2>&-
112 exit 0
113 }
114 }
115
116 mtd erase OpenWrt
117 mount /dev/mtdblock/4 /jffs -t jffs2
118 fopivot /jffs /rom 1
119 }