Execute scripts before VM creation

Hi One Community,

I just finished my installation of OpenNebula 4.12 with Xen host.

My xen servers and my virtual machines are fully diskless with a NFS share and I can create same VM with open nebula interface. But to create VM I need execute my own script to generate Xen config file and new ZFS dataset for the VM.

So my question is : Is there any solution to execute some script before the creation of virtual machine on Open Nebula.

I saw there is Init Scripts in the context menu, but I think those scripts are not usefull for my purpose, I want to create my new ZFS dataset for my new VM, otherwise my virtual machines will not boot :frowning:

Hope I was enough explicit on my explanations…

Mura

Hi,

There are hooks in OpenNebula, but it sounds like it would be better for you to customize the deploy script of your vmm drivers.
See here for more info:
http://docs.opennebula.org/5.0/integration/infrastructure_integration/devel-vmm.html

Hi,

Thank you for the answer, I’ m testing this and give you some feedbacks after.

Mura

Hi,

So I did some tests with the scripts in the vmm driver, they almost did what I want :slight_smile: but is there possible to use some variables on it.
Ex : when I instantiate my VM, I would use the vm_name in the deploy script to create my zfs datastore associate to this vm ?

Mura,

Hi Murasakiiru,

can you also post your modifications and scripts, i would like to use something simular.

Thanks.

Martin

Hi Martin,

Today, I just add some bash ‘actions’ in the deploy script (/var/tmp/one/vmm/xen4/deploy) to create zfs dataset before deploy my Virtual Machine.
If I could use variables from web interface in this bash script, it will be awesome :slight_smile:

Mura,

HI,

AFAIK the deploy scripts are called on the VM host. But I think you can track which TM_MAD script is executed before that and dump-and-copy the VM config XML to the desired XEN host. Then parse the VM XML in the vmm/deploy.

Kind Regards,
Anton Todorov

Thanks for your answer Anton, but I ‘think’ it does not use TM_MAD script as I don’t use drive directly : I use a NFSROOT but OpenNebula always create an empty drive on my VM, so I’m not really sure.

Here a example of my template :

`
TEMPLATE 0 INFORMATION
ID : 0
NAME : First VM
USER : oneadmin
GROUP : oneadmin
REGISTER TIME : 09/02 12:39:41

PERMISSIONS
OWNER : um-
GROUP : —
OTHER : —

TEMPLATE CONTENTS
CONTEXT=[
NETWORK=“YES”,
SERVICE="$SERVICE",
SSH_PUBLIC_KEY="$USER[SSH_PUBLIC_KEY]",
VM_NAME="$VM_NAME" ]
CPU=“1"
HYPERVISOR=“xen"
LOGO=“images/logos/debian.png"
MEMORY=“512"
NIC=[
NETWORK=“Bridge Stockage”,
NETWORK_UNAME=“oneadmin” ]
NIC=[
NETWORK=“Bridge IT”,
NETWORK_UNAME=“oneadmin” ]
OS=[
ARCH=“x86_64”,
INITRD=”/boot/initrd.img-3.16.0-4-amd64-vms”,
KERNEL=”/boot/vmlinuz-3.16.0-4-amd64-vms”,
KERNEL_CMD=“ip=10.42.0.34:10.42.0.2:10.42.0.254:255.255.0.0:template01.mydomain:eth0:static root=/dev/nfs nfsroot=10.42.0.2:/shares/vms_nfsroot/it/template01,rw” ]
USER_INPUTS=[
SERVICE=“M|text|Nom du service associé”,
VM_NAME=“M|text|Nom de la machine virtuelle” ]
VCPU=“1”
`

If somebody can explain me how to do this : you can track which TM_MAD script is executed before that and dump-and-copy the VM config XML to the desired XEN host. Then parse the VM XML in the vmm/deploy. and why is this useful in my case.

Today I try to scp the context.sh file from opennebula to my Xen Host to retrieve User Inputs variable, but I think this is really not the good way ! :slight_smile:

Mura,

1 Like

Hi,

As someone that suggested above here is my answer:

The simple solution to have access to the VM info in OpenNebula you must query needed info locally on the front-end. In the common configuration there are disks managed and provided by OpenNebula. In this case there are TM_MAD scripts executed on the front-end before deploying the VM. You said that you have no such disks - everything is over NFS. So I agree that this is not a solution in your case.

IMO @cmartin already pointed you to the proper solution.

Following the mentioned documentation you must configure the one_vmm_exec script in your VM_MAD configuration for xen to run local deploy via the -l argument. Something like:

VM_MAD = [
...
argumets = " ... -l deploy=local_deploy_script"
...
]

Then in your local_deploy_script you will have access to the entire VM (onevm show $2) info but you must take care to do all needed remote commands to the ZFS and XEN host to deploy the VM.

I hope this helps.

Kind Regards,
Anton Todorov

Thanks Anton for the infos,

So I test to create my new script very basic to test :

#!/bin/bash echo "$NAME $ID" > /tmp/variables

I add in my oned.conf :
VM_MAD = [ name = "xen", executable = "one_vmm_exec", arguments = "-t 15 -r 0 xen4 -l deploy=createvm.sh", default = "vmm_exec/vmm_exec_xen4.conf", type = "xen" ]

So when I launch my vm, it returns me nothing… :confused:

