云服务器挂载新数据硬盘到home目录

由于订购的云服务器,只有系统盘40G,因此又订购数据盘100G,并在管理控制台挂载到云服务器。本文介绍如何将新数据盘挂载到服务器home目录。其中,初始化Linux数据盘方法参考华为云官方文档和腾讯云官方文档:
https://support.huaweicloud.com/usermanual-evs/evs_01_0033.html
将新磁盘挂载到home目录参考了以下链接的方法:
https://cloud.tencent.com/document/product/362/6735

在管理控制台,将新购买的数据盘挂载到云服务器后,进入云服务器终端

1. 查看硬盘情况

输入命令:sudo fdisk -l
结果显示如下:

bioinfo1601@bioinfor1601:~$ sudo fdisk -l
[sudo] password for bioinfo1601:
Disk /dev/vda: 40 GiB, 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
Disklabel type: dos
Disk identifier: 0xcd05077f

Device     Boot Start      End  Sectors Size Id Type
/dev/vda1  *     2048 83884031 83881984  40G 83 Linux


Disk /dev/vdb: 100 GiB, 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

2. 格式化硬盘

1) 运行命令 sudo fdisk /dev/vdb

选择:
n #新建分区
p #primary 分区
1 #分区号
回车 #分区起始sector,默认
回车 #分区终止sector,默认
w #写入分区表
结果显示:

bioinfo1601@bioinfor1601:~$ sudo fdisk /dev/vdb

Welcome to fdisk (util-linux 2.27.1).
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.
Created a new DOS disklabel with disk identifier 0xca84f3b7.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-209715199, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199):

Created a new partition 1 of type 'Linux' and of size 100 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

2) 运行命令 partprobe

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

3) 执行以下命令,将新建分区文件系统设为系统所需格式

sudo mkfs -t ext4 /dev/vdb
结果显示如下:

bioinfo1601@bioinfor1601:~$ sudo mkfs -t ext4 /dev/vdb
mke2fs 1.42.13 (17-May-2015)
Found a dos partition table in /dev/vdb
Proceed anyway? (y,n) y
Creating filesystem with 26214400 4k blocks and 6553600 inodes
Filesystem UUID: b61edd45-245c-4410-8c17-64d361fb6119
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

3. 挂载硬盘

1)新建挂载点

执行以下命令
sudo mkdir /mnt/home

2)将新建分区挂载到新建的挂载点

sudo mount /dev/vdb /mnt/home
执行以下命令,查看挂载结果
df -h
结果显示:

bioinfo1601@bioinfor1601:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            2.0G     0  2.0G   0% /dev
tmpfs           396M  9.2M  386M   3% /run
/dev/vda1        40G  2.7G   35G   8% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
tmpfs           396M     0  396M   0% /run/user/1000
/dev/vdb         99G   60M   94G   1% /mnt/home

4. 设置开机自动挂载磁盘

如果您需要在云服务器系统启动时自动挂载磁盘,不能采用在 /etc/fstab直接指定 /dev/xvdb1的方法,因为云中设备的顺序编码在关闭或者开启云服务器过程中可能发生改变,例如/dev/xvdb1可能会变成/dev/xvdb2。推荐使用UUID来配置自动挂载数据盘。

1)查看数据盘UUID

sudo blkid /dev/vdb

bioinfo1601@bioinfor1601:~$ sudo blkid /dev/vdb
[sudo] password for bioinfo1601:
/dev/vdb: UUID="b61edd45-245c-4410-8c17-64d361fb6119" TYPE="ext4"

2)编辑/etc/fstab

sudo nano /etc/fstab
在文件末尾输入如下行:
UUID=b61edd45-245c-4410-8c17-64d361fb6119 /mnt/home ext4 defaults 0 2

重启云服务器,再次df -h,结果显示新数据盘已经自动挂载在/mnt/home

5. 拷贝home文件到新盘中

sudo rsync -aXS --exclude='/*/.gvfs' /home/. /mnt/home/
sudo diff -r /home /mnt/home -x ".gvfs/*

结果显示:

bioinfo1601@bioinfor1601:~$ sudo diff -r /home /mnt/home -x ".gvfs/*"
Only in /mnt/home: lost+found

6. 重新设置fstab

重新设置fstab,让新分区挂载到/home目录下
打开fstab:
sudo nano /etc/fstab
替换最后一行为:
UUID=b61edd45-245c-4410-8c17-64d361fb6119 /mnt/home ext4 defaults 0 2

7. 将原来/home改名为/old_home,并建立新/home

cd / && sudo mv /home /old_home && sudo mkdir /home
重启后,再次df -h,

        Welcome to Huawei Cloud Service

Last login: Sun Nov  4 19:03:53 2018 from 119.98.157.61
bioinfo1601@bioinfor1601:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            2.0G     0  2.0G   0% /dev
tmpfs           396M  9.2M  386M   3% /run
/dev/vda1        40G  2.7G   35G   8% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/vdb         99G   60M   94G   1% /home
tmpfs           396M     0  396M   0% /run/user/1000

成功了!!!

删除旧的home目录
sudo rm -rI /old_home

你可能感兴趣的:(云服务器挂载新数据硬盘到home目录)