Linux入门实践-数据盘格式化和和多分区挂载

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:

内容

  本文主要介绍了CentOS系统的阿里云ECS服务器中数据盘的格式化和挂载多分区ext4格式的文件系统。

版本 操作系统: CentOS 7.2 64位

适合人群:linux运维人员
步骤

查看文件系统的使用情况

  通过"df -h "命令查看当前文件系统的使用情况,可以观察到文件系统的名称、总容量、已用、可用、使用比例、挂载点等信息。
[user1@iz8vb62snc6e5cage5yvz9z /]$ df -h

文件系统的名称 总容量 已用 可用 使用比例 挂载点

Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 1.9G 17G 11% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 320K 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
tmpfs 783M 0 783M 0% /run/user/0
  由于笔者所购服务器还含有50G的数据盘,但是通过"df -h"命令没有查找到。经过查阅阿里云的文档,发现数据盘需要格式化并挂载后才能正常使用。

  下面就演示如何将数据盘分成两个25G的区,格式化分区后分别挂载到/home/user1/redis/data和/home/user1/program。这里需要先创建这两个目录,此处就不再演示。

查看磁盘的信息

  通过" fdisk -l"命令可以查看当前磁盘的信息,本服务器有个20GB的系统盘/dev/vda,和50GB的数据盘/dev/vdb。可以看到磁盘的扇区数,每个扇区的大小(512字节)。
[user1@iz8vb62snc6e5cage5yvz9z /]$ sudo fdisk -l

20G系统盘/dev/vda

Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000d2717

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 41943039 20970496 83 Linux

50GB的数据盘/dev/vdb

Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes #每个扇区的大小(512字节)
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
执行分区操作

  运行 fdisk /dev/vdb对数据盘进行分区,此处创建两个分区,大小分别为25GB。

  步骤1:输入 n 并按回车键:创建一个新分区;如果有旧的分区,可以输入d进行删除。

  步骤2:输入 p 并按回车键:选择主分区类型。创建4个以内的分区,都可以使用主分区类型。

  步骤3:输入分区编号并按回车键,第一个分区的分区编号设置为1,第二个分区的分区编号设置为2。

  步骤4:分别输入第一个扇区的编号和最后一个扇区的编号,此处第一个分区的扇区从2048到52400000,第二个分区的扇区从52400128到104857599。

  如果创建多个分区,重复执行上述4个步骤。

  步骤5:输入 wq 并按回车键,开始分区。
[user1@iz8vb62snc6e5cage5yvz9z ~]$ sudo 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.

如果有旧的分区,可以输入d进行删除。

Command (m for help): d
Selected partition 3
Partition 3 is deleted

首先输入 n 并按回车键:创建一个新分区。

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free) #主分区类型
e extended #扩展分区类型

输入 p 并按回车键:选择主分区类型

Select (default p): p

输入分区编号并按回车键

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

输入第一个扇区的编号

First sector (2048-104857599, default 2048):
Using default value 2048

输入最后一个扇区的编号

Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): 52400000
Partition 1 of type Linux and of size 25 GiB is set

重复执行上述4个步骤,创建第二个分区

Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (52400001-104857599, default 52400128):
Using default value 52400128
Last sector, +sectors or +size{K,M,G} (52400128-104857599, default 104857599):
Using default value 104857599
Partition 2 of type Linux and of size 25 GiB is set

最后输入 wq 并按回车键,开始分区。

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

Calling ioctl() to re-read partition table.
Syncing disks.
确认分区信息

  再次执行" fdisk -l"命令可以查看当前磁盘的信息,可以看到上述操作创建的分区/dev/vdb1与/dev/vdb2。
[user1@iz8vb62snc6e5cage5yvz9z ~]$ sudo fdisk -l

Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000d2717

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 41943039 20970496 83 Linux

Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 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: 0xc95b5e42

上述操作创建的分区/dev/vdb1与/dev/vdb2

Device Boot Start End Blocks Id System
/dev/vdb1 2048 52400000 26198976+ 83 Linux
/dev/vdb2 52400128 104857599 26228736 83 Linux
创建文件系统

  执行mkfs.ext4 命令,分别在新分区/dev/vdb1与/dev/vdb2上创建ext4类型的文件系统。
[user1@iz8vb62snc6e5cage5yvz9z ~]$ sudo mkfs.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
inodes, 6549744 blocks
blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2155872256
block groups
blocks per group, 32768 fragments per group
inodes per group
Superblock backups stored on blocks:

    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
 这里创建的文件系统类型为ext4,阿里云官方文档创建的是ext3,下面从参考的文章中摘录一些内容说明ext3和ext4的关系:

  Linux kernel 自 2.6.28 开始正式支持新的文件系统 Ext4。 Ext4 是 Ext3 的改进版,修改了 Ext3 中部分重要的数据结构,而不仅仅像 Ext3 对 Ext2 那样,只是增加了一个日志功能而已。Ext4 可以提供更佳的性能和可靠性,还有更为丰富的功能:与 Ext3 兼容;更大的文件系统和更大的文件;多块分配;延迟分配;日志校验;在线碎片整理等。

