Virtualization in Grid'5000: Difference between revisions

From Grid5000
Jump to navigation Jump to search
No edit summary
Line 14: Line 14:
In order to test easily the kvm environment, we will use an interactive job.
In order to test easily the kvm environment, we will use an interactive job.


  jdoe@fnancy:~$ oarsub -I
{{Term|location=frontend|cmd=<code class="command">oarsub -I</code>}}
 
{{Note|text=
- Some clusters don't have virtualization support in the BIOS. You can check if a node supports virtualization with the following command:
  {{Term|location=frontend|cmd=<code class="command"><code class="command">egrep '^flags.*(vmx&#124;svm)' /proc/cpuinfo</code>}}
- You can request nodes with enabled virtualization support, using the "virtual" property:
{{Term|location=frontend|cmd=<code class="command">oarsub -I -p "virtual != 'none'"</code>}}
}}


=== Disk image, virtual machine ===
=== Disk image, virtual machine ===


A disk image containing debian squeeze is available at the following path:
A disk image containing debian squeeze is available at the following path:
/grid5000/images/KVM/squeeze-x64-base.qcow2
<code class="command">/grid5000/images/KVM/squeeze-x64-base.qcow2</code>


It can be use as a base for more advanced work.
It can be use as a base for more advanced work.
For the next steps of this tutorial, copy the disk image to /tmp on the node
For the next steps of this tutorial, copy the disk image to /tmp on the node


jdoe@griffon-37:~$ cp /grid5000/images/KVM/squeeze-x64-base.qcow2 /tmp/
{{Term|location=node|cmd=<code class="command">cp /grid5000/images/KVM/squeeze-x64-base.qcow2 /tmp/</code>}}


=== Network configuration ===
=== Network configuration ===


Create Tun/Tap interface for each virtual machines.
In order to use the network with kvm, a Tun/Tap interface must be created for each virtual machines.
This virtual interface will be attached to your virtual machine, and bridged on the production network.
This virtual interface will be attached to your virtual machine, and bridged on the production network.
Therefore, the virtual machine will be able to get an IP from the DHCP server
Therefore, the virtual machine will be able to get an IP from the DHCP server and access the network.
and access the network.


A script is available to create automatically this interface on the node:
A script is available to create automatically this interface on the node:
<code class="command">create_tap</code>:
<code class="command">create_tap</code>:


  jdoe@griffon-37:~$ sudo create_tap
{{Term|location=node|cmd=<code class="command">sudo create_tap</code>}}


* Tun/Tap interfaces are listed by issuing the command <code class="command">/sbin/ifconfig</code>.
* Tun/Tap interfaces are listed by issuing the command <code class="command">/sbin/ifconfig</code>.
jdoe@griffon-37:~$ /sbin/ifconfig
{{Term|location=node|cmd=<code class="command">/sbin/ifconfig</code>}}


[...]
  tap0      Link encap:Ethernet  HWaddr 00:16:3e:db:c6:41
  tap0      Link encap:Ethernet  HWaddr 00:16:3e:db:c6:41
           inet6 addr: fe80::58ff:a4ff:fe97:c6a8/64 Scope:Link
           inet6 addr: fe80::58ff:a4ff:fe97:c6a8/64 Scope:Link
Line 50: Line 55:
           RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
           RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)


{{Note|text= Create one Tun/Tap interface per guest OS.
{{Note|text=- Create one Tun/Tap interface per guest OS.
- Use <code class="command">tunctl</code> if you need to delete a Tun/Tap device.
{{Term|location=node|cmd=<code class="command">/usr/sbin/tunctl -d tap0</code>}}
}}
}}


=== Generate a random mac address for the Tun/Tap interface ===
=== Generate a random mac address ===
 
* Use the following script <code class="command">random_mac</code>. This will generate a random mac address, starting with 00:16:3e, which will be attributed in the next step to the virtual machine.
 
{{Term|location=node|cmd=<code class="command">export MACADDR=`random_mac`</code>}}
 
{{Note|text= The first three bytes are important (identification for the virtualized network). Do not change it.}}
 
=== Run the guest OS using the kvm command ===
 
Start the virtual machine with the kvm command. The following command is just an example, feel free to adapt it to your use case.


