# 第七章 磁盘分区管理

### 环境介绍：

* CentOS 7系统下有两个20GB的硬盘

### 项目需求：

* 实现第二块新加的磁盘分区、格式化、挂载

### 工具说明：

* fdisk：只支持mbr分区
* gdisk：只支持gpt分区
* parted：同时支持mbr和gpt分区

## （一）MBR小于2TB分区方案

#### 工具介绍：fdisk

fdisk是一个创建和维护分区表的程序，它兼容DOS类型的分区表、BSD或者SUN类型的磁盘列表

### 1、列出所有磁盘，找到要被分区的磁盘

```
[root@zker ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk 
sr0              11:0    1 1024M  0 rom 
```

### 2、使用fdisk工具进行分区（依次输入：m、n、p、1、回车、回车、w）

```
[root@zker ~]# fdisk /dev/sdb  //选择分区的装置
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


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
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   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): n //增加一个新的磁盘
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p //设置主分区
Partition number (1-4, default 1): 1 //设置一个分区
First sector (2048-41943039, default 2048): //默认设置，回车
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): //默认设置，回车 
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set

Command (m for help): w //保存设置
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
```

### 3、列出磁盘，查看分区状态

```
[root@zker ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk 
└─sdb1            8:17   0   20G  0 part 
```

### 4、使用mkfs工具进行磁盘格式化

```
[root@zker ~]# mkfs.xfs /dev/sdb1 
```

### 5、使用blkid工具查看分区信息

```
[root@zker ~]# blkid /dev/sdb1 
/dev/sdb1: UUID="45fd48b0-9ade-4eab-b5bc-526f08ba8825" TYPE="xfs" 
```

### 6、将/dev/sdb1挂载到/home目录下

```
[root@zker ~]# mount /dev/sdb1  /home/
```

### 7、查看分区挂载情况

```
[root@zker ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  4.7G   13G  28% /
devtmpfs                 470M     0  470M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  8.4M  478M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1               1014M  166M  849M  17% /boot
tmpfs                     98M   12K   98M   1% /run/user/42
tmpfs                     98M     0   98M   0% /run/user/0
/dev/sdb1                 20G   33M   20G   1% /home
```

### 8、将/dev/sdb1分区设置为开机自动挂载

```
[root@zker ~]# vim /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Tue Oct  8 19:10:27 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=80d1e1fd-153b-427f-b8ba-59ce34fbf06e /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sdb1                                       xfs     defaults        0 0

```

## （二）GPT大于2TB分区方案

#### 工具介绍：parted

parted的操作都是实时的，也就是说你执行了一个分区的命令，他就实实在在地分区了，而不是像fdisk那样，需要执行w命令写入所做的修改， 所以进行parted的测试千万注意不能在生产环境中。

### 1、

### 2、

### 3、
