Skip to content

Instantly share code, notes, and snippets.

@suuhm
Last active October 22, 2024 04:43
Show Gist options
  • Save suuhm/053f819b000bee4af922d66ff6c5d32e to your computer and use it in GitHub Desktop.
Save suuhm/053f819b000bee4af922d66ff6c5d32e to your computer and use it in GitHub Desktop.
Setup OpenWrt 23.05 LXC Container in Proxmox - Updated Version: 2024
#!/bin/bash
# Setting Up OpenWRT on a Virtual Machine with Proxmox
# Based on: https://community.bigbeartechworld.com/t/setting-up-openwrt-on-a-virtual-machine-with-proxmox/257
# Set your wished version:
export VER="23.05"
export ARCH="amd64"
export INDEX_URL="https://images.linuxcontainers.org/images/openwrt/$VER/$ARCH/default"
#export BUILDDATE=$(date -d "yesterday" '+%Y%m%d')
export BUILDDATE=$(curl -sL $INDEX_URL | grep "<a href=" | sed -E 's/.*<a href="([^"]+)".*/\1/' | sort | tail -n 1 | tr -d \/)
export BD_URL="$INDEX_URL/$BUILDDATE"
echo; echo "[*] Setting Up OpenWRT Version $VER on a Virtual Machine with Proxmox"
echo "[*] Downloading by $BD_URL/ "
sleep 2
wget $BD_URL/rootfs.tar.xz -O /var/lib/vz/template/cache/OpenWrt-$VER.tar.xz
# lvm disk: 1G - RAM 1G - CPU 1Core 64bit
#
pct create 202 /var/lib/vz/template/cache/OpenWrt-$VER.tar.xz --arch $ARCH --hostname OpenWrt-$VER --rootfs local-lvm:1 --memory 1024 --cores 1 --ostype unmanaged --unprivileged 1
echo -e "
Add network on interface and setup network file via dhcp
(vi /etc/config/network):
config interface 'wrtb'
option type 'bridge'
option ifname 'eth0'
option proto 'dhcp'
"
exit 0;
@rickicode
Copy link

rickicode commented Sep 2, 2024

optimize code

#!/bin/bash

# Setting Up OpenWRT on a Virtual Machine with Proxmox
# Based on: https://community.bigbeartechworld.com/t/setting-up-openwrt-on-a-virtual-machine-with-proxmox/257

# Set your wished version:
export VER="23.05"
export ARCH="amd64"
export INDEX_URL="https://images.linuxcontainers.org/images/openwrt/$VER/$ARCH/default"
export BUILDDATE=$(curl -sL $INDEX_URL | grep "<a href=" | sed -E 's/.*<a href="([^"]+)".*/\1/' | sort | tail -n 1 | tr -d \/)
export BD_URL="$INDEX_URL/$BUILDDATE"

# Ask for the Container ID manually
read -p "Please enter the Container ID you want to use: " CT_ID

# Ask for the Container Name
read -p "Please enter the Container Name (hostname) [OpenWrt-$VER]: " CT_NAME
CT_NAME=${CT_NAME:-OpenWrt-$VER}

# Scan available disks
DISKS=($(pvesm status | awk '{if(NR>1) print $1}'))

echo "Available disks:"
for i in "${!DISKS[@]}"; do
    echo "$((i+1)). ${DISKS[$i]}"
done

# Ask for the disk selection
read -p "Please select the disk (1-${#DISKS[@]}): " DISK_SELECTION

# Validate disk selection
if [[ $DISK_SELECTION -lt 1 || $DISK_SELECTION -gt ${#DISKS[@]} ]]; then
    echo "Invalid selection. Exiting."
    exit 1
fi

SELECTED_DISK=${DISKS[$((DISK_SELECTION-1))]}

# Create the LXC container with the specified options
pct create $CT_ID /var/lib/vz/template/cache/OpenWrt-$VER.tar.xz --arch $ARCH --hostname $CT_NAME --rootfs ${SELECTED_DISK}:1 --memory 1024 --cores 1 --ostype unmanaged --unprivileged 1

# Add network configurations
pct set $CT_ID -net0 name=eth0,bridge=vmbr0,ip=dhcp
pct set $CT_ID -net1 name=eth1,bridge=vmbr0,tag=999,ip=dhcp

# Start the container
pct start $CT_ID

# Wait for the container to fully start
sleep 10

# Add configuration to /etc/config/network inside the container
pct exec $CT_ID -- sh -c "cat << EOF >> /etc/config/network

config interface 'lan'
        option type 'bridge'
        option ifname 'eth1'
        option proto 'static'
        option netmask '255.255.255.0'
        option ipaddr '192.168.45.1'
EOF
"

# Restart network service inside the container
pct exec $CT_ID -- /etc/init.d/network restart

exit 0;

@walterzilla
Copy link

walterzilla commented Sep 8, 2024

@rickicode script seems unable to download OpenWRT image:

Please enter the Container ID you want to use: 100
Please enter the Container Name (hostname) [OpenWrt-23.05]: openwrt
Available disks:
1. local
Please select the disk (1-1): 1
Formatting '/var/lib/vz/images/100/vm-100-disk-0.raw', fmt=raw size=1073741824 preallocation=off
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: 586dfa43-6d64-4d0a-9acb-3dea5f275d96
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376
unable to create CT 100 - can't find file '/var/lib/vz/template/cache/OpenWrt-23.05.tar.xz'

Maybe just missing something like wget $BD_URL/rootfs.tar.xz -O /var/lib/vz/template/cache/OpenWrt-$VER.tar.xz?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment