问题 
我使用自定义 initrd 在用户模式下编译了 Linux 内核 5.6,使用以下命令创建: 
- mkdir initrd
 
 - cd initrd
 
 - mkdir bin dev etc home mnt proc sys usr
 
 - mknod dev/console c 5 1
 
  复制代码 
使用初始化文件 
 
初始化/初始化.c 
- #include <stdio.h>
 
 - #include <stdlib.h>
 
 - #include <sys/mount.h>
 
 - #include <unistd.h>
 
  
- int main(int argc, char *argv[]) {
 
 -     printf("init\n");
 
  
-     mount("none", "/proc", "proc", MS_MGC_VAL, "");
 
 -     mount("none", "/sys", "sysfs", MS_MGC_VAL, "");
 
 -     mount("none", "/dev", "devtmpfs", MS_MGC_VAL, "");
 
  
-     if (access("/dev/ubda", F_OK) != -1) {
 
 -         printf("/dev/ubda exists\n");
 
 -     } else {
 
 -         printf("/dev/ubda not exists\n");
 
 -     }
 
  
-     return EXIT_SUCCESS;
 
 - }
 
 
  复制代码 
该程序通过选项检查磁盘。 
 
使用 ubd0=... 编译。 
 
毕竟我编译了内核 
 
gcc -static -o init init.c 
- make mrproper
 
 - make mrproper ARCH=um
 
 - make defconfig ARCH=um
 
 - make menuconfig ARCH=um
 
 - make linux ARCH=um
 
  复制代码 
我更改了文件中的以下选项(使用 menuconfig) 
 
.config 
- CONFIG_BLK_DEV_INITRD=y
 
 - CONFIG_INITRAMFS_SOURCE="initrd"
 
 - CONFIG_RD_GZIP=y
 
 - CONFIG_RD_BZIP2=y
 
 - CONFIG_RD_LZMA=y
 
 - CONFIG_RD_XZ=y
 
 - CONFIG_RD_LZO=y
 
 - CONFIG_RD_LZ4=y
 
  复制代码 
毕竟我试过跑步,一切看起来都很好,除了退出 
 
./linux 内存=32M 
 
如何优雅地退出? 
 
回答 
init 进程绝不能退出。退出init的正确方法是关闭。 
- #include <linux/reboot.h>
 
  
- int main(int argc, char *argv[]) {
 
 -   // ...
 
 -   // return EXIT_SUCCESS;
 
  
-   sync();
 
 -   reboot(LINUX_REBOOT_MAGIC1, 
 
 -      LINUX_REBOOT_MAGIC2, 
 
 -      LINUX_REBOOT_CMD_POWER_OFF, 0);
 
 - }
 
  复制代码 
在 glibc 和大多数可选 libcs?? ??(包括 uclibc、dietlibc、musl 和其他一些)下,所涉及的一些常量已被赋予符号名称 RBú*,并且库调用是系统调用的单参数包装器: 
- #include <sys/reboot.h>
 
  
- int main(int argc, char *argv[]) {
 
 -   // ...
 
 -   // return EXIT_SUCCESS;
 
  
-   sync();
 
 -   reboot(RB_POWER_OFF);
 
 - }
 
  复制代码 
对于停止或重新启动系统的 cmd 值,将不会返回对 reboot() 的成功调用。 
 
 
 
 |