■debian stretchのKVMで仮想マシンのCPU、メモリ、NICの増設とディスクの拡張をしてみた。
$ lsb_release -d
Description: Debian GNU/Linux 9.4 (stretch)
$ /usr/sbin/libvirtd -V
/usr/sbin/libvirtd (libvirt) 3.0.0
$ qemu-img -V | head -1
qemu-img version 2.8.1(Debian 1:2.8+dfsg-6+deb9u3)
$ /usr/bin/qemu-io -V
qemu-io version 2.8.1
$ qemu-nbd -V | head -1
qemu-nbd version 0.0.1
$ virsh list
Id Name State
----------------------------------------------------
3 kvm-stretch running
■稼働中の仮想マシンでシリアルコンソールを有効にする。
$ sudo systemctl enable serial-getty@ttyS0.service
Created symlink /etc/systemd/system/getty.target.wants/serial-getty@ttyS0.service → /lib/systemd/system/serial-getty@.service.
$ sudo systemctl start serial-getty@ttyS0.service
$ virsh console kvm-stretch
Connected to domain kvm-stretch
Escape character is ^]
Debian GNU/Linux 9 kvm-stretch ttyS0
kvm-stretch login: labunix
パスワード:
■CPU数の上限と割り当てCPU数の変更
CPU数の上限を先に設定する必要がある。
後で稼働中の仮想マシンに対してコア数変更を行うので、
一旦4コアでエラーが無いことを確認してから2コアにする。
$ virsh setvcpus kvm-stretch 2 --config
error: invalid argument: requested vcpus is greater than max allowable vcpus for the persistent domain: 2 > 1
$ virsh list --all
Id Name State
----------------------------------------------------
- kvm-stretch shut off
$ virsh dumpxml kvm-stretch | awk '/vcpu/'
<vcpu placement='static'>1</vcpu>
$ virsh dominfo kvm-stretch | awk '/CPU/'
CPU(s): 1
$ virsh setvcpus kvm-stretch 4 --config --maximum
$ virsh setvcpus kvm-stretch 4 --config
$ virsh setvcpus kvm-stretch 2 --config
$ virsh dumpxml kvm-stretch | awk '/vcpu/'
<vcpu placement='static' current='2'>4</vcpu>
$ virsh dominfo kvm-stretch | awk '/CPU/'
CPU(s): 2
■メモリの上限値と割り当て容量を変更
こちらも上限を先に設定する必要がある。
後で稼働中の仮想マシンに対してメモリ変更を行うので、
一旦4Gでエラーが無いことを確認してから3Gにする。
$ virsh dumpxml kvm-stretch | awk '/memory unit/'
<memory unit='KiB'>1048576</memory>
$ virsh dominfo kvm-stretch | awk '/mem/'
Max memory: 1048576 KiB
Used memory: 1048576 KiB
$ virsh setmem kvm-stretch 3G --config
error: invalid argument: cannot set memory higher than max memory
$ virsh setmaxmem kvm-stretch 4G --config
$ virsh setmem kvm-stretch 4G --config
$ virsh setmem kvm-stretch 3G --config
$ virsh dumpxml kvm-stretch | awk '/memory unit/'
<memory unit='KiB'>4194304</memory>
$ virsh dominfo kvm-stretch | awk '/mem/'
Max memory: 4194304 KiB
Used memory: 3145728 KiB
■バックアップ
バックアップの対象は停止状態の仮想マシン設定ファイルとそのディスクイメージ。
拡張前にxmlから対象を確認して、対象がrootユーザなのでsudoでカレントディレクトリに圧縮ファイルとして保管。
リストア時は同じ所有者と権限でディレクトリ、ファイル構成を同じようにコピーして、
「virsh define」でKVMに反映するだけ。
$ virsh list --all
Id Name State
----------------------------------------------------
- kvm-stretch shut off
$ virsh dumpxml kvm-stretch > kvm-stretch.xml.bak.$(date '+%Y%m%d')
$ ls -l kvm-stretch.xml.bak.20180502
-rw-r--r-- 1 labunix labunix 4145 5月 2 19:24 kvm-stretch.xml.bak.20180502
$ virsh dumpxml kvm-stretch | \
awk 'BEGIN{f=0}{if($1=="<disk"||$1=="</disk>") \
{f+=1;print $0}else{if(f==1){print $0}}}'
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/kvm-stretch.img'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk>
<disk type='file' device='cdrom'>
</disk>
$ ls -lh /var/lib/libvirt/images/kvm-stretch.img
-rw------- 1 root root 21G 5月 2 18:31 /var/lib/libvirt/images/kvm-stretch.img
$ time sudo qemu-img convert -c -O qcow2 /var/lib/libvirt/images/kvm-stretch.img kvm-stretch.img.bak.$(date '+%Y%m%d')
real 5m36.724s
user 5m6.384s
sys 0m13.404s
$ ls -lh kvm-stretch.img.bak.20180502
-rw-r--r-- 1 root root 2.3G 5月 2 19:25 kvm-stretch.img.bak.20180502
■ディスクの拡張
事前に必ず、少なくとも仮想ディスクはバックアップしておきましょう。
$ sudo qemu-img info /var/lib/libvirt/images/kvm-stretch.img
image: /var/lib/libvirt/images/kvm-stretch.img
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 5.2G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: true
refcount bits: 16
corrupt: false
$ sudo qemu-img resize /var/lib/libvirt/images/kvm-stretch.img +40G
image resized.
$ sudo qemu-img info /var/lib/libvirt/images/kvm-stretch.img | awk '/size/'
virtual size: 60G (64424509440 bytes)
disk size: 5.2G
cluster_size: 65536
■仮想マシンの起動
$ virsh start kvm-stretch
Domain kvm-stretch started
$ virsh list
Id Name State
----------------------------------------------------
4 kvm-stretch running
■稼働中の仮想マシンからCPU、メモリ、ディスク情報を確認
2コア、3GBメモリ、60GBディスクになっている。
$ virsh console kvm-stretch
$ sudo dmidecode -t processor | awk '/Version|Flag|[Vv]irtual|Count/'
Version: pc-i440fx-2.8
Core Count: 1
Thread Count: 1
Version: pc-i440fx-2.8
Core Count: 1
Thread Count: 1
$ free -m
total used free shared buff/cache available
Mem: 2931 134 2614 9 183 2583
Swap: 1021 0 1021
$ sudo fdisk /dev/vda -l | head -1
Disk /dev/vda: 60 GiB, 64424509440 bytes, 125829120 sectors
■ディスクはパーティションを追加して「/home」としてマウントする方針とする。
$ sudo fdisk /dev/vda
Command (m for help): n
Partition type
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): p
Partition number (3,4, default 3):
First sector (39845888-125829119, default 39845888):
Last sector, +sectors or +size{K,M,G,T,P} (39845888-39847933, default 39847933):
Created a new partition 3 of type 'Linux' and of size 1023 KiB.
Command (m for help): n
Partition type
p primary (2 primary, 1 extended, 1 free)
l logical (numbered from 5)
Select (default p): p
Selected partition 4
First sector (41940992-125829119, default 41940992):
Last sector, +sectors or +size{K,M,G,T,P} (41940992-125829119, default 125829119):
Created a new partition 4 of type 'Linux' and of size 40 GiB.
Command (m for help): wq
$ sudo fdisk -l | grep vda[34]
/dev/vda3 39845888 39847933 2046 1023K 83 Linux
/dev/vda4 41940992 125829119 83888128 40G 83 Linux
$ sudo dmesg | grep vda
[ 1.292826] vda: vda1 vda2 < vda5 >
[ 1.736313] EXT4-fs (vda1): mounted filesystem with ordered data mode. Opts: (null)
[ 1.872710] EXT4-fs (vda1): re-mounted. Opts: errors=remount-ro
[ 2.137134] Adding 1046524k swap on /dev/vda5. Priority:-1 extents:1 across:1046524k FS
$ sudo shutdown -r now && exit
$ sudo dmesg | grep vda
[ 1.416977] vda: vda1 vda2 < vda5 > vda3 vda4
[ 1.774243] EXT4-fs (vda1): mounted filesystem with ordered data mode. Opts: (null)
[ 1.922947] EXT4-fs (vda1): re-mounted. Opts: errors=remount-ro
[ 2.233499] Adding 1046524k swap on /dev/vda5. Priority:-1 extents:1 across:1046524k FS
$ sudo mkdir /media/temp
$ sudo mkfs.ext4 /dev/vda4
mke2fs 1.43.4 (31-Jan-2017)
Creating filesystem with 10485760 4k blocks and 2626560 inodes
Filesystem UUID: 1787b13a-b262-4a25-90b1-06b4b64bf907
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624
Allocating group tables: done
Writing inode tables: done
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done
$ sudo mount /dev/vda4 /media/temp/
$ df -h | grep vda4
/dev/vda4 40G 49M 38G 1% /media/temp
$ sudo blkid | grep vda4
/dev/vda4: UUID="1787b13a-b262-4a25-90b1-06b4b64bf907" TYPE="ext4" PARTUUID="2999f25c-04"
$ sudo cp -a /home/* /media/temp/
$ sudo cp /etc/fstab{,.bak}
$ ls -l /etc/fstab{,.bak}
-rw-r--r-- 1 root root 664 5月 1 20:24 /etc/fstab
-rw-r--r-- 1 root root 664 5月 2 20:06 /etc/fstab.bak
$ echo -e 'UUID=1787b13a-b262-4a25-90b1-06b4b64bf907 /home\t\t ext4\t errors=remount-ro 0\t1' | sudo tee -a /etc/fstab
$ sudo umount /media/temp
$ sudo mount -a
$ df -h | grep home
/dev/vda4 40G 49M 38G 1% /home
$ sudo shutdown -r now && exit
■NICの増設
稼働中の仮想マシンに追加出来る。
追加する値はデフォルトの認識を参考にして設定。
vnet0とvnet1に分かれ、元のens3の他にens9が追加された。後は普通のLinux操作で変えられる。
$ virsh dumpxml kvm-stretch | \
awk 'BEGIN{f=0}{if($1=="<interface"||$1=="</interface>") \
{f+=1;print $0}else{if(f==1){gsub(":[0-9a-f][0-9a-f]",":XX",$0);print $0}}}'
<interface type='bridge'>
<mac address='52:XX:XX:XX:XX:XX'/>
<source bridge='br0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
$ virsh attach-interface --type bridge --source br0 --model virtio kvm-stretch
Interface attached successfully
$ virsh dumpxml kvm-stretch | awk 'BEGIN{f=0}{if($1=="<interface"||$1=="</interface>") \
{f+=1;print $0}else{if(f>0&&f<4){gsub(":[0-9a-f][0-9a-f]",":XX",$0);print $0}}}'
<interface type='bridge'>
<mac address='52:XX:XX:XX:XX:XX'/>
<source bridge='br0'/>
<target dev='vnet0'/>
<model type='virtio'/>
<alias name='net0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<interface type='bridge'>
<mac address='52:XX:XX:XX:XX:XX'/>
<source bridge='br0'/>
<target dev='vnet1'/>
<model type='virtio'/>
<alias name='net1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
</interface>
$ virsh console kvm-stretch
$ sudo dmesg | grep "virtio.*ens"
[ 1.320586] virtio_net virtio0 ens3: renamed from eth0
[ 43.586758] virtio_net virtio4 ens9: renamed from eth0
$ ip a show dev ens9
3: ens9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:4f:10:f9 brd ff:ff:ff:ff:ff:ff
■稼働中の仮想マシンにCPUを追加
設定上限値まで追加が可能。再起動後も有効にするには「--config」が必要。
「online」後もdmidecodeでは確認出来ないが、
「/proc/cpuinfo」ではきちんと認識されていることが確認出来る。
ホストと稼働中の仮想マシンの両方に設定することで動的にCPUコアを増やせる。
guest agent経由でもonlineに出来るらしいけど、そこまで本気じゃないので今回は行わない。
$ virsh vcpucount kvm-stretch
maximum config 4
maximum live 4
current config 2
current live 2
$ virsh setvcpus kvm-stretch 4
$ virsh dominfo kvm-stretch | awk '/CPU/'
CPU(s): 4
CPU time: 161.3s
$ virsh dumpxml kvm-stretch | awk '/vcpu/'
<vcpu placement='static'>4</vcpu>
<vcpus>
<vcpu id='0' enabled='yes' hotpluggable='no' order='1'/>
<vcpu id='1' enabled='yes' hotpluggable='no' order='2'/>
<vcpu id='2' enabled='yes' hotpluggable='yes' order='3'/>
<vcpu id='3' enabled='yes' hotpluggable='yes' order='4'/>
</vcpus>
$ virsh console kvm-stretch
$ su root -c 'echo 1 > /sys/devices/system/cpu/cpu2/online'
$ su root -c 'echo 1 > /sys/devices/system/cpu/cpu3/online'
$ grep processor /proc/cpuinfo
processor : 0
processor : 1
processor : 2
processor : 3
$ sudo apt-get install -y sysstat
$ mpstat -P ALL
Linux 4.9.0-6-amd64 (kvm-stretch) 2018年05月02日 _x86_64_ (4 CPU)
20時40分01秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
20時40分01秒 all 0.00 0.00 0.00 0.00 0.00 0.00 100.00 0.00 0.00 0.00
20時40分01秒 0 0.19 0.00 0.17 0.04 0.00 0.00 0.03 0.00 0.00 99.56
20時40分01秒 1 0.15 0.00 0.16 0.52 0.00 0.00 0.02 0.00 0.00 99.14
20時40分01秒 2 0.00 0.00 0.00 0.00 0.00 0.00 100.00 0.00 0.00 0.00
20時40分01秒 3 0.00 0.00 0.00 0.00 0.00 0.00 100.00 0.00 0.00 0.00
$ exit
$ virsh setvcpus kvm-stretch 2
$ virsh dominfo kvm-stretch | awk '/CPU/'
CPU(s): 2
CPU time: 860.3s
$ virsh dumpxml kvm-stretch | awk '/vcpu/'
<vcpu placement='static' current='2'>4</vcpu>
<vcpus>
<vcpu id='0' enabled='yes' hotpluggable='no' order='1'/>
<vcpu id='1' enabled='yes' hotpluggable='no' order='2'/>
<vcpu id='2' enabled='no' hotpluggable='yes'/>
<vcpu id='3' enabled='no' hotpluggable='yes'/>
</vcpus>
$ virsh console kvm-stretch
$ mpstat -P ALL
Linux 4.9.0-6-amd64 (kvm-stretch) 2018年05月02日 _x86_64_ (2 CPU)
20時40分17秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
20時40分17秒 all 0.00 0.00 0.00 0.00 0.00 0.00 100.00 0.00 0.00 0.00
20時40分17秒 0 0.19 0.00 0.17 0.03 0.00 0.00 0.03 0.00 0.00 99.57
20時40分17秒 1 0.15 0.00 0.16 0.52 0.00 0.00 0.02 0.00 0.00 99.14
■稼働中の仮想マシンにメモリを追加
actual(実際の)の値が現在のメモリ割り当て。
$ virsh setmem kvm-stretch 4G
$ virsh dumpxml kvm-stretch | awk '/mem/'
<memory unit='KiB'>4194304</memory>
<suspend-to-mem enabled='no'/>
<model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
<memballoon model='virtio'>
</memballoon>
$ virsh dominfo kvm-stretch | awk '/mem/'
Max memory: 4194304 KiB
Used memory: 4194304 KiB
$ virsh dommemstat kvm-stretch | awk '/actual/'
actual 4194304
$ virsh console kvm-stretch
$ free -m
total used free shared buff/cache available
Mem: 3955 138 3520 9 296 3595
Swap: 1021 0 1021
$ exit
$ virsh dumpxml kvm-stretch | awk '/mem/'
<memory unit='KiB'>4194304</memory>
<suspend-to-mem enabled='no'/>
<model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
<memballoon model='virtio'>
</memballoon>
$ virsh dominfo kvm-stretch | awk '/mem/'
Max memory: 4194304 KiB
Used memory: 3145728 KiB
$ virsh dommemstat kvm-stretch | awk '/actual/'
actual 3145728