You are not logged in.
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
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
Offline
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
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
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
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
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. )
@Mr.Green
I like this quest, however the answer is not that obvious.
Offline
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
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
Have been told cfdisk -Pc /dev/sdx but chomping hex sounds interesting
Mr Green
Offline
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
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
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
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
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
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
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
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