`
cloudtech
  • 浏览: 4593116 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

菜鸟玩云计算之八:Ubuntu Server12.10 之 KVM 安装和创建虚拟机

 
阅读更多

菜鸟玩云计算之八:Ubuntu Server12.10 之 KVM 安装和创建虚拟机

cheungmine

虚拟化技术甚嚣尘上, KVM是Linux内核级别的虚拟技术. 本文把如何在Ubuntu Server12.10上搭建虚拟机作一个总结. 过程繁复, 但是严格按本文执行, 一定可以创造出自己的虚拟机来. HOST机的安装(Ubuntu Server12.10)过程请参考我前面的文章.

1 安装好Ubuntu Server12.10 amd64 HOST机之后, 在HOST上运行下面的命令升级系统:

# echo 'Acquire::http::Proxy "http://10.112.18.178:3142";' > /etc/apt/apt/conf

其中:

http://10.112.18.178:3142 是你的cache server的IP和端口. 如何你没有这个cache server, 则忽略上一句.

关于cache server请参考:

http://blog.csdn.net/sheismylife/article/details/8066267


# apt-get update

# apt-get upgrade

# apt-get install python-vm-builder

我朋友非常喜欢emacs, 所以他强烈建议我加上下面这句:

# apt-get install emacs

2 修补python的bug

# wget -O ./vmpatch.py.diff https://launchpadlibrarian.net/120169451/usr_share_pyshared_VMBuilder_plugins_ubuntu_dapper.py.diff

# cp /usr/share/pyshared/VMBuilder/plugins/ubuntu/dapper.py dapper.py.bk

# patch /usr/share/pyshared/VMBuilder/plugins/ubuntu/dapper.py ./vmpatch.py.diff

3 配置HOST机器网络

按下面的内容更改你的网络配置文件:

/etc/network/interfaces

注意要把下面的IP地址改为适合你的网络的:

      address 10.112.18.161
      network 10.112.18.0
      netmask 255.255.255.0
      broadcast 10.112.18.255
      gateway 10.112.18.254

/etc/network/interfaces

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet manual

auto br0
iface br0 inet static
      address 10.112.18.161
      network 10.112.18.0
      netmask 255.255.255.0
      broadcast 10.112.18.255
      gateway 10.112.18.254
      dns-nameservers 8.8.8.8 8.8.4.4
   bridge_ports eth0
 bridge_fd 9
 bridge_hello 2
 bridge_maxage 12 

重启网络服务:

# service networking restart

# /etc/init.d/networking restart

4 动手写创建虚拟机的脚本

写好的脚本如下(参考:http://blog.csdn.net/sheismylife/article/details/8105636):

创建虚拟机的脚本: vmcre.sh

#!/bin/bash

function rm_dir {
    if [ -d "$1" ]; then
        rm -rf $1
    else
        echo "folder not existed: $1"
    fi
}

function rm_file {
    if [ -f "$1" ]; then
        rm $1
    else
        echo "file not existed: $1"
    fi
}

# check input parameters
if [ -z $1 ]; then
    echo "Usage: ./vmcre.sh vmname vmip"
    exit 1
fi

if [ $1 = "--help" ]; then
    echo "Usage: ./vmcre.sh vmname vmip"
    exit 2
fi

if [ -z $2 ]; then
    echo "Usage: ./vmcre.sh vmname vmip"
    echo "ip of vm missed"
    exit 3
fi

rm_dir "/var/lib/libvirt/images/$1/"
rm_file "/etc/libvirt/qemu/$1"

mkdir -p "/var/lib/libvirt/images/$1/mytemplates/libvirt"

cp /etc/vmbuilder/libvirt/* /var/lib/libvirt/images/$1/mytemplates/libvirt/
cp ./vm.partition /var/lib/libvirt/images/$1/
cd /var/lib/libvirt/images/$1/

# you should change below line for you
echo `vmbuilder kvm ubuntu --suite=quantal --flavour=virtual --arch=amd64 
--proxy=http://10.112.18.178:3142 -o --libvirt=qemu:///system --ip=$2 --gw=10.112.18.254 --dns=8.8.8.8 
--bcast=10.112.18.255 --mask=255.255.255.0 
--net=10.112.18.0 --part=vm.partition --templates=mytemplates 
--user=car --name=car --pass=abc123 --addpkg=openssh-server --mem=2048 --hostname=$1 --bridge=br0 --debug --verbose`

echo `virsh define /etc/libvirt/qemu/$1.xml`
echo `virsh start $1`

注意到你在使用这个脚本的时候要修改一些参数, 我用红色标记出来. 你也可以完善这个脚本以让它更加通用.

# you should change below line for you
下面的内容要写在一行里, 用空格隔开.


虚拟机的分区脚本: vm.patition

root 24000
swap 2000
---
/var 8000
这个脚本被vmcre.sh调用, 其中的"---"表示分为2个分区, root分区24GB, swap分区2GB, /var分区8GB. 你可以更改为适合你的机器.


5 创建虚拟机

#./vmcre.sh vm1 10.112.18.191

# ./vmcre.sh vm2 10.112.18.192

# ./vmcre.sh vm3 10.112.18.193

创建好之后虚拟机立即启动.

查看所有虚拟机

# virsh list --all

启动虚拟机

# virsh start vm1

修改修改vm1虚拟机参数, 修改下面的文件即可:

/etc/libvirt/qemu/vm1.xml

好了, 创建虚拟机就是这么简单. 一定要在bios里启用硬件支持. 自己上网搜吧!




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics