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

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

## 目的

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

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

```shell-session: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[@]}
```

- すべて

```shell-session:command
for i in ${!vmlist[*]} ; do echo ${vmlist[$i]} `virsh dumpxml ${vmlist[$i]} | grep vnc | grep -Eo [0-9]{4}` ; done | sort -k2 -n
```

- 起動中のもの

```shell-session:command
for i in ${!vmlist_started[*]} ; do echo `virsh dumpxml ${vmlist_started[$i]} | grep vnc | grep -Eo [0-9]{4}` ${vmlist_started[$i]} ; done
```
```shell-session:output_sample
5900 soracom-api_default
5926 esxi6.5-1
```

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

- ARP に乗ってる場合

```shell-session: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
```
```shell-session: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

```shell-session:command
$ virsh pool-list --all --details
```
```shell-session:output_sample
 Name     State    Autostart  Persistent   Capacity  Allocation   Available
----------------------------------------------------------------------------
 default  running  no         yes         13.54 TiB   12.75 TiB  801.19 GiB
```
```shell-session:command
$ virsh pool-info default
```
```shell-session: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
```
```shell-session:command
$ virsh pool-dumpxml default
```
```shell-session: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

```shell-session:command
$ virsh cpu-models x86_64
```
```shell-session: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

```shell-session:command
$ virsh net-list --all
```
```shell-session:output_sample
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes
 devstack0            inactive   no            yes
 devstack_vm10        inactive   no            yes
 vagrant-libvirt      active     no            yes
```
```shell-session:command
$ virsh net-info default
```
```shell-session:output_sample
Name:           default
UUID:           3e9704b9-1392-40d5-a8fb-f6d59361a88a
Active:         yes
Persistent:     yes
Autostart:      yes
Bridge:         virbr0
```
```shell-session:command
$ virsh net-dumpxml default
```
```shell-session: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>
```
```shell-session:command
$ brctl show
```

### Vagrant 関連

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

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