Created
December 23, 2022 21:22
-
-
Save msterhuj/bed2b2a396b2323095d96e459ecddf3b to your computer and use it in GitHub Desktop.
function for createing template from cloud image on pve
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function create_template() { | |
if [ $# -ne 6 ]; then | |
echo "Usage: $0 URL TEMPLATE_ID TEMPLATE_NAME DATASTORE BRIDGE VLAN_ID" | |
echo "Cannot init creation if not all args are set, exiting..." | |
return 1 | |
fi | |
URL=$1 | |
TM_ID=$2 | |
TM_NAME=$3 | |
DATASTORE=$4 | |
BRIDGE=$5 | |
VLAN=$6 | |
# check if vm already exists | |
if [ $(qm list | grep -c $TM_ID) -eq 1 ]; then | |
echo "VM with ID $TM_ID already exists, exiting..." | |
return 1 | |
fi | |
# check if template already exists | |
if [ $(qm list | grep -c $TM_NAME) -eq 1 ]; then | |
echo "Template with name $TM_NAME already exists, exiting..." | |
return 1 | |
fi | |
FILE="${1##*/}" | |
echo "Download the image from $URL" | |
wget "$URL" | |
# check if the file was downloaded | |
if [ ! -f $FILE ]; then | |
echo "Error when download $FILE , exiting..." | |
return 1 | |
fi | |
echo "Create a VM" | |
qm create $TM_ID --name $TM_NAME --memory 1024 --net0 virtio,bridge=$BRIDGE,tag=$VLAN | |
echo "Import the disk in qcow2 format (as unused disk) and remove the downloaded file" | |
qm importdisk $TM_ID $FILE $DATASTORE -format qcow2 | |
rm -v $FILE | |
echo "Attach the disk to the vm using VirtIO SCSI and resize it to 32G" | |
qm set $TM_ID --scsihw virtio-scsi-pci --scsi0 $DATASTORE:vm-$TM_ID-disk-0 | |
qm resize $TM_ID scsi0 +30G | |
echo "Add cloud-init drive, boot order, boot disk, serial console and VGA" | |
qm set $TM_ID --ide2 local:cloudinit --boot c --bootdisk scsi0 --serial0 socket --vga serial0 | |
echo "Set the cloud-init config to use DHCP" | |
qm set $TM_ID --ipconfig0 ip=dhcp | |
echo "Convert to tempalte" | |
qm template $TM_ID | |
echo "Done the template $TM_NAME with ID $TM_ID is ready to be cloned." | |
echo "Enjoy :)" | |
} | |
create_template \ | |
"https://cdimage.debian.org/cdimage/openstack/current-10/debian-10-openstack-amd64.qcow2" \ | |
902 \ | |
debian10-cloud-32G \ | |
local-lvm \ | |
vmbr0 \ | |
1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment