cloudinit/Scripts/CreateCloudInitVM.sh

103 lines
3.2 KiB
Bash
Raw Normal View History

2021-12-03 08:05:29 +01:00
#!/bin/bash
# Description: This simple Scripts creates an Virtualmachine on Proxmox, with the Qemu-Cli.
# Usage: Just Execute the script, it's a guided Script (chmod +x CreateCloudInitVM.sh && ./ CreateCloudInitVM.sh)
#
# Creator: Janic Joël Voser, Mettmenstetten - CH
# Date of Creation: 03.12.2021
#
# Active Maintainer: Janic Joël Voser
# Date of Last Maintenance: 03.12.2021
#
# Current State: Development
# Version: 0.3.0
2021-12-03 08:05:29 +01:00
# Version History:
# v0.0.1 - Basic Script Structure
2021-12-03 08:57:32 +01:00
# v0.1.0 - Get the User input
# v0.2.0 - Gather Virtual Machine ID
# v0.3.0 - Get & Select the Image of the Virtualmachine
2021-12-03 08:05:29 +01:00
# Planned Features:
# - [ ] Guided VM Creation
# - [ ] Image Download URL
# - [ ] Instant VM-Start after Creation
2021-12-03 08:57:32 +01:00
# - [ ] Image Selection
# - [X] VM-ID check
# - [ ] Choose Memorysize
# - [ ] Choose CPU cores
# - [ ] Choose Network
# - [ ] Disable Firewall
2021-12-03 08:05:29 +01:00
# Global Conigurable Variables
2021-12-03 08:57:32 +01:00
# Global Constants
IMAGE_STORAGE_LOCATION="/var/lib/vz/template/iso/"
2021-12-03 08:05:29 +01:00
# Functions
## Gather the ID of the Virtual machine
2021-12-03 08:57:32 +01:00
function gather_vmid {
echo "Already used IDs"
qm list | tr -s ' ' | cut -d ' ' -f 2,3 # Get the already used VMIDs & VM-Name
2021-12-03 08:57:32 +01:00
echo "Enter the new ID of your Virtual Machine (It must be unused)"
read VM_ID # Get the User Input(This VMID will be used for the new created VM)
2021-12-03 08:57:32 +01:00
i=0
readarray -t VMID_ARRAY < <(qm list | tail -n +2 | tr -s ' ' | cut -d ' ' -f 2)
if [[ "${VMID_ARRAY[*]}" =~ "$VM_ID" ]]; then
2021-12-03 08:57:32 +01:00
echo "Your selected Virtual Machine ID is already in use, choose an other ID"
gather_vmid
fi
}
## Download a new Image in to the Image Storage
function get_img {
echo "Do you wish to download an Image? (yes/no)"
read DOWNLOAD_IMG_yn
case "$DOWNLOAD_IMG_yn" in
yes|YES|Yes|y|Y)
echo "Please enter the URL of your Image"
read DOWNLOAD_IMG_URL
wget "$DOWNLOAD_IMG_URL" --directory-prefix="$IMAGE_STORAGE_LOCATION"
if [ $? -eq 0 ]; then
echo "Download was Successful"
else
echo "Download failed"
echo "Retry or Downloadfile manually"
get_img
fi
no|NO|No|n|N)
echo "Carrying on without a new Image"
*)
echo "Invalid Argument: Please type \"yes\" or \"no\""
}
2021-12-03 08:05:29 +01:00
## Select an Image
function sel_img{
echo "The following list represents all Images which exist on the Hoststorage"
ls $IMAGE_STORAGE_LOCATION -l | tr -s ' ' | cut -d' ' -f 6-9
readarray -t OS_IMG_ARRAY < <(ls $IMAGE_STORAGE_LOCATION -l | tr -s ' ' | cut -d' ' -f 9)
echo "Please enter the correct Image Name(shown above)"
read OS_IMG_NAME
if ! [[ "${OS_IMG_ARRAY[*]}" =~ "$OS_IMG_NAME" ]]; then
echo "Your selected Image does not exist"
echo "Maybe it was a Typo, Please check:"
echo " - Upper/Lowercase writing"
echo " - Fileextension(needs to be appended)"
echo "ProTip: Copy & Paste"
sleep 3
sel_img
fi
}
## Get the prepared Cloud-Init Image
function gather_img {
get_img
sel_img
2021-12-03 08:57:32 +01:00
}
## Get all the Variables from the User
2021-12-03 08:57:32 +01:00
function get_user_input {
gather_vmid
gather_img
2021-12-03 08:57:32 +01:00
}
2021-12-03 08:05:29 +01:00
# Main Script
2021-12-03 08:57:32 +01:00
get_user_input
2021-12-03 08:05:29 +01:00
# Debug