System DIY: customized Linux system on a floppy disk
Source: Internet
Author: User
Article Title: System DIY: customized Linux system on a floppy disk. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Embedded Linux is composed of a hundreds of KB Linux kernel and a file system as needed. Because Linux is an open-source operating system, therefore, it has a very broad prospect in the embedded field and has been widely used in many electronic products such as mobile phones, PDAs, and MP3 players. This article will introduce a Linux system on two floppy disks. It can be used as an emergency repair disk, a router or a firewall in many places, it can also enhance the understanding of embedded systems.
I. Preface
Embedded Linux is composed of a hundreds of KB Linux kernel and a file system as needed. Because Linux is an open-source operating system, therefore, it has a very broad prospect in the embedded field and has been widely used in many electronic products such as mobile phones, PDAs, and MP3 players. This article will introduce a Linux system on two floppy disks. It can be used as an emergency repair disk, a router or a firewall in many places, it can also enhance the understanding of embedded systems.
Ii. Linux Startup Process
After all the PCs are powered on, the BIOS will find the first sector of the boot disk and copy it to RAM to execute it. For two different boot methods, this sector usually contains two different types of code: the code of the boot program (such as Lilo or Grub), the boot program will help locate the kernel location. Kernel code, which is usually used for booting from a floppy disk. For the former, the kernel usually needs to support initrd. If the latter is used, the Boot Loader is arch/i386/boot/bootsect. S. When the kernel is compiled, this code is linked to the very beginning of the kernel image. In this way, you can easily copy the kernel to a floppy disk with the first sector as the starting position to obtain a self-starting floppy disk. The kernel will initialize the device driver and internal data structure, and then it will go to a specific location ?? Ramdisk Word to obtain the location of the root file system. The kernel must know where to find the root file system, or it will be shut down.
When starting with a floppy disk, the kernel can release a compressed file system to RAM, which is called Ramdisk. This is a memory area, but the kernel will use it as a disk.
The example described in this article uses Grub as the boot program and initrd to help Linux start up. The two floppy disks are named bootldr disks and rootfs disks respectively. The content in the bootldr disk is grub, kernel, and initrd. The rootfs disk is a compressed root file system. When the system starts, the Grub of the bootldr disk locates and executes the kernel. Then, the kernel unlocks the initrd and runs the linuxrc file. This file prompts you to replace the rootfs disk and decompress the content to the memory, then execute the unzipped init to continue the startup process.
To facilitate understanding of this example, we first introduce the directory structure as follows:
Replace the device in the/boot/grub directory. copy map, stage1, and stage2 to/mnt/floppy/boot/grub, and create grub in the/mnt/floppy/boot/grub directory. conf file:
Default = 0
Timeout = 10
Title Floppy Linux
Kernel (fd0)/bzImage root =/dev/ram0
Initrd (fd0)/initrd.gz
In this way, grub is installed on the bootldr disk.
4. customized Linux Kernel
Due to the limitation of the floppy disk size, the kernel should only contain some necessary support as much as possible. For the example in this article, we must select the initrd support. For example, if the disk is used as a system repair disk, the necessary support can be: IDE, PCI, and the required file system type, without network support. Of course, if it is used as a router or firewall, network support is necessary, and others can be deleted accordingly.
# Make [xconfig | menuconfig | config]
# Make bzImage
If you have added support for the module, you also need
# Make modules
Then we get the kernel image bzImage. If the size of bzImage exceeds the limit of the floppy disk, You need to reconfigure it. Put the compiled bzImage in the root directory of the bootldr disk. If you change the bzImage name, make sure it is the same as the name in grub. conf.
5. Develop initrd
Create the bin, dev, etc, lib, proc, sysroot, and usr directories in the initrd/local directory. The dev directory contains necessary device files, such as tty, ram, console, etc. The necessary executable files in bin include bzip2, chroot, cp, cpio, dd, echo, mount, pivot_root, readkey, sh, and test. Busybox provides most of them. Bzip2, dd, and cpio are used to extract the content on the second floppy disk. chroot and cmdt_root are used to convert the root directory.
Edit the initrd/local/linuxrc file:
#! /Bin/sh
Mount the sysroot directory to a memory and create the tmpfs file system.
Echo "Mounting new root filsystem ..."
Mount tmpfs/sysroot-t tmpfs
Cd/sysroot
The following readkey is a very simple program. When the startup process is executed here, it is paused. Wait for the second chapter to be replaced with a floppy disk, and then accept any key input to continue the startup process. Readers of this small program can implement it by themselves. It is best to use static links.
Echo ""
Echo-en "Insert the second disk and press ANY key ..."
Readkey>/dev/null
Echo ""
Decompress the content on the floppy disk in Chapter 2 to the sysroot directory (memory.
Echo "Loading root-archive from floppy ..."
Dd if =/dev/fd0 bs = 1 k | bzip2-d | cpio-idv
Copy the files in initrd to the sysroot/bin directory. In this way, part of the content in the root file system can be placed in initrd (the first floppy disk) because the disk capacity is limited, this is useful when the space of the first floppy disk is left and the space of the second chapter is insufficient.
Echo "Copying :"
For file in bzip2 chroot cp cpio echo readkey; do
Echo-en ""; echo-n $ file
Cp/bin/$ file./bin/$ file
Done
Next, set the/directory to the current directory, that is, sysroot, and execute the init that was just extracted from the rootfs disk.
Echo-en "Something went wrong ..."
/Bin/sh |/mnt/initrd/bin/sh
After all the required initrd files are put in the bootldr/initrd/local directory, you can run bootldr/initrd/mkinitrd. sh to create the initrd image file. The content of mkinitrd. sh is:
Format the memory block of 4 MB to ext2 and mount it to bootldr/initrd/ramdisk.
Dd if =/dev/zero of =/dev/ram9 bs = 1 k count = 4096
Mke2fs/dev/ram9
Mount-t ext2/dev/ram9 ramdisk/
Copy the local file to the ramdisk directory, that is, the memory.
Cp-R local/* ramdisk/
Umount ramdisk
Compress the content in the memory to initrd.gz and copy it to the bootldr disk.
Dd if =/dev/ram9 bs = 1 k | gzip-v9> initrd.gz
Cp initrd.gz/mnt/floppy/
Umount/mnt/floppy
In this way, the bootldr disk is complete.
6. Customize the root file system
A root file system must contain all files that support Linux running. It usually includes:
Basic File System Structure
Basic directories:/dev,/proc,/bin,/sbin,/etc,/usr,/tmp, etc.
Basic tools: sh, ls, cp, cd, mv, etc.
Basic configuration files: rc, inittab, and fstab.
Device:/dev/hd *,/dev/tty *,/dev/fd0,/dev/ram *,/dev/console, etc.
Basic Runtime Library.
Busybox and Tinylogin are commonly used in embedded systems. They contain the commonly used tools and directory structures mentioned above, besides, the Code generated after rewriting is much smaller than the tool on a common Linux system.
Edit the Config. h file of Busybox and select the required tool. Modify the Makefile files of Busybox and Tinylogin, and set them to use the static Link Method (DOSTATIC = true). In this way, you do not need to add the Runtime Library to the Generated System. Put the compiled Busybox and Tinylogin files into rootfs/local.
In rootfs/local, create the following directories: dev/, tmp/, etc/, proc/
You can copy the devices in/dev to this directory. You only need to copy the necessary devices. For example:
However, be sure to include the required devices/dev/console,/dev/kmem,/dev/mem,/dev/tty,/dev/ram0, /dev/null.
The etc/directory contains the configuration files required for running the target system, including content dependencies and programs to be run by the target system. Minimum, it includes
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service