10003. Take the Linux system on your own floppy disk (reproduced)

xiaoxiao2021-03-06  86

LINUX system on your own floppy disk

2004-04-23 15:18 PM

Authors: from: Linux knowledge base http://www.douzhe.com/docs/linux/Contact:. Unnamed embedded Linux a foreword by a few hundred KB of Linux kernel and constructed in accordance with the need to develop a file system Since Linux is an open source operating system, there is a very broad prospect in the embedded field, and it has been widely used in many electronic products such as mobile phones, PDAs, MP3 players. This article will introduce a Linux system on two floppy disks, which can be used as a system emergency repair disk, router, or firewall, etc., by research on it, can deepen the understanding of embedded systems. II.Linux startup process All PCs After power-on, the BIOS will find the first sector of the boot disk and copy it to the RAM to perform it, for two different start-up mode, this sector usually contains Two different codes: boot procedures (such as LILO or GRUB, etc.) code, bootstrap, will help locate the kernel. The code of the kernel, which is usually the way of boot used when starting from the floppy disk. For the former, the kernel is usually required to support Initrd. If it is the latter, the boot loader used is Arch / i386 / boot / bootsect.s. When the kernel is compiled, this execution code is linked to the most beginning of the kernel image. This is easy to copy the kernel to a floppy disk that can be activated on the floppy disk of the first sector. The kernel will initialize the device driver and internal data structure, which will then go to a specific location -RamDisk Word to get the location of the root file system. The kernel must know where to find this root file system, otherwise it will stop. When using the floppy disk start, the kernel can release a compressed file system into the RAM, called RAMDisk, which is a memory area, but the core will use it as a disk as a disk. The examples described herein use GRUB as a boot program and use initrd to assist in LINUX start. Two floppy disks are named bootldr disc and rootfs, in the bootldr disk, which is grub, kernel, initrd, and rootfs, is a compressed root file system. When the system starts, the GRUB of the bootldr disk is positioned and executed, then the kernel unssessed the initrd and executed the LinuxRC file. This file is responsible for prompting the user to replace the rootfs disk and extracting the content into the memory, and then performs the Just decompressed init. Startup process.

In order to facilitate this example, first introduce the directory structure as follows: / Home / Papaya├ - Bootldr / │ ├─GRUB / │ ├─kernel / │ │ │ │ │ ─Linux-2.4.21 / │ └ -initrd / │ ├─Mkinitrd.sh│ ├─Local / │ └ - L│Disk / ├─rootfs / │ ├─mkrootfs.sh│ ├─ramdisk / │ └ -local / └ --lib / 3. Custom GRUB boot program insertion Floppy disk, then format it, then load it into / mnt / floppy # mou2fs / dev / fd0 # mount -t ext2 / dev / fd0 / mnt / floppy -o loop Create / Boot / GRUB directory #mkdir -p / MNT / FLOPPY / BOOT / GRUB copies the device.map, stage1, stage2 under the system / boot / grub to / mnt / floppy / boot / grub and then creates GRUB in the / mnt / floppy / boot / grub directory. Conf file: default = 0timeout = 10TITLE FLOPPY Linuxkernel (fd0) / bzimage root = / dev / ram0 initrd (fd0) /initrd.gz then create a link #LN -s grub.conf menu.lst execution / sbin / grub - Batch - DEVICE-MAP = / Dev / Null << EOFDEVICE (FD0) / dev / fd0root (fd0) setup (fd0) QUITEOF This GRUB is installed on the bootldr disk. IV. Customized Linux kernels Due to the limit of floppy disk, the internal nuclear should contain only some of the necessary support as much as possible, and for the example in this article, it must be selected. For example, if you do a system repair disk, the necessary support includes: IDE, PCI, and required file system types, etc., without necessary network support, of course, if you do a router or firewall, network support is necessary Other this can be deleted accordingly. #make [xconfig | menuconfig | config] #make bzimage If you add a module support, you will need to get the kernel image BZImage after #make modules. If the size of the BZImage exceeds the restriction of the floppy disk, you need to re-configure it. Place the compiled BZIMAGE in the root directory of the bootldr disk, if you change the BZImage, pay attention to the names in Grub.conf. 5. Develop initrd to establish BIN, DEV, ETC, LIB, PROC, SYSROOT, and USR directory in the initrd / local directory. Where the necessary device files, such as TTY, RAM, CONSOLE, and more, BIN, the necessary executables, BNIP2, Chroot, CP, CPIO, DD, Echo, Mount, Pivot_Root, Readkey, SH, TEST, etc. Busybox provides most of them. BZIP2, DD, CPIO is used to extract the contents on the second floppy disk, chroot, pivot_root used to convert the root directory. Edit the initrd / local / linuxrc file: #! / Bin / sh Put the sysroot directory MOUNT to a memory and establishes a TMPFS file system.

