• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/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
22# escalate to superuser
23if [ "$UID" -ne 0 ]; then
24	exec sudo bash "$0"
25fi
26
27cleanup() {
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
46sleep_time=0.1
47max_attempts=100
48DEFAULTNET=$1
49if [ "$DEFAULTNET" == "" ]; then
50	warn_no_default_network=0
51	warn_disconnect_rockpi=0
52	attempts=0
53	while true; do
54		NETLIST=`ip link | grep "state UP" | sed 's/[0-9]*: \([^:]*\):.*/\1/'`
55		if [[ "${NETLIST}" == "" ]]; then
56			if [[ $warn_no_default_network -eq 0 ]]; then
57				echo "error: couldn't detect any connected default network"
58				warn_no_default_network=1
59			fi
60			continue
61		elif [ `echo "${NETLIST}" | wc -l` -eq 1 ]; then
62			DEFAULTNET=${NETLIST}
63			break
64		elif [ `echo "${NETLIST}" | wc -l` -ne 1 ]; then
65			if [[ $warn_disconnect_rockpi -eq 0 ]]; then
66				echo "Please disconnect the network cable from the Rock Pi"
67				warn_disconnect_rockpi=1
68			fi
69			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
85				warn_no_default_network=0
86				break
87			fi
88			echo -ne "\r"
89			printf "Manual configuration in %.1f seconds..." "$(( max_attempts-attempts ))e-1"
90			sleep $sleep_time
91		fi
92		let attempts+=1
93	done
94fi
95echo "Found default network at ${DEFAULTNET}"
96
97if [ "${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}
108			break
109		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
131		fi
132		sleep $sleep_time
133	done
134fi
135echo "Found Rock Pi network at ${ROCKNET}"
136sudo ifconfig ${ROCKNET} down
137
138echo "Downloading dnsmasq..."
139apt-get install -d -y dnsmasq >/dev/null
140
141echo "Shutting down network-manager to prevent interference..."
142service network-manager stop
143if [ $? != 0 ]; then
144	echo "error: failed to stop network-manager"
145	cleanup 1
146fi
147
148echo "Shutting down networking to prevent interference..."
149service networking stop
150if [ $? != 0 ]; then
151	echo "error: failed to stop networking"
152	cleanup 1
153fi
154
155echo "Installing dnsmasq..."
156apt-get install dnsmasq >/dev/null
157
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
163	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
177echo "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
189
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
226cleanup
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
242echo "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)"
302