在xen下新建一个windows虚拟机

来源:互联网 发布:怎么在淘宝开代购店 编辑:程序博客网 时间:2024/04/28 04:28
1.申请磁盘空间

dd if=/dev/zero of=./Win7.img bs=1M count=20480

dd if=/dev/zero of=./Windisk.img bs=1M count=10240

具体要几个硬盘根据需要新建。

2.上传ISO镜像,上传windows的安装镜像到服务器。

请确保上传的镜像是有效地,一开始我上传了一个镜像,一直不能成功安装虚拟机,后来重新制作了一个iso(物理光盘是一样的),就可以了。

3.建立虚拟机

virt-install -n win7 --file=Win7.img --file=Windisk.img --vnc -v -c /share/nfs02/309www/win7.iso
 -r 1024 --vcpus=1

virt-install 建立虚拟机的命令
-n 虚拟机的命名
--file 虚拟机的磁盘文件
--vnc 使用VNC终端连接方式
-r 虚拟机的内存容量
--vcpus 虚拟机的CPU数量
-v 可视
-c 安装镜像的路径

 

4.编写配置文件


1)拷贝一个系统的配置文件模板#cp /etc/xen/xmexmple.hvm ./windows.hvm
2)修改windows.hvm文件以下相关项:
name = “ win7"      #自己取个名字
vif = [  'type=ioemu, bridge=br1' ]

disk = [ 'file:/share/nfs02/309www/Win7.img,ioemu:hda,w'

,'file:/share/nfs02/309www/Windisk.img,ioemu:hdb,w'#具体需要几个硬盘根据需要挂载。

, 'file:/share/nfs02/309www/win7.iso,hdc:cdrom,r']       #前面一个是虚拟硬盘的位置,后面一个是iso安装文件所在位置,根据实际位置修改

3)boot=cd                #设置默认系统启动位置 CD-ROM(d)  hard-disk(c)
4)vncconsole=1       #类似xm cr -c 中的-c功能,使系统启动之后自动通过vnc连接到虚拟机窗口
5)usbdevice='tablet'   #据说是可以去除跟随鼠标的那个悲剧的点的,不过有的时候有效,有的时候没 - -

5.启动 xm create windows.hvm


如果没有设置vncconsole=1这个项,我们也可以通过vncviewer 127.0.0.1:5900来连接我们启动的虚拟机窗口(5900是一个端口号,可能会变,可以通过xm list -l domid | grep location 获取端口号)

 也可以通过  xm vncviewer 


6.安装windows.也可以在建立镜像是完成。


7.修改启动项,从硬盘启动。