You said yesterday I can retrieve this variable : (onevm show $mylastcreatedvm)

`
VIRTUAL MACHINE 22 INFORMATION
ID : 22
NAME : hello
USER : oneadmin
GROUP : oneadmin
STATE : ACTIVE
LCM_STATE : UNKNOWN
RESCHED : No
HOST : 10.10.10.12
CLUSTER ID : 101
CLUSTER : cluster
START TIME : 09/08 14:18:31
END TIME : -
DEPLOY ID : -

VIRTUAL MACHINE MONITORING

PERMISSIONS
OWNER : um-
GROUP : —
OTHER : —

VM DISKS
ID DATASTORE TARGET IMAGE SIZE TYPE SAVE
0 - hda CONTEXT -/- - -

VM NICS
ID NETWORK VLAN BRIDGE IP MAC
0 bridge stockage no xenbr1.42 10.42.0.200 02:00:0a:2a:00:c8
1 bridge it no xenbr1.10 10.10.10.100 02:00:0a:0a:0a:64

SECURITY

NIC_ID NETWORK SECURITY_GROUPS
0 bridge stockage 0
1 bridge it 0

SECURITY GROUP TYPE PROTOCOL NETWORK RANGE
ID NAME VNET START SIZE
0 default OUTBOUND ALL
0 default INBOUND ALL

VIRTUAL MACHINE HISTORY
SEQ HOST ACTION DS START TIME PROLOG
0 10.10.10.12 none 0 09/08 14:18:53 0d 00h12m 0h00m00s

USER TEMPLATE
HYPERVISOR="xen"
LOGO=“images/logos/debian.png"
SCHED_DS_RANK=”-FREE_MB"
SCHED_RANK="RUNNING_VMS"
SCHED_REQUIREMENTS="ID="2""
SERVICE="testsetset"
USER_INPUTS=[
SERVICE=“M|text|service” ]

VIRTUAL MACHINE TEMPLATE
AUTOMATIC_REQUIREMENTS=“CLUSTER_ID = 101 & !(PUBLIC_CLOUD = YES)“
CONTEXT=[
DISK_ID=“0”,
ETH0_IP=“10.42.0.200”,
ETH0_MAC=“02:00:0a:2a:00:c8”,
ETH1_IP=“10.10.10.100”,
ETH1_MAC=“02:00:0a:0a:0a:64”,
NETWORK=“YES”,
SERVICE=“testsetset”,
TARGET=“hda” ]
CPU=“1"
MEMORY=“512"
OS=[
INITRD=”/boot/initrd.img-3.16.0-4-amd64-vms”,
KERNEL=”/boot/vmlinuz-3.16.0-4-amd64-vms”,
KERNEL_CMD=“ip=10.42.0.32:10.42.0.2:10.42.0.254:255.255.0.0:toto.mydomain:eth0:static root=/dev/nfs nfsroot=10.42.0.2:/shares/vms_nfsroot/it/toto,rw” ]
TEMPLATE_ID="1"
VCPU="1"
VMID=“22”

`

I don’t understand why the script return nothing … :frowning:

Mura,

Hi,

Come back after long time, I try another way.
Finally, seems to work fine with the HOOKS as cmartin mention on beginning of the topic, so I edit the oned.conf like this :

VM_HOOK = [
    name        = "create_zfs_volume",
    on          = "CREATE",
    command     = "/usr/lib/one/mads/zfs_create.sh",
    arguments   = "$ID $TEMPLATE",
    remote      = "NO" ]

And the zfs_create.sh :

#!/bin/sh
contexte=`echo $2 | /usr/bin/base64 -d`
nom_vm=`echo $contexte |/usr/bin/awk -F "<NAME>" '{ print $2 }' |/usr/bin/awk -F "<" '{ print $1 }'`
service=`echo $contexte |/usr/bin/awk -F "<SERVICE>" '{ print $2 }' |/usr/bin/awk -F "[" '{ print $3 }' | awk -F "]" '{ print $1 }'`
zfs=`echo $contexte |/usr/bin/awk -F "<QUOTA_ZFS>" '{ print $2 }' |/usr/bin/awk -F "[" '{ print $3 }' | awk -F "]" '{ print $1 }'`
echo $contexte > /tmp/template
echo "le service est $service" >> /tmp/${nom_vm}
echo "le quota zfs est $zfs" >> /tmp/${nom_vm}
echo $1 >> /tmp/${nom_vm}
ssh root@zfs-stockage "zfs create -o mountpoint=/mnt/${service}/${nom_vm} zroot/${service}_${nom_vm}"
ssh root@zfs-stockage "zfs set quota=${zfs}G zroot/${service}_${nom_vm}"
ssh root@zfs-stockage "rsync -arpogvlH --exclude=.zfs /mnt/master-jessie/ /mnt/${service}/${nom_vm}"

For now it seems to work pretty good to create our zfs share for our vms. Now we just need to adapt our kernel command to launch the virtual machines, but I think the hard part is done.

Thank you for your help :slight_smile:

I want install cmatrix package in VM when it is instantiated from template. So I put this command in sh file. How to do this in specific VM. I have put sh file in mads folder. What are parameters for arguments in VM_HOOK. How to specify which template to run this sh file.??

#!/bin/sh
sudo apt-get install cmatrix