YggdrasillNetwork Blog

A blogging framework for hackers.

KVM Tips

概要

KVM 操作の際の Tips

virsh コマンド

ドメイン一覧表示 ( 停止中を含む )

1
# virsh list --all

ドメインの起動

1
# virsh start centos66

ドメインの強制停止

1
# virsh destroy pc2

ドメインの削除

1
# virsh undefine centos66

※ 停止中のドメインのみ実行可能

ゲストのコンソールに接続

1
# virsh console 5

ゲストのコンソールから抜ける

1
^] 

VM の設定ファイルを編集

1
# virsh edit <vm-name>

実行されている VM の設定確認

1
# virsh dumpxml <vm-name>

ホストに接続されているソケット数、コア数およびハイパースレッド数に関する情報を確認

1
2
3
4
5
6
7
8
9
# virsh nodeinfo
CPU モデル:       x86_64
CPU:                 8
CPU 周波数:       1795 MHz
CPU ソケット数: 1
ソケットあたりのコア数: 4
コアあたりのスレッド数: 1
NUMA セル数:      2
メモリー容量:  65801024 KiB

capabilities

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
[root@kvm1 images]# virsh capabilities
<capabilities>

  <host>
    <uuid>37343730-3838-434e-3734-353030384344</uuid>
    <cpu>
      <arch>x86_64</arch>
      <model>SandyBridge</model>
      <vendor>Intel</vendor>
      <topology sockets='1' cores='4' threads='1'/>
      <feature name='invtsc'/>
      <feature name='erms'/>
      <feature name='smep'/>
      <feature name='fsgsbase'/>
      <feature name='pdpe1gb'/>
      <feature name='rdrand'/>
      <feature name='f16c'/>
      <feature name='osxsave'/>
      <feature name='dca'/>
      <feature name='pcid'/>
      <feature name='pdcm'/>
      <feature name='xtpr'/>
      <feature name='tm2'/>
      <feature name='est'/>
      <feature name='smx'/>
      <feature name='vmx'/>
      <feature name='ds_cpl'/>
      <feature name='monitor'/>
      <feature name='dtes64'/>
      <feature name='pbe'/>
      <feature name='tm'/>
      <feature name='ht'/>
      <feature name='ss'/>
      <feature name='acpi'/>
      <feature name='ds'/>
      <feature name='vme'/>
    </cpu>
    <power_management>
      <suspend_disk/>
    </power_management>
    <migration_features>
      <live/>
      <uri_transports>
        <uri_transport>tcp</uri_transport>
      </uri_transports>
    </migration_features>
    <topology>
      <cells num='2'>
        <cell id='0'>
          <cpus num='4'>
            <cpu id='0' socket_id='0' core_id='0' siblings='0'/>
            <cpu id='1' socket_id='0' core_id='1' siblings='1'/>
            <cpu id='2' socket_id='0' core_id='2' siblings='2'/>
            <cpu id='3' socket_id='0' core_id='3' siblings='3'/>
          </cpus>
        </cell>
        <cell id='1'>
          <cpus num='4'>
            <cpu id='4' socket_id='1' core_id='0' siblings='4'/>
            <cpu id='5' socket_id='1' core_id='1' siblings='5'/>
            <cpu id='6' socket_id='1' core_id='2' siblings='6'/>
            <cpu id='7' socket_id='1' core_id='3' siblings='7'/>
          </cpus>
        </cell>
      </cells>
    </topology>
    <secmodel>
      <model>selinux</model>
      <doi>0</doi>
    </secmodel>
    <secmodel>
      <model>dac</model>
      <doi>0</doi>
    </secmodel>
  </host>

  <guest>
    <os_type>hvm</os_type>
    <arch name='i686'>
      <wordsize>32</wordsize>
      <emulator>/usr/libexec/qemu-kvm</emulator>
      <machine>rhel6.6.0</machine>
      <machine canonical='rhel6.6.0'>pc</machine>
      <machine>rhel6.5.0</machine>
      <machine>rhel6.4.0</machine>
      <machine>rhel6.3.0</machine>
      <machine>rhel6.2.0</machine>
      <machine>rhel6.1.0</machine>
      <machine>rhel6.0.0</machine>
      <machine>rhel5.5.0</machine>
      <machine>rhel5.4.4</machine>
      <machine>rhel5.4.0</machine>
      <domain type='qemu'>
      </domain>
      <domain type='kvm'>
        <emulator>/usr/libexec/qemu-kvm</emulator>
      </domain>
    </arch>
    <features>
      <cpuselection/>
      <deviceboot/>
      <acpi default='on' toggle='yes'/>
      <apic default='on' toggle='no'/>
      <pae/>
      <nonpae/>
    </features>
  </guest>

  <guest>
    <os_type>hvm</os_type>
    <arch name='x86_64'>
      <wordsize>64</wordsize>
      <emulator>/usr/libexec/qemu-kvm</emulator>
      <machine>rhel6.6.0</machine>
      <machine canonical='rhel6.6.0'>pc</machine>
      <machine>rhel6.5.0</machine>
      <machine>rhel6.4.0</machine>
      <machine>rhel6.3.0</machine>
      <machine>rhel6.2.0</machine>
      <machine>rhel6.1.0</machine>
      <machine>rhel6.0.0</machine>
      <machine>rhel5.5.0</machine>
      <machine>rhel5.4.4</machine>
      <machine>rhel5.4.0</machine>
      <domain type='qemu'>
      </domain>
      <domain type='kvm'>
        <emulator>/usr/libexec/qemu-kvm</emulator>
      </domain>
    </arch>
    <features>
      <cpuselection/>
      <deviceboot/>
      <acpi default='on' toggle='yes'/>
      <apic default='on' toggle='no'/>
    </features>
  </guest>

