centos系统下如何挂载磁盘?

有遇到用户购买我司云服务器后,在安装宝塔面板之前,不会挂载磁盘或没有操作挂载磁盘,从而将数据全部默认安装到了系统盘,进而导致数据盘容量浪费,在此,我司技术部呼吁千万不要将宝塔面板和数据库全部安装到系统盘,一旦重装系统则数据全部会被删除,强烈建议务必要挂载数据磁盘,将所有数据放到数据盘中。

以挂载我司云服务器数据盘/dev/xvdb为例,现针对centos系统挂载数据磁盘方法整理如下:

1、创建挂载目录(以创建www磁盘目录为例)

命令:mkdir -p /www

2、确认是否没有分区的磁盘

命令:fdisk -l

如下:可以看到我司云服务器centos系统数据盘/dev/xvdb,大小41.9GB,数据盘下分区为空白,说明该磁盘未挂载分区;系统盘/dev/xvda,大小10.7GB,系统盘下有两个分区/dev/xvda1 和/dev/xvda2。

[root@XS22218659208 ~]# fdisk -l

Disk /dev/xvdb: 41.9 GB, 41875931136 bytes, 81788928 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 /dev/xvda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x000d0b25

Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 2099199 1048576 83 Linux
/dev/xvda2 2099200 20971519 9436160 8e Linux LVM

Disk /dev/mapper/centos-root: 8585 MB, 8585740288 bytes, 16769024 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 /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 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

3、为磁盘分区

命令:fdisk /dev/xvdb

如下:

[root@XS22218659208 ~]# fdisk /dev/xvdb
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 0x5d7fdc5a.

CommAnd (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-81788927, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-81788927, default 81788927):
Using default value 81788927
Partition 1 of type Linux And of size 39 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@XS22218659208 ~]#

4、检查是否分区成功。(分区中带有/dev/xvdb1则代表成功)

命令:fdisk -l

5、格式化分区

命令:mkfs.ext4 /dev/xvdb1

6、将分区挂载信息添加到配置文件/etc/fstab中,实现开机/重启自动挂载

命令:echo "/dev/xvdb1 /www ext4 defaults 0 0" >> /etc/fstab

7、重新挂载所有分区

命令:mount -a

8、检查是否挂载成功

命令:df -h

如下:显示/dev/xvdb1 挂载成功,大小为39G,挂载目录为/www

[root@XS22218659208 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0G 0 4.0G 0% /dev
tmpfs 4.0G 0 4.0G 0% /dev/shm
tmpfs 4.0G 8.4M 4.0G 1% /run
tmpfs 4.0G 0 4.0G 0% /sys/fs/cgroup
/dev/mapper/centos-root 7.8G 1.6G 5.8G 22% /
/dev/xvda1 976M 131M 778M 15% /boot
tmpfs 802M 0 802M 0% /run/user/0
/dev/xvdb1 39G 49M 37G 1% /www

至此,成功挂载磁盘完成。