centos8平台使用parted管理分区

Posted architectforest

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos8平台使用parted管理分区相关的知识,希望对你有一定的参考价值。

一,parted的用途

parted是GNU发布的强大的分区工具,

parted命令可以划分单个分区大于2T的GPT格式的分区,也可以划分普通的MBR分区。

因为fdisk命令对于大于2T的分区无法划分,所以用fdisk无法看到parted划分的GPT格式的分区

 

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: https://github.com/liuhongdi/

 说明:作者:刘宏缔 邮箱: 371125307@qq.com

 

二,两种分区表:MBR分区表/GPT分区表的比较

MBR分区表:(MBR含义:主引导记录)
所支持的最大卷:2T (T: terabytes,1TB=1024GB)
对分区的设限:最多4个主分区或3个主分区加一个扩展分区。

GPT分区表:(GPT含义:GUID Partition Table,即GUID分区表)
支持最大卷:18EB,(E:exabytes,1EB=1024TB)
每个磁盘最多支持128个分区

所以如果要大于2TB的卷或分区就必须得用GPT分区表

 

三,parted命令所属的包及安装

1,所属的包

[root@blog ~]# whereis parted
parted: /usr/sbin/parted /usr/share/man/man8/parted.8.gz /usr/share/info/parted.info.gz

[root@blog ~]# rpm -qf /usr/sbin/parted
parted-3.2-36.el8.x86_64

 

2,如果找不到parted命令,可以用yum安装

[root@blog ~]# yum install parted 

 

四,查看parted的版本和帮助

1,查看版本

[root@blog ~]# parted --version
parted (GNU parted) 3.2
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by <http://git.debian.org/?p=parted/parted.git;a=blob_plain;f=AUTHORS>.

 

2,查看帮助

[root@blog ~]# parted --help 

 

五,例子一:打印磁盘与分区列表:

# --list: lists partition layout on all block devices

列出所有块设备上的分区安排

[root@blog ~]# parted --list
Error: /dev/vdb: unrecognised disk label
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 537GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:

Model: Virtio Block Device (virtblk)
Disk /dev/vda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End    Size   Type     File system  Flags
 1      1049kB  107GB  107GB  primary  xfs          boot

说明:通过上面的命令输出我们可以看到:

一共有两块磁盘: /dev/vdb: 537GB

/dev/vda   107GB

在生产环境中我们也是通用的做法:系统盘和数据盘分开,

避免系统对数据有影响

/dev/vda的Partition Table是msdos,

已经经过了分区,

可以看到它的分区表:File system也已格式化成了xfs文件系统

/dev/vdb的Partition Table是unknown

说明它还没有做分区

 

六,例子二:用parted给/dev/vdb创建一个指定大小的分区

1,进入parted

[root@blog ~]# parted /dev/vdb

 

2,查看帮助

(parted) help
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted

 

3,打印分区的信息

(parted) print
Error: /dev/vdb: unrecognised disk label
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 537GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags: 

 

4,创建一个新的gpt类型的空磁盘分区表

# mklabel用来指定分区表的类型

(parted) mklabel gpt
(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 537GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start  End  Size  File system  Name  Flags

已可以看到分区表的类型被指定为gpt

但还没有分区

 

5,创建分区

#mkpart primary 创建一个主分区类型

(parted) mkpart primary 0 537GB
Warning: The resulting partition is not properly aligned for best performance: 34s % 2048s != 0s
Ignore/Cancel? C

上面提到分区没有按照最优的性能对齐,我们选Cancel,

换一个方式指定分区大小

解决办法:

#mkpart primary 0% 100%     使用百分比可以解决对齐的问题

(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 537GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start  End  Size  File system  Name  Flags

(parted) mkpart primary 0% 100%
(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 537GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End    Size   File system  Name     Flags
 1      1049kB  537GB  537GB               primary

检查一下:

#align-check optimal 1     检查分区的对齐情况,提示aligned则表示已对齐

(parted) align-check optimal 1
1 aligned

 

6,删除一个分区

 # rm 加分区的Number,用来删除一个分区

(parted) rm 1
(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 537GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start  End  Size  File system  Name  Flags

删除后可以见到分区已经不见了

 

7,检查指定的分区是否对齐?

#  align-check  用来检查分区是否对齐,提示aligned表示已对齐

(parted) align-check optimal 1
1 aligned

 

8,退出

(parted) quit
Information: You may need to update /etc/fstab.

 

七,创建分区成功后,可以用fdisk -l 或parted --list查看:

1,用parted --list查看分区

[root@blog ~]# parted --list
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 537GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End    Size   File system  Name     Flags
 1      1049kB  537GB  537GB               primary
...

 

2,用 fdisk -l查看分区 

[root@blog ~]# fdisk -l
...

Disk /dev/vdb: 500 GiB, 536870912000 bytes, 1048576000 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
Disklabel type: gpt
Disk identifier: 46C9B0B7-0D78-43D2-9C2D-0059E02F9635

Device     Start        End    Sectors  Size Type
/dev/vdb1   2048 1048573951 1048571904  500G Linux filesystem

 

八,分区创建成功后,还没有做格式化,请查看这一篇文章了解xfs文件系统

 

九,查看centos的版本

[root@blog databak]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core) 

 

以上是关于centos8平台使用parted管理分区的主要内容,如果未能解决你的问题,请参考以下文章

Centos8磁盘管理和配额

linux(centos8): 临时关闭/永久关闭交换分区swap?

linux(centos8): 临时关闭/永久关闭交换分区swap?

【学了就忘Linux文件系统管理】— 10.parted命令分区(二)

GIT系列——centos8环境下搭建GitLab代码仓库管理服务

Linux环境:fdisk 及parted磁盘分区管理工具介绍