Qemu KVM libvirt Manual (1)

Source: Internet
Author: User
Tags nameserver dmesg
Install

Virtualization support is usually disabled in bios and must be enabled.

For Intel CPU, we can run the following command to check whether virtualization is supported.

# Grep "vmx"/proc/cpuinfo
Flags: fpu vme de pse tsc msr pae mce cx8 APIC Sep limit pge mca cmov Pat limit 36 clflush dts acpi mmx fxsr SSE sse2 ss ht tm pbe syscall NX limit rdtscp lm limit pebs BTS rep_good now.pl please wait until PNI then monitor your vmx smx est TM2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt 127aes xsave avx f16c rdrand lahf_lm Ida Arat EPB limit pln pts dtherm ACCEPT vpid fsgsbase SMEP ERMS

For amd cpu, execute the following command

Grep "SVM"/proc/cpuinfo

KSM: Kernel same page merging

Kernel samepage merging (KSM)(Also: Kernel shared memory, memory merging) lets the hypervisor system share identical memory pages amongst different processes or receivalized guests.

This is done by scanning through the memory finding duplicate pages. the duplicate pair is then merged into a single page, and mapped into both original locations. the page is also marked as "Copy-on-write", so the kernel will automatically separate them again shoshould one process modify its data.

KSM was originally intended to run more virtual machines on one host by sharing memory between processes as well as virtual machines.

Check if support

# Cat/boot/config-3.13.0-27-generic | grep KSM
Config_ksm = y

Check whether enable

# Cat/sys/kernel/MM/KSM/run
0

This is disable. If it is set to 1, it is enable.

Install

Apt-Get install KVM qemu-KVM

KVM Kernel Modules

# Lsmod | grep KVM
Kvm_intel 143060 27
KVM 451511 1 kvm_intel

Run the following command to load the kernel module:

Modprobe KVM

Modprobe kvm_intel

The dmesg command is used to print the Linux system boot information. The kernel stores the boot information in the ring buffer. You can use dmesg to view (print or control the kernel ring buffer) If you cannot view the information when starting the system ). The boot information is also saved in the/var/log/dmesg file. Some hardware devices (such as the 7th signaling card and voice card) usually install the driver (kernel module) during installation and print some information so that they can be viewed through the dmesg command.

Check whether KVM is loaded at startup.

Dmesg | grep KVM

Quick Start

The following process is required to create a virtual machine:

  • Create a virutal Hard Disk
  • Qemu or KVM is started with this disk and specified to install ISO
  • Like installing a Common Operating System

Qemu-IMG create-F qcow2/tmp/centos5.8.img 10g

Virt-install -- virt-type qemu -- name centos-5.8 -- Ram 2048 -- noreboot -- Disk Path =/tmp/centos5.8.img, format = qcow2, bus = virtio, device = disk -- CDROM =/tmp/CentOS-5.8-x86_64-bin-DVD-1of2.iso -- network = default -- graphics VNC, Listen = 0.0.0.0 -- noautoconsole

Qemu-IMG convert-O qcow2-C centos-5.8.img centos-5.8.1.qcow2

Use qemu

Qemu-IMG create-F qcow2 ubuntutest. IMG 5g

# Qemu-system-x86_64-enable-KVM-M 2048-hda ubuntutest. IMG-CDROM ubuntu-14.04-server-amd64.iso-boot d
Cocould not initialize SDL (no available video device)-exiting

SDLSimple DirectMedia Layer is a cross-platform, free, and open-source multimedia library written in C, it provides a simple interface for operating the graphic display, sound, and input devices of the hardware platform.

The image display in the qemu simulator uses SDL by default.

SDL features are very useful and powerful, but it also has a limitation that when you create a client and display it in SDL mode, a window will pop up directly, therefore, the SDL method can only be used in the graphic interface. If you use SDL in a non-graphical interface (such as connecting to the host through SSH), the following error message is displayed.

Because we are in SSH mode, we need to disable SDL and use VNC

# Qemu-system-x86_64-enable-KVM-name ubuntutest-M 2048-hda ubuntutest. IMG-CDROM ubuntu-14.04-server-amd64.iso-boot D-VNC: 19

After the installation is complete, you can start it from the hard disk.

Of course, the above startup cannot be connected to the Internet. Therefore, we also need to configure the network.

Qemu has the following network configurations:

  • None: disables a network card emulation on VM guest. Only the loopback lo network interface is available.
  • Bridge: uses a specified network helper to configure the tap interface and attach it to a specified bridge.
  • NIC: Creates a new network interface card (NIC) and connects it to a specified Virtual Local Area Network (VLAN ).
  • User: Specifies a user-mode networking.
  • Tap: Specifies a bridged or routed networking.

