diff --git a/install.sh b/install.sh index 8f2ca03..d39e10a 100644 --- a/install.sh +++ b/install.sh @@ -7,7 +7,7 @@ set +x # Disable command echoing, remove if you want to see commands 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="ubuntu-cloud" +VM_NAME=${2:-ubuntu-cloudinit2404} # Default to "ubuntu-cloudinit2404", can be overridden by passing an argument MEMORY=2048 CORES=2 STORAGE=${1:-proxmox} # Default to "proxmox", can be overridden by passing an argument @@ -22,47 +22,59 @@ check_command() { fi } -# Step 1: Download Ubuntu Cloud Image +# Function to delete existing VM if it exists +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 +} + +# Step 1: Delete existing VM if it exists +delete_existing_vm + +# Step 2: Download Ubuntu Cloud Image echo "Downloading Ubuntu Cloud Image..." wget $IMG_URL -O $IMG_NAME check_command "Downloading Ubuntu Cloud Image" -# Step 2: Create a new virtual machine -echo "Creating a new virtual machine..." +# Step 3: Create a new virtual machine +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" -# Step 3: Import the downloaded Ubuntu disk to local storage +# Step 4: Import the downloaded Ubuntu disk to specified storage echo "Importing the downloaded Ubuntu disk to storage ($STORAGE)..." qm disk import $VM_ID $IMG_NAME $STORAGE check_command "Importing disk to storage" -# Step 4: Attach the new disk to the VM as a SCSI drive on the SCSI controller +# Step 5: Attach the new disk to the VM as a SCSI drive on the SCSI controller echo "Attaching the new disk to the VM..." qm set $VM_ID --scsihw virtio-scsi-pci --scsi0 $STORAGE:vm-$VM_ID-disk-0 check_command "Attaching disk to VM" -# Step 5: Add cloud init drive +# Step 6: Add cloud init drive echo "Adding cloud init drive..." qm set $VM_ID --ide2 $STORAGE:cloudinit check_command "Adding cloud init drive" -# Step 6: Make the cloud init drive bootable and restrict BIOS to boot from disk only +# Step 7: Make the cloud init drive bootable and restrict BIOS to boot from disk only echo "Making cloud init drive bootable and restricting BIOS..." qm set $VM_ID --boot c --bootdisk scsi0 check_command "Setting boot options" -# Step 7: Add serial console +# Step 8: Add serial console echo "Adding serial console..." qm set $VM_ID --serial0 socket --vga serial0 check_command "Adding serial console" -# Step 8: Create template +# Step 9: Create template echo "Creating template..." qm template $VM_ID check_command "Creating template" -# Step 9: Clone template +# Step 10: Clone template echo "Cloning template..." qm clone $VM_ID $CLONE_ID --name $CLONE_NAME --full check_command "Cloning template"