1.安装kvm
yum install libvirt virt-install qemu-kvm -y
systemctl start libvirtd.service
systemctl status libvirtd.service
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7 --memory 1024 --vcpus 1 --disk /opt/centos7.raw,format=raw,size=10 --cdrom /opt/CentOS-7-x86_64-DVD-1810.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
2.操作kvm虚拟机
virsh start centos7 启动虚拟机
virsh reboot centos7 重启虚拟机
virsh shutdown centos7 关闭虚拟机
virsh destroy centos7 关闭虚拟机
virsh dumpxml centos7 查看虚拟机配置
virsh dumpxml centos7 > centos7.xml 导出配置
virsh define centos7.xml 导入虚拟机 必须有文件
virsh domrename panghu shouhu 重命名虚拟机
virsh suspend centos7 挂起虚拟机
virsh resume centos7 恢复虚拟机
virsh autostart centos7 开机启动重启虚拟机
ll /etc/libvirt/qemu/autostart/ 创建连接kvm到该目录
virsh edit centos7 编辑虚拟机
virsh console centos7
ssh 192.168.122.102
centos6的打开方法:
grubby --update-kernel=ALL --args="console=ttyS0,115200n8"
grep "115200" /boot/grub2/grub.cfg
reboot
centos7的打开方法:
systemctl start serial-getty@ttyS0.service
systemctl enable serial-getty@ttyS0.service
virsh console centos7 输入账户密码
ctrl + ] 退出
3.克隆KVM
1.完整克隆
virsh shutdown centos7
virt-clone --auto-clone -o centos7 -n centos7-backup
virsh list --all
virsh dumpxml web-blog-backup |grep "qcow2"
virsh snapshot-list centos7
2.链接克隆
qemu-img create -b /home/kvm/base.qcow2 -f qcow2 /home/kvm/node5.qcow2
qemu-img info node5.qcow2
virsh dumpxml centos7 > centos7-clone.xml
sed -i '/uuid/d' centos7-clone.xml
sed -i '/mac address/d' centos7-clone.xml
sed -i 's#centos7.qcow2#centos7-clone.qcow2#g' centos7-clone.xml
sed -i '/\<nam/s#centos7#centos7-clone#g' centos7-clone.xml
/etc/libvirt/qemu 配置文件 复制一个 修改 uuid name mac地址
virsh define centos7-clone.xml 导入就正常了
3.桥接网络
virsh iface-bridge eth0 br0
qemu-img create -f qcow2 -b centos7.qcow2 bridge.qcow2
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7-bridge --memory 1024 --vcpus 1 --disk /opt/bridge.qcow2 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole
4.添加硬盘
qemu-img create -f qcow2 centos7-add.qcow2 10G
临时添加
virsh attach-disk centos7 /opt/centos7-add.qcow2 vdb --subdriver qcow2
永久添加
virsh attach-disk centos7 /opt/centos7-add.qcow2 vdb --subdriver qcow2
virsh attach-disk centos7 /opt/centos7-add.qcow2 vdb --subdriver qcow2 --config
5.剥离硬盘
virsh detach-disk centos7 vdb 临时剥离
virsh detach-disk centos7 vdb 永久剥离
virsh detach-disk centos7 vdb --config
6.调整硬盘
qemu-img info /opt/centos7-add.qcow2 附加盘才可以调整
qemu-img resize /opt/centos7-add.qcow2 +10G
virsh attach-disk centos7 /opt/centos7-add.qcow2 vdb --subdriver qcow2
virsh console centos7
fdisk -l /dev/vdb
mount /dev/vdb /mnt/
df -h|tail -1
xfs_growfs /dev/vdb
df -h|tail -1
7.快照
virsh snapshot-list centos7 查看快照
virsh snapshot-create-as centos7 snap1 创建快照可以命名
virsh snapshot-list centos7 创建快照无法命名
virsh snapshot-revert centos7 snap1 恢复快照
virsh snapshot-delete centos7 snap1 删除快照
8.kvm 添加网卡
virsh attach-interface centos7 --type bridge --mac 52:54:00:b1:b5:8a --source br0 --model virtio detachinterface 临时添加网卡
virsh attach-interface centos7 --type bridge --mac 52:54:00:b1:b5:8a --source br0 --model virtio detachinterface --config 永久添加网卡
virsh detach-interface centos7 bridge 临时分离网卡
virsh detach-interface centos7 bridge 永久分离网卡
virsh detach-interface centos7 bridge --config
杂项
[root@localhost ~]# yum history list
已加载插件:fastestmirror
ID | 登录用户 | 日期和时间 | 操作 | 变更数
-------------------------------------------------------------------------------
8 | root <root> | 2020-12-23 10:36 | Reinstall | 1 ##
7 | root <root> | 2020-12-21 16:52 | Install | 1 EE
6 | root <root> | 2020-12-21 16:07 | I, U | 9
5 | root <root> | 2020-12-19 14:02 | Install | 5
4 | root <root> | 2020-12-19 09:08 | I, U | 24
3 | root <root> | 2020-11-02 17:44 | Install | 10
2 | root <root> | 2020-11-02 16:34 | Install | 33
1 | 系统 <空> | 2020-11-02 16:17 | Install | 303
yum history undo 7 删除之前的yum 安装记录
9.kvm 修改nat virbr0网段
virsh net-list 查看当前活动的
virsh net-dumpxml default >/tmp/default.xml 导出配置
[root@localhost ~]# virsh net-edit default
<network>
<name>default</name>
<uuid>edf3e59b-2a70-41c1-a3b8-92ec41ebfe78</uuid>
<forward mode='nat'/>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:59:c2:d3'/>
<ip address='10.4.7.1' netmask='255.255.255.0'> 修改此项网段
<dhcp>
<range start='10.4.7.2' end='10.4.7.254'/> dhcp地址
</dhcp>
</ip>
</network>
virsh net-destroy default 网络 default 被删除
virsh net-start default 网络 default 已启用
ifconfig virbr0 查看修改
10. 查看kvm主机获取的nat IP地址
[root@localhost ~]# virsh dumpxml k8s |grep mac
<partition>/machine</partition>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<mac address='52:54:00:11:8b:8a'/>
root@localhost ~]# arp -a 查看
gateway (183.251.172.1) at ac:85:3d:a5:c7:54 [ether] on eno1
? (10.4.7.24) at 52:54:00:11:8b:8a [ether] on virbr0
arp -a |grep `virsh dumpxml k8s |grep "<mac"|cut -d"'" -f2` 查询ip
kvm链接克隆脚本
vim kvmclone.sh
#!/bin/bash
[ -z $1 ] && echo "`basename $0` kvm克隆名称" && exit 1
[ -f /home/kvm/$1.qcow2 ] && echo "目录中存在$1磁盘,可能会覆盖之前的虚拟机" && exit 2
qemu-img create -b /home/kvm/k8s.qcow2 -f qcow2 /home/kvm/$1.qcow2
virsh dumpxml k8s > $1.xml
num=`openssl rand -hex 1`:`openssl rand -hex 1`:`openssl rand -hex 1`
sed -ri "/<name>/c \ <name>$1</name>" $1.xml
sed -ri "/<uuid>/c\ <uuid>`uuidgen`</uuid>" $1.xml
sed -ri "s#k8s.qcow2#$1.qcow2#g" $1.xml
sed -ri "/mac address/c \ <mac address='52:54:00:${num}'/>" $1.xml
virsh define $1.xml
11. kvm虚拟化虚拟机打开虚拟化功能
首先检查 KVM host(宿主机/母机)上的kvm_intel模块是否打开了嵌套虚拟机功能(默认是开启的):
1.modinfo kvm_intel | grep nested
parm: nested:bool
2.cat /sys/module/kvm_intel/parameters/nested
Y
3.如果上面的显示结果不是 Y 的话需要开启 nested:
modprobe -r kvm-intel
modprobe kvm-intel nested=1
cat /sys/module/kvm_intel/parameters/nested
4.通过方法将之前的虚拟机或者新建的虚拟机CPU类型"core2duo",并且为虚拟机添加"vmx"特性
<cpu mode='custom' match='exact' check='partial'>
<model fallback='allow'>core2duo</model>
<feature policy='require' name='vmx'/>
</cpu>
查看是否打开虚拟化
egrep '(vmx|svm)' /proc/cpuinfo
egrep -c '(vmx|svm)' /proc/cpuinfo 用于脚本 查看是否为0
网友评论