RHEL9.4上建立RHEL8.1虛擬機器

李蔚發表於2024-11-09

日期: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簡要過程 RHEL9.4上建立RHEL8.1虛擬機器 RHEL9.4上建立RHEL8.1虛擬機器 RHEL9.4上建立RHEL8.1虛擬機器 RHEL9.4上建立RHEL8.1虛擬機器 RHEL9.4上建立RHEL8.1虛擬機器

關閉虛擬機器準備修改啟動項配置,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_netmask

nmcli 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  Current
                                 Dload  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/bash

bootdev=cdrom

guestname=rhel8
guestmem=1048576
guestcups=1
hddir=/kvm/
hdsize=10G
hdpath=${hddir}${guestname}.img
vncport=5902
vncpasswd=hatred

wanmac='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}.xml

qemu-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}

相關文章