arm64 linux+busybox 内核编译

185 阅读12分钟

本文最开始发表于CSDN,两者都是一个账号,只是里面的僵尸账号太多了,所以在这里发布,希望能有所帮助:t.csdnimg.cn/SkWmQ

前言:最近学习arm,想要编译一个Linux,记录编译的过程

  • 环境:

OS:Ubuntu23.04 (注意不能使用WSL,也不能将源码位置放到NTFS的文件系统上否则无法解压!) 1.gcc-aarch64-linux-gnu
2.Linux Kernel: linux-6.1.92
3.busybox: busybox-1.36.1
4.qemu

一.下载配置Linux 

Linux内核官网:www.kernel.org/

linux-6.1.92:​

wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.1.92.tar.xz

安装必要文件:

sudo apt-get install gcc-aarch64-linux-gnu
sudo apt-get install libncurses5-dev  build-essential git bison flex libssl-dev

 设置CPU架构

export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-

设置系统选项

make defconfig
make menuconfig

 在此设置一下内容

  1. 添加hotplug支持:

    Device Drivers                                                                                                 
        -> Generic Driver Options                                                                                             
            -> Support for uevent helper                                                                                                                
                (/sbin/hotplug) path to uevent helper
    
  2. 添加initramfs的支持: 

    General setup --->
        [*]Initial RAM filesystem and RAM disk(initramfs/initrd) 
        support(rootfs) Initramfs souce file(s)
    
  3. 内核页分别配置为 :

    Kernel Features  --->
        Page size(4KB)  --->
        Virtual address space size(48-bit)--->
    

至此配置工作结束

二.下载和编译busybox

busybox官网:busybox.net

busybox-1.36.1 :

wget https://busybox.net/downloads/busybox-1.36.1.tar.bz2

解压:

tar jxvf busybox-1.36.1.tar.bz2

 设置CPU架构

export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-

 设置系统选项

make defconfig
make menuconfig

在这里设置一下内容:

Settings  ---> 
       --- Build Options  
       [*] Build static binary (no shared libs) 

编译:

make -j16

安装:

make install

默认路径为当期目录的_install文件夹,至此busybox静态编译完成

三.制作根文件系统rootfs

  •  创建根目录所需的目录

cd busybox-1.36.1/_install
mkdir -p dev etc home lib mnt proc root sys tmp var

根文件系统目录说明:

  1. /bin: 系统管理员和用户均可使用的命令
  2. /sbin: 系统管理员使用的系统命令
  3. /dev: 存储特殊文件或设备文件;设备两种类型:字符设备、块设备
  4. /etc: 系统配置文件
  5. /home: 普通用户目录
  6. /root:root用户目录
  7. /lib: 为系统启动或根文件上的应用程序(/bin,/sbin等)提供共享库,以及为内核提供内核模块
  8. /mnt:临时挂载点
  9. /tmp: 临时文件存储目录
  10. /usr:usr hierarchy,全局共享的只读数据路径
  11. /var:存储常发生变化的数据目录:cache、log等
  12. /proc: 基于内存的虚拟文件系统,用于为内核及进程存储其相关信息
  13. /sys:sysfs虚拟文件系统提供了一种比proc更为理想的访问内核数据的途径:其主要作用在于为管理linux设备提供一种统一模型的接口
  •  创建根目录所需的必要文件 

    cd busybox-1.36.1/_install/etc
    vim profile
    vim inittab
    chmod 755 inittab
    mkdir -p init.d/
    vim init.d/rcS
    chmod 755 init.d/rcS
    vim fstab
    

    inittab:

    ::sysinit:/etc/init.d/rcS
    ::respawn:-/bin/sh
    ::askfirst:-/bin/sh
    ::ctrlaltdel:/bin/umount -a -r
    

    rcS:

    /bin/mount -a
    mkdir -p /dev/pts
    mount -t devpts devpts /dev/pts
    echo /sbin/mdev > /proc/sys/kernel/hotplug
    mdev -s
    echo "Kernel Version:linux-6.1.92"
    echo "***********************************************"
    

    fstab:

    #device mount-point type option dump fsck
    proc  /proc proc  defaults 0 0
    temps /tmp  rpoc  defaults 0 0
    none  /tmp  ramfs defaults 0 0
    sysfs /sys  sysfs defaults 0 0
    mdev  /dev  ramfs defaults 0 0
    
  • 添加设备文件

    cd busybox-1.36.1/_install
    cd dev
    sudo mknod console c 5 1
    sudo mknod null c 1 3
    
  • 拷贝lib库,支持动态编译的应用程序运行 

    cd busybox-1.36.1/_install/lib
    cp /usr/aarch64-linux-gnu/lib/*.so*  -a .
    
  • 制作根文件系统镜像文件rootfs.cpio.gz (备用)

    cd busybox-1.36.1/_install
    find . | cpio -o -H newc > rootfs.cpio 
    gzip -c rootfs.cpio > rootfs.cpio.gz
    

至此rootfs制作完毕

mkdir ../linux-6.1.92/rootfs
sudo cp -rf _install/* ../linux-6.1.92/rootfs

 四.编译Linux

make -j 16

至此Linux编译及文件系统制作完毕

五.安装qemu

sudo apt-get install qemu-system-arm

启动:

qemu-system-aarch64 -machine virt -cpu cortex-a57 -machine type=virt -m 1024 -smp 4 -kernel arch/arm64/boot/Image --append "rdinit=/linuxrc root=/dev/vda rw console=ttyAMA0 loglevel=8"  -nographic 

qemu-system-aarch64 -cpu cortex-a57 -m 512M -machine type=virt -nographic -smp 1 -kernel Image -initrd rootfs.cpio.gz -append "rdinit=/linuxrc console=ttyAMA0" -device virtio-scsi-device
[    0.000000] Booting Linux on physical CPU 0x0000000000 [0x411fd070]
[    0.000000] Linux version 6.1.92 (root@DESKTOP-FF5UR23) (aarch64-linux-gnu-gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #1 SMP PREEMPT Thu Jun  6 15:36:23 CST 2024
[    0.000000] Machine model: linux,dummy-virt
[    0.000000] efi: UEFI not found.
[    0.000000] NUMA: No NUMA configuration found
[    0.000000] NUMA: Faking a node at [mem 0x0000000040000000-0x000000005fffffff]
[    0.000000] NUMA: NODE_DATA [mem 0x5ff83900-0x5ff862ff]
[    0.000000] Zone ranges:
[    0.000000]   DMA      [mem 0x0000000040000000-0x000000005fffffff]
[    0.000000]   DMA32    empty
[    0.000000]   Normal   empty
[    0.000000] Movable zone start for each node
[    0.000000] Early memory node ranges
[    0.000000]   node   0: [mem 0x0000000040000000-0x000000005fffffff]
[    0.000000] Initmem setup node 0 [mem 0x0000000040000000-0x000000005fffffff]
[    0.000000] cma: Failed to reserve 512 MiB
[    0.000000] psci: probing for conduit method from DT.
[    0.000000] psci: PSCIv0.2 detected in firmware.
[    0.000000] psci: Using standard PSCI v0.2 function IDs
[    0.000000] psci: Trusted OS migration not required
[    0.000000] percpu: Embedded 2 pages/cpu s47912 r8192 d74968 u131072
[    0.000000] Detected PIPT I-cache on CPU0
[    0.000000] CPU features: detected: Spectre-v2
[    0.000000] CPU features: detected: Spectre-v3a
[    0.000000] CPU features: detected: Spectre-v4
[    0.000000] CPU features: detected: Spectre-BHB
[    0.000000] CPU features: kernel page table isolation forced ON by KASLR
[    0.000000] CPU features: detected: Kernel page table isolation (KPTI)
[    0.000000] CPU features: detected: ARM erratum 834220
[    0.000000] CPU features: detected: ARM erratum 832075
[    0.000000] CPU features: detected: ARM errata 1165522, 1319367, or 1530923
[    0.000000] alternatives: applying boot alternatives
[    0.000000] Fallback order for Node 0: 0
[    0.000000] Built 1 zonelists, mobility grouping off.  Total pages: 8184
[    0.000000] Policy zone: DMA
[    0.000000] Kernel command line: rdinit=/linuxrc console=ttyAMA0
[    0.000000] Dentry cache hash table entries: 65536 (order: 3, 524288 bytes, linear)
[    0.000000] Inode-cache hash table entries: 32768 (order: 2, 262144 bytes, linear)
[    0.000000] mem auto-init: stack:off, heap alloc:off, heap free:off
[    0.000000] Memory: 479936K/524288K available (16192K kernel code, 3960K rwdata, 9408K rodata, 7872K init, 1019K bss, 44352K reserved, 0K cma-reserved)
[    0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
[    0.000000] rcu: Preemptible hierarchical RCU implementation.
[    0.000000] rcu:     RCU event tracing is enabled.
[    0.000000] rcu:     RCU restricting CPUs from NR_CPUS=256 to nr_cpu_ids=1.
[    0.000000]  Trampoline variant of Tasks RCU enabled.
[    0.000000]  Tracing variant of Tasks RCU enabled.
[    0.000000] rcu: RCU calculated value of scheduler-enlistment delay is 25 jiffies.
[    0.000000] rcu: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=1
[    0.000000] NR_IRQS: 64, nr_irqs: 64, preallocated irqs: 0
[    0.000000] Root IRQ handler: gic_handle_irq
[    0.000000] GICv2m: range[mem 0x08020000-0x08020fff], SPI[80:143]
[    0.000000] rcu: srcu_init: Setting srcu_struct sizes based on contention.
[    0.000000] arch_timer: cp15 timer(s) running at 62.50MHz (virt).
[    0.000000] clocksource: arch_sys_counter: mask: 0x1ffffffffffffff max_cycles: 0x1cd42e208c, max_idle_ns: 881590405314 ns
[    0.000079] sched_clock: 57 bits at 63MHz, resolution 16ns, wraps every 4398046511096ns
[    0.007539] Console: colour dummy device 80x25
[    0.009363] Calibrating delay loop (skipped), value calculated using timer frequency.. 125.00 BogoMIPS (lpj=250000)
[    0.009704] pid_max: default: 32768 minimum: 301
[    0.010287] LSM: Security Framework initializing
[    0.012473] Mount-cache hash table entries: 8192 (order: 0, 65536 bytes, linear)
[    0.012531] Mountpoint-cache hash table entries: 8192 (order: 0, 65536 bytes, linear)
[    0.036646] cacheinfo: Unable to detect cache hierarchy for CPU 0
[    0.042006] cblist_init_generic: Setting adjustable number of callback queues.
[    0.042032] cblist_init_generic: Setting shift to 0 and lim to 1.
[    0.042327] cblist_init_generic: Setting adjustable number of callback queues.
[    0.042338] cblist_init_generic: Setting shift to 0 and lim to 1.
[    0.043833] rcu: Hierarchical SRCU implementation.
[    0.043887] rcu:     Max phase no-delay instances is 1000.
[    0.047671] EFI services will not be available.
[    0.048180] smp: Bringing up secondary CPUs ...
[    0.048264] smp: Brought up 1 node, 1 CPU
[    0.048314] SMP: Total of 1 processors activated.
[    0.048390] CPU features: detected: 32-bit EL0 Support
[    0.048408] CPU features: detected: 32-bit EL1 Support
[    0.048466] CPU features: detected: CRC32 instructions
[    0.050039] CPU: All CPU(s) started at EL1
[    0.050193] alternatives: applying system-wide alternatives
[    0.067780] devtmpfs: initialized
[    0.079061] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 7645041785100000 ns
[    0.079226] futex hash table entries: 256 (order: -2, 16384 bytes, linear)
[    0.081525] pinctrl core: initialized pinctrl subsystem
[    0.090254] DMI not present or invalid.
[    0.098525] NET: Registered PF_NETLINK/PF_ROUTE protocol family
[    0.101673] DMA: preallocated 128 KiB GFP_KERNEL pool for atomic allocations
[    0.101922] DMA: preallocated 128 KiB GFP_KERNEL|GFP_DMA pool for atomic allocations
[    0.102011] DMA: preallocated 128 KiB GFP_KERNEL|GFP_DMA32 pool for atomic allocations
[    0.102230] audit: initializing netlink subsys (disabled)
[    0.107063] audit: type=2000 audit(0.092:1): state=initialized audit_enabled=0 res=1
[    0.108464] thermal_sys: Registered thermal governor 'step_wise'
[    0.108499] thermal_sys: Registered thermal governor 'power_allocator'
[    0.111663] cpuidle: using governor menu
[    0.112682] hw-breakpoint: found 6 breakpoint and 4 watchpoint registers.
[    0.114073] ASID allocator initialised with 32768 entries
[    0.118753] Serial: AMBA PL011 UART driver
[    0.147147] 9000000.pl011: ttyAMA0 at MMIO 0x9000000 (irq = 13, base_baud = 0) is a PL011 rev1
[    0.178724] printk: console [ttyAMA0] enabled
[    0.190016] KASLR enabled
[    0.213671] HugeTLB: registered 16.0 GiB page size, pre-allocated 0 pages
[    0.213853] HugeTLB: 0 KiB vmemmap can be freed for a 16.0 GiB page
[    0.213981] HugeTLB: registered 512 MiB page size, pre-allocated 0 pages
[    0.214193] HugeTLB: 0 KiB vmemmap can be freed for a 512 MiB page
[    0.214466] HugeTLB: registered 2.00 MiB page size, pre-allocated 0 pages
[    0.214710] HugeTLB: 0 KiB vmemmap can be freed for a 2.00 MiB page
[    0.224505] ACPI: Interpreter disabled.
[    0.229437] iommu: Default domain type: Translated
[    0.229614] iommu: DMA domain TLB invalidation policy: strict mode
[    0.230954] SCSI subsystem initialized
[    0.232987] usbcore: registered new interface driver usbfs
[    0.233308] usbcore: registered new interface driver hub
[    0.233754] usbcore: registered new device driver usb
[    0.235750] pps_core: LinuxPPS API ver. 1 registered
[    0.235919] pps_core: Software ver. 5.3.6 - Copyright 2005-2007 Rodolfo Giometti <giometti@linux.it>
[    0.236209] PTP clock support registered
[    0.236691] EDAC MC: Ver: 3.0.0
[    0.240708] FPGA manager framework
[    0.241450] Advanced Linux Sound Architecture Driver Initialized.
[    0.251202] vgaarb: loaded
[    0.255245] clocksource: Switched to clocksource arch_sys_counter
[    0.257636] VFS: Disk quotas dquot_6.6.0
[    0.257921] VFS: Dquot-cache hash table entries: 8192 (order 0, 65536 bytes)
[    0.260293] pnp: PnP ACPI: disabled
[    0.275089] NET: Registered PF_INET protocol family
[    0.276786] IP idents hash table entries: 8192 (order: 0, 65536 bytes, linear)
[    0.281116] tcp_listen_portaddr_hash hash table entries: 4096 (order: 0, 65536 bytes, linear)
[    0.281562] Table-perturb hash table entries: 65536 (order: 2, 262144 bytes, linear)
[    0.282098] TCP established hash table entries: 8192 (order: 0, 65536 bytes, linear)
[    0.282644] TCP bind hash table entries: 8192 (order: 2, 262144 bytes, linear)
[    0.283022] TCP: Hash tables configured (established 8192 bind 8192)
[    0.284761] UDP hash table entries: 2048 (order: 0, 65536 bytes, linear)
[    0.285161] UDP-Lite hash table entries: 2048 (order: 0, 65536 bytes, linear)
[    0.286446] NET: Registered PF_UNIX/PF_LOCAL protocol family
[    0.289611] RPC: Registered named UNIX socket transport module.
[    0.289807] RPC: Registered udp transport module.
[    0.289898] RPC: Registered tcp transport module.
[    0.290153] RPC: Registered tcp NFSv4.1 backchannel transport module.
[    0.290368] PCI: CLS 0 bytes, default 64
[    0.292839] Unpacking initramfs...
[    0.299744] hw perfevents: enabled with armv8_pmuv3 PMU driver, 5 counters available
[    0.303579] kvm [1]: HYP mode not available
[    0.307045] Initialise system trusted keyrings
[    0.316012] workingset: timestamp_bits=42 max_order=13 bucket_order=0
[    0.340779] squashfs: version 4.0 (2009/01/31) Phillip Lougher
[    0.348496] NFS: Registering the id_resolver key type
[    0.348825] Key type id_resolver registered
[    0.349058] Key type id_legacy registered
[    0.349481] nfs4filelayout_init: NFSv4 File Layout Driver Registering...
[    0.349809] nfs4flexfilelayout_init: NFSv4 Flexfile Layout Driver Registering...
[    0.351354] 9p: Installing v9fs 9p2000 file system support
[    0.394345] Key type asymmetric registered
[    0.394538] Asymmetric key parser 'x509' registered
[    0.399371] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 245)
[    0.399659] io scheduler mq-deadline registered
[    0.399807] io scheduler kyber registered
[    0.475660] pl061_gpio 9030000.pl061: PL061 GPIO chip registered
[    0.489699] pci-host-generic 4010000000.pcie: host bridge /pcie@10000000 ranges:
[    0.490657] pci-host-generic 4010000000.pcie:       IO 0x003eff0000..0x003effffff -> 0x0000000000
[    0.495796] pci-host-generic 4010000000.pcie:      MEM 0x0010000000..0x003efeffff -> 0x0010000000
[    0.496074] pci-host-generic 4010000000.pcie:      MEM 0x8000000000..0xffffffffff -> 0x8000000000
[    0.496647] pci-host-generic 4010000000.pcie: Memory resource size exceeds max for 32 bits
[    0.497304] pci-host-generic 4010000000.pcie: ECAM at [mem 0x4010000000-0x401fffffff] for [bus 00-ff]
[    0.503288] pci-host-generic 4010000000.pcie: PCI host bridge to bus 0000:00
[    0.503678] pci_bus 0000:00: root bus resource [bus 00-ff]
[    0.503911] pci_bus 0000:00: root bus resource [io  0x0000-0xffff]
[    0.504103] pci_bus 0000:00: root bus resource [mem 0x10000000-0x3efeffff]
[    0.504537] pci_bus 0000:00: root bus resource [mem 0x8000000000-0xffffffffff]
[    0.506688] pci 0000:00:00.0: [1b36:0008] type 00 class 0x060000
[    0.515738] pci 0000:00:01.0: [1af4:1000] type 00 class 0x020000
[    0.516101] pci 0000:00:01.0: reg 0x10: [io  0x0000-0x001f]
[    0.516276] pci 0000:00:01.0: reg 0x14: [mem 0x00000000-0x00000fff]
[    0.516451] pci 0000:00:01.0: reg 0x20: [mem 0x00000000-0x00003fff 64bit pref]
[    0.516688] pci 0000:00:01.0: reg 0x30: [mem 0x00000000-0x0007ffff pref]
[    0.524311] pci 0000:00:01.0: BAR 6: assigned [mem 0x10000000-0x1007ffff pref]
[    0.524756] pci 0000:00:01.0: BAR 4: assigned [mem 0x8000000000-0x8000003fff 64bit pref]
[    0.525000] pci 0000:00:01.0: BAR 1: assigned [mem 0x10080000-0x10080fff]
[    0.525395] pci 0000:00:01.0: BAR 0: assigned [io  0x1000-0x101f]
[    0.538113] EINJ: ACPI disabled.
[    0.565557] Freeing initrd memory: 2240K
[    0.785302] virtio-pci 0000:00:01.0: enabling device (0000 -> 0003)
[    0.836159] Serial: 8250/16550 driver, 4 ports, IRQ sharing enabled
[    0.866426] SuperH (H)SCI(F) driver initialized
[    0.873534] msm_serial: driver initialized
[    0.886722] cacheinfo: Unable to detect cache hierarchy for CPU 0
[    0.915540] loop: module loaded
[    0.926783] megasas: 07.719.03.00-rc1
[    0.949656] physmap-flash 0.flash: physmap platform flash device: [mem 0x00000000-0x03ffffff]
[    0.952130] 0.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
[    0.953084] Intel/Sharp Extended Query Table at 0x0031
[    0.953822] Using buffer write method
[    0.954518] physmap-flash 0.flash: physmap platform flash device: [mem 0x04000000-0x07ffffff]
[    0.955369] 0.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
[    0.956491] Intel/Sharp Extended Query Table at 0x0031
[    0.957072] Using buffer write method
[    0.957311] Concatenating MTD devices:
[    0.957546] (0): "0.flash"
[    0.957799] (1): "0.flash"
[    0.957912] into device "0.flash"
[    1.065794] tun: Universal TUN/TAP device driver, 1.6
[    1.099000] thunder_xcv, ver 1.0
[    1.102055] thunder_bgx, ver 1.0
[    1.103068] nicpf, ver 1.0
[    1.117869] hns3: Hisilicon Ethernet Network Driver for Hip08 Family - version
[    1.118096] hns3: Copyright (c) 2017 Huawei Corporation.
[    1.118941] hclge is initializing
[    1.119137] e1000: Intel(R) PRO/1000 Network Driver
[    1.120662] e1000: Copyright (c) 1999-2006 Intel Corporation.
[    1.122611] e1000e: Intel(R) PRO/1000 Network Driver
[    1.122769] e1000e: Copyright(c) 1999 - 2015 Intel Corporation.
[    1.124614] igb: Intel(R) Gigabit Ethernet Network Driver
[    1.124773] igb: Copyright (c) 2007-2014 Intel Corporation.
[    1.125350] igbvf: Intel(R) Gigabit Virtual Function Network Driver
[    1.125526] igbvf: Copyright (c) 2009 - 2012 Intel Corporation.
[    1.128686] sky2: driver version 1.30
[    1.141292] VFIO - User Level meta-driver version: 0.3
[    1.169220] usbcore: registered new interface driver usb-storage
[    1.200355] rtc-pl031 9010000.pl031: registered as rtc0
[    1.201121] rtc-pl031 9010000.pl031: setting system clock to 2024-06-06T08:47:09 UTC (1717663629)
[    1.207118] i2c_dev: i2c /dev entries driver
[    1.265192] sdhci: Secure Digital Host Controller Interface driver
[    1.265375] sdhci: Copyright(c) Pierre Ossman
[    1.268131] Synopsys Designware Multimedia Card Interface Driver
[    1.278102] sdhci-pltfm: SDHCI platform and OF driver helper
[    1.290931] ledtrig-cpu: registered to indicate activity on CPUs
[    1.315833] usbcore: registered new interface driver usbhid
[    1.316038] usbhid: USB HID core driver
[    1.372808] NET: Registered PF_PACKET protocol family
[    1.373807] 9pnet: Installing 9P2000 support
[    1.377236] Key type dns_resolver registered
[    1.378811] registered taskstats version 1
[    1.379540] Loading compiled-in X.509 certificates
[    1.400495] input: gpio-keys as /devices/platform/gpio-keys/input/input0
[    1.410521] clk: Disabling unused clocks
[    1.410964] ALSA device list:
[    1.411805]   No soundcards found.
[    1.412158] Warning: unable to open an initial console.
[    1.450079] Freeing unused kernel memory: 7872K
[    1.450876] Run /linuxrc as init process