顯示頁面舊版反向連結Fold/unfold all回到頁頂 本頁是唯讀的,您可以看到原始碼,但不能更動它。您如果覺得它不應被鎖上,請詢問管理員。 ====== CentOS 6 KVM 命令操作相關 ====== ===== 列出 VM 清單 ===== * <code sh>virsh list --all</code> ===== 建立 qcow2 格式的 image ===== * <code sh>qemu-img create -f qcow2 -o preallocation=off kvm.30G.qcow2 30G</code> * 參考網址 - http://kashyapc.wordpress.com/2011/09/24/creating-a-qcow2-virtual-machine/ ===== 建立虛擬網路 ===== * Exp. 建立 virtbr1 <code sh> brctl addbr virbr1 </code> * 參考網址 - 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<code sh> 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 </code> ==== 透過 image template 建立 VM ===== * 與建立全新的 VM 語法相似, 參數需要增加 --boot hd Exp. <code sh> 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 </code> * 參考網址 - * 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 <code sh> virsh destroy ever-test-1 </code> ===== 啟動 VM ===== * Exp. ever-test-1<code sh> virsh start ever-test-1 </code> * 參考網址 - [[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<code sh> virsh autostart ever-percona1 </code><file> [root@HP-ML10G9 ~]# virsh autostart ever-percona1 Domain ever-percona1 marked as autostarted </file> * 如果要關閉自動啟動, 後面增加 --disable Exp. ever-percona1<code sh> virsh autostart ever-percona1 --disable </code><file> [root@HP-ML10G9 ~]# virsh autostart ever-percona1 --disable Domain ever-percona1 unmarked as autostarted </file> * 如果要查看有哪些 VM 會開機自動啟動, 可以看 /etc/libvirt/qemu/autostart/ 內的 VM 清單<code sh> ls -lt /etc/libvirt/qemu/autostart/ </code><file> [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 : : </file> ===== 啟動 VM 並顯示 console ===== * Exp. ever-test-1<code sh> virsh start ever-test-1 && virsh console ever-test-1 </code> * 要離開 console 模式可以輸入 CTRL + 5 或 CTRL + ] <note> * 為了讓 guest vm 開機時的過程可以在遠端命令畫面看見, 可以透過 - 確認 guest vm xml 內定義的 serial Exp. <code sh>virsh edit ever-test-1</code><file> : <serial type='pty'> <target port='0'/> </serial> <console type='pty'> <target type='serial' port='0'/> </console> : </file> - guest vm 開機時的 kernel 參數增加 console=ttyS0,115200 Exp. 進入 guest vm 內 <code sh>vi /boot/grub/grub.conf</code><file> : 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 : </file> </note> ===== VM 掛上與卸下光碟 ISO 檔案 ===== * 掛上光碟 ISO 檔 <code sh> virsh attach-disk ever-test-1 /home/ISO/CentOS-6.8-x86_64-minimal.iso hdc --type cdrom --mode readonly </code> * 卸下光碟 ISO 檔 <code sh> virsh attach-disk ever-test-1 '' hdc --type cdrom --mode readonly </code> * 參考網址 - http://superuser.com/questions/239870/change-cd-rom-via-virsh ===== VM 掛上與卸下外部磁碟 qcow2 檔案 ===== * Exp. 對 VM ever-percona1 掛上 ever-hd-db1.qcow2 磁碟檔 * 先建立 ever-hd-db1.qcow2<code sh> qemu-img create -f qcow2 -o preallocation=off kvm.30G.qcow2 30G cp kvm.30G.qcow2 ever-hd-db1.qcow2 </code> * 立即掛上 vm <code sh> virsh attach-disk ever-percona1 /kvm/image/ever-hd-db1.qcow2 vdb --subdriver qcow2 --cache none </code> * 立即卸下 vm <code sh> virsh detach-disk ever-percona1 vdb </code> <note> * 如果要重起後看到掛上的這外部磁碟, 可以使用 --config 語法 Exp.<code sh> virsh attach-disk --config ever-percona1 /kvm/image/ever-hd-db1.qcow2 vdb --subdriver qcow2 --cache none </code> </note> * 參考網址 - [[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 不會出現在清單內<code sh>virsh list --all</code> * Exp. 要移除 ever-test - 先關閉 ever-test - 移除 ever-test <code sh> virsh undefine ever-test </code> <note> * 如果 VM 之前有建立 snapshot 就會出現錯誤訊息 - Requested operation is not valid: cannot delete inactive domain with 2 snapshots<file> [root@xpc-sh67-i7 ~]# virsh undefine kvm-oVirt0 錯誤:無法為區域 kvm-oVirt0 取消定義 錯誤:Requested operation is not valid: cannot delete inactive domain with 2 snapshots </file> * 解決方式就是先移除這些 snapshots 語法如下:<code sh> virsh snapshot-list --domain VM_NAME </code><file> [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 </file><code sh> virsh snapshot-delete --domain VM_NAME --snapshotname SNAPSHOT_NAME </code><file> [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 </file> * 再來就可以移除掉這 VM <file> [root@xpc-sh67-i7 ~]# virsh undefine kvm-oVirt0 區域 kvm-oVirt0 已經取消定義 </file> </note> * 參考網址 - 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 定義檔 <code sh> cd /etc/libvirt/qemu virsh dumpxml ever-test-1 > ever-esetras.xml </code> - 編輯 ever-esetras.xml <file> : <name>ever-esetras</name> : </file> - 移除 ever-test-1 與啟用 ever-esetras<code sh> virsh undefine ever-test-1 virsh define ever-esetras.xml virsh start ever-esetras </code> * 參考網址 - http://leo0216.blog.51cto.com/188904/1032371 ===== 其他相關頁面 ===== {{topic>linux-kvm}} {{tag>虛擬化 vm linux-kvm}} tech/centos6_kvm.txt 上一次變更: 2018/06/03 00:11由 Jonathan Tsai