您的位置:首页 > 大数据 > 云计算

菜鸟玩云计算之八:Ubuntu Server12.10 之KVM虚拟机及virsh console登录问题

2012-11-01 17:21 471 查看

菜鸟玩云计算之八:Ubuntu Server12.10 之

KVM虚拟机及virsh console登录问题

(持续更新中...)cheungmine 
KVM是Linux内核级的虚拟技术.本文把如何在Ubuntu Server12.10上搭建虚拟机作一个系统的总结. 类似文章网上浩如烟海, 但是都是零星片段, 有些还漏洞百出. 在UbuntuServer上搭建虚拟机过程繁复, 细节很多, 稍有不慎, 前功尽弃. 但是严格按本文执行, 一定可以创造出自己的虚拟机来. HOST机的安装 (Ubuntu Server12.10 amd64) 过程请参考我前面的文章.作为热身, 先熟悉几个名词:
 
HOST:主机, 也就是虚拟机的宿主机器
GUEST或VM: 客户机, 也就是虚拟机

1 升级HOST系统

安装好 Ubuntu Server12.10 amd64 HOST 机之后, 在HOST上运行下面的命令升级系统
# echo 'Acquire::http::proxy "http://yourCacherIP:3142";' > /etc/apt/apt.conf
***如果提示权限问题, 就手动创建一个/etc/apt/apt.conf:
***# vi /etc/apt/apt.conf
其中: http://yourCacherIP:3142 指向你的cacheserver的IP和端口. 如何你没有这个cacher, 则忽略上一句.

1.1 安装cache server

