slitaz-boot-scripts view init @ rev 457

Remove cat process
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Oct 20 12:13:09 2019 +0200 (3 months ago)
parents 161b43b7365c
children
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 grep -q serial /proc/ioports && ! grep -q ttyS /mnt$4/etc/inittab &&
62 echo "ttyS0::respawn:/sbin/getty 115200,38400,9600,4800,2400 ttyS0" \
63 >> /mnt$4/etc/inittab
64 [ -n "$4" ] || grep -q 'tmpfs /mnt tmpfs' /proc/mounts ||
65 busybox mount /mnt -o remount,ro
66 busybox mount --move /run /mnt/${1:-run}
67 cat > /run/init <<EOT
68 $2
69 x=/sbin/switch_root
70 [ -x \$x ] && exec \$x mnt $3 /sbin/init
71 EOT
72 fail 2mDone
73 exit
74 }
77 arg() {
78 grep -q $1 /proc/cmdline
79 }
82 got() {
83 arg $1= && root="$(sed "s/.*$1=\\([^ ]*\\).*/\\1/" </proc/cmdline)"
84 }
87 msg() {
88 echo "Switching / to $1..."
89 }
92 use() {
93 got $1 && msg "$1 $root"
94 }
97 bbmnt() {
98 r=$(busybox blkid 2> /dev/null | grep "$root[:\"]" | sed 's|:.*||;q')
99 r=${r:-$root}
100 if [ ! -b $r ]; then
101 r=${r#/dev/}
102 r=/dev/${r%%/*}
103 fi
104 d=${root#*$r}
105 busybox mount $r $1
106 }
109 mnt() {
110 bbmnt $1 && return
111 w=$(cat /sys/module/usb_storage/parameters/delay_use)
112 w=$((1+${w:-2}))
113 echo "Sleep $w seconds..."
114 sleep $w
115 bbmnt $1
116 }
119 mod() {
120 for i in $@; do
121 echo "Loading module: $i"
122 modprobe $i 2>/dev/null ||
123 for j in $(find /lib/modules|sed "/\/$i.ko/!d"); do
124 busybox insmod $j && break
125 done
126 done
127 }
130 try() {
131 if [ ! -d /mnt/etc ] && got cryptoroot; then
132 mod dm-mod dm-crypt aes-256
133 d=${root#/dev/}
134 l=crypto-$d
135 if cryptsetup isLuks $root 2>/dev/null; then
136 cryptsetup luksOpen $root $l
137 else
138 read -st 60 -p "Pass phrase : " p
139 k=$(echo $p|hashalot -x -n 32 sha512)
140 echo 0 $(cat $(find /sys/block|grep /$d/size))\
141 crypt aes-plain $k 0 $root 0|dmsetup create $l
142 fi
143 busybox mount /dev/mapper/$l /mnt
144 fi
145 got subroot && return
146 got loopfs && return
147 if [ -d /mnt/etc ]; then
148 for i in $@; do
149 cp -a $i /mnt$(dirname $i)
150 done
151 quit
152 fi
153 fail
154 }
157 lvm() {
158 use lvmroot || return
159 mod dm-mod
160 vgscan --ignorelockingfailure
161 vgchange -ay --ignorelockingfailure
162 busybox mount /dev/mapper/$root /mnt
163 try /dev/mapper $1
164 }
167 ldraid() {
168 while read l; do
169 case "$l" in
170 *raid10*) mod raid10;;
171 *raid0*) mod raid0;;
172 *raid1*) mod raid1;;
173 *raid*) mod raid456;;
174 *mirror*) mod dm-mirror;;
175 esac
176 done
177 }
182 if [ "$1" != 'log' ]; then
183 busybox mount -t proc proc /proc
184 busybox mount -t sysfs sys /sys
185 busybox mount -t tmpfs tmpfs /run
186 x=/sbin/init; echo "[ -x $x ] && exec $x" >/run/init
187 $0 log 2>&1 | tee /run/boot.log
188 busybox umount /sys
189 busybox umount /proc
190 . /run/init
191 sh
192 fi
194 for i in /sys/block/*/dev /sys/block/*/*/dev ; do
195 [ -s "$i" ] || continue
196 n=${i%/dev}
197 n=/dev/${n##*/}
198 [ -e $n ] && continue
199 echo "Create $n "
200 mknod $n b $(sed 's/:/ /' < $i)
201 done
203 got modprobe && mod ${root//,/ }
205 [ -d /proc/sys/dev/cdrom ] &&
206 ln -s $(sed '/name/!d;s/.*:[^a-z]*//' /proc/sys/dev/cdrom/info) /dev/cdrom
208 if use dmraid; then
209 dmraid -s | sed '/^type/!ds/.*: *//' | ldraid
210 [ ${root:0:4} = /dev ] ||
211 root=/dev/mapper/$(dmraid -s|sed '/^name/!ds/.*: *//')p${root#p}
212 dmraid -ay
213 fi
214 use raiddev && raiddev="DEVICE ${root//,/ }"
215 use raidmail && raidmail="MAILADDR $root"
216 if use softraid; then
217 for i in 1 2 3 4 5 6 7 8 9; do
218 mdadm -E -s -c partitions > /etc/mdadm.conf
219 grep -qs " $root " /etc/mdadm.conf && break
220 sleep $i
221 done
222 [ "$raiddev" ] && echo "$raiddev" >> /etc/mdadm.conf
223 [ "$raidmail" ] && echo "$raidmail" >> /etc/mdadm.conf
224 grep level=raid /etc/mdadm.conf | ldraid
225 udevd --daemon
226 udevadm trigger
227 udevadm settle
228 for i in 1 2 3 4 5 6 7 8 9; do
229 sleep $i
230 mdadm -A -s
231 for j in $(sed '/\/md/!d;s|.*md/*\([0-9]*\).*|md\1|' \
232 /etc/mdadm.conf); do
233 grep -qs "$j : act" /proc/mdstat || continue 2
234 done
235 break
236 done
237 grep -qs "^$(basename $root) : act" /proc/mdstat ||
238 root=$(awk '/^md/ { print "/dev/" $1; exit }' < /proc/mdstat)
239 lvm /etc/mdadm.conf
240 fi
241 lvm
243 if got mount; then
244 dev=$root
245 x=$(busybox blkid|grep $dev|sed 's/:.*//;q')
246 root=${x:-$dev}
247 [ "$dev" = "$root" ] || dev="$root ($dev)"
248 echo "Mount $dev..."
249 mnt /mnt
250 arg posixovl && echo "And posixovl..." &&
251 mount.posixovl -F /mnt -- -oallow_other -odefault_permissions -osuid
252 fi
254 got loopfs && echo "Into file $root..." &&
255 losetup /dev/loop0 /mnt/$root && busybox mount /dev/loop0 /mnt
256 got bindfs && echo "Bind ${root/,/ to }..." &&
257 busybox mount --bind /mnt/${root%,*} /mnt/${root/,//}
258 arg cryptoroot= && try
259 if use subroot; then
260 cp $(LD_TRACE_LOADED_OBJECTS=1 /lib/ld*.so /usr/sbin/chroot | sed\
261 's|.*=> \(/lib/l[^ ]*\).*|\1|;/^\//!d') /usr/sbin/chroot /run
262 r=$root/run
263 quit $r "export LD_LIBRARY_PATH=$r:/lib"\
264 "$root$(ls /run/ld-*so) $r/chroot $root" "/$root"
265 fi
266 quit
267 msg tmpfs
268 root=100
269 got tmpram
270 r=$root
271 inodes=0
272 got rootfsinodes && inodes=$root
273 root=90%
274 got rootfssize
275 [ $(busybox free|busybox awk '/Mem:/{print int(($4*100)/$3)}') -ge $r ] &&
276 busybox mount -t tmpfs -o size=$root,nr_inodes=$inodes tmpfs /mnt &&
277 for i in $(ls -ar /); do
278 case "$i" in
279 .*|cdrom) ;;
280 mnt|proc|sys) mkdir /mnt/$i;;
281 usr|var|rootfs*) mv /$i /mnt;;
282 *) cp -a /$i /mnt 2>/dev/null && continue
283 fail
284 busybox umount /mnt
285 exit
286 esac
287 done || fail 3mSkipped
288 quit
289 mod squashfs 2>/dev/null || exit
290 msg aufs
291 br=/mnt/.rw
292 mkdir $br /mnt/.wd
293 got rwdev && mnt $br && br=$br$d
294 o=
295 p=
296 c=/mnt/.cdrom
297 if [ -z "$(ls /mnt/rootfs* 2>/dev/null)" ]; then
298 root=/dev/cdrom/fs
299 got rodev
300 mkdir -p $c /mnt$c /mnt/.rw$c
301 mnt $c
302 o="-o 124"
303 p=/.cdrom/boot
304 c=$c$d
305 fi
306 l=0
307 r=
308 got isofs && r=:$c || for i in /mnt$p/rootfs?*.gz; do
309 fs=${i#*root}
310 r=$r:/mnt/.$fs
311 mkdir -p /mnt/.rw/mnt/.$fs /mnt/.$fs
312 losetup $o /dev/loop$l $i
313 busybox mount -o ro -t squashfs /dev/loop$((l++)) /mnt/.$fs
314 done
315 while read type opt; do
316 mod $type && busybox mount -t $type -o $opt none /mnt && break
317 done <<EOT
318 aufs br=$br$r
319 overlayfs workdir=/mnt/.wd${r/:/,lowerdir=},upperdir=$br
320 EOT
321 quit