[libvirt] how to assign an ip when assign a NIC or VF to the Virtual Machine?

Hi, I use the *Intel Virtualization Technology for Directed I/O*(VT-d) or *Single-Root I/O Virtualization*(SR-IOV) to assign a network card or Virtual Function(VF) to a Virtual Machine(VM) to access the network by the hostdev element in libvirt Domain XML. But the ethX in the VM hasn't an ip,I must to enter the VM to assign an ip to the VM manually. So how can I assign an ip to the VM when assign the NIC or VF to the VM ?thanks. best regards, qinguan

On Sat, May 07, 2011 at 11:24:23PM +0800, guan qin wrote:
So how can I assign an ip to the VM when assign the NIC or VF to the VM
It doesn't work like this. Just like with real hardware, all the virtual hardware has is a MAC address. The (real or virtual) machine has to use some other method to get an IP address and hostname after it has booted. As with real machines, either it does that using DHCP, or it has the IP address hard-coded somewhere on disk. The best solution is to create a separate DHCP service which assigns a specific IP address to the known MAC address of the NIC/VF. You can get the MAC address using "virsh dumpxml". Another solution would be to edit the guest before booting it and give it the address you want it to have. eg for RHEL and Fedora guests: guestfish -i -d MyGuest -- \ upload - /etc/sysconfig/network-scripts/ifcfg-eth0 <<_EOF DEVICE=eth0 IPADDR=192.168.1.2 NETMASK=255.255.255.0 NETWORK=192.168.1.0 BROADCAST=192.168.1.255 ONBOOT=yes _EOF Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into Xen guests. http://et.redhat.com/~rjones/virt-p2v

thanks for your reply ! The second solution you mentioned may be difficult , because when I assign the ethX to the VM, the X in the 'ethX' is random (the 'X' in the host may be different in the VM),I don't know it before I boot the VM . so maybe I couldn't edit the guest correctly before booting VM. The first solution : The network card's MAC address I can know and assign an fixed IP in advance, but for the VFs , before I create the VF by "modprobe igb/ixgbe max_vfs=num1,num2" ,I couldn't know the MAC address before either,the MAC address generated randomly too. So maybe I should edit the DHCP server configure file after creating the VFs. At first , I want to make the ethX name eth0, in that case, I can edit the guest before booting the VM ,and I also can use the DHCP to dynamicly assign an ip to the VM without editing the DHCP server configure file . but because of unknowing the MAC address , I don't know how to use udev to bind the netword card or VF to eth0 ? I decided to first try the first solution you mentioned. best regards, qinguan 2011/5/8 Richard W.M. Jones <rjones@redhat.com>
On Sat, May 07, 2011 at 11:24:23PM +0800, guan qin wrote:
So how can I assign an ip to the VM when assign the NIC or VF to the VM
It doesn't work like this.
Just like with real hardware, all the virtual hardware has is a MAC address. The (real or virtual) machine has to use some other method to get an IP address and hostname after it has booted. As with real machines, either it does that using DHCP, or it has the IP address hard-coded somewhere on disk.
The best solution is to create a separate DHCP service which assigns a specific IP address to the known MAC address of the NIC/VF. You can get the MAC address using "virsh dumpxml".
Another solution would be to edit the guest before booting it and give it the address you want it to have. eg for RHEL and Fedora guests:
guestfish -i -d MyGuest -- \ upload - /etc/sysconfig/network-scripts/ifcfg-eth0 <<_EOF DEVICE=eth0 IPADDR=192.168.1.2 NETMASK=255.255.255.0 NETWORK=192.168.1.0 BROADCAST=192.168.1.255 ONBOOT=yes _EOF
Rich.
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into Xen guests. http://et.redhat.com/~rjones/virt-p2v

