Parted 分区

Posted y_zilong

tags:

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

1、查看硬盘信息

[root@5gxx-2-32 ~]# fdisk -l   #可以看到/dev/vdb 4T

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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
Disk label type: dos
Disk identifier: 0x000a4f38

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    83886046    41941999+  83  Linux

Disk /dev/vdb: 4295.0 GB, 4294967296000 bytes, 8388608000 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

2、建立磁盘标签

#因为parted命令只能针对gpt格式的磁盘进行操作,所以这里必须将新建的磁盘标签格式设为gpt。

[root@5gxx-2-32 ~]# parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt   #新建磁盘标签类型为GPT                                                    
(parted) p     #使用print对磁盘进行查看,可以查看磁盘没有进行分区
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 4295GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

(parted) 

3、创建分区,并分配合适的大小

分区
  命令格式:
      mkpart PART-TYPE [FS-TYPE] START END
             PART-TYPE(分区类型)
             primary
             主分区
             logical
             逻辑分区
             extended
             扩展分区
             FS-TYPE(文件系统类型)
             ext4
             ext3
             ext2
             xfs
            其他......
                    START
                    设定磁盘分区起始点;可以为0,numberMiB/GiB/TiB;
                    0
                    设定当前分区的起始点为磁盘的第一个扇区;
                    1G
                    设定当前分区的起始点为磁盘的1G处开始;
                    END
                    设定磁盘分区结束点;
                    -1
                    设定当前分区的结束点为磁盘的最后一个扇区;
                    10G
                    设定当前分区的结束点为磁盘的10G处;

#使用mkpart primary 0% n%创建主分区,n为要分的分区占整个磁盘的百分比.(mkpart extended创建扩展分区).
#例如我这里想要把这个8T的硬盘分成三块,其中前两块分别占2T,最后一块占4T,则可以写为:
#mkpart primary 0% 25%
#mkpart primary 26% 50%
#mkpart primary 51% 100% 

(parted) mkpart primary xfs 0 2145                                        
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? c     #取消                                                    
(parted) p                                                                
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 4295GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

(parted) mkpart primary 0% 50%        #分成两个一样大小的分区
(parted) mkpart primary 51% 100%
(parted) p                                                                
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 4295GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  2147GB  2147GB               primary
 2      2190GB  4295GB  2105GB               primary
 
(parted) quit                                                             
Information: You may need to update /etc/fstab.

4、查看分区并分区格式化挂载

[root@5gxx-2-32 ~]# fdisk -l

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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
Disk label type: dos
Disk identifier: 0x000a4f38

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    83886046    41941999+  83  Linux
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/vdb: 4295.0 GB, 4294967296000 bytes, 8388608000 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
Disk label type: gpt
Disk identifier: 3AEA9541-28C4-4740-B96D-32055555E3DD


#         Start          End    Size  Type            Name
 1         2048   4194303999      2T  Microsoft basic primary
 2   4278190080   8388605951    1.9T  Microsoft basic primary
[root@5gxx-2-32 ~]# mkfs.ext4 /dev/vdb1
[root@5gxx-2-32 ~]# mkfs.ext4 /dev/vdb2
[root@5gxx-2-32 ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      ext4       40G  2.2G   36G   6% /
devtmpfs       devtmpfs  7.6G     0  7.6G   0% /dev
tmpfs          tmpfs     7.6G     0  7.6G   0% /dev/shm
tmpfs          tmpfs     7.6G   41M  7.6G   1% /run
tmpfs          tmpfs     7.6G     0  7.6G   0% /sys/fs/cgroup
tmpfs          tmpfs     1.6G     0  1.6G   0% /run/user/1040
[root@5gxx-2-32 ~]# mkdir -p /data/data1
[root@5gxx-2-32 ~]# mkdir -p /data/data2
[root@5gxx-2-32 ~]# mount /dev/vdb1 /data/data1
[root@5gxx-2-32 ~]# mount /dev/vdb2 /data/data2
[root@5gxx-2-32 ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      ext4       40G  2.2G   36G   6% /
devtmpfs       devtmpfs  7.6G     0  7.6G   0% /dev
tmpfs          tmpfs     7.6G     0  7.6G   0% /dev/shm
tmpfs          tmpfs     7.6G   41M  7.6G   1% /run
tmpfs          tmpfs     7.6G     0  7.6G   0% /sys/fs/cgroup
tmpfs          tmpfs     1.6G     0  1.6G   0% /run/user/1040
/dev/vdb1      ext4      2.0T   81M  1.9T   1% /data/data1
/dev/vdb2      ext4      1.9T   81M  1.8T   1% /data/data2

[root@5gxx-2-32 ~]#echo '/dev/vdb1 /data/data1   ext4   defaults,_netdev   0 0' >>/etc/fstab
[root@5gxx-2-32 ~]#echo '/dev/vdb2 /data/data2   ext4   defaults,_netdev   0 0' >>/etc/fstab

以上是关于Parted 分区的主要内容,如果未能解决你的问题,请参考以下文章

parted 分区命令

fdisk与parted的区别以及parted为何可分128个区

Linux当中怎么用parted分区?请写明步骤

linux系统磁盘分区之parted

linux磁盘分区之parted分区工具

使用parted对大于2TB的硬盘分区