* Use the following script <code class="command">random_mac</code>. This will
{{Term|location=node|cmd=<code class="command">screen kvm -m 512 -hda /tmp/squeeze-x64-base.qcow2 -net nic,macaddr=<code class="command">$MACADDR</code> -net tap,ifname=<code class="command">tap0</code>,script=no -nographic</code>}}
generate a random mac address, starting with 00:16:3e.


jdoe@griffon-37:~$ export MACADDR=`random_mac`
{{Note|text=- <code class="command">tap0</code> is the name of our Tun/Tap interface. Adapt it with the Tun/Tap name on witch you want to attach your guest OS. <br />
{{Note|text= The first three octets are important (identification for the virtualized network). Don't change it.
- The variable $MACADDR contains the previously generated mac address.
- The password for the <code class="command">root</code> account is <code class="command">grid5000</code>
}}
}}


=== Run the guest OS using kvm ===
* Manage the console for all your guest OS with [[Screen]]
 
=== Or, use libvirt ===


Use the kvm command.
Libvirt is a toolkit for managing virtualization servers. Libvirt is also an abstraction layer for different virtualization solutions, including KVM but also Xen and VMWare ESX.
In our case, we use libvirt on top of KVM.


jdoe@griffon-37:~$ screen kvm -m 512 -hda /tmp/squeeze-x64-base.qcow2 -net nic,macaddr=<code class="command">$MACADDR</code> -net tap,ifname=<code class="command">tap0</code>,script=no -nographic
* Create a domain file in XML, describing a virtual machine. Don't forget to adapt this example to your case (especially, replace the mac address by a randomly generated one).
{{Note|text= In this example, <code class="command">tap0</code> is the name of our Tun/Tap interface. Adapt it with the Tun/Tap name on witch you want to attach your guest OS.
}}


=== Or, you can start the guest OS using libvirt ===
* Edit the domain file
eg : <code class="file">domain.xml</code>
eg : <code class="file">domain.xml</code>
<pre class="brush: bash">
<pre class="brush: bash">
  <domain type='kvm'>
  <domain type='kvm'>
Line 111: Line 128:


* Now, the guest OS can be started.
* Now, the guest OS can be started.
jdoe@griffon-37:~$ virsh create domain.xml
{{Term|location=node|cmd=<code class="command">virsh create domain.xml</code>}}
* You can use <code class="command">virsh</code> to manage your guest OS.
 
jdoe@griffon-37:~$ virsh list
* You can also use <code class="command">virsh</code> to manage your guest OS:
jdoe@griffon-37:~$ virsh console squeeze
** list the running virtual machines: <code class="command">virsh list</code>
** open a console on the "squeeze" virtual machine: <code class="command">virsh console squeeze</code>
 
 
{{Note|text= - You can use the script <code class="command">/usr/local/bin/random_mac</code> to generate a mac address. <br> - Use <code class="command">CTRL+AltGr+]</code> to disconnect from <code class="command">virsh console</code>
{{Note|text= - You can use the script <code class="command">/usr/local/bin/random_mac</code> to generate a mac address. <br> - Use <code class="command">CTRL+AltGr+]</code> to disconnect from <code class="command">virsh console</code>
}}
}}


Line 122: Line 141:
* Use [[KVM_contextualization | KVM contextualization]], if you want to perform some customization (like using static address) for your VMs.
* Use [[KVM_contextualization | KVM contextualization]], if you want to perform some customization (like using static address) for your VMs.
* For the deployment of an important number of guest OS, you can use the [[Subnet_reservation | subnet reservation]]. See also the [[Virtual_network_interlink | virtual network interlink]].
* For the deployment of an important number of guest OS, you can use the [[Subnet_reservation | subnet reservation]]. See also the [[Virtual_network_interlink | virtual network interlink]].
* Manage the console for all your guest OS with [[Screen]]
* Account: <code class="command">root:grid5000</code>
* Use <code class="command">tunctl</code> if you need to delete a Tun/Tap device.
jdoe@griffon-37:~$ /usr/sbin/tunctl -d tap0
* Some clusters don't have virtualization support in the BIOS. To check :
jdoe@griffon-37:~$ egrep '^flags.*(vmx|svm)' /proc/cpuinfo

Revision as of 16:51, 26 October 2012

Purpose

This page presents ways to use KVM on production image. The aim is to permit the execution of virtual machines on the nodes with a "non-deploy" reservation.

First steps

Job submission

