一、
xen虚拟状态:
r: 运行
b: 阻塞
p: 暂停
s: 停止
c: 崩溃
d: dying, 正在关闭的过程中
二、创建xen的pv模式虚拟机
1、kernel (本身系统内核既可运行dom0中也可运行在domu中)
2、initrd或initramfs
3、DomU内核模块
4、根文件系统
5、swap设备
6、DomU的配置文件
三、xm的配置文件:
kernel:内核
ramdisk: initramfs或initrd
name: 域名称
memory: 内存大小
disk: 磁盘设备文件列表,格式disk=["disk1", "disk2",], 每个disk都由三个参数进行定义:“backend-dev”,“frontend-dev”,“mode”
backend-dev: 有两种类型,物理设备,虚拟磁盘映像文件,格式为分别为phy:device和file:/path/to/image_file;
front-dev: 定义其在DomU中设备类型;虚拟磁盘映像文件对应的设备文件名称通常为xvd[a-z]
mode: 访问权限模型,r, w
vcpus: 虚拟CPU的个数;
root: 根文件系统所在的设备;
extra: 传递给内核的额外参数;selinux=0
on_reboot: 执行xm reboot命令时的操作,有destroy和restart;
on_crash: 有destroy, restart, preserve(保存崩溃时的信息以用于调试)
vif :vif = [‘ip="172.16.100.11", bridge=br0‘]
type: 设备类型,默认为netfront
mac: 指定mac地址;
bridge: 指定桥接到的物理设备
ip: ip地址;
script: 配置此接口的脚本文件
vifname: 后端设备名称
bootloader: 引导器文件的路径,一般指的PyGrub的路径;
四、根文件系统制作步聚
root@logicserver ~]# mkdir /xen/vm1 -pv mkdir: 已创建目录 "/xen" mkdir: 已创建目录 "/xen/vm1" [root@logicserver ~]# cd /xen/vm1 [root@logicserver vm1]# dd if=/dev/zero of=centos6.img bs=1M oflag=direct seek=1023 count=1 [root@logicserver vm1]# ll -lh 总用量 1.0M -rw-r--r--. 1 root root 1.0G 9月 8 16:48 centos6.img [root@logicserver vm1]# du -sh centos6.img 1.0M centos6.img
格式化centos6.img和挂载到/mnt
[root@logicserver vm1]# mke2fs -t ext4 centos6.img mke2fs 1.41.12 (17-May-2010) centos6.img is not a block special device. 无论如何也要继续? (y,n) y 文件系统标签= 操作系统:Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 65536 inodes, 262144 blocks 13107 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=268435456 8 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376 正在写入inode表: 完成 Creating journal (8192 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 35 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@logicserver vm1]# mount -o loop centos6.img /mnt [root@logicserver vm1]# ls /mnt lost+found [root@logicserver vm1]# df -lh 文件系统 容量 已用 可用 已用%% 挂载点 /dev/sda2 50G 4.6G 43G 10% / tmpfs 462M 0 462M 0% /dev/shm /dev/sda1 120M 62M 50M 56% /boot /dev/sda5 400G 41G 339G 11% /data0 /xen/vm1/centos6.img 976M 1.3M 908M 1% /mnt
创建系统用到基本目录
[root@logicserver mnt]# mkdir -pv etc/{init,rc.d} bin sbin lib64 dev proc sys tmp var mkdir: 已创建目录 "etc" mkdir: 已创建目录 "etc/init" mkdir: 已创建目录 "etc/rc.d" mkdir: 已创建目录 "bin" mkdir: 已创建目录 "sbin" mkdir: 已创建目录 "lib64" mkdir: 已创建目录 "dev" mkdir: 已创建目录 "proc" mkdir: 已创建目录 "sys" mkdir: 已创建目录 "tmp" mkdir: 已创建目录 "var" [root@logicserver mnt]# ls bin dev etc lib64 lost+found proc sbin sys tmp var
执行脚本程序移植
[root@logicserver ~]# vim bincp.sh #!/bin/bash # declare -i DebugLevel=0 Target=/mnt [ -d $Target ] || mkdir $Target &> /dev/null read -p "A command: " Command while [ $Command != ‘q‘ -a $Command != ‘Q‘ ]; do Command=`which $Command | grep -v "^alias" | grep -o "[^[:space:]]*"` [ $DebugLevel -eq 1 ] && echo $Command ComDir=${Command%/*} [ $DebugLevel -eq 1 ] && echo $ComDir [ -d ${Target}${ComDir} ] || mkdir -p ${Target}${ComDir} [ ! -f ${Target}${Command} ] && cp $Command ${Target}${Command} && echo "Copy $Command to $Target finished." for Lib in `ldd $Command | grep -o "[^[:space:]]*/lib[^[:space:]]*"`; do LibDir=${Lib%/*} [root@logicserver ~]# sh bincp.sh A command: init Copy /sbin/init to /mnt finished. Copy /lib64/libnih.so.1 to /mnt finished. Copy /lib64/libnih-dbus.so.1 to /mnt finished. Copy /lib64/libdbus-1.so.3 to /mnt finished. Copy /lib64/libpthread.so.0 to /mnt finished. Copy /lib64/librt.so.1 to /mnt finished. Copy /lib64/libgcc_s.so.1 to /mnt finished. Copy /lib64/libc.so.6 to /mnt finished. Copy /lib64/ld-linux-x86-64.so.2 to /mnt finished. A command: bash Copy /bin/bash to /mnt finished. Copy /lib64/libtinfo.so.5 to /mnt finished. Copy /lib64/libdl.so.2 to /mnt finished. A command: ls Copy /bin/ls to /mnt finished. Copy /lib64/libselinux.so.1 to /mnt finished. Copy /lib64/libcap.so.2 to /mnt finished. Copy /lib64/libacl.so.1 to /mnt finished. Copy /lib64/libattr.so.1 to /mnt finished. A command: cat Copy /bin/cat to /mnt finished. A command: vim Copy /usr/bin/vim to /mnt finished. Copy /lib64/libm.so.6 to /mnt finished. Copy /usr/lib64/libgpm.so.2 to /mnt finished. Copy /usr/lib64/perl5/CORE/libperl.so to /mnt finished. Copy /lib64/libresolv.so.2 to /mnt finished. Copy /lib64/libnsl.so.1 to /mnt finished. Copy /lib64/libcrypt.so.1 to /mnt finished. Copy /lib64/libutil.so.1 to /mnt finished. Copy /lib64/libfreebl3.so to /mnt finished. A command: wget Copy /usr/bin/wget to /mnt finished. Copy /usr/lib64/libssl.so.10 to /mnt finished. Copy /usr/lib64/libcrypto.so.10 to /mnt finished. Copy /lib64/libgssapi_krb5.so.2 to /mnt finished. Copy /lib64/libkrb5.so.3 to /mnt finished. Copy /lib64/libcom_err.so.2 to /mnt finished. Copy /lib64/libk5crypto.so.3 to /mnt finished. Copy /lib64/libz.so.1 to /mnt finished. Copy /lib64/libkrb5support.so.0 to /mnt finished. Copy /lib64/libkeyutils.so.1 to /mnt finished.
给init准备好配置文件并且编辑保存
[root@logicserver ~]# cp /etc/init/rcS.conf /mnt/etc/init/ [root@logicserver ~]# vim /mnt/etc/init/rcS.conf # rcS - runlevel compatibility # # This task runs the old sysv-rc startup scripts. start on startup stop on runlevel task # Note: there can be no previous runlevel here, if we have one it‘s bad # information (we enter rc1 not rcS for maintenance). Run /etc/rc.d/rc # without information so that it defaults to previous=N runlevel=S. exec /etc/rc.d/rc.sysinit ~
创建rc.sysinit脚本并用chroot验证
[root@logicserver ~]# vim /mnt/etc/rc.d/rc.sysinit #!/bin/bash # echo -e "\tWelcom to Xen DomU Linux" /bin/bash ~ [root@logicserver ~]# chmod +x /mnt/etc/rc.d/rc.sysinit [root@logicserver ~]# chroot /mnt bash-4.1# ls bin dev etc lib64 lost+found proc sbin sys tmp usr var bash-4.1#
同步sync到硬盘,并卸载
[root@logicserver ~]# sync [root@logicserver ~]# sync [root@logicserver ~]# sync [root@logicserver ~]# sync [root@logicserver ~]# umount /mnt [root@logicserver ~]# df -lh
五、DomU的配置文件
由于他版号太长,把他们这么复制
[root@logicserver ~]# cp /boot/vmlinuz-2.6.32-279.el6.x86_64 /tmp/vmlinux [root@logicserver ~]# cp /boot/initramfs-2.6.32-279.el6.x86_64.img /tmp/initramfs
复制/etc/xen下例子配置文件
[root@logicserver ~]# cd /etc/xen/ [root@logicserver xen]# cp xmexample1 centos6
修改配置文件选项有
kernel = "/tmp/vmlinux"
ramdisk="/tmp/initramfs"
memory = 512
name = "centos6"
vcpus = 2
disk = [ ‘file:/xen/vm1/centos6.img,xvda,w‘ ]
extra = "3 selinux=0"
#vif = [ ‘‘ ] /注释
[root@logicserver xen]# vim centos6 # -*- mode: python; -*- #============================================================================ # Python configuration setup for ‘xm create‘. # You use a separate script for each domain you want to create, or # you can set the parameters for the domain on the xm command line. #============================================================================ #---------------------------------------------------------------------------- # Kernel image file. kernel = "/tmp/vmlinux" ramdisk="/tmp/initramfs" # Optional ramdisk. #ramdisk = "/boot/initrd.gz" # The domain build function. Default is ‘linux‘. #builder=‘linux‘ # Initial memory allocation (in megabytes) for the new domain. # # WARNING: Creating a domain with insufficient memory may cause out of # memory errors. The domain needs enough memory to boot kernel # and modules. Allocating less than 32MBs is not recommended. memory = 512 # A name for your domain. All domains must have different names. name = "centos6" # 128-bit UUID for the domain. The default behavior is to generate a new UUID # on each call to ‘xm create‘. #uuid = "06ed00fe-1162-4fc4-b5d8-11993ee4a8b9" # List of which CPUS this domain is allowed to use, default Xen picks #cpus = "" # leave to Xen to pick #cpus = "0" # all vcpus run on CPU0 #cpus = "0-3,5,^1" # all vcpus run on cpus 0,2,3,5 #cpus = ["2", "3"] # VCPU0 runs on CPU2, VCPU1 runs on CPU3 # Number of Virtual CPUS to use, default is 1 #vcpus = 1 vcpus = 2 #---------------------------------------------------------------------------- # Define network interfaces. # By default, no network interfaces are configured. You may have one created # with sensible defaults using an empty vif clause: # # vif = [ ‘‘ ] # # or optionally override backend, bridge, ip, mac, script, type, or vifname: # # vif = [ ‘mac=00:16:3e:00:00:11, bridge=xenbr0‘ ] # # or more than one interface may be configured: # # vif = [ ‘‘, ‘bridge=xenbr1‘ ] vif = [ ‘‘ ] #---------------------------------------------------------------------------- # Define the disk devices you want the domain to have access to, and # what you want them accessible as. # Each disk entry is of the form phy:UNAME,DEV,MODE # where UNAME is the device, DEV is the device name the domain will see, # and MODE is r for read-only, w for read-write. #disk = [ ‘phy:hda1,hda1,w‘ ] disk = [ ‘file:/xen/vm1/centos6.img,xvda,w‘ ] #---------------------------------------------------------------------------- # Define frame buffer device. # # By default, no frame buffer device is configured. # # To create one using the SDL backend and sensible defaults: # # vfb = [ ‘sdl=1‘ ] # # This uses environment variables XAUTHORITY and DISPLAY. You # can override that: # # vfb = [ ‘sdl=1,xauthority=/home/bozo/.Xauthority,display=:1‘ ] # # To create one using the VNC backend and sensible defaults: # # vfb = [ ‘vnc=1‘ ] # # The backend listens on 127.0.0.1 port 5900+N by default, where N is # the domain ID. You can override both address and N: # # vfb = [ ‘vnc=1,vnclisten=127.0.0.1,vncdisplay=1‘ ] # # Or you can bind the first unused port above 5900: # # vfb = [ ‘vnc=1,vnclisten=0.0.0.0,vncunused=1‘ ] # # You can override the password: # # vfb = [ ‘vnc=1,vncpasswd=MYPASSWD‘ ] # # Empty password disables authentication. Defaults to the vncpasswd # configured in xend-config.sxp. #---------------------------------------------------------------------------- # Set the kernel command line for the new domain. # You only need to define the IP parameters and hostname if the domain‘s # IP config doesn‘t, e.g. in ifcfg-eth0 or via DHCP. # You can use ‘extra‘ to set the runlevel and custom environment # variables used by custom rc scripts (e.g. VMID=, usr= ). # Set if you want dhcp to allocate the IP address. #dhcp="dhcp" # Set netmask. #netmask= # Set default gateway. #gateway= # Set the hostname. #hostname= "vm%d" % vmid # Set root device. root = "/dev/xvda ro" # Root device for nfs. #root = "/dev/nfs" # The nfs server. #nfs_server = ‘192.0.2.1‘ # You only need to define the IP parameters and hostname if the domain‘s # IP config doesn‘t, e.g. in ifcfg-eth0 or via DHCP. # You can use ‘extra‘ to set the runlevel and custom environment # variables used by custom rc scripts (e.g. VMID=, usr= ). # Set if you want dhcp to allocate the IP address. #dhcp="dhcp" # Set netmask. #netmask= # Set default gateway. #gateway= # Set the hostname. #hostname= "vm%d" % vmid # Set root device. root = "/dev/xvda ro" # Root device for nfs. #root = "/dev/nfs" # The nfs server. #nfs_server = ‘192.0.2.1‘ # Root directory on the nfs server. #nfs_root = ‘/full/path/to/root/directory‘ # Sets runlevel 4. extra = "3 selinux=0" #---------------------------------------------------------------------------- # Configure the behaviour when a domain exits. There are three ‘reasons‘ # for a domain to stop: poweroff, reboot, and crash. For each of these you # may specify: # # "destroy", meaning that the domain is cleaned up as normal; # "restart", meaning that a new domain is started in place of the old # one; # "preserve", meaning that no clean-up is done until the domain is # manually destroyed (using xm destroy, for example); or # "rename-restart", meaning that the old domain is not cleaned up, but is # renamed and a new domain started in its place. # # In the event a domain stops due to a crash, you have the additional options: # # "coredump-destroy", meaning dump the crashed domain‘s core and then destroy; # "coredump-restart‘, meaning dump the crashed domain‘s core and the restart.
六 启动虚拟机
本文出自 “散人” 博客,请务必保留此出处http://zouqingyun.blog.51cto.com/782246/1692831
原文地址:http://zouqingyun.blog.51cto.com/782246/1692831