现在的位置: 首页 > 综合 > 正文

linux容器技术-lxc创建虚拟机的执行过程分析

2013年08月31日 ⁄ 综合 ⁄ 共 4650字 ⁄ 字号 评论关闭

1. lxc介绍

    容器是一种轻量级的虚拟化技术,与qemu/kvm、VMware、Xen等完全的虚拟化方案相比,LXC更像是加强的“chroot”,因为LXC不但没有没有对硬件设备进行仿真,而且可以与主机共享一模一样的操作系统,所以LXC与solaris的zones和BSD的jails相比,更具优势。

    目前,有两个比较方便的管理容器的用户空间的工具:libvirt和lxc。libvirt通过"lxc:///"像管理其他虚拟机驱动一样管理lxc虚拟机。另一个是与libvirt完全独立的LXC,它定义了一些列的命令,可以更灵活的管理和使用lxc。

    下面,将以LXC为例来介绍lxc的使用。

2 LXC的安装和使用

(1)LXC的安装

  1. sudo apt-get install lxc

    该命令将自动安装LXC依赖的其他软件:cgroup-lite, lvm2, and debootstrap。如果想使用libvirt来实现lxc的管理,还需要安装 libvirt-bin和libvirt-lxc。

(2)LXC主机端的配置文件介绍
    在使用LXC之前,首先对其配置文件进行简单的介绍,以便使大家能更好的理解LXC的工作原理。
    a.  /etc/lxc/lxc.conf
     容器默认的配置文件,如果在创建lxc容器的时候不指定配置文件,将默认使用这个配置文件。主要针      对网络以及命名空间的配置。还有一些其他的配置例子可以在/usr/share/doc/lxc/examples/目录下
     找到。
  b. /usr/lib/lxc/templates/
     该目录下保存了当前LXC支持的各种发行版的linux的模板配置文件,目前主要有:
     lxc-ubuntu, lxc-fedora,lxc-opensuse,lxc-debian,lxc-busybox,lxc-sshd,lxc-cloud-ubuntu等。
  c. /var/lib/lxc
     每个容器的实例存放在这个目录下。
  d. /var/cache/lxc
     容器实例的cache,当用户创建一种类型的实例后,将会在此目录下cache,再次创建时将不需要从网      上下载,直接采用cache的版本,加速了容器实例的创建过程。

   下面来分析下lxc-ubuntu的创建过程(详见最下面的注释说明):

  1. #!/bin/bash
  2. #
  3. # template script for generating ubuntu container for LXC
  4. #
  5. # This script consolidates and extends the existing lxc ubuntu scripts
  6. #
  7. # Copyright ?2011 Serge Hallyn <serge.hallyn@canonical.com>
  8. # Copyright ?2010 Wilhelm Meier
  9. # Author: Wilhelm Meier <wilhelm.meier@fh-kl.de>
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License version 2, as
  13. # published by the Free Software Foundation.
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. set -e   ##如果命令带非零值返回,立即退出
  23. if [ -r /etc/default/lxc ]; then
  24. . /etc/default/lxc                 #导入一些环境变量
  25. fi
  26. configure_ubuntu()
  27. {
  28. rootfs=$1
  29. hostname=$2
  30. release=$3
  31. # configure the network using the dhcp
  32. cat <<EOF > $rootfs/etc/network/interfaces
  33. # This file describes the network interfaces available on your system
  34. # and how to activate them. For more information, see interfaces(5).
  35. # The loopback network interface
  36. auto lo
  37. iface lo inet loopback
  38. auto eth0
  39. iface eth0 inet dhcp
  40. EOF
  41. # set the hostname
  42. cat <<EOF > $rootfs/etc/hostname
  43. $hostname
  44. EOF
  45. # set minimal hosts
  46. cat <<EOF > $rootfs/etc/hosts
  47. 127.0.0.1 localhost
  48. 127.0.1.1 $hostname
  49. # The following lines are desirable for IPv6 capable hosts
  50. ::1 ip6-localhost ip6-loopback
  51. fe00::0 ip6-localnet
  52. ff00::0 ip6-mcastprefix
  53. ff02::1 ip6-allnodes
  54. ff02::2 ip6-allrouters
  55. EOF
  56. if [ ! -f $rootfs/etc/init/container-detect.conf ]; then
  57. # suppress log level output for udev
  58. sed -i "s/=\"err\"/=0/" $rootfs/etc/udev/udev.conf
  59. # remove jobs for consoles 5 and 6 since we only create 4 consoles in
  60. # this template
  61. rm -f $rootfs/etc/init/tty{5,6}.conf
  62. fi
  63. if [ -z "$bindhome" ]; then
  64. chroot $rootfs useradd --create-home -s /bin/bash ubuntu
  65. echo "ubuntu:ubuntu" | chroot $rootfs chpasswd
  66. fi
  67. return 0
  68. }
  69. # finish setting up the user in the container by injecting ssh key and
  70. # adding sudo group membership.
  71. # passed-in user is either 'ubuntu' or the user to bind in from host.
  72. finalize_user()
  73. {
  74. user=$1
  75. sudo_version=$(chroot $rootfs dpkg-query -W -f='${Version}' sudo)
  76. if chroot $rootfs dpkg --compare-versions $sudo_version gt "1.8.3p1-1"; then
  77. groups="sudo"
  78. else
  79. groups="sudo admin"
  80. fi
  81. for group in $groups; do
  82. chroot $rootfs groupadd --system $group >/dev/null 2>&1 || true
  83. chroot $rootfs adduser ${user} $group >/dev/null 2>&1 || true
  84. done
  85. if [ -n "$auth_key" -a -f "$auth_key" ]; then
  86. u_path="/home/${user}/.ssh"
  87. root_u_path="$rootfs/$u_path"
  88. mkdir -p $root_u_path
  89. cp $auth_key "$root_u_path/authorized_keys"
  90. chroot $rootfs chown -R ${user}: "$u_path"
  91. echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
  92. fi
  93. return 0
  94. }
  95. write_sourceslist()
  96. {
  97. # $1 => path to the rootfs
  98. # $2 => architecture we want to add
  99. # $3 => whether to use the multi-arch syntax or not
  100. case $2 in
  101. amd64|i386)
  102. MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
  103. SECURITY_MIRROR=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
  104. ;;
  105. *)
  106. MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
  107. SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
  108. ;;
  109. esac
  110. if [ -n "$3" ]; then
  111. cat >> "$1/etc/apt/sources.list" << EOF
  112. deb [arch=$2] $MIRROR ${release} main restricted universe multiverse
  113. deb [arch=$2] $MIRROR ${release}-updates main restricted universe multiverse
  114. deb [arch=$2] $SECURITY_MIRROR ${release}-security main restricted universe multiverse
  115. EOF
  116. else
  117. cat >> "$1/etc/apt/sources.list" << EOF
  118. deb $MIRROR ${release} main restricted universe multiverse
  119. deb $MIRROR ${release}-updates main restricted universe multiverse
  120. deb $SECURITY_MIRROR ${release}-security main restricted universe multiverse
  121. EOF
  122. fi
  123. }
  124. download_ubuntu()
  125. {
  126. cache=$1
  127. arch=$2
  128. release=$3
  129. packages=vim,ssh
  130. echo "installing packages: $packages"
  131. # check the mini ubuntu was not already downloaded
  132. mkdir -p "$cache/partial-$arch"
  133. if [ $? -ne 0 ]; then
  134. echo "Failed to create '$cache/partial-$arch' directory"
  135. return 1
  136. fi

抱歉!评论已关闭.