KVM 创建虚拟机 建立日志
使用KVM 创建虚拟机1、KVM环境的搭建
在CentOS 6 64bit上搭建环境。系统安装时安装虚拟化即可。(进系统后也可以使用yum安装,yum-y install kvm 或yum -y group install KVM) 2、网卡的桥接 网卡桥接:brctl addbr br100 添加桥接网卡br100brctl addif br100 eth0 将桥接网卡添加到物理网口上ifconfig br100 192.168.2.2 netmask 255.255.255.0 配置接口IProute add default gw 192.168.2.1 添加网关(即时生效,重启后即丢失,需修改配置文件) # brctl show (查看桥接的接口列表)bridge name bridge id STP enabled interfacesbr100 8000.001aa00b3eb6 no eth0 vnet0 vnet1 vnet2 vnet3virbr0 8000.001aa00b3eb8 yes eth1 ***对网卡的操作:ifconfig eth0 upifconfig eth0 down ifconfig br100192.168.2.2netmask 255.255.255.0 up 网卡:(文件配置)# cd /etc/sysconfig/network-scripts/# lsifcfg-eth0 ifdown-ipv6 ifup ifup-plip ifup-wirelessifcfg-eth1ifdown-isdn ifup-aliasesifup-plusbinit.ipv6-globalifcfg-lo ifdown-post ifup-bnep ifup-post net.hotplugifdown ifdown-ppp ifup-eth ifup-ppp network-functionsifdown-bnep ifdown-routesifup-ippp ifup-routesnetwork-functions-ipv6ifdown-ethifdown-sit ifup-ipv6 ifup-sitifdown-ippp ifdown-tunnelifup-isdn ifup-tunnel# cat ifcfg-eth0DEVICE="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# 网关:# cat /etc/sysconfig/networkNETWORKING=yes 开启网络GATEWAY=2192.168.2.1 制定网关HOSTNAME=kvm14 主机名称(需重启后生效)# DNS:# cat /etc/resolv.confnameserver 8.8.8.8nameserver 168.95.1.1 网络重启:service network restart/etc/init.d/network restart
3、模板的制作--Create the disk image
*qemu-img create -f qcow2 tmp.qcow210G qemu-img create -f qcow2 centos.img10G生成10img,格式:qcow2,这种格式随容量大小变化,直到超过限定值。生成很快,文件也很小,拷贝很方便。*qemu-img create -f raw windowsxp.img 10G 生成raw格式磁盘*dd if=/dev/zero of=disk.img bs=1G count=5 dd写入生成,较慢。 # qemu-img create -f qcow2 centos56.img 10GFormatting 'centos56.img', fmt=qcow2 size=10737418240 encryption=off cluster_size=0# ls -lhtotal 258M-rw-r--r--. 1 root root 256K Oct 14 18:07 centos56.img# *对于磁盘格式,在测试中使用qcow2、dd生成img能正常开启虚拟机,但在启动中,cpu利用率很高,卡在一界面不动,如截图:
*配置文件中的磁盘类型的指定错去会导致vnc不能开启。*磁盘类型的配置:
<driver name='qemu' type='raw' cache='none'/>or<driver name='qemu' cache='none'/> 4、Boot the image 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 1024-vnc :0 通过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'>
<driver name='qemu' 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 工具生成虚拟机。 # virsh list 查看当前运行的虚拟机列表 Id Name State---------------------------------- 17 centos1 running 18 centos2 running 19 centos3 running ## 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是个新手。记录下来便于交流学习。谢谢!
写的真的很不错 流氓不可怕,就怕流氓有文化。 小手一抖,钱钱到手! 漏洞与补丁齐飞,蓝屏共死机一色! 不在课堂上沉睡,就在酒桌上埋醉。 有道理。。。
页:
[1]