Skip to content

Instantly share code, notes, and snippets.

@fellipec
Created January 25, 2025 12:18
Show Gist options
  • Save fellipec/17250b03ac923a6bccc6930054091ff8 to your computer and use it in GitHub Desktop.
Save fellipec/17250b03ac923a6bccc6930054091ff8 to your computer and use it in GitHub Desktop.
Everything is a file

To Linux evertything is a file.

When you run mkfs.ext4 /dev/sdx or mkfs.ext4 /dev/sdx1 or even mkfs.ext4 /tmp/dummyfile it doesn't care where it is creating your filesystem, it just does.

Let's demonstrate:

  1. First, let's create a 1GB file in my home dir:
dd if=/dev/zero of=testfile bs=1G count=1 oflag=direct status=progress
1+0 records in
1+0 records out
1073741824 bytes (1,1 GB, 1,0 GiB) copied, 0,914921 s, 1,2 GB/s
  1. Now lets format it with ext4
mkfs.ext4 testfile
mke2fs 1.47.0 (5-Feb-2023)
A descartar blocos de dispositivo: pronto
A criar sistema de ficheiros com 262144 4k blocos e 65536 inodes
UUID do sistema de ficheiros: bd296faa-d9b2-46e4-9729-3a264f1e48de
Cópias de segurança de superblocos gravadas em blocos:
	32768, 98304, 163840, 229376

A alocar tabelas de grupo: pronto
Gravando tabelas inode: pronto
A criar diário (8192 blocos): concluído
Escrevendo superblocos e informações de contabilidade de sistema de arquivos: concluído
  1. Now lets create a mountpoint and mount it
mkdir testmountpoint
sudo mount testfile testmountpoint
  1. Let's check how it looks now:

https://postimg.cc/4n4ftNxP

df -h
Sist. Arq.      Tam. Usado Disp. Uso% Montado em
[...]
/dev/loop0      974M   24K  907M   1% /home/fellipec/testmountpoint
  1. Now lets copy some files inside this "drive"
sudo cp -r /usr/share/wallpapers testmountpoint
ls testmountpoint
lost+found  wallpapers

https://postimg.cc/2qfbfRFt

  1. Now if testfile is an entire ext4 filesystem, let's insert a USB drive here and try this:
sudo umount testmountpoint
sudo cp testfile /dev/sdd
sync
  1. Remove the USB drive and insert it back...

Notice that it is mounted in a directory that matches the UUID mkfs.ext created when I ran it in the testfile

df -h
Sist. Arq.      Tam. Usado Disp. Uso% Montado em
[...]
/dev/sdd        974M  2,5M  904M   1% /media/fellipec/bd296faa-d9b2-46e4-9729-3a264f1e48de

ls -la  /media/fellipec/bd296faa-d9b2-46e4-9729-3a264f1e48de/
total 28
drwxr-xr-x   4 root root  4096 jan 25 08:56 .
drwxr-x---+  4 root root  4096 jan 25 09:04 ..
drwx------   2 root root 16384 jan 25 08:49 lost+found
drwxr-xr-x  70 root root  4096 jan 25 08:56 wallpapers

Notice that it shows "16GB Volume" but the free space is just 947MB, because when we created the filesystem it was on a 1GB file.

https://postimg.cc/xX415rq7

gparted even tell us about the unused space:

https://postimg.cc/N9wDKz89


With this demo I hope to have illustrated that concept of everything being a file, and that you can run mkfs on a regular file and mount it, and you can copy this file to the physical device with regular cp command (no need dd, balena, whatever) and the computer will happily mount the result of this.

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