Linux kernel + busybox自制Linux系统
Linux kernel + busybox自制Linux系统
如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:129518033
文章目录
- Linux kernel + busybox自制Linux系统
- 前言
- 1.下载和编译Linux kernel
- 1.1 下载Linux内核
- 1.2 安装所需前置软件
- 1.3 设置CPU架构(★)
- 1.4 设置系统选项
- 1.5 编译
- 2.下载和编译busybox
- 2.1 下载busybox
- 2.2 设置CPU架构(★)
- 2.3 设置系统选项
- 2.4 编译
- 3.制作ramdisk根文件系统rootfs
- 3.1 创建根目录所需的目录
- 3.2 创建根目录所需的必要文件
- 3.3制作根文件系统镜像文件rootfs.img.gz
- 4.Linux系统运行
相关阅读:
1.linux内核之源码编译
相关问题:
1.自制操作系统
2.制作简单的Linux系统
3.Linux系统制作流程
环境:
OS:Ubuntu 20.04
gcc: gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)
Linux Kernel: linux-5.4.50(2020-07-01 08:50)
busybox: busybox-1.32.0(2020-06-26 19:30)
qemu: qemu-system-x86_64 4.2.0
前言
自制x86_64架构的Linux系统
1.下载和编译Linux kernel
1.1 下载Linux内核
Linux内核官网:https://www.kernel.org/
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.4.50.tar.xz
或
wget https://mirror.bjtu.edu.cn/kernel/linux/kernel/v5.x/linux-5.4.50.tar.xz
1.2 安装所需前置软件
sudo apt-get install fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison
1.3 设置CPU架构(★)
这里以x86_64架构为例。常用的架构还有arm64(鲲鹏、飞腾)、mips(龙芯)、alpha(申威)
# 设置CPU架构
$ export ARCH=x86_64# 设置交叉编译工具链前缀
$ export CROSS_COMPILE=x86_64-linux-gnu-
1.4 设置系统选项
# 生成x86_64的默认.config
$ make x86_64_defconfig# 设置系统选项
$ make menuconfig
以下的配置选项是为了支持ramdisk根文件系统:
General setup --->...[*] Initial RAM filesystem and RAM disk (initramfs/initrd) support......Device Drivers ---> ...[*] Block devices ---> ....<*> RAM block device support(16) Default number of RAM disks (65536) Default RAM disk size (kbytes)...........
Linux kernel make详细帮助:
$ make help
Cleaning targets:
...Configuration targets:config - Update current config utilising a line-oriented programnconfig - Update current config utilising a ncurses menu based programmenuconfig - Update current config utilising a menu based programxconfig - Update current config utilising a Qt based front-endgconfig - Update current config utilising a GTK+ based front-endoldconfig - Update current config utilising a provided .config as baselocalmodconfig - Update current config disabling modules not loadedlocalyesconfig - Update current config converting local mods to coredefconfig - New config with default from ARCH supplied defconfigsavedefconfig - Save current config as ./defconfig (minimal config)allnoconfig - New config where all options are answered with noallyesconfig - New config where all options are accepted with yesallmodconfig - New config selecting modules when possiblealldefconfig - New config with all symbols set to defaultrandconfig - New config with random answer to all optionslistnewconfig - List new optionsolddefconfig - Same as oldconfig but sets new symbols to theirdefault value without promptingkvmconfig - Enable additional options for kvm guest kernel supportxenconfig - Enable additional options for xen dom0 and guest kernel supporttinyconfig - Configure the tiniest possible kerneltestconfig - Run Kconfig unit tests (requires python3 and pytest)...Architecture specific targets (x86):
* bzImage - Compressed kernel image (arch/x86/boot/bzImage)install - Install kernel using(your) ~/bin/installkernel or(distribution) /sbin/installkernel orinstall to $(INSTALL_PATH) and run lilofdimage - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)fdimage144 - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)fdimage288 - Create 2.8MB boot floppy image (arch/x86/boot/fdimage)isoimage - Create a boot CD-ROM image (arch/x86/boot/image.iso)bzdisk/fdimage*/isoimage also accept:FDARGS="..." arguments for the booted kernelFDINITRD=file initrd for the booted kerneli386_defconfig - Build for i386x86_64_defconfig - Build for x86_64make V=0|1 [targets] 0 => quiet build (default), 1 => verbose buildmake V=2 [targets] 2 => give reason for rebuild of targetmake O=dir [targets] Locate all output files in "dir", including .configmake C=1 [targets] Check re-compiled c source with $CHECK (sparse by default)make C=2 [targets] Force check of all c source with $CHECKmake RECORDMCOUNT_WARN=1 [targets] Warn about ignored mcount sectionsmake W=n [targets] Enable extra build checks, n=1,2,3 where1: warnings which may be relevant and do not occur too often2: warnings which occur quite often but may still be relevant3: more obscure warnings, can most likely be ignoredMultiple levels can be combined with W=12 or W=123Execute "make" or "make all" to build all targets marked with [*]
For further info see the ./README file
1.5 编译
AMD 4800H CPU 8G内存16线程大约需要3分钟编译
# 根据CPU线程数设置
$ make -j 16
最后编译完成的内核位于arch/x86/boot/bzImage
$ ls -l arch/x86/boot/bzImage
-rw-rw-r-- 1 dev dev 8897920 8月 1 16:08 arch/x86/boot/bzImage
至此,Linux kernel编译完成
linux-5.4.50文件夹编译完的磁盘占用情况
$ du -lh --max-depth=1
208K ./LICENSES
48M ./Documentation
12M ./lib
43M ./sound
41M ./tools
824K ./init
6.7M ./crypto
780K ./virt
8.9M ./mm
48M ./include
25M ./kernel
5.5M ./scripts
120K ./certs
61M ./fs
215M ./arch
1.6M ./samples
6.1M ./security
63M ./net
128K ./usr
4.4M ./block
1020K ./ipc
681M ./drivers
1.5G .
2.下载和编译busybox
busybox 将许多具有共性的小版本的UNIX工具结合到一个单一的可执行文件。这样的集合可以替代大部分常用工具比如的GNU fileutils,shellutils等工具,busybox提供了一个比较完善的环境,可以适用于任何小的嵌入式系统。
2.1 下载busybox
下载地址:https://busybox.net/downloads/
wget https://busybox.net/downloads/busybox-1.32.0.tar.bz2
2.2 设置CPU架构(★)
这里以x86_64架构为例。
# 设置CPU架构
$ export ARCH=x86_64# 设置交叉编译工具链前缀
$ export CROSS_COMPILE=x86_64-linux-gnu-
2.3 设置系统选项
# 生成默认.config
$ make defconfig# 设置busybox选项
$ make menuconfig
修改选项
...Settings ---> ...# 静态编译busybox--- Build Options [*] Build static binary (no shared libs) ......
bosybox make详细帮助:
$ make help
...Configuration:allnoconfig - disable all symbols in .configallyesconfig - enable all symbols in .config (see defconfig)config - text based configurator (of last resort)defconfig - set .config to largest generic configurationmenuconfig - interactive curses-based configuratoroldconfig - resolve any unresolved symbols in .configandroid2_defconfig - Build for android2cygwin_defconfig - Build for cygwinTEST_nommu_defconfig - Build for TEST_nommuandroid_defconfig - Build for androidTEST_noprintf_defconfig - Build for TEST_noprintfandroid_ndk_defconfig - Build for android_ndkandroid_502_defconfig - Build for android_502TEST_rh9_defconfig - Build for TEST_rh9freebsd_defconfig - Build for freebsdInstallation:install - install busybox into CONFIG_PREFIXuninstall
...
2.4 编译
# 根据CPU线程数设置
$ make -j 16
安装busybox,默认路径为当期目录的_install文件夹
make install
至此,busybox静态编译完成
busybox-1.32.0文件夹编译完的磁盘占用情况
$ du -lh --max-depth=1
636K ./examples
256K ./mailutils
268K ./findutils
2.3M ./archival
236K ./init
460K ./console-tools
5.2M ./networking
232K ./e2fsprogs
7.4M ./include
916K ./procps
1.4M ./docs
2.6M ./_install
216K ./sysklogd
4.0K ./.tmp_versions
1.7M ./scripts
520K ./applets
108K ./selinux
352K ./modutils
492K ./loginutils
180K ./debianutils
3.5M ./util-linux
28K ./arch
924K ./editors
6.9M ./shell
2.2M ./miscutils
2.9M ./coreutils
36K ./qemu_multiarch_testing
120K ./printutils
12K ./applets_sh
4.5M ./libbb
108K ./klibc-utils
112K ./libpwdgrp
252K ./configs
1.7M ./testsuite
408K ./runit
57M .
3.制作ramdisk根文件系统rootfs
基于busybox制作ramdisk根文件系统rootfs.
基于busybox的文件系统启动过程:
/sbin/init => /etc/inittab => /etc/init.d/rdS => /etc/fstab ...
3.1 创建根目录所需的目录
$ cd busybox-1.32.0/_install$ ls
bin linuxrc sbin usr$ mkdir -p dev etc home lib mnt proc root sys tmp var$ ls
bin dev etc home lib linuxrc mnt proc root sbin sys tmp usr var
根文件系统目录说明
/bin: 系统管理员和用户均可使用的命令
/sbin: 系统管理员使用的系统命令
/dev: 存储特殊文件或设备文件;设备两种类型:字符设备、块设备
/etc: 系统配置文件
/home: 普通用户目录
/root:root用户目录
/lib: 为系统启动或根文件上的应用程序(/bin,/sbin等)提供共享库,以及为内核提供内核模块
/mnt:临时挂载点
/tmp: 临时文件存储目录
/usr:usr hierarchy,全局共享的只读数据路径
/var:存储常发生变化的数据目录:cache、log等
/proc: 基于内存的虚拟文件系统,用于为内核及进程存储其相关信息
/sys:sysfs虚拟文件系统提供了一种比proc更为理想的访问内核数据的途径:其主要作用在于为管理linux设备提供一种统一模型的接口;
3.2 创建根目录所需的必要文件
- etc/inittab
$ cd busybox-1.32.0/_install
$ vim etc/inittab
$ chmod 755 etc/inittab
etc/inittab内容:
::sysinit:/etc/init.d/rcS
::respawn:-/bin/sh
::askfirst:-/bin/sh
::cttlaltdel:/bin/umount -a -r
inittab语法:
<id>:<runlevels>:<action>:<process>id : /dev/id
runlevels : 忽略
action : 何时执行,有以下选项 sysinit, respawn, askfirst, wait, once,restart, ctrlaltdel, and shutdown
process : 应用程序或脚本
- etc/init.d/rcS
$ cd busybox-1.32.0/_install
$ mkdir -p etc/init.d/
$ vim etc/init.d/rcS
$ chmod 755 etc/init.d/rcS
etc/init.d/rcS内容:
echo "----------mount all in fstab----------------"
/bin/mount -a #读取/etc/fstab,加载文件系统
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
echo "****************Hello itas109******************"
echo "Kernel Version:linux-5.4.50"
echo "***********************************************"
- etc/fstab
$ cd busybox-1.32.0/_install
vim etc/fstab
etc/fstab内容:
#device mount-point type option dump fsck
proc /proc proc defaults 0 0
temps /tmp rpoc defaults 0 0
none /tmp ramfs defaults 0 0
sysfs /sys sysfs defaults 0 0
mdev /dev ramfs defaults 0 0
- 添加设备文件
$ cd busybox-1.32.0/_install
$ cd dev
$ sudo mknod console c 5 1
$ sudo mknod null c 1 3
$ sudo mknod tty1 c 4 1
3.3制作根文件系统镜像文件rootfs.img.gz
$ cd busybox-1.32.0
$ vim makeRootfs.sh
$ chmod +x makeRootfs.sh
$ ./makeRootfs.sh
记录了32+0 的读入
记录了32+0 的写出
33554432 bytes (34 MB, 32 MiB) copied, 0.0351642 s, 954 MB/s
mke2fs 1.45.5 (07-Jan-2020)
丢弃设备块: 完成
创建含有 8192 个块(每块 4k)和 8192 个inode的文件系统正在分配组表: 完成
正在写入inode表: 完成
创建日志(1024 个块) 完成
写入超级块和文件系统账户统计信息: 已完成
makeRootfs.sh内容
#!/bin/bashsudo rm -rf rootfs.ext3
sudo rm -rf fs
dd if=/dev/zero of=./rootfs.ext3 bs=1M count=32
mkfs.ext3 rootfs.ext3
mkdir fs
sudo mount -o loop rootfs.ext3 ./fs
sudo cp -rf ./_install/* ./fs
sudo umount ./fs
gzip --best -c rootfs.ext3 > rootfs.img.gz
4.Linux系统运行
cd ~
$ qemu-system-x86_64 \-kernel ./linux-5.4.50/arch/x86_64/boot/bzImage \-initrd ./busybox-1.32.0/rootfs.img.gz \-append "root=/dev/ram init=/linuxrc"
启动结果:
[ 0.000000] Linux version 5.4.50 (dev@dev) (gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)) #1 SMP Sat Aug 1 16:05:18 CST 2020...****************Hello itas109******************
Kernel Version:linux-5.4.50
***********************************************Please press Enter to activate this console.
/ # uname -a
Linux (none) 5.4.50 #1 SMP Sat Aug 1 16:05:18 CST 2020 x86_64 GNU/Linux
Reference:
NULL
Linux kernel + busybox自制Linux系统相关推荐
- g++ linux 编译开栈_使用 linux kernel +busybox 定制linux系统
写在开头: 本来是想使用linux kernel +busybox 制作一个教程的,后来快要结束的时候,死活找不到硬盘,我了解很多文章都有类似的,但是没有谈到硬盘找不到问题,最后历经艰辛,终于把问题解 ...
- 面具busybox模块_使用 linux kernel +busybox 定制linux系统
目的: 了解linux的启动过程 主要内容: 1.grub 是启动程序的bootloader 2.linux-kernel 是linux的开源内核 3.busybox 是linux的工具集合 启动顺序 ...
- linux kernel 培训,《Linux Kernel培训课件》.pdf
<Linux Kernel培训课件>.pdf Introduction to Linux Kernel Subsystems 中科信软高级技术培训中心- Objectives Unde ...
- Linux Kernel - Debug Guide (Linux内核调试指南 )
linux内核调试指南 一些前言 作者前言 知识从哪里来 为什么撰写本文档 为什么需要汇编级调试 ***第一部分:基础知识*** 总纲:内核世界的陷阱 源码阅读的陷阱 代码调试的陷阱 原理理解的陷阱 ...
- linux kernel 最新版本,linux kernel 最新版本4.10正式发布 附下载地址
欢迎分享本新闻到大家的qq群.qq空间.微信群.微博等. 正如大家所预期的,大神Linus Torvalds今天发布了Linux Kernel 4.10的正式版,新版中带来了大量改善,强化了安全功能, ...
- android linux kernel VS standard linux kernel
在kernel子目录下存放的就是Android的Linux Kernel了, 通过和标准的Linux 2.6.25 Kernel的对比,我们可以发现,其主要增加了以下的内容: 1. 基于ARM架构增加 ...
- linux内核多队列,Linux Kernel 中 Workqueue 使用系统默认队列和创建队列的方法
关于workqueue,我们还是有很多话要说. 想必大家对workqueue相关的函数(schedule_work .queue_work.INIT_WORK.create_singlethread_ ...
- linux kernel 进程管理,Linux内核 | 进程管理
1. 进程和线程 1.1 定义 进程是处于运行状态的程序和相关资源的总称,是资源分配的最小单位. 线程是进程的内部的一个执行序列,是CPU调度的最小单位.有一段可执行程序代码. 有一段进程专用的系统堆 ...
- linux kernel 文件夹,Linux kernel 文件夹说明
Linux 内核代码的目录结构 arch: 包含和硬件体系相关的的代码,每种硬件平台占一个相应的目录,如i386,arm, arm64, powerpc,mips等. block: 块设备驱动程序I/ ...
- linux kernel 调度,在Linux中,实时调度_kernel_开发99编程知识库
在實時調度,fifo和RR不具有完全相同的含義他們在非實時調度. 但是,進程總是在FIFO方式中選擇的時間量程SCHED_FIFO不限制不同的時間量程SCHED_RR . schED_FIFO進程不能 ...
最新文章
- firefly 编译opencv3.3.1, CMake报错
- linux中sh基本语法
- oracle查看表和索引碎片,Oracle 表空间索引存储与碎片检查
- 读jQuery之二十(Deferred对象)
- LeetCode-Scramble String
- [转]不知道能否解决先转下来再说不显示删除回复显示所有回复显示星级回复显示得分回复 没有找到MSVR90D.dll因此这个应用程序未能启动...
- 算法高级(26)-在Java8中为什么要使用红黑树来实现的HashMap?
- qt connect函数_Qt Inside信号和槽之connect
- Python C API 使用详解(二)
- mark制图软件_绘图软件有哪些?
- VMware 虚拟机Win10固定ip地址
- 2015062507 - 星际迷航.红杉
- 数据库MySQL创库、创表基本命令
- java增函数的单变量求解,最底层码农的不易谁能体会?谁心里苦谁知道啊。
- jpg转换为word可编辑的怎么转换呢
- 黑客喜欢的扫描器盒子
- 推荐系统与深度学习(二)——FFM模型原理​
- android 蒙版图片带拖动_snapseed安卓蒙版(snapseed拼接两张图片)V1.6.8 手机版
- 深入理解C++的new
- windows10 使用visual studio 2019编译OpenBLAS