locked
Simulating power failure in VM RRS feed

  • Question

  • I'm working on power failure consistency. This is very hard to test and debug for what should be obvious reasons.

    Running my code in a VM helps because I can hit the "power off" button on the Hyper-V management window frame. However, that doesn't allow me to specify where in my code I want the power failure event to occur, which is what I really need.

    Is there a way in a VM to tell the hypervisor to simulate a power failure, i.e., do the same thing as hitting the "stop" button, at a specific point in code? Maybe executing a particular illegal instruction would do it?

    If not, that would be a wonderful addition to Hyper-V because it would make testing power failure scenarios much easier.

    Thanks in advance for any assistance.

    Saturday, July 4, 2020 12:42 PM

All replies

  • If you pull the plug or forcefully power off the VM then the VM (and host) just stops and no additional code is going to run. A better solution may be to invest in a UPS

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Saturday, July 4, 2020 12:51 PM
  • What I'm trying to do is to test my code that is intended to recover from a power failure.

    That's why I'm trying to simulate a power failure in a VM.

    Saturday, July 4, 2020 2:47 PM
  • Sounds good, puling the plug from any running instance of windows is not recommended. Better options may be a UPS. To the code I'd suggest making it work regardless of how or why windows starts up, or trigger on the 6008 system event if needed. Development questions are better raised somewhere on MSDN, possibly over here.

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=windowsgeneraldevelopmentissues

     

    (please don't forget to mark helpful replies as answer)

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Saturday, July 4, 2020 3:33 PM
  • What happens with SQL Server if the power fails suddenly? I assume they have to maintain data consistency even in that case.

    The same is true of the code I'm developing.

    So I'd appreciate an answer to the question I asked about whether there is a way to trigger a simulate power outage in a Hyper-V VM. If the answer is "no", okay. But that is my question.

    Saturday, July 4, 2020 3:49 PM
  • For help with SQL server I'd reach out to subject matter experts in dedicated forums over here.

    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/home?category=sqlserver

    as to simulating power outage of the VM pressing the stop button should work. If further assistance were needed you can start a case here with product support

    https://support.microsoft.com/en-gb/hub/4343728/support-for-business

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Saturday, July 4, 2020 3:57 PM
  • Yes, I noted in my original post that hitting the stop button works, but can't be synchronized to a particular place in my code.

    I believe I have two incidents included in my Visual Studio Professional subscription, so I'll open a case.

    Thanks.

    Saturday, July 4, 2020 4:49 PM
  • Sounds good then. 

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Saturday, July 4, 2020 4:58 PM
  • No, they said I had to come back here.

    Could you forward my question to someone who can answer whether there is a way to trigger a power-down situation from inside a Hyper-V VM?

    Thanks.

    Saturday, July 4, 2020 5:06 PM
  • What I'm trying to do is to test my code that is intended to recover from a power failure.

    That's why I'm trying to simulate a power failure in a VM.

    Hello technovelist,

    Is it possible for you to insert additional code to send a visual or auditory alert at the point where you want the dirty shutdown to occur so that you know when to flip the switch? 


    Miguel Fra
    Falcon IT Services
    https://www.falconitservices.com

     

    Saturday, July 4, 2020 5:15 PM
  • You can start a case here with product support

    https://support.microsoft.com/en-gb/hub/4343728/support-for-business

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Saturday, July 4, 2020 5:16 PM
  • By definition, a power failure is an external event. Anything done from within the VM does not qualify. The stop button that you already found is the only accurate way to simulate a power failure.

    If you're asking if there is a way to send a message from within the virtual machine to the hypervisor that can tell it to issue a force stop, then yes. If you have network connectivity from the VM to the host, you can issue a PowerShell command to force stop the VM. This is the simplest path.

    I highly recommend using shell access from your testing program to run the above PowerShell. If you simply must use code, then the RequestStateChange and RequestStateChangeEx methods of the Msvm_ComputerSystem WMI class are the underlying API calls used by that PowerShell. Fair warning that WMI/CIM programming is non-trivial and absolutely not for the faint of heart.

    Hyper-V does have some limited ways to communicate from within the VM to the hypervisor over VMBus. To the best of my knowledge, a hard shut down is not in the exposed commands, though. If you're really ambitious, you can write your own interface. This is the hardest path.

    A compromise is to use software inside the guest to trigger a failure. But, since we all live in protected mode now, any such request must go through the active operating system. Therefore, the operating system will be aware that something is happening. So, this is, at best, an approximation. Assuming Windows as your guest OS, call the KeBugCheck system API with a hex value of E5. I have never personally done this. Calling Windows APIs is usually very easy, but this is intended for kernel-mode programs so maybe it's hard.


    Eric Siron
    Altaro Hyper-V Blog
    I am an independent contributor, not an Altaro employee. I accept all responsibility for the content of my posts. You accept all responsibility for any actions that you take based on the content of my posts.


    Saturday, July 4, 2020 5:44 PM
  • What I'm trying to do is to test my code that is intended to recover from a power failure.

    That's why I'm trying to simulate a power failure in a VM.

    Hello technovelist,

    Is it possible for you to insert additional code to send a visual or auditory alert at the point where you want the dirty shutdown to occur so that you know when to flip the switch? 


    Miguel Fra
    Falcon IT Services

     

    Good idea! I should be able to add a statement that is waiting for input, then kill the VM.

    Of course that won't keep the (simulated) CPU caches from being flushed but at least I'll be able to say "it got here, so where is the data that was supposed to be preserved?"

    Saturday, July 4, 2020 5:56 PM
  • Thanks. I went there but can't find any appropriate category for my question.
    Saturday, July 4, 2020 5:57 PM
  • What I'm trying to do is to test my code that is intended to recover from a power failure.

    Hi Technovelist,

    It's good that you do this (plan for contingencies) especially when events are outside your control... like end users of your program not buying a battery backup!


    Miguel Fra
    Falcon IT Services
    https://www.falconitservices.com

     

    Saturday, July 4, 2020 6:59 PM