mikrotik-routeros.sh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2021-2023 tteck
  3. # Author: tteck (tteckster)
  4. # License: MIT
  5. # https://github.com/tteck/Proxmox/raw/main/LICENSE
  6. function header_info {
  7. cat <<"EOF"
  8. __ ____ __ __ _ __ ____ __ ____ _____
  9. / |/ (_) /___________ / /_(_) /__ / __ \____ __ __/ /____ _____/ __ \/ ___/
  10. / /|_/ / / //_/ ___/ __ \/ __/ / //_/ / /_/ / __ \/ / / / __/ _ \/ ___/ / / /\__ \
  11. / / / / / < / / / /_/ / /_/ / < / _ _/ /_/ / /_/ / /_/ __/ / / /_/ /___/ /
  12. /_/ /_/_/_/|_/_/ \____/\__/_/_/|_| /_/ |_|\____/\__,_/\__/\___/_/ \____//____/
  13. EOF
  14. }
  15. clear
  16. header_info
  17. echo -e "Loading..."
  18. GEN_MAC=$(echo '00 60 2f'$(od -An -N3 -t xC /dev/urandom) | sed -e 's/ /:/g' | tr '[:lower:]' '[:upper:]')
  19. NEXTID=$(pvesh get /cluster/nextid)
  20. YW=$(echo "\033[33m")
  21. BL=$(echo "\033[36m")
  22. HA=$(echo "\033[1;34m")
  23. RD=$(echo "\033[01;31m")
  24. BGN=$(echo "\033[4;92m")
  25. GN=$(echo "\033[1;92m")
  26. DGN=$(echo "\033[32m")
  27. CL=$(echo "\033[m")
  28. BFR="\\r\\033[K"
  29. HOLD="-"
  30. CM="${GN}✓${CL}"
  31. set -o errexit
  32. set -o errtrace
  33. set -o nounset
  34. set -o pipefail
  35. shopt -s expand_aliases
  36. alias die='EXIT=$? LINE=$LINENO error_exit'
  37. trap die ERR
  38. trap cleanup EXIT
  39. function error_exit() {
  40. trap - ERR
  41. local reason="Unknown failure occurred."
  42. local msg="${1:-$reason}"
  43. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  44. echo -e "$flag $msg" 1>&2
  45. [ ! -z ${VMID-} ] && cleanup_vmid
  46. exit $EXIT
  47. }
  48. function cleanup_vmid() {
  49. if $(qm status $VMID &>/dev/null); then
  50. if [ "$(qm status $VMID | awk '{print $2}')" == "running" ]; then
  51. qm stop $VMID
  52. fi
  53. qm destroy $VMID
  54. fi
  55. }
  56. function cleanup() {
  57. popd >/dev/null
  58. rm -rf $TEMP_DIR
  59. }
  60. TEMP_DIR=$(mktemp -d)
  61. pushd $TEMP_DIR >/dev/null
  62. if ! pveversion | grep -Eq "pve-manager/(7\.[2-9]|8\.[0-9])"; then
  63. echo "⚠ This version of Proxmox Virtual Environment is not supported"
  64. echo -e "Requires PVE Version 7.2 or higher"
  65. echo "Exiting..."
  66. sleep 3
  67. exit
  68. fi
  69. if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "Mikrotik RouterOS VM" --yesno "This will create a New Mikrotik RouterOS VM. Proceed?" 10 58); then
  70. echo "User selected Yes"
  71. else
  72. clear
  73. echo -e "⚠ User exited script \n"
  74. exit
  75. fi
  76. function msg_info() {
  77. local msg="$1"
  78. echo -ne " ${HOLD} ${YW}${msg}..."
  79. }
  80. function msg_ok() {
  81. local msg="$1"
  82. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  83. }
  84. function default_settings() {
  85. echo -e "${DGN}Using Virtual Machine ID: ${BGN}$NEXTID${CL}"
  86. VMID=$NEXTID
  87. echo -e "${DGN}Using Hostname: ${BGN}mikrotik-routeros${CL}"
  88. HN=mikrotik-routeros
  89. echo -e "${DGN}Allocated Cores: ${BGN}1${CL}"
  90. CORE_COUNT="1"
  91. echo -e "${DGN}Allocated RAM: ${BGN}1024${CL}"
  92. RAM_SIZE="1024"
  93. echo -e "${DGN}Using Bridge: ${BGN}vmbr0${CL}"
  94. BRG="vmbr0"
  95. echo -e "${DGN}Using MAC Address: ${BGN}$GEN_MAC${CL}"
  96. MAC=$GEN_MAC
  97. echo -e "${DGN}Using VLAN: ${BGN}Default${CL}"
  98. VLAN=""
  99. echo -e "${DGN}Using Interface MTU Size: ${BGN}Default${CL}"
  100. MTU=""
  101. echo -e "${DGN}Start VM when completed: ${BGN}no${CL}"
  102. START_VM="no"
  103. echo -e "${BL}Creating a Mikrotik RouterOS VM using the above default settings${CL}"
  104. }
  105. function advanced_settings() {
  106. VMID=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Virtual Machine ID" 8 58 $NEXTID --title "VIRTUAL MACHINE ID" 3>&1 1>&2 2>&3)
  107. exitstatus=$?
  108. if [ $exitstatus = 0 ]; then
  109. echo -e "${DGN}Using Virtual Machine ID: ${BGN}$VMID${CL}"
  110. else
  111. exit
  112. fi
  113. VM_NAME=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Hostname" 8 58 Mikrotik-RouterOS --title "HOSTNAME" 3>&1 1>&2 2>&3)
  114. exitstatus=$?
  115. if [ $exitstatus = 0 ]; then
  116. HN=$(echo ${VM_NAME,,} | tr -d ' ')
  117. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  118. else
  119. exit
  120. fi
  121. CORE_COUNT=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate CPU Cores" 8 58 1 --title "CORE COUNT" 3>&1 1>&2 2>&3)
  122. exitstatus=$?
  123. if [ $exitstatus = 0 ]; then
  124. echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"
  125. else
  126. exit
  127. fi
  128. RAM_SIZE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate RAM in MiB" 8 58 1024 --title "RAM" 3>&1 1>&2 2>&3)
  129. exitstatus=$?
  130. if [ $exitstatus = 0 ]; then
  131. echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"
  132. else
  133. exit
  134. fi
  135. BRG=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a Bridge" 8 58 vmbr0 --title "BRIDGE" 3>&1 1>&2 2>&3)
  136. exitstatus=$?
  137. if [ $exitstatus = 0 ]; then
  138. echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"
  139. else
  140. exit
  141. fi
  142. MAC1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a MAC Address" 8 58 $GEN_MAC --title "MAC ADDRESS" 3>&1 1>&2 2>&3)
  143. exitstatus=$?
  144. if [ $exitstatus = 0 ]; then
  145. MAC="$MAC1"
  146. echo -e "${DGN}Using MAC Address: ${BGN}$MAC1${CL}"
  147. else
  148. exit
  149. fi
  150. VLAN1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a Vlan(leave blank for default)" 8 58 --title "VLAN" 3>&1 1>&2 2>&3)
  151. exitstatus=$?
  152. if [ $exitstatus = 0 ]; then
  153. if [ -z $VLAN1 ]; then
  154. VLAN1="Default" VLAN=""
  155. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  156. else
  157. VLAN=",tag=$VLAN1"
  158. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  159. fi
  160. fi
  161. 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)
  162. exitstatus=$?
  163. if [ $exitstatus = 0 ]; then
  164. if [ -z $MTU1 ]; then
  165. MTU1="Default" MTU=""
  166. echo -e "${DGN}Using Interface MTU Size: ${BGN}$MTU1${CL}"
  167. else
  168. MTU=",mtu=$MTU1"
  169. echo -e "${DGN}Using Interface MTU Size: ${BGN}$MTU1${CL}"
  170. fi
  171. fi
  172. if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "START VIRTUAL MACHINE" --yesno "Start Mikrotik RouterOS VM when completed?" 10 58); then
  173. echo -e "${DGN}Start Mikrotik RouterOS VM when completed: ${BGN}yes${CL}"
  174. START_VM="yes"
  175. else
  176. echo -e "${DGN}Start Mikrotik RouterOS VM when completed: ${BGN}no${CL}"
  177. START_VM="no"
  178. fi
  179. if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "ADVANCED SETTINGS COMPLETE" --yesno "Ready to create Mikrotik RouterOS VM?" 10 58); then
  180. echo -e "${RD}Creating Mikrotik RouterOS VM using the above advanced settings${CL}"
  181. else
  182. clear
  183. header_info
  184. echo -e "${RD}Using Advanced Settings${CL}"
  185. advanced_settings
  186. fi
  187. }
  188. function start_script() {
  189. if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "SETTINGS" --yesno "Use Default Settings?" 10 58); then
  190. clear
  191. header_info
  192. echo -e "${BL}Using Default Settings${CL}"
  193. default_settings
  194. else
  195. clear
  196. header_info
  197. echo -e "${RD}Using Advanced Settings${CL}"
  198. advanced_settings
  199. fi
  200. }
  201. start_script
  202. msg_info "Validating Storage"
  203. while read -r line; do
  204. TAG=$(echo $line | awk '{print $1}')
  205. TYPE=$(echo $line | awk '{printf "%-10s", $2}')
  206. FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
  207. ITEM=" Type: $TYPE Free: $FREE "
  208. OFFSET=2
  209. if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
  210. MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
  211. fi
  212. STORAGE_MENU+=("$TAG" "$ITEM" "OFF")
  213. done < <(pvesm status -content images | awk 'NR>1')
  214. VALID=$(pvesm status -content images | awk 'NR>1')
  215. if [ -z "$VALID" ]; then
  216. echo -e "\n${RD}⚠ Unable to detect a valid storage location.${CL}"
  217. echo -e "Exiting..."
  218. exit
  219. elif [ $((${#STORAGE_MENU[@]} / 3)) -eq 1 ]; then
  220. STORAGE=${STORAGE_MENU[0]}
  221. else
  222. while [ -z "${STORAGE:+x}" ]; do
  223. STORAGE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Storage Pools" --radiolist \
  224. "Which storage pool you would like to use for the Mikrotik RouterOS VM?\n\n" \
  225. 16 $(($MSG_MAX_LENGTH + 23)) 6 \
  226. "${STORAGE_MENU[@]}" 3>&1 1>&2 2>&3) || exit
  227. done
  228. fi
  229. msg_ok "Using ${CL}${BL}$STORAGE${CL} ${GN}for Storage Location."
  230. msg_ok "Virtual Machine ID is ${CL}${BL}$VMID${CL}."
  231. msg_info "Getting URL for Mikrotik RouterOS Disk Image"
  232. URL=https://download.mikrotik.com/routeros/7.11.2/install-image-7.11.2.zip
  233. sleep 2
  234. msg_ok "${CL}${BL}${URL}${CL}"
  235. wget -q --show-progress $URL
  236. echo -en "\e[1A\e[0K"
  237. FILE=$(basename $URL)
  238. msg_ok "Downloaded ${CL}${BL}$FILE${CL}"
  239. msg_info "Extracting Mikrotik RouterOS Disk Image"
  240. gunzip -f -S .zip $FILE
  241. STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
  242. case $STORAGE_TYPE in
  243. nfs | dir)
  244. DISK_EXT=".qcow2"
  245. DISK_REF="$VMID/"
  246. DISK_IMPORT="-format qcow2"
  247. ;;
  248. btrfs)
  249. DISK_EXT=".raw"
  250. DISK_REF="$VMID/"
  251. DISK_FORMAT="subvol"
  252. DISK_IMPORT="-format raw"
  253. ;;
  254. esac
  255. for i in {0,1}; do
  256. disk="DISK$i"
  257. eval DISK${i}=vm-${VMID}-disk-${i}${DISK_EXT:-}
  258. eval DISK${i}_REF=${STORAGE}:${DISK_REF:-}${!disk}
  259. done
  260. msg_ok "Extracted Mikrotik RouterOS Disk Image"
  261. msg_info "Creating Mikrotik RouterOS VM"
  262. qm create $VMID -bios ovmf -cores $CORE_COUNT -memory $RAM_SIZE -name $HN -net0 virtio,bridge=$BRG,macaddr=$MAC$VLAN$MTU \
  263. -onboot 1 -ostype l26 -scsihw virtio-scsi-pci
  264. pvesm alloc $STORAGE $VMID $DISK0 4M 1>&/dev/null
  265. qm importdisk $VMID ${FILE%.*} $STORAGE ${DISK_IMPORT:-} 1>&/dev/null
  266. qm set $VMID \
  267. -efidisk0 ${DISK0_REF},efitype=4m,size=4M \
  268. -scsi0 ${DISK1_REF},size=2G \
  269. -boot order=scsi0 \
  270. -description "# Mikrotik RouterOS VM
  271. ### https://github.com/tteck/Proxmox
  272. [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/D1D7EP4GF)" >/dev/null
  273. msg_ok "Mikrotik RouterOS VM ${CL}${BL}(${HN})"
  274. if [ "$START_VM" == "yes" ]; then
  275. msg_info "Starting Mikrotik RouterOS VM"
  276. qm start $VMID
  277. msg_ok "Started Mikrotik RouterOS VM"
  278. fi
  279. msg_ok "Completed Successfully!\n"