VHD(Virtual Hard Disk)的解析(上篇)——PyVinil的使用

前言:

  最近接到一个新的任务,是关于虚拟化的中的虚拟磁盘文件(VHD)的。一开始的思路就是去寻找一些相关的源码工具,或是相关的库(C/Java/Python都ok的)。不好找,以下就是关于Python工具源码——PyVinil。版本为PyVinil-0.1.0.


概述:

  VHD 是Microsoft Virtual Hard Disk format(微软虚拟磁盘文件)的简称。可以由Microsoft Virtual PC2007,Windows Vista,Windows 7/8,Hyper-V,Windows Server 2008R2/2012,Microsoft Virtual Server 2005等创建,Virtual Box,VMWare等可以挂载使用。
  VHD文件格式可以被压缩成单个文件存放在宿主机器的文件系统上,主要包括虚拟机启动所需系统文件。


本文链接:http://blog.csdn.net/lemon_tree12138/article/details/49664873 -- 编程小笙
                                                                 --转载请注明出处


笔者环境:

1.系统环境:

  CentOS 6.5

  python 2.7.3  
  gcc version 4.4.7
  gcc-c++

2.依赖环境:

  CMake v2.8.3 # yum
  Check v0.9.8 # 下载安装包
  Doxygen 1.7.4 # yum

  libvinil # 下载安装包

  libuuid-devel


依赖环境的安装:

1.CMake v2.8.3

  # yum -y install cmake

2.Check v0.9.8

  # 下载安装包

  # cd package_home/

  # ./configure

  # make install

3.Doxygen 1.7.4

  # yum -y install doxygen

4.libuuid-devel

  # yum -y install libuuid-devel

5.libvinil

  # 下载安装包

  # cd package_home/

  # cmake . # 注意此处要有一个'.'

  # make & make install



PyVinil的使用

1.小试牛刀

  在我们的依赖环境都OK了之后,我们就可以下载PyVinil,并使用了。

  笔者这里的PyVinil使用的是v-0.1.0版本。安装过程如下:

  # python setup.py build

  # python setup.py install

  安装完成之后,我们可以进行如下操作:

  


2.代码测试

# encoding=utf-8

from pyvinil.vhd import VHD
from pyvinil import utils

def main():
    vhd = VHD.open("/root/upload/new.vhd")
    vhd.footer.disk_type = 3
    vhd.footer.uuid = utils.uuid_generate()

    print '-' * 50
    print "pointer: ", vhd.vhd_pointer.value

    print '-' * 50
    print "DLL: ", vhd.vinil_dll

    print '-' * 50
    print "Footer: ", vhd.footer
    print "disk_type: ", vhd.footer.disk_type
    print "real: ", vhd.footer.disk_type.real
    print "denominator: ", vhd.footer.disk_type.denominator
    print "imag: ", vhd.footer.disk_type.imag
    print "numerator: ", vhd.footer.disk_type.numerator
    print "bit_length: ", vhd.footer.disk_type.bit_length()
    print "UUID: ", get_string_Hex_nox(vhd.footer.uuid)
    print "COMMIT: ", vhd.commit_structural_changes()

    print '-' * 50
    print "TELL: ", vhd.tell()

    print '-' * 50
    vhd_read = vhd.read(1)
    print "READ ", vhd_read.title(), ":"
    print "TITLE : ", vhd_read.title()
    print "CAPITALIZE : ", vhd_read.capitalize()
    print "FORMAT : ", vhd_read.format()
    print "ISALNUM : ", vhd_read.isalnum()
    print "ISALPHA : ", vhd_read.isalpha()
    print "ISDIGIT : ", vhd_read.isdigit()
    print "ISLOWER : ", vhd_read.islower()
    print "ISSPACE : ", vhd_read.isspace()
    print "ISTITLE : ", vhd_read.istitle()
    print "ISUPPER : ", vhd_read.isupper()

    print '-' * 50
    print "TELL: ", vhd.tell()

    print '-' * 50
    vhd_read_2 = vhd.read(2)
    print "READ ", vhd_read_2.title(), ":"
    print "TITLE : ", vhd_read_2.title()
    print "CAPITALIZE : ", vhd_read_2.capitalize()
    print "FORMAT : ", vhd_read_2.format()
    print "ISALNUM : ", vhd_read_2.isalnum()
    print "ISALPHA : ", vhd_read_2.isalpha()
    print "ISDIGIT : ", vhd_read_2.isdigit()
    print "ISLOWER : ", vhd_read_2.islower()
    print "ISSPACE : ", vhd_read_2.isspace()
    print "ISTITLE : ", vhd_read_2.istitle()
    print "ISUPPER : ", vhd_read_2.isupper()

# 将一个可见字符串转成其对应的十六进制表示
def get_string_Hex_nox(substr):
    byte_list = bytearray(substr)
    hexStr = ''
    for item in byte_list:
        tmp = hex(item)[2:]
        if len(tmp) % 2 == 1:
            tmp = '0' + tmp
        hexStr += tmp
    return hexStr

if __name__ == '__main__':
    main()

3.测试结果

[root@localhost python]# python test_vhd.py
--------------------------------------------------
pointer:  31212112
--------------------------------------------------
DLL:  <CDLL 'libvinil.so', handle 1e1ac50 at 7fd287189490>
--------------------------------------------------
Footer:  <pyvinil.vhd.VHDFooterStructure object at 0x7fd2871d35f0>
disk_type:  3
real:  3
denominator:  1
imag:  0
numerator:  3
bit_length:  2
UUID:  dc88c8c382a94d06bd86d0aa898b02f6
COMMIT:  None
--------------------------------------------------
TELL:  0
--------------------------------------------------
READ  Conectix :
TITLE :  Conectix
CAPITALIZE :  Conectix
FORMAT :  conectix
ISALNUM :  True
ISALPHA :  True
ISDIGIT :  False
ISLOWER :  True
ISSPACE :  False
ISTITLE :  False
ISUPPER :  False
--------------------------------------------------
TELL:  1
--------------------------------------------------
READ  Cxsparse▒▒▒▒▒▒▒▒ :
TITLE :  Cxsparse▒▒▒▒▒▒▒▒
CAPITALIZE :  Cxsparse▒▒▒▒▒▒▒▒
FORMAT :  cxsparse▒▒▒▒▒▒▒▒
ISALNUM :  False
ISALPHA :  False
ISDIGIT :  False
ISLOWER :  True
ISSPACE :  False
ISTITLE :  False
ISUPPER :  False
[root@localhost python]#

小结:

  从以上的测试和结果来看,可以看到这个工具并没有提供给我们更多的内容。这些结果我们本就可以通过参考VHD结构文档手动去解析获得。

  不过从PyVinil-0.1.0的源码以及系统环境的配置过程来看,这里只是对相关的C的库函数的调用而已。所以,要想从这里获得更多的信息,可能已经走不通了。在后期的调研中,我们要做的事情是自己动手来解析VHD.或是通过一些磁盘挂载程序来挂载硬盘,再从挂载后的磁盘里读数据。(关于挂载vhd磁盘,其实也并非易事。下篇博客再作介绍吧。)

原文地址:https://www.cnblogs.com/fengju/p/6336037.html