天天看点

xen虚拟机利用dom0内核来创建(二)

一、

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

<code>root@logicserver ~]# mkdir /xen/vm1 -pv</code>

<code>mkdir: 已创建目录 </code><code>"/xen"</code>

<code>mkdir: 已创建目录 </code><code>"/xen/vm1"</code>

<code>[root@logicserver ~]# cd /xen/vm1</code>

<code>[root@logicserver vm1]# dd </code><code>if</code><code>=/dev/zero of=centos6.img bs=1M oflag=direct seek=</code><code>1023</code> <code>count=</code><code>1</code> 

<code>[root@logicserver vm1]# ll -lh</code>

<code>总用量 </code><code>1</code><code>.0M</code>

<code>-rw-r--r--. </code><code>1</code> <code>root root </code><code>1</code><code>.0G </code><code>9</code><code>月   </code><code>8</code> <code>16</code><code>:</code><code>48</code> <code>centos6.img</code>

<code>[root@logicserver vm1]# du -sh centos6.img </code>

<code>1</code><code>.0M centos6.img</code>

格式化centos6.img和挂载到/mnt

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

<code>[root@logicserver vm1]# mke2fs -t ext4 centos6.img </code>

<code>mke2fs </code><code>1.41</code><code>.</code><code>12</code> <code>(</code><code>17</code><code>-May-</code><code>2010</code><code>)</code>

<code>centos6.img </code><code>is</code> <code>not a block special device.</code>

<code>无论如何也要继续? (y,n) y</code>

<code>文件系统标签=</code>

<code>操作系统:Linux</code>

<code>块大小=</code><code>4096</code> <code>(log=</code><code>2</code><code>)</code>

<code>分块大小=</code><code>4096</code> <code>(log=</code><code>2</code><code>)</code>

<code>Stride=</code><code>0</code> <code>blocks, Stripe width=</code><code>0</code> <code>blocks</code>

<code>65536</code> <code>inodes, </code><code>262144</code> <code>blocks</code>

<code>13107</code> <code>blocks (</code><code>5.00</code><code>%) reserved </code><code>for</code> <code>the </code><code>super</code> <code>user</code>

<code>第一个数据块=</code><code>0</code>

<code>Maximum filesystem blocks=</code><code>268435456</code>

<code>8</code> <code>block groups</code>

<code>32768</code> <code>blocks per group, </code><code>32768</code> <code>fragments per group</code>

<code>8192</code> <code>inodes per group</code>

<code>Superblock backups stored on blocks: </code>

<code> </code><code>32768</code><code>, </code><code>98304</code><code>, </code><code>163840</code><code>, </code><code>229376</code>

<code>正在写入inode表: 完成                            </code>

<code>Creating journal (</code><code>8192</code> <code>blocks): 完成</code>

<code>Writing superblocks and filesystem accounting information: 完成</code>

<code>This filesystem will be automatically checked every </code><code>35</code> <code>mounts or</code>

<code>180</code> <code>days, whichever comes first.  Use tune2fs -c or -i to </code><code>override</code><code>. </code>

<code>[root@logicserver vm1]# mount -o loop centos6.img /mnt</code>

<code>[root@logicserver vm1]# ls /mnt</code>

<code>lost+found</code>

<code>[root@logicserver vm1]# df -lh</code>

<code>文件系统       容量  已用  可用 已用%% 挂载点</code>

<code>/dev/sda2              50G  </code><code>4</code><code>.6G   43G  </code><code>10</code><code>% /</code>

<code>tmpfs                 462M     </code><code>0</code>  <code>462M   </code><code>0</code><code>% /dev/shm</code>

<code>/dev/sda1             120M   62M   50M  </code><code>56</code><code>% /boot</code>

<code>/dev/sda5             400G   41G  339G  </code><code>11</code><code>% /data0</code>

<code>/xen/vm1/centos6.img  976M  </code><code>1</code><code>.3M  908M   </code><code>1</code><code>% /mnt</code>

创建系统用到基本目录

<code>[root@logicserver mnt]# mkdir -pv etc/{init,rc.d} bin sbin lib64 dev proc sys tmp </code><code>var</code>

<code>mkdir: 已创建目录 </code><code>"etc"</code>

<code>mkdir: 已创建目录 </code><code>"etc/init"</code>

