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 :)

  • If you mask all the keys except for <kbd>8<\kbd> and <kbd>2</kbd>, you can use this solution for your problem – Prasanna Mar 14 '16 at 14:02
  • 1
    I have done this to switch between a primary and secondary hdd. The concept is the same. Splice the power cable, to both hdds, to the mechanical switch. When turned to A power goes to A and when turned to B power goes to B – Ramhound Mar 16 '16 at 16:40

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.

| improve this answer | |
  • It is not sarcasm, but a genuine question. So should the computer operator manual / instruction say Plug USB for Ubuntu and Do not plug USB for Windows. Just asking – Prasanna Mar 14 '16 at 13:18
  • @prasanna there is nothing forbidding you from leaving the full and complete bootloader installed on the PC as well; just make sure the default is for whatever OS the USB does not load by default. – Mahmoud Al-Qudsi Mar 14 '16 at 15:01
  • @MahmoudAl-Qudsi That's really clever and simple, definitely gonna try it! Thanks :) – gris_martin Mar 16 '16 at 23:07
  • I've managed to install the bootloaders now and it works fine, but the UEFI boot entry for the USB is removed when I detach the USB stick, and when I attach it again the entry is at the bottom. I've got the Asus Z97-A motherboard with Asus UEFI BIOS Utility. Any ideas on how to overcome this? – gris_martin Mar 21 '16 at 8:45
  • Enable both UEFI and legacy boot, giving legacy priority, and put "removable devices" above "local disks" if you have the option; then setup the usb to also boot in legacy mode. – Mahmoud Al-Qudsi Mar 21 '16 at 11:52

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.

| improve this answer | |

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.