xen杂记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xen杂记相关的知识,希望对你有一定的参考价值。

一、Xen基础

0、杂项

0.1 调大CPU和内存配置、开启虚拟化功能

技术分享图片

0.2 创建xen镜像源

[[email protected] ~]# uname -r
2.6.32-504.el6.x86_64
[[email protected] ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          3824        162       3662          0         14         50
-/+ buffers/cache:         97       3727
Swap:         4095          0       4095
[[email protected] ~]# 

[[email protected] yum.repos.d]# vim local6.repo 

[ali-Xen]
name=Xen
baseurl=http://mirrors.aliyun.com/centos/6/virt/x86_64/xen/
enabled=1
gpgcheck=0

更新镜像仓库:
[[email protected] ~]# yum repolist

1、安装配置Xen

1.1 安装Xen

[[email protected] ~]# yum install xen

1.1 修改内核文件

[[email protected] ~]# vim /etc/grub.conf 

default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (4.9.75-30.el6.x86_64)
    root (hd0,0)
    kernel /xen.gz dom0_mem=1024M cpufreq=xen dom0_max_vcpus=1 dom0_vcpus_pin
    module /vmlinuz-4.9.75-30.el6.x86_64 ro root=UUID=01c53735-f7c4-4294-a944-e69df17ab9cd rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
    module /initramfs-4.9.75-30.el6.x86_64.img

1.2 重启

# shutdown -r now

1.3 查看系统信息

[[email protected] ~]# uname -r
4.9.75-30.el6.x86_64

1.3.1 查看运行的虚拟机

这个时候原来的虚拟机已经变成Dom0 虚拟机
[[email protected] ~]# xl list
Name                                        ID   Mem VCPUs  State   Time(s)
Domain-0                                     0  1024     1     r-----      69.6
[[email protected] ~]# 

2、xend

2.1 开启xend服务

[[email protected] ~]# service xend start
Starting xend daemon:                                      [  OK  ]
[[email protected] ~]# xm list
WARNING: xend/xm is deprecated.
Name                                        ID   Mem VCPUs      State   Time(s)
Domain-0                                     0  1024     1     r-----    102.4
[[email protected] ~]# 

下面使用xl,关闭xend
[[email protected] ~]# service xend stop

3、xl

3.1 查看帮助

[[email protected] ~]# xl help

xl使用配置文件,启动DomU常用的指令说明

man xl.cfg

xl命令自己的通用所有配置参数

man xl.conf

4、创建虚拟机

4.1 创建磁盘文件

[[email protected] ~]# mkdir -pv /images/xen

[[email protected] ~]# qemu-img create -f raw -o size=4G /images/xen/busybox.img
Formatting ‘/images/xen/busybox.img‘, fmt=raw size=4294967296 
[[email protected] ~]# ll /images/xen/busybox.img 
-rw-r--r-- 1 root root 4294967296 Sep 11 07:51 /images/xen/busybox.img

[[email protected] xen]# du -sh busybox.img 
0   busybox.img

另外一种创建方法:
[[email protected] xen]# rm -r busybox.img 
rm: remove regular file `busybox.img‘? y
[[email protected] xen]# 
[[email protected] xen]# qemu-img create -f raw  /images/xen/busybox.img 4G
Formatting ‘/images/xen/busybox.img‘, fmt=raw size=4294967296 
[[email protected] xen]# ll
total 0
-rw-r--r-- 1 root root 4294967296 Sep 11 07:56 busybox.img

格式化磁盘文件:
[[email protected] xen]# mke2fs -t ext2 busybox.img

[[email protected] xen]# du -sh busybox.img 
66M busybox.img

挂载文件(本地回环设备):
[[email protected] xen]# mount -o loop busybox.img  /mnt/

4.2 安装开发环境、编译busybox

[[email protected] ~]# yum -y groupinstall "Development Tools" "Server Platform Development"

上传busybox文件:
[[email protected] ~]# ll busybox-1.22.1.tar.bz2 
-rw-r--r-- 1 root root 2218650 Sep 11 08:08 busybox-1.22.1.tar.bz2

展开压缩包:
[[email protected] ~]# tar xf busybox-1.22.1.tar.bz2 
[[email protected] ~]# cd busybox-1.22.1

为了把busybox编译成静态文件,需要安装一个包:
[[email protected] ~]# yum install glibc-static

编译busybox:
[[email protected] busybox-1.22.1]# make menuconfig

配置:
技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

安装:
[[email protected] busybox-1.22.1]# make
[[email protected] busybox-1.22.1]# make install

查看安装后的文件:
[[email protected] busybox-1.22.1]# ll _install/
total 12
drwxr-xr-x 2 root root 4096 Sep 11 08:39 bin
lrwxrwxrwx 1 root root   11 Sep 11 08:39 linuxrc -> bin/busybox
drwxr-xr-x 2 root root 4096 Sep 11 08:40 sbin
drwxr-xr-x 4 root root 4096 Sep 11 08:40 usr
[[email protected] busybox-1.22.1]# 

拷贝文件:
[[email protected] busybox-1.22.1]# cp -a _install/* /mnt/

4.3 创建其他系统目录(/mnt现在是根文件系统)

[[email protected] mnt]# mkdir proc sys dev etc var boot home

测试访问:
[[email protected] ~]# chroot /mnt /bin/sh
/ # 
/ # ls
bin         dev         home        lost+found  sbin        usr
boot        etc         linuxrc     proc        sys         var

4.4 拷贝内核和ramdisk

创建软链接:
[[email protected] boot]# ln -sv vmlinuz-2.6.32-504.el6.x86_64 vmlinuz
`vmlinuz‘ -> `vmlinuz-2.6.32-504.el6.x86_64‘
[[email protected] boot]# ll vmlinuz
lrwxrwxrwx 1 root root 29 Sep 11 08:52 vmlinuz -> vmlinuz-2.6.32-504.el6.x86_64

[[email protected] boot]# ln -sv initramfs-2.6.32-504.el6.x86_64.img initramfs.img
`initramfs.img‘ -> `initramfs-2.6.32-504.el6.x86_64.img‘
[[email protected] boot]# ll initramfs.img 
lrwxrwxrwx 1 root root 35 Sep 11 08:53 initramfs.img -> initramfs-2.6.32-504.el6.x86_64.img

4.5 创建xl配置文件(PV模式可以使用Dom0配置文件)

[[email protected] xen]# cp xlexample.pvlinux busybox

[[email protected] xen]# vim busybox 

name = "busybox-001"
kernel = "/boot/vmlinuz"

ramdisk = "/boot/initramfs.img"

extra = "selinux=0 init=/bin/sh"

#暂时关闭网卡
#vif = [ ‘‘ ]

memory = 256

vcpus = 1

disk = [ ‘/images/xen/busybox.img,raw,xvda,rw‘ ]
root = "/dev/xvda ro"

4.6 启动虚拟机

空跑测试,不真正运行:
[[email protected] xen]# xl -v create busybox -n

创建虚拟机:
[[email protected] xen]# xl -v create busybox

查看状态:
[[email protected] xen]# xl list
Name                                        ID   Mem VCPUs  State   Time(s)
Domain-0                                     0  1024     1     r-----     523.0
busyboy-001                                  1   512     1     -b----       2.0

4.7 连接创建的虚拟机

连接:
[[email protected] xen]# xl console busyboy-001

/ # ls
bin         dev         home        lost+found  sbin        usr
boot        etc         linuxrc     proc        sys         var
/ # free -m
             total         used         free       shared      buffers
Mem:           489           27          461            0            0
-/+ buffers:                 27          461
Swap:            0            0            0

退出不能用【exit】,会拆除虚拟机的。要用Ctrl+]

取出配置,去除空白行:
[[email protected] xen]# grep -v "^#" busybox | grep -v "^$"
name = "busybox-001"
kernel = "/boot/vmlinuz"
ramdisk = "/boot/initramfs.img"
extra = "selinux=0 init=/bin/sh"
memory = 512
vcpus = 1
disk = [ ‘/images/xen/busybox.img,raw,xvda,rw‘ ]
root = "/dev/xvda ro"

关电(删除)虚拟机:
[[email protected] xen]# xl destroy busybox-001
DEBUG libxl__device_destroy_tapdisk 105 type=aio:/images/xen/busybox.img disk=:/images/xen/busybox.img

创建虚拟机后立即连接控制台:
[[email protected] xen]# xl create /etc/xen/busybox -c

/ # ls
bin         dev         home        lost+found  sbin        usr
boot        etc         linuxrc     proc        sys         var
Ctrl+] 退出控制台

4.8 Dom0创建桥接口

记得先关闭NetworkManager开机启动
[[email protected] ~]# chkconfig --list NetworkManager
# chkconfig --level 3 NetworkManager off
# service NetworkManager stop

[[email protected] network-scripts]# cp ifcfg-eth0 ifcfg-xenbr0

[[email protected] network-scripts]# vim ifcfg-eth0 

DEVICE=eth0
HWADDR=00:0C:29:5D:1B:80
TYPE=Ethernet
UUID=528d943c-4ffa-4974-a107-c5a1deabafb5
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO="none"
BRIDGE=”xenbr0“

[[email protected] network-scripts]# vim ifcfg-xenbr0 

DEVICE="xenbr0"
TYPE="Bridge"
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO="static"
IPADDR=10.201.106.21
NETMASK=255.255.255.0
GATEWAY=10.201.106.2
DNS1=10.201.106.2

重启网络服务:
[[email protected] network-scripts]# service network restart

[[email protected] ~]# ifconfig | more
eth0      Link encap:Ethernet  HWaddr 00:0C:29:5D:1B:80  
          inet6 addr: fe80::20c:29ff:fe5d:1b80/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:82593 errors:0 dropped:0 overruns:0 frame:0
          TX packets:22191 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:97975403 (93.4 MiB)  TX bytes:2425845 (2.3 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

xenbr0    Link encap:Ethernet  HWaddr 00:0C:29:5D:1B:80  
          inet addr:10.201.106.21  Bcast:10.201.106.255  Mask:255.255.255.0

[[email protected] xen]# brctl stp xenbr0 on
[[email protected] xen]# brctl show
bridge name bridge id       STP enabled interfaces
xenbr0      8000.000c295d1b80   yes     eth0
[[email protected] xen]# 

以上是关于xen杂记的主要内容,如果未能解决你的问题,请参考以下文章

xen杂记

Ubuntu系统下Xen虚拟机的基本安装方法(代码创建)

Xen

xen虚拟化基础篇

Xen的入门到放弃

技术杂记