From 太極
Jump to navigation Jump to search



  • https://en.wikipedia.org/wiki/Libvirt. libvirt is an open-source API, daemon and management tool for managing platform virtualization.[3] It can be used to manage KVM, Xen, VMware ESXi, QEMU and other virtualization technologies.
  • https://libvirt.org/
  • Check libvirt version: sudo head -n2 /var/log/libvirt/qemu/ubuntu2004.log # on Ubuntu 20.04 it shows 6.0.0


$ virt-install --help | grep graphics
$ osinfo-query os 

$ sudo virt-install  -n ubuntu2004 --os-type=Linux  \
  --os-variant=ubuntu18.04  --ram=2096  --vcpus=2  \
  --disk size=20  \
  --network network=br0 \
  --graphics vnc,listen=  \
  --location Downloads/ubuntu-20.04.1-desktop-amd64.iso

$ sudo virt-install --name=centos7server \
  --os-type=Linux \
  --os-variant=centos7.0 \
  --vcpu=2 --ram=2048 \
  --disk size=20 \
  --graphics vnc \

Check if running in VM

9 things to do in your first 10 minutes on a Linux server

$ cat /sys/class/dmi/id/sys_vendor 

$ sudo dmidecode -s system-manufacturer 

On VirtualBox + CentOS vm, it returns "innotek GmbH".

On VMware + CentOS vm, it returns "VMware, Inc".

Guest additions

It seems QEUM/KVM does not have that to download.

Bridge network

  • There are different ways to configure the network in Ubuntu.
    • If we are using a Ubuntu server, we will use the Netplan method.
    • If we are using a Ubuntu desktop, we will use the Network manager command line nmcli method or the GUI tool nm-connection-editor.
  • Use nmcli to add a bridge network and a bridge slave interface
    sudo nmcli con add ifname br0 type bridge con-name br0
    sudo nmcli con add ifname eno1 type bridge-slave master br0
  • Use nmcli to bring down a wired network and bring up a bridge network
    sudo nmcli con down "Wired connection 1"
    sudo nmcli con up br0
  • Use nmcli to delete unused bridge networks
    sudo nmcli device show
    sudo nmcli con show
    sudo nmcli con del "br0"


Install on Ubuntu

  • Virtualization - The Debian Administrator's Handbook
  • How to install KVM on Ubuntu 14.04 LTS Headless Server from cyberciti
  • Simple Virtualization With Ubuntu 16.04 Linux and KVM from linuxconfig
  • KVM virtualization on a home PC with Linux Mint
  • How to Install and Configure KVM on Ubuntu 18.04 LTS Server. I skip step 4: Configure Network Bridge for KVM virtual Machines.
  • How to bring down br0 and delete it? After some messing up I need to delete br0.
    sudo ip link set br0 down  # ifconfig command is not found anymore
    sudo brctl delbr br0
  • Ubuntu 20.04 Essentials (book)Creating an Ubuntu 20.04 KVM Networked Bridge Interface. It is based on a Ubuntu Desktop environment. The description is quite detailed. Have not tried yet!
  • How to Install KVM on Ubuntu 20.04
    • qemu: qemu
    • qemu-kvm: ovmf, qemu-block-extra, qemu-system-common, qemu-system-data, qemu-system-gui, qemu-system-x86, qemu-utils, seabios, sharutils
    • libvritd-daemon: libvirt0, libvirt-daemon, libvirt-daemon-driver-qemu, ...
    • virt-manager: qemu-block-extra, qemu-utils, virt-viewer, virtinst, ...
    sudo apt install -y qemu qemu-kvm libvirt-daemon \
             libvirt-clients bridge-utils virt-manager
    sudo systemctl status libvirtd
    # enable it to start on boot 
    sudo systemctl enable --now libvirtd
    lsmod | grep -i kvm
    ip a
    # I got two new: virbr0, virbr0-nic
    # Click the superkey (Windows) and search/launch "Virtual Machine Manager"

    Click the "Create a new virtual machine" button will result in an error: No active connection to install on. Rebooting does not help. But when I use the command line to create a new VM it works (It seems sudo helps).

    sudo virt-install --name=ubuntu2004 \
      --os-type=Linux \
      --os-variant=ubuntu20.04 \
      --vcpu=2 --ram=2048 \
      --graphics vnc \

    Add --network bridge:br0 if we have added a new bridge network (br0) in the host (eg using netplan or nmcli or nm-connection-editor GUI Tool in Ubuntu). See some resource below.

  • How to install KVM on Ubuntu 20.04 LTS Headless Server.
    • IP is the new static IP for the bridge network (not virtual machine) and is the gateway current ethernet is using.
    • Note according to How to add network bridge with nmcli (NetworkManager) on Linux, WiFi is not possible. It only works with Ethernet. Confirmed! The bridge network can be pinged from another computer.
    • How to create wireless bridge connection with nmcli. Ans: This is not possible.
    • I follow this How to add network bridge with nmcli (NetworkManager) on Linux disable STP, turn off “Wired connection 1” and turn on br0.
    • Question: Do I need to turn off "Wired connection 1". It will lose the original host's static IP. The new bridge IP will become the host's IP. The guest machine still obtain a new IP via DHCP. This is not what I expect?
    • Before turn off “Wired connection 1” and turn on br0:
      $ nmcli connection show --active
      NAME                UUID   TYPE      DEVICE  
      Wired connection 1  XXX    ethernet  eno1    
      docker0             XXX    bridge    docker0 
      virbr0              XXX    bridge    virbr0 
    • After turn off “Wired connection 1” and turn on br0:
      $ nmcli connection show --active
      NAME               UUID   TYPE      DEVICE  
      br0                xxx    bridge    br0     
      docker0            XXX    bridge    docker0 
      virbr0             XXX    bridge    virbr0  
      bridge-slave-eno1  XXX    ethernet  eno1    
      vnet0              XXX    tun       vnet0 
    • I am not able to use remmina to VNC to the bridge VM. Not sure if this is because my host OS is a desktop (not headless).
  • os-variant list in virt-install
    $ osinfo-query os 


  • KVM/QEMU 以 virt-install 指令建立虛擬機器、VNC 顯示畫面教學
    sudo virt-install --virt-type kvm --name ubuntu1804 --ram 2048 
      --disk /home/seal/kvm/ubuntu1804.qcow2,format=qcow2 
      --network network=default 
      --graphics vnc,listen=,password=YOUR_PASSWORD 
      --os-type=linux --os-variant=ubuntu17.10 
    virsh vncdisplay ubuntu1804 #
                                # pay attention to the port number
                                # :0  --> 5900 in ssh
                                # :1  --> 5901 in ssh
    sudo ufw disable
    # Another machine. 
    # Suppose the host machine has an IP
    ssh -NL 5901:localhost:5901 [email protected]
    ssh -NL 5901: [email protected]
    vncviewer # from a different machine

    NOTE: I have to use local port forwarding. If I try to directly connect to the VM in RealVNC, it will show The connection was refused by the computer. A limit by KVM?

