玩具 OS 文件系统

Toy OS Filesystem

我在 assembly/c 中开发了一个基本内核,运行 是一个基本终端。我已经将它设置为 运行 脱离带有 grub 的 iso。

我想继续这个OS,但是没有文件系统,我觉得好像真的没有别的事情可以做。在互联网上花了很长时间后,我真的没有什么可以做的来实现这个。

人们说实施 FAT 或制作 VFS,但没有进一步的说明,也没有教程,也没有任何参考。

谁能解释一下文件系统的工作原理,我可以从哪里获得 started/where 我可以连接预制系统,以及如何使用它?

此外,在编译 os 时我无法访问标准库。我使用 gcc、nasm、ld 和 grub-mkrescue(用于磁盘映像)。我使用 qemu 进行仿真。

编辑以减少 OT

有人可以详细描述一个文件系统是如何工作的,这样当我查看已经实现的文件系统的源代码时,比如 FAT,我可以理解如何将它应用到我自己的操作系统中吗?

编辑 - 更简单

更简单。我怎样才能直接访问硬盘驱动器?我的内核 运行 完全处于保护模式,所以我可以退出并直接写入硬盘。文件系统可以用这样的文件来实现:

名称 特殊字符 文本 特殊字符

即:

你好world.script0x00println"Hello, world!!"0x00

在不需要特殊分段的地方,您只需查找文件名和特殊字符(不在像 '\0' 这样的字符串中的字符),然后阅读直到找到第二个非-字符串字符。

有没有办法通过进入和退出保护模式来访问硬盘或者写一个硬盘驱动来实现这个?

首先,阅读 file systems 上的维基页面以获得更广阔的视野。

有关operating system development is OSdev (but perhaps your question is off-topic here). Kernelnewbies could also help (explaining how Linux is doing). OSdev have wikipages explaining FAT & Ext2的相关资源详细。

您可以设计一个 OS 没有任何文件(但其他一些 persistence machinery). See this answer. You could have persistent processes (read also about application checkpointing, garbage collection, continuations, hibernation)。

但是你应该阅读一些关于操作系统的好书(例如by Tanenbaum, or the freely downloadable Operating Systems: Three Easy Pieces book). Be fluent with some existing free software OS, e.g. Linux (& POSIX), so read Advanced Linux Programming(至少要理解很多概念并获得一个好的术语)。

恕我直言,FAT is such an ugly and inefficient file system that it is not worth looking into (except for legacy and compatibility reasons). Ext4 (see here) should be better & the wikipage on Ext2 的照片不错。

您可以为您的内核改编一些提供文件系统(例如 libext2)的库。

您也许可以调整 sqlite 以在原始磁盘分区上工作。

您可能有一个 file 的概念,它不同于 MSDOS(或 Windows)或 POSIX 或 <stdio.h> 文件。例如,它可能是一系列固定大小的记录(例如 1Kbyte),而不是字节流。

您可以将 OS 整理为 microkernel and have file systems given by application code. Look into VSTa and HURD

您当然需要一个磁盘驱动程序,它可以从您的驱动器(磁盘 I/O 总是按块或 disk sectors. Old small disks had 512 bytes blocks. New large disks have 4Kbytes ones, see advanced format). It should be interrupt driven and uses DMA. You need a task scheduler. AFAIU, you use the BIOS for this (perhaps the UEFI); you need to understand how common hardware (SATA & AHCI)中 fetches/writes 块(4Kbytes)工作。

您应该发布(今天!)您的玩具 OS 作为 free software (e.g. under GPLv3+ on github)以获得反馈和贡献。

您可能会从其他 free software 操作系统复制(如果许可证兼容)现有代码,并且您肯定会研究它们的源代码以了解事情。

所以编写一些任务调度程序,一种 page fault handler, a virtual memory, then add interrupt driven disk IO, and some file system code above that. Then you'll beginning to understand that an OS cannot be a small toy.... You might consider a microkernel or exokernel 方法。

如果许可条款适合您的需要,使用现有的开源文件系统将是最简单的。 ELM FatFs 就是这样一个库,没有任何使用限制。您只需要使用提供的存根和示例来提供设备控制接口层。