当前位置:   article > 正文

Linux/C/C++将虚拟机磁盘文件挂载到本地文件系统_vdmount

vdmount

vdfmount.h

#ifndef VDFMOUNT_H
#define VDFMOUNT_H

#include <sys/mount.h>
#include "public.h"
#include "WidgetConst.h"
#include "Logger.h"

using namespace LogFile;

class vdfMount
{
   
public:
    vdfMount()
    {
   
        m_isMount = -1;
        m_imagePath = "";
        m_mapExtType[".vmdk"] = "VMDK";
        m_mapExtType[".vdi"] = "VDI";
        m_mapExtType[".vhd"] = "VHD";
    }
    ~vdfMount();

    /* Added for 虚拟文件系统检查 by chenkun 2020/10/27 below */
    // TODO:注意:逻辑分区不能挂载
    bool CheckVDMount(const string& fPath);
    bool isContainLoop(std::vector<string>& vecDevLoop);
    bool isContainFs(const std::string& str);// 判断某文件系统是否存在
    bool umountAll();
    /* Added for 虚拟文件系统检查 by chenkun 2020/10/27 above */

public:
    int m_isMount;                                   // 挂载状态: -1-未知, 0-已经挂载, 1-未挂载
    std::string m_imagePath;                         // 挂载的镜像文件路径
    std::map<std::string, std::string> m_mapExtType; // 文件后缀-vdfuseType
    std::vector<std::string> m_vecBasicPartition;    // 虚拟机文件挂载点
    std::vector<std::string> m_vecDevLoop;           // loop分区
};

#endif // VDFMOUNT_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

vdfmount.cpp

#include "vdfmount.h"


bool vdfMount::CheckVDMount(const string& fPath)
{
   
    if (fPath.empty())
    {
   
        return false;
    }
    m_vecBasicPartition.clear();
    m_vecDevLoop.clear();
    string tmpPath = CDir::getFullPath(fPath.c_str());
    // 此函数控制是否挂载成功,不成功则要求客户端重新输入
    // 判断文件是否存在
    if (0 != access(tmpPath.c_str(), F_OK))
    {
   
        LOG_INFO("fuse fPath(not found)==%s", fPath.c_str());
        return false;
    }

    // 根据后缀名判断属于哪种虚拟机文件
    // (VDI, VMDK, VHD)
    //fs::path fp = fs::absolute(fs::path(tmpPath));
    //string ext = fp.extension().string();
    std::size_t pos;
    std::string ext;
    if((pos = tmpPath.rfind(".")) != std::string::npos)
    {
   
        ext = tmpPath.substr(pos);
    }

    auto iter = m_mapExtType.find(ext);
    if (iter == m_mapExtType.end())
    {
   
        LOG_INFO("fuse fPath(type error)==%s", fPath.c_str());
        return false;
    }
    string Type = iter->second;

    // 组合命令产生raw文件
    // ./vdfuse -tVMDK -w -f "/disk3/fd/fd.vmdk" "/tmp/test2"
    if ((false == MKDIR(TmpVDRawPartition.c_str(), false)) ||(false == MKDIR(TmpVDBasicPartition.c_str(), false)))
    {
   
        return false
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/314366
推荐阅读
相关标签
  

闽ICP备14008679号