环境 win11 WSL2:
busybox编译 1 2 3 4 5 wget https://www.busybox.net/downloads/busybox-1.37.0.tar.bz2 bzip2 -d busybox-1.37.0.tar.bz2 tar xvf busybox-1.37.0.tar cd busybox-1.37.0
Patch 这个版本直接编译aarch64有点问题,需要打一个补丁:
编译 1 2 3 4 5 6 7 8 make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- clean make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- defconfig make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j$(nproc ) make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- install
创建inittab,rcS和fstab文件 1 2 3 4 5 6 7 8 9 10 11 12 13 cd _installmkdir -p dev etc home lib mnt proc root sys tmp vartouch etc/inittabmkdir -p etc/init.d/touch etc/init.d/rcStouch etc/fstabchmod 755 etc/inittabchmod 755 etc/init.d/rcS
inittab语法:1 <id>:<runlevels> :<action> :<process>
id : /dev/id
runlevels : 忽略
action : 何时执行,有以下选项 sysinit, respawn, askfirst, wait, once,restart, ctrlaltdel, and shutdown
process : 应用程序或脚本
etc/inittab 写入以下内容1 2 3 4 ::sysinit:/etc/init.d/rcS ::respawn:-/bin/sh ::askfirst:-/bin/sh ::ctrlaltdel:/bin/umount -a -r
etc/init.d/rcS写入如下内容:1 2 3 4 5 6 7 /bin/mount -a mkdir -p /dev/ptsmount -t devpts devpts /dev/pts if [ -e /proc/sys/kernel/hotplug ]; then echo /sbin/mdev > /proc/sys/kernel/hotplug fi mdev -s
etc/fstab写入如下内容:1 2 3 4 5 6 proc /proc proc defaults 0 0 tmpfs /tmp tmpfs defaults 0 0 none /tmp ramfs defaults 0 0 sysfs /sys sysfs defaults 0 0 mdev /dev ramfs defaults 0 0
添加设备文件 1 2 3 cd devsudo mknod console c 5 1sudo mknod null c 1 3
打包 1 2 3 cd busybox-1.37.0/_installfind . | cpio -o -H newc | gzip -c > ../initramfs.cpio.gz cp ../initramfs.cpio.gz ~/tftp
Linux 内核编译 1 2 3 4 5 6 7 8 9 10 11 12 13 14 sudo apt-get install gcc-aarch64-linux-gnusudo apt install libssl-devwget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.10.238.tar.xz tar xvf linux-5.10.238.tar.xz cd linux-5.10.238make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- clean make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- defconfig make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- Image -j$(nproc ) cp arch /arm64/boot/Image ~/tftp
qemu运行 1 2 3 4 5 6 7 8 9 10 qemu-system-aarch64 \ -cpu cortex-a57 \ -m 512M \ -machine type =virt \ -nographic \ -smp 2 \ -kernel ~/tftp/Image \ -initrd ~/tftp/initramfs.cpio.gz \ -append "rdinit=/linuxrc console=ttyAMA0" \ -device virtio-scsi-device
结果:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 zhaohang@ZhaoHang:~/kernel/my_arm64_programming$ qemu-system-aarch64 \ -cpu cortex-a57 \ -m 512M \ -machine type =virt \ -nographic \ -smp 2 \ -kernel ~/tftp/Image \ -initrd ~/tftp/initramfs.cpio.gz \ -append "rdinit=/linuxrc console=ttyAMA0" \ -device virtio-scsi-device [ 0.000000] Booting Linux on physical CPU 0x0000000000 [0x411fd070] [ 0.000000] Linux version 5.10.238 (zhaohang@ZhaoHang) (aarch64-linux-gnu-gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, GNU ld (GNU Binutils for Ubuntu) 2.38) [ 0.000000] random: crng init done [ 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 0x5fef4b00-0x5fef6fff] [ 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: Reserved 32 MiB at 0x000000005c000000 [ 0.000000] psci: probing for conduit method from DT. [ 0.000000] psci: PSCIv1.1 detected in firmware. [ 0.000000] psci: Using standard PSCI v0.2 function IDs [ 0.000000] psci: Trusted OS migration not required [ 0.000000] psci: SMC Calling Convention v1.0 [ 0.000000] percpu: Embedded 23 pages/cpu s56664 r8192 d29352 u94208 [ 0.000000] Detected PIPT I-cache on CPU0 [ 0.000000] CPU features: detected: ARM erratum 832075 [ 0.000000] CPU features: detected: ARM erratum 834220 [ 0.000000] CPU features: detected: EL2 vector hardening [ 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: Spectre-v2 [ 0.000000] CPU features: detected: Spectre-v4 [ 0.000000] CPU features: detected: ARM errata 1165522, 1319367, or 1530923 [ 0.000000] CPU features: detected: Spectre-BHB [ 0.000000] CPU features: detected: ARM erratum 1742098 [ 0.000000] Built 1 zonelists, mobility grouping on. Total pages: 129024 [ 0.000000] Policy zone: DMA [ 0.000000] Kernel command line: rdinit=/linuxrc console=ttyAMA0 [ 0.000000] Dentry cache hash table entries: 65536 (order: 7, 524288 bytes, linear) [ 0.000000] Inode-cache hash table entries: 32768 (order: 6, 262144 bytes, linear) [ 0.000000] mem auto-init: stack:off, heap alloc:off, heap free:off [ 0.000000] Memory: 439372K/524288K available (14464K kernel code, 2810K rwdata, 7632K rodata, 5952K init, 512K bss, 52148K reserved, 32768K cma-reserved) [ 0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=2, 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=2. [ 0.000000] Trampoline 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=2 [ 0.000000] NR_IRQS: 64, nr_irqs: 64, preallocated irqs: 0 [ 0.000000] GICv2m: range[mem 0x08020000-0x08020fff], SPI[80:143] [ 0.000000] arch_timer: cp15 timer(s) running at 62.50MHz (virt). [ 0.000000] clocksource: arch_sys_counter: mask: 0xffffffffffffff max_cycles: 0x1cd42e208c, max_idle_ns: 881590405314 ns [ 0.000135] sched_clock: 56 bits at 62MHz, resolution 16ns, wraps every 4398046511096ns [ 0.005145] Console: colour dummy device 80x25 [ 0.006576] Calibrating delay loop (skipped), value calculated using timer frequency.. 125.00 BogoMIPS (lpj=250000) [ 0.006668] pid_max: default: 32768 minimum: 301 [ 0.007333] LSM: Security Framework initializing [ 0.008050] Mount-cache hash table entries: 1024 (order: 1, 8192 bytes, linear) [ 0.008075] Mountpoint-cache hash table entries: 1024 (order: 1, 8192 bytes, linear) [ 0.025711] /cpus/cpu-map: empty cluster [ 0.030337] rcu: Hierarchical SRCU implementation. [ 0.032757] EFI services will not be available. [ 0.033350] smp: Bringing up secondary CPUs ... [ 0.036124] Detected PIPT I-cache on CPU1 [ 0.036589] CPU1: Booted secondary processor 0x0000000001 [0x411fd070] [ 0.038611] smp: Brought up 1 node, 2 CPUs [ 0.038641] SMP: Total of 2 processors activated. [ 0.038684] CPU features: detected: 32-bit EL0 Support [ 0.038732] CPU features: detected: CRC32 instructions [ 0.038761] CPU features: detected: 32-bit EL1 Support [ 0.087011] CPU: All CPU(s) started at EL1 [ 0.087320] alternatives: patching kernel code [ 0.100786] devtmpfs: initialized [ 0.106839] KASLR enabled [ 0.107566] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 7645041785100000 ns [ 0.107783] futex hash table entries: 512 (order: 3, 32768 bytes, linear) [ 0.110482] pinctrl core: initialized pinctrl subsystem [ 0.117149] DMI not present or invalid. [ 0.123022] NET: Registered protocol family 16 [ 0.130687] DMA: preallocated 128 KiB GFP_KERNEL pool for atomic allocations [ 0.130888] DMA: preallocated 128 KiB GFP_KERNEL|GFP_DMA pool for atomic allocations [ 0.131032] DMA: preallocated 128 KiB GFP_KERNEL|GFP_DMA32 pool for atomic allocations [ 0.131170] audit: initializing netlink subsys (disabled) [ 0.132726] audit: type =2000 audit(0.124:1): state=initialized audit_enabled=0 res=1 [ 0.135229] thermal_sys: Registered thermal governor 'step_wise' [ 0.135284] thermal_sys: Registered thermal governor 'power_allocator' [ 0.135718] cpuidle: using governor menu [ 0.136329] hw-breakpoint: found 6 breakpoint and 4 watchpoint registers. [ 0.136815] ASID allocator initialised with 32768 entries [ 0.139125] Serial: AMBA PL011 UART driver [ 0.165966] 9000000.pl011: ttyAMA0 at MMIO 0x9000000 (irq = 47, base_baud = 0) is a PL011 rev1 [ 0.256055] printk: console [ttyAMA0] enabled [ 0.275753] HugeTLB registered 1.00 GiB page size, pre-allocated 0 pages [ 0.276425] HugeTLB registered 32.0 MiB page size, pre-allocated 0 pages [ 0.278075] HugeTLB registered 2.00 MiB page size, pre-allocated 0 pages [ 0.279114] HugeTLB registered 64.0 KiB page size, pre-allocated 0 pages [ 0.287377] cryptd: max_cpu_qlen set to 1000 [ 0.295509] ACPI: Interpreter disabled. [ 0.298412] iommu: Default domain type : Translated [ 0.299715] vgaarb: loaded [ 0.300806] SCSI subsystem initialized [ 0.302702] usbcore: registered new interface driver usbfs [ 0.303416] usbcore: registered new interface driver hub [ 0.303948] usbcore: registered new device driver usb [ 0.305361] pps_core: LinuxPPS API ver. 1 registered [ 0.305774] pps_core: Software ver. 5.3.6 - Copyright 2005-2007 Rodolfo Giometti <giometti@linux.it> [ 0.306671] PTP clock support registered [ 0.307422] EDAC MC: Ver: 3.0.0 [ 0.311302] FPGA manager framework [ 0.312553] Advanced Linux Sound Architecture Driver Initialized. [ 0.320481] clocksource: Switched to clocksource arch_sys_counter [ 0.321721] VFS: Disk quotas dquot_6.6.0 [ 0.322248] VFS: Dquot-cache hash table entries: 512 (order 0, 4096 bytes) [ 0.325830] pnp: PnP ACPI: disabled [ 0.352101] NET: Registered protocol family 2 [ 0.353501] IP idents hash table entries: 8192 (order: 4, 65536 bytes, linear) [ 0.357091] tcp_listen_portaddr_hash hash table entries: 256 (order: 0, 4096 bytes, linear) [ 0.357844] TCP established hash table entries: 4096 (order: 3, 32768 bytes, linear) [ 0.358536] TCP bind hash table entries: 4096 (order: 4, 65536 bytes, linear) [ 0.359240] TCP: Hash tables configured (established 4096 bind 4096) [ 0.360804] UDP hash table entries: 256 (order: 1, 8192 bytes, linear) [ 0.361461] UDP-Lite hash table entries: 256 (order: 1, 8192 bytes, linear) [ 0.362658] NET: Registered protocol family 1 [ 0.366482] RPC: Registered named UNIX socket transport module. [ 0.366894] RPC: Registered udp transport module. [ 0.367151] RPC: Registered tcp transport module. [ 0.367596] RPC: Registered tcp NFSv4.1 backchannel transport module. [ 0.368532] PCI: CLS 0 bytes, default 64 [ 0.371336] Unpacking initramfs... [ 0.423234] Freeing initrd memory: 1156K [ 0.425734] hw perfevents: enabled with armv8_pmuv3 PMU driver, 7 counters available [ 0.426386] kvm [1]: HYP mode not available [ 0.438866] Initialise system trusted keyrings [ 0.441323] workingset: timestamp_bits=42 max_order=17 bucket_order=0 [ 0.449648] squashfs: version 4.0 (2009/01/31) Phillip Lougher [ 0.451781] NFS: Registering the id_resolver key type [ 0.452635] Key type id_resolver registered [ 0.452872] Key type id_legacy registered [ 0.453347] nfs4filelayout_init: NFSv4 File Layout Driver Registering... [ 0.453801] nfs4flexfilelayout_init: NFSv4 Flexfile Layout Driver Registering... [ 0.454953] 9p: Installing v9fs 9p2000 file system support [ 0.485329] Key type asymmetric registered [ 0.486010] Asymmetric key parser 'x509' registered [ 0.486459] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 245) [ 0.486881] io scheduler mq-deadline registered [ 0.487275] io scheduler kyber registered [ 0.498447] pl061_gpio 9030000.pl061: PL061 GPIO chip registered [ 0.501854] pci-host-generic 4010000000.pcie: host bridge /pcie@10000000 ranges: [ 0.502917] pci-host-generic 4010000000.pcie: IO 0x003eff0000..0x003effffff -> 0x0000000000 [ 0.503846] pci-host-generic 4010000000.pcie: MEM 0x0010000000..0x003efeffff -> 0x0010000000 [ 0.504662] pci-host-generic 4010000000.pcie: MEM 0x8000000000..0xffffffffff -> 0x8000000000 [ 0.505854] pci-host-generic 4010000000.pcie: ECAM at [mem 0x4010000000-0x401fffffff] for [bus 00-ff] [ 0.507028] pci-host-generic 4010000000.pcie: PCI host bridge to bus 0000:00 [ 0.507648] pci_bus 0000:00: root bus resource [bus 00-ff] [ 0.508176] pci_bus 0000:00: root bus resource [io 0x0000-0xffff] [ 0.509569] pci_bus 0000:00: root bus resource [mem 0x10000000-0x3efeffff] [ 0.511529] pci_bus 0000:00: root bus resource [mem 0x8000000000-0xffffffffff] [ 0.513307] pci 0000:00:00.0: [1b36:0008] type 00 class 0x060000 [ 0.515919] pci 0000:00:01.0: [1af4:1000] type 00 class 0x020000 [ 0.517340] pci 0000:00:01.0: reg 0x10: [io 0x0000-0x001f] [ 0.517754] pci 0000:00:01.0: reg 0x14: [mem 0x00000000-0x00000fff] [ 0.518388] pci 0000:00:01.0: reg 0x20: [mem 0x00000000-0x00003fff 64bit pref] [ 0.518923] pci 0000:00:01.0: reg 0x30: [mem 0x00000000-0x0003ffff pref] [ 0.521212] pci 0000:00:01.0: BAR 6: assigned [mem 0x10000000-0x1003ffff pref] [ 0.522137] pci 0000:00:01.0: BAR 4: assigned [mem 0x8000000000-0x8000003fff 64bit pref] [ 0.523076] pci 0000:00:01.0: BAR 1: assigned [mem 0x10040000-0x10040fff] [ 0.523801] pci 0000:00:01.0: BAR 0: assigned [io 0x1000-0x101f] [ 0.528448] EINJ: ACPI disabled. [ 0.542283] virtio-pci 0000:00:01.0: enabling device (0000 -> 0003) [ 0.554485] Serial: 8250/16550 driver, 4 ports, IRQ sharing enabled [ 0.558302] SuperH (H)SCI(F) driver initialized [ 0.559486] msm_serial: driver initialized [ 0.561993] cacheinfo: Unable to detect cache hierarchy for CPU 0 [ 0.575536] loop: module loaded [ 0.577188] megasas: 07.714.04.00-rc1 [ 0.581247] physmap-flash 0.flash: physmap platform flash device: [mem 0x00000000-0x03ffffff] [ 0.583156] 0.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000 [ 0.584568] Intel/Sharp Extended Query Table at 0x0031 [ 0.585746] Using buffer write method [ 0.586466] physmap-flash 0.flash: physmap platform flash device: [mem 0x04000000-0x07ffffff] [ 0.587833] 0.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000 [ 0.589950] Intel/Sharp Extended Query Table at 0x0031 [ 0.590679] Using buffer write method [ 0.591154] Concatenating MTD devices: [ 0.591566] (0): "0.flash" [ 0.591870] (1): "0.flash" [ 0.592172] into device "0.flash" [ 0.639324] tun: Universal TUN/TAP device driver, 1.6 [ 0.648697] thunder_xcv, ver 1.0 [ 0.649091] thunder_bgx, ver 1.0 [ 0.649532] nicpf, ver 1.0 [ 0.651118] hclge is initializing [ 0.651699] hns3: Hisilicon Ethernet Network Driver for Hip08 Family - version [ 0.652969] hns3: Copyright (c) 2017 Huawei Corporation. [ 0.654822] e1000: Intel(R) PRO/1000 Network Driver [ 0.655566] e1000: Copyright (c) 1999-2006 Intel Corporation. [ 0.656421] e1000e: Intel(R) PRO/1000 Network Driver [ 0.657100] e1000e: Copyright(c) 1999 - 2015 Intel Corporation. [ 0.657828] igb: Intel(R) Gigabit Ethernet Network Driver [ 0.658543] igb: Copyright (c) 2007-2014 Intel Corporation. [ 0.659076] igbvf: Intel(R) Gigabit Virtual Function Network Driver [ 0.659905] igbvf: Copyright (c) 2009 - 2012 Intel Corporation. [ 0.661636] sky2: driver version 1.30 [ 0.663482] VFIO - User Level meta-driver version: 0.3 [ 0.666363] ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver [ 0.666801] ehci-pci: EHCI PCI platform driver [ 0.667200] ehci-platform: EHCI generic platform driver [ 0.667555] ehci-orion: EHCI orion driver [ 0.668162] ehci-exynos: EHCI Exynos driver [ 0.669422] ohci_hcd: USB 1.1 'Open' Host Controller (OHCI) Driver [ 0.670008] ohci-pci: OHCI PCI platform driver [ 0.670583] ohci-platform: OHCI generic platform driver [ 0.671027] ohci-exynos: OHCI Exynos driver [ 0.672066] usbcore: registered new interface driver usb-storage [ 0.678548] rtc-pl031 9010000.pl031: registered as rtc0 [ 0.679438] rtc-pl031 9010000.pl031: setting system clock to 2025-06-23T05:12:11 UTC (1750655531) [ 0.680812] i2c /dev entries driver [ 0.687138] sdhci: Secure Digital Host Controller Interface driver [ 0.687486] sdhci: Copyright(c) Pierre Ossman [ 0.688175] Synopsys Designware Multimedia Card Interface Driver [ 0.689770] sdhci-pltfm: SDHCI platform and OF driver helper [ 0.691932] ledtrig-cpu: registered to indicate activity on CPUs [ 0.694429] usbcore: registered new interface driver usbhid [ 0.694654] usbhid: USB HID core driver [ 0.700987] NET: Registered protocol family 17 [ 0.702090] 9pnet: Installing 9P2000 support [ 0.702525] Key type dns_resolver registered [ 0.703492] registered taskstats version 1 [ 0.703776] Loading compiled-in X.509 certificates [ 0.710892] input: gpio-keys as /devices/platform/gpio-keys/input/input0 [ 0.714806] clk: Disabling unused clocks [ 0.715339] ALSA device list: [ 0.715654] No soundcards found. [ 0.718941] uart-pl011 9000000.pl011: no DMA platform data [ 0.752373] Freeing unused kernel memory: 5952K [ 0.753454] Run /linuxrc as init process Please press Enter to activate this console. ~ ~ bin etc lib mnt root sys usr dev home linuxrc proc sbin tmp var
ctrl-a x退出qemu