Echo "Mounting New Root Filsystem ..." Mount TMPFS / SYSROOT -T TMPFSCD / SYSROOT below readkey is a very simple program that is paused when the startup process is executed, waiting for the second chapter floppy disk, and accept any The key input continues to perform the startup process. This small program reader can implement itself, pay attention to it is preferred to use a static link. Echo "Echo -en" Insert The Second Disk and press any key ... "readkey> / dev / nullecho" Differentize the contents of the second chapter floppy to the sysroot directory (memory). Echo "Loading root-archive from floppy ..." DD if = / dev / fd0 bs = 1k | bzip2 -d | cpio -idv under the file copy to the sysroot / bin directory, this can be used Part of the content is placed in the initrd (first floppy disk), because the floppy disk capacity is limited, and it will be very useful when the first floppy disk space is rest, and the second chapter is very useful. echo "Copying:" For File In Bzip2 Chroot CP CPIE Echo Readkey; Doecho -en ""; Echo -n $ filecp / bin / $ file ./bin/$filed below The / Directory is set to the current directory, namely sysroot, And execute the init from the rootfs disc. Echo "" echo "pivoting / ..." pivot_root. mnt / initrdecho "Starting Init Process ..." Exec chroot. / sbin / init / dev / console 2> & 1echo -en "Something Went Wrong. .. "/ bin / bin / month When all the necessary files that INitd are placed in the bootldr / initrd / local directory, bootldr / initrd / mkinitrd.sh can be executed to create an initrd image file. . The content of mkinitrd.sh is: #! / bin / shmount -t ext2 / dev / fd0 / mnt / floppyrm -f /mnt/floppy/initrd.gzrm -f initrd.gz Take 4M size memory block formatted into EXT2 format And put it on bootldr / initrd / ramdisk. DD if = / dev / zero of = / dev / ram9 bs = 1k count = 4096MKE2FS / dev / ram9mount -t ext2 / dev / ram9 ramdisk / copy file in local to the Ramdisk directory, that is, the memory. Cp -r local / * ramdisk / umount ramdisk compresses content in memory to initrd.gz, and copy to the bootldr disk DD if = / dev / ram9 bs = 1k | gzip -v9> initrd.gzcp initrd.gz / mNT / floppy / umount / mnt / floppy, the bootldr disk is completed. 6. Custom root file system A root file system needs to include all files that support the Linux system.

