LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-21-2015, 09:24 PM   #1
kcleveland
LQ Newbie
 
Registered: Jun 2010
Posts: 28

Rep: Reputation: 0
Question How to Get VM's Created on KVM to Send DHCP Requests?


Hey Guys,

Here is the issue I am having:

I'm creating vm's on a kvm host from a webpage/app I am developing using libvirt-php.

For some reason, I'm stuck on trying to access the vm's after they have been created. I've tried everything I can think of.

"virsh console vm-name" doesn't work- it just always hangs at the "escape character is ^]" prompt. There is a console defined- see the vm.xml dump below.

I can't get any of the methods of connecting via vnc to work either (remote-viewer, vncviewer, etc.), and the local kvm host I am working on is command line only (no access to a gui).

I can't ssh to the vm yet, as the vm's are not sending out a DHCP request automatically to get an IP like I assumed they would (since they should be set to iface inet dhcp by default, they are plain Ubuntu 14.04.2 loads).

To make sure my DHCP setup was working (this is a separate DHCP server in the lab since my kvm host is using a bridge, not NAT), I created a vm manually with ubuntu-vm-builder (which is being deprecated) and set a static ip/gw on the vm that way.

Then I SSH'd to the vm on the IP I just set, wiped out the /etc/network/interfaces config in the vm and changed its eth0 from static to dhcp. Rebooted the vm, and voila, it grabbed an address from the dhcp server no problem- so there is no issue with the bonding or bridging or dhcp on the kvm host- dhcp is working. All of these vm's are on the same bridge also, so no variable there.

But when I create a vm from my webpage using libvirt-php (on the same kvm host, same bridge), I don't want to assign a static IP for obvious reasons- instead I want the newly created vm to just get an IP automatically from my DHCP server. I thought the vm would just do this automatically when it first booted up, but apparently I am wrong.

