Choosing OS to Load Using a Hardware Switch

bootfangrubmulti-boot

I'm looking for a more elegant way of choosing which OS to boot than using the menu provided by grub, and I think it would be neat to have a switch on the case with two states (e.g. Ubuntu and Windows), that decide which OS will be loaded. I've been doing some thinking, and it seems to me like you should be able to solve it by hooking the switch to a PWM generator, and connect the generator to a fan speed input pin. Then from grub read the fan speed, and do a conditional test that determines which OS to load.

In pseudocode:

if (fan.speed > threshold) then
  load Windows;
else
  load Ubuntu;
end

My two questions are therefore:

  1. How do you use a test with grub to determine which OS to start?
  2. How can you read the fan speed from grub during startup?

Other solutions and ideas are of course welcome as well 🙂

Best Answer

You're going to need to write your own grub2 module, interface with the BIOS to get the raw fan speed information, and use that to interpret and process the results. Obviously GRUB doesn't know or care about your fan speed and you won't be able to do it natively without writing some code.

A better solution would be to install the bootloader (just the bootloader, nothing more) to a USB stick. You can use GRUB2 or BOOTMGR - it doesn't matter.

The PC is then configured to load from USB first, and when this USB stick is inserted in the PC, it will boot into Ubuntu; whereas the local disk is configured to boot into Windows. i.e. the presence or absence of the USB will determine which OS your PC boots into.

At that point, it's a simple matter of using a modified USB cable with a switch on one of the VCC lines. If it's active, the USB stick will appear to the BIOS and it will boot from the USB's copy of GRUB booting into Ubuntu. If the switch is off, the PC will load "normally" from the local disk, booting into Windows.

You are of course free to disguise the USB stick, the modified USB cable, and the VCC switch inside a box as a single, standalone unit. It's way cooler than your PWM idea because you literally just stick it in your USB port and you're good to go.

Related Question