]>
Commit | Line | Data |
---|---|---|
898720b7 HH |
1 | #!/bin/bash |
2 | # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*- | |
3 | # ex: ts=8 sw=4 sts=4 et filetype=sh | |
4 | PATH=/sbin:/bin:/usr/sbin:/usr/bin | |
5 | export PATH | |
6 | ||
0fe15dc8 | 7 | LOOKS_LIKE_DEBIAN=$(source /etc/os-release && [[ "$ID" = "debian" || "$ID_LIKE" = "debian" ]] && echo yes) |
0d6e798a HH |
8 | KERNEL_VER=${KERNEL_VER-$(uname -r)} |
9 | KERNEL_MODS="/lib/modules/$KERNEL_VER/" | |
91f9f8f1 | 10 | QEMU_TIMEOUT="${QEMU_TIMEOUT:-infinity}" |
43bbb8f0 | 11 | NSPAWN_TIMEOUT="${NSPAWN_TIMEOUT:-infinity}" |
b2ecd099 | 12 | TIMED_OUT= # will be 1 after run_* if *_TIMEOUT is set and test timed out |
91f9f8f1 | 13 | FSTYPE="${FSTYPE:-ext3}" |
856ca72b | 14 | UNIFIED_CGROUP_HIERARCHY="${UNIFIED_CGROUP_HIERARCHY:-no}" |
898720b7 | 15 | |
3486cb6c MP |
16 | if ! ROOTLIBDIR=$(pkg-config --variable=systemdutildir systemd); then |
17 | echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2 | |
18 | ROOTLIBDIR=/usr/lib/systemd | |
19 | fi | |
20 | ||
09f6f45a EV |
21 | BASICTOOLS="sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe sed cmp tee rm" |
22 | DEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort hostname find" | |
889a9042 | 23 | |
c6a77179 RC |
24 | function find_qemu_bin() { |
25 | # SUSE and Red Hat call the binary qemu-kvm | |
26 | # Debian and Gentoo call it kvm | |
27 | [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a kvm qemu-kvm 2>/dev/null | grep '^/' -m1) | |
28 | ||
29 | [ "$ARCH" ] || ARCH=$(uname -m) | |
30 | case $ARCH in | |
31 | x86_64) | |
32 | # QEMU's own build system calls it qemu-system-x86_64 | |
33 | [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-x86_64 2>/dev/null | grep '^/' -m1) | |
34 | ;; | |
35 | i*86) | |
36 | # new i386 version of QEMU | |
37 | [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-i386 2>/dev/null | grep '^/' -m1) | |
38 | ||
39 | # i386 version of QEMU | |
40 | [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu 2>/dev/null | grep '^/' -m1) | |
41 | ;; | |
42 | esac | |
43 | ||
44 | if [ ! -e "$QEMU_BIN" ]; then | |
45 | echo "Could not find a suitable QEMU binary" >&2 | |
46 | return 1 | |
47 | fi | |
48 | } | |
49 | ||
b2ecd099 MP |
50 | # Return 0 if QEMU did run (then you must check the result state/logs for actual |
51 | # success), or 1 if QEMU is not available. | |
889a9042 | 52 | run_qemu() { |
b6f0c419 HH |
53 | if [ -f /etc/machine-id ]; then |
54 | read MACHINE_ID < /etc/machine-id | |
55 | [ -z "$INITRD" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/initrd" ] \ | |
56 | && INITRD="/boot/$MACHINE_ID/$KERNEL_VER/initrd" | |
57 | [ -z "$KERNEL_BIN" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/linux" ] \ | |
58 | && KERNEL_BIN="/boot/$MACHINE_ID/$KERNEL_VER/linux" | |
59 | fi | |
60 | ||
61fea35e EV |
61 | default_fedora_initrd=/boot/initramfs-${KERNEL_VER}.img |
62 | default_debian_initrd=/boot/initrd.img-${KERNEL_VER} | |
c6a77179 | 63 | [ "$KERNEL_BIN" ] || KERNEL_BIN=/boot/vmlinuz-$KERNEL_VER |
61fea35e EV |
64 | [ "$INITRD" ] || { [ -e "$default_fedora_initrd" ] && INITRD=$default_fedora_initrd; } |
65 | [ "$INITRD" ] || { [ "$LOOKS_LIKE_DEBIAN" ] && [ -e "$default_debian_initrd" ] && INITRD=$default_debian_initrd; } | |
c6a77179 RC |
66 | [ "$QEMU_SMP" ] || QEMU_SMP=1 |
67 | ||
68 | find_qemu_bin || return 1 | |
69 | ||
70 | KERNEL_APPEND="root=/dev/sda1 \ | |
c6a77179 RC |
71 | raid=noautodetect \ |
72 | loglevel=2 \ | |
3486cb6c | 73 | init=$ROOTLIBDIR/systemd \ |
c6a77179 RC |
74 | ro \ |
75 | console=ttyS0 \ | |
76 | selinux=0 \ | |
856ca72b | 77 | systemd.unified_cgroup_hierarchy=$UNIFIED_CGROUP_HIERARCHY \ |
c6a77179 RC |
78 | $KERNEL_APPEND \ |
79 | " | |
80 | ||
dbf43a42 | 81 | QEMU_OPTIONS="-smp $QEMU_SMP \ |
c6a77179 RC |
82 | -net none \ |
83 | -m 512M \ | |
84 | -nographic \ | |
85 | -kernel $KERNEL_BIN \ | |
5b23cef0 | 86 | -drive format=raw,cache=unsafe,file=${TESTDIR}/rootdisk.img \ |
c6a77179 RC |
87 | " |
88 | ||
91f9f8f1 | 89 | if [[ "$INITRD" && "$SKIP_INITRD" != "yes" ]]; then |
c6a77179 RC |
90 | QEMU_OPTIONS="$QEMU_OPTIONS -initrd $INITRD" |
91 | fi | |
92 | ||
dbf43a42 DM |
93 | if [ -c /dev/kvm ]; then |
94 | QEMU_OPTIONS="$QEMU_OPTIONS -machine accel=kvm -enable-kvm -cpu host" | |
95 | fi | |
96 | ||
91f9f8f1 EV |
97 | if [[ "$QEMU_TIMEOUT" != "infinity" ]]; then |
98 | QEMU_BIN="timeout --foreground $QEMU_TIMEOUT $QEMU_BIN" | |
99 | fi | |
b2ecd099 MP |
100 | (set -x; $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND") |
101 | rc=$? | |
102 | if [ "$rc" = 124 ] && [ "$QEMU_TIMEOUT" != "infinity" ]; then | |
103 | derror "test timed out after $QEMU_TIMEOUT s" | |
104 | TIMED_OUT=1 | |
105 | else | |
106 | [ "$rc" != 0 ] && derror "QEMU failed with exit code $rc" | |
107 | fi | |
108 | return 0 | |
889a9042 RC |
109 | } |
110 | ||
7cad32bb MP |
111 | # Return 0 if nspawn did run (then you must check the result state/logs for actual |
112 | # success), or 1 if nspawn is not available. | |
889a9042 | 113 | run_nspawn() { |
7cad32bb MP |
114 | [[ -d /run/systemd/system ]] || return 1 |
115 | ||
43bbb8f0 EV |
116 | local _nspawn_cmd="../../systemd-nspawn --register=no --kill-signal=SIGKILL --directory=$TESTDIR/nspawn-root $ROOTLIBDIR/systemd $KERNEL_APPEND" |
117 | if [[ "$NSPAWN_TIMEOUT" != "infinity" ]]; then | |
118 | _nspawn_cmd="timeout --foreground $NSPAWN_TIMEOUT $_nspawn_cmd" | |
119 | fi | |
856ca72b EV |
120 | |
121 | _nspawn_cmd="env UNIFIED_CGROUP_HIERARCHY=$UNIFIED_CGROUP_HIERARCHY $_nspawn_cmd" | |
122 | ||
b2ecd099 MP |
123 | (set -x; $_nspawn_cmd) |
124 | rc=$? | |
125 | if [ "$rc" = 124 ] && [ "$NSPAWN_TIMEOUT" != "infinity" ]; then | |
126 | derror "test timed out after $NSPAWN_TIMEOUT s" | |
127 | TIMED_OUT=1 | |
128 | else | |
129 | [ "$rc" != 0 ] && derror "nspawn failed with exit code $rc" | |
130 | fi | |
131 | return 0 | |
889a9042 RC |
132 | } |
133 | ||
134 | setup_basic_environment() { | |
135 | # create the basic filesystem layout | |
136 | setup_basic_dirs | |
137 | ||
138 | install_systemd | |
139 | install_missing_libraries | |
140 | install_config_files | |
141 | create_rc_local | |
142 | install_basic_tools | |
143 | install_libnss | |
144 | install_pam | |
145 | install_dbus | |
146 | install_fonts | |
147 | install_keymaps | |
148 | install_terminfo | |
149 | install_execs | |
9974ff63 | 150 | install_fsck |
889a9042 RC |
151 | install_plymouth |
152 | install_debug_tools | |
153 | install_ld_so_conf | |
e3ce42e7 | 154 | setup_selinux |
889a9042 RC |
155 | strip_binaries |
156 | install_depmod_files | |
157 | generate_module_dependencies | |
889a9042 RC |
158 | } |
159 | ||
e3ce42e7 EV |
160 | setup_selinux() { |
161 | # don't forget KERNEL_APPEND='... selinux=1 ...' | |
162 | if [[ "$SETUP_SELINUX" != "yes" ]]; then | |
163 | ddebug "Don't setup SELinux" | |
164 | return 0 | |
165 | fi | |
166 | ddebug "Setup SELinux" | |
167 | local _conf_dir=/etc/selinux | |
168 | local _fixfiles_tools="bash uname cat sort uniq awk grep egrep head expr find rm secon setfiles" | |
169 | ||
170 | rm -rf $initdir/$_conf_dir | |
171 | if ! cp -ar $_conf_dir $initdir/$_conf_dir; then | |
172 | dfatal "Failed to copy $_conf_dir" | |
173 | exit 1 | |
174 | fi | |
175 | ||
176 | cat <<EOF >$initdir/etc/systemd/system/autorelabel.service | |
177 | [Unit] | |
178 | Description=Relabel all filesystems | |
179 | DefaultDependencies=no | |
180 | Requires=local-fs.target | |
181 | Conflicts=shutdown.target | |
182 | After=local-fs.target | |
183 | Before=sysinit.target shutdown.target | |
184 | ConditionSecurity=selinux | |
185 | ConditionPathExists=|/.autorelabel | |
186 | ||
187 | [Service] | |
188 | ExecStart=/bin/sh -x -c 'echo 0 >/sys/fs/selinux/enforce && fixfiles -f -F relabel && rm /.autorelabel && systemctl --force reboot' | |
189 | Type=oneshot | |
190 | TimeoutSec=0 | |
191 | RemainAfterExit=yes | |
192 | EOF | |
193 | ||
194 | touch $initdir/.autorelabel | |
195 | mkdir -p $initdir/etc/systemd/system/basic.target.wants | |
196 | ln -fs autorelabel.service $initdir/etc/systemd/system/basic.target.wants/autorelabel.service | |
197 | ||
198 | dracut_install $_fixfiles_tools | |
199 | dracut_install fixfiles | |
200 | dracut_install sestatus | |
201 | } | |
202 | ||
a2fbff31 EV |
203 | install_valgrind() { |
204 | if ! type -p valgrind; then | |
205 | dfatal "Failed to install valgrind" | |
206 | exit 1 | |
207 | fi | |
208 | ||
209 | local _valgrind_bins=$(strace -e execve valgrind /bin/true 2>&1 >/dev/null | perl -lne 'print $1 if /^execve\("([^"]+)"/') | |
210 | dracut_install $_valgrind_bins | |
211 | ||
212 | local _valgrind_libs=$(LD_DEBUG=files valgrind /bin/true 2>&1 >/dev/null | perl -lne 'print $1 if m{calling init: (/.*vgpreload_.*)}') | |
213 | dracut_install $_valgrind_libs | |
214 | ||
215 | local _valgrind_dbg_and_supp=$( | |
216 | strace -e open valgrind /bin/true 2>&1 >/dev/null | | |
217 | perl -lne 'if (my ($fname) = /^open\("([^"]+).*= (?!-)\d+/) { print $fname if $fname =~ /debug|\.supp$/ }' | |
218 | ) | |
219 | dracut_install $_valgrind_dbg_and_supp | |
220 | } | |
221 | ||
cb2f9d3f EV |
222 | create_valgrind_wrapper() { |
223 | local _valgrind_wrapper=$initdir/$ROOTLIBDIR/systemd-under-valgrind | |
224 | ddebug "Create $_valgrind_wrapper" | |
225 | cat >$_valgrind_wrapper <<EOF | |
226 | #!/bin/bash | |
227 | ||
228 | exec valgrind --leak-check=full --log-file=/valgrind.out $ROOTLIBDIR/systemd "\$@" | |
229 | EOF | |
230 | chmod 0755 $_valgrind_wrapper | |
231 | } | |
232 | ||
45dbd7b6 EV |
233 | create_strace_wrapper() { |
234 | local _strace_wrapper=$initdir/$ROOTLIBDIR/systemd-under-strace | |
235 | ddebug "Create $_strace_wrapper" | |
236 | cat >$_strace_wrapper <<EOF | |
237 | #!/bin/bash | |
238 | ||
239 | exec strace -D -o /strace.out $ROOTLIBDIR/systemd "\$@" | |
240 | EOF | |
241 | chmod 0755 $_strace_wrapper | |
242 | } | |
243 | ||
9974ff63 EV |
244 | install_fsck() { |
245 | dracut_install /sbin/fsck* | |
246 | dracut_install -o /bin/fsck* | |
331fb4ca EV |
247 | |
248 | # fskc.reiserfs calls reiserfsck. so, install it | |
249 | dracut_install -o reiserfsck | |
9974ff63 EV |
250 | } |
251 | ||
889a9042 RC |
252 | install_dmevent() { |
253 | instmods dm_crypt =crypto | |
254 | type -P dmeventd >/dev/null && dracut_install dmeventd | |
255 | inst_libdir_file "libdevmapper-event.so*" | |
ac289ce3 EV |
256 | if [[ "$LOOKS_LIKE_DEBIAN" ]]; then |
257 | # dmsetup installs 55-dm and 60-persistent-storage-dm on Debian/Ubuntu | |
258 | # see https://anonscm.debian.org/cgit/pkg-lvm/lvm2.git/tree/debian/patches/0007-udev.patch | |
259 | inst_rules 55-dm.rules 60-persistent-storage-dm.rules | |
260 | else | |
261 | inst_rules 10-dm.rules 13-dm-disk.rules 95-dm-notify.rules | |
262 | fi | |
889a9042 RC |
263 | } |
264 | ||
265 | install_systemd() { | |
266 | # install compiled files | |
8a8332f7 | 267 | (cd $TEST_BASE_DIR/..; set -x; make DESTDIR=$initdir install) |
889a9042 | 268 | # remove unneeded documentation |
23756070 | 269 | rm -fr $initdir/usr/share/{man,doc} |
889a9042 RC |
270 | # we strip binaries since debug symbols increase binaries size a lot |
271 | # and it could fill the available space | |
272 | strip_binaries | |
61b480b6 ZJS |
273 | |
274 | # enable debug logging in PID1 | |
275 | echo LogLevel=debug >> $initdir/etc/systemd/system.conf | |
889a9042 RC |
276 | } |
277 | ||
278 | install_missing_libraries() { | |
279 | # install possible missing libraries | |
280 | for i in $initdir/{sbin,bin}/* $initdir/lib/systemd/*; do | |
281 | inst_libs $i | |
282 | done | |
283 | } | |
284 | ||
285 | create_empty_image() { | |
739d81dd | 286 | rm -f "$TESTDIR/rootdisk.img" |
889a9042 | 287 | # Create the blank file to use as a root filesystem |
1b1eae69 | 288 | dd if=/dev/null of="$TESTDIR/rootdisk.img" bs=1M seek=400 |
889a9042 | 289 | LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img) |
739d81dd | 290 | [ -b "$LOOPDEV" ] || return 1 |
889a9042 | 291 | echo "LOOPDEV=$LOOPDEV" >> $STATEFILE |
edbced8a | 292 | sfdisk "$LOOPDEV" <<EOF |
1b1eae69 | 293 | ,390M |
889a9042 RC |
294 | , |
295 | EOF | |
296 | ||
331fb4ca EV |
297 | local _label="-L systemd" |
298 | # mkfs.reiserfs doesn't know -L. so, use --label instead | |
299 | [[ "$FSTYPE" == "reiserfs" ]] && _label="--label systemd" | |
300 | if ! mkfs -t "${FSTYPE}" ${_label} "${LOOPDEV}p1" -q; then | |
301 | dfatal "Failed to mkfs -t ${FSTYPE}" | |
302 | exit 1 | |
303 | fi | |
889a9042 RC |
304 | } |
305 | ||
306 | check_result_nspawn() { | |
307 | ret=1 | |
308 | [[ -e $TESTDIR/nspawn-root/testok ]] && ret=0 | |
309 | [[ -f $TESTDIR/nspawn-root/failed ]] && cp -a $TESTDIR/nspawn-root/failed $TESTDIR | |
310 | cp -a $TESTDIR/nspawn-root/var/log/journal $TESTDIR | |
311 | [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed | |
312 | ls -l $TESTDIR/journal/*/*.journal | |
313 | test -s $TESTDIR/failed && ret=$(($ret+1)) | |
b2ecd099 | 314 | [ -n "$TIMED_OUT" ] && ret=$(($ret+1)) |
889a9042 RC |
315 | return $ret |
316 | } | |
317 | ||
318 | strip_binaries() { | |
5a613464 EV |
319 | if [[ "$STRIP_BINARIES" = "no" ]]; then |
320 | ddebug "Don't strip binaries" | |
321 | return 0 | |
322 | fi | |
889a9042 RC |
323 | ddebug "Strip binaries" |
324 | find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug | |
325 | } | |
326 | ||
327 | create_rc_local() { | |
328 | mkdir -p $initdir/etc/rc.d | |
329 | cat >$initdir/etc/rc.d/rc.local <<EOF | |
330 | #!/bin/bash | |
331 | exit 0 | |
332 | EOF | |
333 | chmod 0755 $initdir/etc/rc.d/rc.local | |
334 | } | |
335 | ||
336 | install_execs() { | |
c7eda013 EV |
337 | ddebug "install any Execs from the service files" |
338 | ( | |
339 | export PKG_CONFIG_PATH=$TEST_BASE_DIR/../src/core/ | |
340 | systemdsystemunitdir=$(pkg-config --variable=systemdsystemunitdir systemd) | |
341 | systemduserunitdir=$(pkg-config --variable=systemduserunitdir systemd) | |
342 | egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/{$systemdsystemunitdir,$systemduserunitdir}/*.service \ | |
343 | | while read i; do | |
344 | i=${i##Exec*=}; i=${i##-} | |
345 | inst $i | |
346 | done | |
347 | ) | |
889a9042 RC |
348 | } |
349 | ||
350 | generate_module_dependencies() { | |
351 | if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \ | |
352 | ! depmod -a -b "$initdir" $KERNEL_VER; then | |
353 | dfatal "\"depmod -a $KERNEL_VER\" failed." | |
354 | exit 1 | |
355 | fi | |
356 | } | |
357 | ||
358 | install_depmod_files() { | |
359 | inst /lib/modules/$KERNEL_VER/modules.order | |
360 | inst /lib/modules/$KERNEL_VER/modules.builtin | |
361 | } | |
362 | ||
363 | install_plymouth() { | |
364 | # install plymouth, if found... else remove plymouth service files | |
365 | # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then | |
366 | # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \ | |
367 | # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir | |
368 | # dracut_install plymouth plymouthd | |
369 | # else | |
370 | rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth* | |
371 | # fi | |
372 | } | |
373 | ||
374 | install_ld_so_conf() { | |
375 | cp -a /etc/ld.so.conf* $initdir/etc | |
376 | ldconfig -r "$initdir" | |
377 | } | |
378 | ||
379 | install_config_files() { | |
380 | inst /etc/sysconfig/init | |
381 | inst /etc/passwd | |
382 | inst /etc/shadow | |
bf3a947c | 383 | inst /etc/login.defs |
889a9042 RC |
384 | inst /etc/group |
385 | inst /etc/shells | |
386 | inst /etc/nsswitch.conf | |
387 | inst /etc/pam.conf | |
388 | inst /etc/securetty | |
389 | inst /etc/os-release | |
390 | inst /etc/localtime | |
391 | # we want an empty environment | |
392 | > $initdir/etc/environment | |
393 | > $initdir/etc/machine-id | |
394 | # set the hostname | |
395 | echo systemd-testsuite > $initdir/etc/hostname | |
396 | # fstab | |
397 | cat >$initdir/etc/fstab <<EOF | |
331fb4ca | 398 | LABEL=systemd / ${FSTYPE} rw 0 1 |
889a9042 RC |
399 | EOF |
400 | } | |
401 | ||
402 | install_basic_tools() { | |
403 | [[ $BASICTOOLS ]] && dracut_install $BASICTOOLS | |
4be4833e | 404 | dracut_install -o sushell |
7d023341 MP |
405 | # in Debian ldconfig is just a shell script wrapper around ldconfig.real |
406 | dracut_install -o ldconfig.real | |
889a9042 RC |
407 | } |
408 | ||
409 | install_debug_tools() { | |
410 | [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS | |
411 | } | |
412 | ||
413 | install_libnss() { | |
414 | # install libnss_files for login | |
cffae62b | 415 | NSS_LIBS=$(LD_DEBUG=files getent passwd 2>&1 >/dev/null |sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}') |
99877b7e | 416 | dracut_install $NSS_LIBS |
889a9042 RC |
417 | } |
418 | ||
419 | install_dbus() { | |
3486cb6c MP |
420 | inst $ROOTLIBDIR/system/dbus.socket |
421 | inst $ROOTLIBDIR/system/dbus.service | |
889a9042 RC |
422 | |
423 | find \ | |
e63b61be | 424 | /etc/dbus-1 /usr/share/dbus-1 -xtype f \ |
889a9042 RC |
425 | | while read file; do |
426 | inst $file | |
427 | done | |
428 | } | |
429 | ||
430 | install_pam() { | |
0fe15dc8 EV |
431 | ( |
432 | [[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture &>/dev/null && find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f | |
889a9042 RC |
433 | find \ |
434 | /etc/pam.d \ | |
435 | /etc/security \ | |
436 | /lib64/security \ | |
437 | /lib/security -xtype f \ | |
0fe15dc8 | 438 | ) | while read file; do |
889a9042 RC |
439 | inst $file |
440 | done | |
417491f1 | 441 | |
d5172c79 EV |
442 | # pam_unix depends on unix_chkpwd. |
443 | # see http://www.linux-pam.org/Linux-PAM-html/sag-pam_unix.html | |
444 | dracut_install -o unix_chkpwd | |
445 | ||
417491f1 EV |
446 | [[ "$LOOKS_LIKE_DEBIAN" ]] && |
447 | cp /etc/pam.d/systemd-user $initdir/etc/pam.d/ | |
e14b866b ZJS |
448 | |
449 | # set empty root password for easy debugging | |
450 | sed -i 's/^root:x:/root::/' $initdir/etc/passwd | |
889a9042 RC |
451 | } |
452 | ||
453 | install_keymaps() { | |
454 | for i in \ | |
455 | /usr/lib/kbd/keymaps/include/* \ | |
456 | /usr/lib/kbd/keymaps/i386/include/* \ | |
457 | /usr/lib/kbd/keymaps/i386/qwerty/us.*; do | |
458 | [[ -f $i ]] || continue | |
459 | inst $i | |
460 | done | |
461 | } | |
462 | ||
463 | install_fonts() { | |
464 | for i in \ | |
25b47f96 | 465 | /usr/lib/kbd/consolefonts/eurlatgr* \ |
889a9042 RC |
466 | /usr/lib/kbd/consolefonts/latarcyrheb-sun16*; do |
467 | [[ -f $i ]] || continue | |
468 | inst $i | |
469 | done | |
470 | } | |
471 | ||
472 | install_terminfo() { | |
473 | for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do | |
474 | [ -f ${_terminfodir}/l/linux ] && break | |
475 | done | |
476 | dracut_install -o ${_terminfodir}/l/linux | |
477 | } | |
478 | ||
479 | setup_testsuite() { | |
53d90f95 | 480 | cp $TEST_BASE_DIR/testsuite.target $initdir/etc/systemd/system/ |
5c404f1a | 481 | cp $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/ |
889a9042 RC |
482 | |
483 | mkdir -p $initdir/etc/systemd/system/testsuite.target.wants | |
484 | ln -fs $TEST_BASE_DIR/testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service | |
485 | ln -fs $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service | |
486 | ||
487 | # make the testsuite the default target | |
488 | ln -fs testsuite.target $initdir/etc/systemd/system/default.target | |
489 | } | |
490 | ||
491 | setup_nspawn_root() { | |
492 | rm -fr $TESTDIR/nspawn-root | |
493 | ddebug "cp -ar $initdir $TESTDIR/nspawn-root" | |
494 | cp -ar $initdir $TESTDIR/nspawn-root | |
495 | # we don't mount in the nspawn root | |
496 | rm -f $TESTDIR/nspawn-root/etc/fstab | |
497 | } | |
498 | ||
0d6e798a | 499 | setup_basic_dirs() { |
889a9042 RC |
500 | mkdir -p $initdir/run |
501 | mkdir -p $initdir/etc/systemd/system | |
502 | mkdir -p $initdir/var/log/journal | |
503 | ||
0d6e798a | 504 | for d in usr/bin usr/sbin bin etc lib "$libdir" sbin tmp usr var var/log dev proc sys sysroot root run run/lock run/initramfs; do |
898720b7 HH |
505 | if [ -L "/$d" ]; then |
506 | inst_symlink "/$d" | |
507 | else | |
0d6e798a | 508 | inst_dir "/$d" |
898720b7 HH |
509 | fi |
510 | done | |
511 | ||
512 | ln -sfn /run "$initdir/var/run" | |
513 | ln -sfn /run/lock "$initdir/var/lock" | |
514 | } | |
515 | ||
516 | inst_libs() { | |
517 | local _bin=$1 | |
518 | local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)' | |
519 | local _file _line | |
520 | ||
521 | LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do | |
522 | [[ $_line = 'not a dynamic executable' ]] && break | |
523 | ||
524 | if [[ $_line =~ $_so_regex ]]; then | |
525 | _file=${BASH_REMATCH[1]} | |
526 | [[ -e ${initdir}/$_file ]] && continue | |
527 | inst_library "$_file" | |
528 | continue | |
529 | fi | |
530 | ||
531 | if [[ $_line =~ not\ found ]]; then | |
532 | dfatal "Missing a shared library required by $_bin." | |
533 | dfatal "Run \"ldd $_bin\" to find out what it is." | |
534 | dfatal "$_line" | |
535 | dfatal "dracut cannot create an initrd." | |
536 | exit 1 | |
537 | fi | |
538 | done | |
539 | } | |
540 | ||
541 | import_testdir() { | |
542 | STATEFILE=".testdir" | |
543 | [[ -e $STATEFILE ]] && . $STATEFILE | |
544 | if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then | |
545 | TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX) | |
546 | echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE | |
547 | export TESTDIR | |
548 | fi | |
549 | } | |
550 | ||
889a9042 RC |
551 | import_initdir() { |
552 | initdir=$TESTDIR/root | |
553 | export initdir | |
554 | } | |
555 | ||
898720b7 HH |
556 | ## @brief Converts numeric logging level to the first letter of level name. |
557 | # | |
558 | # @param lvl Numeric logging level in range from 1 to 6. | |
559 | # @retval 1 if @a lvl is out of range. | |
560 | # @retval 0 if @a lvl is correct. | |
561 | # @result Echoes first letter of level name. | |
562 | _lvl2char() { | |
563 | case "$1" in | |
564 | 1) echo F;; | |
565 | 2) echo E;; | |
566 | 3) echo W;; | |
567 | 4) echo I;; | |
568 | 5) echo D;; | |
569 | 6) echo T;; | |
570 | *) return 1;; | |
571 | esac | |
572 | } | |
573 | ||
574 | ## @brief Internal helper function for _do_dlog() | |
575 | # | |
576 | # @param lvl Numeric logging level. | |
577 | # @param msg Message. | |
578 | # @retval 0 It's always returned, even if logging failed. | |
579 | # | |
580 | # @note This function is not supposed to be called manually. Please use | |
581 | # dtrace(), ddebug(), or others instead which wrap this one. | |
582 | # | |
583 | # This function calls _do_dlog() either with parameter msg, or if | |
584 | # none is given, it will read standard input and will use every line as | |
585 | # a message. | |
586 | # | |
587 | # This enables: | |
588 | # dwarn "This is a warning" | |
589 | # echo "This is a warning" | dwarn | |
590 | LOG_LEVEL=4 | |
591 | ||
592 | dlog() { | |
593 | [ -z "$LOG_LEVEL" ] && return 0 | |
594 | [ $1 -le $LOG_LEVEL ] || return 0 | |
595 | local lvl="$1"; shift | |
596 | local lvlc=$(_lvl2char "$lvl") || return 0 | |
597 | ||
598 | if [ $# -ge 1 ]; then | |
599 | echo "$lvlc: $*" | |
600 | else | |
601 | while read line; do | |
602 | echo "$lvlc: " "$line" | |
603 | done | |
604 | fi | |
605 | } | |
606 | ||
607 | ## @brief Logs message at TRACE level (6) | |
608 | # | |
609 | # @param msg Message. | |
610 | # @retval 0 It's always returned, even if logging failed. | |
611 | dtrace() { | |
612 | set +x | |
613 | dlog 6 "$@" | |
614 | [ -n "$debug" ] && set -x || : | |
615 | } | |
616 | ||
617 | ## @brief Logs message at DEBUG level (5) | |
618 | # | |
619 | # @param msg Message. | |
620 | # @retval 0 It's always returned, even if logging failed. | |
621 | ddebug() { | |
0d6e798a | 622 | # set +x |
898720b7 | 623 | dlog 5 "$@" |
0d6e798a | 624 | # [ -n "$debug" ] && set -x || : |
898720b7 HH |
625 | } |
626 | ||
627 | ## @brief Logs message at INFO level (4) | |
628 | # | |
629 | # @param msg Message. | |
630 | # @retval 0 It's always returned, even if logging failed. | |
631 | dinfo() { | |
632 | set +x | |
633 | dlog 4 "$@" | |
634 | [ -n "$debug" ] && set -x || : | |
635 | } | |
636 | ||
637 | ## @brief Logs message at WARN level (3) | |
638 | # | |
639 | # @param msg Message. | |
640 | # @retval 0 It's always returned, even if logging failed. | |
641 | dwarn() { | |
642 | set +x | |
643 | dlog 3 "$@" | |
644 | [ -n "$debug" ] && set -x || : | |
645 | } | |
646 | ||
647 | ## @brief Logs message at ERROR level (2) | |
648 | # | |
649 | # @param msg Message. | |
650 | # @retval 0 It's always returned, even if logging failed. | |
651 | derror() { | |
0d6e798a | 652 | # set +x |
898720b7 | 653 | dlog 2 "$@" |
0d6e798a | 654 | # [ -n "$debug" ] && set -x || : |
898720b7 HH |
655 | } |
656 | ||
657 | ## @brief Logs message at FATAL level (1) | |
658 | # | |
659 | # @param msg Message. | |
660 | # @retval 0 It's always returned, even if logging failed. | |
661 | dfatal() { | |
662 | set +x | |
663 | dlog 1 "$@" | |
664 | [ -n "$debug" ] && set -x || : | |
665 | } | |
666 | ||
667 | ||
668 | # Generic substring function. If $2 is in $1, return 0. | |
669 | strstr() { [ "${1#*$2*}" != "$1" ]; } | |
670 | ||
671 | # normalize_path <path> | |
672 | # Prints the normalized path, where it removes any duplicated | |
673 | # and trailing slashes. | |
674 | # Example: | |
675 | # $ normalize_path ///test/test// | |
676 | # /test/test | |
677 | normalize_path() { | |
678 | shopt -q -s extglob | |
679 | set -- "${1//+(\/)//}" | |
680 | shopt -q -u extglob | |
681 | echo "${1%/}" | |
682 | } | |
683 | ||
684 | # convert_abs_rel <from> <to> | |
685 | # Prints the relative path, when creating a symlink to <to> from <from>. | |
686 | # Example: | |
687 | # $ convert_abs_rel /usr/bin/test /bin/test-2 | |
688 | # ../../bin/test-2 | |
689 | # $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test | |
690 | convert_abs_rel() { | |
691 | local __current __absolute __abssize __cursize __newpath | |
692 | local -i __i __level | |
693 | ||
694 | set -- "$(normalize_path "$1")" "$(normalize_path "$2")" | |
695 | ||
696 | # corner case #1 - self looping link | |
697 | [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; } | |
698 | ||
699 | # corner case #2 - own dir link | |
700 | [[ "${1%/*}" == "$2" ]] && { echo "."; return; } | |
701 | ||
702 | IFS="/" __current=($1) | |
703 | IFS="/" __absolute=($2) | |
704 | ||
705 | __abssize=${#__absolute[@]} | |
706 | __cursize=${#__current[@]} | |
707 | ||
708 | while [[ ${__absolute[__level]} == ${__current[__level]} ]] | |
709 | do | |
710 | (( __level++ )) | |
711 | if (( __level > __abssize || __level > __cursize )) | |
712 | then | |
713 | break | |
714 | fi | |
715 | done | |
716 | ||
717 | for ((__i = __level; __i < __cursize-1; __i++)) | |
718 | do | |
719 | if ((__i > __level)) | |
720 | then | |
721 | __newpath=$__newpath"/" | |
722 | fi | |
723 | __newpath=$__newpath".." | |
724 | done | |
725 | ||
726 | for ((__i = __level; __i < __abssize; __i++)) | |
727 | do | |
728 | if [[ -n $__newpath ]] | |
729 | then | |
730 | __newpath=$__newpath"/" | |
731 | fi | |
732 | __newpath=$__newpath${__absolute[__i]} | |
733 | done | |
734 | ||
735 | echo "$__newpath" | |
736 | } | |
737 | ||
738 | ||
739 | # Install a directory, keeping symlinks as on the original system. | |
740 | # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file" | |
741 | # will create ${initdir}/lib64, ${initdir}/lib64/file, | |
742 | # and a symlink ${initdir}/lib -> lib64. | |
743 | inst_dir() { | |
744 | [[ -e ${initdir}/"$1" ]] && return 0 # already there | |
745 | ||
746 | local _dir="$1" _part="${1%/*}" _file | |
747 | while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do | |
748 | _dir="$_part $_dir" | |
749 | _part=${_part%/*} | |
750 | done | |
751 | ||
752 | # iterate over parent directories | |
753 | for _file in $_dir; do | |
754 | [[ -e "${initdir}/$_file" ]] && continue | |
755 | if [[ -L $_file ]]; then | |
756 | inst_symlink "$_file" | |
757 | else | |
758 | # create directory | |
759 | mkdir -m 0755 -p "${initdir}/$_file" || return 1 | |
760 | [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file" | |
761 | chmod u+w "${initdir}/$_file" | |
762 | fi | |
763 | done | |
764 | } | |
765 | ||
766 | # $1 = file to copy to ramdisk | |
767 | # $2 (optional) Name for the file on the ramdisk | |
768 | # Location of the image dir is assumed to be $initdir | |
769 | # We never overwrite the target if it exists. | |
770 | inst_simple() { | |
771 | [[ -f "$1" ]] || return 1 | |
772 | strstr "$1" "/" || return 1 | |
773 | ||
774 | local _src=$1 target="${2:-$1}" | |
775 | if ! [[ -d ${initdir}/$target ]]; then | |
776 | [[ -e ${initdir}/$target ]] && return 0 | |
777 | [[ -L ${initdir}/$target ]] && return 0 | |
778 | [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}" | |
779 | fi | |
780 | # install checksum files also | |
781 | if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then | |
782 | inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac" | |
783 | fi | |
784 | ddebug "Installing $_src" | |
785 | cp --sparse=always -pfL "$_src" "${initdir}/$target" | |
786 | } | |
787 | ||
788 | # find symlinks linked to given library file | |
789 | # $1 = library file | |
790 | # Function searches for symlinks by stripping version numbers appended to | |
791 | # library filename, checks if it points to the same target and finally | |
792 | # prints the list of symlinks to stdout. | |
793 | # | |
794 | # Example: | |
795 | # rev_lib_symlinks libfoo.so.8.1 | |
796 | # output: libfoo.so.8 libfoo.so | |
797 | # (Only if libfoo.so.8 and libfoo.so exists on host system.) | |
798 | rev_lib_symlinks() { | |
799 | [[ ! $1 ]] && return 0 | |
800 | ||
801 | local fn="$1" orig="$(readlink -f "$1")" links='' | |
802 | ||
803 | [[ ${fn} =~ .*\.so\..* ]] || return 1 | |
804 | ||
805 | until [[ ${fn##*.} == so ]]; do | |
806 | fn="${fn%.*}" | |
807 | [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}" | |
808 | done | |
809 | ||
810 | echo "${links}" | |
811 | } | |
812 | ||
813 | # Same as above, but specialized to handle dynamic libraries. | |
814 | # It handles making symlinks according to how the original library | |
815 | # is referenced. | |
816 | inst_library() { | |
817 | local _src="$1" _dest=${2:-$1} _lib _reallib _symlink | |
818 | strstr "$1" "/" || return 1 | |
819 | [[ -e $initdir/$_dest ]] && return 0 | |
820 | if [[ -L $_src ]]; then | |
821 | # install checksum files also | |
822 | if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then | |
823 | inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac" | |
824 | fi | |
825 | _reallib=$(readlink -f "$_src") | |
826 | inst_simple "$_reallib" "$_reallib" | |
827 | inst_dir "${_dest%/*}" | |
828 | [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/} | |
829 | ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}" | |
830 | else | |
831 | inst_simple "$_src" "$_dest" | |
832 | fi | |
833 | ||
834 | # Create additional symlinks. See rev_symlinks description. | |
835 | for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do | |
836 | [[ ! -e $initdir/$_symlink ]] && { | |
837 | ddebug "Creating extra symlink: $_symlink" | |
838 | inst_symlink $_symlink | |
839 | } | |
840 | done | |
841 | } | |
842 | ||
843 | # find a binary. If we were not passed the full path directly, | |
844 | # search in the usual places to find the binary. | |
845 | find_binary() { | |
846 | if [[ -z ${1##/*} ]]; then | |
847 | if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then | |
848 | echo $1 | |
849 | return 0 | |
850 | fi | |
851 | fi | |
852 | ||
853 | type -P $1 | |
854 | } | |
855 | ||
856 | # Same as above, but specialized to install binary executables. | |
857 | # Install binary executable, and all shared library dependencies, if any. | |
858 | inst_binary() { | |
859 | local _bin _target | |
860 | _bin=$(find_binary "$1") || return 1 | |
861 | _target=${2:-$_bin} | |
862 | [[ -e $initdir/$_target ]] && return 0 | |
863 | [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0 | |
864 | local _file _line | |
865 | local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)' | |
866 | # I love bash! | |
867 | LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do | |
868 | [[ $_line = 'not a dynamic executable' ]] && break | |
869 | ||
870 | if [[ $_line =~ $_so_regex ]]; then | |
871 | _file=${BASH_REMATCH[1]} | |
872 | [[ -e ${initdir}/$_file ]] && continue | |
873 | inst_library "$_file" | |
874 | continue | |
875 | fi | |
876 | ||
877 | if [[ $_line =~ not\ found ]]; then | |
878 | dfatal "Missing a shared library required by $_bin." | |
879 | dfatal "Run \"ldd $_bin\" to find out what it is." | |
880 | dfatal "$_line" | |
881 | dfatal "dracut cannot create an initrd." | |
882 | exit 1 | |
883 | fi | |
884 | done | |
885 | inst_simple "$_bin" "$_target" | |
886 | } | |
887 | ||
888 | # same as above, except for shell scripts. | |
889 | # If your shell script does not start with shebang, it is not a shell script. | |
890 | inst_script() { | |
891 | local _bin | |
892 | _bin=$(find_binary "$1") || return 1 | |
893 | shift | |
894 | local _line _shebang_regex | |
895 | read -r -n 80 _line <"$_bin" | |
896 | # If debug is set, clean unprintable chars to prevent messing up the term | |
897 | [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]') | |
898 | _shebang_regex='(#! *)(/[^ ]+).*' | |
899 | [[ $_line =~ $_shebang_regex ]] || return 1 | |
900 | inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@" | |
901 | } | |
902 | ||
903 | # same as above, but specialized for symlinks | |
904 | inst_symlink() { | |
905 | local _src=$1 _target=${2:-$1} _realsrc | |
906 | strstr "$1" "/" || return 1 | |
907 | [[ -L $1 ]] || return 1 | |
908 | [[ -L $initdir/$_target ]] && return 0 | |
909 | _realsrc=$(readlink -f "$_src") | |
910 | if ! [[ -e $initdir/$_realsrc ]]; then | |
911 | if [[ -d $_realsrc ]]; then | |
912 | inst_dir "$_realsrc" | |
913 | else | |
914 | inst "$_realsrc" | |
915 | fi | |
916 | fi | |
917 | [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}" | |
918 | [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/} | |
919 | ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target" | |
920 | } | |
921 | ||
922 | # attempt to install any programs specified in a udev rule | |
923 | inst_rule_programs() { | |
924 | local _prog _bin | |
925 | ||
926 | if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then | |
927 | for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do | |
928 | if [ -x /lib/udev/$_prog ]; then | |
929 | _bin=/lib/udev/$_prog | |
930 | else | |
931 | _bin=$(find_binary "$_prog") || { | |
932 | dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found" | |
933 | continue; | |
934 | } | |
935 | fi | |
936 | ||
937 | #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)" | |
938 | dracut_install "$_bin" | |
939 | done | |
940 | fi | |
941 | } | |
942 | ||
943 | # udev rules always get installed in the same place, so | |
944 | # create a function to install them to make life simpler. | |
945 | inst_rules() { | |
946 | local _target=/etc/udev/rules.d _rule _found | |
947 | ||
948 | inst_dir "/lib/udev/rules.d" | |
949 | inst_dir "$_target" | |
950 | for _rule in "$@"; do | |
951 | if [ "${rule#/}" = "$rule" ]; then | |
952 | for r in /lib/udev/rules.d /etc/udev/rules.d; do | |
953 | if [[ -f $r/$_rule ]]; then | |
954 | _found="$r/$_rule" | |
955 | inst_simple "$_found" | |
956 | inst_rule_programs "$_found" | |
957 | fi | |
958 | done | |
959 | fi | |
960 | for r in '' ./ $dracutbasedir/rules.d/; do | |
961 | if [[ -f ${r}$_rule ]]; then | |
962 | _found="${r}$_rule" | |
963 | inst_simple "$_found" "$_target/${_found##*/}" | |
964 | inst_rule_programs "$_found" | |
965 | fi | |
966 | done | |
967 | [[ $_found ]] || dinfo "Skipping udev rule: $_rule" | |
968 | done | |
969 | } | |
970 | ||
971 | # general purpose installation function | |
972 | # Same args as above. | |
973 | inst() { | |
974 | local _x | |
975 | ||
976 | case $# in | |
977 | 1) ;; | |
978 | 2) [[ ! $initdir && -d $2 ]] && export initdir=$2 | |
979 | [[ $initdir = $2 ]] && set $1;; | |
980 | 3) [[ -z $initdir ]] && export initdir=$2 | |
981 | set $1 $3;; | |
982 | *) dfatal "inst only takes 1 or 2 or 3 arguments" | |
983 | exit 1;; | |
984 | esac | |
985 | for _x in inst_symlink inst_script inst_binary inst_simple; do | |
986 | $_x "$@" && return 0 | |
987 | done | |
988 | return 1 | |
989 | } | |
990 | ||
991 | # install any of listed files | |
992 | # | |
993 | # If first argument is '-d' and second some destination path, first accessible | |
994 | # source is installed into this path, otherwise it will installed in the same | |
995 | # path as source. If none of listed files was installed, function return 1. | |
996 | # On first successful installation it returns with 0 status. | |
997 | # | |
998 | # Example: | |
999 | # | |
1000 | # inst_any -d /bin/foo /bin/bar /bin/baz | |
1001 | # | |
1002 | # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in | |
1003 | # initramfs. | |
1004 | inst_any() { | |
1005 | local to f | |
1006 | ||
1007 | [[ $1 = '-d' ]] && to="$2" && shift 2 | |
1008 | ||
1009 | for f in "$@"; do | |
1010 | if [[ -e $f ]]; then | |
1011 | [[ $to ]] && inst "$f" "$to" && return 0 | |
1012 | inst "$f" && return 0 | |
1013 | fi | |
1014 | done | |
1015 | ||
1016 | return 1 | |
1017 | } | |
1018 | ||
1019 | # dracut_install [-o ] <file> [<file> ... ] | |
1020 | # Install <file> to the initramfs image | |
1021 | # -o optionally install the <file> and don't fail, if it is not there | |
1022 | dracut_install() { | |
1023 | local _optional=no | |
1024 | if [[ $1 = '-o' ]]; then | |
1025 | _optional=yes | |
1026 | shift | |
1027 | fi | |
1028 | while (($# > 0)); do | |
1029 | if ! inst "$1" ; then | |
1030 | if [[ $_optional = yes ]]; then | |
1031 | dinfo "Skipping program $1 as it cannot be found and is" \ | |
1032 | "flagged to be optional" | |
1033 | else | |
1034 | dfatal "Failed to install $1" | |
1035 | exit 1 | |
1036 | fi | |
1037 | fi | |
1038 | shift | |
1039 | done | |
1040 | } | |
1041 | ||
0d6e798a HH |
1042 | # Install a single kernel module along with any firmware it may require. |
1043 | # $1 = full path to kernel module to install | |
1044 | install_kmod_with_fw() { | |
1045 | # no need to go further if the module is already installed | |
1046 | ||
1047 | [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \ | |
1048 | && return 0 | |
1049 | ||
1050 | [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0 | |
1051 | ||
1052 | if [[ $omit_drivers ]]; then | |
1053 | local _kmod=${1##*/} | |
1054 | _kmod=${_kmod%.ko} | |
1055 | _kmod=${_kmod/-/_} | |
1056 | if [[ "$_kmod" =~ $omit_drivers ]]; then | |
1057 | dinfo "Omitting driver $_kmod" | |
1058 | return 1 | |
1059 | fi | |
1060 | if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~ $omit_drivers ]]; then | |
1061 | dinfo "Omitting driver $_kmod" | |
1062 | return 1 | |
1063 | fi | |
1064 | fi | |
1065 | ||
1066 | [ -d "$initdir/.kernelmodseen" ] && \ | |
1067 | > "$initdir/.kernelmodseen/${1##*/}" | |
1068 | ||
1069 | inst_simple "$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \ | |
1070 | || return $? | |
1071 | ||
1072 | local _modname=${1##*/} _fwdir _found _fw | |
1073 | _modname=${_modname%.ko*} | |
1074 | for _fw in $(modinfo -k $KERNEL_VER -F firmware $1 2>/dev/null); do | |
1075 | _found='' | |
1076 | for _fwdir in $fw_dir; do | |
1077 | if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then | |
1078 | inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw" | |
1079 | _found=yes | |
1080 | fi | |
1081 | done | |
1082 | if [[ $_found != yes ]]; then | |
1083 | if ! grep -qe "\<${_modname//-/_}\>" /proc/modules; then | |
1084 | dinfo "Possible missing firmware \"${_fw}\" for kernel module" \ | |
1085 | "\"${_modname}.ko\"" | |
1086 | else | |
1087 | dwarn "Possible missing firmware \"${_fw}\" for kernel module" \ | |
1088 | "\"${_modname}.ko\"" | |
1089 | fi | |
1090 | fi | |
1091 | done | |
1092 | return 0 | |
1093 | } | |
1094 | ||
1095 | # Do something with all the dependencies of a kernel module. | |
1096 | # Note that kernel modules depend on themselves using the technique we use | |
1097 | # $1 = function to call for each dependency we find | |
1098 | # It will be passed the full path to the found kernel module | |
1099 | # $2 = module to get dependencies for | |
1100 | # rest of args = arguments to modprobe | |
1101 | # _fderr specifies FD passed from surrounding scope | |
1102 | for_each_kmod_dep() { | |
1103 | local _func=$1 _kmod=$2 _cmd _modpath _options _found=0 | |
1104 | shift 2 | |
1105 | modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | ( | |
1106 | while read _cmd _modpath _options; do | |
1107 | [[ $_cmd = insmod ]] || continue | |
1108 | $_func ${_modpath} || exit $? | |
1109 | _found=1 | |
1110 | done | |
1111 | [[ $_found -eq 0 ]] && exit 1 | |
1112 | exit 0 | |
1113 | ) | |
1114 | } | |
1115 | ||
1116 | # filter kernel modules to install certain modules that meet specific | |
1117 | # requirements. | |
1118 | # $1 = search only in subdirectory of /kernel/$1 | |
1119 | # $2 = function to call with module name to filter. | |
1120 | # This function will be passed the full path to the module to test. | |
c5315881 | 1121 | # The behavior of this function can vary depending on whether $hostonly is set. |
0d6e798a HH |
1122 | # If it is, we will only look at modules that are already in memory. |
1123 | # If it is not, we will look at all kernel modules | |
1124 | # This function returns the full filenames of modules that match $1 | |
1125 | filter_kernel_modules_by_path () ( | |
1126 | local _modname _filtercmd | |
1127 | if ! [[ $hostonly ]]; then | |
1128 | _filtercmd='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"' | |
1129 | _filtercmd+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"' | |
1130 | _filtercmd+=' -o -name "*.ko.xz"' | |
1131 | _filtercmd+=' 2>/dev/null' | |
1132 | else | |
1133 | _filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename ' | |
1134 | _filtercmd+='-k $KERNEL_VER 2>/dev/null' | |
1135 | fi | |
1136 | for _modname in $(eval $_filtercmd); do | |
1137 | case $_modname in | |
1138 | *.ko) "$2" "$_modname" && echo "$_modname";; | |
1139 | *.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko | |
1140 | $2 $initdir/$$.ko && echo "$_modname" | |
1141 | rm -f $initdir/$$.ko | |
1142 | ;; | |
1143 | *.ko.xz) xz -dc "$_modname" > $initdir/$$.ko | |
1144 | $2 $initdir/$$.ko && echo "$_modname" | |
1145 | rm -f $initdir/$$.ko | |
1146 | ;; | |
1147 | esac | |
1148 | done | |
1149 | ) | |
1150 | find_kernel_modules_by_path () ( | |
1151 | if ! [[ $hostonly ]]; then | |
1152 | find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \ | |
1153 | -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev/null | |
1154 | else | |
1155 | cut -d " " -f 1 </proc/modules \ | |
1156 | | xargs modinfo -F filename -k $KERNEL_VER 2>/dev/null | |
1157 | fi | |
1158 | ) | |
1159 | ||
1160 | filter_kernel_modules () { | |
1161 | filter_kernel_modules_by_path drivers "$1" | |
1162 | } | |
1163 | ||
1164 | find_kernel_modules () { | |
1165 | find_kernel_modules_by_path drivers | |
1166 | } | |
1167 | ||
1168 | # instmods [-c] <kernel module> [<kernel module> ... ] | |
1169 | # instmods [-c] <kernel subsystem> | |
1170 | # install kernel modules along with all their dependencies. | |
1171 | # <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage" | |
1172 | instmods() { | |
1173 | [[ $no_kernel = yes ]] && return | |
1174 | # called [sub]functions inherit _fderr | |
1175 | local _fderr=9 | |
1176 | local _check=no | |
1177 | if [[ $1 = '-c' ]]; then | |
1178 | _check=yes | |
1179 | shift | |
1180 | fi | |
1181 | ||
1182 | function inst1mod() { | |
1183 | local _ret=0 _mod="$1" | |
1184 | case $_mod in | |
1185 | =*) | |
1186 | if [ -f $KERNEL_MODS/modules.${_mod#=} ]; then | |
1187 | ( [[ "$_mpargs" ]] && echo $_mpargs | |
1188 | cat "${KERNEL_MODS}/modules.${_mod#=}" ) \ | |
1189 | | instmods | |
1190 | else | |
1191 | ( [[ "$_mpargs" ]] && echo $_mpargs | |
1192 | find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \ | |
1193 | | instmods | |
1194 | fi | |
1195 | ;; | |
1196 | --*) _mpargs+=" $_mod" ;; | |
1197 | i2o_scsi) return ;; # Do not load this diagnostic-only module | |
1198 | *) | |
1199 | _mod=${_mod##*/} | |
1200 | # if we are already installed, skip this module and go on | |
1201 | # to the next one. | |
1202 | [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return | |
1203 | ||
1204 | if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then | |
1205 | dinfo "Omitting driver ${_mod##$KERNEL_MODS}" | |
1206 | return | |
1207 | fi | |
1208 | # If we are building a host-specific initramfs and this | |
1209 | # module is not already loaded, move on to the next one. | |
1210 | [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc/modules \ | |
1211 | && ! echo $add_drivers | grep -qe "\<${_mod}\>" \ | |
1212 | && return | |
1213 | ||
1214 | # We use '-d' option in modprobe only if modules prefix path | |
1215 | # differs from default '/'. This allows us to use Dracut with | |
1216 | # old version of modprobe which doesn't have '-d' option. | |
1217 | local _moddirname=${KERNEL_MODS%%/lib/modules/*} | |
1218 | [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/" | |
1219 | ||
1220 | # ok, load the module, all its dependencies, and any firmware | |
1221 | # it may require | |
1222 | for_each_kmod_dep install_kmod_with_fw $_mod \ | |
1223 | --set-version $KERNEL_VER ${_moddirname} $_mpargs | |
1224 | ((_ret+=$?)) | |
1225 | ;; | |
1226 | esac | |
1227 | return $_ret | |
1228 | } | |
1229 | ||
1230 | function instmods_1() { | |
1231 | local _mod _mpargs | |
1232 | if (($# == 0)); then # filenames from stdin | |
1233 | while read _mod; do | |
1234 | inst1mod "${_mod%.ko*}" || { | |
1235 | if [ "$_check" = "yes" ]; then | |
1236 | dfatal "Failed to install $_mod" | |
1237 | return 1 | |
1238 | fi | |
1239 | } | |
1240 | done | |
1241 | fi | |
1242 | while (($# > 0)); do # filenames as arguments | |
1243 | inst1mod ${1%.ko*} || { | |
1244 | if [ "$_check" = "yes" ]; then | |
1245 | dfatal "Failed to install $1" | |
1246 | return 1 | |
1247 | fi | |
1248 | } | |
1249 | shift | |
1250 | done | |
1251 | return 0 | |
1252 | } | |
1253 | ||
1254 | local _ret _filter_not_found='FATAL: Module .* not found.' | |
1255 | set -o pipefail | |
1256 | # Capture all stderr from modprobe to _fderr. We could use {var}>... | |
1257 | # redirections, but that would make dracut require bash4 at least. | |
1258 | eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \ | |
1259 | | while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror | |
1260 | _ret=$? | |
1261 | set +o pipefail | |
1262 | return $_ret | |
1263 | } | |
898720b7 HH |
1264 | |
1265 | # inst_libdir_file [-n <pattern>] <file> [<file>...] | |
1266 | # Install a <file> located on a lib directory to the initramfs image | |
1267 | # -n <pattern> install non-matching files | |
1268 | inst_libdir_file() { | |
1269 | if [[ "$1" == "-n" ]]; then | |
1270 | local _pattern=$1 | |
1271 | shift 2 | |
1272 | for _dir in $libdirs; do | |
1273 | for _i in "$@"; do | |
1274 | for _f in "$_dir"/$_i; do | |
1275 | [[ "$_i" =~ $_pattern ]] || continue | |
1276 | [[ -e "$_i" ]] && dracut_install "$_i" | |
1277 | done | |
1278 | done | |
1279 | done | |
1280 | else | |
1281 | for _dir in $libdirs; do | |
1282 | for _i in "$@"; do | |
1283 | for _f in "$_dir"/$_i; do | |
1284 | [[ -e "$_f" ]] && dracut_install "$_f" | |
1285 | done | |
1286 | done | |
1287 | done | |
1288 | fi | |
1289 | } | |
1290 | ||
1291 | do_test() { | |
33a5e20f HH |
1292 | if [[ $UID != "0" ]]; then |
1293 | echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2 | |
1294 | exit 0 | |
1295 | fi | |
1296 | ||
898720b7 HH |
1297 | # Detect lib paths |
1298 | [[ $libdir ]] || for libdir in /lib64 /lib; do | |
1299 | [[ -d $libdir ]] && libdirs+=" $libdir" && break | |
1300 | done | |
1301 | ||
1302 | [[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do | |
1303 | [[ -d $usrlibdir ]] && libdirs+=" $usrlibdir" && break | |
1304 | done | |
1305 | ||
1306 | import_testdir | |
889a9042 | 1307 | import_initdir |
898720b7 HH |
1308 | |
1309 | while (($# > 0)); do | |
1310 | case $1 in | |
1311 | --run) | |
1312 | echo "TEST RUN: $TEST_DESCRIPTION" | |
1313 | test_run | |
1314 | ret=$? | |
1315 | if [ $ret -eq 0 ]; then | |
1316 | echo "TEST RUN: $TEST_DESCRIPTION [OK]" | |
1317 | else | |
1318 | echo "TEST RUN: $TEST_DESCRIPTION [FAILED]" | |
1319 | fi | |
1320 | exit $ret;; | |
1321 | --setup) | |
1322 | echo "TEST SETUP: $TEST_DESCRIPTION" | |
1323 | test_setup | |
1324 | exit $?;; | |
1325 | --clean) | |
1326 | echo "TEST CLEANUP: $TEST_DESCRIPTION" | |
1327 | test_cleanup | |
1328 | rm -fr "$TESTDIR" | |
1329 | rm -f .testdir | |
1330 | exit $?;; | |
1331 | --all) | |
1332 | echo -n "TEST: $TEST_DESCRIPTION "; | |
1333 | ( | |
1334 | test_setup && test_run | |
1335 | ret=$? | |
1336 | test_cleanup | |
1337 | rm -fr "$TESTDIR" | |
1338 | rm -f .testdir | |
1339 | exit $ret | |
1340 | ) </dev/null >test.log 2>&1 | |
1341 | ret=$? | |
1342 | if [ $ret -eq 0 ]; then | |
1343 | rm test.log | |
1344 | echo "[OK]" | |
1345 | else | |
1346 | echo "[FAILED]" | |
1347 | echo "see $(pwd)/test.log" | |
1348 | fi | |
1349 | exit $ret;; | |
1350 | *) break ;; | |
1351 | esac | |
1352 | shift | |
1353 | done | |
1354 | } |