Virtualization in Grid'5000: Difference between revisions

From Grid5000
Jump to navigation Jump to search
(Remove references to RANDOM_MAC)
Line 5: Line 5:


= Purpose =
= Purpose =
This page presents ways to use KVM on production image. The aim is to permit the
This page presents how to use KVM on the production environment, with a "non-deploy" reservation.  
execution of virtual machines on the nodes with a "non-deploy" reservation.
The aim is to permit the execution of virtual machines on the nodes, along with a subnet reservation.
In the second part, we will explain more precisely how to customize a virtual machine, using a contextualization iso file.


= First steps =
= Quick start =
 
In this part, we will create a virtual machine in a few steps, and ssh to it.


=== Job submission===
=== Job submission===


In order to test easily the kvm environment, we will use an interactive job.
In order to test easily the kvm environment, we use an interactive job, and we reserve one subnet and one node.


{{Term|location=frontend|cmd=<code class="command">oarsub -I</code>}}
{{Term|location=frontend|cmd=<code class="command">oarsub -l slash_22=1+nodes=1 -I</code>}}




Line 21: Line 24:
  {{Term|location=node|cmd=<code class="command">egrep '^flags.*(vmx&#124;svm)' /proc/cpuinfo</code>}}
  {{Term|location=node|cmd=<code class="command">egrep '^flags.*(vmx&#124;svm)' /proc/cpuinfo</code>}}
- You can request nodes with enabled virtualization support, using the "virtual" property:
- 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>}}
  {{Term|location=frontend|cmd=<code class="command">oarsub -I -l "slash_22=1+{virtual!='none'}/nodes=1"</code>}}
}}
}}


Line 29: Line 32:
<code class="command">/grid5000/images/KVM/squeeze-x64-base.qcow2</code>
<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 used 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:


{{Term|location=node|cmd=<code class="command">cp /grid5000/images/KVM/squeeze-x64-base.qcow2 /tmp/</code>}}
{{Term|location=node|cmd=<code class="command">cp /grid5000/images/KVM/squeeze-x64-base.qcow2 /tmp/</code>}}
Line 61: Line 64:
}}
}}


=== Generate a random mac address ===
=== Generate the contextualization iso file ===


* 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.
This file contains a script which will set the network configration of your VM.
First, choose an IP in the range you have reserved. The command <code class=command>g5k-subnets</code> will give you more information.
 
{{Term|location=node|cmd=<code class="command">g5k-subnets -a</code>}}
<pre class="brush: bash">
10.172.0.0/22  10.175.255.255  255.252.0.0    10.175.255.254  10.172.0.0      dns.luxembourg.grid5000.fr      172.16.191.101
</pre>


{{Term|location=node|cmd=<code class="command">export MACADDR=`random_mac`</code>}}
You can get the list of available IP, and an associated unique mac address with the following command.


{{Note|text= The first three bytes are important (identification for the virtualized network). Do not change it.}}
{{Term|location=node|cmd=<code class="command">g5k-subnets -im</code>}}
<pre class="brush: bash">
10.172.0.1      00:16:3E:AC:00:01
10.172.0.2      00:16:3E:AC:00:02
10.172.0.3      00:16:3E:AC:00:03
10.172.0.4      00:16:3E:AC:00:04
10.172.0.5      00:16:3E:AC:00:05
10.172.0.6      00:16:3E:AC:00:06
10.172.0.7      00:16:3E:AC:00:07
10.172.0.8      00:16:3E:AC:00:08
10.172.0.9      00:16:3E:AC:00:09
10.172.0.10    00:16:3E:AC:00:0A
...
</pre>
 
{{Note|text=- The mac address is a translation of the IP. Do not change it. <br />
- The first three bytes of the mac address <code class="command">00:16:3e</code> are important. If you omit to use the contextualization iso file for the network configuration, you will be able to receive an IP from the DHCP server only if you use this prefix.
}}
 
 
Generate this iso file with this command:
{{Term|location=node|cmd=<code class="command">/grid5000/images/KVM/g5k-vm --ip 10.172.0.1 --iso /tmp/kvm-context-10.172.0.1.iso</code>}}


=== Run the guest OS using the kvm command ===
=== 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.
Start the virtual machine with the kvm command. The following command is just an example, feel free to adapt it to your use case.
The kvm process in launched a <code class="command">screen</code> session, if you are not familiar with screen, read its [[Screen|documentation]].
The kvm process is launched in a <code class="command">screen</code> session, if you are not familiar with screen, read its [[Screen|documentation]].


{{Term|location=node|cmd=<code class="command">screen kvm -m 512 -hda /tmp/squeeze-x64-base.qcow2 -net nic,model=virtio,macaddr='''$MACADDR''' -net tap,ifname='''tap0''',script=no -nographic</code>}}
{{Term|location=node|cmd=<code class="command">screen kvm -m 512 -hda /tmp/squeeze-x64-base.qcow2 -cdrom '''$ISOFILE''' -net nic,model=virtio,macaddr='''$MACADDR''' -net tap,ifname='''tap0''',script=no -nographic</code>}}