<code>mkdir: 已创建目录 </code><code>"etc/rc.d"</code>

<code>mkdir: 已创建目录 </code><code>"bin"</code>

<code>mkdir: 已创建目录 </code><code>"sbin"</code>

<code>mkdir: 已创建目录 </code><code>"lib64"</code>

<code>mkdir: 已创建目录 </code><code>"dev"</code>

<code>mkdir: 已创建目录 </code><code>"proc"</code>

<code>mkdir: 已创建目录 </code><code>"sys"</code>

<code>mkdir: 已创建目录 </code><code>"tmp"</code>

<code>mkdir: 已创建目录 </code><code>"var"</code>

<code>[root@logicserver mnt]# ls</code>

<code>bin  dev  etc  lib64  lost+found  proc  sbin  sys  tmp  </code><code>var</code>

执行脚本程序移植

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

<code>[root@logicserver ~]# vim bincp.sh</code>

<code>#!/bin/bash</code>

<code>#</code>

<code>declare -i DebugLevel=</code><code>0</code>

<code>Target=/mnt</code>

<code>[ -d $Target ] || mkdir $Target &amp;&gt; /dev/</code><code>null</code>

<code>read -p </code><code>"A command: "</code> <code>Command</code>

<code>while</code> <code>[ $Command != </code><code>'q'</code> <code>-a $Command != </code><code>'Q'</code> <code>]; </code><code>do</code>

<code>Command=`which $Command | grep -v </code><code>"^alias"</code> <code>| grep -o </code><code>"[^[:space:]]*"</code><code>`</code>

<code>[ $DebugLevel -eq </code><code>1</code> <code>] &amp;&amp; echo $Command</code>

