zed.0xff.me

grub2 simple hardware boot switch

1. take any old unused USB stick (any size, 16M is more than enough, mine was 128M)

#fdisk -l /dev/sdc
Disk /dev/sdc: 126 MB, 126353408 bytes
16 heads, 32 sectors/track, 482 cylinders, total 246784 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x59d30b2a

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1   *          32      246271      123120   83  Linux

2. [optional] format it to ext2

3. find out its UUID

#blkid /dev/sdc*
/dev/sdc1: UUID="3a5ef90f-0640-42c3-97c2-8743c822ba60" TYPE="ext2"

4. do some simple grub2 scripting:

menuentry "auto" {
        set foo_uuid=3a5ef90f-0640-42c3-97c2-8743c822ba60
        set foo=empty

        insmod part_gpt
        insmod ext2
        insmod search_fs_uuid

        search --fs-uuid --no-floppy --set=foo $foo_uuid

        if [ $foo = "empty" ]; then
                # boot windows
                insmod fat
                insmod chain
                search --fs-uuid --no-floppy --set=root B0D7-DA71
                chainloader (${root})/efi/Microsoft/Boot/bootmgfw.efi
        else
                # boot linux
                set root='(hd0,gpt1)'
                search --no-floppy --fs-uuid --set=root 22c0603d-670e-4097-83d2-539b520fc75a
                linux /boot/vmlinuz root=/dev/sda1
        fi
}

So, if this (and exactly this, with unique UUID) USB stick is plugged in while computer boots – it will boot Linux, otherwise Windows will boot.

Post a comment


(lesstile enabled - surround code blocks with ---)