How can I mount /dev from C? I'm writing a simple init, and I know this sounds stupid but I would like to have /dev mounted to another folder.
The manpages for mount() don't show dev as a supported option.
Asked
Active
Viewed 798 times
2
-
2Does this answer your question? [How to populate /dev directory when building my own initrd?](https://unix.stackexchange.com/questions/52713/how-to-populate-dev-directory-when-building-my-own-initrd) – sebasth Mar 15 '21 at 23:55
-
@sebasth Almost, but I need to be able to do it from C. – Mason M Mar 16 '21 at 15:28
2 Answers
4
The manpage for mount(2) only gives examples and mentions /proc/filesystems for an exhaustie list. This latter file lists devtmpfs which is what you are looking for.
The resulting C program would then be something like:
if (mount("-", "/.../dev", "devtmpfs", 0, NULL) != 0) perror("mount");
xhienne
- 17,075
- 2
- 52
- 68
-
-
1You're welcome! This is a minimal example and you may want to customize it by adding flags (mount options) like `MS_NOATIME`, `MS_NOEXEC`, etc. – xhienne Mar 16 '21 at 18:27
0
You can also have /dev mounted elsewhere by using the mount command.
If you want to use the mount system call directly, the man page says
Values for the filesystemtype argument supported by the kernel are listed in
/proc/filesystems.
If you are not sure which filesystem type you want on /dev look at your /etc/fstab or /proc/mounts, or do a strace on the mount command to find out how it uses the system call.
RalfFriedl
- 8,816
- 6
- 23
- 34