OpenNebula 镜像管理分析

从OpenNebula 3.8开始增加了镜像管理的功能,主要体现在增加了CLI - oneimage及其sunstone对应的UI。
1. 镜像类型
opennebula有三种类型的镜像,可以通过oneimage chtype改变镜像的类型。
  • OS:此种镜像包含一个完整的os,每一个virtual template必须包含一个OS型的镜像作为root disk
  • CDROM:此种镜像包含只读的数据
  • DATABLOCK:此种镜像作为数据的存储,能够被不同的vm访问和修改
2. 镜像的生命周期
Short state State Meaning
lock LOCKED The image file is being copied or created in the Datastore.
rdy READY Image ready to be used.
used USED Non-persistent Image used by at least one VM. It can still be used by other VMs.
used USED_PERS Persistent Image is use by a VM. It cannot be used by new VMs.
disa DISABLED Image disabled by the owner, it cannot be used by new VMs.
err ERROR Error state, a FS operation failed. See the Image information with oneimage show for an error message.
dele DELETE The image is being deleted from the Datastore.
镜像生命周期的状态转换图
OpenNebula 镜像管理分析_第1张图片

3. 镜像创建
首先,编写镜像创建的配置文件,如下所示:
$ cat ubuntu_img.one
NAME          = "Ubuntu"
PATH          = /home/cloud/images/ubuntu-desktop/disk.0
TYPE          = OS
DESCRIPTION   = "Ubuntu 10.04 desktop for students."
该文件的意思为:
a. 此镜像的名称为"Ubuntu"
b. 做好的镜像文件的路径为/home/cloud/images/ubuntu-desktop/disk.0,注意oneimage的功能是将做好的image文件导入opennebula的镜像管理模块中,并不是从一个iso文件来创建一个镜像文件,所以,需要事先通过virt-manager或virsh命令行来制作image文件
c. 指定镜像的类型为OS,即包含一个OS
d. 此镜像的简单描述

写好配置文件后,通过oneimage命令将此image添加到image repository中,具体命令如下:
  1. oneimage create ubuntu_image.one -d 100 // -d 选项指定此镜像属于哪个datastore

4. 镜像克隆
此功能为将已经存在的image克隆成一个新的,主要用于在修改一个镜像前的备份以及从公共镜像克隆一个私有的镜像,具体的命令为:
  1. oneimage clone oldimage_name newimage_name

5. 发布镜像
这里的发布,指的是将某个用户的镜像与其他用户或全部用户共享,通过oneimage chmod image_permission来实现, 类似于文件的权限控制。

