After CentOS 6 loads the KVM module, we cannot manage virtual machines. If we need to manage KVM virtual machines, we also need management tools. Let’s take a look at the KVM management tool stack first.
yum grouplist |grep -i "virtualization"
Virtualization:
qemu-kvm
Virtualization Client:
python-virtinst, virt-manager, virt-viewer
Virtualization Platform:
libvirt, libvirt-client
Virtualization Tools:
libguestfs
Install qemu-kvm:
yum -y install qemu-kvm ln -sv /usr/libexec/qemu-kvm /usr/bin/
Use of qemu-kvm:
Command options:
Standard options
Show options
block device options
Network options
i386 platform specific options
character device options
Bluetooth Device Options
Linux boot specific options
Debug/Expert Mode Options
Standard options for qemu-kvm:
The standard options of qemu-kvm mainly involve specifying the host type, CPU mode, NUMA, floppy drive device, optical drive device and hardware device.
-name name: Set the virtual machine name
-M machine: Specify the type of host to simulate, such as standard PC, ISA-only PC or Intel-Mac, etc. You can use "qemu-kvm -M ?" to get all supported types
-m megs: Set the RAM size of the virtual machine
-cpu model: Set the CPU model, such as coreduo, qemu64, etc., you can use "qemu-kvm -cpu ?" to get all supported models
-smp n[,cores=cores][,threads=threads][,sockets=sockets][,maxcpus=maxcpus]: Set the number of CPUs in the simulated SMP architecture, the number of cores per CPU and the number of CPUs The number of sockets, etc.: Up to 255 CPUs can be simulated on the PC: maxcpus is used to specify the upper limit of the number of CPUs for hot insertion
-numa opts: Specify a numa device that emulates multiple nodes
-fda file:
-fdb file: use the specified file (file) as the floppy disk image, the file is /dev/fd0 means use the physical floppy drive
-hda file:
-hdb file:
-hdc file:
-hdd file: use the specified file as the hard disk image
-cdrom file: use the specified file as the CD-ROM image, it should be noted that -cdrom and -hdc cannot be used at the same time: specify the file as /dev/cdrom to directly use the physical CD-ROM drive
-drive option[,option[,option[,...]]]: Define a hard disk device: there are many available sub-options.
file=/path/to/somefile: hard disk image file path
if=interface: Specifies the hard disk device connected to
index=index: Set the index number of different devices in the same controller type, that is, the identification number
media=media: Defines the media type as hard disk or CD
snapshot=snapshot: Specifies whether the current hard disk device supports the snapshot function: on or off
cache=cache: defines how to use the physical machine cache to access block data. The available values are none, writeback, unsafe and writethrough.
format=format: specifies the format of the image file. For the specific format, please refer to the qemu-img command
-boot [order=drives][,once=drives][,menu=on|off]: Define the boot order of the boot device, each device is represented by one character: the devices supported by different architectures and their representation characters are endless In the same way, on the x86 PC architecture, a and b represent the floppy drive, c represents the first CD-ROM device, np represents the network adapter, and the default is the hard disk device. For example: -boot order=dc,once=d
Display options for qemu-kvm:
Display options are used to define the related types and properties of the display interface after the virtual machine is started.
-nographic: By default, qemu uses SDL to display VGA output, and this option is used to disable the graphics interface. At this time, qemu is similar to a simple command line program, and its emulated serial port device will be redirected to the console
-curses: disable graphical interface and use curses/ncurses as interactive interface
-alt-grab: use Ctrl+Alt+Shift to release the mouse
-ctrl-grab: use the right Ctrl key to release the mouse
-sdl: enable SDL
-spice option[,option[,...]]: enable spice remote desktop protocol: there are many sub-options, please refer to the qemu-kvm manual for details.
-vga type: Specify the type of VGA interface to emulate. Common types are:
cirrus: Cirrus Logic GD5446 Graphics Card
std: Standard VGA graphics card with Bochs VBI extension
vmware: VMware SVGA-II compatible display adapter
qxl: QXL paravirtualized graphics card: Compatible with VGA, it can work in a very good way after installing the qxl driver in the Guest, it is recommended to use this type when using the spice protocol
none: disable VGA card
-vnc display[,option[,option[,...]]]: By default, qemu uses SDL to display VGA output; with the -vnc option, qemu can listen on vnc and redirect VGA output to vnc Session, when using this option, you must use the -k option to specify the keyboard layout type; there are many sub-options, please refer to the manual of qemu-kvm for details
display:
1) host:N
172.16.100.7:1, listening on port 5900+N of the main 172.16.100.7
2) unix:/path/to/socket_file
3) none
options:
password: The password needs to be verified when connecting. Set the password and use change through the monitor interface.
reverse: "reverse" connection to a listening vncview
-monitor stdio: Indicates the TV monitor interface on standard input and output
-nographic
Ctrl-a,c: switch between console and monitor
Ctrl-a,h: Display help information
Network Properties Related Options
Network attribute related options are used to define the interface type of the network device and its related attributes and other information. Only the attributes of three types of network interfaces, nic, tap and user, are introduced here. For other types, please refer to the qemu-kvm manual.
-net nic[,vlan=n][,macaddr=mac][,model=type][,name=name][,addr=addr][,vectors=v]: Create a new NIC device and connect to vlan In n: the default NIC on the PC architecture is e1000, macaddr is used to specify a mac address for it, and name is used to specify an online device name displayed during monitoring; qemu can simulate multiple types of network card devices, such as virtio, i82557b, i82559er, ne2k_isa, pcnet, rtl8139, e1000, smc91c111, lance, mcf_fec, etc; " to get the types supported by the current platform.
-net tap[,vlan=n][,name=name][,fd=h][,ifname=name][,script=file][,downscript=dfile]: connect to vlan through the TAP network interface of the physical machine n, use the script specified by script=file (default is /etc/qemu-ifup) to configure the current network interface, and use the script specified by downscript=file (default is /etc/qemu-ifdown) to undo the interface configuration; use script=no and downscript=no can be used to disable script execution respectively.
-net user[,option][,option][,...]: Configure the network stack in user mode, which does not depend on administrative privileges; valid options are:
vlan=n: connect to vlan n, default n=0
name=name: Specifies the display name of the interface, commonly used in monitoring mode
net=addr[/mask]: Set the IP network visible in GuestOS, the mask is optional, the default is 10.0.2.0/8
host=addr: Specify the IP address of the physical machine seen in the GuestOS, the default is the second one in the specified network, that is, xxx2
dhcpstart=addr: Specifies the starting IP of the 16 addresses in the DHCP service address pool, the default is the 16th to 31st, that is, xxx16-xxx31
dns=addr: Specify the dns server address visible to GuestOS, the default is the third address in the GuestOS network, that is, xxx3
tftp=dir: activate the built-in tftp server and use the specified dir as the default root directory of the tftp server
bootfile=file: BOOTP file name, used to implement network booting GuestOS, such as: qemu -hda linux.img -boot n -net user,tftp=/tftpserver/pub,bootfile=/pexlinux.0
i386 platform specific options
-no-acpi: Disable ACPI function, use this option when GuestOS has compatibility problems with ACPI
-ballcon none: disable the balloon device
-balloon virtio[,addr=addr]: enable virtio balloon device
Create the first virtual machine:
wget http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-i386-disk.img #download cirros image qemu-kvm -m 256 -name tst -smp 2 -hda cirros-0.3.4-i386-disk.img #Use qemu-kvm to start the virtual machine
yum -y install tigervnc #install vnc vncviewer :5900 #Use vncviewer to remote to the virtual machine
In fact, the above command to create a virtual machine is equivalent to the following command:
qemu-kvm -m 128 -name test -smp 2 -drive file=/images/kvm/cirros-0.3.4-i386-disk.img,if=virtio,media=disk,cache=writeback,format=qcow2
However, the disk image file is specified here using -drive,
Create a Linux virtual machine with qemu-kvm:
qemu-img create -f qcow2 -o size=10G /images/kvm/centos5.img qemu-img info /images/kvm/centos5.img qemu-kvm -name "centos5.5" -m 512 -smp 2 -boot d -drive file=/images/kvm/centos5.img,if=virtio,index=0,media=disk,format=qcow2 -drive file=/opt/CentOS-5.5-i386-bin-DVD.iso,index=1,media=cdrom -net nic,model=virtio,macaddr=52:54:00:A6:71:83 -vga cirrus
After the virtual machine is created, use vncviewer to connect
The system installation interface appears, and the installation process is omitted.
After the system is installed, the reboot interface appears, but after rebooting, the virtual machine still cannot enter the centos system. This is because when qemu-kvm creates a virtual machine, two drive devices have been configured, one is a hard disk device and the other is an optical drive device, and the system can start using the disk device only when the optical drive device is removed.
qemu-kvm -name "centos5.5" -m 512 -smp 2 -boot d -drive file=/images/kvm/centos5.img,if=virtio,index=0,media=disk,format=qcow2 -net nic,model=virtio,macaddr=52:54:00:A6:71:83 -vga cirrus
Use vncviewer to connect again and see the following screen,
In this way, after the virtual machine is powered on, it enters the centos system instead of the system installation interface.
Successfully entered the previously installed system, note that the MAC address here is just what we have set.
However, although the system is installed, the virtual machine cannot obtain the ip address, which always seems inappropriate, so here is an attempt to add a network.
First, execute the following command on the host:
yum -y install bridge-utils brctl addbr br0 brctl addif br0 eth0 ifconfig eth0 0.0.0.0 ifconfig br0 192.168.49.10 netmask 255.255.255.0 brctl show
[root@kvm-node1 ~]# brctl show bridge namebridge idSTP enabledinterfaces br08000.000c2981bdbeyesbr0.0 eth0
Then, use qemu-kvm to create a virtual machine,
qemu-kvm -name "centos5.5" -m 512 -smp 2 -boot d -drive file=/images/kvm/centos5.img,if=virtio,index=0,media=disk,format=qcow2 -net nic -net tap,ifname=br0.0,script=no -vga cirrus
After the virtual machine is started, we can use the ifconfig command on the host to see the network card named br0.0,
Manually add br0.0 to the br0 bridge and assign it an ip address,
brctl addif br0 br0.0 ifconfig br0.0 192.168.49.100 netmask 255.255.255.0 up
Finally, use vncviewer to connect to the virtual machine,
At this point, access the virtual machine named centos5.5 created by kvm from the host machine,
Using ssh, you can also successfully connect.
Use qemu-kvm to create a windows virtual machine:
qemu-img create -f qcow2 -o size=20G /images/kvm/winxp.img qemu-img info /images/kvm/winxp.img qemu-kvm -name winxp -smp 2,socket=1,cores=2,threads=1 -m 512 -drive file=/images/kvm/winxp.img,if=ide,media=disk,cache=writeback,format=qcow2 -drive file=/opt/DBX_GHOST_XP_SP3_CJ_V3.5.iso,media=cdrom
connect using vncviewer,
Installation process omitted
After the installation is complete, if you want to use the image file to enter the windows system, it is best to shut down the virtual machine, and then use the following command to recreate:
qemu-kvm -name winxp -smp 2,socket=1,cores=2,threads=1 -m 512 -drive file=/images/kvm/winxp.img,if=ide,media=disk,cache=writeback,format=qcow2