1#!/bin/bash
2#
3# Create an initrd directory if one does not already exist.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, you can access it online at
17# http://www.gnu.org/licenses/gpl-2.0.html.
18#
19# Copyright (C) IBM Corporation, 2013
20#
21# Author: Connor Shu <Connor.Shu@ibm.com>
22
23D=tools/testing/selftests/rcutorture
24
25# Prerequisite checks
26[ -z "$D" ] && echo >&2 "No argument supplied" && exit 1
27if [ ! -d "$D" ]; then
28    echo >&2 "$D does not exist: Malformed kernel source tree?"
29    exit 1
30fi
31if [ -s "$D/initrd/init" ]; then
32    echo "$D/initrd/init already exists, no need to create it"
33    exit 0
34fi
35
36T=${TMPDIR-/tmp}/mkinitrd.sh.$$
37trap 'rm -rf $T' 0 2
38mkdir $T
39
40cat > $T/init << '__EOF___'
41#!/bin/sh
42# Run in userspace a few milliseconds every second.  This helps to
43# exercise the NO_HZ_FULL portions of RCU.
44while :
45do
46	q=
47	for i in \
48		a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a \
49		a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a \
50		a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a \
51		a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a \
52		a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a \
53		a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
54	do
55		q="$q $i"
56	done
57	sleep 1
58done
59__EOF___
60
61# Try using dracut to create initrd
62if command -v dracut >/dev/null 2>&1
63then
64	echo Creating $D/initrd using dracut.
65	# Filesystem creation
66	dracut --force --no-hostonly --no-hostonly-cmdline --module "base" $T/initramfs.img
67	cd $D
68	mkdir -p initrd
69	cd initrd
70	zcat $T/initramfs.img | cpio -id
71	cp $T/init init
72	chmod +x init
73	echo Done creating $D/initrd using dracut
74	exit 0
75fi
76
77# No dracut, so create a C-language initrd/init program and statically
78# link it.  This results in a very small initrd, but might be a bit less
79# future-proof than dracut.
80echo "Could not find dracut, attempting C initrd"
81cd $D
82mkdir -p initrd
83cd initrd
84cat > init.c << '___EOF___'
85#ifndef NOLIBC
86#include <unistd.h>
87#include <sys/time.h>
88#endif
89
90volatile unsigned long delaycount;
91
92int main(int argc, int argv[])
93{
94	int i;
95	struct timeval tv;
96	struct timeval tvb;
97
98	for (;;) {
99		sleep(1);
100		/* Need some userspace time. */
101		if (gettimeofday(&tvb, NULL))
102			continue;
103		do {
104			for (i = 0; i < 1000 * 100; i++)
105				delaycount = i * i;
106			if (gettimeofday(&tv, NULL))
107				break;
108			tv.tv_sec -= tvb.tv_sec;
109			if (tv.tv_sec > 1)
110				break;
111			tv.tv_usec += tv.tv_sec * 1000 * 1000;
112			tv.tv_usec -= tvb.tv_usec;
113		} while (tv.tv_usec < 1000);
114	}
115	return 0;
116}
117___EOF___
118
119# build using nolibc on supported archs (smaller executable) and fall
120# back to regular glibc on other ones.
121if echo -e "#if __x86_64__||__i386__||__i486__||__i586__||__i686__" \
122           "||__ARM_EABI__||__aarch64__\nyes\n#endif" \
123   | ${CROSS_COMPILE}gcc -E -nostdlib -xc - \
124   | grep -q '^yes'; then
125	# architecture supported by nolibc
126        ${CROSS_COMPILE}gcc -fno-asynchronous-unwind-tables -fno-ident \
127		-nostdlib -include ../bin/nolibc.h -lgcc -s -static -Os \
128		-o init init.c
129else
130	${CROSS_COMPILE}gcc -s -static -Os -o init init.c
131fi
132
133rm init.c
134echo "Done creating a statically linked C-language initrd"
135
136exit 0
137