reinit repo

This commit is contained in:
superconvert 2022-07-28 23:43:44 +08:00
parent ccb6d98569
commit 82769c235c
9 changed files with 707 additions and 0 deletions

13
00_build_env.sh Executable 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"

180
01_build_src.sh Executable file
View File

@ -0,0 +1,180 @@
#!/bin/sh
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
#----------------------------------------------
#
# 下载源码
#
#----------------------------------------------
mkdir -pv source
cd source
#KERNEL_SOURCE_URL=https://kernel.org/pub/linux/kernel/v4.x/linux-4.14.9.tar.xz
KERNEL_SOURCE_URL=https://mirror.bjtu.edu.cn/kernel/linux/kernel/v4.x/linux-4.14.9.tar.xz
if [ ! -f "linux-4.14.9.tar.xz" ]; then
wget $KERNEL_SOURCE_URL
fi
GLIBC_SOURCE_URL=https://ftp.gnu.org/gnu/glibc/glibc-2.32.tar.bz2
if [ ! -f "glibc-2.32.tar.bz2" ]; then
wget $GLIBC_SOURCE_URL
fi
BUSYBOX_SOURCE_URL=https://busybox.net/downloads/busybox-1.34.1.tar.bz2
if [ ! -f "busybox-1.34.1.tar.bz2" ]; then
wget $BUSYBOX_SOURCE_URL
fi
GCC_SOURCE_URL=https://ftpmirror.gnu.org/gcc/gcc-7.5.0/gcc-7.5.0.tar.xz
if [ ! -f "gcc-7.5.0.tar.xz" ]; then
wget $GCC_SOURCE_URL
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 ..
#---------------------------------------------
#
# 解压源码
#
#---------------------------------------------
mkdir -pv work
if [ ! -d "./work/linux-4.14.9" ]; then
echo "unzip kernel source"
tar xf source/linux-4.14.9.tar.xz -C work/
fi
if [ ! -d "./work/glibc-2.32" ]; then
echo "unzip glibc source"
tar xf source/glibc-2.32.tar.bz2 -C work/
fi
if [ ! -d "./work/busybox-1.34.1" ]; then
echo "unzip busybox source"
tar xf source/busybox-1.34.1.tar.bz2 -C work/
fi
if [ ! -d "./work/gcc-7.5.0" ]; then
echo "unzip gcc source"
tar xf source/gcc-7.5.0.tar.xz -C work/
fi
if [ ! -d "./work/binutils-2.36" ]; then
echo "unzip binutils source"
tar xf source/binutils-2.36.tar.xz -C work/
fi
#---------------------------------------------
#
# 编译源码
#
#---------------------------------------------
cd work
# 编译内核, 最终所有模块都装到目录 /lib/modules/4.14.9
if [ ! -d "kernel_install" ]; then
mkdir -pv kernel_install && cd linux-4.14.9 && make mrproper
# Enable the VESA framebuffer for graphics support.
# 网络需要 TUN/TAP 驱动 [ Device Drivers ] ---> [ Network device support ] ---> [ Universal TUN/TAP device driver support ]
make x86_64_defconfig && sed -i "s/.*CONFIG_FB_VESA.*/CONFIG_FB_VESA=y/" .config && make bzImage -j8
#cd linux-4.14.9 && make x86_64_defconfig && make bzImage -j8 && make modules && make modules_install && cd ..
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 distclean
../configure --prefix= \
--with-headers=${kernel_install}/include/ \
--without-gd \
--without-selinux \
--disable-werror
make -j8 && 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 distclean && make defconfig
# 静态编译 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_EXTRA_CFLAGS.*|CONFIG_EXTRA_CFLAGS=\"$CFLAGS -I${kernel_install}/include -I${glibc_install}/include -L${glibc_install}/lib\"|" .config
make busybox -j8 && make CONFIG_PREFIX=${busybox_install} install && cd ..
fi
# 编译 libgcc
if [ ! -d "libgcc_install" ]; then
mkdir -pv libgcc_install && cd gcc-7.5.0 && make distclean && rm ./config.cache
./contrib/download_prerequisites
./configure --prefix= --enable-languages=c,c++ --disable-multilib --disable-static --disable-libquadmath --enable-shared
CFLAGS="-L${glibc_install}/lib $CFLAGS" make -j8 && make install -j8 DESTDIR=${libgcc_install} && cd ..
fi
# 编译 binutils
if [ ! -d "binutils_install" ]; then
mkdir -pv binutils_install && cd binutils-2.36 && make distclean
./configure --prefix=
CFLAGS="-L${glibc_install}/lib $CFLAGS" make -j8 && make install -j8 DESTDIR=${binutils_install} && cd ..
cd ..
fi
cd ..
echo "Run the next script: 02_build_img.sh"

