Skip to content

Commit dffb291

Browse files
authored
OpenWrt: Refactor and increase root-partition (#7653)
* move openwrt to correct naming * Add disk size configuration for OpenWrt VM Introduces a DISK_SIZE variable to allow setting disk size for OpenWrt VM in both default and advanced settings. Updates VM creation logic to use the specified disk size and improves output formatting for clarity. * Update openwrt-vm.sh * Update openwrt-vm.sh * Update openwrt-vm.sh * Add newline before loading message in openwrt-vm.sh Prepends a newline to the loading message for improved output formatting in the openwrt-vm.sh script. * Update openwrt-vm.sh * rename vm * Improve OpenWrt VM network configuration logic Refactored network interface configuration to use a loop for more robust VM status checking and command delivery. Enhanced bridge interface setup with clearer messaging and improved variable handling. Minor shell syntax improvements for consistency. * Update openwrt-vm.sh
1 parent f3bcb32 commit dffb291

File tree

2 files changed

+111
-83
lines changed

2 files changed

+111
-83
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "OpenWrt",
3-
"slug": "openwrt",
3+
"slug": "openwrt-vm",
44
"categories": [
55
4,
66
2
@@ -18,7 +18,7 @@
1818
"install_methods": [
1919
{
2020
"type": "default",
21-
"script": "vm/openwrt.sh",
21+
"script": "vm/openwrt-vm.sh",
2222
"resources": {
2323
"cpu": 1,
2424
"ram": 256,

vm/openwrt.sh renamed to vm/openwrt-vm.sh

Lines changed: 109 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ function header_info {
2222
EOF
2323
}
2424
header_info
25-
echo -e "Loading..."
25+
echo -e "\n Loading..."
2626
RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)"
2727
METHOD=""
2828
NSAPP="openwrt-vm"
2929
var_os="openwrt"
3030
var_version=" "
31-
DISK_SIZE="0.5G"
31+
DISK_SIZE="1G"
3232
GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
3333
GEN_MAC_LAN=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
3434

@@ -40,10 +40,31 @@ BGN=$(echo "\033[4;92m")
4040
GN=$(echo "\033[1;92m")
4141
DGN=$(echo "\033[32m")
4242
CL=$(echo "\033[m")
43+
44+
BOLD=$(echo "\033[1m")
4345
BFR="\\r\\033[K"
44-
HOLD="-"
45-
CM="${GN}${CL}"
46-
CROSS="${RD}${CL}"
46+
HOLD=" "
47+
TAB=" "
48+
49+
CM="${TAB}✔️${TAB}${CL}"
50+
CROSS="${TAB}✖️${TAB}${CL}"
51+
INFO="${TAB}💡${TAB}${CL}"
52+
OS="${TAB}🖥️${TAB}${CL}"
53+
CONTAINERTYPE="${TAB}📦${TAB}${CL}"
54+
DISKSIZE="${TAB}💾${TAB}${CL}"
55+
CPUCORE="${TAB}🧠${TAB}${CL}"
56+
RAMSIZE="${TAB}🛠️${TAB}${CL}"
57+
CONTAINERID="${TAB}🆔${TAB}${CL}"
58+
HOSTNAME="${TAB}🏠${TAB}${CL}"
59+
BRIDGE="${TAB}🌉${TAB}${CL}"
60+
GATEWAY="${TAB}🌐${TAB}${CL}"
61+
DEFAULT="${TAB}⚙️${TAB}${CL}"
62+
MACADDRESS="${TAB}🔗${TAB}${CL}"
63+
VLANTAG="${TAB}🏷️${TAB}${CL}"
64+
CREATING="${TAB}🚀${TAB}${CL}"
65+
ADVANCED="${TAB}🧩${TAB}${CL}"
66+
CLOUD="${TAB}☁️${TAB}${CL}"
67+
4768
set -Eeo pipefail
4869
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
4970
trap cleanup EXIT
@@ -248,35 +269,30 @@ function exit-script() {
248269

249270
function default_settings() {
250271
VMID=$(get_valid_nextid)
251-
HN=openwrt
272+
HN="openwrt"
252273
CORE_COUNT="1"
253274
RAM_SIZE="256"
254275
BRG="vmbr0"
255-
VLAN=""
276+
LAN_BRG="vmbr0"
256277
MAC=$GEN_MAC
257278
LAN_MAC=$GEN_MAC_LAN
258-
LAN_BRG="vmbr0"
279+
VLAN=""
280+
LAN_VLAN=",tag=999"
259281
LAN_IP_ADDR="192.168.1.1"
260282
LAN_NETMASK="255.255.255.0"
261-
LAN_VLAN=",tag=999"
262283
MTU=""
263284
START_VM="yes"
264285
METHOD="default"
265-
echo -e "${DGN}Using Virtual Machine ID: ${BGN}${VMID}${CL}"
266-
echo -e "${DGN}Using Hostname: ${BGN}${HN}${CL}"
267-
echo -e "${DGN}Allocated Cores: ${BGN}${CORE_COUNT}${CL}"
268-
echo -e "${DGN}Allocated RAM: ${BGN}${RAM_SIZE}${CL}"
269-
echo -e "${DGN}Using WAN Bridge: ${BGN}${BRG}${CL}"
270-
echo -e "${DGN}Using WAN VLAN: ${BGN}Default${CL}"
271-
echo -e "${DGN}Using WAN MAC Address: ${BGN}${MAC}${CL}"
272-
echo -e "${DGN}Using LAN MAC Address: ${BGN}${LAN_MAC}${CL}"
273-
echo -e "${DGN}Using LAN Bridge: ${BGN}${LAN_BRG}${CL}"
274-
echo -e "${DGN}Using LAN VLAN: ${BGN}999${CL}"
275-
echo -e "${DGN}Using LAN IP Address: ${BGN}${LAN_IP_ADDR}${CL}"
276-
echo -e "${DGN}Using LAN NETMASK: ${BGN}${LAN_NETMASK}${CL}"
277-
echo -e "${DGN}Using Interface MTU Size: ${BGN}Default${CL}"
278-
echo -e "${DGN}Start VM when completed: ${BGN}yes${CL}"
279-
echo -e "${BL}Creating a OpenWrt VM using the above default settings${CL}"
286+
DISK_SIZE="1G"
287+
echo -e "${CONTAINERID}${BOLD}${DGN}VMID: ${BGN}${VMID}${CL}"
288+
echo -e "${HOSTNAME}${BOLD}${DGN}Hostname: ${BGN}${HN}${CL}"
289+
echo -e "${CPUCORE}${BOLD}${DGN}CPU Cores: ${BGN}${CORE_COUNT}${CL}"
290+
echo -e "${RAMSIZE}${BOLD}${DGN}RAM: ${BGN}${RAM_SIZE}${CL}"
291+
echo -e "${DISKSIZE}${BOLD}${DGN}Disk Size: ${BGN}${DISK_SIZE}${CL}"
292+
echo -e "${BRIDGE}${BOLD}${DGN}WAN Bridge: ${BGN}${BRG}${CL}"
293+
echo -e "${BRIDGE}${BOLD}${DGN}LAN Bridge: ${BGN}${LAN_BRG}${CL}"
294+
echo -e "${MACADDRESS}${BOLD}${DGN}WAN MAC: ${BGN}${MAC}${CL}"
295+
echo -e "${MACADDRESS}${BOLD}${DGN}LAN MAC: ${BGN}${LAN_MAC}${CL}"
280296
}
281297

282298
function advanced_settings() {
@@ -328,6 +344,17 @@ function advanced_settings() {
328344
exit-script
329345
fi
330346

347+
if DISK_SIZE=$(whiptail --backtitle "Proxmox VE Helper Scripts" \
348+
--inputbox "Set Disk Size in GiB (e.g., 1, 2, 4)" 8 58 "1" \
349+
--title "DISK SIZE" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
350+
if [[ "$DISK_SIZE" =~ ^[0-9]+$ ]]; then
351+
DISK_SIZE="${DISK_SIZE}G"
352+
fi
353+
echo -e "${DISKSIZE}${BOLD}${DGN}Disk Size: ${BGN}${DISK_SIZE}${CL}"
354+
else
355+
exit-script
356+
fi
357+
331358
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
332359
if [ -z $BRG ]; then
333360
BRG="vmbr0"
@@ -491,47 +518,39 @@ response=$(curl -fsSL https://openwrt.org)
491518
stableversion=$(echo "$response" | sed -n 's/.*Current stable release - OpenWrt \([0-9.]\+\).*/\1/p' | head -n 1)
492519
URL="https://downloads.openwrt.org/releases/$stableversion/targets/x86/64/openwrt-$stableversion-x86-64-generic-ext4-combined.img.gz"
493520

494-
sleep 2
495521
msg_ok "${CL}${BL}${URL}${CL}"
496522
curl -f#SL -o "$(basename "$URL")" "$URL"
497-
echo -en "\e[1A\e[0K"
498-
FILE=$(basename $URL)
523+
FILE=$(basename "$URL")
499524
msg_ok "Downloaded ${CL}${BL}$FILE${CL}"
500-
gunzip -f $FILE >/dev/null 2>/dev/null || true
501-
NEWFILE="${FILE%.*}"
502-
FILE="$NEWFILE"
503-
mv $FILE ${FILE%.*}
504-
qemu-img resize -f raw ${FILE%.*} 512M >/dev/null 2>/dev/null
505-
msg_ok "Extracted & Resized OpenWrt Disk Image ${CL}${BL}$FILE${CL}"
506-
STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
507-
case $STORAGE_TYPE in
508-
nfs | dir)
509-
DISK_EXT=".qcow2"
510-
DISK_REF="$VMID/"
511-
DISK_IMPORT="-format qcow2"
512-
;;
513-
btrfs)
514-
DISK_EXT=".raw"
515-
DISK_REF="$VMID/"
516-
DISK_IMPORT="-format raw"
517-
;;
518-
esac
519-
for i in {0,1}; do
520-
disk="DISK$i"
521-
eval DISK${i}=vm-${VMID}-disk-${i}${DISK_EXT:-}
522-
eval DISK${i}_REF=${STORAGE}:${DISK_REF:-}${!disk}
523-
done
525+
526+
gunzip -f "$FILE" >/dev/null 2>&1 || true
527+
FILE="${FILE%.*}"
528+
msg_ok "Extracted OpenWrt Disk Image ${CL}${BL}$FILE${CL}"
524529

525530
msg_info "Creating OpenWrt VM"
526-
qm create $VMID -cores $CORE_COUNT -memory $RAM_SIZE -name $HN \
531+
qm create "$VMID" -cores "$CORE_COUNT" -memory "$RAM_SIZE" -name "$HN" \
527532
-onboot 1 -ostype l26 -scsihw virtio-scsi-pci --tablet 0
528-
pvesm alloc $STORAGE $VMID $DISK0 4M 1>&/dev/null
529-
qm importdisk $VMID ${FILE%.*} $STORAGE ${DISK_IMPORT:-} 1>&/dev/null
530-
qm set $VMID \
531-
-efidisk0 ${DISK0_REF},efitype=4m,size=4M \
532-
-scsi0 ${DISK1_REF},size=512M \
533+
pvesm alloc "$STORAGE" "$VMID" "vm-$VMID-disk-0" 4M >/dev/null
534+
535+
IMPORT_OUT="$(qm importdisk "$VMID" "$FILE" "$STORAGE" --format raw 2>&1 || true)"
536+
DISK_REF="$(printf '%s\n' "$IMPORT_OUT" | sed -n "s/.*successfully imported disk '\([^']\+\)'.*/\1/p")"
537+
538+
if [[ -z "$DISK_REF" ]]; then
539+
DISK_REF="$(pvesm list "$STORAGE" | awk -v id="$VMID" '$1 ~ ("vm-"id"-disk-") {print $1}' | sort | tail -n1)"
540+
fi
541+
542+
if [[ -z "$DISK_REF" ]]; then
543+
msg_error "Unable to determine imported disk reference."
544+
echo "$IMPORT_OUT"
545+
exit 1
546+
fi
547+
548+
qm set "$VMID" \
549+
-efidisk0 "${STORAGE}:0,efitype=4m,size=4M" \
550+
-scsi0 "${DISK_REF},size=${DISK_SIZE}" \
533551
-boot order=scsi0 \
534552
-tags community-script >/dev/null
553+
msg_ok "Attached disk (${DISK_SIZE})"
535554

536555
DESCRIPTION=$(
537556
cat <<EOF
@@ -570,36 +589,45 @@ msg_info "OpenWrt is being started in order to configure the network interfaces.
570589
qm start $VMID
571590
sleep 15
572591
msg_ok "Network interfaces are being configured as OpenWrt initiates."
573-
send_line_to_vm ""
574-
send_line_to_vm "uci delete network.@device[0]"
575-
send_line_to_vm "uci set network.wan=interface"
576-
send_line_to_vm "uci set network.wan.device=eth1"
577-
send_line_to_vm "uci set network.wan.proto=dhcp"
578-
send_line_to_vm "uci delete network.lan"
579-
send_line_to_vm "uci set network.lan=interface"
580-
send_line_to_vm "uci set network.lan.device=eth0"
581-
send_line_to_vm "uci set network.lan.proto=static"
582-
send_line_to_vm "uci set network.lan.ipaddr=${LAN_IP_ADDR}"
583-
send_line_to_vm "uci set network.lan.netmask=${LAN_NETMASK}"
584-
send_line_to_vm "uci commit"
585-
send_line_to_vm "halt"
586-
msg_ok "Network interfaces have been successfully configured."
587-
until qm status $VMID | grep -q "stopped"; do
592+
for _ in {1..30}; do
593+
if qm status "$VMID" | grep -q "stopped"; then break; fi
594+
send_line_to_vm ""
595+
send_line_to_vm "uci delete network.@device[0]"
596+
send_line_to_vm "uci set network.wan=interface"
597+
send_line_to_vm "uci set network.wan.device=eth1"
598+
send_line_to_vm "uci set network.wan.proto=dhcp"
599+
send_line_to_vm "uci delete network.lan"
600+
send_line_to_vm "uci set network.lan=interface"
601+
send_line_to_vm "uci set network.lan.device=eth0"
602+
send_line_to_vm "uci set network.lan.proto=static"
603+
send_line_to_vm "uci set network.lan.ipaddr=${LAN_IP_ADDR}"
604+
send_line_to_vm "uci set network.lan.netmask=${LAN_NETMASK}"
605+
send_line_to_vm "uci commit"
606+
send_line_to_vm "halt"
607+
done
608+
msg_ok "Network interfaces configured in OpenWrt"
609+
610+
msg_info "Waiting for OpenWrt to shut down..."
611+
until qm status "$VMID" | grep -q "stopped"; do
588612
sleep 2
589613
done
590-
msg_info "Bridge interfaces are being added."
591-
qm set $VMID \
592-
-net0 virtio,bridge=${LAN_BRG},macaddr=${LAN_MAC}${LAN_VLAN}${MTU} \
593-
-net1 virtio,bridge=${BRG},macaddr=${MAC}${VLAN}${MTU} >/dev/null 2>/dev/null
594-
msg_ok "Bridge interfaces have been successfully added."
595-
if [ "$START_VM" == "yes" ]; then
614+
msg_ok "OpenWrt has shut down"
615+
616+
msg_info "Adding bridge interfaces on Proxmox side"
617+
qm set "$VMID" \
618+
-net0 virtio,bridge="${LAN_BRG}",macaddr="${LAN_MAC}${LAN_VLAN}${MTU}" \
619+
-net1 virtio,bridge="${BRG}",macaddr="${MAC}${VLAN}${MTU}" >/dev/null
620+
msg_ok "Bridge interfaces added"
621+
622+
if [ "$START_VM" = "yes" ]; then
596623
msg_info "Starting OpenWrt VM"
597-
qm start $VMID
624+
qm start "$VMID"
598625
msg_ok "Started OpenWrt VM"
599626
fi
627+
600628
VLAN_FINISH=""
601-
if [ "$VLAN" == "" ] && [ "$VLAN2" != "999" ]; then
629+
if [ -z "$VLAN" ] && [ "$VLAN2" != "999" ]; then
602630
VLAN_FINISH=" Please remember to adjust the VLAN tags to suit your network."
603631
fi
604632
post_update_to_api "done" "none"
605-
msg_ok "Completed Successfully!\n${VLAN_FINISH}"
633+
msg_ok "Completed Successfully!${VLAN_FINISH:+\n$VLAN_FINISH}"

0 commit comments

Comments
 (0)