vm-start/set-tap.sh

133 lines
3.5 KiB
Bash
Executable file

#!/usr/bin/bash
# Set global variables
Usage="
set-tap.sh [PARAMETTER] [OPTION]
set up a bridge and a tap interface. Use either doas or sudo to get the root access. Please make sure that you account have access to theses commands.
This script will use either doas or sudo to access the root user and do some temporary modifications. You will be able to see some with 'ip a' command
Please do your best to not make any mistakes in the options, or else you can be obliged to reboot your computer.
PARAMETTER available
-h, --help Print this help message and quit
-v, --version Print the version of this program and quit
OPTION available
--add-br-id=<bridge-id> Set the name for the new bridge interface, usually it is br0, br1, etc
--rm-br-id=<bridge-id> Remove a bridge interface. Be carefull, it do no verification wheither bridge-id is a real interface or a bridge interface.
--addr=<address> Set an external address on the bridge. The external is the one accessible from the host.
--replace=<interface> In case you want to replace the main connexion of your computer so that only the vm will be able to reach the external network, replace interface by the interface you want to replace, and make sure that the --addr is set to one of the ip on the interace. Please not that it will be possible for your host to lost all connection to the external internet with this parametter.
"
Version="1.0"
add_bridge() {
echo "add bridge interface ${bridge}"
${rooter} ip link add ${bridge} type bridge
#echo "no interface to link with bridge ${bridge}"
#$rooter ip link set dev wlan0 master br0
echo "authorise qemu to use as interface ${bridge} as bridge"
if cat /etc/qemu/bridge.conf | grep -q "${bridge}"
then
echo "pass, bridge already authorised"
else
echo "allow ${bridge}" | doas tee -a /etc/qemu/bridge.conf
echo "entry added to /etc/qemu/bridge.conf"
fi
echo "activate bridge ${bridge}"
${rooter} ip link set dev ${bridge} up
}
rm_bridge() {
echo "rm bridge interface ${bridge}"
${rooter} ip link del ${bridge}
}
# detect the parametters
for i in $@; do
case "$i" in
"-h" | "--help")
echo "$Usage"
exit 0
;;
"-v" | "--version")
echo "$(basename $0) $Version"
exit 0
;;
esac
done
# check if sudo is installed
if which doas &> /dev/null
then
echo "use doas"
rooter=$(which doas)
elif which sudo &> /dev/null
then
echo "use sudo"
rooter=$(which sudo)
else
echo "no command to give access to root is given. Abandon" >&2
exit
fi
# detect the options
for i in $@; do
case "${i%=*}" in
"--add-br-id")
bridge="${i#*=}"
echo "${bridge}"
add_bridge
;;
"--rm-br-id")
bridge="${i#*=}"
echo "${bridge}"
rm_bridge
exit 0
;;
"--addr")
address="${i#*=}"
echo "${address}"
;;
"--replace")
echo "--replace not yet supported, program stopped." >&2
exit 1
;;
*)
echo "parametter ${i%=*} not recognised" >&2
exit 1
;;
esac
done
if [ "${bridge}" == "" ]
then
echo "Error, --bridge have to be specified to make the bridge" >&2
exit
fi
if [ ! "${address}" == "" ]
then
${rooter} ip a add dev ${bridge} ${address}
fi