Xen : Gestion des ressources mémoire et cpu des machines virtuels

Voici un petit script tout bête pour modifier les allocations mémoire et cpu des machines virtuels Xen. Ce script répond au besoin simple d’avoir de la puissance à disposition ponctuellement pour une machine en particulier. Pour ceci il faut nécessairement augmenter le volume mémoire et le nombre de vcpu pour la machine virtuel concernée, or pour ceci diminuer pour toute les autres machines est parfois nécessaire afin de dégager des ressources. Dans un premier temps on diminue les ressources de toutes les machines avant d’augmenter de la puissance rendu disponible uniquement celle en ayant ponctuellement besoin. Le retour à une puissance égale ce faisant avec ce même script.

<span style="color: #808080; font-style: italic;">#!/bin/bash</span> <span style="color: #808080; font-style: italic;">#</span> &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">test</span> -z <span style="color: #ff0000;">&quot;$1&quot;</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #007800;">syntax=</span>fausse; <span style="color: #000000; font-weight: bold;">fi</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">test</span> -z <span style="color: #ff0000;">&quot;$2&quot;</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #007800;">syntax=</span>fausse; <span style="color: #000000; font-weight: bold;">fi</span> &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$syntax&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;fausse&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>  <span style="color: #000000; font-weight: bold;">then</span>    <span style="color: #000000; font-weight: bold;">for</span> I <span style="color: #000000; font-weight: bold;">in</span> `xm list <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $2}'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span>  <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span><span style="color: #000000;">-9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>`;      <span style="color: #000000; font-weight: bold;">do</span> xm vcpu-<span style="color: #000000; font-weight: bold;">set</span> <span style="color: #007800;">$I</span> $<span style="color: #000000;">1</span>;      <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">2</span>;      xm mem-<span style="color: #000000; font-weight: bold;">set</span> <span style="color: #007800;">$I</span> $<span style="color: #000000;">2</span>; <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">2</span>;      <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;VM $I set with $1 vcpu and $2 Ram&quot;</span>;     <span style="color: #000000; font-weight: bold;">done</span>  <span style="color: #000000; font-weight: bold;">else</span>    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Syntax: $0 <span style="color: #000099; font-weight: bold;">\&quot;</span>Nombre de vcspu<span style="color: #000099; font-weight: bold;">\&quot;</span> <span style="color: #000099; font-weight: bold;">\&quot;</span>Volume de la Ram en Mo<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Exemple: $0 1 512&quot;</span> <span style="color: #000000; font-weight: bold;">fi</span>

La temporisation par la commande sleep 2 est nécessaire par éviter d’affoler le backend xen

Laisser un commentaire