parted partition command

Parted is a higher level than fdisk tool that supports a variety of partition table format, including MS-DOS and GPT. It allows users to create, delete, resize, shrink, move and copy partitions, reorganizing disk usage, and copying data to a new hard drive, but when it did not reduce the partition as expected, most of the time I have been wrong, so I recommend that you do not reduce the partition, GParted is a GUI front-end break. As a tool designed for Linux, it is not built into a variety of types of processing fdisk partition is associated with, but it can handle the most common partition formats, including: ext2, ext3, fat16, fat32, NTFS, ReiserFS, JFS , XFS, UFS, HFS and Linux swap partition.

Here we parted major operations are effective immediately, so think carefully before operating to good

A, parted command format

Usage: parted [Option] [device] [command]
command with the "parameter" applied "Device." If no "command", runs in interactive mode.

Options:
-h, display this help information
-l, list all of the system the role of system disk devices, and almost fdisk -l command.
-m, enter the interactive mode, if the back without a disk device is the first to operate
-s, script mode
-v, Display version

Equipment: Disk device names such as / dev / sda

Instruction: If no "instruction", then parted enters interactive mode.

align-check check the partition N type (min | opt) is aligned
help print general help information, or about the [command] help information
mklabel create a new disk label (partition table)
mkpart create a partition
name to the specified partition naming
print print the partition table, or partition
quit to exit the program
rescue repair lost partitions
resizepart resize partitions
rm delete partition
select the device you want to edit the default specified only for equipment operation, where you can change the specified device
disk_set to change the logo on the selected device
disk_toggle selected switch flag status on the device
set to change the partition labeled
toggle settings or cancel the partition labeled
unit set the default unit
version display version information

Two, parted interactive mode partition management


Similarly with fdisk, parted can use the command "parted device name" to enter the interactive mode. After entering the interactive mode, you can manage disk partitions through a variety of command parted.

1, the new GPT partitions
here to be parted major operations are effective immediately, so think carefully before the operation to be good.

root @ debian: ~ # parted / dev / sdb # disk / dev / sdb to partition
the GNU Parted 3.2
the Using / dev / sdb
is available for purchase to the GNU Parted Type 'Help' to View A List of Commands!.
(parted) # If you forget What are the available commands press the Tab key twice to display so instructions are available, and also supports command Tab filled, and up and down to find historical record. The available commands are as follows:
align = left-the Check disk_toggle mklabel mktable Print Rescue resizepart the SELECT Toggle Version
disk_set Help mkpart name quit of a resize RM the SET Unit
(parted) Help mklable # View mklable command usage and support partitioned tables
mklabel, mktable LABEL-TYPE create a new disklabel (partition table) # usage with mktable is the same effect
LABEL-tYPE is one of: atari , aix, amiga, bsd, dvh, gpt, mac, msdos, pc98, sun, loop # supported partition type
(the parted) mklable gpt # create a gpt partition table, if there is a partition table can also change the order here
Warning:.? At The existing Disk label ON / dev / sdb Destroyed by Will BE ON the this and All the Data Disk by Will BE Lost to the Do you want the Continue
? Yes / No yes # warn the user data on the disk will be destroyed, asking whether to continue , here is the new disk, enter yes carriage return
(parted) print # view the partition table information
Model: ATA VBOX HARDDISK (scsi)
disk / dev / sdb: 5369MB
Sector size (the Logical / PHYSICAL): 512B / 512B
partition table: gpt # gpt partition table so the format
Disk Flags:

Number Start End Size File system Name Flags

