qemu configs | Cell 4 | Cell 6 | Search

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.

Cell 5

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

What the code could have been:

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

Code Breakdown

Two QEMU Sessions

The provided code snippet contains two identical QEMU sessions with some minor differences.

QEMU Command

qemu-system-x86_64

The qemu-system-x86_64 command is used to run a 64-bit x86 virtual machine.

Enabling KVM

--enable-kvm

The --enable-kvm option enables the use of the KVM (Kernel-based Virtual Machine) acceleration on x86-based systems.

Virtual Disk

-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.

System Configuration

-smp 4 -m 4096 -vga qxl

Networking

-net nic,model=virtio -net user

Sound Configuration

-soundhw hda

Differences between the Two Sessions

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.