下载linux 内核版本 4.9.6
内核目录/vm/custom2/linux-4.9.6$
make x86_64_defconfig
make -j 8
在/vm/custom2/创建sysroot目录
mkdir -pv {bin,sbin,etc,proc,sys,usr/{bin,sbin}}
bash没有这些目录不会panic
/vm/custom2/目录下建立exportbin.sh
内容如下、
#!/bin/bash
if [ $# != 2 ] ; then
echo "usage $0 PATH_TO_BINARY TARGET_FOLDER"
exit 1
fi
PATH_TO_BINARY="$1"
TARGET_FOLDER="$2"
# if we cannot find the the binary we have to abort
if [ ! -f "$PATH_TO_BINARY" ] ; then
echo "The file '$PATH_TO_BINARY' was not found. Aborting!"
exit 1
fi
# copy the binary to the target folder
# create directories if required
echo "---> copy binary itself"
cp --parents -v "$PATH_TO_BINARY" "$TARGET_FOLDER"
# copy the required shared libs to the target folder
# create directories if required
echo "---> copy libraries"
for lib in `ldd "$PATH_TO_BINARY" | cut -d'>' -f2 | awk '{print $1}'` ; do
if [ -f "$lib" ] ; then
cp -v --parents "$lib" "$TARGET_FOLDER"
fi
done
# I'm on a 64bit system at home. the following code will be not required on a 32bit system.
# however, I've not tested that yet
# create lib64 - if required and link the content from lib to it
if [ ! -d "$TARGET_FOLDER/lib64" ] ; then
mkdir -v "$TARGET_FOLDER/lib64"
fi
用于copy基本的c库
chmod +x exportbin.sh
然后到/vm/custom2/目录下
运行
./exportbin.sh /bin/bash sysroot
其实可以自己编译bash,我使用ubuntu中的bash
下载 util-linux-2.29.2.tar.xz
首先安装ncurses
sudo apt install libncurses5-dev libncursesw5-dev
./configure --prefix=/vm/custom2/sysroot --without-python
make
sudo make install
然后copy
util用到so
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1
li@li-shuangbin:/vm/custom2/sysroot/lib/x86_64-linux-gnu$ cp -r /lib/x86_64-linux-gnu/librt.so.1 .
li@li-shuangbin:/vm/custom2/sysroot/lib/x86_64-linux-gnu$ ll /lib/x86_64-linux-gnu/librt.so.1
lrwxrwxrwx 1 root root 13 May 26 2016 /lib/x86_64-linux-gnu/librt.so.1 -> librt-2.19.so
li@li-shuangbin:/vm/custom2/sysroot/lib/x86_64-linux-gnu$ cp -r /lib/x86_64-linux-gnu/librt-2.19.so .
li@li-shuangbin:/vm/custom2/sysroot/lib/x86_64-linux-gnu$ cp -r /lib/x86_64-linux-gnu/libpthread.so.0 .
li@li-shuangbin:/vm/custom2/sysroot/lib/x86_64-linux-gnu$ ll /lib/x86_64-linux-gnu/libpthread.so.0
lrwxrwxrwx 1 root root 18 May 26 2016 /lib/x86_64-linux-gnu/libpthread.so.0 -> libpthread-2.19.so*
li@li-shuangbin:/vm/custom2/sysroot/lib/x86_64-linux-gnu$ cp -r /lib/x86_64-linux-gnu/libpthread-2.19.so .
在/vm/custom2/sysroot下新建mkcpio.sh
内容如下
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../obj/initramfs-bash-x86.cpio.gz
在/vm/custom2/sysroot下新建init
内容如下
#!/bin/bash
mount -t proc none /proc
mount -t sysfs none /sys
echo -e "\nBoot took $(cut -d' ' -f1 /proc/uptime) seconds\n"
mount -t devtmpfs none /dev
exec /bin/bash
chmod +x init
chmod +x mkcpio.sh
然后运行 ./mkcpio.sh
新建硬盘
mkfs -t ext4 hda.img
mkdir mnt
sudo mount hda.img mnt
编译coreutils
cd coreutils-8.27/
./configure --prefix=/vm/custom2/sysroot
make
sudo make install
exec switch_root /newroot /bin/systemd
sudo apt-get install intltool
sudo apt-get install libcap-dev
/vm/custom2/systemd-233$ ./autogen.sh
./configure --prefix=/vm/custom2/sysroot \
--sysconfdir=/etc \
--localstatedir=/var \
--config-cache \
--with-rootprefix= \
--with-rootlibdir=/lib \
--enable-split-usr \
--disable-firstboot \
--disable-ldconfig \
--disable-sysusers \
--without-python \
--with-default-dnssec=no \
--docdir=/usr/share/doc/systemd-233
在/vm/custom2/目录下,运行
qemu-system-x86_64 \
-kernel linux-4.9.6/arch/x86_64/boot/bzImage \
-initrd obj/initramfs-bash-x86.cpio.gz \
-hda obj/hda.img -append root=/dev/sda
如果没用有
-append root=/dev/sda
出现
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)
其实原因是
1.297807] VFS: Cannot open root device "(null)" or unknown-block(0,0): error -6
[ 1.298684] Please append a correct "root=" boot option; here are the available partitions:
[ 1.299651] 0800 1048576 sda [ 1.300084] driver: sd
[ 1.300376] 0b00 1048575 sr0 [ 1.300853] driver: sr
[ 1.301173] Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)
启动脚本不必添加
-append root=/dev/sda
因为qemu启动只有
128MB Ram,需要改为
qemu-system-x86_64 \
-m 1024 \
-kernel linux-4.9.6/arch/x86_64/boot/bzImage \
-initrd obj/initramfs-bash-x86.cpio.gz \
-nographic -append "console=ttyS0" \
-enable-kvm -hda obj/hda.img