(parted) mkpart #输入mkpart新建分区,如果输入mkpart指令不带任何参数,parted会一步步提示用户输入相关信息最终完成创建,如下所示:
Partition name? []? #分区的名字,这里可以直接回车不给分区命名,也可以输入用来标记的名字
File system type? [ext2]? #分区被格式化的文件系统,可以按两次Tab键查看支持的文件系统
affs0 affs6 amufs3 btrfs freebsd-ufs linux-swap ntfs
affs1 affs7 amufs4 ext2 hfs linux-swap(new) reiserfs
affs2 amufs amufs5 ext3 hfs+ linux-swap(old) sun-ufs
affs3 amufs0 apfs1 ext4 hfsx linux-swap(v0) swsusp
affs4 amufs1 apfs2 fat16 hp-ufs linux-swap(v1) xfs
affs5 amufs2 asfs fat32 jfs nilfs2 zfs
File system type? [ext2]? ext4 #输入被格式化的文件系统为ext4,这里必须要输入文件系统
Start? 0% #开始位置,这里要输入0%或2048s或1M都可以但是不要输入别的,下面有说明。
End? 1G #结束位置,默认单位为compact,我们想要用其他单位必须在数字后面加上单位
(parted) mkpart pname ext4 1G 2G #如果想一步就把分区创建好可以像左边一样,pname表示分区名这样分区这里必须有,后面3项目分别是文件系统,开始位置,结束位置
(parted) mkpart pname 2G 100% #如果不想创建文件系统这里可以不加这个选项,后面两项分别是开始位置,和结束位置,100%表示使用全部剩余空间
(parted) p #输入print查看分区情况,如果指令是唯一的直接输入指令的首字母也可以。
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 5369MB #磁盘的总容量
Sector size (logical/physical): 512B/512B #扇区的大小
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 1000MB 999MB ext4 #通过一步步创建的第一个分区
2 1000MB 2000MB 999MB ext4 pname #通过一步就创建的分区
3 2000MB 5368MB 3368MB pname #创建不带文件系统的分区


如果Start位置输入的不是0%或2048s或1M或0G的数值,分区结束会有这个的提示,Warning: The resulting partition is not properly aligned for best performance.Ignore/Cancel? (意思是说警告:生成的分区没有正确对齐以获得最佳性能。忽略/取消? 我们可以输入Ignore以忽略,但是这样并不好)
这是因为在512B扇区的磁盘驱动器上,Parted希望分区从2048的倍数(即1 MiB对齐)扇区开始。

2、设置默认的单位
(parted) unit #输入unit更改默认单位,如果不知道支持那些单位直接回车
Unit? [compact]? #按两次Tab键,列出支持的单位,在parted交互模式下不知道怎么输入就先按两次Tab键看看有什么结果,很好用
% B chs compact cyl GB GiB kB kiB MB MiB s TB TiB
Unit? [compact]? GB #输入更改后的默认单位
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 5.37GB #默认单位以改称GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 0.00GB 1.00GB 1.00GB ext4 #默认单位以改称GB
2 1.00GB 2.00GB 1.00GB ext4 pname
3 2.00GB 5.37GB 3.37GB pname

3、更改分区名

(parted) name #输入name指令更改分区名,输入后直接回车
Partition number? 3 #输入要更改名的分区号
Partition name? [pname]? namep #输入新的名字
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 5.37GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 0.00GB 1.00GB 1.00GB ext4 p
2 1.00GB 2.00GB 1.00GB ext4 pname
3 2.00GB 5.37GB 3.37GB namep #名称已经更改

4、配置分区的标记
parted的分区标记相当于fdisk gdisk里面的分区类型

(parted) set #输入set更改分区标记,输入后直接回车
Partition number? 3 #更改标记的分区
Flag to Invert? #输入更改标记的类型,按二次Tab键查看支持的类型
atvrecv boot esp hp-service legacy_boot msftdata prep
bios_grub diag hidden irst lvm msftres raid
Flag to Invert? lvm #标记为lvm类型
New state? [on]/off? on #输入on回车表示打开
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 5.37GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 0.00GB 1.00GB 1.00GB ext4
2 1.00GB 2.00GB 1.00GB ext4 pname
3 2.00GB 5.37GB 3.37GB pname lvm #分区的标记已经成效


(parted) set #取消标记操作
Partition number? 3
Flag to Invert? lvm
New state? on/[off]? off #添加标记和取消标记的主要区别就在这里,这里要输入off来关闭标记
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 5.37GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 0.00GB 1.00GB 1.00GB ext4
2 1.00GB 2.00GB 1.00GB ext4 pname
3 2.00GB 5.37GB 3.37GB pname #分区的标记已经取消

