This Bash script sets up a virtual machine using QEMU and a VNC viewer called spicy, allowing for remote access and control. The script can be run to launch a virtual machine with a VNC connection, automatically connecting to it with the VNC viewer.
#!/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 spicy --title Windows 127.0.0.1 -p 5924
bash
#!/bin/bash
# Define virtual machine configuration
VM_CONFIG_FILE="vm_config.sh"
VM_NAME="WindowsVM"
VM_IMAGE="WindowsVM.img"
# Define port for spice connection
SPICE_PORT=${1:-5924}
# Define virtual machine architecture
VM_ARCH="x86_64"
# Define amount of RAM for the virtual machine
VM_RAM=1G
# Define virtual machine graphics
VM_GRAPHICS="qxl"
# Define network settings
VM_NETWORK="user,hostname=${VM_NAME}"
# Define USB settings
VM_USB="tablet"
# Define virtio-serial settings
VM_VIRTIO_SERIAL=device virtio-serial
# Define vdagent settings
VM_VDAGENT="chardev spicevmc,id=vdagent,name=vdagent"
# Define virtserialport settings
VM_VIRT_SERIAL_PORT="device virtserialport,chardev=vdagent,name=com.redhat.spice.0"
# Check for required dependencies
if! command -v qemu-system-x86_64 &> /dev/null; then
echo "Error: QEMU is not installed."
exit 1
fi
if! command -v spicy &> /dev/null; then
echo "Error: Spicy is not installed."
exit 1
fi
# Load user provided arguments
shift 1
# Start QEMU in daemon mode
qemu-system-x86_64 -enable-kvm -daemonize \
-cpu host \
-drive file=${VM_IMAGE},if=virtio \
-net nic -net ${VM_NETWORK} \
-m ${VM_RAM} \
-vga ${VM_GRAPHICS} \
-spice port=${SPICE_PORT},disable-ticketing \
-usb ${VM_USB} \
-${VM_VIRTIO_SERIAL} \
-${VM_VDAGENT} \
-${VM_VIRT_SERIAL_PORT} \
"$@"
# Connect to the virtual machine using Spicy
exec spicy --title ${VM_NAME} 127.0.0.1 -p ${SPICE_PORT}
# TODO: Add error handling for Spicy connection
This is a Bash script that sets up a virtual machine using QEMU and a VNC viewer called spicy.
Environment Variables
SPICE_PORT
is set to 5924, which is the port number for the VNC connection.QEMU Command
qemu-system-x86_64
command is used to start a virtual machine on an x86_64 architecture.-enable-kvm
enables the KVM (Kernel-based Virtual Machine) acceleration feature for better performance.-daemonize
runs QEMU as a daemon, allowing it to detach from the console.-cpu host
specifies that the virtual machine should use the host's CPU.-drive
specifies the virtual hard drive image file.-net nic
and -net user
are used to configure a network interface for the virtual machine.-m 1G
allocates 1 GB of memory for the virtual machine.-vga qxl
specifies the graphical card for the virtual machine.-spice
specifies the VNC connection settings.-usbdevice tablet
and -device virtio-serial
are used to configure a USB tablet and a virtual serial port.-chardev spicevmc
and -device virtserialport
are used to enable VNC support.exec Command
exec
command is used to start a new process, replacing the current one.spicy
is the VNC viewer command.This script is likely used to launch a virtual machine with a VNC connection. When run, it will start the virtual machine and automatically connect to it with the VNC viewer.