[LINUX] How many bytes of files can be placed on a 50M disk?

TL; DR Let's consider the physical capacity, effective capacity, and user capacity separately.

If you're not good at it, you may only be able to store about 840M of data on a 1000G physical disk!

Introduction

What should a person do when he is told to "reserve a 50M data area"?

dd if =/dev/zero of = tmp.img bs = 1M count = 50 mkfs.ext4 tmp.img and sudo mount -t ext4 -o loop tmp.img ./tmptmp?

I will try to make a file.

Now, let's create a file normally and check availability.

kmtr@ubuntu2010:~$  dd if=/dev/zero of=tmp.img bs=1M count=50

50+0 Record input
50+0 record output
52428800 bytes (52 MB, 50 MiB) copied, 6.359 s, 8.2 MB/s
kmtr@ubuntu2010:~$
kmtr@ubuntu2010:~$ mkfs.ext4 tmp.img
mke2fs 1.45.6 (20-Mar-2020)
Discarding device blocks: done
Creating filesystem with 12800 4k blocks and 12800 inodes

Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

kmtr@ubuntu2010:~$ sudo mount -t ext4 -o loop tmp.img tmp
tmp.img  tmptmp/
kmtr@ubuntu2010:~$ sudo mount -t ext4 -o loop tmp.img tmptmp/
kmtr@ubuntu2010:~$ df -B1 tmptmp
Filesystem     1B-blocks  Used Available Use% Mounted on
/dev/loop9      46579712 49152  42860544   1% /home/kmtr/tmptmp

As a result, the reserved size for the 52428800 bytes disk image was 46,579,712 bytes. The available Available is 42,860,544 Bytes. Oh ... God ...

Free up reserved space

Let's set the Reserved block count to 0 to get as close to recognition as possible.

root@ubuntu2010:/home/kmtr# tune2fs -l test2.img  | grep "Reserved block count" -A3 -B3
Filesystem OS type:       Linux
Inode count:              12800
Block count:              12800
Reserved block count:     640
Free blocks:              11360
Free inodes:              12789
First block:              0

tune2fs -m 0 test.img

root@ubuntu2010:/home/kmtr# tune2fs -l test.img  | grep "Reserved block count" -A3 -B3
Filesystem OS type:       Linux
Inode count:              12800
Block count:              12800
Reserved block count:     0
Free blocks:              11360
Free inodes:              12789
First block:              0

root@ubuntu2010:/home/kmtr/tmptmp# df . -B1
Filesystem     1B-blocks  Used Available Use% Mounted on
/dev/loop9      46579712 49152  45481984   1% /home/kmtr/tmptmp

Available has increased from 42,860,544 to 45,481,984!

640 block ✕ 4096 = 2,621,440 Bytes, so the calculation is just right.

However, it seems that this reserved area size is secured so that root can process it even if it becomes disk full, so it is not a good idea to reduce it unnecessarily.

How many bytes do you use to put a 20M file?

So what happens to Used if you create a 20MB file on this tmptmp?

root@ubuntu2010:/home/kmtr# df -k tmptmp -B1
Filesystem     1B-blocks  Used Available Use% Mounted on
/dev/loop9      46579712 49152  45481984   1% /home/kmtr/tmptmp
root@ubuntu2010:/home/kmtr# dd if=/dev/zero of=tmptmp/20M bs=1M count=20
20+0 Record input
20+0 record output
20971520 bytes (21 MB, 20 MiB) copied, 0.0206479 s, 1.0 GB/s
root@ubuntu2010:/home/kmtr# df -k tmptmp -B1
Filesystem     1B-blocks     Used Available Use% Mounted on
/dev/loop9      46579712 21090304  24440832  47% /home/kmtr/tmptmp

Available decreased from 45,481,984 to 24,440,832. That is, 21,440,832 bytes were consumed. This is because a file of 20,971,520 bytes is created, but the management area is used only for 469,312 bytes. This is a little less than 2.2%.

If you roughly estimate the size required for this management to be 3%, the actual file size will be 45481984 / 1.03 = 44,157,266.

Well, that's normal, and there's no reason you don't need any data to manage your files.

Summary: Let's consider physical capacity, effective capacity and user capacity separately.

The table below summarizes the figures that have come up so far. This is because of the size of the journal ...

Capacity when 50M is secured Percentage of physical capacity Remarks
Physical capacity 52,428,800 100% Appearance size
Effective capacity 42,860,544 81.75% Available /Reserved area 5%
Effective capacity 45,481,984 86.75% Available /Reserved area 0%
User capacity 44,157,266 84.22% Data size that can be placed by the user(※1) /Reserved area 0%

So, in order to secure a data area of ​​50M, I think it is necessary to roughly secure about 50M / 0.85 = 58M.

root@ubuntu2010:/home/kmtr# dd if=/dev/zero of=tmp3.dat bs=1M count=58
mkfs.ext4 tmp3.dat
tune2fs -m 0 tmp3.dat
sync
mount -t ext4 -o loop tmp3.dat tmptmp
58+0 Record input
58+0 record output
60817408 bytes (61 MB, 58 MiB) copied, 0.0681551 s, 892 MB/s
root@ubuntu2010:/home/kmtr# mkfs.ext4 tmp3.dat
df -B1 tmptmpmke2fs 1.45.6 (20-Mar-2020)
Discarding device blocks: done
Creating filesystem with 14848 4k blocks and 14848 inodes

Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

root@ubuntu2010:/home/kmtr# tune2fs -m 0 tmp3.dat
tune2fs 1.45.6 (20-Mar-2020)
Setting reserved blocks percentage to 0% (0 blocks)
root@ubuntu2010:/home/kmtr# sync
root@ubuntu2010:/home/kmtr# mount -t ext4 -o loop tmp3.dat tmptmp
root@ubuntu2010:/home/kmtr# df -B1 tmptmp
Filesystem     1B-blocks  Used Available Use% Mounted on
/dev/loop9      54706176 53248  53440512   1% /home/kmtr/tmptmp
root@ubuntu2010:/home/kmtr# dd if=/dev/zero of=tmptmp/50M.dat bs=1M count=50
50+0 Record input
50+0 record output
52428800 bytes (52 MB, 50 MiB) copied, 0.0498229 s, 1.1 GB/s
root@ubuntu2010:/home/kmtr# df -B1 tmptmp
Filesystem     1B-blocks     Used Available Use% Mounted on
/dev/loop9      54706176 52641792    851968  99% /home/kmtr/tmptmp

However, if this is 10MB ✕ 5, it will be so!

root@ubuntu2010:/home/kmtr# df -B1 tmptmp
Filesystem     1B-blocks     Used Available Use% Mounted on
/dev/loop9      54706176 52666368    827392  99% /home/kmtr/tmptmp

That's it.

Recommended Posts

How many bytes of files can be placed on a 50M disk?
How to access the contents of a Linux disk on a Mac (but read-only)
How to create a property of relations that can be prefetch_related by specific conditions
NumPy zeros can be defined even with a size of 0
How to register a package on PyPI (as of September 2017)
How to display a specified column of files in Linux (awk)
How a freelance engineer can read a dissertation on his own (1 / n)
GraalVM's memory usage can be very low. What a 1/16 of Java!
I did a lot of research on how Python is executed