253
02_build_img.sh Executable file
View File

@ -0,0 +1,253 @@
#!/bin/sh
red='\e[0;41m' # 红色
RED='\e[1;31m'
green='\e[0;32m' # 绿色
GREEN='\e[1;32m'
yellow='\e[5;43m' # 黄色
YELLOW='\e[1;33m'
blue='\e[0;34m' # 蓝色
BLUE='\e[1;34m'
purple='\e[0;35m' # 紫色
PURPLE='\e[1;35m'
cyan='\e[4;36m' # 蓝绿色
CYAN='\e[1;36m'
WHITE='\e[1;37m' # 白色
NC='\e[0m' # 没有颜色
# ./02_build_img.sh gcc 这样就能编译带 gcc 的系统
with_gcc=$1
#----------------------------------------------
#
# 进行目录瘦身
#
#----------------------------------------------
./mk_strip.sh
#----------------------------------------------
#
# 制作磁盘
#
#----------------------------------------------
echo "${CYAN}--- build disk --- ${NC}"
# 创建磁盘 64M
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
n
p
w
EOF
echo "${GREEN}+++ build disk ok +++${NC}"
# 磁盘镜像挂载到具体设备
loop_dev=$(losetup -f)
# fdisk -l disk.img 查看 start 为 2048, unit 512 所以 -o 偏移扇区 1048576 = 2048 x 512
losetup -o 1048576 ${loop_dev} disk.img
# 对磁盘进行格式化
mkfs.ext3 ${loop_dev}
diskfs="diskfs"
# 挂载磁盘到本地目录
mkdir -pv ${diskfs}
mount -t ext3 ${loop_dev} ${diskfs}
# 安装grub 引导
grub-install --boot-directory=${diskfs}/boot/ --target=i386-pc --modules=part_msdos disk.img
#---------------------------------------------
#
# 制作内核和 rootfs
#
#---------------------------------------------
mkdir -pv rootfs
mkdir -pv rootfs/dev
mkdir -pv rootfs/etc
mkdir -pv rootfs/sys
mkdir -pv rootfs/mnt
mkdir -pv rootfs/tmp
mkdir -pv rootfs/lib
mkdir -pv rootfs/sbin
mkdir -pv rootfs/proc
mkdir -pv rootfs/root
mkdir -pv rootfs/lib64
mkdir -pv rootfs/lib/modules
# 拷贝内核镜像
cp work/kernel_install/bzImage ${diskfs}/boot/bzImage
# 拷贝 glibc 到 rootfs
cp work/glibc_install/* rootfs/ -r
rm -rf rootfs/lib/*.a
rm -rf rootfs/lib/gconv
rm -rf rootfs/bin/*
rm -rf rootfs/share
rm -rf rootfs/var/db
# 编译的镜像带有 gcc 编译器
if [ ! -n "${with_gcc}" ]; then
rm -rf rootfs/include
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
cp work/busybox_install/* rootfs/ -r
#-----------------------------------------------
#
# 制作启动文件系统 initramfs
#
#-----------------------------------------------
cd rootfs
echo "${CYAN}--- build initrd ---${NC}"
# 这种方法也可以 mkinitramfs -k -o ./${diskfs}/boot/initrd 4.14.9
# 利用 Busybox 采用脚本制作 init 脚本 https://blog.csdn.net/embeddedman/article/details/7721926
make_init() {
cat<<"EOF">init
#!/bin/sh
# 必须首先挂载,否则 mdev 不能正常工作
mount -t sysfs none /sys
mount -t proc none /proc
mount -t devtmpfs none /dev
mount -t tmpfs none /tmp -o mode=1777
# 必须挂载一下,否则下面的 mount 不上
mdev -s
mount -t ext3 /dev/sda1 /mnt
# 关闭内核烦人的输出信息
echo 0 > /proc/sys/kernel/printk
echo -e "\n\e[0;32mBoot took $(cut -d' ' -f1 /proc/uptime) seconds\e[0m"
mkdir -p /dev/pts
mount -t devpts none /dev/pts
# 切换之前,修改 mount 路径
mount --move /dev /mnt/dev
mount --move /sys /mnt/sys
mount --move /proc /mnt/proc
mount --move /tmp /mnt/tmp
# 切换到真正的磁盘系统上 rootfs(initramfs) ---> diskfs
exec switch_root /mnt /sbin/init
EOF
# /sbin/init [switch_root 执行] ---> /etc/inittab [定义了启动顺序] --->
# /etc/init.d/rcS [系统 mount, 安装驱动,配置网络] --->
# /etc/init.d/rc.local [文件配置应用程序需要的环境变量] ---> /etc/profile [部分初始化]
chmod +x init
}
make_init
# 下面这些不用了,利用脚本里面的 busybox 的 mdev -s 自动挂载
# mknod -m 644 dev/tty0 c 4 1
# mknod -m 644 dev/tty c 5 0
# mknod -m 600 dev/console c 5 1
# mknod -m 644 dev/null c 1 3
# mknod -m 640 dev/sda1 b 8 1
# 指定了利用 /etc/init.d/rcS 启动
cat<<"EOF">etc/inittab
::sysinit:echo "sysinit 1++++++++++++++++++++++++++++++++++++++"
::sysinit:/etc/init.d/rcS
::sysinit:echo "sysinit 2++++++++++++++++++++++++++++++++++++++"
::restart:/sbin/init
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
tty1::once:echo "hello smart-os tty1"
tty1::respawn:/bin/sh
tty2::once:echo "hello smart-os tty2"
tty2::respawn:/bin/sh
tty3::once:echo "hello smart-os tty3"
tty3::respawn:/bin/sh
EOF
find . | cpio -R root:root -H newc -o | gzip -9 > ../${diskfs}/boot/initrd
echo "${GREEN}+++ build initrd ok +++${NC}"
cd ..
#--------------------------------------------------------------
#
# 生成磁盘文件系统(利用 busybox 结构,省的自己创建了)
#
#--------------------------------------------------------------
echo "${CYAN}--- build diskfs ---${NC}"
cp rootfs/* ${diskfs} -r
# 带有 gcc 编译器
if [ "${with_gcc}" ]; then
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 即可
./make_driver.sh $(pwd)/${diskfs}/lib/modules
# 编译网卡驱动 ( 目前版本内核已集成 e1000 )
# cd work/linux-4.14.9 && make M=drivers/net/ethernet/intel/e1000/ && cd ../..
# 生成 grub.cfg 文件
cat - > ${diskfs}/boot/grub/grub.cfg << EOF
set timeout=6
menuentry "smart-os" {
root=(hd0,msdos1)
linux /boot/bzImage console=tty0
initrd /boot/initrd
}
EOF
# 生成 /etc/init.d/rcS 文件
title=$(cat<<EOF
\e[0;36m
..######..##.....##....###....########..########..........#######...######.
.##....##.###...###...##.##...##.....##....##............##.....##.##....##
.##.......####.####..##...##..##.....##....##............##.....##.##......
..######..##.###.##.##.....##.########.....##....#######.##.....##..######.
.......##.##.....##.#########.##...##......##............##.....##.......##
.##....##.##.....##.##.....##.##....##.....##............##.....##.##....##
..######..##.....##.##.....##.##.....##....##.............#######...######.
\e[0m
EOF
)
mkdir -pv ${diskfs}/etc/init.d
cat - > ${diskfs}/etc/init.d/rcS << EOF
#!/bin/sh
echo -e "\n“${title}”\n"
# 测试驱动加载
cd /lib/modules && insmod hello_world.ko
# dns 测试 busybox 必须动态编译 动态编译 glibc 已经集成 dns 功能
ifconfig eth0 192.168.100.6 && ifconfig eth0 up
route add default gw 192.168.100.1
echo "nameserver 114.114.114.114" >> /etc/resolv.conf
# exec 执行 /etc/init.d/rc.local 脚本
EOF
chmod +x ${diskfs}/etc/init.d/rcS
echo "${GREEN}+++ build diskfs ok +++${NC}"
# 卸载映射
umount ${loop_dev}
losetup -d ${loop_dev}
#---------------------------------------------------------------
#
# 查看磁盘内容
#
#---------------------------------------------------------------
./cat_img.sh
echo "Run the next script: 03_run_qemu.sh or 04_run_docker.sh"

90
03_run_qemu.sh Executable file
View File

@ -0,0 +1,90 @@
#!/bin/sh
#---------------------
# 停掉 NAT
#---------------------
stop_nat() {
# 停掉 tap0
ip link set tap0 down
# 断开与网桥链接
brctl delif br0 tap0
# 停掉网桥
ip link set br0 down
# 删除设备 tap0
ip tuntap del mode tap tap0
# 删除网桥
brctl delbr br0
}
#---------------------
# 启动 NAT
#---------------------
start_nat() {
# 创建虚拟网桥
brctl addbr br0
# 创建虚拟tap设备
ip tuntap add dev tap0 mode tap
# 将tap设备介入网桥
brctl addif br0 tap0
# 配置网桥ip
ip addr add 192.168.100.1/24 dev br0
ip addr add 192.168.100.2/24 dev tap0
# 启动桥设备和虚拟网卡设备
ip link set br0 up
ip link set tap0 up
# 配置iptables forward转发规则
# 在基本环境搭建这一节中设置了一个本地网络虚机只能访问host无法访问外网如果需要访问外网需要设置SNAT
iptables -t nat -A POSTROUTING -s 192.168.100.0/24 ! -d 192.168.100.0/24 -j MASQUERADE
# 开启物理网卡的转发功能:
sysctl -w net.ipv4.ip_forward=1
# 如果有防火墙的特别是centos系统中记得放开防火墙
iptables -A FORWARD -s 192.168.100.0/24 -j ACCEPT
}
#----------------------
# DNS 服务
#----------------------
start_dns() {
# 准备dnsmasq配置文件,启动dnsmasq服务这样就能为虚拟机自动分配IP了
cat<<EOF>dnsmasq.conf
strict-order
pid-file=/var/run/dnsmasq.pid
except-interface=lo
bind-interfaces
listen-address=192.168.0.1
dhcp-range=192.168.100.2,192.168.100.254
dhcp-no-override
dhcp-leasefile=/var/lib/libvirt/dnsmasq/default.leases
dhcp-lease-max=253
dhcp-hostsfile=/var/lib/libvirt/dnsmasq/default.hostsfile
addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts
EOF
/usr/sbin/dnsmasq --conf-file=./dnsmasq.conf
}
stop_dns() {
# 杀掉 dhcp 服务
killall dnsmasq
}
#----------------------------------------------
# 运行 smart-os 前,先启动这个,这样就能上网了
#----------------------------------------------
start_nat
# 启动镜像 网络对应 run_nat.sh 里面的配置
qemu-system-x86_64 -drive format=raw,file=disk.img -netdev tap,id=nd0,ifname=tap0 -device e1000,netdev=nd0
# stop nat
stop_nat
#----------------------------------------------------
#
# 多硬盘测试 -hdb extra.img
#
#----------------------------------------------------
# make_sdb.sh
# qemu-system-x86_64 -drive format=raw,file=disk.img -hdb extra.img

44
04_run_docker.sh Executable file
View File

@ -0,0 +1,44 @@
#!/bin/sh
dock_name=smart-os
loop_dev=$(losetup -f)
# fdisk -l disk.img 查看 start 为 2048, unit 512 所以 -o 偏移扇区 1048576 = 2048 x 512
losetup -o 1048576 ${loop_dev} disk.img
# 挂载磁盘到本地目录
mkdir -p ./tmp_docker
mount -t ext3 ${loop_dev} ./tmp_docker
cd ./tmp_docker
tar -cvpf ../${dock_name}.tar --directory=./ --exclude=proc --exclude=sys --exclude=dev --exclude=run --exclude=boot .
cd ..
umount ./tmp_docker
rm -rf ./tmp_docker
losetup -d ${loop_dev}
# 删除镜像
clear() {
if [ ! "`docker ps -a | grep ${dock_name}`" = "" ] ; then
docker stop `docker ps -a | grep ${dock_name} | awk '{print $1}'`
docker rm `docker ps -a | grep ${dock_name} | awk '{print $1}'`
fi
if [ ! "`docker images -a | grep ${dock_name}`" = "" ] ; then
docker rmi `docker images -a | grep ${dock_name} | awk '{print $1}'`:1.0
fi
}
# 导入镜像
run() {
cat smart-os.tar | docker import - ${dock_name}:1.0
docker run -t -i ${dock_name}:1.0 /bin/sh
}
clear
run
clear
# 删除镜像文件
if [ -f "${dock_name}.tar" ]; then
rm -rf ${dock_name}.tar
fi

13
cat_img.sh Executable file
View File

@ -0,0 +1,13 @@
loop_dev=$(losetup -f)
# fdisk -l disk.img 查看 start 为 2048, unit 512 所以 -o 偏移扇区 1048576 = 2048 x 512
losetup -o 1048576 ${loop_dev} disk.img
# 挂载磁盘到本地目录
mkdir -p ./mnt1
mount -t ext3 ${loop_dev} ./mnt1
echo "/ ---------------------------------------"
du ./mnt1 -h
umount ./mnt1
rm -rf mnt1
losetup -d ${loop_dev}

36
mk_drv.sh Executable 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 Executable 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 Executable 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