Welcome Guest. Please Login or Register  


You are here: Index > Virtualizor - Virtual Server Control Panel > General Support > Topic : KVM AfterCreate()



Threaded Mode | Print  

 KVM AfterCreate(), Edit VM file before start (7 Replies, Read 3305 times)
netheld
Group: Member
Post Group: Newbie
Posts: 4
Status:
Hi there guys,

So I am trying to deliver a special configuration after a VM creating before it is started.

I have been searching and I really cant find any way to edit the VM.

So far, I have made a PHP hook where I get all data needed to change and boot the VPS in order the client can access it right away.

I need to edit the following file: /etc/rc.conf

And add the following code to the end:

Code
ifconfig_em0="inet IP.FAIL.OVER netmask 255.255.255.255 broadcast IP.FAIL.OVER"
static_routes="net1 net2"
route_net1="-net GATEWAY_VM/32 IP.FAIL.OVER"
route_net2="default GATEWAY_VM"


Can anybody help me out please?
IP: --   

KVM AfterCreate()
asim_shaikh
Group: Member
Post Group: Elite Member
Posts: 381
Status:
Quote
Hi there guys,



So I am trying to deliver a special configuration after a VM creating before it is started.



I have been searching and I really cant find any way to edit the VM.



So far, I have made a PHP hook where I get all data needed to change
and boot the VPS in order the client can access it right away.

I need to edit the following file: /etc/rc.conf

And add the following code to the end:

Codeifconfig_em0="inet IP.FAIL.OVER netmask 255.255.255.255 broadcast IP.FAIL.OVER"

static_routes="net1 net2"

route_net1="-net GATEWAY_VM/32 IP.FAIL.OVER"

route_net2="default GATEWAY_VM"Can anybody help me out please?
       


Hi,

You can use recipe to append your code to /etc/rc.conf .

In Virtualizor 2.8.7 we have added an option to select the recipe to be executed after the VPS creation while creating the VPS.

The recipe will be executed when the VPS is booted for the first time.


-----------------------
Regards,
Virtualizor Team
http://www.virtualizor.com
IP: --   

KVM AfterCreate()
netheld
Group: Member
Post Group: Newbie
Posts: 4
Status:
Ok I did understand that, but how can I get the info from the VPS and pass it to the Receipt?

Thanks in advance.
IP: --   

KVM AfterCreate()
asim_shaikh
Group: Member
Post Group: Elite Member
Posts: 381
Status:
Quote
Ok I did understand that, but how can I get the info from the VPS and pass it to the Receipt?

Thanks in advance.
       


Hi,

The recipe will be execute inside the VPS itself so you can search for the appropriate bash commands to get the data you require for your script.


-----------------------
Regards,
Virtualizor Team
http://www.virtualizor.com
IP: --   

KVM AfterCreate()
netheld
Group: Member
Post Group: Newbie
Posts: 4
Status:
Quote From : asim_shaikh September 12, 2016, 5:47 am
Quote
Ok I did understand that, but how can I get the info from the VPS and pass it to the Receipt?

Thanks in advance.
       


Hi,

The recipe will be execute inside the VPS itself so you can search for the appropriate bash commands to get the data you require for your script.


You didnt answer my question sir...

I need to GET the VPS details like IP assign in order to change the network. How can I pass that info to the RECEIPT?
IP: --   

KVM AfterCreate()
asim_shaikh
Group: Member
Post Group: Elite Member
Posts: 381
Status:
Quote
You didnt answer my question sir...

I need to GET the VPS details like IP assign in order to change the network. How can I pass that info to the RECEIPT?
       


Hi,

As the recipe is running inside the VPS and it is static script added in virtualizor the resources (like ip, hostname) assigned to the VPS will be dyncamic and cannot be added to the recipe, so i suggested you to add shell script commands to get these details, for example if you want the IP assigned to the VPS you can use the following shell script:

ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/'


-----------------------
Regards,
Virtualizor Team
http://www.virtualizor.com
IP: --   

