kvm虚拟机网卡设置

笔者在部署KVM虚拟机时曾遇到一个奇怪的问题,几经探索之后终于解决,现在写出来跟大家分享一下。

笔者在单位部署了一台服务器,上面运行着几部kvm虚拟机,分别执行不同的任务。系统上线之后,需要再增加几部虚拟机。因为当初部署服务器时做了虚拟机备份,所以就复制了一个备份的虚拟机。可是新虚拟机启动之后无法在本地网络上找到新虚拟机的IP地址(本地网络采用DHCP分配IP地址)!因为服务器是远程控制的,当然新虚拟机也就无法使用了。

为了查找原因,笔者把虚拟机复制到本地主机上,用正常方法开启。启动过程及登录都很正常,于是检查网卡状况:

$ ifconfig

可是却只有显示 lo 信息! 怪了, eth0呢?只有lo当然是没有办法同网络通讯的。于是查找一下启动信息:

$ dmesg | grep eth

发现如下信息:

udev: renamed network interface eth0 to eth1

原来eth0已经没有了,被命名为eth1, 再看网卡配置

$ cat /etc/network/interfaces

auto eth0

iface eth0 inet dhcp

至此事情水落石出,原来kvm是在启动时传递mac参数的,如笔者是用下面命令启动kvm虚拟机:

$ sudo kvm -m 256 -hda /data/kvm/mail.img -net nic,vlan=0,macaddr=52-54-00-12-30-05 -net tap,vlan=0,ifname=tap5,script=no -boot c -smp 2 -daemonize -nographic &
注意上面的macaddr=52-54-00-12-30-05, 这就是虚拟机启动后的网卡mac, 因为网络内不可以有相同的mac, 所以启动每个虚拟机的mac都要改。 可是当换了新的mac后,虚拟机里的系统就认为换了新网卡,所以系统改变eth0为eth1, 而在网卡设置里面却只设置了eth0, 所以虚拟机启动之后并没有启动新的eth1网卡,当然就连不上网络了。原因找到了之后问题的解决也就非常简单:

$ vi /etc/network/interfaces

增加以下内容:

auto eth1

iface eth1 inet dhcp

再重新启动网络:

$ /etc/init.d/networking restart

至此问题应该就完全解决了。不过有个问题还要注意, 如果有多次用不同的mac启动虚拟机,可能你的虚拟机里已经有了eth2, eth3甚至是10都是有可能的,因为你每用一个新的mac去启动虚拟机,系统就会增加一个网卡。可以修改下面这个文件:

$ vi /etc/udev/rules.d/70-persistent-net.rules

删除所有的的ethX行,重启虚拟机即可。

关于 清风

关注IT资讯,虚拟化技术,企业VPN, VoIP, CRM应用等。
本文章发表于 (This entry was posted in) linux 技术, 虚拟化技术 标签 (and tagged) . 收藏 (Bookmark the) 永久链接(permalink).

本文有2条评论

  1. Looks like you are an expert in this field, you really got some great points there, thanks.

  2. Really I am not an expert but a common user who would like to share his experience with his friends. If anyone found these information is useful, I will feel grateful.

发表评论 Leave a Reply

邮箱不会出现在评论中(Your email address will not be published.) *号为必填项 Required fields are marked *

*

你可以使用一些简单的 HTML 命令。You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

请回答(What is) 8 + 2 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)