In order to test easily the kvm environment, we will use an interactive job.

Terminal.png frontend:
oarsub -I
Note.png Note

- Some clusters don't have virtualization support in the BIOS. You can check if a node supports virtualization with the following command:

Terminal.png frontend:
egrep '^flags.*(vmx|svm)' /proc/cpuinfo

- You can request nodes with enabled virtualization support, using the "virtual" property:

Terminal.png frontend:
oarsub -I -p "virtual != 'none'"

Disk image, virtual machine

A disk image containing debian squeeze is available at the following path: /grid5000/images/KVM/squeeze-x64-base.qcow2

It can be use as a base for more advanced work. For the next steps of this tutorial, copy the disk image to /tmp on the node

Terminal.png node:
cp /grid5000/images/KVM/squeeze-x64-base.qcow2 /tmp/

Network configuration

In order to use the network with kvm, a Tun/Tap interface must be created for each virtual machines. This virtual interface will be attached to your virtual machine, and bridged on the production network. Therefore, the virtual machine will be able to get an IP from the DHCP server and access the network.

A script is available to create automatically this interface on the node: create_tap:

Terminal.png node:
sudo create_tap
  • Tun/Tap interfaces are listed by issuing the command /sbin/ifconfig.
Terminal.png node:
/sbin/ifconfig
tap0      Link encap:Ethernet  HWaddr 00:16:3e:db:c6:41
          inet6 addr: fe80::58ff:a4ff:fe97:c6a8/64 Scope:Link
          UP BROADCAST RUNNING PROMISC MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:29435 overruns:0 carrier:0
          collisions:0 txqueuelen:500
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
Note.png Note

- Create one Tun/Tap interface per guest OS.

- Use tunctl if you need to delete a Tun/Tap device.

Terminal.png node:
/usr/sbin/tunctl -d tap0

Generate a random mac address

  • Use the following script random_mac. This will generate a random mac address, starting with 00:16:3e, which will be attributed in the next step to the virtual machine.
Terminal.png node:
export MACADDR=`random_mac`
Note.png Note

The first three bytes are important (identification for the virtualized network). Do not change it.

Run the guest OS using the kvm command

Start the virtual machine with the kvm command. The following command is just an example, feel free to adapt it to your use case.

Terminal.png node:
screen kvm -m 512 -hda /tmp/squeeze-x64-base.qcow2 -net nic,macaddr=$MACADDR -net tap,ifname=tap0,script=no -nographic
Note.png Note

- tap0 is the name of our Tun/Tap interface. Adapt it with the Tun/Tap name on witch you want to attach your guest OS.

- The variable $MACADDR contains the previously generated mac address.

- The password for the root account is grid5000

  • Manage the console for all your guest OS with Screen

Or, use libvirt

Libvirt is a toolkit for managing virtualization servers. Libvirt is also an abstraction layer for different virtualization solutions, including KVM but also Xen and VMWare ESX. In our case, we use libvirt on top of KVM.

  • Create a domain file in XML, describing a virtual machine. Don't forget to adapt this example to your case (especially, replace the mac address by a randomly generated one).

eg : domain.xml

 <domain type='kvm'>
  <name>squeeze</name>
  <memory>524288</memory>
  <vcpu>1</vcpu>
  <os>
    <type arch="x86_64">hvm</type>
  </os>
  <clock sync="localtime"/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='file' device='disk'>
      <driver type='qcow2'/>
      <source file='/tmp/squeeze-x64-base.qcow2'/>
      <target dev='vda' bus='virtio'/>
     <shareable/>
    </disk>
    <interface type='ethernet'>
      <target dev='tap0'/>
      <script path='no'/>
      <mac address='00:16:3e:78:b8:04'/>
    </interface>
    <serial type='pty'>
      <source path='/dev/ttyS0'/>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <source path='/dev/ttyS0'/>
      <target port='0'/>
    </console>
  </devices>
 </domain>
  • Now, the guest OS can be started.
Terminal.png node:
virsh create domain.xml
  • You can also use virsh to manage your guest OS:
    • list the running virtual machines: virsh list
    • open a console on the "squeeze" virtual machine: virsh console squeeze


Note.png Note

- You can use the script /usr/local/bin/random_mac to generate a mac address.
- Use CTRL+AltGr+] to disconnect from virsh console

Misc notes