<code>ComDir=${Command%</code><code>/*}</code>

<code>[ $DebugLevel -eq 1 ] &amp;&amp; echo $ComDir</code>

<code>[ -d ${Target}${ComDir} ] || mkdir -p ${Target}${ComDir}</code>

<code>[ ! -f ${Target}${Command} ] &amp;&amp; cp $Command ${Target}${Command} &amp;&amp; echo "Copy $Command to $Target finished."</code>

<code>for Lib in `ldd $Command | grep -o "[^[:space:]]*/</code><code>lib[^[:space:]]*"`; </code><code>do</code>

<code>  </code><code>LibDir=${Lib%/*} </code>

<code>  </code> 

<code>  </code><code>[root@logicserver ~]# sh bincp.sh </code>

<code>A command: init</code>

<code>Copy /sbin/init to /mnt finished.</code>

<code>Copy /lib64/libnih.so.</code><code>1</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libnih-dbus.so.</code><code>1</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libdbus-</code><code>1</code><code>.so.</code><code>3</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libpthread.so.</code><code>0</code> <code>to /mnt finished.</code>

<code>Copy /lib64/librt.so.</code><code>1</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libgcc_s.so.</code><code>1</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libc.so.</code><code>6</code> <code>to /mnt finished.</code>

<code>Copy /lib64/ld-linux-x86-</code><code>64</code><code>.so.</code><code>2</code> <code>to /mnt finished.</code>

<code>A command: bash</code>

<code>Copy /bin/bash to /mnt finished.</code>

<code>Copy /lib64/libtinfo.so.</code><code>5</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libdl.so.</code><code>2</code> <code>to /mnt finished.</code>

<code>A command: ls</code>

<code>Copy /bin/ls to /mnt finished.</code>

<code>Copy /lib64/libselinux.so.</code><code>1</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libcap.so.</code><code>2</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libacl.so.</code><code>1</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libattr.so.</code><code>1</code> <code>to /mnt finished.</code>

<code>A command: cat</code>

<code>Copy /bin/cat to /mnt finished.</code>

<code>A command: vim</code>

<code>Copy /usr/bin/vim to /mnt finished.</code>

<code>Copy /lib64/libm.so.</code><code>6</code> <code>to /mnt finished.</code>

<code>Copy /usr/lib64/libgpm.so.</code><code>2</code> <code>to /mnt finished.</code>

<code>Copy /usr/lib64/perl5/CORE/libperl.so to /mnt finished.</code>

<code>Copy /lib64/libresolv.so.</code><code>2</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libnsl.so.</code><code>1</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libcrypt.so.</code><code>1</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libutil.so.</code><code>1</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libfreebl3.so to /mnt finished.</code>

<code>A command: wget</code>

<code>Copy /usr/bin/wget to /mnt finished.</code>

<code>Copy /usr/lib64/libssl.so.</code><code>10</code> <code>to /mnt finished.</code>

<code>Copy /usr/lib64/libcrypto.so.</code><code>10</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libgssapi_krb5.so.</code><code>2</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libkrb5.so.</code><code>3</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libcom_err.so.</code><code>2</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libk5crypto.so.</code><code>3</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libz.so.</code><code>1</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libkrb5support.so.</code><code>0</code> <code>to /mnt finished.</code>

<code>Copy /lib64/libkeyutils.so.</code><code>1</code> <code>to /mnt finished.</code>

给init准备好配置文件并且编辑保存

<code>[root@logicserver ~]# cp /etc/init/rcS.conf /mnt/etc/init/</code>

<code>[root@logicserver ~]# vim /mnt/etc/init/rcS.conf</code>

<code># rcS - runlevel compatibility</code>

<code># This task runs the old sysv-rc startup scripts.</code>

<code>start on startup</code>

<code>stop on runlevel</code>

<code>task</code>

<code># Note: there can be no previous runlevel here, </code><code>if</code> <code>we have one it's bad</code>

<code># information (we enter rc1 not rcS </code><code>for</code> <code>maintenance).  Run /etc/rc.d/rc</code>

<code># without information so that it defaults to previous=N runlevel=S.</code>

<code>exec /etc/rc.d/rc.sysinit</code>

<code>~</code>

 创建rc.sysinit脚本并用chroot验证

<code>[root@logicserver ~]# vim /mnt/etc/rc.d/rc.sysinit</code>

<code>echo -e </code><code>"\tWelcom to Xen DomU Linux"</code>

<code>/bin/bash</code>

<code>~      </code>

<code>[root@logicserver ~]# chmod +x /mnt/etc/rc.d/rc.sysinit </code>

<code>[root@logicserver ~]# chroot /mnt</code>

<code>bash-</code><code>4.1</code><code># ls</code>

<code>bin  dev  etc  lib64  lost+found  proc sbin  sys  tmp usr  </code><code>var</code>

<code>bash-</code><code>4.1</code><code>#</code>

同步sync到硬盘,并卸载

<code>[root@logicserver ~]# sync</code>

<code>[root@logicserver ~]# sync </code>

<code>[root@logicserver ~]# umount /mnt</code>

<code>[root@logicserver ~]# df -lh</code>

五、DomU的配置文件

由于他版号太长,把他们这么复制

<code> </code><code>[root@logicserver ~]# cp /boot/vmlinuz-</code><code>2.6</code><code>.</code><code>32</code><code>-</code><code>279</code><code>.el6.x86_64 /tmp/vmlinux</code>

<code>[root@logicserver ~]# cp /boot/initramfs-</code><code>2.6</code><code>.</code><code>32</code><code>-</code><code>279</code><code>.el6.x86_64.img /tmp/initramfs</code>

复制/etc/xen下例子配置文件

<code>[root@logicserver ~]# cd /etc/xen/ </code>

<code>[root@logicserver xen]# cp xmexample1 centos6</code>

修改配置文件选项有

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 = [ '' ]    /注释

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

<code>[root@logicserver xen]# vim centos6</code>

<code>#  -*- mode: python; -*-</code>

<code>#============================================================================</code>

<code># Python configuration setup </code><code>for</code> <code>'xm create'</code><code>.</code>

<code># You </code><code>use</code> <code>a separate script </code><code>for</code> <code>each</code> <code>domain you want to create, or </code>

<code># you can </code><code>set</code> <code>the parameters </code><code>for</code> <code>the domain on the xm command line.</code>

<code>#----------------------------------------------------------------------------</code>

<code># Kernel image file.</code>

<code>kernel = </code><code>"/tmp/vmlinux"</code>

<code>ramdisk=</code><code>"/tmp/initramfs"</code>

<code># Optional ramdisk.</code>

<code>#ramdisk = </code><code>"/boot/initrd.gz"</code>

<code># The domain build </code><code>function</code><code>. Default </code><code>is</code> <code>'linux'</code><code>.</code>

<code>#builder=</code><code>'linux'</code>

<code># Initial memory allocation (</code><code>in</code> <code>megabytes) </code><code>for</code> <code>the </code><code>new</code> <code>domain.</code>

<code># WARNING: Creating a domain </code><code>with</code> <code>insufficient memory may cause out of</code>

<code>#          memory errors. The domain needs enough memory to boot kernel</code>

<code>#          and modules. Allocating less than 32MBs </code><code>is</code> <code>not recommended.</code>

<code>memory = </code><code>512</code>

<code># A name </code><code>for</code> <code>your domain. All domains must have different names.</code>

<code>name = </code><code>"centos6"</code>

<code># </code><code>128</code><code>-bit UUID </code><code>for</code> <code>the domain.  The </code><code>default</code> <code>behavior </code><code>is</code> <code>to generate a </code><code>new</code> <code>UUID</code>

<code># on </code><code>each</code> <code>call to </code><code>'xm create'</code><code>.</code>

<code>#uuid = </code><code>"06ed00fe-1162-4fc4-b5d8-11993ee4a8b9"</code>

<code># List of which CPUS </code><code>this</code> <code>domain </code><code>is</code> <code>allowed to </code><code>use</code><code>, </code><code>default</code> <code>Xen picks</code>

<code>#cpus = </code><code>""</code>         <code># leave to Xen to pick</code>

<code>#cpus = </code><code>"0"</code>        <code># all vcpus run on CPU0</code>

<code>#cpus = </code><code>"0-3,5,^1"</code> <code># all vcpus run on cpus </code><code>0</code><code>,</code><code>2</code><code>,</code><code>3</code><code>,</code><code>5</code>

<code>#cpus = [</code><code>"2"</code><code>, </code><code>"3"</code><code>] # VCPU0 runs on CPU2, VCPU1 runs on CPU3</code>

<code># </code><code>Number</code> <code>of Virtual CPUS to </code><code>use</code><code>, </code><code>default</code> <code>is</code> <code>1</code>

<code>#vcpus = </code><code>1</code>

<code>vcpus = </code><code>2</code>

<code># Define network interfaces.</code>

<code># By </code><code>default</code><code>, no network interfaces are configured.  You may have one created</code>

<code># </code><code>with</code> <code>sensible defaults using an empty vif clause:</code>

<code># vif = [ </code><code>''</code> <code>]</code>

<code># or optionally </code><code>override</code> <code>backend, bridge, ip, mac, script, type, or vifname:</code>

<code># vif = [ </code><code>'mac=00:16:3e:00:00:11, bridge=xenbr0'</code> <code>]</code>

<code># or more than one </code><code>interface</code> <code>may be configured:</code>

<code># vif = [ </code><code>''</code><code>, </code><code>'bridge=xenbr1'</code> <code>]</code>

<code>vif = [ </code><code>''</code> <code>]</code>

<code># Define the disk devices you want the domain to have access to, and</code>

<code># what you want them accessible </code><code>as</code><code>.</code>

<code># Each disk entry </code><code>is</code> <code>of the form phy:UNAME,DEV,MODE</code>

<code># where UNAME </code><code>is</code> <code>the device, DEV </code><code>is</code> <code>the device name the domain will see,</code>

<code># and MODE </code><code>is</code> <code>r </code><code>for</code> <code>read-only, w </code><code>for</code> <code>read-write.</code>

<code>#disk = [ </code><code>'phy:hda1,hda1,w'</code> <code>]</code>

<code>disk = [ </code><code>'file:/xen/vm1/centos6.img,xvda,w'</code> <code>]</code>

<code># Define frame buffer device.</code>

<code># By </code><code>default</code><code>, no frame buffer device </code><code>is</code> <code>configured.</code>

<code># To create one using the SDL backend and sensible defaults:</code>

<code># vfb = [ </code><code>'sdl=1'</code> <code>]</code>

<code># This uses environment </code><code>var</code><code>iables XAUTHORITY and DISPLAY.  You</code>

<code># can </code><code>override</code> <code>that:</code>

<code># vfb = [ </code><code>'sdl=1,xauthority=/home/bozo/.Xauthority,display=:1'</code> <code>]</code>

<code># To create one using the VNC backend and sensible defaults:</code>

<code># vfb = [ </code><code>'vnc=1'</code> <code>]</code>

<code># The backend listens on </code><code>127.0</code><code>.</code><code>0.1</code> <code>port </code><code>5900</code><code>+N by </code><code>default</code><code>, where N </code><code>is</code>

<code># the domain ID.  You can </code><code>override</code> <code>both address and N:</code>

<code># vfb = [ </code><code>'vnc=1,vnclisten=127.0.0.1,vncdisplay=1'</code> <code>]</code>

<code># Or you can bind the first unused port above </code><code>5900</code><code>:</code>

<code># vfb = [ </code><code>'vnc=1,vnclisten=0.0.0.0,vncunused=1'</code> <code>]</code>

<code># You can </code><code>override</code> <code>the password:</code>

<code># vfb = [ </code><code>'vnc=1,vncpasswd=MYPASSWD'</code> <code>]</code>

<code># Empty password disables authentication.  Defaults to the vncpasswd</code>

<code># configured </code><code>in</code> <code>xend-config.sxp.</code>

<code># Set the kernel command line </code><code>for</code> <code>the </code><code>new</code> <code>domain.</code>

<code># You only need to define the IP parameters and hostname </code><code>if</code> <code>the domain's</code>

<code># IP config doesn't, e.g. </code><code>in</code> <code>ifcfg-eth0 or via DHCP.</code>

<code># You can </code><code>use</code> <code>'extra'</code> <code>to </code><code>set</code> <code>the runlevel and custom environment</code>

<code># </code><code>var</code><code>iables used by custom rc scripts (e.g. VMID=, usr= ).</code>

<code># Set </code><code>if</code> <code>you want dhcp to allocate the IP address.</code>

<code>#dhcp=</code><code>"dhcp"</code>

<code># Set netmask.</code>

<code>#netmask=</code>

<code># Set </code><code>default</code> <code>gateway.</code>

<code>#gateway=</code>

<code># Set the hostname.</code>

<code>#hostname= </code><code>"vm%d"</code> <code>% vmid</code>

<code># Set root device.</code>

<code>root = </code><code>"/dev/xvda ro"</code>

<code># Root device </code><code>for</code> <code>nfs.</code>

<code>#root = </code><code>"/dev/nfs"</code>

<code># The nfs server.</code>

<code>#nfs_server = </code><code>'192.0.2.1'</code>  

<code># Root directory on the nfs server.</code>

<code>#nfs_root   = </code><code>'/full/path/to/root/directory'</code>

<code># Sets runlevel </code><code>4</code><code>.</code>

<code>extra = </code><code>"3 selinux=0"</code>

<code># Configure the behaviour when a domain exits.  There are three </code><code>'reasons'</code>

<code># </code><code>for</code> <code>a domain to stop: poweroff, reboot, and crash.  For </code><code>each</code> <code>of these you</code>

<code># may specify:</code>

<code>#   </code><code>"destroy"</code><code>,        meaning that the domain </code><code>is</code> <code>cleaned up </code><code>as</code> <code>normal;</code>

<code>#   </code><code>"restart"</code><code>,        meaning that a </code><code>new</code> <code>domain </code><code>is</code> <code>started </code><code>in</code> <code>place of the old</code>

<code>#                     one;</code>

<code>#   </code><code>"preserve"</code><code>,       meaning that no clean-up </code><code>is</code> <code>done until the domain </code><code>is</code>

<code>#                     manually destroyed (using xm destroy, </code><code>for</code> <code>example); or</code>

<code>#   </code><code>"rename-restart"</code><code>, meaning that the old domain </code><code>is</code> <code>not cleaned up, but </code><code>is</code>

<code>#                     renamed and a </code><code>new</code> <code>domain started </code><code>in</code> <code>its place.</code>

<code># In the event a domain stops due to a crash, you have the additional options:</code>

<code>#   </code><code>"coredump-destroy"</code><code>, meaning dump the crashed domain's core and then destroy;</code>

<code>#   "coredump-restart</code><code>', meaning dump the crashed domain'</code><code>s core and the restart.</code>

六 启动虚拟机

本文转自 zouqingyun 51CTO博客,原文链接:http://blog.51cto.com/zouqingyun/1692831,如需转载请自行联系原作者

继续阅读