1d9b658b69960bc6906175797011ba824839da6b
[openwrt/svn-archive/archive.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 ./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 }
41 }
42
43 mountdp() { # <device> <mount_point> <ignored> <fs>
44 dev=$1; mnt=$2; shift 2; opt=$*
45 mount $dev $mnt $opt
46 dupe $mnt $rom
47 pivot $mnt /rom
48 }
49
50 ramoverlay() {
51 mkdir -p /tmp/root
52 mountdp /tmp/root /mnt -o bind
53 }
54
55 [ "${0##*/}" = "firstboot" ] && {
56 [ -z "$rom" ] && {
57 echo "You do not have a squashfs partition; aborting"
58 echo "(firstboot cannot be run on jffs2 based firmwares)"
59 exit 1
60 }
61
62 [ "$1" = "switch2jffs" ] && {
63 mtd erase OpenWrt
64 mount -o remount,ro none / # try to avoid fs changing while copying
65 mount -o bind / /mnt
66 mount /dev/mtdblock/4 /rom/jffs -t jffs2
67 echo -n "copying files ... "
68 cp -a /mnt/* /rom/jffs
69 umount /mnt
70 echo "done"
71 pivot /rom /mnt
72 mount -o move /mnt /tmp/root
73 pivot /jffs /rom
74 jffs2root --clean
75 exit 0
76 }
77
78 # script run manually
79 [ \! -z "$jffs" ] && {
80 echo "firstboot has already been run"
81 echo "jffs2 partition is mounted, only resetting files"
82 dupe $jffs $rom
83 exit 0
84 }
85
86 mtd erase OpenWrt
87 mountdp /dev/mtdblock/4 /jffs -t jffs2
88 }