{{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=- <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 />
- The variable $MACADDR contains the previously generated mac address.
- The variable $ISOFILE contains the full path to the iso previously generated (for example, <code class="file">/tmp/kvm-context-10.172.0.1.iso</code>).
- The variable $MACADDR contains the previously generated mac address, given by <code class="command">g5k-subnets</code>.
- The password for the <code class="command">root</code> account is <code class="command">grid5000</code>
- The password for the <code class="command">root</code> account is <code class="command">grid5000</code>
}}
}}
Line 109: Line 140:
       <target dev='vda' bus='virtio'/>
       <target dev='vda' bus='virtio'/>
     <shareable/>
     <shareable/>
    </disk>
    <disk type='file' device='cdrom'>
      <source file='/tmp/kvm-context.iso'/>
      <target dev='vdb' bus='virtio'/>
      <readonly/>
     </disk>
     </disk>
     <interface type='ethernet'>
     <interface type='ethernet'>
Line 114: Line 150:
       <script path='no'/>
       <script path='no'/>
       <model type='virtio'/>
       <model type='virtio'/>
       <mac address='00:16:3e:78:b8:04'/>
       <mac address='00:16:3e:ac:00:01'/>
     </interface>
     </interface>
     <serial type='pty'>
     <serial type='pty'>
Line 136: Line 172:




{{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=Use <code class="command">CTRL+AltGr+]</code> to disconnect from <code class="command">virsh console</code>
}}
}}



Revision as of 19:27, 18 November 2012

Purpose

This page presents how to use KVM on the production environment, with a "non-deploy" reservation. The aim is to permit the execution of virtual machines on the nodes, along with a subnet reservation. In the second part, we will explain more precisely how to customize a virtual machine, using a contextualization iso file.

Quick start

In this part, we will create a virtual machine in a few steps, and ssh to it.

Job submission

In order to test easily the kvm environment, we use an interactive job, and we reserve one subnet and one node.

Terminal.png frontend:
oarsub -l slash_22=1+nodes=1 -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 node:
egrep '^flags.*(vmx|svm)' /proc/cpuinfo

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

Terminal.png frontend:
oarsub -I -l "slash_22=1+{virtual!='none'}/nodes=1"

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 used 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 the contextualization iso file

This file contains a script which will set the network configration of your VM. First, choose an IP in the range you have reserved. The command g5k-subnets will give you more information.

Terminal.png node:
g5k-subnets -a
10.172.0.0/22   10.175.255.255  255.252.0.0     10.175.255.254  10.172.0.0      dns.luxembourg.grid5000.fr      172.16.191.101

You can get the list of available IP, and an associated unique mac address with the following command.

Terminal.png node:
g5k-subnets -im
10.172.0.1      00:16:3E:AC:00:01
10.172.0.2      00:16:3E:AC:00:02
10.172.0.3      00:16:3E:AC:00:03
10.172.0.4      00:16:3E:AC:00:04
10.172.0.5      00:16:3E:AC:00:05
10.172.0.6      00:16:3E:AC:00:06
10.172.0.7      00:16:3E:AC:00:07
10.172.0.8      00:16:3E:AC:00:08
10.172.0.9      00:16:3E:AC:00:09
10.172.0.10     00:16:3E:AC:00:0A
...
Note.png Note

- The mac address is a translation of the IP. Do not change it.
- The first three bytes of the mac address 00:16:3e are important. If you omit to use the contextualization iso file for the network configuration, you will be able to receive an IP from the DHCP server only if you use this prefix.


Generate this iso file with this command:

Terminal.png node:
/grid5000/images/KVM/g5k-vm --ip 10.172.0.1 --iso /tmp/kvm-context-10.172.0.1.iso

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. The kvm process is launched in a screen session, if you are not familiar with screen, read its documentation.

Terminal.png node:
screen kvm -m 512 -hda /tmp/squeeze-x64-base.qcow2 -cdrom $ISOFILE -net nic,model=virtio,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 $ISOFILE contains the full path to the iso previously generated (for example, /tmp/kvm-context-10.172.0.1.iso). - The variable $MACADDR contains the previously generated mac address, given by g5k-subnets.

- The password for the root account is grid5000

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>
    <disk type='file' device='cdrom'>
      <source file='/tmp/kvm-context.iso'/>
      <target dev='vdb' bus='virtio'/>
      <readonly/>
    </disk>
    <interface type='ethernet'>
      <target dev='tap0'/>
      <script path='no'/>
      <model type='virtio'/>
      <mac address='00:16:3e:ac:00:01'/>
    </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

Use CTRL+AltGr+] to disconnect from virsh console

Misc notes