(parted)

5、删除分区

(parted) rm #输入删除指令rm直接回车,如果后面加上分区号在回车就可以直接删除分区
Partition number? 3 #输入要删除的分区,这里操作一定要慎重,一旦回车就生效了,是不可退的。
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 5.37GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags #成功删除了分区
1 0.00GB 1.00GB 1.00GB
2 1.00GB 2.00GB 1.00GB pname

6、查看剩余可用的空间等信息

(parted) p #先查看分区的信息
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 5.37GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 0.00GB 1.00GB 1.00GB
2 1.00GB 2.00GB 1.00GB pname

可用分区的容量需要用Disk /dev/sdb: 这行提供的总容量减去最后一个分区的结束位置的容量,这里举例中可用分区容量为:5.37GB-2.00GB=3.37G

 

7、调整分区大小
尽量不要对分区进行调整,可能会丢失数据,如果要调整也要先备份数据

(parted) resizepart #输入调整分区的指令resizepart直接回车,这里增加容量,增加容量的分区后面必须得有可用的未分配的分区
Partition number? 2 #输入要更改的分区
End? [2.00GB]? 4G #扩容后这个分区的结束位置,这个要根据扩容前分区结束的位置和增加的容量来确定最终的分区位置
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 5.37GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 0.00GB 1.00GB 1.00GB
2 1.00GB 4.00GB 3.00GB ext4 pname

(parted) resizepart #这里减少容量
Partition number? 2
End? [4.00GB]? 3G
Warning: Shrinking a partition can cause data loss, are you
sure you want to continue?
Yes/No? yes #减少容量会有这样的警告:收缩分区会导致数据丢失,你确定要继续吗?
(parted)

三、非交互式parted分区命令
非交互模式建议把要操作的命令都在文本文件中写出来,确认无误后在进行配置。

1、创建GPT分区表

root@debian:~# parted /dev/sdc mklabel gpt #通过这里命令直接创建GPT分区表
Information: You may need to update /etc/fstab.

root@debian:~# parted /dev/sdc print #通过这里命令直接查看分区信息,print改称p也可以
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdc: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt #创建成功
Disk Flags:

Number Start End Size File system Name Flags

root@debian:~#

2、创建分区

root@debian:~# parted /dev/sdc mkpart pname 0G 1G #创建1G容量的分区pname为分区名必须添加,这里不能直接添加文件系统类型,后面是起始和结束位置

root@debian:~# parted /dev/sdc p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdc: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 1000MB 999MB pname #成功创建,起始位置是0G也会从2048扇区开始创建。

2、更改默认显示单位

root@debian:~# parted /dev/sdc unit GB print #需要在查看指令print前添加unit GB指令来以GB显示
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdc: 5.37GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 0.00GB 1.00GB 1.00GB pname
2 1.00GB 2.00GB 1.00GB pname
3 2.00GB 5.37GB 3.37GB pname

3、更改分区名
root@debian:~# parted /dev/sdc name 3 namep #更改分区3的分区名
root@debian:~# parted /dev/sdc print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdc: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 1000MB 999MB pname
2 1000MB 2000MB 999MB pname
3 2000MB 5368MB 3368MB namep #更改成功


4、配置分区的标记

root@debian:~# parted /dev/sdc set 3 lvm #为分区3添加lvm标记
New state? [on]/off? on #输入on回车表示打开
Information: You may need to update /etc/fstab.

root@debian:~# parted /dev/sdc print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdc: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 1000MB 999MB pname
2 1000MB 2000MB 999MB pname
3 2000MB 5368MB 3368MB namep lvm

root@debian:~# parted /dev/sdc set 3 lvm #取消分区3的lvm标记
New state? on/[off]? off #添加标记和取消标记的主要区别就在这里,这里要输入off来关闭标记
Information: You may need to update /etc/fstab.

root@debian:~# parted /dev/sdc print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdc: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 1000MB 999MB pname
2 1000MB 2000MB 999MB pname
3 2000MB 5368MB 3368MB namep #取消分区3的lvm标记成功

5、删除分区

root@debian:~# parted /dev/sdc rm 3 #删除分区3
Information: You may need to update /etc/fstab.

