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

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

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

1.创建一块硬盘

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

2.查看硬盘信息

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

3.在线热添加硬盘

  1. [root@kvm /opt]# virsh attach-disk web01 /opt/disk-add02.qcow2 vdb --live --cache=none --subdriver=qcow2
  2. Disk attached successfully
  3.  
  4. --live #在线热添加
  5.  
  6. --cache=none #开启镜像的读写缓存
  7.  
  8. --subdriver=qcow2 #驱动类型

4.客户虚拟机查看

  1. [root@kvm /opt]# virsh console web01
  2. Connected to domain web01
  3. Escape character is ^]
  4.  
  5. CentOS Linux 7 (Core)
  6. Kernel 3.10.0-957.el7.x86_64 on an x86_64
  7.  
  8. kvm login: root
  9. Password:
  10. Last login: Thu Jul 30 11:32:59 on ttyS0
  11.  
  12. #硬盘分区
  13. [root@kvm ~]# fdisk /dev/vdb
  14. Command (m for help): n
  15. Partition type:
  16. p primary (0 primary, 0 extended, 4 free)
  17. e extended
  18. Select (default p):
  19. Partition number (1-4, default 1): #回车
  20. First sector (2048-20971519, default 2048): #回车
  21. Using default value 2048
  22. Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): #回车
  23. Using default value 20971519
  24. Partition 1 of type Linux and of size 10 GiB is set
  25.  
  26. Command (m for help): w #保存
  27. The partition table has been altered!
  28.  
  29. #格式化硬盘
  30. [root@kvm ~]# mkfs.xfs /dev/vdb1
  31. meta-data=/dev/vdb1 isize=512 agcount=4, agsize=655296 blks
  32. = sectsz=512 attr=2, projid32bit=1
  33. = crc=1 finobt=0, sparse=0
  34. data = bsize=4096 blocks=2621184, imaxpct=25
  35. = sunit=0 swidth=0 blks
  36. naming =version 2 bsize=4096 ascii-ci=0 ftype=1
  37. log =internal log bsize=4096 blocks=2560, version=2
  38. = sectsz=512 sunit=0 blks, lazy-count=1
  39. realtime =none extsz=4096 blocks=0, rtextents=0
  40.  
  41. #创建挂载目录
  42. [root@kvm ~]# mkdir /data/
  43.  
  44. #挂载
  45. [root@kvm ~]# mount /dev/vdb1 /data/
  46.  
  47. #查看硬盘
  48. [root@kvm ~]# df -h
  49. Filesystem Size Used Avail Use% Mounted on
  50. /dev/vda3 7.8G 999M 6.9G 13% /
  51. devtmpfs 486M 0 486M 0% /dev
  52. tmpfs 496M 0 496M 0% /dev/shm
  53. tmpfs 496M 6.7M 489M 2% /run
  54. tmpfs 496M 0 496M 0% /sys/fs/cgroup
  55. /dev/vda1 197M 105M 93M 53% /boot
  56. tmpfs 100M 0 100M 0% /run/user/0
  57. /dev/vdb1 10G 33M 10G 1% /data

5.移除硬盘

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

6.扩容跟分区

  1. #扩容磁盘大小
  2. [root@kvm /opt]# qemu-img resize /opt/disk_add01.qcow2 +10G
  3. Image resized.
  4.  
  5. #不能对存在快照的磁盘进行扩容
  6. [root@kvm /opt]# qemu-img resize /opt/web01.qcow2 +10G
  7. qemu-img: Can't resize an image which has snapshots
  8. qemu-img: This image does not support resize

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

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