Virtualbox, headless vm, clean stop when shutdown pc, savestate failure

Vbox 6.1.18

I created a vm.
I use vbox autostart service to start the vm with headless at boot. All runs well.

I want a clean shutdown of the vm when I shut down my pc. for this i executed


VBoxManage modifyvm <vm> --autostop-type savestate

if i manually stop the autostart service then a snapshot is created in ~/Virtualbox VMs/<vm>/Snapshots/

if i manually start the autostart service then the vm is started using the snapshot and snapshot is deleted.

if i shut down my pc then no snapshot is created. I can check this using another os.

I assume that the vm is my property and some rights must be set to allow the system making a snapshot or something else.

Hi,

I have not used the default autostart service for Vbox for a while now, but looking at the unit file there is nothing about Before. Only After entries.

[Unit]
SourcePath=/usr/lib/virtualbox/vboxautostart.sh
Description=VirtualBox Linux autostart module
Requires=vboxdrv.service
After=vboxdrv.service
After=network.target
After=time-sync.target
After=nss-user-lookup.target

[Service]
ExecStart=/usr/lib/virtualbox/vboxautostart.sh start
ExecStop=/usr/lib/virtualbox/vboxautostart.sh stop
Type=oneshot
RemainAfterExit=yes
TimeoutStopSec=0

[Install]
WantedBy=multi-user.target

My own autostart service (unit file) has a Before entry.

Before=shutdown.target halt.target runlevel2.target

It works as expected and snapshot(s) are created during shutdown, restart and suspend to disk and ram. The only caveat I have is failing to unmount home, which is printed to the screen during shutdown.
But like I’ve said I have not used other autostart service for quite a while now so, I’m not really sure if that is the reason why there is no snapshot created when shutting down.

If you have the time/motivation to investigate just enable persistent logging and then check the previous logs.

journalctl -b 1 --all

Where 1 is the previous log, it can be any number if it exists.

I would not advise creating snapshots, if diskfiles are corrupted a single diskfile is relatively easy for a recovery attempt(Keep in mind, damaged is damaged and unlikely not recoverable). Multiple files for any reason including snapshots makes recovery incredibly difficult and increases chances recovery will be unsuccessful.

Far better is to execute a proper shutdown and later restart. With no snapshot. If you want a better recovery solution, copy the diskfile instead after shutdown or do a conventional backup system.

IMO,
TSU

Then how do you suppose to restore a vm fro a ‘save state’ if there is no snapshot? Also Vbox creates the snapshot automagically when saving state (at least that is the default) , not the user per se, so the user has no control over that…

Hello thanks for your suggestions

I must complete the definition of the problem.

