CentOS 7 安装 libvirt

参考

准备

下载 CentOS 7 Cloud Image :

  1. wget https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-1802.qcow2.xz

安装

  1. yum install -y libvirt kvm qemu-kvm

如果需要 genisoimage , 请安装:

  1. yum install -y genisoimage

创建第一个虚拟机

准备

启动 libvirtd 服务:

  1. systemctl start libvirtd

查看当前虚拟机列表:

  1. virsh list

创建目录 /data/vos/centos/,进入该目录:

  1. mkdir -pv /data/vos/centos/
  2. cd /data/vos/centos/

复制 CentOS-7-x86_64-GenericCloud-1802.qcow2 到该目录。

创建系统盘 ooclab-dev.qcow2

ooclab-dev :虚拟机的 domain name ,在 libvirt 里用于管理虚拟机实例的标识。下面出现ooclab-dev也是如此,建议保持一致。

  1. qemu-img create -b CentOS-7-x86_64-GenericCloud-1802.qcow2 -f qcow2 ooclab-dev.qcow2

创建 config.iso

创建 user-data :

  1. #cloud-config
  2. runcmd:
  3. - [ yum, -y, remove, cloud-init ]
  4. output:
  5. all: ">> /var/log/cloud-init.log"
  6. # 用户
  7. users:
  8. - name: root
  9. # add ssh public keys
  10. ssh_authorized_keys:
  11. - ssh-rsa AAAAB3NzaC1yc2E...QfC4n03w== root@ooclab-t10 # 需要修改为实际用户的ssh public key
  12. - ssh-rsa AAAAB3NzaC...6IZQ== gwind@mbp
  13. chpasswd:
  14. list: |
  15. root:ooclab # 需要修改为实际用户名和密码
  16. expire: False
  17. # configure interaction with ssh server
  18. ssh_svcname: ssh
  19. ssh_deletekeys: True
  20. ssh_genkeytypes: ['rsa', 'ecdsa']

创建 meta-data :

  1. local-hostname: node01

创建 config.iso :

  1. genisoimage -jcharset utf-8 -output config.iso -volid cidata -joliet -rock user-data meta-data

注意 :

  1. user-data 文件第一行必须是 #cloud-config ,一个其他字符不可以有(含空格).

创建 ooclab-dev.xml

  1. <domain type="kvm">
  2. <name>ooclab-dev</name>
  3. <os>
  4. <type arch="x86_64" machine="pc">hvm</type>
  5. <boot dev="hd"/>
  6. </os>
  7. <memory unit="MiB">4096</memory>
  8. <vcpu>4</vcpu>
  9. <features>
  10. <acpi/>
  11. <apic/>
  12. <pae/>
  13. </features>
  14. <clock offset="utc"/>
  15. <on_poweroff>destroy</on_poweroff>
  16. <on_reboot>restart</on_reboot>
  17. <on_crash>restart</on_crash>
  18. <devices>
  19. <emulator>/usr/bin/qemu-system-x86_64</emulator>
  20. <disk device="disk" type="file">
  21. <target dev="vda" bus="virtio"/>
  22. <driver cache="none" io="threads" name="qemu" type="qcow2"/>
  23. <source file="/data/vos/centos/ooclab-dev.qcow2"/>
  24. </disk>
  25. <disk type='file' device='cdrom'>
  26. <driver name='qemu' type='raw'/>
  27. <source file='/data/vos/centos/config.iso'/>
  28. <target dev='hdc' bus='ide'/>
  29. <readonly/>
  30. </disk>
  31. <interface type="network">
  32. <source network="default"/>
  33. <mac address="AC:DE:92:01:00:00"/>
  34. <model type='virtio' />
  35. </interface>
  36. <serial type="pty">
  37. <target port="0"/>
  38. </serial>
  39. <console type="pty">
  40. <target port="0" type="serial"/>
  41. </console>
  42. <input bus="usb" type="tablet"/>
  43. <graphics autoport="yes" keymap="en-us" listen="0.0.0.0" passwd="sailcraft" port="-1" type="spice"/>
  44. <video>
  45. <model type="qxl" vram="131072"/>
  46. </video>
  47. <sound model="ac97"/>
  48. <memballon model="virtio">
  49. <address bus="0x00" domain="0x0000" function="0x0" slot="0x05" type="pci"/>
  50. </memballon>
  51. <channel type="spicevmc">
  52. <target name="com.redhat.spice.0" type="virtio"/>
  53. </channel>
  54. <channel type="unix">
  55. <source mode="bind"/>
  56. <target name="org.qemu.guest_agent.0" type="virtio"/>
  57. </channel>
  58. </devices>
  59. </domain>

备注:

  • <source file="/data/vos/centos/ooclab-dev.qcow2"/> :修改为对应的qcow2文件的地址
  • <source file='/data/vos/centos/config.iso'/> :修改为对应iso文件的地址

说明:

  • Linux 虚拟机,clock 使用 utc

创建虚拟机

  1. virsh create ooclab-dev.xml

使用

  1. virsh console ooclab-dev

查看 IP , 通过另外的 ssh client 登录:

  1. ssh -v centos@192.168.122.XXX

Tips

创建 base 映像

如果需要将当前工作保存为一个 base image , 需要做些简单清理:

  1. unalias rm
  2. sed -i '/HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth0
  3. ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  4. rm /etc/udev/rules.d/70*
  5. rm ~/.bash_history

查看虚拟机 IP

方法一

  1. # virsh net-dhcp-leases default
  2. Expiry Time MAC 地址 Protocol IP address Hostname Client ID or DUID
  3. -------------------------------------------------------------------------------------------------------------------
  4. 2018-04-28 21:52:08 ac:de:92:01:00:01 ipv4 192.168.122.232/24 - -
  5. 2018-04-28 21:44:31 ac:de:92:01:00:03 ipv4 192.168.122.234/24 - -

方法二

获取 IP 另外一个方式:

  1. virsh qemu-agent-command ooclab-dev '{"execute":"guest-network-get-interfaces"}' | jq

FAQ

failed to initialize KVM: Permission denied

创建虚拟机出现错误:

  1. [root@ooclab-t10 centos]# virsh create ooclab-dev.xml
  2. 错误:从 ooclab-dev.xml 创建域失败
  3. 错误:internal error: process exited while connecting to monitor:
  4. (process:12948): GLib-WARNING **: gmem.c:483: custom memory allocation vtable not supported
  5. Could not access KVM kernel module: Permission denied
  6. failed to initialize KVM: Permission denied

请查看 /dev/kvm 权限:

  1. [root@ooclab-t10 centos]# ls -l /dev/kvm
  2. crw------- 1 root root 10, 232 4 24 23:56 /dev/kvm

可以修改权限:

  1. [root@ooclab-t10 centos]# chmod 660 /dev/kvm
  2. [root@ooclab-t10 centos]# chown root.kvm /dev/kvm

不过最终我发现安装完 qemu-kvm 包即可解决该问题:

  1. [root@ooclab-t10 centos]# ls -l /dev/kvm
  2. crw-rw-rw- 1 root kvm 10, 232 4 25 00:07 /dev/kvm