安装

原来用qemu命令的安装方式已经过时,用virt-install代替

安装需要的Centos命令

yum -y install qemu-kvm libvirt python-virtinst bridge-utils 
/etc/rc.d/init.d/libvirtd start 
chkconfig libvirtd on

cd /etc/sysconfig/network-scripts
cp ifcfg-eth0 ifcfg-br0

编辑ifcfg-br0

DEVICE=br0
TYPE=Bridge

编辑ifcfg-eth0

BRIDGE=br0

启动网络,如果没有发现virbr0起来,重启一下机器。

/etc/rc.d/init.d/network restart

安装Centos guest命令

virt-install -n centos -r 1024 -f centos.img -s 2 --vcpus=2 --os-type linux --os-variant=rhel6 --network bridge=br0 --cdrom ../CentOS-6.3-x86_64-minimal.iso --vnc

安装Xp guest命令

virt-install -n winxpsp3zh -r 512 -f template/winxpsp3zh.img -s 2 --vcpus=1 --os-type windows --os-variant=winxp --network bridge=br0 --cdrom iso/zh-hans_windows_xp_professional_with_service_pack_3_x86_cd_vl_x14-74070.iso --vnc

复制虚拟机命令

virt-clone --original winxpsp3zh --name tax --file /vms/images/tax.img

qcow2格式

如果要用到snapshot的话,默认virt-install的是raw文件格式,不支持snapshot,所以我们需要 转换成qcow2格式

qemu-img convert -f raw -O qcow2 ubuntuprecise ubuntuprecise.qcow2

然后编辑xml

virsh edit ubuntuprecise

主要修改 type和 source file

<disk type='file' device='disk'>
    <driver name='qemu' type='qcow2' cache='none'/>
    <source file='/vms/template/ubuntuprecise.qcow2'/>
    <target dev='hda' bus='ide'/>
    <address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>

创建qcow2镜像

qemu-img create -f qcow2 /var/lib/libvirt/images/guest.qcow2 8192

参考:

  • http://blog.sina.com.cn/s/blog_53ab41fd01013rc0.html
  • http://wiki.libvirt.org/page/After_import_a_guest_from_an_existing_disk_image_using_virt-install,the_guest_starting_stalls_with%22No_boot_device%22
  • http://virt-tools.org/learning/install-with-command-line/

修改 VM Guest的名称

virsh dumpxml {VM_GUEST_NAME} > foo.xml
<edit foo.xml, change the name, move storage>
virsh undefine myvm
virsh define foo.xml

添加 USB 设备到 Guest

virsh attach-device {VM_GUEST_NAME} setting xml.

add usb device
<hostdev mode='subsystem' type='usb'>
      <source>
        <address bus='7' device='3'/>
      </source>
</hostdev>

参考:

  • http://www.server-world.info/en/note?os=CentOS_6&p=kvm&f=1
  • https://wiki.archlinux.org/index.php/Kvm
  • http://www.coder4.com/archives/1160
  • http://www.techotopia.com/index.php/Installing_a_KVM_Guest_OS_from_the_Command-line_%28virt-install%29