-
Notifications
You must be signed in to change notification settings - Fork 7
/
init
587 lines (498 loc) · 14.2 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#!/bin/sh
#
# Simple init script that should handle both
# livecd/livedisk, thinclient and hdd boot
#
PATH=/usr/sbin:/usr/bin:/sbin:/bin
INITRAMFSCONF="/etc/initramfs.conf"
ROOT_LINKS="bin sbin lib boot usr opt"
ROOT_TREES="etc root home var"
TMPFS_DIRS="dev mnt mnt/cdrom mnt/livecd mnt/thin tmp sys proc media"
LOOPBACKFILE="pardus.img"
NORESUME=0
LIVE=0
NFSROOT=0
QUIET=0
RAID_INCREMENTAL=1
RAID=0
LVM=0
COPYTORAM=0
SPLASH=0
WIPEMEM=0
HOTPLUG="/sbin/hotplug"
MNTDIR=""
FS_TYPE=""
INITRAMFS=""
ROOT_FLAGS=""
ROOT_DEVICE=""
ROOT_TARGET=""
RESUME_DEVICE=""
WIPEMEM_OPTS="-llv"
###########################
# Miscellaneous functions #
###########################
info() {
echo "<6>initramfs: $1" > /dev/kmsg
}
log_output() {
$@ | echo "<6>`sed 's#^\(.*\)$#initramfs:\1#g'`" > /dev/kmsg
}
fall2sh() {
# Kill any possible plymouth instances
test -x /bin/plymouth && /bin/plymouth quit &> /dev/null
kill -9 $(pidof plymouthd) &> /dev/null
echo "--> $*"
echo "Reboot with initramfs=shell(noprobe) to further debug the issue."
# Use a login shell for sourcing /etc/profile
/bin/sh -l
}
run_dhcpc() {
udhcpc -C -i eth0 -s /etc/udhcpc.script
}
##################
# Device probers #
##################
probe_devices() {
# Set hotplug helper for firmware loading
echo $HOTPLUG > /proc/sys/kernel/hotplug
if [ "x$1" = "x--with-kms" ]; then
probe_kms
test -x /bin/plymouth && /bin/plymouth show-splash
fi
probe_pci_devices
probe_virtio_devices
probe_usb_devices
# Unset hotplug helper
echo > /proc/sys/kernel/hotplug
[ "${RAID}" -eq "1" ] && probe_raid
[ "${LVM}" -eq "1" ] && probe_lvm
}
probe_kms() {
for device in /sys/bus/pci/devices/*/boot_vga; do
[ -f $device ] || continue
info "Loading KMS driver"
grep -q 1 $device && modprobe -bq `cat ${device%boot_vga}modalias`
done
if [ ! -c /dev/fb0 ]; then
echo "options uvesafb scroll=ywrap mtrr=0 nocrtc=1 mode_option=1024x768-32" > /etc/modprobe.d/uvesafb.conf
modprobe -bq uvesafb
fi
}
probe_pci_devices() {
info "Probing PCI devices"
local MODULES=""
for module in /sys/bus/pci/devices/*/modalias; do
[ -f $module ] || continue
MODULES="$MODULES $(cat $module)"
done
modprobe -bqa $MODULES
}
probe_usb_devices() {
info "Probing USB devices"
local MODULES=""
for module in /sys/bus/usb/devices/*/modalias; do
[ -f $module ] || continue
MODULES="$MODULES $(cat $module)"
done
modprobe -bqa $MODULES
}
probe_raid() {
info "Probing RAID devices"
modprobe -bqa dm-mod raid0 raid1 raid10 raid456
if [ -x /sbin/mdadm ]
then
/sbin/mdadm --examine --scan > /etc/mdadm.conf
/sbin/mdadm -As
fi
}
probe_lvm() {
info "Probing LVM devices"
modprobe -qa dm-mod
if [ -x /sbin/lvm ]
then
/sbin/lvm vgscan --ignorelockingfailure &> /dev/null
/sbin/lvm vgchange -ay --ignorelockingfailure &> /dev/null
fi
}
probe_virtio_devices() {
local MODULES=""
for module in /sys/bus/virtio/devices/*/modalias; do
[ -f $module ] || continue
MODULES="$MODULES $(cat $module)"
done
modprobe -bqa $MODULES
}
#################################
# Filesystem specific functions #
#################################
mount_rootfs() {
FS_TYPE=`disktype $ROOT_DEVICE | grep KERNELMODULE | awk '{print $2}'`
info "Mounting rootfs: $ROOT_DEVICE ($FS_TYPE)"
mount -r -t $FS_TYPE -n ${ROOT_FLAGS} ${ROOT_DEVICE} /newroot
}
find_live_mount() {
if [ "$#" -gt "0" ]
then
for x in $*
do
# this is used for non Linux fs detection
FS_TYPE=`disktype $1 | grep KERNELMODULE | awk '{print $2}'`
if [ -n "$FS_TYPE" -a -f /lib/modules/*/$FS_TYPE.ko ]
then
modprobe $FS_TYPE 1> /dev/null 2>&1
fi
mount -r ${x} /newroot/mnt/cdrom > /dev/null 2>&1
if [ "$?" = "0" ]
then
# Check for cdroot image
if [ -e /newroot/mnt/cdrom/${LOOPBACKFILE} ]
then
ROOT_DEVICE="/newroot${x}"
if [ "$COPYTORAM" == "1" ]
then
info "Copying Live Media files to RAM"
mkdir /newroot/mnt/cdromtemp
cp -af /newroot/mnt/cdrom/* /newroot/mnt/cdromtemp/
umount /newroot/mnt/cdrom
rmdir /newroot/mnt/cdrom
mv /newroot/mnt/cdromtemp /newroot/mnt/cdrom
fi
break
else
umount /newroot/mnt/cdrom
fi
fi
done
fi
}
manage_tmpfs() {
mount -t tmpfs tmpfs /newroot
for d in ${TMPFS_DIRS}; do
mkdir -p "/newroot/${d}"
done
}
mount_nfs() {
FS_LOCATION='mnt/thin'
# Change directory to /newroot
cd /newroot
# FIXME: busybox mount does not load automatically
modprobe -q nfs
# mount nfs
if [ -z "/etc/udhcpc.info" ]
then
fall2sh "/etc/udhcpc.info not found"
fi
. /etc/udhcpc.info
if [ -z "${ROOTPATH}" ]
then
fall2sh "NFS rootpath not found"
fi
echo "Mounting NFS from $ROOTPATH"
mount -o tcp,nolock,ro $ROOTPATH /newroot/mnt/thin
if [ "$?" != '0' ]
then
fall2sh "Could not nfs root"
fi
# Create necessary links
for x in ${ROOT_LINKS}; do
ln -s "${FS_LOCATION}/${x}" "${x}"
done
if [ -e "${FS_LOCATION}/lib32" ]
then
ln -s "${FS_LOCATION}/lib32" "lib32"
fi
# We need this for x86_64
ln -s "${FS_LOCATION}/lib" "lib64"
chmod 1777 tmp
(cd /newroot/${FS_LOCATION}; cp -a ${ROOT_TREES} /newroot)
# Needed for ltspfs mechanism
echo "$IP $HOSTNAME" >> /newroot/etc/hosts
}
mount_cdroot() {
FS_LOCATION="mnt/livecd"
# Change directory to /newroot
cd /newroot
# These are not loaded automatically
modprobe -q squashfs
# Loop type squashfs
mount -t squashfs -o loop,ro /newroot/mnt/cdrom/${LOOPBACKFILE} /newroot/mnt/livecd
if [ "$?" != "0" ]
then
fall2sh "Could not mount root image"
fi
# Create necessary links
for x in ${ROOT_LINKS}; do
ln -s "${FS_LOCATION}/${x}" "${x}"
done
if [ -e "${FS_LOCATION}/lib32" ]
then
ln -s "${FS_LOCATION}/lib32" "lib32"
fi
# We need this for x86_64
ln -s "${FS_LOCATION}/lib" "lib64"
chmod 1777 tmp
(cd /newroot/${FS_LOCATION}; cp -a ${ROOT_TREES} /newroot)
# FIXME: the device list is taken from udev, we can't rely on sys entries since pluggable means different
# in kernel world. Suggestions that do not include this kind of regexp mania are welcome
# for userspace applications
REAL_ROOT_TYPE=`echo "${ROOT_DEVICE}" | sed -e 's/^\/newroot\/dev\///' | grep -qE '^sr[0-9]*|^hd[a-z]|^pcd[0-9]|^xvd*' && echo "optical" || echo "harddisk"`
echo "${REAL_ROOT_TYPE}" > /newroot/var/run/pardus/livemedia
# this is needed for yali
MNTDIR=`grep \/mnt\/cdrom\ /proc/mounts|sed 's/\/newroot//g'`
echo "$MNTDIR" >> /newroot/etc/fstab
}
##############################
# Config and cmdline parsers #
##############################
# FIXME: maybe we should just source the file instead of parsing
# also consider merging conf parser and cmdline parser
parse_config() {
while read inputline;
do
case "${inputline}" in
raid=*)
RAID=$(echo $inputline|cut -f2- -d=)
;;
lvm=*)
LVM=$(echo $inputline|cut -f2- -d=)
;;
thin=*)
NFSROOT=$(echo $inputline|cut -f2- -d=)
;;
root=*)
ROOT_TARGET=$(echo $inputline|cut -f2- -d=)
;;
rootflags=*)
ROOT_FLAGS=$(echo $inputline|cut -f2- -d=)
;;
liveroot=*)
# Installation or livecd, enable RAID by default
# to be able to read existing RAID installations
LIVE=1
RAID_INCREMENTAL=0
LIVEROOT=$(echo $inputline|cut -f2- -d=)
;;
resume=*)
RESUME_DEVICE="${inputline#resume=}"
;;
noresume)
NORESUME=1
;;
copytoram)
COPYTORAM=1
;;
wipemem)
WIPEMEM=1
;;
wipememopts=*)
WIPEMEM=1
WIPEMEM_OPTS=$(echo $inputline|cut -f2- -d=)
;;
splash)
SPLASH=1
;;
init=*)
INIT="${inputline#INIT=}"
;;
esac
done < $INITRAMFSCONF
}
parse_cmdline() {
for x in `cat /proc/cmdline`; do
case "${x}" in
[0123456Ss])
# Normalize 'S' to 's'
LEVEL=`echo ${x}|tr A-Z a-z`
;;
mudur=*)
for m in `echo ${x}|cut -f2 -d=|sed 's/,/ /g'`; do
case "${m}" in
livecd)
LIVE=1
;;
livedisk)
LIVE=1
;;
raid)
RAID=1
;;
lvm)
LVM=1
;;
thin)
NFSROOT=1
;;
esac
done
;;
initramfs=*)
INITRAMFS=`echo ${x}|cut -f2- -d=`
;;
root=*)
ROOT_TARGET=`echo ${x}|cut -f2- -d=`
;;
rootflags=*)
ROOT_FLAGS="-o ${x#rootflags=}"
;;
liveroot=*)
LIVE=1
LIVEROOT=$(echo ${x}|cut -f2- -d=)
;;
resume=*)
RESUME_DEVICE="${x#resume=}"
;;
noresume)
NORESUME=1
;;
init=*)
INIT="${x#init=}"
;;
copytoram)
COPYTORAM=1
;;
wipemem)
WIPEMEM=1
;;
wipememopts=*)
WIPEMEM=1
WIPEMEM_OPTS=$(echo $x|cut -f2- -d=)
;;
splash)
SPLASH=1
;;
single)
LEVEL="s"
;;
quiet)
QUIET=1
;;
blacklist=*)
modules=${x#blacklist=}
for module in ${modules//,/ }; do
echo "blacklist $module" >> /etc/modprobe.d/cmdline.conf
done
;;
esac
done
if [ -f /etc/modprobe.d/cmdline.conf ]; then
cp /etc/modprobe.d/cmdline.conf /dev/.modprobe.initramfs.conf
fi
}
####################
# init starts here #
####################
info "Starting init on initramfs"
# Mount needed filesystems
mount -n -t proc proc /proc
mount -n -t sysfs sysfs /sys
# Prepare /dev (Needs kernel >= 2.6.32)
mount -t devtmpfs devtmpfs /dev
mkdir -m 0755 /dev/pts
mount -t devpts -o gid=5,mode=620 devpts /dev/pts
# First parse config file, then cmdline to allow overwriting internal config
if [ -f "$INITRAMFSCONF" ]
then
#. $INITRAMFSCONF
parse_config
fi
# Parse command line parameters
parse_cmdline
# Minimize printk log
test "x$QUIET" = "x1" && echo "1" > /proc/sys/kernel/printk
# Initialize plymouth daemon if found and splash is true
# Don't even launch plymouthd if we're in single-user mode
if [ "$LEVEL" != "s" -a "$SPLASH" = "1" -a -x /sbin/plymouthd ]; then
/sbin/plymouthd --attach-to-session
fi
# Handle initramfs= parameter
if [ "${INITRAMFS}" == "shellnoprobe" ]
then
fall2sh "Starting up a shell without probing"
elif [ "${INITRAMFS}" == "shell" ]
then
probe_devices --with-kms
fall2sh "Starting up a shell"
fi
if [ "${WIPEMEM}" = "1" ]
then
info "Wiping out memory, system will be shutdown after completion"
sdmem ${WIPEMEM_OPTS}
poweroff -f
fi
# Probe devices
probe_devices --with-kms
if [ -x /bin/resume -a -b "$RESUME_DEVICE" -a "x$NORESUME" != "x1" ]
then
if [ "x$SPLASH" == "x1" ]
then
SPLASHPARAM="-P splash=y"
else
SPLASHPARAM="-P splash=n"
fi
# FIXME: This will fail if resume= contains LABEL/UUID
info "Attempting to resume from hibernation"
/bin/resume $SPLASHPARAM $RESUME_DEVICE
fi
echo 0x0100 > /proc/sys/kernel/real-root-dev
if [ "${LIVE}" -eq "1" ]
then
ROOT_DEVICE=""
manage_tmpfs
# modprobe filesystems that are not in kernel, for live disks
modprobe -qa nls_cp857 nls_utf8 vfat
for i in `seq 50`
do
t=`findfs ${LIVEROOT} 2>/dev/null`
find_live_mount "$t"
if [ "${ROOT_DEVICE}" != "" ]
then
break
else
probe_devices
usleep 200000
fi
done
if [ "${ROOT_DEVICE}" == "" ]
then
fall2sh "Could not find mount media"
fi
mount_cdroot
elif [ "${NFSROOT}" -eq "1" ]
then
run_dhcpc
manage_tmpfs
mount_nfs
# set hostname for mudur
hostname $HOSTNAME
else
# Wait until ROOT_DEVICE appears
for i in `seq 50`
do
# let findfs handle all conversion
ROOT_DEVICE=`findfs ${ROOT_TARGET} 2>/dev/null`
if [ ! -b "${ROOT_DEVICE}" ]
then
probe_devices
usleep 200000
else
break
fi
done
if [ ! -b "${ROOT_DEVICE}" ]
then
fall2sh "Could not find boot device"
else
mount_rootfs
fi
fi
[ "${INIT}" == "" ] && INIT="/sbin/init";
# This stops /lib/udev/rules.d/65-md-incremental.rules from medling with mdraid sets.
[ "${RAID_INCREMENTAL}" -eq "0" ] && touch /dev/.in_sysinit
# Move mounts instead of umount/mount
mount --move /dev /newroot/dev
mount --move /proc /newroot/proc
mount --move /sys /newroot/sys
# And we start
info "Switching to the real root"
test -x /bin/plymouth && /bin/plymouth update-root-fs --new-root-dir=/newroot
exec /bin/switch_root -c /dev/console /newroot ${INIT} ${LEVEL}