云服務器掛載新數據硬盤到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

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容