* Copyright (C) 2000 Linus Torvalds.
* 2000 Transmeta Corp.
*
* Usage limits added by David Gibson, Linuxcare Australia.
* This file is released under the GPL.
*/
/*
* NOTE! This filesystem is probably most useful
* not as a real filesystem, but as an example of
* how virtual filesystems can be written.
*
* It doesn't get much simpler than this. Consider
* that this file implements the full semantics of
* a POSIX-compliant read-write filesystem.
This module was written primarily as an example of how virtual file systems can be written. One of the primary differences between this file system and the ramdisk facility found in modern Linux kernels is its capability to shrink and grow according to its use. A ramdisk does not have this property. This source module is compact and well written. It is presented here for its educational value. You are encouraged to study this good example.
The tmpfs file system is similar to and related to rams. Like ramfs, everything in tmpfs is stored in kernel virtual memory, and the contents of tmpfs are lost on power-down or reboot. The tmpfs file system is useful for fast temporary storage of files. I use tmpfs mounted on /tmp in a midi/audio application to speed up the creation and deletion of temporary objects required by the audio subsystem. This is also a great way to keep your /tmp directory cleanits contents are lost on every reboot. Mounting tmpfs is similar to any other virtual file system:
# mount -t tmpfs /tmpfs /tmp
As with other virtual file systems such as /proc, the first /tmpfs parameter in the previous mount command is a "no-op"that is, it could be the word none and still function. However, it is a good reminder that you are mounting a virtual file system called tmpfs.
9.10. Building a Simple File System
It is straightforward to build a simple file system image. Here we demonstrate the use of the Linux kernel's loopback device. The loopback device enables the use of a regular file as a block device. In short, we build a file system image in a regular file and use the Linux loopback device to mount that file in the same way any other block device is mounted.
To build a simple root file system, start with a fixed-sized file containing all zeros:
# dd if=/dev/zero of=./my-new-fs-image bs=1k count=512
This command creates a file of 512KB containing nothing but zeros. We fill the file with zeros to aid in compression later and to have a consistent data pattern for uninitialized data blocks within the file system. Use caution with the dd command. Executing dd with no boundary (count=) or with an improper boundary can fill up your hard drive and possibly crash your system. dd is a powerful tool; use it with the respect it deserves. Simple typos in commands such as dd, executed as root, have destroyed countless file systems.
When we have the new image file, we actually format the file to contain the data structures defined by a given file system. In this example, we build an ext2 file system. Listing 9-20 details the procedure.
Listing 9-20. Creating an ext2 File System Image
# /sbin/mke2fs ./my-new-fs-image
mke2fs 1.35 (28-Feb-2004)
./my-new-fs-image is not a block special device.
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
64 inodes, 512 blocks
25 blocks (4.88%) reserved for the super user
First data block=1
1 block group
8192 blocks per group, 8192 fragments per group
64 inodes per group
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
#
As with dd, the mke2fs command can destroy your system, so use it with care. In this example, we asked mke2fs to format a file rather than a hard drive partition (block device) for which it was intended. As such, mke2fs detected that fact and asked us to confirm the operation. After confirming, mke2fs proceeded to write an ext2 superblock and file system data structures into the file. We then can mount this file like any block device, using the Linux loopback device:
# mount -o loop ./my-new-fs-image /mnt/flash
This command mounts the file my-new-fs-image as a file system on the mount point named /mnt/flash. The mount point name is not important; you can mount it wherever you want, as long as the mount point exists. Use mkdir to create your mount point.
After the newly created image file is mounted as a file system, we are free to make changes to it. We can add and delete directories, make device nodes, and so on. We can use tar to copy files into or out of it. When the changes are complete, they are saved in the file, assuming that you didn't exceed the size of the device. Remember, using this method, the size is fixed at creation time and cannot be changed.
• Partitions are the logical division of a physical device. Numerous partition types are supported under Linux.
• A file system is mounted on a mount point in Linux. The root file system is mounted at the root of the file system hierarchy and referred to as / .
• The popular ext2 file system is mature and fast, and is often found on embedded and other Linux systems such as Red Hat and the Fedora Core series.
• The ext3 file system adds journaling on top of the ext2 file system, for better data integrity and system reliability.
• ReiserFS is another popular and high-performance journaling file system found on many embedded and other Linux systems.
• JFFS2 is a journaling file system optimized for use with Flash memory. It contains Flash-friendly features such as wear leveling for longer Flash memory lifetime.
• cramfs is a read-only file system perfect for small-system boot ROMs and other read-only programs and data.
• NFS is one of the most powerful development tools for the embedded developer. It can bring the power of a workstation to your target device. Learn how to use NFS as your embedded target's root file system. The convenience and time savings will be worth the effort.
• Many pseudo file systems are available on Linux. A few of the more important ones are presented here, including the proc file system and sysfs.
• The RAM-based tmpfs file system has many uses for embedded systems. Its most significant improvement over traditional ramdisks is the capability to resize itself dynamically to meet operational requirements.
9.11.1. Suggestions for Additional Reading
"Design and Implementation of the Second Extended Filesystem"
Rémy Card, Theodore Ts'o, and Stephen Tweedie
First published in the Proceedings of the First Dutch International Symposium on Linux
Available on http://e2fsprogs.sourceforge.net/ext2intro.html
"A Non-Technical Look Inside the EXT2 File System"
Randy Appleton
www.linuxgazette.com/issue21/ext2.html
Whitepaper: Red Hat's New Journaling File System: ext3
Michael K. Johnson
Читать дальше