don't run mdev on hotplug pseudo-events that come from user space
[openwrt.git] / package / base-files / files / bin / firstboot
1 #!/bin/sh
2 # $Id: firstboot 5544 2006-11-17 03:07:10Z nbd $
3 . /etc/functions.sh
4
5 partname="rootfs_data"
6 mtdpart="$(find_mtd_part $partname)"
7
8 rom=$(awk '/squashfs/ {print $2}' /proc/mounts)
9 jffs=$(awk '/jffs2/ {print $2}' /proc/mounts)
10
11 dupe() { # <new_root> <old_root>
12         cd $1
13         echo -n "creating directories... "
14         {
15                 cd $2 
16                 find . -xdev -type d
17                 echo "./dev ./jffs ./mnt ./proc ./tmp"
18                 # xdev skips mounted directories
19                 cd $1 
20         } | xargs mkdir -p
21         echo "done"
22
23         echo -n "setting up symlinks... "
24         for file in $(cd $2; find . -xdev -type f;); do
25                 case "$file" in
26                 ./rom/note) ;; #nothing
27                 ./etc/config*|\
28                 ./usr/lib/ipkg/info/*) cp -af $2/$file $file;;
29                 *) ln -sf /rom/${file#./*} $file;;
30                 esac
31         done
32         for file in $(cd $2; find . -xdev -type l;); do
33                 cp -af $2/${file#./*} $file
34         done
35         echo "done"
36 }
37
38 pivot() { # <new_root> <old_root>
39         mount -o move /proc $1/proc && \
40         pivot_root $1 $1$2 && {
41                 mount -o move $2/dev /dev
42                 mount -o move $2/tmp /tmp
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 $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                 mtd erase "$partname"
82
83                 # try to avoid fs changing while copying
84                 mount -o remount,ro none / 2>&-
85
86                 # copy ramoverlay to jffs2
87                 mount "$mtdpart" /rom/jffs -t jffs2
88                 echo -n "copying files ... "
89                 cp -a /tmp/root/* /rom/jffs 2>&-
90                 echo "done"
91
92                 # switch back to squashfs (temporarily)
93                 # and park the ramdisk ontop of /tmp/root
94                 pivot /rom /mnt
95                 mount -o move /mnt /tmp/root
96
97                 # /jffs is the overlay
98                 # /rom is the readonly
99                 fopivot /jffs /rom
100
101                 # try to get rid of /tmp/root
102                 # this will almost always fail
103                 umount /tmp/root 2>&-
104
105                 # fs is clean
106                 jffs2root --clean
107                 exit 0
108         }
109
110         # script run manually
111         [ \! -z "$jffs" ] && {
112                 echo "firstboot has already been run"
113                 echo "jffs2 partition is mounted, only resetting files"
114                 grep mini_fo /proc/filesystems >&-
115                 [ $? != 0 ] && {
116                         dupe $jffs $rom
117                         exit 0
118                 } || { 
119                         rm -rf $jffs/* 2>&-
120                         mount -o remount $jffs / 2>&-
121                         exit 0
122                 }
123         }
124
125         mtd erase "$partname"
126         mount "$mtdpart" /jffs -t jffs2
127         fopivot /jffs /rom 1
128 }