config_store.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/bin/bash
  2. #
  3. # store config settings
  4. #
  5. CONF="/etc/speaker.conf"
  6. NET_CONF="/etc/netplan/01-netcfg.yaml"
  7. [ ! -f "/etc/speaker.conf" ] && exit 1
  8. declare -A mask_table=(
  9. ["255.255.255.255"]="32"
  10. ["255.255.255.254"]="31"
  11. ["255.255.255.252"]="30"
  12. ["255.255.255.248"]="29"
  13. ["255.255.255.240"]="28"
  14. ["255.255.255.224"]="27"
  15. ["255.255.255.192"]="26"
  16. ["255.255.255.128"]="25"
  17. ["255.255.255.0"]="24"
  18. ["255.255.254.0"]="23"
  19. ["255.255.252.0"]="22"
  20. ["255.255.248.0"]="21"
  21. ["255.255.240.0"]="20"
  22. ["255.255.224.0"]="19"
  23. ["255.255.192.0"]="18"
  24. ["255.255.128.0"]="17"
  25. ["255.255.0.0"]="16"
  26. ["255.254.0.0"]="15"
  27. ["255.252.0.0"]="14"
  28. ["255.248.0.0"]="13"
  29. ["255.240.0.0"]="12"
  30. ["255.224.0.0"]="11"
  31. ["255.192.0.0"]="10"
  32. ["255.128.0.0"]="9"
  33. ["255.0.0.0"]="8"
  34. ["254.0.0.0"]="7"
  35. ["252.0.0.0"]="6"
  36. ["248.0.0.0"]="5"
  37. ["240.0.0.0"]="4"
  38. ["224.0.0.0"]="3"
  39. ["192.0.0.0"]="2"
  40. ["128.0.0.0"]="1"
  41. ["0.0.0.0"]="0"
  42. )
  43. net_type=`sysconf ${CONF} get system ip_assign`
  44. if [ "${net_type}" = "dhcp" ]; then
  45. cat << END > ${NET_CONF}
  46. network:
  47. version: 2
  48. ethernets:
  49. eth1:
  50. dhcp4: yes
  51. addresses: []
  52. optional: yes
  53. dhcp-identifier: mac
  54. dhcp4-overrides:
  55. route-metric: 200
  56. END
  57. elif [ "${net_type}" = "static" ]; then
  58. ip=`sysconf ${CONF} get system ipaddr`
  59. mask=`sysconf ${CONF} get system netmask`
  60. gateway=`sysconf ${CONF} get system gateway`
  61. dns=`sysconf ${CONF} get system dns1`
  62. dns2=`sysconf ${CONF} get system dns2`
  63. if [[ -n ${mask_table[$mask]} ]]; then
  64. masklen=${mask_table[$mask]}
  65. else
  66. masklen=24
  67. fi
  68. if [ -n "${dns2}" ];then
  69. dns="${dns},${dns2}"
  70. fi
  71. cat << END > ${NET_CONF}
  72. network:
  73. version: 2
  74. ethernets:
  75. eth1:
  76. dhcp4: no
  77. addresses: [${ip}/${masklen}]
  78. gateway4: ${gateway}
  79. nameservers:
  80. addresses: [${dns}]
  81. optional: false
  82. END
  83. fi