使用 KVM 创建虚拟机



使用 KVM 创建虚拟机


 



1 KVM 环境的搭建






在CentOS 6 64bit上搭建环境。

系统安装时安装虚拟化即可。(进系统后也可以使用yum安装,yum -y install kvm 或yum -y group install KVM)

 



2 、网卡的桥接  



网卡桥接

brctl addbr br100         添加桥接网卡br100
brctl addif br100 eth0    将桥接网卡添加到物理网口上
ifconfig br100 192.168.2.2 netmask 255.255.255.0   配置接口IP
route add default gw  192.168.2.1   添加网关(即时生效,重启后即丢失,需修改配置文件)

 

[root@kvm001 kvm14]# brctl show    (查看桥接的接口列表)

bridge name     bridge id               STP enabled     interfaces
br100           8000.001aa00b3eb6       no              eth0
                                                        vnet0
                                                        vnet1
                                                        vnet2
                                                        vnet3
virbr0          8000.001aa00b3eb8       yes             eth1

 

***对网卡的操作:

ifconfig eth0 up
ifconfig eth0 down 
ifconfig br100  192.168.2.2 netmask 255.255.255.0 up

网卡:(文件配置)

[root@kvm001 kvm14]# cd /etc/sysconfig/network-scripts/
[root@kvm001 network-scripts]# ls
ifcfg-eth0   ifdown-ipv6    ifup          ifup-plip    ifup-wireless
ifcfg-eth1   ifdown-isdn    ifup-aliases  ifup-plusb   init.ipv6-global
ifcfg-lo     ifdown-post    ifup-bnep     ifup-post    net.hotplug
ifdown       ifdown-ppp     ifup-eth      ifup-ppp     network-functions
ifdown-bnep  ifdown-routes  ifup-ippp     ifup-routes  network-functions-ipv6
ifdown-eth   ifdown-sit     ifup-ipv6     ifup-sit
ifdown-ippp  ifdown-tunnel  ifup-isdn     ifup-tunnel
[root@kvm001 network-scripts]# cat ifcfg-eth0
DEVICE="eth0"                 设备名称
HWADDR="00:1A:A0:0B:3E:B6"    硬件mac地址
IPADDR= 192.168.2.2           IP地址
NM_CONTROLLED="yes"
ONBOOT="yes"                  开机启动
GATEWAY= 192.168.2.1         配置网关
BOOTPROTO=static
[root@dgcloud001 network-scripts]#

网关:

[root@dgcloud001 network-scripts]# cat /etc/sysconfig/network
NETWORKING=yes                  开启网络
GATEWAY=2 192.168.2.1           制定网关
HOSTNAME=kvm14                  主机名称(需重启后生效)
[root@dgcloud001 network-scripts]#

DNS:

[root@dgcloud001 network-scripts]# cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 168.95.1.1

 

网络重启:

service network restart
/etc/init.d/network restart



 



3 、模板的制作 -- Create the disk p_w_picpath



  



*qemu-img create -f qcow2 tmp.qcow2  10G
 
  
    qemu-img create -f qcow2 centos.img  10G



生成10img,格式:qcow2,这种格式随容量大小变化,直到超过限定值。生成很快,文件也很小,拷贝很方便。



  *qemu-img create -f raw windowsxp.img 10G



    生成 raw 格式磁盘



  *dd if=/dev/zero of=disk.img bs=1G count=5



    dd 写入生成,较慢。



 


[root@kvm001 centos5.6]# qemu-img create -f qcow2 centos56.img 10G
Formatting 'centos56.img', fmt=qcow2 size=10737418240 encryption=off cluster_size=0
[root@kvm001 centos5.6]# ls -lh
total 258M
-rw-r--r--. 1 root root 256K Oct 14 18:07 centos56.img
[root@kvm001 centos5.6]#


 



* 对于磁盘格式,在测试中使用 qcow2 、 dd 生成 img 能正常开启虚拟机,但在启动中, cpu 利用率很高,卡在一界面不动,如截图:








* 配置文件中的磁盘类型的指定错去会导致 vnc 不能开启。


*磁盘类型的配置:


<driver name='qemu' type='raw' cache='none'/>
   
or
   
<driver name='qemu' cache='none'/>


4、Boot the p_w_picpath for an install

(网上截取文段)

note in Scientific linux the qemu-kvm binary is located in /usr/libexec/qemu-kvm, you could create a symlink for it if you wish.

 
/usr/libexec/qemu-kvm -hda disk.img -cdrom archlinux-2009.08-core-x86_64.iso -m 512 -boot d -vnc :0


      
Once it is booted up you can just follow the instructions on installing your guest OS.


实际操作示例:


