massive rewrite of firstboot
authorMike Baker <mbm@openwrt.org>
Mon, 13 Feb 2006 13:13:40 +0000 (13:13 +0000)
committerMike Baker <mbm@openwrt.org>
Mon, 13 Feb 2006 13:13:40 +0000 (13:13 +0000)
SVN-Revision: 3233

openwrt/package/base-files/Makefile
openwrt/package/base-files/default/bin/firstboot
openwrt/package/base-files/default/etc/init.d/S10boot
openwrt/package/base-files/default/etc/init.d/S45firewall
openwrt/package/base-files/default/etc/init.d/S99done
openwrt/package/base-files/default/sbin/mount_root

index f291c4ec9af834dbc8bf42277caf1fff2e5b699a..69fa8e7597e1bb60ca4cfc9e24ebd927fc2461ca 100644 (file)
@@ -47,6 +47,7 @@ $(IPKG_BASE):
        mkdir -p $(IDIR_BASE)/lib
        mkdir -p $(IDIR_BASE)/usr/lib
        mkdir -p $(IDIR_BASE)/usr/bin
+       mkdir -p $(IDIR_BASE)/mnt
        mkdir -p $(IDIR_BASE)/www
        ln -sf /tmp/resolv.conf $(IDIR_BASE)/etc/resolv.conf
        rm -rf $(IDIR_BASE)/var
index 604721873a8ce5a0339e37ea9c08be5c1f7958dc..ee082cffd249055fb59222412a514175353d7737 100755 (executable)
@@ -1,65 +1,85 @@
 #!/bin/sh
 # $Id$
 
-mount | grep squashfs >&- || {
-       echo "You do not have a squashfs partition; aborting"
-       echo "(firstboot cannot be run on jffs2 based firmwares)"
-       return
-}
-
-[ -f "/tmp/.firstboot" ] && {
-       echo "firstboot is already running"
-       return
-}
-touch /tmp/.firstboot
+rom=$(awk '/squashfs/ {print $2}' /proc/mounts)
+jffs=$(awk '/jffs2/ {print $2}' /proc/mounts)
 
-jdev=$(mount | awk '/jffs2/ {print $3}')
+dupe() { # <new_root> <old_root>
+       cd $1
+       echo -n "creating directories... "
+       {
+               cd $2 
+               find . -xdev -type d
+               echo "./dev ./jffs ./mnt ./proc ./tmp"
+               # xdev skips mounted directories
+               cd $1 
+       } | xargs mkdir -p
+       echo "done"
 
-if [ -z "$jdev" ]; then
-       echo -n "Creating jffs2 partition... "
-       mtd erase OpenWrt >&- 2>&-
-       mount -t jffs2 /dev/mtdblock/4 /jffs
+       echo -n "setting up symlinks... "
+       for file in $(cd $2; find . -xdev -type f; find . -xdev -type l;)
+       do {
+               case "$file" in
+               "./rom/note") ;; #nothing
+               "./etc/config"|\
+               "./etc/resolv.conf"|\
+               "./usr/lib/ipkg/info") cp -af $2/$file $file;;
+               *) ln -sf /rom/${file#./*} $file;;
+               esac
+       } done
        echo "done"
-       cd /jffs
-else
-       echo "firstboot has already been run"
-       echo "jffs2 partition is mounted, only resetting files"
-       cd $jdev
-fi
+}
 
-exec 2>/dev/null
+pivot() { # <new_root> <old_root>
+       pivot_root $1 $1$2
+       mount none /proc -t proc
+       umount $2/proc
+       mount -o move $2/dev /dev
+       mount -o move $2/tmp /tmp
+}
 
-mount /dev/mtdblock/2 /rom -o ro
+mountdp() { # <device> <mount_point> <ignored> <fs>
+       mount $1 $2 -t $4
+       dupe $2 $rom
+       pivot $2 /rom
+}
 
-echo -n "creating directories... "
-{
-       cd /rom
-       find . -type d
-       cd -
-} | xargs mkdir
-echo "done"
+# script run by mount_root
+[ "$1" = "ramoverlay" ] && {
+       #1st stage
+       mountdp none /mnt -t tmpfs
+       exit 0
+}
 
