Virtualization in Grid'5000: Difference between revisions
(Remove references to RANDOM_MAC) |
(Draft / Merge kvm-contextualization page) |
||
Line 6: | Line 6: | ||
= Purpose = | = Purpose = | ||
This page presents how to use KVM on the production environment, with a "non-deploy" reservation. | 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. | The aim is to permit the execution of virtual machines on the nodes, along with a [[Subnet_reservation | subnet reservation]], which will give you a range of routed IP for your experiment. | ||
{{Note|text=For more network related information, see the [[Virtual_network_interlink | virtual network interlink]] page.}} | |||
In the second part, we will explain more precisely how to customize a virtual machine, using a contextualization iso file. | In the second part, we will explain more precisely how to customize a virtual machine, using a contextualization iso file. | ||
Line 107: | Line 110: | ||
{{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 $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 $ISOFILE contains the full path to the iso previously generated (for example, <code class="file">/tmp/kvm-context-10.172.0.1.iso</code>). <br /> | ||
- The variable $MACADDR contains the previously generated mac address, given by <code class="command">g5k-subnets</code>. | - The variable $MACADDR contains the previously generated mac address, given by <code class="command">g5k-subnets</code>. <br /> | ||
- 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 172: | Line 175: | ||
{{Note|text=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>}} | ||
}} | |||
= KVM contextualization explained = | |||
This part describes the basic usage of a contextualization iso file with KVM, in order to configure the virtual machines (especially the network side). | |||
The contextualization script can be easily extended for other purposes. | |||
== Mechanism == | |||
Contextualization mechanism works like the following : | |||
* Test for the presence of a CD in the CD drive of the VM | |||
* if it exists, mount the CD, test the presence of a script <code class="file">post-install</code>, and run it as root | |||
* if it does not exist, use <code class="cmd>dhcp</code> on the first network interface. | |||
=== Installation === | |||
* Contextualization script is executed during the boot sequence. | |||
This script can be placed in <code class="file">/etc/rc.local</code>, at the end of the file (before the exit 0 if any). | |||
<pre class="brush: bash"> | |||
# KVM contextualization script | |||
/usr/local/bin/init | |||
</pre> | |||
* Here is an example of the content for that contextualization script : | |||
<pre class="brush: bash"> | |||
#!/bin/bash | |||
DEVICE= | |||
[ -b /dev/hdb ] && DEVICE=/dev/hdb | |||
[ -b /dev/sdb ] && DEVICE=/dev/sdb | |||
[ -b /dev/vdb ] && DEVICE=/dev/vdb | |||
[ -b /dev/xvdb ] && DEVICE=/dev/xvdb | |||
[ -b /dev/sr0 ] && DEVICE=/dev/sr0 | |||
if [ -b "$DEVICE" ];then | |||
/bin/mount -t iso9660 $DEVICE /mnt 2> /dev/null | |||
if [ -f /mnt/post-install ]; then | |||
bash /mnt/post-install | |||
fi | |||
umount /mnt 2> /dev/null | |||
else | |||
ifup eth0 | |||
fi | |||
exit 0 | |||
</pre> | |||
=== Generate your iso file for the contextualization === | |||
This part explain how to generate the contextualization iso file manually. | |||
This step can be automated with the script <code class="command">g5k-vm</code>. | |||
{{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>}} | |||
* A basic example of contextualization iso file is available at <code class="file">/grid5000/images/KVM/kvm-context.tgz</code> on each site. | |||
** That example configure the network interface using static IP address, hostname, DNS for the VMs. | |||
** You can customize that iso file for your experiments. | |||
* Use <code class="command">genisoimage</code> tool to generate the iso file for the contextualisation. | |||
{{Term|location=node|cmd= genisoimage -r -o kvm-context.iso kvm-context/</code>}} | |||
== Basic usage of contextualization == | |||
=== Use the [[Subnet_reservation | subnet reservation]] === | |||
Reserve a node and a subnet with the following command: | |||
{{Term|location=frontend|cmd=<code class="command">oarsub -l slash_22=1+nodes=1 -I</code>}} | |||
=== Create a contextualization iso file === | |||
==== Retrieve the file kvm-context.tgz ==== | |||
Uncompress the file <code class="file">/grid5000/images/KVM/kvm-context.tgz</code> in your home directory. This tarball contains an example of contextualization script. | |||
{{Term|location=node|cmd=<code class="command">tar xzvf /grid5000/images/KVM/kvm-context.tgz</code>}} | |||
==== Adapt the network configuration in the contextualization script ==== | |||
* The contextualization script will apply the network settings provided by you. | |||
* The command <code class="command">g5k-subnets</code> will give you all the needed network information related to your reservation. | |||
{{Term|location=node|cmd=<code class="command">g5k-subnets -a</code>}} | |||
* Choose an IP and a mac address in your range: | |||
{{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> | |||
* Following the information displayed, you should adapt the file <code class="file">./kvm-context/common/network</code>. | |||
This file is used by the script <code class="file">./kvm-context/distributions/debian/00_network</code> in order to configure the network interface for your VM. | |||
<pre class="brush: bash"> | |||
IPADDR=10.172.0.1 | |||
MACADDR=00:16:3e:ac:00:01 | |||
GATEWAY=10.175.255.254 | |||
NETWORK=10.172.0.0 | |||
BROADCAST=10.175.255.255 | |||
NETMASK=255.252.0.0 | |||
NAMESERVER=172.16.191.101 | |||
DOMAIN=luxembourg.grid5000.fr | |||
SEARCH=luxembourg.grid5000.fr | |||
</pre> | |||
==== Generate the iso file ==== | |||
Once you have prepared the content of the iso file for the contextualization, you can generate it in <code class="file">/tmp</code> on the node. | |||
{{Term|location=node|cmd=<code class="command">genisoimage -r -o /tmp/kvm-context.iso kvm-context/</code>}} | |||
=== Retrieve a standard virtual machine image === | |||
* Copy the qcow2 image into the <code class="file">/tmp</code> directory | |||
{{Term|location=node|cmd=<code class="command">cp /grid5000/images/KVM/squeeze-x64-base.qcow2 /tmp/squeeze1.qcow2</code>}} | |||
=== Create a tap device === | |||
* We create tap devices using <code class="command">create_tap</code> command. | |||
In this example, we create 10 tap devices: | |||
{{Term|location=node|cmd=sudo create_tap</code>}} | |||
=== Create the domain.xml file for each guest VMs === | |||
* You can define a xml domain file for each guest VM. | |||
eg : <code class="file">domain1.xml</code> | |||
<pre class="brush: bash"> | |||
<domain type='kvm'> | |||
<name>squeeze1</name> | |||
<memory>362144</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/squeeze1.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> | |||
</pre> | |||
{{Note|text=Ensure you have changed the VM name, tap device and mac address.}} | |||
You can repeat the previous steps 10 times to run 10 more VMs, and list the running machines with the following command: | |||
= | {{Term|location=node|cmd=<code class="command">virsh list</code>}} | ||
Revision as of 20:05, 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, which will give you a range of routed IP for your experiment.
![]() |
Note |
---|---|
For more network related information, see the virtual network interlink page. |
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.
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:
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
:
- Tun/Tap interfaces are listed by issuing the command
/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 |
---|---|
- Create one Tun/Tap interface per guest OS. - Use |
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.
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.
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 ...
Generate this iso file with this 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.
The kvm process is launched in a screen
session, if you are not familiar with screen, read its documentation.
![]() |
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 |
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.
- 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
- list the running virtual machines:
KVM contextualization explained
This part describes the basic usage of a contextualization iso file with KVM, in order to configure the virtual machines (especially the network side). The contextualization script can be easily extended for other purposes.
Mechanism
Contextualization mechanism works like the following :
- Test for the presence of a CD in the CD drive of the VM
- if it exists, mount the CD, test the presence of a script
post-install
, and run it as root - if it does not exist, use
dhcp
on the first network interface.
Installation
- Contextualization script is executed during the boot sequence.
This script can be placed in /etc/rc.local
, at the end of the file (before the exit 0 if any).
# KVM contextualization script /usr/local/bin/init
- Here is an example of the content for that contextualization script :
#!/bin/bash DEVICE= [ -b /dev/hdb ] && DEVICE=/dev/hdb [ -b /dev/sdb ] && DEVICE=/dev/sdb [ -b /dev/vdb ] && DEVICE=/dev/vdb [ -b /dev/xvdb ] && DEVICE=/dev/xvdb [ -b /dev/sr0 ] && DEVICE=/dev/sr0 if [ -b "$DEVICE" ];then /bin/mount -t iso9660 $DEVICE /mnt 2> /dev/null if [ -f /mnt/post-install ]; then bash /mnt/post-install fi umount /mnt 2> /dev/null else ifup eth0 fi exit 0
Generate your iso file for the contextualization
This part explain how to generate the contextualization iso file manually.
This step can be automated with the script g5k-vm
.
- A basic example of contextualization iso file is available at
/grid5000/images/KVM/kvm-context.tgz
on each site.- That example configure the network interface using static IP address, hostname, DNS for the VMs.
- You can customize that iso file for your experiments.
- Use
genisoimage
tool to generate the iso file for the contextualisation.
Basic usage of contextualization
Use the subnet reservation
Reserve a node and a subnet with the following command:
Create a contextualization iso file
Retrieve the file kvm-context.tgz
Uncompress the file /grid5000/images/KVM/kvm-context.tgz
in your home directory. This tarball contains an example of contextualization script.
Adapt the network configuration in the contextualization script
- The contextualization script will apply the network settings provided by you.
- The command
g5k-subnets
will give you all the needed network information related to your reservation.
- Choose an IP and a mac address in your range:
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 ...
- Following the information displayed, you should adapt the file
./kvm-context/common/network
.
This file is used by the script ./kvm-context/distributions/debian/00_network
in order to configure the network interface for your VM.
IPADDR=10.172.0.1 MACADDR=00:16:3e:ac:00:01 GATEWAY=10.175.255.254 NETWORK=10.172.0.0 BROADCAST=10.175.255.255 NETMASK=255.252.0.0 NAMESERVER=172.16.191.101 DOMAIN=luxembourg.grid5000.fr SEARCH=luxembourg.grid5000.fr
Generate the iso file
Once you have prepared the content of the iso file for the contextualization, you can generate it in /tmp
on the node.
Retrieve a standard virtual machine image
- Copy the qcow2 image into the
/tmp
directory
Create a tap device
- We create tap devices using
create_tap
command.
In this example, we create 10 tap devices:
Create the domain.xml file for each guest VMs
- You can define a xml domain file for each guest VM.
eg : domain1.xml
<domain type='kvm'> <name>squeeze1</name> <memory>362144</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/squeeze1.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>
You can repeat the previous steps 10 times to run 10 more VMs, and list the running machines with the following command: