Hyper-V虚拟化linux系统数据磁盘扩容

linux系统

一直以来对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 文件中,不然重启后磁盘还需要手动挂载。


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