You are not logged in.

#1 2014-05-06 14:45:46

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Testing for boot flag on a usb device in bash

Is there a way to check if a usb drive is bootable (boot flag is set). So far not found anything other than using parted. Another option would simply be to set boot flag on sdx. (than I would use possibly fdisk?


Mr Green

Offline

#2 2014-05-07 10:17:27

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Testing for boot flag on a usb device in bash

You can do this with dd.

sudo dd if=/dev/sdX of=/tmp/bootable count=1 bs=512

then read the file

file /tmp/bootable

wink

Offline

#3 2014-05-07 12:05:16

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Testing for boot flag on a usb device in bash

Does work but if I disable boot flag mbr code is still there ie file still shows DOS/MBR. Might have to extract from fdisk.....


Mr Green

Offline

#4 2014-05-07 12:36:12

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Testing for boot flag on a usb device in bash

Hmzz, didn't think of that.
Something like this?, I use GPT.

sudo gdisk -l /dev/sda | grep -A4 '^Partition table scan:'|grep MBR

Offline

#5 2014-05-07 12:37:29

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,444
Website

Re: Testing for boot flag on a usb device in bash

Devices can't be flagged bootable, can they?  Only partitions can (I think).

The BIOS will determine which device(s) are checked for any bootable partitions.

Last edited by Trilby (2014-05-07 12:37:52)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2014-05-07 12:44:19

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Testing for boot flag on a usb device in bash

I have a script that installs isos (loop mount), tried to automate as much as I could but needed a simple way to test if as you say partition was flagged bootable. My thoughts are to use

# fdisk -l /dev/sdx1

And look for the '*'

Have already added checks to see if device is a valid usb (removable) of course will have to think about seeing if device has a filesystem on it at somepoint...


Mr Green

Offline

#7 2014-05-07 13:49:06

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Testing for boot flag on a usb device in bash

Trilby wrote:

Devices can't be flagged bootable, can they?  Only partitions can (I think).

The BIOS will determine which device(s) are checked for any bootable partitions.

True, but the bootable flag is part of the mbr, which is stored in the first 512 bytes.

Was just brainstorming for a solution. wink)


@Mr.Green

I like this quest, however the answer is not that obvious.

Offline

#8 2014-05-07 15:07:02

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Testing for boot flag on a usb device in bash

You could use other packages to find it, have searched but not found anything yet. Another option would be too simply set boot flag regardless. Just to be sure....

Bit more in context

https://github.com/mrgreen3/gloopy

Last edited by Mr Green (2014-05-07 15:08:21)


Mr Green

Offline

#9 2014-05-07 15:21:57

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Testing for boot flag on a usb device in bash

Will look into the gloopy project.
But, I have been sleuthing some more, and found out there is a way to see if the bootable flag is on/off, I just don't know how to get it out.

If you read the mbr in a hex editor, 0x00 = off, 0x80 = on.
I found that here

Offline

#10 2014-05-07 16:22:18

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Testing for boot flag on a usb device in bash

Have been told cfdisk -Pc /dev/sdx but chomping hex sounds interesting


Mr Green

Offline

#11 2014-05-07 16:39:46

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Testing for boot flag on a usb device in bash

Found this using printf

http://stackoverflow.com/questions/1652 … ex-in-bash

or even better

http://linuxcommando.blogspot.co.uk/200 … g-cli.html

Last edited by Mr Green (2014-05-07 16:42:32)


Mr Green

Offline

#12 2014-05-07 17:23:53

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Testing for boot flag on a usb device in bash

Nice , tried that..
Also tried  cfdisk -Pt /dev/sdx, replaced c with t.

in my case it shows 0x00 and 0xEE, which is correct as I run EFI.
My script-fu is still not wonderful, so question, how are you going to get the necessary part to say /tmp/bootfile?

Offline

#13 2014-05-07 19:26:48

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: Testing for boot flag on a usb device in bash

You can still use “dd” to get the flag byte. Assuming you have read-access to the device:

dd if=/dev/sdx bs=1 count=1 status=none skip=$((0x1BE)) | od -t x1 -A n

“od” is used to turn the binary into something a bit more human/script friendly.
Use “count=16” if you want the whole partition record.
More generally, for device $dev, partition $part (1-based):

partrec=$(dd if=$dev bs=1 count=16 status=none skip=$((0x1BE + 16 * ($part - 1) )) | od -t x1 -A n)

Last edited by ukhippo (2014-05-07 19:30:34)

Offline

#14 2014-05-08 07:21:35

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Testing for boot flag on a usb device in bash

od does help, just need to figure a test for '00' then set boot flag on device.


#!/bin/bash

dev=/dev/sdb

[[ $(dd if=$dev bs=1 count=1 status=none skip=$((0x1BE)) | od -t x1 -A n) -eq "00" ]] && echo "Set Boot Flag"

Thanks

Last edited by Mr Green (2014-05-08 08:02:29)


Mr Green

Offline

#15 2014-05-08 08:45:43

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: Testing for boot flag on a usb device in bash

To set the boot flag use “parted”. You can also use parted to view the partition table.
To set the boot flag for partition 1:

parted $dev set 1 boot on

For your bash test you need to be aware that od's output will have a space in front, so you should use:

[[ $(...) = " 00" ]] && {...}

Last edited by ukhippo (2014-05-08 08:53:22)

Offline

#16 2014-05-08 09:17:16

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Testing for boot flag on a usb device in bash

Had a feeling a space was there, guess I could strip it but yes your way is easier, Would using parted to view table be easier than dd?


Mr Green

Offline

#17 2014-05-08 12:50:17

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: Testing for boot flag on a usb device in bash

The advantage of programs like parted etc. is that they know how to decode the information, e.g:

[paul@nas02 ~]$ sudo parted -m -s /dev/sdg print
BYT;
/dev/sdg:7803MB:scsi:512:512:msdos: USB Flash Memory:;
1:31.7kB:298MB:298MB:fat32::boot;
2:298MB:7803MB:7505MB:fat32::;
[paul@nas02 ~]$ sudo parted -m -s /dev/sda print                                                      
BYT;
/dev/sda:4001GB:scsi:512:4096:gpt:ATA WDC WD40EFRX-68W:;
1:1049kB:4001GB:4001GB::zfs:;
9:4001GB:4001GB:8389kB:::;

Whereas using dd means you have to know the structure and meaning of the bytes.

So if you need to know more about the device, use parted and process with something like awk or perl. However if you know you're dealing with an MBR and just want the boot flag, use dd.
Or, as a learning exercise, write two versions - one using parted and the other dd.

Offline

#18 2014-05-08 13:31:12

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Testing for boot flag on a usb device in bash

That might be easier to use parted

 sudo parted -m -s /dev/sdb print
BYT;
/dev/sdb:32.5GB:scsi:512:512:msdos:JetFlash Transcend 32GB:;
1:32.3kB:32.5GB:32.5GB:fat32::boot;

From my usb drive.

We are looking at syslinux as it sets boot flag in install, though I am unsure it will loop mount. The whole point of the exercise.

Certainly [ -f /usr/bin/parted ] will check if it is installed...


Mr Green

Offline

Board footer

Powered by FluxBB