On Sun, May 08, 2011 at 10:56:54AM +0800, guan qin wrote:
The second solution you mentioned may be difficult , because when I assign the ethX to the VM, the X in the 'ethX' is random (the 'X' in the host may be different in the VM),I don't know it before I boot the VM . so maybe I couldn't edit the guest correctly before booting VM.
AFAIK the guest should always see "eth0", so this shouldn't be any problem. If not, write udev rule(s) in the guest to force the name to be stable.
The first solution : The network card's MAC address I can know and assign an fixed IP in advance, but for the VFs , before I create the VF by "modprobe igb/ixgbe max_vfs=num1,num2" ,I couldn't know the MAC address before either,the MAC address generated randomly too. So maybe I should edit the DHCP server configure file after creating the VFs.
It seems that for SR-IOV, MAC addresses are assigned to VFs randomly by the kernel. It should be possible to read out the VF using (eg) libvirt before the VM has booted (if not, it would be a bug). I think you can also assign fixed MAC addresses to VFs in advance if that would be simpler. However I've not really used SR-IOV in anger so this may be wrong. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://et.redhat.com/~rjones/virt-top

If I use SR-IOV, the guest may be always "eth0" , but when I use the VT-d ,I often got an ethX with a random X in VM. AFAIK, udev make the NIC name be stable by the MAC address. So when I haven't assigned a VF or NetworkCard to the VM, how can I force the name in the guest to 'eth0' ? best regard, qinguan 2011/5/8 Richard W.M. Jones <rjones@redhat.com>
On Sun, May 08, 2011 at 10:56:54AM +0800, guan qin wrote:
The second solution you mentioned may be difficult , because when I assign the ethX to the VM, the X in the 'ethX' is random (the 'X' in the host may be different in the VM),I don't know it before I boot the VM . so maybe I couldn't edit the guest correctly before booting VM.
AFAIK the guest should always see "eth0", so this shouldn't be any problem. If not, write udev rule(s) in the guest to force the name to be stable.
The first solution : The network card's MAC address I can know and assign an fixed IP in advance, but for the VFs , before I create the VF by "modprobe igb/ixgbe max_vfs=num1,num2" ,I couldn't know the MAC address before either,the MAC address generated randomly too. So maybe I should edit the DHCP server configure file after creating the VFs.
It seems that for SR-IOV, MAC addresses are assigned to VFs randomly by the kernel. It should be possible to read out the VF using (eg) libvirt before the VM has booted (if not, it would be a bug). I think you can also assign fixed MAC addresses to VFs in advance if that would be simpler. However I've not really used SR-IOV in anger so this may be wrong.
Rich.
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://et.redhat.com/~rjones/virt-top

2011/5/8 Richard W.M. Jones <rjones@redhat.com <mailto:rjones@redhat.com>>
On Sun, May 08, 2011 at 10:56:54AM +0800, guan qin wrote: > The second solution you mentioned may be difficult , because when I assign > the ethX to the VM, the X in the 'ethX' is random (the 'X' in the host may > be different in the VM),I don't know it before I boot the VM . so maybe I > couldn't edit the guest correctly before booting VM.
AFAIK the guest should always see "eth0", so this shouldn't be any problem. If not, write udev rule(s) in the guest to force the name to be stable.
> The first solution : > The network card's MAC address I can know and assign an fixed IP in > advance, but for the VFs , before I create the VF by "modprobe > igb/ixgbe > max_vfs=num1,num2" ,I couldn't know the MAC address before either,the MAC > address generated randomly too. > So maybe I should edit the DHCP server configure file after creating the > VFs.
It seems that for SR-IOV, MAC addresses are assigned to VFs randomly by the kernel. It should be possible to read out the VF using (eg) libvirt before the VM has booted (if not, it would be a bug). I think you can also assign fixed MAC addresses to VFs in advance if that would be simpler. However I've not really used SR-IOV in anger so this may be wrong.
On 05/08/2011 10:35 AM, guan qin wrote:
If I use SR-IOV, the guest may be always "eth0" , but when I use the VT-d ,I often got an ethX with a random X in VM. AFAIK, udev make the NIC name be stable by the MAC address. So when I haven't assigned a VF or NetworkCard to the VM, how can I force the name in the guest to 'eth0' ?
(Please don't top-quote. It makes it hard to follow the discussion later). As I understand it (and I may have it wrong, as I don't actually have any SRIOV hardware to play with), an SRIOV VF gets a different random MAC address each time the host boots. However, udev on the host apparently ignores VFs when saving off its mac address <--> ethX mapping for reference on subsequent boots, so the VFs as seen from the host will have a stable name (but unstable mac address). When you map that VF through to the guest, the guest probably doesn't know that it's a VF of an SRIOV card, it thinks it's a standard ethernet card, so the guest's udev saves that particular MAC address<->ethX mapping for subsequent boots; the problem is that the next time it boots, it will get *yet another* mac address, which will be mapped to *yet another* ethX name. The solution is to clear out the udev mapping on the guest before each boot, or otherwise teach it to not save the mapping. Alternately, use a different method of attaching your guest to the network (ie, instead of using PCI Passthrough, use <interface type='direct'> to attach a guest interface to one of the VFs, and give the guest a fixed MAC address of your own choosing.)

