KVM基础记录
KVM_Learn Install KVM Software # yum install libvirt* virt-* qemu-kvm* -y Install windows need packet # yum install kvm libvirt virt-install qemu-kvm virt-viewer bridge-utils 虚拟机软件说明: libvirt # 虚拟机管理 virt # 虚拟机安装克隆 qemu-kvm # 管理虚拟机磁盘 # systemctl start libvirtd.service # systemctl status libvirtd.service 一般情况下是需要通过vnc连接安装好的KVM # 安装期间记得配置vnc密码,否则第一次进入vnc,下次再进入需要密码,而密码是空,进入不了,参数如下: --graphics vnc,listen=0.0.0.0,port=5920,password=123456, 配置一台KVM虚拟机 # virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7 --memory 1024 --vcpus 1 --disk /data01/vmware/clsn.raw,format=raw,size=100 --cdrom /data01/iso/CentOS-7-x86_64-DVD-1708.iso --network network=default --graphics vnc,listen=0.0.0.0,port=5900,password=123456 --noautoconsole --graphics vnc,listen=0.0.0.0,port=5920,password=123456, # 参数说明 --disk /data01/vmware/clsn.raw,format=raw,size=100 安装的系统文件存放的磁盘位置,格式是raw,最大容量是100G virt-install --name $KVM_NAME --ram=$KVM_RAM --vcpus=$KVM_VCPU \ --os-type=linux --os-variant=rhel6 --network bridge:$VLAN \ --disk path=$KVM_DISK_PATH/$KVM_NAME.img,size=$KVM_DISK_SIZE \ --location $LOCATION --extra-args "ks=$KS ksdevice=eth0 ip=$KVM_NAME netmask=$NETMASK gateway=$GW DNS=114.114.114.114 console=ttyS0" --autostart; 网络自动化安装 virt-install --name 10.57.239.66 --ram=1024 --vcpus=4 \ --os-type=linux --os-variant=rhel7 --network bridge:br0 \ --disk path=/data01/vmware/10.57.239.66.img,size=80 \ --location http://192.168.6.188/tdyum/cdrom/ --extra-args "ks=http://192.168.6.188/ks/CentOS_7.4_1708.ks ksdevice=eth0 ip=10.57.239.66 netmask=255.255.255.0 gateway=10.57.239.1 DNS=192.168.8.55 console=tty0 console=ttyS0,115200n8 edd=off" --graphics vnc,listen=0.0.0.0,port=5922,password=123456, --autostart 上述可正常启动,但是ks脚本有问题 Probing EDD (edd=off to disable) ...ok http://10.57.17.7/centos/7.4.1708/os/x86_64/ http://192.168.6.188/tdyum/cdrom/ --extra-args="console=tty0 console=ttyS0,115200n8 edd=off" # This is install fail virt-install --name 10.57.239.68 --ram=1024 --vcpus=2 \ --os-type=linux --os-variant=rhel7 --network=bridge:br0 \ --disk path=/data01/vmware/10.57.239.68.img,size=80 \ --location http://192.168.6.188/tdyum/cdrom/ --extra-args "ks=http://192.168.6.188/ks/CentOS_7.4_1708.ks ksdevice=eth0 ip=10.57.239.68 netmask=255.255.255.0 gateway=10.57.239.1 DNS=192.168.8.55 console=ttyS0" --autostart 手动安装-测试有问题 virt-install --name 10.57.239.68 --ram=1024 --vcpus=2 \ --os-type=linux --os-variant=rhel7 --network bridge:br0 \ --disk path=/data01/vmware/10.57.239.68.img,size=80 \ --location /data01/iso/CentOS-7-x86_64-DVD-1708.iso --extra-args "ksdevice=eth0 ip=10.57.239.68 netmask=255.255.255.0 gateway=10.57.239.1 DNS=192.168.8.55 console=ttyS0" --autostart 手动安装测试成功 img格式 virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name 10.57.239.67 --ram 1024 --vcpus 2 --disk /data01/vmware/10.57.239.67.img,size=100 --cdrom /data01/iso/CentOS-7-x86_64-DVD-1708.iso --graphics vnc,listen=0.0.0.0,port=5920,password=123456, --network network=default --autostart virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name 10.57.239.67 --ram 1024 --vcpus 2 --disk /data01/vmware/10.57.239.67.raw,size=100 --cdrom /data01/iso/CentOS-7-x86_64-DVD-1708.iso --graphics vnc,listen=0.0.0.0,port=5920,password=123456, --network network=default --autostart virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name 10.57.239.67 --ram 1024 --vcpus 2 --disk /data01/vmware/10.57.239.67.raw,size=100 --location /data01/iso/CentOS-7-x86_64-DVD-1708.iso --graphics vnc,listen=0.0.0.0,port=5920,password=123456, --network network=default --autostart # 加入参数--locatioin会开机安装自动启动 virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name 10.57.239.67 --ram 1024 --vcpus 2 --disk /data01/vmware/10.57.239.67.img,size=100 --location /data01/iso/CentOS-7-x86_64-DVD-1708.iso --graphics vnc,listen=0.0.0.0,port=5920,password=123456, --network network=default --autostart virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name 10.57.239.67 --memory 1024 --vcpus 2 --disk /data01/vmware/10.57.239.67.img,size=100 --location /data01/iso/CentOS-7-x86_64-DVD-1708.iso --graphics vnc,listen=0.0.0.0,port=5920,password=123456, --network network=default --autostart # This is used brideg:br0 使用这种方式手动安装 virt-install --virt-type kvm --os-type=linux --os-variant=rhel7 --name 10.57.239.68 --memory 1024 --vcpus 2 --disk /data01/vmware/10.57.239.68.img,size=80 --location /data01/iso/CentOS-7-x86_64-DVD-1708.iso --graphics vnc,listen=0.0.0.0,port=5920,password=123456, --network=bridge:br0 --autostart # Delete Virtual Host virsh undefine 10.57.239.68 virsh destroy 10.57.239.68 # 解决console登录问题,在虚拟机里面执行,然后重启 grubby --update-kernel=ALL --args="console=ttyS0" 配置kvm的网卡接口为桥接模式 [root@tdok network-scripts]# cat ifcfg-bond0 DEVICE="bond0" TYPE=Ethernet NAME=bond0 BOOTPROTO=none BONDING_MASTER=yes BRIDGE=br0 ONBOOT=yes BONDING_OPTS="mode=4 miimon=100" [root@tdok network-scripts]# cat ifcfg-br0 DEVICE=br0 TYPE=Bridge BOOTPROTO=static IPADDR=10.57.239.21 NETMASK=255.255.255.0 GATEWAY=10.57.239.1 ONBOOT=yes DEFROUTE=yes [root@tdok network-scripts]# cat ifcfg-enp0s31f6 DEVICE=enp0s31f6 MASTER=bond0 SLAVE=yes USERCTL=no BOOTPROTO=none ONBOOT=yes [root@tdok network-scripts]# 配置完成上述网卡之后,编辑生成modprobe.conf此文件,然后添加alias bond0 bonding [root@tdok modprobe.d]# pwd /etc/modprobe.d [root@tdok modprobe.d]# cat modprobe.conf alias bond0 bonding [root@tdok modprobe.d]# 如此操作是为了固定网卡名称不是随机名称,这样固定网卡名称一般为eth0 配置完成桥接模式之后安装虚拟机自动获取IP地址就会是桥接的IP地址 关闭ssh连接解析dns,这样连接的更快 sed -i "s/#UseDNS yes/UseDns no/g" /etc/ssh/sshd_config sed -i "s/GSSAPIAuthentication yes/GSSAPIAuthentication no/g" /etc/ssh/sshd_config 创建快照 virsh # snapshot-create 10.57.239.68 Domain snapshot 1569046385 created 查看快照信息 virsh # snapshot-list 10.57.239.68 Name Creation Time State ------------------------------------------------------------ 1569046385 2019-09-21 14:13:05 +0800 running virsh # snapshot-info 10.57.239.68 --snapshotname 1569046385 Name: 1569046385 Domain: 10.57.239.68 Current: yes State: running Location: internal Parent: - Children: 0 Descendants: 0 Metadata: yes # 删除快照 virsh # snapshot-delete --current --domain 10.57.239.68 # 进入虚拟机进行一系列删除操作,然后恢复快照 virsh # list Id Name State ---------------------------------------------------- 22 10.57.239.68 running virsh # snapshot-list 10.57.239.68 Name Creation Time State ------------------------------------------------------------ 1569046385 2019-09-21 14:13:05 +0800 running virsh # snapshot-revert 10.57.239.68 --snapshotname 1569046385 # 快照配置文件的位置 [root@tdok snapshot]# pwd /var/lib/libvirt/qemu/snapshot [root@tdok snapshot]# tree . └── 10.57.239.68 └── 1569046385.xml 1 directory, 1 file [root@tdok snapshot]# # 开机和关机 virsh start 10.57.239.68 virsh shutdown 10.57.239.68 # 克隆虚拟机 virt-clone --auto-clone -o 10.57.239.68 [root@tdok qemu]# ls 10.57.239.48.xml 10.57.239.68.xml autostart networks # 删除虚拟机 virsh undefine 10.57.239.48 virsh destroy 10.57.239.48 # 修改虚拟机名称 virsh # domrename 10.57.239.68-clone 10.57.239.48 # 查看宿主网桥 brctl show # Install Windows # Build 200G Disk qemu-img create -f qcow2 -o preallocation=metadata /data01/vmware/win7.qcow2 200G # 参数-o preallocation=metadata解释 推荐: http://www.361way.com/kvm-qcow2-preallocation-metadata/3354.html ## 利用预分配,可以直接一步完成KVM guest主机qcow2镜像格式主机的安装 ,而不需要像之前装完再转换 。 ## 利用预分配,可以加速qcow2格式下的KVM guest主机的读写速度 (虽然这里的测试结果不涉及到具体的速度测试,说起来有点牵强) # 基于桥接的网络创建虚拟机 # 这里bus=scsi先选择bus=ide,不然装系统磁盘识别不了,如果使用scsi那么就要安装virtio驱动 参考:https://blog.csdn.net/ks_is_fighting/article/details/38358793 virt-install --name=win7 --ram 4096 --vcpus=4 --disk /data01/vmware/win7.qcow2,bus=ide,sparse --cdrom /data01/iso/cn_windows_7_ultimate_with_sp1_x64_dvd_u_677408.iso --graphics vnc,listen=0.0.0.0,port=5920,password=123456, --network=bridge:br0,model=virtio --force --autostart virt-install --name=win7 --ram 4096 --vcpus=4 --disk /data01/vmware/win7.qcow2,bus=ide,sparse --cdrom /data01 /iso/cn_windows_7_ultimate_with_sp1_x64_dvd_u_677408.iso --graphics vnc,listen=0.0.0.0,port=5920,password=123456, -- network bridge=br0,model=virtio --force --autostart # 申请一块磁盘,给win7虚拟机然后挂载Windows驱动 qemu-img create -f qcow2 /data01/vmware/windowdrivers.img 10G # 下载对应的virtio的驱动,本次是直接加载官方的repo文件安装 wget https://fedorapeople.org/groups/virt/virtio-win/virtio-win.repo -O /etc/yum.repos.d/virtio-win.repo yum clean all yum makecache yum install virtio-win -y # 安装完成之后会在如下目录发现所有的驱动文件 /usr/share/virtio-win/ # 本次需要加载的iso文件如下 virtio-win-0.1.171.iso # 然后使用vim /etc/libvirt/qemu/win7.xml进行编辑将原有的磁盘信息更改为如下 <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/data01/vmware/win7.qcow2'/> <target dev='hda' bus='ide'/> <address type='drive' controller='0' bus='0' target='0' unit='0'/> </disk> <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/data01/vmware/windowdrivers.img'/> <target dev='hdb' bus='ide'/> <address type='drive' controller='0' bus='0' target='0' unit='1'/> </disk> <disk type='file' device='cdrom'> <driver name='qemu' type='raw'/> <source file='/usr/share/virtio-win/virtio-win-0.1.171.iso'/> <target dev='hdc' bus='ide'/> <readonly/> <address type='drive' controller='0' bus='1' target='0' unit='0'/> </disk> # 让win7.xml这个配置文件生效 virsh define /etc/libvirt/qemu/win7.xml # 重启安装好的win7操作系统 #### 可以在Windows下直接重启,也可以使用virsh destroy关闭Windows,在启动 # 上述没有问题的话进入Windows会看的加载好的iso文件,然后找到Windows下的设备管理,找以太网处,更新驱动,选中加载好的iso文件里面的驱动文件即可,具体可参考:https://blog.claves.me/2017/08/25/kvm-windows7%E8%99%9A%E6%8B%9F%E6%9C%BA%E5%AE%89%E8%A3%85%E7%BD%91%E5%8D%A1%E9%A9%B1%E5%8A%A8-%E6%97%A5%E5%B8%B8%E7%AC%94%E8%AE%B0/ # 至此Windows7已经在kvm环境下安装成功 #### 创建win7快照 virsh # snapshot-list win7 ======================================================= # 安装Windows的时候把virtio驱动加载进去 # Create 200G Disk # qemu-img create -f qcow2 -o preallocation=metadata /data01/vmware/win7-10.57.239.247.qcow2 200G # virt-install -n imperva-10.57.239.247 -r 4096 --vcpus=4 --os-type=windows --accelerate -c /data01/iso/cn_windows_7_ultimate_with_sp1_x64_dvd_u_677408.iso --disk /data01/vmware/imperva-13.qcow2,format=qcow2,bus=virtio --network=bridge:br0 --graphics vnc,listen=0.0.0.0,port=5921,password=123456, --force --autostart qemu-img create -f qcow2 -o preallocation=metadata /data01/vmware/imperva-13-10.57.239.247.qcow2 200G
迷茫的人生,需要不断努力,才能看清远方模糊的志向!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2020-12-17 HTB-靶机-RedCross
2020-12-17 HTB-靶机-Vault