You are not logged in.

#1 2011-04-01 11:12:53

sips
Member
Registered: 2008-07-13
Posts: 6

Grub2 chainload script

i'm creating script for generating chainload entries to /boot/grub/grub.cfg
so some testing and suggestions for optimization are welcome.

# cat /etc/grub.d/40_chainloader.sh

#!/bin/bash

blkid | sort | while read line
do
    DEV=$(echo $line | sed 's/:.*//')

    # skip if part. is mounted as root /
    [ -n "$(mount -l | grep " / " | grep "$DEV")" ] && continue
    # skip if part. hasnt boot flag (*)
    [ -z "$(fdisk -l | grep "*" | grep "$DEV")" ] && continue
    
    # vars used later
    DEV=$(echo $DEV | sed 's/\/dev\///')
    LABEL=$(echo $line | grep 'LABEL=' | sed 's/.*LABEL="//' | sed 's/".*//')
    #if [ -n "$LABEL" ]; then LABEL=" [$LABEL]"; fi
    [ -n "$LABEL" ] && LABEL=" [$LABEL]"
    FS=$(echo $line | grep 'TYPE=' | sed 's/.*TYPE="//' | sed 's/".*//')
    DEV_NR=$(echo ${DEV:2:1})
        # ex sdb6 => b
    DEV_NR=$(echo $DEV_NR | sed 's/a/0/' | sed 's/b/1/'  | sed 's/c/2/')
    DEV_NR=$(echo $DEV_NR | sed 's/d/3/' | sed 's/e/4/'  | sed 's/f/5/')
    #PART_NR=${DEV:3:1}

    # output print
    echo "menuentry \"Chainload $DEV$LABEL\" {"
    echo "    # insmod $FS"
            # insmod isnt checked (hfsplus, iso9660, ntfs ...) , so its commented out
    echo "    set root=(hd$DEV_NR,${DEV:3:1})"
    echo "    chainloader +1"
    echo "}"
done

Offline

#2 2011-04-05 00:29:35

juster
Forum Fellow
Registered: 2008-10-07
Posts: 195

Re: Grub2 chainload script

sips wrote:
    DEV_NR=$(echo $DEV_NR | sed 's/a/0/' | sed 's/b/1/'  | sed 's/c/2/')
    DEV_NR=$(echo $DEV_NR | sed 's/d/3/' | sed 's/e/4/'  | sed 's/f/5/')

You can replace this with

DEV_NR=$(echo $DEV_NR | tr abcdef 012345)

You can use here-docs instead of a bunch of echo's one after the other:

cat <<EOF
menuentry "Chainload $DEV$LABEL" {
    set root=(hd$DEV_NR,${DEV:3:1})
    chainloader +1
}
EOF

You can also write it in perl. Hah j/k.

Offline

#3 2011-04-10 12:09:56

sips
Member
Registered: 2008-07-13
Posts: 6

Re: Grub2 chainload script

Thanks!

#!/bin/bash

blkid | sort | while read line
do
    DEV_PATH=$(echo $line | sed 's/:.*//')

    # skip if partition is mounted as root /
    [ -n "$(mount -l | grep " / " | grep "$DEV_PATH")" ] && continue

    # skip if partition hasnt boot flag (*)
    [ -z "$(fdisk -l | grep "*" | grep "$DEV_PATH")" ] && continue
    
    # vars used later
    DEV=$(echo $DEV_PATH | sed 's/\/dev\///')
    LABEL=$(echo $line | grep 'LABEL=' | sed 's/.*LABEL="//' | sed 's/".*//')
    [ -n "$LABEL" ] && LABEL=" [$LABEL]"
    FS=$(echo $line | grep 'TYPE=' | sed 's/.*TYPE="//' | sed 's/".*//')
    DEV_NR=$(echo ${DEV:2:1} | tr abcdefgh 01234567)
    #PART_NR=${DEV:3:1}

    # output print
    echo "Found boot flag on $DEV_PATH$LABEL" >&2
    # insmod isnt tested (hfsplus, iso9660, ntfs ...) , so its commented out
    cat <<EOF
menuentry "Chainload 2 $DEV$LABEL" {
    # insmod $FS
    set root=(hd$DEV_NR,${DEV:3:1})
    chainloader +1
}
EOF
done

https://aur.archlinux.org/packages.php?ID=48123

Offline

Board footer

Powered by FluxBB