54 CIRCUIT CELLAR • APRIL 2014 #285

EMBEDDED IN THIN SLICES Embedded File Systems (Part 4) COLUMNS

Specialized Tools Several types of file systems are available for Linux. This article discusses compressed read- only file systems (), RAM file systems, network file systems (NFS), and the Samba networking protocol.

By Bob Japenga (US)

o help embedded systems designers using the built-in zlib compression to enable Tachieve their design goals, Linux offers random access of all of the files. This makes a variety of file systems that require only a CRAMFS relatively fast. The file metadata moderate amount of effort to implement. This (e.g., information about when the file was article introduces several file systems and a created, read and write privileges, etc.) is networking protocol and describes how and not compressed but uses a more compact when to use each one. notation than is present in most file systems. WHAT IS A CRAMFS? WHEN TO USE A CRAMFS While pursuing my Master’s degree in The primary reason my company has used 1973, a professor made a statement I will a CRAMFS is to cut down on the never forget: “Memory prices are plunging used by the . The first embedded so fast you will never have to worry about Linux system we worked on had 16 MB of RAM how much memory your designs use from and 32 MB of flash. There was a systems- now on.” Obviously, this professor did not level requirement to provide a means for have a crystal ball and missed something the system to recover should the primary that has been a design challenge in virtually partition become corrupt or fail to boot every I have designed. Our in any way (see Part 3 of this article series systems demand more and more memory (or “Designing Robust Flash Memory Systems,” file space) and a compressed read-only file Circuit Cellar 283, 2014, for more detail). We system (CRAMFS) can be a useful solution in met this requirement by creating a backup some instances. partition that used a CRAMFS. A CRAMFS is an open-source file system The backup partition’s only function was to available for Linux. I am not sure where the enable the box to recover from the corrupted CRAMFS gets its name. Perhaps it gets its primary partition. How we accomplished that name because a CRAMFS is one way to cram is a discussion for another day, but we had your file system into a smaller footprint. very little flash memory to hold the Linux The files are compressed one page at a time kernel, the file system, the boot-loader, and circuitcellar.com 55

the application. We were able to have the two mount command, you specify the ramfs size. file systems identical in file content, which However, it can grow in size to exceed that made it easy to maintain. Using a CRAMFS amount of RAM. Thus ramfs will enable you enabled us to cut our backup file system to use your entire RAM and not provide you space requirements in half. any with warning that it is doing it. A second feature of a CRAMFS is its read- does not enable you to write more than the only nature. Given that it is read-only, it space allocated at mount time. An error is does not require wear leveling. This keeps returned when you try to use more than you the overhead for using CRAMFS files very have allocated. Another difference is that low. Due to the typical data retention of flash tmpfs uses swap space and can swap seldom memory, this also means that for products used files out to a flash drive. ramfs does that will be deployed for more than 10 years, not use swapping. This difference is of little you will need to rewrite the CRAMFS partition value to us since we disable swapping in our COLUMNS every three to five years. embedded systems. The fact that the file system is read-only can make it difficult to deploy in all situations. WHEN TO USE RAM FILE SYSTEMS For example, once our CRAMFS began using In 1987, the Department of Defense began encryption keys that were uniquely calculated requiring the use of Ada in new embedded for each system one time at start-up (this systems. Shortly thereafter I bought an Ada took several minutes on a 200-mHz ARM9 compiler for my IBM PC at work. Much to my processor) the CRAMFS became a problem. surprise, the compiler came with a board We could not store the calculated keys on to plug into the PC. The board added a RAM the CRAMFS and the start-up delay was file system to my PC. To improve the speed unacceptable. This forced us to come up with of compiling from glacial to turtle-like, the an alternate backup file system. compiler designers used a RAM file system to store all intermediate results. HOW TO MAKE A CRAMFS Speed is one of the primary reasons to use As with the other file systems I have a RAM file system. Disk writes are lightning discussed, implementing a CRAMFS with fast when you have a RAM disk. We have used Linux is simple and relatively painless. Linux a RAM file system when we are recording a provides a tool (mkfs) you can use to take any burst of high-speed data. In the background, group of directories and files and create a we write the data out to flash. CRAMFS image. Actually, mkfs is a wrapper A second reason to use a RAM file system for a variety of file system building tools. is that it reduces the wear and tear on the mkfs.cramfs is the tool used to create the , which has a limited write CRAMFS image. Once written, the image is life. We make it a rule that all temporary files placed in its own partition. should be kept on the RAM disk. We also use it for temporary variables that are needed WHAT ARE RAM FILE SYSTEMS? across threads/processes. Linux provides two types of RAM file systems: ramfs and tmpfs. Both are full- HOW TO MAKE A RAM FILE SYSTEM featured file systems that reside in RAM and Since tmpfs is built into Linux, you can are thus very fast and volatile (i.e., the data simply mount a RAM drive with a command is not retained across power outages and such as: system restarts). When the systems are created with the mount – o size=16G –t tmpfs my_

Hard drive CRC

PLC PLC

Embedded Embedded Embedded Embedded PLC Embedded Embedded Embedded Embedded controller controller controller controller controller controller controller controller

FIGURE 1 This layout shows the memory regions

Embedded Embedded Embedded Embedded required to bring up one of my controller controller controller controller company’s embedded Linux designs with no redundancy. 56 CIRCUIT CELLAR • APRIL 2014 #285

tmpfs /mnt/tmpfs The major area in which we use NFS today is during software development. Even though where the maximum size is 16 GB, the type flash file systems are fast and new versions of of file system istmpfs , the name is my_ your code can be seamlessly written to flash, tmpfs, and the access point (device name) is it can be time consuming. For example, you /mnt/tmpfs. The size can also be specified can use a flash memory stick over USB to in terms of percentage of your total physical update the flash file system on several of our RAM. tmpfs will only use the RAM that it designs. This is simple but can take anywhere needs up to the maximum specified. If you from several seconds to minutes. don’t specify a maximum, it defaults to 50% With an NFS, all of your development tools of your total physical RAM. can be on a PC and you never have to transfer ramfs is also built into Linux and is created the target code to the target system. You use with a command such as: all of your PC tools to change the file on your ABOUT THE AUTHOR PC, and when the embedded device boots up mount –o size=20m –t ramfs my_ Bob Japenga has been or the application is restarted, those changed

COLUMNS ramfs /mnt/ramfs files will be used on the device. designing embedded systems since 1973. In where the type is ramfs, the name is my_ HOW TO MAKE AN NFS 1988, along with his ramfs and the access point (device name) is An NFS is built into Linux and thus is pretty best friend, he start- /mnt/ramfs. trivial to set up. The embedded system can ed MicroTools, which mount an external drive using a combination specializes in creating WHAT ARE NETWORK FILE of the Mount command and an entry in the file a variety of real-time SYSTEMS? system table file (usually in /etc/fstab). The embedded systems. In the early 1990s I started working with a external drive requires an NFS driver that is With a combined em- company that developed embedded controllers easily available for Windows and is built into bedded systems experi- for machine control. These controllers had a all Linux distributions. ence base of more than user interface that consisted of a PC located 200 years, they love to on the factory floor. The company called WHAT IS SAMBA? tackle impossible prob- this the production line console (PLC). The Although we don’t like to admit it, many lems together. Bob has factory floor was hot, very dirty, and had a of us still have Windows machines on our been awarded 11 pat- lot of vibration. The company had designed desks and on our laptops. And many of us are ents in many areas of a control room console (CRC) that networked attached to some good development tools on embedded systems and together several PLCs. The CRC was located our Windows machines. motion control. You can in a clean and cool environment. The PLC and Samba is not exactly a file system but reach him at rjapenga@ the CRC were running QNX and the PLC was rather a file system/networking protocol microtoolsinc.com. diskless. The PLC booted from and stored all that enables you to write to your embedded of its data on the CRC (see Figure 1). system’s file system from your Windows This was my first exposure to a network machine as if it were a Windows file system. file system (NFS). It was simple and easy to configure and worked flawlessly. The PLCs WHEN TO USE SAMBA could only access their “file system.” The CRC Although I primarily see Samba, like an could access any PLC’s files. NFS, as a development tool, you could certainly QNX was able to do this using the NFS use it in an environment where you needed to protocol. NFS is a protocol developed initially talk to your embedded device from a Windows by (which is now owned by machine. We have never had a need for this, Oracle). Early in its lifetime, Sun turned the but I can imagine it could be useful in certain specification into an open standard, which scenarios. The Linux community documents a circuitcellar.com/ccmaterials was quickly implemented in Unix and its lot of Samba users for embedded Linux. derivatives (e.g., Linux and QNX). RESOURCES HOW TO MAKE SAMBA FreeNFS, http://freenfs. WHEN TO USE AN NFS To make a Samba, two daemons will need sourceforge.net. One obvious usage of an NFS is for to be built for your processor: one (smbd) to environments where a hard drive cannot handle file and printer sharing and one (nmbd) B. Japenga, “Embedded File easily survive, as shown in my earlier to handle NETBIOS to IP name services. Systems (Part 3): Design- example. However, my example was before Virtually all distributions of Linux will have the ing Robust Flash Memory flash file systems became inexpensive and necessary make files and recipes for you to Systems,” Circuit Cellar 283, reliable so that is not a typical use for today. put Samba on your embedded device. 2014. Another use for an NFS would be to simplify software updates. All of the software could be USEFUL EMBEDDED SYSTEMS TOOLS Samba, www.samba.org. placed in one central location. Each individual You are now armed with some additional SOURCE controller would obtain the new software tools to create incredible embedded systems. ARM9 Processor once the central location was updated. Each of these specialized file systems has its ARM, Ltd. | www.arm.com circuitcellar.com 57

own niche. But you have to know what you can do to take advantage of them—even if you only know them—in thin slices. My next article series will explore another dimension of embedded Linux. COLUMNS