8
9

More than 5 years have passed since last update.

すぱ〜す ファイル

Last updated at Posted at 2016-02-03

ファイル作成

通常ファイル
# dd if=/dev/zero of=file bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.0102638 s, 1.0 GB/s
スパースファイル
# dd if=/dev/zero of=file_sparse bs=1M seek=10 count=0
0+0 records in
0+0 records out
0 bytes (0 B) copied, 0.000165579 s, 0.0 kB/s

OR

# truncate -s 10M file_sparse

サイズ確認

普通に見てみる

(論理的なファイル容量)
どちらもサイズ 10M

# ls -l
total 10240K
-rw-r--r-- 1 root root 10485760 Feb  3 15:44 file
-rw-r--r-- 1 root root 10485760 Feb  3 15:44 file_sparse
size option 付きで見てみる

(物理的なファイル容量)
スパースファイルのサイズは 0

# ls -l -s
total 10240K
10240K -rw-r--r-- 1 root root 10485760 Feb  3 15:44 file
    0K -rw-r--r-- 1 root root 10485760 Feb  3 15:44 file_sparse

コピーするとどうなる?

man cp
--sparse= ** WHEN **

control creation of sparse files. See below

  • By default, sparse SOURCE files are detected by a crude heuristic and the corresponding DEST file is made sparse as well. That is the behavior selected by --sparse=auto.
  • Specify --sparse=always to create a sparse DEST file whenever the SOURCE file contains a long enough sequence of zero bytes.
  • Use --sparse=never to inhibit creation of sparse files.
WHEN Description
auto(default) 入力ファイルがスパース -> 出力ファイルもスパース
always 常に出力ファイルをスパース
never 常に出力ファイルをスパースにしない

cp コマンドのそれぞれの option でコピーしてみた

通常ファイル
# cp file file_auto
# cp --sparse=always file file_always
# cp --sparse=never file file_never
スパースファイル
# cp file_sparse file_sparse_auto
# cp --sparse=always file_sparse file_sparse_always
# cp --sparse=never file_sparse file_sparse_never

サイズ確認

通常ファイル

(物理的なファイル容量)
sparse=always の場合、コピー先はスパースファイルに

# ls -l -s --ignore="*_sparse*"
total 30720K
10240K -rw-r--r-- 1 root root 10485760 Feb  3 15:44 file
    0K -rw-r--r-- 1 root root 10485760 Feb  3 15:49 file_always
10240K -rw-r--r-- 1 root root 10485760 Feb  3 15:49 file_auto
10240K -rw-r--r-- 1 root root 10485760 Feb  3 15:49 file_never
スパースファイル

(物理的なファイル容量)
sparse=never の場合、コピー先は通常ファイルに

# ls -l -s file_sparse*
    0K -rw-r--r-- 1 root root 10485760 Feb  3 15:44 file_sparse
    0K -rw-r--r-- 1 root root 10485760 Feb  3 15:50 file_sparse_always
    0K -rw-r--r-- 1 root root 10485760 Feb  3 15:50 file_sparse_auto
10240K -rw-r--r-- 1 root root 10485760 Feb  3 15:50 file_sparse_never

用途

  • コアダンプ
  • シンプロビジョニング vmdk ファイル
  • Xen, KVM 仮想マシンイメージ

例えば

KVM 仮想マシンイメージ
# qemu-img create -f raw qemu_test.img 10M
Formatting 'qemu_test.img', fmt=raw size=10485760

# qemu-img info qemu_test.img
image: qemu_test.img
file format: raw
virtual size: 10M (10485760 bytes)
disk size: 0

# ls -lh qemu_test.img
-rw-r--r-- 1 root root 10M Feb  4 10:05 qemu_test.img

# ls -lhs qemu_test.img
0 -rw-r--r-- 1 root root 10M Feb  4 10:05 qemu_test.img

# du -s qemu_test.img
0K  qemu_test.img
  • 指定サイズではなく、実際に使用している領域分のみ確保
  • 実際にデータを書き込むと、それにつれてファイルサイズが増加。ファイルサイズを大幅に節約
  • 逆にデメリットとして、ファイルサイズの拡張時にオーバーヘッド発生

Reference

https://en.wikipedia.org/wiki/Sparse_file

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9