change the way resolv.conf is being created, so that it also works when dnsmasq is...
[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 mountdp() { # <device> <mount_point> <ignored> <fs>
45 dev=$1; mnt=$2; shift 2; opt=$*
46 mount $dev $mnt $opt
47 dupe $mnt $rom
48 pivot $mnt /rom
49 }
50
51 fopivot() { # <rw_root> <ro_root> <dupe?>
52 root=$1
53 {
54 mount -t mini_fo -o base=/,sto=$1 $1 /mnt 2>&- && root=/mnt
55 } || {
56 [ "$3" = "1" ] && mount -o bind $1 $1 && dupe $1 $rom
57 }
58 pivot $root $2
59 }
60
61 ramoverlay() {
62 mkdir -p /tmp/root
63 fopivot /tmp/root /rom 1
64 }
65
66 [ "${0##*/}" = "firstboot" ] && {
67 [ -z "$rom" ] && {
68 echo "You do not have a squashfs partition; aborting"
69 echo "(firstboot cannot be run on jffs2 based firmwares)"
70 exit 1
71 }
72
73 [ "$1" = "switch2jffs" ] && {
74 mtd erase OpenWrt
75 mount -o remount,ro none / 2>&- # try to avoid fs changing while copying
76 mount -o bind /tmp/root /mnt
77 mount /dev/mtdblock/4 /rom/jffs -t jffs2
78 echo -n "copying files ... "
79 cp -a /mnt/* /rom/jffs
80 umount /mnt
81 echo "done"
82 pivot /rom /mnt
83 mount -o move /mnt /tmp/root
84 fopivot /jffs /rom
85 umount /tmp/root 2>&-
86 jffs2root --clean
87 exit 0
88 }
89
90 # script run manually
91 [ \! -z "$jffs" ] && {
92 echo "firstboot has already been run"
93 echo "jffs2 partition is mounted, only resetting files"
94 dupe $jffs $rom
95 exit 0
96 }
97
98 mtd erase OpenWrt
99 mountdp /dev/mtdblock/4 /jffs -t jffs2
100 }