通过 libguestfs 在 gluster 卷上附加虚拟机磁盘(错误 "No operating system found")

Attach virtual machime disk on gluster volume via libguestfs (Error "No operating system found")

Debian 10 有 3 个计算节点。每个节点都用作基于 QEMU/KVM 的管理程序。

Libvirt0:amd64 5.0.0-4+deb10u1

Libguestfs0:amd 1:1.40.2-2

我使用 GlusterFS 卷上的磁盘创建虚拟机。

<disk type='network' device='disk'>
      <driver name='qemu' type='qcow2' cache='none' io='threads' discard='unmap'/>
      <source protocol='gluster' name='TEST/TEST1'>
        <host name='localhost' port='24007'/>
      </source>
      <target dev='vda' bus='virtio'/>
      <iotune>
        <read_bytes_sec>157286400</read_bytes_sec>
        <write_bytes_sec>104857600</write_bytes_sec>
        <read_iops_sec>40000</read_iops_sec>
        <write_iops_sec>25000</write_iops_sec>
      </iotune>
      <alias name='virtio-disk0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
    </disk>

我想使用 libguestfs 的来宾映像。

使用 guestfish(所有权利!):

><fs> add TEST/TEST1 protocol:gluster server:localhost:24007
><fs> run
><fs> list-filesystems
/dev/sda1: ext4
/dev/sda5: swap

使用 libguestfs 库 (C++) 导致错误“找不到操作系统” 我的代码:

#include <iostream>
#include <libvirt/libvirt.h>
#include <libguestfs/guestfs.h>


void main()
{
    auto handle_ = guestfs_create();
    if (!handle_) {
        std::cout << "Connection failed" << std::endl;
        return;
    }
    auto protocol = "gluster";
    auto host = "localhost:24007";
    auto name = "TEST/TEST1";
    const char* server[2] = { host.c_str(), NULL };

    auto res = guestfs_add_drive_opts(handle_, name,
        GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
        GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
        GUESTFS_ADD_DRIVE_OPTS_PROTOCOL, protocol,
        GUESTFS_ADD_DRIVE_OPTS_SERVER, server,
        -1);
    if (res < 0) {
        std::cout << "Add drive failed" << std::endl;
        return;
    }

    if (guestfs_launch(handle_) < 0) {
        std::cout << "Launch failed" << std::endl;
        return;
    }

    auto filesystems = guestfs_list_filesystems(handle_);

    if (!filesystems) {
        std::cout << "Failsystem failed" << std::endl;
        return;
    }

    if (!filesystems[0]) {
        std::cout << "No failsystem found" << std::endl;
        return;
    }
    auto i = 0;
    while (true) {
        if (!filesystems[i]) {
            std::cout << "No more fs" << std::endl;
            break;
        }
        std::cout << "filesystem: " << filesystems[i] << " -- " << filesystems[i + 1] << std::endl;
        i += 2;
    }

    auto roots_ = guestfs_inspect_os(handle_);
    if (!roots_) {
        std::cout << "Couldn't get root" << std::endl;
        return;
    }
    if (!roots_[0]) {
        std::cout << "No operating system found" << std::endl;
        return;
    }
}

如何使用libguetfs库正确挂载虚拟机磁盘?

本例中虚拟机磁盘格式为qcow2。 默认参数 GUESTFS_ADD_DRIVE_OPTS_FORMAT 是“qcow”,所以使用 guestfish 没有任何错误。 在 C++ 代码中,您应该将 GUESTFS_ADD_DRIVE_OPTS_FORMAT 设置为适当的值。