Worst part is I can't even access the vm's that I am successfully creating using the webpage, because neither virsh console nor vnc seem to work to get me into the vm to troubleshoot. (I don't want my users to have an extra step anyways i.e. vnc, so I need to figure out why the vm's aren't automatically sending a DHCP request.)

Please help. Thank you for your time!

Dump of active vm.xml:
Code:
<domain type='kvm' id='7'>
  <name>newvm17</name>
  <uuid>f85ef41b-42f6-4cae-a875-ecdbfe806aa4</uuid>
  <memory unit='KiB'>2000896</memory>
  <currentMemory unit='KiB'>2000000</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <resource>
    <partition>/machine</partition>
  </resource>
  <os>
    <type arch='x86_64' machine='pc-i440fx-trusty'>hvm</type>
    <boot dev='cdrom'/>
  </os>
  <features>
    <acpi/>
  </features>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/home/apslab/images/newvm17.qcow2'/>
      <target dev='hda' bus='ide'/>
      <alias name='ide0-0-0'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/home/apslab/images/ubuntu-14.04.2-server-amd64.iso'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <alias name='ide0-1-0'/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0'>
      <alias name='usb0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'>
      <alias name='pci.0'/>
    </controller>
    <controller type='ide' index='0'>
      <alias name='ide0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:de:dc:e0'/>
      <source bridge='br0'/>
      <target dev='vnet4'/>
      <model type='virtio'/>
      <alias name='net0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <source path='/dev/pts/5'/>
      <target port='0'/>
      <alias name='serial0'/>
    </serial>
    <console type='pty' tty='/dev/pts/5'>
      <source path='/dev/pts/5'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    </console>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='5904' autoport='yes' listen='127.0.0.1'>
      <listen type='address' address='127.0.0.1'/>
    </graphics>
    <video>
      <model type='cirrus' vram='9216' heads='1'/>
      <alias name='video0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <alias name='balloon0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </memballoon>
  </devices>
  <seclabel type='dynamic' model='apparmor' relabel='yes'>
    <label>libvirt-f85ef41b-42f6-4cae-a875-ecdbfe806aa4</label>
    <imagelabel>libvirt-f85ef41b-42f6-4cae-a875-ecdbfe806aa4</imagelabel>
  </seclabel>
</domain>
 
Old 07-21-2015, 10:16 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
It's hard (for me, anyway) to check if a given XML file is correct. From what I see, it looks good.

If you want to access the serial console (virsh console domain or virsh start domain --console), the VM itself must be configured for it. To see the grub screen, you need to add directives to grub.cfg. Also add a kernel parameter console=ttyS0.

Why is VNC not working? From what you say, I understand you run your VNC viewer on a different system. Could a firewall be blocking port 5904? Try telnet hypervisorhost 5904 to see if you can get through to that port. Check that the qemu process for this VM has the correct VNC option. Use netstat or ss to check if it is really listening.

Now the real question is about DHCP. There are tools for inspecting your VM image, for example guestmount will mount the image's filesystems on your host, or virt-edit allows you to edit specific files like /etc/network/interfaces or /boot/grub2/grub.cfg, or guestfish. Use those to check and fix the networking setup. Personally I like guestmount because it can be used in a very natural way, your VM image as part of your filesystem.
 
Old 07-22-2015, 07:15 AM   #3
kcleveland
LQ Newbie
 
Registered: Jun 2010
Posts: 28

Original Poster
Rep: Reputation: 0
Thanks so much for taking a look. The virt-edit and guestfish commands are a step in the right direction, but I'm still stumped

The guestfish command returns "guestfish: no operating system was found on this disk", so I decided to again look at the vm I built with ubuntu-vm-builder instead of the vm's created through my web app. Issuing a

Code:
guestfish --ro -i /home/demo4/demo4.qcow2
drops me right into the vm (created with ubuntu-vm-builder) like it should, so now I am suspecting that libvirt-php from my web app is not making the vm's correctly. The php code I am using to create the vm's from the web app is (omitting the creation of the variables you see in the xml for brevity):

Code:
$conn = libvirt_connect("qemu+tcp://$ip/system",false);
    if ($conn==false)
    {
        echo ("Libvirt last error: ".libvirt_get_last_error()."\n");
    }

$xml="
<domain type='kvm'>
  <name>$hostname</name>
  <uuid>$uuid</uuid>
  <memory unit='MB'>$ram</memory>
  <vcpu placement='static'>$cores</vcpu>
  <os>
    <type arch='x86_64'>hvm</type>
    <boot dev='cdrom'/>
  </os>
  <features>
    <acpi/>
  </features>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/home/apslab/images/".$hostname.".qcow2'/>
      <target dev='hda' bus='ide'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='$image_path'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
    </disk>
    <interface type='bridge'>
      <source bridge='br0'/>
      <model type='virtio'/>
    </interface>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1'>
      <listen type='address' address='127.0.0.1'/>
    </graphics>    
  </devices>
</domain>
";

$res=libvirt_domain_define_xml($conn, $xml);
libvirt_domain_create($res);
echo ("Libvirt last error: ".libvirt_get_last_error()."\n");
Libvirt does not throw any errors, and issuing a virsh list --all shows the newly created vm as "running". The code above is what created the vm.xml dump from my initial post.

Still need some help on this one

Addition: If someone needs to setup libvirt-php in order to test this out, I have found an easy way of compiling and using the module. Just download the latest libvirt-php tar file, unzip it, cd inside and do a ./configure, make, make install (if it complains make sure gcc and php5dev packages are installed). Now you have the libvirt-php.so module file. Copy the .so file (assumes an Ubuntu 14.04.2 apache2 webserver with php) to /etc/php5/mods-available/. Then create a libvirt.ini file in that same directory (/etc/php5/mods-available) with the contents of extension=libvirt-php.so. Now a simple php5enmod libvirt-php will load up the module for you.

Last edited by kcleveland; 07-22-2015 at 08:10 AM. Reason: Addition
 
  


Reply

Tags
kvm



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to enable the send mail for receive and send the requests for the orangeHRM reddisekhar Linux - Newbie 3 12-13-2015 01:15 PM
How to send periodic directed probe requests? Mintswitch Linux - Networking 1 03-31-2014 06:47 PM
can we use kvm vm's created on rhel6 on ubantu kvm Gil@LQ Linux - Virtualization and Cloud 1 11-28-2011 02:39 PM
DHCP requests - OpenSuSE 11.0 Dan_D SUSE / openSUSE 5 08-28-2008 03:05 AM
blocking DHCP requests jjfate Linux - Networking 4 06-20-2003 01:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration