source: trunk/openwrt/files/package/base-files/files/etc/preinit @ 329

Last change on this file since 329 was 329, checked in by amain, 8 years ago

Add alternate bootscript (/etc/preinit)

  • Property svn:executable set to *
File size: 2.2 KB
RevLine 
[329]1#!/bin/sh
2#
3# DebWrt boot script
4#
5# - discover root device (LABEL=DEBWRT_ROOT)
6# - fallback to /dev/sda1
7# - optional kexec to aternate kernel located in DEBWRT_ROOT/boot
8# - mount root device, pivot_root to it and run /sbin/init
9
10#set -x
11
12mount -t sysfs sysfs /sys
13mount -t proc  none  /proc
14mount -t tmpfs tmpfs /dev -o mode=0755,size=512K
15mount -t tmpfs tmpfs /tmp -o size=3m,nosuid,nodev,mode=1777 
16[ -c /dev/console ] || mknod /dev/console c 5 1
17/sbin/hotplug2 --set-worker /lib/hotplug2/worker_fork.so --set-rules-file /etc/hotplug2-init.rules --no-persistent --set-coldplug-cmd /sbin/udevtrigger
18/sbin/hotplug2 --set-worker /lib/hotplug2/worker_fork.so --set-rules-file /etc/hotplug2-init.rules --persistent &
19
20WAIT=10
21ROOT_LABEL=DEBWRT_ROOT
22DEFAULT_DEV=/dev/sda1
23
24wait_for_root_device() { # return: (default) root device
25   local n=0
26   local dev=""
27
28   while [ $n -lt $WAIT ]; do
29      dev=$(blkid -t LABEL=$ROOT_LABEL)
30      if [ "" != "$dev" ]; then
31         dev=${dev/:*/}
32         break;
33      fi
34      sleep 1
35      n=$((n+1))
36   done
37
38   dev=${dev:-$DEFAULT_DEV}
39   echo $dev
40}
41
42echo "[ ---------- ] DebWrt boot"
43echo -n "[ ---------- ] Waiting for device with LABEL=$ROOT_LABEL..."
44dev=$(wait_for_root_device)
45
46if [ -b $dev ]; then 
47   echo "found. Booting from $dev."
48   # root device found
49   # pivot_root to it and run /sbin/init
50   # or kexec if /boot/vmlinux.elf is found on the new root
51
52   killall hotplug2
53   mount $dev /mnt
54
55   if [ -f /mnt/boot/vmlinux.elf ] && ! grep -q "kexec=1" /proc/cmdline; then
56     # kexec into new kernel
57     echo "[ ---------- ] kexec kernel found. Kexec into it."
58
59     cmdline=$(cat /proc/cmdline)
60
61     [ -f /mnt/boot/kexec.conf ] && source /mnt/boot/kexec.conf  # source append= variable
62
63     kexec -l /mnt/boot/vmlinux.elf --append="kexec=1 $cmdline $append"
64     kexec -e
65   else
66     # pivot_root and run /sbin/init
67     umount /proc 2>&-
68     umount /sys  2>&-
69     umount /tmp  2>&-
70     umount /dev  2>&-
71
72     pivot_root /mnt /mnt/mnt
73     exec /bin/sh -c "umount /mnt; exec /sbin/init"
74  fi
75
76else                         
77   # no root device available
78   # spwan normal shell
79   echo "not found. Starting shell."
80   exec /bin/sh
81fi
82
Note: See TracBrowser for help on using the repository browser.