查看xl目录的帮助:xl help
html
查看xen下安装了哪些虚拟机:xl list
node
# xl list Domain-0 Name ID Mem VCPUs State Time(s) Domain-0 0 1024 2 r----- 53.1
ID:从0开始的编号,即便中间删除了某个虚拟机,编号不会重用,继续往下发。linux
Mem:分配给虚拟机多少内存ios
VCPUS:分配给虚拟机几个CPU核心c++
State:虚拟机的状态shell
Time:此虚拟机运行了多久了。centos
建立虚拟机domU微信
首先要指定domu的模式(pv,hvm,pv on hvm)网络
建立pv模式的domuapp
1,指定kernel,kernel文件放到哪里?能够在dom0中,也能够在domu中
2,initrd或initramfs
3,domu内核模块
4,根文件系统
5,swap设备
将上述内容定义在domu的配置文件
注意:xm和xl命令启动domu的配置文件略有区别。
建立domu的配置文件的具体写法,能够参考man xl.cfg
name:域名称,必须惟一,必须有
builder:指明虚拟机的类型。generic表示pv;hvm表示hvm
vcpus:虚拟机使用cpu核心的数量
maxvcpus:最大的cpu核心使用数量,要大于vcpus。当vcpus数量不够用时,就会增长到maxvcpus
cpus:指明vcpu能够运行在哪些物理cpu核心
memory=MBYTES:虚拟机使用的内存大小
maxmem=MBYTES:最大内存大小(原理同maxvcpus)
on-poweroff=ACTION:指明当用户输入了关机命令后,虚拟机应该怎么响应。不指定的话,就是关闭虚拟机。
ACTION:destroy(关机),restart(重启),preserve等
on-reboot=ACTION:指明当用户输入了关机命令后,虚拟机应该怎么响应。不指定的话,就是重启虚拟机。
on-crash=ACTION:指明当虚拟机崩溃时,应该怎么响应
uuid:domu的惟一标识
disk=[ "DISK_SPEC_STRING", "DISK_SPEC_STRING", ...]:指明磁盘设备列表。
有2种方式指定磁盘。一个是使用磁盘映像文件;另外一个是dom0在物理磁盘上划分一个分区给domu使用,这种方式性能好,但不利于xen的总体迁移。
经常使用选项:[<target>, [<format>, [<vdev>, [<access>]]]],
target:磁盘映像文件
format:磁盘的种类,可使用qemu-img
命令查看,支持的种类有:raw cow qcow vdi vmdk cloop dmg bochs vpc vvfat qcow2 qed vhdx parallels nbd blkdebug null host_cdrom host_floppy host_device file gluster
vdev:在domu种的设备文件类型,支持hd[x], xvd[x], sd[x] 等
access:访问权限
ro
, r
(specifies read-only)
rw
, w
(specifies read/write)
disk=[ "/images/xen/linux.img,raw,xvda,rw"]
用qemu-img
建立磁盘映像:qemu-img create [-f fmt] [-o options] filename [size]
用-o?,查看都有什么options
# qemu-img create -f raw -o? /images/xen/busybox.img Supported options: size Virtual disk size preallocation Preallocation mode (allowed values: off, falloc, full)
下面的命令建立了一个大小为2G的,格式为raw的磁盘映像文件,但实际是0字节。
# qemu-img create -f raw -o size=2G /images/xen/busybox.img # qemu-img create -f raw /images/xen/busybox.img 2G Formatting '/images/xen/busybox.img', fmt=raw size=2147483648 # ll -h -rw-r--r--. 1 root root 2.0G Feb 1 11:51 busybox.img # du -sh busybox.img 0 busybox.img
vif=[ "NET_SPEC_STRING", "NET_SPEC_STRING", ...]:指明网络接口列表。
vfb=[ "VFB_SPEC_STRING", "VFB_SPEC_STRING", ...]:指明virtual frame buffer列表
pci=[ "PCI_SPEC_STRING", "PCI_SPEC_STRING", ...]:pci插槽的设备列表。
kernel="PATHNAME":内核文件路径,此路径为dom0中的路径
ramdisk="PATHNAME":为kernel指定内核提供的ramdisk文件路径
root="STRING":指明根文件系统。
extra="STRING":额外传递给内核引导时使用的参数。
pv模式下的专用的bootloader="PROGRAM":若是domu使用本身的kernel和ramdisk,此时须要一个dom0中的应用程序来实现其bootloader功能。
要么使用kernel和ramdisk的组合,要么使用bootloader。
1,建立一个磁盘映像
# qemu-img create -f raw -o size=2G /images/xen/busybox.img # qemu-img create -f raw /images/xen/busybox.img 2G Formatting '/images/xen/busybox.img', fmt=raw size=2147483648 # ll -h -rw-r--r--. 1 root root 2.0G Feb 1 11:51 busybox.img # du -sh busybox.img 0 busybox.img
2,给磁盘分区建立ext4文件系统
不给此磁盘映像分区了,就按一个分区使用。分区命令(fdisk)
# mkfs -t ext2 busybox.img mke2fs 1.41.12 (17-May-2010) busybox.img is not a block special device. Proceed anyway? (y,n) y Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 131072 inodes, 524288 blocks 26214 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=536870912 16 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 39 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
会提示【busybox.img is not a block special device.】,输入y回车。
使用df和du命令确认一下,文件系统的类型和分区的实际大小。分区大小不是0了,变成了97M了,由于在分区里安装了文件系统。
# df -T busybox.img Filesystem Type 1K-blocks Used Available Use% Mounted on /dev/mapper/vg_c610-lv_root ext4 6795192 2644828 3782136 42% / [root@c610 xen]# du -sh busybox.img 97M busybox.img
3,挂载此文件系统到/mnt目录。
挂载本地回环文件(ios文件,img文件等)
# mount -o loop busybox.img /mnt [root@c610 xen]# ls /mnt/ lost+found
发现有了文件lost+found,说明挂载成功。
如今就能够在此文件系统里安装操做系统了。为了简单起见,用busybox模拟操做系统。因此编译安装busybox。
到此为止咱们发现,对domu的分区建立,文件系统建立等,都是在dom0里操做进行的。
为了编译,先安装开发环境:
# yum -y groupinstall "Development Tolls" "Server Platform Development"
下载busybox源代码,解压缩源代码压缩包
# tar xf busybox.tar.bz2
若是在操做系统上安装busybox的话,busybox使用的库是操做系统上的动态库,但咱们domu上没有操做系统,因此就没有库能够供busybox使用,因此编译busybox时,就须要把所使用的库静态链接进busybox,因此先安装busybox所使用的静态库。
# yum -y install glibc-static # yum -y install gcc
配置busybox的编译选项:
# make menuconfig
出现以下画面:
勾选:Build Busybox as a static binary (no shared libs),保存退出
编译:make
安装:make install
安装成功后,会出现[_install]目录,把此目录里的全部内容,并保持原属性,复制到挂载这domu文件系统的/mnt目录下。
# cp -a _install/* /mnt
查看/mnt目录:
# ll drwxr-xr-x. 2 root root 4096 Feb 1 14:17 bin lrwxrwxrwx. 1 root root 11 Feb 1 14:17 linuxrc -> bin/busybox drwx------. 2 root root 16384 Feb 1 12:30 lost+found drwxr-xr-x. 2 root root 4096 Feb 1 14:17 sbin drwxr-xr-x. 4 root root 4096 Feb 1 14:17 usr
发现已经有了几个目录,但还缺乏几个目录,在/mnt下手动建立这些目录。最主要的是proc,sys,dev。
# mkdir proc sys dev etc var boot home
尝试使用busybox里的sh
# chroot /mnt /bin/sh / # ls bin dev home lost+found sbin usr boot etc linuxrc proc sys var / #
发现咱们建立的目录都存在,说明一切正常。
到此为止,domu的根文件系统,咱们已经准备稳当了,就差启动文件了。
前面说过,centos6不能安装在dom0上,可是能够安装在domu上,全部就直接使用原来centos上的启动文件.
在/boot下建立一个软链接。
# ln -sv vmlinuz-2.6.32-754.el6.x86_64 vmlinuz `vmlinuz' -> `vmlinuz-2.6.32-754.el6.x86_64' # ll -h lrwxrwxrwx. 1 root root 29 Feb 1 14:30 vmlinuz -> vmlinuz-2.6.32-754.el6.x86_64 -rwxr-xr-x. 1 root root 4.2M Jun 20 2018 vmlinuz-2.6.32-754.el6.x86_64
在/boot下再建立一个软链接
# ln -sv initramfs-2.6.32-754.el6.x86_64.img initramfs.img `initramfs.img' -> `initramfs-2.6.32-754.el6.x86_64.img' # ll -h lrwxrwxrwx. 1 root root 35 Feb 1 14:32 initramfs.img -> initramfs-2.6.32-754.el6.x86_64.img -rw-------. 1 root root 5.9M Jan 31 14:16 initrd-2.6.32-754.el6.x86_64kdump.img
建立xl命令,建立domu所使用的配置文件,配置文件放在/etc/xen目录。xlexample是例子。
xl.conf文件是xl命令本身的配置文件。
# cd /etc/xen # ll total 24 drwx------. 2 root root 4096 Dec 12 20:07 auto -rw-r--r--. 1 root root 661 Dec 11 20:44 cpupool drwx------. 2 root root 4096 Jan 31 14:56 scripts -rw-r--r--. 1 root root 1436 Dec 11 20:44 xl.conf -rw-r--r--. 1 root root 1386 Dec 11 20:44 xlexample.hvm -rw-r--r--. 1 root root 1250 Dec 11 20:44 xlexample.pvlinux
复制xlexample.pvlinux
# cp xlexample.pvlinux busybox.pvlinux
修改busybox.pvlinux文件
由于咱们使用的dom0里的内核文件,dom0里是使用SELinux功能的,咱们要关闭它。
name = "busybox001.pvlinux" kernel = "/boot/vmlinuz" #ramdisk = "/boot/initrd.gz" ramdisk = "/boot/initramfs.img" extra = "selinux=0 init=/bin/sh" memory = 256 vcpus = 2 #vif = [ '' ] disk = [ '/images/xen/busybox.img,raw,xvda,rw' ] root = "/dev/xvda ro"
建立domu# xl help create
# xl help create Usage: xl [-vfN] create <ConfigFile> [options] [vars] Create a domain from config file <filename>. Options: -h Print this help. -p Leave the domain paused after it is created. -c Connect to the console after the domain is created. -f FILE, --defconfig=FILE Use the given configuration file. -q, --quiet Quiet. -n, --dryrun Dry run - prints the resulting configuration (deprecated in favour of global -N option). -d Enable debug messages. -F Run in foreground until death of the domain. -e Do not wait in the background for the death of the domain. -V, --vncviewer Connect to the VNC display after the domain is created.
xl [-vfN]里的v:显示详细信息;f:xl命令使用哪一个配置文件,不指定的话默认使用/etc/xen/xl.conf
<ConfigFile>:建立domu所使用的配置文件
options里的-d:debug模式;-n:试验运行
# xl -v create busybox.pvlinux -n Parsing config from busybox.pvlinux { "c_info": { "type": "pv", "name": "busybox001.pvlinux", "uuid": "23537733-95f2-44d5-84d5-07aa76282134", "run_hotplug_scripts": "True" }, "b_info": { "max_vcpus": 2, "avail_vcpus": [ 0, 1 ], "max_memkb": 262144, "target_memkb": 262144, "shadow_memkb": 4096, "sched_params": { }, "claim_mode": "True", "kernel": "/boot/vmlinuz", "cmdline": "root=/dev/xvda ro", "ramdisk": "/boot/initramfs.img", "type.pv": { }, "arch_arm": { } }, "disks": [ { "pdev_path": "/images/xen/busybox.img", "vdev": "xvda", "format": "raw", "readwrite": 1 } ], "on_reboot": "restart", "on_soft_reset": "soft_reset"
查看试验运行的结果,若是没有问题,就建立
[root@c610 xen]# xl -v create busybox.pvlinux
建立成功
# xl -v create busybox.pvlinux # xl list Name ID Mem VCPUs State Time(s) Domain-0 0 1024 2 r----- 748.7 busybox001.pvlinux 3 256 2 r----- 2.4