Cannot hook a script post VM resume from saved-state

Discussions related to using VirtualBox on Windows hosts.
Post Reply
soum
Posts: 2
Joined: 3. Feb 2016, 09:03

Cannot hook a script post VM resume from saved-state

Post by soum »

Hello,

I am working with VirtualBox 5.0.10, on a Windows 7 Host, and a Oracle Linux Guest VM (OEL 5.7).
I was trying to execute a script(test.sh) in the VM, immediately after the VM resumes (thaw) from saved-state.
Placed test.sh inside /etc/pm/sleep.d & given chmod +x permission to it.
But test.sh doesn't get executed in the VM, after resuming from saved-state.

Please let me know what is the correct way to automatically execute a script in VM , post VM thaw.

Thanks,
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Cannot hook a script post VM resume from saved-state

Post by scottgus1 »

Not a Linux guy here, so pardon the question: Is "/etc/pm/sleep.d" a place to put scripts you want to run when the OS wakes up from sleep? If so, your OS isn't waking up from Linux "sleep" when Virtualbox save-states and restarts the guest.

Save-state is a host-controlled process that the guest OS doesn't know anything about, short of possibly having a network cable unplug/replug to reset the network and the clock suddenly getting set forward rather a bit. Your Linux guest is very likely not aware it has paused, and it wouldn't recognize that it has not been running and would not have "slept" in its own fashion. So the script won't run.

I have looked in a Windows guest's Event Viewer when the guest is save-stated, and I can find nothing to indicate the Windows guest OS knows what happened to it. I assume Linux has something similar to an Event Viewer. You could check to see if Linux picks up on anything unique surrounding a save-state and trigger the script on the event. If nothing unique is evident, you could try a possible solution I discussed once with a poster who was trying to recognize a saved state.

Make a script that records the system time on a disk file when the OS boots (the boot time script). Then have another script (the time-check script) which runs every 5 seconds or so and compares the recorded time with the present system time. If more than X seconds has passed since the system time was last recorded (you determine what X should be), assume a save-state has happened and launch the save-state recovery script. The time-check script would re-record the present system time in the disk file each time it runs, after the compare, so under normal operation it would not see more than 5 or 6 seconds between runs. I see on Windows guests that the network cable unplugs & replugs when the guest save-states and restores. As an added protection against false triggers, if you can find evidence of a network reset in the Event Viewer, your time-check script could also check for such an event, so that mere delays in running the time-check script wouldn't get triggered on.
soum
Posts: 2
Joined: 3. Feb 2016, 09:03

Re: Cannot hook a script post VM resume from saved-state

Post by soum »

Hi @scottgus1,

Thanks a lot for your reply.
I am definitely trying out this approach, and would update you on the same.

Regards,
Post Reply