一直以来对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 -lDisk /dev/sdb: 32.2 GB, 32212254720 bytes, 62914560 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 4096 bytesI/O size (minimum/optimal): 4096 bytes / 4096 bytesDisk label type: dosDisk identifier: 0x5f149419 Device Boot Start End Blocks Id System/dev/sdb1 2048 62914559 31456256 83 LinuxDisk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 4096 bytesI/O size (minimum/optimal): 4096 bytes / 4096 bytesDisk label type: dosDisk identifier: 0x00043041 Device Boot Start End Blocks Id System/dev/sda1 * 2048 2099199 1048576 83 Linux/dev/sda2 2099200 20971519 9436160 8e Linux LVMDisk /dev/mapper/cl-root: 8585 MB, 8585740288 bytes, 16769024 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 4096 bytesI/O size (minimum/optimal): 4096 bytes / 4096 bytesDisk /dev/mapper/cl-swap: 1073 MB, 1073741824 bytes, 2097152 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 4096 bytesI/O size (minimum/optimal): 4096 bytes / 4096 bytes[root@renwole-com ~]# df -hTFilesystem 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% /devtmpfs tmpfs 1.9G 0 1.9G 0% /dev/shmtmpfs tmpfs 1.9G 8.3M 1.9G 1% /runtmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup/dev/sda1 xfs 1014M 138M 877M 14% /boottmpfs 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 -lDisk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 4096 bytesI/O size (minimum/optimal): 4096 bytes / 4096 bytesDisk label type: dosDisk identifier: 0x00043041 Device Boot Start End Blocks Id System/dev/sda1 * 2048 2099199 1048576 83 Linux/dev/sda2 2099200 20971519 9436160 8e Linux LVMDisk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 4096 bytesI/O size (minimum/optimal): 4096 bytes / 4096 bytesDisk label type: dosDisk identifier: 0x5f149419 Device Boot Start End Blocks Id System/dev/sdb1 2048 62914559 31456256 83 LinuxDisk /dev/mapper/cl-root: 8585 MB, 8585740288 bytes, 16769024 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 4096 bytesI/O size (minimum/optimal): 4096 bytes / 4096 bytesDisk /dev/mapper/cl-swap: 1073 MB, 1073741824 bytes, 2097152 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 4096 bytesI/O size (minimum/optimal): 4096 bytes / 4096 bytes登录后复制sdb分区大小由原来的30GB扩展到100GB了,但分区sdb1并没有使用扩容的70GB空间,怎么办呢?
删除分区删除分区不同等删除数据,删除分区再重建:
[root@renwole-com ~]# fdisk /dev/sdbThe device presents a logical sector size that is smaller thanthe physical sector size. Aligning to a physical sector (or optimalI/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 1Partition 1 is deletedCommand (m for help): n# 新建分区Partition type: p primary (0 primary, 0 extended, 4 free) e extendedSelect (default p): pPartition number (1-4, default 1): 1# 指定分区号First sector (2048-209715199, default 2048):Using default value 2048Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):Using default value 209715199Partition 1 of type Linux and of size 100 GiB is setCommand (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 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 4096 bytesI/O size (minimum/optimal): 4096 bytes / 4096 bytesDisk label type: dosDisk identifier: 0x5f149419 Device Boot Start End Blocks Id System/dev/sdb1 2048 209715199 104856576 83 Linux...登录后复制已经从原来的30G扩展成100G了,但现在还不能使用,需要进行如下操作才算真正扩容成功:
[root@renwole-com ~]# resize2fs -f /dev/sdb1resize2fs 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 -hTFilesystem 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% /devtmpfs tmpfs 1.9G 0 1.9G 0% /dev/shmtmpfs tmpfs 1.9G 8.3M 1.9G 1% /runtmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup/dev/sda1 xfs 1014M 138M 877M 14% /boottmpfs 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 文件中,不然重启后磁盘还需要手动挂载。
注意:还是那句话,操作任何数据之前,一定要先备份,养成良好的习惯,以防止数据丢失。
以上就是详解:Hyper-V虚拟主机数据磁盘扩容的详细内容!