linux parted命令

2013-02-05

parted是一个磁盘分区管理管理工具,它比fdisk更加灵活,fdisk不支持GPT功能也更丰富,同时还支持GPT磁盘(GUID Partition Table)。parted同时支持交互模式和非交互模式. 它除了能够进行分区的添加,删除等常见操作外,还可以移动分区, 制作文件系统, 调整文件系统大小, 复制文件系统.

====== 1. 交互式模式 ======

由于MBR分区表只支持2T磁盘,所以大于2T的磁盘必须使用GPT分区表,具体方法如下:

GPT格式的磁盘相当于原来MBR磁盘中原来保留4个partition table的4*16个字节,只留第一个16个字节,类似于扩展分区,真正的partition table在512字节之后,GPT磁盘没有四个主分区的限制。

在提示符下输入parted就会进入交互式模式,如果有多个磁盘的话,我们需要运行select sdx x为磁盘,来进行磁盘的选择

代码

parted>

parted>select sdb     //假设磁盘为sdb

parted>mklabel gpt //将MBR磁盘格式化为GPT GPT分区表:(GPT含义:GUID分区表)

#parted>mklabel msdos //将GPT磁盘转化为MBR磁盘

parted>mkpart primary 0 100 //划分一个起始位置为0,大小为100M的主分区

parted>mkpart primary 100 200 //划分一个起始位置为100M,大小为100M的主分区

#parted>mkpart primary 0 -1 //将整块磁盘分成一个分区

parted>print //打印当前分区

parted>quit //退出

[root@server01 ~]# parted
GNU Parted 1.8.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) select /dev/sda
Using /dev/sda
(parted) p

Model: WD WD2000FYYG (scsi)
Disk /dev/sda: 2000GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 32.3kB 53.7GB 53.7GB primary ext3 boot
2 53.7GB 107GB 53.7GB primary ext3
3 107GB 116GB 8587MB primary linux-swap
4 116GB 2000GB 1884GB extended
5 116GB 716GB 600GB logical ext3
6 716GB 1316GB 600GB logical ext3
7 1316GB 2000GB 684GB logical ext3

(parted)quit
====== 2. 使用parted命令格式 ======

**删除一个分区**

命令格式:
parted device rm partition
实例: 删除分区/dev/sda1
[root@dev3-28 ~]# parted /dev/sda print

Model: MAXTOR ATLAS10K5_73SCA (scsi)
Disk /dev/sda: 73.6GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 32.3kB 10.0GB 10.0GB primary ext3
4 12.1GB 73.6GB 61.5GB extended
5 12.1GB 20.4GB 8390MB logical ext3
6 20.4GB 32.1GB 11.6GB logical ext3
7 32.1GB 73.6GB 41.5GB logical ext3

Information: Don't forget to update /etc/fstab, if necessary.

[root@dev3-28 ~]# parted /dev/sda rm 1
Information: Don't forget to update /etc/fstab, if necessary.

[root@dev3-28 ~]# parted /dev/sda print

Model: MAXTOR ATLAS10K5_73SCA (scsi)
Disk /dev/sda: 73.6GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
4 12.1GB 73.6GB 61.5GB extended
5 12.1GB 20.4GB 8390MB logical ext3
6 20.4GB 32.1GB 11.6GB logical ext3
7 32.1GB 73.6GB 41.5GB logical ext3

Information: Don't forget to update /etc/fstab, if necessary.

**添加一个分区**

mkpart

primary

xfs

Start?:0

End? -1 ----指定到分区最后

命令格式:
parted device mkpart part-type [fs-type] start end
实例: 添加一个大小为3GB的主分区
[root@dev3-28 ~]# parted /dev/sda mkpart primary 32.3kB 3GB
[root@dev3-28 ~]# parted /dev/sda print -s

Model: MAXTOR ATLAS10K5_73SCA (scsi)
Disk /dev/sda: 73.6GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 32.3kB 3002MB 3002MB primary ext3
4 12.1GB 73.6GB 61.5GB extended
5 12.1GB 20.4GB 8390MB logical ext3
6 20.4GB 32.1GB 11.6GB logical ext3
7 32.1GB 73.6GB 41.5GB logical ext3

**制作文件系统**

命令格式:
parted device mkfs
实例: 对刚刚添加的分区制作文件系统。
[root@dev3-28 ~]# parted /dev/sda mkfs
Warning: The existing file system will be destroyed and all data on the partition will be lost. Do you want to continue?
Yes/No? y
Partition number? 1
File system? [ext2]?
Information: Don't forget to update /etc/fstab, if necessary.

[root@dev3-28 ~]# parted /dev/sda print -s

Model: MAXTOR ATLAS10K5_73SCA (scsi)
Disk /dev/sda: 73.6GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 32.3kB 3002MB 3002MB primary ext2
4 12.1GB 73.6GB 61.5GB extended
5 12.1GB 20.4GB 8390MB logical ext3
6 20.4GB 32.1GB 11.6GB logical ext3
7 32.1GB 73.6GB 41.5GB logical ext3

**调整文件系统大小**

命令格式:
parted device resize partition start end
实例: 将分区/dev/sda1的大小增加到5GB。
[root@dev3-28 ~]# parted /dev/sda resize 1 32.3kB 5GB
Warning: You should reinstall your boot loader before rebooting. Read section 4 of the Parted User documentation for more
information.
Information: Don't forget to update /etc/fstab, if necessary.

[root@dev3-28 ~]# parted /dev/sda print -s

Model: MAXTOR ATLAS10K5_73SCA (scsi)
Disk /dev/sda: 73.6GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 32.3kB 5001MB 5001MB primary ext2
4 12.1GB 73.6GB 61.5GB extended
5 12.1GB 20.4GB 8390MB logical ext3
6 20.4GB 32.1GB 11.6GB logical ext3
7 32.1GB 73.6GB 41.5GB logical ext3

**移动分区**

命令格式:
parted device move partition start end
实例: 移动分区/dev/sda1
[root@dev3-28 ~]# parted /dev/sda move 1 5001MB
End? [10.0GB]?
Warning: You should reinstall your boot loader before rebooting. Read section 4 of the Parted User documentation for more
information.
Information: Don't forget to update /etc/fstab, if necessary.

[root@dev3-28 ~]# parted /dev/sda print -s

Model: MAXTOR ATLAS10K5_73SCA (scsi)
Disk /dev/sda: 73.6GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 5001MB 10.0GB 5001MB primary ext2
4 12.1GB 73.6GB 61.5GB extended
5 12.1GB 20.4GB 8390MB logical ext3
6 20.4GB 32.1GB 11.6GB logical ext3
7 32.1GB 73.6GB 41.5GB logical ext3

**注意**: 不同于fdisk命令的使用w功能来确认来写入分区表信息, parted命令执行后立即生效,因此输入时需要谨慎.

实例:
parted /dev/sde
rm 1 删除分区
parted) mkpart
Partition name? []? data1
File system type? [ext2]? xfs
Start? 0
End? 2000GB
(parted) p

Model: SEAGATE ST32000444SS (scsi)
Disk /dev/sde: 2000GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 17.4kB 2000GB 2000GB data1

fdisk -l
WARNING: GPT (GUID Partition Table) detected on '/dev/sde'! The util fdisk doesn't support GPT. Use GNU Parted.

parted print

分类:Linux操作系统 | 标签: |

相关日志

评论被关闭!