machyve / xhyve

xhyve, a lightweight OS X virtualization solution
Other
6.44k stars 356 forks source link

Best way to shutdown VM? #209

Open kanav99 opened 3 years ago

kanav99 commented 3 years ago

I am writing a wrapper over xhyve in swift, where I have given setuid to the xhyve binary (!!) and run the Ubuntu VM on a button click. Now I want to shut down the VM. Options -

  1. Make a root user and set its ssh shell to poweroff. But this can be exploited by any other applications.
  2. Run kill - nope, because I don't want to give setuid to a binary that can kill
  3. SIGINT - Not graceful
  4. Simulating Ctrl + SysRq + o - how do I do this?

I want to do a safe shutdown - what should be the best way?

TimDaub commented 3 years ago

I'm also wondering how to do this. I usually use halt when I was successful in booting the machine. But for machines that are broken, I always have to shut down the process manually somehow.

jacobvosmaer commented 3 years ago

Inside a VM I use halt. Outside the VM, you can use SIGTERM. That gets translated into a "power button" press, and that typically triggers an OS shutdown.

https://github.com/machyve/xhyve/blob/83516a009c692ea5d2993d1071e68d05d359b11e/src/pm.c#L273

Note that if the xhyve process runs as root, you need to also send SIGTERM as root.