wok view busybox/stuff/init @ rev 5978
busybox: add lvm support in /init
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Thu Aug 12 18:56:16 2010 +0200 (2010-08-12) |
parents | c5efd45ce56e |
children | 3f326721653c |
line source
1 #!/bin/sh
3 launch_init()
4 {
5 echo -e "\\033[70G[ \\033[1;33mOK\\033[0;39m ]"
6 exec /sbin/switch_root mnt /sbin/init
7 }
9 failed()
10 {
11 echo -e "\\033[70G[ \\033[1;31mFailed\\033[0;39m ]"
12 }
14 try_init()
15 {
16 if [ ! -d /mnt/etc ] && grep -q cryptoroot= /proc/cmdline; then
17 modprobe dm-mod
18 modprobe dm-crypt
19 modprobe aes-i586
20 root="$(sed 's/.*cryptoroot=\([^ ]*\).*/\1/' < /proc/cmdline)"
21 dev=${root#/dev/}
22 dmlabel=crypto-$dev
23 if cryptsetup isLuks $root 2> /dev/null; then
24 cryptsetup luksOpen $root $dmlabel
25 else
26 read -s -t 60 -p "Pass phrase : " passphrase
27 key=$(echo $passphrase | hashalot -x -n 32 sha512)
28 blocks=$(cat $(find /sys/block | grep /$dev/size))
29 echo 0 $blocks crypt aes-plain $key 0 $root 0 | \
30 dmsetup create $dmlabel
31 fi
32 mount /dev/mapper/$dmlabel /mnt
33 fi
34 if [ -d /mnt/etc ]; then
35 umount /sys
36 [ -n "$1" ] && for i in $@ ; do
37 cp -a $i /mnt$i
38 done
39 umount /proc
40 launch_init
41 fi
42 failed
43 }
45 mount_mapper()
46 {
47 mount $root /mnt
48 try_init /dev/mapper $@
49 }
51 lvmsetup()
52 {
53 if grep -q lvmroot= /proc/cmdline; then
54 modprobe dm-mod
55 vgscan --ignorelockingfailure
56 vgchange -ay --ignorelockingfailure
57 root="/dev/mapper/$(sed 's/.*lvmroot=\([^ ]*\).*/\1/' < /proc/cmdline)"
58 return 0
59 fi
60 return 1
61 }
63 load_raid()
64 {
65 while read line; do
66 case "$line" in
67 *raid1*) modprobe dm-mirror ;;
68 *raid[456]*) modprobe raid456 ;;
69 esac
70 done
71 }
73 mount -t proc proc /proc
74 mount -t sysfs sysfs /sys
75 if grep -q dmraid= /proc/cmdline; then
76 root="$(sed 's/.*dmraid=\([^ ]*\).*/\1/' < /proc/cmdline)"
77 echo -n "Switching / to dmraid $root..."
78 dmraid -s | grep ^type | awk '{ print $3 }' | load_raid
79 case "$root" in
80 /dev/*);;
81 *) root=/dev/mapper/$(dmraid -s|grep ^name|awk '{print $3}')p${root#p};;
82 esac
83 dmraid -ay
84 lvmsetup
85 mount_mapper
86 fi
87 if grep -q softraid= /proc/cmdline; then
88 root="$(sed 's/.*softraid=\([^ ]*\).*/\1/' < /proc/cmdline)"
89 echo -n "Switching / to softraid $root..."
90 mdadm --examine --scan --config=partitions > /etc/mdadm.conf
91 grep level=raid /etc/mdadm.conf | load_raid
92 mdadm --assemble --scan
93 lvmsetup
94 mount_mapper /etc/mdadm.conf
95 fi
96 if lvmsetup; then
97 echo -n "Switching / to lvm $root..."
98 mount_mapper
99 fi
100 if grep -q loopfs= /proc/cmdline; then
101 loopfs="$(sed 's/.*loopfs=\([^ ]*\).*/\1/' < /proc/cmdline)"
102 dev=${loopfs%,*}
103 loopfs=${loopfs#*,}
104 echo -n "Switching / to loop $loopfs on $dev..."
105 if ! mount $dev /mnt; then
106 if echo $dev | grep -q "/dev/sd"; then
107 delay=`cat /sys/module/usb_storage/parameters/delay_use`
108 delay=$((1+$delay))
109 echo -n "sleep for $delay seconds..."
110 sleep $delay
111 fi
112 mount $dev /mnt || loopfs=""
113 fi
114 if [ -n "$loopfs" ]; then
115 losetup /dev/loop0 /mnt/$loopfs
116 mount /dev/loop0 /mnt 2> /dev/null
117 fi
118 try_init
119 fi
120 grep -q cryptoroot= /proc/cmdline && try_init
121 umount /sys
122 echo -n "Switching / to tmpfs..."
123 size="$(grep rootfssize= < /proc/cmdline | \
124 sed 's/.*rootfssize=\([0-9]*[kmg%]\).*/-o size=\1/')"
125 free=$(busybox free | busybox awk '/Mem:/ { print int(($4*100)/$3) }')
126 umount /proc
127 [ -n "$size" ] || size="-o size=90%"
128 if [ $free -lt 100 ] || ! mount -t tmpfs $size tmpfs /mnt; then
129 echo -e "\\033[70G[ \\033[1;33mSkipped\\033[0;39m]"
130 exec /sbin/init
131 fi
132 for i in $(ls -a /); do
133 case "$i" in
134 .|..) ;;
135 mnt) mkdir /mnt/mnt;;
136 *) if ! cp -a /$i /mnt 2> /dev/null; then
137 failed
138 umount /mnt
139 exec /sbin/init
140 fi;;
141 esac
142 done
143 launch_init