阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

详解:Hyper-V虚拟主机数据磁盘扩容

1次阅读
没有评论

共计 4908 个字符,预计需要花费 13 分钟才能阅读完成。

一直以来对 Hyper-V Linux 虚拟主机的磁盘扩容困扰,今天将介绍如何利用 fdisk 和 resize2fs 进行数据磁盘扩容。

情况说明:

宿主机系统:Windwos Server 2012 R2 Hyper-V

虚拟主机:CentOS Linux release 7.4.1708 (Core)

平时接触 Hyper- V 较多,所以对磁盘扩容那是常有的事。Windwos 磁盘扩容最简单,本文就不多介绍了。

今天我将以 Centos 虚拟主机进行演示,该小机分为两块磁盘(都是独立磁盘),一块是系统盘 /dev/sda,另一块是数据库盘 /dev/sdb,现在的需求是 sdb 需要在不影响数据的情况下从原来的 30GB 扩容为 100GB。

扩容前先查看磁盘分区及挂载情况
[root@renwole-com ~]# fdisk -l

Disk /dev/sdb: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x5f149419

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    62914559    31456256   83  Linux

Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x00043041

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

Disk /dev/mapper/cl-root: 8585 MB, 8585740288 bytes, 16769024 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/mapper/cl-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

[root@renwole-com ~]# df -hT
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs       8.0G  950M  7.1G  12% /
devtmpfs            devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs               tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs               tmpfs     1.9G  8.3M  1.9G   1% /run
tmpfs               tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1           xfs      1014M  138M  877M  14% /boot
tmpfs               tmpfs     379M     0  379M   0% /run/user/0
/dev/sdb1           ext4       30G   60M   28G   1% /apps

注意:橙色部分 /dev/sdb1 分区大小是 30GB,挂载的也是 30GB。

卸载挂载

删除 /etc/fstab 文件中以下开机磁盘自动挂载信息:

/dev/sdb1 /apps ext4 defaults 0 0
开始虚拟主机磁盘扩容

Hyper- V 虚机操作:

打开 Hyper-V 管理器,找到需要扩容的小机并关机,然后右击设置,点击需要扩容的磁盘,(一般是数据盘)编辑 – 默认下一步 – 选择扩展下一步,这一步骤需要注意,假设你原有磁盘为 30G,那么你想扩展到 100G,请填写 100 即可,点击下一步完成。然后启动虚拟主机。

查看扩容后的磁盘分区情况:

[root@renwole-com ~]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x00043041

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

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x5f149419

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    62914559    31456256   83  Linux

Disk /dev/mapper/cl-root: 8585 MB, 8585740288 bytes, 16769024 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/mapper/cl-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

sdb 分区大小由原来的 30GB 扩展到 100GB 了,但分区 sdb1 并没有使用扩容的 70GB 空间,怎么办呢?

删除分区

删除分区不同等删除数据,删除分区再重建:

[root@renwole-com ~]# fdisk /dev/sdb

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.
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.


Command (m for help): d                 # 删除 sdb1 分区
Selected partition 1
Partition 1 is deleted

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

Command (m for help): w                 # 输入 w 保存
The partition table has been altered!

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

再次查看磁盘分区情况

[root@renwole-com ~]# fdisk -l
...
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x5f149419

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209715199   104856576   83  Linux
...

已经从原来的 30G 扩展成 100G 了,但现在还不能使用,需要进行如下操作才算真正扩容成功:

[root@renwole-com ~]# resize2fs -f /dev/sdb1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/sdb1 to 26214144 (4k) blocks.
The filesystem on /dev/sdb1 is now 26214144 blocks long.

扩容成功。

挂载磁盘
[root@renwole-com ~]# mount /dev/sdb1 /apps/
[root@renwole-com ~]# df -hT
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs       8.0G  950M  7.1G  12% /
devtmpfs            devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs               tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs               tmpfs     1.9G  8.3M  1.9G   1% /run
tmpfs               tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1           xfs      1014M  138M  877M  14% /boot
tmpfs               tmpfs     379M     0  379M   0% /run/user/0
/dev/sdb1           ext4       99G   60M   94G   1% /apps
[root@renwole-com ~]# ls /apps/
web  mysql

如上所示,Linux 虚拟主机数据盘已经成功扩容,查看数据并无丢失。

另外再次将你的磁盘信息写入到 /etc/fstab 文件中,不然重启后磁盘还需要手动挂载。

注意:还是那句话,操作任何数据之前,一定要先备份,养成良好的习惯,以防止数据丢失。

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-09-20发表,共计4908字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中