转自:http://hi.baidu.com/myiaas/item/222e5e602a9ccf196995e6ef
之前有个朋友告诉我,初始化配置脚本在windows下无法正确执行,当时我没有给与他明确的答复,后来经过测试发现了不同,如下所示:
Nebula3.8的Context.sh文件:
1
2
3
4
5
6
7
8
9
10
|
# Context variables generated by OpenNebula
DISK_ID=
"1"
DNS_A=
"202.106.0.20"
DNS_B=
"202.96.69.38"
DRIVER=
"file:"
GATEWAY=
"192.168.10.1"
HOSTNAME=
"centos6.3"
IP=
"192.168.10.60"
NETMASK=
"255.255.255.0"
TARGET=
"hdb"
|
Nebula4.x的Context.sh文件:
1
2
3
4
5
6
7
8
9
|
# Context variables generated by OpenNebula
DISK_ID=
'1'
ETH0_DNS=
'202.106.0.20'
ETH0_GATEWAY=
'192.168.10.1'
ETH0_IP=
'192.168.10.2'
ETH0_MASK=
'255.255.255.0'
ETH0_NETWORK=
'192.168.10.0'
NETWORK=
'YES'
TARGET=
'vdb'
|
在此,原nebula3.8的初始化脚本可继续使用,后面添加了nebula4.x下的初始化脚本。 细心的朋友会发现,nebula3.8的变量使用“双引号”,而nebula4.x版本的变量使用“单引号”,在原3.8的windows初始化脚本中有去除双引号的步骤,但是无法去除单引号,而windows系统认为set 变量“=”号后面的值都为变量值,如去除双引号后:set ETH0_IP='192.168.10.2'系统任务赋予ETH0_IP的值为'192.168.10.2'而非192.168.10.2,结果导致错误。另外变量定义也发生的改变,3.8中我以"IP"定义IP变量,而4.x中系统以"ETH0_IP"定义IP变量。
一、nebula3.8初始化脚本
1、Linux虚拟机:
(1).Linux虚拟机镜像配置:
[root@VM ~]# cd /tmp
[root@VM tmp]# vi contextualizing.sh
#!/bin/bash
for CDROM in $(ls /dev | grep "cdrom" | awk 'END{print}')
do
mount /dev/$CDROM /mnt
if [ -f /mnt/CentOS_init.sh ]; then
sh /mnt/CentOS_init.sh
fi
umount /mnt
done
# sed -i "/^sh/d" /etc/rc.local
[root@VM tmp]# vi /etc/rc.local
加入一行:
sh /tmp/contextualizing.sh
********************************************************************************************
(2).Opennebula管理端配置init.sh脚本:
[oneuser@master ~]$ mkdir scripts
[oneuser@master ~]$ cd scripts
[oneuser@master scripts]$ vi CentOS_init.sh
#!/bin/bash
# By chensh at 2013/08/26
# This script for CentOS6
#Define...................
HOSTS_NAME="/etc/hosts"
NETWORK_HOSTS_NAME="/etc/sysconfig/network"
INTERFACE="/etc/sysconfig/network-scripts/ifcfg-eth0"
CFG_DNS="/etc/resolv.conf"
#Scripts start..............
if [ -f /mnt/context.sh ]; then
. /mnt/context.sh
fi
if [ -n "$HOSTNAME" ]; then
sed -i "/localhost/! d" $HOSTS_NAME
echo "$IP $HOSTNAME" >> $HOSTS_NAME
sed -i "/^HOSTNAME/d" $NETWORK_HOSTS_NAME
echo "HOSTNAME=$HOSTNAME" >> $NETWORK_HOSTS_NAME
fi
if [ -n "$IP" ]; then
echo "DEVICE=eth0" > $INTERFACE
echo "BOOTPROTO=static" >> $INTERFACE
echo "ONBOOT=yes" >> $INTERFACE
echo "IPADDR=$IP" >> $INTERFACE
fi
if [ -n "$NETMASK" ]; then
echo "NETMASK=$NETMASK" >> $INTERFACE
fi
if [ -n "$GATEWAY" ]; then
echo "GATEWAY=$GATEWAY" >> $INTERFACE
echo "TYPE=Ethernet" >> $INTERFACE
fi
if [ -n "$DNS_A" ]; then
echo "nameserver $DNS_A" > $CFG_DNS
fi
if [ -n "$DNS_B" ]; then
sed -i "/nameserver/! d" $CFG_DNS
echo "nameserver $DNS_B" >> $CFG_DNS
fi
if [ -f /mnt/id_rsa.pub ]; then
cat /mnt/id_rsa.pub > /root/.ssh/authorized_keys
fi
reboot
****************************************************************************************
(3).Opennebula Linux VM模版添加内容:
CONTEXT=[
DNS_A="$NETWORK[DNS_A,NETWORK_ID=1]",
DNS_B="$NETWORK[DNS_B,NETWORK_ID=1]",
DRIVER="file:",
FILES="/srv/cloud/one/scripts/CentOS_init.sh",
GATEWAY="$NETWORK[GATEWAY,NETWORK_ID=1]",
HOSTNAME="$NAME",
IP="$NIC[IP]",
NETMASK="$NETWORK[NETWORK_MASK,NETWORK_ID=1]",
TARGET="hdb" ]
*NETWORK_ID为虚拟网络的ID号。
******************************************************************************
(4).Opennebula虚拟网络附加设置:
该附加设置便于虚拟机获取网关和DNS。
******************************************************************************
2、Windows虚拟机:
(1).Windows虚拟机镜像配置:
在C盘根目录下创建Windows_init.bat,内容如下:
@echo off
%开启变量延迟%
setlocal enabledelayedexpansion
for /f "eol=# delims=" %%a in (D:\context.sh) do set "_%%a"
%判断context.sh传递的变量是否存在,定义变量%
%主机名%
if defined _HOSTNAME (
set HOSTNAME=%_HOSTNAME%&set HOSTNAME=!HOSTNAME:"=!
) else (
echo "No HOSTNAME Defined~!"
)
%Administrator用户密码%
if defined _PASSWD (
set PASSWD=%_PASSWD%&set PASSWD=!PASSWD:"=!
) else (
echo "No passwd Defined~!"
)
%IP地址%
if defined _IP (
set IP=%_IP%&set IP=!IP:"=!
) else (
echo "No IP Defined~!"
)
%掩码%
if defined _NETMASK (
set NETMASK=%_NETMASK%&set NETMASK=!NETMASK:"=!
) else (
echo "No NETMASK Defined~!"
)
%网关%
if defined _GATEWAY (
set GATEWAY=%_GATEWAY%&set GATEWAY=!GATEWAY:"=!
) else (
echo "No GATEWAY Defined~!"
)
%第一个DNS地址%
if defined _DNS_A (
set DNS_A=%_DNS_A%&set DNS_A=!DNS_A:"=!
) else (
echo "No DNS_A Defined~!"
)
%第二个DNS地址%
if defined _DNS_B (
set DNS_B=%_DNS_B%&set DNS_B=!DNS_B:"=!
) else (
echo "No DNS_B Defined~!"
)
%在没有主机名的情况下以MAC作为主机名,并定义变量%
for /f "delims=: tokens=1-2" %%a in ('ipconfig /all^|findstr "Physical"') do set mac=%%b
set MAC=%mac:~7,2%%mac:~10,2%%mac:~13,2%%mac:~16,2%
set HOSTNM=VM-%MAC%
%配置主机名,如果传递变量存在按其名称配置主机名,如不存在以MAC地址的方式配置主机名%
if defined HOSTNAME (
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ComputerName" /v "ComputerName" /d "%HOSTNAME%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters" /v "NV Hostname" /d "%HOSTNAME%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters" /v "Hostname" /d "%HOSTNAME%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" /v "ComputerName" /d "%HOSTNAME%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v "NV Hostname" /d "%HOSTNAME%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v "Hostname" /d "%HOSTNAME%" /f
) else (
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ComputerName" /v "ComputerName" /d "%HOSTNM%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters" /v "NV Hostname" /d "%HOSTNM%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters" /v "Hostname" /d "%HOSTNM%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" /v "ComputerName" /d "%HOSTNM%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v "NV Hostname" /d "%HOSTNM%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v "Hostname" /d "%HOSTNM%" /f
)
%配置IP、掩码、网关信息%
if defined IP (
if defined NETMASK (
netsh interface ip set address name="本地连接" source=static addr=%IP% mask=%NETMASK%
if defined GATEWAY (
netsh interface ip set address name="本地连接" gateway=%GATEWAY% gwmetric=1
) else (
echo "No GATEWAY Information"
)
) else (
echo "No NETMASK Information"
)
) else (
echo "No IP Information"
)
%配置DNS信息%
if defined DNS_A (
netsh interface ip set dns name="本地连接" source=static addr=%DNS_A%
if defined DNS_B (
netsh interface ip add dns "本地连接" %DNS_B% index=2
) else (
echo "No Second DNS Information"
)
) else (
echo "No DNS Information"
)
%删除计划任务%
del C:\WINDOWS\Tasks\Windows_init.job
%配置Administrator用户密码%
if defined PASSWD (
net user administrator %PASSWD%
) else (
echo "No Password Information"
)
%删除bat脚本%
if exist D:\context.sh (
del C:\Windows_init.bat
shutdown -r -t 0
) else (
shutdown -r -t 0
)
pause>nul
******************************************************************************
(2).Opennebula Windows VM模版添加内容:
CONTEXT=[
DNS_A="$NETWORK[DNS_A,NETWORK_ID=1]",
DNS_B="$NETWORK[DNS_B,NETWORK_ID=1]",
DRIVER="file:",
GATEWAY="$NETWORK[GATEWAY,NETWORK_ID=1]",
HOSTNAME="$NAME",
IP="$NIC[IP]",
NETMASK="$NETWORK[NETWORK_MASK,NETWORK_ID=1]",
TARGET="hdb" ]
******************************************************************************
(3).Opennebula虚拟网络附加设置:
该附加设置便于虚拟机获取网关和DNS。
******************************************************************************
二、nebula4.x初始化脚本
1、Linux虚拟机:
(1).Linux虚拟机镜像配置初始化执行脚本:
[root@VM ~]# cd /tmp
[root@VM tmp]# vi CentOS_init.sh
#!/bin/bash
# By chensh at 2013/11/01
# This script by CentOS6
#Define...................
HOSTS_NAME="/etc/hosts"
NETWORK_HOSTS_NAME="/etc/sysconfig/network"
INTERFACE="/etc/sysconfig/network-scripts/ifcfg-eth0"
INTERFACE1="/etc/sysconfig/network-scripts/ifcfg-eth1"
INTERFACE2="/etc/sysconfig/network-scripts/ifcfg-eth2"
INTERFACE3="/etc/sysconfig/network-scripts/ifcfg-eth3"
CFG_DNS="/etc/resolv.conf"
#Scripts start..............
for CDROM in $(ls /dev | grep "cdrom" | awk 'END{print}')
do
mount /dev/$CDROM /mnt
done
sleep 2
if [ -f /mnt/context.sh ]; then
. /mnt/context.sh
fi
#check network ip,if exist do exit 0
SIP=`cat "$INTERFACE" | grep IPADDR |cut -d "=" -f 2`
if [ "$ETH0_IP" = "$SIP" ] ;then
exit 0
fi
###############################
if [ -n "$HOSTNAME" ]; then
sed -i "/localhost/! d" $HOSTS_NAME
echo "$ETH0_IP $HOSTNAME" >> $HOSTS_NAME
sed -i "/^HOSTNAME/d" $NETWORK_HOSTS_NAME
echo "HOSTNAME=$HOSTNAME" >> $NETWORK_HOSTS_NAME
fi
if [ -n "$ETH0_IP" ]; then
echo "DEVICE=eth0" > $INTERFACE
echo "BOOTPROTO=static" >> $INTERFACE
echo "ONBOOT=yes" >> $INTERFACE
echo "IPADDR=$ETH0_IP" >> $INTERFACE
fi
if [ -n "$ETH0_MASK" ]; then
echo "NETMASK=$ETH0_MASK" >> $INTERFACE
fi
if [ -n "$ETH0_NETWORK" ]; then
echo "NETWORK=$ETH0_NETWORK" >> $INTERFACE
fi
if [ -n "$ETH0_GATEWAY" ]; then
echo "GATEWAY=$ETH0_GATEWAY" >> $INTERFACE
echo "TYPE=Ethernet" >> $INTERFACE
fi
if [ -n "$ETH0_DNS" ]; then
echo "nameserver $ETH0_DNS" > $CFG_DNS
fi
if [ -n "$DNS_B" ]; then
sed -i "/nameserver/! d" $CFG_DNS
echo "nameserver $DNS_B" >> $CFG_DNS
fi
##########################################################
if [ -n "$ETH1_IP" ]; then
touch /etc/sysconfig/network-scripts/ifcfg-eth1
echo "DEVICE=eth1" > $INTERFACE1
echo "BOOTPROTO=static" >> $INTERFACE1
echo "ONBOOT=yes" >> $INTERFACE1
echo "IPADDR=$ETH1_IP" >> $INTERFACE1
fi
if [ -n "$ETH1_MASK" ]; then
echo "NETMASK=$ETH1_MASK" >> $INTERFACE1
fi
#########################################################
if [ -n "$ETH2_IP" ]; then
touch /etc/sysconfig/network-scripts/ifcfg-eth2
echo "DEVICE=eth2" > $INTERFACE2
echo "BOOTPROTO=static" >> $INTERFACE2
echo "ONBOOT=yes" >> $INTERFACE2
echo "IPADDR=$ETH2_IP" >> $INTERFACE2
fi
if [ -n "$ETH2_MASK" ]; then
touch /etc/sysconfig/network-scripts/ifcfg-eth3
echo "NETMASK=$ETH2_MASK" >> $INTERFACE2
fi
#########################################################
if [ -n "$ETH3_IP" ]; then
echo "DEVICE=eth3" > $INTERFACE3
echo "BOOTPROTO=static" >> $INTERFACE3
echo "ONBOOT=yes" >> $INTERFACE3
echo "IPADDR=$ETH3_IP" >> $INTERFACE3
fi
if [ -n "$ETH3_MASK" ]; then
echo "NETMASK=$ETH3_MASK" >> $INTERFACE3
fi
#########################################################
if [ -n "$SSH_PUBLIC_KEY" ]; then
echo "$SSH_PUBLIC_KEY" > /root/.ssh/authorized_keys
fi
if [ -n "$PASSWORD" ]; then
(
sleep 1
echo "$PASSWORD";
sleep 1
echo "$PASSWORD"
sleep 1) | passwd root
fi
sed -i '/CentOS_init.sh/d' /etc/rc.d/rc.local
service network restart
hostname $HOSTNAME && killall login
(2).rc.local添加执行命令:
[root@VM ~]# echo "sh /tmp/CentOS_init.sh" >> /etc/rc.local
*****************************************************************************
2、Windows虚拟机:
(1).Windows虚拟机镜像配置:
在C盘根目录下创建Windows_init.bat,内容如下:
@echo off
%开启变量延迟%
setlocal enabledelayedexpansion
for /F "eol=# tokens=1,2* delims=' " %%a in (D:\context.sh) do set "%%a%%b"
%判断context.sh传递的变量是否存在,定义变量%
%主机名%
if defined HOSTNAME (
set HOSTNAME=%HOSTNAME%&set HOSTNAME=!HOSTNAME:"=!
) else (
echo "No HOSTNAME Defined~!"
)
%Administrator用户密码%
if defined PASSWORD (
set PASSWD=%PASSWORD%&set PASSWD=!PASSWD:"=!
) else (
echo "No passwd Defined~!"
)
%IP地址%
if defined ETH0_IP (
set IP=%ETH0_IP%&set IP=!IP:"=!
) else (
echo "No IP Defined~!"
)
%掩码%
if defined ETH0_MASK (
set NETMASK=%ETH0_MASK%&set NETMASK=!NETMASK:"=!
) else (
echo "No NETMASK Defined~!"
)
%网关%
if defined ETH0_GATEWAY (
set GATEWAY=%ETH0_GATEWAY%&set GATEWAY=!GATEWAY:"=!
) else (
echo "No GATEWAY Defined~!"
)
%第一个DNS地址%
if defined ETH0_DNS (
set DNS_A=%ETH0_DNS%&set DNS_A=!DNS_A:"=!
) else (
echo "No DNS_A Defined~!"
)
%第二个DNS地址%
if defined DNS_B (
set DNS_B=%DNS_B%&set DNS_B=!DNS_B:"=!
) else (
echo "No DNS_B Defined~!"
)
%在没有主机名的情况下以MAC作为主机名,并定义变量%
for /f "delims=: tokens=1-2" %%a in ('ipconfig /all^|findstr "Physical"') do set mac=%%b
set MAC=%mac:~7,2%%mac:~10,2%%mac:~13,2%%mac:~16,2%
set HOSTNM=VM-%MAC%
%配置主机名,如果传递变量存在按其名称配置主机名,如不存在以MAC地址的方式配置主机名%
if defined HOSTNAME (
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ComputerName" /v "ComputerName" /d "%HOSTNAME%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters" /v "NV Hostname" /d "%HOSTNAME%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters" /v "Hostname" /d "%HOSTNAME%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" /v "ComputerName" /d "%HOSTNAME%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v "NV Hostname" /d "%HOSTNAME%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v "Hostname" /d "%HOSTNAME%" /f
) else (
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ComputerName" /v "ComputerName" /d "%HOSTNM%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters" /v "NV Hostname" /d "%HOSTNM%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters" /v "Hostname" /d "%HOSTNM%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" /v "ComputerName" /d "%HOSTNM%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v "NV Hostname" /d "%HOSTNM%" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v "Hostname" /d "%HOSTNM%" /f
)
%配置IP、掩码、网关信息%
if defined ETH0_IP (
if defined ETH0_MASK (
netsh interface ip set address name="本地连接" source=static addr=%IP% mask=%NETMASK%
if defined ETH0_GATEWAY (
netsh interface ip set address name="本地连接" gateway=%GATEWAY% gwmetric=1
) else (
echo "No GATEWAY Information"
)
) else (
echo "No NETMASK Information"
)
) else (
echo "No IP Information"
)
%配置DNS信息%
if defined ETH0_DNS (
netsh interface ip set dns name="本地连接" source=static addr=%DNS_A%
if defined DNS_B (
netsh interface ip add dns "本地连接" %DNS_B% index=2
) else (
echo "No Second DNS Information"
)
) else (
echo "No DNS Information"
)
%配置Administrator用户密码%
if defined PASSWORD (
net user administrator %PASSWD%
) else (
echo "No Password Information"
)
%删除计划任务%
del C:\WINDOWS\Tasks\Windows_init.job
%NewSid%
c:\newsid.exe /a
pause>nul
2.拷贝newsid.exe文件到C盘根目录下,传输过程省略。
http://pan.baidu.com/s/18EM4M
3.创建计划任务,开机运行Windows_init.bat。
配置完成。