GNS3 Docker Images
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

init.sh 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/sh
  2. [ $$ -eq 1 ] && exec tini -g -- sh "$0" "$@"
  3. HOSTNAME=$(hostname -s)
  4. NVRAM=${NVRAM:-64}
  5. MEM=${MEM:-256}
  6. cd /iou || exit
  7. # disable IPv6 and set MTU to 9000, count interfaces
  8. eth_ifs=0
  9. for if_eth in $(sed -n 's/^ *\(eth[0-9]*\):.*/\1/p' /proc/net/dev); do
  10. ifconfig "$if_eth" down
  11. sysctl -q -w "net.ipv6.conf.${if_eth}.disable_ipv6=1"
  12. ifconfig "$if_eth" mtu 9000 up
  13. eth_ifs=$((eth_ifs + 1))
  14. done
  15. # update /etc/hosts
  16. if ! grep -q -w -F "gns3vm" /etc/hosts; then
  17. printf '127.0.1.2\tgns3vm\n' >> /etc/hosts
  18. printf '127.0.0.127\txml.cisco.com\n' >> /etc/hosts
  19. fi
  20. # create router ID
  21. ID=$(echo "$ID" | grep -o '[0-9-]*$')
  22. [ -z "$ID" ] && ID=0x$(echo "$HOSTNAME" | md5sum | grep -o '^................')
  23. ID=$((ID % 997))
  24. [ $ID -le 0 ] && ID=$((ID + 997))
  25. nvram_file=$(printf "nvram_%05d" $ID)
  26. find nvram_* ! -name "$nvram_file" -print0 2>/dev/null | xargs -0 -r rm
  27. # create NETMAP and iouyap.ini
  28. printf '' > NETMAP
  29. printf "[default]\nbase_port = 49000\nnetmap = NETMAP\n\n" > iouyap.ini
  30. ser_ifs=$((SERIAL + 0))
  31. [ $ser_ifs -gt $eth_ifs ] && ser_ifs=$eth_ifs
  32. eth_ifs=$((eth_ifs - ser_ifs))
  33. eth_cntr=$(( (eth_ifs + 3) / 4))
  34. ser_cntr=$(( (ser_ifs + 3) / 4))
  35. if=0
  36. while [ $if -lt $eth_ifs ]; do
  37. if_iou=$((if / 4))/$((if % 4))
  38. printf "%d:%s %d:%s\n" 1000 "$if_iou" $ID "$if_iou" >> NETMAP
  39. printf "[%d:%s]\neth_dev = eth%d\n\n" 1000 "$if_iou" $if >> iouyap.ini
  40. if=$((if + 1))
  41. done
  42. if=0
  43. while [ $if -lt $ser_ifs ]; do
  44. if_iou=$((eth_cntr + (if / 4) ))/$((if % 4))
  45. printf "%d:%s %d:%s\n" 1000 "$if_iou" $ID "$if_iou" >> NETMAP
  46. printf "[%d:%s]\neth_dev = eth%d\n\n" 1000 "$if_iou" $((eth_ifs+if)) >> iouyap.ini
  47. if=$((if + 1))
  48. done
  49. # first run: replace %h by hostname in config files
  50. if [ ! -f "$nvram_file" ]; then
  51. [ -f startup-config ] && sed -i "s/%h/$HOSTNAME/g" startup-config
  52. [ -f private-config ] && sed -i "s/%h/$HOSTNAME/g" private-config
  53. fi
  54. # update NVRAM
  55. if [ -f startup-config ]; then
  56. [ -f private-config ] && private="private-config" || private=""
  57. iou_import -c "$NVRAM" "$nvram_file" startup-config $private
  58. fi
  59. # start IOU
  60. hostname gns3vm
  61. stty intr undef quit undef susp undef
  62. sig_term=
  63. trap "sig_term=1" TERM
  64. iouyap 1000 > iouyap.log 2>&1 &
  65. iou.bin -e $eth_cntr -s $ser_cntr -n "$NVRAM" -m "$MEM" $ID
  66. # export configs from NVRAM
  67. iou_export "$nvram_file" startup-config private-config
  68. [ -f private-config ] && [ "$(stat -c "%s" private-config)" -le 5 ] && \
  69. rm private-config
  70. # don't close, if IOU is not terminated by SIGTERM (docker stop)
  71. if [ -z "$sig_term" ]; then
  72. printf "\nQuit... "
  73. read -r
  74. fi