</capabilities>

VM 名のリネーム

1
# virsh edit nat01

下記の行を編集

1
<name>nat01</name>

下記の行を削除

1
<uuid>1f85892f-195a-d19b-3a7a-99457b558ab8</uuid>

NIC の追加

Linux Bridge 環境

VM が起動している状態で実行可能

1
# virsh attach-interface --type bridge --source br0 --model e1000 nat

openvswitch 環境

virsh attach-interface コマンドではエラーになるため、VM が停止した状態で設定ファイルに下記のような記述を追加

1
2
3
4
<interface type='bridge'>
  <source bridge='br3'/>
  <virtualport type='openvswitch'/>
</interface>

virsh edit コマンド実行後、自動的に下記のような設定になっている

1
2
3
4
5
6
7
8
<interface type='bridge'>
  <mac address='52:54:00:90:9a:9e'/>
  <source bridge='br3'/>
  <virtualport type='openvswitch'>
    <parameters interfaceid='a52f60f2-2ef5-3eb5-9a89-cb0f13e3aa40'/>
  </virtualport>
  <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</interface>

mac address, parameters, address は自動的に設定してくれる

以下は virsh attach-interface コマンド実行時のエラーメッセージ

1
2
3
# virsh attach-interface --type bridge --source br1 --model e1000 fumi-1
エラー: インターフェースを接続できませんでした
エラー: Unable to add bridge br1 port vnet2: Operation not supported

VM に Disk の追加

VM が Down している状態で実行する

1. Disk Image の作成

1
2
# cd /var/lib/libvirt/images
# qemu-img create -f qcow2 disk_Maria-1.img 40G

2. 設定の追加

virsh edit コマンドを利用して設定を追記

1
# virsh edit Maria-1
  • 追加分 Config
1
2
3
4
5
<disk type='file' device='disk'>
  <driver name='qemu' type='qcow2' cache='none'/>
  <source file='/var/lib/libvirt/images/disk_Maria-1.img'/>
  <target dev='vdb' bus='virtio'/>
</disk>

VM のクローン

1. クローンコマンドの実行

VM が Down している状態で実行

1
# virt-clone --original cent_base --name Maria-1 --file /var/lib/libvirt/images/Maria-1.img

2. MAC Address のメモ

1
2
3
# virsh dumpxml Maria-1 | grep mac
    <type arch='x86_64' machine='rhel6.6.0'>hvm</type>
      <mac address='52:54:00:64:df:4f'/>

3. vnc port の編集

既存の VM とかぶらない VNC の Port 番号に変更

1
# virsh edit Maria-1
1
2
3
<graphics type='vnc' port='20001' autoport='no' listen='0.0.0.0'>
  <listen type='address' address='0.0.0.0'/>
</graphics>

4. VM の起動

1
virsh start Maria-1

5. 70-persistent-net.rules の編集

ここから VNC 経由で接続して作業

1
/etc/udev/rules.d/70-persistent-net.rules

6. ifcfg-eth0 の編集

1
2
3
4
5
6
7
UUID
HWADDR
BOOTPROTO=dchp

BOOTPROTO=none
IPADDR=211.14.1.11
NETMASK=255.255.255.192

7. VM の再起動

1
# shutdown -r now

8. Interface のアドレス設定

手動で IP アドレスを付与

1
# ifconfig eth0 inet 172.25.255.131/24

9. resolv.conf の編集

VNC 設定

1
<graphics type='vnc' port='21144' autoport='no' listen='0.0.0.0' keymap='ja' passwd='Password'>

参照 URL