在libvirt中,可以使用virDomainDestroy方法来强制关机虚拟机。

以下是使用libvirt-go库中的示例代码:

package main

import (
    "fmt"
    "github.com/libvirt/libvirt-go"
)

func main() {
    conn, err := libvirt.NewConnect("qemu:///system")
    if err != nil {
        fmt.Println("Failed to connect to the hypervisor")
        return
    }
    defer conn.Close()

    domain, err := conn.LookupDomainByName("your_domain_name")
    if err != nil {
        fmt.Println("Failed to find the domain")
        return
    }
    defer domain.Free()

    err = domain.Destroy()
    if err != nil {
        fmt.Println("Failed to force shutdown the domain")
        return
    }

    fmt.Println("Domain has been forcefully shutdown")
}

请注意,your_domain_name应替换为要强制关机的虚拟机的名称


原文地址: https://www.cveoy.top/t/topic/hxXy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录