From 056548e3e0951ad1f68572af4168e06a8db1f3ae Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sat, 29 Jun 2024 07:53:41 -0500 Subject: [PATCH] newvm: Add script to run virt-install For the longest time, whenever I needed to create a new virtual machine, I just used `Ctrl+R` to find the last `virt-install` command I had run and tweaked it for the new machine. At some point, my `~/.zsh_history` overflowed, though, so the command I had been running got lost. To avoid this silliness in the future, I've created a script that runs `virt-manager` for me. As a bonus, it has some configuration flags for the parameters that often vary between machines. For most machines, though, the script can be run as simply as `newvm.sh name`. --- newvm.sh | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100755 newvm.sh diff --git a/newvm.sh b/newvm.sh new file mode 100755 index 0000000..2541a10 --- /dev/null +++ b/newvm.sh @@ -0,0 +1,136 @@ +#!/bin/sh +# vim: set sw=4 ts=4 sts=4 et : + +memory=2048 +vcpus=2 +disk_size=16 +kickstart=http://rosalina.pyrocufflink.blue/~dustin/kickstart/fedora.ks +fedora=$(rpm -E %fedora) +network=network=prod +console=true + +usage() { + printf 'usage: %s [options] name\n' "${0##*/}" +} + +while [ $# -gt 0 ]; do + case "$1" in + --kickstart) + shift + kickstart="$1" + ;; + --kickstart=*) + kickstart="${1##*=}" + ;; + --memory) + shift + memory="$1" + ;; + --memory=*) + memory="${1#*=}" + ;; + --vcpus) + shift + vcpus="$1" + ;; + --vcpus=*) + vcpus="${1#*=}" + ;; + --location) + shift + location="$1" + ;; + --location=*) + location="${1#*=}" + ;; + --disk-size) + shift + disk_size="$1" + ;; + --disk=size=*) + disk_size="${1#*=}" + ;; + --fedora) + shift + fedora="$1" + ;; + --fedora=*) + shift + fedora="${1#*=}" + ;; + --no-console|--noconsole) + console=false + ;; + --) + shift + break + ;; + *) + if [ -z "${name}" ]; then + name="$1" + else + usage >&2 + exit 2 + fi + esac + shift +done + +if [ -z "${name}" ]; then + usage >&2 + exit 2 +fi + +if [ -z "${LIBVIRT_DEFAULT_URI}" ]; then + printf 'LIBVIRT_DEFAULT_URI is not set.\n' >&2 + printf 'If you want to create a new VM locally, explicitly set ' + printf 'LIBVIRT_DEFAULT_URI=qemu:///session\n' + exit 1 +fi + +printf 'Creating %s on %s\n' "${name}" "${LIBVIRT_DEFAULT_URI}" + +if [ -z "${location}" ]; then + location=http://dl.fedoraproject.org/pub/fedora/linux/releases/${fedora}/Everything/x86_64/os +fi + +if [ -z ${http_proxy+on} ]; then + export http_proxy=http://proxy.pyrocufflink.blue:3128 + printf 'Using default HTTP proxy: %s\n' "${http_proxy}" >&2 +elif [ -z "${http_proxy}" ]; then + printf 'HTTP proxy disabled\n' "${http_proxy}" >&2 +else + printf 'Using HTTP proxy: %s\n' "${http_proxy}" >&2 +fi + +extra_args="ip=::::${name}::dhcp inst.ks=${kickstart}" +if [ -n "${http_proxy}" ]; then + extra_args="${extra_args} inst.proxy=${http_proxy}" + if [ ${fedora} -lt 40 ]; then + extra_args="${extra_args} proxy=${http_proxy}" + fi +fi +extra_args="${extra_args} inst.notmux quiet systemd.show_status=1 console=ttyS0" + +set -- \ + --name ${name} \ + --memory ${memory} \ + --cpu host \ + --location ${location} \ + --extra-args "${extra_args}" \ + --disk pool=default,size=${disk_size},cache=none \ + --network ${network} \ + --os-variant fedora$(rpm -E %fedora) \ + --sound none \ + --redirdev none \ + --rng /dev/random \ + --boot uefi \ + "$@" + +if $console; then + set -- "$@" --autoconsole text +else + set -- "$@" --noautoconsole --wait -1 +fi + +exec virt-install "$@"