A guest reboot and a guest power-off end the VM the same way: the process exits with 0.
- HVF: PSCI
SYSTEM_OFFandSYSTEM_RESETboth returnVcpuExit::Shutdown(hvf/src/lib.rs#L639-L644), which exits withFC_EXIT_CODE_OK(macos/vstate.rs#L485-L488). - KVM:
KVM_SYSTEM_EVENT_RESETandKVM_SYSTEM_EVENT_SHUTDOWNboth becomeVcpuEmulation::Stopped(linux/vstate.rs#L1566-L1573). - x86: the i8042 reset is the exit event, and it exits with
FC_EXIT_CODE_OK(vmm/mod.rs#L457-L480).
A program running a long-lived guest (a desktop VM through krunkit, in my case) can't tell whether to start the VM again when the process exits, so after a guest reboot its user has to start it by hand.
Giving reset its own exit code by default would break libkrun's init. It ends every workload with reboot(RB_AUTOBOOT) (exec.rs#L107-L110), and reports the workload's exit code with an ioctl only when / is virtio-fs (exec.rs#L65-L76). With a block-device root, the vCPU's exit code becomes the process's, so a new default would turn those 0s into the new code.
Proposal: opt-in. krun_set_reset_exit_code(ctx_id, code) sets the process exit code for a guest reset; the default stays as it is. The exit code the guest sets through the ioctl still takes precedence. HVF, KVM and the i8042 path would all use it. krunkit would get a flag to pass it through.
Tested: macOS, v1.19.5, with a patch mapping SYSTEM_RESET to 32 (crosvm's VmReset): systemctl reboot in a Fedora guest exits 32, and systemctl poweroff exits 0.
Would this shape work? Or would you rather have something else, such as a callback, or a different default where no init is in use?
Activity
3Thanks! Since main is the 2.0 builder API, I'll add krun_vmm_builder_reset_exit_code(builder, code) there, and backport it to 1.19.x as krun_set_reset_exit_code(ctx_id, code) so krunkit can use it. Sound good?
The patch I tested with, for reference: willie/libkrun@cbbca3a2 on v1.19.6. It's the quick version (HVF only, reset always exits 32), not the opt-in API proposed above.
Sounds reasonable to me.