环境介绍:
redhat6.5_x64 basic_server安装
iptables及selinux禁用
光盘作为yum源
1、BIOS开启虚拟化
Linux中x64的环境中才能使用kvm(如果是vmware,需要在CPU那里启用虚拟化)
2、检测是否支持虚拟化
1
|
egrep --color "(vmx|svm)" /proc/cpuinfo
|
VMX:它是Intel处理器虚拟机技术标志;
SVM:它是AMD处理器虚拟机技术的标志;
3、加载并查看内核模块
1
2
3
|
modprobe kvm modprobe kvm_intel(AWD加载kvm_amd) lsmod| grep kvm
|
4、安装kvm 和虚拟化管理软件包
1
2
3
|
yum -y install kvm virt-* libvirts bridge-utils qemu-img
service libvirtd start;chkconfig libvirtd on service acpid start; chkconfig acpid on |
5、配置网络桥接
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
cd /etc/sysconfig/network-scripts/
cp ifcfg-eth0 ifcfg-br0
做相应修改 #cat ifcfg-eth0 DEVICE=eth0 BOOTPROTO=none NM_CONTROLLED=no ONBOOT= yes
TYPE=Ethernet BRIDGE=br0 #cat ifcfg-br0 DEVICE=br0 BOOTPROTO=none NM_CONTROLLED=no ONBOOT= yes
TYPE=Bridge IPADDR=192.168.1.2 NETMASK=255.255.255.0 GATEWAY=192.168.1.253 |
配置桥接网卡br0后,物理网卡eth0不需要获得ip地址,其功能被br0取代,因此在配置的时候无论是静态ip还是动态获取都用br0取代eth0的ip地址,
而实际上数据发送还是通过本机的物理网卡。
6、复制安装光盘镜像
1
2
|
mkdir /iso
dd if = /dev/cdrom of= /iso/centos6 .5.iso
|
7、创建虚拟磁盘存放目录
1
|
mkdir /data/vmdisk -p
|
8、开启VNC监听IP
1
2
|
sed -i 's/^#vnc_listen.*/vnc_listen = "0.0.0.0"/' /etc/libvirt/qemu .conf
/etc/init .d /libvirtd restart
|
9、创建磁盘镜像文件
1
2
|
#virt-install --name=centos6.5-01 --ram=4096 --vcpus=2 --disk=/data/vmdisk/centos6.5-01.raw,size=40,bus=virtio,cache=writeback --network bridge=br0\ --accelerate --cdrom= /iso/centos6 .5.iso --vnc --vncport=5900 -d
|
默认镜像文件为raw格式,可用qemu-img info IMG 查看
--name 指定虚拟机名字
--ram 内存,单位M
--vcpus 分配虚拟机cpu个数
--disk 指定虚拟机安装文件路径
size 虚拟机文件大小,单位G
bus 虚拟机网卡驱动类型
cache 虚拟机磁盘cache类型
--network bridge 指定桥接网卡
--accelerate 使用内核加速功能
--cdrom 安装镜像全路径
--vnc 可以用vnc连接
--vncport 指定vnc端口,-1表示随机生成
-d debug打印调试信息到终端
VNC连接192.168.1.2:5900端口进行后续安装
Other:
kvm安装Windows与装linux类似,但是Windows需要安装额外的驱动文件,一个是硬盘的,一个是网卡的
分别为virtio-win-0.1.96_amd64.vfg和virtio-win-0.1-100.iso
不同的版本与架构,名称也不一样,可在这两个地址下载
硬盘驱动 https://fedoraproject.org/wiki/Windows_Virtio_Drivers#Direct_download.com
网卡驱动 https://fedorapeople.org/groups/virt/virtio-win/deprecated-isos/
1、创建磁盘镜像
1
|
qemu-img create -f qcow2 windows7.qcow2 40G |
2、创建xml文件(/etc/libvirt/qemu)
xml内容如末尾所示
3、定义虚拟机,让kvm来管理
1
|
virsh define windows7.xml |
4、启动虚拟机
1
|
virsh start windows7 |
下边是windows的xml文件,重点在于两个virto文件的加载。当然,也可以用命令生产xml文件,不过我更喜欢复制修改xml
一般主要修改或注释UUID、虚拟机名字及配置、MAC地址、网桥名字、磁盘类型、镜像或驱动或iso路径、vnc或spice端口号
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
|
[root@controller1 ~] # cat /etc/libvirt/qemu/win7-02.xml
<!-- WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE OVERWRITTEN AND LOST. Changes to this xml configuration should be made using: virsh edit windows7
or other application using the libvirt API. --> <domain type = 'kvm' >
<name>windows7< /name >
<uuid>6ce14f21-33ac-dc79-5444-f64ccc80ea17< /uuid >
<memory unit= 'KiB' >2097152< /memory >
<currentMemory unit= 'KiB' >2097152< /currentMemory >
<vcpu placement= 'static' >1< /vcpu >
<os>
< type arch= 'x86_64' machine= 'rhel6.5.0' >hvm< /type >
<boot dev= 'cdrom' />
< /os >
<features>
<acpi/>
<apic/>
<pae/>
< /features >
<cpu mode= 'host-passthrough' >
<topology sockets= '1' cores= '4' threads= '1' />
< /cpu >
<clock offset= 'localtime' />
<on_poweroff>destroy< /on_poweroff >
<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 = 'qcow2' />
< source file = '/data/vmdisk/win7-02.qcow2' />
<target dev= 'vda' bus= 'virtio' />
<address type = 'pci' domain= '0x0000' bus= '0x00' slot= '0x04' function = '0x0' />
< /disk >
<disk type = 'file' device= 'cdrom' >
<driver name= 'qemu' type = 'raw' />
< source file = '/iso/win7.iso' />
<target dev= 'hda' bus= 'ide' />
< readonly />
<address type = 'drive' controller= '0' bus= '0' target= '0' unit= '0' />
< /disk >
<disk type = 'file' device= 'cdrom' >
<driver name= 'qemu' type = 'raw' />
< source file = '/iso/virtio-win-0.1-100.iso' />
<target dev= 'hdb' bus= 'ide' />
< readonly />
<address type = 'drive' controller= '0' bus= '0' target= '0' unit= '1' />
< /disk >
<disk type = 'file' device= 'floppy' >
<driver name= 'qemu' type = 'raw' />
< source file = '/iso/virtio-win-0.1.96_amd64.vfg' />
<target dev= 'fda' bus= 'fdc' />
< readonly />
<address type = 'drive' controller= '0' bus= '0' target= '0' unit= '0' />
< /disk >
<controller type = 'usb' index= '0' >
<address type = 'pci' domain= '0x0000' bus= '0x00' slot= '0x01' function = '0x2' />
< /controller >
<controller type = 'ide' index= '0' >
<address type = 'pci' domain= '0x0000' bus= '0x00' slot= '0x01' function = '0x1' />
< /controller >
<controller type = 'fdc' index= '0' />
<controller type = 'virtio-serial' index= '0' >
<address type = 'pci' domain= '0x0000' bus= '0x00' slot= '0x05' function = '0x0' />
< /controller >
<interface type = 'bridge' >
<mac address= 'fa:c6:3e:a3:e4:a7' />
< source bridge= 'br0' />
<model type = 'virtio' />
<address type = 'pci' domain= '0x0000' bus= '0x00' slot= '0x03' function = '0x0' />
< /interface >
<serial type = 'file' >
< source path= '/tmp/console.log' />
<target port= '0' />
< /serial >
<serial type = 'pty' >
<target port= '1' />
< /serial >
<console type = 'file' >
< source path= '/tmp/console.log' />
<target type = 'serial' port= '0' />
< /console >
<channel type = 'spicevmc' >
<target type = 'virtio' name= 'com.redhat.spice.0' />
<address type = 'virtio-serial' controller= '0' bus= '0' port= '1' />
< /channel >
<input type = 'tablet' bus= 'usb' />
<input type = 'mouse' bus= 'ps2' />
<graphics type = 'spice' port= '6789' autoport= 'no' listen= '0.0.0.0' keymap= 'en-us' >
<listen type = 'address' address= '0.0.0.0' />
<channel name= 'main' mode= 'insecure' />
<channel name= 'display' mode= 'insecure' />
<channel name= 'inputs' mode= 'insecure' />
<channel name= 'cursor' mode= 'insecure' />
<channel name= 'playback' mode= 'insecure' />
<channel name= 'record' mode= 'insecure' />
<channel name= 'smartcard' mode= 'insecure' />
<channel name= 'usbredir' mode= 'insecure' />
<image compression= 'auto_glz' />
<streaming mode= 'all' />
<clipboard copypaste= 'yes' />
< /graphics >
<video>
<model type = 'qxl' ram = '65536' vram= '65536' heads= '1' >
<acceleration accel3d= 'yes' accel2d= 'yes' />
< /model >
<address type = 'pci' domain= '0x0000' bus= '0x00' slot= '0x02' function = '0x0' />
< /video >
<memballoon model= 'virtio' >
<address type = 'pci' domain= '0x0000' bus= '0x00' slot= '0x06' function = '0x0' />
< /memballoon >
< /devices >
< /domain >
[root@controller1 ~] #
|