-
Notifications
You must be signed in to change notification settings - Fork 1
/
init
executable file
·350 lines (314 loc) · 8.68 KB
/
init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/bin/sh
### cached kernel parameters :
#
# modprobe=
# list of modules to load, example modprobe=r8169,btrfs
#
### Frugal mode or live CD : using a root ram disk.
#
# tmpram= [RAMDISK]
# minimum % of free RAM to build the root tmpfs, default 100
# a lower value may work, such as 50
# rootfssize= [RAMDISK]
# size of / regarding the RAM size, default 90%
# rootfsinodes= [RAMDISK]
# files count of / regarding the RAM size
#
### Indirect mode ; an initramfs is used to mount root from the hard disk.
### Any rootfs should fit, minimum required is slitaz-base.
#
# mount= [MOUNT]
# device with root loop file or sub directory
# posixovl [MOUNT]
# use posix attributes on a non posix filesystem (VFAT, NTFS...)
# loopfs= [MOUNT]
# loop file for root filesystem
# subroot= [MOUNT]
# sub directory for root filesystem
# bindfs= [MOUNT] (deprecated)
# bind out of root tree part
#
# extra= [MOUNT]
# extra initrd files list to load
#
### Special disk mode ; needs extra modules/softwares from preinit flavor.
#
# dmraid= [RAID]
# root semi hardware raid device name in /dev/mapper
# softraid= [RAID]
# root software device name in /dev/mapper
# lvmroot= [LVM]
# root logical volume name in /dev/mapper
# cryptoroot= [CRYPTO]
# encrypted root device using luks or loop-aes
#
### Special mode used by loram flavor with aufs/overlayfs & squashfs
### incremental/nested rootfs (russian dolls).
#
# rodev= [LORAM][MERGE]
# device (+ optional path) for the read only aufs branch
# rwdev= [LORAM][MERGE]
# (persistent) device for the r/w aufs branch instead of the ramfs
# isofs [LORAM]
# do not use squashfs filesystem images, but the cdrom filesystem only
fail() {
echo -e '\033[70G[ \033[1;3'${1:-1mFailed}'\033[0;39m ]'
}
quit() {
[ -d /mnt$4/etc ] || return
grep -q serial /proc/ioports && ! grep -q ttyS /mnt$4/etc/inittab &&
echo "ttyS0::respawn:/sbin/getty 115200,38400,9600,4800,2400 ttyS0" \
>> /mnt$4/etc/inittab
[ -n "$4" ] || grep -q 'tmpfs /mnt tmpfs' /proc/mounts ||
busybox mount /mnt -o remount,ro
busybox mount --move /run /mnt/${1:-run}
cat > /run/init <<EOT
$2
x=/sbin/switch_root
[ -x \$x ] && exec \$x mnt $3 /sbin/init
EOT
fail 2mDone
exit
}
arg() {
grep -q $1 /proc/cmdline
}
got() {
arg $1= && root="$(sed "s/.*$1=\\([^ ]*\\).*/\\1/" </proc/cmdline)"
}
msg() {
echo "Switching / to $1..."
}
use() {
got $1 && msg "$1 $root"
}
bbmnt() {
r=$(busybox blkid 2> /dev/null | grep "$root[:\"]" | sed 's|:.*||;q')
r=${r:-$(busybox blkid $root 2> /dev/null | sed 's|:.*||;q')}
r=${r:-$root}
if [ ! -b $r ]; then
r=${r#/dev/}
r=/dev/${r%%/*}
fi
d=${root#*$r}
t=$(busybox blkid $r 2> /dev/null | sed '/ TYPE=/!d;s/.* TYPE="//;s/".*//')
busybox mount $r $1 ${t:+-t $t}
}
mnt() {
bbmnt $1 && return
w=$(cat /sys/module/usb_storage/parameters/delay_use)
w=$((1+${w:-2}))
echo "Sleep $w seconds..."
sleep $w
bbmnt $1
}
mod() {
for i in $@; do
grep -q "^$i\\W" /proc/modules && continue
grep -q "\\W$i\$" /proc/filesystems && continue
echo "Loading module: $i"
modprobe $i 2>/dev/null ||
for j in $(find /lib/modules /mnt/.fs*/lib/modules|sed "/\/$i.ko/!d" 2> /dev/null); do
busybox insmod $j && break
done
done
}
try() {
if [ ! -d /mnt/etc ] && got cryptoroot; then
mod dm-mod dm-crypt aes-256
d=${root#/dev/}
l=crypto-$d
if cryptsetup isLuks $root 2>/dev/null; then
cryptsetup luksOpen $root $l
else
read -st 60 -p "Pass phrase : " p
k=$(echo $p|hashalot -x -n 32 sha512)
echo 0 $(cat $(find /sys/block|grep /$d/size))\
crypt aes-plain $k 0 $root 0|dmsetup create $l
fi
busybox mount /dev/mapper/$l /mnt
fi
got subroot && return
got loopfs && return
if [ -d /mnt/etc ]; then
for i in $@; do
cp -a $i /mnt$(dirname $i)
done
quit
fi
fail
}
lvm() {
use lvmroot || return
mod dm-mod
vgscan --ignorelockingfailure
vgchange -ay --ignorelockingfailure
busybox mount /dev/mapper/$root /mnt
try /dev/mapper $1
}
ldraid() {
while read l; do
case "$l" in
*raid10*) mod raid10;;
*raid0*) mod raid0;;
*raid1*) mod raid1;;
*raid*) mod raid456;;
*mirror*) mod dm-mirror;;
esac
done
}
mntiso() {
umnt=
if got mount; then
dev=$root
x=$(busybox blkid|grep $dev|sed 's/:.*//;q')
root=${x:-$dev}
[ "$dev" = "$root" ] || dev="$root ($dev)"
echo "Mount $dev..."
mnt $1
umnt=" $umnt $1"
arg posixovl && echo "And posixovl..." && umnt=" $umnt $1" &&
mount.posixovl -F $1 -- -oallow_other -odefault_permissions -osuid
fi
got loopfs && echo "Into file $root..." && umnt=" $umnt $1" &&
losetup /dev/loop0 $1/$root && root=/dev/loop0 && mnt $1
[ -z "$umnt" ] && mnt $1
got bindfs && echo "Bind ${root/,/ to }..." &&
busybox mount --bind $1/${root%,*} $1/${root/,//}
true
}
if [ "$1" != 'log' ]; then
busybox mount -t proc proc /proc
busybox mount -t sysfs sys /sys
busybox mount -t tmpfs tmpfs /run
x=/sbin/init; echo "[ -x $x ] && exec $x" >/run/init
$0 log 2>&1 | tee /run/boot.log
busybox umount /sys
busybox umount /proc
. /run/init
sh
fi
mdev -s 2> /dev/null ||
for i in /sys/block/*/dev /sys/block/*/*/dev ; do
[ -s "$i" ] || continue
n=${i%/dev}
n=/dev/${n##*/}
[ -e $n ] && continue
echo "Create $n "
mknod $n b $(sed 's/:/ /' < $i)
done
got modprobe && mod ${root//,/ }
[ -d /proc/sys/dev/cdrom ] &&
ln -s $(sed '/name/!d;s/.*:[^a-z]*//' /proc/sys/dev/cdrom/info) /dev/cdrom
dev=$(busybox fdisk -l|awk '/sectors/{d=$2}/SliTaz persistent \/home/{print d $1;quit}'|sed 's|:||')
if got root && [ "$dev" -a "$root" == "/dev/null" ]; then
busybox blkid $dev | grep -q TYPE= || mkfs.vfat $dev
mount $dev /home && case $(busybox blkid $dev) in
*vfat*|*exfat*|*ntfs*)
mount.posixovl -F /home -- -oallow_other -odefault_permissions -osuid
esac
fs=/home/boot/rootfs.gz; [ -s $fs ] && (zcat $fs || unlzma < $fs || cat $fs) | cpio -idmu
[ -d /init.d ] && for i in /init.d/* ; do [ -x $i ] && . $i ; done
fi 2>&1 >/dev/null
if use dmraid; then
dmraid -s | sed '/^type/!ds/.*: *//' | ldraid
[ ${root:0:4} = /dev ] ||
root=/dev/mapper/$(dmraid -s|sed '/^name/!ds/.*: *//')p${root#p}
dmraid -ay
fi
use raiddev && raiddev="DEVICE ${root//,/ }"
use raidmail && raidmail="MAILADDR $root"
if use softraid; then
for i in 1 2 3 4 5 6 7 8 9; do
mdadm -E -s -c partitions > /etc/mdadm.conf
grep -qs " $root " /etc/mdadm.conf && break
sleep $i
done
[ "$raiddev" ] && echo "$raiddev" >> /etc/mdadm.conf
[ "$raidmail" ] && echo "$raidmail" >> /etc/mdadm.conf
grep level=raid /etc/mdadm.conf | ldraid
udevd --daemon
udevadm trigger
udevadm settle
for i in 1 2 3 4 5 6 7 8 9; do
sleep $i
mdadm -A -s
for j in $(sed '/\/md/!d;s|.*md/*\([0-9]*\).*|md\1|' \
/etc/mdadm.conf); do
grep -qs "$j : act" /proc/mdstat || continue 2
done
break
done
grep -qs "^$(basename $root) : act" /proc/mdstat ||
root=$(awk '/^md/ { print "/dev/" $1; exit }' < /proc/mdstat)
lvm /etc/mdadm.conf
fi
lvm
got extra && extra=$root && mntiso /mnt && for i in ${extra//,/ }; do
grep -q ' lm ' /proc/cpuinfo && [ -s /mnt/${i}64 ] && i=${i}64
(unlzma < /mnt/$i 2>/dev/null || zcat /mnt/$i 2>/dev/null || cat /mnt/$i) | cpio -idmu
done && for i in $umnt; do umount -l $i; done
arg cryptoroot= && try
if use subroot; then
cp $(LD_TRACE_LOADED_OBJECTS=1 /lib/ld*.so /usr/sbin/chroot | sed\
's|.*=> \(/lib/l[^ ]*\).*|\1|;/^\//!d') /usr/sbin/chroot /run
r=$root/run
quit $r "export LD_LIBRARY_PATH=$r:/lib"\
"$root$(ls /run/ld-*so) $r/chroot $root" "/$root"
fi
quit
msg tmpfs
root=100
got tmpram
r=$root
got rootfsinodes && inodes=",nr_inodes=$root"
root=90%
got rootfssize
[ $(busybox free|busybox awk '/Mem:/{print int(($4*100)/$3)}') -ge $r ] &&
busybox mount -t tmpfs -o size=$root$inodes tmpfs /mnt &&
export PATH=/mnt/bin:$PATH &&
for i in $(ls -a /); do
case "$i" in
.*|cdrom) ;;
mnt|proc|run|sys) mkdir /mnt/$i;;
dev) cp -a /$i /mnt;;
*) mv /$i /mnt; ln -s /mnt/$i / 2> /dev/null ||
LD_LIBRARY_PATH=/mnt/lib:/mnt/usr/lib:/lib:/usr/lib \
/mnt/lib/ld-linux.so.2 /mnt/bin/ln -s /mnt/$i /;;
esac
done || fail 3mSkipped
quit
mod squashfs 2>/dev/null || exit
msg aufs
br=/mnt/.rw
mkdir $br /mnt/.wd
got rwdev && mnt $br && br=$br$d
o=
p=
c=/mnt/.cdrom
if [ -z "$(ls /mnt/rootfs* 2>/dev/null)" ]; then
root=/dev/cdrom/fs
got rodev
mkdir -p $c /mnt$c /mnt/.rw$c
mntiso $c
o="-o 124"
p=/.cdrom/boot
c=$c$d
fi
l=0
r=
got isofs && r=:$c || for i in /mnt$p/rootfs?*.gz; do
fs=${i#*root}
r=$r:/mnt/.$fs
mkdir -p /mnt/.rw/mnt/.$fs /mnt/.$fs
losetup $o /dev/loop$((++l)) $i
busybox mount -o ro -t squashfs /dev/loop$l /mnt/.$fs
done
while read type opt; do
mod $type || continue
busybox mount -t $type -o $opt none /mnt && break
done <<EOT
aufs br=$br$r
overlayfs workdir=/mnt/.wd${r/:/,lowerdir=},upperdir=$br
EOT
quit