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.
#!/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}
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-system-x86_64
: The QEMU binary to run.enable-kvm
: Enable KVM acceleration if available.daemonize
: Run QEMU in the background and detach from the console.cpu host
: Use the host CPU model.drive
: Attach a virtual disk image (WindowsVM.img
) to the VM.net nic
and net user
: Enable a network interface and provide a user-space networking backend.m 1G
: Allocate 1 GB of RAM to the VM.vga qxl
: Use the QXL graphics driver.spice
: Enable the SpICE protocol and listen on the specified port (SPICE_PORT
).usbdevice tablet
: Attach a virtual tablet device to the VM.device virtio-serial
: Enable a virtio serial port.chardev spicevmc
: Create a character device for the SpICE VM console.device virtserialport
: Create a virtio serial port device.exec remote-viewer
: Run the remote viewer command.--title Windows
: Set the title of the remote viewer window.spice://127.0.0.1:${SPICE_PORT}
: Connect to the SpICE server on the specified port (SPICE_PORT
).Variables
SPICE_PORT
: The port number to use for the SpICE server."$@"
: Pass any additional command-line arguments to the remote-viewer
command.