Skip to content

Instantly share code, notes, and snippets.

@ScottDillman
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ScottDillman/dff8e813b229f9fa03aa to your computer and use it in GitHub Desktop.
Save ScottDillman/dff8e813b229f9fa03aa to your computer and use it in GitHub Desktop.
#!/bin/bash
#title...........:raspi-boot.sh
#description.....:Set up bridged network and launch raspberry pi image
#author..........:scott@bitwise.ninja
#date............:2015.07.20
#version.........:0.5
#usage...........:sudo bash raspi-boot.sh <usernmae>
#notes...........:none
#==============================================================================
## get raspbian image file name
pi_img=$(ls -t *.img | head -1 )
## create TAP as this user
run_user=$1
## find the host's gateway
gw=$(/sbin/ip route | awk '/default/ { print $3 }')
## find the host's IP
host_ip=$(/sbin/ifconfig eth0 | sed '/inet\ /!d;s/.*r://g;s/\ .*//g')
## give some feedback on what the script found
echo "Running on host: " $host_ip
echo "Using gateway..: " $gw
echo "Run as user....: " $run_user
echo "Using image....: " $pi_img
## set up network bridge on host
pi_if_up()
{
ip tuntap add dev tap0 mode tap user $run_user
brctl addbr br0
brctl setfd br0 0.0
brctl addif br0 eth0
brctl addif br0 tap0
ip link set dev br0 up
ip link set dev tap0 up
ip addr del $host_ip/24 dev eth0
ip addr add $host_ip/24 broadcast + dev br0
ip route add default via $gw
}
## restore network settings
pi_if_down()
{
tunctl -d tap0
ifconfig br0 down
brctl delbr br0
ip addr add $host_ip/24 dev eth0
ip route add default via $gw
}
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
else
## set up network
pi_if_up
## launch the VM
qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -serial stdio -append "root=/dev/sda2 panic=1" -hda $pi_img -net tap,vlan=0,ifname=tap0,script=no,downscript=no -net nic,vlan=0
## restore network
pi_if_down
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment