LoginSignup
1
1

More than 5 years have passed since last update.

kvm 日常的ワンライナーコマンドメモ

Last updated at Posted at 2017-06-22

kvm 日常的ワンライナーコマンドメモ

目的

  • ご家庭にある KVM ホストを簡易に管理するときのコマンドコピペ用途

アサインしているVNCポート番号をリストする

command
vmlist=($(virsh -c qemu:///system list --all | awk '{print $2}'| grep -v Name))
vmlist_started=($(virsh -c qemu:///system list | awk '{print $2}'| grep -v Name))
echo ${vmlist[*]}
echo ${#vmlist[@]}
echo ${vmlist_started[*]}
echo ${#vmlist_started[@]}
  • すべて
command
for i in ${!vmlist[*]} ; do echo ${vmlist[$i]} `virsh dumpxml ${vmlist[$i]} | grep vnc | grep -Eo [0-9]{4}` ; done | sort -k2 -n
  • 起動中のもの
command
for i in ${!vmlist_started[*]} ; do echo `virsh dumpxml ${vmlist_started[$i]} | grep vnc | grep -Eo [0-9]{4}` ${vmlist_started[$i]} ; done
output_sample
5900 soracom-api_default
5926 esxi6.5-1

DHCP で VM に振られた IP アドレスを確認する

  • ARP に乗ってる場合
command
for i in ${!vmlist_started[*]} ; do echo -n ${vmlist_started[$i]}" " ; arp -a | grep `virsh dumpxml ${vmlist_started[$i]} | grep "mac address" | grep -Eo '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}'` ; done
output_sample
soracom-api_default ? (192.168.121.114) at 52:54:00:fb:12:6f [ether] on virbr1
esxi6.5-1 ? (192.168.100.192) at 52:54:00:0d:46:b7 [ether] on br0

Storage 関連のコマンド

ref. https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Virtualization_Deployment_and_Administration_Guide/sect-Managing_guest_virtual_machines_with_virsh-Storage_pool_commands.html

command
$ virsh pool-list --all --details
output_sample
 Name     State    Autostart  Persistent   Capacity  Allocation   Available
----------------------------------------------------------------------------
 default  running  no         yes         13.54 TiB   12.75 TiB  801.19 GiB
command
$ virsh pool-info default
output_sample
Name:           default
UUID:           2c407142-ca54-49a9-87a8-650a7dfc3077
State:          running
Persistent:     yes
Autostart:      no
Capacity:       13.54 TiB
Allocation:     12.75 TiB
Available:      801.19 GiB
command
$ virsh pool-dumpxml default
output_sample
<pool type='dir'>
  <name>default</name>
  <uuid>2c407142-ca54-49a9-87a8-650a7dfc3077</uuid>
  <capacity unit='bytes'>14883942006784</capacity>
  <allocation unit='bytes'>14023674671104</allocation>
  <available unit='bytes'>860267335680</available>
  <source>
  </source>
  <target>
    <path>/mnt/export/var/lib/libvirt/images</path>
    <permissions>
      <mode>0755</mode>
      <owner>-1</owner>
      <group>-1</group>
    </permissions>
  </target>
</pool>

VM 関連

ref. https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Virtualization_Deployment_and_Administration_Guide/sect-KVM_guest_virtual_machine_compatibility-Supported_CPU_Models.html#sect-Supported_CPU_Models-Listing_the_guest_CPU_models

command
$ virsh cpu-models x86_64
command
$ cat /usr/share/libvirt/cpu_map.xml

NW 関連

ref. https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Virtualization_Deployment_and_Administration_Guide/chap-Network_configuration.html

command
$ virsh net-list --all
output_sample
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes
 devstack0            inactive   no            yes
 devstack_vm10        inactive   no            yes
 vagrant-libvirt      active     no            yes
command
$ virsh net-info default
output_sample
Name:           default
UUID:           3e9704b9-1392-40d5-a8fb-f6d59361a88a
Active:         yes
Persistent:     yes
Autostart:      yes
Bridge:         virbr0
command
$ virsh net-dumpxml default
output_sample
<network>
  <name>default</name>
  <uuid>3e9704b9-1392-40d5-a8fb-f6d59361a88a</uuid>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr0' stp='on' delay='0'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
    </dhcp>
  </ip>
</network>
command
$ brctl show

Vagrant 関連

ref. https://github.com/vagrant-libvirt/vagrant-libvirt

$ vagrant box add centos/7 --provider libvirt
$ vagrant box add opensuse/openSUSE-42.2-x86_64 --provider libvirt
$ vagrant box update --box centos/7 --provider=libvirt
$ vagrant box update --box opensuse/openSUSE-42.2-x86_64 --provider=libvirt
$ vagrant box list
centos/7 (libvirt, 1602.02)
centos/7 (libvirt, 1603.01)
centos/7 (libvirt, 1609.01)
centos/7 (libvirt, 1705.02)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1