Skip to content

Instantly share code, notes, and snippets.

@mazgi
Last active December 23, 2017 05:09
Show Gist options
  • Save mazgi/d7317aa1959ca4434ab1 to your computer and use it in GitHub Desktop.
Save mazgi/d7317aa1959ca4434ab1 to your computer and use it in GitHub Desktop.
Btrfs subvolume quota

refs:

@mazgi qgroupというものがあってできます。snapshotをとったりする関係上普通のquotaだとうまく動かないんですよね。あるsubvolからfileを消してもsnapshotには残っているので結局容量へらないとかいうことがあるので

— 和装のGentoo人 (@naota344) August 21, 2014
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
[root@btrfs-testdrive] # uname -a
Linux btrfs-testdrive 3.14.14-gentoo #1 SMP Wed Aug 6 13:51:55 JST 2014 x86_64 Intel(R) Xeon(R) CPU X5675 @ 3.07GHz GenuineIntel GNU/Linux
[root@btrfs-testdrive] # btrfs version
Btrfs v3.12
[root@btrfs-testdrive] # mount | grep /mnt/btrfs
/dev/vda3 on /mnt/btrfs type btrfs (rw)
[root@btrfs-testdrive] # btrfs subvolume list /mnt/btrfs
ID 256 gen 8917 top level 5 path gentoo
ID 258 gen 8314 top level 5 path usr-portage
ID 259 gen 8911 top level 5 path var
ID 260 gen 8913 top level 5 path var-log
[root@btrfs-testdrive] # btrfs qgroup create 1/100 /mnt/btrfs
[root@btrfs-testdrive] # btrfs qgroup assign 0/276 1/100 /mnt/btrfs
[root@btrfs-testdrive] # btrfs qgroup show /mnt/btrfs
qgroupid rfer       excl       
-------- ----       ----       
0/5      16384      16384      
0/256    2864136192 2864136192 
0/258    833703936  833703936  
0/259    52510720   52510720   
0/260    733184     733184     
0/270    360448     360448     
0/271    3407872    3407872    
0/272    364544     364544     
0/273    3330048    3330048    
0/274    0          0          
0/275    16384      16384      
0/276    16384      16384      
0/277    16384      16384      
1/100    0          0          
[root@btrfs-testdrive] # btrfs qgroup show /mnt/btrfs -repc
qgroupid rfer       excl       max_rfer max_excl  parent  child 
-------- ----       ----       -------- --------  ------  ----- 
0/5      16384      16384      0        0         ---     ---  
0/256    2864136192 2864136192 0        0         ---     ---  
0/258    833703936  833703936  0        0         ---     ---  
0/259    52510720   52510720   0        0         ---     ---  
0/260    733184     733184     0        0         ---     ---  
0/270    360448     360448     0        10485760  ---     ---  
0/271    3407872    3407872    0        104857600 ---     ---  
0/272    364544     364544     0        10485760  ---     ---  
0/273    3330048    3330048    0        104857600 ---     ---  
0/274    0          0          0        0         ---     ---  
0/275    16384      16384      0        0         ---     ---  
0/276    16384      16384      0        0         1/100   ---  
0/277    16384      16384      0        0         ---     ---  
1/100    0          0          0        0         ---     0/276
[root@btrfs-testdrive] # btrfs qgroup limit -e 200m 1/200 /mnt/btrfs
[root@btrfs-testdrive] # btrfs qgroup limit 10m 1/100 /mnt/btrfs
[root@btrfs-testdrive] # btrfs qgroup show /mnt/btrfs -repc
qgroupid rfer       excl       max_rfer max_excl  parent      child       
-------- ----       ----       -------- --------  ------      -----       
0/5      16384      16384      0        0         ---         ---        
0/256    2864140288 2864140288 0        0         ---         ---        
0/258    833703936  833703936  0        0         ---         ---        
0/259    52510720   52510720   0        0         ---         ---        
0/260    733184     733184     0        0         ---         ---        
0/270    360448     360448     0        10485760  ---         ---        
0/271    3407872    3407872    0        104857600 ---         ---        
0/272    364544     364544     0        10485760  ---         ---        
0/273    3330048    3330048    0        104857600 ---         ---        
0/274    0          0          0        0         ---         ---        
0/275    16384      16384      0        0         ---         ---        
0/276    16384      16384      0        0         1/100,1/200 ---        
0/277    16384      16384      0        0         1/200       ---        
1/100    0          0          10485760 0         ---         0/276      
1/200    0          0          0        209715200 ---         0/276,0/277
[root@btrfs-testdrive] # for i in {00..24}
do              
btrfs quota rescan /mnt/btrfs -w > /dev/null
sleep 30
dd if=/dev/zero of=/mnt/btrfs/parent/child01/zero.${i}.img bs=10k count=2048
done
dd: error writing ‘/mnt/btrfs/parent/child01/zero.00.img’: Disk quota exceeded
956+0 records in
955+0 records out
9779200 bytes (9.8 MB) copied, 0.0117364 s, 833 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.01.img’: Disk quota exceeded
926+0 records in
925+0 records out
9472000 bytes (9.5 MB) copied, 0.0102403 s, 925 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.02.img’: Disk quota exceeded
891+0 records in
890+0 records out
9113600 bytes (9.1 MB) copied, 0.0102027 s, 893 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.03.img’: Disk quota exceeded
860+0 records in
859+0 records out
8796160 bytes (8.8 MB) copied, 0.0103407 s, 851 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.04.img’: Disk quota exceeded
829+0 records in
828+0 records out
8478720 bytes (8.5 MB) copied, 0.00930198 s, 911 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.05.img’: Disk quota exceeded
798+0 records in
797+0 records out
8161280 bytes (8.2 MB) copied, 0.00901611 s, 905 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.06.img’: Disk quota exceeded
768+0 records in
767+0 records out
7854080 bytes (7.9 MB) copied, 0.00886019 s, 886 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.07.img’: Disk quota exceeded
741+0 records in
740+0 records out
7577600 bytes (7.6 MB) copied, 0.00890008 s, 851 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.08.img’: Disk quota exceeded
715+0 records in
714+0 records out
7311360 bytes (7.3 MB) copied, 0.00810682 s, 902 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.09.img’: Disk quota exceeded
689+0 records in
688+0 records out
7045120 bytes (7.0 MB) copied, 0.00811421 s, 868 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.10.img’: Disk quota exceeded
663+0 records in
662+0 records out
6778880 bytes (6.8 MB) copied, 0.00780635 s, 868 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.11.img’: Disk quota exceeded
639+0 records in
638+0 records out
6533120 bytes (6.5 MB) copied, 0.00752098 s, 869 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.12.img’: Disk quota exceeded
616+0 records in
615+0 records out
6297600 bytes (6.3 MB) copied, 0.00743855 s, 847 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.13.img’: Disk quota exceeded
593+0 records in
592+0 records out
6062080 bytes (6.1 MB) copied, 0.00669214 s, 906 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.14.img’: Disk quota exceeded
569+0 records in
568+0 records out
5816320 bytes (5.8 MB) copied, 0.00893952 s, 651 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.15.img’: Disk quota exceeded
548+0 records in
547+0 records out
5601280 bytes (5.6 MB) copied, 0.00625001 s, 896 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.16.img’: Disk quota exceeded
528+0 records in
527+0 records out
5396480 bytes (5.4 MB) copied, 0.00598858 s, 901 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.17.img’: Disk quota exceeded
539+0 records in
538+0 records out
5509120 bytes (5.5 MB) copied, 0.00727839 s, 757 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.18.img’: Disk quota exceeded
81+0 records in
80+0 records out
819200 bytes (819 kB) copied, 0.00111382 s, 735 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.19.img’: Disk quota exceeded
78+0 records in
77+0 records out
788480 bytes (788 kB) copied, 0.00101193 s, 779 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.20.img’: Disk quota exceeded
72+0 records in
71+0 records out
727040 bytes (727 kB) copied, 0.00119237 s, 610 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.21.img’: Disk quota exceeded
65+0 records in
64+0 records out
655360 bytes (655 kB) copied, 0.00100051 s, 655 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.22.img’: Disk quota exceeded
60+0 records in
59+0 records out
604160 bytes (604 kB) copied, 0.000885687 s, 682 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.23.img’: Disk quota exceeded
55+0 records in
54+0 records out
552960 bytes (553 kB) copied, 0.000874137 s, 633 MB/s
dd: error writing ‘/mnt/btrfs/parent/child01/zero.24.img’: Disk quota exceeded
50+0 records in
49+0 records out
501760 bytes (502 kB) copied, 0.000816612 s, 614 MB/s
[root@btrfs-testdrive] # ls -lh /mnt/btrfs/parent/child01
total 130M
-rw-r--r-- 1 root root 9.4M Aug 21 23:55 zero.00.img
-rw-r--r-- 1 root root 9.1M Aug 21 23:55 zero.01.img
-rw-r--r-- 1 root root 8.7M Aug 21 23:56 zero.02.img
-rw-r--r-- 1 root root 8.4M Aug 21 23:56 zero.03.img
-rw-r--r-- 1 root root 8.1M Aug 21 23:57 zero.04.img
-rw-r--r-- 1 root root 7.8M Aug 21 23:57 zero.05.img
-rw-r--r-- 1 root root 7.5M Aug 21 23:58 zero.06.img
-rw-r--r-- 1 root root 7.3M Aug 21 23:58 zero.07.img
-rw-r--r-- 1 root root 7.0M Aug 21 23:59 zero.08.img
-rw-r--r-- 1 root root 6.8M Aug 21 23:59 zero.09.img
-rw-r--r-- 1 root root 6.5M Aug 22 00:00 zero.10.img
-rw-r--r-- 1 root root 6.3M Aug 22 00:00 zero.11.img
-rw-r--r-- 1 root root 6.1M Aug 22 00:01 zero.12.img
-rw-r--r-- 1 root root 5.8M Aug 22 00:01 zero.13.img
-rw-r--r-- 1 root root 5.6M Aug 22 00:02 zero.14.img
-rw-r--r-- 1 root root 5.4M Aug 22 00:03 zero.15.img
-rw-r--r-- 1 root root 5.2M Aug 22 00:03 zero.16.img
-rw-r--r-- 1 root root 5.3M Aug 22 00:04 zero.17.img
-rw-r--r-- 1 root root 800K Aug 22 00:04 zero.18.img
-rw-r--r-- 1 root root 770K Aug 22 00:05 zero.19.img
-rw-r--r-- 1 root root 710K Aug 22 00:05 zero.20.img
-rw-r--r-- 1 root root 640K Aug 22 00:06 zero.21.img
-rw-r--r-- 1 root root 590K Aug 22 00:06 zero.22.img
-rw-r--r-- 1 root root 540K Aug 22 00:07 zero.23.img
-rw-r--r-- 1 root root 490K Aug 22 00:07 zero.24.img
[root@btrfs-testdrive] # for limit in {10,100}m_{10,100}m
do
btrfs subvolume create /mnt/btrfs/subvol_${limit} 
done
Create subvolume '/mnt/btrfs/subvol_10m_10m'
Create subvolume '/mnt/btrfs/subvol_10m_100m'
Create subvolume '/mnt/btrfs/subvol_100m_10m'
Create subvolume '/mnt/btrfs/subvol_100m_100m'
[root@btrfs-testdrive] # for limit in {10,100}m_{10,100}m
do
mkdir -p /mnt/subvol_${limit}
mount -t btrfs -o subvol=subvol_${limit},defaults,autodefrag /dev/vda3 /mnt/subvol_${limit}
done
[root@btrfs-testdrive] # mount | grep /mnt/subvol_
/dev/vda3 on /mnt/subvol_10m_10m type btrfs (rw,subvol=subvol_10m_10m,autodefrag)
/dev/vda3 on /mnt/subvol_10m_100m type btrfs (rw,subvol=subvol_10m_100m,autodefrag)
/dev/vda3 on /mnt/subvol_100m_10m type btrfs (rw,subvol=subvol_100m_10m,autodefrag)
/dev/vda3 on /mnt/subvol_100m_100m type btrfs (rw,subvol=subvol_100m_100m,autodefrag)
[root@btrfs-testdrive] # btrfs subvolume list /mnt/btrfs
ID 256 gen 9105 top level 5 path gentoo
ID 258 gen 8970 top level 5 path usr-portage
ID 259 gen 8974 top level 5 path var
ID 260 gen 9036 top level 5 path var-log
ID 270 gen 9097 top level 5 path subvol_10m_10m
ID 271 gen 9098 top level 5 path subvol_10m_100m
ID 272 gen 9099 top level 5 path subvol_100m_10m
ID 273 gen 9100 top level 5 path subvol_100m_100m
[root@btrfs-testdrive] # btrfs qgroup show /mnt/btrfs -re
qgroupid rfer       excl       max_rfer max_excl 
-------- ----       ----       -------- -------- 
0/5      16384      16384      0        0        
0/256    2864132096 2864132096 0        0        
0/258    833703936  833703936  0        0        
0/259    52510720   52510720   0        0        
0/260    733184     733184     0        0        
0/270    16384      16384      0        0        
0/271    16384      16384      0        0        
0/272    16384      16384      0        0        
0/273    16384      16384      0        0        

???

[root@btrfs-testdrive] # while read x id x gen x x level x subvol
do
rfer=$(echo $subvol | cut -d'_' -f2)
excl=$(echo $subvol | cut -d'_' -f3)
cmd_rfer=$(printf "btrfs qgroup limit %s %d /mnt/%s" $rfer $id $subvol)
cmd_excl=$(printf "btrfs qgroup limit -e %s %d /mnt/%s" $excl $id $subvol)
echo "rfer:$cmd_rfer"
eval $cmd_rfer
echo "excl:$cmd_excl"
eval $cmd_excl
done < <(btrfs subvolume list /mnt/btrfs | grep -E 'subvol_[0-9m_]+$')
rfer:btrfs qgroup limit 10m 270 /mnt/subvol_10m_10m
excl:btrfs qgroup limit -e 10m 270 /mnt/subvol_10m_10m
rfer:btrfs qgroup limit 10m 271 /mnt/subvol_10m_100m
excl:btrfs qgroup limit -e 100m 271 /mnt/subvol_10m_100m
rfer:btrfs qgroup limit 100m 272 /mnt/subvol_100m_10m
excl:btrfs qgroup limit -e 10m 272 /mnt/subvol_100m_10m
rfer:btrfs qgroup limit 100m 273 /mnt/subvol_100m_100m
excl:btrfs qgroup limit -e 100m 273 /mnt/subvol_100m_100m
[root@btrfs-testdrive] # btrfs subvolume list /mnt/btrfs | grep -E 'subvol_[0-9m_]+$'
ID 270 gen 9125 top level 5 path subvol_10m_10m
ID 271 gen 9125 top level 5 path subvol_10m_100m
ID 272 gen 9125 top level 5 path subvol_100m_10m
ID 273 gen 9125 top level 5 path subvol_100m_100m
[root@btrfs-testdrive] # btrfs qgroup show /mnt/btrfs -re
qgroupid rfer       excl       max_rfer max_excl  
-------- ----       ----       -------- --------  
0/5      16384      16384      0        0         
0/256    2864132096 2864132096 0        0         
0/258    833703936  833703936  0        0         
0/259    52510720   52510720   0        0         
0/260    733184     733184     0        0         
0/270    16384      16384      0        10485760  
0/271    16384      16384      0        104857600 
0/272    16384      16384      0        10485760  
0/273    16384      16384      0        104857600 
[root@btrfs-testdrive] # for qg in $(/bin/ls -1 /mnt | grep -E '^subvol_')
do
echo "Write to ${qg} ..."
for i in {0..2048}
do
cmd=$(printf "dd if=/dev/zero of=/mnt/%s/zero.%05d.img bs=1MB count=1" $(basename ${qg}) ${i})
eval ${cmd} > /dev/null 2>&1 || break
done
done
Write to subvol_100m_100m ...
Write to subvol_100m_10m ...
Write to subvol_10m_100m ...
Write to subvol_10m_10m ...
[root@btrfs-testdrive] # btrfs filesystem df /mnt/btrfs
Data, single: total=5.01GiB, used=3.04GiB
System, DUP: total=8.00MiB, used=16.00KiB
System, single: total=4.00MiB, used=0.00
Metadata, DUP: total=1.00GiB, used=491.66MiB
Metadata, single: total=8.00MiB, used=0.00
[root@btrfs-testdrive] # du -h --max-depth=1 /mnt/subvol_*
90M     /mnt/subvol_100m_100m
9.1M    /mnt/subvol_100m_10m
95M     /mnt/subvol_10m_100m
9.0M    /mnt/subvol_10m_10m
@mazgi
Copy link
Author

mazgi commented Aug 21, 2014

[root@btrfs-testdrive] # btrfs qgroup limit 10m /mnt/limit_10m
[root@btrfs-testdrive] # btrfs qgroup limit 10m /mnt/limit_10m_compressed
[root@btrfs-testdrive] # btrfs qgroup limit 1g /mnt/limit_1g
[root@btrfs-testdrive] # btrfs qgroup limit 1g /mnt/limit_1g_compressed
[root@btrfs-testdrive] # btrfs subvolume list /mnt/btrfs
ID 256 gen 9015 top level 5 path gentoo
ID 258 gen 8970 top level 5 path usr-portage
ID 259 gen 8974 top level 5 path var
ID 260 gen 8975 top level 5 path var-log
ID 266 gen 9005 top level 5 path limit_10m
ID 267 gen 9005 top level 5 path limit_1g
ID 268 gen 9005 top level 5 path limit_10m_compressed
ID 269 gen 9005 top level 5 path limit_1g_compressed
[root@btrfs-testdrive] # btrfs qgroup show /mnt/btrfs -re
qgroupid rfer       excl       max_rfer   max_excl 
-------- ----       ----       --------   -------- 
0/5      16384      16384      0          0        
0/256    2865479680 2865479680 0          0        
0/258    833703936  833703936  0          0        
0/259    52510720   52510720   0          0        
0/260    729088     729088     0          0        
0/266    16384      16384      10485760   0        
0/267    16384      16384      1073741824 0        
0/268    16384      16384      10485760   0        
0/269    16384      16384      1073741824 0        

