In Praise of QEMU
drewdevault.com
drewdevault.com
Few years ago there was an uprise of blog posts explaining how to run Linux as a primary system and virtualize Windows machine with GPU passthrough for gaming. ~ 1 year ago I took a dive and did a lot of reading and attempts to make it work, the final results made me really happy. QEMU/KVM/VFIO is one of the best software combinations I've ever used.
Witch CPU isolation and scheduling properly setup my Linux part of machine runs Docker with 25 services, while Windows part happily plays games without stutter at the same time.
That said, quite a few gaming companies are anti-virtualization (with reasonings being that mostly those who use it are using it for cheating purposes) and ban your account, without rights to appeal (ex. Rainbow Six). I honestly find this stance blunt and it feels like discrimination. But currently such use of virtualization is really niche, so what one can do?There are very significant pain points, specifically:
1. if one reserves the video card for VFIO, it won't have any power management; this means that it will run hot while doing nothing; in order to work this around:
1a. first has to battle with X, which has an option not-to-take-over-a-card-but-it-takes-it-over-nonetheless
1b. then one can give exclusive access to the graphic card driver, which can be switched out/in when starting/stopping the VM; this unfortunately works, but not reliably
2. the points above apply to nvidia; AMD is worse, as it hasn't supported soft GPU reset until very recently (I think it was added on 5.19 or so)
2a. this means that one starts the VM, then stops it, and most of the times the card will hang
2b. there resize BAR functionality is not supported by VFIO (at least, last year it wasn't), which means, one loses additional performance (I could be ok with it, as the loss is not significant, but performance losses compound)
The problem is that all the points above are not in control of the user; the problems happen at driver level (if, say, there is no reset support, one can't add it out of thin air).
If one uses Nvidia, and they're ok with the card running hot all the time, then definitely, VFIO works wonder. But this lead me to abandon VFIO, as I don't want that (and the alternative of the card having a most-of-the-time-malfunctiong driver was not appealing, either).
Big shame! I loved VFIO :)
QEMU/VFIO with Looking Glass is just such an amazing setup for my use cases.
I guess libvirt would be the "docker compose" front-end, but I remember considering that more complex than would be necessary.
I know Proxmox has some QEMU management tools (which I find decent), but they don't seem to be very popular outside of it.
One cool QEMU thing I recently discovered is the microvm machine type (https://qemu.readthedocs.io/en/latest/system/i386/microvm.ht...), which enables it to spawn VMs just as quickly as Firecracker!
It would be nicer yet if there was some way to say "give me a shell in this ephemeral VM" like docker does it, but sadly Kata Containers appear to have dropped docker support and I haven't been able to get it to run with the alternative runtimes.
I end up designing a lot of CLI tools and this is a design problem I run into a lot with my own stuff: how many flags are too many? Introducing a config file works, but now it makes your tool much less feasible to run in a shell script but, like you say, too many flags makes your tool unwieldy to run anyway. The problem is harder if you have a venerable tool that's always been flag heavy. Do you keep lumping flags in or do you steer the community toward the use of a config file? Or a hybrid?
I think mysqld has a similar approach, though I don't think it's as complete. I'll bet both of those projects would answer that there's no such thing as a tipping point where you tell users to use a config file. They're just two ways of doing the same thing; use whichever makes sense for your deployment.
#!/bin/sh
set -eu
# Argh I can't write comments in between lines... just email me if you have q's: nerdponx@mail.example.net
exec /usr/sbin/boop \
--enable-foobars \
--this-is-a-really-long-option \
--sound=quack:.:loader=org.example.soundfactory-lib.SoundFactory.wrapped_loader \
--sound=woof:.:loader=net.quibbler.QuibblerWoofMaker \
--jvm-opts='-Xms512M -Xmx1G -Xquux=1234 -Xbootclasspath:.'
But you can preserve what's left of your brain cells without switching to a "non-shell" language if you use Zsh, and then you don't have to sweat over shell quoting and parameter expansion either: #!/usr/bin/env zsh
# Enable "strict" features for typo-safety
emulate zsh
setopt err_exit pipe_fail warn_create_global warn_nested_var no_unset
boop_args=(
# We always want foobars, why isn't this on by default?
--enable-foobars
# See: https://stackoverflow.com/questions/4973
--this-is-a-really-long-option
# Use the good sounds! Please see README for obtaining these deps.
--sound=quack:.:loader=org.example.soundfactory-lib.SoundFactory.wrapped_loader
--sound=woof:.:loader=net.quibbler.QuibblerWoofMaker
# Trust me, don't touch these.
--jvm-opts='-Xms512M -Xmx1G -Xquux=1234 -Xbootclasspath:.'
)
exec /usr/sbin/boop ${(@)boop_args}previously I've done this, which works, but is a bit....... yeah.
some command \
`# did you know comments work inside backticks like this?` \
--complex_flag_needing_descriptions=... \
`# literally any ffmpeg flag` \
--lol no
now if only shellcheck would support zsh.... CMD="some command"
# Some comment
CMD="$CMD --verbose --complex-flag=..."
# Other stuff
CMD="$CMD --lol=\"no thanks\""
eval $CMDthere are ways to deal with that of course, but it's kinda unavoidably more complex.
Since the default zsh behaviour is much more reasonable (e.g. just $foo won't do globbing or word splitting) and you don't really need POSIX compatibility if you're going to use zsh-specific features, the need for shellcheck is hugely less than what you need with bash or POSIX sh.
So my QEMU OSX instance got better performance than a Mac Pro max, very well near the top ~5 scores for macs in geekbench.
Props
I did get some warnings from QEMU along the lines of "X register doesnt exist" or something but the machine runs fine and geekbench runs fine as well. Play with the cores/threads settings as well. If you go too high (for me was around 8), it doesn't boot up and just hangs.
While I can't speak for your board specifically, if the pin assignment is software reconfigurable (and is reset to sd-card access on reboot!) and you're not using the SD card once your program is running, you could reconfigure the pins in software and then wait for a JTAG debugger to attach (by having an infinite loop you use openOCD to continue past). Of course if you can debug your issue using QEMU that's indeed much easier, thanks Fabrice!
Of course without things like KVM, and assorted accelerators, etc. it would lose a fair bit of luster. We all stand on the shoulders of giants to some degree.
Nonetheless, I don't know of another free emulator that even comes close... are there any to speak of?
But with QEMU you can emulate extremely exotic CPUs on standard x86's; that's the stuff that Console Emulators struggled with immensely!
That said, trying to follow the QEMU code using static analysis is impossible. Every function call is a function pointer stored as a parameter in a struct that was set up by a macro called in initialization. The only way I could figure out how things are tied together is with GDB.
I feel like using C++ would have been a better language choice.
The original QEMU had an implementation of the required machine ops in C, but instead of calling those ops, it memcpy()d their implementation, thus enabling binary translation without interpreter overhead -- basically using the underlying "gcc" as a code-generator at runtime without including it. Pure elegance and/or madness.
And it worked very well.
[1] Markku Rossi, Kengatharan Sivalingam, 1996, A Survey of Instruction Dispatch Techniques for Byte-Code Interpreters, https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.53....
[2] M. Anton Ertl, David Gregg, 2004, Combining Stack Caching with Dynamic Superinstructions https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.90...
(*) the trick being the environment. I'm writing assembly code that just invokes Linux system calls. If you need shared libraries or other OS services, it gets more complicated.
My colleague, who is working on kboot for FreeBSD (to run the FreeBSD bootloader as a linux binary that kexec's into FreeBSD from a linuxboot environment) also found QEMU an invaluable tool.
In both cases, I think QEMU probably cut development time by 50% or more.
Also:
virsh domxml-to-native qemu-argv --domain "your_wm_name"
To get the Qemu launching script if you plain to deploy a prototype over thousands of machines.I still have to install sway though!
I didn't even realize he was the maintainer of sway, I was reading his stuff while using his software and didn't even know it! Try out sway, it's fantastic.
E.g. problems suspending/unsuspending the mac, problems starting/restarting machines, etc.
Digging out logs to figure out what went wrong seems to be a pain too.
How did GitHub ever get so big when this exists? Is it because GH predates SourceHut? I'm not clear on when SH came into existence, but they have entries on their blog from 2019.
> * Absolutely no tracking or advertising
> * All features work without JavaScript
> * Many features work without an account
> * The fastest & lightest software forge [links to https://forgeperf.org/ - which is very impressive]
> * 100% free and open source software
Edit: here's the actual "forge" site: https://sr.ht/
* GitHub was much earlier
* GitHub was backed by venture capital, and is now backed by Microsoft
* GitHub doesn't force you to adopt an email-only workflow. Pull request are much maligned in tech circles (following Linus Torvald's rant), but people all over dev teams love them. They are a huge argument why companies don't just stand up a git repo and call it done, devs want all those workflows and issue trackers and stuff. SourceHut is very barebones even where it has them. (The exception would be build actions, where SourceHut has no real integrations into big tools, but the core build action system is superior to GitHub's, IMO).
Incidentally, the build system depends on qemu: https://drewdevault.com/2018/09/10/Getting-started-with-qemu...
That link was very interesting, I read it all.
The footnote made me laugh, which is good, because apparently I make poor life choices, and am an embarrassment to us all. ;)