Posted on

Create a Sparse File

Let’s take allocating 7TB as an example,1

$ NAME='asr-btrfs'
$ dd if=/dev/zero of=${NAME}.img bs=1 count=0 seek=7T
0+0 records in
0+0 records out
0 bytes copied, 9.9685e-05 s, 0.0 kB/s

After this, with ls -alh you can see that the file size is 7TB, and with du asr-btrfs.img to see the actual size.

$ ls -alh
-rw-r--r--  1 root   root   7.0T Nov 10 10:15 asr-btrfs.img
$ du asr-btrfs.img
0       asr-btrfs.img

Create Disk Partition

$ mkfs.btrfs ${NAME}.img
btrfs-progs v5.4.1 
See http://btrfs.wiki.kernel.org for more information.

Label:              (null)
UUID:               c172f5e7-b188-4b9f-a35a-5e2a4eb17c69
Node size:          16384
Sector size:        4096
Filesystem size:    7.00TiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         DUP               1.00GiB
  System:           DUP               8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Checksum:           crc32c
Number of devices:  1
Devices:
   ID        SIZE  PATH
    1     7.00TiB  asr-btrfs.img

Mount the Partition

$ mount='/data'
$ mkdir ${mount}
$ mount ${NAME}.img ${mount}

Move the Data

Now with mv you can move the files to the new filesystem.

Docker Configuration

If the folder is used as a volume on Ubuntu/Debian Docker, it is supported, but you have to enable the btrfs storage driver.

On the host modify the /etc/docker/daemon.json file, adding one line to it:1

{
  "storage-driver": "btrfs"
}

Then restart the docker container you want to use btrfs.

$ docker restart ubuntu-lang

Disk Space Lookup

# 查看磁盘文件占用空间
du -h filesystem.img
# 查看btrfs元数据占用空间
btrfs filesystem df /mnt
# 也是查看空间
btrfs filesystem usage /mnt

Leave a Reply

Your email address will not be published. Required fields are marked *