wok-stable view busybox/stuff/init @ rev 8113

Up: murrine to 0.98.1.1.
author Christopher Rogers <slaxemulator@gmail.com>
date Sun Jan 23 16:00:57 2011 +0000 (2011-01-23)
parents 0751ee65c01f
children ed659050472b
line source
1 #!/bin/sh
3 launch_init()
4 {
5 umount /proc
6 echo -e "\\033[70G[ \\033[1;33mOK\\033[0;39m ]"
7 exec /sbin/switch_root mnt /sbin/init
8 }
10 failed()
11 {
12 umount /proc
13 echo -e "\\033[70G[ \\033[1;31mFailed\\033[0;39m ]"
14 }
16 try_init()
17 {
18 if [ ! -d /mnt/etc ] && grep -q cryptoroot= /proc/cmdline; then
19 modprobe dm-mod
20 modprobe dm-crypt
21 modprobe aes-i586
22 root="$(sed 's/.*cryptoroot=\([^ ]*\).*/\1/' < /proc/cmdline)"
23 dev=${root#/dev/}
24 dmlabel=crypto-$dev
25 if cryptsetup isLuks $root 2> /dev/null; then
26 cryptsetup luksOpen $root $dmlabel
27 else
28 read -s -t 60 -p "Pass phrase : " passphrase
29 key=$(echo $passphrase | hashalot -x -n 32 sha512)
30 blocks=$(cat $(find /sys/block | grep /$dev/size))
31 echo 0 $blocks crypt aes-plain $key 0 $root 0 | \
32 dmsetup create $dmlabel
33 fi
34 mount /dev/mapper/$dmlabel /mnt
35 fi
36 if [ -d /mnt/etc ]; then
37 umount /sys
38 [ -n "$1" ] && for i in $@ ; do
39 cp -a $i /mnt$(dirname $i)
40 done
41 mount /mnt -o remount,ro
42 launch_init
43 fi
44 failed
45 }
47 mount_mapper()
48 {
49 mount $root /mnt
50 try_init /dev/mapper $@
51 }
53 lvmsetup()
54 {
55 grep -q lvmroot= /proc/cmdline || return 1
56 modprobe dm-mod
57 vgscan --ignorelockingfailure
58 vgchange -ay --ignorelockingfailure
59 root="/dev/mapper/$(sed 's/.*lvmroot=\([^ ]*\).*/\1/' < /proc/cmdline)"
60 return 0
61 }
63 load_raid()
64 {
65 while read line; do
66 case "$line" in
67 *raid10*) modprobe raid10 ;;
68 *raid0*) modprobe raid0 ;;
69 *raid1*) modprobe raid1 ;;
70 *raid[456]*) modprobe raid456 ;;
71 esac
72 done
73 }
75 mount -t proc proc /proc
76 mount -t sysfs sysfs /sys
77 if grep -q dmraid= /proc/cmdline; then
78 root="$(sed 's/.*dmraid=\([^ ]*\).*/\1/' < /proc/cmdline)"
79 echo -n "Switching / to dmraid $root..."
80 dmraid -s | grep ^type | awk '{ print $3 }' | load_raid
81 case "$root" in
82 /dev/*);;
83 *) root=/dev/mapper/$(dmraid -s|grep ^name|awk '{print $3}')p${root#p};;
84 esac
85 dmraid -ay
86 lvmsetup
87 mount_mapper
88 fi
89 if grep -q softraid= /proc/cmdline; then
90 root="$(sed 's/.*softraid=\([^ ]*\).*/\1/' < /proc/cmdline)"
91 echo -n "Switching / to softraid $root..."
92 mdadm --examine --scan --config=partitions > /etc/mdadm.conf
93 grep -qs " $root " /etc/mdadm.conf ||
94 root=$(awk '/dev.md/ { print $2; exit }' < /etc/mdadm.conf)
95 grep level=raid /etc/mdadm.conf | load_raid
96 mdadm --assemble --scan
97 lvmsetup
98 mount_mapper /etc/mdadm.conf
99 fi
100 if lvmsetup; then
101 echo -n "Switching / to lvm $root..."
102 mount_mapper
103 fi
104 if grep -q mount= /proc/cmdline; then
105 root="$(sed 's/.*mount=\([^ ]*\).*/\1/' < /proc/cmdline)"
106 dev=$(blkid | grep $root | sed 's/:.*//;q')
107 echo -n "Mounting $dev ($root) ..."
108 if ! mount $dev /mnt; then
109 if echo $dev | grep -q "/dev/sd"; then
110 delay=`cat /sys/module/usb_storage/parameters/delay_use`
111 delay=$((1+$delay))
112 echo -n "sleep for $delay seconds..."
113 sleep $delay
114 fi
115 mount $dev /mnt
116 fi
117 fi
118 if grep -q loopfs= /proc/cmdline; then
119 loopfs="$(sed 's/.*loopfs=\([^ ]*\).*/\1/' < /proc/cmdline)"
120 echo -n "Switching / to loop $loopfs..."
121 losetup /dev/loop0 /mnt/$loopfs
122 mount /dev/loop0 /mnt 2> /dev/null
123 fi
124 grep -q cryptoroot= /proc/cmdline && try_init
125 umount /sys
126 if grep -q subroot= /proc/cmdline; then
127 subroot="/$(sed 's/.*subroot=\([^ ]*\).*/\1/' < /proc/cmdline)" &&
128 if [ -s /usr/share/boot/busybox-static ]; then
129 mv /usr/share/boot/busybox-static .
130 /busybox-static rm -rf /etc /lib /*bin /usr /var
131 exec /busybox-static chroot /mnt$subroot /sbin/init
132 else
133 exec chroot /mnt$subroot /sbin/init
134 fi
135 fi
136 echo -n "Switching / to tmpfs..."
137 size="$(grep rootfssize= < /proc/cmdline | \
138 sed 's/.*rootfssize=\([0-9]*[kmg%]\).*/-o size=\1/')"
139 free=$(busybox free | busybox awk '/Mem:/ { print int(($4*100)/$3) }')
140 umount /proc
141 [ -n "$size" ] || size="-o size=90%"
142 if [ $free -lt 100 ] || ! mount -t tmpfs $size tmpfs /mnt; then
143 echo -e "\\033[70G[ \\033[1;33mSkipped\\033[0;39m]"
144 umount /proc
145 exec /sbin/init
146 fi
147 for i in $(ls -a /); do
148 case "$i" in
149 .|..) ;;
150 mnt) mkdir /mnt/mnt;;
151 *) if ! cp -a /$i /mnt 2> /dev/null; then
152 failed
153 umount /mnt
154 exec /sbin/init
155 fi;;
156 esac
157 done
158 launch_init