Advertisement
vonschutter

make_kvm_virtual_machine_now_from_ubuntu_com

Mar 7th, 2023 (edited)
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.45 KB | None | 0 0
  1. make_kvm_virtual_machine_now_from_ubuntu_com ()
  2. {
  3. # Description: Function to create a KVM virtual michine disk and define a VM. This function should be
  4. # used on a Qemu/KVM virtual host that the virtual guest is to be used on.
  5. # NOTE: QCOW2 is a storage format for virtual machine disk images.
  6. # QCOW stands for QEMU copy on write. The QCOW2 format decouples the physical storage
  7. # layer from the virtual layer by adding a mapping between logical and physical blocks.
  8. # NOTE2: Kernel-based Virtual Machine (KVM) is an open source virtualization technology built into Linux®.
  9. # Specifically, KVM lets you turn Linux into a hypervisor that allows a host machine to run multiple,
  10. # isolated virtual environments called guests or virtual machines (VMs). KVM is part of Linux.
  11. # For complete info: https://www.redhat.com/en/topics/virtualization/what-is-KVM
  12. #
  13. # Usage: Either simply call this function to use the defaults or:
  14. # Call this function with "function" "RAM_SIZE" "DISK_SIZE" "DISK_FORMAT" "DISK_FILE"
  15. # Omitting any of the arguments will us the default instead.
  16. #
  17. # Globals:
  18. # Arguments: None
  19. # Outputs:
  20. # Returns:
  21. # Usage:
  22. # Example:
  23. #                                              
  24. # make_kvm_virtual_machine_now_from_ubuntu_com kubuntu
  25. #
  26. # If complete flexibility is needed simply use the virt-install binary and directly write the arguments.
  27. # Alternatively; edit the function with new defaults that suit the needs.
  28. # End of Documentation
  29.     ubuntu_flavor="${1}"
  30.     count=1
  31.     for i in qemu-utils qemu-tools qemu-kvm qemu-system-x86 virt-install wget
  32.     do
  33.         write_status "🧩 ${count} - Verifying dependencies ( ${i} )..."
  34.         check_dependencies ${i} &>/dev/null
  35.         count=$((count + 1))
  36.     done
  37.  
  38.     write_information "🔎 Retreive list of available $ubuntu_flavor versions..."
  39.     case "$ubuntu_flavor" in
  40.         ubuntu)
  41.             declare -a all_lts_versions=($(wget -O- releases.ubuntu.com -q | perl -ne '/Ubuntu (\d+.\d+.\d+)/ && print "$1\n"' | sort -Vu))
  42.             dist_logo="$ubuntulogo"
  43.         ;;
  44.         kubuntu)
  45.             declare -a all_lts_versions=($(wget -O- cdimage.ubuntu.com/kubuntu/releases -q | perl -ne '/ (\d+.\d+.\d+)/ && print "$1\n"' | sort -Vu))
  46.             dist_logo="$kubuntulogo"
  47.         ;;
  48.         *)
  49.             declare -a all_ubuntu_lts_versions=($(wget -O- releases.ubuntu.com -q | perl -ne '/Ubuntu (\d+.\d+.\d+)/ && print "$1\n"' | sort -Vu))
  50.             dist_logo="$ubuntulogo"
  51.         ;;
  52.     esac
  53.  
  54.     tgt_ubuntu_ver=$($RTD_GUI --colors --title "Select Release Version of $ubuntu_flavor " --inputbox "\n 👍 Please pick an available \Z1 $ubuntu_flavor \Zn version by entering it below.
  55.     Long Term Support Versions to choose from are the following:\Z4 ${all_lts_versions[*]} \Zn You may also enter an inbetween release version
  56.     by typing its release number below as well. If you are not sure just let me choose intelligently for you... \n \Z1 ${dist_logo} \Zn"  30 90 "${all_lts_versions[-1]}" 3>&1 1>&2 2>&3)
  57.     case $? in
  58.         "$DIALOG_CANCEL") return ;;
  59.         "$DIALOG_ESC") return ;;
  60.     esac
  61.     clear
  62.  
  63.     [ "$tgt_ubuntu_ver" ] || tgt_ubuntu_ver=${all_lts_versions[-1]}
  64.  
  65.     # Detect and set variables for key programs
  66.     : ${bin_virt_install:=$(type -p virt-install)}
  67.     : ${virt_net="$(if ls /sys/class/net |grep br |grep -v virbr >/dev/null; then echo "bridge:$(ls /sys/class/net)" |grep br ; else echo default;fi)"}
  68.     : ${ubuntu_iso_url=$(rtd_oem_find_live_release $tgt_ubuntu_ver $ubuntu_flavor live)}
  69.     : ${iso_filename:="$(basename $ubuntu_iso_url)"}
  70.     : ${permanent_download_dir:="/var/lib/libvirt/boot/"}
  71.  
  72.     write_status "Checking if $iso_filename already downloaded..."
  73.     iso=$(find "$permanent_download_dir" -name "$iso_filename")
  74.     if [ ! -e "$iso" ]; then
  75.         write_warning "$iso_filename is not in cache, downloading..."
  76.         wget -nc $ubuntu_iso_url -P "$permanent_download_dir" || read -p "Failure to download ISO file"
  77.         iso="$permanent_download_dir/$iso_filename"
  78.     fi
  79.  
  80.     _summary_message="The virtual machine (VDI_${CONFIG}-${tgt_ubuntu_ver}-${RANDOM}) \n
  81.     📋 - Using the instructions in PRESEED: ${PRESEED_FILE} \n
  82.     🔧 - And Using this source for the packages and files to download: \n
  83.     🌎 - ${ubuntu_iso_url} \n
  84.     🌎 - ${iso} \n
  85.     ✅ - Using the network: ${virt_net} \n
  86.     ✅ - Memory: ${3:-2048} \n
  87.     ✅ - CPU's: ${2:-2} \n
  88.     ✅ - Disk Size: ${4:-40} \n You may attach to this server and see the progress at IP: $(hostname -I))"
  89.  
  90.     "${bin_virt_install}" --name VDI_${CONFIG}-${tgt_ubuntu_ver}-${RANDOM} \
  91.         --vcpus "${2:-2}" \
  92.         --memory "${3:-2048}" \
  93.         --network "${virt_net}" \
  94.         --disk size="${4:-30}" \
  95.         --os-variant="ubuntu21.10" \
  96.         --video "virtio" --channel "spicevmc" \
  97.         --location="${iso},kernel=casper/vmlinuz,initrd=casper/initrd" \
  98.         --extra-args="ks=file:$(basename $PRESEED_FILE) auto=true priority=critical debian-installer/locale=en_US keyboard-configuration/layoutcode=us \
  99.         netcfg/get_hostname=${CONFIG}${tgt_ubuntu_ver} console-setup/ask_detect=false ubiquity/reboot=true languagechooser/language-name=English countrychooser/shortlist=US \
  100.         localechooser/supported-locales=en_US.UTF-8 ubiquity/use_nonfree=true boot=casper automatic-ubiquity splash noprompt " \
  101.         --initrd-inject="$PRESEED_FILE" \
  102.         --initrd-inject="$( dirname ${PRESEED_FILE})/ks.cfg" \
  103.         --noautoconsole && $RTD_GUI --backtitle "$BRANDING" --title "NOTICE!" --msgbox "${_summary_message}" 0 0 \
  104.     || ( read -p "💥 - An ERROR has occurred. Please press [ENTER] to continue..." && return 1 )
  105.     for i in  PRESEED_FILE vm_kernel vm_initrd source_url CONFIG tgt_ubuntu_ver ubuntu_iso_url iso_filename ; do unset $i ; done
  106.     # extra args: quiet noshell
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement