元のリーンファイルが後で拡張された場合、再びリーンになることはできますか?

元のリーンファイルが後で拡張された場合、再びリーンになることはできますか?

私は、スパースファイルを理解するユーティリティを使用せずに元のスパースファイルの内容をコピーまたは転送すると、「穴」がいっぱいになることがわかります。スパースファイルをスパースファイルに復元する方法やユーティリティはありますか?

例:
スパースファイルを生成します。

% dd if=/dev/zero of=TEST bs=1 count=0 seek=1G
# do some op that pads out the holes
% scp TEST localhost:~/TEST2
% ls -lhs TEST*
   0 -rw-rw-r--. 1 tony tony 1.0G Oct 16 13:35 TEST
1.1G -rw-rw-r--. 1 tony tony 1.0G Oct 16 13:37 TEST2

次の方法はありますか?

% resparse TEST2
to get:
   0 -rw-rw-r--. 1 tony tony 1.0G Oct 16 13:35 TEST
  0G -rw-rw-r--. 1 tony tony 1.0G Oct 16 13:37 TEST2

ベストアンサー1

2015年に編集

util-linux 2.25以降、fallocateLinuxのユーティリティには-d/--dig-holeオプションがあります。

fallocate -d the-file

塗りつぶされたブロックごとに穴が掘られます。ゼロファイルに


以前のシステムでは手動で実行できます。

Linuxにはこれを行うFALLOC_FL_PUNCH_HOLEオプションがありますfallocate。例を含むスクリプトをgithubで見つけました。

PythonでFALLOC_FL_PUNCH_HOLEを使用する

お客様の要件に合わせて少し修正しました。 0で埋められたファイル領域に穴を作成しました。ここにいる:

PythonでFALLOC_FL_PUNCH_HOLEを使用してファイルに穴を開けます。

usage: punch.py [-h] [-v VERBOSE] FILE [FILE ...]

Punch out the empty areas in a file, making it sparse

positional arguments:
  FILE                  file(s) to modify in-place

optional arguments:
  -h, --help            show this help message and exit
  -v VERBOSE, --verbose VERBOSE
                        be verbose

例:

# create a file with some data, a hole, and some more data
$ dd if=/dev/urandom of=test1 bs=4096 count=1 seek=0
$ dd if=/dev/urandom of=test1 bs=4096 count=1 seek=2

# see that it has holes
$ du --block-size=1 --apparent-size test1
12288   test1
$ du --block-size=1 test1
8192    test1

# copy it, ignoring the hole
$ cat test1 > test2
$ du --block-size=1 --apparent-size test2
12288   test2
$ du --block-size=1 test2
12288    test2

# punch holes again
$ ./punch.py test2
$ du --block-size=1 --apparent-size test2
12288   test2
$ du --block-size=1 test2
8192    test2

# verify
$ cmp test1 test2 && echo "files are the same"
files are the same

パンクチャリングする 4096 バイトのブロックのみを検索するため、punch.pyファイルの起動時だけリーンにならない場合があります。もちろん、よりスマートになることもあります。返品、軽くテストした結果だから、注意してください。サポート信じる前に!

おすすめ記事