1#!/bin/bash
2# The following is a synthesis of info in:
3#
4#  http://vmsplice.net/~stefan/stefanha-kernel-recipes-2015.pdf
5#  http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/README
6#
7KBASE=../../linux
8#APPEND="console=ttyS0"
9
10function die {
11    echo "$*"
12    exit 1
13}
14
15pushd ..
16make test || die "failed to make test of libcap tree"
17make -C progs tcapsh-static || die "failed to make progs/tcapsh-static"
18popd
19
20# Assumes desired make *config (eg. make defconfig) is already done.
21pushd $KBASE
22pwd
23make V=1 all || die "failed to build kernel: $0"
24popd
25
26HERE=$(/bin/pwd)
27
28cat > fs.conf <<EOF
29file /init test-init.sh 0755 0 0
30dir /etc 0755 0 0
31file /etc/passwd test-passwd 0444 0 0
32dir /lib 0755 0 0
33dir /proc 0755 0 0
34dir /dev 0755 0 0
35dir /sys 0755 0 0
36dir /sbin 0755 0 0
37file /sbin/busybox /usr/sbin/busybox 0755 0 0
38dir /bin 0755 0 0
39file /bin/myprompt test-prompt.sh 0755 0 0
40file /bin/bash test-bash.sh 0755 0 0
41dir /usr 0755 0 0
42dir /usr/bin 0755 0 0
43dir /root 0755 0 0
44file /root/quicktest.sh $HERE/../progs/quicktest.sh 0755 0 0
45file /root/setcap $HERE/../progs/setcap 0755 0 0
46file /root/getcap $HERE/../progs/getcap 0755 0 0
47file /root/capsh $HERE/../progs/capsh 0755 0 0
48file /root/getpcaps $HERE/../progs/getpcaps 0755 0 0
49file /root/tcapsh-static $HERE/../progs/tcapsh-static 0755 0 0
50EOF
51
52# convenience for some local experiments
53if [ -f "$HERE/extras.sh" ]; then
54    echo "local, uncommitted enhancements to kernel test"
55    . "$HERE/extras.sh"
56fi
57
58COMMANDS="awk cat chmod cp dmesg fgrep id less ln ls mkdir mount pwd rm rmdir sh sort umount uniq vi"
59for f in $COMMANDS; do
60    echo slink /bin/$f /sbin/busybox 0755 0 0 >> fs.conf
61done
62
63UCOMMANDS="id cut"
64for f in $UCOMMANDS; do
65    echo slink /usr/bin/$f /sbin/busybox 0755 0 0 >> fs.conf
66done
67
68$KBASE/usr/gen_init_cpio fs.conf | gzip -9 > initramfs.img
69
70KERNEL=$KBASE/arch/$(uname -m)/boot/bzImage
71
72qemu-system-$(uname -m) -m 1024 \
73		   -kernel $KERNEL \
74		   -initrd initramfs.img \
75		   -append "$APPEND" \
76		   -smp sockets=2,dies=1,cores=4
77