root@debian:~# parted /dev/sdc print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdc: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags #删除分区3成功
1 1049kB 1000MB 999MB pname
2 1000MB 2000MB 999MB pname

6、查看剩余可用的空间等信息

root@debian:~# parted /dev/sdc unit GB print #查看分区的信息,以GB为单位显示
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 5.37GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 0.00GB 1.00GB 1.00GB
2 1.00GB 2.00GB 1.00GB pname

可用分区的容量需要用Disk /dev/sdb: 这行提供的总容量减去最后一个分区的结束位置的容量,这里举例中可用分区容量为:5.37GB-2.00GB=3.37G

7、调整分区大小
尽量不要对分区进行调整,可能会丢失数据,如果要调整也要先备份数据

root@debian:~# parted /dev/sdc resizepart 2 4G #增加分区容量,要输入调整分区的指令及调整的分区,和扩容后这个分区的结束位置,这个要根据扩容前分区结束的位置和增加的容量来确定最终的分区位置
Information: You may need to update /etc/fstab.

root@debian:~# parted /dev/sdc unit GB print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdc: 5.37GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 0.00GB 1.00GB 1.00GB ext4 pname
2 1.00GB 4.00GB 3.00GB ext4 pname #分区2的容量已经增加

root@debian:~# parted /dev/sdc resizepart 2 3G #减小分区的容量
Warning: Shrinking a partition can cause data loss, are you
sure you want to continue?
Yes/No? yes #减少容量会有这样的警告:收缩分区会导致数据丢失,你确定要继续吗?
Information: You may need to update /etc/fstab.

root@debian:~# parted /dev/sdc unit GB print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdc: 5.37GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 0.00GB 1.00GB 1.00GB ext4 pname
2 1.00GB 3.00GB 2.00GB ext4 pname #分区2的容量已经减少


四、parted配额MBR分区
实际生产用对MBR的分区推荐用fdisk来进行操作

1、更改分区表
(parted) mklabel msdos #将现有的GPT分区更改成MBR分区,新磁盘也是通过这个指令来创建MBR分区
Warning: The existing disk label on /dev/sdb will be destroyed
and all data on this disk will be lost. Do you want to
continue? #警告:/dev/sdb上现有的磁盘标签将被销毁这个磁盘上的所有数据都将丢失。你想继续吗?
Yes/No? yes #输入yes继续,我的硬盘有gpt分区表才会提示,如果是新的磁盘不会提示
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos #分区表已经改成MBR
Disk Flags:

Number Start End Size Type File system Flags

2、新建MBR分区

(parted) mkpart #新建第一个分区,MBR磁盘标签不支持分区名下面不会有输入选项
Partition type? primary/extended? primary #新建主分区还是扩展分区
File system type? [ext2]? ext4 #分区的文件系统类型
Start? 0% #起始位置
End? 1G #结束位置
(parted) mkpart primary 1G 2G #不进行交互直接创建主分区,不创建文件系统,
(parted) mkpart extended 2G 100% #直接创建扩展分区,不进行交互
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
1 1049kB 1000MB 999MB primary ext4 lba #新建的主分区
2 1000MB 2000MB 999MB primary lba #新建的主分区
3 2000MB 5369MB 3369MB extended lba #新建的扩展分区

(parted) mkpart #继续创建逻辑分区
Partition type? primary/logical? logical #新建主分区还是逻辑分区,只有创建了扩展分区才会出现逻辑分区的选项,输入logical
File system type? [ext2]? ext4
Start? 2G
End? 3G
(parted) mkpart logical ext4 3G 4G #直接创建逻辑分区同时格式化
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
1 1049kB 1000MB 999MB primary
2 1000MB 2000MB 999MB primary
3 2000MB 5369MB 3369MB extended lba
5 2001MB 3000MB 999MB logical ext4 lba #逻辑分区成功分区号从5开始
6 3001MB 4000MB 999MB logical ext4 lba


其他的操作和gpt分区下一样这里就不说了

 

 

Guess you like

Origin www.cnblogs.com/pipci/p/11372530.html