Optional ID for fdisk processing disk MBR in Embedded Linux

Source: Internet
Author: User

Author: gzshun. Original Works. Reprinted, please indicate the source!

In Embedded Linux, hard disk formatting is often involved. fdisk is a commonly used tool, which is powerful and implements fdisk in busybox. When the fdisk implemented by busybox is a simplified version, it is quite different from the GNU fdisk in the original version, mainly because it lacks some detailed functions.
This article mainly describes an optional ID of a disk written by fdisk into MBR. This region can be used to uniquely mark a hard disk, with a total of 4 bytes and 32 times of 2.
The MBR structure will be detailed later ..
The following is a table from Wikipedia that describes the structure of MBR:
Structure of a Master Boot Record
Address descriptionsize in bytes
Hex Oct
Dec
0000 0000
0 code area
440 (max. 446)
01b8 0670
440 disk signature (optional) 4 (this article covers this region)
01bc 0674
444 usually nulls; 0x00002
01be 0676
446 table of primary partitions (four 16-byte entries, IBM Partition Table Scheme) 64
01fe 0776
510 55 h
MBR Signature 2
01ff 0777
511 aah.
MBR, total size: 446 + 64 + 2 = 512

To modify the region, make sure that the address of the region is 440-443. The region occupies 4 bytes and can be read only with an int variable.

The source code is as follows:

int main(void){int fd;unsigned int id;fd = open("/dev/sda", O_RDONLY);if (fd < 0) {perror("open");exit(1);}lseek(fd, 440, SEEK_SET);if (read(fd, &id, 4) != 4) {perror("read");close(fd);exit(1);}close(fd);return 0;}

As mentioned above, busybox may not implement the fdisk function. In some cases, the embedded system needs to mark the hard disk ID. Therefore, a random number must be generated to write the optional ID of the hard disk's MBR disk, you can use time (null) to obtain a random number.
In GNU fdisk source code, use the following function to generate a unique ID:

static unsigned int get_random_id(void) {int fd;unsigned int v;ssize_t rv = -1;fd = open("/dev/urandom", O_RDONLY);if (fd >= 0) {rv = read(fd, &v, sizeof v);close(fd);}if (rv == sizeof v)return v;/* Fallback: sucks, but better than nothing */return (unsigned int)(getpid() + time(NULL));}

This function is used to read/dev/urandom devices. This device is used to generate an unsigned random number. If the read fails, it will directly return the sum of the PID of the process and the current UNIX time, to ensure that a unique ID is returned.
The following source code generates a unique ID and writes it to the region of the MBR disk's optional ID:

int main(void){int fd;unsigned int id;id = get_random_id();fd = open("/dev/sda", O_RDONLY);if (fd < 0) {perror("open");exit(1);}lseek(fd, 440, SEEK_SET);if (write(fd, &id, 4) != 4) {perror("write");close(fd);exit(1);}close(fd);return 0;}

4 bytes must be specified here. If you are not sure, it may cause cross-border attacks and affect other regions.
It is stored in this region and will not affect the normal use of MBR. This region is reserved and optional.

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.