$ sudo apt-get install kvm libvirt-bin
$ sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils
$ sudo apt-get install virtinst
$ sudo apt-get install virt-viewer
sudo virt-install -n web_devel -r 256 \
--disk path=/var/lib/libvirt/images/web_devel.img,bus=virtio,size=4 -c \
jeos.iso --accelerate --network network=default,model=virtio \
--connect=qemu:///system --vnc --noautoconsole -v
ssh -X abc@x.x.x.x
这里有许多管理工具,包括Web界面的管理工具:
http://www.linux-kvm.org/page/Management_Tools
例如,安装WebVirtMgr:
https://github.com/retspen/webvirtmgr/wiki/Install-WebVirtMgr
如果出现错误:
Failed to connect socket to '/var/run/libvirt/libvirt-sock': Permission denied
则需要开启Socket连接权限。
如果是用ssh连接,则必须以wevvirtmgr运行的账户用户,创建从wevvirtmgr服务器到虚拟机宿主机的ssh连接才可以!
就是说,在网页上,创建ssh连接的时候,有个用户名,那么你必须以这个用户名能够从wevirtmgr所在的服务器,自动ssh到虚拟机宿主机!可以用ssh key自动登录来解决。
一般地,webvirtmgr以www-data账户或者nginx账户运行,可以用:
sudo su - www-data -s /bin/bash
即可以www-data账户登录,然后运行ssh-keygen生成密钥(默认保存在 /var/www/.ssh下),把生成的公钥加入虚拟机主机登录用户名(例如 root 或者 abc)的 ~/.ssh/authorized_keys 文件即可。
如果无法创建网桥,可以手动添加,参考下面手动添加网桥部分内容。
Could not start virtual network 'default': internal error
Child process (/usr/sbin/dnsmasq --strict-order --bind-interfaces
--pid-file=/var/run/libvirt/network/default.pid --conf-file=
--except-interface lo --listen-address 192.168.122.1
--dhcp-range 192.168.122.2,192.168.122.254
--dhcp-leasefile=/var/lib/libvirt/dnsmasq/default.leases
--dhcp-lease-max=253 --dhcp-no-override) status unexpected: exit status 2
使用另外一个网段即可,不要再使用192.168.122.0网段。
internal error Child process (/usr/sbin/dnsmasq -u libvirt-dnsmasq --strict-order --bind-interfaces --pid-file
解决方法:给虚拟网卡设定IP段即可,即编辑/etc/network/interfaces,增加虚拟网卡对应的配置
error: Failed to start network default
error: internal error 'brctl setfd virbr%d 0' exited with non-zero status 1 and signal 0: set forward delay failed: No such device
处理方法:暂无。
bridge virbr0 is still up; can't delete it
解决方法:
sudo ifconfig virbr0 down
记住不能用ifdown virbr0,ifdown不支持虚拟网卡
error: Failed to start network default
error: internal error Network is already in use by interface virbr0
Error creating virtual network: cannot create bridge 'virbr0': File exists
处理的核心原则,就是手动创建和设定虚拟网卡或者虚拟的接口即可。
sudo ifconfig virbr0 down
sudo brctl delbr virbr0
sudo brctl delif virbr0 virbr0-nic
error: Cannot read CA certificate '/etc/pki/CA/cacert.pem': No such file or directory
error: failed to connect to the hypervisor
或者
error: Failed to connect socket to '/var/run/libvirt/libvirt-sock': Permission denied
error: failed to connect to the hypervisor
原因:
当你用virsh -c qemu:///system list命令的时候,实际上是要求qemu去连接默认的本机的KVM,但本地KVM又没有设定好权限,允许哪些用户登录!
权限控制有多个地方需要修改,默认是qemu,如果你用TCP或者SSH,需要设定ssh用户名或者TCP连接。
TCP连接需要修改/etc/libvirt/libvirtd.conf,设置 listen_tcp = 1,tcp_port = "16509",listen_addr = "127.0.0.1",unix_sock_group = "libvirtd",unix_sock_ro_perms = "0777",同时记得把你的用户加入libvirtd组!可以参考: http://wiki.libvirt.org/page/Failed_to_connect_to_the_hypervisor
解决方法:
编辑 sudo vi /etc/libvirt/libvirtd.conf
sasl_allowed_username_list = ["<user name>@127.0.0.1", "<user name>@localhost" ]
libvirt report error "error: End of file while reading data: Input/output error"
解决方法:
请检查你的libvirt库和kvm, qemu等的版本匹配!
请使用 ls -l --color /usr/lib/libvirt*.so*
-rwxr-xr-x 1 root root 1045 Sep 28 11:38 libvirt.la*
-rwxr-xr-x 1 root root 1089 Sep 28 11:38 libvirt-lxc.la*
lrwxrwxrwx 1 root root 23 Sep 28 11:38 libvirt-lxc.so -> libvirt-lxc.so.0.1002.9*
lrwxrwxrwx 1 root root 23 Sep 28 11:38 libvirt-lxc.so.0 -> libvirt-lxc.so.0.1002.9*
-rwxr-xr-x 1 root root 73171 Sep 28 11:38 libvirt-lxc.so.0.1002.9*
-rwxr-xr-x 1 root root 1095 Sep 28 11:38 libvirt-qemu.la*
lrwxrwxrwx 1 root root 21 Sep 28 12:28 libvirt-qemu.so -> libvirt-qemu.so.0.9.8
lrwxrwxrwx 1 root root 21 Sep 28 15:12 libvirt-qemu.so.0 -> libvirt-qemu.so.0.9.8
-rwxr-xr-x 1 root root 73548 Sep 28 11:38 libvirt-qemu.so.0.1002.9*
-rw-r--r-- 1 root root 6144 Apr 18 04:03 libvirt-qemu.so.0.9.8
lrwxrwxrwx 1 root root 16 Sep 28 14:37 libvirt.so -> libvirt.so.0.9.8
lrwxrwxrwx 1 root root 16 Sep 28 15:10 libvirt.so.0 -> libvirt.so.0.9.8
-rwxr-xr-x 1 root root 9860299 Sep 28 11:38 libvirt.so.0.1002.9*
-rw-r--r-- 1 root root 1825536 Apr 18 04:03 libvirt.so.0.9.8
如果版本不匹配,请手动改成匹配一致的版本!
this function is not supported by the connection driver: virConnectNumOfInterfaces
原因:
你的虚拟机KVM不支持netcf接口,即编译的时候,没有带 --netcf进行编译,无法支持virsh iface-* 管理指令!
如果是要支持interface,你需要重新编译一个libvirt,工程浩大,如果只是为了创建一个bridge连接,可以用下面方法解决。
解决方法:手动添加网桥。
http://beginlinux.com/sec_train_m/10-traincat/1310-set-up-the-bridge
sudo brctl addbr br0 eth0
sudo ifconfig br0 up
sudo brctl addif br0 eth1
ifconfig br0 192.168.7.119 netmask 255.255.255.0 up
route add default gw 192.168.7.2 dev br0
ifconfig eth0 0.0.0.0 promisc up
ifconfig eth1 0.0.0.0 promisc up
brctl stp br0 off
请参考: https://help.ubuntu.com/community/KVM/Networking
如果是DHCP的,你需要修改/etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet manual
auto br0
iface br0 inet static
address 192.168.0.10
network 192.168.0.0
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.1
bridge_ports eth0
bridge_stp off
bridge_fd 0
bridge_maxwait 0
or to use DHCP
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet manual
auto br0
iface br0 inet dhcp
bridge_ports eth0
bridge_stp off
bridge_fd 0
bridge_maxwait 0