构建自己的Linux 之一 基本框架

2019-07-12 17:15发布

构建自己的Linux 之一 基本框架

自己动手构建一个Linux不仅可以加深对Linux的理解,对嵌入式Linux的学习也有很大的帮助。构建一个自己的Linux(内核的编译,文件系统的制作,图形界面,驱动等)和开发嵌入式Linux的过程基本一样。

1开发环境

RedFlag Linux 6.0 VMware 7.1.3build-324285

2 添加磁盘

添加一个新的虚拟硬盘,硬盘类型选择“IDE”,如下图所示:   注意:一定选择IDE类型的虚拟磁盘,否则系统无法识别磁盘,会无法挂载文件系统!,在initrd.img中还要插入相应的内核模块。

3查看磁盘

启动虚拟机,在终端下查看刚刚添加的硬盘 [root@localhost ~]# fdisk -l ... Disk /dev/sdc: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 cylinders Units = cylinders of 16065 * 512 = 8225280bytes Disk /dev/sdc doesn't contain a validpartition table

4给硬盘分区

[root@localhost ~]# fdisk /dev/sdc Device contains neither a valid DOSpartition table, nor Sun, SGI or OSF disklab el Building a new DOS disklabel. Changes willremain in memory only, until you decide to write them. After that,of course, the previous content 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 could in certain setups cause 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 //添加一个新的分区 Command action e extended p primary partition (1-4) p //选择主分区 Partition number (1-4): 1 First cylinder (1-1044, default 1): 1 Last cylinder or +size or +sizeM or +sizeK(1-1044, default 1044): +100M//boot分区为100M Command (m for help): n Command action e extended p primary partition (1-4) p //添加一个新的分区 Partition number (1-4): 2 First cylinder (14-1044, default 14): 14 Last cylinder or +size or +sizeM or +sizeK(14-1044, default 1044): +2048M//主分区为2G Command (m for help): w //写入分区表 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.

5查看分区

[root@localhost ~]# fdisk -l Disk /dev/sdc: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 cylinders Units = 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格式化为ext3格式

[root@localhost ~]# mke2fs -j /dev/sdc1 [root@localhost ~]# mke2fs -j /dev/sdc2

7安装grub

[root@localhost ~]#mkdir /mnt/boot [root@localhost ~]#mkdir /mnt/sysroot [root@localhost ~]# mount /dev/sdc1/mnt/boot //挂载分区 [root@localhost ~]# mount /dev/sdc2/mnt/sysroot//挂载分区 [root@localhost ~]# grub-install--root-directory=/mnt/ /dev/sdc //安装grub 修改/mnt/boot/grub/device.map,只留下(hd0)   /dev/sda

 8修改initrd

关于initrd的更多的资料,可以参看我前面写的文章(linux 2.6内核initrd.img文件分析)。 [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 //建立一个临时目录 [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 //解压initrd.img [root@localhost 1]#cp/lib/modules/2.6.23.1-4/kernel/drivers/scsi/ide-scsi.ko/mnt/boot/1/lib
修改/mnt/boot/1/init如下: #!/bin/nash mount -t proc /proc /proc setquiet echo Mounting proc filesystem echo Mounting sysfs filesystem mount -t sysfs /sys /sys echo > /sys/power/suspend2/do_resume echo Creating /dev mount -o mode=0755 -t tmpfs /dev /dev mkdir /dev/pts mount -t devpts -o gid=5,mode=620 /dev/pts/dev/pts mkdir /dev/shm mkdir /dev/mapper echo Creating initial device nodes mknod /dev/null c 1 3 mknod /dev/zero c 1 5 mknod /dev/systty 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. hotplug echo Creating block device nodes. mkblkdevs echo "Loading uhci-hcd.ko module" insmod /lib/uhci-hcd.ko echo "Loading ohci-hcd.ko module" insmod /lib/ohci-hcd.ko echo "Loading ehci-hcd.ko module" insmod /lib/ehci-hcd.ko mount -t usbfs /proc/bus/usb /proc/bus/usb echo "Loading jbd.ko module" insmod /lib/jbd.ko echo "Loading ext3.ko module" insmod /lib/ext3.ko echo "Loading BusLogic.ko module" insmod /lib/BusLogic.ko echo "Loading libata.ko module" insmod /lib/libata.ko echo "Loading ata_piix.ko module" insmod /lib/ata_piix.ko echo Waiting for driver initialization. stabilized --hash --interval 250/proc/scsi/scsi echo "Loading ide-core.ko module" insmod /lib/ide-core.ko echo "Loading ide-disk.ko module" insmod /lib/ide-disk.ko echo "Loading ide-scsi.ko module"//添加SCSI硬盘驱动模块,增加IDE设备对SCSI的模拟,否则无法挂载文件系统 insmod /lib/ide-scsi.ko mkblkdevs echo Creating root device. mkrootdev -t ext3 -o defaults,rw sda2 // 生成根目录设备 注意:一定要以rw(可读写)方式挂载,否则系统启动后无法修改和创建文件 echo Mounting root filesystem. mount /sysroot echo Setting up other filesystems. setuproot echo Switching to new root and runninginit. switchroot
修改完成后,下面生成新的initrd.img,先删除旧的initrd.img。 [root@localhost 1]#del initrd.img [root@localhost 1]# find ./ | cpio -c -o> initrd.img 26626 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再次修改grub

[root@localhost ~]#cp /boot/message/mnt/boot/message //引导画面,可以去掉 [root@localhost ~]#cp /boot/grub/grub.conf/mnt/boot/grub/grub.conf
修改如下: default=0 timeout=20 gfxmenu (hd0,0)/message title EmbeddedCE123 root(hd0,0) kernel/vmlinuz ro root=/dev/sda2 initrd/initrd.img

10构建文件系统

10.1命令移植

[root@localhost~]# cd /mnt/sysroot/ [root@localhostsysroot]# mkdir -pv root bin sbin usr/{bin,sbin} lib dev tmp proc sys home mnt media boot //创建文件系统的基本框架 复制基本的命令 init bash。 [root@localhost sysroot]#cp /bin/bash /bin/bash [root@localhostsysroot]#cp/sbin/init /sbin/init
查看命令所依赖的可文件: [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)
在宿主机中找到这些文件并拷贝到相应的目录下。同理,移植init命令。最后,这两个命令所需的库文件如下: libc-2.6.so libdl.so.2 libtermcap.so.2 ld-2.6.so libc.so.6 libselinux.so.1 libtermcap.so.2.0.8 ld-linux.so.2 libdl-2.6.so libsepol.so.1

10.2配置文件(/etc目录下的文件)

inittab文件的内容如下: id:3:initdefault: si::sysinit:/etc/rc.d/rc.sysinit fstab文件的内容如下: /dev/sda2 / ext3 defaults 0 0 /dev/sda1 /boot ext3 defaults 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 tmpfs /tmp tmpfs defaults 0 0 tmpfs /dev tmpfs defaults 0 0 var /dev tmpfs defaults 0 0 rc.d/rc.sysinit文件的内容如下: #!/bin/bash echo -e " Welcome to EmbeddedCE123 Linux" /bin/bash 给rc.sysinit赋执行权限 [root@localhostsysroot]# chmod +x /etc/rc.d/rc.sysinit

11测试

新建一个虚拟机,选择linux2.6xx ,挂载上这块硬盘。效果如下图所示: