需要注意的地方:
!!!如果修改了虚拟机XML配置,需要停止虚拟机,然后使用以下命令重新读取XML配置!!!:
virsh define /etc/libvirt/qemu/centos7.xml
Linux服务器两块独立的网卡最好不要配置同一网段的IP地址,不然有时会有些Bug。
virsh edit name 可以更改虚拟机配置
如果虚拟机需要使用双网卡,virsh edit新增一块网卡的配置,并且将新增的网卡挂载到宿主机另一块网卡上即可。挂载在同一块上也行
新增一个网卡:
virsh attach-interface cent_xu --type bridge --source br0
virsh dumpxml cent_xu > /etc/libvirt/qemu/cent_xu.xml
KVM 是基于 x86 虚拟化扩展(Intel VT 或者 AMD-V) 技术的虚拟机软件,所以查看 CPU 是否支持 VT 技术,就可以判断是否支持KVM。有返回结果,如果结果中有vmx(Intel)或svm(AMD)字样,就说明CPU的支持的。
cat /proc/cpuinfo | egrep 'vmx|svm'
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm arat epb pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm xsaveopt cqm_llc cqm_occup_llc
关闭SELinux,将 /etc/sysconfig/selinux 中的 SELinux=enforcing
修改为 SELinux=disabled
通过 yum 安装 kvm 基础包和管理工具
# 安装 kvm 基础包
# ------------------------
yum -y install kvm
# 安装 kvm 管理工具
# ------------------------
yum -y install qemu-kvm python-virtinst libvirt libvirt-python virt-manager libguestfs-tools bridge-utils virt-install
# qemu-kvm: KVM模块
# pyhon-virtinst: 包含python模块和工具(virt-install,virt-clone和virt-image),
# 用于安装和克隆虚拟机使用libvirt。 它完全支持paravirtulized客人和客人虚拟客人。
# 支持的虚拟机管理程序是Xen,qemu(QEMU)和kvm
# libvirt: 虚拟管理模块
# virt-manager: 图形界面管理虚拟机
# libguestfs* : virt-cat等命令的支持软件包
# 重启宿主机,以便加载 kvm 模块
# ------------------------
reboot
# 查看KVM模块是否被正确加载
# ------------------------
lsmod | grep kvm
kvm_intel 162153 0
kvm 525259 1 kvm_intel
开启kvm服务,并且设置其开机自动启动
systemctl start acpid.service
systemctl enable acpid.service
systemctl start libvirtd.service
systemctl enable libvirtd.service
查看状态操作结果,如Active: active (running)
,说明运行情况良好
systemctl status libvirtd
systemctl is-enabled libvirtd
● libvirtd.service - Virtualization daemon
Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
Active: active (running) since 二 2001-01-02 11:29:53 CST; 1h 41min ago
Docs: man:libvirtd(8)
http://libvirt.org
安装KVM成功后会在宿主机上自动生成一个虚拟网桥,用来虚拟机和宿主机之间的通信
下面来安装虚拟机:
安装前要设置环境语言为英文LANG="en_US.UTF-8"
,如果是中文的话某些版本可能会报错。CentOS 7
在这里修改 /etc/locale.conf
。
kvm创建虚拟机,特别注意.iso
镜像文件一定放到/home
或者根目录重新创建目录,不然会因为权限报错,无法创建虚拟机。
注:下列两条命令都是在黑窗中进行系统配置然后安装
virt-install --virt-type=kvm --name=centos88 --vcpus=2 --memory=4096 --location=/tmp/CentOS-7-x86_64-Minimal-1511.iso --disk path=/home/vms/centos88.qcow2,size=40,format=qcow2 --network bridge=br0 --graphics none --extra-args='console=ttyS0' --force
virt-install --virt-type=kvm --name=centos74_2 --vcpus=2 --memory=4096 --location=/home/ISO/CentOS-7-x86_64-Minimal-1708.iso --disk path=/home/vms/centos74_2.qcow2,size=40,format=qcow2 --network bridge=br0 --graphics none --extra-args='console=ttyS0' --force
提供另一种方式:在服务器上只完成虚拟机的配置信息和镜像的引导,系统的安装通过VNC远程登录系统进行
virt-install --connect qemu:///system --name kali --memory=4096 --vcpus=2 --disk path=/home/vms/kali.qcow2,device=disk,format=qcow2,bus=virtio,cache=none,size=50 --cdrom /home/ISO/kali-linux-kde-amd64.iso --os-type=linux --network bridge=br0,model=virtio,model=e1000 --hvm --virt-type=kvm --noautoconsole --graphics vnc,listen=0.0.0.0,port=5901
安装WindowsServer2008R2(硬盘格式为ide,即bus=ide)
virt-install --connect qemu:///system --name windows --memory=4096 --vcpus=2 --disk path=/home/vms/windows.qcow2,device=disk,format=qcow2,bus=ide,cache=none,size=50 --cdrom /home/ISO/cn_windows_server_2008_r2_standard_enterprise_datacenter_web_x64_dvd_x15-50360.iso --os-type=windows --network bridge=br0,model=virtio,model=e1000 --hvm --virt-type=kvm --noautoconsole --graphics vnc,listen=0.0.0.0,port=5902
KVM所在服务器显示如下:
通过VNC远程登录虚拟机后完成系统安装
上面创建虚拟机命令最终需要你配置系统基础设置,带 [!]
基本都是要配置的,按照顺序往下配置,按对用的数字以此进行设置。
Installation
1) [x] Language settings 2) [!] Timezone settings
(English (United States)) (Timezone is not set.)
3) [!] Installation source 4) [!] Software selection
(Processing...) (Processing...)
5) [!] Installation Destination 6) [x] Kdump
(No disks selected) (Kdump is enabled)
7) [ ] Network configuration 8) [!] Root password
(Not connected) (Password is not set.)
9) [!] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]:
一步步配置下去即可
通过 virsh console <虚拟机名称>
命令来连接虚拟机
# 查看虚拟机
virsh list # 查看在运行的虚拟机
virsh list –all # 查看所有虚拟机
Id Name State
----------------------------------------------------
7 centos72 running
配置虚拟机网络,编辑vi /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
IPADDR=IP #配置成与网桥同一网段的IP地址
PREFIX=24
GATEWAY=GateWay_IP #配置网关地址,用来连接外网
DNS1=114.114.114.114 # 配置两个DNS服务器地址,用于域名解析
DNS2=8.8.8.8
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=adfa3b7d-bf60-47e6-8482-871dee686fb5
DEVICE=eth0
ONBOOT=yes
一般来说,服务器可能有多个网卡,选择一个网卡当做网桥来与虚拟机通信
在/etc/sysconfig/network-scripts里新增一个文件:ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
NAME=br0
DEVICE=br0
GATEWAY=GateWay_IP # 网关IP
DNS1=114.114.114
DNS2=8.8.8.8
ONBOOT=yes
IPADDR=IP # 此处的IP其实是你宿主机上物理网卡,用来与虚拟机通信的网卡的IP
PREFIX=24
物理网卡配置:
TYPE=Ethernet
BOOTPROTO=none
NAME=eno1
DEVICE=eno1
ONBOOT=yes
BRIDGE="br0"
配置路由转发vi /etc/sysctl.conf
# Controls IP packet forwarding
net.ipv4.ip_forward = 0
修改为
# Controls IP packet forwarding
net.ipv4.ip_forward = 1 允许内置路由
Bridge方式即虚拟网桥的网络连接方式,是客户机和子网里面的机器能够互相通信。可以使虚拟机成为网络中具有独立IP的主机。桥接网络(也叫 物理设备共享)被用作把一个物理设备复制到一台虚拟机。网桥多用作高级设置,特别是主机多个网络接口的情况。
┌─────────────────────────┐ ┌─────────────────┐
│ HOST │ │Virtual Machine 1│
│ ┌──────┐ ┌───────┐ │ │ ┌──────┐ │
│ │ br0 │──┬───│ vnet0 │─│─ ─ ─ │ │ br0 │ │
│ └──────┘ │ └───────┘ │ │ └──────┘ │
│ │ │ │ └─────────────────┘
│ │ │ ┌───────┐ │ ┌─────────────────┐
│ ┌──────┐ └───│ vnet1 │─│─ │Virtual Machine 2│
│ │ eno0 │ └───────┘ │ │ │ ┌──────┐ │
│ └──────┘ │ ─ ─ │ │ br0 │ │
│ ┌──────┐ │ │ └──────┘ │
│ │ eno1 │ │ └─────────────────┘
│ └──────┘ │
└─────────────────────────┘
KVM一些常用命令:
克隆虚拟机:将74克隆到xiang
virt-clone -o centos74 -n cent_xiang -f /home/cent_xiang.qcow2
virsh list --all
virsh dominfo kvm-1
yum install virt-top -y
virt-top
virt-df kvm-1
virsh shutdown kvm-1
virsh start kvm-1
virsh autostart kvm-1
virsh autostart --disable kvm-1
virsh undefine kvm-1
virsh console kvm-1
添加硬盘(lvm卷)或者USB到虚拟机上
virsh attach-disk kvm-1 /dev/sdb vbd --driver qemu --mode shareable
virsh detach-disk kvm vdb
添加lvm卷,并挂载
[root@sh-kvm-1 ~]# lvcreate -n kvm-1-data -L 50G vg_shkvm1
[root@sh-kvm-1 ~]# virsh attach-disk kvm-1 /dev/vg_shkvm1/kvm-1-data vdb --driver qemu --mode shareable
Disk attached successfully
# 登录到kvm-1上查看lvm是否已经被挂载
[root@sh-kvm-1 ~]# virsh console kvm-1 # 输入kvm-1的用户和密码
[root@sh-kvm-1-1 ~]# fdisk -l # 查看硬盘挂载情况
Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00058197
Device Boot Start End Blocks Id System
/dev/vda1 * 3 1018 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2 1018 41611 20458496 8e Linux LVM
Partition 2 does not end on cylinder boundary.
Disk /dev/mapper/VolGroup-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/VolGroup-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/vdb: 53.7 GB, 53687091200 bytes # 新添加的硬盘
16 heads, 63 sectors/track, 104025 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
# 对新添加的硬盘分区
[root@sh-kvm-1-1 ~]# fdisk /dev/vdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xf04b6807.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): m # 查看帮助
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): n #添加一个分区
Command action
e extended
p primary partition (1-4)
p #选择添加一个扩展分区
Partition number (1-4):
Value out of range.
Partition number (1-4): 1
First cylinder (1-104025, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-104025, default 104025):
Using default value 104025
Command (m for help): t #改变分区的格式
Selected partition 1
Hex code (type L to list codes): 8e #改成lvm
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): w # 保存更改
root@sh-kvm-1-1 ~]# mkfs.ext4 /dev/vdb1 # 格式化分区
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3276800 inodes, 13107142 blocks
655357 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
400 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@sh-kvm-1-1 ~]# pvc reate /dev/vdb1 # 创建pv
vdb vdb1
[root@sh-kvm-1-1 ~]# vgextend VolGroup /dev/vdb # 扩展lvm vg
vdb vdb1
[root@sh-kvm-1-1 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup 2 2 0 wz--n- 69.50g 50.00g
# 从上面能看出,新添加的 已经加到lvm组中
通过命令行更改创建之后虚拟机的内存,cpu等信息
# 1. 查看虚拟机当前内存
[root@sh-kvm-1 ~]# virsh dominfo kvm-1 | grep memory
Max memory: 4194304 KiB
Used memory: 4194304 KiB
# 2、动态设置内存为512MB,内存减少
virsh setmem kvm-1 524288
# 注意单位必须是KB
# 3、查看内存变化
# virsh dominfo kvm-1 | grep memory
Max memory: 14194304 KiB
Used memory: 524288 kiB
# 4、内存增加
virsh shutdown kvm-1
virsh edit kvm-1 # 直接更改memory
virsh create /etc/libvirt/demu/kvm-1/xml
# 之后操作1,2,3步骤增加内存
需要修改配置文件,因此需要停止虚拟机
virsh shutdown kvm-1
virsh edit kvm-1
# 2 # 4 > 2
virsh create /etc/libvirt/demu/kvm-1/xml
1. Create a 10-GB non-sparse file:
# dd if=/dev/zero of=/vm-images/vm1-add.img bs=1M count=10240
2. Shutdown the VM:
# virsh shutdown vm1
3. Add an extra entry for ‘disk’ in the VM's XML file in /etc/libvirt/qemu. You can look copy & paste
the entry for your mail storage device and just change the target and address tags. For example:
# virsh edit vm1
Add:
# 这里建议使用上面的添加硬盘的方式添加
virsh shutdown kvm-1
virsh destroy kvm-1
virsh undefine kvm-1
rm /dev/vg_shkvm1/kvm-1 # 不建议删除硬盘
更为详细的KVM内容见:http://blog.csdn.net/github_27924183/article/details/76914322?locationNum=5&fps=1