parted命令

  运维

[root@localhost ~]# parted –help
Usage: parted [OPTION]… [DEVICE [COMMAND [PARAMETERS]…]…] #用法:parted [选项]… [设备 [命令 [参数]…]…]
Apply COMMANDs with PARAMETERS to DEVICE. If no COMMAND(s) are given, run in
interactive mode. #将带有“参数”的命令应用于“设备”。如果没有给出“命令”,则以交互模式运行.

OPTIONs:
-h, –help displays this help message
-l, –list lists partition layout on all block devices #列出所有分区
-m, –machine displays machine parseable output
-s, –script never prompts for user intervention #不交互
-v, –version displays the version
-a, –align=[none|cyl|min|opt] alignment for new partitions

COMMANDs: #子命令,fdisk 的交互模式交互模式不同,在 parted 的交互模式下执行命令,一旦按回车键确认,命令就马上执行,对磁盘的更改就立刻生效
align-check TYPE N check partition N for TYPE(min|opt) alignment #分区校正
check NUMBER do a simple check on the file system #检查指定编号 NUMBER 分区中的文件系统是否有什么错误,需要先umount
cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER copy file system to another partition #将分区 FROM-NUMBER 上的文件系统完整地复制到分区TO-NUMBER 中,作为可选项还可以指定一个来源硬盘的设备名称FROM-DEVICE,若省略则在当前设备上进行复制。
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table) #创建一个新的 LABEL-TYPE 类型的空磁盘分区表,对于PC而言 msdos 是常用的 LABELTYPE。 若是用 GUID 分区表,LABEL-TYPE 应该为 gpt
mkfs NUMBER FS-TYPE make a FS-TYPE file system on partition NUMBER #对指定编号 NUMBER 的分区创建指定类型 FS-TYPE 的文件系 统
mkpart PART-TYPE [FS-TYPE] START END make a partition #创建新分区。PART-TYPE 是以下类型之一:primary(主分 区)、extended(扩展分区)、logical(逻辑分区)。START 和 END 是新分区开始和结束的具体位置。
mkpartfs PART-TYPE FS-TYPE START END make a partition with a file system #创建新分区同时创建文件系统。FS-TYPE 是以下类型一:ext2、fat16、fat32、linuxswap、NTFS、reiserfs、ufs 等
move NUMBER START END move partition NUMBER #将指定编号 NUMBER 的分区移动到从 START 开始 END 结束的位置上。注意:(1)只能将分区移动到空闲空间中。(2)虽然分区被移动了,但它的分区编号是不会改变的
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 #p,显示分区表, 指定编号的分区, 或所有设备的分区表
quit exit program
rescue START END rescue a lost partition near START and END #恢复临近“起始点”、“终止点”的遗失的分区
resize NUMBER START END resize partition NUMBER and its file system #对指定编号 NUMBER 的分区调整大小。分区的开始位置和结束位置由 START 和 END 决定
rm NUMBER delete partition NUMBER #删除指定编号 NUMBER 的分区。
select DEVICE choose the device to edit
set NUMBER FLAG STATE change the FLAG on partition NUMBER #对指定编号 NUMBER 的分区设置分区标记 FLAG。对于 PC 常用的 msdos 分区表来说,分区标记 FLAG 可有如下值:”boot”(引导), “hidden”(隐藏), “raid”(软RAID磁盘阵), “lvm”(逻辑卷), “lba” (LBA,Logic Block Addressing模式)。 状态STATE 的取值是:on 或 off
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
unit UNIT set the default unit to UNIT #设置默认输出时表示磁盘大小的单位为 UNIT,UNIT 的常用取值可以为:‘MB’、‘GB’、‘%’(占整个磁盘设备的百分之多少)、‘compact’(人类易读方式,类似于 df 命令中 -h 参数的用)、‘s’(扇区)、‘cyl’ (柱面)、‘chs’ (柱面cylinders:磁头 heads:扇区 sectors 的地址)
version display the version number and copyright information of GNU Parted

