linux parted给磁盘分区

概述

        通常我们用的比较多的分区工具是fdisk命令,但由于fdisk只支持MBR分区,MBR分区表最大支撑2T的磁盘,所以无法划分大于2T的分区。而parted工具可以划分单个分区大于2T的GPT格式的分区,也可以划分普通的MBR分区。

1.查看磁盘大小

因磁盘大小超过2T,用fdisk划分磁盘时,最多只划出2T的磁盘。所以,改用parted命令划分

# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0 447.1G  0 disk 
├─sda1            8:1    0     5G  0 part /boot
└─sda2            8:2    0 442.1G  0 part 
  ├─rootvg-root 253:0    0   100G  0 lvm  /
  ├─rootvg-swap 253:1    0    32G  0 lvm  [SWAP]
  ├─rootvg-bomc 253:2    0 130.1G  0 lvm  /bomc
  ├─rootvg-home 253:3    0    80G  0 lvm  /home
  └─rootvg-var  253:4    0   100G  0 lvm  /var
sdb               8:16   0  80.1T  0 disk 
sr0              11:0    1  1024M  0 rom  

1.1parted常用命令及说明

check NUMBER	                        做一次简单的文件系统检测
cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER	复制文件系统到另一个分区
help [COMMAND]	                        显示所有的命令帮助
mklabel,mktable LABEL-TYPE	            创建新的磁盘卷标(分区表)
mkfs NUMBER FS-TYPE	                    在分区上建立文件系统
mkpart PART-TYPE [FS-TYPE] START END	创建一个分区
mkpartfs PART-TYPE FS-TYPE START END	创建分区,并建立文件系统
move NUMBER START END	                移动分区
name NUMBER NAME	                    给分区命名
print [devices|free|list,all|NUMBER]	显示分区表、活动设备、空闲空间、所有分区
quit	                                退出
rescue START END	                    修复丢失的分区
resize NUMBER START END	                修改分区大小
rm NUMBER	                            删除分区
select DEVICE	                        选择需要编辑的设备
set NUMBER FLAG STATE	                改变分区标记
toggle [NUMBER [FLAG]]	                切换分区表的状态
unit UNIT	                            设置默认的单位
Version	                                显示版本

1.2 parted常用的选项

-h	–help 显示此求助信息
-l	–list 列出所有设别的分区信息
-i	–interactive 在必要时,提示用户
-s	–script 从不提示用户
-v  -version 显示版本

2.划分磁盘

2.1 选择分区

# parted /dev/sdb
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

2.2 创建分区

# mklabel                                                          
New disk label type? gpt                                                  
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?
Yes/No? yes                                                               
(parted)  

2.3 完成分区操作

# mkpart                                                           
Partition name?  []? sdb1                                                 
File system type?  [ext2]? xfs                                            
Start? 0                                                                  
End? 20%                                                                  
Warning: The resulting partition is not properly aligned for best performance: 34s % 5632s != 0s
Ignore/Cancel? Ignore                                                     
(parted)                                                                  
(parted) print                                                            
Model: ZTE LOGICAL VOLUME (scsi)
Disk /dev/sdb: 88.0TB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      17.4kB  17.6TB  17.6TB  xfs          sdb1

2.4 分区错误,删除分区

# mkpart                                                           
Partition name?  []? sdb1                                                 
File system type?  [ext2]? xfs                                            
Start? 20%                                                                
End?                                                                      
End? 30%                                                                  
(parted) rm sdb1                                                          
Error: Expecting a partition number.
(parted) rm 1                                                             
(parted)                                                                  
(parted) print                                                            
Model: ZTE LOGICAL VOLUME (scsi)
Disk /dev/sdb: 88.0TB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

2.5 验证分区

print出信息,看是否符合预期

# print                                                            
Model: ZTE LOGICAL VOLUME (scsi)
Disk /dev/sdb: 88.0TB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      17.4kB  17.6TB  17.6TB  xfs          sdb1

