smart-os/02_build_img.sh

429 lines
15 KiB
Bash
Raw Normal View History

2022-07-28 23:43:44 +08:00
#!/bin/sh
2022-07-31 18:36:44 +08:00
# 导入公共环境
. ./common.sh
2022-07-28 23:43:44 +08:00
#----------------------------------------------
#
# 进行目录瘦身
#
#----------------------------------------------
2022-07-31 01:54:15 +08:00
./mk_strip.sh
2022-07-28 23:43:44 +08:00
#----------------------------------------------
#
# 制作磁盘
#
#----------------------------------------------
echo "${CYAN}--- build disk --- ${NC}"
2022-08-18 06:43:52 +08:00
# 创建磁盘 128M 或 256M
2022-07-31 18:36:44 +08:00
if [ "${with_gcc}" = false ]; then
2022-10-17 22:26:20 +08:00
create_disk disk.img 4096
2022-07-28 23:43:44 +08:00
else
2022-10-17 22:26:20 +08:00
create_disk disk.img 4096
2022-07-28 23:43:44 +08:00
fi
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}
2022-10-13 22:19:12 +08:00
# 如果制作的 disk.img 转换为 qemu-img convert disk.img -f raw -O vmdk out.vmdk, vmware 的磁盘类型一定设置为 SATA ,否则,启动失败
2022-07-28 23:43:44 +08:00
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
#---------------------------------------------
#
2022-10-19 22:09:51 +08:00
# 制作内核和 rootfs ( run 目录下 udev 被服务 udevd 使用,否则 xfce 鼠标不能使用 )
2022-07-28 23:43:44 +08:00
#
#---------------------------------------------
2022-07-31 00:01:00 +08:00
rm -rf rootfs
2022-07-28 23:43:44 +08:00
mkdir -pv rootfs
mkdir -pv rootfs/dev
mkdir -pv rootfs/etc
mkdir -pv rootfs/sys
mkdir -pv rootfs/mnt
2022-10-19 22:09:51 +08:00
mkdir -pv rootfs/run
2022-07-28 23:43:44 +08:00
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
# 拷贝内核镜像
2022-07-31 18:36:44 +08:00
cp ${linux_install}/bzImage ${diskfs}/boot/bzImage
2022-10-13 22:19:12 +08:00
cp ${linux_install}/lib ${diskfs}/ -r
2022-07-28 23:43:44 +08:00
# 拷贝 glibc 到 rootfs
2022-07-31 18:36:44 +08:00
cp ${glibc_install}/* rootfs/ -r
2022-07-28 23:43:44 +08:00
rm -rf rootfs/var/db
2022-07-31 01:54:15 +08:00
rm -rf rootfs/share
rm -rf rootfs/usr/share
find rootfs/ -name "*.a" -exec rm -rf {} \;
2022-07-28 23:43:44 +08:00
# 编译的镜像带有 gcc 编译器
2022-07-31 18:36:44 +08:00
if [ "${with_gcc}" = false ]; then
rm -rf rootfs/usr/include
2022-07-28 23:43:44 +08:00
else
2022-07-31 18:36:44 +08:00
cp ${glibc_install}/usr/lib64/libc_nonshared.a rootfs/usr/lib64
2022-07-28 23:43:44 +08:00
fi
2022-07-31 00:01:00 +08:00
#----------------------------------------------------------------------
# 这个解释器必须设置对,否则系统会启动时 crash, 导致启动失败 !!!!!!
2022-07-31 18:36:44 +08:00
# 这个现在 glibc 编译时,已经自动生成,先注释掉
2022-07-31 00:01:00 +08:00
#-----------------------------------------------------------------------
2022-07-31 18:36:44 +08:00
# ln -s /lib/ld-2.32.so rootfs/lib64/ld-linux-x86-64.so.2
2022-07-31 00:01:00 +08:00
2022-07-28 23:43:44 +08:00
# 拷贝 busybox 到 rootfs
2022-07-31 18:36:44 +08:00
cp ${busybox_install}/* rootfs/ -r
2022-07-28 23:43:44 +08:00
#-----------------------------------------------
#
# 制作启动文件系统 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
2022-10-14 21:15:23 +08:00
# 光驱挂载 : /dev/cdrom 是 /dev/sr0 的软连接,也就是说 /dev/sr0 才是实际意义上的光驱。所以没有软连接,
# 照样可以挂载光驱。使用命令"mount /dev/sr0 /mnt/cdrom"便可以实现挂载。
2022-07-28 23:43:44 +08:00
make_init() {
cat<<"EOF">init
#!/bin/sh
# 必须首先挂载,否则 mdev 不能正常工作
mount -t sysfs sysfs /sys
mount -t proc proc /proc
mount -t devtmpfs udev /dev
mount -t tmpfs tmpfs /tmp -o mode=1777
2022-07-28 23:43:44 +08:00
# 必须挂载一下,否则下面的 mount 不上
mdev -s
mount -t ext3 /dev/sda1 /mnt
# 关闭内核烦人的输出信息
echo 0 > /proc/sys/kernel/printk
2022-07-31 18:36:44 +08:00
# 热插拔处理都交给 mdev
2022-10-14 21:15:23 +08:00
# echo /sbin/mdev > /proc/sys/kernel/hotplug
2022-07-28 23:43:44 +08:00
echo -e "\n\e[0;32mBoot took $(cut -d' ' -f1 /proc/uptime) seconds\e[0m"
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts
2022-07-28 23:43:44 +08:00
# 切换之前,修改 mount 路径
mount --move /dev /mnt/dev
mount --move /sys /mnt/sys
mount --move /proc /mnt/proc
mount --move /tmp /mnt/tmp
2022-07-31 18:36:44 +08:00
# 切换到真正的磁盘系统上 rootfs ---> diskfs
2022-10-01 19:47:23 +08:00
export LD_LIBRARY_PATH="/lib:/lib64:/usr/lib:/usr/lib64:/usr/local/lib:/usr/local/lib64:/usr/lib/x86_64-linux-gnu"
2022-10-14 21:15:23 +08:00
exec switch_root /mnt /sbin/init
2022-07-28 23:43:44 +08:00
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
2022-10-09 21:52:51 +08:00
# xfce 需要显卡设备
# mknod -m 664 dev/dri/card0 c 226 0
2022-07-28 23:43:44 +08:00
# 指定了利用 /etc/init.d/rcS 启动
cat<<"EOF">etc/inittab
# 启动 udevd 服务,保证鼠标设备能正常监视,否则键盘不能使用
::sysinit:/bin/echo "starting udevd ... ..."
::sysinit:/usr/sbin/udevd --daemon
::sysinit:/usr/sbin/udevadm trigger
::sysinit:/usr/sbin/udevadm settle
::sysinit:echo "sysinit ++++++++++++++++++++++++++++++++++++++"
2022-10-17 22:26:20 +08:00
::sysinit:/etc/init.d/rcS
::sysinit:echo "sysinit ++++++++++++++++++++++++++++++++++++++"
2022-10-17 22:26:20 +08:00
# /bin/sh invocations on selected ttys
#
# Note below that we prefix the shell commands with a "-" to indicate to the
# shell that it is supposed to be a login shell. Normally this is handled by
# login, but since we are bypassing login in this case, BusyBox lets you do
# this yourself...
#
2022-10-18 20:55:17 +08:00
# Start an "askfirst" shell on the console (whatever that may be) -f root 自动登录
::respawn:-/bin/login -f root
2022-10-17 22:26:20 +08:00
# Start an "askfirst" shell on /dev/tty2-4
tty2::respawn:-/bin/sh
tty3::respawn:-/bin/sh
tty4::respawn:-/bin/sh
# /sbin/getty invocations for selected ttys
tty4::respawn:/sbin/getty 38400 tty5
tty5::respawn:/sbin/getty 38400 tty6
# Example of how to put a getty on a serial line (for a terminal)
#::respawn:/sbin/getty -L ttyS0 9600 vt100
#::respawn:/sbin/getty -L ttyS1 9600 vt100
#
# Example how to put a getty on a modem line.
#::respawn:/sbin/getty 57600 ttyS2
# Stuff to do when restarting the init process
2022-07-28 23:43:44 +08:00
::restart:/sbin/init
2022-10-17 22:26:20 +08:00
# Stuff to do before rebooting
2022-07-28 23:43:44 +08:00
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
2022-08-01 22:57:30 +08:00
::shutdown:/sbin/swapoff -a
2022-07-28 23:43:44 +08:00
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
2022-10-14 21:15:23 +08:00
2022-10-18 20:55:17 +08:00
# 单独的 lshw
cp ${lshw_install}/* ${diskfs} -r
2022-10-19 22:09:51 +08:00
# 单独的 lsof
cp ${lsof_install}/* ${diskfs} -r
2022-10-14 21:15:23 +08:00
# 单独的 pciutils
cp ${pciutils_install}/* ${diskfs} -r
if [ -f "${diskfs}/usr/share/pci.ids.gz" ]; then
mkdir -pv ${diskfs}/usr/local/share
mv ${diskfs}/usr/share/pci.ids.gz ${diskfs}/usr/local/share/pci.ids.gz
fi
# 单独的 strace
cp ${strace_install}/* ${diskfs} -r
2022-10-17 22:26:20 +08:00
# 带有 openssl
cp ${openssl_install}/* ${diskfs} -r
# 带有 openssh
cp ${openssh_install}/* ${diskfs} -r
2022-07-28 23:43:44 +08:00
# 带有 gcc 编译器
2022-07-31 18:36:44 +08:00
if [ "${with_gcc}" = true ]; then
2022-10-01 22:33:38 +08:00
echo "${RED} ... build with-gcc${NC}"
2022-07-31 18:36:44 +08:00
cp ${gcc_install}/* ${diskfs} -r
cp ${binutils_install}/usr/x86_64-pc-linux-gnu/* ${diskfs} -r
2022-07-28 23:43:44 +08:00
fi
2022-07-31 01:54:15 +08:00
rm -rf ${diskfs}/init ${diskfs}/lost+found
2022-07-28 23:43:44 +08:00
2022-10-01 22:33:38 +08:00
# 测试用户登陆模式: root/123456
if [ "${with_login}" = true ]; then
echo "${RED} ... build with-login${NC}"
./mk_login.sh ${diskfs}
fi
2022-09-26 20:56:15 +08:00
# 带有 xfce 编译器
if [ "${with_xfce}" = true ]; then
2022-09-27 22:16:11 +08:00
echo "${RED} ... build xfce desktop${NC}"
2022-10-01 22:33:38 +08:00
# 构建 Xorg 的键盘数据
2022-10-01 19:47:23 +08:00
rm ${xfce_install}/usr/local/share/X11/xkb -rf
ln -s /usr/share/X11/xkb ${xfce_install}/usr/local/share/X11
2022-10-10 21:19:13 +08:00
# 依赖版本 libpcre.so.3
2022-10-01 22:33:38 +08:00
if [ -f "${xfce_install}/usr/local/lib/libpcre.so.1" ]; then
2022-10-13 22:19:12 +08:00
cp ${xfce_install}/usr/local/lib/libpcre.so.1 ${xfce_install}/usr/local/lib/libpcre.so.3
2022-10-01 22:33:38 +08:00
fi
2022-10-09 21:52:51 +08:00
# 依赖版本 libedit2
if [ -f "${xfce_install}/usr/local/lib/libedit.so.0" ]; then
2022-10-13 22:19:12 +08:00
cp ${xfce_install}/usr/local/lib/libedit.so.0 ${xfce_install}/usr/local/lib/libedit.so.2
2022-10-09 21:52:51 +08:00
fi
# 依赖版本 libtinfo.so.5
if [ -f "${xfce_install}/usr/lib/libtinfo.so.6" ]; then
2022-10-13 22:19:12 +08:00
cp ${xfce_install}/usr/lib/libtinfo.so.6 ${xfce_install}/usr/lib/libtinfo.so.5
2022-10-09 21:52:51 +08:00
fi
2022-10-10 21:19:13 +08:00
# 依赖版本 libffi.so.6
if [ -f "${xfce_install}/usr/local/lib/libffi.so.8" ]; then
cp ${xfce_install}/usr/local/lib/libffi.so.8 ${xfce_install}/usr/local/lib/libffi.so.6
fi
2022-10-01 22:33:38 +08:00
# dbus 用户添加
2022-10-09 21:52:51 +08:00
echo "video:x:44:" >> ${diskfs}/etc/group
2022-10-01 22:33:38 +08:00
echo "messagebus:x:107:" >> ${diskfs}/etc/group
echo "messagebus:x:103:107::/nonexistent:/usr/sbin/nologin" >> ${diskfs}/etc/passwd
# dbus 启动脚本
# dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
# dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
# dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 3
2022-10-18 20:55:17 +08:00
echo "if [ -f "/swapfile" ]; then" > ${diskfs}/xfce.sh
echo " dd if=/dev/zero of=/swapfile bs=1M count=2048" >> ${diskfs}/xfce.sh
echo " mkswap /swapfile" >> ${diskfs}/xfce.sh
echo "fi" >> ${diskfs}/xfce.sh
2022-10-17 22:26:20 +08:00
echo "swapon /swapfile" >> ${diskfs}/xfce.sh
echo "dbus-daemon --system --nopidfile --systemd-activation" >> ${diskfs}/xfce.sh
2022-10-09 21:52:51 +08:00
echo "xinit /usr/local/bin/xfce4-session -- /usr/local/bin/Xorg :10" >> ${diskfs}/xfce.sh
2022-10-01 19:47:23 +08:00
chmod +x ${diskfs}/xfce.sh
2022-10-09 07:05:29 +08:00
# 添加 machine-id
2022-10-09 21:52:51 +08:00
mkdir -p ${diskfs}/usr/local/var/lib/dbus
echo "2add25d2f5994832ba171755bc21f9fe" > ${diskfs}/etc/machine-id
echo "2add25d2f5994832ba171755bc21f9fe" > ${diskfs}/usr/local/var/lib/dbus/machine-id
2022-10-09 07:05:29 +08:00
# 这些本来需要编译完成,目前暂且拷贝
2022-10-09 21:52:51 +08:00
cp /usr/lib/x86_64-linux-gnu/libLLVM-10.so.1 build/xfce_install/usr/lib/x86_64-linux-gnu/
# cp /usr/lib/x86_64-linux-gnu/libffi.so.6 build/xfce_install/usr/lib/x86_64-linux-gnu/
# 拷贝 xfce4 到镜像目录
cp ${xfce_install}/* ${diskfs} -r -n
2022-09-28 22:03:27 +08:00
# xfce 需要系统内执行下面两句,保证键盘数据存在 Xorg :10 才能执行成功
# 1. 键盘数据
# rm /usr/local/share/X11/xkb -rf
# ln -s /usr/share/X11/xkb /usr/local/share/X11
# 2. 需要改动 libpcre.so.1 ---> libpcre.so.3
# 3. xfce4-session 需要 libuuid.so
2022-10-18 20:55:17 +08:00
# 依赖版本 libpcre.so.3
if [ -f "${xfce_install}/usr/local/lib/libpcre.so.1" ]; then
rm ${xfce_install}/usr/local/lib/libpcre.so.3 -rf
fi
# 依赖版本 libedit2
if [ -f "${xfce_install}/usr/local/lib/libedit.so.0" ]; then
rm ${xfce_install}/usr/local/lib/libedit.so.2 -rf
fi
# 依赖版本 libtinfo.so.5
if [ -f "${xfce_install}/usr/lib/libtinfo.so.6" ]; then
rm ${xfce_install}/usr/lib/libtinfo.so.5 -rf
fi
# 依赖版本 libffi.so.6
if [ -f "${xfce_install}/usr/local/lib/libffi.so.8" ]; then
rm ${xfce_install}/usr/local/lib/libffi.so.6 -rf
fi
2022-09-26 20:56:15 +08:00
fi
2022-07-28 23:43:44 +08:00
# 我们测试驱动, 制作的镜像启动后,我们进入此目录 insmod hello_world.ko 即可
2022-07-31 00:01:00 +08:00
./mk_drv.sh $(pwd)/${diskfs}/lib/modules
2022-07-28 23:43:44 +08:00
# 编译网卡驱动 ( 目前版本内核已集成 e1000 )
2022-10-10 21:19:13 +08:00
# cd ${build_dir}/linux-5.8.6 && make M=drivers/net/ethernet/intel/e1000/ && cd ../..
2022-07-28 23:43:44 +08:00
2022-07-31 00:01:00 +08:00
# 生成 grub.cfg 文件, 增加 console=ttyS0 就会让 qemu 输出日志到 qemu.log
2022-07-28 23:43:44 +08:00
cat - > ${diskfs}/boot/grub/grub.cfg << EOF
2022-07-31 00:01:00 +08:00
set timeout=3
2022-07-28 23:43:44 +08:00
menuentry "smart-os" {
root=(hd0,msdos1)
linux /boot/bzImage console=tty0
initrd /boot/initrd
}
EOF
2022-08-01 22:57:30 +08:00
# 生成 /etc/resolv.conf 文件
cat -> ${diskfs}/etc/resolv.conf << EOF
nameserver 8.8.8.8
nameserver 114.114.114.114
EOF
cat -> ${diskfs}/etc/fstab << EOF
# <file system> <dir> <type> <options> <dump> <pass>
proc /proc proc defaults 0 0
tmpfs /tmp tmpfs defaults 0 0
sysfs /sys sysfs defaults 0 0
EOF
2022-07-28 23:43:44 +08:00
# 生成 /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 功能
2022-10-17 22:26:20 +08:00
# qemu
# ifconfig eth0 192.168.100.6 && ifconfig eth0 up
# route add default gw 192.168.100.1
# vmware
ifconfig eth0 192.168.222.195 && ifconfig eth0 up
route add default gw 192.168.222.2
2022-07-28 23:43:44 +08:00
# exec 执行 /etc/init.d/rc.local 脚本
2022-10-19 22:09:51 +08:00
# 启动 sshd 服务,保证远程连接,调试跟踪非常方便
2022-10-17 22:26:20 +08:00
/usr/sbin/sshd
2022-07-28 23:43:44 +08:00
EOF
chmod +x ${diskfs}/etc/init.d/rcS
2022-09-26 20:56:15 +08:00
# 登陆 login shell ,非 non-login shell
if [ "${with_login}" = true ]; then
2022-10-18 20:55:17 +08:00
echo "export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" >> ${diskfs}/etc/profile
echo "export LD_LIBRARY_PATH=/lib:/lib64:/usr/lib:/usr/lib64:/usr/local/lib:/usr/local/lib64:/usr/lib/x86_64-linux-gnu" >> ${diskfs}/etc/profile
2022-09-26 20:56:15 +08:00
else
2022-10-18 20:55:17 +08:00
echo "export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" >> ${diskfs}/etc/bash.bashrc
echo "export LD_LIBRARY_PATH=/lib:/lib64:/usr/lib:/usr/lib64:/usr/local/lib:/usr/local/lib64:/usr/lib/x86_64-linux-gnu" >> ${diskfs}/etc/bash.bashrc
2022-09-26 20:56:15 +08:00
fi
2022-07-28 23:43:44 +08:00
echo "${GREEN}+++ build diskfs ok +++${NC}"
# 卸载映射
umount ${loop_dev}
losetup -d ${loop_dev}
2022-10-18 20:55:17 +08:00
#----------------------------------------------------------------
#
# 常用命令
#
#----------------------------------------------------------------
# 查看CPU信息cat /proc/cpuinfo
# 查看板卡信息cat /proc/pci
# 查看PCI信息lspci (相比cat /proc/pci更直观)
# 查看内存信息cat /proc/meminfo
# 查看USB设备cat /proc/bus/usb/devices
# 查看键盘和鼠标:cat /proc/bus/input/devices
# 查看系统硬盘信息和使用情况fdisk & disk - l & df
# 查看各设备的中断请求(IRQ):cat /proc/interrupts
# 查看系统体系结构uname -a
# dmidecode查看硬件信息包括bios、cpu、内存等信息
# dmesg | more 查看硬件信息
# modinfo命令可以单看指定的模块/驱动的信息
2022-10-19 22:09:51 +08:00
# 查看设备名称 cat /sys/class/input/mouse2/device/name
2022-10-18 20:55:17 +08:00
# linux为什么访问设备数据先要mount? https://www.zhihu.com/question/524667726
2022-07-28 23:43:44 +08:00
#---------------------------------------------------------------
#
# 查看磁盘内容
#
#---------------------------------------------------------------
2022-08-08 22:41:05 +08:00
./ls_img.sh
2022-07-28 23:43:44 +08:00
2022-10-17 22:26:20 +08:00
#---------------------------------------------------------------
#
# 转换为 vmware 格式
#
#---------------------------------------------------------------
qemu-img convert disk.img -f raw -O vmdk disk.vmdk
2022-07-28 23:43:44 +08:00
echo "Run the next script: 03_run_qemu.sh or 04_run_docker.sh"