Skip to content

Instantly share code, notes, and snippets.

@Low-power
Created March 19, 2017 00:54
Show Gist options
  • Save Low-power/865763f86625587dfa01b6ad14baeb96 to your computer and use it in GitHub Desktop.
Save Low-power/865763f86625587dfa01b6ad14baeb96 to your computer and use it in GitHub Desktop.
create-sh.sh script for PVE
#!/bin/sh
# Create a containter in Proxmox Virtual Environment with ZFS support
# Copyright 2015-2017 Rivoreo
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
CT_STORAGE_ZFS=nss-pve-main/var/ct
CT_STORAGE_PATH=/var/ct
TEMPLATE_PATH=/var/template/cache
DEFAULT_MEMORY_SIZE=512
DEFAULT_SWAP_SIZE=512
DEFAULT_DISK_QUOTA=8
is_id_taken() {
[ -e /etc/pve/*/${1}.conf ]
}
set -e
#vmid_default=101
while true; do
read -p "ID: " vmid || exit 1
[ -z "$vmid" ] && continue
#if [ "$vmid" -lt 101 -o "$vmid" -gt 999 ]; then
# echo "ID invalid"
# continue
#fi
#if [ "$vmid" -gt 100 -a "$vmid" -lt 1000 ]; then
if [ "$vmid" -gt 100 -a "$vmid" -lt 201 ]; then
if is_id_taken "$vmid"; then
echo "ID $vmid is already taken"
else
break
fi
else
echo "ID invalid"
fi
done
if [ -d "$CT_STORAGE_PATH/$vmid" ]; then
echo "Error: root directory $CT_STORAGE_PATH/$vmid already exist"
exit 1
fi
cd "$TEMPLATE_PATH"
i=1
for f in *.tar*; do
echo "$i: $f"
i=$((i+1))
done
if [ $i -lt 2 ]; then
echo "No templates found under $TEMPLATE_PATH"
echo "Please use pveam(1) to download some"
exit 1
fi
while true; do
read -p "Template? " template_i
[ -z "$template_i" ] && continue
i=1
for f in *.tar*; do
if [ $i = "$template_i" ]; then
echo "Selected $f"
template="$f"
break 2
fi
i=$((i+1))
done
echo not found
done
hostname=
while [ -z "$hostname" ]; do
read -p "Hostname: " hostname
done
while true; do
read -p "Memory size (MiB) [$DEFAULT_MEMORY_SIZE]: " memsize
if [ -z "$memsize" ]; then
memsize="$DEFAULT_MEMORY_SIZE"
break
fi
[ "$memsize" -le 16384 -a "$memsize" -ge 2 ] && break || true
echo "Invalid size or out of range"
done
while true; do
read -p "Swap size (MiB) [$DEFAULT_SWAP_SIZE]: " swapsize
if [ -z "$swapsize" ]; then
swapsize="$DEFAULT_SWAP_SIZE"
break
fi
[ "$swapsize" -le 65536 ] && break || true
echo "Invalid size or out of range"
done
while true; do
read -p "Disk quota (GiB) or unlimited [$DEFAULT_DISK_QUOTA]: " diskquota
if [ -z "$diskquota" ]; then
diskquota="$DEFAULT_DISK_QUOTA"
break
fi
[ "$diskquota" -ge 1 -o "$diskquota" = unlimited ] && break || true
echo "Invalid size or out of range"
done
while true; do
read -p "CPU cores or unlimited [unlimited]: " cores
if [ -z "$cores" ]; then
cores=unlimited
break
fi
[ "$cores" = unlimited ] && break || true
[ "$cores" -le 128 -a "$cores" -ge 1 ] && break || true
echo "Invalid cores number or out of range"
done
echo
echo "You are about to create a containter with following options"
echo "vmid = $vmid"
echo "template = $template"
echo "hostname = $hostname"
echo "memsize = $memsize"
echo "swapsize = $swapsize"
echo "diskquota = $diskquota"
echo "cores = $cores"
echo "Press Enter to continue"
read unused
zfs_options=
[ "$diskquota" != unlimited ] && zfs_options="-o quota=${diskquota}G"
# No need to set mountpoint option
echo "creating file system"
zfs create $zfs_options "$CT_STORAGE_ZFS/$vmid"
pct_option_cores=
[ "$cores" != unlimited ] && pct_option_cores="--cores $cores"
#pct create $vmid template:"$template" --hostname "$hostname" $pct_option_cores --memory "$memsize" --net0 name=en0,ip=172.24.5.$vmid/24,bridge=vmbr0 --storage ct --swap "$swapsize" --rootfs "$CT_STORAGE_PATH/$vmid"
pct create $vmid "$template" --hostname "$hostname" $pct_option_cores --memory "$memsize" --net0 name=en0,ip=172.24.5.$vmid/24,bridge=vmbr0 --storage ct --swap "$swapsize" --rootfs "$CT_STORAGE_PATH/$vmid"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment