|
Creating Initial RAM Disks on LinuxThis Tech Tip discusses initial RAM disks, which are commonly used during the Linux boot process. You’ll also read about the differences between how they are created on native Linux systems, the most traditional type of initial RAM disk (which is a compressed file that contains an ext2 filesystem), and the traditional method for creating initial RAM disks of this type on a Linux system. (And if that’s not enough, watch your inbox for next month’s issue of TimeSource, where we explore alternate methods.) Initial RAM Disks 101 Initial RAM disks are compressed files that provide an initial filesystem for use by the Linux kernel at boot time. The Linux kernel accepts initial RAM disks in multiple formats, ranging from compressed archive files produced by the Unix/Linux cpio command, to compressed files that, when expanded, contain an actual filesystem in a format such as ext2, romfs, and so on. The contents of an initial RAM disk differ based on the hardware, system capabilities, and type of Linux system that you are using. Desktop Linux systems typically use initial RAM disks to provide loadable device drivers or mandatory system setup procedures that are required to proceed with the boot process. Embedded Linux systems often use initial RAM disks to minimize storage requirements in the absence of extensive disk, flash, or compact Flash storage. Creating Initial RAM Disks Under Linux The traditional mechanism for creating an initial RAM disk on Linux systems is to create an empty file of the desired size using the Linux dd command, creating a filesystem in that file using the mkfs command, and then mounting the formatted file using a loopback device. In this context, a loopback devices is a special Linux device that enables the system to access a file as though it were a block device. The commands to create and mount a 32-MB loopback filesystem on Linux would be something like the following: $ dd if=/dev/zero of=my_filesystem.img bs=1024 count=32768 Once you mount the filesystem image, you can access it just like any other filesystem, adding applications, drivers, support programs and scripts, and anything else that you will need to use it as a root filesystem during your system’s boot process. Populating Initial RAM Disks As you add things to a filesystem that is destined for use as an initial RAM disk, whatever you install must obey certain rules:
As you populate this filesystem, you must also make sure that any of the infrastructure required by a Linux system is present there. An initial RAM disk must have an entry named /linurc, which is usually either a file that contains startup commands to execute, or a symlink to /bin/init, which can be used to initiate the traditional Linux SysVinit process. Your initial RAM disk must contain a /dev directory, which must at least contain device nodes for /dev/console and /dev/null. The RAM disk must contain a /tmp directory to hold the temporary files used by many other commands, and so on. Using Initial RAM Disks It’s important to differentiate between how you create and add content to a file that contains a filesystem, and how you actually use it as an initial RAM disk. Creating and mounting a file containing a filesystem makes it available for use by your development system, just like any other filesystem. You can compile and install applications into it just like any other mounted filesystem. In this form, it is therefore both readable and writable by anyone with the appropriate permissions. Once you’ve populated the filesystem image with everything that you want the system have access to during the boot process, your filesystem image requires one final transformation in order to prepare it for use as an initial RAM disk. This last step is to compress the filesystem image, which minimizes the space that it requires on your system and is therefore especially important for embedded systems. This compression is typically done using the GNU gzip command, as in the following examples: # umount /mnt/initrd Using a filesystem image as an initial RAM disk requires that the kernel is configured to use an initial RAM disk and that it knows where to find the compressed filesystem image. Initial RAM disks can either be appended to the actual kernel image, which is commonly done on embedded systems, or identified by the bootloader, as is commonly done on desktop Linux systems. When you boot a kernel that uses an initial RAM disk, the kernel locates the compressed filesystem image and extracts into a portion of RAM memory that it can then use as a filesystem. This explains why compressed filesystem images used by the kernel are known as “initial RAM disks”—they are used as an initial filesystem during a Linux System’s boot process, and are uncompressed into RAM memory in order to be used by the kernel. An initial RAM disk can either be a temporary filesystem, used to load drivers or perform initial configuration during your system’s boot process, or as the primary filesystem used by your Linux system, as is the case on many embedded systems. The only real downside to using an initial RAM disk as your only filesystem is that it doesn’t give you the option of saving data across system restarts. Because the version of the filesystem that is being used is the version in memory, any changes that you make in memory are lost when you restart the system. Summary Extracting a filesystem into memory and using it is extremely convenient for loading drivers, doing initial system configuration, or for providing a filesystem to the kernel when no local storage devices are present. This Tech Tip explored the concepts and process for building an initial RAM disk on Linux systems. This procedure differs on actual Linux systems and on Windows systems running Cygwin. Author: Bill von Hagen
|
| ||||||||||||||||||||||||






