1、开启宿主机的CPU虚拟化功能
编辑虚拟机设置--处理器--勾选 虚拟化Intel VT-x/EPT 或AMD-V/RVI(V).
2、验证是否开启虚拟化支持
[root@KVM ~]#grep -Em 1 "vmx|svm" /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves arat avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid movdiri movdir64b avx512_vp2intersect md_clear flush_l1d arch_capabilities
3、安装KVM工具包并设置服务开机自启
[root@KVM ~]#dnf install -y qemu-kvm libvirt virt-manager virt-install virt-viewer
[root@KVM ~]#systemctl start --now libvirtd
[root@KVM ~]#systemctl status libvirtd
● libvirtd.service - Virtualization daemon
Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enab>
Active: active (running) (thawing) since Mon 2022-04-11 11:57:42 CST; 16s ago
Docs: man:libvirtd(8)
https://libvirt.org
Main PID: 2215 (libvirtd)
Tasks: 19 (limit: 32768)
Memory: 42.3M
CGroup: /system.slice/libvirtd.service
├─1371 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --l>
├─1372 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --l>
└─2215 /usr/sbin/libvirtd --timeout 120
Apr 11 11:57:42 KVM systemd[1]: Starting Virtualization daemon...
Apr 11 11:57:42 KVM systemd[1]: Started Virtualization daemon.
Apr 11 11:57:42 KVM dnsmasq[1371]: read /etc/hosts - 2 addresses
Apr 11 11:57:42 KVM dnsmasq[1371]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 ad>
Apr 11 11:57:42 KVM dnsmasq-dhcp[1371]: read /var/lib/libvirt/dnsmasq/default.hostsfile
4、准备安装系统的iso相关文件
[root@KVM ~]#mkdir -pv /data/isos
mkdir: created directory '/data'
mkdir: created directory '/data/isos'
[root@KVM ~]#cd /data/isos/
[root@KVM isos]#ls
CentOS-8.3.2011-x86_64-dvd1.iso
5、创建虚拟机从光盘启动并利用kickstart自动安装系统
准备yum仓库和kickstart环境
[root@KVM ~]#yum install -y httpd
[root@KVM ~]#systemctl enable --now httpd
[root@KVM ~]#mkdir -pv /var/www/html/centos8/os/x86_64
[root@KVM ~]#mount /dev/sr0 /var/www/html/centos8/os/x86_64/
[root@KVM ~]#cat /var/www/html/ks/centos8.cfg
ignoredisk --only-use=sda
zerombr
text
reboot
clearpart --all --initlabel
selinux --disabled
firewall --disabled
url --url=http://10.0.0.150/centos8/os/x86_64/
keyboard --vckeymap=us --xlayouts='us'
lang en_US.UTF-8
bootloader --append="net.ifnames=0" --location=mbr --boot-drive=sda
network --bootproto=dhcp --device=eth0 --ipv6=auto --activate
network --hostname=centos8.tan.org
rootpw --iscrypted $6$j9YhzDUnQVnxaAk8$qv7rkMcPAEbV5yvwsP666DXWYadd3jYjkA9fpxAo9qYotjGGBUclCGoP1TRvgHBpqgc5n0RypMsPTQnVDcpO01
firstboot --enable
skipx
services --disabled="chronyd"
timezone Asia/Shanghai --isUtc --nontp
user --name=tan --password=6oUfb/02CWfLb5l8f$sgEZeR7c7DpqfpmFDH6huSmDbW1XQNR4qKl2EPns.gOXqlnAIgv9pTogtFVaDtEpMOC.SWXKYqxfVtd9MCwxb1 --iscrypted --gecos="tan"
autopart --type=lvm
%packages
@^minimal-environment
kexec-tools
%end
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
%post
useradd tanl
echo 123456 | passwd --stdin tanl &> /dev/null
%end
#利用virt-install的两项选项实现kickstart安装
#创建磁盘文件
[root@KVM ~]#qemu-img create -f qcow2 /var/lib/libvirt/images/centos8-vm1.qcow2 20G
Formatting '/var/lib/libvirt/images/centos8-vm1.qcow2', fmt=qcow2 size=21474836480 cluster_size=65536 lazy_refcounts=off refcount_bits=16
#创建虚拟机
[root@KVM ~]#virt-install --virt-type kvm --name centos8-vm1 --ram 2048 --vcpus 2 --disk path=/var/lib/libvirt/images/centos8-vm1.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --location=/data/isos/CentOS-8.3.2011-x86_64-dvd1.iso --extra-args="ks=http://10.0.0.150/ks/centos8.cfg"
#验证宿主机进程
[root@KVM ~]#ps aux|grep qemu-kvm
qemu 3581 189 8.3 3185700 1369156 ? Sl 12:30 10:50 /usr/libexec/qemu-kvm -name guest=centos8-vm1,debug-threads=on -S -object secret,id=masterKey0,format=raw,file=/var/lib/libvirt/qemu/domain-1-centos8-vm1/master-key.aes -machine pc-i440fx-rhel7.6.0,accel=kvm,usb=off,dump-guest-core=off -cpu Cooperlake,ss=on,hypervisor=on,tsc-adjust=on,avx512ifma=on,sha-ni=on,avx512vbmi=on,umip=on,avx512vbmi2=on,gfni=on,vaes=on,vpclmulqdq=on,avx512bitalg=on,avx512-vpopcntdq=on,md-clear=on,xsaves=on,ibpb=on,amd-ssbd=on,hle=off,rtm=off,avx512-bf16=off,taa-no=off -m 2048 -overcommit mem-lock=off -smp 2,sockets=2,cores=1,threads=1 -uuid 0ee96344-b772-4472-9ebe-b99a4416759f -no-user-config -nodefaults -chardev socket,id=charmonitor,fd=36,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=delay -no-hpet -no-reboot -global PIIX4_PM.disable_s3=1 -global PIIX4_PM.disable_s4=1 -boot strict=on -kernel /var/lib/libvirt/boot/virtinst-_0_vb3ya-vmlinuz -initrd /var/lib/libvirt/boot/virtinst-jou8dx9f-initrd.img -append ks=http://10.0.0.150/ks/centos8.cfg -device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x4.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x4 -device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x4.0x1 -device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x4.0x2 -blockdev {"driver":"file","filename":"/var/lib/libvirt/images/centos8-vm1.qcow2","node-name":"libvirt-2-storage","auto-read-only":true,"discard":"unmap"} -blockdev {"node-name":"libvirt-2-format","read-only":false,"driver":"qcow2","file":"libvirt-2-storage","backing":null} -device ide-hd,bus=ide.0,unit=0,drive=libvirt-2-format,id=ide0-0-0,bootindex=1 -blockdev {"driver":"file","filename":"/data/isos/CentOS-8.3.2011-x86_64-dvd1.iso","node-name":"libvirt-1-storage","auto-read-only":true,"discard":"unmap"} -blockdev {"node-name":"libvirt-1-format","read-only":true,"driver":"raw","file":"libvirt-1-storage"} -device ide-cd,bus=ide.0,unit=1,drive=libvirt-1-format,id=ide0-0-1 -netdev tap,fd=39,id=hostnet0 -device e1000,netdev=hostnet0,id=net0,mac=52:54:00:53:db:58,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -device usb-tablet,id=input0,bus=usb.0,port=1 -vnc 0.0.0.0:0 -device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,vram64_size_mb=0,vgamem_mb=16,max_outputs=1,bus=pci.0,addr=0x2 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x5 -sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,resourcecontrol=deny -msg timestamp=on
root 3791 0.0 0.0 12112 1084 pts/2 S+ 12:36 0:00 grep --color=auto qemu-kvm
#可以修改VNC端口的范围,默认5900-65535
[root@KVM ~]#ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
LISTEN 0 128 127.0.0.1:6010 0.0.0.0:*
LISTEN 0 128 127.0.0.1:6011 0.0.0.0:*
LISTEN 0 128 0.0.0.0:39327 0.0.0.0:*
LISTEN 0 1 0.0.0.0:5900 0.0.0.0:*
[root@KVM ~]#grep remote_display_port /etc/libvirt/qemu.conf
#remote_display_port_min = 5900
#remote_display_port_max = 65535
标签:KVM,--,创建,虚拟机,kvm,libvirt,0.0,root,centos8
From: https://www.cnblogs.com/tanll/p/17748161.html