上传镜像的操作是通过downloader.sh脚本来实现的,其功能是将镜像上传到指定的datastore的目录下,位置为: $INSTALL_PATH/var/datastores/100/

  1. #downloader.sh分析

  2. # Execute a command (first parameter) and use the first kb of stdout
  3. # to determine the file type
  4. # 获取上传image文件的类型,具体执行操作为:cat path/image | head -n 1024 | file ---mime-type -
  5. function get_type
  6. {
  7.     if [ "$NO_DECOMPRESS" = "yes" ]; then
  8.         echo "application/octet-stream"
  9.     else
  10.         command=$1

  11.         ( $command | head -n 1024 | file ---mime-type - ) 2>/dev/null
  12.     fi
  13. }

  14. # Gets the command needed to decompress an stream.
  15. function get_decompressor #get_type函数的返回值作为参数
  16. {
  17.     type=$1

  18.     case "$type" in
  19.     "application/x-gzip")
  20.         echo "gunzip -c -"
  21.         ;;
  22.     "application/x-bzip2")
  23.         echo "bunzip2 -c -"
  24.         ;;
  25.     *)
  26.         echo "cat"
  27.         ;;
  28.     esac
  29. }

  30. # Function called to decompress a stream. The first parameter is the command
  31. # used to decompress the stream. Second parameter is the output file or
  32. - for stdout.
  33. function decompress
  34. {
  35.     command="$1"
  36.     to="$2"

  37.     if [ "$to" = "-" ]; then
  38.         $command
  39.     else
  40.         $command > "$to"
  41.     fi
  42. }

  43. # Function called to hash a stream. First parameter is the algorithm name.
  44. function hasher
  45. {
  46.     if [ -"$1" ]; then
  47.         openssl dgst -$| awk '{print $NF}' > $HASH_FILE
  48.     else
  49.         # Needs something consuming stdin or the pipe will break
  50.         cat >/dev/null
  51.     fi
  52. }

  53. # Unarchives a tar or a zip a file to a directpry with the same name.
  54. function unarchive              ##对压缩的镜像进行解压
  55. {
  56.     TO="$1"

  57.     file_type=$(get_type "cat $TO")

  58.     tmp="$TO"

  59.     # Add full path if it is relative
  60.     if [ ${tmp:0:1} != "/" ]; then
  61.         tmp="$PWD/$tmp"
  62.     fi

  63.     IN="$tmp.tmp"
  64.     OUT="$tmp"

  65.     case "$file_type" in
  66.     "application/x-tar")
  67.         command="tar -xf $IN -C $OUT"
  68.         ;;
  69.     "application/zip")
  70.         command="unzip -d $OUT $IN"
  71.         ;;
  72.     *)
  73.         command=""
  74.         ;;
  75.     esac

  76.     if [ -"$command" ]; then
  77.         mv "$OUT" "$IN"
  78.         mkdir "$OUT"

  79.         $command

  80.         if [ "$?" != "0" ]; then
  81.             echo "Error uncompressing archive" >&2
  82.             exit -1
  83.         fi

  84.         rm "$IN"
  85.     fi
  86. }

  87. TEMP=`getopt -o m:s:l:-l md5:,sha1:,limit:,nodecomp -- "$@"`

  88. if [ $? != 0 ] ; then
  89.     echo "Arguments error"
  90.     exit -1
  91. fi

  92. eval set -- "$TEMP"

  93. while true; do              ###处理downloader.sh脚本的输入参数
  94.     case "$1" in
  95.         -m|--md5)
  96.             HASH_TYPE=md5
  97.             HASH=$2
  98.             shift 2
  99.             ;;
  100.         -s|--sha1)
  101.             HASH_TYPE=sha1
  102.             HASH=$2
  103.             shift 2
  104.             ;;
  105.         -n|--nodecomp)
  106.             export NO_DECOMPRESS="yes"
  107.             shift
  108.             ;;
  109.         -l|--limit)
  110.             export LIMIT_RATE="$2"
  111.             shift 2
  112.             ;;
  113.         --)
  114.             shift
  115.             break
  116.             ;;
  117.         *)
  118.             shift
  119.             ;;
  120.     esac
  121. done

  122. FROM="$1"
  123. TO="$2"

  124. # File used by the hasher function to store the resulting hash
  125. export HASH_FILE="/tmp/downloader.hash.$$"

  126. case "$FROM" in      
  127. http://*|https://*)
  128.     # -k so it does not check the certificate
  129.     # -L to follow redirects
  130.     # -sS to hide output except on failure
  131.     # --limit_rate to limit the bw
  132.     curl_args="-sS -k -L $FROM"

  133.     if [ -"$LIMIT_RATE" ]; then
  134.         curl_args="--limit-rate $LIMIT_RATE $curl_args"
  135.     fi

  136.     command="curl $curl_args"
  137.     ;;
  138. *)
  139.     command="cat $FROM"
  140.     ;;
  141. esac



  142. file_type=$(get_type "$command")
  143. decompressor=$(get_decompressor "$file_type")

  144. echo $file_type
  145. echo $decompressor

  146. $command | tee >( decompress "$decompressor" "$TO" ) \
  147.     >( hasher $HASH_TYPE ) >/dev/null

  148. if [ "$?" != "0" ]; then
  149.     echo "Error copying" >&2
  150.     exit -1
  151. fi

  152. if [ -"$HASH_TYPE" ]; then
  153.     HASH_RESULT=$( cat $HASH_FILE)
  154.     rm $HASH_FILE
  155.     if [ "$HASH_RESULT" != "$HASH" ]; then
  156.         echo "Hash does not match" >&2
  157.         exit -1
  158.     fi
  159. fi

  160. # Unarchive only if the destination is filesystem
  161. if [ "$TO" != "-" ]; then
  162.     unarchive "$TO"
  163. fi

你可能感兴趣的:(OpenNebula 镜像管理分析)