The VM is an android-x86 8.1-r5 (http://android-x86.org). I want to use android with my PC because it is more comfortable to use a few applications I use frequently.

A good solution to stop cleanly android is to send an acpi event. Just replace “savestate” by “acpishutdown”.

But if you do this then you don’t get a shutdown but a menu where to choose by clicking the command “stop”.

There is no way to execute a local or an android script by using vboxmanage because there are no guest additions for android. Perhaps there is a way by modifying the menu, so the stop is executed by default after 10 s. It is too techie for me. I don’t know how to do this.

The only solution is to use “savestate”.

Now I know using headless feature with vbox auto service is perhaps a big solution for a little problem.

I found a light solution in plasma environment.

A script to shut down vm with savestate, executed when I log out, or I shut down.
A script to launch the snapshot, executed when I log in.

  • yes I know a snapshot is one file. It is risky. If it is corrupt I lost all. But no data are stored in this vm just a few apps. If there is a problem I just stop the vm with vboxmanage thus nothing is recorded in disk file. And I use the disk file. This disk file is not a recent state, but it is not a problem. In the worst case recreating a vm is a matter of 15-30 mn.

Hi
Maybe switch to qemu as a solution (or libvirt and virsh)?

Took about 3 minutes to use qemu… (although no sound, needs other tweaks)


Create image...

qemu-img create -f qcow2 cm-x86-14.1-r5-k419.img 10G

Create bash script

#!/usr/bin/bash

qemu-system-x86_64 \
-enable-kvm \
-m 2048 \
-smp 2 \
-cpu host \
-device virtio-mouse-pci -device virtio-keyboard-pci \
-serial mon:stdio \
-boot menu=on \
-net nic \
-net user,hostfwd=tcp::5555-:22 \
-device virtio-vga,virgl=on \
-display gtk,gl=on \
-hda cm-x86-14.1-r5-k419.img \
-cdrom cm-x86-14.1-r5-k419.iso

Uses a virtual wifi connection in the qemu device and host nic… use qemu to create a snapshot as well…

https://forums.opensuse.org/attachment.php?attachmentid=992&stc=1

Screenshot from 2021-03-16 11-28-40.png

@https://forums.opensuse.org/image.php?u=740&dateline=1502045882](https://forums.opensuse.org/member.php/740-malcolmlewis) malcolmlewis](https://forums.opensuse.org/member.php/740-malcolmlewis)

My problem is how to send a command as something as “shutdown -now” to android vm.

I am a very very beginner with qemu.

Does headless vm exist ?

Can I use rdesktop to connect to the vm ?

Can I send a command or script to the vm for executing a “shutdown - now” for example ?

If I keep the snapshot solution : can I send a command to sync time to the android vm ?

Can I share dynamically a webcam and its microphone ? I mean if in the vm no app is using the webcam then the webcam is released and captured by the host.

if yes for the two last question then i am ready to test qemu because there is no solution with Vbox because there is no guest additions for android.

Thanks

Hi
Normally the usb devices are allocated to the vm for the duration of running… so that would be a no… you could probably unbind, but that would take some experimenting…

For headless, yes, rdesktop, I imaging so. Time, yes, just configure (I use -rtc clock=host,base=utc).

Look at virt-manager and full control from the command line with virsh which can control the virtual machine.

And it automatically saves VM state during shutdown?

Uses a virtual wifi connection in the qemu device

What is it? I am not aware of working emulation of wifi for QEMU.

Hi
Pause is an option in the menu I see…

Identified as virtwifi on android…


-net nic \
-net user,hostfwd=tcp::5555-:22 \

https://forums.opensuse.org/attachment.php?attachmentid=995&stc=1

Screenshot from 2021-03-16 15-04-00.png

Sigh … the question was how to do it automatically on host shutdown

if i shut down my pc then no snapshot is created
If you had actually read OP question you would have seen that it is perfectly possible to manually save VM state using VB. So how exactly is your “solution” an improvement?

Identified as virtwifi on android…

Must be android speific.

Hi
It’s not, it’s an alternative, like I indicated use virsh/virtual manager, not qemu.

As I described, shutdown and poweroff your virtual machine.
State is then in a completely consistent state for whenever it’s booted up again.
And, of course if your system is powered off, snapshots aren’t going to make a bit of difference since the snapshot should be empty.

Snapshots should be used for checkpointing within your running session,
When things work right, you won’t have problems restoring a snapshot, maybe even 99% of the time.
It’s that tiny percentage of times when you could suffer disk corruption (yes, it does happen. data storage can never be expected to be perfect) in which case you really want to maximize your odds of success.

From the Voice of Experience,
TSU

Powering off a virtual Android device is an interesting thought.
If it was a physical device, you just hit the power button, and it commences an orderly shutdown.
As you probably know, Android is meant for mobile devices where a soft poweroff only shuts down the display and the device continues to run and you have to execute a hard shutdown to really poweroff.

When I wrote Android apps and tested them running in an emulator, I remember there was no thought or concern about an orderly shutdown, you simply powered off without a second thought.
The question is what might happen if you’re running Android as a Server with potentially long running processes which can be considered likely contrary to its design.
Viewing what is posted on the Internet, it seems that an ACPI shutdown should initiate an orderly shutdown and not simply poweroff immediately like what would normally happen with normal x86/x64 OS. This seems likely based on my experience with real, physical Android devices… When I hit the power button to shut completely off, it can take several minutes to poweroff completely.

Some links to consider
Graphically shut down your Android
https://liucs.net/cs164s13/x86android.html
Suggests simply executing the ACPI poweroff command should invoke an orderly shutdown
https://groups.google.com/g/android-x86/c/pEI7xAnOpNY?pli=1

The question probably can be answered with some simple experimentation.

Besides,
An important issue may be that Android is still a SysVinit system and not systemd, so it’s possible the OS has absolutely no concept of state and running processes during a shutdown.
Although not perfect, systemd solves some problems by its support for hierarchical processes… In SysVinit there is no concept by default, so unless you script code to intelligently check for processes still running, the default is to just kill whatever is happening. This is why, to provide minimal protection the shutdown command used to impose a delay to hopefully allow long running processes to end, but it was completely blind and could just as easily wait far longer than necessary or not long enough.

TSU

@tsu2

Thanks for your suggestion about powering off a virtual Android device.

I will try it