Xcp/xenserver automate the creation and initialization of virtual machines

Source: Internet
Author: User
Tags script tag syslog touch uuid

In our production environment, has been using the EC2 instances front-end server, in the increase in access, by setting a threshold, the threshold will automatically trigger the creation of a number of new instances, when the amount of traffic below a certain value, Automatically deletes a certain number of instances; Because AWS can copy a new host and give it a new IP address based on the set template, it is only necessary to manage the host name; In this respect, we manage the ID of the hostname through a redis database, When the host is created, it automatically increments according to the maximum ID, and the deletion of the host automatically decreases according to the maximum ID.
But this is not the focus of my article, to achieve this automatic scaling, there are many ways, we are through a lot of simple scripts to achieve.

Later, we built a local computer room, purchased a certain number of physical servers, each server installed XCP, and intended to create a virtual machine on top. We have been through the xencenter way, graphical operations to create, but to achieve the same as the above EC2 Automation to create a host, we encountered a problem, that is, each new VM, its IP address and host name, etc. after replication, can not automatically update. I have consulted some peers, may be they do not have such a large-scale use of xcp/xenserver, so the xencenter inside the creation of a good machine, and then through the console to modify the IP address and host name and so on.

As a result, I searched repeatedly on Google and finally found a solution. That is, by modifying the VM's kernel bootstrapper, passing the custom parameters to a kernel parameter called Pv-args, so that after the VM is started, the parameters can be obtained through the/proc/cmdline, and after acquisition, the VM executes a shell script on the first boot, To update the IP address and host name based on custom parameters; so that a newly created VM can automatically connect to the puppet such a management tool, then automatically pull the latest configuration, complete the entire server Automation configuration, and then online.

Environment Introduction:
Xenhost1 a physical Xcp/xenserver host
Vm-template templates that were previously defined
Vm-host-1 the VM that will be created

The following is my specific record of operations:
Get the Vm-template UUID
[Root@xenhost1 ~]# XE Vm-list | Grep-b 1 Vm-template

UUID (RO): C77040ae-3a50-9217-ff03-41992c34d1ec

2 Name-label (RW): vm-host-1


To modify the kernel boot mode and pass custom parameters
[Root@xenhost1 ~]# Xe vm-param-set uuid=c77040ae-3a50-9217-ff03-41992c34d1ec hvm-boot-policy= ""
[Root@xenhost1 ~]# Xe vm-param-set uuid=c77040ae-3a50-9217-ff03-41992c34d1ec pv-bootloader= "Pygrub"
[Root@xenhost1 ~]# Xe vm-param-set uuid=c77040ae-3a50-9217-ff03-41992c34d1ec pv-args= _hostname=vm-template "_ipaddr= 192.168.1.121 _netmask=255.255.255.0 _gateway=192.168.1.1 "

Start Vm-template
[Root@xenhost1 ~]# XE Vm-start vm=vm-template

Get Custom parameters
[Root@vm-template ~]# Cat/proc/cmdline

View Sourceprint?
1 ro root=/dev/mapper/vg_t-lv_root rd_no_luks lang=en_us. UTF-8 rd_no_md sysfont=latarcyrheb-sun16 rd_lvm_lv=vg_t/lv_root crashkernel=129m@0m keyboardtype=pc KEYTABLE=us Rd_NO _DM rhgb quiet _hostname=vm-template _ipaddr=192.168.1.121 _netmask=255.255.255.0
Defining initialization Scripts
[Root@vm-template ~]# cat/etc/rc.local

View Sourceprint?
1 #!/bin/sh

2 #

3 # This script would be executed *after* all the other init scripts.

4 # You can put your own initialization stuff with If you don ' t

5 # Want to did the full Sys V style init stuff.

6

7 touch/var/lock/subsys/local

8/root/bootstrap.sh
Create a specific script
[Root@vm-template ~]# touch/root/bootstrap.sh
[Root@vm-template ~]# chmod +x/root/bootstrap.sh

[Root@vm-template ~]# vim/root/bootstrap.sh

#!/bin/bash
#
# Bootstrap Script for hostname,network ...
#
# Author:dong Guo
# Last modified:2013/10/24 by Dong Guo

options=$ (cat/proc/cmdline|sed ' s/.*rhgb quiet//g ')
Config=/etc/sysconfig/network-scripts/ifcfg-eth0
Failed=/root/bootstrap.failed

