快轉到主要內容

我的Linux生活日記 28.KVM

·1441 字·3 分鐘·
PolloChang
作者
PolloChang
我是一隻雞

我的Linux生活日記 28.KVM
#

在 Linux 中可以透過建立 KVM「核心基礎虛擬機器 (Kernel-based Virtual Machine;KVM)」 建立不同作業系統環境,無論是 RedHat 、Debian 或是 windows 都可以建立。在學習 Linux 的過程是不錯的學習工具。

啟動 CPU 虛擬化技術
#

這部份需要進入 BIOS 開啟,AMD 是 VT , Intel 是 VT-x、VT-d 或 VT-c 。

檢查硬體資源是否支援 虛擬技術
#

啟動之後可以下指令檢查

1
grep --color --perl-regexp 'vmx|svm' /proc/cpuinfo

安裝
#

安裝指令
#

  • Debain
1
2
sudo apt -y install qemu-system libvirt-clients libvirt-daemon-system qemu-kvm libvirt-daemon  bridge-utils virtinst virt-manager libosinfo-bin osinfo-db-tools
sudo systemctl enable libvirtd
  • Fedora
1
2
sudo dnf install @virtualization
sudo systemctl enable --now libvirtd
1
2
3
sudo usermod -aG kvm $(whoami)
sudo usermod -aG libvirt $(whoami)
sudo usermod -aG input $(whoami)

最佳化設定
#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
[root@kvm-004 ~]# tuned-adm list
Available profiles:
- accelerator-performance     - Throughput performance based tuning with disabled higher latency STOP states
- aws                         - Optimize for aws ec2 instances
- balanced                    - General non-specialized tuned profile
- desktop                     - Optimize for the desktop use-case
- epyc-eda                    - Optimize for EDA compute workloads on AMD EPYC CPUs
- hpc-compute                 - Optimize for HPC compute workloads
- intel-sst                   - Configure for Intel Speed Select Base Frequency
- latency-performance         - Optimize for deterministic performance at the cost of increased power consumption
- network-latency             - Optimize for deterministic performance at the cost of increased power consumption, focused on low latency network performance
- network-throughput          - Optimize for streaming network throughput, generally only necessary on older CPUs or 40G+ networks
- optimize-serial-console     - Optimize for serial console use.
- powersave                   - Optimize for low power consumption
- throughput-performance      - Broadly applicable tuning that provides excellent performance across a variety of common server workloads
- virtual-guest               - Optimize for running inside a virtual guest
- virtual-host                - Optimize for running KVM guests
Current active profile: throughput-performance
[root@kvm-004 ~]# tuned-adm active
Current active profile: throughput-performance
[root@kvm-004 ~]# tuned-adm profile virtual-host

使用 vhost_net 模組
#

[root@kvm-004 ~]# tuned-adm active Current active profile: virtual-host

1
2
3
4
5

### 使用 vhost_net 模組