Typically include: basic directory of basic file system structure: / dev, / proc, / bin, / sbin, / etc, / usr, / tmp, etc. Basic tools: SH, LS, CP, CD, MV, etc. Basic configuration files: RC, INITTAB, FSTAB, etc. Equipment: / dev / hd *, / dev / tty *, / dev / fd0, / dev / ram *, / dev / console, etc. Basic runtime. BusyBox and TinyLogin are commonly used in embedded systems, which contain common tools and directory structures mentioned above, etc., and the code generated after rewriting is much smaller than the tools on ordinary Linux systems. . Edit busybox's config.h file, choose the tool you need. Modify the Makefile file of busybox and tinylogin, develop them using static link modes (dostatic = true), so you don't need to add a run library in the generated system. Place the Compilation Busybox and Tinylogin files in rootfs / local. Create the following directory in Rootfs / local: DEV /, TMP /, ETC /, PROC / can copy the device under / dev to this directory, only need to copy the necessary, for example: #CP -DPR / DEV / TTY [0-9] / mnt / rootfs / dev # cp -dpr / dev / ram * / mnt / rootfs / dev But Be careful to include all devices / dev / console, / DEV / KMEM, / DEV / MEM, / DEV / TTY, / DEV / RAM0, / DEV / NULL, etc. The ETC / Directory contains the configuration files necessary for the target system run, which includes the program that relies on the program to run with the target system. Minimum, it includes several files below: inittab, rc, fstab, passwd, group, shadow, termcap, etc. As a parameter of the init, INITTAB can be very simple, just include the following lines ::: sysinit: / etc / rc :: askfirst: / bin / login tty2 :: askfirst: / bin / logintty3 :: askfirst: / bin / logintty4 :: askfirst: / bin / login :: ctrlatdel: / sbin / reboot :: shutdown: / bin / umount -a -r :: shutdown: / sbin / swapoff -a SYSINIT specifies the system initialization script RC. The contents of the RC include very few: #! / bin / sh / bin / mount -av / bin / umount / mnt / initrd / bin / hostname Papayafstab content is: None / Proc Proc defaults 0 0none / TMP TMPFS DEFAULTS 0 0 Other profiles can be obtained from the original system, then repair unnecessary content. Now in / mnt / rootfs already contains all files and tools that are required to run a minimum Linux system, you need to compress them into a file system.

Insert the rootfs floppy disk and perform bootldr / rootfs / mkrootfs.sh #! / Bin / shrm -f rootfs.cpio.bz2dd if = / dev / zero of = / dev / ram0 BS = 1k count = 4096MKE2FS / DEV / RAM0MOUNT -T2 / dev / ram0 ramdisk / cp -r local / * ramdisk / cd Ramdisk / Find. -Depth -print | cpio -o> ../rootfs.cpiocd ..bzip2 rootfs.cpioumount ramdiskddix if = rootfs.cpio.bz2 of = / dev / fd0 bs = 1kok, the rootfs is also completed, and the machine can be restarted. 7. Other methods integrate the kernel to the file system. If there are two options without the GRUB boot, the root file system cannot be packaged and compressed as above, nor will I use initrd. Put all root file system files in a directory (such as rootfs / local), then execute dd if = / dev / zero of = / dev / ram0 bs = 1k count = 4096MKE2FS / DEV / RAM0MOUNT -T EXT2 / DEV / ram0 ramdisk / cp -r local / * ramdisk / umount ramdiskddix if = / / dev / ram0 bs = 1k | gzip -v9> rootfs.gz1. Place the kernel and file system on a floppy disk to determine the size and size of the kernel There is no limit to the floppy disk. Remember the size of the kernel, then write the kernel to the floppy disk: #dd if = bzimage of = / dev / fd0 BS = 1k353 1 Records in353 1 Records OUT, set the root device as the floppy disk itself, and set the root to read and write. RDEV / DEV / FD0 / DEV / FD0 # RDEV -R / DEV / FD0 0 The above example indicates that the DD writes 353 full records and a portion of the record to the floppy disk, so the core occupies the first 354 record blocks of the floppy disk. Remember this number, then set the root of RamDisk Word. RamDisk Word can be set via the RDEV command, which is: If 15 bit settings, the kernel will prompt before loading the file system, which is necessary to separate the kernel and the file system disk. For the above situation, it is necessary to indicate the offset of RAMDISK at 0-10, and the 14 position 1, so the resulting RAMDisk Word decimal is represented as: 355 2 ^ 14 = 355 16384 = 16739 # rdev -r / dev / fd0 16739 after #dd if = rootfs.gz of = / dev / fd0 bs = 1K seek = 354 This is successful in the same time. 2. The kernel and file system occupies a floppy disk as the above #dd if = bzimage of = / dev / fd0 BS = 1k # rdev / dev / fd0 / dev / fd0 # rdev -r / dev / fd0 0 is different Ramdisk Word 0 2 ^ 14 2 ^ 15 = 49152 # rdev -r / dev / fd0 49152 then change one floppy disk #dd if = rootfs.gz of = / dev / fd0 BS = 1K core, root file system There are a lot of integration methods between boot procedures, and they have their own characteristics, and they have to think about themselves.

转载请注明原文地址:https://www.9cbs.com/read-98562.html

New Post(0)