• libvirt/libvirt
    • 绑定: c#, go, java, ocaml. perl, python, php, ruby
    • API 分为: common, domain, domain snapshot, error, event, host, interface, network, node device, network filter, secret, storage, stream
  • [libvirt]
    • LXC – lightweight Linux container system
    • OpenVZ – lightweight Linux container system
    • Kernel-based Virtual Machine/QEMU (KVM) – open source hypervisor for Linux and SmartOS
    • Xen – Bare-Metal hypervisor
    • User-mode Linux (UML) paravirtualized kernel
    • VirtualBox – hypervisor by Oracle (formerly Sun) for Windows, Linux, Mac OS X, and Solaris
    • VMware ESX and GSX – hypervisors for Intel hardware
    • VMware Workstation and Player – hypervisors for Windows and Linux
    • Hyper-V – hypervisor for Windows by Microsoft
    • PowerVM – hypervisor by IBM for AIX, Linux and IBM i
    • Parallels Workstation – hypervisor for Mac by Parallels IP Holdings GmbH
    • Bhyve – hypervisor for FreeBSD 10+.
  • QEMUSwitchToLibvirt
  • UbuntuKVMWalkthrough
  • libvirt cloud-init with static networking
  • 概念 - API
    • domain
      • Guest Host
    • network
    • storage pool
    • storage volume
  • 问题
# macOS
# 安装 vbox
brew install libvirt
# alpine
apk add virt-install
# 启动服务进程
libvirtd -v
# 客户端连接
virsh -c vbox:///session
# 日志控制
LIBVIRT_LOG_FILTERS=1:vbox virsh -c vbox:///session
# export LIBVIRT_DEFAULT_URI="qemu+ssh://[email protected]/system"
virsh -c 'qemu+ssh://[email protected]/system'
# 问题排查
# user 会使用 /usr/local/var/run/libvirt/libvirt-sock
LIBVIRT_LOG_FILTERS=1:* virsh -c 'qemu+ssh://[email protected]/system' list
# 修改使用的 socket
virsh -c 'qemu+ssh://[email protected]/system?socket=/var/run/libvirt/libvirt-sock' list
# user 会使用 /usr/local/var/run/libvirt/libvirt-sock
virsh -c 'unix+ssh://[email protected]/run/libvirt/libvirt-sock' list
# 如果是非 root,先验证能访问
virsh -c unix:///run/libvirt/libvirt-sock list
# 可以转发 unix socket
ssh -R /run/libvirt/libvirt-sock: [email protected] -Nv
export VIRSH_DEFAULT_CONNECT_URI=qemu:///system
virsh list --all
# 虚拟机列表
virsh list
echo 'unix_sock_group = "libvirt"' >> /etc/libvirt/libvirtd.conf
echo 'unix_sock_rw_perms = "0770"' >> /etc/libvirt/libvirtd.conf
usermod -G libvirt -a $USER
rc-service libvirtd restart
virsh -c qemu+ssh://[email protected]/system


invalid argument: Failed to parse user 'qemu'


virt-xml-validate /path/to/XML/file


virt-install --virt-type kvm --name xp \
--location \
--extra-args "console=ttyS0" -v --os-variant debiansqueeze \
--disk size=4 --memory 512
-n xp --vcpus 2 --import winxp

virt-install \ --connect qemu:///system \ --virt-type qemu \ --name demo \ --ram 500 \ --graphics vnc \ --disk path=/root/alp.qcow2 \ --cdrom /root/alpine-standard-3.8.0-x86_64.iso \ --os-variant=generic \ --dry-run --print-xml

libvirtd [options]
-h | --help Display program help:
-v | --verbose Verbose messages.
-d | --daemon Run as a daemon & write PID file.
-l | --listen Listen for TCP/IP connections.
-t | --timeout <secs> Exit after timeout period.
-f | --config <file> Configuration file.
-V | --version Display version information.
-p | --pid-file <file> Change name of PID file.
libvirt management daemon:
Default paths:
Configuration file (unless overridden by -f):
CA certificate: $HOME/.pki/libvirt/cacert.pem
Server certificate: $HOME/.pki/libvirt/servercert.pem
Server private key: $HOME/.pki/libvirt/serverkey.pem
PID file:

virsh -c qemu+ssh://[email protected]:2222/system?socket=/var/run/libvirt/libvirt-sock list --all

listen_tls = 0
unix_sock_dir = "/usr/local/var/run/libvirt"
unix_sock_ro_perms = "0777"
unix_sock_rw_perms = "0770"
auth_unix_ro = "none"
auth_unix_rw = "none"