KVM Virtual Machine and qemu (command line option)

Source: Internet
Author: User
KVM
Install RHEL/Fedora/centos
yum install bridge-utils kvm

Bridge-utils is a network card bridge tool,


Example 1: RedHat system KVM
Create Disk
# qemu-img create -f qcow2 turbo-10.5.5-rc2.img 20G
Formatting 'turbo-10.5.5-rc2.img', fmt=qcow2, size=20971520 kB
# file turbo-10.5.5-rc2.img
turbo-10.5.5-rc2.img: QEMU Copy-On-Write disk image version 2, size 5 + 0

Configure bridging

Configuration/etc/sysconfig/network-scripts
The following network script

# cat /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
ONBOOT=yes
BOOTPROTO=static
IPADDR=172.16.70.30
NETMASK=255.255.252.0
GATEWAY=172.16.68.1
TYPE=Bridge
# cat /etc/sysconfig/network-scripts/ifcfg-eth0
# 3Com Corporation 3c905B 100BaseTX [Cyclone]
DEVICE=eth0
#BOOTPROTO=dhcp
#HWADDR=00:01:02:8C:50:09
ONBOOT=yes
BRIDGE=br0

Add a bridge network interface and add the configured eth0 to the bridge interface.

Brctl addbr br0 # create a bridge Interface
Brctl addif br0 eth0 # Add eth0 to br0, important

Use the following script:

# cat /etc/init.d/qemu-ifup
#!/bin/bash

switch=br0

if [ -n "$1" ];then
/sbin/ip link set $1 up
sleep 0.5s
/usr/sbin/brctl addif $switch $1
exit 0
else
echo "Error: no interface specified"
exit 1
fi

Use a bridge Nic

You can use a bridge Nic to start qemu-KVM and use two gtes10.5.5 files
NFS installation system:

# qemu-kvm -m 512 -hda turbo-10.5.5-rc2.img -kernel vmlinuz /
-initrd initrd.img -net nic -net tap,script=/etc/init.d/qemu-ifup --daemonize

You can also use ISO for installation. Therefore, you need to control multiple ISO distributions in qemu.
Disk replacement:

# Qemu-KVM-M 512-hda turbo-10.5.5-rc2.img-CDROM your ISO path/
-Net NIC-net tap, script =/etc/init. d/qemu-IFUP-boot d -- daemonize

Go to the qemu console, and press and hold the mouse to focus on the qemu interface.CTRL + ALT + 2
Into
Log on to the console:

(Qemu) Change CDROM your other ISO

If the command above prompts "device not found ". A device ing error may occur, as shown in the following code:

(Qemu) info Block
...
(Qemu) Change ide1-cd0 ISO file

Common qemu usage
Use VNC
# qemu-kvm -m 512 -hda xp.img -net nic,macaddr=00:00:11:33:02:02 /
-net tap,ifname=tap2,script=/etc/init.d/qemu-ifup /
-localtime -vnc 172.16.70.30:2 --daemonize

The above allows the KVM Virtual Machine to run in the background and start the VNC server, so that we can use the VNC client
User link: 172.16.70.30: 5092.


Qemu options:
General options
-M Machine
Select a simulated machine (we can enter-m? A simulated machine list is mentioned)
-FDA File
-FDB File
Use file as a floppy disk image. You can also use/dev/fd0 as the file name to use the host floppy disk.
-Hda File
-HDB File
-HDC File
-HDD File
Use file as the image of Hard Disk 0, 1, 2, and 3.
-CDROM File
Use a file as a CD-ROM image (but we cannot make
Use '-HDC' and '-CDROM'). You can use '/dev/CDROM' as the file name.
Use the host's CD-ROM.
-Boot [A | c | D]
It is started by floppy disk (A), hard disk (c), or CD-ROM (d). By default.
-Snapshot
Write a temporary file instead of a disk image file. In this case
The disk image file we use. However, we can force write by pressing C-a s.
Back to the disk image file.
-M megs
Set the virtual memory size to megs M bytes. The default size is 128 M.
-SMP n
Simulate an SMP system with N CPUs. It is the target PC and can support up to 255 CPUs.
-Nographic
In general, qemu uses SDL to display VGA output. With this option, we can
Disable all graphic output, so that qemu is just a simple command line program.
The port will be redirected to the command line. Therefore, we can still use the serial port
To debug the Linux kernel.
-K Language
Use the keyboard layout language (for example, FR is French ).
We do not need to use this in PC/Linux or PC/Windows hosts.
Option. The default value is en-us. The available layout is as follows:
ar  de-ch  es  fo    fr-ca  hu  ja  mk    no  pt-br  sv
da en-gb et fr fr-ch is lt nl pl ru th
de en-us fi fr-be hr it lv nl-be pt sl tr
-Audio-help
This option displays the Audio Subsystem help: Driver list and callable parameters.
-Soundhw card1, card2... or-soundhw all
Allow sound and select sound hardware. Use? List all available sound hardware
qemu -soundhw sb16,adlib hda
qemu -soundhw es1370 hda
qemu -soundhw all hda
qemu -soundhw ?
-Localtime
Set the clock to Local Time (UTC time by default). If you are on a MS-DOS or Windows
This option requires the correct date.
-Full-screen
Start in full screen mode.
-Pidfile File
The qemu process PID is stored in the file. If it is started by a script, select
Items are quite useful.
Win2k-hack
This option can be used to avoid disk errors when installing Windows 2000.
In Windows 2000, we no longer need this option (this option reduces
Transmission speed ).

