QEMU CLI

最後更新: 2024-09-26
 

目錄

另見: virsh qemu-monitor-command

 


User mode & System mode

 

User mode
    qemu linux.img

System mode
    qemu-i386 -L / /bin/ls

 


VM 's boot

 

qemu -hda c.img -cdrom linux.iso -boot d -m 128 -localtime -vnc,password -monitor stdio

-boot [a|b|c|d]

  • a, b (floppy 1 and 2)
  • c (first hard disk)    <- default
  • d (first CD-ROM)
  • n-p (Etherboot from network adapter 1-4)

 


CPU

 

-cpu SandyBridge -smp 1,sockets=1,cores=1,threads=1

# The qemu host default is to assume that host overcommits all resources.("off" by default)

 


Memory

 

-m 512 -overcommit mem-lock=on

# The qemu host default is to assume that host overcommits all resources.

# "off" by default
-overcommit mem-lock=off
-overcommit cpu-pm=off

mem-lock=on

Locking memory means it becomes inaccessible by other processes running on the host system.
(To preventing memory swapping)

P.S.

 


Network

 

CLI

MAC=88:88:88:88:88:01
IFUP=/etc/qemu/script/ifup.sh

qemu ...\
  -nic tap,ifname=ros-lan,model=virtio,mac=$MAC,script=$IFUP,vhost=on

Script

The default network configure script

  • /etc/qemu-ifup        # 預設會連上 default bridge (ip route)
  • /etc/qemu-ifdown

Use "script=no" to disable up 時的 script.

 * Down 時的 script 是 downscript, 建議保留

自訂 Script: /etc/qemu/script/ifup.sh

#!/bin/bash

SW=ros-sw

if [ -n "$1" ];then
  /sbin/ip link set $1 up
  sleep 1
  /sbin/ip link set $1 master $SW
  exit 0
else
  echo "Error: no interface specified"
  exit 1
fi

 


Console

 

Serial Port Console

TELNET_ENDPOINT=localhost:23001

-serial telnet:$TELNET_ENDPOINT,server,nowait

VNC Console

 -vnc [host:]display[,option[,option[,...]]]

e.g.

VNC_ENDPOINT="192.168.33.1:19"

-vnc $VNC_ENDPOINT -device cirrus-vga

option

password

The password must be set separately using the set_password command in the QEMU monitor.

Set VNC Password

# set_password [ vnc | spice ] password [ -d display ] [ action-if-connected ]

(qemu) set_password vnc PW

VNC INFO

(qemu) info vnc

Output

Server:
     address: 0.0.0.0:5900
        auth: vnc
Client: none
(qemu) info vnc
Server:
     address: 0.0.0.0:5900
        auth: vnc
Client:
     address: 192.168.123.21:1478
  x509_dname: none
    username: none

 


Clock

 

-rtc base=utc -no-hpet

base=utc(default)/localtime

localtime is required for correct date in MS-DOS or Windows.

-no-hpet

Disable HPET support

 


Machine

 

-machine pc-i440fx-3.1,accel=kvm,usb=off,vmport=off,dump-guest-core=off

accel=X

To enable an accelerator (kvm, xen ...)

vmport=on|off|auto

Enables emulation of VMWare IO port, for vmmouse etc. Default: on

dump-guest-core=on|off

Include guest memory in a core dump. The default is on.

 


Other Options

 

-nodefaults

Don't create default devices. (serial port, parallel port, VGA adapter, floppy and CD-ROM ...)

-no-user-config

makes QEMU not load any of the user-provided config files on sysconfdir

-object

-object typename[,prop1=value1,...]

Create a new object of type typename setting properties in the order they are specified.

The 'id' property must be set.

memory-backend-file

which can be used to back the guest RAM with huge pages.

e.g.

...
-object memory-backend-file,id=mem,size=512M,mem-path=/dev/hugepages \
-numa node,memdev=mem

size

accepts common suffixes, e.g. M, G

mem-path

provides the path to either a shared memory or huge page filesystem mount.

Verify 使用了 hugepages

grep Huge /proc/meminfo    # start VM 後 HugePages_Free 少了

 


virtio-rng

 

paravirtualized device that is exposed as a hardware RNG device to the guest.

* Linux kernel contains the guest driver

-device virtio-rng-pci

* default backend is to use the host's /dev/random

-object rng-random,filename=/dev/hwrng,id=rng0 \
-device virtio-rng-pci,rng=rng0
cat /sys/devices/virtual/misc/hw_random/rng_available
virtio
cat /sys/devices/virtual/misc/hw_random/rng_current
virtio
cat /dev/hwrng

 

rngd -r /dev/hwrng

 

 

Creative Commons license icon Creative Commons license icon