2.6 再依次划分其他盘

# mkpart                                                           
Partition name?  []? sdb2                                                 
File system type?  [ext2]? xfs                                            
Start? 20%                                                                
End? 40%                                                                  
(parted)                                                                  
(parted) priint 
Model: ZTE LOGICAL VOLUME (scsi)
Disk /dev/sdb: 88.0TB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      17.4kB  17.6TB  17.6TB  xfs          sdb1
 2      17.6TB  35.2TB  17.6TB  xfs          sdb2

# mkpart                                                           
Partition name?  []? sdb3                                                 
File system type?  [ext2]? xfs                                            
Start? 40%                                                                
End? 60%                                                                  
(parted)                                                                  
(parted) mkpart
Partition name?  []? sdb4
File system type?  [ext2]? xfs                                            
Start? 60%                                                                
End? 80%                                                                  
(parted)                                                                  
(parted) mkpart                                                           
Partition name?  []? sdb5                                                 
File system type?  [ext2]? xfs                                            
Start? 80%                                                                
End? 100%                                                                 
(parted)
(parted) print                                                            
Model: ZTE LOGICAL VOLUME (scsi)
Disk /dev/sdb: 88.0TB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      17.4kB  17.6TB  17.6TB  xfs          sdb1
 2      17.6TB  35.2TB  17.6TB  xfs          sdb2
 3      35.2TB  52.8TB  17.6TB  xfs          sdb3
 4      52.8TB  70.4TB  17.6TB  xfs          sdb4
 5      70.4TB  88.0TB  17.6TB  xfs          sdb5
(parted) quit                                                             
Information: You may need to update /etc/fstab.

2.7 格式化分区

quit退出parted交互,使用mkfs给新建的分区格式化

2.7.1  lsblk查看磁盘结构

# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0 447.1G  0 disk 
├─sda1            8:1    0     5G  0 part /boot
└─sda2            8:2    0 442.1G  0 part 
  ├─rootvg-root 253:0    0   100G  0 lvm  /
  ├─rootvg-swap 253:1    0    32G  0 lvm  [SWAP]
  ├─rootvg-bomc 253:2    0 130.1G  0 lvm  /bomc
  ├─rootvg-home 253:3    0    80G  0 lvm  /home
  └─rootvg-var  253:4    0   100G  0 lvm  /var
sdb               8:16   0  80.1T  0 disk 
├─sdb1            8:17   0    16T  0 part 
├─sdb2            8:18   0    16T  0 part 
├─sdb3            8:19   0    16T  0 part 
├─sdb4            8:20   0    16T  0 part 
└─sdb5            8:21   0    16T  0 part 
sr0              11:0    1  1024M  0 rom  
[root@hkcrmlog08 ~]# 

2.7.2 fdisk -l 查看磁盘

# fdisk -l
Disk /dev/sda: 447.1 GiB, 480068075520 bytes, 937632960 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 262144 bytes / 262144 bytes
Disklabel type: dos
Disk identifier: 0x604ee21e

Device     Boot    Start       End   Sectors   Size Id Type
/dev/sda1  *        2048  10487807  10485760     5G 83 Linux
/dev/sda2       10487808 937631743 927143936 442.1G 8e Linux LVM


Disk /dev/sdb: 80.1 TiB, 88016821370880 bytes, 171907854240 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 262144 bytes / 2883584 bytes
Disklabel type: gpt
Disk identifier: 72F44DFD-4B36-4ADD-8A51-40308B207C55

Device            Start          End     Sectors Size Type
/dev/sdb1            34  34381570847 34381570814  16T Linux filesystem
/dev/sdb2   34381574656  68763143679 34381569024  16T Linux filesystem
/dev/sdb3   68763143680 103144712703 34381569024  16T Linux filesystem
/dev/sdb4  103144712704 137526281727 34381569024  16T Linux filesystem
/dev/sdb5  137526281728 171907850751 34381569024  16T Linux filesystem

Partition 1 does not start on physical sector boundary.

