slitaz-boot-scripts view init @ rev 419

init: enforce soft raid init (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Dec 24 11:38:20 2015 +0100 (2015-12-24)
parents 220cadd40305
children d874107263eb
line source
1 #!/bin/sh
3 ### cached kernel parameters :
4 #
5 # modprobe=
6 # list of modules to load, example modprobe=r8169,btrfs
7 #
8 ### Frugal mode or live CD : using a root ram disk.
9 #
10 # tmpram= [RAMDISK]
11 # minimum % of free RAM to build the root tmpfs, default 100
12 # a lower value may work, such as 50
13 # rootfssize= [RAMDISK]
14 # size of / regarding the RAM size, default 90%
15 # rootfsinodes= [RAMDISK]
16 # files count of / regarding the RAM size, default 0 (unlimited)
17 #
18 ### Indirect mode ; an initramfs is used to mount root from the hard disk.
19 ### Any rootfs should fit, minimum required is slitaz-base.
20 #
21 # mount= [MOUNT]
22 # device with root loop file or sub directory
23 # posixovl [MOUNT]
24 # use posix attributes on a non posix filesystem (VFAT, NTFS...)
25 # loopfs= [MOUNT]
26 # loop file for root filesystem
27 # subroot= [MOUNT]
28 # sub directory for root filesystem
29 # bindfs= [MOUNT] (deprecated)
30 # bind out of root tree part
31 #
32 ### Special disk mode ; needs extra modules/softwares from preinit flavor.
33 #
34 # dmraid= [RAID]
35 # root semi hardware raid device name in /dev/mapper
36 # softraid= [RAID]
37 # root software device name in /dev/mapper
38 # lvmroot= [LVM]
39 # root logical volume name in /dev/mapper
40 # cryptoroot= [CRYPTO]
41 # encrypted root device using luks or loop-aes
42 #
43 ### Special mode used by loram flavor with aufs/overlayfs & squashfs
44 ### incremental/nested rootfs (russian dolls).
45 #
46 # rodev= [LORAM][MERGE]
47 # device (+ optional path) for the read only aufs branch
48 # rwdev= [LORAM][MERGE]
49 # (persistent) device for the r/w aufs branch instead of the ramfs
50 # isofs [LORAM]
51 # do not use squashfs filesystem images, but the cdrom filesystem only
54 fail() {
55 echo -e '\033[70G[ \033[1;3'${1:-1mFailed}'\033[0;39m ]'
56 }
59 quit() {
60 [ -d /mnt$4/etc ] || return
61 [ -n "$4" ] || busybox mount /mnt -o remount,ro
62 busybox mount --move /run /mnt/${1:-run}
63 cat > /run/init <<EOT
64 $2
65 x=/sbin/switch_root
66 [ -x \$x ] && exec \$x mnt $3 /sbin/init
67 EOT
68 fail 2mDone
69 exit
70 }
73 arg() {
74 grep -q $1 /proc/cmdline
75 }
78 got() {
79 arg $1= && root="$(sed "s/.*$1=\([^ ]*\).*/\1/" </proc/cmdline)"
80 }
83 msg() {
84 echo "Switching / to $1..."
85 }
88 use() {
89 got $1 && msg "$1 $root"
90 }
93 mnt() {
94 r=$(busybox blkid 2> /dev/null | sed "s|\"||g;/${root//[^a-zA-Z0-9]/.}/!d;s|:.*||;q")
95 r=${r:-$root}
96 if [ ! -b $r ]; then
97 r=${r#/dev/}
98 r=/dev/${r%%/*}
99 fi
100 d=${root#*$r}
101 busybox mount $r $1 && return
102 w=$(cat /sys/module/usb_storage/parameters/delay_use)
103 w=$((1+${w:-2}))
104 echo -n "Sleep $w seconds..."
105 sleep $w
106 busybox mount $r $1
107 }
110 mod() {
111 for i in $@; do
112 echo "Loading module: $i"
113 modprobe $i 2>/dev/null || busybox insmod $(find /lib/modules|sed "/$i.ko/!dq")
114 done
115 }
118 try() {
119 if [ ! -d /mnt/etc ] && got cryptoroot; then
120 mod dm-mod dm-crypt aes-256
121 d=${root#/dev/}
122 l=crypto-$d
123 if cryptsetup isLuks $root 2>/dev/null; then
124 cryptsetup luksOpen $root $l
125 else
126 read -st 60 -p "Pass phrase : " p
127 k=$(echo $p|hashalot -x -n 32 sha512)
128 echo 0 $(cat $(find /sys/block|grep /$d/size))\
129 crypt aes-plain $k 0 $root 0|dmsetup create $l
130 fi
131 busybox mount /dev/mapper/$l /mnt
132 fi
133 got subroot && return
134 got loopfs && return
135 if [ -d /mnt/etc ]; then
136 for i in $@; do
137 cp -a $i /mnt$(dirname $i)
138 done
139 quit
140 fi
141 fail
142 }
145 lvm() {
146 use lvmroot || return
147 mod dm-mod
148 vgscan --ignorelockingfailure
149 vgchange -ay --ignorelockingfailure
150 busybox mount /dev/mapper/$root /mnt
151 try /dev/mapper $1
152 }
155 ldraid() {
156 while read l; do
157 case "$l" in
158 *raid10*) mod raid10;;
159 *raid0*) mod raid0;;
160 *raid1*) mod raid1;;
161 *raid*) mod raid456;;
162 *mirror*) mod dm-mirror;;
163 esac
164 done
165 }
170 if [ "$1" != 'log' ]; then
171 busybox mount -t proc proc /proc
172 busybox mount -t sysfs sys /sys
173 busybox mount -t tmpfs tmpfs /run
174 x=/sbin/init; echo "[ -x $x ] && exec $x" >/run/init
175 $0 log 2>&1 | tee /run/boot.log
176 busybox umount /sys
177 busybox umount /proc
178 . /run/init
179 sh
180 fi
182 for i in /sys/block/*/dev /sys/block/*/*/dev ; do
183 [ -s "$i" ] || continue
184 n=${i%/dev}
185 n=/dev/${n##*/}
186 [ -e $n ] && continue
187 echo "Create $n "
188 mknod $n b $(sed 's/:/ /' < $i)
189 done
191 got modprobe && mod ${root//,/ }
193 [ -d /proc/sys/dev/cdrom ] &&
194 ln -s $(sed '/name/!d;s/.*:[^a-z]*//' /proc/sys/dev/cdrom/info) /dev/cdrom
196 if use dmraid; then
197 dmraid -s | sed '/^type/!ds/.*: *//' | ldraid
198 [ ${root:0:4} = /dev ] ||
199 root=/dev/mapper/$(dmraid -s|sed '/^name/!ds/.*: *//')p${root#p}
200 dmraid -ay
201 fi
202 use raiddev && raiddev="DEVICE ${root//,/ }"
203 use raidmail && raidmail="MAILADDR $root"
204 if use softraid; then
205 for i in 1 2 3 4 5 6 7 8 9; do
206 mdadm -E -s -c partitions > /etc/mdadm.conf
207 grep -qs " $root " /etc/mdadm.conf && break
208 sleep $i
209 done
210 [ "$raiddev" ] && echo "$raiddev" >> /etc/mdadm.conf
211 [ "$raidmail" ] && echo "$raidmail" >> /etc/mdadm.conf
212 grep level=raid /etc/mdadm.conf | ldraid
213 for i in 1 2 3 4 5 6 7 8 9; do
214 sleep $i
215 mdadm -A -s
216 grep -qs "^$(basename $root) : act" /proc/mdstat && break
217 done
218 grep -qs "^$(basename $root) : act" /proc/mdstat ||
219 root=$(awk '/^md/ { print "/dev/" $1; exit }' < /proc/mdstat)
220 lvm /etc/mdadm.conf
221 fi
222 lvm
224 if got mount; then
225 dev=$root
226 x=$(busybox blkid|grep $dev|sed 's/:.*//;q')
227 root=${x:-$dev}
228 [ "$dev" = "$root" ] || dev="$root ($dev)"
229 echo "Mount $dev..."
230 mnt /mnt
231 arg posixovl && echo "And posixovl..." &&
232 mount.posixovl -F /mnt -- -oallow_other -odefault_permissions -osuid
233 fi
235 got loopfs && echo "Into file $root..." &&
236 losetup /dev/loop0 /mnt/$root && busybox mount /dev/loop0 /mnt
237 got bindfs && echo "Bind ${root/,/ to }..." &&
238 busybox mount --bind /mnt/${root%,*} /mnt/${root/,//}
239 arg cryptoroot= && try
240 if use subroot; then
241 cp $(LD_TRACE_LOADED_OBJECTS=1 /lib/ld*.so /usr/sbin/chroot | sed\
242 's|.*=> \(/lib/l[^ ]*\).*|\1|;/^\//!d') /usr/sbin/chroot /run
243 r=$root/run
244 quit $r "export LD_LIBRARY_PATH=$r:/lib"\
245 "$root$(ls /run/ld-*so) $r/chroot $root" "/$root"
246 fi
247 quit
248 msg tmpfs
249 root=100
250 got tmpram
251 r=$root
252 inodes=0
253 got rootfsinodes && inodes=$root
254 root=90%
255 got rootfssize
256 [ $(busybox free|busybox awk '/Mem:/{print int(($4*100)/$3)}') -ge $r ] &&
257 busybox mount -t tmpfs -o size=$root,nr_inodes=$inodes tmpfs /mnt &&
258 for i in $(ls -ar /); do
259 case "$i" in
260 .*|cdrom) ;;
261 mnt|proc|sys) mkdir /mnt/$i;;
262 usr|var|rootfs*) mv /$i /mnt;;
263 *) cp -a /$i /mnt 2>/dev/null && continue
264 fail
265 busybox umount /mnt
266 exit
267 esac
268 done || fail 3mSkipped
269 quit
270 mod squashfs 2>/dev/null || exit
271 msg aufs
272 br=/mnt/.rw
273 mkdir $br /mnt/.wd
274 got rwdev && mnt $br && br=$br$d
275 o=
276 p=
277 c=/mnt/.cdrom
278 if [ -z "$(ls /mnt/rootfs* 2>/dev/null)" ]; then
279 root=/dev/cdrom/fs
280 got rodev
281 mkdir -p $c /mnt$c /mnt/.rw$c
282 mnt $c
283 o="-o 124"
284 p=/.cdrom/boot
285 c=$c$d
286 fi
287 l=0
288 r=
289 got isofs && r=:$c || for i in /mnt$p/rootfs?*.gz; do
290 fs=${i#*root}
291 r=$r:/mnt/.$fs
292 mkdir -p /mnt/.rw/mnt/.$fs /mnt/.$fs
293 losetup $o /dev/loop$l $i
294 busybox mount -o ro -t squashfs /dev/loop$((l++)) /mnt/.$fs
295 done
296 while read type opt; do
297 mod $type && busybox mount -t $type -o $opt none /mnt && break
298 done <<EOT
299 aufs br=$br$r
300 overlayfs workdir=/mnt/.wd${r/:/,lowerdir=},upperdir=$br
301 EOT
302 quit