kvm 虚拟机 (centos7) 硬盘扩容

本贴最后更新于 2052 天前,其中的信息可能已经沧海桑田

一、扩展虚拟机磁盘

1.关闭虚拟机

virsh shutdown vm1-clone

2.查看虚拟机磁盘当前大小

[root@kvm01 ~]# qemu-img info /vm-images/vm1-clone.img 
image: /vm-images/vm1-clone.img
file format: qcow2
virtual size: 10G (11206127360 bytes)
disk size: 1.7G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

3.扩展磁盘大小

qemu-img resize /vm-images/vm1-clone.img +5G

4.再次查看虚拟机磁盘当前大小

[root@kvm01 ~]# qemu-img info /vm-images/vm1-clone.img 
image: /vm-images/vm1-clone.img
file format: qcow2
virtual size: 15G (16106127360 bytes)
disk size: 1.7G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

二、扩展虚拟机根分区

1.查看当前分区状况,可以看到磁盘/dev/vda 大小变化

[root@localhost ~]# fdisk -l

Disk /dev/vda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           1          25      200781   83  Linux
/dev/vda2              26         156     1052257+  82  Linux swap / Solaris
/dev/vda3             157        1305     9229342+  83  Linux

#注意这里起始 cylinder 是 157,起始 cylinder 绝对不可以改,否则会破坏原分区的数据。

2.扩展根分区

[root@localhost ~]# fdisk /dev/vda

The number of cylinders for this disk is set to 1958.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/vda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           1          25      200781   83  Linux
/dev/vda2              26         156     1052257+  82  Linux swap / Solaris
/dev/vda3             157        1305     9229342+  83  Linux

Command (m for help): d
Partition number (1-4): 3                    #根/分区是多少就输入几,这里是/dev/vda3

Command (m for help): p

Disk /dev/vda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           1          25      200781   83  Linux
/dev/vda2              26         156     1052257+  82  Linux swap / Solaris

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3                  #之前是多少,这里还输入多少
First cylinder (157-1958, default 157): 
Using default value 157
Last cylinder or +size or +sizeM or +sizeK (157-1958, default 1958):                      #由之前1305,扩为1958
Using default value 1958

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

三、重启虚拟机,完成根分区扩容

执行如下命令,重启系统

[root@localhost ~]#reboot

重启后执行如下命令,完成根分区扩充

[root@localhost ~]#resize2fs /dev/vda3

resize2fs 1.39 (29-May-2006)
Filesystem at /dev/vda3 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/vda3 to 3618641 (4k) blocks.
The filesystem on /dev/vda3 is now 3618641 blocks long.

以上扩容对于 ext2、ext3、ext4 文件系统是有有用的。对于 xfs 则会报错:

执行resize2fs报错:
[root@backup binlog]# resize2fs /dev/mapper/centos-home
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block 当尝试打开 /dev/mapper/centos-home 时
找不到有效的文件系统超级块.

对于 xfs 则使用:xfs_growfs

相关命令如下:


特别注意的是:
resize2fs 命令 针对的是 ext2、ext3、ext4 文件系统
xfs_growfs 命令 针对的是 xfs 文件系统

1)ext2/ext3/ext4 文件系统的调整命令是 resize2fs(增大和减小都支持)

lvextend -L 120G /dev/mapper/centos-home     //增大至120G
lvextend -L +20G /dev/mapper/centos-home     //增加20G
lvreduce -L 50G /dev/mapper/centos-home      //减小至50G
lvreduce -L -8G /dev/mapper/centos-home      //减小8G
resize2fs /dev/mapper/centos-home            //执行调整

2)xfs 文件系统的调整命令是 xfs_growfs(只支持增大)

lvextend -L 120G /dev/mapper/centos-home    //增大至120G
lvextend -L +20G /dev/mapper/centos-home    //增加20G
xfs_growfs /dev/mapper/centos-home          //执行调整

就是说:xfs 文件系统只支持增大分区空间的情况,不支持减小的情况(切记!!!!!)。
硬要减小的话,只能在减小后将逻辑分区重新通过 mkfs.xfs 命令重新格式化才能挂载上,这样的话这个逻辑分区上原来的数据就丢失了。如果有重要文件,那就歇菜喽 ~~~

四、验证

查看当前磁盘状态

[root@localhost ~]# df -h
文件系统              容量  已用 可用 已用% 挂载点
/dev/vda3              14G  1.3G   12G  11% /
/dev/vda1             190M   12M  169M   7% /boot
tmpfs                 379M     0  379M   0% /dev/shm

如上,完成根分区扩容

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...