make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- -j64 menuconfig
CROSS_COMPILE=riscv64-linux-gnu- -j64 CROSS_COMPILE=riscv64-linux-gnu- -j64 install root@ubuntu:/home/ubuntu/busybox-1.32.1# find ./ -name tcpdump root@ubuntu:/home/ubuntu/busybox-1.32.1# cd _install/ root@ubuntu:/home/ubuntu/busybox-1.32.1/_install# ls bin linuxrc sbin usr root@ubuntu:/home/ubuntu/busybox-1.32.1/_install# mkdir bin dev tmp proc sys mkdir: cannot create directory ‘bin’: File exists root@ubuntu:/home/ubuntu/busybox-1.32.1/_install# ls bin dev linuxrc proc sbin sys tmp usr root@ubuntu:/home/ubuntu/busybox-1.32.1/_install# root@ubuntu:/home/ubuntu/busybox-1.32.1/_install# ls bin linuxrc sbin usr ---------文件系统简单,后续上新功能一大堆问题, root@ubuntu:/home/ubuntu/busybox-1.32.1/_install# mkdir bin dev tmp proc sys mkdir: cannot create directory ‘bin’: File exists root@ubuntu:/home/ubuntu/busybox-1.32.1/_install# ls bin dev linuxrc proc sbin sys tmp usr root@ubuntu:/home/ubuntu/busybox-1.32.1/_install# ln -s ../bin/busybox sbin/init ln: failed to create symbolic link 'sbin/init': File exists root@ubuntu:/home/ubuntu/busybox-1.32.1/_install# ln -s bin/busybox linuxrc ln: failed to create symbolic link 'linuxrc': File exists root@ubuntu:/home/ubuntu/busybox-1.32.1/_install# ln -s sbin/init init root@ubuntu:/home/ubuntu/busybox-1.32.1/_install#
root@ubuntu:/home/ubuntu/ busybox-1.32.1/_install# mknod dev/console c 5 1
root@ubuntu:/home/ubuntu/ busybox-1.32.1/_install# mknod dev/ram b 1 0
mkdir bin dev tmp proc sys # 创建一些系统目录 cp inittab $RISCV/rootfs/etc/inittab # 拷贝进inittab 文档(可选,可以不拷贝) ln -s ../bin/busybox sbin/init # 设定init起始文档连接到busybox ln -s bin/busybox linuxrc # ln -s sbin/init init # mknod dev/console c 5 1 # 设置一些必要的虚拟外设() mknod dev/null c 1 3 /home/ubuntu/busybox-1.32.1# find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../initrams.my.gz
qemu-system-riscv64 -nographic -machine virt -bios fw_jump.elf -m 1024 \ -kernel ~/riscv_debug/linux-5.14/arch/riscv/boot/Image -append "root=/dev/vda ro console=ttyS0,115200n8 earlycon" \ -initrd initrams.my.gz
参考
https://blog.csdn.net/ALLap97/article/details/114300337
install之后的命令
root@ubuntu:/home/ubuntu/~busybox-1.32.1# find ./ -name ping ./_install/bin/ping root@ubuntu:/home/ubuntu/~busybox-1.32.1# find ./ -name iperf root@ubuntu:/home/ubuntu/~busybox-1.32.1# ls _install/usr/sbin/ addgroup chroot fakeidentd i2cdump loadfont ntpd readprofile telnetd ubirsvol add-shell crond fbset i2cget lpd partprobe remove-shell tftpd ubiupdatevol adduser delgroup fdformat i2cset mim popmaildir rtcwake ubiattach udhcpd arping deluser fsfreeze i2ctransfer nanddump powertop sendmail ubidetach brctl dhcprelay ftpd ifplugd nandwrite rdate setfont ubimkvol chat dnsd httpd inetd nbd-client rdev setlogcons ubirename chpasswd ether-wake i2cdetect killall5 nologin readahead svlogd ubirmvol root@ubuntu:/home/ubuntu/~busybox-1.32.1# ls _install/usr/bin/ '[' cut flock logname nsenter runsv ssl_client truncate w '[[' dc fold lpq nslookup runsvdir strings ts wall awk deallocvt free lpr od rx sum tty wc basename diff ftpget lsof openvt script sv ttysize wget bc dirname ftpput lspci passwd seq svc udhcpc6 which beep dos2unix fuser lsscsi paste setfattr svok udpsvd who blkdiscard dpkg groups lsusb patch setkeycodes tac unexpand whoami bunzip2 dpkg-deb hd lzcat pgrep setsid tail uniq whois bzcat du head lzma pkill setuidgid taskset unix2dos xargs bzip2 dumpleases hexdump man pmap sha1sum tcpsvd unlink xxd cal eject hexedit md5sum printf sha256sum tee unlzma xz chpst env hostid mesg pscan sha3sum telnet unshare xzcat chrt envdir id microcom pstree sha512sum test unxz yes chvt envuidgid install mkfifo pwdx showkey tftp unzip cksum expand ipcrm mkpasswd readlink shred time uptime clear expr ipcs nc realpath shuf timeout users cmp factor killall nl renice smemcap top uudecode comm fallocate last nmeter reset softlimit tr uuencode crontab fgconsole less nohup resize sort traceroute vlock cryptpw find logger nproc rpm2cpio split traceroute6 volname root@ubuntu:/home/ubuntu/~busybox-1.32.1#
交叉编译ethtool
root@ubuntu:/home/ubuntu# git clone git://git.kernel.org/pub/scm/network/ethtool/ethtool.git Cloning into 'ethtool'... remote: Enumerating objects: 37, done. remote: Counting objects: 100% (37/37), done. remote: Compressing objects: 100% (37/37), done. remote: Total 3129 (delta 21), reused 0 (delta 0), pack-reused 3092 Receiving objects: 100% (3129/3129), 1.29 MiB | 2.66 MiB/s, done. Resolving deltas: 100% (2171/2171), done. root@ubuntu:/home/ubuntu# riscv64-linux-gnu-gcc -v Using built-in specs. COLLECT_GCC=riscv64-linux-gnu-gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc-cross/riscv64-linux-gnu/7/lto-wrapper Target: riscv64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.5.0-3ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,c++,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix -
-libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-libitm --disable-libsanitizer --disable-libquadmath --disable-libquadmath-support --enable-plugin --with-system-zlib --enable-multiarch --disable-werror --disable-multilib --with-arch=rv64imafdc --with-abi=lp64d --enable-checking=release --build=aarch64-linux-gnu --host=aarch64-linux-gnu
--target=riscv64-linux-gnu
--program-prefix=riscv64-linux-gnu- --includedir=/usr/riscv64-linux-gnu/include Thread model: posix gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
-host:代码所在机器
root@ubuntu:/home/ubuntu/ethtool# ./configure --host=aarch64-linux-gnu CC=riscv64-linux-gnu-gcc LDFLAGS=-static
configure: error: Package requirements (libmnl) were not met: No package 'libmnl' found Consider adjusting the PKG_CONFIG_PATH environment variable if you installed software in a non-standard prefix. Alternatively, you may set the environment variables MNL_CFLAGS and MNL_LIBS to avoid the need to call pkg-config. See the pkg-config man page for more details. root@ubuntu:/home/ubuntu/ethtool#
交叉编译tcpdump
pcap和tcpdump的prefix要一致
--prefix=/home/ubuntu/rams3/install
ubuntu@ubuntu:~/ rams3$ mkdir /home/ubuntu/ rams3/install ubuntu@ubuntu:~/ rams3$ export CC=riscv64-linux-gnu-gcc ubuntu@ubuntu:~/ rams3$ cd libpcap/ ubuntu@ubuntu:~/ rams3/libpcap$ ./configure --prefix=/home/ubuntu/rams3/install --host=riscv64-linux --with-pcap=linux ubuntu@ubuntu:~/ rams3/libpcap$ make -j 64 ubuntu@ubuntu:~/ rams3/libpcap$ make -j 64 install ubuntu@ubuntu:~/ rams3/tcpdump$ ./configure --prefix=/home/ubuntu/rams3/install --host=riscv64-linux ubuntu@ubuntu:~/ rams3/tcpdump$ make -j64 ubuntu@ubuntu:~/ rams3/tcpdump$ find ./ -name tcpdump ./tcpdump ubuntu@ubuntu:~/ rams3/tcpdump$ file tcpdump tcpdump: ELF 64-bit LSB shared object, UCB RISC-V, version 1 (SYSV), dynamically linked, interpreter /lib/ld-, for GNU/Linux 4.15.0, BuildID[sha1]=b2d3378702e876cedc1bc339bb1b3ef13fc4dd3a, with debug_info, not stripped ubuntu@ubuntu:~/ rams3/tcpdump$
将tcpdump拷贝到
root@ubuntu:/home/ubuntu/ busybox-1.32.1# find ./ -name tcpdump ./_install/usr/sbin/tcpdump root@ubuntu:/home/ubuntu/ busybox-1.32.1# find ./ -name ethool root@ubuntu:/home/ubuntu/ busybox-1.32.1# find ./ -name ethtool ./_install/usr/sbin/ethtool root@ubuntu:/home/ubuntu/ busybox-1.32.1#
ethool
root@ubuntu:/home/ubuntu/ busybox-1.32.1/_install/usr/lib# ln -s libmnl.so.0.2.0 libmnl.so.0 drwxr-xr-x 5 root root 4096 Dec 3 17:52 .. lrwxrwxrwx 1 root root 15 Dec 3 17:53 libmnl.so.0 -> libmnl.so.0.2.0 -rw-r--r-- 1 root root 14248 Dec 3 16:36 libmnl.so.0.2.0 root@ubuntu:/home/ubuntu/ busybox-1.32.1/_install/usr/lib#
tcpdump: socket: Address family not supported by protocol
The remedy If necessary, update your module dependencies and then load the module manually: root@linux# depmod -a root@linux# modprobe af_packet If required, check whether the module is present: root@linux# find /lib/modules -name "af_packet*" /lib/modules/2.6.13-15-default/kernel/net/packet/af_packet.ko If modprobe does not work, you can try to load the module directly with insmod: root@linux# insmod /lib/modules/2.6.13-15-default/kernel/net/packet/af_packet.ko
can't open /dev/tty3: No such file or directory can't open /dev/tty4: No such file or directory can't open /dev/tty2: No such file or directory can't open /dev/tty3: No such file or directory can't open /dev/tty4: No such file or directory can't open /dev/tty2: No such file or directory can't open /dev/tty3: No such file or directory can't open /dev/tty2: No such file or directory can't open /dev/tty4: No such file or directory can't open /dev/tty3: No such file or directory can't open /dev/tty2: No such file or directory can't open /dev/tty4: No such file or directory can't open /dev/tty3: No such file or directory can't open /dev/tty2: No such file or directory can't open /dev/tty4: No such file or directory can't open /dev/tty3: No such file or directory can't open /dev/tty2: No such file or directory can't open /dev/tty4: No such file or directory can't open /dev/tty3: No such file or directory can't open /dev/tty2: No such file or directory can't open /dev/tty4: No such file or directory can't open /dev/tty3: No such file or directory can't open /dev/tty2: No such file or directory can't open /dev/tty4: No such file or directory can't open /dev/tty3: No such file or directory can't open /dev/tty2: No such file or directory can't open /dev/tty4: No such file or directory can't open /dev/tty3: No such file or directory can't open /dev/tty2: No such file or directory can't open /dev/tty4: No such file or directory can't open /dev/tty3: No such file or directory / # can't open /dev/tty4: No such file or directory can't open /dev/tty2: No such file or directory can't open /dev/tty3: No such file or directory can't open /dev/tty2: No such file or directory can't open /dev/tty4: No such file or directory f
简单解决办法
不断弹出这样的报错信息
can't open /dev/tty3: No such file or directory
can't open /dev/tty2: No such file or directory
can't open /dev/tty4: No such file or directory
输入
ln -sf /dev/null /dev/tty2
ln -sf /dev/null /dev/tty3
ln -sf /dev/null /dev/tty4
就可以正常使用了
复杂解决办法
原来是没有这几个文件
root@ubuntu:/home/ubuntu/ busybox-1.32.1/_install# cat etc/init.d/rcS cat: etc/init.d/rcS: No such file or directory root@ubuntu:/home/ubuntu/ busybox-1.32.1/_install# cat etc/fstab cat: etc/fstab: No such file or directory root@ubuntu:/home/ubuntu/ busybox-1.32.1/_install# etc/inittab bash: etc/inittab: No such file or directory root@ubuntu:/home/ubuntu/ busybox-1.32.1/_install#
用buildroot构建的rootfs有
正常的有
[root@centos7 rams]# cat etc/init.d/rcS #!/bin/sh # Start all init scripts in /etc/init.d # executing them in numerical order. # for i in /etc/init.d/S??* ;do # Ignore dangling symlinks (if any). [ ! -f "$i" ] && continue case "$i" in *.sh) # Source shell script for speed. ( trap - INT QUIT TSTP set start . $i ) ;; *) # No sh extension, so fork subprocess. $i start ;; esac done [root@centos7 rams]# cat etc/fstab # <file system> <mount pt> <type> <options> <dump> <pass> /dev/root / ext2 rw,noauto 0 1 proc /proc proc defaults 0 0 devpts /dev/pts devpts defaults,gid=5,mode=620,ptmxmode=0666 0 0 tmpfs /dev/shm tmpfs mode=0777 0 0 tmpfs /tmp tmpfs mode=1777 0 0 tmpfs /run tmpfs mode=0755,nosuid,nodev 0 0 sysfs /sys sysfs defaults 0 0 [root@centos7 rams]# cat etc/inittab # /etc/inittab # # Copyright (C) 2001 Erik Andersen <andersen@codepoet.org> # # Note: BusyBox init doesn't support runlevels. The runlevels field is # completely ignored by BusyBox init. If you want runlevels, use # sysvinit. # # Format for each entry: <id>:<runlevels>:<action>:<process> # # id == tty to run on, or empty for /dev/console # runlevels == ignored # action == one of sysinit, respawn, askfirst, wait, and once # process == program to run # Startup the system ::sysinit:/bin/mount -t proc proc /proc ::sysinit:/bin/mount -o remount,rw / ::sysinit:/bin/mkdir -p /dev/pts /dev/shm ::sysinit:/bin/mount -a ::sysinit:/sbin/swapon -a null::sysinit:/bin/ln -sf /proc/self/fd /dev/fd null::sysinit:/bin/ln -sf /proc/self/fd/0 /dev/stdin null::sysinit:/bin/ln -sf /proc/self/fd/1 /dev/stdout null::sysinit:/bin/ln -sf /proc/self/fd/2 /dev/stderr ::sysinit:/bin/hostname -F /etc/hostname # now run any rc scripts ::sysinit:/etc/init.d/rcS # Put a getty on the serial port console::respawn:/sbin/getty -L console 0 vt100 # GENERIC_SERIAL # Stuff to do for the 3-finger salute #::ctrlaltdel:/sbin/reboot # Stuff to do before rebooting ::shutdown:/etc/init.d/rcK ::shutdown:/sbin/swapoff -a ::shutdown:/bin/umount -a -r
添加etc目录之后
modprobe: can't change directory to '/lib/modules': No such file or directory Initializing random number generator... [ 8.710573] random: dd: uninitialized urandom read (512 bytes read) Jan 1 00:00:08 buildroot user.notice kernel: [ 8.710573] random: dd: uninitialized urandom read (512 bytes read) done. Starting network: ifup: can't open '/var/run/ifstate.new': No such file or directory FAIL Starting dropbear sshd: start-stop-daemon: can't execute '/usr/sbin/dropbear': No such file or directory FAIL Jan 1 00:00:08 buildroot daemon.info nit: starting pid 83, tty '/dev/console': '/sbin/getty -L console 0 vt100 ' Welcome to Buildroot buildroot login: root Password: Jan 1 00:03:41 buildroot auth.info login[83]: root login on 'console' # ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: sit0@NONE: <NOARP> mtu 1480 qdisc noop qlen 1000 link/sit 0.0.0.0 brd 0.0.0.0 # ethtool -sh: ethtool: not found # cd / # ls bin etc linuxrc proc run sys usr dev init net.sh root sbin tmp # ls etc/ fstab inittab nsswitch.conf protocols shells group issue os-release random-seed hostname mdev.conf passwd screenrc hosts mke2fs.conf profile services init.d network profile.d shadow # ls usr/sbin/ethtool usr/sbin/ethtool #
-sh: /usr/sbin/tcpdump: not found
modprobe: can't change directory to '/lib/modules': No such file or directory Initializing random number generator... [ 8.711048] random: dd: uninitialized urandom read (512 bytes read) Jan 1 00:00:08 buildroot user.notice kernel: [ 8.711048] random: dd: uninitialized urandom read (512 bytes read) done. Starting network: ifup: can't open '/var/run/ifstate.new': No such file or directory FAIL Starting dropbear sshd: start-stop-daemon: can't execute '/usr/sbin/dropbear': No such file or directory FAIL Jan 1 00:00:08 buildroot daemon.info nit: starting pid 83, tty '/dev/console': '/sbin/getty -L console 0 vt100 ' Welcome to Buildroot buildroot login: root Password: Jan 1 00:00:14 buildroot auth.info login[83]: root login on 'console' # ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: sit0@NONE: <NOARP> mtu 1480 qdisc noop qlen 1000 link/sit 0.0.0.0 brd 0.0.0.0 # tcpdump -h -sh: tcpdump: not found # find ./ -name tcpdump # ls /usr/sbin/tcpdump /usr/sbin/tcpdump # /usr/sbin/tcpdump -sh: /usr/sbin/tcpdump: not found # ls -al /usr/sbin/tcpdump -rwxr-xr-x 1 root root 7156824 Dec 3 2021 /usr/sbin/tcpdump # /usr/sbin/tcpdump -h -sh: /usr/sbin/tcpdump: not found #
原来是busybox中没有这个命令
root@ubuntu:/home/ubuntu/busybox-1.32.1/_install# ls usr/sbin/ addgroup chroot ethtool i2cdetect killall5 nologin readahead svlogd ubirename add-shell crond fakeidentd i2cdump loadfont ntpd readprofile tcpdump ubirmvol adduser delgroup fbset i2cget lpd partprobe remove-shell telnetd ubirsvol arping deluser fdformat i2cset mim popmaildir rtcwake tftpd ubiupdatevol brctl dhcprelay fsfreeze i2ctransfer nanddump powertop sendmail ubiattach udhcpd chat dnsd ftpd ifplugd nandwrite rdate setfont ubidetach chpasswd ether-wake httpd inetd nbd-client rdev setlogcons ubimkvol root@ubuntu:/home/ubuntu/busybox-1.32.1/_install# grep chpasswd -rn * Binary file bin/busybox matches Binary file init matches Binary file linuxrc matches root@ubuntu:/home/ubuntu/busybox-1.32.1/_install# grep httpd -rn * Binary file bin/busybox matches Binary file init matches Binary file linuxrc matches root@ubuntu:/home/ubuntu/busybox-1.32.1/_install# grep tcpdump -rn * Binary file usr/sbin/tcpdump matches root@ubuntu:/home/ubuntu/busybox-1.32.1/_install#
最终方法
1、找个成熟的initrams
2、交叉编译tcpdump 、ethtool
3、更新initrams
方法二
debootstraps构造的rootfs
root@ubuntu:/home/ubuntu/data2# find ./ -name libm.so.6 ./usr/lib/riscv64-linux-gnu/libm.so.6 root@ubuntu:/home/ubuntu/data2# find ./ -name libresolv.so.2 ./usr/lib/riscv64-linux-gnu/libresolv.so.2 root@ubuntu:/home/ubuntu/data2# find ./ -name libc.so.6 ./usr/lib/riscv64-linux-gnu/libc.so.6 root@ubuntu:/home/ubuntu/data2# find ./ -name ld-linux-riscv64-lp64d.so.1 ./usr/lib/riscv64-linux-gnu/ld-linux-riscv64-lp64d.so.1 ./usr/lib/ld-linux-riscv64-lp64d.so.1 root@ubuntu:/home/ubuntu/data2#
先用 debootstrap构造rootfs
make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- -j64
Static linking against glibc, can't use --gc-sections Trying libraries: crypt m resolv rt Library crypt is not needed, excluding it Library m is needed, can't exclude it (yet) Library resolv is needed, can't exclude it (yet) Library rt is not needed, excluding it Library m is needed, can't exclude it (yet) Library resolv is needed, can't exclude it (yet) Final link with: m resolv
有这些报错也会生成busybox
root@ubuntu:/home/ubuntu/busybox-1.32.1# ls rootfs/lib64 ld-uClibc-1.0.39.so ld-uClibc.so.1 libatomic.so.1 libc.so.0 libgcc_s.so libuClibc-1.0.39.so ld-uClibc.so.0 libatomic.so libatomic.so.1.2.0 libc.so.1 libgcc_s.so.1 root@ubuntu:/home/ubuntu/busybox-1.32.1# 查看busybox依赖的so root@ubuntu:/home/ubuntu/busybox-1.32.1# ls busybox -al -rwxr-xr-x 1 root root 1038120 Dec 4 18:42 busybox root@ubuntu:/home/ubuntu/busybox-1.32.1# readelf -d busybox | grep NEEDED 0x0000000000000001 (NEEDED) Shared library: [libm.so.6] 0x0000000000000001 (NEEDED) Shared library: [libresolv.so.2] 0x0000000000000001 (NEEDED) Shared library: [libc.so.6] 0x0000000000000001 (NEEDED) Shared library: [ld-linux-riscv64-lp64d.so.1]
构建内存 OS (基于 Ubuntu rootfs 和busybox)
http://linuxcoming.com/blog/2019/06/21/build_ram_os.html
包含glibc
1 Building a minimal RootFS with Busybox, GLIBC and DropBear