xref: /OK3568_Linux_fs/kernel/Documentation/admin-guide/device-mapper/zero.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun=======
2*4882a593Smuzhiyundm-zero
3*4882a593Smuzhiyun=======
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunDevice-Mapper's "zero" target provides a block-device that always returns
6*4882a593Smuzhiyunzero'd data on reads and silently drops writes. This is similar behavior to
7*4882a593Smuzhiyun/dev/zero, but as a block-device instead of a character-device.
8*4882a593Smuzhiyun
9*4882a593SmuzhiyunDm-zero has no target-specific parameters.
10*4882a593Smuzhiyun
11*4882a593SmuzhiyunOne very interesting use of dm-zero is for creating "sparse" devices in
12*4882a593Smuzhiyunconjunction with dm-snapshot. A sparse device reports a device-size larger
13*4882a593Smuzhiyunthan the amount of actual storage space available for that device. A user can
14*4882a593Smuzhiyunwrite data anywhere within the sparse device and read it back like a normal
15*4882a593Smuzhiyundevice. Reads to previously unwritten areas will return a zero'd buffer. When
16*4882a593Smuzhiyunenough data has been written to fill up the actual storage space, the sparse
17*4882a593Smuzhiyundevice is deactivated. This can be very useful for testing device and
18*4882a593Smuzhiyunfilesystem limitations.
19*4882a593Smuzhiyun
20*4882a593SmuzhiyunTo create a sparse device, start by creating a dm-zero device that's the
21*4882a593Smuzhiyundesired size of the sparse device. For this example, we'll assume a 10TB
22*4882a593Smuzhiyunsparse device::
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun  TEN_TERABYTES=`expr 10 \* 1024 \* 1024 \* 1024 \* 2`   # 10 TB in sectors
25*4882a593Smuzhiyun  echo "0 $TEN_TERABYTES zero" | dmsetup create zero1
26*4882a593Smuzhiyun
27*4882a593SmuzhiyunThen create a snapshot of the zero device, using any available block-device as
28*4882a593Smuzhiyunthe COW device. The size of the COW device will determine the amount of real
29*4882a593Smuzhiyunspace available to the sparse device. For this example, we'll assume /dev/sdb1
30*4882a593Smuzhiyunis an available 10GB partition::
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun  echo "0 $TEN_TERABYTES snapshot /dev/mapper/zero1 /dev/sdb1 p 128" | \
33*4882a593Smuzhiyun     dmsetup create sparse1
34*4882a593Smuzhiyun
35*4882a593SmuzhiyunThis will create a 10TB sparse device called /dev/mapper/sparse1 that has
36*4882a593Smuzhiyun10GB of actual storage space available. If more than 10GB of data is written
37*4882a593Smuzhiyunto this device, it will start returning I/O errors.
38