简介

从编译内核,制作initramfs到使用qemu运行内核

参考

https://www.bilibili.com/video/BV1Kd4y1R7tV/?spm_id_from=333.880.my_history.page.click&vd_source=75dbec7ad4709dbb9145a059a5374980

制作bzImage

我使用默认的配置,出现了下面的警告

1
2
3
4
5
6
7
8
9
10
11
12
13
#
# using defaults found in /boot/config-6.0.9-200.fc36.x86_64
#
.config:4591:warning: symbol value 'm' invalid for GPIO_TPS68470
.config:5606:warning: symbol value 'm' invalid for RADIO_ADAPTERS
.config:6128:warning: symbol value 'm' invalid for DRM_GEM_CMA_HELPER
.config:6732:warning: symbol value 'm' invalid for SND_SOC_SOF_DEBUG_PROBES
.config:6769:warning: symbol value 'm' invalid for SND_SOC_SOF_HDA_PROBES
.config:10428:warning: symbol value 'm' invalid for KPROBES_SANITY_TEST
configuration written to .config

*** End of the configuration.
*** Execute 'make' to start the build or try 'make help'.

暂时不明原因,忽视这个警告

最终得到了内核镜像bzImage

1
2
3
4
[liode@liodePC:68:boot]$ pwd
/home/liode/nfs_dir/kernel/source/linux-5.15.79/arch/x86/boot
[liode@liodePC:69:boot]$ file bzImage
bzImage: Linux kernel x86 boot executable bzImage, version 5.15.79 (liode@liodePC) #1 SMP Sun Nov 27 19:17:16 CST 2022, RO-rootFS, swap_dev 0XB, Normal VGA

比较一下我正在用的image

1
2
3
4
[liode@liodePC:68:boot]$ pwd
/home/liode/nfs_dir/kernel/source/linux-5.15.79/arch/x86/boot
[liode@liodePC:69:boot]$ file bzImage
bzImage: Linux kernel x86 boot executable bzImage, version 5.15.79 (liode@liodePC) #1 SMP Sun Nov 27 19:17:16 CST 2022, RO-rootFS, swap_dev 0XB, Normal VGA

创建一个叫linux-demo的文件夹,将bzImage拷贝进去

制作busybox

编译并安装busybox,配置的时候勾选“build static binary”选项

安装成功后,将产生的busybox可执行文件复制到linux-demo中

1
2
3
4
[liode@liodePC:82:linux-demo]$ ll
total 14164
-rwxr-xr-x. 1 liode liode 2425088 Nov 27 19:53 busybox
-rw-r--r--. 1 liode liode 12072096 Nov 27 19:48 bzImage

制作initramfs

在linux-demo中创建initramfs_d目录,里面创建bin目录,将busybox可执行文件移动进去

在initramfs目录创建init文件,内容如下,并添加可执行权限

1
2
3
4
[liode@liodePC:9:linux-demo]$ cat initramfs_d/init
#!/bin/busybox sh
/bin/busybox echo "HelloToYou"
/bin/busybox sh

在linux-demo中创建makefile,内容如下

1
2
3
4
5
6
7
8
9
.PHONY: initramfs
initramfs:
cd initramfs_d && find . -print0|cpio -ov --null --format=newc|gzip -9 >../initramfs.img
.PHONY: run
run:
qemu-system-x86_64 -kernel bzImage -initrd initramfs.img -m 1G -nographic -append "earlyprintk=serial,ttyS0 console=ttyS0"
.PHONY: clean
clean:
rm initramfs.img

解释

这样,执行make initramfs就会将前面的initramfs目录打包成initramfs.img文件了,也就制作好了initramfs

执行如下命令打包

make initramfs

之后的文件结构如下

1
2
3
4
5
6
7
8
9
10
11
[liode@liodePC:36:linux-demo]$ tree
.
├── bzImage
├── initramfs_d
│ ├── bin
│ │ └── busybox
│ └── init
├── initramfs.img
└── Makefile

2 directories, 5 files

使用

首先需要安装qemu,不再赘述,安装之后,就可以使用了

执行make run

