构建自己的Linux 之一 基本框架
来源:互联网 发布:戏曲服装淘宝网 编辑:程序博客网 时间:2024/05/22 10:35
构建自己的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 bytes255 heads, 63 sectors/track, 1044 cylindersUnits = cylinders of 16065 * 512 = 8225280bytes Disk /dev/sdc doesn't contain a validpartition table
4给硬盘分区
[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 previouscontent 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): 1Last cylinder or +size or +sizeM or +sizeK(1-1044, default 1044): +100M//boot分区为100M Command (m for help): nCommand action e extended p primary partition (1-4)p //添加一个新的分区Partition number (1-4): 2First cylinder (14-1044, default 14): 14Last 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 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格式化为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 /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 3mknod /dev/zero c 1 5mknod /dev/systty c 4 0mknod /dev/tty c 5 0mknod /dev/console c 5 1mknod /dev/ptmx c 5 2mknod /dev/rtc c 10 135mknod /dev/tty0 c 4 0mknod /dev/tty1 c 4 1mknod /dev/tty2 c 4 2mknod /dev/tty3 c 4 3mknod /dev/tty4 c 4 4mknod /dev/tty5 c 4 5mknod /dev/tty6 c 4 6mknod /dev/tty7 c 4 7mknod /dev/tty8 c 4 8mknod /dev/tty9 c 4 9mknod /dev/tty10 c 4 10mknod /dev/tty11 c 4 11mknod /dev/tty12 c 4 12mknod /dev/ttyS0 c 4 64mknod /dev/ttyS1 c 4 65mknod /dev/ttyS2 c 4 66mknod /dev/ttyS3 c 4 67echo 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"//添加SCSI硬盘驱动模块,增加IDE设备对SCSI的模拟,否则无法挂载文件系统insmod /lib/ide-scsi.komkblkdevsecho Creating root device.mkrootdev -t ext3 -o defaults,rw sda2 // 生成根目录设备 注意:一定要以rw(可读写)方式挂载,否则系统启动后无法修改和创建文件echo Mounting root filesystem.mount /sysrootecho Setting up other filesystems.setuprootecho 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.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再次修改grub
[root@localhost ~]#cp /boot/message/mnt/boot/message //引导画面,可以去掉[root@localhost ~]#cp /boot/grub/grub.conf/mnt/boot/grub/grub.conf
修改如下:
default=0timeout=20gfxmenu (hd0,0)/messagetitle 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.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配置文件(/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 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
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 ,挂载上这块硬盘。效果如下图所示:
- 构建自己的Linux 之一 基本框架
- 构建自己的Java Web框架(二)之一个Filter引发的血案
- 如何构建自己的网游基本构架
- 构建自己的Java并发模型框架
- 构建自己的MVC框架-MVCLight
- [构建自己的持久层]六、开始开发DAO之一
- 构建struts2框架的基本方法
- Linux From Scratch 构建自己的 Linux
- 如何构建自己的 Linux 发行版
- 快速构建自己的linux发行光盘
- 构建自己的Linux系统记录(一)
- 构建自己的Linux系统记录(二)
- 构建自己的Linux系统记录(三)
- 构建自己的Linux系统反思(一)
- 构建自己的Linux系统记录(四)
- 构建自己的Linux系统记录(五)
- 构建自己的Linux系统反思(二)
- 从头构建自己的Linux系统 -转
- JAR export finished with warnings
- 一个不错的在线软件销售模式
- 把由Word绘图得到的图形转化成latex/beamer中可用的pdf格式的矢量图
- 动态规划-poj1088 滑雪 ---增加了最优路径构建
- 好玩的自助建站
- 构建自己的Linux 之一 基本框架
- 网页弹出层并引起回调
- 拉格朗日乘子(百度百科)
- 构建自己的Linux 之二 BusyBox移植
- Social Media Expert社交媒体专家真的存在么?
- 用C++写的三子棋代码(基于DOS界面)
- 使用ANSI C and Microsoft C++中常用的预定义宏
- 团购在升温
- 用C++写的五子棋代码(基于DOS界面)