====== CentOS 6 KVM 命令操作相關 ====== ===== 列出 VM 清單 ===== * virsh list --all ===== 建立 qcow2 格式的 image ===== * qemu-img create -f qcow2 -o preallocation=off kvm.30G.qcow2 30G * 參考網址 - http://kashyapc.wordpress.com/2011/09/24/creating-a-qcow2-virtual-machine/ ===== 建立虛擬網路 ===== * Exp. 建立 virtbr1 brctl addbr virbr1 * 參考網址 - https://www.ichiayi.com/wiki/tech/linux_bridge ===== 建立全新的 VM ===== * Exp. ever-test-1 2 vCPU/2G RAM/20G HD CDROM:CentOS-6.8-x86_64-minimal.iso virt-install --name ever-test-1 --vcpu 2 --ram 2048 --cdrom /home/ISO/CentOS-6.8-x86_64-minimal.iso --os-type linux --disk /kvm/image/ever-test-1.qcow2,device=disk,bus=virtio,format=qcow2,size=20 --network bridge:br0 --network bridge:br1 ==== 透過 image template 建立 VM ===== * 與建立全新的 VM 語法相似, 參數需要增加 --boot hd Exp. virt-install --name kvm-autopack --vcpu 2 --ram 2048 --os-type linux --disk /media/a32af356-7f68-491a-b60c-b8985e4e7df8/kvm_image/kvm-autopack.qcow2,device=disk,bus=virtio,format=qcow2,size=20 --network bridge:br0 --boot hd * 參考網址 - * https://www.centos.org/docs/5/html/5.2/Virtualization/chap-Virtualization-Managing_guests_with_virsh.html * http://www.lijyyh.com/2015/12/linux-kvm-set-up-linux-kvm.html ===== 強制關閉 VM ===== * Exp. ever-test-1 virsh destroy ever-test-1 ===== 啟動 VM ===== * Exp. ever-test-1 virsh start ever-test-1 * 參考網址 - [[https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Virtualization_Deployment_and_Administration_Guide/sect-Troubleshooting-Common_libvirt_errors_and_troubleshooting.html|A.18.5. internal error cannot find character device (null)]] ===== 設定 Host 開機自動啟動 VM ===== * Exp. ever-percona1 virsh autostart ever-percona1 [root@HP-ML10G9 ~]# virsh autostart ever-percona1 Domain ever-percona1 marked as autostarted * 如果要關閉自動啟動, 後面增加 --disable Exp. ever-percona1 virsh autostart ever-percona1 --disable [root@HP-ML10G9 ~]# virsh autostart ever-percona1 --disable Domain ever-percona1 unmarked as autostarted * 如果要查看有哪些 VM 會開機自動啟動, 可以看 /etc/libvirt/qemu/autostart/ 內的 VM 清單 ls -lt /etc/libvirt/qemu/autostart/ [root@HP-ML10G9 ~]# ls -lt /etc/libvirt/qemu/autostart/ 總計 0 lrwxrwxrwx 1 root root 34 2018-01-14 13:59 ever-phplist.xml -> /etc/libvirt/qemu/ever-phplist.xml : : ===== 啟動 VM 並顯示 console ===== * Exp. ever-test-1 virsh start ever-test-1 && virsh console ever-test-1 * 要離開 console 模式可以輸入 CTRL + 5 或 CTRL + ] * 為了讓 guest vm 開機時的過程可以在遠端命令畫面看見, 可以透過 - 確認 guest vm xml 內定義的 serial Exp. virsh edit ever-test-1 : : - guest vm 開機時的 kernel 參數增加 console=ttyS0,115200 Exp. 進入 guest vm 內 vi /boot/grub/grub.conf : title CentOS (2.6.32-642.6.1.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-642.6.1.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=zh_TW.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap crashkernel=auto rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet console=ttyS0,115200 initrd /initramfs-2.6.32-642.6.1.el6.x86_64.img : ===== VM 掛上與卸下光碟 ISO 檔案 ===== * 掛上光碟 ISO 檔 virsh attach-disk ever-test-1 /home/ISO/CentOS-6.8-x86_64-minimal.iso hdc --type cdrom --mode readonly * 卸下光碟 ISO 檔 virsh attach-disk ever-test-1 '' hdc --type cdrom --mode readonly * 參考網址 - http://superuser.com/questions/239870/change-cd-rom-via-virsh ===== VM 掛上與卸下外部磁碟 qcow2 檔案 ===== * Exp. 對 VM ever-percona1 掛上 ever-hd-db1.qcow2 磁碟檔 * 先建立 ever-hd-db1.qcow2 qemu-img create -f qcow2 -o preallocation=off kvm.30G.qcow2 30G cp kvm.30G.qcow2 ever-hd-db1.qcow2 * 立即掛上 vm virsh attach-disk ever-percona1 /kvm/image/ever-hd-db1.qcow2 vdb --subdriver qcow2 --cache none * 立即卸下 vm virsh detach-disk ever-percona1 vdb * 如果要重起後看到掛上的這外部磁碟, 可以使用 --config 語法 Exp. virsh attach-disk --config ever-percona1 /kvm/image/ever-hd-db1.qcow2 vdb --subdriver qcow2 --cache none * 參考網址 - [[https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Virtualization_Administration_Guide/sect-Virtualization-Virtualized_block_devices-Adding_storage_devices_to_guests.html#sect-Virtualization-Adding_storage_devices_to_guests-Adding_file_based_storage_to_a_guest|13.3.1. Adding File-based Storage to a Guest]] ===== 移除 VM ===== * 主要是移除 kvm 內這 VM 的定義, 讓該 VM 不會出現在清單內virsh list --all * Exp. 要移除 ever-test - 先關閉 ever-test - 移除 ever-test virsh undefine ever-test * 如果 VM 之前有建立 snapshot 就會出現錯誤訊息 - Requested operation is not valid: cannot delete inactive domain with 2 snapshots [root@xpc-sh67-i7 ~]# virsh undefine kvm-oVirt0 錯誤:無法為區域 kvm-oVirt0 取消定義 錯誤:Requested operation is not valid: cannot delete inactive domain with 2 snapshots * 解決方式就是先移除這些 snapshots 語法如下: virsh snapshot-list --domain VM_NAME [root@xpc-sh67-i7 ~]# virsh snapshot-list --domain kvm-oVirt0 名稱 Creation Time 狀態 ------------------------------------------------------------ 1383751914 2013-11-06 23:31:54 +0800 running 1383753206 2013-11-06 23:53:26 +0800 running virsh snapshot-delete --domain VM_NAME --snapshotname SNAPSHOT_NAME [root@xpc-sh67-i7 ~]# virsh snapshot-delete --domain kvm-oVirt0 --snapshotname 1383751914 Domain snapshot 1383751914 deleted [root@xpc-sh67-i7 ~]# virsh snapshot-delete --domain kvm-oVirt0 --snapshotname 1383753206 Domain snapshot 1383753206 deleted * 再來就可以移除掉這 VM [root@xpc-sh67-i7 ~]# virsh undefine kvm-oVirt0 區域 kvm-oVirt0 已經取消定義 * 參考網址 - https://www.cyberciti.biz/faq/howto-linux-delete-a-running-vm-guest-on-kvm/ ===== 更改 VM 名稱 ===== * Exp. 對 VM ever-test-1 改成 ever-esetras - 先關閉 ever-test-1 - 複製 vm 定義檔 cd /etc/libvirt/qemu virsh dumpxml ever-test-1 > ever-esetras.xml - 編輯 ever-esetras.xml : ever-esetras : - 移除 ever-test-1 與啟用 ever-esetras virsh undefine ever-test-1 virsh define ever-esetras.xml virsh start ever-esetras * 參考網址 - http://leo0216.blog.51cto.com/188904/1032371 ===== 其他相關頁面 ===== {{topic>linux-kvm}} {{tag>虛擬化 vm linux-kvm}}