一、重新初始化数据盘

停用实例,在云盘中选择数据盘d-2zeauk43zyovup2agv9k,点击【重新初始化云盘】按钮。

二、创建分区

2.1 查看数据盘是否已分区

fdisk -l
Disk /dev/vda: 60 GiB, 64424509440 bytes, 125829120 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: 0xaf2d0413

Device     Boot Start       End   Sectors Size Id Type
/dev/vda1  *     2048 125829086 125827039  60G 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

可以看到,目前ECS中有一块大小为100 GiB 的尚未分区的数据盘/dev/vdb

2.2 数据盘分区

fdisk /dev/vdb
Welcome to fdisk (util-linux 2.34).
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 0xb07d778c.

Command (m for help): 

输入m,查看命令。

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

输入p查看当前硬盘的分区信息。

Command (m for help): p
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
Disklabel type: dos
Disk identifier: 0xb07d778c

可以看到,当前硬盘没有分区信息。

输入n新建分区,然后输入p为主分区,其余默认。

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): 
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.

输入w保存分区信息。

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

2.3 格式化分区

新创建的分区需要进行格式化才能进行使用。

mkfs.ext4 /dev/vdb1
mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 26214144 4k blocks and 6553600 inodes
Filesystem UUID: 76fa9acc-1093-4236-bd26-e992b1c3d6ca
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 (131072 blocks): done
Writing superblocks and filesystem accounting information: done   

再次查看数据盘分区。

fdisk -l
Disk /dev/vda: 60 GiB, 64424509440 bytes, 125829120 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: 0xaf2d0413

Device     Boot Start       End   Sectors Size Id Type
/dev/vda1  *     2048 125829086 125827039  60G 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
Disklabel type: dos
Disk identifier: 0xb07d778c

Device     Boot Start       End   Sectors  Size Id Type
/dev/vdb1        2048 209715199 209713152  100G 83 Linux

三、迁移数据

这里以迁移www目录至数据盘/dev/vdb1为例。

3.1 将新分区挂载到临时目录下

mkdir temp
mount /dev/vdb1 /root/temp/

3.2 同步www目录下内容至数据盘

cp -r /www/* /root/temp/
或者
sudo rsync -avx /www/ /root/temp/

复制完成后,需要将数据盘挂载到系统盘相应的目录下。

四、挂载分区

4.1 通知系统内核分区表变化

partprobe /dev/vdb1

4.2 挂载分区

将数据盘挂载到系统盘www的目录下。

mount /dev/vdb1 /www/

并查看是否挂载成功。

df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            1.9G     0  1.9G   0% /dev
tmpfs           380M  680K  379M   1% /run
/dev/vda1        59G  3.7G   53G   7% /
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
tmpfs           380M     0  380M   0% /run/user/0
/dev/vdb1        98G   61M   93G   1% /www

4.3 设置开机自动挂载

挂载后需要设置开机自动挂载,否则重启服务器后挂载的分区就没了。

先查看分区UUID

sudo blkid
/dev/vda1: LABEL="/" UUID="60596b1d-a9fb-4f03-ab01-4c3c86656f6d" TYPE="ext4" PARTUUID="af2d0413-01"
/dev/vdb1: UUID="76fa9acc-1093-4236-bd26-e992b1c3d6ca" TYPE="ext4" PARTUUID="b07d778c-01"

修改/etc/fstab文件,添加新挂载的分区。

vim /etc/fstab

填入新分区挂载信息

UUID=76fa9acc-1093-4236-bd26-e992b1c3d6ca /www      ext4        defaults     1         1

至此,数据www迁移完毕,重启服务器查看数据盘是否正常挂载。

发表回复

后才能评论