Writing an OS Kernel from Scratch – Part 20: User Toolchain — Implementing ls, cat, mkdir and Other Basic Commands

“No matter how powerful the kernel, without user tools, it’s just an island. Today, we write the first user-mode shell toolset, making your OS truly usable!”

In the previous 19 posts, we built a complete OS kernel:
✅ Multi-core SMP support
✅ Higher-half kernel address space
✅ ext2 filesystem
✅ Process management and scheduling
✅ Standard input/output

But users can still only run hardcoded test programs. A real OS must provide basic command-line tools, allowing users to:

  • Browse files (ls)
  • Create directories (mkdir)
  • View files (cat)
  • Create empty files (touch)
  • Output text (echo)

Today, we write simplified versions of these tools as user programs, load and execute them via the ext2 filesystem, and implement a simple shell.


Tool List

Command Function Syscalls
ls List directory contents open, getdents
cat Display file contents open, read, write
mkdir Create directory mkdir
touch Create empty file open, close
echo Output arguments write
shell Command interpreter fork, exec, wait

All tools are loaded from ext2 via exec(“/bin/ls”, …)!


Shell Implementation

The shell uses fork + exec to run arbitrary commands. It reads a line of input, parses the command, forks a child process, and execs the requested program in the child while the parent waits.

Last modified: 2025年2月2日

Author

Comments

Write a Reply or Comment

Your email address will not be published.