(参考http://blog.csdn.net/sheismylife/article/details/8066267)
 
# apt-get install apt-cacher
 在弹出的对话框中选daemon方式(默认选项), 安装完毕之后, 修改
/etc/apt-cacher/apt-cacher.conf 文件, 把:
#allowed_hosts = *
改为(去掉注释符号#即可), 以允许任何客户访问:
allowed_hosts = *
 
重启cacher服务:
# service apt-cacher restart
在同一网络的其他机器的浏览器中查看: http://yourCacherIP:3142/apt-cacher
可以看到apt-cacher启动了.
 
以后在任何需要使用它的客户机器上运行(保险起见, 运行完下面的语句最好重启客户机):
# echo 'Acquire::http::proxy"http://yourCacherIP:3142";' > /etc/apt/apt.conf

# echo 'Acquire::http::proxy"http://yourCacherIP:3142";' > /etc/apt/apt.conf.d/01proxy
即可使用apt-cacher服务, 包括下面安装虚拟机的时候, 需要指定--proxy, 就可以指定这个代理:
--proxy=http://yourCacherIP:3142

1.2 升级HOST系统

# apt-get update
# apt-get upgrade
# apt-get install python-vm-builder

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机器网络

3.1 网络配置文件

这个是一定要注意的地方, 仿照下面的内容更改你的网络配置文件:
/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 networkinterfaces available on your system
# and how to activate them. For moreinformation, 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

3.2 重启网络服务

# service networking restart

# /etc/init.d/networking


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

(参考http://blog.csdn.net/sheismylife/article/details/8105636)

4.1 创建虚拟机的脚本: create_vm.sh

#!/bin/bash
echo "Usage: ./vmcre.sh vmnamevmip"
echo "remove old vm..."
virsh destroy $1
rm -fr /var/lib/libvirt/images/$1/
virsh undefine $1 --remove-all-storage
rm /etc/libvirt/qemu/$1
echo "create new vm:/var/lib/libvirt/images/"$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/
cp ./firstboot_vm.sh /var/lib/libvirt/images/$1/
cd /var/lib/libvirt/images/$1/

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 \
--firstboot=/var/lib/libvirt/images/$1/firstboot_vm.sh \
--mem=2048 \
--hostname=$1 \
--bridge=br0 \
--debug \
--verbose`

echo "define and start vm: "$1
echo `virsh define /etc/libvirt/qemu/$1.xml`
echo `virsh start $1`
 注意到你在使用这个脚本的时候要修改一些参数:
car (是我使用的用户名)
pass (是我使用的密码)
所有的IP地址

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

root 24000
swap 2000
---
/var 8000

这个脚本被创建脚本create_vm.sh调用, 其中的"---"分隔表示分为2个分区, root 分区 24GB, swap
分区2GB, /var 分区 8GB. 你可以更改为适合你的配置.

4.3 虚拟机第一次启动脚本firstboot_vm.sh

参考本文的“9 为虚拟机添加启动脚本”

5 开始创建虚拟机

创建脚本有2个参数: vm1 是虚拟机的名称, 后面跟ip地址
#./create_vm.sh vm1 10.112.18.191

首次创建的时间会较长, 依赖于你的网络情况, 接下来会很快. 但是一般都是创建好之后就用克隆方法创建其他虚拟机了. 创建好之后虚拟机立即启动.

5.1 常用的几个虚拟机管理的命令

查看所有虚拟机
# virsh list --all

启动虚拟机
# virsh start vm1

关闭虚拟机
# virsh shutdown vm1

强制关闭虚拟机
# virsh destroy vm1

重启动虚拟机
# virsh reboot vm1

5.2 安装电源管理

运行下面的命令语句之后, 在virsh list --all中看到vm1并没有关闭
# virsh shutdown vm1

这是因为虚拟机中没有安装电源管理, 下面的命令是ssh到虚拟机安装电源管理acpid:
# ssh car@10.112.18.191
# apt-get install acpid 请参考virsh手册修改vm1的其他参数(如cpu数目)
好了, 创建虚拟机就是这么简单. 一定要在bios里启用virtualization硬件支持. 自己上网搜吧!

6 克隆虚拟机

我们创建了虚拟机之后, 要安装一系列软件, 这个过程是繁复的. 我们希望这个设置好的虚拟机能被很容易地克隆, 因此需要克隆虚拟机.克隆虚拟机很简单. 假设我们已经存在了虚拟机 vm1, 接下来克隆出vm2, 3, ...

6.1 首先关闭虚拟机vm1

# virsh shutdown vm1
# virsh list --all

确保vm1已经shutdown

6.2 克隆出vm2

# virt-clone -o vm1 -n vm2 --file /var/lib/libvirt/images/vm2a.img --file /var/lib/libvirt/images/vm2b.img 因为vm1是分区为2个磁盘, 因此上句中的--file出现了2次不同的vm2a.img和vm2b.img, 这个是必须注意的. 然后启动vm2, 立即进去修改网络设置:
# virsh start vm2
# ssh car@10.112.18.191
# vi /etc/network/interfaces
更改为:

...
auto br0
iface br0 inet static
address 10.112.18.192
network 10.112.18.0
netmask 255.255.255.0
...

保存退出vm2, 回到host.
# exit
# virsh shutdown vm2
# virsh start vm2

vm2 已经可以使用新的IP了, 其他和vm1完全一样
# ssh car@10.112.18.192

6.3 复制vm2到其他HOST计算机HOST2

你需要复制带走的文件就是
/etc/libvirt/qemu/vm2.xml

/var/lib/libvirt/images/vm2a.img
/var/lib/libvirt/images/vm2b.img
必须注意HOST2有和HOST相同的目录位置和网络配置, 然后在HOST2上运行:$ sudo virsh define ./vm2.xml
$ sudo virsh start vm2

7 删除虚拟机

如果有了vm定义文件, 如vm1.xml, 我们在创建虚拟机的最后阶段就是运行下面的命令来启用 (不是启动 start) 虚拟机vm1.
# virsh define /etc/libvirt/qemu/vm1.xml

删除虚拟机就是这个逆向的过程:
# virsh undefine vm1 --remove-all-storage
如果有下面的提示:
Storage volume../var/lib/libvirt/images/vm1.. is not managed by libvirt. Remove it manually.
 
那么就手动删除虚拟机设备文件:
# rm -fr /var/lib/libvirt/images/vm1
因此, 搞清楚虚拟机vm1是被定义在"/etc/libvirt/qemu/vm1.xml"中, 并且指向了物理路径
/var/lib/libvirt/images/vm1/
那么管理虚拟机就不是一个问题了. 详细的virsh命令请参考它的手册:
$ man virsh

8 virsh 控制台进入虚拟机

8.1 HOST上的设置

虚拟机提供了ssh进入的方式, 如果我们想要以virsh console方式进入虚拟机, 需要在host机器上的vm1.xml做些更改:
# virsh edit vm1
进入vim界面, 在<devices>...</devices>里面添加如下内容(注意, 如果已经存在就不要加了):
 <devices>
...
<serial type='pty'>
<source path='/dev/pts/2'/>
<target port='0'/>
</serial>
<console type='pty' tty='/dev/pts/2'>
<source path='/dev/pts/2'/>
<target port='0'/>
</console>
</devices>

8.2 GUEST上的设置使virsh console可以登录虚拟机

1) ubuntu

在虚拟机vm1中, 需要创建一个文件"/etc/init/ttyS0.conf", 内容如下(#注释部分可以忽略):
 
# ttyS0.conf - getty
# This service maintains a getty on ttyS0 from the point the system is
# started until it is shut down again.

start on stopped rc RUNLEVEL=[2345]
stop on runlevel [!2345]
respawn
exec /sbin/getty -L 115200 ttyS0 vt220 这个"/etc/init/ttyS0.conf"可以在创建虚拟机的最后阶段, 作为一个启动脚本加进去.

2) rhel6.4或centos6.5

How to activate the serial console in CentOS 6. Useful for logging into a virtual guest via 'virsh console'.

/etc/init/ttyS0.conf:
# ttyS0 - agetty for rhel6

stop on runlevel [016]
start on runlevel [345]

instance ttyS0
respawn
pre-start exec /sbin/securetty ttyS0
exec /sbin/agetty /dev/ttyS0 115200 vt100-nav

3) rhel5.4

# 添加ttyS0的安全许可,允许root登录:

echo "ttyS0" >> /etc/securetty


# 在 /etc/grub.conf 文件中为内核添加参数, 让内核把输出定向至 ttyS0。
console=ttyS0
 # 在 /etc/inittab 中添加 agetty, 在inittab里加一个ttyS0在系统启动时会生成一个ttyS0来接收内核的数据。
S0:12345:respawn:/sbin/agetty ttyS0 115200

8.3 virsh console

重启虚拟机后,进入 vm1
# virsh console vm1

多敲几次回车, 敲入登录的用户名和密码.
退出虚拟机回到HOST按组合键:
Ctrl+]

9 为虚拟机添加启动脚本

创建一个脚本文件: firstboot_vm.sh
# Set time zone
cp /usr/share/zoneinfo/Asia/Harbin /etc/localtime

# Set proxy server
# replaceyourProxyHostIP by your true IP, if not, comment below line
echo 'Acquire::http::proxy "http://yourProxyHostIP:3142";' >> /etc/apt/apt.conf

# ttyS0 -getty
#
# Thisservice maintains a getty on ttyS0 from the point the system is
# starteduntil it is shut down again.
echo "start on stopped rc RUNLEVEL=[2345]" > /etc/init/ttyS0.conf
echo "stop on runlevel [!2345]" >> /etc/init/ttyS0.conf
echo "respawn" >> /etc/init/ttyS0.conf
echo "exec /sbin/getty -L 115200 ttyS0 vt220" >> /etc/init/ttyS0.conf 在创建虚拟机时指定它, 参考 "4.1 创建虚拟机的脚本: create_vm.sh":
--firstboot=firstboot_vm.sh

(本文全部内容都经过测试)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: