KVM管理虚拟机:常用命令

电玩女神 2022-01-28 05:11 854阅读 0赞

安装kvm

文档参考:
CentOS7 :http://www.linux-kvm.org/page/HOWTO#RHEL.2FCentOS_7
CentOS6 :https://wiki.centos.org/zh/HowTos/KVM --> 白皮书: 第一册, 第二册

  • 1.a,创建虚拟机
  • 1.b,运行/停止虚拟机
  • 1.c,查看虚拟机

  • 虚拟机配置调整:内存,磁盘,cpu数
  • 虚拟机的两大主要文件:image, xml文件

    1, image路径:

    root@controller:/var/lib/libvirt/images# ls
    cent7-clone.qcow2 cent7.qcow2 centos7.0.qcow2

    2, 虚拟机xml配置文件路径:

    root@controller:/etc/libvirt/qemu# ls
    cent7-clone.xml cent7.xml centos7.0.xml networks

1, 虚拟机:生命周期管理

  • a, 创建虚拟机 (virt-install)

    内存:1G, cup: 2, 硬盘:30G

    执行命令后,满足条件就会弹出centos的安装界面

    virt-install \
    —name c6 \
    —ram=1024 \
    —vcpus=2 \
    —network=bridge:br0 \
    —disk path=/var/lib/libvirt/images/c6.img,size=30 \
    —cdrom /CentOS-6.10-x86_64-minimal.iso

  • b,查看/运行/停止虚拟机 (virsh list/ start/ shutdown,destroy)

    root@controller:/home/wang# virsh list

    Id Name State

    6 centos7.0 running
    9 cent7 running

    root@controller:~/openstack# virsh dominfo cent7
    Id: 20
    Name: cent7
    UUID: a566d8e4-ec6f-42d7-9d31-8d0d3ce5f09a
    OS Type: hvm
    State: running
    CPU(s): 2
    CPU time: 197.0s
    Max memory: 2097152 KiB
    Used memory: 1048576 KiB
    Persistent: yes
    Autostart: disable
    Managed save: no
    Security model: apparmor
    Security DOI: 0
    Security label: libvirt-a566d8e4-ec6f-42d7-9d31-8d0d3ce5f09a (enforcing)

    ########### autostart, start, shutdown, reboot //pause, resume

    root@controller:/etc/network# virsh shutdown cent7
    Domain cent7 is being shutdown

    root@controller:/etc/network# virsh list —all

    Id Name State

    6 centos7.0 running

    • cent7 shut off
  • 3, 删除虚拟机

    root@controller:/ # cat /usr/local/bin/kvm-delete.sh
    read -p ‘输入(要删除的虚拟机):’ name
    echo “….going to delete ..$name”

    1,destroy

    virsh destroy $name

    2,undefine

    virsh undefine $name

    3,del image

    rm -f /var/lib/libvirt/images/$name.qcow2
    echo “delete kvm $name —> ok….”

2, kvm登录虚拟机

  • virsh console xx

    virt-viewer -c qemu:///system VMachinename

    virt-viewer cent7

3, 虚拟机内存调整

  1. ## setmaxmem change maximum memory limit
  2. ## setmem change memory allocation
  3. #虚拟机开机后,动态内存扩展(加上--config表示写入到配置文件,永久生效)
  4. virsh setmaxmem centos7 9GB --config
  5. virsh setmem centos7 8GB --config
  6. ## setvcpus change number of virtual CPUs
  7. ## setvcpu attach/detach vcpu or groups of threads

4, 复制/克隆虚拟机

virt-clone命令: 需要关闭虚拟机再操作

  1. root@controller:~/openstack# virt-clone -o cent7 -n cent7_2 --auto-clone
  2. 正在分配 'cent7_2.qcow2' | 5.0 GB 00:00:23
  3. 成功克隆 'cent7_2'
  4. root@controller:~/openstack# virsh list --all
  5. Id Name State
  6. ----------------------------------------------------
  7. 18 stack running
  8. - cent7 shut off
  9. - cent7_2 shut off
  10. root@controller:~/openstack# ls /var/lib/libvirt/images/
  11. cent7_2.qcow2 cent7.qcow2 centos7.0.qcow2 stack.qcow2
  12. root@controller:~/openstack# ls /etc/libvirt/qemu
  13. cent7_2.xml cent7.xml networks stack.xml

