惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
T
Tailwind CSS Blog
罗磊的独立博客
B
Blog
博客园_首页
A
About on SuperTechFans
有赞技术团队
有赞技术团队
V
V2EX
U
Unit 42
I
InfoQ
IT之家
IT之家
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
H
Help Net Security

Danb Blog

August 2026: What I've Been Working On · Danb Blog Great Tech: Intel 2500k CPU · Danb Blog July 2026: What I've Been Working On · Danb Blog My Experience with Ubuntu Desktop 26.04 · Danb Blog Thumbnails for Orca 3MF in GNOME Files · Danb Blog Cheaper Sodastream Gas with a Big Tank · Danb Blog June 2026: What I've Been Working On · Danb Blog Running Snyk On Forgejo/Codeberg Actions · Danb Blog Tuta & Proton: An Open Source Client Does Not Result in an Open Source Service · Danb Blog May 2026: What I've Been Working On · Danb Blog 3D Printing Parts for my Greenhouse, Round 2 · Danb Blog Great Devices: Nexus 7 2013 · Danb Blog R36S Console RetroArch Stuck Menu Issue · Danb Blog April 2026: What I've Been Working On · Danb Blog Basic OpenCode Sandboxing with Docker · Danb Blog Games Played in 2025 · Danb Blog March 2026: What I've Been Working On · Danb Blog Shivam Mathur's "node-docker" Images are Awesome for PHP CI · Danb Blog February 2026: What I've Been Working On · Danb Blog Your BookStackApp project was assigned as a project for my Software Architecture course · Danb Blog My Sovol SV08 3D Printer Setup in 2026 · Danb Blog January 2026: What I've Been Working On · Danb Blog Epson Printer: The administrator password you entered was not recognized · Danb Blog Pressure to Follow Process · Danb Blog Online Shopping Email Notifications · Danb Blog My HomeLab Setup in 2026 · Danb Blog Linux Label Printing with the Phomemo D30 · Danb Blog Reporting to the CMA: Google Android Developer Verification · Danb Blog OPNsense & Dnsmasq: Responding with specific DNS servers · Danb Blog An Impromptu Introduction to ZFS Drive Replacement at 1am · Danb Blog
Controlling Default GRUB Boot Option via USB · Danb Blog
2023-07-24 · via Danb Blog

On my main desktop PC I primarily use Linux (Currently Fedora 38) but I do dual-boot with Windows for testing or for some games that don’t work quite right in Linux. One thing I’ve always wanted is some level of external physical control over which OS will be used on start-up. If I’m doing something that requires restarts, it’s annoying to be vigilant during boot to choose the right OS, and sometimes I like to switch on my PC then do something else like make a cup of tea (even though boot only takes like 10 seconds).

In searching, I came across this hackaday article which uses a proper hardware switch along with a microcontroller board. I didn’t have a good switch on-hand, but looking at the grub code of the mentioned project led me to a simpler option. The code used the GRUB search command which can essentially allow you to conditionally check if a filesystem exists.

Using this I put together a simplistic solution that changes the default GRUB entry depending if you have a specific USB storage drive inserted during boot:

search --no-floppy --fs-uuid --set usbswitch B2B0-DC58

if [ "$usbswitch" ] ; then
  set default=4
fi

That’s the GRUB2 config code. There’s a couple of bits to configure for your own use-case:

  • Change the B2B0-DC58 ID to be a filesystem UUID on your own USB storage drive.
    • You can get filesystem info, with UUID shown, via:
    • lsblk --output=NAME,MOUNTPOINTS,SIZE,UUID
  • Change the 4 in set default=4 to be the boot option you want to be default when the USB is plugged in.
    • They are usually shown in order in the grub menu, with 0 being the top option.

Usage in Fedora

This is how I added the above config on my Fedora 38 system which is using EFI boot:

I created a /etc/grub.d/50_usb_selector file with the below contents:

#!/usr/bin/sh
#
# Changes default boot option if specific USB filesystem is plugged in.
# Change the ID at the end of the search command below to be the
# UUID of the filesystem on the USB.
# Change the 4 at the end of the set command to change the boot entry
# that should be made default when the USB is plugged in.
cat <<'EOF'
search --no-floppy --fs-uuid --set usbswitch B2B0-DC58

if [ "$usbswitch" ] ; then
  set default=4
fi
EOF

Then I rebuilt the grub config via: grub2-mkconfig -o /boot/grub2/grub.cfg (following guidance from the Fedora docs).

Building on the idea

I think it should be simple to evolve this into a hardware switch, without getting a custom microcontroller board involved. We could just do what’s done here but on a switchable USB extension (with data support).

While properly exploring grub for the first time, I noticed it also has some networking commands, including one to perform a DNS lookup. This got me wondering if I could control the boot option via providing a specific DNS response from elsewhere on my network, so I could maybe configure the current boot option via software remotely, like via Home Assistant for example. Would need to dig into use of this command and the specific of DNS to validate this though.