1. 程式人生 > >xen雜記(1)

xen雜記(1)

xen 雜記 基礎

一、Xen基礎

0、雜項

0.1 調大CPU和內存配置、開啟虛擬化功能

技術分享圖片

0.2 創建xen鏡像源

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

[root@node1 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

更新鏡像倉庫:
[root@node1 ~]# yum repolist

1、安裝配置Xen

1.1 安裝Xen

[root@node1 ~]# yum install xen

1.1 修改內核文件

[root@node1 ~]# 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 查看系統信息

[root@node1 ~]# uname -r
4.9.75-30.el6.x86_64

1.3.1 查看運行的虛擬機

這個時候原來的虛擬機已經變成Dom0 虛擬機
[root@node1 ~]# xl list
Name                                        ID   Mem VCPUs  State   Time(s)
Domain-0                                     0  1024     1     r-----      69.6
[root@node1 ~]# 

2、xend

2.1 開啟xend服務

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

下面使用xl,關閉xend
[root@node1 ~]# service xend stop

3、xl

3.1 查看幫助

[root@node1 ~]# xl help

xl使用配置文件,啟動DomU常用的指令說明

man xl.cfg

xl命令自己的通用所有配置參數

man xl.conf

4、創建虛擬機

4.1 創建磁盤文件

[root@node1 ~]# mkdir -pv /images/xen

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

[root@node1 xen]# du -sh busybox.img 
0   busybox.img

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

格式化磁盤文件:
[root@node1 xen]# mke2fs -t ext2 busybox.img

[root@node1 xen]# du -sh busybox.img 
66M busybox.img

掛載文件(本地回環設備):
[root@node1 xen]# mount -o loop busybox.img  /mnt/

4.2 安裝開發環境、編譯busybox

[root@node1 ~]# yum -y groupinstall "Development Tools" "Server Platform Development"

上傳busybox文件:
[root@node1 ~]# ll busybox-1.22.1.tar.bz2 
-rw-r--r-- 1 root root 2218650 Sep 11 08:08 busybox-1.22.1.tar.bz2

展開壓縮包:
[root@node1 ~]# tar xf busybox-1.22.1.tar.bz2 
[root@node1 ~]# cd busybox-1.22.1

為了把busybox編譯成靜態文件,需要安裝一個包:
[root@node1 ~]# yum install glibc-static

編譯busybox:
[root@node1 busybox-1.22.1]# make menuconfig

配置:
技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

安裝:
[root@node1 busybox-1.22.1]# make
[root@node1 busybox-1.22.1]# make install

查看安裝後的文件:
[root@node1 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
[root@node1 busybox-1.22.1]# 

拷貝文件:
[root@node1 busybox-1.22.1]# cp -a _install/* /mnt/

4.3 創建其他系統目錄(/mnt現在是根文件系統)

[root@node1 mnt]# mkdir proc sys dev etc var boot home

測試訪問:
[root@node1 ~]# chroot /mnt /bin/sh
/ # 
/ # ls
bin         dev         home        lost+found  sbin        usr
boot        etc         linuxrc     proc        sys         var

4.4 拷貝內核和ramdisk

創建軟鏈接:
[root@node1 boot]# ln -sv vmlinuz-2.6.32-504.el6.x86_64 vmlinuz
`vmlinuz‘ -> `vmlinuz-2.6.32-504.el6.x86_64‘
[root@node1 boot]# ll vmlinuz
lrwxrwxrwx 1 root root 29 Sep 11 08:52 vmlinuz -> vmlinuz-2.6.32-504.el6.x86_64

[root@node1 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‘
[root@node1 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配置文件)

[root@node1 xen]# cp xlexample.pvlinux busybox

[root@node1 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 啟動虛擬機

空跑測試,不真正運行:
[root@node1 xen]# xl -v create busybox -n

創建虛擬機:
[root@node1 xen]# xl -v create busybox

查看狀態:
[root@node1 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 連接創建的虛擬機

連接:
[root@node1 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+]

取出配置,去除空白行:
[root@node1 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"

關電(刪除)虛擬機:
[root@node1 xen]# xl destroy busybox-001
DEBUG libxl__device_destroy_tapdisk 105 type=aio:/images/xen/busybox.img disk=:/images/xen/busybox.img

創建虛擬機後立即連接控制臺:
[root@node1 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開機啟動
[root@node1 ~]# chkconfig --list NetworkManager
# chkconfig --level 3 NetworkManager off
# service NetworkManager stop

[root@node1 network-scripts]# cp ifcfg-eth0 ifcfg-xenbr0

[root@node1 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“

[root@node1 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

重啟網絡服務:
[root@node1 network-scripts]# service network restart

[root@node1 ~]# 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

[root@node1 xen]# brctl stp xenbr0 on
[root@node1 xen]# brctl show
bridge name bridge id       STP enabled interfaces
xenbr0      8000.000c295d1b80   yes     eth0
[root@node1 xen]# 

xen雜記(1)