---
title: "嵌入式Linux下fdisk处理磁盘MBR的可选ID"
author: "mmdev"
date: "2011-12-27T23:41:00Z"
tags: []
slug: "linux-fdisk-mbr-id"
draft: false
lastmod: "2012-08-04T20:18:12Z"
id: 1620519
url: "https://mmdev.iteye.com/blog/1620519"
---

**作者：gzshun. 原创作品，转载请标明出处！**

在嵌入式Linux中，经常涉及到格式化硬盘，常用的工具就是fdisk，这工具功能强大，busybox里面也实现了fdisk。当busybox实现的fdisk是简化版，与原版的GNU的fdisk相差挺大的，主要是缺少一些细节性的功能。  
本文主要是说明fdisk写入MBR的一个磁盘可选id，这个区域可以用来唯一的标记一块硬盘，总共有4个字节，2的32次方中情况。  
以后将会详细的介绍MBR结构。。  
以下是摘自维基百科的一个表格，介绍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 (本文涉及到该区域)  
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 55h MBR signature 2  
01FF 0777 511 AAh  
MBR, total size: 446 + 64 + 2 = 512

如果要修改该区域，需要确保该区域的地址，地址是：440-443；该区域占4个字节，只需要用一个int型变量即可读取。

源码如下：

```
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;
}
```

上面已经说了，busybox可能没有实现fdisk这个功能，有些时候嵌入式系统需要标志硬盘ID，所以需要随机产生一个数字来写入硬盘的MBR的磁盘可选id的区域，这随便产生一个随机数就行，可以使用time(NULL)获取，很简单。  
在GNU的fdisk源码中，使用以下函数来生成唯一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));
}
```

该函数是去读取/dev/urandom设备，该设备用于产生一个无符号的随机数，如果读取失败，将会直接返回进程的pid与当前的unix时间的总和，以保证返回一个唯一的id。  
以下源码产生一个唯一id，并写入mbr的磁盘可选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个字节，如果不确定，可能会造成越界，影响到其他区域。  
保存在该区域，不会影响到MBR的正常使用，这区域本来就是保留着，可选使用。