Update install.sh

This commit is contained in:
colin 2024-08-11 19:47:31 -04:00
parent d17b3544d0
commit 759ecac565
1 changed files with 13 additions and 22 deletions

View File

@ -1,18 +1,16 @@
#!/bin/bash #!/bin/bash
set -e # Exit on any error set -e
set +x # Disable command echoing, remove if you want to see commands set +x
# Default Variables (Can be overridden with command-line arguments)
IMG_URL="https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img" IMG_URL="https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
IMG_NAME="noble-server-cloudimg-amd64.img" IMG_NAME="noble-server-cloudimg-amd64.img"
VM_ID=8000 VM_ID=8000
VM_NAME=${2:-ubuntu-cloudinit2404} # Default to "ubuntu-cloudinit2404", can be overridden by passing an argument VM_NAME=${2:-ubuntu-cloudinit2404}
MEMORY=2048 MEMORY=2048
CORES=2 CORES=2
STORAGE=${1:-proxmox} # Default to "proxmox", can be overridden by passing an argument STORAGE=${1:-proxmox}
# Function to check if a command failed
check_command() { check_command() {
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Error: $1 failed." echo "Error: $1 failed."
@ -20,7 +18,6 @@ check_command() {
fi fi
} }
# Function to delete existing VM if it exists
delete_existing_vm() { delete_existing_vm() {
if qm status $VM_ID &> /dev/null; then if qm status $VM_ID &> /dev/null; then
echo "Deleting existing VM with ID $VM_ID..." echo "Deleting existing VM with ID $VM_ID..."
@ -29,47 +26,41 @@ delete_existing_vm() {
fi fi
} }
# Step 1: Delete existing VM if it exists
delete_existing_vm delete_existing_vm
# Step 2: Download Ubuntu Cloud Image if [ -f "$IMG_NAME" ]; then
echo "Downloading Ubuntu Cloud Image..." echo "Image file already exists at: $(realpath $IMG_NAME)"
wget $IMG_URL -O $IMG_NAME echo "Skipping download..."
check_command "Downloading Ubuntu Cloud Image" else
echo "Downloading Ubuntu Cloud Image..."
wget $IMG_URL -O $IMG_NAME
check_command "Downloading Ubuntu Cloud Image"
fi
# Step 3: Create a new virtual machine
echo "Creating a new virtual machine with name $VM_NAME..." 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 qm create $VM_ID --memory $MEMORY --core $CORES --name $VM_NAME --net0 virtio,bridge=vmbr0
check_command "Creating virtual machine" check_command "Creating virtual machine"
# Step 4: Import the downloaded Ubuntu disk to specified storage
echo "Importing the downloaded Ubuntu disk to storage ($STORAGE)..." echo "Importing the downloaded Ubuntu disk to storage ($STORAGE)..."
qm disk import $VM_ID $IMG_NAME $STORAGE qm disk import $VM_ID $IMG_NAME $STORAGE
check_command "Importing disk to storage" check_command "Importing disk to storage"
# Step 5: Identify the unused disk and attach it to the VM as a SCSI drive on the SCSI controller
echo "Attaching the imported disk to the VM..." echo "Attaching the imported disk to the VM..."
DISK_ID=$(qm config $VM_ID | grep "^unused" | awk -F ':' '{print $1}') qm set $VM_ID --scsihw virtio-scsi-pci --scsi0 proxmox:8000/vm-8000-disk-0.raw
qm set $VM_ID --scsihw virtio-scsi-pci --scsi0 ${STORAGE}:vm-${VM_ID}-disk-0
qm set $VM_ID --delete $DISK_ID
check_command "Attaching disk to VM" check_command "Attaching disk to VM"
# Step 6: Add cloud init drive
echo "Adding cloud init drive..." echo "Adding cloud init drive..."
qm set $VM_ID --ide2 $STORAGE:cloudinit qm set $VM_ID --ide2 $STORAGE:cloudinit
check_command "Adding cloud init drive" check_command "Adding cloud init drive"
# 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..." echo "Making cloud init drive bootable and restricting BIOS..."
qm set $VM_ID --boot c --bootdisk scsi0 qm set $VM_ID --boot c --bootdisk scsi0
check_command "Setting boot options" check_command "Setting boot options"
# Step 8: Add serial console
echo "Adding serial console..." echo "Adding serial console..."
qm set $VM_ID --serial0 socket --vga serial0 qm set $VM_ID --serial0 socket --vga serial0
check_command "Adding serial console" check_command "Adding serial console"
# Step 9: Echo template creation commands for later manual execution
echo "Suggested command for creating a template:" echo "Suggested command for creating a template:"
echo "qm template $VM_ID" echo "qm template $VM_ID"