Page 1 of 27 12311 ... LastLast
Results 1 to 10 of 263

Thread: Looking for Grub and Windows bootloader in all partitions.

  1. #1

    Default Looking for Grub and Windows bootloader in all partitions.

    (split from Two major problems with dual boot installation of openSuSE 11.1 and Windows 7)

    Quote Originally Posted by Carlos E. R. View Post
    Thinking.... I would perhaps do a for loop on /dev/sd*, checking for strings (is the "strings"program, or something else, always installed?)
    I thought it would be enough to look for GRUB, NTLDR and BOOTMGR, since Grub (legacy and Grub2) contains 'GRUB' in its stage1, Windows from NT to Vista 'NTLDR' and Windows 7 'BOOTMGR' in their bootcode. I reallly wanted to check those strings only (explaining why I didn't use a case insensitive grep). But I must be missing a string for Windows 7 since the script couldn't find it on James's sda1

    I changed the default behaviour to a loop on /dev/sd* rather than /dev/sda. You can still give a specific disk as argument.

    Code:
    #! /bin/bash
    
    devs=(`fdisk -l | sed -n 's|^Disk \(/dev/[hs]d[a-z]\):.*|\1|p'`)
    
    [ "$1" ] && devs=(/dev/$1) 
    
    for dev in ${devs[*]} ; do
            if [ -b $dev ] ; then
                    dd if=$dev bs=512 count=1 2> /dev/null | grep -q GRUB && echo "Grub found in MBR on $dev"
                    for pt in `fdisk -l $dev | awk '/^\/dev/ { print $1 }'` ; do
                            dd if=$pt bs=512 count=1 2> /dev/null | grep -q GRUB && echo "Grub found in $pt"
                            dd if=$pt bs=512 count=1 2> /dev/null | grep -q NTLDR && echo "NT/XP/VISTA Loader found in $pt"
                            dd if=$dev bs=512 count=1 2> /dev/null | grep -q BOOTMGR && echo "Windows 7 Loader found in $pt"
                    done
            fi
    done
    Quote Originally Posted by Carlos E. R. View Post
    Your script, if it can be made to check all cases, can be a very useful tool. It should print all the things we usually ask.
    That's what I thought. But I wanted to focus on bootsectors. Maybe this code could be used as a function in a wider script to display other infos as well.

  2. #2
    Join Date
    Mar 2010
    Location
    Austin - Texas
    Posts
    10,500
    Blog Entries
    48

    Smile Re: Looking for Grub and Windows bootloader in all partitions.

    So, I have made some changes to the program. I am happy to report that it is finding my grub now, but it does not find the Windows 7 partition.

    On the script file, I suggest it be placed in the ~/bin folder (~ = your home folder) and made executable there as the file findgrub:

    Code:
    #!/bin/bash
    
    #: Title       : findgrub
    #: Date Created: Sun Sep 26 19:40:40 CDT 2010
    #: Last Edit   : Mon Sep 27 18:07:40 CDT 2010
    #: Author      : please_try_again & edited by j McDaniel
    #: Version     : 1.00
    #: Description : Locates the Grub Boot Loader
    #: Options     : name of drive, ex: sda
    
    #
    # Check to see if we are root
    #
    
    if [[ $UID -ne 0 ]]; then
      echo "Root User Permissions are required, Please Enter the ..."
      echo
      sudo $0 $1
      exit 0
    fi
    
    echo "Find Grub Version 1.0 : Syntax: findgrub [sdx] : x = a,b or c etc."
    echo
    
    devs=(`fdisk -l | sed -n 's|^Disk \(/dev/[hs]d[a-z]\):.*|\1|p'`)
    
    [ "$1" ] && devs=(/dev/$1) 
    
    for dev in ${devs[*]} ; do
            if [ -b $dev ] ; then
                    dd if=$dev bs=512 count=1 2> /dev/null | grep -q GRUB && echo "Grub found in MBR on $dev"
                    for pt in `fdisk -l $dev | awk '/^\/dev/ { print $1 }'` ; do
                            dd if=$pt bs=512 count=1 2> /dev/null | grep -q GRUB && echo "Grub found in partition $pt"
                            dd if=$pt bs=512 count=1 2> /dev/null | grep -q NTLDR && echo "NT/XP/VISTA Loader found in $pt"
                            dd if=$dev bs=512 count=1 2> /dev/null | grep -q BOOTMGR && echo "Windows 7 Loader found in $pt"
                    done
            fi
    done
    
    echo
    echo "findgrub search is complete..."
    echo
    
    exit 0
    # End Of Script
    To make the file executable the following terminal command:

    Code:
    chmod +x ~/bin/findgrub
    To use findgrub, open up a terminal session and type the terminal command of findgrub. The root user password will be requested and the the applications will be run. When I run the program with no parameters it says:

    Code:
    james@LinuxUser:~> findgrub
    Root User Permissions are required, Please Enter the ...
    
    root's password:
    
    Find Grub Version 1.0 : Syntax: findgrub [sdx] : x = a,b or c etc.
    
    Grub found in partition /dev/sdb2
    
    findgrub search is complete...
    This is correct for my openSUSE drive. Here are two manual commands on my Windows 7 drive, which is not found.

    Code:
    dd if=/dev/sda bs=512 count=1
    3�м|��ؾ|�����Ph������~|
                           ������▒�VU�F�F�A��U�]r��U�u      ��t�Ff`�~t&fhf�hh|hh�B�V����������|�V�v�N�n�fas�Nu
                                                                                                              �~�������U2��V�]랁>�}U�un�v��u����d�����`�|���d�u����▒f#�u;f��TCPAu2��r,fh�fhffSfSfUfhfh|fah�▒Z2��|�▒�������2����<t ��������+��d�$��$�Invalid partition tableError loading operating systemMissing operating systemc{�    ��L� !��x��U�1+0 records in
    1+0 records out
    512 bytes (512 B) copied, 1.78e-05 s, 28.8 MB/s
    Code:
    dd if=/dev/sda bs=512 count=1
    3�м|��ؾ|�����Ph������~|
                           ������▒�VU�F�F�A��U�]r��U�u      ��t�Ff`�~t&fhf�hh|hh�B�V����������|�V�v�N�n�fas�Nu
                                                                                                              �~�������U2��V�]랁>�}U�un�v��u����d�����`�|���d�u����▒f#�u;f��TCPAu2��r,fh�fhffSfSfUfhfh|fah�▒Z2��|�▒�������2����<t ��������+��d�$��$�Invalid partition tableError loading operating systemMissing operating systemc{�    ��L� !��x��U�1+0 records in
    1+0 records out
    512 bytes (512 B) copied, 2.0506e-05 s, 25.0 MB/s
    Hope this information is helpful. I put in enough instructions so all users could follow along if they wish to do so.
    Thank You,
    My Blog: https://forums.opensuse.org/blogs/jdmcdaniel3/

    Software efficiency halves every 18 months, thus compensating for Moore's Law

    Its James again from Austin, Texas

  3. #3

    Default Re: Looking for Grub and Windows bootloader in all partitions.

    You did read and post twice the MBR of sda. The most interesting ouput would be the bs in /dev/sda1 (if it is your Windows 7 partition), so:

    dd if=/dev/sda1 bs=512 count=1

    This should show which string Windows 7 uses in its bootcode (last time I checked it was BOOTMGR).

  4. #4
    Join Date
    Mar 2010
    Location
    Austin - Texas
    Posts
    10,500
    Blog Entries
    48

    Smile Re: Looking for Grub and Windows bootloader in all partitions.

    I can only say please_try_again, thanks for hanging in there with me. It is the details that kill you and me.

    Code:
    sudo dd if=/dev/sda1 bs=512 count=1
    �R�NTFS   �?���w��
                      ����^��^��3�м|�h�hfˈf�>NTFSu�A��U�r
                                                         ��U�u��u����▒h▒�H�������▒�Xr�;
                                                                                       u�.Z3۹ +�f�����K+�w����▒f#�u-f��TCPAu$��rh�hph       fSfSfUh�fa�▒3�(������_��f`f�ffhfPShh�B����fY[ZfYfY�f����u�fa��       ����������<t    ������
    A disk read error occurred
    BOOTMGR is missing
    BOOTMGR is compressed
    Press Ctrl+Alt+Del to restart
    ����U�1+0 records in
    1+0 records out
    512 bytes (512 B) copied, 2.0948e-05 s, 24.4 MB/s
    It is kind of funny that it says the BOOTMGR is missing & the BOOTMGR is compressed at the same time.

    Thank You,
    My Blog: https://forums.opensuse.org/blogs/jdmcdaniel3/

    Software efficiency halves every 18 months, thus compensating for Moore's Law

    Its James again from Austin, Texas

  5. #5

    Default Re: Looking for Grub and Windows bootloader in all partitions.

    Whether BOOTMGR is missing or compressed, this is the string the script was looking for in this bootsector. So I don't quite understand why it didn't also display:
    Windows 7 Loader found in /dev/sda1

  6. #6
    Join Date
    Mar 2010
    Location
    Austin - Texas
    Posts
    10,500
    Blog Entries
    48

    Default Re: Looking for Grub and Windows bootloader in all partitions.

    So, I surely don't know what is wrong, but I notice that $dev=/dev/sda (where you are looking) while $pt=/dev/sda1 (which you say it was found). count 1 2, does this read in one or two sectors?

    Code:
    dd if=$dev bs=512 count=1 2> /dev/null | grep -q BOOTMGR && echo "Windows 7 Loader found in $pt"
    So, for some reason, it just does not like my Windows drive, even as it seems some output should come out. Humm....

    Thank You,
    My Blog: https://forums.opensuse.org/blogs/jdmcdaniel3/

    Software efficiency halves every 18 months, thus compensating for Moore's Law

    Its James again from Austin, Texas

  7. #7

    Default Re: Looking for Grub and Windows bootloader in all partitions.

    Quote Originally Posted by jdmcdaniel3 View Post
    So, I surely don't know what is wrong, but I notice that $dev=/dev/sda (where you are looking) while $pt=/dev/sda1 (which you say it was found). count 1 2, does this read in one or two sectors?
    It reads 1 sector. Reading 2 sectors would be 'count=2'.
    2 > /dev/null redirects standard error to nowhere, meaning we're not interesting at reading dd extra output.
    You can see the difference if you compare the output of :
    dd if=/dev/sda1 bs=512 count=1
    with the one of
    dd if=/dev/sda1 bs=512 count=1 2> /dev/null
    In the later case, it doesn't display a similar message at the end:
    Code:
    1+0 records out
    512 bytes (512 B) copied, 6.3077e-05 s, 8.1 MB/s
    You can append '2> /dev/null' to any command if you're not interesting in warnings or error messages.

  8. #8

    Default Re: Looking for Grub and Windows bootloader in all partitions.

    Quote Originally Posted by jdmcdaniel3 View Post
    So, I surely don't know what is wrong, but I notice that $dev=/dev/sda (where you are looking) while $pt=/dev/sda1 (which you say it was found). count 1 2, does this read in one or two sectors?

    Code:
    dd if=$dev bs=512 count=1 2> /dev/null | grep -q BOOTMGR && echo "Windows 7 Loader found in $pt"
    So, for some reason, it just does not like my Windows drive, even as it seems some output should come out. Humm....
    OOOPs ! Yes. It should be $pt ! I slept.
    But now you're going to find more than one partition.

  9. #9
    Join Date
    Mar 2010
    Location
    Austin - Texas
    Posts
    10,500
    Blog Entries
    48

    Smile Re: Looking for Grub and Windows bootloader in all partitions.

    So I made that change (as well as another) and indeed now, I see two partitions as having the Windows boot loader. The first is correct, while the second is not. It is just an NTFS partition on a USB 3 hard drive.

    Code:
    james@LinuxUser:~> findgrub
    Root User Permissions are required, Please Enter the ...
    
    root's password:
    
    Find Grub Version 1.0 : Syntax: findgrub [sdx] : x = a,b or c etc.
    
    Searching Partition /dev/sda1
    Windows 7 Loader found in /dev/sda1
    Searching Partition /dev/sdb1
    Searching Partition /dev/sdb2
    Grub found in partition /dev/sdb2
    Searching Partition /dev/sdb3
    Searching Partition /dev/sdc1
    Windows 7 Loader found in /dev/sdc1
    
    findgrub search is complete...
    So what is our next step?

    Thank You,
    My Blog: https://forums.opensuse.org/blogs/jdmcdaniel3/

    Software efficiency halves every 18 months, thus compensating for Moore's Law

    Its James again from Austin, Texas

  10. #10

    Default Re: Looking for Grub and Windows bootloader in all partitions.

    Hi James, Thank you for finding my bugs !
    Quote Originally Posted by jdmcdaniel3 View Post
    So what is our next step?
    Well, I didn't want to read (mount) the filesystems, but I don't seem to have the choice. It might not work if the filesystem is busy.
    Now it makes the script definitely longer. I have no idea how/if it works if the Windows partitions are already mounted (actually I do have an idea, since I had to unmount them). But let's hope that people looking for their Windows boot partition don't have it mounted already.

    I removed the device arguments but added an option "--writemenu" to append the (hopefully) correct Windows boot entry to /boot/grub/menu.lst.

    In the ideal case, you'll just have to run the script and if you are happy with the ouput (in green if you did copy/paste the escape sequences), run it again with the option --writemenu ... then reboot Windows (I said "in the ideal case!").

    I used (to use) makeactive to boot Windows ... but to be honnest I don't boot Windows. I completely disagree with openSUSE boot strategy (like Grub in the extended partition and activate the root partition, whether it is a primary or not). But anymay, if you think "makeactive" is bothering - you might know better than I do - feel free to remove or comment out that line in the script.

    Code:
    #! /bin/bash
    
    # if execute with option --writemenu, add boot entries to /boot/grub/menu.lst
    
    function Linux2LegacyGrub {
    	eval `echo ${1:2} | sed 's|\(.\)|maj=\1;min=|;s|$|;|'`
    	let min--
    	echo "(hd$(($(printf "%d\n" \'$maj) - 97)),$min)" 
    }
    
    function chainloadEntry { 
    
    if  [ "${WRITEMENU}" ] ; then
    	echo "append boot entry to chainload Windows bootmanager on $1 to /boot/grub/menu.lst"
    	cp /boot/grub/menu.lst{,.findgrub}
    	cat >> /boot/grub/menu.lst << EOFWriteGrubEntry
    
    ###Don't change this comment - YaST2 identifier: Original name: WindowsBootLoader###
    title Windows on $1
        rootnoverify $(Linux2LegacyGrub ${1#/dev/*})
        makeactive
        chainloader +1
    
    EOFWriteGrubEntry
    else
    	echo You can add the following entry to /boot/grub/menu.lst:
    	cat << EOFDisplayGrubEntry
    
    ###Don't change this comment - YaST2 identifier: Original name: WindowsBootLoader###
    title Windows on $1
        rootnoverify $(Linux2LegacyGrub ${1#/dev/*})
        makeactive
        chainloader +1
    
    EOFDisplayGrubEntry
    fi
    }
    
    function findbootmgr {
    	fs=`sfdisk -c ${pt%%[0-9]*} ${pt/#*d[a-z]} 2>/dev/null`
    	case $fs in
    	27) 
    		echo "Windows 7 recovery partition found in $pt"
    		return
    		;;
    	6|b|c|e) fs=vfat ;;
    	7) fs=ntfs;;
    	esac
    	[ -d /findgrub ] || mkdir /findgrub
    	mount -t $fs $pt /findgrub
    	case $2 in
    		bootmgr)
    			find /findgrub -maxdepth 1 -iname "bootmgr" | grep -q -i "bootmgr" && {
    			echo "Windows7/Vista Loader found in $pt"
    			chainloadEntry $pt
    		}
    	;;
    		ntldr)
    			find /findgrub -maxdepth 1 -iname "ntldr" | grep -q  -i "ntldr" && {
    			echo "Windows NT/2K/XP Loader found in $pt"
    			chainloadEntry $pt
    		}
     	;;
    	esac
    	umount /findgrub
    }
    
    
    # Main - BEGIN --------------------------
    
    devs=(`fdisk -l | sed -n 's|^Disk \(/dev/[hs]d[a-z]\):.*|\1|p'`)
    
    # add boot entry to /boot/grub/menu.lst
    if [ "$1" == "--writemenu" ] ; then
    	WRITEMENU=1
    	shift		
    fi
    
    
    #[ "$1" ] && devs=(/dev/$1) 
    
    for dev in ${devs[*]} ; do
    	if [ -b $dev ] ; then
    		dd if=$dev bs=512 count=1 2> /dev/null | grep -q GRUB && echo "Grub found in MBR on $dev"
    		for pt in `fdisk -l $dev | awk '/^\/dev/ { print $1 }'` ; do
    			dd if=$pt bs=512 count=1 2> /dev/null | grep -q GRUB && echo "Grub found in $pt"
    			dd if=$pt bs=512 count=1 2> /dev/null | grep -q NTLDR && findbootmgr $pt ntldr
    			dd if=$pt bs=512 count=1 2> /dev/null | grep -q BOOTMGR && findbootmgr $pt bootmgr
    		done
    	fi
    done
    
    [ -d /findgrub ] && rmdir /findgrub
    
    # Main - END ---------------------------
    I know, it's kinda "write only" shell scripting style ...

Page 1 of 27 12311 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •