你的位置:首页 > 操作系统

[操作系统]构建 ARM Linux 4.7.3 嵌入式开发环境 —— BusyBox 构建 RootFS


上一篇我们已经成功将 ARM Linux 4.7.3 的内核利用 U-BOOT 引导了起来。但是细心的你会发现,引导到后面,系统无法启动,出现内核恐慌 (Kernel Panic)。 原因是找不到文件系统。为了让内核成功启动,我们还需要构建一个根文件系统。为了后期开发的方便,我们采用 NFS 网络文件系统。

利用 BusyBox 构建 mini 根文件系统

  • 什么是 BusyBox ?

BusyBox 是一个遵循GPL协议、以自由软件形式发行的应用程序。Busybox在单一的可执行文件中提供了精简的Unix工具集,可运行于多款POSIX环境的操作系统,例如Linux(包括Android、Hurd、FreeBSD等等。 由于BusyBox可执行文件尺寸小、并通常使用 Linux内核,这使得它非常适合使用于嵌入式系统。 此外,由于BusyBox功能强大,因此有些人将 BusyBox 称为“嵌入式Linux的瑞士军刀”。

  • 下载最新 BusyBox 源码
$ wget -c http://busybox.net/downloads/busybox-1.25.0.tar.bz2$ tar xvf busybox-1.25.0.tar.bz2$ cd busybox-1.25.0

  • 配置编译 BusyBox
$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- defconfig$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- menuconfig

BusyBox Setting >> Build Options 勾选编译成静态文件。

busybox

$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- $ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- install$ cd _install$ mkdir proc sys dev etc etc/init.d$ touch etc/init.d/rcS$ cat << EOF > etc/init.d/rcS#!bin/shmount -t proc none /procmount -t sysfs none /sys/sbin/mdev -sEOF$ chmod +x etc/init.d/rcS$ sudo mkdir /rootfs$ sudo pax -r -w -p e . /rootfs$ sudo chmod 777 /rootfs -R$ sudo chown -R nobody:nobody /rootfs

到这里,根文件系统的内容已经准备好了,接下来配置 NFS 。

NFS 服务配置

$ sudo apt install nfs-kernel-server$ sudo touch /etc/exports$ sudo cat << EOF > /etc/exports/rootfs  *(rw,sync,no_root_squash,no_subtree_check)EOF$ sudo service start nfs-kernel-server.service

OK, 到这里 NFS 服务配置完毕。

最后的准备

  • 确保 Kernel 配置了 NFS 支持模块。
File systems --->  [*] Network File Systems --->    <*>  NFS client support    <*>   NFS client support for NFS version 3    [*]    NFS client support for the NFSv3 ACL protocol extension    [*]  Root file system on NFS
  • 修改 U-BOOT 传递给内核的引导参数

想要开机就生效,请参看本系列文章(一),在编译 U-BOOT 时配置。

setenv bootargs 'root=/dev/nfs rw nfsroot=192.168.0.3:/rootfs init=/linuxrc console=ttyAMA0 ip=192.168.0.5'

启动系统

$ sudo qemu-system-arm -M vexpress-a9 -m 128M -nographic -net nic -net tap,ifname=tap0 -kernel u-boot

U-Boot 2016.07 (Sep 16 2016 - 11:18:06 +0800)DRAM: 128 MiBWARNING: Caches not enabledFlash: 128 MiBMMC:  MMC: 0*** Warning - bad CRC, using default environmentIn:  serialOut:  serialErr:  serialNet:  smc911x-0Hit any key to stop autoboot: 0 => setenv bootargs 'root=/dev/nfs rw nfsroot=192.168.0.3:/home/rain/rootfs init=/linuxrc console=ttyAMA0 ip=192.168.0.5'=> tftp 0x62000000 kernelsmc911x: MAC 52:54:00:12:34:56smc911x: detected LAN9118 controllersmc911x: phy initializedsmc911x: MAC 52:54:00:12:34:56Using smc911x-0 deviceTFTP from server 192.168.0.3; our IP address is 192.168.0.5Filename 'kernel'.Load address: 0x62000000Loading: #################################################################   #################################################################   #################################################################   #################################################################   #################################################################   #################################################################   #################################################################   #################################################################   #################################################################   #################################################################   ################################################   1.6 MiB/sdoneBytes transferred = 3571036 (367d5c hex)smc911x: MAC 52:54:00:12:34:56=> bootz 0x62000000Kernel image @ 0x62000000 [ 0x000000 - 0x3643e8 ]Starting kernel ...Uncompressing Linux... done, booting the kernel.Booting Linux on physical CPU 0x0Linux version 4.7.3 (rain@rain-pc) (gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.1) ) #7 SMP Fri Sep 16 11:11:23 CST 2016CPU: ARMv7 Processor [410fc090] revision 0 (ARMv7), cr=10c5387dCPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cacheMachine model: V2P-CA9Memory policy: Data cache writebackCPU: All CPU(s) started in SVC mode.percpu: Embedded 13 pages/cpu @87eb8000 s23116 r8192 d21940 u53248Built 1 zonelists in Zone order, mobility grouping on. Total pages: 32512Kernel command line: root=/dev/nfs rw nfsroot=192.168.0.3:/home/rain/rootfs init=/linuxrc console=ttyAMA0 ip=192.168.0.5log_buf_len individual max cpu contribution: 4096 byteslog_buf_len total cpu_extra contributions: 12288 byteslog_buf_len min size: 16384 byteslog_buf_len: 32768 bytesearly log buf free: 14944(91%)PID hash table entries: 512 (order: -1, 2048 bytes)Dentry cache hash table entries: 16384 (order: 4, 65536 bytes)Inode-cache hash table entries: 8192 (order: 3, 32768 bytes)Memory: 121092K/131072K available (5261K kernel code, 161K rwdata, 1424K rodata, 1024K init, 155K bss, 9980K reserved, 0K cma-reserved)Virtual kernel memory layout:  vector : 0xffff0000 - 0xffff1000  (  4 kB)  fixmap : 0xffc00000 - 0xfff00000  (3072 kB)  vmalloc : 0x88800000 - 0xff800000  (1904 MB)  lowmem : 0x80000000 - 0x88000000  ( 128 MB)  modules : 0x7f000000 - 0x80000000  ( 16 MB)   .text : 0x80008000 - 0x807875c0  (7678 kB)   .init : 0x80800000 - 0x80900000  (1024 kB)   .data : 0x80900000 - 0x809284a0  ( 162 kB)    .bss : 0x8092a000 - 0x80950cc4  ( 156 kB)SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=4, Nodes=1Hierarchical RCU implementation.  Build-time adjustment of leaf fanout to 32.  RCU restricting CPUs from NR_CPUS=8 to nr_cpu_ids=4.RCU: Adjusting geometry for rcu_fanout_leaf=32, nr_cpu_ids=4NR_IRQS:16 nr_irqs:16 16GIC CPU mask not found - kernel will fail to boot.GIC CPU mask not found - kernel will fail to boot.L2C: platform modifies aux control register: 0x02020000 -> 0x02420000L2C: DT/platform modifies aux control register: 0x02020000 -> 0x02420000L2C-310 enabling early BRESP for Cortex-A9L2C-310 full line of zeros enabled for Cortex-A9L2C-310 dynamic clock gating disabled, standby mode disabledL2C-310 cache controller enabled, 8 ways, 128 kBL2C-310: CACHE_ID 0x410000c8, AUX_CTRL 0x46420001smp_twd: clock not found -2sched_clock: 32 bits at 24MHz, resolution 41ns, wraps every 89478484971nsclocksource: arm,sp804: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 1911260446275 nsConsole: colour dummy device 80x30Calibrating local timer... 92.79MHz.Calibrating delay loop... 516.09 BogoMIPS (lpj=2580480)pid_max: default: 32768 minimum: 301Mount-cache hash table entries: 1024 (order: 0, 4096 bytes)Mountpoint-cache hash table entries: 1024 (order: 0, 4096 bytes)CPU: Testing write buffer coherency: okCPU0: thread -1, cpu 0, socket 0, mpidr 80000000Setting up static identity map for 0x60100000 - 0x60100058Brought up 1 CPUsSMP: Total of 1 processors activated (516.09 BogoMIPS).CPU: All CPU(s) started in SVC mode.devtmpfs: initializedVFP support v0.3: implementor 41 architecture 3 part 30 variant 9 rev 0clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 nsNET: Registered protocol family 16DMA: preallocated 256 KiB pool for atomic coherent allocationscpuidle: using governor ladderof_amba_device_create(): amba_device_add() failed (-19) for /memory-controller@100e0000of_amba_device_create(): amba_device_add() failed (-19) for /memory-controller@100e1000of_amba_device_create(): amba_device_add() failed (-19) for /watchdog@100e5000of_amba_device_create(): amba_device_add() failed (-19) for /smb@04000000/motherboard/iofpga@7,00000000/sysctl@01000of_amba_device_create(): amba_device_add() failed (-19) for /smb@04000000/motherboard/iofpga@7,00000000/wdt@0f000hw-breakpoint: debug architecture 0x4 unsupported.Serial: AMBA PL011 UART driver10009000.uart: ttyAMA0 at MMIO 0x10009000 (irq = 35, base_baud = 0) is a PL011 rev1console [ttyAMA0] enabled1000a000.uart: ttyAMA1 at MMIO 0x1000a000 (irq = 36, base_baud = 0) is a PL011 rev11000b000.uart: ttyAMA2 at MMIO 0x1000b000 (irq = 37, base_baud = 0) is a PL011 rev11000c000.uart: ttyAMA3 at MMIO 0x1000c000 (irq = 38, base_baud = 0) is a PL011 rev1SCSI subsystem initializedusbcore: registered new interface driver usbfsusbcore: registered new interface driver hubusbcore: registered new device driver usbAdvanced Linux Sound Architecture Driver Initialized.clocksource: Switched to clocksource arm,sp804NET: Registered protocol family 2TCP established hash table entries: 1024 (order: 0, 4096 bytes)TCP bind hash table entries: 1024 (order: 1, 8192 bytes)TCP: Hash tables configured (established 1024 bind 1024)UDP hash table entries: 256 (order: 1, 8192 bytes)UDP-Lite hash table entries: 256 (order: 1, 8192 bytes)NET: Registered protocol family 1RPC: Registered named UNIX socket transport module.RPC: Registered udp transport module.RPC: Registered tcp transport module.RPC: Registered tcp NFSv4.1 backchannel transport module.hw perfevents: enabled with armv7_cortex_a9 PMU driver, 1 counters availablefutex hash table entries: 1024 (order: 4, 65536 bytes)workingset: timestamp_bits=29 max_order=15 bucket_order=0squashfs: version 4.0 (2009/01/31) Phillip Lougherjffs2: version 2.2. (NAND) © 2001-2006 Red Hat, Inc.9p: Installing v9fs 9p2000 file system supportio scheduler noop registered (default)clcd-pl11x 10020000.clcd: PL111 rev2 at 0x10020000clcd-pl11x 10020000.clcd: /clcd@10020000 hardware, 1024x768@59 displayConsole: switching to colour frame buffer device 128x48clcd-pl11x 1001f000.clcd: PL111 rev2 at 0x1001f000clcd-pl11x 1001f000.clcd: /smb@04000000/motherboard/iofpga@7,00000000/clcd@1f000 hardware, 640x480@59 display40000000.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000Intel/Sharp Extended Query Table at 0x0031Using buffer write method40000000.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000Intel/Sharp Extended Query Table at 0x0031Using buffer write methodConcatenating MTD devices:(0): "40000000.flash"(1): "40000000.flash"into device "40000000.flash"libphy: smsc911x-mdio: probedGeneric PHY 4e000000.etherne:01: attached PHY driver [Generic PHY] (mii_bus:phy_addr=4e000000.etherne:01, irq=-1)smsc911x 4e000000.ethernet eth0: MAC Address: 52:54:00:12:34:56isp1760 4f000000.usb: bus width: 32, oc: digitalisp1760 4f000000.usb: NXP ISP1760 USB Host Controllerisp1760 4f000000.usb: new USB bus registered, assigned bus number 1isp1760 4f000000.usb: Scratch test failed.isp1760 4f000000.usb: can't setup: -19isp1760 4f000000.usb: USB bus 1 deregisteredusbcore: registered new interface driver usb-storagemousedev: PS/2 mouse device common for all micertc-pl031 10017000.rtc: rtc core: registered pl031 as rtc0mmci-pl18x 10005000.mmci: Got CD GPIOmmci-pl18x 10005000.mmci: Got WP GPIOmmci-pl18x 10005000.mmci: mmc0: PL181 manf 41 rev0 at 0x10005000 irq 31,32 (pio)ledtrig-cpu: registered to indicate activity on CPUsusbcore: registered new interface driver usbhidusbhid: USB HID core driverinput: AT Raw Set 2 keyboard as /devices/platform/smb@04000000/smb@04000000:motherboard/smb@04000000:motherboard:iofpga@7,00000000/10006000.kmi/serio0/input/input0aaci-pl041 10004000.aaci: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 30aaci-pl041 10004000.aaci: FIFO 512 entriesoprofile: using arm/armv7-ca9NET: Registered protocol family 179pnet: Installing 9P2000 supportRegistering SWP/SWPB emulation handlerrtc-pl031 10017000.rtc: setting system clock to 2016-09-19 05:56:32 UTC (1474264592)smsc911x 4e000000.ethernet eth0: SMSC911x/921x identified at 0x892a0000, IRQ: 28input: ImExPS/2 BYD TouchPad as /devices/platform/smb@04000000/smb@04000000:motherboard/smb@04000000:motherboard:iofpga@7,00000000/10007000.kmi/serio1/input/input2IP-Config: Guessing netmask 255.255.255.0IP-Config: Complete:   device=eth0, hwaddr=52:54:00:12:34:56, ipaddr=192.168.0.5, mask=255.255.255.0, gw=255.255.255.255   host=192.168.0.5, domain=, nis-domain=(none)   bootserver=255.255.255.255, rootserver=192.168.0.3, rootpath=ALSA device list: #0: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 30VFS: Mounted root (nfs filesystem) on device 0:12.Freeing unused kernel memory: 1024K (80800000 - 80900000)nfs: server 192.168.0.3 not responding, still tryingnfs: server 192.168.0.3 OKPlease press Enter to activate this console. / # lsbin   dev   etc   linuxrc proc   sbin   sys   usr/ # ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1  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 forever2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000  link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff  inet 192.168.0.5/24 brd 192.168.0.255 scope global eth0    valid_lft forever preferred_lft forever/ # uname -aLinux 192.168.0.5 4.7.3 #7 SMP Fri Sep 16 11:11:23 CST 2016 armv7l GNU/Linux/ #

参考文章

  • Build an embedded Linux distro from scratch
  • Network File System (NFS)
  • Building a Root File System using BusyBox
  • u-boot引导kernel,用nfs挂载根文件系统

原创文章,转载请注明出处!