且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

磁盘分区工具fdisk 详解

更新时间:2022-08-14 21:08:19


一、什么是磁盘分区工具

磁盘分区工具一种可以帮助你配置和管理分区的工具。它可以删除或者格式化分区,也可以重新设置分区大小以及移动你的分区。

二、为什么要使用磁盘分区工具

如果不对磁盘进行分区,所有的文件都保存在一个分区内,这样如果其中一个文件受到病毒感染,其他文件的安全性就会受到很大的威胁。分区后也便于文件的查找与管理,对磁盘进行合理的分区更有提高磁盘的有效利用率。


三、如何使用磁盘分区工具

Linux系统中的磁盘分区工具主要是fdisk。本文也将着重对其进行介绍


fdisk ---Partition table manipulator for Linux (对Linux系统进行磁盘分区的工具)

语法

1
2
       fdisk [-uc] [-b sectorsize] [-C cyls] [-H heads] [-S sects] device
       fdisk -l [-u] [device...]


常用参数

-l 列出指定设备的磁盘分区信息,默认显示 /proc/partitions的分区信息


管理分区

# fdisk DEVICE

fdisk 提供了一个交互式接口来管理分区,分别用于不同的管理功能,所有操作均在内存中完成;没有直接同步到磁盘:


1
2
3
4
5
6
7
8
9
10
p: 显示现有分区表;
n: 创建新分区
d: 删除现有分区
t: 修改分区文件系统类型
l: 显示fdisk所支持的文件系统代码
 
q: 不保存退出
m: 显示帮助信息
b: 设置卷标
v: 检验分区


查看/dev/sdb磁盘信息

1
2
3
4
5
6
7
[root@1inux ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 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: 0x00000000


给/dev/sdb磁盘进行分区,

1
2
3
4
5
6
7
8
9
10
[root@1inux ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x6a09b062.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
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 ” 可以查看支持的选项:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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)


输入“n” 然后回车新建分区

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

选择要创建的分区类型:e-创建扩展分区;p-创建主分区,在此我们选择创建主分区输入“p”

1
Partition number (1-4):

提示要输入要创建主分区的ID号,在1-4之间,在此我们输入1

1
2
Partition number (1-4): 1
First cylinder (1-10443, default 1):


设置分区起始的柱面,直接回车就行:然后如下:

1
2
First cylinder (1-10443, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443):


此处是设置分区结束柱面,也可指定要分区的大小,可支持k、M、G单位,在此我们设置一个大小为5G的分区,+5G ,然后回车

1
Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): +5G


通过输入‘p’可以查看已经建立的分区,

1
2
3
4
5
6
7
8
9
Command (m for help): p
Disk /dev/sdb: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 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: 0x6a09b062
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         654     5253223+  83  Linux

查看分区设置无误后可以通过键入“w” 来保存对磁盘的更改设置

1
2
3
4
5
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@1inux ~]#



到此 已经使用fdisk命令在/dev/sdb磁盘上创建了一个/dev/sdb1大小为5G的主分区,


然后我们通过内核查看系统是否已经识别了我们刚才创建的分区

1
2
3
4
5
6
7
8
9
10
11
12
[root@1inux ~]# cat /proc/partitions
major minor  #blocks  name
   8        0  125829120 sda
   8        1     122880 sda1
   8        2   31426560 sda2
   8        3   10485760 sda3
   8        4          1 sda4
   8        5   10485760 sda5
   8        6    6291456 sda6
   8        7    2097152 sda7
   8       16   83886080 sdb
   8       17    5253223 sdb1    //发现已经被识别



如果发现分区未被识别,我们可以通知内核强制重读分区


1
2
3
4
CentOS 5: partprobe /dev/DEVICE
CentOS 6: partx,kpartx
 partx -a /dev/DEVICE
 kpartx -af /dev/DEVICE

注意:如果执行一次命令,未能成功读取分区表,可重复执行2次或以上


本文转自 1inux 51CTO博客,原文链接:http://blog.51cto.com/1inux/1629370