KVM虚拟化之虚拟机根分区扩容 2019-07-16

CentOS7.2 KVM虚拟化之虚拟机根分区扩容

一、扩展虚拟磁盘

1. 关闭虚拟机

virsh shutdown  web01

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

[root@kvm01 /data]# qemu-img info centos2-clone.qcow2 
image: centos2-clone.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.0G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

3.扩展磁盘大小

[root@kvm01 /data]# qemu-img resize centos2-clone.qcow2 +10G 

4.再次查看虚拟机大小

[root@kvm01 /data]# qemu-img info centos2-clone.qcow2 
image: centos2-clone.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 1.0G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

二、扩展虚拟机根分区

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

[root@localhost ~]# fdisk -l 

Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x000513fd

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20971519    10484736   83  Linux  #注意这里起始cylinder 是2048,起始cylinder 绝对不可以改,否则会破坏原分区的数据。

2.扩展根分区

[root@localhost ~]# fdisk /dev/vda 
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): p

Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x000513fd

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20971519    10484736   83  Linux

Command (m for help): d
Selected partition 1    #根/分区是多少就输入几,这里是/dev/vda3
Partition 1 is deleted

Command (m for help): p

Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x000513fd

   Device Boot      Start         End      Blocks   Id  System

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

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

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

执行如下命令,重启系统

[root@localhost ~]#reboot

重启后执行如下命令,完成根分区扩充
[root@localhost ~]# xfs_growfs /dev/vda1 
meta-data=/dev/vda1              isize=256    agcount=4, agsize=655296 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=2621184, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2621184 to 5242624

四、验证

[root@localhost ~]# df -h 
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        20G  912M   20G   5% /
devtmpfs        488M     0  488M   0% /dev
tmpfs           497M     0  497M   0% /dev/shm
tmpfs           497M  6.6M  491M   2% /run
tmpfs           497M     0  497M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/0
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容