Now, in order to make sure we got it right, I spent a bit of time re-learning the details of MBR partition tables. Turns out there are a number of good resources describing the details. The Wikipedia articles listed here were very helpful. MBR article, extended partition article, partition types article
I'll write a quick summary here.
Note: I'm discussing disks that use the historical, old, MSDOS partitioning system. There are many other partitioning schemes, but this is the one I care about right now.
The Master Boot Record (MBR) is the first sector (512 bytes) on the disk. In addition to boot-time start-up code, there are four 16 byte partition records (64 bytes total) stored in this data. That's why there can only be 4 primary partitions on a disk. There is no more room. Each of the records stores information the kind of partition, the starting location and the ending location of the partition.
To overcome the 4 partition limit, extended partitions were added. There are 2 common partition types that mean extended. They differ in the way addresses are represented, but are mostly the same. The codes are 0x05 and 0x0F. Only one of the primary partitions can be classified as extended. The extended partition's space is then available to be divided into logical partitions.
The first sector (512 bytes) of the extended partition, has another MBR-like structure, with slots for defining 4 partitions. The first entry defines the first logical partition's starting position and size. The second entry defines the next logical partition's starting position and size. The third and fourth entries are not used.
Each logical partition also has an MBR-like structure that describes the logical partition itself, and the next logical partition. The last logical partition shows its next logical partition as all zeros, indicating the end of the list. Many logical partitions can be represented using this linked-list style approach.
If you care about the actual format and details of the records, read the articles referenced above.
For our purposes, we need to know which sectors contain the partition table structure, how to copy them, and how to restore them. FOG worked only with primary partitions, because it was only copying the MBR at the front of the disk, not the MBR-like blocks in the extended and logical partitions necessary to re-create the extended and logical partitions. We need to re-construct all of those elements to get an identical partition table with extended and logical partitions.
We could write a tool to do that, and make sure we get all of the bit-twiddling correct, and send it through a testing cycle to get all the bugs worked out. Or, we could use software that has already been vetting by millions of users, even if they don't know it.
The sfdisk command can read the partition table and display a straightforward and precise textual representation:
$ sudo sfdisk -d
# partition table of /dev/sda
unit: sectors
/dev/sda1 : start= 2048, size= 204800, Id= 7, bootable
/dev/sda2 : start= 206848, size=390623232, Id= 7
/dev/sda3 : start=390830080, size= 15624192, Id=82
/dev/sda4 : start=406454272, size= 72060928, Id= f
/dev/sda5 : start=406456320, size= 72058880, Id=83
This format can then be used to create a new partition table with the same structure. sfdisk does it for us.
Thanks to the FOG developers and support team, the Wikipedia contributors, and software developers that made this possible.
No comments:
Post a Comment