一、先创建一个简单的虚拟机:
1.创建一个10G的虚拟磁盘:
#qemu-img create -f qcow2 xp.qcow2 10
#qemu-system-x86_64 -hda xp.qcow2 -m 1024 -boot d -cdrom ubuntu_x86_64.iso -spice port=5999,disable-ticketing
注:-d指光驱启动安装ubuntu_x86_64.iso,-c从硬盘启动,-n是从网络启动。如果是-dcn,则按顺序找启动项。
3.用spice命令连接:
使用spice连接此虚拟机,上面加了-spice port = 5999,disable-ticketing(默认是 VNC)
#spicec -h 10.20.65.240 -p 5999
二、创建一个常用的虚拟机:
同样创建一个磁盘文件,在/opt/keqi/monitors目录下创建一个monitor文件:
#touch /opt/keqi/monitors/730d16b6-985c-4393-8910-3ccc12c200132
同时配置网络,在/opt/keqi/下创建脚本netconfig.sh
#!/bin/sh
ifconfig eth0 0.0.0.0 promisc up
brctl addbr vSwitch0
brctl addif vSwitch0 eth0
ifconfig vSwitch0 hw ether 00:26:9e:fe:77:f0
ifconfig vSwitch0 10.20.65.52 netmask 255.255.255.0 up
route add default gw 10.20.65.254 dev vSwitch0
在该目录下创建子目录.network,并在其中创建脚本:
if-down:
#!/bin/bash
if [ -n "$1" ];then
/sbin/ifconfig $1 down
/usr/sbin/tunctl -d $1
exit 0
else
exit 1
fi
#!/bin/bash
if [ -n "$1" ];then
iface=$1
s=`echo $1 | cut -d"-" -f 2`
type=${s:0:1}
num=${s:1}
if [ "s" = $type ];then
net=vSwitch$num
else
if [ "v" = $type ];then
net=vLan-$num
else
net=eth$num
fi
fi
# sudo has error in redhat
/usr/sbin/tunctl -u `whoami` -t $iface
/sbin/ip link set $iface up
sleep 0.5s
/usr/sbin/brctl addif $net $iface
exit 0
else
echo "Error: no interface specified"
在一个终端 启动虚拟机命令如下:
/usr/local/bin/qemu-system-x86_64 -enable-kvm -M pc -nodefconfig -nodefaults -rtc base=localtime,clock=host -chardev socket,
id=monitor,path=/opt/keqi/monitors/730d16b6-985c-4393-8910-3ccc12c200132,server,nowait -mon chardev=monitor,
mode=readline -smp 1 -m 1024 -name UbuntuD-1110 -boot order=c -drive if=none,id=drive-fdc0-0-0,readonly=on,format=raw -global isa-fdc.
driveA=drive-fdc0-0-0 -drive file=/opt/datastore/default/ubuntu-11.10-desktop-amd64.iso,if=none,media=cdrom,id=drive-ide0-1-0,
readonly=on,format=raw -device ide-drive,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0 -drive media=disk,
file=/opt/datastore/default/UD_11_10.qcow2,if=none,id=drive-ide0-0-0,format=qcow2,cache=writethrough -device ide-drive,
bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -net nic,model=rtl8139,macaddr=00:e0:4c:31:c0:bd,vlan=0 -net tap,
script=/opt/keqi/.network/if-up,downscript=/opt/keqi/.network/if-down,ifname=tap-s0-66,vlan=0 -vga cirrus -soundhw ac97
-cpu qemu64,+ssse3,+sse4.1,+sse4.2,+x2apic -spice addr=0.0.0.0,port=5906,password=11111111 -usb -usbdevice tablet
-device virtio-serial-pci,id=virtio-serial0,max_ports=16,bus=pci.0 -chardev spicevmc,name=vdagent,id=vdagent -device virtserialport,nr=1,bus=virtio-serial0.0,
chardev=vdagent,name=com.redhat.spice.0
具体各个命令含义,到qemu官网查看。(注意:命令中file=/opt/datastore/default/ubuntu-11.10-desktop-amd64.iso 是你安装系统iso路径)
在另一个终端连接:
#spicec -h 10.20.65.52 -p 5906 -w 11111111