Add files via upload

This commit is contained in:
superconvert 2022-07-28 22:25:05 +08:00 committed by GitHub
parent ef4e46c890
commit 4c38f08f8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 243 additions and 57 deletions

13
00_build_env.sh Normal file
View File

@ -0,0 +1,13 @@
#!/bin/sh
rm work/kernel_install/ work/glibc_install/ work/busybox_install/ work/libgcc_install work/binutils_install -rf
if [ -f "/usr/bin/apt" ]; then
apt -y install gcc g++ make gawk bison libelf-dev bridge-utils qemu-system docker.io
fi
if [ -f "/usr/bin/yum" ]; then
yum -y install gcc gcc-c++ make gawk bison elfutils-libelf bridge-utils qemu-img qemu-kvm qemu-kvm-tools docker
fi
echo "Run the next script: 01_build_src.sh"

View File

@ -1,6 +1,57 @@
#!/bin/sh #!/bin/sh
SYSROOT=`pwd`"/rootfs" core_num=`nproc`
kernel_install=`pwd`"/work/kernel_install"
glibc_install=`pwd`"/work/glibc_install"
busybox_install=`pwd`"/work/busybox_install"
libgcc_install=`pwd`"/work/libgcc_install"
binutils_install=`pwd`"/work/binutils_install"
#-----------------------------------------------
#
# 重新生成目标文件
#
#-----------------------------------------------
if [ "$1" != "" ]; then
if [ $1 != "rebuild" ]; then
exit
fi
echo "rebuild"
cd work
rm -rf kernel_install glibc_install busybox_install libgcc_install binutils_install
# 编译内核, 最终所有模块都装到目录 /lib/modules/4.14.9
if [ ! -d "kernel_install" ]; then
mkdir -pv kernel_install && cd linux-4.14.9
make INSTALL_HDR_PATH=${kernel_install} headers_install -j8 && cp arch/x86_64/boot/bzImage ${kernel_install} && cd ..
fi
# 编译glibc
if [ ! -d "glibc_install" ]; then
mkdir -pv glibc_install && cd glibc-2.32
mkdir -pv build && cd build
make install -j8 DESTDIR=${glibc_install} && cd .. && cd ..
fi
# 编译 busybox
if [ ! -d "busybox_install" ]; then
mkdir -pv busybox_install && cd busybox-1.34.1
make CONFIG_PREFIX=${busybox_install} install && cd ..
fi
# 编译 libgcc
if [ ! -d "libgcc_install" ]; then
mkdir -pv libgcc_install && cd gcc-7.5.0
make install -j8 DESTDIR=${libgcc_install} && cd ..
fi
# 编译 binutils
if [ ! -d "binutils_install" ]; then
mkdir -pv binutils_install && cd binutils-2.36
make install -j8 DESTDIR=${binutils_install} && cd ..
fi
cd ..
exit
fi
#---------------------------------------------- #----------------------------------------------
# #
@ -31,6 +82,11 @@ if [ ! -f "gcc-7.5.0.tar.xz" ]; then
wget $GCC_SOURCE_URL wget $GCC_SOURCE_URL
fi fi
BINUTILS_SOURCE_URL=https://ftp.gnu.org/gnu/binutils/binutils-2.36.tar.xz
if [ ! -f "binutils-2.36.tar.xz" ]; then
wget $BINUTILS_SOURCE_URL
fi
cd .. cd ..
#--------------------------------------------- #---------------------------------------------
@ -41,23 +97,28 @@ cd ..
mkdir -pv work mkdir -pv work
if [ ! -d "./work/linux-4.14.9" ]; then if [ ! -d "./work/linux-4.14.9" ]; then
tar xvf source/linux-4.14.9.tar.xz -C work/ echo "unzip kernel source"
tar xf source/linux-4.14.9.tar.xz -C work/
fi fi
if [ ! -d "./work/glibc-2.32" ]; then if [ ! -d "./work/glibc-2.32" ]; then
tar xvf source/glibc-2.32.tar.bz2 -C work/ echo "unzip glibc source"
tar xf source/glibc-2.32.tar.bz2 -C work/
fi fi
if [ ! -d "./work/busybox-1.34.1" ]; then if [ ! -d "./work/busybox-1.34.1" ]; then
tar xvf source/busybox-1.34.1.tar.bz2 -C work/ echo "unzip busybox source"
tar xf source/busybox-1.34.1.tar.bz2 -C work/
fi fi
if [ ! -d "./work/gcc-7.5.0" ]; then if [ ! -d "./work/gcc-7.5.0" ]; then
tar xvf source/gcc-7.5.0.tar.xz -C work/ echo "unzip gcc source"
tar xf source/gcc-7.5.0.tar.xz -C work/
fi fi
if [ ! -d "./work/binutils-2.36" ]; then if [ ! -d "./work/binutils-2.36" ]; then
tar xvf source/binutils-2.36.tar.xz -C work/ echo "unzip binutils source"
tar xf source/binutils-2.36.tar.xz -C work/
fi fi
#--------------------------------------------- #---------------------------------------------
@ -67,12 +128,6 @@ fi
#--------------------------------------------- #---------------------------------------------
cd work cd work
kernel_install=`pwd`"/kernel_install"
glibc_install=`pwd`"/glibc_install"
busybox_install=`pwd`"/busybox_install"
libgcc_install=`pwd`"/libgcc_install"
binutils_install=`pwd`"/binutils_install"
# 编译内核, 最终所有模块都装到目录 /lib/modules/4.14.9 # 编译内核, 最终所有模块都装到目录 /lib/modules/4.14.9
if [ ! -d "kernel_install" ]; then if [ ! -d "kernel_install" ]; then
mkdir -pv kernel_install && cd linux-4.14.9 && make mrproper mkdir -pv kernel_install && cd linux-4.14.9 && make mrproper
@ -85,7 +140,6 @@ fi
# 编译glibc # 编译glibc
if [ ! -d "glibc_install" ]; then if [ ! -d "glibc_install" ]; then
echo $PATH
mkdir -pv glibc_install && cd glibc-2.32 mkdir -pv glibc_install && cd glibc-2.32
mkdir -pv build && cd build && make distclean mkdir -pv build && cd build && make distclean
../configure --prefix= \ ../configure --prefix= \
@ -102,7 +156,6 @@ if [ ! -d "busybox_install" ]; then
# 静态编译 sed -i "s/# CONFIG_STATIC is not set/CONFIG_STATIC=y/g" .config # 静态编译 sed -i "s/# CONFIG_STATIC is not set/CONFIG_STATIC=y/g" .config
sed -i "s|.*CONFIG_SYSROOT.*|CONFIG_SYSROOT=\"${glibc_install}\"|" .config sed -i "s|.*CONFIG_SYSROOT.*|CONFIG_SYSROOT=\"${glibc_install}\"|" .config
sed -i "s|.*CONFIG_EXTRA_CFLAGS.*|CONFIG_EXTRA_CFLAGS=\"$CFLAGS -I${kernel_install}/include -I${glibc_install}/include -L${glibc_install}/lib\"|" .config sed -i "s|.*CONFIG_EXTRA_CFLAGS.*|CONFIG_EXTRA_CFLAGS=\"$CFLAGS -I${kernel_install}/include -I${glibc_install}/include -L${glibc_install}/lib\"|" .config
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
make busybox -j8 && make CONFIG_PREFIX=${busybox_install} install && cd .. make busybox -j8 && make CONFIG_PREFIX=${busybox_install} install && cd ..
fi fi
@ -123,3 +176,5 @@ if [ ! -d "binutils_install" ]; then
fi fi
cd .. cd ..
echo "Run the next script: 02_build_img.sh"