The question is how do we know the client's IP? I guess cockpit method is better.

Install qemu-system-arm in Ubuntu

  • sudo apt install qemu-system-arm
  • How to emulate Raspberry Pi in QEMU which is based on qemu-rpi-kernel-Qemu kernel for emulating Rpi on QEMU.
    mkdir rpi-vm && cd rpi-vm
    git clone https://github.com/dhruvvyas90/qemu-rpi-kernel.git
    # Download Raspbian Buster Lite
    # from https://www.raspberrypi.org/downloads/raspberry-pi-os/
    dd if=/dev/zero bs=1M count=4096 >> 2020-08-20-raspios-buster-armhf-lite.img
    sudo losetup -f -P --show 2020-08-20-raspios-buster-armhf-lite.img
    sudo mount -o rw /dev/loopXp2 /mnt
    sudo nano /mnt/etc/fstab
    sudo nano /etc/ld.so.preload # Comment out the line and save the file. 
    # Then unmount with the following commands:
    # sudo umount /mnt
    # sudo losetup -d /dev/loopX
    qemu-system-arm \
      -M versatilepb \
      -cpu arm1176 \
      -m 256 \
      -kernel qemu-rpi-kernel/kernel-qemu-4.19.50-buster \
      -hda 2020-08-20-raspios-buster-armhf-lite.img \
      -append "dwc_otg.lpm_enable=0 root=/dev/sda2 console=tty1 rootfstype=ext4 elevator=deadline rootwait" \
      -dtb qemu-rpi-kernel/versatile-pb-buster.dtb \
      -no-reboot \
      -serial stdio
    # We can install a desktop. See 
    # https://www.raspberrypi.org/forums/viewtopic.php?t=133691

Install on CentOS


Install using qcow2 image

