Guest User

setup-tap.sh

a guest
Dec 13th, 2020
89
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /bin/bash
  2.  
  3. set -e
  4.  
  5. # Deny non-root run
  6. if [ "$EUID" -ne 0 ]; then
  7.   echo "ERROR: Please run as root"
  8.   exit
  9. fi
  10.  
  11. # Parse command line arguments
  12. while [[ "$#" -gt 0 ]]; do
  13.   case $1 in
  14.     --bridge=) LIBVIRT_BRIDGE="$2"; shift ;;
  15.     --tap=) QEMU_TAP="$2"; shift ;;
  16.     *) echo "Unknown parameter passed: $1"; exit 1 ;;
  17.   esac
  18.   shift
  19. done
  20.  
  21. if [ -z "${LIBVIRT_BRIDGE}" ]; then
  22.   LIBVIRT_BRIDGE="virbr0"
  23. fi
  24.  
  25. if [ -z "${QEMU_TAP}" ]; then
  26.   QEMU_TAP="qtap"
  27. fi
  28.  
  29. # Avoid configuring if TAP already exists
  30. if [ `ip --oneline link list | grep ${QEMU_TAP} | wc -l` -ne 0 ]; then
  31.   echo "Tap '${QEMU_TAP}' already exists. Exiting."
  32.   exit
  33. fi
  34.  
  35. # Create TAP, turn it on, attach to libvirt managed bridge
  36. ip tuntap add dev ${QEMU_TAP} mode tap
  37. ip link set ${QEMU_TAP} up
  38. ip link set dev ${QEMU_TAP} master ${LIBVIRT_BRIDGE}
RAW Paste Data