| 463 | === Partition table problems === |
| 464 | |
| 465 | Some software failures can lead to zeroes or random data being written on the first block of a disk. For disks that use a DOS-based partitioning scheme this will overwrite the partition table which is found at the end of the first block. This is a single point of failure so after the damage tools like `fdisk` have no alternate data to use so they report no partitions or a damaged partition table. |
| 466 | |
| 467 | One utility that may help is `testdisk` [#footnote6 [6]] which can scan a disk looking for partitions and recreate a partition table if requested. |
| 468 | |
| 469 | Programs that create DOS partitions often place the first partition at logical block address `63`. In Linux a loop back mount can be attempted at the appropriate offset of a disk with a damaged partition table. This approach may involve placing the disk with the damaged partition table in a working computer or perhaps an external USB enclosure. Assuming the disk with the damaged partition is `/dev/hdb`. Then the following read-only loop back mount could be tried: |
| 470 | |
| 471 | {{{ |
| 472 | # mount -r /dev/hdb -o loop,offset=32256 /mnt |
| 473 | }}} |
| 474 | |
| 475 | The offset is in bytes so the number given is `(63 * 512)`. If the file system cannot be identified then a `-t <fs_type>` may be needed (although this is not a good sign). If this mount is successful, a backup procedure is advised. |
| 476 | |
| 477 | Only the primary DOS partitions are recorded in the first block of a disk. The extended DOS partition table is placed elsewhere on a disk. Again there is only one copy of it so it represents another single point of failure. All DOS partition information can be read in a form that can be used to recreate the tables with the `sfdisk` command. Obviously this needs to be done beforehand and the file put on other media. Here is how to fetch the partition table information: |
| 478 | |
| 479 | {{{ |
| 480 | # sfdisk -dx /dev/hda > my_disk_partition_info.txt |
| 481 | }}} |
| 482 | |
| 483 | Then `my_disk_partition_info.txt` should be placed on other media. If disaster strikes, then the disk with the damaged partition table(s) can be placed in a working system, let us say the damaged disk is now at `/dev/hdc`, and the following command restores the partition table(s): |
| 484 | |
| 485 | {{{ |
| 486 | # sfdisk -x -O part_block_prior.img /dev/hdc < my_disk_partition_info.txt |
| 487 | }}} |
| 488 | |
| 489 | Since the above command is potentially destructive it takes a copy of the block(s) holding the partition table(s) and puts it in part_block_prior.img prior to any changes. Then it changes the partition tables as indicated by `my_disk_partition_info.txt`. For what it is worth the author did test this on his system! [#footnote7 [7]] |
| 490 | |
| 491 | For creating, destroying, resizing, checking and copying partitions, and the file systems on them, GNU's `parted` is worth examining. The [http://www.tldp.org/HOWTO/Large-Disk-HOWTO.html Large Disk HOWTO] is also a useful resource. |