-echo -n "setting up symlinks... "
-for file in $(cd /rom; find *  -type f; find *  -type l;)
-do {
-  case "${file%/*}" in
-    "usr/lib/ipkg/info"|"etc/config") cp -f /rom/$file $file;;
-    *) ln -sf /rom/$file $file;;
-  esac
-} done
-echo "done"
+[ "$1" = "switch2jffs" ] && {
+       #2nd stage
+       mtd erase OpenWrt
+       mount -o remount,ro none / # try to avoid fs changing while copying
+       mount -o bind / /mnt
+       mount /dev/mtdblock/4 /rom/jffs -t jffs2
+       echo -n "copying files ... "
+       cp -a /mnt/* /rom/jffs
+       umount /mnt
+       echo "done"
+       pivot /rom /mnt
+       pivot /jffs /rom
+       jffs2root --clean
+       exit 0
+}
 
-touch /tmp/resolv.conf
-ln -s /tmp/resolv.conf /etc/resolv.conf
+# script run manually
+[ -z "$rom" ] && {
+       echo "You do not have a squashfs partition; aborting"
+       echo "(firstboot cannot be run on jffs2 based firmwares)"
+       exit 1
+}
 
-umount /rom
-mount none /jffs/proc -t proc
-pivot_root /jffs /jffs/rom
-mount none /dev  -t devfs
-mount none /tmp  -t ramfs
-umount /rom/proc
-umount /rom/tmp
-umount /rom/dev
+[ \! -z "$jffs" ] && {
+       echo "firstboot has already been run"
+       echo "jffs2 partition is mounted, only resetting files"
+       dupe $jffs $rom
+       exit 0
+}
 
-# normally created by boot script
-mkdir -p /var/run
+mtd erase OpenWrt
+mountdp /dev/mtdblock/4 /jffs -t jffs2
index cb8be94b5cbe1abd407359cf3635e884bafa7360..9e7009d8764bb9f31ac24274c7592094bb5de63e 100755 (executable)
@@ -1,5 +1,5 @@
 #!/bin/sh
-echo "S" > /proc/jffs2_bbc
+[ -f /proc/jffs2_bbc ] && echo "S" > /proc/jffs2_bbc
 
 mkdir -p /var/run
 mkdir -p /var/log
@@ -11,9 +11,9 @@ sed 's/^[^#]/insmod &/' /etc/modules /etc/modules.d/* 2>&-|ash
 ifconfig lo 127.0.0.1 up
 ifconfig eth0 promisc
 
-# configure the switch based on nvram if not in failsafe mode
-[ -d /proc/switch/eth0 -a -z "$FAILSAFE" ] && {
-       for nr in `seq 0 15`; do
+# configure the switch based on nvram
+[ -d /proc/switch/eth0 ] && {
+       for nr in $(seq 0 15); do
                vp="$(nvram get vlan${nr}ports)"
                [ -z "$vp" -o -z "$(nvram get vlan${nr}hwname)" ] || {
                        echo "$vp" > /proc/switch/eth0/vlan/$nr/ports
@@ -27,7 +27,3 @@ echo ${HOSTNAME:=OpenWrt}>/proc/sys/kernel/hostname
 
 vconfig set_name_type VLAN_PLUS_VID_NO_PAD
 
-# automagically run firstboot
-[ -z "$FAILSAFE" -a -z "$(nvram get no_root_swap)" ] && {
-       { mount|grep "on / type jffs2" 1>&-; } || firstboot
-}
index 9069bf4a2faaccb68aec6c74d4e3c45491c00d4c..96ac521334b3d1182b9c98b9c7ca50dfc8a58c6f 100755 (executable)
@@ -1,7 +1,6 @@
 #!/bin/sh
 
 ## Please make changes in /etc/firewall.user
-${FAILSAFE:+exit}
 
 . /etc/functions.sh
 WAN=$(nvram get wan_ifname)
index ce60337212d07f38d5e6ef2e8e9287bb2a42bdd7..786a83f377dc2a71e9adeaca5448aeca69e0568d 100755 (executable)
@@ -1,4 +1,10 @@
 #!/bin/sh
+sysctl -p >&-
+
+# automagically run firstboot
+[ "$(nvram get no_root_swap)" != 1 ] && {
+       { mount|grep "on / type tmpfs" 1>&-; } && firstboot switch2jffs
+}
+
 # set leds to normal state
 echo "0x00" > /proc/sys/diag
-sysctl -p >&-
index 88a233662dcdbfb0c3e7b28b98ef67d5974e6d92..5f9ae37a87f085e38ede04bd8024aa077f2425b6 100755 (executable)
@@ -1,37 +1,35 @@
 #!/bin/sh
-is_clean() {
-       OFFSET="$((0x$(dd if=/dev/mtdblock/1 bs=1 skip=$((0x14)) count=2 2>&- | hexdump | grep 0000000 | cut -d ' ' -f 2) - 1))"
-       dd if=/dev/mtdblock/1 bs=1 skip=$OFFSET count=1 2>&- | hexdump -v | grep ' 0000' > /dev/null && return 255 || return 0
+is_dirty() {
+       grep Broadcom /proc/cpuinfo >&- || return 1
+       OFFSET="$(($(hexdump -v /dev/mtdblock/1 -s 20 -n 2 -e '"%d"')-1))"
+       return $(hexdump -v /dev/mtdblock/1 -s $OFFSET -n 1 -e '"%d"')
 }
 
-if [ "$1" != "failsafe" ]; then 
+if [ "$1" != "failsafe" -a "$(nvram get no_root_swap)" != 1 ]; then 
+       mtd unlock linux
        mount | grep jffs2 >&-
        if [ $? = 0 ] ; then
                if [ $(cat /proc/mtd | wc -l) = 6 ]; then
-                       echo 5 > /proc/sys/diag
-                       mtd unlock linux
                        mtd erase OpenWrt
                        jffs2root --move
                else
-                       mtd unlock rootfs
                        mount -o remount,rw /dev/root /
                fi
        else
-               if [ -z "$(nvram get no_root_swap)" ]; then
-                       is_clean || {
-                               mtd erase OpenWrt
-                               mtd unlock linux
-                               jffs2root --clean
-                       }
-                       mtd unlock OpenWrt
-                       mount -t jffs2 /dev/mtdblock/4 /jffs
+               is_dirty
+               if [ $? = 0 ]; then
+                       firstboot ramoverlay >&- 2>&-
+               else
+                       mount /dev/mtdblock/4 /jffs
                        pivot_root /jffs /jffs/rom
                        mount none /proc -t proc
-                       mount none /dev -t devfs
-                       umount /rom/proc /rom/dev >&-
+                       umount /rom/proc
+                       mount -o move /rom/dev /dev
                fi
        fi
 fi
+
 mount none /tmp -t tmpfs -o nosuid,nodev,mode=1777,size=50%
 mkdir -p /dev/pts
 mount none /dev/pts -t devpts
+mount -t sysfs none /sys 2>&-