Skip to content

Instantly share code, notes, and snippets.

@armarti
Created September 28, 2020 19:16
Show Gist options
  • Save armarti/db6c4d0e614f09448e0ddb72a5e046d2 to your computer and use it in GitHub Desktop.
Save armarti/db6c4d0e614f09448e0ddb72a5e046d2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
cat <<'EOF'
This is where we get the `mknod` numbers from:
* /dev/net/tun - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/admin-guide/devices.txt#:~:text=%2Fdev%2Fnet%2Ftun
* /dev/tap0 = https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/admin-guide/devices.txt#:~:text=%2Fdev%2Ftap0
EOF
if [ ${UID:-1} -ne 0 ] && [ "x$USER" != "xroot" ]; then
echo "You must run this script as root."
exit 3
fi
nodtype=c
major=36
minor=16
_devpfx=/dev/tap
_dev=''
maxdev=15
for i in $(seq 0 $maxdev); do
_dev=$_devpfx$i
minor=$(expr $minor + $i)
if [ ! -e $_dev ]; then
declare -l _resp
read -n 1 -r -t 30 -p "Create $_dev? [y/N]: " _resp
echo
if [ x$_resp = xy ]; then
echo "Creating $_dev"
_xt="$(shopt -op xtrace)"
set -x
mknod $_dev $nodtype $major $minor
$_xt
break
else
echo "Continuing..."
fi
else
echo "$_dev already exists"
fi
done
if [ -n $_dev ]; then
if [ -e $_dev ]; then
echo "Done."
exit 0
else
echo "Already at max TAP devices."
exit 1
fi
else
echo "Something went wrong."
exit 2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment