This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
технология_kvm [2022/02/10 14:13] val [Технология KVM] |
технология_kvm [2025/02/13 08:34] (current) val [Загрузка образа] |
||
---|---|---|---|
Line 5: | Line 5: | ||
* [[https://docs.fedoraproject.org/ru-RU/Fedora/12/html/Virtualization_Guide/sect-Virtualization_Guide-KVM_live_migration-Live_KVM_migration_with_virsh.html|Живая миграция с помощью virsh]] | * [[https://docs.fedoraproject.org/ru-RU/Fedora/12/html/Virtualization_Guide/sect-Virtualization_Guide-KVM_live_migration-Live_KVM_migration_with_virsh.html|Живая миграция с помощью virsh]] | ||
* [[https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/high_availability_add-on_reference/virtualnoderesource|Configuring a Virtual Domain as a Resource]] | * [[https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/high_availability_add-on_reference/virtualnoderesource|Configuring a Virtual Domain as a Resource]] | ||
+ | * [[https://reintech.io/blog/installing-using-kvm-virtualization-debian-12|Installing and Using KVM for Virtualization on Debian 12]] | ||
+ | * [[https://linux-notes.org/vnc-dostup-k-virtual-noj-mashine-kvm/?ysclid=ly7bkwvrj8307857187|VNC доступ к виртуальной машине KVM]] | ||
+ | * [[https://sumit-ghosh.com/posts/create-vm-using-libvirt-cloud-images-cloud-init/|Creating a VM using Libvirt, Cloud Image and Cloud-Init]] | ||
===== Установка ===== | ===== Установка ===== | ||
+ | |||
+ | * !!! 4GB RAM в хост систему (однажды хватило 2:) | ||
<code> | <code> | ||
# egrep -c '(vmx|svm)' /proc/cpuinfo | # egrep -c '(vmx|svm)' /proc/cpuinfo | ||
- | # apt install qemu qemu-kvm libvirt-daemon libvirt-clients bridge-utils virt-manager | + | # apt update |
+ | # time apt install qemu-kvm libvirt-daemon libvirt-clients bridge-utils virt-manager #qemu | ||
+ | real 4m27.401s | ||
# apt install libosinfo-bin | # apt install libosinfo-bin | ||
Line 19: | Line 26: | ||
<code> | <code> | ||
node1# wget -O /tmp/WindowsXPProSP3_rus_boot.iso http://val.bmstu.ru/unix/iso/WindowsXPProSP3_rus_boot.iso | node1# wget -O /tmp/WindowsXPProSP3_rus_boot.iso http://val.bmstu.ru/unix/iso/WindowsXPProSP3_rus_boot.iso | ||
- | |||
- | node1# mkdir /disk3/images | ||
node1# virt-install --name winxp --os-type windows --os-variant winxp \ | node1# virt-install --name winxp --os-type windows --os-variant winxp \ | ||
- | --ram 512 --vcpu 1 --disk path=/disk3/images/winxp.qcow2,size=2 \ | + | --ram 512 --vcpu 1 --disk path=/disk3/winxp.qcow2,size=2 \ |
--graphics vnc,listen=0.0.0.0 --noautoconsole --hvm \ | --graphics vnc,listen=0.0.0.0 --noautoconsole --hvm \ | ||
--network bridge=br0,model=rtl8139 \ | --network bridge=br0,model=rtl8139 \ | ||
Line 30: | Line 35: | ||
node1# virsh list --all | node1# virsh list --all | ||
- | node1# virsh vncdisplay winxp1 | + | node1# virsh vncdisplay winxp |
:0 | :0 | ||
</code> | </code> | ||
- | Подключаемся любым vnc клиентом к порту 5900, в процессе инсталляции XP VM надо будет повторно запустить | + | Подключаемся любым vnc клиентом ([[Сервис VNC]]) к порту 5900, в процессе инсталляции XP VM надо будет повторно запустить |
<code> | <code> | ||
# virsh start winxp | # virsh start winxp | ||
Line 46: | Line 51: | ||
# virsh undefine winxp | # virsh undefine winxp | ||
- | # cp /disk3/images/winxp.qcow2 /disk3/images/winxp2.qcow2 | + | # cp /disk3/winxp.qcow2 /disk3/winxp1.qcow2 |
</code> | </code> | ||
===== Создание конфигурации VM из образа ===== | ===== Создание конфигурации VM из образа ===== | ||
<code> | <code> | ||
- | node1# wget -O /disk3/images/winxp2.qcow2 http://val.bmstu.ru/unix/img/winxp.qcow2 | + | node1# wget http://val.bmstu.ru/unix/img/winxp.qcow2.zip |
- | node1# virt-install --name winxp2 --os-type windows --os-variant winxp \ | + | node1# time zcat winxp.qcow2.zip > /disk3/winxp1.qcow2 |
- | --ram 512 --vcpu 1 --disk path=/disk3/images/winxp2.qcow2 --graphics vnc,listen=0.0.0.0 \ | + | |
+ | node1# virt-install --name winxp1 --os-type windows --os-variant winxp \ | ||
+ | --ram 512 --vcpu 1 --disk path=/disk3/winxp1.qcow2 --graphics vnc,listen=0.0.0.0 \ | ||
--noautoconsole --hvm --boot hd --network bridge=br0,model=rtl8139 \ | --noautoconsole --hvm --boot hd --network bridge=br0,model=rtl8139 \ | ||
- | --print-xml | tee /disk3/winxp2.xml | + | --print-xml | tee /disk3/winxp1.xml |
- | node1# virsh define /disk3/winxp2.xml | + | node1# virsh define /disk3/winxp1.xml |
- | node1# virsh start winxp2 | + | node1# virsh edit winxp1 |
- | ... | + | node1# virsh start winxp1 |
- | node1# virsh undefine winxp2 | + | node1# virsh undefine winxp1 |
- | или запуск без регистрации VM в KVM | + | или, запуск без регистрации VM в KVM |
- | node1# virsh create /disk3/winxp2.xml | + | node1# virsh create /disk3/winxp1.xml |
+ | |||
+ | node1# virsh list --all | ||
+ | |||
+ | node1# virsh vncdisplay winxp1 | ||
</code> | </code> | ||
+ | * [[Сервис VNC]] | ||
===== Миграция VM ===== | ===== Миграция VM ===== | ||
+ | |||
+ | * Для "зрелищности" можно подключиться к winxp1 по RDP и/или подключиться с winxp1 по ssh к server1 | ||
+ | |||
<code> | <code> | ||
- | node1# virsh -c qemu+ssh://node2/system list --all | + | node1# virsh migrate --live winxp1 qemu+ssh://node2/system |
- | node1# virsh migrate --live winxp2 qemu+ssh://node2/system | + | node1# virsh -c qemu+ssh://node2/system list |
+ | |||
+ | node1# virsh -c qemu+ssh://node2/system shutdown winxp1 | ||
</code> | </code> | ||
===== Интеграция с Pacemaker ===== | ===== Интеграция с Pacemaker ===== | ||
- | * [[Пакет Pacemaker]] | + | * В [[Пакет Pacemaker#Управление конфигурацией]] Pacemaker увеличить op timeout и настроить [[Пакет Pacemaker#Запуск после монтирования файловых систем]] |
<code> | <code> | ||
- | crm(live/node1)configure# primitive pr_vm_winxp2 ocf:heartbeat:VirtualDomain params config=/disk3/winxp2.xml migration_transport=ssh meta allow-migrate=true | + | node1# ssn2 virsh -c qemu+ssh://node1/system list # проверка ssh доступа с node2 на node1 |
+ | |||
+ | crm(live/node1)configure# primitive pr_vm_winxp1 ocf:heartbeat:VirtualDomain params config=/disk3/winxp1.xml migration_transport=ssh meta allow-migrate=true | ||
+ | </code> | ||
+ | |||
+ | ===== Интеграция с Cloud-init ===== | ||
+ | |||
+ | * [[https://sumit-ghosh.com/posts/create-vm-using-libvirt-cloud-images-cloud-init/|Creating a VM using Libvirt, Cloud Image and Cloud-Init]] | ||
+ | ==== Загрузка образа ==== | ||
+ | |||
+ | * [[https://cloud-images.ubuntu.com/]] | ||
+ | * [[https://cloud.debian.org/images/cloud/]] | ||
+ | <code> | ||
+ | # wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img | ||
+ | # wget https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2 | ||
+ | |||
+ | # qemu-img info --output json /root/noble-server-cloudimg-amd64.img | ||
+ | # qemu-img info --output json /root/debian-12-generic-amd64.qcow2 | ||
+ | |||
+ | # time apt install libguestfs-tools | ||
+ | real 1m47.380s | ||
+ | |||
+ | # time virt-customize -a /root/noble-server-cloudimg-amd64.img --root-password password:'strongpassword' | ||
+ | # time virt-customize -a /root/debian-12-generic-amd64.qcow2 --root-password password:'strongpassword' | ||
+ | real 1m26.032s | ||
+ | </code> | ||
+ | |||
+ | * [[https://gist.github.com/shamil/62935d9b456a6f9877b5|How to mount a qcow2 disk image]] | ||
+ | ==== Создание пула ==== | ||
+ | <code> | ||
+ | # virsh pool-define-as pool-nodes --type dir --target /var/lib/libvirt/pool-nodes | ||
+ | |||
+ | # mkdir /var/lib/libvirt/pool-nodes | ||
+ | |||
+ | # virsh pool-start pool-nodes | ||
+ | |||
+ | # virsh pool-list --all | ||
+ | |||
+ | # ###virsh pool-destroy --pool pool-nodes | ||
+ | # ###virsh pool-delete --pool pool-nodes | ||
+ | # ###virsh pool-undefine --pool pool-nodes | ||
+ | </code> | ||
+ | ==== Создание тома ==== | ||
+ | |||
+ | * [[https://askubuntu.com/questions/299570/how-do-i-import-a-disk-image-into-libvirt|How do I import a disk image into libvirt?]] | ||
+ | * [[https://serverfault.com/questions/324281/how-do-you-increase-a-kvm-guests-disk-space|How do you increase a KVM guest's disk space?]] | ||
+ | |||
+ | <code> | ||
+ | # apt install jq | ||
+ | |||
+ | # virsh vol-create-as --pool pool-nodes --name disk-node1 --capacity $(qemu-img info --output json /root/noble-server-cloudimg-amd64.img | jq -r .[\"virtual-size\"]) | ||
+ | # virsh vol-create-as --pool pool-nodes --name disk-node1 --capacity $(qemu-img info --output json /root/debian-12-generic-amd64.qcow2 | jq -r .[\"virtual-size\"]) | ||
+ | |||
+ | # virsh vol-upload --pool pool-nodes --vol disk-node1 --file /root/noble-server-cloudimg-amd64.img | ||
+ | # virsh vol-upload --pool pool-nodes --vol disk-node1 --file /root/debian-12-generic-amd64.qcow2 | ||
+ | |||
+ | # virsh vol-info --pool pool-nodes --vol disk-node1 | ||
+ | |||
+ | # qemu-img resize /var/lib/libvirt/pool-nodes/disk-node1 +5G | ||
+ | |||
+ | # ###virsh vol-delete --pool pool-nodes --vol disk-node1 | ||
+ | </code> | ||
+ | |||
+ | ==== Создание образа cloud-init ==== | ||
+ | <code> | ||
+ | # mkdir -p nodes; cd $_ | ||
+ | </code> | ||
+ | * [[Технология Cloud-init]] | ||
+ | |||
+ | <code> | ||
+ | # apt install genisoimage | ||
+ | |||
+ | # genisoimage -output cloud-init-node1.iso -volid cidata -joliet -rock user-data meta-data network-config | ||
+ | |||
+ | # virsh vol-create-as --pool pool-nodes --name cloud-init-node1 --capacity $(stat -Lc%s cloud-init-node1.iso) --format raw | ||
+ | |||
+ | # virsh vol-upload --pool pool-nodes --vol cloud-init-node1 --file cloud-init-node1.iso | ||
+ | |||
+ | # virsh vol-list --pool pool-nodes | ||
+ | |||
+ | # ###virsh vol-delete --pool pool-nodes --vol cloud-init-node1 | ||
+ | </code> | ||
+ | |||
+ | ==== Создание, регистрация и запуск VM ==== | ||
+ | |||
+ | * [[Технология LXC#Настройка bridge]] | ||
+ | |||
+ | <code> | ||
+ | # virt-install --osinfo list | grep debian | ||
+ | |||
+ | # virt-install \ | ||
+ | --name domain-node1 \ | ||
+ | --osinfo debian11 \ | ||
+ | --ram=2048 \ | ||
+ | --vcpus=2 \ | ||
+ | --network bridge:br0 \ | ||
+ | --disk vol=pool-nodes/disk-node1 \ | ||
+ | --disk vol=pool-nodes/cloud-init-node1,device=cdrom \ | ||
+ | --boot hd \ | ||
+ | --serial pty \ | ||
+ | --autoconsole text \ | ||
+ | --graphics vnc,listen=0.0.0.0 \ | ||
+ | --import | ||
+ | </code><code> | ||
+ | Escape character is ^] (Ctrl + ]) | ||
+ | ... | ||
+ | node1 login: | ||
+ | ... | ||
+ | [ OK ] Reached target cloud-init.target - Cloud-init target. | ||
+ | |||
+ | # virsh list --all | ||
+ | |||
+ | # ###virsh console --domain domain-node1 | ||
+ | # ###virsh --connect qemu:///system console domain-node1 | ||
+ | |||
+ | # virsh vncdisplay domain-node1 | ||
+ | |||
+ | # virsh --connect qemu:///system start domain-node1 | ||
+ | |||
+ | # ###virsh destroy domain-node1 | ||
+ | # ###virsh undefine --domain domain-node1 | ||
</code> | </code> | ||
Line 94: | Line 231: | ||
virt-install --name winxp1 --os-type windows --os-variant winxp --ram 512 \ | virt-install --name winxp1 --os-type windows --os-variant winxp --ram 512 \ | ||
- | --vcpu 1 --disk path=/var/lib/libvirt/images/winxp1.qcow2,size=2 \ | + | --vcpu 1 --disk path=/var/lib/libvirt/winxp1.qcow2,size=2 \ |
--graphics vnc,listen=0.0.0.0 --noautoconsole \ | --graphics vnc,listen=0.0.0.0 --noautoconsole \ | ||
--hvm --cdrom /var/lib/libvirt/boot/WindowsXPProSP3_rus_boot.iso --boot cdrom,hd | --hvm --cdrom /var/lib/libvirt/boot/WindowsXPProSP3_rus_boot.iso --boot cdrom,hd |