/usr/libexec/qemu-kvm -hdb centos56.img –cdrom CentOS-6.0-i386-minimal.iso -boot d -m 1024  -vnc :0

img文件在hdb盘里寻找、iso 从光驱启动、配置从D盘启动,内存指定1024M,指定VNC连接,端口5900.

如果提示找不到镜像文件可以多试着运行命令几次,也可以修改添加img以及iso具体路径。

如:


/usr/libexec/qemu-kvm -hdb centos56.img -cdrom     /root/centos5.6/CentOS-5.6- x86_64-bin-DVD-1of2.iso 
    -m 1024 -boot d -vnc :0




执行命令后,如果没有报错,即可使用vnc连接界面进行远程系统的安装。

通过vnc安装系统和实机操作一样,因是做模板用,如没有其他需求,安装基本系统即可。

系统安装完成后会提示重启,重启会报错--找不到驱动(截图如下)。



此时要修改启动盘位置,重启启动命令:


 

/usr/libexec/qemu-kvm -hdb centos56.img –cdrom CentOS-6.0-i386-minimal.iso -boot c -m

 

通过vnc连接可正常进入系统。模板到此安装完成,可批量拷贝使用。




5 、在KVM上建立虚拟机


需要文件:刚安装好的img文件、xml配置文件。




配置文件示例:


<domain type='kvm' id='5'>
   
  <name>centos1</name>
   
  <uuid>e78a3b2f-2c64-4042-8942-590d5a701234</uuid>
   
  <memory>1536000</memory>                                   配置虚拟机内存
   
  <currentMemory>1536000</currentMemory>
   
  <vcpu>2</vcpu>                                                        虚拟机虚拟 
   cpu 
   个数
   
  <os>
   
    <type arch='x86_64' machine='pc'>hvm</type>
   
    <boot dev='hd'/>                                                 启动类型
   
  </os>
   
  <features>
   
    <acpi/>
   
    <apic/>
   
    <pae/>
   
  </features>
   
  <clock offset='utc'/>
   
  <on_poweroff>destroy</on_poweroff>      对虚拟机的操作。 
   shutdown 
   、 
   reboot 
   没效果 
   destroy 
   很有用。
   
  <on_reboot>restart</on_reboot>
   
  <on_crash>restart</on_crash>
   
  <devices>                                                       设备配置行
   
    <emulator>/usr/libexec/qemu-kvm</emulator>
   
    <disk type='file' device='disk'>
   
type='raw' cache='none'/>                  这里修改磁盘类型
   
      <source file='/root/centos1/centos56.img'/> img    路径,路径错误会提示的。
   
      <target dev='hda' bus='ide'/>
   
    </disk>
   
    <interface type='bridge'>                                     网卡类型
   
      <mac address='50:b2:00:4c:a5:61'/>                  设定的 
   mac 
   地址(开启虚拟机多的话最好使用DHCP,要不然每台虚拟机配置IP都很麻烦)
   
      <source bridge='br100'/>                                   桥接的网卡口
   
      <target dev='vnet0'/>
   
      <model type='virtio' />
   
    </interface>
   
    <input type='mouse' bus='ps2'/>
   
    <graphics type='vnc' port='5901' listen='0.0.0.0' autoport='no' keymap='en-us'/>
   
  </devices>                                                                  
   
 </domain>
   
    
 
   
 *vnc    端口固定,也可以自动获取 
   --autoport='yes'.
   
 *    也可以配置密码,添加语句:password=‘password’


6 、配置文件写好后即可使用virsh 工具生成虚拟机。


 


[root@kvm vmpool]#     virsh list         
   查看当前运行的虚拟机列表
   
 Id Name                 State
   
----------------------------------
   
 17 centos1           running
   
 18 centos2           running
   
 19 centos3           running
   
    
 
   
[root@kvm vmpool]#
   
[root@kvm vmpool]# virsh destroy 17    此命令丢弃已生成的虚拟机


 


执行virsh create 后,如没有错误,虚拟即被开起来。如果报错可根据报错提示修改xml配置文件。


 


生成后即可使用vnc连接虚拟机。


 


vnc 也可以配置密码,示例如下


<graphics type='vnc' port='-1' autoport='yes' passwd='password' keymap='en-us'/>
   
or
   
<graphics type='vnc' port='-1' autoport='yes' listen='192.168.1.5' passwd='YOUR-PASSWORD-HERE' keymap='en-us'/>




通过vnc连接虚拟机后即可配置IP,使网络畅通。




记录在最后:之前有看过kvm的官方文档,太复杂,一直没有实际操作。对于Linux是个新手。记录下来便于交流学习。谢谢!




转载于:https://blog.51cto.com/c371193852/688598