安装完后,每次启动都要到光盘项,很麻烦,修改配置文件windows.hvm,将disk项改为disk = [ 'file:/home/wq/image/pv/hvm/vmdisk.img,ioemu:hda,w' ],boot=c

    可以通过xm

    xm vncviewer win7 启动vnc到指定虚拟机。"win7"是虚拟机的名字。

     

    ps:这是我的配置文件

    [html] view plaincopyprint?
    1. #  -*- mode: python; -*-  
    2. #============================================================================  
    3. # Python configuration setup for 'xm create'.  
    4. # This script sets the parameters used when a domain is created using 'xm create'.  
    5. # You use a separate script for each domain you want to create, or   
    6. # you can set the parameters for the domain on the xm command line.  
    7. #============================================================================  
    8.   
    9. import os, re  
    10. arch = os.uname()[4]  
    11. if re.search('64', arch):  
    12.     arch_libdir = 'lib64'  
    13. else:  
    14.     arch_libdir = 'lib'  
    15.   
    16. #----------------------------------------------------------------------------  
    17. # Kernel image file.  
    18. kernel = "/usr/lib/xen/boot/hvmloader"  
    19.   
    20. # The domain build function. HVM domain uses 'hvm'.  
    21. builder='hvm'  
    22.   
    23. # Initial memory allocation (in megabytes) for the new domain.  
    24. #  
    25. # WARNING: Creating a domain with insufficient memory may cause out of  
    26. #          memory errors. The domain needs enough memory to boot kernel  
    27. #          and modules. Allocating less than 32MBs is not recommended.  
    28. memory = 1024  
    29.   
    30. # Shadow pagetable memory for the domain, in MB.  
    31. # If not explicictly set, xend will pick an appropriate value.    
    32. # Should be at least 2KB per MB of domain memory, plus a few MB per vcpu.  
    33. shadow_memory = 8  
    34.   
    35. # A name for your domain. All domains must have different names.  
    36. name = "win7"  
    37.   
    38. # 128-bit UUID for the domain.  The default behavior is to generate a new UUID  
    39. # on each call to 'xm create'.  
    40. #uuid = "06ed00fe-1162-4fc4-b5d8-11993ee4a8b9"  
    41.   
    42. #-----------------------------------------------------------------------------  
    43. # The number of cpus guest platform has, default=1  
    44. #vcpus=1  
    45.   
    46. # Enable/disable HVM guest PAE, default=1 (enabled)  
    47. #pae=1  
    48.   
    49. # Enable/disable HVM guest ACPI, default=1 (enabled)  
    50. #acpi=1  
    51.   
    52. # Enable/disable HVM APIC mode, default=1 (enabled)  
    53. # Note that this option is ignored if vcpus > 1  
    54. #apic=1  
    55.   
    56. # List of which CPUS this domain is allowed to use, default Xen picks  
    57. #cpus = ""         # leave to Xen to pick  
    58. #cpus = "0"        # all vcpus run on CPU0  
    59. #cpus = "0-3,5,^1" # all vcpus run on cpus 0,2,3,5  
    60. #cpus = ["2", "3"] # VCPU0 runs on CPU2, VCPU1 runs on CPU3  
    61.   
    62. # Optionally define mac and/or bridge for the network interfaces.  
    63. # Random MACs are assigned if not given.  
    64. #vif = [ 'type=ioemumac=00:16:3e:00:00:11, bridge=xenbr0model=ne2k_pci' ]  
    65. type=ioemu specify the NIC is an ioemu device not netfront  
    66. vif = [ 'type=ioemu,mac=00:16:3e:3f:45:8f, bridge=br0' ]  
    67.   
    68. #----------------------------------------------------------------------------  
    69. # Define the disk devices you want the domain to have access to, and  
    70. # what you want them accessible as.  
    71. # Each disk entry is of the form phy:UNAME,DEV,MODE  
    72. # where UNAME is the device, DEV is the device name the domain will see,  
    73. # and MODE is r for read-only, w for read-write.  
    74.   
    75. #disk = [ 'phy:hda1,hda1,r' ]  
    76. disk = [ 'file:/share/nfs02/309www/Win7.img,hda,w',  
    77.      'file:/share/nfs02/309www/Windisk.img,hdb,w',  
    78.      'file:/share/nfs02/309www/win7.iso,hdc:cdrom,r' ]  
    79.   
    80. #----------------------------------------------------------------------------  
    81. # Configure the behaviour when a domain exits.  There are three 'reasons'  
    82. # for a domain to stop: poweroff, reboot, and crash.  For each of these you  
    83. # may specify:  
    84. #  
    85. #   "destroy",        meaning that the domain is cleaned up as normal;  
    86. #   "restart",        meaning that a new domain is started in place of the old  
    87. #                     one;  
    88. #   "preserve",       meaning that no clean-up is done until the domain is  
    89. #                     manually destroyed (using xm destroy, for example); or  
    90. #   "rename-restart", meaning that the old domain is not cleaned up, but is  
    91. #                     renamed and a new domain started in its place.  
    92. #  
    93. # In the event a domain stops due to a crash, you have the additional options:  
    94. #  
    95. #   "coredump-destroy", meaning dump the crashed domain's core and then destroy;  
    96. #   "coredump-restart', meaning dump the crashed domain's core and the restart.  
    97. #  
    98. # The default is  
    99. #  
    100. #   on_poweroff = 'destroy'  
    101. #   on_reboot   = 'restart'  
    102. #   on_crash    = 'restart'  
    103. #  
    104. # For backwards compatibility we also support the deprecated option restart  
    105. #  
    106. restart = 'onreboot' means on_poweroff = 'destroy'  
    107. #                            on_reboot   = 'restart'  
    108. #                            on_crash    = 'destroy'  
    109. #  
    110. restart = 'always'   means on_poweroff = 'restart'  
    111. #                            on_reboot   = 'restart'  
    112. #                            on_crash    = 'restart'  
    113. #  
    114. restart = 'never'    means on_poweroff = 'destroy'  
    115. #                            on_reboot   = 'destroy'  
    116. #                            on_crash    = 'destroy'  
    117.   
    118. #on_poweroff = 'destroy'  
    119. #on_reboot   = 'restart'  
    120. #on_crash    = 'restart'  
    121.   
    122. #============================================================================  
    123.   
    124. # Device Model to be used  
    125. device_model = '/usr/' + arch_libdir + '/xen/bin/qemu-dm'  
    126.   
    127. #-----------------------------------------------------------------------------  
    128. # boot on floppy (a), hard disk (c), Network (n) or CD-ROM (d)   
    129. # default: hard disk, cd-rom, floppy  
    130. #boot="cda"  
    131. boot="dc"  
    132.   
    133. #-----------------------------------------------------------------------------  
    134. #  write to temporary files instead of disk image files  
    135. #snapshot=1  
    136.   
    137. #----------------------------------------------------------------------------  
    138. # enable SDL library for graphics, default = 0  
    139. sdl=0  
    140.   
    141. #----------------------------------------------------------------------------  
    142. # enable OpenGL for texture rendering inside the SDL window, default = 1  
    143. # valid only if sdl is enabled.  
    144. opengl=1  
    145.   
    146. #----------------------------------------------------------------------------  
    147. # enable VNC library for graphics, default = 1  
    148. vnc=1  
    149.   
    150. #----------------------------------------------------------------------------  
    151. # address that should be listened on for the VNC server if vnc is set.  
    152. # default is to use 'vnc-listen' setting from /etc/xen/xend-config.sxp  
    153. #vnclisten="127.0.0.1"  
    154.   
    155. #----------------------------------------------------------------------------  
    156. # set VNC display number, default = domid  
    157. #vncdisplay=1  
    158.   
    159. #----------------------------------------------------------------------------  
    160. # try to find an unused port for the VNC server, default = 1  
    161. #vncunused=1  
    162.   
    163. #----------------------------------------------------------------------------  
    164. # set password for domain's VNC console  
    165. # default is depents on vncpasswd in xend-config.sxp  
    166. vncpasswd=''  
    167.   
    168. #----------------------------------------------------------------------------  
    169. # no graphics, use serial port  
    170. #nographic=0  
    171.   
    172. #----------------------------------------------------------------------------  
    173. # enable stdvga, default = 0 (use cirrus logic device model)  
    174. stdvga=0  
    175.   
    176. #-----------------------------------------------------------------------------  
    177. #   serial port re-direct to pty deivce, /dev/pts/n   
    178. #   then xm console or minicom can connect  
    179. serial='pty'  
    180.   
    181.   
    182. #-----------------------------------------------------------------------------  
    183. #   Qemu Monitor, default is disable  
    184. #   Use ctrl-alt-2 to connect  
    185. #monitor=1  
    186.   
    187.   
    188. #-----------------------------------------------------------------------------  
    189. #   enable sound card support, [sb16|es1370|all|..,..], default none  
    190. #soundhw='sb16'  
    191.   
    192.   
    193. #-----------------------------------------------------------------------------  
    194. #    set the real time clock to local time [default=0 i.e. set to utc]  
    195. #localtime=1  
    196.   
    197.   
    198. #-----------------------------------------------------------------------------  
    199. #    set the real time clock offset in seconds [default=0 i.e. same as dom0]  
    200. #rtc_timeoffset=3600  
    201.   
    202. #-----------------------------------------------------------------------------  
    203. #    start in full screen  
    204. #full-screen=1     
    205.   
    206.   
    207. #-----------------------------------------------------------------------------  
    208. #   Enable USB support (specific devices specified at runtime through the  
    209. #           monitor window)  
    210. #usb=1  
    211.   
    212. #   Enable USB mouse support (only enable one of the following, `mouse' for  
    213. #                 PS/2 protocol relative mouse, `tablet' for  
    214. #                 absolute mouse)  
    215. #usbdevice='mouse'  
    216. #usbdevice='tablet'  
    217.   
    218. #-----------------------------------------------------------------------------  
    219. #   Set keyboard layout, default is en-us keyboard.   
    220. #keymap='ja'  
    221.   
    222. #-----------------------------------------------------------------------------  
    223. #   Configure guest CPUID responses:  
    224. #  
    225. #cpuid=[ '1:ecx=xxxxxxxxxxx00xxxxxxxxxxxxxxxxxxx,  
    226. #           eax=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' ]  
    227. # - Unset the SSE4 features (CPUID.1[ECX][20-19])  
    228. # - Default behaviour for all other bits in ECX And EAX registers.  
    229. #   
    230. # Each successive character represent a lesser-significant bit:  
    231. #  '1' -> force the corresponding bit to 1  
    232. #  '0' -> force to 0  
    233. #  'x' -> Get a safe value (pass through and mask with the default policy)  
    234. #  'k' -> pass through the host bit value  
    235. #  's' -> as 'k' but preserve across save/restore and migration  
    236. #   
    237. #   Expose to the guest multi-core cpu instead of multiple processors  
    238. # Example for intel, expose a 8-core processor :  
    239. #cpuid=['1:edx=xxx1xxxxxxxxxxxxxxxxxxxxxxxxxxxx,  
    240. #          ebx=xxxxxxxx00010000xxxxxxxxxxxxxxxx',  
    241. #     '4,0:eax=001111xxxxxxxxxxxxxxxxxxxxxxxxxx']  
    242. #  - CPUID.1[EDX][HT] : Enable HT  
    243. #  - CPUID.1[EBX] : Number of vcpus * 2  
    244. #  - CPUID.4,0[EAX] : Number of vcpus * 2 - 1  
    245. #vcpus=8  
    246. #  
    247. # Example for amd, expose a 5-core processor :  
    248. cpuid = ['1:ebx=xxxxxxxx00001010xxxxxxxxxxxxxxxx,  
    249. #             edx=xxx1xxxxxxxxxxxxxxxxxxxxxxxxxxxx',  
    250. # '0x80000001:ecx=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1x',  
    251. # '0x80000008:ecx=xxxxxxxxxxxxxxxxxxxxxxxxxx001001']  
    252. #   - CPUID.1[EBX] : Threads per Core * Cores per Socket (2 * #vcpus)  
    253. #   - CPUID.1[EDX][HT] : Enable HT  
    254. #   - CPUID.0x80000001[CmpLegacy] : Use legacy method  
    255. #   - CPUID.0x80000008[ECX] : #vcpus * 2 - 1  
    256. #vcpus=5  
    257. #  
    258. #  Downgrade the cpuid to make a better compatibility for migration :  
    259. # Look like a generic 686 :  
    260. cpuid = [ '0:eax=0x3,ebx=0x0,ecx=0x0,edx=0x0',  
    261. #           '1:eax=0x06b1,  
    262. #              ecx=xxxxxxxxxx0000xx00xxx0000000xx0,  
    263. #              edx=xx00000xxxxxxx0xxxxxxxxx0xxxxxx',  
    264. #           '4:eax=0x3,ebx=0x0,ecx=0x0,edx=0x0',  
    265. #  '0x80000000:eax=0x3,ebx=0x0,ecx=0x0,edx=0x0']  
    266. #  with the highest leaf  
    267. #  - CPUID.0[EAX] : Set the highest leaf  
    268. #  - CPUID.1[EAX] : 686   
    269. #  - CPUID.1[ECX] : Mask some features  
    270. #  - CPUID.1[EDX] : Mask some features  
    271. #  - CPUID.4 : Reply like the highest leaf, in our case CPUID.3  
    272. #  - CPUID.0x80000000 : No extension we are on a Pentium III, reply like the  
    273. #  highest leaf (CPUID.3).  
    274. #  
    275. #   Configure host CPUID consistency checks, which must be satisfied for this  
    276. #   VM to be allowed to run on this host's processor type:  
    277. #cpuid_check=[ '1:ecx=xxxxxxxxxxxxxxxxxxxxxxxxxx1xxxxx' ]  
    278. # - Host must have VMX feature flag set  
    279. #  
    280. # The format is similar to the above for 'cpuid':  
    281. #  '1' -> the bit must be '1'  
    282. #  '0' -> the bit must be '0'  
    283. #  'x' -> we don't care (do not check)  
    284. #  's' -> the bit must be the same as on the host that started this VM  
    285.   
    286.   
    287. #-----------------------------------------------------------------------------  
    288. #   Configure PVSCSI devices:  
    289. #  
    290. #vscsi=[ 'PDEV, VDEV' ]  
    291. #  
    292. #   PDEV   gives physical SCSI device to be attached to specified guest  
    293. #          domain by one of the following identifier format.  
    294. #          - XX:XX:XX:XX (4-tuples with decimal notation which shows  
    295. #                          "host:channel:target:lun")  
    296. #          - /dev/sdxx or sdx  
    297. #          - /dev/stxx or stx  
    298. #          - /dev/sgxx or sgx  
    299. #          - result of 'scsi_id -gu -s'.  
    300. #            ex. # scsi_id -gu -s /block/sdb  
    301. #                  36000b5d0006a0000006a0257004c0000  
    302. #  
    303. #   VDEV   gives virtual SCSI device by 4-tuples (XX:XX:XX:XX) as   
    304. #          which the specified guest domain recognize.  
    305. #  
    306.   
    307. #vscsi = [ '/dev/sdx, 0:0:0:0' ]  
0 0