LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 01-27-2007, 06:29 PM   #1
tantan
LQ Newbie
 
Registered: May 2006
Location: Italy
Distribution: RedHat
Posts: 13

Rep: Reputation: 0
MBR and Boot Loader


Hi,
just to understand...
I've ever known that in the last bytes of the MBR there is the partitions table. That is, the MBR is 512 bytes long, and in the last bytes (from 446 to 509) find place the partitions table (created by fdisk) (and in the last two bytes the magic mumber).
I found in the GRUB manual and tutorials, that its "stage1" file (the one to put in the MBR) is 512 bytes long, to fit the MBR. OK, but if the installation of the boot loader overwrite the first 512 bytes of the disk, this overwrite the partitions table too, doesn't it?
What step am I loosing?
 
Old 01-27-2007, 06:36 PM   #2
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Squeeze 2.6.32.9 SMP AMD64
Posts: 3,136

Rep: Reputation: 106Reputation: 106
"but if the installation of the boot loader overwrite the first 512 bytes of the disk, this overwrite the partitions table too, doesn't it?
What step am I loosing?"

Probably that the system reads a copy of the first sector, so it already knows what belongs there. It shouldn't be a surprise to learn that programmers take side-effects such as this into account in order to produce a working product.
 
Old 01-27-2007, 07:01 PM   #3
saikee
Senior Member
 
Registered: Sep 2005
Location: Newcastle upon Tyne UK
Distribution: Any free distro.
Posts: 3,398
Blog Entries: 1

Rep: Reputation: 112Reputation: 112
No boot loader dares to overwrite the partition table. The code of stage1 actually stops at 446th byte if it is installed in the MBR.

The only known full application of the 512 bytes of stage1 is for writing on the floppy.

For writing on the MBR one must used the "grub-install" shell script which is part of the Grub build. Alternative it can be done in a Grub shell or a Grub prompt by the "root" + "setup" commands.
 
Old 01-27-2007, 07:06 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 10,456

Rep: Reputation: 622Reputation: 622Reputation: 622Reputation: 622Reputation: 622Reputation: 622
What you're missing is that the stage1 isn't "just copied" into the MBR. It needs to be tailored for *your* particular instance of installation. What partition the (other) stage files ultimately reside in, and on which disk are data that are contained in the boot loader record for example.
 
Old 01-27-2007, 07:21 PM   #5
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Squeeze 2.6.32.9 SMP AMD64
Posts: 3,136

Rep: Reputation: 106Reputation: 106
"No boot loader dares to overwrite the partition table. The code of stage1 actually stops at 446th byte if it is installed in the MBR."

While this is logically true, physically it is not. The smallest chunk of data that can be read or written to a disk is 512 bytes. Therefore, the partition table is actually overwritten when the MBR is saved. It's just that it is overwritten with the same data as it already had. There is some good information here: http://en.wikipedia.org/wiki/Partition_table
 
Old 01-27-2007, 07:24 PM   #6
tantan
LQ Newbie
 
Registered: May 2006
Location: Italy
Distribution: RedHat
Posts: 13

Original Poster
Rep: Reputation: 0
I see. I was a little confusing, because of a sentence I found, don't remember exatly where, if on the maual or other book, that stated something like "the MBR is 512 byte, that's the reason why the stage1 file is exatly 512 bytes" (and if you check the length of the stage1 file you can find with grub, it's true). Then, the manual tell how to create a bootable floppy by simply copying the stage1. So I assumed that also the installation copies that file on the MBR. But as saikee and syg00 say, I missed the installation is a more complex procedure, (not deeply described at a low level by the manual).
Thank you for your help
 
Old 01-27-2007, 07:31 PM   #7
tantan
LQ Newbie
 
Registered: May 2006
Location: Italy
Distribution: RedHat
Posts: 13

Original Poster
Rep: Reputation: 0
Our post crossed Quakeboy, I read you just now. Your observation is very very interesting!
 
Old 01-27-2007, 07:45 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 10,456

Rep: Reputation: 622Reputation: 622Reputation: 622Reputation: 622Reputation: 622Reputation: 622
Interesting but wrong.
I regularly "zap" the boot record by copying /dev/zero for some required number of bytes. Almost always less than 512.
 
Old 01-27-2007, 08:00 PM   #9
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Squeeze 2.6.32.9 SMP AMD64
Posts: 3,136

Rep: Reputation: 106Reputation: 106
Quote:
Interesting but wrong.
I regularly "zap" the boot record by copying /dev/zero for some required number of bytes. Almost always less than 512.
A hard disk sector is 512 bytes long; not 327, not 513, but exactly 512 bytes.

Added:
From:http://www.schicktech.com/items.php?...76&itemid=1382

Quote:
Fdisk has an undocumented parameter called /MBR that causes it to write the master boot record to the hard disk without altering the partition table information.
If you were to look under the covers, you would see the software first read the MBR, then rewrite it with the new data, thus not changing the partition table. Due to the way the data is stored physically on a hard disk (synchronous encoding) it is not possible to write only part of a sector. Software may fool you into thinking that's what you're doing, of course.

Last edited by Quakeboy02; 01-27-2007 at 08:16 PM.
 
Old 01-27-2007, 11:32 PM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 10,456

Rep: Reputation: 622Reputation: 622Reputation: 622Reputation: 622Reputation: 622Reputation: 622
That fdisk reference is to the old M$oft (DOS) fdisk. Wash your mouth out ...

Thinking out what you said, it certainly sounded reasonable in the "normal" case. I'm currently seeing if I can make any sense of the source for "dd" to see if it always does a hidden read of the outfile for every block (which I think unlikely), or opens the disk as a "byte special" rather than block device in a case such as I described.
Could take a while - it's the Australia day long weekend, and the cricket has just started ...
 
Old 01-28-2007, 12:56 AM   #11
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Squeeze 2.6.32.9 SMP AMD64
Posts: 3,136

Rep: Reputation: 106Reputation: 106
"That fdisk reference is to the old M$oft (DOS) fdisk. Wash your mouth out"

Pass. I think you'll find that the location of the partition table hasn't changed since the first hard disk was made.

"I'm currently seeing if I can make any sense of the source for "dd" to see if it always does a hidden read of the outfile for every block (which I think unlikely)"

There's no reason it should. But it should do a hidden read if you're starting in the middle of a sector and probably if you end in the middle of one. Otherwise what does it do with the rest of the bytes? Why would zero-fill be the proper thing to do, especially on sector zero.

"or opens the disk as a "byte special" rather than block device in a case such as I described."

So, I assume you do understand the implications of byte special, then.


Consider the case of where you want to append to a file. You cannot actually seek to a byte position on a disk, you can only seek to the sector. If the data you want to append begins in the middle of a sector, then that sector has to be read, the data appended, and the sector written when the sector is full. This continues for each byte you append, though of course the device drivers etc take care of caching data rather than thrashing the disk for every single byte.

Consider the case where you want to change byte 1023 in a file. You can't simply rewrite that byte on the disk. The hardware can't do that. The disk is spinning around and it has a 512 byte synchronous block of data it has to deal with. But, you can read the second sector in the file, change the byte in the read buffer, and then write it back.

Last edited by Quakeboy02; 01-28-2007 at 12:58 AM.
 
Old 01-28-2007, 03:30 AM   #12
saikee
Senior Member
 
Registered: Sep 2005
Location: Newcastle upon Tyne UK
Distribution: Any free distro.
Posts: 3,398
Blog Entries: 1

Rep: Reputation: 112Reputation: 112
If we look at Awesomemachine's thread on how to use dd it has been established beyond doubt that we can write one byte or one word with dd.

dd defaults to write one complete sector of 512 bytes but the user can impose something different.

one can verify the dd command at any time
Code:
dd if=/dev/hda bs=1 count=1
I donot know the details of how "grub-install" or "lilo -b" in Linux or "setup" in Grub is executed when the MBR is altered. The stage1 of GRub is 512 bytes long but the only reference it has for writing it out the full length of 512 bytes in the Grub Manual is for a floppy. We are only speculating here. However it is a fact that all major boot loaders (Grub, Lilo and NTLDR) when reinstoring the MBR are known not to alter the partition table.
 
Old 01-28-2007, 11:09 AM   #13
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Squeeze 2.6.32.9 SMP AMD64
Posts: 3,136

Rep: Reputation: 106Reputation: 106
"If we look at Awesomemachine's thread on how to use dd it has been established beyond doubt that we can write one byte or one word with dd."

I don't doubt that at all. But on the hardware level, there is no such thing as writing one byte. Software does a lot of nice stuff for us and lets us fool ourselves into thinking we know what's going on, sometimes.
 
Old 01-28-2007, 11:21 AM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: back to Arch
Posts: 16,668

Rep: Reputation: 425Reputation: 425Reputation: 425Reputation: 425Reputation: 425
Quote:
Originally Posted by Quakeboy02
"If we look at Awesomemachine's thread on how to use dd it has been established beyond doubt that we can write one byte or one word with dd."

I don't doubt that at all. But on the hardware level, there is no such thing as writing one byte. Software does a lot of nice stuff for us and lets us fool ourselves into thinking we know what's going on, sometimes.
continuing off-topic, they dug deeper and deeper into the nuances......

The implication of the above is that dd (or something below it) is copying and rewriting in 1-sector chunks. Can anyone describe exactly what happens if, for example, I tell dd to write just one byte?

Quote:
But on the hardware level, there is no such thing as writing one byte.
Hard to see how that would be any kind of universal truth. Do you mean that disk drives are normally configured so they only write in larger chunks? If so, I'm betting that's a firmware function, not hardware.
 
Old 01-28-2007, 01:06 PM   #15
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Squeeze 2.6.32.9 SMP AMD64
Posts: 3,136

Rep: Reputation: 106Reputation: 106
"Hard to see how that would be any kind of universal truth. Do you mean that disk drives are normally configured so they only write in larger chunks? If so, I'm betting that's a firmware function, not hardware."

It's a function of the way the data is physically stored on the hard disk. It is not stored as 1 byte chunks. It is stored as 512 byte chunks. In an LBA_28 system (you guys have heard of that, right?), you have a limit of 137GB. How can this be, if 28 bits only defines a range of 0-268,435,456? As it turns out, 268,435,456 times 512 (the size of a sector) equals 137,438,953,472, i.e. the dreaded 137GB limit.

In summary, there is no way to address a single byte on a hard disk. What we do is to read a sector change the byte we're interested in, and then write the sector to the disk.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Installed FC5> Wrote over Ubuntu MBR: How do i add Ubuntu Partition to Grub Loader Free_beer Ubuntu 4 10-02-2006 06:11 PM
Fedora Remove and restoring windows MBR boot loader eng_muhammad Fedora 3 12-03-2005 12:39 PM
Mandrake Install Screwed up MBR; Need to Make Boot Loader CD paul_m_d Linux - Newbie 5 12-09-2003 03:36 AM
boot loader, MBR, redhat9 on primary slave, winxp on primary master samik Linux - Hardware 4 10-15-2003 08:55 PM
possible to reinstall boot loader on the MBR? tarballed Linux - Newbie 3 02-14-2003 02:49 PM


All times are GMT -5. The time now is 03:10 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration