使用GPT分区表分区并格式化大硬盘

来自linux中国网wiki
Evan讨论 | 贡献2022年9月5日 (一) 08:32的版本 →‎创建GPT分区
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

使用GPT分区表分区并格式化大硬盘


格式化后,数据盘中的数据将被全部清空。请在格式化之前,确保数据盘中没有数据或对重要数据已进行备份。为避免服务发生异常,格式化前请确保云服务器已停止对外服务。

查看磁盘列表


使用以下命令,查看磁盘设备列表:
 fdisk  -l

 

创建GPT分区

 使用parted工具,创建GPT分区
举例 这里为 vdc
root@VM_13_1_centos ~]#  parted /dev/vdc
GNU Parted 2.1
使用 /dev/vdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt     #我的输入   
Yes/No? yes                                               
(parted) print                                                            
Model: Virtio Block Device (virtblk)
Disk /dev/vdc: 1074GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start  End  Size  File system  Name  标志

(parted) mkpart primary 0 1073.7GB (这个由前面的fdisk -l 得出的容量大小)
警告: The resulting partition is not properly aligned for best performance.
忽略/Ignore/放弃/Cancel? Ignore                                           
(parted) print                                                            
Model: Virtio Block Device (virtblk)
Disk /dev/vdc: 1074GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name     标志
 1      17.4kB  1074GB  1074GB               primary

(parted) quit

 

3. 查看新分区信息


分区创建完成后,可以使用以下命令查看到新分区信息:
 fdisk -l

4. 格式化分区


使用mkfs工具格式化分区
 mkfs.ext4 -T largefile /dev/vdx1

5. 挂载新分区


格式化完成后,使用以下命令挂载新分区
mount 文件系统 分区路径 挂载点
此时使用以下命令可以查看到磁盘剩余容量
 df  -h

mkdir /data1
 mount -t ext4 /dev/vdx1 /data1

6. 设置自动挂载

vim /etc/fstab
 /dev/vdx1 /data1   ext4  defaults 0 0


如果是新硬盘

#先分区

[root@localhost ~]# fdisk  /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0x945484a9 创建新的 DOS 磁盘标签。

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-1953525167,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-1953525167,默认为 1953525167):
将使用默认值 1953525167
分区 1 已设置为 Linux 类型,大小设为 931.5 GiB

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@localhost ~]# 

再查看  有/dev/sdb1 了
fdisk  -l 

磁盘 /dev/sdb:1000.2 GB, 1000204886016 字节,1953525168 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 4096 字节
I/O 大小(最小/最佳):4096 字节 / 4096 字节
磁盘标签类型:dos
磁盘标识符:0x945484a9

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048  1953525167   976761560   83  Linux
[root@localhost ~]# 

格式化 和其它的一样了
 mkfs.xfs  /dev/sdb1

trouble shooting

fdiskl -l Partition 1 does not start on physical sector boundary. 用命令行gpt 分区后 老提示空间不够 后来 用gparted 重新分区好了

see also

https://www.qcloud.com/document/product/213/2043

 

GPT和parted命令详解(原创)
https://yq.aliyun.com/articles/52294

 

aliyun Linux 格式化和挂载数据盘


CentOS 7硬盘格式化和分区