From d26165ad7b0fc4e8d3f2b0fbe59292785381b368 Mon Sep 17 00:00:00 2001 From: Janic Voser Date: Fri, 3 Dec 2021 10:55:01 +0100 Subject: [PATCH] v0.3.0 - Get & Select the Image of the Virtualmachine --- README.md | 9 +++-- Scripts/CreateCloudInitVM.sh | 64 ++++++++++++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 551801d..56e6484 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,16 @@ Current State: Development ### Usage: Just Execute the script, it's a guided Script (chmod +x CreateCloudInitVM.sh && ./ CreateCloudInitVM.sh) ### Planned Features: +The checkbox visualises, if the feature is already working - [ ] Guided VM Creation - - [ ] Image Download URL + - [X] Image Download URL - [ ] Instant VM-Start after Creation - - [ ] Image Selection + - [X] Image Selection - [X] VM-ID check + - [ ] Choose Memorysize + - [ ] Choose CPU cores + - [ ] Choose Network + - [ ] Disable Firewall # Bookstack Link to general Cloud-Init Information https://bookstack.voser.cloud/books/virtualisierung/chapter/cloud-init \ No newline at end of file diff --git a/Scripts/CreateCloudInitVM.sh b/Scripts/CreateCloudInitVM.sh index 492a9cf..7a15bc2 100644 --- a/Scripts/CreateCloudInitVM.sh +++ b/Scripts/CreateCloudInitVM.sh @@ -9,11 +9,12 @@ # Date of Last Maintenance: 03.12.2021 # # Current State: Development -# Version: 0.2.0 +# Version: 0.3.0 # Version History: # v0.0.1 - Basic Script Structure # v0.1.0 - Get the User input # v0.2.0 - Gather Virtual Machine ID +# v0.3.0 - Get & Select the Image of the Virtualmachine # Planned Features: @@ -22,27 +23,78 @@ # - [ ] Instant VM-Start after Creation # - [ ] Image Selection # - [X] VM-ID check +# - [ ] Choose Memorysize +# - [ ] Choose CPU cores +# - [ ] Choose Network +# - [ ] Disable Firewall # Global Conigurable Variables # Global Constants +IMAGE_STORAGE_LOCATION="/var/lib/vz/template/iso/" # Functions +## Gather the ID of the Virtual machine function gather_vmid { echo "Already used IDs" - qm list | tr -s ' ' | cut -d ' ' -f 2,3 + qm list | tr -s ' ' | cut -d ' ' -f 2,3 # Get the already used VMIDs & VM-Name echo "Enter the new ID of your Virtual Machine (It must be unused)" - read VM_ID + read VM_ID # Get the User Input(This VMID will be used for the new created VM) i=0 - readarray -t vmid_array < <(qm list | tail -n +2 | tr -s ' ' | cut -d ' ' -f 2) - if [[ "${vmid_array[*]}" =~ "$VM_ID" ]]; then + readarray -t VMID_ARRAY < <(qm list | tail -n +2 | tr -s ' ' | cut -d ' ' -f 2) + if [[ "${VMID_ARRAY[*]}" =~ "$VM_ID" ]]; then 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\"" +} + +## 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 +} +## Get all the Variables from the User function get_user_input { gather_vmid + gather_img } # Main Script