#!/usr/bin/env bash # Creating a new virtual (another virt-viewer wrapper, yes) # # == Defaults ================================================================= RADOS_HOST=('172.17.1.51' '172.17.1.52' '172.17.1.53') DEFAULT_SECRET_UUID="1ceb9c1b-aad0-4ac1-a671-a3459389a8c3" DEFAULT_SECRET_XML=" {{SECRET_UUID}} client.{{CEPH_USER}} secret " DEFAULT_HOST_DESC="" DEFAULT_VIRT_POOL=" {{RBD_POOL}} {{RBD_POOL}} {{HOST_DESC}} " DEFAULT_BASE_PATH=/tmp/create-vm DEFAULT_USER_INIT="#cloud-config package_upgrade: false runcmd: - touch /test.txt packages: - vim - htop users: - name: ubuntu ssh-authorized-keys: - {{PUBKEY}} sudo: ['ALL=(ALL) NOPASSWD:ALL'] ssh_pwauth: true lock-passwd: false chpasswd: {expire: False} groups: sudo shell: /bin/bash - name: adminuser ssh-authorized-keys: - {{PUBKEY}} sudo: ['ALL=(ALL) NOPASSWD:ALL'] ssh_pwauth: true lock-passwd: false chpasswd: {expire: False} groups: sudo shell: /bin/bash " DEFAULT_NETPLAN="network: version: 2 renderer: networkd ethernets: enp1s0: dhcp4: false addresses: - {{IP}}/{{PREFIX}} routes: - to: default via: {{GATEWAY}} nameservers: addresses: - {{DNS}} " DEFAULT_ENV="NAME=Ubuntu22.04 OS_VARIANT=ubuntu22.04 MEMORY=2048 VCPUS=2 IMAGE=https://cloud-images.ubuntu.com/jammy/20231010/jammy-server-cloudimg-amd64.img SIZE=8G BRIDGE=virbr0 IP=192.168.122.2 PREFIX=24 GATEWAY=192.168.122.1 DNS=8.8.8.8 RM=true RM_RBD=true PUBKEY=/home/cloudadmin/.ssh/id_rsa.pub VNC_PASSWORD=PASSWD1! CEPH_USER=admin RBD_POOL=pool-ssd RBD=ubuntu2204_test RBD_SIZE=16G " # == Help ===================================================================== HELP=" usage: $0 required_args [optional_args] OR $0 misc_args Create a new virtual machine from a specified yaml config misc_args: --bootstrap-env ENV_PATH create template env file and exit required_args: -n, --name NAME name of a virtual machine -o, --os-variant OS_VARIANT operating system -m, --memory MEMORY RAM size -c, --vcpus VCPUS Amount of of cores -i, --image IMAGE URL or path to an ISO image -s, --size SIZE Size of an OS drive (example: 8G) -b, --bridge BRIDGE bridge name (LinuxBridge only) -a, --ip-address IP ip address of eth0 -p, --prefix PREFIX network prefix -g, --gateway GATEWAY ip address of network gateway -d, --dns DNS ip address of dns server --ceph-user CEPH_USER ceph auth user with rights on pool --rbd-pool RBD_POOL ceph pool name --rbd-name RBD name of rbd in a pool --rbd-size RBD_SIZE size of RBD (default: 8G) optional_args: -h, --help show this help message and exit -e, --env export args from env file -r, --rm-if-exist if vm exist, remove it first default: false -R, --rm-rbd-if-exist create brand new RBD default: false -P, --pubkey PUBKEY place public key into autorized_keys of all defined in config.yaml users default: /home/\$USER/.ssh/id_rsa.pub -v, --vnc-password VNC_PASSWORD vnc console password default: PASSWD1! -C, --cloud-init CLOUD_INIT Path to a cloud-init config - will be created if not provided env example: $(echo -e "$DEFAULT_ENV"|paste /dev/null -|expand -4) cloud-init example: $(echo -e "$DEFAULT_USER_INIT"|paste /dev/null -|expand -4) " # == Functions ================================================================ function help() { echo -ne "${HELP/[$'\n']}" echo } function required() { [[ -n "$1" ]] || { echo "Incorrect options provided" help exit 1 } } function bootstrap_env() { required $1 echo -e "$DEFAULT_ENV" > $1 } function rm_domain() { virsh undefine --nvram $2 virsh destroy $2 #virsh pool-destroy $2 rm -f $1/backing.img rm -f $1/cidata.iso rm -f $1/meta-data rm -f $1/network-config rm -f $1/Ubuntu22.04.img rm -f $1/Ubuntu22.04.qcow2 rm -f $1/user-data virsh pool-destroy $2 virsh pool-delete $2 } function rm_rbd() { virsh undefine --nvram $3 virsh destroy $3 rbd rm $1/$2 virsh vol-delete $2 --pool $1 } function cleanup_known_host() { ssh-keygen -f "/home/$(id -nu 1000)/.ssh/known_hosts" -R "$1" ssh-keygen -f "/root/.ssh/known_hosts" -R "$1" } # == Args parse =============================================================== options=$(getopt -n $0 -o n:o:m:c:i:s:p:b:a:g:d:he:rRP:v:C: \ --long bootstrap-env: \ --long name: \ --long os-variant: \ --long memory: \ --long vcpus: \ --long image: \ --long size: \ --long bridge: \ --long ip-address: \ --long prefix: \ --long gateway: \ --long dns: \ --long ceph-user: \ --long rbd-pool: \ --long rbd-name: \ --long rbd-size: \ --long help \ --long env: \ --long rm-if-exist \ --long rm-rbd-if-exist \ --long pubkey: \ --long vnc-password: \ --long cloud-init: \ -- "$@") [ $? -eq 0 ] || { echo "Incorrect options provided" help exit 1 } eval set -- "$options" while true; do case $1 in --bootstrap-env) shift bootstrap_env $1 exit 0 ;; -n | --name) shift NAME=$1 shift ;; -o | --os-variant) shift OS_VARIANT=$1 shift ;; -m | --memory) shift MEMORY=$1 shift ;; -c | --vcpus) shift VCPUS=$1 shift ;; -i | --image) shift IMAGE=$1 shift ;; -s | --size) shift SIZE=${1:-"8G"} shift ;; -b | --bridge) shift BRIDGE=$1 shift ;; -p | --prefix) shift PREFIX=$1 shift ;; -a | --ip-address) shift IP=$1 shift ;; -g | --gateway) shift GATEWAY=$1 shift ;; -d | --dns) shift DNS=$1 shift ;; --ceph-user) shift CEPH_USER=$1 shift ;; --rbd-pool) shift RBD_POOL=$1 shift ;; --rbd-name) shift RBD=$1 shift ;; --rbd-size) shift RBD_SIZE=${1:-"8G"} shift ;; -h | --help) help exit 0 ;; -e | --env) shift ENV=$1 shift ;; -r | --rm-if-exist) shift RM=${1:-false} shift ;; -R | --rm-if-exist) shift RM_RBD=${1:-false} shift ;; -p | --pubkey) shift PUBKEY=${1:-"/home/$USER/.ssh/id_rsa.pub"} shift ;; -v | --vnc-password) shift VNC_PASSWORD=${1:-"PASSWD1!"} shift ;; -C | --cloud-init) shift CLOUD_INIT=$1 shift ;; --) shift break esac done # Required args if [[ -z "$ENV" ]]; then required $NAME required $OS_VARIANT required $MEMORY required $VCPUS required $IMAGE required $BRIDGE required $IP required $PREFIX required $GATEWAY required $DNS fi if [[ -n "$ENV" ]]; then if [[ $ENV != /* ]] || [[ $ENV != ./* ]] || [[ $ENV != ./* ]]; then ENV="./$ENV" fi if [[ -e "$ENV" ]]; then set -a source $ENV set +a else echo Env file $ENV does not exist exit 1 fi fi IMAGE_IS_URL=false if [[ "$IMAGE" == https://* ]] || [[ "$IMAGE" == http://* ]]; then IMAGE_IS_URL=true fi CP_CLOUD_INIT=false if [[ -n "$CLOUD_INIT" ]] && [[ -e "$CLOUD_INIT" ]]; then CP_CLOUD_INIT=true fi ENV=$(readlink -f "$ENV") PUBKEY=$(readlink -f "$PUBKEY") CLOUD_INIT=$(readlink -f "$CLOUD_INIT") BASE_PATH=$DEFAULT_BASE_PATH/$NAME # == Dependencies ============================================================= function dependencies() { DEBIAN_FRONTEND=noninteractive apt-get install -y genisoimage \ qemu-kvm libvirt-daemon-system virtinst } dependencies # == Asserts ================================================================== printf "virsh " if ! virsh --version; then echo "Please install libvirt packages first" exit 1 fi printf "virt-install " if ! virt-install --version; then echo "Please install virt-install package first" exit 1 fi if ! genisoimage --version; then echo "Please install genisoimage package first" exit 1 fi if ! getopt --version; then echo "Please install getopt package first" exit 1 fi if [[ ! -e $PUBKEY ]]; then ssh-keygen -t rsa -f ${PUBKEY//".pub"/""} -q -P "" fi # == Main ===================================================================== # Remove if exist if [ "$RM" = true ]; then rm_domain $BASE_PATH $NAME 2>/dev/null fi if [ "$RM_RBD" = true ]; then rm_rbd $RBD_POOL $RBD $NAME 2>/dev/null fi mkdir -p $BASE_PATH # Preparing user-data if [[ "$CP_CLOUD_INIT" == true ]]; then cp -f $CLOUD_INIT $BASE_PATH/user-data else USER_INIT="${DEFAULT_USER_INIT//"{{PUBKEY}}"/"$(cat $PUBKEY 2>/dev/null)"}" echo -e "$USER_INIT" > $BASE_PATH/user-data fi # Preparing meta-data echo -e "local-hostname: $NAME" > $BASE_PATH/meta-data # Preparing network-config netplan=${DEFAULT_NETPLAN//"{{IP}}"/"$IP"} netplan=${netplan//"{{PREFIX}}"/"$PREFIX"} netplan=${netplan//"{{GATEWAY}}"/"$GATEWAY"} netplan=${netplan//"{{DNS}}"/"$DNS"} echo -e "$netplan" > $BASE_PATH/network-config # Preparing rbd pool secret_xml=${DEFAULT_SECRET_XML//"{{SECRET_UUID}}"/"$DEFAULT_SECRET_UUID"} secret_xml=${secret_xml//"{{CEPH_USER}}"/"$CEPH_USER"} secret_path="/tmp/secret-$DEFAULT_SECRET_UUID.xml" echo -e "$secret_xml" > $secret_path if [[ -z $(virsh secret-list|grep $DEFAULT_SECRET_UUID) ]]; then secret_xml=${DEFAULT_SECRET_XML//"{{SECRET_UUID}}"/"$DEFAULT_SECRET_UUID"} secret_xml=${secret_xml//"{{CEPH_USER}}"/"$CEPH_USER"} secret_path="/tmp/secret-$DEFAULT_SECRET_UUID.xml" echo -n "$secret_xml" > $secret_path virsh secret-define --file "$secret_path" rm -f "$secret_path" virsh secret-set-value --secret "$DEFAULT_SECRET_UUID" \ --base64 "$(ceph auth get-key client.$CEPH_USER)" fi host_desc_combined="" for host in "${RADOS_HOST[@]}"; do host_desc_combined="$host_desc_combined ${DEFAULT_HOST_DESC//"{{RADOS_HOST}}"/"$host"}" done host_desc_combined=$(echo -e "$host_desc_combined"|tail -n +2) virt_pool=${DEFAULT_VIRT_POOL//"{{RBD_POOL}}"/"$RBD_POOL"} virt_pool=${virt_pool//"{{HOST_DESC}}"/"$host_desc_combined"} virt_pool=${virt_pool//"{{CEPH_USER}}"/"$CEPH_USER"} virt_pool=${virt_pool//"{{SECRET_XML_UUID}}"/"$DEFAULT_SECRET_UUID"} virt_pool_xml_path=/tmp/pool-$RBD_POOL.xml echo -e "$virt_pool" > $virt_pool_xml_path if [[ -z $(virsh pool-list --all --type rbd|grep $RBD_POOL) ]]; then host_desc_combined="" for host in "${RADOS_HOST[@]}"; do host_desc_combined="$host_desc_combined ${DEFAULT_HOST_DESC//"{{RADOS_HOST}}"/"$host"}" done host_desc_combined=$(echo -e "$host_desc_combined"|tail -n +2) virt_pool=${DEFAULT_VIRT_POOL//"{{RBD_POOL}}"/"$RBD_POOL"} virt_pool=${virt_pool//"{{HOST_DESC}}"/"$host_desc_combined"} virt_pool=${virt_pool//"{{CEPH_USER}}"/"$CEPH_USER"} virt_pool=${virt_pool//"{{SECRET_XML_UUID}}"/"$DEFAULT_SECRET_UUID"} virt_pool_xml_path=/tmp/pool-$RBD_POOL.xml echo -n "$virt_pool" > $virt_pool_xml_path virsh pool-define $virt_pool_xml_path rm -f $virt_pool_xml_path virsh pool-autostart "$RBD_POOL" virsh pool-start "$RBD_POOL" fi # Preparing rbd vol virsh vol-create-as "$RBD_POOL" "$RBD" "$RBD_SIZE" --format raw # Preparing image if [ "$IMAGE_IS_URL" == true ]; then curl -o $BASE_PATH/backing.img -L $IMAGE fi qemu-img create -f qcow2 -F qcow2 \ -o backing_file=$BASE_PATH/backing.img \ $BASE_PATH/$NAME.qcow2 qemu-img convert -f qcow2 -O raw $BASE_PATH/$NAME.qcow2 \ $BASE_PATH/$NAME.img qemu-img resize -f raw $BASE_PATH/$NAME.img $SIZE # Preparing cidata genisoimage -output $BASE_PATH/cidata.iso -volid cidata -joliet \ -rock $BASE_PATH/user-data $BASE_PATH/meta-data \ $BASE_PATH/network-config # virt-install virt-install --name $NAME \ --os-variant $OS_VARIANT \ --memory $MEMORY \ --vcpus $VCPUS \ --cpu host \ --virt-type kvm \ --disk path=$BASE_PATH/$NAME.img,format=raw,device=disk \ --disk vol=$RBD_POOL/$RBD,format=raw,device=disk \ --network bridge:$BRIDGE \ --graphics vnc,listen=0.0.0.0,password=$VNC_PASSWORD \ --import \ --boot uefi \ --disk $BASE_PATH/cidata.iso,device=cdrom \ --noautoconsole \ --noreboot # Starting vm virsh start $NAME # Cleaning up existing known_hosts cleanup_known_host $IP 2>/dev/null