wok-stable view busybox/stuff/init @ rev 7845
busybox-static: add chroot
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Tue Dec 28 16:44:48 2010 +0100 (2010-12-28) |
parents | 7b3b7e2a569d |
children | aae44f936ae8 |
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$subroot /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$subroot/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$subroot/etc ]; then
37 umount /sys
38 [ -n "$1" ] && for i in $@ ; do
39 cp -a $i /mnt$subroot$(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 if grep -q lvmroot= /proc/cmdline; then
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 fi
62 return 1
63 }
65 load_raid()
66 {
67 while read line; do
68 case "$line" in
69 *raid10*) modprobe raid10 ;;
70 *raid0*) modprobe raid0 ;;
71 *raid1*) modprobe raid1 ;;
72 *raid[456]*) modprobe raid456 ;;
73 esac
74 done
75 }
77 mount -t proc proc /proc
78 mount -t sysfs sysfs /sys
79 if grep -q dmraid= /proc/cmdline; then
80 root="$(sed 's/.*dmraid=\([^ ]*\).*/\1/' < /proc/cmdline)"
81 echo -n "Switching / to dmraid $root..."
82 dmraid -s | grep ^type | awk '{ print $3 }' | load_raid
83 case "$root" in
84 /dev/*);;
85 *) root=/dev/mapper/$(dmraid -s|grep ^name|awk '{print $3}')p${root#p};;
86 esac
87 dmraid -ay
88 lvmsetup
89 mount_mapper
90 fi
91 if grep -q softraid= /proc/cmdline; then
92 root="$(sed 's/.*softraid=\([^ ]*\).*/\1/' < /proc/cmdline)"
93 echo -n "Switching / to softraid $root..."
94 mdadm --examine --scan --config=partitions > /etc/mdadm.conf
95 grep -qs " $root " /etc/mdadm.conf ||
96 root=$(awk '/dev.md/ { print $2; exit }' < /etc/mdadm.conf)
97 grep level=raid /etc/mdadm.conf | load_raid
98 mdadm --assemble --scan
99 lvmsetup
100 mount_mapper /etc/mdadm.conf
101 fi
102 if lvmsetup; then
103 echo -n "Switching / to lvm $root..."
104 mount_mapper
105 fi
106 if grep -q mount= /proc/cmdline; then
107 root="$(sed 's/.*mount=\([^ ]*\).*/\1/' < /proc/cmdline)"
108 dev=$(blkid | grep $root | sed 's/:.*//;q')
109 echo -n "Mounting $dev ($root) ..."
110 if ! mount $dev /mnt; then
111 if echo $dev | grep -q "/dev/sd"; then
112 delay=`cat /sys/module/usb_storage/parameters/delay_use`
113 delay=$((1+$delay))
114 echo -n "sleep for $delay seconds..."
115 sleep $delay
116 fi
117 mount $dev /mnt
118 fi
119 fi
120 if grep -q loopfs= /proc/cmdline; then
121 loopfs="$(sed 's/.*loopfs=\([^ ]*\).*/\1/' < /proc/cmdline)"
122 echo -n "Switching / to loop $loopfs..."
123 losetup /dev/loop0 /mnt/$loopfs
124 mount /dev/loop0 /mnt 2> /dev/null
125 fi
126 grep -q cryptoroot= /proc/cmdline && try_init
127 umount /sys
128 grep -q subroot= /proc/cmdline &&
129 subroot="/$(sed 's/.*subroot=\([^ ]*\).*/\1/' < /proc/cmdline)" &&
130 exec chroot /mnt$subroot /sbin/init
131 echo -n "Switching / to tmpfs..."
132 size="$(grep rootfssize= < /proc/cmdline | \
133 sed 's/.*rootfssize=\([0-9]*[kmg%]\).*/-o size=\1/')"
134 free=$(busybox free | busybox awk '/Mem:/ { print int(($4*100)/$3) }')
135 [ -n "$size" ] || size="-o size=90%"
136 if [ $free -lt 100 ] || ! mount -t tmpfs $size tmpfs /mnt; then
137 echo -e "\\033[70G[ \\033[1;33mSkipped\\033[0;39m]"
138 umount /proc
139 exec /sbin/init
140 fi
141 for i in $(ls -a /); do
142 case "$i" in
143 .|..) ;;
144 mnt) mkdir /mnt/mnt;;
145 *) if ! cp -a /$i /mnt 2> /dev/null; then
146 failed
147 umount /mnt
148 exec /sbin/init
149 fi;;
150 esac
151 done
152 launch_init