Extra Disk Space on Linux – For Free! :-)

Today, I held a class on file systems, during which I demonstrated how to create a mountable image file on Linux. After mounting the image, my students and I somehow got to wonder what would happen, if you copied or moved the image file into its mount point folder. So we boldly did what probably no one did before, and the result was mind-blowing. I always thought that Linux, other than Micro$oft Windows, doesn’t do “strange things”. Well, I was quickly disabused from this idea.

Here is a tutorial on how you can seemingly create disk space out of thin air and for free!

  1. Create an empty image file using the dd command
    (base) maierm@abacus:~$ dd if=/dev/zero of=image.img bs=1M count=500
    500+0 records in
    500+0 records out
    524288000 bytes (524 MB, 500 MiB) copied, 0,211291 s, 2,5 GB/s
    (base) maierm@abacus:~$
  2. Create a file system within the image file.
    (base) maierm@abacus:~$ mkfs.ext4 image.img
    mke2fs 1.45.5 (07-Jan-2020)
    Discarding device blocks: done
    Creating filesystem with 128000 4k blocks and 128000 inodes
    Filesystem UUID: 77d581a9-705e-4b1b-a176-dba78d949484
    Superblock backups stored on blocks:
    32768, 98304
    Allocating group tables: done
    Writing inode tables: done
    Creating journal (4096 blocks): done
    Writing superblocks and filesystem accounting information: done
    (base) maierm@abacus:~$
  3. Create a mount point directory, and mount the image.
    (base) maierm@abacus:~$ mkdir mntpt
    (base) maierm@abacus:~$ mount image.img mntpt
    (base) maierm@abacus:~$ sudo mount image.img mntpt
    [sudo] password for maierm:
    (base) maierm@abacus:~$
  4. Copy the image file into the mount point directory.
    (base) maierm@abacus:~$ sudo cp image.img mntpt
    (base) maierm@abacus:~$

Done! Now, lo and behold! Abracadabra …

(base) maierm@abacus:~$ ls -lh mntpt
total 33M
-rw-r--r-- 1 root root 500M Nov 28 19:36 image.img
drwx------ 2 root root 16K Nov 28 19:25 lost+found
(base) maierm@abacus:~$

The 500 MiB image now seems to contain 516 MiB of data in total!

But there is even more than that …

(base) maierm@abacus:~$ sudo du -hs mntpt
[sudo] password for maierm: 
33M mntpt
(base) maierm@abacus:~$

The 500 MiB image inside the mount point directory seems to occupy only 33 MiB of actual disk space. With this magic in place, we can happily create more copies of the image.

(base) maierm@abacus:~$ sudo cp image.img mntpt/image2.img
(base) maierm@abacus:~$ sudo cp image.img mntpt/image3.img
(base) maierm@abacus:~$ sudo cp image.img mntpt/image4.img
(base) maierm@abacus:~$ sudo du -hs mntpt
230M mntpt
(base) maierm@abacus:~$ ls -lh mntpt
total 230M
-rw-r--r-- 1 root root 500M Nov 28 19:57 image2.img
-rw-r--r-- 1 root root 500M Nov 28 19:57 image3.img
-rw-r--r-- 1 root root 500M Nov 28 19:57 image4.img
-rw-r--r-- 1 root root 500M Nov 28 19:36 image.img
drwx------ 2 root root 16K Nov 28 19:25 lost+found
(base) maierm@abacus:~$

Strange, isn’t it? Unfortunately, I lack the time to dig into the related parts of the Linux source code to debug what exactly is happening in this scenario.
If you have an explanation for this behavior, please let me know!

See you,

— Andre M. Maier

About bitjunkie

Teacher, Lecturer, and BITJUNKIE ...
This entry was posted in Fun, Linux, Non-technical, Uncategorized and tagged , , , , , , . Bookmark the permalink.