# 磁盘分区

## 磁盘分区

`fdisk` 命令：

介绍：查看当前系统硬盘分区和设置分区

格式：fdisk \[option] partition

用法：

* fdisk /dev/sd\* : 给硬盘分区
* fdisk -l /dev/sd\* ：查看分区

```bash
[root@localhost ~]# fdisk -l

磁盘 /dev/sda：42.9 GB, 42949672960 字节，83886080 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理)：512 字节 / 512 字节
I/O 大小(最小/最佳)：512 字节 / 512 字节
磁盘标签类型：dos
磁盘标识符：0x000d8827

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048     5220351     2097152   82  Linux swap / Solaris
/dev/sda3         5220352    83886079    39332864   83  Linux

磁盘 /dev/sdb：10.7 GB, 10737418240 字节，20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理)：512 字节 / 512 字节
I/O 大小(最小/最佳)：512 字节 / 512 字节

[root@localhost ~]# fdisk -l /dev/sdb

磁盘 /dev/sdb：10.7 GB, 10737418240 字节，20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理)：512 字节 / 512 字节
I/O 大小(最小/最佳)：512 字节 / 512 字节
```

## 给磁盘分区

```bash
[root@localhost ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中，直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0x66239032 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助)：m
命令操作
   a   toggle a bootable flag  # 切换可引导标志
   b   edit bsd disklabel   # 编辑bsd 磁盘标签
   c   toggle the dos compatibility flag  # 切换dos兼容性标志
   d   delete a partition  # 删除磁盘分区（重要）
   g   create a new empty GPT partition table # 创建一个新的空GPT分区表
   G   create an IRIX (SGI) partition table # 创建一个IRIX（SGI）分区表
   l   list known partition types # 列出已知的分区类型
   m   print this menu # 输出此菜单  
   n   add a new partition # 添加一个分区（重要）
   o   create a new empty DOS partition table # 创建一个新的空DOS分区表
   p   print the partition table # 输入分区表
   q   quit without saving changes # 退出但不保存
   s   create a new empty Sun disklabel # create a new empty Sun disk label
   t   change a partition's system id  # 更改分区的系统ID
   u   change display/entry units # 更改显示/输入单位
   v   verify the partition table # 验证分区表
   w   write table to disk and exit # 将表写入磁盘并退出
   x   extra functionality (experts only) # 额外功能（仅限专家）

命令(输入 m 获取帮助)：n
Partition type:
   p   primary (0 primary, 0 extended, 4 free) # 主分区
   e   extended # 扩展分区
Select (default p): p
分区号 (1-4，默认 1)：1
起始 扇区 (2048-20971519，默认为 2048)：
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-20971519，默认为 20971519)：+2G
分区 1 已设置为 Linux 类型，大小设为 2 GiB

命令(输入 m 获取帮助)：w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@localhost ~]# fdisk -l /dev/sdb

磁盘 /dev/sdb：10.7 GB, 10737418240 字节，20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理)：512 字节 / 512 字节
I/O 大小(最小/最佳)：512 字节 / 512 字节
磁盘标签类型：dos
磁盘标识符：0x66239032

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
```

> linux磁盘最多只能有四个主分区，要分配超过四个的分区则需要将其中一个分区设置为扩展分区
>
> fdisk 命令只能分配小雨2T的磁盘，大于2T的要使用 parted 命令
