• 欢迎访问显哥博客,本网站纯属学习技术,绝无商业用途,欢迎小伙伴们共同学习!研究技术!QQ:52249909 加我QQ
  • 世界75亿人,这么小的概率,能认识你,是我一生的幸运,不妨加个QQ接触一下:52249909 加我QQ

Kvm虚拟机热添加硬盘及扩容【显哥出品,必为精品】

Kvm lixian 4年前 (2020-07-31) 21925次浏览 0个评论 扫描二维码

1.创建一块硬盘

[root@kvm /opt]# qemu-img   create   -f  qcow2    /opt/disk-add02.qcow2   10G
Formatting '/opt/disk-add02.qcow2', fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off 

2.查看硬盘信息

[root@kvm /opt]# qemu-img   info  /opt/disk-add02.qcow2 
image: /opt/disk-add02.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

3.在线热添加硬盘

[root@kvm /opt]# virsh    attach-disk   web01 /opt/disk-add02.qcow2    vdb   --live  --cache=none   --subdriver=qcow2
Disk attached successfully

--live		#在线热添加 

--cache=none  #开启镜像的读写缓存 

--subdriver=qcow2	#驱动类型

4.客户虚拟机查看

[root@kvm /opt]# virsh console web01 
Connected to domain web01
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-957.el7.x86_64 on an x86_64

kvm login: root
Password: 
Last login: Thu Jul 30 11:32:59 on ttyS0

#硬盘分区
[root@kvm ~]# fdisk /dev/vdb 
Command (m for help): n    
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Partition number (1-4, default 1):    #回车
First sector (2048-20971519, default 2048):   #回车
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):    #回车
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set

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

#格式化硬盘
[root@kvm ~]# mkfs.xfs /dev/vdb1 
meta-data=/dev/vdb1              isize=512    agcount=4, agsize=655296 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2621184, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

#创建挂载目录
[root@kvm ~]# mkdir /data/

#挂载
[root@kvm ~]# mount /dev/vdb1 /data/

#查看硬盘
[root@kvm ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda3       7.8G  999M  6.9G  13% /
devtmpfs        486M     0  486M   0% /dev
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           496M  6.7M  489M   2% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
/dev/vda1       197M  105M   93M  53% /boot
tmpfs           100M     0  100M   0% /run/user/0
/dev/vdb1        10G   33M   10G   1% /data

5.移除硬盘

[root@kvm /opt]# virsh detach-disk web01 vdb
Disk detached successfully
#web01===虚拟机名称
#vdb ===硬盘名称

6.扩容跟分区

#扩容磁盘大小
[root@kvm /opt]# qemu-img resize /opt/disk_add01.qcow2 +10G
Image resized.

#不能对存在快照的磁盘进行扩容
[root@kvm /opt]# qemu-img resize /opt/web01.qcow2 +10G
qemu-img: Can't resize an image which has snapshots
qemu-img: This image does not support resize

本站博主 , 版权所有丨如未注明 , 均为原创
转载请注明原文链接:Kvm虚拟机热添加硬盘及扩容【显哥出品,必为精品】
喜欢 (2)

您必须 登录 才能发表评论!