1#!/bin/bash
2
3# Kernel configration options.
4OPTIONS=" IPV6 IPV6_ROUTER_PREF IPV6_MULTIPLE_TABLES IPV6_ROUTE_INFO"
5OPTIONS="$OPTIONS TUN SYN_COOKIES IP_ADVANCED_ROUTER IP_MULTIPLE_TABLES"
6OPTIONS="$OPTIONS NETFILTER NETFILTER_ADVANCED NETFILTER_XTABLES"
7OPTIONS="$OPTIONS NETFILTER_XT_MARK NETFILTER_XT_TARGET_MARK"
8OPTIONS="$OPTIONS IP_NF_IPTABLES IP_NF_MANGLE"
9OPTIONS="$OPTIONS IP6_NF_IPTABLES IP6_NF_MANGLE INET6_IPCOMP"
10OPTIONS="$OPTIONS IPV6_PRIVACY IPV6_OPTIMISTIC_DAD"
11# For 3.1 kernels, where devtmpfs is not on by default.
12OPTIONS="$OPTIONS DEVTMPFS DEVTMPFS_MOUNT"
13
14# How many tap interfaces to create.
15NUMTAPINTERFACES=2
16
17# The root filesystem disk image we'll use.
18ROOTFS=net_test.rootfs.20150203
19COMPRESSED_ROOTFS=$ROOTFS.xz
20URL=https://dl.google.com/dl/android/$COMPRESSED_ROOTFS
21
22# Figure out which test to run.
23if [ -z "$1" ]; then
24  echo "Usage: $0 <test>" >&2
25  exit 1
26fi
27test=$1
28
29set -e
30
31# Check if we need to uncompress the disk image.
32# We use xz because it compresses better: to 42M vs 72M (gzip) / 62M (bzip2).
33cd $(dirname $0)
34if [ ! -f $ROOTFS ]; then
35  echo "Deleting $COMPRESSED_ROOTFS" >&2
36  rm -f $COMPRESSED_ROOTFS
37  echo "Downloading $URL" >&2
38  wget $URL
39  echo "Uncompressing $COMPRESSED_ROOTFS" >&2
40  unxz $COMPRESSED_ROOTFS
41fi
42echo "Using $ROOTFS"
43cd -
44
45# Create NUMTAPINTERFACES tap interfaces on the host, and prepare UML command
46# line params to use them. The interfaces are called <user>TAP0, <user>TAP1,
47# ..., on the host, and eth0, eth1, ..., in the VM.
48user=${USER:0:10}
49tapinterfaces=
50netconfig=
51for id in $(seq 0 $(( NUMTAPINTERFACES - 1 )) ); do
52  tap=${user}TAP$id
53  tapinterfaces="$tapinterfaces $tap"
54  mac=$(printf fe:fd:00:00:00:%02x $id)
55  netconfig="$netconfig eth$id=tuntap,$tap,$mac"
56done
57
58for tap in $tapinterfaces; do
59  if ! ip link list $tap > /dev/null; then
60    echo "Creating tap interface $tap" >&2
61    sudo tunctl -u $USER -t $tap
62    sudo ip link set $tap up
63  fi
64done
65
66# Exporting ARCH=um SUBARCH=x86_64 doesn't seem to work, as it "sometimes" (?)
67# results in a 32-bit kernel.
68
69# If there's no kernel config at all, create one or UML won't work.
70[ -f .config ] || make defconfig ARCH=um SUBARCH=x86_64
71
72# Enable the kernel config options listed in $OPTIONS.
73cmdline=${OPTIONS// / -e }
74./scripts/config $cmdline
75
76# olddefconfig doesn't work on old kernels.
77if ! make olddefconfig ARCH=um SUBARCH=x86_64 CROSS_COMPILE= ; then
78  cat >&2 << EOF
79
80Warning: "make olddefconfig" failed.
81Perhaps this kernel is too old to support it.
82You may get asked lots of questions.
83Keep enter pressed to accept the defaults.
84
85EOF
86fi
87
88# Compile the kernel.
89make -j12 linux ARCH=um SUBARCH=x86_64 CROSS_COMPILE=
90
91# Get the absolute path to the test file that's being run.
92dir=/host$(dirname $(readlink -f $0))
93
94# Start the VM.
95exec ./linux umid=net_test ubda=$(dirname $0)/$ROOTFS \
96    mem=512M init=/sbin/net_test.sh net_test=$dir/$test \
97    $netconfig
98