æ¦è¦
æ¬è¨äºã§ã¯ãQEMUã¨libvirtã使ã£ã¦ä»®æ³ãã·ã³ãCLIã§å¶å¾¡ããæ¹æ³ã解説ãã¾ããç¹ã«ä»¥ä¸ã®ãã¤ã³ãã«ç¦ç¹ãå½ã¦ã¦ãã¾ãã
- ã¹ãããã·ã§ããã®ä½æã»ãªãã¼ãæ¹æ³
- å·®åãã£ã¹ã¯ã使ã£ãå¹ççãªç°å¢æ§ç¯
ã¯ããã«
ãã®è¨äºã¯ãNFLaboratories Advent Calendar 2024 3æ¥ç®ã®è¨äºã§ã*1ã
ããã«ã¡ã¯ãæè²ã½ãªã¥ã¼ã·ã§ã³æ
å½ã®åæ¦ãæè¤ãç¥ã§ãã
çããã¯æ®æ®µã®çæ´»ã®ä¸ã§ã©ããããä»®æ³ãã·ã³ã«ä¾åãã¦ããã§ããããã
æã
ã»ãã¥ãªãã£ã¨ã³ã¸ãã¢ã«ã¨ã£ã¦ãä»®æ³ãã·ã³ã¯ãã«ã¦ã§ã¢ã®è§£æç°å¢ãããããç°å¢ã®æ§ç¯ãªã©æ§ã
ãªç¨éã§å©ç¨ããå¿
è¦ä¸å¯æ¬ ãªåå¨ã§ããã»ãã¥ãªãã£ã¨ã³ã¸ãã¢ã§ãªãã¨ããæä»®æ³å製åã®å¤ä¸ãã®å½±é¿ã«ããæ製åããã®è±å´ãæ¤è¨ãã¦ããã¨ãã声ãè´ããæ¹ã¯å¤ãããããã¾ãããèªç¤¾ãµã¼ããæ±ããå¤ãã®çµç¹ã«ã¨ã£ã¦ãä»®æ³ãã·ã³ã®éç¨æ¹éãã©ããããã¨ãã課é¡ã¯é¿ããããªããã®ã¨ãªã£ã¦ãã¾ãã
ãã®ãããªæ代ã®ä¸ã§ãNFLabs.ã解æç°å¢ã"KVM+QEMU"ã使ã£ã¦æ§ç¯ãã¦ã¿ããã¨ã«ãã¾ãããããã¦å®éã«ä½¿ç¨ãã¦ã¿ã¾ãã¨ã解æç°å¢ã¨ãã¦é常ã«æç¨ãªç¹ãå¤ã è¦ã¤ããã¾ããã
- ï¼ä½æçã«ã¯ï¼æ製åãããé«éãªèµ·åã»ãªãã¼ããã§ãã
- èµ·åããçµäºã¾ã§ã®å·®åãå·®åãã£ã¹ã¯ã¨ãã¦ä¿åã§ãããããã¨ã¦ã軽éã§å¹ççã«ã·ã°ããã£ãããã³ã°ãã§ãã
- ç°ãªãCPUã¢ã¼ããã¯ãã£ã®ç°å¢ãæ§ç¯ãããã¨ãã§ãã
ä¸è¨ã®ãã¨ãããæ¬è¨äºã§ã¯"KVM+QEMU"ãå¸æããæå³ãè¾¼ãã¦ãQEMUã§æ§ç¯ããä»®æ³ãã·ã³ã®æä½æ¹æ³ï¼èµ·åã»åæ¢ãã¹ãããã·ã§ããã®åå¾ï¼ã«ã¤ãã¦ç´¹ä»ãã¾ããç¹ã«å·®åãã£ã¹ã¯ã®ä½æã¨ãªãã¼ãã¾ã§ã®ä¸é£ã®æµãã«ã¤ãã¦ã¯ããã«ã¦ã§ã¢è§£æç°å¢ãæ§ç¯ããããã§ã®ä¸çªã®éè¦ãªãã¤ã³ãã ã¨æãã¾ãã®ã§ãé¡ä¼¼ã®è§£æç°å¢æ§ç¯ãèãã¦ããæ¹ã®åèã«ãªãã°å¹¸ãã§ãã
ãªããæ¬è¨äºã§ã¯æç« éã®é½åä¸ãKVMãQEMUãlibvirtã®ã¤ã³ã¹ãã¼ã«æ¹æ³ãªã©ã¯å«ã¾ãã¦ãã¾ãããã¤ã³ã¹ãã¼ã«æ¹æ³ã«ã¤ãã¦ã¯ããã§ã«æèè
ã®è¨äºãå¤æ°åå¨ããããããã¡ãããåç
§ãã ãã*2, *3ã
KVMã¨QEMU
QEMUã¯ä»®æ³ãã·ã³ãå®ç¾ããããã®ãªã¼ãã³ã½ã¼ã¹ã®ã¨ãã¥ã¬ã¼ã¿ã§ã*4ãç°ãªãCPUã¢ã¼ããã¯ãã£ã®OSãä»®æ³çã«åä½ããããã¨ãã§ããã®ã§ãVMwareãVirtualBoxã¨ã¯ç°ãªãx86_64ã®CPUä¸ã§ARM OSãåä½ããããã¨ãå¯è½ã§ãã
ãã®æ©è½ã ãã§ãä»ã®ä»®æ³å製åã¨ã¯ç°ãªãå¼·ã¿ãããã¾ããããããªãããã¨ãã¥ã¬ã¼ã·ã§ã³ãè¡ãç¹æ§ä¸ãåä½ãéããªããã¡ãªç¹ããããWindowsã®GUIæä½ãªã©ãã¦ã¼ã¶ã¼ä½é¨ãæãªããããã¨ãããã¾ãã
ãã®ãããCPUã®ä»®æ³åæ¯æ´æ©è½ã使ç¨ããQEMUããã¤ãã¼ãã¤ã¶ã¼ä¸ã§åä½ããããã¨ãä¸è¬çã§ãããããå®ç¾ããããã«ãQEMUã¨çµã¿åããã¦ãã使ç¨ãããã®ãKVMã§ããKVMï¼Kernel-based Virtual Machineï¼ã¯Linuxã«ã¼ãã«ã«çµã¿è¾¼ã¾ããä»®æ³åæ©æ§ã§ãQEMUã¨çµã¿åããããã¨ã§ããã¼ãã¦ã§ã¢æ¯æ´åã®é«éãªä»®æ³åãå®ç¾ãã¾ãã "KVM+QEMU"ã®æ§æã«ãããã¨ã§ãã¦ã¼ã¶ã¼ä½é¨ãè½ã¨ããã¨ãªãç°ãªãCPUã¢ã¼ããã¯ãã£ã®OSç°å¢ãè¤æ°åä½ããããã¨ãã§ãã¾ããããã¯è§£æè ã«ã¨ã£ã¦é常ã«ä¾¿å©ãªæ©è½ã¨ããã¾ãã
ããã¾ã§ãèªã¿ãªãããæ¹ã¯QEMUã®è¯ããç解ãã¦é ããã¨æãã¾ããããããã¯ãå®éã«QEMUã®ä»®æ³ãã·ã³ãæä½ãã¦ããã¾ããããä»åã®è§£æç°å¢ã§ã¯èªåã§èµ·åããªãã¼ããããããã®ã§ãlibvirtã使ã£ã¦ããã¾ãã
libvirtã使ã£ãåºæ¬æä½
libvirtã¯ä»®æ³åãã©ãããã©ã¼ã ï¼QEMU/KVMãVMwareãªã©ï¼ãçµ±ä¸çã«ç®¡çããããã®ãªã¼ãã³ã½ã¼ã¹ã§ã*5ã
QEMUãVMwareãªã©ã®ä»®æ³åæè¡ãæ½è±¡åããAPIãæä¾ããä»®æ³ãã·ã³ã®ä½æãèµ·åãåæ¢ã¨ãã£ãã©ã¤ããµã¤ã¯ã«ã管çãããã¨ãã§ãã¾ãã ã¾ããvirshãªã©ã®CLIãã¼ã«ãvirt-managerã¨ãã£ãGUIã®ã¯ã©ã¤ã¢ã³ããã¼ã«ãããã®ã³ã³ãã¼ãã³ãã®ä¸é¨ã§ãããä»åã¯ä¸»ã«virshã³ãã³ãï¼ä¸é¨ã¯qemu-imgï¼ã使ã£ã¦ä»®æ³ãã·ã³ã®æä½ãè¡ã£ã¦ãã¾ãã
virshã³ãã³ãã使ã£ãä»®æ³ãã·ã³ã®æä½
ããã§ç¨èªã«ã¤ãã¦è£è¶³ãã¾ãããç¹å®ã®ä»®æ³ãã·ã³ã®ãã¨ããã¡ã¤ã³ã¨å¼ã³ã¾ãã
ã³ãã³ãã©ã¤ã³ä¸ã§"ãã¡ã¤ã³å"ãå¼æ°ã¨ãã¦æ¸¡ãå ´åã¯ãç¹å®ã®ä»®æ³ãã·ã³ãæããã¨ã«æ³¨æãã¦ãã ããã
åä½ç°å¢
- OS: Ubuntu 24.04 LTS
- QEMU: 8.2.2
- libvirt: 10.0.0
- python: 3.12.3
ãªãã以éã®æä½ã§ä½¿ç¨ãã¦ããUbuntuã®ã¦ã¼ã¶ã¼ï¼workerã¦ã¼ã¶ã¼ï¼ã¯ãããããKVMã°ã«ã¼ãã«è¿½å ãã¦ãã¾ãã
ã¾ããQEMUä»®æ³ãã·ã³ã§ã¯ãWindows 11ãå
ã«ä½æãã qcow2 å½¢å¼ã®ãã£ã¹ã¯ã¤ã¡ã¼ã¸ï¼ä»¥éã®"windows11-0712-clone.qcow2"ã§ãï¼ã使ç¨ããä»®æ³ãã·ã³ã®åå㯠win11-test ã«è¨å®ãã¦ãã¾ãï¼ããã VM ã®ãã¡ã¤ã³åã«ãªãã¾ãï¼ã
$ ls /var/lib/libvirt/images windows11-0712-clone.qcow2
ä»®æ³ãã·ã³ãGUIä¸ã§ç¢ºèªããã«ã¯Virtual Machine Managerã使ãã¾ã*6ã
Virtual Machine Managerã¯ä»®æ³ãã·ã³ã管çã§ããGUIãã¼ã«ã§ãããlibvirtã使ç¨ãã¦ä»®æ³åæè¡ãç°¡åã«æä½ãããã¨ãå¯è½ã§ãã
ä»åã¯CLIã§ã®å¶å¾¡ãã¡ã¤ã³ãªã®ã§ãã¡ãã®ãã¼ã«ã«é¢ãã¦ã¯ãå®è¡çµæã®ç¢ºèªç¨åº¦ã«å©ç¨ãã¦ãã¾ãã
Virtual Machine Managerãã¤ã³ã¹ãã¼ã«ããç¶æ ã§ãã¿ã¼ããã«ã§ä»¥ä¸ã®ã³ãã³ããå®è¡ãããã¨ã§èµ·åã§ãã¾ãã
$ virt-manager
GUIã§ã¯ãæ¢ã«ä½æãããä»®æ³ãã·ã³ã¨ãã®ã¹ãã¼ã¿ã¹ã確èªãããã¨ãã§ãã¾ãã
æºåãæ´ã£ãã¨ããã§ããã£ããlibvirtã®virshã³ãã³ãã使ç¨ãã¦ããã¾ãã
ã¾ãã¯ä»®æ³ãã·ã³ã®ä¸è¦§ã表示ããã¦ã¿ã¾ãã
$ virsh list --all Id Name State ----------------------------------- 1 windows11-sample paused 109 win11-yo-2 paused - win11-0712 shut off - win11-test shut off
listãµãã³ãã³ãã¯ãä½ææ¸ã¿ã®ä»®æ³ãã·ã³ã¨ãã®ã¹ãã¼ã¿ã¹ã®ä¸è¦§ã表示ããããã¨ãã§ãããããä»®æ³ãã·ã³ã®ç¶æ ã確èªããéã«å½¹ç«ã¡ã¾ããã¾ããdomstateãµãã³ãã³ãã使ãã°ç¹å®ã®ä»®æ³ãã·ã³ã®ç¶æ ãåºåããããã¨ãå¯è½ã§ãã
$ virsh domstate win11-test shut off
èµ·åããéã¯ãstartãµãã³ãã³ãã使ãã¾ãã
$ virsh start win11-test Domain 'win11-test' started
åæ¢ããéã«ã¯ãshutdownãµãã³ãã³ããå©ç¨ã§ãã¾ãã
$ # virsh shutdown "VMãã¡ã¤ã³å" $ virsh shutdown win11-test Domain 'win11-test' is being shutdown $ # ä»®æ³ãã·ã³ãå®å ¨ã«åæ¢ããå¾ã«domstateãµãã³ãã³ããå®è¡ $ virsh domstate win11-test shut off
ãã®ããã«ãã¦ãlibvirtã§ä»®æ³ãã·ã³ã®å¶å¾¡ãç°¡åã«è¡ããã¨ãã§ãã¾ãã
次ã¯ã¹ãããã·ã§ããã®ä½æãä»®æ³ãã·ã³ã®ãªã¹ãã¢å¦çã«ã¤ãã¦ã§ãã
ã¹ãããã·ã§ããä½æãããªãã¼ãã¾ã§
ä»®æ³ãã·ã³å ã§ãã«ã¦ã§ã¢ã®æ¤ä½ãå®è¡ããå®è¡å¾ã®ã¡ã¢ãªã»ãã£ã¹ã¯ã®ç¶æ ãä¿åããã¨ãã£ããã¨ãæ³å®ãã¦ãã¡ã¢ãªã»ãã£ã¹ã¯ã¹ãããã·ã§ããã®åå¾ã¨ãªãã¼ããlibvirtã使ã£ã¦è¡ãã¾ããå¦çã®å ¨ä½çãªæµãã¯ä»¥ä¸ã®éãã§ãã
ã¹ãããã·ã§ããã®ä½æ
ã¹ãããã·ã§ãããä½æããã«ã¯ãvirshã®snapshot-create-asãµãã³ãã³ãã使ãã¾ã*7ã
snapshot-create-asãµãã³ãã³ãã§ã¯ããã£ã¹ã¯ã®ç¶æ
ã®ã¿ãä¿åãããã¨ããã¡ã¢ãªã®ç¶æ
ãä¸ç·ã«ä¿åãããã¨ãå¯è½ã§ãããªããQEMUã«ã¯å
é¨ã¹ãããã·ã§ããã¨å¤é¨ã¹ãããã·ã§ããã¨ãã2種é¡ã®ã¹ãããã·ã§ãããããã¾ã*8ãå
é¨ã¹ãããã·ã§ããã§ã¯ãã¼ã¹ã®qcow2å½¢å¼ã®ã¤ã¡ã¼ã¸ãã¡ã¤ã«ã«ä»®æ³ãã·ã³ã®ãã£ã¹ã¯ã®ç¶æ
ãä¿åãããã¨ãã§ãã¾ããããããç°å¢ï¼ãã¡ã¼ã ã¦ã§ã¢çï¼ã«ãã£ã¦ã¯ä½¿ç¨ã§ããªãã£ããããããã使ç¨ãéæ¨å¥¨ã¨ããã¦ãããã¨ãããã¨ãããã¨ã§ãä»åã¯å¤é¨ã¹ãããã·ã§ããæ©è½ã使ç¨ãã¦ããã¾ã*9 ,*10ã 以ä¸ã§ã¯--memspecãªãã·ã§ã³ã使ç¨ãã¦ãã¡ã¢ãªã¤ã¡ã¼ã¸ãä¿åãããã¡ã¤ã«åãæå®ããsnapshot=externalã§å¤é¨ã«ã¤ã¡ã¼ã¸ãã¡ã¤ã«ãä¿åããããã«æå®ãã¦ãã¾ããä½æãããã¹ãããã·ã§ããã¯æ¤ä½å®è¡å¾ã«ãªãã¼ãã§ã¯ãªã¼ã³ãªç¶æ
ã«ä»®æ³ãã·ã³ãæ»ãããã«ä½¿ç¨ãã¦ããã¾ãã
$ # virsh snapshot-create-as "VMãã¡ã¤ã³å" "ã¹ãããã·ã§ããå" --memspec file="ãã¡ã¤ã«ãã¹",snapshot="no, internal or external" --atomic $ virsh snapshot-create-as win11-test clean --memspec file=/var/lib/libvirt/images/win11-test-clean.memory,snapshot=external --atomic Domain snapshot clean created $ # ã¹ãããã·ã§ãããä½æããããã¨ã確èªãã $ virsh snapshot-list win11-test Name Creation Time State ---------------------------------------------- clean 2024-12-07 07:44:13 +0000 running
ã¹ãããã·ã§ãããä½æããã¨ãä¸è¨ã®ä¾ã§ã¯/var/lib/libvirt/images/ã«ã¡ã¢ãªã¤ã¡ã¼ã¸ãã¡ã¤ã«ã¨å·®åãã£ã¹ã¯ã®ãã¡ã¤ã«ãçæããã¾ãã
å·®åãã£ã¹ã¯ã¯ãã¼ã¹ã¨ãªããã£ã¹ã¯ã¤ã¡ã¼ã¸ï¼qcow2å½¢å¼ã§ãä»åã®å ´åã¯"windows11-0712-clone.qcow2"ãããã«è©²å½ãã¾ãï¼ã¯å¤æ´ããã«ãå¥ã®ãã¡ã¤ã«ã«å¤æ´åãä¿åãããã®ã¨ãªãã¾ãããã®ããããã¹ãã®ãã£ã¹ã¯å®¹éãç¯ç´ã§ããã¨ããã®ã大ããªã¡ãªããã§ããã¾ãããã«ã¦ã§ã¢è§£æã§ã¯ããã«ã¦ã§ã¢ãæ®ããçè·¡ï¼ã©ããªãã¡ã¤ã«ãä½æã»åé¤ããã®ããªã©ï¼ãå¹çããè¦ã¤ãããããããã©ã¬ã³ã¸ãã¯ã«ããã¢ã¼ãã£ãã¡ã¯ãã®åéããã·ã°ããã£ã®ä½æã«å½¹ç«ã¡ã¾ããä¿åå
ã®ãã£ã¹ã¯å½¢å¼ã¯ãã¼ã¹ã®ãã£ã¹ã¯ã¤ã¡ã¼ã¸ï¼ä»åã®å ´åã¯qcow2å½¢å¼ï¼ã«åãããå¿
è¦ãããç¹ã«æ³¨æãã¦ãã ããã
ããã§ããã«ã¦ã§ã¢ã®æ¤ä½ãå®è¡ããã¨ä»®å®ãã¾ãã
æ¤ä½å®è¡å¾ã®æ±æããããã£ã¹ã¯ãå®è¡å¾ã®ã¡ã¢ãªã¤ã¡ã¼ã¸ãä¿åãããããåã³ã¹ãããã·ã§ãããä½æãã¾ãã
ãã®ã¨ããã¡ã¢ãªã¨ãã£ã¹ã¯ã®ç¶æ
ã®æ´åæ§ãä¿ã¡ã¤ã¤å®å
¨ã«ãã¼ã¿ãä¿åãããããä»®æ³ãã·ã³ãä¸æåæ¢ãã¦ããã¾ããä»®æ³ãã·ã³ã®ä¸æåæ¢ã«ã¯suspendãµãã³ãã³ãã使ç¨ã§ãã¾ãã
ã¾ããä»®ã«ã¡ã¢ãªã¤ã¡ã¼ã¸ã®ã¿ãä¿åãããã¨ããå ´åã¯ãsnapshot-create-asãµãã³ãã³ãã®ä»£ããã«ãsaveãµãã³ãã³ãã使ç¨ã§ãã¾ããsaveãµãã³ãã³ãã¯ãå®è¡ä¸ã®ä»®æ³ãã·ã³ã®ã¡ã¢ãªã®ç¶æ ãä¿åãããã¨ãä»®æ³ãã·ã³ãåæ¢ããæ©è½ãæã¡ã¾ããä»®æ³ãã·ã³ã®åæ¢å¾ã¯ãrestoreãµãã³ãã³ãã使ç¨ãããã¨ã§ä»®æ³ãã·ã³ããªã¹ãã¢ï¼ã¡ã¢ãªã®ç¶æ ãåºã«æ»ãï¼ãããã¨ãã§ãã¾ããããã£ã¹ã¯ã®ç¶æ ã¯å¾©å ãããã«ææ°ã®ãã£ã¹ã¯ã®ç¶æ ï¼XMLã®è¨å®ãã¡ã¤ã«ã«è¨è¼ãã¦ããå·®åãã£ã¹ã¯ã¤ã¡ã¼ã¸ï¼ã§èµ·åããããããªãã¼ãã¨ã¯ç°ãªãåä½ããããã¨ã«ã¯æ³¨æãã¦ãã ãããä¸æ¹ãå·®åãã£ã¹ã¯ã®ã¿ãä¿åãããå ´åã«ã¯ãsnapshot-create-asãµãã³ãã³ãã®ãªãã·ã§ã³--disk-onlyãæå®ã§ãã¾ãã
以ä¸ã®ä¾ã§ã¯ãsnapshot-create-asãµãã³ãã³ãã§ã¡ã¢ãªã¤ã¡ã¼ã¸ã¨å·®åãã£ã¹ã¯ãåå¾ãã¦ãã¾ããéç¨æ¹éã«å¿ãã¦ãsaveãsnapshot-create-asã使ãåããã¨è¯ãã§ãããã
$ # ä»®æ³ãã·ã³ãä¸æåæ¢ $ virsh suspend win11-test Domain 'win11-test' suspended $ # ãã«ã¦ã§ã¢æ¤ä½å®è¡å¾ã®ã¹ãããã·ã§ãããä½æ $ virsh snapshot-create-as win11-test dirty --memspec file=/var/lib/libvirt/images/win11-test-clean.memory,snapshot=external --atomic Domain snapshot dirty created
ãªãã¼ããã¦ã¿ã
snapshot-revertãµãã³ãã³ãã使ç¨ãã¦ä»®æ³ãã·ã³ããªãã¼ããããã¨ãã§ãã¾ãã --runnigãªãã·ã§ã³ãä»ãããã¨ã§ãä»®æ³ãã·ã³ãèµ·åç¶æ ã§ãªãã¼ãã§ãã¾ãã
$ # ç¾å¨ã®ã¹ãããã·ã§ãããªã¹ããç¢ºèª $ virsh snapshot-list win11-test Name Creation Time State ----------------------------------------------------- clean 2024-12-07 07:44:13 +0000 running dirty 2024-12-07 07:49:37 +0000 paused $ # virsh snapshot-revert "VMãã¡ã¤ã³å" "ãªãã¼ããããã¹ãããã·ã§ããå" --running $virsh snapshot-revert win11-test clean --running Domain snapshot clean reverted
ããã§ãªãã¼ããå®äºããã¯ãªã¼ã³ãªç¶æ
ã§ä»®æ³ãã·ã³ãèµ·åãããã¨ãã§ãã¾ããããã«ã¦ã§ã¢æ¤ä½ãå®è¡ãããã³ã«ä»¥ä¸ã®å¦çãè¡ããã¨ã§ãæ±æããããã£ã¹ã¯ã®ä¿åã¨ã¯ãªã¼ã³ãªç¶æ
ã§ã®èµ·åãç¹°ãè¿ãè¡ããã¨ãã§ãã¾ãã
1. ä»®æ³ãã·ã³ã®ä¸æåæ¢
2. å·®åãã£ã¹ã¯ãã¡ã¢ãªã¤ã¡ã¼ã¸ã®ä¿å
3. ãªãã¼ã
ã¹ãããã·ã§ããã®ä½æããªãã¼ãå¦çãè¡ãã¨ãQEMUã¯æ°ããå·®åãã£ã¹ã¯ãèªåçã«çæããããã«é¢é£ããæ å ±ãä»®æ³ãã·ã³ã®XMLå½¢å¼ã®è¨å®ãã¡ã¤ã«ï¼ä»åã®å ´åã¯/etc/libvirt/qemu/win11-test.xmlï¼ã«åæ ãã¾ãããã®å·®åãã£ã¹ã¯ã¯ã©ã³ãã ãªååã§ä¿åãããä¾ãã°ä»¥ä¸ã®ããã«ãªãã¾ãã
<!-- è¨å®ãã¡ã¤ã« ï¼ä¸é¨æç²ï¼ /etc/libvirt/qemu/win11-test.xml --> ... <devices> <emulator>/usr/bin/qemu-system-x86_64</emulator> <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/var/lib/libvirt/images/windows11-0712-clone.1733554667'/> <backingStore type='file'> <format type='qcow2'/> <source file='/var/lib/libvirt/images/windows11-0712-clone.qcow2'/> </backingStore> <target dev='sda' bus='sata'/> <address type='drive' controller='0' bus='0' target='0' unit='0'/> </disk> ...
ã¯ãªã¼ã³ãªç¶æ ã®ã¹ãããã·ã§ããï¼cleanï¼ã«ãªãã¼ããã¦è¨å®ãã¡ã¤ã«ã確èªããã¨ãä¸è¨ã®ããã«"/var/lib/libvirt/images/windows11-0712-clone.1733554667" ã¨ããå·®åãã£ã¹ã¯ãä½æããã¦ãããã¨ã確èªã§ãã¾ãããã¹ãããã·ã§ãããç¶ãã¦ä½æãã¦ããã¨ã"backingStore"ã¿ã°ãå ¥ãåã«ãªããé¢é£ããå·®åãã£ã¹ã¯ã®æ å ±ãè¨é²ããã¦ããã¾ã*11ãä»®æ³ãã·ã³ãèµ·åããéã«ã¯ãä¾åé¢ä¿ã«ãããã¹ã¦ã®å·®åãã£ã¹ã¯ãæ£ããå ´æã«åå¨ãã¦ãããã¨ãäºåã«ç¢ºèªãã¦ãããã¨ããå§ããã¾ãã
libvirt-python
ããã¾ã§virshã³ãã³ãã§ä»®æ³ãã·ã³ã®æä½ãè¡ã£ã¦ãã¾ããããlibvirtã®APIãPythonã§å©ç¨å¯è½ã«ãããã¤ã³ãã£ã³ã°ã§ããlibvirt-pythonã使ç¨ããã¨ãã¹ã¯ãªããã«ããä»®æ³ãã·ã³æä½ã®èªååãç°¡åã«è¡ãã¾ã*12ã
以ä¸ã®ã¹ã¯ãªããã¯ãä»åã®ã¹ãããã·ã§ããã®ä½æãè¡ãcreate_snapshot.pyã¨ãªãã¼ããè¡ãrevert.pyã®ä¾ã§ã
# create_snapshot.py # ã¹ãããã·ã§ãããä½æããã¹ã¯ãªãã import libvirt from xml.etree import ElementTree vm_name = "win11-test" snapshot_name = "dirty" memory_file = "/var/lib/libvirt/images/dirty.memory" disk_file = "/var/lib/libvirt/images/windows11-0712-clone.dirty" conn = libvirt.open("qemu:///system") if not conn: raise SystemExit("Failed to open connection to qemu:///system") dom = conn.lookupByName(vm_name) if not dom: raise SystemExit(f"Cannot find guest: {vm_name}") snapshot_xml = f""" <domainsnapshot> <name>{snapshot_name}</name> <memory file='{memory_file}' snapshot='external'/> <disks> <disk name='sda' snapshot='external' file='{disk_file}'/> </disks> </domainsnapshot> """ try: snapshot = dom.snapshotCreateXML(snapshot_xml, libvirt.VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC) if not snapshot: raise SystemExit("Failed to create snapshot") print(f"Snapshot '{snapshot_name}' created successfully.") except libvirt.libvirtError as e: print(f"Failed to create snapshot: {e}") finally: conn.close()
# revert.py # ãªãã¼ããè¡ãã¹ã¯ãªãã import libvirt vm_name = "win11-test" snapshot_name = "clean" conn = libvirt.open("qemu:///system") if not conn: raise SystemExit("Failed to open connection to qemu:///system") dom = conn.lookupByName(vm_name) if not dom: raise SystemExit(f"Cannot find guest: {vm_name}") try: snapshot = dom.snapshotLookupByName(snapshot_name, 0) if not snapshot: raise SystemExit(f"Cannot find snapshot: {snapshot_name}") dom.revertToSnapshot(snapshot, libvirt.VIR_DOMAIN_SNAPSHOT_REVERT_RUNNING) print(f"Reverted to snapshot '{snapshot_name}' and VM is now running.") except libvirt.libvirtError as e: print(f"Failed to revert to snapshot: {e}") finally: conn.close()
ãããã«
åãã¦QEMUãlibvirtã«è§¦ãã¾ããããæä½ã¯ã·ã³ãã«ã§ãè±å¯ãªãªãã¡ã¬ã³ã¹ãæã£ã¦ããããé常ã«åãçµã¿ãããå°è±¡ãåãã¾ããã
åºæ¬çã«ã¯virshã®ãªãã¡ã¬ã³ã¹ã«å¾ã£ã¦ã³ãã³ããå®è¡ããã ãã§æ¸ã¿ã¾ãããã¹ãããã·ã§ããã»å·®åãã£ã¹ã¯ã®ä½æãªã©ã§ã¯ãéç¨ã«é©ãããµãã³ãã³ãã使ç¨ããå¿
è¦ããããè¤éãªå
容ãå¤ãã£ãã§ãããã¨ã¦ãåå¼·ã«ãªãã¾ããã
æå¾ã«ãããã¾ã§ãèªã¿ããã ããããã¨ããããã¾ããã
ãã®è¨äºãå°ãã§ãä»®æ³å管çã®ç解ãæ·±ããå©ãã«ãªãã°å¹¸ãã§ãã
åèãªã³ã¯
*1:https://adventar.org/calendars/10492
*2:https://www.server-world.info/query?os=Ubuntu_24.04&p=kvm&f=1
*3:https://qiita.com/rxg03350/items/e76a6a858f6b9ac267b3
*4:https://www.qemu.org/docs/master/about/index.html
*7:https://www.libvirt.org/manpages/virsh.html
*8:https://wiki.libvirt.org/I_created_an_external_snapshot_but_libvirt_will_not_let_me_delete_or_revert_to_it.html
*9:https://unix.stackexchange.com/questions/663372/error-creating-snapshot-operation-not-supported-internal-snapshots-of-a-vm-wit
*10:https://docs.redhat.com/ja/documentation/red_hat_enterprise_linux/7/html/virtualization_deployment_and_administration_guide/sect-troubleshooting-workaround_for_creating_external_snapshots_with_libvirt#sect-Troubleshooting-Workaround_for_creating_external_snapshots_with_libvirt