在iaas平台中,当用户创建一个虚拟机后,必须按照用户自定义的信息对虚拟机进行初始化,比如:主机名,用户名/密码,ip地址,mac地址等,另外,可能用户还想在虚拟机启动后,某些服务就已经被自动配置好了,比如ssh登录等。 所有这些对Virtual Machine的定制,在opennebula中是通过一个叫Context iso的文件来完成的。
opennebula把所有的用户对Virtual Machine的定制化信息都做成一个iso文件,然后在Virtual Machine启动的时候,将此iso文件挂载到VM的光驱中,然后执行光驱中的相对应的脚本来完成对VM的定制,整个过程非常类似VMWare的vmware-tools。
- #!/bin/bash
- # -------------------------------------------------------------------------- #
- # Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
- # #
- # Licensed under the Apache License, Version 2.0 (the "License"); you may #
- # not use this file except in compliance with the License. You may obtain #
- # a copy of the License at #
- # #
- # http://www.apache.org/licenses/LICENSE-2.0 #
- # #
- # Unless required by applicable law or agreed to in writing, software #
- # distributed under the License is distributed on an "AS IS" BASIS, #
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
- # See the License for the specific language governing permissions and #
- # limitations under the License. #
- #--------------------------------------------------------------------------- #
- while (( "$#" )); do
- if [ "$#" == "1" ]; then
- DST=$1
- else
- SRC="$SRC $1"
- fi
- shift
- done
- if [ -z "${ONE_LOCATION}" ]; then
- TMCOMMON=/usr/lib/one/mads/tm_common.sh
- else
- TMCOMMON=$ONE_LOCATION/lib/mads/tm_common.sh
- fi
- . $TMCOMMON
- DST_PATH=`arg_path $DST`
- DST_DIR=`dirname $DST_PATH`
- DST_FILE=`basename $DST_PATH`
- DST_HASH=`echo -n $DST | $MD5SUM | $AWK '{print $1}'`
- if [ -z "$ONE_LOCATION" ]; then
- TMP_DIR="/var/lib/one/$DST_HASH"
- else
- TMP_DIR="$ONE_LOCATION/var/$DST_HASH"
- fi
- ISO_DIR="$TMP_DIR/isofiles"
- //前面都是一些脚本的include和变量的定义,不去详细分析
//创建context iso目录,制作iso所需的所有脚本,都要事先拷贝到此目录下,注意此处使用了MD5SUM,用来保证每个vm的iso文件目录的唯一性,可以学习此方法
- exec_and_log "mkdir -p $ISO_DIR" \
- "Error creating directory $ISO_DIR"
- //对于输入目录下的所有文件,执行根据文件的前缀(即传输协议),采用wget或者cp来获得文件,我们这里以cp为例,就是拷贝所需的所有文件到$ISO_DIR目录下
- for f in $SRC; do
- case $f in
- http://*)
- exec_and_log "$WGET -P $ISO_DIR $f" \
- "Error downloading $f"
- ;;
- *)
- exec_and_log "cp -R $f $ISO_DIR" \
- "Error copying $f to $ISO_DIR"
- ;;
- esac
- done
//将ISO_DIR目录制作成iso格式的文件$TMP_DIR/$DST_FILE
- exec_and_log "$MKISOFS -o $TMP_DIR/$DST_FILE -J -R $ISO_DIR" \
- "Error creating iso fs"
- //将生成的ISO文件通过scp拷贝到目标主机,一旦拷贝完成,vm就可挂载iso光驱,执行脚本了
- exec_and_log "$SCP $TMP_DIR/$DST_FILE $DST" \
- "Error copying $TMP_DIR/$DST_FILE to $DST"
- exec_and_log "rm -rf $TMP_DIR" \
- "Error deleting $TMP_DIR"
3. opennebula生成的vm的配置文件中对context iso的描述
在prolog_action函数中,会根据vm的disk的数据,将context iso自动的分配到disk.num上,下面这段代码的意思是: opennebula core构造命令:
xfr << "CONTEXT " << vm->get_context_file() << " "; 和
- xfr << vm->get_hostname() << ":" << vm->get_remote_dir()
- << "/disk." << num << endl;
- 前者的意思是,将CONTEXT命令用来作用哪些文件,即用哪些文件来生成iso,
- 后者的意思是,将生成iso拷贝到远程目录下的disk.num
- // ------------------------------------------------------------------------
- // Generate context file (There are 0...num-1 disks, constext is disk.num)
- // ------------------------------------------------------------------------
- context_result = vm->generate_context(files);
- if ( context_result == -1 )
- {
- goto error_context;
- }
- if ( context_result )
- {
- xfr << "CONTEXT " << vm->get_context_file() << " ";
- if (!files.empty())
- {
- xfr << files << " ";
- }
- xfr << vm->get_hostname() << ":" << vm->get_remote_dir()
- << "/disk." << num << endl;
- }