Host : Intel pentium 4, RHEL 6
Target : ARM Cortex A9 running linux with my own small sized rootffile file system
I developed a minimal initramfs filesystem following the instructions here: Minimalist Initramfs and Initrd .
So my initrmafs has only following things:
1- console
2- init
The init is the binary having the following code:
* 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;
}
This works well with the linux kernel and in the log message at the end I get a hello world message.
But what I wanted is that after printing hello world, the echo
command should work and do the following:
echo 10 > test.txt
echo "$(cat test.txt)"
So what I did is:
#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;
}
I am cross compiling my code using the following command:
arm-xilinx-linux-gnueabi-gcc -static echotest.c -o init
Hello world is printing correctly but echo is not working as the content of the file ie 10 is not printing.
Note: Please note that this time I have following things in my file system ie initramfs:
1- console
2- init (binary for second program)
3- test.txt (an empty file)
4- a folder bin having the binary echo
5- a folder bin having the binary cat
Aprt from these, there is nothing. The main idea was to have a minimal file system, having only things which are really needed for my aplication.
The binary cat and echo I copied from a working linux system.
Kindly help me in correctly running the echo command as above?
Update
My latest code is as under where I am mounting /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;
}
Unfortunately this is also not working. The last two lines in the boot log message are:
mounting successful
Hello world from echotest.c!
system()
requires /bin/sh
to be available.
If you want really minimal, use fork
/ exec
instead. Shell constructs like redirection will then (obviously) not be available.
As a stylistic comment, always checking system calls for errors would be a good first start for diagnosing problems, even if there will be no sane way to recover.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.