試驗環境centos6.10
xen的工具堆疊介紹:
查看xl目錄的幫助:xl help
查看xen下安裝了哪些虛擬機:xl list
# xl list Domain-0
Name ID Mem VCPUs State Time(s)
Domain-0 0 1024 2 r----- 53.1
ID:從0開始的編號,即使中間洗掉了某個虛擬機,編號不會重用,繼續往下發,
Mem:分配給虛擬機多少記憶體
VCPUS:分配給虛擬機幾個CPU核心
State:虛擬機的狀態
- r:正常運行中
- b:阻塞
- p:暫停
- s:停止
- c:崩潰了
- d:正在關機的程序中
Time:此虛擬機運行了多久了,
創建虛擬機domU
首先要指定domu的模式(pv,hvm,pv on hvm)
創建pv模式的domu
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核心
- all:所有
- "0-3,5,^1":運行在0,2,3,5物理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的整體遷移,
-
disk指定的官方檔案
-
常用選項:[<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 glustervdev:在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"
- name :domu的名字,不能和別的domu重復
- kernel:內核檔案
- ramdisk:?
- extar:給內核傳遞的引數,這里傳遞的引數的目的是關閉selinux功能;init行程是/bin/sh
- memory:記憶體大小
- vcpus:cpu個數
- vif:網卡
- disk:檔案系統
- root:系統啟動是,把根檔案系統掛載到哪里,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
# c/c++ 學習互助QQ群:877684253

# 本人微信:xiaoshitou5854
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/41176.html
標籤:其他
上一篇:python筆記07
下一篇:python筆記11
