The provided code snippet contains two identical QEMU sessions with the same parameters, but the second session has an added option -cpu host
which specifies that the virtual machine should use the host CPU. This option is the only difference between the two sessions, and it allows the virtual machine to utilize the same CPU model as the physical machine running the QEMU session.
qemu-system-x86_64 --enable-kvm \
-drive file=./win10.img,if=virtio \
-smp 4 -m 4096 -vga qxl \
-net nic,model=virtio -net user \
-soundhw hda
qemu-system-x86_64 --enable-kvm \
-drive file=./win10.img,if=virtio \
-smp 4 -cpu host -m 4096 -vga qxl \
-net nic,model=virtio -net user \
-soundhw hda
bash
#!/bin/bash
# Set default VM settings
DEFAULT_SMP_CORES=4
DEFAULT_MEMORY_MB=4096
DEFAULT_VGA_TYPE=qxl
DEFAULT_DRIVE_FILE=./win10.img
DEFAULT_DRIVE_IF=virtio
DEFAULT_CPU_TYPE=host
DEFAULT_SOUND_HW=hda
DEFAULT_NETWORK_MODE=user
function create_vm {
local drive_file=$1
local smp_cores=${2:-$DEFAULT_SMP_CORES}
local memory_mb=${3:-$DEFAULT_MEMORY_MB}
local vga_type=${4:-$DEFAULT_VGA_TYPE}
local drive_if=${5:-$DEFAULT_DRIVE_IF}
local cpu_type=${6:-$DEFAULT_CPU_TYPE}
local sound_hw=${7:-$DEFAULT_SOUND_HW}
local network_mode=${8:-$DEFAULT_NETWORK_MODE}
# Create VM with KVM enabled
qemu-system-x86_64 --enable-kvm \
# Add drive with virtio interface
-drive file=$drive_file,if=$drive_if \
# Set smp cores
-smp ${smp_cores} \
# Set memory
-m ${memory_mb} \
# Set vga type
-vga ${vga_type} \
# Set network nic and mode
-net nic,model=virtio -net ${network_mode} \
# Set sound hardware
-soundhw ${sound_hw}
}
# Example usage:
# Create VM with default settings
create_vm $DEFAULT_DRIVE_FILE
# Create VM with custom settings
create_vm $DEFAULT_DRIVE_FILE 2 2048 qxl virtio host pc
The provided code snippet contains two identical QEMU sessions with some minor differences.
qemu-system-x86_64
The qemu-system-x86_64
command is used to run a 64-bit x86 virtual machine.
--enable-kvm
The --enable-kvm
option enables the use of the KVM (Kernel-based Virtual Machine) acceleration on x86-based systems.
-drive file=./win10.img,if=virtio
The -drive
option adds a virtual disk to the system. The file
parameter specifies the path to the disk image, and if=virtio
specifies the interface type as VirtIO.
-smp 4 -m 4096 -vga qxl
-smp 4
: Specifies the number of virtual CPUs (4 in this case).-m 4096
: Allocates 4096 MB of RAM to the system.-vga qxl
: Uses the QXL (QEMU Enhanced Video Acceleration) graphics driver.-net nic,model=virtio -net user
-net nic,model=virtio
: Creates a VirtIO network interface.-net user
: Enables user mode network emulation.-soundhw hda
-soundhw hda
: Uses the Intel HD Audio (HDA) sound driver.The only difference between the two sessions is the -cpu host
option in the second session:
-cpu host
This option specifies that the virtual machine should use the host CPU, which means it will use the same CPU model as the physical machine running the QEMU session.