Linux parted命令

Linux parted命令:分区维护程序。

Linux parted命令 功能描述

使用parted命令可以对磁盘进行分区和调整管理分区大小,它比fdisk更加灵活,功能也更丰富,同时还支持GUID分区表。parted同时支持交互模式和非交互模式操作。它除了能够进行分区的添加、删除等常见操作外,还可以移动和复制分区、指定分区类型及调整分区大小。与fdisk的交互模式不同,在parted交互模式下执行命令,一旦按回车键确认,命令就马上执行,对磁盘的更改就立刻生效。

Linux parted命令 语法

parted [选项] [设备 [命令 [参数]]]

命令中各选项的含义如表所示。

parted命令选项含义

parted命令的命令部分描述如表所示。
parted命令的命令部分描述

Linux parted命令 示例

在磁盘/dev/sda中创建、删除、查看分区及转换分区的类型

(1)查看分区信息

[root@rhel ~]# parted /dev/sda print -s
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 537GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number   Start    End    Size    Type     File system     标志
 1        1049kB  106MB  105MB  primary    ext4          启动
 2        106MB  161GB  161GB  primary    ext4
 3        161GB  163GB  2147MB primary    linux-swap(v1)
//查看磁盘/dev/sda的分区信息,共有3个分区

(2)创建主分区

[root@rhel ~]# parted /dev/sda mkpart primary 163G 263G
警告: WARNING: the kernel failed to re-read the partition table on /dev/sda (设备或资源忙). As a result, it may not reflect all of your
changes until after reboot.
//创建一个大小为100GB的主分区,从163GB开始,263GB结束

(3)删除主分区

[root@rhel ~]# parted /dev/sda rm 4
警告: WARNING: the kernel failed to re-read the partition table on /dev/sda (设备或资源忙). As a result, it may not reflect all of your
changes until after reboot.
信息: You may need to update /etc/fstab.
//删除分区号码为4的分区

(4)创建扩展分区

[root@rhel ~]# parted /dev/sda mkpart extended 163G 536.9G
警告: WARNING: the kernel failed to re-read the partition table on /dev/sda (设备或资源忙). As a result, it may not reflect all of your
changes until after reboot.
//创建一个大小为373.9GB的扩展分区分区,从163GB开始,536.9GB结束

(5)创建逻辑驱动器

[root@rhel ~]# parted /dev/sda mkpart logical 163G 263G
警告: WARNING: the kernel failed to re-read the partition table on /dev/sda (设备或资源忙). As a result, it may not reflect all of your
changes until after reboot.
//创建一个大小为100GB的逻辑驱动器,从163GB开始,263GB结束

(6)对分区指定分区类型

[root@rhel ~]# parted /dev/sda mkfs
WARNING: you are attempting to use parted to operate on (mkfs) a file system.
parted's file system manipulation code is not as robust as what you'll find in
dedicated, file-system-specific packages like e2fsprogs. We recommend
you use parted only to manipulate partition tables, whenever possible.
Support for performing most operations on most types of file systems
will be removed in an upcoming release.
警告:The existing file system will be destroyed and all data on the partition will be lost. Do you want to continue?
是/Yes/否/No? yes               //输入yes确认设置分区类型
分区编号?5                  //指定磁盘分区编号
文件系统类型? [ext2]? ext2      //指定分区类型
警告:WARNING: the kernel failed to re-read the partition table on/dev/sda(设备或资源忙). As a result, it may not reflect all of your
changes until after reboot.
//指定分区号码为5的分区类型是ext2
[root@rhel ~]# parted /dev/sda print -s
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 537GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number     Start       End       Size       Type        File system       标志
 1        1049kB      106MB      105MB   primary        ext4              启动
 2        106MB       161GB      161GB   primary        ext4
 3        161GB       163GB      2147MB  primary        linux-swap(v1)
 4        163GB       537GB      374GB   extended       lba
 5        163GB       263GB      99.7GB   logical         ext2
//查看磁盘/dev/sda的分区情况,可以看到分区号码为5的分区类型是ext2

(7)重启计算机系统并查看分区

使用以下命令重新启动Linux系统。

[root@rhel ~]# init 6

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程