blob: 019c1935a216cd03a07417e99a3e297f84edce97 [file] [log] [blame]
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -07001#!/bin/bash
2
3# Copyright 2019 Google Inc. All rights reserved.
4
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8
9# http://www.apache.org/licenses/LICENSE-2.0
10
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17if [[ "$OSTYPE" != "linux-gnu" ]]; then
18 echo "error: must be running linux"
19 exit 1
20fi
21
Tristan Muntsinger63c657a2019-10-23 23:19:40 -070022# escalate to superuser
23if [ "$UID" -ne 0 ]; then
24 exec sudo bash "$0"
25fi
26
Tristan Muntsingera18d9e52019-10-24 14:52:38 -070027cleanup() {
28 echo "Starting up network-manager..."
29 service network-manager start
30 if [ $? != 0 ]; then
31 echo "error: failed to start network-manager"
32 exit 1
33 fi
34
35 echo "Starting up networking..."
36 service networking start
37 if [ $? != 0 ]; then
38 echo "error: failed to start networking"
39 exit 1
40 fi
41 if [ ! -z "$1" ]; then
42 exit $1
43 fi
44}
45
Tristan Muntsingere935b212019-10-09 21:53:11 -070046sleep_time=0.1
Tristan Muntsinger63c657a2019-10-23 23:19:40 -070047max_attempts=100
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -070048DEFAULTNET=$1
49if [ "$DEFAULTNET" == "" ]; then
Tristan Muntsingere935b212019-10-09 21:53:11 -070050 warn_no_default_network=0
Tristan Muntsingere935b212019-10-09 21:53:11 -070051 warn_disconnect_rockpi=0
Tristan Muntsinger63c657a2019-10-23 23:19:40 -070052 attempts=0
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -070053 while true; do
Tristan Muntsinger63c657a2019-10-23 23:19:40 -070054 NETLIST=`ip link | grep "state UP" | sed 's/[0-9]*: \([^:]*\):.*/\1/'`
55 if [[ "${NETLIST}" == "" ]]; then
Tristan Muntsingere935b212019-10-09 21:53:11 -070056 if [[ $warn_no_default_network -eq 0 ]]; then
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -070057 echo "error: couldn't detect any connected default network"
Tristan Muntsingere935b212019-10-09 21:53:11 -070058 warn_no_default_network=1
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -070059 fi
60 continue
Tristan Muntsinger63c657a2019-10-23 23:19:40 -070061 elif [ `echo "${NETLIST}" | wc -l` -eq 1 ]; then
62 DEFAULTNET=${NETLIST}
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -070063 break
Tristan Muntsinger63c657a2019-10-23 23:19:40 -070064 elif [ `echo "${NETLIST}" | wc -l` -ne 1 ]; then
Tristan Muntsingere935b212019-10-09 21:53:11 -070065 if [[ $warn_disconnect_rockpi -eq 0 ]]; then
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -070066 echo "Please disconnect the network cable from the Rock Pi"
Tristan Muntsingere935b212019-10-09 21:53:11 -070067 warn_disconnect_rockpi=1
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -070068 fi
Tristan Muntsinger63c657a2019-10-23 23:19:40 -070069 if [[ ${attempts} -gt ${max_attempts} ]]; then
70 echo -e "\nerror: detected multiple connected networks, please tell me what to do:"
71 count=1
72 for net in ${NETLIST}; do
73 echo "${count}) $net"
74 let count+=1
75 done
76 read -p "Enter the number of your default network connection: " num_default
77 count=1
78 for net in ${NETLIST}; do
79 if [ ${count} -eq ${num_default} ]; then
80 echo "Setting default to: ${net}"
81 DEFAULTNET=${net}
82 fi
83 let count+=1
84 done
Tristan Muntsingere935b212019-10-09 21:53:11 -070085 warn_no_default_network=0
Tristan Muntsinger63c657a2019-10-23 23:19:40 -070086 break
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -070087 fi
Tristan Muntsinger63c657a2019-10-23 23:19:40 -070088 echo -ne "\r"
89 printf "Manual configuration in %.1f seconds..." "$(( max_attempts-attempts ))e-1"
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -070090 sleep $sleep_time
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -070091 fi
Tristan Muntsinger63c657a2019-10-23 23:19:40 -070092 let attempts+=1
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -070093 done
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -070094fi
Tristan Muntsingere935b212019-10-09 21:53:11 -070095echo "Found default network at ${DEFAULTNET}"
Tristan Muntsingere935b212019-10-09 21:53:11 -070096
Tristan Muntsinger63c657a2019-10-23 23:19:40 -070097if [ "${ROCKNET}" == "" ]; then
98 echo "Please reconnect network cable from Rock Pi to PC's spare network port"
99 attempts=0
100 while true; do
101 NETLIST=`ip link | grep "state UP" | grep -v $DEFAULTNET | sed 's/[0-9]*: \([^:]*\):.*/\1/' | awk 'NF'`
102 networks=`echo "$NETLIST" | wc -l`
103 if [[ "${NETLIST}" == "" ]]; then
104 networks=0
105 fi
106 if [ $networks -eq 1 ]; then
107 ROCKNET=${NETLIST}
Tristan Muntsingere935b212019-10-09 21:53:11 -0700108 break
Tristan Muntsinger63c657a2019-10-23 23:19:40 -0700109 elif [ $networks -gt 1 ]; then
110 if [[ ${attempts} -gt ${max_attempts} ]]; then
111 echo -e "\nerror: detected multiple connected networks, please tell me what to do:"
112 count=1
113 for net in ${NETLIST}; do
114 echo "${count}) $net"
115 let count+=1
116 done
117 read -p "Enter the number of your rock pi network connection: " num_rockpi
118 count=1
119 for net in ${NETLIST}; do
120 if [ ${count} -eq ${num_rockpi} ]; then
121 echo "Setting rock pi to: ${net}"
122 ROCKNET=${net}
123 fi
124 let count+=1
125 done
126 break
127 fi
128 echo -ne "\r"
129 printf "Manual configuration in %.1f seconds..." "$(( max_attempts-attempts ))e-1"
130 let attempts+=1
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -0700131 fi
132 sleep $sleep_time
Tristan Muntsinger63c657a2019-10-23 23:19:40 -0700133 done
134fi
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -0700135echo "Found Rock Pi network at ${ROCKNET}"
136sudo ifconfig ${ROCKNET} down
137
Tristan Muntsingera18d9e52019-10-24 14:52:38 -0700138echo "Downloading dnsmasq..."
139apt-get install -d -y dnsmasq >/dev/null
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -0700140
Tristan Muntsingera18d9e52019-10-24 14:52:38 -0700141echo "Shutting down network-manager to prevent interference..."
142service network-manager stop
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -0700143if [ $? != 0 ]; then
Tristan Muntsingera18d9e52019-10-24 14:52:38 -0700144 echo "error: failed to stop network-manager"
145 cleanup 1
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -0700146fi
147
Tristan Muntsingera18d9e52019-10-24 14:52:38 -0700148echo "Shutting down networking to prevent interference..."
149service networking stop
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -0700150if [ $? != 0 ]; then
Tristan Muntsingera18d9e52019-10-24 14:52:38 -0700151 echo "error: failed to stop networking"
152 cleanup 1
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -0700153fi
154
Tristan Muntsingera18d9e52019-10-24 14:52:38 -0700155echo "Installing dnsmasq..."
156apt-get install dnsmasq >/dev/null
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -0700157
158echo "Enabling dnsmasq daemon..."
159cat /etc/default/dnsmasq | grep "ENABLED" >/dev/null
160if [ $? == 0 ]; then
161 sed -i 's/.*ENABLED.*/ENABLED=1/' /etc/default/dnsmasq
162else
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -0700163 echo "ENABLED=1" >> /etc/default/dnsmasq
164fi
165
166echo "Configuring dnsmasq for Rock Pi network..."
167cat >/etc/dnsmasq.d/${ROCKNET}.conf << EOF
168interface=${ROCKNET}
169bind-interfaces
170except-interface=lo
171dhcp-authoritative
172leasefile-ro
173port=0
174dhcp-range=192.168.0.100,192.168.0.199
175EOF
176
Tristan Muntsingera18d9e52019-10-24 14:52:38 -0700177echo "Configuring udev rules..."
178cat >/etc/udev/rules.d/82-${ROCKNET}.rules <<EOF
179ACTION=="add", SUBSYSTEM=="net", KERNEL=="${ROCKNET}", ENV{NM_UNMANAGED}="1"
180EOF
181
182echo "Configuring network interface..."
183cat >/etc/network/interfaces.d/${ROCKNET}.conf <<EOF
184auto ${ROCKNET}
185iface ${ROCKNET} inet static
186 address 192.168.0.1
187 netmask 255.255.255.0
188EOF
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -0700189
190echo "Enabling IP forwarding..."
191echo 1 >/proc/sys/net/ipv4/ip_forward
192
193echo "Creating IP tables rules script..."
194cat > /usr/local/sbin/iptables-rockpi.sh << EOF
195#!/bin/bash
196/sbin/iptables -A FORWARD -i ${ROCKNET} -o ${DEFAULTNET} -m state --state RELATED,ESTABLISHED -j ACCEPT
197/sbin/iptables -A FORWARD -i ${ROCKNET} -o ${DEFAULTNET} -j ACCEPT
198/sbin/iptables -t nat -A POSTROUTING -o ${DEFAULTNET} -j MASQUERADE
199EOF
200sudo chown root:root /usr/local/sbin/iptables-rockpi.sh
201sudo chmod 750 /usr/local/sbin/iptables-rockpi.sh
202
203echo "Creating IP tables rules service..."
204cat > /etc/systemd/system/iptables-rockpi.service << EOF
205[Unit]
206Description=iptables rockpi service
207After=network.target
208
209[Service]
210Type=oneshot
211ExecStart=/usr/local/sbin/iptables-rockpi.sh
212RemainAfterExit=true
213StandardOutput=journal
214
215[Install]
216WantedBy=multi-user.target
217EOF
218
219echo "Reloading systemd manager configuration..."
220sudo systemctl daemon-reload
221
222echo "Start IP tables rules service..."
223sudo systemctl enable iptables-rockpi
224sudo systemctl start iptables-rockpi
225
Tristan Muntsingera18d9e52019-10-24 14:52:38 -0700226cleanup
227
228echo "Restarting dnsmasq service..."
229service dnsmasq restart
230if [ $? != 0 ]; then
231 echo "error: failed to restart dnsmasq"
232 exit 1
233fi
234
235# Verify the Rock Pi was configured correctly
236ip link show ${ROCKNET} >/dev/null
237if [ $? != 0 ]; then
238 echo "error: wasn't able to successfully configure connection to Rock Pi"
239 exit 1
240fi
241
Tristan Muntsinger16b4aa62019-07-30 16:00:17 -0700242echo "Searching for Rock Pi's IP address..."
243while true; do
244 rockip=`cat /proc/net/arp | grep ${ROCKNET} | grep -v 00:00:00:00:00:00 | cut -d" " -f1`
245 if [[ ${#rockip} -ge 7 ]] && [[ ${#rockip} -le 15 ]]; then
246 break
247 fi
248 sleep 0.1
249done
250
251echo "Writing Rock Pi configuration to ~/.ssh/config..."
252USER_HOME=$(getent passwd $SUDO_USER | cut -d: -f6)
253grep -w "Host rock01" $USER_HOME/.ssh/config > /dev/null 2>&1
254if [ $? != 0 ]; then
255 cat >>$USER_HOME/.ssh/config << EOF
256Host rock01
257 HostName ${rockip}
258 User vsoc-01
259 IdentityFile ~/.ssh/rock01_key
260 LocalForward 6520 127.0.0.1:6520
261 LocalForward 6444 127.0.0.1:6444
262EOF
263else
264 sed -i '/Host rock01/{n;s/.*/ HostName '${rockip}'/}' $USER_HOME/.ssh/config
265fi
266grep -w "Host rockpi01" $USER_HOME/.ssh/config > /dev/null 2>&1
267if [ $? != 0 ]; then
268 cat >>$USER_HOME/.ssh/config << EOF
269Host rockpi01
270 HostName ${rockip}
271 User vsoc-01
272 IdentityFile ~/.ssh/rock01_key
273EOF
274else
275 sed -i '/Host rockpi01/{n;s/.*/ HostName '${rockip}'/}' $USER_HOME/.ssh/config
276fi
277
278sudo chown $SUDO_USER:`id -ng $SUDO_USER` $USER_HOME/.ssh/config
279sudo chmod 600 $USER_HOME/.ssh/config
280
281echo "Creating ssh key..."
282sudo -u $SUDO_USER echo "n" | sudo -u $SUDO_USER ssh-keygen -q -t rsa -b 4096 -f $USER_HOME/.ssh/rock01_key -N '' >/dev/null 2>&1
283tmpfile=`mktemp`
284echo "echo cuttlefish" > "$tmpfile"
285chmod a+x "$tmpfile"
286chown $SUDO_USER "$tmpfile"
287sudo SSH_ASKPASS="${tmpfile}" DISPLAY=:0 su $SUDO_USER -c "setsid -w ssh-copy-id -i ${USER_HOME}/.ssh/rock01_key -o StrictHostKeyChecking=no vsoc-01@${rockip} >/dev/null 2>&1"
288if [ $? != 0 ]; then
289 sed -i "/${rockip}/d" ${USER_HOME}/.ssh/known_hosts
290 sudo SSH_ASKPASS="${tmpfile}" DISPLAY=:0 su $SUDO_USER -c "setsid -w ssh-copy-id -i ${USER_HOME}/.ssh/rock01_key -o StrictHostKeyChecking=no vsoc-01@${rockip} >/dev/null 2>&1"
291 if [ $? != 0 ]; then
292 echo "error: wasn't able to connect to Rock Pi over ssh"
293 exit 1
294 fi
295fi
296
297echo "Successfully configured!"
298echo " Host: 192.168.0.1"
299echo "RockPi: ${rockip}"
300echo "SSH Alias: rock01 (auto port-forwarding)"
301echo "SSH Alias: rockpi01 (no port-forwarding)"