• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2if [[ -n "${verbose}" ]]; then
3  echo 'Current working directory:'
4  echo " - according to builtin:  [$(pwd)]"
5  echo " - according to /bin/pwd: [$(/bin/pwd)]"
6  echo
7
8  echo 'Shell environment:'
9  env
10  echo
11
12  echo -n "net_test.sh (pid $$, parent ${PPID}, tty $(tty)) running [$0] with args:"
13  for arg in "$@"; do
14    echo -n " [${arg}]"
15  done
16  echo
17  echo
18fi
19
20if [[ "$(tty)" == 'not a tty' ]]; then
21  echo 'not a tty? perhaps not quite real kernel default /dev/console - trying to fix.'
22  if [[ -c /dev/console ]]; then
23    [[ "$(readlink /proc/$$/fd/0)" != '/dev/console' ]] || exec < /dev/console
24    [[ "$(readlink /proc/$$/fd/1)" != '/dev/console' ]] || exec > /dev/console
25    [[ "$(readlink /proc/$$/fd/2)" != '/dev/console' ]] || exec 2> /dev/console
26  fi
27fi
28
29if [[ "$(tty)" == '/dev/console' ]]; then
30  ARCH="$(uname -m)"
31  # Underscore is illegal in hostname, replace with hyphen
32  ARCH="${ARCH//_/-}"
33
34  # setsid + /dev/tty{,AMA,S}0 allows bash's job control to work, ie. Ctrl+C/Z
35  if [[ -e '/proc/exitcode' ]]; then
36    # exists only in UML
37    CON='/dev/tty0'
38    hostname "uml-${ARCH}"
39  elif [[ -c '/dev/ttyAMA0' ]]; then
40    # Qemu for arm (note: /dev/ttyS0 also exists for exitcode)
41    CON='/dev/ttyAMA0'
42    hostname "qemu-${ARCH}"
43  elif [[ -c '/dev/ttyS0' ]]; then
44    # Qemu for x86 (note: /dev/ttyS1 also exists for exitcode)
45    CON='/dev/ttyS0'
46    hostname "qemu-${ARCH}"
47  else
48    # Can't figure it out, job control won't work, tough luck
49    echo 'Unable to figure out proper console - job control will not work.' >&2
50    CON=''
51    hostname "local-${ARCH}"
52  fi
53
54  unset ARCH
55
56  echo -n "$(hostname): Currently tty[/dev/console], but it should be [${CON}]..."
57
58  if [[ -n "${CON}" ]]; then
59    # Redirect std{in,out,err} to the console equivalent tty
60    # which actually supports all standard tty ioctls
61    exec <"${CON}" >&"${CON}"
62
63    # Bash wants to be session leader, hence need for setsid
64    echo " re-executing..."
65    exec /usr/bin/setsid "$0" "$@"
66    # If the above exec fails, we just fall through...
67    # (this implies failure to *find* setsid, not error return from bash,
68    #  in practice due to image construction this cannot happen)
69  else
70    echo
71  fi
72
73  # In case we fall through, clean up
74  unset CON
75fi
76
77if [[ -n "${verbose}" ]]; then
78  echo 'TTY settings:'
79  stty
80  echo
81
82  echo 'TTY settings (verbose):'
83  stty -a
84  echo
85
86  echo 'Restoring TTY sanity...'
87fi
88
89stty sane
90stty 115200
91[[ -z "${console_cols}" ]] || stty columns "${console_cols}"
92[[ -z "${console_rows}" ]] || stty rows    "${console_rows}"
93
94if [[ -n "${verbose}" ]]; then
95  echo
96
97  echo 'TTY settings:'
98  stty
99  echo
100
101  echo 'TTY settings (verbose):'
102  stty -a
103  echo
104fi
105
106# By the time we get here we should have a sane console:
107#  - 115200 baud rate
108#  - appropriate (and known) width and height (note: this assumes
109#    that the terminal doesn't get further resized)
110#  - it is no longer /dev/console, so job control should function
111#    (this means working ctrl+c [abort] and ctrl+z [suspend])
112
113
114# This defaults to 60 which is needlessly long during boot
115# (we will reset it back to the default later)
116echo 0 > /proc/sys/kernel/random/urandom_min_reseed_secs
117
118if [[ -n "${entropy}" ]]; then
119  echo "adding entropy from hex string [${entropy}]" >&2
120
121  # In kernel/include/uapi/linux/random.h RNDADDENTROPY is defined as
122  # _IOW('R', 0x03, int[2]) =(R is 0x52)= 0x40085203 = 1074287107
123  /usr/bin/python 3>/dev/random <<EOF
124import fcntl, struct
125rnd = '${entropy}'.decode('base64')
126fcntl.ioctl(3, 0x40085203, struct.pack('ii', len(rnd) * 8, len(rnd)) + rnd)
127EOF
128
129fi
130
131# Make sure the urandom pool has a chance to initialize before we reset
132# the reseed timer back to 60 seconds.  One timer tick should be enough.
133sleep 1.1
134
135# By this point either 'random: crng init done' (newer kernels)
136# or 'random: nonblocking pool is initialized' (older kernels)
137# should have been printed out to dmesg/console.
138
139# Reset it back to boot time default
140echo 60 > /proc/sys/kernel/random/urandom_min_reseed_secs
141
142
143# In case IPv6 is compiled as a module.
144[ -f /proc/net/if_inet6 ] || insmod $DIR/kernel/net-next/net/ipv6/ipv6.ko
145
146# Minimal network setup.
147ip link set lo up
148ip link set lo mtu 16436
149ip link set eth0 up
150
151# Allow people to run ping.
152echo '0 2147483647' > /proc/sys/net/ipv4/ping_group_range
153
154# Read environment variables passed to the kernel to determine if script is
155# running on builder and to find which test to run.
156
157if [ "$net_test_mode" != "builder" ]; then
158  # Fall out to a shell once the test completes or if there's an error.
159  trap "exec /bin/bash" ERR EXIT
160fi
161
162echo -e "Running $net_test $net_test_args\n"
163$net_test $net_test_args
164rv="$?"
165
166# Write exit code of net_test to a file so that the builder can use it
167# to signal failure if any tests fail.
168echo "${rv}" > "${net_test_exitcode}"
169
170# Additionally on UML make it the exit code of UML kernel binary itself.
171if [[ -e '/proc/exitcode' ]]; then
172  echo "${rv}" > /proc/exitcode
173fi
174