Linux中“mbr”、“gpt”分区表的设定及swap分区的建立和删除
一“mbr”、“gpt”分区的划分
1. mbr方式划分分区
mbr (类型为dos)划分系统时默认只能有4个主分区,若划分分区时划分了4个主分区后,则不可以再划分新的分区,硬盘中剩下的空间也不可以再用,这时我们要将第四个分区划分时划分为扩展分区(占用硬盘剩下的空间),在扩展分区内再划分逻辑分区来实现系统划分多个分区。扩展分区和逻辑分区一共最多可划分16个。
主分区:可以在硬盘上直接创建主分区,创建后的主分区可以直接使用,用于存储与读取数据;
扩展分区:可以在硬盘上直接创建扩展分区,创建后的扩展分区不可以直接使用,必须在扩展分区上再创建逻辑分区,才能在逻辑分区上存储与读取数据;逻辑分区:不可以在硬盘上直接创建逻辑分区,必须在硬盘上先创建扩展分区后,再在扩展分区上创建逻辑分区,逻辑分区创建后就可以使用了。
2.分区划分
具体步骤:
[aaa@qq.com ~]fdisk /dev/vdb
elcome 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 partitio ##删除分区
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 ##修改分区功能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): ##默认位主分区
Using default response p
Partition number (1-4, default 1): 1 ##主分区id
First sector (2048-20971519, default 2048): ##此分区起始位置(默认为2048)
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M ##分区大小
Partition 1 of type Linux and of size 100 MiB is set
Command (m for help): p ##显示分区
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x193488c3
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
Command (m for help): wq ##保存推出,如果安q表示放弃更改退出
[aaa@qq.com mnt]# partprobe ##同步分区表
[aaa@qq.com mnt]cat /proc/partitions ##查看系统识别的分区信息
图 解 :
3.挂载新划分的分区
##新划分的分区是不能用的,应该先格式化,格式化时会默认给它一个文件系统,之后便可挂载。
[aaa@qq.com ~]# mount /dev/vdb2
mount: can't find /dev/vdb2 in /etc/fstab
[aaa@qq.com ~]# mkfs.xfs /dev/vdb1 ##格式化
[aaa@qq.com ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
/dev/vdb1: UUID="c383b1ff-56ef-4f43-932f-fd383f471c5e" TYPE="xfs"
/dev/vdb2: UUID="4080e068-afc0-49c6-80c2-56e8ef184471" TYPE="xfs"
##显示可用
mount /dev/vdb2 /mnt ##临时挂载
[aaa@qq.com ~]#vim /etc/fstab ##永久挂载
--->编辑内容<---
/dev/vdb1 /mnt xfs defaults 0 0
## 设备 目录 类型 默认参数类型 不备份 不检测
[aaa@qq.com ~]#mount -a ##使/etc/fstab中记录的挂载策略生效
图 解 :
3.设定分区方式为gpt
GPT 分区表
如果你电脑硬盘的容量超过了2TB,或者你电脑使用了UEFI,那么,GPT分区表更适合您的需求。GPT是新一代分区表格式,能很好的管理大容量硬盘,很好的与UEFI相配合。GPT分区表,没有扩展分区与逻辑分区的概念,所有分区都是主分区,最多可以划分出128个分区。
[aaa@qq.com ~]# parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)mklabel
New disk label type? gpt
Warning: The existing disk label on /dev/vdb will be destroyed and all data on this disk will be lost. Do you
want to continue?
Yes/No? y
(parted) quit
[aaa@qq.com ~]# fdisk -l ##查看其属性为gpt分区方式
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: gpt ##成功改为gpt分区方式
图 解 :
二、swap分区的设定
(1)建立一个swap分区
1. [aaa@qq.com ~]# fdisk /dev/vdb ##新建一个分区
2.[aaa@qq.com ~]# fdisk /dev/vdb ##修改分区类型为“82 Linux swap / ##So c1 DRDOS/sec (FAT-”
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): t ##转换分区类型
Partition number (1,2, default 2): 2 ##选择分区id
Hex code (type L to list all codes): 82 ##选择修改类型
Changed type of partition 'Linux' to 'Linux swap / Solaris'
Command (m for help): p
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0003b697
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 411647 102400 82 Linux swap / Solaris ##修改类型成功
3.[aaa@qq.com ~]# partprobe ##手动更新同步分区表
[aaa@qq.com ~]# cat /proc/partitions
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 102400 vdb1
253 18 102400 vdb2 ##划分的swqp分区
4.[aaa@qq.com ~]#mkswap /dev/vdb2 ##建立swap(交换)分区
5.[aaa@qq.com ~]#swapon -a /dev/vdb2 ##自动启用所有的swap装置
6.[aaa@qq.com ~]#vim /etc/fstab ##永久挂载
--->编辑内容<---
/dev/vdb2 swap swap defaults 0 0
7.[aaa@qq.com ~]#swapon -a ##使/etc/fstab中记录的挂载策略生效
(2)删除swap分区
1.删除"/etc/fsvdtab"内之前编辑内容。
2.用“swapoff /dev/vdb2 ”命令删除swap分区。
3.用“swapon -s ”查看swap分区,此时已无显示。
使用MBR分区表时,分区分为主分区、扩展分区、逻辑分区,三种类型。
主分区:可以在硬盘上直接创建主分区,创建后的主分区可以直接使用,用于存储与读取数据;
扩展分区:可以在硬盘上直接创建扩展分区,创建后的扩展分区不可以直接使用,必须在扩展分区上再创建逻辑分区,才能在逻辑分区上存储与读取数据;
逻辑分区:不可以在硬盘上直接创建逻辑分区,必须在硬盘上先创建扩展分区后,再在扩展分区上创建逻辑分区,逻辑分区创建后就可以使用了。