通过fdisk初始化Linux数据盘

1.查看新增数据盘

fdisk -l

[root@ecs-test-0001 ~]# 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: 0x000bcb4e

Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    83886079    41942016   83  Linux

Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 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

表示当前系统有两块磁盘,"/dev/vda"是系统盘,"/dev/vdb"是新增数据盘。

2.新增数据盘分区

fdisk 新增数据盘 如fdisk /dev/vdb

[root@ecs-test-0001 ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x38717fc1.

Command (m for help):

3.开始新建分区

输入"n",按"Enter",开始新建分区

Command (m for help): n
Partition type:
p   primary (0 primary, 0 extended, 4 free)
e   extended

"p"表示主分区,"e"表示扩展分区
● 磁盘使用MBR分区形式,最多可以创建4个主分区,或者3个主分区加1个扩展分区,扩展分区不可以直接使用,需要划分成若干个逻辑分区才可以使用。

● 磁盘使用GPT分区形式时,没有主分区、扩展分区以及逻辑分区之分。

4.创建主分区

输入"p",按"Enter",创建主分区

Select (default p): p
Partition number (1-4, default 1):

5.输入分区编号

以分区编号选择"1"为例,输入主分区编号"1",按"Enter"。

Partition number (1-4, default 1): 1
First sector (2048-209715199, default 2048):

6.选择起始磁柱值

以分区编号选择"1"为例,输入主分区编号"1",按"Enter"。

First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):

7.选择截止磁柱值

以选择默认截止磁柱值209715199为例,按"Enter"。

Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set

Command (m for help):

8.查看新建分区信息

输入"p",按"Enter",查看新建分区的详细信息。

Command (m for help): p

Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x38717fc1

Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048   209715199   104856576   83  Linux

Command (m for help):

9.将分区结果写入分区表

输入"w",按"Enter",将分区结果写入分区表中。

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

10.新的分区表变更同步至操作系统

输入 partprobe将新的分区表变更同步至操作系统。

11.格式化新建分区文件系统

mkfs -t 文件系统格式 /dev/vdb1

以设置文件系统为"ext4"为例:

mkfs -t ext4 /dev/vdb1

[root@ecs-test-0001 ~]# mkfs -t ext4 /dev/vdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26214144 blocks
1310707 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2174746624
800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

12.新建挂载目录

mkdir 挂载目录

以新建挂载目录"/data"为例:

mkdir /data

13.将新建分区挂载到新创建的目录下

mount 磁盘分区 挂载目录

以挂载新建分区"/dev/vdb1"至"/data"为例:

mount /dev/vdb1 /data

14.查看挂载结果

df -TH

[root@ecs-test-0001 ~]# df -TH
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      ext4       43G  1.9G   39G   5% /
devtmpfs       devtmpfs  2.0G     0  2.0G   0% /dev
tmpfs          tmpfs     2.0G     0  2.0G   0% /dev/shm
tmpfs          tmpfs     2.0G  9.1M  2.0G   1% /run
tmpfs          tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
tmpfs          tmpfs     398M     0  398M   0% /run/user/0
/dev/vdb1      ext4      106G   63M  101G   1% /data

15.查询磁盘分区UUID

blkid 磁盘分区

以查询磁盘分区"/dev/vdb1"的UUID为例:

blkid /dev/vdb1

[root@ecs-test-0001 ~]# blkid /dev/vdb1
/dev/vdb1: UUID="0b3040e2-1367-4abb-841d-ddb0b92693df" TYPE="ext4"

16.编辑fstab文件

vi /etc/fstab

按"i",进入编辑模式。

将光标移至文件末尾,按"Enter",添加如下内容。

UUID=0b3040e2-1367-4abb-841d-ddb0b92693df /mnt/sdc ext4 defaults 0 2

按"ESC"后,输入":wq",按"Enter"。

17.验证自动挂载功能

umount 磁盘分区 卸载已挂载的分区 umount /dev/vdb1

mount -a 重新加载/etc/fstab文件

mount | grep 挂载目录 查询文件系统挂载信息 mount | grep /data

回显类似如下信息,说明自动挂载功能生效

root@ecs-test-0001 ~]# mount | grep /data
/dev/vdb1 on /data type ext4 (rw,relatime,data=ordered)
相关推荐
chian-ocean16 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=18 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
橘子味的茶二27 分钟前
高级系统架构师--第十章:计算机网络
服务器·计算机网络·系统架构
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
紫菜(Nori)1 小时前
短连接服务器压测-wrk
服务器·压力测试
追寻光1 小时前
Cannot find a valid baseurl for repo: base/7/x86_64
linux
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
陆沙1 小时前
deepseek-r1-centos-本地服务器配置方法
linux·服务器·centos
北京耐用通信1 小时前
航空装配自动化神器Ethercat转profient网关搭配机器人精准控制
运维·机器人·自动化
吃汤圆的抹香鲸1 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net