Build one of the basic Linux frameworks
Building a Linux by yourself can not only deepen your understanding of Linux, but also help you learn embedded Linux. The process of building a Linux (kernel compilation, file system creation, graphical interface, driver, etc.) is basically the same as that of developing an embedded Linux.
1. Development Environment
Redflag Linux 6.0
VMware 7.1.3build-324285
2. Add a disk
Add a new virtual hard disk and select "ide" as the hard disk type, as shown in:
Note: You must select an ide-type virtual disk. Otherwise, the system cannot identify the disk and the file system cannot be mounted !, Insert the corresponding kernel module in initrd. IMG.
3. View Disks
Start the VM and view the added hard disk under the terminal
[root@localhost ~]# fdisk -l...Disk /dev/sdc: 8589 MB, 8589934592 bytes255 heads, 63 sectors/track, 1044 cylindersUnits = cylinders of 16065 * 512 = 8225280bytes Disk /dev/sdc doesn't contain a validpartition table
4. Partitioning Hard Disks
[Root @ localhost ~] # Fdisk/dev/sdcdevice contains neither a valid dospartition table, Nor Sun, SGI or OSF disklabelbuilding a new dos disklabel. changes willremain in memory only, until you decide to write them. after that, of course, the previuscontent won't be recoverable. the number of cylinders for this disk isset to 1044. there is nothing wrong with that, but thisis larger than 1024, and cocould in certain setups ca Use problemswith: 1) software that runs at boot time (e.g ., old versions of lilo) 2) booting and partitioning software fromother OSS (e.g ., DOS fdisk, OS/2 fdisk) Warning: Invalid flag 0x0000 of partitiontable 4 will be corrected by W (RITE) Command (M for help ): N // Add a new partition command Action E extended P primary partition (1-4) P // select the primary partition Partition Number (1-4 ): 1 first cylinder (1-1044, default 1): 1 last C Ylinder or + size or + sizem or + sizek (1-1044, default 1044): + 100 m // The Boot partition is 100 m command (M for help ): ncommand Action E extended P primary partition (1-4) P // Add a new partition Partition Number (1-4): 2 first cylinder (14-1044, default 14 ): 14 last cylinder or + size or + sizem or + sizek (14-1044, default 1044): + 2048 M // The primary partition is 2G command (M for help ): W // write the partition table has been altered! Calling IOCTL () to re-read Partition Table. syncing disks.
5. View partitions
[root@localhost ~]# fdisk -l Disk /dev/sdc: 8589 MB, 8589934592 bytes255 heads, 63 sectors/track, 1044 cylindersUnits = cylinders of 16065 * 512 = 8225280bytes Device Boot Start End Blocks Id System/dev/sdc1 1 13 104391 83 Linux/dev/sdc2 14 263 2008125 83 Linux
6 format to ext3 format
[root@localhost ~]# mke2fs -j /dev/sdc1[root@localhost ~]# mke2fs -j /dev/sdc2
7. Install grub
[Root @ localhost ~] # Mkdir/mnt/boot [root @ localhost ~] # Mkdir/mnt/sysroot [root @ localhost ~] # Mount/dev/sdc1/mnt/boot // mount the partition [root @ localhost ~] # Mount/dev/sdc2/mnt/sysroot // mount the partition [root @ localhost ~] # Grub-install -- root-directory =/mnt // DEV/SDC // install grub
Modify/mnt/boot/GRUB/device. Map and leave only (hd0)/dev/SDA
8. Modify initrd
For more information about initrd, see my previous article (Linux 2.6 kernel initrd. imgfile analysis ).
[Root @ localhost ~] # Cp/boot/vmlinuz-2.6.23.1-4/mnt/boot/vmlinuz [root @ localhost ~] # Cp/boot/initrd-2.6.23.1-4.img/mnt/boot/initrd. IMG [root @ localhost ~] # Mkdir/mnt/boot/1 // create a temporary directory [root @ localhost ~] # Cp/mnt/boot/initrd. img/mnt/boot/New/initrd. IMG [root @ localhost ~] # Cd/mnt/boot/1 [root @ localhost 1] # gzip-DC initrd. IMG | cpio-idvm // decompress initrd. IMG [root @ localhost 1] # cp/lib/modules/2.6.23.1-4/kernel/Drivers/SCSI/ide-scsi.ko/mnt/boot/1/lib
Modify/mnt/boot/1/init as follows:
#! /Bin/Nash Mount-T proc/procsetquietecho mounting proc filesystemecho mounting sysfs filesystemmount-T sysfs/sys/sysecho>/sys/power/suspend2/do_resumeecho creating/devmount-o mode = 0755-T tmpfs/dev/devmkdir/dev/ptsmount-T devpts-O gid = 5, mode = 620/dev/pts/dev/ptsmkdir/dev/shmmkdir/dev/mapperecho creating initial device nodesmknod/dev/null C 1 3 mknod/dev/Zero C 1 5 mknod/ dev/shortty C 4 0 mknod/dev/tty C 5 0 mknod/dev/console C 5 1 mknod/dev/ptmx C 5 2 mknod/dev/rtc c 10 135 mknod/ dev/tty0 C 4 0 mknod/dev/tty1 C 4 1 mknod/dev/tty2 C 4 2 mknod/dev/tty3 C 4 3 mknod/dev/tty4 C 4 4 mknod/ dev/tty5 C 4 5 mknod/dev/tty6 C 4 6 mknod/dev/tty7 C 4 7 mknod/dev/tty8 C 4 8 mknod/dev/tty9 C 4 9 mknod/ dev/tty10 C 4 10 mknod/dev/tty11 C 4 11 mknod/dev/tty12 C 4 12 mknod/dev/ttys0 C 4 64 mknod/dev/ttys1 C 4 65 mknod/ dev/ttys2 C 4 66 mknod/dev/ttys3 C 4 67 echo setting up hotplug. hotplugecho creating block device nodes. mkblkdevsecho "loading uhci-hcd.ko module" insmod/lib/uhci-hcd.koecho "loading ohci-hcd.ko module" insmod/lib/ohci-hcd.koecho "loading ehci-hcd.ko module" insmod/lib/ehci-hcd.komount-T usbfs/proc/bus/USB /proc/bus/usbecho "loading jbd. ko module "insmod/lib/jbd. koecho "loading ext3.ko module" insmod/lib/ext3.koecho "loading buslogic. ko module "insmod/lib/buslogic. koecho "loading libata. ko module "insmod/lib/libata. koecho "loading ata_piix.ko module" insmod/lib/ata_piix.koecho waiting for driver initialization. stabilized -- hash -- interval 250/proc/SCSI/scsiecho "loading ide-core.ko module" insmod/lib/ide-core.koecho "loading ide-disk.ko module" insmod/lib/ide-disk.koecho "loading ide-scsi.ko module" // Add SCSI hard drive module, increase the simulation of SCSI by the IDE device, otherwise the file system insmod/lib/ide-scsi.komkblkdevsecho creating root device cannot be mounted. mkrootdev-T ext3-O defaults, RW sda2 // generate the root directory device. Note: It must be mounted in RW (read/write) mode, otherwise, the echo mounting root filesystem file cannot be modified or created after the system is started. mount/sysrootecho setting up other filesystems. setuprootecho switching to New Root and runninginit. switchroot
After the modification, a new initrd. IMG is generated and the old initrd. IMG is deleted.
[root@localhost 1]#del initrd.img[root@localhost 1]# find ./ | cpio -c -o> initrd.img26626 blocks[root@localhost 1]# gzip -9 initrd.img[root@localhost 1]# mv initrd.img.gzinitrd.img[root@localhost 1]#cp initrd.img /mnt/boot/initrd.img
9 modify grub again
[Root @ localhost ~] # Cp/boot/message/mnt/boot/message // boot screen, which can be removed from [root @ localhost ~] # Cp/boot/GRUB/grub. CONF/mnt/boot/GRUB/grub. conf
Modify as follows:
default=0timeout=20gfxmenu (hd0,0)/messagetitle EmbeddedCE123 root(hd0,0) kernel/vmlinuz ro root=/dev/sda2 initrd/initrd.img
10 build a file system 10.1 command port
[Root @ localhost ~] # Cd/mnt/sysroot/[root @ localhostsysroot] # mkdir-PV root bin sbin usr/{bin, sbin} lib Dev TMP proc sys home MNT media boot // basic framework for creating a File System
Copy the basic command init bash.
[root@localhost sysroot]#cp /bin/bash /bin/bash[root@localhostsysroot]#cp/sbin/init /sbin/init
View the files that the command depends on:
[root@localhost sysroot]# ldd/bin/bash linux-gate.so.1 => (0xb7f2d000) libtermcap.so.2 =>/lib/libtermcap.so.2 (0xb7f12000) libdl.so.2 => /lib/libdl.so.2 (0xb7f0d000) libc.so.6 => /lib/libc.so.6(0xb7db6000) /lib/ld-linux.so.2 (0xb7f2e000)
Find these files on the host and copy them to the corresponding directory. Similarly, port the init command. Finally, the library files required for these two commands are as follows:
libc-2.6.so libdl.so.2 libtermcap.so.2ld-2.6.so libc.so.6 libselinux.so.1 libtermcap.so.2.0.8ld-linux.so.2 libdl-2.6.so libsepol.so.1
10.2 configuration file (file under the/etc directory)
The content of the inittab file is as follows:
id:3:initdefault:si::sysinit:/etc/rc.d/rc.sysinit
The content of the fstab file is as follows:
/dev/sda2 / ext3 defaults 0 0/dev/sda1 /boot ext3 defaults 0 0sysfs /sys sysfs defaults 0 0proc /proc proc defaults 0 0tmpfs /tmp tmpfs defaults 0 0tmpfs /dev tmpfs defaults 0 0var /dev tmpfs defaults 0 0
The content of the rc. d/rc. sysinit file is as follows:
#!/bin/bash echo -e " Welcome to EmbeddedCE123 Linux"/bin/bash
Grant the execution permission to RC. sysinit
[root@localhostsysroot]# chmod +x /etc/rc.d/rc.sysinit
11 Testing
Create a new virtual machine and select linux2.6xx to mount the hard disk. Shows the effect: