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