KVM AfterCreate()
netheld
Group: Member
Post Group: Newbie
Posts: 4
Status:
Quote From : asim_shaikh September 12, 2016, 12:44 pm
Quote
You didnt answer my question sir...

I need to GET the VPS details like IP assign in order to change the network. How can I pass that info to the RECEIPT?
       


Hi,

As the recipe is running inside the VPS and it is static script added in virtualizor the resources (like ip, hostname) assigned to the VPS will be dyncamic and cannot be added to the recipe, so i suggested you to add shell script commands to get these details, for example if you want the IP assigned to the VPS you can use the following shell script:

ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/'


Hi again,

I don't think you're following me.

I dont want to assign an IP because Virtualizor does that.

I want to run the receipt in order to change the network of the VPS.

My FreeBSD template DOESN'T AUTO CONFIGURE, so I need to insert in the NEW VPS /etc/rc.conf file some lines so the NETWORK Works.

I understood that Receipt will run inside the VPS the bash script BUT that doesnt work unless i can pass to the SCRIPT the IP ASSIGN TO THE VPS by Virtualizor.

I can't be much more explicit sir..
IP: --   

KVM AfterCreate()
asim_shaikh
Group: Member
Post Group: Elite Member
Posts: 381
Status:
Quote
Hi again,

I don't think you're following me.

I dont want to assign an IP because Virtualizor does that.

I want to run the receipt in order to change the network of the VPS.

My FreeBSD template DOESN'T AUTO CONFIGURE, so I need to insert in
the NEW VPS /etc/rc.conf file some lines so the NETWORK Works.

I understood that Receipt will run inside the VPS the bash script
BUT that doesnt work unless i can pass to the SCRIPT the IP ASSIGN TO
THE VPS by Virtualizor.

I can't be much more explicit sir..
   
   


Hi,

Ok, in that case no information will be available inside the VPS.
In such what you can do is you can mount the VPS and alter the /etc/rc.conf file.
For that firstly you will need to shutdown the VPS, then mount the VPS.

To get the gateway of the IP assigned to the VPS you will first need to get the ippool id to fetch the ippool details from the ippool table.

You can get the IPpool id from the ips table.

You can use the following code to do your task.
Let me know if you face any issue.

PHP Code

 include_once(dirname(__DIR__)'/main/funtions.php');
    
    function 
__after_createvps($vps){

        
$vid $vps['vpsid'];

       
$res mysql_query("SELECT ippid from ips where vpsid = '.$vid.' AND primary = 1");

      
$row mysql_fetch_assoc($res);

      
$res mysql_query("SELECT * from ippool where ippid = '.$row['ippid'].'");


      
$row mysql_fetch_assoc($res);

      
$gateway $row['gateway '];
        
        
exec('virsh destroy '.$vps['vps_name']);
        
        
sleep(5);       
        
        
// Mount it
        
$mount mount($vid);
        
        
// Did it mount ?
        
if(!is_array($mount)){
            return 
false;
        }
        
        
$code '
ifconfig_em0="inet IP.FAIL.OVER netmask 255.255.255.255 broadcast IP.FAIL.OVER"
static_routes="net1 net2"
route_net1="-net '
.$gateway.'/32 IP.FAIL.OVER"
route_net2="default "'
.$gateway;
        
        
file_put_contents($mount['mount_point'].'/etc/rc.conf'$codeFILE_APPEND);
                
        
// Unmount it
        
$umount umount($mount);
    } 





Edited by asim_shaikh : September 14, 2016, 7:01 am

-----------------------
Regards,
Virtualizor Team
http://www.virtualizor.com
IP: --   

« Previous    Next »

Threaded Mode | Print  



Jump To :


Users viewing this topic
1 guests, 0 users.


All times are GMT. The time now is June 20, 2024, 9:36 am.

  Powered By AEF 1.0.8 © 2007-2008 Electron Inc.Queries: 10  |  Page Created In:0.370