天天看點

通過virt工具安裝管理KVM虛拟機

1.virt-install 指令安裝虛拟機

[root@hyperStor images]# virt-install --name rhcexam --ram 1024  --vcpus 1 --description=rhce --cdrom /var/lib/libvirt/isos/rhel-server-6.2-32.iso --os-type=linux --os-variant=rhel6 --disk path=/var/lib/libvirt/images/rhcexam.img,size=20,format=qcow2 --network bridge=br0 --graphics vnc,listen=192.168.3.30,password=123456,port=5910

指令參數:

--name             虛拟機的名稱

--ram               記憶體大小

--vcpus             CPU數目

--description      虛拟機描述

--cdrom            指定cdrom的鏡像檔案或者裝置名

--os-type           作業系統類型

--os-variant     作業系統版本

--disk               磁盤屬性:path路徑,size 大小,format 存儲類型

--network         指定網絡裝置,bridge為橋接裝置

--graphic          指定圖形化類别,此處為vnc,後面接屬性:listen 監聽IP,passsword 連接配接密碼,port 監聽端口

[root@hyperStor images]# virt-install  --name rhcsa --ram 1024 --vcpus 1  --import --disk path=rhcsa.img --network bridge=br0 --graphics vnc,listen=192.168.3.30,password=123,port=5901 //導入已經存在的虛拟機檔案

2.virt-viewer  用于連接配接正在運作的虛拟機

[root@hyperStor ~]# virsh  list --all

Id Name                 State

----------------------------------

 5 node1                running

 6 node2                running

12 rhcexam            running

[root@hyperStor ~]# virt-viewer  12  //連接配接ID為12的rhcexam虛拟機,virt-viewer後接rhcexam也可以

3.virsh 指令管理虛拟機

[root@hyperStor ~]#  virsh list  //列出所有運作的虛拟機

[root@hyperStor ~]#  virsh list --all //列出所有虛拟機,包括未運作的

[root@hyperStor ~]#  virsh  shutdown  id|name    //正常關閉虛拟機

[root@hyperStor ~]#  virsh  destroy id|name     //強制關閉虛拟機

[root@hyperStor ~]#  virsh  undefine id|name   //從虛拟機清單中删除虛拟機【不删除檔案】

[root@hyperStor ~]#   virsh start id|name  //啟動虛拟機

[root@hyperStor ~]#  virsh  suspend  id|name //挂起虛拟機

[root@hyperStor ~]#  virsh  resume    id|name  //恢複虛拟機

[root@hyperStor ~]#  virsh  reboot id|name     //重新開機虛拟機

[root@hyperStor images]# virsh  autostart 16  //将id為16的虛拟機設定為自動啟動【随着實體機啟動】

Domain 16 marked as autostarted

[root@hyperStor ~]# virsh   vol-clone  /var/lib/libvirt/images/rhcexam.img  rhcexam-clone.img  //克隆鏡像檔案

Vol rhcexam-clone.img cloned from rhcexam.img

紅色字型路徑必須指定,後面的名稱不用指定路徑,預設跟原始鏡像一個路徑下

4. qemu-img 工具

給虛拟機做快照

[root@hyperStor ~]# qemu-img  snapshot -c rhce-sp1 /var/lib/libvirt/images/rhcexam.img //建立鏡像

[root@hyperStor ~]# qemu-img  snapshot -l /var/lib/libvirt/images/rhcexam.img   //列出鏡像快照

Snapshot list:

ID        TAG                 VM SIZE                DATE       VM CLOCK

1         rhce-sp1                  0 2013-03-22 10:26:06   00:00:00.000

[root@hyperStor ~]# qemu-img  snapshot -a rhce-sp1 /var/lib/libvirt/images/rhcexam.img //恢複快照

[root@hyperStor ~]# qemu-img  snapshot -d rhce-sp1 /var/lib/libvirt/images/rhcexam.img //删除快照

本文轉自 暗黑魔君 51CTO部落格,原文連結:http://blog.51cto.com/clovemfong/1197577,如需轉載請自行聯系原作者

繼續閱讀