qemu configs | Cell 6 | Cell 8 | Search

This Bash script runs a QEMU virtual machine (VM) with various settings, such as enabling KVM acceleration, attaching a virtual disk, and allocating 1 GB of RAM. The script then connects to the VM using Remmina remote viewer, using the SpICE protocol over a specified port.

Cell 7

#!/bin/sh
SPICE_PORT=5924
qemu-system-x86_64 -enable-kvm -daemonize \
    -cpu host \
    -drive file=WindowsVM.img,if=virtio \
    -net nic -net user,hostname=windowsvm \
    -m 1G \
    -vga qxl \
    -spice port=${SPICE_PORT},disable-ticketing \
    -usbdevice tablet \
    -device virtio-serial \
    -chardev spicevmc,id=vdagent,name=vdagent \
    -device virtserialport,chardev=vdagent,name=com.redhat.spice.0 \
    "$@"
exec remote-viewer --title Windows spice://127.0.0.1:${SPICE_PORT}

What the code could have been:

bash
#!/usr/bin/env bash

# Set Spice port
SPICE_PORT=${1:-5924}

# Set VM configuration
VM_NAME="WindowsVM"
VM_DISK="WindowsVM.img"
VM_MEM="1G"
VM_VGA="qxl"
VM_NIC="user,hostname=windowsvm"
VM_DEVICE_TABLET="tablet"
VM_DEVICE_VIRTIO_SERIAL="virtio-serial"
VM_DEVICE_VDAGENT="vdagent"

# Define function to start VM
start_vm() {
  local cpu="host"
  local spice_options="port=${SPICE_PORT},disable-ticketing"

  qemu-system-x86_64 -enable-kvm -daemonize \
    -cpu ${cpu} \
    -drive file=${VM_DISK},if=virtio \
    -net nic -net ${VM_NIC} \
    -m ${VM_MEM} \
    -vga ${VM_VGA} \
    -spice ${spice_options} \
    -usbdevice ${VM_DEVICE_TABLET} \
    -device ${VM_DEVICE_VIRTIO_SERIAL} \
    -chardev spicevmc,id=${VM_DEVICE_VDAGENT},name=${VM_DEVICE_VDAGENT} \
    -device virtserialport,chardev=${VM_DEVICE_VDAGENT},name=com.redhat.spice.0
}

# Define function to launch remote viewer
launch_remote_viewer() {
  local spice_url="spice://127.0.0.1:${SPICE_PORT}"
  remote-viewer --title Windows ${spice_url}
}

# Start VM
start_vm

# Launch remote viewer
launch_remote_viewer

Script Overview

This is a Bash script that runs a QEMU virtual machine (VM) and connects to it using the Remmina remote viewer.

QEMU Options

Remote Viewer Connection

Variables