slitaz-boot-scripts view init @ rev 408

init: create missing block devices
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Aug 02 15:13:28 2015 +0200 (2015-08-02)
parents 1f15d8a06ea9
children e8f0c7c134f1
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 & 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
53 fail()
54 {
55 echo -e '\033[70G[ \033[1;3'${1:-1mFailed}'\033[0;39m ]'
56 }
58 quit()
59 {
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 }
72 arg()
73 {
74 grep -q $1 /proc/cmdline
75 }
77 got()
78 {
79 arg $1= && root="$(sed "s/.*$1=\([^ ]*\).*/\1/" </proc/cmdline)"
80 }
82 msg()
83 {
84 echo "Switching / to $1..."
85 }
87 use()
88 {
89 got $1 && msg "$1 $root"
90 }
92 mnt()
93 {
94 r=$(blkid 2> /dev/null | sed "s|\"||g;/$(echo $root | tr / .)/!d;s|:.*||;q")
95 r=${r:-$root}
96 r=${r#/dev/}
97 r=/dev/${r%%/*}
98 d=${root#*$r}
99 busybox mount $r $1 && return
100 w=$(cat /sys/module/usb_storage/parameters/delay_use)
101 w=$((1+${w:-2}))
102 echo -n "Sleep $w seconds..."
103 sleep $w
104 busybox mount $r $1
105 }
107 mod()
108 {
109 for i in $@; do
110 echo "Loading module: $i"
111 modprobe $i 2>/dev/null || busybox insmod $(find /lib/modules|sed "/$i.ko/!dq")
112 done
113 }
115 try()
116 {
117 if [ ! -d /mnt/etc ] && got cryptoroot; then
118 mod dm-mod dm-crypt aes-256
119 d=${root#/dev/}
120 l=crypto-$d
121 if cryptsetup isLuks $root 2>/dev/null; then
122 cryptsetup luksOpen $root $l
123 else
124 read -st 60 -p "Pass phrase : " p
125 k=$(echo $p|hashalot -x -n 32 sha512)
126 echo 0 $(cat $(find /sys/block|grep /$d/size))\
127 crypt aes-plain $k 0 $root 0|dmsetup create $l
128 fi
129 busybox mount /dev/mapper/$l /mnt
130 fi
131 got subroot && return
132 got loopfs && return
133 if [ -d /mnt/etc ]; then
134 for i in $@; do
135 cp -a $i /mnt$(dirname $i)
136 done
137 quit
138 fi
139 fail
140 }
142 lvm()
143 {
144 use lvmroot || return
145 mod dm-mod
146 vgscan --ignorelockingfailure
147 vgchange -ay --ignorelockingfailure
148 busybox mount /dev/mapper/$root /mnt
149 try /dev/mapper $1
150 }
152 ldraid()
153 {
154 while read l; do
155 case "$l" in
156 *raid10*) mod raid10;;
157 *raid0*) mod raid0;;
158 *raid1*) mod raid1;;
159 *raid*) mod raid456;;
160 *mirror*) mod dm-mirror;;
161 esac
162 done
163 }
165 if [ "$1" != "log" ]; then
166 busybox mount -t proc proc /proc
167 busybox mount -t sysfs sys /sys
168 busybox mount -t tmpfs tmpfs /run
169 for i in /sys/block/*/dev /sys/block/*/*/dev ; do
170 n=/dev/$(basename ${i%/dev})
171 [ -e $n ] || mknod $n b $(sed 's/:/ /' < $i)
172 done
173 x=/sbin/init; echo "[ -x $x ] && exec $x" >/run/init
174 $0 log 2>&1 | tee /run/boot.log
175 busybox umount /proc
176 busybox umount /sys
177 . /run/init
178 sh
179 fi
180 got modprobe && mod ${root//,/ }
181 [ -d /proc/sys/dev/cdrom ] &&
182 ln -s $(sed '/name/!d;s/.*:[^a-z]*//' /proc/sys/dev/cdrom/info) /dev/cdrom
183 if use dmraid; then
184 dmraid -s | sed '/^type/!ds/.*: *//' | ldraid
185 [ ${root:0:4} = /dev ] ||
186 root=/dev/mapper/$(dmraid -s|sed '/^name/!ds/.*: *//')p${root#p}
187 dmraid -ay
188 fi
189 use raiddev && raiddev="DEVICE ${root//,/ }"
190 use raidmail && raidmail="MAILADDR $root"
191 if use softraid; then
192 mdadm -E -s -c partitions > /etc/mdadm.conf
193 [ "$raiddev" ] && echo "$raiddev" >> /etc/mdadm.conf
194 [ "$raidmail" ] && echo "$raidmail" >> /etc/mdadm.conf
195 grep -qs " $root " /etc/mdadm.conf ||
196 root=$(awk '/dev.md/{print $2;exit}' < /etc/mdadm.conf)
197 grep level=raid /etc/mdadm.conf | ldraid
198 for i in 1 2 3 4 5 6 7 8 9; do
199 sleep $i
200 mdadm -A -s
201 grep -qs ': act' /proc/mdstat && break
202 done
203 lvm /etc/mdadm.conf
204 fi
205 lvm
206 if got mount; then
207 dev=$root
208 x=$(blkid|grep $dev|sed 's/:.*//;q')
209 root=${x:-$dev}
210 [ "$dev" = "$root" ] || dev="$root ($dev)"
211 echo "Mount $dev..."
212 mnt /mnt
213 arg posixovl && echo "And posixovl..." &&
214 mount.posixovl -F /mnt -- -oallow_other -odefault_permissions -osuid
215 fi
216 got loopfs && echo "Into file $root..." &&
217 losetup /dev/loop0 /mnt/$root && busybox mount /dev/loop0 /mnt
218 got bindfs && echo "Bind ${root/,/ to }..." &&
219 busybox mount --bind /mnt/${root%,*} /mnt/${root/,//}
220 arg cryptoroot= && try
221 if use subroot; then
222 cp $(LD_TRACE_LOADED_OBJECTS=1 /lib/ld*.so /usr/sbin/chroot | sed\
223 's|.*=> \(/lib/l[^ ]*\).*|\1|;/^\//!d') /usr/sbin/chroot /run
224 r=$root/run
225 quit $r "export LD_LIBRARY_PATH=$r:/lib"\
226 "$root$(ls /run/ld-*so) $r/chroot $root" "/$root"
227 fi
228 quit
229 msg tmpfs
230 root=100
231 got tmpram
232 r=$root
233 inodes=0
234 got rootfsinodes && inodes=$root
235 root=90%
236 got rootfssize
237 [ $(busybox free|busybox awk '/Mem:/{print int(($4*100)/$3)}') -ge $r ] &&
238 busybox mount -t tmpfs -o size=$root,nr_inodes=$inodes tmpfs /mnt &&
239 for i in $(ls -ar /); do
240 case "$i" in
241 .*|cdrom) ;;
242 mnt|proc|sys) mkdir /mnt/$i;;
243 usr|var|rootfs*) mv /$i /mnt;;
244 *) cp -a /$i /mnt 2>/dev/null && continue
245 fail
246 busybox umount /mnt
247 exit
248 esac
249 done || fail 3mSkipped
250 quit
251 mod squashfs 2>/dev/null || exit
252 msg aufs
253 br=/mnt/.rw
254 mkdir $br /mnt/.wd
255 got rwdev && mnt $br && br=$br$d
256 o=
257 p=
258 c=/mnt/.cdrom
259 if [ -z "$(ls /mnt/rootfs* 2>/dev/null)" ]; then
260 root=/dev/cdrom/fs
261 got rodev
262 mkdir -p $c /mnt$c /mnt/.rw$c
263 mnt $c
264 o="-o 124"
265 p=/.cdrom/boot
266 c=$c$d
267 fi
268 l=0
269 r=
270 got isofs && r=:$c || for i in /mnt$p/rootfs?*.gz; do
271 fs=${i#*root}
272 r=$r:/mnt/.$fs
273 mkdir -p /mnt/.rw/mnt/.$fs /mnt/.$fs
274 losetup $o /dev/loop$l $i
275 busybox mount -o ro -t squashfs /dev/loop$((l++)) /mnt/.$fs
276 done
277 while read type opt; do
278 mod $type && busybox mount -t $type -o $opt none /mnt && break
279 done <<EOT
280 aufs br=$br$r
281 overlayfs workdir=/mnt/.wd${r/:/,lowerdir=},upperdir=$br
282 EOT
283 quit