备份并写入分区信息

  首先备份分区信息文件/etc/fstab,然后在该文件下追加/dev/vdb1与/dev/vdb2的分区信息,包括挂载点和文件系统的格式。

备份分区信息文件

[user1@iz8vb62snc6e5cage5yvz9z /]$ sudo cp /etc/fstab /etc/fstab.bak

user1用户使用sudo执行命令扔提示权限不足,切换到root账户下执行命令

[user1@iz8vb62snc6e5cage5yvz9z data]$ sudo echo /dev/vdb1 /home/user1/redis/data ext4 defaults 0 0 >> /etc/fstab
bash: /etc/fstab: Permission denied

追加/dev/vdb1的分区信息,该分区挂载的路径为/home/user1/redis/data,格式为ext4

[root@iz8vb62snc6e5cage5yvz9z data]# echo /dev/vdb1 /home/user1/redis/data ext4 defaults 0 0 >> /etc/fstab

追加/dev/vdb2的分区信息,该分区挂载的路径为/home/user1/program,格式为ext4

[root@iz8vb62snc6e5cage5yvz9z home]# echo /dev/vdb2 /home/user1/program ext4 defaults 0 0 >> /etc/fstab

查看分区信息

[root@iz8vb62snc6e5cage5yvz9z home]# cat /etc/fstab

/etc/fstab

Created by anaconda on Thu Aug 17 07:38:21 2017

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

UUID=976105f5-f402-456c-aadd-50de49ff88f9 / ext4 defaults 1 1
/dev/vdb1 /home/user1/redis/data ext4 defaults 0 0
/dev/vdb2 /home/user1/program/ ext4 defaults 0 0
挂载分区

  使用mount将分区挂载到对应的路径下,并使用df -h观察挂载后的文件系统信息。

  到此为止,完成了数据盘的分区、格式化、挂载等工作;不需要重启实例即可开始使用新的文件系统。

使用mount将分区挂载到对应的路径下

[root@iz8vb62snc6e5cage5yvz9z data]# mount /dev/vdb1 /home/user1/redis/data
[root@iz8vb62snc6e5cage5yvz9z data]# mount /dev/vdb2 /home/user1/program

使用df -h观察挂载后的文件系统

[root@iz8vb62snc6e5cage5yvz9z home]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 1.9G 17G 11% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 328K 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
tmpfs 783M 0 783M 0% /run/user/0
/dev/vdb1 25G 45M 24G 1% /home/user1/redis/data
/dev/vdb2 25G 45M 24G 1% /home/user1/program

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
17天前
|
缓存 Linux 测试技术
安装【银河麒麟V10】linux系统--并挂载镜像
安装【银河麒麟V10】linux系统--并挂载镜像
90 0
|
20天前
|
存储 安全 数据管理
探索Linux的挂载操作🌈
在Linux这个强大的操作系统中,挂载操作是一个基本而重要的概念。它涉及到文件系统、设备和数据访问,对于理解Linux的工作方式至关重要。那么,挂载操作究竟是什么,为什么我们需要它,如果没有它,我们将面临什么问题呢?让我们一起深入探讨。
探索Linux的挂载操作🌈
|
29天前
|
Linux 数据安全/隐私保护 Windows
Linux入门指南:linux权限究竟是什么?和文件有什么关系?
Linux入门指南:linux权限究竟是什么?和文件有什么关系?
30 0
|
1月前
|
Linux Shell C语言
【Shell 命令集合 磁盘维护 】Linux 分区管理的工具 sfdisk命令使用教程
【Shell 命令集合 磁盘维护 】Linux 分区管理的工具 sfdisk命令使用教程
30 1
|
1月前
|
存储 Shell Linux
【Shell 命令集合 磁盘维护 】Linux 管理硬盘分区 mpartition命令使用教程
【Shell 命令集合 磁盘维护 】Linux 管理硬盘分区 mpartition命令使用教程
36 1
|
30天前
|
存储 算法 Linux
【Linux 应用开发 共享内存】深入理解和实践 ftruncate:共享内存的有效管理
【Linux 应用开发 共享内存】深入理解和实践 ftruncate:共享内存的有效管理
62 5
|
1月前
|
监控 Linux Shell
【Shell 命令集合 磁盘维护 】Linux 交换分区的特殊文件或设备 swapon命令使用指南
【Shell 命令集合 磁盘维护 】Linux 交换分区的特殊文件或设备 swapon命令使用指南
38 1
|
1月前
|
存储 安全 Linux
【Shell 命令集合 磁盘维护 】Linux 在特定的分区上建立 linux 文件系统 mkfs命令使用教程
【Shell 命令集合 磁盘维护 】Linux 在特定的分区上建立 linux 文件系统 mkfs命令使用教程
27 0
|
17天前
|
Linux
Linux操作系统调优相关工具(三)查看IO运行状态相关工具 查看哪个磁盘或分区最繁忙?
Linux操作系统调优相关工具(三)查看IO运行状态相关工具 查看哪个磁盘或分区最繁忙?
21 0
|
1天前
|
Linux
如何将一个linux服务器挂载到另外一个linux服务器上
如何将一个linux服务器挂载到另外一个linux服务器上
13 1