使用Vagrant管理使用QEMU的机器. 用 Apple 硅 / M1 和 CentOS arch64 图像进行测试
This is a Vagrant plugin that adds a simple QEMU provider to Vagrant, allowing Vagrant to control and provision machines using QEMU.
Notes: test with Apple Silicon / M1 and CentOS / Ubuntu aarch64 image
Tested:
Others:
config.vm.provider "qemu" do |qe|
qe.machine = "virt,accel=hvf,highmem=off"
qe.cpu = "cortex-a72"
endprivate_network support via QEMU native vmnet (macOS), TAP (Linux), or a socket netdev — multicast (Linux/Windows) or point-to-point listen/connect (no root, works on macOS)Make sure QEMU is installed, if not:
brew install qemuInstall plugin:
vagrant plugin install vagrant-qemuPrepare a Vagrantfile, see Example, and start:
vagrant up --provider qemuNotes:
Same as vagrant-libvirt version-1:
box.imgmetadata.json file describing box image (provider, virtual_size, format)Vagrantfile that does default settingsThis provider exposes a few provider-specific configuration options:
ssh_port - The SSH port number used to access VM, default: 50022arch - The architecture of VM, default: auto-detected from the host (aarch64 on Apple Silicon, x86_64 on Intel)machine - The machine type of VM, default: auto-detected from host OS + arch. For native virtualization (guest arch == host arch): virt,highmem=on,accel=hvf (arm64) / q35,accel=hvf (x86_64) on macOS, accel=kvm on Linux, accel=whpx on Windows. When emulating a non-host arch it uses accel=tcg.cpu - The cpu model of VM, default: host for native virtualization, max when emulating a non-host archsmp - The smp setting (Simulate an SMP system with n CPUs) of VM, default: 2memory - The memory setting of VM, default: 4Gdisk_resize - The target disk size of the primary disk, requires resizing of filesystem inside of VM, default: nil.ssh_host - The SSH IP used to access VM, default: 127.0.0.1ssh_auto_correct - Auto correct port collisions for ssh port, default: falsenet_device - The network device, default: auto-detected — virtio-net-device (arm64 virt) or virtio-net-pci (x86_64 q35)drive_interface - The interface type for the main drive, default virtioimage_path - The path (or array of paths) to qcow2 image for box-less VM, default is nil valueqemu_bin - Path to an alternative QEMU binary, default: autodetectedqemu_dir - The path to QEMU's data/firmware dir. Default resolution order: ENV["QEMU_DIR"] → ${HOMEBREW_PREFIX}/share/qemu → per-host default (/opt/homebrew/share/qemu on Apple Silicon, /usr/local/share/qemu on Intel macOS, /usr/share/qemu on Linux). Only consumed for aarch64 firmware; ignored (and not validated) for x86_64.extra_qemu_args - The raw list of additional arguments to pass to QEMU. Use with extreme caution. (see "Force Multicore" below as example)extra_netdev_args - extra, comma-separated arguments to pass to the -netdev parameter. Use with caution. (see "Force Local IP" below as example)extra_drive_args - Add optional extra arguments to each drive attached, default: []control_port - The port number used to control vm from vagrant, default is nil value. (nil means use unix socket)debug_port - The port number used to export serial port of the vm for debug, default is nil value. (nil means use unix socket, see "Debug" below for details)no_daemonize - Disable the "daemonize" mode of QEMU, default is false. (see "Windows host" below as example)firmware_format - The format of aarch64 firmware images (edk2-aarch64-code.fd and edk2-arm-vars.fd) loaded from qemu_dir, default: rawother_default - The other default arguments used by this plugin, default: %W(-parallel null -monitor none -display none -vga none)extra_image_opts - Options passed via -o to qemu-img when the base qcow2 images are created, default: []graceful_timeout - Seconds to wait at each vagrant halt stage before escalating, default: 60. Halt sends ACPI system_powerdown, waits up to this long, then sends QEMU's quit monitor command (a clean shutdown that flushes and closes the disk images), waits again, and finally SIGKILLs QEMU as a last resort — so halt still completes even when the guest was already halted from inside (e.g. sudo systemctl halt), where system_powerdown is a no-opadvanced_network = true)advanced_network - Enable dual-NIC advanced networking with private_network support, default: falsenet_mode - Network backend: :auto (detect by platform), :vmnet_shared, :vmnet_host, :vmnet_bridged (macOS, sudo), :socket_vmnet (macOS, no sudo — via the socket_vmnet daemon), :tap (Linux), :socket (QEMU socket netdev — multicast or point-to-point, see socket_opts), default: :autovmnet_interface - Physical interface for vmnet-bridged mode, default: en0tap_device - TAP device name for Linux tap backend, default: nil (uses tap0)mcast_addr - Convenience shortcut for the :socket backend's multicast address, default: nil (uses 230.0.0.1:1234)socket_opts - Raw options for the :socket netdev, emitted verbatim as -netdev socket,id=netN,. You pick the mode: "mcast=230.0.0.1:1234" (multicast, N-way), "listen=:1234" / "connect=127.0.0.1:1234" (point-to-point; you decide which VM listens and which connects — the no-root, macOS-friendly path). Overrides mcast_addr. Default: nil (falls back to multicast)socket_vmnet_socket - Path to the socket_vmnet daemon's unix socket, used by the :socket_vmnet backend, default: /var/run/socket_vmnet (HOMEBREW_PREFIX env, else /opt/homebrew on Apple Silicon, /usr/local on Intel)socket_vmnet_client - Path to the socket_vmnet_client wrapper, used only on QEMU older than 7.2 (which lacks the native stream netdev), default: /opt/socket_vmnet/bin/socket_vmnet_clientThese can be set like typical provider-specific configuration:
# Basic Vagrant config (API version 2)
Vagrant.configure(2) do |config|
# ... other stuff
config.vm.provider "qemu" do |qe|
qe.memory = "8G"
end
endnil valueTo be able to custom the result qemu command deeply, you can set some config options
to nil value to skip related qemu arguments.
machine: skip -machine xxxcpu: skip -cpu xxxsmp: skip -smp xxxmemory: skip -m xxxnet_device: skip all network related arguments:-device xxx,netdev=net0-netdev user,id=net0,xxxdrive_interface: skip drive for the main image, -drive if=xxx,xxxfirmware_format: skip firmware setup for aarch64, -drive if=pflash,xxxWith other_default = [], all default arguments will be skipped.
vagrant init ppggff/centos-7-aarch64-2009-4K
vagrant up --provider qemu# Basic Vagrant config (API version 2)
Vagrant.configure(2) do |config|
config.vm.box = "test-box"
config.vm.box_url = "file:///Users/xxx/test.box"
config.vm.box_check_update = false
end# Basic Vagrant config (API version 2)
Vagrant.configure(2) do |config|
config.vm.provider "qemu" do |qe, override|
override.ssh.username = "xxx"
override.ssh.password = "vagrant"
qe.image_path = "/Users/xxx/test.qcow2"
end
endOn an Intel Mac (or Linux x86_64 host) these defaults are now auto-detected,
so a plain config.vm.box = "..." with no provider overrides is usually enough.
The explicit settings below are only needed to emulate x86_64 on an Apple
Silicon host (cross-arch → TCG):
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
config.vm.provider "qemu" do |qe|
qe.arch = "x86_64"
qe.machine = "q35"
qe.cpu = "qemu64"
qe.net_device = "virtio-net-pci"
end
end# Basic Vagrant config (API version 2)
Vagrant.configure(2) do |config|
# ... other stuff
config.vm.network "forwarded_port", guest: 80, host: 8080
endThanks to taraszka for providing this config.
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.provider "qemu" do |qe|
qe.arch = "x86_64"
qe.machine = "q35"
qe.cpu = "max"
qe.smp = "cpus=2,sockets=1,cores=2,threads=1"
qe.net_device = "virtio-net-pci"
qe.extra_qemu_args = %w(-accel tcg,thread=multi,tb-size=512)
qe.qemu_dir = "/usr/local/share/qemu"
end
endVagrant.configure("2") do |config|
config.vm.box = "debian/bullseye64"
config.vm.provider "qemu" do |qe|
qe.extra_netdev_args = "net=192.168.51.0/24,dhcpstart=192.168.51.10"
end
endWindows version QEMU doesn't support daemonize mode and unix socket
Vagrant.configure("2") do |config|
# ... other stuff
config.vm.provider "qemu" do |qe|
qe.no_daemonize = true
qe.control_port = 33333
qe.debug_port = 33334
end
endVagrant.configure("2") do |config|
config.vm.define "vm1" do |c|
c.vm.box = "ppggff/centos-7-aarch64-2009-4K"
c.vm.provider "qemu" do |qe|
qe.memory = "2G"
qe.ssh_auto_correct = true
end
c.vm.synced_folder ".", "/vagrant", disabled: true
end
config.vm.define "vm2" do |c|
c.vm.box = "ppggff/centos-7-aarch64-2009-4K"
c.vm.provider "qemu" do |qe|
qe.memory = "2G"
qe.ssh_auto_correct = true
end
c.vm.synced_folder ".", "/vagrant", disabled: true
end
endThanks example from @Leandros.
See pr#73 for details.
When creating the disks that are attached, each disk is an id assign in order
they appear in the Vagrantfile. The primary disk has the id of disk0.
Vagrant.configure("2") do |config|
# ... other stuff
config.vm.provider "qemu" do |qe|
# Use a `none` drive interface.
qe.drive_interface = "none"
qe.extra_drive_args = "cache=none,aio=threads"
# To improve I/O performance, create a separate I/O thread.
# We refer to the primary disk as `disk0`.暂无开放 Issues,或尚未同步最近议题。