openwrt.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2021-2023 tteck
  3. # Author: tteck (tteckster)
  4. # Jon Spriggs (jontheniceguy)
  5. # License: MIT
  6. # https://github.com/tteck/Proxmox/raw/main/LICENSE
  7. # Based on work from https://i12bretro.github.io/tutorials/0405.html
  8. function header_info {
  9. clear
  10. cat <<"EOF"
  11. ____ _ __ __
  12. / __ \____ ___ ____| | / /____/ /_
  13. / / / / __ \/ _ \/ __ \ | /| / / ___/ __/
  14. / /_/ / /_/ / __/ / / / |/ |/ / / / /_
  15. \____/ .___/\___/_/ /_/|__/|__/_/ \__/
  16. /_/ W I R E L E S S F R E E D O M
  17. EOF
  18. }
  19. header_info
  20. echo -e "Loading..."
  21. GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
  22. GEN_MAC_LAN=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
  23. NEXTID=$(pvesh get /cluster/nextid)
  24. YW=$(echo "\033[33m")
  25. BL=$(echo "\033[36m")
  26. HA=$(echo "\033[1;34m")
  27. RD=$(echo "\033[01;31m")
  28. BGN=$(echo "\033[4;92m")
  29. GN=$(echo "\033[1;92m")
  30. DGN=$(echo "\033[32m")
  31. CL=$(echo "\033[m")
  32. BFR="\\r\\033[K"
  33. HOLD="-"
  34. CM="${GN}✓${CL}"
  35. CROSS="${RD}✗${CL}"
  36. set -Eeo pipefail
  37. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  38. trap cleanup EXIT
  39. function error_handler() {
  40. local exit_code="$?"
  41. local line_number="$1"
  42. local command="$2"
  43. local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
  44. echo -e "\n$error_message\n"
  45. cleanup_vmid
  46. }
  47. function cleanup_vmid() {
  48. if qm status $VMID &>/dev/null; then
  49. qm stop $VMID &>/dev/null
  50. qm destroy $VMID &>/dev/null
  51. fi
  52. }
  53. function cleanup() {
  54. popd >/dev/null
  55. rm -rf $TEMP_DIR
  56. }
  57. TEMP_DIR=$(mktemp -d)
  58. pushd $TEMP_DIR >/dev/null
  59. function send_line_to_vm() {
  60. echo -e "${DGN}Sending line: ${YW}$1${CL}"
  61. for ((i = 0; i < ${#1}; i++)); do
  62. character=${1:i:1}
  63. case $character in
  64. " ") character="spc" ;;
  65. "-") character="minus" ;;
  66. "=") character="equal" ;;
  67. ",") character="comma" ;;
  68. ".") character="dot" ;;
  69. "/") character="slash" ;;
  70. "'") character="apostrophe" ;;
  71. ";") character="semicolon" ;;
  72. '\') character="backslash" ;;
  73. '`') character="grave_accent" ;;
  74. "[") character="bracket_left" ;;
  75. "]") character="bracket_right" ;;
  76. "_") character="shift-minus" ;;
  77. "+") character="shift-equal" ;;
  78. "?") character="shift-slash" ;;
  79. "<") character="shift-comma" ;;
  80. ">") character="shift-dot" ;;
  81. '"') character="shift-apostrophe" ;;
  82. ":") character="shift-semicolon" ;;
  83. "|") character="shift-backslash" ;;
  84. "~") character="shift-grave_accent" ;;
  85. "{") character="shift-bracket_left" ;;
  86. "}") character="shift-bracket_right" ;;
  87. "A") character="shift-a" ;;
  88. "B") character="shift-b" ;;
  89. "C") character="shift-c" ;;
  90. "D") character="shift-d" ;;
  91. "E") character="shift-e" ;;
  92. "F") character="shift-f" ;;
  93. "G") character="shift-g" ;;
  94. "H") character="shift-h" ;;
  95. "I") character="shift-i" ;;
  96. "J") character="shift-j" ;;
  97. "K") character="shift-k" ;;
  98. "L") character="shift-l" ;;
  99. "M") character="shift-m" ;;
  100. "N") character="shift-n" ;;
  101. "O") character="shift-o" ;;
  102. "P") character="shift-p" ;;
  103. "Q") character="shift-q" ;;
  104. "R") character="shift-r" ;;
  105. "S") character="shift-s" ;;
  106. "T") character="shift-t" ;;
  107. "U") character="shift-u" ;;
  108. "V") character="shift-v" ;;
  109. "W") character="shift-w" ;;
  110. "X") character="shift=x" ;;
  111. "Y") character="shift-y" ;;
  112. "Z") character="shift-z" ;;
  113. "!") character="shift-1" ;;
  114. "@") character="shift-2" ;;
  115. "#") character="shift-3" ;;
  116. '$') character="shift-4" ;;
  117. "%") character="shift-5" ;;
  118. "^") character="shift-6" ;;
  119. "&") character="shift-7" ;;
  120. "*") character="shift-8" ;;
  121. "(") character="shift-9" ;;
  122. ")") character="shift-0" ;;
  123. esac
  124. qm sendkey $VMID "$character"
  125. done
  126. qm sendkey $VMID ret
  127. }
  128. TEMP_DIR=$(mktemp -d)
  129. pushd $TEMP_DIR >/dev/null
  130. if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "OpenWrt VM" --yesno "This will create a New OpenWrt VM. Proceed?" 10 58); then
  131. :
  132. else
  133. header_info && echo -e "⚠ User exited script \n" && exit
  134. fi
  135. function msg_info() {
  136. local msg="$1"
  137. echo -ne " ${HOLD} ${YW}${msg}..."
  138. }
  139. function msg_ok() {
  140. local msg="$1"
  141. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  142. }
  143. function msg_error() {
  144. local msg="$1"
  145. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  146. }
  147. function pve_check() {
  148. if ! pveversion | grep -Eq "pve-manager/(7\.[2-9]|8\.[0-9])"; then
  149. echo -e "${CROSS} This version of Proxmox Virtual Environment is not supported"
  150. echo -e "Requires PVE Version 7.2 or higher"
  151. echo -e "Exiting..."
  152. sleep 2
  153. exit
  154. fi
  155. }
  156. function arch_check() {
  157. if [ "$(dpkg --print-architecture)" != "amd64" ]; then
  158. echo -e "\n ${CROSS} This script will not work with PiMox! \n"
  159. echo -e "Exiting..."
  160. sleep 2
  161. exit
  162. fi
  163. }
  164. function ssh_check() {
  165. if command -v pveversion >/dev/null 2>&1; then
  166. if [ -n "${SSH_CLIENT:+x}" ]; then
  167. if whiptail --backtitle "Proxmox VE Helper Scripts" --defaultno --title "SSH DETECTED" --yesno "It's suggested to use the Proxmox shell instead of SSH, since SSH can create issues while gathering variables. Would you like to proceed with using SSH?" 10 62; then
  168. echo "you've been warned"
  169. else
  170. clear
  171. exit
  172. fi
  173. fi
  174. fi
  175. }
  176. function exit-script() {
  177. clear
  178. echo -e "⚠ User exited script \n"
  179. exit
  180. }
  181. function default_settings() {
  182. VMID=$NEXTID
  183. HN=openwrt
  184. CORE_COUNT="1"
  185. RAM_SIZE="256"
  186. BRG="vmbr0"
  187. VLAN=""
  188. MAC=$GEN_MAC
  189. LAN_MAC=$GEN_MAC_LAN
  190. LAN_BRG="vmbr0"
  191. LAN_IP_ADDR="192.168.1.1"
  192. LAN_NETMASK="255.255.255.0"
  193. LAN_VLAN=",tag=999"
  194. MTU=""
  195. START_VM="yes"
  196. echo -e "${DGN}Using Virtual Machine ID: ${BGN}${VMID}${CL}"
  197. echo -e "${DGN}Using Hostname: ${BGN}${HN}${CL}"
  198. echo -e "${DGN}Allocated Cores: ${BGN}${CORE_COUNT}${CL}"
  199. echo -e "${DGN}Allocated RAM: ${BGN}${RAM_SIZE}${CL}"
  200. echo -e "${DGN}Using WAN Bridge: ${BGN}${BRG}${CL}"
  201. echo -e "${DGN}Using WAN VLAN: ${BGN}Default${CL}"
  202. echo -e "${DGN}Using WAN MAC Address: ${BGN}${MAC}${CL}"
  203. echo -e "${DGN}Using LAN MAC Address: ${BGN}${LAN_MAC}${CL}"
  204. echo -e "${DGN}Using LAN Bridge: ${BGN}${LAN_BRG}${CL}"
  205. echo -e "${DGN}Using LAN VLAN: ${BGN}999${CL}"
  206. echo -e "${DGN}Using LAN IP Address: ${BGN}${LAN_IP_ADDR}${CL}"
  207. echo -e "${DGN}Using LAN NETMASK: ${BGN}${LAN_NETMASK}${CL}"
  208. echo -e "${DGN}Using Interface MTU Size: ${BGN}Default${CL}"
  209. echo -e "${DGN}Start VM when completed: ${BGN}yes${CL}"
  210. echo -e "${BL}Creating a OpenWRT VM using the above default settings${CL}"
  211. }
  212. function advanced_settings() {
  213. while true; do
  214. if VMID=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Virtual Machine ID" 8 58 $NEXTID --title "VIRTUAL MACHINE ID" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  215. if [ -z "$VMID" ]; then
  216. VMID="$NEXTID"
  217. fi
  218. if pct status "$VMID" &>/dev/null || qm status "$VMID" &>/dev/null; then
  219. echo -e "${CROSS}${RD} ID $VMID is already in use${CL}"
  220. sleep 2
  221. continue
  222. fi
  223. echo -e "${DGN}Virtual Machine ID: ${BGN}$VMID${CL}"
  224. break
  225. else
  226. exit-script
  227. fi
  228. done
  229. if VM_NAME=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Hostname" 8 58 openwrt --title "HOSTNAME" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  230. if [ -z $VM_NAME ]; then
  231. HN="openwrt"
  232. else
  233. HN=$(echo ${VM_NAME,,} | tr -d ' ')
  234. fi
  235. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  236. else
  237. exit-script
  238. fi
  239. if CORE_COUNT=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate CPU Cores" 8 58 1 --title "CORE COUNT" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  240. if [ -z $CORE_COUNT ]; then
  241. CORE_COUNT="1"
  242. fi
  243. echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"
  244. else
  245. exit-script
  246. fi
  247. if RAM_SIZE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate RAM in MiB" 8 58 256 --title "RAM" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  248. if [ -z $RAM_SIZE ]; then
  249. RAM_SIZE="256"
  250. fi
  251. echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"
  252. else
  253. exit-script
  254. fi
  255. if BRG=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a WAN Bridge" 8 58 vmbr0 --title "WAN BRIDGE" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  256. if [ -z $BRG ]; then
  257. BRG="vmbr0"
  258. fi
  259. echo -e "${DGN}Using WAN Bridge: ${BGN}$BRG${CL}"
  260. else
  261. exit-script
  262. fi
  263. if LAN_BRG=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a LAN Bridge" 8 58 vmbr0 --title "LAN BRIDGE" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  264. if [ -z $LAN_BRG ]; then
  265. LAN_BRG="vmbr0"
  266. fi
  267. echo -e "${DGN}Using LAN Bridge: ${BGN}$LAN_BRG${CL}"
  268. else
  269. exit-script
  270. fi
  271. if LAN_IP_ADDR=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a router IP" 8 58 $LAN_IP_ADDR --title "LAN IP ADDRESS" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  272. if [ -z $LAN_IP_ADDR ]; then
  273. LAN_IP_ADDR="192.168.1.1"
  274. fi
  275. echo -e "${DGN}Using LAN IP ADDRESS: ${BGN}$LAN_IP_ADDR${CL}"
  276. else
  277. exit-script
  278. fi
  279. if LAN_NETMASK=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a router netmmask" 8 58 $LAN_NETMASK --title "LAN NETMASK" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  280. if [ -z $LAN_NETMASK ]; then
  281. LAN_NETMASK="255.255.255.0"
  282. fi
  283. echo -e "${DGN}Using LAN NETMASK: ${BGN}$LAN_NETMASK${CL}"
  284. else
  285. exit-script
  286. fi
  287. if MAC1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a WAN MAC Address" 8 58 $GEN_MAC --title "WAN MAC ADDRESS" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  288. if [ -z $MAC1 ]; then
  289. MAC="$GEN_MAC"
  290. else
  291. MAC="$MAC1"
  292. fi
  293. echo -e "${DGN}Using WAN MAC Address: ${BGN}$MAC${CL}"
  294. else
  295. exit-script
  296. fi
  297. if MAC2=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a LAN MAC Address" 8 58 $GEN_MAC_LAN --title "LAN MAC ADDRESS" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  298. if [ -z $MAC2 ]; then
  299. LAN_MAC="$GEN_MAC_LAN"
  300. else
  301. LAN_MAC="$MAC2"
  302. fi
  303. echo -e "${DGN}Using LAN MAC Address: ${BGN}$LAN_MAC${CL}"
  304. else
  305. exit-script
  306. fi
  307. if VLAN1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a WAN Vlan (leave blank for default)" 8 58 --title "WAN VLAN" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  308. if [ -z $VLAN1 ]; then
  309. VLAN1="Default"
  310. VLAN=""
  311. else
  312. VLAN=",tag=$VLAN1"
  313. fi
  314. echo -e "${DGN}Using WAN Vlan: ${BGN}$VLAN1${CL}"
  315. else
  316. exit-script
  317. fi
  318. if VLAN2=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a LAN Vlan" 8 58 999 --title "LAN VLAN" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  319. if [ -z $VLAN2 ]; then
  320. VLAN2="999"
  321. LAN_VLAN=",tag=$VLAN2"
  322. else
  323. LAN_VLAN=",tag=$VLAN2"
  324. fi
  325. echo -e "${DGN}Using LAN Vlan: ${BGN}$VLAN2${CL}"
  326. else
  327. exit-script
  328. fi
  329. if MTU1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Interface MTU Size (leave blank for default)" 8 58 --title "MTU SIZE" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  330. if [ -z $MTU1 ]; then
  331. MTU1="Default"
  332. MTU=""
  333. else
  334. MTU=",mtu=$MTU1"
  335. fi
  336. echo -e "${DGN}Using Interface MTU Size: ${BGN}$MTU1${CL}"
  337. else
  338. exit-script
  339. fi
  340. if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "START VIRTUAL MACHINE" --yesno "Start VM when completed?" 10 58); then
  341. START_VM="yes"
  342. else
  343. START_VM="no"
  344. fi
  345. echo -e "${DGN}Start VM when completed: ${BGN}$START_VM${CL}"
  346. if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "ADVANCED SETTINGS COMPLETE" --yesno "Ready to create OpenWrt VM?" --no-button Do-Over 10 58); then
  347. echo -e "${RD}Creating a OpenWrt VM using the above advanced settings${CL}"
  348. else
  349. header_info
  350. echo -e "${RD}Using Advanced Settings${CL}"
  351. advanced_settings
  352. fi
  353. }
  354. function start_script() {
  355. if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "SETTINGS" --yesno "Use Default Settings?" --no-button Advanced 10 58); then
  356. header_info
  357. echo -e "${BL}Using Default Settings${CL}"
  358. default_settings
  359. else
  360. header_info
  361. echo -e "${RD}Using Advanced Settings${CL}"
  362. advanced_settings
  363. fi
  364. }
  365. arch_check
  366. pve_check
  367. ssh_check
  368. start_script
  369. msg_info "Validating Storage"
  370. while read -r line; do
  371. TAG=$(echo $line | awk '{print $1}')
  372. TYPE=$(echo $line | awk '{printf "%-10s", $2}')
  373. FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
  374. ITEM=" Type: $TYPE Free: $FREE "
  375. OFFSET=2
  376. if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
  377. MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
  378. fi
  379. STORAGE_MENU+=("$TAG" "$ITEM" "OFF")
  380. done < <(pvesm status -content images | awk 'NR>1')
  381. VALID=$(pvesm status -content images | awk 'NR>1')
  382. if [ -z "$VALID" ]; then
  383. echo -e "\n${RD}⚠ Unable to detect a valid storage location.${CL}"
  384. echo -e "Exiting..."
  385. exit
  386. elif [ $((${#STORAGE_MENU[@]} / 3)) -eq 1 ]; then
  387. STORAGE=${STORAGE_MENU[0]}
  388. else
  389. while [ -z "${STORAGE:+x}" ]; do
  390. STORAGE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Storage Pools" --radiolist \
  391. "Which storage pool you would like to use for the OpenWrt VM?\n\n" \
  392. 16 $(($MSG_MAX_LENGTH + 23)) 6 \
  393. "${STORAGE_MENU[@]}" 3>&1 1>&2 2>&3) || exit
  394. done
  395. fi
  396. msg_ok "Using ${CL}${BL}$STORAGE${CL} ${GN}for Storage Location."
  397. msg_ok "Virtual Machine ID is ${CL}${BL}$VMID${CL}."
  398. msg_info "Getting URL for OpenWrt Disk Image"
  399. response=$(curl -s https://openwrt.org)
  400. stableversion=$(echo "$response" | sed -n 's/.*Current stable release - OpenWrt \([0-9.]\+\).*/\1/p')
  401. URL="https://downloads.openwrt.org/releases/$stableversion/targets/x86/64/openwrt-$stableversion-x86-64-generic-ext4-combined.img.gz"
  402. sleep 2
  403. msg_ok "${CL}${BL}${URL}${CL}"
  404. wget -q --show-progress $URL
  405. echo -en "\e[1A\e[0K"
  406. FILE=$(basename $URL)
  407. msg_ok "Downloaded ${CL}${BL}$FILE${CL}"
  408. gunzip -f $FILE >/dev/null 2>/dev/null || true
  409. NEWFILE="${FILE%.*}"
  410. FILE="$NEWFILE"
  411. mv $FILE ${FILE%.*}
  412. qemu-img resize -f raw ${FILE%.*} 512M >/dev/null 2>/dev/null
  413. msg_ok "Extracted & Resized OpenWrt Disk Image ${CL}${BL}$FILE${CL}"
  414. STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
  415. case $STORAGE_TYPE in
  416. nfs | dir)
  417. DISK_EXT=".qcow2"
  418. DISK_REF="$VMID/"
  419. DISK_IMPORT="-format qcow2"
  420. ;;
  421. btrfs)
  422. DISK_EXT=".raw"
  423. DISK_REF="$VMID/"
  424. DISK_IMPORT="-format raw"
  425. ;;
  426. esac
  427. for i in {0,1}; do
  428. disk="DISK$i"
  429. eval DISK${i}=vm-${VMID}-disk-${i}${DISK_EXT:-}
  430. eval DISK${i}_REF=${STORAGE}:${DISK_REF:-}${!disk}
  431. done
  432. msg_info "Creating OpenWrt VM"
  433. qm create $VMID -cores $CORE_COUNT -memory $RAM_SIZE -name $HN \
  434. -onboot 1 -ostype l26 -scsihw virtio-scsi-pci --tablet 0
  435. pvesm alloc $STORAGE $VMID $DISK0 4M 1>&/dev/null
  436. qm importdisk $VMID ${FILE%.*} $STORAGE ${DISK_IMPORT:-} 1>&/dev/null
  437. qm set $VMID \
  438. -efidisk0 ${DISK0_REF},efitype=4m,size=4M \
  439. -scsi0 ${DISK1_REF},size=512M \
  440. -boot order=scsi0 \
  441. -description "<div align='center'><a href='https://Helper-Scripts.com'><img src='https://raw.githubusercontent.com/tteck/Proxmox/main/misc/images/logo-81x112.png'/></a>
  442. # OpenWRT
  443. <a href='https://ko-fi.com/D1D7EP4GF'><img src='https://img.shields.io/badge/&#x2615;-Buy me a coffee-blue' /></a>
  444. </div>" >/dev/null
  445. msg_ok "Created OpenWrt VM ${CL}${BL}(${HN})"
  446. msg_info "OpenWrt is being started in order to configure the network interfaces."
  447. qm start $VMID
  448. sleep 15
  449. msg_ok "Network interfaces are being configured as OpenWrt initiates."
  450. send_line_to_vm ""
  451. send_line_to_vm "uci delete network.@device[0]"
  452. send_line_to_vm "uci set network.wan=interface"
  453. send_line_to_vm "uci set network.wan.device=eth1"
  454. send_line_to_vm "uci set network.wan.proto=dhcp"
  455. send_line_to_vm "uci delete network.lan"
  456. send_line_to_vm "uci set network.lan=interface"
  457. send_line_to_vm "uci set network.lan.device=eth0"
  458. send_line_to_vm "uci set network.lan.proto=static"
  459. send_line_to_vm "uci set network.lan.ipaddr=${LAN_IP_ADDR}"
  460. send_line_to_vm "uci set network.lan.netmask=${LAN_NETMASK}"
  461. send_line_to_vm "uci set firewall.@zone[1].input='ACCEPT'"
  462. send_line_to_vm "uci set firewall.@zone[1].forward='ACCEPT'"
  463. send_line_to_vm "uci commit"
  464. send_line_to_vm "halt"
  465. msg_ok "Network interfaces have been successfully configured."
  466. until qm status $VMID | grep -q "stopped"; do
  467. sleep 2
  468. done
  469. msg_info "Bridge interfaces are being added."
  470. qm set $VMID \
  471. -net0 virtio,bridge=${LAN_BRG},macaddr=${LAN_MAC}${LAN_VLAN}${MTU} \
  472. -net1 virtio,bridge=${BRG},macaddr=${MAC}${VLAN}${MTU} >/dev/null 2>/dev/null
  473. msg_ok "Bridge interfaces have been successfully added."
  474. if [ "$START_VM" == "yes" ]; then
  475. msg_info "Starting OpenWrt VM"
  476. qm start $VMID
  477. msg_ok "Started OpenWrt VM"
  478. fi
  479. VLAN_FINISH=""
  480. if [ "$VLAN" == "" ] && [ "$VLAN2" != "999" ]; then
  481. VLAN_FINISH=" Please remember to adjust the VLAN tags to suit your network."
  482. fi
  483. msg_ok "Completed Successfully!\n${VLAN_FINISH}"