We can usually define a virtual network card through the following method

-net nic,vlan=1,macaddr=00:16:35:AF:94:4B,model=virtio,name=ncard1
VLAN = 1 is the ID of the virtual network. If this parameter is not specified, the default value is 0. This virtual network is completely different from IEEE 802.1Q. It refers to the network defined by qemu.
You can specify the MAC address.
You can view the model.

# Qemu-system-x86_64-enable-KVM-net Nic, model =?
Qemu: supported Nic models: ne2k_pci, i82551, i82557b, i82559er, rtl8139, e1000, pcnet, virtio

Configure user networking (slirp)

This is the default networking backend and generally is the easiest to use. It does not require root/administrator privileges. It has the following limitations:

  • There is a lot of overhead so the performance is poor
  • ICMP traffic does not work (so you cannot use Ping within a guest)
  • The guest is not directly accessible from the host or the external network

 

qemu-system-x86_64 -enable-kvm -name ubuntutest  -m 2048 -hda ubuntu-14.04.img -boot c -vnc :19 -net user -net nic,model=virtio
The VM Guest allocates an IP address from a virtual DHCP server. VM Host Server (the DHCP server) is reachable at 10.0.2.2, while the IP address range for allocation starts from 10.0.2.15. You can use ssh to connect to VM Host Server at 10.0.2.2, and scp to copy files back and forth.

Configure bridge networking

With the-net tap option, qemu creates a Network Bridge by connecting the host tap network device to a specified VLAN of VM guest. Its network interface is then visible to the rest of the network.

bridge=br0
  • Name of the bridge device.
tap=$(sudo tunctl -u $(whoami) -b)
  • Prepare a new TAP device and assign it to the user who runs the script. Tap devices are virtual network devices often used for virtualization and emulation setups.
sudo ip link set $tap up
  • Bring up the newly created tap network interface.
sudo brctl addif $bridge $tap
  • Add the new TAP device to the Network Bridge br0.0.
qemu-kvm -m 512 -hda /images/sles11sp1_base.raw 
-net nic,vlan=0,model=virtio,macaddr=00:16:35:AF:94:4B 
-net tap,vlan=0,ifname=$tap,script=no,downscript=no
  • The ifname = suboption specifies the name of the TAP network interface used for bridging.
  • Before qemu-KVM connects to a Network Bridge, it checks the script and downscript values. if it finds the specified scripts on the VM host Server File System, it runs the script before it connects to the Network Bridge and downscript after it exits the network environment. you can use these scripts to first set up and bring up the bridged network devices, and then to deconfigure them. by default,/etc/qemu-IFUP and/etc/qemu-ifdown are examined. if script = No and downscript = No are specified, the script execution is disabled and you have to take care manually.
sudo brctl delif $bridge $tap
  • Deletes the tap interface from a Network Bridge br0.0.
sudo ip link set $tap down
  • Sets the state of the TAP device to 'drop '.
sudo tunctl -d $tap
  • Deconfigures the tap device.

(1) Create bridge br0 on the host machine

Brctl addbr br0

(2) Set br0 to up

IP link set br0 up

(3) create a tap Device

# Tunctl-B
Tap0

(4) set tap0 to up

IP link set tap0 up

(5) add tap0 to br0.

Brctl addif br0 tap0

(6) Start A Virtual Machine

Qemu-system-x86_64-enable-KVM-name ubuntutest-M 2048-hda ubuntu-14.04.img-boot C-VNC: 19-net Nic, model = virtio-net tap, ifname = tap0, script = No, downscript = No

Virtual Machine connection tap0, tap0 connection br0

(7) after the VM is started, the NIC is not configured, so it cannot connect to the Internet. First, set an IP address for br0.

Ifconfig br0 192.168.57.1/24

(8) set the IP address for the NIC in the VM.

Ifconfig eth0 192.168.57.2/24

At this time, you can ping 192.168.57.1 on the VM, but still cannot access the Internet.

(9) set nat on the host and enable IP Forwarding

# Sysctl-P
Net. ipv4.ip _ forward = 1

Sudo iptables-T Nat-A postrouting-O eth0-J Masquerade

(10) set the default gateway in the Virtual Machine

Route add-net default GW 192.168.57.1

At this time, you can ping the Internet gateway.

Ping 16.158.164.1

(11) then set DNS in the virtual machine, you can perform apt-Get

# Cat/etc/resolv. conf
# Dynamic resolv. conf (5) file for glibc resolver (3) generated by resolvconf (8)
# Do not edit this file by hand -- your changes will be overwritten
Nameserver 16.110.135.52
Nameserver 16.110.135.51

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.