finally move buildroot-ng to trunk
[openwrt.git] / package / base-files / brcm-2.6 / bin / firstboot
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
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 ./sys"
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                 ./etc/resolv.conf|\
25                 ./usr/lib/ipkg/info) cp -af $2/$file $file;;
26                 *) ln -sf /rom/${file#./*} $file;;
27                 esac
28         done
29         for file in $(cd $2; find . -xdev -type l;); do
30                 cp -af $2/${file#./*} $file
31         done
32         echo "done"
33 }
34
35 pivot() { # <new_root> <old_root>
36         mount -o move /proc $1/proc && \
37         pivot_root $1 $1$2 && {
38                 mount -o move $2/dev /dev
39                 mount -o move $2/tmp /tmp
40                 mount -o move $2/sys /sys
41                 return 0
42         }
43 }
44
45 mountdp() { # <device> <mount_point> <ignored> <fs>
46         dev=$1; mnt=$2; shift 2; opt=$*
47         mount $dev $mnt $opt
48         dupe $mnt $rom
49         pivot $mnt /rom
50 }
51
52 ramoverlay() {
53         mkdir -p /tmp/root
54         mountdp /tmp/root /mnt -o bind
55 }
56
57 [ "${0##*/}" = "firstboot" ] && {
58         [ -z "$rom" ] && {
59                 echo "You do not have a squashfs partition; aborting"
60                 echo "(firstboot cannot be run on jffs2 based firmwares)"
61                 exit 1
62         }
63
64         [ "$1" = "switch2jffs" ] && {
65                 mtd erase OpenWrt
66                 mount -o remount,ro none / # try to avoid fs changing while copying
67                 mount -o bind / /mnt
68                 mount /dev/mtdblock/4 /rom/jffs -t jffs2
69                 echo -n "copying files ... "
70                 cp -a /mnt/* /rom/jffs
71                 umount /mnt
72                 echo "done"
73                 pivot /rom /mnt
74                 mount -o move /mnt /tmp/root
75                 pivot /jffs /rom
76                 jffs2root --clean
77                 exit 0
78         }
79
80         # script run manually
81         [ \! -z "$jffs" ] && {
82                 echo "firstboot has already been run"
83                 echo "jffs2 partition is mounted, only resetting files"
84                 dupe $jffs $rom
85                 exit 0
86         }
87
88         mtd erase OpenWrt
89         mountdp /dev/mtdblock/4 /jffs -t jffs2
90 }