Laine Stump wrote:
2011/5/8 Richard W.M. Jones <rjones@redhat.com <mailto:rjones@redhat.com>>
On Sun, May 08, 2011 at 10:56:54AM +0800, guan qin wrote: > The second solution you mentioned may be difficult , because when I assign > the ethX to the VM, the X in the 'ethX' is random (the 'X' in the host may > be different in the VM),I don't know it before I boot the VM . so maybe I > couldn't edit the guest correctly before booting VM.
AFAIK the guest should always see "eth0", so this shouldn't be any problem. If not, write udev rule(s) in the guest to force the name to be stable.
> The first solution : > The network card's MAC address I can know and assign an fixed IP in > advance, but for the VFs , before I create the VF by "modprobe > igb/ixgbe > max_vfs=num1,num2" ,I couldn't know the MAC address before either,the MAC > address generated randomly too. > So maybe I should edit the DHCP server configure file after creating the > VFs.
It seems that for SR-IOV, MAC addresses are assigned to VFs randomly by the kernel. It should be possible to read out the VF using (eg) libvirt before the VM has booted (if not, it would be a bug). I think you can also assign fixed MAC addresses to VFs in advance if that would be simpler. However I've not really used SR-IOV in anger so this may be wrong.
On 05/08/2011 10:35 AM, guan qin wrote:
If I use SR-IOV, the guest may be always "eth0" , but when I use the VT-d ,I often got an ethX with a random X in VM. AFAIK, udev make the NIC name be stable by the MAC address. So when I haven't assigned a VF or NetworkCard to the VM, how can I force the name in the guest to 'eth0' ?
(Please don't top-quote. It makes it hard to follow the discussion later).
As I understand it (and I may have it wrong, as I don't actually have any SRIOV hardware to play with), an SRIOV VF gets a different random MAC address each time the host boots. However, udev on the host apparently ignores VFs when saving off its mac address <--> ethX mapping for reference on subsequent boots, so the VFs as seen from the host will have a stable name (but unstable mac address).
Perhaps some of the work done in iproute2, et.al. can be leveraged. The ip command could be used to set user-defined MAC addr prior to passing to the guest ip link set dev ethX vf Y mac aa:bb:cc:dd:ee:ff Regards, Jim
When you map that VF through to the guest, the guest probably doesn't know that it's a VF of an SRIOV card, it thinks it's a standard ethernet card, so the guest's udev saves that particular MAC address<->ethX mapping for subsequent boots; the problem is that the next time it boots, it will get *yet another* mac address, which will be mapped to *yet another* ethX name.
The solution is to clear out the udev mapping on the guest before each boot, or otherwise teach it to not save the mapping. Alternately, use a different method of attaching your guest to the network (ie, instead of using PCI Passthrough, use <interface type='direct'> to attach a guest interface to one of the VFs, and give the guest a fixed MAC address of your own choosing.)
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
participants (4)
-
guan qin
-
Jim Fehlig
-
Laine Stump
-
Richard W.M. Jones