kvm虛擬化用到的命令

http://www.it165.net/os/html/201309/6183.html

1、virt-install 啓動虛機

virt-install \

-n www \

-r 2048 \

-f /var/kvm/images/www.img \

-s 20 \

--vcpus=2 \

--os-type linux \

--os-variant=debianwheezy \

--network bridge=br0 \

--nographics \

--location='http://mirrors.163.com/debian/dists/wheezy/main/installer-amd64/' \

--extra-args='console=tty0 console=ttyS0,115200n8 serial'

其中location如果是網絡的url可以網絡啓動虛機,但是選擇包的時候應該儘量的少,保持一個基本的和SSH服務即可,不然下載很浪費資源。

2、克隆虛機

 virt-clone -o debian001 -n debian002 -f /vHost/vm/debian002.img

3、導出虛機配置dumpxml

virsh dumpxml wintest01 > /etc/libvirt/qemu/wintest02.xml

KVM虛擬機的管理主要是通過virsh命令對虛擬機進行管理。

1. 查看KVM虛擬機配置文件及運行狀態

(1) KVM虛擬機默認配置文件位置: /etc/libvirt/qemu/

autostart目錄是配置kvm虛擬機開機自啓動目錄。

image

(2) virsh命令幫助

# virsh -help

或直接virsh命令和,再執行子命令。如下所示。

[root@node1 ~]# virsh
歡迎使用 virsh,虛擬化的交互式終端。

輸入:'help' 來獲得命令的幫助信息
'quit' 退出

virsh # help
……

(3) 查看kvm虛擬機狀態

# virsh list --all

2. KVM虛擬機開機

# virsh start oeltest01

3. KVM虛擬機關機或斷電

(1) 關機

默認情況下virsh工具不能對linux虛擬機進行關機操作,linux操作系統需要開啓與啓動acpid服務。在安裝KVM linux虛擬機必須配置此服務。

# chkconfig acpid on
# service acpid restart

virsh關機

# virsh shutdown oeltest01

(2) 強制關閉電源

# virsh destroy wintest01

4. 通過配置文件啓動虛擬機

# virsh create /etc/libvirt/qemu/wintest01.xml

image

5. 配置開機自啓動虛擬機

# virsh autostart oeltest01

autostart目錄是kvm虛擬機開機自啓動目錄,可以看到該目錄中有KVM配置文件鏈接。

6. 導出KVM虛擬機配置文件

# virsh dumpxml wintest01 > /etc/libvirt/qemu/wintest02.xml

image

KVM虛擬機配置文件可以通過這種方式進行備份。

7. 添加與刪除KVM虛擬機

(1) 刪除kvm虛擬機

# virsh undefine wintest01

說明:該命令只是刪除wintest01的配置文件,並不刪除虛擬磁盤文件。如下圖所示。

image

(2) 重新定義虛擬機配置文件

通過導出備份的配置文件恢復原KVM虛擬機的定義,並重新定義虛擬機。

# mv /etc/libvirt/qemu/wintest02.xml /etc/libvirt/qemu/wintest01.xml

# virsh define /etc/libvirt/qemu/wintest01.xml

20130908073409603.jpg

8. 編輯KVM虛擬機配置文件

# virsh edit wintest01

virsh edit將調用vi命令編輯/etc/libvirt/qemu/wintest01.xml配置文件。也可以直接通過vi命令進行編輯,修改,保存。

可以但不建議直接通過vi編輯。

image

[root@node1 qemu]# vi /etc/libvirt/qemu/wintest01.xml

view sourceprint?

01.<!--

02.WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE

03.OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:

04.virsh edit wintest01

05.or other application using the libvirt API.

06.-->

07.<domain type='kvm'>

08.<name>wintest01</name>

09.<uuid>fe31ea48-7d6a-f3cb-cede-2f9bd9dec2bd</uuid>

10.<memory unit='KiB'>524288</memory>

11.<currentMemory unit='KiB'>524288</currentMemory>

12.<vcpu placement='static'>2</vcpu>

13.<os>

14.<type arch='x86_64' machine='rhel6.4.0'>hvm</type>

15.<boot dev='hd'/>

16.</os>

17.<features>

18.<acpi/>

19.<apic/>

20.<pae/>

21.</features>

22.<clock offset='utc'/>

23.<on_poweroff>destroy</on_poweroff>

24.<on_reboot>restart</on_reboot>

25.<on_crash>restart</on_crash>

26.<devices>

27.<emulator>/usr/libexec/qemu-kvm</emulator>

28.<disk type='file' device='disk'>

29.<driver name='qemu' type='raw' cache='none'/>

30.<source file='/data/wintest01.img'/>

31.<target dev='hda' bus='ide'/>

32.<address type='drive' controller='0' bus='0' target='0' unit='0'/>

33.</disk>

34.<disk type='block' device='cdrom'>

35.<driver name='qemu' type='raw'/>

36.<target dev='hdc' bus='ide'/>

37.<readonly/>

38.<address type='drive' controller='0' bus='1' target='0' unit='0'/>

39.</disk>

40.<controller type='usb' index='0'>

41.<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>

42.</controller>

43.<controller type='ide' index='0'>

44.<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>

45.</controller>

46.<interface type='bridge'>

47.<mac address='52:54:00:2b:2f:fe'/>

48.<source bridge='br0'/>

49.<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>

50.</interface>

51.<serial type='pty'>

52.<target port='0'/>

53.</serial>

54.<console type='pty'>

55.<target type='serial' port='0'/>

56.</console>

57.<input type='mouse' bus='ps2'/>

58.<graphics type='vnc' port='5911' autoport='no' listen='0.0.0.0'>

59.<listen type='address' address='0.0.0.0'/>

60.</graphics>

61.<video>

62.<model type='cirrus' vram='9216' heads='1'/>

63.<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>

64.</video>

65.<memballoon model='virtio'>

66.<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>

67.</memballoon>

68.</devices>

69.<seclabel type='none'/>

70.</domain>

9. virsh console 控制檯管理linux虛擬機

配置virsh console見下文

kvm虛擬化學習筆記(六)之kvm虛擬機控制檯登錄配置

# virsh console oeltest01

10. 其它virsh命令

(1) 掛起服務器

# virsh suspend oeltest01

image

(2) 恢復服務器

# virsh resume oeltest01

image

virsh命令豐富。可以執行各種維護任務,本文只是從維護與管理的角度例舉了常用的命令,爲該命令的使用提供一個思路。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章