69 lines
1.9 KiB
Bash
69 lines
1.9 KiB
Bash
#!/bin/bash
|
|
|
|
set -e
|
|
set +x
|
|
|
|
IMG_URL="https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
|
|
IMG_NAME="noble-server-cloudimg-amd64.img"
|
|
VM_ID=8000
|
|
VM_NAME=${2:-ubuntu-cloudinit2404}
|
|
MEMORY=2048
|
|
CORES=2
|
|
STORAGE=${1:-proxmox}
|
|
|
|
check_command() {
|
|
if [ $? -ne 0 ]; then
|
|
echo "Error: $1 failed."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
delete_existing_vm() {
|
|
if qm status $VM_ID &> /dev/null; then
|
|
echo "Deleting existing VM with ID $VM_ID..."
|
|
qm destroy $VM_ID --purge
|
|
check_command "Deleting existing VM"
|
|
fi
|
|
}
|
|
|
|
delete_existing_vm
|
|
|
|
if [ -f "$IMG_NAME" ]; then
|
|
echo "Image file already exists at: $(realpath $IMG_NAME)"
|
|
echo "Skipping download..."
|
|
else
|
|
echo "Downloading Ubuntu Cloud Image..."
|
|
wget $IMG_URL -O $IMG_NAME
|
|
check_command "Downloading Ubuntu Cloud Image"
|
|
fi
|
|
|
|
echo "Creating a new virtual machine with name $VM_NAME..."
|
|
qm create $VM_ID --memory $MEMORY --core $CORES --name $VM_NAME --net0 virtio,bridge=vmbr0
|
|
check_command "Creating virtual machine"
|
|
|
|
echo "Importing the downloaded Ubuntu disk to storage ($STORAGE)..."
|
|
qm disk import $VM_ID $IMG_NAME $STORAGE
|
|
check_command "Importing disk to storage"
|
|
|
|
echo "Attaching the imported disk to the VM..."
|
|
qm set $VM_ID --scsihw virtio-scsi-pci --scsi0 proxmox:8000/vm-8000-disk-0.raw
|
|
check_command "Attaching disk to VM"
|
|
|
|
echo "Adding cloud init drive..."
|
|
qm set $VM_ID --ide2 $STORAGE:cloudinit
|
|
check_command "Adding cloud init drive"
|
|
|
|
echo "Making cloud init drive bootable and restricting BIOS..."
|
|
qm set $VM_ID --boot c --bootdisk scsi0
|
|
check_command "Setting boot options"
|
|
|
|
echo "Adding serial console..."
|
|
qm set $VM_ID --serial0 socket --vga serial0
|
|
check_command "Adding serial console"
|
|
|
|
echo "Suggested command for creating a template:"
|
|
echo "qm template $VM_ID"
|
|
|
|
echo "Script completed successfully! Don't forget to create the template when you're ready with:"
|
|
echo "qm template $VM_ID"
|