日期:2024.11.5
目的:在物理机RHEL9.4搭建的虚拟化环境中安装虚拟机,操作系统为RHEL8.1,虚拟出网卡3块,用来模拟路由,后续还打算在上面搭建一个DHCP的中继服务。
参照:
- 鸟哥Linux私房菜服务器篇 第二章、第一個虛擬機器的安裝與調整
https://linux.vbird.org/linux_server/rocky9/0130vmtuning.php
还是之前规划好的topology
创建两个文件夹用来放操作系统镜像文件,ISO文件夹放完整镜像,iso文件夹放最小化或网络安装镜像。安装wget准备下载镜像
[root@RHEL9 ~]# mkdir /data/{ISO,iso}
[root@RHEL9 ~]# dnf install -y wget
nohup保证终端断开也能继续下载,wget -O /文件路径/文件名,结尾&设置后台下载
[root@RHEL9 ~]# nohup wget -O /data/ISO/rhel-8.10-x86_64-dvd.iso https://access.cdn.redhat.com/content/origin/files/sha256/9b/9b3c8e31bc2cdd2de9cf96abb3726347f5840ff3b176270647b3e66639af291b/rhel-8.10-x86_64-dvd.iso?_auth_=1730756644_605364e6f7bf5f9356b2de6cf00954aa &
[1] 5759
[root@RHEL9 ~]# nohup: ignoring input and appending output to 'nohup.out'
虚拟机硬盘放/kvm下,xml文件放/data/xml下
[root@RHEL9 ~]# df -Th /kvm
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/rhel-home xfs 100G 746M 100G 1% /kvm
[root@RHEL9 ~]# mkdir /data/xml
先创建虚拟机硬盘,使用qcow2格式,初始占用空间极小,写入数据时再分配空间
qcow2:qemu copy on write
[root@RHEL9 ~]# cd /kvm;qemu-img create -f qcow2 rhel8.img 10G
Formatting 'rhel8.img', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=10737418240 lazy_refcounts=off refcount_bits=16
[root@RHEL9 kvm]# ll -h /kvm/rhel8.img
-rw-r--r--. 1 root root 193K Nov 5 05:53 /kvm/rhel8.img
创建虚拟机可以使用virt-install指令,我对这个指令不太熟悉,我是直接编辑XML文件
[root@RHEL9 ~]# dnf info virt-install
Updating Subscription Management repositories.
Last metadata expiration check: 7:56:35 ago on Tue 05 Nov 2024 01:37:01 PM CST.
Available Packages
Name : virt-install
Version : 4.1.0
Release : 5.el9
Architecture : noarch
Size : 44 k
Source : virt-manager-4.1.0-5.el9.src.rpm
Repository : rhel-9-for-x86_64-appstream-rpms
Summary : Utilities for installing virtual machines
URL : https://virt-manager.org/
License : GPLv2+
Description : Package includes several command line utilities, including virt-install: (build and install new VMs) and virt-clone (clone an existing virtual: machine).
先给虚拟机生成一个UUID,然后编辑XML文件
[root@RHEL9 ~]# uuidgen
52552710-4887-44f2-ae42-2734b681922f
[root@RHEL9 ~]# vim /data/xml/rhel8.xml
RHEL8初始的虚拟机配置文件-route-cdrom
<domain type="kvm"><name>rhel8</name> ///虚拟机名称<uuid>52552710-4887-44f2-ae42-2734b681922f</uuid> ///刚生成的UUID<memory>1048576</memory> ///内存单位Byte,我这是1024*1024,给的1G<vcpu>1</vcpu> ///cup核心数<os><type arch="x86_64" machine="q35">hvm</type><boot dev="hd"/> ///硬盘启动项<boot dev="cdrom"/> ///光驱启动项</os><features><acpi/><apic/></features><cpu mode="host-passthrough"/><clock offset="utc"><timer name="rtc" tickpolicy="catchup"/><timer name="pit" tickpolicy="delay"/><timer name="hpet" present="no"/></clock><pm><suspend-to-mem enabled="no"/><suspend-to-disk enabled="no"/></pm><devices><emulator>/usr/libexec/qemu-kvm</emulator><disk type="file" device="cdrom"><driver name="qemu" type="raw"/><source file="/data/ISO/rhel-8.10-x86_64-dvd.iso"/> ///系统安装光盘位置<target dev="sda" bus="sata"/><readonly/></disk><disk type="file" device="disk"><driver name="qemu" type="qcow2" discard="unmap"/><source file="/kvm/rhel8.img"/> ///刚创建的硬盘路径<target dev="vda" bus="virtio"/></disk><controller type="usb" model="qemu-xhci" ports="15"/><controller type="pci" model="pcie-root"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><interface type="bridge"> ///网络方式选网桥<source bridge="WANbridge"/> ///宿主机配置的的网桥<mac address="52:54:00:00:31:68"/> ///mac地址16进制,不冲突即可<model type="virtio"/></interface><interface type="bridge"><source bridge="DMZbridge"/><mac address="52:54:00:00:31:72"/><model type="virtio"/></interface><interface type="bridge"><source bridge="LANbridge"/><mac address="52:54:00:00:31:10"/><model type="virtio"/></interface><console type="pty"/><channel type="unix"><source mode="bind"/><target type="virtio" name="org.qemu.guest_agent.0"/></channel><input type="tablet" bus="usb"/><graphics type="vnc" port="5902" listen="0.0.0.0" passwd="hatred"/> ///宿主机5902端口,passwd连接密码<video><model type="virtio"/></video><memballoon model="virtio"/><rng model="virtio"><backend model="random">/dev/urandom</backend></rng></devices>
</domain>
安装Virt-viewer准备发起远程VNC连接————路由221.229.XX.X1:62202端口————RHEL9:192.168.5.253:5902端口
https://virt-manager.org/download.html
在RHEL9上通过配置好的XML文件启动虚拟机
[root@RHEL9 ~]# virsh create /data/xml/rhel8.xml
Domain 'rhel8' created from /data/xml/rhel8.xml[root@RHEL9 ~]# virsh list Id Name State
-----------------------1 rhel8 running
发起连接后输入XML文件里设置的VNC密码,通过验证后显示虚拟机的显示器画面,进行安装
连接vnc及安装rhel8简要过程
关闭虚拟机准备修改启动项配置,shutdownd不行就destroy
[root@RHEL9 ~]# virsh shutdown rhel8
Domain 'rhel8' is being shutdown[root@RHEL9 ~]# virsh list Id Name State
-----------------------1 rhel8 running[root@RHEL9 ~]# virsh destroy rhel8
Domain 'rhel8' destroyed
XML文件中有关cdrom的配置有两处,一处是在启动项<boot>块里,一处是在硬盘配置<disk>块里
删除这两处并重新创建RHEL8
[root@RHEL9 ~]# grep -n cdrom /data/xml/rhel8.xml
9: <boot dev="cdrom"/> ///光驱启动项
27: <disk type="file" device="cdrom">
[root@RHEL9 ~]# sed -En '/<disk/,/<\/disk/p' /data/xml/rhel8.xml<disk type="file" device="cdrom"><driver name="qemu" type="raw"/><source file="/data/ISO/rhel-8.10-x86_64-dvd.iso"/> ///系统安装光盘位置<target dev="sda" bus="sata"/><readonly/></disk><disk type="file" device="disk"><driver name="qemu" type="qcow2" discard="unmap"/><source file="/kvm/rhel8.img"/> ///刚创建的硬盘路径<target dev="vda" bus="virtio"/></disk>
[root@RHEL9 ~]# sed -Ei.bak '/<boot dev="cdrom"\/>/d' /data/xml/rhel8.xml
[root@RHEL9 ~]# sed -Ei '/<disk type="file" device="cdrom">/,/<\/disk>/d' /data/xml/rhel8.xml
[root@RHEL9 ~]# virsh create /data/xml/rhel8.xml
Domain 'rhel8' created from /data/xml/rhel8.xml
重新vnc连接虚拟机,导入我的笔记本和台式机的公钥方便远程连接
调用以下脚本修改网络配置
#netinit.sh
#Date: 2024-10-27
#!/bin/bash#需要配置的IP地址,请手动输入
read -p "请输入IP地址:" -t 60 eth_ip#抓出网卡配置文件名称,如有多个网卡请手动赋值
eth_con='enp1s0'
#eth_con=$(nmcli connection show | sed -En 's/^([[:alnum:]]+) +.+ +ethernet.+$/\1/p')#抓出原DHCP获取的子网掩码、网关和DNS1,DNS2
eth_netmask=$(nmcli connection show $eth_con | sed -En 's/IP4.ADDRESS\[1\]: +.+\/([[:digit:]]+)$/\1/p')
eth_gateway=$(nmcli connection show $eth_con | sed -En 's/^IP4.GATEWAY: +(.+)$/\1/p')
eth_dns1=$(nmcli connection show $eth_con | sed -En 's/^IP4.DNS\[1\]: +(.+)$/\1/p')
eth_dns2=$(nmcli connection show $eth_con | sed -En 's/^IP4.DNS\[2\]: +(.+)$/\1/p')#将新IP和旧子网掩码组成IP/掩码格式
eth_ip=$eth_ip/$eth_netmasknmcli connection modify $eth_con ipv4.method manual ipv4.addresses $eth_ip
nmcli connection modify $eth_con ipv4.gateway $eth_gateway
nmcli connection modify $eth_con ipv4.dns $eth_dns1 +ipv4.dns $eth_dns2
nmcli connection up $eth_con
将路由端口63333转发到本机22供远程连接
Xshell发起远程连接登录RHEL8
Connecting to 221.229.XX.X1:63333...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.WARNING! The remote SSH server rejected X11 forwarding request.
Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Wed Nov 6 00:56:36 2024
[root@localhost ~]# ls
anaconda-ks.cfg netinit.sh
[root@localhost ~]# poweroff
关机后,删除vnc端口,xml文件不再调整。重新定义RHEL8主机,启动,设置开机自启
[root@RHEL9 ~]# virsh list Id Name State
--------------------[root@RHEL9 ~]# grep hatred /data/xml/rhel8.xml<graphics type="vnc" port="5902" listen="0.0.0.0" passwd="hatred"/> ///宿主机5902端口,passwd连接密码
[root@RHEL9 ~]# sed -Ei '/hatred/d' /data/xml/rhel8.xml
[root@RHEL9 ~]# virsh define /data/xml/rhel8.xml
Domain 'rhel8' defined from /data/xml/rhel8.xml[root@RHEL9 ~]# virsh start rhel8
Domain 'rhel8' started[root@RHEL9 ~]# virsh autostart rhel8
Domain 'rhel8' marked as autostarted[root@RHEL9 ~]# virsh list --autostart Id Name State
-----------------------2 rhel8 running
修改主机名、安装常用软件的过程省略
RHEL9后续打算做ansible主控,先实现key认证登录RHEL8,RHEL9生成一个公钥传到我的http服务器上供RHEL8导入
[root@RHEL9 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
[root@RHEL9 ~]# scp -P 22222 /root/.ssh/id_rsa.pub root@58.218.XX.X2:/file/PK/
root@58.218.XX.X2's password:
id_rsa.pub
[root@RHEL8 ~]# echo '' >> .ssh/authorized_keys
[root@RHEL8 ~]# curl http://58.218.XX.X2:22280/PK/id_rsa.pub >> .ssh/authorized_keys% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 564 100 564 0 0 94000 0 --:--:-- --:--:-- --:--:-- 94000
创建RHEL8虚拟机的脚本
[root@RHEL9 ~]# cat rhel8create-cd.sh
#guestcreate.sh
#Date: 2024-11-08
#!/bin/bashbootdev=cdromguestname=rhel8
guestmem=1048576
guestcups=1
hddir=/kvm/
hdsize=10G
hdpath=${hddir}${guestname}.img
vncport=5902
vncpasswd=hatredwanmac='52:54:00:92:31:FE'
dmzmac='52:54:00:72:31:FE'
lanmac='52:54:00:10:31:FE'
isopath='/data/iso/data/ISO/rhel-8.10-x86_64-dvd.iso'xmldir=/data/xml/
xmlpath=${xmldir}${guestname}.xmlqemu-img create -f qcow2 ${hdpath} ${hdsize}cat > ${xmlpath} << EOF
<domain type="kvm"><name>${guestname}</name><uuid>$(uuidgen)</uuid><memory>${guestmem}</memory><vcpu>${guestcups}</vcpu><os><type arch="x86_64" machine="q35">hvm</type><boot dev="hd"/></os><features><acpi/><apic/></features><cpu mode="host-passthrough"/><clock offset="utc"><timer name="rtc" tickpolicy="catchup"/><timer name="pit" tickpolicy="delay"/><timer name="hpet" present="no"/></clock><pm><suspend-to-mem enabled="no"/><suspend-to-disk enabled="no"/></pm><devices><emulator>/usr/libexec/qemu-kvm</emulator><disk type="file" device="disk"><driver name="qemu" type="qcow2" discard="unmap"/><source file="${hdpath}"/><target dev="vda" bus="virtio"/></disk><controller type="usb" model="qemu-xhci" ports="15"/><controller type="pci" model="pcie-root"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><controller type="pci" model="pcie-root-port"/><console type="pty"/><channel type="unix"><source mode="bind"/><target type="virtio" name="org.qemu.guest_agent.0"/></channel><input type="tablet" bus="usb"/><graphics type="vnc" port="${vncport}" listen="0.0.0.0" passwd="${vncpasswd}"/><video><model type="virtio"/></video><memballoon model="virtio"/><rng model="virtio"><backend model="random">/dev/urandom</backend></rng></devices>
</domain>
EOF[[ -n ${bootdev} ]] && sed -Ei.bak '/ <boot dev="hd"\/>/a \ <boot dev="'''${bootdev}'''"\/>' ${xmlpath}
[[ -n ${isopath} ]] && sed -Ei '/ <controller type="usb" model="qemu-xhci" ports="15"\/>/i \ <disk type="file" device="cdrom">\n <driver name="qemu" type="raw"\/>\n <source file="'''${isopath}'''"\/>\n <target dev="sda" bus="sata"\/>\n <readonly\/>\n <\/disk>' ${xmlpath}
[[ -n ${wanmac} ]] && sed -Ei '/ <console type="pty"\/>/i \ <interface type="bridge">\n <source bridge="WANbridge"/>\n <mac address="'''${wanmac}'''"\/>\n <model type="virtio"\/>\n <\/interface>' ${xmlpath}
[[ -n ${dmzmac} ]] && sed -Ei '/ <console type="pty"\/>/i \ <interface type="bridge">\n <source bridge="DMZbridge"/>\n <mac address="'''${dmzmac}'''"\/>\n <model type="virtio"\/>\n <\/interface>' ${xmlpath}
[[ -n ${lanmac} ]] && sed -Ei '/ <console type="pty"\/>/i \ <interface type="bridge">\n <source bridge="LANbridge"/>\n <mac address="'''${lanmac}'''"\/>\n <model type="virtio"\/>\n <\/interface>' ${xmlpath}
virsh create ${xmlpath}
定义RHEL8虚拟机的脚本
[root@RHEL9 ~]# cat rhel8define-cd.sh
#guestdefine.sh
#Date: 2024-11-09
#!/bin/bash#配置文件位置
xmlpath=/data/xml/rhel8#获取虚拟机名称
guestname=$(sed -En 's/<name>(rocky9)<\/name>/\1/p' ${xmlpath})#确保虚拟机关闭
virsh guestinfo ${guestname} && virsh destroy ${guestname}#删除vnc远程连接
sed -Ei.bak '/<graphics type="vnc" port=/d' ${xmlpath}#删除启动项cdrom
sed -Ei '/<boot dev="cdrom"\/>/d' ${xmlpath}#删除设备cdrom
sed -Ei '/<disk type="file" device="cdrom">/,/<\/disk>/d' ${xmlpath}#定义虚拟机
virsh define ${xmlpath}#启动虚拟机
virsh start ${guestname}#设置虚拟机开机启动
virsh autostart ${guestname}