Running a virtual machine from a ramdisk

  • List of RAM drive software
  • How to Create a RAM Drive in Linux
    if [ "$(mount | grep -o "/mnt/ram")" != "/mnt/ram" ]; then
      sudo mkdir -p /mnt/ram
      sudo mount -t ramfs -o size=1g ramfs /mnt/ram
      sudo chown -R $(whoami):$(whoami) /mnt/ram
    mount | grep ram
    sudo umount /mnt/ram
  • https://youtu.be/fOaWrhLoVK0
  • Libvirt In-Memory Storage
  • How to Easily Create RAM Disk on Debian, Ubuntu, Linux Mint, CentOS. How to Create a RAM Disk in Any Linux Distro.
    sudo mkdir /tmp/ramdisk
    sudo chmod 777 /tmp/ramdisk
    htop  # check free ram
    sudo mount -t tmpfs -o size=1G myramdisk /tmp/ramdisk
                # myramdisk is the device name I gave to it
    mount | tail -n 1
    sudo umount /tmp/ramdisk/

    Test RAM speed

    # test write speed
    sudo dd if=/dev/zero of=/tmp/ramdisk/zero bs=4k count=100000
    ## 409600000 bytes (410 MB, 391 MiB) copied, 0.734813 s, 557 MB/s  # SATA SSD
    ## 409600000 bytes (410 MB, 391 MiB) copied, 0.18296 s, 2.2 GB/s   # NVME
    # test read speed
    sudo dd if=/tmp/ramdisk/zero of=/dev/null bs=4k count=100000
    ## 409600000 bytes (410 MB, 391 MiB) copied, 0.410809 s, 997 MB/s  # SATA SSD
    ## 409600000 bytes (410 MB, 391 MiB) copied, 0.139487 s, 2.9 GB/s  # NVME

    Auto-mount on System Boot

    sudo nano /etc/fstab
    ## myramdisk  /tmp/ramdisk  tmpfs  defaults,size=1G,x-gvfs-show  0  0
    ## To mount it immediately without reboot, run the following command.
    sudo mount -a 

    How to Run VirtualBox VM on RAM Disk. You should set the machine folder to the RAM disk directory (/tmp/ramdisk/).

kvm command


  • How to Create, Revert and Delete KVM Virtual machine (domain) snapshot with virsh command, How to create snapshot in Linux KVM VM/Domain
    $ sudo su
    # virsh list --all
    # virsh snapshot-create-as --domain win10 --name win10_snap --description "Windows 10 snapshot"    # Create a snapshot
    # virsh snapshot-list win10
    # virsh snapshot-info --domain win10 --snapshotname win10_snap
    # virsh snapshot-revert win10 win10_snap    # Restore
    # virsh snapshot-delete --domain win10 --snapshotname win10_snap
  • To create a snapshot using the GUI, open virtualization manager window. In the VM properties, click on the icon (two displays overlapped) which is on the right of the menu, which says Manage VM snapshots.
  • To restore a snapshot using Virtual Machine Manager, right click on a snapshot and select "Start snapshot".


Virtual Machine Manager/virt-manager, Cockpit, virt-viewer, remote-viewer

sudo apt install cockpit cockpit-machines
  • Creating Ubuntu 20.04 KVM Virtual Machines using Cockpit and virt-manager*.
    • It shows how to handle the error of cannot opening the ISO file.
    • It shows how to use virt-viewer to connect to a VM (virt-viewer)
    • remote-viewer will be used if we change the Console type from 'VNC' to 'Graphics Console in Desktop Viewer' and click the 'Launch Remote Viewer' button. A text file called 'download' will be downloaded. If we click it, it will launch remote-viewer. An interesting thing is the file will be automatically deleted once we click on it:)
  • Consider running a VM on a remote machine with cockpit version 215.
    • For Ubuntu 20.04.1 Desktop. If I increase the display resolution from 1024x768 (4:3) to others without keeping the same ratio, the mouse calibration has a problem:( Maybe it's better not to change the resolution. The VNC method works.
    • For Windows 10, we need to unplug ethernet first in order to create a local account. I've not found a way to use virt-viewer or remote-viewer to view the desktop locally. I can still use cockpit web interface (Consoles) to use the Windows desktop. I am able to increase the display resolution to 1280x800 from 1024x768 which makes the interface easy for my eyes. One problem is the machine will freeze for some unknown reason.



Find out the IP of the guest machine

# Method 1:
$ virsh list
$ virsh domifaddr centos8

# Method 2:
$ sudo apt install net-tools   # Debian
$ sudo dnf install net-tools   # CentOS
$ arp -n               # find the rows with virbr0 adapter

# Method 3:
# KVM -> Info -> NIC

Back and restore images

How to KVM Backup and Restore in Linux

# Backup 
virsh list --all
virsh dumpxml win10 > /MyBackup/win10.xml
sudo cp /var/lib/libvirt/images/win10.qcow2 /MyBackup/win10.qcow2
sudo chown $USER:$USER /MyBackup/*.qcow2

# Restore Making
sudo virsh undefine win10
ls -l /etc/libvirt/qemu
virsh list --all
sudo rm /var/lib/libvirt/images/win10.qcow2
sudo cp /MyBackup/win10.qcow2 /var/lib/libvirt/images/
sudo virsh define --file /MyBackup/win10.xml
# virsh start win10

Cheat sheet

KVM Cheatsheet


How to Use Virtualbox VMs on KVM In Linux

How to Use Virtualbox VMs on KVM In Linux

Run macOS on KVM / QEMU