#列出所有分区
[root@localhost ~]# parted -l
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 1049kB 316MB 315MB primary ext4 boot
2 316MB 83.8GB 83.5GB primary ext4
3 83.8GB 85.9GB 2080MB primary linux-swap(v1)

Error: /dev/sdb: unrecognised disk label

#列出所有分区
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 1049kB 316MB 315MB primary ext4 boot
2 316MB 83.8GB 83.5GB primary ext4
3 83.8GB 85.9GB 2080MB primary linux-swap(v1)

(parted)

#分区校正
[root@localhost ~]# parted
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type ‘help’ to view a list of commands.
(parted) align-check
alignment type(min/opt) [optimal]/minimal? minimal
Partition number? 1
1 aligned
(parted)

#创建分区
[root@localhost ~]# parted /dev/sdb
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type ‘help’ to view a list of commands.
(parted) p
Error: /dev/sdb: unrecognised disk label
(parted)
(parted) mklabel gpt #如果使用MBR分区,输入mklabel msdos;使用GPT分区,输入mklabel gpt
(parted)
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 268GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags

(parted) mkpart primary 0 20480 #创建一个20GB的主分区,(mkpart primary 0 -1创建一整块主分区)
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? ignore
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 268GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 17.4kB 20.5GB 20.5GB primary

(parted) mkpart logical 20481 40960
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 268GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 17.4kB 20.5GB 20.5GB primary
2 20.5GB 41.0GB 20.5GB logical

(parted) q #实时保存
Information: You may need to update /etc/fstab.
[root@localhost ~]# parted -l #已经产生
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 1049kB 316MB 315MB primary ext4 boot
2 316MB 83.8GB 83.5GB primary ext4
3 83.8GB 85.9GB 2080MB primary linux-swap(v1)

Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 268GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 17.4kB 20.5GB 20.5GB primary
2 20.5GB 41.0GB 20.5GB logical

[root@localhost ~]# mkfs.ext4 -T largefile /dev/sdb1 #大磁盘格式化

[root@localhost ~]# parted /dev/sdb
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type ‘help’ to view a list of commands.
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 268GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 17.4kB 20.5GB 20.5GB ext4 primary
2 20.5GB 41.0GB 20.5GB logical

(parted) mkpart primary 40961 60G
(parted) mkpart primary 60G 80G
(parted) set 3 lvm on
(parted) set 4 lvm on #增加lvm
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 268GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 17.4kB 20.5GB 20.5GB ext4 primary
2 20.5GB 41.0GB 20.5GB logical
3 41.0GB 60.0GB 19.0GB primary lvm
4 60.0GB 80.0GB 20.0GB primary lvm

(parted) q
Information: You may need to update /etc/fstab.
#lvm
[root@localhost ~]# pvcreate /dev/sdb3
Physical volume “/dev/sdb3” successfully created
[root@localhost ~]# pvcreate /dev/sdb4
dev_is_mpath: failed to get device for 8:20
Physical volume “/dev/sdb4” successfully created
[root@localhost ~]# vgcreate vgdatabackup /dev/sdb3 /dev/sdb4
Volume group “vgdatabackup” successfully created
[root@localhost ~]# lvcreate -L 30G -n dbbackup vgdatabackup
Logical volume “dbbackup” created

[root@localhost ~]# mkfs.ext4 -T largefile /dev/vgdatabackup/dbbackup
[root@localhost ~]# mount /dev/vgdatabackup/dbbackup /tmp

One Reply to “parted命令”

  • partx -d /dev/sda //因为内核中存有部分未调整磁盘的信息,故先将所有信息清零
    partx -a /dev/sda //添加调整后的磁盘分区信息
    partx -s /dev/sda //显示磁盘分区信息

LEAVE A COMMENT

发表评论前,请选对水果【Apple=苹果、Banana=香蕉、Pineapple=菠萝、Grapes=葡萄】 Banana...