ในบทความก่อนหน้าได้อภิปรายการติดตั้ง KVM Hypervisor และสร้าง VM ในบทความเดียวไม่ครอบคลุมการจัดการ KVM VM รายละเอียด วันนี้จะแสดงวิธีจัดการ VMs จาก Command Line ด้วย virsh: วิธีเปลี่ยน VM Settings, เพิ่ม Virtual Devices เพิ่มเติม และคำสั่งสำหรับจัดการ KVM VMs บน Linux
การใช้คำสั่ง Virsh (Virtual Shell) จัดการ KVM Virtual Machines
มาดูวิธีดู Registered KVM VMs, เริ่ม, หยุด หรือลบพวกเขา Virsh (based on libvirt API) ใช้จัดการ KVM VMs ใน Linux Console ได้
# virsh list – แสดงรายชื่อ Running VMs
# virsh list --all – แสดงรายชื่อ Registered VMs ทั้งหมด (รวม Shutdown VMs)

เห็นใน Screenshot กรณีแรก Shutdown VM ไม่แสดง
# virsh shutdown <vm name> — Shutdown VM
# virsh start <vm name> — Start VM
# virsh suspend <vm name> — Suspend VM
# virsh resume <vm name> — Resume Suspended VM
# virsh reboot <vm name> — Restart VM
# virsh destroy <vm name> — Initiates Immediate Ungraceful Shutdown และหยุด VM (ใช้เมื่อ Guest VM ไม่ตอบสนอง)
# virsh undefine <vm name> — Unregister VM และลบ Storage Volumes
# virsh vcpuinfo <vm name> — แสดงข้อมูล vCPU ของ VM

คำสั่งอื่นๆ สำหรับดูข้อมูล VM:
# virsh domid <vm name> — Get VM ID
# virsh domuuid <vm name> — Show VM UUID
# virsh dominfo <vm name> — Display VM Information
# virsh domstate <vm name> — View VM State

# virsh dumpxml <vm name> — List VM Config File ใน XML Format
วิธีเพิ่ม Memory และ vCPU ให้ KVM Virtual Machine
ใน KVM Console สามารถเพิ่มหรือลด CPU/Memory Resources ที่มอบหมายให้ VM ได้ 2 วิธี:
- ใช้ virsh
- ผ่าน VM Config XML File
หาก VM กำลังรัน ให้ Shut Down ก่อน:
# virsh shutdown test-centos
Domain test-centos is being shutdown
ใช้ virsh เปลี่ยนจำนวน vCPUs เป็น 6:
# virsh setvcpus <vm name> <vcpu_count> --config
เช่น:
# virsh setvcpus test-centos 6 --config
หลังรันคำสั่ง เกิด Error:
error: invalid argument: requested vcpus is greater than max allowable vcpus for the persistent domain: 6 > 4
ไม่สามารถตั้ง Cores มากกว่า Max ได้ เพื่อเพิ่ม Max Allowed VM vCPUs รัน:
# virsh setvcpus test-centos 6 --config --maximum
จากนั้นรันคำสั่งก่อนหน้าและ Start VM:

ตรวจสอบจำนวน vCPUs ใน VM Settings:
# virsh dumpxml test-centos
<domain type='kvm'> <name>test-centos</name> <uuid>3b223aa2-b322-1ff1-ab23-c1223abc1f01</uuid> <memory unit='KiB'>2097152</memory> <currentMemory unit='KiB'>2097152</currentMemory> <vcpu placement='static'>6</vcpu>
เพิ่ม RAM ให้ VM แบบเดียวกัน:
# virsh setmem <vm_name> <memsize> --config
เช่น:
# virsh setmem test-centos 4G --config
Error เหมือนเดิม:
error: invalid argument: cannot set memory higher than max memory.
เพิ่ม Max Memory:
# virsh setmaxmem test-centos 6G --config
จากนั้นเพิ่ม Memory ให้ VM ได้
ก่อนเปลี่ยนแปลง ให้ Shutdown VM แล้ว Start ใหม่เสมอ
ยังสามารถเปลี่ยน Resources ของ KVM VM ใน XML Config File ได้ Edit Online หรือ Backup XML File, Modify และ Apply ให้ VM
Edit VM XML File Online:
# virsh edit <vm_name>
ใน Editor vi ที่ปรากฏ ทำการเปลี่ยนแปลงโดยกด Insert
เปลี่ยน Block ต่อไปนี้:
<domain type='kvm'> <name>test-centos</name> <uuid>3b223aa2-b322-1ff1-ab23-c1223abc1f01</uuid> <memory unit='KiB'>6291456</memory> <currentMemory unit='KiB'>4194304</currentMemory> <vcpu placement='static'>6</vcpu> <os>
เช่น จัดสรร 2 Processor Cores และ 1 GB Memory ให้ VM:

หมายเหตุ: Memory Size แสดงเป็น Kilobytes
Save Changes และ Reboot VM:
# virsh reboot <vm_name>
ทำได้เหมือนกันถ้า Backup XML File:
# virsh dumpxml <vm_name> > /root/test.xml
# vi /root/test.xml
เปลี่ยน Settings ที่ต้องการ Save File และ Apply ให้ VM:
# virsh shutdown test-centos
# virsh define /root/test.xml
# virsh start test-centos
บางครั้งเวลาเปลี่ยน VM Config File Online Resources ที่มอบหมายจะ Reset หลัง Reboot ในกรณีนี้ Stop VM แล้ว Start ใหม่
เพิ่ม Disk ให้ KVM VM ที่มีอยู่แล้ว
ในบทความก่อนเคยอธิบาย Extend/Reduce Disk Size ของ KVM VM แต่ไม่ได้บอกวิธีเพิ่ม Disk เพิ่มเติม
ขั้นแรก สร้าง Disk File ใหม่สำหรับ VM:
# qemu-img create -f qcow2 -o size=20G /vz/disk/test.img
แทน qcow2 ใช้ Disk Format ที่ต้องการได้ ระบุ File Path Disk Storage ของผมอยู่ที่ /vz/disk/
จากนั้นเพิ่ม Virtual Disk Device ให้ VM:
# virsh attach-disk <vm_name> /vz/disk/test.img vdb --type disk --persistent
Shutdown และ Reboot VM แล้วดูผล:
# virsh shutdown test-centos
# virsh start test-centos
# virsh dumpxml test-centos
<domain type='kvm' id='14'>
<name>test-centos</name>
<uuid>3b223aa2-b322-1ff1-ab23-c1223abc1f01</uuid>
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
<vcpu placement='static'>6</vcpu>
<resource>
<partition>/machine</partition>
</resource>
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='cdrom'/>
<boot dev='hd'/>
<bootmenu enable='yes'/>
</os>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/vz/disk/test.img'/>
<backingStore/>
<target dev='vdb' bus='virtio'/>
<alias name='virtio-disk1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
</disk>
เห็นว่า Disk ถูกเพิ่มแล้ว
วิธีเพิ่ม NIC ที่ 2 ให้ KVM Virtual Machine
ลองเพิ่ม Network Interface อีกตัวให้ VM ขั้นแรกตรวจสอบ Network Interfaces บน Linux Host:
# brctl show

ผมมี VM 1 ตัว มี Network Interface 1 ตัวบน KVM Host ต้องการเพิ่ม Virtual Network Interface อีกตัวไปที่ br0 รันคำสั่ง:
# virsh shutdown test-centos
# virsh attach-interface test-centos --type bridge --source br0 --persistent
# virsh start test-centos
ตรวจสอบว่า Network Interface อีกตัวปรากฏใน VM:

<interface type='bridge'> <mac address='52:54:00:7e:c1:9f'/> <source bridge='br0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> <interface type='bridge'> <mac address='52:54:00:2f:23:79'/> <source bridge='br0'/> <model type='rtl8139'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/> </interface>
ยังสามารถเปลี่ยน Network Settings ของ VM โดยตรงผ่าน XML File:
# virsh edit test-centos
หลัง Network Interface ตัวแรก เพิ่มบรรทัด:
<interface type='bridge'> <source bridge='br0'/> </interface>
Save File และ Start VM KVM จะเพิ่ม Config ที่เหลือ (MAC Address ฯลฯ) ให้อัตโนมัติ
บทความนี้ครอบคลุมสิ่งสำคัญที่ต้องการจัดการ KVM VMs จาก Linux Server Console ในบทความถัดไปจะแสดงวิธีจัดการ VMs ผ่าน virt-manager GUI