USB Option
-USB
Allow USB Drivers (will soon be the default option)
-Usbdevice devname
Add the USB device name. You can view the monitor command usb_add to obtain more detailed information.

Network options
-Net Nic [, VLAN = N] [, macaddr = ADDR]
Create a new Nic and connect to VLAN n (n = 0 by default ).
Nic is currently ne2000. as an option, the MAC address can be changed. If
If the-net option is not specified, a single Nic is created.
-Net user [, VLAN = N]
The user-mode network stack does not require the Administrator permission to run.
Fixed-net options, which will be the default situation.
-Net tap [, VLAN = N] [, FD = H] [, ifname = Name] [, script = file]
Connect the tap network interface name to vlan n and use the network configuration script file.
Configuration. The default network configuration script is/etc/qemu-IFUP. If no name is specified
A. FD = H will be automatically specified to specify
Handle. Example:
qemu linux.img -net nic -net tap

The following is a more complex example (two NICs, each connected to a tap device ):

qemu linux.img -net nic,vlan=0 -net tap,vlan=0,ifname=tap0 /
-net nic,vlan=1 -net tap,vlan=1,ifname=tap1
-Net
Socket [, VLAN = N] [, FD = H] [, Listen = [host]: Port] [, connect = Host: Port]
Use TCP socket to connect VLAN n to the VLAN of another remote qemu VM
If listen is specified, qemu listens to the connection request on the port (host is optional ).
Connect can be used to connect to another qemu instance using the listen option.
Connection. FD = H specifies an opened TCP socket. For example:
# launch a first QEMU instance
qemu linux.img -net nic,macaddr=52:54:00:12:34:56 -net socket,listen=:1234
# connect the VLAN 0 of this instance to the VLAN 0 of the first instance
qemu linux.img -net nic,macaddr=52:54:00:12:34:57 -net
socket,connect=127.0.0.1:1234
-Net socket [, VLAN = N] [, FD = H] [, MCAST = maddr: Port]
Create a vlan n and use the UDP multi-access communication socket to connect with other qemu virtual machines.
Line sharing, especially for each qemu that uses the same multi-access communication address and port
Bus. Here we should pay attention to the following points:
  • Several qemus can run on different hosts but use the same bus.
    Set correct multi-access communication for these hosts)
  • MCAST is compatible with Linux in user mode.
  • Use FD = h to specify an opened UDP multi-access communication set interface.

    For example:

# launch one QEMU instance
qemu linux.img -net nic,macaddr=52:54:00:12:34:56 -net
socket,mcast=230.0.0.1:1234
# launch another QEMU instance on same "bus"
qemu linux.img -net nic,macaddr=52:54:00:12:34:57 -net
socket,mcast=230.0.0.1:1234
# launch yet another QEMU instance on same "bus"
qemu linux.img -net nic,macaddr=52:54:00:12:34:58 -net
socket,mcast=230.0.0.1:1234

The following is an example of Linux in user mode:

# launch QEMU instance (note mcast address selected is UML's default)
qemu linux.img -net nic,macaddr=52:54:00:12:34:56 -net
socket,mcast=239.192.168.1:1102
# launch UML
/path/to/linux ubd0=/path/to/root_fs eth0=mcast
-Net none
Indicates that no network device needs to be configured. If the-net option is not specified, it will be used to override
Cover the active default configuration.
-TFTP prefix
When the user mode network stack is used, a built-in TFTP server is activated. All
Files starting with prefix will be downloaded from the host using a TFTP client.
The local TFTP client must be configured in binary mode (the Unix TFTP client is used)
The Host IP address on the client, for example, 10.0.2.2.
-SMB dir
-Redir [TCP | UDP]: Host-Port: [guest-host]: Guest-Port
When the user mode grid stack is used, the TCP or UDP connection to the host port host-port is
The connection is redirected to the client port guest-port. If no client port is specified,
The value is 10.0.2.15 (the default address is specified by the built-in DHCP server ). For example:
To redirect the X11 connection from Screen 1 to screen 0 on the client, we can use
Method:
# on the host
qemu -redir tcp:6001::6000 [...]
# this host xterm should open in the guest X11 server
xterm -display :1
To redirect telnet connections from host port 5555 to telnet port on
the guest, use the following:

# on the host
qemu -redir tcp:5555::23 [...]
telnet localhost 5555

Then when we use it on the host Telnet localhost 5555, we connect
The telnet server of the client.


Linux Startup options

When we use these options, we can use a specified kernel
Install it in a disk image. This is quite useful for simple testing of various kernels.

