How can I convince Windows 7 to reboot upon hibernating rather than powering down?

I have a Linux and Windows 7 dual-boot setup on my PC and I would like to configure the OSes so that when working in one OS, I can hibernate (saving the state to disk), reboot, use the other OS, reboot again when done, and resume using the first OS from where I left off. The critical factor here is that I want each of the OSes to reboot after saving their state to disk instead of powering off; otherwise, I have to turn the computer back on in between which is quite annoying. I'm well aware that it is trivial to configure Linux to do this (see this question). However, I don't know how to tell Windows 7 that I would like it to do the same thing: namely, to reboot the PC after it finishes saving its memory contents to disk instead of powering off like it normally would in a hibernation situation.

Is there any way to do this?


Solution 1:

You can't do what you want because Windows always assumes there are no other non-Windows operating systems on your machine.

So long as the Windows bootloader (NTLDR or BOOTMGR, regardless) is installed to the MBR, (re)starting the machine after a hibernation will always resume from the hibernation data on disk and will not prompt you to select an OS to boot from. This is because mounting a volume that was mounted in the hibernated OS from any other OS or environment is undefined behavior. At the best case, when you then attempt to boot into the hibernated OS after mounting one of its volumes in Linux or another Windows install will detect the hibernation data as invalid and refuse to boot from it. In the worst case scenario, it'll boot from the hibernation store and there will be an inconsistent filesystem data in memory vs on disk and your volumes may be corrupted beyond repair (and I speak from experience on this one).

As such, Windows (somewhat rightly) assumes that restarting (instead of shutting down) after a hibernation is pointless, as it would simply result in the machine being in the same state it was before hibernation with nothing achieved and 5 minutes of your time lost.

Now, of course, if a non-BOOTMGR/NTLDR bootloader is installed to the MBR you can accomplish a hibernation of Windows and a boot of a different OS (though you must not modify any volumes that were mounted under the now-hibernated OS or else risk severe data loss). But Windows knows nothing of this (whether or not this is purposely ignored is a topic for another day). So the short answer is that we're trying to do isn't possible without a (para)virtualization environment of some sort.

If you're often switching between OSes and trying to go back to where you were, consider using VMware workstation or even ES(I|X) to boot into multiple OSes at once. Else, a KVM solution like the one @WillGunn suggested isn't a bad option.

Solution 2:

I too have been looking for a clean way to do this and haven't found one yet. I have found a bit of a hack though that others might be interested in...

The key to the hack is the xbootmgr tool from Microsoft. This tool is designed primarily for developers to gather timing information to figure out what's slowing down booting. As an added bonus, it wires the system up to automatically reboot once you hibernate. The tool is part of the Windows Performance Tools which you install via the Windows SDK. At least on my system, it takes the system about a minute to wake back up from the suspend state, which is a little annoying, but it works. YMMV.

As others have mentioned, don't try to mount your NTFS R/W in linux once hibernated.

My experience is with UEFI based booting. This theoretically should work with MBR/BIOS based bootloaders, but you'll have to use GRUB or some other non-windows bootloader in the MBR to avoid the "auto resume" feature of that bootloader (whenever it detects a hibernated system it ignores any other boot options you may have added for Linux.) If you go the grub/MBR route, take a look at the grub-reboot command to pop over to Windows for one boot cycle.

UEFI With Windows Primary

If you spend most of your time in Windows and have it set as your default boot option, you'll need a tool on the windows side to toggle your Linux install to "boot once" - something like EasyUEFI. When you want to hibernate and reboot to Linux you'll toggle it to boot once.

After toggling the boot-once flag, then as admin, run:

xbootmgr -trace hibernate

If you wired it up correctly, you should wind up in your Linux environment exactly one time, and once you reboot again, you'll find yourself back in windows, resumed from your prior hibernated state.

UEFI With Linux Primary

If you spend most of your time in Linux and have it wired up as your primary boot option, you can skip the UEFI tool on the windows side described above. You'll instead use something like efibootmgr. First you need to figure out which boot entry maps to windows. Just run

sudo efibootmgr

And you'll see something like this:

BootCurrent: 0000
Timeout: 1 seconds
BootOrder: 0000,0001
Boot0000* ubuntu
Boot0001* Windows Boot Manager

Then to set Windows to boot once, you'd use the number that matches up (0001 in my example) with:

sudo efibootmgr --bootnext 0001

Then go ahead and hibernate/reboot from Linux, and you'll land in windows once, and when you're ready to go back to Linux, run xbootmgr on the windows side to hibernate there and you can happily toggle back and forth between the two.