1. 为什么要对linux磁盘进行分区
对磁盘分区可以提升数据的安全性, 有助于数据读取的速度与效能的提升!
2. 如何对磁盘分区
1. 前置条件
语法
fdisk (选项) (参数)
选项
-b<分区大小>:指定每个分区的大小;
-l:列出指定的外围设备的分区表状况;
-s<分区编号>:将指定的分区大小输出到标准输出上,单位为区块;
-u:搭配"-l"参数列表,会用分区数目取代柱面数目,来表示每个分区的起始地址;
-v:显示版本信息。
实例
[root@donl-pc donl]# fdisk /dev/sdb
输入m
查看命令(罗列出的为常用的命令)
Command (m for help): m
Command action
d delete a partition # 删除分区
l list known partition types # 列出分区信息
m print this menu # 显示操作帮助
n add a new partition # 添加一个新的分区
p print the partition table # 显示分区表单
q quit without saving changes # 不保存退出
w write table to disk and exit # 写入操作并退出
2. 开始分区
p
: 查看磁盘分区信息
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x01aef726
Device Boot Start End Blocks Id System
n
: 新建一个分区
Command (m for help): n # 开始新建分区
Command action
e extended # 扩展分区
p primary partition (1-4) # [1]主分区
p # 输入p创建主分区
Partition number (1-4): 1 # 分区编号
First cylinder (1-652, default 1): 1 # [2]柱面起始编号
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): 300 # 柱面结束编号
注意:
[1] 在linux系统中 扩展分区与主分区个数之和只能是4个, 但是可以使用扩展分区再将其划分为逻辑分区(数量不限)
[2]: 可以使用的柱面编号进行分区, 也可以使用直接给定大小(+1024M
)
再次输入p
查看分区情况
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x01aef726
Device Boot Start End Blocks Id System
/dev/sdb1 1 300 2409718+ 83 Linux # 这是新建的分区
- 新建扩展分区
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e # 选择扩展分区
Partition number (1-4): 2
First cylinder (301-652, default 301):
Using default value 301
Last cylinder, +cylinders or +size{K,M,G} (301-652, default 652): 500
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x01aef726
Device Boot Start End Blocks Id System
/dev/sdb1 1 300 2409718+ 83 Linux
/dev/sdb2 301 500 1606500 5 Extended # 新建的扩展分区
注意: 新建的扩展分区无法直接格式化使用, 必须先将其划分为逻辑分区才可以使用
- 将扩展分区划分为逻辑分区
Command (m for help): n
Command action
l logical (5 or over) # 划分为扩展分区后, 此处发生了变化, 表示划分逻辑分区
p primary partition (1-4)
l # 选择划分逻辑分区
First cylinder (301-500, default 301):
Using default value 301
Last cylinder, +cylinders or +size{K,M,G} (301-500, default 500): 500
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x01aef726
Device Boot Start End Blocks Id System
/dev/sdb1 1 300 2409718+ 83 Linux
/dev/sdb2 301 500 1606500 5 Extended
/dev/sdb5 301 500 1606468+ 83 Linux # 新划分的逻辑分区, 其编号是从5开始的
- 将下面的分区都划分为了一个主分区, 但是你也可以做其他划分
- 保存修改
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
磁盘格式化
1. 为什么要对磁盘格式化
磁盘分区需要先对其格式化才能正常使用
2. 如何格式化刚分区的磁盘
1. 前置条件
语法
mkfs (选项) (参数)
选项
fs:指定建立文件系统时的参数;
-t<文件系统类型>:指定要建立何种文件系统;
-v:显示版本信息与详细的使用方法;
-V:显示简要的使用方法;
-c:在制做档案系统前,检查该partition是否有坏轨。
2. 格式化分区
先了解一下:
ext2文件系统: 没有日志文件(商业致命缺陷)
ext3文件系统: ext2的升级(健全提高了ext2)
ext4文件系统: ext3的升级 (更加丰富和完善的功能)
点击查看ext2、ext3与ext4的区别
sdb分区信息:
[root@donl-pc donl]# ls -l /dev/sdb*
brw-rw----. 1 root disk 8, 16 3月 23 12:05 /dev/sdb
brw-rw----. 1 root disk 8, 17 3月 23 12:05 /dev/sdb1
brw-rw----. 1 root disk 8, 18 3月 23 12:05 /dev/sdb2
brw-rw----. 1 root disk 8, 19 3月 23 12:05 /dev/sdb3
brw-rw----. 1 root disk 8, 21 3月 23 12:05 /dev/sdb5
- 将
/dev/sdb1
格式化为ext3
[root@donl-pc donl]mkfs -t ext3 /dev/sdb1
- 可以使用
Tab Tab
查看简单其他命令
[root@donl-pc donl]mkfs # 按两下Tab键, 自动弹出下面的
mkfs mkfs.ext2 mkfs.ext4 mkfs.msdos
mkfs.cramfs mkfs.ext3 mkfs.ext4dev mkfs.vfat
使用mkfs.ext4
格式化/dev/sdb3
(/dev/sdb3
逻辑分区)
[root@donl-pc donl]mkfs.ext3 /dev/sdb3
- 划分其他的
[root@donl-pc donl]mkfs.ext4 /dev/sdb5