设为首页 收藏本站
查看: 1224|回复: 0

[经验分享] xen虚拟机创建和使用(二)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-9-9 08:57:09 | 显示全部楼层 |阅读模式
一、
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的路径;

四、根文件系统制作步聚
1
2
3
4
5
6
7
8
9
10
root@logicserver ~]# mkdir /xen/vm1 -pv
mkdir: 已创建目录 "/xen"
mkdir: 已创建目录 "/xen/vm1"
[iyunv@logicserver ~]# cd /xen/vm1
[iyunv@logicserver vm1]# dd if=/dev/zero of=centos6.img bs=1M oflag=direct seek=1023 count=1
[iyunv@logicserver vm1]# ll -lh
总用量 1.0M
-rw-r--r--. 1 root root 1.0G 9月   8 16:48 centos6.img
[iyunv@logicserver vm1]# du -sh centos6.img
1.0M centos6.img




格式化centos6.img和挂载到/mnt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[iyunv@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.
[iyunv@logicserver vm1]# mount -o loop centos6.img /mnt
[iyunv@logicserver vm1]# ls /mnt
lost+found
[iyunv@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




创建系统用到基本目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[iyunv@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"
[iyunv@logicserver mnt]# ls
bin  dev  etc  lib64  lost+found  proc  sbin  sys  tmp  var




执行脚本程序移植
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
[iyunv@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%/*}
   
   
   
   
  [iyunv@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准备好配置文件并且编辑保存
1
2
3
4
5
6
7
8
9
10
11
12
13
[iyunv@logicserver ~]# cp /etc/init/rcS.conf /mnt/etc/init/
[iyunv@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验证
1
2
3
4
5
6
7
8
9
10
11
[iyunv@logicserver ~]# vim /mnt/etc/rc.d/rc.sysinit
#!/bin/bash
#
echo -e "\tWelcom to Xen DomU Linux"
/bin/bash
~      
[iyunv@logicserver ~]# chmod +x /mnt/etc/rc.d/rc.sysinit
[iyunv@logicserver ~]# chroot /mnt
bash-4.1# ls
bin  dev  etc  lib64  lost+found  proc sbin  sys  tmp usr  var
bash-4.1#




同步sync到硬盘,并卸载
1
2
3
4
5
6
[iyunv@logicserver ~]# sync
[iyunv@logicserver ~]# sync
[iyunv@logicserver ~]# sync
[iyunv@logicserver ~]# sync
[iyunv@logicserver ~]# umount /mnt
[iyunv@logicserver ~]# df -lh




五、DomU的配置文件
由于他版号太长,把他们这么复制
1
2
[iyunv@logicserver ~]# cp /boot/vmlinuz-2.6.32-279.el6.x86_64 /tmp/vmlinux
[iyunv@logicserver ~]# cp /boot/initramfs-2.6.32-279.el6.x86_64.img /tmp/initramfs




复制/etc/xen下例子配置文件
1
2
[iyunv@logicserver ~]# cd /etc/xen/
[iyunv@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 = [ '' ]    /注释
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
[iyunv@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.



六 启动虚拟机



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-111257-1-1.html 上篇帖子: xen安装(一) 下篇帖子: xen的实时迁移 虚拟机
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表