Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mounting a file system using a C program

In my initramfs.cpio I have only these directories:

root@localhost extract]# ls 
dev  init  tmp sys 

dev has console and sys is empty.

init is a binary corresponding to program that I discussed in Accessing GPIO after kernel boots.

Now in the same GPIO program I would like to write code to mount a /sys. I understand it can be mounted using mount:

mount -t sysfs none /sys

How do I write a C program that will implement the above line. Please note that I do not have a file system; initramfs.cpio has empty folders: /sys, /tmp. I can put more empty folder if required. But I cannot put full file system.

My main intention To access GPIO using this program or otherwise, but without using a full file system. I dont need any other thing to run, but just want GPIO access (and LED blink)

like image 670
user2799508 Avatar asked Apr 21 '14 05:04

user2799508


People also ask

What is mounting in C?

mount() attaches the filesystem specified by source (which is often a pathname referring to a device, but can also be the pathname of a directory or file, or a dummy string) to the location (a directory or file) specified by the pathname in target .

How can a file system be mounted?

You can mount NFS file system resources by using a client-side service called automounting (or AutoFS), which enables a system to automatically mount and unmount NFS resources whenever you access them. The resource remains mounted as long as you remain in the directory and are using a file.

Which command is used to mount file systems?

Mounts all file systems specified in the /etc/vfstab file. The mountall command is run automatically when entering multiuser run states. Unmounts file systems and remote resources. Unmounts all file systems specified in the /etc/vfstab file.

What is file system discuss mounting with the help of an example?

Typically, a mount point is an empty directory. For instance, on a UNIX system, a file system containing a user's home directories might be mounted as /home; then, to access the directory structure within that file system, we could precede the directory names with ftiome, as in /homc/janc.


1 Answers

You use the mount(2) system call. From the manpage:

SYNOPSIS

  #include <sys/mount.h>

  int mount(const char *source, const char *target,
            const char *filesystemtype, unsigned long mountflags,
            const void *data);

So, in your C code, that'd look something like:

#include <sys/mount.h>

/* ... */

void mount_sys() {
    if (0 != mount("none", "/sys", "sysfs", 0, "")) {
        /* handle error */
    }
}

(That last empty string is where you'd pass mount options, but AFAIK sysfs doesn't take any.)

like image 163
derobert Avatar answered Nov 09 '22 13:11

derobert