将零写入文件块 [英] Write zeros to file blocks

查看:69
本文介绍了将零写入文件块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试识别与特定文件绑定的块,并向其写入零.我发现有几种方法可以对磁盘上的可用空间执行此操作,但是到目前为止,我还没有发现执行以下操作的任何建议:

I'm attempting to identify the blocks that are tied to a specific file and write zeros to them. I've found several methods that do this to the free space on a disk, but so far I haven't found any slid suggestions for doing the following:

  • 识别文件的块
  • 将零写入这些块.

此操作的目的是用于虚拟化系统.该系统具有对被标识为相同的块进行重复数据删除的能力.这用于减少驱动器上的来宾OS使用的空间.

The purpose of this is for a virtualized system. This system has the ability to dedupe blocks that are identified as being the same. This is used to reduce space used by the guest OSes on the drive.

当前,这是使用 dd 将零写入驱动器的可用空间来完成的.但是,这会对VMWare系统产生副作用,导致来宾OS驱动器使用已分配的所有磁盘空间,从系统上的那一刻起,所有字节均已写入.

Currently this is being done using dd to write zeros to the free space on the drive. However this has the side effect on VMWare systems to cause the guest OS drive to use the entire disk space it has been allocated as from that point on the system things all the bytes have been written to.

推荐答案

即使可以安全地修改已卸载的文件系统,编写代码也需要花费大量精力.除非没有其他选择,否则应避免这种情况.

Writing code that can safely modify even an unmounted filesystem will require significant effort. It is to be avoided unless there is no other option.

基本上,有两种选择可以使修改文件系统变得容易:

You basically have two choices to make modifying the filesystem easy:

  • 在虚拟环境中运行python .
  • 在主机上安装虚拟文件系统.大多数类似UNIX的系统都可以做到这一点,例如借助 FUSE (支持
  • Run python in the virtual environment.
  • Mount the virtualized filesystem on the host. Most UNIX-like systems can do that, e.g. with the help of FUSE (which support a lot of filesystem types) and loop devices.

这样,您可以使用(来宾或主机)操作系统的文件系统代码,而不必自行滚动.:-)如果可以使用这些选项之一,则下面列出的代码片段将用零填充文件:

This way you can use the (guest or host) OS's filesystem code instead of having to roll your own. :-) If you can use one of those options, the code fragment listed below will fill a file with zeroes:

import os

def overwrite(f):
    """Overwrite a file with zeroes.

    Arguments:
    f -- name of the file
    """
    stat = os.stat(f)
    with open(f, 'r+') as of:
        of.write('\0' * stat.st_size)
        of.flush()

这篇关于将零写入文件块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