Skip to content

Instantly share code, notes, and snippets.

@TNick
Created August 2, 2019 16:46
Show Gist options
  • Save TNick/3d154707c24a332dab32d98aa160655c to your computer and use it in GitHub Desktop.
Save TNick/3d154707c24a332dab32d98aa160655c to your computer and use it in GitHub Desktop.
proxmox openwrt
#!/bin/bash
# Inspired by https://github.com/whiskerz007/proxmox_tuya-convert_container/blob/master/create_container.sh
# Arguments
#
# 1 - LXC_STORAGE (local-lvm by default)
# 2 - DISK_SIZE (1G by default) can be a number and must be followed
# by either a G or a M
# 3 - TEMPLATE (local:vztmpl/openwrt-18.06-openwrt_18.06.4_amd64.tar.gz by default)
# 4 - CORES (1 by default)
# 5 - SWAP (512 by default)
# 6 - MEMORY (512 by default)
# 7 - PASSWORD (123456789 by default)
# 8 - HOSTNAME (openwrt by default)
# 9 - OSTYPE (unmanaged by default)
# 10 - POOL (Others by default)
# 11 - ONBOOT (1 by default) 1 to enable, 0 to disble
# 12 - NET_NAME (eth0 by default) the name of the interface
# 13 - BRIDGE (vmbr0 by default) the name of the bridge
# 14 - IP (dhcp by default) the IP to assogn on NET_NAME
# 15 - SIMULATE (0 by default) only print commands, do not execute
# 16 - DEBUG (0 by default) print debug info
# Setup script
set -o pipefail
shopt -s expand_aliases
alias die='EXIT=$? LINE=$LINENO error_exit'
trap die ERR
function error_exit() {
REASON=$1
MSG="\e[91mERROR: \e[93m$EXIT@"
if [ -z "$REASON" ]; then
MSG="$MSG$LINE:"
REASON="A failure has occured."
else
MSG="$MSG`echo $(( $LINE - 1 ))`:"
fi
echo -e "$MSG \e[97m$REASON\e[39m\e[49m"
exit $EXIT
}
function debug() {
MSG=$1
if [ "$DEBUG" == "1" ] ; then
echo "$MSG"
fi
}
# Verify valid storage location; use local-lvm by default.
LXC_STORAGE=${1:-local-lvm}
SIMULATE=${15:-0}
DEBUG=${16:-1}
pvesm list $LXC_STORAGE >&/dev/null ||
die "'$LXC_STORAGE' is not a valid storage ID.\n\n\n"
pvesm status -content images -storage $LXC_STORAGE >&/dev/null ||
die "'$LXC_STORAGE' does not allow 'Disk image' to be stored."
debug "LXC_STORAGE = ${LXC_STORAGE}"
# Get storage type
STORAGE_TYPE=`pvesm status -storage $LXC_STORAGE | awk 'NR>1 {print $2}'`
debug "STORAGE_TYPE = ${STORAGE_TYPE}"
# Get the next guest VM/LXC ID
CTID=$(cat<<EOF | python3
import json
with open('/etc/pve/.vmlist') as vmlist:
vmids = json.load(vmlist)
if 'ids' not in vmids:
print(100)
else:
for i in range(100, 10000):
if not str(i) in vmids['ids']:
print(i)
break
# last_vm = sorted(vmids['ids'].keys())[-1:][0]
# print(int(last_vm)+1)
EOF
)
debug "CTID = ${CTID}"
# For dir add special extensions.
if [ "$STORAGE_TYPE" = "dir" ]; then
DISK_EXT=".raw"
DISK_REF="$CTID/"
fi
debug "DISK_EXT = ${DISK_EXT}"
debug "DISK_REF = ${DISK_REF}"
# Compute storage info.
DISK=vm-${CTID}-disk-0${DISK_EXT}
ROOTFS=${LXC_STORAGE}:${DISK_REF}${DISK}
DISK_PATH=`pvesm path $ROOTFS`
DISK_SIZE=${2:-1G}
debug "DISK = ${DISK}"
debug "ROOTFS = ${ROOTFS}"
debug "DISK_PATH = ${DISK_PATH}"
debug "DISK_SIZE = ${DISK_SIZE}"
# Allocate the disk and format it.
PVESM_COMMAND="pvesm alloc $LXC_STORAGE $CTID $DISK $DISK_SIZE"
MKEFS_COMMAND="mke2fs $DISK_PATH"
if [ $SIMULATE == "1" ] ; then
echo "$PVESM_COMMAND"
echo "$MKEFS_COMMAND"
else
$PVESM_COMMAND
$MKEFS_COMMAND
fi
# Create the container
TEMPLATE=${3:-local:vztmpl/openwrt-18.06-openwrt_18.06.4_amd64.tar.gz}
CORES=${4:-1}
SWAP=${5:-512}
MEMORY=${6:-512}
PASSWORD=${7:-123456789}
HOSTNAME=${8:-openwrt}
OSTYPE=${9:-unmanaged}
POOL=${10:-Others}
ONBOOT=${11:-1}
NET_NAME=${12:-eth0}
BRIDGE=${13:-vmbr0}
IP=${14:-dhcp}
debug "TEMPLATE = ${TEMPLATE}"
debug "CORES = ${CORES}"
debug "SWAP = $SWAP{}"
debug "MEMORY = ${MEMORY}"
debug "PASSWORD = ${PASSWORD}"
debug "HOSTNAME = ${HOSTNAME}"
debug "OSTYPE = ${OSTYPE}"
debug "POOL = ${POOL}"
debug "ONBOOT = ${ONBOOT}"
debug "NET_NAME = ${NET_NAME}"
debug "BRIDGE = ${BRIDGE}"
debug "IP = ${IP}"
PCT_COMMAND="pct create $CTID $TEMPLATE \
-arch amd64 -cores $CORES \
-swap $SWAP -memory $MEMORY -password "$PASSWORD" -hostname $HOSTNAME \
-ostype $OSTYPE -pool $POOL \
-onboot $ONBOOT \
-net0 name=$NET_NAME,bridge=$BRIDGE,ip=$IP,type=veth \
-rootfs $ROOTFS -storage $LXC_STORAGE"
if [ $SIMULATE == "1" ] ; then
echo "$PCT_COMMAND"
else
$PCT_COMMAND
cat <<EOF >> /etc/pve/lxc/${CTID}.conf
lxc.include: /usr/share/lxc/config/openwrt.common.conf
lxc.include: /usr/share/lxc/config/common.conf
lxc.cap.drop: sys_admin
lxc.mount.entry: tmp tmp tmpfs rw,nodev,relatime,mode=1777 0 0
EOF
echo -e "\n\n\n" \
"******************************\n" \
"* Successfully Create New CT *\n" \
"* CT ID is $CTID *\n" \
"******************************\n\n"
fi
@bro0k
Copy link

bro0k commented Jan 20, 2020

OpenWrt 19.07.0 release

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