logo

drewdevault.com

[mirror] blog and personal website of Drew DeVault git clone https://hacktivis.me/git/mirror/drewdevault.com.git

Getting-started-with-qemu.md (4983B)


  1. ---
  2. date: 2018-09-10
  3. layout: post
  4. title: Getting started with qemu
  5. tags: ["qemu", "instructional"]
  6. ---
  7. I often get asked questions about using my software, particularly sway, on
  8. hypervisors like VirtualBox and VMWare, as well as for general advice on
  9. which hypervisor to choose. My answer is always the same: qemu. There's no
  10. excuse to use anything other than qemu, in my books. But I can admit that it
  11. might be a bit obtuse to understand at first. qemu's greatest strength is also
  12. its greatest weakness: it has so many options that it's hard to know which ones
  13. you need just to get started.
  14. qemu is the swiss army knife of virtualisation, much like ffmpeg is the swiss
  15. army knife of multimedia (which comes as no surprise, given that both are written
  16. by Fabrice Bellard). I run a dozen permanent VMs with qemu, as well as all of
  17. the ephemeral VMs used on [builds.sr.ht](https://meta.sr.ht). Why is it better
  18. than all of the other options? Well, in short: qemu is fast, portable, better
  19. supported by guests, and has more features than Hollywood. There's nothing other
  20. hypervisors can do that qemu can't, and there's plenty qemu can that they
  21. cannot.
  22. Studying the full breadth of qemu's featureset is something you can do over
  23. time. For now, let's break down a simple Linux guest installation. We'll start
  24. by downloading some install media (how about [Alpine
  25. Linux](https://alpinelinux.org/), I like Alpine Linux) and preparing a virtual
  26. hard drive.
  27. curl -O https://nl.alpinelinux.org/alpine/v3.8/releases/x86_64/alpine-standard-3.8.0-x86_64.iso
  28. qemu-img create -f qcow2 alpine.qcow2 16G
  29. This makes a 16G virtual hard disk in a file named alpine.qcow2, the qcow2
  30. format being a format which appears to be 16G to the guest (VM), but only
  31. actually writes to the host any sectors which were written to by the guest in
  32. practice. You can also expose this as a block device on your local system (or a
  33. remote system!) with qemu-nbd if you need to. Now let's boot up a VM using our
  34. install media and virtual hard disk:
  35. qemu-system-x86_64 \
  36. -enable-kvm \
  37. -m 2048 \
  38. -nic user,model=virtio \
  39. -drive file=alpine.qcow2,media=disk,if=virtio \
  40. -cdrom alpine-standard-3.8.0-x86_64.iso \
  41. -sdl
  42. This is a lot to take in. Let's break it down:
  43. **-enable-kvm**: This enables use of the KVM (kernel virtual machine) subsystem
  44. to use hardware accelerated virtualisation on Linux hosts.
  45. **-m 2048**: This specifies 2048M (2G) of RAM to provide to the guest.
  46. **-nic user,model=virtio**: Adds a virtual **n**etwork **i**nterface
  47. **c**ontroller, using a virtual LAN emulated by qemu. This is the most
  48. straightforward way to get internet in a guest, but there are other options (for
  49. example, you will probably want to use `-nic tap` if you want the guest to do
  50. networking directly on the host NIC). `model=virtio` specifies a special
  51. `virtio` NIC model, which is used by the virtio kernel module in the guest to
  52. provide faster networking.
  53. **-drive file=alpine.qcow2,media=disk,if=virtio**: This attaches our virtual
  54. disk to the guest. It'll show up as `/dev/vda`. We specify `if=virtio` for the
  55. same reason we did for `-nic`: it's the fastest interface, but requires special
  56. guest support from the Linux virtio kernel module.
  57. **-cdrom alpine-standard-3.8.0-x86_64.iso** connects a virtual CD drive to the
  58. guest and loads our install media into it.
  59. **-sdl** finally specifies the graphical configuration. We're using the SDL
  60. backend, which is the simplest usable graphical backend. It attaches a display
  61. to the guest and shows it in an [SDL](https://www.libsdl.org/) window on the
  62. host.
  63. When you run this command, the SDL window will appear and Alpine will boot! You
  64. can complete the Alpine installation normally, using `setup-alpine` to install
  65. it to the attached disk. When you shut down Alpine, run qemu again without
  66. `-cdrom` to start the VM.
  67. That covers enough to get you off of VirtualBox or whatever other bad hypervisor
  68. you're using. What else is possible with qemu? Here's a short list of common
  69. stuff you can look into:
  70. - Running pretty much any guest operating system
  71. - Software emulation of non-native architectures like ARM, PPC, RISC-V
  72. - Using `-spice` instead of `-sdl` to enable remote access to the
  73. display/keyboard/mouse
  74. - Read-only disk images with guest writes stored in RAM (`snapshot=on`)
  75. - Non-graphical boot with `-nographic` and `console=ttyS0` configured in your
  76. kernel command line
  77. - Giving a genuine graphics card to your guest with KVM passthrough for high
  78. performance gaming, OpenCL, etc
  79. - Using [virt-manager](https://virt-manager.org/) or
  80. [Boxes](https://help.gnome.org/users/gnome-boxes/stable/) if you want a GUI to
  81. hold your hand
  82. - And much more...
  83. There's really no excuse to be using any other hypervisor[^1]. They're all
  84. dogshit compared to qemu.
  85. [^1]: Especially VirtualBox. If you use VirtualBox after reading this article you make poor life choices and are an embarrassment to us all.