输出如下,回车就会进入shell,执行shell 命令的时候,前面要加上busybox,退出是ctrl+a ,松开再按一下x

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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
[    0.000000] Linux version 5.15.79 (liode@liodePC) (gcc (GCC) 12.2.1 20221121 
[ 0.000000] Command line: earlyprintk=serial,ttyS0 console=ttyS0
[ 0.000000] x86/fpu: x87 FPU will use FXSAVE
[ 0.000000] signal: max sigframe size: 1440
[ 0.000000] BIOS-provided physical RAM map:
[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
[ 0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved
[ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000003ffdffff] usable
[ 0.000000] BIOS-e820: [mem 0x000000003ffe0000-0x000000003fffffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved
[ 0.000000] printk: bootconsole [earlyser0] enabled
[ 0.000000] NX (Execute Disable) protection: active
[ 0.000000] SMBIOS 2.8 present.
[ 0.000000] DMI: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.0-2.fc37 0
[ 0.000000] last_pfn = 0x3ffe0 max_arch_pfn = 0x400000000
[ 0.000000] x86/PAT: Configuration [0-7]: WB WC UC- UC WB WP UC- WT
Memory KASLR using RDTSC...
[ 0.000000] found SMP MP-table at [mem 0x000f5bc0-0x000f5bcf]
[ 0.000000] RAMDISK: [mem 0x3feb3000-0x3ffdffff]
[ 0.000000] ACPI: Early table checksum verification disabled
[ 0.000000] ACPI: RSDP 0x00000000000F5A00 000014 (v00 BOCHS )
[ 0.000000] ACPI: RSDT 0x000000003FFE1905 000034 (v01 BOCHS BXPC 0000000
[ 0.000000] ACPI: FACP 0x000000003FFE17B9 000074 (v01 BOCHS BXPC 0000000
[ 0.000000] ACPI: DSDT 0x000000003FFE0040 001779 (v01 BOCHS BXPC 0000000
[ 0.000000] ACPI: FACS 0x000000003FFE0000 000040
[ 0.000000] ACPI: APIC 0x000000003FFE182D 000078 (v01 BOCHS BXPC 0000000
[ 0.000000] ACPI: HPET 0x000000003FFE18A5 000038 (v01 BOCHS BXPC 0000000
[ 0.000000] ACPI: WAET 0x000000003FFE18DD 000028 (v01 BOCHS BXPC 0000000
[ 0.000000] ACPI: Reserving FACP table memory at [mem 0x3ffe17b9-0x3ffe182c]
[ 0.000000] ACPI: Reserving DSDT table memory at [mem 0x3ffe0040-0x3ffe17b8]
[ 0.000000] ACPI: Reserving FACS table memory at [mem 0x3ffe0000-0x3ffe003f]
[ 0.000000] ACPI: Reserving APIC table memory at [mem 0x3ffe182d-0x3ffe18a4]
[ 0.000000] ACPI: Reserving HPET table memory at [mem 0x3ffe18a5-0x3ffe18dc]
[ 0.000000] ACPI: Reserving WAET table memory at [mem 0x3ffe18dd-0x3ffe1904]
[ 0.000000] No NUMA configuration found
[ 0.000000] Faking a node at [mem 0x0000000000000000-0x000000003ffdffff]
[ 0.000000] NODE_DATA(0) allocated [mem 0x3fe88000-0x3feb2fff]
[ 0.000000] Zone ranges:
[ 0.000000] DMA [mem 0x0000000000001000-0x0000000000ffffff]
[ 0.000000] DMA32 [mem 0x0000000001000000-0x000000003ffdffff]
[ 0.000000] Normal empty
[ 0.000000] Device empty
[ 0.000000] Movable zone start for each node
[ 0.000000] Early memory node ranges
[ 0.000000] node 0: [mem 0x0000000000001000-0x000000000009efff]
[ 0.000000] node 0: [mem 0x0000000000100000-0x000000003ffdffff]
[ 0.000000] Initmem setup node 0 [mem 0x0000000000001000-0x000000003ffdffff]
[ 0.000000] On node 0, zone DMA: 1 pages in unavailable ranges
[ 0.000000] On node 0, zone DMA: 97 pages in unavailable ranges
[ 0.000000] On node 0, zone DMA32: 32 pages in unavailable ranges
[ 0.000000] ACPI: PM-Timer IO Port: 0x608
[ 0.000000] ACPI: LAPIC_NMI (acpi_id[0xff] dfl dfl lint[0x1])
[ 0.000000] IOAPIC[0]: apic_id 0, version 32, address 0xfec00000, GSI 0-23
[ 0.000000] ACPI: INT_SRC_OVR (bus 0 bus_irq 0 global_irq 2 dfl dfl)
[ 0.000000] ACPI: INT_SRC_OVR (bus 0 bus_irq 5 global_irq 5 high level)
[ 0.000000] ACPI: INT_SRC_OVR (bus 0 bus_irq 9 global_irq 9 high level)
[ 0.000000] ACPI: INT_SRC_OVR (bus 0 bus_irq 10 global_irq 10 high level)
[ 0.000000] ACPI: INT_SRC_OVR (bus 0 bus_irq 11 global_irq 11 high level)
[ 0.000000] ACPI: Using ACPI (MADT) for SMP configuration information
[ 0.000000] ACPI: HPET id: 0x8086a201 base: 0xfed00000
[ 0.000000] smpboot: Allowing 1 CPUs, 0 hotplug CPUs
[ 0.000000] PM: hibernation: Registered nosave memory: [mem 0x00000000-0x0000
[ 0.000000] PM: hibernation: Registered nosave memory: [mem 0x0009f000-0x0009
[ 0.000000] PM: hibernation: Registered nosave memory: [mem 0x000a0000-0x000e
[ 0.000000] PM: hibernation: Registered nosave memory: [mem 0x000f0000-0x000f
[ 0.000000] [mem 0x40000000-0xfffbffff] available for PCI devices
[ 0.000000] Booting paravirtualized kernel on bare hardware
[ 0.000000] clocksource: refined-jiffies: mask: 0xffffffff max_cycles: 0xffff
[ 0.000000] setup_percpu: NR_CPUS:8192 nr_cpumask_bits:1 nr_cpu_ids:1 nr_node
[ 0.000000] percpu: Embedded 61 pages/cpu s212992 r8192 d28672 u2097152
[ 0.000000] Built 1 zonelists, mobility grouping on. Total pages: 257760
[ 0.000000] Policy zone: DMA32
[ 0.000000] Kernel command line: earlyprintk=serial,ttyS0 console=ttyS0
[ 0.000000] Dentry cache hash table entries: 131072 (order: 8, 1048576 bytes,
[ 0.000000] Inode-cache hash table entries: 65536 (order: 7, 524288 bytes, li
[ 0.000000] mem auto-init: stack:all(zero), heap alloc:off, heap free:off
[ 0.000000] Memory: 968096K/1048056K available (16393K kernel code, 3601K rwd
[ 0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
[ 0.000000] ftrace: allocating 48746 entries in 191 pages
[ 0.000000] ftrace: allocated 191 pages with 7 groups
[ 0.000000] rcu: Hierarchical RCU implementation.
[ 0.000000] rcu: RCU restricting CPUs from NR_CPUS=8192 to nr_cpu_ids=1.
[ 0.000000] Trampoline variant of Tasks RCU enabled.
[ 0.000000] Rude variant of Tasks RCU enabled.
[ 0.000000] Tracing variant of Tasks RCU enabled.
[ 0.000000] rcu: RCU calculated value of scheduler-enlistment delay is 100 ji
[ 0.000000] rcu: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=1
[ 0.000000] NR_IRQS: 524544, nr_irqs: 256, preallocated irqs: 16
[ 0.000000] kfence: initialized - using 2097152 bytes for 255 objects at 0x(_
[ 0.000000] Console: colour VGA+ 80x25
[ 0.000000] printk: console [ttyS0] enabled
[ 0.000000] printk: console [ttyS0] enabled
[ 0.000000] printk: bootconsole [earlyser0] disabled
[ 0.000000] printk: bootconsole [earlyser0] disabled
[ 0.000000] ACPI: Core revision 20210730
[ 0.000000] clocksource: hpet: mask: 0xffffffff max_cycles: 0xffffffff, max_s
[ 0.003000] APIC: Switch to symmetric I/O mode setup
[ 0.006000] ..TIMER: vector=0x30 apic1=0 pin1=2 apic2=-1 pin2=-1
[ 0.014000] tsc: Unable to calibrate against PIT
[ 0.014000] tsc: using HPET reference calibration
[ 0.014000] tsc: Detected 2589.764 MHz processor
[ 0.000346] clocksource: tsc-early: mask: 0xffffffffffffffff max_cycles: 0x25
[ 0.000889] Calibrating delay loop (skipped), value calculated using timer fr
[ 0.001827] pid_max: default: 32768 minimum: 301
[ 0.003270] LSM: Security Framework initializing
[ 0.004154] Yama: becoming mindful.
[ 0.005063] SELinux: Initializing.
[ 0.005890] LSM support for eBPF active
[ 0.006048] landlock: Up and running.
[ 0.007535] Mount-cache hash table entries: 2048 (order: 2, 16384 bytes, line
[ 0.007794] Mountpoint-cache hash table entries: 2048 (order: 2, 16384 bytes,
Poking KASLR using RDTSC...
[ 0.025094] process: using AMD E400 aware idle routine
[ 0.025488] Last level iTLB entries: 4KB 512, 2MB 255, 4MB 127
[ 0.025668] Last level dTLB entries: 4KB 512, 2MB 255, 4MB 127, 1GB 0
[ 0.025958] Spectre V1 : Mitigation: usercopy/swapgs barriers and __user poin
[ 0.026264] Spectre V2 : Mitigation: Retpolines
[ 0.026402] Spectre V2 : Spectre v2 / SpectreRSB mitigation: Filling RSB on c
[ 0.026623] Spectre V2 : Spectre v2 / SpectreRSB : Filling RSB on VMEXIT
[ 0.439883] Freeing SMP alternatives memory: 44K
[ 0.553190] smpboot: CPU0: AMD QEMU Virtual CPU version 2.5+ (family: 0xf, mo
[ 0.561263] Performance Events: PMU not available due to virtualization, usin
[ 0.563065] rcu: Hierarchical SRCU implementation.
[ 0.568142] NMI watchdog: Perf NMI watchdog permanently disabled
[ 0.570440] smp: Bringing up secondary CPUs ...
[ 0.570610] smp: Brought up 1 node, 1 CPU
[ 0.570813] smpboot: Max logical packages: 1
[ 0.571007] smpboot: Total of 1 processors activated (5179.52 BogoMIPS)
[ 0.582662] devtmpfs: initialized
[ 0.585543] x86/mm: Memory block size: 128MB
[ 0.589985] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, ma
[ 0.590433] futex hash table entries: 256 (order: 2, 16384 bytes, linear)
[ 0.593422] pinctrl core: initialized pinctrl subsystem
[ 0.599020] PM: RTC time: 12:58:37, date: 2022-11-27
[ 0.605059] NET: Registered PF_NETLINK/PF_ROUTE protocol family
[ 0.607949] DMA: preallocated 128 KiB GFP_KERNEL pool for atomic allocations
[ 0.608339] DMA: preallocated 128 KiB GFP_KERNEL|GFP_DMA pool for atomic allo
[ 0.608576] DMA: preallocated 128 KiB GFP_KERNEL|GFP_DMA32 pool for atomic al
[ 0.608991] audit: initializing netlink subsys (disabled)
[ 0.613019] audit: type=2000 audit(1669553916.624:1): state=initialized audit
[ 0.615016] thermal_sys: Registered thermal governor 'fair_share'
[ 0.615040] thermal_sys: Registered thermal governor 'bang_bang'
[ 0.615195] thermal_sys: Registered thermal governor 'step_wise'
[ 0.615317] thermal_sys: Registered thermal governor 'user_space'
[ 0.615590] cpuidle: using governor menu
[ 0.616646] ACPI: bus type PCI registered
[ 0.616833] acpiphp: ACPI Hot Plug PCI Controller Driver version: 0.5
[ 0.619275] PCI: Using configuration type 1 for base access
[ 0.634570] Kprobes globally optimized
[ 0.636447] HugeTLB registered 2.00 MiB page size, pre-allocated 0 pages
[ 1.096613] cryptd: max_cpu_qlen set to 1000
[ 1.117382] alg: No test for 842 (842-generic)
[ 1.117886] alg: No test for 842 (842-scomp)
[ 1.157039] DRBG: Continuing without Jitter RNG
[ 1.190659] raid6: skip pq benchmark and using algorithm sse2x4
[ 1.190928] raid6: using intx1 recovery algorithm
[ 1.192446] ACPI: Added _OSI(Module Device)
[ 1.192562] ACPI: Added _OSI(Processor Device)
[ 1.192647] ACPI: Added _OSI(3.0 _SCP Extensions)
[ 1.192732] ACPI: Added _OSI(Processor Aggregator Device)
[ 1.192986] ACPI: Added _OSI(Linux-Dell-Video)
[ 1.193078] ACPI: Added _OSI(Linux-Lenovo-NV-HDMI-Audio)
[ 1.193179] ACPI: Added _OSI(Linux-HPI-Hybrid-Graphics)
[ 1.205775] ACPI: 1 ACPI AML tables successfully acquired and loaded
[ 1.217162] ACPI: Interpreter enabled
[ 1.217894] ACPI: PM: (supports S0 S3 S4 S5)
[ 1.218002] ACPI: Using IOAPIC for interrupt routing
[ 1.218478] PCI: Using host bridge windows from ACPI; if necessary, use "pci=
[ 1.219759] ACPI: Enabled 2 GPEs in block 00 to 0F
[ 1.245149] ACPI: PCI Root Bridge [PCI0] (domain 0000 [bus 00-ff])
[ 1.245891] acpi PNP0A03:00: _OSC: OS supports [ASPM ClockPM Segments MSI EDR
[ 1.246649] acpi PNP0A03:00: fail to add MMCONFIG information, can't access e
[ 1.251687] acpiphp: Slot [3] registered
[ 1.251875] acpiphp: Slot [4] registered
[ 1.252062] acpiphp: Slot [5] registered
[ 1.252259] acpiphp: Slot [6] registered
[ 1.252408] acpiphp: Slot [7] registered
[ 1.252554] acpiphp: Slot [8] registered
[ 1.252696] acpiphp: Slot [9] registered
[ 1.252846] acpiphp: Slot [10] registered
[ 1.252997] acpiphp: Slot [11] registered
[ 1.253174] acpiphp: Slot [12] registered
[ 1.253324] acpiphp: Slot [13] registered
[ 1.253471] acpiphp: Slot [14] registered
[ 1.253618] acpiphp: Slot [15] registered
[ 1.253846] acpiphp: Slot [16] registered
[ 1.254026] acpiphp: Slot [17] registered
[ 1.254224] acpiphp: Slot [18] registered
[ 1.254377] acpiphp: Slot [19] registered
[ 1.254524] acpiphp: Slot [20] registered
[ 1.254673] acpiphp: Slot [21] registered
[ 1.254847] acpiphp: Slot [22] registered
[ 1.255092] acpiphp: Slot [23] registered
[ 1.255267] acpiphp: Slot [24] registered
[ 1.255435] acpiphp: Slot [25] registered
[ 1.255585] acpiphp: Slot [26] registered
[ 1.255732] acpiphp: Slot [27] registered
[ 1.255933] acpiphp: Slot [28] registered
[ 1.256084] acpiphp: Slot [29] registered
[ 1.256275] acpiphp: Slot [30] registered
[ 1.256468] acpiphp: Slot [31] registered
[ 1.256805] PCI host bridge to bus 0000:00
[ 1.257028] pci_bus 0000:00: root bus resource [io 0x0000-0x0cf7 window]
[ 1.257246] pci_bus 0000:00: root bus resource [io 0x0d00-0xffff window]
[ 1.257363] pci_bus 0000:00: root bus resource [mem 0x000a0000-0x000bffff win
[ 1.257518] pci_bus 0000:00: root bus resource [mem 0x40000000-0xfebfffff win
[ 1.257644] pci_bus 0000:00: root bus resource [mem 0x100000000-0x17fffffff w
[ 1.257890] pci_bus 0000:00: root bus resource [bus 00-ff]
[ 1.260837] pci 0000:00:00.0: [8086:1237] type 00 class 0x060000
[ 1.267478] pci 0000:00:01.0: [8086:7000] type 00 class 0x060100
[ 1.268458] pci 0000:00:01.1: [8086:7010] type 00 class 0x010180
[ 1.269908] pci 0000:00:01.1: reg 0x20: [io 0xc040-0xc04f]
[ 1.270590] pci 0000:00:01.1: legacy IDE quirk: reg 0x10: [io 0x01f0-0x01f7]
[ 1.270793] pci 0000:00:01.1: legacy IDE quirk: reg 0x14: [io 0x03f6]
[ 1.270939] pci 0000:00:01.1: legacy IDE quirk: reg 0x18: [io 0x0170-0x0177]
[ 1.271102] pci 0000:00:01.1: legacy IDE quirk: reg 0x1c: [io 0x0376]
[ 1.272288] pci 0000:00:01.3: [8086:7113] type 00 class 0x068000
[ 1.272657] pci 0000:00:01.3: quirk: [io 0x0600-0x063f] claimed by PIIX4 ACP
[ 1.272792] pci 0000:00:01.3: quirk: [io 0x0700-0x070f] claimed by PIIX4 SMB
[ 1.274179] pci 0000:00:02.0: [1234:1111] type 00 class 0x030000
[ 1.274777] pci 0000:00:02.0: reg 0x10: [mem 0xfd000000-0xfdffffff pref]
[ 1.275780] pci 0000:00:02.0: reg 0x18: [mem 0xfebf0000-0xfebf0fff]
[ 1.277344] pci 0000:00:02.0: reg 0x30: [mem 0xfebe0000-0xfebeffff pref]
[ 1.277876] pci 0000:00:02.0: Video device with shadowed ROM at [mem 0x000c00
[ 1.282290] pci 0000:00:03.0: [8086:100e] type 00 class 0x020000
[ 1.282684] pci 0000:00:03.0: reg 0x10: [mem 0xfebc0000-0xfebdffff]
[ 1.283087] pci 0000:00:03.0: reg 0x14: [io 0xc000-0xc03f]
[ 1.284779] pci 0000:00:03.0: reg 0x30: [mem 0xfeb80000-0xfebbffff pref]
[ 1.291361] ACPI: PCI: Interrupt link LNKA configured for IRQ 10
[ 1.292300] ACPI: PCI: Interrupt link LNKB configured for IRQ 10
[ 1.292813] ACPI: PCI: Interrupt link LNKC configured for IRQ 11
[ 1.293289] ACPI: PCI: Interrupt link LNKD configured for IRQ 11
[ 1.293567] ACPI: PCI: Interrupt link LNKS configured for IRQ 9
[ 1.295937] iommu: Default domain type: Translated
[ 1.296047] iommu: DMA domain TLB invalidation policy: lazy mode
[ 1.299168] pci 0000:00:02.0: vgaarb: setting as boot VGA device
[ 1.299369] pci 0000:00:02.0: vgaarb: VGA device added: decodes=io+mem,owns=i
[ 1.299549] pci 0000:00:02.0: vgaarb: bridge control possible
[ 1.299765] vgaarb: loaded
[ 1.301668] SCSI subsystem initialized
[ 1.303105] ACPI: bus type USB registered
[ 1.303493] usbcore: registered new interface driver usbfs
[ 1.303854] usbcore: registered new interface driver hub
[ 1.304059] usbcore: registered new device driver usb
[ 1.304805] pps_core: LinuxPPS API ver. 1 registered
[ 1.304901] pps_core: Software ver. 5.3.6 - Copyright 2005-2007 Rodolfo Giome
[ 1.305099] PTP clock support registered
[ 1.305798] EDAC MC: Ver: 3.0.0
[ 1.312519] NetLabel: Initializing
[ 1.312595] NetLabel: domain hash size = 128
[ 1.312672] NetLabel: protocols = UNLABELED CIPSOv4 CALIPSO
[ 1.313309] NetLabel: unlabeled traffic allowed by default
[ 1.313455] PCI: Using ACPI for IRQ routing
[ 1.316393] hpet: 3 channels of 0 reserved for per-cpu timers
[ 1.316915] hpet0: at MMIO 0xfed00000, IRQs 2, 8, 0
[ 1.317083] hpet0: 3 comparators, 64-bit 100.000000 MHz counter
[ 1.321249] clocksource: Switched to clocksource tsc-early
[ 1.461190] VFS: Disk quotas dquot_6.6.0
[ 1.461452] VFS: Dquot-cache hash table entries: 512 (order 0, 4096 bytes)
[ 1.463272] pnp: PnP ACPI init
[ 1.466951] pnp: PnP ACPI: found 6 devices
[ 1.483940] clocksource: acpi_pm: mask: 0xffffff max_cycles: 0xffffff, max_id
[ 1.484943] NET: Registered PF_INET protocol family
[ 1.485840] IP idents hash table entries: 16384 (order: 5, 131072 bytes, line
[ 1.495204] tcp_listen_portaddr_hash hash table entries: 512 (order: 1, 8192
[ 1.495446] Table-perturb hash table entries: 65536 (order: 6, 262144 bytes,
[ 1.495650] TCP established hash table entries: 8192 (order: 4, 65536 bytes,
[ 1.495972] TCP bind hash table entries: 8192 (order: 5, 131072 bytes, linear
[ 1.496274] TCP: Hash tables configured (established 8192 bind 8192)
[ 1.497703] MPTCP token hash table entries: 1024 (order: 2, 24576 bytes, line
[ 1.498233] UDP hash table entries: 512 (order: 2, 16384 bytes, linear)
[ 1.498517] UDP-Lite hash table entries: 512 (order: 2, 16384 bytes, linear)
[ 1.499832] NET: Registered PF_UNIX/PF_LOCAL protocol family
[ 1.500238] NET: Registered PF_XDP protocol family
[ 1.500963] pci_bus 0000:00: resource 4 [io 0x0000-0x0cf7 window]
[ 1.501107] pci_bus 0000:00: resource 5 [io 0x0d00-0xffff window]
[ 1.501214] pci_bus 0000:00: resource 6 [mem 0x000a0000-0x000bffff window]
[ 1.501333] pci_bus 0000:00: resource 7 [mem 0x40000000-0xfebfffff window]
[ 1.501447] pci_bus 0000:00: resource 8 [mem 0x100000000-0x17fffffff window]
[ 1.502188] pci 0000:00:01.0: PIIX3: Enabling Passive Release
[ 1.502361] pci 0000:00:00.0: Limiting direct PCI/PCI transfers
[ 1.502509] pci 0000:00:01.0: Activating ISA DMA hang workarounds
[ 1.502677] PCI: CLS 0 bytes, default 64
[ 1.506009] Trying to unpack rootfs image as initramfs...
[ 1.548100] Initialise system trusted keyrings
[ 1.549625] Key type blacklist registered
[ 1.554567] workingset: timestamp_bits=36 max_order=18 bucket_order=0
[ 1.576513] zbud: loaded
[ 1.593208] integrity: Platform Keyring initialized
[ 1.632615] Freeing initrd memory: 1204K
[ 1.633594] NET: Registered PF_ALG protocol family
[ 1.633920] xor: measuring software checksum speed
[ 1.638350] prefetch64-sse : 2509 MB/sec
[ 1.642615] generic_sse : 2459 MB/sec
[ 1.642712] xor: using function: prefetch64-sse (2509 MB/sec)
[ 1.642960] Key type asymmetric registered
[ 1.643091] Asymmetric key parser 'x509' registered
[ 1.643561] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 2
[ 1.644306] io scheduler mq-deadline registered
[ 1.644437] io scheduler kyber registered
[ 1.644872] io scheduler bfq registered
[ 1.654823] atomic64_test: passed for x86-64 platform with CX8 and with SSE
[ 1.656426] shpchp: Standard Hot Plug PCI Controller Driver version: 0.4
[ 1.658543] input: Power Button as /devices/LNXSYSTM:00/LNXPWRBN:00/input/inp
[ 1.661113] ACPI: button: Power Button [PWRF]
[ 1.663288] Serial: 8250/16550 driver, 32 ports, IRQ sharing enabled
[ 1.665324] 00:04: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 1655
[ 1.678006] Non-volatile memory driver v1.3
[ 1.678390] Linux agpgart interface v0.103
[ 1.689167] scsi host0: ata_piix
[ 1.691921] scsi host1: ata_piix
[ 1.692394] ata1: PATA max MWDMA2 cmd 0x1f0 ctl 0x3f6 bmdma 0xc040 irq 14
[ 1.692592] ata2: PATA max MWDMA2 cmd 0x170 ctl 0x376 bmdma 0xc048 irq 15
[ 1.697964] ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver
[ 1.698182] ehci-pci: EHCI PCI platform driver
[ 1.698459] ohci_hcd: USB 1.1 'Open' Host Controller (OHCI) Driver
[ 1.698617] ohci-pci: OHCI PCI platform driver
[ 1.698911] uhci_hcd: USB Universal Host Controller Interface driver
[ 1.699764] usbcore: registered new interface driver usbserial_generic
[ 1.700063] usbserial: USB Serial support registered for generic
[ 1.700877] i8042: PNP: PS/2 Controller [PNP0303:KBD,PNP0f13:MOU] at 0x60,0x6
[ 1.703162] serio: i8042 KBD port at 0x60,0x64 irq 1
[ 1.703508] serio: i8042 AUX port at 0x60,0x64 irq 12
[ 1.704806] mousedev: PS/2 mouse device common for all mice
[ 1.707195] input: AT Translated Set 2 keyboard as /devices/platform/i8042/se
[ 1.709655] rtc_cmos 00:05: RTC can wake from S4
[ 1.714296] rtc_cmos 00:05: registered as rtc0
[ 1.714802] rtc_cmos 00:05: setting system clock to 2022-11-27T12:58:38 UTC (
[ 1.715440] rtc_cmos 00:05: alarms up to one day, y3k, 242 bytes nvram, hpet
[ 1.715795] device-mapper: core: CONFIG_IMA_DISABLE_HTABLE is disabled. Dupli
[ 1.716138] device-mapper: uevent: version 1.0.3
[ 1.717054] device-mapper: ioctl: 4.45.0-ioctl (2021-03-22) initialised: dm-d
[ 1.719260] hid: raw HID events driver (C) Jiri Kosina
[ 1.719824] usbcore: registered new interface driver usbhid
[ 1.719946] usbhid: USB HID core driver
[ 1.720574] drop_monitor: Initializing network drop monitor service
[ 1.721609] Initializing XFRM netlink socket
[ 1.722763] NET: Registered PF_INET6 protocol family
[ 1.740027] Segment Routing with IPv6
[ 1.740201] RPL Segment Routing with IPv6
[ 1.740434] In-situ OAM (IOAM) with IPv6
[ 1.740835] mip6: Mobile IPv6
[ 1.741003] NET: Registered PF_PACKET protocol family
[ 1.741469] mctp: management component transport protocol core
[ 1.741581] NET: Registered PF_MCTP protocol family
[ 1.743677] IPI shorthand broadcast: enabled
[ 1.744215] sched_clock: Marking stable (1758023367, -14240970)->(1745278566,
[ 1.746836] registered taskstats version 1
[ 1.747643] Loading compiled-in X.509 certificates
[ 1.753028] Loaded X.509 cert 'Build time autogenerated kernel key: cde4c69dd
[ 1.754125] zswap: loaded using pool lzo/zbud
[ 1.755544] page_owner is disabled
[ 1.756282] Key type .fscrypt registered
[ 1.756362] Key type fscrypt-provisioning registered
[ 1.760528] Btrfs loaded, crc32c=crc32c-generic, zoned=yes, fsverity=yes
[ 1.761493] Key type big_key registered
[ 1.765957] Key type encrypted registered
[ 1.766452] ima: No TPM chip found, activating TPM-bypass!
[ 1.766651] Loading compiled-in module X.509 certificates
[ 1.768305] Loaded X.509 cert 'Build time autogenerated kernel key: cde4c69dd
[ 1.768515] ima: Allocated hash algorithm: sha256
[ 1.769997] ima: No architecture policies found
[ 1.770618] evm: Initialising EVM extended attributes:
[ 1.770737] evm: security.selinux
[ 1.770915] evm: security.SMACK64 (disabled)
[ 1.771010] evm: security.SMACK64EXEC (disabled)
[ 1.771085] evm: security.SMACK64TRANSMUTE (disabled)
[ 1.771167] evm: security.SMACK64MMAP (disabled)
[ 1.771241] evm: security.apparmor (disabled)
[ 1.771305] evm: security.ima
[ 1.771358] evm: security.capability
[ 1.771431] evm: HMAC attrs: 0x1
[ 1.773499] PM: Magic number: 14:774:987
[ 1.773682] machinecheck machinecheck0: hash matches
[ 1.774414] RAS: Correctable Errors collector initialized.
[ 1.854249] ata2.00: ATAPI: QEMU DVD-ROM, 2.5+, max UDMA/100
[ 1.864702] scsi 1:0:0:0: CD-ROM QEMU QEMU DVD-ROM 2.5+ PQ
[ 1.884108] sr 1:0:0:0: [sr0] scsi3-mmc drive: 4x/4x cd/rw xa/form2 tray
[ 1.884476] cdrom: Uniform CD-ROM driver Revision: 3.20
[ 1.904798] sr 1:0:0:0: Attached scsi generic sg0 type 5
[ 1.912391] Freeing unused decrypted memory: 2036K
[ 1.954047] Freeing unused kernel image (initmem) memory: 3132K
[ 1.954824] Write protecting the kernel read-only data: 30720k
[ 1.957156] Freeing unused kernel image (text/rodata gap) memory: 2036K
[ 1.957594] Freeing unused kernel image (rodata/data gap) memory: 268K
[ 2.089931] x86/mm: Checked W+X mappings: passed, no W+X pages found.
[ 2.090256] rodata_test: all tests were successful
[ 2.090954] Run /init as init process
HelloToYou
sh: can't access tty; job control turned off
/ # [ 2.514386] tsc: Refined TSC clocksource calibration: 2592.005 MHz
[ 2.514851] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x255cbb91
[ 2.515190] clocksource: Switched to clocksource tsc
[ 2.603388] input: ImExPS/2 Generic Explorer Mouse as /devices/platform/i8042

/ # ls
sh: ls: not found
/ # busybox ls
bin dev init root
/ # busybox echo hello you
hello you
/ # busybox ps
PID USER TIME COMMAND
ps: can't open '/proc': No such file or directory
/ # QEMU: Terminated

改进

增加proc文件系统

在上面制作的initramfs里面,如果执行ps命令会报错

1
2
3
/ # busybox ps
PID USER TIME COMMAND
ps: can't open '/proc': No such file or directory

因为确实是没有proc文件系统,如果需要使用这个功能的话,需要挂载proc文件系统,需要在init进程里面执行挂载的操作,也就是需要修改init文件

1
2
3
4
#!/bin/busybox sh
/bin/busybox mkdir -p /proc && /bin/busybox mount -t proc none /proc
/bin/busybox echo "HelloToYou"
/bin/busybox shs

重新编译就可以了

1
2
3
4
#!/bin/busybox sh
/bin/busybox mkdir -p /proc && /bin/busybox mount -t proc none /proc
/bin/busybox echo "HelloToYou"
/bin/busybox shs