[英]Failed to execute echo command in system() in INIT in root file system
主機 :英特爾奔騰4,RHEL 6
目標 :使用我自己的小型rootffile文件系統運行Linux的ARM Cortex A9
我按照此處的說明開發了一個最小的initramfs文件系統: 極簡主義的Initramfs和Initrd 。
所以我的initrmafs只有以下幾件事:
1-控制台
2-初始化
init是具有以下代碼的二進制文件:
* myinit.c
* Build instructions:
* ${CROSS_COMPILE}gcc -static init.c -o init
* */
#include <stdio.h>
int
main ()
{
printf ("\n");
printf ("Hello world from %s!\n", __FILE__);
while (1) { }
return 0;
}
這在Linux內核上運行良好,並且在最后的日志消息中,我收到了hello world消息。
但是我想要的是在打印hello world之后, echo
命令應該起作用並執行以下操作:
echo 10 > test.txt
echo "$(cat test.txt)"
所以我所做的是:
#include <stdlib.h>
#include <stdio.h>
int main ()
{
printf ("\n");
printf ("Hello world from %s!\n", __FILE__);
system("echo 10 > test.txt");
system("echo \"$(cat test.txt)\"");
while (1) { }
return 0;
}
我正在使用以下命令交叉編譯代碼:
arm-xilinx-linux-gnueabi-gcc -static echotest.c -o init
Hello world正在正確打印,但是由於文件的內容(例如10未在打印),所以echo無法正常工作。
注意:請注意,這次我的文件系統中有以下內容,即initramfs:
1-控制台
2-init(第二程序的二進制)
3- test.txt(一個空文件)
4-具有二進制echo
的文件夾箱
5-具有二進制cat
的文件夾箱
從這些聲音中,什么都沒有。 主要思想是擁有一個最小的文件系統,僅包含我的應用程序真正需要的東西。
我從正常的linux系統復制的二進制cat和echo。
請幫助我正確運行上述echo命令?
更新資料
我最新的代碼是在我掛載/ bin的地方:
#include <stdlib.h>
#include <sys/mount.h>
#include <errno.h>
#include <stdio.h>
void mount_sys() {
if (0 != mount("none", "/bin", "sysfs", 0, "")) {
perror("there is an error in mounting \n"); /* handle error */
}
printf("mounting successful");
}
int main ()
{
mount_sys();
printf ("\n");
printf ("Hello world from %s!\n", __FILE__);
system("echo 10 > test.txt");
system("echo \"$(cat test.txt)\"");
while (1) { }
return 0;
}
不幸的是,這也不起作用。 引導日志消息中的最后兩行是:
mounting successful
Hello world from echotest.c!
system()
要求/bin/sh
可用。
如果您真的希望最小,請使用fork
/ exec
代替。 諸如重定向之類的Shell構造將(顯然)不可用。
作為一種風格上的評論,即使沒有理智的恢復方法,始終檢查系統調用中的錯誤將是診斷問題的良好起點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.