function Check_root () {
If [$EUID-ne 0]; Then
echo "This script must is run as root"
Exit 1
Fi
}

function Configure_os () {
echo "Device=eth0" > $config
echo "Onboot=yes" >> $config
echo "Bootproto=none" >> $config

For I in $options
Todo
option=$ (echo $i |cut-d "="-F 1)
value=$ (echo $i |cut-d "="-F 2)
if ["${option:0:1}" = "_"]; Then
Case "$option" in
_hostname)
oldname=$ (hostname)
Newname= $value
Sed-i s/"$oldname"/"$newname"/g/etc/sysconfig/network
Hostname $newname
;;
_IPADDR)
echo "Ipaddr= $value" >> $config
;;
_netmask)
echo "netmask= $value" >> $config
;;
_gateway)
echo "gateway= $value" >> $config
;;
Esac
Fi
Done
}

function Restart_network () {
/etc/init.d/network restart
}

function Check_status () {
gateway=$ (grep-w GATEWAY $config |cut-d "="-F 2)
Route-n | Grep-wq $gateway
If [$?-eq 0]; Then
Sed-i/bootstrap/d/etc/rc.local
If [-a $failed]; Then
Rm-f $failed
Fi
Else
Touch $failed
Fi
}

Check_root
Configure_os
Restart_network
Check_status

viewing scripts

[Root@vm-template ~]# ls

Anaconda-ks.cfg Install.log.syslog bootstrap.sh Install.log

Exit Vm-template

[Root@vm-template ~]# exit

Close Vm-template

[Root@xenhost1 ~]# XE Vm-shutdown vm=vm-template

Get the UUID for XENHOST1 local storage
[Root@xenhost1 ~]# XE Sr-list | Grep-a 2-b 3 Xenhost1 | Grep-a 4-b 1 "Local Storage"

UUID (RO): 38e1ae16-6d5e-55af-5b55-8e26d30b13d7
Name-label (RW): Local storage
Name-description (RW):
Host (RO): Xenhost1
Type (RO): LVM
Content-type (RO): User

Replication creates a new VM Vm-host-1

[Root@xenhost1 ~]# Xe vm-copy new-name-label=vm-host-1 vm=vm-template Sr-uuid=38e1ae16-6d5e-55af-5b55-8e26d30b13d7

9529e2e5-e5b8-a22f-83da-6d3cd1be8101

Get the vm-host-1 UUID

[Root@xenhost1 ~]# XE Vm-list | Grep-a 1 9529e2e5-e5b8-a22f-83da-6d3cd1be8101

UUID (RO): 9529e2e5-e5b8-a22f-83da-6d3cd1be8101
Name-label (RW): vm-host-1

To pass a custom parameter

[Root@xenhost1 ~]# Xe vm-param-set uuid=9529e2e5-e5b8-a22f-83da-6d3cd1be8101 pv-args= _hostname=vm-host-1 "_ipaddr= 192.168.1.122 _netmask=255.255.255.0 _gateway=192.168.1.1 "

Start vm-host-1

[Root@xenhost1 ~]# XE Vm-start vm=vm-host-1

Landing vm-host-1

[Root@xenhost1 ~]# ssh root@192.168.1.122

The authenticity of host ' 192.168.1.122 (192.168.1.122) ' can ' t be established.
RSA key fingerprint is 81:f9:a4:45:0e:95:36:c5:e5:3e:80:33:8a:3a:db:18.
Are you sure your want to continue connecting (yes/no)? Yes
warning:permanently added ' 192.168.1.122 ' (RSA) to the list of known hosts.
root@192.168.1.122 ' s Password:

Last Login:thu Oct 05:49:24 2013 from 10.0.8.34

To see if the initialization script tag is removed

[Root@vm-host-1 ~]# cat/etc/rc.local

#!/bin/sh
#
# This script would be executed *after* all the other init scripts.
# You can put your own initialization stuff with If you don ' t
# want to does the full Sys V style init stuff.

Touch/var/lock/subsys/local
Done
[Root@vm-host-1 ~]# ls

Anaconda-ks.cfg Install.log.syslog bootstrap.sh Install.log

Exit Vm-host-1
[Root@vm-host-1 ~]# exit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.