QEMU命令创建KVM Guest(bridge桥接)

时间:2023-03-10 01:37:38
QEMU命令创建KVM Guest(bridge桥接)

1. Check QEMU version

[root@pqsfc018 ~]# /usr/bin/qemu-system-x86_64 -version

QEMU emulator version 1.6.0, Copyright (c) 2003-2008 Fabrice Bellard

2. Check if the KVM modules are loaded

[root@pqsfc018 ~]# lsmod | grep kvm kvm_intel

134476  10 kvm

418302  1 kvm_intel

If not, run commands below to load

#modprobe kvm

#modprobe kvm_intel

3. Configure storage for kvm guest

#qemu-img create -f raw /var/lib/libvirt/images/pqsfc083.img 16G

4. Configure network for kvm guest

4.1 Use network instead of NetworkManager to control networks

#chkconfig NetworkManager off

#chkconfig --levels 35 network on

#service NetworkManager stop

4.2. Create net bridge on host new a ifconfig file named ifcfg-br0

#vi /etc/sysconfig/network-scripts/ifcfg-br0

DEVICE=br0

ONBOOT=yes

BOOTPROTO=static

IPADDR=15.xx.xx.xx

NETMASK=255.255.248.0

GATEWAY=15.xx.xx.xx

TYPE=Bridge

Add one line:

BRIDGE=br0

to ifconfig file ifcfg-eth0

[root@pqsfc070 network-scripts]# cat ifcfg-eth0

DEVICE=eth0

ONBOOT=yes

HWADDR=00:1A:4B:A5:69:B4

TYPE=Ethernet

BOOTPROTO=static

IPADDR=15.xx.xx.xx

NETMASK=255.255.248.0

BRIDGE=br0

#service network restart

4.3 Create a vnic for kvm guest(mapping to br0)

#yum install -y bridge-utils tunctl

#tunctl -u root -b -t vnet12

#ifconfig vnet12 up

#brctl addif br0 vnet12

<command to remove vnic>

#brctl delif br0 vnet12

#ifconfig vnet12 down

#tunctl -d vnet12

5. Create kvm guest by QEMU command

#/usr/bin/qemu-system-x86_64 -name pqsfc085 -enable-kvm -m 2048 -smp 2,sockets=2,cores=1,threads=1 \

-boot order=nc,once=d \

-hda /var/lib/pqsfc085.img \

-cdrom /root/RHEL6.4-20130130.0-Server-x86_64-DVD1.iso \

-net nic,model=virtio,macaddr=00:16:3e:3a:c0:99 \

-net tap,ifname=vnet12,script=no,downscript=no \

-vnc 127.0.0.1:66

Note:

1.-enable-kvm option is must, otherwise the system performance will be poor.

2.-boot order option specify the boot orders. n stands for boot from network, c stands for boot from HDD, d stand for boot from CN-ROM or ISO image

3.specify -net nic and -net tap to use bidege networks, if you don't specify these two options, the guest will use NAT as default.

4.We specify -vnc 127.0.0.1:66 here, you can login host with GUI and connect to guest by VNC viewer or ohter VNC tools

5.Specify a Mac address for the guest. Do not be duplicated to others.

6. Boot guest

#/usr/bin/qemu-system-x86_64 -name pqsfc085 -enable-kvm -m 2048 -smp 8,sockets=8,cores=8,threads=8 \

-hda /var/lib/pqsfc085.img \

-net nic,model=virtio,macaddr=00:16:3e:3a:c0:99 \

-net tap,ifname=vnet12,script=no,downscript=no \

-vnc 127.0.0.1:66