View File

@ -15,13 +15,28 @@ CYAN='\e[1;36m'
WHITE='\e[1;37m' # 白色 WHITE='\e[1;37m' # 白色
NC='\e[0m' # 没有颜色 NC='\e[0m' # 没有颜色
# ./02_build_img.sh gcc 这样就能编译带 gcc 的系统
with_gcc=$1
#----------------------------------------------
#
# 进行目录瘦身
#
#----------------------------------------------
./mk_strip.sh
#---------------------------------------------- #----------------------------------------------
# #
# 制作磁盘 # 制作磁盘
# #
#---------------------------------------------- #----------------------------------------------
echo "${CYAN}--- build disk --- ${NC}"
# 创建磁盘 64M # 创建磁盘 64M
dd if=/dev/zero of=disk.img bs=1M count=256 if [ ! -n "${with_gcc}" ]; then
dd if=/dev/zero of=disk.img bs=1M count=64
else
dd if=/dev/zero of=disk.img bs=1M count=512
fi
# 对磁盘进行分区一个主分区 # 对磁盘进行分区一个主分区
fdisk disk.img << EOF fdisk disk.img << EOF
n n
@ -31,7 +46,7 @@ p
w w
EOF EOF
echo ".........................................................." echo "${GREEN}+++ build disk ok +++${NC}"
# 磁盘镜像挂载到具体设备 # 磁盘镜像挂载到具体设备
loop_dev=$(losetup -f) loop_dev=$(losetup -f)
@ -44,7 +59,6 @@ diskfs="diskfs"
# 挂载磁盘到本地目录 # 挂载磁盘到本地目录
mkdir -pv ${diskfs} mkdir -pv ${diskfs}
mount -t ext3 ${loop_dev} ${diskfs} mount -t ext3 ${loop_dev} ${diskfs}
# 安装grub 引导 # 安装grub 引导
grub-install --boot-directory=${diskfs}/boot/ --target=i386-pc --modules=part_msdos disk.img grub-install --boot-directory=${diskfs}/boot/ --target=i386-pc --modules=part_msdos disk.img
@ -76,13 +90,15 @@ rm -rf rootfs/lib/gconv
rm -rf rootfs/bin/* rm -rf rootfs/bin/*
rm -rf rootfs/share rm -rf rootfs/share
rm -rf rootfs/var/db rm -rf rootfs/var/db
rm -rf rootfs/include # 编译的镜像带有 gcc 编译器
rm -rf rootfs/lib/ld-linux-x86-64.so.2 if [ ! -n "${with_gcc}" ]; then
ln -s ../lib rootfs/usr/lib rm -rf rootfs/include
ln -s ../lib/ld-2.32.so rootfs/lib64/ld-linux-x86-64.so.2 else
echo "${RED} with-gcc tools --- you can build your world${NC}"
cp work/glibc_install/lib/libc_nonshared.a rootfs/lib
fi
# 拷贝 busybox 到 rootfs # 拷贝 busybox 到 rootfs
echo "${CYAN}开始制作rootfs...${NC}"
cp work/busybox_install/* rootfs/ -r cp work/busybox_install/* rootfs/ -r
#----------------------------------------------- #-----------------------------------------------
@ -91,6 +107,7 @@ cp work/busybox_install/* rootfs/ -r
# #
#----------------------------------------------- #-----------------------------------------------
cd rootfs cd rootfs
echo "${CYAN}--- build initrd ---${NC}"
# 这种方法也可以 mkinitramfs -k -o ./${diskfs}/boot/initrd 4.14.9 # 这种方法也可以 mkinitramfs -k -o ./${diskfs}/boot/initrd 4.14.9
# 利用 Busybox 采用脚本制作 init 脚本 https://blog.csdn.net/embeddedman/article/details/7721926 # 利用 Busybox 采用脚本制作 init 脚本 https://blog.csdn.net/embeddedman/article/details/7721926
@ -128,8 +145,7 @@ EOF
# /sbin/init [switch_root 执行] ---> /etc/inittab [定义了启动顺序] ---> # /sbin/init [switch_root 执行] ---> /etc/inittab [定义了启动顺序] --->
# /etc/init.d/rcS [系统 mount, 安装驱动,配置网络] ---> # /etc/init.d/rcS [系统 mount, 安装驱动,配置网络] --->
# /etc/init.d/rc.local [文件配置应用程序需要的环境变量] ---> # /etc/init.d/rc.local [文件配置应用程序需要的环境变量] ---> /etc/profile [部分初始化]
# /etc/profile [部分初始化]
chmod +x init chmod +x init
} }
@ -158,18 +174,8 @@ tty3::once:echo "hello smart-os tty3"
tty3::respawn:/bin/sh tty3::respawn:/bin/sh
EOF EOF
# dns 测试
# 0. 启动脚本 run_nat.sh
# 1. busybox 必须动态编译
# 2. ifconfig eth0 192.168.100.6 && ifconfig eth0 up
# 3. route add default gw 192.168.100.1
# 4. echo "nameserver 114.114.114.114" >> /etc/resolv.conf
# cp -rf ../fixed/lib* lib/ && cp ../fixed/ld-linux-x86-64.so.2 lib64/
strip -g bin/* sbin/* lib/*
find . | cpio -R root:root -H newc -o | gzip -9 > ../${diskfs}/boot/initrd find . | cpio -R root:root -H newc -o | gzip -9 > ../${diskfs}/boot/initrd
echo "${GREEN}rootfs制作成功!!!${NC}" echo "${GREEN}+++ build initrd ok +++${NC}"
echo ".........................................................."
cd .. cd ..
#-------------------------------------------------------------- #--------------------------------------------------------------
@ -177,11 +183,15 @@ cd ..
# 生成磁盘文件系统(利用 busybox 结构,省的自己创建了) # 生成磁盘文件系统(利用 busybox 结构,省的自己创建了)
# #
#-------------------------------------------------------------- #--------------------------------------------------------------
echo "${CYAN}开始制作diskfs...${NC}" echo "${CYAN}--- build diskfs ---${NC}"
cp rootfs/* ${diskfs} -r cp rootfs/* ${diskfs} -r
cp work/libgcc_install/* ${diskfs} -r # 带有 gcc 编译器
cp work/binutils_install/* ${diskfs} -r if [ "${with_gcc}" ]; then
rm -rf ${diskfs}/init && rm -rf ${diskfs}/linuxrc && rm -rf ${diskfs}/lost+found echo "${RED} with-gcc tools --- you can build your world${NC}"
cp work/libgcc_install/* ${diskfs} -r
cp work/binutils_install/* ${diskfs} -r
fi
rm -rf ${diskfs}/init ${diskfs}/linuxrc ${diskfs}/lost+found ${diskfs}/share
# 我们测试驱动, 制作的镜像启动后,我们进入此目录 insmod hello_world.ko 即可 # 我们测试驱动, 制作的镜像启动后,我们进入此目录 insmod hello_world.ko 即可
./make_driver.sh $(pwd)/${diskfs}/lib/modules ./make_driver.sh $(pwd)/${diskfs}/lib/modules
@ -198,7 +208,7 @@ menuentry "smart-os" {
} }
EOF EOF
# 生成 rcS 文件 # 生成 /etc/init.d/rcS 文件
title=$(cat<<EOF title=$(cat<<EOF
\e[0;36m \e[0;36m
..######..##.....##....###....########..########..........#######...######. ..######..##.....##....###....########..########..........#######...######.
@ -219,7 +229,7 @@ echo -e "\n“${title}”\n"
# 测试驱动加载 # 测试驱动加载
cd /lib/modules && insmod hello_world.ko cd /lib/modules && insmod hello_world.ko
# dns 测试 # dns 测试 busybox 必须动态编译 动态编译 glibc 已经集成 dns 功能
ifconfig eth0 192.168.100.6 && ifconfig eth0 up ifconfig eth0 192.168.100.6 && ifconfig eth0 up
route add default gw 192.168.100.1 route add default gw 192.168.100.1
echo "nameserver 114.114.114.114" >> /etc/resolv.conf echo "nameserver 114.114.114.114" >> /etc/resolv.conf
@ -227,10 +237,17 @@ echo "nameserver 114.114.114.114" >> /etc/resolv.conf
# exec 执行 /etc/init.d/rc.local 脚本 # exec 执行 /etc/init.d/rc.local 脚本
EOF EOF
chmod +x ${diskfs}/etc/init.d/rcS chmod +x ${diskfs}/etc/init.d/rcS
echo "${GREEN}+++ build diskfs ok +++${NC}"
echo "${GREEN}diskfs制作成功!!!${NC}"
echo ".........................................................."
# 卸载映射 # 卸载映射
umount ${loop_dev} umount ${loop_dev}
losetup -d ${loop_dev} losetup -d ${loop_dev}
#---------------------------------------------------------------
#
# 查看磁盘内容
#
#---------------------------------------------------------------
./cat_img.sh
echo "Run the next script: 03_run_qemu.sh or 04_run_docker.sh"

View File

@ -7,20 +7,7 @@ losetup -o 1048576 ${loop_dev} disk.img
mkdir -p ./mnt1 mkdir -p ./mnt1
mount -t ext3 ${loop_dev} ./mnt1 mount -t ext3 ${loop_dev} ./mnt1
echo "/ ---------------------------------------" echo "/ ---------------------------------------"
ls ./mnt1/
du ./mnt1 -h du ./mnt1 -h
echo "boot ---------------------------------------"
ls ./mnt1/boot
echo "sbin ---------------------------------------"
ls ./mnt1/sbin
echo "etc ---------------------------------------"
ls ./mnt1/etc
echo "dev ---------------------------------------"
ls ./mnt1/dev
echo "lib ---------------------------------------"
ls ./mnt1/lib
echo "lib64 -------------------------------------"
ls ./mnt1/lib64
umount ./mnt1 umount ./mnt1
rm -rf mnt1 rm -rf mnt1
losetup -d ${loop_dev} losetup -d ${loop_dev}

36
mk_drv.sh Normal file
View File

@ -0,0 +1,36 @@
#!/bin/sh
mkdir -pv driver && cd driver
cat<<EOF>hello_world.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
static int __init hello_world_init(void)
{
printk(KERN_DEBUG "hello world!!!\n");
return 0;
}
static void __exit hello_world_exit(void)
{
printk(KERN_DEBUG "goodbye world!!!\n");
}
module_init(hello_world_init);
module_exit(hello_world_exit);
EOF
cat<<EOF>Makefile
obj-m += hello_world.o
all:
make -C ../work/linux-4.14.9 M=`pwd` modules
clean:
make -C ../work/linux-4.14.9 M=`pwd` clean
EOF
echo $1
make && mv hello_world.ko $1 && make clean && cd .. && rm -rf driver

37
mk_sdb.sh Normal file
View File

@ -0,0 +1,37 @@
#!/bin/sh
#----------------------------------------------
#
# 制作磁盘
#
#----------------------------------------------
echo "${CYAN}开始制作磁盘...${NC}"
# 创建磁盘 64M
dd if=/dev/zero of=extra.img bs=1M count=64
# 对磁盘进行分区一个主分区
fdisk extra.img << EOF
n
p
w
EOF
echo "${GREEN}磁盘制作成功!!!${NC}"
echo ".........................................................."
# 磁盘镜像挂载到具体设备
loop_dev=$(losetup -f)
# fdisk -l disk.img 查看 start 为 2048, unit 512 所以 -o 偏移扇区 1048576 = 2048 x 512
losetup -o 1048576 ${loop_dev} extra.img
# 对磁盘进行格式化
mkfs.ext3 ${loop_dev}
losetup -d ${loop_dev}

41
mk_strip.sh Normal file
View File

@ -0,0 +1,41 @@
#!/bin/sh
strip_dir() {
for file in `ls $1`
do
if [ -d $1"/"$file ]; then
strip_dir $1"/"$file
else
if [ -x $1"/"$file ]; then
case "$file" in
*.a);;
*.la);;
*)strip $1"/"$file; continue;;
esac
fi
case "$file" in
*.a) strip -g -S -d $1"/"$file;;
*.so) strip $1"/"$file;;
*.so.*) strip $1"/"$file;;
*);;
esac
fi
done
}
# strip glibc
rm -rf work/glibc_install/share
strip_dir work/glibc_install
# strip busybox
strip work/busybox_install/bin/busybox
# strip gcc
rm -rf work/libgcc_install/share
strip_dir work/libgcc_install
# strip binutils
rm -rf work/binutils_install/share
rm -rf work/binutils_install/x86_64-pc-linux-gnu
strip_dir work/binutils_install