summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLans Zhang <jia.zhang@windriver.com>2017-07-04 17:22:12 +0800
committerLans Zhang <jia.zhang@windriver.com>2017-07-04 17:22:12 +0800
commit6ace7c99baf1b8ab5eb12793331cb3f4ffe695cf (patch)
treeab45aa69710009a1b722d7a3cd6e6234d9cc5a95
parenta9e266c4810d46b0f844b326c74541982fe921f1 (diff)
downloadmeta-secure-core-6ace7c99baf1b8ab5eb12793331cb3f4ffe695cf.tar.gz
init: clean up
Signed-off-by: Lans Zhang <jia.zhang@windriver.com>
-rw-r--r--meta/recipes-core/initrdscripts/files/init120
1 files changed, 40 insertions, 80 deletions
diff --git a/meta/recipes-core/initrdscripts/files/init b/meta/recipes-core/initrdscripts/files/init
index ff7531f..4c5155a 100644
--- a/meta/recipes-core/initrdscripts/files/init
+++ b/meta/recipes-core/initrdscripts/files/init
@@ -1,89 +1,52 @@
1#!/bin/sh 1#!/bin/sh
2 2
3PATH="/sbin:/bin:/usr/sbin:/usr/bin" 3PATH="/sbin:/bin:/usr/sbin:/usr/bin"
4ROOTFS_MOUNT="/rootfs"
5INIT="/sbin/init"
6
7mkdir /proc /sys /run
8mount -t proc none /proc
9mount -t sysfs none /sys
10mount -t devtmpfs none /dev
11mount -t tmpfs none /run
12
13cmdline="`cat /proc/cmdline`"
14for arg in $cmdline; do
15 optarg=`expr "x$arg" : 'x[^=]*=\(.*\)'`
16
17 case "$arg" in
18 root=*)
19 ROOTFS_DEV="$optarg"
20 ;;
21 init=*)
22 INIT="$optarg"
23 ;;
24 esac
25done
4 26
5ROOT_MOUNT="/rootfs" 27[ -z "$ROOTFS_DEV" ] && {
6MOUNT="/bin/mount" 28 echo "exiting on missing the kernel parameter root= ..."
7UMOUNT="/bin/umount" 29 exit 1
8
9# Copied from initramfs-framework. The core of this script probably should be
10# turned into initramfs-framework modules to reduce duplication.
11udev_daemon() {
12 PROGS="/sbin/udev/udevd /sbin/udevd /lib/udev/udevd /lib/systemd/systemd-udevd"
13
14 for o in $PROGS; do
15 if [ -x "$o" ]; then
16 echo "$o"
17 return 0
18 fi
19 done
20
21 return 1
22}
23
24_UDEV_DAEMON="`udev_daemon`"
25
26early_setup() {
27 mount -t proc proc /proc
28 mount -t sysfs sysfs /sys
29 mount -t devtmpfs none /dev
30 mount -t tmpfs none /run
31
32 $_UDEV_DAEMON --daemon
33 udevadm trigger --action=add
34}
35
36read_args() {
37 [ -z "$CMDLINE" ] && CMDLINE="`cat /proc/cmdline`"
38 for arg in $CMDLINE; do
39 optarg=`expr "x$arg" : 'x[^=]*=\(.*\)'`
40 case "$arg" in
41 root=*)
42 ROOT_DEVICE="$optarg"
43 ;;
44 rootdelay=*)
45 ROOT_DELAY="$optarg"
46 ;;
47 init=*)
48 INIT="$optarg"
49 ;;
50 esac
51 done
52}
53
54fatal() {
55 echo $1 >$CONSOLE
56 echo >$CONSOLE
57 exec sh
58} 30}
59 31
60early_setup 32mkdir -p "$ROOTFS_MOUNT"
61
62read_args
63
64[ -z "$CONSOLE" ] && CONSOLE="/dev/console"
65[ -z "$INIT" ] && INIT="/sbin/init"
66
67udevadm settle --timeout=3
68killall "${_UDEV_DAEMON##*/}" 2>/dev/null
69
70mkdir -p "$ROOT_MOUNT"
71 33
72try_to_mount_rootfs() { 34try_to_mount_rootfs() {
73 local mount_flags="rw,noatime,iversion" 35 local mount_flags="rw,noatime,iversion"
74 36
75 mount -o "$mount_flags" "$ROOT_DEVICE" "$ROOT_MOUNT" \ 37 mount -o "$mount_flags" "$ROOTFS_DEV" "$ROOTFS_MOUNT" \
76 2>/dev/null && return 0 38 2>/dev/null && return 0
77 39
78 [ -x /init.cryptfs ] && 40 [ -x /init.cryptfs ] &&
79 /init.cryptfs "$ROOT_MOUNT" "$ROOT_DEVICE" \ 41 /init.cryptfs "$ROOTFS_MOUNT" "$ROOTFS_DEV" \
80 "$mount_flags" OVERCROOTFS && return 0 42 "$mount_flags" OVERCROOTFS && return 0
81 43
82 return 1 44 return 1
83} 45}
84 46
85echo "Waiting for root device to be ready..." 47echo "Waiting for root device to be ready ..."
86while [ 1 ] ; do 48
49while [ 1 ]; do
87 try_to_mount_rootfs && break 50 try_to_mount_rootfs && break
88 sleep 0.1 51 sleep 0.1
89done 52done
@@ -91,24 +54,18 @@ done
91# Move the mount points of some filesystems over to 54# Move the mount points of some filesystems over to
92# the corresponding directories under the real root filesystem. 55# the corresponding directories under the real root filesystem.
93for dir in `cat /proc/mounts | grep -v rootfs | awk '{ print $2 }'` ; do 56for dir in `cat /proc/mounts | grep -v rootfs | awk '{ print $2 }'` ; do
94 mkdir -p $ROOT_MOUNT/${dir##*/} 57 mkdir -p "$ROOTFS_MOUNT"/${dir##*/}
95 mount -nv --move "$dir" $ROOT_MOUNT/${dir##*/} 58 mount -nv --move "$dir" "$ROOTFS_MOUNT"/${dir##*/}
96done 59done
97 60
98cd "$ROOT_MOUNT" 61cd "$ROOTFS_MOUNT"
99
100# If we pass args to bash, it will assume they are text files
101# to source and run.
102if [ "$INIT" == "/bin/bash" ] || [ "$INIT" == "/bin/sh" ]; then
103 CMDLINE=""
104fi
105 62
106# !!! The Big Fat Warnings !!! 63# !!! The Big Fat Warnings !!!
107# 64#
108# The IMA policy may enforce appraising the executable and verifying the 65# The IMA policy may enforce appraising the executable and verifying the
109# signature stored in xattr. However, ramfs doesn't support xattr, and all 66# signature stored in xattr. However, ramfs doesn't support xattr, and all
110# other initializations must *NOT* be placed after IMA initialization! 67# other initializations must *NOT* be placed after IMA initialization!
111[ -x /init.ima ] && /init.ima "$ROOT_MOUNT" && { 68[ -x /init.ima ] && /init.ima "$ROOTFS_MOUNT" && {
112 # switch_root is an exception. We call it in the real rootfs and it 69 # switch_root is an exception. We call it in the real rootfs and it
113 # should be already signed properly. 70 # should be already signed properly.
114 switch_root="usr/sbin/switch_root.static" 71 switch_root="usr/sbin/switch_root.static"
@@ -116,5 +73,8 @@ fi
116 switch_root="switch_root" 73 switch_root="switch_root"
117} 74}
118 75
119exec "$switch_root" "$ROOT_MOUNT" "$INIT" "$CMDLINE" || 76exec "$switch_root" "$ROOTFS_MOUNT" "$INIT" || {
120 fatal "Couldn't switch_root, dropping to shell" 77 "$ROOTFS_MOUNT/bin/echo.coreutils" "Couldn't switch to the real rootfs"
78 # Cause kernel panic.
79 exit 2
80}