原来还能这么用 ?!|

isOllie

园龄:2年10个月粉丝:0关注:0

Linux 硬盘扩容 分区 & 挂载

1. 添加分区

1.1. 查看新添加的硬盘

fdisk -l

假设当前未挂载的盘符是/dev/sdb,后文中所有操作都按挂载/dev/sdb 操作

1.2. 分区管理

  • 小硬盘
fdisk /dev/sdb
  • 大硬盘(2TB以上)
gdisk /dev/sdb

1.3. 编辑分区

⚠️ 下方注释一行一行看,不要跳过

# 执行完 fdisk /dev/sdb 后命令行会出现以下交互式命令行
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
GPT PMBR size mismatch (2048 != 1935671295) will be corrected by write.
The backup GPT table is not on the end of the device.
This problem will be corrected by write.
# 1.3.1. 输入 'n', 创建一个新分区
Command (m for help): n
Partition number (4-128, default 4):
First sector (2048-1935671295, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1935671295, default 1935671295):
Created a new partition 4 of type 'Linux filesystem' and of size 923 GiB.
# 1.3.2. 输入 't', 改变当前分区的文件格式
Command (m for help): t
Partition number (1-4, default 1):
# 这里需要注意一下,有的系统是8e,有的系统是31,
# 可以L查看列表查询LVM格式的编号,需要分区格式为'Linux LVM'
Partition type (type L to list all types): 8e
Changed type of partition 'Linux filesystem' to 'Linux LVM'.
# 1.3.3. 输入 'p', 打印出当前硬盘的分区表信息, 查看硬盘分区是否创建成功
# 部分硬盘过大可能无法正常创建(部分Linux内核无法创建过大的硬盘分区,如2TB以上需要通过gdisk进行分区)
# 如果下方 /dev/sdb1 那一行的 Size列 显示的硬盘大小不是你和创建的硬盘大小,直接退出,使用gdisk重新执行
Command (m for help): p
Disk /dev/sdb: 923 GiB, 991063703552 bytes, 1935671296 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x7568f956
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 1935671295 1935669248 923G 8e Linux LVM
# 1.3.4. 输入 'w', 写入分区并退出交互式命令行
Command (m for help): w
The partition table has been altered.
Syncing disks.

2. 重读硬盘分区表

  • 通过partprobe⼯具让内核重读硬盘分区表,否则lsblk命令查看分区时,⽆法看到刚刚新建的分区
partprobe /dev/sdb

3. 格式化分区

格式化文件格式为mkfs.ext4

mkfs.ext4 /dev/sdb1 -N 1638400

4. 挂载硬盘 & 设置开机自动挂载

⚠️ 如果 /data 目录不存在,需要先创建

⚠️ 不推荐使用已有目录进行挂载,如:/home目录,但是可以挂载到其子目录下,如:/home/data

✅ 目录挂载后,为使用方便,可以使用软连接方式: ' ln -s ' 将目录软连接到你的工作目录下

4.1. 临时挂载硬盘

mount /dev/sdb1 /data

4.2. 永久挂载硬盘

如果没有临时挂载,需要重启服务器,硬盘才会被挂载上

echo "/dev/sdb1 /data ext4 defaults 0 0" >> /etc/fstab

本文作者:ollie的学习笔记

本文链接:https://www.cnblogs.com/isollie/p/18670476

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   isOllie  阅读(61)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起