Open-source ext2read code-Description of extended partitions and logical partitions and how to read the Partition Table of extended partitions (EBR)

Source: Internet
Author: User

A hard disk can have many partitions, but there are only four MBR partition tables. How can we break through this restriction? The answer is the extended Boot Record (EBR-extended Boot Record). By setting an item in the MBR partition table as an extended partition (the System ID is 0x05 or 0x0f ), the partition table item specifies the start position and length of the extended partition. In the item, place another partition table at the same position (0x1be) of the Start sector (EBR) and MBR, which is generally called the extended partition table. The first item of the extended partition table specifies the current information of the extended partition. If there are more logical partitions, the second item of the extended partition table specifies the location of the next EBR; otherwise, it is 0. The last two Partition Table items are always 0. In this way, there is no limit on the number of partitions on a hard disk.

The above means that the extended partition is one of the four partition tables in the MBR table item, and the extended partition manages multiple logical partitions through a chain partition table. That is, the logical partition is a part of the extended partition and a sub-level.

Note: In a logical partition, each partition is only useful for the information of the first two partition tables, and the last two partition tables are not used. Therefore, the following program does not read the information of three or four partitions.

So how do I read the partition tables of multiple logical partitions in the extended partition?

Int ext2read: scan_ebr (filehandle handle, loff_t base, int sectsize, int disk)

{
Unsigned char sector [sector_size];
Struct mbrpartition * part, * Part1;
Ext2partition * partition;
Int logical = 4, RET;
Loff_t ebrbase, nextpart, ebr2 = 0;
// Indicates the relative sector ID of the first slice at the beginning of the extended partition (it should be relative to the previous partition)
Ebrbase = base;
Nextpart = base;
While (1)
{
// Read the first sector of the logical Partition
Ret = read_disk (handle, sector, nextpart, 1, sectsize );
If (Ret <0)
Return ret;

If (Ret <sectsize)
{
Printf ("error reading the EBR \ n ");
Return-1;
}
// Obtain the partition information of the Current Logical partition, with the offset of 446 bytes.
Part = pt_offset (Sector, 0 );
Printf ("index % d id % x size % LD \ n", logical, part-> sys_ind, get_nr_sects (part ));
// If the file system is ext2, the system processes the data and records the total number of slice in the partition. The current partition is relative to the slice ID, the slice size, and the device control handle.
If (Part-> sys_ind = ext2)
{
Partition = new ext2partition (get_nr_sects (Part), get_start_sect (Part) + ebrbase + ebr2, sectsize, handle, null );
If (partition-> is_valid)
{
// Why is the initial value of logical 4? Because the Partition Number of the logical partition starts from 5, it will be changed to + 1 in the set_linux_name Function
Partition-> set_linux_name ("/dev/sd", disk, logical );
// Save the ext2 partition object to the ext2 valid partition list
Nparts. push_back (partition );
}
Else
{
Delete partition;
}
}
// Processing of LVM partitions
If (Part-> sys_ind = linux_lvm)
{
Printf ("LVM physical volume found disk % d partition % d \ n", disk, logical );
LVM (handle, get_start_sect (Part) + ebrbase + ebr2, this );
LVM. scan_pv ();
}
// Obtain the partition information of the next logical partition. The information of the next logical partition is in the second partition table of the Current Logical partition table;
Part1 = pt_offset (Sector, 1 );
// Obtain the sector ID relative to the start sector of the current partition (it should be relative to the previous partition)
Ebr2 = get_start_sect (Part1 );
// Accumulate the relative sector ID of the Start sector of the current Partition
Nextpart = (ebr2 + ebrbase );
// Accumulate the number of partitions
Logical ++;
// If the file system of the second table in the logical Partition Table is 0, no logical partition is available.
If (Part1-> sys_ind = 0)
Break;
}
Return logical;
}

Open-source ext2read code-Description of extended partitions and logical partitions and how to read the Partition Table of extended partitions (EBR)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.