Fundamentals 13 min read

Designing a Simple Ext2‑Like File System from Scratch

This tutorial walks through building a tiny 1 MB file system using 1 KB blocks, block bitmaps, 128‑byte inodes, superblocks, directory structures, and single‑ and double‑indirect indexing, culminating in a design comparable to Linux's ext2 filesystem.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Designing a Simple Ext2‑Like File System from Scratch

Goal

You have a 1 TB (treated as 1 MB for the demo) hard‑disk and a collection of files that you want to store and retrieve efficiently.

1. Block Layout

To avoid dealing with low‑level sectors and drivers, the disk is divided into logical blocks of 1 KB (2 physical sectors). With a 1 MB disk this yields 1024 blocks.

2. Block Bitmap

Block 0 is reserved as a block bitmap that records whether each block is free (0) or used (1). The bitmap is updated each time a block is allocated.

3. Inode Structure

Each file gets a 128‑byte inode that stores metadata such as name, size, timestamps, permissions, and a file‑type field (regular file or directory). One block can hold eight inodes, so block 1 holds the inode bitmap and block 2 holds the inode table .

4. Superblock

Block 0 (after the bitmap) is used as a superblock to store global information: total number of inodes, free inodes, total blocks, and free blocks.

5. Storing Files

When a file is created, the first free block (found via the block bitmap) is allocated and the file’s inode records that block number. For larger files, the system initially uses contiguous allocation , storing the first block number and the number of additional blocks needed.

Contiguous allocation can create internal holes that waste space, so the design is extended.

6. Indirect Indexing

To support larger files, one block can be used as a single‑indirect index , containing 256 block numbers (since each block is 1 KB). This expands the addressable space to 263 blocks (≈263 KB). Additional levels (double‑indirect) can be added, each multiplying the addressable blocks by 256.

7. Directory Files

Inodes are given a type field (4 bytes). Regular files store data directly; directory inodes store a block that contains a list of directory entries , each entry pointing to another inode (name + inode number). This enables hierarchical structures like:

葵花宝典.txt (regular file)

数学期末复习资料.mp4 (regular file)

赘婿/ (directory)

赘婿1.mp4

赘婿2.mp4

赘婿3.mp4

赘婿4.mp4

低并发编程的秘密.pdf

File paths are formed by joining directory names, e.g., 赘婿/赘婿1.mp4.

8. Root Directory

The inode with number 0 is designated as the root directory . All navigation starts from this inode.

9. Comparison with ext2

The resulting design mirrors the classic Linux ext2 filesystem: both use a superblock, block bitmap, inode bitmap, inode table, and data blocks. Differences include the location of the boot sector, the number of inode table blocks, and the exact layout of indirect indexes.

For deeper study, consult the ext2 source code (Linux 1.0+), the official PDF at https://www.nongnu.org/ext2-doc/ext2.pdf, or quality blogs such as http://docs.linuxtone.org/ebooks/C&CPP/c/ch29s02.html. You can also generate an ext2 image with mke2fs and analyse its raw layout.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Operating Systemsfile systeminodeExt2block bitmap
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.