# QEMU linux kernel and rootfs (busybox-1.22.0 and linux-4.4.50)
###### tags: `linux` busybox` kernel`
<style>
.blue {
color: blue;
}
.bgblue {
color: blue;
font-size: 24px;
}
.red {
color: red;
font-size: 24px;
}
h1 {text-align: center;}
</style>
Author: WhoAmI
email: kccddb@gmail.com
Date: 20230224
Copyright: CC BY-NC-SA
---
Interface names:
<span class="blue">enp0s3</span> : NAT
<span class="blue">enp0s8</span> : Bridged Mode
Ubuntu 16.04.7 (server version)
kclai@ubuntu16:/boot$ uname -r
4.4.0-186-generic
kclai@ubuntu16:/boot$
[Linux Cross Reference ](https://lxr.linux.no/linux+v4.4.50/)
[BusyBox Cross Reference](https://elixir.bootlin.com/busybox/latest/source)
<h1>
Build Linux kernel 4.4.50 x86_64 Version
</h1>
<span class="blue">
編譯 Linux kernel 與 建構 rootfs 是 很基本的工作~務必學會
</span>
**找一不算離很多且不用patch 的 4.4.x 的版本**
wget https://mirrors.edge.kernel.org/pub/linux/kernel/v4.x/linux-4.4.50.tar.gz
tar xvfz linux-4.4.50.tar.gz
:::info
sudo apt install bc
sudo apt-get install libssl-dev
:::
**使用 64 bits
meke menuconfig
make**
日後再來幫 linux kernel 減肥瘦身
[Chapter 11. Kernel build command line reference](https://www.linuxtopia.org/online_books/linux_kernel/kernel_configuration/ch11s03.html)
kclai@ubuntu16:/share/src/linux-4.4.50/arch/x86/boot$
---
:::info
[Testing/System Images](https://wiki.qemu.org/Testing/System_Images)
:::
用 qemu-system-x86_64 測試
**sudo qemu-system-x86_64 -machine pc -m 1024 -kernel bzImage -nographic -append "console=ttyS0"**
---
:::info
...
2.317362] 6230002000000010 ffff88003e2efe80 ffff88003e2efe18 00000000c86e4
[ 2.317362] Call Trace:
[ 2.317362] [<ffffffff813d8b83>] dump_stack+0x63/0x90
[ 2.317362] [<ffffffff8118aa47>] panic+0xd3/0x215
[ 2.317362] [<ffffffff81f5a576>] mount_block_root+0x201/0x294
[ 2.317362] [<ffffffff81f5a66e>] mount_root+0x65/0x68
[ 2.317362] [<ffffffff81f5a7ab>] prepare_namespace+0x13a/0x172
[ 2.317362] [<ffffffff81f5a21b>] kernel_init_freeable+0x1e9/0x212
[ 2.317362] [<ffffffff81807b50>] ? rest_init+0x80/0x80
[ 2.317362] [<ffffffff81807b5e>] kernel_init+0xe/0xe0
[ 2.317362] [<ffffffff8181468f>] ret_from_fork+0x3f/0x70
[ 2.317362] [<ffffffff81807b50>] ? rest_init+0x80/0x80
[ 2.317362] Kernel Offset: disabled
**[ 2.317362] ---[ end Kernel panic - not syncing: VFS: Unable to mount root f)**
:::
:::success
有時候重造 kernel 可能需要
[Why both `make clean` and `make mrproper` are used? ](https://unix.stackexchange.com/questions/387640/why-both-make-clean-and-make-mrproper-are-used)
:::
**正常狀況, 因為沒有 rootfs!!!**
:::info
https://lxr.linux.no/linux+v4.4.50/init/main.c
**通常 Linux 版本愈新 通常愈複雜, 最好由 Linux 2.4 以前開始 看kernel source**
```c=
asmlinkage __visible void __init start_kernel(void)
498{
499 char *command_line;
500 char *after_dashes;
501
502 /*
503 * Need to run as early as possible, to initialize the
504 * lockdep hash:
505 */
506 lockdep_init();
507 set_task_stack_end_magic(&init_task);
508 smp_setup_processor_id();
509 debug_objects_early_init();
510
511 /*
512 * Set up the the initial canary ASAP:
513 */
514 boot_init_stack_canary();
515
516 cgroup_init_early();
517
518 local_irq_disable();
519 early_boot_irqs_disabled = true;
520
521/*
522 * Interrupts are still disabled. Do necessary setups, then
523 * enable them
524 */
525 boot_cpu_init();
526 page_address_init();
527 pr_notice("%s", linux_banner);
528 setup_arch(&command_line);
529 mm_init_cpumask(&init_mm);
530 setup_command_line(command_line);
531 setup_nr_cpu_ids();
532 setup_per_cpu_areas();
533 smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
534
535 build_all_zonelists(NULL, NULL);
536 page_alloc_init();
537
538 pr_notice("Kernel command line: %s\n", boot_command_line);
539 parse_early_param();
540 after_dashes = parse_args("Booting kernel",
541 static_command_line, __start___param,
542 __stop___param - __start___param,
543 -1, -1, NULL, &unknown_bootoption);
544 if (!IS_ERR_OR_NULL(after_dashes))
545 parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
546 NULL, set_init_arg);
547
548 jump_label_init();
549
550 /*
551 * These use large bootmem allocations and must precede
552 * kmem_cache_init()
553 */
554 setup_log_buf(0);
555 pidhash_init();
556 vfs_caches_init_early();
557 sort_main_extable();
558 trap_init();
559 mm_init();
560
561 /*
562 * Set up the scheduler prior starting any interrupts (such as the
563 * timer interrupt). Full topology setup happens at smp_init()
564 * time - but meanwhile we still have a functioning scheduler.
565 */
566 sched_init();
567 /*
568 * Disable preemption - early bootup scheduling is extremely
569 * fragile until we cpu_idle() for the first time.
570 */
571 preempt_disable();
572 if (WARN(!irqs_disabled(),
573 "Interrupts were enabled *very* early, fixing it\n"))
574 local_irq_disable();
575 idr_init_cache();
576 rcu_init();
577
578 /* trace_printk() and trace points may be used after this */
579 trace_init();
580
581 context_tracking_init();
582 radix_tree_init();
583 /* init some links before init_ISA_irqs() */
584 early_irq_init();
585 init_IRQ();
586 tick_init();
587 rcu_init_nohz();
588 init_timers();
589 hrtimers_init();
590 softirq_init();
591 timekeeping_init();
592 time_init();
593 sched_clock_postinit();
594 perf_event_init();
595 profile_init();
596 call_function_init();
597 WARN(!irqs_disabled(), "Interrupts were enabled early\n");
598 early_boot_irqs_disabled = false;
599 local_irq_enable();
600
601 kmem_cache_init_late();
602
603 /*
604 * HACK ALERT! This is early. We're enabling the console before
605 * we've done PCI setups etc, and console_init() must be aware of
606 * this. But we do want output early, in case something goes wrong.
607 */
608 console_init();
609 if (panic_later)
610 panic("Too many boot %s vars at `%s'", panic_later,
611 panic_param);
612
613 lockdep_info();
614
615 /*
616 * Need to run this when irqs are enabled, because it wants
617 * to self-test [hard/soft]-irqs on/off lock inversion bugs
618 * too:
619 */
620 locking_selftest();
621
622#ifdef CONFIG_BLK_DEV_INITRD
623 if (initrd_start && !initrd_below_start_ok &&
624 page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
625 pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
626 page_to_pfn(virt_to_page((void *)initrd_start)),
627 min_low_pfn);
628 initrd_start = 0;
629 }
630#endif
631 page_ext_init();
632 debug_objects_mem_init();
633 kmemleak_init();
634 setup_per_cpu_pageset();
635 numa_policy_init();
636 if (late_time_init)
637 late_time_init();
638 sched_clock_init();
639 calibrate_delay();
640 pidmap_init();
641 anon_vma_init();
642 acpi_early_init();
643#ifdef CONFIG_X86
644 if (efi_enabled(EFI_RUNTIME_SERVICES))
645 efi_enter_virtual_mode();
646#endif
647#ifdef CONFIG_X86_ESPFIX64
648 /* Should be run before the first non-init thread is created */
649 init_espfix_bsp();
650#endif
651 thread_info_cache_init();
652 cred_init();
653 fork_init();
654 proc_caches_init();
655 buffer_init();
656 key_init();
657 security_init();
658 dbg_late_init();
659 vfs_caches_init();
660 signals_init();
661 /* rootfs populating might need page-writeback */
662 page_writeback_init();
663 proc_root_init();
664 nsfs_init();
665 cpuset_init();
666 cgroup_init();
667 taskstats_init_early();
668 delayacct_init();
669
670 check_bugs();
671
672 acpi_subsystem_init();
673 sfi_init_late();
674
675 if (efi_enabled(EFI_RUNTIME_SERVICES)) {
676 efi_late_init();
677 efi_free_boot_services();
678 }
679
680 ftrace_init();
681
682 /* Do the rest non-__init'ed, we're now alive */
683 rest_init();
684}
685
```
:::info
rest_init();
```c=
static noinline void __init_refok rest_init(void)
387{
388 int pid;
389
390 rcu_scheduler_starting();
391 smpboot_thread_init();
392 /*
393 * We need to spawn init first so that it obtains pid 1, however
394 * the init task will end up wanting to create kthreads, which, if
395 * we schedule it before we create kthreadd, will OOPS.
396 */
397 kernel_thread(kernel_init, NULL, CLONE_FS);
398 numa_default_policy();
399 pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
400 rcu_read_lock();
401 kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
402 rcu_read_unlock();
403 complete(&kthreadd_done);
404
405 /*
406 * The boot idle thread must execute schedule()
407 * at least once to get things moving:
408 */
409 init_idle_bootup_task(current);
410 schedule_preempt_disabled();
411 /* Call into cpu_idle with preempt disabled */
412 cpu_startup_entry(CPUHP_ONLINE);
413}
```
kernel_init
```c=
static int __ref kernel_init(void *unused)
933{
934 int ret;
935
936 kernel_init_freeable();
937 /* need to finish all async __init code before freeing the memory */
938 async_synchronize_full();
939 free_initmem();
940 mark_rodata_ro();
941 system_state = SYSTEM_RUNNING;
942 numa_default_policy();
943
944 flush_delayed_fput();
945
946 if (ramdisk_execute_command) {
947 ret = run_init_process(ramdisk_execute_command);
948 if (!ret)
949 return 0;
950 pr_err("Failed to execute %s (error %d)\n",
951 ramdisk_execute_command, ret);
952 }
953
954 /*
955 * We try each of these until one succeeds.
956 *
957 * The Bourne shell can be used instead of init if we are
958 * trying to recover a really broken machine.
959 */
960 if (execute_command) {
961 ret = run_init_process(execute_command);
962 if (!ret)
963 return 0;
964 panic("Requested init %s failed (error %d).",
965 execute_command, ret);
966 }
967 if (!try_to_run_init_process("/sbin/init") ||
968 !try_to_run_init_process("/etc/init") ||
969 !try_to_run_init_process("/bin/init") ||
970 !try_to_run_init_process("/bin/sh"))
971 return 0;
972
973 panic("No working init found. Try passing init= option to kernel. "
974 "See Linux Documentation/init.txt for guidance.");
975}
976
```
:::
**kclai@ubuntu16:~$ ip addr show**
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 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 forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:d5:31:79 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fed5:3179/64 scope link
valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:22:41:71 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.31/24 scope global enp0s8
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe22:4171/64 scope link
valid_lft forever preferred_lft forever
kclai@ubuntu16:~$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 10.0.2.2 0.0.0.0 UG 0 0 0 enp0s3
10.0.2.0 * 255.255.255.0 U 0 0 0 enp0s3
192.168.1.0 * 255.255.255.0 U 0 0 0 enp0s8
kclai@ubuntu16:~$
:::
start network
:::info
kclai@ubuntu16:~$ cat net.sh
#/bin/sh
sudo ifconfig enp0s8 up
sudo ip addr add 192.168.1.31/24 dev enp0s8
sudo service ssh restart
sudo service ssh status
sudo /etc/init.d/samba reload
:::
kclai@ubuntu16:~$
<h1>
busybox and rootfs
</h1>
wget https://busybox.net/downloads/busybox-1.22.0.tar.bz2
https://elixir.bootlin.com/busybox/1.22.0/source/include/applets.src.h
**kclai@ubuntu16:/share/src$ tar jxvf busybox-1.22.0.tar.bz2**
**Setup busybox**
kclai@ubuntu16:/share/src/busybox-1.22.0$ pwd
/share/src/busybox-1.22.0
kclai@ubuntu16:/share/src/busybox-1.22.0$ make menuconfig
x86 先試試
make
make install
[**運用 ldd**](https://www.man7.org/linux/man-pages/man1/ldd.1.html)
**ldd prints the shared objects (shared libraries) required by each
program or shared object specified on the command line.**
:::info
kclai@ubuntu16:/share/src/busybox-1.22.0/_install/bin$ ldd busybox
linux-vdso.so.1 => (0x00007ffe1f1cf000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f6de0694000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6de02ca000)
/lib64/ld-linux-x86-64.so.2 (0x00007f6de099d000)
kclai@ubuntu16:/share/src/busybox-1.22.0/_install/bin$
:::
上課時 再解釋 如何設計的 (回憶 c Language main(int argc, char argv*[]) 與 symbolic Linux/ UNIX link)
:::info
kclai@ubuntu16:/share/src/busybox-1.22.0/_install/bin$ ls -al
total 844
drwxrwxr-x 2 kclai kclai 4096 Oct 2 09:15 .
drwxrwxr-x 5 kclai kclai 4096 Oct 2 09:15 ..
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 ash -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 base64 -> busybox
-rwxr-xr-x 1 kclai kclai 853592 Oct 2 09:15 busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 cat -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 catv -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 chattr -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 chgrp -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 chmod -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 chown -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 conspy -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 cp -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 cpio -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 cttyhack -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 date -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 dd -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 df -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 dmesg -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 dnsdomainname -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 dumpkmap -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 echo -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 ed -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 egrep -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 false -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 fdflush -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 fgrep -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 fsync -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 getopt -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 grep -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 gunzip -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 gzip -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 hostname -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 hush -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 ionice -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 iostat -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 ipcalc -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 kbd_mode -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 kill -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 linux32 -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 linux64 -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 ln -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 login -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 ls -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 lsattr -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 lzop -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 makemime -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 mkdir -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 mknod -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 mktemp -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 more -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 mount -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 mountpoint -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 mpstat -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 mt -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 mv -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 netstat -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 nice -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 pidof -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 ping -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 ping6 -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 pipe_progress -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 printenv -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 ps -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 pwd -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 reformime -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 rev -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 rm -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 rmdir -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 rpm -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 run-parts -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 scriptreplay -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 sed -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 setarch -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 setserial -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 sh -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 sleep -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 stat -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 stty -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 su -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 sync -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 tar -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 touch -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 true -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 umount -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 uname -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 usleep -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 vi -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 watch -> busybox
lrwxrwxrwx 1 kclai kclai 7 Oct 2 09:15 zcat -> busybox
kclai@ubuntu16:/share/src/busybox-1.22.0/_install/bin$
:::
**需要 shared libraries:**
kclai@ubuntu16:/share/src/busybox-1.22.0/_install$ ls
bin linuxrc sbin usr
**kclai@ubuntu16:/share/src/busybox-1.22.0/_install$ mkdir lib**
kclai@ubuntu16:/share/src/busybox-1.22.0/_install$ cd lib
kclai@ubuntu16:/share/src/busybox-1.22.0/_install/lib$ cp /lib/x86_64-linux-gnu/libm.so.6 .
kclai@ubuntu16:/share/src/busybox-1.22.0/_install/lib$ ls
libm.so.6
kclai@ubuntu16:/share/src/busybox-1.22.0/_install/lib$ cp /lib/x86_64-linux-gnu/libc.so.6 .
kclai@ubuntu16:/share/src/busybox-1.22.0/_install/lib$ cp /lib64/ld-linux-x86-64.so.2 .
kclai@ubuntu16:/share/src/busybox-1.22.0/_install/lib$
/lib64/ld-linux-x86-64.so.2 我 cp 至錯誤路徑, chroot 測才發現!!!
**製造路徑**
**mkdir proc sys etc etc/init.d dev**
**dev/console, dev/ram**
**sudo mknod dev/console c 5 1**
**sudo mknod dev/ram b 1 0**
```c=
kclai@ubuntu16:/share/src/busybox-1.22.0/_install/dev$ ls -al
total 8
drwxrwxr-x 2 kclai kclai 4096 Feb 24 08:28 .
drwxrwxr-x 13 kclai kclai 4096 Feb 24 08:31 ..
crw-r--r-- 1 root root 5, 1 Oct 3 08:23 console
brw-r--r-- 1 root root 1, 0 Feb 24 08:28 ram
kclai@ubuntu16:/share/src/busybox-1.22.0/_install/dev$
```
:::info
drwxrwxr-x 10 kclai kclai 4096 Oct 2 09:43 .
drwxr-xr-x 35 kclai kclai 4096 Oct 2 09:15 ..
drwxrwxr-x 2 kclai kclai 4096 Oct 2 09:33 bin
drwxrwxr-x 2 kclai kclai 4096 Oct 2 09:43 dev
drwxrwxr-x 3 kclai kclai 4096 Oct 2 09:43 etc
drwxrwxr-x 2 kclai kclai 4096 Oct 2 09:23 lib
lrwxrwxrwx 1 kclai kclai 11 Oct 2 09:15 linuxrc -> bin/busybox
drwxrwxr-x 2 kclai kclai 4096 Oct 2 09:40 proc
drwxrwxr-x 2 kclai kclai 4096 Oct 2 09:15 sbin
drwxrwxr-x 2 kclai kclai 4096 Oct 2 09:40 sys
drwxrwxr-x 4 kclai kclai 4096 Oct 2 09:15 usr
kclai@ubuntu16:/share/src/busybox-1.22.0/_install$
```c=
# cat go.sh
#!/bin/sh
find . -print0 | cpio --null -ov --format=newc | gzip -9 >/share/initramfs.gz
/ #
執行 go.sh 產生 /share/initramfs.gz, 再複製至 /share/x86
```
sudo qemu-system-x86_64 -machine pc -kernel ./bzImage -initrd ./initramfs.gz -nographic -m 512 -append "root=/dev/ram rdinit=/sbin/init console=ttyS0 nokaslr"
```c=
iPXE (http://ipxe.org) 00:03.0 CA00 PCI2.10 PnP PMM+1FF90770+1FEF0770 CA00
Booting from ROM...
[ 0.000000] Initializing cgroup subsys cpuset
[ 0.000000] Initializing cgroup subsys cpu
[ 0.000000] Initializing cgroup subsys cpuacct
[ 0.000000] Linux version 4.4.50 (kclai@ubuntu16) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.2
[ 0.000000] Command line: root=/dev/ram rdinit=/sbin/init console=ttyS0 nokaslr
[ 0.000000] x86/fpu: Legacy x87 FPU detected.
[ 0.000000] x86/fpu: Using 'lazy' FPU context switches.
[ 0.000000] e820: 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-0x000000001ffdffff] usable
[ 0.000000] BIOS-e820: [mem 0x000000001ffe0000-0x000000001fffffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved
[ 0.000000] NX (Execute Disable) protection: active
[ 0.000000] SMBIOS 2.8 present.
[ 0.000000] e820: last_pfn = 0x1ffe0 max_arch_pfn = 0x400000000
[ 0.000000] x86/PAT: Configuration [0-7]: WB WC UC- UC WB WC UC- WT
[ 0.000000] found SMP MP-table at [mem 0x000f5ab0-0x000f5abf] mapped at [ffff8800000f5ab0]
[ 0.000000] Scanning 1 areas for low memory corruption
[ 0.000000] RAMDISK: [mem 0x1fdd8000-0x1ffdffff]
[ 0.000000] ACPI: Early table checksum verification disabled
[ 0.000000] ACPI: RSDP 0x00000000000F58D0 000014 (v00 BOCHS )
[ 0.000000] ACPI: RSDT 0x000000001FFE156F 000030 (v01 BOCHS BXPCRSDT 00000001 BXPC 00000001)
[ 0.000000] ACPI: FACP 0x000000001FFE144B 000074 (v01 BOCHS BXPCFACP 00000001 BXPC 00000001)
[ 0.000000] ACPI: DSDT 0x000000001FFE0040 00140B (v01 BOCHS BXPCDSDT 00000001 BXPC 00000001)
[ 0.000000] ACPI: FACS 0x000000001FFE0000 000040
[ 0.000000] ACPI: APIC 0x000000001FFE14BF 000078 (v01 BOCHS BXPCAPIC 00000001 BXPC 00000001)
[ 0.000000] ACPI: HPET 0x000000001FFE1537 000038 (v01 BOCHS BXPCHPET 00000001 BXPC 00000001)
[ 0.000000] No NUMA configuration found
[ 0.000000] Faking a node at [mem 0x0000000000000000-0x000000001ffdffff]
[ 0.000000] NODE_DATA(0) allocated [mem 0x1fdd4000-0x1fdd7fff]
[ 0.000000] Zone ranges:
[ 0.000000] DMA [mem 0x0000000000001000-0x0000000000ffffff]
[ 0.000000] DMA32 [mem 0x0000000001000000-0x000000001ffdffff]
[ 0.000000] Normal 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-0x000000001ffdffff]
[ 0.000000] Initmem setup node 0 [mem 0x0000000000001000-0x000000001ffdffff]
[ 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] 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: Registered nosave memory: [mem 0x00000000-0x00000fff]
[ 0.000000] PM: Registered nosave memory: [mem 0x0009f000-0x0009ffff]
[ 0.000000] PM: Registered nosave memory: [mem 0x000a0000-0x000effff]
[ 0.000000] PM: Registered nosave memory: [mem 0x000f0000-0x000fffff]
[ 0.000000] e820: [mem 0x20000000-0xfffbffff] available for PCI devices
[ 0.000000] clocksource: refined-jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19109699s
[ 0.000000] setup_percpu: NR_CPUS:64 nr_cpumask_bits:64 nr_cpu_ids:1 nr_node_ids:1
[ 0.000000] PERCPU: Embedded 32 pages/cpu @ffff88001fa00000 s90648 r8192 d32232 u2097152
[ 0.000000] Built 1 zonelists in Node order, mobility grouping on. Total pages: 128873
[ 0.000000] Policy zone: DMA32
[ 0.000000] Kernel command line: root=/dev/ram rdinit=/sbin/init console=ttyS0 nokaslr
[ 0.000000] PID hash table entries: 2048 (order: 2, 16384 bytes)
[ 0.000000] Memory: 495664K/523768K available (8713K kernel code, 1143K rwdata, 2764K rodata, 1080K init)
[ 0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
[ 0.000000] Hierarchical RCU implementation.
[ 0.000000] Build-time adjustment of leaf fanout to 64.
[ 0.000000] RCU restricting CPUs from NR_CPUS=64 to nr_cpu_ids=1.
[ 0.000000] RCU: Adjusting geometry for rcu_fanout_leaf=64, nr_cpu_ids=1
[ 0.000000] NR_IRQS:4352 nr_irqs:256 16
[ 0.000000] Console: colour VGA+ 80x25
[ 0.000000] console [ttyS0] enabled
[ 0.000000] clocksource: hpet: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604467 ns
[ 0.000000] tsc: Unable to calibrate against PIT
[ 0.000000] tsc: using HPET reference calibration
[ 0.000000] tsc: Detected 2807.976 MHz processor
[ 0.013000] Calibrating delay loop (skipped), value calculated using timer frequency.. 5615.95 BogoMIPS )
[ 0.016150] pid_max: default: 32768 minimum: 301
[ 0.017277] ACPI: Core revision 20150930
[ 0.049609] ACPI: 1 ACPI AML tables successfully acquired and loaded
[ 0.052610] Security Framework initialized
[ 0.053195] SELinux: Initializing.
[ 0.056148] Dentry cache hash table entries: 65536 (order: 7, 524288 bytes)
[ 0.059556] Inode-cache hash table entries: 32768 (order: 6, 262144 bytes)
[ 0.062042] Mount-cache hash table entries: 1024 (order: 1, 8192 bytes)
[ 0.063095] Mountpoint-cache hash table entries: 1024 (order: 1, 8192 bytes)
[ 0.082207] Initializing cgroup subsys freezer
[ 0.087036] mce: CPU supports 10 MCE banks
[ 0.090788] Last level iTLB entries: 4KB 0, 2MB 0, 4MB 0
[ 0.091062] Last level dTLB entries: 4KB 0, 2MB 0, 4MB 0, 1GB 0
[ 0.400567] Freeing SMP alternatives memory: 32K (ffffffff8202d000 - ffffffff82035000)
[ 0.427000] ..TIMER: vector=0x30 apic1=0 pin1=2 apic2=-1 pin2=-1
[ 0.436000] APIC calibration not consistent with PM-Timer: 101ms instead of 100ms
[ 0.436000] APIC delta adjusted to PM-Timer: 6249988 (6344588)
[ 0.437600] smpboot: CPU0: AMD QEMU Virtual CPU version 2.5+ (family: 0x6, model: 0x6, stepping: 0x3)
[ 0.443315] Performance Events: Broken PMU hardware detected, using software events only.
[ 0.445060] Failed to access perfctr msr (MSR c0010007 is 0)
[ 0.458442] Huh? What family is it: 0x6?!
[ 0.459064] MCE: In-kernel MCE decoding enabled.
[ 0.460438] x86: Booted up 1 node, 1 CPUs
[ 0.462145] smpboot: Total of 1 processors activated (5615.95 BogoMIPS)
[ 0.481644] devtmpfs: initialized
[ 0.493746] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 1911260446275000s
[ 0.504254] RTC time: 0:34:09, date: 02/24/23
[ 0.514031] kworker/u2:0 (13) used greatest stack depth: 15040 bytes left
[ 0.519280] NET: Registered protocol family 16
[ 0.534270] cpuidle: using governor ladder
[ 0.535516] cpuidle: using governor menu
[ 0.539064] ACPI: bus type PCI registered
[ 0.543230] PCI: Using configuration type 1 for base access
[ 0.554145] kworker/u2:1 (34) used greatest stack depth: 14856 bytes left
[ 0.556592] kworker/u2:1 (36) used greatest stack depth: 14152 bytes left
[ 0.685250] ACPI: Added _OSI(Module Device)
[ 0.687123] ACPI: Added _OSI(Processor Device)
[ 0.688039] ACPI: Added _OSI(3.0 _SCP Extensions)
[ 0.689022] ACPI: Added _OSI(Processor Aggregator Device)
[ 0.725118] ACPI: Interpreter enabled
[ 0.728019] ACPI: (supports S0 S3 S4 S5)
[ 0.728276] ACPI: Using IOAPIC for interrupt routing
[ 0.729399] PCI: Using host bridge windows from ACPI; if necessary, use "pci=nocrs" and report a bug
[ 0.806957] ACPI: PCI Root Bridge [PCI0] (domain 0000 [bus 00-ff])
[ 0.809637] acpi PNP0A03:00: _OSC: OS supports [ASPM ClockPM Segments MSI]
[ 0.810421] acpi PNP0A03:00: _OSC failed (AE_NOT_FOUND); disabling ASPM
[ 0.811817] acpi PNP0A03:00: fail to add MMCONFIG information, can't access extended PCI configuration s.
[ 0.816000] PCI host bridge to bus 0000:00
[ 0.816317] pci_bus 0000:00: root bus resource [io 0x0000-0x0cf7 window]
[ 0.817092] pci_bus 0000:00: root bus resource [io 0x0d00-0xffff window]
[ 0.818061] pci_bus 0000:00: root bus resource [mem 0x000a0000-0x000bffff window]
[ 0.820333] pci_bus 0000:00: root bus resource [mem 0x20000000-0xfebfffff window]
[ 0.821479] pci_bus 0000:00: root bus resource [mem 0x100000000-0x17fffffff window]
[ 0.822205] pci_bus 0000:00: root bus resource [bus 00-ff]
[ 0.836298] pci 0000:00:01.1: legacy IDE quirk: reg 0x10: [io 0x01f0-0x01f7]
[ 0.838047] pci 0000:00:01.1: legacy IDE quirk: reg 0x14: [io 0x03f6]
[ 0.840163] pci 0000:00:01.1: legacy IDE quirk: reg 0x18: [io 0x0170-0x0177]
[ 0.841077] pci 0000:00:01.1: legacy IDE quirk: reg 0x1c: [io 0x0376]
[ 0.845194] pci 0000:00:01.3: quirk: [io 0x0600-0x063f] claimed by PIIX4 ACPI
[ 0.846189] pci 0000:00:01.3: quirk: [io 0x0700-0x070f] claimed by PIIX4 SMB
[ 0.880303] ACPI: PCI Interrupt Link [LNKA] (IRQs 5 *10 11)
[ 0.884484] ACPI: PCI Interrupt Link [LNKB] (IRQs 5 *10 11)
[ 0.887000] ACPI: PCI Interrupt Link [LNKC] (IRQs 5 10 *11)
[ 0.889775] ACPI: PCI Interrupt Link [LNKD] (IRQs 5 10 *11)
[ 0.891628] ACPI: PCI Interrupt Link [LNKS] (IRQs *9)
[ 0.897000] ACPI: Enabled 2 GPEs in block 00 to 0F
[ 0.904616] vgaarb: setting as boot device: PCI:0000:00:02.0
[ 0.905000] vgaarb: device added: PCI:0000:00:02.0,decodes=io+mem,owns=io+mem,locks=none
[ 0.905069] vgaarb: loaded
[ 0.906140] vgaarb: bridge control possible 0000:00:02.0
[ 0.910329] SCSI subsystem initialized
[ 0.915498] ACPI: bus type USB registered
[ 0.917305] usbcore: registered new interface driver usbfs
[ 0.920520] usbcore: registered new interface driver hub
[ 0.922566] usbcore: registered new device driver usb
[ 0.925407] pps_core: LinuxPPS API ver. 1 registered
[ 0.926143] pps_core: Software ver. 5.3.6 - Copyright 2005-2007 Rodolfo Giometti <giometti@linux.it>
[ 0.929210] PTP clock support registered
[ 0.933751] Advanced Linux Sound Architecture Driver Initialized.
[ 0.935666] PCI: Using ACPI for IRQ routing
[ 0.959349] NetLabel: Initializing
[ 0.960084] NetLabel: domain hash size = 128
[ 0.961030] NetLabel: protocols = UNLABELED CIPSOv4
[ 0.964688] NetLabel: unlabeled traffic allowed by default
[ 0.968238] HPET: 3 timers in total, 0 timers will be used for per-cpu timer
[ 0.970464] hpet0: at MMIO 0xfed00000, IRQs 2, 8, 0
[ 0.971211] hpet0: 3 comparators, 64-bit 100.000000 MHz counter
[ 0.978321] amd_nb: Cannot enumerate AMD northbridges
[ 0.979452] clocksource: Switched to clocksource hpet
[ 1.134438] pnp: PnP ACPI init
[ 1.170954] pnp: PnP ACPI: found 6 devices
[ 1.247187] clocksource: acpi_pm: mask: 0xffffff max_cycles: 0xffffff, max_idle_ns: 2085701024 ns
[ 1.256503] NET: Registered protocol family 2
[ 1.269211] TCP established hash table entries: 4096 (order: 3, 32768 bytes)
[ 1.276816] TCP bind hash table entries: 4096 (order: 4, 65536 bytes)
[ 1.279665] TCP: Hash tables configured (established 4096 bind 4096)
[ 1.282799] UDP hash table entries: 256 (order: 1, 8192 bytes)
[ 1.285486] UDP-Lite hash table entries: 256 (order: 1, 8192 bytes)
[ 1.290502] NET: Registered protocol family 1
[ 1.298507] RPC: Registered named UNIX socket transport module.
[ 1.305696] RPC: Registered udp transport module.
[ 1.307530] RPC: Registered tcp transport module.
[ 1.308892] RPC: Registered tcp NFSv4.1 backchannel transport module.
[ 1.310808] pci 0000:00:00.0: Limiting direct PCI/PCI transfers
[ 1.313318] pci 0000:00:01.0: PIIX3: Enabling Passive Release
[ 1.315729] pci 0000:00:01.0: Activating ISA DMA hang workarounds
[ 1.326248] Unpacking initramfs...
[ 1.493488] Freeing initrd memory: 2080K (ffff88001fdd8000 - ffff88001ffe0000)
[ 1.504783] Scanning for low memory corruption every 60 seconds
[ 1.520346] futex hash table entries: 256 (order: 2, 16384 bytes)
[ 1.528654] audit: initializing netlink subsys (disabled)
[ 1.533442] audit: type=2000 audit(1677198849.532:1): initialized
[ 1.551033] HugeTLB registered 2 MB page size, pre-allocated 0 pages
[ 1.604265] VFS: Disk quotas dquot_6.6.0
[ 1.610890] VFS: Dquot-cache hash table entries: 512 (order 0, 4096 bytes)
[ 1.629836] NFS: Registering the id_resolver key type
[ 1.637792] Key type id_resolver registered
[ 1.641550] Key type id_legacy registered
[ 1.664294] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 251)
[ 1.673333] io scheduler noop registered
[ 1.676912] io scheduler deadline registered
[ 1.679651] io scheduler cfq registered (default)
[ 1.688313] pci_hotplug: PCI Hot Plug PCI Core version: 0.5
[ 1.699062] input: Power Button as /devices/LNXSYSTM:00/LNXPWRBN:00/input/input0
[ 1.708577] ACPI: Power Button [PWRF]
[ 1.719883] Serial: 8250/16550 driver, 4 ports, IRQ sharing enabled
[ 1.773453] 00:05: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A
[ 1.791145] Non-volatile memory driver v1.3
[ 1.798430] Linux agpgart interface v0.103
[ 1.806519] [drm] Initialized drm 1.1.0 20060810
[ 1.849436] loop: module loaded
[ 1.876007] scsi host0: ata_piix
[ 1.885059] scsi host1: ata_piix
[ 1.894429] ata1: PATA max MWDMA2 cmd 0x1f0 ctl 0x3f6 bmdma 0xc040 irq 14
[ 1.901345] ata2: PATA max MWDMA2 cmd 0x170 ctl 0x376 bmdma 0xc048 irq 15
[ 1.912066] e100: Intel(R) PRO/100 Network Driver, 3.5.24-k2-NAPI
[ 1.918406] e100: Copyright(c) 1999-2006 Intel Corporation
[ 1.921838] e1000: Intel(R) PRO/1000 Network Driver - version 7.3.21-k8-NAPI
[ 1.925363] e1000: Copyright (c) 1999-2006 Intel Corporation.
[ 2.086238] ata2.00: ATAPI: QEMU DVD-ROM, 2.5+, max UDMA/100
[ 2.099592] ata2.00: configured for MWDMA2
[ 2.126157] scsi 1:0:0:0: CD-ROM QEMU QEMU DVD-ROM 2.5+ PQ: 0 ANSI: 5
[ 2.163187] sr 1:0:0:0: [sr0] scsi3-mmc drive: 4x/4x cd/rw xa/form2 tray
[ 2.171543] cdrom: Uniform CD-ROM driver Revision: 3.20
[ 2.196266] sr 1:0:0:0: Attached scsi generic sg0 type 5
[ 2.468345] ACPI: PCI Interrupt Link [LNKC] enabled at IRQ 11
[ 2.752526] tsc: Refined TSC clocksource calibration: 2807.989 MHz
[ 2.757853] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x2879bbf0a17, max_idle_ns: 440795299s
[ 2.792094] e1000 0000:00:03.0 eth0: (PCI:33MHz:32-bit) 52:54:00:12:34:56
[ 2.800831] e1000 0000:00:03.0 eth0: Intel(R) PRO/1000 Network Connection
[ 2.805076] sky2: driver version 1.30
[ 2.809719] ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver
[ 2.814967] ehci-pci: EHCI PCI platform driver
[ 2.819211] ohci_hcd: USB 1.1 'Open' Host Controller (OHCI) Driver
[ 2.822941] ohci-pci: OHCI PCI platform driver
[ 2.825612] uhci_hcd: USB Universal Host Controller Interface driver
[ 2.830529] usbcore: registered new interface driver usblp
[ 2.837703] usbcore: registered new interface driver usb-storage
[ 2.843127] i8042: PNP: PS/2 Controller [PNP0303:KBD,PNP0f13:MOU] at 0x60,0x64 irq 1,12
[ 2.852918] serio: i8042 KBD port at 0x60,0x64 irq 1
[ 2.859595] serio: i8042 AUX port at 0x60,0x64 irq 12
[ 2.865018] mousedev: PS/2 mouse device common for all mice
[ 2.885977] rtc_cmos 00:00: RTC can wake from S4
[ 2.898460] rtc_cmos 00:00: rtc core: registered rtc_cmos as rtc0
[ 2.905347] input: AT Translated Set 2 keyboard as /devices/platform/i8042/serio0/input/input1
[ 2.914822] rtc_cmos 00:00: alarms up to one day, y3k, 114 bytes nvram, hpet irqs
[ 2.919976] device-mapper: ioctl: 4.34.0-ioctl (2015-10-28) initialised: dm-devel@redhat.com
[ 2.926109] hidraw: raw HID events driver (C) Jiri Kosina
[ 2.946969] usbcore: registered new interface driver usbhid
[ 2.954385] usbhid: USB HID core driver
[ 2.975412] Netfilter messages via NETLINK v0.30.
[ 2.998788] nf_conntrack version 0.5.0 (3888 buckets, 15552 max)
[ 3.012170] ctnetlink v0.93: registering with nfnetlink.
[ 3.036351] ip_tables: (C) 2000-2006 Netfilter Core Team
[ 3.055162] Initializing XFRM netlink socket
[ 3.116729] NET: Registered protocol family 10
[ 3.152990] ip6_tables: (C) 2000-2006 Netfilter Core Team
[ 3.169103] sit: IPv6 over IPv4 tunneling driver
[ 3.192919] NET: Registered protocol family 17
[ 3.208213] Key type dns_resolver registered
[ 3.225023] microcode: AMD CPU family 0x6 not supported
[ 3.243247] registered taskstats version 1
[ 3.270292] Magic number: 11:803:557
[ 3.283291] console [netcon0] enabled
[ 3.288475] netconsole: network logging started
[ 3.304035] ALSA device list:
[ 3.310234] No soundcards found.
[ 3.569557] Freeing unused kernel memory: 1080K (ffffffff81f1f000 - ffffffff8202d000)
[ 3.576171] Write protecting the kernel read-only data: 14336k
[ 3.597761] Freeing unused kernel memory: 1516K (ffff880001885000 - ffff880001a00000)
[ 3.639881] Freeing unused kernel memory: 1332K (ffff880001cb3000 - ffff880001e00000)
[ 3.766144] clocksource: Switched to clocksource tsc
[ 3.783079] input: ImExPS/2 Generic Explorer Mouse as /devices/platform/i8042/serio1/input/input3
[ 4.211896] mdev (928) used greatest stack depth: 14088 bytes left
Please press Enter to activate this console.
/ # ls
bin etc init lib64 proc sbin tmp var
dev go.sh lib linuxrc root sys usr
/ # cat go.sh
#!/bin/sh
find . -print0 | cpio --null -ov --format=newc | gzip -9 >/share/initramfs.gz
/ # ls -al
total 12
drwxrwxr-x 14 1000 1000 0 Feb 24 00:34 .
drwxrwxr-x 14 1000 1000 0 Feb 24 00:34 ..
-rw------- 1 0 0 20 Feb 24 00:41 .ash_history
drwxrwxr-x 2 1000 1000 0 Oct 2 01:33 bin
drwxrwxr-x 7 1000 1000 0 Feb 24 00:34 dev
drwxrwxr-x 3 1000 1000 0 Oct 2 06:02 etc
-rwxrwxr-x 1 1000 1000 88 Feb 24 00:31 go.sh
-rwxrwxrwx 1 1000 1000 214 Feb 24 00:27 init
drwxrwxr-x 2 1000 1000 0 Oct 3 00:47 lib
drwxrwxr-x 2 1000 1000 0 Oct 3 00:47 lib64
lrwxrwxrwx 1 1000 1000 11 Oct 2 01:15 linuxrc -> bin/busybox
dr-xr-xr-x 57 0 0 0 Feb 24 00:34 proc
drwx------ 2 0 0 0 Oct 2 03:00 root
drwxrwxr-x 2 1000 1000 0 Oct 2 01:15 sbin
dr-xr-xr-x 12 0 0 0 Feb 24 00:34 sys
drwxrwxr-x 2 1000 1000 0 Oct 2 06:03 tmp
drwxrwxr-x 4 1000 1000 0 Oct 2 01:15 usr
drwxrwxr-x 2 1000 1000 0 Oct 2 06:03 var
/ #
```
上面 /init 只是一些 command 怕自己忘而已, 不是真正 init

[find - search for files in a directory hierarchy](https://www.man7.org/linux/man-pages/man1/find.1.html)
[cpio- copy files to and from archives](https://linux.die.net/man/1/cpio)
[10+ cpio command examples in Linux [Cheat Sheet]](https://www.golinuxcloud.com/cpio-command-in-linux/)
:::
```c=
/etc # ls
init.d inittab
/etc # cat inittab
# /etc/inittab init(8) configuration for BusyBox
#
# Copyright (C) 1999-2004 by Erik Andersen
#
#
# 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: :::
#
# : WARNING: This field has a non-traditional meaning for BusyBox init!
#
# The id field is used by BusyBox init to specify the controlling tty for
# the specified process to run on. The contents of this field are
# appended to "/dev/" and used as-is. There is no need for this field to
# be unique, although if it isn't you may have strange results. If this
# field is left blank, then the init's stdin/out will be used.
#
# : The runlevels field is completely ignored.
#
# : Valid actions include: sysinit, respawn, askfirst, wait, once,
# restart, ctrlaltdel, and shutdown.
#
# Note: askfirst acts just like respawn, but before running the specified
# process it displays the line "Please press Enter to activate this
# console." and then waits for the user to press enter before starting
# the specified process.
#
# Note: unrecognized actions (like initdefault) will cause init to emit
# an error message, and then go along with its business.
#
# : Specifies the process to be executed and it's command line.
#
# Note: BusyBox init works just fine without an inittab. If no inittab is
# found, it has the following default behavior:
# ::sysinit:/etc/init.d/rcS
# ::askfirst:/bin/sh
# ::ctrlaltdel:/sbin/reboot
# ::shutdown:/sbin/swapoff -a
# ::shutdown:/bin/umount -a -r
# ::restart:/sbin/init
# tty2::askfirst:/bin/sh
# tty3::askfirst:/bin/sh
# tty4::askfirst:/bin/sh
#
# Boot-time system configuration/initialization script.
# This is run first except when booting in single-user mode.
#
::sysinit:/etc/init.d/rcS
# /bin/sh invocations on selected ttys
#
# Note below that we prefix the shell commands with a "-" to indicate to the
# shell that it is supposed to be a login shell. Normally this is handled by
# login, but since we are bypassing login in this case, BusyBox lets you do
# this yourself...
#
# Start an "askfirst" shell on the console (whatever that may be)
::askfirst:-/bin/sh
# Start an "askfirst" shell on /dev/tty2-4
#tty2::askfirst:-/bin/sh
#tty3::askfirst:-/bin/sh
#tty4::askfirst:-/bin/sh
# /sbin/getty invocations for selected ttys
#tty4::respawn:/sbin/getty 38400 tty5
#tty5::respawn:/sbin/getty 38400 tty6
# Example of how to put a getty on a serial line (for a terminal)
#::respawn:/sbin/getty -L ttyS0 9600 vt100
#::respawn:/sbin/getty -L ttyS1 9600 vt100
#
# Example how to put a getty on a modem line.
#::respawn:/sbin/getty 57600 ttyS2
# Stuff to do when restarting the init process
::restart:/sbin/init
# Stuff to do before rebooting
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
/etc #
```
```c=
/etc/init.d # cat rcS
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
/sbin/mdev -s
/etc/init.d #
/etc/init.d # cat /proc/version
Linux version 4.4.50 (kclai@ubuntu16) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.12) ) #3 SMP2
/etc/init.d #
```
**mdev (busybox 的 udev) 透過 netlink socket 與 kernel 溝通 (util-linux/mdev.c, util-linux/util-linux/uevent.c)**
****
---
**產生 tmpfs(temporary file system)**
可以 放檔案 (RAM 中), 這裡 20m
/ # mkdir tmpmem
/ # mount -t tmpfs -o size=20m tmpfs /tmpmem
/ # mount
rootfs on / type rootfs (rw)
none on /proc type proc (rw,relatime)
none on /sys type sysfs (rw,relatime)
tmpfs on /tmpmem type tmpfs (rw,relatime,size=20480k)
---
**增加功能**
[rcS](https://www.796t.com/content/1546138399.html)
:::info
**mdev 是 busybox 版的** [udev- dynamic device management](https://linux.die.net/man/7/udev#:~:text=udev%287%29%3A%20dynamic%20device%20management%20-%20Linux%20man%20page,the%20%2Fdev%20directory%2C%20or%20it%20renames%20network%20interfaces.)
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
/sbin/mdev -s
:::
**multilines:**
https://stackoverflow.com/questions/12168523/how-does-operator-work-in-linux-shell
```c=
kclai@ubuntu16:~$ cat <<EOT >>ipshow.sh
> #!/bin
> ip addr show
> ip route
> EOT
kclai@ubuntu16:~$ cat ipshow.sh
#!/bin
ip addr show
ip route
kclai@ubuntu16:~$
kclai@ubuntu16:~$ cat <<EOT > ipshow.sh
> #!/bin/sh
> ip addr show
> ip route
> EOT
```
```c=
kclai@ubuntu16:~$ sh ipshow.sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 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 forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:d5:31:79 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fed5:3179/64 scope link
valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:22:41:71 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.31/24 scope global enp0s8
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe22:4171/64 scope link
valid_lft forever preferred_lft forever
default via 10.0.2.2 dev enp0s3
10.0.2.0/24 dev enp0s3 proto kernel scope link src 10.0.2.15
192.168.1.0/24 dev enp0s8 proto kernel scope link src 192.168.1.31
kclai@ubuntu16:~$
kclai@ubuntu16:~$ netstat -na |grep 192.168
tcp 0 0 192.168.1.31:22 192.168.1.10:14898 ESTABLISHED
tcp 0 0 192.168.1.31:22 192.168.1.10:14897 ESTABLISHED
tcp 0 48 192.168.1.31:22 192.168.1.10:1136 ESTABLISHED
tcp 0 0 192
```
**kclai@ubuntu16:~$ cat ipshow.sh**
#!/bin/sh
ip addr show
ip route
kclai@ubuntu16:~$
<span class="red">Remark. ">" or ">>" </span>
> is used to overwrite a file and >> is used to append to a file.
[Remark. << and <](https://stackoverflow.com/questions/12168523/how-does-operator-work-in-linux-shell)
:::
**inittab 放置 etc/
設定 rcS 與 /bin/sh**
```c=
# /etc/inittab init(8) configuration for BusyBox
#
# Copyright (C) 1999-2004 by Erik Andersen
#
#
# 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: :::
#
# : WARNING: This field has a non-traditional meaning for BusyBox init!
#
# The id field is used by BusyBox init to specify the controlling tty for
# the specified process to run on. The contents of this field are
# appended to "/dev/" and used as-is. There is no need for this field to
# be unique, although if it isn't you may have strange results. If this
# field is left blank, then the init's stdin/out will be used.
#
# : The runlevels field is completely ignored.
#
# : Valid actions include: sysinit, respawn, askfirst, wait, once,
# restart, ctrlaltdel, and shutdown.
#
# Note: askfirst acts just like respawn, but before running the specified
# process it displays the line "Please press Enter to activate this
# console." and then waits for the user to press enter before starting
# the specified process.
#
# Note: unrecognized actions (like initdefault) will cause init to emit
# an error message, and then go along with its business.
#
# : Specifies the process to be executed and it's command line.
#
# Note: BusyBox init works just fine without an inittab. If no inittab is
# found, it has the following default behavior:
# ::sysinit:/etc/init.d/rcS
# ::askfirst:/bin/sh
# ::ctrlaltdel:/sbin/reboot
# ::shutdown:/sbin/swapoff -a
# ::shutdown:/bin/umount -a -r
# ::restart:/sbin/init
# tty2::askfirst:/bin/sh
# tty3::askfirst:/bin/sh
# tty4::askfirst:/bin/sh
#
# Boot-time system configuration/initialization script.
# This is run first except when booting in single-user mode.
#
::sysinit:/etc/init.d/rcS
# /bin/sh invocations on selected ttys
#
# Note below that we prefix the shell commands with a "-" to indicate to the
# shell that it is supposed to be a login shell. Normally this is handled by
# login, but since we are bypassing login in this case, BusyBox lets you do
# this yourself...
#
# Start an "askfirst" shell on the console (whatever that may be)
::askfirst:-/bin/sh
# Start an "askfirst" shell on /dev/tty2-4
#tty2::askfirst:-/bin/sh
#tty3::askfirst:-/bin/sh
#tty4::askfirst:-/bin/sh
# /sbin/getty invocations for selected ttys
#tty4::respawn:/sbin/getty 38400 tty5
#tty5::respawn:/sbin/getty 38400 tty6
# Example of how to put a getty on a serial line (for a terminal)
#::respawn:/sbin/getty -L ttyS0 9600 vt100
#::respawn:/sbin/getty -L ttyS1 9600 vt100
#
# Example how to put a getty on a modem line.
#::respawn:/sbin/getty 57600 ttyS2
# Stuff to do when restarting the init process
::restart:/sbin/init
# Stuff to do before rebooting
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
::shutdown:/sbin/swapoff -a
```
kclai@ubuntu16:/share/src/busybox-1.22.0/_install$ ls -al
:::info
total 48
drwxrwxr-x 12 kclai kclai 4096 Oct 2 14:03 .
drwxr-xr-x 35 kclai kclai 4096 Oct 2 09:15 ..
drwxrwxr-x 2 kclai kclai 4096 Oct 2 09:33 bin
drwxrwxr-x 2 kclai kclai 4096 Oct 2 09:43 dev
drwxrwxr-x 3 kclai kclai 4096 Oct 2 14:02 etc
drwxrwxr-x 2 kclai kclai 4096 Oct 2 09:23 lib
lrwxrwxrwx 1 kclai kclai 11 Oct 2 09:15 linuxrc -> bin/busybox
drwxrwxr-x 2 kclai kclai 4096 Oct 2 09:40 proc
drwxrwxr-x 2 kclai kclai 4096 Oct 2 09:15 sbin
drwxrwxr-x 2 kclai kclai 4096 Oct 2 09:40 sys
drwxrwxr-x 2 kclai kclai 4096 Oct 2 14:03 tmp
drwxrwxr-x 4 kclai kclai 4096 Oct 2 09:15 usr
drwxrwxr-x 2 kclai kclai 4096 Oct 2 14:03 var
kclai@ubuntu16:/share/src/busybox-1.22.0/_install$
:::
**用 chroot 測一下**
:::info
kclai@ubuntu16:/share/src/busybox-1.22.0/_install$ sudo chroot /share/src/busybox-1.22.0/_install /bin/ash
chroot: failed to run command ‘/bin/ash’: No such file or directory
:::
**錯了~~~xxx**
kclai@ubuntu16:/share/src/busybox-1.22.0/_install$ ldd bin/busybox
linux-vdso.so.1 => (0x00007fffcb9e2000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f9e4b9db000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9e4b611000)
/lib64/ld-linux-x86-64.so.2 (0x00007f9e4bce4000)
kclai@ubuntu16:/share/src/busybox-1.22.0/_install$
...
**/lib64/ld-linux-x86-64.so.2 我 cp 至錯誤路徑!!!**
mkdir lib64
**因為 沒有 bash
用 ash 代替**
**完工!! [[choot]](https://www.man7.org/linux/man-pages/man1/chroot.1.html)) 測試**
:::info
kclai@ubuntu16:/share/src/busybox-1.22.0/_install$ sudo chroot . /bin/ash
/ #
/ # ls
bin etc lib64 proc sys usr
dev lib linuxrc sbin tmp var
/ # ls -al
total 52
drwxrwxr-x 13 1000 1000 4096 Oct 3 00:46 .
drwxrwxr-x 13 1000 1000 4096 Oct 3 00:46 ..
drwxrwxr-x 2 1000 1000 4096 Oct 2 01:33 bin
drwxrwxr-x 2 1000 1000 4096 Oct 3 00:23 dev
drwxrwxr-x 3 1000 1000 4096 Oct 2 06:02 etc
drwxrwxr-x 2 1000 1000 4096 Oct 3 00:47 lib
drwxrwxr-x 2 1000 1000 4096 Oct 3 00:47 lib64
lrwxrwxrwx 1 1000 1000 11 Oct 2 01:15 linuxrc -> bin/busybox
drwxrwxr-x 2 1000 1000 4096 Oct 2 01:40 proc
drwxrwxr-x 2 1000 1000 4096 Oct 2 01:15 sbin
drwxrwxr-x 2 1000 1000 4096 Oct 2 01:40 sys
drwxrwxr-x 2 1000 1000 4096 Oct 2 06:03 tmp
drwxrwxr-x 4 1000 1000 4096 Oct 2 01:15 usr
drwxrwxr-x 2 1000 1000 4096 Oct 2 06:03 var
/ #
/ # exit
kclai@ubuntu16:/share/src/busybox-1.22.0/_install$
:::
[cpio- copy files to and from archives](https://linux.die.net/man/1/cpio)
[10+ cpio command examples in Linux [Cheat Sheet]](https://www.golinuxcloud.com/cpio-command-in-linux/)
---
**Build rootfs**
:::info
重要:
[Using the initial RAM disk (initrd)](https://docs.kernel.org/admin-guide/initrd.html)
:::
**kclai@ubuntu16:/share/src/busybox-1.22.0/_install$ find . | cpio -o --format=newc > ../rootfs.img**
kclai@ubuntu16:/share/x86$ ls
bzImage rootfs.img.gz
kclai@ubuntu16:/share/x86$
:::info
kclai@ubuntu16:/share/src/busybox-1.22.0/_install$ find . | cpio --quiet -H newc -o | gzip -9 -n > /share/imagefile.img.gz
kclai@ubuntu16:/share/src/busybox-1.22.0/_install$
:::
**觀察 rootfs 大小**
**du -ah _install**
...
0 _install/bin/ipcalc
0 _install/bin/dnsdomainname
840K _install/bin
4.0K _install/var
4.0K _install/dev
**3.9M _install/**
[dd 指令, 備份及回復資料工具](https://www.ltsplus.com/linux/dd-backup-tool)
**注意
1. 大小要夠 rootfs 放入**
2. **rootfs 有時 要重新 dd, 否則可能愈來愈大**~因為 dd if=/dev/zero of=/tmp/rootfs-file bs=1k count=4096
**剩餘都是 0 壓縮 更有效, 否則留一堆垃圾資料**~
**dd if=/dev/zero of=/tmp/rootfs-file bs=1k count=4096
mkfs.ext2 /tmp/rootfs-file 4096**
:::info
kclai@ubuntu16:/share/src/busybox-1.22.0$ mkfs.ext2 /tmp/rootfs-file 4096
mke2fs 1.42.13 (17-May-2015)
Discarding device blocks: done
Creating filesystem with 4096 1k blocks and 1024 inodes
Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
kclai@ubuntu16:/share/src/busybox-1.22.0$
:::
**將 檔案 /tmp/rootfs-file mount 成 file system**
sudo mount -o loop -t ext2 /tmp/rootfs-file /share/rootfs
kclai@ubuntu16:/share/src/busybox-1.22.0/_install$ sudo cp -R * /share/rootfs
kclai@ubuntu16:/share$ sudo umount /share/rootfs
[sudo] password for kclai:
kclai@ubuntu16:/share$
**此時 我們有 沒壓縮 的 rootfs: /tmp/rootfs-file**
---
loop device
[losetup - set up and control loop devices](https://www.man7.org/linux/man-pages/man8/losetup.8.html)
```c=
# dd if=/dev/zero of=~/file.img bs=1024k count=10
# losetup --find --show ~/file.img
/dev/loop0
# mkfs -t ext2 /dev/loop0
# mount /dev/loop0 /mnt
...
# umount /dev/loop0
# losetup --detach /dev/loop0
```
---
[Using the initial RAM disk (initrd)](https://docs.kernel.org/admin-guide/initrd.html)
[10+ losetup command examples in Linux [Cheat Sheet]](https://www.golinuxcloud.com/losetup-command-in-linux/)
Study https://lxr.linux.no/linux+v4.5/drivers/char/mem.c
<h1>ARM version</h1>
[安裝arm-2013.11-33-arm-none-linux-gnueabi toolchain 與 小的測試 rootfs, init 給 QEMU](https://hackmd.io/@pingulinux/toolchain)
HW.
剩下 留給讀者 用QEMU 自行測試