Centos7挂载硬盘

以root身份登录系统进行以下操作。

一. 查看磁盘信息

注意:在没有分区和格式化数据盘之前,使用 df-h 命令是无法看到数据盘的。

fdisk -l
[root@ecs-CwM ~]# 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: 0x0000425c

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    41943039    19921920   8e  Linux LVM

Disk /dev/vdb: 139.6 GB, 139586437120 bytes, 272629760 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-root: 18.2 GB, 18249416704 bytes, 35643392 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: 2147 MB, 2147483648 bytes, 4194304 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

二. 新建分区

fdisk /dev/vdb  //这里的vdb是要挂载的新硬盘
n //添加新分区
p //创建主分区
1 分区号1
按回车 //起始扇区选择默认
也是回车默认 //为了不浪费空间
p //查看创建出来的分区
w //保存
[root@ecs-CwM ~]# 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 0x7adc4797.

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-272629759, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-272629759, default 272629759): 
Using default value 272629759
Partition 1 of type Linux and of size 130 GiB is set

查看创建出来的分区

Command (m for help): p

Disk /dev/vdb: 139.6 GB, 139586437120 bytes, 272629760 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: 0x7adc4797

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048   272629759   136313856   83  Linux

保存并退出fdisk工具命令行

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

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

三. 格式化分区

可格式为ext4或xfs

注:若选择xfs,需要匹配docker默认overlay2存储驱动,建议使用命令:    mkfs.xfs -n ftype=1 /dev/vdb1

[root@ecs-CwM ~]# mkfs.xfs -n ftype=1 /dev/vdb1
meta-data=/dev/vdb1              isize=512    agcount=4, agsize=8519616 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=34078464, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=16639, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

四. 将新添加硬盘分区挂载到指定文件目录

mkdir /datavol
mount /dev/vdb1 /datavol

五. 编辑配置文件,实现开机自动挂载

vi /etc/fstab

在末尾添加如下:

/dev/vdb1    /datavol    xfs    defaults    1    2

使用vi命令:wq保存退出,查看/etc/fstab配置文件

cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Jun 10 14:18:53 2019
#
# 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
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=8223c1d3-9644-4885-80b9-f24158ea6013 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/vdb1    /datavol    xfs    defaults    1    2

六. 重启之后,验证是否成功

df -T
[root@ecs-CwM ~]# df -T
Filesystem              Type     1K-blocks    Used Available Use% Mounted on
devtmpfs                devtmpfs   4064316       0   4064316   0% /dev
tmpfs                   tmpfs      4077448       0   4077448   0% /dev/shm
tmpfs                   tmpfs      4077448    8696   4068752   1% /run
tmpfs                   tmpfs      4077448       0   4077448   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs       17811456 2175180  15636276  13% /
/dev/vda1               xfs        1038336  289548    748788  28% /boot
tmpfs                   tmpfs       815492       0    815492   0% /run/user/0
/dev/vdb1               xfs      136247300   32992 136214308   1% /datavol

七. 跨分区跨文件系统创建软链接目录

mv /var /datavol/
ln -s /datavol/var /var