ZFS thin provisioning / sparse ZVOL

3 minute read

背景

zfs支持在zpool基础上创建块设备或volume.  
这个volume同样拥有快照等zfs的功能.  
正常情况下创建zvol时, -V指定的空间会直接从zpool中取出, 确保这个zvol可以使用指定的空间, 而不会导致空间溢出的问题.  
[root@db- zp1]# df -h  
Filesystem      Size  Used Avail Use% Mounted on  
/dev/sda1        32G  3.7G   27G  13% /  
tmpfs            16G     0   16G   0% /dev/shm  
/dev/sda3       236G  188M  223G   1% /opt  
zp1              29T  128K   29T   1% /zp1  
[root@db- zp1]# zfs create -V 10TB zp1/vol1  
[root@db- zp1]# df -h  
Filesystem      Size  Used Avail Use% Mounted on  
/dev/sda1        32G  3.7G   27G  13% /  
tmpfs            16G     0   16G   0% /dev/shm  
/dev/sda3       236G  188M  223G   1% /opt  
zp1              19T  128K   19T   1% /zp1  
[root@db- zp1]# ll /dev/zvol/zp1/vol1   
lrwxrwxrwx 1 root root 9 Jun 17 14:06 /dev/zvol/zp1/vol1 -> ../../zd0  
[root@db- zp1]# ll /dev/zd0  
brw-rw---- 1 root disk 230, 0 Jun 17 14:06 /dev/zd0  
  
使用thin zvol的话, 甚至可以创建超出zpool大小的zvol, 有点类似EMC存储的thin provisioning卷  
例如  
[root@db- zp1]# zfs create -s -V 30TB zp1/vol2  
[root@db- zp1]# zfs create -s -V 300TB zp1/vol3  
[root@db- zp1]# df -h  
Filesystem      Size  Used Avail Use% Mounted on  
/dev/sda1        32G  3.7G   27G  13% /  
tmpfs            16G     0   16G   0% /dev/shm  
/dev/sda3       236G  188M  223G   1% /opt  
zp1              19T  128K   19T   1% /zp1  
非thin zvol不允许空间超出zpool的剩余空间.  
[root@db- zp1]# zfs create -V 300TB zp1/vol4  
cannot create 'zp1/vol4': out of space  
[root@db-192-168-173-219 zp1]# zfs create -V 20TB zp1/vol4  
cannot create 'zp1/vol4': out of space  
  
[root@db- zp1]# ll /dev/zd*  
brw-rw---- 1 root disk 230,  0 Jun 17 14:06 /dev/zd0  
brw-rw---- 1 root disk 230, 16 Jun 17 14:08 /dev/zd16  
brw-rw---- 1 root disk 230, 32 Jun 17 14:08 /dev/zd32  
[root@db- zp1]# ll /dev/zvol/zp1/vol*  
lrwxrwxrwx 1 root root  9 Jun 17 14:06 /dev/zvol/zp1/vol1 -> ../../zd0  
lrwxrwxrwx 1 root root 10 Jun 17 14:08 /dev/zvol/zp1/vol2 -> ../../zd16  
lrwxrwxrwx 1 root root 10 Jun 17 14:08 /dev/zvol/zp1/vol3 -> ../../zd32  

参考

1. man zfs

       zfs create [-ps] [-b blocksize] [-o property=value] ... -V size volume  
  
           Creates  a volume of the given size. The volume is exported as a block device in /dev/zvol/path, where path  
           is the name of the volume in the ZFS namespace. The size represents the logical size  as  exported  by  the  
           device. By default, a reservation of equal size is created.  
  
           size is automatically rounded up to the nearest 128 Kbytes to ensure that the volume has an integral number  
           of blocks regardless of blocksize.  
  
           -p  
  
               Creates all the non-existing parent datasets. Datasets created in this manner are automatically mounted  
               according to the mountpoint property inherited from their parent. Any property specified on the command  
               line using the -o option is ignored. If the target filesystem already exists, the  operation  completes  
               successfully.  
  
           -s  
  
               Creates  a  sparse  volume  with  no reservation. See volsize in the Native Properties section for more  
               information about sparse volumes.  
  
           -o property=value  
  
               Sets the specified property as if the zfs set property=value command was invoked at the same  time  the  
               dataset  was  created.  Any editable ZFS property can also be set at creation time. Multiple -o options  
               can be specified. An error results if the same property is specified in multiple -o options.  
  
           -b blocksize  
  
               Equivalent to -o volblocksize=blocksize. If this option is specified in conjunction with  -o  volblock-  
               size, the resulting behavior is undefined.  
  
       volsize=size  
  
           For volumes, specifies the logical size of the volume. By default, creating a volume establishes a reserva-  
           tion of equal size. For storage pools with a version number  of  9  or  higher,  a  refreservation  is  set  
           instead.  Any  changes  to volsize are reflected in an equivalent change to the reservation (or refreserva-  
           tion). The volsize can only be set to a multiple of volblocksize, and cannot be zero.  
  
           The reservation is kept equal to the volume’s logical size to prevent unexpected  behavior  for  consumers.  
           Without the reservation, the volume could run out of space, resulting in undefined behavior or data corrup-  
           tion, depending on how the volume is used. These effects can also occur when the  volume  size  is  changed  
           while  it  is in use (particularly when shrinking the size). Extreme care should be used when adjusting the  
           volume size.  
  
           Though not recommended, a "sparse volume" (also known as "thin provisioning") can be created by  specifying  
           the  -s  option to the zfs create -V command, or by changing the reservation after the volume has been cre-  
           ated. A "sparse volume" is a volume where the reservation is  less  then  the  volume  size.  Consequently,  
           writes  to a sparse volume can fail with ENOSPC when the pool is low on space. For a sparse volume, changes  
           to volsize are not reflected in the reservation.  

Flag Counter

digoal’s 大量PostgreSQL文章入口