Is it possible to choose which OS will be loaded using a hardware switch?

Posted on

Problem :

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;
  load Ubuntu;

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 🙂

Solution :

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.

How do you use a test with grub to determine which OS to start?

With the standard GRUB: you do not. Technically you could modify GRUB to do this, however that might only work for one kind of motherboard and there are easier ways.

What might be easier is to actually load a third OS. E.g. a tiny Linux install with all relevant drivers. Then either chain from there try this tricks:

  • Set to boot your tiny OS (lets call it ‘chooser OS’) as default.
  • once booted in chooser read out the fan status (this is doable since at this point you have a full OS loaded, including the relevant drivers for fan speed, serial ports, etc).
  • Select the next OS to boot and adjust the bootloader.
  • Reboot.

Then, in both other OS’s add a startup script which resets the loader to the chooser OS.

Your hard which switch could switch HDDs, putting the appropriate HDD in the place the BIOS/EFI is looking to boot from.

Leave a Reply

Your email address will not be published. Required fields are marked *