-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcreate-initrd
executable file
·226 lines (195 loc) · 4.83 KB
/
create-initrd
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
#!/bin/bash
#
# Copyright (c) 2015 SUSE Linux GmbH
#
# This file is part of the kernel-ci project and licensed under the GNU GPLv2
#
DEFPROGRAMS=(sh cp rm mkdir cat chroot echo insmod modprobe mount sleep)
#MODULES=('efivarfs.ko' 'mcb.ko' 'mcb-pci.ko')
DEFROOT="/dev/vda3"
O=$(pwd)
#
# $1 program
copy_libs_for_prog()
{
local prog libs
prog=$1
libs=$(ldd ${prog} | cut -d '>' -f 2 | cut -d ' ' -f 2 | grep ^/)
for l in ${libs}; do
cp $l lib64
done
}
usage()
{
echo "Usage: $1 [-f '<file[ <file>][...]'] [-i init] [-k kdir ] [-m '<module[ <module>][...]'] [-o <outdir>] [-r root]" >&2
echo "" >&2
echo " -f Space separated list of files to include in initrd" >&2
echo " image." >&2
echo " -i bin/init source." >&2
echo " -k Directory having the kernel sources." >&2
echo " -m Space separated list of kernel modules to include" >&2
echo " in initrd image." >&2
echo " -p Space separated list of programs to include in" >&2
echo " initrd image." >&2
echo " -o <outdir> generate everything into <outdir>" >&2
echo " -r root= kernel option (default $DEFROOT)." >&2
exit 1
}
program=$(basename $0)
while getopts "f:i:hk:m:p:o:r:?" flag; do
case ${flag} in
f)
FILES=${OPTARG}
;;
i)
INIT_SRC=${OPTARG}
;;
k)
KDIR=${OPTARG}
;;
m)
MODULES=(${OPTARG})
;;
p)
PROGRAMS=(${OPTARG})
;;
o)
O=${OPTARG}
;;
r)
ROOT=${OPTARG}
;;
[?h])
usage ${program}
exit 1
;;
*)
echo "invalid option"
usage ${program}
exit 1
;;
esac
done
if [ x"$ROOT" == "x" ]; then
ROOT=$DEFROOT
fi
if [ x"$PROGRAMS" == "x" ]; then
PROGRAMS=("${DEFPROGRAMS[@]}")
fi
# set to CWD in case we're called in the kernel build dir
if [ x"$KDIR" == "x" ]; then
KDIR=$(pwd)
fi
krel=$(make -s -C "$KDIR" kernelrelease)
O=$(cd "$O"; pwd)
TMPDIR=$(mktemp -qd)
mkdir -p $TMPDIR/initrd/{bin,dev,etc,lib/modules,lib64,proc,sys,sysroot,root}
pushd $TMPDIR/initrd >/dev/null
ln -s bin sbin
if [ x"$INIT_SRC" == "x" ]; then
echo "#!/bin/sh" >> bin/init
for mod in "${MODULES[@]}"; do
echo "echo \"Loading ${mod}\"" >> bin/init
echo "modprobe ${mod%.ko}" >> bin/init
done
cat >> bin/init << __EOF__
echo "Mounting /proc, /dev and /sys"
mount -t proc /proc /proc
mount -t sysfs /sys /sys
mount -t devtmpfs /dev /dev
# Mount rootfs to /sysroot
mount $ROOT /sysroot
echo "Copying kernel modules to new root"
mkdir /sysroot/lib/modules/$krel
cp /lib/modules/$krel/* /sysroot/lib/modules/$krel/
# Disable the JeOS wizard
rm -f /sysroot/usr/lib/systemd/system/sysinit.target.wants/jeos-firstboot.service
echo "Switching to new root"
cd /sysroot
exec chroot . sh -c 'exec /sbin/init'
__EOF__
else
cp ${INIT_SRC} bin/init
fi
chmod 755 bin/init
if [ -x /usr/bin/busybox-static ]; then
cp /usr/bin/busybox-static bin/busybox
for prog in "${PROGRAMS[@]}"; do
ln -s busybox "bin/$prog"
done
else
LD_2=`rpm -ql glibc | grep ld-2`
LD_LINUX=`rpm -ql glibc | grep ld-linux`
for lib in ${LD_2} ${LD_LINUX}; do
cp ${lib} lib64/
done
for prog in ${PROGRAMS[@]}; do
absprog=$(which ${prog})
if [ -z ${absprog} ]; then
if [ ! -f /sbin/${prog} ]; then
echo "${prog} not found, skipping.."
else
absprog=/sbin/${prog}
fi
continue
fi
cp ${absprog} bin/
copy_libs_for_prog ${absprog}
done
fi
if [ x"$KDIR" != "x" ]; then
KDIR=$(realpath $KDIR)
mkdir -p "lib/modules/$krel"
cp "$KDIR/modules.order" "$KDIR/modules.builtin" "lib/modules/$krel" 2>/dev/null
set -- "${MODULES[@]}"
while test $# -gt 0; do
# This is not a noop: it replaces [-_] with a literal [-_] to
# use with find(1), because module names use - and _
# interchangeably
mod=${1//[-_]/[-_]}
shift
if find "lib/modules/$krel" -name "$mod" | grep -q .; then
continue
fi
file=$(find $KDIR -name "$mod.ko")
if test -z "$file"; then
echo "warning: module $mod not found"
continue
fi
for f in "${file[@]}"; do
cp "$f" "lib/modules/$krel"
done
for dep in $(/sbin/modinfo -F depends "$file" | sed 's/,/ /g'); do
set -- "$@" "$dep"
done
done
/sbin/depmod -F "$KDIR/System.map" -e -b "$PWD" "$krel"
fi
if [ "x$FILES" != "x" ]; then
for file in ${FILES}; do
cp ${file} root/
done
fi
find ./ | cpio --quiet -H newc -o > $O/initrd.cpio
popd >/dev/null
gzip -9 -c $O/initrd.cpio > $O/initrd.img
rm -rf $O/initrd.cpio
if [[ ! -d /lib/firmware/amd-ucode && ! -d /lib/firmware/intel-ucode ]]; then
rm -rf $TMPDIR
exit 0
fi
# microcode
UCODE_DIR=$TMPDIR/kernel/x86/microcode
mkdir -p $UCODE_DIR
if [ -d /lib/firmware/amd-ucode ]; then
cat /lib/firmware/amd-ucode/microcode_amd*.bin > $UCODE_DIR/AuthenticAMD.bin
fi
if [ -d /lib/firmware/intel-ucode ]; then
cat /lib/firmware/intel-ucode/* > $UCODE_DIR/GenuineIntel.bin
fi
cd $TMPDIR
find . | cpio --quiet -H newc -o > $O/microcode.cpio
cat $O/microcode.cpio $O/initrd.img > $O/initrd.microcode.img
mv -f $O/initrd.microcode.img $O/initrd.img
rm -rf $O/microcode.cpio
rm -rf $TMPDIR