---
title: "linux下对SD卡分区"
author: "mmdev"
date: "2010-12-31T16:08:00Z"
tags: []
slug: "linux-sd"
draft: false
lastmod: "2013-03-06T09:53:59Z"
id: 1823388
url: "https://mmdev.iteye.com/blog/1823388"
---

最近做机顶盒GUI开发需要将SD卡分成若干个分区，现将如何分区总结如下

一 使用linux下的磁盘管理工具fdisk

sudo fdisk /dev/sdc

使用fdisk打开你的SD卡设备名称,我的sd卡设备名称是sdc

二进入fdisk管理工具

打开后进入如下界面

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to  
switch off the mode (command 'c') and change display units to  
sectors (command 'u').

Command (m for help): m  
Command action  
a toggle a bootable flag  
b edit bsd disklabel  
c toggle the dos compatibility flag  
d delete a partition  
l list known partition types  
m print this menu  
n add a new partition  
o create a new empty DOS partition table  
p print the partition table  
q quit without saving changes  
s create a new empty Sun disklabel  
t change a partition's system id  
u change display/entry units  
v verify the partition table  
w write table to disk and exit  
x extra functionality (experts only)

Command (m for help):

我们只需要使用到fdisk的的几个主要命令

m：使用帮助

n: 创建一个分区

d: 删除一个分区

p:打印分区信息

t:修改分区ID，通过修改ID我们可以改变分区格式，例如windows的fat32格式id为6和linux下ext3格式id为83

w:保存分区信息并退出

三：创建分区

1,打印显示已有分区

输入p

Command (m for help): p

Disk /dev/sdc: 1967 MB, 1967128576 bytes  
57 heads, 56 sectors/track, 1203 cylinders  
Units = cylinders of 3192 * 512 = 1634304 bytes  
Sector size (logical/physical): 512 bytes / 512 bytes  
I/O size (minimum/optimal): 512 bytes / 512 bytes  
Disk identifier: 0x00000000

Device Boot Start End Blocks Id System  
/dev/sdc1 1 23 36680 83 Linux  
/dev/sdc2 24 34 17556 83 Linux

2，删除已有分区

输入d之后选择分区编号

Command (m for help): d  
Partition number (1-4): 1

Command (m for help): d  
Selected partition 2

3，新建分区

3.1建立主分区

输入n

Command (m for help): n  
Command action  
e extended  
p primary partition (1-4)  
p

Partition number (1-4): 1  
First cylinder (1-1203, default 1): 1  
Last cylinder, +cylinders or +size{K,M,G} (1-1203, default 1203): 23  
输入n后选择p建立一个主分区，选择分区编号为1，分区起始柱面位置(first cylinder)选择1，末端位置（last cylinder）选择23

3.2建立扩展分区

Command (m for help): n  
Command action  
e extended  
p primary partition (1-4)  
e  
Partition number (1-4): 2  
First cylinder (24-1203, default 24):  
Using default value 24  
Last cylinder, +cylinders or +size{K,M,G} (24-1203, default 1203):  
Using default value 1203  
输入n后选择e建立一个扩展分区，选择编号2，起始位置和末端选择默认的24~1023

3.3建立逻辑分区

Command (m for help): n  
Command action  
l logical (5 or over)  
p primary partition (1-4)  
l  
First cylinder (24-1203, default 24):  
Using default value 24  
Last cylinder, +cylinders or +size{K,M,G} (24-1203, default 1203): 43  
输入n后选择l建立一个逻辑分区，选选择起始和末端位置为24～43

建立第二个逻辑分区

Command (m for help): n  
Command action  
l logical (5 or over)  
p primary partition (1-4)  
l  
First cylinder (44-1203, default 44):  
Using default value 44  
Last cylinder, +cylinders or +size{K,M,G} (44-1203, default 1203):  
Using default value 1203

三修改分区ID

输入t

Command (m for help): t  
Partition number (1-6): 1  
Hex code (type L to list codes): 6  
Changed system type of partition 1 to 6 (FAT16)  
选择编号为1的分区，这里我将id设为6（FAT16格式，ext32格式的id为83）

同理修改其他分区的id

四，保存退出

Command (m for help): p

Disk /dev/sdb: 1967 MB, 1967128576 bytes  
57 heads, 56 sectors/track, 1203 cylinders  
Units = cylinders of 3192 * 512 = 1634304 bytes  
Sector size (logical/physical): 512 bytes / 512 bytes  
I/O size (minimum/optimal): 512 bytes / 512 bytes  
Disk identifier: 0x00000000

Device Boot Start End Blocks Id System  
/dev/sdb1 1 23 36680 6 FAT16  
/dev/sdb2 24 1203 1883280 5 Extended  
/dev/sdb5 24 43 31892 83 Linux  
/dev/sdb6 44 1203 1851332 83 Linux

这里可以看见我刚刚建立的分区

输入w

Command (m for help): w  
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.  
The kernel still uses the old table. The new table will be used at  
the next reboot or after you run partprobe(8) or kpartx(8)

WARNING: If you have created or modified any DOS 6.x  
partitions, please see the fdisk manual page for additional  
information.  
Syncing disks.  
保存退出，之前我以为到这一步就完事来，其实不然还有下面的

四：格式化分区

在第三步之后你会发现你的sd卡丝毫反应，这是因为第三步仅仅是写入分区表信息而没有完成格式化

FAT格式分区格式化命令 mkfs.vfat /dev/sdb1

ext3格式分区的格式化命令 mkfs.ext3 /dev/sdb5

这里要注意，如果你紧接着就使用这两个命令进行格式化会提示

yang@yang-pc:~$ sudo mkfs.ext3 /dev/sdb3  
mke2fs 1.41.12 (17-May-2010)  
无法对 /dev/sdb3 进行 stat 调用 --- 没有那个文件或目录  
这是因为我们写进去的分区表还未生效，把SD卡取下，再插入就OK了，这下你可以在/dev/sd\*看到你建立的分区

然后使用格式化命令分区格式化对应格式的分区，如果你SD卡原先有东西，再格式化后面加 -I 参数，覆盖掉里面的内容

五，修改分区名字

在第四步完成之后就可以看见我们刚在建立的分区被自动挂载到/media/下了。可是这些分区的名字还是一串难看的字符

因此们需要修改分区的名字也就是Label

修改ext3格式的label命令 e2label /dev/sdb labelname

修改fat格式的我目前还未找到好的方法，只能在windows下重名的方法解决

ps:如有疑问，可以给我留言，如有哪位仁兄找到更好的方法命名FAT格式分区名字，希望告知，感激不尽