2018年第43周-Linux下使用gpt给硬盘分区

由于MBR分区表最大支持2T硬盘,而且主分区数量还有限制.
而GPT分区表就没有上述问题,线上服务器基本都是采用这个来格式化系统, 所以使用gpt进行分区.

方法如下:
1.进入parted工具,输入help可查看帮助

[root@amd ~]# sudo parted
GNU Parted 3.1
# 默认使用第一个硬盘
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  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
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted

2.选择你要操作的设备,一定要注意,不要把默认设备误操作了

(parted) select /dev/sdb                                                  
Using /dev/sdb

3.设定使用的分区类型, 如果要用MBR分区,输入msdos即可。

(parted) mklabel 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   

4.添加一个分区

(parted) mkpart 

5.输入分区名称,可以不用输入,直接回车

Partition name?  []? 

6.输入使用哪种文件系统,默认ext2,回车 (此处可以随意选,之后可以使用mkfs重新格式化写入文件系统)

File system type?  [ext2]? xfs

7.输入分区从第几MB的位置开始,输入1,从第1MB开始(最好不要从0开始), 输入为什么位置结束,可以带单位,如我这里输入的是2000GB,也可以直接输3TB

Start? 1                                                                  
End? 2000GB  

8.此时已经分区成功,输入print查看当前分区/dev/sdb情况

(parted) print                                                            
Model: ATA ST2000DM006-2DM1 (scsi)
Disk /dev/sdb: 2000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  2000GB  2000GB  ext4

(parted)            

额, 我这个好像不给力啊, 怎么File System还是ext4,不过没关系, 后面可以强制格式化它来修改文件系统

9.退出parted工具

(parted) quit                                                             
Information: You may need to update /etc/fstab.

10.格式化系统

[root@amd ~]# mkfs.xfs /dev/sdb1
mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (ext4).
mkfs.xfs: Use the -f option to force overwrite.
[root@amd ~]# mkfs.xfs -f /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=122094592 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=488378368, imaxpct=5
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=238466, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

11.创建目录/mydata

[root@amd ~]# mkdir /mydata

12.修改/etc/fstab

[root@amd ~]# vim /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sat Oct 20 01:23:55 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/cl-root     /                       xfs     defaults        0 0
UUID=7f16402f-ec92-4a9d-bbe0-56ee0b509d83 /boot                   xfs     defaults        0 0
UUID=32AA-91B7          /boot/efi               vfat    umask=0077,shortname=winnt 0 0
/dev/mapper/cl-home     /home                   xfs     defaults        0 0
/dev/mapper/cl-swap     swap                    swap    defaults        0 0
/dev/sdb1        /mydata            xfs    defaults     0 0

13.挂载

[root@amd ~]# mount -a
[root@amd ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/cl-root   50G  1.6G   49G   4% /
devtmpfs             3.7G     0  3.7G   0% /dev
tmpfs                3.7G     0  3.7G   0% /dev/shm
tmpfs                3.7G  8.7M  3.7G   1% /run
tmpfs                3.7G     0  3.7G   0% /sys/fs/cgroup
/dev/mapper/cl-home  873G   33M  873G   1% /home
/dev/sda2           1014M  151M  864M  15% /boot
/dev/sda1            200M   12M  189M   6% /boot/efi
tmpfs                744M     0  744M   0% /run/user/0
/dev/sdb1            1.9T   33M  1.9T   1% /mydata

14.获取UUID方法

[root@amd ~]# blkid /dev/sdb1
/dev/sdb1: UUID="a8b15d78-e144-40e4-8f0e-c68b9749affd" TYPE="xfs" PARTUUID="142a3cd8-f2a2-4c83-987d-be3811eca4fc" 
    原文作者:黄小数
    原文地址: https://segmentfault.com/a/1190000016830674
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