diff --git a/README.md b/README.md index 56e6484..80c9ee4 100644 --- a/README.md +++ b/README.md @@ -13,15 +13,14 @@ Current State: Development 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 + - [X] Guided VM Creation - [X] Image Download URL - - [ ] Instant VM-Start after Creation - [X] Image Selection - [X] VM-ID check - - [ ] Choose Memorysize - - [ ] Choose CPU cores - - [ ] Choose Network - - [ ] Disable Firewall + - [X] Choose Memorysize + - [X] Choose CPU cores + - [X] Choose Network + - [X] IMG & ISO support # 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 7a15bc2..01e849a 100644 --- a/Scripts/CreateCloudInitVM.sh +++ b/Scripts/CreateCloudInitVM.sh @@ -9,29 +9,30 @@ # Date of Last Maintenance: 03.12.2021 # # Current State: Development -# Version: 0.3.0 +# Version: 0.4.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 +# v0.4.0 - Create the VM & Gather additional Information # Planned Features: -# - [ ] Guided VM Creation -# - [ ] Image Download URL -# - [ ] Instant VM-Start after Creation -# - [ ] Image Selection +# - [X] Guided VM Creation +# - [X] Image Download URL +# - [X] Image Selection # - [X] VM-ID check -# - [ ] Choose Memorysize -# - [ ] Choose CPU cores -# - [ ] Choose Network -# - [ ] Disable Firewall +# - [X] Choose Memorysize +# - [X] Choose CPU cores +# - [X] Choose Network +# - [X] IMG & ISO & qcow2 support # Global Conigurable Variables # Global Constants IMAGE_STORAGE_LOCATION="/var/lib/vz/template/iso/" +DEFAULT_VM_STORAGE="tank-vms" # Functions ## Gather the ID of the Virtual machine @@ -63,14 +64,17 @@ function get_img { 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\"" + ;; + esac } - ## Select an Image -function sel_img{ +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) @@ -91,13 +95,94 @@ function gather_img { get_img sel_img } +## Gather Virtualmachine Network +function gather_vm_net { + ip -o link show | awk -F': ' '{print $2}' | grep vmbr + echo "Enter one of the above shown Network-Interfaces to add to your VM" + read VM_NET + readarray -t VM_NET_ARRAY < <(ip -o link show | awk -F': ' '{print $2}' | grep vmbr) + if ! [[ "${VM_NET_ARRAY[*]}" =~ "$VM_NET" ]]; then + echo "Your selected Network does not exist" + echo "Maybe it was a Typo, Please check:" + echo " - Upper/Lowercase writing" + echo "ProTip: Copy & Paste" + sleep 3 + gather_vm_net + fi +} + +## Gather hardware +function gather_hardware { + echo "Enter the amount of memory in MegaBytes" + echo "Example for 2GB = \"2048\"" + read VM_RAM + echo "Enter the amount of Cpu-Cores you want to give to the Virtual machine" + read VM_CORES + gather_vm_net +} +## Gather all aditional Data +function gather_additional { + gather_vmid + echo "Enter the name of the new Virtualmachine" + read VM_NAME + +} +## Gather VM_Storage +function gather_vm_storage { + echo "Please enter the Virtual Machine Storage Pool:[$DEFAULT_VM_STORAGE]" + read VM_STORAGE + VM_STORAGE=${VM_STORAGE:=$DEFAULT_VM_STORAGE} +} ## Get all the Variables from the User function get_user_input { - gather_vmid gather_img - + gather_hardware + gather_vm_storage + gather_additional +} +function vm_import_cloudinit_img { + IMG_TYPE=$(echo "$OS_IMG_NAME" | rev | cut -d "." -f 1 | rev) + if [ $IMG_TYPE == "iso" ] || [ $IMG_TYPE == "img" ];then + qm importdisk "$VM_ID" "$IMAGE_STORAGE_LOCATION$OS_IMG_NAME" "$VM_STORAGE" -format qcow2 + if ! [ $? -eq 0 ];then + echo "It was not possible to convert a new Image with your values" + echo "Read the error message above" + exit + fi + elif [ $IMG_TYPE == "qcow2" ];then + qm importdisk "$VM_ID" "$IMAGE_STORAGE_LOCATION$OS_IMG_NAME" "$VM_STORAGE" + if ! [ $? -eq 0 ];then + echo "It was not possible to convert a new Image with your values" + echo "Read the error message above" + exit + fi + else + echo "Your OS-Image does not have a fileending which is supported by this scirpt" + echo "only iso & img are Supported" + exit + fi +} +function create_qm_vm { + qm create "$VM_ID" --name "$VM_NAME" --memory "$VM_RAM" --cores "$VM_CORES" --net0 virtio,bridge="$VM_NET" + if ! [ $? -eq 0 ];then + echo "It was not possible to create a Virtual machine with your values" + echo "Read the error message above" + exit + fi + vm_import_cloudinit_img + qm set $VM_ID --scsihw virtio-scsi-pci --scsi0 $VM_STORAGE:vm-$VM_ID-disk-0 + if ! [ $? -eq 0 ];then + echo "It was not possible to add the custom Image to your Virtual machine" + echo "Read the error message above" + exit + fi + qm set $VM_ID --ide2 $VM_STORAGE:cloudinit --boot c --bootdisk scsi0 --serial0 socket --vga serial0 + if ! [ $? -eq 0 ];then + echo "It was not possible to add an cloudinit drive to your Virtual machine" + echo "Read the error message above" + exit + fi } # Main Script get_user_input - -# Debug \ No newline at end of file +create_qm_vm \ No newline at end of file