2.7.3 对盘进行格式化

# mkfs.xfs /dev/sdb1
warning: device is not properly aligned /dev/sdb1
Use -f to force usage of a misaligned device
[root@hkcrmlog08 ~]# mkfs.xfs /dev/sdb2
meta-data=/dev/sdb2              isize=512    agcount=32, agsize=134303040 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=0 inobtcount=0
data     =                       bsize=4096   blocks=4297696128, imaxpct=5
         =                       sunit=64     swidth=704 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=521728, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@hkcrmlog08 ~]# mkfs.xfs /dev/sdb3
meta-data=/dev/sdb3              isize=512    agcount=32, agsize=134303040 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=0 inobtcount=0
data     =                       bsize=4096   blocks=4297696128, imaxpct=5
         =                       sunit=64     swidth=704 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=521728, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@hkcrmlog08 ~]# mkfs.xfs /dev/sdb4
meta-data=/dev/sdb4              isize=512    agcount=32, agsize=134303040 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=0 inobtcount=0
data     =                       bsize=4096   blocks=4297696128, imaxpct=5
         =                       sunit=64     swidth=704 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=521728, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@hkcrmlog08 ~]# 
[root@hkcrmlog08 ~]# mkfs.xfs /dev/sdb5
meta-data=/dev/sdb5              isize=512    agcount=32, agsize=134303040 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=0 inobtcount=0
data     =                       bsize=4096   blocks=4297696128, imaxpct=5
         =                       sunit=64     swidth=704 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=521728, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@hkcrmlog08 ~]# mkfs.xfs -f /dev/sdb1
warning: device is not properly aligned /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=32, agsize=134303040 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=0 inobtcount=0
data     =                       bsize=4096   blocks=4297696351, imaxpct=5
         =                       sunit=64     swidth=704 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=521728, version=2
         =                       sectsz=512   sunit=64 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

2.8挂载磁盘

2.8.1 新建目录

mkdir /data01 /data02 /data03 /data04

2.8.2 修改/etc/fstab文件

/dev/sdb1   /data   xfs  defaults 0 0
/dev/sdb2   /data01   xfs  defaults 0 0
/dev/sdb3   /data02   xfs  defaults 0 0
/dev/sdb4   /data03   xfs  defaults 0 0
/dev/sdb5   /data03   xfs  defaults 0 0

2.8.3 挂载

# mount /dev/sdb1 /data
[root@hkcrmlog08 ~]# mount /dev/sdb2 /data01
[root@hkcrmlog08 ~]# mount /dev/sdb3 /data02
[root@hkcrmlog08 ~]# mount /dev/sdb4 /data03
[root@hkcrmlog08 ~]# mount /dev/sdb5 /data04
[root@hkcrmlog08 ~]# 
[root@hkcrmlog08 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                  94G     0   94G   0% /dev
tmpfs                     94G     0   94G   0% /dev/shm
tmpfs                     94G   42M   94G   1% /run
tmpfs                     94G     0   94G   0% /sys/fs/cgroup
/dev/mapper/rootvg-root   98G  1.6G   92G   2% /
/dev/sda1                4.9G  245M  4.4G   6% /boot
/dev/mapper/rootvg-bomc  127G   28K  121G   1% /bomc
/dev/mapper/rootvg-home   79G   44K   75G   1% /home
/dev/mapper/rootvg-var    98G  610M   93G   1% /var
tmpfs                     19G     0   19G   0% /run/user/0
/dev/sdb1                 17T  115G   16T   1% /data
/dev/sdb2                 17T  115G   16T   1% /data01
/dev/sdb3                 17T  115G   16T   1% /data02
/dev/sdb4                 17T  115G   16T   1% /data03
/dev/sdb5                 17T  115G   16T   1% /data04

原因为哈,刚格式化后,磁盘就被占用100多G。应该是有地方能将此步骤节省出来,我没有找到。

你可能感兴趣的:(Linux,计算机,linux,运维,服务器)