Skip to content

Instantly share code, notes, and snippets.

@JPablomr
Last active November 15, 2016 04:30
Show Gist options
  • Save JPablomr/da201228ff7f1547f0e95acc05c9b7ce to your computer and use it in GitHub Desktop.
Save JPablomr/da201228ff7f1547f0e95acc05c9b7ce to your computer and use it in GitHub Desktop.
Create Containers in proxmox 4
SSH_KEY="your_key_here"
CONTAINER_VOLUME="lxcvols"
create-container(){
local name="$1"
local memory="$2"
local disk="$3"
local ID="$(expr $(pct list | tail -n1 | cut -d" " -f1) + 1)"
local DISKNAME="subvol-$ID-disk-1"
if [ -z "$4" ];then
# this creates an image global variable
prompt-os
local image="$IMAGE"
unset IMAGE
else
local image="$4"
fi
pct create $ID "/var/lib/vz/template/cache/$image" -storage $CONTAINER_VOLUME -hostname $name -memory $memory -net0 name=eth0,bridge=vmbr0,ip=dhcp,ip6=dhcp
if [ -n "$disk" ];then
pct resize $ID rootfs "$disk"
fi
pct start $ID
mkdir "/rpool/lxc_disks/$DISKNAME/root/.ssh"
echo "$SSH_KEY" > "/rpool/lxc_disks/$DISKNAME/root/.ssh/authorized_keys"
chmod 700 "/rpool/lxc_disks/$DISKNAME/root/.ssh"
chmod 644 "/rpool/lxc_disks/$DISKNAME/root/.ssh/authorized_keys"
if [[ "$image" =~ "^centos" ]]; then
pct exec $ID /usr/sbin/service sshd restart
elif [[ "$image" =~ "^ubuntu" ]]; then
pct exec $ID /usr/sbin/service ssh restart
fi
}
prompt-os(){
local index=0
while : ; do
echo "Select Container Image:"
list-container-images | cat -n
printf "Select: "
read index
[[ $index -gt 0 && $index -le $(list-container-images | wc -l ) ]] && break
done
IMAGE=$(list-container-images | sed -n "${index}p")
}
list-container-images(){
find /var/lib/vz/template/cache/ -type f | cut -d"/" -f7
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment