{"id":26,"date":"2010-01-27T20:24:13","date_gmt":"2010-01-28T02:24:13","guid":{"rendered":"http:\/\/www.linuxpoweruser.com\/?p=26"},"modified":"2010-01-27T20:24:13","modified_gmt":"2010-01-28T02:24:13","slug":"boot-sector-management","status":"publish","type":"post","link":"https:\/\/www.linuxpoweruser.com\/?p=26","title":{"rendered":"Boot Sector Management"},"content":{"rendered":"<p>As promised, tonight we explore boot sector management on X86 style hardware.\u00a0 Anyone who works with PC hardware long enough, and especially those using linux as primary or secondary OS in a dual boot configuration will find this information valuable.<\/p>\n<p>The system boot sector on x86 style hardware is crucial to being able to boot a linux system on this common platform. \u00a0Occaisonally the boot sector becomes corrupted or needs to be backed up. \u00a0In the days of MS DOS systems, a command was used to &#8220;restore&#8221; the boot sector. \u00a0The command was<br \/>\n<code><br \/>\nFDISK \/MBR<br \/>\n<\/code><br \/>\nEssentially this would re-write the boot sector on the primary hard disk.<\/p>\n<p>The dd command can be used to perform similar functions, however as is usual with Linux, more boot sector related tasks can be accomplished.<\/p>\n<p>First of all lets review the structure of a boot sector or master boot record on a PC hard disk:<\/p>\n<p>Format of the boot sector:<\/p>\n<table>\n<tbody>\n<tr>\n<td>Size (bytes)<\/td>\n<td>Description<\/td>\n<\/tr>\n<tr>\n<td>446<\/td>\n<td>Executable code section<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>Optional Disk signature<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Usually nulls<\/td>\n<\/tr>\n<tr>\n<td>64<\/td>\n<td>Partition table<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>MBR signature<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The first 446 bytes of the boot sector contain executable code that is loaded by the BIOS and then executed, and is where OS boot loaders and boot managers (such as grub) store their initial code. \u00a0Its also an area of the disk that can become corrupted, or replaced during operating system installs.<\/p>\n<p>The other part of the boot sector that is significant is the partition table. \u00a0This is where the disk partition information is stored. \u00a0This should not be modified by anything other than a disk partitioning utility such as fdisk. \u00a0It can be backed up for data security reasons though. \u00a0The total bytes in the master boot record comes to 512. \u00a0With dd, simply reading or writing the first 446 or 512 bytes of the disk device will read or write the master boot record.<\/p>\n<p>Scenario 1: \u00a0Backup the boot sector (or MBR)<\/p>\n<p>If the first harddisk in the system is \/dev\/sda, to backup the boot sector the following command can be used:<br \/>\n<code><br \/>\n# dd if=\/dev\/sda of=bsbackup.bin bs=512 count=1<br \/>\n<\/code><br \/>\nEssentially this command will read the first 512 bytes of \/dev\/sda and write it to the file bsbackup.bin.<\/p>\n<p>Scenario 2: Restore the boot sector from a file:<br \/>\n<code><br \/>\n# dd if=bsbackup.bin of=\/dev\/sda bs=512 count=1<br \/>\n<\/code><br \/>\nThis will restore the boot sector to \/dev\/sda that was backed up in Scenario\u00a01.<\/p>\n<p>Scenario 3: \u00a0Zero out the boot sector (leaving the partition table intact)<\/p>\n<p>Sometimes a virus or other issue can leave a corrupted executable code section in the MBR. \u00a0I have personally seen a boot sector that would not store grub information (and thus boot linux after its installed) properly until the first 446 bytes were zeroed out and grub re-installed. \u00a0The following command will do just that:<br \/>\n<code><br \/>\n# dd\u00a0 if=\/dev\/zero of=\/dev\/sda bs=446 count=1<br \/>\n<\/code><br \/>\nScenario 4: \u00a0Zero out the entire MBR (this will erase the partition table as well &#8211; effectively destroying the ability to easily access data on the drive)<\/p>\n<p>A variation of the last dd command will wipe out the master boot record entirely. \u00a0You will have to repartition and reformat your hard disk after this:<br \/>\n<code><br \/>\n# dd if=\/dev\/zero of=\/dev\/sda bs=512 count=1<br \/>\n<\/code><br \/>\nIn summary, the use of dd for boot sector management is a handy tool to have in your linux arsenal.<\/p>\n<p>Next up are some networking topics, such as SSH tunneling, IPSEC VPNs.\u00a0 Keep watching the site, or subscribe to our RSS Feed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As promised, tonight we explore boot sector management on X86 style hardware.\u00a0 Anyone who works with PC hardware long enough, and especially those using linux as primary or secondary OS in a dual boot configuration will find this information valuable. The system boot sector on x86 style hardware is crucial to being able to boot [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[17,25,27],"_links":{"self":[{"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/posts\/26"}],"collection":[{"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26"}],"version-history":[{"count":0,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/posts\/26\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}