```bash
sudo modprobe vhost_net

設定網路
#

1
2
sudo virsh net-start default
sudo virsh net-autostart default

新增 KVM 權限
#

1
2
sudo usermod -G libvirt-qemu pollochang
sudo usermod -G libvirt pollochang

KVM 更新OS清單
#

1
2
3
4
5
6
7
8
sudo apt install libosinfo-bin osinfo-db-tools
wget -O "/tmp/osinfo-db.tar.xz" https://releases.pagure.org/libosinfo/osinfo-db-20240701.tar.xz
wget -O "/tmp/osinfo-db.tar.xz" https://releases.pagure.org/libosinfo/osinfo-db-20231215.tar.xz
wget -O "/tmp/osinfo-db.tar.xz" https://releases.pagure.org/libosinfo/osinfo-db-20160728.tar.xz
wget -O "/tmp/osinfo-db.tar.xz" https://releases.pagure.org/libosinfo/osinfo-db-20181101.tar.xz
wget -O "/tmp/osinfo-db.tar.xz" https://releases.pagure.org/libosinfo/osinfo-db-20250124.tar.xz
sudo osinfo-db-import --local "/tmp/osinfo-db.tar.xz"
osinfo-query os | grep solaris

Debian 12 火牆使用Firewalld
#

  • 錯誤
1
2
3
❯ sudo virsh net-start default                                                                                                                                                                               ─╯
error: Failed to start network default
error: internal error: firewalld is set to use the nftables backend, but the required firewalld 'libvirt' zone is missing. Either set the firewalld backend to 'iptables', or ensure that firewalld has a 'libvirt' zone by upgrading firewalld to a version supporting rule priorities (0.7.0+) and/or rebuilding libvirt with --with-firewalld-zone
  • 解決方式

/etc/firewalld/firewalld.conf

1
FirewallBackend=nftables >> FirewallBackend=iptables

建立虛擬機
#

建立虛擬硬碟
#

建立虛擬機之前必須先建立虛擬硬碟。

1
sudo qemu-img create -f qcow2 /home/jameschang/VMS/linux/debian9.qcow2 20G

建立虛擬機器
#

建立完虛擬磁碟就是建立虛擬機啦!指令說明如下。

1
virt-install --virt-type kvm --name [虛擬機識別名稱] --ram [記憶體大小mb] --disk [虛擬硬碟掛載目錄],format=[虛擬磁碟格式] --network network=[虛擬網路界面] --graphics vnc,listen=0.0.0.0,password=debian9 --noautoconsole --os-type=linux --os-variant=[作業系統版本] --cdrom=[ISO光碟位置]
1
sudo virt-install --virt-type kvm --name debian9 --ram 4096 --disk /home/jameschang/VMS/linux/debian9.qcow2,format=qcow2 --network network=network1 --graphics vnc,listen=0.0.0.0,password=debian9 --noautoconsole --os-type=linux --os-variant=debian9 --cdrom=/data/softwares/linux/debian-9.13.0-amd64-netinst.iso

啟動KVM
#

1
sudo virsh start debian9

設定開機時啟動
#

如果須樣在主機中依開機後就啟動虛擬機可以這樣處理。

1
sudo virsh autostart debian9

KVM 轉 VMDisk
#

有時候在 KVM 設定完虛擬機環境後,可以把虛擬機轉成 vmdk 格式,讓其他類型虛擬管理工具使用,如:oracle virtualbox。

1
qemu-img convert -f qcow2 -O vmdk zimbra9-ubuntu18.04 zimbra9-ubuntu18.04.vmdk

KVM 網路
#

指定虛擬機網卡IP,除了可以在虛擬機裡設定,也可以直接在KVM 中直接設定

編輯網路
#

查詢 KVM 網卡
#

1
sudo virsh net-list
  • 執行結果
1
2
3
4
5
$ sudo virsh net-list
 Name      State    Autostart   Persistent
--------------------------------------------
 default   active   yes         yes
 network   active   yes         yes
1
virsh net-edit $NETWORK_NAME
  • 编辑段落
1
2
3
4
5
6
7
<dhcp>
  <range start='192.168.122.100' end='192.168.122.254'/>
  <host mac='52:54:00:ed:3c:bc' name='winxp' ip='192.168.122.2'/>
  <host mac='52:54:00:19:1b:70' name='loki' ip='192.168.122.3'/>
  <host mac='52:54:00:b8:f1:68' name='gilLab' ip='192.168.122.4'/>
  <host mac='' name='vm1' ip=''/>
</dhcp>
  • 重起網路

$NETWORK_NAME

1
2
sudo virsh net-destroy $NETWORK_NAME &&\
sudo virsh net-start $NETWORK_NAME

設定 mtu
#

如果 KVM 有多張網卡,而虛擬機也會需要多張網卡需要設定 MTU,因為預設是1500 ,不設定有可能造成虛擬機網路無法出去的狀況。

1
virsh net-edit $NETWORK_NAME
  • 編輯
1
2
3
4
5
6
<network>
  ...
  <bridge ~/>
  <mtu size='9000' />
  ...
</network>
  • 重啟主機
1
sudo reboot

維運相關-KVM 虛擬硬碟加大
#

1
2
#                [磁碟路徑] [要擴充的容量]
qemu-img resize /kvm/win10 +20G
  • 執行結果
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
qemu-img info /kvm/win10
image: /kvm/win10
file format: qcow2
virtual size: 40 GiB (42949672960 bytes)
disk size: 39.8 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: true
    refcount bits: 16
    corrupt: false
    extended l2: false

維運相關-winXP 無法上網
#

我工作上有時候也會測試 windows XP ,又碰到無法上網的問題,主要是驅動問題,設定成 Realtek 即可。

1
qemu-kvm -net nic,model=rtl8139 -net user -hda /opt/WinXP2010.img -smb /path/to/shared/dir -m 1024

參考資料
#

解決kvm虛擬機器windows系統間歇性網路中斷的問題

參考資料
#

觀察那些 KVM 虛擬機,設定成為開機自動啟動?

How to Add Memory, vCPU, Hard Disk to Linux KVM Virtual Machine

架設 Linux KVM 虛擬化主機 (Set up Linux KVM virtualization host)

Network XML format