'-Kernel bzimage'
Use bzimage as the kernel image.
'-Append into Line'
Use cmdline as the command line of the kernel.
'-Initrd file'
Use file as the initial RAM disk.

Debugging options
'-Serial Dev'
Redirects the virtual string to the device Dev of the host. The available devices are as follows:
VC
Virtual Terminal
Pty
(Linux) pseudo tty (automatically allocate a new TTY)
Null
Empty Device
/Dev/XXX"
(Linux) use the TTY of the host. For example, '/dev/ttys0 '. The serial port parameters of the host are set through simulation.
/Dev/parportn
(Linux) uses the host's parallel port n. Currently, only spp parallel features can be used.
File: Filename
Write the output to the filename file. No characters are readable.
Stdio
(UNIX) standard input/output
Pipe: Filename
(UNIX) famous pipe filename

The default device in the graphic mode is VC, and the device in the non-graphic mode is stdio. This option
It can be used multiple times. Up to four serial ports can be simulated.

'-Parallel Dev'

Redirect the virtual parallel port to the device Dev of the host (the same as that of the serial port ). In Linux
On the machine, '/dev/parportn' can be used to use the hardware settings connected to the corresponding parallel port.
Backup. This option can be used multiple times, and up to three parallel ports can be simulated.

'-Monitor dev'

Redirect the browser to the device Dev of the host (the same as that of the serial port ). In graphic mode
The default device is VC, but stdio in non-graphic mode.

'-S'
Wait for GDB to connect to port 1234.
'-P port'
Change the gdb connection port.
'-S'
CPU is not started at startup (we must enter 'C' in the monitor ')
'-D'
Output logs to/tmp/qemu. Log
'-Hdachs C, H, S, [, T]'
Physical Parameters of force hard disk 0 (1 <= C <= 16383, 1 <= H <= 16, 1 <= s
<= 63), and you can select the forced BIOS conversion mode (t = none, LBA or auto ).
Often qemu can detect these parameters. This option is equivalent to the disk image of the old MS-DOS
Useful.
'-STD-VGA'
Simulate a standard VGA graphics card (Cirrus logic by default) with an bochs VBE Extension
Gd5446 pci vga)
'-Loadvm file'
Starts from a save status.

Key combination

During graphic simulation, we can use the following key combinations:

CTRL-alt-F
Full Screen
CTRL-Alt-N
Switch virtual terminal 'n'. The standard terminal ing is as follows:
  • N = 1: The target system displays
  • N = 2: Browser
  • N = 3: Serial Port
    CTRL-alt
    Capture the mouse and keyboard

    In the virtual console, we can use ctrl-up, Ctrl-down, Ctrl-Pageup
    And Ctrl-Pagedown move in the screen.

During simulation, if we use the '-nographic' option, we can use ctrl-a h to get the terminal command:

CTRL-A H
Print help information
CTRL-A x
Exit Simulation
CTRL-A S
Save disk information to a file (if it is-Snapshot)
CTRL-A B
Interrupt
CTRL-A C
Switch with the monitor on the console
CTRL-A Ctrl-
Send Ctrl-

Disk Image

Since 0.6.1, qemu supports multiple disk image formats, including increasing Disk Images, compression and Addition
Encrypted disk image.

We can use the following command to create a disk image:

qemu-img create myimage.img mysize

Here myimage. IMG is the file name of the disk image, and mysize is the size represented by K. We
You can use M to indicate the size or G as the prefix and use g to indicate the size.

Qemu-IMG Option

The following commands are supported:

`create [-e] [-b base_image] [-f fmt] filename [size]'
`commit [-f fmt] filename'
`convert [-c] [-e] [-f fmt] filename [-O output_fmt] output_filename'
`info [-f fmt] filename

Command Parameters

Filename
Disk image file name.
Base_image
Read-Only disk images can be used as the basis for copying to the write image.
Only the modified data is stored.
FMT
The disk image format can be automatically detected in most cases. The following formats are supported:
  • Raw

    Raw disk format (default). This format is simple and easy to transplant to other Simulators
    Advantage. If our file system supports holes (for example, ext2 on Linux or
    Ext3), and then only the written part is left blank. Use qemu-IMG info to obtain the ing.
    Such as the actual size or the use of LS-ls on Unix/Linux.

  • Qcow

    Qemu image format. The most common format. You can use it to obtain a smaller image (if we
    The file system does not support holes. For example, in windows, this is quite useful.
    Use AES encryption or zlib-based compression.

  • Cow

    Linux copies of the user mode in the write image format. As a growing image in qemu
    Format used. This option is only for compatibility with previous versions and cannot be used on Win32
    Use.

  • Vmdk

    VMware 3 or 4 compatible image formats.

  • Cloop

    Linux compressed circular image, reuse directly compressed CD-ROM image.

  • Size

    The size of the disk image in K format. At the same time, m or G can be used as the prefix.

  • Output_filename

    Target disk image file name

  • Output_fmt

    Target format

  • -C

    Indicates that the target image must be compressed (only in qcow format)

  • -E

    Indicates that the target image must be encrypted (only in qcow format)

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.