若没有关闭虚拟机来克隆就会报以下错误:

  1. root@controller:~# virsh list
  2. Id Name State
  3. ----------------------------------------------------
  4. 11 centos7.0 running
  5. 12 cent7 paused
  6. root@controller:~# virt-clone -o cent7 --auto-clone
  7. WARNING 设置图形设备端口为自动端口,以避免相互冲突。
  8. ERROR Couldn't create storage volume 'cent7-clone.qcow2': 'internal error: Child process (/usr/bin/qemu-img convert -f qcow2 -O qcow2 -o compat=1.1,lazy_refcounts /var/lib/libvirt/images/cent7.qcow2 /var/lib/libvirt/images/cent7-clone.qcow2) unexpected exit status 1: qemu-img: Could not open '/var/lib/libvirt/images/cent7.qcow2': Failed to get shared "write" lock
  9. Is another process using the image?

5,虚拟机重命名

把上面的cent7-clone 这个刚克隆的虚拟机重命名(先停止虚拟机)–> cent7_2

  1. root@controller:~# virt-clone -o cent7 --auto-clone
  2. 正在分配 'cent7-clone.qcow2' | 40 GB 00:00:47
  3. 成功克隆 'cent7-clone'
  4. #启动克隆好的虚拟机
  5. root@controller:~# virsh start cent7
  6. Domain cent7 started
  7. root@controller:~# virsh start cent7-clone
  8. Domain cent7-clone started
  9. root@controller:~# virsh list
  10. Id Name State
  11. ----------------------------------------------------
  12. 11 centos7.0 running
  13. 13 cent7 running
  14. 14 cent7-clone running
  15. # 1, xml修改
  16. root@controller:~# virsh dumpxml cent7-clone > /etc/libvirt/qemu/cent7_2.xml
  17. root@controller:/etc/libvirt/qemu# ls
  18. cent7_2.xml cent7-clone.xml cent7.xml networks
  19. ####修改旧的名字, 为新的名字
  20. root@controller:/etc/libvirt/qemu# grep cent7-clone cent7_2.xml
  21. <name>cent7-clone</name>
  22. <source file='/var/lib/libvirt/images/cent7-clone.qcow2'/>
  23. root@controller:/etc/libvirt/qemu# sed -i s/cent7-clone/cent7_2/g cent7_2.xml
  24. # 2, image文件重命名
  25. root@controller:/var/lib/libvirt/images# ls
  26. cent7-clone.qcow2 cent7.qcow2
  27. root@controller:/var/lib/libvirt/images# mv cent7-clone.qcow2 cent7_2.qcow2
  28. # 3, 废弃旧的虚拟机,使用新的虚拟机
  29. root@controller:~# virsh list
  30. Id Name State
  31. ----------------------------------------------------
  32. 13 cent7 running
  33. 14 cent7-clone running
  34. root@controller:~# virsh shutdown cent7-clone
  35. Domain cent7-clone is being shutdown
  36. root@controller:/etc/libvirt/qemu# virsh undefine cent7-clone
  37. Domain cent7-clone has been undefined
  38. root@controller:/etc/network# ls /etc/libvirt/qemu/
  39. cent7_2.xml cent7.xml centos7.0.xml networks/
  40. root@controller:/etc/libvirt/qemu# virsh list --all
  41. Id Name State
  42. ----------------------------------------------------
  43. 13 cent7 running
  44. root@controller:/etc/libvirt/qemu# virsh define cent7_2.xml
  45. Domain cent7_2 defined from cent7_2.xml
  46. root@controller:/etc/libvirt/qemu# virsh start cent7_2
  47. viDomain cent7_2 started
  48. root@controller:/etc/libvirt/qemu# virsh list
  49. Id Name State
  50. ----------------------------------------------------
  51. 13 cent7 running
  52. 15 cent7_2 running

6, kvm导出virtualbox/vmware虚拟机

  • 把kvm的虚拟机磁盘文件导出为vdi,vmdk即可在virtualbox或vmware中使用此虚拟机

    sudo qemu-img convert /var/lib/libvirt/images/win7.qcow2 -O vdi ./win7.vdi

发表评论

表情:
评论列表 (有 0 条评论,854人围观)

还没有评论,来说两句吧...

相关阅读

    相关 kvm虚拟迁移

    说明:kvm虚拟机迁移我的总结分为三种: 冷迁移 热迁移 图形迁移   下面说各种迁移的步骤和注意事项: 冷迁移: 这个和克隆还是有区别的:克隆是把源虚拟机关闭,