xen学习(一)
添加镜像源
[root@xen xen]# cat /etc/yum.repos.d/xen.repo
[xen]
name=xenserver
baseurl=http://mirrors.163.com/centos/6.7/xen4/x86_64/
gpgcheck=0
安装
yum -y install xen
修改引导文件
title CentOS (3.18.21-16.el6.x86_64)
root (hd0,0)
kernel /xen.gz dom0_men=1024M cpufreq=xen dom0_max_vcpus=2 d0m0_vcpus_pin
module /vmlinuz-3.18.21-16.el6.x86_64 ro root=/dev/mapper/vg_xen-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_LVM_LV=vg_xen/lv_swap rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_xen/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
module /initramfs-3.18.21-16.el6.x86_64.img
1.重启后查看内核
[root@xen xen]# uname -r
3.18.21-16.el6.x86_64
设置桥接模式
1.eth0设置
[root@xen network-scripts]# cat ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
UUID=ff8ebf1a-a744-458c-b0db-9f8d4b0542a2
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=static
BRIDGE=xenbr0
2.xenbr0设置
[root@xen network-scripts]# cat ifcfg-xenbr0
DEVICE=xenbr0
TYPE=Bridge
NM_CONTROLLED=no
BOOTPROTO=static
IPADDR=10.1.100.19
PREFIX=24
GATEWAY=10.1.100.2
DNS1=114.114.114.114
3.重启网络并查看
#/etc/init.d/network restart
[root@xen network-scripts]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:C0:35:B0
inet6 addr: fe80::20c:29ff:fec0:35b0/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3579 errors:0 dropped:0 overruns:0 frame:0
TX packets:1549 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:438611 (428.3 KiB) TX bytes:190668 (186.1 KiB)
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:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
xenbr0 Link encap:Ethernet HWaddr 00:0C:29:C0:35:B0
inet addr:10.1.100.19 Bcast:10.1.100.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fec0:35b0/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1864 errors:0 dropped:0 overruns:0 frame:0
TX packets:637 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:269867 (263.5 KiB) TX bytes:73018 (71.3 KiB)
创建虚拟机
1.创建磁盘
[root@xen /]# qemu-img create -f raw -o size=2G /images/xen/busybox.img
-
查看磁盘大小
[root@xen xen]# ll -h total 0 -rw-r--r--. 1 root root 2.0G Dec 14 22:30 busybox.img [root@xen xen]# du -sh busybox.img 0 busybox.img
3.格式化
[root@xen xen]# mke2fs -t ext4 busybox.img
4.挂载
[root@xen xen]# mount -o loop /images/xen/busybox.img /mnt/
创建一个最小化的虚拟机
1.下载busybox
wget -c https://busybox.net/downloads/busybox-1.22.1.tar.bz2
[root@xen ~]# tar xf busybox-1.21.1.tar.bz2
[root@xen ~]# cd busybox-1.21.1
2.安装编译环境
# yum -y groupinstall "Development Tools" "Server Platform Development"
3.安装glibc-static 将busybox编译成静态文件
# yum -y install glibc-static
-
编译
# make menuconfig
1)选择busybox settings
2)选择build options
3)勾选第一项,保存退出
然后执行编译
# make -j
#make install
5.复制install到mnt目录,并创建linux必须的一些目录
[root@xen busybox-1.21.1]# cp -a _install/* /mnt/
[root@xen busybox-1.21.1]# cd /mnt/
[root@xen mnt]# mkdir proc sys dev etc var boot home
[root@xen mnt]# ll
total 56
drwxr-xr-x. 2 root root 4096 Dec 14 23:20 bin
drwxr-xr-x. 2 root root 4096 Dec 14 23:22 boot
drwxr-xr-x. 2 root root 4096 Dec 14 23:22 dev
drwxr-xr-x. 2 root root 4096 Dec 14 23:22 etc
drwxr-xr-x. 2 root root 4096 Dec 14 23:22 home
lrwxrwxrwx. 1 root root 11 Dec 14 23:20 linuxrc -> bin/busybox
drwx------. 2 root root 16384 Dec 14 22:30 lost+found
drwxr-xr-x. 2 root root 4096 Dec 14 23:22 proc
drwxr-xr-x. 2 root root 4096 Dec 14 23:20 sbin
drwxr-xr-x. 2 root root 4096 Dec 14 23:22 sys
drwxr-xr-x. 4 root root 4096 Dec 14 23:20 usr
drwxr-xr-x. 2 root root 4096 Dec 14 23:22 var
测试是否可以访问
1.测试是否可以访问目标系统
[root@xen /]# chroot /mnt/ /bin/sh
/ # ls
bin dev home lost+found sbin usr
boot etc linuxrc proc sys var
/ #
2.创建内核链接到目标系统
[root@xen boot]# ln -s vmlinuz-2.6.32-573.el6.x86_64 vmlinuz
[root@xen boot]# ln -sv initramfs-2.6.32-573.el6.x86_64.img initramfs.img
`initramfs.img' -> `initramfs-2.6.32-573.el6.x86_64.img'
3.设置xen启动配置文件
[root@xen xen]# cp xlexample.pvlinux busybox
[root@xen xen]# vim busybox
1)配置文件
# =====================================================================
# Example PV Linux guest configuration
# =====================================================================
#
# This is a fairly minimal example of what is required for a
# Paravirtualised Linux guest. For a more complete guide see xl.cfg(5)
# Guest name
name = "busybox-001"
# 128-bit UUID for the domain as a hexadecimal number.
# Use "uuidgen" to generate one if required.
# The default behavior is to generate a new UUID each time the guest is started.
#uuid = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
# Kernel image to boot
kernel = "/boot/vmlinuz"
# Ramdisk (optional)
ramdisk = "/boot/initramfs.img"
# Kernel command line options
extra = "selinux=0 init=/bin/sh"
# Initial memory allocation (MB)
memory = 512
# Maximum memory (MB)
# If this is greater than `memory' then the slack will start ballooned
# (this assumes guest kernel support for ballooning)
#maxmem = 512
# Number of VCPUS
vcpus = 2
# Network devices
# A list of 'vifspec' entries as described in
# docs/misc/xl-network-configuration.markdown
#vif = [ '' ]
# Disk Devices
# A list of `diskspec' entries as described in
# docs/misc/xl-disk-configuration.txt
disk = [ '/images/xen/busybox.img,raw,xvda,rw' ]
root = "/dev/xvda ro"
~
3.启动虚拟机直接进入console
[root@xen xen]# xl create busybox -c
*******
/ # ls
bin dev home lost+found sbin usr
boot etc linuxrc proc sys var
/ # ifconfig -a
lo Link encap:Local Loopback
LOOPBACK 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:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ #
4.退出
exit 拆除虚拟机
ctrl + ] 退出控制台
5.查看虚拟机
# [root@xen xen]# xl list
Name ID Mem VCPUs State Time(s)
Domain-0 0 4234 2 r----- 787.3
busybox-001 2 512 2 -b---- 2.2
6.进取虚拟机
[root@xen xen]# xl console busybox-001
7.关闭虚拟机
/bin # [root@xen xen]# xl destroy busybox-001
DEBUG libxl__device_destroy_tapdisk 105 type=aio:/images/xen/busybox.img disk=:/images/xen/busybox.img
[root@xen xen]# xl list
Name ID Mem VCPUs State Time(s)
Domain-0 0 4234 2 r----- 793.9
8.添加网卡
常用的key mac = 00:16:3e开头,默认会自己指定 bridge = 指定此网络接口在dom0中被关联在哪个桥设备中 model=模拟成哪一种设备,例如e1000 virname=接口名称,在dom0中显示 script=创建接口时需要执行的脚本 ip地址
1)编辑配置文件
vif = [ 'bridge=xenbr0,mac=00:16:3e:56' ]
2.)查看网卡驱动
[root@xen xen]# cd /lib/modules/2.6.32-573.el6.x86_64/kernel/drivers/net/
[root@xen net]# cp xen-netfront.ko 8139too.ko mii.ko /mnt/lib/modules/
3)启动虚拟机加载模块
/ # insmod /lib/modules/xen-netfront.ko
Initialising Xen virtual ethernet driver.
/ # ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:16:3E:56:DA:00
BROADCAST MULTICAST MTU:1500 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:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:18
lo Link encap:Local Loopback
LOOPBACK 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:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)