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

5

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

gris_martin

Posted 2016-03-14T12:34:47.510

Reputation: 85

If you mask all the keys except for <kbd>8<\kbd> and <kbd>2</kbd>, you can use this solution for your problem

– Prasanna – 2016-03-14T14:02:06.617

1I 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 – 2016-03-16T16:40:25.523

Answers

4

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.

Mahmoud Al-Qudsi

Posted 2016-03-14T12:34:47.510

Reputation: 3 274

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 – 2016-03-14T13:18:58.910

@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 – 2016-03-14T15:01:32.600

@MahmoudAl-Qudsi That's really clever and simple, definitely gonna try it! Thanks :) – gris_martin – 2016-03-16T23:07:36.770

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 – 2016-03-21T08:45:15.600

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 – 2016-03-21T11:52:49.193

0

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.

Hennes

Posted 2016-03-14T12:34:47.510

Reputation: 60 739

0

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

iAdjunct

Posted 2016-03-14T12:34:47.510

Reputation: 1 570