KKTC

K A M U N E T

Dokümantasyon Merkezi

QEMU ile Linux-hosted sanallaştırma

Host :

Dış IP: 172.25.182.17 İç IP: 172.25.182.16 Örneği şöyle:

IP Alias ekle

/etc/systemd/network/wired.network:

[Network]
Address=172.25.182.7
Address=172.25.182.12
Address=172.25.182.15
Address=172.25.182.17

Sanal, boşta olan bir interface seç, ör. vnet2 gibi. QEMU’ya sağlanacak bir network ayağa kaldırma script’ini oluştur:

#!/bin/bash
ifconfig vnet2 up
route add -host 172.25.182.16 dev vnet2

/mnt/store-1/gentooFarmNetDown2.sh

iptables -A QEMU2 -o vnet2 -d 172.25.182.16 -j ACCEPT
iptables -A QEMU2 -o vnet2 -m state --state RELATED,ESTABLISHED,NEW -j ACCEPT
iptables -A QEMU2 -i vnet2 -o enp4s0 -j ACCEPT
iptables -A QEMU2 -i vnet2 -o lo -j ACCEPT
iptables -A QEMU2 -i vnet2 -j ACCEPT
iptables -A QEMU2 -o vnet2 -j ACCEPT

Ayrıca network’ü indirirken kullanıcacak bir script hazırla:

#!/bin/bash
iptables -F QEMU2

Gerekli Chain’i yarat:

iptables -N QEMU2
iptables -A FORWARD -j QEMU2

Dış IP’yi iç IP’ye routela:

iptables -t nat -A PREROUTING -p tcp -d 172.25.182.17 -j DNAT --to 172.25.182.16

Eğer başka bir makina için eklenmemişse, ekle: Aksi halde her makine için tekrar edilmesine gerek yoktur:

iptables -t nat -A PREROUTING -s 172.25.182.0/24 -j MASQUERADE

systemd servisini local’de ayarla:

qemu@gentooFarm2.service.d/00gentoo.conf

[Service]
Environment="ARGS=-enable-kvm -machine type=q35,accel=kvm -display curses -cpu SandyBridge -smp 4 -drive file=/mnt/store-1/%i.img,if=virtio -object rng-random,id=rng0,filename=/dev/urandom -device virtio-rng-pci,rng=rng0 -device virtio-net,netdev=vmnic -netdev tap,id=vmnic,ifname=vnet2,script=/mnt/store-1/gentooFarmNet2.sh,downscript=/mnt/store-1/gentooFarmNetDown2.sh -m 2G -k tr -nographic -name 'GentooFarm' -fsdev local,id=portage,path=/usr/portage,security_model=none -device virtio-9p-pci,fsdev=portage,mount_tag=portage  -fsdev local,id=overlay,path=/export.data/portage.overlays/,security_model=none -device virtio-9p-pci,fsdev=overlay,mount_tag=overlay  -fsdev local,id=portageetc,path=/export.data/portage.conf.d/etc/,security_model=none -device virtio-9p-pci,fsdev=portageetc,mount_tag=portageetc"

Environment="HALTCMD=echo 'system_powerdown' | nc localhost 7100"

qemu@.service

[Unit]
Description=QEMU virtual machine

[Service]
ExecStart=/usr/bin/qemu-system-x86_64 $ARGS
ExecStop=/bin/bash -c ${HALTCMD}
ExecStop=/bin/bash -c 'while nc localhost 7100; do sleep 1; done'

[Install]
WantedBy=multi-user.target