ubuntu 虚拟化 virt命令

查看所有创建的虚拟机
virsh list --all

开机
virsh start win7

关机
virsh shutdown win7

关电源
virsh destroy win7


引用

三、安装guest虚拟机

1、直接通过virt-manager安装、管理虚拟机(略)

2、通过命令行安装guest虚拟机
[root@target ~]# yum install virt-viewer   #开启图形控制台安装虚拟客户机需要,
[root@target ~]# virt-install \            #安装选项可用virt-install --help查看
--name node4 \           #虚拟机名
--ram=1024 \             #分配内存大小,MB
--arch=x86_64 \          #模拟的CPU 构架
--vcpus=1 \              #配置虚拟机的vcpu 数目
--check-cpu \            #检查确定vcpu是否超过物理 CPU数目,如果超过则发出警告。
--os-type=linux \        #要安装的操作系统类型,例如:'linux'、'unix'、'windows'
--os-variant=rhel5 \     #操作系统版本,如:'Fedora6', 'rhel5', 'solaris10', 'win2k'
--disk path=/virhost/node7.img,device=disk,bus=virtio,size=20,sparse=true \   #虚拟机所用磁盘或镜像文件,size大小G
--bridge=br0 \           #指定网络,采用透明网桥
--noautoconsole \        #不自动开启控制台
--pxe                    #网络安装

四、利用virsh对虚拟机管理
1、开关机
[root@target ~]# virsh start node4      #开机
[root@target ~]# virsh create /etc/libvirt/qemu/node4.xml #直接通过主机配置文档启动主机
[root@target ~]# virsh shutdown node4   #关机
[root@target ~]# virsh destroy node4    #强制关闭电源
[root@target ~]# virsh list --all       #查看虚拟机状态        
Id 名称               状态
----------------------------------
18 node4                running
  - node5                关闭
  - win8                 关闭


2、添加删除虚拟机
[root@target ~]# virsh define /etc/libvirt/qemu/node5.xml   #根据主机配置文档添加虚拟机
[root@target ~]# virsh list --all        #node5已经添加
Id 名称               状态
----------------------------------
18 node4                running
  - node5                关闭
  - win8                 关闭

[root@target ~]# virsh undefine node5     #移除虚拟机
[root@target ~]# ls /etc/libvirt/qemu
networks  node4.xml  win8.xml
[root@target ~]# virsh list --all         #node5已经被移除
Id 名称               状态
----------------------------------
18 node4                running
  - win8                 关闭


3、远程管理虚拟机(qemu+ssh连接)
[root@target ~]# yum install virt-viewer
[root@target ~]# export DISPLAY=192.168.40.18:0.0
[root@target ~]# virt-viewer -c qemu///system node4                          #本地管理虚拟机,system:获取system权限,注意qemu后是三个/
[root@manager ~]# virt-viewer -c qemu+ssh://[email protected]/system node4  #远程linux通过virt-viewer+ssh管理虚拟机
Xlib:  extension "RANDR" missing on display "192.168.40.18:0.0".
[email protected]'s password:
[email protected]'s password:
#会弹出virt-viwer的gtk管理界面


4、使用已存在的虚拟机配置文档安裝新的虚拟机
[root@target ~]# qemu-img create -f qcow2 /virhost/kvm_node/node6.img 20G
#为新建虚拟机生产磁盘镜像文件


[root@target ~]# virsh list
Id 名称               状态
----------------------------------
18 node4                running


[root@target ~]# virsh dumpxml node4 >/etc/libvirt/qemu/node6.xml
#导出虚拟机node6的硬件配置信息为/etc/libvirt/qemu/node6.xml


[root@target ~]# vim /etc/libvirt/qemu/node6.xml
<domain type='kvm' id='20'>    #修改node6的id号
  <name>node6</name>           #虚拟机node6的name
  <uuid>4b7e91eb-6521-c2c6-cc64-c1ba72707fc7</uuid>  #uuid必须修改,否则会和node4的冲突
  <memory>524288</memory>
  <currentMemory>524288</currentMemory>
  <vcpu>1</vcpu>
  <os>
    <type arch='x86_64' machine='rhel5.4.0'>hvm</type>
    <boot dev='network'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' cache='none'/>
      <source file='/virhost/node4.img'/>         #指定新虚拟机的硬盘文件
      <target dev='vda' bus='virtio'/>
    </disk>
    <interface type='bridge'>
      <mac address='54:52:00:69:d5:c7'/>
      <source bridge='br0'/>
      <target dev='vnet0'/>
      <model type='virtio'/>
    </interface>
    <interface type='bridge'>
      <mac address='54:52:00:69:d5:d7'/>
      <source bridge='br0'/>
      <target dev='vnet1'/>
      <model type='virtio'/>
    </interface>
    <serial type='pty'>
      <source path='/dev/pts/4'/>
      <target port='0'/>
    </serial>
    <console type='pty' tty='/dev/pts/4'>
      <source path='/dev/pts/4'/>
      <target port='0'/>
    </console>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='5900' autoport='yes' keymap='en-us'/>
  </devices>
</domain>

[root@target ~]# virsh define /etc/libvirt/qemu/node6.xml
#使用虚拟描述文档建立虚拟机,可用virsh edit node6修改node6的配置文件

[root@target ~]# virsh start node6
#启动虚拟机

5 为虚拟机开启vnc
[root@target ~]# virsh edit node4     #编辑node4的配置文件;不建议直接通过vim node4.xml修改。
<graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1' keymap='en-us'/>
   #port='-1' autoport='yes':port自动分配,监听回环网络(virt-manager管理需要listen='127.0.0.1'),无密码
改为
<graphics type='vnc' port='5904' autoport='no' listen='0.0.0.0' keymap='en-us' passwd='xiaobai'/>
   #固定vnc管理端口5904,不自动分配,vnc密码xiaobai,监听所有网络

2. 远程vnc访问地址:192.168.32.40:5904

猜你喜欢

转载自powertech.iteye.com/blog/2293180