@mazgi
Copy link
Author

mazgi commented Aug 21, 2014

[root@btrfs-testdrive] # dd if=/dev/zero of=/mnt/limit_10m/zero.img bs=100k
dd: error writing ‘/mnt/limit_10m/zero.img’: Disk quota exceeded
102+0 records in
101+0 records out
10342400 bytes (10 MB) copied, 0.00663378 s, 1.6 GB/s
[root@btrfs-testdrive] # dd if=/dev/zero of=/mnt/limit_1g/zero.img bs=100k
dd: error writing ‘/mnt/limit_1g/zero.img’: Disk quota exceeded
183563+0 records in
183562+0 records out
18796748800 bytes (19 GB) copied, 17.0202 s, 1.1 GB/s
[root@btrfs-testdrive] # dd if=/dev/zero of=/mnt/limit_10m_compressed/zero.img bs=10k
dd: error writing ‘/mnt/limit_10m_compressed/zero.img’: Disk quota exceeded
1018+0 records in
1017+0 records out
10414080 bytes (10 MB) copied, 0.0119557 s, 871 MB/s
[root@btrfs-testdrive] # dd if=/dev/zero of=/mnt/limit_1g_compressed/zero.img bs=100k
dd: error writing ‘/mnt/limit_1g_compressed/zero.img’: Disk quota exceeded
154163+0 records in
154162+0 records out
15786188800 bytes (16 GB) copied, 17.524 s, 901 MB/s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment